-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathes3.js
359 lines (311 loc) · 13.6 KB
/
es3.js
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright (C) 2007 Chris Double.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Forward Declarations
var SourceElement =
function(input) { return SourceElement(input); }
var AssignmentExpression =
function(input) { return AssignmentExpression(input); }
var Expression =
function(input) { return Expression(input); }
var Statement =
function(input) { return Statement(input); }
var LeftHandSideExpression =
function(input) { return LeftHandSideExpression(input); }
var Whitespace = choice("\t", " ");
var LineTerminator = choice(ch("\r"), ch("\n"));
var SingleLineCommentChars = join_action(repeat1(negate(LineTerminator)), "");
var SingleLineComment = sequence("//", optional(SingleLineCommentChars), LineTerminator);
var Comment =
choice(SingleLineComment);
var NullLiteral = token("null");
var BooleanLiteral = choice("true", "false");
var Zero = action("0", function(ast) { return 0; });
var DecimalDigit = action(range("0", "9"), function(ast) { return parseInt(ast); });
var NonZeroDigit = action(range("1", "9"), function(ast) { return parseInt(ast); });
var DecimalDigits = repeat1(DecimalDigit);
var DecimalIntegerLiteral = choice(Zero, sequence(NonZeroDigit, optional(DecimalDigits)));
var SignedInteger = choice(DecimalDigits, sequence("+", DecimalDigits), sequence("-", DecimalDigits));
var ExponentIndicator = choice("e", "E");
var ExponentPart = sequence(ExponentIndicator, SignedInteger);
var DecimalLiteral = choice(sequence(DecimalIntegerLiteral, ".", optional(DecimalDigits), optional(ExponentPart)),
sequence(".", DecimalDigits, optional(ExponentPart)),
sequence(DecimalIntegerLiteral, optional(ExponentPart)));
var HexDigit = choice(range("0", "9"), range("a", "f"), range("A", "F"));
var HexIntegerLiteral = sequence(choice("0x", "0X"), repeat1(HexDigit));
var NumericLiteral = choice(HexIntegerLiteral, DecimalLiteral);
var SingleEscapeCharacter = choice("'", "\"", "\\", "b", "f", "n", "r", "t", "v");
var NonEscapeCharacter = negate(SingleEscapeCharacter);
var CharacterEscapeSequence = choice(SingleEscapeCharacter, NonEscapeCharacter);
var HexEscapeSequence = sequence("x", HexDigit, HexDigit);
var UnicodeEscapeSequence = sequence("u", HexDigit, HexDigit, HexDigit, HexDigit);
var EscapeSequence = choice(HexEscapeSequence, UnicodeEscapeSequence, CharacterEscapeSequence);
var SingleStringCharacter = choice(negate(choice("\'", "\\", "\r", "\n")),
sequence("\\", EscapeSequence));
var DoubleStringCharacter = choice(negate(choice("\"", "\\", "\r", "\n")),
sequence("\\", EscapeSequence));
var SingleStringCharacters = repeat1(SingleStringCharacter);
var DoubleStringCharacters = repeat1(DoubleStringCharacter);
var StringLiteral = choice(sequence("\"", optional(DoubleStringCharacters), "\""),
sequence("'", optional(SingleStringCharacters), "'"));
var Literal = choice(NullLiteral, BooleanLiteral, NumericLiteral, StringLiteral);
var Keyword =
choice("break",
"case",
"catch",
"continue",
"default",
"delete",
"do",
"else",
"finally",
"for",
"function",
"if",
"in",
"instanceof",
"new",
"return",
"switch",
"this",
"throw",
"try",
"typeof",
"var",
"void",
"while",
"with");
var ReservedWord = choice(Keyword, NullLiteral, BooleanLiteral);
var HexDigit = choice(range("0", "9"), range("a", "f"), range("A", "F"));
var IdentifierLetter = choice(range("a", "z"), range("A", "Z"));
var IdentifierStart = choice(IdentifierLetter, "$", "_");
var IdentifierPart = choice(IdentifierStart, range("0-9"));
var IdentifierName =
action(sequence(IdentifierStart, join_action(repeat0(IdentifierPart), "")),
function(ast) {
return ast[0].concat(ast[1]);
});
var Identifier = butnot(IdentifierName, ReservedWord);
var StatementList = repeat1(Statement);
var Block = wsequence("{", optional(StatementList), "}");
var Initialiser = wsequence("=", AssignmentExpression);
var VariableDeclaration = wsequence(Identifier, optional(Initialiser));
var VariableDeclarationList = wlist(VariableDeclaration, ",");
var VariableStatement =
wsequence("var", VariableDeclarationList);
var EmptyStatement = token(";");
var IfStatement =
choice(wsequence("if", "(", Expression, ")", Statement, "else", Statement),
wsequence("if", "(", Expression, ")", Statement));
var IterationStatement =
choice(wsequence("do", Statement, "while", "(", Expression, ")", ";"),
wsequence("while", "(", Expression, ")", Statement),
wsequence("for", "(", optional(Expression), ";", optional(Expression), ";", optional(Expression), ")", Statement),
wsequence("for", "(", "var", VariableDeclarationList, ";", optional(Expression), ";", optional(Expression), ")", Statement),
wsequence("for", "(", LeftHandSideExpression, "in", Expression, ")", Statement),
wsequence("for", "(", "var", VariableDeclaration, "in", Expression, ")", Statement));
var ContinueStatement = wsequence("continue", optional(Identifier), ";");
var BreakStatement = wsequence("break", optional(Identifier), ";");
var ReturnStatement = wsequence("return", optional(Expression), ";");
var WithStatement = wsequence("with", "(", Expression, ")", Statement);
var CaseClause =
wsequence("case", Expression, ":", optional(StatementList));
var DefaultClause =
wsequence("default", ":", optional(StatementList));
var CaseBlock =
choice(wsequence("{", repeat0(CaseClause), "}"),
wsequence("{", repeat0(CaseClause), DefaultClause, repeat0(CaseClause), "}"));
var SwitchStatement = wsequence("switch", "(", Expression, ")", CaseBlock);
var LabelledStatement = wsequence(Identifier, ":", Statement);
var ThrowStatement = wsequence("throw", Expression, ";");
var Catch = wsequence("catch", "(", Identifier, ")", Block);
var Finally = wsequence("finally", Block);
var TryStatement =
choice(wsequence("try", Block, Catch),
wsequence("try", Block, Finally),
wsequence("try", Block, Catch, Finally));
var ExpressionStatement =
choice(sequence(choice("{", "function"), nothing_p),
Expression);
var Statement =
choice(Block,
VariableStatement,
EmptyStatement,
ExpressionStatement,
IfStatement,
IterationStatement,
ContinueStatement,
BreakStatement,
ReturnStatement,
WithStatement,
SwitchStatement,
LabelledStatement,
ThrowStatement,
TryStatement);
var FunctionDeclaration =
function(input) { return FunctionDeclaration(input); }
var FunctionBody = repeat0(SourceElement);
var FormalParameterList = wlist(Identifier, ",");
var FunctionExpression =
wsequence("function", optional(Identifier), "(", optional(FormalParameterList), ")", "{", FunctionBody, "}");
var FunctionDeclaration =
wsequence("function", Identifier, "(", optional(FormalParameterList), ")", "{", FunctionBody, "}");
var PrimaryExpression =
function(input) { return PrimaryExpression(input); }
var ArgumentList = list(AssignmentExpression, ",");
var Arguments =
choice(wsequence("(", ")"),
wsequence("(", ArgumentList, ")"));
var MemberExpression = function(input) { return MemberExpression(input); }
var MemberExpression =
left_factor_action(sequence(choice(wsequence("new", MemberExpression, Arguments),
PrimaryExpression,
FunctionExpression),
repeat0(choice(wsequence("[", Expression, "]"),
wsequence(".", Identifier)))));
var NewExpression =
choice(MemberExpression,
wsequence("new", NewExpression));
var CallExpression =
left_factor_action(wsequence(wsequence(MemberExpression, Arguments),
repeat0(choice(Arguments,
wsequence("[", Expression, "]"),
wsequence(".", Identifier)))));
var LeftHandSideExpression = choice(CallExpression, NewExpression);
var AssignmentOperator =
choice("=",
"*=",
"/=",
"%=",
"+=",
"-=",
"<<=",
">>=",
">>>=",
"&=",
"^=",
"|=");
var LogicalORExpression =
function(input) { return LogicalORExpression(input); }
var LogicalANDExpression =
function(input) { return LogicalANDExpression(input); }
var BitwiseORExpression =
function(input) { return BitwiseORExpression(input); }
var BitwiseXORExpression =
function(input) { return BitwiseXORExpression(input); }
var BitwiseANDExpression =
function(input) { return BitwiseANDExpression(input); }
var EqualityExpression =
function(input) { return EqualityExpression(input); }
var RelationalExpression =
function(input) { return RelationalExpression(input); }
var ShiftExpression =
function(input) { return ShiftExpression(input); }
var AdditiveExpression =
function(input) { return AdditiveExpression(input); }
var MultiplicativeExpression =
function(input) { return MultiplicativeExpression(input); }
var UnaryExpression =
function(input) { return UnaryExpression(input); }
var PostfixExpression =
function(input) { return PostfixExpression(input); }
var PostfixExpression =
choice(wsequence(LeftHandSideExpression, "++"),
wsequence(LeftHandSideExpression, "--"),
LeftHandSideExpression);
var UnaryExpression =
choice(PostfixExpression,
wsequence("delete", UnaryExpression),
wsequence("void", UnaryExpression),
wsequence("typeof", UnaryExpression),
wsequence("++", UnaryExpression),
wsequence("--", UnaryExpression),
wsequence("+", UnaryExpression),
wsequence("-", UnaryExpression),
wsequence("~", UnaryExpression),
wsequence("!", UnaryExpression));
var MultiplicativeExpression =
wsequence(UnaryExpression,
repeat0(choice(wsequence("*", UnaryExpression),
wsequence("/", UnaryExpression),
wsequence("%", UnaryExpression))));
var AdditiveExpression =
wsequence(MultiplicativeExpression,
repeat0(choice(wsequence("+", MultiplicativeExpression),
wsequence("-", MultiplicativeExpression))));
var ShiftExpression =
wsequence(AdditiveExpression,
repeat0(choice(wsequence("<<", AdditiveExpression),
wsequence(">>", AdditiveExpression),
wsequence(">>>", AdditiveExpression))));
var RelationalExpression =
wsequence(ShiftExpression,
repeat0(choice(wsequence("<", ShiftExpression),
wsequence(">", ShiftExpression),
wsequence("<=", ShiftExpression),
wsequence(">=", ShiftExpression),
wsequence("instanceof", ShiftExpression))));
var EqualityExpression =
wsequence(RelationalExpression,
repeat0(choice(wsequence("==", RelationalExpression),
wsequence("!==", RelationalExpression),
wsequence("===", RelationalExpression),
wsequence("!==", RelationalExpression))));
var BitwiseANDExpression =
wsequence(EqualityExpression, repeat0(wsequence("&", EqualityExpression)));
var BitwiseXORExpression =
wsequence(BitwiseANDExpression, repeat0(wsequence("^", BitwiseANDExpression)));
var BitwiseORExpression =
wsequence(BitwiseXORExpression, repeat0(wsequence("|", BitwiseXORExpression)));
var LogicalANDExpression =
wsequence(BitwiseORExpression, repeat0(wsequence("&&", BitwiseORExpression)));
var LogicalORExpression =
wsequence(LogicalANDExpression, repeat0(wsequence("||", LogicalANDExpression)));
var ConditionalExpression =
choice(LogicalORExpression,
wsequence(LogicalORExpression, "?", AssignmentExpression, ":", AssignmentExpression));
var AssignmentExpression =
choice(wsequence(LeftHandSideExpression, AssignmentOperator, AssignmentExpression),
ConditionalExpression);
var Expression = list(AssignmentExpression, ",");
var Elision = repeat1(",");
var ElementList = list(wsequence(optional(Elision), AssignmentExpression), ",");
var ArrayLiteral =
choice(wsequence("[", optional(Elision), "]"),
wsequence("[", ElementList, "]"),
wsequence("[", ElementList, optional(Elision), "]"));
var PropertyName = choice(Identifier, StringLiteral, NumericLiteral);
var PropertyNameAndValueList =
list(wsequence(PropertyName, ":", AssignmentExpression), ",");
var ObjectLiteral =
choice(wsequence("{", "}"),
wsequence("{", PropertyNameAndValueList, "}"));
var PrimaryExpression =
choice("this",
wsequence("(", Expression, ")"),
Identifier,
ArrayLiteral,
ObjectLiteral,
Literal);
var SourceElement = choice(Comment, Statement, FunctionDeclaration);
var Program = repeat0(SourceElement);