/
SfCore.hx
141 lines (130 loc) · 4.1 KB
/
SfCore.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
package sf;
import haxe.macro.JSGenApi;
import haxe.macro.Type;
import sf.SfGenerator;
import sf.type.SfBuffer;
import haxe.macro.Context;
import haxe.extern.Rest;
import sf.type.SfPrintf;
/**
* ...
* @author YellowAfterlife
*/
class SfCore {
/** The current SfGenerator */
public static var sfGenerator:SfGenerator = null;
public static var jsGenAPI:JSGenApi;
/** Config holder */
public static var sfConfig:SfConfig = null;
/**
Formatted output. Supported tags:
` Optional space
%` An actual backtick
; Semicolon (omitted if immediately after a closing curly bracket)
%s A String
%d An Int
%c An Int as a charcode
%x An inline expression (flags=Expr)
%w A wrapped inline expression (flags=ExprWrap)
%(const) A TConst
%(stat) A wrapped statement (flags=StatWrap)
%(block) A statement in a block, if needed (flags=Stat)
%(expr) Same as %x
%(args) An array of arguments ([a, b, c] -> `a, b, c`) via addArguments
%(targs) An array of trailing arguments ([a, b, c] -> `, a, b, c`) via addTrailArgs
%(+\n) Adds a line break with increased indentation (e.g. "if (x) {%(+\n)...")
%(-\n) Adds a line break with decreased indentation (e.g. "...%(-\n)}")
Generators may implement additional tags by overriding SfGenerator.printFormat
**/
#if (haxe >= "4.2")
public static function printf(buf:SfBuffer, format:String, params:Rest<Dynamic>):SfBuffer {
SfPrintf.proc(buf, format, params.toArray());
return buf;
}
#else
public static var printf:SfCore_printf = Reflect.makeVarArgs(function(params:Array<Dynamic>):Dynamic {
SfPrintf.proc(null, null, params);
return params[0];
});
#end
/** Like printf, but returns a string */
#if (haxe >= "4.2")
public static function sprintf(format:String, params:Rest<Dynamic>):String {
var buf = new SfBuffer();
SfPrintf.proc(buf, format, params);
return buf.toString();
}
#else
public static var sprintf:SfCore_sprintf = Reflect.makeVarArgs(function(params:Array<Dynamic>):Dynamic {
var buf = new SfBuffer();
SfPrintf.proc(buf, null, params);
return buf.toString();
});
#end
/** exception tag */
public static var xt:Dynamic = null;
public static var xt2:Dynamic = null;
private static var typesFound:Array<Type>;
private static function indexType() {
var t = Context.getLocalType();
if (t != null) typesFound.push(t);
return null;
}
/** Macros entrypoint */
private static function main() {
#if !sf_no_gen
SfTimes.init();
SfGenerator.main();
if (Context.defined("js")) {
var sfg = new SfGenerator();
haxe.macro.Compiler.setCustomJSGenerator(function(api:JSGenApi) {
jsGenAPI = api;
try {
sfg.compile(api.types, api.main, api.outputFile);
} catch (e:Dynamic) {
var stack = haxe.CallStack.exceptionStack();
Sys.println("Stack: " + haxe.CallStack.toString(stack));
Sys.println("Tag: " + Std.string(xt));
Sys.println("Tag2: " + Std.string(xt2));
Sys.println("Error: " + Std.string(e));
#if !eval_stack
if (stack.length == 0) Sys.println("(no eval-stack)");
#end
var pos = haxe.macro.PositionTools.make({min:0, max:0, file:api.outputFile});
Context.error("Generator error (see full output): " + e, pos);
}
SfTimes.finish();
});
} else {
var outPath = haxe.macro.Compiler.getOutput();
if (!Context.defined("no-output")) outPath += ".cs";
var sfg = new SfGenerator();
typesFound = [];
Context.onGenerate(function(types) {
typesFound = types;
});
Context.onAfterGenerate(function() {
var types = [];
for (t in typesFound) {
var r;
try {
var s = haxe.macro.TypeTools.toString(t);
var i = s.indexOf("<");
if (i >= 0) s = s.substring(0, i);
r = Context.getType(s);
} catch (_:Dynamic) {
if (sf.type.expr.SfExprTools.typeHasMeta(t, ":used")) {
r = t;
} else r = null;
}
if (r != null) types.push(r);
}
//
sfg.compile(types, null, outPath);
});
}
#end
}
}
typedef SfCore_printf = (buf:SfBuffer, fmt:String, args:Rest<Dynamic>)->SfBuffer;
typedef SfCore_sprintf = (fmt:String, args:Rest<Dynamic>)->String;