PageRenderTime 72ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

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

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

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