-
-
Notifications
You must be signed in to change notification settings - Fork 648
/
Format.hx
119 lines (116 loc) · 3.58 KB
/
Format.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
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.macro;
import haxe.macro.Expr;
import haxe.macro.Context;
/**
The actual macro implemented for Std.format
**/
class Format {
#if (macro || display)
public static function format(estr:Expr) {
var str = switch (estr.expr) {
case EConst(c): switch (c) {
case CString(s): s;
default: null;
}
default: null;
};
if (str == null)
Context.error("Constant string required", estr.pos);
var pos = Context.getPosInfos(estr.pos);
var min = pos.min;
pos.min++;
var expr = null;
function make(size) {
pos.max = pos.min + size;
var p = Context.makePosition(pos);
pos.min += size;
return p;
}
function add(e) {
if (expr == null)
expr = e;
else
expr = {expr: EBinop(OpAdd, expr, e), pos: Context.makePosition({min: min, max: pos.min, file: pos.file})};
}
var i = 0, start = 0;
var max = str.length;
while (i < max) {
if (StringTools.fastCodeAt(str, i++) != '$'.code)
continue;
var len = i - start - 1;
if (len > 0 || expr == null)
add({expr: EConst(CString(str.substr(start, len))), pos: make(len)});
pos.min++;
start = i;
var c = StringTools.fastCodeAt(str, i);
if (c == '{'.code) {
var count = 1;
i++;
while (i < max) {
var c = StringTools.fastCodeAt(str, i++);
if (c == "}".code) {
if (--count == 0)
break;
} else if (c == "{".code)
count++;
}
if (count > 0)
Context.error("Closing brace not found", make(1));
pos.min++;
start++;
var len = i - start - 1;
var expr = str.substr(start, len);
add(Context.parseInlineString(expr, make(len)));
pos.min++;
start++;
} else if ((c >= 'a'.code && c <= 'z'.code) || (c >= 'A'.code && c <= 'Z'.code) || c == '_'.code) {
i++;
while (true) {
var c = StringTools.fastCodeAt(str, i);
if ((c >= 'a'.code && c <= 'z'.code) || (c >= 'A'.code && c <= 'Z'.code) || (c >= '0'.code && c <= '9'.code) || c == '_'.code)
i++;
else
break;
}
var len = i - start;
var ident = str.substr(start, len);
add({expr: EConst(CIdent(ident)), pos: make(len)});
} else if (c == '$'.code) {
start = i++;
continue;
} else {
start = i - 1;
continue;
}
start = i;
}
var len = i - start;
if (len > 0)
add({expr: EConst(CString(str.substr(start, len))), pos: make(len)});
if (expr == null)
expr = {expr: EConst(CString("")), pos: make(0)};
return {expr: ECheckType(expr, TPath({pack: [], name: "String", params: []})), pos: expr.pos};
}
#end
}