Skip to content

Commit

Permalink
Module-level static declarations HaxeFoundation/haxe#8460
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHaxe committed Jun 27, 2020
1 parent f7215ff commit f983e8e
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/haxeparser/Data.hx
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,10 @@ typedef Definition<A,B> = {
enum TypeDef {
EClass(d:Definition<ClassFlag, Array<Field>>);
EEnum(d:Definition<EnumFlag, Array<EnumConstructor>>);
ETypedef(d:Definition<EnumFlag, ComplexType>);
EAbstract(a:Definition<AbstractFlag, Array<Field>>);
EStatic(s:Definition<StaticFlag, FieldType>);
EImport(sl:Array<{pack:String, pos:Position}>, mode:ImportMode);
ETypedef(d:Definition<EnumFlag, ComplexType>);
EUsing(path:TypePath);
}

Expand Down Expand Up @@ -230,6 +231,14 @@ enum EnumFlag {
EExtern;
}

enum StaticFlag {
SDynamic;
SFinal;
SInline;
SMacro;
SPrivate;
}

enum ImportMode {
INormal;
IAsName(s:String);
Expand Down
23 changes: 23 additions & 0 deletions src/haxeparser/DefinitionConverter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class DefinitionConverter {
case EEnum(d): convertEnum(d);
case EAbstract(d): convertAbstract(d);
case ETypedef(d): convertTypedef(d);
case EStatic(s): convertStatic(s);
case _: throw 'Cannot convert $t';
}
td.pack = pack;
Expand Down Expand Up @@ -111,4 +112,26 @@ class DefinitionConverter {
def.fields = en.data.map(enumConstructorToClassField);
return def;
}


static function convertStatic(c:Definition<StaticFlag, FieldType>) {
var def = getGeneralDefinition(c);
var access:Array<Access>=[];
for (flag in c.flags) {
switch (flag) {
case SDynamic:
access.push(ADynamic);
case SFinal:
access.push(AFinal);
case SInline:
access.push(AInline);
case SMacro:
access.push(AMacro);
case SPrivate:
access.push(APrivate);
}
}
def.kind = TDField(c.data, access);
return def;
}
}
70 changes: 65 additions & 5 deletions src/haxeparser/HaxeParser.hx
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ class HaxeParser extends hxparse.Parser<HaxeTokenSource, Token> implements hxpar
}
}

