PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Happy | 676 lines | 588 code | 88 blank | 0 comment | 0 complexity | e2e68db69ee039a38675371681b8b40a 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. }
  95. %start Program
  96. /* expect a shift/reduce conflict from the &quot;dangling else&quot; problem
  97. when using bison the warning can be supressed */
  98. // %expect 1
  99. /* literals */
  100. %token NULLTOKEN TRUETOKEN FALSETOKEN
  101. %token STRING NUMBER
  102. /* keywords */
  103. %token BREAK CASE DEFAULT FOR NEW VAR CONTINUE
  104. %token FUNCTION RETURN VOID DELETE
  105. %token IF THIS DO WHILE ELSE IN INSTANCEOF TYPEOF
  106. %token SWITCH WITH RESERVED
  107. %token THROW TRY CATCH FINALLY
  108. /* punctuators */
  109. %token EQEQ NE /* == and != */
  110. %token STREQ STRNEQ /* === and !== */
  111. %token LE GE /* &lt; and &gt; */
  112. %token OR AND /* || and &amp;&amp; */
  113. %token PLUSPLUS MINUSMINUS /* ++ and -- */
  114. %token LSHIFT /* &lt;&lt; */
  115. %token RSHIFT URSHIFT /* &gt;&gt; and &gt;&gt;&gt; */
  116. %token PLUSEQUAL MINUSEQUAL /* += and -= */
  117. %token MULTEQUAL DIVEQUAL /* *= and /= */
  118. %token LSHIFTEQUAL /* &lt;&lt;= */
  119. %token RSHIFTEQUAL URSHIFTEQUAL /* &gt;&gt;= and &gt;&gt;&gt;= */
  120. %token ANDEQUAL MODEQUAL /* &amp;= and %= */
  121. %token XOREQUAL OREQUAL /* ^= and |= */
  122. /* terminal types */
  123. %token &lt;dval&gt; NUMBER
  124. %token &lt;ustr&gt; STRING
  125. %token &lt;ident&gt; IDENT
  126. /* automatically inserted semicolon */
  127. %token AUTOPLUSPLUS AUTOMINUSMINUS
  128. /* non-terminal types */
  129. %type &lt;node&gt; Literal PrimaryExpr Expr MemberExpr FunctionExpr NewExpr CallExpr
  130. %type &lt;node&gt; ArrayLiteral
  131. %type &lt;node&gt; LeftHandSideExpr PostfixExpr UnaryExpr
  132. %type &lt;node&gt; MultiplicativeExpr AdditiveExpr
  133. %type &lt;node&gt; ShiftExpr RelationalExpr EqualityExpr
  134. %type &lt;node&gt; BitwiseANDExpr BitwiseXORExpr BitwiseORExpr
  135. %type &lt;node&gt; LogicalANDExpr LogicalORExpr
  136. %type &lt;node&gt; ConditionalExpr AssignmentExpr
  137. %type &lt;node&gt; ExprOpt
  138. %type &lt;node&gt; CallExpr
  139. %type &lt;node&gt; Catch Finally
  140. %type &lt;stat&gt; Statement Block
  141. %type &lt;stat&gt; VariableStatement EmptyStatement ExprStatement
  142. %type &lt;stat&gt; IfStatement IterationStatement ContinueStatement
  143. %type &lt;stat&gt; BreakStatement ReturnStatement WithStatement
  144. %type &lt;stat&gt; SwitchStatement LabelledStatement
  145. %type &lt;stat&gt; ThrowStatement TryStatement
  146. %type &lt;stat&gt; SourceElement
  147. %type &lt;slist&gt; StatementList
  148. %type &lt;init&gt; Initializer
  149. %type &lt;func&gt; FunctionDeclaration
  150. %type &lt;body&gt; FunctionBody
  151. %type &lt;srcs&gt; SourceElements
  152. %type &lt;param&gt; FormalParameterList
  153. %type &lt;op&gt; AssignmentOperator
  154. %type &lt;prog&gt; Program
  155. %type &lt;args&gt; Arguments
  156. %type &lt;alist&gt; ArgumentList
  157. %type &lt;vlist&gt; VariableDeclarationList
  158. %type &lt;decl&gt; VariableDeclaration
  159. %type &lt;cblk&gt; CaseBlock
  160. %type &lt;ccl&gt; CaseClause DefaultClause
  161. %type &lt;clist&gt; CaseClauses CaseClausesOpt
  162. %type &lt;ival&gt; Elision ElisionOpt
  163. %type &lt;elm&gt; ElementList
  164. %type &lt;plist&gt; PropertyNameAndValueList
  165. %type &lt;pnode&gt; PropertyName
  166. %%
  167. Literal:
  168. NULLTOKEN { $$ = new NullNode(); }
  169. | TRUETOKEN { $$ = new BooleanNode(true); }
  170. | FALSETOKEN { $$ = new BooleanNode(false); }
  171. | NUMBER { $$ = new NumberNode($1); }
  172. | STRING { $$ = new StringNode($1); delete $1; }
  173. | '/' /* a RegExp ? */ { Lexer *l = Lexer::curr();
  174. if (!l-&gt;scanRegExp()) YYABORT;
  175. $$ = new RegExpNode(l-&gt;pattern,l-&gt;flags);}
  176. | DIVEQUAL /* a RegExp starting with /= ! */
  177. { Lexer *l = Lexer::curr();
  178. if (!l-&gt;scanRegExp()) YYABORT;
  179. $$ = new RegExpNode(UString('=')+l-&gt;pattern,l-&gt;flags);}
  180. ;
  181. PrimaryExpr:
  182. THIS { $$ = new ThisNode(); }
  183. | IDENT { $$ = new ResolveNode(*$1);
  184. delete $1; }
  185. | Literal
  186. | ArrayLiteral
  187. | '(' Expr ')' { $$ = new GroupNode($2); }
  188. | '{' '}' { $$ = new ObjectLiteralNode(0L); }
  189. | '{' PropertyNameAndValueList '}' { $$ = new ObjectLiteralNode($2); }
  190. ;
  191. ArrayLiteral:
  192. '[' ElisionOpt ']' { $$ = new ArrayNode($2); }
  193. | '[' ElementList ']' { $$ = new ArrayNode($2); }
  194. | '[' ElementList ',' ElisionOpt ']' { $$ = new ArrayNode($4, $2); }
  195. ;
  196. ElementList:
  197. ElisionOpt AssignmentExpr { $$ = new ElementNode($1, $2); }
  198. | ElementList ',' ElisionOpt AssignmentExpr
  199. { $$ = new ElementNode($1, $3, $4); }
  200. ;
  201. ElisionOpt:
  202. /* nothing */ { $$ = 0; }
  203. | Elision
  204. ;
  205. Elision:
  206. ',' { $$ = 1; }
  207. | Elision ',' { $$ = $1 + 1; }
  208. ;
  209. PropertyNameAndValueList:
  210. PropertyName ':' AssignmentExpr { $$ = new PropertyValueNode($1, $3); }
  211. | PropertyNameAndValueList ',' PropertyName ':' AssignmentExpr
  212. { $$ = new PropertyValueNode($3, $5, $1); }
  213. ;
  214. PropertyName:
  215. IDENT { $$ = new PropertyNode(*$1); delete $1; }
  216. | STRING { $$ = new PropertyNode(Identifier(*$1)); delete $1; }
  217. | NUMBER { $$ = new PropertyNode($1); }
  218. ;
  219. MemberExpr:
  220. PrimaryExpr
  221. | FunctionExpr
  222. | MemberExpr '[' Expr ']' { $$ = new AccessorNode1($1, $3); }
  223. | MemberExpr '.' IDENT { $$ = new AccessorNode2($1, *$3); delete $3; }
  224. | NEW MemberExpr Arguments { $$ = new NewExprNode($2, $3); }
  225. ;
  226. NewExpr:
  227. MemberExpr
  228. | NEW NewExpr { $$ = new NewExprNode($2); }
  229. ;
  230. CallExpr:
  231. MemberExpr Arguments { $$ = new FunctionCallNode($1, $2); }
  232. | CallExpr Arguments { $$ = new FunctionCallNode($1, $2); }
  233. | CallExpr '[' Expr ']' { $$ = new AccessorNode1($1, $3); }
  234. | CallExpr '.' IDENT { $$ = new AccessorNode2($1, *$3);
  235. delete $3; }
  236. ;
  237. Arguments:
  238. '(' ')' { $$ = new ArgumentsNode(0L); }
  239. | '(' ArgumentList ')' { $$ = new ArgumentsNode($2); }
  240. ;
  241. ArgumentList:
  242. AssignmentExpr { $$ = new ArgumentListNode($1); }
  243. | ArgumentList ',' AssignmentExpr { $$ = new ArgumentListNode($1, $3); }
  244. ;
  245. LeftHandSideExpr:
  246. NewExpr
  247. | CallExpr
  248. ;
  249. PostfixExpr: /* TODO: no line terminator here */
  250. LeftHandSideExpr
  251. | LeftHandSideExpr PLUSPLUS { $$ = new PostfixNode($1, OpPlusPlus); }
  252. | LeftHandSideExpr MINUSMINUS { $$ = new PostfixNode($1, OpMinusMinus); }
  253. ;
  254. UnaryExpr:
  255. PostfixExpr
  256. | DELETE UnaryExpr { $$ = new DeleteNode($2); }
  257. | VOID UnaryExpr { $$ = new VoidNode($2); }
  258. | TYPEOF UnaryExpr { $$ = new TypeOfNode($2); }
  259. | PLUSPLUS UnaryExpr { $$ = new PrefixNode(OpPlusPlus, $2); }
  260. | AUTOPLUSPLUS UnaryExpr { $$ = new PrefixNode(OpPlusPlus, $2); }
  261. | MINUSMINUS UnaryExpr { $$ = new PrefixNode(OpMinusMinus, $2); }
  262. | AUTOMINUSMINUS UnaryExpr { $$ = new PrefixNode(OpMinusMinus, $2); }
  263. | '+' UnaryExpr { $$ = new UnaryPlusNode($2); }
  264. | '-' UnaryExpr { $$ = new NegateNode($2); }
  265. | '~' UnaryExpr { $$ = new BitwiseNotNode($2); }
  266. | '!' UnaryExpr { $$ = new LogicalNotNode($2); }
  267. ;
  268. MultiplicativeExpr:
  269. UnaryExpr
  270. | MultiplicativeExpr '*' UnaryExpr { $$ = new MultNode($1, $3, '*'); }
  271. | MultiplicativeExpr '/' UnaryExpr { $$ = new MultNode($1, $3, '/'); }
  272. | MultiplicativeExpr '%' UnaryExpr { $$ = new MultNode($1,$3,'%'); }
  273. ;
  274. AdditiveExpr:
  275. MultiplicativeExpr
  276. | AdditiveExpr '+' MultiplicativeExpr { $$ = new AddNode($1, $3, '+'); }
  277. | AdditiveExpr '-' MultiplicativeExpr { $$ = new AddNode($1, $3, '-'); }
  278. ;
  279. ShiftExpr:
  280. AdditiveExpr
  281. | ShiftExpr LSHIFT AdditiveExpr { $$ = new ShiftNode($1, OpLShift, $3); }
  282. | ShiftExpr RSHIFT AdditiveExpr { $$ = new ShiftNode($1, OpRShift, $3); }
  283. | ShiftExpr URSHIFT AdditiveExpr { $$ = new ShiftNode($1, OpURShift, $3); }
  284. ;
  285. RelationalExpr:
  286. ShiftExpr
  287. | RelationalExpr '&lt;' ShiftExpr
  288. { $$ = new RelationalNode($1, OpLess, $3); }
  289. | RelationalExpr '&gt;' ShiftExpr
  290. { $$ = new RelationalNode($1, OpGreater, $3); }
  291. | RelationalExpr LE ShiftExpr
  292. { $$ = new RelationalNode($1, OpLessEq, $3); }
  293. | RelationalExpr GE ShiftExpr
  294. { $$ = new RelationalNode($1, OpGreaterEq, $3); }
  295. | RelationalExpr INSTANCEOF ShiftExpr
  296. { $$ = new RelationalNode($1, OpInstanceOf, $3); }
  297. | RelationalExpr IN ShiftExpr
  298. { $$ = new RelationalNode($1, OpIn, $3); }
  299. ;
  300. EqualityExpr:
  301. RelationalExpr
  302. | EqualityExpr EQEQ RelationalExpr { $$ = new EqualNode($1, OpEqEq, $3); }
  303. | EqualityExpr NE RelationalExpr { $$ = new EqualNode($1, OpNotEq, $3); }
  304. | EqualityExpr STREQ RelationalExpr { $$ = new EqualNode($1, OpStrEq, $3); }
  305. | EqualityExpr STRNEQ RelationalExpr { $$ = new EqualNode($1, OpStrNEq, $3);}
  306. ;
  307. BitwiseANDExpr:
  308. EqualityExpr
  309. | BitwiseANDExpr '&amp;' EqualityExpr { $$ = new BitOperNode($1, OpBitAnd, $3); }
  310. ;
  311. BitwiseXORExpr:
  312. BitwiseANDExpr
  313. | BitwiseXORExpr '^' EqualityExpr { $$ = new BitOperNode($1, OpBitXOr, $3); }
  314. ;
  315. BitwiseORExpr:
  316. BitwiseXORExpr
  317. | BitwiseORExpr '|' EqualityExpr { $$ = new BitOperNode($1, OpBitOr, $3); }
  318. ;
  319. LogicalANDExpr:
  320. BitwiseORExpr
  321. | LogicalANDExpr AND BitwiseORExpr
  322. { $$ = new BinaryLogicalNode($1, OpAnd, $3); }
  323. ;
  324. LogicalORExpr:
  325. LogicalANDExpr
  326. | LogicalORExpr OR LogicalANDExpr
  327. { $$ = new BinaryLogicalNode($1, OpOr, $3); }
  328. ;
  329. ConditionalExpr:
  330. LogicalORExpr
  331. | LogicalORExpr '?' AssignmentExpr ':' AssignmentExpr
  332. { $$ = new ConditionalNode($1, $3, $5); }
  333. ;
  334. AssignmentExpr:
  335. ConditionalExpr
  336. | LeftHandSideExpr AssignmentOperator AssignmentExpr
  337. { $$ = new AssignNode($1, $2, $3);}
  338. ;
  339. AssignmentOperator:
  340. '=' { $$ = OpEqual; }
  341. | PLUSEQUAL { $$ = OpPlusEq; }
  342. | MINUSEQUAL { $$ = OpMinusEq; }
  343. | MULTEQUAL { $$ = OpMultEq; }
  344. | DIVEQUAL { $$ = OpDivEq; }
  345. | LSHIFTEQUAL { $$ = OpLShift; }
  346. | RSHIFTEQUAL { $$ = OpRShift; }
  347. | URSHIFTEQUAL { $$ = OpURShift; }
  348. | ANDEQUAL { $$ = OpAndEq; }
  349. | XOREQUAL { $$ = OpXOrEq; }
  350. | OREQUAL { $$ = OpOrEq; }
  351. | MODEQUAL { $$ = OpModEq; }
  352. ;
  353. Expr:
  354. AssignmentExpr
  355. | Expr ',' AssignmentExpr { $$ = new CommaNode($1, $3); }
  356. ;
  357. Statement:
  358. Block
  359. | VariableStatement
  360. | EmptyStatement
  361. | ExprStatement
  362. | IfStatement
  363. | IterationStatement
  364. | ContinueStatement
  365. | BreakStatement
  366. | ReturnStatement
  367. | WithStatement
  368. | SwitchStatement
  369. | LabelledStatement
  370. | ThrowStatement
  371. | TryStatement
  372. ;
  373. Block:
  374. '{' '}' { $$ = new BlockNode(0L); DBG($$, @2, @2); }
  375. | '{' SourceElements '}' { $$ = new BlockNode($2); DBG($$, @3, @3); }
  376. ;
  377. StatementList:
  378. Statement { $$ = new StatListNode($1); }
  379. | StatementList Statement { $$ = new StatListNode($1, $2); }
  380. ;
  381. VariableStatement:
  382. VAR VariableDeclarationList ';' { $$ = new VarStatementNode($2);
  383. DBG($$, @1, @3); }
  384. | VAR VariableDeclarationList error { if (automatic()) {
  385. $$ = new VarStatementNode($2);
  386. DBG($$, @1, @2);
  387. } else {
  388. YYABORT;
  389. }
  390. }
  391. ;
  392. VariableDeclarationList:
  393. VariableDeclaration { $$ = new VarDeclListNode($1); }
  394. | VariableDeclarationList ',' VariableDeclaration
  395. { $$ = new VarDeclListNode($1, $3); }
  396. ;
  397. VariableDeclaration:
  398. IDENT { $$ = new VarDeclNode(*$1, 0); delete $1; }
  399. | IDENT Initializer { $$ = new VarDeclNode(*$1, $2); delete $1; }
  400. ;
  401. Initializer:
  402. '=' AssignmentExpr { $$ = new AssignExprNode($2); }
  403. ;
  404. EmptyStatement:
  405. ';' { $$ = new EmptyStatementNode(); }
  406. ;
  407. ExprStatement:
  408. Expr ';' { $$ = new ExprStatementNode($1);
  409. DBG($$, @1, @2); }
  410. | Expr error { if (automatic()) {
  411. $$ = new ExprStatementNode($1);
  412. DBG($$, @1, @1);
  413. } else
  414. YYABORT; }
  415. ;
  416. IfStatement: /* shift/reduce conflict due to dangling else */
  417. IF '(' Expr ')' Statement { $$ = new IfNode($3,$5,0L);DBG($$,@1,@4); }
  418. | IF '(' Expr ')' Statement ELSE Statement
  419. { $$ = new IfNode($3,$5,$7);DBG($$,@1,@4); }
  420. ;
  421. IterationStatement:
  422. DO Statement WHILE '(' Expr ')' { $$=new DoWhileNode($2,$5);DBG($$,@1,@3);}
  423. | WHILE '(' Expr ')' Statement { $$ = new WhileNode($3,$5);DBG($$,@1,@4); }
  424. | FOR '(' ExprOpt ';' ExprOpt ';' ExprOpt ')'
  425. Statement { $$ = new ForNode($3,$5,$7,$9);
  426. DBG($$,@1,@8); }
  427. | FOR '(' VAR VariableDeclarationList ';' ExprOpt ';' ExprOpt ')'
  428. Statement { $$ = new ForNode($4,$6,$8,$10);
  429. DBG($$,@1,@9); }
  430. | FOR '(' LeftHandSideExpr IN Expr ')'
  431. Statement { $$ = new ForInNode($3, $5, $7);
  432. DBG($$,@1,@6); }
  433. | FOR '(' VAR IDENT IN Expr ')'
  434. Statement { $$ = new ForInNode(*$4,0L,$6,$8);
  435. DBG($$,@1,@7);
  436. delete $4; }
  437. | FOR '(' VAR IDENT Initializer IN Expr ')'
  438. Statement { $$ = new ForInNode(*$4,$5,$7,$9);
  439. DBG($$,@1,@8);
  440. delete $4; }
  441. ;
  442. ExprOpt:
  443. /* nothing */ { $$ = 0L; }
  444. | Expr
  445. ;
  446. ContinueStatement:
  447. CONTINUE ';' { $$ = new ContinueNode(); DBG($$,@1,@2); }
  448. | CONTINUE error { if (automatic()) {
  449. $$ = new ContinueNode(); DBG($$,@1,@2);
  450. } else
  451. YYABORT; }
  452. | CONTINUE IDENT ';' { $$ = new ContinueNode(*$2); DBG($$,@1,@3);
  453. delete $2; }
  454. | CONTINUE IDENT error { if (automatic()) {
  455. $$ = new ContinueNode(*$2);DBG($$,@1,@2);
  456. delete $2;
  457. } else
  458. YYABORT; }
  459. ;
  460. BreakStatement:
  461. BREAK ';' { $$ = new BreakNode();DBG($$,@1,@2); }
  462. | BREAK error { if (automatic()) {
  463. $$ = new BreakNode(); DBG($$,@1,@1);
  464. } else
  465. YYABORT; }
  466. | BREAK IDENT ';' { $$ = new BreakNode(*$2); DBG($$,@1,@3);
  467. delete $2; }
  468. | BREAK IDENT error { if (automatic()) {
  469. $$ = new BreakNode(*$2); DBG($$,@1,@2);
  470. delete $2;
  471. } else
  472. YYABORT;
  473. }
  474. ;
  475. ReturnStatement:
  476. RETURN ';' { $$ = new ReturnNode(0L); DBG($$,@1,@2); }
  477. | RETURN error { if (automatic()) {
  478. $$ = new ReturnNode(0L); DBG($$,@1,@1);
  479. } else
  480. YYABORT; }
  481. | RETURN Expr ';' { $$ = new ReturnNode($2); }
  482. | RETURN Expr error { if (automatic())
  483. $$ = new ReturnNode($2);
  484. else
  485. YYABORT; }
  486. ;
  487. WithStatement:
  488. WITH '(' Expr ')' Statement { $$ = new WithNode($3,$5);
  489. DBG($$, @1, @4); }
  490. ;
  491. SwitchStatement:
  492. SWITCH '(' Expr ')' CaseBlock { $$ = new SwitchNode($3, $5);
  493. DBG($$, @1, @4); }
  494. ;
  495. CaseBlock:
  496. '{' CaseClausesOpt '}' { $$ = new CaseBlockNode($2, 0L, 0L); }
  497. | '{' CaseClausesOpt DefaultClause CaseClausesOpt '}'
  498. { $$ = new CaseBlockNode($2, $3, $4); }
  499. ;
  500. CaseClausesOpt:
  501. /* nothing */ { $$ = 0L; }
  502. | CaseClauses
  503. ;
  504. CaseClauses:
  505. CaseClause { $$ = new ClauseListNode($1); }
  506. | CaseClauses CaseClause { $$ = new ClauseListNode($1, $2); }
  507. ;
  508. CaseClause:
  509. CASE Expr ':' { $$ = new CaseClauseNode($2, 0L); }
  510. | CASE Expr ':' StatementList { $$ = new CaseClauseNode($2, $4); }
  511. ;
  512. DefaultClause:
  513. DEFAULT ':' { $$ = new CaseClauseNode(0L, 0L);; }
  514. | DEFAULT ':' StatementList { $$ = new CaseClauseNode(0L, $3); }
  515. ;
  516. LabelledStatement:
  517. IDENT ':' Statement { $3-&gt;pushLabel(*$1);
  518. $$ = new LabelNode(*$1, $3);
  519. delete $1; }
  520. ;
  521. ThrowStatement:
  522. THROW Expr ';' { $$ = new ThrowNode($2); }
  523. ;
  524. TryStatement:
  525. TRY Block Catch { $$ = new TryNode($2, $3); }
  526. | TRY Block Finally { $$ = new TryNode($2, 0L, $3); }
  527. | TRY Block Catch Finally { $$ = new TryNode($2, $3, $4); }
  528. ;
  529. Catch:
  530. CATCH '(' IDENT ')' Block { $$ = new CatchNode(*$3, $5); delete $3; }
  531. ;
  532. Finally:
  533. FINALLY Block { $$ = new FinallyNode($2); }
  534. ;
  535. FunctionDeclaration:
  536. FUNCTION IDENT '(' ')' FunctionBody { $$ = new FuncDeclNode(*$2, 0L, $5);
  537. delete $2; }
  538. | FUNCTION IDENT '(' FormalParameterList ')' FunctionBody
  539. { $$ = new FuncDeclNode(*$2, $4, $6);
  540. delete $2; }
  541. FunctionExpr:
  542. FUNCTION '(' ')' FunctionBody { $$ = new FuncExprNode(0L, $4); }
  543. | FUNCTION '(' FormalParameterList ')' FunctionBody
  544. { $$ = new FuncExprNode($3, $5); }
  545. ;
  546. FormalParameterList:
  547. IDENT { $$ = new ParameterNode(*$1); delete $1; }
  548. | FormalParameterList ',' IDENT { $$ = new ParameterNode($1, *$3); delete $3; }
  549. ;
  550. FunctionBody:
  551. '{' '}' /* TODO: spec ??? */ { $$ = new FunctionBodyNode(0L);
  552. DBG($$, @1, @2);}
  553. | '{' SourceElements '}' { $$ = new FunctionBodyNode($2);
  554. DBG($$, @1, @3);}
  555. ;
  556. Program:
  557. /* nothing, empty script */ { $$ = new ProgramNode(0L);
  558. Parser::progNode = $$; }
  559. | SourceElements { $$ = new ProgramNode($1);
  560. Parser::progNode = $$; }
  561. ;
  562. SourceElements:
  563. SourceElement { $$ = new SourceElementsNode($1); }
  564. | SourceElements SourceElement { $$ = new SourceElementsNode($1, $2); }
  565. ;
  566. SourceElement:
  567. Statement { $$ = $1; }
  568. | FunctionDeclaration { $$ = $1; }
  569. ;
  570. %%
  571. int yyerror (const char *) /* Called by yyparse on error */
  572. {
  573. // fprintf(stderr, &quot;ERROR: %s at line %d\n&quot;,
  574. // s, KJScript::lexer()-&gt;lineNo());
  575. return 1;
  576. }
  577. /* may we automatically insert a semicolon ? */
  578. bool automatic()
  579. {
  580. if (yychar == '}' || yychar == 0)
  581. return true;
  582. else if (Lexer::curr()-&gt;prevTerminator())
  583. return true;
  584. return false;
  585. }
  586. </pre>
  587. <hr />
  588. </body></html>