PageRenderTime 67ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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

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