JavaScriptCore-466.1.tar.gz
[apple/javascriptcore.git] / kjs / grammar.y
1 %{
2
3 /*
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>
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
24 #include "config.h"
25
26 #include <string.h>
27 #include <stdlib.h>
28 #include "value.h"
29 #include "object.h"
30 #include "types.h"
31 #include "nodes.h"
32 #include "lexer.h"
33 #include "internal.h"
34 #include "CommonIdentifiers.h"
35 #include "NodeInfo.h"
36 #include "Parser.h"
37 #include <wtf/MathExtras.h>
38
39 // Not sure why, but yacc doesn't add this define along with the others.
40 #define yylloc kjsyylloc
41
42 #define YYMAXDEPTH 10000
43 #define YYENABLE_NLS 0
44
45 /* default values for bison */
46 #define YYDEBUG 0 // Set to 1 to debug a parse error.
47 #define kjsyydebug 0 // Set to 1 to debug a parse error.
48 #if !PLATFORM(DARWIN)
49 // avoid triggering warnings in older bison
50 #define YYERROR_VERBOSE
51 #endif
52
53 extern int kjsyylex();
54 int kjsyyerror(const char *);
55 static bool allowAutomaticSemicolon();
56
57 #define AUTO_SEMICOLON do { if (!allowAutomaticSemicolon()) YYABORT; } while (0)
58 #define DBG(l, s, e) (l)->setLoc((s).first_line, (e).last_line)
59
60 using namespace KJS;
61 using namespace std;
62
63 static AddNode* makeAddNode(ExpressionNode*, ExpressionNode*);
64 static LessNode* makeLessNode(ExpressionNode*, ExpressionNode*);
65 static ExpressionNode* makeAssignNode(ExpressionNode* loc, Operator, ExpressionNode* expr);
66 static ExpressionNode* makePrefixNode(ExpressionNode* expr, Operator);
67 static ExpressionNode* makePostfixNode(ExpressionNode* expr, Operator);
68 static PropertyNode* makeGetterOrSetterPropertyNode(const Identifier &getOrSet, const Identifier& name, ParameterNode*, FunctionBodyNode*);
69 static ExpressionNode* makeFunctionCallNode(ExpressionNode* func, ArgumentsNode*);
70 static ExpressionNode* makeTypeOfNode(ExpressionNode*);
71 static ExpressionNode* makeDeleteNode(ExpressionNode*);
72 static ExpressionNode* makeNegateNode(ExpressionNode*);
73 static NumberNode* makeNumberNode(double);
74 static StatementNode* makeVarStatementNode(ExpressionNode*);
75 static ExpressionNode* combineVarInitializers(ExpressionNode* list, AssignResolveNode* init);
76
77
78 #if COMPILER(MSVC)
79
80 #pragma warning(disable: 4065)
81 #pragma warning(disable: 4244)
82 #pragma warning(disable: 4702)
83
84 // At least some of the time, the declarations of malloc and free that bison
85 // generates are causing warnings. A way to avoid this is to explicitly define
86 // the macros so that bison doesn't try to declare malloc and free.
87 #define YYMALLOC malloc
88 #define YYFREE free
89
90 #endif
91
92 template <typename T> NodeInfo<T> createNodeInfo(T node, ParserRefCountedData<DeclarationStacks::VarStack>* varDecls,
93 ParserRefCountedData<DeclarationStacks::FunctionStack>* funcDecls)
94 {
95 NodeInfo<T> result = {node, varDecls, funcDecls};
96 return result;
97 }
98
99 template <typename T> T mergeDeclarationLists(T decls1, T decls2)
100 {
101 // decls1 or both are null
102 if (!decls1)
103 return decls2;
104 // only decls1 is non-null
105 if (!decls2)
106 return decls1;
107
108 // Both are non-null
109 decls1->data.append(decls2->data);
110
111 // We manually release the declaration lists to avoid accumulating many many
112 // unused heap allocated vectors
113 decls2->ref();
114 decls2->deref();
115 return decls1;
116 }
117
118 static void appendToVarDeclarationList(ParserRefCountedData<DeclarationStacks::VarStack>*& varDecls, const Identifier& ident, unsigned attrs)
119 {
120 if (!varDecls)
121 varDecls = new ParserRefCountedData<DeclarationStacks::VarStack>;
122
123 varDecls->data.append(make_pair(ident, attrs));
124
125 }
126
127 static inline void appendToVarDeclarationList(ParserRefCountedData<DeclarationStacks::VarStack>*& varDecls, ConstDeclNode* decl)
128 {
129 unsigned attrs = DeclarationStacks::IsConstant;
130 if (decl->m_init)
131 attrs |= DeclarationStacks::HasInitializer;
132 appendToVarDeclarationList(varDecls, decl->m_ident, attrs);
133 }
134
135 %}
136
137 %union {
138 int intValue;
139 double doubleValue;
140 UString* string;
141 Identifier* ident;
142
143 // expression subtrees
144 ExpressionNode* expressionNode;
145 FuncDeclNode* funcDeclNode;
146 PropertyNode* propertyNode;
147 ArgumentsNode* argumentsNode;
148 ConstDeclNode* constDeclNode;
149 CaseBlockNodeInfo caseBlockNode;
150 CaseClauseNodeInfo caseClauseNode;
151 FuncExprNode* funcExprNode;
152
153 // statement nodes
154 StatementNodeInfo statementNode;
155 FunctionBodyNode* functionBodyNode;
156 ProgramNode* programNode;
157
158 SourceElementsInfo sourceElements;
159 PropertyList propertyList;
160 ArgumentList argumentList;
161 VarDeclListInfo varDeclList;
162 ConstDeclListInfo constDeclList;
163 ClauseListInfo clauseList;
164 ElementList elementList;
165 ParameterList parameterList;
166
167 Operator op;
168 }
169
170 %start Program
171
172 /* literals */
173 %token NULLTOKEN TRUETOKEN FALSETOKEN
174
175 /* keywords */
176 %token BREAK CASE DEFAULT FOR NEW VAR CONSTTOKEN CONTINUE
177 %token FUNCTION RETURN VOIDTOKEN DELETETOKEN
178 %token IF THISTOKEN DO WHILE INTOKEN INSTANCEOF TYPEOF
179 %token SWITCH WITH RESERVED
180 %token THROW TRY CATCH FINALLY
181 %token DEBUGGER
182
183 /* give an if without an else higher precedence than an else to resolve the ambiguity */
184 %nonassoc IF_WITHOUT_ELSE
185 %nonassoc ELSE
186
187 /* punctuators */
188 %token EQEQ NE /* == and != */
189 %token STREQ STRNEQ /* === and !== */
190 %token LE GE /* < and > */
191 %token OR AND /* || and && */
192 %token PLUSPLUS MINUSMINUS /* ++ and -- */
193 %token LSHIFT /* << */
194 %token RSHIFT URSHIFT /* >> and >>> */
195 %token PLUSEQUAL MINUSEQUAL /* += and -= */
196 %token MULTEQUAL DIVEQUAL /* *= and /= */
197 %token LSHIFTEQUAL /* <<= */
198 %token RSHIFTEQUAL URSHIFTEQUAL /* >>= and >>>= */
199 %token ANDEQUAL MODEQUAL /* &= and %= */
200 %token XOREQUAL OREQUAL /* ^= and |= */
201
202 /* terminal types */
203 %token <doubleValue> NUMBER
204 %token <string> STRING
205 %token <ident> IDENT
206
207 /* automatically inserted semicolon */
208 %token AUTOPLUSPLUS AUTOMINUSMINUS
209
210 /* non-terminal types */
211 %type <expressionNode> Literal ArrayLiteral
212
213 %type <expressionNode> PrimaryExpr PrimaryExprNoBrace
214 %type <expressionNode> MemberExpr MemberExprNoBF /* BF => brace or function */
215 %type <expressionNode> NewExpr NewExprNoBF
216 %type <expressionNode> CallExpr CallExprNoBF
217 %type <expressionNode> LeftHandSideExpr LeftHandSideExprNoBF
218 %type <expressionNode> PostfixExpr PostfixExprNoBF
219 %type <expressionNode> UnaryExpr UnaryExprNoBF UnaryExprCommon
220 %type <expressionNode> MultiplicativeExpr MultiplicativeExprNoBF
221 %type <expressionNode> AdditiveExpr AdditiveExprNoBF
222 %type <expressionNode> ShiftExpr ShiftExprNoBF
223 %type <expressionNode> RelationalExpr RelationalExprNoIn RelationalExprNoBF
224 %type <expressionNode> EqualityExpr EqualityExprNoIn EqualityExprNoBF
225 %type <expressionNode> BitwiseANDExpr BitwiseANDExprNoIn BitwiseANDExprNoBF
226 %type <expressionNode> BitwiseXORExpr BitwiseXORExprNoIn BitwiseXORExprNoBF
227 %type <expressionNode> BitwiseORExpr BitwiseORExprNoIn BitwiseORExprNoBF
228 %type <expressionNode> LogicalANDExpr LogicalANDExprNoIn LogicalANDExprNoBF
229 %type <expressionNode> LogicalORExpr LogicalORExprNoIn LogicalORExprNoBF
230 %type <expressionNode> ConditionalExpr ConditionalExprNoIn ConditionalExprNoBF
231 %type <expressionNode> AssignmentExpr AssignmentExprNoIn AssignmentExprNoBF
232 %type <expressionNode> Expr ExprNoIn ExprNoBF
233
234 %type <expressionNode> ExprOpt ExprNoInOpt
235
236 %type <statementNode> Statement Block
237 %type <statementNode> VariableStatement ConstStatement EmptyStatement ExprStatement
238 %type <statementNode> IfStatement IterationStatement ContinueStatement
239 %type <statementNode> BreakStatement ReturnStatement WithStatement
240 %type <statementNode> SwitchStatement LabelledStatement
241 %type <statementNode> ThrowStatement TryStatement
242 %type <statementNode> DebuggerStatement
243 %type <statementNode> SourceElement
244
245 %type <expressionNode> Initializer InitializerNoIn
246 %type <funcDeclNode> FunctionDeclaration
247 %type <funcExprNode> FunctionExpr
248 %type <functionBodyNode> FunctionBody
249 %type <sourceElements> SourceElements
250 %type <parameterList> FormalParameterList
251 %type <op> AssignmentOperator
252 %type <argumentsNode> Arguments
253 %type <argumentList> ArgumentList
254 %type <varDeclList> VariableDeclarationList VariableDeclarationListNoIn
255 %type <constDeclList> ConstDeclarationList
256 %type <constDeclNode> ConstDeclaration
257 %type <caseBlockNode> CaseBlock
258 %type <caseClauseNode> CaseClause DefaultClause
259 %type <clauseList> CaseClauses CaseClausesOpt
260 %type <intValue> Elision ElisionOpt
261 %type <elementList> ElementList
262 %type <propertyNode> Property
263 %type <propertyList> PropertyList
264 %%
265
266 Literal:
267 NULLTOKEN { $$ = new NullNode; }
268 | TRUETOKEN { $$ = new TrueNode; }
269 | FALSETOKEN { $$ = new FalseNode; }
270 | NUMBER { $$ = makeNumberNode($1); }
271 | STRING { $$ = new StringNode($1); }
272 | '/' /* regexp */ {
273 Lexer& l = lexer();
274 if (!l.scanRegExp())
275 YYABORT;
276 $$ = new RegExpNode(l.pattern(), l.flags());
277 }
278 | DIVEQUAL /* regexp with /= */ {
279 Lexer& l = lexer();
280 if (!l.scanRegExp())
281 YYABORT;
282 $$ = new RegExpNode("=" + l.pattern(), l.flags());
283 }
284 ;
285
286 Property:
287 IDENT ':' AssignmentExpr { $$ = new PropertyNode(*$1, $3, PropertyNode::Constant); }
288 | STRING ':' AssignmentExpr { $$ = new PropertyNode(Identifier(*$1), $3, PropertyNode::Constant); }
289 | NUMBER ':' AssignmentExpr { $$ = new PropertyNode(Identifier(UString::from($1)), $3, PropertyNode::Constant); }
290 | IDENT IDENT '(' ')' '{' FunctionBody '}' { $$ = makeGetterOrSetterPropertyNode(*$1, *$2, 0, $6); DBG($6, @5, @7); if (!$$) YYABORT; }
291 | IDENT IDENT '(' FormalParameterList ')' '{' FunctionBody '}'
292 { $$ = makeGetterOrSetterPropertyNode(*$1, *$2, $4.head, $7); DBG($7, @6, @8); if (!$$) YYABORT; }
293 ;
294
295 PropertyList:
296 Property { $$.head = new PropertyListNode($1);
297 $$.tail = $$.head; }
298 | PropertyList ',' Property { $$.head = $1.head;
299 $$.tail = new PropertyListNode($3, $1.tail); }
300 ;
301
302 PrimaryExpr:
303 PrimaryExprNoBrace
304 | '{' '}' { $$ = new ObjectLiteralNode(); }
305 | '{' PropertyList '}' { $$ = new ObjectLiteralNode($2.head); }
306 /* allow extra comma, see http://bugs.webkit.org/show_bug.cgi?id=5939 */
307 | '{' PropertyList ',' '}' { $$ = new ObjectLiteralNode($2.head); }
308 ;
309
310 PrimaryExprNoBrace:
311 THISTOKEN { $$ = new ThisNode(); }
312 | Literal
313 | ArrayLiteral
314 | IDENT { $$ = new ResolveNode(*$1); }
315 | '(' Expr ')' { $$ = $2; }
316 ;
317
318 ArrayLiteral:
319 '[' ElisionOpt ']' { $$ = new ArrayNode($2); }
320 | '[' ElementList ']' { $$ = new ArrayNode($2.head); }
321 | '[' ElementList ',' ElisionOpt ']' { $$ = new ArrayNode($4, $2.head); }
322 ;
323
324 ElementList:
325 ElisionOpt AssignmentExpr { $$.head = new ElementNode($1, $2);
326 $$.tail = $$.head; }
327 | ElementList ',' ElisionOpt AssignmentExpr
328 { $$.head = $1.head;
329 $$.tail = new ElementNode($1.tail, $3, $4); }
330 ;
331
332 ElisionOpt:
333 /* nothing */ { $$ = 0; }
334 | Elision
335 ;
336
337 Elision:
338 ',' { $$ = 1; }
339 | Elision ',' { $$ = $1 + 1; }
340 ;
341
342 MemberExpr:
343 PrimaryExpr
344 | FunctionExpr { $$ = $1; }
345 | MemberExpr '[' Expr ']' { $$ = new BracketAccessorNode($1, $3); }
346 | MemberExpr '.' IDENT { $$ = new DotAccessorNode($1, *$3); }
347 | NEW MemberExpr Arguments { $$ = new NewExprNode($2, $3); }
348 ;
349
350 MemberExprNoBF:
351 PrimaryExprNoBrace
352 | MemberExprNoBF '[' Expr ']' { $$ = new BracketAccessorNode($1, $3); }
353 | MemberExprNoBF '.' IDENT { $$ = new DotAccessorNode($1, *$3); }
354 | NEW MemberExpr Arguments { $$ = new NewExprNode($2, $3); }
355 ;
356
357 NewExpr:
358 MemberExpr
359 | NEW NewExpr { $$ = new NewExprNode($2); }
360 ;
361
362 NewExprNoBF:
363 MemberExprNoBF
364 | NEW NewExpr { $$ = new NewExprNode($2); }
365 ;
366
367 CallExpr:
368 MemberExpr Arguments { $$ = makeFunctionCallNode($1, $2); }
369 | CallExpr Arguments { $$ = makeFunctionCallNode($1, $2); }
370 | CallExpr '[' Expr ']' { $$ = new BracketAccessorNode($1, $3); }
371 | CallExpr '.' IDENT { $$ = new DotAccessorNode($1, *$3); }
372 ;
373
374 CallExprNoBF:
375 MemberExprNoBF Arguments { $$ = makeFunctionCallNode($1, $2); }
376 | CallExprNoBF Arguments { $$ = makeFunctionCallNode($1, $2); }
377 | CallExprNoBF '[' Expr ']' { $$ = new BracketAccessorNode($1, $3); }
378 | CallExprNoBF '.' IDENT { $$ = new DotAccessorNode($1, *$3); }
379 ;
380
381 Arguments:
382 '(' ')' { $$ = new ArgumentsNode(); }
383 | '(' ArgumentList ')' { $$ = new ArgumentsNode($2.head); }
384 ;
385
386 ArgumentList:
387 AssignmentExpr { $$.head = new ArgumentListNode($1);
388 $$.tail = $$.head; }
389 | ArgumentList ',' AssignmentExpr { $$.head = $1.head;
390 $$.tail = new ArgumentListNode($1.tail, $3); }
391 ;
392
393 LeftHandSideExpr:
394 NewExpr
395 | CallExpr
396 ;
397
398 LeftHandSideExprNoBF:
399 NewExprNoBF
400 | CallExprNoBF
401 ;
402
403 PostfixExpr:
404 LeftHandSideExpr
405 | LeftHandSideExpr PLUSPLUS { $$ = makePostfixNode($1, OpPlusPlus); }
406 | LeftHandSideExpr MINUSMINUS { $$ = makePostfixNode($1, OpMinusMinus); }
407 ;
408
409 PostfixExprNoBF:
410 LeftHandSideExprNoBF
411 | LeftHandSideExprNoBF PLUSPLUS { $$ = makePostfixNode($1, OpPlusPlus); }
412 | LeftHandSideExprNoBF MINUSMINUS { $$ = makePostfixNode($1, OpMinusMinus); }
413 ;
414
415 UnaryExprCommon:
416 DELETETOKEN UnaryExpr { $$ = makeDeleteNode($2); }
417 | VOIDTOKEN UnaryExpr { $$ = new VoidNode($2); }
418 | TYPEOF UnaryExpr { $$ = makeTypeOfNode($2); }
419 | PLUSPLUS UnaryExpr { $$ = makePrefixNode($2, OpPlusPlus); }
420 | AUTOPLUSPLUS UnaryExpr { $$ = makePrefixNode($2, OpPlusPlus); }
421 | MINUSMINUS UnaryExpr { $$ = makePrefixNode($2, OpMinusMinus); }
422 | AUTOMINUSMINUS UnaryExpr { $$ = makePrefixNode($2, OpMinusMinus); }
423 | '+' UnaryExpr { $$ = new UnaryPlusNode($2); }
424 | '-' UnaryExpr { $$ = makeNegateNode($2); }
425 | '~' UnaryExpr { $$ = new BitwiseNotNode($2); }
426 | '!' UnaryExpr { $$ = new LogicalNotNode($2); }
427
428 UnaryExpr:
429 PostfixExpr
430 | UnaryExprCommon
431 ;
432
433 UnaryExprNoBF:
434 PostfixExprNoBF
435 | UnaryExprCommon
436 ;
437
438 MultiplicativeExpr:
439 UnaryExpr
440 | MultiplicativeExpr '*' UnaryExpr { $$ = new MultNode($1, $3); }
441 | MultiplicativeExpr '/' UnaryExpr { $$ = new DivNode($1, $3); }
442 | MultiplicativeExpr '%' UnaryExpr { $$ = new ModNode($1, $3); }
443 ;
444
445 MultiplicativeExprNoBF:
446 UnaryExprNoBF
447 | MultiplicativeExprNoBF '*' UnaryExpr
448 { $$ = new MultNode($1, $3); }
449 | MultiplicativeExprNoBF '/' UnaryExpr
450 { $$ = new DivNode($1, $3); }
451 | MultiplicativeExprNoBF '%' UnaryExpr
452 { $$ = new ModNode($1, $3); }
453 ;
454
455 AdditiveExpr:
456 MultiplicativeExpr
457 | AdditiveExpr '+' MultiplicativeExpr { $$ = makeAddNode($1, $3); }
458 | AdditiveExpr '-' MultiplicativeExpr { $$ = new SubNode($1, $3); }
459 ;
460
461 AdditiveExprNoBF:
462 MultiplicativeExprNoBF
463 | AdditiveExprNoBF '+' MultiplicativeExpr
464 { $$ = makeAddNode($1, $3); }
465 | AdditiveExprNoBF '-' MultiplicativeExpr
466 { $$ = new SubNode($1, $3); }
467 ;
468
469 ShiftExpr:
470 AdditiveExpr
471 | ShiftExpr LSHIFT AdditiveExpr { $$ = new LeftShiftNode($1, $3); }
472 | ShiftExpr RSHIFT AdditiveExpr { $$ = new RightShiftNode($1, $3); }
473 | ShiftExpr URSHIFT AdditiveExpr { $$ = new UnsignedRightShiftNode($1, $3); }
474 ;
475
476 ShiftExprNoBF:
477 AdditiveExprNoBF
478 | ShiftExprNoBF LSHIFT AdditiveExpr { $$ = new LeftShiftNode($1, $3); }
479 | ShiftExprNoBF RSHIFT AdditiveExpr { $$ = new RightShiftNode($1, $3); }
480 | ShiftExprNoBF URSHIFT AdditiveExpr { $$ = new UnsignedRightShiftNode($1, $3); }
481 ;
482
483 RelationalExpr:
484 ShiftExpr
485 | RelationalExpr '<' ShiftExpr { $$ = makeLessNode($1, $3); }
486 | RelationalExpr '>' ShiftExpr { $$ = new GreaterNode($1, $3); }
487 | RelationalExpr LE ShiftExpr { $$ = new LessEqNode($1, $3); }
488 | RelationalExpr GE ShiftExpr { $$ = new GreaterEqNode($1, $3); }
489 | RelationalExpr INSTANCEOF ShiftExpr { $$ = new InstanceOfNode($1, $3); }
490 | RelationalExpr INTOKEN ShiftExpr { $$ = new InNode($1, $3); }
491 ;
492
493 RelationalExprNoIn:
494 ShiftExpr
495 | RelationalExprNoIn '<' ShiftExpr { $$ = makeLessNode($1, $3); }
496 | RelationalExprNoIn '>' ShiftExpr { $$ = new GreaterNode($1, $3); }
497 | RelationalExprNoIn LE ShiftExpr { $$ = new LessEqNode($1, $3); }
498 | RelationalExprNoIn GE ShiftExpr { $$ = new GreaterEqNode($1, $3); }
499 | RelationalExprNoIn INSTANCEOF ShiftExpr
500 { $$ = new InstanceOfNode($1, $3); }
501 ;
502
503 RelationalExprNoBF:
504 ShiftExprNoBF
505 | RelationalExprNoBF '<' ShiftExpr { $$ = makeLessNode($1, $3); }
506 | RelationalExprNoBF '>' ShiftExpr { $$ = new GreaterNode($1, $3); }
507 | RelationalExprNoBF LE ShiftExpr { $$ = new LessEqNode($1, $3); }
508 | RelationalExprNoBF GE ShiftExpr { $$ = new GreaterEqNode($1, $3); }
509 | RelationalExprNoBF INSTANCEOF ShiftExpr
510 { $$ = new InstanceOfNode($1, $3); }
511 | RelationalExprNoBF INTOKEN ShiftExpr { $$ = new InNode($1, $3); }
512 ;
513
514 EqualityExpr:
515 RelationalExpr
516 | EqualityExpr EQEQ RelationalExpr { $$ = new EqualNode($1, $3); }
517 | EqualityExpr NE RelationalExpr { $$ = new NotEqualNode($1, $3); }
518 | EqualityExpr STREQ RelationalExpr { $$ = new StrictEqualNode($1, $3); }
519 | EqualityExpr STRNEQ RelationalExpr { $$ = new NotStrictEqualNode($1, $3); }
520 ;
521
522 EqualityExprNoIn:
523 RelationalExprNoIn
524 | EqualityExprNoIn EQEQ RelationalExprNoIn
525 { $$ = new EqualNode($1, $3); }
526 | EqualityExprNoIn NE RelationalExprNoIn
527 { $$ = new NotEqualNode($1, $3); }
528 | EqualityExprNoIn STREQ RelationalExprNoIn
529 { $$ = new StrictEqualNode($1, $3); }
530 | EqualityExprNoIn STRNEQ RelationalExprNoIn
531 { $$ = new NotStrictEqualNode($1, $3); }
532 ;
533
534 EqualityExprNoBF:
535 RelationalExprNoBF
536 | EqualityExprNoBF EQEQ RelationalExpr
537 { $$ = new EqualNode($1, $3); }
538 | EqualityExprNoBF NE RelationalExpr { $$ = new NotEqualNode($1, $3); }
539 | EqualityExprNoBF STREQ RelationalExpr
540 { $$ = new StrictEqualNode($1, $3); }
541 | EqualityExprNoBF STRNEQ RelationalExpr
542 { $$ = new NotStrictEqualNode($1, $3); }
543 ;
544
545 BitwiseANDExpr:
546 EqualityExpr
547 | BitwiseANDExpr '&' EqualityExpr { $$ = new BitAndNode($1, $3); }
548 ;
549
550 BitwiseANDExprNoIn:
551 EqualityExprNoIn
552 | BitwiseANDExprNoIn '&' EqualityExprNoIn
553 { $$ = new BitAndNode($1, $3); }
554 ;
555
556 BitwiseANDExprNoBF:
557 EqualityExprNoBF
558 | BitwiseANDExprNoBF '&' EqualityExpr { $$ = new BitAndNode($1, $3); }
559 ;
560
561 BitwiseXORExpr:
562 BitwiseANDExpr
563 | BitwiseXORExpr '^' BitwiseANDExpr { $$ = new BitXOrNode($1, $3); }
564 ;
565
566 BitwiseXORExprNoIn:
567 BitwiseANDExprNoIn
568 | BitwiseXORExprNoIn '^' BitwiseANDExprNoIn
569 { $$ = new BitXOrNode($1, $3); }
570 ;
571
572 BitwiseXORExprNoBF:
573 BitwiseANDExprNoBF
574 | BitwiseXORExprNoBF '^' BitwiseANDExpr
575 { $$ = new BitXOrNode($1, $3); }
576 ;
577
578 BitwiseORExpr:
579 BitwiseXORExpr
580 | BitwiseORExpr '|' BitwiseXORExpr { $$ = new BitOrNode($1, $3); }
581 ;
582
583 BitwiseORExprNoIn:
584 BitwiseXORExprNoIn
585 | BitwiseORExprNoIn '|' BitwiseXORExprNoIn
586 { $$ = new BitOrNode($1, $3); }
587 ;
588
589 BitwiseORExprNoBF:
590 BitwiseXORExprNoBF
591 | BitwiseORExprNoBF '|' BitwiseXORExpr
592 { $$ = new BitOrNode($1, $3); }
593 ;
594
595 LogicalANDExpr:
596 BitwiseORExpr
597 | LogicalANDExpr AND BitwiseORExpr { $$ = new LogicalAndNode($1, $3); }
598 ;
599
600 LogicalANDExprNoIn:
601 BitwiseORExprNoIn
602 | LogicalANDExprNoIn AND BitwiseORExprNoIn
603 { $$ = new LogicalAndNode($1, $3); }
604 ;
605
606 LogicalANDExprNoBF:
607 BitwiseORExprNoBF
608 | LogicalANDExprNoBF AND BitwiseORExpr
609 { $$ = new LogicalAndNode($1, $3); }
610 ;
611
612 LogicalORExpr:
613 LogicalANDExpr
614 | LogicalORExpr OR LogicalANDExpr { $$ = new LogicalOrNode($1, $3); }
615 ;
616
617 LogicalORExprNoIn:
618 LogicalANDExprNoIn
619 | LogicalORExprNoIn OR LogicalANDExprNoIn
620 { $$ = new LogicalOrNode($1, $3); }
621 ;
622
623 LogicalORExprNoBF:
624 LogicalANDExprNoBF
625 | LogicalORExprNoBF OR LogicalANDExpr { $$ = new LogicalOrNode($1, $3); }
626 ;
627
628 ConditionalExpr:
629 LogicalORExpr
630 | LogicalORExpr '?' AssignmentExpr ':' AssignmentExpr
631 { $$ = new ConditionalNode($1, $3, $5); }
632 ;
633
634 ConditionalExprNoIn:
635 LogicalORExprNoIn
636 | LogicalORExprNoIn '?' AssignmentExprNoIn ':' AssignmentExprNoIn
637 { $$ = new ConditionalNode($1, $3, $5); }
638 ;
639
640 ConditionalExprNoBF:
641 LogicalORExprNoBF
642 | LogicalORExprNoBF '?' AssignmentExpr ':' AssignmentExpr
643 { $$ = new ConditionalNode($1, $3, $5); }
644 ;
645
646 AssignmentExpr:
647 ConditionalExpr
648 | LeftHandSideExpr AssignmentOperator AssignmentExpr
649 { $$ = makeAssignNode($1, $2, $3); }
650 ;
651
652 AssignmentExprNoIn:
653 ConditionalExprNoIn
654 | LeftHandSideExpr AssignmentOperator AssignmentExprNoIn
655 { $$ = makeAssignNode($1, $2, $3); }
656 ;
657
658 AssignmentExprNoBF:
659 ConditionalExprNoBF
660 | LeftHandSideExprNoBF AssignmentOperator AssignmentExpr
661 { $$ = makeAssignNode($1, $2, $3); }
662 ;
663
664 AssignmentOperator:
665 '=' { $$ = OpEqual; }
666 | PLUSEQUAL { $$ = OpPlusEq; }
667 | MINUSEQUAL { $$ = OpMinusEq; }
668 | MULTEQUAL { $$ = OpMultEq; }
669 | DIVEQUAL { $$ = OpDivEq; }
670 | LSHIFTEQUAL { $$ = OpLShift; }
671 | RSHIFTEQUAL { $$ = OpRShift; }
672 | URSHIFTEQUAL { $$ = OpURShift; }
673 | ANDEQUAL { $$ = OpAndEq; }
674 | XOREQUAL { $$ = OpXOrEq; }
675 | OREQUAL { $$ = OpOrEq; }
676 | MODEQUAL { $$ = OpModEq; }
677 ;
678
679 Expr:
680 AssignmentExpr
681 | Expr ',' AssignmentExpr { $$ = new CommaNode($1, $3); }
682 ;
683
684 ExprNoIn:
685 AssignmentExprNoIn
686 | ExprNoIn ',' AssignmentExprNoIn { $$ = new CommaNode($1, $3); }
687 ;
688
689 ExprNoBF:
690 AssignmentExprNoBF
691 | ExprNoBF ',' AssignmentExpr { $$ = new CommaNode($1, $3); }
692 ;
693
694 Statement:
695 Block
696 | VariableStatement
697 | ConstStatement
698 | EmptyStatement
699 | ExprStatement
700 | IfStatement
701 | IterationStatement
702 | ContinueStatement
703 | BreakStatement
704 | ReturnStatement
705 | WithStatement
706 | SwitchStatement
707 | LabelledStatement
708 | ThrowStatement
709 | TryStatement
710 | DebuggerStatement
711 ;
712
713 Block:
714 '{' '}' { $$ = createNodeInfo<StatementNode*>(new BlockNode(0), 0, 0);
715 DBG($$.m_node, @1, @2); }
716 | '{' SourceElements '}' { $$ = createNodeInfo<StatementNode*>(new BlockNode($2.m_node), $2.m_varDeclarations, $2.m_funcDeclarations);
717 DBG($$.m_node, @1, @3); }
718 ;
719
720 VariableStatement:
721 VAR VariableDeclarationList ';' { $$ = createNodeInfo<StatementNode*>(makeVarStatementNode($2.m_node), $2.m_varDeclarations, $2.m_funcDeclarations);
722 DBG($$.m_node, @1, @3); }
723 | VAR VariableDeclarationList error { $$ = createNodeInfo<StatementNode*>(makeVarStatementNode($2.m_node), $2.m_varDeclarations, $2.m_funcDeclarations);
724 DBG($$.m_node, @1, @2);
725 AUTO_SEMICOLON; }
726 ;
727
728 VariableDeclarationList:
729 IDENT { $$.m_node = 0;
730 $$.m_varDeclarations = new ParserRefCountedData<DeclarationStacks::VarStack>;
731 appendToVarDeclarationList($$.m_varDeclarations, *$1, 0);
732 $$.m_funcDeclarations = 0;
733 }
734 | IDENT Initializer { $$.m_node = new AssignResolveNode(*$1, $2);
735 $$.m_varDeclarations = new ParserRefCountedData<DeclarationStacks::VarStack>;
736 appendToVarDeclarationList($$.m_varDeclarations, *$1, DeclarationStacks::HasInitializer);
737 $$.m_funcDeclarations = 0;
738 }
739 | VariableDeclarationList ',' IDENT
740 { $$.m_node = $1.m_node;
741 $$.m_varDeclarations = $1.m_varDeclarations;
742 appendToVarDeclarationList($$.m_varDeclarations, *$3, 0);
743 $$.m_funcDeclarations = 0;
744 }
745 | VariableDeclarationList ',' IDENT Initializer
746 { $$.m_node = combineVarInitializers($1.m_node, new AssignResolveNode(*$3, $4));
747 $$.m_varDeclarations = $1.m_varDeclarations;
748 appendToVarDeclarationList($$.m_varDeclarations, *$3, DeclarationStacks::HasInitializer);
749 $$.m_funcDeclarations = 0;
750 }
751 ;
752
753 VariableDeclarationListNoIn:
754 IDENT { $$.m_node = 0;
755 $$.m_varDeclarations = new ParserRefCountedData<DeclarationStacks::VarStack>;
756 appendToVarDeclarationList($$.m_varDeclarations, *$1, 0);
757 $$.m_funcDeclarations = 0;
758 }
759 | IDENT InitializerNoIn { $$.m_node = new AssignResolveNode(*$1, $2);
760 $$.m_varDeclarations = new ParserRefCountedData<DeclarationStacks::VarStack>;
761 appendToVarDeclarationList($$.m_varDeclarations, *$1, DeclarationStacks::HasInitializer);
762 $$.m_funcDeclarations = 0;
763 }
764 | VariableDeclarationListNoIn ',' IDENT
765 { $$.m_node = $1.m_node;
766 $$.m_varDeclarations = $1.m_varDeclarations;
767 appendToVarDeclarationList($$.m_varDeclarations, *$3, 0);
768 $$.m_funcDeclarations = 0;
769 }
770 | VariableDeclarationListNoIn ',' IDENT InitializerNoIn
771 { $$.m_node = combineVarInitializers($1.m_node, new AssignResolveNode(*$3, $4));
772 $$.m_varDeclarations = $1.m_varDeclarations;
773 appendToVarDeclarationList($$.m_varDeclarations, *$3, DeclarationStacks::HasInitializer);
774 $$.m_funcDeclarations = 0;
775 }
776 ;
777
778 ConstStatement:
779 CONSTTOKEN ConstDeclarationList ';' { $$ = createNodeInfo<StatementNode*>(new ConstStatementNode($2.m_node.head), $2.m_varDeclarations, $2.m_funcDeclarations);
780 DBG($$.m_node, @1, @3); }
781 | CONSTTOKEN ConstDeclarationList error
782 { $$ = createNodeInfo<StatementNode*>(new ConstStatementNode($2.m_node.head), $2.m_varDeclarations, $2.m_funcDeclarations);
783 DBG($$.m_node, @1, @2); AUTO_SEMICOLON; }
784 ;
785
786 ConstDeclarationList:
787 ConstDeclaration { $$.m_node.head = $1;
788 $$.m_node.tail = $$.m_node.head;
789 $$.m_varDeclarations = new ParserRefCountedData<DeclarationStacks::VarStack>;
790 appendToVarDeclarationList($$.m_varDeclarations, $1);
791 $$.m_funcDeclarations = 0; }
792 | ConstDeclarationList ',' ConstDeclaration
793 { $$.m_node.head = $1.m_node.head;
794 $1.m_node.tail->m_next = $3;
795 $$.m_node.tail = $3;
796 $$.m_varDeclarations = $1.m_varDeclarations;
797 appendToVarDeclarationList($$.m_varDeclarations, $3);
798 $$.m_funcDeclarations = 0; }
799 ;
800
801 ConstDeclaration:
802 IDENT { $$ = new ConstDeclNode(*$1, 0); }
803 | IDENT Initializer { $$ = new ConstDeclNode(*$1, $2); }
804 ;
805
806 Initializer:
807 '=' AssignmentExpr { $$ = $2; }
808 ;
809
810 InitializerNoIn:
811 '=' AssignmentExprNoIn { $$ = $2; }
812 ;
813
814 EmptyStatement:
815 ';' { $$ = createNodeInfo<StatementNode*>(new EmptyStatementNode(), 0, 0); }
816 ;
817
818 ExprStatement:
819 ExprNoBF ';' { $$ = createNodeInfo<StatementNode*>(new ExprStatementNode($1), 0, 0);
820 DBG($$.m_node, @1, @2); }
821 | ExprNoBF error { $$ = createNodeInfo<StatementNode*>(new ExprStatementNode($1), 0, 0);
822 DBG($$.m_node, @1, @1); AUTO_SEMICOLON; }
823 ;
824
825 IfStatement:
826 IF '(' Expr ')' Statement %prec IF_WITHOUT_ELSE
827 { $$ = createNodeInfo<StatementNode*>(new IfNode($3, $5.m_node), $5.m_varDeclarations, $5.m_funcDeclarations);
828 DBG($$.m_node, @1, @4); }
829 | IF '(' Expr ')' Statement ELSE Statement
830 { $$ = createNodeInfo<StatementNode*>(new IfElseNode($3, $5.m_node, $7.m_node), mergeDeclarationLists($5.m_varDeclarations, $7.m_varDeclarations), mergeDeclarationLists($5.m_funcDeclarations, $7.m_funcDeclarations));
831 DBG($$.m_node, @1, @4); }
832 ;
833
834 IterationStatement:
835 DO Statement WHILE '(' Expr ')' ';' { $$ = createNodeInfo<StatementNode*>(new DoWhileNode($2.m_node, $5), $2.m_varDeclarations, $2.m_funcDeclarations);
836 DBG($$.m_node, @1, @3); }
837 | DO Statement WHILE '(' Expr ')' error { $$ = createNodeInfo<StatementNode*>(new DoWhileNode($2.m_node, $5), $2.m_varDeclarations, $2.m_funcDeclarations);
838 DBG($$.m_node, @1, @3); } // Always performs automatic semicolon insertion.
839 | WHILE '(' Expr ')' Statement { $$ = createNodeInfo<StatementNode*>(new WhileNode($3, $5.m_node), $5.m_varDeclarations, $5.m_funcDeclarations);
840 DBG($$.m_node, @1, @4); }
841 | FOR '(' ExprNoInOpt ';' ExprOpt ';' ExprOpt ')' Statement
842 { $$ = createNodeInfo<StatementNode*>(new ForNode($3, $5, $7, $9.m_node, false), $9.m_varDeclarations, $9.m_funcDeclarations);
843 DBG($$.m_node, @1, @8);
844 }
845 | FOR '(' VAR VariableDeclarationListNoIn ';' ExprOpt ';' ExprOpt ')' Statement
846 { $$ = createNodeInfo<StatementNode*>(new ForNode($4.m_node, $6, $8, $10.m_node, true),
847 mergeDeclarationLists($4.m_varDeclarations, $10.m_varDeclarations),
848 mergeDeclarationLists($4.m_funcDeclarations, $10.m_funcDeclarations));
849 DBG($$.m_node, @1, @9); }
850 | FOR '(' LeftHandSideExpr INTOKEN Expr ')' Statement
851 {
852 ExpressionNode* n = $3;
853 if (!n->isLocation())
854 YYABORT;
855 $$ = createNodeInfo<StatementNode*>(new ForInNode(n, $5, $7.m_node), $7.m_varDeclarations, $7.m_funcDeclarations);
856 DBG($$.m_node, @1, @6);
857 }
858 | FOR '(' VAR IDENT INTOKEN Expr ')' Statement
859 { ForInNode *forIn = new ForInNode(*$4, 0, $6, $8.m_node);
860 appendToVarDeclarationList($8.m_varDeclarations, *$4, DeclarationStacks::HasInitializer);
861 $$ = createNodeInfo<StatementNode*>(forIn, $8.m_varDeclarations, $8.m_funcDeclarations);
862 DBG($$.m_node, @1, @7); }
863 | FOR '(' VAR IDENT InitializerNoIn INTOKEN Expr ')' Statement
864 { ForInNode *forIn = new ForInNode(*$4, $5, $7, $9.m_node);
865 appendToVarDeclarationList($9.m_varDeclarations, *$4, DeclarationStacks::HasInitializer);
866 $$ = createNodeInfo<StatementNode*>(forIn, $9.m_varDeclarations, $9.m_funcDeclarations);
867 DBG($$.m_node, @1, @8); }
868 ;
869
870 ExprOpt:
871 /* nothing */ { $$ = 0; }
872 | Expr
873 ;
874
875 ExprNoInOpt:
876 /* nothing */ { $$ = 0; }
877 | ExprNoIn
878 ;
879
880 ContinueStatement:
881 CONTINUE ';' { $$ = createNodeInfo<StatementNode*>(new ContinueNode(), 0, 0);
882 DBG($$.m_node, @1, @2); }
883 | CONTINUE error { $$ = createNodeInfo<StatementNode*>(new ContinueNode(), 0, 0);
884 DBG($$.m_node, @1, @1); AUTO_SEMICOLON; }
885 | CONTINUE IDENT ';' { $$ = createNodeInfo<StatementNode*>(new ContinueNode(*$2), 0, 0);
886 DBG($$.m_node, @1, @3); }
887 | CONTINUE IDENT error { $$ = createNodeInfo<StatementNode*>(new ContinueNode(*$2), 0, 0);
888 DBG($$.m_node, @1, @2); AUTO_SEMICOLON; }
889 ;
890
891 BreakStatement:
892 BREAK ';' { $$ = createNodeInfo<StatementNode*>(new BreakNode(), 0, 0); DBG($$.m_node, @1, @2); }
893 | BREAK error { $$ = createNodeInfo<StatementNode*>(new BreakNode(), 0, 0); DBG($$.m_node, @1, @1); AUTO_SEMICOLON; }
894 | BREAK IDENT ';' { $$ = createNodeInfo<StatementNode*>(new BreakNode(*$2), 0, 0); DBG($$.m_node, @1, @3); }
895 | BREAK IDENT error { $$ = createNodeInfo<StatementNode*>(new BreakNode(*$2), 0, 0); DBG($$.m_node, @1, @2); AUTO_SEMICOLON; }
896 ;
897
898 ReturnStatement:
899 RETURN ';' { $$ = createNodeInfo<StatementNode*>(new ReturnNode(0), 0, 0); DBG($$.m_node, @1, @2); }
900 | RETURN error { $$ = createNodeInfo<StatementNode*>(new ReturnNode(0), 0, 0); DBG($$.m_node, @1, @1); AUTO_SEMICOLON; }
901 | RETURN Expr ';' { $$ = createNodeInfo<StatementNode*>(new ReturnNode($2), 0, 0); DBG($$.m_node, @1, @3); }
902 | RETURN Expr error { $$ = createNodeInfo<StatementNode*>(new ReturnNode($2), 0, 0); DBG($$.m_node, @1, @2); AUTO_SEMICOLON; }
903 ;
904
905 WithStatement:
906 WITH '(' Expr ')' Statement { $$ = createNodeInfo<StatementNode*>(new WithNode($3, $5.m_node), $5.m_varDeclarations, $5.m_funcDeclarations);
907 DBG($$.m_node, @1, @4); }
908 ;
909
910 SwitchStatement:
911 SWITCH '(' Expr ')' CaseBlock { $$ = createNodeInfo<StatementNode*>(new SwitchNode($3, $5.m_node), $5.m_varDeclarations, $5.m_funcDeclarations);
912 DBG($$.m_node, @1, @4); }
913 ;
914
915 CaseBlock:
916 '{' CaseClausesOpt '}' { $$ = createNodeInfo<CaseBlockNode*>(new CaseBlockNode($2.m_node.head, 0, 0), $2.m_varDeclarations, $2.m_funcDeclarations); }
917 | '{' CaseClausesOpt DefaultClause CaseClausesOpt '}'
918 { $$ = createNodeInfo<CaseBlockNode*>(new CaseBlockNode($2.m_node.head, $3.m_node, $4.m_node.head),
919 mergeDeclarationLists(mergeDeclarationLists($2.m_varDeclarations, $3.m_varDeclarations), $4.m_varDeclarations),
920 mergeDeclarationLists(mergeDeclarationLists($2.m_funcDeclarations, $3.m_funcDeclarations), $4.m_funcDeclarations)); }
921 ;
922
923 CaseClausesOpt:
924 /* nothing */ { $$.m_node.head = 0; $$.m_node.tail = 0; $$.m_varDeclarations = 0; $$.m_funcDeclarations = 0; }
925 | CaseClauses
926 ;
927
928 CaseClauses:
929 CaseClause { $$.m_node.head = new ClauseListNode($1.m_node);
930 $$.m_node.tail = $$.m_node.head;
931 $$.m_varDeclarations = $1.m_varDeclarations;
932 $$.m_funcDeclarations = $1.m_funcDeclarations; }
933 | CaseClauses CaseClause { $$.m_node.head = $1.m_node.head;
934 $$.m_node.tail = new ClauseListNode($1.m_node.tail, $2.m_node);
935 $$.m_varDeclarations = mergeDeclarationLists($1.m_varDeclarations, $2.m_varDeclarations);
936 $$.m_funcDeclarations = mergeDeclarationLists($1.m_funcDeclarations, $2.m_funcDeclarations);
937 }
938 ;
939
940 CaseClause:
941 CASE Expr ':' { $$ = createNodeInfo<CaseClauseNode*>(new CaseClauseNode($2), 0, 0); }
942 | CASE Expr ':' SourceElements { $$ = createNodeInfo<CaseClauseNode*>(new CaseClauseNode($2, $4.m_node), $4.m_varDeclarations, $4.m_funcDeclarations); }
943 ;
944
945 DefaultClause:
946 DEFAULT ':' { $$ = createNodeInfo<CaseClauseNode*>(new CaseClauseNode(0), 0, 0); }
947 | DEFAULT ':' SourceElements { $$ = createNodeInfo<CaseClauseNode*>(new CaseClauseNode(0, $3.m_node), $3.m_varDeclarations, $3.m_funcDeclarations); }
948 ;
949
950 LabelledStatement:
951 IDENT ':' Statement { $3.m_node->pushLabel(*$1);
952 $$ = createNodeInfo<StatementNode*>(new LabelNode(*$1, $3.m_node), $3.m_varDeclarations, $3.m_funcDeclarations); }
953 ;
954
955 ThrowStatement:
956 THROW Expr ';' { $$ = createNodeInfo<StatementNode*>(new ThrowNode($2), 0, 0); DBG($$.m_node, @1, @3); }
957 | THROW Expr error { $$ = createNodeInfo<StatementNode*>(new ThrowNode($2), 0, 0); DBG($$.m_node, @1, @2); AUTO_SEMICOLON; }
958 ;
959
960 TryStatement:
961 TRY Block FINALLY Block { $$ = createNodeInfo<StatementNode*>(new TryNode($2.m_node, CommonIdentifiers::shared()->nullIdentifier, 0, $4.m_node),
962 mergeDeclarationLists($2.m_varDeclarations, $4.m_varDeclarations),
963 mergeDeclarationLists($2.m_funcDeclarations, $4.m_funcDeclarations));
964 DBG($$.m_node, @1, @2); }
965 | TRY Block CATCH '(' IDENT ')' Block { $$ = createNodeInfo<StatementNode*>(new TryNode($2.m_node, *$5, $7.m_node, 0),
966 mergeDeclarationLists($2.m_varDeclarations, $7.m_varDeclarations),
967 mergeDeclarationLists($2.m_funcDeclarations, $7.m_funcDeclarations));
968 DBG($$.m_node, @1, @2); }
969 | TRY Block CATCH '(' IDENT ')' Block FINALLY Block
970 { $$ = createNodeInfo<StatementNode*>(new TryNode($2.m_node, *$5, $7.m_node, $9.m_node),
971 mergeDeclarationLists(mergeDeclarationLists($2.m_varDeclarations, $7.m_varDeclarations), $9.m_varDeclarations),
972 mergeDeclarationLists(mergeDeclarationLists($2.m_funcDeclarations, $7.m_funcDeclarations), $9.m_funcDeclarations));
973 DBG($$.m_node, @1, @2); }
974 ;
975
976 DebuggerStatement:
977 DEBUGGER ';' { $$ = createNodeInfo<StatementNode*>(new EmptyStatementNode(), 0, 0);
978 DBG($$.m_node, @1, @2); }
979 | DEBUGGER error { $$ = createNodeInfo<StatementNode*>(new EmptyStatementNode(), 0, 0);
980 DBG($$.m_node, @1, @1); AUTO_SEMICOLON; }
981 ;
982
983 FunctionDeclaration:
984 FUNCTION IDENT '(' ')' '{' FunctionBody '}' { $$ = new FuncDeclNode(*$2, $6); DBG($6, @5, @7); }
985 | FUNCTION IDENT '(' FormalParameterList ')' '{' FunctionBody '}'
986 { $$ = new FuncDeclNode(*$2, $4.head, $7); DBG($7, @6, @8); }
987 ;
988
989 FunctionExpr:
990 FUNCTION '(' ')' '{' FunctionBody '}' { $$ = new FuncExprNode(CommonIdentifiers::shared()->nullIdentifier, $5); DBG($5, @4, @6); }
991 | FUNCTION '(' FormalParameterList ')' '{' FunctionBody '}' { $$ = new FuncExprNode(CommonIdentifiers::shared()->nullIdentifier, $6, $3.head); DBG($6, @5, @7); }
992 | FUNCTION IDENT '(' ')' '{' FunctionBody '}' { $$ = new FuncExprNode(*$2, $6); DBG($6, @5, @7); }
993 | FUNCTION IDENT '(' FormalParameterList ')' '{' FunctionBody '}' { $$ = new FuncExprNode(*$2, $7, $4.head); DBG($7, @6, @8); }
994 ;
995
996 FormalParameterList:
997 IDENT { $$.head = new ParameterNode(*$1);
998 $$.tail = $$.head; }
999 | FormalParameterList ',' IDENT { $$.head = $1.head;
1000 $$.tail = new ParameterNode($1.tail, *$3); }
1001 ;
1002
1003 FunctionBody:
1004 /* not in spec */ { $$ = FunctionBodyNode::create(0, 0, 0); }
1005 | SourceElements { $$ = FunctionBodyNode::create($1.m_node, $1.m_varDeclarations ? &$1.m_varDeclarations->data : 0,
1006 $1.m_funcDeclarations ? &$1.m_funcDeclarations->data : 0);
1007 // As in mergeDeclarationLists() we have to ref/deref to safely get rid of
1008 // the declaration lists.
1009 if ($1.m_varDeclarations) {
1010 $1.m_varDeclarations->ref();
1011 $1.m_varDeclarations->deref();
1012 }
1013 if ($1.m_funcDeclarations) {
1014 $1.m_funcDeclarations->ref();
1015 $1.m_funcDeclarations->deref();
1016 }
1017 }
1018 ;
1019
1020 Program:
1021 /* not in spec */ { parser().didFinishParsing(0, 0, 0, @0.last_line); }
1022 | SourceElements { parser().didFinishParsing($1.m_node, $1.m_varDeclarations, $1.m_funcDeclarations, @1.last_line); }
1023 ;
1024
1025 SourceElements:
1026 SourceElement { $$.m_node = new SourceElements;
1027 $$.m_node->append($1.m_node);
1028 $$.m_varDeclarations = $1.m_varDeclarations;
1029 $$.m_funcDeclarations = $1.m_funcDeclarations;
1030 }
1031 | SourceElements SourceElement { $$.m_node->append($2.m_node);
1032 $$.m_varDeclarations = mergeDeclarationLists($1.m_varDeclarations, $2.m_varDeclarations);
1033 $$.m_funcDeclarations = mergeDeclarationLists($1.m_funcDeclarations, $2.m_funcDeclarations);
1034 }
1035 ;
1036
1037 SourceElement:
1038 FunctionDeclaration { $$ = createNodeInfo<StatementNode*>($1, 0, new ParserRefCountedData<DeclarationStacks::FunctionStack>); $$.m_funcDeclarations->data.append($1); }
1039 | Statement { $$ = $1; }
1040 ;
1041
1042 %%
1043
1044 static AddNode* makeAddNode(ExpressionNode* left, ExpressionNode* right)
1045 {
1046 JSType t1 = left->expectedReturnType();
1047 JSType t2 = right->expectedReturnType();
1048
1049 if (t1 == NumberType && t2 == NumberType)
1050 return new AddNumbersNode(left, right);
1051 if (t1 == StringType && t2 == StringType)
1052 return new AddStringsNode(left, right);
1053 if (t1 == StringType)
1054 return new AddStringLeftNode(left, right);
1055 if (t2 == StringType)
1056 return new AddStringRightNode(left, right);
1057 return new AddNode(left, right);
1058 }
1059
1060 static LessNode* makeLessNode(ExpressionNode* left, ExpressionNode* right)
1061 {
1062 JSType t1 = left->expectedReturnType();
1063 JSType t2 = right->expectedReturnType();
1064
1065 if (t1 == StringType && t2 == StringType)
1066 return new LessStringsNode(left, right);
1067
1068 // There are certainly more efficient ways to do this type check if necessary
1069 if (t1 == NumberType || t1 == BooleanType || t1 == UndefinedType || t1 == NullType ||
1070 t2 == NumberType || t2 == BooleanType || t2 == UndefinedType || t2 == NullType)
1071 return new LessNumbersNode(left, right);
1072
1073 // Neither is certain to be a number, nor were both certain to be strings, so we use the default (slow) implementation.
1074 return new LessNode(left, right);
1075 }
1076
1077 static ExpressionNode* makeAssignNode(ExpressionNode* loc, Operator op, ExpressionNode* expr)
1078 {
1079 if (!loc->isLocation())
1080 return new AssignErrorNode(loc, op, expr);
1081
1082 if (loc->isResolveNode()) {
1083 ResolveNode* resolve = static_cast<ResolveNode*>(loc);
1084 if (op == OpEqual)
1085 return new AssignResolveNode(resolve->identifier(), expr);
1086 else
1087 return new ReadModifyResolveNode(resolve->identifier(), op, expr);
1088 }
1089 if (loc->isBracketAccessorNode()) {
1090 BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(loc);
1091 if (op == OpEqual)
1092 return new AssignBracketNode(bracket->base(), bracket->subscript(), expr);
1093 else
1094 return new ReadModifyBracketNode(bracket->base(), bracket->subscript(), op, expr);
1095 }
1096 ASSERT(loc->isDotAccessorNode());
1097 DotAccessorNode* dot = static_cast<DotAccessorNode*>(loc);
1098 if (op == OpEqual)
1099 return new AssignDotNode(dot->base(), dot->identifier(), expr);
1100 return new ReadModifyDotNode(dot->base(), dot->identifier(), op, expr);
1101 }
1102
1103 static ExpressionNode* makePrefixNode(ExpressionNode* expr, Operator op)
1104 {
1105 if (!expr->isLocation())
1106 return new PrefixErrorNode(expr, op);
1107
1108 if (expr->isResolveNode()) {
1109 ResolveNode* resolve = static_cast<ResolveNode*>(expr);
1110 if (op == OpPlusPlus)
1111 return new PreIncResolveNode(resolve->identifier());
1112 else
1113 return new PreDecResolveNode(resolve->identifier());
1114 }
1115 if (expr->isBracketAccessorNode()) {
1116 BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(expr);
1117 if (op == OpPlusPlus)
1118 return new PreIncBracketNode(bracket->base(), bracket->subscript());
1119 else
1120 return new PreDecBracketNode(bracket->base(), bracket->subscript());
1121 }
1122 ASSERT(expr->isDotAccessorNode());
1123 DotAccessorNode* dot = static_cast<DotAccessorNode*>(expr);
1124 if (op == OpPlusPlus)
1125 return new PreIncDotNode(dot->base(), dot->identifier());
1126 return new PreDecDotNode(dot->base(), dot->identifier());
1127 }
1128
1129 static ExpressionNode* makePostfixNode(ExpressionNode* expr, Operator op)
1130 {
1131 if (!expr->isLocation())
1132 return new PostfixErrorNode(expr, op);
1133
1134 if (expr->isResolveNode()) {
1135 ResolveNode* resolve = static_cast<ResolveNode*>(expr);
1136 if (op == OpPlusPlus)
1137 return new PostIncResolveNode(resolve->identifier());
1138 else
1139 return new PostDecResolveNode(resolve->identifier());
1140 }
1141 if (expr->isBracketAccessorNode()) {
1142 BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(expr);
1143 if (op == OpPlusPlus)
1144 return new PostIncBracketNode(bracket->base(), bracket->subscript());
1145 else
1146 return new PostDecBracketNode(bracket->base(), bracket->subscript());
1147 }
1148 ASSERT(expr->isDotAccessorNode());
1149 DotAccessorNode* dot = static_cast<DotAccessorNode*>(expr);
1150
1151 if (op == OpPlusPlus)
1152 return new PostIncDotNode(dot->base(), dot->identifier());
1153 return new PostDecDotNode(dot->base(), dot->identifier());
1154 }
1155
1156 static ExpressionNode* makeFunctionCallNode(ExpressionNode* func, ArgumentsNode* args)
1157 {
1158 if (!func->isLocation())
1159 return new FunctionCallValueNode(func, args);
1160 if (func->isResolveNode()) {
1161 ResolveNode* resolve = static_cast<ResolveNode*>(func);
1162 return new FunctionCallResolveNode(resolve->identifier(), args);
1163 }
1164 if (func->isBracketAccessorNode()) {
1165 BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(func);
1166 return new FunctionCallBracketNode(bracket->base(), bracket->subscript(), args);
1167 }
1168 ASSERT(func->isDotAccessorNode());
1169 DotAccessorNode* dot = static_cast<DotAccessorNode*>(func);
1170 return new FunctionCallDotNode(dot->base(), dot->identifier(), args);
1171 }
1172
1173 static ExpressionNode* makeTypeOfNode(ExpressionNode* expr)
1174 {
1175 if (expr->isResolveNode()) {
1176 ResolveNode* resolve = static_cast<ResolveNode*>(expr);
1177 return new TypeOfResolveNode(resolve->identifier());
1178 }
1179 return new TypeOfValueNode(expr);
1180 }
1181
1182 static ExpressionNode* makeDeleteNode(ExpressionNode* expr)
1183 {
1184 if (!expr->isLocation())
1185 return new DeleteValueNode(expr);
1186 if (expr->isResolveNode()) {
1187 ResolveNode* resolve = static_cast<ResolveNode*>(expr);
1188 return new DeleteResolveNode(resolve->identifier());
1189 }
1190 if (expr->isBracketAccessorNode()) {
1191 BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(expr);
1192 return new DeleteBracketNode(bracket->base(), bracket->subscript());
1193 }
1194 ASSERT(expr->isDotAccessorNode());
1195 DotAccessorNode* dot = static_cast<DotAccessorNode*>(expr);
1196 return new DeleteDotNode(dot->base(), dot->identifier());
1197 }
1198
1199 static PropertyNode* makeGetterOrSetterPropertyNode(const Identifier& getOrSet, const Identifier& name, ParameterNode* params, FunctionBodyNode* body)
1200 {
1201 PropertyNode::Type type;
1202 if (getOrSet == "get")
1203 type = PropertyNode::Getter;
1204 else if (getOrSet == "set")
1205 type = PropertyNode::Setter;
1206 else
1207 return 0;
1208 return new PropertyNode(name, new FuncExprNode(CommonIdentifiers::shared()->nullIdentifier, body, params), type);
1209 }
1210
1211 static ExpressionNode* makeNegateNode(ExpressionNode* n)
1212 {
1213 if (n->isNumber()) {
1214 NumberNode* number = static_cast<NumberNode*>(n);
1215
1216 if (number->value() > 0.0) {
1217 number->setValue(-number->value());
1218 return number;
1219 }
1220 }
1221
1222 return new NegateNode(n);
1223 }
1224
1225 static NumberNode* makeNumberNode(double d)
1226 {
1227 JSValue* value = JSImmediate::from(d);
1228 if (value)
1229 return new ImmediateNumberNode(value, d);
1230 return new NumberNode(d);
1231 }
1232
1233 /* called by yyparse on error */
1234 int yyerror(const char *)
1235 {
1236 return 1;
1237 }
1238
1239 /* may we automatically insert a semicolon ? */
1240 static bool allowAutomaticSemicolon()
1241 {
1242 return yychar == '}' || yychar == 0 || lexer().prevTerminator();
1243 }
1244
1245 static ExpressionNode* combineVarInitializers(ExpressionNode* list, AssignResolveNode* init)
1246 {
1247 if (!list)
1248 return init;
1249 return new VarDeclCommaNode(list, init);
1250 }
1251
1252 // We turn variable declarations into either assignments or empty
1253 // statements (which later get stripped out), because the actual
1254 // declaration work is hoisted up to the start of the function body
1255 static StatementNode* makeVarStatementNode(ExpressionNode* expr)
1256 {
1257 if (!expr)
1258 return new EmptyStatementNode();
1259 return new VarStatementNode(expr);
1260 }
1261