From 5f3d79c4561eea42aae43ee1b3d58e043358c03e Mon Sep 17 00:00:00 2001 From: AlexHaxe Date: Wed, 5 Jul 2023 19:14:19 +0200 Subject: [PATCH] updated enum abstract handling to match recent Haxe changes --- haxe_libraries/test-adapter.hxml | 6 +- src/haxeparser/Data.hx | 13 +- src/haxeparser/DefinitionConverter.hx | 291 +++++++++++++------------- src/haxeparser/HaxeParser.hx | 37 ++-- test/Test.hx | 4 + 5 files changed, 183 insertions(+), 168 deletions(-) diff --git a/haxe_libraries/test-adapter.hxml b/haxe_libraries/test-adapter.hxml index f041683..2fde4ca 100644 --- a/haxe_libraries/test-adapter.hxml +++ b/haxe_libraries/test-adapter.hxml @@ -1,5 +1,5 @@ -# @install: lix --silent download "haxelib:/test-adapter#2.0.4" into test-adapter/2.0.4/haxelib +# @install: lix --silent download "haxelib:/test-adapter#2.0.5" into test-adapter/2.0.5/haxelib -lib json2object --cp ${HAXE_LIBCACHE}/test-adapter/2.0.4/haxelib/ --D test-adapter=2.0.4 +-cp ${HAXE_LIBCACHE}/test-adapter/2.0.5/haxelib/ +-D test-adapter=2.0.5 --macro _testadapter.Macro.init() \ No newline at end of file diff --git a/src/haxeparser/Data.hx b/src/haxeparser/Data.hx index 0aca8d2..5638f5e 100644 --- a/src/haxeparser/Data.hx +++ b/src/haxeparser/Data.hx @@ -242,7 +242,7 @@ enum TypeDef { EClass(d:Definition>); EEnum(d:Definition>); ETypedef(d:Definition); - EAbstract(a:Definition>); + EAbstract(a:Definition>); EStatic(s:Definition); EImport(sl:Array<{pack:String, pos:Position}>, mode:ImportMode); EUsing(path:TypePath); @@ -264,11 +264,12 @@ enum ClassFlag { } enum AbstractFlag { - APrivAbstract; - AFromType(ct:ComplexType); - AToType(ct:ComplexType); - AIsType(ct:ComplexType); - AExtern; + AbPrivate; + AbFrom(ct:ComplexType); + AbTo(ct:ComplexType); + AbOver(ct:ComplexType); + AbExtern; + AbEnum; } enum EnumFlag { diff --git a/src/haxeparser/DefinitionConverter.hx b/src/haxeparser/DefinitionConverter.hx index 7c2628a..963a560 100644 --- a/src/haxeparser/DefinitionConverter.hx +++ b/src/haxeparser/DefinitionConverter.hx @@ -1,140 +1,151 @@ -package haxeparser; - -import haxeparser.Data; -import haxe.macro.Expr; - -using Lambda; - -class DefinitionConverter { - static public function convertTypeDef(pack:Array, t:TypeDef):TypeDefinition { - var td = switch(t) { - case EClass(d): convertClass(d); - 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; - return td; - } - - static function getGeneralDefinition(d:Definition) { - return { - pack: [], - name: d.name, - meta: d.meta, - params: d.params, - pos: null, - isExtern: false, - kind: TDStructure, - fields: [], - doc: null - } - } - - static function convertClass(c:Definition>) { - var def = getGeneralDefinition(c); - var isInterface = false; - var isFinal = false; - var isAbstract = false; - var superClass = null; - var implementsList = []; - for (flag in c.flags) { - switch(flag) { - case HInterface: isInterface = true; - case HExtern: def.isExtern = true; - case HFinal: isFinal = true; - case HExtends(t): superClass = t; - case HImplements(t): implementsList.push(t); - case HPrivate: // TODO: ignored? - case HAbstract: isAbstract = true; - } - } - def.fields = c.data; - def.kind = TDClass(superClass, implementsList, isInterface, isFinal, isAbstract); - return def; - } - - static function convertTypedef(t:Definition) { - var def = getGeneralDefinition(t); - def.kind = TDAlias(t.data); - return def; - } - - static function convertAbstract(a:Definition>) { - var def = getGeneralDefinition(a); - var to = []; - var from = []; - var thisT = null; - for (flag in a.flags) { - switch(flag) { - case AFromType(t): from.push(t); - case AToType(t): to.push(t); - case AIsType(t): thisT = t; - case APrivAbstract: - case AExtern: def.isExtern = true; - } - } - def.fields = a.data; - def.kind = TDAbstract(thisT, from, to); - return def; - } - - static function enumConstructorToClassField(ctor:EnumConstructor) { - var kind = if(ctor.args.length == 0) { - FVar(ctor.type, null); - } else { - FFun({ - params: ctor.params, - expr: null, - ret: ctor.type, - args: ctor.args.map(function(arg) return { - name: arg.name, - opt: arg.opt, - type: arg.type, - value: null, - meta: [] - }) - }); - } - return { - name: ctor.name, - doc: ctor.doc, - access: [], - pos: ctor.pos, - meta: ctor.meta, - kind: kind - } - } - - static function convertEnum(en:Definition>) { - var def = getGeneralDefinition(en); - def.kind = TDEnum; - 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); - case SOverload: - access.push(AOverload); - } - } - def.kind = TDField(c.data, access); - return def; - } -} +package haxeparser; + +import haxeparser.Data; +import haxe.macro.Expr; + +using Lambda; + +class DefinitionConverter { + static public function convertTypeDef(pack:Array, t:TypeDef):TypeDefinition { + var td = switch(t) { + case EClass(d): convertClass(d); + 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; + return td; + } + + static function getGeneralDefinition(d:Definition) { + return { + pack: [], + name: d.name, + meta: d.meta, + params: d.params, + pos: null, + isExtern: false, + kind: TDStructure, + fields: [], + doc: null + } + } + + static function convertClass(c:Definition>) { + var def = getGeneralDefinition(c); + var isInterface = false; + var isFinal = false; + var isAbstract = false; + var superClass = null; + var implementsList = []; + for (flag in c.flags) { + switch(flag) { + case HInterface: isInterface = true; + case HExtern: def.isExtern = true; + case HFinal: isFinal = true; + case HExtends(t): superClass = t; + case HImplements(t): implementsList.push(t); + case HPrivate: // TODO: ignored? + case HAbstract: isAbstract = true; + } + } + def.fields = c.data; + def.kind = TDClass(superClass, implementsList, isInterface, isFinal, isAbstract); + return def; + } + + static function convertTypedef(t:Definition) { + var def = getGeneralDefinition(t); + def.kind = TDAlias(t.data); + return def; + } + + static function convertAbstract(a:Definition>) { + var def = getGeneralDefinition(a); + var to = []; + var from = []; + var flags = []; + var thisT = null; + + for (flag in a.flags) { + switch(flag) { + case AbFrom(t): from.push(t); + case AbTo(t): to.push(t); + case AbOver(t): thisT = t; + case AbPrivate: + #if (haxe >= version ("5.0.0-alpha.1")) + case AbEnum: flags.push(haxe.macro.Expr.AbstractFlag.AbEnum); + #else + case AbEnum: def.meta = [{name: ":enum", params: [], pos: null}].concat(def.meta); + #end + case AbExtern: def.isExtern = true; + } + } + def.fields = a.data; + #if (haxe >= version ("5.0.0-alpha.1")) + def.kind = TDAbstract(thisT, flags, from, to); + #else + def.kind = TDAbstract(thisT, from, to); + #end + return def; + } + + static function enumConstructorToClassField(ctor:EnumConstructor) { + var kind = if(ctor.args.length == 0) { + FVar(ctor.type, null); + } else { + FFun({ + params: ctor.params, + expr: null, + ret: ctor.type, + args: ctor.args.map(function(arg) return { + name: arg.name, + opt: arg.opt, + type: arg.type, + value: null, + meta: [] + }) + }); + } + return { + name: ctor.name, + doc: ctor.doc, + access: [], + pos: ctor.pos, + meta: ctor.meta, + kind: kind + } + } + + static function convertEnum(en:Definition>) { + var def = getGeneralDefinition(en); + def.kind = TDEnum; + 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); + case SOverload: + access.push(AOverload); + } + } + def.kind = TDField(c.data, access); + return def; + } +} diff --git a/src/haxeparser/HaxeParser.hx b/src/haxeparser/HaxeParser.hx index d332918..a07d4cc 100644 --- a/src/haxeparser/HaxeParser.hx +++ b/src/haxeparser/HaxeParser.hx @@ -507,16 +507,15 @@ class HaxeParser extends hxparse.Parser implements hxpar case OpAdd | OpSub : {p: 3, left: left}; case OpShl | OpShr | OpUShr : {p: 4, left: left}; case OpOr | OpAnd | OpXor : {p: 5, left: left}; - case OpEq | OpNotEq | OpGt | OpLt | OpGte | OpLte : {p: 6, left: left}; - case OpInterval : {p: 7, left: left}; - case OpBoolAnd : {p: 8, left: left}; - case OpBoolOr : {p: 9, left: left}; #if (haxe >= version("4.3.0-rc.1")) - case OpArrow | OpNullCoal : {p: 10, left: right}; - #else - case OpArrow : {p: 10, left: right}; + case OpNullCoal: {p: 6, left: left}; #end - case OpAssign | OpAssignOp(_) : {p:11, left:right}; + case OpEq | OpNotEq | OpGt | OpLt | OpGte | OpLte : {p: 7, left: left}; + case OpInterval : {p: 8, left: left}; + case OpBoolAnd : {p: 9, left: left}; + case OpBoolOr : {p: 10, left: left}; + case OpArrow : {p: 11, left: right}; + case OpAssign | OpAssignOp(_) : {p:12, left:right}; } } @@ -740,7 +739,7 @@ class HaxeParser extends hxparse.Parser implements hxpar } } - function parseAbstract (doc, meta, flags:Array<{c:ClassFlag, e:EnumFlag, a:AbstractFlag, s:StaticFlag, pos:Position}>, p1) { + function parseAbstract (doc, meta, flags:Array<{c:ClassFlag, e:EnumFlag, a:haxeparser.AbstractFlag, s:StaticFlag, pos:Position}>, p1) { return switch stream { case [name = typeName(), tl = parseConstraintParams(), st = parseAbstractSubtype(), sl = parseRepeat(parseAbstractRelations)]: var fl = switch stream { @@ -748,8 +747,8 @@ class HaxeParser extends hxparse.Parser implements hxpar fl; } var aflags = flags.map(function(i) return i.a); - if (st != null){ - aflags.push(AIsType(st)); + if (st != null) { + aflags.push(AbOver(st)); } { decl: EAbstract({ name: name, @@ -762,7 +761,7 @@ class HaxeParser extends hxparse.Parser implements hxpar } } - function parseClassContent(doc, meta, flags:Array<{c:ClassFlag, e:EnumFlag, a:AbstractFlag, s:StaticFlag, pos:Position}>, n, p1) { + function parseClassContent(doc, meta, flags:Array<{c:ClassFlag, e:EnumFlag, a:haxeparser.AbstractFlag, s:StaticFlag, pos:Position}>, n, p1) { var name = typeName(); var tl = parseConstraintParams(); var hl = parseRepeat(parseClassHerit); @@ -837,7 +836,7 @@ class HaxeParser extends hxparse.Parser implements hxpar } case [{tok:Kwd(KwdEnum), pos:p1}]: switch stream { - case [{tok:Kwd(KwdAbstract), pos:p1}, a = parseAbstract(doc, [{name: ":enum", params: [], pos: p1}].concat(meta), c, p1)]: + case [{tok:Kwd(KwdAbstract), pos:p1}, a = parseAbstract(doc, meta, c.concat([{{c:null, e:null, a:AbEnum, s:null, pos:null}}]), p1)]: { decl: a.decl, pos: punion(p1, a.pos)}; case [name = typeName(), tl = parseConstraintParams(), {tok:BrOpen}, l = parseRepeat(parseEnum), {tok:BrClose, pos: p2}]: {decl: EEnum({ @@ -998,10 +997,10 @@ class HaxeParser extends hxparse.Parser implements hxpar } } - function parseAbstractRelations() { + function parseAbstractRelations():haxeparser.AbstractFlag { return switch stream { - case [{tok:Const(CIdent("to"))}, t = parseComplexType()]: AToType(t); - case [{tok:Const(CIdent("from"))}, t = parseComplexType()]: AFromType(t); + case [{tok:Const(CIdent("to"))}, t = parseComplexType()]: AbTo(t); + case [{tok:Const(CIdent("from"))}, t = parseComplexType()]: AbFrom(t); } } @@ -1033,10 +1032,10 @@ class HaxeParser extends hxparse.Parser implements hxpar return parseRepeat(parseClassField); } - function parseCommonFlags():Array<{c:ClassFlag, e:EnumFlag, a:AbstractFlag, s:StaticFlag, pos:Position}> { + function parseCommonFlags():Array<{c:ClassFlag, e:EnumFlag, a:haxeparser.AbstractFlag, s:StaticFlag, pos:Position}> { return switch stream { - 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(KwdPrivate), pos:p}, l = parseCommonFlags()]: aunshift(l, {c:HPrivate, e:EPrivate, a:AbPrivate, s:SPrivate, pos:p}); + case [{tok:Kwd(KwdExtern), pos:p}, l = parseCommonFlags()]: aunshift(l, {c:HExtern, e:EExtern, a:AbExtern, 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(KwdMacro), pos:p}, l = parseCommonFlags()]: aunshift(l, {c:null, e:null, a:null, s:SMacro, pos:p}); case [{tok:Kwd(KwdDynamic), pos:p}, l = parseCommonFlags()]: aunshift(l, {c:null, e:null, a:null, s:SDynamic, pos:p}); diff --git a/test/Test.hx b/test/Test.hx index 964d854..60d1bea 100644 --- a/test/Test.hx +++ b/test/Test.hx @@ -659,7 +659,11 @@ class Test implements ITest { peq("enum C {}", "enum C {}"); peq("@:enum abstract C(Int) {}", "@:enum abstract C(Int) {}"); + #if (haxe >= version ("5.0.0-alpha.1")) + peq("enum abstract C(Int) {}", "enum abstract C(Int) {}"); + #else peq("enum abstract C(Int) {}", "@:enum abstract C(Int) {}"); + #end peq("abstract Any(Dynamic) {extern inline static function __cast(value:T):Any return cast value;}", "abstract Any(Dynamic) {extern inline static function __cast(value:T):Any return cast value;}");