PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/compiler/parser.y

http://parrotjs.googlecode.com/
Happy | 1617 lines | 1476 code | 141 blank | 0 comment | 0 complexity | f8abe6c7c6370205ba450cfc7a92c2e3 MD5 | raw file
  1. %pure-parser
  2. %locations
  3. %defines
  4. %error-verbose
  5. %parse-param { Params* params }
  6. %lex-param { void* scanner }
  7. %name-prefix="pjs_yy"
  8. %{
  9. #include "params.h"
  10. #include "mempool.h"
  11. #include "str_escaping.h"
  12. #define YYERROR_VERBOSE 1
  13. #define yyparams ((Params*)params)
  14. #define MP (yyparams->mempool)
  15. #include <stdio.h>
  16. #include <assert.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include "nodes.h"
  20. #include "list.h"
  21. #define scanner params->scanner
  22. #define DBG(txt) fprintf(stderr, " @@@ %s", (txt))
  23. /*#define DBG */
  24. #define YYDEBUG 1
  25. #define yymatch_len (yyparams->match_len)
  26. static void err_msg(Params* p, const char* s);
  27. static int automatic_test(Params* params, int yychar);
  28. %}
  29. %union NODE {
  30. double num;
  31. char* str;
  32. Node expr;
  33. PjsList exprList;
  34. PjsList nameValueList;
  35. int tokType;
  36. Node stmt;
  37. PjsList stmtList;
  38. NameValue nameValue;
  39. PjsList params;
  40. CaseClause caseClause;
  41. PjsList caseList;
  42. NodePair pair;
  43. PjsList nodePairList;
  44. }
  45. %{
  46. void yyerror (YYLTYPE* locp, Params* params, const char* err);
  47. extern int pjs_yylex(YYSTYPE* lvalp, YYLTYPE* llocp, void* scanner_);
  48. %}
  49. %type <expr> identifier
  50. %type <expr> primary_expr
  51. %type <expr> member_expr
  52. %type <expr> new_expr
  53. %type <expr> call_expr
  54. %type <expr> left_hand_side_expr
  55. %type <expr> postfix_expr
  56. %type <expr> unary_expr
  57. %type <expr> multiplicative_expr
  58. %type <expr> additive_expr
  59. %type <expr> shift_expr
  60. %type <expr> relational_expr
  61. %type <expr> relational_expr_noin
  62. %type <expr> equality_expr
  63. %type <expr> equality_expr_noin
  64. %type <expr> bitwise_and_expr
  65. %type <expr> bitwise_and_expr_noin
  66. %type <expr> bitwise_xor_expr
  67. %type <expr> bitwise_xor_expr_noin
  68. %type <expr> bitwise_or_expr
  69. %type <expr> bitwise_or_expr_noin
  70. %type <expr> logical_and_expr
  71. %type <expr> logical_and_expr_noin
  72. %type <expr> logical_or_expr
  73. %type <expr> logical_or_expr_noin
  74. %type <expr> conditional_expr
  75. %type <expr> conditional_expr_noin
  76. %type <expr> assignment_expr
  77. %type <expr> assignment_expr_noin
  78. %type <expr> expr
  79. %type <expr> expr_noin
  80. %type <expr> function_expr
  81. %type <expr> initialiser initialiser_noin
  82. %type <exprList> array_literal
  83. %type <exprList> elision
  84. %type <exprList> element_list
  85. %type <exprList> arguments
  86. %type <exprList> argument_list
  87. %type <stmt> statement
  88. %type <stmt> variable_statement
  89. %type <stmt> empty_statement
  90. %type <stmt> expression_statement;
  91. %type <stmt> if_statement
  92. %type <stmt> iteration_statement
  93. %type <stmt> continue_statement_no_semi
  94. %type <stmt> continue_statement
  95. %type <stmt> break_statement_no_semi
  96. %type <stmt> break_statement
  97. %type <stmt> return_statement_no_semi
  98. %type <stmt> return_statement
  99. %type <stmt> with_statement
  100. %type <stmt> labelled_statement
  101. %type <stmt> throw_statement
  102. %type <stmt> try_statement
  103. %type <stmt> catch finally
  104. %type <stmt> function_declaration
  105. %type <stmt> source_element
  106. %type <stmt> switch_statement
  107. %type <stmt> case_block
  108. %type <stmtList> block_contents
  109. %type <stmtList> statement_list
  110. %type <stmtList> function_body
  111. %type <stmtList> source_elements
  112. %type <stmtList> program
  113. %type <stmtList> default_clause
  114. %type <params> formal_parameter_list
  115. %type <nameValue> variable_declaration
  116. %type <nameValue> variable_declaration_noin
  117. %type <nameValueList> object_literal
  118. /*%type <nameValueList> property_name_and_value_list */
  119. %type <nodePairList> property_name_and_value_list
  120. %type <nameValueList> variable_declaration_list
  121. %type <nameValueList> variable_declaration_list_noin
  122. %type <caseClause> case_clause
  123. %type <caseList> case_clauses
  124. /*%type <str> property_name */
  125. %type <expr> property_name
  126. %type <tokType> assignment_operator
  127. %token <str> NUMERIC_LITERAL STRING_LITERAL IDENTIFIER OCTAL_NUMBER_LITERAL
  128. /*-- ECMAScript Special - bool constants + pseudo stuff */
  129. %token TRUE_ FALSE_ NULL_
  130. %token REGEX_LITERAL
  131. %token INC_OP DEC_OP LEFT_OP RIGHT_OP
  132. %token LE_OP GE_OP EQ_OP NE_OP STRICT_EQ_OP STRICT_NE_OP
  133. %token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
  134. %token SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
  135. %token XOR_ASSIGN OR_ASSIGN
  136. %token UNSIGNED_RIGHT UNSIGNED_RIGHT_ASSIGN
  137. /*-- ECMAScript Keywords */
  138. %token BREAK FOR NEW VAR CONTINUE FUNCTION RETURN VOID
  139. %token DELETE IF THIS WHILE ELSE IN TYPEOF WITH
  140. /*-- ECMAScript Reserved Words */
  141. %token CASE DEBUGGER EXPORT SUPER CATCH DEFAULT EXTENDS SWITCH
  142. %token CLASS DO FINALLY THROW CONST ENUM IMPORT TRY
  143. %token INSTANCEOF FINAL ABSTRACT INT SHORT BOOLEAN INTERFACE
  144. %token STATIC BYTE LONG CHAR NATIVE SYNCHRONIZED FLOAT PACKAGE_
  145. %token THROWS GOTO PRIVATE TRANSIENT IMPLEMENTS
  146. %token PROTECTED VOLATILE DOUBLE PUBLIC
  147. %token RETURN_NL THROW_NL BREAK_NL CONTINUE_NL NL_INC_OP NL_DEC_OP
  148. %token PIR_CODE
  149. %token END LEX_ERROR
  150. /* The next are only used as enums for
  151. the AST, not for tokenizing. */
  152. %token PRE_INC_OP PRE_DEC_OP POST_INC_OP POST_DEC_OP
  153. %start program
  154. %%
  155. /*========== EXPRESSIONS =============================*/
  156. identifier
  157. : IDENTIFIER
  158. {{ $$ = newNode(EIdentifier, MP);
  159. $$->u.strVal = dupl(yylval.str, yymatch_len, MP);
  160. }}
  161. ;
  162. primary_expr
  163. : THIS
  164. {{ $$ = newNode(EThis, MP);
  165. }}
  166. | identifier
  167. {{ $$ = $1;
  168. }}
  169. | NULL_
  170. {{ $$ = newNode(ENull, MP);
  171. }}
  172. | TRUE_
  173. {{ $$ = newNode(EBoolean, MP);
  174. $$->u.boolVal = 1;
  175. }}
  176. | FALSE_
  177. {{ $$ = newNode(EBoolean, MP);
  178. $$->u.boolVal = 0;
  179. }}
  180. | NUMERIC_LITERAL
  181. {{ $$ = newNumberExpr(dupl(yylval.str, yymatch_len, MP), MP);
  182. }}
  183. | STRING_LITERAL
  184. {{ int err = 0;
  185. $$ = newNode(EString, MP);
  186. $$->u.strVal = unescape_js_string(yylval.str, yymatch_len, &err);
  187. if (err) {
  188. err_msg(params, "Can't parse string literal.");
  189. YYABORT;
  190. } else {
  191. mp_register($$->u.strVal, MP);
  192. }
  193. }}
  194. | array_literal
  195. {{ $$ = newNode(EArrayLit, MP);
  196. $$->u.exprList = $1;
  197. }}
  198. | object_literal
  199. {{ $$ = newNode(EObjectLit, MP);
  200. /*$$->u.nameValueList = $1; */
  201. $$->u.nodePairList = $1;
  202. }}
  203. | '(' expr ')'
  204. {{ $$ = $2;
  205. }}
  206. | REGEX_LITERAL
  207. {{
  208. $$ = newRegexExpr(dupl(yylval.str, yymatch_len, MP), MP);
  209. }}
  210. | '(' expr error
  211. {{ err_msg(params, "Unclosed paren."); YYABORT; }}
  212. | '(' error
  213. {{ err_msg(params, "Expected an expression."); YYABORT; }}
  214. ;
  215. array_literal
  216. : '[' ']'
  217. {{ $$ = newList(MP);
  218. }}
  219. | '[' elision ']'
  220. {{ $$ = $2;
  221. }}
  222. | '[' element_list ']'
  223. {{ $$ = $2;
  224. }}
  225. | '[' element_list ',' ']'
  226. {{ $$ = $2;
  227. }}
  228. | '[' element_list ',' elision ']'
  229. {{ $$ = appendList($2, $4);
  230. /*free($4); freed from mempool */
  231. }}
  232. ;
  233. element_list
  234. : assignment_expr
  235. {{ $$ = newList(MP);
  236. pushBack($1, $$, MP);
  237. }}
  238. | elision assignment_expr
  239. {{ $$ =$1;
  240. pushBack($2, $$, MP);
  241. }}
  242. | element_list ',' assignment_expr
  243. {{ $$ = $1;
  244. pushBack($3, $$, MP);
  245. }}
  246. | element_list ',' elision assignment_expr
  247. {{ $$ = appendList($1, $3);
  248. pushBack($4, $$, MP);
  249. /*free($3); freed from mempool */
  250. }}
  251. ;
  252. elision
  253. : ','
  254. {{ $$ = newList(MP);
  255. pushBack(0, $$, MP);
  256. }}
  257. | elision ','
  258. {{ $$ = $1;
  259. pushBack(0, $$, MP);
  260. }}
  261. ;
  262. object_literal
  263. : '{' '}'
  264. {{ $$ = newList(MP); }}
  265. | '{' property_name_and_value_list '}'
  266. {{ $$ = $2; }}
  267. ;
  268. property_name_and_value_list
  269. : identifier ':' assignment_expr
  270. {{ $$ = newList(MP);
  271. /*pushBack(newNameValue($1->u.strVal, $3, MP), $$, MP); */
  272. pushBack(newNodePair($1, $3, MP), $$, MP);
  273. }}
  274. | property_name ':' assignment_expr
  275. {{ $$ = newList(MP);
  276. pushBack(newNodePair($1, $3, MP), $$, MP);
  277. }}
  278. | property_name_and_value_list ',' identifier ':' assignment_expr
  279. {{ $$ = $1;
  280. pushBack(newNodePair($3, $5, MP), $$, MP);
  281. }}
  282. | property_name_and_value_list ',' property_name ':' assignment_expr
  283. {{ $$ = $1;
  284. pushBack(newNodePair($3, $5, MP), $$, MP);
  285. }}
  286. ;
  287. property_name
  288. : STRING_LITERAL
  289. {{ int err = 0;
  290. $$ = newNode(EString, MP);
  291. $$->u.strVal = unescape_js_string(yylval.str, yymatch_len, &err);
  292. if (err) {
  293. err_msg(params, "Can't parse string literal.");
  294. YYABORT;
  295. } else {
  296. mp_register($$->u.strVal, MP);
  297. }
  298. }}
  299. | NUMERIC_LITERAL
  300. {{ $$ = newNumberExpr(dupl(yylval.str, yymatch_len, MP), MP);
  301. }}
  302. ;
  303. member_expr
  304. : primary_expr
  305. {{ $$ = $1; }}
  306. | function_expr
  307. {{ $$ = $1; }}
  308. | member_expr '[' expr ']'
  309. {{ $$ = newNode(EIndexedAccess, MP);
  310. $$->u.getindexed.base = $1;
  311. $$->u.getindexed.index = $3;
  312. }}
  313. | member_expr '.' IDENTIFIER
  314. {{ char* id = mp_malloc(yymatch_len + 1, MP);
  315. strcpy(id, yylval.str);
  316. $$ = newNode(EPropAccess, MP);
  317. $$->u.getprop.base = $1;
  318. $$->u.getprop.propName = id;
  319. }}
  320. | NEW member_expr arguments
  321. {{ $$ = newNode(ENewFuncall, MP);
  322. $$->u.funcall.fun = $2;
  323. $$->u.funcall.args = $3;
  324. }}
  325. ;
  326. new_expr
  327. : member_expr
  328. {{ $$ = $1; }}
  329. | NEW new_expr
  330. {{ $$ = newNode(ENewFuncall, MP);
  331. $$->u.funcall.fun = $2;
  332. $$->u.funcall.args = newList(MP);
  333. }}
  334. ;
  335. call_expr
  336. : member_expr arguments
  337. {{ $$ = newNode(EFuncall, MP);
  338. $$->u.funcall.fun = $1;
  339. $$->u.funcall.args = $2;
  340. }}
  341. | call_expr arguments
  342. {{ $$ = newNode(EFuncall, MP);
  343. $$->u.funcall.fun = $1;
  344. $$->u.funcall.args = $2;
  345. }}
  346. | call_expr '[' expr ']'
  347. {{ $$ = newNode(EIndexedAccess, MP);
  348. $$->u.getindexed.base = $1;
  349. $$->u.getindexed.index = $3;
  350. }}
  351. | call_expr '.' IDENTIFIER
  352. {{ char* id = mp_malloc(yymatch_len + 1, MP);
  353. strcpy(id, yylval.str);
  354. $$ = newNode(EPropAccess, MP);
  355. $$->u.getprop.base = $1;
  356. $$->u.getprop.propName = id;
  357. }}
  358. ;
  359. arguments
  360. : '(' ')'
  361. {{ $$ = newList(MP); }}
  362. | '(' argument_list ')'
  363. {{ $$ = $2; }}
  364. |
  365. '(' error {
  366. err_msg(params, "Unclosed paren.");
  367. YYABORT;
  368. }
  369. |
  370. '(' argument_list error {
  371. err_msg(params, "Unclosed paren.");
  372. YYABORT;
  373. }
  374. ;
  375. argument_list
  376. : assignment_expr
  377. {{ $$ = newList(MP);
  378. pushBack($1, $$, MP);
  379. }}
  380. | argument_list ',' assignment_expr
  381. {{ $$ = $1;
  382. pushBack($3, $$, MP);
  383. }}
  384. ;
  385. left_hand_side_expr
  386. : new_expr
  387. {{ $$ = $1; }}
  388. | call_expr
  389. {{ $$ = $1; }}
  390. ;
  391. postfix_expr
  392. : left_hand_side_expr
  393. {{ $$ = $1; }}
  394. | left_hand_side_expr INC_OP
  395. {{ $$ = newUnop(POST_INC_OP, $1, MP);
  396. if (! $$) YYABORT;
  397. }}
  398. | left_hand_side_expr DEC_OP
  399. {{ $$ = newUnop(POST_DEC_OP, $1, MP);
  400. if (! $$) YYABORT;
  401. }}
  402. ;
  403. unary_expr
  404. : postfix_expr
  405. {{ $$ = $1; }}
  406. | DELETE unary_expr
  407. {{ $$ = newUnop(DELETE, $2, MP);
  408. if (! $$) YYABORT;
  409. }}
  410. | VOID unary_expr
  411. {{ $$ = newUnop(VOID, $2, MP);
  412. if (! $$) YYABORT;
  413. }}
  414. | TYPEOF unary_expr
  415. {{ $$ = newUnop(TYPEOF, $2, MP);
  416. if (! $$) YYABORT;
  417. }}
  418. | INC_OP unary_expr
  419. {{ $$ = newUnop(PRE_INC_OP, $2, MP);
  420. if (! $$) YYABORT;
  421. }}
  422. | NL_INC_OP unary_expr
  423. {{ $$ = newUnop(PRE_INC_OP, $2, MP);
  424. if (! $$) YYABORT;
  425. }}
  426. | DEC_OP unary_expr
  427. {{ $$ = newUnop(PRE_DEC_OP, $2, MP);
  428. if (! $$) YYABORT;
  429. }}
  430. | NL_DEC_OP unary_expr
  431. {{ $$ = newUnop(PRE_DEC_OP, $2, MP);
  432. if (! $$) YYABORT;
  433. }}
  434. | '+' unary_expr
  435. {{ $$ = newUnop('+', $2, MP);
  436. if (! $$) YYABORT;
  437. }}
  438. | '-' unary_expr
  439. {{ $$ = newUnop('-', $2, MP);
  440. if (! $$) YYABORT;
  441. }}
  442. | '~' unary_expr
  443. {{ $$ = newUnop('~', $2, MP);
  444. if (! $$) YYABORT;
  445. }}
  446. | '!' unary_expr
  447. {{ $$ = newUnop('!', $2, MP);
  448. if (! $$) YYABORT;
  449. }}
  450. ;
  451. multiplicative_expr
  452. : unary_expr
  453. {{ $$ = $1; }}
  454. | multiplicative_expr '*' unary_expr
  455. {{ $$ = newNode(EBinop, MP);
  456. $$->u.binop.left = $1;
  457. $$->u.binop.right = $3;
  458. $$->u.binop.op = '*';
  459. }}
  460. | multiplicative_expr '/' unary_expr
  461. {{ $$ = newNode(EBinop, MP);
  462. $$->u.binop.left = $1;
  463. $$->u.binop.right = $3;
  464. $$->u.binop.op = '/';
  465. }}
  466. | multiplicative_expr '%' unary_expr
  467. {{ $$ = newNode(EBinop, MP);
  468. $$->u.binop.left = $1;
  469. $$->u.binop.right = $3;
  470. $$->u.binop.op = '%';
  471. }}
  472. ;
  473. additive_expr
  474. : multiplicative_expr
  475. {{ $$ = $1; }}
  476. | additive_expr '+' multiplicative_expr
  477. {{ $$ = newNode(EBinop, MP);
  478. $$->u.binop.left = $1;
  479. $$->u.binop.right = $3;
  480. $$->u.binop.op = '+';
  481. }}
  482. | additive_expr '-' multiplicative_expr
  483. {{ $$ = newNode(EBinop, MP);
  484. $$->u.binop.left = $1;
  485. $$->u.binop.right = $3;
  486. $$->u.binop.op = '-';
  487. }}
  488. ;
  489. shift_expr
  490. : additive_expr
  491. {{ $$ = $1; }}
  492. | shift_expr LEFT_OP additive_expr
  493. {{ $$ = newNode(EBinop, MP);
  494. $$->u.binop.left = $1;
  495. $$->u.binop.right = $3;
  496. $$->u.binop.op = LEFT_OP;
  497. }}
  498. | shift_expr RIGHT_OP additive_expr
  499. {{ $$ = newNode(EBinop, MP);
  500. $$->u.binop.left = $1;
  501. $$->u.binop.right = $3;
  502. $$->u.binop.op = RIGHT_OP;
  503. }}
  504. | shift_expr UNSIGNED_RIGHT additive_expr
  505. {{ $$ = newNode(EBinop, MP);
  506. $$->u.binop.left = $1;
  507. $$->u.binop.right = $3;
  508. $$->u.binop.op = UNSIGNED_RIGHT;
  509. }}
  510. ;
  511. relational_expr
  512. : shift_expr
  513. {{ $$ = $1; }}
  514. | relational_expr '<' shift_expr
  515. {{ $$ = newNode(EBinop, MP);
  516. $$->u.binop.left = $1;
  517. $$->u.binop.right = $3;
  518. $$->u.binop.op = '<';
  519. }}
  520. | relational_expr '>' shift_expr
  521. {{ $$ = newNode(EBinop, MP);
  522. $$->u.binop.left = $1;
  523. $$->u.binop.right = $3;
  524. $$->u.binop.op = '>';
  525. }}
  526. | relational_expr LE_OP shift_expr
  527. {{ $$ = newNode(EBinop, MP);
  528. $$->u.binop.left = $1;
  529. $$->u.binop.right = $3;
  530. $$->u.binop.op = LE_OP;
  531. }}
  532. | relational_expr GE_OP shift_expr
  533. {{ $$ = newNode(EBinop, MP);
  534. $$->u.binop.left = $1;
  535. $$->u.binop.right = $3;
  536. $$->u.binop.op = GE_OP;
  537. }}
  538. | relational_expr IN shift_expr
  539. {{ $$ = newNode(EBinop, MP);
  540. $$->u.binop.left = $1;
  541. $$->u.binop.right = $3;
  542. $$->u.binop.op = IN;
  543. }}
  544. | relational_expr INSTANCEOF shift_expr
  545. {{ $$ = newNode(EBinop, MP);
  546. $$->u.binop.left = $1;
  547. $$->u.binop.right = $3;
  548. $$->u.binop.op = INSTANCEOF;
  549. }}
  550. ;
  551. relational_expr_noin
  552. : shift_expr
  553. {{ $$ = $1; }}
  554. | relational_expr '<' shift_expr
  555. {{ $$ = newNode(EBinop, MP);
  556. $$->u.binop.left = $1;
  557. $$->u.binop.right = $3;
  558. $$->u.binop.op = '<';
  559. }}
  560. | relational_expr '>' shift_expr
  561. {{ $$ = newNode(EBinop, MP);
  562. $$->u.binop.left = $1;
  563. $$->u.binop.right = $3;
  564. $$->u.binop.op = '>';
  565. }}
  566. | relational_expr LE_OP shift_expr
  567. {{ $$ = newNode(EBinop, MP);
  568. $$->u.binop.left = $1;
  569. $$->u.binop.right = $3;
  570. $$->u.binop.op = LE_OP;
  571. }}
  572. | relational_expr GE_OP shift_expr
  573. {{ $$ = newNode(EBinop, MP);
  574. $$->u.binop.left = $1;
  575. $$->u.binop.right = $3;
  576. $$->u.binop.op = GE_OP;
  577. }}
  578. | relational_expr INSTANCEOF shift_expr
  579. {{ $$ = newNode(EBinop, MP);
  580. $$->u.binop.left = $1;
  581. $$->u.binop.right = $3;
  582. $$->u.binop.op = INSTANCEOF;
  583. }}
  584. ;
  585. equality_expr
  586. : relational_expr
  587. {{ $$ = $1; }}
  588. | equality_expr EQ_OP relational_expr
  589. {{ $$ = newNode(EBinop, MP);
  590. $$->u.binop.left = $1;
  591. $$->u.binop.right = $3;
  592. $$->u.binop.op = EQ_OP;
  593. }}
  594. | equality_expr NE_OP relational_expr
  595. {{ $$ = newNode(EBinop, MP);
  596. $$->u.binop.left = $1;
  597. $$->u.binop.right = $3;
  598. $$->u.binop.op = NE_OP;
  599. }}
  600. | equality_expr STRICT_EQ_OP relational_expr
  601. {{ $$ = newNode(EBinop, MP);
  602. $$->u.binop.left = $1;
  603. $$->u.binop.right = $3;
  604. $$->u.binop.op = STRICT_EQ_OP;
  605. }}
  606. | equality_expr STRICT_NE_OP relational_expr
  607. {{ $$ = newNode(EBinop, MP);
  608. $$->u.binop.left = $1;
  609. $$->u.binop.right = $3;
  610. $$->u.binop.op = STRICT_NE_OP;
  611. }}
  612. ;
  613. equality_expr_noin
  614. : relational_expr_noin
  615. {{ $$ = $1; }}
  616. | equality_expr_noin EQ_OP relational_expr_noin
  617. {{ $$ = newNode(EBinop, MP);
  618. $$->u.binop.left = $1;
  619. $$->u.binop.right = $3;
  620. $$->u.binop.op = EQ_OP;
  621. }}
  622. | equality_expr_noin NE_OP relational_expr_noin
  623. {{ $$ = newNode(EBinop, MP);
  624. $$->u.binop.left = $1;
  625. $$->u.binop.right = $3;
  626. $$->u.binop.op = NE_OP;
  627. }}
  628. | equality_expr_noin STRICT_EQ_OP relational_expr_noin
  629. {{ $$ = newNode(EBinop, MP);
  630. $$->u.binop.left = $1;
  631. $$->u.binop.right = $3;
  632. $$->u.binop.op = STRICT_EQ_OP;
  633. }}
  634. | equality_expr_noin STRICT_NE_OP relational_expr_noin
  635. {{ $$ = newNode(EBinop, MP);
  636. $$->u.binop.left = $1;
  637. $$->u.binop.right = $3;
  638. $$->u.binop.op = STRICT_NE_OP;
  639. }}
  640. ;
  641. bitwise_and_expr
  642. : equality_expr
  643. {{ $$ = $1; }}
  644. | bitwise_and_expr '&' equality_expr
  645. {{ $$ = newNode(EBinop, MP);
  646. $$->u.binop.left = $1;
  647. $$->u.binop.right = $3;
  648. $$->u.binop.op = '&';
  649. }}
  650. ;
  651. bitwise_and_expr_noin
  652. : equality_expr_noin
  653. {{ $$ = $1; }}
  654. | bitwise_and_expr_noin '&' equality_expr_noin
  655. {{ $$ = newNode(EBinop, MP);
  656. $$->u.binop.left = $1;
  657. $$->u.binop.right = $3;
  658. $$->u.binop.op = '&';
  659. }}
  660. ;
  661. bitwise_xor_expr
  662. : bitwise_and_expr
  663. {{ $$ = $1; }}
  664. | bitwise_xor_expr '^' bitwise_and_expr
  665. {{ $$ = newNode(EBinop, MP);
  666. $$->u.binop.left = $1;
  667. $$->u.binop.right = $3;
  668. $$->u.binop.op = '^';
  669. }}
  670. ;
  671. bitwise_xor_expr_noin
  672. : bitwise_and_expr_noin
  673. {{ $$ = $1; }}
  674. | bitwise_xor_expr_noin '^' bitwise_and_expr_noin
  675. {{ $$ = newNode(EBinop, MP);
  676. $$->u.binop.left = $1;
  677. $$->u.binop.right = $3;
  678. $$->u.binop.op = '^';
  679. }}
  680. ;
  681. bitwise_or_expr
  682. : bitwise_xor_expr
  683. {{ $$ = $1; }}
  684. | bitwise_or_expr '|' bitwise_xor_expr
  685. {{ $$ = newNode(EBinop, MP);
  686. $$->u.binop.left = $1;
  687. $$->u.binop.right = $3;
  688. $$->u.binop.op = '|';
  689. }}
  690. ;
  691. bitwise_or_expr_noin
  692. : bitwise_xor_expr_noin
  693. {{ $$ = $1; }}
  694. | bitwise_or_expr_noin '|' bitwise_xor_expr_noin
  695. {{ $$ = newNode(EBinop, MP);
  696. $$->u.binop.left = $1;
  697. $$->u.binop.right = $3;
  698. $$->u.binop.op = '|';
  699. }}
  700. ;
  701. logical_and_expr
  702. : bitwise_or_expr
  703. {{ $$ = $1; }}
  704. | logical_and_expr AND_OP bitwise_or_expr
  705. {{ $$ = newNode(EBinop, MP);
  706. $$->u.binop.left = $1;
  707. $$->u.binop.right = $3;
  708. $$->u.binop.op = AND_OP;
  709. }}
  710. ;
  711. logical_and_expr_noin
  712. : bitwise_or_expr_noin
  713. {{ $$ = $1; }}
  714. | logical_and_expr_noin AND_OP bitwise_or_expr_noin
  715. {{ $$ = newNode(EBinop, MP);
  716. $$->u.binop.left = $1;
  717. $$->u.binop.right = $3;
  718. $$->u.binop.op = AND_OP;
  719. }}
  720. ;
  721. logical_or_expr
  722. : logical_and_expr
  723. {{ $$ = $1; }}
  724. | logical_or_expr OR_OP logical_and_expr
  725. {{ $$ = newNode(EBinop, MP);
  726. $$->u.binop.left = $1;
  727. $$->u.binop.right = $3;
  728. $$->u.binop.op = OR_OP;
  729. }}
  730. ;
  731. logical_or_expr_noin
  732. : logical_and_expr_noin
  733. {{ $$ = $1; }}
  734. | logical_or_expr_noin OR_OP logical_and_expr_noin
  735. {{ $$ = newNode(EBinop, MP);
  736. $$->u.binop.left = $1;
  737. $$->u.binop.right = $3;
  738. $$->u.binop.op = OR_OP;
  739. }}
  740. ;
  741. conditional_expr
  742. : logical_or_expr
  743. {{ $$ = $1; }}
  744. | logical_or_expr '?' assignment_expr ':' assignment_expr
  745. {{ $$ = newNode(ETernop, MP);
  746. $$->u.ternop.cond = $1;
  747. $$->u.ternop.ifTrue = $3;
  748. $$->u.ternop.ifFalse = $5;
  749. }}
  750. ;
  751. conditional_expr_noin
  752. : logical_or_expr_noin
  753. {{ $$ = $1; }}
  754. | logical_or_expr_noin '?' assignment_expr ':' assignment_expr
  755. {{ $$ = newNode(ETernop, MP);
  756. $$->u.ternop.cond = $1;
  757. $$->u.ternop.ifTrue = $3;
  758. $$->u.ternop.ifFalse = $5;
  759. }}
  760. ;
  761. assignment_expr
  762. : conditional_expr
  763. {{ $$ = $1; }}
  764. | left_hand_side_expr assignment_operator assignment_expr
  765. {{ $$ = newNode(EAssign, MP);
  766. $$->u.assign.left = $1;
  767. $$->u.assign.type = $2;
  768. $$->u.assign.right = $3;
  769. }}
  770. ;
  771. assignment_expr_noin
  772. : conditional_expr_noin
  773. {{ $$ = $1; }}
  774. | left_hand_side_expr assignment_operator assignment_expr_noin
  775. {{ $$ = newNode(EAssign, MP);
  776. $$->u.assign.left = $1;
  777. $$->u.assign.type = $2;
  778. $$->u.assign.right = $3;
  779. }}
  780. ;
  781. assignment_operator
  782. : '='
  783. {{ $$ = '='; }}
  784. | MUL_ASSIGN
  785. {{ $$ = '*'; }}
  786. | DIV_ASSIGN
  787. {{ $$ = '/'; }}
  788. | MOD_ASSIGN
  789. {{ $$ = '%'; }}
  790. | ADD_ASSIGN
  791. {{ $$ = '+'; }}
  792. | SUB_ASSIGN
  793. {{ $$ = '-'; }}
  794. | LEFT_ASSIGN
  795. {{ $$ = LEFT_OP; }}
  796. | RIGHT_ASSIGN
  797. {{ $$ = RIGHT_OP; }}
  798. | AND_ASSIGN
  799. {{ $$ = '&'; }}
  800. | XOR_ASSIGN
  801. {{ $$ = '^'; }}
  802. | OR_ASSIGN
  803. {{ $$ = '|'; }}
  804. | UNSIGNED_RIGHT_ASSIGN
  805. {{ $$ = UNSIGNED_RIGHT; }}
  806. ;
  807. expr
  808. : assignment_expr
  809. {{ $$ = $1;
  810. }}
  811. | expr ',' assignment_expr
  812. {{ $$ = newNode(ECompound, MP);
  813. $$->u.exprPair.left = $1;
  814. $$->u.exprPair.right = $3;
  815. }}
  816. ;
  817. expr_noin
  818. : assignment_expr_noin
  819. {{ $$ = $1;
  820. }}
  821. | expr_noin ',' assignment_expr_noin
  822. {{ $$ = newNode(ECompound, MP);
  823. $$->u.exprPair.left = $1;
  824. $$->u.exprPair.right = $3;
  825. }}
  826. ;
  827. /*========== STATEMENTS =============================*/
  828. statement
  829. : variable_statement
  830. {{ $$ = $1; }}
  831. | empty_statement
  832. {{ $$ = $1; }}
  833. | '{' '}'
  834. {{ $$ = newNode(EBlock_stm, MP);
  835. $$->u.stmtList = newList(MP);
  836. }}
  837. | '{' statement_list '}'
  838. {{ $$ = newNode(EBlock_stm, MP);
  839. $$->u.stmtList = $2;
  840. }}
  841. | expression_statement
  842. {{ $$ = $1; }}
  843. | if_statement
  844. {{ $$ = $1; }}
  845. | iteration_statement
  846. {{ $$ = $1; }}
  847. | continue_statement
  848. {{ $$ = $1; }}
  849. | break_statement
  850. {{ $$ = $1; }}
  851. | return_statement
  852. {{ $$ = $1; }}
  853. | with_statement
  854. {{ $$ = $1; }}
  855. | labelled_statement
  856. {{ $$ = $1; }}
  857. | switch_statement
  858. {{ $$ = $1; }}
  859. | throw_statement
  860. {{ $$ = $1; }}
  861. | try_statement
  862. {{ $$ = $1; }}
  863. | PIR_CODE
  864. {{ char* start = yylval.str;
  865. char* end = start + yymatch_len;
  866. while (start < end && *start != '\n')
  867. start++;
  868. start++;
  869. while (end > start && *end != '\n')
  870. end--;
  871. $$ = newNode(EPirCode_stm, MP);
  872. if (start < end) {
  873. $$->u.strVal = dupl(start, end-start, MP);
  874. } else {
  875. $$->u.strVal = dupl("", 0, MP);
  876. }
  877. }}
  878. ;
  879. block_contents
  880. : /* empty */
  881. {{ $$ = newList(MP); }}
  882. | statement_list
  883. {{ $$ = $1; }}
  884. ;
  885. statement_list
  886. : statement
  887. {{ $$ = newList(MP);
  888. pushBack($1, $$, MP);
  889. }}
  890. | statement_list statement
  891. {{ $$ = $1;
  892. pushBack($2, $$, MP);
  893. }}
  894. ;
  895. variable_statement
  896. : VAR variable_declaration_list ';'
  897. {{ $$ = newNode(EVardec_stm, MP);
  898. $$->u.nameValueList = $2;
  899. }}
  900. | VAR variable_declaration_list error
  901. {{ if(! automatic_test(yyparams, yychar)) YYABORT;
  902. $$ = newNode(EVardec_stm, MP);
  903. $$->u.nameValueList = $2;
  904. }}
  905. ;
  906. variable_declaration_list
  907. : variable_declaration
  908. {{ $$ = newList(MP);
  909. pushBack($1, $$, MP);
  910. }}
  911. | variable_declaration_list ',' variable_declaration
  912. {{ pushBack($3, $1, MP);
  913. }}
  914. ;
  915. variable_declaration_list_noin
  916. : variable_declaration_noin
  917. {{ $$ = newList(MP);
  918. pushBack($1, $$, MP);
  919. }}
  920. | variable_declaration_list_noin ',' variable_declaration_noin
  921. {{ pushBack($3, $1, MP);
  922. }}
  923. ;
  924. variable_declaration
  925. : identifier
  926. {{ $$ = newNameValue($1->u.strVal, 0, MP);
  927. /*free($1); freed from mempool */
  928. }}
  929. | identifier initialiser
  930. {{ $$ = newNameValue($1->u.strVal, $2, MP);
  931. /*free($1); freed from mempool */
  932. }}
  933. ;
  934. variable_declaration_noin
  935. : identifier
  936. {{ $$ = newNameValue($1->u.strVal, 0, MP);
  937. /*free($1); freed from mempool */
  938. }}
  939. | identifier initialiser_noin
  940. {{ $$ = newNameValue($1->u.strVal, $2, MP);
  941. /*free($1); freed from mempool */
  942. }}
  943. ;
  944. initialiser
  945. : '=' assignment_expr
  946. {{ $$ = $2; }}
  947. ;
  948. initialiser_noin
  949. : '=' assignment_expr_noin
  950. {{ $$ = $2; }}
  951. ;
  952. empty_statement
  953. : ';'
  954. {{ $$ = newNode(EBlock_stm, MP);
  955. $$->u.stmtList = newList(MP);
  956. }}
  957. ;
  958. /* lookahead?*/
  959. expression_statement
  960. : expr ';'
  961. {{ $$ = $1; }}
  962. | expr error
  963. {{ if(! automatic_test(yyparams, yychar)) YYABORT;
  964. $$ = $1;
  965. }}
  966. ;
  967. if_statement
  968. : IF '(' expr ')' statement
  969. {{ $$ = newNode(EIfElse_stm, MP);
  970. $$->u.ifElse.cond = $3;
  971. $$->u.ifElse.ifTrue = $5;
  972. $$->u.ifElse.ifFalse = 0;
  973. }}
  974. | IF '(' expr ')' statement ELSE statement
  975. {{ $$ = newNode(EIfElse_stm, MP);
  976. $$->u.ifElse.cond = $3;
  977. $$->u.ifElse.ifTrue = $5;
  978. $$->u.ifElse.ifFalse = $7;
  979. }}
  980. ;
  981. iteration_statement
  982. : WHILE '(' expr ')' statement
  983. {{ $$ = newNode(EWhile_stm, MP);
  984. $$->u.whileLoop.cond = $3;
  985. $$->u.whileLoop.stmt = $5;
  986. }}
  987. | DO statement WHILE '(' expr ')' ';'
  988. {{ $$ = newNode(EDoWhile_stm, MP);
  989. $$->u.whileLoop.cond = $5;
  990. $$->u.whileLoop.stmt = $2;
  991. }}
  992. | FOR '(' ';' ';' ')' statement
  993. {{ $$ = newNode(EFor_stm, MP);
  994. $$->u.forLoop.init = 0;
  995. $$->u.forLoop.cond = 0;
  996. $$->u.forLoop.next = 0;
  997. $$->u.forLoop.stmt = $6;
  998. }}
  999. | FOR '(' ';' ';' expr ')' statement
  1000. {{ $$ = newNode(EFor_stm, MP);
  1001. $$->u.forLoop.init = 0;
  1002. $$->u.forLoop.cond = 0;
  1003. $$->u.forLoop.next = $5;
  1004. $$->u.forLoop.stmt = $7;
  1005. }}
  1006. | FOR '(' ';' expr ';' ')' statement
  1007. {{ $$ = newNode(EFor_stm, MP);
  1008. $$->u.forLoop.init = 0;
  1009. $$->u.forLoop.cond = $4;
  1010. $$->u.forLoop.next = 0;
  1011. $$->u.forLoop.stmt = $7;
  1012. }}
  1013. | FOR '(' ';' expr ';' expr ')' statement
  1014. {{ $$ = newNode(EFor_stm, MP);
  1015. $$->u.forLoop.init = 0;
  1016. $$->u.forLoop.cond = $4;
  1017. $$->u.forLoop.next = $6;
  1018. $$->u.forLoop.stmt = $8;
  1019. }}
  1020. | FOR '(' expr_noin ';' ';' ')' statement
  1021. {{ $$ = newNode(EFor_stm, MP);
  1022. $$->u.forLoop.init = $3;
  1023. $$->u.forLoop.cond = 0;
  1024. $$->u.forLoop.next = 0;
  1025. $$->u.forLoop.stmt = $7;
  1026. }}
  1027. | FOR '(' expr_noin ';' ';' expr ')' statement
  1028. {{ $$ = newNode(EFor_stm, MP);
  1029. $$->u.forLoop.init = $3;
  1030. $$->u.forLoop.cond = 0;
  1031. $$->u.forLoop.next = $6;
  1032. $$->u.forLoop.stmt = $8;
  1033. }}
  1034. | FOR '(' expr_noin ';' expr ';' ')' statement
  1035. {{ $$ = newNode(EFor_stm, MP);
  1036. $$->u.forLoop.init = $3;
  1037. $$->u.forLoop.cond = $5;
  1038. $$->u.forLoop.next = 0;
  1039. $$->u.forLoop.stmt = $8;
  1040. }}
  1041. | FOR '(' expr_noin ';' expr ';' expr ')' statement
  1042. {{ $$ = newNode(EFor_stm, MP);
  1043. $$->u.forLoop.init = $3;
  1044. $$->u.forLoop.cond = $5;
  1045. $$->u.forLoop.next = $7;
  1046. $$->u.forLoop.stmt = $9;
  1047. }}
  1048. | FOR '(' VAR variable_declaration_list_noin ';' ';' ')' statement
  1049. {{ Node varDec = newNode(EVardec_stm, MP);
  1050. varDec->u.nameValueList = $4;
  1051. $$ = newNode(EFor_stm, MP);
  1052. $$->u.forLoop.init = varDec;
  1053. $$->u.forLoop.cond = 0;
  1054. $$->u.forLoop.next = 0;
  1055. $$->u.forLoop.stmt = $8;
  1056. }}
  1057. | FOR '(' VAR variable_declaration_list_noin ';' ';' expr ')' statement
  1058. {{ Node varDec = newNode(EVardec_stm, MP);
  1059. varDec->u.nameValueList = $4;
  1060. $$ = newNode(EFor_stm, MP);
  1061. $$->u.forLoop.init = varDec;
  1062. $$->u.forLoop.cond = 0;
  1063. $$->u.forLoop.next = $7;
  1064. $$->u.forLoop.stmt = $9;
  1065. }}
  1066. | FOR '(' VAR variable_declaration_list_noin ';' expr ';' ')' statement
  1067. {{ Node varDec = newNode(EVardec_stm, MP);
  1068. varDec->u.nameValueList = $4;
  1069. $$ = newNode(EFor_stm, MP);
  1070. $$->u.forLoop.init = varDec;
  1071. $$->u.forLoop.cond = $6;
  1072. $$->u.forLoop.next = 0;
  1073. $$->u.forLoop.stmt = $9;
  1074. }}
  1075. | FOR '(' VAR variable_declaration_list_noin ';' expr ';' expr ')' statement
  1076. {{ Node varDec = newNode(EVardec_stm, MP);
  1077. varDec->u.nameValueList = $4;
  1078. $$ = newNode(EFor_stm, MP);
  1079. $$->u.forLoop.init = varDec;
  1080. $$->u.forLoop.cond = $6;
  1081. $$->u.forLoop.next = $8;
  1082. $$->u.forLoop.stmt = $10;
  1083. }}
  1084. | FOR '(' left_hand_side_expr IN expr ')' statement
  1085. {{ $$ = newNode(EForIn_stm, MP);
  1086. $$->u.forInLoop.var = $3;
  1087. $$->u.forInLoop.collection = $5;
  1088. $$->u.forInLoop.stmt = $7;
  1089. $$->u.forInLoop.isVarDec = 0;
  1090. }}
  1091. /*| FOR '(' VAR left_hand_side_expr IN expr ')' statement
  1092. {{ $$ = newNode(EForIn_stm, MP);
  1093. $$->u.forInLoop.var = $4;
  1094. $$->u.forInLoop.collection = $6;
  1095. $$->u.forInLoop.stmt = $8;
  1096. $$->u.forInLoop.isVarDec = 1;
  1097. }}*/
  1098. | FOR '(' VAR variable_declaration_noin IN expr ')' statement
  1099. {{ $$ = newNode(EForIn_stm, MP);
  1100. $$->u.forInLoop.var = 0;
  1101. $$->u.forInLoop.newVar = $4;
  1102. $$->u.forInLoop.collection = $6;
  1103. $$->u.forInLoop.stmt = $8;
  1104. $$->u.forInLoop.isVarDec = 1;
  1105. }}
  1106. ;
  1107. continue_statement_no_semi
  1108. :
  1109. CONTINUE identifier
  1110. {{ $$ = newNode(EContinue_stm, MP);
  1111. $$->u.continueBreak.label = $2->u.strVal;
  1112. $$->u.continueBreak.brokenStmt = 0;
  1113. /*free($2); freed from mempool */
  1114. }}
  1115. | CONTINUE
  1116. {{ $$ = newNode(EContinue_stm, MP);
  1117. $$->u.continueBreak.label = 0;
  1118. $$->u.continueBreak.brokenStmt = 0;
  1119. }}
  1120. | CONTINUE_NL
  1121. {{ $$ = newNode(EContinue_stm, MP);
  1122. $$->u.continueBreak.label = 0;
  1123. $$->u.continueBreak.brokenStmt = 0;
  1124. }}
  1125. ;
  1126. continue_statement
  1127. : continue_statement_no_semi ';'
  1128. {{ $$ = $1; }}
  1129. | continue_statement_no_semi error
  1130. {{ if(! automatic_test(yyparams, yychar)) YYABORT;
  1131. $$ = $1;
  1132. }}
  1133. ;
  1134. break_statement_no_semi
  1135. : BREAK identifier
  1136. {{ $$ = newNode(EBreak_stm, MP);
  1137. $$->u.continueBreak.label = $2->u.strVal;
  1138. $$->u.continueBreak.brokenStmt = 0;
  1139. /*free($2); freed from mempool */
  1140. }}
  1141. | BREAK
  1142. {{ $$ = newNode(EBreak_stm, MP);
  1143. $$->u.continueBreak.label = 0;
  1144. $$->u.continueBreak.brokenStmt = 0;
  1145. }}
  1146. | BREAK_NL
  1147. {{ $$ = newNode(EBreak_stm, MP);
  1148. $$->u.continueBreak.label = 0;
  1149. $$->u.continueBreak.brokenStmt = 0;
  1150. }}
  1151. ;
  1152. break_statement
  1153. : break_statement_no_semi ';'
  1154. {{ $$ = $1; }}
  1155. | break_statement_no_semi error
  1156. {{ if(! automatic_test(yyparams, yychar)) YYABORT;
  1157. $$ = $1;
  1158. }}
  1159. ;
  1160. return_statement_no_semi
  1161. : RETURN expr
  1162. {{ $$ = newNode(EReturn_stm, MP);
  1163. $$->u.expr = $2;
  1164. }}
  1165. | RETURN
  1166. {{ $$ = newNode(EReturn_stm, MP);
  1167. $$->u.expr = 0;
  1168. }}
  1169. | RETURN_NL
  1170. {{ $$ = newNode(EReturn_stm, MP);
  1171. $$->u.expr = 0;
  1172. }}
  1173. ;
  1174. return_statement
  1175. : return_statement_no_semi ';'
  1176. {{ $$ = $1; }}
  1177. | return_statement_no_semi error
  1178. {{ if(! automatic_test(yyparams, yychar)) YYABORT;
  1179. $$ = $1;
  1180. }}
  1181. ;
  1182. with_statement
  1183. : WITH '(' expr ')' statement
  1184. {{ $$ = newNode(EWith_stm, MP);
  1185. $$->u.with.scope = $3;
  1186. $$->u.with.stmt = $5;
  1187. }}
  1188. ;
  1189. /* TODO */
  1190. switch_statement
  1191. : SWITCH '(' expr ')' case_block
  1192. {{ $$ = $5;
  1193. $$->u.switchStm.expr = $3;
  1194. }}
  1195. ;
  1196. /* TODO */
  1197. case_block
  1198. : '{' '}'
  1199. {{ $$ = newNode(ESwitch_stm, MP);
  1200. $$->u.switchStm.defaultStmtList = 0;
  1201. $$->u.switchStm.caseList = newList(MP);
  1202. }}
  1203. | '{' case_clauses '}'
  1204. {{ $$ = newNode(ESwitch_stm, MP);
  1205. $$->u.switchStm.defaultStmtList = 0;
  1206. $$->u.switchStm.caseList = $2;
  1207. }}
  1208. | '{' default_clause '}'
  1209. {{ $$ = newNode(ESwitch_stm, MP);
  1210. $$->u.switchStm.defaultStmtList = $2;
  1211. $$->u.switchStm.caseList = newList(MP);
  1212. }}
  1213. | '{' default_clause case_clauses '}'
  1214. {{ $$ = newNode(ESwitch_stm, MP);
  1215. $$->u.switchStm.defaultStmtList = $2;
  1216. $$->u.switchStm.caseList = $3;
  1217. }}
  1218. | '{' case_clauses default_clause '}'
  1219. {{ $$ = newNode(ESwitch_stm, MP);
  1220. $$->u.switchStm.defaultStmtList = $3;
  1221. $$->u.switchStm.caseList = $2;
  1222. }}
  1223. | '{' case_clauses default_clause case_clauses '}'
  1224. {{ $$ = newNode(ESwitch_stm, MP);
  1225. $$->u.switchStm.defaultStmtList = $3;
  1226. $$->u.switchStm.caseList = appendList($2, $4);
  1227. /*free($4); freed from mempool */
  1228. }}
  1229. ;
  1230. case_clauses
  1231. : case_clause
  1232. {{ $$ = newList(MP);
  1233. pushBack($1, $$, MP);
  1234. }}
  1235. | case_clauses case_clause
  1236. {{ $$ = $1;
  1237. pushBack($2, $$, MP);
  1238. }}
  1239. ;
  1240. case_clause
  1241. : CASE expr ':'
  1242. {{ $$ = newCaseClause($2, 0, MP); }}
  1243. | CASE expr ':' statement_list
  1244. {{ $$ = newCaseClause($2, $4, MP); }}
  1245. ;
  1246. default_clause
  1247. : DEFAULT ':'
  1248. {{ $$ = 0; }}
  1249. | DEFAULT ':' statement_list
  1250. {{ $$ = $3;
  1251. }}
  1252. ;
  1253. labelled_statement
  1254. : identifier ':' statement
  1255. {{ $$ = $3;
  1256. if (! $$->stmtLabels)
  1257. $$->stmtLabels = newList(MP);
  1258. pushFront($1->u.strVal, $$->stmtLabels, MP);
  1259. }}
  1260. ;
  1261. /* no line terminator here ?? */
  1262. throw_statement
  1263. :
  1264. THROW expr ';'
  1265. {{ $$ = newNode(EThrow_stm, MP);
  1266. $$->u.expr = $2;
  1267. }}
  1268. | THROW expr error
  1269. {{ if(! automatic_test(yyparams, yychar)) YYABORT;
  1270. $$ = newNode(EThrow_stm, MP);
  1271. $$->u.expr = $2;
  1272. }}
  1273. ;
  1274. try_statement
  1275. : TRY '{' block_contents '}' catch
  1276. {{ $$ = $5;
  1277. $$->u.tryCatchFinally.tryBlock = $3;
  1278. $$->u.tryCatchFinally.finallyBlock = 0;
  1279. }}
  1280. | TRY '{' block_contents '}' finally
  1281. {{ $$ = $5;
  1282. $$->u.tryCatchFinally.tryBlock = $3;
  1283. $$->u.tryCatchFinally.catchBlock = 0;
  1284. $$->u.tryCatchFinally.catchVar = 0;
  1285. }}
  1286. | TRY '{' block_contents '}' catch finally
  1287. {{ $$ = $5;
  1288. $$->u.tryCatchFinally.tryBlock = $3;
  1289. $$->u.tryCatchFinally.finallyBlock =
  1290. $6->u.tryCatchFinally.finallyBlock;
  1291. /*free($6); freed from mempool */
  1292. }}
  1293. ;
  1294. catch
  1295. : CATCH '(' identifier ')' '{' block_contents '}'
  1296. {{ $$ = newNode(ETry_stm, MP);
  1297. $$->u.tryCatchFinally.catchVar = $3->u.strVal;
  1298. /*free($3); freed from mempool */
  1299. $$->u.tryCatchFinally.catchBlock = $6;
  1300. }}
  1301. ;
  1302. finally
  1303. : FINALLY '{' block_contents '}'
  1304. {{ $$ = newNode(ETry_stm, MP);
  1305. $$->u.tryCatchFinally.finallyBlock = $3;
  1306. }}
  1307. ;
  1308. function_declaration
  1309. : FUNCTION identifier '(' formal_parameter_list ')' function_body
  1310. {{ char* name = $2->u.strVal;
  1311. /*free($2); freed from mempool */
  1312. $$ = newNode(EFunDec_stm, MP);
  1313. $$->u.function.name = name;
  1314. $$->u.function.params = $4;
  1315. $$->u.function.stmtList = $6;
  1316. $$->u.function.functionNr = 0;
  1317. $$->u.function.vardecList = newList(MP);
  1318. $$->u.function.innerFuncs = newList(MP);
  1319. $$->u.function.nInnerFuncs = 0;
  1320. $$->u.function.funDecList = newList(MP);
  1321. $$->u.function.hasTryCatch = 0;
  1322. }}
  1323. | FUNCTION identifier '(' ')' function_body
  1324. {{ char* name = $2->u.strVal;
  1325. /*free($2); freed from mempool */
  1326. $$ = newNode(EFunDec_stm, MP);
  1327. $$->u.function.name = name;
  1328. $$->u.function.params = newList(MP);
  1329. $$->u.function.stmtList = $5;
  1330. $$->u.function.functionNr = 0;
  1331. $$->u.function.vardecList = newList(MP);
  1332. $$->u.function.innerFuncs = newList(MP);
  1333. $$->u.function.nInnerFuncs = 0;
  1334. $$->u.function.funDecList = newList(MP);
  1335. $$->u.function.hasTryCatch = 0;
  1336. }}
  1337. ;
  1338. function_expr
  1339. : FUNCTION identifier '(' formal_parameter_list ')' function_body
  1340. {{ char* name = $2->u.strVal;
  1341. /*free($2); freed from mempool */
  1342. $$ = newNode(EFunExpr, MP);
  1343. $$->u.function.name = name;
  1344. $$->u.function.params = $4;
  1345. $$->u.function.stmtList = $6;
  1346. $$->u.function.functionNr = 0;
  1347. $$->u.function.vardecList = newList(MP);
  1348. $$->u.function.innerFuncs = newList(MP);
  1349. $$->u.function.nInnerFuncs = 0;
  1350. $$->u.function.funDecList = newList(MP);
  1351. $$->u.function.hasTryCatch = 0;
  1352. }}
  1353. | FUNCTION identifier '(' ')' function_body
  1354. {{ char* name = $2->u.strVal;
  1355. /*free($2); freed from mempool */
  1356. $$ = newNode(EFunExpr, MP);
  1357. $$->u.function.name = name;
  1358. $$->u.function.params = newList(MP);
  1359. $$->u.function.stmtList = $5;
  1360. $$->u.function.functionNr = 0;
  1361. $$->u.function.vardecList = newList(MP);
  1362. $$->u.function.innerFuncs = newList(MP);
  1363. $$->u.function.nInnerFuncs = 0;
  1364. $$->u.function.funDecList = newList(MP);
  1365. $$->u.function.hasTryCatch = 0;
  1366. }}
  1367. | FUNCTION '(' formal_parameter_list ')' function_body
  1368. {{ $$ = newNode(EFunExpr, MP);
  1369. $$->u.function.name = 0;
  1370. $$->u.function.params = $3;
  1371. $$->u.function.stmtList = $5;
  1372. $$->u.function.functionNr = 0;
  1373. $$->u.function.vardecList = newList(MP);
  1374. $$->u.function.innerFuncs = newList(MP);
  1375. $$->u.function.nInnerFuncs = 0;
  1376. $$->u.function.funDecList = newList(MP);
  1377. $$->u.function.hasTryCatch = 0;
  1378. }}
  1379. | FUNCTION '(' ')' function_body
  1380. {{ $$ = newNode(EFunExpr, MP);
  1381. $$->u.function.name = 0;
  1382. $$->u.function.params = newList(MP);
  1383. $$->u.function.stmtList = $4;
  1384. $$->u.function.functionNr = 0;
  1385. $$->u.function.vardecList = newList(MP);
  1386. $$->u.function.innerFuncs = newList(MP);
  1387. $$->u.function.nInnerFuncs = 0;
  1388. $$->u.function.funDecList = newList(MP);
  1389. $$->u.function.hasTryCatch = 0;
  1390. }}
  1391. ;
  1392. formal_parameter_list
  1393. : identifier
  1394. {{ char* name = $1->u.strVal;
  1395. /*free($1); freed from mempool */
  1396. $$ = newList(MP);
  1397. pushBack(name, $$, MP);
  1398. }}
  1399. | formal_parameter_list ',' identifier
  1400. {{ char* name = $3->u.strVal;
  1401. /*free($3); freed from mempool */
  1402. $$ = $1;
  1403. pushBack(name, $$, MP);
  1404. }}
  1405. ;
  1406. function_body
  1407. : '{' source_elements '}'
  1408. {{ $$ = $2; }}
  1409. | '{' '}'
  1410. {{ $$ = newList(MP); }}
  1411. ;
  1412. program
  1413. : source_elements
  1414. {{ $$ = $1;
  1415. yyparams->program = newNode(EFunExpr, MP);
  1416. yyparams->program->u.function.name = 0;
  1417. yyparams->program->u.function.params = newList(MP);
  1418. yyparams->program->u.function.stmtList = $$;
  1419. yyparams->program->u.function.functionNr = 0;
  1420. yyparams->program->u.function.vardecList = newList(MP);
  1421. yyparams->program->u.function.innerFuncs = newList(MP);
  1422. yyparams->program->u.function.nInnerFuncs = 0;
  1423. yyparams->program->u.function.funDecList = newList(MP);
  1424. yyparams->program->u.function.hasTryCatch = 0;
  1425. }}
  1426. | /*empty*/
  1427. {{ $$ = newList(MP);
  1428. yyparams->program = newNode(EFunExpr, MP);
  1429. yyparams->program->u.function.name = 0;
  1430. yyparams->program->u.function.params = newList(MP);
  1431. yyparams->program->u.function.stmtList = $$;
  1432. yyparams->program->u.function.functionNr = 0;
  1433. yyparams->program->u.function.vardecList = newList(MP);
  1434. yyparams->program->u.function.innerFuncs = newList(MP);
  1435. yyparams->program->u.function.nInnerFuncs = 0;
  1436. yyparams->program->u.function.funDecList = newList(MP);
  1437. yyparams->program->u.function.hasTryCatch = 0;
  1438. }}
  1439. ;
  1440. source_elements
  1441. : source_element
  1442. {{ $$ = newList(MP);
  1443. pushBack($1, $$, MP);
  1444. }}
  1445. | source_elements source_element
  1446. {{ $$ = $1;
  1447. pushBack($2, $$, MP);
  1448. }}
  1449. ;
  1450. source_element
  1451. : statement
  1452. {{ $$ = $1; }}
  1453. | function_declaration
  1454. {{ $$ = $1; }}
  1455. ;
  1456. %%
  1457. static void err_msg(Params* params, const char *msg) {
  1458. snprintf(params->err_msg, sizeof(params->err_msg)-1, "[line %d, column %d] %s",
  1459. params->line_no + 1, params->column_no, msg);
  1460. }
  1461. /* may we automatically insert a semicolon? */
  1462. static int automatic_test(Params* params, int yychar) {
  1463. if (yychar == LEX_ERROR) {
  1464. return 0;
  1465. } else if (yychar == '}' || yychar == 0) {
  1466. return 1;
  1467. } else if (params->prev_was_newline) {
  1468. return 1;
  1469. } else {
  1470. return 0;
  1471. }
  1472. }
  1473. void yyerror (YYLTYPE* locp, Params* params, const char* msg) {
  1474. snprintf(params->err_msg, sizeof(params->err_msg)-1, "[line %d, column %d] %s",
  1475. params->line_no + 1, params->column_no, msg);
  1476. }