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