Ruby  2.7.2p137(2020-10-01revision5445e0435260b449decf2ac16f9d09bae3cafe72)
ripper.y
Go to the documentation of this file.
1 /**********************************************************************
2 
3  parse.y -
4 
5  $Author$
6  created at: Fri May 28 18:02:42 JST 1993
7 
8  Copyright (C) 1993-2007 Yukihiro Matsumoto
9 
10 **********************************************************************/
11 
12 %{
13 
14 #if !YYPURE
15 # error needs pure parser
16 #endif
17 #define YYDEBUG 1
18 #define YYERROR_VERBOSE 1
19 #define YYSTACK_USE_ALLOCA 0
20 #define YYLTYPE rb_code_location_t
21 #define YYLTYPE_IS_DECLARED 1
22 
23 #include "ruby/ruby.h"
24 #include "ruby/st.h"
25 #include "ruby/encoding.h"
26 #include "internal.h"
27 #include "node.h"
28 #include "parse.h"
29 #include "symbol.h"
30 #include "regenc.h"
31 #include <stdio.h>
32 #include <errno.h>
33 #include <ctype.h>
34 #include "probes.h"
35 
36 #ifndef WARN_PAST_SCOPE
37 # define WARN_PAST_SCOPE 0
38 #endif
39 
40 #define TAB_WIDTH 8
41 
42 #define yydebug (p->debug) /* disable the global variable definition */
43 
44 #define YYMALLOC(size) rb_parser_malloc(p, (size))
45 #define YYREALLOC(ptr, size) rb_parser_realloc(p, (ptr), (size))
46 #define YYCALLOC(nelem, size) rb_parser_calloc(p, (nelem), (size))
47 #define YYFREE(ptr) rb_parser_free(p, (ptr))
48 #define YYFPRINTF rb_parser_printf
49 #define YYPRINT(out, tok, val) parser_token_value_print(p, (tok), &(val))
50 #define YY_LOCATION_PRINT(File, loc) \
51  rb_parser_printf(p, "%d.%d-%d.%d", \
52  (loc).beg_pos.lineno, (loc).beg_pos.column,\
53  (loc).end_pos.lineno, (loc).end_pos.column)
54 #define YYLLOC_DEFAULT(Current, Rhs, N) \
55  do \
56  if (N) \
57  { \
58  (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
59  (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
60  } \
61  else \
62  { \
63  (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
64  (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
65  } \
66  while (0)
67 
68 #define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
69  rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
70 #define RUBY_SET_YYLLOC_OF_NONE(Current) \
71  rb_parser_set_location_of_none(p, &(Current))
72 #define RUBY_SET_YYLLOC(Current) \
73  rb_parser_set_location(p, &(Current))
74 #define RUBY_INIT_YYLLOC() \
75  { \
76  {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
77  {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
78  }
79 
80 enum lex_state_bits {
81  EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
82  EXPR_END_bit, /* newline significant, +/- is an operator. */
83  EXPR_ENDARG_bit, /* ditto, and unbound braces. */
84  EXPR_ENDFN_bit, /* ditto, and unbound braces. */
85  EXPR_ARG_bit, /* newline significant, +/- is an operator. */
86  EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
87  EXPR_MID_bit, /* newline significant, +/- is an operator. */
88  EXPR_FNAME_bit, /* ignore newline, no reserved words. */
89  EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
90  EXPR_CLASS_bit, /* immediate after `class', no here document. */
91  EXPR_LABEL_bit, /* flag bit, label is allowed. */
92  EXPR_LABELED_bit, /* flag bit, just after a label. */
93  EXPR_FITEM_bit, /* symbol literal as FNAME. */
94  EXPR_MAX_STATE
95 };
96 /* examine combinations */
97 enum lex_state_e {
98 #define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
99  DEF_EXPR(BEG),
100  DEF_EXPR(END),
101  DEF_EXPR(ENDARG),
102  DEF_EXPR(ENDFN),
103  DEF_EXPR(ARG),
104  DEF_EXPR(CMDARG),
105  DEF_EXPR(MID),
106  DEF_EXPR(FNAME),
107  DEF_EXPR(DOT),
108  DEF_EXPR(CLASS),
109  DEF_EXPR(LABEL),
110  DEF_EXPR(LABELED),
111  DEF_EXPR(FITEM),
112  EXPR_VALUE = EXPR_BEG,
113  EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
114  EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
115  EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN),
116  EXPR_NONE = 0
117 };
118 #define IS_lex_state_for(x, ls) ((x) & (ls))
119 #define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
120 #define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
121 #define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
122 
123 # define SET_LEX_STATE(ls) \
124  (p->lex.state = \
125  (p->debug ? \
126  rb_parser_trace_lex_state(p, p->lex.state, (ls), __LINE__) : \
127  (enum lex_state_e)(ls)))
128 
129 typedef VALUE stack_type;
130 
131 static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
132 
133 # define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
134 # define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
135 # define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
136 # define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
137 # define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
138 
139 /* A flag to identify keyword_do_cond, "do" keyword after condition expression.
140  Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
141 #define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
142 #define COND_POP() BITSTACK_POP(cond_stack)
143 #define COND_P() BITSTACK_SET_P(cond_stack)
144 #define COND_SET(n) BITSTACK_SET(cond_stack, (n))
145 
146 /* A flag to identify keyword_do_block; "do" keyword after command_call.
147  Example: `foo 1, 2 do`. */
148 #define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
149 #define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
150 #define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
151 #define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
152 
153 struct vtable {
154  ID *tbl;
155  int pos;
156  int capa;
157  struct vtable *prev;
158 };
159 
160 struct local_vars {
161  struct vtable *args;
162  struct vtable *vars;
163  struct vtable *used;
164 # if WARN_PAST_SCOPE
165  struct vtable *past;
166 # endif
167  struct local_vars *prev;
168 # ifndef RIPPER
169  struct {
170  NODE *outer, *inner, *current;
171  } numparam;
172 # endif
173 };
174 
175 enum {
176  ORDINAL_PARAM = -1,
177  NO_PARAM = 0,
178  NUMPARAM_MAX = 9,
179 };
180 
181 #define NUMPARAM_ID_P(id) numparam_id_p(id)
182 #define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - tNUMPARAM_1 + 1)
183 #define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 + (idx) - 1))
184 static int
185 numparam_id_p(ID id)
186 {
187  if (!is_local_id(id)) return 0;
188  unsigned int idx = NUMPARAM_ID_TO_IDX(id);
189  return idx > 0 && idx <= NUMPARAM_MAX;
190 }
191 static void numparam_name(struct parser_params *p, ID id);
192 
193 #define DVARS_INHERIT ((void*)1)
194 #define DVARS_TOPSCOPE NULL
195 #define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
196 
197 typedef struct token_info {
198  const char *token;
199  rb_code_position_t beg;
200  int indent;
201  int nonspc;
202  struct token_info *next;
203 } token_info;
204 
205 typedef struct rb_strterm_struct rb_strterm_t;
206 
207 /*
208  Structure of Lexer Buffer:
209 
210  lex.pbeg lex.ptok lex.pcur lex.pend
211  | | | |
212  |------------+------------+------------|
213  |<---------->|
214  token
215 */
216 struct parser_params {
217  rb_imemo_tmpbuf_t *heap;
218 
219  YYSTYPE *lval;
220 
221  struct {
222  rb_strterm_t *strterm;
223  VALUE (*gets)(struct parser_params*,VALUE);
224  VALUE input;
225  VALUE prevline;
226  VALUE lastline;
227  VALUE nextline;
228  const char *pbeg;
229  const char *pcur;
230  const char *pend;
231  const char *ptok;
232  union {
233  long ptr;
234  VALUE (*call)(VALUE, int);
235  } gets_;
236  enum lex_state_e state;
237  /* track the nest level of any parens "()[]{}" */
238  int paren_nest;
239  /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
240  int lpar_beg;
241  /* track the nest level of only braces "{}" */
242  int brace_nest;
243  } lex;
244  stack_type cond_stack;
245  stack_type cmdarg_stack;
246  int tokidx;
247  int toksiz;
248  int tokline;
249  int heredoc_end;
250  int heredoc_indent;
251  int heredoc_line_indent;
252  char *tokenbuf;
253  struct local_vars *lvtbl;
254  st_table *pvtbl;
255  st_table *pktbl;
256  int line_count;
257  int ruby_sourceline; /* current line no. */
258  const char *ruby_sourcefile; /* current source file */
259  VALUE ruby_sourcefile_string;
260  rb_encoding *enc;
261  token_info *token_info;
262  VALUE case_labels;
263  VALUE compile_option;
264 
265  VALUE debug_buffer;
266  VALUE debug_output;
267 
268  ID cur_arg;
269 
270  rb_ast_t *ast;
271  int node_id;
272 
273  int max_numparam;
274 
275  unsigned int command_start:1;
276  unsigned int eofp: 1;
277  unsigned int ruby__end__seen: 1;
278  unsigned int debug: 1;
279  unsigned int has_shebang: 1;
280  unsigned int in_defined: 1;
281  unsigned int in_kwarg: 1;
282  unsigned int in_def: 1;
283  unsigned int in_class: 1;
284  unsigned int token_seen: 1;
285  unsigned int token_info_enabled: 1;
286 # if WARN_PAST_SCOPE
287  unsigned int past_scope_enabled: 1;
288 # endif
289  unsigned int error_p: 1;
290  unsigned int cr_seen: 1;
291 
292 #ifndef RIPPER
293  /* Ruby core only */
294 
295  unsigned int do_print: 1;
296  unsigned int do_loop: 1;
297  unsigned int do_chomp: 1;
298  unsigned int do_split: 1;
299  unsigned int warn_location: 1;
300 
301  NODE *eval_tree_begin;
302  NODE *eval_tree;
303  VALUE error_buffer;
304  VALUE debug_lines;
305  const struct rb_iseq_struct *parent_iseq;
306 #else
307  /* Ripper only */
308 
309  struct {
310  VALUE token;
311  int line;
312  int col;
313  } delayed;
314 
315  VALUE value;
316  VALUE result;
317  VALUE parsing_thread;
318 #endif
319 };
320 
321 #define intern_cstr(n,l,en) rb_intern3(n,l,en)
322 
323 #define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
324 #define STR_NEW0() rb_enc_str_new(0,0,p->enc)
325 #define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
326 #define STR_NEW3(ptr,len,e,func) parser_str_new((ptr),(len),(e),(func),p->enc)
327 #define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
328 
329 static st_table *
330 push_pvtbl(struct parser_params *p)
331 {
332  st_table *tbl = p->pvtbl;
333  p->pvtbl = st_init_numtable();
334  return tbl;
335 }
336 
337 static void
338 pop_pvtbl(struct parser_params *p, st_table *tbl)
339 {
340  st_free_table(p->pvtbl);
341  p->pvtbl = tbl;
342 }
343 
344 static st_table *
345 push_pktbl(struct parser_params *p)
346 {
347  st_table *tbl = p->pktbl;
348  p->pktbl = 0;
349  return tbl;
350 }
351 
352 static void
353 pop_pktbl(struct parser_params *p, st_table *tbl)
354 {
355  if (p->pktbl) st_free_table(p->pktbl);
356  p->pktbl = tbl;
357 }
358 
359 static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
360 #define yyerror0(msg) parser_yyerror(p, NULL, (msg))
361 #define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
362 #define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
363 #define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
364 
365 #ifdef RIPPER
366 #define compile_for_eval (0)
367 #else
368 #define compile_for_eval (p->parent_iseq != 0)
369 #endif
370 
371 #define token_column ((int)(p->lex.ptok - p->lex.pbeg))
372 
373 #define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
374 #define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
375 #define NEW_QCALL(q,r,m,a,loc) NEW_NODE(NODE_CALL_Q(q),r,m,a,loc)
376 
377 #define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
378 
379 static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
380 
381 #ifndef RIPPER
382 static inline void
383 rb_discard_node(struct parser_params *p, NODE *n)
384 {
385  rb_ast_delete_node(p->ast, n);
386 }
387 #endif
388 
389 #ifdef RIPPER
390 static inline VALUE
391 add_mark_object(struct parser_params *p, VALUE obj)
392 {
393  if (!SPECIAL_CONST_P(obj)
394  && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */
395  ) {
396  rb_ast_add_mark_object(p->ast, obj);
397  }
398  return obj;
399 }
400 #else
401 static NODE* node_newnode_with_locals(struct parser_params *, enum node_type, VALUE, VALUE, const rb_code_location_t*);
402 #endif
403 
404 static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE, const rb_code_location_t*);
405 #define rb_node_newnode(type, a1, a2, a3, loc) node_newnode(p, (type), (a1), (a2), (a3), (loc))
406 
407 static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
408 
409 static int
410 parser_get_node_id(struct parser_params *p)
411 {
412  int node_id = p->node_id;
413  p->node_id++;
414  return node_id;
415 }
416 
417 #ifndef RIPPER
418 static inline void
419 set_line_body(NODE *body, int line)
420 {
421  if (!body) return;
422  switch (nd_type(body)) {
423  case NODE_RESCUE:
424  case NODE_ENSURE:
425  nd_set_line(body, line);
426  }
427 }
428 
429 #define yyparse ruby_yyparse
430 
431 static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
432 static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
433 #define new_nil(loc) NEW_NIL(loc)
434 static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
435 static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
436 static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
437 
438 static NODE *newline_node(NODE*);
439 static void fixpos(NODE*,NODE*);
440 
441 static int value_expr_gen(struct parser_params*,NODE*);
442 static void void_expr(struct parser_params*,NODE*);
443 static NODE *remove_begin(NODE*);
444 static NODE *remove_begin_all(NODE*);
445 #define value_expr(node) value_expr_gen(p, (node) = remove_begin(node))
446 static NODE *void_stmts(struct parser_params*,NODE*);
447 static void reduce_nodes(struct parser_params*,NODE**);
448 static void block_dup_check(struct parser_params*,NODE*,NODE*);
449 
450 static NODE *block_append(struct parser_params*,NODE*,NODE*);
451 static NODE *list_append(struct parser_params*,NODE*,NODE*);
452 static NODE *list_concat(NODE*,NODE*);
453 static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
454 static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
455 static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
456 static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
457 static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
458 static NODE *evstr2dstr(struct parser_params*,NODE*);
459 static NODE *splat_array(NODE*);
460 static void mark_lvar_used(struct parser_params *p, NODE *rhs);
461 
462 static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
463 static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
464 static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
465 static NODE *new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc);
466 static NODE *method_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc) {b->nd_iter = m; b->nd_loc = *loc; return b;}
467 
468 static bool args_info_empty_p(struct rb_args_info *args);
469 static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
470 static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
471 static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
472 static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
473 static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
474 static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
475 static NODE *new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc);
476 
477 static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
478 static NODE *args_with_numbered(struct parser_params*,NODE*,int);
479 
480 static VALUE negate_lit(struct parser_params*, VALUE);
481 static NODE *ret_args(struct parser_params*,NODE*);
482 static NODE *arg_blk_pass(NODE*,NODE*);
483 static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
484 static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
485 
486 static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
487 static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
488 
489 static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
490 static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
491 
492 static void rb_backref_error(struct parser_params*,NODE*);
493 static NODE *node_assign(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
494 
495 static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc);
496 static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
497 static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
498 static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc);
499 static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
500 
501 static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
502 
503 static NODE *opt_arg_append(NODE*, NODE*);
504 static NODE *kwd_append(NODE*, NODE*);
505 
506 static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
507 static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
508 
509 static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
510 
511 static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
512 
513 #define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
514 
515 static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
516 
517 static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
518 
519 static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
520 
521 static ID *local_tbl(struct parser_params*);
522 
523 static VALUE reg_compile(struct parser_params*, VALUE, int);
524 static void reg_fragment_setenc(struct parser_params*, VALUE, int);
525 static int reg_fragment_check(struct parser_params*, VALUE, int);
526 static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
527 
528 static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
529 static NODE *heredoc_dedent(struct parser_params*,NODE*);
530 
531 static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
532 
533 #define get_id(id) (id)
534 #define get_value(val) (val)
535 #define get_num(num) (num)
536 #else /* RIPPER */
537 #define NODE_RIPPER NODE_CDECL
538 
539 static inline int ripper_is_node_yylval(VALUE n);
540 
541 static inline VALUE
542 ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
543 {
544  if (ripper_is_node_yylval(c)) c = RNODE(c)->nd_cval;
545  add_mark_object(p, b);
546  add_mark_object(p, c);
547  return (VALUE)NEW_CDECL(a, b, c, &NULL_LOC);
548 }
549 
550 static inline int
551 ripper_is_node_yylval(VALUE n)
552 {
553  return RB_TYPE_P(n, T_NODE) && nd_type(RNODE(n)) == NODE_RIPPER;
554 }
555 
556 #define value_expr(node) ((void)(node))
557 #define remove_begin(node) (node)
558 #define void_stmts(p,x) (x)
559 #define rb_dvar_defined(id, base) 0
560 #define rb_local_defined(id, base) 0
561 static ID ripper_get_id(VALUE);
562 #define get_id(id) ripper_get_id(id)
563 static VALUE ripper_get_value(VALUE);
564 #define get_value(val) ripper_get_value(val)
565 #define get_num(num) (int)get_id(num)
566 static VALUE assignable(struct parser_params*,VALUE);
567 static int id_is_var(struct parser_params *p, ID id);
568 
569 #define method_cond(p,node,loc) (node)
570 #define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
571 #define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
572 #define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
573 #define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
574 
575 #define new_nil(loc) Qnil
576 
577 static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
578 
579 static VALUE const_decl(struct parser_params *p, VALUE path);
580 
581 static VALUE var_field(struct parser_params *p, VALUE a);
582 static VALUE assign_error(struct parser_params *p, VALUE a);
583 
584 static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
585 
586 #endif /* !RIPPER */
587 
588 /* forward declaration */
589 typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
590 
591 RUBY_SYMBOL_EXPORT_BEGIN
592 VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
593 int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
594 enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
595 VALUE rb_parser_lex_state_name(enum lex_state_e state);
596 void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
597 PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
598 YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
599 YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
600 YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
601 RUBY_SYMBOL_EXPORT_END
602 
603 static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
604 static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
605 static void parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp);
606 static ID formal_argument(struct parser_params*, ID);
607 static ID shadowing_lvar(struct parser_params*,ID);
608 static void new_bv(struct parser_params*,ID);
609 
610 static void local_push(struct parser_params*,int);
611 static void local_pop(struct parser_params*);
612 static void local_var(struct parser_params*, ID);
613 static void arg_var(struct parser_params*, ID);
614 static int local_id(struct parser_params *p, ID id);
615 static int local_id_ref(struct parser_params*, ID, ID **);
616 #ifndef RIPPER
617 static ID internal_id(struct parser_params*);
618 #endif
619 
620 static const struct vtable *dyna_push(struct parser_params *);
621 static void dyna_pop(struct parser_params*, const struct vtable *);
622 static int dyna_in_block(struct parser_params*);
623 #define dyna_var(p, id) local_var(p, id)
624 static int dvar_defined(struct parser_params*, ID);
625 static int dvar_defined_ref(struct parser_params*, ID, ID**);
626 static int dvar_curr(struct parser_params*,ID);
627 
628 static int lvar_defined(struct parser_params*, ID);
629 
630 static NODE *numparam_push(struct parser_params *p);
631 static void numparam_pop(struct parser_params *p, NODE *prev_inner);
632 
633 #ifdef RIPPER
634 # define METHOD_NOT idNOT
635 #else
636 # define METHOD_NOT '!'
637 #endif
638 
639 #define idFWD_REST '*'
640 #ifdef RUBY3_KEYWORDS
641 #define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
642 #else
643 #define idFWD_KWREST 0
644 #endif
645 #define idFWD_BLOCK '&'
646 
647 #define RE_OPTION_ONCE (1<<16)
648 #define RE_OPTION_ENCODING_SHIFT 8
649 #define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
650 #define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
651 #define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
652 #define RE_OPTION_MASK 0xff
653 #define RE_OPTION_ARG_ENCODING_NONE 32
654 
655 /* structs for managing terminator of string literal and heredocment */
656 typedef struct rb_strterm_literal_struct {
657  union {
658  VALUE dummy;
659  long nest;
660  } u0;
661  union {
662  VALUE dummy;
663  long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
664  } u1;
665  union {
666  VALUE dummy;
667  long paren; /* '(' of `%q(...)` */
668  } u2;
669  union {
670  VALUE dummy;
671  long term; /* ')' of `%q(...)` */
672  } u3;
673 } rb_strterm_literal_t;
674 
675 #define HERETERM_LENGTH_BITS ((SIZEOF_VALUE - 1) * CHAR_BIT - 1)
676 
677 struct rb_strterm_heredoc_struct {
678  VALUE lastline; /* the string of line that contains `<<"END"` */
679  long offset; /* the column of END in `<<"END"` */
680  int sourceline; /* lineno of the line that contains `<<"END"` */
681  unsigned length /* the length of END in `<<"END"` */
682 #if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
683  : HERETERM_LENGTH_BITS
684 # define HERETERM_LENGTH_MAX ((1U << HERETERM_LENGTH_BITS) - 1)
685 #else
686 # define HERETERM_LENGTH_MAX UINT_MAX
687 #endif
688  ;
689 #if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
690  unsigned quote: 1;
691  unsigned func: 8;
692 #else
693  uint8_t quote;
694  uint8_t func;
695 #endif
696 };
697 STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
698 
699 #define STRTERM_HEREDOC IMEMO_FL_USER0
700 
701 struct rb_strterm_struct {
702  VALUE flags;
703  union {
704  rb_strterm_literal_t literal;
705  rb_strterm_heredoc_t heredoc;
706  } u;
707 };
708 
709 #ifndef RIPPER
710 void
711 rb_strterm_mark(VALUE obj)
712 {
713  rb_strterm_t *strterm = (rb_strterm_t*)obj;
714  if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
715  rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
716  rb_gc_mark(heredoc->lastline);
717  }
718 }
719 #endif
720 
721 #define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
722 size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
723 
724 #define TOKEN2ID(tok) ( \
725  tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
726  tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
727  tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
728  tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
729  tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
730  tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
731  ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
732 
733 /****** Ripper *******/
734 
735 #ifdef RIPPER
736 #define RIPPER_VERSION "0.1.0"
737 
738 static inline VALUE intern_sym(const char *name);
739 
740 #include "eventids1.c"
741 #include "eventids2.c"
742 
743 static VALUE ripper_dispatch0(struct parser_params*,ID);
744 static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
745 static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
746 static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
747 static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
748 static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
749 static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
750 static void ripper_error(struct parser_params *p);
751 
752 #define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
753 #define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
754 #define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
755 #define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
756 #define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
757 #define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
758 #define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e), (f), (g))
759 
760 #define yyparse ripper_yyparse
761 
762 #define ID2VAL(id) STATIC_ID2SYM(id)
763 #define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
764 #define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
765 
766 #define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
767  dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
768 
769 #define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
770 
771 static inline VALUE
772 new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
773 {
774  NODE *t = (NODE *)tail;
775  VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value, block = t->u3.value;
776  return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, escape_Qundef(block));
777 }
778 
779 static inline VALUE
780 new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
781 {
782  NODE *t = rb_node_newnode(NODE_ARGS_AUX, kw_args, kw_rest_arg, block, &NULL_LOC);
783  add_mark_object(p, kw_args);
784  add_mark_object(p, kw_rest_arg);
785  add_mark_object(p, block);
786  return (VALUE)t;
787 }
788 
789 static inline VALUE
790 args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
791 {
792  return args;
793 }
794 
795 static VALUE
796 new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
797 {
798  NODE *t = (NODE *)aryptn;
799  struct rb_ary_pattern_info *apinfo = t->nd_apinfo;
800  VALUE pre_args = Qnil, rest_arg = Qnil, post_args = Qnil;
801 
802  if (apinfo) {
803  pre_args = rb_ary_entry(apinfo->imemo, 0);
804  rest_arg = rb_ary_entry(apinfo->imemo, 1);
805  post_args = rb_ary_entry(apinfo->imemo, 2);
806  }
807 
808  if (!NIL_P(pre_arg)) {
809  if (!NIL_P(pre_args)) {
810  rb_ary_unshift(pre_args, pre_arg);
811  }
812  else {
813  pre_args = rb_ary_new_from_args(1, pre_arg);
814  }
815  }
816  return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
817 }
818 
819 static VALUE
820 new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
821 {
822  NODE *t;
823  struct rb_ary_pattern_info *apinfo;
824 
825  if (has_rest) {
826  rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
827  }
828  else {
829  rest_arg = Qnil;
830  }
831 
832  VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
833  apinfo = ZALLOC(struct rb_ary_pattern_info);
834  rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
835  apinfo->imemo = rb_ary_new_from_args(4, pre_args, rest_arg, post_args, tmpbuf);
836 
837  t = rb_node_newnode(NODE_ARYPTN, Qnil, Qnil, (VALUE)apinfo, &NULL_LOC);
838  RB_OBJ_WRITTEN(p->ast, Qnil, apinfo->imemo);
839 
840  return (VALUE)t;
841 }
842 
843 #define new_hash(p,h,l) rb_ary_new_from_args(0)
844 
845 static VALUE
846 new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
847 {
848  return ary;
849 }
850 
851 static VALUE
852 new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
853 {
854  NODE *t = (NODE *)hshptn;
855  VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value;
856  return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
857 }
858 
859 static VALUE
860 new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
861 {
862  NODE *t;
863  if (kw_rest_arg) {
864  kw_rest_arg = dispatch1(var_field, kw_rest_arg);
865  }
866  else {
867  kw_rest_arg = Qnil;
868  }
869  t = rb_node_newnode(NODE_HSHPTN, kw_args, kw_rest_arg, 0, &NULL_LOC);
870 
871  add_mark_object(p, kw_args);
872  add_mark_object(p, kw_rest_arg);
873  return (VALUE)t;
874 }
875 
876 #define new_defined(p,expr,loc) dispatch1(defined, (expr))
877 
878 static VALUE heredoc_dedent(struct parser_params*,VALUE);
879 
880 #else
881 #define ID2VAL(id) (id)
882 #define TOKEN2VAL(t) ID2VAL(t)
883 #define KWD2EID(t, v) keyword_##t
884 #endif /* RIPPER */
885 
886 #ifndef RIPPER
887 # define Qnone 0
888 # define Qnull 0
889 # define ifndef_ripper(x) (x)
890 #else
891 # define Qnone Qnil
892 # define Qnull Qundef
893 # define ifndef_ripper(x)
894 #endif
895 
896 # define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
897 # define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
898 # define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
899 # define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
900 # define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
901 # define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
902 # define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
903 # define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
904 # define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
905 # define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
906 # define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
907 # define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
908 # define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
909 # define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
910 # define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
911 # define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
912 # define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
913 # define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
914 # define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
915 # define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
916 #ifdef RIPPER
917 static ID id_warn, id_warning, id_gets, id_assoc;
918 # define WARN_S_L(s,l) STR_NEW(s,l)
919 # define WARN_S(s) STR_NEW2(s)
920 # define WARN_I(i) INT2NUM(i)
921 # define WARN_ID(i) rb_id2str(i)
922 # define WARN_IVAL(i) i
923 # define PRIsWARN "s"
924 # define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
925 # define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
926 # ifdef HAVE_VA_ARGS_MACRO
927 # define WARN_CALL(...) rb_funcall(__VA_ARGS__)
928 # else
929 # define WARN_CALL rb_funcall
930 # endif
931 # define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
932 # define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
933 # ifdef HAVE_VA_ARGS_MACRO
934 # define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
935 # else
936 # define WARNING_CALL rb_funcall
937 # endif
938 PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
939 # define compile_error ripper_compile_error
940 #else
941 # define WARN_S_L(s,l) s
942 # define WARN_S(s) s
943 # define WARN_I(i) i
944 # define WARN_ID(i) rb_id2name(i)
945 # define WARN_IVAL(i) NUM2INT(i)
946 # define PRIsWARN PRIsVALUE
947 # define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
948 # define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
949 # define WARN_CALL rb_compile_warn
950 # define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
951 # define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
952 # define WARNING_CALL rb_compile_warning
953 PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
954 # define compile_error parser_compile_error
955 #endif
956 
957 static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
958 static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
959 static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
960 static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
961 
962 #define WARN_EOL(tok) \
963  (looking_at_eol_p(p) ? \
964  (void)rb_warning0("`" tok "' at the end of line without an expression") : \
965  (void)0)
966 static int looking_at_eol_p(struct parser_params *p);
967 %}
968 
969 %expect 0
970 %define api.pure
971 %define parse.error verbose
972 %lex-param {struct parser_params *p}
973 %parse-param {struct parser_params *p}
974 %initial-action
975 {
976  RUBY_SET_YYLLOC_OF_NONE(@$);
977 };
978 
979 %union {
980  VALUE val;
981  NODE *node;
982  ID id;
983  int num;
984  st_table *tbl;
985  const struct vtable *vars;
986  struct rb_strterm_struct *strterm;
987 }
988 
989 %token <val>
990  keyword_class "`class'"
991  keyword_module "`module'"
992  keyword_def "`def'"
993  keyword_undef "`undef'"
994  keyword_begin "`begin'"
995  keyword_rescue "`rescue'"
996  keyword_ensure "`ensure'"
997  keyword_end "`end'"
998  keyword_if "`if'"
999  keyword_unless "`unless'"
1000  keyword_then "`then'"
1001  keyword_elsif "`elsif'"
1002  keyword_else "`else'"
1003  keyword_case "`case'"
1004  keyword_when "`when'"
1005  keyword_while "`while'"
1006  keyword_until "`until'"
1007  keyword_for "`for'"
1008  keyword_break "`break'"
1009  keyword_next "`next'"
1010  keyword_redo "`redo'"
1011  keyword_retry "`retry'"
1012  keyword_in "`in'"
1013  keyword_do "`do'"
1014  keyword_do_cond "`do' for condition"
1015  keyword_do_block "`do' for block"
1016  keyword_do_LAMBDA "`do' for lambda"
1017  keyword_return "`return'"
1018  keyword_yield "`yield'"
1019  keyword_super "`super'"
1020  keyword_self "`self'"
1021  keyword_nil "`nil'"
1022  keyword_true "`true'"
1023  keyword_false "`false'"
1024  keyword_and "`and'"
1025  keyword_or "`or'"
1026  keyword_not "`not'"
1027  modifier_if "`if' modifier"
1028  modifier_unless "`unless' modifier"
1029  modifier_while "`while' modifier"
1030  modifier_until "`until' modifier"
1031  modifier_rescue "`rescue' modifier"
1032  keyword_alias "`alias'"
1033  keyword_defined "`defined?'"
1034  keyword_BEGIN "`BEGIN'"
1035  keyword_END "`END'"
1036  keyword__LINE__ "`__LINE__'"
1037  keyword__FILE__ "`__FILE__'"
1038  keyword__ENCODING__ "`__ENCODING__'"
1039 
1040 %token <val> tIDENTIFIER "local variable or method"
1041 %token <val> tFID "method"
1042 %token <val> tGVAR "global variable"
1043 %token <val> tIVAR "instance variable"
1044 %token <val> tCONSTANT "constant"
1045 %token <val> tCVAR "class variable"
1046 %token <val> tLABEL
1047 %token <val> tINTEGER "integer literal"
1048 %token <val> tFLOAT "float literal"
1049 %token <val> tRATIONAL "rational literal"
1050 %token <val> tIMAGINARY "imaginary literal"
1051 %token <val> tCHAR "char literal"
1052 %token <val> tNTH_REF "numbered reference"
1053 %token <val> tBACK_REF "back reference"
1054 %token <val> tSTRING_CONTENT "literal content"
1055 %token <val> tREGEXP_END
1056 
1057 %type <val> singleton strings string string1 xstring regexp
1058 %type <val> string_contents xstring_contents regexp_contents string_content
1059 %type <val> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1060 %type <val> literal numeric simple_numeric ssym dsym symbol cpath
1061 %type <val> top_compstmt top_stmts top_stmt begin_block
1062 %type <val> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1063 %type <val> expr_value expr_value_do arg_value primary_value fcall rel_expr
1064 %type <val> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1065 %type <val> args call_args opt_call_args
1066 %type <val> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1067 %type <val> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1068 %type <val> command_rhs arg_rhs
1069 %type <val> command_asgn mrhs mrhs_arg superclass block_call block_command
1070 %type <val> f_block_optarg f_block_opt
1071 %type <val> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs f_rest_marg
1072 %type <val> assoc_list assocs assoc undef_list backref string_dvar for_var
1073 %type <val> block_param opt_block_param block_param_def f_opt
1074 %type <val> f_kwarg f_kw f_block_kwarg f_block_kw
1075 %type <val> bv_decls opt_bv_decl bvar
1076 %type <val> lambda f_larglist lambda_body brace_body do_body
1077 %type <val> brace_block cmd_brace_block do_block lhs none fitem
1078 %type <val> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1079 %type <val> p_case_body p_cases p_top_expr p_top_expr_body
1080 %type <val> p_expr p_as p_alt p_expr_basic
1081 %type <val> p_args p_args_head p_args_tail p_args_post p_arg
1082 %type <val> p_value p_primitive p_variable p_var_ref p_const
1083 %type <val> p_kwargs p_kwarg p_kw
1084 %type <val> keyword_variable user_variable sym operation operation2 operation3
1085 %type <val> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1086 %type <val> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1087 %type <val> p_kwrest p_kwnorest p_kw_label
1088 %type <val> f_no_kwarg args_forward
1089 %token END_OF_INPUT 0 "end-of-input"
1090 %token <val> '.'
1091 /* escaped chars, should be ignored otherwise */
1092 %token <val> '\\' "backslash"
1093 %token tSP "escaped space"
1094 %token <val> '\t' "escaped horizontal tab"
1095 %token <val> '\f' "escaped form feed"
1096 %token <val> '\r' "escaped carriage return"
1097 %token <val> '\13' "escaped vertical tab"
1098 %token tUPLUS 132 "unary+"
1099 %token tUMINUS 133 "unary-"
1100 %token tPOW 134 "**"
1101 %token tCMP 135 "<=>"
1102 %token tEQ 140 "=="
1103 %token tEQQ 141 "==="
1104 %token tNEQ 142 "!="
1105 %token tGEQ 139 ">="
1106 %token tLEQ 138 "<="
1107 %token tANDOP 148 "&&"
1108 %token tOROP 149 "||"
1109 %token tMATCH 143 "=~"
1110 %token tNMATCH 144 "!~"
1111 %token tDOT2 128 ".."
1112 %token tDOT3 129 "..."
1113 %token tBDOT2 130 "(.."
1114 %token tBDOT3 131 "(..."
1115 %token tAREF 145 "[]"
1116 %token tASET 146 "[]="
1117 %token tLSHFT 136 "<<"
1118 %token tRSHFT 137 ">>"
1119 %token <val> tANDDOT 150 "&."
1120 %token <val> tCOLON2 147 "::"
1121 %token tCOLON3 ":: at EXPR_BEG"
1122 %token <val> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1123 %token tASSOC "=>"
1124 %token tLPAREN "("
1125 %token tLPAREN_ARG "( arg"
1126 %token tRPAREN ")"
1127 %token tLBRACK "["
1128 %token tLBRACE "{"
1129 %token tLBRACE_ARG "{ arg"
1130 %token tSTAR "*"
1131 %token tDSTAR "**arg"
1132 %token tAMPER "&"
1133 %token tLAMBDA "->"
1134 %token tSYMBEG "symbol literal"
1135 %token tSTRING_BEG "string literal"
1136 %token tXSTRING_BEG "backtick literal"
1137 %token tREGEXP_BEG "regexp literal"
1138 %token tWORDS_BEG "word list"
1139 %token tQWORDS_BEG "verbatim word list"
1140 %token tSYMBOLS_BEG "symbol list"
1141 %token tQSYMBOLS_BEG "verbatim symbol list"
1142 %token tSTRING_END "terminator"
1143 %token tSTRING_DEND "'}'"
1144 %token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1145 
1146 /*
1147  * precedence table
1148  */
1149 
1150 %nonassoc tLOWEST
1151 %nonassoc tLBRACE_ARG
1152 
1153 %nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1154 %left keyword_or keyword_and
1155 %right keyword_not
1156 %nonassoc keyword_defined
1157 %right '=' tOP_ASGN
1158 %left modifier_rescue
1159 %right '?' ':'
1160 %nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1161 %left tOROP
1162 %left tANDOP
1163 %nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1164 %left '>' tGEQ '<' tLEQ
1165 %left '|' '^'
1166 %left '&'
1167 %left tLSHFT tRSHFT
1168 %left '+' '-'
1169 %left '*' '/' '%'
1170 %right tUMINUS_NUM tUMINUS
1171 %right tPOW
1172 %right '!' '~' tUPLUS
1173 
1174 %token tLAST_TOKEN
1175 
1176 %%
1177 program : {
1178  SET_LEX_STATE(EXPR_BEG);
1179  local_push(p, ifndef_ripper(1)+0);
1180  }
1181  top_compstmt
1182  {
1183 #if 0
1184  if ($2 && !compile_for_eval) {
1185  NODE *node = $2;
1186  /* last expression should not be void */
1187  if (nd_type(node) == NODE_BLOCK) {
1188  while (node->nd_next) {
1189  node = node->nd_next;
1190  }
1191  node = node->nd_head;
1192  }
1193  node = remove_begin(node);
1194  void_expr(p, node);
1195  }
1196  p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1197 #endif
1198  {VALUE v1,v2;v1=$2;v2=dispatch1(program,v1);p->result=v2;}
1199  local_pop(p);
1200  }
1201  ;
1202 
1203 top_compstmt : top_stmts opt_terms
1204  {
1205  $$ = void_stmts(p, $1);
1206  }
1207  ;
1208 
1209 top_stmts : none
1210  {
1211 #if 0
1212  $$ = NEW_BEGIN(0, &@$);
1213 #endif
1214  {VALUE v1,v2,v3,v4,v5;v1=dispatch0(stmts_new);v2=dispatch0(void_stmt);v3=v1;v4=v2;v5=dispatch2(stmts_add,v3,v4);$$=v5;}
1215  }
1216  | top_stmt
1217  {
1218 #if 0
1219  $$ = newline_node($1);
1220 #endif
1221  {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1222  }
1223  | top_stmts terms top_stmt
1224  {
1225 #if 0
1226  $$ = block_append(p, $1, newline_node($3));
1227 #endif
1228  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1229  }
1230  | error top_stmt
1231  {
1232  $$ = remove_begin($2);
1233  }
1234  ;
1235 
1236 top_stmt : stmt
1237  | keyword_BEGIN begin_block
1238  {
1239  $$ = $2;
1240  }
1241  ;
1242 
1243 begin_block : '{' top_compstmt '}'
1244  {
1245 #if 0
1246  p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1247  NEW_BEGIN($2, &@$));
1248  $$ = NEW_BEGIN(0, &@$);
1249 #endif
1250  {VALUE v1,v2;v1=$2;v2=dispatch1(BEGIN,v1);$$=v2;}
1251  }
1252  ;
1253 
1254 bodystmt : compstmt
1255  opt_rescue
1256  k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1257  compstmt
1258  opt_ensure
1259  {
1260 #if 0
1261  $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1262 #endif
1263  {VALUE v1,v2,v3,v4,v5;v1=escape_Qundef($1);v2=escape_Qundef($2);v3=escape_Qundef($5);v4=escape_Qundef($6);v5=dispatch4(bodystmt,v1,v2,v3,v4);$$=v5;}
1264  }
1265  | compstmt
1266  opt_rescue
1267  opt_ensure
1268  {
1269 #if 0
1270  $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1271 #endif
1272  {VALUE v1,v2,v3,v4,v5;v1=escape_Qundef($1);v2=escape_Qundef($2);v3=Qnil;v4=escape_Qundef($3);v5=dispatch4(bodystmt,v1,v2,v3,v4);$$=v5;}
1273  }
1274  ;
1275 
1276 compstmt : stmts opt_terms
1277  {
1278  $$ = void_stmts(p, $1);
1279  }
1280  ;
1281 
1282 stmts : none
1283  {
1284 #if 0
1285  $$ = NEW_BEGIN(0, &@$);
1286 #endif
1287  {VALUE v1,v2,v3,v4,v5;v1=dispatch0(stmts_new);v2=dispatch0(void_stmt);v3=v1;v4=v2;v5=dispatch2(stmts_add,v3,v4);$$=v5;}
1288  }
1289  | stmt_or_begin
1290  {
1291 #if 0
1292  $$ = newline_node($1);
1293 #endif
1294  {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1295  }
1296  | stmts terms stmt_or_begin
1297  {
1298 #if 0
1299  $$ = block_append(p, $1, newline_node($3));
1300 #endif
1301  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1302  }
1303  | error stmt
1304  {
1305  $$ = remove_begin($2);
1306  }
1307  ;
1308 
1309 stmt_or_begin : stmt
1310  {
1311  $$ = $1;
1312  }
1313  | keyword_BEGIN
1314  {
1315  yyerror1(&@1, "BEGIN is permitted only at toplevel");
1316  }
1317  begin_block
1318  {
1319  $$ = $3;
1320  }
1321  ;
1322 
1323 stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1324  {
1325 #if 0
1326  $$ = NEW_ALIAS($2, $4, &@$);
1327 #endif
1328  {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(alias,v1,v2);$$=v3;}
1329  }
1330  | keyword_alias tGVAR tGVAR
1331  {
1332 #if 0
1333  $$ = NEW_VALIAS($2, $3, &@$);
1334 #endif
1335  {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1336  }
1337  | keyword_alias tGVAR tBACK_REF
1338  {
1339 #if 0
1340  char buf[2];
1341  buf[0] = '$';
1342  buf[1] = (char)$3->nd_nth;
1343  $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1344 #endif
1345  {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1346  }
1347  | keyword_alias tGVAR tNTH_REF
1348  {
1349 #if 0
1350  yyerror1(&@3, "can't make alias for the number variables");
1351  $$ = NEW_BEGIN(0, &@$);
1352 #endif
1353  {VALUE v1,v2,v3,v4,v5;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);v4=v3;v5=dispatch1(alias_error,v4);$$=v5;}ripper_error(p);
1354  }
1355  | keyword_undef undef_list
1356  {
1357 #if 0
1358  $$ = $2;
1359 #endif
1360  {VALUE v1,v2;v1=$2;v2=dispatch1(undef,v1);$$=v2;}
1361  }
1362  | stmt modifier_if expr_value
1363  {
1364 #if 0
1365  $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1366  fixpos($$, $3);
1367 #endif
1368  {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
1369  }
1370  | stmt modifier_unless expr_value
1371  {
1372 #if 0
1373  $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1374  fixpos($$, $3);
1375 #endif
1376  {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
1377  }
1378  | stmt modifier_while expr_value
1379  {
1380 #if 0
1381  if ($1 && nd_type($1) == NODE_BEGIN) {
1382  $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1383  }
1384  else {
1385  $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1386  }
1387 #endif
1388  {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(while_mod,v1,v2);$$=v3;}
1389  }
1390  | stmt modifier_until expr_value
1391  {
1392 #if 0
1393  if ($1 && nd_type($1) == NODE_BEGIN) {
1394  $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1395  }
1396  else {
1397  $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1398  }
1399 #endif
1400  {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(until_mod,v1,v2);$$=v3;}
1401  }
1402  | stmt modifier_rescue stmt
1403  {
1404 #if 0
1405  NODE *resq;
1406  YYLTYPE loc = code_loc_gen(&@2, &@3);
1407  resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1408  $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1409 #endif
1410  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1411  }
1412  | keyword_END '{' compstmt '}'
1413  {
1414  if (p->in_def) {
1415  rb_warn0("END in method; use at_exit");
1416  }
1417 #if 0
1418  {
1419  NODE *scope = NEW_NODE(
1420  NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1421  $$ = NEW_POSTEXE(scope, &@$);
1422  }
1423 #endif
1424  {VALUE v1,v2;v1=$3;v2=dispatch1(END,v1);$$=v2;}
1425  }
1426  | command_asgn
1427  | mlhs '=' command_call
1428  {
1429 #if 0
1430  value_expr($3);
1431  $$ = node_assign(p, $1, $3, &@$);
1432 #endif
1433  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(massign,v1,v2);$$=v3;}
1434  }
1435  | lhs '=' mrhs
1436  {
1437 #if 0
1438  value_expr($3);
1439  $$ = node_assign(p, $1, $3, &@$);
1440 #endif
1441  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
1442  }
1443  | mlhs '=' mrhs_arg modifier_rescue stmt
1444  {
1445 #if 0
1446  YYLTYPE loc = code_loc_gen(&@4, &@5);
1447  value_expr($3);
1448  $$ = node_assign(p, $1, NEW_RESCUE($3, NEW_RESBODY(0, remove_begin($5), 0, &loc), 0, &@$), &@$);
1449 #endif
1450  {VALUE v1,v2,v3,v4,v5,v6;v1=$3;v2=$5;v3=dispatch2(rescue_mod,v1,v2);v4=$1;v5=v3;v6=dispatch2(massign,v4,v5);$$=v6;}
1451  }
1452  | mlhs '=' mrhs_arg
1453  {
1454 #if 0
1455  $$ = node_assign(p, $1, $3, &@$);
1456 #endif
1457  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(massign,v1,v2);$$=v3;}
1458  }
1459  | expr
1460  ;
1461 
1462 command_asgn : lhs '=' command_rhs
1463  {
1464 #if 0
1465  $$ = node_assign(p, $1, $3, &@$);
1466 #endif
1467  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
1468  }
1469  | var_lhs tOP_ASGN command_rhs
1470  {
1471 #if 0
1472  $$ = new_op_assign(p, $1, $2, $3, &@$);
1473 #endif
1474  {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
1475  }
1476  | primary_value '[' opt_call_args rbracket tOP_ASGN command_rhs
1477  {
1478 #if 0
1479  $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
1480 #endif
1481  {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);v4=v3;v5=$5;v6=$6;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
1482 
1483  }
1484  | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
1485  {
1486 #if 0
1487  $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1488 #endif
1489  {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1490  }
1491  | primary_value call_op tCONSTANT tOP_ASGN command_rhs
1492  {
1493 #if 0
1494  $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1495 #endif
1496  {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1497  }
1498  | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
1499  {
1500 #if 0
1501  YYLTYPE loc = code_loc_gen(&@1, &@3);
1502  $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
1503 #endif
1504  {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);v4=v3;v5=$4;v6=$5;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
1505  }
1506  | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
1507  {
1508 #if 0
1509  $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
1510 #endif
1511  {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1512  }
1513  | backref tOP_ASGN command_rhs
1514  {
1515 #if 0
1516  rb_backref_error(p, $1);
1517  $$ = NEW_BEGIN(0, &@$);
1518 #endif
1519  {VALUE v1,v2,v3,v4,v5;v1=var_field(p, $1);v2=$3;v3=dispatch2(assign,v1,v2);v4=v3;v5=dispatch1(assign_error,v4);$$=v5;}ripper_error(p);
1520  }
1521  ;
1522 
1523 command_rhs : command_call %prec tOP_ASGN
1524  {
1525  value_expr($1);
1526  $$ = $1;
1527  }
1528  | command_call modifier_rescue stmt
1529  {
1530 #if 0
1531  YYLTYPE loc = code_loc_gen(&@2, &@3);
1532  value_expr($1);
1533  $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1534 #endif
1535  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1536  }
1537  | command_asgn
1538  ;
1539 
1540 expr : command_call
1541  | expr keyword_and expr
1542  {
1543  $$ = logop(p, idAND, $1, $3, &@2, &@$);
1544  }
1545  | expr keyword_or expr
1546  {
1547  $$ = logop(p, idOR, $1, $3, &@2, &@$);
1548  }
1549  | keyword_not opt_nl expr
1550  {
1551  $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
1552  }
1553  | '!' command_call
1554  {
1555  $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
1556  }
1557  | arg keyword_in
1558  {
1559  value_expr($1);
1560  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1561  p->command_start = FALSE;
1562  $<num>$ = p->in_kwarg;
1563  p->in_kwarg = 1;
1564  }
1565  {$<tbl>$ = push_pvtbl(p);}
1566  p_expr
1567  {pop_pvtbl(p, $<tbl>4);}
1568  {
1569  p->in_kwarg = !!$<num>3;
1570 #if 0
1571  $$ = new_case3(p, $1, NEW_IN($5, 0, 0, &@5), &@$);
1572 #endif
1573  {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$5;v2=Qnil;v3=Qnil;v4=dispatch3(in,v1,v2,v3);v5=$1;v6=v4;v7=dispatch2(case,v5,v6);$$=v7;}
1574  }
1575  | arg %prec tLBRACE_ARG
1576  ;
1577 
1578 expr_value : expr
1579  {
1580  value_expr($1);
1581  $$ = $1;
1582  }
1583  ;
1584 
1585 expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1586  {
1587  $$ = $2;
1588  }
1589 
1590 
1591 command_call : command
1592  | block_command
1593  ;
1594 
1595 block_command : block_call
1596  | block_call call_op2 operation2 command_args
1597  {
1598 #if 0
1599  $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
1600 #endif
1601  {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
1602  }
1603  ;
1604 
1605 cmd_brace_block : tLBRACE_ARG brace_body '}'
1606  {
1607  $$ = $2;
1608 #if 0
1609  $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
1610  nd_set_line($$, @1.end_pos.lineno);
1611 #endif
1612  }
1613  ;
1614 
1615 fcall : operation
1616  {
1617 #if 0
1618  $$ = NEW_FCALL($1, 0, &@$);
1619  nd_set_line($$, p->tokline);
1620 #endif
1621  $$=$1;
1622  }
1623  ;
1624 
1625 command : fcall command_args %prec tLOWEST
1626  {
1627 #if 0
1628  $1->nd_args = $2;
1629  nd_set_last_loc($1, @2.end_pos);
1630  $$ = $1;
1631 #endif
1632  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(command,v1,v2);$$=v3;}
1633  }
1634  | fcall command_args cmd_brace_block
1635  {
1636 #if 0
1637  block_dup_check(p, $2, $3);
1638  $1->nd_args = $2;
1639  $$ = method_add_block(p, $1, $3, &@$);
1640  fixpos($$, $1);
1641  nd_set_last_loc($1, @2.end_pos);
1642 #endif
1643  {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=$2;v3=dispatch2(command,v1,v2);v4=v3;v5=$3;v6=dispatch2(method_add_block,v4,v5);$$=v6;}
1644  }
1645  | primary_value call_op operation2 command_args %prec tLOWEST
1646  {
1647 #if 0
1648  $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
1649 #endif
1650  {VALUE v1,v2,v3,v4,v5;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1651  }
1652  | primary_value call_op operation2 command_args cmd_brace_block
1653  {
1654 #if 0
1655  $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
1656 #endif
1657  {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=dispatch2(method_add_block,v6,v7);$$=v8;}
1658  }
1659  | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1660  {
1661 #if 0
1662  $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
1663 #endif
1664  {VALUE v1,v2,v3,v4,v5;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1665  }
1666  | primary_value tCOLON2 operation2 command_args cmd_brace_block
1667  {
1668 #if 0
1669  $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
1670 #endif
1671  {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=dispatch2(method_add_block,v6,v7);$$=v8;}
1672  }
1673  | keyword_super command_args
1674  {
1675 #if 0
1676  $$ = NEW_SUPER($2, &@$);
1677  fixpos($$, $2);
1678 #endif
1679  {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
1680  }
1681  | keyword_yield command_args
1682  {
1683 #if 0
1684  $$ = new_yield(p, $2, &@$);
1685  fixpos($$, $2);
1686 #endif
1687  {VALUE v1,v2;v1=$2;v2=dispatch1(yield,v1);$$=v2;}
1688  }
1689  | k_return call_args
1690  {
1691 #if 0
1692  $$ = NEW_RETURN(ret_args(p, $2), &@$);
1693 #endif
1694  {VALUE v1,v2;v1=$2;v2=dispatch1(return,v1);$$=v2;}
1695  }
1696  | keyword_break call_args
1697  {
1698 #if 0
1699  $$ = NEW_BREAK(ret_args(p, $2), &@$);
1700 #endif
1701  {VALUE v1,v2;v1=$2;v2=dispatch1(break,v1);$$=v2;}
1702  }
1703  | keyword_next call_args
1704  {
1705 #if 0
1706  $$ = NEW_NEXT(ret_args(p, $2), &@$);
1707 #endif
1708  {VALUE v1,v2;v1=$2;v2=dispatch1(next,v1);$$=v2;}
1709  }
1710  ;
1711 
1712 mlhs : mlhs_basic
1713  | tLPAREN mlhs_inner rparen
1714  {
1715 #if 0
1716  $$ = $2;
1717 #endif
1718  {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1719  }
1720  ;
1721 
1722 mlhs_inner : mlhs_basic
1723  | tLPAREN mlhs_inner rparen
1724  {
1725 #if 0
1726  $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1727 #endif
1728  {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1729  }
1730  ;
1731 
1732 mlhs_basic : mlhs_head
1733  {
1734 #if 0
1735  $$ = NEW_MASGN($1, 0, &@$);
1736 #endif
1737  $$=$1;
1738  }
1739  | mlhs_head mlhs_item
1740  {
1741 #if 0
1742  $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
1743 #endif
1744  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1745  }
1746  | mlhs_head tSTAR mlhs_node
1747  {
1748 #if 0
1749  $$ = NEW_MASGN($1, $3, &@$);
1750 #endif
1751  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1752  }
1753  | mlhs_head tSTAR mlhs_node ',' mlhs_post
1754  {
1755 #if 0
1756  $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
1757 #endif
1758  {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);v4=v3;v5=$5;v6=dispatch2(mlhs_add_post,v4,v5);$$=v6;}
1759  }
1760  | mlhs_head tSTAR
1761  {
1762 #if 0
1763  $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
1764 #endif
1765  {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1766  }
1767  | mlhs_head tSTAR ',' mlhs_post
1768  {
1769 #if 0
1770  $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
1771 #endif
1772  {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=Qnil;v3=dispatch2(mlhs_add_star,v1,v2);v4=v3;v5=$4;v6=dispatch2(mlhs_add_post,v4,v5);$$=v6;}
1773  }
1774  | tSTAR mlhs_node
1775  {
1776 #if 0
1777  $$ = NEW_MASGN(0, $2, &@$);
1778 #endif
1779  {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$2;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1780  }
1781  | tSTAR mlhs_node ',' mlhs_post
1782  {
1783 #if 0
1784  $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
1785 #endif
1786  {VALUE v1,v2,v3,v4,v5,v6,v7;v1=dispatch0(mlhs_new);v2=v1;v3=$2;v4=dispatch2(mlhs_add_star,v2,v3);v5=v4;v6=$4;v7=dispatch2(mlhs_add_post,v5,v6);$$=v7;}
1787  }
1788  | tSTAR
1789  {
1790 #if 0
1791  $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
1792 #endif
1793  {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=Qnil;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1794  }
1795  | tSTAR ',' mlhs_post
1796  {
1797 #if 0
1798  $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
1799 #endif
1800  {VALUE v1,v2,v3,v4,v5,v6,v7;v1=dispatch0(mlhs_new);v2=v1;v3=Qnil;v4=dispatch2(mlhs_add_star,v2,v3);v5=v4;v6=$3;v7=dispatch2(mlhs_add_post,v5,v6);$$=v7;}
1801  }
1802  ;
1803 
1804 mlhs_item : mlhs_node
1805  | tLPAREN mlhs_inner rparen
1806  {
1807 #if 0
1808  $$ = $2;
1809 #endif
1810  {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1811  }
1812  ;
1813 
1814 mlhs_head : mlhs_item ','
1815  {
1816 #if 0
1817  $$ = NEW_LIST($1, &@1);
1818 #endif
1819  {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
1820  }
1821  | mlhs_head mlhs_item ','
1822  {
1823 #if 0
1824  $$ = list_append(p, $1, $2);
1825 #endif
1826  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1827  }
1828  ;
1829 
1830 mlhs_post : mlhs_item
1831  {
1832 #if 0
1833  $$ = NEW_LIST($1, &@$);
1834 #endif
1835  {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
1836  }
1837  | mlhs_post ',' mlhs_item
1838  {
1839 #if 0
1840  $$ = list_append(p, $1, $3);
1841 #endif
1842  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1843  }
1844  ;
1845 
1846 mlhs_node : user_variable
1847  {
1848 #if 0
1849  $$ = assignable(p, $1, 0, &@$);
1850 #endif
1851  $$=assignable(p, var_field(p, $1));
1852  }
1853  | keyword_variable
1854  {
1855 #if 0
1856  $$ = assignable(p, $1, 0, &@$);
1857 #endif
1858  $$=assignable(p, var_field(p, $1));
1859  }
1860  | primary_value '[' opt_call_args rbracket
1861  {
1862 #if 0
1863  $$ = aryset(p, $1, $3, &@$);
1864 #endif
1865  {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
1866  }
1867  | primary_value call_op tIDENTIFIER
1868  {
1869  if ($2 == tANDDOT) {
1870  yyerror1(&@2, "&. inside multiple assignment destination");
1871  }
1872 #if 0
1873  $$ = attrset(p, $1, $2, $3, &@$);
1874 #endif
1875  {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1876  }
1877  | primary_value tCOLON2 tIDENTIFIER
1878  {
1879 #if 0
1880  $$ = attrset(p, $1, idCOLON2, $3, &@$);
1881 #endif
1882  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=v3;}
1883  }
1884  | primary_value call_op tCONSTANT
1885  {
1886  if ($2 == tANDDOT) {
1887  yyerror1(&@2, "&. inside multiple assignment destination");
1888  }
1889 #if 0
1890  $$ = attrset(p, $1, $2, $3, &@$);
1891 #endif
1892  {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1893  }
1894  | primary_value tCOLON2 tCONSTANT
1895  {
1896 #if 0
1897  $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1898 #endif
1899  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
1900  }
1901  | tCOLON3 tCONSTANT
1902  {
1903 #if 0
1904  $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1905 #endif
1906  {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
1907  }
1908  | backref
1909  {
1910 #if 0
1911  rb_backref_error(p, $1);
1912  $$ = NEW_BEGIN(0, &@$);
1913 #endif
1914  {VALUE v1,v2;v1=var_field(p, $1);v2=dispatch1(assign_error,v1);$$=v2;}ripper_error(p);
1915  }
1916  ;
1917 
1918 lhs : user_variable
1919  {
1920 #if 0
1921  $$ = assignable(p, $1, 0, &@$);
1922 #endif
1923  $$=assignable(p, var_field(p, $1));
1924  }
1925  | keyword_variable
1926  {
1927 #if 0
1928  $$ = assignable(p, $1, 0, &@$);
1929 #endif
1930  $$=assignable(p, var_field(p, $1));
1931  }
1932  | primary_value '[' opt_call_args rbracket
1933  {
1934 #if 0
1935  $$ = aryset(p, $1, $3, &@$);
1936 #endif
1937  {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
1938  }
1939  | primary_value call_op tIDENTIFIER
1940  {
1941 #if 0
1942  $$ = attrset(p, $1, $2, $3, &@$);
1943 #endif
1944  {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1945  }
1946  | primary_value tCOLON2 tIDENTIFIER
1947  {
1948 #if 0
1949  $$ = attrset(p, $1, idCOLON2, $3, &@$);
1950 #endif
1951  {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1952  }
1953  | primary_value call_op tCONSTANT
1954  {
1955 #if 0
1956  $$ = attrset(p, $1, $2, $3, &@$);
1957 #endif
1958  {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1959  }
1960  | primary_value tCOLON2 tCONSTANT
1961  {
1962 #if 0
1963  $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1964 #endif
1965  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
1966  }
1967  | tCOLON3 tCONSTANT
1968  {
1969 #if 0
1970  $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1971 #endif
1972  {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
1973  }
1974  | backref
1975  {
1976 #if 0
1977  rb_backref_error(p, $1);
1978  $$ = NEW_BEGIN(0, &@$);
1979 #endif
1980  {VALUE v1,v2;v1=var_field(p, $1);v2=dispatch1(assign_error,v1);$$=v2;}ripper_error(p);
1981  }
1982  ;
1983 
1984 cname : tIDENTIFIER
1985  {
1986 #if 0
1987  yyerror1(&@1, "class/module name must be CONSTANT");
1988 #endif
1989  {VALUE v1,v2;v1=$1;v2=dispatch1(class_name_error,v1);$$=v2;}ripper_error(p);
1990  }
1991  | tCONSTANT
1992  ;
1993 
1994 cpath : tCOLON3 cname
1995  {
1996 #if 0
1997  $$ = NEW_COLON3($2, &@$);
1998 #endif
1999  {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2000  }
2001  | cname
2002  {
2003 #if 0
2004  $$ = NEW_COLON2(0, $$, &@$);
2005 #endif
2006  {VALUE v1,v2;v1=$1;v2=dispatch1(const_ref,v1);$$=v2;}
2007  }
2008  | primary_value tCOLON2 cname
2009  {
2010 #if 0
2011  $$ = NEW_COLON2($1, $3, &@$);
2012 #endif
2013  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2014  }
2015  ;
2016 
2017 fname : tIDENTIFIER
2018  | tCONSTANT
2019  | tFID
2020  | op
2021  {
2022  SET_LEX_STATE(EXPR_ENDFN);
2023  $$ = $1;
2024  }
2025  | reswords
2026  ;
2027 
2028 fitem : fname
2029  {
2030 #if 0
2031  $$ = NEW_LIT(ID2SYM($1), &@$);
2032 #endif
2033  {VALUE v1,v2;v1=$1;v2=dispatch1(symbol_literal,v1);$$=v2;}
2034  }
2035  | symbol
2036  ;
2037 
2038 undef_list : fitem
2039  {
2040 #if 0
2041  $$ = NEW_UNDEF($1, &@$);
2042 #endif
2043  $$=rb_ary_new3(1, get_value($1));
2044  }
2045  | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2046  {
2047 #if 0
2048  NODE *undef = NEW_UNDEF($4, &@4);
2049  $$ = block_append(p, $1, undef);
2050 #endif
2051  $$=rb_ary_push($1, get_value($4));
2052  }
2053  ;
2054 
2055 op : '|' { ifndef_ripper($$ = '|'); }
2056  | '^' { ifndef_ripper($$ = '^'); }
2057  | '&' { ifndef_ripper($$ = '&'); }
2058  | tCMP { ifndef_ripper($$ = tCMP); }
2059  | tEQ { ifndef_ripper($$ = tEQ); }
2060  | tEQQ { ifndef_ripper($$ = tEQQ); }
2061  | tMATCH { ifndef_ripper($$ = tMATCH); }
2062  | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2063  | '>' { ifndef_ripper($$ = '>'); }
2064  | tGEQ { ifndef_ripper($$ = tGEQ); }
2065  | '<' { ifndef_ripper($$ = '<'); }
2066  | tLEQ { ifndef_ripper($$ = tLEQ); }
2067  | tNEQ { ifndef_ripper($$ = tNEQ); }
2068  | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2069  | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2070  | '+' { ifndef_ripper($$ = '+'); }
2071  | '-' { ifndef_ripper($$ = '-'); }
2072  | '*' { ifndef_ripper($$ = '*'); }
2073  | tSTAR { ifndef_ripper($$ = '*'); }
2074  | '/' { ifndef_ripper($$ = '/'); }
2075  | '%' { ifndef_ripper($$ = '%'); }
2076  | tPOW { ifndef_ripper($$ = tPOW); }
2077  | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2078  | '!' { ifndef_ripper($$ = '!'); }
2079  | '~' { ifndef_ripper($$ = '~'); }
2080  | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2081  | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2082  | tAREF { ifndef_ripper($$ = tAREF); }
2083  | tASET { ifndef_ripper($$ = tASET); }
2084  | '`' { ifndef_ripper($$ = '`'); }
2085  ;
2086 
2087 reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2088  | keyword_BEGIN | keyword_END
2089  | keyword_alias | keyword_and | keyword_begin
2090  | keyword_break | keyword_case | keyword_class | keyword_def
2091  | keyword_defined | keyword_do | keyword_else | keyword_elsif
2092  | keyword_end | keyword_ensure | keyword_false
2093  | keyword_for | keyword_in | keyword_module | keyword_next
2094  | keyword_nil | keyword_not | keyword_or | keyword_redo
2095  | keyword_rescue | keyword_retry | keyword_return | keyword_self
2096  | keyword_super | keyword_then | keyword_true | keyword_undef
2097  | keyword_when | keyword_yield | keyword_if | keyword_unless
2098  | keyword_while | keyword_until
2099  ;
2100 
2101 arg : lhs '=' arg_rhs
2102  {
2103 #if 0
2104  $$ = node_assign(p, $1, $3, &@$);
2105 #endif
2106  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
2107  }
2108  | var_lhs tOP_ASGN arg_rhs
2109  {
2110 #if 0
2111  $$ = new_op_assign(p, $1, $2, $3, &@$);
2112 #endif
2113  {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
2114  }
2115  | primary_value '[' opt_call_args rbracket tOP_ASGN arg_rhs
2116  {
2117 #if 0
2118  value_expr($6);
2119  $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
2120 #endif
2121  {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);v4=v3;v5=$5;v6=$6;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
2122  }
2123  | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
2124  {
2125 #if 0
2126  value_expr($5);
2127  $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2128 #endif
2129  {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2130  }
2131  | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
2132  {
2133 #if 0
2134  value_expr($5);
2135  $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2136 #endif
2137  {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2138  }
2139  | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
2140  {
2141 #if 0
2142  value_expr($5);
2143  $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
2144 #endif
2145  {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2146  }
2147  | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
2148  {
2149 #if 0
2150  YYLTYPE loc = code_loc_gen(&@1, &@3);
2151  $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
2152 #endif
2153  {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);v4=v3;v5=$4;v6=$5;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
2154  }
2155  | tCOLON3 tCONSTANT tOP_ASGN arg_rhs
2156  {
2157 #if 0
2158  $$ = new_const_op_assign(p, NEW_COLON3($2, &@$), $3, $4, &@$);
2159 #endif
2160  {VALUE v1,v2,v3,v4,v5,v6;v1=$2;v2=dispatch1(top_const_field,v1);v3=v2;v4=$3;v5=$4;v6=dispatch3(opassign,v3,v4,v5);$$=v6;}
2161  }
2162  | backref tOP_ASGN arg_rhs
2163  {
2164 #if 0
2165  rb_backref_error(p, $1);
2166  $$ = NEW_BEGIN(0, &@$);
2167 #endif
2168  {VALUE v1,v2,v3,v4,v5,v6;v1=var_field(p, $1);v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);v5=v4;v6=dispatch1(assign_error,v5);$$=v6;}ripper_error(p);
2169  }
2170  | arg tDOT2 arg
2171  {
2172 #if 0
2173  value_expr($1);
2174  value_expr($3);
2175  $$ = NEW_DOT2($1, $3, &@$);
2176 #endif
2177  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
2178  }
2179  | arg tDOT3 arg
2180  {
2181 #if 0
2182  value_expr($1);
2183  value_expr($3);
2184  $$ = NEW_DOT3($1, $3, &@$);
2185 #endif
2186  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
2187  }
2188  | arg tDOT2
2189  {
2190 #if 0
2191  YYLTYPE loc;
2192  loc.beg_pos = @2.end_pos;
2193  loc.end_pos = @2.end_pos;
2194 
2195  value_expr($1);
2196  $$ = NEW_DOT2($1, new_nil(&loc), &@$);
2197 #endif
2198  {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
2199  }
2200  | arg tDOT3
2201  {
2202 #if 0
2203  YYLTYPE loc;
2204  loc.beg_pos = @2.end_pos;
2205  loc.end_pos = @2.end_pos;
2206 
2207  value_expr($1);
2208  $$ = NEW_DOT3($1, new_nil(&loc), &@$);
2209 #endif
2210  {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
2211  }
2212  | tBDOT2 arg
2213  {
2214 #if 0
2215  YYLTYPE loc;
2216  loc.beg_pos = @1.beg_pos;
2217  loc.end_pos = @1.beg_pos;
2218 
2219  value_expr($2);
2220  $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
2221 #endif
2222  {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
2223  }
2224  | tBDOT3 arg
2225  {
2226 #if 0
2227  YYLTYPE loc;
2228  loc.beg_pos = @1.beg_pos;
2229  loc.end_pos = @1.beg_pos;
2230 
2231  value_expr($2);
2232  $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
2233 #endif
2234  {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
2235  }
2236  | arg '+' arg
2237  {
2238  $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2239  }
2240  | arg '-' arg
2241  {
2242  $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2243  }
2244  | arg '*' arg
2245  {
2246  $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2247  }
2248  | arg '/' arg
2249  {
2250  $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2251  }
2252  | arg '%' arg
2253  {
2254  $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2255  }
2256  | arg tPOW arg
2257  {
2258  $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2259  }
2260  | tUMINUS_NUM simple_numeric tPOW arg
2261  {
2262  $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2263  }
2264  | tUPLUS arg
2265  {
2266  $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2267  }
2268  | tUMINUS arg
2269  {
2270  $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2271  }
2272  | arg '|' arg
2273  {
2274  $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2275  }
2276  | arg '^' arg
2277  {
2278  $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2279  }
2280  | arg '&' arg
2281  {
2282  $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2283  }
2284  | arg tCMP arg
2285  {
2286  $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2287  }
2288  | rel_expr %prec tCMP
2289  | arg tEQ arg
2290  {
2291  $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2292  }
2293  | arg tEQQ arg
2294  {
2295  $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2296  }
2297  | arg tNEQ arg
2298  {
2299  $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2300  }
2301  | arg tMATCH arg
2302  {
2303  $$ = match_op(p, $1, $3, &@2, &@$);
2304  }
2305  | arg tNMATCH arg
2306  {
2307  $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2308  }
2309  | '!' arg
2310  {
2311  $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2312  }
2313  | '~' arg
2314  {
2315  $$ = call_uni_op(p, $2, '~', &@1, &@$);
2316  }
2317  | arg tLSHFT arg
2318  {
2319  $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2320  }
2321  | arg tRSHFT arg
2322  {
2323  $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2324  }
2325  | arg tANDOP arg
2326  {
2327  $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2328  }
2329  | arg tOROP arg
2330  {
2331  $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2332  }
2333  | keyword_defined opt_nl {p->in_defined = 1;} arg
2334  {
2335  p->in_defined = 0;
2336  $$ = new_defined(p, $4, &@$);
2337  }
2338  | arg '?' arg opt_nl ':' arg
2339  {
2340 #if 0
2341  value_expr($1);
2342  $$ = new_if(p, $1, $3, $6, &@$);
2343  fixpos($$, $1);
2344 #endif
2345  {VALUE v1,v2,v3,v4;v1=$1;v2=$3;v3=$6;v4=dispatch3(ifop,v1,v2,v3);$$=v4;}
2346  }
2347  | primary
2348  {
2349  $$ = $1;
2350  }
2351  ;
2352 
2353 relop : '>' {$$ = '>';}
2354  | '<' {$$ = '<';}
2355  | tGEQ {$$ = idGE;}
2356  | tLEQ {$$ = idLE;}
2357  ;
2358 
2359 rel_expr : arg relop arg %prec '>'
2360  {
2361  $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2362  }
2363  | rel_expr relop arg %prec '>'
2364  {
2365  rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2366  $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2367  }
2368  ;
2369 
2370 arg_value : arg
2371  {
2372  value_expr($1);
2373  $$ = $1;
2374  }
2375  ;
2376 
2377 aref_args : none
2378  | args trailer
2379  {
2380  $$ = $1;
2381  }
2382  | args ',' assocs trailer
2383  {
2384 #if 0
2385  $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2386 #endif
2387  {VALUE v1,v2,v3,v4,v5;v1=$3;v2=dispatch1(bare_assoc_hash,v1);v3=$1;v4=v2;v5=dispatch2(args_add,v3,v4);$$=v5;}
2388  }
2389  | assocs trailer
2390  {
2391 #if 0
2392  $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2393 #endif
2394  {VALUE v1,v2,v3,v4,v5,v6;v1=dispatch0(args_new);v2=$1;v3=dispatch1(bare_assoc_hash,v2);v4=v1;v5=v3;v6=dispatch2(args_add,v4,v5);$$=v6;}
2395  }
2396  ;
2397 
2398 arg_rhs : arg %prec tOP_ASGN
2399  {
2400  value_expr($1);
2401  $$ = $1;
2402  }
2403  | arg modifier_rescue arg
2404  {
2405 #if 0
2406  YYLTYPE loc = code_loc_gen(&@2, &@3);
2407  value_expr($1);
2408  $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
2409 #endif
2410  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
2411  }
2412  ;
2413 
2414 paren_args : '(' opt_call_args rparen
2415  {
2416 #if 0
2417  $$ = $2;
2418 #endif
2419  {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(arg_paren,v1);$$=v2;}
2420  }
2421  | '(' args_forward rparen
2422  {
2423  if (!local_id(p, idFWD_REST) ||
2424 #if idFWD_KWREST
2425  !local_id(p, idFWD_KWREST) ||
2426 #endif
2427  !local_id(p, idFWD_BLOCK)) {
2428  compile_error(p, "unexpected ...");
2429  $$ = Qnone;
2430  }
2431  else {
2432 #if 0
2433  NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@2), &@2);
2434 #if idFWD_KWREST
2435  NODE *kwrest = list_append(p, NEW_LIST(0, &@2), NEW_LVAR(idFWD_KWREST, &@2));
2436 #endif
2437  NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@2), &@2);
2438 #if idFWD_KWREST
2439  $$ = arg_append(p, splat, new_hash(p, kwrest, &@2), &@2);
2440 #else
2441  $$ = splat;
2442 #endif
2443  $$ = arg_blk_pass($$, block);
2444 #endif
2445  {VALUE v1,v2;v1=$2;v2=dispatch1(arg_paren,v1);$$=v2;}
2446  }
2447  }
2448  ;
2449 
2450 opt_paren_args : none
2451  | paren_args
2452  ;
2453 
2454 opt_call_args : none
2455  | call_args
2456  | args ','
2457  {
2458  $$ = $1;
2459  }
2460  | args ',' assocs ','
2461  {
2462 #if 0
2463  $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2464 #endif
2465  {VALUE v1,v2,v3,v4,v5;v1=$3;v2=dispatch1(bare_assoc_hash,v1);v3=$1;v4=v2;v5=dispatch2(args_add,v3,v4);$$=v5;}
2466  }
2467  | assocs ','
2468  {
2469 #if 0
2470  $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2471 #endif
2472  {VALUE v1,v2,v3,v4,v5,v6;v1=dispatch0(args_new);v2=$1;v3=dispatch1(bare_assoc_hash,v2);v4=v1;v5=v3;v6=dispatch2(args_add,v4,v5);$$=v6;}
2473  }
2474  ;
2475 
2476 call_args : command
2477  {
2478 #if 0
2479  value_expr($1);
2480  $$ = NEW_LIST($1, &@$);
2481 #endif
2482  {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2483  }
2484  | args opt_block_arg
2485  {
2486 #if 0
2487  $$ = arg_blk_pass($1, $2);
2488 #endif
2489  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(args_add_block,v1,v2);$$=v3;}
2490  }
2491  | assocs opt_block_arg
2492  {
2493 #if 0
2494  $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2495  $$ = arg_blk_pass($$, $2);
2496 #endif
2497  {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9;v1=dispatch0(args_new);v2=$1;v3=dispatch1(bare_assoc_hash,v2);v4=v1;v5=v3;v6=dispatch2(args_add,v4,v5);v7=v6;v8=$2;v9=dispatch2(args_add_block,v7,v8);$$=v9;}
2498  }
2499  | args ',' assocs opt_block_arg
2500  {
2501 #if 0
2502  $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2503  $$ = arg_blk_pass($$, $4);
2504 #endif
2505  {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$3;v2=dispatch1(bare_assoc_hash,v1);v3=$1;v4=v2;v5=dispatch2(args_add,v3,v4);v6=v5;v7=$4;v8=dispatch2(args_add_block,v6,v7);$$=v8;}
2506  }
2507  | block_arg
2508  {{VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add_block,v2,v3);$$=v4;}}
2509  ;
2510 
2511 command_args : {
2512  /* If call_args starts with a open paren '(' or '[',
2513  * look-ahead reading of the letters calls CMDARG_PUSH(0),
2514  * but the push must be done after CMDARG_PUSH(1).
2515  * So this code makes them consistent by first cancelling
2516  * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
2517  * and finally redoing CMDARG_PUSH(0).
2518  */
2519  int lookahead = 0;
2520  switch (yychar) {
2521  case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
2522  lookahead = 1;
2523  }
2524  if (lookahead) CMDARG_POP();
2525  CMDARG_PUSH(1);
2526  if (lookahead) CMDARG_PUSH(0);
2527  }
2528  call_args
2529  {
2530  /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
2531  * but the push must be done after CMDARG_POP() in the parser.
2532  * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
2533  * CMDARG_POP() to pop 1 pushed by command_args,
2534  * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
2535  */
2536  int lookahead = 0;
2537  switch (yychar) {
2538  case tLBRACE_ARG:
2539  lookahead = 1;
2540  }
2541  if (lookahead) CMDARG_POP();
2542  CMDARG_POP();
2543  if (lookahead) CMDARG_PUSH(0);
2544  $$ = $2;
2545  }
2546  ;
2547 
2548 block_arg : tAMPER arg_value
2549  {
2550 #if 0
2551  $$ = NEW_BLOCK_PASS($2, &@$);
2552 #endif
2553  $$=$2;
2554  }
2555  ;
2556 
2557 opt_block_arg : ',' block_arg
2558  {
2559  $$ = $2;
2560  }
2561  | none
2562  {
2563  $$ = 0;
2564  }
2565  ;
2566 
2567 args : arg_value
2568  {
2569 #if 0
2570  $$ = NEW_LIST($1, &@$);
2571 #endif
2572  {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2573  }
2574  | tSTAR arg_value
2575  {
2576 #if 0
2577  $$ = NEW_SPLAT($2, &@$);
2578 #endif
2579  {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
2580  }
2581  | args ',' arg_value
2582  {
2583 #if 0
2584  $$ = last_arg_append(p, $1, $3, &@$);
2585 #endif
2586  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
2587  }
2588  | args ',' tSTAR arg_value
2589  {
2590 #if 0
2591  $$ = rest_arg_append(p, $1, $4, &@$);
2592 #endif
2593  {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
2594  }
2595  ;
2596 
2597 mrhs_arg : mrhs
2598  | arg_value
2599  ;
2600 
2601 mrhs : args ',' arg_value
2602  {
2603 #if 0
2604  $$ = last_arg_append(p, $1, $3, &@$);
2605 #endif
2606  {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(mrhs_new_from_args,v1);v3=v2;v4=$3;v5=dispatch2(mrhs_add,v3,v4);$$=v5;}
2607  }
2608  | args ',' tSTAR arg_value
2609  {
2610 #if 0
2611  $$ = rest_arg_append(p, $1, $4, &@$);
2612 #endif
2613  {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(mrhs_new_from_args,v1);v3=v2;v4=$4;v5=dispatch2(mrhs_add_star,v3,v4);$$=v5;}
2614  }
2615  | tSTAR arg_value
2616  {
2617 #if 0
2618  $$ = NEW_SPLAT($2, &@$);
2619 #endif
2620  {VALUE v1,v2,v3,v4;v1=dispatch0(mrhs_new);v2=v1;v3=$2;v4=dispatch2(mrhs_add_star,v2,v3);$$=v4;}
2621  }
2622  ;
2623 
2624 primary : literal
2625  | strings
2626  | xstring
2627  | regexp
2628  | words
2629  | qwords
2630  | symbols
2631  | qsymbols
2632  | var_ref
2633  | backref
2634  | tFID
2635  {
2636 #if 0
2637  $$ = NEW_FCALL($1, 0, &@$);
2638 #endif
2639  {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=dispatch1(fcall,v1);v3=dispatch0(args_new);v4=v2;v5=v3;v6=dispatch2(method_add_arg,v4,v5);$$=v6;}
2640  }
2641  | k_begin
2642  {
2643  CMDARG_PUSH(0);
2644  }
2645  bodystmt
2646  k_end
2647  {
2648  CMDARG_POP();
2649 #if 0
2650  set_line_body($3, @1.end_pos.lineno);
2651  $$ = NEW_BEGIN($3, &@$);
2652  nd_set_line($$, @1.end_pos.lineno);
2653 #endif
2654  {VALUE v1,v2;v1=$3;v2=dispatch1(begin,v1);$$=v2;}
2655  }
2656  | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2657  {
2658 #if 0
2659  $$ = NEW_BEGIN(0, &@$);
2660 #endif
2661  {VALUE v1,v2;v1=0;v2=dispatch1(paren,v1);$$=v2;}
2662  }
2663  | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2664  {
2665 #if 0
2666  if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2667  $$ = $2;
2668 #endif
2669  {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2670  }
2671  | tLPAREN compstmt ')'
2672  {
2673 #if 0
2674  if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2675  $$ = $2;
2676 #endif
2677  {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2678  }
2679  | primary_value tCOLON2 tCONSTANT
2680  {
2681 #if 0
2682  $$ = NEW_COLON2($1, $3, &@$);
2683 #endif
2684  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2685  }
2686  | tCOLON3 tCONSTANT
2687  {
2688 #if 0
2689  $$ = NEW_COLON3($2, &@$);
2690 #endif
2691  {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2692  }
2693  | tLBRACK aref_args ']'
2694  {
2695 #if 0
2696  $$ = make_list($2, &@$);
2697 #endif
2698  {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(array,v1);$$=v2;}
2699  }
2700  | tLBRACE assoc_list '}'
2701  {
2702 #if 0
2703  $$ = new_hash(p, $2, &@$);
2704  $$->nd_brace = TRUE;
2705 #endif
2706  {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(hash,v1);$$=v2;}
2707  }
2708  | k_return
2709  {
2710 #if 0
2711  $$ = NEW_RETURN(0, &@$);
2712 #endif
2713  {VALUE v1;v1=dispatch0(return0);$$=v1;}
2714  }
2715  | keyword_yield '(' call_args rparen
2716  {
2717 #if 0
2718  $$ = new_yield(p, $3, &@$);
2719 #endif
2720  {VALUE v1,v2,v3,v4;v1=$3;v2=dispatch1(paren,v1);v3=v2;v4=dispatch1(yield,v3);$$=v4;}
2721  }
2722  | keyword_yield '(' rparen
2723  {
2724 #if 0
2725  $$ = NEW_YIELD(0, &@$);
2726 #endif
2727  {VALUE v1,v2,v3,v4,v5;v1=dispatch0(args_new);v2=v1;v3=dispatch1(paren,v2);v4=v3;v5=dispatch1(yield,v4);$$=v5;}
2728  }
2729  | keyword_yield
2730  {
2731 #if 0
2732  $$ = NEW_YIELD(0, &@$);
2733 #endif
2734  {VALUE v1;v1=dispatch0(yield0);$$=v1;}
2735  }
2736  | keyword_defined opt_nl '(' {p->in_defined = 1;} expr rparen
2737  {
2738  p->in_defined = 0;
2739  $$ = new_defined(p, $5, &@$);
2740  }
2741  | keyword_not '(' expr rparen
2742  {
2743  $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2744  }
2745  | keyword_not '(' rparen
2746  {
2747  $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
2748  }
2749  | fcall brace_block
2750  {
2751 #if 0
2752  $$ = method_add_block(p, $1, $2, &@$);
2753 #endif
2754  {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9;v1=$1;v2=dispatch1(fcall,v1);v3=dispatch0(args_new);v4=v2;v5=v3;v6=dispatch2(method_add_arg,v4,v5);v7=v6;v8=$2;v9=dispatch2(method_add_block,v7,v8);$$=v9;}
2755  }
2756  | method_call
2757  | method_call brace_block
2758  {
2759 #if 0
2760  block_dup_check(p, $1->nd_args, $2);
2761  $$ = method_add_block(p, $1, $2, &@$);
2762 #endif
2763  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
2764  }
2765  | tLAMBDA
2766  {
2767  token_info_push(p, "->", &@1);
2768  }
2769  lambda
2770  {
2771  $$ = $3;
2772 #if 0
2773  nd_set_first_loc($$, @1.beg_pos);
2774 #endif
2775  }
2776  | k_if expr_value then
2777  compstmt
2778  if_tail
2779  k_end
2780  {
2781 #if 0
2782  $$ = new_if(p, $2, $4, $5, &@$);
2783  fixpos($$, $2);
2784 #endif
2785  {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(if,v1,v2,v3);$$=v4;}
2786  }
2787  | k_unless expr_value then
2788  compstmt
2789  opt_else
2790  k_end
2791  {
2792 #if 0
2793  $$ = new_unless(p, $2, $4, $5, &@$);
2794  fixpos($$, $2);
2795 #endif
2796  {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(unless,v1,v2,v3);$$=v4;}
2797  }
2798  | k_while expr_value_do
2799  compstmt
2800  k_end
2801  {
2802 #if 0
2803  $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
2804  fixpos($$, $2);
2805 #endif
2806  {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(while,v1,v2);$$=v3;}
2807  }
2808  | k_until expr_value_do
2809  compstmt
2810  k_end
2811  {
2812 #if 0
2813  $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
2814  fixpos($$, $2);
2815 #endif
2816  {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(until,v1,v2);$$=v3;}
2817  }
2818  | k_case expr_value opt_terms
2819  {
2820  $<val>$ = p->case_labels;
2821  p->case_labels = Qnil;
2822  }
2823  case_body
2824  k_end
2825  {
2826  if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2827  p->case_labels = $<val>4;
2828 #if 0
2829  $$ = NEW_CASE($2, $5, &@$);
2830  fixpos($$, $2);
2831 #endif
2832  {VALUE v1,v2,v3;v1=$2;v2=$5;v3=dispatch2(case,v1,v2);$$=v3;}
2833  }
2834  | k_case opt_terms
2835  {
2836  $<val>$ = p->case_labels;
2837  p->case_labels = 0;
2838  }
2839  case_body
2840  k_end
2841  {
2842  if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2843  p->case_labels = $<val>3;
2844 #if 0
2845  $$ = NEW_CASE2($4, &@$);
2846 #endif
2847  {VALUE v1,v2,v3;v1=Qnil;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
2848  }
2849  | k_case expr_value opt_terms
2850  p_case_body
2851  k_end
2852  {
2853 #if 0
2854  $$ = new_case3(p, $2, $4, &@$);
2855 #endif
2856  {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
2857  }
2858  | k_for for_var keyword_in expr_value_do
2859  compstmt
2860  k_end
2861  {
2862 #if 0
2863  /*
2864  * for a, b, c in e
2865  * #=>
2866  * e.each{|*x| a, b, c = x}
2867  *
2868  * for a in e
2869  * #=>
2870  * e.each{|x| a, = x}
2871  */
2872  ID id = internal_id(p);
2873  NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
2874  NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
2875  ID *tbl = ALLOC_N(ID, 3);
2876  tbl[0] = 1 /* length of local var table */; tbl[1] = id /* internal id */;
2877  rb_ast_add_local_table(p->ast, tbl);
2878 
2879  switch (nd_type($2)) {
2880  case NODE_LASGN:
2881  case NODE_DASGN:
2882  case NODE_DASGN_CURR: /* e.each {|internal_var| a = internal_var; ... } */
2883  $2->nd_value = internal_var;
2884  id = 0;
2885  m->nd_plen = 1;
2886  m->nd_next = $2;
2887  break;
2888  case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
2889  m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), &@2);
2890  break;
2891  default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
2892  m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, &@2);
2893  }
2894  /* {|*internal_id| <m> = internal_id; ... } */
2895  args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
2896  scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
2897  $$ = NEW_FOR($4, scope, &@$);
2898  fixpos($$, $2);
2899 #endif
2900  {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=$5;v4=dispatch3(for,v1,v2,v3);$$=v4;}
2901  }
2902  | k_class cpath superclass
2903  {
2904  if (p->in_def) {
2905  YYLTYPE loc = code_loc_gen(&@1, &@2);
2906  yyerror1(&loc, "class definition in method body");
2907  }
2908  $<num>1 = p->in_class;
2909  p->in_class = 1;
2910  local_push(p, 0);
2911  }
2912  bodystmt
2913  k_end
2914  {
2915 #if 0
2916  $$ = NEW_CLASS($2, $5, $3, &@$);
2917  nd_set_line($$->nd_body, @6.end_pos.lineno);
2918  set_line_body($5, @3.end_pos.lineno);
2919  nd_set_line($$, @3.end_pos.lineno);
2920 #endif
2921  {VALUE v1,v2,v3,v4;v1=$2;v2=$3;v3=$5;v4=dispatch3(class,v1,v2,v3);$$=v4;}
2922  local_pop(p);
2923  p->in_class = $<num>1 & 1;
2924  }
2925  | k_class tLSHFT expr
2926  {
2927  $<num>$ = (p->in_class << 1) | p->in_def;
2928  p->in_def = 0;
2929  p->in_class = 0;
2930  local_push(p, 0);
2931  }
2932  term
2933  bodystmt
2934  k_end
2935  {
2936 #if 0
2937  $$ = NEW_SCLASS($3, $6, &@$);
2938  nd_set_line($$->nd_body, @7.end_pos.lineno);
2939  set_line_body($6, nd_line($3));
2940  fixpos($$, $3);
2941 #endif
2942  {VALUE v1,v2,v3;v1=$3;v2=$6;v3=dispatch2(sclass,v1,v2);$$=v3;}
2943  local_pop(p);
2944  p->in_def = $<num>4 & 1;
2945  p->in_class = ($<num>4 >> 1) & 1;
2946  }
2947  | k_module cpath
2948  {
2949  if (p->in_def) {
2950  YYLTYPE loc = code_loc_gen(&@1, &@2);
2951  yyerror1(&loc, "module definition in method body");
2952  }
2953  $<num>1 = p->in_class;
2954  p->in_class = 1;
2955  local_push(p, 0);
2956  }
2957  bodystmt
2958  k_end
2959  {
2960 #if 0
2961  $$ = NEW_MODULE($2, $4, &@$);
2962  nd_set_line($$->nd_body, @5.end_pos.lineno);
2963  set_line_body($4, @2.end_pos.lineno);
2964  nd_set_line($$, @2.end_pos.lineno);
2965 #endif
2966  {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(module,v1,v2);$$=v3;}
2967  local_pop(p);
2968  p->in_class = $<num>1 & 1;
2969  }
2970  | k_def fname
2971  {
2972  numparam_name(p, get_id($2));
2973  local_push(p, 0);
2974  $<id>$ = p->cur_arg;
2975  p->cur_arg = 0;
2976  }
2977  {
2978  $<num>$ = p->in_def;
2979  p->in_def = 1;
2980  }
2981  f_arglist
2982  bodystmt
2983  k_end
2984  {
2985 #if 0
2986  NODE *body = remove_begin($6);
2987  reduce_nodes(p, &body);
2988  $$ = NEW_DEFN($2, $5, body, &@$);
2989  nd_set_line($$->nd_defn, @7.end_pos.lineno);
2990  set_line_body(body, @1.beg_pos.lineno);
2991 #endif
2992  {VALUE v1,v2,v3,v4;v1=$2;v2=$5;v3=$6;v4=dispatch3(def,v1,v2,v3);$$=v4;}
2993  local_pop(p);
2994  p->in_def = $<num>4 & 1;
2995  p->cur_arg = $<id>3;
2996  }
2997  | k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname
2998  {
2999  numparam_name(p, get_id($5));
3000  $<num>4 = p->in_def;
3001  p->in_def = 1;
3002  SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3003  local_push(p, 0);
3004  $<id>$ = p->cur_arg;
3005  p->cur_arg = 0;
3006  }
3007  f_arglist
3008  bodystmt
3009  k_end
3010  {
3011 #if 0
3012  NODE *body = remove_begin($8);
3013  reduce_nodes(p, &body);
3014  $$ = NEW_DEFS($2, $5, $7, body, &@$);
3015  nd_set_line($$->nd_defn, @9.end_pos.lineno);
3016  set_line_body(body, @1.beg_pos.lineno);
3017 #endif
3018  {VALUE v1,v2,v3,v4,v5,v6;v1=$2;v2=$3;v3=$5;v4=$7;v5=$8;v6=dispatch5(defs,v1,v2,v3,v4,v5);$$=v6;}
3019  local_pop(p);
3020  p->in_def = $<num>4 & 1;
3021  p->cur_arg = $<id>6;
3022  }
3023  | keyword_break
3024  {
3025 #if 0
3026  $$ = NEW_BREAK(0, &@$);
3027 #endif
3028  {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(break,v2);$$=v3;}
3029  }
3030  | keyword_next
3031  {
3032 #if 0
3033  $$ = NEW_NEXT(0, &@$);
3034 #endif
3035  {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(next,v2);$$=v3;}
3036  }
3037  | keyword_redo
3038  {
3039 #if 0
3040  $$ = NEW_REDO(&@$);
3041 #endif
3042  {VALUE v1;v1=dispatch0(redo);$$=v1;}
3043  }
3044  | keyword_retry
3045  {
3046 #if 0
3047  $$ = NEW_RETRY(&@$);
3048 #endif
3049  {VALUE v1;v1=dispatch0(retry);$$=v1;}
3050  }
3051  ;
3052 
3053 primary_value : primary
3054  {
3055  value_expr($1);
3056  $$ = $1;
3057  }
3058  ;
3059 
3060 k_begin : keyword_begin
3061  {
3062  token_info_push(p, "begin", &@$);
3063  }
3064  ;
3065 
3066 k_if : keyword_if
3067  {
3068  WARN_EOL("if");
3069  token_info_push(p, "if", &@$);
3070  if (p->token_info && p->token_info->nonspc &&
3071  p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3072  const char *tok = p->lex.ptok;
3073  const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3074  beg += rb_strlen_lit("else");
3075  while (beg < tok && ISSPACE(*beg)) beg++;
3076  if (beg == tok) {
3077  p->token_info->nonspc = 0;
3078  }
3079  }
3080  }
3081  ;
3082 
3083 k_unless : keyword_unless
3084  {
3085  token_info_push(p, "unless", &@$);
3086  }
3087  ;
3088 
3089 k_while : keyword_while
3090  {
3091  token_info_push(p, "while", &@$);
3092  }
3093  ;
3094 
3095 k_until : keyword_until
3096  {
3097  token_info_push(p, "until", &@$);
3098  }
3099  ;
3100 
3101 k_case : keyword_case
3102  {
3103  token_info_push(p, "case", &@$);
3104  }
3105  ;
3106 
3107 k_for : keyword_for
3108  {
3109  token_info_push(p, "for", &@$);
3110  }
3111  ;
3112 
3113 k_class : keyword_class
3114  {
3115  token_info_push(p, "class", &@$);
3116  }
3117  ;
3118 
3119 k_module : keyword_module
3120  {
3121  token_info_push(p, "module", &@$);
3122  }
3123  ;
3124 
3125 k_def : keyword_def
3126  {
3127  token_info_push(p, "def", &@$);
3128  }
3129  ;
3130 
3131 k_do : keyword_do
3132  {
3133  token_info_push(p, "do", &@$);
3134  }
3135  ;
3136 
3137 k_do_block : keyword_do_block
3138  {
3139  token_info_push(p, "do", &@$);
3140  }
3141  ;
3142 
3143 k_rescue : keyword_rescue
3144  {
3145  token_info_warn(p, "rescue", p->token_info, 1, &@$);
3146  }
3147  ;
3148 
3149 k_ensure : keyword_ensure
3150  {
3151  token_info_warn(p, "ensure", p->token_info, 1, &@$);
3152  }
3153  ;
3154 
3155 k_when : keyword_when
3156  {
3157  token_info_warn(p, "when", p->token_info, 0, &@$);
3158  }
3159  ;
3160 
3161 k_else : keyword_else
3162  {
3163  token_info *ptinfo_beg = p->token_info;
3164  int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3165  token_info_warn(p, "else", p->token_info, same, &@$);
3166  if (same) {
3167  token_info e;
3168  e.next = ptinfo_beg->next;
3169  e.token = "else";
3170  token_info_setup(&e, p->lex.pbeg, &@$);
3171  if (!e.nonspc) *ptinfo_beg = e;
3172  }
3173  }
3174  ;
3175 
3176 k_elsif : keyword_elsif
3177  {
3178  WARN_EOL("elsif");
3179  token_info_warn(p, "elsif", p->token_info, 1, &@$);
3180  }
3181  ;
3182 
3183 k_end : keyword_end
3184  {
3185  token_info_pop(p, "end", &@$);
3186  }
3187  ;
3188 
3189 k_return : keyword_return
3190  {
3191  if (p->in_class && !p->in_def && !dyna_in_block(p))
3192  yyerror1(&@1, "Invalid return in class/module body");
3193  }
3194  ;
3195 
3196 then : term
3197  | keyword_then
3198  | term keyword_then
3199  ;
3200 
3201 do : term
3202  | keyword_do_cond
3203  ;
3204 
3205 if_tail : opt_else
3206  | k_elsif expr_value then
3207  compstmt
3208  if_tail
3209  {
3210 #if 0
3211  $$ = new_if(p, $2, $4, $5, &@$);
3212  fixpos($$, $2);
3213 #endif
3214  {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(elsif,v1,v2,v3);$$=v4;}
3215  }
3216  ;
3217 
3218 opt_else : none
3219  | k_else compstmt
3220  {
3221 #if 0
3222  $$ = $2;
3223 #endif
3224  {VALUE v1,v2;v1=$2;v2=dispatch1(else,v1);$$=v2;}
3225  }
3226  ;
3227 
3228 for_var : lhs
3229  | mlhs
3230  ;
3231 
3232 f_marg : f_norm_arg
3233  {
3234 #if 0
3235  $$ = assignable(p, $1, 0, &@$);
3236  mark_lvar_used(p, $$);
3237 #endif
3238  $$=assignable(p, $1);
3239  }
3240  | tLPAREN f_margs rparen
3241  {
3242 #if 0
3243  $$ = $2;
3244 #endif
3245  {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
3246  }
3247  ;
3248 
3249 f_marg_list : f_marg
3250  {
3251 #if 0
3252  $$ = NEW_LIST($1, &@$);
3253 #endif
3254  {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
3255  }
3256  | f_marg_list ',' f_marg
3257  {
3258 #if 0
3259  $$ = list_append(p, $1, $3);
3260 #endif
3261  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
3262  }
3263  ;
3264 
3265 f_margs : f_marg_list
3266  {
3267 #if 0
3268  $$ = NEW_MASGN($1, 0, &@$);
3269 #endif
3270  $$=$1;
3271  }
3272  | f_marg_list ',' f_rest_marg
3273  {
3274 #if 0
3275  $$ = NEW_MASGN($1, $3, &@$);
3276 #endif
3277  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
3278  }
3279  | f_marg_list ',' f_rest_marg ',' f_marg_list
3280  {
3281 #if 0
3282  $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3283 #endif
3284  {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);v4=v3;v5=$5;v6=dispatch2(mlhs_add_post,v4,v5);$$=v6;}
3285  }
3286  | f_rest_marg
3287  {
3288 #if 0
3289  $$ = NEW_MASGN(0, $1, &@$);
3290 #endif
3291  {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
3292  }
3293  | f_rest_marg ',' f_marg_list
3294  {
3295 #if 0
3296  $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3297 #endif
3298  {VALUE v1,v2,v3,v4,v5,v6,v7;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add_star,v2,v3);v5=v4;v6=$3;v7=dispatch2(mlhs_add_post,v5,v6);$$=v7;}
3299  }
3300  ;
3301 
3302 f_rest_marg : tSTAR f_norm_arg
3303  {
3304 #if 0
3305  $$ = assignable(p, $2, 0, &@$);
3306  mark_lvar_used(p, $$);
3307 #endif
3308  $$=assignable(p, $2);
3309  }
3310  | tSTAR
3311  {
3312 #if 0
3313  $$ = NODE_SPECIAL_NO_NAME_REST;
3314 #endif
3315  $$=Qnil;
3316  }
3317  ;
3318 
3319 block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3320  {
3321  $$ = new_args_tail(p, $1, $3, $4, &@3);
3322  }
3323  | f_block_kwarg opt_f_block_arg
3324  {
3325  $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3326  }
3327  | f_kwrest opt_f_block_arg
3328  {
3329  $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3330  }
3331  | f_no_kwarg opt_f_block_arg
3332  {
3333  $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
3334  }
3335  | f_block_arg
3336  {
3337  $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3338  }
3339  ;
3340 
3341 opt_block_args_tail : ',' block_args_tail
3342  {
3343  $$ = $2;
3344  }
3345  | /* none */
3346  {
3347  $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3348  }
3349  ;
3350 
3351 block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3352  {
3353  $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3354  }
3355  | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3356  {
3357  $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3358  }
3359  | f_arg ',' f_block_optarg opt_block_args_tail
3360  {
3361  $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3362  }
3363  | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3364  {
3365  $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3366  }
3367  | f_arg ',' f_rest_arg opt_block_args_tail
3368  {
3369  $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3370  }
3371  | f_arg ','
3372  {
3373 #if 0
3374  /* magic number for rest_id in iseq_set_arguments() */
3375  $$ = new_args(p, $1, Qnone, NODE_SPECIAL_EXCESSIVE_COMMA, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, &@1), &@$);
3376 #endif
3377  {VALUE v1;v1=dispatch0(excessed_comma);$$=new_args(p, $1, Qnone, v1, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, NULL), NULL);}
3378  }
3379  | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3380  {
3381  $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
3382  }
3383  | f_arg opt_block_args_tail
3384  {
3385  $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
3386  }
3387  | f_block_optarg ',' f_rest_arg opt_block_args_tail
3388  {
3389  $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
3390  }
3391  | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3392  {
3393  $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
3394  }
3395  | f_block_optarg opt_block_args_tail
3396  {
3397  $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
3398  }
3399  | f_block_optarg ',' f_arg opt_block_args_tail
3400  {
3401  $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
3402  }
3403  | f_rest_arg opt_block_args_tail
3404  {
3405  $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
3406  }
3407  | f_rest_arg ',' f_arg opt_block_args_tail
3408  {
3409  $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
3410  }
3411  | block_args_tail
3412  {
3413  $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
3414  }
3415  ;
3416 
3417 opt_block_param : none
3418  | block_param_def
3419  {
3420  p->command_start = TRUE;
3421  }
3422  ;
3423 
3424 block_param_def : '|' opt_bv_decl '|'
3425  {
3426  p->cur_arg = 0;
3427  p->max_numparam = ORDINAL_PARAM;
3428 #if 0
3429  $$ = 0;
3430 #endif
3431  {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11;v1=Qnil;v2=Qnil;v3=Qnil;v4=Qnil;v5=Qnil;v6=Qnil;v7=Qnil;v8=dispatch7(params,v1,v2,v3,v4,v5,v6,v7);v9=v8;v10=escape_Qundef($2);v11=dispatch2(block_var,v9,v10);$$=v11;}
3432  }
3433  | '|' block_param opt_bv_decl '|'
3434  {
3435  p->cur_arg = 0;
3436  p->max_numparam = ORDINAL_PARAM;
3437 #if 0
3438  $$ = $2;
3439 #endif
3440  {VALUE v1,v2,v3;v1=escape_Qundef($2);v2=escape_Qundef($3);v3=dispatch2(block_var,v1,v2);$$=v3;}
3441  }
3442  ;
3443 
3444 
3445 opt_bv_decl : opt_nl
3446  {
3447  $$ = 0;
3448  }
3449  | opt_nl ';' bv_decls opt_nl
3450  {
3451 #if 0
3452  $$ = 0;
3453 #endif
3454  $$=$3;
3455  }
3456  ;
3457 
3458 bv_decls : bvar
3459  {$$=rb_ary_new3(1, get_value($1));}
3460  | bv_decls ',' bvar
3461  {$$=rb_ary_push($1, get_value($3));}
3462  ;
3463 
3464 bvar : tIDENTIFIER
3465  {
3466  new_bv(p, get_id($1));
3467  $$=get_value($1);
3468  }
3469  | f_bad_arg
3470  {
3471  $$ = 0;
3472  }
3473  ;
3474 
3475 lambda : {
3476  $<vars>$ = dyna_push(p);
3477  }
3478  {
3479  $<num>$ = p->lex.lpar_beg;
3480  p->lex.lpar_beg = p->lex.paren_nest;
3481  }
3482  {
3483  $<num>$ = p->max_numparam;
3484  p->max_numparam = 0;
3485  }
3486  {
3487  $<node>$ = numparam_push(p);
3488  }
3489  f_larglist
3490  {
3491  CMDARG_PUSH(0);
3492  }
3493  lambda_body
3494  {
3495  int max_numparam = p->max_numparam;
3496  p->lex.lpar_beg = $<num>2;
3497  p->max_numparam = $<num>3;
3498  CMDARG_POP();
3499  $5 = args_with_numbered(p, $5, max_numparam);
3500 #if 0
3501  {
3502  YYLTYPE loc = code_loc_gen(&@5, &@7);
3503  $$ = NEW_LAMBDA($5, $7, &loc);
3504  nd_set_line($$->nd_body, @7.end_pos.lineno);
3505  nd_set_line($$, @5.end_pos.lineno);
3506  }
3507 #endif
3508  {VALUE v1,v2,v3;v1=$5;v2=$7;v3=dispatch2(lambda,v1,v2);$$=v3;}
3509  numparam_pop(p, $<node>4);
3510  dyna_pop(p, $<vars>1);
3511  }
3512  ;
3513 
3514 f_larglist : '(' f_args opt_bv_decl ')'
3515  {
3516 #if 0
3517  $$ = $2;
3518  p->max_numparam = ORDINAL_PARAM;
3519 #endif
3520  {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
3521  }
3522  | f_args
3523  {
3524 #if 0
3525  if (!args_info_empty_p($1->nd_ainfo))
3526  p->max_numparam = ORDINAL_PARAM;
3527 #endif
3528  $$ = $1;
3529  }
3530  ;
3531 
3532 lambda_body : tLAMBEG compstmt '}'
3533  {
3534  token_info_pop(p, "}", &@3);
3535  $$ = $2;
3536  }
3537  | keyword_do_LAMBDA bodystmt k_end
3538  {
3539  $$ = $2;
3540  }
3541  ;
3542 
3543 do_block : k_do_block do_body k_end
3544  {
3545  $$ = $2;
3546 #if 0
3547  $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3548  nd_set_line($$, @1.end_pos.lineno);
3549 #endif
3550  }
3551  ;
3552 
3553 block_call : command do_block
3554  {
3555 #if 0
3556  if (nd_type($1) == NODE_YIELD) {
3557  compile_error(p, "block given to yield");
3558  }
3559  else {
3560  block_dup_check(p, $1->nd_args, $2);
3561  }
3562  $$ = method_add_block(p, $1, $2, &@$);
3563  fixpos($$, $1);
3564 #endif
3565  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
3566  }
3567  | block_call call_op2 operation2 opt_paren_args
3568  {
3569 #if 0
3570  $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3571 #endif
3572  {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=v6==Qundef ? v5 : dispatch2(method_add_arg,v5,v6);$$=v7;}
3573  }
3574  | block_call call_op2 operation2 opt_paren_args brace_block
3575  {
3576 #if 0
3577  $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3578 #endif
3579  {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=v7==Qundef ? v6 : dispatch2(method_add_block,v6,v7);$$=v8;}
3580  }
3581  | block_call call_op2 operation2 command_args do_block
3582  {
3583 #if 0
3584  $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3585 #endif
3586  {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=dispatch2(method_add_block,v6,v7);$$=v8;}
3587  }
3588  ;
3589 
3590 method_call : fcall paren_args
3591  {
3592 #if 0
3593  $$ = $1;
3594  $$->nd_args = $2;
3595  nd_set_last_loc($1, @2.end_pos);
3596 #endif
3597  {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(fcall,v1);v3=v2;v4=$2;v5=dispatch2(method_add_arg,v3,v4);$$=v5;}
3598  }
3599  | primary_value call_op operation2 opt_paren_args
3600  {
3601 #if 0
3602  $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3603  nd_set_line($$, @3.end_pos.lineno);
3604 #endif
3605  {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=v6==Qundef ? v5 : dispatch2(method_add_arg,v5,v6);$$=v7;}
3606  }
3607  | primary_value tCOLON2 operation2 paren_args
3608  {
3609 #if 0
3610  $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
3611  nd_set_line($$, @3.end_pos.lineno);
3612 #endif
3613  {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
3614  }
3615  | primary_value tCOLON2 operation3
3616  {
3617 #if 0
3618  $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
3619 #endif
3620  {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(call,v1,v2,v3);$$=v4;}
3621  }
3622  | primary_value call_op paren_args
3623  {
3624 #if 0
3625  $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
3626  nd_set_line($$, @2.end_pos.lineno);
3627 #endif
3628  {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=ID2VAL(idCall);v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$3;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
3629  }
3630  | primary_value tCOLON2 paren_args
3631  {
3632 #if 0
3633  $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
3634  nd_set_line($$, @2.end_pos.lineno);
3635 #endif
3636  {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=ID2VAL(idCOLON2);v3=ID2VAL(idCall);v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$3;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
3637  }
3638  | keyword_super paren_args
3639  {
3640 #if 0
3641  $$ = NEW_SUPER($2, &@$);
3642 #endif
3643  {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
3644  }
3645  | keyword_super
3646  {
3647 #if 0
3648  $$ = NEW_ZSUPER(&@$);
3649 #endif
3650  {VALUE v1;v1=dispatch0(zsuper);$$=v1;}
3651  }
3652  | primary_value '[' opt_call_args rbracket
3653  {
3654 #if 0
3655  if ($1 && nd_type($1) == NODE_SELF)
3656  $$ = NEW_FCALL(tAREF, $3, &@$);
3657  else
3658  $$ = NEW_CALL($1, tAREF, $3, &@$);
3659  fixpos($$, $1);
3660 #endif
3661  {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref,v1,v2);$$=v3;}
3662  }
3663  ;
3664 
3665 brace_block : '{' brace_body '}'
3666  {
3667  $$ = $2;
3668 #if 0
3669  $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3670  nd_set_line($$, @1.end_pos.lineno);
3671 #endif
3672  }
3673  | k_do do_body k_end
3674  {
3675  $$ = $2;
3676 #if 0
3677  $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3678  nd_set_line($$, @1.end_pos.lineno);
3679 #endif
3680  }
3681  ;
3682 
3683 brace_body : {$<vars>$ = dyna_push(p);}
3684  {
3685  $<num>$ = p->max_numparam;
3686  p->max_numparam = 0;
3687  }
3688  {
3689  $<node>$ = numparam_push(p);
3690  }
3691  opt_block_param compstmt
3692  {
3693  int max_numparam = p->max_numparam;
3694  p->max_numparam = $<num>2;
3695  $4 = args_with_numbered(p, $4, max_numparam);
3696 #if 0
3697  $$ = NEW_ITER($4, $5, &@$);
3698 #endif
3699  {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(brace_block,v1,v2);$$=v3;}
3700  numparam_pop(p, $<node>3);
3701  dyna_pop(p, $<vars>1);
3702  }
3703  ;
3704 
3705 do_body : {$<vars>$ = dyna_push(p);}
3706  {
3707  $<num>$ = p->max_numparam;
3708  p->max_numparam = 0;
3709  }
3710  {
3711  $<node>$ = numparam_push(p);
3712  CMDARG_PUSH(0);
3713  }
3714  opt_block_param bodystmt
3715  {
3716  int max_numparam = p->max_numparam;
3717  p->max_numparam = $<num>2;
3718  $4 = args_with_numbered(p, $4, max_numparam);
3719 #if 0
3720  $$ = NEW_ITER($4, $5, &@$);
3721 #endif
3722  {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(do_block,v1,v2);$$=v3;}
3723  CMDARG_POP();
3724  numparam_pop(p, $<node>3);
3725  dyna_pop(p, $<vars>1);
3726  }
3727  ;
3728 
3729 case_args : arg_value
3730  {
3731 #if 0
3732  check_literal_when(p, $1, &@1);
3733  $$ = NEW_LIST($1, &@$);
3734 #endif
3735  {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
3736  }
3737  | tSTAR arg_value
3738  {
3739 #if 0
3740  $$ = NEW_SPLAT($2, &@$);
3741 #endif
3742  {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
3743  }
3744  | case_args ',' arg_value
3745  {
3746 #if 0
3747  check_literal_when(p, $3, &@3);
3748  $$ = last_arg_append(p, $1, $3, &@$);
3749 #endif
3750  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
3751  }
3752  | case_args ',' tSTAR arg_value
3753  {
3754 #if 0
3755  $$ = rest_arg_append(p, $1, $4, &@$);
3756 #endif
3757  {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
3758  }
3759  ;
3760 
3761 case_body : k_when case_args then
3762  compstmt
3763  cases
3764  {
3765 #if 0
3766  $$ = NEW_WHEN($2, $4, $5, &@$);
3767  fixpos($$, $2);
3768 #endif
3769  {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(when,v1,v2,v3);$$=v4;}
3770  }
3771  ;
3772 
3773 cases : opt_else
3774  | case_body
3775  ;
3776 
3777 p_case_body : keyword_in
3778  {
3779  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
3780  p->command_start = FALSE;
3781  $<num>$ = p->in_kwarg;
3782  p->in_kwarg = 1;
3783  }
3784  {$<tbl>$ = push_pvtbl(p);}
3785  {$<tbl>$ = push_pktbl(p);}
3786  p_top_expr then
3787  {pop_pktbl(p, $<tbl>4);}
3788  {pop_pvtbl(p, $<tbl>3);}
3789  {
3790  p->in_kwarg = !!$<num>2;
3791  }
3792  compstmt
3793  p_cases
3794  {
3795 #if 0
3796  $$ = NEW_IN($5, $10, $11, &@$);
3797 #endif
3798  {VALUE v1,v2,v3,v4;v1=$5;v2=$10;v3=escape_Qundef($11);v4=dispatch3(in,v1,v2,v3);$$=v4;}
3799  }
3800  ;
3801 
3802 p_cases : opt_else
3803  | p_case_body
3804  ;
3805 
3806 p_top_expr : p_top_expr_body
3807  | p_top_expr_body modifier_if expr_value
3808  {
3809 #if 0
3810  $$ = new_if(p, $3, remove_begin($1), 0, &@$);
3811  fixpos($$, $3);
3812 #endif
3813  {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
3814  }
3815  | p_top_expr_body modifier_unless expr_value
3816  {
3817 #if 0
3818  $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
3819  fixpos($$, $3);
3820 #endif
3821  {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
3822  }
3823  ;
3824 
3825 p_top_expr_body : p_expr
3826  | p_expr ','
3827  {
3828  $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
3829  $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
3830  }
3831  | p_expr ',' p_args
3832  {
3833  $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
3834 #if 0
3835  nd_set_first_loc($$, @1.beg_pos);
3836 #endif
3837 
3838  }
3839  | p_args_tail
3840  {
3841  $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
3842  }
3843  | p_kwargs
3844  {
3845  $$ = new_hash_pattern(p, Qnone, $1, &@$);
3846  }
3847  ;
3848 
3849 p_expr : p_as
3850  ;
3851 
3852 p_as : p_expr tASSOC p_variable
3853  {
3854 #if 0
3855  NODE *n = NEW_LIST($1, &@$);
3856  n = list_append(p, n, $3);
3857  $$ = new_hash(p, n, &@$);
3858 #endif
3859  {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(id_assoc);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
3860  }
3861  | p_alt
3862  ;
3863 
3864 p_alt : p_alt '|' p_expr_basic
3865  {
3866 #if 0
3867  $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
3868 #endif
3869  {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(idOr);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
3870  }
3871  | p_expr_basic
3872  ;
3873 
3874 p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
3875 p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
3876 
3877 p_expr_basic : p_value
3878  | p_const p_lparen p_args rparen
3879  {
3880  pop_pktbl(p, $<tbl>2);
3881  $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3882 #if 0
3883  nd_set_first_loc($$, @1.beg_pos);
3884 #endif
3885 
3886  }
3887  | p_const p_lparen p_kwargs rparen
3888  {
3889  pop_pktbl(p, $<tbl>2);
3890  $$ = new_hash_pattern(p, $1, $3, &@$);
3891 #if 0
3892  nd_set_first_loc($$, @1.beg_pos);
3893 #endif
3894 
3895  }
3896  | p_const '(' rparen
3897  {
3898  $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3899  $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3900  }
3901  | p_const p_lbracket p_args rbracket
3902  {
3903  pop_pktbl(p, $<tbl>2);
3904  $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3905 #if 0
3906  nd_set_first_loc($$, @1.beg_pos);
3907 #endif
3908 
3909  }
3910  | p_const p_lbracket p_kwargs rbracket
3911  {
3912  pop_pktbl(p, $<tbl>2);
3913  $$ = new_hash_pattern(p, $1, $3, &@$);
3914 #if 0
3915  nd_set_first_loc($$, @1.beg_pos);
3916 #endif
3917 
3918  }
3919  | p_const '[' rbracket
3920  {
3921  $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3922  $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3923  }
3924  | tLBRACK {$<tbl>$ = push_pktbl(p);} p_args rbracket
3925  {
3926  pop_pktbl(p, $<tbl>2);
3927  $$ = new_array_pattern(p, Qnone, Qnone, $3, &@$);
3928  }
3929  | tLBRACK rbracket
3930  {
3931  $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3932  $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
3933  }
3934  | tLBRACE
3935  {
3936  $<tbl>$ = push_pktbl(p);
3937  $<num>1 = p->in_kwarg;
3938  p->in_kwarg = 0;
3939  }
3940  p_kwargs rbrace
3941  {
3942  pop_pktbl(p, $<tbl>2);
3943  p->in_kwarg = $<num>1;
3944  $$ = new_hash_pattern(p, Qnone, $3, &@$);
3945  }
3946  | tLBRACE rbrace
3947  {
3948  $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
3949  $$ = new_hash_pattern(p, Qnone, $$, &@$);
3950  }
3951  | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
3952  {
3953  pop_pktbl(p, $<tbl>2);
3954  $$ = $3;
3955  }
3956  ;
3957 
3958 p_args : p_expr
3959  {
3960 #if 0
3961  NODE *pre_args = NEW_LIST($1, &@$);
3962  $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
3963 #endif
3964  $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
3965 
3966  }
3967  | p_args_head
3968  {
3969  $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
3970  }
3971  | p_args_head p_arg
3972  {
3973 #if 0
3974  $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
3975 #endif
3976  VALUE pre_args = rb_ary_concat($1, get_value($2));
3977  $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
3978 
3979  }
3980  | p_args_head tSTAR tIDENTIFIER
3981  {
3982  $$ = new_array_pattern_tail(p, $1, 1, $3, Qnone, &@$);
3983  }
3984  | p_args_head tSTAR tIDENTIFIER ',' p_args_post
3985  {
3986  $$ = new_array_pattern_tail(p, $1, 1, $3, $5, &@$);
3987  }
3988  | p_args_head tSTAR
3989  {
3990  $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
3991  }
3992  | p_args_head tSTAR ',' p_args_post
3993  {
3994  $$ = new_array_pattern_tail(p, $1, 1, 0, $4, &@$);
3995  }
3996  | p_args_tail
3997  ;
3998 
3999 p_args_head : p_arg ','
4000  {
4001  $$ = $1;
4002  }
4003  | p_args_head p_arg ','
4004  {
4005 #if 0
4006  $$ = list_concat($1, $2);
4007 #endif
4008  $$=rb_ary_concat($1, get_value($2));
4009  }
4010  ;
4011 
4012 p_args_tail : tSTAR tIDENTIFIER
4013  {
4014  $$ = new_array_pattern_tail(p, Qnone, 1, $2, Qnone, &@$);
4015  }
4016  | tSTAR tIDENTIFIER ',' p_args_post
4017  {
4018  $$ = new_array_pattern_tail(p, Qnone, 1, $2, $4, &@$);
4019  }
4020  | tSTAR
4021  {
4022  $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4023  }
4024  | tSTAR ',' p_args_post
4025  {
4026  $$ = new_array_pattern_tail(p, Qnone, 1, 0, $3, &@$);
4027  }
4028  ;
4029 
4030 p_args_post : p_arg
4031  | p_args_post ',' p_arg
4032  {
4033 #if 0
4034  $$ = list_concat($1, $3);
4035 #endif
4036  $$=rb_ary_concat($1, get_value($3));
4037  }
4038  ;
4039 
4040 p_arg : p_expr
4041  {
4042 #if 0
4043  $$ = NEW_LIST($1, &@$);
4044 #endif
4045  $$=rb_ary_new_from_args(1, get_value($1));
4046  }
4047  ;
4048 
4049 p_kwargs : p_kwarg ',' p_kwrest
4050  {
4051  $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4052  }
4053  | p_kwarg
4054  {
4055  $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4056  }
4057  | p_kwarg ','
4058  {
4059  $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4060  }
4061  | p_kwrest
4062  {
4063  $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4064  }
4065  | p_kwarg ',' p_kwnorest
4066  {
4067  $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), ID2VAL(idNil), &@$);
4068  }
4069  | p_kwnorest
4070  {
4071  $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), ID2VAL(idNil), &@$);
4072  }
4073  ;
4074 
4075 p_kwarg : p_kw
4076  {$$=rb_ary_new_from_args(1, $1);}
4077  | p_kwarg ',' p_kw
4078  {
4079 #if 0
4080  $$ = list_concat($1, $3);
4081 #endif
4082  $$=rb_ary_push($1, $3);
4083  }
4084  ;
4085 
4086 p_kw : p_kw_label p_expr
4087  {
4088  error_duplicate_pattern_key(p, get_id($1), &@1);
4089 #if 0
4090  $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), $2);
4091 #endif
4092  $$=rb_ary_new_from_args(2, get_value($1), get_value($2));
4093  }
4094  | p_kw_label
4095  {
4096  error_duplicate_pattern_key(p, get_id($1), &@1);
4097  if ($1 && !is_local_id(get_id($1))) {
4098  yyerror1(&@1, "key must be valid as local variables");
4099  }
4100  error_duplicate_pattern_variable(p, get_id($1), &@1);
4101 #if 0
4102  $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4103 #endif
4104  $$=rb_ary_new_from_args(2, get_value($1), Qnil);
4105  }
4106  ;
4107 
4108 p_kw_label : tLABEL
4109  | tSTRING_BEG string_contents tLABEL_END
4110  {
4111  YYLTYPE loc = code_loc_gen(&@1, &@3);
4112 #if 0
4113  if (!$2 || nd_type($2) == NODE_STR) {
4114  NODE *node = dsym_node(p, $2, &loc);
4115  $$ = SYM2ID(node->nd_lit);
4116  }
4117 #endif
4118  if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4119  VALUE label = RNODE($2)->nd_cval;
4120  VALUE rval = RNODE($2)->nd_rval;
4121  $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4122  RNODE($$)->nd_loc = loc;
4123  }
4124 
4125  else {
4126  yyerror1(&loc, "symbol literal with interpolation is not allowed");
4127  $$ = 0;
4128  }
4129  }
4130  ;
4131 
4132 p_kwrest : kwrest_mark tIDENTIFIER
4133  {
4134  $$ = $2;
4135  }
4136  | kwrest_mark
4137  {
4138  $$ = 0;
4139  }
4140  ;
4141 
4142 p_kwnorest : kwrest_mark keyword_nil
4143  {
4144  $$ = 0;
4145  }
4146  ;
4147 
4148 p_value : p_primitive
4149  | p_primitive tDOT2 p_primitive
4150  {
4151 #if 0
4152  value_expr($1);
4153  value_expr($3);
4154  $$ = NEW_DOT2($1, $3, &@$);
4155 #endif
4156  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
4157  }
4158  | p_primitive tDOT3 p_primitive
4159  {
4160 #if 0
4161  value_expr($1);
4162  value_expr($3);
4163  $$ = NEW_DOT3($1, $3, &@$);
4164 #endif
4165  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
4166  }
4167  | p_primitive tDOT2
4168  {
4169 #if 0
4170  YYLTYPE loc;
4171  loc.beg_pos = @2.end_pos;
4172  loc.end_pos = @2.end_pos;
4173 
4174  value_expr($1);
4175  $$ = NEW_DOT2($1, new_nil(&loc), &@$);
4176 #endif
4177  {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
4178  }
4179  | p_primitive tDOT3
4180  {
4181 #if 0
4182  YYLTYPE loc;
4183  loc.beg_pos = @2.end_pos;
4184  loc.end_pos = @2.end_pos;
4185 
4186  value_expr($1);
4187  $$ = NEW_DOT3($1, new_nil(&loc), &@$);
4188 #endif
4189  {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
4190  }
4191  | p_variable
4192  | p_var_ref
4193  | p_const
4194  | tBDOT2 p_primitive
4195  {
4196 #if 0
4197  YYLTYPE loc;
4198  loc.beg_pos = @1.beg_pos;
4199  loc.end_pos = @1.beg_pos;
4200 
4201  value_expr($2);
4202  $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
4203 #endif
4204  {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
4205  }
4206  | tBDOT3 p_primitive
4207  {
4208 #if 0
4209  YYLTYPE loc;
4210  loc.beg_pos = @1.beg_pos;
4211  loc.end_pos = @1.beg_pos;
4212 
4213  value_expr($2);
4214  $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
4215 #endif
4216  {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
4217  }
4218  ;
4219 
4220 p_primitive : literal
4221  | strings
4222  | xstring
4223  | regexp
4224  | words
4225  | qwords
4226  | symbols
4227  | qsymbols
4228  | keyword_variable
4229  {
4230 #if 0
4231  if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4232 #endif
4233  {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4234  }
4235  | tLAMBDA
4236  {
4237  token_info_push(p, "->", &@1);
4238  }
4239  lambda
4240  {
4241  $$ = $3;
4242 #if 0
4243  nd_set_first_loc($$, @1.beg_pos);
4244 #endif
4245  }
4246  ;
4247 
4248 p_variable : tIDENTIFIER
4249  {
4250 #if 0
4251  error_duplicate_pattern_variable(p, $1, &@1);
4252  $$ = assignable(p, $1, 0, &@$);
4253 #endif
4254  $$=assignable(p, var_field(p, $1));
4255  }
4256  ;
4257 
4258 p_var_ref : '^' tIDENTIFIER
4259  {
4260 #if 0
4261  NODE *n = gettable(p, $2, &@$);
4262  if (!(nd_type(n) == NODE_LVAR || nd_type(n) == NODE_DVAR)) {
4263  compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4264  }
4265  $$ = n;
4266 #endif
4267  {VALUE v1,v2;v1=$2;v2=dispatch1(var_ref,v1);$$=v2;}
4268  }
4269  ;
4270 
4271 p_const : tCOLON3 cname
4272  {
4273 #if 0
4274  $$ = NEW_COLON3($2, &@$);
4275 #endif
4276  {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
4277  }
4278  | p_const tCOLON2 cname
4279  {
4280 #if 0
4281  $$ = NEW_COLON2($1, $3, &@$);
4282 #endif
4283  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
4284  }
4285  | tCONSTANT
4286  {
4287 #if 0
4288  $$ = gettable(p, $1, &@$);
4289 #endif
4290  {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4291  }
4292  ;
4293 
4294 opt_rescue : k_rescue exc_list exc_var then
4295  compstmt
4296  opt_rescue
4297  {
4298 #if 0
4299  $$ = NEW_RESBODY($2,
4300  $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), &@3), $5) : $5,
4301  $6, &@$);
4302  fixpos($$, $2?$2:$5);
4303 #endif
4304  {VALUE v1,v2,v3,v4,v5;v1=escape_Qundef($2);v2=escape_Qundef($3);v3=escape_Qundef($5);v4=escape_Qundef($6);v5=dispatch4(rescue,v1,v2,v3,v4);$$=v5;}
4305  }
4306  | none
4307  ;
4308 
4309 exc_list : arg_value
4310  {
4311 #if 0
4312  $$ = NEW_LIST($1, &@$);
4313 #endif
4314  $$=rb_ary_new3(1, get_value($1));
4315  }
4316  | mrhs
4317  {
4318 #if 0
4319  if (!($$ = splat_array($1))) $$ = $1;
4320 #endif
4321  $$=$1;
4322  }
4323  | none
4324  ;
4325 
4326 exc_var : tASSOC lhs
4327  {
4328  $$ = $2;
4329  }
4330  | none
4331  ;
4332 
4333 opt_ensure : k_ensure compstmt
4334  {
4335 #if 0
4336  $$ = $2;
4337 #endif
4338  {VALUE v1,v2;v1=$2;v2=dispatch1(ensure,v1);$$=v2;}
4339  }
4340  | none
4341  ;
4342 
4343 literal : numeric
4344  | symbol
4345  ;
4346 
4347 strings : string
4348  {
4349 #if 0
4350  NODE *node = $1;
4351  if (!node) {
4352  node = NEW_STR(STR_NEW0(), &@$);
4353  RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
4354  }
4355  else {
4356  node = evstr2dstr(p, node);
4357  }
4358  $$ = node;
4359 #endif
4360  $$=$1;
4361  }
4362  ;
4363 
4364 string : tCHAR
4365  | string1
4366  | string string1
4367  {
4368 #if 0
4369  $$ = literal_concat(p, $1, $2, &@$);
4370 #endif
4371  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_concat,v1,v2);$$=v3;}
4372  }
4373  ;
4374 
4375 string1 : tSTRING_BEG string_contents tSTRING_END
4376  {
4377 #if 0
4378  $$ = heredoc_dedent(p, $2);
4379  if ($$) nd_set_loc($$, &@$);
4380 #endif
4381  {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(string_literal,v1);$$=v2;}
4382  }
4383  ;
4384 
4385 xstring : tXSTRING_BEG xstring_contents tSTRING_END
4386  {
4387 #if 0
4388  $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4389 #endif
4390  {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(xstring_literal,v1);$$=v2;}
4391  }
4392  ;
4393 
4394 regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4395  {
4396  $$ = new_regexp(p, $2, $3, &@$);
4397  }
4398  ;
4399 
4400 words : tWORDS_BEG ' ' word_list tSTRING_END
4401  {
4402 #if 0
4403  $$ = make_list($3, &@$);
4404 #endif
4405  {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4406  }
4407  ;
4408 
4409 word_list : /* none */
4410  {
4411 #if 0
4412  $$ = 0;
4413 #endif
4414  {VALUE v1;v1=dispatch0(words_new);$$=v1;}
4415  }
4416  | word_list word ' '
4417  {
4418 #if 0
4419  $$ = list_append(p, $1, evstr2dstr(p, $2));
4420 #endif
4421  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(words_add,v1,v2);$$=v3;}
4422  }
4423  ;
4424 
4425 word : string_content
4426  {{VALUE v1,v2,v3,v4;v1=dispatch0(word_new);v2=v1;v3=$1;v4=dispatch2(word_add,v2,v3);$$=v4;}}
4427  | word string_content
4428  {
4429 #if 0
4430  $$ = literal_concat(p, $1, $2, &@$);
4431 #endif
4432  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(word_add,v1,v2);$$=v3;}
4433  }
4434  ;
4435 
4436 symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4437  {
4438 #if 0
4439  $$ = make_list($3, &@$);
4440 #endif
4441  {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4442  }
4443  ;
4444 
4445 symbol_list : /* none */
4446  {
4447 #if 0
4448  $$ = 0;
4449 #endif
4450  {VALUE v1;v1=dispatch0(symbols_new);$$=v1;}
4451  }
4452  | symbol_list word ' '
4453  {
4454 #if 0
4455  $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4456 #endif
4457  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(symbols_add,v1,v2);$$=v3;}
4458  }
4459  ;
4460 
4461 qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4462  {
4463 #if 0
4464  $$ = make_list($3, &@$);
4465 #endif
4466  {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4467  }
4468  ;
4469 
4470 qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4471  {
4472 #if 0
4473  $$ = make_list($3, &@$);
4474 #endif
4475  {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4476  }
4477  ;
4478 
4479 qword_list : /* none */
4480  {
4481 #if 0
4482  $$ = 0;
4483 #endif
4484  {VALUE v1;v1=dispatch0(qwords_new);$$=v1;}
4485  }
4486  | qword_list tSTRING_CONTENT ' '
4487  {
4488 #if 0
4489  $$ = list_append(p, $1, $2);
4490 #endif
4491  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qwords_add,v1,v2);$$=v3;}
4492  }
4493  ;
4494 
4495 qsym_list : /* none */
4496  {
4497 #if 0
4498  $$ = 0;
4499 #endif
4500  {VALUE v1;v1=dispatch0(qsymbols_new);$$=v1;}
4501  }
4502  | qsym_list tSTRING_CONTENT ' '
4503  {
4504 #if 0
4505  $$ = symbol_append(p, $1, $2);
4506 #endif
4507  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qsymbols_add,v1,v2);$$=v3;}
4508  }
4509  ;
4510 
4511 string_contents : /* none */
4512  {
4513 #if 0
4514  $$ = 0;
4515 #endif
4516  {VALUE v1;v1=dispatch0(string_content);$$=v1;}
4517 #if 0
4518 #endif
4519  $$ = ripper_new_yylval(p, 0, $$, 0);
4520 
4521  }
4522  | string_contents string_content
4523  {
4524 #if 0
4525  $$ = literal_concat(p, $1, $2, &@$);
4526 #endif
4527  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_add,v1,v2);$$=v3;}
4528 #if 0
4529 #endif
4530  if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
4531  !RNODE($1)->nd_cval) {
4532  RNODE($1)->nd_cval = RNODE($2)->nd_cval;
4533  RNODE($1)->nd_rval = add_mark_object(p, $$);
4534  $$ = $1;
4535  }
4536 
4537  }
4538  ;
4539 
4540 xstring_contents: /* none */
4541  {
4542 #if 0
4543  $$ = 0;
4544 #endif
4545  {VALUE v1;v1=dispatch0(xstring_new);$$=v1;}
4546  }
4547  | xstring_contents string_content
4548  {
4549 #if 0
4550  $$ = literal_concat(p, $1, $2, &@$);
4551 #endif
4552  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(xstring_add,v1,v2);$$=v3;}
4553  }
4554  ;
4555 
4556 regexp_contents: /* none */
4557  {
4558 #if 0
4559  $$ = 0;
4560 #endif
4561  {VALUE v1;v1=dispatch0(regexp_new);$$=v1;}
4562 #if 0
4563 #endif
4564  $$ = ripper_new_yylval(p, 0, $$, 0);
4565 
4566  }
4567  | regexp_contents string_content
4568  {
4569 #if 0
4570  NODE *head = $1, *tail = $2;
4571  if (!head) {
4572  $$ = tail;
4573  }
4574  else if (!tail) {
4575  $$ = head;
4576  }
4577  else {
4578  switch (nd_type(head)) {
4579  case NODE_STR:
4580  nd_set_type(head, NODE_DSTR);
4581  break;
4582  case NODE_DSTR:
4583  break;
4584  default:
4585  head = list_append(p, NEW_DSTR(Qnil, &@$), head);
4586  break;
4587  }
4588  $$ = list_append(p, head, tail);
4589  }
4590 #endif
4591  VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4592  if (ripper_is_node_yylval(n1)) {
4593  s1 = RNODE(n1)->nd_cval;
4594  n1 = RNODE(n1)->nd_rval;
4595  }
4596  if (ripper_is_node_yylval(n2)) {
4597  s2 = RNODE(n2)->nd_cval;
4598  n2 = RNODE(n2)->nd_rval;
4599  }
4600  $$ = dispatch2(regexp_add, n1, n2);
4601  if (!s1 && s2) {
4602  $$ = ripper_new_yylval(p, 0, $$, s2);
4603  }
4604 
4605  }
4606  ;
4607 
4608 string_content : tSTRING_CONTENT
4609  {$$=ripper_new_yylval(p, 0, get_value($1), $1);}
4610  | tSTRING_DVAR
4611  {
4612  /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
4613  $<strterm>$ = p->lex.strterm;
4614  p->lex.strterm = 0;
4615  SET_LEX_STATE(EXPR_BEG);
4616  }
4617  string_dvar
4618  {
4619  p->lex.strterm = $<strterm>2;
4620 #if 0
4621  $$ = NEW_EVSTR($3, &@$);
4622  nd_set_line($$, @3.end_pos.lineno);
4623 #endif
4624  {VALUE v1,v2;v1=$3;v2=dispatch1(string_dvar,v1);$$=v2;}
4625  }
4626  | tSTRING_DBEG
4627  {
4628  CMDARG_PUSH(0);
4629  COND_PUSH(0);
4630  }
4631  {
4632  /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
4633  $<strterm>$ = p->lex.strterm;
4634  p->lex.strterm = 0;
4635  }
4636  {
4637  $<num>$ = p->lex.state;
4638  SET_LEX_STATE(EXPR_BEG);
4639  }
4640  {
4641  $<num>$ = p->lex.brace_nest;
4642  p->lex.brace_nest = 0;
4643  }
4644  {
4645  $<num>$ = p->heredoc_indent;
4646  p->heredoc_indent = 0;
4647  }
4648  compstmt tSTRING_DEND
4649  {
4650  COND_POP();
4651  CMDARG_POP();
4652  p->lex.strterm = $<strterm>3;
4653  SET_LEX_STATE($<num>4);
4654  p->lex.brace_nest = $<num>5;
4655  p->heredoc_indent = $<num>6;
4656  p->heredoc_line_indent = -1;
4657 #if 0
4658  if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4659  $$ = new_evstr(p, $7, &@$);
4660 #endif
4661  {VALUE v1,v2;v1=$7;v2=dispatch1(string_embexpr,v1);$$=v2;}
4662  }
4663  ;
4664 
4665 string_dvar : tGVAR
4666  {
4667 #if 0
4668  $$ = NEW_GVAR($1, &@$);
4669 #endif
4670  {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4671  }
4672  | tIVAR
4673  {
4674 #if 0
4675  $$ = NEW_IVAR($1, &@$);
4676 #endif
4677  {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4678  }
4679  | tCVAR
4680  {
4681 #if 0
4682  $$ = NEW_CVAR($1, &@$);
4683 #endif
4684  {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4685  }
4686  | backref
4687  ;
4688 
4689 symbol : ssym
4690  | dsym
4691  ;
4692 
4693 ssym : tSYMBEG sym
4694  {
4695  SET_LEX_STATE(EXPR_END);
4696 #if 0
4697  $$ = NEW_LIT(ID2SYM($2), &@$);
4698 #endif
4699  {VALUE v1,v2,v3,v4;v1=$2;v2=dispatch1(symbol,v1);v3=v2;v4=dispatch1(symbol_literal,v3);$$=v4;}
4700  }
4701  ;
4702 
4703 sym : fname
4704  | tIVAR
4705  | tGVAR
4706  | tCVAR
4707  ;
4708 
4709 dsym : tSYMBEG string_contents tSTRING_END
4710  {
4711  SET_LEX_STATE(EXPR_END);
4712 #if 0
4713  $$ = dsym_node(p, $2, &@$);
4714 #endif
4715  {VALUE v1,v2;v1=$2;v2=dispatch1(dyna_symbol,v1);$$=v2;}
4716  }
4717  ;
4718 
4719 numeric : simple_numeric
4720  | tUMINUS_NUM simple_numeric %prec tLOWEST
4721  {
4722 #if 0
4723  $$ = $2;
4724  RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
4725 #endif
4726  {VALUE v1,v2,v3;v1=ID2VAL(idUMinus);v2=$2;v3=dispatch2(unary,v1,v2);$$=v3;}
4727  }
4728  ;
4729 
4730 simple_numeric : tINTEGER
4731  | tFLOAT
4732  | tRATIONAL
4733  | tIMAGINARY
4734  ;
4735 
4736 user_variable : tIDENTIFIER
4737  | tIVAR
4738  | tGVAR
4739  | tCONSTANT
4740  | tCVAR
4741  ;
4742 
4743 keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
4744  | keyword_self {$$ = KWD2EID(self, $1);}
4745  | keyword_true {$$ = KWD2EID(true, $1);}
4746  | keyword_false {$$ = KWD2EID(false, $1);}
4747  | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
4748  | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
4749  | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
4750  ;
4751 
4752 var_ref : user_variable
4753  {
4754 #if 0
4755  if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4756 #endif
4757  if (id_is_var(p, get_id($1))) {
4758  $$ = dispatch1(var_ref, $1);
4759  }
4760  else {
4761  $$ = dispatch1(vcall, $1);
4762  }
4763 
4764  }
4765  | keyword_variable
4766  {
4767 #if 0
4768  if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4769 #endif
4770  {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4771  }
4772  ;
4773 
4774 var_lhs : user_variable
4775  {
4776 #if 0
4777  $$ = assignable(p, $1, 0, &@$);
4778 #endif
4779  $$=assignable(p, var_field(p, $1));
4780  }
4781  | keyword_variable
4782  {
4783 #if 0
4784  $$ = assignable(p, $1, 0, &@$);
4785 #endif
4786  $$=assignable(p, var_field(p, $1));
4787  }
4788  ;
4789 
4790 backref : tNTH_REF
4791  | tBACK_REF
4792  ;
4793 
4794 superclass : '<'
4795  {
4796  SET_LEX_STATE(EXPR_BEG);
4797  p->command_start = TRUE;
4798  }
4799  expr_value term
4800  {
4801  $$ = $3;
4802  }
4803  | /* none */
4804  {
4805 #if 0
4806  $$ = 0;
4807 #endif
4808  $$=Qnil;
4809  }
4810  ;
4811 
4812 f_arglist : '(' f_args rparen
4813  {
4814 #if 0
4815  $$ = $2;
4816 #endif
4817  {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
4818  SET_LEX_STATE(EXPR_BEG);
4819  p->command_start = TRUE;
4820  }
4821  | '(' args_forward rparen
4822  {
4823  arg_var(p, idFWD_REST);
4824 #if idFWD_KWREST
4825  arg_var(p, idFWD_KWREST);
4826 #endif
4827  arg_var(p, idFWD_BLOCK);
4828 #if 0
4829  $$ = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, &@2);
4830  $$ = new_args(p, Qnone, Qnone, idFWD_REST, Qnone, $$, &@2);
4831 #endif
4832  {VALUE v1,v2;v1=params_new(Qnone, Qnone, $2, Qnone, Qnone, Qnone, Qnone);v2=dispatch1(paren,v1);$$=v2;}
4833  SET_LEX_STATE(EXPR_BEG);
4834  p->command_start = TRUE;
4835  }
4836  | {
4837  $<num>$ = p->in_kwarg;
4838  p->in_kwarg = 1;
4839  SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
4840  }
4841  f_args term
4842  {
4843  p->in_kwarg = !!$<num>1;
4844  $$ = $2;
4845  SET_LEX_STATE(EXPR_BEG);
4846  p->command_start = TRUE;
4847  }
4848  ;
4849 
4850 args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
4851  {
4852  $$ = new_args_tail(p, $1, $3, $4, &@3);
4853  }
4854  | f_kwarg opt_f_block_arg
4855  {
4856  $$ = new_args_tail(p, $1, Qnone, $2, &@1);
4857  }
4858  | f_kwrest opt_f_block_arg
4859  {
4860  $$ = new_args_tail(p, Qnone, $1, $2, &@1);
4861  }
4862  | f_no_kwarg opt_f_block_arg
4863  {
4864  $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
4865  }
4866  | f_block_arg
4867  {
4868  $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
4869  }
4870  ;
4871 
4872 opt_args_tail : ',' args_tail
4873  {
4874  $$ = $2;
4875  }
4876  | /* none */
4877  {
4878  $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4879  }
4880  ;
4881 
4882 f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
4883  {
4884  $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
4885  }
4886  | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4887  {
4888  $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4889  }
4890  | f_arg ',' f_optarg opt_args_tail
4891  {
4892  $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
4893  }
4894  | f_arg ',' f_optarg ',' f_arg opt_args_tail
4895  {
4896  $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4897  }
4898  | f_arg ',' f_rest_arg opt_args_tail
4899  {
4900  $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4901  }
4902  | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
4903  {
4904  $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4905  }
4906  | f_arg opt_args_tail
4907  {
4908  $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4909  }
4910  | f_optarg ',' f_rest_arg opt_args_tail
4911  {
4912  $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4913  }
4914  | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4915  {
4916  $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4917  }
4918  | f_optarg opt_args_tail
4919  {
4920  $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4921  }
4922  | f_optarg ',' f_arg opt_args_tail
4923  {
4924  $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4925  }
4926  | f_rest_arg opt_args_tail
4927  {
4928  $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4929  }
4930  | f_rest_arg ',' f_arg opt_args_tail
4931  {
4932  $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4933  }
4934  | args_tail
4935  {
4936  $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4937  }
4938  | /* none */
4939  {
4940  $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4941  $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
4942  }
4943  ;
4944 
4945 args_forward : tBDOT3
4946  {
4947 #if 0
4948  $$ = idDot3;
4949 #endif
4950  {VALUE v1;v1=dispatch0(args_forward);$$=v1;}
4951  }
4952  ;
4953 
4954 f_bad_arg : tCONSTANT
4955  {
4956 #if 0
4957  yyerror1(&@1, "formal argument cannot be a constant");
4958  $$ = 0;
4959 #endif
4960  {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4961  }
4962  | tIVAR
4963  {
4964 #if 0
4965  yyerror1(&@1, "formal argument cannot be an instance variable");
4966  $$ = 0;
4967 #endif
4968  {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4969  }
4970  | tGVAR
4971  {
4972 #if 0
4973  yyerror1(&@1, "formal argument cannot be a global variable");
4974  $$ = 0;
4975 #endif
4976  {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4977  }
4978  | tCVAR
4979  {
4980 #if 0
4981  yyerror1(&@1, "formal argument cannot be a class variable");
4982  $$ = 0;
4983 #endif
4984  {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4985  }
4986  ;
4987 
4988 f_norm_arg : f_bad_arg
4989  | tIDENTIFIER
4990  {
4991  formal_argument(p, get_id($1));
4992  p->max_numparam = ORDINAL_PARAM;
4993  $$ = $1;
4994  }
4995  ;
4996 
4997 f_arg_asgn : f_norm_arg
4998  {
4999  ID id = get_id($1);
5000  arg_var(p, id);
5001  p->cur_arg = id;
5002  $$ = $1;
5003  }
5004  ;
5005 
5006 f_arg_item : f_arg_asgn
5007  {
5008  p->cur_arg = 0;
5009 #if 0
5010  $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5011 #endif
5012  $$=get_value($1);
5013  }
5014  | tLPAREN f_margs rparen
5015  {
5016 #if 0
5017  ID tid = internal_id(p);
5018  YYLTYPE loc;
5019  loc.beg_pos = @2.beg_pos;
5020  loc.end_pos = @2.beg_pos;
5021  arg_var(p, tid);
5022  if (dyna_in_block(p)) {
5023  $2->nd_value = NEW_DVAR(tid, &loc);
5024  }
5025  else {
5026  $2->nd_value = NEW_LVAR(tid, &loc);
5027  }
5028  $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5029  $$->nd_next = $2;
5030 #endif
5031  {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
5032  }
5033  ;
5034 
5035 f_arg : f_arg_item
5036  {$$=rb_ary_new3(1, get_value($1));}
5037  | f_arg ',' f_arg_item
5038  {
5039 #if 0
5040  $$ = $1;
5041  $$->nd_plen++;
5042  $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5043  rb_discard_node(p, $3);
5044 #endif
5045  $$=rb_ary_push($1, get_value($3));
5046  }
5047  ;
5048 
5049 
5050 f_label : tLABEL
5051  {
5052  ID id = get_id($1);
5053  arg_var(p, formal_argument(p, id));
5054  p->cur_arg = id;
5055  p->max_numparam = ORDINAL_PARAM;
5056  $$ = $1;
5057  }
5058  ;
5059 
5060 f_kw : f_label arg_value
5061  {
5062  p->cur_arg = 0;
5063 #if 0
5064  $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5065 #endif
5066  $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5067  }
5068  | f_label
5069  {
5070  p->cur_arg = 0;
5071 #if 0
5072  $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5073 #endif
5074  $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5075  }
5076  ;
5077 
5078 f_block_kw : f_label primary_value
5079  {
5080 #if 0
5081  $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5082 #endif
5083  $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5084  }
5085  | f_label
5086  {
5087 #if 0
5088  $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5089 #endif
5090  $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5091  }
5092  ;
5093 
5094 f_block_kwarg : f_block_kw
5095  {
5096 #if 0
5097  $$ = $1;
5098 #endif
5099  $$=rb_ary_new3(1, get_value($1));
5100  }
5101  | f_block_kwarg ',' f_block_kw
5102  {
5103 #if 0
5104  $$ = kwd_append($1, $3);
5105 #endif
5106  $$=rb_ary_push($1, get_value($3));
5107  }
5108  ;
5109 
5110 
5111 f_kwarg : f_kw
5112  {
5113 #if 0
5114  $$ = $1;
5115 #endif
5116  $$=rb_ary_new3(1, get_value($1));
5117  }
5118  | f_kwarg ',' f_kw
5119  {
5120 #if 0
5121  $$ = kwd_append($1, $3);
5122 #endif
5123  $$=rb_ary_push($1, get_value($3));
5124  }
5125  ;
5126 
5127 kwrest_mark : tPOW
5128  | tDSTAR
5129  ;
5130 
5131 f_no_kwarg : kwrest_mark keyword_nil
5132  {
5133 #if 0
5134 #endif
5135  {VALUE v1,v2;v1=Qnil;v2=dispatch1(nokw_param,v1);$$=v2;}
5136  }
5137  ;
5138 
5139 f_kwrest : kwrest_mark tIDENTIFIER
5140  {
5141  arg_var(p, shadowing_lvar(p, get_id($2)));
5142 #if 0
5143  $$ = $2;
5144 #endif
5145  {VALUE v1,v2;v1=$2;v2=dispatch1(kwrest_param,v1);$$=v2;}
5146  }
5147  | kwrest_mark
5148  {
5149 #if 0
5150  $$ = internal_id(p);
5151  arg_var(p, $$);
5152 #endif
5153  {VALUE v1,v2;v1=Qnil;v2=dispatch1(kwrest_param,v1);$$=v2;}
5154  }
5155  ;
5156 
5157 f_opt : f_arg_asgn '=' arg_value
5158  {
5159  p->cur_arg = 0;
5160 #if 0
5161  $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5162 #endif
5163  $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5164  }
5165  ;
5166 
5167 f_block_opt : f_arg_asgn '=' primary_value
5168  {
5169  p->cur_arg = 0;
5170 #if 0
5171  $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5172 #endif
5173  $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5174  }
5175  ;
5176 
5177 f_block_optarg : f_block_opt
5178  {
5179 #if 0
5180  $$ = $1;
5181 #endif
5182  $$=rb_ary_new3(1, get_value($1));
5183  }
5184  | f_block_optarg ',' f_block_opt
5185  {
5186 #if 0
5187  $$ = opt_arg_append($1, $3);
5188 #endif
5189  $$=rb_ary_push($1, get_value($3));
5190  }
5191  ;
5192 
5193 f_optarg : f_opt
5194  {
5195 #if 0
5196  $$ = $1;
5197 #endif
5198  $$=rb_ary_new3(1, get_value($1));
5199  }
5200  | f_optarg ',' f_opt
5201  {
5202 #if 0
5203  $$ = opt_arg_append($1, $3);
5204 #endif
5205  $$=rb_ary_push($1, get_value($3));
5206  }
5207  ;
5208 
5209 restarg_mark : '*'
5210  | tSTAR
5211  ;
5212 
5213 f_rest_arg : restarg_mark tIDENTIFIER
5214  {
5215  arg_var(p, shadowing_lvar(p, get_id($2)));
5216 #if 0
5217  $$ = $2;
5218 #endif
5219  {VALUE v1,v2;v1=$2;v2=dispatch1(rest_param,v1);$$=v2;}
5220  }
5221  | restarg_mark
5222  {
5223 #if 0
5224  $$ = internal_id(p);
5225  arg_var(p, $$);
5226 #endif
5227  {VALUE v1,v2;v1=Qnil;v2=dispatch1(rest_param,v1);$$=v2;}
5228  }
5229  ;
5230 
5231 blkarg_mark : '&'
5232  | tAMPER
5233  ;
5234 
5235 f_block_arg : blkarg_mark tIDENTIFIER
5236  {
5237  arg_var(p, shadowing_lvar(p, get_id($2)));
5238 #if 0
5239  $$ = $2;
5240 #endif
5241  {VALUE v1,v2;v1=$2;v2=dispatch1(blockarg,v1);$$=v2;}
5242  }
5243  ;
5244 
5245 opt_f_block_arg : ',' f_block_arg
5246  {
5247  $$ = $2;
5248  }
5249  | none
5250  {
5251  $$ = Qnull;
5252  }
5253  ;
5254 
5255 singleton : var_ref
5256  {
5257  value_expr($1);
5258  $$ = $1;
5259  }
5260  | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5261  {
5262 #if 0
5263  switch (nd_type($3)) {
5264  case NODE_STR:
5265  case NODE_DSTR:
5266  case NODE_XSTR:
5267  case NODE_DXSTR:
5268  case NODE_DREGX:
5269  case NODE_LIT:
5270  case NODE_LIST:
5271  case NODE_ZLIST:
5272  yyerror1(&@3, "can't define singleton method for literals");
5273  break;
5274  default:
5275  value_expr($3);
5276  break;
5277  }
5278  $$ = $3;
5279 #endif
5280  {VALUE v1,v2;v1=$3;v2=dispatch1(paren,v1);$$=v2;}
5281  }
5282  ;
5283 
5284 assoc_list : none
5285  | assocs trailer
5286  {
5287 #if 0
5288  $$ = $1;
5289 #endif
5290  {VALUE v1,v2;v1=$1;v2=dispatch1(assoclist_from_args,v1);$$=v2;}
5291  }
5292  ;
5293 
5294 assocs : assoc
5295  {$$=rb_ary_new3(1, get_value($1));}
5296  | assocs ',' assoc
5297  {
5298 #if 0
5299  NODE *assocs = $1;
5300  NODE *tail = $3;
5301  if (!assocs) {
5302  assocs = tail;
5303  }
5304  else if (tail) {
5305  if (assocs->nd_head &&
5306  !tail->nd_head && nd_type(tail->nd_next) == NODE_LIST &&
5307  nd_type(tail->nd_next->nd_head) == NODE_HASH) {
5308  /* DSTAR */
5309  tail = tail->nd_next->nd_head->nd_head;
5310  }
5311  assocs = list_concat(assocs, tail);
5312  }
5313  $$ = assocs;
5314 #endif
5315  $$=rb_ary_push($1, get_value($3));
5316  }
5317  ;
5318 
5319 assoc : arg_value tASSOC arg_value
5320  {
5321 #if 0
5322  if (nd_type($1) == NODE_STR) {
5323  nd_set_type($1, NODE_LIT);
5324  RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
5325  }
5326  $$ = list_append(p, NEW_LIST($1, &@$), $3);
5327 #endif
5328  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5329  }
5330  | tLABEL arg_value
5331  {
5332 #if 0
5333  $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5334 #endif
5335  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5336  }
5337  | tSTRING_BEG string_contents tLABEL_END arg_value
5338  {
5339 #if 0
5340  YYLTYPE loc = code_loc_gen(&@1, &@3);
5341  $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
5342 #endif
5343  {VALUE v1,v2,v3,v4,v5;v1=$2;v2=dispatch1(dyna_symbol,v1);v3=v2;v4=$4;v5=dispatch2(assoc_new,v3,v4);$$=v5;}
5344  }
5345  | tDSTAR arg_value
5346  {
5347 #if 0
5348  if (nd_type($2) == NODE_HASH &&
5349  !($2->nd_head && $2->nd_head->nd_alen)) {
5350  static VALUE empty_hash;
5351  if (!empty_hash) {
5352  empty_hash = rb_obj_freeze(rb_hash_new());
5353  rb_gc_register_mark_object(empty_hash);
5354  }
5355  $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
5356  }
5357  else
5358  $$ = list_append(p, NEW_LIST(0, &@$), $2);
5359 #endif
5360  {VALUE v1,v2;v1=$2;v2=dispatch1(assoc_splat,v1);$$=v2;}
5361  }
5362  ;
5363 
5364 operation : tIDENTIFIER
5365  | tCONSTANT
5366  | tFID
5367  ;
5368 
5369 operation2 : tIDENTIFIER
5370  | tCONSTANT
5371  | tFID
5372  | op
5373  ;
5374 
5375 operation3 : tIDENTIFIER
5376  | tFID
5377  | op
5378  ;
5379 
5380 dot_or_colon : '.'
5381  | tCOLON2
5382  ;
5383 
5384 call_op : '.'
5385  | tANDDOT
5386  ;
5387 
5388 call_op2 : call_op
5389  | tCOLON2
5390  ;
5391 
5392 opt_terms : /* none */
5393  | terms
5394  ;
5395 
5396 opt_nl : /* none */
5397  | '\n'
5398  ;
5399 
5400 rparen : opt_nl ')'
5401  ;
5402 
5403 rbracket : opt_nl ']'
5404  ;
5405 
5406 rbrace : opt_nl '}'
5407  ;
5408 
5409 trailer : /* none */
5410  | '\n'
5411  | ','
5412  ;
5413 
5414 term : ';' {yyerrok;token_flush(p);}
5415  | '\n' {token_flush(p);}
5416  ;
5417 
5418 terms : term
5419  | terms ';' {yyerrok;}
5420  ;
5421 
5422 none : /* none */
5423  {
5424  $$ = Qnull;
5425  }
5426  ;
5427 %%
5428 # undef p
5429 # undef yylex
5430 # undef yylval
5431 # define yylval (*p->lval)
5432 
5433 static int regx_options(struct parser_params*);
5434 static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5435 static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5436 static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5437 static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
5438 
5439 #ifndef RIPPER
5440 # define set_yylval_node(x) { \
5441  YYLTYPE _cur_loc; \
5442  rb_parser_set_location(p, &_cur_loc); \
5443  yylval.node = (x); \
5444 }
5445 # define set_yylval_str(x) \
5446 do { \
5447  set_yylval_node(NEW_STR(x, &_cur_loc)); \
5448  RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5449 } while(0)
5450 # define set_yylval_literal(x) \
5451 do { \
5452  set_yylval_node(NEW_LIT(x, &_cur_loc)); \
5453  RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5454 } while(0)
5455 # define set_yylval_num(x) (yylval.num = (x))
5456 # define set_yylval_id(x) (yylval.id = (x))
5457 # define set_yylval_name(x) (yylval.id = (x))
5458 # define yylval_id() (yylval.id)
5459 #else
5460 static inline VALUE
5461 ripper_yylval_id(struct parser_params *p, ID x)
5462 {
5463  return ripper_new_yylval(p, x, ID2SYM(x), 0);
5464 }
5465 # define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
5466 # define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
5467 # define set_yylval_id(x) (void)(x)
5468 # define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
5469 # define set_yylval_literal(x) add_mark_object(p, (x))
5470 # define set_yylval_node(x) (void)(x)
5471 # define yylval_id() yylval.id
5472 # define _cur_loc NULL_LOC /* dummy */
5473 #endif
5474 
5475 #define set_yylval_noname() set_yylval_id(keyword_nil)
5476 
5477 #ifndef RIPPER
5478 #define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
5479 #define dispatch_scan_event(p, t) ((void)0)
5480 #define dispatch_delayed_token(p, t) ((void)0)
5481 #define has_delayed_token(p) (0)
5482 #else
5483 #define literal_flush(p, ptr) ((void)(ptr))
5484 
5485 #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5486 
5487 static inline VALUE
5488 intern_sym(const char *name)
5489 {
5490  ID id = rb_intern_const(name);
5491  return ID2SYM(id);
5492 }
5493 
5494 static int
5495 ripper_has_scan_event(struct parser_params *p)
5496 {
5497  if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
5498  return p->lex.pcur > p->lex.ptok;
5499 }
5500 
5501 static VALUE
5502 ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
5503 {
5504  VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
5505  VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
5506  token_flush(p);
5507  return rval;
5508 }
5509 
5510 static void
5511 ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
5512 {
5513  if (!ripper_has_scan_event(p)) return;
5514  add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
5515 }
5516 #define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
5517 
5518 static void
5519 ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
5520 {
5521  int saved_line = p->ruby_sourceline;
5522  const char *saved_tokp = p->lex.ptok;
5523 
5524  if (NIL_P(p->delayed.token)) return;
5525  p->ruby_sourceline = p->delayed.line;
5526  p->lex.ptok = p->lex.pbeg + p->delayed.col;
5527  add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
5528  p->delayed.token = Qnil;
5529  p->ruby_sourceline = saved_line;
5530  p->lex.ptok = saved_tokp;
5531 }
5532 #define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
5533 #define has_delayed_token(p) (!NIL_P(p->delayed.token))
5534 #endif /* RIPPER */
5535 
5536 #include "ruby/regex.h"
5537 #include "ruby/util.h"
5538 
5539 static inline int
5540 is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
5541 {
5542  return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
5543 }
5544 
5545 static inline int
5546 parser_is_identchar(struct parser_params *p)
5547 {
5548  return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5549 }
5550 
5551 static inline int
5552 parser_isascii(struct parser_params *p)
5553 {
5554  return ISASCII(*(p->lex.pcur-1));
5555 }
5556 
5557 static void
5558 token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
5559 {
5560  int column = 1, nonspc = 0, i;
5561  for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
5562  if (*ptr == '\t') {
5563  column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5564  }
5565  column++;
5566  if (*ptr != ' ' && *ptr != '\t') {
5567  nonspc = 1;
5568  }
5569  }
5570 
5571  ptinfo->beg = loc->beg_pos;
5572  ptinfo->indent = column;
5573  ptinfo->nonspc = nonspc;
5574 }
5575 
5576 static void
5577 token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5578 {
5579  token_info *ptinfo;
5580 
5581  if (!p->token_info_enabled) return;
5582  ptinfo = ALLOC(token_info);
5583  ptinfo->token = token;
5584  ptinfo->next = p->token_info;
5585  token_info_setup(ptinfo, p->lex.pbeg, loc);
5586 
5587  p->token_info = ptinfo;
5588 }
5589 
5590 static void
5591 token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5592 {
5593  token_info *ptinfo_beg = p->token_info;
5594 
5595  if (!ptinfo_beg) return;
5596  p->token_info = ptinfo_beg->next;
5597 
5598  /* indentation check of matched keywords (begin..end, if..end, etc.) */
5599  token_info_warn(p, token, ptinfo_beg, 1, loc);
5600  ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5601 }
5602 
5603 static void
5604 token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
5605 {
5606  token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
5607  if (!p->token_info_enabled) return;
5608  if (!ptinfo_beg) return;
5609  token_info_setup(ptinfo_end, p->lex.pbeg, loc);
5610  if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
5611  if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
5612  if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
5613  if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
5614  rb_warn3L(ptinfo_end->beg.lineno,
5615  "mismatched indentations at '%s' with '%s' at %d",
5616  WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
5617 }
5618 
5619 static int
5620 parser_precise_mbclen(struct parser_params *p, const char *ptr)
5621 {
5622  int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
5623  if (!MBCLEN_CHARFOUND_P(len)) {
5624  compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
5625  return -1;
5626  }
5627  return len;
5628 }
5629 
5630 #ifndef RIPPER
5631 static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
5632 
5633 static inline void
5634 parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5635 {
5636  VALUE str;
5637  int lineno = p->ruby_sourceline;
5638  if (!yylloc) {
5639  return;
5640  }
5641  else if (yylloc->beg_pos.lineno == lineno) {
5642  str = p->lex.lastline;
5643  }
5644  else {
5645  return;
5646  }
5647  ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
5648 }
5649 
5650 static int
5651 parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5652 {
5653  YYLTYPE current;
5654 
5655  if (!yylloc) {
5656  yylloc = RUBY_SET_YYLLOC(current);
5657  }
5658  else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
5659  p->ruby_sourceline != yylloc->end_pos.lineno) ||
5660  (yylloc->beg_pos.lineno == yylloc->end_pos.lineno &&
5661  yylloc->beg_pos.column == yylloc->end_pos.column)) {
5662  yylloc = 0;
5663  }
5664  compile_error(p, "%s", msg);
5665  parser_show_error_line(p, yylloc);
5666  return 0;
5667 }
5668 
5669 static void
5670 ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
5671 {
5672  VALUE mesg;
5673  const int max_line_margin = 30;
5674  const char *ptr, *ptr_end, *pt, *pb;
5675  const char *pre = "", *post = "", *pend;
5676  const char *code = "", *caret = "";
5677  const char *lim;
5678  const char *const pbeg = RSTRING_PTR(str);
5679  char *buf;
5680  long len;
5681  int i;
5682 
5683  if (!yylloc) return;
5684  pend = RSTRING_END(str);
5685  if (pend > pbeg && pend[-1] == '\n') {
5686  if (--pend > pbeg && pend[-1] == '\r') --pend;
5687  }
5688 
5689  pt = pend;
5690  if (lineno == yylloc->end_pos.lineno &&
5691  (pend - pbeg) > yylloc->end_pos.column) {
5692  pt = pbeg + yylloc->end_pos.column;
5693  }
5694 
5695  ptr = ptr_end = pt;
5696  lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
5697  while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
5698 
5699  lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
5700  while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
5701 
5702  len = ptr_end - ptr;
5703  if (len > 4) {
5704  if (ptr > pbeg) {
5705  ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
5706  if (ptr > pbeg) pre = "...";
5707  }
5708  if (ptr_end < pend) {
5709  ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
5710  if (ptr_end < pend) post = "...";
5711  }
5712  }
5713  pb = pbeg;
5714  if (lineno == yylloc->beg_pos.lineno) {
5715  pb += yylloc->beg_pos.column;
5716  if (pb > pt) pb = pt;
5717  }
5718  if (pb < ptr) pb = ptr;
5719  if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
5720  return;
5721  }
5722  if (RTEST(errbuf)) {
5723  mesg = rb_attr_get(errbuf, idMesg);
5724  if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
5725  rb_str_cat_cstr(mesg, "\n");
5726  }
5727  else {
5728  mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
5729  }
5730  if (!errbuf && rb_stderr_tty_p()) {
5731 #define CSI_BEGIN "\033["
5732 #define CSI_SGR "m"
5733  rb_str_catf(mesg,
5734  CSI_BEGIN""CSI_SGR"%s" /* pre */
5735  CSI_BEGIN"1"CSI_SGR"%.*s"
5736  CSI_BEGIN"1;4"CSI_SGR"%.*s"
5737  CSI_BEGIN";1"CSI_SGR"%.*s"
5738  CSI_BEGIN""CSI_SGR"%s" /* post */
5739  "\n",
5740  pre,
5741  (int)(pb - ptr), ptr,
5742  (int)(pt - pb), pb,
5743  (int)(ptr_end - pt), pt,
5744  post);
5745  }
5746  else {
5747  char *p2;
5748 
5749  len = ptr_end - ptr;
5750  lim = pt < pend ? pt : pend;
5751  i = (int)(lim - ptr);
5752  buf = ALLOCA_N(char, i+2);
5753  code = ptr;
5754  caret = p2 = buf;
5755  if (ptr <= pb) {
5756  while (ptr < pb) {
5757  *p2++ = *ptr++ == '\t' ? '\t' : ' ';
5758  }
5759  *p2++ = '^';
5760  ptr++;
5761  }
5762  if (lim > ptr) {
5763  memset(p2, '~', (lim - ptr));
5764  p2 += (lim - ptr);
5765  }
5766  *p2 = '\0';
5767  rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
5768  pre, (int)len, code, post,
5769  pre, caret);
5770  }
5771  if (!errbuf) rb_write_error_str(mesg);
5772 }
5773 #else
5774 static int
5775 parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5776 {
5777  const char *pcur = 0, *ptok = 0;
5778  if (yylloc &&
5779  p->ruby_sourceline == yylloc->beg_pos.lineno &&
5780  p->ruby_sourceline == yylloc->end_pos.lineno) {
5781  pcur = p->lex.pcur;
5782  ptok = p->lex.ptok;
5783  p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
5784  p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
5785  }
5786  dispatch1(parse_error, STR_NEW2(msg));
5787  ripper_error(p);
5788  if (pcur) {
5789  p->lex.ptok = ptok;
5790  p->lex.pcur = pcur;
5791  }
5792  return 0;
5793 }
5794 
5795 static inline void
5796 parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5797 {
5798 }
5799 #endif /* !RIPPER */
5800 
5801 #ifndef RIPPER
5802 static int
5803 vtable_size(const struct vtable *tbl)
5804 {
5805  if (!DVARS_TERMINAL_P(tbl)) {
5806  return tbl->pos;
5807  }
5808  else {
5809  return 0;
5810  }
5811 }
5812 #endif
5813 
5814 static struct vtable *
5815 vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
5816 {
5817  struct vtable *tbl = ALLOC(struct vtable);
5818  tbl->pos = 0;
5819  tbl->capa = 8;
5820  tbl->tbl = ALLOC_N(ID, tbl->capa);
5821  tbl->prev = prev;
5822 #ifndef RIPPER
5823  if (p->debug) {
5824  rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
5825  }
5826 #endif
5827  return tbl;
5828 }
5829 #define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
5830 
5831 static void
5832 vtable_free_gen(struct parser_params *p, int line, const char *name,
5833  struct vtable *tbl)
5834 {
5835 #ifndef RIPPER
5836  if (p->debug) {
5837  rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
5838  }
5839 #endif
5840  if (!DVARS_TERMINAL_P(tbl)) {
5841  if (tbl->tbl) {
5842  ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
5843  }
5844  ruby_sized_xfree(tbl, sizeof(tbl));
5845  }
5846 }
5847 #define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
5848 
5849 static void
5850 vtable_add_gen(struct parser_params *p, int line, const char *name,
5851  struct vtable *tbl, ID id)
5852 {
5853 #ifndef RIPPER
5854  if (p->debug) {
5855  rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
5856  line, name, (void *)tbl, rb_id2name(id));
5857  }
5858 #endif
5859  if (DVARS_TERMINAL_P(tbl)) {
5860  rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
5861  return;
5862  }
5863  if (tbl->pos == tbl->capa) {
5864  tbl->capa = tbl->capa * 2;
5865  SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
5866  }
5867  tbl->tbl[tbl->pos++] = id;
5868 }
5869 #define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
5870 
5871 #ifndef RIPPER
5872 static void
5873 vtable_pop_gen(struct parser_params *p, int line, const char *name,
5874  struct vtable *tbl, int n)
5875 {
5876  if (p->debug) {
5877  rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
5878  line, name, (void *)tbl, n);
5879  }
5880  if (tbl->pos < n) {
5881  rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
5882  return;
5883  }
5884  tbl->pos -= n;
5885 }
5886 #define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
5887 #endif
5888 
5889 static int
5890 vtable_included(const struct vtable * tbl, ID id)
5891 {
5892  int i;
5893 
5894  if (!DVARS_TERMINAL_P(tbl)) {
5895  for (i = 0; i < tbl->pos; i++) {
5896  if (tbl->tbl[i] == id) {
5897  return i+1;
5898  }
5899  }
5900  }
5901  return 0;
5902 }
5903 
5904 static void parser_prepare(struct parser_params *p);
5905 
5906 #ifndef RIPPER
5907 static NODE *parser_append_options(struct parser_params *p, NODE *node);
5908 
5909 static VALUE
5910 debug_lines(VALUE fname)
5911 {
5912  ID script_lines;
5913  CONST_ID(script_lines, "SCRIPT_LINES__");
5914  if (rb_const_defined_at(rb_cObject, script_lines)) {
5915  VALUE hash = rb_const_get_at(rb_cObject, script_lines);
5916  if (RB_TYPE_P(hash, T_HASH)) {
5917  VALUE lines = rb_ary_new();
5918  rb_hash_aset(hash, fname, lines);
5919  return lines;
5920  }
5921  }
5922  return 0;
5923 }
5924 
5925 static int
5926 e_option_supplied(struct parser_params *p)
5927 {
5928  return strcmp(p->ruby_sourcefile, "-e") == 0;
5929 }
5930 
5931 static VALUE
5932 yycompile0(VALUE arg)
5933 {
5934  int n;
5935  NODE *tree;
5936  struct parser_params *p = (struct parser_params *)arg;
5937  VALUE cov = Qfalse;
5938 
5939  if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
5940  p->debug_lines = debug_lines(p->ruby_sourcefile_string);
5941  if (p->debug_lines && p->ruby_sourceline > 0) {
5942  VALUE str = STR_NEW0();
5943  n = p->ruby_sourceline;
5944  do {
5945  rb_ary_push(p->debug_lines, str);
5946  } while (--n);
5947  }
5948 
5949  if (!e_option_supplied(p)) {
5950  cov = Qtrue;
5951  }
5952  }
5953 
5954  parser_prepare(p);
5955 #define RUBY_DTRACE_PARSE_HOOK(name) \
5956  if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
5957  RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
5958  }
5959  RUBY_DTRACE_PARSE_HOOK(BEGIN);
5960  n = yyparse(p);
5961  RUBY_DTRACE_PARSE_HOOK(END);
5962  p->debug_lines = 0;
5963 
5964  p->lex.strterm = 0;
5965  p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
5966  p->lex.prevline = p->lex.lastline = p->lex.nextline = 0;
5967  if (n || p->error_p) {
5968  VALUE mesg = p->error_buffer;
5969  if (!mesg) {
5970  mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
5971  }
5972  rb_set_errinfo(mesg);
5973  return FALSE;
5974  }
5975  tree = p->eval_tree;
5976  if (!tree) {
5977  tree = NEW_NIL(&NULL_LOC);
5978  }
5979  else {
5980  VALUE opt = p->compile_option;
5981  NODE *prelude;
5982  NODE *body = parser_append_options(p, tree->nd_body);
5983  if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
5984  rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
5985  prelude = block_append(p, p->eval_tree_begin, body);
5986  tree->nd_body = prelude;
5987  RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
5988  }
5989  p->ast->body.root = tree;
5990  p->ast->body.line_count = p->line_count;
5991  return TRUE;
5992 }
5993 
5994 static rb_ast_t *
5995 yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
5996 {
5997  rb_ast_t *ast;
5998  if (NIL_P(fname)) {
5999  p->ruby_sourcefile_string = Qnil;
6000  p->ruby_sourcefile = "(none)";
6001  }
6002  else {
6003  p->ruby_sourcefile_string = rb_fstring(fname);
6004  p->ruby_sourcefile = StringValueCStr(fname);
6005  }
6006  p->ruby_sourceline = line - 1;
6007 
6008  p->ast = ast = rb_ast_new();
6009  rb_suppress_tracing(yycompile0, (VALUE)p);
6010  p->ast = 0;
6011  RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6012 
6013  return ast;
6014 }
6015 #endif /* !RIPPER */
6016 
6017 static rb_encoding *
6018 must_be_ascii_compatible(VALUE s)
6019 {
6020  rb_encoding *enc = rb_enc_get(s);
6021  if (!rb_enc_asciicompat(enc)) {
6022  rb_raise(rb_eArgError, "invalid source encoding");
6023  }
6024  return enc;
6025 }
6026 
6027 static VALUE
6028 lex_get_str(struct parser_params *p, VALUE s)
6029 {
6030  char *beg, *end, *start;
6031  long len;
6032 
6033  beg = RSTRING_PTR(s);
6034  len = RSTRING_LEN(s);
6035  start = beg;
6036  if (p->lex.gets_.ptr) {
6037  if (len == p->lex.gets_.ptr) return Qnil;
6038  beg += p->lex.gets_.ptr;
6039  len -= p->lex.gets_.ptr;
6040  }
6041  end = memchr(beg, '\n', len);
6042  if (end) len = ++end - beg;
6043  p->lex.gets_.ptr += len;
6044  return rb_str_subseq(s, beg - start, len);
6045 }
6046 
6047 static VALUE
6048 lex_getline(struct parser_params *p)
6049 {
6050  VALUE line = (*p->lex.gets)(p, p->lex.input);
6051  if (NIL_P(line)) return line;
6052  must_be_ascii_compatible(line);
6053 #ifndef RIPPER
6054  if (p->debug_lines) {
6055  rb_enc_associate(line, p->enc);
6056  rb_ary_push(p->debug_lines, line);
6057  }
6058 #endif
6059  p->line_count++;
6060  return line;
6061 }
6062 
6063 static const rb_data_type_t parser_data_type;
6064 
6065 #ifndef RIPPER
6066 static rb_ast_t*
6067 parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6068 {
6069  struct parser_params *p;
6070 
6071  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6072 
6073  p->lex.gets = lex_get_str;
6074  p->lex.gets_.ptr = 0;
6075  p->lex.input = rb_str_new_frozen(s);
6076  p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6077 
6078  return yycompile(vparser, p, fname, line);
6079 }
6080 
6081 rb_ast_t*
6082 rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6083 {
6084  return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6085 }
6086 
6087 rb_ast_t*
6088 rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6089 {
6090  must_be_ascii_compatible(s);
6091  return parser_compile_string(vparser, f, s, line);
6092 }
6093 
6094 VALUE rb_io_gets_internal(VALUE io);
6095 
6096 static VALUE
6097 lex_io_gets(struct parser_params *p, VALUE io)
6098 {
6099  return rb_io_gets_internal(io);
6100 }
6101 
6102 rb_ast_t*
6103 rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6104 {
6105  struct parser_params *p;
6106 
6107  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6108 
6109  p->lex.gets = lex_io_gets;
6110  p->lex.input = file;
6111  p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6112 
6113  return yycompile(vparser, p, fname, start);
6114 }
6115 
6116 static VALUE
6117 lex_generic_gets(struct parser_params *p, VALUE input)
6118 {
6119  return (*p->lex.gets_.call)(input, p->line_count);
6120 }
6121 
6122 rb_ast_t*
6123 rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6124 {
6125  struct parser_params *p;
6126 
6127  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6128 
6129  p->lex.gets = lex_generic_gets;
6130  p->lex.gets_.call = lex_gets;
6131  p->lex.input = input;
6132  p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6133 
6134  return yycompile(vparser, p, fname, start);
6135 }
6136 #endif /* !RIPPER */
6137 
6138 #define STR_FUNC_ESCAPE 0x01
6139 #define STR_FUNC_EXPAND 0x02
6140 #define STR_FUNC_REGEXP 0x04
6141 #define STR_FUNC_QWORDS 0x08
6142 #define STR_FUNC_SYMBOL 0x10
6143 #define STR_FUNC_INDENT 0x20
6144 #define STR_FUNC_LABEL 0x40
6145 #define STR_FUNC_LIST 0x4000
6146 #define STR_FUNC_TERM 0x8000
6147 
6148 enum string_type {
6149  str_label = STR_FUNC_LABEL,
6150  str_squote = (0),
6151  str_dquote = (STR_FUNC_EXPAND),
6152  str_xquote = (STR_FUNC_EXPAND),
6153  str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6154  str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6155  str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6156  str_ssym = (STR_FUNC_SYMBOL),
6157  str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6158 };
6159 
6160 static VALUE
6161 parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6162 {
6163  VALUE str;
6164 
6165  str = rb_enc_str_new(ptr, len, enc);
6166  if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6167  if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6168  }
6169  else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6170  rb_enc_associate(str, rb_ascii8bit_encoding());
6171  }
6172  }
6173 
6174  return str;
6175 }
6176 
6177 #define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6178 #define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6179 #define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6180 #define peek(p,c) peek_n(p, (c), 0)
6181 #define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6182 #define peekc(p) peekc_n(p, 0)
6183 #define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6184 
6185 #ifdef RIPPER
6186 static void
6187 add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6188 {
6189  if (tok < end) {
6190  if (!has_delayed_token(p)) {
6191  p->delayed.token = rb_str_buf_new(end - tok);
6192  rb_enc_associate(p->delayed.token, p->enc);
6193  p->delayed.line = p->ruby_sourceline;
6194  p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6195  }
6196  rb_str_buf_cat(p->delayed.token, tok, end - tok);
6197  p->lex.ptok = end;
6198  }
6199 }
6200 #else
6201 #define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6202 #endif
6203 
6204 static int
6205 nextline(struct parser_params *p)
6206 {
6207  VALUE v = p->lex.nextline;
6208  p->lex.nextline = 0;
6209  if (!v) {
6210  if (p->eofp)
6211  return -1;
6212 
6213  if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6214  goto end_of_input;
6215  }
6216 
6217  if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6218  end_of_input:
6219  p->eofp = 1;
6220  lex_goto_eol(p);
6221  return -1;
6222  }
6223  p->cr_seen = FALSE;
6224  }
6225  else if (NIL_P(v)) {
6226  /* after here-document without terminator */
6227  goto end_of_input;
6228  }
6229  add_delayed_token(p, p->lex.ptok, p->lex.pend);
6230  if (p->heredoc_end > 0) {
6231  p->ruby_sourceline = p->heredoc_end;
6232  p->heredoc_end = 0;
6233  }
6234  p->ruby_sourceline++;
6235  p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
6236  p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
6237  token_flush(p);
6238  p->lex.prevline = p->lex.lastline;
6239  p->lex.lastline = v;
6240  return 0;
6241 }
6242 
6243 static int
6244 parser_cr(struct parser_params *p, int c)
6245 {
6246  if (peek(p, '\n')) {
6247  p->lex.pcur++;
6248  c = '\n';
6249  }
6250  else if (!p->cr_seen) {
6251  p->cr_seen = TRUE;
6252  /* carried over with p->lex.nextline for nextc() */
6253  rb_warn0("encountered \\r in middle of line, treated as a mere space");
6254  }
6255  return c;
6256 }
6257 
6258 static inline int
6259 nextc(struct parser_params *p)
6260 {
6261  int c;
6262 
6263  if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6264  if (nextline(p)) return -1;
6265  }
6266  c = (unsigned char)*p->lex.pcur++;
6267  if (UNLIKELY(c == '\r')) {
6268  c = parser_cr(p, c);
6269  }
6270 
6271  return c;
6272 }
6273 
6274 static void
6275 pushback(struct parser_params *p, int c)
6276 {
6277  if (c == -1) return;
6278  p->lex.pcur--;
6279  if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6280  p->lex.pcur--;
6281  }
6282 }
6283 
6284 #define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6285 
6286 #define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6287 #define tok(p) (p)->tokenbuf
6288 #define toklen(p) (p)->tokidx
6289 
6290 static int
6291 looking_at_eol_p(struct parser_params *p)
6292 {
6293  const char *ptr = p->lex.pcur;
6294  while (ptr < p->lex.pend) {
6295  int c = (unsigned char)*ptr++;
6296  int eol = (c == '\n' || c == '#');
6297  if (eol || !ISSPACE(c)) {
6298  return eol;
6299  }
6300  }
6301  return TRUE;
6302 }
6303 
6304 static char*
6305 newtok(struct parser_params *p)
6306 {
6307  p->tokidx = 0;
6308  p->tokline = p->ruby_sourceline;
6309  if (!p->tokenbuf) {
6310  p->toksiz = 60;
6311  p->tokenbuf = ALLOC_N(char, 60);
6312  }
6313  if (p->toksiz > 4096) {
6314  p->toksiz = 60;
6315  REALLOC_N(p->tokenbuf, char, 60);
6316  }
6317  return p->tokenbuf;
6318 }
6319 
6320 static char *
6321 tokspace(struct parser_params *p, int n)
6322 {
6323  p->tokidx += n;
6324 
6325  if (p->tokidx >= p->toksiz) {
6326  do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6327  REALLOC_N(p->tokenbuf, char, p->toksiz);
6328  }
6329  return &p->tokenbuf[p->tokidx-n];
6330 }
6331 
6332 static void
6333 tokadd(struct parser_params *p, int c)
6334 {
6335  p->tokenbuf[p->tokidx++] = (char)c;
6336  if (p->tokidx >= p->toksiz) {
6337  p->toksiz *= 2;
6338  REALLOC_N(p->tokenbuf, char, p->toksiz);
6339  }
6340 }
6341 
6342 static int
6343 tok_hex(struct parser_params *p, size_t *numlen)
6344 {
6345  int c;
6346 
6347  c = scan_hex(p->lex.pcur, 2, numlen);
6348  if (!*numlen) {
6349  yyerror0("invalid hex escape");
6350  token_flush(p);
6351  return 0;
6352  }
6353  p->lex.pcur += *numlen;
6354  return c;
6355 }
6356 
6357 #define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6358 
6359 static int
6360 escaped_control_code(int c)
6361 {
6362  int c2 = 0;
6363  switch (c) {
6364  case ' ':
6365  c2 = 's';
6366  break;
6367  case '\n':
6368  c2 = 'n';
6369  break;
6370  case '\t':
6371  c2 = 't';
6372  break;
6373  case '\v':
6374  c2 = 'v';
6375  break;
6376  case '\r':
6377  c2 = 'r';
6378  break;
6379  case '\f':
6380  c2 = 'f';
6381  break;
6382  }
6383  return c2;
6384 }
6385 
6386 #define WARN_SPACE_CHAR(c, prefix) \
6387  rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6388 
6389 static int
6390 tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6391  int regexp_literal, int wide)
6392 {
6393  size_t numlen;
6394  int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6395  literal_flush(p, p->lex.pcur);
6396  p->lex.pcur += numlen;
6397  if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6398  yyerror0("invalid Unicode escape");
6399  return wide && numlen > 0;
6400  }
6401  if (codepoint > 0x10ffff) {
6402  yyerror0("invalid Unicode codepoint (too large)");
6403  return wide;
6404  }
6405  if ((codepoint & 0xfffff800) == 0xd800) {
6406  yyerror0("invalid Unicode codepoint");
6407  return wide;
6408  }
6409  if (regexp_literal) {
6410  tokcopy(p, (int)numlen);
6411  }
6412  else if (codepoint >= 0x80) {
6413  rb_encoding *utf8 = rb_utf8_encoding();
6414  if (*encp && utf8 != *encp) {
6415  YYLTYPE loc = RUBY_INIT_YYLLOC();
6416  compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6417  parser_show_error_line(p, &loc);
6418  return wide;
6419  }
6420  *encp = utf8;
6421  tokaddmbc(p, codepoint, *encp);
6422  }
6423  else {
6424  tokadd(p, codepoint);
6425  }
6426  return TRUE;
6427 }
6428 
6429 /* return value is for ?\u3042 */
6430 static void
6431 tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6432  int term, int symbol_literal, int regexp_literal)
6433 {
6434  /*
6435  * If `term` is not -1, then we allow multiple codepoints in \u{}
6436  * upto `term` byte, otherwise we're parsing a character literal.
6437  * And then add the codepoints to the current token.
6438  */
6439  static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6440 
6441  const int open_brace = '{', close_brace = '}';
6442 
6443  if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6444 
6445  if (peek(p, open_brace)) { /* handle \u{...} form */
6446  const char *second = NULL;
6447  int c, last = nextc(p);
6448  if (p->lex.pcur >= p->lex.pend) goto unterminated;
6449  while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6450  while (c != close_brace) {
6451  if (c == term) goto unterminated;
6452  if (second == multiple_codepoints)
6453  second = p->lex.pcur;
6454  if (regexp_literal) tokadd(p, last);
6455  if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6456  break;
6457  }
6458  while (ISSPACE(c = *p->lex.pcur)) {
6459  if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6460  last = c;
6461  }
6462  if (term == -1 && !second)
6463  second = multiple_codepoints;
6464  }
6465 
6466  if (c != close_brace) {
6467  unterminated:
6468  token_flush(p);
6469  yyerror0("unterminated Unicode escape");
6470  return;
6471  }
6472  if (second && second != multiple_codepoints) {
6473  const char *pcur = p->lex.pcur;
6474  p->lex.pcur = second;
6475  dispatch_scan_event(p, tSTRING_CONTENT);
6476  token_flush(p);
6477  p->lex.pcur = pcur;
6478  yyerror0(multiple_codepoints);
6479  token_flush(p);
6480  }
6481 
6482  if (regexp_literal) tokadd(p, close_brace);
6483  nextc(p);
6484  }
6485  else { /* handle \uxxxx form */
6486  if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6487  token_flush(p);
6488  return;
6489  }
6490  }
6491 }
6492 
6493 #define ESCAPE_CONTROL 1
6494 #define ESCAPE_META 2
6495 
6496 static int
6497 read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6498 {
6499  int c;
6500  size_t numlen;
6501 
6502  switch (c = nextc(p)) {
6503  case '\\': /* Backslash */
6504  return c;
6505 
6506  case 'n': /* newline */
6507  return '\n';
6508 
6509  case 't': /* horizontal tab */
6510  return '\t';
6511 
6512  case 'r': /* carriage-return */
6513  return '\r';
6514 
6515  case 'f': /* form-feed */
6516  return '\f';
6517 
6518  case 'v': /* vertical tab */
6519  return '\13';
6520 
6521  case 'a': /* alarm(bell) */
6522  return '\007';
6523 
6524  case 'e': /* escape */
6525  return 033;
6526 
6527  case '0': case '1': case '2': case '3': /* octal constant */
6528  case '4': case '5': case '6': case '7':
6529  pushback(p, c);
6530  c = scan_oct(p->lex.pcur, 3, &numlen);
6531  p->lex.pcur += numlen;
6532  return c;
6533 
6534  case 'x': /* hex constant */
6535  c = tok_hex(p, &numlen);
6536  if (numlen == 0) return 0;
6537  return c;
6538 
6539  case 'b': /* backspace */
6540  return '\010';
6541 
6542  case 's': /* space */
6543  return ' ';
6544 
6545  case 'M':
6546  if (flags & ESCAPE_META) goto eof;
6547  if ((c = nextc(p)) != '-') {
6548  goto eof;
6549  }
6550  if ((c = nextc(p)) == '\\') {
6551  if (peek(p, 'u')) goto eof;
6552  return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6553  }
6554  else if (c == -1 || !ISASCII(c)) goto eof;
6555  else {
6556  int c2 = escaped_control_code(c);
6557  if (c2) {
6558  if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6559  WARN_SPACE_CHAR(c2, "\\M-");
6560  }
6561  else {
6562  WARN_SPACE_CHAR(c2, "\\C-\\M-");
6563  }
6564  }
6565  else if (ISCNTRL(c)) goto eof;
6566  return ((c & 0xff) | 0x80);
6567  }
6568 
6569  case 'C':
6570  if ((c = nextc(p)) != '-') {
6571  goto eof;
6572  }
6573  case 'c':
6574  if (flags & ESCAPE_CONTROL) goto eof;
6575  if ((c = nextc(p))== '\\') {
6576  if (peek(p, 'u')) goto eof;
6577  c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6578  }
6579  else if (c == '?')
6580  return 0177;
6581  else if (c == -1 || !ISASCII(c)) goto eof;
6582  else {
6583  int c2 = escaped_control_code(c);
6584  if (c2) {
6585  if (ISCNTRL(c)) {
6586  if (flags & ESCAPE_META) {
6587  WARN_SPACE_CHAR(c2, "\\M-");
6588  }
6589  else {
6590  WARN_SPACE_CHAR(c2, "");
6591  }
6592  }
6593  else {
6594  if (flags & ESCAPE_META) {
6595  WARN_SPACE_CHAR(c2, "\\M-\\C-");
6596  }
6597  else {
6598  WARN_SPACE_CHAR(c2, "\\C-");
6599  }
6600  }
6601  }
6602  else if (ISCNTRL(c)) goto eof;
6603  }
6604  return c & 0x9f;
6605 
6606  eof:
6607  case -1:
6608  yyerror0("Invalid escape character syntax");
6609  token_flush(p);
6610  return '\0';
6611 
6612  default:
6613  return c;
6614  }
6615 }
6616 
6617 static void
6618 tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
6619 {
6620  int len = rb_enc_codelen(c, enc);
6621  rb_enc_mbcput(c, tokspace(p, len), enc);
6622 }
6623 
6624 static int
6625 tokadd_escape(struct parser_params *p, rb_encoding **encp)
6626 {
6627  int c;
6628  int flags = 0;
6629  size_t numlen;
6630 
6631  first:
6632  switch (c = nextc(p)) {
6633  case '\n':
6634  return 0; /* just ignore */
6635 
6636  case '0': case '1': case '2': case '3': /* octal constant */
6637  case '4': case '5': case '6': case '7':
6638  {
6639  ruby_scan_oct(--p->lex.pcur, 3, &numlen);
6640  if (numlen == 0) goto eof;
6641  p->lex.pcur += numlen;
6642  tokcopy(p, (int)numlen + 1);
6643  }
6644  return 0;
6645 
6646  case 'x': /* hex constant */
6647  {
6648  tok_hex(p, &numlen);
6649  if (numlen == 0) return -1;
6650  tokcopy(p, (int)numlen + 2);
6651  }
6652  return 0;
6653 
6654  case 'M':
6655  if (flags & ESCAPE_META) goto eof;
6656  if ((c = nextc(p)) != '-') {
6657  pushback(p, c);
6658  goto eof;
6659  }
6660  tokcopy(p, 3);
6661  flags |= ESCAPE_META;
6662  goto escaped;
6663 
6664  case 'C':
6665  if (flags & ESCAPE_CONTROL) goto eof;
6666  if ((c = nextc(p)) != '-') {
6667  pushback(p, c);
6668  goto eof;
6669  }
6670  tokcopy(p, 3);
6671  goto escaped;
6672 
6673  case 'c':
6674  if (flags & ESCAPE_CONTROL) goto eof;
6675  tokcopy(p, 2);
6676  flags |= ESCAPE_CONTROL;
6677  escaped:
6678  if ((c = nextc(p)) == '\\') {
6679  goto first;
6680  }
6681  else if (c == -1) goto eof;
6682  tokadd(p, c);
6683  return 0;
6684 
6685  eof:
6686  case -1:
6687  yyerror0("Invalid escape character syntax");
6688  token_flush(p);
6689  return -1;
6690 
6691  default:
6692  tokadd(p, '\\');
6693  tokadd(p, c);
6694  }
6695  return 0;
6696 }
6697 
6698 static int
6699 regx_options(struct parser_params *p)
6700 {
6701  int kcode = 0;
6702  int kopt = 0;
6703  int options = 0;
6704  int c, opt, kc;
6705 
6706  newtok(p);
6707  while (c = nextc(p), ISALPHA(c)) {
6708  if (c == 'o') {
6709  options |= RE_OPTION_ONCE;
6710  }
6711  else if (rb_char_to_option_kcode(c, &opt, &kc)) {
6712  if (kc >= 0) {
6713  if (kc != rb_ascii8bit_encindex()) kcode = c;
6714  kopt = opt;
6715  }
6716  else {
6717  options |= opt;
6718  }
6719  }
6720  else {
6721  tokadd(p, c);
6722  }
6723  }
6724  options |= kopt;
6725  pushback(p, c);
6726  if (toklen(p)) {
6727  YYLTYPE loc = RUBY_INIT_YYLLOC();
6728  tokfix(p);
6729  compile_error(p, "unknown regexp option%s - %*s",
6730  toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
6731  parser_show_error_line(p, &loc);
6732  }
6733  return options | RE_OPTION_ENCODING(kcode);
6734 }
6735 
6736 static int
6737 tokadd_mbchar(struct parser_params *p, int c)
6738 {
6739  int len = parser_precise_mbclen(p, p->lex.pcur-1);
6740  if (len < 0) return -1;
6741  tokadd(p, c);
6742  p->lex.pcur += --len;
6743  if (len > 0) tokcopy(p, len);
6744  return c;
6745 }
6746 
6747 static inline int
6748 simple_re_meta(int c)
6749 {
6750  switch (c) {
6751  case '$': case '*': case '+': case '.':
6752  case '?': case '^': case '|':
6753  case ')': case ']': case '}': case '>':
6754  return TRUE;
6755  default:
6756  return FALSE;
6757  }
6758 }
6759 
6760 static int
6761 parser_update_heredoc_indent(struct parser_params *p, int c)
6762 {
6763  if (p->heredoc_line_indent == -1) {
6764  if (c == '\n') p->heredoc_line_indent = 0;
6765  }
6766  else {
6767  if (c == ' ') {
6768  p->heredoc_line_indent++;
6769  return TRUE;
6770  }
6771  else if (c == '\t') {
6772  int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
6773  p->heredoc_line_indent = w * TAB_WIDTH;
6774  return TRUE;
6775  }
6776  else if (c != '\n') {
6777  if (p->heredoc_indent > p->heredoc_line_indent) {
6778  p->heredoc_indent = p->heredoc_line_indent;
6779  }
6780  p->heredoc_line_indent = -1;
6781  }
6782  }
6783  return FALSE;
6784 }
6785 
6786 static void
6787 parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
6788 {
6789  YYLTYPE loc = RUBY_INIT_YYLLOC();
6790  const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
6791  compile_error(p, "%s mixed within %s source", n1, n2);
6792  parser_show_error_line(p, &loc);
6793 }
6794 
6795 static void
6796 parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
6797 {
6798  const char *pos = p->lex.pcur;
6799  p->lex.pcur = beg;
6800  parser_mixed_error(p, enc1, enc2);
6801  p->lex.pcur = pos;
6802 }
6803 
6804 static int
6805 tokadd_string(struct parser_params *p,
6806  int func, int term, int paren, long *nest,
6807  rb_encoding **encp, rb_encoding **enc)
6808 {
6809  int c;
6810  bool erred = false;
6811 
6812 #define mixed_error(enc1, enc2) \
6813  (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
6814 #define mixed_escape(beg, enc1, enc2) \
6815  (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
6816 
6817  while ((c = nextc(p)) != -1) {
6818  if (p->heredoc_indent > 0) {
6819  parser_update_heredoc_indent(p, c);
6820  }
6821 
6822  if (paren && c == paren) {
6823  ++*nest;
6824  }
6825  else if (c == term) {
6826  if (!nest || !*nest) {
6827  pushback(p, c);
6828  break;
6829  }
6830  --*nest;
6831  }
6832  else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
6833  int c2 = *p->lex.pcur;
6834  if (c2 == '$' || c2 == '@' || c2 == '{') {
6835  pushback(p, c);
6836  break;
6837  }
6838  }
6839  else if (c == '\\') {
6840  literal_flush(p, p->lex.pcur - 1);
6841  c = nextc(p);
6842  switch (c) {
6843  case '\n':
6844  if (func & STR_FUNC_QWORDS) break;
6845  if (func & STR_FUNC_EXPAND) {
6846  if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
6847  continue;
6848  if (c == term) {
6849  c = '\\';
6850  goto terminate;
6851  }
6852  }
6853  tokadd(p, '\\');
6854  break;
6855 
6856  case '\\':
6857  if (func & STR_FUNC_ESCAPE) tokadd(p, c);
6858  break;
6859 
6860  case 'u':
6861  if ((func & STR_FUNC_EXPAND) == 0) {
6862  tokadd(p, '\\');
6863  break;
6864  }
6865  tokadd_utf8(p, enc, term,
6866  func & STR_FUNC_SYMBOL,
6867  func & STR_FUNC_REGEXP);
6868  continue;
6869 
6870  default:
6871  if (c == -1) return -1;
6872  if (!ISASCII(c)) {
6873  if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
6874  goto non_ascii;
6875  }
6876  if (func & STR_FUNC_REGEXP) {
6877  if (c == term && !simple_re_meta(c)) {
6878  tokadd(p, c);
6879  continue;
6880  }
6881  pushback(p, c);
6882  if ((c = tokadd_escape(p, enc)) < 0)
6883  return -1;
6884  if (*enc && *enc != *encp) {
6885  mixed_escape(p->lex.ptok+2, *enc, *encp);
6886  }
6887  continue;
6888  }
6889  else if (func & STR_FUNC_EXPAND) {
6890  pushback(p, c);
6891  if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
6892  c = read_escape(p, 0, enc);
6893  }
6894  else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6895  /* ignore backslashed spaces in %w */
6896  }
6897  else if (c != term && !(paren && c == paren)) {
6898  tokadd(p, '\\');
6899  pushback(p, c);
6900  continue;
6901  }
6902  }
6903  }
6904  else if (!parser_isascii(p)) {
6905  non_ascii:
6906  if (!*enc) {
6907  *enc = *encp;
6908  }
6909  else if (*enc != *encp) {
6910  mixed_error(*enc, *encp);
6911  continue;
6912  }
6913  if (tokadd_mbchar(p, c) == -1) return -1;
6914  continue;
6915  }
6916  else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6917  pushback(p, c);
6918  break;
6919  }
6920  if (c & 0x80) {
6921  if (!*enc) {
6922  *enc = *encp;
6923  }
6924  else if (*enc != *encp) {
6925  mixed_error(*enc, *encp);
6926  continue;
6927  }
6928  }
6929  tokadd(p, c);
6930  }
6931  terminate:
6932  if (*enc) *encp = *enc;
6933  return c;
6934 }
6935 
6936 static inline rb_strterm_t *
6937 new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
6938 {
6939  return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
6940 }
6941 
6942 /* imemo_parser_strterm for literal */
6943 #define NEW_STRTERM(func, term, paren) \
6944  new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
6945 
6946 #ifdef RIPPER
6947 static void
6948 flush_string_content(struct parser_params *p, rb_encoding *enc)
6949 {
6950  VALUE content = yylval.val;
6951  if (!ripper_is_node_yylval(content))
6952  content = ripper_new_yylval(p, 0, 0, content);
6953  if (has_delayed_token(p)) {
6954  ptrdiff_t len = p->lex.pcur - p->lex.ptok;
6955  if (len > 0) {
6956  rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
6957  }
6958  dispatch_delayed_token(p, tSTRING_CONTENT);
6959  p->lex.ptok = p->lex.pcur;
6960  RNODE(content)->nd_rval = yylval.val;
6961  }
6962  dispatch_scan_event(p, tSTRING_CONTENT);
6963  if (yylval.val != content)
6964  RNODE(content)->nd_rval = yylval.val;
6965  yylval.val = content;
6966 }
6967 #else
6968 #define flush_string_content(p, enc) ((void)(enc))
6969 #endif
6970 
6971 RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
6972 /* this can be shared with ripper, since it's independent from struct
6973  * parser_params. */
6974 #ifndef RIPPER
6975 #define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
6976 #define SPECIAL_PUNCT(idx) ( \
6977  BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
6978  BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
6979  BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
6980  BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
6981  BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
6982  BIT('0', idx))
6983 const unsigned int ruby_global_name_punct_bits[] = {
6984  SPECIAL_PUNCT(0),
6985  SPECIAL_PUNCT(1),
6986  SPECIAL_PUNCT(2),
6987 };
6988 #undef BIT
6989 #undef SPECIAL_PUNCT
6990 #endif
6991 
6992 static enum yytokentype
6993 parser_peek_variable_name(struct parser_params *p)
6994 {
6995  int c;
6996  const char *ptr = p->lex.pcur;
6997 
6998  if (ptr + 1 >= p->lex.pend) return 0;
6999  c = *ptr++;
7000  switch (c) {
7001  case '$':
7002  if ((c = *ptr) == '-') {
7003  if (++ptr >= p->lex.pend) return 0;
7004  c = *ptr;
7005  }
7006  else if (is_global_name_punct(c) || ISDIGIT(c)) {
7007  return tSTRING_DVAR;
7008  }
7009  break;
7010  case '@':
7011  if ((c = *ptr) == '@') {
7012  if (++ptr >= p->lex.pend) return 0;
7013  c = *ptr;
7014  }
7015  break;
7016  case '{':
7017  p->lex.pcur = ptr;
7018  p->command_start = TRUE;
7019  return tSTRING_DBEG;
7020  default:
7021  return 0;
7022  }
7023  if (!ISASCII(c) || c == '_' || ISALPHA(c))
7024  return tSTRING_DVAR;
7025  return 0;
7026 }
7027 
7028 #define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7029 #define IS_END() IS_lex_state(EXPR_END_ANY)
7030 #define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7031 #define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7032 #define IS_LABEL_POSSIBLE() (\
7033  (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7034  IS_ARG())
7035 #define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7036 #define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7037 
7038 static inline enum yytokentype
7039 parser_string_term(struct parser_params *p, int func)
7040 {
7041  p->lex.strterm = 0;
7042  if (func & STR_FUNC_REGEXP) {
7043  set_yylval_num(regx_options(p));
7044  dispatch_scan_event(p, tREGEXP_END);
7045  SET_LEX_STATE(EXPR_END);
7046  return tREGEXP_END;
7047  }
7048  if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7049  nextc(p);
7050  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7051  return tLABEL_END;
7052  }
7053  SET_LEX_STATE(EXPR_END);
7054  return tSTRING_END;
7055 }
7056 
7057 static enum yytokentype
7058 parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7059 {
7060  int func = (int)quote->u1.func;
7061  int term = (int)quote->u3.term;
7062  int paren = (int)quote->u2.paren;
7063  int c, space = 0;
7064  rb_encoding *enc = p->enc;
7065  rb_encoding *base_enc = 0;
7066  VALUE lit;
7067 
7068  if (func & STR_FUNC_TERM) {
7069  if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7070  SET_LEX_STATE(EXPR_END);
7071  p->lex.strterm = 0;
7072  return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7073  }
7074  c = nextc(p);
7075  if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7076  do {c = nextc(p);} while (ISSPACE(c));
7077  space = 1;
7078  }
7079  if (func & STR_FUNC_LIST) {
7080  quote->u1.func &= ~STR_FUNC_LIST;
7081  space = 1;
7082  }
7083  if (c == term && !quote->u0.nest) {
7084  if (func & STR_FUNC_QWORDS) {
7085  quote->u1.func |= STR_FUNC_TERM;
7086  pushback(p, c); /* dispatch the term at tSTRING_END */
7087  add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7088  return ' ';
7089  }
7090  return parser_string_term(p, func);
7091  }
7092  if (space) {
7093  pushback(p, c);
7094  add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7095  return ' ';
7096  }
7097  newtok(p);
7098  if ((func & STR_FUNC_EXPAND) && c == '#') {
7099  int t = parser_peek_variable_name(p);
7100  if (t) return t;
7101  tokadd(p, '#');
7102  c = nextc(p);
7103  }
7104  pushback(p, c);
7105  if (tokadd_string(p, func, term, paren, &quote->u0.nest,
7106  &enc, &base_enc) == -1) {
7107  if (p->eofp) {
7108 #ifndef RIPPER
7109 # define unterminated_literal(mesg) yyerror0(mesg)
7110 #else
7111 # define unterminated_literal(mesg) compile_error(p, mesg)
7112 #endif
7113  literal_flush(p, p->lex.pcur);
7114  if (func & STR_FUNC_QWORDS) {
7115  /* no content to add, bailing out here */
7116  unterminated_literal("unterminated list meets end of file");
7117  p->lex.strterm = 0;
7118  return tSTRING_END;
7119  }
7120  if (func & STR_FUNC_REGEXP) {
7121  unterminated_literal("unterminated regexp meets end of file");
7122  }
7123  else {
7124  unterminated_literal("unterminated string meets end of file");
7125  }
7126  quote->u1.func |= STR_FUNC_TERM;
7127  }
7128  }
7129 
7130  tokfix(p);
7131  lit = STR_NEW3(tok(p), toklen(p), enc, func);
7132  set_yylval_str(lit);
7133  flush_string_content(p, enc);
7134 
7135  return tSTRING_CONTENT;
7136 }
7137 
7138 static enum yytokentype
7139 heredoc_identifier(struct parser_params *p)
7140 {
7141  /*
7142  * term_len is length of `<<"END"` except `END`,
7143  * in this case term_len is 4 (<, <, " and ").
7144  */
7145  long len, offset = p->lex.pcur - p->lex.pbeg;
7146  int c = nextc(p), term, func = 0, quote = 0;
7147  enum yytokentype token = tSTRING_BEG;
7148  int indent = 0;
7149 
7150  if (c == '-') {
7151  c = nextc(p);
7152  func = STR_FUNC_INDENT;
7153  offset++;
7154  }
7155  else if (c == '~') {
7156  c = nextc(p);
7157  func = STR_FUNC_INDENT;
7158  offset++;
7159  indent = INT_MAX;
7160  }
7161  switch (c) {
7162  case '\'':
7163  func |= str_squote; goto quoted;
7164  case '"':
7165  func |= str_dquote; goto quoted;
7166  case '`':
7167  token = tXSTRING_BEG;
7168  func |= str_xquote; goto quoted;
7169 
7170  quoted:
7171  quote++;
7172  offset++;
7173  term = c;
7174  len = 0;
7175  while ((c = nextc(p)) != term) {
7176  if (c == -1 || c == '\r' || c == '\n') {
7177  yyerror(NULL, p, "unterminated here document identifier");
7178  return -1;
7179  }
7180  }
7181  break;
7182 
7183  default:
7184  if (!parser_is_identchar(p)) {
7185  pushback(p, c);
7186  if (func & STR_FUNC_INDENT) {
7187  pushback(p, indent > 0 ? '~' : '-');
7188  }
7189  return 0;
7190  }
7191  func |= str_dquote;
7192  do {
7193  int n = parser_precise_mbclen(p, p->lex.pcur-1);
7194  if (n < 0) return 0;
7195  p->lex.pcur += --n;
7196  } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7197  pushback(p, c);
7198  break;
7199  }
7200 
7201  len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7202  if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7203  yyerror(NULL, p, "too long here document identifier");
7204  dispatch_scan_event(p, tHEREDOC_BEG);
7205  lex_goto_eol(p);
7206 
7207  p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7208  p->lex.strterm->flags |= STRTERM_HEREDOC;
7209  rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7210  here->offset = offset;
7211  here->sourceline = p->ruby_sourceline;
7212  here->length = (int)len;
7213  here->quote = quote;
7214  here->func = func;
7215 
7216  token_flush(p);
7217  p->heredoc_indent = indent;
7218  p->heredoc_line_indent = 0;
7219  return token;
7220 }
7221 
7222 static void
7223 heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7224 {
7225  VALUE line;
7226 
7227  p->lex.strterm = 0;
7228  line = here->lastline;
7229  p->lex.lastline = line;
7230  p->lex.pbeg = RSTRING_PTR(line);
7231  p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7232  p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7233  p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7234  p->heredoc_end = p->ruby_sourceline;
7235  p->ruby_sourceline = (int)here->sourceline;
7236  if (p->eofp) p->lex.nextline = Qnil;
7237  p->eofp = 0;
7238 }
7239 
7240 static int
7241 dedent_string(VALUE string, int width)
7242 {
7243  char *str;
7244  long len;
7245  int i, col = 0;
7246 
7247  RSTRING_GETMEM(string, str, len);
7248  for (i = 0; i < len && col < width; i++) {
7249  if (str[i] == ' ') {
7250  col++;
7251  }
7252  else if (str[i] == '\t') {
7253  int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7254  if (n > width) break;
7255  col = n;
7256  }
7257  else {
7258  break;
7259  }
7260  }
7261  if (!i) return 0;
7262  rb_str_modify(string);
7263  str = RSTRING_PTR(string);
7264  if (RSTRING_LEN(string) != len)
7265  rb_fatal("literal string changed: %+"PRIsVALUE, string);
7266  MEMMOVE(str, str + i, char, len - i);
7267  rb_str_set_len(string, len - i);
7268  return i;
7269 }
7270 
7271 #ifndef RIPPER
7272 static NODE *
7273 heredoc_dedent(struct parser_params *p, NODE *root)
7274 {
7275  NODE *node, *str_node, *prev_node;
7276  int indent = p->heredoc_indent;
7277  VALUE prev_lit = 0;
7278 
7279  if (indent <= 0) return root;
7280  p->heredoc_indent = 0;
7281  if (!root) return root;
7282 
7283  prev_node = node = str_node = root;
7284  if (nd_type(root) == NODE_LIST) str_node = root->nd_head;
7285 
7286  while (str_node) {
7287  VALUE lit = str_node->nd_lit;
7288  if (str_node->flags & NODE_FL_NEWLINE) {
7289  dedent_string(lit, indent);
7290  }
7291  if (!prev_lit) {
7292  prev_lit = lit;
7293  }
7294  else if (!literal_concat0(p, prev_lit, lit)) {
7295  return 0;
7296  }
7297  else {
7298  NODE *end = node->nd_end;
7299  node = prev_node->nd_next = node->nd_next;
7300  if (!node) {
7301  if (nd_type(prev_node) == NODE_DSTR)
7302  nd_set_type(prev_node, NODE_STR);
7303  break;
7304  }
7305  node->nd_end = end;
7306  goto next_str;
7307  }
7308 
7309  str_node = 0;
7310  while ((node = (prev_node = node)->nd_next) != 0) {
7311  next_str:
7312  if (nd_type(node) != NODE_LIST) break;
7313  if ((str_node = node->nd_head) != 0) {
7314  enum node_type type = nd_type(str_node);
7315  if (type == NODE_STR || type == NODE_DSTR) break;
7316  prev_lit = 0;
7317  str_node = 0;
7318  }
7319  }
7320  }
7321  return root;
7322 }
7323 #else /* RIPPER */
7324 static VALUE
7325 heredoc_dedent(struct parser_params *p, VALUE array)
7326 {
7327  int indent = p->heredoc_indent;
7328 
7329  if (indent <= 0) return array;
7330  p->heredoc_indent = 0;
7331  dispatch2(heredoc_dedent, array, INT2NUM(indent));
7332  return array;
7333 }
7334 
7335 /*
7336  * call-seq:
7337  * Ripper.dedent_string(input, width) -> Integer
7338  *
7339  * USE OF RIPPER LIBRARY ONLY.
7340  *
7341  * Strips up to +width+ leading whitespaces from +input+,
7342  * and returns the stripped column width.
7343  */
7344 static VALUE
7345 parser_dedent_string(VALUE self, VALUE input, VALUE width)
7346 {
7347  int wid, col;
7348 
7349  StringValue(input);
7350  wid = NUM2UINT(width);
7351  col = dedent_string(input, wid);
7352  return INT2NUM(col);
7353 }
7354 #endif
7355 
7356 static int
7357 whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7358 {
7359  const char *ptr = p->lex.pbeg;
7360  long n;
7361 
7362  if (indent) {
7363  while (*ptr && ISSPACE(*ptr)) ptr++;
7364  }
7365  n = p->lex.pend - (ptr + len);
7366  if (n < 0) return FALSE;
7367  if (n > 0 && ptr[len] != '\n') {
7368  if (ptr[len] != '\r') return FALSE;
7369  if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7370  }
7371  return strncmp(eos, ptr, len) == 0;
7372 }
7373 
7374 static int
7375 word_match_p(struct parser_params *p, const char *word, long len)
7376 {
7377  if (strncmp(p->lex.pcur, word, len)) return 0;
7378  if (p->lex.pcur + len == p->lex.pend) return 1;
7379  int c = (unsigned char)p->lex.pcur[len];
7380  if (ISSPACE(c)) return 1;
7381  switch (c) {
7382  case '\0': case '\004': case '\032': return 1;
7383  }
7384  return 0;
7385 }
7386 
7387 #define NUM_SUFFIX_R (1<<0)
7388 #define NUM_SUFFIX_I (1<<1)
7389 #define NUM_SUFFIX_ALL 3
7390 
7391 static int
7392 number_literal_suffix(struct parser_params *p, int mask)
7393 {
7394  int c, result = 0;
7395  const char *lastp = p->lex.pcur;
7396 
7397  while ((c = nextc(p)) != -1) {
7398  if ((mask & NUM_SUFFIX_I) && c == 'i') {
7399  result |= (mask & NUM_SUFFIX_I);
7400  mask &= ~NUM_SUFFIX_I;
7401  /* r after i, rational of complex is disallowed */
7402  mask &= ~NUM_SUFFIX_R;
7403  continue;
7404  }
7405  if ((mask & NUM_SUFFIX_R) && c == 'r') {
7406  result |= (mask & NUM_SUFFIX_R);
7407  mask &= ~NUM_SUFFIX_R;
7408  continue;
7409  }
7410  if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7411  p->lex.pcur = lastp;
7412  literal_flush(p, p->lex.pcur);
7413  return 0;
7414  }
7415  pushback(p, c);
7416  break;
7417  }
7418  return result;
7419 }
7420 
7421 static enum yytokentype
7422 set_number_literal(struct parser_params *p, VALUE v,
7423  enum yytokentype type, int suffix)
7424 {
7425  if (suffix & NUM_SUFFIX_I) {
7426  v = rb_complex_raw(INT2FIX(0), v);
7427  type = tIMAGINARY;
7428  }
7429  set_yylval_literal(v);
7430  SET_LEX_STATE(EXPR_END);
7431  return type;
7432 }
7433 
7434 static enum yytokentype
7435 set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7436 {
7437  enum yytokentype type = tINTEGER;
7438  if (suffix & NUM_SUFFIX_R) {
7439  v = rb_rational_raw1(v);
7440  type = tRATIONAL;
7441  }
7442  return set_number_literal(p, v, type, suffix);
7443 }
7444 
7445 #ifdef RIPPER
7446 static void
7447 dispatch_heredoc_end(struct parser_params *p)
7448 {
7449  VALUE str;
7450  if (has_delayed_token(p))
7451  dispatch_delayed_token(p, tSTRING_CONTENT);
7452  str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7453  ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7454  lex_goto_eol(p);
7455  token_flush(p);
7456 }
7457 
7458 #else
7459 #define dispatch_heredoc_end(p) ((void)0)
7460 #endif
7461 
7462 static enum yytokentype
7463 here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7464 {
7465  int c, func, indent = 0;
7466  const char *eos, *ptr, *ptr_end;
7467  long len;
7468  VALUE str = 0;
7469  rb_encoding *enc = p->enc;
7470  rb_encoding *base_enc = 0;
7471  int bol;
7472 
7473  eos = RSTRING_PTR(here->lastline) + here->offset;
7474  len = here->length;
7475  indent = (func = here->func) & STR_FUNC_INDENT;
7476 
7477  if ((c = nextc(p)) == -1) {
7478  error:
7479 #ifdef RIPPER
7480  if (!has_delayed_token(p)) {
7481  dispatch_scan_event(p, tSTRING_CONTENT);
7482  }
7483  else {
7484  if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7485  if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7486  int cr = ENC_CODERANGE_UNKNOWN;
7487  rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7488  if (cr != ENC_CODERANGE_7BIT &&
7489  p->enc == rb_usascii_encoding() &&
7490  enc != rb_utf8_encoding()) {
7491  enc = rb_ascii8bit_encoding();
7492  }
7493  }
7494  rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7495  }
7496  dispatch_delayed_token(p, tSTRING_CONTENT);
7497  }
7498  lex_goto_eol(p);
7499 #endif
7500  heredoc_restore(p, &p->lex.strterm->u.heredoc);
7501  compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7502  (int)len, eos);
7503  token_flush(p);
7504  p->lex.strterm = 0;
7505  SET_LEX_STATE(EXPR_END);
7506  return tSTRING_END;
7507  }
7508  bol = was_bol(p);
7509  if (!bol) {
7510  /* not beginning of line, cannot be the terminator */
7511  }
7512  else if (p->heredoc_line_indent == -1) {
7513  /* `heredoc_line_indent == -1` means
7514  * - "after an interpolation in the same line", or
7515  * - "in a continuing line"
7516  */
7517  p->heredoc_line_indent = 0;
7518  }
7519  else if (whole_match_p(p, eos, len, indent)) {
7520  dispatch_heredoc_end(p);
7521  restore:
7522  heredoc_restore(p, &p->lex.strterm->u.heredoc);
7523  token_flush(p);
7524  p->lex.strterm = 0;
7525  SET_LEX_STATE(EXPR_END);
7526  return tSTRING_END;
7527  }
7528 
7529  if (!(func & STR_FUNC_EXPAND)) {
7530  do {
7531  ptr = RSTRING_PTR(p->lex.lastline);
7532  ptr_end = p->lex.pend;
7533  if (ptr_end > ptr) {
7534  switch (ptr_end[-1]) {
7535  case '\n':
7536  if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7537  ptr_end++;
7538  break;
7539  }
7540  case '\r':
7541  --ptr_end;
7542  }
7543  }
7544 
7545  if (p->heredoc_indent > 0) {
7546  long i = 0;
7547  while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7548  i++;
7549  p->heredoc_line_indent = 0;
7550  }
7551 
7552  if (str)
7553  rb_str_cat(str, ptr, ptr_end - ptr);
7554  else
7555  str = STR_NEW(ptr, ptr_end - ptr);
7556  if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7557  lex_goto_eol(p);
7558  if (p->heredoc_indent > 0) {
7559  goto flush_str;
7560  }
7561  if (nextc(p) == -1) {
7562  if (str) {
7563  str = 0;
7564  }
7565  goto error;
7566  }
7567  } while (!whole_match_p(p, eos, len, indent));
7568  }
7569  else {
7570  /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7571  newtok(p);
7572  if (c == '#') {
7573  int t = parser_peek_variable_name(p);
7574  if (p->heredoc_line_indent != -1) {
7575  if (p->heredoc_indent > p->heredoc_line_indent) {
7576  p->heredoc_indent = p->heredoc_line_indent;
7577  }
7578  p->heredoc_line_indent = -1;
7579  }
7580  if (t) return t;
7581  tokadd(p, '#');
7582  c = nextc(p);
7583  }
7584  do {
7585  pushback(p, c);
7586  enc = p->enc;
7587  if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7588  if (p->eofp) goto error;
7589  goto restore;
7590  }
7591  if (c != '\n') {
7592  if (c == '\\') p->heredoc_line_indent = -1;
7593  flush:
7594  str = STR_NEW3(tok(p), toklen(p), enc, func);
7595  flush_str:
7596  set_yylval_str(str);
7597 #ifndef RIPPER
7598  if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7599 #endif
7600  flush_string_content(p, enc);
7601  return tSTRING_CONTENT;
7602  }
7603  tokadd(p, nextc(p));
7604  if (p->heredoc_indent > 0) {
7605  lex_goto_eol(p);
7606  goto flush;
7607  }
7608  /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
7609  if ((c = nextc(p)) == -1) goto error;
7610  } while (!whole_match_p(p, eos, len, indent));
7611  str = STR_NEW3(tok(p), toklen(p), enc, func);
7612  }
7613  dispatch_heredoc_end(p);
7614 #ifdef RIPPER
7615  str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
7616  yylval.val, str);
7617 #endif
7618  heredoc_restore(p, &p->lex.strterm->u.heredoc);
7619  token_flush(p);
7620  p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
7621  set_yylval_str(str);
7622 #ifndef RIPPER
7623  if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7624 #endif
7625  return tSTRING_CONTENT;
7626 }
7627 
7628 #include "lex.c"
7629 
7630 static int
7631 arg_ambiguous(struct parser_params *p, char c)
7632 {
7633 #ifndef RIPPER
7634  rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
7635 #else
7636  dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
7637 #endif
7638  return TRUE;
7639 }
7640 
7641 static ID
7642 formal_argument(struct parser_params *p, ID lhs)
7643 {
7644  switch (id_type(lhs)) {
7645  case ID_LOCAL:
7646  break;
7647 #ifndef RIPPER
7648  case ID_CONST:
7649  yyerror0("formal argument cannot be a constant");
7650  return 0;
7651  case ID_INSTANCE:
7652  yyerror0("formal argument cannot be an instance variable");
7653  return 0;
7654  case ID_GLOBAL:
7655  yyerror0("formal argument cannot be a global variable");
7656  return 0;
7657  case ID_CLASS:
7658  yyerror0("formal argument cannot be a class variable");
7659  return 0;
7660  default:
7661  yyerror0("formal argument must be local variable");
7662  return 0;
7663 #else
7664  default:
7665  lhs = dispatch1(param_error, lhs);
7666  ripper_error(p);
7667  return 0;
7668 #endif
7669  }
7670  shadowing_lvar(p, lhs);
7671  return lhs;
7672 }
7673 
7674 static int
7675 lvar_defined(struct parser_params *p, ID id)
7676 {
7677  return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
7678 }
7679 
7680 /* emacsen -*- hack */
7681 static long
7682 parser_encode_length(struct parser_params *p, const char *name, long len)
7683 {
7684  long nlen;
7685 
7686  if (len > 5 && name[nlen = len - 5] == '-') {
7687  if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
7688  return nlen;
7689  }
7690  if (len > 4 && name[nlen = len - 4] == '-') {
7691  if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
7692  return nlen;
7693  if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
7694  !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
7695  /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
7696  return nlen;
7697  }
7698  return len;
7699 }
7700 
7701 static void
7702 parser_set_encode(struct parser_params *p, const char *name)
7703 {
7704  int idx = rb_enc_find_index(name);
7705  rb_encoding *enc;
7706  VALUE excargs[3];
7707 
7708  if (idx < 0) {
7709  excargs[1] = rb_sprintf("unknown encoding name: %s", name);
7710  error:
7711  excargs[0] = rb_eArgError;
7712  excargs[2] = rb_make_backtrace();
7713  rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
7714  rb_exc_raise(rb_make_exception(3, excargs));
7715  }
7716  enc = rb_enc_from_index(idx);
7717  if (!rb_enc_asciicompat(enc)) {
7718  excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
7719  goto error;
7720  }
7721  p->enc = enc;
7722 #ifndef RIPPER
7723  if (p->debug_lines) {
7724  VALUE lines = p->debug_lines;
7725  long i, n = RARRAY_LEN(lines);
7726  for (i = 0; i < n; ++i) {
7727  rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
7728  }
7729  }
7730 #endif
7731 }
7732 
7733 static int
7734 comment_at_top(struct parser_params *p)
7735 {
7736  const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
7737  if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
7738  while (ptr < ptr_end) {
7739  if (!ISSPACE(*ptr)) return 0;
7740  ptr++;
7741  }
7742  return 1;
7743 }
7744 
7745 typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
7746 typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
7747 
7748 static void
7749 magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
7750 {
7751  if (!comment_at_top(p)) {
7752  return;
7753  }
7754  parser_set_encode(p, val);
7755 }
7756 
7757 static int
7758 parser_get_bool(struct parser_params *p, const char *name, const char *val)
7759 {
7760  switch (*val) {
7761  case 't': case 'T':
7762  if (strcasecmp(val, "true") == 0) {
7763  return TRUE;
7764  }
7765  break;
7766  case 'f': case 'F':
7767  if (strcasecmp(val, "false") == 0) {
7768  return FALSE;
7769  }
7770  break;
7771  }
7772  rb_compile_warning(p->ruby_sourcefile, p->ruby_sourceline, "invalid value for %s: %s", name, val);
7773  return -1;
7774 }
7775 
7776 static void
7777 parser_set_token_info(struct parser_params *p, const char *name, const char *val)
7778 {
7779  int b = parser_get_bool(p, name, val);
7780  if (b >= 0) p->token_info_enabled = b;
7781 }
7782 
7783 static void
7784 parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
7785 {
7786  int b;
7787 
7788  if (p->token_seen) {
7789  rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
7790  return;
7791  }
7792 
7793  b = parser_get_bool(p, name, val);
7794  if (b < 0) return;
7795 
7796  if (!p->compile_option)
7797  p->compile_option = rb_obj_hide(rb_ident_hash_new());
7798  rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
7799  (b ? Qtrue : Qfalse));
7800 }
7801 
7802 # if WARN_PAST_SCOPE
7803 static void
7804 parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
7805 {
7806  int b = parser_get_bool(p, name, val);
7807  if (b >= 0) p->past_scope_enabled = b;
7808 }
7809 # endif
7810 
7811 struct magic_comment {
7812  const char *name;
7813  rb_magic_comment_setter_t func;
7814  rb_magic_comment_length_t length;
7815 };
7816 
7817 static const struct magic_comment magic_comments[] = {
7818  {"coding", magic_comment_encoding, parser_encode_length},
7819  {"encoding", magic_comment_encoding, parser_encode_length},
7820  {"frozen_string_literal", parser_set_compile_option_flag},
7821  {"warn_indent", parser_set_token_info},
7822 # if WARN_PAST_SCOPE
7823  {"warn_past_scope", parser_set_past_scope},
7824 # endif
7825 };
7826 
7827 static const char *
7828 magic_comment_marker(const char *str, long len)
7829 {
7830  long i = 2;
7831 
7832  while (i < len) {
7833  switch (str[i]) {
7834  case '-':
7835  if (str[i-1] == '*' && str[i-2] == '-') {
7836  return str + i + 1;
7837  }
7838  i += 2;
7839  break;
7840  case '*':
7841  if (i + 1 >= len) return 0;
7842  if (str[i+1] != '-') {
7843  i += 4;
7844  }
7845  else if (str[i-1] != '-') {
7846  i += 2;
7847  }
7848  else {
7849  return str + i + 2;
7850  }
7851  break;
7852  default:
7853  i += 3;
7854  break;
7855  }
7856  }
7857  return 0;
7858 }
7859 
7860 static int
7861 parser_magic_comment(struct parser_params *p, const char *str, long len)
7862 {
7863  int indicator = 0;
7864  VALUE name = 0, val = 0;
7865  const char *beg, *end, *vbeg, *vend;
7866 #define str_copy(_s, _p, _n) ((_s) \
7867  ? (void)(rb_str_resize((_s), (_n)), \
7868  MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
7869  : (void)((_s) = STR_NEW((_p), (_n))))
7870 
7871  if (len <= 7) return FALSE;
7872  if (!!(beg = magic_comment_marker(str, len))) {
7873  if (!(end = magic_comment_marker(beg, str + len - beg)))
7874  return FALSE;
7875  indicator = TRUE;
7876  str = beg;
7877  len = end - beg - 3;
7878  }
7879 
7880  /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
7881  while (len > 0) {
7882  const struct magic_comment *mc = magic_comments;
7883  char *s;
7884  int i;
7885  long n = 0;
7886 
7887  for (; len > 0 && *str; str++, --len) {
7888  switch (*str) {
7889  case '\'': case '"': case ':': case ';':
7890  continue;
7891  }
7892  if (!ISSPACE(*str)) break;
7893  }
7894  for (beg = str; len > 0; str++, --len) {
7895  switch (*str) {
7896  case '\'': case '"': case ':': case ';':
7897  break;
7898  default:
7899  if (ISSPACE(*str)) break;
7900  continue;
7901  }
7902  break;
7903  }
7904  for (end = str; len > 0 && ISSPACE(*str); str++, --len);
7905  if (!len) break;
7906  if (*str != ':') {
7907  if (!indicator) return FALSE;
7908  continue;
7909  }
7910 
7911  do str++; while (--len > 0 && ISSPACE(*str));
7912  if (!len) break;
7913  if (*str == '"') {
7914  for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
7915  if (*str == '\\') {
7916  --len;
7917  ++str;
7918  }
7919  }
7920  vend = str;
7921  if (len) {
7922  --len;
7923  ++str;
7924  }
7925  }
7926  else {
7927  for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
7928  vend = str;
7929  }
7930  if (indicator) {
7931  while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
7932  }
7933  else {
7934  while (len > 0 && (ISSPACE(*str))) --len, str++;
7935  if (len) return FALSE;
7936  }
7937 
7938  n = end - beg;
7939  str_copy(name, beg, n);
7940  s = RSTRING_PTR(name);
7941  for (i = 0; i < n; ++i) {
7942  if (s[i] == '-') s[i] = '_';
7943  }
7944  do {
7945  if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
7946  n = vend - vbeg;
7947  if (mc->length) {
7948  n = (*mc->length)(p, vbeg, n);
7949  }
7950  str_copy(val, vbeg, n);
7951  (*mc->func)(p, mc->name, RSTRING_PTR(val));
7952  break;
7953  }
7954  } while (++mc < magic_comments + numberof(magic_comments));
7955 #ifdef RIPPER
7956  str_copy(val, vbeg, vend - vbeg);
7957  dispatch2(magic_comment, name, val);
7958 #endif
7959  }
7960 
7961  return TRUE;
7962 }
7963 
7964 static void
7965 set_file_encoding(struct parser_params *p, const char *str, const char *send)
7966 {
7967  int sep = 0;
7968  const char *beg = str;
7969  VALUE s;
7970 
7971  for (;;) {
7972  if (send - str <= 6) return;
7973  switch (str[6]) {
7974  case 'C': case 'c': str += 6; continue;
7975  case 'O': case 'o': str += 5; continue;
7976  case 'D': case 'd': str += 4; continue;
7977  case 'I': case 'i': str += 3; continue;
7978  case 'N': case 'n': str += 2; continue;
7979  case 'G': case 'g': str += 1; continue;
7980  case '=': case ':':
7981  sep = 1;
7982  str += 6;
7983  break;
7984  default:
7985  str += 6;
7986  if (ISSPACE(*str)) break;
7987  continue;
7988  }
7989  if (STRNCASECMP(str-6, "coding", 6) == 0) break;
7990  }
7991  for (;;) {
7992  do {
7993  if (++str >= send) return;
7994  } while (ISSPACE(*str));
7995  if (sep) break;
7996  if (*str != '=' && *str != ':') return;
7997  sep = 1;
7998  str++;
7999  }
8000  beg = str;
8001  while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8002  s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8003  parser_set_encode(p, RSTRING_PTR(s));
8004  rb_str_resize(s, 0);
8005 }
8006 
8007 static void
8008 parser_prepare(struct parser_params *p)
8009 {
8010  int c = nextc(p);
8011  p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8012  switch (c) {
8013  case '#':
8014  if (peek(p, '!')) p->has_shebang = 1;
8015  break;
8016  case 0xef: /* UTF-8 BOM marker */
8017  if (p->lex.pend - p->lex.pcur >= 2 &&
8018  (unsigned char)p->lex.pcur[0] == 0xbb &&
8019  (unsigned char)p->lex.pcur[1] == 0xbf) {
8020  p->enc = rb_utf8_encoding();
8021  p->lex.pcur += 2;
8022  p->lex.pbeg = p->lex.pcur;
8023  return;
8024  }
8025  break;
8026  case EOF:
8027  return;
8028  }
8029  pushback(p, c);
8030  p->enc = rb_enc_get(p->lex.lastline);
8031 }
8032 
8033 #ifndef RIPPER
8034 #define ambiguous_operator(tok, op, syn) ( \
8035  rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8036  rb_warning0("even though it seems like "syn""))
8037 #else
8038 #define ambiguous_operator(tok, op, syn) \
8039  dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8040 #endif
8041 #define warn_balanced(tok, op, syn) ((void) \
8042  (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8043  space_seen && !ISSPACE(c) && \
8044  (ambiguous_operator(tok, op, syn), 0)), \
8045  (enum yytokentype)(tok))
8046 
8047 static VALUE
8048 parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8049 {
8050  VALUE v;
8051  char *point = &str[seen_point];
8052  size_t fraclen = len-seen_point-1;
8053  memmove(point, point+1, fraclen+1);
8054  v = rb_cstr_to_inum(str, 10, FALSE);
8055  return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8056 }
8057 
8058 static enum yytokentype
8059 no_digits(struct parser_params *p)
8060 {
8061  yyerror0("numeric literal without digits");
8062  if (peek(p, '_')) nextc(p);
8063  /* dummy 0, for tUMINUS_NUM at numeric */
8064  return set_integer_literal(p, INT2FIX(0), 0);
8065 }
8066 
8067 static enum yytokentype
8068 parse_numeric(struct parser_params *p, int c)
8069 {
8070  int is_float, seen_point, seen_e, nondigit;
8071  int suffix;
8072 
8073  is_float = seen_point = seen_e = nondigit = 0;
8074  SET_LEX_STATE(EXPR_END);
8075  newtok(p);
8076  if (c == '-' || c == '+') {
8077  tokadd(p, c);
8078  c = nextc(p);
8079  }
8080  if (c == '0') {
8081  int start = toklen(p);
8082  c = nextc(p);
8083  if (c == 'x' || c == 'X') {
8084  /* hexadecimal */
8085  c = nextc(p);
8086  if (c != -1 && ISXDIGIT(c)) {
8087  do {
8088  if (c == '_') {
8089  if (nondigit) break;
8090  nondigit = c;
8091  continue;
8092  }
8093  if (!ISXDIGIT(c)) break;
8094  nondigit = 0;
8095  tokadd(p, c);
8096  } while ((c = nextc(p)) != -1);
8097  }
8098  pushback(p, c);
8099  tokfix(p);
8100  if (toklen(p) == start) {
8101  return no_digits(p);
8102  }
8103  else if (nondigit) goto trailing_uc;
8104  suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8105  return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8106  }
8107  if (c == 'b' || c == 'B') {
8108  /* binary */
8109  c = nextc(p);
8110  if (c == '0' || c == '1') {
8111  do {
8112  if (c == '_') {
8113  if (nondigit) break;
8114  nondigit = c;
8115  continue;
8116  }
8117  if (c != '0' && c != '1') break;
8118  nondigit = 0;
8119  tokadd(p, c);
8120  } while ((c = nextc(p)) != -1);
8121  }
8122  pushback(p, c);
8123  tokfix(p);
8124  if (toklen(p) == start) {
8125  return no_digits(p);
8126  }
8127  else if (nondigit) goto trailing_uc;
8128  suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8129  return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8130  }
8131  if (c == 'd' || c == 'D') {
8132  /* decimal */
8133  c = nextc(p);
8134  if (c != -1 && ISDIGIT(c)) {
8135  do {
8136  if (c == '_') {
8137  if (nondigit) break;
8138  nondigit = c;
8139  continue;
8140  }
8141  if (!ISDIGIT(c)) break;
8142  nondigit = 0;
8143  tokadd(p, c);
8144  } while ((c = nextc(p)) != -1);
8145  }
8146  pushback(p, c);
8147  tokfix(p);
8148  if (toklen(p) == start) {
8149  return no_digits(p);
8150  }
8151  else if (nondigit) goto trailing_uc;
8152  suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8153  return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8154  }
8155  if (c == '_') {
8156  /* 0_0 */
8157  goto octal_number;
8158  }
8159  if (c == 'o' || c == 'O') {
8160  /* prefixed octal */
8161  c = nextc(p);
8162  if (c == -1 || c == '_' || !ISDIGIT(c)) {
8163  return no_digits(p);
8164  }
8165  }
8166  if (c >= '0' && c <= '7') {
8167  /* octal */
8168  octal_number:
8169  do {
8170  if (c == '_') {
8171  if (nondigit) break;
8172  nondigit = c;
8173  continue;
8174  }
8175  if (c < '0' || c > '9') break;
8176  if (c > '7') goto invalid_octal;
8177  nondigit = 0;
8178  tokadd(p, c);
8179  } while ((c = nextc(p)) != -1);
8180  if (toklen(p) > start) {
8181  pushback(p, c);
8182  tokfix(p);
8183  if (nondigit) goto trailing_uc;
8184  suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8185  return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8186  }
8187  if (nondigit) {
8188  pushback(p, c);
8189  goto trailing_uc;
8190  }
8191  }
8192  if (c > '7' && c <= '9') {
8193  invalid_octal:
8194  yyerror0("Invalid octal digit");
8195  }
8196  else if (c == '.' || c == 'e' || c == 'E') {
8197  tokadd(p, '0');
8198  }
8199  else {
8200  pushback(p, c);
8201  suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8202  return set_integer_literal(p, INT2FIX(0), suffix);
8203  }
8204  }
8205 
8206  for (;;) {
8207  switch (c) {
8208  case '0': case '1': case '2': case '3': case '4':
8209  case '5': case '6': case '7': case '8': case '9':
8210  nondigit = 0;
8211  tokadd(p, c);
8212  break;
8213 
8214  case '.':
8215  if (nondigit) goto trailing_uc;
8216  if (seen_point || seen_e) {
8217  goto decode_num;
8218  }
8219  else {
8220  int c0 = nextc(p);
8221  if (c0 == -1 || !ISDIGIT(c0)) {
8222  pushback(p, c0);
8223  goto decode_num;
8224  }
8225  c = c0;
8226  }
8227  seen_point = toklen(p);
8228  tokadd(p, '.');
8229  tokadd(p, c);
8230  is_float++;
8231  nondigit = 0;
8232  break;
8233 
8234  case 'e':
8235  case 'E':
8236  if (nondigit) {
8237  pushback(p, c);
8238  c = nondigit;
8239  goto decode_num;
8240  }
8241  if (seen_e) {
8242  goto decode_num;
8243  }
8244  nondigit = c;
8245  c = nextc(p);
8246  if (c != '-' && c != '+' && !ISDIGIT(c)) {
8247  pushback(p, c);
8248  nondigit = 0;
8249  goto decode_num;
8250  }
8251  tokadd(p, nondigit);
8252  seen_e++;
8253  is_float++;
8254  tokadd(p, c);
8255  nondigit = (c == '-' || c == '+') ? c : 0;
8256  break;
8257 
8258  case '_': /* `_' in number just ignored */
8259  if (nondigit) goto decode_num;
8260  nondigit = c;
8261  break;
8262 
8263  default:
8264  goto decode_num;
8265  }
8266  c = nextc(p);
8267  }
8268 
8269  decode_num:
8270  pushback(p, c);
8271  if (nondigit) {
8272  trailing_uc:
8273  literal_flush(p, p->lex.pcur - 1);
8274  YYLTYPE loc = RUBY_INIT_YYLLOC();
8275  compile_error(p, "trailing `%c' in number", nondigit);
8276  parser_show_error_line(p, &loc);
8277  }
8278  tokfix(p);
8279  if (is_float) {
8280  enum yytokentype type = tFLOAT;
8281  VALUE v;
8282 
8283  suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8284  if (suffix & NUM_SUFFIX_R) {
8285  type = tRATIONAL;
8286  v = parse_rational(p, tok(p), toklen(p), seen_point);
8287  }
8288  else {
8289  double d = strtod(tok(p), 0);
8290  if (errno == ERANGE) {
8291  rb_warning1("Float %s out of range", WARN_S(tok(p)));
8292  errno = 0;
8293  }
8294  v = DBL2NUM(d);
8295  }
8296  return set_number_literal(p, v, type, suffix);
8297  }
8298  suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8299  return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8300 }
8301 
8302 static enum yytokentype
8303 parse_qmark(struct parser_params *p, int space_seen)
8304 {
8305  rb_encoding *enc;
8306  register int c;
8307  VALUE lit;
8308 
8309  if (IS_END()) {
8310  SET_LEX_STATE(EXPR_VALUE);
8311  return '?';
8312  }
8313  c = nextc(p);
8314  if (c == -1) {
8315  compile_error(p, "incomplete character syntax");
8316  return 0;
8317  }
8318  if (rb_enc_isspace(c, p->enc)) {
8319  if (!IS_ARG()) {
8320  int c2 = escaped_control_code(c);
8321  if (c2) {
8322  WARN_SPACE_CHAR(c2, "?");
8323  }
8324  }
8325  ternary:
8326  pushback(p, c);
8327  SET_LEX_STATE(EXPR_VALUE);
8328  return '?';
8329  }
8330  newtok(p);
8331  enc = p->enc;
8332  if (!parser_isascii(p)) {
8333  if (tokadd_mbchar(p, c) == -1) return 0;
8334  }
8335  else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8336  p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8337  if (space_seen) {
8338  const char *start = p->lex.pcur - 1, *ptr = start;
8339  do {
8340  int n = parser_precise_mbclen(p, ptr);
8341  if (n < 0) return -1;
8342  ptr += n;
8343  } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8344  rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8345  " a conditional operator, put a space after `?'",
8346  WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8347  }
8348  goto ternary;
8349  }
8350  else if (c == '\\') {
8351  if (peek(p, 'u')) {
8352  nextc(p);
8353  enc = rb_utf8_encoding();
8354  tokadd_utf8(p, &enc, -1, 0, 0);
8355  }
8356  else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8357  nextc(p);
8358  if (tokadd_mbchar(p, c) == -1) return 0;
8359  }
8360  else {
8361  c = read_escape(p, 0, &enc);
8362  tokadd(p, c);
8363  }
8364  }
8365  else {
8366  tokadd(p, c);
8367  }
8368  tokfix(p);
8369  lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8370  set_yylval_str(lit);
8371  SET_LEX_STATE(EXPR_END);
8372  return tCHAR;
8373 }
8374 
8375 static enum yytokentype
8376 parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8377 {
8378  register int c;
8379  const char *ptok = p->lex.pcur;
8380 
8381  if (IS_BEG()) {
8382  int term;
8383  int paren;
8384 
8385  c = nextc(p);
8386  quotation:
8387  if (c == -1 || !ISALNUM(c)) {
8388  term = c;
8389  c = 'Q';
8390  }
8391  else {
8392  term = nextc(p);
8393  if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8394  yyerror0("unknown type of %string");
8395  return 0;
8396  }
8397  }
8398  if (c == -1 || term == -1) {
8399  compile_error(p, "unterminated quoted string meets end of file");
8400  return 0;
8401  }
8402  paren = term;
8403  if (term == '(') term = ')';
8404  else if (term == '[') term = ']';
8405  else if (term == '{') term = '}';
8406  else if (term == '<') term = '>';
8407  else paren = 0;
8408 
8409  p->lex.ptok = ptok-1;
8410  switch (c) {
8411  case 'Q':
8412  p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8413  return tSTRING_BEG;
8414 
8415  case 'q':
8416  p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8417  return tSTRING_BEG;
8418 
8419  case 'W':
8420  p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8421  return tWORDS_BEG;
8422 
8423  case 'w':
8424  p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8425  return tQWORDS_BEG;
8426 
8427  case 'I':
8428  p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8429  return tSYMBOLS_BEG;
8430 
8431  case 'i':
8432  p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8433  return tQSYMBOLS_BEG;
8434 
8435  case 'x':
8436  p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8437  return tXSTRING_BEG;
8438 
8439  case 'r':
8440  p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8441  return tREGEXP_BEG;
8442 
8443  case 's':
8444  p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8445  SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8446  return tSYMBEG;
8447 
8448  default:
8449  yyerror0("unknown type of %string");
8450  return 0;
8451  }
8452  }
8453  if ((c = nextc(p)) == '=') {
8454  set_yylval_id('%');
8455  SET_LEX_STATE(EXPR_BEG);
8456  return tOP_ASGN;
8457  }
8458  if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8459  goto quotation;
8460  }
8461  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8462  pushback(p, c);
8463  return warn_balanced('%', "%%", "string literal");
8464 }
8465 
8466 static int
8467 tokadd_ident(struct parser_params *p, int c)
8468 {
8469  do {
8470  if (tokadd_mbchar(p, c) == -1) return -1;
8471  c = nextc(p);
8472  } while (parser_is_identchar(p));
8473  pushback(p, c);
8474  return 0;
8475 }
8476 
8477 static ID
8478 tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8479 {
8480  ID ident = TOK_INTERN();
8481 
8482  set_yylval_name(ident);
8483 
8484  return ident;
8485 }
8486 
8487 static int
8488 parse_numvar(struct parser_params *p)
8489 {
8490  size_t len;
8491  int overflow;
8492  unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8493  const unsigned long nth_ref_max =
8494  ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8495  /* NTH_REF is left-shifted to be ORed with back-ref flag and
8496  * turned into a Fixnum, in compile.c */
8497 
8498  if (overflow || n > nth_ref_max) {
8499  /* compile_error()? */
8500  rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8501  return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8502  }
8503  else {
8504  return (int)n;
8505  }
8506 }
8507 
8508 static enum yytokentype
8509 parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8510 {
8511  const char *ptr = p->lex.pcur;
8512  register int c;
8513 
8514  SET_LEX_STATE(EXPR_END);
8515  p->lex.ptok = ptr - 1; /* from '$' */
8516  newtok(p);
8517  c = nextc(p);
8518  switch (c) {
8519  case '_': /* $_: last read line string */
8520  c = nextc(p);
8521  if (parser_is_identchar(p)) {
8522  tokadd(p, '$');
8523  tokadd(p, '_');
8524  break;
8525  }
8526  pushback(p, c);
8527  c = '_';
8528  /* fall through */
8529  case '~': /* $~: match-data */
8530  case '*': /* $*: argv */
8531  case '$': /* $$: pid */
8532  case '?': /* $?: last status */
8533  case '!': /* $!: error string */
8534  case '@': /* $@: error position */
8535  case '/': /* $/: input record separator */
8536  case '\\': /* $\: output record separator */
8537  case ';': /* $;: field separator */
8538  case ',': /* $,: output field separator */
8539  case '.': /* $.: last read line number */
8540  case '=': /* $=: ignorecase */
8541  case ':': /* $:: load path */
8542  case '<': /* $<: reading filename */
8543  case '>': /* $>: default output handle */
8544  case '\"': /* $": already loaded files */
8545  tokadd(p, '$');
8546  tokadd(p, c);
8547  goto gvar;
8548 
8549  case '-':
8550  tokadd(p, '$');
8551  tokadd(p, c);
8552  c = nextc(p);
8553  if (parser_is_identchar(p)) {
8554  if (tokadd_mbchar(p, c) == -1) return 0;
8555  }
8556  else {
8557  pushback(p, c);
8558  pushback(p, '-');
8559  return '$';
8560  }
8561  gvar:
8562  set_yylval_name(TOK_INTERN());
8563  return tGVAR;
8564 
8565  case '&': /* $&: last match */
8566  case '`': /* $`: string before last match */
8567  case '\'': /* $': string after last match */
8568  case '+': /* $+: string matches last paren. */
8569  if (IS_lex_state_for(last_state, EXPR_FNAME)) {
8570  tokadd(p, '$');
8571  tokadd(p, c);
8572  goto gvar;
8573  }
8574  set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
8575  return tBACK_REF;
8576 
8577  case '1': case '2': case '3':
8578  case '4': case '5': case '6':
8579  case '7': case '8': case '9':
8580  tokadd(p, '$');
8581  do {
8582  tokadd(p, c);
8583  c = nextc(p);
8584  } while (c != -1 && ISDIGIT(c));
8585  pushback(p, c);
8586  if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
8587  tokfix(p);
8588  set_yylval_node(NEW_NTH_REF(parse_numvar(p), &_cur_loc));
8589  return tNTH_REF;
8590 
8591  default:
8592  if (!parser_is_identchar(p)) {
8593  YYLTYPE loc = RUBY_INIT_YYLLOC();
8594  if (c == -1 || ISSPACE(c)) {
8595  compile_error(p, "`$' without identifiers is not allowed as a global variable name");
8596  }
8597  else {
8598  pushback(p, c);
8599  compile_error(p, "`$%c' is not allowed as a global variable name", c);
8600  }
8601  parser_show_error_line(p, &loc);
8602  set_yylval_noname();
8603  return tGVAR;
8604  }
8605  /* fall through */
8606  case '0':
8607  tokadd(p, '$');
8608  }
8609 
8610  if (tokadd_ident(p, c)) return 0;
8611  SET_LEX_STATE(EXPR_END);
8612  tokenize_ident(p, last_state);
8613  return tGVAR;
8614 }
8615 
8616 #ifndef RIPPER
8617 static bool
8618 parser_numbered_param(struct parser_params *p, int n)
8619 {
8620  if (n < 0) return false;
8621 
8622  if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
8623  return false;
8624  }
8625  if (p->max_numparam == ORDINAL_PARAM) {
8626  compile_error(p, "ordinary parameter is defined");
8627  return false;
8628  }
8629  struct vtable *args = p->lvtbl->args;
8630  if (p->max_numparam < n) {
8631  p->max_numparam = n;
8632  }
8633  while (n > args->pos) {
8634  vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
8635  }
8636  return true;
8637 }
8638 #endif
8639 
8640 static enum yytokentype
8641 parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
8642 {
8643  const char *ptr = p->lex.pcur;
8644  enum yytokentype result = tIVAR;
8645  register int c = nextc(p);
8646  YYLTYPE loc;
8647 
8648  p->lex.ptok = ptr - 1; /* from '@' */
8649  newtok(p);
8650  tokadd(p, '@');
8651  if (c == '@') {
8652  result = tCVAR;
8653  tokadd(p, '@');
8654  c = nextc(p);
8655  }
8656  SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
8657  if (c == -1 || !parser_is_identchar(p)) {
8658  pushback(p, c);
8659  RUBY_SET_YYLLOC(loc);
8660  if (result == tIVAR) {
8661  compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
8662  }
8663  else {
8664  compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
8665  }
8666  parser_show_error_line(p, &loc);
8667  set_yylval_noname();
8668  SET_LEX_STATE(EXPR_END);
8669  return result;
8670  }
8671  else if (ISDIGIT(c)) {
8672  pushback(p, c);
8673  RUBY_SET_YYLLOC(loc);
8674  if (result == tIVAR) {
8675  compile_error(p, "`@%c' is not allowed as an instance variable name", c);
8676  }
8677  else {
8678  compile_error(p, "`@@%c' is not allowed as a class variable name", c);
8679  }
8680  parser_show_error_line(p, &loc);
8681  set_yylval_noname();
8682  SET_LEX_STATE(EXPR_END);
8683  return result;
8684  }
8685 
8686  if (tokadd_ident(p, c)) return 0;
8687  tokenize_ident(p, last_state);
8688  return result;
8689 }
8690 
8691 static enum yytokentype
8692 parse_ident(struct parser_params *p, int c, int cmd_state)
8693 {
8694  enum yytokentype result;
8695  int mb = ENC_CODERANGE_7BIT;
8696  const enum lex_state_e last_state = p->lex.state;
8697  ID ident;
8698 
8699  do {
8700  if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
8701  if (tokadd_mbchar(p, c) == -1) return 0;
8702  c = nextc(p);
8703  } while (parser_is_identchar(p));
8704  if ((c == '!' || c == '?') && !peek(p, '=')) {
8705  result = tFID;
8706  tokadd(p, c);
8707  }
8708  else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
8709  (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
8710  result = tIDENTIFIER;
8711  tokadd(p, c);
8712  }
8713  else {
8714  result = tCONSTANT; /* assume provisionally */
8715  pushback(p, c);
8716  }
8717  tokfix(p);
8718 
8719  if (IS_LABEL_POSSIBLE()) {
8720  if (IS_LABEL_SUFFIX(0)) {
8721  SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8722  nextc(p);
8723  set_yylval_name(TOK_INTERN());
8724  return tLABEL;
8725  }
8726  }
8727  if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
8728  const struct kwtable *kw;
8729 
8730  /* See if it is a reserved word. */
8731  kw = rb_reserved_word(tok(p), toklen(p));
8732  if (kw) {
8733  enum lex_state_e state = p->lex.state;
8734  if (IS_lex_state_for(state, EXPR_FNAME)) {
8735  SET_LEX_STATE(EXPR_ENDFN);
8736  set_yylval_name(rb_intern2(tok(p), toklen(p)));
8737  return kw->id[0];
8738  }
8739  SET_LEX_STATE(kw->state);
8740  if (IS_lex_state(EXPR_BEG)) {
8741  p->command_start = TRUE;
8742  }
8743  if (kw->id[0] == keyword_do) {
8744  if (lambda_beginning_p()) {
8745  p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
8746  return keyword_do_LAMBDA;
8747  }
8748  if (COND_P()) return keyword_do_cond;
8749  if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
8750  return keyword_do_block;
8751  return keyword_do;
8752  }
8753  if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
8754  return kw->id[0];
8755  else {
8756  if (kw->id[0] != kw->id[1])
8757  SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
8758  return kw->id[1];
8759  }
8760  }
8761  }
8762 
8763  if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
8764  if (cmd_state) {
8765  SET_LEX_STATE(EXPR_CMDARG);
8766  }
8767  else {
8768  SET_LEX_STATE(EXPR_ARG);
8769  }
8770  }
8771  else if (p->lex.state == EXPR_FNAME) {
8772  SET_LEX_STATE(EXPR_ENDFN);
8773  }
8774  else {
8775  SET_LEX_STATE(EXPR_END);
8776  }
8777 
8778  ident = tokenize_ident(p, last_state);
8779  if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
8780  if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
8781  (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
8782  lvar_defined(p, ident)) {
8783  SET_LEX_STATE(EXPR_END|EXPR_LABEL);
8784  }
8785  return result;
8786 }
8787 
8788 static enum yytokentype
8789 parser_yylex(struct parser_params *p)
8790 {
8791  register int c;
8792  int space_seen = 0;
8793  int cmd_state;
8794  int label;
8795  enum lex_state_e last_state;
8796  int fallthru = FALSE;
8797  int token_seen = p->token_seen;
8798 
8799  if (p->lex.strterm) {
8800  if (p->lex.strterm->flags & STRTERM_HEREDOC) {
8801  return here_document(p, &p->lex.strterm->u.heredoc);
8802  }
8803  else {
8804  token_flush(p);
8805  return parse_string(p, &p->lex.strterm->u.literal);
8806  }
8807  }
8808  cmd_state = p->command_start;
8809  p->command_start = FALSE;
8810  p->token_seen = TRUE;
8811  retry:
8812  last_state = p->lex.state;
8813 #ifndef RIPPER
8814  token_flush(p);
8815 #endif
8816  switch (c = nextc(p)) {
8817  case '\0': /* NUL */
8818  case '\004': /* ^D */
8819  case '\032': /* ^Z */
8820  case -1: /* end of script. */
8821  return 0;
8822 
8823  /* white spaces */
8824  case ' ': case '\t': case '\f': case '\r':
8825  case '\13': /* '\v' */
8826  space_seen = 1;
8827 #ifdef RIPPER
8828  while ((c = nextc(p))) {
8829  switch (c) {
8830  case ' ': case '\t': case '\f': case '\r':
8831  case '\13': /* '\v' */
8832  break;
8833  default:
8834  goto outofloop;
8835  }
8836  }
8837  outofloop:
8838  pushback(p, c);
8839  dispatch_scan_event(p, tSP);
8840 #endif
8841  goto retry;
8842 
8843  case '#': /* it's a comment */
8844  p->token_seen = token_seen;
8845  /* no magic_comment in shebang line */
8846  if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
8847  if (comment_at_top(p)) {
8848  set_file_encoding(p, p->lex.pcur, p->lex.pend);
8849  }
8850  }
8851  lex_goto_eol(p);
8852  dispatch_scan_event(p, tCOMMENT);
8853  fallthru = TRUE;
8854  /* fall through */
8855  case '\n':
8856  p->token_seen = token_seen;
8857  c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
8858  !IS_lex_state(EXPR_LABELED));
8859  if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
8860  if (!fallthru) {
8861  dispatch_scan_event(p, tIGNORED_NL);
8862  }
8863  fallthru = FALSE;
8864  if (!c && p->in_kwarg) {
8865  goto normal_newline;
8866  }
8867  goto retry;
8868  }
8869  while (1) {
8870  switch (c = nextc(p)) {
8871  case ' ': case '\t': case '\f': case '\r':
8872  case '\13': /* '\v' */
8873  space_seen = 1;
8874  break;
8875  case '#':
8876  pushback(p, c);
8877  if (space_seen) dispatch_scan_event(p, tSP);
8878  goto retry;
8879  case '&':
8880  case '.': {
8881  dispatch_delayed_token(p, tIGNORED_NL);
8882  if (peek(p, '.') == (c == '&')) {
8883  pushback(p, c);
8884  dispatch_scan_event(p, tSP);
8885  goto retry;
8886  }
8887  }
8888  default:
8889  p->ruby_sourceline--;
8890  p->lex.nextline = p->lex.lastline;
8891  case -1: /* EOF no decrement*/
8892 #ifndef RIPPER
8893  if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
8894  p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
8895  p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
8896  pushback(p, 1); /* always pushback */
8897  p->lex.ptok = p->lex.pcur;
8898 #else
8899  lex_goto_eol(p);
8900  if (c != -1) {
8901  p->lex.ptok = p->lex.pcur;
8902  }
8903 #endif
8904  goto normal_newline;
8905  }
8906  }
8907  normal_newline:
8908  p->command_start = TRUE;
8909  SET_LEX_STATE(EXPR_BEG);
8910  return '\n';
8911 
8912  case '*':
8913  if ((c = nextc(p)) == '*') {
8914  if ((c = nextc(p)) == '=') {
8915  set_yylval_id(idPow);
8916  SET_LEX_STATE(EXPR_BEG);
8917  return tOP_ASGN;
8918  }
8919  pushback(p, c);
8920  if (IS_SPCARG(c)) {
8921  rb_warning0("`**' interpreted as argument prefix");
8922  c = tDSTAR;
8923  }
8924  else if (IS_BEG()) {
8925  c = tDSTAR;
8926  }
8927  else {
8928  c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
8929  }
8930  }
8931  else {
8932  if (c == '=') {
8933  set_yylval_id('*');
8934  SET_LEX_STATE(EXPR_BEG);
8935  return tOP_ASGN;
8936  }
8937  pushback(p, c);
8938  if (IS_SPCARG(c)) {
8939  rb_warning0("`*' interpreted as argument prefix");
8940  c = tSTAR;
8941  }
8942  else if (IS_BEG()) {
8943  c = tSTAR;
8944  }
8945  else {
8946  c = warn_balanced('*', "*", "argument prefix");
8947  }
8948  }
8949  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8950  return c;
8951 
8952  case '!':
8953  c = nextc(p);
8954  if (IS_AFTER_OPERATOR()) {
8955  SET_LEX_STATE(EXPR_ARG);
8956  if (c == '@') {
8957  return '!';
8958  }
8959  }
8960  else {
8961  SET_LEX_STATE(EXPR_BEG);
8962  }
8963  if (c == '=') {
8964  return tNEQ;
8965  }
8966  if (c == '~') {
8967  return tNMATCH;
8968  }
8969  pushback(p, c);
8970  return '!';
8971 
8972  case '=':
8973  if (was_bol(p)) {
8974  /* skip embedded rd document */
8975  if (word_match_p(p, "begin", 5)) {
8976  int first_p = TRUE;
8977 
8978  lex_goto_eol(p);
8979  dispatch_scan_event(p, tEMBDOC_BEG);
8980  for (;;) {
8981  lex_goto_eol(p);
8982  if (!first_p) {
8983  dispatch_scan_event(p, tEMBDOC);
8984  }
8985  first_p = FALSE;
8986  c = nextc(p);
8987  if (c == -1) {
8988  compile_error(p, "embedded document meets end of file");
8989  return 0;
8990  }
8991  if (c == '=' && word_match_p(p, "end", 3)) {
8992  break;
8993  }
8994  pushback(p, c);
8995  }
8996  lex_goto_eol(p);
8997  dispatch_scan_event(p, tEMBDOC_END);
8998  goto retry;
8999  }
9000  }
9001 
9002  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9003  if ((c = nextc(p)) == '=') {
9004  if ((c = nextc(p)) == '=') {
9005  return tEQQ;
9006  }
9007  pushback(p, c);
9008  return tEQ;
9009  }
9010  if (c == '~') {
9011  return tMATCH;
9012  }
9013  else if (c == '>') {
9014  return tASSOC;
9015  }
9016  pushback(p, c);
9017  return '=';
9018 
9019  case '<':
9020  c = nextc(p);
9021  if (c == '<' &&
9022  !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9023  !IS_END() &&
9024  (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9025  int token = heredoc_identifier(p);
9026  if (token) return token < 0 ? 0 : token;
9027  }
9028  if (IS_AFTER_OPERATOR()) {
9029  SET_LEX_STATE(EXPR_ARG);
9030  }
9031  else {
9032  if (IS_lex_state(EXPR_CLASS))
9033  p->command_start = TRUE;
9034  SET_LEX_STATE(EXPR_BEG);
9035  }
9036  if (c == '=') {
9037  if ((c = nextc(p)) == '>') {
9038  return tCMP;
9039  }
9040  pushback(p, c);
9041  return tLEQ;
9042  }
9043  if (c == '<') {
9044  if ((c = nextc(p)) == '=') {
9045  set_yylval_id(idLTLT);
9046  SET_LEX_STATE(EXPR_BEG);
9047  return tOP_ASGN;
9048  }
9049  pushback(p, c);
9050  return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9051  }
9052  pushback(p, c);
9053  return '<';
9054 
9055  case '>':
9056  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9057  if ((c = nextc(p)) == '=') {
9058  return tGEQ;
9059  }
9060  if (c == '>') {
9061  if ((c = nextc(p)) == '=') {
9062  set_yylval_id(idGTGT);
9063  SET_LEX_STATE(EXPR_BEG);
9064  return tOP_ASGN;
9065  }
9066  pushback(p, c);
9067  return tRSHFT;
9068  }
9069  pushback(p, c);
9070  return '>';
9071 
9072  case '"':
9073  label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9074  p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9075  p->lex.ptok = p->lex.pcur-1;
9076  return tSTRING_BEG;
9077 
9078  case '`':
9079  if (IS_lex_state(EXPR_FNAME)) {
9080  SET_LEX_STATE(EXPR_ENDFN);
9081  return c;
9082  }
9083  if (IS_lex_state(EXPR_DOT)) {
9084  if (cmd_state)
9085  SET_LEX_STATE(EXPR_CMDARG);
9086  else
9087  SET_LEX_STATE(EXPR_ARG);
9088  return c;
9089  }
9090  p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9091  return tXSTRING_BEG;
9092 
9093  case '\'':
9094  label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9095  p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9096  p->lex.ptok = p->lex.pcur-1;
9097  return tSTRING_BEG;
9098 
9099  case '?':
9100  return parse_qmark(p, space_seen);
9101 
9102  case '&':
9103  if ((c = nextc(p)) == '&') {
9104  SET_LEX_STATE(EXPR_BEG);
9105  if ((c = nextc(p)) == '=') {
9106  set_yylval_id(idANDOP);
9107  SET_LEX_STATE(EXPR_BEG);
9108  return tOP_ASGN;
9109  }
9110  pushback(p, c);
9111  return tANDOP;
9112  }
9113  else if (c == '=') {
9114  set_yylval_id('&');
9115  SET_LEX_STATE(EXPR_BEG);
9116  return tOP_ASGN;
9117  }
9118  else if (c == '.') {
9119  set_yylval_id(idANDDOT);
9120  SET_LEX_STATE(EXPR_DOT);
9121  return tANDDOT;
9122  }
9123  pushback(p, c);
9124  if (IS_SPCARG(c)) {
9125  if ((c != ':') ||
9126  (c = peekc_n(p, 1)) == -1 ||
9127  !(c == '\'' || c == '"' ||
9128  is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9129  rb_warning0("`&' interpreted as argument prefix");
9130  }
9131  c = tAMPER;
9132  }
9133  else if (IS_BEG()) {
9134  c = tAMPER;
9135  }
9136  else {
9137  c = warn_balanced('&', "&", "argument prefix");
9138  }
9139  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9140  return c;
9141 
9142  case '|':
9143  if ((c = nextc(p)) == '|') {
9144  SET_LEX_STATE(EXPR_BEG);
9145  if ((c = nextc(p)) == '=') {
9146  set_yylval_id(idOROP);
9147  SET_LEX_STATE(EXPR_BEG);
9148  return tOP_ASGN;
9149  }
9150  pushback(p, c);
9151  if (IS_lex_state_for(last_state, EXPR_BEG)) {
9152  c = '|';
9153  pushback(p, '|');
9154  return c;
9155  }
9156  return tOROP;
9157  }
9158  if (c == '=') {
9159  set_yylval_id('|');
9160  SET_LEX_STATE(EXPR_BEG);
9161  return tOP_ASGN;
9162  }
9163  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9164  pushback(p, c);
9165  return '|';
9166 
9167  case '+':
9168  c = nextc(p);
9169  if (IS_AFTER_OPERATOR()) {
9170  SET_LEX_STATE(EXPR_ARG);
9171  if (c == '@') {
9172  return tUPLUS;
9173  }
9174  pushback(p, c);
9175  return '+';
9176  }
9177  if (c == '=') {
9178  set_yylval_id('+');
9179  SET_LEX_STATE(EXPR_BEG);
9180  return tOP_ASGN;
9181  }
9182  if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9183  SET_LEX_STATE(EXPR_BEG);
9184  pushback(p, c);
9185  if (c != -1 && ISDIGIT(c)) {
9186  return parse_numeric(p, '+');
9187  }
9188  return tUPLUS;
9189  }
9190  SET_LEX_STATE(EXPR_BEG);
9191  pushback(p, c);
9192  return warn_balanced('+', "+", "unary operator");
9193 
9194  case '-':
9195  c = nextc(p);
9196  if (IS_AFTER_OPERATOR()) {
9197  SET_LEX_STATE(EXPR_ARG);
9198  if (c == '@') {
9199  return tUMINUS;
9200  }
9201  pushback(p, c);
9202  return '-';
9203  }
9204  if (c == '=') {
9205  set_yylval_id('-');
9206  SET_LEX_STATE(EXPR_BEG);
9207  return tOP_ASGN;
9208  }
9209  if (c == '>') {
9210  SET_LEX_STATE(EXPR_ENDFN);
9211  return tLAMBDA;
9212  }
9213  if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9214  SET_LEX_STATE(EXPR_BEG);
9215  pushback(p, c);
9216  if (c != -1 && ISDIGIT(c)) {
9217  return tUMINUS_NUM;
9218  }
9219  return tUMINUS;
9220  }
9221  SET_LEX_STATE(EXPR_BEG);
9222  pushback(p, c);
9223  return warn_balanced('-', "-", "unary operator");
9224 
9225  case '.': {
9226  int is_beg = IS_BEG();
9227  SET_LEX_STATE(EXPR_BEG);
9228  if ((c = nextc(p)) == '.') {
9229  if ((c = nextc(p)) == '.') {
9230  if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9231  rb_warn0("... at EOL, should be parenthesized?");
9232  }
9233  return is_beg ? tBDOT3 : tDOT3;
9234  }
9235  pushback(p, c);
9236  return is_beg ? tBDOT2 : tDOT2;
9237  }
9238  pushback(p, c);
9239  if (c != -1 && ISDIGIT(c)) {
9240  char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9241  parse_numeric(p, '.');
9242  if (ISDIGIT(prev)) {
9243  yyerror0("unexpected fraction part after numeric literal");
9244  }
9245  else {
9246  yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9247  }
9248  SET_LEX_STATE(EXPR_END);
9249  p->lex.ptok = p->lex.pcur;
9250  goto retry;
9251  }
9252  set_yylval_id('.');
9253  SET_LEX_STATE(EXPR_DOT);
9254  return '.';
9255  }
9256 
9257  case '0': case '1': case '2': case '3': case '4':
9258  case '5': case '6': case '7': case '8': case '9':
9259  return parse_numeric(p, c);
9260 
9261  case ')':
9262  COND_POP();
9263  CMDARG_POP();
9264  SET_LEX_STATE(EXPR_ENDFN);
9265  p->lex.paren_nest--;
9266  return c;
9267 
9268  case ']':
9269  COND_POP();
9270  CMDARG_POP();
9271  SET_LEX_STATE(EXPR_END);
9272  p->lex.paren_nest--;
9273  return c;
9274 
9275  case '}':
9276  /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9277  if (!p->lex.brace_nest--) return tSTRING_DEND;
9278  COND_POP();
9279  CMDARG_POP();
9280  SET_LEX_STATE(EXPR_END);
9281  p->lex.paren_nest--;
9282  return c;
9283 
9284  case ':':
9285  c = nextc(p);
9286  if (c == ':') {
9287  if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9288  SET_LEX_STATE(EXPR_BEG);
9289  return tCOLON3;
9290  }
9291  set_yylval_id(idCOLON2);
9292  SET_LEX_STATE(EXPR_DOT);
9293  return tCOLON2;
9294  }
9295  if (IS_END() || ISSPACE(c) || c == '#') {
9296  pushback(p, c);
9297  c = warn_balanced(':', ":", "symbol literal");
9298  SET_LEX_STATE(EXPR_BEG);
9299  return c;
9300  }
9301  switch (c) {
9302  case '\'':
9303  p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9304  break;
9305  case '"':
9306  p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9307  break;
9308  default:
9309  pushback(p, c);
9310  break;
9311  }
9312  SET_LEX_STATE(EXPR_FNAME);
9313  return tSYMBEG;
9314 
9315  case '/':
9316  if (IS_BEG()) {
9317  p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9318  return tREGEXP_BEG;
9319  }
9320  if ((c = nextc(p)) == '=') {
9321  set_yylval_id('/');
9322  SET_LEX_STATE(EXPR_BEG);
9323  return tOP_ASGN;
9324  }
9325  pushback(p, c);
9326  if (IS_SPCARG(c)) {
9327  arg_ambiguous(p, '/');
9328  p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9329  return tREGEXP_BEG;
9330  }
9331  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9332  return warn_balanced('/', "/", "regexp literal");
9333 
9334  case '^':
9335  if ((c = nextc(p)) == '=') {
9336  set_yylval_id('^');
9337  SET_LEX_STATE(EXPR_BEG);
9338  return tOP_ASGN;
9339  }
9340  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9341  pushback(p, c);
9342  return '^';
9343 
9344  case ';':
9345  SET_LEX_STATE(EXPR_BEG);
9346  p->command_start = TRUE;
9347  return ';';
9348 
9349  case ',':
9350  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9351  return ',';
9352 
9353  case '~':
9354  if (IS_AFTER_OPERATOR()) {
9355  if ((c = nextc(p)) != '@') {
9356  pushback(p, c);
9357  }
9358  SET_LEX_STATE(EXPR_ARG);
9359  }
9360  else {
9361  SET_LEX_STATE(EXPR_BEG);
9362  }
9363  return '~';
9364 
9365  case '(':
9366  if (IS_BEG()) {
9367  c = tLPAREN;
9368  }
9369  else if (!space_seen) {
9370  /* foo( ... ) => method call, no ambiguity */
9371  }
9372  else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9373  c = tLPAREN_ARG;
9374  }
9375  else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9376  rb_warning0("parentheses after method name is interpreted as "
9377  "an argument list, not a decomposed argument");
9378  }
9379  p->lex.paren_nest++;
9380  COND_PUSH(0);
9381  CMDARG_PUSH(0);
9382  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9383  return c;
9384 
9385  case '[':
9386  p->lex.paren_nest++;
9387  if (IS_AFTER_OPERATOR()) {
9388  if ((c = nextc(p)) == ']') {
9389  SET_LEX_STATE(EXPR_ARG);
9390  if ((c = nextc(p)) == '=') {
9391  return tASET;
9392  }
9393  pushback(p, c);
9394  return tAREF;
9395  }
9396  pushback(p, c);
9397  SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9398  return '[';
9399  }
9400  else if (IS_BEG()) {
9401  c = tLBRACK;
9402  }
9403  else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9404  c = tLBRACK;
9405  }
9406  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9407  COND_PUSH(0);
9408  CMDARG_PUSH(0);
9409  return c;
9410 
9411  case '{':
9412  ++p->lex.brace_nest;
9413  if (lambda_beginning_p())
9414  c = tLAMBEG;
9415  else if (IS_lex_state(EXPR_LABELED))
9416  c = tLBRACE; /* hash */
9417  else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9418  c = '{'; /* block (primary) */
9419  else if (IS_lex_state(EXPR_ENDARG))
9420  c = tLBRACE_ARG; /* block (expr) */
9421  else
9422  c = tLBRACE; /* hash */
9423  if (c != tLBRACE) {
9424  p->command_start = TRUE;
9425  SET_LEX_STATE(EXPR_BEG);
9426  }
9427  else {
9428  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9429  }
9430  ++p->lex.paren_nest; /* after lambda_beginning_p() */
9431  COND_PUSH(0);
9432  CMDARG_PUSH(0);
9433  return c;
9434 
9435  case '\\':
9436  c = nextc(p);
9437  if (c == '\n') {
9438  space_seen = 1;
9439  dispatch_scan_event(p, tSP);
9440  goto retry; /* skip \\n */
9441  }
9442  if (c == ' ') return tSP;
9443  if (ISSPACE(c)) return c;
9444  pushback(p, c);
9445  return '\\';
9446 
9447  case '%':
9448  return parse_percent(p, space_seen, last_state);
9449 
9450  case '$':
9451  return parse_gvar(p, last_state);
9452 
9453  case '@':
9454  return parse_atmark(p, last_state);
9455 
9456  case '_':
9457  if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9458  p->ruby__end__seen = 1;
9459  p->eofp = 1;
9460 #ifndef RIPPER
9461  return -1;
9462 #else
9463  lex_goto_eol(p);
9464  dispatch_scan_event(p, k__END__);
9465  return 0;
9466 #endif
9467  }
9468  newtok(p);
9469  break;
9470 
9471  default:
9472  if (!parser_is_identchar(p)) {
9473  compile_error(p, "Invalid char `\\x%02X' in expression", c);
9474  token_flush(p);
9475  goto retry;
9476  }
9477 
9478  newtok(p);
9479  break;
9480  }
9481 
9482  return parse_ident(p, c, cmd_state);
9483 }
9484 
9485 static enum yytokentype
9486 yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9487 {
9488  enum yytokentype t;
9489 
9490  p->lval = lval;
9491  lval->val = Qundef;
9492  t = parser_yylex(p);
9493 
9494  if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9495  RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9496  else
9497  RUBY_SET_YYLLOC(*yylloc);
9498 
9499  if (has_delayed_token(p))
9500  dispatch_delayed_token(p, t);
9501  else if (t != 0)
9502  dispatch_scan_event(p, t);
9503 
9504  return t;
9505 }
9506 
9507 #define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9508 
9509 static NODE*
9510 node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9511 {
9512  NODE *n = rb_ast_newnode(p->ast, type);
9513 
9514  rb_node_init(n, type, a0, a1, a2);
9515 
9516  nd_set_loc(n, loc);
9517  nd_set_node_id(n, parser_get_node_id(p));
9518  return n;
9519 }
9520 
9521 static NODE *
9522 nd_set_loc(NODE *nd, const YYLTYPE *loc)
9523 {
9524  nd->nd_loc = *loc;
9525  nd_set_line(nd, loc->beg_pos.lineno);
9526  return nd;
9527 }
9528 
9529 #ifndef RIPPER
9530 static enum node_type
9531 nodetype(NODE *node) /* for debug */
9532 {
9533  return (enum node_type)nd_type(node);
9534 }
9535 
9536 static int
9537 nodeline(NODE *node)
9538 {
9539  return nd_line(node);
9540 }
9541 
9542 static NODE*
9543 newline_node(NODE *node)
9544 {
9545  if (node) {
9546  node = remove_begin(node);
9547  node->flags |= NODE_FL_NEWLINE;
9548  }
9549  return node;
9550 }
9551 
9552 static void
9553 fixpos(NODE *node, NODE *orig)
9554 {
9555  if (!node) return;
9556  if (!orig) return;
9557  nd_set_line(node, nd_line(orig));
9558 }
9559 
9560 static void
9561 parser_warning(struct parser_params *p, NODE *node, const char *mesg)
9562 {
9563  rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9564 }
9565 
9566 static void
9567 parser_warn(struct parser_params *p, NODE *node, const char *mesg)
9568 {
9569  rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9570 }
9571 
9572 static NODE*
9573 block_append(struct parser_params *p, NODE *head, NODE *tail)
9574 {
9575  NODE *end, *h = head, *nd;
9576 
9577  if (tail == 0) return head;
9578 
9579  if (h == 0) return tail;
9580  switch (nd_type(h)) {
9581  case NODE_LIT:
9582  case NODE_STR:
9583  case NODE_SELF:
9584  case NODE_TRUE:
9585  case NODE_FALSE:
9586  case NODE_NIL:
9587  parser_warning(p, h, "unused literal ignored");
9588  return tail;
9589  default:
9590  h = end = NEW_BLOCK(head, &head->nd_loc);
9591  end->nd_end = end;
9592  head = end;
9593  break;
9594  case NODE_BLOCK:
9595  end = h->nd_end;
9596  break;
9597  }
9598 
9599  nd = end->nd_head;
9600  switch (nd_type(nd)) {
9601  case NODE_RETURN:
9602  case NODE_BREAK:
9603  case NODE_NEXT:
9604  case NODE_REDO:
9605  case NODE_RETRY:
9606  if (RTEST(ruby_verbose)) {
9607  parser_warning(p, tail, "statement not reached");
9608  }
9609  break;
9610 
9611  default:
9612  break;
9613  }
9614 
9615  if (nd_type(tail) != NODE_BLOCK) {
9616  tail = NEW_BLOCK(tail, &tail->nd_loc);
9617  tail->nd_end = tail;
9618  }
9619  end->nd_next = tail;
9620  h->nd_end = tail->nd_end;
9621  nd_set_last_loc(head, nd_last_loc(tail));
9622  return head;
9623 }
9624 
9625 /* append item to the list */
9626 static NODE*
9627 list_append(struct parser_params *p, NODE *list, NODE *item)
9628 {
9629  NODE *last;
9630 
9631  if (list == 0) return NEW_LIST(item, &item->nd_loc);
9632  if (list->nd_next) {
9633  last = list->nd_next->nd_end;
9634  }
9635  else {
9636  last = list;
9637  }
9638 
9639  list->nd_alen += 1;
9640  last->nd_next = NEW_LIST(item, &item->nd_loc);
9641  list->nd_next->nd_end = last->nd_next;
9642 
9643  nd_set_last_loc(list, nd_last_loc(item));
9644 
9645  return list;
9646 }
9647 
9648 /* concat two lists */
9649 static NODE*
9650 list_concat(NODE *head, NODE *tail)
9651 {
9652  NODE *last;
9653 
9654  if (head->nd_next) {
9655  last = head->nd_next->nd_end;
9656  }
9657  else {
9658  last = head;
9659  }
9660 
9661  head->nd_alen += tail->nd_alen;
9662  last->nd_next = tail;
9663  if (tail->nd_next) {
9664  head->nd_next->nd_end = tail->nd_next->nd_end;
9665  }
9666  else {
9667  head->nd_next->nd_end = tail;
9668  }
9669 
9670  nd_set_last_loc(head, nd_last_loc(tail));
9671 
9672  return head;
9673 }
9674 
9675 static int
9676 literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
9677 {
9678  if (NIL_P(tail)) return 1;
9679  if (!rb_enc_compatible(head, tail)) {
9680  compile_error(p, "string literal encodings differ (%s / %s)",
9681  rb_enc_name(rb_enc_get(head)),
9682  rb_enc_name(rb_enc_get(tail)));
9683  rb_str_resize(head, 0);
9684  rb_str_resize(tail, 0);
9685  return 0;
9686  }
9687  rb_str_buf_append(head, tail);
9688  return 1;
9689 }
9690 
9691 /* concat two string literals */
9692 static NODE *
9693 literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
9694 {
9695  enum node_type htype;
9696  NODE *headlast;
9697  VALUE lit;
9698 
9699  if (!head) return tail;
9700  if (!tail) return head;
9701 
9702  htype = nd_type(head);
9703  if (htype == NODE_EVSTR) {
9704  NODE *node = NEW_DSTR(STR_NEW0(), loc);
9705  RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9706  head = list_append(p, node, head);
9707  htype = NODE_DSTR;
9708  }
9709  if (p->heredoc_indent > 0) {
9710  switch (htype) {
9711  case NODE_STR:
9712  nd_set_type(head, NODE_DSTR);
9713  case NODE_DSTR:
9714  return list_append(p, head, tail);
9715  default:
9716  break;
9717  }
9718  }
9719  switch (nd_type(tail)) {
9720  case NODE_STR:
9721  if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9722  nd_type(headlast) == NODE_STR) {
9723  htype = NODE_STR;
9724  lit = headlast->nd_lit;
9725  }
9726  else {
9727  lit = head->nd_lit;
9728  }
9729  if (htype == NODE_STR) {
9730  if (!literal_concat0(p, lit, tail->nd_lit)) {
9731  error:
9732  rb_discard_node(p, head);
9733  rb_discard_node(p, tail);
9734  return 0;
9735  }
9736  rb_discard_node(p, tail);
9737  }
9738  else {
9739  list_append(p, head, tail);
9740  }
9741  break;
9742 
9743  case NODE_DSTR:
9744  if (htype == NODE_STR) {
9745  if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
9746  goto error;
9747  tail->nd_lit = head->nd_lit;
9748  rb_discard_node(p, head);
9749  head = tail;
9750  }
9751  else if (NIL_P(tail->nd_lit)) {
9752  append:
9753  head->nd_alen += tail->nd_alen - 1;
9754  head->nd_next->nd_end->nd_next = tail->nd_next;
9755  head->nd_next->nd_end = tail->nd_next->nd_end;
9756  rb_discard_node(p, tail);
9757  }
9758  else if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9759  nd_type(headlast) == NODE_STR) {
9760  lit = headlast->nd_lit;
9761  if (!literal_concat0(p, lit, tail->nd_lit))
9762  goto error;
9763  tail->nd_lit = Qnil;
9764  goto append;
9765  }
9766  else {
9767  list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
9768  }
9769  break;
9770 
9771  case NODE_EVSTR:
9772  if (htype == NODE_STR) {
9773  nd_set_type(head, NODE_DSTR);
9774  head->nd_alen = 1;
9775  }
9776  list_append(p, head, tail);
9777  break;
9778  }
9779  return head;
9780 }
9781 
9782 static NODE *
9783 evstr2dstr(struct parser_params *p, NODE *node)
9784 {
9785  if (nd_type(node) == NODE_EVSTR) {
9786  NODE * dstr = NEW_DSTR(STR_NEW0(), &node->nd_loc);
9787  RB_OBJ_WRITTEN(p->ast, Qnil, dstr->nd_lit);
9788  node = list_append(p, dstr, node);
9789  }
9790  return node;
9791 }
9792 
9793 static NODE *
9794 new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
9795 {
9796  NODE *head = node;
9797 
9798  if (node) {
9799  switch (nd_type(node)) {
9800  case NODE_STR: case NODE_DSTR: case NODE_EVSTR:
9801  return node;
9802  }
9803  }
9804  return NEW_EVSTR(head, loc);
9805 }
9806 
9807 static NODE *
9808 call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
9809  const YYLTYPE *op_loc, const YYLTYPE *loc)
9810 {
9811  NODE *expr;
9812  value_expr(recv);
9813  value_expr(arg1);
9814  expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
9815  nd_set_line(expr, op_loc->beg_pos.lineno);
9816  return expr;
9817 }
9818 
9819 static NODE *
9820 call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
9821 {
9822  NODE *opcall;
9823  value_expr(recv);
9824  opcall = NEW_OPCALL(recv, id, 0, loc);
9825  nd_set_line(opcall, op_loc->beg_pos.lineno);
9826  return opcall;
9827 }
9828 
9829 static NODE *
9830 new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
9831 {
9832  NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
9833  nd_set_line(qcall, op_loc->beg_pos.lineno);
9834  return qcall;
9835 }
9836 
9837 static NODE*
9838 new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
9839 {
9840  NODE *ret;
9841  if (block) block_dup_check(p, args, block);
9842  ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
9843  if (block) ret = method_add_block(p, ret, block, loc);
9844  fixpos(ret, recv);
9845  return ret;
9846 }
9847 
9848 #define nd_once_body(node) (nd_type(node) == NODE_ONCE ? (node)->nd_body : node)
9849 static NODE*
9850 match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
9851 {
9852  NODE *n;
9853  int line = op_loc->beg_pos.lineno;
9854 
9855  value_expr(node1);
9856  value_expr(node2);
9857  if (node1 && (n = nd_once_body(node1)) != 0) {
9858  switch (nd_type(n)) {
9859  case NODE_DREGX:
9860  {
9861  NODE *match = NEW_MATCH2(node1, node2, loc);
9862  nd_set_line(match, line);
9863  return match;
9864  }
9865 
9866  case NODE_LIT:
9867  if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
9868  const VALUE lit = n->nd_lit;
9869  NODE *match = NEW_MATCH2(node1, node2, loc);
9870  match->nd_args = reg_named_capture_assign(p, lit, loc);
9871  nd_set_line(match, line);
9872  return match;
9873  }
9874  }
9875  }
9876 
9877  if (node2 && (n = nd_once_body(node2)) != 0) {
9878  NODE *match3;
9879 
9880  switch (nd_type(n)) {
9881  case NODE_LIT:
9882  if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
9883  /* fallthru */
9884  case NODE_DREGX:
9885  match3 = NEW_MATCH3(node2, node1, loc);
9886  return match3;
9887  }
9888  }
9889 
9890  n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
9891  nd_set_line(n, line);
9892  return n;
9893 }
9894 
9895 # if WARN_PAST_SCOPE
9896 static int
9897 past_dvar_p(struct parser_params *p, ID id)
9898 {
9899  struct vtable *past = p->lvtbl->past;
9900  while (past) {
9901  if (vtable_included(past, id)) return 1;
9902  past = past->prev;
9903  }
9904  return 0;
9905 }
9906 # endif
9907 
9908 /* As Ripper#warn does not have arguments for the location, so the
9909  * following messages cannot be separated */
9910 #define WARN_LOCATION(type) do { \
9911  if (p->warn_location) { \
9912  int line; \
9913  VALUE file = rb_source_location(&line); \
9914  rb_warn3(type" in eval may not return location in binding;" \
9915  " use Binding#source_location instead\n" \
9916  "%"PRIsWARN":%d: warning: in `%"PRIsWARN"'", \
9917  file, WARN_I(line), rb_id2str(rb_frame_this_func())); \
9918  } \
9919 } while (0)
9920 
9921 static int
9922 numparam_nested_p(struct parser_params *p)
9923 {
9924  struct local_vars *local = p->lvtbl;
9925  NODE *outer = local->numparam.outer;
9926  NODE *inner = local->numparam.inner;
9927  if (outer || inner) {
9928  NODE *used = outer ? outer : inner;
9929  compile_error(p, "numbered parameter is already used in\n"
9930  "%s:%d: %s block here",
9931  p->ruby_sourcefile, nd_line(used),
9932  outer ? "outer" : "inner");
9933  parser_show_error_line(p, &used->nd_loc);
9934  return 1;
9935  }
9936  return 0;
9937 }
9938 
9939 static NODE*
9940 gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
9941 {
9942  ID *vidp = NULL;
9943  NODE *node;
9944  switch (id) {
9945  case keyword_self:
9946  return NEW_SELF(loc);
9947  case keyword_nil:
9948  return NEW_NIL(loc);
9949  case keyword_true:
9950  return NEW_TRUE(loc);
9951  case keyword_false:
9952  return NEW_FALSE(loc);
9953  case keyword__FILE__:
9954  WARN_LOCATION("__FILE__");
9955  {
9956  VALUE file = p->ruby_sourcefile_string;
9957  if (NIL_P(file))
9958  file = rb_str_new(0, 0);
9959  else
9960  file = rb_str_dup(file);
9961  node = NEW_STR(file, loc);
9962  RB_OBJ_WRITTEN(p->ast, Qnil, file);
9963  }
9964  return node;
9965  case keyword__LINE__:
9966  WARN_LOCATION("__LINE__");
9967  return NEW_LIT(INT2FIX(p->tokline), loc);
9968  case keyword__ENCODING__:
9969  node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
9970  RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9971  return node;
9972 
9973  }
9974  switch (id_type(id)) {
9975  case ID_LOCAL:
9976  if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
9977  if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
9978  if (id == p->cur_arg) {
9979  compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
9980  return 0;
9981  }
9982  if (vidp) *vidp |= LVAR_USED;
9983  node = NEW_DVAR(id, loc);
9984  return node;
9985  }
9986  if (local_id_ref(p, id, &vidp)) {
9987  if (id == p->cur_arg) {
9988  compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
9989  return 0;
9990  }
9991  if (vidp) *vidp |= LVAR_USED;
9992  node = NEW_LVAR(id, loc);
9993  return node;
9994  }
9995  if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
9996  parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
9997  if (numparam_nested_p(p)) return 0;
9998  node = NEW_DVAR(id, loc);
9999  struct local_vars *local = p->lvtbl;
10000  if (!local->numparam.current) local->numparam.current = node;
10001  return node;
10002  }
10003 # if WARN_PAST_SCOPE
10004  if (!p->in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10005  rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10006  }
10007 # endif
10008  /* method call without arguments */
10009  return NEW_VCALL(id, loc);
10010  case ID_GLOBAL:
10011  return NEW_GVAR(id, loc);
10012  case ID_INSTANCE:
10013  return NEW_IVAR(id, loc);
10014  case ID_CONST:
10015  return NEW_CONST(id, loc);
10016  case ID_CLASS:
10017  return NEW_CVAR(id, loc);
10018  }
10019  compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10020  return 0;
10021 }
10022 
10023 static NODE *
10024 opt_arg_append(NODE *opt_list, NODE *opt)
10025 {
10026  NODE *opts = opt_list;
10027  opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10028 
10029  while (opts->nd_next) {
10030  opts = opts->nd_next;
10031  opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10032  }
10033  opts->nd_next = opt;
10034 
10035  return opt_list;
10036 }
10037 
10038 static NODE *
10039 kwd_append(NODE *kwlist, NODE *kw)
10040 {
10041  if (kwlist) {
10042  NODE *kws = kwlist;
10043  kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10044  while (kws->nd_next) {
10045  kws = kws->nd_next;
10046  kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10047  }
10048  kws->nd_next = kw;
10049  }
10050  return kwlist;
10051 }
10052 
10053 static NODE *
10054 new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10055 {
10056  return NEW_DEFINED(remove_begin_all(expr), loc);
10057 }
10058 
10059 static NODE*
10060 symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10061 {
10062  if (nd_type(symbol) == NODE_DSTR) {
10063  nd_set_type(symbol, NODE_DSYM);
10064  }
10065  else {
10066  nd_set_type(symbol, NODE_LIT);
10067  RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10068  }
10069  return list_append(p, symbols, symbol);
10070 }
10071 
10072 static NODE *
10073 new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10074 {
10075  NODE *list, *prev;
10076  VALUE lit;
10077 
10078  if (!node) {
10079  node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10080  RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10081  return node;
10082  }
10083  switch (nd_type(node)) {
10084  case NODE_STR:
10085  {
10086  VALUE src = node->nd_lit;
10087  nd_set_type(node, NODE_LIT);
10088  nd_set_loc(node, loc);
10089  RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10090  }
10091  break;
10092  default:
10093  lit = STR_NEW0();
10094  node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10095  RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10096  /* fall through */
10097  case NODE_DSTR:
10098  nd_set_type(node, NODE_DREGX);
10099  nd_set_loc(node, loc);
10100  node->nd_cflag = options & RE_OPTION_MASK;
10101  if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10102  for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10103  if (nd_type(list->nd_head) == NODE_STR) {
10104  VALUE tail = list->nd_head->nd_lit;
10105  if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10106  VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10107  if (!literal_concat0(p, lit, tail)) {
10108  return NEW_NIL(loc); /* dummy node on error */
10109  }
10110  rb_str_resize(tail, 0);
10111  prev->nd_next = list->nd_next;
10112  rb_discard_node(p, list->nd_head);
10113  rb_discard_node(p, list);
10114  list = prev;
10115  }
10116  else {
10117  prev = list;
10118  }
10119  }
10120  else {
10121  prev = 0;
10122  }
10123  }
10124  if (!node->nd_next) {
10125  VALUE src = node->nd_lit;
10126  nd_set_type(node, NODE_LIT);
10127  RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10128  }
10129  if (options & RE_OPTION_ONCE) {
10130  node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10131  }
10132  break;
10133  }
10134  return node;
10135 }
10136 
10137 static NODE *
10138 new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10139 {
10140  if (!k) return 0;
10141  return NEW_KW_ARG(0, (k), loc);
10142 }
10143 
10144 static NODE *
10145 new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10146 {
10147  if (!node) {
10148  VALUE lit = STR_NEW0();
10149  NODE *xstr = NEW_XSTR(lit, loc);
10150  RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10151  return xstr;
10152  }
10153  switch (nd_type(node)) {
10154  case NODE_STR:
10155  nd_set_type(node, NODE_XSTR);
10156  nd_set_loc(node, loc);
10157  break;
10158  case NODE_DSTR:
10159  nd_set_type(node, NODE_DXSTR);
10160  nd_set_loc(node, loc);
10161  break;
10162  default:
10163  node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10164  break;
10165  }
10166  return node;
10167 }
10168 
10169 static void
10170 check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10171 {
10172  VALUE lit;
10173 
10174  if (!arg || !p->case_labels) return;
10175 
10176  lit = rb_node_case_when_optimizable_literal(arg);
10177  if (lit == Qundef) return;
10178  if (nd_type(arg) == NODE_STR) {
10179  RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10180  }
10181 
10182  if (NIL_P(p->case_labels)) {
10183  p->case_labels = rb_obj_hide(rb_hash_new());
10184  }
10185  else {
10186  VALUE line = rb_hash_lookup(p->case_labels, lit);
10187  if (!NIL_P(line)) {
10188  rb_warning1("duplicated `when' clause with line %d is ignored",
10189  WARN_IVAL(line));
10190  return;
10191  }
10192  }
10193  rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10194 }
10195 
10196 #else /* !RIPPER */
10197 static int
10198 id_is_var(struct parser_params *p, ID id)
10199 {
10200  if (is_notop_id(id)) {
10201  switch (id & ID_SCOPE_MASK) {
10202  case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10203  return 1;
10204  case ID_LOCAL:
10205  if (dyna_in_block(p)) {
10206  if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10207  }
10208  if (local_id(p, id)) return 1;
10209  /* method call without arguments */
10210  return 0;
10211  }
10212  }
10213  compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10214  return 0;
10215 }
10216 
10217 static VALUE
10218 new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10219 {
10220  VALUE src = 0, err;
10221  int options = 0;
10222  if (ripper_is_node_yylval(re)) {
10223  src = RNODE(re)->nd_cval;
10224  re = RNODE(re)->nd_rval;
10225  }
10226  if (ripper_is_node_yylval(opt)) {
10227  options = (int)RNODE(opt)->nd_tag;
10228  opt = RNODE(opt)->nd_rval;
10229  }
10230  if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10231  compile_error(p, "%"PRIsVALUE, err);
10232  }
10233  return dispatch2(regexp_literal, re, opt);
10234 }
10235 #endif /* !RIPPER */
10236 
10237 
10238 #ifndef RIPPER
10239 static const char rb_parser_lex_state_names[][8] = {
10240  "BEG", "END", "ENDARG", "ENDFN", "ARG",
10241  "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10242  "LABEL", "LABELED","FITEM",
10243 };
10244 
10245 static VALUE
10246 append_lex_state_name(enum lex_state_e state, VALUE buf)
10247 {
10248  int i, sep = 0;
10249  unsigned int mask = 1;
10250  static const char none[] = "NONE";
10251 
10252  for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10253  if ((unsigned)state & mask) {
10254  if (sep) {
10255  rb_str_cat(buf, "|", 1);
10256  }
10257  sep = 1;
10258  rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10259  }
10260  }
10261  if (!sep) {
10262  rb_str_cat(buf, none, sizeof(none)-1);
10263  }
10264  return buf;
10265 }
10266 
10267 static void
10268 flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10269 {
10270  VALUE mesg = p->debug_buffer;
10271 
10272  if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10273  p->debug_buffer = Qnil;
10274  rb_io_puts(1, &mesg, out);
10275  }
10276  if (!NIL_P(str) && RSTRING_LEN(str)) {
10277  rb_io_write(p->debug_output, str);
10278  }
10279 }
10280 
10281 enum lex_state_e
10282 rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10283  enum lex_state_e to, int line)
10284 {
10285  VALUE mesg;
10286  mesg = rb_str_new_cstr("lex_state: ");
10287  append_lex_state_name(from, mesg);
10288  rb_str_cat_cstr(mesg, " -> ");
10289  append_lex_state_name(to, mesg);
10290  rb_str_catf(mesg, " at line %d\n", line);
10291  flush_debug_buffer(p, p->debug_output, mesg);
10292  return to;
10293 }
10294 
10295 VALUE
10296 rb_parser_lex_state_name(enum lex_state_e state)
10297 {
10298  return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10299 }
10300 
10301 static void
10302 append_bitstack_value(stack_type stack, VALUE mesg)
10303 {
10304  if (stack == 0) {
10305  rb_str_cat_cstr(mesg, "0");
10306  }
10307  else {
10308  stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10309  for (; mask && !(stack & mask); mask >>= 1) continue;
10310  for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10311  }
10312 }
10313 
10314 void
10315 rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10316  const char *name, int line)
10317 {
10318  VALUE mesg = rb_sprintf("%s: ", name);
10319  append_bitstack_value(stack, mesg);
10320  rb_str_catf(mesg, " at line %d\n", line);
10321  flush_debug_buffer(p, p->debug_output, mesg);
10322 }
10323 
10324 void
10325 rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10326 {
10327  va_list ap;
10328  VALUE mesg = rb_str_new_cstr("internal parser error: ");
10329 
10330  va_start(ap, fmt);
10331  rb_str_vcatf(mesg, fmt, ap);
10332  va_end(ap);
10333  parser_yyerror(p, NULL, RSTRING_PTR(mesg));
10334  RB_GC_GUARD(mesg);
10335 
10336  mesg = rb_str_new(0, 0);
10337  append_lex_state_name(p->lex.state, mesg);
10338  compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10339  rb_str_resize(mesg, 0);
10340  append_bitstack_value(p->cond_stack, mesg);
10341  compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10342  rb_str_resize(mesg, 0);
10343  append_bitstack_value(p->cmdarg_stack, mesg);
10344  compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10345  if (p->debug_output == rb_stdout)
10346  p->debug_output = rb_stderr;
10347  p->debug = TRUE;
10348 }
10349 
10350 YYLTYPE *
10351 rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10352 {
10353  int sourceline = here->sourceline;
10354  int beg_pos = (int)here->offset - here->quote
10355  - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10356  int end_pos = (int)here->offset + here->length + here->quote;
10357 
10358  yylloc->beg_pos.lineno = sourceline;
10359  yylloc->beg_pos.column = beg_pos;
10360  yylloc->end_pos.lineno = sourceline;
10361  yylloc->end_pos.column = end_pos;
10362  return yylloc;
10363 }
10364 
10365 YYLTYPE *
10366 rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10367 {
10368  yylloc->beg_pos.lineno = p->ruby_sourceline;
10369  yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10370  yylloc->end_pos.lineno = p->ruby_sourceline;
10371  yylloc->end_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10372  return yylloc;
10373 }
10374 
10375 YYLTYPE *
10376 rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10377 {
10378  yylloc->beg_pos.lineno = p->ruby_sourceline;
10379  yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10380  yylloc->end_pos.lineno = p->ruby_sourceline;
10381  yylloc->end_pos.column = (int)(p->lex.pcur - p->lex.pbeg);
10382  return yylloc;
10383 }
10384 #endif /* !RIPPER */
10385 
10386 static void
10387 parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp)
10388 {
10389  VALUE v;
10390 
10391  switch (type) {
10392  case tIDENTIFIER: case tFID: case tGVAR: case tIVAR:
10393  case tCONSTANT: case tCVAR: case tLABEL: case tOP_ASGN:
10394 #ifndef RIPPER
10395  v = rb_id2str(valp->id);
10396 #else
10397  v = valp->node->nd_rval;
10398 #endif
10399  rb_parser_printf(p, "%"PRIsVALUE, v);
10400  break;
10401  case tINTEGER: case tFLOAT: case tRATIONAL: case tIMAGINARY:
10402  case tSTRING_CONTENT: case tCHAR:
10403 #ifndef RIPPER
10404  v = valp->node->nd_lit;
10405 #else
10406  v = valp->val;
10407 #endif
10408  rb_parser_printf(p, "%+"PRIsVALUE, v);
10409  break;
10410  case tNTH_REF:
10411 #ifndef RIPPER
10412  rb_parser_printf(p, "$%ld", valp->node->nd_nth);
10413 #else
10414  rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10415 #endif
10416  break;
10417  case tBACK_REF:
10418 #ifndef RIPPER
10419  rb_parser_printf(p, "$%c", (int)valp->node->nd_nth);
10420 #else
10421  rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10422 #endif
10423  break;
10424  default:
10425  break;
10426  }
10427 }
10428 
10429 static int
10430 assignable0(struct parser_params *p, ID id, const char **err)
10431 {
10432  if (!id) return -1;
10433  switch (id) {
10434  case keyword_self:
10435  *err = "Can't change the value of self";
10436  return -1;
10437  case keyword_nil:
10438  *err = "Can't assign to nil";
10439  return -1;
10440  case keyword_true:
10441  *err = "Can't assign to true";
10442  return -1;
10443  case keyword_false:
10444  *err = "Can't assign to false";
10445  return -1;
10446  case keyword__FILE__:
10447  *err = "Can't assign to __FILE__";
10448  return -1;
10449  case keyword__LINE__:
10450  *err = "Can't assign to __LINE__";
10451  return -1;
10452  case keyword__ENCODING__:
10453  *err = "Can't assign to __ENCODING__";
10454  return -1;
10455  }
10456  switch (id_type(id)) {
10457  case ID_LOCAL:
10458  if (dyna_in_block(p)) {
10459  if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10460  compile_error(p, "Can't assign to numbered parameter _%d",
10461  NUMPARAM_ID_TO_IDX(id));
10462  return -1;
10463  }
10464  if (dvar_curr(p, id)) return NODE_DASGN_CURR;
10465  if (dvar_defined(p, id)) return NODE_DASGN;
10466  if (local_id(p, id)) return NODE_LASGN;
10467  dyna_var(p, id);
10468  return NODE_DASGN_CURR;
10469  }
10470  else {
10471  if (!local_id(p, id)) local_var(p, id);
10472  return NODE_LASGN;
10473  }
10474  break;
10475  case ID_GLOBAL: return NODE_GASGN;
10476  case ID_INSTANCE: return NODE_IASGN;
10477  case ID_CONST:
10478  if (!p->in_def) return NODE_CDECL;
10479  *err = "dynamic constant assignment";
10480  return -1;
10481  case ID_CLASS: return NODE_CVASGN;
10482  default:
10483  compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10484  }
10485  return -1;
10486 }
10487 
10488 #ifndef RIPPER
10489 static NODE*
10490 assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10491 {
10492  const char *err = 0;
10493  int node_type = assignable0(p, id, &err);
10494  switch (node_type) {
10495  case NODE_DASGN_CURR: return NEW_DASGN_CURR(id, val, loc);
10496  case NODE_DASGN: return NEW_DASGN(id, val, loc);
10497  case NODE_LASGN: return NEW_LASGN(id, val, loc);
10498  case NODE_GASGN: return NEW_GASGN(id, val, loc);
10499  case NODE_IASGN: return NEW_IASGN(id, val, loc);
10500  case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10501  case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10502  }
10503  if (err) yyerror1(loc, err);
10504  return NEW_BEGIN(0, loc);
10505 }
10506 #else
10507 static VALUE
10508 assignable(struct parser_params *p, VALUE lhs)
10509 {
10510  const char *err = 0;
10511  assignable0(p, get_id(lhs), &err);
10512  if (err) lhs = assign_error(p, lhs);
10513  return lhs;
10514 }
10515 #endif
10516 
10517 static int
10518 is_private_local_id(ID name)
10519 {
10520  VALUE s;
10521  if (name == idUScore) return 1;
10522  if (!is_local_id(name)) return 0;
10523  s = rb_id2str(name);
10524  if (!s) return 0;
10525  return RSTRING_PTR(s)[0] == '_';
10526 }
10527 
10528 static int
10529 shadowing_lvar_0(struct parser_params *p, ID name)
10530 {
10531  if (is_private_local_id(name)) return 1;
10532  if (dyna_in_block(p)) {
10533  if (dvar_curr(p, name)) {
10534  yyerror0("duplicated argument name");
10535  }
10536  else if (dvar_defined(p, name) || local_id(p, name)) {
10537  vtable_add(p->lvtbl->vars, name);
10538  if (p->lvtbl->used) {
10539  vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
10540  }
10541  return 0;
10542  }
10543  }
10544  else {
10545  if (local_id(p, name)) {
10546  yyerror0("duplicated argument name");
10547  }
10548  }
10549  return 1;
10550 }
10551 
10552 static ID
10553 shadowing_lvar(struct parser_params *p, ID name)
10554 {
10555  shadowing_lvar_0(p, name);
10556  return name;
10557 }
10558 
10559 static void
10560 new_bv(struct parser_params *p, ID name)
10561 {
10562  if (!name) return;
10563  if (!is_local_id(name)) {
10564  compile_error(p, "invalid local variable - %"PRIsVALUE,
10565  rb_id2str(name));
10566  return;
10567  }
10568  if (!shadowing_lvar_0(p, name)) return;
10569  dyna_var(p, name);
10570 }
10571 
10572 #ifndef RIPPER
10573 static NODE *
10574 aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
10575 {
10576  return NEW_ATTRASGN(recv, tASET, idx, loc);
10577 }
10578 
10579 static void
10580 block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
10581 {
10582  if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
10583  compile_error(p, "both block arg and actual block given");
10584  }
10585 }
10586 
10587 static NODE *
10588 attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
10589 {
10590  if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
10591  return NEW_ATTRASGN(recv, id, 0, loc);
10592 }
10593 
10594 static void
10595 rb_backref_error(struct parser_params *p, NODE *node)
10596 {
10597  switch (nd_type(node)) {
10598  case NODE_NTH_REF:
10599  compile_error(p, "Can't set variable $%ld", node->nd_nth);
10600  break;
10601  case NODE_BACK_REF:
10602  compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
10603  break;
10604  }
10605 }
10606 
10607 static NODE *
10608 arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10609 {
10610  if (!node1) return NEW_LIST(node2, &node2->nd_loc);
10611  switch (nd_type(node1)) {
10612  case NODE_LIST:
10613  return list_append(p, node1, node2);
10614  case NODE_BLOCK_PASS:
10615  node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
10616  node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
10617  return node1;
10618  case NODE_ARGSPUSH:
10619  node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
10620  node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10621  nd_set_type(node1, NODE_ARGSCAT);
10622  return node1;
10623  case NODE_ARGSCAT:
10624  if (nd_type(node1->nd_body) != NODE_LIST) break;
10625  node1->nd_body = list_append(p, node1->nd_body, node2);
10626  node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10627  return node1;
10628  }
10629  return NEW_ARGSPUSH(node1, node2, loc);
10630 }
10631 
10632 static NODE *
10633 arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10634 {
10635  if (!node2) return node1;
10636  switch (nd_type(node1)) {
10637  case NODE_BLOCK_PASS:
10638  if (node1->nd_head)
10639  node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
10640  else
10641  node1->nd_head = NEW_LIST(node2, loc);
10642  return node1;
10643  case NODE_ARGSPUSH:
10644  if (nd_type(node2) != NODE_LIST) break;
10645  node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
10646  nd_set_type(node1, NODE_ARGSCAT);
10647  return node1;
10648  case NODE_ARGSCAT:
10649  if (nd_type(node2) != NODE_LIST ||
10650  nd_type(node1->nd_body) != NODE_LIST) break;
10651  node1->nd_body = list_concat(node1->nd_body, node2);
10652  return node1;
10653  }
10654  return NEW_ARGSCAT(node1, node2, loc);
10655 }
10656 
10657 static NODE *
10658 last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
10659 {
10660  NODE *n1;
10661  if ((n1 = splat_array(args)) != 0) {
10662  return list_append(p, n1, last_arg);
10663  }
10664  return arg_append(p, args, last_arg, loc);
10665 }
10666 
10667 static NODE *
10668 rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
10669 {
10670  NODE *n1;
10671  if ((nd_type(rest_arg) == NODE_LIST) && (n1 = splat_array(args)) != 0) {
10672  return list_concat(n1, rest_arg);
10673  }
10674  return arg_concat(p, args, rest_arg, loc);
10675 }
10676 
10677 static NODE *
10678 splat_array(NODE* node)
10679 {
10680  if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
10681  if (nd_type(node) == NODE_LIST) return node;
10682  return 0;
10683 }
10684 
10685 static void
10686 mark_lvar_used(struct parser_params *p, NODE *rhs)
10687 {
10688  ID *vidp = NULL;
10689  if (!rhs) return;
10690  switch (nd_type(rhs)) {
10691  case NODE_LASGN:
10692  if (local_id_ref(p, rhs->nd_vid, &vidp)) {
10693  if (vidp) *vidp |= LVAR_USED;
10694  }
10695  break;
10696  case NODE_DASGN:
10697  case NODE_DASGN_CURR:
10698  if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
10699  if (vidp) *vidp |= LVAR_USED;
10700  }
10701  break;
10702 #if 0
10703  case NODE_MASGN:
10704  for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
10705  mark_lvar_used(p, rhs->nd_head);
10706  }
10707  break;
10708 #endif
10709  }
10710 }
10711 
10712 static NODE *
10713 node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, const YYLTYPE *loc)
10714 {
10715  if (!lhs) return 0;
10716 
10717  switch (nd_type(lhs)) {
10718  case NODE_GASGN:
10719  case NODE_IASGN:
10720  case NODE_LASGN:
10721  case NODE_DASGN:
10722  case NODE_DASGN_CURR:
10723  case NODE_MASGN:
10724  case NODE_CDECL:
10725  case NODE_CVASGN:
10726  lhs->nd_value = rhs;
10727  nd_set_loc(lhs, loc);
10728  break;
10729 
10730  case NODE_ATTRASGN:
10731  lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
10732  nd_set_loc(lhs, loc);
10733  break;
10734 
10735  default:
10736  /* should not happen */
10737  break;
10738  }
10739 
10740  return lhs;
10741 }
10742 
10743 static NODE *
10744 value_expr_check(struct parser_params *p, NODE *node)
10745 {
10746  NODE *void_node = 0, *vn;
10747 
10748  if (!node) {
10749  rb_warning0("empty expression");
10750  }
10751  while (node) {
10752  switch (nd_type(node)) {
10753  case NODE_RETURN:
10754  case NODE_BREAK:
10755  case NODE_NEXT:
10756  case NODE_REDO:
10757  case NODE_RETRY:
10758  return void_node ? void_node : node;
10759 
10760  case NODE_CASE3:
10761  if (!node->nd_body || nd_type(node->nd_body) != NODE_IN) {
10762  compile_error(p, "unexpected node");
10763  return NULL;
10764  }
10765  if (node->nd_body->nd_body) {
10766  return NULL;
10767  }
10768  /* single line pattern matching */
10769  return void_node ? void_node : node;
10770 
10771  case NODE_BLOCK:
10772  while (node->nd_next) {
10773  node = node->nd_next;
10774  }
10775  node = node->nd_head;
10776  break;
10777 
10778  case NODE_BEGIN:
10779  node = node->nd_body;
10780  break;
10781 
10782  case NODE_IF:
10783  case NODE_UNLESS:
10784  if (!node->nd_body) {
10785  return NULL;
10786  }
10787  else if (!node->nd_else) {
10788  return NULL;
10789  }
10790  vn = value_expr_check(p, node->nd_body);
10791  if (!vn) return NULL;
10792  if (!void_node) void_node = vn;
10793  node = node->nd_else;
10794  break;
10795 
10796  case NODE_AND:
10797  case NODE_OR:
10798  node = node->nd_1st;
10799  break;
10800 
10801  case NODE_LASGN:
10802  case NODE_DASGN:
10803  case NODE_DASGN_CURR:
10804  case NODE_MASGN:
10805  mark_lvar_used(p, node);
10806  return NULL;
10807 
10808  default:
10809  return NULL;
10810  }
10811  }
10812 
10813  return NULL;
10814 }
10815 
10816 static int
10817 value_expr_gen(struct parser_params *p, NODE *node)
10818 {
10819  NODE *void_node = value_expr_check(p, node);
10820  if (void_node) {
10821  yyerror1(&void_node->nd_loc, "void value expression");
10822  /* or "control never reach"? */
10823  return FALSE;
10824  }
10825  return TRUE;
10826 }
10827 static void
10828 void_expr(struct parser_params *p, NODE *node)
10829 {
10830  const char *useless = 0;
10831 
10832  if (!RTEST(ruby_verbose)) return;
10833 
10834  if (!node || !(node = nd_once_body(node))) return;
10835  switch (nd_type(node)) {
10836  case NODE_OPCALL:
10837  switch (node->nd_mid) {
10838  case '+':
10839  case '-':
10840  case '*':
10841  case '/':
10842  case '%':
10843  case tPOW:
10844  case tUPLUS:
10845  case tUMINUS:
10846  case '|':
10847  case '^':
10848  case '&':
10849  case tCMP:
10850  case '>':
10851  case tGEQ:
10852  case '<':
10853  case tLEQ:
10854  case tEQ:
10855  case tNEQ:
10856  useless = rb_id2name(node->nd_mid);
10857  break;
10858  }
10859  break;
10860 
10861  case NODE_LVAR:
10862  case NODE_DVAR:
10863  case NODE_GVAR:
10864  case NODE_IVAR:
10865  case NODE_CVAR:
10866  case NODE_NTH_REF:
10867  case NODE_BACK_REF:
10868  useless = "a variable";
10869  break;
10870  case NODE_CONST:
10871  useless = "a constant";
10872  break;
10873  case NODE_LIT:
10874  case NODE_STR:
10875  case NODE_DSTR:
10876  case NODE_DREGX:
10877  useless = "a literal";
10878  break;
10879  case NODE_COLON2:
10880  case NODE_COLON3:
10881  useless = "::";
10882  break;
10883  case NODE_DOT2:
10884  useless = "..";
10885  break;
10886  case NODE_DOT3:
10887  useless = "...";
10888  break;
10889  case NODE_SELF:
10890  useless = "self";
10891  break;
10892  case NODE_NIL:
10893  useless = "nil";
10894  break;
10895  case NODE_TRUE:
10896  useless = "true";
10897  break;
10898  case NODE_FALSE:
10899  useless = "false";
10900  break;
10901  case NODE_DEFINED:
10902  useless = "defined?";
10903  break;
10904  }
10905 
10906  if (useless) {
10907  rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
10908  }
10909 }
10910 
10911 static NODE *
10912 void_stmts(struct parser_params *p, NODE *node)
10913 {
10914  NODE *const n = node;
10915  if (!RTEST(ruby_verbose)) return n;
10916  if (!node) return n;
10917  if (nd_type(node) != NODE_BLOCK) return n;
10918 
10919  while (node->nd_next) {
10920  void_expr(p, node->nd_head);
10921  node = node->nd_next;
10922  }
10923  return n;
10924 }
10925 
10926 static NODE *
10927 remove_begin(NODE *node)
10928 {
10929  NODE **n = &node, *n1 = node;
10930  while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
10931  *n = n1 = n1->nd_body;
10932  }
10933  return node;
10934 }
10935 
10936 static NODE *
10937 remove_begin_all(NODE *node)
10938 {
10939  NODE **n = &node, *n1 = node;
10940  while (n1 && nd_type(n1) == NODE_BEGIN) {
10941  *n = n1 = n1->nd_body;
10942  }
10943  return node;
10944 }
10945 
10946 static void
10947 reduce_nodes(struct parser_params *p, NODE **body)
10948 {
10949  NODE *node = *body;
10950 
10951  if (!node) {
10952  *body = NEW_NIL(&NULL_LOC);
10953  return;
10954  }
10955 #define subnodes(n1, n2) \
10956  ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
10957  (!node->n2) ? (body = &node->n1, 1) : \
10958  (reduce_nodes(p, &node->n1), body = &node->n2, 1))
10959 
10960  while (node) {
10961  int newline = (int)(node->flags & NODE_FL_NEWLINE);
10962  switch (nd_type(node)) {
10963  end:
10964  case NODE_NIL:
10965  *body = 0;
10966  return;
10967  case NODE_RETURN:
10968  *body = node = node->nd_stts;
10969  if (newline && node) node->flags |= NODE_FL_NEWLINE;
10970  continue;
10971  case NODE_BEGIN:
10972  *body = node = node->nd_body;
10973  if (newline && node) node->flags |= NODE_FL_NEWLINE;
10974  continue;
10975  case NODE_BLOCK:
10976  body = &node->nd_end->nd_head;
10977  break;
10978  case NODE_IF:
10979  case NODE_UNLESS:
10980  if (subnodes(nd_body, nd_else)) break;
10981  return;
10982  case NODE_CASE:
10983  body = &node->nd_body;
10984  break;
10985  case NODE_WHEN:
10986  if (!subnodes(nd_body, nd_next)) goto end;
10987  break;
10988  case NODE_ENSURE:
10989  if (!subnodes(nd_head, nd_resq)) goto end;
10990  break;
10991  case NODE_RESCUE:
10992  if (node->nd_else) {
10993  body = &node->nd_resq;
10994  break;
10995  }
10996  if (!subnodes(nd_head, nd_resq)) goto end;
10997  break;
10998  default:
10999  return;
11000  }
11001  node = *body;
11002  if (newline && node) node->flags |= NODE_FL_NEWLINE;
11003  }
11004 
11005 #undef subnodes
11006 }
11007 
11008 static int
11009 is_static_content(NODE *node)
11010 {
11011  if (!node) return 1;
11012  switch (nd_type(node)) {
11013  case NODE_HASH:
11014  if (!(node = node->nd_head)) break;
11015  case NODE_LIST:
11016  do {
11017  if (!is_static_content(node->nd_head)) return 0;
11018  } while ((node = node->nd_next) != 0);
11019  case NODE_LIT:
11020  case NODE_STR:
11021  case NODE_NIL:
11022  case NODE_TRUE:
11023  case NODE_FALSE:
11024  case NODE_ZLIST:
11025  break;
11026  default:
11027  return 0;
11028  }
11029  return 1;
11030 }
11031 
11032 static int
11033 assign_in_cond(struct parser_params *p, NODE *node)
11034 {
11035  switch (nd_type(node)) {
11036  case NODE_MASGN:
11037  case NODE_LASGN:
11038  case NODE_DASGN:
11039  case NODE_DASGN_CURR:
11040  case NODE_GASGN:
11041  case NODE_IASGN:
11042  break;
11043 
11044  default:
11045  return 0;
11046  }
11047 
11048  if (!node->nd_value) return 1;
11049  if (is_static_content(node->nd_value)) {
11050  /* reports always */
11051  parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11052  }
11053  return 1;
11054 }
11055 
11056 enum cond_type {
11057  COND_IN_OP,
11058  COND_IN_COND,
11059  COND_IN_FF
11060 };
11061 
11062 #define SWITCH_BY_COND_TYPE(t, w, arg) \
11063  switch (t) { \
11064  case COND_IN_OP: break; \
11065  case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11066  case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11067  }
11068 
11069 static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11070 
11071 static NODE*
11072 range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11073 {
11074  enum node_type type;
11075 
11076  if (node == 0) return 0;
11077 
11078  type = nd_type(node);
11079  value_expr(node);
11080  if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11081  if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11082  return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(rb_intern("$."), loc), loc), loc);
11083  }
11084  return cond0(p, node, COND_IN_FF, loc);
11085 }
11086 
11087 static NODE*
11088 cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11089 {
11090  if (node == 0) return 0;
11091  if (!(node = nd_once_body(node))) return 0;
11092  assign_in_cond(p, node);
11093 
11094  switch (nd_type(node)) {
11095  case NODE_DSTR:
11096  case NODE_EVSTR:
11097  case NODE_STR:
11098  SWITCH_BY_COND_TYPE(type, warn, "string ")
11099  break;
11100 
11101  case NODE_DREGX:
11102  if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11103 
11104  return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11105 
11106  case NODE_AND:
11107  case NODE_OR:
11108  node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11109  node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11110  break;
11111 
11112  case NODE_DOT2:
11113  case NODE_DOT3:
11114  node->nd_beg = range_op(p, node->nd_beg, loc);
11115  node->nd_end = range_op(p, node->nd_end, loc);
11116  if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
11117  else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
11118  break;
11119 
11120  case NODE_DSYM:
11121  SWITCH_BY_COND_TYPE(type, warning, "string ")
11122  break;
11123 
11124  case NODE_LIT:
11125  if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11126  if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11127  nd_set_type(node, NODE_MATCH);
11128  }
11129  else if (node->nd_lit == Qtrue ||
11130  node->nd_lit == Qfalse) {
11131  /* booleans are OK, e.g., while true */
11132  }
11133  else {
11134  SWITCH_BY_COND_TYPE(type, warning, "")
11135  }
11136  default:
11137  break;
11138  }
11139  return node;
11140 }
11141 
11142 static NODE*
11143 cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11144 {
11145  if (node == 0) return 0;
11146  return cond0(p, node, COND_IN_COND, loc);
11147 }
11148 
11149 static NODE*
11150 method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11151 {
11152  if (node == 0) return 0;
11153  return cond0(p, node, COND_IN_OP, loc);
11154 }
11155 
11156 static NODE*
11157 new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11158 {
11159  if (!cc) return right;
11160  cc = cond0(p, cc, COND_IN_COND, loc);
11161  return newline_node(NEW_IF(cc, left, right, loc));
11162 }
11163 
11164 static NODE*
11165 new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11166 {
11167  if (!cc) return right;
11168  cc = cond0(p, cc, COND_IN_COND, loc);
11169  return newline_node(NEW_UNLESS(cc, left, right, loc));
11170 }
11171 
11172 static NODE*
11173 logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11174  const YYLTYPE *op_loc, const YYLTYPE *loc)
11175 {
11176  enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11177  NODE *op;
11178  value_expr(left);
11179  if (left && (enum node_type)nd_type(left) == type) {
11180  NODE *node = left, *second;
11181  while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
11182  node = second;
11183  }
11184  node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11185  nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11186  left->nd_loc.end_pos = loc->end_pos;
11187  return left;
11188  }
11189  op = NEW_NODE(type, left, right, 0, loc);
11190  nd_set_line(op, op_loc->beg_pos.lineno);
11191  return op;
11192 }
11193 
11194 static void
11195 no_blockarg(struct parser_params *p, NODE *node)
11196 {
11197  if (node && nd_type(node) == NODE_BLOCK_PASS) {
11198  compile_error(p, "block argument should not be given");
11199  }
11200 }
11201 
11202 static NODE *
11203 ret_args(struct parser_params *p, NODE *node)
11204 {
11205  if (node) {
11206  no_blockarg(p, node);
11207  if (nd_type(node) == NODE_LIST) {
11208  if (node->nd_next == 0) {
11209  node = node->nd_head;
11210  }
11211  else {
11212  nd_set_type(node, NODE_VALUES);
11213  }
11214  }
11215  }
11216  return node;
11217 }
11218 
11219 static NODE *
11220 new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11221 {
11222  if (node) no_blockarg(p, node);
11223 
11224  return NEW_YIELD(node, loc);
11225 }
11226 
11227 static VALUE
11228 negate_lit(struct parser_params *p, VALUE lit)
11229 {
11230  if (FIXNUM_P(lit)) {
11231  return LONG2FIX(-FIX2LONG(lit));
11232  }
11233  if (SPECIAL_CONST_P(lit)) {
11234 #if USE_FLONUM
11235  if (FLONUM_P(lit)) {
11236  return DBL2NUM(-RFLOAT_VALUE(lit));
11237  }
11238 #endif
11239  goto unknown;
11240  }
11241  switch (BUILTIN_TYPE(lit)) {
11242  case T_BIGNUM:
11243  BIGNUM_NEGATE(lit);
11244  lit = rb_big_norm(lit);
11245  break;
11246  case T_RATIONAL:
11247  RRATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11248  break;
11249  case T_COMPLEX:
11250  RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11251  RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11252  break;
11253  case T_FLOAT:
11254  RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
11255  break;
11256  unknown:
11257  default:
11258  rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11259  rb_builtin_class_name(lit));
11260  break;
11261  }
11262  return lit;
11263 }
11264 
11265 static NODE *
11266 arg_blk_pass(NODE *node1, NODE *node2)
11267 {
11268  if (node2) {
11269  if (!node1) return node2;
11270  node2->nd_head = node1;
11271  nd_set_first_lineno(node2, nd_first_lineno(node1));
11272  nd_set_first_column(node2, nd_first_column(node1));
11273  return node2;
11274  }
11275  return node1;
11276 }
11277 
11278 static bool
11279 args_info_empty_p(struct rb_args_info *args)
11280 {
11281  if (args->pre_args_num) return false;
11282  if (args->post_args_num) return false;
11283  if (args->rest_arg) return false;
11284  if (args->opt_args) return false;
11285  if (args->block_arg) return false;
11286  if (args->kw_args) return false;
11287  if (args->kw_rest_arg) return false;
11288  return true;
11289 }
11290 
11291 static NODE*
11292 new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
11293 {
11294  int saved_line = p->ruby_sourceline;
11295  struct rb_args_info *args = tail->nd_ainfo;
11296 
11297  args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
11298  args->pre_init = pre_args ? pre_args->nd_next : 0;
11299 
11300  args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
11301  args->post_init = post_args ? post_args->nd_next : 0;
11302  args->first_post_arg = post_args ? post_args->nd_pid : 0;
11303 
11304  args->rest_arg = rest_arg;
11305 
11306  args->opt_args = opt_args;
11307 
11308  args->ruby2_keywords = rest_arg == idFWD_REST;
11309 
11310  p->ruby_sourceline = saved_line;
11311  nd_set_loc(tail, loc);
11312 
11313  return tail;
11314 }
11315 
11316 static NODE*
11317 new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *loc)
11318 {
11319  int saved_line = p->ruby_sourceline;
11320  NODE *node;
11321  VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11322  struct rb_args_info *args = ZALLOC(struct rb_args_info);
11323  rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
11324  args->imemo = tmpbuf;
11325  node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
11326  RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11327  if (p->error_p) return node;
11328 
11329  args->block_arg = block;
11330  args->kw_args = kw_args;
11331 
11332  if (kw_args) {
11333  /*
11334  * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
11335  * variable order: k1, kr1, k2, &b, internal_id, krest
11336  * #=> <reorder>
11337  * variable order: kr1, k1, k2, internal_id, krest, &b
11338  */
11339  ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
11340  struct vtable *vtargs = p->lvtbl->args;
11341  NODE *kwn = kw_args;
11342 
11343  vtable_pop(vtargs, !!block + !!kw_rest_arg);
11344  required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
11345  while (kwn) {
11346  if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
11347  --kw_vars;
11348  --required_kw_vars;
11349  kwn = kwn->nd_next;
11350  }
11351 
11352  for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
11353  ID vid = kwn->nd_body->nd_vid;
11354  if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
11355  *required_kw_vars++ = vid;
11356  }
11357  else {
11358  *kw_vars++ = vid;
11359  }
11360  }
11361 
11362  arg_var(p, kw_bits);
11363  if (kw_rest_arg) arg_var(p, kw_rest_arg);
11364  if (block) arg_var(p, block);
11365 
11366  args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11367  args->kw_rest_arg->nd_cflag = kw_bits;
11368  }
11369  else if (kw_rest_arg == idNil) {
11370  args->no_kwarg = 1;
11371  }
11372  else if (kw_rest_arg) {
11373  args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11374  }
11375 
11376  p->ruby_sourceline = saved_line;
11377  return node;
11378 }
11379 
11380 static NODE *
11381 args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
11382 {
11383  if (max_numparam > NO_PARAM) {
11384  if (!args) {
11385  YYLTYPE loc = RUBY_INIT_YYLLOC();
11386  args = new_args_tail(p, 0, 0, 0, 0);
11387  nd_set_loc(args, &loc);
11388  }
11389  args->nd_ainfo->pre_args_num = max_numparam;
11390  }
11391  return args;
11392 }
11393 
11394 static NODE*
11395 new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
11396 {
11397  struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
11398 
11399  aryptn->nd_pconst = constant;
11400 
11401  if (pre_arg) {
11402  NODE *pre_args = NEW_LIST(pre_arg, loc);
11403  if (apinfo->pre_args) {
11404  apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
11405  }
11406  else {
11407  apinfo->pre_args = pre_args;
11408  }
11409  }
11410  return aryptn;
11411 }
11412 
11413 static NODE*
11414 new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
11415 {
11416  int saved_line = p->ruby_sourceline;
11417  NODE *node;
11418  VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11419  struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
11420  rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
11421  node = NEW_NODE(NODE_ARYPTN, 0, 0, apinfo, loc);
11422  apinfo->imemo = tmpbuf;
11423  RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11424 
11425  apinfo->pre_args = pre_args;
11426 
11427  if (has_rest) {
11428  if (rest_arg) {
11429  apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
11430  }
11431  else {
11432  apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
11433  }
11434  }
11435  else {
11436  apinfo->rest_arg = NULL;
11437  }
11438 
11439  apinfo->post_args = post_args;
11440 
11441  p->ruby_sourceline = saved_line;
11442  return node;
11443 }
11444 
11445 static NODE*
11446 new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
11447 {
11448  hshptn->nd_pconst = constant;
11449  return hshptn;
11450 }
11451 
11452 static NODE*
11453 new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
11454 {
11455  int saved_line = p->ruby_sourceline;
11456  NODE *node, *kw_rest_arg_node;
11457 
11458  if (kw_rest_arg == idNil) {
11459  kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
11460  }
11461  else if (kw_rest_arg) {
11462  kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
11463  }
11464  else {
11465  kw_rest_arg_node = NULL;
11466  }
11467 
11468  node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
11469 
11470  p->ruby_sourceline = saved_line;
11471  return node;
11472 }
11473 
11474 static NODE *
11475 new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc)
11476 {
11477  NODE *node = NEW_CASE3(val, pat, loc);
11478 
11479  if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL))
11480  rb_warn0L(nd_line(node), "Pattern matching is experimental, and the behavior may change in future versions of Ruby!");
11481  return node;
11482 }
11483 
11484 static NODE*
11485 dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11486 {
11487  VALUE lit;
11488 
11489  if (!node) {
11490  return NEW_LIT(ID2SYM(idNULL), loc);
11491  }
11492 
11493  switch (nd_type(node)) {
11494  case NODE_DSTR:
11495  nd_set_type(node, NODE_DSYM);
11496  nd_set_loc(node, loc);
11497  break;
11498  case NODE_STR:
11499  lit = node->nd_lit;
11500  RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
11501  nd_set_type(node, NODE_LIT);
11502  nd_set_loc(node, loc);
11503  break;
11504  default:
11505  node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
11506  break;
11507  }
11508  return node;
11509 }
11510 
11511 static int
11512 append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
11513 {
11514  NODE *node = (NODE *)v;
11515  NODE **result = (NODE **)h;
11516  node->nd_alen = 2;
11517  node->nd_next->nd_end = node->nd_next;
11518  node->nd_next->nd_next = 0;
11519  if (*result)
11520  list_concat(*result, node);
11521  else
11522  *result = node;
11523  return ST_CONTINUE;
11524 }
11525 
11526 static NODE *
11527 remove_duplicate_keys(struct parser_params *p, NODE *hash)
11528 {
11529  st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
11530  NODE *result = 0;
11531  rb_code_location_t loc = hash->nd_loc;
11532  while (hash && hash->nd_head && hash->nd_next) {
11533  NODE *head = hash->nd_head;
11534  NODE *value = hash->nd_next;
11535  NODE *next = value->nd_next;
11536  VALUE key = (VALUE)head;
11537  st_data_t data;
11538  if (nd_type(head) == NODE_LIT &&
11539  st_lookup(literal_keys, (key = head->nd_lit), &data)) {
11540  rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
11541  "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
11542  head->nd_lit, nd_line(head));
11543  head = ((NODE *)data)->nd_next;
11544  head->nd_head = block_append(p, head->nd_head, value->nd_head);
11545  }
11546  else {
11547  st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
11548  }
11549  hash = next;
11550  }
11551  st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
11552  st_free_table(literal_keys);
11553  if (hash) {
11554  if (!result) result = hash;
11555  else list_concat(result, hash);
11556  }
11557  result->nd_loc = loc;
11558  return result;
11559 }
11560 
11561 static NODE *
11562 new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11563 {
11564  if (hash) hash = remove_duplicate_keys(p, hash);
11565  return NEW_HASH(hash, loc);
11566 }
11567 #endif
11568 
11569 static void
11570 error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
11571 {
11572  if (is_private_local_id(id)) {
11573  return;
11574  }
11575  if (st_is_member(p->pvtbl, id)) {
11576  yyerror1(loc, "duplicated variable name");
11577  }
11578  else {
11579  st_insert(p->pvtbl, (st_data_t)id, 0);
11580  }
11581 }
11582 
11583 static void
11584 error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
11585 {
11586  if (!p->pktbl) {
11587  p->pktbl = st_init_numtable();
11588  }
11589  else if (st_is_member(p->pktbl, key)) {
11590  yyerror1(loc, "duplicated key name");
11591  return;
11592  }
11593  st_insert(p->pktbl, (st_data_t)key, 0);
11594 }
11595 
11596 #ifndef RIPPER
11597 static NODE *
11598 new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11599 {
11600  return NEW_HASH(hash, loc);
11601 }
11602 #endif /* !RIPPER */
11603 
11604 #ifndef RIPPER
11605 static NODE *
11606 new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11607 {
11608  NODE *asgn;
11609 
11610  if (lhs) {
11611  ID vid = lhs->nd_vid;
11612  YYLTYPE lhs_loc = lhs->nd_loc;
11613  if (op == tOROP) {
11614  lhs->nd_value = rhs;
11615  nd_set_loc(lhs, loc);
11616  asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
11617  if (is_notop_id(vid)) {
11618  switch (id_type(vid)) {
11619  case ID_GLOBAL:
11620  case ID_INSTANCE:
11621  case ID_CLASS:
11622  asgn->nd_aid = vid;
11623  }
11624  }
11625  }
11626  else if (op == tANDOP) {
11627  lhs->nd_value = rhs;
11628  nd_set_loc(lhs, loc);
11629  asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
11630  }
11631  else {
11632  asgn = lhs;
11633  asgn->nd_value = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
11634  nd_set_loc(asgn, loc);
11635  }
11636  }
11637  else {
11638  asgn = NEW_BEGIN(0, loc);
11639  }
11640  return asgn;
11641 }
11642 
11643 static NODE *
11644 new_ary_op_assign(struct parser_params *p, NODE *ary,
11645  NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
11646 {
11647  NODE *asgn;
11648 
11649  args = make_list(args, args_loc);
11650  if (nd_type(args) == NODE_BLOCK_PASS) {
11651  args = NEW_ARGSCAT(args, rhs, loc);
11652  }
11653  else {
11654  args = arg_concat(p, args, rhs, loc);
11655  }
11656  asgn = NEW_OP_ASGN1(ary, op, args, loc);
11657  fixpos(asgn, ary);
11658  return asgn;
11659 }
11660 
11661 static NODE *
11662 new_attr_op_assign(struct parser_params *p, NODE *lhs,
11663  ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
11664 {
11665  NODE *asgn;
11666 
11667  asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
11668  fixpos(asgn, lhs);
11669  return asgn;
11670 }
11671 
11672 static NODE *
11673 new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11674 {
11675  NODE *asgn;
11676 
11677  if (lhs) {
11678  asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
11679  }
11680  else {
11681  asgn = NEW_BEGIN(0, loc);
11682  }
11683  fixpos(asgn, lhs);
11684  return asgn;
11685 }
11686 
11687 static NODE *
11688 const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
11689 {
11690  if (p->in_def) {
11691  yyerror1(loc, "dynamic constant assignment");
11692  }
11693  return NEW_CDECL(0, 0, (path), loc);
11694 }
11695 #else
11696 static VALUE
11697 const_decl(struct parser_params *p, VALUE path)
11698 {
11699  if (p->in_def) {
11700  path = dispatch1(assign_error, path);
11701  ripper_error(p);
11702  }
11703  return path;
11704 }
11705 
11706 static VALUE
11707 assign_error(struct parser_params *p, VALUE a)
11708 {
11709  a = dispatch1(assign_error, a);
11710  ripper_error(p);
11711  return a;
11712 }
11713 
11714 static VALUE
11715 var_field(struct parser_params *p, VALUE a)
11716 {
11717  return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
11718 }
11719 #endif
11720 
11721 #ifndef RIPPER
11722 static NODE *
11723 new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
11724 {
11725  NODE *result = head;
11726  if (rescue) {
11727  NODE *tmp = rescue_else ? rescue_else : rescue;
11728  YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
11729 
11730  result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
11731  nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
11732  }
11733  else if (rescue_else) {
11734  result = block_append(p, result, rescue_else);
11735  }
11736  if (ensure) {
11737  result = NEW_ENSURE(result, ensure, loc);
11738  }
11739  fixpos(result, head);
11740  return result;
11741 }
11742 #endif
11743 
11744 static void
11745 warn_unused_var(struct parser_params *p, struct local_vars *local)
11746 {
11747  int cnt;
11748 
11749  if (!local->used) return;
11750  cnt = local->used->pos;
11751  if (cnt != local->vars->pos) {
11752  rb_parser_fatal(p, "local->used->pos != local->vars->pos");
11753  }
11754 #ifndef RIPPER
11755  ID *v = local->vars->tbl;
11756  ID *u = local->used->tbl;
11757  for (int i = 0; i < cnt; ++i) {
11758  if (!v[i] || (u[i] & LVAR_USED)) continue;
11759  if (is_private_local_id(v[i])) continue;
11760  rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
11761  }
11762 #endif
11763 }
11764 
11765 static void
11766 local_push(struct parser_params *p, int toplevel_scope)
11767 {
11768  struct local_vars *local;
11769  int inherits_dvars = toplevel_scope && compile_for_eval;
11770  int warn_unused_vars = RTEST(ruby_verbose);
11771 
11772  local = ALLOC(struct local_vars);
11773  local->prev = p->lvtbl;
11774  local->args = vtable_alloc(0);
11775  local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
11776 #ifndef RIPPER
11777  if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
11778  if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
11779  local->numparam.outer = 0;
11780  local->numparam.inner = 0;
11781  local->numparam.current = 0;
11782 #endif
11783  local->used = warn_unused_vars ? vtable_alloc(0) : 0;
11784 
11785 # if WARN_PAST_SCOPE
11786  local->past = 0;
11787 # endif
11788  CMDARG_PUSH(0);
11789  COND_PUSH(0);
11790  p->lvtbl = local;
11791 }
11792 
11793 static void
11794 local_pop(struct parser_params *p)
11795 {
11796  struct local_vars *local = p->lvtbl->prev;
11797  if (p->lvtbl->used) {
11798  warn_unused_var(p, p->lvtbl);
11799  vtable_free(p->lvtbl->used);
11800  }
11801 # if WARN_PAST_SCOPE
11802  while (p->lvtbl->past) {
11803  struct vtable *past = p->lvtbl->past;
11804  p->lvtbl->past = past->prev;
11805  vtable_free(past);
11806  }
11807 # endif
11808  vtable_free(p->lvtbl->args);
11809  vtable_free(p->lvtbl->vars);
11810  CMDARG_POP();
11811  COND_POP();
11812  ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
11813  p->lvtbl = local;
11814 }
11815 
11816 #ifndef RIPPER
11817 static ID*
11818 local_tbl(struct parser_params *p)
11819 {
11820  int cnt_args = vtable_size(p->lvtbl->args);
11821  int cnt_vars = vtable_size(p->lvtbl->vars);
11822  int cnt = cnt_args + cnt_vars;
11823  int i, j;
11824  ID *buf;
11825 
11826  if (cnt <= 0) return 0;
11827  buf = ALLOC_N(ID, cnt + 2);
11828  MEMCPY(buf+1, p->lvtbl->args->tbl, ID, cnt_args);
11829  /* remove IDs duplicated to warn shadowing */
11830  for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) {
11831  ID id = p->lvtbl->vars->tbl[i];
11832  if (!vtable_included(p->lvtbl->args, id)) {
11833  buf[j++] = id;
11834  }
11835  }
11836  if (--j < cnt) {
11837  REALLOC_N(buf, ID, (cnt = j) + 2);
11838  }
11839  buf[0] = cnt;
11840  rb_ast_add_local_table(p->ast, buf);
11841 
11842  return buf;
11843 }
11844 
11845 static NODE*
11846 node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
11847 {
11848  ID *a0;
11849  NODE *n;
11850 
11851  a0 = local_tbl(p);
11852  n = NEW_NODE(type, a0, a1, a2, loc);
11853  return n;
11854 }
11855 
11856 #endif
11857 
11858 static void
11859 numparam_name(struct parser_params *p, ID id)
11860 {
11861  if (!NUMPARAM_ID_P(id)) return;
11862  rb_warn1("`_%d' is reserved for numbered parameter; consider another name",
11863  WARN_I(NUMPARAM_ID_TO_IDX(id)));
11864 }
11865 
11866 static void
11867 arg_var(struct parser_params *p, ID id)
11868 {
11869  numparam_name(p, id);
11870  vtable_add(p->lvtbl->args, id);
11871 }
11872 
11873 static void
11874 local_var(struct parser_params *p, ID id)
11875 {
11876  numparam_name(p, id);
11877  vtable_add(p->lvtbl->vars, id);
11878  if (p->lvtbl->used) {
11879  vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
11880  }
11881 }
11882 
11883 static int
11884 local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
11885 {
11886  struct vtable *vars, *args, *used;
11887 
11888  vars = p->lvtbl->vars;
11889  args = p->lvtbl->args;
11890  used = p->lvtbl->used;
11891 
11892  while (vars && !DVARS_TERMINAL_P(vars->prev)) {
11893  vars = vars->prev;
11894  args = args->prev;
11895  if (used) used = used->prev;
11896  }
11897 
11898  if (vars && vars->prev == DVARS_INHERIT) {
11899  return rb_local_defined(id, p->parent_iseq);
11900  }
11901  else if (vtable_included(args, id)) {
11902  return 1;
11903  }
11904  else {
11905  int i = vtable_included(vars, id);
11906  if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
11907  return i != 0;
11908  }
11909 }
11910 
11911 static int
11912 local_id(struct parser_params *p, ID id)
11913 {
11914  return local_id_ref(p, id, NULL);
11915 }
11916 
11917 static NODE *
11918 numparam_push(struct parser_params *p)
11919 {
11920 #ifndef RIPPER
11921  struct local_vars *local = p->lvtbl;
11922  NODE *inner = local->numparam.inner;
11923  if (!local->numparam.outer) {
11924  local->numparam.outer = local->numparam.current;
11925  }
11926  local->numparam.inner = 0;
11927  local->numparam.current = 0;
11928  return inner;
11929 #else
11930  return 0;
11931 #endif
11932 }
11933 
11934 static void
11935 numparam_pop(struct parser_params *p, NODE *prev_inner)
11936 {
11937 #ifndef RIPPER
11938  struct local_vars *local = p->lvtbl;
11939  if (prev_inner) {
11940  /* prefer first one */
11941  local->numparam.inner = prev_inner;
11942  }
11943  else if (local->numparam.current) {
11944  /* current and inner are exclusive */
11945  local->numparam.inner = local->numparam.current;
11946  }
11947  if (p->max_numparam > NO_PARAM) {
11948  /* current and outer are exclusive */
11949  local->numparam.current = local->numparam.outer;
11950  local->numparam.outer = 0;
11951  }
11952  else {
11953  /* no numbered parameter */
11954  local->numparam.current = 0;
11955  }
11956 #endif
11957 }
11958 
11959 static const struct vtable *
11960 dyna_push(struct parser_params *p)
11961 {
11962  p->lvtbl->args = vtable_alloc(p->lvtbl->args);
11963  p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
11964  if (p->lvtbl->used) {
11965  p->lvtbl->used = vtable_alloc(p->lvtbl->used);
11966  }
11967  return p->lvtbl->args;
11968 }
11969 
11970 static void
11971 dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
11972 {
11973  struct vtable *tmp = *vtblp;
11974  *vtblp = tmp->prev;
11975 # if WARN_PAST_SCOPE
11976  if (p->past_scope_enabled) {
11977  tmp->prev = p->lvtbl->past;
11978  p->lvtbl->past = tmp;
11979  return;
11980  }
11981 # endif
11982  vtable_free(tmp);
11983 }
11984 
11985 static void
11986 dyna_pop_1(struct parser_params *p)
11987 {
11988  struct vtable *tmp;
11989 
11990  if ((tmp = p->lvtbl->used) != 0) {
11991  warn_unused_var(p, p->lvtbl);
11992  p->lvtbl->used = p->lvtbl->used->prev;
11993  vtable_free(tmp);
11994  }
11995  dyna_pop_vtable(p, &p->lvtbl->args);
11996  dyna_pop_vtable(p, &p->lvtbl->vars);
11997 }
11998 
11999 static void
12000 dyna_pop(struct parser_params *p, const struct vtable *lvargs)
12001 {
12002  while (p->lvtbl->args != lvargs) {
12003  dyna_pop_1(p);
12004  if (!p->lvtbl->args) {
12005  struct local_vars *local = p->lvtbl->prev;
12006  ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12007  p->lvtbl = local;
12008  }
12009  }
12010  dyna_pop_1(p);
12011 }
12012 
12013 static int
12014 dyna_in_block(struct parser_params *p)
12015 {
12016  return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12017 }
12018 
12019 static int
12020 dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12021 {
12022  struct vtable *vars, *args, *used;
12023  int i;
12024 
12025  args = p->lvtbl->args;
12026  vars = p->lvtbl->vars;
12027  used = p->lvtbl->used;
12028 
12029  while (!DVARS_TERMINAL_P(vars)) {
12030  if (vtable_included(args, id)) {
12031  return 1;
12032  }
12033  if ((i = vtable_included(vars, id)) != 0) {
12034  if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12035  return 1;
12036  }
12037  args = args->prev;
12038  vars = vars->prev;
12039  if (!vidrefp) used = 0;
12040  if (used) used = used->prev;
12041  }
12042 
12043  if (vars == DVARS_INHERIT) {
12044  return rb_dvar_defined(id, p->parent_iseq);
12045  }
12046 
12047  return 0;
12048 }
12049 
12050 static int
12051 dvar_defined(struct parser_params *p, ID id)
12052 {
12053  return dvar_defined_ref(p, id, NULL);
12054 }
12055 
12056 static int
12057 dvar_curr(struct parser_params *p, ID id)
12058 {
12059  return (vtable_included(p->lvtbl->args, id) ||
12060  vtable_included(p->lvtbl->vars, id));
12061 }
12062 
12063 static void
12064 reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12065 {
12066  compile_error(p,
12067  "regexp encoding option '%c' differs from source encoding '%s'",
12068  c, rb_enc_name(rb_enc_get(str)));
12069 }
12070 
12071 #ifndef RIPPER
12072 int
12073 rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12074 {
12075  int c = RE_OPTION_ENCODING_IDX(options);
12076 
12077  if (c) {
12078  int opt, idx;
12079  rb_char_to_option_kcode(c, &opt, &idx);
12080  if (idx != ENCODING_GET(str) &&
12081  rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12082  goto error;
12083  }
12084  ENCODING_SET(str, idx);
12085  }
12086  else if (RE_OPTION_ENCODING_NONE(options)) {
12087  if (!ENCODING_IS_ASCII8BIT(str) &&
12088  rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12089  c = 'n';
12090  goto error;
12091  }
12092  rb_enc_associate(str, rb_ascii8bit_encoding());
12093  }
12094  else if (p->enc == rb_usascii_encoding()) {
12095  if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12096  /* raise in re.c */
12097  rb_enc_associate(str, rb_usascii_encoding());
12098  }
12099  else {
12100  rb_enc_associate(str, rb_ascii8bit_encoding());
12101  }
12102  }
12103  return 0;
12104 
12105  error:
12106  return c;
12107 }
12108 
12109 static void
12110 reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12111 {
12112  int c = rb_reg_fragment_setenc(p, str, options);
12113  if (c) reg_fragment_enc_error(p, str, c);
12114 }
12115 
12116 static int
12117 reg_fragment_check(struct parser_params* p, VALUE str, int options)
12118 {
12119  VALUE err;
12120  reg_fragment_setenc(p, str, options);
12121  err = rb_reg_check_preprocess(str);
12122  if (err != Qnil) {
12123  err = rb_obj_as_string(err);
12124  compile_error(p, "%"PRIsVALUE, err);
12125  return 0;
12126  }
12127  return 1;
12128 }
12129 
12130 typedef struct {
12131  struct parser_params* parser;
12132  rb_encoding *enc;
12133  NODE *succ_block;
12134  const YYLTYPE *loc;
12135 } reg_named_capture_assign_t;
12136 
12137 static int
12138 reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12139  int back_num, int *back_refs, OnigRegex regex, void *arg0)
12140 {
12141  reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12142  struct parser_params* p = arg->parser;
12143  rb_encoding *enc = arg->enc;
12144  long len = name_end - name;
12145  const char *s = (const char *)name;
12146  ID var;
12147  NODE *node, *succ;
12148 
12149  if (!len) return ST_CONTINUE;
12150  if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len))
12151  return ST_CONTINUE;
12152  if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
12153  return ST_CONTINUE;
12154 
12155  var = intern_cstr(s, len, enc);
12156  node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), arg->loc);
12157  succ = arg->succ_block;
12158  if (!succ) succ = NEW_BEGIN(0, arg->loc);
12159  succ = block_append(p, succ, node);
12160  arg->succ_block = succ;
12161  return ST_CONTINUE;
12162 }
12163 
12164 static NODE *
12165 reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
12166 {
12167  reg_named_capture_assign_t arg;
12168 
12169  arg.parser = p;
12170  arg.enc = rb_enc_get(regexp);
12171  arg.succ_block = 0;
12172  arg.loc = loc;
12173  onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
12174 
12175  if (!arg.succ_block) return 0;
12176  return arg.succ_block->nd_next;
12177 }
12178 
12179 static VALUE
12180 parser_reg_compile(struct parser_params* p, VALUE str, int options)
12181 {
12182  reg_fragment_setenc(p, str, options);
12183  return rb_parser_reg_compile(p, str, options);
12184 }
12185 
12186 VALUE
12187 rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
12188 {
12189  return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
12190 }
12191 
12192 static VALUE
12193 reg_compile(struct parser_params* p, VALUE str, int options)
12194 {
12195  VALUE re;
12196  VALUE err;
12197 
12198  err = rb_errinfo();
12199  re = parser_reg_compile(p, str, options);
12200  if (NIL_P(re)) {
12201  VALUE m = rb_attr_get(rb_errinfo(), idMesg);
12202  rb_set_errinfo(err);
12203  compile_error(p, "%"PRIsVALUE, m);
12204  return Qnil;
12205  }
12206  return re;
12207 }
12208 #else
12209 static VALUE
12210 parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
12211 {
12212  VALUE err = rb_errinfo();
12213  VALUE re;
12214  str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
12215  int c = rb_reg_fragment_setenc(p, str, options);
12216  if (c) reg_fragment_enc_error(p, str, c);
12217  re = rb_parser_reg_compile(p, str, options);
12218  if (NIL_P(re)) {
12219  *errmsg = rb_attr_get(rb_errinfo(), idMesg);
12220  rb_set_errinfo(err);
12221  }
12222  return re;
12223 }
12224 #endif
12225 
12226 #ifndef RIPPER
12227 void
12228 rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
12229 {
12230  struct parser_params *p;
12231  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12232  p->do_print = print;
12233  p->do_loop = loop;
12234  p->do_chomp = chomp;
12235  p->do_split = split;
12236 }
12237 
12238 void
12239 rb_parser_warn_location(VALUE vparser, int warn)
12240 {
12241  struct parser_params *p;
12242  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12243  p->warn_location = warn;
12244 }
12245 
12246 static NODE *
12247 parser_append_options(struct parser_params *p, NODE *node)
12248 {
12249  static const YYLTYPE default_location = {{1, 0}, {1, 0}};
12250  const YYLTYPE *const LOC = &default_location;
12251 
12252  if (p->do_print) {
12253  NODE *print = NEW_FCALL(rb_intern("print"),
12254  NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
12255  LOC);
12256  node = block_append(p, node, print);
12257  }
12258 
12259  if (p->do_loop) {
12260  if (p->do_split) {
12261  NODE *args = NEW_LIST(NEW_GVAR(rb_intern("$;"), LOC), LOC);
12262  NODE *split = NEW_GASGN(rb_intern("$F"),
12263  NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12264  rb_intern("split"), args, LOC),
12265  LOC);
12266  node = block_append(p, split, node);
12267  }
12268  if (p->do_chomp) {
12269  NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12270  rb_intern("chomp!"), 0, LOC);
12271  node = block_append(p, chomp, node);
12272  }
12273 
12274  node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
12275  }
12276 
12277  return node;
12278 }
12279 
12280 void
12281 rb_init_parse(void)
12282 {
12283  /* just to suppress unused-function warnings */
12284  (void)nodetype;
12285  (void)nodeline;
12286 }
12287 
12288 static ID
12289 internal_id(struct parser_params *p)
12290 {
12291  const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
12292  ID id = (ID)vtable_size(p->lvtbl->args) + (ID)vtable_size(p->lvtbl->vars);
12293  id = max_id - id;
12294  return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
12295 }
12296 #endif /* !RIPPER */
12297 
12298 static void
12299 parser_initialize(struct parser_params *p)
12300 {
12301  /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
12302  p->command_start = TRUE;
12303  p->ruby_sourcefile_string = Qnil;
12304  p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
12305  p->node_id = 0;
12306 #ifdef RIPPER
12307  p->delayed.token = Qnil;
12308  p->result = Qnil;
12309  p->parsing_thread = Qnil;
12310 #else
12311  p->error_buffer = Qfalse;
12312 #endif
12313  p->debug_buffer = Qnil;
12314  p->debug_output = rb_stdout;
12315  p->enc = rb_utf8_encoding();
12316 }
12317 
12318 #ifdef RIPPER
12319 #define parser_mark ripper_parser_mark
12320 #define parser_free ripper_parser_free
12321 #endif
12322 
12323 static void
12324 parser_mark(void *ptr)
12325 {
12326  struct parser_params *p = (struct parser_params*)ptr;
12327 
12328  rb_gc_mark(p->lex.input);
12329  rb_gc_mark(p->lex.prevline);
12330  rb_gc_mark(p->lex.lastline);
12331  rb_gc_mark(p->lex.nextline);
12332  rb_gc_mark(p->ruby_sourcefile_string);
12333  rb_gc_mark((VALUE)p->lex.strterm);
12334  rb_gc_mark((VALUE)p->ast);
12335  rb_gc_mark(p->case_labels);
12336 #ifndef RIPPER
12337  rb_gc_mark(p->debug_lines);
12338  rb_gc_mark(p->compile_option);
12339  rb_gc_mark(p->error_buffer);
12340 #else
12341  rb_gc_mark(p->delayed.token);
12342  rb_gc_mark(p->value);
12343  rb_gc_mark(p->result);
12344  rb_gc_mark(p->parsing_thread);
12345 #endif
12346  rb_gc_mark(p->debug_buffer);
12347  rb_gc_mark(p->debug_output);
12348 #ifdef YYMALLOC
12349  rb_gc_mark((VALUE)p->heap);
12350 #endif
12351 }
12352 
12353 static void
12354 parser_free(void *ptr)
12355 {
12356  struct parser_params *p = (struct parser_params*)ptr;
12357  struct local_vars *local, *prev;
12358 
12359  if (p->tokenbuf) {
12360  ruby_sized_xfree(p->tokenbuf, p->toksiz);
12361  }
12362  for (local = p->lvtbl; local; local = prev) {
12363  if (local->vars) xfree(local->vars);
12364  prev = local->prev;
12365  xfree(local);
12366  }
12367  {
12368  token_info *ptinfo;
12369  while ((ptinfo = p->token_info) != 0) {
12370  p->token_info = ptinfo->next;
12371  xfree(ptinfo);
12372  }
12373  }
12374  xfree(ptr);
12375 }
12376 
12377 static size_t
12378 parser_memsize(const void *ptr)
12379 {
12380  struct parser_params *p = (struct parser_params*)ptr;
12381  struct local_vars *local;
12382  size_t size = sizeof(*p);
12383 
12384  size += p->toksiz;
12385  for (local = p->lvtbl; local; local = local->prev) {
12386  size += sizeof(*local);
12387  if (local->vars) size += local->vars->capa * sizeof(ID);
12388  }
12389  return size;
12390 }
12391 
12392 static const rb_data_type_t parser_data_type = {
12393 #ifndef RIPPER
12394  "parser",
12395 #else
12396  "ripper",
12397 #endif
12398  {
12399  parser_mark,
12400  parser_free,
12401  parser_memsize,
12402  },
12403  0, 0, RUBY_TYPED_FREE_IMMEDIATELY
12404 };
12405 
12406 #ifndef RIPPER
12407 #undef rb_reserved_word
12408 
12409 const struct kwtable *
12410 rb_reserved_word(const char *str, unsigned int len)
12411 {
12412  return reserved_word(str, len);
12413 }
12414 
12415 VALUE
12416 rb_parser_new(void)
12417 {
12418  struct parser_params *p;
12419  VALUE parser = TypedData_Make_Struct(0, struct parser_params,
12420  &parser_data_type, p);
12421  parser_initialize(p);
12422  return parser;
12423 }
12424 
12425 VALUE
12426 rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
12427 {
12428  struct parser_params *p;
12429 
12430  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12431  p->error_buffer = main ? Qfalse : Qnil;
12432  p->parent_iseq = base;
12433  return vparser;
12434 }
12435 #endif
12436 
12437 #ifdef RIPPER
12438 #define rb_parser_end_seen_p ripper_parser_end_seen_p
12439 #define rb_parser_encoding ripper_parser_encoding
12440 #define rb_parser_get_yydebug ripper_parser_get_yydebug
12441 #define rb_parser_set_yydebug ripper_parser_set_yydebug
12442 #define rb_parser_get_debug_output ripper_parser_get_debug_output
12443 #define rb_parser_set_debug_output ripper_parser_set_debug_output
12444 static VALUE ripper_parser_end_seen_p(VALUE vparser);
12445 static VALUE ripper_parser_encoding(VALUE vparser);
12446 static VALUE ripper_parser_get_yydebug(VALUE self);
12447 static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
12448 static VALUE ripper_parser_get_debug_output(VALUE self);
12449 static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
12450 
12451 /*
12452  * call-seq:
12453  * ripper.error? -> Boolean
12454  *
12455  * Return true if parsed source has errors.
12456  */
12457 static VALUE
12458 ripper_error_p(VALUE vparser)
12459 {
12460  struct parser_params *p;
12461 
12462  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12463  return p->error_p ? Qtrue : Qfalse;
12464 }
12465 #endif
12466 
12467 /*
12468  * call-seq:
12469  * ripper.end_seen? -> Boolean
12470  *
12471  * Return true if parsed source ended by +\_\_END\_\_+.
12472  */
12473 VALUE
12474 rb_parser_end_seen_p(VALUE vparser)
12475 {
12476  struct parser_params *p;
12477 
12478  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12479  return p->ruby__end__seen ? Qtrue : Qfalse;
12480 }
12481 
12482 /*
12483  * call-seq:
12484  * ripper.encoding -> encoding
12485  *
12486  * Return encoding of the source.
12487  */
12488 VALUE
12489 rb_parser_encoding(VALUE vparser)
12490 {
12491  struct parser_params *p;
12492 
12493  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12494  return rb_enc_from_encoding(p->enc);
12495 }
12496 
12497 #ifdef RIPPER
12498 /*
12499  * call-seq:
12500  * ripper.yydebug -> true or false
12501  *
12502  * Get yydebug.
12503  */
12504 VALUE
12505 rb_parser_get_yydebug(VALUE self)
12506 {
12507  struct parser_params *p;
12508 
12509  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12510  return p->debug ? Qtrue : Qfalse;
12511 }
12512 #endif
12513 
12514 /*
12515  * call-seq:
12516  * ripper.yydebug = flag
12517  *
12518  * Set yydebug.
12519  */
12520 VALUE
12521 rb_parser_set_yydebug(VALUE self, VALUE flag)
12522 {
12523  struct parser_params *p;
12524 
12525  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12526  p->debug = RTEST(flag);
12527  return flag;
12528 }
12529 
12530 /*
12531  * call-seq:
12532  * ripper.debug_output -> obj
12533  *
12534  * Get debug output.
12535  */
12536 VALUE
12537 rb_parser_get_debug_output(VALUE self)
12538 {
12539  struct parser_params *p;
12540 
12541  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12542  return p->debug_output;
12543 }
12544 
12545 /*
12546  * call-seq:
12547  * ripper.debug_output = obj
12548  *
12549  * Set debug output.
12550  */
12551 VALUE
12552 rb_parser_set_debug_output(VALUE self, VALUE output)
12553 {
12554  struct parser_params *p;
12555 
12556  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12557  return p->debug_output = output;
12558 }
12559 
12560 #ifndef RIPPER
12561 #ifdef YYMALLOC
12562 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
12563 /* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
12564  * potential memory leak */
12565 #define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
12566 #define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
12567  (new)->cnt = (cnt), (ptr))
12568 
12569 void *
12570 rb_parser_malloc(struct parser_params *p, size_t size)
12571 {
12572  size_t cnt = HEAPCNT(1, size);
12573  rb_imemo_tmpbuf_t *n = NEWHEAP();
12574  void *ptr = xmalloc(size);
12575 
12576  return ADD2HEAP(n, cnt, ptr);
12577 }
12578 
12579 void *
12580 rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
12581 {
12582  size_t cnt = HEAPCNT(nelem, size);
12583  rb_imemo_tmpbuf_t *n = NEWHEAP();
12584  void *ptr = xcalloc(nelem, size);
12585 
12586  return ADD2HEAP(n, cnt, ptr);
12587 }
12588 
12589 void *
12590 rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
12591 {
12592  rb_imemo_tmpbuf_t *n;
12593  size_t cnt = HEAPCNT(1, size);
12594 
12595  if (ptr && (n = p->heap) != NULL) {
12596  do {
12597  if (n->ptr == ptr) {
12598  n->ptr = ptr = xrealloc(ptr, size);
12599  if (n->cnt) n->cnt = cnt;
12600  return ptr;
12601  }
12602  } while ((n = n->next) != NULL);
12603  }
12604  n = NEWHEAP();
12605  ptr = xrealloc(ptr, size);
12606  return ADD2HEAP(n, cnt, ptr);
12607 }
12608 
12609 void
12610 rb_parser_free(struct parser_params *p, void *ptr)
12611 {
12612  rb_imemo_tmpbuf_t **prev = &p->heap, *n;
12613 
12614  while ((n = *prev) != NULL) {
12615  if (n->ptr == ptr) {
12616  *prev = n->next;
12617  rb_gc_force_recycle((VALUE)n);
12618  break;
12619  }
12620  prev = &n->next;
12621  }
12622  xfree(ptr);
12623 }
12624 #endif
12625 
12626 void
12627 rb_parser_printf(struct parser_params *p, const char *fmt, ...)
12628 {
12629  va_list ap;
12630  VALUE mesg = p->debug_buffer;
12631 
12632  if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
12633  va_start(ap, fmt);
12634  rb_str_vcatf(mesg, fmt, ap);
12635  va_end(ap);
12636  if (RSTRING_END(mesg)[-1] == '\n') {
12637  rb_io_write(p->debug_output, mesg);
12638  p->debug_buffer = Qnil;
12639  }
12640 }
12641 
12642 static void
12643 parser_compile_error(struct parser_params *p, const char *fmt, ...)
12644 {
12645  va_list ap;
12646 
12647  rb_io_flush(p->debug_output);
12648  p->error_p = 1;
12649  va_start(ap, fmt);
12650  p->error_buffer =
12651  rb_syntax_error_append(p->error_buffer,
12652  p->ruby_sourcefile_string,
12653  p->ruby_sourceline,
12654  rb_long2int(p->lex.pcur - p->lex.pbeg),
12655  p->enc, fmt, ap);
12656  va_end(ap);
12657 }
12658 
12659 static size_t
12660 count_char(const char *str, int c)
12661 {
12662  int n = 0;
12663  while (str[n] == c) ++n;
12664  return n;
12665 }
12666 
12667 /*
12668  * strip enclosing double-quotes, same as the default yytnamerr except
12669  * for that single-quotes matching back-quotes do not stop stripping.
12670  *
12671  * "\"`class' keyword\"" => "`class' keyword"
12672  */
12673 RUBY_FUNC_EXPORTED size_t
12674 rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
12675 {
12676  YYUSE(p);
12677  if (*yystr == '"') {
12678  size_t yyn = 0, bquote = 0;
12679  const char *yyp = yystr;
12680 
12681  while (*++yyp) {
12682  switch (*yyp) {
12683  case '`':
12684  if (!bquote) {
12685  bquote = count_char(yyp+1, '`') + 1;
12686  if (yyres) memcpy(&yyres[yyn], yyp, bquote);
12687  yyn += bquote;
12688  yyp += bquote - 1;
12689  break;
12690  }
12691  goto default_char;
12692 
12693  case '\'':
12694  if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
12695  if (yyres) memcpy(yyres + yyn, yyp, bquote);
12696  yyn += bquote;
12697  yyp += bquote - 1;
12698  bquote = 0;
12699  break;
12700  }
12701  if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
12702  if (yyres) memcpy(yyres + yyn, yyp, 3);
12703  yyn += 3;
12704  yyp += 2;
12705  break;
12706  }
12707  goto do_not_strip_quotes;
12708 
12709  case ',':
12710  goto do_not_strip_quotes;
12711 
12712  case '\\':
12713  if (*++yyp != '\\')
12714  goto do_not_strip_quotes;
12715  /* Fall through. */
12716  default_char:
12717  default:
12718  if (yyres)
12719  yyres[yyn] = *yyp;
12720  yyn++;
12721  break;
12722 
12723  case '"':
12724  case '\0':
12725  if (yyres)
12726  yyres[yyn] = '\0';
12727  return yyn;
12728  }
12729  }
12730  do_not_strip_quotes: ;
12731  }
12732 
12733  if (!yyres) return strlen(yystr);
12734 
12735  return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
12736 }
12737 #endif
12738 
12739 #ifdef RIPPER
12740 #ifdef RIPPER_DEBUG
12741 /* :nodoc: */
12742 static VALUE
12743 ripper_validate_object(VALUE self, VALUE x)
12744 {
12745  if (x == Qfalse) return x;
12746  if (x == Qtrue) return x;
12747  if (x == Qnil) return x;
12748  if (x == Qundef)
12749  rb_raise(rb_eArgError, "Qundef given");
12750  if (FIXNUM_P(x)) return x;
12751  if (SYMBOL_P(x)) return x;
12752  switch (BUILTIN_TYPE(x)) {
12753  case T_STRING:
12754  case T_OBJECT:
12755  case T_ARRAY:
12756  case T_BIGNUM:
12757  case T_FLOAT:
12758  case T_COMPLEX:
12759  case T_RATIONAL:
12760  break;
12761  case T_NODE:
12762  if (nd_type((NODE *)x) != NODE_RIPPER) {
12763  rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
12764  }
12765  x = ((NODE *)x)->nd_rval;
12766  break;
12767  default:
12768  rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
12769  (void *)x, rb_obj_classname(x));
12770  }
12771  if (!RBASIC_CLASS(x)) {
12772  rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
12773  (void *)x, rb_builtin_type_name(TYPE(x)));
12774  }
12775  return x;
12776 }
12777 #endif
12778 
12779 #define validate(x) ((x) = get_value(x))
12780 
12781 static VALUE
12782 ripper_dispatch0(struct parser_params *p, ID mid)
12783 {
12784  return rb_funcall(p->value, mid, 0);
12785 }
12786 
12787 static VALUE
12788 ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
12789 {
12790  validate(a);
12791  return rb_funcall(p->value, mid, 1, a);
12792 }
12793 
12794 static VALUE
12795 ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
12796 {
12797  validate(a);
12798  validate(b);
12799  return rb_funcall(p->value, mid, 2, a, b);
12800 }
12801 
12802 static VALUE
12803 ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
12804 {
12805  validate(a);
12806  validate(b);
12807  validate(c);
12808  return rb_funcall(p->value, mid, 3, a, b, c);
12809 }
12810 
12811 static VALUE
12812 ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
12813 {
12814  validate(a);
12815  validate(b);
12816  validate(c);
12817  validate(d);
12818  return rb_funcall(p->value, mid, 4, a, b, c, d);
12819 }
12820 
12821 static VALUE
12822 ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
12823 {
12824  validate(a);
12825  validate(b);
12826  validate(c);
12827  validate(d);
12828  validate(e);
12829  return rb_funcall(p->value, mid, 5, a, b, c, d, e);
12830 }
12831 
12832 static VALUE
12833 ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
12834 {
12835  validate(a);
12836  validate(b);
12837  validate(c);
12838  validate(d);
12839  validate(e);
12840  validate(f);
12841  validate(g);
12842  return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
12843 }
12844 
12845 static ID
12846 ripper_get_id(VALUE v)
12847 {
12848  NODE *nd;
12849  if (!RB_TYPE_P(v, T_NODE)) return 0;
12850  nd = (NODE *)v;
12851  if (nd_type(nd) != NODE_RIPPER) return 0;
12852  return nd->nd_vid;
12853 }
12854 
12855 static VALUE
12856 ripper_get_value(VALUE v)
12857 {
12858  NODE *nd;
12859  if (v == Qundef) return Qnil;
12860  if (!RB_TYPE_P(v, T_NODE)) return v;
12861  nd = (NODE *)v;
12862  if (nd_type(nd) != NODE_RIPPER) return Qnil;
12863  return nd->nd_rval;
12864 }
12865 
12866 static void
12867 ripper_error(struct parser_params *p)
12868 {
12869  p->error_p = TRUE;
12870 }
12871 
12872 static void
12873 ripper_compile_error(struct parser_params *p, const char *fmt, ...)
12874 {
12875  VALUE str;
12876  va_list args;
12877 
12878  va_start(args, fmt);
12879  str = rb_vsprintf(fmt, args);
12880  va_end(args);
12881  rb_funcall(p->value, rb_intern("compile_error"), 1, str);
12882  ripper_error(p);
12883 }
12884 
12885 static VALUE
12886 ripper_lex_get_generic(struct parser_params *p, VALUE src)
12887 {
12888  VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
12889  if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
12890  rb_raise(rb_eTypeError,
12891  "gets returned %"PRIsVALUE" (expected String or nil)",
12892  rb_obj_class(line));
12893  }
12894  return line;
12895 }
12896 
12897 static VALUE
12898 ripper_lex_io_get(struct parser_params *p, VALUE src)
12899 {
12900  return rb_io_gets(src);
12901 }
12902 
12903 static VALUE
12904 ripper_s_allocate(VALUE klass)
12905 {
12906  struct parser_params *p;
12907  VALUE self = TypedData_Make_Struct(klass, struct parser_params,
12908  &parser_data_type, p);
12909  p->value = self;
12910  return self;
12911 }
12912 
12913 #define ripper_initialized_p(r) ((r)->lex.input != 0)
12914 
12915 /*
12916  * call-seq:
12917  * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
12918  *
12919  * Create a new Ripper object.
12920  * _src_ must be a String, an IO, or an Object which has #gets method.
12921  *
12922  * This method does not starts parsing.
12923  * See also Ripper#parse and Ripper.parse.
12924  */
12925 static VALUE
12926 ripper_initialize(int argc, VALUE *argv, VALUE self)
12927 {
12928  struct parser_params *p;
12929  VALUE src, fname, lineno;
12930 
12931  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12932  rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
12933  if (RB_TYPE_P(src, T_FILE)) {
12934  p->lex.gets = ripper_lex_io_get;
12935  }
12936  else if (rb_respond_to(src, id_gets)) {
12937  p->lex.gets = ripper_lex_get_generic;
12938  }
12939  else {
12940  StringValue(src);
12941  p->lex.gets = lex_get_str;
12942  }
12943  p->lex.input = src;
12944  p->eofp = 0;
12945  if (NIL_P(fname)) {
12946  fname = STR_NEW2("(ripper)");
12947  OBJ_FREEZE(fname);
12948  }
12949  else {
12950  StringValueCStr(fname);
12951  fname = rb_str_new_frozen(fname);
12952  }
12953  parser_initialize(p);
12954 
12955  p->ruby_sourcefile_string = fname;
12956  p->ruby_sourcefile = RSTRING_PTR(fname);
12957  p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
12958 
12959  return Qnil;
12960 }
12961 
12962 static VALUE
12963 ripper_parse0(VALUE parser_v)
12964 {
12965  struct parser_params *p;
12966 
12967  TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
12968  parser_prepare(p);
12969  p->ast = rb_ast_new();
12970  ripper_yyparse((void*)p);
12971  rb_ast_dispose(p->ast);
12972  p->ast = 0;
12973  return p->result;
12974 }
12975 
12976 static VALUE
12977 ripper_ensure(VALUE parser_v)
12978 {
12979  struct parser_params *p;
12980 
12981  TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
12982  p->parsing_thread = Qnil;
12983  return Qnil;
12984 }
12985 
12986 /*
12987  * call-seq:
12988  * ripper.parse
12989  *
12990  * Start parsing and returns the value of the root action.
12991  */
12992 static VALUE
12993 ripper_parse(VALUE self)
12994 {
12995  struct parser_params *p;
12996 
12997  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12998  if (!ripper_initialized_p(p)) {
12999  rb_raise(rb_eArgError, "method called for uninitialized object");
13000  }
13001  if (!NIL_P(p->parsing_thread)) {
13002  if (p->parsing_thread == rb_thread_current())
13003  rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13004  else
13005  rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13006  }
13007  p->parsing_thread = rb_thread_current();
13008  rb_ensure(ripper_parse0, self, ripper_ensure, self);
13009 
13010  return p->result;
13011 }
13012 
13013 /*
13014  * call-seq:
13015  * ripper.column -> Integer
13016  *
13017  * Return column number of current parsing line.
13018  * This number starts from 0.
13019  */
13020 static VALUE
13021 ripper_column(VALUE self)
13022 {
13023  struct parser_params *p;
13024  long col;
13025 
13026  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13027  if (!ripper_initialized_p(p)) {
13028  rb_raise(rb_eArgError, "method called for uninitialized object");
13029  }
13030  if (NIL_P(p->parsing_thread)) return Qnil;
13031  col = p->lex.ptok - p->lex.pbeg;
13032  return LONG2NUM(col);
13033 }
13034 
13035 /*
13036  * call-seq:
13037  * ripper.filename -> String
13038  *
13039  * Return current parsing filename.
13040  */
13041 static VALUE
13042 ripper_filename(VALUE self)
13043 {
13044  struct parser_params *p;
13045 
13046  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13047  if (!ripper_initialized_p(p)) {
13048  rb_raise(rb_eArgError, "method called for uninitialized object");
13049  }
13050  return p->ruby_sourcefile_string;
13051 }
13052 
13053 /*
13054  * call-seq:
13055  * ripper.lineno -> Integer
13056  *
13057  * Return line number of current parsing line.
13058  * This number starts from 1.
13059  */
13060 static VALUE
13061 ripper_lineno(VALUE self)
13062 {
13063  struct parser_params *p;
13064 
13065  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13066  if (!ripper_initialized_p(p)) {
13067  rb_raise(rb_eArgError, "method called for uninitialized object");
13068  }
13069  if (NIL_P(p->parsing_thread)) return Qnil;
13070  return INT2NUM(p->ruby_sourceline);
13071 }
13072 
13073 /*
13074  * call-seq:
13075  * ripper.state -> Integer
13076  *
13077  * Return scanner state of current token.
13078  */
13079 static VALUE
13080 ripper_state(VALUE self)
13081 {
13082  struct parser_params *p;
13083 
13084  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13085  if (!ripper_initialized_p(p)) {
13086  rb_raise(rb_eArgError, "method called for uninitialized object");
13087  }
13088  if (NIL_P(p->parsing_thread)) return Qnil;
13089  return INT2NUM(p->lex.state);
13090 }
13091 
13092 /*
13093  * call-seq:
13094  * ripper.token -> String
13095  *
13096  * Return the current token string.
13097  */
13098 static VALUE
13099 ripper_token(VALUE self)
13100 {
13101  struct parser_params *p;
13102  long pos, len;
13103 
13104  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13105  if (!ripper_initialized_p(p)) {
13106  rb_raise(rb_eArgError, "method called for uninitialized object");
13107  }
13108  if (NIL_P(p->parsing_thread)) return Qnil;
13109  pos = p->lex.ptok - p->lex.pbeg;
13110  len = p->lex.pcur - p->lex.ptok;
13111  return rb_str_subseq(p->lex.lastline, pos, len);
13112 }
13113 
13114 #ifdef RIPPER_DEBUG
13115 /* :nodoc: */
13116 static VALUE
13117 ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13118 {
13119  StringValue(msg);
13120  if (obj == Qundef) {
13121  rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13122  }
13123  return Qnil;
13124 }
13125 
13126 /* :nodoc: */
13127 static VALUE
13128 ripper_value(VALUE self, VALUE obj)
13129 {
13130  return ULONG2NUM(obj);
13131 }
13132 #endif
13133 
13134 /*
13135  * call-seq:
13136  * Ripper.lex_state_name(integer) -> string
13137  *
13138  * Returns a string representation of lex_state.
13139  */
13140 static VALUE
13141 ripper_lex_state_name(VALUE self, VALUE state)
13142 {
13143  return rb_parser_lex_state_name(NUM2INT(state));
13144 }
13145 
13146 void
13147 Init_ripper(void)
13148 {
13149  ripper_init_eventids1();
13150  ripper_init_eventids2();
13151  id_warn = rb_intern_const("warn");
13152  id_warning = rb_intern_const("warning");
13153  id_gets = rb_intern_const("gets");
13154  id_assoc = rb_intern_const("=>");
13155 
13156  (void)yystpcpy; /* may not used in newer bison */
13157 
13158  InitVM(ripper);
13159 }
13160 
13161 void
13162 InitVM_ripper(void)
13163 {
13164  VALUE Ripper;
13165 
13166  Ripper = rb_define_class("Ripper", rb_cObject);
13167  /* version of Ripper */
13168  rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
13169  rb_define_alloc_func(Ripper, ripper_s_allocate);
13170  rb_define_method(Ripper, "initialize", ripper_initialize, -1);
13171  rb_define_method(Ripper, "parse", ripper_parse, 0);
13172  rb_define_method(Ripper, "column", ripper_column, 0);
13173  rb_define_method(Ripper, "filename", ripper_filename, 0);
13174  rb_define_method(Ripper, "lineno", ripper_lineno, 0);
13175  rb_define_method(Ripper, "state", ripper_state, 0);
13176  rb_define_method(Ripper, "token", ripper_token, 0);
13177  rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
13178  rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
13179  rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
13180  rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
13181  rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
13182  rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
13183  rb_define_method(Ripper, "error?", ripper_error_p, 0);
13184 #ifdef RIPPER_DEBUG
13185  rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
13186  rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
13187  rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
13188 #endif
13189 
13190  rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
13191  rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
13192 
13193  rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
13194 
13195  /* ignore newline, +/- is a sign. */
13196  rb_define_const(Ripper, "EXPR_BEG", INT2NUM(EXPR_BEG));
13197  /* newline significant, +/- is an operator. */
13198  rb_define_const(Ripper, "EXPR_END", INT2NUM(EXPR_END));
13199  /* ditto, and unbound braces. */
13200  rb_define_const(Ripper, "EXPR_ENDARG", INT2NUM(EXPR_ENDARG));
13201  /* ditto, and unbound braces. */
13202  rb_define_const(Ripper, "EXPR_ENDFN", INT2NUM(EXPR_ENDFN));
13203  /* newline significant, +/- is an operator. */
13204  rb_define_const(Ripper, "EXPR_ARG", INT2NUM(EXPR_ARG));
13205  /* newline significant, +/- is an operator. */
13206  rb_define_const(Ripper, "EXPR_CMDARG", INT2NUM(EXPR_CMDARG));
13207  /* newline significant, +/- is an operator. */
13208  rb_define_const(Ripper, "EXPR_MID", INT2NUM(EXPR_MID));
13209  /* ignore newline, no reserved words. */
13210  rb_define_const(Ripper, "EXPR_FNAME", INT2NUM(EXPR_FNAME));
13211  /* right after `.' or `::', no reserved words. */
13212  rb_define_const(Ripper, "EXPR_DOT", INT2NUM(EXPR_DOT));
13213  /* immediate after `class', no here document. */
13214  rb_define_const(Ripper, "EXPR_CLASS", INT2NUM(EXPR_CLASS));
13215  /* flag bit, label is allowed. */
13216  rb_define_const(Ripper, "EXPR_LABEL", INT2NUM(EXPR_LABEL));
13217  /* flag bit, just after a label. */
13218  rb_define_const(Ripper, "EXPR_LABELED", INT2NUM(EXPR_LABELED));
13219  /* symbol literal as FNAME. */
13220  rb_define_const(Ripper, "EXPR_FITEM", INT2NUM(EXPR_FITEM));
13221  /* equals to +EXPR_BEG+ */
13222  rb_define_const(Ripper, "EXPR_VALUE", INT2NUM(EXPR_VALUE));
13223  /* equals to <tt>(EXPR_BEG | EXPR_MID | EXPR_CLASS)</tt> */
13224  rb_define_const(Ripper, "EXPR_BEG_ANY", INT2NUM(EXPR_BEG_ANY));
13225  /* equals to <tt>(EXPR_ARG | EXPR_CMDARG)</tt> */
13226  rb_define_const(Ripper, "EXPR_ARG_ANY", INT2NUM(EXPR_ARG_ANY));
13227  /* equals to <tt>(EXPR_END | EXPR_ENDARG | EXPR_ENDFN)</tt> */
13228  rb_define_const(Ripper, "EXPR_END_ANY", INT2NUM(EXPR_END_ANY));
13229  /* equals to +0+ */
13230  rb_define_const(Ripper, "EXPR_NONE", INT2NUM(EXPR_NONE));
13231 
13232  ripper_init_eventids1_table(Ripper);
13233  ripper_init_eventids2_table(Ripper);
13234 
13235 # if 0
13236  /* Hack to let RDoc document SCRIPT_LINES__ */
13237 
13238  /*
13239  * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
13240  * after the assignment will be added as an Array of lines with the file
13241  * name as the key.
13242  */
13243  rb_define_global_const("SCRIPT_LINES__", Qnil);
13244 #endif
13245 
13246 }
13247 #endif /* RIPPER */
13248 
13249 /*
13250  * Local variables:
13251  * mode: c
13252  * c-file-style: "ruby"
13253  * End:
13254  */