-
-
Notifications
You must be signed in to change notification settings - Fork 905
/
tss.pegjs
291 lines (239 loc) · 6.87 KB
/
tss.pegjs
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
- TSS parser based on JSON parser from https://github.com/dmajda/pegjs/blob/master/examples/json.pegjs
- JSON parser based on the grammar described at http://json.org/. */
/* ===== Syntactical Elements ===== */
{
var ALLOY_EXPR = '__ALLOY_EXPR__--';
function processValue(o) {
if (Object.prototype.toString.call(o) === '[object String]') {
var str = o.replace(ALLOY_EXPR,'');
if (str.length === o.length) {
return '"' + str.replace(/"/g, "\\\"") + '"';
} else {
return str;
}
} else {
return o;
}
}
}
start
= __ topobject:topobject __ { return topobject; }
topobject
= "{" __ "}" { return {}; }
/ "{" __ topmembers:topmembers __ "}" { return topmembers; }
object
= "{" __ "}" { return {}; }
/ "{" __ members:members __ "}" { return members; }
topmembers
= head:pair tail:(___ pair?)* {
var result = {};
result[head[0]] = head[1];
for (var i = 0; i < tail.length; i++) {
result[tail[i][1][0]] = tail[i][1][1];
}
if (typeof result['undefined'] === 'undefined') {
delete result['undefined'];
}
return result;
}
members
= head:pair tail:(__ "," __ pair?)* {
var result = {};
result[head[0]] = head[1];
for (var i = 0; i < tail.length; i++) {
result[tail[i][3][0]] = tail[i][3][1];
}
if (typeof result['undefined'] === 'undefined') {
delete result['undefined'];
}
return result;
}
pair
= name:(string / bareString) __ ":" __ value:value { return [name, value]; }
array
= "[" __ "]" { return []; }
/ "[" __ elements:elements __ "]" { return elements; }
elements
= head:value tail:(__ "," __ value)* {
var result = [head];
for (var i = 0; i < tail.length; i++) {
result.push(tail[i][3]);
}
return result;
}
value
= head:basevalue tail:(__ bitwise_operator __ basevalue)+ {
var str = processValue(head);
for (var i = 0; i < tail.length; i++) {
str += tail[i][1] + processValue(tail[i][3]);
}
return ALLOY_EXPR + str;
}
/ basevalue
basevalue
= LocaleCall
/ TiConstant
/ WPATH
/ string
/ number
/ object
/ array
/ "true" __ { return true; }
/ "false" __ { return false; }
/ "undefined" __ { return ALLOY_EXPR + "undefined"; }
/ "null" __ { return ALLOY_EXPR + "null"; }
/ DollarArgs
/* ===== Lexical Elements ===== */
DollarArgs
= parts:("$.args" ('.' bareString)+) {
var args = ALLOY_EXPR + parts[0];
var len = parts[1] ? parts[1].length : 0;
for (var i = 0; i < len; i++) {
args += parts[1][i].join('');
}
return args;
}
bitwise_operator "bitwise_operator"
= ">>"
/ ">>>"
/ "<<"
/ "<<<"
/ op: [&|^] { return op; }
OpenParen
= '(' { return '('; }
CloseParen
= ')' { return ')'; }
WPATH
= 'WPATH' OpenParen __ param1:paramString __ (paramComma __ paramString)* __ CloseParen {
return ALLOY_EXPR + 'WPATH(' + param1 + ')';
}
LocaleCall
= Locale OpenParen __ param1:paramString __ param2:(paramComma __ paramString)? __ CloseParen {
return ALLOY_EXPR + 'L(' + param1 + (param2 ? param2.join('') : '') + ')';
}
Locale
= TiNS '.Locale.getString'
/ 'L'
TiConstant
= parts:(TiNS ('.' bareString)+) {
var tc = ALLOY_EXPR + parts[0];
var len = parts[1] ? parts[1].length : 0;
for (var i = 0; i < len; i++) {
tc += parts[1][i].join('');
}
return tc;
}
TiNS
= 'Titanium'
/ 'Ti'
/ 'Alloy'
paramComma
= __ ',' __ { return ','; }
paramString
= '"' '"' __ { return '""'; }
/ "'" "'" __ { return "''"; }
/ '"' chars:chars '"' __ { return '"' + chars + '"'; }
/ "'" schars:schars "'" __ { return "'" + schars + "'"; }
/ c:TiConstant { return c.replace(ALLOY_EXPR, ''); }
string "string"
= '"' '"' __ { return ""; }
/ "'" "'" __ { return ""; }
/ '"' chars:chars '"' __ { return chars; }
/ "'" schars:schars "'" __ { return schars; }
bareString "bare word"
= chars:bareChar+ { return chars.join(''); }
bareChar
= [a-zA-Z0-9_$]
schars
= schars:schar+ { return schars.join(''); }
chars
= chars:char+ { return chars.join(''); }
schar
// In the original JSON grammar: "any-Unicode-character-except-"-or-\-or-control-character"
= [^'\\\0-\x1F\x7f]
/ "\\'" { return "'"; }
/ "\\\\" { return "\\"; }
/ "\\/" { return "/"; }
/ "\\b" { return "\b"; }
/ "\\f" { return "\f"; }
/ "\\n" { return "\n"; }
/ "\\r" { return "\r"; }
/ "\\t" { return "\t"; }
/ "\\u" h1:hexDigit h2:hexDigit h3:hexDigit h4:hexDigit {
return String.fromCharCode(parseInt("0x" + h1 + h2 + h3 + h4));
}
char
// In the original JSON grammar: "any-Unicode-character-except-"-or-\-or-control-character"
= [^"\\\0-\x1F\x7f]
/ '\\"' { return '"'; }
/ "\\\\" { return "\\"; }
/ "\\/" { return "/"; }
/ "\\b" { return "\b"; }
/ "\\f" { return "\f"; }
/ "\\n" { return "\n"; }
/ "\\r" { return "\r"; }
/ "\\t" { return "\t"; }
/ "\\u" h1:hexDigit h2:hexDigit h3:hexDigit h4:hexDigit {
return String.fromCharCode(parseInt("0x" + h1 + h2 + h3 + h4));
}
number "number"
= int_:int frac:frac exp:exp __ { return parseFloat(int_ + frac + exp); }
/ int_:int frac:frac __ { return parseFloat(int_ + frac); }
/ int_:int exp:exp __ { return parseFloat(int_ + exp); }
/ int_:int __ { return parseFloat(int_); }
int
= digit19:digit19 digits:digits { return digit19 + digits; }
/ digit:digit
/ "-" digit19:digit19 digits:digits { return "-" + digit19 + digits; }
/ "-" digit:digit { return "-" + digit; }
frac
= "." digits:digits { return "." + digits; }
exp
= e:e digits:digits { return e + digits; }
digits
= digits:digit+ { return digits.join(""); }
e
= e:[eE] sign:[+-]? { return e + sign; }
/*
* The following rules are not present in the original JSON gramar, but they are
* assumed to exist implicitly.
*
* FIXME: Define them according to ECMA-262, 5th ed.
*/
digit
= [0-9]
digit19
= [1-9]
hexDigit
= [0-9a-fA-F]
/* Whitespace and comments */
_
= (WhiteSpace / MultiLineCommentNoLineTerminator / SingleLineComment)*
__
= (WhiteSpace / LineTerminatorSequence / Comment)*
___
= (WhiteSpace / LineTerminatorSequence / Comment / Comma)+
Comma
= ","
LineTerminator
= [\n\r\u2028\u2029]
LineTerminatorSequence "end of line"
= "\n"
/ "\r\n"
/ "\r"
/ "\u2028" // line separator
/ "\u2029" // paragraph separator
SourceCharacter
= .
WhiteSpace "whitespace"
= [\t\v\f \u00A0\uFEFF]
Comment "comment"
= MultiLineComment
/ SingleLineComment
MultiLineComment
= "/*" (!"*/" SourceCharacter)* "*/"
SingleLineComment
= "//" (!LineTerminator SourceCharacter)*
MultiLineCommentNoLineTerminator
= "/*" (!("*/" / LineTerminator) SourceCharacter)* "*/"