PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/opensource.apple.com/source/ruby/ruby-22/ruby/parse.y

#
Happy | 3226 lines | 2988 code | 238 blank | 0 comment | 0 complexity | b7bda74b50c4fe8e34f2341f744bd99f 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

Large files files are truncated, but you can click here to view the full file

  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>parse.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">parse.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. parse.y -
  25. $Author: matz $
  26. $Date: 2004/11/29 06:13:51 $
  27. created at: Fri May 28 18:02:42 JST 1993
  28. Copyright (C) 1993-2003 Yukihiro Matsumoto
  29. **********************************************************************/
  30. %{
  31. #define YYDEBUG 1
  32. #include &quot;ruby.h&quot;
  33. #include &quot;env.h&quot;
  34. #include &quot;intern.h&quot;
  35. #include &quot;node.h&quot;
  36. #include &quot;st.h&quot;
  37. #include &lt;stdio.h&gt;
  38. #include &lt;errno.h&gt;
  39. #include &lt;ctype.h&gt;
  40. #define yyparse ruby_yyparse
  41. #define yylex ruby_yylex
  42. #define yyerror ruby_yyerror
  43. #define yylval ruby_yylval
  44. #define yychar ruby_yychar
  45. #define yydebug ruby_yydebug
  46. #define ID_SCOPE_SHIFT 3
  47. #define ID_SCOPE_MASK 0x07
  48. #define ID_LOCAL 0x01
  49. #define ID_INSTANCE 0x02
  50. #define ID_GLOBAL 0x03
  51. #define ID_ATTRSET 0x04
  52. #define ID_CONST 0x05
  53. #define ID_CLASS 0x06
  54. #define ID_JUNK 0x07
  55. #define ID_INTERNAL ID_JUNK
  56. #define is_notop_id(id) ((id)&gt;tLAST_TOKEN)
  57. #define is_local_id(id) (is_notop_id(id)&amp;&amp;((id)&amp;ID_SCOPE_MASK)==ID_LOCAL)
  58. #define is_global_id(id) (is_notop_id(id)&amp;&amp;((id)&amp;ID_SCOPE_MASK)==ID_GLOBAL)
  59. #define is_instance_id(id) (is_notop_id(id)&amp;&amp;((id)&amp;ID_SCOPE_MASK)==ID_INSTANCE)
  60. #define is_attrset_id(id) (is_notop_id(id)&amp;&amp;((id)&amp;ID_SCOPE_MASK)==ID_ATTRSET)
  61. #define is_const_id(id) (is_notop_id(id)&amp;&amp;((id)&amp;ID_SCOPE_MASK)==ID_CONST)
  62. #define is_class_id(id) (is_notop_id(id)&amp;&amp;((id)&amp;ID_SCOPE_MASK)==ID_CLASS)
  63. #define is_junk_id(id) (is_notop_id(id)&amp;&amp;((id)&amp;ID_SCOPE_MASK)==ID_JUNK)
  64. #define is_asgn_or_id(id) ((is_notop_id(id)) &amp;&amp; \
  65. (((id)&amp;ID_SCOPE_MASK) == ID_GLOBAL || \
  66. ((id)&amp;ID_SCOPE_MASK) == ID_INSTANCE || \
  67. ((id)&amp;ID_SCOPE_MASK) == ID_CLASS))
  68. NODE *ruby_eval_tree_begin = 0;
  69. NODE *ruby_eval_tree = 0;
  70. char *ruby_sourcefile; /* current source file */
  71. int ruby_sourceline; /* current line no. */
  72. static int yylex();
  73. static int yyerror();
  74. static enum lex_state {
  75. EXPR_BEG, /* ignore newline, +/- is a sign. */
  76. EXPR_END, /* newline significant, +/- is a operator. */
  77. EXPR_ARG, /* newline significant, +/- is a operator. */
  78. EXPR_CMDARG, /* newline significant, +/- is a operator. */
  79. EXPR_ENDARG, /* newline significant, +/- is a operator. */
  80. EXPR_MID, /* newline significant, +/- is a operator. */
  81. EXPR_FNAME, /* ignore newline, no reserved words. */
  82. EXPR_DOT, /* right after `.' or `::', no reserved words. */
  83. EXPR_CLASS, /* immediate after `class', no here document. */
  84. } lex_state;
  85. static NODE *lex_strterm;
  86. #ifdef HAVE_LONG_LONG
  87. typedef unsigned LONG_LONG stack_type;
  88. #else
  89. typedef unsigned long stack_type;
  90. #endif
  91. #define BITSTACK_PUSH(stack, n) (stack = (stack&lt;&lt;1)|((n)&amp;1))
  92. #define BITSTACK_POP(stack) (stack &gt;&gt;= 1)
  93. #define BITSTACK_LEXPOP(stack) (stack = (stack &gt;&gt; 1) | (stack &amp; 1))
  94. #define BITSTACK_SET_P(stack) (stack&amp;1)
  95. static stack_type cond_stack = 0;
  96. #define COND_PUSH(n) BITSTACK_PUSH(cond_stack, n)
  97. #define COND_POP() BITSTACK_POP(cond_stack)
  98. #define COND_LEXPOP() BITSTACK_LEXPOP(cond_stack)
  99. #define COND_P() BITSTACK_SET_P(cond_stack)
  100. static stack_type cmdarg_stack = 0;
  101. #define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, n)
  102. #define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
  103. #define CMDARG_LEXPOP() BITSTACK_LEXPOP(cmdarg_stack)
  104. #define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
  105. static int class_nest = 0;
  106. static int in_single = 0;
  107. static int in_def = 0;
  108. static int compile_for_eval = 0;
  109. static ID cur_mid = 0;
  110. static NODE *cond();
  111. static NODE *logop();
  112. static int cond_negative();
  113. static NODE *newline_node();
  114. static void fixpos();
  115. static int value_expr0();
  116. static void void_expr0();
  117. static void void_stmts();
  118. static NODE *remove_begin();
  119. #define value_expr(node) value_expr0((node) = remove_begin(node))
  120. #define void_expr(node) void_expr0((node) = remove_begin(node))
  121. static NODE *block_append();
  122. static NODE *list_append();
  123. static NODE *list_concat();
  124. static NODE *arg_concat();
  125. static NODE *arg_prepend();
  126. static NODE *literal_concat();
  127. static NODE *new_evstr();
  128. static NODE *evstr2dstr();
  129. static NODE *call_op();
  130. static int in_defined = 0;
  131. static NODE *negate_lit();
  132. static NODE *ret_args();
  133. static NODE *arg_blk_pass();
  134. static NODE *new_call();
  135. static NODE *new_fcall();
  136. static NODE *new_super();
  137. static NODE *new_yield();
  138. static NODE *gettable();
  139. static NODE *assignable();
  140. static NODE *aryset();
  141. static NODE *attrset();
  142. static void rb_backref_error();
  143. static NODE *node_assign();
  144. static NODE *match_gen();
  145. static void local_push();
  146. static void local_pop();
  147. static int local_append();
  148. static int local_cnt();
  149. static int local_id();
  150. static ID *local_tbl();
  151. static ID internal_id();
  152. static struct RVarmap *dyna_push();
  153. static void dyna_pop();
  154. static int dyna_in_block();
  155. static NODE *dyna_init();
  156. static void top_local_init();
  157. static void top_local_setup();
  158. #define RE_OPTION_ONCE 0x80
  159. #define NODE_STRTERM NODE_ZARRAY /* nothing to gc */
  160. #define NODE_HEREDOC NODE_ARRAY /* 1, 3 to gc */
  161. #define SIGN_EXTEND(x,n) (((1&lt;&lt;(n)-1)^((x)&amp;~(~0&lt;&lt;(n))))-(1&lt;&lt;(n)-1))
  162. #define nd_func u1.id
  163. #if SIZEOF_SHORT == 2
  164. #define nd_term(node) ((signed short)(node)-&gt;u2.id)
  165. #else
  166. #define nd_term(node) SIGN_EXTEND((node)-&gt;u2.id, CHAR_BIT*2)
  167. #endif
  168. #define nd_paren(node) (char)((node)-&gt;u2.id &gt;&gt; CHAR_BIT*2)
  169. #define nd_nest u3.id
  170. /* Older versions of Yacc set YYMAXDEPTH to a very low value by default (150,
  171. for instance). This is too low for Ruby to parse some files, such as
  172. date/format.rb, therefore bump the value up to at least Bison's default. */
  173. #ifdef OLD_YACC
  174. #ifndef YYMAXDEPTH
  175. #define YYMAXDEPTH 10000
  176. #endif
  177. #endif
  178. %}
  179. %union {
  180. NODE *node;
  181. ID id;
  182. int num;
  183. struct RVarmap *vars;
  184. }
  185. %token kCLASS
  186. kMODULE
  187. kDEF
  188. kUNDEF
  189. kBEGIN
  190. kRESCUE
  191. kENSURE
  192. kEND
  193. kIF
  194. kUNLESS
  195. kTHEN
  196. kELSIF
  197. kELSE
  198. kCASE
  199. kWHEN
  200. kWHILE
  201. kUNTIL
  202. kFOR
  203. kBREAK
  204. kNEXT
  205. kREDO
  206. kRETRY
  207. kIN
  208. kDO
  209. kDO_COND
  210. kDO_BLOCK
  211. kRETURN
  212. kYIELD
  213. kSUPER
  214. kSELF
  215. kNIL
  216. kTRUE
  217. kFALSE
  218. kAND
  219. kOR
  220. kNOT
  221. kIF_MOD
  222. kUNLESS_MOD
  223. kWHILE_MOD
  224. kUNTIL_MOD
  225. kRESCUE_MOD
  226. kALIAS
  227. kDEFINED
  228. klBEGIN
  229. klEND
  230. k__LINE__
  231. k__FILE__
  232. %token &lt;id&gt; tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR
  233. %token &lt;node&gt; tINTEGER tFLOAT tSTRING_CONTENT
  234. %token &lt;node&gt; tNTH_REF tBACK_REF
  235. %token &lt;num&gt; tREGEXP_END
  236. %type &lt;node&gt; singleton strings string string1 xstring regexp
  237. %type &lt;node&gt; string_contents xstring_contents string_content
  238. %type &lt;node&gt; words qwords word_list qword_list word
  239. %type &lt;node&gt; literal numeric dsym cpath
  240. %type &lt;node&gt; bodystmt compstmt stmts stmt expr arg primary command command_call method_call
  241. %type &lt;node&gt; expr_value arg_value primary_value
  242. %type &lt;node&gt; if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure
  243. %type &lt;node&gt; args when_args call_args call_args2 open_args paren_args opt_paren_args
  244. %type &lt;node&gt; command_args aref_args opt_block_arg block_arg var_ref var_lhs
  245. %type &lt;node&gt; mrhs superclass block_call block_command
  246. %type &lt;node&gt; f_arglist f_args f_optarg f_opt f_block_arg opt_f_block_arg
  247. %type &lt;node&gt; assoc_list assocs assoc undef_list backref string_dvar
  248. %type &lt;node&gt; block_var opt_block_var brace_block cmd_brace_block do_block lhs none
  249. %type &lt;node&gt; mlhs mlhs_head mlhs_basic mlhs_entry mlhs_item mlhs_node
  250. %type &lt;id&gt; fitem variable sym symbol operation operation2 operation3
  251. %type &lt;id&gt; cname fname op f_rest_arg
  252. %type &lt;num&gt; f_norm_arg f_arg
  253. %token tUPLUS /* unary+ */
  254. %token tUMINUS /* unary- */
  255. %token tPOW /* ** */
  256. %token tCMP /* &lt;=&gt; */
  257. %token tEQ /* == */
  258. %token tEQQ /* === */
  259. %token tNEQ /* != */
  260. %token tGEQ /* &gt;= */
  261. %token tLEQ /* &lt;= */
  262. %token tANDOP tOROP /* &amp;&amp; and || */
  263. %token tMATCH tNMATCH /* =~ and !~ */
  264. %token tDOT2 tDOT3 /* .. and ... */
  265. %token tAREF tASET /* [] and []= */
  266. %token tLSHFT tRSHFT /* &lt;&lt; and &gt;&gt; */
  267. %token tCOLON2 /* :: */
  268. %token tCOLON3 /* :: at EXPR_BEG */
  269. %token &lt;id&gt; tOP_ASGN /* +=, -= etc. */
  270. %token tASSOC /* =&gt; */
  271. %token tLPAREN /* ( */
  272. %token tLPAREN_ARG /* ( */
  273. %token tRPAREN /* ) */
  274. %token tLBRACK /* [ */
  275. %token tLBRACE /* { */
  276. %token tLBRACE_ARG /* { */
  277. %token tSTAR /* * */
  278. %token tAMPER /* &amp; */
  279. %token tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG
  280. %token tSTRING_DBEG tSTRING_DVAR tSTRING_END
  281. /*
  282. * precedence table
  283. */
  284. %nonassoc tLOWEST
  285. %nonassoc tLBRACE_ARG
  286. %nonassoc kIF_MOD kUNLESS_MOD kWHILE_MOD kUNTIL_MOD
  287. %left kOR kAND
  288. %right kNOT
  289. %nonassoc kDEFINED
  290. %right '=' tOP_ASGN
  291. %left kRESCUE_MOD
  292. %right '?' ':'
  293. %nonassoc tDOT2 tDOT3
  294. %left tOROP
  295. %left tANDOP
  296. %nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
  297. %left '&gt;' tGEQ '&lt;' tLEQ
  298. %left '|' '^'
  299. %left '&amp;'
  300. %left tLSHFT tRSHFT
  301. %left '+' '-'
  302. %left '*' '/' '%'
  303. %right tUMINUS_NUM tUMINUS
  304. %right tPOW
  305. %right '!' '~' tUPLUS
  306. %token tLAST_TOKEN
  307. %%
  308. program : {
  309. lex_state = EXPR_BEG;
  310. top_local_init();
  311. if (ruby_class == rb_cObject) class_nest = 0;
  312. else class_nest = 1;
  313. }
  314. compstmt
  315. {
  316. if ($2 &amp;&amp; !compile_for_eval) {
  317. /* last expression should not be void */
  318. if (nd_type($2) != NODE_BLOCK) void_expr($2);
  319. else {
  320. NODE *node = $2;
  321. while (node-&gt;nd_next) {
  322. node = node-&gt;nd_next;
  323. }
  324. void_expr(node-&gt;nd_head);
  325. }
  326. }
  327. ruby_eval_tree = block_append(ruby_eval_tree, $2);
  328. top_local_setup();
  329. class_nest = 0;
  330. }
  331. ;
  332. bodystmt : compstmt
  333. opt_rescue
  334. opt_else
  335. opt_ensure
  336. {
  337. $$ = $1;
  338. if ($2) {
  339. $$ = NEW_RESCUE($1, $2, $3);
  340. }
  341. else if ($3) {
  342. rb_warn(&quot;else without rescue is useless&quot;);
  343. $$ = block_append($$, $3);
  344. }
  345. if ($4) {
  346. $$ = NEW_ENSURE($$, $4);
  347. }
  348. fixpos($$, $1);
  349. }
  350. ;
  351. compstmt : stmts opt_terms
  352. {
  353. void_stmts($1);
  354. $$ = $1;
  355. }
  356. ;
  357. stmts : none
  358. | stmt
  359. {
  360. $$ = newline_node($1);
  361. }
  362. | stmts terms stmt
  363. {
  364. $$ = block_append($1, newline_node($3));
  365. }
  366. | error stmt
  367. {
  368. $$ = $2;
  369. }
  370. ;
  371. stmt : kALIAS fitem {lex_state = EXPR_FNAME;} fitem
  372. {
  373. $$ = NEW_ALIAS($2, $4);
  374. }
  375. | kALIAS tGVAR tGVAR
  376. {
  377. $$ = NEW_VALIAS($2, $3);
  378. }
  379. | kALIAS tGVAR tBACK_REF
  380. {
  381. char buf[3];
  382. sprintf(buf, &quot;$%c&quot;, (char)$3-&gt;nd_nth);
  383. $$ = NEW_VALIAS($2, rb_intern(buf));
  384. }
  385. | kALIAS tGVAR tNTH_REF
  386. {
  387. yyerror(&quot;can't make alias for the number variables&quot;);
  388. $$ = 0;
  389. }
  390. | kUNDEF undef_list
  391. {
  392. $$ = $2;
  393. }
  394. | stmt kIF_MOD expr_value
  395. {
  396. $$ = NEW_IF(cond($3), $1, 0);
  397. fixpos($$, $3);
  398. if (cond_negative(&amp;$$-&gt;nd_cond)) {
  399. $$-&gt;nd_else = $$-&gt;nd_body;
  400. $$-&gt;nd_body = 0;
  401. }
  402. }
  403. | stmt kUNLESS_MOD expr_value
  404. {
  405. $$ = NEW_UNLESS(cond($3), $1, 0);
  406. fixpos($$, $3);
  407. if (cond_negative(&amp;$$-&gt;nd_cond)) {
  408. $$-&gt;nd_body = $$-&gt;nd_else;
  409. $$-&gt;nd_else = 0;
  410. }
  411. }
  412. | stmt kWHILE_MOD expr_value
  413. {
  414. if ($1 &amp;&amp; nd_type($1) == NODE_BEGIN) {
  415. $$ = NEW_WHILE(cond($3), $1-&gt;nd_body, 0);
  416. }
  417. else {
  418. $$ = NEW_WHILE(cond($3), $1, 1);
  419. }
  420. if (cond_negative(&amp;$$-&gt;nd_cond)) {
  421. nd_set_type($$, NODE_UNTIL);
  422. }
  423. }
  424. | stmt kUNTIL_MOD expr_value
  425. {
  426. if ($1 &amp;&amp; nd_type($1) == NODE_BEGIN) {
  427. $$ = NEW_UNTIL(cond($3), $1-&gt;nd_body, 0);
  428. }
  429. else {
  430. $$ = NEW_UNTIL(cond($3), $1, 1);
  431. }
  432. if (cond_negative(&amp;$$-&gt;nd_cond)) {
  433. nd_set_type($$, NODE_WHILE);
  434. }
  435. }
  436. | stmt kRESCUE_MOD stmt
  437. {
  438. $$ = NEW_RESCUE($1, NEW_RESBODY(0,$3,0), 0);
  439. }
  440. | klBEGIN
  441. {
  442. if (in_def || in_single) {
  443. yyerror(&quot;BEGIN in method&quot;);
  444. }
  445. local_push(0);
  446. }
  447. '{' compstmt '}'
  448. {
  449. ruby_eval_tree_begin = block_append(ruby_eval_tree_begin,
  450. NEW_PREEXE($4));
  451. local_pop();
  452. $$ = 0;
  453. }
  454. | klEND '{' compstmt '}'
  455. {
  456. if (in_def || in_single) {
  457. rb_warn(&quot;END in method; use at_exit&quot;);
  458. }
  459. $$ = NEW_ITER(0, NEW_POSTEXE(), $3);
  460. }
  461. | lhs '=' command_call
  462. {
  463. $$ = node_assign($1, $3);
  464. }
  465. | mlhs '=' command_call
  466. {
  467. value_expr($3);
  468. $1-&gt;nd_value = ($1-&gt;nd_head) ? NEW_TO_ARY($3) : NEW_ARRAY($3);
  469. $$ = $1;
  470. }
  471. | var_lhs tOP_ASGN command_call
  472. {
  473. value_expr($3);
  474. if ($1) {
  475. ID vid = $1-&gt;nd_vid;
  476. if ($2 == tOROP) {
  477. $1-&gt;nd_value = $3;
  478. $$ = NEW_OP_ASGN_OR(gettable(vid), $1);
  479. if (is_asgn_or_id(vid)) {
  480. $$-&gt;nd_aid = vid;
  481. }
  482. }
  483. else if ($2 == tANDOP) {
  484. $1-&gt;nd_value = $3;
  485. $$ = NEW_OP_ASGN_AND(gettable(vid), $1);
  486. }
  487. else {
  488. $$ = $1;
  489. $$-&gt;nd_value = call_op(gettable(vid),$2,1,$3);
  490. }
  491. }
  492. else {
  493. $$ = 0;
  494. }
  495. }
  496. | primary_value '[' aref_args ']' tOP_ASGN command_call
  497. {
  498. NODE *args;
  499. value_expr($6);
  500. args = NEW_LIST($6);
  501. if ($3 &amp;&amp; nd_type($3) != NODE_ARRAY)
  502. $3 = NEW_LIST($3);
  503. $3 = list_append($3, NEW_NIL());
  504. list_concat(args, $3);
  505. if ($5 == tOROP) {
  506. $5 = 0;
  507. }
  508. else if ($5 == tANDOP) {
  509. $5 = 1;
  510. }
  511. $$ = NEW_OP_ASGN1($1, $5, args);
  512. fixpos($$, $1);
  513. }
  514. | primary_value '.' tIDENTIFIER tOP_ASGN command_call
  515. {
  516. value_expr($5);
  517. if ($4 == tOROP) {
  518. $4 = 0;
  519. }
  520. else if ($4 == tANDOP) {
  521. $4 = 1;
  522. }
  523. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  524. fixpos($$, $1);
  525. }
  526. | primary_value '.' tCONSTANT tOP_ASGN command_call
  527. {
  528. value_expr($5);
  529. if ($4 == tOROP) {
  530. $4 = 0;
  531. }
  532. else if ($4 == tANDOP) {
  533. $4 = 1;
  534. }
  535. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  536. fixpos($$, $1);
  537. }
  538. | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_call
  539. {
  540. value_expr($5);
  541. if ($4 == tOROP) {
  542. $4 = 0;
  543. }
  544. else if ($4 == tANDOP) {
  545. $4 = 1;
  546. }
  547. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  548. fixpos($$, $1);
  549. }
  550. | backref tOP_ASGN command_call
  551. {
  552. rb_backref_error($1);
  553. $$ = 0;
  554. }
  555. | lhs '=' mrhs
  556. {
  557. $$ = node_assign($1, NEW_SVALUE($3));
  558. }
  559. | mlhs '=' arg_value
  560. {
  561. $1-&gt;nd_value = ($1-&gt;nd_head) ? NEW_TO_ARY($3) : NEW_ARRAY($3);
  562. $$ = $1;
  563. }
  564. | mlhs '=' mrhs
  565. {
  566. $1-&gt;nd_value = $3;
  567. $$ = $1;
  568. }
  569. | expr
  570. ;
  571. expr : command_call
  572. | expr kAND expr
  573. {
  574. $$ = logop(NODE_AND, $1, $3);
  575. }
  576. | expr kOR expr
  577. {
  578. $$ = logop(NODE_OR, $1, $3);
  579. }
  580. | kNOT expr
  581. {
  582. $$ = NEW_NOT(cond($2));
  583. }
  584. | '!' command_call
  585. {
  586. $$ = NEW_NOT(cond($2));
  587. }
  588. | arg
  589. ;
  590. expr_value : expr
  591. {
  592. value_expr($$);
  593. $$ = $1;
  594. }
  595. ;
  596. command_call : command
  597. | block_command
  598. | kRETURN call_args
  599. {
  600. $$ = NEW_RETURN(ret_args($2));
  601. }
  602. | kBREAK call_args
  603. {
  604. $$ = NEW_BREAK(ret_args($2));
  605. }
  606. | kNEXT call_args
  607. {
  608. $$ = NEW_NEXT(ret_args($2));
  609. }
  610. ;
  611. block_command : block_call
  612. | block_call '.' operation2 command_args
  613. {
  614. $$ = new_call($1, $3, $4);
  615. }
  616. | block_call tCOLON2 operation2 command_args
  617. {
  618. $$ = new_call($1, $3, $4);
  619. }
  620. ;
  621. cmd_brace_block : tLBRACE_ARG
  622. {
  623. $&lt;vars&gt;$ = dyna_push();
  624. $&lt;num&gt;1 = ruby_sourceline;
  625. }
  626. opt_block_var {$&lt;vars&gt;$ = ruby_dyna_vars;}
  627. compstmt
  628. '}'
  629. {
  630. $$ = NEW_ITER($3, 0, dyna_init($5, $&lt;vars&gt;4));
  631. nd_set_line($$, $&lt;num&gt;1);
  632. dyna_pop($&lt;vars&gt;2);
  633. }
  634. ;
  635. command : operation command_args %prec tLOWEST
  636. {
  637. $$ = new_fcall($1, $2);
  638. fixpos($$, $2);
  639. }
  640. | operation command_args cmd_brace_block
  641. {
  642. $$ = new_fcall($1, $2);
  643. if ($3) {
  644. if (nd_type($$) == NODE_BLOCK_PASS) {
  645. rb_compile_error(&quot;both block arg and actual block given&quot;);
  646. }
  647. $3-&gt;nd_iter = $$;
  648. $$ = $3;
  649. }
  650. fixpos($$, $2);
  651. }
  652. | primary_value '.' operation2 command_args %prec tLOWEST
  653. {
  654. $$ = new_call($1, $3, $4);
  655. fixpos($$, $1);
  656. }
  657. | primary_value '.' operation2 command_args cmd_brace_block
  658. {
  659. $$ = new_call($1, $3, $4);
  660. if ($5) {
  661. if (nd_type($$) == NODE_BLOCK_PASS) {
  662. rb_compile_error(&quot;both block arg and actual block given&quot;);
  663. }
  664. $5-&gt;nd_iter = $$;
  665. $$ = $5;
  666. }
  667. fixpos($$, $1);
  668. }
  669. | primary_value tCOLON2 operation2 command_args %prec tLOWEST
  670. {
  671. $$ = new_call($1, $3, $4);
  672. fixpos($$, $1);
  673. }
  674. | primary_value tCOLON2 operation2 command_args cmd_brace_block
  675. {
  676. $$ = new_call($1, $3, $4);
  677. if ($5) {
  678. if (nd_type($$) == NODE_BLOCK_PASS) {
  679. rb_compile_error(&quot;both block arg and actual block given&quot;);
  680. }
  681. $5-&gt;nd_iter = $$;
  682. $$ = $5;
  683. }
  684. fixpos($$, $1);
  685. }
  686. | kSUPER command_args
  687. {
  688. $$ = new_super($2);
  689. fixpos($$, $2);
  690. }
  691. | kYIELD command_args
  692. {
  693. $$ = new_yield($2);
  694. fixpos($$, $2);
  695. }
  696. ;
  697. mlhs : mlhs_basic
  698. | tLPAREN mlhs_entry ')'
  699. {
  700. $$ = $2;
  701. }
  702. ;
  703. mlhs_entry : mlhs_basic
  704. | tLPAREN mlhs_entry ')'
  705. {
  706. $$ = NEW_MASGN(NEW_LIST($2), 0);
  707. }
  708. ;
  709. mlhs_basic : mlhs_head
  710. {
  711. $$ = NEW_MASGN($1, 0);
  712. }
  713. | mlhs_head mlhs_item
  714. {
  715. $$ = NEW_MASGN(list_append($1,$2), 0);
  716. }
  717. | mlhs_head tSTAR mlhs_node
  718. {
  719. $$ = NEW_MASGN($1, $3);
  720. }
  721. | mlhs_head tSTAR
  722. {
  723. $$ = NEW_MASGN($1, -1);
  724. }
  725. | tSTAR mlhs_node
  726. {
  727. $$ = NEW_MASGN(0, $2);
  728. }
  729. | tSTAR
  730. {
  731. $$ = NEW_MASGN(0, -1);
  732. }
  733. ;
  734. mlhs_item : mlhs_node
  735. | tLPAREN mlhs_entry ')'
  736. {
  737. $$ = $2;
  738. }
  739. ;
  740. mlhs_head : mlhs_item ','
  741. {
  742. $$ = NEW_LIST($1);
  743. }
  744. | mlhs_head mlhs_item ','
  745. {
  746. $$ = list_append($1, $2);
  747. }
  748. ;
  749. mlhs_node : variable
  750. {
  751. $$ = assignable($1, 0);
  752. }
  753. | primary_value '[' aref_args ']'
  754. {
  755. $$ = aryset($1, $3);
  756. }
  757. | primary_value '.' tIDENTIFIER
  758. {
  759. $$ = attrset($1, $3);
  760. }
  761. | primary_value tCOLON2 tIDENTIFIER
  762. {
  763. $$ = attrset($1, $3);
  764. }
  765. | primary_value '.' tCONSTANT
  766. {
  767. $$ = attrset($1, $3);
  768. }
  769. | primary_value tCOLON2 tCONSTANT
  770. {
  771. if (in_def || in_single)
  772. yyerror(&quot;dynamic constant assignment&quot;);
  773. $$ = NEW_CDECL(0, 0, NEW_COLON2($1, $3));
  774. }
  775. | tCOLON3 tCONSTANT
  776. {
  777. if (in_def || in_single)
  778. yyerror(&quot;dynamic constant assignment&quot;);
  779. $$ = NEW_CDECL(0, 0, NEW_COLON3($2));
  780. }
  781. | backref
  782. {
  783. rb_backref_error($1);
  784. $$ = 0;
  785. }
  786. ;
  787. lhs : variable
  788. {
  789. $$ = assignable($1, 0);
  790. }
  791. | primary_value '[' aref_args ']'
  792. {
  793. $$ = aryset($1, $3);
  794. }
  795. | primary_value '.' tIDENTIFIER
  796. {
  797. $$ = attrset($1, $3);
  798. }
  799. | primary_value tCOLON2 tIDENTIFIER
  800. {
  801. $$ = attrset($1, $3);
  802. }
  803. | primary_value '.' tCONSTANT
  804. {
  805. $$ = attrset($1, $3);
  806. }
  807. | primary_value tCOLON2 tCONSTANT
  808. {
  809. if (in_def || in_single)
  810. yyerror(&quot;dynamic constant assignment&quot;);
  811. $$ = NEW_CDECL(0, 0, NEW_COLON2($1, $3));
  812. }
  813. | tCOLON3 tCONSTANT
  814. {
  815. if (in_def || in_single)
  816. yyerror(&quot;dynamic constant assignment&quot;);
  817. $$ = NEW_CDECL(0, 0, NEW_COLON3($2));
  818. }
  819. | backref
  820. {
  821. rb_backref_error($1);
  822. $$ = 0;
  823. }
  824. ;
  825. cname : tIDENTIFIER
  826. {
  827. yyerror(&quot;class/module name must be CONSTANT&quot;);
  828. }
  829. | tCONSTANT
  830. ;
  831. cpath : tCOLON3 cname
  832. {
  833. $$ = NEW_COLON3($2);
  834. }
  835. | cname
  836. {
  837. $$ = NEW_COLON2(0, $$);
  838. }
  839. | primary_value tCOLON2 cname
  840. {
  841. $$ = NEW_COLON2($1, $3);
  842. }
  843. ;
  844. fname : tIDENTIFIER
  845. | tCONSTANT
  846. | tFID
  847. | op
  848. {
  849. lex_state = EXPR_END;
  850. $$ = $1;
  851. }
  852. | reswords
  853. {
  854. lex_state = EXPR_END;
  855. $$ = $&lt;id&gt;1;
  856. }
  857. ;
  858. fitem : fname
  859. | symbol
  860. ;
  861. undef_list : fitem
  862. {
  863. $$ = NEW_UNDEF($1);
  864. }
  865. | undef_list ',' {lex_state = EXPR_FNAME;} fitem
  866. {
  867. $$ = block_append($1, NEW_UNDEF($4));
  868. }
  869. ;
  870. op : '|' { $$ = '|'; }
  871. | '^' { $$ = '^'; }
  872. | '&amp;' { $$ = '&amp;'; }
  873. | tCMP { $$ = tCMP; }
  874. | tEQ { $$ = tEQ; }
  875. | tEQQ { $$ = tEQQ; }
  876. | tMATCH { $$ = tMATCH; }
  877. | '&gt;' { $$ = '&gt;'; }
  878. | tGEQ { $$ = tGEQ; }
  879. | '&lt;' { $$ = '&lt;'; }
  880. | tLEQ { $$ = tLEQ; }
  881. | tLSHFT { $$ = tLSHFT; }
  882. | tRSHFT { $$ = tRSHFT; }
  883. | '+' { $$ = '+'; }
  884. | '-' { $$ = '-'; }
  885. | '*' { $$ = '*'; }
  886. | tSTAR { $$ = '*'; }
  887. | '/' { $$ = '/'; }
  888. | '%' { $$ = '%'; }
  889. | tPOW { $$ = tPOW; }
  890. | '~' { $$ = '~'; }
  891. | tUPLUS { $$ = tUPLUS; }
  892. | tUMINUS { $$ = tUMINUS; }
  893. | tAREF { $$ = tAREF; }
  894. | tASET { $$ = tASET; }
  895. | '`' { $$ = '`'; }
  896. ;
  897. reswords : k__LINE__ | k__FILE__ | klBEGIN | klEND
  898. | kALIAS | kAND | kBEGIN | kBREAK | kCASE | kCLASS | kDEF
  899. | kDEFINED | kDO | kELSE | kELSIF | kEND | kENSURE | kFALSE
  900. | kFOR | kIN | kMODULE | kNEXT | kNIL | kNOT
  901. | kOR | kREDO | kRESCUE | kRETRY | kRETURN | kSELF | kSUPER
  902. | kTHEN | kTRUE | kUNDEF | kWHEN | kYIELD
  903. | kIF_MOD | kUNLESS_MOD | kWHILE_MOD | kUNTIL_MOD | kRESCUE_MOD
  904. ;
  905. arg : lhs '=' arg
  906. {
  907. $$ = node_assign($1, $3);
  908. }
  909. | lhs '=' arg kRESCUE_MOD arg
  910. {
  911. $$ = node_assign($1, NEW_RESCUE($3, NEW_RESBODY(0,$5,0), 0));
  912. }
  913. | var_lhs tOP_ASGN arg
  914. {
  915. value_expr($3);
  916. if ($1) {
  917. ID vid = $1-&gt;nd_vid;
  918. if ($2 == tOROP) {
  919. $1-&gt;nd_value = $3;
  920. $$ = NEW_OP_ASGN_OR(gettable(vid), $1);
  921. if (is_asgn_or_id(vid)) {
  922. $$-&gt;nd_aid = vid;
  923. }
  924. }
  925. else if ($2 == tANDOP) {
  926. $1-&gt;nd_value = $3;
  927. $$ = NEW_OP_ASGN_AND(gettable(vid), $1);
  928. }
  929. else {
  930. $$ = $1;
  931. $$-&gt;nd_value = call_op(gettable(vid),$2,1,$3);
  932. }
  933. }
  934. else {
  935. $$ = 0;
  936. }
  937. }
  938. | primary_value '[' aref_args ']' tOP_ASGN arg
  939. {
  940. NODE *args;
  941. value_expr($6);
  942. args = NEW_LIST($6);
  943. if ($3 &amp;&amp; nd_type($3) != NODE_ARRAY)
  944. $3 = NEW_LIST($3);
  945. $3 = list_append($3, NEW_NIL());
  946. list_concat(args, $3);
  947. if ($5 == tOROP) {
  948. $5 = 0;
  949. }
  950. else if ($5 == tANDOP) {
  951. $5 = 1;
  952. }
  953. $$ = NEW_OP_ASGN1($1, $5, args);
  954. fixpos($$, $1);
  955. }
  956. | primary_value '.' tIDENTIFIER tOP_ASGN arg
  957. {
  958. value_expr($5);
  959. if ($4 == tOROP) {
  960. $4 = 0;
  961. }
  962. else if ($4 == tANDOP) {
  963. $4 = 1;
  964. }
  965. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  966. fixpos($$, $1);
  967. }
  968. | primary_value '.' tCONSTANT tOP_ASGN arg
  969. {
  970. value_expr($5);
  971. if ($4 == tOROP) {
  972. $4 = 0;
  973. }
  974. else if ($4 == tANDOP) {
  975. $4 = 1;
  976. }
  977. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  978. fixpos($$, $1);
  979. }
  980. | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg
  981. {
  982. value_expr($5);
  983. if ($4 == tOROP) {
  984. $4 = 0;
  985. }
  986. else if ($4 == tANDOP) {
  987. $4 = 1;
  988. }
  989. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  990. fixpos($$, $1);
  991. }
  992. | primary_value tCOLON2 tCONSTANT tOP_ASGN arg
  993. {
  994. yyerror(&quot;constant re-assignment&quot;);
  995. $$ = 0;
  996. }
  997. | tCOLON3 tCONSTANT tOP_ASGN arg
  998. {
  999. yyerror(&quot;constant re-assignment&quot;);
  1000. $$ = 0;
  1001. }
  1002. | backref tOP_ASGN arg
  1003. {
  1004. rb_backref_error($1);
  1005. $$ = 0;
  1006. }
  1007. | arg tDOT2 arg
  1008. {
  1009. value_expr($1);
  1010. value_expr($3);
  1011. $$ = NEW_DOT2($1, $3);
  1012. }
  1013. | arg tDOT3 arg
  1014. {
  1015. value_expr($1);
  1016. value_expr($3);
  1017. $$ = NEW_DOT3($1, $3);
  1018. }
  1019. | arg '+' arg
  1020. {
  1021. $$ = call_op($1, '+', 1, $3);
  1022. }
  1023. | arg '-' arg
  1024. {
  1025. $$ = call_op($1, '-', 1, $3);
  1026. }
  1027. | arg '*' arg
  1028. {
  1029. $$ = call_op($1, '*', 1, $3);
  1030. }
  1031. | arg '/' arg
  1032. {
  1033. $$ = call_op($1, '/', 1, $3);
  1034. }
  1035. | arg '%' arg
  1036. {
  1037. $$ = call_op($1, '%', 1, $3);
  1038. }
  1039. | arg tPOW arg
  1040. {
  1041. $$ = call_op($1, tPOW, 1, $3);
  1042. }
  1043. | tUMINUS_NUM tINTEGER tPOW arg
  1044. {
  1045. $$ = call_op(call_op($2, tPOW, 1, $4), tUMINUS, 0, 0);
  1046. }
  1047. | tUMINUS_NUM tFLOAT tPOW arg
  1048. {
  1049. $$ = call_op(call_op($2, tPOW, 1, $4), tUMINUS, 0, 0);
  1050. }
  1051. | tUPLUS arg
  1052. {
  1053. if ($2 &amp;&amp; nd_type($2) == NODE_LIT) {
  1054. $$ = $2;
  1055. }
  1056. else {
  1057. $$ = call_op($2, tUPLUS, 0, 0);
  1058. }
  1059. }
  1060. | tUMINUS arg
  1061. {
  1062. $$ = call_op($2, tUMINUS, 0, 0);
  1063. }
  1064. | arg '|' arg
  1065. {
  1066. $$ = call_op($1, '|', 1, $3);
  1067. }
  1068. | arg '^' arg
  1069. {
  1070. $$ = call_op($1, '^', 1, $3);
  1071. }
  1072. | arg '&amp;' arg
  1073. {
  1074. $$ = call_op($1, '&amp;', 1, $3);
  1075. }
  1076. | arg tCMP arg
  1077. {
  1078. $$ = call_op($1, tCMP, 1, $3);
  1079. }
  1080. | arg '&gt;' arg
  1081. {
  1082. $$ = call_op($1, '&gt;', 1, $3);
  1083. }
  1084. | arg tGEQ arg
  1085. {
  1086. $$ = call_op($1, tGEQ, 1, $3);
  1087. }
  1088. | arg '&lt;' arg
  1089. {
  1090. $$ = call_op($1, '&lt;', 1, $3);
  1091. }
  1092. | arg tLEQ arg
  1093. {
  1094. $$ = call_op($1, tLEQ, 1, $3);
  1095. }
  1096. | arg tEQ arg
  1097. {
  1098. $$ = call_op($1, tEQ, 1, $3);
  1099. }
  1100. | arg tEQQ arg
  1101. {
  1102. $$ = call_op($1, tEQQ, 1, $3);
  1103. }
  1104. | arg tNEQ arg
  1105. {
  1106. $$ = NEW_NOT(call_op($1, tEQ, 1, $3));
  1107. }
  1108. | arg tMATCH arg
  1109. {
  1110. $$ = match_gen($1, $3);
  1111. }
  1112. | arg tNMATCH arg
  1113. {
  1114. $$ = NEW_NOT(match_gen($1, $3));
  1115. }
  1116. | '!' arg
  1117. {
  1118. $$ = NEW_NOT(cond($2));
  1119. }
  1120. | '~' arg
  1121. {
  1122. $$ = call_op($2, '~', 0, 0);
  1123. }
  1124. | arg tLSHFT arg
  1125. {
  1126. $$ = call_op($1, tLSHFT, 1, $3);
  1127. }
  1128. | arg tRSHFT arg
  1129. {
  1130. $$ = call_op($1, tRSHFT, 1, $3);
  1131. }
  1132. | arg tANDOP arg
  1133. {
  1134. $$ = logop(NODE_AND, $1, $3);
  1135. }
  1136. | arg tOROP arg
  1137. {
  1138. $$ = logop(NODE_OR, $1, $3);
  1139. }
  1140. | kDEFINED opt_nl {in_defined = 1;} arg
  1141. {
  1142. in_defined = 0;
  1143. $$ = NEW_DEFINED($4);
  1144. }
  1145. | arg '?' arg ':' arg
  1146. {
  1147. $$ = NEW_IF(cond($1), $3, $5);
  1148. fixpos($$, $1);
  1149. }
  1150. | primary
  1151. {
  1152. $$ = $1;
  1153. }
  1154. ;
  1155. arg_value : arg
  1156. {
  1157. value_expr($1);
  1158. $$ = $1;
  1159. }
  1160. ;
  1161. aref_args : none
  1162. | command opt_nl
  1163. {
  1164. rb_warn(&quot;parenthesize argument(s) for future version&quot;);
  1165. $$ = NEW_LIST($1);
  1166. }
  1167. | args trailer
  1168. {
  1169. $$ = $1;
  1170. }
  1171. | args ',' tSTAR arg opt_nl
  1172. {
  1173. value_expr($4);
  1174. $$ = arg_concat($1, $4);
  1175. }
  1176. | assocs trailer
  1177. {
  1178. $$ = NEW_LIST(NEW_HASH($1));
  1179. }
  1180. | tSTAR arg opt_nl
  1181. {
  1182. value_expr($2);
  1183. $$ = NEW_NEWLINE(NEW_SPLAT($2));
  1184. }
  1185. ;
  1186. paren_args : '(' none ')'
  1187. {
  1188. $$ = $2;
  1189. }
  1190. | '(' call_args opt_nl ')'
  1191. {
  1192. $$ = $2;
  1193. }
  1194. | '(' block_call opt_nl ')'
  1195. {
  1196. rb_warn(&quot;parenthesize argument for future version&quot;);
  1197. $$ = NEW_LIST($2);
  1198. }
  1199. | '(' args ',' block_call opt_nl ')'
  1200. {
  1201. rb_warn(&quot;parenthesize argument for future version&quot;);
  1202. $$ = list_append($2, $4);
  1203. }
  1204. ;
  1205. opt_paren_args : none
  1206. | paren_args
  1207. ;
  1208. call_args : command
  1209. {
  1210. rb_warn(&quot;parenthesize argument(s) for future version&quot;);
  1211. $$ = NEW_LIST($1);
  1212. }
  1213. | args opt_block_arg
  1214. {
  1215. $$ = arg_blk_pass($1, $2);
  1216. }
  1217. | args ',' tSTAR arg_value opt_block_arg
  1218. {
  1219. $$ = arg_concat($1, $4);
  1220. $$ = arg_blk_pass($$, $5);
  1221. }
  1222. | assocs opt_block_arg
  1223. {
  1224. $$ = NEW_LIST(NEW_HASH($1));
  1225. $$ = arg_blk_pass($$, $2);
  1226. }
  1227. | assocs ',' tSTAR arg_value opt_block_arg
  1228. {
  1229. $$ = arg_concat(NEW_LIST(NEW_HASH($1)), $4);
  1230. $$ = arg_blk_pass($$, $5);
  1231. }
  1232. | args ',' assocs opt_block_arg
  1233. {
  1234. $$ = list_append($1, NEW_HASH($3));
  1235. $$ = arg_blk_pass($$, $4);
  1236. }
  1237. | args ',' assocs ',' tSTAR arg opt_block_arg
  1238. {
  1239. value_expr($6);
  1240. $$ = arg_concat(list_append($1, NEW_HASH($3)), $6);
  1241. $$ = arg_blk_pass($$, $7);
  1242. }
  1243. | tSTAR arg_value opt_block_arg
  1244. {
  1245. $$ = arg_blk_pass(NEW_SPLAT($2), $3);
  1246. }
  1247. | block_arg
  1248. ;
  1249. call_args2 : arg_value ',' args opt_block_arg
  1250. {
  1251. $$ = arg_blk_pass(list_concat(NEW_LIST($1),$3), $4);
  1252. }
  1253. | arg_value ',' block_arg
  1254. {
  1255. $$ = arg_blk_pass($1, $3);
  1256. }
  1257. | arg_value ',' tSTAR arg_value opt_block_arg
  1258. {
  1259. $$ = arg_concat(NEW_LIST($1), $4);
  1260. $$ = arg_blk_pass($$, $5);
  1261. }
  1262. | arg_value ',' args ',' tSTAR arg_value opt_block_arg
  1263. {
  1264. $$ = arg_concat(list_concat(NEW_LIST($1),$3), $6);
  1265. $$ = arg_blk_pass($$, $7);
  1266. }
  1267. | assocs opt_block_arg
  1268. {
  1269. $$ = NEW_LIST(NEW_HASH($1));
  1270. $$ = arg_blk_pass($$, $2);
  1271. }
  1272. | assocs ',' tSTAR arg_value opt_block_arg
  1273. {
  1274. $$ = arg_concat(NEW_LIST(NEW_HASH($1)), $4);
  1275. $$ = arg_blk_pass($$, $5);
  1276. }
  1277. | arg_value ',' assocs opt_block_arg
  1278. {
  1279. $$ = list_append(NEW_LIST($1), NEW_HASH($3));
  1280. $$ = arg_blk_pass($$, $4);
  1281. }
  1282. | arg_value ',' args ',' assocs opt_block_arg
  1283. {
  1284. $$ = list_append(list_concat(NEW_LIST($1),$3), NEW_HASH($5));
  1285. $$ = arg_blk_pass($$, $6);
  1286. }
  1287. | arg_value ',' assocs ',' tSTAR arg_value opt_block_arg
  1288. {
  1289. $$ = arg_concat(list_append(NEW_LIST($1), NEW_HASH($3)), $6);
  1290. $$ = arg_blk_pass($$, $7);
  1291. }
  1292. | arg_value ',' args ',' assocs ',' tSTAR arg_value opt_block_arg
  1293. {
  1294. $$ = arg_concat(list_append(list_concat(NEW_LIST($1), $3), NEW_HASH($5)), $8);
  1295. $$ = arg_blk_pass($$, $9);
  1296. }
  1297. | tSTAR arg_value opt_block_arg
  1298. {
  1299. $$ = arg_blk_pass(NEW_SPLAT($2), $3);
  1300. }
  1301. | block_arg
  1302. ;
  1303. command_args : {
  1304. $&lt;num&gt;$ = cmdarg_stack;
  1305. CMDARG_PUSH(1);
  1306. }
  1307. open_args
  1308. {
  1309. /* CMDARG_POP() */
  1310. cmdarg_stack = $&lt;num&gt;1;
  1311. $$ = $2;
  1312. }
  1313. ;
  1314. open_args : call_args
  1315. | tLPAREN_ARG {lex_state = EXPR_ENDARG;} ')'
  1316. {
  1317. rb_warn(&quot;don't put space before argument parentheses&quot;);
  1318. $$ = 0;
  1319. }
  1320. | tLPAREN_ARG call_args2 {lex_state = EXPR_ENDARG;} ')'
  1321. {
  1322. rb_warn(&quot;don't put space before argument parentheses&quot;);
  1323. $$ = $2;
  1324. }
  1325. ;
  1326. block_arg : tAMPER arg_value
  1327. {
  1328. $$ = NEW_BLOCK_PASS($2);
  1329. }
  1330. ;
  1331. opt_block_arg : ',' block_arg
  1332. {
  1333. $$ = $2;
  1334. }
  1335. | none
  1336. ;
  1337. args : arg_value
  1338. {
  1339. $$ = NEW_LIST($1);
  1340. }
  1341. | args ',' arg_value
  1342. {
  1343. $$ = list_append($1, $3);
  1344. }
  1345. ;
  1346. mrhs : args ',' arg_value
  1347. {
  1348. $$ = list_append($1, $3);
  1349. }
  1350. | args ',' tSTAR arg_value
  1351. {
  1352. $$ = arg_concat($1, $4);
  1353. }
  1354. | tSTAR arg_value
  1355. {
  1356. $$ = NEW_SPLAT($2);
  1357. }
  1358. ;
  1359. primary : literal
  1360. | strings
  1361. | xstring
  1362. | regexp
  1363. | words
  1364. | qwords
  1365. | var_ref
  1366. | backref
  1367. | tFID
  1368. {
  1369. $$ = NEW_FCALL($1, 0);
  1370. }
  1371. | kBEGIN
  1372. {
  1373. $&lt;num&gt;1 = ruby_sourceline;
  1374. }
  1375. bodystmt
  1376. kEND
  1377. {
  1378. if ($3 == NULL)
  1379. $$ = NEW_NIL();
  1380. else
  1381. $$ = NEW_BEGIN($3);
  1382. nd_set_line($$, $&lt;num&gt;1);
  1383. }
  1384. | tLPAREN_ARG expr {lex_state = EXPR_ENDARG;} opt_nl ')'
  1385. {
  1386. rb_warning(&quot;(...) interpreted as grouped expression&quot;);
  1387. $$ = $2;
  1388. }
  1389. | tLPAREN compstmt ')'
  1390. {
  1391. $$ = $2;
  1392. }
  1393. | primary_value tCOLON2 tCONSTANT
  1394. {
  1395. $$ = NEW_COLON2($1, $3);
  1396. }
  1397. | tCOLON3 tCONSTANT
  1398. {
  1399. $$ = NEW_COLON3($2);
  1400. }
  1401. | primary_value '[' aref_args ']'
  1402. {
  1403. if ($1 &amp;&amp; nd_type($1) == NODE_SELF)
  1404. $$ = NEW_FCALL(tAREF, $3);
  1405. else
  1406. $$ = NEW_CALL($1, tAREF, $3);
  1407. fixpos($$, $1);
  1408. }
  1409. | tLBRACK aref_args ']'
  1410. {
  1411. if ($2 == 0) {
  1412. $$ = NEW_ZARRAY(); /* zero length array*/
  1413. }
  1414. else {
  1415. $$ = $2;
  1416. }
  1417. }
  1418. | tLBRACE assoc_list '}'
  1419. {
  1420. $$ = NEW_HASH($2);
  1421. }
  1422. | kRETURN
  1423. {
  1424. $$ = NEW_RETURN(0);
  1425. }
  1426. | kYIELD '(' call_args ')'
  1427. {
  1428. $$ = new_yield($3);
  1429. }
  1430. | kYIELD '(' ')'
  1431. {
  1432. $$ = NEW_YIELD(0, Qfalse);
  1433. }
  1434. | kYIELD
  1435. {
  1436. $$ = NEW_YIELD(0, Qfalse);
  1437. }
  1438. | kDEFINED opt_nl '(' {in_defined = 1;} expr ')'
  1439. {
  1440. in_defined = 0;
  1441. $$ = NEW_DEFINED($5);
  1442. }
  1443. | operation brace_block
  1444. {
  1445. $2-&gt;nd_iter = NEW_FCALL($1, 0);
  1446. $$ = $2;
  1447. fixpos($2-&gt;nd_iter, $2);
  1448. }
  1449. | method_call
  1450. | method_call brace_block
  1451. {
  1452. if ($1 &amp;&amp; nd_type($1) == NODE_BLOCK_PASS) {
  1453. rb_compile_error(&quot;both block arg and actual block given&quot;);
  1454. }
  1455. $2-&gt;nd_iter = $1;
  1456. $$ = $2;
  1457. fixpos($$, $1);
  1458. }
  1459. | kIF expr_value then
  1460. compstmt
  1461. if_tail
  1462. kEND
  1463. {
  1464. $$ = NEW_IF(cond($2), $4, $5);
  1465. fixpos($$, $2);
  1466. if (cond_negative(&amp;$$-&gt;nd_cond)) {
  1467. NODE *tmp = $$-&gt;nd_body;
  1468. $$-&gt;nd_body = $$-&gt;nd_else;
  1469. $$-&gt;nd_else = tmp;
  1470. }
  1471. }
  1472. | kUNLESS expr_value then
  1473. compstmt
  1474. opt_else
  1475. kEND
  1476. {
  1477. $$ = NEW_UNLESS(cond($2), $4, $5);
  1478. fixpos($$, $2);
  1479. if (cond_negative(&amp;$$-&gt;nd_cond)) {
  1480. NODE *tmp = $$-&gt;nd_body;
  1481. $$-&gt;nd_body = $$-&gt;nd_else;
  1482. $$-&gt;nd_else = tmp;
  1483. }
  1484. }
  1485. | kWHILE {COND_PUSH(1);} expr_value do {COND_POP();}
  1486. compstmt
  1487. kEND
  1488. {
  1489. $$ = NEW_WHILE(cond($3), $6, 1);
  1490. fixpos($$, $3);
  1491. if (cond_negative(&amp;$$-&gt;nd_cond)) {
  1492. nd_set_type($$, NODE_UNTIL);
  1493. }
  1494. }
  1495. | kUNTIL {COND_PUSH(1);} expr_value do {COND_POP();}
  1496. compstmt
  1497. kEND
  1498. {
  1499. $$ = NEW_UNTIL(cond($3), $6, 1);
  1500. fixpos($$, $3);
  1501. if (cond_negative(&amp;$$-&gt;nd_cond)) {
  1502. nd_set_type($$, NODE_WHILE);
  1503. }
  1504. }
  1505. | kCASE expr_value opt_terms
  1506. case_body
  1507. kEND
  1508. {
  1509. $$ = NEW_CASE($2, $4);
  1510. fixpos($$, $2);
  1511. }
  1512. | kCASE opt_terms case_body kEND
  1513. {
  1514. $$ = $3;
  1515. }
  1516. | kCASE opt_terms kELSE compstmt kEND
  1517. {
  1518. $$ = $4;
  1519. }
  1520. | kFOR block_var kIN {COND_PUSH(1);} expr_value do {COND_POP();}
  1521. compstmt
  1522. kEND
  1523. {
  1524. $$ = NEW_FOR($2, $5, $8);
  1525. fixpos($$, $2);
  1526. }
  1527. | kCLASS cpath superclass
  1528. {
  1529. if (in_def || in_single)
  1530. yyerror(&quot;class definition in method body&quot;);
  1531. class_nest++;
  1532. local_push(0);
  1533. $&lt;num&gt;$ = ruby_sourceline;
  1534. }
  1535. bodystmt
  1536. kEND
  1537. {
  1538. $$ = NEW_CLASS($2, $5, $3);
  1539. nd_set_line($$, $&lt;num&gt;4);
  1540. local_pop();
  1541. class_nest--;
  1542. }
  1543. | kCLASS tLSHFT expr
  1544. {
  1545. $&lt;num&gt;$ = in_def;
  1546. in_def = 0;
  1547. }
  1548. term
  1549. {
  1550. $&lt;num&gt;$ = in_single;
  1551. in_single = 0;
  1552. class_nest++;
  1553. local_push(0);
  1554. }
  1555. bodystmt
  1556. kEND
  1557. {
  1558. $$ = NEW_SCLASS($3, $7);
  1559. fixpos($$, $3);
  1560. local_pop();
  1561. class_nest--;
  1562. in_def = $&lt;num&gt;4;
  1563. in_single = $&lt;num&gt;6;
  1564. }
  1565. | kMODULE cpath
  1566. {
  1567. if (in_def || in_single)
  1568. yyerror(&quot;module definition in method body&quot;);
  1569. class_nest++;
  1570. local_push(0);
  1571. $&lt;num&gt;$ = ruby_sourceline;
  1572. }
  1573. bodystmt
  1574. kEND
  1575. {
  1576. $$ = NEW_MODULE($2, $4);
  1577. nd_set_line($$, $&lt;num&gt;3);
  1578. local_pop();
  1579. class_nest--;
  1580. }
  1581. | kDEF fname
  1582. {
  1583. $&lt;id&gt;$ = cur_mid;
  1584. cur_mid = $2;
  1585. in_def++;
  1586. local_push(0);
  1587. }
  1588. f_arglist
  1589. bodystmt
  1590. kEND
  1591. {
  1592. if (!$5) $5 = NEW_NIL();
  1593. $$ = NEW_DEFN($2, $4, $5, NOEX_PRIVATE);
  1594. fixpos($$, $4);
  1595. local_pop();
  1596. in_def--;
  1597. cur_mid = $&lt;id&gt;3;
  1598. }
  1599. | kDEF singleton dot_or_colon {lex_state = EXPR_FNAME;} fname
  1600. {
  1601. in_single++;
  1602. local_push(0);
  1603. lex_state = EXPR_END; /* force for args */
  1604. }
  1605. f_arglist
  1606. bodystmt
  1607. kEND
  1608. {
  1609. $$ = NEW_DEFS($2, $5, $7, $8);
  1610. fixpos($$, $2);
  1611. local_pop();
  1612. in_single--;
  1613. }
  1614. | kBREAK
  1615. {
  1616. $$ = NEW_BREAK(0);
  1617. }
  1618. | kNEXT
  1619. {
  1620. $$ = NEW_NEXT(0);
  1621. }
  1622. | kREDO
  1623. {
  1624. $$ = NEW_REDO();
  1625. }
  1626. | kRETRY
  1627. {
  1628. $$ = NEW_RETRY();
  1629. }
  1630. ;
  1631. primary_value : primary
  1632. {
  1633. value_expr($1);
  1634. $$ = $1;
  1635. }
  1636. ;
  1637. then : term
  1638. | ':'
  1639. | kTHEN
  1640. | term kTHEN
  1641. ;
  1642. do : term
  1643. | ':'
  1644. | kDO_COND
  1645. ;
  1646. if_tail : opt_else
  1647. | kELSIF expr_value then
  1648. compstmt
  1649. if_tail
  1650. {
  1651. $$ = NEW_IF(cond($2), $4, $5);
  1652. fixpos($$, $2);
  1653. }
  1654. ;
  1655. opt_else : none
  1656. | kELSE compstmt
  1657. {
  1658. $$ = $2;
  1659. }
  1660. ;
  1661. block_var : lhs
  1662. | mlhs
  1663. ;
  1664. opt_block_var : none
  1665. | '|' /* none */ '|'
  1666. {
  1667. $$ = (NODE*)1;
  1668. }
  1669. | tOROP
  1670. {
  1671. $$ = (NODE*)1;
  1672. }
  1673. | '|' block_var '|'
  1674. {
  1675. $$ = $2;
  1676. }
  1677. ;
  1678. do_block : kDO_BLOCK
  1679. {
  1680. $&lt;vars&gt;$ = dyna_push();
  1681. $&lt;num&gt;1 = ruby_sourceline;
  1682. }
  1683. opt_block_var {$&lt;vars&gt;$ = ruby_dyna_vars;}
  1684. compstmt
  1685. kEND
  1686. {
  1687. $$ = NEW_ITER($3, 0, dyna_init($5, $&lt;vars&gt;4));
  1688. nd_set_line($$, $&lt;num&gt;1);
  1689. dyna_pop($&lt;vars&gt;2);
  1690. }
  1691. ;
  1692. block_call : command do_block
  1693. {
  1694. if ($1 &amp;&amp; nd_type($1) == NODE_BLOCK_PASS) {
  1695. rb_compile_error(&quot;both block arg and actual block given&quot;);
  1696. }
  1697. $2-&gt;nd_iter = $1;
  1698. $$ = $2;
  1699. fixpos($$, $1);
  1700. }
  1701. | block_call '.' operation2 opt_paren_args
  1702. {
  1703. $$ = new_call($1, $3, $4);
  1704. }
  1705. | block_call tCOLON2 operation2 opt_paren_args
  1706. {
  1707. $$ = new_call($1, $3, $4);
  1708. }
  1709. ;
  1710. method_call : operation paren_args
  1711. {
  1712. $$ = new_fcall($1, $2);
  1713. fixpos($$, $2);
  1714. }
  1715. | primary_value '.' operation2 opt_paren_args
  1716. {
  1717. $$ = new_call($1, $3, $4);
  1718. fixpos($$, $1);
  1719. }
  1720. | primary_value tCOLON2 operation2 paren_args
  1721. {
  1722. $$ = new_call($1, $3, $4);
  1723. fixpos($$, $1);
  1724. }
  1725. | primary_value tCOLON2 operation3
  1726. {
  1727. $$ = new_call($1, $3, 0);
  1728. }
  1729. | kSUPER paren_args
  1730. {
  1731. $$ = new_super($2);
  1732. }
  1733. | kSUPER
  1734. {
  1735. $$ = NEW_ZSUPER();
  1736. }
  1737. ;
  1738. brace_block : '{'
  1739. {
  1740. $&lt;vars&gt;$ = dyna_push();
  1741. $&lt;num&gt;1 = ruby_sourceline;
  1742. }
  1743. opt_block_var {$&lt;vars&gt;$ = ruby_dyna_vars;}
  1744. compstmt '}'
  1745. {
  1746. $$ = NEW_ITER($3, 0, dyna_init($5, $&lt;vars&gt;4));
  1747. nd_set_line($$, $&lt;num&gt;1);
  1748. dyna_pop($&lt;vars&gt;2);
  1749. }
  1750. | kDO
  1751. {
  1752. $&lt;vars&gt;$ = dyna_push();
  1753. $&lt;num&gt;1 = ruby_sourceline;
  1754. }
  1755. opt_block_var {$&lt;vars&gt;$ = ruby_dyna_vars;}
  1756. compstmt kEND
  1757. {
  1758. $$ = NEW_ITER($3, 0, dyna_init($5, $&lt;vars&gt;4));
  1759. nd_set_line($$, $&lt;num&gt;1);
  1760. dyna_pop($&lt;vars&gt;2);
  1761. }
  1762. ;
  1763. case_body : kWHEN when_args then
  1764. compstmt
  1765. cases
  1766. {
  1767. $$ = NEW_WHEN($2, $4, $5);
  1768. }
  1769. ;
  1770. when_args : args
  1771. | args ',' tSTAR arg_value
  1772. {
  1773. $$ = list_append($1, NEW_WHEN($4, 0, 0));
  1774. }
  1775. | tSTAR arg_value
  1776. {
  1777. $$ = NEW_LIST(NEW_WHEN($2, 0, 0));
  1778. }
  1779. ;
  1780. cases : opt_else
  1781. | case_body
  1782. ;
  1783. opt_rescue : kRESCUE exc_list exc_var then
  1784. compstmt
  1785. opt_rescue
  1786. {
  1787. if ($3) {
  1788. $3 = node_assign($3, NEW_GVAR(rb_intern(&quot;$!&quot;)));
  1789. $5 = block_append($3, $5);
  1790. }
  1791. $$ = NEW_RESBODY($2, $5, $6);
  1792. fixpos($$, $2?$2:$5);
  1793. }
  1794. | none
  1795. ;
  1796. exc_list : arg_value
  1797. {
  1798. $$ = NEW_LIST($1);
  1799. }
  1800. | mrhs
  1801. | none
  1802. ;
  1803. exc_var : tASSOC lhs
  1804. {
  1805. $$ = $2;
  1806. }
  1807. | none
  1808. ;
  1809. opt_ensure : kENSURE compstmt
  1810. {
  1811. if ($2)
  1812. $$ = $2;
  1813. else
  1814. /* place holder */
  1815. $$ = NEW_NIL();
  1816. }
  1817. | none
  1818. ;
  1819. literal : numeric
  1820. | symbol
  1821. {
  1822. $$ = NEW_LIT(ID2SYM($1));
  1823. }
  1824. | dsym
  1825. ;
  1826. strings : string
  1827. {
  1828. NODE *node = $1;
  1829. if (!node) {
  1830. node = NEW_STR(rb_str_new(0, 0));
  1831. }
  1832. else {
  1833. node = evstr2dstr(node);
  1834. }
  1835. $$ = node;
  1836. }
  1837. ;
  1838. string : string1
  1839. | string string1
  1840. {
  1841. $$ = literal_concat($1, $2);
  1842. }
  1843. ;
  1844. string1 : tSTRING_BEG string_contents tSTRING_END
  1845. {
  1846. $$ = $2;
  1847. }
  1848. ;
  1849. xstring : tXSTRING_BEG xstring_contents tSTRING_END
  1850. {
  1851. NODE *node = $2;
  1852. if (!node) {
  1853. node = NEW_XSTR(rb_str_new(0, 0));
  1854. }
  1855. else {
  1856. switch (nd_type(node)) {
  1857. case NODE_STR:
  1858. nd_set_type(node, NODE_XSTR);
  1859. break;
  1860. case NODE_DSTR:
  1861. nd_set_type(node, NODE_DXSTR);
  1862. break;
  1863. default:
  1864. node = NEW_NODE(NODE_DXSTR, rb_str_new(0, 0), 1, NEW_LIST(node));
  1865. break;
  1866. }
  1867. }
  1868. $$ = node;
  1869. }
  1870. ;
  1871. regexp : tREGEXP_BEG xstring_contents tREGEXP_END
  1872. {
  1873. int options = $3;
  1874. NODE *node = $2;
  1875. if (!node) {
  1876. node = NEW_LIT(rb_reg_new(&quot;&quot;, 0, options &amp; ~RE_OPTION_ONCE));
  1877. }
  1878. else switch (nd_type(node)) {
  1879. case NODE_STR:
  1880. {
  1881. VALUE src = node-&gt;nd_lit;
  1882. nd_set_type(node, NODE_LIT);
  1883. node-&gt;nd_lit = rb_reg_new(RSTRING(src)-&gt;ptr,
  1884. RSTRING(src)-&gt;len,
  1885. options &amp; ~RE_OPTION_ONCE);
  1886. }
  1887. break;
  1888. default:
  1889. node = NEW_NODE(NODE_DSTR, rb_str_new(0, 0), 1, NEW_LIST(node));
  1890. case NODE_DSTR:
  1891. if (options &amp; RE_OPTION_ONCE) {
  1892. nd_set_type(node, NODE_DREGX_ONCE);
  1893. }
  1894. else {
  1895. nd_set_type(node, NODE_DREGX);
  1896. }
  1897. node-&gt;nd_cflag = options &amp; ~RE_OPTION_ONCE;
  1898. break;
  1899. }
  1900. $$ = node;
  1901. }
  1902. ;
  1903. words : tWORDS_BEG ' ' tSTRING_END
  1904. {
  1905. $$ = NEW_ZARRAY();
  1906. }
  1907. | tWORDS_BEG word_list tSTRING_END
  1908. {
  1909. $$ = $2;
  1910. }
  1911. ;
  1912. word_list : /* none */
  1913. {
  1914. $$ = 0;
  1915. }
  1916. | word_list word ' '
  1917. {
  1918. $$ = list_append($1, evstr2dstr($2));
  1919. }
  1920. ;
  1921. word : string_content
  1922. | word string_content
  1923. {
  1924. $$ = literal_concat($1, $2);
  1925. }
  1926. ;
  1927. qwords : tQWORDS_BEG ' ' tSTRING_END
  1928. {
  1929. $$ = NEW_ZARRAY();
  1930. }
  1931. | tQWORDS_BEG qword_list tSTRING_END
  1932. {
  1933. $$ = $2;
  1934. }
  1935. ;
  1936. qword_list : /* none */
  1937. {
  1938. $$ = 0;
  1939. }
  1940. | qword_list tSTRING_CONTENT ' '
  1941. {
  1942. $$ = list_append($1, $2);
  1943. }
  1944. ;
  1945. string_contents : /* none */
  1946. {
  1947. $$ = 0;
  1948. }
  1949. | string_contents string_content
  1950. {
  1951. $$ = literal_concat($1, $2);
  1952. }
  1953. ;
  1954. xstring_contents: /* none */
  1955. {
  1956. $$ = 0;
  1957. }
  1958. | xstring_contents string_content
  1959. {
  1960. $$ = literal_concat($1, $2);
  1961. }
  1962. ;
  1963. string_content : tSTRING_CONTENT
  1964. | tSTRING_DVAR
  1965. {
  1966. $&lt;node&gt;$ = lex_strterm;
  1967. lex_strterm = 0;
  1968. lex_state = EXPR_BEG;
  1969. }
  1970. string_dvar
  1971. {
  1972. lex_strterm = $&lt;node&gt;2;
  1973. $$ = NEW_EVSTR($3);
  1974. }
  1975. | tSTRING_DBEG
  1976. {
  1977. $&lt;node&gt;$ = lex_strterm;
  1978. lex_strterm = 0;
  1979. lex_state = EXPR_BEG;
  1980. COND_PUSH(0);
  1981. CMDARG_PUSH(0);
  1982. }
  1983. compstmt '}'
  1984. {
  1985. lex_strterm = $&lt;node&gt;2;
  1986. COND_LEXPOP();
  1987. CMDARG_LEXPOP();
  1988. if (($$ = $3) &amp;&amp; nd_type($$) == NODE_NEWLINE) {
  1989. $$ = $$-&gt;nd_next;
  1990. rb_gc_force_recycle((VALUE)$3);
  1991. }
  1992. $$ = new_evstr($$);
  1993. }
  1994. ;
  1995. string_dvar : tGVAR {$$ = NEW_GVAR($1);}
  1996. | tIVAR {$$ = NEW_IVAR($1);}
  1997. | tCVAR {$$ = NEW_CVAR($1);}
  1998. | backref
  1999. ;
  2000. symbol : tSYMBEG sym
  2001. {
  2002. lex_state = EXPR_END;
  2003. $$ = $2;
  2004. }
  2005. ;
  2006. sym : fname
  2007. | tIVAR
  2008. | tGVAR
  2009. | tCVAR
  2010. ;
  2011. dsym : tSYMBEG xstring_contents tSTRING_END
  2012. {
  2013. lex_state = EXPR_END;
  2014. if (!($$ = $2)) {
  2015. yyerror(&quot;empty symbol literal&quot;);
  2016. }
  2017. else {
  2018. switch (nd_type($$)) {
  2019. case NODE_DSTR:
  2020. nd_set_type($$, NODE_DSYM);
  2021. break;
  2022. case NODE_STR:
  2023. if (strlen(RSTRING($$-&gt;nd_lit)-&gt;ptr) == RSTRING($$-&gt;nd_lit)-&gt;len) {
  2024. $$-&gt;nd_lit = ID2SYM(rb_intern(RSTRING($$-&gt;nd_lit)-&gt;ptr));
  2025. nd_set_type($$, NODE_LIT);
  2026. break;
  2027. }
  2028. /* fall through */
  2029. default:
  2030. $$ = NEW_NODE(NODE_DSYM, rb_str_new(0, 0), 1, NEW_LIST($$));
  2031. break;
  2032. }
  2033. }
  2034. }
  2035. ;
  2036. numeric : tINTEGER
  2037. | tFLOAT
  2038. | tUMINUS_NUM tINTEGER %prec tLOWEST
  2039. {
  2040. $$ = negate_lit($2);
  2041. }
  2042. | tUMINUS_NUM tFLOAT %prec tLOWEST
  2043. {
  2044. $$ = negate_lit($2);
  2045. }
  2046. ;
  2047. variable : tIDENTIFIER
  2048. | tIVAR
  2049. | tGVAR
  2050. | tCONSTANT
  2051. | tCVAR
  2052. | kNIL {$$ = kNIL;}
  2053. | kSELF {$$ = kSELF;}
  2054. | kTRUE {$$ = kTRUE;}
  2055. | kFALSE {$$ = kFALSE;}
  2056. | k__FILE__ {$$ = k__FILE__;}
  2057. | k__LINE__ {$$ = k__LINE__;}
  2058. ;
  2059. var_ref : variable
  2060. {
  2061. $$ = gettable($1);
  2062. }
  2063. ;
  2064. var_lhs : variable
  2065. {
  2066. $$ = assignable($1, 0);
  2067. }
  2068. ;
  2069. backref : tNTH_REF
  2070. | tBACK_REF
  2071. ;
  2072. superclass : term
  2073. {
  2074. $$ = 0;
  2075. }
  2076. | '&lt;'
  2077. {
  2078. lex_state = EXPR_BEG;
  2079. }
  2080. expr_value term
  2081. {
  2082. $$ = $3;
  2083. }
  2084. | error term {yyerrok; $$ = 0;}
  2085. ;
  2086. f_arglist : '(' f_args opt_nl ')'
  2087. {
  2088. $$ = $2;
  2089. lex_state = EXPR_BEG;
  2090. }
  2091. | f_args term
  2092. {
  2093. $$ = $1;
  2094. }
  2095. ;
  2096. f_args : f_arg ',' f_optarg ',' f_rest_arg opt_f_block_arg
  2097. {
  2098. $$ = block_append(NEW_ARGS($1, $3, $5), $6);
  2099. }
  2100. | f_arg ',' f_optarg opt_f_block_arg
  2101. {
  2102. $$ = block_append(NEW_ARGS($1, $3, -1), $4);
  2103. }
  2104. | f_arg ',' f_rest_arg opt_f_block_arg
  2105. {
  2106. $$ = block_append(NEW_ARGS($1, 0, $3), $4);
  2107. }
  2108. | f_arg opt_f_block_arg
  2109. {
  2110. $$ = block_append(NEW_ARGS($1, 0, -1), $2);
  2111. }
  2112. | f_optarg ',' f_rest_arg opt_f_block_arg
  2113. {
  2114. $$ = block_append(NEW_ARGS(0, $1, $3), $4);
  2115. }
  2116. | f_optarg opt_f_block_arg
  2117. {
  2118. $$ = block_append(NEW_ARGS(0, $1, -1), $2);
  2119. }
  2120. | f_rest_arg opt_f_block_arg
  2121. {
  2122. $$ = block_append(NEW_ARGS(0, 0, $1), $2);
  2123. }
  2124. | f_block_arg
  2125. {
  2126. $$ = block_append(NEW_ARGS(0, 0, -1), $1);
  2127. }
  2128. | /* none */
  2129. {
  2130. $$ = NEW_ARGS(0, 0, -1);
  2131. }
  2132. ;
  2133. f_norm_arg : tCONSTANT
  2134. {
  2135. yyerror(&quot;formal argument cannot be a constant&quot;);
  2136. }
  2137. | tIVAR
  2138. {
  2139. yyerror(&quot;formal argument cannot be an instance variable&quot;);
  2140. }
  2141. | tGVAR
  2142. {
  2143. yyerror(&quot;formal argument cannot be a global variable&quot;);
  2144. }
  2145. | tCVAR
  2146. {
  2147. yyerror(&quot;formal argument cannot be a class variable&quot;);
  2148. }
  2149. | tIDENTIFIER
  2150. {
  2151. if (!is_local_id($1))
  2152. yyerror(&quot;formal argument must be local variable&quot;);
  2153. else if (local_id($1))
  2154. yyerror(&quot;duplicate argument name&quot;);
  2155. local_cnt($1);
  2156. $$ = 1;
  2157. }
  2158. ;
  2159. f_arg : f_norm_arg
  2160. | f_arg ',' f_norm_arg
  2161. {
  2162. $$ += 1;
  2163. }
  2164. ;
  2165. f_opt : tIDENTIFIER '=' arg_value
  2166. {
  2167. if (!is_local_id($1))
  2168. yyerror(&quot;formal argument must be local variable&quot;);
  2169. else if (local_id($1))
  2170. yyerror(&quot;duplicate optional argument name&quot;);
  2171. $$ = assignable($1, $3);
  2172. }
  2173. ;
  2174. f_optarg : f_opt
  2175. {
  2176. $$ = NEW_BLOCK($1);
  2177. $$-&gt;nd_end = $$;
  2178. }
  2179. | f_optarg ',' f_opt
  2180. {
  2181. $$ = block_append($1, $3);
  2182. }
  2183. ;
  2184. restarg_mark : '*'
  2185. | tSTAR
  2186. ;
  2187. f_rest_arg : restarg_mark tIDENTIFIER
  2188. {
  2189. if (!is_local_id($2))
  2190. yyerror(&quot;rest argument must be local variable&quot;);
  2191. else if (local_id($2))
  2192. yyerror(&quot;duplicate rest argument name&quot;);
  2193. $$ = local_cnt($2);
  2194. }
  2195. | restarg_mark
  2196. {
  2197. $$ = -2;
  2198. }
  2199. ;
  2200. blkarg_mark : '&amp;'
  2201. | tAMPER
  2202. ;
  2203. f_block_arg : blkarg_mark tIDENTIFIER
  2204. {
  2205. if (!is_local_id($2))
  2206. yyerror(&quot;block argument must be local variable&quot;);
  2207. else if (local_id($2))
  2208. yyerror(&quot;duplicate block argument name&quot;);
  2209. $$ = NEW_BLOCK_ARG($2);
  2210. }
  2211. ;
  2212. opt_f_block_arg : ',' f_block_arg
  2213. {
  2214. $$ = $2;
  2215. }
  2216. | none
  2217. ;
  2218. singleton : var_ref
  2219. {
  2220. if (nd_type($1) == NODE_SELF) {
  2221. $$ = NEW_SELF();
  2222. }
  2223. else {
  2224. $$ = $1;
  2225. value_expr($$);
  2226. }
  2227. }
  2228. | '(' {lex_state = EXPR_BEG;} expr opt_nl ')'
  2229. {
  2230. if ($3 == 0) {
  2231. yyerror(&quot;can't define singleton method for ().&quot;);
  2232. }
  2233. else {
  2234. switch (nd_type($3)) {
  2235. case NODE_STR:
  2236. case NODE_DSTR:
  2237. case NODE_XSTR:
  2238. case NODE_DXSTR:
  2239. case NODE_DREGX:
  2240. case NODE_LIT:
  2241. case NODE_ARRAY:
  2242. case NODE_ZARRAY:
  2243. yyerror(&quot;can't define singleton method for literals&quot;);
  2244. default:
  2245. value_expr($3);
  2246. break;
  2247. }
  2248. }
  2249. $$ = $3;
  2250. }
  2251. ;
  2252. assoc_list : none
  2253. | assocs trailer
  2254. {
  2255. $$ = $1;
  2256. }
  2257. | args trailer
  2258. {
  2259. if ($1-&gt;nd_alen%2 != 0) {
  2260. yyerror(&quot;odd number list for Hash&quot;);
  2261. }
  2262. $$ = $1;
  2263. }
  2264. ;
  2265. assocs : assoc
  2266. | assocs ',' assoc
  2267. {
  2268. $$ = list_concat($1, $3);
  2269. }
  2270. ;
  2271. assoc : arg_value tASSOC arg_value
  2272. {
  2273. $$ = list_append(NEW_LIST($1), $3);
  2274. }
  2275. ;
  2276. operation : tIDENTIFIER
  2277. | tCONSTANT
  2278. | tFID
  2279. ;
  2280. operation2 : tIDENTIFIER
  2281. | tCONSTANT
  2282. | tFID
  2283. | op
  2284. ;
  2285. operation3 : tIDENTIFIER
  2286. | tFID
  2287. | op
  2288. ;
  2289. dot_or_colon : '.'
  2290. | tCOLON2
  2291. ;
  2292. opt_terms : /* none */
  2293. | terms
  2294. ;
  2295. opt_nl : /* none */
  2296. | '\n'
  2297. ;
  2298. trailer : /* none */
  2299. | '\n'

Large files files are truncated, but you can click here to view the full file