diff --git a/src/haxeparser/Data.hx b/src/haxeparser/Data.hx index e4d77dd..9d63e0d 100644 --- a/src/haxeparser/Data.hx +++ b/src/haxeparser/Data.hx @@ -197,9 +197,10 @@ typedef Definition = { enum TypeDef { EClass(d:Definition>); EEnum(d:Definition>); + ETypedef(d:Definition); EAbstract(a:Definition>); + EStatic(s:Definition); EImport(sl:Array<{pack:String, pos:Position}>, mode:ImportMode); - ETypedef(d:Definition); EUsing(path:TypePath); } @@ -230,6 +231,14 @@ enum EnumFlag { EExtern; } +enum StaticFlag { + SDynamic; + SFinal; + SInline; + SMacro; + SPrivate; +} + enum ImportMode { INormal; IAsName(s:String); diff --git a/src/haxeparser/DefinitionConverter.hx b/src/haxeparser/DefinitionConverter.hx index d6310b8..47b12e0 100644 --- a/src/haxeparser/DefinitionConverter.hx +++ b/src/haxeparser/DefinitionConverter.hx @@ -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; @@ -111,4 +112,26 @@ class DefinitionConverter { def.fields = en.data.map(enumConstructorToClassField); return def; } + + + static function convertStatic(c:Definition) { + var def = getGeneralDefinition(c); + var access:Array=[]; + 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; + } } \ No newline at end of file diff --git a/src/haxeparser/HaxeParser.hx b/src/haxeparser/HaxeParser.hx index a747363..0d9f641 100644 --- a/src/haxeparser/HaxeParser.hx +++ b/src/haxeparser/HaxeParser.hx @@ -542,7 +542,7 @@ class HaxeParser extends hxparse.Parser 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 { @@ -572,6 +572,53 @@ class HaxeParser extends hxparse.Parser 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)]: @@ -611,6 +658,16 @@ class HaxeParser extends hxparse.Parser 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)}; } } } @@ -705,11 +762,14 @@ class HaxeParser extends hxparse.Parser 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 _: []; } } diff --git a/test/Test.hx b/test/Test.hx index d8ab884..8a171b2 100644 --- a/test/Test.hx +++ b/test/Test.hx @@ -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();