-
Notifications
You must be signed in to change notification settings - Fork 1
/
Convert.hx
219 lines (193 loc) · 6.28 KB
/
Convert.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import haxe.DynamicAccess;
import haxe.macro.Expr;
using haxe.macro.Tools;
using StringTools;
typedef AtomApi = {
classes:DynamicAccess<AtomClass>,
}
typedef Desc = {
summary:String,
description:String,
}
typedef AtomClass = {
>Desc,
name:String,
superClass:String,
classMethods:Array<AtomMethod>,
instanceMethods:Array<AtomMethod>,
classProperties:Array<AtomProperty>,
instanceProperties:Array<AtomProperty>,
}
typedef AtomField = {
>Desc,
name:String,
}
typedef AtomMethod = {
>AtomField,
arguments:Array<AtomArg>,
returnValues:Array<AtomReturn>,
}
typedef AtomArg = {
name:String,
type:String,
isOptional:Bool,
children:Array<AtomArg>,
}
typedef AtomReturn = {
type:String,
}
typedef AtomProperty = {
>AtomField,
}
class Convert {
static var pos = {min: 0, max: 0, file: ""};
static function main() {
var api:AtomApi = haxe.Json.parse(sys.io.File.getContent(Sys.args()[0]));
sys.FileSystem.createDirectory("atom");
var printer = new haxe.macro.Printer();
for (key in api.classes.keys()) {
var cls = api.classes[key];
var fields:Array<Field> = [];
for (p in cls.classProperties) {
var f = convertProperty(p);
f.access.push(AStatic);
fields.push(f);
}
for (p in cls.instanceProperties) {
fields.push(convertProperty(p));
}
for (m in cls.classMethods) {
var f = convertMethod(m);
f.access.push(AStatic);
fields.push(f);
}
if (cls.instanceMethods != null)
for (m in cls.instanceMethods) {
var f = convertMethod(m);
if (f.name == "constructor")
f.name = "new";
fields.push(f);
}
var sup = null;
if (cls.superClass != null && cls.superClass != "Model") {
sup = {pack: ["atom"], name: cls.superClass};
}
var s = printer.printTypeDefinition({
pos: pos,
pack: ["atom"],
name: cls.name,
isExtern: true,
kind: TDClass(sup),
fields: fields,
meta: [
{name: ":native", params: [{expr: EConst(CString(cls.name)), pos: pos}], pos: pos}
]
});
if (cls.summary != null) {
s = "/**\n\t" + cls.summary.replace("\n", "\n\t") + "\n**/\n" + s;
}
sys.io.File.saveContent("atom/" + cls.name + ".hx", s);
}
}
static function convertProperty(p:AtomProperty):Field {
var summaryRe = ~/An? \{(.*)\} instance/;
var type =
if (summaryRe.match(p.summary))
convertType(summaryRe.matched(1), []);
else
macro : Dynamic;
return {
pos: pos,
name: p.name,
kind: FVar(type),
doc: p.summary,
};
}
static function convertMethod(m:AtomMethod):Field {
var returnType = macro : Dynamic;
if (m.name == "constructor")
returnType = macro : Void;
if (m.returnValues != null)
returnType = makeEither([for (r in m.returnValues) r.type]);
var args:Array<FunctionArg> = [];
if (m.arguments != null) {
for (a in m.arguments)
args.push({name: a.name, type: convertType(a.type, a.children)});
}
return {
pos: pos,
name: m.name,
kind: FFun({
args: args,
ret: returnType,
expr: null
}),
access: [],
doc: m.summary,
};
}
static var kwds = ["class"];
static function escapeName(n:String):String {
return if (kwds.indexOf(n) != -1) n + "_" else n; // TODO: we have to make some abstract for that
}
static function convertType(type:String, children:Array<AtomArg>):ComplexType {
return switch (type) {
case null:
macro : Dynamic;
case "Function":
if (children != null) {
var args = [for (child in children) convertType(child.type, child.children)];
var ret = macro : Dynamic;
TFunction(args, ret);
} else {
macro : haxe.Constraints.Function;
}
case "String":
macro : String;
case "Boolean" | "Bool":
macro : Bool;
case "Number":
macro : Float;
case "Object":
if (children != null) {
var fields:Array<Field> = [];
for (child in children) {
fields.push({
pos: pos,
name: escapeName(child.name),
kind: FVar(convertType(child.type, child.children))
});
}
TAnonymous(fields);
} else {
macro : Dynamic<Dynamic>;
}
case "Package" | "KeyBinding" | "MarkerObservationWindow": // wtf is this
macro : Dynamic<Dynamic>;
case "RegExp":
macro : js.RegExp;
case "Error":
macro : js.Error;
case "Set":
macro : Dynamic; // TODO: add es6 Set extern to haxe
case "Promise":
macro : js.Promise<Dynamic>;
case "Array":
macro : Array<Dynamic>;
default:
TPath({pack: ["atom"], name: type});
}
}
static function makeEither(types:Array<String>):ComplexType {
var uniq = new Map();
for (t in types) {
var ct = convertType(t, null);
var s = ct.toString();
if (s == "Dynamic")
return ct;
uniq[s] = ct;
}
var converted = Lambda.array(uniq);
return Lambda.fold(converted.slice(1), function(a, b) return macro : haxe.extern.EitherType<$a, $b>, converted[0]);
}
}