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