Ruby  2.7.1p83(2020-03-31revisiona0c7c23c9cec0d0ffcba012279cd652d28ad5bf3)
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 %lex-param {struct parser_params *p}
972 %parse-param {struct parser_params *p}
973 %initial-action
974 {
975  RUBY_SET_YYLLOC_OF_NONE(@$);
976 };
977 
978 %union {
979  VALUE val;
980  NODE *node;
981  ID id;
982  int num;
983  st_table *tbl;
984  const struct vtable *vars;
985  struct rb_strterm_struct *strterm;
986 }
987 
988 %token <val>
989  keyword_class "`class'"
990  keyword_module "`module'"
991  keyword_def "`def'"
992  keyword_undef "`undef'"
993  keyword_begin "`begin'"
994  keyword_rescue "`rescue'"
995  keyword_ensure "`ensure'"
996  keyword_end "`end'"
997  keyword_if "`if'"
998  keyword_unless "`unless'"
999  keyword_then "`then'"
1000  keyword_elsif "`elsif'"
1001  keyword_else "`else'"
1002  keyword_case "`case'"
1003  keyword_when "`when'"
1004  keyword_while "`while'"
1005  keyword_until "`until'"
1006  keyword_for "`for'"
1007  keyword_break "`break'"
1008  keyword_next "`next'"
1009  keyword_redo "`redo'"
1010  keyword_retry "`retry'"
1011  keyword_in "`in'"
1012  keyword_do "`do'"
1013  keyword_do_cond "`do' for condition"
1014  keyword_do_block "`do' for block"
1015  keyword_do_LAMBDA "`do' for lambda"
1016  keyword_return "`return'"
1017  keyword_yield "`yield'"
1018  keyword_super "`super'"
1019  keyword_self "`self'"
1020  keyword_nil "`nil'"
1021  keyword_true "`true'"
1022  keyword_false "`false'"
1023  keyword_and "`and'"
1024  keyword_or "`or'"
1025  keyword_not "`not'"
1026  modifier_if "`if' modifier"
1027  modifier_unless "`unless' modifier"
1028  modifier_while "`while' modifier"
1029  modifier_until "`until' modifier"
1030  modifier_rescue "`rescue' modifier"
1031  keyword_alias "`alias'"
1032  keyword_defined "`defined?'"
1033  keyword_BEGIN "`BEGIN'"
1034  keyword_END "`END'"
1035  keyword__LINE__ "`__LINE__'"
1036  keyword__FILE__ "`__FILE__'"
1037  keyword__ENCODING__ "`__ENCODING__'"
1038 
1039 %token <val> tIDENTIFIER "local variable or method"
1040 %token <val> tFID "method"
1041 %token <val> tGVAR "global variable"
1042 %token <val> tIVAR "instance variable"
1043 %token <val> tCONSTANT "constant"
1044 %token <val> tCVAR "class variable"
1045 %token <val> tLABEL
1046 %token <val> tINTEGER "integer literal"
1047 %token <val> tFLOAT "float literal"
1048 %token <val> tRATIONAL "rational literal"
1049 %token <val> tIMAGINARY "imaginary literal"
1050 %token <val> tCHAR "char literal"
1051 %token <val> tNTH_REF "numbered reference"
1052 %token <val> tBACK_REF "back reference"
1053 %token <val> tSTRING_CONTENT "literal content"
1054 %token <val> tREGEXP_END
1055 
1056 %type <val> singleton strings string string1 xstring regexp
1057 %type <val> string_contents xstring_contents regexp_contents string_content
1058 %type <val> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1059 %type <val> literal numeric simple_numeric ssym dsym symbol cpath
1060 %type <val> top_compstmt top_stmts top_stmt begin_block
1061 %type <val> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1062 %type <val> expr_value expr_value_do arg_value primary_value fcall rel_expr
1063 %type <val> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1064 %type <val> args call_args opt_call_args
1065 %type <val> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1066 %type <val> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1067 %type <val> command_rhs arg_rhs
1068 %type <val> command_asgn mrhs mrhs_arg superclass block_call block_command
1069 %type <val> f_block_optarg f_block_opt
1070 %type <val> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs f_rest_marg
1071 %type <val> assoc_list assocs assoc undef_list backref string_dvar for_var
1072 %type <val> block_param opt_block_param block_param_def f_opt
1073 %type <val> f_kwarg f_kw f_block_kwarg f_block_kw
1074 %type <val> bv_decls opt_bv_decl bvar
1075 %type <val> lambda f_larglist lambda_body brace_body do_body
1076 %type <val> brace_block cmd_brace_block do_block lhs none fitem
1077 %type <val> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1078 %type <val> p_case_body p_cases p_top_expr p_top_expr_body
1079 %type <val> p_expr p_as p_alt p_expr_basic
1080 %type <val> p_args p_args_head p_args_tail p_args_post p_arg
1081 %type <val> p_value p_primitive p_variable p_var_ref p_const
1082 %type <val> p_kwargs p_kwarg p_kw
1083 %type <val> keyword_variable user_variable sym operation operation2 operation3
1084 %type <val> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1085 %type <val> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1086 %type <val> p_kwrest p_kwnorest p_kw_label
1087 %type <val> f_no_kwarg args_forward
1088 %token END_OF_INPUT 0 "end-of-input"
1089 %token <val> '.'
1090 /* escaped chars, should be ignored otherwise */
1091 %token <val> '\\' "backslash"
1092 %token tSP "escaped space"
1093 %token <val> '\t' "escaped horizontal tab"
1094 %token <val> '\f' "escaped form feed"
1095 %token <val> '\r' "escaped carriage return"
1096 %token <val> '\13' "escaped vertical tab"
1097 %token tUPLUS 132 "unary+"
1098 %token tUMINUS 133 "unary-"
1099 %token tPOW 134 "**"
1100 %token tCMP 135 "<=>"
1101 %token tEQ 140 "=="
1102 %token tEQQ 141 "==="
1103 %token tNEQ 142 "!="
1104 %token tGEQ 139 ">="
1105 %token tLEQ 138 "<="
1106 %token tANDOP 148 "&&"
1107 %token tOROP 149 "||"
1108 %token tMATCH 143 "=~"
1109 %token tNMATCH 144 "!~"
1110 %token tDOT2 128 ".."
1111 %token tDOT3 129 "..."
1112 %token tBDOT2 130 "(.."
1113 %token tBDOT3 131 "(..."
1114 %token tAREF 145 "[]"
1115 %token tASET 146 "[]="
1116 %token tLSHFT 136 "<<"
1117 %token tRSHFT 137 ">>"
1118 %token <val> tANDDOT 150 "&."
1119 %token <val> tCOLON2 147 "::"
1120 %token tCOLON3 ":: at EXPR_BEG"
1121 %token <val> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1122 %token tASSOC "=>"
1123 %token tLPAREN "("
1124 %token tLPAREN_ARG "( arg"
1125 %token tRPAREN ")"
1126 %token tLBRACK "["
1127 %token tLBRACE "{"
1128 %token tLBRACE_ARG "{ arg"
1129 %token tSTAR "*"
1130 %token tDSTAR "**arg"
1131 %token tAMPER "&"
1132 %token tLAMBDA "->"
1133 %token tSYMBEG "symbol literal"
1134 %token tSTRING_BEG "string literal"
1135 %token tXSTRING_BEG "backtick literal"
1136 %token tREGEXP_BEG "regexp literal"
1137 %token tWORDS_BEG "word list"
1138 %token tQWORDS_BEG "verbatim word list"
1139 %token tSYMBOLS_BEG "symbol list"
1140 %token tQSYMBOLS_BEG "verbatim symbol list"
1141 %token tSTRING_END "terminator"
1142 %token tSTRING_DEND "'}'"
1143 %token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1144 
1145 /*
1146  * precedence table
1147  */
1148 
1149 %nonassoc tLOWEST
1150 %nonassoc tLBRACE_ARG
1151 
1152 %nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1153 %left keyword_or keyword_and
1154 %right keyword_not
1155 %nonassoc keyword_defined
1156 %right '=' tOP_ASGN
1157 %left modifier_rescue
1158 %right '?' ':'
1159 %nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1160 %left tOROP
1161 %left tANDOP
1162 %nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1163 %left '>' tGEQ '<' tLEQ
1164 %left '|' '^'
1165 %left '&'
1166 %left tLSHFT tRSHFT
1167 %left '+' '-'
1168 %left '*' '/' '%'
1169 %right tUMINUS_NUM tUMINUS
1170 %right tPOW
1171 %right '!' '~' tUPLUS
1172 
1173 %token tLAST_TOKEN
1174 
1175 %%
1176 program : {
1177  SET_LEX_STATE(EXPR_BEG);
1178  local_push(p, ifndef_ripper(1)+0);
1179  }
1180  top_compstmt
1181  {
1182 #if 0
1183  if ($2 && !compile_for_eval) {
1184  NODE *node = $2;
1185  /* last expression should not be void */
1186  if (nd_type(node) == NODE_BLOCK) {
1187  while (node->nd_next) {
1188  node = node->nd_next;
1189  }
1190  node = node->nd_head;
1191  }
1192  node = remove_begin(node);
1193  void_expr(p, node);
1194  }
1195  p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1196 #endif
1197  {VALUE v1,v2;v1=$2;v2=dispatch1(program,v1);p->result=v2;}
1198  local_pop(p);
1199  }
1200  ;
1201 
1202 top_compstmt : top_stmts opt_terms
1203  {
1204  $$ = void_stmts(p, $1);
1205  }
1206  ;
1207 
1208 top_stmts : none
1209  {
1210 #if 0
1211  $$ = NEW_BEGIN(0, &@$);
1212 #endif
1213  {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;}
1214  }
1215  | top_stmt
1216  {
1217 #if 0
1218  $$ = newline_node($1);
1219 #endif
1220  {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1221  }
1222  | top_stmts terms top_stmt
1223  {
1224 #if 0
1225  $$ = block_append(p, $1, newline_node($3));
1226 #endif
1227  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1228  }
1229  | error top_stmt
1230  {
1231  $$ = remove_begin($2);
1232  }
1233  ;
1234 
1235 top_stmt : stmt
1236  | keyword_BEGIN begin_block
1237  {
1238  $$ = $2;
1239  }
1240  ;
1241 
1242 begin_block : '{' top_compstmt '}'
1243  {
1244 #if 0
1245  p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1246  NEW_BEGIN($2, &@$));
1247  $$ = NEW_BEGIN(0, &@$);
1248 #endif
1249  {VALUE v1,v2;v1=$2;v2=dispatch1(BEGIN,v1);$$=v2;}
1250  }
1251  ;
1252 
1253 bodystmt : compstmt
1254  opt_rescue
1255  k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1256  compstmt
1257  opt_ensure
1258  {
1259 #if 0
1260  $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1261 #endif
1262  {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;}
1263  }
1264  | compstmt
1265  opt_rescue
1266  opt_ensure
1267  {
1268 #if 0
1269  $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1270 #endif
1271  {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;}
1272  }
1273  ;
1274 
1275 compstmt : stmts opt_terms
1276  {
1277  $$ = void_stmts(p, $1);
1278  }
1279  ;
1280 
1281 stmts : none
1282  {
1283 #if 0
1284  $$ = NEW_BEGIN(0, &@$);
1285 #endif
1286  {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;}
1287  }
1288  | stmt_or_begin
1289  {
1290 #if 0
1291  $$ = newline_node($1);
1292 #endif
1293  {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1294  }
1295  | stmts terms stmt_or_begin
1296  {
1297 #if 0
1298  $$ = block_append(p, $1, newline_node($3));
1299 #endif
1300  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1301  }
1302  | error stmt
1303  {
1304  $$ = remove_begin($2);
1305  }
1306  ;
1307 
1308 stmt_or_begin : stmt
1309  {
1310  $$ = $1;
1311  }
1312  | keyword_BEGIN
1313  {
1314  yyerror1(&@1, "BEGIN is permitted only at toplevel");
1315  }
1316  begin_block
1317  {
1318  $$ = $3;
1319  }
1320  ;
1321 
1322 stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1323  {
1324 #if 0
1325  $$ = NEW_ALIAS($2, $4, &@$);
1326 #endif
1327  {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(alias,v1,v2);$$=v3;}
1328  }
1329  | keyword_alias tGVAR tGVAR
1330  {
1331 #if 0
1332  $$ = NEW_VALIAS($2, $3, &@$);
1333 #endif
1334  {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1335  }
1336  | keyword_alias tGVAR tBACK_REF
1337  {
1338 #if 0
1339  char buf[2];
1340  buf[0] = '$';
1341  buf[1] = (char)$3->nd_nth;
1342  $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1343 #endif
1344  {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1345  }
1346  | keyword_alias tGVAR tNTH_REF
1347  {
1348 #if 0
1349  yyerror1(&@3, "can't make alias for the number variables");
1350  $$ = NEW_BEGIN(0, &@$);
1351 #endif
1352  {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);
1353  }
1354  | keyword_undef undef_list
1355  {
1356 #if 0
1357  $$ = $2;
1358 #endif
1359  {VALUE v1,v2;v1=$2;v2=dispatch1(undef,v1);$$=v2;}
1360  }
1361  | stmt modifier_if expr_value
1362  {
1363 #if 0
1364  $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1365  fixpos($$, $3);
1366 #endif
1367  {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
1368  }
1369  | stmt modifier_unless expr_value
1370  {
1371 #if 0
1372  $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1373  fixpos($$, $3);
1374 #endif
1375  {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
1376  }
1377  | stmt modifier_while expr_value
1378  {
1379 #if 0
1380  if ($1 && nd_type($1) == NODE_BEGIN) {
1381  $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1382  }
1383  else {
1384  $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1385  }
1386 #endif
1387  {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(while_mod,v1,v2);$$=v3;}
1388  }
1389  | stmt modifier_until expr_value
1390  {
1391 #if 0
1392  if ($1 && nd_type($1) == NODE_BEGIN) {
1393  $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1394  }
1395  else {
1396  $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1397  }
1398 #endif
1399  {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(until_mod,v1,v2);$$=v3;}
1400  }
1401  | stmt modifier_rescue stmt
1402  {
1403 #if 0
1404  NODE *resq;
1405  YYLTYPE loc = code_loc_gen(&@2, &@3);
1406  resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1407  $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1408 #endif
1409  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1410  }
1411  | keyword_END '{' compstmt '}'
1412  {
1413  if (p->in_def) {
1414  rb_warn0("END in method; use at_exit");
1415  }
1416 #if 0
1417  {
1418  NODE *scope = NEW_NODE(
1419  NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1420  $$ = NEW_POSTEXE(scope, &@$);
1421  }
1422 #endif
1423  {VALUE v1,v2;v1=$3;v2=dispatch1(END,v1);$$=v2;}
1424  }
1425  | command_asgn
1426  | mlhs '=' command_call
1427  {
1428 #if 0
1429  value_expr($3);
1430  $$ = node_assign(p, $1, $3, &@$);
1431 #endif
1432  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(massign,v1,v2);$$=v3;}
1433  }
1434  | lhs '=' mrhs
1435  {
1436 #if 0
1437  value_expr($3);
1438  $$ = node_assign(p, $1, $3, &@$);
1439 #endif
1440  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
1441  }
1442  | mlhs '=' mrhs_arg modifier_rescue stmt
1443  {
1444 #if 0
1445  YYLTYPE loc = code_loc_gen(&@4, &@5);
1446  value_expr($3);
1447  $$ = node_assign(p, $1, NEW_RESCUE($3, NEW_RESBODY(0, remove_begin($5), 0, &loc), 0, &@$), &@$);
1448 #endif
1449  {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;}
1450  }
1451  | mlhs '=' mrhs_arg
1452  {
1453 #if 0
1454  $$ = node_assign(p, $1, $3, &@$);
1455 #endif
1456  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(massign,v1,v2);$$=v3;}
1457  }
1458  | expr
1459  ;
1460 
1461 command_asgn : lhs '=' command_rhs
1462  {
1463 #if 0
1464  $$ = node_assign(p, $1, $3, &@$);
1465 #endif
1466  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
1467  }
1468  | var_lhs tOP_ASGN command_rhs
1469  {
1470 #if 0
1471  $$ = new_op_assign(p, $1, $2, $3, &@$);
1472 #endif
1473  {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
1474  }
1475  | primary_value '[' opt_call_args rbracket tOP_ASGN command_rhs
1476  {
1477 #if 0
1478  $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
1479 #endif
1480  {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;}
1481 
1482  }
1483  | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
1484  {
1485 #if 0
1486  $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1487 #endif
1488  {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;}
1489  }
1490  | primary_value call_op tCONSTANT tOP_ASGN command_rhs
1491  {
1492 #if 0
1493  $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1494 #endif
1495  {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;}
1496  }
1497  | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
1498  {
1499 #if 0
1500  YYLTYPE loc = code_loc_gen(&@1, &@3);
1501  $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
1502 #endif
1503  {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;}
1504  }
1505  | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
1506  {
1507 #if 0
1508  $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
1509 #endif
1510  {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;}
1511  }
1512  | backref tOP_ASGN command_rhs
1513  {
1514 #if 0
1515  rb_backref_error(p, $1);
1516  $$ = NEW_BEGIN(0, &@$);
1517 #endif
1518  {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);
1519  }
1520  ;
1521 
1522 command_rhs : command_call %prec tOP_ASGN
1523  {
1524  value_expr($1);
1525  $$ = $1;
1526  }
1527  | command_call modifier_rescue stmt
1528  {
1529 #if 0
1530  YYLTYPE loc = code_loc_gen(&@2, &@3);
1531  value_expr($1);
1532  $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1533 #endif
1534  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1535  }
1536  | command_asgn
1537  ;
1538 
1539 expr : command_call
1540  | expr keyword_and expr
1541  {
1542  $$ = logop(p, idAND, $1, $3, &@2, &@$);
1543  }
1544  | expr keyword_or expr
1545  {
1546  $$ = logop(p, idOR, $1, $3, &@2, &@$);
1547  }
1548  | keyword_not opt_nl expr
1549  {
1550  $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
1551  }
1552  | '!' command_call
1553  {
1554  $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
1555  }
1556  | arg keyword_in
1557  {
1558  value_expr($1);
1559  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1560  p->command_start = FALSE;
1561  $<num>$ = p->in_kwarg;
1562  p->in_kwarg = 1;
1563  }
1564  {$<tbl>$ = push_pvtbl(p);}
1565  p_expr
1566  {pop_pvtbl(p, $<tbl>4);}
1567  {
1568  p->in_kwarg = !!$<num>3;
1569 #if 0
1570  $$ = new_case3(p, $1, NEW_IN($5, 0, 0, &@5), &@$);
1571 #endif
1572  {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;}
1573  }
1574  | arg %prec tLBRACE_ARG
1575  ;
1576 
1577 expr_value : expr
1578  {
1579  value_expr($1);
1580  $$ = $1;
1581  }
1582  ;
1583 
1584 expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1585  {
1586  $$ = $2;
1587  }
1588 
1589 
1590 command_call : command
1591  | block_command
1592  ;
1593 
1594 block_command : block_call
1595  | block_call call_op2 operation2 command_args
1596  {
1597 #if 0
1598  $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
1599 #endif
1600  {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;}
1601  }
1602  ;
1603 
1604 cmd_brace_block : tLBRACE_ARG brace_body '}'
1605  {
1606  $$ = $2;
1607 #if 0
1608  $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
1609  nd_set_line($$, @1.end_pos.lineno);
1610 #endif
1611  }
1612  ;
1613 
1614 fcall : operation
1615  {
1616 #if 0
1617  $$ = NEW_FCALL($1, 0, &@$);
1618  nd_set_line($$, p->tokline);
1619 #endif
1620  $$=$1;
1621  }
1622  ;
1623 
1624 command : fcall command_args %prec tLOWEST
1625  {
1626 #if 0
1627  $1->nd_args = $2;
1628  nd_set_last_loc($1, @2.end_pos);
1629  $$ = $1;
1630 #endif
1631  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(command,v1,v2);$$=v3;}
1632  }
1633  | fcall command_args cmd_brace_block
1634  {
1635 #if 0
1636  block_dup_check(p, $2, $3);
1637  $1->nd_args = $2;
1638  $$ = method_add_block(p, $1, $3, &@$);
1639  fixpos($$, $1);
1640  nd_set_last_loc($1, @2.end_pos);
1641 #endif
1642  {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;}
1643  }
1644  | primary_value call_op operation2 command_args %prec tLOWEST
1645  {
1646 #if 0
1647  $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
1648 #endif
1649  {VALUE v1,v2,v3,v4,v5;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1650  }
1651  | primary_value call_op operation2 command_args cmd_brace_block
1652  {
1653 #if 0
1654  $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
1655 #endif
1656  {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;}
1657  }
1658  | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1659  {
1660 #if 0
1661  $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
1662 #endif
1663  {VALUE v1,v2,v3,v4,v5;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1664  }
1665  | primary_value tCOLON2 operation2 command_args cmd_brace_block
1666  {
1667 #if 0
1668  $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
1669 #endif
1670  {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;}
1671  }
1672  | keyword_super command_args
1673  {
1674 #if 0
1675  $$ = NEW_SUPER($2, &@$);
1676  fixpos($$, $2);
1677 #endif
1678  {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
1679  }
1680  | keyword_yield command_args
1681  {
1682 #if 0
1683  $$ = new_yield(p, $2, &@$);
1684  fixpos($$, $2);
1685 #endif
1686  {VALUE v1,v2;v1=$2;v2=dispatch1(yield,v1);$$=v2;}
1687  }
1688  | k_return call_args
1689  {
1690 #if 0
1691  $$ = NEW_RETURN(ret_args(p, $2), &@$);
1692 #endif
1693  {VALUE v1,v2;v1=$2;v2=dispatch1(return,v1);$$=v2;}
1694  }
1695  | keyword_break call_args
1696  {
1697 #if 0
1698  $$ = NEW_BREAK(ret_args(p, $2), &@$);
1699 #endif
1700  {VALUE v1,v2;v1=$2;v2=dispatch1(break,v1);$$=v2;}
1701  }
1702  | keyword_next call_args
1703  {
1704 #if 0
1705  $$ = NEW_NEXT(ret_args(p, $2), &@$);
1706 #endif
1707  {VALUE v1,v2;v1=$2;v2=dispatch1(next,v1);$$=v2;}
1708  }
1709  ;
1710 
1711 mlhs : mlhs_basic
1712  | tLPAREN mlhs_inner rparen
1713  {
1714 #if 0
1715  $$ = $2;
1716 #endif
1717  {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1718  }
1719  ;
1720 
1721 mlhs_inner : mlhs_basic
1722  | tLPAREN mlhs_inner rparen
1723  {
1724 #if 0
1725  $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1726 #endif
1727  {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1728  }
1729  ;
1730 
1731 mlhs_basic : mlhs_head
1732  {
1733 #if 0
1734  $$ = NEW_MASGN($1, 0, &@$);
1735 #endif
1736  $$=$1;
1737  }
1738  | mlhs_head mlhs_item
1739  {
1740 #if 0
1741  $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
1742 #endif
1743  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1744  }
1745  | mlhs_head tSTAR mlhs_node
1746  {
1747 #if 0
1748  $$ = NEW_MASGN($1, $3, &@$);
1749 #endif
1750  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1751  }
1752  | mlhs_head tSTAR mlhs_node ',' mlhs_post
1753  {
1754 #if 0
1755  $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
1756 #endif
1757  {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;}
1758  }
1759  | mlhs_head tSTAR
1760  {
1761 #if 0
1762  $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
1763 #endif
1764  {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1765  }
1766  | mlhs_head tSTAR ',' mlhs_post
1767  {
1768 #if 0
1769  $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
1770 #endif
1771  {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;}
1772  }
1773  | tSTAR mlhs_node
1774  {
1775 #if 0
1776  $$ = NEW_MASGN(0, $2, &@$);
1777 #endif
1778  {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$2;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1779  }
1780  | tSTAR mlhs_node ',' mlhs_post
1781  {
1782 #if 0
1783  $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
1784 #endif
1785  {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;}
1786  }
1787  | tSTAR
1788  {
1789 #if 0
1790  $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
1791 #endif
1792  {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=Qnil;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1793  }
1794  | tSTAR ',' mlhs_post
1795  {
1796 #if 0
1797  $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
1798 #endif
1799  {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;}
1800  }
1801  ;
1802 
1803 mlhs_item : mlhs_node
1804  | tLPAREN mlhs_inner rparen
1805  {
1806 #if 0
1807  $$ = $2;
1808 #endif
1809  {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1810  }
1811  ;
1812 
1813 mlhs_head : mlhs_item ','
1814  {
1815 #if 0
1816  $$ = NEW_LIST($1, &@1);
1817 #endif
1818  {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
1819  }
1820  | mlhs_head mlhs_item ','
1821  {
1822 #if 0
1823  $$ = list_append(p, $1, $2);
1824 #endif
1825  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1826  }
1827  ;
1828 
1829 mlhs_post : mlhs_item
1830  {
1831 #if 0
1832  $$ = NEW_LIST($1, &@$);
1833 #endif
1834  {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
1835  }
1836  | mlhs_post ',' mlhs_item
1837  {
1838 #if 0
1839  $$ = list_append(p, $1, $3);
1840 #endif
1841  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1842  }
1843  ;
1844 
1845 mlhs_node : user_variable
1846  {
1847 #if 0
1848  $$ = assignable(p, $1, 0, &@$);
1849 #endif
1850  $$=assignable(p, var_field(p, $1));
1851  }
1852  | keyword_variable
1853  {
1854 #if 0
1855  $$ = assignable(p, $1, 0, &@$);
1856 #endif
1857  $$=assignable(p, var_field(p, $1));
1858  }
1859  | primary_value '[' opt_call_args rbracket
1860  {
1861 #if 0
1862  $$ = aryset(p, $1, $3, &@$);
1863 #endif
1864  {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
1865  }
1866  | primary_value call_op tIDENTIFIER
1867  {
1868  if ($2 == tANDDOT) {
1869  yyerror1(&@2, "&. inside multiple assignment destination");
1870  }
1871 #if 0
1872  $$ = attrset(p, $1, $2, $3, &@$);
1873 #endif
1874  {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1875  }
1876  | primary_value tCOLON2 tIDENTIFIER
1877  {
1878 #if 0
1879  $$ = attrset(p, $1, idCOLON2, $3, &@$);
1880 #endif
1881  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=v3;}
1882  }
1883  | primary_value call_op tCONSTANT
1884  {
1885  if ($2 == tANDDOT) {
1886  yyerror1(&@2, "&. inside multiple assignment destination");
1887  }
1888 #if 0
1889  $$ = attrset(p, $1, $2, $3, &@$);
1890 #endif
1891  {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1892  }
1893  | primary_value tCOLON2 tCONSTANT
1894  {
1895 #if 0
1896  $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1897 #endif
1898  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
1899  }
1900  | tCOLON3 tCONSTANT
1901  {
1902 #if 0
1903  $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1904 #endif
1905  {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
1906  }
1907  | backref
1908  {
1909 #if 0
1910  rb_backref_error(p, $1);
1911  $$ = NEW_BEGIN(0, &@$);
1912 #endif
1913  {VALUE v1,v2;v1=var_field(p, $1);v2=dispatch1(assign_error,v1);$$=v2;}ripper_error(p);
1914  }
1915  ;
1916 
1917 lhs : user_variable
1918  {
1919 #if 0
1920  $$ = assignable(p, $1, 0, &@$);
1921 #endif
1922  $$=assignable(p, var_field(p, $1));
1923  }
1924  | keyword_variable
1925  {
1926 #if 0
1927  $$ = assignable(p, $1, 0, &@$);
1928 #endif
1929  $$=assignable(p, var_field(p, $1));
1930  }
1931  | primary_value '[' opt_call_args rbracket
1932  {
1933 #if 0
1934  $$ = aryset(p, $1, $3, &@$);
1935 #endif
1936  {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
1937  }
1938  | primary_value call_op tIDENTIFIER
1939  {
1940 #if 0
1941  $$ = attrset(p, $1, $2, $3, &@$);
1942 #endif
1943  {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1944  }
1945  | primary_value tCOLON2 tIDENTIFIER
1946  {
1947 #if 0
1948  $$ = attrset(p, $1, idCOLON2, $3, &@$);
1949 #endif
1950  {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1951  }
1952  | primary_value call_op tCONSTANT
1953  {
1954 #if 0
1955  $$ = attrset(p, $1, $2, $3, &@$);
1956 #endif
1957  {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1958  }
1959  | primary_value tCOLON2 tCONSTANT
1960  {
1961 #if 0
1962  $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1963 #endif
1964  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
1965  }
1966  | tCOLON3 tCONSTANT
1967  {
1968 #if 0
1969  $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1970 #endif
1971  {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
1972  }
1973  | backref
1974  {
1975 #if 0
1976  rb_backref_error(p, $1);
1977  $$ = NEW_BEGIN(0, &@$);
1978 #endif
1979  {VALUE v1,v2;v1=var_field(p, $1);v2=dispatch1(assign_error,v1);$$=v2;}ripper_error(p);
1980  }
1981  ;
1982 
1983 cname : tIDENTIFIER
1984  {
1985 #if 0
1986  yyerror1(&@1, "class/module name must be CONSTANT");
1987 #endif
1988  {VALUE v1,v2;v1=$1;v2=dispatch1(class_name_error,v1);$$=v2;}ripper_error(p);
1989  }
1990  | tCONSTANT
1991  ;
1992 
1993 cpath : tCOLON3 cname
1994  {
1995 #if 0
1996  $$ = NEW_COLON3($2, &@$);
1997 #endif
1998  {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
1999  }
2000  | cname
2001  {
2002 #if 0
2003  $$ = NEW_COLON2(0, $$, &@$);
2004 #endif
2005  {VALUE v1,v2;v1=$1;v2=dispatch1(const_ref,v1);$$=v2;}
2006  }
2007  | primary_value tCOLON2 cname
2008  {
2009 #if 0
2010  $$ = NEW_COLON2($1, $3, &@$);
2011 #endif
2012  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2013  }
2014  ;
2015 
2016 fname : tIDENTIFIER
2017  | tCONSTANT
2018  | tFID
2019  | op
2020  {
2021  SET_LEX_STATE(EXPR_ENDFN);
2022  $$ = $1;
2023  }
2024  | reswords
2025  {
2026  SET_LEX_STATE(EXPR_ENDFN);
2027  $$ = $1;
2028  }
2029  ;
2030 
2031 fitem : fname
2032  {
2033 #if 0
2034  $$ = NEW_LIT(ID2SYM($1), &@$);
2035 #endif
2036  {VALUE v1,v2;v1=$1;v2=dispatch1(symbol_literal,v1);$$=v2;}
2037  }
2038  | symbol
2039  ;
2040 
2041 undef_list : fitem
2042  {
2043 #if 0
2044  $$ = NEW_UNDEF($1, &@$);
2045 #endif
2046  $$=rb_ary_new3(1, get_value($1));
2047  }
2048  | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2049  {
2050 #if 0
2051  NODE *undef = NEW_UNDEF($4, &@4);
2052  $$ = block_append(p, $1, undef);
2053 #endif
2054  $$=rb_ary_push($1, get_value($4));
2055  }
2056  ;
2057 
2058 op : '|' { ifndef_ripper($$ = '|'); }
2059  | '^' { ifndef_ripper($$ = '^'); }
2060  | '&' { ifndef_ripper($$ = '&'); }
2061  | tCMP { ifndef_ripper($$ = tCMP); }
2062  | tEQ { ifndef_ripper($$ = tEQ); }
2063  | tEQQ { ifndef_ripper($$ = tEQQ); }
2064  | tMATCH { ifndef_ripper($$ = tMATCH); }
2065  | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2066  | '>' { ifndef_ripper($$ = '>'); }
2067  | tGEQ { ifndef_ripper($$ = tGEQ); }
2068  | '<' { ifndef_ripper($$ = '<'); }
2069  | tLEQ { ifndef_ripper($$ = tLEQ); }
2070  | tNEQ { ifndef_ripper($$ = tNEQ); }
2071  | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2072  | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2073  | '+' { ifndef_ripper($$ = '+'); }
2074  | '-' { ifndef_ripper($$ = '-'); }
2075  | '*' { ifndef_ripper($$ = '*'); }
2076  | tSTAR { ifndef_ripper($$ = '*'); }
2077  | '/' { ifndef_ripper($$ = '/'); }
2078  | '%' { ifndef_ripper($$ = '%'); }
2079  | tPOW { ifndef_ripper($$ = tPOW); }
2080  | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2081  | '!' { ifndef_ripper($$ = '!'); }
2082  | '~' { ifndef_ripper($$ = '~'); }
2083  | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2084  | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2085  | tAREF { ifndef_ripper($$ = tAREF); }
2086  | tASET { ifndef_ripper($$ = tASET); }
2087  | '`' { ifndef_ripper($$ = '`'); }
2088  ;
2089 
2090 reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2091  | keyword_BEGIN | keyword_END
2092  | keyword_alias | keyword_and | keyword_begin
2093  | keyword_break | keyword_case | keyword_class | keyword_def
2094  | keyword_defined | keyword_do | keyword_else | keyword_elsif
2095  | keyword_end | keyword_ensure | keyword_false
2096  | keyword_for | keyword_in | keyword_module | keyword_next
2097  | keyword_nil | keyword_not | keyword_or | keyword_redo
2098  | keyword_rescue | keyword_retry | keyword_return | keyword_self
2099  | keyword_super | keyword_then | keyword_true | keyword_undef
2100  | keyword_when | keyword_yield | keyword_if | keyword_unless
2101  | keyword_while | keyword_until
2102  ;
2103 
2104 arg : lhs '=' arg_rhs
2105  {
2106 #if 0
2107  $$ = node_assign(p, $1, $3, &@$);
2108 #endif
2109  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
2110  }
2111  | var_lhs tOP_ASGN arg_rhs
2112  {
2113 #if 0
2114  $$ = new_op_assign(p, $1, $2, $3, &@$);
2115 #endif
2116  {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
2117  }
2118  | primary_value '[' opt_call_args rbracket tOP_ASGN arg_rhs
2119  {
2120 #if 0
2121  value_expr($6);
2122  $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
2123 #endif
2124  {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;}
2125  }
2126  | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
2127  {
2128 #if 0
2129  value_expr($5);
2130  $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2131 #endif
2132  {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;}
2133  }
2134  | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
2135  {
2136 #if 0
2137  value_expr($5);
2138  $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2139 #endif
2140  {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;}
2141  }
2142  | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
2143  {
2144 #if 0
2145  value_expr($5);
2146  $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
2147 #endif
2148  {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;}
2149  }
2150  | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
2151  {
2152 #if 0
2153  YYLTYPE loc = code_loc_gen(&@1, &@3);
2154  $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
2155 #endif
2156  {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;}
2157  }
2158  | tCOLON3 tCONSTANT tOP_ASGN arg_rhs
2159  {
2160 #if 0
2161  $$ = new_const_op_assign(p, NEW_COLON3($2, &@$), $3, $4, &@$);
2162 #endif
2163  {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;}
2164  }
2165  | backref tOP_ASGN arg_rhs
2166  {
2167 #if 0
2168  rb_backref_error(p, $1);
2169  $$ = NEW_BEGIN(0, &@$);
2170 #endif
2171  {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);
2172  }
2173  | arg tDOT2 arg
2174  {
2175 #if 0
2176  value_expr($1);
2177  value_expr($3);
2178  $$ = NEW_DOT2($1, $3, &@$);
2179 #endif
2180  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
2181  }
2182  | arg tDOT3 arg
2183  {
2184 #if 0
2185  value_expr($1);
2186  value_expr($3);
2187  $$ = NEW_DOT3($1, $3, &@$);
2188 #endif
2189  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
2190  }
2191  | arg tDOT2
2192  {
2193 #if 0
2194  YYLTYPE loc;
2195  loc.beg_pos = @2.end_pos;
2196  loc.end_pos = @2.end_pos;
2197 
2198  value_expr($1);
2199  $$ = NEW_DOT2($1, new_nil(&loc), &@$);
2200 #endif
2201  {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
2202  }
2203  | arg tDOT3
2204  {
2205 #if 0
2206  YYLTYPE loc;
2207  loc.beg_pos = @2.end_pos;
2208  loc.end_pos = @2.end_pos;
2209 
2210  value_expr($1);
2211  $$ = NEW_DOT3($1, new_nil(&loc), &@$);
2212 #endif
2213  {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
2214  }
2215  | tBDOT2 arg
2216  {
2217 #if 0
2218  YYLTYPE loc;
2219  loc.beg_pos = @1.beg_pos;
2220  loc.end_pos = @1.beg_pos;
2221 
2222  value_expr($2);
2223  $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
2224 #endif
2225  {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
2226  }
2227  | tBDOT3 arg
2228  {
2229 #if 0
2230  YYLTYPE loc;
2231  loc.beg_pos = @1.beg_pos;
2232  loc.end_pos = @1.beg_pos;
2233 
2234  value_expr($2);
2235  $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
2236 #endif
2237  {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
2238  }
2239  | arg '+' arg
2240  {
2241  $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2242  }
2243  | arg '-' arg
2244  {
2245  $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2246  }
2247  | arg '*' arg
2248  {
2249  $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2250  }
2251  | arg '/' arg
2252  {
2253  $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2254  }
2255  | arg '%' arg
2256  {
2257  $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2258  }
2259  | arg tPOW arg
2260  {
2261  $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2262  }
2263  | tUMINUS_NUM simple_numeric tPOW arg
2264  {
2265  $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2266  }
2267  | tUPLUS arg
2268  {
2269  $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2270  }
2271  | tUMINUS arg
2272  {
2273  $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2274  }
2275  | arg '|' arg
2276  {
2277  $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2278  }
2279  | arg '^' arg
2280  {
2281  $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2282  }
2283  | arg '&' arg
2284  {
2285  $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2286  }
2287  | arg tCMP arg
2288  {
2289  $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2290  }
2291  | rel_expr %prec tCMP
2292  | arg tEQ arg
2293  {
2294  $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2295  }
2296  | arg tEQQ arg
2297  {
2298  $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2299  }
2300  | arg tNEQ arg
2301  {
2302  $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2303  }
2304  | arg tMATCH arg
2305  {
2306  $$ = match_op(p, $1, $3, &@2, &@$);
2307  }
2308  | arg tNMATCH arg
2309  {
2310  $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2311  }
2312  | '!' arg
2313  {
2314  $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2315  }
2316  | '~' arg
2317  {
2318  $$ = call_uni_op(p, $2, '~', &@1, &@$);
2319  }
2320  | arg tLSHFT arg
2321  {
2322  $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2323  }
2324  | arg tRSHFT arg
2325  {
2326  $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2327  }
2328  | arg tANDOP arg
2329  {
2330  $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2331  }
2332  | arg tOROP arg
2333  {
2334  $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2335  }
2336  | keyword_defined opt_nl {p->in_defined = 1;} arg
2337  {
2338  p->in_defined = 0;
2339  $$ = new_defined(p, $4, &@$);
2340  }
2341  | arg '?' arg opt_nl ':' arg
2342  {
2343 #if 0
2344  value_expr($1);
2345  $$ = new_if(p, $1, $3, $6, &@$);
2346  fixpos($$, $1);
2347 #endif
2348  {VALUE v1,v2,v3,v4;v1=$1;v2=$3;v3=$6;v4=dispatch3(ifop,v1,v2,v3);$$=v4;}
2349  }
2350  | primary
2351  {
2352  $$ = $1;
2353  }
2354  ;
2355 
2356 relop : '>' {$$ = '>';}
2357  | '<' {$$ = '<';}
2358  | tGEQ {$$ = idGE;}
2359  | tLEQ {$$ = idLE;}
2360  ;
2361 
2362 rel_expr : arg relop arg %prec '>'
2363  {
2364  $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2365  }
2366  | rel_expr relop arg %prec '>'
2367  {
2368  rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2369  $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2370  }
2371  ;
2372 
2373 arg_value : arg
2374  {
2375  value_expr($1);
2376  $$ = $1;
2377  }
2378  ;
2379 
2380 aref_args : none
2381  | args trailer
2382  {
2383  $$ = $1;
2384  }
2385  | args ',' assocs trailer
2386  {
2387 #if 0
2388  $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2389 #endif
2390  {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;}
2391  }
2392  | assocs trailer
2393  {
2394 #if 0
2395  $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2396 #endif
2397  {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;}
2398  }
2399  ;
2400 
2401 arg_rhs : arg %prec tOP_ASGN
2402  {
2403  value_expr($1);
2404  $$ = $1;
2405  }
2406  | arg modifier_rescue arg
2407  {
2408 #if 0
2409  YYLTYPE loc = code_loc_gen(&@2, &@3);
2410  value_expr($1);
2411  $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
2412 #endif
2413  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
2414  }
2415  ;
2416 
2417 paren_args : '(' opt_call_args rparen
2418  {
2419 #if 0
2420  $$ = $2;
2421 #endif
2422  {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(arg_paren,v1);$$=v2;}
2423  }
2424  | '(' args_forward rparen
2425  {
2426  if (!local_id(p, idFWD_REST) ||
2427 #if idFWD_KWREST
2428  !local_id(p, idFWD_KWREST) ||
2429 #endif
2430  !local_id(p, idFWD_BLOCK)) {
2431  compile_error(p, "unexpected ...");
2432  $$ = Qnone;
2433  }
2434  else {
2435 #if 0
2436  NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@2), &@2);
2437 #if idFWD_KWREST
2438  NODE *kwrest = list_append(p, NEW_LIST(0, &@2), NEW_LVAR(idFWD_KWREST, &@2));
2439 #endif
2440  NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@2), &@2);
2441 #if idFWD_KWREST
2442  $$ = arg_append(p, splat, new_hash(p, kwrest, &@2), &@2);
2443 #else
2444  $$ = splat;
2445 #endif
2446  $$ = arg_blk_pass($$, block);
2447 #endif
2448  {VALUE v1,v2;v1=$2;v2=dispatch1(arg_paren,v1);$$=v2;}
2449  }
2450  }
2451  ;
2452 
2453 opt_paren_args : none
2454  | paren_args
2455  ;
2456 
2457 opt_call_args : none
2458  | call_args
2459  | args ','
2460  {
2461  $$ = $1;
2462  }
2463  | args ',' assocs ','
2464  {
2465 #if 0
2466  $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2467 #endif
2468  {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;}
2469  }
2470  | assocs ','
2471  {
2472 #if 0
2473  $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2474 #endif
2475  {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;}
2476  }
2477  ;
2478 
2479 call_args : command
2480  {
2481 #if 0
2482  value_expr($1);
2483  $$ = NEW_LIST($1, &@$);
2484 #endif
2485  {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2486  }
2487  | args opt_block_arg
2488  {
2489 #if 0
2490  $$ = arg_blk_pass($1, $2);
2491 #endif
2492  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(args_add_block,v1,v2);$$=v3;}
2493  }
2494  | assocs opt_block_arg
2495  {
2496 #if 0
2497  $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2498  $$ = arg_blk_pass($$, $2);
2499 #endif
2500  {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;}
2501  }
2502  | args ',' assocs opt_block_arg
2503  {
2504 #if 0
2505  $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2506  $$ = arg_blk_pass($$, $4);
2507 #endif
2508  {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;}
2509  }
2510  | block_arg
2511  {{VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add_block,v2,v3);$$=v4;}}
2512  ;
2513 
2514 command_args : {
2515  /* If call_args starts with a open paren '(' or '[',
2516  * look-ahead reading of the letters calls CMDARG_PUSH(0),
2517  * but the push must be done after CMDARG_PUSH(1).
2518  * So this code makes them consistent by first cancelling
2519  * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
2520  * and finally redoing CMDARG_PUSH(0).
2521  */
2522  int lookahead = 0;
2523  switch (yychar) {
2524  case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
2525  lookahead = 1;
2526  }
2527  if (lookahead) CMDARG_POP();
2528  CMDARG_PUSH(1);
2529  if (lookahead) CMDARG_PUSH(0);
2530  }
2531  call_args
2532  {
2533  /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
2534  * but the push must be done after CMDARG_POP() in the parser.
2535  * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
2536  * CMDARG_POP() to pop 1 pushed by command_args,
2537  * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
2538  */
2539  int lookahead = 0;
2540  switch (yychar) {
2541  case tLBRACE_ARG:
2542  lookahead = 1;
2543  }
2544  if (lookahead) CMDARG_POP();
2545  CMDARG_POP();
2546  if (lookahead) CMDARG_PUSH(0);
2547  $$ = $2;
2548  }
2549  ;
2550 
2551 block_arg : tAMPER arg_value
2552  {
2553 #if 0
2554  $$ = NEW_BLOCK_PASS($2, &@$);
2555 #endif
2556  $$=$2;
2557  }
2558  ;
2559 
2560 opt_block_arg : ',' block_arg
2561  {
2562  $$ = $2;
2563  }
2564  | none
2565  {
2566  $$ = 0;
2567  }
2568  ;
2569 
2570 args : arg_value
2571  {
2572 #if 0
2573  $$ = NEW_LIST($1, &@$);
2574 #endif
2575  {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2576  }
2577  | tSTAR arg_value
2578  {
2579 #if 0
2580  $$ = NEW_SPLAT($2, &@$);
2581 #endif
2582  {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
2583  }
2584  | args ',' arg_value
2585  {
2586 #if 0
2587  $$ = last_arg_append(p, $1, $3, &@$);
2588 #endif
2589  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
2590  }
2591  | args ',' tSTAR arg_value
2592  {
2593 #if 0
2594  $$ = rest_arg_append(p, $1, $4, &@$);
2595 #endif
2596  {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
2597  }
2598  ;
2599 
2600 mrhs_arg : mrhs
2601  | arg_value
2602  ;
2603 
2604 mrhs : args ',' arg_value
2605  {
2606 #if 0
2607  $$ = last_arg_append(p, $1, $3, &@$);
2608 #endif
2609  {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;}
2610  }
2611  | args ',' tSTAR arg_value
2612  {
2613 #if 0
2614  $$ = rest_arg_append(p, $1, $4, &@$);
2615 #endif
2616  {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;}
2617  }
2618  | tSTAR arg_value
2619  {
2620 #if 0
2621  $$ = NEW_SPLAT($2, &@$);
2622 #endif
2623  {VALUE v1,v2,v3,v4;v1=dispatch0(mrhs_new);v2=v1;v3=$2;v4=dispatch2(mrhs_add_star,v2,v3);$$=v4;}
2624  }
2625  ;
2626 
2627 primary : literal
2628  | strings
2629  | xstring
2630  | regexp
2631  | words
2632  | qwords
2633  | symbols
2634  | qsymbols
2635  | var_ref
2636  | backref
2637  | tFID
2638  {
2639 #if 0
2640  $$ = NEW_FCALL($1, 0, &@$);
2641 #endif
2642  {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;}
2643  }
2644  | k_begin
2645  {
2646  CMDARG_PUSH(0);
2647  }
2648  bodystmt
2649  k_end
2650  {
2651  CMDARG_POP();
2652 #if 0
2653  set_line_body($3, @1.end_pos.lineno);
2654  $$ = NEW_BEGIN($3, &@$);
2655  nd_set_line($$, @1.end_pos.lineno);
2656 #endif
2657  {VALUE v1,v2;v1=$3;v2=dispatch1(begin,v1);$$=v2;}
2658  }
2659  | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2660  {
2661 #if 0
2662  $$ = NEW_BEGIN(0, &@$);
2663 #endif
2664  {VALUE v1,v2;v1=0;v2=dispatch1(paren,v1);$$=v2;}
2665  }
2666  | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2667  {
2668 #if 0
2669  if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2670  $$ = $2;
2671 #endif
2672  {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2673  }
2674  | tLPAREN compstmt ')'
2675  {
2676 #if 0
2677  if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2678  $$ = $2;
2679 #endif
2680  {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2681  }
2682  | primary_value tCOLON2 tCONSTANT
2683  {
2684 #if 0
2685  $$ = NEW_COLON2($1, $3, &@$);
2686 #endif
2687  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2688  }
2689  | tCOLON3 tCONSTANT
2690  {
2691 #if 0
2692  $$ = NEW_COLON3($2, &@$);
2693 #endif
2694  {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2695  }
2696  | tLBRACK aref_args ']'
2697  {
2698 #if 0
2699  $$ = make_list($2, &@$);
2700 #endif
2701  {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(array,v1);$$=v2;}
2702  }
2703  | tLBRACE assoc_list '}'
2704  {
2705 #if 0
2706  $$ = new_hash(p, $2, &@$);
2707  $$->nd_brace = TRUE;
2708 #endif
2709  {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(hash,v1);$$=v2;}
2710  }
2711  | k_return
2712  {
2713 #if 0
2714  $$ = NEW_RETURN(0, &@$);
2715 #endif
2716  {VALUE v1;v1=dispatch0(return0);$$=v1;}
2717  }
2718  | keyword_yield '(' call_args rparen
2719  {
2720 #if 0
2721  $$ = new_yield(p, $3, &@$);
2722 #endif
2723  {VALUE v1,v2,v3,v4;v1=$3;v2=dispatch1(paren,v1);v3=v2;v4=dispatch1(yield,v3);$$=v4;}
2724  }
2725  | keyword_yield '(' rparen
2726  {
2727 #if 0
2728  $$ = NEW_YIELD(0, &@$);
2729 #endif
2730  {VALUE v1,v2,v3,v4,v5;v1=dispatch0(args_new);v2=v1;v3=dispatch1(paren,v2);v4=v3;v5=dispatch1(yield,v4);$$=v5;}
2731  }
2732  | keyword_yield
2733  {
2734 #if 0
2735  $$ = NEW_YIELD(0, &@$);
2736 #endif
2737  {VALUE v1;v1=dispatch0(yield0);$$=v1;}
2738  }
2739  | keyword_defined opt_nl '(' {p->in_defined = 1;} expr rparen
2740  {
2741  p->in_defined = 0;
2742  $$ = new_defined(p, $5, &@$);
2743  }
2744  | keyword_not '(' expr rparen
2745  {
2746  $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2747  }
2748  | keyword_not '(' rparen
2749  {
2750  $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
2751  }
2752  | fcall brace_block
2753  {
2754 #if 0
2755  $$ = method_add_block(p, $1, $2, &@$);
2756 #endif
2757  {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;}
2758  }
2759  | method_call
2760  | method_call brace_block
2761  {
2762 #if 0
2763  block_dup_check(p, $1->nd_args, $2);
2764  $$ = method_add_block(p, $1, $2, &@$);
2765 #endif
2766  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
2767  }
2768  | tLAMBDA
2769  {
2770  token_info_push(p, "->", &@1);
2771  }
2772  lambda
2773  {
2774  $$ = $3;
2775 #if 0
2776  nd_set_first_loc($$, @1.beg_pos);
2777 #endif
2778  }
2779  | k_if expr_value then
2780  compstmt
2781  if_tail
2782  k_end
2783  {
2784 #if 0
2785  $$ = new_if(p, $2, $4, $5, &@$);
2786  fixpos($$, $2);
2787 #endif
2788  {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(if,v1,v2,v3);$$=v4;}
2789  }
2790  | k_unless expr_value then
2791  compstmt
2792  opt_else
2793  k_end
2794  {
2795 #if 0
2796  $$ = new_unless(p, $2, $4, $5, &@$);
2797  fixpos($$, $2);
2798 #endif
2799  {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(unless,v1,v2,v3);$$=v4;}
2800  }
2801  | k_while expr_value_do
2802  compstmt
2803  k_end
2804  {
2805 #if 0
2806  $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
2807  fixpos($$, $2);
2808 #endif
2809  {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(while,v1,v2);$$=v3;}
2810  }
2811  | k_until expr_value_do
2812  compstmt
2813  k_end
2814  {
2815 #if 0
2816  $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
2817  fixpos($$, $2);
2818 #endif
2819  {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(until,v1,v2);$$=v3;}
2820  }
2821  | k_case expr_value opt_terms
2822  {
2823  $<val>$ = p->case_labels;
2824  p->case_labels = Qnil;
2825  }
2826  case_body
2827  k_end
2828  {
2829  if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2830  p->case_labels = $<val>4;
2831 #if 0
2832  $$ = NEW_CASE($2, $5, &@$);
2833  fixpos($$, $2);
2834 #endif
2835  {VALUE v1,v2,v3;v1=$2;v2=$5;v3=dispatch2(case,v1,v2);$$=v3;}
2836  }
2837  | k_case opt_terms
2838  {
2839  $<val>$ = p->case_labels;
2840  p->case_labels = 0;
2841  }
2842  case_body
2843  k_end
2844  {
2845  if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2846  p->case_labels = $<val>3;
2847 #if 0
2848  $$ = NEW_CASE2($4, &@$);
2849 #endif
2850  {VALUE v1,v2,v3;v1=Qnil;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
2851  }
2852  | k_case expr_value opt_terms
2853  p_case_body
2854  k_end
2855  {
2856 #if 0
2857  $$ = new_case3(p, $2, $4, &@$);
2858 #endif
2859  {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
2860  }
2861  | k_for for_var keyword_in expr_value_do
2862  compstmt
2863  k_end
2864  {
2865 #if 0
2866  /*
2867  * for a, b, c in e
2868  * #=>
2869  * e.each{|*x| a, b, c = x}
2870  *
2871  * for a in e
2872  * #=>
2873  * e.each{|x| a, = x}
2874  */
2875  ID id = internal_id(p);
2876  NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
2877  NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
2878  VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
2879  ID *tbl = ALLOC_N(ID, 3);
2880  rb_imemo_tmpbuf_set_ptr(tmpbuf, tbl);
2881  tbl[0] = 1 /* length of local var table */; tbl[1] = id /* internal id */;
2882  tbl[2] = tmpbuf;
2883 
2884  switch (nd_type($2)) {
2885  case NODE_LASGN:
2886  case NODE_DASGN:
2887  case NODE_DASGN_CURR: /* e.each {|internal_var| a = internal_var; ... } */
2888  $2->nd_value = internal_var;
2889  id = 0;
2890  m->nd_plen = 1;
2891  m->nd_next = $2;
2892  break;
2893  case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
2894  m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), &@2);
2895  break;
2896  default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
2897  m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, &@2);
2898  }
2899  /* {|*internal_id| <m> = internal_id; ... } */
2900  args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
2901  scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
2902  RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
2903  $$ = NEW_FOR($4, scope, &@$);
2904  fixpos($$, $2);
2905 #endif
2906  {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=$5;v4=dispatch3(for,v1,v2,v3);$$=v4;}
2907  }
2908  | k_class cpath superclass
2909  {
2910  if (p->in_def) {
2911  YYLTYPE loc = code_loc_gen(&@1, &@2);
2912  yyerror1(&loc, "class definition in method body");
2913  }
2914  $<num>1 = p->in_class;
2915  p->in_class = 1;
2916  local_push(p, 0);
2917  }
2918  bodystmt
2919  k_end
2920  {
2921 #if 0
2922  $$ = NEW_CLASS($2, $5, $3, &@$);
2923  nd_set_line($$->nd_body, @6.end_pos.lineno);
2924  set_line_body($5, @3.end_pos.lineno);
2925  nd_set_line($$, @3.end_pos.lineno);
2926 #endif
2927  {VALUE v1,v2,v3,v4;v1=$2;v2=$3;v3=$5;v4=dispatch3(class,v1,v2,v3);$$=v4;}
2928  local_pop(p);
2929  p->in_class = $<num>1 & 1;
2930  }
2931  | k_class tLSHFT expr
2932  {
2933  $<num>$ = (p->in_class << 1) | p->in_def;
2934  p->in_def = 0;
2935  p->in_class = 0;
2936  local_push(p, 0);
2937  }
2938  term
2939  bodystmt
2940  k_end
2941  {
2942 #if 0
2943  $$ = NEW_SCLASS($3, $6, &@$);
2944  nd_set_line($$->nd_body, @7.end_pos.lineno);
2945  set_line_body($6, nd_line($3));
2946  fixpos($$, $3);
2947 #endif
2948  {VALUE v1,v2,v3;v1=$3;v2=$6;v3=dispatch2(sclass,v1,v2);$$=v3;}
2949  local_pop(p);
2950  p->in_def = $<num>4 & 1;
2951  p->in_class = ($<num>4 >> 1) & 1;
2952  }
2953  | k_module cpath
2954  {
2955  if (p->in_def) {
2956  YYLTYPE loc = code_loc_gen(&@1, &@2);
2957  yyerror1(&loc, "module definition in method body");
2958  }
2959  $<num>1 = p->in_class;
2960  p->in_class = 1;
2961  local_push(p, 0);
2962  }
2963  bodystmt
2964  k_end
2965  {
2966 #if 0
2967  $$ = NEW_MODULE($2, $4, &@$);
2968  nd_set_line($$->nd_body, @5.end_pos.lineno);
2969  set_line_body($4, @2.end_pos.lineno);
2970  nd_set_line($$, @2.end_pos.lineno);
2971 #endif
2972  {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(module,v1,v2);$$=v3;}
2973  local_pop(p);
2974  p->in_class = $<num>1 & 1;
2975  }
2976  | k_def fname
2977  {
2978  numparam_name(p, get_id($2));
2979  local_push(p, 0);
2980  $<id>$ = p->cur_arg;
2981  p->cur_arg = 0;
2982  }
2983  {
2984  $<num>$ = p->in_def;
2985  p->in_def = 1;
2986  }
2987  f_arglist
2988  bodystmt
2989  k_end
2990  {
2991 #if 0
2992  NODE *body = remove_begin($6);
2993  reduce_nodes(p, &body);
2994  $$ = NEW_DEFN($2, $5, body, &@$);
2995  nd_set_line($$->nd_defn, @7.end_pos.lineno);
2996  set_line_body(body, @1.beg_pos.lineno);
2997 #endif
2998  {VALUE v1,v2,v3,v4;v1=$2;v2=$5;v3=$6;v4=dispatch3(def,v1,v2,v3);$$=v4;}
2999  local_pop(p);
3000  p->in_def = $<num>4 & 1;
3001  p->cur_arg = $<id>3;
3002  }
3003  | k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname
3004  {
3005  numparam_name(p, get_id($5));
3006  $<num>4 = p->in_def;
3007  p->in_def = 1;
3008  SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3009  local_push(p, 0);
3010  $<id>$ = p->cur_arg;
3011  p->cur_arg = 0;
3012  }
3013  f_arglist
3014  bodystmt
3015  k_end
3016  {
3017 #if 0
3018  NODE *body = remove_begin($8);
3019  reduce_nodes(p, &body);
3020  $$ = NEW_DEFS($2, $5, $7, body, &@$);
3021  nd_set_line($$->nd_defn, @9.end_pos.lineno);
3022  set_line_body(body, @1.beg_pos.lineno);
3023 #endif
3024  {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;}
3025  local_pop(p);
3026  p->in_def = $<num>4 & 1;
3027  p->cur_arg = $<id>6;
3028  }
3029  | keyword_break
3030  {
3031 #if 0
3032  $$ = NEW_BREAK(0, &@$);
3033 #endif
3034  {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(break,v2);$$=v3;}
3035  }
3036  | keyword_next
3037  {
3038 #if 0
3039  $$ = NEW_NEXT(0, &@$);
3040 #endif
3041  {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(next,v2);$$=v3;}
3042  }
3043  | keyword_redo
3044  {
3045 #if 0
3046  $$ = NEW_REDO(&@$);
3047 #endif
3048  {VALUE v1;v1=dispatch0(redo);$$=v1;}
3049  }
3050  | keyword_retry
3051  {
3052 #if 0
3053  $$ = NEW_RETRY(&@$);
3054 #endif
3055  {VALUE v1;v1=dispatch0(retry);$$=v1;}
3056  }
3057  ;
3058 
3059 primary_value : primary
3060  {
3061  value_expr($1);
3062  $$ = $1;
3063  }
3064  ;
3065 
3066 k_begin : keyword_begin
3067  {
3068  token_info_push(p, "begin", &@$);
3069  }
3070  ;
3071 
3072 k_if : keyword_if
3073  {
3074  WARN_EOL("if");
3075  token_info_push(p, "if", &@$);
3076  if (p->token_info && p->token_info->nonspc &&
3077  p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3078  const char *tok = p->lex.ptok;
3079  const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3080  beg += rb_strlen_lit("else");
3081  while (beg < tok && ISSPACE(*beg)) beg++;
3082  if (beg == tok) {
3083  p->token_info->nonspc = 0;
3084  }
3085  }
3086  }
3087  ;
3088 
3089 k_unless : keyword_unless
3090  {
3091  token_info_push(p, "unless", &@$);
3092  }
3093  ;
3094 
3095 k_while : keyword_while
3096  {
3097  token_info_push(p, "while", &@$);
3098  }
3099  ;
3100 
3101 k_until : keyword_until
3102  {
3103  token_info_push(p, "until", &@$);
3104  }
3105  ;
3106 
3107 k_case : keyword_case
3108  {
3109  token_info_push(p, "case", &@$);
3110  }
3111  ;
3112 
3113 k_for : keyword_for
3114  {
3115  token_info_push(p, "for", &@$);
3116  }
3117  ;
3118 
3119 k_class : keyword_class
3120  {
3121  token_info_push(p, "class", &@$);
3122  }
3123  ;
3124 
3125 k_module : keyword_module
3126  {
3127  token_info_push(p, "module", &@$);
3128  }
3129  ;
3130 
3131 k_def : keyword_def
3132  {
3133  token_info_push(p, "def", &@$);
3134  }
3135  ;
3136 
3137 k_do : keyword_do
3138  {
3139  token_info_push(p, "do", &@$);
3140  }
3141  ;
3142 
3143 k_do_block : keyword_do_block
3144  {
3145  token_info_push(p, "do", &@$);
3146  }
3147  ;
3148 
3149 k_rescue : keyword_rescue
3150  {
3151  token_info_warn(p, "rescue", p->token_info, 1, &@$);
3152  }
3153  ;
3154 
3155 k_ensure : keyword_ensure
3156  {
3157  token_info_warn(p, "ensure", p->token_info, 1, &@$);
3158  }
3159  ;
3160 
3161 k_when : keyword_when
3162  {
3163  token_info_warn(p, "when", p->token_info, 0, &@$);
3164  }
3165  ;
3166 
3167 k_else : keyword_else
3168  {
3169  token_info *ptinfo_beg = p->token_info;
3170  int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3171  token_info_warn(p, "else", p->token_info, same, &@$);
3172  if (same) {
3173  token_info e;
3174  e.next = ptinfo_beg->next;
3175  e.token = "else";
3176  token_info_setup(&e, p->lex.pbeg, &@$);
3177  if (!e.nonspc) *ptinfo_beg = e;
3178  }
3179  }
3180  ;
3181 
3182 k_elsif : keyword_elsif
3183  {
3184  WARN_EOL("elsif");
3185  token_info_warn(p, "elsif", p->token_info, 1, &@$);
3186  }
3187  ;
3188 
3189 k_end : keyword_end
3190  {
3191  token_info_pop(p, "end", &@$);
3192  }
3193  ;
3194 
3195 k_return : keyword_return
3196  {
3197  if (p->in_class && !p->in_def && !dyna_in_block(p))
3198  yyerror1(&@1, "Invalid return in class/module body");
3199  }
3200  ;
3201 
3202 then : term
3203  | keyword_then
3204  | term keyword_then
3205  ;
3206 
3207 do : term
3208  | keyword_do_cond
3209  ;
3210 
3211 if_tail : opt_else
3212  | k_elsif expr_value then
3213  compstmt
3214  if_tail
3215  {
3216 #if 0
3217  $$ = new_if(p, $2, $4, $5, &@$);
3218  fixpos($$, $2);
3219 #endif
3220  {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(elsif,v1,v2,v3);$$=v4;}
3221  }
3222  ;
3223 
3224 opt_else : none
3225  | k_else compstmt
3226  {
3227 #if 0
3228  $$ = $2;
3229 #endif
3230  {VALUE v1,v2;v1=$2;v2=dispatch1(else,v1);$$=v2;}
3231  }
3232  ;
3233 
3234 for_var : lhs
3235  | mlhs
3236  ;
3237 
3238 f_marg : f_norm_arg
3239  {
3240 #if 0
3241  $$ = assignable(p, $1, 0, &@$);
3242  mark_lvar_used(p, $$);
3243 #endif
3244  $$=assignable(p, $1);
3245  }
3246  | tLPAREN f_margs rparen
3247  {
3248 #if 0
3249  $$ = $2;
3250 #endif
3251  {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
3252  }
3253  ;
3254 
3255 f_marg_list : f_marg
3256  {
3257 #if 0
3258  $$ = NEW_LIST($1, &@$);
3259 #endif
3260  {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
3261  }
3262  | f_marg_list ',' f_marg
3263  {
3264 #if 0
3265  $$ = list_append(p, $1, $3);
3266 #endif
3267  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
3268  }
3269  ;
3270 
3271 f_margs : f_marg_list
3272  {
3273 #if 0
3274  $$ = NEW_MASGN($1, 0, &@$);
3275 #endif
3276  $$=$1;
3277  }
3278  | f_marg_list ',' f_rest_marg
3279  {
3280 #if 0
3281  $$ = NEW_MASGN($1, $3, &@$);
3282 #endif
3283  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
3284  }
3285  | f_marg_list ',' f_rest_marg ',' f_marg_list
3286  {
3287 #if 0
3288  $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3289 #endif
3290  {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;}
3291  }
3292  | f_rest_marg
3293  {
3294 #if 0
3295  $$ = NEW_MASGN(0, $1, &@$);
3296 #endif
3297  {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
3298  }
3299  | f_rest_marg ',' f_marg_list
3300  {
3301 #if 0
3302  $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3303 #endif
3304  {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;}
3305  }
3306  ;
3307 
3308 f_rest_marg : tSTAR f_norm_arg
3309  {
3310 #if 0
3311  $$ = assignable(p, $2, 0, &@$);
3312  mark_lvar_used(p, $$);
3313 #endif
3314  $$=assignable(p, $2);
3315  }
3316  | tSTAR
3317  {
3318 #if 0
3319  $$ = NODE_SPECIAL_NO_NAME_REST;
3320 #endif
3321  $$=Qnil;
3322  }
3323  ;
3324 
3325 block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3326  {
3327  $$ = new_args_tail(p, $1, $3, $4, &@3);
3328  }
3329  | f_block_kwarg opt_f_block_arg
3330  {
3331  $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3332  }
3333  | f_kwrest opt_f_block_arg
3334  {
3335  $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3336  }
3337  | f_no_kwarg opt_f_block_arg
3338  {
3339  $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
3340  }
3341  | f_block_arg
3342  {
3343  $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3344  }
3345  ;
3346 
3347 opt_block_args_tail : ',' block_args_tail
3348  {
3349  $$ = $2;
3350  }
3351  | /* none */
3352  {
3353  $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3354  }
3355  ;
3356 
3357 block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3358  {
3359  $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3360  }
3361  | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3362  {
3363  $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3364  }
3365  | f_arg ',' f_block_optarg opt_block_args_tail
3366  {
3367  $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3368  }
3369  | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3370  {
3371  $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3372  }
3373  | f_arg ',' f_rest_arg opt_block_args_tail
3374  {
3375  $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3376  }
3377  | f_arg ','
3378  {
3379 #if 0
3380  /* magic number for rest_id in iseq_set_arguments() */
3381  $$ = new_args(p, $1, Qnone, NODE_SPECIAL_EXCESSIVE_COMMA, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, &@1), &@$);
3382 #endif
3383  {VALUE v1;v1=dispatch0(excessed_comma);$$=new_args(p, $1, Qnone, v1, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, NULL), NULL);}
3384  }
3385  | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3386  {
3387  $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
3388  }
3389  | f_arg opt_block_args_tail
3390  {
3391  $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
3392  }
3393  | f_block_optarg ',' f_rest_arg opt_block_args_tail
3394  {
3395  $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
3396  }
3397  | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3398  {
3399  $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
3400  }
3401  | f_block_optarg opt_block_args_tail
3402  {
3403  $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
3404  }
3405  | f_block_optarg ',' f_arg opt_block_args_tail
3406  {
3407  $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
3408  }
3409  | f_rest_arg opt_block_args_tail
3410  {
3411  $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
3412  }
3413  | f_rest_arg ',' f_arg opt_block_args_tail
3414  {
3415  $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
3416  }
3417  | block_args_tail
3418  {
3419  $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
3420  }
3421  ;
3422 
3423 opt_block_param : none
3424  | block_param_def
3425  {
3426  p->command_start = TRUE;
3427  }
3428  ;
3429 
3430 block_param_def : '|' opt_bv_decl '|'
3431  {
3432  p->cur_arg = 0;
3433  p->max_numparam = ORDINAL_PARAM;
3434 #if 0
3435  $$ = 0;
3436 #endif
3437  {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;}
3438  }
3439  | '|' block_param opt_bv_decl '|'
3440  {
3441  p->cur_arg = 0;
3442  p->max_numparam = ORDINAL_PARAM;
3443 #if 0
3444  $$ = $2;
3445 #endif
3446  {VALUE v1,v2,v3;v1=escape_Qundef($2);v2=escape_Qundef($3);v3=dispatch2(block_var,v1,v2);$$=v3;}
3447  }
3448  ;
3449 
3450 
3451 opt_bv_decl : opt_nl
3452  {
3453  $$ = 0;
3454  }
3455  | opt_nl ';' bv_decls opt_nl
3456  {
3457 #if 0
3458  $$ = 0;
3459 #endif
3460  $$=$3;
3461  }
3462  ;
3463 
3464 bv_decls : bvar
3465  {$$=rb_ary_new3(1, get_value($1));}
3466  | bv_decls ',' bvar
3467  {$$=rb_ary_push($1, get_value($3));}
3468  ;
3469 
3470 bvar : tIDENTIFIER
3471  {
3472  new_bv(p, get_id($1));
3473  $$=get_value($1);
3474  }
3475  | f_bad_arg
3476  {
3477  $$ = 0;
3478  }
3479  ;
3480 
3481 lambda : {
3482  $<vars>$ = dyna_push(p);
3483  }
3484  {
3485  $<num>$ = p->lex.lpar_beg;
3486  p->lex.lpar_beg = p->lex.paren_nest;
3487  }
3488  {
3489  $<num>$ = p->max_numparam;
3490  p->max_numparam = 0;
3491  }
3492  {
3493  $<node>$ = numparam_push(p);
3494  }
3495  f_larglist
3496  {
3497  CMDARG_PUSH(0);
3498  }
3499  lambda_body
3500  {
3501  int max_numparam = p->max_numparam;
3502  p->lex.lpar_beg = $<num>2;
3503  p->max_numparam = $<num>3;
3504  CMDARG_POP();
3505  $5 = args_with_numbered(p, $5, max_numparam);
3506 #if 0
3507  {
3508  YYLTYPE loc = code_loc_gen(&@5, &@7);
3509  $$ = NEW_LAMBDA($5, $7, &loc);
3510  nd_set_line($$->nd_body, @7.end_pos.lineno);
3511  nd_set_line($$, @5.end_pos.lineno);
3512  }
3513 #endif
3514  {VALUE v1,v2,v3;v1=$5;v2=$7;v3=dispatch2(lambda,v1,v2);$$=v3;}
3515  numparam_pop(p, $<node>4);
3516  dyna_pop(p, $<vars>1);
3517  }
3518  ;
3519 
3520 f_larglist : '(' f_args opt_bv_decl ')'
3521  {
3522 #if 0
3523  $$ = $2;
3524  p->max_numparam = ORDINAL_PARAM;
3525 #endif
3526  {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
3527  }
3528  | f_args
3529  {
3530 #if 0
3531  if (!args_info_empty_p($1->nd_ainfo))
3532  p->max_numparam = ORDINAL_PARAM;
3533 #endif
3534  $$ = $1;
3535  }
3536  ;
3537 
3538 lambda_body : tLAMBEG compstmt '}'
3539  {
3540  token_info_pop(p, "}", &@3);
3541  $$ = $2;
3542  }
3543  | keyword_do_LAMBDA bodystmt k_end
3544  {
3545  $$ = $2;
3546  }
3547  ;
3548 
3549 do_block : k_do_block do_body k_end
3550  {
3551  $$ = $2;
3552 #if 0
3553  $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3554  nd_set_line($$, @1.end_pos.lineno);
3555 #endif
3556  }
3557  ;
3558 
3559 block_call : command do_block
3560  {
3561 #if 0
3562  if (nd_type($1) == NODE_YIELD) {
3563  compile_error(p, "block given to yield");
3564  }
3565  else {
3566  block_dup_check(p, $1->nd_args, $2);
3567  }
3568  $$ = method_add_block(p, $1, $2, &@$);
3569  fixpos($$, $1);
3570 #endif
3571  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
3572  }
3573  | block_call call_op2 operation2 opt_paren_args
3574  {
3575 #if 0
3576  $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3577 #endif
3578  {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;}
3579  }
3580  | block_call call_op2 operation2 opt_paren_args brace_block
3581  {
3582 #if 0
3583  $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3584 #endif
3585  {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;}
3586  }
3587  | block_call call_op2 operation2 command_args do_block
3588  {
3589 #if 0
3590  $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3591 #endif
3592  {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;}
3593  }
3594  ;
3595 
3596 method_call : fcall paren_args
3597  {
3598 #if 0
3599  $$ = $1;
3600  $$->nd_args = $2;
3601  nd_set_last_loc($1, @2.end_pos);
3602 #endif
3603  {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(fcall,v1);v3=v2;v4=$2;v5=dispatch2(method_add_arg,v3,v4);$$=v5;}
3604  }
3605  | primary_value call_op operation2 opt_paren_args
3606  {
3607 #if 0
3608  $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3609  nd_set_line($$, @3.end_pos.lineno);
3610 #endif
3611  {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;}
3612  }
3613  | primary_value tCOLON2 operation2 paren_args
3614  {
3615 #if 0
3616  $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
3617  nd_set_line($$, @3.end_pos.lineno);
3618 #endif
3619  {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;}
3620  }
3621  | primary_value tCOLON2 operation3
3622  {
3623 #if 0
3624  $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
3625 #endif
3626  {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(call,v1,v2,v3);$$=v4;}
3627  }
3628  | primary_value call_op paren_args
3629  {
3630 #if 0
3631  $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
3632  nd_set_line($$, @2.end_pos.lineno);
3633 #endif
3634  {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;}
3635  }
3636  | primary_value tCOLON2 paren_args
3637  {
3638 #if 0
3639  $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
3640  nd_set_line($$, @2.end_pos.lineno);
3641 #endif
3642  {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;}
3643  }
3644  | keyword_super paren_args
3645  {
3646 #if 0
3647  $$ = NEW_SUPER($2, &@$);
3648 #endif
3649  {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
3650  }
3651  | keyword_super
3652  {
3653 #if 0
3654  $$ = NEW_ZSUPER(&@$);
3655 #endif
3656  {VALUE v1;v1=dispatch0(zsuper);$$=v1;}
3657  }
3658  | primary_value '[' opt_call_args rbracket
3659  {
3660 #if 0
3661  if ($1 && nd_type($1) == NODE_SELF)
3662  $$ = NEW_FCALL(tAREF, $3, &@$);
3663  else
3664  $$ = NEW_CALL($1, tAREF, $3, &@$);
3665  fixpos($$, $1);
3666 #endif
3667  {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref,v1,v2);$$=v3;}
3668  }
3669  ;
3670 
3671 brace_block : '{' brace_body '}'
3672  {
3673  $$ = $2;
3674 #if 0
3675  $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3676  nd_set_line($$, @1.end_pos.lineno);
3677 #endif
3678  }
3679  | k_do do_body k_end
3680  {
3681  $$ = $2;
3682 #if 0
3683  $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3684  nd_set_line($$, @1.end_pos.lineno);
3685 #endif
3686  }
3687  ;
3688 
3689 brace_body : {$<vars>$ = dyna_push(p);}
3690  {
3691  $<num>$ = p->max_numparam;
3692  p->max_numparam = 0;
3693  }
3694  {
3695  $<node>$ = numparam_push(p);
3696  }
3697  opt_block_param compstmt
3698  {
3699  int max_numparam = p->max_numparam;
3700  p->max_numparam = $<num>2;
3701  $4 = args_with_numbered(p, $4, max_numparam);
3702 #if 0
3703  $$ = NEW_ITER($4, $5, &@$);
3704 #endif
3705  {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(brace_block,v1,v2);$$=v3;}
3706  numparam_pop(p, $<node>3);
3707  dyna_pop(p, $<vars>1);
3708  }
3709  ;
3710 
3711 do_body : {$<vars>$ = dyna_push(p);}
3712  {
3713  $<num>$ = p->max_numparam;
3714  p->max_numparam = 0;
3715  }
3716  {
3717  $<node>$ = numparam_push(p);
3718  CMDARG_PUSH(0);
3719  }
3720  opt_block_param bodystmt
3721  {
3722  int max_numparam = p->max_numparam;
3723  p->max_numparam = $<num>2;
3724  $4 = args_with_numbered(p, $4, max_numparam);
3725 #if 0
3726  $$ = NEW_ITER($4, $5, &@$);
3727 #endif
3728  {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(do_block,v1,v2);$$=v3;}
3729  CMDARG_POP();
3730  numparam_pop(p, $<node>3);
3731  dyna_pop(p, $<vars>1);
3732  }
3733  ;
3734 
3735 case_args : arg_value
3736  {
3737 #if 0
3738  check_literal_when(p, $1, &@1);
3739  $$ = NEW_LIST($1, &@$);
3740 #endif
3741  {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
3742  }
3743  | tSTAR arg_value
3744  {
3745 #if 0
3746  $$ = NEW_SPLAT($2, &@$);
3747 #endif
3748  {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
3749  }
3750  | case_args ',' arg_value
3751  {
3752 #if 0
3753  check_literal_when(p, $3, &@3);
3754  $$ = last_arg_append(p, $1, $3, &@$);
3755 #endif
3756  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
3757  }
3758  | case_args ',' tSTAR arg_value
3759  {
3760 #if 0
3761  $$ = rest_arg_append(p, $1, $4, &@$);
3762 #endif
3763  {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
3764  }
3765  ;
3766 
3767 case_body : k_when case_args then
3768  compstmt
3769  cases
3770  {
3771 #if 0
3772  $$ = NEW_WHEN($2, $4, $5, &@$);
3773  fixpos($$, $2);
3774 #endif
3775  {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(when,v1,v2,v3);$$=v4;}
3776  }
3777  ;
3778 
3779 cases : opt_else
3780  | case_body
3781  ;
3782 
3783 p_case_body : keyword_in
3784  {
3785  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
3786  p->command_start = FALSE;
3787  $<num>$ = p->in_kwarg;
3788  p->in_kwarg = 1;
3789  }
3790  {$<tbl>$ = push_pvtbl(p);}
3791  {$<tbl>$ = push_pktbl(p);}
3792  p_top_expr then
3793  {pop_pktbl(p, $<tbl>4);}
3794  {pop_pvtbl(p, $<tbl>3);}
3795  {
3796  p->in_kwarg = !!$<num>2;
3797  }
3798  compstmt
3799  p_cases
3800  {
3801 #if 0
3802  $$ = NEW_IN($5, $10, $11, &@$);
3803 #endif
3804  {VALUE v1,v2,v3,v4;v1=$5;v2=$10;v3=escape_Qundef($11);v4=dispatch3(in,v1,v2,v3);$$=v4;}
3805  }
3806  ;
3807 
3808 p_cases : opt_else
3809  | p_case_body
3810  ;
3811 
3812 p_top_expr : p_top_expr_body
3813  | p_top_expr_body modifier_if expr_value
3814  {
3815 #if 0
3816  $$ = new_if(p, $3, remove_begin($1), 0, &@$);
3817  fixpos($$, $3);
3818 #endif
3819  {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
3820  }
3821  | p_top_expr_body modifier_unless expr_value
3822  {
3823 #if 0
3824  $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
3825  fixpos($$, $3);
3826 #endif
3827  {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
3828  }
3829  ;
3830 
3831 p_top_expr_body : p_expr
3832  | p_expr ','
3833  {
3834  $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
3835  $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
3836  }
3837  | p_expr ',' p_args
3838  {
3839  $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
3840 #if 0
3841  nd_set_first_loc($$, @1.beg_pos);
3842 #endif
3843 
3844  }
3845  | p_args_tail
3846  {
3847  $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
3848  }
3849  | p_kwargs
3850  {
3851  $$ = new_hash_pattern(p, Qnone, $1, &@$);
3852  }
3853  ;
3854 
3855 p_expr : p_as
3856  ;
3857 
3858 p_as : p_expr tASSOC p_variable
3859  {
3860 #if 0
3861  NODE *n = NEW_LIST($1, &@$);
3862  n = list_append(p, n, $3);
3863  $$ = new_hash(p, n, &@$);
3864 #endif
3865  {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(id_assoc);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
3866  }
3867  | p_alt
3868  ;
3869 
3870 p_alt : p_alt '|' p_expr_basic
3871  {
3872 #if 0
3873  $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
3874 #endif
3875  {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(idOr);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
3876  }
3877  | p_expr_basic
3878  ;
3879 
3880 p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
3881 p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
3882 
3883 p_expr_basic : p_value
3884  | p_const p_lparen p_args rparen
3885  {
3886  pop_pktbl(p, $<tbl>2);
3887  $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3888 #if 0
3889  nd_set_first_loc($$, @1.beg_pos);
3890 #endif
3891 
3892  }
3893  | p_const p_lparen p_kwargs rparen
3894  {
3895  pop_pktbl(p, $<tbl>2);
3896  $$ = new_hash_pattern(p, $1, $3, &@$);
3897 #if 0
3898  nd_set_first_loc($$, @1.beg_pos);
3899 #endif
3900 
3901  }
3902  | p_const '(' rparen
3903  {
3904  $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3905  $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3906  }
3907  | p_const p_lbracket p_args rbracket
3908  {
3909  pop_pktbl(p, $<tbl>2);
3910  $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3911 #if 0
3912  nd_set_first_loc($$, @1.beg_pos);
3913 #endif
3914 
3915  }
3916  | p_const p_lbracket p_kwargs rbracket
3917  {
3918  pop_pktbl(p, $<tbl>2);
3919  $$ = new_hash_pattern(p, $1, $3, &@$);
3920 #if 0
3921  nd_set_first_loc($$, @1.beg_pos);
3922 #endif
3923 
3924  }
3925  | p_const '[' rbracket
3926  {
3927  $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3928  $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3929  }
3930  | tLBRACK {$<tbl>$ = push_pktbl(p);} p_args rbracket
3931  {
3932  pop_pktbl(p, $<tbl>2);
3933  $$ = new_array_pattern(p, Qnone, Qnone, $3, &@$);
3934  }
3935  | tLBRACK rbracket
3936  {
3937  $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3938  $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
3939  }
3940  | tLBRACE
3941  {
3942  $<tbl>$ = push_pktbl(p);
3943  $<num>1 = p->in_kwarg;
3944  p->in_kwarg = 0;
3945  }
3946  p_kwargs rbrace
3947  {
3948  pop_pktbl(p, $<tbl>2);
3949  p->in_kwarg = $<num>1;
3950  $$ = new_hash_pattern(p, Qnone, $3, &@$);
3951  }
3952  | tLBRACE rbrace
3953  {
3954  $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
3955  $$ = new_hash_pattern(p, Qnone, $$, &@$);
3956  }
3957  | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
3958  {
3959  pop_pktbl(p, $<tbl>2);
3960  $$ = $3;
3961  }
3962  ;
3963 
3964 p_args : p_expr
3965  {
3966 #if 0
3967  NODE *pre_args = NEW_LIST($1, &@$);
3968  $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
3969 #endif
3970  $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
3971 
3972  }
3973  | p_args_head
3974  {
3975  $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
3976  }
3977  | p_args_head p_arg
3978  {
3979 #if 0
3980  $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
3981 #endif
3982  VALUE pre_args = rb_ary_concat($1, get_value($2));
3983  $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
3984 
3985  }
3986  | p_args_head tSTAR tIDENTIFIER
3987  {
3988  $$ = new_array_pattern_tail(p, $1, 1, $3, Qnone, &@$);
3989  }
3990  | p_args_head tSTAR tIDENTIFIER ',' p_args_post
3991  {
3992  $$ = new_array_pattern_tail(p, $1, 1, $3, $5, &@$);
3993  }
3994  | p_args_head tSTAR
3995  {
3996  $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
3997  }
3998  | p_args_head tSTAR ',' p_args_post
3999  {
4000  $$ = new_array_pattern_tail(p, $1, 1, 0, $4, &@$);
4001  }
4002  | p_args_tail
4003  ;
4004 
4005 p_args_head : p_arg ','
4006  {
4007  $$ = $1;
4008  }
4009  | p_args_head p_arg ','
4010  {
4011 #if 0
4012  $$ = list_concat($1, $2);
4013 #endif
4014  $$=rb_ary_concat($1, get_value($2));
4015  }
4016  ;
4017 
4018 p_args_tail : tSTAR tIDENTIFIER
4019  {
4020  $$ = new_array_pattern_tail(p, Qnone, 1, $2, Qnone, &@$);
4021  }
4022  | tSTAR tIDENTIFIER ',' p_args_post
4023  {
4024  $$ = new_array_pattern_tail(p, Qnone, 1, $2, $4, &@$);
4025  }
4026  | tSTAR
4027  {
4028  $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4029  }
4030  | tSTAR ',' p_args_post
4031  {
4032  $$ = new_array_pattern_tail(p, Qnone, 1, 0, $3, &@$);
4033  }
4034  ;
4035 
4036 p_args_post : p_arg
4037  | p_args_post ',' p_arg
4038  {
4039 #if 0
4040  $$ = list_concat($1, $3);
4041 #endif
4042  $$=rb_ary_concat($1, get_value($3));
4043  }
4044  ;
4045 
4046 p_arg : p_expr
4047  {
4048 #if 0
4049  $$ = NEW_LIST($1, &@$);
4050 #endif
4051  $$=rb_ary_new_from_args(1, get_value($1));
4052  }
4053  ;
4054 
4055 p_kwargs : p_kwarg ',' p_kwrest
4056  {
4057  $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4058  }
4059  | p_kwarg
4060  {
4061  $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4062  }
4063  | p_kwarg ','
4064  {
4065  $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4066  }
4067  | p_kwrest
4068  {
4069  $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4070  }
4071  | p_kwarg ',' p_kwnorest
4072  {
4073  $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), ID2VAL(idNil), &@$);
4074  }
4075  | p_kwnorest
4076  {
4077  $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), ID2VAL(idNil), &@$);
4078  }
4079  ;
4080 
4081 p_kwarg : p_kw
4082  {$$=rb_ary_new_from_args(1, $1);}
4083  | p_kwarg ',' p_kw
4084  {
4085 #if 0
4086  $$ = list_concat($1, $3);
4087 #endif
4088  $$=rb_ary_push($1, $3);
4089  }
4090  ;
4091 
4092 p_kw : p_kw_label p_expr
4093  {
4094  error_duplicate_pattern_key(p, get_id($1), &@1);
4095 #if 0
4096  $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), $2);
4097 #endif
4098  $$=rb_ary_new_from_args(2, get_value($1), get_value($2));
4099  }
4100  | p_kw_label
4101  {
4102  error_duplicate_pattern_key(p, get_id($1), &@1);
4103  if ($1 && !is_local_id(get_id($1))) {
4104  yyerror1(&@1, "key must be valid as local variables");
4105  }
4106  error_duplicate_pattern_variable(p, get_id($1), &@1);
4107 #if 0
4108  $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4109 #endif
4110  $$=rb_ary_new_from_args(2, get_value($1), Qnil);
4111  }
4112  ;
4113 
4114 p_kw_label : tLABEL
4115  | tSTRING_BEG string_contents tLABEL_END
4116  {
4117  YYLTYPE loc = code_loc_gen(&@1, &@3);
4118 #if 0
4119  if (!$2 || nd_type($2) == NODE_STR) {
4120  NODE *node = dsym_node(p, $2, &loc);
4121  $$ = SYM2ID(node->nd_lit);
4122  }
4123 #endif
4124  if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4125  VALUE label = RNODE($2)->nd_cval;
4126  VALUE rval = RNODE($2)->nd_rval;
4127  $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4128  RNODE($$)->nd_loc = loc;
4129  }
4130 
4131  else {
4132  yyerror1(&loc, "symbol literal with interpolation is not allowed");
4133  $$ = 0;
4134  }
4135  }
4136  ;
4137 
4138 p_kwrest : kwrest_mark tIDENTIFIER
4139  {
4140  $$ = $2;
4141  }
4142  | kwrest_mark
4143  {
4144  $$ = 0;
4145  }
4146  ;
4147 
4148 p_kwnorest : kwrest_mark keyword_nil
4149  {
4150  $$ = 0;
4151  }
4152  ;
4153 
4154 p_value : p_primitive
4155  | p_primitive tDOT2 p_primitive
4156  {
4157 #if 0
4158  value_expr($1);
4159  value_expr($3);
4160  $$ = NEW_DOT2($1, $3, &@$);
4161 #endif
4162  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
4163  }
4164  | p_primitive tDOT3 p_primitive
4165  {
4166 #if 0
4167  value_expr($1);
4168  value_expr($3);
4169  $$ = NEW_DOT3($1, $3, &@$);
4170 #endif
4171  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
4172  }
4173  | p_primitive tDOT2
4174  {
4175 #if 0
4176  YYLTYPE loc;
4177  loc.beg_pos = @2.end_pos;
4178  loc.end_pos = @2.end_pos;
4179 
4180  value_expr($1);
4181  $$ = NEW_DOT2($1, new_nil(&loc), &@$);
4182 #endif
4183  {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
4184  }
4185  | p_primitive tDOT3
4186  {
4187 #if 0
4188  YYLTYPE loc;
4189  loc.beg_pos = @2.end_pos;
4190  loc.end_pos = @2.end_pos;
4191 
4192  value_expr($1);
4193  $$ = NEW_DOT3($1, new_nil(&loc), &@$);
4194 #endif
4195  {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
4196  }
4197  | p_variable
4198  | p_var_ref
4199  | p_const
4200  | tBDOT2 p_primitive
4201  {
4202 #if 0
4203  YYLTYPE loc;
4204  loc.beg_pos = @1.beg_pos;
4205  loc.end_pos = @1.beg_pos;
4206 
4207  value_expr($2);
4208  $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
4209 #endif
4210  {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
4211  }
4212  | tBDOT3 p_primitive
4213  {
4214 #if 0
4215  YYLTYPE loc;
4216  loc.beg_pos = @1.beg_pos;
4217  loc.end_pos = @1.beg_pos;
4218 
4219  value_expr($2);
4220  $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
4221 #endif
4222  {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
4223  }
4224  ;
4225 
4226 p_primitive : literal
4227  | strings
4228  | xstring
4229  | regexp
4230  | words
4231  | qwords
4232  | symbols
4233  | qsymbols
4234  | keyword_variable
4235  {
4236 #if 0
4237  if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4238 #endif
4239  {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4240  }
4241  | tLAMBDA
4242  {
4243  token_info_push(p, "->", &@1);
4244  }
4245  lambda
4246  {
4247  $$ = $3;
4248 #if 0
4249  nd_set_first_loc($$, @1.beg_pos);
4250 #endif
4251  }
4252  ;
4253 
4254 p_variable : tIDENTIFIER
4255  {
4256 #if 0
4257  error_duplicate_pattern_variable(p, $1, &@1);
4258  $$ = assignable(p, $1, 0, &@$);
4259 #endif
4260  $$=assignable(p, var_field(p, $1));
4261  }
4262  ;
4263 
4264 p_var_ref : '^' tIDENTIFIER
4265  {
4266 #if 0
4267  NODE *n = gettable(p, $2, &@$);
4268  if (!(nd_type(n) == NODE_LVAR || nd_type(n) == NODE_DVAR)) {
4269  compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4270  }
4271  $$ = n;
4272 #endif
4273  {VALUE v1,v2;v1=$2;v2=dispatch1(var_ref,v1);$$=v2;}
4274  }
4275  ;
4276 
4277 p_const : tCOLON3 cname
4278  {
4279 #if 0
4280  $$ = NEW_COLON3($2, &@$);
4281 #endif
4282  {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
4283  }
4284  | p_const tCOLON2 cname
4285  {
4286 #if 0
4287  $$ = NEW_COLON2($1, $3, &@$);
4288 #endif
4289  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
4290  }
4291  | tCONSTANT
4292  {
4293 #if 0
4294  $$ = gettable(p, $1, &@$);
4295 #endif
4296  {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4297  }
4298  ;
4299 
4300 opt_rescue : k_rescue exc_list exc_var then
4301  compstmt
4302  opt_rescue
4303  {
4304 #if 0
4305  $$ = NEW_RESBODY($2,
4306  $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), &@3), $5) : $5,
4307  $6, &@$);
4308  fixpos($$, $2?$2:$5);
4309 #endif
4310  {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;}
4311  }
4312  | none
4313  ;
4314 
4315 exc_list : arg_value
4316  {
4317 #if 0
4318  $$ = NEW_LIST($1, &@$);
4319 #endif
4320  $$=rb_ary_new3(1, get_value($1));
4321  }
4322  | mrhs
4323  {
4324 #if 0
4325  if (!($$ = splat_array($1))) $$ = $1;
4326 #endif
4327  $$=$1;
4328  }
4329  | none
4330  ;
4331 
4332 exc_var : tASSOC lhs
4333  {
4334  $$ = $2;
4335  }
4336  | none
4337  ;
4338 
4339 opt_ensure : k_ensure compstmt
4340  {
4341 #if 0
4342  $$ = $2;
4343 #endif
4344  {VALUE v1,v2;v1=$2;v2=dispatch1(ensure,v1);$$=v2;}
4345  }
4346  | none
4347  ;
4348 
4349 literal : numeric
4350  | symbol
4351  ;
4352 
4353 strings : string
4354  {
4355 #if 0
4356  NODE *node = $1;
4357  if (!node) {
4358  node = NEW_STR(STR_NEW0(), &@$);
4359  RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
4360  }
4361  else {
4362  node = evstr2dstr(p, node);
4363  }
4364  $$ = node;
4365 #endif
4366  $$=$1;
4367  }
4368  ;
4369 
4370 string : tCHAR
4371  | string1
4372  | string string1
4373  {
4374 #if 0
4375  $$ = literal_concat(p, $1, $2, &@$);
4376 #endif
4377  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_concat,v1,v2);$$=v3;}
4378  }
4379  ;
4380 
4381 string1 : tSTRING_BEG string_contents tSTRING_END
4382  {
4383 #if 0
4384  $$ = heredoc_dedent(p, $2);
4385  if ($$) nd_set_loc($$, &@$);
4386 #endif
4387  {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(string_literal,v1);$$=v2;}
4388  }
4389  ;
4390 
4391 xstring : tXSTRING_BEG xstring_contents tSTRING_END
4392  {
4393 #if 0
4394  $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4395 #endif
4396  {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(xstring_literal,v1);$$=v2;}
4397  }
4398  ;
4399 
4400 regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4401  {
4402  $$ = new_regexp(p, $2, $3, &@$);
4403  }
4404  ;
4405 
4406 words : tWORDS_BEG ' ' word_list tSTRING_END
4407  {
4408 #if 0
4409  $$ = make_list($3, &@$);
4410 #endif
4411  {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4412  }
4413  ;
4414 
4415 word_list : /* none */
4416  {
4417 #if 0
4418  $$ = 0;
4419 #endif
4420  {VALUE v1;v1=dispatch0(words_new);$$=v1;}
4421  }
4422  | word_list word ' '
4423  {
4424 #if 0
4425  $$ = list_append(p, $1, evstr2dstr(p, $2));
4426 #endif
4427  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(words_add,v1,v2);$$=v3;}
4428  }
4429  ;
4430 
4431 word : string_content
4432  {{VALUE v1,v2,v3,v4;v1=dispatch0(word_new);v2=v1;v3=$1;v4=dispatch2(word_add,v2,v3);$$=v4;}}
4433  | word string_content
4434  {
4435 #if 0
4436  $$ = literal_concat(p, $1, $2, &@$);
4437 #endif
4438  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(word_add,v1,v2);$$=v3;}
4439  }
4440  ;
4441 
4442 symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4443  {
4444 #if 0
4445  $$ = make_list($3, &@$);
4446 #endif
4447  {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4448  }
4449  ;
4450 
4451 symbol_list : /* none */
4452  {
4453 #if 0
4454  $$ = 0;
4455 #endif
4456  {VALUE v1;v1=dispatch0(symbols_new);$$=v1;}
4457  }
4458  | symbol_list word ' '
4459  {
4460 #if 0
4461  $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4462 #endif
4463  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(symbols_add,v1,v2);$$=v3;}
4464  }
4465  ;
4466 
4467 qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4468  {
4469 #if 0
4470  $$ = make_list($3, &@$);
4471 #endif
4472  {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4473  }
4474  ;
4475 
4476 qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4477  {
4478 #if 0
4479  $$ = make_list($3, &@$);
4480 #endif
4481  {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4482  }
4483  ;
4484 
4485 qword_list : /* none */
4486  {
4487 #if 0
4488  $$ = 0;
4489 #endif
4490  {VALUE v1;v1=dispatch0(qwords_new);$$=v1;}
4491  }
4492  | qword_list tSTRING_CONTENT ' '
4493  {
4494 #if 0
4495  $$ = list_append(p, $1, $2);
4496 #endif
4497  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qwords_add,v1,v2);$$=v3;}
4498  }
4499  ;
4500 
4501 qsym_list : /* none */
4502  {
4503 #if 0
4504  $$ = 0;
4505 #endif
4506  {VALUE v1;v1=dispatch0(qsymbols_new);$$=v1;}
4507  }
4508  | qsym_list tSTRING_CONTENT ' '
4509  {
4510 #if 0
4511  $$ = symbol_append(p, $1, $2);
4512 #endif
4513  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qsymbols_add,v1,v2);$$=v3;}
4514  }
4515  ;
4516 
4517 string_contents : /* none */
4518  {
4519 #if 0
4520  $$ = 0;
4521 #endif
4522  {VALUE v1;v1=dispatch0(string_content);$$=v1;}
4523 #if 0
4524 #endif
4525  $$ = ripper_new_yylval(p, 0, $$, 0);
4526 
4527  }
4528  | string_contents string_content
4529  {
4530 #if 0
4531  $$ = literal_concat(p, $1, $2, &@$);
4532 #endif
4533  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_add,v1,v2);$$=v3;}
4534 #if 0
4535 #endif
4536  if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
4537  !RNODE($1)->nd_cval) {
4538  RNODE($1)->nd_cval = RNODE($2)->nd_cval;
4539  RNODE($1)->nd_rval = add_mark_object(p, $$);
4540  $$ = $1;
4541  }
4542 
4543  }
4544  ;
4545 
4546 xstring_contents: /* none */
4547  {
4548 #if 0
4549  $$ = 0;
4550 #endif
4551  {VALUE v1;v1=dispatch0(xstring_new);$$=v1;}
4552  }
4553  | xstring_contents string_content
4554  {
4555 #if 0
4556  $$ = literal_concat(p, $1, $2, &@$);
4557 #endif
4558  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(xstring_add,v1,v2);$$=v3;}
4559  }
4560  ;
4561 
4562 regexp_contents: /* none */
4563  {
4564 #if 0
4565  $$ = 0;
4566 #endif
4567  {VALUE v1;v1=dispatch0(regexp_new);$$=v1;}
4568 #if 0
4569 #endif
4570  $$ = ripper_new_yylval(p, 0, $$, 0);
4571 
4572  }
4573  | regexp_contents string_content
4574  {
4575 #if 0
4576  NODE *head = $1, *tail = $2;
4577  if (!head) {
4578  $$ = tail;
4579  }
4580  else if (!tail) {
4581  $$ = head;
4582  }
4583  else {
4584  switch (nd_type(head)) {
4585  case NODE_STR:
4586  nd_set_type(head, NODE_DSTR);
4587  break;
4588  case NODE_DSTR:
4589  break;
4590  default:
4591  head = list_append(p, NEW_DSTR(Qnil, &@$), head);
4592  break;
4593  }
4594  $$ = list_append(p, head, tail);
4595  }
4596 #endif
4597  VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4598  if (ripper_is_node_yylval(n1)) {
4599  s1 = RNODE(n1)->nd_cval;
4600  n1 = RNODE(n1)->nd_rval;
4601  }
4602  if (ripper_is_node_yylval(n2)) {
4603  s2 = RNODE(n2)->nd_cval;
4604  n2 = RNODE(n2)->nd_rval;
4605  }
4606  $$ = dispatch2(regexp_add, n1, n2);
4607  if (!s1 && s2) {
4608  $$ = ripper_new_yylval(p, 0, $$, s2);
4609  }
4610 
4611  }
4612  ;
4613 
4614 string_content : tSTRING_CONTENT
4615  {$$=ripper_new_yylval(p, 0, get_value($1), $1);}
4616  | tSTRING_DVAR
4617  {
4618  /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
4619  $<strterm>$ = p->lex.strterm;
4620  p->lex.strterm = 0;
4621  SET_LEX_STATE(EXPR_BEG);
4622  }
4623  string_dvar
4624  {
4625  p->lex.strterm = $<strterm>2;
4626 #if 0
4627  $$ = NEW_EVSTR($3, &@$);
4628  nd_set_line($$, @3.end_pos.lineno);
4629 #endif
4630  {VALUE v1,v2;v1=$3;v2=dispatch1(string_dvar,v1);$$=v2;}
4631  }
4632  | tSTRING_DBEG
4633  {
4634  CMDARG_PUSH(0);
4635  COND_PUSH(0);
4636  }
4637  {
4638  /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
4639  $<strterm>$ = p->lex.strterm;
4640  p->lex.strterm = 0;
4641  }
4642  {
4643  $<num>$ = p->lex.state;
4644  SET_LEX_STATE(EXPR_BEG);
4645  }
4646  {
4647  $<num>$ = p->lex.brace_nest;
4648  p->lex.brace_nest = 0;
4649  }
4650  {
4651  $<num>$ = p->heredoc_indent;
4652  p->heredoc_indent = 0;
4653  }
4654  compstmt tSTRING_DEND
4655  {
4656  COND_POP();
4657  CMDARG_POP();
4658  p->lex.strterm = $<strterm>3;
4659  SET_LEX_STATE($<num>4);
4660  p->lex.brace_nest = $<num>5;
4661  p->heredoc_indent = $<num>6;
4662  p->heredoc_line_indent = -1;
4663 #if 0
4664  if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4665  $$ = new_evstr(p, $7, &@$);
4666 #endif
4667  {VALUE v1,v2;v1=$7;v2=dispatch1(string_embexpr,v1);$$=v2;}
4668  }
4669  ;
4670 
4671 string_dvar : tGVAR
4672  {
4673 #if 0
4674  $$ = NEW_GVAR($1, &@$);
4675 #endif
4676  {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4677  }
4678  | tIVAR
4679  {
4680 #if 0
4681  $$ = NEW_IVAR($1, &@$);
4682 #endif
4683  {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4684  }
4685  | tCVAR
4686  {
4687 #if 0
4688  $$ = NEW_CVAR($1, &@$);
4689 #endif
4690  {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4691  }
4692  | backref
4693  ;
4694 
4695 symbol : ssym
4696  | dsym
4697  ;
4698 
4699 ssym : tSYMBEG sym
4700  {
4701  SET_LEX_STATE(EXPR_END);
4702 #if 0
4703  $$ = NEW_LIT(ID2SYM($2), &@$);
4704 #endif
4705  {VALUE v1,v2,v3,v4;v1=$2;v2=dispatch1(symbol,v1);v3=v2;v4=dispatch1(symbol_literal,v3);$$=v4;}
4706  }
4707  ;
4708 
4709 sym : fname
4710  | tIVAR
4711  | tGVAR
4712  | tCVAR
4713  ;
4714 
4715 dsym : tSYMBEG string_contents tSTRING_END
4716  {
4717  SET_LEX_STATE(EXPR_END);
4718 #if 0
4719  $$ = dsym_node(p, $2, &@$);
4720 #endif
4721  {VALUE v1,v2;v1=$2;v2=dispatch1(dyna_symbol,v1);$$=v2;}
4722  }
4723  ;
4724 
4725 numeric : simple_numeric
4726  | tUMINUS_NUM simple_numeric %prec tLOWEST
4727  {
4728 #if 0
4729  $$ = $2;
4730  RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
4731 #endif
4732  {VALUE v1,v2,v3;v1=ID2VAL(idUMinus);v2=$2;v3=dispatch2(unary,v1,v2);$$=v3;}
4733  }
4734  ;
4735 
4736 simple_numeric : tINTEGER
4737  | tFLOAT
4738  | tRATIONAL
4739  | tIMAGINARY
4740  ;
4741 
4742 user_variable : tIDENTIFIER
4743  | tIVAR
4744  | tGVAR
4745  | tCONSTANT
4746  | tCVAR
4747  ;
4748 
4749 keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
4750  | keyword_self {$$ = KWD2EID(self, $1);}
4751  | keyword_true {$$ = KWD2EID(true, $1);}
4752  | keyword_false {$$ = KWD2EID(false, $1);}
4753  | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
4754  | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
4755  | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
4756  ;
4757 
4758 var_ref : user_variable
4759  {
4760 #if 0
4761  if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4762 #endif
4763  if (id_is_var(p, get_id($1))) {
4764  $$ = dispatch1(var_ref, $1);
4765  }
4766  else {
4767  $$ = dispatch1(vcall, $1);
4768  }
4769 
4770  }
4771  | keyword_variable
4772  {
4773 #if 0
4774  if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4775 #endif
4776  {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4777  }
4778  ;
4779 
4780 var_lhs : user_variable
4781  {
4782 #if 0
4783  $$ = assignable(p, $1, 0, &@$);
4784 #endif
4785  $$=assignable(p, var_field(p, $1));
4786  }
4787  | keyword_variable
4788  {
4789 #if 0
4790  $$ = assignable(p, $1, 0, &@$);
4791 #endif
4792  $$=assignable(p, var_field(p, $1));
4793  }
4794  ;
4795 
4796 backref : tNTH_REF
4797  | tBACK_REF
4798  ;
4799 
4800 superclass : '<'
4801  {
4802  SET_LEX_STATE(EXPR_BEG);
4803  p->command_start = TRUE;
4804  }
4805  expr_value term
4806  {
4807  $$ = $3;
4808  }
4809  | /* none */
4810  {
4811 #if 0
4812  $$ = 0;
4813 #endif
4814  $$=Qnil;
4815  }
4816  ;
4817 
4818 f_arglist : '(' f_args rparen
4819  {
4820 #if 0
4821  $$ = $2;
4822 #endif
4823  {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
4824  SET_LEX_STATE(EXPR_BEG);
4825  p->command_start = TRUE;
4826  }
4827  | '(' args_forward rparen
4828  {
4829  arg_var(p, idFWD_REST);
4830 #if idFWD_KWREST
4831  arg_var(p, idFWD_KWREST);
4832 #endif
4833  arg_var(p, idFWD_BLOCK);
4834 #if 0
4835  $$ = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, &@2);
4836  $$ = new_args(p, Qnone, Qnone, idFWD_REST, Qnone, $$, &@2);
4837 #endif
4838  {VALUE v1,v2;v1=params_new(Qnone, Qnone, $2, Qnone, Qnone, Qnone, Qnone);v2=dispatch1(paren,v1);$$=v2;}
4839  SET_LEX_STATE(EXPR_BEG);
4840  p->command_start = TRUE;
4841  }
4842  | {
4843  $<num>$ = p->in_kwarg;
4844  p->in_kwarg = 1;
4845  SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
4846  }
4847  f_args term
4848  {
4849  p->in_kwarg = !!$<num>1;
4850  $$ = $2;
4851  SET_LEX_STATE(EXPR_BEG);
4852  p->command_start = TRUE;
4853  }
4854  ;
4855 
4856 args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
4857  {
4858  $$ = new_args_tail(p, $1, $3, $4, &@3);
4859  }
4860  | f_kwarg opt_f_block_arg
4861  {
4862  $$ = new_args_tail(p, $1, Qnone, $2, &@1);
4863  }
4864  | f_kwrest opt_f_block_arg
4865  {
4866  $$ = new_args_tail(p, Qnone, $1, $2, &@1);
4867  }
4868  | f_no_kwarg opt_f_block_arg
4869  {
4870  $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
4871  }
4872  | f_block_arg
4873  {
4874  $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
4875  }
4876  ;
4877 
4878 opt_args_tail : ',' args_tail
4879  {
4880  $$ = $2;
4881  }
4882  | /* none */
4883  {
4884  $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4885  }
4886  ;
4887 
4888 f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
4889  {
4890  $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
4891  }
4892  | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4893  {
4894  $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4895  }
4896  | f_arg ',' f_optarg opt_args_tail
4897  {
4898  $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
4899  }
4900  | f_arg ',' f_optarg ',' f_arg opt_args_tail
4901  {
4902  $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4903  }
4904  | f_arg ',' f_rest_arg opt_args_tail
4905  {
4906  $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4907  }
4908  | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
4909  {
4910  $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4911  }
4912  | f_arg opt_args_tail
4913  {
4914  $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4915  }
4916  | f_optarg ',' f_rest_arg opt_args_tail
4917  {
4918  $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4919  }
4920  | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4921  {
4922  $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4923  }
4924  | f_optarg opt_args_tail
4925  {
4926  $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4927  }
4928  | f_optarg ',' f_arg opt_args_tail
4929  {
4930  $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4931  }
4932  | f_rest_arg opt_args_tail
4933  {
4934  $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4935  }
4936  | f_rest_arg ',' f_arg opt_args_tail
4937  {
4938  $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4939  }
4940  | args_tail
4941  {
4942  $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4943  }
4944  | /* none */
4945  {
4946  $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4947  $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
4948  }
4949  ;
4950 
4951 args_forward : tBDOT3
4952  {
4953 #if 0
4954  $$ = idDot3;
4955 #endif
4956  {VALUE v1;v1=dispatch0(args_forward);$$=v1;}
4957  }
4958  ;
4959 
4960 f_bad_arg : tCONSTANT
4961  {
4962 #if 0
4963  yyerror1(&@1, "formal argument cannot be a constant");
4964  $$ = 0;
4965 #endif
4966  {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4967  }
4968  | tIVAR
4969  {
4970 #if 0
4971  yyerror1(&@1, "formal argument cannot be an instance variable");
4972  $$ = 0;
4973 #endif
4974  {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4975  }
4976  | tGVAR
4977  {
4978 #if 0
4979  yyerror1(&@1, "formal argument cannot be a global variable");
4980  $$ = 0;
4981 #endif
4982  {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4983  }
4984  | tCVAR
4985  {
4986 #if 0
4987  yyerror1(&@1, "formal argument cannot be a class variable");
4988  $$ = 0;
4989 #endif
4990  {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4991  }
4992  ;
4993 
4994 f_norm_arg : f_bad_arg
4995  | tIDENTIFIER
4996  {
4997  formal_argument(p, get_id($1));
4998  p->max_numparam = ORDINAL_PARAM;
4999  $$ = $1;
5000  }
5001  ;
5002 
5003 f_arg_asgn : f_norm_arg
5004  {
5005  ID id = get_id($1);
5006  arg_var(p, id);
5007  p->cur_arg = id;
5008  $$ = $1;
5009  }
5010  ;
5011 
5012 f_arg_item : f_arg_asgn
5013  {
5014  p->cur_arg = 0;
5015 #if 0
5016  $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5017 #endif
5018  $$=get_value($1);
5019  }
5020  | tLPAREN f_margs rparen
5021  {
5022 #if 0
5023  ID tid = internal_id(p);
5024  YYLTYPE loc;
5025  loc.beg_pos = @2.beg_pos;
5026  loc.end_pos = @2.beg_pos;
5027  arg_var(p, tid);
5028  if (dyna_in_block(p)) {
5029  $2->nd_value = NEW_DVAR(tid, &loc);
5030  }
5031  else {
5032  $2->nd_value = NEW_LVAR(tid, &loc);
5033  }
5034  $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5035  $$->nd_next = $2;
5036 #endif
5037  {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
5038  }
5039  ;
5040 
5041 f_arg : f_arg_item
5042  {$$=rb_ary_new3(1, get_value($1));}
5043  | f_arg ',' f_arg_item
5044  {
5045 #if 0
5046  $$ = $1;
5047  $$->nd_plen++;
5048  $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5049  rb_discard_node(p, $3);
5050 #endif
5051  $$=rb_ary_push($1, get_value($3));
5052  }
5053  ;
5054 
5055 
5056 f_label : tLABEL
5057  {
5058  ID id = get_id($1);
5059  arg_var(p, formal_argument(p, id));
5060  p->cur_arg = id;
5061  p->max_numparam = ORDINAL_PARAM;
5062  $$ = $1;
5063  }
5064  ;
5065 
5066 f_kw : f_label arg_value
5067  {
5068  p->cur_arg = 0;
5069 #if 0
5070  $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5071 #endif
5072  $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5073  }
5074  | f_label
5075  {
5076  p->cur_arg = 0;
5077 #if 0
5078  $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5079 #endif
5080  $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5081  }
5082  ;
5083 
5084 f_block_kw : f_label primary_value
5085  {
5086 #if 0
5087  $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5088 #endif
5089  $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5090  }
5091  | f_label
5092  {
5093 #if 0
5094  $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5095 #endif
5096  $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5097  }
5098  ;
5099 
5100 f_block_kwarg : f_block_kw
5101  {
5102 #if 0
5103  $$ = $1;
5104 #endif
5105  $$=rb_ary_new3(1, get_value($1));
5106  }
5107  | f_block_kwarg ',' f_block_kw
5108  {
5109 #if 0
5110  $$ = kwd_append($1, $3);
5111 #endif
5112  $$=rb_ary_push($1, get_value($3));
5113  }
5114  ;
5115 
5116 
5117 f_kwarg : f_kw
5118  {
5119 #if 0
5120  $$ = $1;
5121 #endif
5122  $$=rb_ary_new3(1, get_value($1));
5123  }
5124  | f_kwarg ',' f_kw
5125  {
5126 #if 0
5127  $$ = kwd_append($1, $3);
5128 #endif
5129  $$=rb_ary_push($1, get_value($3));
5130  }
5131  ;
5132 
5133 kwrest_mark : tPOW
5134  | tDSTAR
5135  ;
5136 
5137 f_no_kwarg : kwrest_mark keyword_nil
5138  {
5139 #if 0
5140 #endif
5141  {VALUE v1,v2;v1=Qnil;v2=dispatch1(nokw_param,v1);$$=v2;}
5142  }
5143  ;
5144 
5145 f_kwrest : kwrest_mark tIDENTIFIER
5146  {
5147  arg_var(p, shadowing_lvar(p, get_id($2)));
5148 #if 0
5149  $$ = $2;
5150 #endif
5151  {VALUE v1,v2;v1=$2;v2=dispatch1(kwrest_param,v1);$$=v2;}
5152  }
5153  | kwrest_mark
5154  {
5155 #if 0
5156  $$ = internal_id(p);
5157  arg_var(p, $$);
5158 #endif
5159  {VALUE v1,v2;v1=Qnil;v2=dispatch1(kwrest_param,v1);$$=v2;}
5160  }
5161  ;
5162 
5163 f_opt : f_arg_asgn '=' arg_value
5164  {
5165  p->cur_arg = 0;
5166 #if 0
5167  $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5168 #endif
5169  $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5170  }
5171  ;
5172 
5173 f_block_opt : f_arg_asgn '=' primary_value
5174  {
5175  p->cur_arg = 0;
5176 #if 0
5177  $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5178 #endif
5179  $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5180  }
5181  ;
5182 
5183 f_block_optarg : f_block_opt
5184  {
5185 #if 0
5186  $$ = $1;
5187 #endif
5188  $$=rb_ary_new3(1, get_value($1));
5189  }
5190  | f_block_optarg ',' f_block_opt
5191  {
5192 #if 0
5193  $$ = opt_arg_append($1, $3);
5194 #endif
5195  $$=rb_ary_push($1, get_value($3));
5196  }
5197  ;
5198 
5199 f_optarg : f_opt
5200  {
5201 #if 0
5202  $$ = $1;
5203 #endif
5204  $$=rb_ary_new3(1, get_value($1));
5205  }
5206  | f_optarg ',' f_opt
5207  {
5208 #if 0
5209  $$ = opt_arg_append($1, $3);
5210 #endif
5211  $$=rb_ary_push($1, get_value($3));
5212  }
5213  ;
5214 
5215 restarg_mark : '*'
5216  | tSTAR
5217  ;
5218 
5219 f_rest_arg : restarg_mark tIDENTIFIER
5220  {
5221  arg_var(p, shadowing_lvar(p, get_id($2)));
5222 #if 0
5223  $$ = $2;
5224 #endif
5225  {VALUE v1,v2;v1=$2;v2=dispatch1(rest_param,v1);$$=v2;}
5226  }
5227  | restarg_mark
5228  {
5229 #if 0
5230  $$ = internal_id(p);
5231  arg_var(p, $$);
5232 #endif
5233  {VALUE v1,v2;v1=Qnil;v2=dispatch1(rest_param,v1);$$=v2;}
5234  }
5235  ;
5236 
5237 blkarg_mark : '&'
5238  | tAMPER
5239  ;
5240 
5241 f_block_arg : blkarg_mark tIDENTIFIER
5242  {
5243  arg_var(p, shadowing_lvar(p, get_id($2)));
5244 #if 0
5245  $$ = $2;
5246 #endif
5247  {VALUE v1,v2;v1=$2;v2=dispatch1(blockarg,v1);$$=v2;}
5248  }
5249  ;
5250 
5251 opt_f_block_arg : ',' f_block_arg
5252  {
5253  $$ = $2;
5254  }
5255  | none
5256  {
5257  $$ = Qnull;
5258  }
5259  ;
5260 
5261 singleton : var_ref
5262  {
5263  value_expr($1);
5264  $$ = $1;
5265  }
5266  | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5267  {
5268 #if 0
5269  switch (nd_type($3)) {
5270  case NODE_STR:
5271  case NODE_DSTR:
5272  case NODE_XSTR:
5273  case NODE_DXSTR:
5274  case NODE_DREGX:
5275  case NODE_LIT:
5276  case NODE_LIST:
5277  case NODE_ZLIST:
5278  yyerror1(&@3, "can't define singleton method for literals");
5279  break;
5280  default:
5281  value_expr($3);
5282  break;
5283  }
5284  $$ = $3;
5285 #endif
5286  {VALUE v1,v2;v1=$3;v2=dispatch1(paren,v1);$$=v2;}
5287  }
5288  ;
5289 
5290 assoc_list : none
5291  | assocs trailer
5292  {
5293 #if 0
5294  $$ = $1;
5295 #endif
5296  {VALUE v1,v2;v1=$1;v2=dispatch1(assoclist_from_args,v1);$$=v2;}
5297  }
5298  ;
5299 
5300 assocs : assoc
5301  {$$=rb_ary_new3(1, get_value($1));}
5302  | assocs ',' assoc
5303  {
5304 #if 0
5305  NODE *assocs = $1;
5306  NODE *tail = $3;
5307  if (!assocs) {
5308  assocs = tail;
5309  }
5310  else if (tail) {
5311  if (assocs->nd_head &&
5312  !tail->nd_head && nd_type(tail->nd_next) == NODE_LIST &&
5313  nd_type(tail->nd_next->nd_head) == NODE_HASH) {
5314  /* DSTAR */
5315  tail = tail->nd_next->nd_head->nd_head;
5316  }
5317  assocs = list_concat(assocs, tail);
5318  }
5319  $$ = assocs;
5320 #endif
5321  $$=rb_ary_push($1, get_value($3));
5322  }
5323  ;
5324 
5325 assoc : arg_value tASSOC arg_value
5326  {
5327 #if 0
5328  if (nd_type($1) == NODE_STR) {
5329  nd_set_type($1, NODE_LIT);
5330  RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
5331  }
5332  $$ = list_append(p, NEW_LIST($1, &@$), $3);
5333 #endif
5334  {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5335  }
5336  | tLABEL arg_value
5337  {
5338 #if 0
5339  $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5340 #endif
5341  {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5342  }
5343  | tSTRING_BEG string_contents tLABEL_END arg_value
5344  {
5345 #if 0
5346  YYLTYPE loc = code_loc_gen(&@1, &@3);
5347  $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
5348 #endif
5349  {VALUE v1,v2,v3,v4,v5;v1=$2;v2=dispatch1(dyna_symbol,v1);v3=v2;v4=$4;v5=dispatch2(assoc_new,v3,v4);$$=v5;}
5350  }
5351  | tDSTAR arg_value
5352  {
5353 #if 0
5354  if (nd_type($2) == NODE_HASH &&
5355  !($2->nd_head && $2->nd_head->nd_alen)) {
5356  static VALUE empty_hash;
5357  if (!empty_hash) {
5358  empty_hash = rb_obj_freeze(rb_hash_new());
5359  rb_gc_register_mark_object(empty_hash);
5360  }
5361  $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
5362  }
5363  else
5364  $$ = list_append(p, NEW_LIST(0, &@$), $2);
5365 #endif
5366  {VALUE v1,v2;v1=$2;v2=dispatch1(assoc_splat,v1);$$=v2;}
5367  }
5368  ;
5369 
5370 operation : tIDENTIFIER
5371  | tCONSTANT
5372  | tFID
5373  ;
5374 
5375 operation2 : tIDENTIFIER
5376  | tCONSTANT
5377  | tFID
5378  | op
5379  ;
5380 
5381 operation3 : tIDENTIFIER
5382  | tFID
5383  | op
5384  ;
5385 
5386 dot_or_colon : '.'
5387  | tCOLON2
5388  ;
5389 
5390 call_op : '.'
5391  | tANDDOT
5392  ;
5393 
5394 call_op2 : call_op
5395  | tCOLON2
5396  ;
5397 
5398 opt_terms : /* none */
5399  | terms
5400  ;
5401 
5402 opt_nl : /* none */
5403  | '\n'
5404  ;
5405 
5406 rparen : opt_nl ')'
5407  ;
5408 
5409 rbracket : opt_nl ']'
5410  ;
5411 
5412 rbrace : opt_nl '}'
5413  ;
5414 
5415 trailer : /* none */
5416  | '\n'
5417  | ','
5418  ;
5419 
5420 term : ';' {yyerrok;token_flush(p);}
5421  | '\n' {token_flush(p);}
5422  ;
5423 
5424 terms : term
5425  | terms ';' {yyerrok;}
5426  ;
5427 
5428 none : /* none */
5429  {
5430  $$ = Qnull;
5431  }
5432  ;
5433 %%
5434 # undef p
5435 # undef yylex
5436 # undef yylval
5437 # define yylval (*p->lval)
5438 
5439 static int regx_options(struct parser_params*);
5440 static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5441 static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5442 static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5443 static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
5444 
5445 #ifndef RIPPER
5446 # define set_yylval_node(x) { \
5447  YYLTYPE _cur_loc; \
5448  rb_parser_set_location(p, &_cur_loc); \
5449  yylval.node = (x); \
5450 }
5451 # define set_yylval_str(x) \
5452 do { \
5453  set_yylval_node(NEW_STR(x, &_cur_loc)); \
5454  RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5455 } while(0)
5456 # define set_yylval_literal(x) \
5457 do { \
5458  set_yylval_node(NEW_LIT(x, &_cur_loc)); \
5459  RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5460 } while(0)
5461 # define set_yylval_num(x) (yylval.num = (x))
5462 # define set_yylval_id(x) (yylval.id = (x))
5463 # define set_yylval_name(x) (yylval.id = (x))
5464 # define yylval_id() (yylval.id)
5465 #else
5466 static inline VALUE
5467 ripper_yylval_id(struct parser_params *p, ID x)
5468 {
5469  return ripper_new_yylval(p, x, ID2SYM(x), 0);
5470 }
5471 # define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
5472 # define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
5473 # define set_yylval_id(x) (void)(x)
5474 # define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
5475 # define set_yylval_literal(x) add_mark_object(p, (x))
5476 # define set_yylval_node(x) (void)(x)
5477 # define yylval_id() yylval.id
5478 # define _cur_loc NULL_LOC /* dummy */
5479 #endif
5480 
5481 #define set_yylval_noname() set_yylval_id(keyword_nil)
5482 
5483 #ifndef RIPPER
5484 #define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
5485 #define dispatch_scan_event(p, t) ((void)0)
5486 #define dispatch_delayed_token(p, t) ((void)0)
5487 #define has_delayed_token(p) (0)
5488 #else
5489 #define literal_flush(p, ptr) ((void)(ptr))
5490 
5491 #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5492 
5493 static inline VALUE
5494 intern_sym(const char *name)
5495 {
5496  ID id = rb_intern_const(name);
5497  return ID2SYM(id);
5498 }
5499 
5500 static int
5501 ripper_has_scan_event(struct parser_params *p)
5502 {
5503  if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
5504  return p->lex.pcur > p->lex.ptok;
5505 }
5506 
5507 static VALUE
5508 ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
5509 {
5510  VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
5511  VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
5512  token_flush(p);
5513  return rval;
5514 }
5515 
5516 static void
5517 ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
5518 {
5519  if (!ripper_has_scan_event(p)) return;
5520  add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
5521 }
5522 #define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
5523 
5524 static void
5525 ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
5526 {
5527  int saved_line = p->ruby_sourceline;
5528  const char *saved_tokp = p->lex.ptok;
5529 
5530  if (NIL_P(p->delayed.token)) return;
5531  p->ruby_sourceline = p->delayed.line;
5532  p->lex.ptok = p->lex.pbeg + p->delayed.col;
5533  add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
5534  p->delayed.token = Qnil;
5535  p->ruby_sourceline = saved_line;
5536  p->lex.ptok = saved_tokp;
5537 }
5538 #define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
5539 #define has_delayed_token(p) (!NIL_P(p->delayed.token))
5540 #endif /* RIPPER */
5541 
5542 #include "ruby/regex.h"
5543 #include "ruby/util.h"
5544 
5545 static inline int
5546 is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
5547 {
5548  return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
5549 }
5550 
5551 static inline int
5552 parser_is_identchar(struct parser_params *p)
5553 {
5554  return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5555 }
5556 
5557 static inline int
5558 parser_isascii(struct parser_params *p)
5559 {
5560  return ISASCII(*(p->lex.pcur-1));
5561 }
5562 
5563 static void
5564 token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
5565 {
5566  int column = 1, nonspc = 0, i;
5567  for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
5568  if (*ptr == '\t') {
5569  column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5570  }
5571  column++;
5572  if (*ptr != ' ' && *ptr != '\t') {
5573  nonspc = 1;
5574  }
5575  }
5576 
5577  ptinfo->beg = loc->beg_pos;
5578  ptinfo->indent = column;
5579  ptinfo->nonspc = nonspc;
5580 }
5581 
5582 static void
5583 token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5584 {
5585  token_info *ptinfo;
5586 
5587  if (!p->token_info_enabled) return;
5588  ptinfo = ALLOC(token_info);
5589  ptinfo->token = token;
5590  ptinfo->next = p->token_info;
5591  token_info_setup(ptinfo, p->lex.pbeg, loc);
5592 
5593  p->token_info = ptinfo;
5594 }
5595 
5596 static void
5597 token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5598 {
5599  token_info *ptinfo_beg = p->token_info;
5600 
5601  if (!ptinfo_beg) return;
5602  p->token_info = ptinfo_beg->next;
5603 
5604  /* indentation check of matched keywords (begin..end, if..end, etc.) */
5605  token_info_warn(p, token, ptinfo_beg, 1, loc);
5606  ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5607 }
5608 
5609 static void
5610 token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
5611 {
5612  token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
5613  if (!p->token_info_enabled) return;
5614  if (!ptinfo_beg) return;
5615  token_info_setup(ptinfo_end, p->lex.pbeg, loc);
5616  if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
5617  if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
5618  if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
5619  if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
5620  rb_warn3L(ptinfo_end->beg.lineno,
5621  "mismatched indentations at '%s' with '%s' at %d",
5622  WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
5623 }
5624 
5625 static int
5626 parser_precise_mbclen(struct parser_params *p, const char *ptr)
5627 {
5628  int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
5629  if (!MBCLEN_CHARFOUND_P(len)) {
5630  compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
5631  return -1;
5632  }
5633  return len;
5634 }
5635 
5636 #ifndef RIPPER
5637 static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
5638 
5639 static inline void
5640 parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5641 {
5642  VALUE str;
5643  int lineno = p->ruby_sourceline;
5644  if (!yylloc) {
5645  return;
5646  }
5647  else if (yylloc->beg_pos.lineno == lineno) {
5648  str = p->lex.lastline;
5649  }
5650  else {
5651  return;
5652  }
5653  ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
5654 }
5655 
5656 static int
5657 parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5658 {
5659  YYLTYPE current;
5660 
5661  if (!yylloc) {
5662  yylloc = RUBY_SET_YYLLOC(current);
5663  }
5664  else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
5665  p->ruby_sourceline != yylloc->end_pos.lineno) ||
5666  (yylloc->beg_pos.lineno == yylloc->end_pos.lineno &&
5667  yylloc->beg_pos.column == yylloc->end_pos.column)) {
5668  yylloc = 0;
5669  }
5670  compile_error(p, "%s", msg);
5671  parser_show_error_line(p, yylloc);
5672  return 0;
5673 }
5674 
5675 static void
5676 ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
5677 {
5678  VALUE mesg;
5679  const int max_line_margin = 30;
5680  const char *ptr, *ptr_end, *pt, *pb;
5681  const char *pre = "", *post = "", *pend;
5682  const char *code = "", *caret = "";
5683  const char *lim;
5684  const char *const pbeg = RSTRING_PTR(str);
5685  char *buf;
5686  long len;
5687  int i;
5688 
5689  if (!yylloc) return;
5690  pend = RSTRING_END(str);
5691  if (pend > pbeg && pend[-1] == '\n') {
5692  if (--pend > pbeg && pend[-1] == '\r') --pend;
5693  }
5694 
5695  pt = pend;
5696  if (lineno == yylloc->end_pos.lineno &&
5697  (pend - pbeg) > yylloc->end_pos.column) {
5698  pt = pbeg + yylloc->end_pos.column;
5699  }
5700 
5701  ptr = ptr_end = pt;
5702  lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
5703  while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
5704 
5705  lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
5706  while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
5707 
5708  len = ptr_end - ptr;
5709  if (len > 4) {
5710  if (ptr > pbeg) {
5711  ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
5712  if (ptr > pbeg) pre = "...";
5713  }
5714  if (ptr_end < pend) {
5715  ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
5716  if (ptr_end < pend) post = "...";
5717  }
5718  }
5719  pb = pbeg;
5720  if (lineno == yylloc->beg_pos.lineno) {
5721  pb += yylloc->beg_pos.column;
5722  if (pb > pt) pb = pt;
5723  }
5724  if (pb < ptr) pb = ptr;
5725  if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
5726  return;
5727  }
5728  if (RTEST(errbuf)) {
5729  mesg = rb_attr_get(errbuf, idMesg);
5730  if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
5731  rb_str_cat_cstr(mesg, "\n");
5732  }
5733  else {
5734  mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
5735  }
5736  if (!errbuf && rb_stderr_tty_p()) {
5737 #define CSI_BEGIN "\033["
5738 #define CSI_SGR "m"
5739  rb_str_catf(mesg,
5740  CSI_BEGIN""CSI_SGR"%s" /* pre */
5741  CSI_BEGIN"1"CSI_SGR"%.*s"
5742  CSI_BEGIN"1;4"CSI_SGR"%.*s"
5743  CSI_BEGIN";1"CSI_SGR"%.*s"
5744  CSI_BEGIN""CSI_SGR"%s" /* post */
5745  "\n",
5746  pre,
5747  (int)(pb - ptr), ptr,
5748  (int)(pt - pb), pb,
5749  (int)(ptr_end - pt), pt,
5750  post);
5751  }
5752  else {
5753  char *p2;
5754 
5755  len = ptr_end - ptr;
5756  lim = pt < pend ? pt : pend;
5757  i = (int)(lim - ptr);
5758  buf = ALLOCA_N(char, i+2);
5759  code = ptr;
5760  caret = p2 = buf;
5761  if (ptr <= pb) {
5762  while (ptr < pb) {
5763  *p2++ = *ptr++ == '\t' ? '\t' : ' ';
5764  }
5765  *p2++ = '^';
5766  ptr++;
5767  }
5768  if (lim > ptr) {
5769  memset(p2, '~', (lim - ptr));
5770  p2 += (lim - ptr);
5771  }
5772  *p2 = '\0';
5773  rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
5774  pre, (int)len, code, post,
5775  pre, caret);
5776  }
5777  if (!errbuf) rb_write_error_str(mesg);
5778 }
5779 #else
5780 static int
5781 parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5782 {
5783  const char *pcur = 0, *ptok = 0;
5784  if (yylloc &&
5785  p->ruby_sourceline == yylloc->beg_pos.lineno &&
5786  p->ruby_sourceline == yylloc->end_pos.lineno) {
5787  pcur = p->lex.pcur;
5788  ptok = p->lex.ptok;
5789  p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
5790  p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
5791  }
5792  dispatch1(parse_error, STR_NEW2(msg));
5793  ripper_error(p);
5794  if (pcur) {
5795  p->lex.ptok = ptok;
5796  p->lex.pcur = pcur;
5797  }
5798  return 0;
5799 }
5800 
5801 static inline void
5802 parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5803 {
5804 }
5805 #endif /* !RIPPER */
5806 
5807 #ifndef RIPPER
5808 static int
5809 vtable_size(const struct vtable *tbl)
5810 {
5811  if (!DVARS_TERMINAL_P(tbl)) {
5812  return tbl->pos;
5813  }
5814  else {
5815  return 0;
5816  }
5817 }
5818 #endif
5819 
5820 static struct vtable *
5821 vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
5822 {
5823  struct vtable *tbl = ALLOC(struct vtable);
5824  tbl->pos = 0;
5825  tbl->capa = 8;
5826  tbl->tbl = ALLOC_N(ID, tbl->capa);
5827  tbl->prev = prev;
5828 #ifndef RIPPER
5829  if (p->debug) {
5830  rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
5831  }
5832 #endif
5833  return tbl;
5834 }
5835 #define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
5836 
5837 static void
5838 vtable_free_gen(struct parser_params *p, int line, const char *name,
5839  struct vtable *tbl)
5840 {
5841 #ifndef RIPPER
5842  if (p->debug) {
5843  rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
5844  }
5845 #endif
5846  if (!DVARS_TERMINAL_P(tbl)) {
5847  if (tbl->tbl) {
5848  ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
5849  }
5850  ruby_sized_xfree(tbl, sizeof(tbl));
5851  }
5852 }
5853 #define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
5854 
5855 static void
5856 vtable_add_gen(struct parser_params *p, int line, const char *name,
5857  struct vtable *tbl, ID id)
5858 {
5859 #ifndef RIPPER
5860  if (p->debug) {
5861  rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
5862  line, name, (void *)tbl, rb_id2name(id));
5863  }
5864 #endif
5865  if (DVARS_TERMINAL_P(tbl)) {
5866  rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
5867  return;
5868  }
5869  if (tbl->pos == tbl->capa) {
5870  tbl->capa = tbl->capa * 2;
5871  SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
5872  }
5873  tbl->tbl[tbl->pos++] = id;
5874 }
5875 #define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
5876 
5877 #ifndef RIPPER
5878 static void
5879 vtable_pop_gen(struct parser_params *p, int line, const char *name,
5880  struct vtable *tbl, int n)
5881 {
5882  if (p->debug) {
5883  rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
5884  line, name, (void *)tbl, n);
5885  }
5886  if (tbl->pos < n) {
5887  rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
5888  return;
5889  }
5890  tbl->pos -= n;
5891 }
5892 #define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
5893 #endif
5894 
5895 static int
5896 vtable_included(const struct vtable * tbl, ID id)
5897 {
5898  int i;
5899 
5900  if (!DVARS_TERMINAL_P(tbl)) {
5901  for (i = 0; i < tbl->pos; i++) {
5902  if (tbl->tbl[i] == id) {
5903  return i+1;
5904  }
5905  }
5906  }
5907  return 0;
5908 }
5909 
5910 static void parser_prepare(struct parser_params *p);
5911 
5912 #ifndef RIPPER
5913 static NODE *parser_append_options(struct parser_params *p, NODE *node);
5914 
5915 static VALUE
5916 debug_lines(VALUE fname)
5917 {
5918  ID script_lines;
5919  CONST_ID(script_lines, "SCRIPT_LINES__");
5920  if (rb_const_defined_at(rb_cObject, script_lines)) {
5921  VALUE hash = rb_const_get_at(rb_cObject, script_lines);
5922  if (RB_TYPE_P(hash, T_HASH)) {
5923  VALUE lines = rb_ary_new();
5924  rb_hash_aset(hash, fname, lines);
5925  return lines;
5926  }
5927  }
5928  return 0;
5929 }
5930 
5931 static int
5932 e_option_supplied(struct parser_params *p)
5933 {
5934  return strcmp(p->ruby_sourcefile, "-e") == 0;
5935 }
5936 
5937 static VALUE
5938 yycompile0(VALUE arg)
5939 {
5940  int n;
5941  NODE *tree;
5942  struct parser_params *p = (struct parser_params *)arg;
5943  VALUE cov = Qfalse;
5944 
5945  if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
5946  p->debug_lines = debug_lines(p->ruby_sourcefile_string);
5947  if (p->debug_lines && p->ruby_sourceline > 0) {
5948  VALUE str = STR_NEW0();
5949  n = p->ruby_sourceline;
5950  do {
5951  rb_ary_push(p->debug_lines, str);
5952  } while (--n);
5953  }
5954 
5955  if (!e_option_supplied(p)) {
5956  cov = Qtrue;
5957  }
5958  }
5959 
5960  parser_prepare(p);
5961 #define RUBY_DTRACE_PARSE_HOOK(name) \
5962  if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
5963  RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
5964  }
5965  RUBY_DTRACE_PARSE_HOOK(BEGIN);
5966  n = yyparse(p);
5967  RUBY_DTRACE_PARSE_HOOK(END);
5968  p->debug_lines = 0;
5969 
5970  p->lex.strterm = 0;
5971  p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
5972  p->lex.prevline = p->lex.lastline = p->lex.nextline = 0;
5973  if (n || p->error_p) {
5974  VALUE mesg = p->error_buffer;
5975  if (!mesg) {
5976  mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
5977  }
5978  rb_set_errinfo(mesg);
5979  return FALSE;
5980  }
5981  tree = p->eval_tree;
5982  if (!tree) {
5983  tree = NEW_NIL(&NULL_LOC);
5984  }
5985  else {
5986  VALUE opt = p->compile_option;
5987  NODE *prelude;
5988  NODE *body = parser_append_options(p, tree->nd_body);
5989  if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
5990  rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
5991  prelude = block_append(p, p->eval_tree_begin, body);
5992  tree->nd_body = prelude;
5993  RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
5994  }
5995  p->ast->body.root = tree;
5996  p->ast->body.line_count = p->line_count;
5997  return TRUE;
5998 }
5999 
6000 static rb_ast_t *
6001 yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
6002 {
6003  rb_ast_t *ast;
6004  if (NIL_P(fname)) {
6005  p->ruby_sourcefile_string = Qnil;
6006  p->ruby_sourcefile = "(none)";
6007  }
6008  else {
6009  p->ruby_sourcefile_string = rb_fstring(fname);
6010  p->ruby_sourcefile = StringValueCStr(fname);
6011  }
6012  p->ruby_sourceline = line - 1;
6013 
6014  p->ast = ast = rb_ast_new();
6015  rb_suppress_tracing(yycompile0, (VALUE)p);
6016  p->ast = 0;
6017  RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6018 
6019  return ast;
6020 }
6021 #endif /* !RIPPER */
6022 
6023 static rb_encoding *
6024 must_be_ascii_compatible(VALUE s)
6025 {
6026  rb_encoding *enc = rb_enc_get(s);
6027  if (!rb_enc_asciicompat(enc)) {
6028  rb_raise(rb_eArgError, "invalid source encoding");
6029  }
6030  return enc;
6031 }
6032 
6033 static VALUE
6034 lex_get_str(struct parser_params *p, VALUE s)
6035 {
6036  char *beg, *end, *start;
6037  long len;
6038 
6039  beg = RSTRING_PTR(s);
6040  len = RSTRING_LEN(s);
6041  start = beg;
6042  if (p->lex.gets_.ptr) {
6043  if (len == p->lex.gets_.ptr) return Qnil;
6044  beg += p->lex.gets_.ptr;
6045  len -= p->lex.gets_.ptr;
6046  }
6047  end = memchr(beg, '\n', len);
6048  if (end) len = ++end - beg;
6049  p->lex.gets_.ptr += len;
6050  return rb_str_subseq(s, beg - start, len);
6051 }
6052 
6053 static VALUE
6054 lex_getline(struct parser_params *p)
6055 {
6056  VALUE line = (*p->lex.gets)(p, p->lex.input);
6057  if (NIL_P(line)) return line;
6058  must_be_ascii_compatible(line);
6059 #ifndef RIPPER
6060  if (p->debug_lines) {
6061  rb_enc_associate(line, p->enc);
6062  rb_ary_push(p->debug_lines, line);
6063  }
6064 #endif
6065  p->line_count++;
6066  return line;
6067 }
6068 
6069 static const rb_data_type_t parser_data_type;
6070 
6071 #ifndef RIPPER
6072 static rb_ast_t*
6073 parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6074 {
6075  struct parser_params *p;
6076 
6077  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6078 
6079  p->lex.gets = lex_get_str;
6080  p->lex.gets_.ptr = 0;
6081  p->lex.input = rb_str_new_frozen(s);
6082  p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6083 
6084  return yycompile(vparser, p, fname, line);
6085 }
6086 
6087 rb_ast_t*
6088 rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6089 {
6090  return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6091 }
6092 
6093 rb_ast_t*
6094 rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6095 {
6096  must_be_ascii_compatible(s);
6097  return parser_compile_string(vparser, f, s, line);
6098 }
6099 
6100 VALUE rb_io_gets_internal(VALUE io);
6101 
6102 static VALUE
6103 lex_io_gets(struct parser_params *p, VALUE io)
6104 {
6105  return rb_io_gets_internal(io);
6106 }
6107 
6108 rb_ast_t*
6109 rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6110 {
6111  struct parser_params *p;
6112 
6113  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6114 
6115  p->lex.gets = lex_io_gets;
6116  p->lex.input = file;
6117  p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6118 
6119  return yycompile(vparser, p, fname, start);
6120 }
6121 
6122 static VALUE
6123 lex_generic_gets(struct parser_params *p, VALUE input)
6124 {
6125  return (*p->lex.gets_.call)(input, p->line_count);
6126 }
6127 
6128 rb_ast_t*
6129 rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6130 {
6131  struct parser_params *p;
6132 
6133  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6134 
6135  p->lex.gets = lex_generic_gets;
6136  p->lex.gets_.call = lex_gets;
6137  p->lex.input = input;
6138  p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6139 
6140  return yycompile(vparser, p, fname, start);
6141 }
6142 #endif /* !RIPPER */
6143 
6144 #define STR_FUNC_ESCAPE 0x01
6145 #define STR_FUNC_EXPAND 0x02
6146 #define STR_FUNC_REGEXP 0x04
6147 #define STR_FUNC_QWORDS 0x08
6148 #define STR_FUNC_SYMBOL 0x10
6149 #define STR_FUNC_INDENT 0x20
6150 #define STR_FUNC_LABEL 0x40
6151 #define STR_FUNC_LIST 0x4000
6152 #define STR_FUNC_TERM 0x8000
6153 
6154 enum string_type {
6155  str_label = STR_FUNC_LABEL,
6156  str_squote = (0),
6157  str_dquote = (STR_FUNC_EXPAND),
6158  str_xquote = (STR_FUNC_EXPAND),
6159  str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6160  str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6161  str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6162  str_ssym = (STR_FUNC_SYMBOL),
6163  str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6164 };
6165 
6166 static VALUE
6167 parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6168 {
6169  VALUE str;
6170 
6171  str = rb_enc_str_new(ptr, len, enc);
6172  if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6173  if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6174  }
6175  else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6176  rb_enc_associate(str, rb_ascii8bit_encoding());
6177  }
6178  }
6179 
6180  return str;
6181 }
6182 
6183 #define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6184 #define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6185 #define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6186 #define peek(p,c) peek_n(p, (c), 0)
6187 #define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6188 #define peekc(p) peekc_n(p, 0)
6189 #define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6190 
6191 #ifdef RIPPER
6192 static void
6193 add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6194 {
6195  if (tok < end) {
6196  if (!has_delayed_token(p)) {
6197  p->delayed.token = rb_str_buf_new(end - tok);
6198  rb_enc_associate(p->delayed.token, p->enc);
6199  p->delayed.line = p->ruby_sourceline;
6200  p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6201  }
6202  rb_str_buf_cat(p->delayed.token, tok, end - tok);
6203  p->lex.ptok = end;
6204  }
6205 }
6206 #else
6207 #define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6208 #endif
6209 
6210 static int
6211 nextline(struct parser_params *p)
6212 {
6213  VALUE v = p->lex.nextline;
6214  p->lex.nextline = 0;
6215  if (!v) {
6216  if (p->eofp)
6217  return -1;
6218 
6219  if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6220  goto end_of_input;
6221  }
6222 
6223  if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6224  end_of_input:
6225  p->eofp = 1;
6226  lex_goto_eol(p);
6227  return -1;
6228  }
6229  p->cr_seen = FALSE;
6230  }
6231  else if (NIL_P(v)) {
6232  /* after here-document without terminator */
6233  goto end_of_input;
6234  }
6235  add_delayed_token(p, p->lex.ptok, p->lex.pend);
6236  if (p->heredoc_end > 0) {
6237  p->ruby_sourceline = p->heredoc_end;
6238  p->heredoc_end = 0;
6239  }
6240  p->ruby_sourceline++;
6241  p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
6242  p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
6243  token_flush(p);
6244  p->lex.prevline = p->lex.lastline;
6245  p->lex.lastline = v;
6246  return 0;
6247 }
6248 
6249 static int
6250 parser_cr(struct parser_params *p, int c)
6251 {
6252  if (peek(p, '\n')) {
6253  p->lex.pcur++;
6254  c = '\n';
6255  }
6256  else if (!p->cr_seen) {
6257  p->cr_seen = TRUE;
6258  /* carried over with p->lex.nextline for nextc() */
6259  rb_warn0("encountered \\r in middle of line, treated as a mere space");
6260  }
6261  return c;
6262 }
6263 
6264 static inline int
6265 nextc(struct parser_params *p)
6266 {
6267  int c;
6268 
6269  if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6270  if (nextline(p)) return -1;
6271  }
6272  c = (unsigned char)*p->lex.pcur++;
6273  if (UNLIKELY(c == '\r')) {
6274  c = parser_cr(p, c);
6275  }
6276 
6277  return c;
6278 }
6279 
6280 static void
6281 pushback(struct parser_params *p, int c)
6282 {
6283  if (c == -1) return;
6284  p->lex.pcur--;
6285  if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6286  p->lex.pcur--;
6287  }
6288 }
6289 
6290 #define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6291 
6292 #define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6293 #define tok(p) (p)->tokenbuf
6294 #define toklen(p) (p)->tokidx
6295 
6296 static int
6297 looking_at_eol_p(struct parser_params *p)
6298 {
6299  const char *ptr = p->lex.pcur;
6300  while (ptr < p->lex.pend) {
6301  int c = (unsigned char)*ptr++;
6302  int eol = (c == '\n' || c == '#');
6303  if (eol || !ISSPACE(c)) {
6304  return eol;
6305  }
6306  }
6307  return TRUE;
6308 }
6309 
6310 static char*
6311 newtok(struct parser_params *p)
6312 {
6313  p->tokidx = 0;
6314  p->tokline = p->ruby_sourceline;
6315  if (!p->tokenbuf) {
6316  p->toksiz = 60;
6317  p->tokenbuf = ALLOC_N(char, 60);
6318  }
6319  if (p->toksiz > 4096) {
6320  p->toksiz = 60;
6321  REALLOC_N(p->tokenbuf, char, 60);
6322  }
6323  return p->tokenbuf;
6324 }
6325 
6326 static char *
6327 tokspace(struct parser_params *p, int n)
6328 {
6329  p->tokidx += n;
6330 
6331  if (p->tokidx >= p->toksiz) {
6332  do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6333  REALLOC_N(p->tokenbuf, char, p->toksiz);
6334  }
6335  return &p->tokenbuf[p->tokidx-n];
6336 }
6337 
6338 static void
6339 tokadd(struct parser_params *p, int c)
6340 {
6341  p->tokenbuf[p->tokidx++] = (char)c;
6342  if (p->tokidx >= p->toksiz) {
6343  p->toksiz *= 2;
6344  REALLOC_N(p->tokenbuf, char, p->toksiz);
6345  }
6346 }
6347 
6348 static int
6349 tok_hex(struct parser_params *p, size_t *numlen)
6350 {
6351  int c;
6352 
6353  c = scan_hex(p->lex.pcur, 2, numlen);
6354  if (!*numlen) {
6355  yyerror0("invalid hex escape");
6356  token_flush(p);
6357  return 0;
6358  }
6359  p->lex.pcur += *numlen;
6360  return c;
6361 }
6362 
6363 #define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6364 
6365 static int
6366 escaped_control_code(int c)
6367 {
6368  int c2 = 0;
6369  switch (c) {
6370  case ' ':
6371  c2 = 's';
6372  break;
6373  case '\n':
6374  c2 = 'n';
6375  break;
6376  case '\t':
6377  c2 = 't';
6378  break;
6379  case '\v':
6380  c2 = 'v';
6381  break;
6382  case '\r':
6383  c2 = 'r';
6384  break;
6385  case '\f':
6386  c2 = 'f';
6387  break;
6388  }
6389  return c2;
6390 }
6391 
6392 #define WARN_SPACE_CHAR(c, prefix) \
6393  rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6394 
6395 static int
6396 tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6397  int regexp_literal, int wide)
6398 {
6399  size_t numlen;
6400  int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6401  literal_flush(p, p->lex.pcur);
6402  p->lex.pcur += numlen;
6403  if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6404  yyerror0("invalid Unicode escape");
6405  return wide && numlen > 0;
6406  }
6407  if (codepoint > 0x10ffff) {
6408  yyerror0("invalid Unicode codepoint (too large)");
6409  return wide;
6410  }
6411  if ((codepoint & 0xfffff800) == 0xd800) {
6412  yyerror0("invalid Unicode codepoint");
6413  return wide;
6414  }
6415  if (regexp_literal) {
6416  tokcopy(p, (int)numlen);
6417  }
6418  else if (codepoint >= 0x80) {
6419  rb_encoding *utf8 = rb_utf8_encoding();
6420  if (*encp && utf8 != *encp) {
6421  YYLTYPE loc = RUBY_INIT_YYLLOC();
6422  compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6423  parser_show_error_line(p, &loc);
6424  return wide;
6425  }
6426  *encp = utf8;
6427  tokaddmbc(p, codepoint, *encp);
6428  }
6429  else {
6430  tokadd(p, codepoint);
6431  }
6432  return TRUE;
6433 }
6434 
6435 /* return value is for ?\u3042 */
6436 static void
6437 tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6438  int term, int symbol_literal, int regexp_literal)
6439 {
6440  /*
6441  * If `term` is not -1, then we allow multiple codepoints in \u{}
6442  * upto `term` byte, otherwise we're parsing a character literal.
6443  * And then add the codepoints to the current token.
6444  */
6445  static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6446 
6447  const int open_brace = '{', close_brace = '}';
6448 
6449  if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6450 
6451  if (peek(p, open_brace)) { /* handle \u{...} form */
6452  const char *second = NULL;
6453  int c, last = nextc(p);
6454  if (p->lex.pcur >= p->lex.pend) goto unterminated;
6455  while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6456  while (c != close_brace) {
6457  if (c == term) goto unterminated;
6458  if (second == multiple_codepoints)
6459  second = p->lex.pcur;
6460  if (regexp_literal) tokadd(p, last);
6461  if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6462  break;
6463  }
6464  while (ISSPACE(c = *p->lex.pcur)) {
6465  if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6466  last = c;
6467  }
6468  if (term == -1 && !second)
6469  second = multiple_codepoints;
6470  }
6471 
6472  if (c != close_brace) {
6473  unterminated:
6474  token_flush(p);
6475  yyerror0("unterminated Unicode escape");
6476  return;
6477  }
6478  if (second && second != multiple_codepoints) {
6479  const char *pcur = p->lex.pcur;
6480  p->lex.pcur = second;
6481  dispatch_scan_event(p, tSTRING_CONTENT);
6482  token_flush(p);
6483  p->lex.pcur = pcur;
6484  yyerror0(multiple_codepoints);
6485  token_flush(p);
6486  }
6487 
6488  if (regexp_literal) tokadd(p, close_brace);
6489  nextc(p);
6490  }
6491  else { /* handle \uxxxx form */
6492  if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6493  token_flush(p);
6494  return;
6495  }
6496  }
6497 }
6498 
6499 #define ESCAPE_CONTROL 1
6500 #define ESCAPE_META 2
6501 
6502 static int
6503 read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6504 {
6505  int c;
6506  size_t numlen;
6507 
6508  switch (c = nextc(p)) {
6509  case '\\': /* Backslash */
6510  return c;
6511 
6512  case 'n': /* newline */
6513  return '\n';
6514 
6515  case 't': /* horizontal tab */
6516  return '\t';
6517 
6518  case 'r': /* carriage-return */
6519  return '\r';
6520 
6521  case 'f': /* form-feed */
6522  return '\f';
6523 
6524  case 'v': /* vertical tab */
6525  return '\13';
6526 
6527  case 'a': /* alarm(bell) */
6528  return '\007';
6529 
6530  case 'e': /* escape */
6531  return 033;
6532 
6533  case '0': case '1': case '2': case '3': /* octal constant */
6534  case '4': case '5': case '6': case '7':
6535  pushback(p, c);
6536  c = scan_oct(p->lex.pcur, 3, &numlen);
6537  p->lex.pcur += numlen;
6538  return c;
6539 
6540  case 'x': /* hex constant */
6541  c = tok_hex(p, &numlen);
6542  if (numlen == 0) return 0;
6543  return c;
6544 
6545  case 'b': /* backspace */
6546  return '\010';
6547 
6548  case 's': /* space */
6549  return ' ';
6550 
6551  case 'M':
6552  if (flags & ESCAPE_META) goto eof;
6553  if ((c = nextc(p)) != '-') {
6554  goto eof;
6555  }
6556  if ((c = nextc(p)) == '\\') {
6557  if (peek(p, 'u')) goto eof;
6558  return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6559  }
6560  else if (c == -1 || !ISASCII(c)) goto eof;
6561  else {
6562  int c2 = escaped_control_code(c);
6563  if (c2) {
6564  if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6565  WARN_SPACE_CHAR(c2, "\\M-");
6566  }
6567  else {
6568  WARN_SPACE_CHAR(c2, "\\C-\\M-");
6569  }
6570  }
6571  else if (ISCNTRL(c)) goto eof;
6572  return ((c & 0xff) | 0x80);
6573  }
6574 
6575  case 'C':
6576  if ((c = nextc(p)) != '-') {
6577  goto eof;
6578  }
6579  case 'c':
6580  if (flags & ESCAPE_CONTROL) goto eof;
6581  if ((c = nextc(p))== '\\') {
6582  if (peek(p, 'u')) goto eof;
6583  c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6584  }
6585  else if (c == '?')
6586  return 0177;
6587  else if (c == -1 || !ISASCII(c)) goto eof;
6588  else {
6589  int c2 = escaped_control_code(c);
6590  if (c2) {
6591  if (ISCNTRL(c)) {
6592  if (flags & ESCAPE_META) {
6593  WARN_SPACE_CHAR(c2, "\\M-");
6594  }
6595  else {
6596  WARN_SPACE_CHAR(c2, "");
6597  }
6598  }
6599  else {
6600  if (flags & ESCAPE_META) {
6601  WARN_SPACE_CHAR(c2, "\\M-\\C-");
6602  }
6603  else {
6604  WARN_SPACE_CHAR(c2, "\\C-");
6605  }
6606  }
6607  }
6608  else if (ISCNTRL(c)) goto eof;
6609  }
6610  return c & 0x9f;
6611 
6612  eof:
6613  case -1:
6614  yyerror0("Invalid escape character syntax");
6615  token_flush(p);
6616  return '\0';
6617 
6618  default:
6619  return c;
6620  }
6621 }
6622 
6623 static void
6624 tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
6625 {
6626  int len = rb_enc_codelen(c, enc);
6627  rb_enc_mbcput(c, tokspace(p, len), enc);
6628 }
6629 
6630 static int
6631 tokadd_escape(struct parser_params *p, rb_encoding **encp)
6632 {
6633  int c;
6634  int flags = 0;
6635  size_t numlen;
6636 
6637  first:
6638  switch (c = nextc(p)) {
6639  case '\n':
6640  return 0; /* just ignore */
6641 
6642  case '0': case '1': case '2': case '3': /* octal constant */
6643  case '4': case '5': case '6': case '7':
6644  {
6645  ruby_scan_oct(--p->lex.pcur, 3, &numlen);
6646  if (numlen == 0) goto eof;
6647  p->lex.pcur += numlen;
6648  tokcopy(p, (int)numlen + 1);
6649  }
6650  return 0;
6651 
6652  case 'x': /* hex constant */
6653  {
6654  tok_hex(p, &numlen);
6655  if (numlen == 0) return -1;
6656  tokcopy(p, (int)numlen + 2);
6657  }
6658  return 0;
6659 
6660  case 'M':
6661  if (flags & ESCAPE_META) goto eof;
6662  if ((c = nextc(p)) != '-') {
6663  pushback(p, c);
6664  goto eof;
6665  }
6666  tokcopy(p, 3);
6667  flags |= ESCAPE_META;
6668  goto escaped;
6669 
6670  case 'C':
6671  if (flags & ESCAPE_CONTROL) goto eof;
6672  if ((c = nextc(p)) != '-') {
6673  pushback(p, c);
6674  goto eof;
6675  }
6676  tokcopy(p, 3);
6677  goto escaped;
6678 
6679  case 'c':
6680  if (flags & ESCAPE_CONTROL) goto eof;
6681  tokcopy(p, 2);
6682  flags |= ESCAPE_CONTROL;
6683  escaped:
6684  if ((c = nextc(p)) == '\\') {
6685  goto first;
6686  }
6687  else if (c == -1) goto eof;
6688  tokadd(p, c);
6689  return 0;
6690 
6691  eof:
6692  case -1:
6693  yyerror0("Invalid escape character syntax");
6694  token_flush(p);
6695  return -1;
6696 
6697  default:
6698  tokadd(p, '\\');
6699  tokadd(p, c);
6700  }
6701  return 0;
6702 }
6703 
6704 static int
6705 regx_options(struct parser_params *p)
6706 {
6707  int kcode = 0;
6708  int kopt = 0;
6709  int options = 0;
6710  int c, opt, kc;
6711 
6712  newtok(p);
6713  while (c = nextc(p), ISALPHA(c)) {
6714  if (c == 'o') {
6715  options |= RE_OPTION_ONCE;
6716  }
6717  else if (rb_char_to_option_kcode(c, &opt, &kc)) {
6718  if (kc >= 0) {
6719  if (kc != rb_ascii8bit_encindex()) kcode = c;
6720  kopt = opt;
6721  }
6722  else {
6723  options |= opt;
6724  }
6725  }
6726  else {
6727  tokadd(p, c);
6728  }
6729  }
6730  options |= kopt;
6731  pushback(p, c);
6732  if (toklen(p)) {
6733  YYLTYPE loc = RUBY_INIT_YYLLOC();
6734  tokfix(p);
6735  compile_error(p, "unknown regexp option%s - %*s",
6736  toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
6737  parser_show_error_line(p, &loc);
6738  }
6739  return options | RE_OPTION_ENCODING(kcode);
6740 }
6741 
6742 static int
6743 tokadd_mbchar(struct parser_params *p, int c)
6744 {
6745  int len = parser_precise_mbclen(p, p->lex.pcur-1);
6746  if (len < 0) return -1;
6747  tokadd(p, c);
6748  p->lex.pcur += --len;
6749  if (len > 0) tokcopy(p, len);
6750  return c;
6751 }
6752 
6753 static inline int
6754 simple_re_meta(int c)
6755 {
6756  switch (c) {
6757  case '$': case '*': case '+': case '.':
6758  case '?': case '^': case '|':
6759  case ')': case ']': case '}': case '>':
6760  return TRUE;
6761  default:
6762  return FALSE;
6763  }
6764 }
6765 
6766 static int
6767 parser_update_heredoc_indent(struct parser_params *p, int c)
6768 {
6769  if (p->heredoc_line_indent == -1) {
6770  if (c == '\n') p->heredoc_line_indent = 0;
6771  }
6772  else {
6773  if (c == ' ') {
6774  p->heredoc_line_indent++;
6775  return TRUE;
6776  }
6777  else if (c == '\t') {
6778  int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
6779  p->heredoc_line_indent = w * TAB_WIDTH;
6780  return TRUE;
6781  }
6782  else if (c != '\n') {
6783  if (p->heredoc_indent > p->heredoc_line_indent) {
6784  p->heredoc_indent = p->heredoc_line_indent;
6785  }
6786  p->heredoc_line_indent = -1;
6787  }
6788  }
6789  return FALSE;
6790 }
6791 
6792 static void
6793 parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
6794 {
6795  YYLTYPE loc = RUBY_INIT_YYLLOC();
6796  const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
6797  compile_error(p, "%s mixed within %s source", n1, n2);
6798  parser_show_error_line(p, &loc);
6799 }
6800 
6801 static void
6802 parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
6803 {
6804  const char *pos = p->lex.pcur;
6805  p->lex.pcur = beg;
6806  parser_mixed_error(p, enc1, enc2);
6807  p->lex.pcur = pos;
6808 }
6809 
6810 static int
6811 tokadd_string(struct parser_params *p,
6812  int func, int term, int paren, long *nest,
6813  rb_encoding **encp, rb_encoding **enc)
6814 {
6815  int c;
6816  bool erred = false;
6817 
6818 #define mixed_error(enc1, enc2) \
6819  (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
6820 #define mixed_escape(beg, enc1, enc2) \
6821  (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
6822 
6823  while ((c = nextc(p)) != -1) {
6824  if (p->heredoc_indent > 0) {
6825  parser_update_heredoc_indent(p, c);
6826  }
6827 
6828  if (paren && c == paren) {
6829  ++*nest;
6830  }
6831  else if (c == term) {
6832  if (!nest || !*nest) {
6833  pushback(p, c);
6834  break;
6835  }
6836  --*nest;
6837  }
6838  else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
6839  int c2 = *p->lex.pcur;
6840  if (c2 == '$' || c2 == '@' || c2 == '{') {
6841  pushback(p, c);
6842  break;
6843  }
6844  }
6845  else if (c == '\\') {
6846  literal_flush(p, p->lex.pcur - 1);
6847  c = nextc(p);
6848  switch (c) {
6849  case '\n':
6850  if (func & STR_FUNC_QWORDS) break;
6851  if (func & STR_FUNC_EXPAND) {
6852  if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
6853  continue;
6854  if (c == term) {
6855  c = '\\';
6856  goto terminate;
6857  }
6858  }
6859  tokadd(p, '\\');
6860  break;
6861 
6862  case '\\':
6863  if (func & STR_FUNC_ESCAPE) tokadd(p, c);
6864  break;
6865 
6866  case 'u':
6867  if ((func & STR_FUNC_EXPAND) == 0) {
6868  tokadd(p, '\\');
6869  break;
6870  }
6871  tokadd_utf8(p, enc, term,
6872  func & STR_FUNC_SYMBOL,
6873  func & STR_FUNC_REGEXP);
6874  continue;
6875 
6876  default:
6877  if (c == -1) return -1;
6878  if (!ISASCII(c)) {
6879  if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
6880  goto non_ascii;
6881  }
6882  if (func & STR_FUNC_REGEXP) {
6883  if (c == term && !simple_re_meta(c)) {
6884  tokadd(p, c);
6885  continue;
6886  }
6887  pushback(p, c);
6888  if ((c = tokadd_escape(p, enc)) < 0)
6889  return -1;
6890  if (*enc && *enc != *encp) {
6891  mixed_escape(p->lex.ptok+2, *enc, *encp);
6892  }
6893  continue;
6894  }
6895  else if (func & STR_FUNC_EXPAND) {
6896  pushback(p, c);
6897  if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
6898  c = read_escape(p, 0, enc);
6899  }
6900  else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6901  /* ignore backslashed spaces in %w */
6902  }
6903  else if (c != term && !(paren && c == paren)) {
6904  tokadd(p, '\\');
6905  pushback(p, c);
6906  continue;
6907  }
6908  }
6909  }
6910  else if (!parser_isascii(p)) {
6911  non_ascii:
6912  if (!*enc) {
6913  *enc = *encp;
6914  }
6915  else if (*enc != *encp) {
6916  mixed_error(*enc, *encp);
6917  continue;
6918  }
6919  if (tokadd_mbchar(p, c) == -1) return -1;
6920  continue;
6921  }
6922  else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6923  pushback(p, c);
6924  break;
6925  }
6926  if (c & 0x80) {
6927  if (!*enc) {
6928  *enc = *encp;
6929  }
6930  else if (*enc != *encp) {
6931  mixed_error(*enc, *encp);
6932  continue;
6933  }
6934  }
6935  tokadd(p, c);
6936  }
6937  terminate:
6938  if (*enc) *encp = *enc;
6939  return c;
6940 }
6941 
6942 static inline rb_strterm_t *
6943 new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
6944 {
6945  return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
6946 }
6947 
6948 /* imemo_parser_strterm for literal */
6949 #define NEW_STRTERM(func, term, paren) \
6950  new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
6951 
6952 #ifdef RIPPER
6953 static void
6954 flush_string_content(struct parser_params *p, rb_encoding *enc)
6955 {
6956  VALUE content = yylval.val;
6957  if (!ripper_is_node_yylval(content))
6958  content = ripper_new_yylval(p, 0, 0, content);
6959  if (has_delayed_token(p)) {
6960  ptrdiff_t len = p->lex.pcur - p->lex.ptok;
6961  if (len > 0) {
6962  rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
6963  }
6964  dispatch_delayed_token(p, tSTRING_CONTENT);
6965  p->lex.ptok = p->lex.pcur;
6966  RNODE(content)->nd_rval = yylval.val;
6967  }
6968  dispatch_scan_event(p, tSTRING_CONTENT);
6969  if (yylval.val != content)
6970  RNODE(content)->nd_rval = yylval.val;
6971  yylval.val = content;
6972 }
6973 #else
6974 #define flush_string_content(p, enc) ((void)(enc))
6975 #endif
6976 
6977 RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
6978 /* this can be shared with ripper, since it's independent from struct
6979  * parser_params. */
6980 #ifndef RIPPER
6981 #define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
6982 #define SPECIAL_PUNCT(idx) ( \
6983  BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
6984  BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
6985  BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
6986  BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
6987  BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
6988  BIT('0', idx))
6989 const unsigned int ruby_global_name_punct_bits[] = {
6990  SPECIAL_PUNCT(0),
6991  SPECIAL_PUNCT(1),
6992  SPECIAL_PUNCT(2),
6993 };
6994 #undef BIT
6995 #undef SPECIAL_PUNCT
6996 #endif
6997 
6998 static enum yytokentype
6999 parser_peek_variable_name(struct parser_params *p)
7000 {
7001  int c;
7002  const char *ptr = p->lex.pcur;
7003 
7004  if (ptr + 1 >= p->lex.pend) return 0;
7005  c = *ptr++;
7006  switch (c) {
7007  case '$':
7008  if ((c = *ptr) == '-') {
7009  if (++ptr >= p->lex.pend) return 0;
7010  c = *ptr;
7011  }
7012  else if (is_global_name_punct(c) || ISDIGIT(c)) {
7013  return tSTRING_DVAR;
7014  }
7015  break;
7016  case '@':
7017  if ((c = *ptr) == '@') {
7018  if (++ptr >= p->lex.pend) return 0;
7019  c = *ptr;
7020  }
7021  break;
7022  case '{':
7023  p->lex.pcur = ptr;
7024  p->command_start = TRUE;
7025  return tSTRING_DBEG;
7026  default:
7027  return 0;
7028  }
7029  if (!ISASCII(c) || c == '_' || ISALPHA(c))
7030  return tSTRING_DVAR;
7031  return 0;
7032 }
7033 
7034 #define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7035 #define IS_END() IS_lex_state(EXPR_END_ANY)
7036 #define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7037 #define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7038 #define IS_LABEL_POSSIBLE() (\
7039  (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7040  IS_ARG())
7041 #define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7042 #define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7043 
7044 static inline enum yytokentype
7045 parser_string_term(struct parser_params *p, int func)
7046 {
7047  p->lex.strterm = 0;
7048  if (func & STR_FUNC_REGEXP) {
7049  set_yylval_num(regx_options(p));
7050  dispatch_scan_event(p, tREGEXP_END);
7051  SET_LEX_STATE(EXPR_END);
7052  return tREGEXP_END;
7053  }
7054  if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7055  nextc(p);
7056  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7057  return tLABEL_END;
7058  }
7059  SET_LEX_STATE(EXPR_END);
7060  return tSTRING_END;
7061 }
7062 
7063 static enum yytokentype
7064 parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7065 {
7066  int func = (int)quote->u1.func;
7067  int term = (int)quote->u3.term;
7068  int paren = (int)quote->u2.paren;
7069  int c, space = 0;
7070  rb_encoding *enc = p->enc;
7071  rb_encoding *base_enc = 0;
7072  VALUE lit;
7073 
7074  if (func & STR_FUNC_TERM) {
7075  if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7076  SET_LEX_STATE(EXPR_END);
7077  p->lex.strterm = 0;
7078  return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7079  }
7080  c = nextc(p);
7081  if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7082  do {c = nextc(p);} while (ISSPACE(c));
7083  space = 1;
7084  }
7085  if (func & STR_FUNC_LIST) {
7086  quote->u1.func &= ~STR_FUNC_LIST;
7087  space = 1;
7088  }
7089  if (c == term && !quote->u0.nest) {
7090  if (func & STR_FUNC_QWORDS) {
7091  quote->u1.func |= STR_FUNC_TERM;
7092  pushback(p, c); /* dispatch the term at tSTRING_END */
7093  add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7094  return ' ';
7095  }
7096  return parser_string_term(p, func);
7097  }
7098  if (space) {
7099  pushback(p, c);
7100  add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7101  return ' ';
7102  }
7103  newtok(p);
7104  if ((func & STR_FUNC_EXPAND) && c == '#') {
7105  int t = parser_peek_variable_name(p);
7106  if (t) return t;
7107  tokadd(p, '#');
7108  c = nextc(p);
7109  }
7110  pushback(p, c);
7111  if (tokadd_string(p, func, term, paren, &quote->u0.nest,
7112  &enc, &base_enc) == -1) {
7113  if (p->eofp) {
7114 #ifndef RIPPER
7115 # define unterminated_literal(mesg) yyerror0(mesg)
7116 #else
7117 # define unterminated_literal(mesg) compile_error(p, mesg)
7118 #endif
7119  literal_flush(p, p->lex.pcur);
7120  if (func & STR_FUNC_QWORDS) {
7121  /* no content to add, bailing out here */
7122  unterminated_literal("unterminated list meets end of file");
7123  p->lex.strterm = 0;
7124  return tSTRING_END;
7125  }
7126  if (func & STR_FUNC_REGEXP) {
7127  unterminated_literal("unterminated regexp meets end of file");
7128  }
7129  else {
7130  unterminated_literal("unterminated string meets end of file");
7131  }
7132  quote->u1.func |= STR_FUNC_TERM;
7133  }
7134  }
7135 
7136  tokfix(p);
7137  lit = STR_NEW3(tok(p), toklen(p), enc, func);
7138  set_yylval_str(lit);
7139  flush_string_content(p, enc);
7140 
7141  return tSTRING_CONTENT;
7142 }
7143 
7144 static enum yytokentype
7145 heredoc_identifier(struct parser_params *p)
7146 {
7147  /*
7148  * term_len is length of `<<"END"` except `END`,
7149  * in this case term_len is 4 (<, <, " and ").
7150  */
7151  long len, offset = p->lex.pcur - p->lex.pbeg;
7152  int c = nextc(p), term, func = 0, quote = 0;
7153  enum yytokentype token = tSTRING_BEG;
7154  int indent = 0;
7155 
7156  if (c == '-') {
7157  c = nextc(p);
7158  func = STR_FUNC_INDENT;
7159  offset++;
7160  }
7161  else if (c == '~') {
7162  c = nextc(p);
7163  func = STR_FUNC_INDENT;
7164  offset++;
7165  indent = INT_MAX;
7166  }
7167  switch (c) {
7168  case '\'':
7169  func |= str_squote; goto quoted;
7170  case '"':
7171  func |= str_dquote; goto quoted;
7172  case '`':
7173  token = tXSTRING_BEG;
7174  func |= str_xquote; goto quoted;
7175 
7176  quoted:
7177  quote++;
7178  offset++;
7179  term = c;
7180  len = 0;
7181  while ((c = nextc(p)) != term) {
7182  if (c == -1 || c == '\r' || c == '\n') {
7183  yyerror(NULL, p, "unterminated here document identifier");
7184  return -1;
7185  }
7186  }
7187  break;
7188 
7189  default:
7190  if (!parser_is_identchar(p)) {
7191  pushback(p, c);
7192  if (func & STR_FUNC_INDENT) {
7193  pushback(p, indent > 0 ? '~' : '-');
7194  }
7195  return 0;
7196  }
7197  func |= str_dquote;
7198  do {
7199  int n = parser_precise_mbclen(p, p->lex.pcur-1);
7200  if (n < 0) return 0;
7201  p->lex.pcur += --n;
7202  } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7203  pushback(p, c);
7204  break;
7205  }
7206 
7207  len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7208  if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7209  yyerror(NULL, p, "too long here document identifier");
7210  dispatch_scan_event(p, tHEREDOC_BEG);
7211  lex_goto_eol(p);
7212 
7213  p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7214  p->lex.strterm->flags |= STRTERM_HEREDOC;
7215  rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7216  here->offset = offset;
7217  here->sourceline = p->ruby_sourceline;
7218  here->length = (int)len;
7219  here->quote = quote;
7220  here->func = func;
7221 
7222  token_flush(p);
7223  p->heredoc_indent = indent;
7224  p->heredoc_line_indent = 0;
7225  return token;
7226 }
7227 
7228 static void
7229 heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7230 {
7231  VALUE line;
7232 
7233  p->lex.strterm = 0;
7234  line = here->lastline;
7235  p->lex.lastline = line;
7236  p->lex.pbeg = RSTRING_PTR(line);
7237  p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7238  p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7239  p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7240  p->heredoc_end = p->ruby_sourceline;
7241  p->ruby_sourceline = (int)here->sourceline;
7242  if (p->eofp) p->lex.nextline = Qnil;
7243  p->eofp = 0;
7244 }
7245 
7246 static int
7247 dedent_string(VALUE string, int width)
7248 {
7249  char *str;
7250  long len;
7251  int i, col = 0;
7252 
7253  RSTRING_GETMEM(string, str, len);
7254  for (i = 0; i < len && col < width; i++) {
7255  if (str[i] == ' ') {
7256  col++;
7257  }
7258  else if (str[i] == '\t') {
7259  int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7260  if (n > width) break;
7261  col = n;
7262  }
7263  else {
7264  break;
7265  }
7266  }
7267  if (!i) return 0;
7268  rb_str_modify(string);
7269  str = RSTRING_PTR(string);
7270  if (RSTRING_LEN(string) != len)
7271  rb_fatal("literal string changed: %+"PRIsVALUE, string);
7272  MEMMOVE(str, str + i, char, len - i);
7273  rb_str_set_len(string, len - i);
7274  return i;
7275 }
7276 
7277 #ifndef RIPPER
7278 static NODE *
7279 heredoc_dedent(struct parser_params *p, NODE *root)
7280 {
7281  NODE *node, *str_node, *prev_node;
7282  int indent = p->heredoc_indent;
7283  VALUE prev_lit = 0;
7284 
7285  if (indent <= 0) return root;
7286  p->heredoc_indent = 0;
7287  if (!root) return root;
7288 
7289  prev_node = node = str_node = root;
7290  if (nd_type(root) == NODE_LIST) str_node = root->nd_head;
7291 
7292  while (str_node) {
7293  VALUE lit = str_node->nd_lit;
7294  if (str_node->flags & NODE_FL_NEWLINE) {
7295  dedent_string(lit, indent);
7296  }
7297  if (!prev_lit) {
7298  prev_lit = lit;
7299  }
7300  else if (!literal_concat0(p, prev_lit, lit)) {
7301  return 0;
7302  }
7303  else {
7304  NODE *end = node->nd_end;
7305  node = prev_node->nd_next = node->nd_next;
7306  if (!node) {
7307  if (nd_type(prev_node) == NODE_DSTR)
7308  nd_set_type(prev_node, NODE_STR);
7309  break;
7310  }
7311  node->nd_end = end;
7312  goto next_str;
7313  }
7314 
7315  str_node = 0;
7316  while ((node = (prev_node = node)->nd_next) != 0) {
7317  next_str:
7318  if (nd_type(node) != NODE_LIST) break;
7319  if ((str_node = node->nd_head) != 0) {
7320  enum node_type type = nd_type(str_node);
7321  if (type == NODE_STR || type == NODE_DSTR) break;
7322  prev_lit = 0;
7323  str_node = 0;
7324  }
7325  }
7326  }
7327  return root;
7328 }
7329 #else /* RIPPER */
7330 static VALUE
7331 heredoc_dedent(struct parser_params *p, VALUE array)
7332 {
7333  int indent = p->heredoc_indent;
7334 
7335  if (indent <= 0) return array;
7336  p->heredoc_indent = 0;
7337  dispatch2(heredoc_dedent, array, INT2NUM(indent));
7338  return array;
7339 }
7340 
7341 /*
7342  * call-seq:
7343  * Ripper.dedent_string(input, width) -> Integer
7344  *
7345  * USE OF RIPPER LIBRARY ONLY.
7346  *
7347  * Strips up to +width+ leading whitespaces from +input+,
7348  * and returns the stripped column width.
7349  */
7350 static VALUE
7351 parser_dedent_string(VALUE self, VALUE input, VALUE width)
7352 {
7353  int wid, col;
7354 
7355  StringValue(input);
7356  wid = NUM2UINT(width);
7357  col = dedent_string(input, wid);
7358  return INT2NUM(col);
7359 }
7360 #endif
7361 
7362 static int
7363 whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7364 {
7365  const char *ptr = p->lex.pbeg;
7366  long n;
7367 
7368  if (indent) {
7369  while (*ptr && ISSPACE(*ptr)) ptr++;
7370  }
7371  n = p->lex.pend - (ptr + len);
7372  if (n < 0) return FALSE;
7373  if (n > 0 && ptr[len] != '\n') {
7374  if (ptr[len] != '\r') return FALSE;
7375  if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7376  }
7377  return strncmp(eos, ptr, len) == 0;
7378 }
7379 
7380 static int
7381 word_match_p(struct parser_params *p, const char *word, long len)
7382 {
7383  if (strncmp(p->lex.pcur, word, len)) return 0;
7384  if (p->lex.pcur + len == p->lex.pend) return 1;
7385  int c = (unsigned char)p->lex.pcur[len];
7386  if (ISSPACE(c)) return 1;
7387  switch (c) {
7388  case '\0': case '\004': case '\032': return 1;
7389  }
7390  return 0;
7391 }
7392 
7393 #define NUM_SUFFIX_R (1<<0)
7394 #define NUM_SUFFIX_I (1<<1)
7395 #define NUM_SUFFIX_ALL 3
7396 
7397 static int
7398 number_literal_suffix(struct parser_params *p, int mask)
7399 {
7400  int c, result = 0;
7401  const char *lastp = p->lex.pcur;
7402 
7403  while ((c = nextc(p)) != -1) {
7404  if ((mask & NUM_SUFFIX_I) && c == 'i') {
7405  result |= (mask & NUM_SUFFIX_I);
7406  mask &= ~NUM_SUFFIX_I;
7407  /* r after i, rational of complex is disallowed */
7408  mask &= ~NUM_SUFFIX_R;
7409  continue;
7410  }
7411  if ((mask & NUM_SUFFIX_R) && c == 'r') {
7412  result |= (mask & NUM_SUFFIX_R);
7413  mask &= ~NUM_SUFFIX_R;
7414  continue;
7415  }
7416  if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7417  p->lex.pcur = lastp;
7418  literal_flush(p, p->lex.pcur);
7419  return 0;
7420  }
7421  pushback(p, c);
7422  break;
7423  }
7424  return result;
7425 }
7426 
7427 static enum yytokentype
7428 set_number_literal(struct parser_params *p, VALUE v,
7429  enum yytokentype type, int suffix)
7430 {
7431  if (suffix & NUM_SUFFIX_I) {
7432  v = rb_complex_raw(INT2FIX(0), v);
7433  type = tIMAGINARY;
7434  }
7435  set_yylval_literal(v);
7436  SET_LEX_STATE(EXPR_END);
7437  return type;
7438 }
7439 
7440 static enum yytokentype
7441 set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7442 {
7443  enum yytokentype type = tINTEGER;
7444  if (suffix & NUM_SUFFIX_R) {
7445  v = rb_rational_raw1(v);
7446  type = tRATIONAL;
7447  }
7448  return set_number_literal(p, v, type, suffix);
7449 }
7450 
7451 #ifdef RIPPER
7452 static void
7453 dispatch_heredoc_end(struct parser_params *p)
7454 {
7455  VALUE str;
7456  if (has_delayed_token(p))
7457  dispatch_delayed_token(p, tSTRING_CONTENT);
7458  str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7459  ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7460  lex_goto_eol(p);
7461  token_flush(p);
7462 }
7463 
7464 #else
7465 #define dispatch_heredoc_end(p) ((void)0)
7466 #endif
7467 
7468 static enum yytokentype
7469 here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7470 {
7471  int c, func, indent = 0;
7472  const char *eos, *ptr, *ptr_end;
7473  long len;
7474  VALUE str = 0;
7475  rb_encoding *enc = p->enc;
7476  rb_encoding *base_enc = 0;
7477  int bol;
7478 
7479  eos = RSTRING_PTR(here->lastline) + here->offset;
7480  len = here->length;
7481  indent = (func = here->func) & STR_FUNC_INDENT;
7482 
7483  if ((c = nextc(p)) == -1) {
7484  error:
7485 #ifdef RIPPER
7486  if (!has_delayed_token(p)) {
7487  dispatch_scan_event(p, tSTRING_CONTENT);
7488  }
7489  else {
7490  if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7491  if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7492  int cr = ENC_CODERANGE_UNKNOWN;
7493  rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7494  if (cr != ENC_CODERANGE_7BIT &&
7495  p->enc == rb_usascii_encoding() &&
7496  enc != rb_utf8_encoding()) {
7497  enc = rb_ascii8bit_encoding();
7498  }
7499  }
7500  rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7501  }
7502  dispatch_delayed_token(p, tSTRING_CONTENT);
7503  }
7504  lex_goto_eol(p);
7505 #endif
7506  heredoc_restore(p, &p->lex.strterm->u.heredoc);
7507  compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7508  (int)len, eos);
7509  token_flush(p);
7510  p->lex.strterm = 0;
7511  SET_LEX_STATE(EXPR_END);
7512  return tSTRING_END;
7513  }
7514  bol = was_bol(p);
7515  if (!bol) {
7516  /* not beginning of line, cannot be the terminator */
7517  }
7518  else if (p->heredoc_line_indent == -1) {
7519  /* `heredoc_line_indent == -1` means
7520  * - "after an interpolation in the same line", or
7521  * - "in a continuing line"
7522  */
7523  p->heredoc_line_indent = 0;
7524  }
7525  else if (whole_match_p(p, eos, len, indent)) {
7526  dispatch_heredoc_end(p);
7527  restore:
7528  heredoc_restore(p, &p->lex.strterm->u.heredoc);
7529  token_flush(p);
7530  p->lex.strterm = 0;
7531  SET_LEX_STATE(EXPR_END);
7532  return tSTRING_END;
7533  }
7534 
7535  if (!(func & STR_FUNC_EXPAND)) {
7536  do {
7537  ptr = RSTRING_PTR(p->lex.lastline);
7538  ptr_end = p->lex.pend;
7539  if (ptr_end > ptr) {
7540  switch (ptr_end[-1]) {
7541  case '\n':
7542  if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7543  ptr_end++;
7544  break;
7545  }
7546  case '\r':
7547  --ptr_end;
7548  }
7549  }
7550 
7551  if (p->heredoc_indent > 0) {
7552  long i = 0;
7553  while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7554  i++;
7555  p->heredoc_line_indent = 0;
7556  }
7557 
7558  if (str)
7559  rb_str_cat(str, ptr, ptr_end - ptr);
7560  else
7561  str = STR_NEW(ptr, ptr_end - ptr);
7562  if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7563  lex_goto_eol(p);
7564  if (p->heredoc_indent > 0) {
7565  goto flush_str;
7566  }
7567  if (nextc(p) == -1) {
7568  if (str) {
7569  str = 0;
7570  }
7571  goto error;
7572  }
7573  } while (!whole_match_p(p, eos, len, indent));
7574  }
7575  else {
7576  /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7577  newtok(p);
7578  if (c == '#') {
7579  int t = parser_peek_variable_name(p);
7580  if (p->heredoc_line_indent != -1) {
7581  if (p->heredoc_indent > p->heredoc_line_indent) {
7582  p->heredoc_indent = p->heredoc_line_indent;
7583  }
7584  p->heredoc_line_indent = -1;
7585  }
7586  if (t) return t;
7587  tokadd(p, '#');
7588  c = nextc(p);
7589  }
7590  do {
7591  pushback(p, c);
7592  enc = p->enc;
7593  if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7594  if (p->eofp) goto error;
7595  goto restore;
7596  }
7597  if (c != '\n') {
7598  if (c == '\\') p->heredoc_line_indent = -1;
7599  flush:
7600  str = STR_NEW3(tok(p), toklen(p), enc, func);
7601  flush_str:
7602  set_yylval_str(str);
7603 #ifndef RIPPER
7604  if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7605 #endif
7606  flush_string_content(p, enc);
7607  return tSTRING_CONTENT;
7608  }
7609  tokadd(p, nextc(p));
7610  if (p->heredoc_indent > 0) {
7611  lex_goto_eol(p);
7612  goto flush;
7613  }
7614  /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
7615  if ((c = nextc(p)) == -1) goto error;
7616  } while (!whole_match_p(p, eos, len, indent));
7617  str = STR_NEW3(tok(p), toklen(p), enc, func);
7618  }
7619  dispatch_heredoc_end(p);
7620 #ifdef RIPPER
7621  str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
7622  yylval.val, str);
7623 #endif
7624  heredoc_restore(p, &p->lex.strterm->u.heredoc);
7625  token_flush(p);
7626  p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
7627  set_yylval_str(str);
7628 #ifndef RIPPER
7629  if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7630 #endif
7631  return tSTRING_CONTENT;
7632 }
7633 
7634 #include "lex.c"
7635 
7636 static int
7637 arg_ambiguous(struct parser_params *p, char c)
7638 {
7639 #ifndef RIPPER
7640  rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
7641 #else
7642  dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
7643 #endif
7644  return TRUE;
7645 }
7646 
7647 static ID
7648 formal_argument(struct parser_params *p, ID lhs)
7649 {
7650  switch (id_type(lhs)) {
7651  case ID_LOCAL:
7652  break;
7653 #ifndef RIPPER
7654  case ID_CONST:
7655  yyerror0("formal argument cannot be a constant");
7656  return 0;
7657  case ID_INSTANCE:
7658  yyerror0("formal argument cannot be an instance variable");
7659  return 0;
7660  case ID_GLOBAL:
7661  yyerror0("formal argument cannot be a global variable");
7662  return 0;
7663  case ID_CLASS:
7664  yyerror0("formal argument cannot be a class variable");
7665  return 0;
7666  default:
7667  yyerror0("formal argument must be local variable");
7668  return 0;
7669 #else
7670  default:
7671  lhs = dispatch1(param_error, lhs);
7672  ripper_error(p);
7673  return 0;
7674 #endif
7675  }
7676  shadowing_lvar(p, lhs);
7677  return lhs;
7678 }
7679 
7680 static int
7681 lvar_defined(struct parser_params *p, ID id)
7682 {
7683  return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
7684 }
7685 
7686 /* emacsen -*- hack */
7687 static long
7688 parser_encode_length(struct parser_params *p, const char *name, long len)
7689 {
7690  long nlen;
7691 
7692  if (len > 5 && name[nlen = len - 5] == '-') {
7693  if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
7694  return nlen;
7695  }
7696  if (len > 4 && name[nlen = len - 4] == '-') {
7697  if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
7698  return nlen;
7699  if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
7700  !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
7701  /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
7702  return nlen;
7703  }
7704  return len;
7705 }
7706 
7707 static void
7708 parser_set_encode(struct parser_params *p, const char *name)
7709 {
7710  int idx = rb_enc_find_index(name);
7711  rb_encoding *enc;
7712  VALUE excargs[3];
7713 
7714  if (idx < 0) {
7715  excargs[1] = rb_sprintf("unknown encoding name: %s", name);
7716  error:
7717  excargs[0] = rb_eArgError;
7718  excargs[2] = rb_make_backtrace();
7719  rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
7720  rb_exc_raise(rb_make_exception(3, excargs));
7721  }
7722  enc = rb_enc_from_index(idx);
7723  if (!rb_enc_asciicompat(enc)) {
7724  excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
7725  goto error;
7726  }
7727  p->enc = enc;
7728 #ifndef RIPPER
7729  if (p->debug_lines) {
7730  VALUE lines = p->debug_lines;
7731  long i, n = RARRAY_LEN(lines);
7732  for (i = 0; i < n; ++i) {
7733  rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
7734  }
7735  }
7736 #endif
7737 }
7738 
7739 static int
7740 comment_at_top(struct parser_params *p)
7741 {
7742  const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
7743  if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
7744  while (ptr < ptr_end) {
7745  if (!ISSPACE(*ptr)) return 0;
7746  ptr++;
7747  }
7748  return 1;
7749 }
7750 
7751 typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
7752 typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
7753 
7754 static void
7755 magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
7756 {
7757  if (!comment_at_top(p)) {
7758  return;
7759  }
7760  parser_set_encode(p, val);
7761 }
7762 
7763 static int
7764 parser_get_bool(struct parser_params *p, const char *name, const char *val)
7765 {
7766  switch (*val) {
7767  case 't': case 'T':
7768  if (strcasecmp(val, "true") == 0) {
7769  return TRUE;
7770  }
7771  break;
7772  case 'f': case 'F':
7773  if (strcasecmp(val, "false") == 0) {
7774  return FALSE;
7775  }
7776  break;
7777  }
7778  rb_compile_warning(p->ruby_sourcefile, p->ruby_sourceline, "invalid value for %s: %s", name, val);
7779  return -1;
7780 }
7781 
7782 static void
7783 parser_set_token_info(struct parser_params *p, const char *name, const char *val)
7784 {
7785  int b = parser_get_bool(p, name, val);
7786  if (b >= 0) p->token_info_enabled = b;
7787 }
7788 
7789 static void
7790 parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
7791 {
7792  int b;
7793 
7794  if (p->token_seen) {
7795  rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
7796  return;
7797  }
7798 
7799  b = parser_get_bool(p, name, val);
7800  if (b < 0) return;
7801 
7802  if (!p->compile_option)
7803  p->compile_option = rb_obj_hide(rb_ident_hash_new());
7804  rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
7805  (b ? Qtrue : Qfalse));
7806 }
7807 
7808 # if WARN_PAST_SCOPE
7809 static void
7810 parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
7811 {
7812  int b = parser_get_bool(p, name, val);
7813  if (b >= 0) p->past_scope_enabled = b;
7814 }
7815 # endif
7816 
7817 struct magic_comment {
7818  const char *name;
7819  rb_magic_comment_setter_t func;
7820  rb_magic_comment_length_t length;
7821 };
7822 
7823 static const struct magic_comment magic_comments[] = {
7824  {"coding", magic_comment_encoding, parser_encode_length},
7825  {"encoding", magic_comment_encoding, parser_encode_length},
7826  {"frozen_string_literal", parser_set_compile_option_flag},
7827  {"warn_indent", parser_set_token_info},
7828 # if WARN_PAST_SCOPE
7829  {"warn_past_scope", parser_set_past_scope},
7830 # endif
7831 };
7832 
7833 static const char *
7834 magic_comment_marker(const char *str, long len)
7835 {
7836  long i = 2;
7837 
7838  while (i < len) {
7839  switch (str[i]) {
7840  case '-':
7841  if (str[i-1] == '*' && str[i-2] == '-') {
7842  return str + i + 1;
7843  }
7844  i += 2;
7845  break;
7846  case '*':
7847  if (i + 1 >= len) return 0;
7848  if (str[i+1] != '-') {
7849  i += 4;
7850  }
7851  else if (str[i-1] != '-') {
7852  i += 2;
7853  }
7854  else {
7855  return str + i + 2;
7856  }
7857  break;
7858  default:
7859  i += 3;
7860  break;
7861  }
7862  }
7863  return 0;
7864 }
7865 
7866 static int
7867 parser_magic_comment(struct parser_params *p, const char *str, long len)
7868 {
7869  int indicator = 0;
7870  VALUE name = 0, val = 0;
7871  const char *beg, *end, *vbeg, *vend;
7872 #define str_copy(_s, _p, _n) ((_s) \
7873  ? (void)(rb_str_resize((_s), (_n)), \
7874  MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
7875  : (void)((_s) = STR_NEW((_p), (_n))))
7876 
7877  if (len <= 7) return FALSE;
7878  if (!!(beg = magic_comment_marker(str, len))) {
7879  if (!(end = magic_comment_marker(beg, str + len - beg)))
7880  return FALSE;
7881  indicator = TRUE;
7882  str = beg;
7883  len = end - beg - 3;
7884  }
7885 
7886  /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
7887  while (len > 0) {
7888  const struct magic_comment *mc = magic_comments;
7889  char *s;
7890  int i;
7891  long n = 0;
7892 
7893  for (; len > 0 && *str; str++, --len) {
7894  switch (*str) {
7895  case '\'': case '"': case ':': case ';':
7896  continue;
7897  }
7898  if (!ISSPACE(*str)) break;
7899  }
7900  for (beg = str; len > 0; str++, --len) {
7901  switch (*str) {
7902  case '\'': case '"': case ':': case ';':
7903  break;
7904  default:
7905  if (ISSPACE(*str)) break;
7906  continue;
7907  }
7908  break;
7909  }
7910  for (end = str; len > 0 && ISSPACE(*str); str++, --len);
7911  if (!len) break;
7912  if (*str != ':') {
7913  if (!indicator) return FALSE;
7914  continue;
7915  }
7916 
7917  do str++; while (--len > 0 && ISSPACE(*str));
7918  if (!len) break;
7919  if (*str == '"') {
7920  for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
7921  if (*str == '\\') {
7922  --len;
7923  ++str;
7924  }
7925  }
7926  vend = str;
7927  if (len) {
7928  --len;
7929  ++str;
7930  }
7931  }
7932  else {
7933  for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
7934  vend = str;
7935  }
7936  if (indicator) {
7937  while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
7938  }
7939  else {
7940  while (len > 0 && (ISSPACE(*str))) --len, str++;
7941  if (len) return FALSE;
7942  }
7943 
7944  n = end - beg;
7945  str_copy(name, beg, n);
7946  s = RSTRING_PTR(name);
7947  for (i = 0; i < n; ++i) {
7948  if (s[i] == '-') s[i] = '_';
7949  }
7950  do {
7951  if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
7952  n = vend - vbeg;
7953  if (mc->length) {
7954  n = (*mc->length)(p, vbeg, n);
7955  }
7956  str_copy(val, vbeg, n);
7957  (*mc->func)(p, mc->name, RSTRING_PTR(val));
7958  break;
7959  }
7960  } while (++mc < magic_comments + numberof(magic_comments));
7961 #ifdef RIPPER
7962  str_copy(val, vbeg, vend - vbeg);
7963  dispatch2(magic_comment, name, val);
7964 #endif
7965  }
7966 
7967  return TRUE;
7968 }
7969 
7970 static void
7971 set_file_encoding(struct parser_params *p, const char *str, const char *send)
7972 {
7973  int sep = 0;
7974  const char *beg = str;
7975  VALUE s;
7976 
7977  for (;;) {
7978  if (send - str <= 6) return;
7979  switch (str[6]) {
7980  case 'C': case 'c': str += 6; continue;
7981  case 'O': case 'o': str += 5; continue;
7982  case 'D': case 'd': str += 4; continue;
7983  case 'I': case 'i': str += 3; continue;
7984  case 'N': case 'n': str += 2; continue;
7985  case 'G': case 'g': str += 1; continue;
7986  case '=': case ':':
7987  sep = 1;
7988  str += 6;
7989  break;
7990  default:
7991  str += 6;
7992  if (ISSPACE(*str)) break;
7993  continue;
7994  }
7995  if (STRNCASECMP(str-6, "coding", 6) == 0) break;
7996  }
7997  for (;;) {
7998  do {
7999  if (++str >= send) return;
8000  } while (ISSPACE(*str));
8001  if (sep) break;
8002  if (*str != '=' && *str != ':') return;
8003  sep = 1;
8004  str++;
8005  }
8006  beg = str;
8007  while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8008  s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8009  parser_set_encode(p, RSTRING_PTR(s));
8010  rb_str_resize(s, 0);
8011 }
8012 
8013 static void
8014 parser_prepare(struct parser_params *p)
8015 {
8016  int c = nextc(p);
8017  p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8018  switch (c) {
8019  case '#':
8020  if (peek(p, '!')) p->has_shebang = 1;
8021  break;
8022  case 0xef: /* UTF-8 BOM marker */
8023  if (p->lex.pend - p->lex.pcur >= 2 &&
8024  (unsigned char)p->lex.pcur[0] == 0xbb &&
8025  (unsigned char)p->lex.pcur[1] == 0xbf) {
8026  p->enc = rb_utf8_encoding();
8027  p->lex.pcur += 2;
8028  p->lex.pbeg = p->lex.pcur;
8029  return;
8030  }
8031  break;
8032  case EOF:
8033  return;
8034  }
8035  pushback(p, c);
8036  p->enc = rb_enc_get(p->lex.lastline);
8037 }
8038 
8039 #ifndef RIPPER
8040 #define ambiguous_operator(tok, op, syn) ( \
8041  rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8042  rb_warning0("even though it seems like "syn""))
8043 #else
8044 #define ambiguous_operator(tok, op, syn) \
8045  dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8046 #endif
8047 #define warn_balanced(tok, op, syn) ((void) \
8048  (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8049  space_seen && !ISSPACE(c) && \
8050  (ambiguous_operator(tok, op, syn), 0)), \
8051  (enum yytokentype)(tok))
8052 
8053 static VALUE
8054 parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8055 {
8056  VALUE v;
8057  char *point = &str[seen_point];
8058  size_t fraclen = len-seen_point-1;
8059  memmove(point, point+1, fraclen+1);
8060  v = rb_cstr_to_inum(str, 10, FALSE);
8061  return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8062 }
8063 
8064 static enum yytokentype
8065 no_digits(struct parser_params *p)
8066 {
8067  yyerror0("numeric literal without digits");
8068  if (peek(p, '_')) nextc(p);
8069  /* dummy 0, for tUMINUS_NUM at numeric */
8070  return set_integer_literal(p, INT2FIX(0), 0);
8071 }
8072 
8073 static enum yytokentype
8074 parse_numeric(struct parser_params *p, int c)
8075 {
8076  int is_float, seen_point, seen_e, nondigit;
8077  int suffix;
8078 
8079  is_float = seen_point = seen_e = nondigit = 0;
8080  SET_LEX_STATE(EXPR_END);
8081  newtok(p);
8082  if (c == '-' || c == '+') {
8083  tokadd(p, c);
8084  c = nextc(p);
8085  }
8086  if (c == '0') {
8087  int start = toklen(p);
8088  c = nextc(p);
8089  if (c == 'x' || c == 'X') {
8090  /* hexadecimal */
8091  c = nextc(p);
8092  if (c != -1 && ISXDIGIT(c)) {
8093  do {
8094  if (c == '_') {
8095  if (nondigit) break;
8096  nondigit = c;
8097  continue;
8098  }
8099  if (!ISXDIGIT(c)) break;
8100  nondigit = 0;
8101  tokadd(p, c);
8102  } while ((c = nextc(p)) != -1);
8103  }
8104  pushback(p, c);
8105  tokfix(p);
8106  if (toklen(p) == start) {
8107  return no_digits(p);
8108  }
8109  else if (nondigit) goto trailing_uc;
8110  suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8111  return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8112  }
8113  if (c == 'b' || c == 'B') {
8114  /* binary */
8115  c = nextc(p);
8116  if (c == '0' || c == '1') {
8117  do {
8118  if (c == '_') {
8119  if (nondigit) break;
8120  nondigit = c;
8121  continue;
8122  }
8123  if (c != '0' && c != '1') break;
8124  nondigit = 0;
8125  tokadd(p, c);
8126  } while ((c = nextc(p)) != -1);
8127  }
8128  pushback(p, c);
8129  tokfix(p);
8130  if (toklen(p) == start) {
8131  return no_digits(p);
8132  }
8133  else if (nondigit) goto trailing_uc;
8134  suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8135  return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8136  }
8137  if (c == 'd' || c == 'D') {
8138  /* decimal */
8139  c = nextc(p);
8140  if (c != -1 && ISDIGIT(c)) {
8141  do {
8142  if (c == '_') {
8143  if (nondigit) break;
8144  nondigit = c;
8145  continue;
8146  }
8147  if (!ISDIGIT(c)) break;
8148  nondigit = 0;
8149  tokadd(p, c);
8150  } while ((c = nextc(p)) != -1);
8151  }
8152  pushback(p, c);
8153  tokfix(p);
8154  if (toklen(p) == start) {
8155  return no_digits(p);
8156  }
8157  else if (nondigit) goto trailing_uc;
8158  suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8159  return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8160  }
8161  if (c == '_') {
8162  /* 0_0 */
8163  goto octal_number;
8164  }
8165  if (c == 'o' || c == 'O') {
8166  /* prefixed octal */
8167  c = nextc(p);
8168  if (c == -1 || c == '_' || !ISDIGIT(c)) {
8169  return no_digits(p);
8170  }
8171  }
8172  if (c >= '0' && c <= '7') {
8173  /* octal */
8174  octal_number:
8175  do {
8176  if (c == '_') {
8177  if (nondigit) break;
8178  nondigit = c;
8179  continue;
8180  }
8181  if (c < '0' || c > '9') break;
8182  if (c > '7') goto invalid_octal;
8183  nondigit = 0;
8184  tokadd(p, c);
8185  } while ((c = nextc(p)) != -1);
8186  if (toklen(p) > start) {
8187  pushback(p, c);
8188  tokfix(p);
8189  if (nondigit) goto trailing_uc;
8190  suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8191  return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8192  }
8193  if (nondigit) {
8194  pushback(p, c);
8195  goto trailing_uc;
8196  }
8197  }
8198  if (c > '7' && c <= '9') {
8199  invalid_octal:
8200  yyerror0("Invalid octal digit");
8201  }
8202  else if (c == '.' || c == 'e' || c == 'E') {
8203  tokadd(p, '0');
8204  }
8205  else {
8206  pushback(p, c);
8207  suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8208  return set_integer_literal(p, INT2FIX(0), suffix);
8209  }
8210  }
8211 
8212  for (;;) {
8213  switch (c) {
8214  case '0': case '1': case '2': case '3': case '4':
8215  case '5': case '6': case '7': case '8': case '9':
8216  nondigit = 0;
8217  tokadd(p, c);
8218  break;
8219 
8220  case '.':
8221  if (nondigit) goto trailing_uc;
8222  if (seen_point || seen_e) {
8223  goto decode_num;
8224  }
8225  else {
8226  int c0 = nextc(p);
8227  if (c0 == -1 || !ISDIGIT(c0)) {
8228  pushback(p, c0);
8229  goto decode_num;
8230  }
8231  c = c0;
8232  }
8233  seen_point = toklen(p);
8234  tokadd(p, '.');
8235  tokadd(p, c);
8236  is_float++;
8237  nondigit = 0;
8238  break;
8239 
8240  case 'e':
8241  case 'E':
8242  if (nondigit) {
8243  pushback(p, c);
8244  c = nondigit;
8245  goto decode_num;
8246  }
8247  if (seen_e) {
8248  goto decode_num;
8249  }
8250  nondigit = c;
8251  c = nextc(p);
8252  if (c != '-' && c != '+' && !ISDIGIT(c)) {
8253  pushback(p, c);
8254  nondigit = 0;
8255  goto decode_num;
8256  }
8257  tokadd(p, nondigit);
8258  seen_e++;
8259  is_float++;
8260  tokadd(p, c);
8261  nondigit = (c == '-' || c == '+') ? c : 0;
8262  break;
8263 
8264  case '_': /* `_' in number just ignored */
8265  if (nondigit) goto decode_num;
8266  nondigit = c;
8267  break;
8268 
8269  default:
8270  goto decode_num;
8271  }
8272  c = nextc(p);
8273  }
8274 
8275  decode_num:
8276  pushback(p, c);
8277  if (nondigit) {
8278  trailing_uc:
8279  literal_flush(p, p->lex.pcur - 1);
8280  YYLTYPE loc = RUBY_INIT_YYLLOC();
8281  compile_error(p, "trailing `%c' in number", nondigit);
8282  parser_show_error_line(p, &loc);
8283  }
8284  tokfix(p);
8285  if (is_float) {
8286  enum yytokentype type = tFLOAT;
8287  VALUE v;
8288 
8289  suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8290  if (suffix & NUM_SUFFIX_R) {
8291  type = tRATIONAL;
8292  v = parse_rational(p, tok(p), toklen(p), seen_point);
8293  }
8294  else {
8295  double d = strtod(tok(p), 0);
8296  if (errno == ERANGE) {
8297  rb_warning1("Float %s out of range", WARN_S(tok(p)));
8298  errno = 0;
8299  }
8300  v = DBL2NUM(d);
8301  }
8302  return set_number_literal(p, v, type, suffix);
8303  }
8304  suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8305  return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8306 }
8307 
8308 static enum yytokentype
8309 parse_qmark(struct parser_params *p, int space_seen)
8310 {
8311  rb_encoding *enc;
8312  register int c;
8313  VALUE lit;
8314 
8315  if (IS_END()) {
8316  SET_LEX_STATE(EXPR_VALUE);
8317  return '?';
8318  }
8319  c = nextc(p);
8320  if (c == -1) {
8321  compile_error(p, "incomplete character syntax");
8322  return 0;
8323  }
8324  if (rb_enc_isspace(c, p->enc)) {
8325  if (!IS_ARG()) {
8326  int c2 = escaped_control_code(c);
8327  if (c2) {
8328  WARN_SPACE_CHAR(c2, "?");
8329  }
8330  }
8331  ternary:
8332  pushback(p, c);
8333  SET_LEX_STATE(EXPR_VALUE);
8334  return '?';
8335  }
8336  newtok(p);
8337  enc = p->enc;
8338  if (!parser_isascii(p)) {
8339  if (tokadd_mbchar(p, c) == -1) return 0;
8340  }
8341  else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8342  p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8343  if (space_seen) {
8344  const char *start = p->lex.pcur - 1, *ptr = start;
8345  do {
8346  int n = parser_precise_mbclen(p, ptr);
8347  if (n < 0) return -1;
8348  ptr += n;
8349  } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8350  rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8351  " a conditional operator, put a space after `?'",
8352  WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8353  }
8354  goto ternary;
8355  }
8356  else if (c == '\\') {
8357  if (peek(p, 'u')) {
8358  nextc(p);
8359  enc = rb_utf8_encoding();
8360  tokadd_utf8(p, &enc, -1, 0, 0);
8361  }
8362  else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8363  nextc(p);
8364  if (tokadd_mbchar(p, c) == -1) return 0;
8365  }
8366  else {
8367  c = read_escape(p, 0, &enc);
8368  tokadd(p, c);
8369  }
8370  }
8371  else {
8372  tokadd(p, c);
8373  }
8374  tokfix(p);
8375  lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8376  set_yylval_str(lit);
8377  SET_LEX_STATE(EXPR_END);
8378  return tCHAR;
8379 }
8380 
8381 static enum yytokentype
8382 parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8383 {
8384  register int c;
8385  const char *ptok = p->lex.pcur;
8386 
8387  if (IS_BEG()) {
8388  int term;
8389  int paren;
8390 
8391  c = nextc(p);
8392  quotation:
8393  if (c == -1 || !ISALNUM(c)) {
8394  term = c;
8395  c = 'Q';
8396  }
8397  else {
8398  term = nextc(p);
8399  if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8400  yyerror0("unknown type of %string");
8401  return 0;
8402  }
8403  }
8404  if (c == -1 || term == -1) {
8405  compile_error(p, "unterminated quoted string meets end of file");
8406  return 0;
8407  }
8408  paren = term;
8409  if (term == '(') term = ')';
8410  else if (term == '[') term = ']';
8411  else if (term == '{') term = '}';
8412  else if (term == '<') term = '>';
8413  else paren = 0;
8414 
8415  p->lex.ptok = ptok-1;
8416  switch (c) {
8417  case 'Q':
8418  p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8419  return tSTRING_BEG;
8420 
8421  case 'q':
8422  p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8423  return tSTRING_BEG;
8424 
8425  case 'W':
8426  p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8427  return tWORDS_BEG;
8428 
8429  case 'w':
8430  p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8431  return tQWORDS_BEG;
8432 
8433  case 'I':
8434  p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8435  return tSYMBOLS_BEG;
8436 
8437  case 'i':
8438  p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8439  return tQSYMBOLS_BEG;
8440 
8441  case 'x':
8442  p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8443  return tXSTRING_BEG;
8444 
8445  case 'r':
8446  p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8447  return tREGEXP_BEG;
8448 
8449  case 's':
8450  p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8451  SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8452  return tSYMBEG;
8453 
8454  default:
8455  yyerror0("unknown type of %string");
8456  return 0;
8457  }
8458  }
8459  if ((c = nextc(p)) == '=') {
8460  set_yylval_id('%');
8461  SET_LEX_STATE(EXPR_BEG);
8462  return tOP_ASGN;
8463  }
8464  if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8465  goto quotation;
8466  }
8467  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8468  pushback(p, c);
8469  return warn_balanced('%', "%%", "string literal");
8470 }
8471 
8472 static int
8473 tokadd_ident(struct parser_params *p, int c)
8474 {
8475  do {
8476  if (tokadd_mbchar(p, c) == -1) return -1;
8477  c = nextc(p);
8478  } while (parser_is_identchar(p));
8479  pushback(p, c);
8480  return 0;
8481 }
8482 
8483 static ID
8484 tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8485 {
8486  ID ident = TOK_INTERN();
8487 
8488  set_yylval_name(ident);
8489 
8490  return ident;
8491 }
8492 
8493 static int
8494 parse_numvar(struct parser_params *p)
8495 {
8496  size_t len;
8497  int overflow;
8498  unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8499  const unsigned long nth_ref_max =
8500  ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8501  /* NTH_REF is left-shifted to be ORed with back-ref flag and
8502  * turned into a Fixnum, in compile.c */
8503 
8504  if (overflow || n > nth_ref_max) {
8505  /* compile_error()? */
8506  rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8507  return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8508  }
8509  else {
8510  return (int)n;
8511  }
8512 }
8513 
8514 static enum yytokentype
8515 parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8516 {
8517  const char *ptr = p->lex.pcur;
8518  register int c;
8519 
8520  SET_LEX_STATE(EXPR_END);
8521  p->lex.ptok = ptr - 1; /* from '$' */
8522  newtok(p);
8523  c = nextc(p);
8524  switch (c) {
8525  case '_': /* $_: last read line string */
8526  c = nextc(p);
8527  if (parser_is_identchar(p)) {
8528  tokadd(p, '$');
8529  tokadd(p, '_');
8530  break;
8531  }
8532  pushback(p, c);
8533  c = '_';
8534  /* fall through */
8535  case '~': /* $~: match-data */
8536  case '*': /* $*: argv */
8537  case '$': /* $$: pid */
8538  case '?': /* $?: last status */
8539  case '!': /* $!: error string */
8540  case '@': /* $@: error position */
8541  case '/': /* $/: input record separator */
8542  case '\\': /* $\: output record separator */
8543  case ';': /* $;: field separator */
8544  case ',': /* $,: output field separator */
8545  case '.': /* $.: last read line number */
8546  case '=': /* $=: ignorecase */
8547  case ':': /* $:: load path */
8548  case '<': /* $<: reading filename */
8549  case '>': /* $>: default output handle */
8550  case '\"': /* $": already loaded files */
8551  tokadd(p, '$');
8552  tokadd(p, c);
8553  goto gvar;
8554 
8555  case '-':
8556  tokadd(p, '$');
8557  tokadd(p, c);
8558  c = nextc(p);
8559  if (parser_is_identchar(p)) {
8560  if (tokadd_mbchar(p, c) == -1) return 0;
8561  }
8562  else {
8563  pushback(p, c);
8564  pushback(p, '-');
8565  return '$';
8566  }
8567  gvar:
8568  set_yylval_name(TOK_INTERN());
8569  return tGVAR;
8570 
8571  case '&': /* $&: last match */
8572  case '`': /* $`: string before last match */
8573  case '\'': /* $': string after last match */
8574  case '+': /* $+: string matches last paren. */
8575  if (IS_lex_state_for(last_state, EXPR_FNAME)) {
8576  tokadd(p, '$');
8577  tokadd(p, c);
8578  goto gvar;
8579  }
8580  set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
8581  return tBACK_REF;
8582 
8583  case '1': case '2': case '3':
8584  case '4': case '5': case '6':
8585  case '7': case '8': case '9':
8586  tokadd(p, '$');
8587  do {
8588  tokadd(p, c);
8589  c = nextc(p);
8590  } while (c != -1 && ISDIGIT(c));
8591  pushback(p, c);
8592  if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
8593  tokfix(p);
8594  set_yylval_node(NEW_NTH_REF(parse_numvar(p), &_cur_loc));
8595  return tNTH_REF;
8596 
8597  default:
8598  if (!parser_is_identchar(p)) {
8599  YYLTYPE loc = RUBY_INIT_YYLLOC();
8600  if (c == -1 || ISSPACE(c)) {
8601  compile_error(p, "`$' without identifiers is not allowed as a global variable name");
8602  }
8603  else {
8604  pushback(p, c);
8605  compile_error(p, "`$%c' is not allowed as a global variable name", c);
8606  }
8607  parser_show_error_line(p, &loc);
8608  set_yylval_noname();
8609  return tGVAR;
8610  }
8611  /* fall through */
8612  case '0':
8613  tokadd(p, '$');
8614  }
8615 
8616  if (tokadd_ident(p, c)) return 0;
8617  SET_LEX_STATE(EXPR_END);
8618  tokenize_ident(p, last_state);
8619  return tGVAR;
8620 }
8621 
8622 #ifndef RIPPER
8623 static bool
8624 parser_numbered_param(struct parser_params *p, int n)
8625 {
8626  if (n < 0) return false;
8627 
8628  if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
8629  return false;
8630  }
8631  if (p->max_numparam == ORDINAL_PARAM) {
8632  compile_error(p, "ordinary parameter is defined");
8633  return false;
8634  }
8635  struct vtable *args = p->lvtbl->args;
8636  if (p->max_numparam < n) {
8637  p->max_numparam = n;
8638  }
8639  while (n > args->pos) {
8640  vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
8641  }
8642  return true;
8643 }
8644 #endif
8645 
8646 static enum yytokentype
8647 parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
8648 {
8649  const char *ptr = p->lex.pcur;
8650  enum yytokentype result = tIVAR;
8651  register int c = nextc(p);
8652  YYLTYPE loc;
8653 
8654  p->lex.ptok = ptr - 1; /* from '@' */
8655  newtok(p);
8656  tokadd(p, '@');
8657  if (c == '@') {
8658  result = tCVAR;
8659  tokadd(p, '@');
8660  c = nextc(p);
8661  }
8662  SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
8663  if (c == -1 || !parser_is_identchar(p)) {
8664  pushback(p, c);
8665  RUBY_SET_YYLLOC(loc);
8666  if (result == tIVAR) {
8667  compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
8668  }
8669  else {
8670  compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
8671  }
8672  parser_show_error_line(p, &loc);
8673  set_yylval_noname();
8674  SET_LEX_STATE(EXPR_END);
8675  return result;
8676  }
8677  else if (ISDIGIT(c)) {
8678  pushback(p, c);
8679  RUBY_SET_YYLLOC(loc);
8680  if (result == tIVAR) {
8681  compile_error(p, "`@%c' is not allowed as an instance variable name", c);
8682  }
8683  else {
8684  compile_error(p, "`@@%c' is not allowed as a class variable name", c);
8685  }
8686  parser_show_error_line(p, &loc);
8687  set_yylval_noname();
8688  SET_LEX_STATE(EXPR_END);
8689  return result;
8690  }
8691 
8692  if (tokadd_ident(p, c)) return 0;
8693  tokenize_ident(p, last_state);
8694  return result;
8695 }
8696 
8697 static enum yytokentype
8698 parse_ident(struct parser_params *p, int c, int cmd_state)
8699 {
8700  enum yytokentype result;
8701  int mb = ENC_CODERANGE_7BIT;
8702  const enum lex_state_e last_state = p->lex.state;
8703  ID ident;
8704 
8705  do {
8706  if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
8707  if (tokadd_mbchar(p, c) == -1) return 0;
8708  c = nextc(p);
8709  } while (parser_is_identchar(p));
8710  if ((c == '!' || c == '?') && !peek(p, '=')) {
8711  result = tFID;
8712  tokadd(p, c);
8713  }
8714  else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
8715  (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
8716  result = tIDENTIFIER;
8717  tokadd(p, c);
8718  }
8719  else {
8720  result = tCONSTANT; /* assume provisionally */
8721  pushback(p, c);
8722  }
8723  tokfix(p);
8724 
8725  if (IS_LABEL_POSSIBLE()) {
8726  if (IS_LABEL_SUFFIX(0)) {
8727  SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8728  nextc(p);
8729  set_yylval_name(TOK_INTERN());
8730  return tLABEL;
8731  }
8732  }
8733  if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
8734  const struct kwtable *kw;
8735 
8736  /* See if it is a reserved word. */
8737  kw = rb_reserved_word(tok(p), toklen(p));
8738  if (kw) {
8739  enum lex_state_e state = p->lex.state;
8740  SET_LEX_STATE(kw->state);
8741  if (IS_lex_state_for(state, EXPR_FNAME)) {
8742  set_yylval_name(rb_intern2(tok(p), toklen(p)));
8743  return kw->id[0];
8744  }
8745  if (IS_lex_state(EXPR_BEG)) {
8746  p->command_start = TRUE;
8747  }
8748  if (kw->id[0] == keyword_do) {
8749  if (lambda_beginning_p()) {
8750  p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
8751  return keyword_do_LAMBDA;
8752  }
8753  if (COND_P()) return keyword_do_cond;
8754  if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
8755  return keyword_do_block;
8756  return keyword_do;
8757  }
8758  if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
8759  return kw->id[0];
8760  else {
8761  if (kw->id[0] != kw->id[1])
8762  SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
8763  return kw->id[1];
8764  }
8765  }
8766  }
8767 
8768  if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
8769  if (cmd_state) {
8770  SET_LEX_STATE(EXPR_CMDARG);
8771  }
8772  else {
8773  SET_LEX_STATE(EXPR_ARG);
8774  }
8775  }
8776  else if (p->lex.state == EXPR_FNAME) {
8777  SET_LEX_STATE(EXPR_ENDFN);
8778  }
8779  else {
8780  SET_LEX_STATE(EXPR_END);
8781  }
8782 
8783  ident = tokenize_ident(p, last_state);
8784  if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
8785  if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
8786  (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
8787  lvar_defined(p, ident)) {
8788  SET_LEX_STATE(EXPR_END|EXPR_LABEL);
8789  }
8790  return result;
8791 }
8792 
8793 static enum yytokentype
8794 parser_yylex(struct parser_params *p)
8795 {
8796  register int c;
8797  int space_seen = 0;
8798  int cmd_state;
8799  int label;
8800  enum lex_state_e last_state;
8801  int fallthru = FALSE;
8802  int token_seen = p->token_seen;
8803 
8804  if (p->lex.strterm) {
8805  if (p->lex.strterm->flags & STRTERM_HEREDOC) {
8806  return here_document(p, &p->lex.strterm->u.heredoc);
8807  }
8808  else {
8809  token_flush(p);
8810  return parse_string(p, &p->lex.strterm->u.literal);
8811  }
8812  }
8813  cmd_state = p->command_start;
8814  p->command_start = FALSE;
8815  p->token_seen = TRUE;
8816  retry:
8817  last_state = p->lex.state;
8818 #ifndef RIPPER
8819  token_flush(p);
8820 #endif
8821  switch (c = nextc(p)) {
8822  case '\0': /* NUL */
8823  case '\004': /* ^D */
8824  case '\032': /* ^Z */
8825  case -1: /* end of script. */
8826  return 0;
8827 
8828  /* white spaces */
8829  case ' ': case '\t': case '\f': case '\r':
8830  case '\13': /* '\v' */
8831  space_seen = 1;
8832 #ifdef RIPPER
8833  while ((c = nextc(p))) {
8834  switch (c) {
8835  case ' ': case '\t': case '\f': case '\r':
8836  case '\13': /* '\v' */
8837  break;
8838  default:
8839  goto outofloop;
8840  }
8841  }
8842  outofloop:
8843  pushback(p, c);
8844  dispatch_scan_event(p, tSP);
8845 #endif
8846  goto retry;
8847 
8848  case '#': /* it's a comment */
8849  p->token_seen = token_seen;
8850  /* no magic_comment in shebang line */
8851  if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
8852  if (comment_at_top(p)) {
8853  set_file_encoding(p, p->lex.pcur, p->lex.pend);
8854  }
8855  }
8856  lex_goto_eol(p);
8857  dispatch_scan_event(p, tCOMMENT);
8858  fallthru = TRUE;
8859  /* fall through */
8860  case '\n':
8861  p->token_seen = token_seen;
8862  c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
8863  !IS_lex_state(EXPR_LABELED));
8864  if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
8865  if (!fallthru) {
8866  dispatch_scan_event(p, tIGNORED_NL);
8867  }
8868  fallthru = FALSE;
8869  if (!c && p->in_kwarg) {
8870  goto normal_newline;
8871  }
8872  goto retry;
8873  }
8874  while (1) {
8875  switch (c = nextc(p)) {
8876  case ' ': case '\t': case '\f': case '\r':
8877  case '\13': /* '\v' */
8878  space_seen = 1;
8879  break;
8880  case '#':
8881  pushback(p, c);
8882  if (space_seen) dispatch_scan_event(p, tSP);
8883  goto retry;
8884  case '&':
8885  case '.': {
8886  dispatch_delayed_token(p, tIGNORED_NL);
8887  if (peek(p, '.') == (c == '&')) {
8888  pushback(p, c);
8889  dispatch_scan_event(p, tSP);
8890  goto retry;
8891  }
8892  }
8893  default:
8894  p->ruby_sourceline--;
8895  p->lex.nextline = p->lex.lastline;
8896  case -1: /* EOF no decrement*/
8897 #ifndef RIPPER
8898  if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
8899  p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
8900  p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
8901  pushback(p, 1); /* always pushback */
8902  p->lex.ptok = p->lex.pcur;
8903 #else
8904  lex_goto_eol(p);
8905  if (c != -1) {
8906  p->lex.ptok = p->lex.pcur;
8907  }
8908 #endif
8909  goto normal_newline;
8910  }
8911  }
8912  normal_newline:
8913  p->command_start = TRUE;
8914  SET_LEX_STATE(EXPR_BEG);
8915  return '\n';
8916 
8917  case '*':
8918  if ((c = nextc(p)) == '*') {
8919  if ((c = nextc(p)) == '=') {
8920  set_yylval_id(idPow);
8921  SET_LEX_STATE(EXPR_BEG);
8922  return tOP_ASGN;
8923  }
8924  pushback(p, c);
8925  if (IS_SPCARG(c)) {
8926  rb_warning0("`**' interpreted as argument prefix");
8927  c = tDSTAR;
8928  }
8929  else if (IS_BEG()) {
8930  c = tDSTAR;
8931  }
8932  else {
8933  c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
8934  }
8935  }
8936  else {
8937  if (c == '=') {
8938  set_yylval_id('*');
8939  SET_LEX_STATE(EXPR_BEG);
8940  return tOP_ASGN;
8941  }
8942  pushback(p, c);
8943  if (IS_SPCARG(c)) {
8944  rb_warning0("`*' interpreted as argument prefix");
8945  c = tSTAR;
8946  }
8947  else if (IS_BEG()) {
8948  c = tSTAR;
8949  }
8950  else {
8951  c = warn_balanced('*', "*", "argument prefix");
8952  }
8953  }
8954  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8955  return c;
8956 
8957  case '!':
8958  c = nextc(p);
8959  if (IS_AFTER_OPERATOR()) {
8960  SET_LEX_STATE(EXPR_ARG);
8961  if (c == '@') {
8962  return '!';
8963  }
8964  }
8965  else {
8966  SET_LEX_STATE(EXPR_BEG);
8967  }
8968  if (c == '=') {
8969  return tNEQ;
8970  }
8971  if (c == '~') {
8972  return tNMATCH;
8973  }
8974  pushback(p, c);
8975  return '!';
8976 
8977  case '=':
8978  if (was_bol(p)) {
8979  /* skip embedded rd document */
8980  if (word_match_p(p, "begin", 5)) {
8981  int first_p = TRUE;
8982 
8983  lex_goto_eol(p);
8984  dispatch_scan_event(p, tEMBDOC_BEG);
8985  for (;;) {
8986  lex_goto_eol(p);
8987  if (!first_p) {
8988  dispatch_scan_event(p, tEMBDOC);
8989  }
8990  first_p = FALSE;
8991  c = nextc(p);
8992  if (c == -1) {
8993  compile_error(p, "embedded document meets end of file");
8994  return 0;
8995  }
8996  if (c == '=' && word_match_p(p, "end", 3)) {
8997  break;
8998  }
8999  pushback(p, c);
9000  }
9001  lex_goto_eol(p);
9002  dispatch_scan_event(p, tEMBDOC_END);
9003  goto retry;
9004  }
9005  }
9006 
9007  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9008  if ((c = nextc(p)) == '=') {
9009  if ((c = nextc(p)) == '=') {
9010  return tEQQ;
9011  }
9012  pushback(p, c);
9013  return tEQ;
9014  }
9015  if (c == '~') {
9016  return tMATCH;
9017  }
9018  else if (c == '>') {
9019  return tASSOC;
9020  }
9021  pushback(p, c);
9022  return '=';
9023 
9024  case '<':
9025  c = nextc(p);
9026  if (c == '<' &&
9027  !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9028  !IS_END() &&
9029  (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9030  int token = heredoc_identifier(p);
9031  if (token) return token < 0 ? 0 : token;
9032  }
9033  if (IS_AFTER_OPERATOR()) {
9034  SET_LEX_STATE(EXPR_ARG);
9035  }
9036  else {
9037  if (IS_lex_state(EXPR_CLASS))
9038  p->command_start = TRUE;
9039  SET_LEX_STATE(EXPR_BEG);
9040  }
9041  if (c == '=') {
9042  if ((c = nextc(p)) == '>') {
9043  return tCMP;
9044  }
9045  pushback(p, c);
9046  return tLEQ;
9047  }
9048  if (c == '<') {
9049  if ((c = nextc(p)) == '=') {
9050  set_yylval_id(idLTLT);
9051  SET_LEX_STATE(EXPR_BEG);
9052  return tOP_ASGN;
9053  }
9054  pushback(p, c);
9055  return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9056  }
9057  pushback(p, c);
9058  return '<';
9059 
9060  case '>':
9061  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9062  if ((c = nextc(p)) == '=') {
9063  return tGEQ;
9064  }
9065  if (c == '>') {
9066  if ((c = nextc(p)) == '=') {
9067  set_yylval_id(idGTGT);
9068  SET_LEX_STATE(EXPR_BEG);
9069  return tOP_ASGN;
9070  }
9071  pushback(p, c);
9072  return tRSHFT;
9073  }
9074  pushback(p, c);
9075  return '>';
9076 
9077  case '"':
9078  label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9079  p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9080  p->lex.ptok = p->lex.pcur-1;
9081  return tSTRING_BEG;
9082 
9083  case '`':
9084  if (IS_lex_state(EXPR_FNAME)) {
9085  SET_LEX_STATE(EXPR_ENDFN);
9086  return c;
9087  }
9088  if (IS_lex_state(EXPR_DOT)) {
9089  if (cmd_state)
9090  SET_LEX_STATE(EXPR_CMDARG);
9091  else
9092  SET_LEX_STATE(EXPR_ARG);
9093  return c;
9094  }
9095  p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9096  return tXSTRING_BEG;
9097 
9098  case '\'':
9099  label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9100  p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9101  p->lex.ptok = p->lex.pcur-1;
9102  return tSTRING_BEG;
9103 
9104  case '?':
9105  return parse_qmark(p, space_seen);
9106 
9107  case '&':
9108  if ((c = nextc(p)) == '&') {
9109  SET_LEX_STATE(EXPR_BEG);
9110  if ((c = nextc(p)) == '=') {
9111  set_yylval_id(idANDOP);
9112  SET_LEX_STATE(EXPR_BEG);
9113  return tOP_ASGN;
9114  }
9115  pushback(p, c);
9116  return tANDOP;
9117  }
9118  else if (c == '=') {
9119  set_yylval_id('&');
9120  SET_LEX_STATE(EXPR_BEG);
9121  return tOP_ASGN;
9122  }
9123  else if (c == '.') {
9124  set_yylval_id(idANDDOT);
9125  SET_LEX_STATE(EXPR_DOT);
9126  return tANDDOT;
9127  }
9128  pushback(p, c);
9129  if (IS_SPCARG(c)) {
9130  if ((c != ':') ||
9131  (c = peekc_n(p, 1)) == -1 ||
9132  !(c == '\'' || c == '"' ||
9133  is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9134  rb_warning0("`&' interpreted as argument prefix");
9135  }
9136  c = tAMPER;
9137  }
9138  else if (IS_BEG()) {
9139  c = tAMPER;
9140  }
9141  else {
9142  c = warn_balanced('&', "&", "argument prefix");
9143  }
9144  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9145  return c;
9146 
9147  case '|':
9148  if ((c = nextc(p)) == '|') {
9149  SET_LEX_STATE(EXPR_BEG);
9150  if ((c = nextc(p)) == '=') {
9151  set_yylval_id(idOROP);
9152  SET_LEX_STATE(EXPR_BEG);
9153  return tOP_ASGN;
9154  }
9155  pushback(p, c);
9156  if (IS_lex_state_for(last_state, EXPR_BEG)) {
9157  c = '|';
9158  pushback(p, '|');
9159  return c;
9160  }
9161  return tOROP;
9162  }
9163  if (c == '=') {
9164  set_yylval_id('|');
9165  SET_LEX_STATE(EXPR_BEG);
9166  return tOP_ASGN;
9167  }
9168  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9169  pushback(p, c);
9170  return '|';
9171 
9172  case '+':
9173  c = nextc(p);
9174  if (IS_AFTER_OPERATOR()) {
9175  SET_LEX_STATE(EXPR_ARG);
9176  if (c == '@') {
9177  return tUPLUS;
9178  }
9179  pushback(p, c);
9180  return '+';
9181  }
9182  if (c == '=') {
9183  set_yylval_id('+');
9184  SET_LEX_STATE(EXPR_BEG);
9185  return tOP_ASGN;
9186  }
9187  if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9188  SET_LEX_STATE(EXPR_BEG);
9189  pushback(p, c);
9190  if (c != -1 && ISDIGIT(c)) {
9191  return parse_numeric(p, '+');
9192  }
9193  return tUPLUS;
9194  }
9195  SET_LEX_STATE(EXPR_BEG);
9196  pushback(p, c);
9197  return warn_balanced('+', "+", "unary operator");
9198 
9199  case '-':
9200  c = nextc(p);
9201  if (IS_AFTER_OPERATOR()) {
9202  SET_LEX_STATE(EXPR_ARG);
9203  if (c == '@') {
9204  return tUMINUS;
9205  }
9206  pushback(p, c);
9207  return '-';
9208  }
9209  if (c == '=') {
9210  set_yylval_id('-');
9211  SET_LEX_STATE(EXPR_BEG);
9212  return tOP_ASGN;
9213  }
9214  if (c == '>') {
9215  SET_LEX_STATE(EXPR_ENDFN);
9216  return tLAMBDA;
9217  }
9218  if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9219  SET_LEX_STATE(EXPR_BEG);
9220  pushback(p, c);
9221  if (c != -1 && ISDIGIT(c)) {
9222  return tUMINUS_NUM;
9223  }
9224  return tUMINUS;
9225  }
9226  SET_LEX_STATE(EXPR_BEG);
9227  pushback(p, c);
9228  return warn_balanced('-', "-", "unary operator");
9229 
9230  case '.': {
9231  int is_beg = IS_BEG();
9232  SET_LEX_STATE(EXPR_BEG);
9233  if ((c = nextc(p)) == '.') {
9234  if ((c = nextc(p)) == '.') {
9235  if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9236  rb_warn0("... at EOL, should be parenthesized?");
9237  }
9238  return is_beg ? tBDOT3 : tDOT3;
9239  }
9240  pushback(p, c);
9241  return is_beg ? tBDOT2 : tDOT2;
9242  }
9243  pushback(p, c);
9244  if (c != -1 && ISDIGIT(c)) {
9245  char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9246  parse_numeric(p, '.');
9247  if (ISDIGIT(prev)) {
9248  yyerror0("unexpected fraction part after numeric literal");
9249  }
9250  else {
9251  yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9252  }
9253  SET_LEX_STATE(EXPR_END);
9254  p->lex.ptok = p->lex.pcur;
9255  goto retry;
9256  }
9257  set_yylval_id('.');
9258  SET_LEX_STATE(EXPR_DOT);
9259  return '.';
9260  }
9261 
9262  case '0': case '1': case '2': case '3': case '4':
9263  case '5': case '6': case '7': case '8': case '9':
9264  return parse_numeric(p, c);
9265 
9266  case ')':
9267  COND_POP();
9268  CMDARG_POP();
9269  SET_LEX_STATE(EXPR_ENDFN);
9270  p->lex.paren_nest--;
9271  return c;
9272 
9273  case ']':
9274  COND_POP();
9275  CMDARG_POP();
9276  SET_LEX_STATE(EXPR_END);
9277  p->lex.paren_nest--;
9278  return c;
9279 
9280  case '}':
9281  /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9282  if (!p->lex.brace_nest--) return tSTRING_DEND;
9283  COND_POP();
9284  CMDARG_POP();
9285  SET_LEX_STATE(EXPR_END);
9286  p->lex.paren_nest--;
9287  return c;
9288 
9289  case ':':
9290  c = nextc(p);
9291  if (c == ':') {
9292  if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9293  SET_LEX_STATE(EXPR_BEG);
9294  return tCOLON3;
9295  }
9296  set_yylval_id(idCOLON2);
9297  SET_LEX_STATE(EXPR_DOT);
9298  return tCOLON2;
9299  }
9300  if (IS_END() || ISSPACE(c) || c == '#') {
9301  pushback(p, c);
9302  c = warn_balanced(':', ":", "symbol literal");
9303  SET_LEX_STATE(EXPR_BEG);
9304  return c;
9305  }
9306  switch (c) {
9307  case '\'':
9308  p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9309  break;
9310  case '"':
9311  p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9312  break;
9313  default:
9314  pushback(p, c);
9315  break;
9316  }
9317  SET_LEX_STATE(EXPR_FNAME);
9318  return tSYMBEG;
9319 
9320  case '/':
9321  if (IS_BEG()) {
9322  p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9323  return tREGEXP_BEG;
9324  }
9325  if ((c = nextc(p)) == '=') {
9326  set_yylval_id('/');
9327  SET_LEX_STATE(EXPR_BEG);
9328  return tOP_ASGN;
9329  }
9330  pushback(p, c);
9331  if (IS_SPCARG(c)) {
9332  arg_ambiguous(p, '/');
9333  p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9334  return tREGEXP_BEG;
9335  }
9336  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9337  return warn_balanced('/', "/", "regexp literal");
9338 
9339  case '^':
9340  if ((c = nextc(p)) == '=') {
9341  set_yylval_id('^');
9342  SET_LEX_STATE(EXPR_BEG);
9343  return tOP_ASGN;
9344  }
9345  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9346  pushback(p, c);
9347  return '^';
9348 
9349  case ';':
9350  SET_LEX_STATE(EXPR_BEG);
9351  p->command_start = TRUE;
9352  return ';';
9353 
9354  case ',':
9355  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9356  return ',';
9357 
9358  case '~':
9359  if (IS_AFTER_OPERATOR()) {
9360  if ((c = nextc(p)) != '@') {
9361  pushback(p, c);
9362  }
9363  SET_LEX_STATE(EXPR_ARG);
9364  }
9365  else {
9366  SET_LEX_STATE(EXPR_BEG);
9367  }
9368  return '~';
9369 
9370  case '(':
9371  if (IS_BEG()) {
9372  c = tLPAREN;
9373  }
9374  else if (!space_seen) {
9375  /* foo( ... ) => method call, no ambiguity */
9376  }
9377  else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9378  c = tLPAREN_ARG;
9379  }
9380  else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9381  rb_warning0("parentheses after method name is interpreted as "
9382  "an argument list, not a decomposed argument");
9383  }
9384  p->lex.paren_nest++;
9385  COND_PUSH(0);
9386  CMDARG_PUSH(0);
9387  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9388  return c;
9389 
9390  case '[':
9391  p->lex.paren_nest++;
9392  if (IS_AFTER_OPERATOR()) {
9393  if ((c = nextc(p)) == ']') {
9394  SET_LEX_STATE(EXPR_ARG);
9395  if ((c = nextc(p)) == '=') {
9396  return tASET;
9397  }
9398  pushback(p, c);
9399  return tAREF;
9400  }
9401  pushback(p, c);
9402  SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9403  return '[';
9404  }
9405  else if (IS_BEG()) {
9406  c = tLBRACK;
9407  }
9408  else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9409  c = tLBRACK;
9410  }
9411  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9412  COND_PUSH(0);
9413  CMDARG_PUSH(0);
9414  return c;
9415 
9416  case '{':
9417  ++p->lex.brace_nest;
9418  if (lambda_beginning_p())
9419  c = tLAMBEG;
9420  else if (IS_lex_state(EXPR_LABELED))
9421  c = tLBRACE; /* hash */
9422  else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9423  c = '{'; /* block (primary) */
9424  else if (IS_lex_state(EXPR_ENDARG))
9425  c = tLBRACE_ARG; /* block (expr) */
9426  else
9427  c = tLBRACE; /* hash */
9428  if (c != tLBRACE) {
9429  p->command_start = TRUE;
9430  SET_LEX_STATE(EXPR_BEG);
9431  }
9432  else {
9433  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9434  }
9435  ++p->lex.paren_nest; /* after lambda_beginning_p() */
9436  COND_PUSH(0);
9437  CMDARG_PUSH(0);
9438  return c;
9439 
9440  case '\\':
9441  c = nextc(p);
9442  if (c == '\n') {
9443  space_seen = 1;
9444  dispatch_scan_event(p, tSP);
9445  goto retry; /* skip \\n */
9446  }
9447  if (c == ' ') return tSP;
9448  if (ISSPACE(c)) return c;
9449  pushback(p, c);
9450  return '\\';
9451 
9452  case '%':
9453  return parse_percent(p, space_seen, last_state);
9454 
9455  case '$':
9456  return parse_gvar(p, last_state);
9457 
9458  case '@':
9459  return parse_atmark(p, last_state);
9460 
9461  case '_':
9462  if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9463  p->ruby__end__seen = 1;
9464  p->eofp = 1;
9465 #ifndef RIPPER
9466  return -1;
9467 #else
9468  lex_goto_eol(p);
9469  dispatch_scan_event(p, k__END__);
9470  return 0;
9471 #endif
9472  }
9473  newtok(p);
9474  break;
9475 
9476  default:
9477  if (!parser_is_identchar(p)) {
9478  compile_error(p, "Invalid char `\\x%02X' in expression", c);
9479  token_flush(p);
9480  goto retry;
9481  }
9482 
9483  newtok(p);
9484  break;
9485  }
9486 
9487  return parse_ident(p, c, cmd_state);
9488 }
9489 
9490 static enum yytokentype
9491 yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9492 {
9493  enum yytokentype t;
9494 
9495  p->lval = lval;
9496  lval->val = Qundef;
9497  t = parser_yylex(p);
9498  if (has_delayed_token(p))
9499  dispatch_delayed_token(p, t);
9500  else if (t != 0)
9501  dispatch_scan_event(p, t);
9502 
9503  if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9504  RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9505  else
9506  RUBY_SET_YYLLOC(*yylloc);
9507 
9508  return t;
9509 }
9510 
9511 #define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9512 
9513 static NODE*
9514 node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9515 {
9516  NODE *n = rb_ast_newnode(p->ast, type);
9517 
9518  rb_node_init(n, type, a0, a1, a2);
9519 
9520  nd_set_loc(n, loc);
9521  nd_set_node_id(n, parser_get_node_id(p));
9522  return n;
9523 }
9524 
9525 static NODE *
9526 nd_set_loc(NODE *nd, const YYLTYPE *loc)
9527 {
9528  nd->nd_loc = *loc;
9529  nd_set_line(nd, loc->beg_pos.lineno);
9530  return nd;
9531 }
9532 
9533 #ifndef RIPPER
9534 static enum node_type
9535 nodetype(NODE *node) /* for debug */
9536 {
9537  return (enum node_type)nd_type(node);
9538 }
9539 
9540 static int
9541 nodeline(NODE *node)
9542 {
9543  return nd_line(node);
9544 }
9545 
9546 static NODE*
9547 newline_node(NODE *node)
9548 {
9549  if (node) {
9550  node = remove_begin(node);
9551  node->flags |= NODE_FL_NEWLINE;
9552  }
9553  return node;
9554 }
9555 
9556 static void
9557 fixpos(NODE *node, NODE *orig)
9558 {
9559  if (!node) return;
9560  if (!orig) return;
9561  nd_set_line(node, nd_line(orig));
9562 }
9563 
9564 static void
9565 parser_warning(struct parser_params *p, NODE *node, const char *mesg)
9566 {
9567  rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9568 }
9569 
9570 static void
9571 parser_warn(struct parser_params *p, NODE *node, const char *mesg)
9572 {
9573  rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9574 }
9575 
9576 static NODE*
9577 block_append(struct parser_params *p, NODE *head, NODE *tail)
9578 {
9579  NODE *end, *h = head, *nd;
9580 
9581  if (tail == 0) return head;
9582 
9583  if (h == 0) return tail;
9584  switch (nd_type(h)) {
9585  case NODE_LIT:
9586  case NODE_STR:
9587  case NODE_SELF:
9588  case NODE_TRUE:
9589  case NODE_FALSE:
9590  case NODE_NIL:
9591  parser_warning(p, h, "unused literal ignored");
9592  return tail;
9593  default:
9594  h = end = NEW_BLOCK(head, &head->nd_loc);
9595  end->nd_end = end;
9596  head = end;
9597  break;
9598  case NODE_BLOCK:
9599  end = h->nd_end;
9600  break;
9601  }
9602 
9603  nd = end->nd_head;
9604  switch (nd_type(nd)) {
9605  case NODE_RETURN:
9606  case NODE_BREAK:
9607  case NODE_NEXT:
9608  case NODE_REDO:
9609  case NODE_RETRY:
9610  if (RTEST(ruby_verbose)) {
9611  parser_warning(p, tail, "statement not reached");
9612  }
9613  break;
9614 
9615  default:
9616  break;
9617  }
9618 
9619  if (nd_type(tail) != NODE_BLOCK) {
9620  tail = NEW_BLOCK(tail, &tail->nd_loc);
9621  tail->nd_end = tail;
9622  }
9623  end->nd_next = tail;
9624  h->nd_end = tail->nd_end;
9625  nd_set_last_loc(head, nd_last_loc(tail));
9626  return head;
9627 }
9628 
9629 /* append item to the list */
9630 static NODE*
9631 list_append(struct parser_params *p, NODE *list, NODE *item)
9632 {
9633  NODE *last;
9634 
9635  if (list == 0) return NEW_LIST(item, &item->nd_loc);
9636  if (list->nd_next) {
9637  last = list->nd_next->nd_end;
9638  }
9639  else {
9640  last = list;
9641  }
9642 
9643  list->nd_alen += 1;
9644  last->nd_next = NEW_LIST(item, &item->nd_loc);
9645  list->nd_next->nd_end = last->nd_next;
9646 
9647  nd_set_last_loc(list, nd_last_loc(item));
9648 
9649  return list;
9650 }
9651 
9652 /* concat two lists */
9653 static NODE*
9654 list_concat(NODE *head, NODE *tail)
9655 {
9656  NODE *last;
9657 
9658  if (head->nd_next) {
9659  last = head->nd_next->nd_end;
9660  }
9661  else {
9662  last = head;
9663  }
9664 
9665  head->nd_alen += tail->nd_alen;
9666  last->nd_next = tail;
9667  if (tail->nd_next) {
9668  head->nd_next->nd_end = tail->nd_next->nd_end;
9669  }
9670  else {
9671  head->nd_next->nd_end = tail;
9672  }
9673 
9674  nd_set_last_loc(head, nd_last_loc(tail));
9675 
9676  return head;
9677 }
9678 
9679 static int
9680 literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
9681 {
9682  if (NIL_P(tail)) return 1;
9683  if (!rb_enc_compatible(head, tail)) {
9684  compile_error(p, "string literal encodings differ (%s / %s)",
9685  rb_enc_name(rb_enc_get(head)),
9686  rb_enc_name(rb_enc_get(tail)));
9687  rb_str_resize(head, 0);
9688  rb_str_resize(tail, 0);
9689  return 0;
9690  }
9691  rb_str_buf_append(head, tail);
9692  return 1;
9693 }
9694 
9695 /* concat two string literals */
9696 static NODE *
9697 literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
9698 {
9699  enum node_type htype;
9700  NODE *headlast;
9701  VALUE lit;
9702 
9703  if (!head) return tail;
9704  if (!tail) return head;
9705 
9706  htype = nd_type(head);
9707  if (htype == NODE_EVSTR) {
9708  NODE *node = NEW_DSTR(STR_NEW0(), loc);
9709  RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9710  head = list_append(p, node, head);
9711  htype = NODE_DSTR;
9712  }
9713  if (p->heredoc_indent > 0) {
9714  switch (htype) {
9715  case NODE_STR:
9716  nd_set_type(head, NODE_DSTR);
9717  case NODE_DSTR:
9718  return list_append(p, head, tail);
9719  default:
9720  break;
9721  }
9722  }
9723  switch (nd_type(tail)) {
9724  case NODE_STR:
9725  if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9726  nd_type(headlast) == NODE_STR) {
9727  htype = NODE_STR;
9728  lit = headlast->nd_lit;
9729  }
9730  else {
9731  lit = head->nd_lit;
9732  }
9733  if (htype == NODE_STR) {
9734  if (!literal_concat0(p, lit, tail->nd_lit)) {
9735  error:
9736  rb_discard_node(p, head);
9737  rb_discard_node(p, tail);
9738  return 0;
9739  }
9740  rb_discard_node(p, tail);
9741  }
9742  else {
9743  list_append(p, head, tail);
9744  }
9745  break;
9746 
9747  case NODE_DSTR:
9748  if (htype == NODE_STR) {
9749  if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
9750  goto error;
9751  tail->nd_lit = head->nd_lit;
9752  rb_discard_node(p, head);
9753  head = tail;
9754  }
9755  else if (NIL_P(tail->nd_lit)) {
9756  append:
9757  head->nd_alen += tail->nd_alen - 1;
9758  head->nd_next->nd_end->nd_next = tail->nd_next;
9759  head->nd_next->nd_end = tail->nd_next->nd_end;
9760  rb_discard_node(p, tail);
9761  }
9762  else if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9763  nd_type(headlast) == NODE_STR) {
9764  lit = headlast->nd_lit;
9765  if (!literal_concat0(p, lit, tail->nd_lit))
9766  goto error;
9767  tail->nd_lit = Qnil;
9768  goto append;
9769  }
9770  else {
9771  list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
9772  }
9773  break;
9774 
9775  case NODE_EVSTR:
9776  if (htype == NODE_STR) {
9777  nd_set_type(head, NODE_DSTR);
9778  head->nd_alen = 1;
9779  }
9780  list_append(p, head, tail);
9781  break;
9782  }
9783  return head;
9784 }
9785 
9786 static NODE *
9787 evstr2dstr(struct parser_params *p, NODE *node)
9788 {
9789  if (nd_type(node) == NODE_EVSTR) {
9790  NODE * dstr = NEW_DSTR(STR_NEW0(), &node->nd_loc);
9791  RB_OBJ_WRITTEN(p->ast, Qnil, dstr->nd_lit);
9792  node = list_append(p, dstr, node);
9793  }
9794  return node;
9795 }
9796 
9797 static NODE *
9798 new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
9799 {
9800  NODE *head = node;
9801 
9802  if (node) {
9803  switch (nd_type(node)) {
9804  case NODE_STR: case NODE_DSTR: case NODE_EVSTR:
9805  return node;
9806  }
9807  }
9808  return NEW_EVSTR(head, loc);
9809 }
9810 
9811 static NODE *
9812 call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
9813  const YYLTYPE *op_loc, const YYLTYPE *loc)
9814 {
9815  NODE *expr;
9816  value_expr(recv);
9817  value_expr(arg1);
9818  expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
9819  nd_set_line(expr, op_loc->beg_pos.lineno);
9820  return expr;
9821 }
9822 
9823 static NODE *
9824 call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
9825 {
9826  NODE *opcall;
9827  value_expr(recv);
9828  opcall = NEW_OPCALL(recv, id, 0, loc);
9829  nd_set_line(opcall, op_loc->beg_pos.lineno);
9830  return opcall;
9831 }
9832 
9833 static NODE *
9834 new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
9835 {
9836  NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
9837  nd_set_line(qcall, op_loc->beg_pos.lineno);
9838  return qcall;
9839 }
9840 
9841 static NODE*
9842 new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
9843 {
9844  NODE *ret;
9845  if (block) block_dup_check(p, args, block);
9846  ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
9847  if (block) ret = method_add_block(p, ret, block, loc);
9848  fixpos(ret, recv);
9849  return ret;
9850 }
9851 
9852 #define nd_once_body(node) (nd_type(node) == NODE_ONCE ? (node)->nd_body : node)
9853 static NODE*
9854 match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
9855 {
9856  NODE *n;
9857  int line = op_loc->beg_pos.lineno;
9858 
9859  value_expr(node1);
9860  value_expr(node2);
9861  if (node1 && (n = nd_once_body(node1)) != 0) {
9862  switch (nd_type(n)) {
9863  case NODE_DREGX:
9864  {
9865  NODE *match = NEW_MATCH2(node1, node2, loc);
9866  nd_set_line(match, line);
9867  return match;
9868  }
9869 
9870  case NODE_LIT:
9871  if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
9872  const VALUE lit = n->nd_lit;
9873  NODE *match = NEW_MATCH2(node1, node2, loc);
9874  match->nd_args = reg_named_capture_assign(p, lit, loc);
9875  nd_set_line(match, line);
9876  return match;
9877  }
9878  }
9879  }
9880 
9881  if (node2 && (n = nd_once_body(node2)) != 0) {
9882  NODE *match3;
9883 
9884  switch (nd_type(n)) {
9885  case NODE_LIT:
9886  if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
9887  /* fallthru */
9888  case NODE_DREGX:
9889  match3 = NEW_MATCH3(node2, node1, loc);
9890  return match3;
9891  }
9892  }
9893 
9894  n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
9895  nd_set_line(n, line);
9896  return n;
9897 }
9898 
9899 # if WARN_PAST_SCOPE
9900 static int
9901 past_dvar_p(struct parser_params *p, ID id)
9902 {
9903  struct vtable *past = p->lvtbl->past;
9904  while (past) {
9905  if (vtable_included(past, id)) return 1;
9906  past = past->prev;
9907  }
9908  return 0;
9909 }
9910 # endif
9911 
9912 /* As Ripper#warn does not have arguments for the location, so the
9913  * following messages cannot be separated */
9914 #define WARN_LOCATION(type) do { \
9915  if (p->warn_location) { \
9916  int line; \
9917  VALUE file = rb_source_location(&line); \
9918  rb_warn3(type" in eval may not return location in binding;" \
9919  " use Binding#source_location instead\n" \
9920  "%"PRIsWARN":%d: warning: in `%"PRIsWARN"'", \
9921  file, WARN_I(line), rb_id2str(rb_frame_this_func())); \
9922  } \
9923 } while (0)
9924 
9925 static int
9926 numparam_nested_p(struct parser_params *p)
9927 {
9928  struct local_vars *local = p->lvtbl;
9929  NODE *outer = local->numparam.outer;
9930  NODE *inner = local->numparam.inner;
9931  if (outer || inner) {
9932  NODE *used = outer ? outer : inner;
9933  compile_error(p, "numbered parameter is already used in\n"
9934  "%s:%d: %s block here",
9935  p->ruby_sourcefile, nd_line(used),
9936  outer ? "outer" : "inner");
9937  parser_show_error_line(p, &used->nd_loc);
9938  return 1;
9939  }
9940  return 0;
9941 }
9942 
9943 static NODE*
9944 gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
9945 {
9946  ID *vidp = NULL;
9947  NODE *node;
9948  switch (id) {
9949  case keyword_self:
9950  return NEW_SELF(loc);
9951  case keyword_nil:
9952  return NEW_NIL(loc);
9953  case keyword_true:
9954  return NEW_TRUE(loc);
9955  case keyword_false:
9956  return NEW_FALSE(loc);
9957  case keyword__FILE__:
9958  WARN_LOCATION("__FILE__");
9959  {
9960  VALUE file = p->ruby_sourcefile_string;
9961  if (NIL_P(file))
9962  file = rb_str_new(0, 0);
9963  else
9964  file = rb_str_dup(file);
9965  node = NEW_STR(file, loc);
9966  RB_OBJ_WRITTEN(p->ast, Qnil, file);
9967  }
9968  return node;
9969  case keyword__LINE__:
9970  WARN_LOCATION("__LINE__");
9971  return NEW_LIT(INT2FIX(p->tokline), loc);
9972  case keyword__ENCODING__:
9973  node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
9974  RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9975  return node;
9976 
9977  }
9978  switch (id_type(id)) {
9979  case ID_LOCAL:
9980  if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
9981  if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
9982  if (id == p->cur_arg) {
9983  compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
9984  return 0;
9985  }
9986  if (vidp) *vidp |= LVAR_USED;
9987  node = NEW_DVAR(id, loc);
9988  return node;
9989  }
9990  if (local_id_ref(p, id, &vidp)) {
9991  if (id == p->cur_arg) {
9992  compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
9993  return 0;
9994  }
9995  if (vidp) *vidp |= LVAR_USED;
9996  node = NEW_LVAR(id, loc);
9997  return node;
9998  }
9999  if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
10000  parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
10001  if (numparam_nested_p(p)) return 0;
10002  node = NEW_DVAR(id, loc);
10003  struct local_vars *local = p->lvtbl;
10004  if (!local->numparam.current) local->numparam.current = node;
10005  return node;
10006  }
10007 # if WARN_PAST_SCOPE
10008  if (!p->in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10009  rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10010  }
10011 # endif
10012  /* method call without arguments */
10013  return NEW_VCALL(id, loc);
10014  case ID_GLOBAL:
10015  return NEW_GVAR(id, loc);
10016  case ID_INSTANCE:
10017  return NEW_IVAR(id, loc);
10018  case ID_CONST:
10019  return NEW_CONST(id, loc);
10020  case ID_CLASS:
10021  return NEW_CVAR(id, loc);
10022  }
10023  compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10024  return 0;
10025 }
10026 
10027 static NODE *
10028 opt_arg_append(NODE *opt_list, NODE *opt)
10029 {
10030  NODE *opts = opt_list;
10031  opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10032 
10033  while (opts->nd_next) {
10034  opts = opts->nd_next;
10035  opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10036  }
10037  opts->nd_next = opt;
10038 
10039  return opt_list;
10040 }
10041 
10042 static NODE *
10043 kwd_append(NODE *kwlist, NODE *kw)
10044 {
10045  if (kwlist) {
10046  NODE *kws = kwlist;
10047  kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10048  while (kws->nd_next) {
10049  kws = kws->nd_next;
10050  kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10051  }
10052  kws->nd_next = kw;
10053  }
10054  return kwlist;
10055 }
10056 
10057 static NODE *
10058 new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10059 {
10060  return NEW_DEFINED(remove_begin_all(expr), loc);
10061 }
10062 
10063 static NODE*
10064 symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10065 {
10066  if (nd_type(symbol) == NODE_DSTR) {
10067  nd_set_type(symbol, NODE_DSYM);
10068  }
10069  else {
10070  nd_set_type(symbol, NODE_LIT);
10071  RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10072  }
10073  return list_append(p, symbols, symbol);
10074 }
10075 
10076 static NODE *
10077 new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10078 {
10079  NODE *list, *prev;
10080  VALUE lit;
10081 
10082  if (!node) {
10083  node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10084  RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10085  return node;
10086  }
10087  switch (nd_type(node)) {
10088  case NODE_STR:
10089  {
10090  VALUE src = node->nd_lit;
10091  nd_set_type(node, NODE_LIT);
10092  nd_set_loc(node, loc);
10093  RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10094  }
10095  break;
10096  default:
10097  lit = STR_NEW0();
10098  node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10099  RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10100  /* fall through */
10101  case NODE_DSTR:
10102  nd_set_type(node, NODE_DREGX);
10103  nd_set_loc(node, loc);
10104  node->nd_cflag = options & RE_OPTION_MASK;
10105  if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10106  for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10107  if (nd_type(list->nd_head) == NODE_STR) {
10108  VALUE tail = list->nd_head->nd_lit;
10109  if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10110  VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10111  if (!literal_concat0(p, lit, tail)) {
10112  return NEW_NIL(loc); /* dummy node on error */
10113  }
10114  rb_str_resize(tail, 0);
10115  prev->nd_next = list->nd_next;
10116  rb_discard_node(p, list->nd_head);
10117  rb_discard_node(p, list);
10118  list = prev;
10119  }
10120  else {
10121  prev = list;
10122  }
10123  }
10124  else {
10125  prev = 0;
10126  }
10127  }
10128  if (!node->nd_next) {
10129  VALUE src = node->nd_lit;
10130  nd_set_type(node, NODE_LIT);
10131  RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10132  }
10133  if (options & RE_OPTION_ONCE) {
10134  node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10135  }
10136  break;
10137  }
10138  return node;
10139 }
10140 
10141 static NODE *
10142 new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10143 {
10144  if (!k) return 0;
10145  return NEW_KW_ARG(0, (k), loc);
10146 }
10147 
10148 static NODE *
10149 new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10150 {
10151  if (!node) {
10152  VALUE lit = STR_NEW0();
10153  NODE *xstr = NEW_XSTR(lit, loc);
10154  RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10155  return xstr;
10156  }
10157  switch (nd_type(node)) {
10158  case NODE_STR:
10159  nd_set_type(node, NODE_XSTR);
10160  nd_set_loc(node, loc);
10161  break;
10162  case NODE_DSTR:
10163  nd_set_type(node, NODE_DXSTR);
10164  nd_set_loc(node, loc);
10165  break;
10166  default:
10167  node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10168  break;
10169  }
10170  return node;
10171 }
10172 
10173 static void
10174 check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10175 {
10176  VALUE lit;
10177 
10178  if (!arg || !p->case_labels) return;
10179 
10180  lit = rb_node_case_when_optimizable_literal(arg);
10181  if (lit == Qundef) return;
10182  if (nd_type(arg) == NODE_STR) {
10183  RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10184  }
10185 
10186  if (NIL_P(p->case_labels)) {
10187  p->case_labels = rb_obj_hide(rb_hash_new());
10188  }
10189  else {
10190  VALUE line = rb_hash_lookup(p->case_labels, lit);
10191  if (!NIL_P(line)) {
10192  rb_warning1("duplicated `when' clause with line %d is ignored",
10193  WARN_IVAL(line));
10194  return;
10195  }
10196  }
10197  rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10198 }
10199 
10200 #else /* !RIPPER */
10201 static int
10202 id_is_var(struct parser_params *p, ID id)
10203 {
10204  if (is_notop_id(id)) {
10205  switch (id & ID_SCOPE_MASK) {
10206  case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10207  return 1;
10208  case ID_LOCAL:
10209  if (dyna_in_block(p)) {
10210  if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10211  }
10212  if (local_id(p, id)) return 1;
10213  /* method call without arguments */
10214  return 0;
10215  }
10216  }
10217  compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10218  return 0;
10219 }
10220 
10221 static VALUE
10222 new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10223 {
10224  VALUE src = 0, err;
10225  int options = 0;
10226  if (ripper_is_node_yylval(re)) {
10227  src = RNODE(re)->nd_cval;
10228  re = RNODE(re)->nd_rval;
10229  }
10230  if (ripper_is_node_yylval(opt)) {
10231  options = (int)RNODE(opt)->nd_tag;
10232  opt = RNODE(opt)->nd_rval;
10233  }
10234  if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10235  compile_error(p, "%"PRIsVALUE, err);
10236  }
10237  return dispatch2(regexp_literal, re, opt);
10238 }
10239 #endif /* !RIPPER */
10240 
10241 
10242 #ifndef RIPPER
10243 static const char rb_parser_lex_state_names[][8] = {
10244  "BEG", "END", "ENDARG", "ENDFN", "ARG",
10245  "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10246  "LABEL", "LABELED","FITEM",
10247 };
10248 
10249 static VALUE
10250 append_lex_state_name(enum lex_state_e state, VALUE buf)
10251 {
10252  int i, sep = 0;
10253  unsigned int mask = 1;
10254  static const char none[] = "NONE";
10255 
10256  for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10257  if ((unsigned)state & mask) {
10258  if (sep) {
10259  rb_str_cat(buf, "|", 1);
10260  }
10261  sep = 1;
10262  rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10263  }
10264  }
10265  if (!sep) {
10266  rb_str_cat(buf, none, sizeof(none)-1);
10267  }
10268  return buf;
10269 }
10270 
10271 static void
10272 flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10273 {
10274  VALUE mesg = p->debug_buffer;
10275 
10276  if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10277  p->debug_buffer = Qnil;
10278  rb_io_puts(1, &mesg, out);
10279  }
10280  if (!NIL_P(str) && RSTRING_LEN(str)) {
10281  rb_io_write(p->debug_output, str);
10282  }
10283 }
10284 
10285 enum lex_state_e
10286 rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10287  enum lex_state_e to, int line)
10288 {
10289  VALUE mesg;
10290  mesg = rb_str_new_cstr("lex_state: ");
10291  append_lex_state_name(from, mesg);
10292  rb_str_cat_cstr(mesg, " -> ");
10293  append_lex_state_name(to, mesg);
10294  rb_str_catf(mesg, " at line %d\n", line);
10295  flush_debug_buffer(p, p->debug_output, mesg);
10296  return to;
10297 }
10298 
10299 VALUE
10300 rb_parser_lex_state_name(enum lex_state_e state)
10301 {
10302  return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10303 }
10304 
10305 static void
10306 append_bitstack_value(stack_type stack, VALUE mesg)
10307 {
10308  if (stack == 0) {
10309  rb_str_cat_cstr(mesg, "0");
10310  }
10311  else {
10312  stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10313  for (; mask && !(stack & mask); mask >>= 1) continue;
10314  for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10315  }
10316 }
10317 
10318 void
10319 rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10320  const char *name, int line)
10321 {
10322  VALUE mesg = rb_sprintf("%s: ", name);
10323  append_bitstack_value(stack, mesg);
10324  rb_str_catf(mesg, " at line %d\n", line);
10325  flush_debug_buffer(p, p->debug_output, mesg);
10326 }
10327 
10328 void
10329 rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10330 {
10331  va_list ap;
10332  VALUE mesg = rb_str_new_cstr("internal parser error: ");
10333 
10334  va_start(ap, fmt);
10335  rb_str_vcatf(mesg, fmt, ap);
10336  va_end(ap);
10337  parser_yyerror(p, NULL, RSTRING_PTR(mesg));
10338  RB_GC_GUARD(mesg);
10339 
10340  mesg = rb_str_new(0, 0);
10341  append_lex_state_name(p->lex.state, mesg);
10342  compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10343  rb_str_resize(mesg, 0);
10344  append_bitstack_value(p->cond_stack, mesg);
10345  compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10346  rb_str_resize(mesg, 0);
10347  append_bitstack_value(p->cmdarg_stack, mesg);
10348  compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10349  if (p->debug_output == rb_stdout)
10350  p->debug_output = rb_stderr;
10351  p->debug = TRUE;
10352 }
10353 
10354 YYLTYPE *
10355 rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10356 {
10357  int sourceline = here->sourceline;
10358  int beg_pos = (int)here->offset - here->quote
10359  - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10360  int end_pos = (int)here->offset + here->length + here->quote;
10361 
10362  yylloc->beg_pos.lineno = sourceline;
10363  yylloc->beg_pos.column = beg_pos;
10364  yylloc->end_pos.lineno = sourceline;
10365  yylloc->end_pos.column = end_pos;
10366  return yylloc;
10367 }
10368 
10369 YYLTYPE *
10370 rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10371 {
10372  yylloc->beg_pos.lineno = p->ruby_sourceline;
10373  yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10374  yylloc->end_pos.lineno = p->ruby_sourceline;
10375  yylloc->end_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10376  return yylloc;
10377 }
10378 
10379 YYLTYPE *
10380 rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10381 {
10382  yylloc->beg_pos.lineno = p->ruby_sourceline;
10383  yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10384  yylloc->end_pos.lineno = p->ruby_sourceline;
10385  yylloc->end_pos.column = (int)(p->lex.pcur - p->lex.pbeg);
10386  return yylloc;
10387 }
10388 #endif /* !RIPPER */
10389 
10390 static void
10391 parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp)
10392 {
10393  VALUE v;
10394 
10395  switch (type) {
10396  case tIDENTIFIER: case tFID: case tGVAR: case tIVAR:
10397  case tCONSTANT: case tCVAR: case tLABEL: case tOP_ASGN:
10398 #ifndef RIPPER
10399  v = rb_id2str(valp->id);
10400 #else
10401  v = valp->node->nd_rval;
10402 #endif
10403  rb_parser_printf(p, "%"PRIsVALUE, v);
10404  break;
10405  case tINTEGER: case tFLOAT: case tRATIONAL: case tIMAGINARY:
10406  case tSTRING_CONTENT: case tCHAR:
10407 #ifndef RIPPER
10408  v = valp->node->nd_lit;
10409 #else
10410  v = valp->val;
10411 #endif
10412  rb_parser_printf(p, "%+"PRIsVALUE, v);
10413  break;
10414  case tNTH_REF:
10415 #ifndef RIPPER
10416  rb_parser_printf(p, "$%ld", valp->node->nd_nth);
10417 #else
10418  rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10419 #endif
10420  break;
10421  case tBACK_REF:
10422 #ifndef RIPPER
10423  rb_parser_printf(p, "$%c", (int)valp->node->nd_nth);
10424 #else
10425  rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10426 #endif
10427  break;
10428  default:
10429  break;
10430  }
10431 }
10432 
10433 static int
10434 assignable0(struct parser_params *p, ID id, const char **err)
10435 {
10436  if (!id) return -1;
10437  switch (id) {
10438  case keyword_self:
10439  *err = "Can't change the value of self";
10440  return -1;
10441  case keyword_nil:
10442  *err = "Can't assign to nil";
10443  return -1;
10444  case keyword_true:
10445  *err = "Can't assign to true";
10446  return -1;
10447  case keyword_false:
10448  *err = "Can't assign to false";
10449  return -1;
10450  case keyword__FILE__:
10451  *err = "Can't assign to __FILE__";
10452  return -1;
10453  case keyword__LINE__:
10454  *err = "Can't assign to __LINE__";
10455  return -1;
10456  case keyword__ENCODING__:
10457  *err = "Can't assign to __ENCODING__";
10458  return -1;
10459  }
10460  switch (id_type(id)) {
10461  case ID_LOCAL:
10462  if (dyna_in_block(p)) {
10463  if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10464  compile_error(p, "Can't assign to numbered parameter _%d",
10465  NUMPARAM_ID_TO_IDX(id));
10466  return -1;
10467  }
10468  if (dvar_curr(p, id)) return NODE_DASGN_CURR;
10469  if (dvar_defined(p, id)) return NODE_DASGN;
10470  if (local_id(p, id)) return NODE_LASGN;
10471  dyna_var(p, id);
10472  return NODE_DASGN_CURR;
10473  }
10474  else {
10475  if (!local_id(p, id)) local_var(p, id);
10476  return NODE_LASGN;
10477  }
10478  break;
10479  case ID_GLOBAL: return NODE_GASGN;
10480  case ID_INSTANCE: return NODE_IASGN;
10481  case ID_CONST:
10482  if (!p->in_def) return NODE_CDECL;
10483  *err = "dynamic constant assignment";
10484  return -1;
10485  case ID_CLASS: return NODE_CVASGN;
10486  default:
10487  compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10488  }
10489  return -1;
10490 }
10491 
10492 #ifndef RIPPER
10493 static NODE*
10494 assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10495 {
10496  const char *err = 0;
10497  int node_type = assignable0(p, id, &err);
10498  switch (node_type) {
10499  case NODE_DASGN_CURR: return NEW_DASGN_CURR(id, val, loc);
10500  case NODE_DASGN: return NEW_DASGN(id, val, loc);
10501  case NODE_LASGN: return NEW_LASGN(id, val, loc);
10502  case NODE_GASGN: return NEW_GASGN(id, val, loc);
10503  case NODE_IASGN: return NEW_IASGN(id, val, loc);
10504  case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10505  case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10506  }
10507  if (err) yyerror1(loc, err);
10508  return NEW_BEGIN(0, loc);
10509 }
10510 #else
10511 static VALUE
10512 assignable(struct parser_params *p, VALUE lhs)
10513 {
10514  const char *err = 0;
10515  assignable0(p, get_id(lhs), &err);
10516  if (err) lhs = assign_error(p, lhs);
10517  return lhs;
10518 }
10519 #endif
10520 
10521 static int
10522 is_private_local_id(ID name)
10523 {
10524  VALUE s;
10525  if (name == idUScore) return 1;
10526  if (!is_local_id(name)) return 0;
10527  s = rb_id2str(name);
10528  if (!s) return 0;
10529  return RSTRING_PTR(s)[0] == '_';
10530 }
10531 
10532 static int
10533 shadowing_lvar_0(struct parser_params *p, ID name)
10534 {
10535  if (is_private_local_id(name)) return 1;
10536  if (dyna_in_block(p)) {
10537  if (dvar_curr(p, name)) {
10538  yyerror0("duplicated argument name");
10539  }
10540  else if (dvar_defined(p, name) || local_id(p, name)) {
10541  vtable_add(p->lvtbl->vars, name);
10542  if (p->lvtbl->used) {
10543  vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
10544  }
10545  return 0;
10546  }
10547  }
10548  else {
10549  if (local_id(p, name)) {
10550  yyerror0("duplicated argument name");
10551  }
10552  }
10553  return 1;
10554 }
10555 
10556 static ID
10557 shadowing_lvar(struct parser_params *p, ID name)
10558 {
10559  shadowing_lvar_0(p, name);
10560  return name;
10561 }
10562 
10563 static void
10564 new_bv(struct parser_params *p, ID name)
10565 {
10566  if (!name) return;
10567  if (!is_local_id(name)) {
10568  compile_error(p, "invalid local variable - %"PRIsVALUE,
10569  rb_id2str(name));
10570  return;
10571  }
10572  if (!shadowing_lvar_0(p, name)) return;
10573  dyna_var(p, name);
10574 }
10575 
10576 #ifndef RIPPER
10577 static NODE *
10578 aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
10579 {
10580  return NEW_ATTRASGN(recv, tASET, idx, loc);
10581 }
10582 
10583 static void
10584 block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
10585 {
10586  if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
10587  compile_error(p, "both block arg and actual block given");
10588  }
10589 }
10590 
10591 static NODE *
10592 attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
10593 {
10594  if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
10595  return NEW_ATTRASGN(recv, id, 0, loc);
10596 }
10597 
10598 static void
10599 rb_backref_error(struct parser_params *p, NODE *node)
10600 {
10601  switch (nd_type(node)) {
10602  case NODE_NTH_REF:
10603  compile_error(p, "Can't set variable $%ld", node->nd_nth);
10604  break;
10605  case NODE_BACK_REF:
10606  compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
10607  break;
10608  }
10609 }
10610 
10611 static NODE *
10612 arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10613 {
10614  if (!node1) return NEW_LIST(node2, &node2->nd_loc);
10615  switch (nd_type(node1)) {
10616  case NODE_LIST:
10617  return list_append(p, node1, node2);
10618  case NODE_BLOCK_PASS:
10619  node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
10620  node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
10621  return node1;
10622  case NODE_ARGSPUSH:
10623  node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
10624  node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10625  nd_set_type(node1, NODE_ARGSCAT);
10626  return node1;
10627  case NODE_ARGSCAT:
10628  if (nd_type(node1->nd_body) != NODE_LIST) break;
10629  node1->nd_body = list_append(p, node1->nd_body, node2);
10630  node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10631  return node1;
10632  }
10633  return NEW_ARGSPUSH(node1, node2, loc);
10634 }
10635 
10636 static NODE *
10637 arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10638 {
10639  if (!node2) return node1;
10640  switch (nd_type(node1)) {
10641  case NODE_BLOCK_PASS:
10642  if (node1->nd_head)
10643  node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
10644  else
10645  node1->nd_head = NEW_LIST(node2, loc);
10646  return node1;
10647  case NODE_ARGSPUSH:
10648  if (nd_type(node2) != NODE_LIST) break;
10649  node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
10650  nd_set_type(node1, NODE_ARGSCAT);
10651  return node1;
10652  case NODE_ARGSCAT:
10653  if (nd_type(node2) != NODE_LIST ||
10654  nd_type(node1->nd_body) != NODE_LIST) break;
10655  node1->nd_body = list_concat(node1->nd_body, node2);
10656  return node1;
10657  }
10658  return NEW_ARGSCAT(node1, node2, loc);
10659 }
10660 
10661 static NODE *
10662 last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
10663 {
10664  NODE *n1;
10665  if ((n1 = splat_array(args)) != 0) {
10666  return list_append(p, n1, last_arg);
10667  }
10668  return arg_append(p, args, last_arg, loc);
10669 }
10670 
10671 static NODE *
10672 rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
10673 {
10674  NODE *n1;
10675  if ((nd_type(rest_arg) == NODE_LIST) && (n1 = splat_array(args)) != 0) {
10676  return list_concat(n1, rest_arg);
10677  }
10678  return arg_concat(p, args, rest_arg, loc);
10679 }
10680 
10681 static NODE *
10682 splat_array(NODE* node)
10683 {
10684  if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
10685  if (nd_type(node) == NODE_LIST) return node;
10686  return 0;
10687 }
10688 
10689 static void
10690 mark_lvar_used(struct parser_params *p, NODE *rhs)
10691 {
10692  ID *vidp = NULL;
10693  if (!rhs) return;
10694  switch (nd_type(rhs)) {
10695  case NODE_LASGN:
10696  if (local_id_ref(p, rhs->nd_vid, &vidp)) {
10697  if (vidp) *vidp |= LVAR_USED;
10698  }
10699  break;
10700  case NODE_DASGN:
10701  case NODE_DASGN_CURR:
10702  if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
10703  if (vidp) *vidp |= LVAR_USED;
10704  }
10705  break;
10706 #if 0
10707  case NODE_MASGN:
10708  for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
10709  mark_lvar_used(p, rhs->nd_head);
10710  }
10711  break;
10712 #endif
10713  }
10714 }
10715 
10716 static NODE *
10717 node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, const YYLTYPE *loc)
10718 {
10719  if (!lhs) return 0;
10720 
10721  switch (nd_type(lhs)) {
10722  case NODE_GASGN:
10723  case NODE_IASGN:
10724  case NODE_LASGN:
10725  case NODE_DASGN:
10726  case NODE_DASGN_CURR:
10727  case NODE_MASGN:
10728  case NODE_CDECL:
10729  case NODE_CVASGN:
10730  lhs->nd_value = rhs;
10731  nd_set_loc(lhs, loc);
10732  break;
10733 
10734  case NODE_ATTRASGN:
10735  lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
10736  nd_set_loc(lhs, loc);
10737  break;
10738 
10739  default:
10740  /* should not happen */
10741  break;
10742  }
10743 
10744  return lhs;
10745 }
10746 
10747 static NODE *
10748 value_expr_check(struct parser_params *p, NODE *node)
10749 {
10750  NODE *void_node = 0, *vn;
10751 
10752  if (!node) {
10753  rb_warning0("empty expression");
10754  }
10755  while (node) {
10756  switch (nd_type(node)) {
10757  case NODE_RETURN:
10758  case NODE_BREAK:
10759  case NODE_NEXT:
10760  case NODE_REDO:
10761  case NODE_RETRY:
10762  return void_node ? void_node : node;
10763 
10764  case NODE_CASE3:
10765  if (!node->nd_body || nd_type(node->nd_body) != NODE_IN) {
10766  compile_error(p, "unexpected node");
10767  return NULL;
10768  }
10769  if (node->nd_body->nd_body) {
10770  return NULL;
10771  }
10772  /* single line pattern matching */
10773  return void_node ? void_node : node;
10774 
10775  case NODE_BLOCK:
10776  while (node->nd_next) {
10777  node = node->nd_next;
10778  }
10779  node = node->nd_head;
10780  break;
10781 
10782  case NODE_BEGIN:
10783  node = node->nd_body;
10784  break;
10785 
10786  case NODE_IF:
10787  case NODE_UNLESS:
10788  if (!node->nd_body) {
10789  return NULL;
10790  }
10791  else if (!node->nd_else) {
10792  return NULL;
10793  }
10794  vn = value_expr_check(p, node->nd_body);
10795  if (!vn) return NULL;
10796  if (!void_node) void_node = vn;
10797  node = node->nd_else;
10798  break;
10799 
10800  case NODE_AND:
10801  case NODE_OR:
10802  node = node->nd_1st;
10803  break;
10804 
10805  case NODE_LASGN:
10806  case NODE_DASGN:
10807  case NODE_DASGN_CURR:
10808  case NODE_MASGN:
10809  mark_lvar_used(p, node);
10810  return NULL;
10811 
10812  default:
10813  return NULL;
10814  }
10815  }
10816 
10817  return NULL;
10818 }
10819 
10820 static int
10821 value_expr_gen(struct parser_params *p, NODE *node)
10822 {
10823  NODE *void_node = value_expr_check(p, node);
10824  if (void_node) {
10825  yyerror1(&void_node->nd_loc, "void value expression");
10826  /* or "control never reach"? */
10827  return FALSE;
10828  }
10829  return TRUE;
10830 }
10831 static void
10832 void_expr(struct parser_params *p, NODE *node)
10833 {
10834  const char *useless = 0;
10835 
10836  if (!RTEST(ruby_verbose)) return;
10837 
10838  if (!node || !(node = nd_once_body(node))) return;
10839  switch (nd_type(node)) {
10840  case NODE_OPCALL:
10841  switch (node->nd_mid) {
10842  case '+':
10843  case '-':
10844  case '*':
10845  case '/':
10846  case '%':
10847  case tPOW:
10848  case tUPLUS:
10849  case tUMINUS:
10850  case '|':
10851  case '^':
10852  case '&':
10853  case tCMP:
10854  case '>':
10855  case tGEQ:
10856  case '<':
10857  case tLEQ:
10858  case tEQ:
10859  case tNEQ:
10860  useless = rb_id2name(node->nd_mid);
10861  break;
10862  }
10863  break;
10864 
10865  case NODE_LVAR:
10866  case NODE_DVAR:
10867  case NODE_GVAR:
10868  case NODE_IVAR:
10869  case NODE_CVAR:
10870  case NODE_NTH_REF:
10871  case NODE_BACK_REF:
10872  useless = "a variable";
10873  break;
10874  case NODE_CONST:
10875  useless = "a constant";
10876  break;
10877  case NODE_LIT:
10878  case NODE_STR:
10879  case NODE_DSTR:
10880  case NODE_DREGX:
10881  useless = "a literal";
10882  break;
10883  case NODE_COLON2:
10884  case NODE_COLON3:
10885  useless = "::";
10886  break;
10887  case NODE_DOT2:
10888  useless = "..";
10889  break;
10890  case NODE_DOT3:
10891  useless = "...";
10892  break;
10893  case NODE_SELF:
10894  useless = "self";
10895  break;
10896  case NODE_NIL:
10897  useless = "nil";
10898  break;
10899  case NODE_TRUE:
10900  useless = "true";
10901  break;
10902  case NODE_FALSE:
10903  useless = "false";
10904  break;
10905  case NODE_DEFINED:
10906  useless = "defined?";
10907  break;
10908  }
10909 
10910  if (useless) {
10911  rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
10912  }
10913 }
10914 
10915 static NODE *
10916 void_stmts(struct parser_params *p, NODE *node)
10917 {
10918  NODE *const n = node;
10919  if (!RTEST(ruby_verbose)) return n;
10920  if (!node) return n;
10921  if (nd_type(node) != NODE_BLOCK) return n;
10922 
10923  while (node->nd_next) {
10924  void_expr(p, node->nd_head);
10925  node = node->nd_next;
10926  }
10927  return n;
10928 }
10929 
10930 static NODE *
10931 remove_begin(NODE *node)
10932 {
10933  NODE **n = &node, *n1 = node;
10934  while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
10935  *n = n1 = n1->nd_body;
10936  }
10937  return node;
10938 }
10939 
10940 static NODE *
10941 remove_begin_all(NODE *node)
10942 {
10943  NODE **n = &node, *n1 = node;
10944  while (n1 && nd_type(n1) == NODE_BEGIN) {
10945  *n = n1 = n1->nd_body;
10946  }
10947  return node;
10948 }
10949 
10950 static void
10951 reduce_nodes(struct parser_params *p, NODE **body)
10952 {
10953  NODE *node = *body;
10954 
10955  if (!node) {
10956  *body = NEW_NIL(&NULL_LOC);
10957  return;
10958  }
10959 #define subnodes(n1, n2) \
10960  ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
10961  (!node->n2) ? (body = &node->n1, 1) : \
10962  (reduce_nodes(p, &node->n1), body = &node->n2, 1))
10963 
10964  while (node) {
10965  int newline = (int)(node->flags & NODE_FL_NEWLINE);
10966  switch (nd_type(node)) {
10967  end:
10968  case NODE_NIL:
10969  *body = 0;
10970  return;
10971  case NODE_RETURN:
10972  *body = node = node->nd_stts;
10973  if (newline && node) node->flags |= NODE_FL_NEWLINE;
10974  continue;
10975  case NODE_BEGIN:
10976  *body = node = node->nd_body;
10977  if (newline && node) node->flags |= NODE_FL_NEWLINE;
10978  continue;
10979  case NODE_BLOCK:
10980  body = &node->nd_end->nd_head;
10981  break;
10982  case NODE_IF:
10983  case NODE_UNLESS:
10984  if (subnodes(nd_body, nd_else)) break;
10985  return;
10986  case NODE_CASE:
10987  body = &node->nd_body;
10988  break;
10989  case NODE_WHEN:
10990  if (!subnodes(nd_body, nd_next)) goto end;
10991  break;
10992  case NODE_ENSURE:
10993  if (!subnodes(nd_head, nd_resq)) goto end;
10994  break;
10995  case NODE_RESCUE:
10996  if (node->nd_else) {
10997  body = &node->nd_resq;
10998  break;
10999  }
11000  if (!subnodes(nd_head, nd_resq)) goto end;
11001  break;
11002  default:
11003  return;
11004  }
11005  node = *body;
11006  if (newline && node) node->flags |= NODE_FL_NEWLINE;
11007  }
11008 
11009 #undef subnodes
11010 }
11011 
11012 static int
11013 is_static_content(NODE *node)
11014 {
11015  if (!node) return 1;
11016  switch (nd_type(node)) {
11017  case NODE_HASH:
11018  if (!(node = node->nd_head)) break;
11019  case NODE_LIST:
11020  do {
11021  if (!is_static_content(node->nd_head)) return 0;
11022  } while ((node = node->nd_next) != 0);
11023  case NODE_LIT:
11024  case NODE_STR:
11025  case NODE_NIL:
11026  case NODE_TRUE:
11027  case NODE_FALSE:
11028  case NODE_ZLIST:
11029  break;
11030  default:
11031  return 0;
11032  }
11033  return 1;
11034 }
11035 
11036 static int
11037 assign_in_cond(struct parser_params *p, NODE *node)
11038 {
11039  switch (nd_type(node)) {
11040  case NODE_MASGN:
11041  case NODE_LASGN:
11042  case NODE_DASGN:
11043  case NODE_DASGN_CURR:
11044  case NODE_GASGN:
11045  case NODE_IASGN:
11046  break;
11047 
11048  default:
11049  return 0;
11050  }
11051 
11052  if (!node->nd_value) return 1;
11053  if (is_static_content(node->nd_value)) {
11054  /* reports always */
11055  parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11056  }
11057  return 1;
11058 }
11059 
11060 enum cond_type {
11061  COND_IN_OP,
11062  COND_IN_COND,
11063  COND_IN_FF
11064 };
11065 
11066 #define SWITCH_BY_COND_TYPE(t, w, arg) \
11067  switch (t) { \
11068  case COND_IN_OP: break; \
11069  case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11070  case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11071  }
11072 
11073 static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11074 
11075 static NODE*
11076 range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11077 {
11078  enum node_type type;
11079 
11080  if (node == 0) return 0;
11081 
11082  type = nd_type(node);
11083  value_expr(node);
11084  if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11085  if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11086  return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(rb_intern("$."), loc), loc), loc);
11087  }
11088  return cond0(p, node, COND_IN_FF, loc);
11089 }
11090 
11091 static NODE*
11092 cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11093 {
11094  if (node == 0) return 0;
11095  if (!(node = nd_once_body(node))) return 0;
11096  assign_in_cond(p, node);
11097 
11098  switch (nd_type(node)) {
11099  case NODE_DSTR:
11100  case NODE_EVSTR:
11101  case NODE_STR:
11102  SWITCH_BY_COND_TYPE(type, warn, "string ")
11103  break;
11104 
11105  case NODE_DREGX:
11106  if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11107 
11108  return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11109 
11110  case NODE_AND:
11111  case NODE_OR:
11112  node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11113  node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11114  break;
11115 
11116  case NODE_DOT2:
11117  case NODE_DOT3:
11118  node->nd_beg = range_op(p, node->nd_beg, loc);
11119  node->nd_end = range_op(p, node->nd_end, loc);
11120  if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
11121  else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
11122  break;
11123 
11124  case NODE_DSYM:
11125  SWITCH_BY_COND_TYPE(type, warning, "string ")
11126  break;
11127 
11128  case NODE_LIT:
11129  if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11130  if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11131  nd_set_type(node, NODE_MATCH);
11132  }
11133  else if (node->nd_lit == Qtrue ||
11134  node->nd_lit == Qfalse) {
11135  /* booleans are OK, e.g., while true */
11136  }
11137  else {
11138  SWITCH_BY_COND_TYPE(type, warning, "")
11139  }
11140  default:
11141  break;
11142  }
11143  return node;
11144 }
11145 
11146 static NODE*
11147 cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11148 {
11149  if (node == 0) return 0;
11150  return cond0(p, node, COND_IN_COND, loc);
11151 }
11152 
11153 static NODE*
11154 method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11155 {
11156  if (node == 0) return 0;
11157  return cond0(p, node, COND_IN_OP, loc);
11158 }
11159 
11160 static NODE*
11161 new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11162 {
11163  if (!cc) return right;
11164  cc = cond0(p, cc, COND_IN_COND, loc);
11165  return newline_node(NEW_IF(cc, left, right, loc));
11166 }
11167 
11168 static NODE*
11169 new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11170 {
11171  if (!cc) return right;
11172  cc = cond0(p, cc, COND_IN_COND, loc);
11173  return newline_node(NEW_UNLESS(cc, left, right, loc));
11174 }
11175 
11176 static NODE*
11177 logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11178  const YYLTYPE *op_loc, const YYLTYPE *loc)
11179 {
11180  enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11181  NODE *op;
11182  value_expr(left);
11183  if (left && (enum node_type)nd_type(left) == type) {
11184  NODE *node = left, *second;
11185  while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
11186  node = second;
11187  }
11188  node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11189  nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11190  left->nd_loc.end_pos = loc->end_pos;
11191  return left;
11192  }
11193  op = NEW_NODE(type, left, right, 0, loc);
11194  nd_set_line(op, op_loc->beg_pos.lineno);
11195  return op;
11196 }
11197 
11198 static void
11199 no_blockarg(struct parser_params *p, NODE *node)
11200 {
11201  if (node && nd_type(node) == NODE_BLOCK_PASS) {
11202  compile_error(p, "block argument should not be given");
11203  }
11204 }
11205 
11206 static NODE *
11207 ret_args(struct parser_params *p, NODE *node)
11208 {
11209  if (node) {
11210  no_blockarg(p, node);
11211  if (nd_type(node) == NODE_LIST) {
11212  if (node->nd_next == 0) {
11213  node = node->nd_head;
11214  }
11215  else {
11216  nd_set_type(node, NODE_VALUES);
11217  }
11218  }
11219  }
11220  return node;
11221 }
11222 
11223 static NODE *
11224 new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11225 {
11226  if (node) no_blockarg(p, node);
11227 
11228  return NEW_YIELD(node, loc);
11229 }
11230 
11231 static VALUE
11232 negate_lit(struct parser_params *p, VALUE lit)
11233 {
11234  if (FIXNUM_P(lit)) {
11235  return LONG2FIX(-FIX2LONG(lit));
11236  }
11237  if (SPECIAL_CONST_P(lit)) {
11238 #if USE_FLONUM
11239  if (FLONUM_P(lit)) {
11240  return DBL2NUM(-RFLOAT_VALUE(lit));
11241  }
11242 #endif
11243  goto unknown;
11244  }
11245  switch (BUILTIN_TYPE(lit)) {
11246  case T_BIGNUM:
11247  BIGNUM_NEGATE(lit);
11248  lit = rb_big_norm(lit);
11249  break;
11250  case T_RATIONAL:
11251  RRATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11252  break;
11253  case T_COMPLEX:
11254  RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11255  RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11256  break;
11257  case T_FLOAT:
11258  RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
11259  break;
11260  unknown:
11261  default:
11262  rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11263  rb_builtin_class_name(lit));
11264  break;
11265  }
11266  return lit;
11267 }
11268 
11269 static NODE *
11270 arg_blk_pass(NODE *node1, NODE *node2)
11271 {
11272  if (node2) {
11273  if (!node1) return node2;
11274  node2->nd_head = node1;
11275  nd_set_first_lineno(node2, nd_first_lineno(node1));
11276  nd_set_first_column(node2, nd_first_column(node1));
11277  return node2;
11278  }
11279  return node1;
11280 }
11281 
11282 static bool
11283 args_info_empty_p(struct rb_args_info *args)
11284 {
11285  if (args->pre_args_num) return false;
11286  if (args->post_args_num) return false;
11287  if (args->rest_arg) return false;
11288  if (args->opt_args) return false;
11289  if (args->block_arg) return false;
11290  if (args->kw_args) return false;
11291  if (args->kw_rest_arg) return false;
11292  return true;
11293 }
11294 
11295 static NODE*
11296 new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
11297 {
11298  int saved_line = p->ruby_sourceline;
11299  struct rb_args_info *args = tail->nd_ainfo;
11300 
11301  args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
11302  args->pre_init = pre_args ? pre_args->nd_next : 0;
11303 
11304  args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
11305  args->post_init = post_args ? post_args->nd_next : 0;
11306  args->first_post_arg = post_args ? post_args->nd_pid : 0;
11307 
11308  args->rest_arg = rest_arg;
11309 
11310  args->opt_args = opt_args;
11311 
11312  args->ruby2_keywords = rest_arg == idFWD_REST;
11313 
11314  p->ruby_sourceline = saved_line;
11315  nd_set_loc(tail, loc);
11316 
11317  return tail;
11318 }
11319 
11320 static NODE*
11321 new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *loc)
11322 {
11323  int saved_line = p->ruby_sourceline;
11324  NODE *node;
11325  VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11326  struct rb_args_info *args = ZALLOC(struct rb_args_info);
11327  rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
11328  args->imemo = tmpbuf;
11329  node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
11330  RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11331  if (p->error_p) return node;
11332 
11333  args->block_arg = block;
11334  args->kw_args = kw_args;
11335 
11336  if (kw_args) {
11337  /*
11338  * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
11339  * variable order: k1, kr1, k2, &b, internal_id, krest
11340  * #=> <reorder>
11341  * variable order: kr1, k1, k2, internal_id, krest, &b
11342  */
11343  ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
11344  struct vtable *vtargs = p->lvtbl->args;
11345  NODE *kwn = kw_args;
11346 
11347  vtable_pop(vtargs, !!block + !!kw_rest_arg);
11348  required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
11349  while (kwn) {
11350  if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
11351  --kw_vars;
11352  --required_kw_vars;
11353  kwn = kwn->nd_next;
11354  }
11355 
11356  for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
11357  ID vid = kwn->nd_body->nd_vid;
11358  if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
11359  *required_kw_vars++ = vid;
11360  }
11361  else {
11362  *kw_vars++ = vid;
11363  }
11364  }
11365 
11366  arg_var(p, kw_bits);
11367  if (kw_rest_arg) arg_var(p, kw_rest_arg);
11368  if (block) arg_var(p, block);
11369 
11370  args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11371  args->kw_rest_arg->nd_cflag = kw_bits;
11372  }
11373  else if (kw_rest_arg == idNil) {
11374  args->no_kwarg = 1;
11375  }
11376  else if (kw_rest_arg) {
11377  args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11378  }
11379 
11380  p->ruby_sourceline = saved_line;
11381  return node;
11382 }
11383 
11384 static NODE *
11385 args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
11386 {
11387  if (max_numparam > NO_PARAM) {
11388  if (!args) {
11389  YYLTYPE loc = RUBY_INIT_YYLLOC();
11390  args = new_args_tail(p, 0, 0, 0, 0);
11391  nd_set_loc(args, &loc);
11392  }
11393  args->nd_ainfo->pre_args_num = max_numparam;
11394  }
11395  return args;
11396 }
11397 
11398 static NODE*
11399 new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
11400 {
11401  struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
11402 
11403  aryptn->nd_pconst = constant;
11404 
11405  if (pre_arg) {
11406  NODE *pre_args = NEW_LIST(pre_arg, loc);
11407  if (apinfo->pre_args) {
11408  apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
11409  }
11410  else {
11411  apinfo->pre_args = pre_args;
11412  }
11413  }
11414  return aryptn;
11415 }
11416 
11417 static NODE*
11418 new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
11419 {
11420  int saved_line = p->ruby_sourceline;
11421  NODE *node;
11422  VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11423  struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
11424  rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
11425  node = NEW_NODE(NODE_ARYPTN, 0, 0, apinfo, loc);
11426  apinfo->imemo = tmpbuf;
11427  RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11428 
11429  apinfo->pre_args = pre_args;
11430 
11431  if (has_rest) {
11432  if (rest_arg) {
11433  apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
11434  }
11435  else {
11436  apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
11437  }
11438  }
11439  else {
11440  apinfo->rest_arg = NULL;
11441  }
11442 
11443  apinfo->post_args = post_args;
11444 
11445  p->ruby_sourceline = saved_line;
11446  return node;
11447 }
11448 
11449 static NODE*
11450 new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
11451 {
11452  hshptn->nd_pconst = constant;
11453  return hshptn;
11454 }
11455 
11456 static NODE*
11457 new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
11458 {
11459  int saved_line = p->ruby_sourceline;
11460  NODE *node, *kw_rest_arg_node;
11461 
11462  if (kw_rest_arg == idNil) {
11463  kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
11464  }
11465  else if (kw_rest_arg) {
11466  kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
11467  }
11468  else {
11469  kw_rest_arg_node = NULL;
11470  }
11471 
11472  node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
11473 
11474  p->ruby_sourceline = saved_line;
11475  return node;
11476 }
11477 
11478 static NODE *
11479 new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc)
11480 {
11481  NODE *node = NEW_CASE3(val, pat, loc);
11482 
11483  if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL))
11484  rb_warn0L(nd_line(node), "Pattern matching is experimental, and the behavior may change in future versions of Ruby!");
11485  return node;
11486 }
11487 
11488 static NODE*
11489 dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11490 {
11491  VALUE lit;
11492 
11493  if (!node) {
11494  return NEW_LIT(ID2SYM(idNULL), loc);
11495  }
11496 
11497  switch (nd_type(node)) {
11498  case NODE_DSTR:
11499  nd_set_type(node, NODE_DSYM);
11500  nd_set_loc(node, loc);
11501  break;
11502  case NODE_STR:
11503  lit = node->nd_lit;
11504  RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
11505  nd_set_type(node, NODE_LIT);
11506  nd_set_loc(node, loc);
11507  break;
11508  default:
11509  node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
11510  break;
11511  }
11512  return node;
11513 }
11514 
11515 static int
11516 append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
11517 {
11518  NODE *node = (NODE *)v;
11519  NODE **result = (NODE **)h;
11520  node->nd_alen = 2;
11521  node->nd_next->nd_end = node->nd_next;
11522  node->nd_next->nd_next = 0;
11523  if (*result)
11524  list_concat(*result, node);
11525  else
11526  *result = node;
11527  return ST_CONTINUE;
11528 }
11529 
11530 static NODE *
11531 remove_duplicate_keys(struct parser_params *p, NODE *hash)
11532 {
11533  st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
11534  NODE *result = 0;
11535  rb_code_location_t loc = hash->nd_loc;
11536  while (hash && hash->nd_head && hash->nd_next) {
11537  NODE *head = hash->nd_head;
11538  NODE *value = hash->nd_next;
11539  NODE *next = value->nd_next;
11540  VALUE key = (VALUE)head;
11541  st_data_t data;
11542  if (nd_type(head) == NODE_LIT &&
11543  st_lookup(literal_keys, (key = head->nd_lit), &data)) {
11544  rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
11545  "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
11546  head->nd_lit, nd_line(head));
11547  head = ((NODE *)data)->nd_next;
11548  head->nd_head = block_append(p, head->nd_head, value->nd_head);
11549  }
11550  else {
11551  st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
11552  }
11553  hash = next;
11554  }
11555  st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
11556  st_free_table(literal_keys);
11557  if (hash) {
11558  if (!result) result = hash;
11559  else list_concat(result, hash);
11560  }
11561  result->nd_loc = loc;
11562  return result;
11563 }
11564 
11565 static NODE *
11566 new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11567 {
11568  if (hash) hash = remove_duplicate_keys(p, hash);
11569  return NEW_HASH(hash, loc);
11570 }
11571 #endif
11572 
11573 static void
11574 error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
11575 {
11576  if (is_private_local_id(id)) {
11577  return;
11578  }
11579  if (st_is_member(p->pvtbl, id)) {
11580  yyerror1(loc, "duplicated variable name");
11581  }
11582  else {
11583  st_insert(p->pvtbl, (st_data_t)id, 0);
11584  }
11585 }
11586 
11587 static void
11588 error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
11589 {
11590  if (!p->pktbl) {
11591  p->pktbl = st_init_numtable();
11592  }
11593  else if (st_is_member(p->pktbl, key)) {
11594  yyerror1(loc, "duplicated key name");
11595  return;
11596  }
11597  st_insert(p->pktbl, (st_data_t)key, 0);
11598 }
11599 
11600 #ifndef RIPPER
11601 static NODE *
11602 new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11603 {
11604  return NEW_HASH(hash, loc);
11605 }
11606 #endif /* !RIPPER */
11607 
11608 #ifndef RIPPER
11609 static NODE *
11610 new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11611 {
11612  NODE *asgn;
11613 
11614  if (lhs) {
11615  ID vid = lhs->nd_vid;
11616  YYLTYPE lhs_loc = lhs->nd_loc;
11617  if (op == tOROP) {
11618  lhs->nd_value = rhs;
11619  nd_set_loc(lhs, loc);
11620  asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
11621  if (is_notop_id(vid)) {
11622  switch (id_type(vid)) {
11623  case ID_GLOBAL:
11624  case ID_INSTANCE:
11625  case ID_CLASS:
11626  asgn->nd_aid = vid;
11627  }
11628  }
11629  }
11630  else if (op == tANDOP) {
11631  lhs->nd_value = rhs;
11632  nd_set_loc(lhs, loc);
11633  asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
11634  }
11635  else {
11636  asgn = lhs;
11637  asgn->nd_value = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
11638  nd_set_loc(asgn, loc);
11639  }
11640  }
11641  else {
11642  asgn = NEW_BEGIN(0, loc);
11643  }
11644  return asgn;
11645 }
11646 
11647 static NODE *
11648 new_ary_op_assign(struct parser_params *p, NODE *ary,
11649  NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
11650 {
11651  NODE *asgn;
11652 
11653  args = make_list(args, args_loc);
11654  if (nd_type(args) == NODE_BLOCK_PASS) {
11655  args = NEW_ARGSCAT(args, rhs, loc);
11656  }
11657  else {
11658  args = arg_concat(p, args, rhs, loc);
11659  }
11660  asgn = NEW_OP_ASGN1(ary, op, args, loc);
11661  fixpos(asgn, ary);
11662  return asgn;
11663 }
11664 
11665 static NODE *
11666 new_attr_op_assign(struct parser_params *p, NODE *lhs,
11667  ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
11668 {
11669  NODE *asgn;
11670 
11671  asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
11672  fixpos(asgn, lhs);
11673  return asgn;
11674 }
11675 
11676 static NODE *
11677 new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11678 {
11679  NODE *asgn;
11680 
11681  if (lhs) {
11682  asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
11683  }
11684  else {
11685  asgn = NEW_BEGIN(0, loc);
11686  }
11687  fixpos(asgn, lhs);
11688  return asgn;
11689 }
11690 
11691 static NODE *
11692 const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
11693 {
11694  if (p->in_def) {
11695  yyerror1(loc, "dynamic constant assignment");
11696  }
11697  return NEW_CDECL(0, 0, (path), loc);
11698 }
11699 #else
11700 static VALUE
11701 const_decl(struct parser_params *p, VALUE path)
11702 {
11703  if (p->in_def) {
11704  path = dispatch1(assign_error, path);
11705  ripper_error(p);
11706  }
11707  return path;
11708 }
11709 
11710 static VALUE
11711 assign_error(struct parser_params *p, VALUE a)
11712 {
11713  a = dispatch1(assign_error, a);
11714  ripper_error(p);
11715  return a;
11716 }
11717 
11718 static VALUE
11719 var_field(struct parser_params *p, VALUE a)
11720 {
11721  return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
11722 }
11723 #endif
11724 
11725 #ifndef RIPPER
11726 static NODE *
11727 new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
11728 {
11729  NODE *result = head;
11730  if (rescue) {
11731  NODE *tmp = rescue_else ? rescue_else : rescue;
11732  YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
11733 
11734  result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
11735  nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
11736  }
11737  else if (rescue_else) {
11738  result = block_append(p, result, rescue_else);
11739  }
11740  if (ensure) {
11741  result = NEW_ENSURE(result, ensure, loc);
11742  }
11743  fixpos(result, head);
11744  return result;
11745 }
11746 #endif
11747 
11748 static void
11749 warn_unused_var(struct parser_params *p, struct local_vars *local)
11750 {
11751  int cnt;
11752 
11753  if (!local->used) return;
11754  cnt = local->used->pos;
11755  if (cnt != local->vars->pos) {
11756  rb_parser_fatal(p, "local->used->pos != local->vars->pos");
11757  }
11758 #ifndef RIPPER
11759  ID *v = local->vars->tbl;
11760  ID *u = local->used->tbl;
11761  for (int i = 0; i < cnt; ++i) {
11762  if (!v[i] || (u[i] & LVAR_USED)) continue;
11763  if (is_private_local_id(v[i])) continue;
11764  rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
11765  }
11766 #endif
11767 }
11768 
11769 static void
11770 local_push(struct parser_params *p, int toplevel_scope)
11771 {
11772  struct local_vars *local;
11773  int inherits_dvars = toplevel_scope && compile_for_eval;
11774  int warn_unused_vars = RTEST(ruby_verbose);
11775 
11776  local = ALLOC(struct local_vars);
11777  local->prev = p->lvtbl;
11778  local->args = vtable_alloc(0);
11779  local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
11780 #ifndef RIPPER
11781  if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
11782  if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
11783  local->numparam.outer = 0;
11784  local->numparam.inner = 0;
11785  local->numparam.current = 0;
11786 #endif
11787  local->used = warn_unused_vars ? vtable_alloc(0) : 0;
11788 
11789 # if WARN_PAST_SCOPE
11790  local->past = 0;
11791 # endif
11792  CMDARG_PUSH(0);
11793  COND_PUSH(0);
11794  p->lvtbl = local;
11795 }
11796 
11797 static void
11798 local_pop(struct parser_params *p)
11799 {
11800  struct local_vars *local = p->lvtbl->prev;
11801  if (p->lvtbl->used) {
11802  warn_unused_var(p, p->lvtbl);
11803  vtable_free(p->lvtbl->used);
11804  }
11805 # if WARN_PAST_SCOPE
11806  while (p->lvtbl->past) {
11807  struct vtable *past = p->lvtbl->past;
11808  p->lvtbl->past = past->prev;
11809  vtable_free(past);
11810  }
11811 # endif
11812  vtable_free(p->lvtbl->args);
11813  vtable_free(p->lvtbl->vars);
11814  CMDARG_POP();
11815  COND_POP();
11816  ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
11817  p->lvtbl = local;
11818 }
11819 
11820 #ifndef RIPPER
11821 static ID*
11822 local_tbl(struct parser_params *p)
11823 {
11824  int cnt_args = vtable_size(p->lvtbl->args);
11825  int cnt_vars = vtable_size(p->lvtbl->vars);
11826  int cnt = cnt_args + cnt_vars;
11827  int i, j;
11828  ID *buf;
11829  VALUE tbl = 0;
11830 
11831  if (cnt <= 0) return 0;
11832  tbl = rb_imemo_tmpbuf_auto_free_pointer();
11833  buf = ALLOC_N(ID, cnt + 2);
11834  rb_imemo_tmpbuf_set_ptr(tbl, buf);
11835  MEMCPY(buf+1, p->lvtbl->args->tbl, ID, cnt_args);
11836  /* remove IDs duplicated to warn shadowing */
11837  for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) {
11838  ID id = p->lvtbl->vars->tbl[i];
11839  if (!vtable_included(p->lvtbl->args, id)) {
11840  buf[j++] = id;
11841  }
11842  }
11843  if (--j < cnt) {
11844  REALLOC_N(buf, ID, (cnt = j) + 2);
11845  rb_imemo_tmpbuf_set_ptr(tbl, buf);
11846  }
11847  buf[0] = cnt;
11848  buf[cnt + 1] = (ID)tbl;
11849  RB_OBJ_WRITTEN(p->ast, Qnil, tbl);
11850 
11851  return buf;
11852 }
11853 
11854 static NODE*
11855 node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
11856 {
11857  ID *a0;
11858  NODE *n;
11859 
11860  a0 = local_tbl(p);
11861  n = NEW_NODE(type, a0, a1, a2, loc);
11862  return n;
11863 }
11864 
11865 #endif
11866 
11867 static void
11868 numparam_name(struct parser_params *p, ID id)
11869 {
11870  if (!NUMPARAM_ID_P(id)) return;
11871  rb_warn1("`_%d' is reserved for numbered parameter; consider another name",
11872  WARN_I(NUMPARAM_ID_TO_IDX(id)));
11873 }
11874 
11875 static void
11876 arg_var(struct parser_params *p, ID id)
11877 {
11878  numparam_name(p, id);
11879  vtable_add(p->lvtbl->args, id);
11880 }
11881 
11882 static void
11883 local_var(struct parser_params *p, ID id)
11884 {
11885  numparam_name(p, id);
11886  vtable_add(p->lvtbl->vars, id);
11887  if (p->lvtbl->used) {
11888  vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
11889  }
11890 }
11891 
11892 static int
11893 local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
11894 {
11895  struct vtable *vars, *args, *used;
11896 
11897  vars = p->lvtbl->vars;
11898  args = p->lvtbl->args;
11899  used = p->lvtbl->used;
11900 
11901  while (vars && !DVARS_TERMINAL_P(vars->prev)) {
11902  vars = vars->prev;
11903  args = args->prev;
11904  if (used) used = used->prev;
11905  }
11906 
11907  if (vars && vars->prev == DVARS_INHERIT) {
11908  return rb_local_defined(id, p->parent_iseq);
11909  }
11910  else if (vtable_included(args, id)) {
11911  return 1;
11912  }
11913  else {
11914  int i = vtable_included(vars, id);
11915  if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
11916  return i != 0;
11917  }
11918 }
11919 
11920 static int
11921 local_id(struct parser_params *p, ID id)
11922 {
11923  return local_id_ref(p, id, NULL);
11924 }
11925 
11926 static NODE *
11927 numparam_push(struct parser_params *p)
11928 {
11929 #ifndef RIPPER
11930  struct local_vars *local = p->lvtbl;
11931  NODE *inner = local->numparam.inner;
11932  if (!local->numparam.outer) {
11933  local->numparam.outer = local->numparam.current;
11934  }
11935  local->numparam.inner = 0;
11936  local->numparam.current = 0;
11937  return inner;
11938 #else
11939  return 0;
11940 #endif
11941 }
11942 
11943 static void
11944 numparam_pop(struct parser_params *p, NODE *prev_inner)
11945 {
11946 #ifndef RIPPER
11947  struct local_vars *local = p->lvtbl;
11948  if (prev_inner) {
11949  /* prefer first one */
11950  local->numparam.inner = prev_inner;
11951  }
11952  else if (local->numparam.current) {
11953  /* current and inner are exclusive */
11954  local->numparam.inner = local->numparam.current;
11955  }
11956  if (p->max_numparam > NO_PARAM) {
11957  /* current and outer are exclusive */
11958  local->numparam.current = local->numparam.outer;
11959  local->numparam.outer = 0;
11960  }
11961  else {
11962  /* no numbered parameter */
11963  local->numparam.current = 0;
11964  }
11965 #endif
11966 }
11967 
11968 static const struct vtable *
11969 dyna_push(struct parser_params *p)
11970 {
11971  p->lvtbl->args = vtable_alloc(p->lvtbl->args);
11972  p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
11973  if (p->lvtbl->used) {
11974  p->lvtbl->used = vtable_alloc(p->lvtbl->used);
11975  }
11976  return p->lvtbl->args;
11977 }
11978 
11979 static void
11980 dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
11981 {
11982  struct vtable *tmp = *vtblp;
11983  *vtblp = tmp->prev;
11984 # if WARN_PAST_SCOPE
11985  if (p->past_scope_enabled) {
11986  tmp->prev = p->lvtbl->past;
11987  p->lvtbl->past = tmp;
11988  return;
11989  }
11990 # endif
11991  vtable_free(tmp);
11992 }
11993 
11994 static void
11995 dyna_pop_1(struct parser_params *p)
11996 {
11997  struct vtable *tmp;
11998 
11999  if ((tmp = p->lvtbl->used) != 0) {
12000  warn_unused_var(p, p->lvtbl);
12001  p->lvtbl->used = p->lvtbl->used->prev;
12002  vtable_free(tmp);
12003  }
12004  dyna_pop_vtable(p, &p->lvtbl->args);
12005  dyna_pop_vtable(p, &p->lvtbl->vars);
12006 }
12007 
12008 static void
12009 dyna_pop(struct parser_params *p, const struct vtable *lvargs)
12010 {
12011  while (p->lvtbl->args != lvargs) {
12012  dyna_pop_1(p);
12013  if (!p->lvtbl->args) {
12014  struct local_vars *local = p->lvtbl->prev;
12015  ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12016  p->lvtbl = local;
12017  }
12018  }
12019  dyna_pop_1(p);
12020 }
12021 
12022 static int
12023 dyna_in_block(struct parser_params *p)
12024 {
12025  return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12026 }
12027 
12028 static int
12029 dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12030 {
12031  struct vtable *vars, *args, *used;
12032  int i;
12033 
12034  args = p->lvtbl->args;
12035  vars = p->lvtbl->vars;
12036  used = p->lvtbl->used;
12037 
12038  while (!DVARS_TERMINAL_P(vars)) {
12039  if (vtable_included(args, id)) {
12040  return 1;
12041  }
12042  if ((i = vtable_included(vars, id)) != 0) {
12043  if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12044  return 1;
12045  }
12046  args = args->prev;
12047  vars = vars->prev;
12048  if (!vidrefp) used = 0;
12049  if (used) used = used->prev;
12050  }
12051 
12052  if (vars == DVARS_INHERIT) {
12053  return rb_dvar_defined(id, p->parent_iseq);
12054  }
12055 
12056  return 0;
12057 }
12058 
12059 static int
12060 dvar_defined(struct parser_params *p, ID id)
12061 {
12062  return dvar_defined_ref(p, id, NULL);
12063 }
12064 
12065 static int
12066 dvar_curr(struct parser_params *p, ID id)
12067 {
12068  return (vtable_included(p->lvtbl->args, id) ||
12069  vtable_included(p->lvtbl->vars, id));
12070 }
12071 
12072 static void
12073 reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12074 {
12075  compile_error(p,
12076  "regexp encoding option '%c' differs from source encoding '%s'",
12077  c, rb_enc_name(rb_enc_get(str)));
12078 }
12079 
12080 #ifndef RIPPER
12081 int
12082 rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12083 {
12084  int c = RE_OPTION_ENCODING_IDX(options);
12085 
12086  if (c) {
12087  int opt, idx;
12088  rb_char_to_option_kcode(c, &opt, &idx);
12089  if (idx != ENCODING_GET(str) &&
12090  rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12091  goto error;
12092  }
12093  ENCODING_SET(str, idx);
12094  }
12095  else if (RE_OPTION_ENCODING_NONE(options)) {
12096  if (!ENCODING_IS_ASCII8BIT(str) &&
12097  rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12098  c = 'n';
12099  goto error;
12100  }
12101  rb_enc_associate(str, rb_ascii8bit_encoding());
12102  }
12103  else if (p->enc == rb_usascii_encoding()) {
12104  if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12105  /* raise in re.c */
12106  rb_enc_associate(str, rb_usascii_encoding());
12107  }
12108  else {
12109  rb_enc_associate(str, rb_ascii8bit_encoding());
12110  }
12111  }
12112  return 0;
12113 
12114  error:
12115  return c;
12116 }
12117 
12118 static void
12119 reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12120 {
12121  int c = rb_reg_fragment_setenc(p, str, options);
12122  if (c) reg_fragment_enc_error(p, str, c);
12123 }
12124 
12125 static int
12126 reg_fragment_check(struct parser_params* p, VALUE str, int options)
12127 {
12128  VALUE err;
12129  reg_fragment_setenc(p, str, options);
12130  err = rb_reg_check_preprocess(str);
12131  if (err != Qnil) {
12132  err = rb_obj_as_string(err);
12133  compile_error(p, "%"PRIsVALUE, err);
12134  return 0;
12135  }
12136  return 1;
12137 }
12138 
12139 typedef struct {
12140  struct parser_params* parser;
12141  rb_encoding *enc;
12142  NODE *succ_block;
12143  const YYLTYPE *loc;
12144 } reg_named_capture_assign_t;
12145 
12146 static int
12147 reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12148  int back_num, int *back_refs, OnigRegex regex, void *arg0)
12149 {
12150  reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12151  struct parser_params* p = arg->parser;
12152  rb_encoding *enc = arg->enc;
12153  long len = name_end - name;
12154  const char *s = (const char *)name;
12155  ID var;
12156  NODE *node, *succ;
12157 
12158  if (!len) return ST_CONTINUE;
12159  if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len))
12160  return ST_CONTINUE;
12161  if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
12162  return ST_CONTINUE;
12163 
12164  var = intern_cstr(s, len, enc);
12165  node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), arg->loc);
12166  succ = arg->succ_block;
12167  if (!succ) succ = NEW_BEGIN(0, arg->loc);
12168  succ = block_append(p, succ, node);
12169  arg->succ_block = succ;
12170  return ST_CONTINUE;
12171 }
12172 
12173 static NODE *
12174 reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
12175 {
12176  reg_named_capture_assign_t arg;
12177 
12178  arg.parser = p;
12179  arg.enc = rb_enc_get(regexp);
12180  arg.succ_block = 0;
12181  arg.loc = loc;
12182  onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
12183 
12184  if (!arg.succ_block) return 0;
12185  return arg.succ_block->nd_next;
12186 }
12187 
12188 static VALUE
12189 parser_reg_compile(struct parser_params* p, VALUE str, int options)
12190 {
12191  reg_fragment_setenc(p, str, options);
12192  return rb_parser_reg_compile(p, str, options);
12193 }
12194 
12195 VALUE
12196 rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
12197 {
12198  return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
12199 }
12200 
12201 static VALUE
12202 reg_compile(struct parser_params* p, VALUE str, int options)
12203 {
12204  VALUE re;
12205  VALUE err;
12206 
12207  err = rb_errinfo();
12208  re = parser_reg_compile(p, str, options);
12209  if (NIL_P(re)) {
12210  VALUE m = rb_attr_get(rb_errinfo(), idMesg);
12211  rb_set_errinfo(err);
12212  compile_error(p, "%"PRIsVALUE, m);
12213  return Qnil;
12214  }
12215  return re;
12216 }
12217 #else
12218 static VALUE
12219 parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
12220 {
12221  VALUE err = rb_errinfo();
12222  VALUE re;
12223  str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
12224  int c = rb_reg_fragment_setenc(p, str, options);
12225  if (c) reg_fragment_enc_error(p, str, c);
12226  re = rb_parser_reg_compile(p, str, options);
12227  if (NIL_P(re)) {
12228  *errmsg = rb_attr_get(rb_errinfo(), idMesg);
12229  rb_set_errinfo(err);
12230  }
12231  return re;
12232 }
12233 #endif
12234 
12235 #ifndef RIPPER
12236 void
12237 rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
12238 {
12239  struct parser_params *p;
12240  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12241  p->do_print = print;
12242  p->do_loop = loop;
12243  p->do_chomp = chomp;
12244  p->do_split = split;
12245 }
12246 
12247 void
12248 rb_parser_warn_location(VALUE vparser, int warn)
12249 {
12250  struct parser_params *p;
12251  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12252  p->warn_location = warn;
12253 }
12254 
12255 static NODE *
12256 parser_append_options(struct parser_params *p, NODE *node)
12257 {
12258  static const YYLTYPE default_location = {{1, 0}, {1, 0}};
12259  const YYLTYPE *const LOC = &default_location;
12260 
12261  if (p->do_print) {
12262  NODE *print = NEW_FCALL(rb_intern("print"),
12263  NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
12264  LOC);
12265  node = block_append(p, node, print);
12266  }
12267 
12268  if (p->do_loop) {
12269  if (p->do_split) {
12270  NODE *args = NEW_LIST(NEW_GVAR(rb_intern("$;"), LOC), LOC);
12271  NODE *split = NEW_GASGN(rb_intern("$F"),
12272  NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12273  rb_intern("split"), args, LOC),
12274  LOC);
12275  node = block_append(p, split, node);
12276  }
12277  if (p->do_chomp) {
12278  NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12279  rb_intern("chomp!"), 0, LOC);
12280  node = block_append(p, chomp, node);
12281  }
12282 
12283  node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
12284  }
12285 
12286  return node;
12287 }
12288 
12289 void
12290 rb_init_parse(void)
12291 {
12292  /* just to suppress unused-function warnings */
12293  (void)nodetype;
12294  (void)nodeline;
12295 }
12296 
12297 static ID
12298 internal_id(struct parser_params *p)
12299 {
12300  const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
12301  ID id = (ID)vtable_size(p->lvtbl->args) + (ID)vtable_size(p->lvtbl->vars);
12302  id = max_id - id;
12303  return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
12304 }
12305 #endif /* !RIPPER */
12306 
12307 static void
12308 parser_initialize(struct parser_params *p)
12309 {
12310  /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
12311  p->command_start = TRUE;
12312  p->ruby_sourcefile_string = Qnil;
12313  p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
12314  p->node_id = 0;
12315 #ifdef RIPPER
12316  p->delayed.token = Qnil;
12317  p->result = Qnil;
12318  p->parsing_thread = Qnil;
12319 #else
12320  p->error_buffer = Qfalse;
12321 #endif
12322  p->debug_buffer = Qnil;
12323  p->debug_output = rb_stdout;
12324  p->enc = rb_utf8_encoding();
12325 }
12326 
12327 #ifdef RIPPER
12328 #define parser_mark ripper_parser_mark
12329 #define parser_free ripper_parser_free
12330 #endif
12331 
12332 static void
12333 parser_mark(void *ptr)
12334 {
12335  struct parser_params *p = (struct parser_params*)ptr;
12336 
12337  rb_gc_mark(p->lex.input);
12338  rb_gc_mark(p->lex.prevline);
12339  rb_gc_mark(p->lex.lastline);
12340  rb_gc_mark(p->lex.nextline);
12341  rb_gc_mark(p->ruby_sourcefile_string);
12342  rb_gc_mark((VALUE)p->lex.strterm);
12343  rb_gc_mark((VALUE)p->ast);
12344  rb_gc_mark(p->case_labels);
12345 #ifndef RIPPER
12346  rb_gc_mark(p->debug_lines);
12347  rb_gc_mark(p->compile_option);
12348  rb_gc_mark(p->error_buffer);
12349 #else
12350  rb_gc_mark(p->delayed.token);
12351  rb_gc_mark(p->value);
12352  rb_gc_mark(p->result);
12353  rb_gc_mark(p->parsing_thread);
12354 #endif
12355  rb_gc_mark(p->debug_buffer);
12356  rb_gc_mark(p->debug_output);
12357 #ifdef YYMALLOC
12358  rb_gc_mark((VALUE)p->heap);
12359 #endif
12360 }
12361 
12362 static void
12363 parser_free(void *ptr)
12364 {
12365  struct parser_params *p = (struct parser_params*)ptr;
12366  struct local_vars *local, *prev;
12367 
12368  if (p->tokenbuf) {
12369  ruby_sized_xfree(p->tokenbuf, p->toksiz);
12370  }
12371  for (local = p->lvtbl; local; local = prev) {
12372  if (local->vars) xfree(local->vars);
12373  prev = local->prev;
12374  xfree(local);
12375  }
12376  {
12377  token_info *ptinfo;
12378  while ((ptinfo = p->token_info) != 0) {
12379  p->token_info = ptinfo->next;
12380  xfree(ptinfo);
12381  }
12382  }
12383  xfree(ptr);
12384 }
12385 
12386 static size_t
12387 parser_memsize(const void *ptr)
12388 {
12389  struct parser_params *p = (struct parser_params*)ptr;
12390  struct local_vars *local;
12391  size_t size = sizeof(*p);
12392 
12393  size += p->toksiz;
12394  for (local = p->lvtbl; local; local = local->prev) {
12395  size += sizeof(*local);
12396  if (local->vars) size += local->vars->capa * sizeof(ID);
12397  }
12398  return size;
12399 }
12400 
12401 static const rb_data_type_t parser_data_type = {
12402 #ifndef RIPPER
12403  "parser",
12404 #else
12405  "ripper",
12406 #endif
12407  {
12408  parser_mark,
12409  parser_free,
12410  parser_memsize,
12411  },
12412  0, 0, RUBY_TYPED_FREE_IMMEDIATELY
12413 };
12414 
12415 #ifndef RIPPER
12416 #undef rb_reserved_word
12417 
12418 const struct kwtable *
12419 rb_reserved_word(const char *str, unsigned int len)
12420 {
12421  return reserved_word(str, len);
12422 }
12423 
12424 VALUE
12425 rb_parser_new(void)
12426 {
12427  struct parser_params *p;
12428  VALUE parser = TypedData_Make_Struct(0, struct parser_params,
12429  &parser_data_type, p);
12430  parser_initialize(p);
12431  return parser;
12432 }
12433 
12434 VALUE
12435 rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
12436 {
12437  struct parser_params *p;
12438 
12439  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12440  p->error_buffer = main ? Qfalse : Qnil;
12441  p->parent_iseq = base;
12442  return vparser;
12443 }
12444 #endif
12445 
12446 #ifdef RIPPER
12447 #define rb_parser_end_seen_p ripper_parser_end_seen_p
12448 #define rb_parser_encoding ripper_parser_encoding
12449 #define rb_parser_get_yydebug ripper_parser_get_yydebug
12450 #define rb_parser_set_yydebug ripper_parser_set_yydebug
12451 #define rb_parser_get_debug_output ripper_parser_get_debug_output
12452 #define rb_parser_set_debug_output ripper_parser_set_debug_output
12453 static VALUE ripper_parser_end_seen_p(VALUE vparser);
12454 static VALUE ripper_parser_encoding(VALUE vparser);
12455 static VALUE ripper_parser_get_yydebug(VALUE self);
12456 static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
12457 static VALUE ripper_parser_get_debug_output(VALUE self);
12458 static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
12459 
12460 /*
12461  * call-seq:
12462  * ripper.error? -> Boolean
12463  *
12464  * Return true if parsed source has errors.
12465  */
12466 static VALUE
12467 ripper_error_p(VALUE vparser)
12468 {
12469  struct parser_params *p;
12470 
12471  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12472  return p->error_p ? Qtrue : Qfalse;
12473 }
12474 #endif
12475 
12476 /*
12477  * call-seq:
12478  * ripper.end_seen? -> Boolean
12479  *
12480  * Return true if parsed source ended by +\_\_END\_\_+.
12481  */
12482 VALUE
12483 rb_parser_end_seen_p(VALUE vparser)
12484 {
12485  struct parser_params *p;
12486 
12487  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12488  return p->ruby__end__seen ? Qtrue : Qfalse;
12489 }
12490 
12491 /*
12492  * call-seq:
12493  * ripper.encoding -> encoding
12494  *
12495  * Return encoding of the source.
12496  */
12497 VALUE
12498 rb_parser_encoding(VALUE vparser)
12499 {
12500  struct parser_params *p;
12501 
12502  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12503  return rb_enc_from_encoding(p->enc);
12504 }
12505 
12506 #ifdef RIPPER
12507 /*
12508  * call-seq:
12509  * ripper.yydebug -> true or false
12510  *
12511  * Get yydebug.
12512  */
12513 VALUE
12514 rb_parser_get_yydebug(VALUE self)
12515 {
12516  struct parser_params *p;
12517 
12518  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12519  return p->debug ? Qtrue : Qfalse;
12520 }
12521 #endif
12522 
12523 /*
12524  * call-seq:
12525  * ripper.yydebug = flag
12526  *
12527  * Set yydebug.
12528  */
12529 VALUE
12530 rb_parser_set_yydebug(VALUE self, VALUE flag)
12531 {
12532  struct parser_params *p;
12533 
12534  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12535  p->debug = RTEST(flag);
12536  return flag;
12537 }
12538 
12539 /*
12540  * call-seq:
12541  * ripper.debug_output -> obj
12542  *
12543  * Get debug output.
12544  */
12545 VALUE
12546 rb_parser_get_debug_output(VALUE self)
12547 {
12548  struct parser_params *p;
12549 
12550  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12551  return p->debug_output;
12552 }
12553 
12554 /*
12555  * call-seq:
12556  * ripper.debug_output = obj
12557  *
12558  * Set debug output.
12559  */
12560 VALUE
12561 rb_parser_set_debug_output(VALUE self, VALUE output)
12562 {
12563  struct parser_params *p;
12564 
12565  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12566  return p->debug_output = output;
12567 }
12568 
12569 #ifndef RIPPER
12570 #ifdef YYMALLOC
12571 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
12572 /* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
12573  * potential memory leak */
12574 #define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
12575 #define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
12576  (new)->cnt = (cnt), (ptr))
12577 
12578 void *
12579 rb_parser_malloc(struct parser_params *p, size_t size)
12580 {
12581  size_t cnt = HEAPCNT(1, size);
12582  rb_imemo_tmpbuf_t *n = NEWHEAP();
12583  void *ptr = xmalloc(size);
12584 
12585  return ADD2HEAP(n, cnt, ptr);
12586 }
12587 
12588 void *
12589 rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
12590 {
12591  size_t cnt = HEAPCNT(nelem, size);
12592  rb_imemo_tmpbuf_t *n = NEWHEAP();
12593  void *ptr = xcalloc(nelem, size);
12594 
12595  return ADD2HEAP(n, cnt, ptr);
12596 }
12597 
12598 void *
12599 rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
12600 {
12601  rb_imemo_tmpbuf_t *n;
12602  size_t cnt = HEAPCNT(1, size);
12603 
12604  if (ptr && (n = p->heap) != NULL) {
12605  do {
12606  if (n->ptr == ptr) {
12607  n->ptr = ptr = xrealloc(ptr, size);
12608  if (n->cnt) n->cnt = cnt;
12609  return ptr;
12610  }
12611  } while ((n = n->next) != NULL);
12612  }
12613  n = NEWHEAP();
12614  ptr = xrealloc(ptr, size);
12615  return ADD2HEAP(n, cnt, ptr);
12616 }
12617 
12618 void
12619 rb_parser_free(struct parser_params *p, void *ptr)
12620 {
12621  rb_imemo_tmpbuf_t **prev = &p->heap, *n;
12622 
12623  while ((n = *prev) != NULL) {
12624  if (n->ptr == ptr) {
12625  *prev = n->next;
12626  rb_gc_force_recycle((VALUE)n);
12627  break;
12628  }
12629  prev = &n->next;
12630  }
12631  xfree(ptr);
12632 }
12633 #endif
12634 
12635 void
12636 rb_parser_printf(struct parser_params *p, const char *fmt, ...)
12637 {
12638  va_list ap;
12639  VALUE mesg = p->debug_buffer;
12640 
12641  if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
12642  va_start(ap, fmt);
12643  rb_str_vcatf(mesg, fmt, ap);
12644  va_end(ap);
12645  if (RSTRING_END(mesg)[-1] == '\n') {
12646  rb_io_write(p->debug_output, mesg);
12647  p->debug_buffer = Qnil;
12648  }
12649 }
12650 
12651 static void
12652 parser_compile_error(struct parser_params *p, const char *fmt, ...)
12653 {
12654  va_list ap;
12655 
12656  rb_io_flush(p->debug_output);
12657  p->error_p = 1;
12658  va_start(ap, fmt);
12659  p->error_buffer =
12660  rb_syntax_error_append(p->error_buffer,
12661  p->ruby_sourcefile_string,
12662  p->ruby_sourceline,
12663  rb_long2int(p->lex.pcur - p->lex.pbeg),
12664  p->enc, fmt, ap);
12665  va_end(ap);
12666 }
12667 
12668 static size_t
12669 count_char(const char *str, int c)
12670 {
12671  int n = 0;
12672  while (str[n] == c) ++n;
12673  return n;
12674 }
12675 
12676 /*
12677  * strip enclosing double-quotes, same as the default yytnamerr except
12678  * for that single-quotes matching back-quotes do not stop stripping.
12679  *
12680  * "\"`class' keyword\"" => "`class' keyword"
12681  */
12682 RUBY_FUNC_EXPORTED size_t
12683 rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
12684 {
12685  YYUSE(p);
12686  if (*yystr == '"') {
12687  size_t yyn = 0, bquote = 0;
12688  const char *yyp = yystr;
12689 
12690  while (*++yyp) {
12691  switch (*yyp) {
12692  case '`':
12693  if (!bquote) {
12694  bquote = count_char(yyp+1, '`') + 1;
12695  if (yyres) memcpy(&yyres[yyn], yyp, bquote);
12696  yyn += bquote;
12697  yyp += bquote - 1;
12698  break;
12699  }
12700  goto default_char;
12701 
12702  case '\'':
12703  if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
12704  if (yyres) memcpy(yyres + yyn, yyp, bquote);
12705  yyn += bquote;
12706  yyp += bquote - 1;
12707  bquote = 0;
12708  break;
12709  }
12710  if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
12711  if (yyres) memcpy(yyres + yyn, yyp, 3);
12712  yyn += 3;
12713  yyp += 2;
12714  break;
12715  }
12716  goto do_not_strip_quotes;
12717 
12718  case ',':
12719  goto do_not_strip_quotes;
12720 
12721  case '\\':
12722  if (*++yyp != '\\')
12723  goto do_not_strip_quotes;
12724  /* Fall through. */
12725  default_char:
12726  default:
12727  if (yyres)
12728  yyres[yyn] = *yyp;
12729  yyn++;
12730  break;
12731 
12732  case '"':
12733  case '\0':
12734  if (yyres)
12735  yyres[yyn] = '\0';
12736  return yyn;
12737  }
12738  }
12739  do_not_strip_quotes: ;
12740  }
12741 
12742  if (!yyres) return strlen(yystr);
12743 
12744  return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
12745 }
12746 #endif
12747 
12748 #ifdef RIPPER
12749 #ifdef RIPPER_DEBUG
12750 /* :nodoc: */
12751 static VALUE
12752 ripper_validate_object(VALUE self, VALUE x)
12753 {
12754  if (x == Qfalse) return x;
12755  if (x == Qtrue) return x;
12756  if (x == Qnil) return x;
12757  if (x == Qundef)
12758  rb_raise(rb_eArgError, "Qundef given");
12759  if (FIXNUM_P(x)) return x;
12760  if (SYMBOL_P(x)) return x;
12761  switch (BUILTIN_TYPE(x)) {
12762  case T_STRING:
12763  case T_OBJECT:
12764  case T_ARRAY:
12765  case T_BIGNUM:
12766  case T_FLOAT:
12767  case T_COMPLEX:
12768  case T_RATIONAL:
12769  break;
12770  case T_NODE:
12771  if (nd_type((NODE *)x) != NODE_RIPPER) {
12772  rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
12773  }
12774  x = ((NODE *)x)->nd_rval;
12775  break;
12776  default:
12777  rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
12778  (void *)x, rb_obj_classname(x));
12779  }
12780  if (!RBASIC_CLASS(x)) {
12781  rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
12782  (void *)x, rb_builtin_type_name(TYPE(x)));
12783  }
12784  return x;
12785 }
12786 #endif
12787 
12788 #define validate(x) ((x) = get_value(x))
12789 
12790 static VALUE
12791 ripper_dispatch0(struct parser_params *p, ID mid)
12792 {
12793  return rb_funcall(p->value, mid, 0);
12794 }
12795 
12796 static VALUE
12797 ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
12798 {
12799  validate(a);
12800  return rb_funcall(p->value, mid, 1, a);
12801 }
12802 
12803 static VALUE
12804 ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
12805 {
12806  validate(a);
12807  validate(b);
12808  return rb_funcall(p->value, mid, 2, a, b);
12809 }
12810 
12811 static VALUE
12812 ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
12813 {
12814  validate(a);
12815  validate(b);
12816  validate(c);
12817  return rb_funcall(p->value, mid, 3, a, b, c);
12818 }
12819 
12820 static VALUE
12821 ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
12822 {
12823  validate(a);
12824  validate(b);
12825  validate(c);
12826  validate(d);
12827  return rb_funcall(p->value, mid, 4, a, b, c, d);
12828 }
12829 
12830 static VALUE
12831 ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
12832 {
12833  validate(a);
12834  validate(b);
12835  validate(c);
12836  validate(d);
12837  validate(e);
12838  return rb_funcall(p->value, mid, 5, a, b, c, d, e);
12839 }
12840 
12841 static VALUE
12842 ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
12843 {
12844  validate(a);
12845  validate(b);
12846  validate(c);
12847  validate(d);
12848  validate(e);
12849  validate(f);
12850  validate(g);
12851  return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
12852 }
12853 
12854 static ID
12855 ripper_get_id(VALUE v)
12856 {
12857  NODE *nd;
12858  if (!RB_TYPE_P(v, T_NODE)) return 0;
12859  nd = (NODE *)v;
12860  if (nd_type(nd) != NODE_RIPPER) return 0;
12861  return nd->nd_vid;
12862 }
12863 
12864 static VALUE
12865 ripper_get_value(VALUE v)
12866 {
12867  NODE *nd;
12868  if (v == Qundef) return Qnil;
12869  if (!RB_TYPE_P(v, T_NODE)) return v;
12870  nd = (NODE *)v;
12871  if (nd_type(nd) != NODE_RIPPER) return Qnil;
12872  return nd->nd_rval;
12873 }
12874 
12875 static void
12876 ripper_error(struct parser_params *p)
12877 {
12878  p->error_p = TRUE;
12879 }
12880 
12881 static void
12882 ripper_compile_error(struct parser_params *p, const char *fmt, ...)
12883 {
12884  VALUE str;
12885  va_list args;
12886 
12887  va_start(args, fmt);
12888  str = rb_vsprintf(fmt, args);
12889  va_end(args);
12890  rb_funcall(p->value, rb_intern("compile_error"), 1, str);
12891  ripper_error(p);
12892 }
12893 
12894 static VALUE
12895 ripper_lex_get_generic(struct parser_params *p, VALUE src)
12896 {
12897  VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
12898  if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
12899  rb_raise(rb_eTypeError,
12900  "gets returned %"PRIsVALUE" (expected String or nil)",
12901  rb_obj_class(line));
12902  }
12903  return line;
12904 }
12905 
12906 static VALUE
12907 ripper_lex_io_get(struct parser_params *p, VALUE src)
12908 {
12909  return rb_io_gets(src);
12910 }
12911 
12912 static VALUE
12913 ripper_s_allocate(VALUE klass)
12914 {
12915  struct parser_params *p;
12916  VALUE self = TypedData_Make_Struct(klass, struct parser_params,
12917  &parser_data_type, p);
12918  p->value = self;
12919  return self;
12920 }
12921 
12922 #define ripper_initialized_p(r) ((r)->lex.input != 0)
12923 
12924 /*
12925  * call-seq:
12926  * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
12927  *
12928  * Create a new Ripper object.
12929  * _src_ must be a String, an IO, or an Object which has #gets method.
12930  *
12931  * This method does not starts parsing.
12932  * See also Ripper#parse and Ripper.parse.
12933  */
12934 static VALUE
12935 ripper_initialize(int argc, VALUE *argv, VALUE self)
12936 {
12937  struct parser_params *p;
12938  VALUE src, fname, lineno;
12939 
12940  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12941  rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
12942  if (RB_TYPE_P(src, T_FILE)) {
12943  p->lex.gets = ripper_lex_io_get;
12944  }
12945  else if (rb_respond_to(src, id_gets)) {
12946  p->lex.gets = ripper_lex_get_generic;
12947  }
12948  else {
12949  StringValue(src);
12950  p->lex.gets = lex_get_str;
12951  }
12952  p->lex.input = src;
12953  p->eofp = 0;
12954  if (NIL_P(fname)) {
12955  fname = STR_NEW2("(ripper)");
12956  OBJ_FREEZE(fname);
12957  }
12958  else {
12959  StringValueCStr(fname);
12960  fname = rb_str_new_frozen(fname);
12961  }
12962  parser_initialize(p);
12963 
12964  p->ruby_sourcefile_string = fname;
12965  p->ruby_sourcefile = RSTRING_PTR(fname);
12966  p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
12967 
12968  return Qnil;
12969 }
12970 
12971 static VALUE
12972 ripper_parse0(VALUE parser_v)
12973 {
12974  struct parser_params *p;
12975 
12976  TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
12977  parser_prepare(p);
12978  p->ast = rb_ast_new();
12979  ripper_yyparse((void*)p);
12980  rb_ast_dispose(p->ast);
12981  p->ast = 0;
12982  return p->result;
12983 }
12984 
12985 static VALUE
12986 ripper_ensure(VALUE parser_v)
12987 {
12988  struct parser_params *p;
12989 
12990  TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
12991  p->parsing_thread = Qnil;
12992  return Qnil;
12993 }
12994 
12995 /*
12996  * call-seq:
12997  * ripper.parse
12998  *
12999  * Start parsing and returns the value of the root action.
13000  */
13001 static VALUE
13002 ripper_parse(VALUE self)
13003 {
13004  struct parser_params *p;
13005 
13006  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13007  if (!ripper_initialized_p(p)) {
13008  rb_raise(rb_eArgError, "method called for uninitialized object");
13009  }
13010  if (!NIL_P(p->parsing_thread)) {
13011  if (p->parsing_thread == rb_thread_current())
13012  rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13013  else
13014  rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13015  }
13016  p->parsing_thread = rb_thread_current();
13017  rb_ensure(ripper_parse0, self, ripper_ensure, self);
13018 
13019  return p->result;
13020 }
13021 
13022 /*
13023  * call-seq:
13024  * ripper.column -> Integer
13025  *
13026  * Return column number of current parsing line.
13027  * This number starts from 0.
13028  */
13029 static VALUE
13030 ripper_column(VALUE self)
13031 {
13032  struct parser_params *p;
13033  long col;
13034 
13035  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13036  if (!ripper_initialized_p(p)) {
13037  rb_raise(rb_eArgError, "method called for uninitialized object");
13038  }
13039  if (NIL_P(p->parsing_thread)) return Qnil;
13040  col = p->lex.ptok - p->lex.pbeg;
13041  return LONG2NUM(col);
13042 }
13043 
13044 /*
13045  * call-seq:
13046  * ripper.filename -> String
13047  *
13048  * Return current parsing filename.
13049  */
13050 static VALUE
13051 ripper_filename(VALUE self)
13052 {
13053  struct parser_params *p;
13054 
13055  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13056  if (!ripper_initialized_p(p)) {
13057  rb_raise(rb_eArgError, "method called for uninitialized object");
13058  }
13059  return p->ruby_sourcefile_string;
13060 }
13061 
13062 /*
13063  * call-seq:
13064  * ripper.lineno -> Integer
13065  *
13066  * Return line number of current parsing line.
13067  * This number starts from 1.
13068  */
13069 static VALUE
13070 ripper_lineno(VALUE self)
13071 {
13072  struct parser_params *p;
13073 
13074  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13075  if (!ripper_initialized_p(p)) {
13076  rb_raise(rb_eArgError, "method called for uninitialized object");
13077  }
13078  if (NIL_P(p->parsing_thread)) return Qnil;
13079  return INT2NUM(p->ruby_sourceline);
13080 }
13081 
13082 /*
13083  * call-seq:
13084  * ripper.state -> Integer
13085  *
13086  * Return scanner state of current token.
13087  */
13088 static VALUE
13089 ripper_state(VALUE self)
13090 {
13091  struct parser_params *p;
13092 
13093  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13094  if (!ripper_initialized_p(p)) {
13095  rb_raise(rb_eArgError, "method called for uninitialized object");
13096  }
13097  if (NIL_P(p->parsing_thread)) return Qnil;
13098  return INT2NUM(p->lex.state);
13099 }
13100 
13101 /*
13102  * call-seq:
13103  * ripper.token -> String
13104  *
13105  * Return the current token string.
13106  */
13107 static VALUE
13108 ripper_token(VALUE self)
13109 {
13110  struct parser_params *p;
13111  long pos, len;
13112 
13113  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13114  if (!ripper_initialized_p(p)) {
13115  rb_raise(rb_eArgError, "method called for uninitialized object");
13116  }
13117  if (NIL_P(p->parsing_thread)) return Qnil;
13118  pos = p->lex.ptok - p->lex.pbeg;
13119  len = p->lex.pcur - p->lex.ptok;
13120  return rb_str_subseq(p->lex.lastline, pos, len);
13121 }
13122 
13123 #ifdef RIPPER_DEBUG
13124 /* :nodoc: */
13125 static VALUE
13126 ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13127 {
13128  StringValue(msg);
13129  if (obj == Qundef) {
13130  rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13131  }
13132  return Qnil;
13133 }
13134 
13135 /* :nodoc: */
13136 static VALUE
13137 ripper_value(VALUE self, VALUE obj)
13138 {
13139  return ULONG2NUM(obj);
13140 }
13141 #endif
13142 
13143 /*
13144  * call-seq:
13145  * Ripper.lex_state_name(integer) -> string
13146  *
13147  * Returns a string representation of lex_state.
13148  */
13149 static VALUE
13150 ripper_lex_state_name(VALUE self, VALUE state)
13151 {
13152  return rb_parser_lex_state_name(NUM2INT(state));
13153 }
13154 
13155 void
13156 Init_ripper(void)
13157 {
13158  ripper_init_eventids1();
13159  ripper_init_eventids2();
13160  id_warn = rb_intern_const("warn");
13161  id_warning = rb_intern_const("warning");
13162  id_gets = rb_intern_const("gets");
13163  id_assoc = rb_intern_const("=>");
13164 
13165  (void)yystpcpy; /* may not used in newer bison */
13166 
13167  InitVM(ripper);
13168 }
13169 
13170 void
13171 InitVM_ripper(void)
13172 {
13173  VALUE Ripper;
13174 
13175  Ripper = rb_define_class("Ripper", rb_cObject);
13176  /* version of Ripper */
13177  rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
13178  rb_define_alloc_func(Ripper, ripper_s_allocate);
13179  rb_define_method(Ripper, "initialize", ripper_initialize, -1);
13180  rb_define_method(Ripper, "parse", ripper_parse, 0);
13181  rb_define_method(Ripper, "column", ripper_column, 0);
13182  rb_define_method(Ripper, "filename", ripper_filename, 0);
13183  rb_define_method(Ripper, "lineno", ripper_lineno, 0);
13184  rb_define_method(Ripper, "state", ripper_state, 0);
13185  rb_define_method(Ripper, "token", ripper_token, 0);
13186  rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
13187  rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
13188  rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
13189  rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
13190  rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
13191  rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
13192  rb_define_method(Ripper, "error?", ripper_error_p, 0);
13193 #ifdef RIPPER_DEBUG
13194  rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
13195  rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
13196  rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
13197 #endif
13198 
13199  rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
13200  rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
13201 
13202  rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
13203 
13204  /* ignore newline, +/- is a sign. */
13205  rb_define_const(Ripper, "EXPR_BEG", INT2NUM(EXPR_BEG));
13206  /* newline significant, +/- is an operator. */
13207  rb_define_const(Ripper, "EXPR_END", INT2NUM(EXPR_END));
13208  /* ditto, and unbound braces. */
13209  rb_define_const(Ripper, "EXPR_ENDARG", INT2NUM(EXPR_ENDARG));
13210  /* ditto, and unbound braces. */
13211  rb_define_const(Ripper, "EXPR_ENDFN", INT2NUM(EXPR_ENDFN));
13212  /* newline significant, +/- is an operator. */
13213  rb_define_const(Ripper, "EXPR_ARG", INT2NUM(EXPR_ARG));
13214  /* newline significant, +/- is an operator. */
13215  rb_define_const(Ripper, "EXPR_CMDARG", INT2NUM(EXPR_CMDARG));
13216  /* newline significant, +/- is an operator. */
13217  rb_define_const(Ripper, "EXPR_MID", INT2NUM(EXPR_MID));
13218  /* ignore newline, no reserved words. */
13219  rb_define_const(Ripper, "EXPR_FNAME", INT2NUM(EXPR_FNAME));
13220  /* right after `.' or `::', no reserved words. */
13221  rb_define_const(Ripper, "EXPR_DOT", INT2NUM(EXPR_DOT));
13222  /* immediate after `class', no here document. */
13223  rb_define_const(Ripper, "EXPR_CLASS", INT2NUM(EXPR_CLASS));
13224  /* flag bit, label is allowed. */
13225  rb_define_const(Ripper, "EXPR_LABEL", INT2NUM(EXPR_LABEL));
13226  /* flag bit, just after a label. */
13227  rb_define_const(Ripper, "EXPR_LABELED", INT2NUM(EXPR_LABELED));
13228  /* symbol literal as FNAME. */
13229  rb_define_const(Ripper, "EXPR_FITEM", INT2NUM(EXPR_FITEM));
13230  /* equals to +EXPR_BEG+ */
13231  rb_define_const(Ripper, "EXPR_VALUE", INT2NUM(EXPR_VALUE));
13232  /* equals to <tt>(EXPR_BEG | EXPR_MID | EXPR_CLASS)</tt> */
13233  rb_define_const(Ripper, "EXPR_BEG_ANY", INT2NUM(EXPR_BEG_ANY));
13234  /* equals to <tt>(EXPR_ARG | EXPR_CMDARG)</tt> */
13235  rb_define_const(Ripper, "EXPR_ARG_ANY", INT2NUM(EXPR_ARG_ANY));
13236  /* equals to <tt>(EXPR_END | EXPR_ENDARG | EXPR_ENDFN)</tt> */
13237  rb_define_const(Ripper, "EXPR_END_ANY", INT2NUM(EXPR_END_ANY));
13238  /* equals to +0+ */
13239  rb_define_const(Ripper, "EXPR_NONE", INT2NUM(EXPR_NONE));
13240 
13241  ripper_init_eventids1_table(Ripper);
13242  ripper_init_eventids2_table(Ripper);
13243 
13244 # if 0
13245  /* Hack to let RDoc document SCRIPT_LINES__ */
13246 
13247  /*
13248  * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
13249  * after the assignment will be added as an Array of lines with the file
13250  * name as the key.
13251  */
13252  rb_define_global_const("SCRIPT_LINES__", Qnil);
13253 #endif
13254 
13255 }
13256 #endif /* RIPPER */
13257 
13258 /*
13259  * Local variables:
13260  * mode: c
13261  * c-file-style: "ruby"
13262  * End:
13263  */