Skip to content

Commit

Permalink
updated enum abstract handling to match recent Haxe changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHaxe committed Jul 5, 2023
1 parent 7e98c9a commit 5f3d79c
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 168 deletions.
6 changes: 3 additions & 3 deletions haxe_libraries/test-adapter.hxml
Original file line number Diff line number Diff line change
@@ -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()
13 changes: 7 additions & 6 deletions src/haxeparser/Data.hx
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ 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>>);
EAbstract(a:Definition<haxeparser.AbstractFlag, Array<Field>>);
EStatic(s:Definition<StaticFlag, FieldType>);
EImport(sl:Array<{pack:String, pos:Position}>, mode:ImportMode);
EUsing(path:TypePath);
Expand All @@ -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 {
Expand Down
291 changes: 151 additions & 140 deletions src/haxeparser/DefinitionConverter.hx
Original file line number Diff line number Diff line change
@@ -1,140 +1,151 @@
package haxeparser;

import haxeparser.Data;
import haxe.macro.Expr;

using Lambda;

class DefinitionConverter {
static public function convertTypeDef(pack:Array<String>, 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<A, B>(d:Definition<A, B>) {
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<ClassFlag, Array<Field>>) {
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<EnumFlag, ComplexType>) {
var def = getGeneralDefinition(t);
def.kind = TDAlias(t.data);
return def;
}

static function convertAbstract(a:Definition<AbstractFlag, Array<Field>>) {
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<EnumFlag, Array<EnumConstructor>>) {
var def = getGeneralDefinition(en);
def.kind = TDEnum;
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);
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<String>, 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<A, B>(d:Definition<A, B>) {
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<ClassFlag, Array<Field>>) {
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<EnumFlag, ComplexType>) {
var def = getGeneralDefinition(t);
def.kind = TDAlias(t.data);
return def;
}

static function convertAbstract(a:Definition<haxeparser.AbstractFlag, Array<Field>>) {
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<EnumFlag, Array<EnumConstructor>>) {
var def = getGeneralDefinition(en);
def.kind = TDEnum;
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);
case SOverload:
access.push(AOverload);
}
}
def.kind = TDField(c.data, access);
return def;
}
}
Loading

0 comments on commit 5f3d79c

Please sign in to comment.