function parseAbstract (doc, meta, flags:Array<{c:ClassFlag, e:EnumFlag, a:AbstractFlag}>) {
function parseAbstract (doc, meta, flags:Array<{c:ClassFlag, e:EnumFlag, a:AbstractFlag, pos:Position}>) {
return switch stream {
case [{tok:Kwd(KwdAbstract), pos:p1}, name = typeName(), tl = parseConstraintParams(), st = parseAbstractSubtype(), sl = parseRepeat(parseAbstractRelations)]:
var fl = switch stream {
Expand Down Expand Up @@ -572,6 +572,53 @@ class HaxeParser extends hxparse.Parser<HaxeTokenSource, Token> implements hxpar
{decl: EUsing(t), pos: punion(p1, p2)};
case [doc = getDoc(), meta = parseMeta(), c = parseCommonFlags()]:
switch stream {
case [{tok:Kwd(KwdFunction), pos:p1}, name = parseFunName(), pl = parseConstraintParams(), {tok:POpen}, al = psep(Comma, parseFunParam), {tok:PClose}, t = parseTypeOpt()]:
var e = switch stream {
case [e = toplevelExpr(), _ = semicolon()]:
{ expr: e, pos: e.pos };
case [{tok: Semicolon,pos:p}]:
{ expr: null, pos: p}
case _: unexpected();
}
var f = {
params: pl,
args: al,
ret: t,
expr: e.expr
}
{decl: EStatic({
name: name,
doc: doc,
meta: meta,
params: pl,
flags: c.map(function(i) return i.s),
data: FFun(f)
}), pos: punion(p1, e.pos)};
case [{tok:Kwd(KwdVar), pos:p1}, name = dollarIdent()]:
switch stream {
case [{tok:POpen}, i1 = propertyIdent(), {tok:Comma}, i2 = propertyIdent(), {tok:PClose}]:
var t = parseTypeOpt();
var e = parseVarFieldAssignment();
{decl: EStatic({
name: name.name,
doc: doc,
meta: meta,
params: [],
flags: c.map(function(i) return i.s),
data: FProp(i1,i2,t,e.expr)
}), pos: punion(p1, e.pos)};

case [t = parseTypeOpt()]:
var e = parseVarFieldAssignment();
{decl: EStatic({
name: name.name,
doc: doc,
meta: meta,
params: [],
flags: c.map(function(i) return i.s),
data: FVar(t,e.expr)
}), pos: punion(p1, e.pos)};
}
case [{tok:Kwd(KwdEnum), pos:p1}]:
switch stream {
case [a = parseAbstract(doc, [{name: ":enum", params: [], pos: p1}].concat(meta), c)]:
Expand Down Expand Up @@ -611,6 +658,16 @@ class HaxeParser extends hxparse.Parser<HaxeTokenSource, Token> implements hxpar

case [a = parseAbstract(doc, meta, c)]:
{decl: a.decl, pos: a.pos};

case [name = dollarIdent(), t = parseTypeOpt(), e = parseVarFieldAssignment()]:
{decl: EStatic({
name: name.name,
doc: doc,
meta: meta,
params: [],
flags: c.map(function(i) return i.s),
data: FVar(t,e.expr)
}), pos: punion(c[0].pos, e.pos)};
}
}
}
Expand Down Expand Up @@ -705,11 +762,14 @@ class HaxeParser extends hxparse.Parser<HaxeTokenSource, Token> implements hxpar
return parseRepeat(parseClassField);
}

function parseCommonFlags():Array<{c:ClassFlag, e:EnumFlag, a:AbstractFlag}> {
function parseCommonFlags():Array<{c:ClassFlag, e:EnumFlag, a:AbstractFlag, s:StaticFlag, pos:Position}> {
return switch stream {
case [{tok:Kwd(KwdPrivate)}, l = parseCommonFlags()]: apush(l, {c:HPrivate, e:EPrivate, a:APrivAbstract});
case [{tok:Kwd(KwdExtern)}, l = parseCommonFlags()]: apush(l, {c:HExtern, e:EExtern, a:AExtern});
case [{tok:Kwd(KwdFinal)}, l = parseCommonFlags()]: apush(l, {c:HFinal, e:null, a:null});
case [{tok:Kwd(KwdPrivate), pos:p}, l = parseCommonFlags()]: aunshift(l, {c:HPrivate, e:EPrivate, a:APrivAbstract, s:SPrivate, pos:p});
case [{tok:Kwd(KwdExtern), pos:p}, l = parseCommonFlags()]: aunshift(l, {c:HExtern, e:EExtern, a:AExtern, s:null, pos:p});
case [{tok:Kwd(KwdFinal), pos:p}, l = parseCommonFlags()]: aunshift(l, {c:HFinal, e:null, a:null, s:SFinal, pos:p});
case [{tok:Kwd(KwdInline), pos:p}, l = parseCommonFlags()]: aunshift(l, {c:null, e:null, a:null, s:SInline, pos:p});
case [{tok:Kwd(KwdDynamic), pos:p}, l = parseCommonFlags()]: aunshift(l, {c:null, e:null, a:null, s:SDynamic, pos:p});
case [{tok:Kwd(KwdMacro), pos:p}, l = parseCommonFlags()]: aunshift(l, {c:null, e:null, a:null, s:SMacro, pos:p});
case _: [];
}
}
Expand Down
33 changes: 33 additions & 0 deletions test/Test.hx
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,39 @@ class Test extends haxe.unit.TestCase {
eeq("var @:a(b) c:D = e");
}

function testModuleStatics() {
peq('final finalInit = "finalInit";');
peq('final finalHintInit : String = "finalHintInit";');
peq('inline final inlineFinalInit = "inlineFinalInit";');
peq('inline final inlineFinalHintInit : String = "inlineFinalHintInit";');
peq('private final privateFinalInit = "privateFinalInit";');
peq('private final privateFinalHintInit : String = "privateFinalHintInit";');
peq('private inline final privateInlineFinalInit = "privateInlineFinalInit";');
peq('private inline final privateInlineFinalHintInit : String = "privateInlineFinalHintInit";');
peq('inline private final inlinePrivateFinalInit = "inlinePrivateFinalInit";');
peq('inline private final inlinePrivateFinalHintInit : String = "inlinePrivateFinalHintInit";');
peq('var varInit = "varInit";');
peq('var varInitHint : String = "varInitHint";');
peq('var varHint : String;');
peq('inline var inlineVarInit = "inlineVarInit";');
peq('inline var inlineVarInitHint : String = "inlineVarInitHint";');
peq('private var privateVarInit = "privateVarInit";');
peq('private var privateVarInitHint : String = "privateVarInitHint";');
peq('private var privateVarHint : String;');
peq('private inline var privateInlineVarInit = "privateInlineVarInit";');
peq('private inline var privateInlineVarInitHint : String = "privateInlineVarInitHint";');
peq('inline private var inlinePrivateVarInit = "inlinePrivateVarInit";');
peq('inline private var inlinePrivateVarInitHint : String = "inlinePrivateVarInitHint";');
peq('function func() return "func";');
peq('private function privateFunc() return "privateFunc";');
peq('private inline function privateInlineFunc() return "privateInlineFunc";');
peq('inline private function inlinePrivateFunc() return "inlinePrivateFunc";');
peq('dynamic function dynamicFunc() return "dynamicFunc";');
peq('private dynamic function privateDynamicFunc() return "privateDynamicFunc";');
peq('dynamic private function dynamicPrivateFunc() return "dynamicPrivateFunc";');
peq('@:isVar var prop(get, set) : String = "prop";function get_prop() return prop + "-get";function set_prop(value) return prop = value + "-set";');
}

static function parseExpr(inputCode:String, ?p:haxe.PosInfos) {
var parser = new haxeparser.HaxeParser(byte.ByteData.ofString(inputCode), '${p.methodName}:${p.lineNumber}');
var expr = parser.expr();
Expand Down

0 comments on commit f983e8e

Please sign in to comment.