4 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
5 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
6 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include "interpreter.h"
35 #include "CommonIdentifiers.h"
36 #include <wtf/MathExtras.h>
38 // Not sure why, but yacc doesn't add this define along with the others.
39 #define yylloc kjsyylloc
41 #define YYMAXDEPTH 10000
42 #define YYENABLE_NLS 0
44 /* default values for bison */
47 // avoid triggering warnings in older bison
48 #define YYERROR_VERBOSE
51 extern int kjsyylex();
52 int kjsyyerror(const char *);
53 static bool allowAutomaticSemicolon();
55 #define AUTO_SEMICOLON do { if (!allowAutomaticSemicolon()) YYABORT; } while (0)
56 #define DBG(l, s, e) (l)->setLoc((s).first_line, (e).last_line)
60 static Node* makeAssignNode(Node* loc, Operator, Node* expr);
61 static Node* makePrefixNode(Node* expr, Operator);
62 static Node* makePostfixNode(Node* expr, Operator);
63 static PropertyNode* makeGetterOrSetterPropertyNode(const Identifier &getOrSet, const Identifier& name, ParameterNode*, FunctionBodyNode*);
64 static Node *makeFunctionCallNode(Node *func, ArgumentsNode *args);
65 static Node *makeTypeOfNode(Node *expr);
66 static Node *makeDeleteNode(Node *expr);
67 static Node *makeNegateNode(Node *expr);
68 static Node* makeNumberNode(double);
72 #pragma warning(disable: 4065)
73 #pragma warning(disable: 4244)
74 #pragma warning(disable: 4702)
76 // At least some of the time, the declarations of malloc and free that bison
77 // generates are causing warnings. A way to avoid this is to explicitly define
78 // the macros so that bison doesn't try to declare malloc and free.
79 #define YYMALLOC malloc
94 FunctionBodyNode *body;
96 FuncExprNode *funcExpr;
116 %token NULLTOKEN TRUETOKEN FALSETOKEN
119 %token BREAK CASE DEFAULT FOR NEW VAR CONSTTOKEN CONTINUE
120 %token FUNCTION RETURN VOIDTOKEN DELETETOKEN
121 %token IF THISTOKEN DO WHILE INTOKEN INSTANCEOF TYPEOF
122 %token SWITCH WITH RESERVED
123 %token THROW TRY CATCH FINALLY
126 /* give an if without an else higher precedence than an else to resolve the ambiguity */
127 %nonassoc IF_WITHOUT_ELSE
131 %token EQEQ NE /* == and != */
132 %token STREQ STRNEQ /* === and !== */
133 %token LE GE /* < and > */
134 %token OR AND /* || and && */
135 %token PLUSPLUS MINUSMINUS /* ++ and -- */
136 %token LSHIFT /* << */
137 %token RSHIFT URSHIFT /* >> and >>> */
138 %token PLUSEQUAL MINUSEQUAL /* += and -= */
139 %token MULTEQUAL DIVEQUAL /* *= and /= */
140 %token LSHIFTEQUAL /* <<= */
141 %token RSHIFTEQUAL URSHIFTEQUAL /* >>= and >>>= */
142 %token ANDEQUAL MODEQUAL /* &= and %= */
143 %token XOREQUAL OREQUAL /* ^= and |= */
150 /* automatically inserted semicolon */
151 %token AUTOPLUSPLUS AUTOMINUSMINUS
153 /* non-terminal types */
154 %type <node> Literal ArrayLiteral
156 %type <node> PrimaryExpr PrimaryExprNoBrace
157 %type <node> MemberExpr MemberExprNoBF /* BF => brace or function */
158 %type <node> NewExpr NewExprNoBF
159 %type <node> CallExpr CallExprNoBF
160 %type <node> LeftHandSideExpr LeftHandSideExprNoBF
161 %type <node> PostfixExpr PostfixExprNoBF
162 %type <node> UnaryExpr UnaryExprNoBF UnaryExprCommon
163 %type <node> MultiplicativeExpr MultiplicativeExprNoBF
164 %type <node> AdditiveExpr AdditiveExprNoBF
165 %type <node> ShiftExpr ShiftExprNoBF
166 %type <node> RelationalExpr RelationalExprNoIn RelationalExprNoBF
167 %type <node> EqualityExpr EqualityExprNoIn EqualityExprNoBF
168 %type <node> BitwiseANDExpr BitwiseANDExprNoIn BitwiseANDExprNoBF
169 %type <node> BitwiseXORExpr BitwiseXORExprNoIn BitwiseXORExprNoBF
170 %type <node> BitwiseORExpr BitwiseORExprNoIn BitwiseORExprNoBF
171 %type <node> LogicalANDExpr LogicalANDExprNoIn LogicalANDExprNoBF
172 %type <node> LogicalORExpr LogicalORExprNoIn LogicalORExprNoBF
173 %type <node> ConditionalExpr ConditionalExprNoIn ConditionalExprNoBF
174 %type <node> AssignmentExpr AssignmentExprNoIn AssignmentExprNoBF
175 %type <node> Expr ExprNoIn ExprNoBF
177 %type <node> ExprOpt ExprNoInOpt
179 %type <stat> Statement Block
180 %type <stat> VariableStatement ConstStatement EmptyStatement ExprStatement
181 %type <stat> IfStatement IterationStatement ContinueStatement
182 %type <stat> BreakStatement ReturnStatement WithStatement
183 %type <stat> SwitchStatement LabelledStatement
184 %type <stat> ThrowStatement TryStatement
185 %type <stat> DebuggerStatement
186 %type <stat> SourceElement
188 %type <init> Initializer InitializerNoIn
189 %type <func> FunctionDeclaration
190 %type <funcExpr> FunctionExpr
191 %type <body> FunctionBody
192 %type <srcs> SourceElements
193 %type <param> FormalParameterList
194 %type <op> AssignmentOperator
195 %type <args> Arguments
196 %type <alist> ArgumentList
197 %type <vlist> VariableDeclarationList VariableDeclarationListNoIn ConstDeclarationList
198 %type <decl> VariableDeclaration VariableDeclarationNoIn ConstDeclaration
199 %type <cblk> CaseBlock
200 %type <ccl> CaseClause DefaultClause
201 %type <clist> CaseClauses CaseClausesOpt
202 %type <ival> Elision ElisionOpt
203 %type <elm> ElementList
204 %type <pnode> Property
205 %type <plist> PropertyList
209 NULLTOKEN { $$ = new NullNode(); }
210 | TRUETOKEN { $$ = new BooleanNode(true); }
211 | FALSETOKEN { $$ = new BooleanNode(false); }
212 | NUMBER { $$ = makeNumberNode($1); }
213 | STRING { $$ = new StringNode($1); }
215 Lexer *l = Lexer::curr();
216 if (!l->scanRegExp()) YYABORT;
217 $$ = new RegExpNode(l->pattern, l->flags);
219 | DIVEQUAL /* regexp with /= */ {
220 Lexer *l = Lexer::curr();
221 if (!l->scanRegExp()) YYABORT;
222 $$ = new RegExpNode("=" + l->pattern, l->flags);
227 IDENT ':' AssignmentExpr { $$ = new PropertyNode(*$1, $3, PropertyNode::Constant); }
228 | STRING ':' AssignmentExpr { $$ = new PropertyNode(Identifier(*$1), $3, PropertyNode::Constant); }
229 | NUMBER ':' AssignmentExpr { $$ = new PropertyNode(Identifier(UString::from($1)), $3, PropertyNode::Constant); }
230 | IDENT IDENT '(' ')' FunctionBody { $$ = makeGetterOrSetterPropertyNode(*$1, *$2, 0, $5); if (!$$) YYABORT; }
231 | IDENT IDENT '(' FormalParameterList ')' FunctionBody
232 { $$ = makeGetterOrSetterPropertyNode(*$1, *$2, $4.head, $6); if (!$$) YYABORT; }
236 Property { $$.head = new PropertyListNode($1);
238 | PropertyList ',' Property { $$.head = $1.head;
239 $$.tail = new PropertyListNode($3, $1.tail); }
244 | '{' '}' { $$ = new ObjectLiteralNode(); }
245 | '{' PropertyList '}' { $$ = new ObjectLiteralNode($2.head); }
246 /* allow extra comma, see http://bugs.webkit.org/show_bug.cgi?id=5939 */
247 | '{' PropertyList ',' '}' { $$ = new ObjectLiteralNode($2.head); }
251 THISTOKEN { $$ = new ThisNode(); }
254 | IDENT { $$ = new ResolveNode(*$1); }
255 | '(' Expr ')' { $$ = $2; }
259 '[' ElisionOpt ']' { $$ = new ArrayNode($2); }
260 | '[' ElementList ']' { $$ = new ArrayNode($2.head); }
261 | '[' ElementList ',' ElisionOpt ']' { $$ = new ArrayNode($4, $2.head); }
265 ElisionOpt AssignmentExpr { $$.head = new ElementNode($1, $2);
267 | ElementList ',' ElisionOpt AssignmentExpr
269 $$.tail = new ElementNode($1.tail, $3, $4); }
273 /* nothing */ { $$ = 0; }
279 | Elision ',' { $$ = $1 + 1; }
284 | FunctionExpr { $$ = $1; }
285 | MemberExpr '[' Expr ']' { $$ = new BracketAccessorNode($1, $3); }
286 | MemberExpr '.' IDENT { $$ = new DotAccessorNode($1, *$3); }
287 | NEW MemberExpr Arguments { $$ = new NewExprNode($2, $3); }
292 | MemberExprNoBF '[' Expr ']' { $$ = new BracketAccessorNode($1, $3); }
293 | MemberExprNoBF '.' IDENT { $$ = new DotAccessorNode($1, *$3); }
294 | NEW MemberExpr Arguments { $$ = new NewExprNode($2, $3); }
299 | NEW NewExpr { $$ = new NewExprNode($2); }
304 | NEW NewExpr { $$ = new NewExprNode($2); }
308 MemberExpr Arguments { $$ = makeFunctionCallNode($1, $2); }
309 | CallExpr Arguments { $$ = makeFunctionCallNode($1, $2); }
310 | CallExpr '[' Expr ']' { $$ = new BracketAccessorNode($1, $3); }
311 | CallExpr '.' IDENT { $$ = new DotAccessorNode($1, *$3); }
315 MemberExprNoBF Arguments { $$ = makeFunctionCallNode($1, $2); }
316 | CallExprNoBF Arguments { $$ = makeFunctionCallNode($1, $2); }
317 | CallExprNoBF '[' Expr ']' { $$ = new BracketAccessorNode($1, $3); }
318 | CallExprNoBF '.' IDENT { $$ = new DotAccessorNode($1, *$3); }
322 '(' ')' { $$ = new ArgumentsNode(); }
323 | '(' ArgumentList ')' { $$ = new ArgumentsNode($2.head); }
327 AssignmentExpr { $$.head = new ArgumentListNode($1);
329 | ArgumentList ',' AssignmentExpr { $$.head = $1.head;
330 $$.tail = new ArgumentListNode($1.tail, $3); }
338 LeftHandSideExprNoBF:
345 | LeftHandSideExpr PLUSPLUS { $$ = makePostfixNode($1, OpPlusPlus); }
346 | LeftHandSideExpr MINUSMINUS { $$ = makePostfixNode($1, OpMinusMinus); }
351 | LeftHandSideExprNoBF PLUSPLUS { $$ = makePostfixNode($1, OpPlusPlus); }
352 | LeftHandSideExprNoBF MINUSMINUS { $$ = makePostfixNode($1, OpMinusMinus); }
356 DELETETOKEN UnaryExpr { $$ = makeDeleteNode($2); }
357 | VOIDTOKEN UnaryExpr { $$ = new VoidNode($2); }
358 | TYPEOF UnaryExpr { $$ = makeTypeOfNode($2); }
359 | PLUSPLUS UnaryExpr { $$ = makePrefixNode($2, OpPlusPlus); }
360 | AUTOPLUSPLUS UnaryExpr { $$ = makePrefixNode($2, OpPlusPlus); }
361 | MINUSMINUS UnaryExpr { $$ = makePrefixNode($2, OpMinusMinus); }
362 | AUTOMINUSMINUS UnaryExpr { $$ = makePrefixNode($2, OpMinusMinus); }
363 | '+' UnaryExpr { $$ = new UnaryPlusNode($2); }
364 | '-' UnaryExpr { $$ = makeNegateNode($2); }
365 | '~' UnaryExpr { $$ = new BitwiseNotNode($2); }
366 | '!' UnaryExpr { $$ = new LogicalNotNode($2); }
380 | MultiplicativeExpr '*' UnaryExpr { $$ = new MultNode($1, $3); }
381 | MultiplicativeExpr '/' UnaryExpr { $$ = new DivNode($1, $3); }
382 | MultiplicativeExpr '%' UnaryExpr { $$ = new ModNode($1, $3); }
385 MultiplicativeExprNoBF:
387 | MultiplicativeExprNoBF '*' UnaryExpr
388 { $$ = new MultNode($1, $3); }
389 | MultiplicativeExprNoBF '/' UnaryExpr
390 { $$ = new DivNode($1, $3); }
391 | MultiplicativeExprNoBF '%' UnaryExpr
392 { $$ = new ModNode($1, $3); }
397 | AdditiveExpr '+' MultiplicativeExpr { $$ = new AddNode($1, $3); }
398 | AdditiveExpr '-' MultiplicativeExpr { $$ = new SubNode($1, $3); }
402 MultiplicativeExprNoBF
403 | AdditiveExprNoBF '+' MultiplicativeExpr
404 { $$ = new AddNode($1, $3); }
405 | AdditiveExprNoBF '-' MultiplicativeExpr
406 { $$ = new SubNode($1, $3); }
411 | ShiftExpr LSHIFT AdditiveExpr { $$ = new LeftShiftNode($1, $3); }
412 | ShiftExpr RSHIFT AdditiveExpr { $$ = new RightShiftNode($1, $3); }
413 | ShiftExpr URSHIFT AdditiveExpr { $$ = new UnsignedRightShiftNode($1, $3); }
418 | ShiftExprNoBF LSHIFT AdditiveExpr { $$ = new LeftShiftNode($1, $3); }
419 | ShiftExprNoBF RSHIFT AdditiveExpr { $$ = new RightShiftNode($1, $3); }
420 | ShiftExprNoBF URSHIFT AdditiveExpr { $$ = new UnsignedRightShiftNode($1, $3); }
425 | RelationalExpr '<' ShiftExpr { $$ = new LessNode($1, $3); }
426 | RelationalExpr '>' ShiftExpr { $$ = new GreaterNode($1, $3); }
427 | RelationalExpr LE ShiftExpr { $$ = new LessEqNode($1, $3); }
428 | RelationalExpr GE ShiftExpr { $$ = new GreaterEqNode($1, $3); }
429 | RelationalExpr INSTANCEOF ShiftExpr { $$ = new InstanceOfNode($1, $3); }
430 | RelationalExpr INTOKEN ShiftExpr { $$ = new InNode($1, $3); }
435 | RelationalExprNoIn '<' ShiftExpr { $$ = new LessNode($1, $3); }
436 | RelationalExprNoIn '>' ShiftExpr { $$ = new GreaterNode($1, $3); }
437 | RelationalExprNoIn LE ShiftExpr { $$ = new LessEqNode($1, $3); }
438 | RelationalExprNoIn GE ShiftExpr { $$ = new GreaterEqNode($1, $3); }
439 | RelationalExprNoIn INSTANCEOF ShiftExpr
440 { $$ = new InstanceOfNode($1, $3); }
445 | RelationalExprNoBF '<' ShiftExpr { $$ = new LessNode($1, $3); }
446 | RelationalExprNoBF '>' ShiftExpr { $$ = new GreaterNode($1, $3); }
447 | RelationalExprNoBF LE ShiftExpr { $$ = new LessEqNode($1, $3); }
448 | RelationalExprNoBF GE ShiftExpr { $$ = new GreaterEqNode($1, $3); }
449 | RelationalExprNoBF INSTANCEOF ShiftExpr
450 { $$ = new InstanceOfNode($1, $3); }
451 | RelationalExprNoBF INTOKEN ShiftExpr { $$ = new InNode($1, $3); }
456 | EqualityExpr EQEQ RelationalExpr { $$ = new EqualNode($1, $3); }
457 | EqualityExpr NE RelationalExpr { $$ = new NotEqualNode($1, $3); }
458 | EqualityExpr STREQ RelationalExpr { $$ = new StrictEqualNode($1, $3); }
459 | EqualityExpr STRNEQ RelationalExpr { $$ = new NotStrictEqualNode($1, $3); }
464 | EqualityExprNoIn EQEQ RelationalExprNoIn
465 { $$ = new EqualNode($1, $3); }
466 | EqualityExprNoIn NE RelationalExprNoIn
467 { $$ = new NotEqualNode($1, $3); }
468 | EqualityExprNoIn STREQ RelationalExprNoIn
469 { $$ = new StrictEqualNode($1, $3); }
470 | EqualityExprNoIn STRNEQ RelationalExprNoIn
471 { $$ = new NotStrictEqualNode($1, $3); }
476 | EqualityExprNoBF EQEQ RelationalExpr
477 { $$ = new EqualNode($1, $3); }
478 | EqualityExprNoBF NE RelationalExpr { $$ = new NotEqualNode($1, $3); }
479 | EqualityExprNoBF STREQ RelationalExpr
480 { $$ = new StrictEqualNode($1, $3); }
481 | EqualityExprNoBF STRNEQ RelationalExpr
482 { $$ = new NotStrictEqualNode($1, $3); }
487 | BitwiseANDExpr '&' EqualityExpr { $$ = new BitAndNode($1, $3); }
492 | BitwiseANDExprNoIn '&' EqualityExprNoIn
493 { $$ = new BitAndNode($1, $3); }
498 | BitwiseANDExprNoBF '&' EqualityExpr { $$ = new BitAndNode($1, $3); }
503 | BitwiseXORExpr '^' BitwiseANDExpr { $$ = new BitXOrNode($1, $3); }
508 | BitwiseXORExprNoIn '^' BitwiseANDExprNoIn
509 { $$ = new BitXOrNode($1, $3); }
514 | BitwiseXORExprNoBF '^' BitwiseANDExpr
515 { $$ = new BitXOrNode($1, $3); }
520 | BitwiseORExpr '|' BitwiseXORExpr { $$ = new BitOrNode($1, $3); }
525 | BitwiseORExprNoIn '|' BitwiseXORExprNoIn
526 { $$ = new BitOrNode($1, $3); }
531 | BitwiseORExprNoBF '|' BitwiseXORExpr
532 { $$ = new BitOrNode($1, $3); }
537 | LogicalANDExpr AND BitwiseORExpr { $$ = new LogicalAndNode($1, $3); }
542 | LogicalANDExprNoIn AND BitwiseORExprNoIn
543 { $$ = new LogicalAndNode($1, $3); }
548 | LogicalANDExprNoBF AND BitwiseORExpr
549 { $$ = new LogicalAndNode($1, $3); }
554 | LogicalORExpr OR LogicalANDExpr { $$ = new LogicalOrNode($1, $3); }
559 | LogicalORExprNoIn OR LogicalANDExprNoIn
560 { $$ = new LogicalOrNode($1, $3); }
565 | LogicalORExprNoBF OR LogicalANDExpr { $$ = new LogicalOrNode($1, $3); }
570 | LogicalORExpr '?' AssignmentExpr ':' AssignmentExpr
571 { $$ = new ConditionalNode($1, $3, $5); }
576 | LogicalORExprNoIn '?' AssignmentExprNoIn ':' AssignmentExprNoIn
577 { $$ = new ConditionalNode($1, $3, $5); }
582 | LogicalORExprNoBF '?' AssignmentExpr ':' AssignmentExpr
583 { $$ = new ConditionalNode($1, $3, $5); }
588 | LeftHandSideExpr AssignmentOperator AssignmentExpr
589 { $$ = makeAssignNode($1, $2, $3); }
594 | LeftHandSideExpr AssignmentOperator AssignmentExprNoIn
595 { $$ = makeAssignNode($1, $2, $3); }
600 | LeftHandSideExprNoBF AssignmentOperator AssignmentExpr
601 { $$ = makeAssignNode($1, $2, $3); }
605 '=' { $$ = OpEqual; }
606 | PLUSEQUAL { $$ = OpPlusEq; }
607 | MINUSEQUAL { $$ = OpMinusEq; }
608 | MULTEQUAL { $$ = OpMultEq; }
609 | DIVEQUAL { $$ = OpDivEq; }
610 | LSHIFTEQUAL { $$ = OpLShift; }
611 | RSHIFTEQUAL { $$ = OpRShift; }
612 | URSHIFTEQUAL { $$ = OpURShift; }
613 | ANDEQUAL { $$ = OpAndEq; }
614 | XOREQUAL { $$ = OpXOrEq; }
615 | OREQUAL { $$ = OpOrEq; }
616 | MODEQUAL { $$ = OpModEq; }
621 | Expr ',' AssignmentExpr { $$ = new CommaNode($1, $3); }
626 | ExprNoIn ',' AssignmentExprNoIn { $$ = new CommaNode($1, $3); }
631 | ExprNoBF ',' AssignmentExpr { $$ = new CommaNode($1, $3); }
654 '{' '}' { $$ = new BlockNode(0); DBG($$, @2, @2); }
655 | '{' SourceElements '}' { $$ = new BlockNode($2); DBG($$, @3, @3); }
659 VAR VariableDeclarationList ';' { $$ = new VarStatementNode($2.head); DBG($$, @1, @3); }
660 | VAR VariableDeclarationList error { $$ = new VarStatementNode($2.head); DBG($$, @1, @2); AUTO_SEMICOLON; }
663 VariableDeclarationList:
664 VariableDeclaration { $$.head = $1;
666 | VariableDeclarationList ',' VariableDeclaration
672 VariableDeclarationListNoIn:
673 VariableDeclarationNoIn { $$.head = $1;
675 | VariableDeclarationListNoIn ',' VariableDeclarationNoIn
682 IDENT { $$ = new VarDeclNode(*$1, 0, VarDeclNode::Variable); }
683 | IDENT Initializer { $$ = new VarDeclNode(*$1, $2, VarDeclNode::Variable); }
686 VariableDeclarationNoIn:
687 IDENT { $$ = new VarDeclNode(*$1, 0, VarDeclNode::Variable); }
688 | IDENT InitializerNoIn { $$ = new VarDeclNode(*$1, $2, VarDeclNode::Variable); }
692 CONSTTOKEN ConstDeclarationList ';' { $$ = new VarStatementNode($2.head); DBG($$, @1, @3); }
693 | CONSTTOKEN ConstDeclarationList error
694 { $$ = new VarStatementNode($2.head); DBG($$, @1, @2); AUTO_SEMICOLON; }
697 ConstDeclarationList:
698 ConstDeclaration { $$.head = $1;
700 | ConstDeclarationList ',' ConstDeclaration
707 IDENT { $$ = new VarDeclNode(*$1, 0, VarDeclNode::Constant); }
708 | IDENT Initializer { $$ = new VarDeclNode(*$1, $2, VarDeclNode::Constant); }
712 '=' AssignmentExpr { $$ = new AssignExprNode($2); }
716 '=' AssignmentExprNoIn { $$ = new AssignExprNode($2); }
720 ';' { $$ = new EmptyStatementNode(); }
724 ExprNoBF ';' { $$ = new ExprStatementNode($1); DBG($$, @1, @2); }
725 | ExprNoBF error { $$ = new ExprStatementNode($1); DBG($$, @1, @1); AUTO_SEMICOLON; }
729 IF '(' Expr ')' Statement %prec IF_WITHOUT_ELSE
730 { $$ = new IfNode($3, $5, 0); DBG($$, @1, @4); }
731 | IF '(' Expr ')' Statement ELSE Statement
732 { $$ = new IfNode($3, $5, $7); DBG($$, @1, @4); }
736 DO Statement WHILE '(' Expr ')' ';' { $$ = new DoWhileNode($2, $5); DBG($$, @1, @3); }
737 | DO Statement WHILE '(' Expr ')' error { $$ = new DoWhileNode($2, $5); DBG($$, @1, @3); } // Always performs automatic semicolon insertion.
738 | WHILE '(' Expr ')' Statement { $$ = new WhileNode($3, $5); DBG($$, @1, @4); }
739 | FOR '(' ExprNoInOpt ';' ExprOpt ';' ExprOpt ')' Statement
740 { $$ = new ForNode($3, $5, $7, $9); DBG($$, @1, @8); }
741 | FOR '(' VAR VariableDeclarationListNoIn ';' ExprOpt ';' ExprOpt ')' Statement
742 { $$ = new ForNode($4.head, $6, $8, $10); DBG($$, @1, @9); }
743 | FOR '(' LeftHandSideExpr INTOKEN Expr ')' Statement
746 if (!n->isLocation())
748 $$ = new ForInNode(n, $5, $7);
751 | FOR '(' VAR IDENT INTOKEN Expr ')' Statement
752 { $$ = new ForInNode(*$4, 0, $6, $8); DBG($$, @1, @7); }
753 | FOR '(' VAR IDENT InitializerNoIn INTOKEN Expr ')' Statement
754 { $$ = new ForInNode(*$4, $5, $7, $9); DBG($$, @1, @8); }
758 /* nothing */ { $$ = 0; }
763 /* nothing */ { $$ = 0; }
768 CONTINUE ';' { $$ = new ContinueNode(); DBG($$, @1, @2); }
769 | CONTINUE error { $$ = new ContinueNode(); DBG($$, @1, @1); AUTO_SEMICOLON; }
770 | CONTINUE IDENT ';' { $$ = new ContinueNode(*$2); DBG($$, @1, @3); }
771 | CONTINUE IDENT error { $$ = new ContinueNode(*$2); DBG($$, @1, @2); AUTO_SEMICOLON; }
775 BREAK ';' { $$ = new BreakNode(); DBG($$, @1, @2); }
776 | BREAK error { $$ = new BreakNode(); DBG($$, @1, @1); AUTO_SEMICOLON; }
777 | BREAK IDENT ';' { $$ = new BreakNode(*$2); DBG($$, @1, @3); }
778 | BREAK IDENT error { $$ = new BreakNode(*$2); DBG($$, @1, @2); AUTO_SEMICOLON; }
782 RETURN ';' { $$ = new ReturnNode(0); DBG($$, @1, @2); }
783 | RETURN error { $$ = new ReturnNode(0); DBG($$, @1, @1); AUTO_SEMICOLON; }
784 | RETURN Expr ';' { $$ = new ReturnNode($2); DBG($$, @1, @3); }
785 | RETURN Expr error { $$ = new ReturnNode($2); DBG($$, @1, @2); AUTO_SEMICOLON; }
789 WITH '(' Expr ')' Statement { $$ = new WithNode($3, $5); DBG($$, @1, @4); }
793 SWITCH '(' Expr ')' CaseBlock { $$ = new SwitchNode($3, $5); DBG($$, @1, @4); }
797 '{' CaseClausesOpt '}' { $$ = new CaseBlockNode($2.head, 0, 0); }
798 | '{' CaseClausesOpt DefaultClause CaseClausesOpt '}'
799 { $$ = new CaseBlockNode($2.head, $3, $4.head); }
803 /* nothing */ { $$.head = 0; $$.tail = 0; }
808 CaseClause { $$.head = new ClauseListNode($1);
810 | CaseClauses CaseClause { $$.head = $1.head;
811 $$.tail = new ClauseListNode($1.tail, $2); }
815 CASE Expr ':' { $$ = new CaseClauseNode($2); }
816 | CASE Expr ':' SourceElements { $$ = new CaseClauseNode($2, $4); }
820 DEFAULT ':' { $$ = new CaseClauseNode(0); }
821 | DEFAULT ':' SourceElements { $$ = new CaseClauseNode(0, $3); }
825 IDENT ':' Statement { $3->pushLabel(*$1); $$ = new LabelNode(*$1, $3); }
829 THROW Expr ';' { $$ = new ThrowNode($2); DBG($$, @1, @3); }
830 | THROW Expr error { $$ = new ThrowNode($2); DBG($$, @1, @2); AUTO_SEMICOLON; }
834 TRY Block FINALLY Block { $$ = new TryNode($2, CommonIdentifiers::shared()->nullIdentifier, 0, $4); DBG($$, @1, @2); }
835 | TRY Block CATCH '(' IDENT ')' Block { $$ = new TryNode($2, *$5, $7, 0); DBG($$, @1, @2); }
836 | TRY Block CATCH '(' IDENT ')' Block FINALLY Block
837 { $$ = new TryNode($2, *$5, $7, $9); DBG($$, @1, @2); }
841 DEBUGGER ';' { $$ = new EmptyStatementNode(); DBG($$, @1, @2); }
842 | DEBUGGER error { $$ = new EmptyStatementNode(); DBG($$, @1, @1); AUTO_SEMICOLON; }
846 FUNCTION IDENT '(' ')' FunctionBody { $$ = new FuncDeclNode(*$2, $5); }
847 | FUNCTION IDENT '(' FormalParameterList ')' FunctionBody
848 { $$ = new FuncDeclNode(*$2, $4.head, $6); }
852 FUNCTION '(' ')' FunctionBody { $$ = new FuncExprNode(CommonIdentifiers::shared()->nullIdentifier, $4); }
853 | FUNCTION '(' FormalParameterList ')' FunctionBody
854 { $$ = new FuncExprNode(CommonIdentifiers::shared()->nullIdentifier, $5, $3.head); }
855 | FUNCTION IDENT '(' ')' FunctionBody { $$ = new FuncExprNode(*$2, $5); }
856 | FUNCTION IDENT '(' FormalParameterList ')' FunctionBody
857 { $$ = new FuncExprNode(*$2, $6, $4.head); }
861 IDENT { $$.head = new ParameterNode(*$1);
863 | FormalParameterList ',' IDENT { $$.head = $1.head;
864 $$.tail = new ParameterNode($1.tail, *$3); }
868 '{' '}' /* not in spec */ { $$ = new FunctionBodyNode(0); DBG($$, @1, @2); }
869 | '{' SourceElements '}' { $$ = new FunctionBodyNode($2); DBG($$, @1, @3); }
873 /* not in spec */ { Parser::accept(new ProgramNode(0)); }
874 | SourceElements { Parser::accept(new ProgramNode($1)); }
878 SourceElement { $$ = new Vector<RefPtr<StatementNode> >();
880 | SourceElements SourceElement { $$->append($2); }
884 FunctionDeclaration { $$ = $1; }
885 | Statement { $$ = $1; }
890 static Node* makeAssignNode(Node* loc, Operator op, Node* expr)
892 if (!loc->isLocation())
893 return new AssignErrorNode(loc, op, expr);
895 if (loc->isResolveNode()) {
896 ResolveNode* resolve = static_cast<ResolveNode*>(loc);
898 return new AssignResolveNode(resolve->identifier(), expr);
900 return new ReadModifyResolveNode(resolve->identifier(), op, expr);
902 if (loc->isBracketAccessorNode()) {
903 BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(loc);
905 return new AssignBracketNode(bracket->base(), bracket->subscript(), expr);
907 return new ReadModifyBracketNode(bracket->base(), bracket->subscript(), op, expr);
909 ASSERT(loc->isDotAccessorNode());
910 DotAccessorNode* dot = static_cast<DotAccessorNode*>(loc);
912 return new AssignDotNode(dot->base(), dot->identifier(), expr);
913 return new ReadModifyDotNode(dot->base(), dot->identifier(), op, expr);
916 static Node* makePrefixNode(Node* expr, Operator op)
918 if (!expr->isLocation())
919 return new PrefixErrorNode(expr, op);
921 if (expr->isResolveNode()) {
922 ResolveNode* resolve = static_cast<ResolveNode*>(expr);
923 if (op == OpPlusPlus)
924 return new PreIncResolveNode(resolve->identifier());
926 return new PreDecResolveNode(resolve->identifier());
928 if (expr->isBracketAccessorNode()) {
929 BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(expr);
930 if (op == OpPlusPlus)
931 return new PreIncBracketNode(bracket->base(), bracket->subscript());
933 return new PreDecBracketNode(bracket->base(), bracket->subscript());
935 ASSERT(expr->isDotAccessorNode());
936 DotAccessorNode* dot = static_cast<DotAccessorNode*>(expr);
937 if (op == OpPlusPlus)
938 return new PreIncDotNode(dot->base(), dot->identifier());
939 return new PreDecDotNode(dot->base(), dot->identifier());
942 static Node* makePostfixNode(Node* expr, Operator op)
944 if (!expr->isLocation())
945 return new PostfixErrorNode(expr, op);
947 if (expr->isResolveNode()) {
948 ResolveNode* resolve = static_cast<ResolveNode*>(expr);
949 if (op == OpPlusPlus)
950 return new PostIncResolveNode(resolve->identifier());
952 return new PostDecResolveNode(resolve->identifier());
954 if (expr->isBracketAccessorNode()) {
955 BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(expr);
956 if (op == OpPlusPlus)
957 return new PostIncBracketNode(bracket->base(), bracket->subscript());
959 return new PostDecBracketNode(bracket->base(), bracket->subscript());
961 ASSERT(expr->isDotAccessorNode());
962 DotAccessorNode* dot = static_cast<DotAccessorNode*>(expr);
964 if (op == OpPlusPlus)
965 return new PostIncDotNode(dot->base(), dot->identifier());
966 return new PostDecDotNode(dot->base(), dot->identifier());
969 static Node* makeFunctionCallNode(Node* func, ArgumentsNode* args)
971 if (!func->isLocation())
972 return new FunctionCallValueNode(func, args);
973 if (func->isResolveNode()) {
974 ResolveNode* resolve = static_cast<ResolveNode*>(func);
975 return new FunctionCallResolveNode(resolve->identifier(), args);
977 if (func->isBracketAccessorNode()) {
978 BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(func);
979 return new FunctionCallBracketNode(bracket->base(), bracket->subscript(), args);
981 ASSERT(func->isDotAccessorNode());
982 DotAccessorNode* dot = static_cast<DotAccessorNode*>(func);
983 return new FunctionCallDotNode(dot->base(), dot->identifier(), args);
986 static Node* makeTypeOfNode(Node* expr)
988 if (expr->isResolveNode()) {
989 ResolveNode* resolve = static_cast<ResolveNode*>(expr);
990 return new TypeOfResolveNode(resolve->identifier());
992 return new TypeOfValueNode(expr);
995 static Node* makeDeleteNode(Node* expr)
997 if (!expr->isLocation())
998 return new DeleteValueNode(expr);
999 if (expr->isResolveNode()) {
1000 ResolveNode* resolve = static_cast<ResolveNode*>(expr);
1001 return new DeleteResolveNode(resolve->identifier());
1003 if (expr->isBracketAccessorNode()) {
1004 BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(expr);
1005 return new DeleteBracketNode(bracket->base(), bracket->subscript());
1007 ASSERT(expr->isDotAccessorNode());
1008 DotAccessorNode* dot = static_cast<DotAccessorNode*>(expr);
1009 return new DeleteDotNode(dot->base(), dot->identifier());
1012 static PropertyNode* makeGetterOrSetterPropertyNode(const Identifier& getOrSet, const Identifier& name, ParameterNode* params, FunctionBodyNode* body)
1014 PropertyNode::Type type;
1015 if (getOrSet == "get")
1016 type = PropertyNode::Getter;
1017 else if (getOrSet == "set")
1018 type = PropertyNode::Setter;
1021 return new PropertyNode(name, new FuncExprNode(CommonIdentifiers::shared()->nullIdentifier, body, params), type);
1024 static Node* makeNegateNode(Node* n)
1026 if (n->isNumber()) {
1027 NumberNode* number = static_cast<NumberNode*>(n);
1029 if (number->value() > 0.0) {
1030 number->setValue(-number->value());
1035 return new NegateNode(n);
1038 static Node* makeNumberNode(double d)
1040 JSValue* value = JSImmediate::fromDouble(d);
1042 return new ImmediateNumberNode(value, d);
1043 return new NumberNode(d);
1046 /* called by yyparse on error */
1047 int yyerror(const char *)
1052 /* may we automatically insert a semicolon ? */
1053 static bool allowAutomaticSemicolon()
1055 return yychar == '}' || yychar == 0 || Lexer::curr()->prevTerminator();