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

/sources/ruby-1.8.5/parse.y

http://rubyworks.googlecode.com/
Happy | 3333 lines | 3086 code | 247 blank | 0 comment | 0 complexity | b077407913da137f78803ef4882ae85f MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, AGPL-3.0, 0BSD, Unlicense

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

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

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