|
1 # |
|
2 # Jasy - Web Tooling Framework |
|
3 # Copyright 2010-2012 Zynga Inc. |
|
4 # |
|
5 |
|
6 # |
|
7 # License: MPL 1.1/GPL 2.0/LGPL 2.1 |
|
8 # Authors: |
|
9 # - Brendan Eich <brendan@mozilla.org> (Original JavaScript) (2004-2010) |
|
10 # - Sebastian Werner <info@sebastian-werner.net> (Python Port) (2010-2012) |
|
11 # |
|
12 |
|
13 import jasy.js.tokenize.Tokenizer |
|
14 import jasy.js.parse.VanillaBuilder |
|
15 import jasy.js.tokenize.Lang |
|
16 |
|
17 __all__ = [ "parse", "parseExpression" ] |
|
18 |
|
19 def parseExpression(source, fileId=None, line=1, builder=None): |
|
20 if builder == None: |
|
21 builder = jasy.js.parse.VanillaBuilder.VanillaBuilder() |
|
22 |
|
23 # Convert source into expression statement to be friendly to the Tokenizer |
|
24 if not source.endswith(";"): |
|
25 source = source + ";" |
|
26 |
|
27 tokenizer = jasy.js.tokenize.Tokenizer.Tokenizer(source, fileId, line) |
|
28 staticContext = StaticContext(False, builder) |
|
29 |
|
30 return Expression(tokenizer, staticContext) |
|
31 |
|
32 |
|
33 def parse(source, fileId=None, line=1, builder=None): |
|
34 if builder == None: |
|
35 builder = jasy.js.parse.VanillaBuilder.VanillaBuilder() |
|
36 |
|
37 tokenizer = jasy.js.tokenize.Tokenizer.Tokenizer(source, fileId, line) |
|
38 staticContext = StaticContext(False, builder) |
|
39 node = Script(tokenizer, staticContext) |
|
40 |
|
41 # store fileId on top-level node |
|
42 node.fileId = tokenizer.fileId |
|
43 |
|
44 # add missing comments e.g. empty file with only a comment etc. |
|
45 # if there is something non-attached by an inner node it is attached to |
|
46 # the top level node, which is not correct, but might be better than |
|
47 # just ignoring the comment after all. |
|
48 if len(node) > 0: |
|
49 builder.COMMENTS_add(node[-1], None, tokenizer.getComments()) |
|
50 else: |
|
51 builder.COMMENTS_add(node, None, tokenizer.getComments()) |
|
52 |
|
53 if not tokenizer.done(): |
|
54 raise SyntaxError("Unexpected end of file", tokenizer) |
|
55 |
|
56 return node |
|
57 |
|
58 |
|
59 |
|
60 class SyntaxError(Exception): |
|
61 def __init__(self, message, tokenizer): |
|
62 Exception.__init__(self, "Syntax error: %s\n%s:%s" % ( |
|
63 message, tokenizer.fileId, tokenizer.line)) |
|
64 |
|
65 |
|
66 # Used as a status container during tree-building for every def body and the global body |
|
67 class StaticContext(object): |
|
68 # inFunction is used to check if a return stm appears in a valid context. |
|
69 def __init__(self, inFunction, builder): |
|
70 # Whether this is inside a function, mostly True, only for top-level scope |
|
71 # it's False |
|
72 self.inFunction = inFunction |
|
73 |
|
74 self.hasEmptyReturn = False |
|
75 self.hasReturnWithValue = False |
|
76 self.isGenerator = False |
|
77 self.blockId = 0 |
|
78 self.builder = builder |
|
79 self.statementStack = [] |
|
80 |
|
81 # Sets to store variable uses |
|
82 # self.functions = set() |
|
83 # self.variables = set() |
|
84 |
|
85 # Status |
|
86 # self.needsHoisting = False |
|
87 self.bracketLevel = 0 |
|
88 self.curlyLevel = 0 |
|
89 self.parenLevel = 0 |
|
90 self.hookLevel = 0 |
|
91 |
|
92 # Configure strict ecmascript 3 mode |
|
93 self.ecma3OnlyMode = False |
|
94 |
|
95 # Status flag during parsing |
|
96 self.inForLoopInit = False |
|
97 |
|
98 |
|
99 def Script(tokenizer, staticContext): |
|
100 """Parses the toplevel and def bodies.""" |
|
101 node = Statements(tokenizer, staticContext) |
|
102 |
|
103 # change type from "block" to "script" for script root |
|
104 node.type = "script" |
|
105 |
|
106 # copy over data from compiler context |
|
107 # node.functions = staticContext.functions |
|
108 # node.variables = staticContext.variables |
|
109 |
|
110 return node |
|
111 |
|
112 |
|
113 def nest(tokenizer, staticContext, node, func, end=None): |
|
114 """Statement stack and nested statement handler.""" |
|
115 staticContext.statementStack.append(node) |
|
116 node = func(tokenizer, staticContext) |
|
117 staticContext.statementStack.pop() |
|
118 end and tokenizer.mustMatch(end) |
|
119 |
|
120 return node |
|
121 |
|
122 |
|
123 def Statements(tokenizer, staticContext): |
|
124 """Parses a list of Statements.""" |
|
125 |
|
126 builder = staticContext.builder |
|
127 node = builder.BLOCK_build(tokenizer, staticContext.blockId) |
|
128 staticContext.blockId += 1 |
|
129 |
|
130 builder.BLOCK_hoistLets(node) |
|
131 staticContext.statementStack.append(node) |
|
132 |
|
133 prevNode = None |
|
134 while not tokenizer.done() and tokenizer.peek(True) != "right_curly": |
|
135 comments = tokenizer.getComments() |
|
136 childNode = Statement(tokenizer, staticContext) |
|
137 builder.COMMENTS_add(childNode, prevNode, comments) |
|
138 builder.BLOCK_addStatement(node, childNode) |
|
139 prevNode = childNode |
|
140 |
|
141 staticContext.statementStack.pop() |
|
142 builder.BLOCK_finish(node) |
|
143 |
|
144 # if getattr(node, "needsHoisting", False): |
|
145 # # TODO |
|
146 # raise Exception("Needs hoisting went true!!!") |
|
147 # builder.setHoists(node.id, node.variables) |
|
148 # # Propagate up to the function. |
|
149 # staticContext.needsHoisting = True |
|
150 |
|
151 return node |
|
152 |
|
153 |
|
154 def Block(tokenizer, staticContext): |
|
155 tokenizer.mustMatch("left_curly") |
|
156 node = Statements(tokenizer, staticContext) |
|
157 tokenizer.mustMatch("right_curly") |
|
158 |
|
159 return node |
|
160 |
|
161 |
|
162 def Statement(tokenizer, staticContext): |
|
163 """Parses a Statement.""" |
|
164 |
|
165 tokenType = tokenizer.get(True) |
|
166 builder = staticContext.builder |
|
167 |
|
168 # Cases for statements ending in a right curly return early, avoiding the |
|
169 # common semicolon insertion magic after this switch. |
|
170 |
|
171 if tokenType == "function": |
|
172 # "declared_form" extends functions of staticContext, |
|
173 # "statement_form" doesn'tokenizer. |
|
174 if len(staticContext.statementStack) > 1: |
|
175 kind = "statement_form" |
|
176 else: |
|
177 kind = "declared_form" |
|
178 |
|
179 return FunctionDefinition(tokenizer, staticContext, True, kind) |
|
180 |
|
181 |
|
182 elif tokenType == "left_curly": |
|
183 node = Statements(tokenizer, staticContext) |
|
184 tokenizer.mustMatch("right_curly") |
|
185 |
|
186 return node |
|
187 |
|
188 |
|
189 elif tokenType == "if": |
|
190 node = builder.IF_build(tokenizer) |
|
191 builder.IF_setCondition(node, ParenExpression(tokenizer, staticContext)) |
|
192 staticContext.statementStack.append(node) |
|
193 builder.IF_setThenPart(node, Statement(tokenizer, staticContext)) |
|
194 |
|
195 if tokenizer.match("else"): |
|
196 comments = tokenizer.getComments() |
|
197 elsePart = Statement(tokenizer, staticContext) |
|
198 builder.COMMENTS_add(elsePart, node, comments) |
|
199 builder.IF_setElsePart(node, elsePart) |
|
200 |
|
201 staticContext.statementStack.pop() |
|
202 builder.IF_finish(node) |
|
203 |
|
204 return node |
|
205 |
|
206 |
|
207 elif tokenType == "switch": |
|
208 # This allows CASEs after a "default", which is in the standard. |
|
209 node = builder.SWITCH_build(tokenizer) |
|
210 builder.SWITCH_setDiscriminant(node, ParenExpression(tokenizer, staticContext)) |
|
211 staticContext.statementStack.append(node) |
|
212 |
|
213 tokenizer.mustMatch("left_curly") |
|
214 tokenType = tokenizer.get() |
|
215 |
|
216 while tokenType != "right_curly": |
|
217 if tokenType == "default": |
|
218 if node.defaultIndex >= 0: |
|
219 raise SyntaxError("More than one switch default", tokenizer) |
|
220 |
|
221 childNode = builder.DEFAULT_build(tokenizer) |
|
222 builder.SWITCH_setDefaultIndex(node, len(node)-1) |
|
223 tokenizer.mustMatch("colon") |
|
224 builder.DEFAULT_initializeStatements(childNode, tokenizer) |
|
225 |
|
226 while True: |
|
227 tokenType=tokenizer.peek(True) |
|
228 if tokenType == "case" or tokenType == "default" or tokenType == "right_curly": |
|
229 break |
|
230 builder.DEFAULT_addStatement(childNode, Statement(tokenizer, staticContext)) |
|
231 |
|
232 builder.DEFAULT_finish(childNode) |
|
233 |
|
234 elif tokenType == "case": |
|
235 childNode = builder.CASE_build(tokenizer) |
|
236 builder.CASE_setLabel(childNode, Expression(tokenizer, staticContext)) |
|
237 tokenizer.mustMatch("colon") |
|
238 builder.CASE_initializeStatements(childNode, tokenizer) |
|
239 |
|
240 while True: |
|
241 tokenType=tokenizer.peek(True) |
|
242 if tokenType == "case" or tokenType == "default" or tokenType == "right_curly": |
|
243 break |
|
244 builder.CASE_addStatement(childNode, Statement(tokenizer, staticContext)) |
|
245 |
|
246 builder.CASE_finish(childNode) |
|
247 |
|
248 else: |
|
249 raise SyntaxError("Invalid switch case", tokenizer) |
|
250 |
|
251 builder.SWITCH_addCase(node, childNode) |
|
252 tokenType = tokenizer.get() |
|
253 |
|
254 staticContext.statementStack.pop() |
|
255 builder.SWITCH_finish(node) |
|
256 |
|
257 return node |
|
258 |
|
259 |
|
260 elif tokenType == "for": |
|
261 node = builder.FOR_build(tokenizer) |
|
262 forBlock = None |
|
263 |
|
264 if tokenizer.match("identifier") and tokenizer.token.value == "each": |
|
265 builder.FOR_rebuildForEach(node) |
|
266 |
|
267 tokenizer.mustMatch("left_paren") |
|
268 tokenType = tokenizer.peek() |
|
269 childNode = None |
|
270 |
|
271 if tokenType != "semicolon": |
|
272 staticContext.inForLoopInit = True |
|
273 |
|
274 if tokenType == "var" or tokenType == "const": |
|
275 tokenizer.get() |
|
276 childNode = Variables(tokenizer, staticContext) |
|
277 |
|
278 elif tokenType == "let": |
|
279 tokenizer.get() |
|
280 |
|
281 if tokenizer.peek() == "left_paren": |
|
282 childNode = LetBlock(tokenizer, staticContext, False) |
|
283 |
|
284 else: |
|
285 # Let in for head, we need to add an implicit block |
|
286 # around the rest of the for. |
|
287 forBlock = builder.BLOCK_build(tokenizer, staticContext.blockId) |
|
288 staticContext.blockId += 1 |
|
289 staticContext.statementStack.append(forBlock) |
|
290 childNode = Variables(tokenizer, staticContext, forBlock) |
|
291 |
|
292 else: |
|
293 childNode = Expression(tokenizer, staticContext) |
|
294 |
|
295 staticContext.inForLoopInit = False |
|
296 |
|
297 if childNode and tokenizer.match("in"): |
|
298 builder.FOR_rebuildForIn(node) |
|
299 builder.FOR_setObject(node, Expression(tokenizer, staticContext), forBlock) |
|
300 |
|
301 if childNode.type == "var" or childNode.type == "let": |
|
302 if len(childNode) != 1: |
|
303 raise SyntaxError("Invalid for..in left-hand side", tokenizer) |
|
304 |
|
305 builder.FOR_setIterator(node, childNode, forBlock) |
|
306 |
|
307 else: |
|
308 builder.FOR_setIterator(node, childNode, forBlock) |
|
309 |
|
310 else: |
|
311 builder.FOR_setSetup(node, childNode) |
|
312 tokenizer.mustMatch("semicolon") |
|
313 |
|
314 if node.isEach: |
|
315 raise SyntaxError("Invalid for each..in loop", tokenizer) |
|
316 |
|
317 if tokenizer.peek() == "semicolon": |
|
318 builder.FOR_setCondition(node, None) |
|
319 else: |
|
320 builder.FOR_setCondition(node, Expression(tokenizer, staticContext)) |
|
321 |
|
322 tokenizer.mustMatch("semicolon") |
|
323 |
|
324 if tokenizer.peek() == "right_paren": |
|
325 builder.FOR_setUpdate(node, None) |
|
326 else: |
|
327 builder.FOR_setUpdate(node, Expression(tokenizer, staticContext)) |
|
328 |
|
329 tokenizer.mustMatch("right_paren") |
|
330 builder.FOR_setBody(node, nest(tokenizer, staticContext, node, Statement)) |
|
331 |
|
332 if forBlock: |
|
333 builder.BLOCK_finish(forBlock) |
|
334 staticContext.statementStack.pop() |
|
335 |
|
336 builder.FOR_finish(node) |
|
337 return node |
|
338 |
|
339 |
|
340 elif tokenType == "while": |
|
341 node = builder.WHILE_build(tokenizer) |
|
342 |
|
343 builder.WHILE_setCondition(node, ParenExpression(tokenizer, staticContext)) |
|
344 builder.WHILE_setBody(node, nest(tokenizer, staticContext, node, Statement)) |
|
345 builder.WHILE_finish(node) |
|
346 |
|
347 return node |
|
348 |
|
349 |
|
350 elif tokenType == "do": |
|
351 node = builder.DO_build(tokenizer) |
|
352 |
|
353 builder.DO_setBody(node, nest(tokenizer, staticContext, node, Statement, "while")) |
|
354 builder.DO_setCondition(node, ParenExpression(tokenizer, staticContext)) |
|
355 builder.DO_finish(node) |
|
356 |
|
357 if not staticContext.ecma3OnlyMode: |
|
358 # <script language="JavaScript"> (without version hints) may need |
|
359 # automatic semicolon insertion without a newline after do-while. |
|
360 # See http://bugzilla.mozilla.org/show_bug.cgi?id=238945. |
|
361 tokenizer.match("semicolon") |
|
362 return node |
|
363 |
|
364 # NO RETURN |
|
365 |
|
366 |
|
367 elif tokenType == "break" or tokenType == "continue": |
|
368 if tokenType == "break": |
|
369 node = builder.BREAK_build(tokenizer) |
|
370 else: |
|
371 node = builder.CONTINUE_build(tokenizer) |
|
372 |
|
373 if tokenizer.peekOnSameLine() == "identifier": |
|
374 tokenizer.get() |
|
375 |
|
376 if tokenType == "break": |
|
377 builder.BREAK_setLabel(node, tokenizer.token.value) |
|
378 else: |
|
379 builder.CONTINUE_setLabel(node, tokenizer.token.value) |
|
380 |
|
381 statementStack = staticContext.statementStack |
|
382 i = len(statementStack) |
|
383 label = node.label if hasattr(node, "label") else None |
|
384 |
|
385 if label: |
|
386 while True: |
|
387 i -= 1 |
|
388 if i < 0: |
|
389 raise SyntaxError("Label not found", tokenizer) |
|
390 if getattr(statementStack[i], "label", None) == label: |
|
391 break |
|
392 |
|
393 # |
|
394 # Both break and continue to label need to be handled specially |
|
395 # within a labeled loop, so that they target that loop. If not in |
|
396 # a loop, then break targets its labeled statement. Labels can be |
|
397 # nested so we skip all labels immediately enclosing the nearest |
|
398 # non-label statement. |
|
399 # |
|
400 while i < len(statementStack) - 1 and statementStack[i+1].type == "label": |
|
401 i += 1 |
|
402 |
|
403 if i < len(statementStack) - 1 and getattr(statementStack[i+1], "isLoop", False): |
|
404 i += 1 |
|
405 elif tokenType == "continue": |
|
406 raise SyntaxError("Invalid continue", tokenizer) |
|
407 |
|
408 else: |
|
409 while True: |
|
410 i -= 1 |
|
411 if i < 0: |
|
412 if tokenType == "break": |
|
413 raise SyntaxError("Invalid break", tokenizer) |
|
414 else: |
|
415 raise SyntaxError("Invalid continue", tokenizer) |
|
416 |
|
417 if getattr(statementStack[i], "isLoop", False) or (tokenType == "break" and statementStack[i].type == "switch"): |
|
418 break |
|
419 |
|
420 if tokenType == "break": |
|
421 builder.BREAK_finish(node) |
|
422 else: |
|
423 builder.CONTINUE_finish(node) |
|
424 |
|
425 # NO RETURN |
|
426 |
|
427 |
|
428 elif tokenType == "try": |
|
429 node = builder.TRY_build(tokenizer) |
|
430 builder.TRY_setTryBlock(node, Block(tokenizer, staticContext)) |
|
431 |
|
432 while tokenizer.match("catch"): |
|
433 childNode = builder.CATCH_build(tokenizer) |
|
434 tokenizer.mustMatch("left_paren") |
|
435 nextTokenType = tokenizer.get() |
|
436 |
|
437 if nextTokenType == "left_bracket" or nextTokenType == "left_curly": |
|
438 # Destructured catch identifiers. |
|
439 tokenizer.unget() |
|
440 exception = DestructuringExpression(tokenizer, staticContext, True) |
|
441 |
|
442 elif nextTokenType == "identifier": |
|
443 exception = builder.CATCH_wrapException(tokenizer) |
|
444 |
|
445 else: |
|
446 raise SyntaxError("Missing identifier in catch", tokenizer) |
|
447 |
|
448 builder.CATCH_setException(childNode, exception) |
|
449 |
|
450 if tokenizer.match("if"): |
|
451 if staticContext.ecma3OnlyMode: |
|
452 raise SyntaxError("Illegal catch guard", tokenizer) |
|
453 |
|
454 if node.getChildrenLength() > 0 and not node.getUnrelatedChildren()[0].guard: |
|
455 raise SyntaxError("Guarded catch after unguarded", tokenizer) |
|
456 |
|
457 builder.CATCH_setGuard(childNode, Expression(tokenizer, staticContext)) |
|
458 |
|
459 else: |
|
460 builder.CATCH_setGuard(childNode, None) |
|
461 |
|
462 tokenizer.mustMatch("right_paren") |
|
463 |
|
464 builder.CATCH_setBlock(childNode, Block(tokenizer, staticContext)) |
|
465 builder.CATCH_finish(childNode) |
|
466 |
|
467 builder.TRY_addCatch(node, childNode) |
|
468 |
|
469 builder.TRY_finishCatches(node) |
|
470 |
|
471 if tokenizer.match("finally"): |
|
472 builder.TRY_setFinallyBlock(node, Block(tokenizer, staticContext)) |
|
473 |
|
474 if node.getChildrenLength() == 0 and not hasattr(node, "finallyBlock"): |
|
475 raise SyntaxError("Invalid try statement", tokenizer) |
|
476 |
|
477 builder.TRY_finish(node) |
|
478 return node |
|
479 |
|
480 |
|
481 elif tokenType == "catch" or tokenType == "finally": |
|
482 raise SyntaxError(tokenizer.tokens[tokenType] + " without preceding try", tokenizer) |
|
483 |
|
484 |
|
485 elif tokenType == "throw": |
|
486 node = builder.THROW_build(tokenizer) |
|
487 |
|
488 builder.THROW_setException(node, Expression(tokenizer, staticContext)) |
|
489 builder.THROW_finish(node) |
|
490 |
|
491 # NO RETURN |
|
492 |
|
493 |
|
494 elif tokenType == "return": |
|
495 node = returnOrYield(tokenizer, staticContext) |
|
496 |
|
497 # NO RETURN |
|
498 |
|
499 |
|
500 elif tokenType == "with": |
|
501 node = builder.WITH_build(tokenizer) |
|
502 |
|
503 builder.WITH_setObject(node, ParenExpression(tokenizer, staticContext)) |
|
504 builder.WITH_setBody(node, nest(tokenizer, staticContext, node, Statement)) |
|
505 builder.WITH_finish(node) |
|
506 |
|
507 return node |
|
508 |
|
509 |
|
510 elif tokenType == "var" or tokenType == "const": |
|
511 node = Variables(tokenizer, staticContext) |
|
512 |
|
513 # NO RETURN |
|
514 |
|
515 |
|
516 elif tokenType == "let": |
|
517 if tokenizer.peek() == "left_paren": |
|
518 node = LetBlock(tokenizer, staticContext, True) |
|
519 else: |
|
520 node = Variables(tokenizer, staticContext) |
|
521 |
|
522 # NO RETURN |
|
523 |
|
524 |
|
525 elif tokenType == "debugger": |
|
526 node = builder.DEBUGGER_build(tokenizer) |
|
527 |
|
528 # NO RETURN |
|
529 |
|
530 |
|
531 elif tokenType == "newline" or tokenType == "semicolon": |
|
532 node = builder.SEMICOLON_build(tokenizer) |
|
533 |
|
534 builder.SEMICOLON_setExpression(node, None) |
|
535 builder.SEMICOLON_finish(tokenizer) |
|
536 |
|
537 return node |
|
538 |
|
539 |
|
540 else: |
|
541 if tokenType == "identifier": |
|
542 tokenType = tokenizer.peek() |
|
543 |
|
544 # Labeled statement. |
|
545 if tokenType == "colon": |
|
546 label = tokenizer.token.value |
|
547 statementStack = staticContext.statementStack |
|
548 |
|
549 i = len(statementStack)-1 |
|
550 while i >= 0: |
|
551 if getattr(statementStack[i], "label", None) == label: |
|
552 raise SyntaxError("Duplicate label", tokenizer) |
|
553 |
|
554 i -= 1 |
|
555 |
|
556 tokenizer.get() |
|
557 node = builder.LABEL_build(tokenizer) |
|
558 |
|
559 builder.LABEL_setLabel(node, label) |
|
560 builder.LABEL_setStatement(node, nest(tokenizer, staticContext, node, Statement)) |
|
561 builder.LABEL_finish(node) |
|
562 |
|
563 return node |
|
564 |
|
565 # Expression statement. |
|
566 # We unget the current token to parse the expression as a whole. |
|
567 node = builder.SEMICOLON_build(tokenizer) |
|
568 tokenizer.unget() |
|
569 builder.SEMICOLON_setExpression(node, Expression(tokenizer, staticContext)) |
|
570 node.end = node.expression.end |
|
571 builder.SEMICOLON_finish(node) |
|
572 |
|
573 # NO RETURN |
|
574 |
|
575 |
|
576 MagicalSemicolon(tokenizer) |
|
577 return node |
|
578 |
|
579 |
|
580 |
|
581 def MagicalSemicolon(tokenizer): |
|
582 if tokenizer.line == tokenizer.token.line: |
|
583 tokenType = tokenizer.peekOnSameLine() |
|
584 |
|
585 if tokenType != "end" and tokenType != "newline" and tokenType != "semicolon" and tokenType != "right_curly": |
|
586 raise SyntaxError("Missing ; before statement", tokenizer) |
|
587 |
|
588 tokenizer.match("semicolon") |
|
589 |
|
590 |
|
591 |
|
592 def returnOrYield(tokenizer, staticContext): |
|
593 builder = staticContext.builder |
|
594 tokenType = tokenizer.token.type |
|
595 |
|
596 if tokenType == "return": |
|
597 if not staticContext.inFunction: |
|
598 raise SyntaxError("Return not in function", tokenizer) |
|
599 |
|
600 node = builder.RETURN_build(tokenizer) |
|
601 |
|
602 else: |
|
603 if not staticContext.inFunction: |
|
604 raise SyntaxError("Yield not in function", tokenizer) |
|
605 |
|
606 staticContext.isGenerator = True |
|
607 node = builder.YIELD_build(tokenizer) |
|
608 |
|
609 nextTokenType = tokenizer.peek(True) |
|
610 if nextTokenType != "end" and nextTokenType != "newline" and nextTokenType != "semicolon" and nextTokenType != "right_curly" and (tokenType != "yield" or (nextTokenType != tokenType and nextTokenType != "right_bracket" and nextTokenType != "right_paren" and nextTokenType != "colon" and nextTokenType != "comma")): |
|
611 if tokenType == "return": |
|
612 builder.RETURN_setValue(node, Expression(tokenizer, staticContext)) |
|
613 staticContext.hasReturnWithValue = True |
|
614 else: |
|
615 builder.YIELD_setValue(node, AssignExpression(tokenizer, staticContext)) |
|
616 |
|
617 elif tokenType == "return": |
|
618 staticContext.hasEmptyReturn = True |
|
619 |
|
620 # Disallow return v; in generator. |
|
621 if staticContext.hasReturnWithValue and staticContext.isGenerator: |
|
622 raise SyntaxError("Generator returns a value", tokenizer) |
|
623 |
|
624 if tokenType == "return": |
|
625 builder.RETURN_finish(node) |
|
626 else: |
|
627 builder.YIELD_finish(node) |
|
628 |
|
629 return node |
|
630 |
|
631 |
|
632 |
|
633 def FunctionDefinition(tokenizer, staticContext, requireName, functionForm): |
|
634 builder = staticContext.builder |
|
635 functionNode = builder.FUNCTION_build(tokenizer) |
|
636 |
|
637 if tokenizer.match("identifier"): |
|
638 builder.FUNCTION_setName(functionNode, tokenizer.token.value) |
|
639 elif requireName: |
|
640 raise SyntaxError("Missing def identifier", tokenizer) |
|
641 |
|
642 tokenizer.mustMatch("left_paren") |
|
643 |
|
644 if not tokenizer.match("right_paren"): |
|
645 builder.FUNCTION_initParams(functionNode, tokenizer) |
|
646 prevParamNode = None |
|
647 while True: |
|
648 tokenType = tokenizer.get() |
|
649 if tokenType == "left_bracket" or tokenType == "left_curly": |
|
650 # Destructured formal parameters. |
|
651 tokenizer.unget() |
|
652 paramNode = DestructuringExpression(tokenizer, staticContext) |
|
653 |
|
654 elif tokenType == "identifier": |
|
655 paramNode = builder.FUNCTION_wrapParam(tokenizer) |
|
656 |
|
657 else: |
|
658 raise SyntaxError("Missing formal parameter", tokenizer) |
|
659 |
|
660 builder.FUNCTION_addParam(functionNode, tokenizer, paramNode) |
|
661 builder.COMMENTS_add(paramNode, prevParamNode, tokenizer.getComments()) |
|
662 |
|
663 if not tokenizer.match("comma"): |
|
664 break |
|
665 |
|
666 prevParamNode = paramNode |
|
667 |
|
668 tokenizer.mustMatch("right_paren") |
|
669 |
|
670 # Do we have an expression closure or a normal body? |
|
671 tokenType = tokenizer.get() |
|
672 if tokenType != "left_curly": |
|
673 builder.FUNCTION_setExpressionClosure(functionNode, True) |
|
674 tokenizer.unget() |
|
675 |
|
676 childContext = StaticContext(True, builder) |
|
677 tokenizer.save() |
|
678 |
|
679 if staticContext.inFunction: |
|
680 # Inner functions don't reset block numbering, only functions at |
|
681 # the top level of the program do. |
|
682 childContext.blockId = staticContext.blockId |
|
683 |
|
684 if tokenType != "left_curly": |
|
685 builder.FUNCTION_setBody(functionNode, AssignExpression(tokenizer, staticContext)) |
|
686 if staticContext.isGenerator: |
|
687 raise SyntaxError("Generator returns a value", tokenizer) |
|
688 |
|
689 else: |
|
690 builder.FUNCTION_hoistVars(childContext.blockId) |
|
691 builder.FUNCTION_setBody(functionNode, Script(tokenizer, childContext)) |
|
692 |
|
693 # |
|
694 # Hoisting makes parse-time binding analysis tricky. A taxonomy of hoists: |
|
695 # |
|
696 # 1. vars hoist to the top of their function: |
|
697 # |
|
698 # var x = 'global'; |
|
699 # function f() { |
|
700 # x = 'f'; |
|
701 # if (false) |
|
702 # var x; |
|
703 # } |
|
704 # f(); |
|
705 # print(x); // "global" |
|
706 # |
|
707 # 2. lets hoist to the top of their block: |
|
708 # |
|
709 # function f() { // id: 0 |
|
710 # var x = 'f'; |
|
711 # { |
|
712 # { |
|
713 # print(x); // "undefined" |
|
714 # } |
|
715 # let x; |
|
716 # } |
|
717 # } |
|
718 # f(); |
|
719 # |
|
720 # 3. inner functions at function top-level hoist to the beginning |
|
721 # of the function. |
|
722 # |
|
723 # If the builder used is doing parse-time analyses, hoisting may |
|
724 # invalidate earlier conclusions it makes about variable scope. |
|
725 # |
|
726 # The builder can opt to set the needsHoisting flag in a |
|
727 # CompilerContext (in the case of var and function hoisting) or in a |
|
728 # node of type BLOCK (in the case of let hoisting). This signals for |
|
729 # the parser to reparse sections of code. |
|
730 # |
|
731 # To avoid exponential blowup, if a function at the program top-level |
|
732 # has any hoists in its child blocks or inner functions, we reparse |
|
733 # the entire toplevel function. Each toplevel function is parsed at |
|
734 # most twice. |
|
735 # |
|
736 # The list of declarations can be tied to block ids to aid talking |
|
737 # about declarations of blocks that have not yet been fully parsed. |
|
738 # |
|
739 # Blocks are already uniquely numbered; see the comment in |
|
740 # Statements. |
|
741 # |
|
742 |
|
743 # |
|
744 # wpbasti: |
|
745 # Don't have the feeling that I need this functionality because the |
|
746 # tree is often modified before the variables and names inside are |
|
747 # of any interest. So better doing this in a post-scan. |
|
748 # |
|
749 |
|
750 # |
|
751 # if childContext.needsHoisting: |
|
752 # # Order is important here! Builders expect functions to come after variables! |
|
753 # builder.setHoists(functionNode.body.id, childContext.variables.concat(childContext.functions)) |
|
754 # |
|
755 # if staticContext.inFunction: |
|
756 # # If an inner function needs hoisting, we need to propagate |
|
757 # # this flag up to the parent function. |
|
758 # staticContext.needsHoisting = True |
|
759 # |
|
760 # else: |
|
761 # # Only re-parse functions at the top level of the program. |
|
762 # childContext = StaticContext(True, builder) |
|
763 # tokenizer.rewind(rp) |
|
764 # |
|
765 # # Set a flag in case the builder wants to have different behavior |
|
766 # # on the second pass. |
|
767 # builder.secondPass = True |
|
768 # builder.FUNCTION_hoistVars(functionNode.body.id, True) |
|
769 # builder.FUNCTION_setBody(functionNode, Script(tokenizer, childContext)) |
|
770 # builder.secondPass = False |
|
771 |
|
772 if tokenType == "left_curly": |
|
773 tokenizer.mustMatch("right_curly") |
|
774 |
|
775 functionNode.end = tokenizer.token.end |
|
776 functionNode.functionForm = functionForm |
|
777 |
|
778 builder.COMMENTS_add(functionNode.body, functionNode.body, tokenizer.getComments()) |
|
779 builder.FUNCTION_finish(functionNode, staticContext) |
|
780 |
|
781 return functionNode |
|
782 |
|
783 |
|
784 |
|
785 def Variables(tokenizer, staticContext, letBlock=None): |
|
786 """Parses a comma-separated list of var declarations (and maybe initializations).""" |
|
787 |
|
788 builder = staticContext.builder |
|
789 if tokenizer.token.type == "var": |
|
790 build = builder.VAR_build |
|
791 addDecl = builder.VAR_addDecl |
|
792 finish = builder.VAR_finish |
|
793 childContext = staticContext |
|
794 |
|
795 elif tokenizer.token.type == "const": |
|
796 build = builder.CONST_build |
|
797 addDecl = builder.CONST_addDecl |
|
798 finish = builder.CONST_finish |
|
799 childContext = staticContext |
|
800 |
|
801 elif tokenizer.token.type == "let" or tokenizer.token.type == "left_paren": |
|
802 build = builder.LET_build |
|
803 addDecl = builder.LET_addDecl |
|
804 finish = builder.LET_finish |
|
805 |
|
806 if not letBlock: |
|
807 statementStack = staticContext.statementStack |
|
808 i = len(statementStack) - 1 |
|
809 |
|
810 # a BLOCK *must* be found. |
|
811 while statementStack[i].type != "block": |
|
812 i -= 1 |
|
813 |
|
814 # Lets at the def toplevel are just vars, at least in SpiderMonkey. |
|
815 if i == 0: |
|
816 build = builder.VAR_build |
|
817 addDecl = builder.VAR_addDecl |
|
818 finish = builder.VAR_finish |
|
819 childContext = staticContext |
|
820 |
|
821 else: |
|
822 childContext = statementStack[i] |
|
823 |
|
824 else: |
|
825 childContext = letBlock |
|
826 |
|
827 node = build(tokenizer) |
|
828 |
|
829 while True: |
|
830 tokenType = tokenizer.get() |
|
831 |
|
832 # Done in Python port! |
|
833 # FIXME Should have a special DECLARATION node instead of overloading |
|
834 # IDENTIFIER to mean both identifier declarations and destructured |
|
835 # declarations. |
|
836 childNode = builder.DECL_build(tokenizer) |
|
837 |
|
838 if tokenType == "left_bracket" or tokenType == "left_curly": |
|
839 # Pass in childContext if we need to add each pattern matched into |
|
840 # its variables, else pass in staticContext. |
|
841 # Need to unget to parse the full destructured expression. |
|
842 tokenizer.unget() |
|
843 builder.DECL_setNames(childNode, DestructuringExpression(tokenizer, staticContext, True, childContext)) |
|
844 |
|
845 if staticContext.inForLoopInit and tokenizer.peek() == "in": |
|
846 addDecl(node, childNode, childContext) |
|
847 if tokenizer.match("comma"): |
|
848 continue |
|
849 else: |
|
850 break |
|
851 |
|
852 tokenizer.mustMatch("assign") |
|
853 if tokenizer.token.assignOp: |
|
854 raise SyntaxError("Invalid variable initialization", tokenizer) |
|
855 |
|
856 # Parse the init as a normal assignment. |
|
857 builder.DECL_setInitializer(childNode, AssignExpression(tokenizer, staticContext)) |
|
858 builder.DECL_finish(childNode) |
|
859 addDecl(node, childNode, childContext) |
|
860 |
|
861 # Copy over names for variable list |
|
862 # for nameNode in childNode.names: |
|
863 # childContext.variables.add(nameNode.value) |
|
864 |
|
865 if tokenizer.match("comma"): |
|
866 continue |
|
867 else: |
|
868 break |
|
869 |
|
870 if tokenType != "identifier": |
|
871 raise SyntaxError("Missing variable name", tokenizer) |
|
872 |
|
873 builder.DECL_setName(childNode, tokenizer.token.value) |
|
874 builder.DECL_setReadOnly(childNode, node.type == "const") |
|
875 addDecl(node, childNode, childContext) |
|
876 |
|
877 if tokenizer.match("assign"): |
|
878 if tokenizer.token.assignOp: |
|
879 raise SyntaxError("Invalid variable initialization", tokenizer) |
|
880 |
|
881 initializerNode = AssignExpression(tokenizer, staticContext) |
|
882 builder.DECL_setInitializer(childNode, initializerNode) |
|
883 |
|
884 builder.DECL_finish(childNode) |
|
885 |
|
886 # If we directly use the node in "let" constructs |
|
887 # if not hasattr(childContext, "variables"): |
|
888 # childContext.variables = set() |
|
889 |
|
890 # childContext.variables.add(childNode.name) |
|
891 |
|
892 if not tokenizer.match("comma"): |
|
893 break |
|
894 |
|
895 finish(node) |
|
896 return node |
|
897 |
|
898 |
|
899 |
|
900 def LetBlock(tokenizer, staticContext, isStatement): |
|
901 """Does not handle let inside of for loop init.""" |
|
902 builder = staticContext.builder |
|
903 |
|
904 # tokenizer.token.type must be "let" |
|
905 node = builder.LETBLOCK_build(tokenizer) |
|
906 tokenizer.mustMatch("left_paren") |
|
907 builder.LETBLOCK_setVariables(node, Variables(tokenizer, staticContext, node)) |
|
908 tokenizer.mustMatch("right_paren") |
|
909 |
|
910 if isStatement and tokenizer.peek() != "left_curly": |
|
911 # If this is really an expression in let statement guise, then we |
|
912 # need to wrap the "let_block" node in a "semicolon" node so that we pop |
|
913 # the return value of the expression. |
|
914 childNode = builder.SEMICOLON_build(tokenizer) |
|
915 builder.SEMICOLON_setExpression(childNode, node) |
|
916 builder.SEMICOLON_finish(childNode) |
|
917 isStatement = False |
|
918 |
|
919 if isStatement: |
|
920 childNode = Block(tokenizer, staticContext) |
|
921 builder.LETBLOCK_setBlock(node, childNode) |
|
922 |
|
923 else: |
|
924 childNode = AssignExpression(tokenizer, staticContext) |
|
925 builder.LETBLOCK_setExpression(node, childNode) |
|
926 |
|
927 builder.LETBLOCK_finish(node) |
|
928 return node |
|
929 |
|
930 |
|
931 def checkDestructuring(tokenizer, staticContext, node, simpleNamesOnly=None, data=None): |
|
932 if node.type == "array_comp": |
|
933 raise SyntaxError("Invalid array comprehension left-hand side", tokenizer) |
|
934 |
|
935 if node.type != "array_init" and node.type != "object_init": |
|
936 return |
|
937 |
|
938 builder = staticContext.builder |
|
939 |
|
940 for child in node: |
|
941 if child == None: |
|
942 continue |
|
943 |
|
944 if child.type == "property_init": |
|
945 lhs = child[0] |
|
946 rhs = child[1] |
|
947 else: |
|
948 lhs = None |
|
949 rhs = None |
|
950 |
|
951 |
|
952 if rhs and (rhs.type == "array_init" or rhs.type == "object_init"): |
|
953 checkDestructuring(tokenizer, staticContext, rhs, simpleNamesOnly, data) |
|
954 |
|
955 if lhs and simpleNamesOnly: |
|
956 # In declarations, lhs must be simple names |
|
957 if lhs.type != "identifier": |
|
958 raise SyntaxError("Missing name in pattern", tokenizer) |
|
959 |
|
960 elif data: |
|
961 childNode = builder.DECL_build(tokenizer) |
|
962 builder.DECL_setName(childNode, lhs.value) |
|
963 |
|
964 # Don't need to set initializer because it's just for |
|
965 # hoisting anyways. |
|
966 builder.DECL_finish(childNode) |
|
967 |
|
968 # Each pattern needs to be added to variables. |
|
969 # data.variables.add(childNode.name) |
|
970 |
|
971 |
|
972 # JavaScript 1.7 |
|
973 def DestructuringExpression(tokenizer, staticContext, simpleNamesOnly=None, data=None): |
|
974 node = PrimaryExpression(tokenizer, staticContext) |
|
975 checkDestructuring(tokenizer, staticContext, node, simpleNamesOnly, data) |
|
976 |
|
977 return node |
|
978 |
|
979 |
|
980 # JavsScript 1.7 |
|
981 def GeneratorExpression(tokenizer, staticContext, expression): |
|
982 builder = staticContext.builder |
|
983 node = builder.GENERATOR_build(tokenizer) |
|
984 |
|
985 builder.GENERATOR_setExpression(node, expression) |
|
986 builder.GENERATOR_setTail(node, comprehensionTail(tokenizer, staticContext)) |
|
987 builder.GENERATOR_finish(node) |
|
988 |
|
989 return node |
|
990 |
|
991 |
|
992 # JavaScript 1.7 Comprehensions Tails (Generators / Arrays) |
|
993 def comprehensionTail(tokenizer, staticContext): |
|
994 builder = staticContext.builder |
|
995 |
|
996 # tokenizer.token.type must be "for" |
|
997 body = builder.COMPTAIL_build(tokenizer) |
|
998 |
|
999 while True: |
|
1000 node = builder.FOR_build(tokenizer) |
|
1001 |
|
1002 # Comprehension tails are always for..in loops. |
|
1003 builder.FOR_rebuildForIn(node) |
|
1004 if tokenizer.match("identifier"): |
|
1005 # But sometimes they're for each..in. |
|
1006 if tokenizer.token.value == "each": |
|
1007 builder.FOR_rebuildForEach(node) |
|
1008 else: |
|
1009 tokenizer.unget() |
|
1010 |
|
1011 tokenizer.mustMatch("left_paren") |
|
1012 |
|
1013 tokenType = tokenizer.get() |
|
1014 if tokenType == "left_bracket" or tokenType == "left_curly": |
|
1015 tokenizer.unget() |
|
1016 # Destructured left side of for in comprehension tails. |
|
1017 builder.FOR_setIterator(node, DestructuringExpression(tokenizer, staticContext)) |
|
1018 |
|
1019 elif tokenType == "identifier": |
|
1020 # Removed variable/declaration substructure in Python port. |
|
1021 # Variable declarations are not allowed here. So why process them in such a way? |
|
1022 |
|
1023 # declaration = builder.DECL_build(tokenizer) |
|
1024 # builder.DECL_setName(declaration, tokenizer.token.value) |
|
1025 # builder.DECL_finish(declaration) |
|
1026 # childNode = builder.VAR_build(tokenizer) |
|
1027 # builder.VAR_addDecl(childNode, declaration) |
|
1028 # builder.VAR_finish(childNode) |
|
1029 # builder.FOR_setIterator(node, declaration) |
|
1030 |
|
1031 # Don't add to variables since the semantics of comprehensions is |
|
1032 # such that the variables are in their own def when desugared. |
|
1033 |
|
1034 identifier = builder.PRIMARY_build(tokenizer, "identifier") |
|
1035 builder.FOR_setIterator(node, identifier) |
|
1036 |
|
1037 else: |
|
1038 raise SyntaxError("Missing identifier", tokenizer) |
|
1039 |
|
1040 tokenizer.mustMatch("in") |
|
1041 builder.FOR_setObject(node, Expression(tokenizer, staticContext)) |
|
1042 tokenizer.mustMatch("right_paren") |
|
1043 builder.COMPTAIL_addFor(body, node) |
|
1044 |
|
1045 if not tokenizer.match("for"): |
|
1046 break |
|
1047 |
|
1048 # Optional guard. |
|
1049 if tokenizer.match("if"): |
|
1050 builder.COMPTAIL_setGuard(body, ParenExpression(tokenizer, staticContext)) |
|
1051 |
|
1052 builder.COMPTAIL_finish(body) |
|
1053 |
|
1054 return body |
|
1055 |
|
1056 |
|
1057 def ParenExpression(tokenizer, staticContext): |
|
1058 tokenizer.mustMatch("left_paren") |
|
1059 |
|
1060 # Always accept the 'in' operator in a parenthesized expression, |
|
1061 # where it's unambiguous, even if we might be parsing the init of a |
|
1062 # for statement. |
|
1063 oldLoopInit = staticContext.inForLoopInit |
|
1064 staticContext.inForLoopInit = False |
|
1065 node = Expression(tokenizer, staticContext) |
|
1066 staticContext.inForLoopInit = oldLoopInit |
|
1067 |
|
1068 err = "expression must be parenthesized" |
|
1069 if tokenizer.match("for"): |
|
1070 if node.type == "yield" and not node.parenthesized: |
|
1071 raise SyntaxError("Yield " + err, tokenizer) |
|
1072 |
|
1073 if node.type == "comma" and not node.parenthesized: |
|
1074 raise SyntaxError("Generator " + err, tokenizer) |
|
1075 |
|
1076 node = GeneratorExpression(tokenizer, staticContext, node) |
|
1077 |
|
1078 tokenizer.mustMatch("right_paren") |
|
1079 |
|
1080 return node |
|
1081 |
|
1082 |
|
1083 def Expression(tokenizer, staticContext): |
|
1084 """Top-down expression parser matched against SpiderMonkey.""" |
|
1085 builder = staticContext.builder |
|
1086 node = AssignExpression(tokenizer, staticContext) |
|
1087 |
|
1088 if tokenizer.match("comma"): |
|
1089 childNode = builder.COMMA_build(tokenizer) |
|
1090 builder.COMMA_addOperand(childNode, node) |
|
1091 node = childNode |
|
1092 while True: |
|
1093 childNode = node[len(node)-1] |
|
1094 if childNode.type == "yield" and not childNode.parenthesized: |
|
1095 raise SyntaxError("Yield expression must be parenthesized", tokenizer) |
|
1096 builder.COMMA_addOperand(node, AssignExpression(tokenizer, staticContext)) |
|
1097 |
|
1098 if not tokenizer.match("comma"): |
|
1099 break |
|
1100 |
|
1101 builder.COMMA_finish(node) |
|
1102 |
|
1103 return node |
|
1104 |
|
1105 |
|
1106 def AssignExpression(tokenizer, staticContext): |
|
1107 builder = staticContext.builder |
|
1108 |
|
1109 # Have to treat yield like an operand because it could be the leftmost |
|
1110 # operand of the expression. |
|
1111 if tokenizer.match("yield", True): |
|
1112 return returnOrYield(tokenizer, staticContext) |
|
1113 |
|
1114 comments = tokenizer.getComments() |
|
1115 node = builder.ASSIGN_build(tokenizer) |
|
1116 lhs = ConditionalExpression(tokenizer, staticContext) |
|
1117 builder.COMMENTS_add(lhs, None, comments) |
|
1118 |
|
1119 if not tokenizer.match("assign"): |
|
1120 builder.ASSIGN_finish(node) |
|
1121 return lhs |
|
1122 |
|
1123 if lhs.type == "object_init" or lhs.type == "array_init": |
|
1124 checkDestructuring(tokenizer, staticContext, lhs) |
|
1125 elif lhs.type == "identifier" or lhs.type == "dot" or lhs.type == "index" or lhs.type == "call": |
|
1126 pass |
|
1127 else: |
|
1128 raise SyntaxError("Bad left-hand side of assignment", tokenizer) |
|
1129 |
|
1130 builder.ASSIGN_setAssignOp(node, tokenizer.token.assignOp) |
|
1131 builder.ASSIGN_addOperand(node, lhs) |
|
1132 builder.ASSIGN_addOperand(node, AssignExpression(tokenizer, staticContext)) |
|
1133 builder.ASSIGN_finish(node) |
|
1134 |
|
1135 return node |
|
1136 |
|
1137 |
|
1138 def ConditionalExpression(tokenizer, staticContext): |
|
1139 builder = staticContext.builder |
|
1140 node = OrExpression(tokenizer, staticContext) |
|
1141 |
|
1142 if tokenizer.match("hook"): |
|
1143 childNode = node |
|
1144 node = builder.HOOK_build(tokenizer) |
|
1145 builder.HOOK_setCondition(node, childNode) |
|
1146 |
|
1147 # Always accept the 'in' operator in the middle clause of a ternary, |
|
1148 # where it's unambiguous, even if we might be parsing the init of a |
|
1149 # for statement. |
|
1150 oldLoopInit = staticContext.inForLoopInit |
|
1151 staticContext.inForLoopInit = False |
|
1152 builder.HOOK_setThenPart(node, AssignExpression(tokenizer, staticContext)) |
|
1153 staticContext.inForLoopInit = oldLoopInit |
|
1154 |
|
1155 if not tokenizer.match("colon"): |
|
1156 raise SyntaxError("Missing : after ?", tokenizer) |
|
1157 |
|
1158 builder.HOOK_setElsePart(node, AssignExpression(tokenizer, staticContext)) |
|
1159 builder.HOOK_finish(node) |
|
1160 |
|
1161 return node |
|
1162 |
|
1163 |
|
1164 def OrExpression(tokenizer, staticContext): |
|
1165 builder = staticContext.builder |
|
1166 node = AndExpression(tokenizer, staticContext) |
|
1167 |
|
1168 while tokenizer.match("or"): |
|
1169 childNode = builder.OR_build(tokenizer) |
|
1170 builder.OR_addOperand(childNode, node) |
|
1171 builder.OR_addOperand(childNode, AndExpression(tokenizer, staticContext)) |
|
1172 builder.OR_finish(childNode) |
|
1173 node = childNode |
|
1174 |
|
1175 return node |
|
1176 |
|
1177 |
|
1178 def AndExpression(tokenizer, staticContext): |
|
1179 builder = staticContext.builder |
|
1180 node = BitwiseOrExpression(tokenizer, staticContext) |
|
1181 |
|
1182 while tokenizer.match("and"): |
|
1183 childNode = builder.AND_build(tokenizer) |
|
1184 builder.AND_addOperand(childNode, node) |
|
1185 builder.AND_addOperand(childNode, BitwiseOrExpression(tokenizer, staticContext)) |
|
1186 builder.AND_finish(childNode) |
|
1187 node = childNode |
|
1188 |
|
1189 return node |
|
1190 |
|
1191 |
|
1192 def BitwiseOrExpression(tokenizer, staticContext): |
|
1193 builder = staticContext.builder |
|
1194 node = BitwiseXorExpression(tokenizer, staticContext) |
|
1195 |
|
1196 while tokenizer.match("bitwise_or"): |
|
1197 childNode = builder.BITWISEOR_build(tokenizer) |
|
1198 builder.BITWISEOR_addOperand(childNode, node) |
|
1199 builder.BITWISEOR_addOperand(childNode, BitwiseXorExpression(tokenizer, staticContext)) |
|
1200 builder.BITWISEOR_finish(childNode) |
|
1201 node = childNode |
|
1202 |
|
1203 return node |
|
1204 |
|
1205 |
|
1206 def BitwiseXorExpression(tokenizer, staticContext): |
|
1207 builder = staticContext.builder |
|
1208 node = BitwiseAndExpression(tokenizer, staticContext) |
|
1209 |
|
1210 while tokenizer.match("bitwise_xor"): |
|
1211 childNode = builder.BITWISEXOR_build(tokenizer) |
|
1212 builder.BITWISEXOR_addOperand(childNode, node) |
|
1213 builder.BITWISEXOR_addOperand(childNode, BitwiseAndExpression(tokenizer, staticContext)) |
|
1214 builder.BITWISEXOR_finish(childNode) |
|
1215 node = childNode |
|
1216 |
|
1217 return node |
|
1218 |
|
1219 |
|
1220 def BitwiseAndExpression(tokenizer, staticContext): |
|
1221 builder = staticContext.builder |
|
1222 node = EqualityExpression(tokenizer, staticContext) |
|
1223 |
|
1224 while tokenizer.match("bitwise_and"): |
|
1225 childNode = builder.BITWISEAND_build(tokenizer) |
|
1226 builder.BITWISEAND_addOperand(childNode, node) |
|
1227 builder.BITWISEAND_addOperand(childNode, EqualityExpression(tokenizer, staticContext)) |
|
1228 builder.BITWISEAND_finish(childNode) |
|
1229 node = childNode |
|
1230 |
|
1231 return node |
|
1232 |
|
1233 |
|
1234 def EqualityExpression(tokenizer, staticContext): |
|
1235 builder = staticContext.builder |
|
1236 node = RelationalExpression(tokenizer, staticContext) |
|
1237 |
|
1238 while tokenizer.match("eq") or tokenizer.match("ne") or tokenizer.match("strict_eq") or tokenizer.match("strict_ne"): |
|
1239 childNode = builder.EQUALITY_build(tokenizer) |
|
1240 builder.EQUALITY_addOperand(childNode, node) |
|
1241 builder.EQUALITY_addOperand(childNode, RelationalExpression(tokenizer, staticContext)) |
|
1242 builder.EQUALITY_finish(childNode) |
|
1243 node = childNode |
|
1244 |
|
1245 return node |
|
1246 |
|
1247 |
|
1248 def RelationalExpression(tokenizer, staticContext): |
|
1249 builder = staticContext.builder |
|
1250 oldLoopInit = staticContext.inForLoopInit |
|
1251 |
|
1252 # Uses of the in operator in shiftExprs are always unambiguous, |
|
1253 # so unset the flag that prohibits recognizing it. |
|
1254 staticContext.inForLoopInit = False |
|
1255 node = ShiftExpression(tokenizer, staticContext) |
|
1256 |
|
1257 while tokenizer.match("lt") or tokenizer.match("le") or tokenizer.match("ge") or tokenizer.match("gt") or (oldLoopInit == False and tokenizer.match("in")) or tokenizer.match("instanceof"): |
|
1258 childNode = builder.RELATIONAL_build(tokenizer) |
|
1259 builder.RELATIONAL_addOperand(childNode, node) |
|
1260 builder.RELATIONAL_addOperand(childNode, ShiftExpression(tokenizer, staticContext)) |
|
1261 builder.RELATIONAL_finish(childNode) |
|
1262 node = childNode |
|
1263 |
|
1264 staticContext.inForLoopInit = oldLoopInit |
|
1265 |
|
1266 return node |
|
1267 |
|
1268 |
|
1269 def ShiftExpression(tokenizer, staticContext): |
|
1270 builder = staticContext.builder |
|
1271 node = AddExpression(tokenizer, staticContext) |
|
1272 |
|
1273 while tokenizer.match("lsh") or tokenizer.match("rsh") or tokenizer.match("ursh"): |
|
1274 childNode = builder.SHIFT_build(tokenizer) |
|
1275 builder.SHIFT_addOperand(childNode, node) |
|
1276 builder.SHIFT_addOperand(childNode, AddExpression(tokenizer, staticContext)) |
|
1277 builder.SHIFT_finish(childNode) |
|
1278 node = childNode |
|
1279 |
|
1280 return node |
|
1281 |
|
1282 |
|
1283 def AddExpression(tokenizer, staticContext): |
|
1284 builder = staticContext.builder |
|
1285 node = MultiplyExpression(tokenizer, staticContext) |
|
1286 |
|
1287 while tokenizer.match("plus") or tokenizer.match("minus"): |
|
1288 childNode = builder.ADD_build(tokenizer) |
|
1289 builder.ADD_addOperand(childNode, node) |
|
1290 builder.ADD_addOperand(childNode, MultiplyExpression(tokenizer, staticContext)) |
|
1291 builder.ADD_finish(childNode) |
|
1292 node = childNode |
|
1293 |
|
1294 return node |
|
1295 |
|
1296 |
|
1297 def MultiplyExpression(tokenizer, staticContext): |
|
1298 builder = staticContext.builder |
|
1299 node = UnaryExpression(tokenizer, staticContext) |
|
1300 |
|
1301 while tokenizer.match("mul") or tokenizer.match("div") or tokenizer.match("mod"): |
|
1302 childNode = builder.MULTIPLY_build(tokenizer) |
|
1303 builder.MULTIPLY_addOperand(childNode, node) |
|
1304 builder.MULTIPLY_addOperand(childNode, UnaryExpression(tokenizer, staticContext)) |
|
1305 builder.MULTIPLY_finish(childNode) |
|
1306 node = childNode |
|
1307 |
|
1308 return node |
|
1309 |
|
1310 |
|
1311 def UnaryExpression(tokenizer, staticContext): |
|
1312 builder = staticContext.builder |
|
1313 tokenType = tokenizer.get(True) |
|
1314 |
|
1315 if tokenType in ["delete", "void", "typeof", "not", "bitwise_not", "plus", "minus"]: |
|
1316 node = builder.UNARY_build(tokenizer) |
|
1317 builder.UNARY_addOperand(node, UnaryExpression(tokenizer, staticContext)) |
|
1318 |
|
1319 elif tokenType == "increment" or tokenType == "decrement": |
|
1320 # Prefix increment/decrement. |
|
1321 node = builder.UNARY_build(tokenizer) |
|
1322 builder.UNARY_addOperand(node, MemberExpression(tokenizer, staticContext, True)) |
|
1323 |
|
1324 else: |
|
1325 tokenizer.unget() |
|
1326 node = MemberExpression(tokenizer, staticContext, True) |
|
1327 |
|
1328 # Don't look across a newline boundary for a postfix {in,de}crement. |
|
1329 if tokenizer.tokens[(tokenizer.tokenIndex + tokenizer.lookahead - 1) & 3].line == tokenizer.line: |
|
1330 if tokenizer.match("increment") or tokenizer.match("decrement"): |
|
1331 childNode = builder.UNARY_build(tokenizer) |
|
1332 builder.UNARY_setPostfix(childNode) |
|
1333 builder.UNARY_finish(node) |
|
1334 builder.UNARY_addOperand(childNode, node) |
|
1335 node = childNode |
|
1336 |
|
1337 builder.UNARY_finish(node) |
|
1338 return node |
|
1339 |
|
1340 |
|
1341 def MemberExpression(tokenizer, staticContext, allowCallSyntax): |
|
1342 builder = staticContext.builder |
|
1343 |
|
1344 if tokenizer.match("new"): |
|
1345 node = builder.MEMBER_build(tokenizer) |
|
1346 builder.MEMBER_addOperand(node, MemberExpression(tokenizer, staticContext, False)) |
|
1347 |
|
1348 if tokenizer.match("left_paren"): |
|
1349 builder.MEMBER_rebuildNewWithArgs(node) |
|
1350 builder.MEMBER_addOperand(node, ArgumentList(tokenizer, staticContext)) |
|
1351 |
|
1352 builder.MEMBER_finish(node) |
|
1353 |
|
1354 else: |
|
1355 node = PrimaryExpression(tokenizer, staticContext) |
|
1356 |
|
1357 while True: |
|
1358 tokenType = tokenizer.get() |
|
1359 if tokenType == "end": |
|
1360 break |
|
1361 |
|
1362 if tokenType == "dot": |
|
1363 childNode = builder.MEMBER_build(tokenizer) |
|
1364 builder.MEMBER_addOperand(childNode, node) |
|
1365 tokenizer.mustMatch("identifier") |
|
1366 builder.MEMBER_addOperand(childNode, builder.MEMBER_build(tokenizer)) |
|
1367 |
|
1368 elif tokenType == "left_bracket": |
|
1369 childNode = builder.MEMBER_build(tokenizer, "index") |
|
1370 builder.MEMBER_addOperand(childNode, node) |
|
1371 builder.MEMBER_addOperand(childNode, Expression(tokenizer, staticContext)) |
|
1372 tokenizer.mustMatch("right_bracket") |
|
1373 |
|
1374 elif tokenType == "left_paren" and allowCallSyntax: |
|
1375 childNode = builder.MEMBER_build(tokenizer, "call") |
|
1376 builder.MEMBER_addOperand(childNode, node) |
|
1377 builder.MEMBER_addOperand(childNode, ArgumentList(tokenizer, staticContext)) |
|
1378 |
|
1379 else: |
|
1380 tokenizer.unget() |
|
1381 return node |
|
1382 |
|
1383 builder.MEMBER_finish(childNode) |
|
1384 node = childNode |
|
1385 |
|
1386 return node |
|
1387 |
|
1388 |
|
1389 def ArgumentList(tokenizer, staticContext): |
|
1390 builder = staticContext.builder |
|
1391 node = builder.LIST_build(tokenizer) |
|
1392 |
|
1393 if tokenizer.match("right_paren", True): |
|
1394 return node |
|
1395 |
|
1396 while True: |
|
1397 childNode = AssignExpression(tokenizer, staticContext) |
|
1398 if childNode.type == "yield" and not childNode.parenthesized and tokenizer.peek() == "comma": |
|
1399 raise SyntaxError("Yield expression must be parenthesized", tokenizer) |
|
1400 |
|
1401 if tokenizer.match("for"): |
|
1402 childNode = GeneratorExpression(tokenizer, staticContext, childNode) |
|
1403 if len(node) > 1 or tokenizer.peek(True) == "comma": |
|
1404 raise SyntaxError("Generator expression must be parenthesized", tokenizer) |
|
1405 |
|
1406 builder.LIST_addOperand(node, childNode) |
|
1407 if not tokenizer.match("comma"): |
|
1408 break |
|
1409 |
|
1410 tokenizer.mustMatch("right_paren") |
|
1411 builder.LIST_finish(node) |
|
1412 |
|
1413 return node |
|
1414 |
|
1415 |
|
1416 def PrimaryExpression(tokenizer, staticContext): |
|
1417 builder = staticContext.builder |
|
1418 tokenType = tokenizer.get(True) |
|
1419 |
|
1420 if tokenType == "function": |
|
1421 node = FunctionDefinition(tokenizer, staticContext, False, "expressed_form") |
|
1422 |
|
1423 elif tokenType == "left_bracket": |
|
1424 node = builder.ARRAYINIT_build(tokenizer) |
|
1425 while True: |
|
1426 tokenType = tokenizer.peek(True) |
|
1427 if tokenType == "right_bracket": |
|
1428 break |
|
1429 |
|
1430 if tokenType == "comma": |
|
1431 tokenizer.get() |
|
1432 builder.ARRAYINIT_addElement(node, None) |
|
1433 continue |
|
1434 |
|
1435 builder.ARRAYINIT_addElement(node, AssignExpression(tokenizer, staticContext)) |
|
1436 |
|
1437 if tokenType != "comma" and not tokenizer.match("comma"): |
|
1438 break |
|
1439 |
|
1440 # If we matched exactly one element and got a "for", we have an |
|
1441 # array comprehension. |
|
1442 if len(node) == 1 and tokenizer.match("for"): |
|
1443 childNode = builder.ARRAYCOMP_build(tokenizer) |
|
1444 builder.ARRAYCOMP_setExpression(childNode, node[0]) |
|
1445 builder.ARRAYCOMP_setTail(childNode, comprehensionTail(tokenizer, staticContext)) |
|
1446 node = childNode |
|
1447 |
|
1448 builder.COMMENTS_add(node, node, tokenizer.getComments()) |
|
1449 tokenizer.mustMatch("right_bracket") |
|
1450 builder.PRIMARY_finish(node) |
|
1451 |
|
1452 elif tokenType == "left_curly": |
|
1453 node = builder.OBJECTINIT_build(tokenizer) |
|
1454 |
|
1455 if not tokenizer.match("right_curly"): |
|
1456 while True: |
|
1457 tokenType = tokenizer.get() |
|
1458 tokenValue = getattr(tokenizer.token, "value", None) |
|
1459 comments = tokenizer.getComments() |
|
1460 |
|
1461 if tokenValue in ("get", "set") and tokenizer.peek() == "identifier": |
|
1462 if staticContext.ecma3OnlyMode: |
|
1463 raise SyntaxError("Illegal property accessor", tokenizer) |
|
1464 |
|
1465 fd = FunctionDefinition(tokenizer, staticContext, True, "expressed_form") |
|
1466 builder.OBJECTINIT_addProperty(node, fd) |
|
1467 |
|
1468 else: |
|
1469 if tokenType == "identifier" or tokenType == "number" or tokenType == "string": |
|
1470 id = builder.PRIMARY_build(tokenizer, "identifier") |
|
1471 builder.PRIMARY_finish(id) |
|
1472 |
|
1473 elif tokenType == "right_curly": |
|
1474 if staticContext.ecma3OnlyMode: |
|
1475 raise SyntaxError("Illegal trailing ,", tokenizer) |
|
1476 |
|
1477 tokenizer.unget() |
|
1478 break |
|
1479 |
|
1480 else: |
|
1481 if tokenValue in jasy.js.tokenize.Lang.keywords: |
|
1482 id = builder.PRIMARY_build(tokenizer, "identifier") |
|
1483 builder.PRIMARY_finish(id) |
|
1484 else: |
|
1485 print("Value is '%s'" % tokenValue) |
|
1486 raise SyntaxError("Invalid property name", tokenizer) |
|
1487 |
|
1488 if tokenizer.match("colon"): |
|
1489 childNode = builder.PROPERTYINIT_build(tokenizer) |
|
1490 builder.COMMENTS_add(childNode, node, comments) |
|
1491 builder.PROPERTYINIT_addOperand(childNode, id) |
|
1492 builder.PROPERTYINIT_addOperand(childNode, AssignExpression(tokenizer, staticContext)) |
|
1493 builder.PROPERTYINIT_finish(childNode) |
|
1494 builder.OBJECTINIT_addProperty(node, childNode) |
|
1495 |
|
1496 else: |
|
1497 # Support, e.g., |var {staticContext, y} = o| as destructuring shorthand |
|
1498 # for |var {staticContext: staticContext, y: y} = o|, per proposed JS2/ES4 for JS1.8. |
|
1499 if tokenizer.peek() != "comma" and tokenizer.peek() != "right_curly": |
|
1500 raise SyntaxError("Missing : after property", tokenizer) |
|
1501 builder.OBJECTINIT_addProperty(node, id) |
|
1502 |
|
1503 if not tokenizer.match("comma"): |
|
1504 break |
|
1505 |
|
1506 builder.COMMENTS_add(node, node, tokenizer.getComments()) |
|
1507 tokenizer.mustMatch("right_curly") |
|
1508 |
|
1509 builder.OBJECTINIT_finish(node) |
|
1510 |
|
1511 elif tokenType == "left_paren": |
|
1512 # ParenExpression does its own matching on parentheses, so we need to unget. |
|
1513 tokenizer.unget() |
|
1514 node = ParenExpression(tokenizer, staticContext) |
|
1515 node.parenthesized = True |
|
1516 |
|
1517 elif tokenType == "let": |
|
1518 node = LetBlock(tokenizer, staticContext, False) |
|
1519 |
|
1520 elif tokenType in ["null", "this", "true", "false", "identifier", "number", "string", "regexp"]: |
|
1521 node = builder.PRIMARY_build(tokenizer, tokenType) |
|
1522 builder.PRIMARY_finish(node) |
|
1523 |
|
1524 else: |
|
1525 raise SyntaxError("Missing operand. Found type: %s" % tokenType, tokenizer) |
|
1526 |
|
1527 return node |