PageRenderTime 57ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Happy | 3205 lines | 2969 code | 236 blank | 0 comment | 0 complexity | 7cf159668d4dca30e4c054020b89426a 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: 2008-06-29 16:52:47 +0900 (Sun, 29 Jun 2008) $
  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. $$ = remove_begin($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), remove_begin($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), remove_begin($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. NODE *resq = NEW_RESBODY(0, remove_begin($3), 0);
  456. $$ = NEW_RESCUE(remove_begin($1), resq, 0);
  457. }
  458. | klBEGIN
  459. {
  460. if (in_def || in_single) {
  461. yyerror(&quot;BEGIN in method&quot;);
  462. }
  463. local_push(0);
  464. }
  465. '{' compstmt '}'
  466. {
  467. ruby_eval_tree_begin = block_append(ruby_eval_tree_begin,
  468. NEW_PREEXE($4));
  469. local_pop();
  470. $$ = 0;
  471. }
  472. | klEND '{' compstmt '}'
  473. {
  474. if (in_def || in_single) {
  475. rb_warn(&quot;END in method; use at_exit&quot;);
  476. }
  477. $$ = NEW_ITER(0, NEW_POSTEXE(), $3);
  478. }
  479. | lhs '=' command_call
  480. {
  481. $$ = node_assign($1, $3);
  482. }
  483. | mlhs '=' command_call
  484. {
  485. value_expr($3);
  486. $1-&gt;nd_value = ($1-&gt;nd_head) ? NEW_TO_ARY($3) : NEW_ARRAY($3);
  487. $$ = $1;
  488. }
  489. | var_lhs tOP_ASGN command_call
  490. {
  491. value_expr($3);
  492. if ($1) {
  493. ID vid = $1-&gt;nd_vid;
  494. if ($2 == tOROP) {
  495. $1-&gt;nd_value = $3;
  496. $$ = NEW_OP_ASGN_OR(gettable(vid), $1);
  497. if (is_asgn_or_id(vid)) {
  498. $$-&gt;nd_aid = vid;
  499. }
  500. }
  501. else if ($2 == tANDOP) {
  502. $1-&gt;nd_value = $3;
  503. $$ = NEW_OP_ASGN_AND(gettable(vid), $1);
  504. }
  505. else {
  506. $$ = $1;
  507. $$-&gt;nd_value = call_op(gettable(vid),$2,1,$3);
  508. }
  509. }
  510. else {
  511. $$ = 0;
  512. }
  513. }
  514. | primary_value '[' aref_args ']' tOP_ASGN command_call
  515. {
  516. NODE *args;
  517. value_expr($6);
  518. if (!$3) $3 = NEW_ZARRAY();
  519. args = arg_concat($6, $3);
  520. if ($5 == tOROP) {
  521. $5 = 0;
  522. }
  523. else if ($5 == tANDOP) {
  524. $5 = 1;
  525. }
  526. $$ = NEW_OP_ASGN1($1, $5, args);
  527. fixpos($$, $1);
  528. }
  529. | primary_value '.' tIDENTIFIER tOP_ASGN command_call
  530. {
  531. value_expr($5);
  532. if ($4 == tOROP) {
  533. $4 = 0;
  534. }
  535. else if ($4 == tANDOP) {
  536. $4 = 1;
  537. }
  538. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  539. fixpos($$, $1);
  540. }
  541. | primary_value '.' tCONSTANT tOP_ASGN command_call
  542. {
  543. value_expr($5);
  544. if ($4 == tOROP) {
  545. $4 = 0;
  546. }
  547. else if ($4 == tANDOP) {
  548. $4 = 1;
  549. }
  550. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  551. fixpos($$, $1);
  552. }
  553. | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_call
  554. {
  555. value_expr($5);
  556. if ($4 == tOROP) {
  557. $4 = 0;
  558. }
  559. else if ($4 == tANDOP) {
  560. $4 = 1;
  561. }
  562. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  563. fixpos($$, $1);
  564. }
  565. | backref tOP_ASGN command_call
  566. {
  567. rb_backref_error($1);
  568. $$ = 0;
  569. }
  570. | lhs '=' mrhs
  571. {
  572. $$ = node_assign($1, NEW_SVALUE($3));
  573. }
  574. | mlhs '=' arg_value
  575. {
  576. $1-&gt;nd_value = ($1-&gt;nd_head) ? NEW_TO_ARY($3) : NEW_ARRAY($3);
  577. $$ = $1;
  578. }
  579. | mlhs '=' mrhs
  580. {
  581. $1-&gt;nd_value = $3;
  582. $$ = $1;
  583. }
  584. | expr
  585. ;
  586. expr : command_call
  587. | expr kAND expr
  588. {
  589. $$ = logop(NODE_AND, $1, $3);
  590. }
  591. | expr kOR expr
  592. {
  593. $$ = logop(NODE_OR, $1, $3);
  594. }
  595. | kNOT expr
  596. {
  597. $$ = NEW_NOT(cond($2));
  598. }
  599. | '!' command_call
  600. {
  601. $$ = NEW_NOT(cond($2));
  602. }
  603. | arg
  604. ;
  605. expr_value : expr
  606. {
  607. value_expr($$);
  608. $$ = $1;
  609. }
  610. ;
  611. command_call : command
  612. | block_command
  613. | kRETURN call_args
  614. {
  615. $$ = NEW_RETURN(ret_args($2));
  616. }
  617. | kBREAK call_args
  618. {
  619. $$ = NEW_BREAK(ret_args($2));
  620. }
  621. | kNEXT call_args
  622. {
  623. $$ = NEW_NEXT(ret_args($2));
  624. }
  625. ;
  626. block_command : block_call
  627. | block_call '.' operation2 command_args
  628. {
  629. $$ = new_call($1, $3, $4);
  630. }
  631. | block_call tCOLON2 operation2 command_args
  632. {
  633. $$ = new_call($1, $3, $4);
  634. }
  635. ;
  636. cmd_brace_block : tLBRACE_ARG
  637. {
  638. $&lt;vars&gt;$ = dyna_push();
  639. $&lt;num&gt;1 = ruby_sourceline;
  640. }
  641. opt_block_var {$&lt;vars&gt;$ = ruby_dyna_vars;}
  642. compstmt
  643. '}'
  644. {
  645. $$ = NEW_ITER($3, 0, dyna_init($5, $&lt;vars&gt;4));
  646. nd_set_line($$, $&lt;num&gt;1);
  647. dyna_pop($&lt;vars&gt;2);
  648. }
  649. ;
  650. command : operation command_args %prec tLOWEST
  651. {
  652. $$ = new_fcall($1, $2);
  653. fixpos($$, $2);
  654. }
  655. | operation command_args cmd_brace_block
  656. {
  657. $$ = new_fcall($1, $2);
  658. if ($3) {
  659. if (nd_type($$) == NODE_BLOCK_PASS) {
  660. rb_compile_error(&quot;both block arg and actual block given&quot;);
  661. }
  662. $3-&gt;nd_iter = $$;
  663. $$ = $3;
  664. }
  665. fixpos($$, $2);
  666. }
  667. | primary_value '.' operation2 command_args %prec tLOWEST
  668. {
  669. $$ = new_call($1, $3, $4);
  670. fixpos($$, $1);
  671. }
  672. | primary_value '.' operation2 command_args cmd_brace_block
  673. {
  674. $$ = new_call($1, $3, $4);
  675. if ($5) {
  676. if (nd_type($$) == NODE_BLOCK_PASS) {
  677. rb_compile_error(&quot;both block arg and actual block given&quot;);
  678. }
  679. $5-&gt;nd_iter = $$;
  680. $$ = $5;
  681. }
  682. fixpos($$, $1);
  683. }
  684. | primary_value tCOLON2 operation2 command_args %prec tLOWEST
  685. {
  686. $$ = new_call($1, $3, $4);
  687. fixpos($$, $1);
  688. }
  689. | primary_value tCOLON2 operation2 command_args cmd_brace_block
  690. {
  691. $$ = new_call($1, $3, $4);
  692. if ($5) {
  693. if (nd_type($$) == NODE_BLOCK_PASS) {
  694. rb_compile_error(&quot;both block arg and actual block given&quot;);
  695. }
  696. $5-&gt;nd_iter = $$;
  697. $$ = $5;
  698. }
  699. fixpos($$, $1);
  700. }
  701. | kSUPER command_args
  702. {
  703. $$ = new_super($2);
  704. fixpos($$, $2);
  705. }
  706. | kYIELD command_args
  707. {
  708. $$ = new_yield($2);
  709. fixpos($$, $2);
  710. }
  711. ;
  712. mlhs : mlhs_basic
  713. | tLPAREN mlhs_entry ')'
  714. {
  715. $$ = $2;
  716. }
  717. ;
  718. mlhs_entry : mlhs_basic
  719. | tLPAREN mlhs_entry ')'
  720. {
  721. $$ = NEW_MASGN(NEW_LIST($2), 0);
  722. }
  723. ;
  724. mlhs_basic : mlhs_head
  725. {
  726. $$ = NEW_MASGN($1, 0);
  727. }
  728. | mlhs_head mlhs_item
  729. {
  730. $$ = NEW_MASGN(list_append($1,$2), 0);
  731. }
  732. | mlhs_head tSTAR mlhs_node
  733. {
  734. $$ = NEW_MASGN($1, $3);
  735. }
  736. | mlhs_head tSTAR
  737. {
  738. $$ = NEW_MASGN($1, -1);
  739. }
  740. | tSTAR mlhs_node
  741. {
  742. $$ = NEW_MASGN(0, $2);
  743. }
  744. | tSTAR
  745. {
  746. $$ = NEW_MASGN(0, -1);
  747. }
  748. ;
  749. mlhs_item : mlhs_node
  750. | tLPAREN mlhs_entry ')'
  751. {
  752. $$ = $2;
  753. }
  754. ;
  755. mlhs_head : mlhs_item ','
  756. {
  757. $$ = NEW_LIST($1);
  758. }
  759. | mlhs_head mlhs_item ','
  760. {
  761. $$ = list_append($1, $2);
  762. }
  763. ;
  764. mlhs_node : variable
  765. {
  766. $$ = assignable($1, 0);
  767. }
  768. | primary_value '[' aref_args ']'
  769. {
  770. $$ = aryset($1, $3);
  771. }
  772. | primary_value '.' tIDENTIFIER
  773. {
  774. $$ = attrset($1, $3);
  775. }
  776. | primary_value tCOLON2 tIDENTIFIER
  777. {
  778. $$ = attrset($1, $3);
  779. }
  780. | primary_value '.' tCONSTANT
  781. {
  782. $$ = attrset($1, $3);
  783. }
  784. | primary_value tCOLON2 tCONSTANT
  785. {
  786. if (in_def || in_single)
  787. yyerror(&quot;dynamic constant assignment&quot;);
  788. $$ = NEW_CDECL(0, 0, NEW_COLON2($1, $3));
  789. }
  790. | tCOLON3 tCONSTANT
  791. {
  792. if (in_def || in_single)
  793. yyerror(&quot;dynamic constant assignment&quot;);
  794. $$ = NEW_CDECL(0, 0, NEW_COLON3($2));
  795. }
  796. | backref
  797. {
  798. rb_backref_error($1);
  799. $$ = 0;
  800. }
  801. ;
  802. lhs : variable
  803. {
  804. $$ = assignable($1, 0);
  805. }
  806. | primary_value '[' aref_args ']'
  807. {
  808. $$ = aryset($1, $3);
  809. }
  810. | primary_value '.' tIDENTIFIER
  811. {
  812. $$ = attrset($1, $3);
  813. }
  814. | primary_value tCOLON2 tIDENTIFIER
  815. {
  816. $$ = attrset($1, $3);
  817. }
  818. | primary_value '.' tCONSTANT
  819. {
  820. $$ = attrset($1, $3);
  821. }
  822. | primary_value tCOLON2 tCONSTANT
  823. {
  824. if (in_def || in_single)
  825. yyerror(&quot;dynamic constant assignment&quot;);
  826. $$ = NEW_CDECL(0, 0, NEW_COLON2($1, $3));
  827. }
  828. | tCOLON3 tCONSTANT
  829. {
  830. if (in_def || in_single)
  831. yyerror(&quot;dynamic constant assignment&quot;);
  832. $$ = NEW_CDECL(0, 0, NEW_COLON3($2));
  833. }
  834. | backref
  835. {
  836. rb_backref_error($1);
  837. $$ = 0;
  838. }
  839. ;
  840. cname : tIDENTIFIER
  841. {
  842. yyerror(&quot;class/module name must be CONSTANT&quot;);
  843. }
  844. | tCONSTANT
  845. ;
  846. cpath : tCOLON3 cname
  847. {
  848. $$ = NEW_COLON3($2);
  849. }
  850. | cname
  851. {
  852. $$ = NEW_COLON2(0, $$);
  853. }
  854. | primary_value tCOLON2 cname
  855. {
  856. $$ = NEW_COLON2($1, $3);
  857. }
  858. ;
  859. fname : tIDENTIFIER
  860. | tCONSTANT
  861. | tFID
  862. | op
  863. {
  864. lex_state = EXPR_END;
  865. $$ = $1;
  866. }
  867. | reswords
  868. {
  869. lex_state = EXPR_END;
  870. $$ = $&lt;id&gt;1;
  871. }
  872. ;
  873. fsym : fname
  874. | symbol
  875. ;
  876. fitem : fsym
  877. {
  878. $$ = NEW_LIT(ID2SYM($1));
  879. }
  880. | dsym
  881. ;
  882. undef_list : fitem
  883. {
  884. $$ = NEW_UNDEF($1);
  885. }
  886. | undef_list ',' {lex_state = EXPR_FNAME;} fitem
  887. {
  888. $$ = block_append($1, NEW_UNDEF($4));
  889. }
  890. ;
  891. op : '|' { $$ = '|'; }
  892. | '^' { $$ = '^'; }
  893. | '&amp;' { $$ = '&amp;'; }
  894. | tCMP { $$ = tCMP; }
  895. | tEQ { $$ = tEQ; }
  896. | tEQQ { $$ = tEQQ; }
  897. | tMATCH { $$ = tMATCH; }
  898. | '&gt;' { $$ = '&gt;'; }
  899. | tGEQ { $$ = tGEQ; }
  900. | '&lt;' { $$ = '&lt;'; }
  901. | tLEQ { $$ = tLEQ; }
  902. | tLSHFT { $$ = tLSHFT; }
  903. | tRSHFT { $$ = tRSHFT; }
  904. | '+' { $$ = '+'; }
  905. | '-' { $$ = '-'; }
  906. | '*' { $$ = '*'; }
  907. | tSTAR { $$ = '*'; }
  908. | '/' { $$ = '/'; }
  909. | '%' { $$ = '%'; }
  910. | tPOW { $$ = tPOW; }
  911. | '~' { $$ = '~'; }
  912. | tUPLUS { $$ = tUPLUS; }
  913. | tUMINUS { $$ = tUMINUS; }
  914. | tAREF { $$ = tAREF; }
  915. | tASET { $$ = tASET; }
  916. | '`' { $$ = '`'; }
  917. ;
  918. reswords : k__LINE__ | k__FILE__ | klBEGIN | klEND
  919. | kALIAS | kAND | kBEGIN | kBREAK | kCASE | kCLASS | kDEF
  920. | kDEFINED | kDO | kELSE | kELSIF | kEND | kENSURE | kFALSE
  921. | kFOR | kIN | kMODULE | kNEXT | kNIL | kNOT
  922. | kOR | kREDO | kRESCUE | kRETRY | kRETURN | kSELF | kSUPER
  923. | kTHEN | kTRUE | kUNDEF | kWHEN | kYIELD
  924. | kIF | kUNLESS | kWHILE | kUNTIL
  925. ;
  926. arg : lhs '=' arg
  927. {
  928. $$ = node_assign($1, $3);
  929. }
  930. | lhs '=' arg kRESCUE_MOD arg
  931. {
  932. $$ = node_assign($1, NEW_RESCUE($3, NEW_RESBODY(0,$5,0), 0));
  933. }
  934. | var_lhs tOP_ASGN arg
  935. {
  936. value_expr($3);
  937. if ($1) {
  938. ID vid = $1-&gt;nd_vid;
  939. if ($2 == tOROP) {
  940. $1-&gt;nd_value = $3;
  941. $$ = NEW_OP_ASGN_OR(gettable(vid), $1);
  942. if (is_asgn_or_id(vid)) {
  943. $$-&gt;nd_aid = vid;
  944. }
  945. }
  946. else if ($2 == tANDOP) {
  947. $1-&gt;nd_value = $3;
  948. $$ = NEW_OP_ASGN_AND(gettable(vid), $1);
  949. }
  950. else {
  951. $$ = $1;
  952. $$-&gt;nd_value = call_op(gettable(vid),$2,1,$3);
  953. }
  954. }
  955. else {
  956. $$ = 0;
  957. }
  958. }
  959. | primary_value '[' aref_args ']' tOP_ASGN arg
  960. {
  961. NODE *args;
  962. value_expr($6);
  963. if (!$3) $3 = NEW_ZARRAY();
  964. args = arg_concat($6, $3);
  965. if ($5 == tOROP) {
  966. $5 = 0;
  967. }
  968. else if ($5 == tANDOP) {
  969. $5 = 1;
  970. }
  971. $$ = NEW_OP_ASGN1($1, $5, args);
  972. fixpos($$, $1);
  973. }
  974. | primary_value '.' tIDENTIFIER tOP_ASGN arg
  975. {
  976. value_expr($5);
  977. if ($4 == tOROP) {
  978. $4 = 0;
  979. }
  980. else if ($4 == tANDOP) {
  981. $4 = 1;
  982. }
  983. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  984. fixpos($$, $1);
  985. }
  986. | primary_value '.' tCONSTANT tOP_ASGN arg
  987. {
  988. value_expr($5);
  989. if ($4 == tOROP) {
  990. $4 = 0;
  991. }
  992. else if ($4 == tANDOP) {
  993. $4 = 1;
  994. }
  995. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  996. fixpos($$, $1);
  997. }
  998. | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg
  999. {
  1000. value_expr($5);
  1001. if ($4 == tOROP) {
  1002. $4 = 0;
  1003. }
  1004. else if ($4 == tANDOP) {
  1005. $4 = 1;
  1006. }
  1007. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  1008. fixpos($$, $1);
  1009. }
  1010. | primary_value tCOLON2 tCONSTANT tOP_ASGN arg
  1011. {
  1012. yyerror(&quot;constant re-assignment&quot;);
  1013. $$ = 0;
  1014. }
  1015. | tCOLON3 tCONSTANT tOP_ASGN arg
  1016. {
  1017. yyerror(&quot;constant re-assignment&quot;);
  1018. $$ = 0;
  1019. }
  1020. | backref tOP_ASGN arg
  1021. {
  1022. rb_backref_error($1);
  1023. $$ = 0;
  1024. }
  1025. | arg tDOT2 arg
  1026. {
  1027. value_expr($1);
  1028. value_expr($3);
  1029. if (nd_type($1) == NODE_LIT &amp;&amp; FIXNUM_P($1-&gt;nd_lit) &amp;&amp;
  1030. nd_type($3) == NODE_LIT &amp;&amp; FIXNUM_P($3-&gt;nd_lit)) {
  1031. $1-&gt;nd_lit = rb_range_new($1-&gt;nd_lit, $3-&gt;nd_lit, Qfalse);
  1032. $$ = $1;
  1033. }
  1034. else {
  1035. $$ = NEW_DOT2($1, $3);
  1036. }
  1037. }
  1038. | arg tDOT3 arg
  1039. {
  1040. value_expr($1);
  1041. value_expr($3);
  1042. if (nd_type($1) == NODE_LIT &amp;&amp; FIXNUM_P($1-&gt;nd_lit) &amp;&amp;
  1043. nd_type($3) == NODE_LIT &amp;&amp; FIXNUM_P($3-&gt;nd_lit)) {
  1044. $1-&gt;nd_lit = rb_range_new($1-&gt;nd_lit, $3-&gt;nd_lit, Qtrue);
  1045. $$ = $1;
  1046. }
  1047. else {
  1048. $$ = NEW_DOT3($1, $3);
  1049. }
  1050. }
  1051. | arg '+' arg
  1052. {
  1053. $$ = call_op($1, '+', 1, $3);
  1054. }
  1055. | arg '-' arg
  1056. {
  1057. $$ = call_op($1, '-', 1, $3);
  1058. }
  1059. | arg '*' arg
  1060. {
  1061. $$ = call_op($1, '*', 1, $3);
  1062. }
  1063. | arg '/' arg
  1064. {
  1065. $$ = call_op($1, '/', 1, $3);
  1066. }
  1067. | arg '%' arg
  1068. {
  1069. $$ = call_op($1, '%', 1, $3);
  1070. }
  1071. | arg tPOW arg
  1072. {
  1073. $$ = call_op($1, tPOW, 1, $3);
  1074. }
  1075. | tUMINUS_NUM tINTEGER tPOW arg
  1076. {
  1077. $$ = call_op(call_op($2, tPOW, 1, $4), tUMINUS, 0, 0);
  1078. }
  1079. | tUMINUS_NUM tFLOAT tPOW arg
  1080. {
  1081. $$ = call_op(call_op($2, tPOW, 1, $4), tUMINUS, 0, 0);
  1082. }
  1083. | tUPLUS arg
  1084. {
  1085. if ($2 &amp;&amp; nd_type($2) == NODE_LIT) {
  1086. $$ = $2;
  1087. }
  1088. else {
  1089. $$ = call_op($2, tUPLUS, 0, 0);
  1090. }
  1091. }
  1092. | tUMINUS arg
  1093. {
  1094. $$ = call_op($2, tUMINUS, 0, 0);
  1095. }
  1096. | arg '|' arg
  1097. {
  1098. $$ = call_op($1, '|', 1, $3);
  1099. }
  1100. | arg '^' arg
  1101. {
  1102. $$ = call_op($1, '^', 1, $3);
  1103. }
  1104. | arg '&amp;' arg
  1105. {
  1106. $$ = call_op($1, '&amp;', 1, $3);
  1107. }
  1108. | arg tCMP arg
  1109. {
  1110. $$ = call_op($1, tCMP, 1, $3);
  1111. }
  1112. | arg '&gt;' arg
  1113. {
  1114. $$ = call_op($1, '&gt;', 1, $3);
  1115. }
  1116. | arg tGEQ arg
  1117. {
  1118. $$ = call_op($1, tGEQ, 1, $3);
  1119. }
  1120. | arg '&lt;' arg
  1121. {
  1122. $$ = call_op($1, '&lt;', 1, $3);
  1123. }
  1124. | arg tLEQ arg
  1125. {
  1126. $$ = call_op($1, tLEQ, 1, $3);
  1127. }
  1128. | arg tEQ arg
  1129. {
  1130. $$ = call_op($1, tEQ, 1, $3);
  1131. }
  1132. | arg tEQQ arg
  1133. {
  1134. $$ = call_op($1, tEQQ, 1, $3);
  1135. }
  1136. | arg tNEQ arg
  1137. {
  1138. $$ = NEW_NOT(call_op($1, tEQ, 1, $3));
  1139. }
  1140. | arg tMATCH arg
  1141. {
  1142. $$ = match_gen($1, $3);
  1143. }
  1144. | arg tNMATCH arg
  1145. {
  1146. $$ = NEW_NOT(match_gen($1, $3));
  1147. }
  1148. | '!' arg
  1149. {
  1150. $$ = NEW_NOT(cond($2));
  1151. }
  1152. | '~' arg
  1153. {
  1154. $$ = call_op($2, '~', 0, 0);
  1155. }
  1156. | arg tLSHFT arg
  1157. {
  1158. $$ = call_op($1, tLSHFT, 1, $3);
  1159. }
  1160. | arg tRSHFT arg
  1161. {
  1162. $$ = call_op($1, tRSHFT, 1, $3);
  1163. }
  1164. | arg tANDOP arg
  1165. {
  1166. $$ = logop(NODE_AND, $1, $3);
  1167. }
  1168. | arg tOROP arg
  1169. {
  1170. $$ = logop(NODE_OR, $1, $3);
  1171. }
  1172. | kDEFINED opt_nl {in_defined = 1;} arg
  1173. {
  1174. in_defined = 0;
  1175. $$ = NEW_DEFINED($4);
  1176. }
  1177. | arg '?' arg ':' arg
  1178. {
  1179. $$ = NEW_IF(cond($1), $3, $5);
  1180. fixpos($$, $1);
  1181. }
  1182. | primary
  1183. {
  1184. $$ = $1;
  1185. }
  1186. ;
  1187. arg_value : arg
  1188. {
  1189. value_expr($1);
  1190. $$ = $1;
  1191. }
  1192. ;
  1193. aref_args : none
  1194. | command opt_nl
  1195. {
  1196. rb_warn(&quot;parenthesize argument(s) for future version&quot;);
  1197. $$ = NEW_LIST($1);
  1198. }
  1199. | args trailer
  1200. {
  1201. $$ = $1;
  1202. }
  1203. | args ',' tSTAR arg opt_nl
  1204. {
  1205. value_expr($4);
  1206. $$ = arg_concat($1, $4);
  1207. }
  1208. | assocs trailer
  1209. {
  1210. $$ = NEW_LIST(NEW_HASH($1));
  1211. }
  1212. | tSTAR arg opt_nl
  1213. {
  1214. value_expr($2);
  1215. $$ = NEW_NEWLINE(NEW_SPLAT($2));
  1216. }
  1217. ;
  1218. paren_args : '(' none ')'
  1219. {
  1220. $$ = $2;
  1221. }
  1222. | '(' call_args opt_nl ')'
  1223. {
  1224. $$ = $2;
  1225. }
  1226. | '(' block_call opt_nl ')'
  1227. {
  1228. rb_warn(&quot;parenthesize argument for future version&quot;);
  1229. $$ = NEW_LIST($2);
  1230. }
  1231. | '(' args ',' block_call opt_nl ')'
  1232. {
  1233. rb_warn(&quot;parenthesize argument for future version&quot;);
  1234. $$ = list_append($2, $4);
  1235. }
  1236. ;
  1237. opt_paren_args : none
  1238. | paren_args
  1239. ;
  1240. call_args : command
  1241. {
  1242. rb_warn(&quot;parenthesize argument(s) for future version&quot;);
  1243. $$ = NEW_LIST($1);
  1244. }
  1245. | args opt_block_arg
  1246. {
  1247. $$ = arg_blk_pass($1, $2);
  1248. }
  1249. | args ',' tSTAR arg_value opt_block_arg
  1250. {
  1251. $$ = arg_concat($1, $4);
  1252. $$ = arg_blk_pass($$, $5);
  1253. }
  1254. | assocs opt_block_arg
  1255. {
  1256. $$ = NEW_LIST(NEW_HASH($1));
  1257. $$ = arg_blk_pass($$, $2);
  1258. }
  1259. | assocs ',' tSTAR arg_value opt_block_arg
  1260. {
  1261. $$ = arg_concat(NEW_LIST(NEW_HASH($1)), $4);
  1262. $$ = arg_blk_pass($$, $5);
  1263. }
  1264. | args ',' assocs opt_block_arg
  1265. {
  1266. $$ = list_append($1, NEW_HASH($3));
  1267. $$ = arg_blk_pass($$, $4);
  1268. }
  1269. | args ',' assocs ',' tSTAR arg opt_block_arg
  1270. {
  1271. value_expr($6);
  1272. $$ = arg_concat(list_append($1, NEW_HASH($3)), $6);
  1273. $$ = arg_blk_pass($$, $7);
  1274. }
  1275. | tSTAR arg_value opt_block_arg
  1276. {
  1277. $$ = arg_blk_pass(NEW_SPLAT($2), $3);
  1278. }
  1279. | block_arg
  1280. ;
  1281. call_args2 : arg_value ',' args opt_block_arg
  1282. {
  1283. $$ = arg_blk_pass(list_concat(NEW_LIST($1),$3), $4);
  1284. }
  1285. | arg_value ',' block_arg
  1286. {
  1287. $$ = arg_blk_pass($1, $3);
  1288. }
  1289. | arg_value ',' tSTAR arg_value opt_block_arg
  1290. {
  1291. $$ = arg_concat(NEW_LIST($1), $4);
  1292. $$ = arg_blk_pass($$, $5);
  1293. }
  1294. | arg_value ',' args ',' tSTAR arg_value opt_block_arg
  1295. {
  1296. $$ = arg_concat(list_concat(NEW_LIST($1),$3), $6);
  1297. $$ = arg_blk_pass($$, $7);
  1298. }
  1299. | assocs opt_block_arg
  1300. {
  1301. $$ = NEW_LIST(NEW_HASH($1));
  1302. $$ = arg_blk_pass($$, $2);
  1303. }
  1304. | assocs ',' tSTAR arg_value opt_block_arg
  1305. {
  1306. $$ = arg_concat(NEW_LIST(NEW_HASH($1)), $4);
  1307. $$ = arg_blk_pass($$, $5);
  1308. }
  1309. | arg_value ',' assocs opt_block_arg
  1310. {
  1311. $$ = list_append(NEW_LIST($1), NEW_HASH($3));
  1312. $$ = arg_blk_pass($$, $4);
  1313. }
  1314. | arg_value ',' args ',' assocs opt_block_arg
  1315. {
  1316. $$ = list_append(list_concat(NEW_LIST($1),$3), NEW_HASH($5));
  1317. $$ = arg_blk_pass($$, $6);
  1318. }
  1319. | arg_value ',' assocs ',' tSTAR arg_value opt_block_arg
  1320. {
  1321. $$ = arg_concat(list_append(NEW_LIST($1), NEW_HASH($3)), $6);
  1322. $$ = arg_blk_pass($$, $7);
  1323. }
  1324. | arg_value ',' args ',' assocs ',' tSTAR arg_value opt_block_arg
  1325. {
  1326. $$ = arg_concat(list_append(list_concat(NEW_LIST($1), $3), NEW_HASH($5)), $8);
  1327. $$ = arg_blk_pass($$, $9);
  1328. }
  1329. | tSTAR arg_value opt_block_arg
  1330. {
  1331. $$ = arg_blk_pass(NEW_SPLAT($2), $3);
  1332. }
  1333. | block_arg
  1334. ;
  1335. command_args : {
  1336. $&lt;num&gt;$ = cmdarg_stack;
  1337. CMDARG_PUSH(1);
  1338. }
  1339. open_args
  1340. {
  1341. /* CMDARG_POP() */
  1342. cmdarg_stack = $&lt;num&gt;1;
  1343. $$ = $2;
  1344. }
  1345. ;
  1346. open_args : call_args
  1347. | tLPAREN_ARG {lex_state = EXPR_ENDARG;} ')'
  1348. {
  1349. rb_warn(&quot;don't put space before argument parentheses&quot;);
  1350. $$ = 0;
  1351. }
  1352. | tLPAREN_ARG call_args2 {lex_state = EXPR_ENDARG;} ')'
  1353. {
  1354. rb_warn(&quot;don't put space before argument parentheses&quot;);
  1355. $$ = $2;
  1356. }
  1357. ;
  1358. block_arg : tAMPER arg_value
  1359. {
  1360. $$ = NEW_BLOCK_PASS($2);
  1361. }
  1362. ;
  1363. opt_block_arg : ',' block_arg
  1364. {
  1365. $$ = $2;
  1366. }
  1367. | none
  1368. ;
  1369. args : arg_value
  1370. {
  1371. $$ = NEW_LIST($1);
  1372. }
  1373. | args ',' arg_value
  1374. {
  1375. $$ = list_append($1, $3);
  1376. }
  1377. ;
  1378. mrhs : args ',' arg_value
  1379. {
  1380. $$ = list_append($1, $3);
  1381. }
  1382. | args ',' tSTAR arg_value
  1383. {
  1384. $$ = arg_concat($1, $4);
  1385. }
  1386. | tSTAR arg_value
  1387. {
  1388. $$ = NEW_SPLAT($2);
  1389. }
  1390. ;
  1391. primary : literal
  1392. | strings
  1393. | xstring
  1394. | regexp
  1395. | words
  1396. | qwords
  1397. | var_ref
  1398. | backref
  1399. | tFID
  1400. {
  1401. $$ = NEW_FCALL($1, 0);
  1402. }
  1403. | kBEGIN
  1404. {
  1405. $&lt;num&gt;1 = ruby_sourceline;
  1406. }
  1407. bodystmt
  1408. kEND
  1409. {
  1410. if ($3 == NULL)
  1411. $$ = NEW_NIL();
  1412. else
  1413. $$ = NEW_BEGIN($3);
  1414. nd_set_line($$, $&lt;num&gt;1);
  1415. }
  1416. | tLPAREN_ARG expr {lex_state = EXPR_ENDARG;} opt_nl ')'
  1417. {
  1418. rb_warning(&quot;(...) interpreted as grouped expression&quot;);
  1419. $$ = $2;
  1420. }
  1421. | tLPAREN compstmt ')'
  1422. {
  1423. if (!$2) $$ = NEW_NIL();
  1424. else $$ = $2;
  1425. }
  1426. | primary_value tCOLON2 tCONSTANT
  1427. {
  1428. $$ = NEW_COLON2($1, $3);
  1429. }
  1430. | tCOLON3 tCONSTANT
  1431. {
  1432. $$ = NEW_COLON3($2);
  1433. }
  1434. | primary_value '[' aref_args ']'
  1435. {
  1436. if ($1 &amp;&amp; nd_type($1) == NODE_SELF)
  1437. $$ = NEW_FCALL(tAREF, $3);
  1438. else
  1439. $$ = NEW_CALL($1, tAREF, $3);
  1440. fixpos($$, $1);
  1441. }
  1442. | tLBRACK aref_args ']'
  1443. {
  1444. if ($2 == 0) {
  1445. $$ = NEW_ZARRAY(); /* zero length array*/
  1446. }
  1447. else {
  1448. $$ = $2;
  1449. }
  1450. }
  1451. | tLBRACE assoc_list '}'
  1452. {
  1453. $$ = NEW_HASH($2);
  1454. }
  1455. | kRETURN
  1456. {
  1457. $$ = NEW_RETURN(0);
  1458. }
  1459. | kYIELD '(' call_args ')'
  1460. {
  1461. $$ = new_yield($3);
  1462. }
  1463. | kYIELD '(' ')'
  1464. {
  1465. $$ = NEW_YIELD(0, Qfalse);
  1466. }
  1467. | kYIELD
  1468. {
  1469. $$ = NEW_YIELD(0, Qfalse);
  1470. }
  1471. | kDEFINED opt_nl '(' {in_defined = 1;} expr ')'
  1472. {
  1473. in_defined = 0;
  1474. $$ = NEW_DEFINED($5);
  1475. }
  1476. | operation brace_block
  1477. {
  1478. $2-&gt;nd_iter = NEW_FCALL($1, 0);
  1479. $$ = $2;
  1480. fixpos($2-&gt;nd_iter, $2);
  1481. }
  1482. | method_call
  1483. | method_call brace_block
  1484. {
  1485. if ($1 &amp;&amp; nd_type($1) == NODE_BLOCK_PASS) {
  1486. rb_compile_error(&quot;both block arg and actual block given&quot;);
  1487. }
  1488. $2-&gt;nd_iter = $1;
  1489. $$ = $2;
  1490. fixpos($$, $1);
  1491. }
  1492. | kIF expr_value then
  1493. compstmt
  1494. if_tail
  1495. kEND
  1496. {
  1497. $$ = NEW_IF(cond($2), $4, $5);
  1498. fixpos($$, $2);
  1499. if (cond_negative(&amp;$$-&gt;nd_cond)) {
  1500. NODE *tmp = $$-&gt;nd_body;
  1501. $$-&gt;nd_body = $$-&gt;nd_else;
  1502. $$-&gt;nd_else = tmp;
  1503. }
  1504. }
  1505. | kUNLESS expr_value then
  1506. compstmt
  1507. opt_else
  1508. kEND
  1509. {
  1510. $$ = NEW_UNLESS(cond($2), $4, $5);
  1511. fixpos($$, $2);
  1512. if (cond_negative(&amp;$$-&gt;nd_cond)) {
  1513. NODE *tmp = $$-&gt;nd_body;
  1514. $$-&gt;nd_body = $$-&gt;nd_else;
  1515. $$-&gt;nd_else = tmp;
  1516. }
  1517. }
  1518. | kWHILE {COND_PUSH(1);} expr_value do {COND_POP();}
  1519. compstmt
  1520. kEND
  1521. {
  1522. $$ = NEW_WHILE(cond($3), $6, 1);
  1523. fixpos($$, $3);
  1524. if (cond_negative(&amp;$$-&gt;nd_cond)) {
  1525. nd_set_type($$, NODE_UNTIL);
  1526. }
  1527. }
  1528. | kUNTIL {COND_PUSH(1);} expr_value do {COND_POP();}
  1529. compstmt
  1530. kEND
  1531. {
  1532. $$ = NEW_UNTIL(cond($3), $6, 1);
  1533. fixpos($$, $3);
  1534. if (cond_negative(&amp;$$-&gt;nd_cond)) {
  1535. nd_set_type($$, NODE_WHILE);
  1536. }
  1537. }
  1538. | kCASE expr_value opt_terms
  1539. case_body
  1540. kEND
  1541. {
  1542. $$ = NEW_CASE($2, $4);
  1543. fixpos($$, $2);
  1544. }
  1545. | kCASE opt_terms case_body kEND
  1546. {
  1547. $$ = $3;
  1548. }
  1549. | kCASE opt_terms kELSE compstmt kEND
  1550. {
  1551. $$ = $4;
  1552. }
  1553. | kFOR block_var kIN {COND_PUSH(1);} expr_value do {COND_POP();}
  1554. compstmt
  1555. kEND
  1556. {
  1557. $$ = NEW_FOR($2, $5, $8);
  1558. fixpos($$, $2);
  1559. }
  1560. | kCLASS cpath superclass
  1561. {
  1562. if (in_def || in_single)
  1563. yyerror(&quot;class definition in method body&quot;);
  1564. class_nest++;
  1565. local_push(0);
  1566. $&lt;num&gt;$ = ruby_sourceline;
  1567. }
  1568. bodystmt
  1569. kEND
  1570. {
  1571. $$ = NEW_CLASS($2, $5, $3);
  1572. nd_set_line($$, $&lt;num&gt;4);
  1573. local_pop();
  1574. class_nest--;
  1575. }
  1576. | kCLASS tLSHFT expr
  1577. {
  1578. $&lt;num&gt;$ = in_def;
  1579. in_def = 0;
  1580. }
  1581. term
  1582. {
  1583. $&lt;num&gt;$ = in_single;
  1584. in_single = 0;
  1585. class_nest++;
  1586. local_push(0);
  1587. }
  1588. bodystmt
  1589. kEND
  1590. {
  1591. $$ = NEW_SCLASS($3, $7);
  1592. fixpos($$, $3);
  1593. local_pop();
  1594. class_nest--;
  1595. in_def = $&lt;num&gt;4;
  1596. in_single = $&lt;num&gt;6;
  1597. }
  1598. | kMODULE cpath
  1599. {
  1600. if (in_def || in_single)
  1601. yyerror(&quot;module definition in method body&quot;);
  1602. class_nest++;
  1603. local_push(0);
  1604. $&lt;num&gt;$ = ruby_sourceline;
  1605. }
  1606. bodystmt
  1607. kEND
  1608. {
  1609. $$ = NEW_MODULE($2, $4);
  1610. nd_set_line($$, $&lt;num&gt;3);
  1611. local_pop();
  1612. class_nest--;
  1613. }
  1614. | kDEF fname
  1615. {
  1616. $&lt;id&gt;$ = cur_mid;
  1617. cur_mid = $2;
  1618. in_def++;
  1619. local_push(0);
  1620. }
  1621. f_arglist
  1622. bodystmt
  1623. kEND
  1624. {
  1625. if (!$5) $5 = NEW_NIL();
  1626. $$ = NEW_DEFN($2, $4, $5, NOEX_PRIVATE);
  1627. fixpos($$, $4);
  1628. local_pop();
  1629. in_def--;
  1630. cur_mid = $&lt;id&gt;3;
  1631. }
  1632. | kDEF singleton dot_or_colon {lex_state = EXPR_FNAME;} fname
  1633. {
  1634. in_single++;
  1635. local_push(0);
  1636. lex_state = EXPR_END; /* force for args */
  1637. }
  1638. f_arglist
  1639. bodystmt
  1640. kEND
  1641. {
  1642. $$ = NEW_DEFS($2, $5, $7, $8);
  1643. fixpos($$, $2);
  1644. local_pop();
  1645. in_single--;
  1646. }
  1647. | kBREAK
  1648. {
  1649. $$ = NEW_BREAK(0);
  1650. }
  1651. | kNEXT
  1652. {
  1653. $$ = NEW_NEXT(0);
  1654. }
  1655. | kREDO
  1656. {
  1657. $$ = NEW_REDO();
  1658. }
  1659. | kRETRY
  1660. {
  1661. $$ = NEW_RETRY();
  1662. }
  1663. ;
  1664. primary_value : primary
  1665. {
  1666. value_expr($1);
  1667. $$ = $1;
  1668. }
  1669. ;
  1670. then : term
  1671. | ':'
  1672. | kTHEN
  1673. | term kTHEN
  1674. ;
  1675. do : term
  1676. | ':'
  1677. | kDO_COND
  1678. ;
  1679. if_tail : opt_else
  1680. | kELSIF expr_value then
  1681. compstmt
  1682. if_tail
  1683. {
  1684. $$ = NEW_IF(cond($2), $4, $5);
  1685. fixpos($$, $2);
  1686. }
  1687. ;
  1688. opt_else : none
  1689. | kELSE compstmt
  1690. {
  1691. $$ = $2;
  1692. }
  1693. ;
  1694. block_var : lhs
  1695. | mlhs
  1696. ;
  1697. opt_block_var : none
  1698. | '|' /* none */ '|'
  1699. {
  1700. $$ = (NODE*)1;
  1701. }
  1702. | tOROP
  1703. {
  1704. $$ = (NODE*)1;
  1705. }
  1706. | '|' block_var '|'
  1707. {
  1708. $$ = $2;
  1709. }
  1710. ;
  1711. do_block : kDO_BLOCK
  1712. {
  1713. $&lt;vars&gt;$ = dyna_push();
  1714. $&lt;num&gt;1 = ruby_sourceline;
  1715. }
  1716. opt_block_var {$&lt;vars&gt;$ = ruby_dyna_vars;}
  1717. compstmt
  1718. kEND
  1719. {
  1720. $$ = NEW_ITER($3, 0, dyna_init($5, $&lt;vars&gt;4));
  1721. nd_set_line($$, $&lt;num&gt;1);
  1722. dyna_pop($&lt;vars&gt;2);
  1723. }
  1724. ;
  1725. block_call : command do_block
  1726. {
  1727. if ($1 &amp;&amp; nd_type($1) == NODE_BLOCK_PASS) {
  1728. rb_compile_error(&quot;both block arg and actual block given&quot;);
  1729. }
  1730. $2-&gt;nd_iter = $1;
  1731. $$ = $2;
  1732. fixpos($$, $1);
  1733. }
  1734. | block_call '.' operation2 opt_paren_args
  1735. {
  1736. $$ = new_call($1, $3, $4);
  1737. }
  1738. | block_call tCOLON2 operation2 opt_paren_args
  1739. {
  1740. $$ = new_call($1, $3, $4);
  1741. }
  1742. ;
  1743. method_call : operation paren_args
  1744. {
  1745. $$ = new_fcall($1, $2);
  1746. fixpos($$, $2);
  1747. }
  1748. | primary_value '.' operation2 opt_paren_args
  1749. {
  1750. $$ = new_call($1, $3, $4);
  1751. fixpos($$, $1);
  1752. }
  1753. | primary_value tCOLON2 operation2 paren_args
  1754. {
  1755. $$ = new_call($1, $3, $4);
  1756. fixpos($$, $1);
  1757. }
  1758. | primary_value tCOLON2 operation3
  1759. {
  1760. $$ = new_call($1, $3, 0);
  1761. }
  1762. | kSUPER paren_args
  1763. {
  1764. $$ = new_super($2);
  1765. }
  1766. | kSUPER
  1767. {
  1768. $$ = NEW_ZSUPER();
  1769. }
  1770. ;
  1771. brace_block : '{'
  1772. {
  1773. $&lt;vars&gt;$ = dyna_push();
  1774. $&lt;num&gt;1 = ruby_sourceline;
  1775. }
  1776. opt_block_var {$&lt;vars&gt;$ = ruby_dyna_vars;}
  1777. compstmt '}'
  1778. {
  1779. $$ = NEW_ITER($3, 0, dyna_init($5, $&lt;vars&gt;4));
  1780. nd_set_line($$, $&lt;num&gt;1);
  1781. dyna_pop($&lt;vars&gt;2);
  1782. }
  1783. | kDO
  1784. {
  1785. $&lt;vars&gt;$ = dyna_push();
  1786. $&lt;num&gt;1 = ruby_sourceline;
  1787. }
  1788. opt_block_var {$&lt;vars&gt;$ = ruby_dyna_vars;}
  1789. compstmt kEND
  1790. {
  1791. $$ = NEW_ITER($3, 0, dyna_init($5, $&lt;vars&gt;4));
  1792. nd_set_line($$, $&lt;num&gt;1);
  1793. dyna_pop($&lt;vars&gt;2);
  1794. }
  1795. ;
  1796. case_body : kWHEN when_args then
  1797. compstmt
  1798. cases
  1799. {
  1800. $$ = NEW_WHEN($2, $4, $5);
  1801. }
  1802. ;
  1803. when_args : args
  1804. | args ',' tSTAR arg_value
  1805. {
  1806. $$ = list_append($1, NEW_WHEN($4, 0, 0));
  1807. }
  1808. | tSTAR arg_value
  1809. {
  1810. $$ = NEW_LIST(NEW_WHEN($2, 0, 0));
  1811. }
  1812. ;
  1813. cases : opt_else
  1814. | case_body
  1815. ;
  1816. opt_rescue : kRESCUE exc_list exc_var then
  1817. compstmt
  1818. opt_rescue
  1819. {
  1820. if ($3) {
  1821. $3 = node_assign($3, NEW_GVAR(rb_intern(&quot;$!&quot;)));
  1822. $5 = block_append($3, $5);
  1823. }
  1824. $$ = NEW_RESBODY($2, $5, $6);
  1825. fixpos($$, $2?$2:$5);
  1826. }
  1827. | none
  1828. ;
  1829. exc_list : arg_value
  1830. {
  1831. $$ = NEW_LIST($1);
  1832. }
  1833. | mrhs
  1834. | none
  1835. ;
  1836. exc_var : tASSOC lhs
  1837. {
  1838. $$ = $2;
  1839. }
  1840. | none
  1841. ;
  1842. opt_ensure : kENSURE compstmt
  1843. {
  1844. if ($2)
  1845. $$ = $2;
  1846. else
  1847. /* place holder */
  1848. $$ = NEW_NIL();
  1849. }
  1850. | none
  1851. ;
  1852. literal : numeric
  1853. | symbol
  1854. {
  1855. $$ = NEW_LIT(ID2SYM($1));
  1856. }
  1857. | dsym
  1858. ;
  1859. strings : string
  1860. {
  1861. NODE *node = $1;
  1862. if (!node) {
  1863. node = NEW_STR(rb_str_new(0, 0));
  1864. }
  1865. else {
  1866. node = evstr2dstr(node);
  1867. }
  1868. $$ = node;
  1869. }
  1870. ;
  1871. string : string1
  1872. | string string1
  1873. {
  1874. $$ = literal_concat($1, $2);
  1875. }
  1876. ;
  1877. string1 : tSTRING_BEG string_contents tSTRING_END
  1878. {
  1879. $$ = $2;
  1880. }
  1881. ;
  1882. xstring : tXSTRING_BEG xstring_contents tSTRING_END
  1883. {
  1884. NODE *node = $2;
  1885. if (!node) {
  1886. node = NEW_XSTR(rb_str_new(0, 0));
  1887. }
  1888. else {
  1889. switch (nd_type(node)) {
  1890. case NODE_STR:
  1891. nd_set_type(node, NODE_XSTR);
  1892. break;
  1893. case NODE_DSTR:
  1894. nd_set_type(node, NODE_DXSTR);
  1895. break;
  1896. default:
  1897. node = NEW_NODE(NODE_DXSTR, rb_str_new(0, 0), 1, NEW_LIST(node));
  1898. break;
  1899. }
  1900. }
  1901. $$ = node;
  1902. }
  1903. ;
  1904. regexp : tREGEXP_BEG xstring_contents tREGEXP_END
  1905. {
  1906. int options = $3;
  1907. NODE *node = $2;
  1908. if (!node) {
  1909. node = NEW_LIT(rb_reg_new(&quot;&quot;, 0, options &amp; ~RE_OPTION_ONCE));
  1910. }
  1911. else switch (nd_type(node)) {
  1912. case NODE_STR:
  1913. {
  1914. VALUE src = node-&gt;nd_lit;
  1915. nd_set_type(node, NODE_LIT);
  1916. node-&gt;nd_lit = rb_reg_new(RSTRING(src)-&gt;ptr,
  1917. RSTRING(src)-&gt;len,
  1918. options &amp; ~RE_OPTION_ONCE);
  1919. }
  1920. break;
  1921. default:
  1922. node = NEW_NODE(NODE_DSTR, rb_str_new(0, 0), 1, NEW_LIST(node));
  1923. case NODE_DSTR:
  1924. if (options &amp; RE_OPTION_ONCE) {
  1925. nd_set_type(node, NODE_DREGX_ONCE);
  1926. }
  1927. else {
  1928. nd_set_type(node, NODE_DREGX);
  1929. }
  1930. node-&gt;nd_cflag = options &amp; ~RE_OPTION_ONCE;
  1931. break;
  1932. }
  1933. $$ = node;
  1934. }
  1935. ;
  1936. words : tWORDS_BEG ' ' tSTRING_END
  1937. {
  1938. $$ = NEW_ZARRAY();
  1939. }
  1940. | tWORDS_BEG word_list tSTRING_END
  1941. {
  1942. $$ = $2;
  1943. }
  1944. ;
  1945. word_list : /* none */
  1946. {
  1947. $$ = 0;
  1948. }
  1949. | word_list word ' '
  1950. {
  1951. $$ = list_append($1, evstr2dstr($2));
  1952. }
  1953. ;
  1954. word : string_content
  1955. | word string_content
  1956. {
  1957. $$ = literal_concat($1, $2);
  1958. }
  1959. ;
  1960. qwords : tQWORDS_BEG ' ' tSTRING_END
  1961. {
  1962. $$ = NEW_ZARRAY();
  1963. }
  1964. | tQWORDS_BEG qword_list tSTRING_END
  1965. {
  1966. $$ = $2;
  1967. }
  1968. ;
  1969. qword_list : /* none */
  1970. {
  1971. $$ = 0;
  1972. }
  1973. | qword_list tSTRING_CONTENT ' '
  1974. {
  1975. $$ = list_append($1, $2);
  1976. }
  1977. ;
  1978. string_contents : /* none */
  1979. {
  1980. $$ = 0;
  1981. }
  1982. | string_contents string_content
  1983. {
  1984. $$ = literal_concat($1, $2);
  1985. }
  1986. ;
  1987. xstring_contents: /* none */
  1988. {
  1989. $$ = 0;
  1990. }
  1991. | xstring_contents string_content
  1992. {
  1993. $$ = literal_concat($1, $2);
  1994. }
  1995. ;
  1996. string_content : tSTRING_CONTENT
  1997. | tSTRING_DVAR
  1998. {
  1999. $&lt;node&gt;$ = lex_strterm;
  2000. lex_strterm = 0;
  2001. lex_state = EXPR_BEG;
  2002. }
  2003. string_dvar
  2004. {
  2005. lex_strterm = $&lt;node&gt;2;
  2006. $$ = NEW_EVSTR($3);
  2007. }
  2008. | tSTRING_DBEG
  2009. {
  2010. $&lt;node&gt;$ = lex_strterm;
  2011. lex_strterm = 0;
  2012. lex_state = EXPR_BEG;
  2013. COND_PUSH(0);
  2014. CMDARG_PUSH(0);
  2015. }
  2016. compstmt '}'
  2017. {
  2018. lex_strterm = $&lt;node&gt;2;
  2019. COND_LEXPOP();
  2020. CMDARG_LEXPOP();
  2021. if (($$ = $3) &amp;&amp; nd_type($$) == NODE_NEWLINE) {
  2022. $$ = $$-&gt;nd_next;
  2023. rb_gc_force_recycle((VALUE)$3);
  2024. }
  2025. $$ = new_evstr($$);
  2026. }
  2027. ;
  2028. string_dvar : tGVAR {$$ = NEW_GVAR($1);}
  2029. | tIVAR {$$ = NEW_IVAR($1);}
  2030. | tCVAR {$$ = NEW_CVAR($1);}
  2031. | backref
  2032. ;
  2033. symbol : tSYMBEG sym
  2034. {
  2035. lex_state = EXPR_END;
  2036. $$ = $2;
  2037. }
  2038. ;
  2039. sym : fname
  2040. | tIVAR
  2041. | tGVAR
  2042. | tCVAR
  2043. ;
  2044. dsym : tSYMBEG xstring_contents tSTRING_END
  2045. {
  2046. lex_state = EXPR_END;
  2047. if (!($$ = $2)) {
  2048. $$ = NEW_NIL();
  2049. yyerror(&quot;empty symbol literal&quot;);
  2050. }
  2051. else {
  2052. VALUE lit;
  2053. switch (nd_type($$)) {
  2054. case NODE_DSTR:
  2055. nd_set_type($$, NODE_DSYM);
  2056. break;
  2057. case NODE_STR:
  2058. lit = $$-&gt;nd_lit;
  2059. if (RSTRING(lit)-&gt;len == 0) {
  2060. yyerror(&quot;empty symbol literal&quot;);
  2061. break;
  2062. }
  2063. if (strlen(RSTRING(lit)-&gt;ptr) == RSTRING(lit)-&gt;len) {
  2064. $$-&gt;nd_lit = ID2SYM(rb_intern(RSTRING($$-&gt;nd_lit)-&gt;ptr));
  2065. nd_set_type($$, NODE_LIT);
  2066. break;
  2067. }
  2068. /* fall through */
  2069. default:
  2070. $$ = NEW_NODE(NODE_DSYM, rb_str_new(0, 0), 1, NEW_LIST($$));
  2071. break;
  2072. }
  2073. }
  2074. }
  2075. ;
  2076. numeric : tINTEGER
  2077. | tFLOAT
  2078. | tUMINUS_NUM tINTEGER %prec tLOWEST
  2079. {
  2080. $$ = negate_lit($2);
  2081. }
  2082. | tUMINUS_NUM tFLOAT %prec tLOWEST
  2083. {
  2084. $$ = negate_lit($2);
  2085. }
  2086. ;
  2087. variable : tIDENTIFIER
  2088. | tIVAR
  2089. | tGVAR
  2090. | tCONSTANT
  2091. | tCVAR
  2092. | kNIL {$$ = kNIL;}
  2093. | kSELF {$$ = kSELF;}
  2094. | kTRUE {$$ = kTRUE;}
  2095. | kFALSE {$$ = kFALSE;}
  2096. | k__FILE__ {$$ = k__FILE__;}
  2097. | k__LINE__ {$$ = k__LINE__;}
  2098. ;
  2099. var_ref : variable
  2100. {
  2101. $$ = gettable($1);
  2102. }
  2103. ;
  2104. var_lhs : variable
  2105. {
  2106. $$ = assignable($1, 0);
  2107. }
  2108. ;
  2109. backref : tNTH_REF
  2110. | tBACK_REF
  2111. ;
  2112. superclass : term
  2113. {
  2114. $$ = 0;
  2115. }
  2116. | '&lt;'
  2117. {
  2118. lex_state = EXPR_BEG;
  2119. }
  2120. expr_value term
  2121. {
  2122. $$ = $3;
  2123. }
  2124. | error term {yyerrok; $$ = 0;}
  2125. ;
  2126. f_arglist : '(' f_args opt_nl ')'
  2127. {
  2128. $$ = $2;
  2129. lex_state = EXPR_BEG;
  2130. command_start = Qtrue;
  2131. }
  2132. | f_args term
  2133. {
  2134. $$ = $1;
  2135. }
  2136. ;
  2137. f_args : f_arg ',' f_optarg ',' f_rest_arg opt_f_block_arg
  2138. {
  2139. $$ = block_append(NEW_ARGS($1, $3, $5), $6);
  2140. }
  2141. | f_arg ',' f_optarg opt_f_block_arg
  2142. {
  2143. $$ = block_append(NEW_ARGS($1, $3, 0), $4);
  2144. }
  2145. | f_arg ',' f_rest_arg opt_f_block_arg
  2146. {
  2147. $$ = block_append(NEW_ARGS($1, 0, $3), $4);
  2148. }
  2149. | f_arg opt_f_block_arg
  2150. {
  2151. $$ = block_append(NEW_ARGS($1, 0, 0), $2);
  2152. }
  2153. | f_optarg ',' f_rest_arg opt_f_block_arg
  2154. {
  2155. $$ = block_append(NEW_ARGS(0, $1, $3), $4);
  2156. }
  2157. | f_optarg opt_f_block_arg
  2158. {
  2159. $$ = block_append(NEW_ARGS(0, $1, 0), $2);
  2160. }
  2161. | f_rest_arg opt_f_block_arg
  2162. {
  2163. $$ = block_append(NEW_ARGS(0, 0, $1), $2);
  2164. }
  2165. | f_block_arg
  2166. {
  2167. $$ = block_append(NEW_ARGS(0, 0, 0), $1);
  2168. }
  2169. | /* none */
  2170. {
  2171. $$ = NEW_ARGS(0, 0, 0);
  2172. }
  2173. ;
  2174. f_norm_arg : tCONSTANT
  2175. {
  2176. yyerror(&quot;formal argument cannot be a constant&quot;);
  2177. }
  2178. | tIVAR
  2179. {
  2180. yyerror(&quot;formal argument cannot be an instance variable&quot;);
  2181. }
  2182. | tGVAR
  2183. {
  2184. yyerror(&quot;formal argument cannot be a global variable&quot;);
  2185. }
  2186. | tCVAR
  2187. {
  2188. yyerror(&quot;formal argument cannot be a class variable&quot;);
  2189. }
  2190. | tIDENTIFIER
  2191. {
  2192. if (!is_local_id($1))
  2193. yyerror(&quot;formal argument must be local variable&quot;);
  2194. else if (local_id($1))
  2195. yyerror(&quot;duplicate argument name&quot;);
  2196. local_cnt($1);
  2197. $$ = 1;
  2198. }
  2199. ;
  2200. f_arg : f_norm_arg
  2201. | f_arg ',' f_norm_arg
  2202. {
  2203. $$ += 1;
  2204. }
  2205. ;
  2206. f_opt : tIDENTIFIER '=' arg_value
  2207. {
  2208. if (!is_local_id($1))
  2209. yyerror(&quot;formal argument must be local variable&quot;);
  2210. else if (local_id($1))
  2211. yyerror(&quot;duplicate optional argument name&quot;);
  2212. $$ = assignable($1, $3);
  2213. }
  2214. ;
  2215. f_optarg : f_opt
  2216. {
  2217. $$ = NEW_BLOCK($1);
  2218. $$-&gt;nd_end = $$;
  2219. }
  2220. | f_optarg ',' f_opt
  2221. {
  2222. $$ = block_append($1, $3);
  2223. }
  2224. ;
  2225. restarg_mark : '*'
  2226. | tSTAR
  2227. ;
  2228. f_rest_arg : restarg_mark tIDENTIFIER
  2229. {
  2230. if (!is_local_id($2))
  2231. yyerror(&quot;rest argument must be local variable&quot;);
  2232. else if (local_id($2))
  2233. yyerror(&quot;duplicate rest argument name&quot;);
  2234. if (dyna_in_block()) {
  2235. rb_dvar_push($2, Qnil);
  2236. }
  2237. $$ = assignable($2, 0);
  2238. }
  2239. | restarg_mark
  2240. {
  2241. if (dyna_in_block()) {
  2242. $$ = NEW_DASGN_CURR(internal_id(), 0);
  2243. }
  2244. else {
  2245. $$ = NEW_NODE(NODE_LASGN,0,0,local_append(0));
  2246. }
  2247. }
  2248. ;
  2249. blkarg_mark : '&amp;'
  2250. | tAMPER
  2251. ;
  2252. f_block_arg : blkarg_mark tIDENTIFIER
  2253. {
  2254. if (!is_local_id($2))
  2255. yyerror(&quot;block argument must be local variable&quot;);
  2256. else if (local_id($2))
  2257. yyerror(&quot;duplicate block argument name&quot;);
  2258. $$ = NEW_BLOCK_ARG($2);
  2259. }
  2260. ;
  2261. opt_f_block_arg : ',' f_b

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