PageRenderTime 62ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/opensource.apple.com/source/JavaScriptCore/JavaScriptCore-418.3/kjs/grammar.y

#
Happy | 670 lines | 581 code | 89 blank | 0 comment | 0 complexity | 577ccb24f0f520ed29265b203d8a130e MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, GPL-2.0, BSD-3-Clause, GPL-3.0, MPL-2.0, LGPL-2.0, LGPL-2.1, CC-BY-SA-3.0, IPL-1.0, ISC, AGPL-1.0, AGPL-3.0, JSON, Apache-2.0, 0BSD
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <title>grammar.y</title>
  6. <style type="text/css">
  7. .enscript-comment { font-style: italic; color: rgb(178,34,34); }
  8. .enscript-function-name { font-weight: bold; color: rgb(0,0,255); }
  9. .enscript-variable-name { font-weight: bold; color: rgb(184,134,11); }
  10. .enscript-keyword { font-weight: bold; color: rgb(160,32,240); }
  11. .enscript-reference { font-weight: bold; color: rgb(95,158,160); }
  12. .enscript-string { font-weight: bold; color: rgb(188,143,143); }
  13. .enscript-builtin { font-weight: bold; color: rgb(218,112,214); }
  14. .enscript-type { font-weight: bold; color: rgb(34,139,34); }
  15. .enscript-highlight { text-decoration: underline; color: 0; }
  16. </style>
  17. </head>
  18. <body id="top">
  19. <h1 style="margin:8px;" id="f1">grammar.y&nbsp;&nbsp;&nbsp;<span style="font-weight: normal; font-size: 0.5em;">[<a href="?txt">plain text</a>]</span></h1>
  20. <hr/>
  21. <div></div>
  22. <pre>
  23. %{
  24. /*
  25. * This file is part of the KDE libraries
  26. * Copyright (C) 1999-2000 Harri Porten (<a href="mailto:porten@kde.org">porten@kde.org</a>)
  27. *
  28. * This library is free software; you can redistribute it and/or
  29. * modify it under the terms of the GNU Lesser General Public
  30. * License as published by the Free Software Foundation; either
  31. * version 2 of the License, or (at your option) any later version.
  32. *
  33. * This library is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  36. * Lesser General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU Lesser General Public
  39. * License along with this library; if not, write to the Free Software
  40. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  41. *
  42. */
  43. #ifdef HAVE_CONFIG_H
  44. #include &lt;config.h&gt;
  45. #endif
  46. #include &lt;string.h&gt;
  47. #include &lt;stdlib.h&gt;
  48. #include &quot;value.h&quot;
  49. #include &quot;object.h&quot;
  50. #include &quot;types.h&quot;
  51. #include &quot;interpreter.h&quot;
  52. #include &quot;nodes.h&quot;
  53. #include &quot;lexer.h&quot;
  54. #include &quot;internal.h&quot;
  55. // Not sure why, but yacc doesn't add this define along with the others.
  56. #define yylloc kjsyylloc
  57. /* default values for bison */
  58. #define YYDEBUG 0
  59. #define YYMAXDEPTH 0
  60. #if !APPLE_CHANGES
  61. #define YYERROR_VERBOSE
  62. #endif
  63. #define DBG(l, s, e) { l-&gt;setLoc(s.first_line, e.last_line, Parser::sid); } // location
  64. extern int kjsyylex();
  65. int kjsyyerror(const char *);
  66. static bool automatic();
  67. using namespace KJS;
  68. %}
  69. %union {
  70. int ival;
  71. double dval;
  72. UString *ustr;
  73. Identifier *ident;
  74. Node *node;
  75. StatementNode *stat;
  76. ParameterNode *param;
  77. FunctionBodyNode *body;
  78. FuncDeclNode *func;
  79. ProgramNode *prog;
  80. AssignExprNode *init;
  81. SourceElementsNode *srcs;
  82. StatListNode *slist;
  83. ArgumentsNode *args;
  84. ArgumentListNode *alist;
  85. VarDeclNode *decl;
  86. VarDeclListNode *vlist;
  87. CaseBlockNode *cblk;
  88. ClauseListNode *clist;
  89. CaseClauseNode *ccl;
  90. ElementNode *elm;
  91. Operator op;
  92. PropertyValueNode *plist;
  93. PropertyNode *pnode;
  94. CatchNode *cnode;
  95. FinallyNode *fnode;
  96. }
  97. %start Program
  98. /* expect a shift/reduce conflict from the &quot;dangling else&quot; problem
  99. when using bison the warning can be supressed */
  100. // %expect 1
  101. /* literals */
  102. %token NULLTOKEN TRUETOKEN FALSETOKEN
  103. %token STRING NUMBER
  104. /* keywords */
  105. %token BREAK CASE DEFAULT FOR NEW VAR CONTINUE
  106. %token FUNCTION RETURN VOID DELETE
  107. %token IF THIS DO WHILE ELSE IN INSTANCEOF TYPEOF
  108. %token SWITCH WITH RESERVED
  109. %token THROW TRY CATCH FINALLY
  110. /* punctuators */
  111. %token EQEQ NE /* == and != */
  112. %token STREQ STRNEQ /* === and !== */
  113. %token LE GE /* &lt; and &gt; */
  114. %token OR AND /* || and &amp;&amp; */
  115. %token PLUSPLUS MINUSMINUS /* ++ and -- */
  116. %token LSHIFT /* &lt;&lt; */
  117. %token RSHIFT URSHIFT /* &gt;&gt; and &gt;&gt;&gt; */
  118. %token PLUSEQUAL MINUSEQUAL /* += and -= */
  119. %token MULTEQUAL DIVEQUAL /* *= and /= */
  120. %token LSHIFTEQUAL /* &lt;&lt;= */
  121. %token RSHIFTEQUAL URSHIFTEQUAL /* &gt;&gt;= and &gt;&gt;&gt;= */
  122. %token ANDEQUAL MODEQUAL /* &amp;= and %= */
  123. %token XOREQUAL OREQUAL /* ^= and |= */
  124. /* terminal types */
  125. %token &lt;dval&gt; NUMBER
  126. %token &lt;ustr&gt; STRING
  127. %token &lt;ident&gt; IDENT
  128. /* automatically inserted semicolon */
  129. %token AUTOPLUSPLUS AUTOMINUSMINUS
  130. /* non-terminal types */
  131. %type &lt;node&gt; Literal PrimaryExpr Expr MemberExpr FunctionExpr NewExpr CallExpr
  132. %type &lt;node&gt; ArrayLiteral
  133. %type &lt;node&gt; LeftHandSideExpr PostfixExpr UnaryExpr
  134. %type &lt;node&gt; MultiplicativeExpr AdditiveExpr
  135. %type &lt;node&gt; ShiftExpr RelationalExpr EqualityExpr
  136. %type &lt;node&gt; BitwiseANDExpr BitwiseXORExpr BitwiseORExpr
  137. %type &lt;node&gt; LogicalANDExpr LogicalORExpr
  138. %type &lt;node&gt; ConditionalExpr AssignmentExpr
  139. %type &lt;node&gt; ExprOpt
  140. %type &lt;node&gt; CallExpr
  141. %type &lt;cnode&gt; Catch
  142. %type &lt;fnode&gt; Finally
  143. %type &lt;stat&gt; Statement Block
  144. %type &lt;stat&gt; VariableStatement EmptyStatement ExprStatement
  145. %type &lt;stat&gt; IfStatement IterationStatement ContinueStatement
  146. %type &lt;stat&gt; BreakStatement ReturnStatement WithStatement
  147. %type &lt;stat&gt; SwitchStatement LabelledStatement
  148. %type &lt;stat&gt; ThrowStatement TryStatement
  149. %type &lt;stat&gt; SourceElement
  150. %type &lt;slist&gt; StatementList
  151. %type &lt;init&gt; Initializer
  152. %type &lt;func&gt; FunctionDeclaration
  153. %type &lt;body&gt; FunctionBody
  154. %type &lt;srcs&gt; SourceElements
  155. %type &lt;param&gt; FormalParameterList
  156. %type &lt;op&gt; AssignmentOperator
  157. %type &lt;prog&gt; Program
  158. %type &lt;args&gt; Arguments
  159. %type &lt;alist&gt; ArgumentList
  160. %type &lt;vlist&gt; VariableDeclarationList
  161. %type &lt;decl&gt; VariableDeclaration
  162. %type &lt;cblk&gt; CaseBlock
  163. %type &lt;ccl&gt; CaseClause DefaultClause
  164. %type &lt;clist&gt; CaseClauses CaseClausesOpt
  165. %type &lt;ival&gt; Elision ElisionOpt
  166. %type &lt;elm&gt; ElementList
  167. %type &lt;plist&gt; PropertyNameAndValueList
  168. %type &lt;pnode&gt; PropertyName
  169. %%
  170. Literal:
  171. NULLTOKEN { $$ = new NullNode(); }
  172. | TRUETOKEN { $$ = new BooleanNode(true); }
  173. | FALSETOKEN { $$ = new BooleanNode(false); }
  174. | NUMBER { $$ = new NumberNode($1); }
  175. | STRING { $$ = new StringNode($1); }
  176. | '/' /* a RegExp ? */ { Lexer *l = Lexer::curr();
  177. if (!l-&gt;scanRegExp()) YYABORT;
  178. $$ = new RegExpNode(l-&gt;pattern,l-&gt;flags);}
  179. | DIVEQUAL /* a RegExp starting with /= ! */
  180. { Lexer *l = Lexer::curr();
  181. if (!l-&gt;scanRegExp()) YYABORT;
  182. $$ = new RegExpNode(UString('=')+l-&gt;pattern,l-&gt;flags);}
  183. ;
  184. PrimaryExpr:
  185. THIS { $$ = new ThisNode(); }
  186. | IDENT { $$ = new ResolveNode(*$1); }
  187. | Literal
  188. | ArrayLiteral
  189. | '(' Expr ')' { $$ = new GroupNode($2); }
  190. | '{' '}' { $$ = new ObjectLiteralNode(); }
  191. | '{' PropertyNameAndValueList '}' { $$ = new ObjectLiteralNode($2); }
  192. ;
  193. ArrayLiteral:
  194. '[' ElisionOpt ']' { $$ = new ArrayNode($2); }
  195. | '[' ElementList ']' { $$ = new ArrayNode($2); }
  196. | '[' ElementList ',' ElisionOpt ']' { $$ = new ArrayNode($4, $2); }
  197. ;
  198. ElementList:
  199. ElisionOpt AssignmentExpr { $$ = new ElementNode($1, $2); }
  200. | ElementList ',' ElisionOpt AssignmentExpr
  201. { $$ = new ElementNode($1, $3, $4); }
  202. ;
  203. ElisionOpt:
  204. /* nothing */ { $$ = 0; }
  205. | Elision
  206. ;
  207. Elision:
  208. ',' { $$ = 1; }
  209. | Elision ',' { $$ = $1 + 1; }
  210. ;
  211. PropertyNameAndValueList:
  212. PropertyName ':' AssignmentExpr { $$ = new PropertyValueNode($1, $3); }
  213. | PropertyNameAndValueList ',' PropertyName ':' AssignmentExpr
  214. { $$ = new PropertyValueNode($3, $5, $1); }
  215. ;
  216. PropertyName:
  217. IDENT { $$ = new PropertyNode(*$1); }
  218. | STRING { $$ = new PropertyNode(Identifier(*$1)); }
  219. | NUMBER { $$ = new PropertyNode($1); }
  220. ;
  221. MemberExpr:
  222. PrimaryExpr
  223. | FunctionExpr
  224. | MemberExpr '[' Expr ']' { $$ = new AccessorNode1($1, $3); }
  225. | MemberExpr '.' IDENT { $$ = new AccessorNode2($1, *$3); }
  226. | NEW MemberExpr Arguments { $$ = new NewExprNode($2, $3); }
  227. ;
  228. NewExpr:
  229. MemberExpr
  230. | NEW NewExpr { $$ = new NewExprNode($2); }
  231. ;
  232. CallExpr:
  233. MemberExpr Arguments { $$ = new FunctionCallNode($1, $2); }
  234. | CallExpr Arguments { $$ = new FunctionCallNode($1, $2); }
  235. | CallExpr '[' Expr ']' { $$ = new AccessorNode1($1, $3); }
  236. | CallExpr '.' IDENT { $$ = new AccessorNode2($1, *$3); }
  237. ;
  238. Arguments:
  239. '(' ')' { $$ = new ArgumentsNode(); }
  240. | '(' ArgumentList ')' { $$ = new ArgumentsNode($2); }
  241. ;
  242. ArgumentList:
  243. AssignmentExpr { $$ = new ArgumentListNode($1); }
  244. | ArgumentList ',' AssignmentExpr { $$ = new ArgumentListNode($1, $3); }
  245. ;
  246. LeftHandSideExpr:
  247. NewExpr
  248. | CallExpr
  249. ;
  250. PostfixExpr: /* TODO: no line terminator here */
  251. LeftHandSideExpr
  252. | LeftHandSideExpr PLUSPLUS { $$ = new PostfixNode($1, OpPlusPlus); }
  253. | LeftHandSideExpr MINUSMINUS { $$ = new PostfixNode($1, OpMinusMinus); }
  254. ;
  255. UnaryExpr:
  256. PostfixExpr
  257. | DELETE UnaryExpr { $$ = new DeleteNode($2); }
  258. | VOID UnaryExpr { $$ = new VoidNode($2); }
  259. | TYPEOF UnaryExpr { $$ = new TypeOfNode($2); }
  260. | PLUSPLUS UnaryExpr { $$ = new PrefixNode(OpPlusPlus, $2); }
  261. | AUTOPLUSPLUS UnaryExpr { $$ = new PrefixNode(OpPlusPlus, $2); }
  262. | MINUSMINUS UnaryExpr { $$ = new PrefixNode(OpMinusMinus, $2); }
  263. | AUTOMINUSMINUS UnaryExpr { $$ = new PrefixNode(OpMinusMinus, $2); }
  264. | '+' UnaryExpr { $$ = new UnaryPlusNode($2); }
  265. | '-' UnaryExpr { $$ = new NegateNode($2); }
  266. | '~' UnaryExpr { $$ = new BitwiseNotNode($2); }
  267. | '!' UnaryExpr { $$ = new LogicalNotNode($2); }
  268. ;
  269. MultiplicativeExpr:
  270. UnaryExpr
  271. | MultiplicativeExpr '*' UnaryExpr { $$ = new MultNode($1, $3, '*'); }
  272. | MultiplicativeExpr '/' UnaryExpr { $$ = new MultNode($1, $3, '/'); }
  273. | MultiplicativeExpr '%' UnaryExpr { $$ = new MultNode($1,$3,'%'); }
  274. ;
  275. AdditiveExpr:
  276. MultiplicativeExpr
  277. | AdditiveExpr '+' MultiplicativeExpr { $$ = new AddNode($1, $3, '+'); }
  278. | AdditiveExpr '-' MultiplicativeExpr { $$ = new AddNode($1, $3, '-'); }
  279. ;
  280. ShiftExpr:
  281. AdditiveExpr
  282. | ShiftExpr LSHIFT AdditiveExpr { $$ = new ShiftNode($1, OpLShift, $3); }
  283. | ShiftExpr RSHIFT AdditiveExpr { $$ = new ShiftNode($1, OpRShift, $3); }
  284. | ShiftExpr URSHIFT AdditiveExpr { $$ = new ShiftNode($1, OpURShift, $3); }
  285. ;
  286. RelationalExpr:
  287. ShiftExpr
  288. | RelationalExpr '&lt;' ShiftExpr
  289. { $$ = new RelationalNode($1, OpLess, $3); }
  290. | RelationalExpr '&gt;' ShiftExpr
  291. { $$ = new RelationalNode($1, OpGreater, $3); }
  292. | RelationalExpr LE ShiftExpr
  293. { $$ = new RelationalNode($1, OpLessEq, $3); }
  294. | RelationalExpr GE ShiftExpr
  295. { $$ = new RelationalNode($1, OpGreaterEq, $3); }
  296. | RelationalExpr INSTANCEOF ShiftExpr
  297. { $$ = new RelationalNode($1, OpInstanceOf, $3); }
  298. | RelationalExpr IN ShiftExpr
  299. { $$ = new RelationalNode($1, OpIn, $3); }
  300. ;
  301. EqualityExpr:
  302. RelationalExpr
  303. | EqualityExpr EQEQ RelationalExpr { $$ = new EqualNode($1, OpEqEq, $3); }
  304. | EqualityExpr NE RelationalExpr { $$ = new EqualNode($1, OpNotEq, $3); }
  305. | EqualityExpr STREQ RelationalExpr { $$ = new EqualNode($1, OpStrEq, $3); }
  306. | EqualityExpr STRNEQ RelationalExpr { $$ = new EqualNode($1, OpStrNEq, $3);}
  307. ;
  308. BitwiseANDExpr:
  309. EqualityExpr
  310. | BitwiseANDExpr '&amp;' EqualityExpr { $$ = new BitOperNode($1, OpBitAnd, $3); }
  311. ;
  312. BitwiseXORExpr:
  313. BitwiseANDExpr
  314. | BitwiseXORExpr '^' BitwiseANDExpr { $$ = new BitOperNode($1, OpBitXOr, $3); }
  315. ;
  316. BitwiseORExpr:
  317. BitwiseXORExpr
  318. | BitwiseORExpr '|' BitwiseXORExpr { $$ = new BitOperNode($1, OpBitOr, $3); }
  319. ;
  320. LogicalANDExpr:
  321. BitwiseORExpr
  322. | LogicalANDExpr AND BitwiseORExpr
  323. { $$ = new BinaryLogicalNode($1, OpAnd, $3); }
  324. ;
  325. LogicalORExpr:
  326. LogicalANDExpr
  327. | LogicalORExpr OR LogicalANDExpr
  328. { $$ = new BinaryLogicalNode($1, OpOr, $3); }
  329. ;
  330. ConditionalExpr:
  331. LogicalORExpr
  332. | LogicalORExpr '?' AssignmentExpr ':' AssignmentExpr
  333. { $$ = new ConditionalNode($1, $3, $5); }
  334. ;
  335. AssignmentExpr:
  336. ConditionalExpr
  337. | LeftHandSideExpr AssignmentOperator AssignmentExpr
  338. { $$ = new AssignNode($1, $2, $3);}
  339. ;
  340. AssignmentOperator:
  341. '=' { $$ = OpEqual; }
  342. | PLUSEQUAL { $$ = OpPlusEq; }
  343. | MINUSEQUAL { $$ = OpMinusEq; }
  344. | MULTEQUAL { $$ = OpMultEq; }
  345. | DIVEQUAL { $$ = OpDivEq; }
  346. | LSHIFTEQUAL { $$ = OpLShift; }
  347. | RSHIFTEQUAL { $$ = OpRShift; }
  348. | URSHIFTEQUAL { $$ = OpURShift; }
  349. | ANDEQUAL { $$ = OpAndEq; }
  350. | XOREQUAL { $$ = OpXOrEq; }
  351. | OREQUAL { $$ = OpOrEq; }
  352. | MODEQUAL { $$ = OpModEq; }
  353. ;
  354. Expr:
  355. AssignmentExpr
  356. | Expr ',' AssignmentExpr { $$ = new CommaNode($1, $3); }
  357. ;
  358. Statement:
  359. Block
  360. | VariableStatement
  361. | EmptyStatement
  362. | ExprStatement
  363. | IfStatement
  364. | IterationStatement
  365. | ContinueStatement
  366. | BreakStatement
  367. | ReturnStatement
  368. | WithStatement
  369. | SwitchStatement
  370. | LabelledStatement
  371. | ThrowStatement
  372. | TryStatement
  373. ;
  374. Block:
  375. '{' '}' { $$ = new BlockNode(0); DBG($$, @2, @2); }
  376. | '{' SourceElements '}' { $$ = new BlockNode($2); DBG($$, @3, @3); }
  377. ;
  378. StatementList:
  379. Statement { $$ = new StatListNode($1); }
  380. | StatementList Statement { $$ = new StatListNode($1, $2); }
  381. ;
  382. VariableStatement:
  383. VAR VariableDeclarationList ';' { $$ = new VarStatementNode($2);
  384. DBG($$, @1, @3); }
  385. | VAR VariableDeclarationList error { if (automatic()) {
  386. $$ = new VarStatementNode($2);
  387. DBG($$, @1, @2);
  388. } else {
  389. YYABORT;
  390. }
  391. }
  392. ;
  393. VariableDeclarationList:
  394. VariableDeclaration { $$ = new VarDeclListNode($1); }
  395. | VariableDeclarationList ',' VariableDeclaration
  396. { $$ = new VarDeclListNode($1, $3); }
  397. ;
  398. VariableDeclaration:
  399. IDENT { $$ = new VarDeclNode(*$1, 0); }
  400. | IDENT Initializer { $$ = new VarDeclNode(*$1, $2); }
  401. ;
  402. Initializer:
  403. '=' AssignmentExpr { $$ = new AssignExprNode($2); }
  404. ;
  405. EmptyStatement:
  406. ';' { $$ = new EmptyStatementNode(); }
  407. ;
  408. ExprStatement:
  409. Expr ';' { $$ = new ExprStatementNode($1);
  410. DBG($$, @1, @2); }
  411. | Expr error { if (automatic()) {
  412. $$ = new ExprStatementNode($1);
  413. DBG($$, @1, @1);
  414. } else
  415. YYABORT; }
  416. ;
  417. IfStatement: /* shift/reduce conflict due to dangling else */
  418. IF '(' Expr ')' Statement { $$ = new IfNode($3,$5,0);DBG($$,@1,@4); }
  419. | IF '(' Expr ')' Statement ELSE Statement
  420. { $$ = new IfNode($3,$5,$7);DBG($$,@1,@4); }
  421. ;
  422. IterationStatement:
  423. DO Statement WHILE '(' Expr ')' { $$=new DoWhileNode($2,$5);DBG($$,@1,@3);}
  424. | WHILE '(' Expr ')' Statement { $$ = new WhileNode($3,$5);DBG($$,@1,@4); }
  425. | FOR '(' ExprOpt ';' ExprOpt ';' ExprOpt ')'
  426. Statement { $$ = new ForNode($3,$5,$7,$9);
  427. DBG($$,@1,@8); }
  428. | FOR '(' VAR VariableDeclarationList ';' ExprOpt ';' ExprOpt ')'
  429. Statement { $$ = new ForNode($4,$6,$8,$10);
  430. DBG($$,@1,@9); }
  431. | FOR '(' LeftHandSideExpr IN Expr ')'
  432. Statement { $$ = new ForInNode($3, $5, $7);
  433. DBG($$,@1,@6); }
  434. | FOR '(' VAR IDENT IN Expr ')'
  435. Statement { $$ = new ForInNode(*$4,0,$6,$8);
  436. DBG($$,@1,@7); }
  437. | FOR '(' VAR IDENT Initializer IN Expr ')'
  438. Statement { $$ = new ForInNode(*$4,$5,$7,$9);
  439. DBG($$,@1,@8); }
  440. ;
  441. ExprOpt:
  442. /* nothing */ { $$ = 0; }
  443. | Expr
  444. ;
  445. ContinueStatement:
  446. CONTINUE ';' { $$ = new ContinueNode(); DBG($$,@1,@2); }
  447. | CONTINUE error { if (automatic()) {
  448. $$ = new ContinueNode(); DBG($$,@1,@1);
  449. } else
  450. YYABORT; }
  451. | CONTINUE IDENT ';' { $$ = new ContinueNode(*$2); DBG($$,@1,@3); }
  452. | CONTINUE IDENT error { if (automatic()) {
  453. $$ = new ContinueNode(*$2);DBG($$,@1,@2);
  454. } else
  455. YYABORT; }
  456. ;
  457. BreakStatement:
  458. BREAK ';' { $$ = new BreakNode();DBG($$,@1,@2); }
  459. | BREAK error { if (automatic()) {
  460. $$ = new BreakNode(); DBG($$,@1,@1);
  461. } else
  462. YYABORT; }
  463. | BREAK IDENT ';' { $$ = new BreakNode(*$2); DBG($$,@1,@3); }
  464. | BREAK IDENT error { if (automatic()) {
  465. $$ = new BreakNode(*$2); DBG($$,@1,@2);
  466. } else
  467. YYABORT;
  468. }
  469. ;
  470. ReturnStatement:
  471. RETURN ';' { $$ = new ReturnNode(0); DBG($$,@1,@2); }
  472. | RETURN error { if (automatic()) {
  473. $$ = new ReturnNode(0); DBG($$,@1,@1);
  474. } else
  475. YYABORT; }
  476. | RETURN Expr ';' { $$ = new ReturnNode($2); DBG($$,@1,@3); }
  477. | RETURN Expr error { if (automatic()) {
  478. $$ = new ReturnNode($2); DBG($$,@1,@2);
  479. } else
  480. YYABORT; }
  481. ;
  482. WithStatement:
  483. WITH '(' Expr ')' Statement { $$ = new WithNode($3,$5);
  484. DBG($$, @1, @4); }
  485. ;
  486. SwitchStatement:
  487. SWITCH '(' Expr ')' CaseBlock { $$ = new SwitchNode($3, $5);
  488. DBG($$, @1, @4); }
  489. ;
  490. CaseBlock:
  491. '{' CaseClausesOpt '}' { $$ = new CaseBlockNode($2, 0, 0); }
  492. | '{' CaseClausesOpt DefaultClause CaseClausesOpt '}'
  493. { $$ = new CaseBlockNode($2, $3, $4); }
  494. ;
  495. CaseClausesOpt:
  496. /* nothing */ { $$ = 0; }
  497. | CaseClauses
  498. ;
  499. CaseClauses:
  500. CaseClause { $$ = new ClauseListNode($1); }
  501. | CaseClauses CaseClause { $$ = new ClauseListNode($1, $2); }
  502. ;
  503. CaseClause:
  504. CASE Expr ':' { $$ = new CaseClauseNode($2); }
  505. | CASE Expr ':' StatementList { $$ = new CaseClauseNode($2, $4); }
  506. ;
  507. DefaultClause:
  508. DEFAULT ':' { $$ = new CaseClauseNode(0); }
  509. | DEFAULT ':' StatementList { $$ = new CaseClauseNode(0, $3); }
  510. ;
  511. LabelledStatement:
  512. IDENT ':' Statement { $3-&gt;pushLabel(*$1);
  513. $$ = new LabelNode(*$1, $3); }
  514. ;
  515. ThrowStatement:
  516. THROW Expr ';' { $$ = new ThrowNode($2); DBG($$,@1,@3); }
  517. | THROW Expr error { if (automatic()) { $$ = new ThrowNode($2); DBG($$,@1,@2); } else YYABORT; }
  518. ;
  519. TryStatement:
  520. TRY Block Catch { $$ = new TryNode($2, $3); DBG($$,@1,@2); }
  521. | TRY Block Finally { $$ = new TryNode($2, $3); DBG($$,@1,@2); }
  522. | TRY Block Catch Finally { $$ = new TryNode($2, $3, $4); DBG($$,@1,@2); }
  523. ;
  524. Catch:
  525. CATCH '(' IDENT ')' Block { $$ = new CatchNode(*$3, $5); }
  526. ;
  527. Finally:
  528. FINALLY Block { $$ = new FinallyNode($2); }
  529. ;
  530. FunctionDeclaration:
  531. FUNCTION IDENT '(' ')' FunctionBody { $$ = new FuncDeclNode(*$2, $5); }
  532. | FUNCTION IDENT '(' FormalParameterList ')' FunctionBody
  533. { $$ = new FuncDeclNode(*$2, $4, $6); }
  534. FunctionExpr:
  535. FUNCTION '(' ')' FunctionBody { $$ = new FuncExprNode($4); }
  536. | FUNCTION '(' FormalParameterList ')' FunctionBody
  537. { $$ = new FuncExprNode($3, $5); }
  538. ;
  539. FormalParameterList:
  540. IDENT { $$ = new ParameterNode(*$1); }
  541. | FormalParameterList ',' IDENT { $$ = new ParameterNode($1, *$3); }
  542. ;
  543. FunctionBody:
  544. '{' '}' /* TODO: spec ??? */ { $$ = new FunctionBodyNode(0);
  545. DBG($$, @1, @2);}
  546. | '{' SourceElements '}' { $$ = new FunctionBodyNode($2);
  547. DBG($$, @1, @3);}
  548. ;
  549. Program:
  550. /* nothing, empty script */ { $$ = new ProgramNode(0);
  551. Parser::accept($$); }
  552. | SourceElements { $$ = new ProgramNode($1);
  553. Parser::accept($$); }
  554. ;
  555. SourceElements:
  556. SourceElement { $$ = new SourceElementsNode($1); }
  557. | SourceElements SourceElement { $$ = new SourceElementsNode($1, $2); }
  558. ;
  559. SourceElement:
  560. Statement { $$ = $1; }
  561. | FunctionDeclaration { $$ = $1; }
  562. ;
  563. %%
  564. int yyerror (const char * /* s */) /* Called by yyparse on error */
  565. {
  566. // fprintf(stderr, &quot;ERROR: %s at line %d\n&quot;,
  567. // s, KJS::Lexer::curr()-&gt;lineNo());
  568. return 1;
  569. }
  570. /* may we automatically insert a semicolon ? */
  571. bool automatic()
  572. {
  573. if (yychar == '}' || yychar == 0)
  574. return true;
  575. else if (Lexer::curr()-&gt;prevTerminator())
  576. return true;
  577. return false;
  578. }
  579. </pre>
  580. <hr />
  581. </body></html>