PageRenderTime 58ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/parse.y

https://code.google.com/p/arosruby/
Happy | 3351 lines | 3102 code | 249 blank | 0 comment | 0 complexity | 8264b33deca24e6185c68da38ed582f1 MD5 | raw file
Possible License(s): AGPL-3.0, 0BSD, Unlicense, GPL-2.0, BSD-3-Clause, LGPL-2.1

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

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

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