Ruby  2.4.2p198(2017-09-14revision59899)
parse.y
Go to the documentation of this file.
1 /**********************************************************************
2 
3  parse.y -
4 
5  $Author: nagachika $
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 #ifndef PARSER_DEBUG
18 #define PARSER_DEBUG 0
19 #endif
20 #define YYDEBUG 1
21 #define YYERROR_VERBOSE 1
22 #define YYSTACK_USE_ALLOCA 0
23 
24 #include "ruby/ruby.h"
25 #include "ruby/st.h"
26 #include "ruby/encoding.h"
27 #include "internal.h"
28 #include "node.h"
29 #include "parse.h"
30 #include "symbol.h"
31 #include "regenc.h"
32 #include <stdio.h>
33 #include <errno.h>
34 #include <ctype.h>
35 #include "probes.h"
36 
37 #ifndef WARN_PAST_SCOPE
38 # define WARN_PAST_SCOPE 0
39 #endif
40 
41 #define TAB_WIDTH 8
42 
43 #define YYMALLOC(size) rb_parser_malloc(parser, (size))
44 #define YYREALLOC(ptr, size) rb_parser_realloc(parser, (ptr), (size))
45 #define YYCALLOC(nelem, size) rb_parser_calloc(parser, (nelem), (size))
46 #define YYFREE(ptr) rb_parser_free(parser, (ptr))
47 #define YYFPRINTF rb_parser_printf
48 #if defined YYLTYPE_IS_TRIVIAL && YYLTYPE_IS_TRIVIAL
49 # define YY_LOCATION_PRINT(File, Loc) \
50  rb_parser_printf(parser, "%d.%d-%d.%d", \
51  (Loc).first_line, (Loc).first_column, \
52  (Loc).last_line, (Loc).last_column)
53 #endif
54 #undef malloc
55 #undef realloc
56 #undef calloc
57 #undef free
58 #define malloc YYMALLOC
59 #define realloc YYREALLOC
60 #define calloc YYCALLOC
61 #define free YYFREE
62 
63 enum lex_state_bits {
64  EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
65  EXPR_END_bit, /* newline significant, +/- is an operator. */
66  EXPR_ENDARG_bit, /* ditto, and unbound braces. */
67  EXPR_ENDFN_bit, /* ditto, and unbound braces. */
68  EXPR_ARG_bit, /* newline significant, +/- is an operator. */
69  EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
70  EXPR_MID_bit, /* newline significant, +/- is an operator. */
71  EXPR_FNAME_bit, /* ignore newline, no reserved words. */
72  EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
73  EXPR_CLASS_bit, /* immediate after `class', no here document. */
74  EXPR_LABEL_bit, /* flag bit, label is allowed. */
75  EXPR_LABELED_bit, /* flag bit, just after a label. */
76  EXPR_FITEM_bit, /* symbol literal as FNAME. */
77  EXPR_MAX_STATE
78 };
79 /* examine combinations */
80 enum lex_state_e {
81 #define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
82  DEF_EXPR(BEG),
83  DEF_EXPR(END),
84  DEF_EXPR(ENDARG),
85  DEF_EXPR(ENDFN),
86  DEF_EXPR(ARG),
87  DEF_EXPR(CMDARG),
88  DEF_EXPR(MID),
89  DEF_EXPR(FNAME),
90  DEF_EXPR(DOT),
91  DEF_EXPR(CLASS),
92  DEF_EXPR(LABEL),
93  DEF_EXPR(LABELED),
94  DEF_EXPR(FITEM),
95  EXPR_VALUE = EXPR_BEG,
96  EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
97  EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
98  EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN)
99 };
100 #define IS_lex_state_for(x, ls) ((x) & (ls))
101 #define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
102 #define IS_lex_state(ls) IS_lex_state_for(lex_state, (ls))
103 #define IS_lex_state_all(ls) IS_lex_state_all_for(lex_state, (ls))
104 
105 # define SET_LEX_STATE(ls) \
106  (lex_state = (yydebug ? trace_lex_state(lex_state, (ls), __LINE__) : \
107  (enum lex_state_e)(ls)))
108 static enum lex_state_e trace_lex_state(enum lex_state_e from, enum lex_state_e to, int line);
109 
110 typedef VALUE stack_type;
111 
112 static void show_bitstack(stack_type, const char *, int);
113 # define SHOW_BITSTACK(stack, name) (yydebug ? show_bitstack(stack, name, __LINE__) : (void)0)
114 # define BITSTACK_PUSH(stack, n) (((stack) = ((stack)<<1)|((n)&1)), SHOW_BITSTACK(stack, #stack"(push)"))
115 # define BITSTACK_POP(stack) (((stack) = (stack) >> 1), SHOW_BITSTACK(stack, #stack"(pop)"))
116 # define BITSTACK_LEXPOP(stack) (((stack) = ((stack) >> 1) | ((stack) & 1)), SHOW_BITSTACK(stack, #stack"(lexpop)"))
117 # define BITSTACK_SET_P(stack) (SHOW_BITSTACK(stack, #stack), (stack)&1)
118 # define BITSTACK_SET(stack, n) ((stack)=(n), SHOW_BITSTACK(stack, #stack"(set)"))
119 
120 #define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
121 #define COND_POP() BITSTACK_POP(cond_stack)
122 #define COND_LEXPOP() BITSTACK_LEXPOP(cond_stack)
123 #define COND_P() BITSTACK_SET_P(cond_stack)
124 #define COND_SET(n) BITSTACK_SET(cond_stack, (n))
125 
126 #define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
127 #define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
128 #define CMDARG_LEXPOP() BITSTACK_LEXPOP(cmdarg_stack)
129 #define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
130 #define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
131 
132 struct vtable {
133  ID *tbl;
134  int pos;
135  int capa;
136  struct vtable *prev;
137 };
138 
139 struct local_vars {
140  struct vtable *args;
141  struct vtable *vars;
142  struct vtable *used;
143 # if WARN_PAST_SCOPE
144  struct vtable *past;
145 # endif
146  struct local_vars *prev;
147  stack_type cmdargs;
148 };
149 
150 #define DVARS_INHERIT ((void*)1)
151 #define DVARS_TOPSCOPE NULL
152 #define DVARS_SPECIAL_P(tbl) (!POINTER_P(tbl))
153 #define POINTER_P(val) ((VALUE)(val) & ~(VALUE)3)
154 
155 static int
156 vtable_size(const struct vtable *tbl)
157 {
158  if (POINTER_P(tbl)) {
159  return tbl->pos;
160  }
161  else {
162  return 0;
163  }
164 }
165 
166 #define VTBL_DEBUG 0
167 
168 static struct vtable *
169 vtable_alloc(struct vtable *prev)
170 {
171  struct vtable *tbl = ALLOC(struct vtable);
172  tbl->pos = 0;
173  tbl->capa = 8;
174  tbl->tbl = ALLOC_N(ID, tbl->capa);
175  tbl->prev = prev;
176  if (VTBL_DEBUG) printf("vtable_alloc: %p\n", (void *)tbl);
177  return tbl;
178 }
179 
180 static void
181 vtable_free(struct vtable *tbl)
182 {
183  if (VTBL_DEBUG)printf("vtable_free: %p\n", (void *)tbl);
184  if (POINTER_P(tbl)) {
185  if (tbl->tbl) {
186  xfree(tbl->tbl);
187  }
188  xfree(tbl);
189  }
190 }
191 
192 static void
193 vtable_add(struct vtable *tbl, ID id)
194 {
195  if (!POINTER_P(tbl)) {
196  rb_bug("vtable_add: vtable is not allocated (%p)", (void *)tbl);
197  }
198  if (VTBL_DEBUG) printf("vtable_add: %p, %"PRIsVALUE"\n", (void *)tbl, rb_id2str(id));
199 
200  if (tbl->pos == tbl->capa) {
201  tbl->capa = tbl->capa * 2;
202  REALLOC_N(tbl->tbl, ID, tbl->capa);
203  }
204  tbl->tbl[tbl->pos++] = id;
205 }
206 
207 #ifndef RIPPER
208 static void
209 vtable_pop(struct vtable *tbl, int n)
210 {
211  if (tbl->pos < n) rb_bug("vtable_pop: unreachable");
212  tbl->pos -= n;
213 }
214 #endif
215 
216 static int
217 vtable_included(const struct vtable * tbl, ID id)
218 {
219  int i;
220 
221  if (POINTER_P(tbl)) {
222  for (i = 0; i < tbl->pos; i++) {
223  if (tbl->tbl[i] == id) {
224  return i+1;
225  }
226  }
227  }
228  return 0;
229 }
230 
231 typedef struct token_info {
232  const char *token;
233  int linenum;
234  int column;
235  int nonspc;
236  struct token_info *next;
237 } token_info;
238 
239 /*
240  Structure of Lexer Buffer:
241 
242  lex_pbeg tokp lex_p lex_pend
243  | | | |
244  |-----------+--------------+------------|
245  |<------------>|
246  token
247 */
248 struct parser_params {
249  NODE *heap;
250 
251  YYSTYPE *lval;
252 
253  struct {
254  NODE *strterm;
255  VALUE (*gets)(struct parser_params*,VALUE);
256  VALUE input;
257  VALUE lastline;
258  VALUE nextline;
259  const char *pbeg;
260  const char *pcur;
261  const char *pend;
262  long gets_ptr;
263  enum lex_state_e state;
264  int paren_nest;
265  int lpar_beg;
266  int brace_nest;
267  } lex;
268  stack_type cond_stack;
269  stack_type cmdarg_stack;
270  int tokidx;
271  int toksiz;
272  int tokline;
273  int heredoc_end;
274  int heredoc_indent;
275  int heredoc_line_indent;
276  char *tokenbuf;
277  struct local_vars *lvtbl;
278  int line_count;
279  int ruby_sourceline; /* current line no. */
280  char *ruby_sourcefile; /* current source file */
281  VALUE ruby_sourcefile_string;
282  rb_encoding *enc;
283  token_info *token_info;
284  VALUE compile_option;
285 
286  VALUE debug_buffer;
287 
288  ID cur_arg;
289 
290  unsigned int command_start:1;
291  unsigned int eofp: 1;
292  unsigned int ruby__end__seen: 1;
293  unsigned int yydebug: 1;
294  unsigned int has_shebang: 1;
295  unsigned int in_defined: 1;
296  unsigned int in_main: 1;
297  unsigned int in_kwarg: 1;
298  unsigned int in_single: 1;
299  unsigned int in_def: 1;
300  unsigned int token_seen: 1;
301  unsigned int token_info_enabled: 1;
302 # if WARN_PAST_SCOPE
303  unsigned int past_scope_enabled: 1;
304 # endif
305  unsigned int error_p: 1;
306  unsigned int cr_seen: 1;
307 
308 #ifndef RIPPER
309  /* Ruby core only */
310 
311  NODE *eval_tree_begin;
312  NODE *eval_tree;
313  VALUE error_buffer;
314  VALUE debug_lines;
315  VALUE coverage;
316  const struct rb_block *base_block;
317 #else
318  /* Ripper only */
319 
320  const char *tokp;
321  VALUE delayed;
322  int delayed_line;
323  int delayed_col;
324 
325  VALUE value;
326  VALUE result;
327  VALUE parsing_thread;
328 #endif
329 };
330 
331 #ifdef RIPPER
332 #define intern_cstr(n,l,en) rb_intern3(n,l,en)
333 #else
334 #define intern_cstr(n,l,en) rb_intern3(n,l,en)
335 #endif
336 
337 #define STR_NEW(p,n) rb_enc_str_new((p),(n),current_enc)
338 #define STR_NEW0() rb_enc_str_new(0,0,current_enc)
339 #define STR_NEW2(p) rb_enc_str_new((p),strlen(p),current_enc)
340 #define STR_NEW3(p,n,e,func) parser_str_new((p),(n),(e),(func),current_enc)
341 #define TOK_INTERN() intern_cstr(tok(), toklen(), current_enc)
342 
343 static int parser_yyerror(struct parser_params*, const char*);
344 #define yyerror(msg) parser_yyerror(parser, (msg))
345 
346 #define lex_strterm (parser->lex.strterm)
347 #define lex_state (parser->lex.state)
348 #define cond_stack (parser->cond_stack)
349 #define cmdarg_stack (parser->cmdarg_stack)
350 #define paren_nest (parser->lex.paren_nest)
351 #define lpar_beg (parser->lex.lpar_beg)
352 #define brace_nest (parser->lex.brace_nest)
353 #define in_single (parser->in_single)
354 #define in_def (parser->in_def)
355 #define in_main (parser->in_main)
356 #define in_defined (parser->in_defined)
357 #define tokenbuf (parser->tokenbuf)
358 #define tokidx (parser->tokidx)
359 #define toksiz (parser->toksiz)
360 #define tokline (parser->tokline)
361 #define lex_input (parser->lex.input)
362 #define lex_lastline (parser->lex.lastline)
363 #define lex_nextline (parser->lex.nextline)
364 #define lex_pbeg (parser->lex.pbeg)
365 #define lex_p (parser->lex.pcur)
366 #define lex_pend (parser->lex.pend)
367 #define heredoc_end (parser->heredoc_end)
368 #define heredoc_indent (parser->heredoc_indent)
369 #define heredoc_line_indent (parser->heredoc_line_indent)
370 #define command_start (parser->command_start)
371 #define lex_gets_ptr (parser->lex.gets_ptr)
372 #define lex_gets (parser->lex.gets)
373 #define lvtbl (parser->lvtbl)
374 #define ruby__end__seen (parser->ruby__end__seen)
375 #define ruby_sourceline (parser->ruby_sourceline)
376 #define ruby_sourcefile (parser->ruby_sourcefile)
377 #define ruby_sourcefile_string (parser->ruby_sourcefile_string)
378 #define current_enc (parser->enc)
379 #define current_arg (parser->cur_arg)
380 #define yydebug (parser->yydebug)
381 #ifdef RIPPER
382 #define compile_for_eval (0)
383 #else
384 #define compile_for_eval (parser->base_block != 0 && !in_main)
385 #define ruby_eval_tree (parser->eval_tree)
386 #define ruby_eval_tree_begin (parser->eval_tree_begin)
387 #define ruby_debug_lines (parser->debug_lines)
388 #define ruby_coverage (parser->coverage)
389 #endif
390 
391 #define CALL_Q_P(q) ((q) == tANDDOT)
392 #define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
393 #define NEW_QCALL(q,r,m,a) NEW_NODE(NODE_CALL_Q(q),r,m,a)
394 
395 #define lambda_beginning_p() (lpar_beg && lpar_beg == paren_nest)
396 
397 static int yylex(YYSTYPE*, struct parser_params*);
398 
399 #ifndef RIPPER
400 #define yyparse ruby_yyparse
401 
402 static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE);
403 #define rb_node_newnode(type, a1, a2, a3) node_newnode(parser, (type), (a1), (a2), (a3))
404 
405 static NODE *cond_gen(struct parser_params*,NODE*,int);
406 #define cond(node) cond_gen(parser, (node), FALSE)
407 #define method_cond(node) cond_gen(parser, (node), TRUE)
408 static NODE *new_if_gen(struct parser_params*,NODE*,NODE*,NODE*);
409 #define new_if(cc,left,right) new_if_gen(parser, (cc), (left), (right))
410 #define new_unless(cc,left,right) new_if_gen(parser, (cc), (right), (left))
411 static NODE *logop_gen(struct parser_params*,enum node_type,NODE*,NODE*);
412 #define logop(type,node1,node2) logop_gen(parser, (type), (node1), (node2))
413 
414 static NODE *newline_node(NODE*);
415 static void fixpos(NODE*,NODE*);
416 
417 static int value_expr_gen(struct parser_params*,NODE*);
418 static void void_expr_gen(struct parser_params*,NODE*);
419 static NODE *remove_begin(NODE*);
420 static NODE *remove_begin_all(NODE*);
421 #define value_expr(node) value_expr_gen(parser, (node) = remove_begin(node))
422 #define void_expr0(node) void_expr_gen(parser, (node))
423 #define void_expr(node) void_expr0((node) = remove_begin(node))
424 static void void_stmts_gen(struct parser_params*,NODE*);
425 #define void_stmts(node) void_stmts_gen(parser, (node))
426 static void reduce_nodes_gen(struct parser_params*,NODE**);
427 #define reduce_nodes(n) reduce_nodes_gen(parser,(n))
428 static void block_dup_check_gen(struct parser_params*,NODE*,NODE*);
429 #define block_dup_check(n1,n2) block_dup_check_gen(parser,(n1),(n2))
430 
431 static NODE *block_append_gen(struct parser_params*,NODE*,NODE*);
432 #define block_append(h,t) block_append_gen(parser,(h),(t))
433 static NODE *list_append_gen(struct parser_params*,NODE*,NODE*);
434 #define list_append(l,i) list_append_gen(parser,(l),(i))
435 static NODE *list_concat(NODE*,NODE*);
436 static NODE *arg_append_gen(struct parser_params*,NODE*,NODE*);
437 #define arg_append(h,t) arg_append_gen(parser,(h),(t))
438 static NODE *arg_concat_gen(struct parser_params*,NODE*,NODE*);
439 #define arg_concat(h,t) arg_concat_gen(parser,(h),(t))
440 static NODE *literal_concat_gen(struct parser_params*,NODE*,NODE*);
441 #define literal_concat(h,t) literal_concat_gen(parser,(h),(t))
442 static int literal_concat0(struct parser_params *, VALUE, VALUE);
443 static NODE *new_evstr_gen(struct parser_params*,NODE*);
444 #define new_evstr(n) new_evstr_gen(parser,(n))
445 static NODE *evstr2dstr_gen(struct parser_params*,NODE*);
446 #define evstr2dstr(n) evstr2dstr_gen(parser,(n))
447 static NODE *splat_array(NODE*);
448 
449 static NODE *call_bin_op_gen(struct parser_params*,NODE*,ID,NODE*);
450 #define call_bin_op(recv,id,arg1) call_bin_op_gen(parser, (recv),(id),(arg1))
451 static NODE *call_uni_op_gen(struct parser_params*,NODE*,ID);
452 #define call_uni_op(recv,id) call_uni_op_gen(parser, (recv),(id))
453 
454 static NODE *new_args_gen(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*);
455 #define new_args(f,o,r,p,t) new_args_gen(parser, (f),(o),(r),(p),(t))
456 static NODE *new_args_tail_gen(struct parser_params*,NODE*,ID,ID);
457 #define new_args_tail(k,kr,b) new_args_tail_gen(parser, (k),(kr),(b))
458 #define new_kw_arg(k) ((k) ? NEW_KW_ARG(0, (k)) : 0)
459 
460 static VALUE negate_lit(VALUE);
461 static NODE *ret_args_gen(struct parser_params*,NODE*);
462 #define ret_args(node) ret_args_gen(parser, (node))
463 static NODE *arg_blk_pass(NODE*,NODE*);
464 static NODE *new_yield_gen(struct parser_params*,NODE*);
465 #define new_yield(node) new_yield_gen(parser, (node))
466 static NODE *dsym_node_gen(struct parser_params*,NODE*);
467 #define dsym_node(node) dsym_node_gen(parser, (node))
468 
469 static NODE *gettable_gen(struct parser_params*,ID);
470 #define gettable(id) gettable_gen(parser,(id))
471 static NODE *assignable_gen(struct parser_params*,ID,NODE*);
472 #define assignable(id,node) assignable_gen(parser, (id), (node))
473 
474 static NODE *aryset_gen(struct parser_params*,NODE*,NODE*);
475 #define aryset(node1,node2) aryset_gen(parser, (node1), (node2))
476 static NODE *attrset_gen(struct parser_params*,NODE*,ID,ID);
477 #define attrset(node,q,id) attrset_gen(parser, (node), (q), (id))
478 
479 static void rb_backref_error_gen(struct parser_params*,NODE*);
480 #define rb_backref_error(n) rb_backref_error_gen(parser,(n))
481 static NODE *node_assign_gen(struct parser_params*,NODE*,NODE*);
482 #define node_assign(node1, node2) node_assign_gen(parser, (node1), (node2))
483 
484 static NODE *new_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs);
485 static NODE *new_attr_op_assign_gen(struct parser_params *parser, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs);
486 #define new_attr_op_assign(lhs, type, attr, op, rhs) new_attr_op_assign_gen(parser, (lhs), (type), (attr), (op), (rhs))
487 static NODE *new_const_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs);
488 #define new_const_op_assign(lhs, op, rhs) new_const_op_assign_gen(parser, (lhs), (op), (rhs))
489 
490 #define const_path_field(w, n) NEW_COLON2(w, n)
491 #define top_const_field(n) NEW_COLON3(n)
492 static NODE *const_decl_gen(struct parser_params *parser, NODE* path);
493 #define const_decl(path) const_decl_gen(parser, path)
494 
495 #define var_field(n) (n)
496 #define backref_assign_error(n, a) (rb_backref_error(n), NEW_BEGIN(0))
497 
498 static NODE *kwd_append(NODE*, NODE*);
499 
500 static NODE *new_hash_gen(struct parser_params *parser, NODE *hash);
501 #define new_hash(hash) new_hash_gen(parser, (hash))
502 
503 #define new_defined(expr) NEW_DEFINED(remove_begin_all(expr))
504 
505 static NODE *new_regexp_gen(struct parser_params *, NODE *, int);
506 #define new_regexp(node, opt) new_regexp_gen(parser, node, opt)
507 
508 static NODE *new_xstring_gen(struct parser_params *, NODE *);
509 #define new_xstring(node) new_xstring_gen(parser, node)
510 #define new_string1(str) (str)
511 
512 #define new_brace_body(param, stmt) NEW_ITER(param, stmt)
513 #define new_do_body(param, stmt) NEW_ITER(param, stmt)
514 
515 static NODE *match_op_gen(struct parser_params*,NODE*,NODE*);
516 #define match_op(node1,node2) match_op_gen(parser, (node1), (node2))
517 
518 static ID *local_tbl_gen(struct parser_params*);
519 #define local_tbl() local_tbl_gen(parser)
520 
521 static VALUE reg_compile_gen(struct parser_params*, VALUE, int);
522 #define reg_compile(str,options) reg_compile_gen(parser, (str), (options))
523 static void reg_fragment_setenc_gen(struct parser_params*, VALUE, int);
524 #define reg_fragment_setenc(str,options) reg_fragment_setenc_gen(parser, (str), (options))
525 static int reg_fragment_check_gen(struct parser_params*, VALUE, int);
526 #define reg_fragment_check(str,options) reg_fragment_check_gen(parser, (str), (options))
527 static NODE *reg_named_capture_assign_gen(struct parser_params* parser, VALUE regexp);
528 #define reg_named_capture_assign(regexp) reg_named_capture_assign_gen(parser,(regexp))
529 
530 static NODE *parser_heredoc_dedent(struct parser_params*,NODE*);
531 # define heredoc_dedent(str) parser_heredoc_dedent(parser, (str))
532 
533 #define get_id(id) (id)
534 #define get_value(val) (val)
535 #else /* RIPPER */
536 #define NODE_RIPPER NODE_CDECL
537 
538 static inline VALUE
539 ripper_new_yylval(ID a, VALUE b, VALUE c)
540 {
541  return (VALUE)NEW_CDECL(a, b, c);
542 }
543 
544 static inline int
545 ripper_is_node_yylval(VALUE n)
546 {
547  return RB_TYPE_P(n, T_NODE) && nd_type(RNODE(n)) == NODE_RIPPER;
548 }
549 
550 #define value_expr(node) ((void)(node))
551 #define remove_begin(node) (node)
552 #define rb_dvar_defined(id, base) 0
553 #define rb_local_defined(id, base) 0
554 static ID ripper_get_id(VALUE);
555 #define get_id(id) ripper_get_id(id)
556 static VALUE ripper_get_value(VALUE);
557 #define get_value(val) ripper_get_value(val)
558 static VALUE assignable_gen(struct parser_params*,VALUE);
559 #define assignable(lhs,node) assignable_gen(parser, (lhs))
560 static int id_is_var_gen(struct parser_params *parser, ID id);
561 #define id_is_var(id) id_is_var_gen(parser, (id))
562 
563 #define node_assign(node1, node2) dispatch2(assign, (node1), (node2))
564 
565 static VALUE new_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE op, VALUE rhs);
566 static VALUE new_attr_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE type, VALUE attr, VALUE op, VALUE rhs);
567 #define new_attr_op_assign(lhs, type, attr, op, rhs) new_attr_op_assign_gen(parser, (lhs), (type), (attr), (op), (rhs))
568 #define new_const_op_assign(lhs, op, rhs) new_op_assign(lhs, op, rhs)
569 
570 static VALUE new_regexp_gen(struct parser_params *, VALUE, VALUE);
571 #define new_regexp(node, opt) new_regexp_gen(parser, node, opt)
572 
573 static VALUE new_xstring_gen(struct parser_params *, VALUE);
574 #define new_xstring(str) new_xstring_gen(parser, str)
575 #define new_string1(str) dispatch1(string_literal, str)
576 
577 #define new_brace_body(param, stmt) dispatch2(brace_block, escape_Qundef(param), stmt)
578 #define new_do_body(param, stmt) dispatch2(do_block, escape_Qundef(param), stmt)
579 
580 #define const_path_field(w, n) dispatch2(const_path_field, (w), (n))
581 #define top_const_field(n) dispatch1(top_const_field, (n))
582 static VALUE const_decl_gen(struct parser_params *parser, VALUE path);
583 #define const_decl(path) const_decl_gen(parser, path)
584 
585 #define var_field(n) dispatch1(var_field, (n))
586 static VALUE assign_error_gen(struct parser_params *parser, VALUE a);
587 #define assign_error(a) assign_error_gen(parser, (a))
588 #define backref_assign_error(n, a) assign_error(a)
589 
590 static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
591 
592 #endif /* !RIPPER */
593 
594 #define new_op_assign(lhs, op, rhs) new_op_assign_gen(parser, (lhs), (op), (rhs))
595 
596 RUBY_FUNC_EXPORTED VALUE rb_parser_reg_compile(struct parser_params* parser, VALUE str, int options);
597 RUBY_FUNC_EXPORTED int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
598 
599 
600 static ID formal_argument_gen(struct parser_params*, ID);
601 #define formal_argument(id) formal_argument_gen(parser, (id))
602 static ID shadowing_lvar_gen(struct parser_params*,ID);
603 #define shadowing_lvar(name) shadowing_lvar_gen(parser, (name))
604 static void new_bv_gen(struct parser_params*,ID);
605 #define new_bv(id) new_bv_gen(parser, (id))
606 
607 static void local_push_gen(struct parser_params*,int);
608 #define local_push(top) local_push_gen(parser,(top))
609 static void local_pop_gen(struct parser_params*);
610 #define local_pop() local_pop_gen(parser)
611 static void local_var_gen(struct parser_params*, ID);
612 #define local_var(id) local_var_gen(parser, (id))
613 static void arg_var_gen(struct parser_params*, ID);
614 #define arg_var(id) arg_var_gen(parser, (id))
615 static int local_id_gen(struct parser_params*, ID);
616 #define local_id(id) local_id_gen(parser, (id))
617 static ID internal_id_gen(struct parser_params*);
618 #define internal_id() internal_id_gen(parser)
619 
620 static const struct vtable *dyna_push_gen(struct parser_params *);
621 #define dyna_push() dyna_push_gen(parser)
622 static void dyna_pop_gen(struct parser_params*, const struct vtable *);
623 #define dyna_pop(node) dyna_pop_gen(parser, (node))
624 static int dyna_in_block_gen(struct parser_params*);
625 #define dyna_in_block() dyna_in_block_gen(parser)
626 #define dyna_var(id) local_var(id)
627 static int dvar_defined_gen(struct parser_params*,ID,int);
628 #define dvar_defined(id) dvar_defined_gen(parser, (id), 0)
629 #define dvar_defined_get(id) dvar_defined_gen(parser, (id), 1)
630 static int dvar_curr_gen(struct parser_params*,ID);
631 #define dvar_curr(id) dvar_curr_gen(parser, (id))
632 
633 static int lvar_defined_gen(struct parser_params*, ID);
634 #define lvar_defined(id) lvar_defined_gen(parser, (id))
635 
636 #define RE_OPTION_ONCE (1<<16)
637 #define RE_OPTION_ENCODING_SHIFT 8
638 #define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
639 #define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
640 #define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
641 #define RE_OPTION_MASK 0xff
642 #define RE_OPTION_ARG_ENCODING_NONE 32
643 
644 #define NODE_STRTERM NODE_ZARRAY /* nothing to gc */
645 #define NODE_HEREDOC NODE_ARRAY /* 1, 3 to gc */
646 #define SIGN_EXTEND(x,n) (((1<<(n)-1)^((x)&~(~0<<(n))))-(1<<(n)-1))
647 #define nd_func u1.id
648 #if SIZEOF_SHORT == 2
649 #define nd_term(node) ((signed short)(node)->u2.id)
650 #else
651 #define nd_term(node) SIGN_EXTEND((node)->u2.id, CHAR_BIT*2)
652 #endif
653 #define nd_paren(node) (char)((node)->u2.id >> CHAR_BIT*2)
654 #define nd_nest u3.cnt
655 
656 /****** Ripper *******/
657 
658 #ifdef RIPPER
659 #define RIPPER_VERSION "0.1.0"
660 
661 static inline VALUE intern_sym(const char *name);
662 
663 #include "eventids1.c"
664 #include "eventids2.c"
665 
666 static VALUE ripper_dispatch0(struct parser_params*,ID);
667 static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
668 static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
669 static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
670 static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
671 static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
672 static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
673 static void ripper_error_gen(struct parser_params *parser);
674 #define ripper_error() ripper_error_gen(parser)
675 
676 #define dispatch0(n) ripper_dispatch0(parser, TOKEN_PASTE(ripper_id_, n))
677 #define dispatch1(n,a) ripper_dispatch1(parser, TOKEN_PASTE(ripper_id_, n), (a))
678 #define dispatch2(n,a,b) ripper_dispatch2(parser, TOKEN_PASTE(ripper_id_, n), (a), (b))
679 #define dispatch3(n,a,b,c) ripper_dispatch3(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
680 #define dispatch4(n,a,b,c,d) ripper_dispatch4(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
681 #define dispatch5(n,a,b,c,d,e) ripper_dispatch5(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
682 #define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e), (f), (g))
683 
684 #define yyparse ripper_yyparse
685 
686 #define ripper_intern(s) ID2SYM(rb_intern(s))
687 static VALUE ripper_id2sym(ID);
688 #ifdef __GNUC__
689 #define ripper_id2sym(id) (rb_ispunct((int)(id)) ? \
690  ID2SYM(id) : ripper_id2sym(id))
691 #endif
692 
693 #define arg_new() dispatch0(args_new)
694 #define arg_add(l,a) dispatch2(args_add, (l), (a))
695 #define arg_add_star(l,a) dispatch2(args_add_star, (l), (a))
696 #define arg_add_block(l,b) dispatch2(args_add_block, (l), (b))
697 #define arg_add_optblock(l,b) ((b)==Qundef? (l) : dispatch2(args_add_block, (l), (b)))
698 #define bare_assoc(v) dispatch1(bare_assoc_hash, (v))
699 #define arg_add_assocs(l,b) arg_add((l), bare_assoc(b))
700 
701 #define args2mrhs(a) dispatch1(mrhs_new_from_args, (a))
702 #define mrhs_new() dispatch0(mrhs_new)
703 #define mrhs_add(l,a) dispatch2(mrhs_add, (l), (a))
704 #define mrhs_add_star(l,a) dispatch2(mrhs_add_star, (l), (a))
705 
706 #define mlhs_new() dispatch0(mlhs_new)
707 #define mlhs_add(l,a) dispatch2(mlhs_add, (l), (a))
708 #define mlhs_add_star(l,a) dispatch2(mlhs_add_star, (l), (a))
709 
710 #define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
711  dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
712 
713 #define blockvar_new(p,v) dispatch2(block_var, (p), (v))
714 #define blockvar_add_star(l,a) dispatch2(block_var_add_star, (l), (a))
715 #define blockvar_add_block(l,a) dispatch2(block_var_add_block, (l), (a))
716 
717 #define method_optarg(m,a) ((a)==Qundef ? (m) : dispatch2(method_add_arg,(m),(a)))
718 #define method_arg(m,a) dispatch2(method_add_arg,(m),(a))
719 #define method_add_block(m,b) dispatch2(method_add_block, (m), (b))
720 
721 #define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
722 
723 static inline VALUE
724 new_args_gen(struct parser_params *parser, VALUE f, VALUE o, VALUE r, VALUE p, VALUE tail)
725 {
726  NODE *t = (NODE *)tail;
727  VALUE k = t->u1.value, kr = t->u2.value, b = t->u3.value;
728  return params_new(f, o, r, p, k, kr, escape_Qundef(b));
729 }
730 #define new_args(f,o,r,p,t) new_args_gen(parser, (f),(o),(r),(p),(t))
731 
732 static inline VALUE
733 new_args_tail_gen(struct parser_params *parser, VALUE k, VALUE kr, VALUE b)
734 {
735  return (VALUE)MEMO_NEW(k, kr, b);
736 }
737 #define new_args_tail(k,kr,b) new_args_tail_gen(parser, (k),(kr),(b))
738 
739 #define new_defined(expr) dispatch1(defined, (expr))
740 
741 static VALUE parser_heredoc_dedent(struct parser_params*,VALUE);
742 # define heredoc_dedent(str) parser_heredoc_dedent(parser, (str))
743 
744 #define FIXME 0
745 
746 #else
747 #define ripper_id2sym(id) id
748 #endif /* RIPPER */
749 
750 #ifndef RIPPER
751 # define Qnone 0
752 # define ifndef_ripper(x) (x)
753 #else
754 # define Qnone Qnil
755 # define ifndef_ripper(x)
756 #endif
757 
758 # define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
759 # define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
760 # define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
761 # define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
762 # define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
763 # define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
764 # define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
765 # define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
766 # define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
767 # define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
768 # define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
769 # define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
770 # define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
771 # define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
772 # define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
773 # define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
774 # define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
775 # define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
776 # define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
777 # define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
778 #ifdef RIPPER
779 static ID id_warn, id_warning, id_gets;
780 # define WARN_S_L(s,l) STR_NEW(s,l)
781 # define WARN_S(s) STR_NEW2(s)
782 # define WARN_I(i) INT2NUM(i)
783 # define PRIsWARN "s"
784 # define WARN_ARGS(fmt,n) parser->value, id_warn, n, rb_usascii_str_new_lit(fmt)
785 # define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
786 # define WARN_CALL rb_funcall
787 # define WARNING_ARGS(fmt,n) parser->value, id_warning, n, rb_usascii_str_new_lit(fmt)
788 # define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
789 # define WARNING_CALL rb_funcall
790 static void ripper_compile_error(struct parser_params*, const char *fmt, ...);
791 # define compile_error ripper_compile_error
792 # define PARSER_ARG parser,
793 #else
794 # define WARN_S_L(s,l) s
795 # define WARN_S(s) s
796 # define WARN_I(i) i
797 # define PRIsWARN PRIsVALUE
798 # define WARN_ARGS(fmt,n) WARN_ARGS_L(ruby_sourceline,fmt,n)
799 # define WARN_ARGS_L(l,fmt,n) ruby_sourcefile, (l), (fmt)
800 # define WARN_CALL rb_compile_warn
801 # define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
802 # define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
803 # define WARNING_CALL rb_compile_warning
804 static void parser_compile_error(struct parser_params*, const char *fmt, ...);
805 # define compile_error parser_compile_error
806 # define PARSER_ARG parser,
807 #endif
808 
809 /* Older versions of Yacc set YYMAXDEPTH to a very low value by default (150,
810  for instance). This is too low for Ruby to parse some files, such as
811  date/format.rb, therefore bump the value up to at least Bison's default. */
812 #ifdef OLD_YACC
813 #ifndef YYMAXDEPTH
814 #define YYMAXDEPTH 10000
815 #endif
816 #endif
817 
818 static void token_info_push_gen(struct parser_params*, const char *token, size_t len);
819 static void token_info_pop_gen(struct parser_params*, const char *token, size_t len);
820 #define token_info_push(token) token_info_push_gen(parser, (token), rb_strlen_lit(token))
821 #define token_info_pop(token) token_info_pop_gen(parser, (token), rb_strlen_lit(token))
822 %}
823 
824 %pure-parser
825 %lex-param {struct parser_params *parser}
826 %parse-param {struct parser_params *parser}
827 
828 %union {
829  VALUE val;
830  NODE *node;
831  ID id;
832  int num;
833  const struct vtable *vars;
834 }
835 
836 /*%%%*/
837 %token
838 /*%
839 %token <val>
840 %*/
841  keyword_class
842  keyword_module
843  keyword_def
844  keyword_undef
845  keyword_begin
846  keyword_rescue
847  keyword_ensure
848  keyword_end
849  keyword_if
850  keyword_unless
851  keyword_then
852  keyword_elsif
853  keyword_else
854  keyword_case
855  keyword_when
856  keyword_while
857  keyword_until
858  keyword_for
859  keyword_break
860  keyword_next
861  keyword_redo
862  keyword_retry
863  keyword_in
864  keyword_do
865  keyword_do_cond
866  keyword_do_block
867  keyword_do_LAMBDA
868  keyword_return
869  keyword_yield
870  keyword_super
871  keyword_self
872  keyword_nil
873  keyword_true
874  keyword_false
875  keyword_and
876  keyword_or
877  keyword_not
878  modifier_if
879  modifier_unless
880  modifier_while
881  modifier_until
882  modifier_rescue
883  keyword_alias
884  keyword_defined
885  keyword_BEGIN
886  keyword_END
887  keyword__LINE__
888  keyword__FILE__
889  keyword__ENCODING__
890 
891 %token <id> tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL
892 %token <node> tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
893 %token <node> tNTH_REF tBACK_REF
894 %token <num> tREGEXP_END
895 
896 %type <node> singleton strings string string1 xstring regexp
897 %type <node> string_contents xstring_contents regexp_contents string_content
898 %type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
899 %type <node> literal numeric simple_numeric dsym cpath
900 %type <node> top_compstmt top_stmts top_stmt
901 %type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
902 %type <node> expr_value arg_value primary_value fcall
903 %type <node> if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure
904 %type <node> args call_args opt_call_args
905 %type <node> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
906 %type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
907 %type <node> command_rhs arg_rhs
908 %type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
909 %type <node> f_block_optarg f_block_opt
910 %type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs
911 %type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
912 %type <node> block_param opt_block_param block_param_def f_opt
913 %type <node> f_kwarg f_kw f_block_kwarg f_block_kw
914 %type <node> bv_decls opt_bv_decl bvar
915 %type <node> lambda f_larglist lambda_body brace_body do_body
916 %type <node> brace_block cmd_brace_block do_block lhs none fitem
917 %type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
918 %type <id> fsym keyword_variable user_variable sym symbol operation operation2 operation3
919 %type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
920 %type <id> f_kwrest f_label f_arg_asgn call_op call_op2
921 /*%%%*/
922 /*%
923 %type <val> program reswords then do dot_or_colon
924 %*/
925 %token END_OF_INPUT 0 "end-of-input"
926 %token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
927 %token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
928 %token tPOW RUBY_TOKEN(POW) "**"
929 %token tCMP RUBY_TOKEN(CMP) "<=>"
930 %token tEQ RUBY_TOKEN(EQ) "=="
931 %token tEQQ RUBY_TOKEN(EQQ) "==="
932 %token tNEQ RUBY_TOKEN(NEQ) "!="
933 %token tGEQ RUBY_TOKEN(GEQ) ">="
934 %token tLEQ RUBY_TOKEN(LEQ) "<="
935 %token tANDOP RUBY_TOKEN(ANDOP) "&&"
936 %token tOROP RUBY_TOKEN(OROP) "||"
937 %token tMATCH RUBY_TOKEN(MATCH) "=~"
938 %token tNMATCH RUBY_TOKEN(NMATCH) "!~"
939 %token tDOT2 RUBY_TOKEN(DOT2) ".."
940 %token tDOT3 RUBY_TOKEN(DOT3) "..."
941 %token tAREF RUBY_TOKEN(AREF) "[]"
942 %token tASET RUBY_TOKEN(ASET) "[]="
943 %token tLSHFT RUBY_TOKEN(LSHFT) "<<"
944 %token tRSHFT RUBY_TOKEN(RSHFT) ">>"
945 %token tANDDOT RUBY_TOKEN(ANDDOT) "&."
946 %token tCOLON2 "::"
947 %token tCOLON3 ":: at EXPR_BEG"
948 %token <id> tOP_ASGN /* +=, -= etc. */
949 %token tASSOC "=>"
950 %token tLPAREN "("
951 %token tLPAREN_ARG "( arg"
952 %token tRPAREN ")"
953 %token tLBRACK "["
954 %token tLBRACE "{"
955 %token tLBRACE_ARG "{ arg"
956 %token tSTAR "*"
957 %token tDSTAR "**arg"
958 %token tAMPER "&"
959 %token tLAMBDA "->"
960 %token tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG tSYMBOLS_BEG tQSYMBOLS_BEG
961 %token tSTRING_DBEG tSTRING_DEND tSTRING_DVAR tSTRING_END tLAMBEG tLABEL_END
962 
963 /*
964  * precedence table
965  */
966 
967 %nonassoc tLOWEST
968 %nonassoc tLBRACE_ARG
969 
970 %nonassoc modifier_if modifier_unless modifier_while modifier_until
971 %left keyword_or keyword_and
972 %right keyword_not
973 %nonassoc keyword_defined
974 %right '=' tOP_ASGN
975 %left modifier_rescue
976 %right '?' ':'
977 %nonassoc tDOT2 tDOT3
978 %left tOROP
979 %left tANDOP
980 %nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
981 %left '>' tGEQ '<' tLEQ
982 %left '|' '^'
983 %left '&'
984 %left tLSHFT tRSHFT
985 %left '+' '-'
986 %left '*' '/' '%'
987 %right tUMINUS_NUM tUMINUS
988 %right tPOW
989 %right '!' '~' tUPLUS
990 
991 %token tLAST_TOKEN
992 
993 %%
994 program : {
995  SET_LEX_STATE(EXPR_BEG);
996  /*%%%*/
997  local_push(compile_for_eval || in_main);
998  /*%
999  local_push(0);
1000  %*/
1001  }
1002  top_compstmt
1003  {
1004  /*%%%*/
1005  if ($2 && !compile_for_eval) {
1006  /* last expression should not be void */
1007  if (nd_type($2) != NODE_BLOCK) void_expr($2);
1008  else {
1009  NODE *node = $2;
1010  while (node->nd_next) {
1011  node = node->nd_next;
1012  }
1013  void_expr(node->nd_head);
1014  }
1015  }
1016  ruby_eval_tree = NEW_SCOPE(0, block_append(ruby_eval_tree, $2));
1017  /*%
1018  $$ = $2;
1019  parser->result = dispatch1(program, $$);
1020  %*/
1021  local_pop();
1022  }
1023  ;
1024 
1025 top_compstmt : top_stmts opt_terms
1026  {
1027  /*%%%*/
1028  void_stmts($1);
1029  /*%
1030  %*/
1031  $$ = $1;
1032  }
1033  ;
1034 
1035 top_stmts : none
1036  {
1037  /*%%%*/
1038  $$ = NEW_BEGIN(0);
1039  /*%
1040  $$ = dispatch2(stmts_add, dispatch0(stmts_new),
1041  dispatch0(void_stmt));
1042  %*/
1043  }
1044  | top_stmt
1045  {
1046  /*%%%*/
1047  $$ = newline_node($1);
1048  /*%
1049  $$ = dispatch2(stmts_add, dispatch0(stmts_new), $1);
1050  %*/
1051  }
1052  | top_stmts terms top_stmt
1053  {
1054  /*%%%*/
1055  $$ = block_append($1, newline_node($3));
1056  /*%
1057  $$ = dispatch2(stmts_add, $1, $3);
1058  %*/
1059  }
1060  | error top_stmt
1061  {
1062  $$ = remove_begin($2);
1063  }
1064  ;
1065 
1066 top_stmt : stmt
1067  | keyword_BEGIN
1068  {
1069  /*%%%*/
1070  /* local_push(0); */
1071  /*%
1072  %*/
1073  }
1074  '{' top_compstmt '}'
1075  {
1076  /*%%%*/
1077  ruby_eval_tree_begin = block_append(ruby_eval_tree_begin,
1078  $4);
1079  /* NEW_PREEXE($4)); */
1080  /* local_pop(); */
1081  $$ = NEW_BEGIN(0);
1082  /*%
1083  $$ = dispatch1(BEGIN, $4);
1084  %*/
1085  }
1086  ;
1087 
1088 bodystmt : compstmt
1089  opt_rescue
1090  opt_else
1091  opt_ensure
1092  {
1093  /*%%%*/
1094  $$ = $1;
1095  if ($2) {
1096  $$ = NEW_RESCUE($1, $2, $3);
1097  }
1098  else if ($3) {
1099  rb_warn0("else without rescue is useless");
1100  $$ = block_append($$, $3);
1101  }
1102  if ($4) {
1103  if ($$) {
1104  $$ = NEW_ENSURE($$, $4);
1105  }
1106  else {
1107  $$ = block_append($4, NEW_NIL());
1108  }
1109  }
1110  fixpos($$, $1);
1111  /*%
1112  $$ = dispatch4(bodystmt,
1113  escape_Qundef($1),
1114  escape_Qundef($2),
1115  escape_Qundef($3),
1116  escape_Qundef($4));
1117  %*/
1118  }
1119  ;
1120 
1121 compstmt : stmts opt_terms
1122  {
1123  /*%%%*/
1124  void_stmts($1);
1125  /*%
1126  %*/
1127  $$ = $1;
1128  }
1129  ;
1130 
1131 stmts : none
1132  {
1133  /*%%%*/
1134  $$ = NEW_BEGIN(0);
1135  /*%
1136  $$ = dispatch2(stmts_add, dispatch0(stmts_new),
1137  dispatch0(void_stmt));
1138  %*/
1139  }
1140  | stmt_or_begin
1141  {
1142  /*%%%*/
1143  $$ = newline_node($1);
1144  /*%
1145  $$ = dispatch2(stmts_add, dispatch0(stmts_new), $1);
1146  %*/
1147  }
1148  | stmts terms stmt_or_begin
1149  {
1150  /*%%%*/
1151  $$ = block_append($1, newline_node($3));
1152  /*%
1153  $$ = dispatch2(stmts_add, $1, $3);
1154  %*/
1155  }
1156  | error stmt
1157  {
1158  $$ = remove_begin($2);
1159  }
1160  ;
1161 
1162 stmt_or_begin : stmt
1163  {
1164  $$ = $1;
1165  }
1166  | keyword_BEGIN
1167  {
1168  yyerror("BEGIN is permitted only at toplevel");
1169  /*%%%*/
1170  /* local_push(0); */
1171  /*%
1172  %*/
1173  }
1174  '{' top_compstmt '}'
1175  {
1176  /*%%%*/
1177  ruby_eval_tree_begin = block_append(ruby_eval_tree_begin,
1178  $4);
1179  /* NEW_PREEXE($4)); */
1180  /* local_pop(); */
1181  $$ = NEW_BEGIN(0);
1182  /*%
1183  $$ = dispatch1(BEGIN, $4);
1184  %*/
1185  }
1186 
1187 stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1188  {
1189  /*%%%*/
1190  $$ = NEW_ALIAS($2, $4);
1191  /*%
1192  $$ = dispatch2(alias, $2, $4);
1193  %*/
1194  }
1195  | keyword_alias tGVAR tGVAR
1196  {
1197  /*%%%*/
1198  $$ = NEW_VALIAS($2, $3);
1199  /*%
1200  $$ = dispatch2(var_alias, $2, $3);
1201  %*/
1202  }
1203  | keyword_alias tGVAR tBACK_REF
1204  {
1205  /*%%%*/
1206  char buf[2];
1207  buf[0] = '$';
1208  buf[1] = (char)$3->nd_nth;
1209  $$ = NEW_VALIAS($2, rb_intern2(buf, 2));
1210  /*%
1211  $$ = dispatch2(var_alias, $2, $3);
1212  %*/
1213  }
1214  | keyword_alias tGVAR tNTH_REF
1215  {
1216  /*%%%*/
1217  yyerror("can't make alias for the number variables");
1218  $$ = NEW_BEGIN(0);
1219  /*%
1220  $$ = dispatch2(var_alias, $2, $3);
1221  $$ = dispatch1(alias_error, $$);
1222  ripper_error();
1223  %*/
1224  }
1225  | keyword_undef undef_list
1226  {
1227  /*%%%*/
1228  $$ = $2;
1229  /*%
1230  $$ = dispatch1(undef, $2);
1231  %*/
1232  }
1233  | stmt modifier_if expr_value
1234  {
1235  /*%%%*/
1236  $$ = new_if($3, remove_begin($1), 0);
1237  fixpos($$, $3);
1238  /*%
1239  $$ = dispatch2(if_mod, $3, $1);
1240  %*/
1241  }
1242  | stmt modifier_unless expr_value
1243  {
1244  /*%%%*/
1245  $$ = new_unless($3, remove_begin($1), 0);
1246  fixpos($$, $3);
1247  /*%
1248  $$ = dispatch2(unless_mod, $3, $1);
1249  %*/
1250  }
1251  | stmt modifier_while expr_value
1252  {
1253  /*%%%*/
1254  if ($1 && nd_type($1) == NODE_BEGIN) {
1255  $$ = NEW_WHILE(cond($3), $1->nd_body, 0);
1256  }
1257  else {
1258  $$ = NEW_WHILE(cond($3), $1, 1);
1259  }
1260  /*%
1261  $$ = dispatch2(while_mod, $3, $1);
1262  %*/
1263  }
1264  | stmt modifier_until expr_value
1265  {
1266  /*%%%*/
1267  if ($1 && nd_type($1) == NODE_BEGIN) {
1268  $$ = NEW_UNTIL(cond($3), $1->nd_body, 0);
1269  }
1270  else {
1271  $$ = NEW_UNTIL(cond($3), $1, 1);
1272  }
1273  /*%
1274  $$ = dispatch2(until_mod, $3, $1);
1275  %*/
1276  }
1277  | stmt modifier_rescue stmt
1278  {
1279  /*%%%*/
1280  NODE *resq = NEW_RESBODY(0, remove_begin($3), 0);
1281  $$ = NEW_RESCUE(remove_begin($1), resq, 0);
1282  /*%
1283  $$ = dispatch2(rescue_mod, $1, $3);
1284  %*/
1285  }
1286  | keyword_END '{' compstmt '}'
1287  {
1288  if (in_def || in_single) {
1289  rb_warn0("END in method; use at_exit");
1290  }
1291  /*%%%*/
1292  $$ = NEW_POSTEXE(NEW_NODE(
1293  NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */));
1294  /*%
1295  $$ = dispatch1(END, $3);
1296  %*/
1297  }
1298  | command_asgn
1299  | mlhs '=' command_call
1300  {
1301  /*%%%*/
1302  value_expr($3);
1303  $1->nd_value = $3;
1304  $$ = $1;
1305  /*%
1306  $$ = dispatch2(massign, $1, $3);
1307  %*/
1308  }
1309  | lhs '=' mrhs
1310  {
1311  value_expr($3);
1312  $$ = node_assign($1, $3);
1313  }
1314  | mlhs '=' mrhs_arg
1315  {
1316  /*%%%*/
1317  $1->nd_value = $3;
1318  $$ = $1;
1319  /*%
1320  $$ = dispatch2(massign, $1, $3);
1321  %*/
1322  }
1323  | expr
1324  ;
1325 
1326 command_asgn : lhs '=' command_rhs
1327  {
1328  value_expr($3);
1329  $$ = node_assign($1, $3);
1330  }
1331  | var_lhs tOP_ASGN command_rhs
1332  {
1333  value_expr($3);
1334  $$ = new_op_assign($1, $2, $3);
1335  }
1336  | primary_value '[' opt_call_args rbracket tOP_ASGN command_rhs
1337  {
1338  /*%%%*/
1339  NODE *args;
1340 
1341  value_expr($6);
1342  if (!$3) $3 = NEW_ZARRAY();
1343  args = arg_concat($3, $6);
1344  if ($5 == tOROP) {
1345  $5 = 0;
1346  }
1347  else if ($5 == tANDOP) {
1348  $5 = 1;
1349  }
1350  $$ = NEW_OP_ASGN1($1, $5, args);
1351  fixpos($$, $1);
1352  /*%
1353  $$ = dispatch2(aref_field, $1, escape_Qundef($3));
1354  $$ = dispatch3(opassign, $$, $5, $6);
1355  %*/
1356  }
1357  | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
1358  {
1359  value_expr($5);
1360  $$ = new_attr_op_assign($1, $2, $3, $4, $5);
1361  }
1362  | primary_value call_op tCONSTANT tOP_ASGN command_rhs
1363  {
1364  value_expr($5);
1365  $$ = new_attr_op_assign($1, $2, $3, $4, $5);
1366  }
1367  | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
1368  {
1369  $$ = const_path_field($1, $3);
1370  $$ = new_const_op_assign($$, $4, $5);
1371  }
1372  | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
1373  {
1374  value_expr($5);
1375  $$ = new_attr_op_assign($1, ripper_id2sym(idCOLON2), $3, $4, $5);
1376  }
1377  | backref tOP_ASGN command_rhs
1378  {
1379  $1 = var_field($1);
1380  $$ = backref_assign_error($1, node_assign($1, $3));
1381  }
1382  ;
1383 
1384 command_rhs : command_call %prec tOP_ASGN
1385  {
1386  /*%%%*/
1387  value_expr($1);
1388  $$ = $1;
1389  /*%
1390  %*/
1391  }
1392  | command_call modifier_rescue stmt
1393  {
1394  /*%%%*/
1395  value_expr($1);
1396  $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0), 0);
1397  /*%
1398  $$ = dispatch2(rescue_mod, $1, $3);
1399  %*/
1400  }
1401  | command_asgn
1402  ;
1403 
1404 expr : command_call
1405  | expr keyword_and expr
1406  {
1407  /*%%%*/
1408  $$ = logop(NODE_AND, $1, $3);
1409  /*%
1410  $$ = dispatch3(binary, $1, ripper_intern("and"), $3);
1411  %*/
1412  }
1413  | expr keyword_or expr
1414  {
1415  /*%%%*/
1416  $$ = logop(NODE_OR, $1, $3);
1417  /*%
1418  $$ = dispatch3(binary, $1, ripper_intern("or"), $3);
1419  %*/
1420  }
1421  | keyword_not opt_nl expr
1422  {
1423  /*%%%*/
1424  $$ = call_uni_op(method_cond($3), '!');
1425  /*%
1426  $$ = dispatch2(unary, ripper_intern("not"), $3);
1427  %*/
1428  }
1429  | '!' command_call
1430  {
1431  /*%%%*/
1432  $$ = call_uni_op(method_cond($2), '!');
1433  /*%
1434  $$ = dispatch2(unary, ripper_id2sym('!'), $2);
1435  %*/
1436  }
1437  | arg
1438  ;
1439 
1440 expr_value : expr
1441  {
1442  /*%%%*/
1443  value_expr($1);
1444  $$ = $1;
1445  if (!$$) $$ = NEW_NIL();
1446  /*%
1447  $$ = $1;
1448  %*/
1449  }
1450  ;
1451 
1452 command_call : command
1453  | block_command
1454  ;
1455 
1456 block_command : block_call
1457  | block_call call_op2 operation2 command_args
1458  {
1459  /*%%%*/
1460  $$ = NEW_QCALL($2, $1, $3, $4);
1461  /*%
1462  $$ = dispatch3(call, $1, $2, $3);
1463  $$ = method_arg($$, $4);
1464  %*/
1465  }
1466  ;
1467 
1468 cmd_brace_block : tLBRACE_ARG
1469  {
1470  /*%%%*/
1471  $<num>$ = ruby_sourceline;
1472  /*%
1473  %*/
1474  }
1475  brace_body '}'
1476  {
1477  $$ = $3;
1478  /*%%%*/
1479  nd_set_line($$, $<num>2);
1480  /*% %*/
1481  }
1482  ;
1483 
1484 fcall : operation
1485  {
1486  /*%%%*/
1487  $$ = NEW_FCALL($1, 0);
1488  nd_set_line($$, tokline);
1489  /*%
1490  %*/
1491  }
1492  ;
1493 
1494 command : fcall command_args %prec tLOWEST
1495  {
1496  /*%%%*/
1497  $$ = $1;
1498  $$->nd_args = $2;
1499  /*%
1500  $$ = dispatch2(command, $1, $2);
1501  %*/
1502  }
1503  | fcall command_args cmd_brace_block
1504  {
1505  /*%%%*/
1506  block_dup_check($2,$3);
1507  $1->nd_args = $2;
1508  $3->nd_iter = $1;
1509  $$ = $3;
1510  fixpos($$, $1);
1511  /*%
1512  $$ = dispatch2(command, $1, $2);
1513  $$ = method_add_block($$, $3);
1514  %*/
1515  }
1516  | primary_value call_op operation2 command_args %prec tLOWEST
1517  {
1518  /*%%%*/
1519  $$ = NEW_QCALL($2, $1, $3, $4);
1520  fixpos($$, $1);
1521  /*%
1522  $$ = dispatch4(command_call, $1, $2, $3, $4);
1523  %*/
1524  }
1525  | primary_value call_op operation2 command_args cmd_brace_block
1526  {
1527  /*%%%*/
1528  block_dup_check($4,$5);
1529  $5->nd_iter = NEW_QCALL($2, $1, $3, $4);
1530  $$ = $5;
1531  fixpos($$, $1);
1532  /*%
1533  $$ = dispatch4(command_call, $1, $2, $3, $4);
1534  $$ = method_add_block($$, $5);
1535  %*/
1536  }
1537  | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1538  {
1539  /*%%%*/
1540  $$ = NEW_CALL($1, $3, $4);
1541  fixpos($$, $1);
1542  /*%
1543  $$ = dispatch4(command_call, $1, ID2SYM(idCOLON2), $3, $4);
1544  %*/
1545  }
1546  | primary_value tCOLON2 operation2 command_args cmd_brace_block
1547  {
1548  /*%%%*/
1549  block_dup_check($4,$5);
1550  $5->nd_iter = NEW_CALL($1, $3, $4);
1551  $$ = $5;
1552  fixpos($$, $1);
1553  /*%
1554  $$ = dispatch4(command_call, $1, ID2SYM(idCOLON2), $3, $4);
1555  $$ = method_add_block($$, $5);
1556  %*/
1557  }
1558  | keyword_super command_args
1559  {
1560  /*%%%*/
1561  $$ = NEW_SUPER($2);
1562  fixpos($$, $2);
1563  /*%
1564  $$ = dispatch1(super, $2);
1565  %*/
1566  }
1567  | keyword_yield command_args
1568  {
1569  /*%%%*/
1570  $$ = new_yield($2);
1571  fixpos($$, $2);
1572  /*%
1573  $$ = dispatch1(yield, $2);
1574  %*/
1575  }
1576  | keyword_return call_args
1577  {
1578  /*%%%*/
1579  $$ = NEW_RETURN(ret_args($2));
1580  /*%
1581  $$ = dispatch1(return, $2);
1582  %*/
1583  }
1584  | keyword_break call_args
1585  {
1586  /*%%%*/
1587  $$ = NEW_BREAK(ret_args($2));
1588  /*%
1589  $$ = dispatch1(break, $2);
1590  %*/
1591  }
1592  | keyword_next call_args
1593  {
1594  /*%%%*/
1595  $$ = NEW_NEXT(ret_args($2));
1596  /*%
1597  $$ = dispatch1(next, $2);
1598  %*/
1599  }
1600  ;
1601 
1602 mlhs : mlhs_basic
1603  | tLPAREN mlhs_inner rparen
1604  {
1605  /*%%%*/
1606  $$ = $2;
1607  /*%
1608  $$ = dispatch1(mlhs_paren, $2);
1609  %*/
1610  }
1611  ;
1612 
1613 mlhs_inner : mlhs_basic
1614  | tLPAREN mlhs_inner rparen
1615  {
1616  /*%%%*/
1617  $$ = NEW_MASGN(NEW_LIST($2), 0);
1618  /*%
1619  $$ = dispatch1(mlhs_paren, $2);
1620  %*/
1621  }
1622  ;
1623 
1624 mlhs_basic : mlhs_head
1625  {
1626  /*%%%*/
1627  $$ = NEW_MASGN($1, 0);
1628  /*%
1629  $$ = $1;
1630  %*/
1631  }
1632  | mlhs_head mlhs_item
1633  {
1634  /*%%%*/
1635  $$ = NEW_MASGN(list_append($1,$2), 0);
1636  /*%
1637  $$ = mlhs_add($1, $2);
1638  %*/
1639  }
1640  | mlhs_head tSTAR mlhs_node
1641  {
1642  /*%%%*/
1643  $$ = NEW_MASGN($1, $3);
1644  /*%
1645  $$ = mlhs_add_star($1, $3);
1646  %*/
1647  }
1648  | mlhs_head tSTAR mlhs_node ',' mlhs_post
1649  {
1650  /*%%%*/
1651  $$ = NEW_MASGN($1, NEW_POSTARG($3,$5));
1652  /*%
1653  $1 = mlhs_add_star($1, $3);
1654  $$ = mlhs_add($1, $5);
1655  %*/
1656  }
1657  | mlhs_head tSTAR
1658  {
1659  /*%%%*/
1660  $$ = NEW_MASGN($1, -1);
1661  /*%
1662  $$ = mlhs_add_star($1, Qnil);
1663  %*/
1664  }
1665  | mlhs_head tSTAR ',' mlhs_post
1666  {
1667  /*%%%*/
1668  $$ = NEW_MASGN($1, NEW_POSTARG(-1, $4));
1669  /*%
1670  $1 = mlhs_add_star($1, Qnil);
1671  $$ = mlhs_add($1, $4);
1672  %*/
1673  }
1674  | tSTAR mlhs_node
1675  {
1676  /*%%%*/
1677  $$ = NEW_MASGN(0, $2);
1678  /*%
1679  $$ = mlhs_add_star(mlhs_new(), $2);
1680  %*/
1681  }
1682  | tSTAR mlhs_node ',' mlhs_post
1683  {
1684  /*%%%*/
1685  $$ = NEW_MASGN(0, NEW_POSTARG($2,$4));
1686  /*%
1687  $2 = mlhs_add_star(mlhs_new(), $2);
1688  $$ = mlhs_add($2, $4);
1689  %*/
1690  }
1691  | tSTAR
1692  {
1693  /*%%%*/
1694  $$ = NEW_MASGN(0, -1);
1695  /*%
1696  $$ = mlhs_add_star(mlhs_new(), Qnil);
1697  %*/
1698  }
1699  | tSTAR ',' mlhs_post
1700  {
1701  /*%%%*/
1702  $$ = NEW_MASGN(0, NEW_POSTARG(-1, $3));
1703  /*%
1704  $$ = mlhs_add_star(mlhs_new(), Qnil);
1705  $$ = mlhs_add($$, $3);
1706  %*/
1707  }
1708  ;
1709 
1710 mlhs_item : mlhs_node
1711  | tLPAREN mlhs_inner rparen
1712  {
1713  /*%%%*/
1714  $$ = $2;
1715  /*%
1716  $$ = dispatch1(mlhs_paren, $2);
1717  %*/
1718  }
1719  ;
1720 
1721 mlhs_head : mlhs_item ','
1722  {
1723  /*%%%*/
1724  $$ = NEW_LIST($1);
1725  /*%
1726  $$ = mlhs_add(mlhs_new(), $1);
1727  %*/
1728  }
1729  | mlhs_head mlhs_item ','
1730  {
1731  /*%%%*/
1732  $$ = list_append($1, $2);
1733  /*%
1734  $$ = mlhs_add($1, $2);
1735  %*/
1736  }
1737  ;
1738 
1739 mlhs_post : mlhs_item
1740  {
1741  /*%%%*/
1742  $$ = NEW_LIST($1);
1743  /*%
1744  $$ = mlhs_add(mlhs_new(), $1);
1745  %*/
1746  }
1747  | mlhs_post ',' mlhs_item
1748  {
1749  /*%%%*/
1750  $$ = list_append($1, $3);
1751  /*%
1752  $$ = mlhs_add($1, $3);
1753  %*/
1754  }
1755  ;
1756 
1757 mlhs_node : user_variable
1758  {
1759  $$ = assignable($1, 0);
1760  }
1761  | keyword_variable
1762  {
1763  $$ = assignable($1, 0);
1764  }
1765  | primary_value '[' opt_call_args rbracket
1766  {
1767  /*%%%*/
1768  $$ = aryset($1, $3);
1769  /*%
1770  $$ = dispatch2(aref_field, $1, escape_Qundef($3));
1771  %*/
1772  }
1773  | primary_value call_op tIDENTIFIER
1774  {
1775  /*%%%*/
1776  $$ = attrset($1, $2, $3);
1777  /*%
1778  $$ = dispatch3(field, $1, $2, $3);
1779  %*/
1780  }
1781  | primary_value tCOLON2 tIDENTIFIER
1782  {
1783  /*%%%*/
1784  $$ = attrset($1, idCOLON2, $3);
1785  /*%
1786  $$ = dispatch2(const_path_field, $1, $3);
1787  %*/
1788  }
1789  | primary_value call_op tCONSTANT
1790  {
1791  /*%%%*/
1792  $$ = attrset($1, $2, $3);
1793  /*%
1794  $$ = dispatch3(field, $1, $2, $3);
1795  %*/
1796  }
1797  | primary_value tCOLON2 tCONSTANT
1798  {
1799  $$ = const_decl(const_path_field($1, $3));
1800  }
1801  | tCOLON3 tCONSTANT
1802  {
1803  $$ = const_decl(top_const_field($2));
1804  }
1805  | backref
1806  {
1807  $1 = var_field($1);
1808  $$ = backref_assign_error($1, $1);
1809  }
1810  ;
1811 
1812 lhs : user_variable
1813  {
1814  $$ = assignable($1, 0);
1815  /*%%%*/
1816  if (!$$) $$ = NEW_BEGIN(0);
1817  /*%
1818  $$ = dispatch1(var_field, $$);
1819  %*/
1820  }
1821  | keyword_variable
1822  {
1823  $$ = assignable($1, 0);
1824  /*%%%*/
1825  if (!$$) $$ = NEW_BEGIN(0);
1826  /*%
1827  $$ = dispatch1(var_field, $$);
1828  %*/
1829  }
1830  | primary_value '[' opt_call_args rbracket
1831  {
1832  /*%%%*/
1833  $$ = aryset($1, $3);
1834  /*%
1835  $$ = dispatch2(aref_field, $1, escape_Qundef($3));
1836  %*/
1837  }
1838  | primary_value call_op tIDENTIFIER
1839  {
1840  /*%%%*/
1841  $$ = attrset($1, $2, $3);
1842  /*%
1843  $$ = dispatch3(field, $1, $2, $3);
1844  %*/
1845  }
1846  | primary_value tCOLON2 tIDENTIFIER
1847  {
1848  /*%%%*/
1849  $$ = attrset($1, idCOLON2, $3);
1850  /*%
1851  $$ = dispatch3(field, $1, ID2SYM(idCOLON2), $3);
1852  %*/
1853  }
1854  | primary_value call_op tCONSTANT
1855  {
1856  /*%%%*/
1857  $$ = attrset($1, $2, $3);
1858  /*%
1859  $$ = dispatch3(field, $1, $2, $3);
1860  %*/
1861  }
1862  | primary_value tCOLON2 tCONSTANT
1863  {
1864  $$ = const_decl(const_path_field($1, $3));
1865  }
1866  | tCOLON3 tCONSTANT
1867  {
1868  $$ = const_decl(top_const_field($2));
1869  }
1870  | backref
1871  {
1872  $1 = var_field($1);
1873  $$ = backref_assign_error($1, $1);
1874  }
1875  ;
1876 
1877 cname : tIDENTIFIER
1878  {
1879  /*%%%*/
1880  yyerror("class/module name must be CONSTANT");
1881  /*%
1882  $$ = dispatch1(class_name_error, $1);
1883  ripper_error();
1884  %*/
1885  }
1886  | tCONSTANT
1887  ;
1888 
1889 cpath : tCOLON3 cname
1890  {
1891  /*%%%*/
1892  $$ = NEW_COLON3($2);
1893  /*%
1894  $$ = dispatch1(top_const_ref, $2);
1895  %*/
1896  }
1897  | cname
1898  {
1899  /*%%%*/
1900  $$ = NEW_COLON2(0, $$);
1901  /*%
1902  $$ = dispatch1(const_ref, $1);
1903  %*/
1904  }
1905  | primary_value tCOLON2 cname
1906  {
1907  /*%%%*/
1908  $$ = NEW_COLON2($1, $3);
1909  /*%
1910  $$ = dispatch2(const_path_ref, $1, $3);
1911  %*/
1912  }
1913  ;
1914 
1915 fname : tIDENTIFIER
1916  | tCONSTANT
1917  | tFID
1918  | op
1919  {
1920  SET_LEX_STATE(EXPR_ENDFN);
1921  $$ = $1;
1922  }
1923  | reswords
1924  {
1925  SET_LEX_STATE(EXPR_ENDFN);
1926  /*%%%*/
1927  $$ = $<id>1;
1928  /*%
1929  $$ = $1;
1930  %*/
1931  }
1932  ;
1933 
1934 fsym : fname
1935  | symbol
1936  ;
1937 
1938 fitem : fsym
1939  {
1940  /*%%%*/
1941  $$ = NEW_LIT(ID2SYM($1));
1942  /*%
1943  $$ = dispatch1(symbol_literal, $1);
1944  %*/
1945  }
1946  | dsym
1947  ;
1948 
1949 undef_list : fitem
1950  {
1951  /*%%%*/
1952  $$ = NEW_UNDEF($1);
1953  /*%
1954  $$ = rb_ary_new3(1, $1);
1955  %*/
1956  }
1957  | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1958  {
1959  /*%%%*/
1960  $$ = block_append($1, NEW_UNDEF($4));
1961  /*%
1962  rb_ary_push($1, $4);
1963  %*/
1964  }
1965  ;
1966 
1967 op : '|' { ifndef_ripper($$ = '|'); }
1968  | '^' { ifndef_ripper($$ = '^'); }
1969  | '&' { ifndef_ripper($$ = '&'); }
1970  | tCMP { ifndef_ripper($$ = tCMP); }
1971  | tEQ { ifndef_ripper($$ = tEQ); }
1972  | tEQQ { ifndef_ripper($$ = tEQQ); }
1973  | tMATCH { ifndef_ripper($$ = tMATCH); }
1974  | tNMATCH { ifndef_ripper($$ = tNMATCH); }
1975  | '>' { ifndef_ripper($$ = '>'); }
1976  | tGEQ { ifndef_ripper($$ = tGEQ); }
1977  | '<' { ifndef_ripper($$ = '<'); }
1978  | tLEQ { ifndef_ripper($$ = tLEQ); }
1979  | tNEQ { ifndef_ripper($$ = tNEQ); }
1980  | tLSHFT { ifndef_ripper($$ = tLSHFT); }
1981  | tRSHFT { ifndef_ripper($$ = tRSHFT); }
1982  | '+' { ifndef_ripper($$ = '+'); }
1983  | '-' { ifndef_ripper($$ = '-'); }
1984  | '*' { ifndef_ripper($$ = '*'); }
1985  | tSTAR { ifndef_ripper($$ = '*'); }
1986  | '/' { ifndef_ripper($$ = '/'); }
1987  | '%' { ifndef_ripper($$ = '%'); }
1988  | tPOW { ifndef_ripper($$ = tPOW); }
1989  | tDSTAR { ifndef_ripper($$ = tDSTAR); }
1990  | '!' { ifndef_ripper($$ = '!'); }
1991  | '~' { ifndef_ripper($$ = '~'); }
1992  | tUPLUS { ifndef_ripper($$ = tUPLUS); }
1993  | tUMINUS { ifndef_ripper($$ = tUMINUS); }
1994  | tAREF { ifndef_ripper($$ = tAREF); }
1995  | tASET { ifndef_ripper($$ = tASET); }
1996  | '`' { ifndef_ripper($$ = '`'); }
1997  ;
1998 
1999 reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2000  | keyword_BEGIN | keyword_END
2001  | keyword_alias | keyword_and | keyword_begin
2002  | keyword_break | keyword_case | keyword_class | keyword_def
2003  | keyword_defined | keyword_do | keyword_else | keyword_elsif
2004  | keyword_end | keyword_ensure | keyword_false
2005  | keyword_for | keyword_in | keyword_module | keyword_next
2006  | keyword_nil | keyword_not | keyword_or | keyword_redo
2007  | keyword_rescue | keyword_retry | keyword_return | keyword_self
2008  | keyword_super | keyword_then | keyword_true | keyword_undef
2009  | keyword_when | keyword_yield | keyword_if | keyword_unless
2010  | keyword_while | keyword_until
2011  ;
2012 
2013 arg : lhs '=' arg_rhs
2014  {
2015  $$ = node_assign($1, $3);
2016  }
2017  | var_lhs tOP_ASGN arg_rhs
2018  {
2019  $$ = new_op_assign($1, $2, $3);
2020  }
2021  | primary_value '[' opt_call_args rbracket tOP_ASGN arg_rhs
2022  {
2023  /*%%%*/
2024  NODE *args;
2025 
2026  value_expr($6);
2027  if (!$3) $3 = NEW_ZARRAY();
2028  if (nd_type($3) == NODE_BLOCK_PASS) {
2029  args = NEW_ARGSCAT($3, $6);
2030  }
2031  else {
2032  args = arg_concat($3, $6);
2033  }
2034  if ($5 == tOROP) {
2035  $5 = 0;
2036  }
2037  else if ($5 == tANDOP) {
2038  $5 = 1;
2039  }
2040  $$ = NEW_OP_ASGN1($1, $5, args);
2041  fixpos($$, $1);
2042  /*%
2043  $1 = dispatch2(aref_field, $1, escape_Qundef($3));
2044  $$ = dispatch3(opassign, $1, $5, $6);
2045  %*/
2046  }
2047  | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
2048  {
2049  value_expr($5);
2050  $$ = new_attr_op_assign($1, $2, $3, $4, $5);
2051  }
2052  | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
2053  {
2054  value_expr($5);
2055  $$ = new_attr_op_assign($1, $2, $3, $4, $5);
2056  }
2057  | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
2058  {
2059  value_expr($5);
2060  $$ = new_attr_op_assign($1, ripper_id2sym(idCOLON2), $3, $4, $5);
2061  }
2062  | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
2063  {
2064  $$ = const_path_field($1, $3);
2065  $$ = new_const_op_assign($$, $4, $5);
2066  }
2067  | tCOLON3 tCONSTANT tOP_ASGN arg_rhs
2068  {
2069  $$ = top_const_field($2);
2070  $$ = new_const_op_assign($$, $3, $4);
2071  }
2072  | backref tOP_ASGN arg_rhs
2073  {
2074  $1 = var_field($1);
2075  $$ = backref_assign_error($1, new_op_assign($1, $2, $3));
2076  }
2077  | arg tDOT2 arg
2078  {
2079  /*%%%*/
2080  value_expr($1);
2081  value_expr($3);
2082  $$ = NEW_DOT2($1, $3);
2083  /*%
2084  $$ = dispatch2(dot2, $1, $3);
2085  %*/
2086  }
2087  | arg tDOT3 arg
2088  {
2089  /*%%%*/
2090  value_expr($1);
2091  value_expr($3);
2092  $$ = NEW_DOT3($1, $3);
2093  /*%
2094  $$ = dispatch2(dot3, $1, $3);
2095  %*/
2096  }
2097  | arg '+' arg
2098  {
2099  /*%%%*/
2100  $$ = call_bin_op($1, '+', $3);
2101  /*%
2102  $$ = dispatch3(binary, $1, ID2SYM('+'), $3);
2103  %*/
2104  }
2105  | arg '-' arg
2106  {
2107  /*%%%*/
2108  $$ = call_bin_op($1, '-', $3);
2109  /*%
2110  $$ = dispatch3(binary, $1, ID2SYM('-'), $3);
2111  %*/
2112  }
2113  | arg '*' arg
2114  {
2115  /*%%%*/
2116  $$ = call_bin_op($1, '*', $3);
2117  /*%
2118  $$ = dispatch3(binary, $1, ID2SYM('*'), $3);
2119  %*/
2120  }
2121  | arg '/' arg
2122  {
2123  /*%%%*/
2124  $$ = call_bin_op($1, '/', $3);
2125  /*%
2126  $$ = dispatch3(binary, $1, ID2SYM('/'), $3);
2127  %*/
2128  }
2129  | arg '%' arg
2130  {
2131  /*%%%*/
2132  $$ = call_bin_op($1, '%', $3);
2133  /*%
2134  $$ = dispatch3(binary, $1, ID2SYM('%'), $3);
2135  %*/
2136  }
2137  | arg tPOW arg
2138  {
2139  /*%%%*/
2140  $$ = call_bin_op($1, tPOW, $3);
2141  /*%
2142  $$ = dispatch3(binary, $1, ID2SYM(idPow), $3);
2143  %*/
2144  }
2145  | tUMINUS_NUM simple_numeric tPOW arg
2146  {
2147  /*%%%*/
2148  $$ = NEW_CALL(call_bin_op($2, tPOW, $4), tUMINUS, 0);
2149  /*%
2150  $$ = dispatch3(binary, $2, ID2SYM(idPow), $4);
2151  $$ = dispatch2(unary, ID2SYM(idUMinus), $$);
2152  %*/
2153  }
2154  | tUPLUS arg
2155  {
2156  /*%%%*/
2157  $$ = call_uni_op($2, tUPLUS);
2158  /*%
2159  $$ = dispatch2(unary, ID2SYM(idUPlus), $2);
2160  %*/
2161  }
2162  | tUMINUS arg
2163  {
2164  /*%%%*/
2165  $$ = call_uni_op($2, tUMINUS);
2166  /*%
2167  $$ = dispatch2(unary, ID2SYM(idUMinus), $2);
2168  %*/
2169  }
2170  | arg '|' arg
2171  {
2172  /*%%%*/
2173  $$ = call_bin_op($1, '|', $3);
2174  /*%
2175  $$ = dispatch3(binary, $1, ID2SYM('|'), $3);
2176  %*/
2177  }
2178  | arg '^' arg
2179  {
2180  /*%%%*/
2181  $$ = call_bin_op($1, '^', $3);
2182  /*%
2183  $$ = dispatch3(binary, $1, ID2SYM('^'), $3);
2184  %*/
2185  }
2186  | arg '&' arg
2187  {
2188  /*%%%*/
2189  $$ = call_bin_op($1, '&', $3);
2190  /*%
2191  $$ = dispatch3(binary, $1, ID2SYM('&'), $3);
2192  %*/
2193  }
2194  | arg tCMP arg
2195  {
2196  /*%%%*/
2197  $$ = call_bin_op($1, tCMP, $3);
2198  /*%
2199  $$ = dispatch3(binary, $1, ID2SYM(idCmp), $3);
2200  %*/
2201  }
2202  | arg '>' arg
2203  {
2204  /*%%%*/
2205  $$ = call_bin_op($1, '>', $3);
2206  /*%
2207  $$ = dispatch3(binary, $1, ID2SYM('>'), $3);
2208  %*/
2209  }
2210  | arg tGEQ arg
2211  {
2212  /*%%%*/
2213  $$ = call_bin_op($1, tGEQ, $3);
2214  /*%
2215  $$ = dispatch3(binary, $1, ID2SYM(idGE), $3);
2216  %*/
2217  }
2218  | arg '<' arg
2219  {
2220  /*%%%*/
2221  $$ = call_bin_op($1, '<', $3);
2222  /*%
2223  $$ = dispatch3(binary, $1, ID2SYM('<'), $3);
2224  %*/
2225  }
2226  | arg tLEQ arg
2227  {
2228  /*%%%*/
2229  $$ = call_bin_op($1, tLEQ, $3);
2230  /*%
2231  $$ = dispatch3(binary, $1, ID2SYM(idLE), $3);
2232  %*/
2233  }
2234  | arg tEQ arg
2235  {
2236  /*%%%*/
2237  $$ = call_bin_op($1, tEQ, $3);
2238  /*%
2239  $$ = dispatch3(binary, $1, ID2SYM(idEq), $3);
2240  %*/
2241  }
2242  | arg tEQQ arg
2243  {
2244  /*%%%*/
2245  $$ = call_bin_op($1, tEQQ, $3);
2246  /*%
2247  $$ = dispatch3(binary, $1, ID2SYM(idEqq), $3);
2248  %*/
2249  }
2250  | arg tNEQ arg
2251  {
2252  /*%%%*/
2253  $$ = call_bin_op($1, tNEQ, $3);
2254  /*%
2255  $$ = dispatch3(binary, $1, ID2SYM(idNeq), $3);
2256  %*/
2257  }
2258  | arg tMATCH arg
2259  {
2260  /*%%%*/
2261  $$ = match_op($1, $3);
2262  if (nd_type($1) == NODE_LIT) {
2263  VALUE lit = $1->nd_lit;
2264  if (RB_TYPE_P(lit, T_REGEXP)) {
2265  $$->nd_args = reg_named_capture_assign(lit);
2266  }
2267  }
2268  /*%
2269  $$ = dispatch3(binary, $1, ID2SYM(idEqTilde), $3);
2270  %*/
2271  }
2272  | arg tNMATCH arg
2273  {
2274  /*%%%*/
2275  $$ = call_bin_op($1, tNMATCH, $3);
2276  /*%
2277  $$ = dispatch3(binary, $1, ID2SYM(idNeqTilde), $3);
2278  %*/
2279  }
2280  | '!' arg
2281  {
2282  /*%%%*/
2283  $$ = call_uni_op(method_cond($2), '!');
2284  /*%
2285  $$ = dispatch2(unary, ID2SYM('!'), $2);
2286  %*/
2287  }
2288  | '~' arg
2289  {
2290  /*%%%*/
2291  $$ = call_uni_op($2, '~');
2292  /*%
2293  $$ = dispatch2(unary, ID2SYM('~'), $2);
2294  %*/
2295  }
2296  | arg tLSHFT arg
2297  {
2298  /*%%%*/
2299  $$ = call_bin_op($1, tLSHFT, $3);
2300  /*%
2301  $$ = dispatch3(binary, $1, ID2SYM(idLTLT), $3);
2302  %*/
2303  }
2304  | arg tRSHFT arg
2305  {
2306  /*%%%*/
2307  $$ = call_bin_op($1, tRSHFT, $3);
2308  /*%
2309  $$ = dispatch3(binary, $1, ID2SYM(idGTGT), $3);
2310  %*/
2311  }
2312  | arg tANDOP arg
2313  {
2314  /*%%%*/
2315  $$ = logop(NODE_AND, $1, $3);
2316  /*%
2317  $$ = dispatch3(binary, $1, ID2SYM(idANDOP), $3);
2318  %*/
2319  }
2320  | arg tOROP arg
2321  {
2322  /*%%%*/
2323  $$ = logop(NODE_OR, $1, $3);
2324  /*%
2325  $$ = dispatch3(binary, $1, ID2SYM(idOROP), $3);
2326  %*/
2327  }
2328  | keyword_defined opt_nl {in_defined = 1;} arg
2329  {
2330  in_defined = 0;
2331  /*%%%*/
2332  $$ = new_defined($4);
2333  /*%
2334  $$ = dispatch1(defined, $4);
2335  %*/
2336  }
2337  | arg '?' arg opt_nl ':' arg
2338  {
2339  /*%%%*/
2340  value_expr($1);
2341  $$ = new_if($1, $3, $6);
2342  fixpos($$, $1);
2343  /*%
2344  $$ = dispatch3(ifop, $1, $3, $6);
2345  %*/
2346  }
2347  | primary
2348  {
2349  $$ = $1;
2350  }
2351  ;
2352 
2353 arg_value : arg
2354  {
2355  /*%%%*/
2356  value_expr($1);
2357  $$ = $1;
2358  if (!$$) $$ = NEW_NIL();
2359  /*%
2360  $$ = $1;
2361  %*/
2362  }
2363  ;
2364 
2365 aref_args : none
2366  | args trailer
2367  {
2368  $$ = $1;
2369  }
2370  | args ',' assocs trailer
2371  {
2372  /*%%%*/
2373  $$ = $3 ? arg_append($1, new_hash($3)) : $1;
2374  /*%
2375  $$ = arg_add_assocs($1, $3);
2376  %*/
2377  }
2378  | assocs trailer
2379  {
2380  /*%%%*/
2381  $$ = $1 ? NEW_LIST(new_hash($1)) : 0;
2382  /*%
2383  $$ = arg_add_assocs(arg_new(), $1);
2384  %*/
2385  }
2386  ;
2387 
2388 arg_rhs : arg %prec tOP_ASGN
2389  {
2390  /*%%%*/
2391  value_expr($1);
2392  $$ = $1;
2393  /*%
2394  %*/
2395  }
2396  | arg modifier_rescue arg
2397  {
2398  /*%%%*/
2399  value_expr($1);
2400  $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0), 0);
2401  /*%
2402  $$ = dispatch2(rescue_mod, $1, $3);
2403  %*/
2404  }
2405  ;
2406 
2407 paren_args : '(' opt_call_args rparen
2408  {
2409  /*%%%*/
2410  $$ = $2;
2411  /*%
2412  $$ = dispatch1(arg_paren, escape_Qundef($2));
2413  %*/
2414  }
2415  ;
2416 
2417 opt_paren_args : none
2418  | paren_args
2419  ;
2420 
2421 opt_call_args : none
2422  | call_args
2423  | args ','
2424  {
2425  $$ = $1;
2426  }
2427  | args ',' assocs ','
2428  {
2429  /*%%%*/
2430  $$ = $3 ? arg_append($1, new_hash($3)) : $1;
2431  /*%
2432  $$ = arg_add_assocs($1, $3);
2433  %*/
2434  }
2435  | assocs ','
2436  {
2437  /*%%%*/
2438  $$ = $1 ? NEW_LIST(new_hash($1)) : 0;
2439  /*%
2440  $$ = arg_add_assocs(arg_new(), $1);
2441  %*/
2442  }
2443  ;
2444 
2445 call_args : command
2446  {
2447  /*%%%*/
2448  value_expr($1);
2449  $$ = NEW_LIST($1);
2450  /*%
2451  $$ = arg_add(arg_new(), $1);
2452  %*/
2453  }
2454  | args opt_block_arg
2455  {
2456  /*%%%*/
2457  $$ = arg_blk_pass($1, $2);
2458  /*%
2459  $$ = arg_add_optblock($1, $2);
2460  %*/
2461  }
2462  | assocs opt_block_arg
2463  {
2464  /*%%%*/
2465  $$ = $1 ? NEW_LIST(new_hash($1)) : 0;
2466  $$ = arg_blk_pass($$, $2);
2467  /*%
2468  $$ = arg_add_assocs(arg_new(), $1);
2469  $$ = arg_add_optblock($$, $2);
2470  %*/
2471  }
2472  | args ',' assocs opt_block_arg
2473  {
2474  /*%%%*/
2475  $$ = $3 ? arg_append($1, new_hash($3)) : $1;
2476  $$ = arg_blk_pass($$, $4);
2477  /*%
2478  $$ = arg_add_optblock(arg_add_assocs($1, $3), $4);
2479  %*/
2480  }
2481  | block_arg
2482  /*%c%*/
2483  /*%c
2484  {
2485  $$ = arg_add_block(arg_new(), $1);
2486  }
2487  %*/
2488  ;
2489 
2490 command_args : {
2491  $<val>$ = cmdarg_stack;
2492  CMDARG_PUSH(1);
2493  }
2494  call_args
2495  {
2496  /* CMDARG_POP() */
2497  CMDARG_SET($<val>1);
2498  $$ = $2;
2499  }
2500  ;
2501 
2502 block_arg : tAMPER arg_value
2503  {
2504  /*%%%*/
2505  $$ = NEW_BLOCK_PASS($2);
2506  /*%
2507  $$ = $2;
2508  %*/
2509  }
2510  ;
2511 
2512 opt_block_arg : ',' block_arg
2513  {
2514  $$ = $2;
2515  }
2516  | none
2517  {
2518  $$ = 0;
2519  }
2520  ;
2521 
2522 args : arg_value
2523  {
2524  /*%%%*/
2525  $$ = NEW_LIST($1);
2526  /*%
2527  $$ = arg_add(arg_new(), $1);
2528  %*/
2529  }
2530  | tSTAR arg_value
2531  {
2532  /*%%%*/
2533  $$ = NEW_SPLAT($2);
2534  /*%
2535  $$ = arg_add_star(arg_new(), $2);
2536  %*/
2537  }
2538  | args ',' arg_value
2539  {
2540  /*%%%*/
2541  NODE *n1;
2542  if ((n1 = splat_array($1)) != 0) {
2543  $$ = list_append(n1, $3);
2544  }
2545  else {
2546  $$ = arg_append($1, $3);
2547  }
2548  /*%
2549  $$ = arg_add($1, $3);
2550  %*/
2551  }
2552  | args ',' tSTAR arg_value
2553  {
2554  /*%%%*/
2555  NODE *n1;
2556  if ((nd_type($4) == NODE_ARRAY) && (n1 = splat_array($1)) != 0) {
2557  $$ = list_concat(n1, $4);
2558  }
2559  else {
2560  $$ = arg_concat($1, $4);
2561  }
2562  /*%
2563  $$ = arg_add_star($1, $4);
2564  %*/
2565  }
2566  ;
2567 
2568 mrhs_arg : mrhs
2569  | arg_value
2570  ;
2571 
2572 mrhs : args ',' arg_value
2573  {
2574  /*%%%*/
2575  NODE *n1;
2576  if ((n1 = splat_array($1)) != 0) {
2577  $$ = list_append(n1, $3);
2578  }
2579  else {
2580  $$ = arg_append($1, $3);
2581  }
2582  /*%
2583  $$ = mrhs_add(args2mrhs($1), $3);
2584  %*/
2585  }
2586  | args ',' tSTAR arg_value
2587  {
2588  /*%%%*/
2589  NODE *n1;
2590  if (nd_type($4) == NODE_ARRAY &&
2591  (n1 = splat_array($1)) != 0) {
2592  $$ = list_concat(n1, $4);
2593  }
2594  else {
2595  $$ = arg_concat($1, $4);
2596  }
2597  /*%
2598  $$ = mrhs_add_star(args2mrhs($1), $4);
2599  %*/
2600  }
2601  | tSTAR arg_value
2602  {
2603  /*%%%*/
2604  $$ = NEW_SPLAT($2);
2605  /*%
2606  $$ = mrhs_add_star(mrhs_new(), $2);
2607  %*/
2608  }
2609  ;
2610 
2611 primary : literal
2612  | strings
2613  | xstring
2614  | regexp
2615  | words
2616  | qwords
2617  | symbols
2618  | qsymbols
2619  | var_ref
2620  | backref
2621  | tFID
2622  {
2623  /*%%%*/
2624  $$ = NEW_FCALL($1, 0);
2625  /*%
2626  $$ = method_arg(dispatch1(fcall, $1), arg_new());
2627  %*/
2628  }
2629  | k_begin
2630  {
2631  $<val>1 = cmdarg_stack;
2632  CMDARG_SET(0);
2633  /*%%%*/
2634  $<num>$ = ruby_sourceline;
2635  /*%
2636  %*/
2637  }
2638  bodystmt
2639  k_end
2640  {
2641  CMDARG_SET($<val>1);
2642  /*%%%*/
2643  if ($3 == NULL) {
2644  $$ = NEW_NIL();
2645  }
2646  else {
2647  if (nd_type($3) == NODE_RESCUE ||
2648  nd_type($3) == NODE_ENSURE)
2649  nd_set_line($3, $<num>2);
2650  $$ = NEW_BEGIN($3);
2651  }
2652  nd_set_line($$, $<num>2);
2653  /*%
2654  $$ = dispatch1(begin, $3);
2655  %*/
2656  }
2657  | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2658  {
2659  /*%%%*/
2660  $$ = NEW_BEGIN(0);
2661  /*%
2662  $$ = dispatch1(paren, 0);
2663  %*/
2664  }
2665  | tLPAREN_ARG
2666  {
2667  $<val>1 = cmdarg_stack;
2668  CMDARG_SET(0);
2669  }
2670  stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2671  {
2672  CMDARG_SET($<val>1);
2673  /*%%%*/
2674  $$ = $3;
2675  /*%
2676  $$ = dispatch1(paren, $3);
2677  %*/
2678  }
2679  | tLPAREN compstmt ')'
2680  {
2681  /*%%%*/
2682  $$ = $2;
2683  /*%
2684  $$ = dispatch1(paren, $2);
2685  %*/
2686  }
2687  | primary_value tCOLON2 tCONSTANT
2688  {
2689  /*%%%*/
2690  $$ = NEW_COLON2($1, $3);
2691  /*%
2692  $$ = dispatch2(const_path_ref, $1, $3);
2693  %*/
2694  }
2695  | tCOLON3 tCONSTANT
2696  {
2697  /*%%%*/
2698  $$ = NEW_COLON3($2);
2699  /*%
2700  $$ = dispatch1(top_const_ref, $2);
2701  %*/
2702  }
2703  | tLBRACK aref_args ']'
2704  {
2705  /*%%%*/
2706  if ($2 == 0) {
2707  $$ = NEW_ZARRAY(); /* zero length array*/
2708  }
2709  else {
2710  $$ = $2;
2711  }
2712  /*%
2713  $$ = dispatch1(array, escape_Qundef($2));
2714  %*/
2715  }
2716  | tLBRACE assoc_list '}'
2717  {
2718  /*%%%*/
2719  $$ = new_hash($2);
2720  /*%
2721  $$ = dispatch1(hash, escape_Qundef($2));
2722  %*/
2723  }
2724  | keyword_return
2725  {
2726  /*%%%*/
2727  $$ = NEW_RETURN(0);
2728  /*%
2729  $$ = dispatch0(return0);
2730  %*/
2731  }
2732  | keyword_yield '(' call_args rparen
2733  {
2734  /*%%%*/
2735  $$ = new_yield($3);
2736  /*%
2737  $$ = dispatch1(yield, dispatch1(paren, $3));
2738  %*/
2739  }
2740  | keyword_yield '(' rparen
2741  {
2742  /*%%%*/
2743  $$ = NEW_YIELD(0);
2744  /*%
2745  $$ = dispatch1(yield, dispatch1(paren, arg_new()));
2746  %*/
2747  }
2748  | keyword_yield
2749  {
2750  /*%%%*/
2751  $$ = NEW_YIELD(0);
2752  /*%
2753  $$ = dispatch0(yield0);
2754  %*/
2755  }
2756  | keyword_defined opt_nl '(' {in_defined = 1;} expr rparen
2757  {
2758  in_defined = 0;
2759  /*%%%*/
2760  $$ = new_defined($5);
2761  /*%
2762  $$ = dispatch1(defined, $5);
2763  %*/
2764  }
2765  | keyword_not '(' expr rparen
2766  {
2767  /*%%%*/
2768  $$ = call_uni_op(method_cond($3), '!');
2769  /*%
2770  $$ = dispatch2(unary, ripper_intern("not"), $3);
2771  %*/
2772  }
2773  | keyword_not '(' rparen
2774  {
2775  /*%%%*/
2776  $$ = call_uni_op(method_cond(NEW_NIL()), '!');
2777  /*%
2778  $$ = dispatch2(unary, ripper_intern("not"), Qnil);
2779  %*/
2780  }
2781  | fcall brace_block
2782  {
2783  /*%%%*/
2784  $2->nd_iter = $1;
2785  $$ = $2;
2786  /*%
2787  $$ = method_arg(dispatch1(fcall, $1), arg_new());
2788  $$ = method_add_block($$, $2);
2789  %*/
2790  }
2791  | method_call
2792  | method_call brace_block
2793  {
2794  /*%%%*/
2795  block_dup_check($1->nd_args, $2);
2796  $2->nd_iter = $1;
2797  $$ = $2;
2798  /*%
2799  $$ = method_add_block($1, $2);
2800  %*/
2801  }
2802  | tLAMBDA lambda
2803  {
2804  $$ = $2;
2805  }
2806  | k_if expr_value then
2807  compstmt
2808  if_tail
2809  k_end
2810  {
2811  /*%%%*/
2812  $$ = new_if($2, $4, $5);
2813  fixpos($$, $2);
2814  /*%
2815  $$ = dispatch3(if, $2, $4, escape_Qundef($5));
2816  %*/
2817  }
2818  | k_unless expr_value then
2819  compstmt
2820  opt_else
2821  k_end
2822  {
2823  /*%%%*/
2824  $$ = new_unless($2, $4, $5);
2825  fixpos($$, $2);
2826  /*%
2827  $$ = dispatch3(unless, $2, $4, escape_Qundef($5));
2828  %*/
2829  }
2830  | k_while {COND_PUSH(1);} expr_value do {COND_POP();}
2831  compstmt
2832  k_end
2833  {
2834  /*%%%*/
2835  $$ = NEW_WHILE(cond($3), $6, 1);
2836  fixpos($$, $3);
2837  /*%
2838  $$ = dispatch2(while, $3, $6);
2839  %*/
2840  }
2841  | k_until {COND_PUSH(1);} expr_value do {COND_POP();}
2842  compstmt
2843  k_end
2844  {
2845  /*%%%*/
2846  $$ = NEW_UNTIL(cond($3), $6, 1);
2847  fixpos($$, $3);
2848  /*%
2849  $$ = dispatch2(until, $3, $6);
2850  %*/
2851  }
2852  | k_case expr_value opt_terms
2853  case_body
2854  k_end
2855  {
2856  /*%%%*/
2857  $$ = NEW_CASE($2, $4);
2858  fixpos($$, $2);
2859  /*%
2860  $$ = dispatch2(case, $2, $4);
2861  %*/
2862  }
2863  | k_case opt_terms case_body k_end
2864  {
2865  /*%%%*/
2866  $$ = NEW_CASE(0, $3);
2867  /*%
2868  $$ = dispatch2(case, Qnil, $3);
2869  %*/
2870  }
2871  | k_for for_var keyword_in
2872  {COND_PUSH(1);}
2873  expr_value do
2874  {COND_POP();}
2875  compstmt
2876  k_end
2877  {
2878  /*%%%*/
2879  /*
2880  * for a, b, c in e
2881  * #=>
2882  * e.each{|*x| a, b, c = x}
2883  *
2884  * for a in e
2885  * #=>
2886  * e.each{|x| a, = x}
2887  */
2888  ID id = internal_id();
2889  ID *tbl = ALLOC_N(ID, 2);
2890  NODE *m = NEW_ARGS_AUX(0, 0);
2891  NODE *args, *scope;
2892 
2893  switch (nd_type($2)) {
2894  case NODE_MASGN:
2895  m->nd_next = node_assign($2, NEW_FOR(NEW_DVAR(id), 0, 0));
2896  args = new_args(m, 0, id, 0, new_args_tail(0, 0, 0));
2897  break;
2898  case NODE_LASGN:
2899  case NODE_DASGN:
2900  case NODE_DASGN_CURR:
2901  $2->nd_value = NEW_DVAR(id);
2902  m->nd_plen = 1;
2903  m->nd_next = $2;
2904  args = new_args(m, 0, 0, 0, new_args_tail(0, 0, 0));
2905  break;
2906  default:
2907  m->nd_next = node_assign(NEW_MASGN(NEW_LIST($2), 0), NEW_DVAR(id));
2908  args = new_args(m, 0, id, 0, new_args_tail(0, 0, 0));
2909  break;
2910  }
2911  scope = NEW_NODE(NODE_SCOPE, tbl, $8, args);
2912  tbl[0] = 1; tbl[1] = id;
2913  $$ = NEW_FOR(0, $5, scope);
2914  fixpos($$, $2);
2915  /*%
2916  $$ = dispatch3(for, $2, $5, $8);
2917  %*/
2918  }
2919  | k_class cpath superclass
2920  {
2921  if (in_def || in_single)
2922  yyerror("class definition in method body");
2923  local_push(0);
2924  /*%%%*/
2925  $<num>$ = ruby_sourceline;
2926  /*%
2927  %*/
2928  }
2929  bodystmt
2930  k_end
2931  {
2932  /*%%%*/
2933  $$ = NEW_CLASS($2, $5, $3);
2934  nd_set_line($$, $<num>4);
2935  /*%
2936  $$ = dispatch3(class, $2, $3, $5);
2937  %*/
2938  local_pop();
2939  }
2940  | k_class tLSHFT expr
2941  {
2942  $<num>$ = (in_def << 1) | in_single;
2943  in_def = 0;
2944  in_single = 0;
2945  local_push(0);
2946  }
2947  term
2948  bodystmt
2949  k_end
2950  {
2951  /*%%%*/
2952  $$ = NEW_SCLASS($3, $6);
2953  fixpos($$, $3);
2954  /*%
2955  $$ = dispatch2(sclass, $3, $6);
2956  %*/
2957  local_pop();
2958  in_def = ($<num>4 >> 1) & 1;
2959  in_single = $<num>4 & 1;
2960  }
2961  | k_module cpath
2962  {
2963  if (in_def || in_single)
2964  yyerror("module definition in method body");
2965  local_push(0);
2966  /*%%%*/
2967  $<num>$ = ruby_sourceline;
2968  /*%
2969  %*/
2970  }
2971  bodystmt
2972  k_end
2973  {
2974  /*%%%*/
2975  $$ = NEW_MODULE($2, $4);
2976  nd_set_line($$, $<num>3);
2977  /*%
2978  $$ = dispatch2(module, $2, $4);
2979  %*/
2980  local_pop();
2981  }
2982  | k_def fname
2983  {
2984  local_push(0);
2985  $<id>$ = current_arg;
2986  current_arg = 0;
2987  }
2988  {
2989  $<num>$ = in_def;
2990  in_def = 1;
2991  }
2992  f_arglist
2993  bodystmt
2994  k_end
2995  {
2996  /*%%%*/
2997  NODE *body = remove_begin($6);
2998  reduce_nodes(&body);
2999  $$ = NEW_DEFN($2, $5, body, METHOD_VISI_PRIVATE);
3000  nd_set_line($$, $<num>1);
3001  /*%
3002  $$ = dispatch3(def, $2, $5, $6);
3003  %*/
3004  local_pop();
3005  in_def = $<num>4 & 1;
3006  current_arg = $<id>3;
3007  }
3008  | k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname
3009  {
3010  $<num>4 = in_single;
3011  in_single = 1;
3012  SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3013  local_push(0);
3014  $<id>$ = current_arg;
3015  current_arg = 0;
3016  }
3017  f_arglist
3018  bodystmt
3019  k_end
3020  {
3021  /*%%%*/
3022  NODE *body = remove_begin($8);
3023  reduce_nodes(&body);
3024  $$ = NEW_DEFS($2, $5, $7, body);
3025  nd_set_line($$, $<num>1);
3026  /*%
3027  $$ = dispatch5(defs, $2, $3, $5, $7, $8);
3028  %*/
3029  local_pop();
3030  in_single = $<num>4 & 1;
3031  current_arg = $<id>6;
3032  }
3033  | keyword_break
3034  {
3035  /*%%%*/
3036  $$ = NEW_BREAK(0);
3037  /*%
3038  $$ = dispatch1(break, arg_new());
3039  %*/
3040  }
3041  | keyword_next
3042  {
3043  /*%%%*/
3044  $$ = NEW_NEXT(0);
3045  /*%
3046  $$ = dispatch1(next, arg_new());
3047  %*/
3048  }
3049  | keyword_redo
3050  {
3051  /*%%%*/
3052  $$ = NEW_REDO();
3053  /*%
3054  $$ = dispatch0(redo);
3055  %*/
3056  }
3057  | keyword_retry
3058  {
3059  /*%%%*/
3060  $$ = NEW_RETRY();
3061  /*%
3062  $$ = dispatch0(retry);
3063  %*/
3064  }
3065  ;
3066 
3067 primary_value : primary
3068  {
3069  /*%%%*/
3070  value_expr($1);
3071  $$ = $1;
3072  if (!$$) $$ = NEW_NIL();
3073  /*%
3074  $$ = $1;
3075  %*/
3076  }
3077  ;
3078 
3079 k_begin : keyword_begin
3080  {
3081  token_info_push("begin");
3082  }
3083  ;
3084 
3085 k_if : keyword_if
3086  {
3087  token_info_push("if");
3088  }
3089  ;
3090 
3091 k_unless : keyword_unless
3092  {
3093  token_info_push("unless");
3094  }
3095  ;
3096 
3097 k_while : keyword_while
3098  {
3099  token_info_push("while");
3100  }
3101  ;
3102 
3103 k_until : keyword_until
3104  {
3105  token_info_push("until");
3106  }
3107  ;
3108 
3109 k_case : keyword_case
3110  {
3111  token_info_push("case");
3112  }
3113  ;
3114 
3115 k_for : keyword_for
3116  {
3117  token_info_push("for");
3118  }
3119  ;
3120 
3121 k_class : keyword_class
3122  {
3123  token_info_push("class");
3124  }
3125  ;
3126 
3127 k_module : keyword_module
3128  {
3129  token_info_push("module");
3130  }
3131  ;
3132 
3133 k_def : keyword_def
3134  {
3135  token_info_push("def");
3136  /*%%%*/
3137  $<num>$ = ruby_sourceline;
3138  /*%
3139  %*/
3140  }
3141  ;
3142 
3143 k_end : keyword_end
3144  {
3145  token_info_pop("end");
3146  }
3147  ;
3148 
3149 then : term
3150  /*%c%*/
3151  /*%c
3152  { $$ = Qnil; }
3153  %*/
3154  | keyword_then
3155  | term keyword_then
3156  /*%c%*/
3157  /*%c
3158  { $$ = $2; }
3159  %*/
3160  ;
3161 
3162 do : term
3163  /*%c%*/
3164  /*%c
3165  { $$ = Qnil; }
3166  %*/
3167  | keyword_do_cond
3168  ;
3169 
3170 if_tail : opt_else
3171  | keyword_elsif expr_value then
3172  compstmt
3173  if_tail
3174  {
3175  /*%%%*/
3176  $$ = new_if($2, $4, $5);
3177  fixpos($$, $2);
3178  /*%
3179  $$ = dispatch3(elsif, $2, $4, escape_Qundef($5));
3180  %*/
3181  }
3182  ;
3183 
3184 opt_else : none
3185  | keyword_else compstmt
3186  {
3187  /*%%%*/
3188  $$ = $2;
3189  /*%
3190  $$ = dispatch1(else, $2);
3191  %*/
3192  }
3193  ;
3194 
3195 for_var : lhs
3196  | mlhs
3197  ;
3198 
3199 f_marg : f_norm_arg
3200  {
3201  $$ = assignable($1, 0);
3202  /*%%%*/
3203  /*%
3204  $$ = dispatch1(mlhs_paren, $$);
3205  %*/
3206  }
3207  | tLPAREN f_margs rparen
3208  {
3209  /*%%%*/
3210  $$ = $2;
3211  /*%
3212  $$ = dispatch1(mlhs_paren, $2);
3213  %*/
3214  }
3215  ;
3216 
3217 f_marg_list : f_marg
3218  {
3219  /*%%%*/
3220  $$ = NEW_LIST($1);
3221  /*%
3222  $$ = mlhs_add(mlhs_new(), $1);
3223  %*/
3224  }
3225  | f_marg_list ',' f_marg
3226  {
3227  /*%%%*/
3228  $$ = list_append($1, $3);
3229  /*%
3230  $$ = mlhs_add($1, $3);
3231  %*/
3232  }
3233  ;
3234 
3235 f_margs : f_marg_list
3236  {
3237  /*%%%*/
3238  $$ = NEW_MASGN($1, 0);
3239  /*%
3240  $$ = $1;
3241  %*/
3242  }
3243  | f_marg_list ',' tSTAR f_norm_arg
3244  {
3245  $$ = assignable($4, 0);
3246  /*%%%*/
3247  $$ = NEW_MASGN($1, $$);
3248  /*%
3249  $$ = mlhs_add_star($1, $$);
3250  %*/
3251  }
3252  | f_marg_list ',' tSTAR f_norm_arg ',' f_marg_list
3253  {
3254  $$ = assignable($4, 0);
3255  /*%%%*/
3256  $$ = NEW_MASGN($1, NEW_POSTARG($$, $6));
3257  /*%
3258  $$ = mlhs_add_star($1, $$);
3259  %*/
3260  }
3261  | f_marg_list ',' tSTAR
3262  {
3263  /*%%%*/
3264  $$ = NEW_MASGN($1, -1);
3265  /*%
3266  $$ = mlhs_add_star($1, Qnil);
3267  %*/
3268  }
3269  | f_marg_list ',' tSTAR ',' f_marg_list
3270  {
3271  /*%%%*/
3272  $$ = NEW_MASGN($1, NEW_POSTARG(-1, $5));
3273  /*%
3274  $$ = mlhs_add_star($1, $5);
3275  %*/
3276  }
3277  | tSTAR f_norm_arg
3278  {
3279  $$ = assignable($2, 0);
3280  /*%%%*/
3281  $$ = NEW_MASGN(0, $$);
3282  /*%
3283  $$ = mlhs_add_star(mlhs_new(), $$);
3284  %*/
3285  }
3286  | tSTAR f_norm_arg ',' f_marg_list
3287  {
3288  $$ = assignable($2, 0);
3289  /*%%%*/
3290  $$ = NEW_MASGN(0, NEW_POSTARG($$, $4));
3291  /*%
3292  #if 0
3293  TODO: Check me
3294  #endif
3295  $$ = mlhs_add_star($$, $4);
3296  %*/
3297  }
3298  | tSTAR
3299  {
3300  /*%%%*/
3301  $$ = NEW_MASGN(0, -1);
3302  /*%
3303  $$ = mlhs_add_star(mlhs_new(), Qnil);
3304  %*/
3305  }
3306  | tSTAR ',' f_marg_list
3307  {
3308  /*%%%*/
3309  $$ = NEW_MASGN(0, NEW_POSTARG(-1, $3));
3310  /*%
3311  $$ = mlhs_add_star(mlhs_new(), Qnil);
3312  %*/
3313  }
3314  ;
3315 
3316 
3317 block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3318  {
3319  $$ = new_args_tail($1, $3, $4);
3320  }
3321  | f_block_kwarg opt_f_block_arg
3322  {
3323  $$ = new_args_tail($1, Qnone, $2);
3324  }
3325  | f_kwrest opt_f_block_arg
3326  {
3327  $$ = new_args_tail(Qnone, $1, $2);
3328  }
3329  | f_block_arg
3330  {
3331  $$ = new_args_tail(Qnone, Qnone, $1);
3332  }
3333  ;
3334 
3335 opt_block_args_tail : ',' block_args_tail
3336  {
3337  $$ = $2;
3338  }
3339  | /* none */
3340  {
3341  $$ = new_args_tail(Qnone, Qnone, Qnone);
3342  }
3343  ;
3344 
3345 block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3346  {
3347  $$ = new_args($1, $3, $5, Qnone, $6);
3348  }
3349  | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3350  {
3351  $$ = new_args($1, $3, $5, $7, $8);
3352  }
3353  | f_arg ',' f_block_optarg opt_block_args_tail
3354  {
3355  $$ = new_args($1, $3, Qnone, Qnone, $4);
3356  }
3357  | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3358  {
3359  $$ = new_args($1, $3, Qnone, $5, $6);
3360  }
3361  | f_arg ',' f_rest_arg opt_block_args_tail
3362  {
3363  $$ = new_args($1, Qnone, $3, Qnone, $4);
3364  }
3365  | f_arg ','
3366  {
3367  $$ = new_args($1, Qnone, 1, Qnone, new_args_tail(Qnone, Qnone, Qnone));
3368  /*%%%*/
3369  /*%
3370  dispatch1(excessed_comma, $$);
3371  %*/
3372  }
3373  | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3374  {
3375  $$ = new_args($1, Qnone, $3, $5, $6);
3376  }
3377  | f_arg opt_block_args_tail
3378  {
3379  $$ = new_args($1, Qnone, Qnone, Qnone, $2);
3380  }
3381  | f_block_optarg ',' f_rest_arg opt_block_args_tail
3382  {
3383  $$ = new_args(Qnone, $1, $3, Qnone, $4);
3384  }
3385  | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3386  {
3387  $$ = new_args(Qnone, $1, $3, $5, $6);
3388  }
3389  | f_block_optarg opt_block_args_tail
3390  {
3391  $$ = new_args(Qnone, $1, Qnone, Qnone, $2);
3392  }
3393  | f_block_optarg ',' f_arg opt_block_args_tail
3394  {
3395  $$ = new_args(Qnone, $1, Qnone, $3, $4);
3396  }
3397  | f_rest_arg opt_block_args_tail
3398  {
3399  $$ = new_args(Qnone, Qnone, $1, Qnone, $2);
3400  }
3401  | f_rest_arg ',' f_arg opt_block_args_tail
3402  {
3403  $$ = new_args(Qnone, Qnone, $1, $3, $4);
3404  }
3405  | block_args_tail
3406  {
3407  $$ = new_args(Qnone, Qnone, Qnone, Qnone, $1);
3408  }
3409  ;
3410 
3411 opt_block_param : none
3412  | block_param_def
3413  {
3414  command_start = TRUE;
3415  }
3416  ;
3417 
3418 block_param_def : '|' opt_bv_decl '|'
3419  {
3420  current_arg = 0;
3421  /*%%%*/
3422  $$ = 0;
3423  /*%
3424  $$ = blockvar_new(params_new(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil),
3425  escape_Qundef($2));
3426  %*/
3427  }
3428  | tOROP
3429  {
3430  /*%%%*/
3431  $$ = 0;
3432  /*%
3433  $$ = blockvar_new(params_new(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil),
3434  Qnil);
3435  %*/
3436  }
3437  | '|' block_param opt_bv_decl '|'
3438  {
3439  current_arg = 0;
3440  /*%%%*/
3441  $$ = $2;
3442  /*%
3443  $$ = blockvar_new(escape_Qundef($2), escape_Qundef($3));
3444  %*/
3445  }
3446  ;
3447 
3448 
3449 opt_bv_decl : opt_nl
3450  {
3451  $$ = 0;
3452  }
3453  | opt_nl ';' bv_decls opt_nl
3454  {
3455  /*%%%*/
3456  $$ = 0;
3457  /*%
3458  $$ = $3;
3459  %*/
3460  }
3461  ;
3462 
3463 bv_decls : bvar
3464  /*%c%*/
3465  /*%c
3466  {
3467  $$ = rb_ary_new3(1, $1);
3468  }
3469  %*/
3470  | bv_decls ',' bvar
3471  /*%c%*/
3472  /*%c
3473  {
3474  rb_ary_push($1, $3);
3475  }
3476  %*/
3477  ;
3478 
3479 bvar : tIDENTIFIER
3480  {
3481  new_bv(get_id($1));
3482  /*%%%*/
3483  /*%
3484  $$ = get_value($1);
3485  %*/
3486  }
3487  | f_bad_arg
3488  {
3489  $$ = 0;
3490  }
3491  ;
3492 
3493 lambda : {
3494  $<vars>$ = dyna_push();
3495  }
3496  {
3497  $<num>$ = lpar_beg;
3498  lpar_beg = ++paren_nest;
3499  }
3500  f_larglist
3501  {
3502  $<num>$ = ruby_sourceline;
3503  }
3504  {
3505  $<val>$ = cmdarg_stack;
3506  CMDARG_SET(0);
3507  }
3508  lambda_body
3509  {
3510  lpar_beg = $<num>2;
3511  CMDARG_SET($<val>5);
3512  CMDARG_LEXPOP();
3513  /*%%%*/
3514  $$ = NEW_LAMBDA($3, $6);
3515  nd_set_line($$, $<num>4);
3516  /*%
3517  $$ = dispatch2(lambda, $3, $6);
3518  %*/
3519  dyna_pop($<vars>1);
3520  }
3521  ;
3522 
3523 f_larglist : '(' f_args opt_bv_decl ')'
3524  {
3525  /*%%%*/
3526  $$ = $2;
3527  /*%
3528  $$ = dispatch1(paren, $2);
3529  %*/
3530  }
3531  | f_args
3532  {
3533  $$ = $1;
3534  }
3535  ;
3536 
3537 lambda_body : tLAMBEG compstmt '}'
3538  {
3539  token_info_pop("}");
3540  $$ = $2;
3541  }
3542  | keyword_do_LAMBDA compstmt k_end
3543  {
3544  $$ = $2;
3545  }
3546  ;
3547 
3548 do_block : keyword_do_block
3549  {
3550  /*%%%*/
3551  $<num>$ = ruby_sourceline;
3552  /*% %*/
3553  }
3554  do_body keyword_end
3555  {
3556  $$ = $3;
3557  /*%%%*/
3558  nd_set_line($$, $<num>2);
3559  /*% %*/
3560  }
3561  ;
3562 
3563 block_call : command do_block
3564  {
3565  /*%%%*/
3566  if (nd_type($1) == NODE_YIELD) {
3567  compile_error(PARSER_ARG "block given to yield");
3568  }
3569  else {
3570  block_dup_check($1->nd_args, $2);
3571  }
3572  $2->nd_iter = $1;
3573  $$ = $2;
3574  fixpos($$, $1);
3575  /*%
3576  $$ = method_add_block($1, $2);
3577  %*/
3578  }
3579  | block_call call_op2 operation2 opt_paren_args
3580  {
3581  /*%%%*/
3582  $$ = NEW_QCALL($2, $1, $3, $4);
3583  /*%
3584  $$ = dispatch3(call, $1, $2, $3);
3585  $$ = method_optarg($$, $4);
3586  %*/
3587  }
3588  | block_call call_op2 operation2 opt_paren_args brace_block
3589  {
3590  /*%%%*/
3591  block_dup_check($4, $5);
3592  $5->nd_iter = NEW_QCALL($2, $1, $3, $4);
3593  $$ = $5;
3594  fixpos($$, $1);
3595  /*%
3596  $$ = dispatch4(command_call, $1, $2, $3, $4);
3597  $$ = method_add_block($$, $5);
3598  %*/
3599  }
3600  | block_call call_op2 operation2 command_args do_block
3601  {
3602  /*%%%*/
3603  block_dup_check($4, $5);
3604  $5->nd_iter = NEW_QCALL($2, $1, $3, $4);
3605  $$ = $5;
3606  fixpos($$, $1);
3607  /*%
3608  $$ = dispatch4(command_call, $1, $2, $3, $4);
3609  $$ = method_add_block($$, $5);
3610  %*/
3611  }
3612  ;
3613 
3614 method_call : fcall paren_args
3615  {
3616  /*%%%*/
3617  $$ = $1;
3618  $$->nd_args = $2;
3619  /*%
3620  $$ = method_arg(dispatch1(fcall, $1), $2);
3621  %*/
3622  }
3623  | primary_value call_op operation2
3624  {
3625  /*%%%*/
3626  $<num>$ = ruby_sourceline;
3627  /*% %*/
3628  }
3629  opt_paren_args
3630  {
3631  /*%%%*/
3632  $$ = NEW_QCALL($2, $1, $3, $5);
3633  nd_set_line($$, $<num>4);
3634  /*%
3635  $$ = dispatch3(call, $1, $2, $3);
3636  $$ = method_optarg($$, $5);
3637  %*/
3638  }
3639  | primary_value tCOLON2 operation2
3640  {
3641  /*%%%*/
3642  $<num>$ = ruby_sourceline;
3643  /*% %*/
3644  }
3645  paren_args
3646  {
3647  /*%%%*/
3648  $$ = NEW_CALL($1, $3, $5);
3649  nd_set_line($$, $<num>4);
3650  /*%
3651  $$ = dispatch3(call, $1, ripper_id2sym(idCOLON2), $3);
3652  $$ = method_optarg($$, $5);
3653  %*/
3654  }
3655  | primary_value tCOLON2 operation3
3656  {
3657  /*%%%*/
3658  $$ = NEW_CALL($1, $3, 0);
3659  /*%
3660  $$ = dispatch3(call, $1, ID2SYM(idCOLON2), $3);
3661  %*/
3662  }
3663  | primary_value call_op
3664  {
3665  /*%%%*/
3666  $<num>$ = ruby_sourceline;
3667  /*% %*/
3668  }
3669  paren_args
3670  {
3671  /*%%%*/
3672  $$ = NEW_QCALL($2, $1, idCall, $4);
3673  nd_set_line($$, $<num>3);
3674  /*%
3675  $$ = dispatch3(call, $1, $2, ID2SYM(idCall));
3676  $$ = method_optarg($$, $4);
3677  %*/
3678  }
3679  | primary_value tCOLON2
3680  {
3681  /*%%%*/
3682  $<num>$ = ruby_sourceline;
3683  /*% %*/
3684  }
3685  paren_args
3686  {
3687  /*%%%*/
3688  $$ = NEW_CALL($1, idCall, $4);
3689  nd_set_line($$, $<num>3);
3690  /*%
3691  $$ = dispatch3(call, $1, ID2SYM(idCOLON2),
3692  ID2SYM(idCall));
3693  $$ = method_optarg($$, $4);
3694  %*/
3695  }
3696  | keyword_super paren_args
3697  {
3698  /*%%%*/
3699  $$ = NEW_SUPER($2);
3700  /*%
3701  $$ = dispatch1(super, $2);
3702  %*/
3703  }
3704  | keyword_super
3705  {
3706  /*%%%*/
3707  $$ = NEW_ZSUPER();
3708  /*%
3709  $$ = dispatch0(zsuper);
3710  %*/
3711  }
3712  | primary_value '[' opt_call_args rbracket
3713  {
3714  /*%%%*/
3715  if ($1 && nd_type($1) == NODE_SELF)
3716  $$ = NEW_FCALL(tAREF, $3);
3717  else
3718  $$ = NEW_CALL($1, tAREF, $3);
3719  fixpos($$, $1);
3720  /*%
3721  $$ = dispatch2(aref, $1, escape_Qundef($3));
3722  %*/
3723  }
3724  ;
3725 
3726 brace_block : '{'
3727  {
3728  /*%%%*/
3729  $<num>$ = ruby_sourceline;
3730  /*% %*/
3731  }
3732  brace_body '}'
3733  {
3734  $$ = $3;
3735  /*%%%*/
3736  nd_set_line($$, $<num>2);
3737  /*% %*/
3738  }
3739  | keyword_do
3740  {
3741  /*%%%*/
3742  $<num>$ = ruby_sourceline;
3743  /*% %*/
3744  }
3745  do_body keyword_end
3746  {
3747  $$ = $3;
3748  /*%%%*/
3749  nd_set_line($$, $<num>2);
3750  /*% %*/
3751  }
3752  ;
3753 
3754 brace_body : {$<vars>$ = dyna_push();}
3755  {$<val>$ = cmdarg_stack >> 1; CMDARG_SET(0);}
3756  opt_block_param compstmt
3757  {
3758  $$ = new_brace_body($3, $4);
3759  dyna_pop($<vars>1);
3760  CMDARG_SET($<val>2);
3761  }
3762  ;
3763 
3764 do_body : {$<vars>$ = dyna_push();}
3765  {$<val>$ = cmdarg_stack; CMDARG_SET(0);}
3766  opt_block_param compstmt
3767  {
3768  $$ = new_do_body($3, $4);
3769  dyna_pop($<vars>1);
3770  CMDARG_SET($<val>2);
3771  }
3772  ;
3773 
3774 case_body : keyword_when args then
3775  compstmt
3776  cases
3777  {
3778  /*%%%*/
3779  $$ = NEW_WHEN($2, $4, $5);
3780  /*%
3781  $$ = dispatch3(when, $2, $4, escape_Qundef($5));
3782  %*/
3783  }
3784  ;
3785 
3786 cases : opt_else
3787  | case_body
3788  ;
3789 
3790 opt_rescue : keyword_rescue exc_list exc_var then
3791  compstmt
3792  opt_rescue
3793  {
3794  /*%%%*/
3795  if ($3) {
3796  $3 = node_assign($3, NEW_ERRINFO());
3797  $5 = block_append($3, $5);
3798  }
3799  $$ = NEW_RESBODY($2, $5, $6);
3800  fixpos($$, $2?$2:$5);
3801  /*%
3802  $$ = dispatch4(rescue,
3803  escape_Qundef($2),
3804  escape_Qundef($3),
3805  escape_Qundef($5),
3806  escape_Qundef($6));
3807  %*/
3808  }
3809  | none
3810  ;
3811 
3812 exc_list : arg_value
3813  {
3814  /*%%%*/
3815  $$ = NEW_LIST($1);
3816  /*%
3817  $$ = rb_ary_new3(1, $1);
3818  %*/
3819  }
3820  | mrhs
3821  {
3822  /*%%%*/
3823  if (!($$ = splat_array($1))) $$ = $1;
3824  /*%
3825  $$ = $1;
3826  %*/
3827  }
3828  | none
3829  ;
3830 
3831 exc_var : tASSOC lhs
3832  {
3833  $$ = $2;
3834  }
3835  | none
3836  ;
3837 
3838 opt_ensure : keyword_ensure compstmt
3839  {
3840  /*%%%*/
3841  $$ = $2;
3842  /*%
3843  $$ = dispatch1(ensure, $2);
3844  %*/
3845  }
3846  | none
3847  ;
3848 
3849 literal : numeric
3850  | symbol
3851  {
3852  /*%%%*/
3853  $$ = NEW_LIT(ID2SYM($1));
3854  /*%
3855  $$ = dispatch1(symbol_literal, $1);
3856  %*/
3857  }
3858  | dsym
3859  ;
3860 
3861 strings : string
3862  {
3863  /*%%%*/
3864  NODE *node = $1;
3865  if (!node) {
3866  node = NEW_STR(STR_NEW0());
3867  }
3868  else {
3869  node = evstr2dstr(node);
3870  }
3871  $$ = node;
3872  /*%
3873  $$ = $1;
3874  %*/
3875  }
3876  ;
3877 
3878 string : tCHAR
3879  | string1
3880  | string string1
3881  {
3882  /*%%%*/
3883  $$ = literal_concat($1, $2);
3884  /*%
3885  $$ = dispatch2(string_concat, $1, $2);
3886  %*/
3887  }
3888  ;
3889 
3890 string1 : tSTRING_BEG string_contents tSTRING_END
3891  {
3892  $$ = new_string1(heredoc_dedent($2));
3893  }
3894  ;
3895 
3896 xstring : tXSTRING_BEG xstring_contents tSTRING_END
3897  {
3898  $$ = new_xstring(heredoc_dedent($2));
3899  }
3900  ;
3901 
3902 regexp : tREGEXP_BEG regexp_contents tREGEXP_END
3903  {
3904  $$ = new_regexp($2, $3);
3905  }
3906  ;
3907 
3908 words : tWORDS_BEG ' ' tSTRING_END
3909  {
3910  /*%%%*/
3911  $$ = NEW_ZARRAY();
3912  /*%
3913  $$ = dispatch0(words_new);
3914  $$ = dispatch1(array, $$);
3915  %*/
3916  }
3917  | tWORDS_BEG word_list tSTRING_END
3918  {
3919  /*%%%*/
3920  $$ = $2;
3921  /*%
3922  $$ = dispatch1(array, $2);
3923  %*/
3924  }
3925  ;
3926 
3927 word_list : /* none */
3928  {
3929  /*%%%*/
3930  $$ = 0;
3931  /*%
3932  $$ = dispatch0(words_new);
3933  %*/
3934  }
3935  | word_list word ' '
3936  {
3937  /*%%%*/
3938  $$ = list_append($1, evstr2dstr($2));
3939  /*%
3940  $$ = dispatch2(words_add, $1, $2);
3941  %*/
3942  }
3943  ;
3944 
3945 word : string_content
3946  /*%c%*/
3947  /*%c
3948  {
3949  $$ = dispatch0(word_new);
3950  $$ = dispatch2(word_add, $$, $1);
3951  }
3952  %*/
3953  | word string_content
3954  {
3955  /*%%%*/
3956  $$ = literal_concat($1, $2);
3957  /*%
3958  $$ = dispatch2(word_add, $1, $2);
3959  %*/
3960  }
3961  ;
3962 
3963 symbols : tSYMBOLS_BEG ' ' tSTRING_END
3964  {
3965  /*%%%*/
3966  $$ = NEW_ZARRAY();
3967  /*%
3968  $$ = dispatch0(symbols_new);
3969  $$ = dispatch1(array, $$);
3970  %*/
3971  }
3972  | tSYMBOLS_BEG symbol_list tSTRING_END
3973  {
3974  /*%%%*/
3975  $$ = $2;
3976  /*%
3977  $$ = dispatch1(array, $2);
3978  %*/
3979  }
3980  ;
3981 
3982 symbol_list : /* none */
3983  {
3984  /*%%%*/
3985  $$ = 0;
3986  /*%
3987  $$ = dispatch0(symbols_new);
3988  %*/
3989  }
3990  | symbol_list word ' '
3991  {
3992  /*%%%*/
3993  $2 = evstr2dstr($2);
3994  if (nd_type($2) == NODE_DSTR) {
3995  nd_set_type($2, NODE_DSYM);
3996  }
3997  else {
3998  nd_set_type($2, NODE_LIT);
3999  $2->nd_lit = rb_str_intern($2->nd_lit);
4000  }
4001  $$ = list_append($1, $2);
4002  /*%
4003  $$ = dispatch2(symbols_add, $1, $2);
4004  %*/
4005  }
4006  ;
4007 
4008 qwords : tQWORDS_BEG ' ' tSTRING_END
4009  {
4010  /*%%%*/
4011  $$ = NEW_ZARRAY();
4012  /*%
4013  $$ = dispatch0(qwords_new);
4014  $$ = dispatch1(array, $$);
4015  %*/
4016  }
4017  | tQWORDS_BEG qword_list tSTRING_END
4018  {
4019  /*%%%*/
4020  $$ = $2;
4021  /*%
4022  $$ = dispatch1(array, $2);
4023  %*/
4024  }
4025  ;
4026 
4027 qsymbols : tQSYMBOLS_BEG ' ' tSTRING_END
4028  {
4029  /*%%%*/
4030  $$ = NEW_ZARRAY();
4031  /*%
4032  $$ = dispatch0(qsymbols_new);
4033  $$ = dispatch1(array, $$);
4034  %*/
4035  }
4036  | tQSYMBOLS_BEG qsym_list tSTRING_END
4037  {
4038  /*%%%*/
4039  $$ = $2;
4040  /*%
4041  $$ = dispatch1(array, $2);
4042  %*/
4043  }
4044  ;
4045 
4046 qword_list : /* none */
4047  {
4048  /*%%%*/
4049  $$ = 0;
4050  /*%
4051  $$ = dispatch0(qwords_new);
4052  %*/
4053  }
4054  | qword_list tSTRING_CONTENT ' '
4055  {
4056  /*%%%*/
4057  $$ = list_append($1, $2);
4058  /*%
4059  $$ = dispatch2(qwords_add, $1, $2);
4060  %*/
4061  }
4062  ;
4063 
4064 qsym_list : /* none */
4065  {
4066  /*%%%*/
4067  $$ = 0;
4068  /*%
4069  $$ = dispatch0(qsymbols_new);
4070  %*/
4071  }
4072  | qsym_list tSTRING_CONTENT ' '
4073  {
4074  /*%%%*/
4075  VALUE lit;
4076  lit = $2->nd_lit;
4077  $2->nd_lit = ID2SYM(rb_intern_str(lit));
4078  nd_set_type($2, NODE_LIT);
4079  $$ = list_append($1, $2);
4080  /*%
4081  $$ = dispatch2(qsymbols_add, $1, $2);
4082  %*/
4083  }
4084  ;
4085 
4086 string_contents : /* none */
4087  {
4088  /*%%%*/
4089  $$ = 0;
4090  /*%
4091  $$ = dispatch0(string_content);
4092  %*/
4093  }
4094  | string_contents string_content
4095  {
4096  /*%%%*/
4097  $$ = literal_concat($1, $2);
4098  /*%
4099  $$ = dispatch2(string_add, $1, $2);
4100  %*/
4101  }
4102  ;
4103 
4104 xstring_contents: /* none */
4105  {
4106  /*%%%*/
4107  $$ = 0;
4108  /*%
4109  $$ = dispatch0(xstring_new);
4110  %*/
4111  }
4112  | xstring_contents string_content
4113  {
4114  /*%%%*/
4115  $$ = literal_concat($1, $2);
4116  /*%
4117  $$ = dispatch2(xstring_add, $1, $2);
4118  %*/
4119  }
4120  ;
4121 
4122 regexp_contents: /* none */
4123  {
4124  /*%%%*/
4125  $$ = 0;
4126  /*%
4127  $$ = ripper_new_yylval(0, dispatch0(regexp_new), 0);
4128  %*/
4129  }
4130  | regexp_contents string_content
4131  {
4132  /*%%%*/
4133  NODE *head = $1, *tail = $2;
4134  if (!head) {
4135  $$ = tail;
4136  }
4137  else if (!tail) {
4138  $$ = head;
4139  }
4140  else {
4141  switch (nd_type(head)) {
4142  case NODE_STR:
4143  nd_set_type(head, NODE_DSTR);
4144  break;
4145  case NODE_DSTR:
4146  break;
4147  default:
4148  head = list_append(NEW_DSTR(Qnil), head);
4149  break;
4150  }
4151  $$ = list_append(head, tail);
4152  }
4153  /*%
4154  VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4155  if (ripper_is_node_yylval(n1)) {
4156  s1 = RNODE(n1)->nd_cval;
4157  n1 = RNODE(n1)->nd_rval;
4158  }
4159  if (ripper_is_node_yylval(n2)) {
4160  s2 = RNODE(n2)->nd_cval;
4161  n2 = RNODE(n2)->nd_rval;
4162  }
4163  $$ = dispatch2(regexp_add, n1, n2);
4164  if (!s1 && s2) {
4165  $$ = ripper_new_yylval(0, $$, s2);
4166  }
4167  %*/
4168  }
4169  ;
4170 
4171 string_content : tSTRING_CONTENT
4172  | tSTRING_DVAR
4173  {
4174  $<node>$ = lex_strterm;
4175  lex_strterm = 0;
4176  SET_LEX_STATE(EXPR_BEG);
4177  }
4178  string_dvar
4179  {
4180  lex_strterm = $<node>2;
4181  /*%%%*/
4182  $$ = NEW_EVSTR($3);
4183  /*%
4184  $$ = dispatch1(string_dvar, $3);
4185  %*/
4186  }
4187  | tSTRING_DBEG
4188  {
4189  $<val>1 = cond_stack;
4190  $<val>$ = cmdarg_stack;
4191  COND_SET(0);
4192  CMDARG_SET(0);
4193  }
4194  {
4195  $<node>$ = lex_strterm;
4196  lex_strterm = 0;
4197  }
4198  {
4199  $<num>$ = lex_state;
4200  SET_LEX_STATE(EXPR_BEG);
4201  }
4202  {
4203  $<num>$ = brace_nest;
4204  brace_nest = 0;
4205  }
4206  {
4207  $<num>$ = heredoc_indent;
4208  heredoc_indent = 0;
4209  }
4210  compstmt tSTRING_DEND
4211  {
4212  COND_SET($<val>1);
4213  CMDARG_SET($<val>2);
4214  lex_strterm = $<node>3;
4215  SET_LEX_STATE($<num>4);
4216  brace_nest = $<num>5;
4217  heredoc_indent = $<num>6;
4218  heredoc_line_indent = -1;
4219  /*%%%*/
4220  if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4221  $$ = new_evstr($7);
4222  /*%
4223  $$ = dispatch1(string_embexpr, $7);
4224  %*/
4225  }
4226  ;
4227 
4228 string_dvar : tGVAR
4229  {
4230  /*%%%*/
4231  $$ = NEW_GVAR($1);
4232  /*%
4233  $$ = dispatch1(var_ref, $1);
4234  %*/
4235  }
4236  | tIVAR
4237  {
4238  /*%%%*/
4239  $$ = NEW_IVAR($1);
4240  /*%
4241  $$ = dispatch1(var_ref, $1);
4242  %*/
4243  }
4244  | tCVAR
4245  {
4246  /*%%%*/
4247  $$ = NEW_CVAR($1);
4248  /*%
4249  $$ = dispatch1(var_ref, $1);
4250  %*/
4251  }
4252  | backref
4253  ;
4254 
4255 symbol : tSYMBEG sym
4256  {
4257  SET_LEX_STATE(EXPR_END|EXPR_ENDARG);
4258  /*%%%*/
4259  $$ = $2;
4260  /*%
4261  $$ = dispatch1(symbol, $2);
4262  %*/
4263  }
4264  ;
4265 
4266 sym : fname
4267  | tIVAR
4268  | tGVAR
4269  | tCVAR
4270  ;
4271 
4272 dsym : tSYMBEG xstring_contents tSTRING_END
4273  {
4274  SET_LEX_STATE(EXPR_END|EXPR_ENDARG);
4275  /*%%%*/
4276  $$ = dsym_node($2);
4277  /*%
4278  $$ = dispatch1(dyna_symbol, $2);
4279  %*/
4280  }
4281  ;
4282 
4283 numeric : simple_numeric
4284  | tUMINUS_NUM simple_numeric %prec tLOWEST
4285  {
4286  /*%%%*/
4287  $$ = $2;
4288  $$->nd_lit = negate_lit($$->nd_lit);
4289  /*%
4290  $$ = dispatch2(unary, ID2SYM(idUMinus), $2);
4291  %*/
4292  }
4293  ;
4294 
4295 simple_numeric : tINTEGER
4296  | tFLOAT
4297  | tRATIONAL
4298  | tIMAGINARY
4299  ;
4300 
4301 user_variable : tIDENTIFIER
4302  | tIVAR
4303  | tGVAR
4304  | tCONSTANT
4305  | tCVAR
4306  ;
4307 
4308 keyword_variable: keyword_nil {ifndef_ripper($$ = keyword_nil);}
4309  | keyword_self {ifndef_ripper($$ = keyword_self);}
4310  | keyword_true {ifndef_ripper($$ = keyword_true);}
4311  | keyword_false {ifndef_ripper($$ = keyword_false);}
4312  | keyword__FILE__ {ifndef_ripper($$ = keyword__FILE__);}
4313  | keyword__LINE__ {ifndef_ripper($$ = keyword__LINE__);}
4314  | keyword__ENCODING__ {ifndef_ripper($$ = keyword__ENCODING__);}
4315  ;
4316 
4317 var_ref : user_variable
4318  {
4319  /*%%%*/
4320  if (!($$ = gettable($1))) $$ = NEW_BEGIN(0);
4321  /*%
4322  if (id_is_var(get_id($1))) {
4323  $$ = dispatch1(var_ref, $1);
4324  }
4325  else {
4326  $$ = dispatch1(vcall, $1);
4327  }
4328  %*/
4329  }
4330  | keyword_variable
4331  {
4332  /*%%%*/
4333  if (!($$ = gettable($1))) $$ = NEW_BEGIN(0);
4334  /*%
4335  $$ = dispatch1(var_ref, $1);
4336  %*/
4337  }
4338  ;
4339 
4340 var_lhs : user_variable
4341  {
4342  $$ = assignable($1, 0);
4343  /*%%%*/
4344  /*%
4345  $$ = dispatch1(var_field, $$);
4346  %*/
4347  }
4348  | keyword_variable
4349  {
4350  $$ = assignable($1, 0);
4351  /*%%%*/
4352  /*%
4353  $$ = dispatch1(var_field, $$);
4354  %*/
4355  }
4356  ;
4357 
4358 backref : tNTH_REF
4359  | tBACK_REF
4360  ;
4361 
4362 superclass : '<'
4363  {
4364  SET_LEX_STATE(EXPR_BEG);
4365  command_start = TRUE;
4366  }
4367  expr_value term
4368  {
4369  $$ = $3;
4370  }
4371  | /* none */
4372  {
4373  /*%%%*/
4374  $$ = 0;
4375  /*%
4376  $$ = Qnil;
4377  %*/
4378  }
4379  ;
4380 
4381 f_arglist : '(' f_args rparen
4382  {
4383  /*%%%*/
4384  $$ = $2;
4385  /*%
4386  $$ = dispatch1(paren, $2);
4387  %*/
4388  SET_LEX_STATE(EXPR_BEG);
4389  command_start = TRUE;
4390  }
4391  | {
4392  $<num>$ = parser->in_kwarg;
4393  parser->in_kwarg = 1;
4394  lex_state |= EXPR_LABEL; /* force for args */
4395  }
4396  f_args term
4397  {
4398  parser->in_kwarg = !!$<num>1;
4399  $$ = $2;
4400  SET_LEX_STATE(EXPR_BEG);
4401  command_start = TRUE;
4402  }
4403  ;
4404 
4405 args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
4406  {
4407  $$ = new_args_tail($1, $3, $4);
4408  }
4409  | f_kwarg opt_f_block_arg
4410  {
4411  $$ = new_args_tail($1, Qnone, $2);
4412  }
4413  | f_kwrest opt_f_block_arg
4414  {
4415  $$ = new_args_tail(Qnone, $1, $2);
4416  }
4417  | f_block_arg
4418  {
4419  $$ = new_args_tail(Qnone, Qnone, $1);
4420  }
4421  ;
4422 
4423 opt_args_tail : ',' args_tail
4424  {
4425  $$ = $2;
4426  }
4427  | /* none */
4428  {
4429  $$ = new_args_tail(Qnone, Qnone, Qnone);
4430  }
4431  ;
4432 
4433 f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
4434  {
4435  $$ = new_args($1, $3, $5, Qnone, $6);
4436  }
4437  | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4438  {
4439  $$ = new_args($1, $3, $5, $7, $8);
4440  }
4441  | f_arg ',' f_optarg opt_args_tail
4442  {
4443  $$ = new_args($1, $3, Qnone, Qnone, $4);
4444  }
4445  | f_arg ',' f_optarg ',' f_arg opt_args_tail
4446  {
4447  $$ = new_args($1, $3, Qnone, $5, $6);
4448  }
4449  | f_arg ',' f_rest_arg opt_args_tail
4450  {
4451  $$ = new_args($1, Qnone, $3, Qnone, $4);
4452  }
4453  | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
4454  {
4455  $$ = new_args($1, Qnone, $3, $5, $6);
4456  }
4457  | f_arg opt_args_tail
4458  {
4459  $$ = new_args($1, Qnone, Qnone, Qnone, $2);
4460  }
4461  | f_optarg ',' f_rest_arg opt_args_tail
4462  {
4463  $$ = new_args(Qnone, $1, $3, Qnone, $4);
4464  }
4465  | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4466  {
4467  $$ = new_args(Qnone, $1, $3, $5, $6);
4468  }
4469  | f_optarg opt_args_tail
4470  {
4471  $$ = new_args(Qnone, $1, Qnone, Qnone, $2);
4472  }
4473  | f_optarg ',' f_arg opt_args_tail
4474  {
4475  $$ = new_args(Qnone, $1, Qnone, $3, $4);
4476  }
4477  | f_rest_arg opt_args_tail
4478  {
4479  $$ = new_args(Qnone, Qnone, $1, Qnone, $2);
4480  }
4481  | f_rest_arg ',' f_arg opt_args_tail
4482  {
4483  $$ = new_args(Qnone, Qnone, $1, $3, $4);
4484  }
4485  | args_tail
4486  {
4487  $$ = new_args(Qnone, Qnone, Qnone, Qnone, $1);
4488  }
4489  | /* none */
4490  {
4491  $$ = new_args_tail(Qnone, Qnone, Qnone);
4492  $$ = new_args(Qnone, Qnone, Qnone, Qnone, $$);
4493  }
4494  ;
4495 
4496 f_bad_arg : tCONSTANT
4497  {
4498  /*%%%*/
4499  yyerror("formal argument cannot be a constant");
4500  $$ = 0;
4501  /*%
4502  $$ = dispatch1(param_error, $1);
4503  ripper_error();
4504  %*/
4505  }
4506  | tIVAR
4507  {
4508  /*%%%*/
4509  yyerror("formal argument cannot be an instance variable");
4510  $$ = 0;
4511  /*%
4512  $$ = dispatch1(param_error, $1);
4513  ripper_error();
4514  %*/
4515  }
4516  | tGVAR
4517  {
4518  /*%%%*/
4519  yyerror("formal argument cannot be a global variable");
4520  $$ = 0;
4521  /*%
4522  $$ = dispatch1(param_error, $1);
4523  ripper_error();
4524  %*/
4525  }
4526  | tCVAR
4527  {
4528  /*%%%*/
4529  yyerror("formal argument cannot be a class variable");
4530  $$ = 0;
4531  /*%
4532  $$ = dispatch1(param_error, $1);
4533  ripper_error();
4534  %*/
4535  }
4536  ;
4537 
4538 f_norm_arg : f_bad_arg
4539  | tIDENTIFIER
4540  {
4541  formal_argument(get_id($1));
4542  $$ = $1;
4543  }
4544  ;
4545 
4546 f_arg_asgn : f_norm_arg
4547  {
4548  ID id = get_id($1);
4549  arg_var(id);
4550  current_arg = id;
4551  $$ = $1;
4552  }
4553  ;
4554 
4555 f_arg_item : f_arg_asgn
4556  {
4557  current_arg = 0;
4558  /*%%%*/
4559  $$ = NEW_ARGS_AUX($1, 1);
4560  /*%
4561  $$ = get_value($1);
4562  %*/
4563  }
4564  | tLPAREN f_margs rparen
4565  {
4566  ID tid = internal_id();
4567  arg_var(tid);
4568  /*%%%*/
4569  if (dyna_in_block()) {
4570  $2->nd_value = NEW_DVAR(tid);
4571  }
4572  else {
4573  $2->nd_value = NEW_LVAR(tid);
4574  }
4575  $$ = NEW_ARGS_AUX(tid, 1);
4576  $$->nd_next = $2;
4577  /*%
4578  $$ = dispatch1(mlhs_paren, $2);
4579  %*/
4580  }
4581  ;
4582 
4583 f_arg : f_arg_item
4584  /*%c%*/
4585  /*%c
4586  {
4587  $$ = rb_ary_new3(1, $1);
4588  }
4589  c%*/
4590  | f_arg ',' f_arg_item
4591  {
4592  /*%%%*/
4593  $$ = $1;
4594  $$->nd_plen++;
4595  $$->nd_next = block_append($$->nd_next, $3->nd_next);
4596  rb_gc_force_recycle((VALUE)$3);
4597  /*%
4598  $$ = rb_ary_push($1, $3);
4599  %*/
4600  }
4601  ;
4602 
4603 
4604 f_label : tLABEL
4605  {
4606  ID id = get_id($1);
4607  arg_var(formal_argument(id));
4608  current_arg = id;
4609  $$ = $1;
4610  }
4611  ;
4612 
4613 f_kw : f_label arg_value
4614  {
4615  current_arg = 0;
4616  $$ = assignable($1, $2);
4617  /*%%%*/
4618  $$ = new_kw_arg($$);
4619  /*%
4620  $$ = rb_assoc_new($$, $2);
4621  %*/
4622  }
4623  | f_label
4624  {
4625  current_arg = 0;
4626  $$ = assignable($1, (NODE *)-1);
4627  /*%%%*/
4628  $$ = new_kw_arg($$);
4629  /*%
4630  $$ = rb_assoc_new($$, 0);
4631  %*/
4632  }
4633  ;
4634 
4635 f_block_kw : f_label primary_value
4636  {
4637  $$ = assignable($1, $2);
4638  /*%%%*/
4639  $$ = new_kw_arg($$);
4640  /*%
4641  $$ = rb_assoc_new($$, $2);
4642  %*/
4643  }
4644  | f_label
4645  {
4646  $$ = assignable($1, (NODE *)-1);
4647  /*%%%*/
4648  $$ = new_kw_arg($$);
4649  /*%
4650  $$ = rb_assoc_new($$, 0);
4651  %*/
4652  }
4653  ;
4654 
4655 f_block_kwarg : f_block_kw
4656  {
4657  /*%%%*/
4658  $$ = $1;
4659  /*%
4660  $$ = rb_ary_new3(1, $1);
4661  %*/
4662  }
4663  | f_block_kwarg ',' f_block_kw
4664  {
4665  /*%%%*/
4666  $$ = kwd_append($1, $3);
4667  /*%
4668  $$ = rb_ary_push($1, $3);
4669  %*/
4670  }
4671  ;
4672 
4673 
4674 f_kwarg : f_kw
4675  {
4676  /*%%%*/
4677  $$ = $1;
4678  /*%
4679  $$ = rb_ary_new3(1, $1);
4680  %*/
4681  }
4682  | f_kwarg ',' f_kw
4683  {
4684  /*%%%*/
4685  $$ = kwd_append($1, $3);
4686  /*%
4687  $$ = rb_ary_push($1, $3);
4688  %*/
4689  }
4690  ;
4691 
4692 kwrest_mark : tPOW
4693  | tDSTAR
4694  ;
4695 
4696 f_kwrest : kwrest_mark tIDENTIFIER
4697  {
4698  shadowing_lvar(get_id($2));
4699  $$ = $2;
4700  }
4701  | kwrest_mark
4702  {
4703  $$ = internal_id();
4704  arg_var($$);
4705  }
4706  ;
4707 
4708 f_opt : f_arg_asgn '=' arg_value
4709  {
4710  current_arg = 0;
4711  $$ = assignable($1, $3);
4712  /*%%%*/
4713  $$ = NEW_OPT_ARG(0, $$);
4714  /*%
4715  $$ = rb_assoc_new($$, $3);
4716  %*/
4717  }
4718  ;
4719 
4720 f_block_opt : f_arg_asgn '=' primary_value
4721  {
4722  current_arg = 0;
4723  $$ = assignable($1, $3);
4724  /*%%%*/
4725  $$ = NEW_OPT_ARG(0, $$);
4726  /*%
4727  $$ = rb_assoc_new($$, $3);
4728  %*/
4729  }
4730  ;
4731 
4732 f_block_optarg : f_block_opt
4733  {
4734  /*%%%*/
4735  $$ = $1;
4736  /*%
4737  $$ = rb_ary_new3(1, $1);
4738  %*/
4739  }
4740  | f_block_optarg ',' f_block_opt
4741  {
4742  /*%%%*/
4743  NODE *opts = $1;
4744 
4745  while (opts->nd_next) {
4746  opts = opts->nd_next;
4747  }
4748  opts->nd_next = $3;
4749  $$ = $1;
4750  /*%
4751  $$ = rb_ary_push($1, $3);
4752  %*/
4753  }
4754  ;
4755 
4756 f_optarg : f_opt
4757  {
4758  /*%%%*/
4759  $$ = $1;
4760  /*%
4761  $$ = rb_ary_new3(1, $1);
4762  %*/
4763  }
4764  | f_optarg ',' f_opt
4765  {
4766  /*%%%*/
4767  NODE *opts = $1;
4768 
4769  while (opts->nd_next) {
4770  opts = opts->nd_next;
4771  }
4772  opts->nd_next = $3;
4773  $$ = $1;
4774  /*%
4775  $$ = rb_ary_push($1, $3);
4776  %*/
4777  }
4778  ;
4779 
4780 restarg_mark : '*'
4781  | tSTAR
4782  ;
4783 
4784 f_rest_arg : restarg_mark tIDENTIFIER
4785  {
4786  /*%%%*/
4787  if (!is_local_id($2))
4788  yyerror("rest argument must be local variable");
4789  /*% %*/
4790  arg_var(shadowing_lvar(get_id($2)));
4791  /*%%%*/
4792  $$ = $2;
4793  /*%
4794  $$ = dispatch1(rest_param, $2);
4795  %*/
4796  }
4797  | restarg_mark
4798  {
4799  /*%%%*/
4800  $$ = internal_id();
4801  arg_var($$);
4802  /*%
4803  $$ = dispatch1(rest_param, Qnil);
4804  %*/
4805  }
4806  ;
4807 
4808 blkarg_mark : '&'
4809  | tAMPER
4810  ;
4811 
4812 f_block_arg : blkarg_mark tIDENTIFIER
4813  {
4814  /*%%%*/
4815  if (!is_local_id($2))
4816  yyerror("block argument must be local variable");
4817  else if (!dyna_in_block() && local_id($2))
4818  yyerror("duplicated block argument name");
4819  /*% %*/
4820  arg_var(shadowing_lvar(get_id($2)));
4821  /*%%%*/
4822  $$ = $2;
4823  /*%
4824  $$ = dispatch1(blockarg, $2);
4825  %*/
4826  }
4827  ;
4828 
4829 opt_f_block_arg : ',' f_block_arg
4830  {
4831  $$ = $2;
4832  }
4833  | none
4834  {
4835  /*%%%*/
4836  $$ = 0;
4837  /*%
4838  $$ = Qundef;
4839  %*/
4840  }
4841  ;
4842 
4843 singleton : var_ref
4844  {
4845  /*%%%*/
4846  value_expr($1);
4847  $$ = $1;
4848  if (!$$) $$ = NEW_NIL();
4849  /*%
4850  $$ = $1;
4851  %*/
4852  }
4853  | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
4854  {
4855  /*%%%*/
4856  if ($3 == 0) {
4857  yyerror("can't define singleton method for ().");
4858  }
4859  else {
4860  switch (nd_type($3)) {
4861  case NODE_STR:
4862  case NODE_DSTR:
4863  case NODE_XSTR:
4864  case NODE_DXSTR:
4865  case NODE_DREGX:
4866  case NODE_LIT:
4867  case NODE_ARRAY:
4868  case NODE_ZARRAY:
4869  yyerror("can't define singleton method for literals");
4870  default:
4871  value_expr($3);
4872  break;
4873  }
4874  }
4875  $$ = $3;
4876  /*%
4877  $$ = dispatch1(paren, $3);
4878  %*/
4879  }
4880  ;
4881 
4882 assoc_list : none
4883  | assocs trailer
4884  {
4885  /*%%%*/
4886  $$ = $1;
4887  /*%
4888  $$ = dispatch1(assoclist_from_args, $1);
4889  %*/
4890  }
4891  ;
4892 
4893 assocs : assoc
4894  /*%c%*/
4895  /*%c
4896  {
4897  $$ = rb_ary_new3(1, $1);
4898  }
4899  %*/
4900  | assocs ',' assoc
4901  {
4902  /*%%%*/
4903  NODE *assocs = $1;
4904  NODE *tail = $3;
4905  if (!assocs) {
4906  assocs = tail;
4907  }
4908  else if (tail) {
4909  if (assocs->nd_head &&
4910  !tail->nd_head && nd_type(tail->nd_next) == NODE_ARRAY &&
4911  nd_type(tail->nd_next->nd_head) == NODE_HASH) {
4912  /* DSTAR */
4913  tail = tail->nd_next->nd_head->nd_head;
4914  }
4915  assocs = list_concat(assocs, tail);
4916  }
4917  $$ = assocs;
4918  /*%
4919  $$ = rb_ary_push($1, $3);
4920  %*/
4921  }
4922  ;
4923 
4924 assoc : arg_value tASSOC arg_value
4925  {
4926  /*%%%*/
4927  if (nd_type($1) == NODE_STR) {
4928  nd_set_type($1, NODE_LIT);
4929  $1->nd_lit = rb_fstring($1->nd_lit);
4930  }
4931  $$ = list_append(NEW_LIST($1), $3);
4932  /*%
4933  $$ = dispatch2(assoc_new, $1, $3);
4934  %*/
4935  }
4936  | tLABEL arg_value
4937  {
4938  /*%%%*/
4939  $$ = list_append(NEW_LIST(NEW_LIT(ID2SYM($1))), $2);
4940  /*%
4941  $$ = dispatch2(assoc_new, $1, $2);
4942  %*/
4943  }
4944  | tSTRING_BEG string_contents tLABEL_END arg_value
4945  {
4946  /*%%%*/
4947  $$ = list_append(NEW_LIST(dsym_node($2)), $4);
4948  /*%
4949  $$ = dispatch2(assoc_new, dispatch1(dyna_symbol, $2), $4);
4950  %*/
4951  }
4952  | tDSTAR arg_value
4953  {
4954  /*%%%*/
4955  if (nd_type($2) == NODE_HASH &&
4956  !($2->nd_head && $2->nd_head->nd_alen))
4957  $$ = 0;
4958  else
4959  $$ = list_append(NEW_LIST(0), $2);
4960  /*%
4961  $$ = dispatch1(assoc_splat, $2);
4962  %*/
4963  }
4964  ;
4965 
4966 operation : tIDENTIFIER
4967  | tCONSTANT
4968  | tFID
4969  ;
4970 
4971 operation2 : tIDENTIFIER
4972  | tCONSTANT
4973  | tFID
4974  | op
4975  ;
4976 
4977 operation3 : tIDENTIFIER
4978  | tFID
4979  | op
4980  ;
4981 
4982 dot_or_colon : '.'
4983  /*%c%*/
4984  /*%c
4985  { $$ = $<val>1; }
4986  %*/
4987  | tCOLON2
4988  /*%c%*/
4989  /*%c
4990  { $$ = $<val>1; }
4991  %*/
4992  ;
4993 
4994 call_op : '.'
4995  {
4996  /*%%%*/
4997  $$ = '.';
4998  /*%
4999  $$ = ripper_id2sym('.');
5000  %*/
5001  }
5002  | tANDDOT
5003  {
5004  /*%%%*/
5005  $$ = tANDDOT;
5006  /*%
5007  $$ = ripper_id2sym(idANDDOT);
5008  %*/
5009  }
5010  ;
5011 
5012 call_op2 : call_op
5013  | tCOLON2
5014  {
5015  /*%%%*/
5016  $$ = tCOLON2;
5017  /*%
5018  $$ = ripper_id2sym(idCOLON2);
5019  %*/
5020  }
5021  ;
5022 
5023 opt_terms : /* none */
5024  | terms
5025  ;
5026 
5027 opt_nl : /* none */
5028  | '\n'
5029  ;
5030 
5031 rparen : opt_nl ')'
5032  ;
5033 
5034 rbracket : opt_nl ']'
5035  ;
5036 
5037 trailer : /* none */
5038  | '\n'
5039  | ','
5040  ;
5041 
5042 term : ';' {yyerrok;}
5043  | '\n'
5044  ;
5045 
5046 terms : term
5047  | terms ';' {yyerrok;}
5048  ;
5049 
5050 none : /* none */
5051  {
5052  /*%%%*/
5053  $$ = 0;
5054  /*%
5055  $$ = Qundef;
5056  %*/
5057  }
5058  ;
5059 %%
5060 # undef parser
5061 # undef yylex
5062 # undef yylval
5063 # define yylval (*parser->lval)
5064 
5065 static int parser_regx_options(struct parser_params*);
5066 static int parser_tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**);
5067 static void parser_tokaddmbc(struct parser_params *parser, int c, rb_encoding *enc);
5068 static int parser_parse_string(struct parser_params*,NODE*);
5069 static int parser_here_document(struct parser_params*,NODE*);
5070 
5071 
5072 # define nextc() parser_nextc(parser)
5073 # define pushback(c) parser_pushback(parser, (c))
5074 # define newtok() parser_newtok(parser)
5075 # define tokspace(n) parser_tokspace(parser, (n))
5076 # define tokadd(c) parser_tokadd(parser, (c))
5077 # define tok_hex(numlen) parser_tok_hex(parser, (numlen))
5078 # define read_escape(flags,e) parser_read_escape(parser, (flags), (e))
5079 # define tokadd_escape(e) parser_tokadd_escape(parser, (e))
5080 # define regx_options() parser_regx_options(parser)
5081 # define tokadd_string(f,t,p,n,e) parser_tokadd_string(parser,(f),(t),(p),(n),(e))
5082 # define parse_string(n) parser_parse_string(parser,(n))
5083 # define tokaddmbc(c, enc) parser_tokaddmbc(parser, (c), (enc))
5084 # define here_document(n) parser_here_document(parser,(n))
5085 # define heredoc_identifier() parser_heredoc_identifier(parser)
5086 # define heredoc_restore(n) parser_heredoc_restore(parser,(n))
5087 # define whole_match_p(e,l,i) parser_whole_match_p(parser,(e),(l),(i))
5088 # define number_literal_suffix(f) parser_number_literal_suffix(parser, (f))
5089 # define set_number_literal(v, t, f) parser_set_number_literal(parser, (v), (t), (f))
5090 # define set_integer_literal(v, f) parser_set_integer_literal(parser, (v), (f))
5091 
5092 #ifndef RIPPER
5093 # define set_yylval_str(x) (yylval.node = NEW_STR(x))
5094 # define set_yylval_num(x) (yylval.num = (x))
5095 # define set_yylval_id(x) (yylval.id = (x))
5096 # define set_yylval_name(x) (yylval.id = (x))
5097 # define set_yylval_literal(x) (yylval.node = NEW_LIT(x))
5098 # define set_yylval_node(x) (yylval.node = (x))
5099 # define yylval_id() (yylval.id)
5100 #else
5101 static inline VALUE
5102 ripper_yylval_id(ID x)
5103 {
5104  return ripper_new_yylval(x, ID2SYM(x), 0);
5105 }
5106 # define set_yylval_str(x) (yylval.val = (x))
5107 # define set_yylval_num(x) (yylval.val = ripper_new_yylval((x), 0, 0))
5108 # define set_yylval_id(x) (void)(x)
5109 # define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(x))
5110 # define set_yylval_literal(x) (void)(x)
5111 # define set_yylval_node(x) (void)(x)
5112 # define yylval_id() yylval.id
5113 #endif
5114 
5115 #ifndef RIPPER
5116 #define ripper_flush(p) (void)(p)
5117 #define dispatch_scan_event(t) ((void)0)
5118 #define dispatch_delayed_token(t) ((void)0)
5119 #define has_delayed_token() (0)
5120 #else
5121 #define ripper_flush(p) ((p)->tokp = (p)->lex.pcur)
5122 
5123 #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5124 
5125 static inline VALUE
5126 intern_sym(const char *name)
5127 {
5128  ID id = rb_intern_const(name);
5129  return ID2SYM(id);
5130 }
5131 
5132 static int
5133 ripper_has_scan_event(struct parser_params *parser)
5134 {
5135 
5136  if (lex_p < parser->tokp) rb_raise(rb_eRuntimeError, "lex_p < tokp");
5137  return lex_p > parser->tokp;
5138 }
5139 
5140 static VALUE
5141 ripper_scan_event_val(struct parser_params *parser, int t)
5142 {
5143  VALUE str = STR_NEW(parser->tokp, lex_p - parser->tokp);
5144  VALUE rval = ripper_dispatch1(parser, ripper_token2eventid(t), str);
5145  ripper_flush(parser);
5146  return rval;
5147 }
5148 
5149 static void
5150 ripper_dispatch_scan_event(struct parser_params *parser, int t)
5151 {
5152  if (!ripper_has_scan_event(parser)) return;
5153  yylval_rval = ripper_scan_event_val(parser, t);
5154 }
5155 #define dispatch_scan_event(t) ripper_dispatch_scan_event(parser, t)
5156 
5157 static void
5158 ripper_dispatch_delayed_token(struct parser_params *parser, int t)
5159 {
5160  int saved_line = ruby_sourceline;
5161  const char *saved_tokp = parser->tokp;
5162 
5163  ruby_sourceline = parser->delayed_line;
5164  parser->tokp = lex_pbeg + parser->delayed_col;
5165  yylval_rval = ripper_dispatch1(parser, ripper_token2eventid(t), parser->delayed);
5166  parser->delayed = Qnil;
5167  ruby_sourceline = saved_line;
5168  parser->tokp = saved_tokp;
5169 }
5170 #define dispatch_delayed_token(t) ripper_dispatch_delayed_token(parser, t)
5171 #define has_delayed_token() (!NIL_P(parser->delayed))
5172 #endif /* RIPPER */
5173 
5174 #include "ruby/regex.h"
5175 #include "ruby/util.h"
5176 
5177 #define parser_encoding_name() (current_enc->name)
5178 #define parser_mbclen() mbclen((lex_p-1),lex_pend,current_enc)
5179 #define is_identchar(p,e,enc) (rb_enc_isalnum((unsigned char)(*(p)),(enc)) || (*(p)) == '_' || !ISASCII(*(p)))
5180 #define parser_is_identchar() (!parser->eofp && is_identchar((lex_p-1),lex_pend,current_enc))
5181 
5182 #define parser_isascii() ISASCII(*(lex_p-1))
5183 
5184 static int
5185 token_info_get_column(struct parser_params *parser, const char *pend)
5186 {
5187  int column = 1;
5188  const char *p;
5189  for (p = lex_pbeg; p < pend; p++) {
5190  if (*p == '\t') {
5191  column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5192  }
5193  column++;
5194  }
5195  return column;
5196 }
5197 
5198 static int
5199 token_info_has_nonspaces(struct parser_params *parser, const char *pend)
5200 {
5201  const char *p;
5202  for (p = lex_pbeg; p < pend; p++) {
5203  if (*p != ' ' && *p != '\t') {
5204  return 1;
5205  }
5206  }
5207  return 0;
5208 }
5209 
5210 static void
5211 token_info_push_gen(struct parser_params *parser, const char *token, size_t len)
5212 {
5213  token_info *ptinfo;
5214  const char *t = lex_p - len;
5215 
5216  if (!parser->token_info_enabled) return;
5217  ptinfo = ALLOC(token_info);
5218  ptinfo->token = token;
5219  ptinfo->linenum = ruby_sourceline;
5220  ptinfo->column = token_info_get_column(parser, t);
5221  ptinfo->nonspc = token_info_has_nonspaces(parser, t);
5222  ptinfo->next = parser->token_info;
5223 
5224  parser->token_info = ptinfo;
5225 }
5226 
5227 static void
5228 token_info_pop_gen(struct parser_params *parser, const char *token, size_t len)
5229 {
5230  int linenum;
5231  token_info *ptinfo = parser->token_info;
5232  const char *t = lex_p - len;
5233 
5234  if (!ptinfo) return;
5235  parser->token_info = ptinfo->next;
5236  linenum = ruby_sourceline;
5237  if (parser->token_info_enabled &&
5238  linenum != ptinfo->linenum && !ptinfo->nonspc &&
5239  !token_info_has_nonspaces(parser, t) &&
5240  token_info_get_column(parser, t) != ptinfo->column) {
5241  rb_warn3L(linenum,
5242  "mismatched indentations at '%s' with '%s' at %d",
5243  WARN_S(token), WARN_S(ptinfo->token), WARN_I(ptinfo->linenum));
5244  }
5245 
5246  xfree(ptinfo);
5247 }
5248 
5249 static int
5250 parser_precise_mbclen(struct parser_params *parser, const char *p)
5251 {
5252  int len = rb_enc_precise_mbclen(p, lex_pend, current_enc);
5253  if (!MBCLEN_CHARFOUND_P(len)) {
5254  compile_error(PARSER_ARG "invalid multibyte char (%s)", parser_encoding_name());
5255  return -1;
5256  }
5257  return len;
5258 }
5259 
5260 static int
5261 parser_yyerror(struct parser_params *parser, const char *msg)
5262 {
5263 #ifndef RIPPER
5264  const int max_line_margin = 30;
5265  const char *p, *pe;
5266  const char *pre = "", *post = "";
5267  const char *code = "", *caret = "", *newline = "";
5268  const char *lim;
5269  char *buf;
5270  long len;
5271  int i;
5272 
5273  p = lex_p;
5274  lim = p - lex_pbeg > max_line_margin ? p - max_line_margin : lex_pbeg;
5275  while (lim < p) {
5276  if (*(p-1) == '\n') break;
5277  p--;
5278  }
5279 
5280  pe = lex_p;
5281  lim = lex_pend - pe > max_line_margin ? pe + max_line_margin : lex_pend;
5282  while (pe < lim) {
5283  if (*pe == '\n') break;
5284  pe++;
5285  }
5286 
5287  len = pe - p;
5288  if (len > 4) {
5289  char *p2;
5290 
5291  if (len > max_line_margin * 2 + 10) {
5292  if (lex_p - p > max_line_margin) {
5293  p = rb_enc_prev_char(p, lex_p - max_line_margin, pe, rb_enc_get(lex_lastline));
5294  pre = "...";
5295  }
5296  if (pe - lex_p > max_line_margin) {
5297  pe = rb_enc_prev_char(lex_p, lex_p + max_line_margin, pe, rb_enc_get(lex_lastline));
5298  post = "...";
5299  }
5300  len = pe - p;
5301  }
5302  i = (int)(lex_p - p);
5303  buf = ALLOCA_N(char, i+2);
5304  code = p;
5305  caret = p2 = buf;
5306  while (i-- > 0) {
5307  *p2++ = *p++ == '\t' ? '\t' : ' ';
5308  }
5309  *p2++ = '^';
5310  *p2 = '\0';
5311  newline = "\n";
5312  }
5313  else {
5314  len = 0;
5315  }
5316  compile_error(PARSER_ARG "%s%s""%s%.*s%s%s""%s%s",
5317  msg, newline,
5318  pre, (int)len, code, post, newline,
5319  pre, caret);
5320 #else
5321  dispatch1(parse_error, STR_NEW2(msg));
5322  ripper_error();
5323 #endif /* !RIPPER */
5324  return 0;
5325 }
5326 
5327 static void parser_prepare(struct parser_params *parser);
5328 
5329 #ifndef RIPPER
5330 static VALUE
5331 debug_lines(VALUE fname)
5332 {
5333  ID script_lines;
5334  CONST_ID(script_lines, "SCRIPT_LINES__");
5335  if (rb_const_defined_at(rb_cObject, script_lines)) {
5336  VALUE hash = rb_const_get_at(rb_cObject, script_lines);
5337  if (RB_TYPE_P(hash, T_HASH)) {
5338  VALUE lines = rb_ary_new();
5339  rb_hash_aset(hash, fname, lines);
5340  return lines;
5341  }
5342  }
5343  return 0;
5344 }
5345 
5346 static VALUE
5347 coverage(VALUE fname, int n)
5348 {
5349  VALUE coverages = rb_get_coverages();
5350  if (RTEST(coverages) && RBASIC(coverages)->klass == 0) {
5351  VALUE lines = n > 0 ? rb_ary_tmp_new_fill(n) : rb_ary_tmp_new(0);
5352  rb_hash_aset(coverages, fname, lines);
5353  return lines;
5354  }
5355  return 0;
5356 }
5357 
5358 static int
5359 e_option_supplied(struct parser_params *parser)
5360 {
5361  return strcmp(ruby_sourcefile, "-e") == 0;
5362 }
5363 
5364 static VALUE
5365 yycompile0(VALUE arg)
5366 {
5367  int n;
5368  NODE *tree;
5369  struct parser_params *parser = (struct parser_params *)arg;
5370  VALUE cov = Qfalse;
5371 
5372  if (!compile_for_eval && rb_safe_level() == 0) {
5373  ruby_debug_lines = debug_lines(ruby_sourcefile_string);
5374  if (ruby_debug_lines && ruby_sourceline > 0) {
5375  VALUE str = STR_NEW0();
5376  n = ruby_sourceline;
5377  do {
5378  rb_ary_push(ruby_debug_lines, str);
5379  } while (--n);
5380  }
5381 
5382  if (!e_option_supplied(parser)) {
5383  ruby_coverage = coverage(ruby_sourcefile_string, ruby_sourceline);
5384  cov = Qtrue;
5385  }
5386  }
5387 
5388  parser_prepare(parser);
5389 #ifndef RIPPER
5390 #define RUBY_DTRACE_PARSE_HOOK(name) \
5391  if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
5392  RUBY_DTRACE_PARSE_##name(ruby_sourcefile, ruby_sourceline); \
5393  }
5394  RUBY_DTRACE_PARSE_HOOK(BEGIN);
5395 #endif
5396  n = yyparse((void*)parser);
5397 #ifndef RIPPER
5398  RUBY_DTRACE_PARSE_HOOK(END);
5399 #endif
5400  ruby_debug_lines = 0;
5401  ruby_coverage = 0;
5402 
5403  lex_strterm = 0;
5404  lex_p = lex_pbeg = lex_pend = 0;
5405  lex_lastline = lex_nextline = 0;
5406  if (parser->error_p) {
5407  VALUE mesg = parser->error_buffer;
5408  if (!mesg) {
5409  mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
5410  }
5411  rb_set_errinfo(mesg);
5412  return 0;
5413  }
5414  tree = ruby_eval_tree;
5415  if (!tree) {
5416  tree = NEW_NIL();
5417  }
5418  else {
5419  VALUE opt = parser->compile_option;
5420  if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
5421  rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
5422  tree->nd_body = NEW_PRELUDE(ruby_eval_tree_begin, tree->nd_body, opt);
5423  }
5424  return (VALUE)tree;
5425 }
5426 
5427 static NODE*
5428 yycompile(struct parser_params *parser, VALUE fname, int line)
5429 {
5430  ruby_sourcefile_string = rb_str_new_frozen(fname);
5431  ruby_sourcefile = RSTRING_PTR(fname);
5432  ruby_sourceline = line - 1;
5433  return (NODE *)rb_suppress_tracing(yycompile0, (VALUE)parser);
5434 }
5435 #endif /* !RIPPER */
5436 
5437 static rb_encoding *
5438 must_be_ascii_compatible(VALUE s)
5439 {
5440  rb_encoding *enc = rb_enc_get(s);
5441  if (!rb_enc_asciicompat(enc)) {
5442  rb_raise(rb_eArgError, "invalid source encoding");
5443  }
5444  return enc;
5445 }
5446 
5447 static VALUE
5448 lex_get_str(struct parser_params *parser, VALUE s)
5449 {
5450  char *beg, *end, *start;
5451  long len;
5452 
5453  beg = RSTRING_PTR(s);
5454  len = RSTRING_LEN(s);
5455  start = beg;
5456  if (lex_gets_ptr) {
5457  if (len == lex_gets_ptr) return Qnil;
5458  beg += lex_gets_ptr;
5459  len -= lex_gets_ptr;
5460  }
5461  end = memchr(beg, '\n', len);
5462  if (end) len = ++end - beg;
5463  lex_gets_ptr += len;
5464  return rb_str_subseq(s, beg - start, len);
5465 }
5466 
5467 static VALUE
5468 lex_getline(struct parser_params *parser)
5469 {
5470  VALUE line = (*lex_gets)(parser, lex_input);
5471  if (NIL_P(line)) return line;
5472  must_be_ascii_compatible(line);
5473 #ifndef RIPPER
5474  if (ruby_debug_lines) {
5475  rb_enc_associate(line, current_enc);
5476  rb_ary_push(ruby_debug_lines, line);
5477  }
5478  if (ruby_coverage) {
5479  rb_ary_push(ruby_coverage, Qnil);
5480  }
5481 #endif
5482  return line;
5483 }
5484 
5485 static const rb_data_type_t parser_data_type;
5486 
5487 #ifndef RIPPER
5488 static NODE*
5489 parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
5490 {
5491  struct parser_params *parser;
5492  NODE *node;
5493 
5494  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
5495  lex_gets = lex_get_str;
5496  lex_gets_ptr = 0;
5497  lex_input = rb_str_new_frozen(s);
5498  lex_pbeg = lex_p = lex_pend = 0;
5499 
5500  node = yycompile(parser, fname, line);
5501  RB_GC_GUARD(vparser); /* prohibit tail call optimization */
5502 
5503  return node;
5504 }
5505 
5506 NODE*
5507 rb_compile_string(const char *f, VALUE s, int line)
5508 {
5509  must_be_ascii_compatible(s);
5510  return parser_compile_string(rb_parser_new(), rb_filesystem_str_new_cstr(f), s, line);
5511 }
5512 
5513 NODE*
5514 rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
5515 {
5516  return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
5517 }
5518 
5519 NODE*
5520 rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
5521 {
5522  must_be_ascii_compatible(s);
5523  return parser_compile_string(vparser, f, s, line);
5524 }
5525 
5526 NODE*
5527 rb_compile_cstr(const char *f, const char *s, int len, int line)
5528 {
5529  VALUE str = rb_str_new(s, len);
5530  return parser_compile_string(rb_parser_new(), rb_filesystem_str_new_cstr(f), str, line);
5531 }
5532 
5533 NODE*
5534 rb_parser_compile_cstr(VALUE vparser, const char *f, const char *s, int len, int line)
5535 {
5536  VALUE str = rb_str_new(s, len);
5537  return parser_compile_string(vparser, rb_filesystem_str_new_cstr(f), str, line);
5538 }
5539 
5540 VALUE rb_io_gets_internal(VALUE io);
5541 
5542 static VALUE
5543 lex_io_gets(struct parser_params *parser, VALUE io)
5544 {
5545  return rb_io_gets_internal(io);
5546 }
5547 
5548 NODE*
5549 rb_compile_file(const char *f, VALUE file, int start)
5550 {
5551  VALUE vparser = rb_parser_new();
5552 
5553  return rb_parser_compile_file(vparser, f, file, start);
5554 }
5555 
5556 NODE*
5557 rb_parser_compile_file(VALUE vparser, const char *f, VALUE file, int start)
5558 {
5559  return rb_parser_compile_file_path(vparser, rb_filesystem_str_new_cstr(f), file, start);
5560 }
5561 
5562 NODE*
5563 rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
5564 {
5565  struct parser_params *parser;
5566  NODE *node;
5567 
5568  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
5569  lex_gets = lex_io_gets;
5570  lex_input = file;
5571  lex_pbeg = lex_p = lex_pend = 0;
5572 
5573  node = yycompile(parser, fname, start);
5574  RB_GC_GUARD(vparser); /* prohibit tail call optimization */
5575 
5576  return node;
5577 }
5578 #endif /* !RIPPER */
5579 
5580 #define STR_FUNC_ESCAPE 0x01
5581 #define STR_FUNC_EXPAND 0x02
5582 #define STR_FUNC_REGEXP 0x04
5583 #define STR_FUNC_QWORDS 0x08
5584 #define STR_FUNC_SYMBOL 0x10
5585 #define STR_FUNC_INDENT 0x20
5586 #define STR_FUNC_LABEL 0x40
5587 #define STR_TERM_END -1
5588 
5589 enum string_type {
5590  str_label = STR_FUNC_LABEL,
5591  str_squote = (0),
5592  str_dquote = (STR_FUNC_EXPAND),
5593  str_xquote = (STR_FUNC_EXPAND),
5594  str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
5595  str_sword = (STR_FUNC_QWORDS),
5596  str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND),
5597  str_ssym = (STR_FUNC_SYMBOL),
5598  str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
5599 };
5600 
5601 static VALUE
5602 parser_str_new(const char *p, long n, rb_encoding *enc, int func, rb_encoding *enc0)
5603 {
5604  VALUE str;
5605 
5606  str = rb_enc_str_new(p, n, enc);
5607  if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
5608  if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
5609  }
5610  else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
5611  rb_enc_associate(str, rb_ascii8bit_encoding());
5612  }
5613  }
5614 
5615  return str;
5616 }
5617 
5618 #define lex_goto_eol(parser) ((parser)->lex.pcur = (parser)->lex.pend)
5619 #define lex_eol_p() (lex_p >= lex_pend)
5620 #define peek(c) peek_n((c), 0)
5621 #define peek_n(c,n) (lex_p+(n) < lex_pend && (c) == (unsigned char)lex_p[n])
5622 #define peekc() peekc_n(0)
5623 #define peekc_n(n) (lex_p+(n) < lex_pend ? (unsigned char)lex_p[n] : -1)
5624 
5625 static int
5626 parser_nextline(struct parser_params *parser)
5627 {
5628  VALUE v = lex_nextline;
5629  lex_nextline = 0;
5630  if (!v) {
5631  if (parser->eofp)
5632  return -1;
5633 
5634  if (!lex_input || NIL_P(v = lex_getline(parser))) {
5635  parser->eofp = 1;
5636  lex_goto_eol(parser);
5637  return -1;
5638  }
5639  parser->cr_seen = FALSE;
5640  }
5641 #ifdef RIPPER
5642  if (parser->tokp < lex_pend) {
5643  if (!has_delayed_token()) {
5644  parser->delayed = rb_str_buf_new(1024);
5645  rb_enc_associate(parser->delayed, current_enc);
5646  rb_str_buf_cat(parser->delayed,
5647  parser->tokp, lex_pend - parser->tokp);
5648  parser->delayed_line = ruby_sourceline;
5649  parser->delayed_col = (int)(parser->tokp - lex_pbeg);
5650  }
5651  else {
5652  rb_str_buf_cat(parser->delayed,
5653  parser->tokp, lex_pend - parser->tokp);
5654  }
5655  }
5656 #endif
5657  if (heredoc_end > 0) {
5658  ruby_sourceline = heredoc_end;
5659  heredoc_end = 0;
5660  }
5661  ruby_sourceline++;
5662  parser->line_count++;
5663  lex_pbeg = lex_p = RSTRING_PTR(v);
5664  lex_pend = lex_p + RSTRING_LEN(v);
5665  ripper_flush(parser);
5666  lex_lastline = v;
5667  return 0;
5668 }
5669 
5670 static int
5671 parser_cr(struct parser_params *parser, int c)
5672 {
5673  if (peek('\n')) {
5674  lex_p++;
5675  c = '\n';
5676  }
5677  else if (!parser->cr_seen) {
5678  parser->cr_seen = TRUE;
5679  /* carried over with lex_nextline for nextc() */
5680  rb_warn0("encountered \\r in middle of line, treated as a mere space");
5681  }
5682  return c;
5683 }
5684 
5685 static inline int
5686 parser_nextc(struct parser_params *parser)
5687 {
5688  int c;
5689 
5690  if (UNLIKELY(lex_p == lex_pend)) {
5691  if (parser_nextline(parser)) return -1;
5692  }
5693  c = (unsigned char)*lex_p++;
5694  if (UNLIKELY(c == '\r')) {
5695  c = parser_cr(parser, c);
5696  }
5697 
5698  return c;
5699 }
5700 
5701 static void
5702 parser_pushback(struct parser_params *parser, int c)
5703 {
5704  if (c == -1) return;
5705  lex_p--;
5706  if (lex_p > lex_pbeg && lex_p[0] == '\n' && lex_p[-1] == '\r') {
5707  lex_p--;
5708  }
5709 }
5710 
5711 #define was_bol() (lex_p == lex_pbeg + 1)
5712 
5713 #define tokfix() (tokenbuf[tokidx]='\0')
5714 #define tok() tokenbuf
5715 #define toklen() tokidx
5716 #define toklast() (tokidx>0?tokenbuf[tokidx-1]:0)
5717 
5718 static char*
5719 parser_newtok(struct parser_params *parser)
5720 {
5721  tokidx = 0;
5722  tokline = ruby_sourceline;
5723  if (!tokenbuf) {
5724  toksiz = 60;
5725  tokenbuf = ALLOC_N(char, 60);
5726  }
5727  if (toksiz > 4096) {
5728  toksiz = 60;
5729  REALLOC_N(tokenbuf, char, 60);
5730  }
5731  return tokenbuf;
5732 }
5733 
5734 static char *
5735 parser_tokspace(struct parser_params *parser, int n)
5736 {
5737  tokidx += n;
5738 
5739  if (tokidx >= toksiz) {
5740  do {toksiz *= 2;} while (toksiz < tokidx);
5741  REALLOC_N(tokenbuf, char, toksiz);
5742  }
5743  return &tokenbuf[tokidx-n];
5744 }
5745 
5746 static void
5747 parser_tokadd(struct parser_params *parser, int c)
5748 {
5749  tokenbuf[tokidx++] = (char)c;
5750  if (tokidx >= toksiz) {
5751  toksiz *= 2;
5752  REALLOC_N(tokenbuf, char, toksiz);
5753  }
5754 }
5755 
5756 static int
5757 parser_tok_hex(struct parser_params *parser, size_t *numlen)
5758 {
5759  int c;
5760 
5761  c = scan_hex(lex_p, 2, numlen);
5762  if (!*numlen) {
5763  yyerror("invalid hex escape");
5764  return 0;
5765  }
5766  lex_p += *numlen;
5767  return c;
5768 }
5769 
5770 #define tokcopy(n) memcpy(tokspace(n), lex_p - (n), (n))
5771 
5772 static int
5773 parser_tokadd_codepoint(struct parser_params *parser, rb_encoding **encp,
5774  int regexp_literal, int wide)
5775 {
5776  size_t numlen;
5777  int codepoint = scan_hex(lex_p, wide ? 6 : 4, &numlen);
5778  if (wide ? (numlen == 0) : (numlen < 4)) {
5779  yyerror("invalid Unicode escape");
5780  return FALSE;
5781  }
5782  if (codepoint > 0x10ffff) {
5783  yyerror("invalid Unicode codepoint (too large)");
5784  return FALSE;
5785  }
5786  if ((codepoint & 0xfffff800) == 0xd800) {
5787  yyerror("invalid Unicode codepoint");
5788  return FALSE;
5789  }
5790  lex_p += numlen;
5791  if (regexp_literal) {
5792  tokcopy((int)numlen);
5793  }
5794  else if (codepoint >= 0x80) {
5795  *encp = rb_utf8_encoding();
5796  tokaddmbc(codepoint, *encp);
5797  }
5798  else {
5799  tokadd(codepoint);
5800  }
5801  return TRUE;
5802 }
5803 
5804 /* return value is for ?\u3042 */
5805 static int
5806 parser_tokadd_utf8(struct parser_params *parser, rb_encoding **encp,
5807  int string_literal, int symbol_literal, int regexp_literal)
5808 {
5809  /*
5810  * If string_literal is true, then we allow multiple codepoints
5811  * in \u{}, and add the codepoints to the current token.
5812  * Otherwise we're parsing a character literal and return a single
5813  * codepoint without adding it
5814  */
5815 
5816  const int open_brace = '{', close_brace = '}';
5817 
5818  if (regexp_literal) { tokadd('\\'); tokadd('u'); }
5819 
5820  if (peek(open_brace)) { /* handle \u{...} form */
5821  int c, last = nextc();
5822  do c = nextc(); while (ISSPACE(c));
5823  pushback(c);
5824  while (!string_literal || c != close_brace) {
5825  if (regexp_literal) tokadd(last);
5826  if (!parser_tokadd_codepoint(parser, encp, regexp_literal, TRUE)) {
5827  return 0;
5828  }
5829  while (ISSPACE(c = nextc())) last = c;
5830  pushback(c);
5831  if (!string_literal) break;
5832  }
5833 
5834  if (c != close_brace) {
5835  yyerror("unterminated Unicode escape");
5836  return 0;
5837  }
5838 
5839  if (regexp_literal) tokadd(close_brace);
5840  nextc();
5841  }
5842  else { /* handle \uxxxx form */
5843  if (!parser_tokadd_codepoint(parser, encp, regexp_literal, FALSE)) {
5844  return 0;
5845  }
5846  }
5847 
5848  return TRUE;
5849 }
5850 
5851 #define ESCAPE_CONTROL 1
5852 #define ESCAPE_META 2
5853 
5854 static int
5855 parser_read_escape(struct parser_params *parser, int flags,
5856  rb_encoding **encp)
5857 {
5858  int c;
5859  size_t numlen;
5860 
5861  switch (c = nextc()) {
5862  case '\\': /* Backslash */
5863  return c;
5864 
5865  case 'n': /* newline */
5866  return '\n';
5867 
5868  case 't': /* horizontal tab */
5869  return '\t';
5870 
5871  case 'r': /* carriage-return */
5872  return '\r';
5873 
5874  case 'f': /* form-feed */
5875  return '\f';
5876 
5877  case 'v': /* vertical tab */
5878  return '\13';
5879 
5880  case 'a': /* alarm(bell) */
5881  return '\007';
5882 
5883  case 'e': /* escape */
5884  return 033;
5885 
5886  case '0': case '1': case '2': case '3': /* octal constant */
5887  case '4': case '5': case '6': case '7':
5888  pushback(c);
5889  c = scan_oct(lex_p, 3, &numlen);
5890  lex_p += numlen;
5891  return c;
5892 
5893  case 'x': /* hex constant */
5894  c = tok_hex(&numlen);
5895  if (numlen == 0) return 0;
5896  return c;
5897 
5898  case 'b': /* backspace */
5899  return '\010';
5900 
5901  case 's': /* space */
5902  return ' ';
5903 
5904  case 'M':
5905  if (flags & ESCAPE_META) goto eof;
5906  if ((c = nextc()) != '-') {
5907  pushback(c);
5908  goto eof;
5909  }
5910  if ((c = nextc()) == '\\') {
5911  if (peek('u')) goto eof;
5912  return read_escape(flags|ESCAPE_META, encp) | 0x80;
5913  }
5914  else if (c == -1 || !ISASCII(c)) goto eof;
5915  else {
5916  return ((c & 0xff) | 0x80);
5917  }
5918 
5919  case 'C':
5920  if ((c = nextc()) != '-') {
5921  pushback(c);
5922  goto eof;
5923  }
5924  case 'c':
5925  if (flags & ESCAPE_CONTROL) goto eof;
5926  if ((c = nextc())== '\\') {
5927  if (peek('u')) goto eof;
5928  c = read_escape(flags|ESCAPE_CONTROL, encp);
5929  }
5930  else if (c == '?')
5931  return 0177;
5932  else if (c == -1 || !ISASCII(c)) goto eof;
5933  return c & 0x9f;
5934 
5935  eof:
5936  case -1:
5937  yyerror("Invalid escape character syntax");
5938  return '\0';
5939 
5940  default:
5941  return c;
5942  }
5943 }
5944 
5945 static void
5946 parser_tokaddmbc(struct parser_params *parser, int c, rb_encoding *enc)
5947 {
5948  int len = rb_enc_codelen(c, enc);
5949  rb_enc_mbcput(c, tokspace(len), enc);
5950 }
5951 
5952 static int
5953 parser_tokadd_escape(struct parser_params *parser, rb_encoding **encp)
5954 {
5955  int c;
5956  int flags = 0;
5957  size_t numlen;
5958 
5959  first:
5960  switch (c = nextc()) {
5961  case '\n':
5962  return 0; /* just ignore */
5963 
5964  case '0': case '1': case '2': case '3': /* octal constant */
5965  case '4': case '5': case '6': case '7':
5966  {
5967  ruby_scan_oct(--lex_p, 3, &numlen);
5968  if (numlen == 0) goto eof;
5969  lex_p += numlen;
5970  tokcopy((int)numlen + 1);
5971  }
5972  return 0;
5973 
5974  case 'x': /* hex constant */
5975  {
5976  tok_hex(&numlen);
5977  if (numlen == 0) return -1;
5978  tokcopy((int)numlen + 2);
5979  }
5980  return 0;
5981 
5982  case 'M':
5983  if (flags & ESCAPE_META) goto eof;
5984  if ((c = nextc()) != '-') {
5985  pushback(c);
5986  goto eof;
5987  }
5988  tokcopy(3);
5989  flags |= ESCAPE_META;
5990  goto escaped;
5991 
5992  case 'C':
5993  if (flags & ESCAPE_CONTROL) goto eof;
5994  if ((c = nextc()) != '-') {
5995  pushback(c);
5996  goto eof;
5997  }
5998  tokcopy(3);
5999  goto escaped;
6000 
6001  case 'c':
6002  if (flags & ESCAPE_CONTROL) goto eof;
6003  tokcopy(2);
6004  flags |= ESCAPE_CONTROL;
6005  escaped:
6006  if ((c = nextc()) == '\\') {
6007  goto first;
6008  }
6009  else if (c == -1) goto eof;
6010  tokadd(c);
6011  return 0;
6012 
6013  eof:
6014  case -1:
6015  yyerror("Invalid escape character syntax");
6016  return -1;
6017 
6018  default:
6019  tokadd('\\');
6020  tokadd(c);
6021  }
6022  return 0;
6023 }
6024 
6025 static int
6026 parser_regx_options(struct parser_params *parser)
6027 {
6028  int kcode = 0;
6029  int kopt = 0;
6030  int options = 0;
6031  int c, opt, kc;
6032 
6033  newtok();
6034  while (c = nextc(), ISALPHA(c)) {
6035  if (c == 'o') {
6036  options |= RE_OPTION_ONCE;
6037  }
6038  else if (rb_char_to_option_kcode(c, &opt, &kc)) {
6039  if (kc >= 0) {
6040  if (kc != rb_ascii8bit_encindex()) kcode = c;
6041  kopt = opt;
6042  }
6043  else {
6044  options |= opt;
6045  }
6046  }
6047  else {
6048  tokadd(c);
6049  }
6050  }
6051  options |= kopt;
6052  pushback(c);
6053  if (toklen()) {
6054  tokfix();
6055  compile_error(PARSER_ARG "unknown regexp option%s - %s",
6056  toklen() > 1 ? "s" : "", tok());
6057  }
6058  return options | RE_OPTION_ENCODING(kcode);
6059 }
6060 
6061 static void
6062 dispose_string(VALUE str)
6063 {
6064  rb_str_free(str);
6065  rb_gc_force_recycle(str);
6066 }
6067 
6068 static int
6069 parser_tokadd_mbchar(struct parser_params *parser, int c)
6070 {
6071  int len = parser_precise_mbclen(parser, lex_p-1);
6072  if (len < 0) return -1;
6073  tokadd(c);
6074  lex_p += --len;
6075  if (len > 0) tokcopy(len);
6076  return c;
6077 }
6078 
6079 #define tokadd_mbchar(c) parser_tokadd_mbchar(parser, (c))
6080 
6081 static inline int
6082 simple_re_meta(int c)
6083 {
6084  switch (c) {
6085  case '$': case '*': case '+': case '.':
6086  case '?': case '^': case '|':
6087  case ')': case ']': case '}': case '>':
6088  return TRUE;
6089  default:
6090  return FALSE;
6091  }
6092 }
6093 
6094 static int
6095 parser_update_heredoc_indent(struct parser_params *parser, int c)
6096 {
6097  if (heredoc_line_indent == -1) {
6098  if (c == '\n') heredoc_line_indent = 0;
6099  }
6100  else {
6101  if (c == ' ') {
6102  heredoc_line_indent++;
6103  return TRUE;
6104  }
6105  else if (c == '\t') {
6106  int w = (heredoc_line_indent / TAB_WIDTH) + 1;
6107  heredoc_line_indent = w * TAB_WIDTH;
6108  return TRUE;
6109  }
6110  else if (c != '\n') {
6111  if (heredoc_indent > heredoc_line_indent) {
6112  heredoc_indent = heredoc_line_indent;
6113  }
6114  heredoc_line_indent = -1;
6115  }
6116  }
6117  return FALSE;
6118 }
6119 
6120 static int
6121 parser_tokadd_string(struct parser_params *parser,
6122  int func, int term, int paren, long *nest,
6123  rb_encoding **encp)
6124 {
6125  int c;
6126  int has_nonascii = 0;
6127  rb_encoding *enc = *encp;
6128  char *errbuf = 0;
6129  static const char mixed_msg[] = "%s mixed within %s source";
6130 
6131 #define mixed_error(enc1, enc2) if (!errbuf) { \
6132  size_t len = sizeof(mixed_msg) - 4; \
6133  len += strlen(rb_enc_name(enc1)); \
6134  len += strlen(rb_enc_name(enc2)); \
6135  errbuf = ALLOCA_N(char, len); \
6136  snprintf(errbuf, len, mixed_msg, \
6137  rb_enc_name(enc1), \
6138  rb_enc_name(enc2)); \
6139  yyerror(errbuf); \
6140  }
6141 #define mixed_escape(beg, enc1, enc2) do { \
6142  const char *pos = lex_p; \
6143  lex_p = (beg); \
6144  mixed_error((enc1), (enc2)); \
6145  lex_p = pos; \
6146  } while (0)
6147 
6148  while ((c = nextc()) != -1) {
6149  if (heredoc_indent > 0) {
6150  parser_update_heredoc_indent(parser, c);
6151  }
6152 
6153  if (paren && c == paren) {
6154  ++*nest;
6155  }
6156  else if (c == term) {
6157  if (!nest || !*nest) {
6158  pushback(c);
6159  break;
6160  }
6161  --*nest;
6162  }
6163  else if ((func & STR_FUNC_EXPAND) && c == '#' && lex_p < lex_pend) {
6164  int c2 = *lex_p;
6165  if (c2 == '$' || c2 == '@' || c2 == '{') {
6166  pushback(c);
6167  break;
6168  }
6169  }
6170  else if (c == '\\') {
6171  const char *beg = lex_p - 1;
6172  c = nextc();
6173  switch (c) {
6174  case '\n':
6175  if (func & STR_FUNC_QWORDS) break;
6176  if (func & STR_FUNC_EXPAND) continue;
6177  tokadd('\\');
6178  break;
6179 
6180  case '\\':
6181  if (func & STR_FUNC_ESCAPE) tokadd(c);
6182  break;
6183 
6184  case 'u':
6185  if ((func & STR_FUNC_EXPAND) == 0) {
6186  tokadd('\\');
6187  break;
6188  }
6189  parser_tokadd_utf8(parser, &enc, 1,
6190  func & STR_FUNC_SYMBOL,
6191  func & STR_FUNC_REGEXP);
6192  if (has_nonascii && enc != *encp) {
6193  mixed_escape(beg, enc, *encp);
6194  }
6195  continue;
6196 
6197  default:
6198  if (c == -1) return -1;
6199  if (!ISASCII(c)) {
6200  if ((func & STR_FUNC_EXPAND) == 0) tokadd('\\');
6201  goto non_ascii;
6202  }
6203  if (func & STR_FUNC_REGEXP) {
6204  if (c == term && !simple_re_meta(c)) {
6205  tokadd(c);
6206  continue;
6207  }
6208  pushback(c);
6209  if ((c = tokadd_escape(&enc)) < 0)
6210  return -1;
6211  if (has_nonascii && enc != *encp) {
6212  mixed_escape(beg, enc, *encp);
6213  }
6214  continue;
6215  }
6216  else if (func & STR_FUNC_EXPAND) {
6217  pushback(c);
6218  if (func & STR_FUNC_ESCAPE) tokadd('\\');
6219  c = read_escape(0, &enc);
6220  }
6221  else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6222  /* ignore backslashed spaces in %w */
6223  }
6224  else if (c != term && !(paren && c == paren)) {
6225  tokadd('\\');
6226  pushback(c);
6227  continue;
6228  }
6229  }
6230  }
6231  else if (!parser_isascii()) {
6232  non_ascii:
6233  has_nonascii = 1;
6234  if (enc != *encp) {
6235  mixed_error(enc, *encp);
6236  continue;
6237  }
6238  if (tokadd_mbchar(c) == -1) return -1;
6239  continue;
6240  }
6241  else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6242  pushback(c);
6243  break;
6244  }
6245  if (c & 0x80) {
6246  has_nonascii = 1;
6247  if (enc != *encp) {
6248  mixed_error(enc, *encp);
6249  continue;
6250  }
6251  }
6252  tokadd(c);
6253  }
6254  *encp = enc;
6255  return c;
6256 }
6257 
6258 #define NEW_STRTERM(func, term, paren) \
6259  rb_node_newnode(NODE_STRTERM, (func), (term) | ((paren) << (CHAR_BIT * 2)), 0)
6260 
6261 #ifdef RIPPER
6262 static void
6263 ripper_flush_string_content(struct parser_params *parser, rb_encoding *enc)
6264 {
6265  VALUE content = yylval.val;
6266  if (!ripper_is_node_yylval(content))
6267  content = ripper_new_yylval(0, 0, content);
6268  if (has_delayed_token()) {
6269  ptrdiff_t len = lex_p - parser->tokp;
6270  if (len > 0) {
6271  rb_enc_str_buf_cat(parser->delayed, parser->tokp, len, enc);
6272  }
6273  dispatch_delayed_token(tSTRING_CONTENT);
6274  parser->tokp = lex_p;
6275  RNODE(content)->nd_rval = yylval.val;
6276  }
6277  dispatch_scan_event(tSTRING_CONTENT);
6278  if (yylval.val != content)
6279  RNODE(content)->nd_rval = yylval.val;
6280  yylval.val = content;
6281 }
6282 
6283 #define flush_string_content(enc) ripper_flush_string_content(parser, (enc))
6284 #else
6285 #define flush_string_content(enc) ((void)(enc))
6286 #endif
6287 
6288 RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
6289 /* this can be shared with ripper, since it's independent from struct
6290  * parser_params. */
6291 #ifndef RIPPER
6292 #define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
6293 #define SPECIAL_PUNCT(idx) ( \
6294  BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
6295  BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
6296  BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
6297  BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
6298  BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
6299  BIT('0', idx))
6300 const unsigned int ruby_global_name_punct_bits[] = {
6301  SPECIAL_PUNCT(0),
6302  SPECIAL_PUNCT(1),
6303  SPECIAL_PUNCT(2),
6304 };
6305 #undef BIT
6306 #undef SPECIAL_PUNCT
6307 #endif
6308 
6309 static int
6310 parser_peek_variable_name(struct parser_params *parser)
6311 {
6312  int c;
6313  const char *p = lex_p;
6314 
6315  if (p + 1 >= lex_pend) return 0;
6316  c = *p++;
6317  switch (c) {
6318  case '$':
6319  if ((c = *p) == '-') {
6320  if (++p >= lex_pend) return 0;
6321  c = *p;
6322  }
6323  else if (is_global_name_punct(c) || ISDIGIT(c)) {
6324  return tSTRING_DVAR;
6325  }
6326  break;
6327  case '@':
6328  if ((c = *p) == '@') {
6329  if (++p >= lex_pend) return 0;
6330  c = *p;
6331  }
6332  break;
6333  case '{':
6334  lex_p = p;
6335  command_start = TRUE;
6336  return tSTRING_DBEG;
6337  default:
6338  return 0;
6339  }
6340  if (!ISASCII(c) || c == '_' || ISALPHA(c))
6341  return tSTRING_DVAR;
6342  return 0;
6343 }
6344 
6345 static inline int
6346 parser_string_term(struct parser_params *parser, int func)
6347 {
6348  if (!(func & STR_FUNC_REGEXP)) return tSTRING_END;
6349  set_yylval_num(regx_options());
6350  dispatch_scan_event(tREGEXP_END);
6351  return tREGEXP_END;
6352 }
6353 
6354 static int
6355 parser_parse_string(struct parser_params *parser, NODE *quote)
6356 {
6357  int func = (int)quote->nd_func;
6358  int term = nd_term(quote);
6359  int paren = nd_paren(quote);
6360  int c, space = 0;
6361  rb_encoding *enc = current_enc;
6362 
6363  if (term == STR_TERM_END) return tSTRING_END;
6364  c = nextc();
6365  if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6366  do {c = nextc();} while (ISSPACE(c));
6367  space = 1;
6368  }
6369  if (c == term && !quote->nd_nest) {
6370  if (func & STR_FUNC_QWORDS) {
6371  quote->u2.id = STR_TERM_END;
6372  return ' ';
6373  }
6374  return parser_string_term(parser, func);
6375  }
6376  if (space) {
6377  pushback(c);
6378  return ' ';
6379  }
6380  newtok();
6381  if ((func & STR_FUNC_EXPAND) && c == '#') {
6382  int t = parser_peek_variable_name(parser);
6383  if (t) return t;
6384  tokadd('#');
6385  c = nextc();
6386  }
6387  pushback(c);
6388  if (tokadd_string(func, term, paren, &quote->nd_nest,
6389  &enc) == -1) {
6390  ruby_sourceline = nd_line(quote);
6391  if (func & STR_FUNC_REGEXP) {
6392  if (parser->eofp)
6393  compile_error(PARSER_ARG "unterminated regexp meets end of file");
6394  return tREGEXP_END;
6395  }
6396  else {
6397  if (parser->eofp)
6398  compile_error(PARSER_ARG "unterminated string meets end of file");
6399  return tSTRING_END;
6400  }
6401  }
6402 
6403  tokfix();
6404  set_yylval_str(STR_NEW3(tok(), toklen(), enc, func));
6405  flush_string_content(enc);
6406 
6407  return tSTRING_CONTENT;
6408 }
6409 
6410 static int
6411 parser_heredoc_identifier(struct parser_params *parser)
6412 {
6413  int c = nextc(), term, func = 0;
6414  int token = tSTRING_BEG;
6415  long len;
6416  int newline = 0;
6417  int indent = 0;
6418 
6419  if (c == '-') {
6420  c = nextc();
6421  func = STR_FUNC_INDENT;
6422  }
6423  else if (c == '~') {
6424  c = nextc();
6425  func = STR_FUNC_INDENT;
6426  indent = INT_MAX;
6427  }
6428  switch (c) {
6429  case '\'':
6430  func |= str_squote; goto quoted;
6431  case '"':
6432  func |= str_dquote; goto quoted;
6433  case '`':
6434  token = tXSTRING_BEG;
6435  func |= str_xquote; goto quoted;
6436 
6437  quoted:
6438  newtok();
6439  tokadd(func);
6440  term = c;
6441  while ((c = nextc()) != -1 && c != term) {
6442  if (tokadd_mbchar(c) == -1) return 0;
6443  if (!newline && c == '\n') newline = 1;
6444  else if (newline) newline = 2;
6445  }
6446  if (c == -1) {
6447  compile_error(PARSER_ARG "unterminated here document identifier");
6448  return 0;
6449  }
6450  switch (newline) {
6451  case 1:
6452  rb_warn0("here document identifier ends with a newline");
6453  if (--tokidx > 0 && tokenbuf[tokidx] == '\r') --tokidx;
6454  break;
6455  case 2:
6456  compile_error(PARSER_ARG "here document identifier across newlines, never match");
6457  return -1;
6458  }
6459  break;
6460 
6461  default:
6462  if (!parser_is_identchar()) {
6463  pushback(c);
6464  if (func & STR_FUNC_INDENT) {
6465  pushback(indent > 0 ? '~' : '-');
6466  }
6467  return 0;
6468  }
6469  newtok();
6470  tokadd(func |= str_dquote);
6471  do {
6472  if (tokadd_mbchar(c) == -1) return 0;
6473  } while ((c = nextc()) != -1 && parser_is_identchar());
6474  pushback(c);
6475  break;
6476  }
6477 
6478  tokfix();
6479  dispatch_scan_event(tHEREDOC_BEG);
6480  len = lex_p - lex_pbeg;
6481  lex_goto_eol(parser);
6482  lex_strterm = rb_node_newnode(NODE_HEREDOC,
6483  STR_NEW(tok(), toklen()), /* nd_lit */
6484  len, /* nd_nth */
6485  lex_lastline); /* nd_orig */
6486  nd_set_line(lex_strterm, ruby_sourceline);
6487  ripper_flush(parser);
6488  heredoc_indent = indent;
6489  heredoc_line_indent = 0;
6490  return token;
6491 }
6492 
6493 static void
6494 parser_heredoc_restore(struct parser_params *parser, NODE *here)
6495 {
6496  VALUE line;
6497 
6498  lex_strterm = 0;
6499  line = here->nd_orig;
6500  lex_lastline = line;
6501  lex_pbeg = RSTRING_PTR(line);
6502  lex_pend = lex_pbeg + RSTRING_LEN(line);
6503  lex_p = lex_pbeg + here->nd_nth;
6504  heredoc_end = ruby_sourceline;
6505  ruby_sourceline = nd_line(here);
6506  dispose_string(here->nd_lit);
6507  rb_gc_force_recycle((VALUE)here);
6508  ripper_flush(parser);
6509 }
6510 
6511 static int
6512 dedent_string(VALUE string, int width)
6513 {
6514  char *str;
6515  long len;
6516  int i, col = 0;
6517 
6518  RSTRING_GETMEM(string, str, len);
6519  for (i = 0; i < len && col < width; i++) {
6520  if (str[i] == ' ') {
6521  col++;
6522  }
6523  else if (str[i] == '\t') {
6524  int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
6525  if (n > width) break;
6526  col = n;
6527  }
6528  else {
6529  break;
6530  }
6531  }
6532  if (!i) return 0;
6533  rb_str_modify(string);
6534  str = RSTRING_PTR(string);
6535  if (RSTRING_LEN(string) != len)
6536  rb_fatal("literal string changed: %+"PRIsVALUE, string);
6537  MEMMOVE(str, str + i, char, len - i);
6538  rb_str_set_len(string, len - i);
6539  return i;
6540 }
6541 
6542 #ifndef RIPPER
6543 static NODE *
6544 parser_heredoc_dedent(struct parser_params *parser, NODE *root)
6545 {
6546  NODE *node, *str_node;
6547  int bol = TRUE;
6548  int indent = heredoc_indent;
6549 
6550  if (indent <= 0) return root;
6551  heredoc_indent = 0;
6552  if (!root) return root;
6553 
6554  node = str_node = root;
6555  if (nd_type(root) == NODE_ARRAY) str_node = root->nd_head;
6556 
6557  while (str_node) {
6558  VALUE lit = str_node->nd_lit;
6559  if (bol) dedent_string(lit, indent);
6560  bol = TRUE;
6561 
6562  str_node = 0;
6563  while ((node = node->nd_next) != 0 && nd_type(node) == NODE_ARRAY) {
6564  if ((str_node = node->nd_head) != 0) {
6565  enum node_type type = nd_type(str_node);
6566  if (type == NODE_STR || type == NODE_DSTR) break;
6567  bol = FALSE;
6568  str_node = 0;
6569  }
6570  }
6571  }
6572  return root;
6573 }
6574 #else /* RIPPER */
6575 static VALUE
6576 parser_heredoc_dedent(struct parser_params *parser, VALUE array)
6577 {
6578  int indent = heredoc_indent;
6579 
6580  if (indent <= 0) return array;
6581  heredoc_indent = 0;
6582  dispatch2(heredoc_dedent, array, INT2NUM(indent));
6583  return array;
6584 }
6585 
6586 static VALUE
6587 parser_dedent_string(VALUE self, VALUE input, VALUE width)
6588 {
6589  int wid, col;
6590 
6591  StringValue(input);
6592  wid = NUM2UINT(width);
6593  col = dedent_string(input, wid);
6594  return INT2NUM(col);
6595 }
6596 #endif
6597 
6598 static int
6599 parser_whole_match_p(struct parser_params *parser,
6600  const char *eos, long len, int indent)
6601 {
6602  const char *p = lex_pbeg;
6603  long n;
6604 
6605  if (indent) {
6606  while (*p && ISSPACE(*p)) p++;
6607  }
6608  n = lex_pend - (p + len);
6609  if (n < 0) return FALSE;
6610  if (n > 0 && p[len] != '\n') {
6611  if (p[len] != '\r') return FALSE;
6612  if (n <= 1 || p[len+1] != '\n') return FALSE;
6613  }
6614  return strncmp(eos, p, len) == 0;
6615 }
6616 
6617 #define NUM_SUFFIX_R (1<<0)
6618 #define NUM_SUFFIX_I (1<<1)
6619 #define NUM_SUFFIX_ALL 3
6620 
6621 static int
6622 parser_number_literal_suffix(struct parser_params *parser, int mask)
6623 {
6624  int c, result = 0;
6625  const char *lastp = lex_p;
6626 
6627  while ((c = nextc()) != -1) {
6628  if ((mask & NUM_SUFFIX_I) && c == 'i') {
6629  result |= (mask & NUM_SUFFIX_I);
6630  mask &= ~NUM_SUFFIX_I;
6631  /* r after i, rational of complex is disallowed */
6632  mask &= ~NUM_SUFFIX_R;
6633  continue;
6634  }
6635  if ((mask & NUM_SUFFIX_R) && c == 'r') {
6636  result |= (mask & NUM_SUFFIX_R);
6637  mask &= ~NUM_SUFFIX_R;
6638  continue;
6639  }
6640  if (!ISASCII(c) || ISALPHA(c) || c == '_') {
6641  lex_p = lastp;
6642  return 0;
6643  }
6644  pushback(c);
6645  if (c == '.') {
6646  c = peekc_n(1);
6647  if (ISDIGIT(c)) {
6648  yyerror("unexpected fraction part after numeric literal");
6649  lex_p += 2;
6650  while (parser_is_identchar()) nextc();
6651  }
6652  }
6653  break;
6654  }
6655  return result;
6656 }
6657 
6658 static int
6659 parser_set_number_literal(struct parser_params *parser, VALUE v, int type, int suffix)
6660 {
6661  if (suffix & NUM_SUFFIX_I) {
6662  v = rb_complex_raw(INT2FIX(0), v);
6663  type = tIMAGINARY;
6664  }
6665  set_yylval_literal(v);
6666  SET_LEX_STATE(EXPR_END|EXPR_ENDARG);
6667  return type;
6668 }
6669 
6670 static int
6671 parser_set_integer_literal(struct parser_params *parser, VALUE v, int suffix)
6672 {
6673  int type = tINTEGER;
6674  if (suffix & NUM_SUFFIX_R) {
6675  v = rb_rational_raw1(v);
6676  type = tRATIONAL;
6677  }
6678  return set_number_literal(v, type, suffix);
6679 }
6680 
6681 #ifdef RIPPER
6682 static void
6683 ripper_dispatch_heredoc_end(struct parser_params *parser)
6684 {
6685  VALUE str;
6686  if (has_delayed_token())
6687  dispatch_delayed_token(tSTRING_CONTENT);
6688  str = STR_NEW(parser->tokp, lex_pend - parser->tokp);
6689  ripper_dispatch1(parser, ripper_token2eventid(tHEREDOC_END), str);
6690  lex_goto_eol(parser);
6691  ripper_flush(parser);
6692 }
6693 
6694 #define dispatch_heredoc_end() ripper_dispatch_heredoc_end(parser)
6695 #else
6696 #define dispatch_heredoc_end() ((void)0)
6697 #endif
6698 
6699 static int
6700 parser_here_document(struct parser_params *parser, NODE *here)
6701 {
6702  int c, func, indent = 0;
6703  const char *eos, *p, *pend;
6704  long len;
6705  VALUE str = 0;
6706  rb_encoding *enc = current_enc;
6707 
6708  eos = RSTRING_PTR(here->nd_lit);
6709  len = RSTRING_LEN(here->nd_lit) - 1;
6710  indent = (func = *eos++) & STR_FUNC_INDENT;
6711 
6712  if ((c = nextc()) == -1) {
6713  error:
6714  compile_error(PARSER_ARG "can't find string \"%s\" anywhere before EOF", eos);
6715 #ifdef RIPPER
6716  if (!has_delayed_token()) {
6717  dispatch_scan_event(tSTRING_CONTENT);
6718  }
6719  else {
6720  if (str) {
6721  rb_str_append(parser->delayed, str);
6722  }
6723  else if ((len = lex_p - parser->tokp) > 0) {
6724  if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6725  int cr = ENC_CODERANGE_UNKNOWN;
6726  rb_str_coderange_scan_restartable(parser->tokp, lex_p, enc, &cr);
6727  if (cr != ENC_CODERANGE_7BIT &&
6728  current_enc == rb_usascii_encoding() &&
6729  enc != rb_utf8_encoding()) {
6730  enc = rb_ascii8bit_encoding();
6731  }
6732  }
6733  rb_enc_str_buf_cat(parser->delayed, parser->tokp, len, enc);
6734  }
6735  dispatch_delayed_token(tSTRING_CONTENT);
6736  }
6737  lex_goto_eol(parser);
6738 #endif
6739  restore:
6740  heredoc_restore(lex_strterm);
6741  return 0;
6742  }
6743  if (was_bol() && whole_match_p(eos, len, indent)) {
6744  dispatch_heredoc_end();
6745  heredoc_restore(lex_strterm);
6746  return tSTRING_END;
6747  }
6748 
6749  if (!(func & STR_FUNC_EXPAND)) {
6750  do {
6751  p = RSTRING_PTR(lex_lastline);
6752  pend = lex_pend;
6753  if (pend > p) {
6754  switch (pend[-1]) {
6755  case '\n':
6756  if (--pend == p || pend[-1] != '\r') {
6757  pend++;
6758  break;
6759  }
6760  case '\r':
6761  --pend;
6762  }
6763  }
6764 
6765  if (heredoc_indent > 0) {
6766  long i = 0;
6767  while (p + i < pend && parser_update_heredoc_indent(parser, p[i]))
6768  i++;
6769  heredoc_line_indent = 0;
6770  }
6771 
6772  if (str)
6773  rb_str_cat(str, p, pend - p);
6774  else
6775  str = STR_NEW(p, pend - p);
6776  if (pend < lex_pend) rb_str_cat(str, "\n", 1);
6777  lex_goto_eol(parser);
6778  if (heredoc_indent > 0) {
6779  set_yylval_str(str);
6780  flush_string_content(enc);
6781  return tSTRING_CONTENT;
6782  }
6783  if (nextc() == -1) {
6784  if (str) {
6785  dispose_string(str);
6786  str = 0;
6787  }
6788  goto error;
6789  }
6790  } while (!whole_match_p(eos, len, indent));
6791  }
6792  else {
6793  /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
6794  newtok();
6795  if (c == '#') {
6796  int t = parser_peek_variable_name(parser);
6797  if (t) return t;
6798  tokadd('#');
6799  c = nextc();
6800  }
6801  do {
6802  pushback(c);
6803  if ((c = tokadd_string(func, '\n', 0, NULL, &enc)) == -1) {
6804  if (parser->eofp) goto error;
6805  goto restore;
6806  }
6807  if (c != '\n') {
6808  flush:
6809  set_yylval_str(STR_NEW3(tok(), toklen(), enc, func));
6810  flush_string_content(enc);
6811  return tSTRING_CONTENT;
6812  }
6813  tokadd(nextc());
6814  if (heredoc_indent > 0) {
6815  lex_goto_eol(parser);
6816  goto flush;
6817  }
6818  /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
6819  if ((c = nextc()) == -1) goto error;
6820  } while (!whole_match_p(eos, len, indent));
6821  str = STR_NEW3(tok(), toklen(), enc, func);
6822  }
6823  dispatch_heredoc_end();
6824 #ifdef RIPPER
6825  str = ripper_new_yylval(ripper_token2eventid(tSTRING_CONTENT),
6826  yylval.val, str);
6827 #endif
6828  heredoc_restore(lex_strterm);
6829  lex_strterm = NEW_STRTERM(func, STR_TERM_END, 0);
6830  set_yylval_str(str);
6831  return tSTRING_CONTENT;
6832 }
6833 
6834 #include "lex.c"
6835 
6836 static void
6837 arg_ambiguous_gen(struct parser_params *parser, char c)
6838 {
6839 #ifndef RIPPER
6840  rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
6841 #else
6842  dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
6843 #endif
6844 }
6845 #define arg_ambiguous(c) (arg_ambiguous_gen(parser, (c)), 1)
6846 
6847 static ID
6848 formal_argument_gen(struct parser_params *parser, ID lhs)
6849 {
6850  switch (id_type(lhs)) {
6851  case ID_LOCAL:
6852  break;
6853 #ifndef RIPPER
6854  case ID_CONST:
6855  yyerror("formal argument cannot be a constant");
6856  return 0;
6857  case ID_INSTANCE:
6858  yyerror("formal argument cannot be an instance variable");
6859  return 0;
6860  case ID_GLOBAL:
6861  yyerror("formal argument cannot be a global variable");
6862  return 0;
6863  case ID_CLASS:
6864  yyerror("formal argument cannot be a class variable");
6865  return 0;
6866  default:
6867  yyerror("formal argument must be local variable");
6868  return 0;
6869 #else
6870  default:
6871  lhs = dispatch1(param_error, lhs);
6872  ripper_error();
6873  return 0;
6874 #endif
6875  }
6876  shadowing_lvar(lhs);
6877  return lhs;
6878 }
6879 
6880 static int
6881 lvar_defined_gen(struct parser_params *parser, ID id)
6882 {
6883  return (dyna_in_block() && dvar_defined_get(id)) || local_id(id);
6884 }
6885 
6886 /* emacsen -*- hack */
6887 static long
6888 parser_encode_length(struct parser_params *parser, const char *name, long len)
6889 {
6890  long nlen;
6891 
6892  if (len > 5 && name[nlen = len - 5] == '-') {
6893  if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
6894  return nlen;
6895  }
6896  if (len > 4 && name[nlen = len - 4] == '-') {
6897  if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
6898  return nlen;
6899  if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
6900  !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
6901  /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
6902  return nlen;
6903  }
6904  return len;
6905 }
6906 
6907 static void
6908 parser_set_encode(struct parser_params *parser, const char *name)
6909 {
6910  int idx = rb_enc_find_index(name);
6911  rb_encoding *enc;
6912  VALUE excargs[3];
6913 
6914  if (idx < 0) {
6915  excargs[1] = rb_sprintf("unknown encoding name: %s", name);
6916  error:
6917  excargs[0] = rb_eArgError;
6918  excargs[2] = rb_make_backtrace();
6919  rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", ruby_sourcefile_string, ruby_sourceline));
6920  rb_exc_raise(rb_make_exception(3, excargs));
6921  }
6922  enc = rb_enc_from_index(idx);
6923  if (!rb_enc_asciicompat(enc)) {
6924  excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
6925  goto error;
6926  }
6927  parser->enc = enc;
6928 #ifndef RIPPER
6929  if (ruby_debug_lines) {
6930  VALUE lines = ruby_debug_lines;
6931  long i, n = RARRAY_LEN(lines);
6932  for (i = 0; i < n; ++i) {
6933  rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
6934  }
6935  }
6936 #endif
6937 }
6938 
6939 static int
6940 comment_at_top(struct parser_params *parser)
6941 {
6942  const char *p = lex_pbeg, *pend = lex_p - 1;
6943  if (parser->line_count != (parser->has_shebang ? 2 : 1)) return 0;
6944  while (p < pend) {
6945  if (!ISSPACE(*p)) return 0;
6946  p++;
6947  }
6948  return 1;
6949 }
6950 
6951 typedef long (*rb_magic_comment_length_t)(struct parser_params *parser, const char *name, long len);
6952 typedef void (*rb_magic_comment_setter_t)(struct parser_params *parser, const char *name, const char *val);
6953 
6954 static void
6955 magic_comment_encoding(struct parser_params *parser, const char *name, const char *val)
6956 {
6957  if (!comment_at_top(parser)) {
6958  return;
6959  }
6960  parser_set_encode(parser, val);
6961 }
6962 
6963 static int
6964 parser_get_bool(struct parser_params *parser, const char *name, const char *val)
6965 {
6966  switch (*val) {
6967  case 't': case 'T':
6968  if (strcasecmp(val, "true") == 0) {
6969  return TRUE;
6970  }
6971  break;
6972  case 'f': case 'F':
6973  if (strcasecmp(val, "false") == 0) {
6974  return FALSE;
6975  }
6976  break;
6977  }
6978  rb_compile_warning(ruby_sourcefile, ruby_sourceline, "invalid value for %s: %s", name, val);
6979  return -1;
6980 }
6981 
6982 static void
6983 parser_set_token_info(struct parser_params *parser, const char *name, const char *val)
6984 {
6985  int b = parser_get_bool(parser, name, val);
6986  if (b >= 0) parser->token_info_enabled = b;
6987 }
6988 
6989 static void
6990 parser_set_compile_option_flag(struct parser_params *parser, const char *name, const char *val)
6991 {
6992  int b;
6993 
6994  if (parser->token_seen) {
6995  rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
6996  return;
6997  }
6998 
6999  b = parser_get_bool(parser, name, val);
7000  if (b < 0) return;
7001 
7002  if (!parser->compile_option)
7003  parser->compile_option = rb_obj_hide(rb_ident_hash_new());
7004  rb_hash_aset(parser->compile_option, ID2SYM(rb_intern(name)),
7005  (b ? Qtrue : Qfalse));
7006 }
7007 
7008 # if WARN_PAST_SCOPE
7009 static void
7010 parser_set_past_scope(struct parser_params *parser, const char *name, const char *val)
7011 {
7012  int b = parser_get_bool(parser, name, val);
7013  if (b >= 0) parser->past_scope_enabled = b;
7014 }
7015 # endif
7016 
7017 struct magic_comment {
7018  const char *name;
7019  rb_magic_comment_setter_t func;
7020  rb_magic_comment_length_t length;
7021 };
7022 
7023 static const struct magic_comment magic_comments[] = {
7024  {"coding", magic_comment_encoding, parser_encode_length},
7025  {"encoding", magic_comment_encoding, parser_encode_length},
7026  {"frozen_string_literal", parser_set_compile_option_flag},
7027  {"warn_indent", parser_set_token_info},
7028 # if WARN_PAST_SCOPE
7029  {"warn_past_scope", parser_set_past_scope},
7030 # endif
7031 };
7032 
7033 static const char *
7034 magic_comment_marker(const char *str, long len)
7035 {
7036  long i = 2;
7037 
7038  while (i < len) {
7039  switch (str[i]) {
7040  case '-':
7041  if (str[i-1] == '*' && str[i-2] == '-') {
7042  return str + i + 1;
7043  }
7044  i += 2;
7045  break;
7046  case '*':
7047  if (i + 1 >= len) return 0;
7048  if (str[i+1] != '-') {
7049  i += 4;
7050  }
7051  else if (str[i-1] != '-') {
7052  i += 2;
7053  }
7054  else {
7055  return str + i + 2;
7056  }
7057  break;
7058  default:
7059  i += 3;
7060  break;
7061  }
7062  }
7063  return 0;
7064 }
7065 
7066 static int
7067 parser_magic_comment(struct parser_params *parser, const char *str, long len)
7068 {
7069  int indicator = 0;
7070  VALUE name = 0, val = 0;
7071  const char *beg, *end, *vbeg, *vend;
7072 #define str_copy(_s, _p, _n) ((_s) \
7073  ? (void)(rb_str_resize((_s), (_n)), \
7074  MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
7075  : (void)((_s) = STR_NEW((_p), (_n))))
7076 
7077  if (len <= 7) return FALSE;
7078  if (!!(beg = magic_comment_marker(str, len))) {
7079  if (!(end = magic_comment_marker(beg, str + len - beg)))
7080  return FALSE;
7081  indicator = TRUE;
7082  str = beg;
7083  len = end - beg - 3;
7084  }
7085 
7086  /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
7087  while (len > 0) {
7088  const struct magic_comment *p = magic_comments;
7089  char *s;
7090  int i;
7091  long n = 0;
7092 
7093  for (; len > 0 && *str; str++, --len) {
7094  switch (*str) {
7095  case '\'': case '"': case ':': case ';':
7096  continue;
7097  }
7098  if (!ISSPACE(*str)) break;
7099  }
7100  for (beg = str; len > 0; str++, --len) {
7101  switch (*str) {
7102  case '\'': case '"': case ':': case ';':
7103  break;
7104  default:
7105  if (ISSPACE(*str)) break;
7106  continue;
7107  }
7108  break;
7109  }
7110  for (end = str; len > 0 && ISSPACE(*str); str++, --len);
7111  if (!len) break;
7112  if (*str != ':') {
7113  if (!indicator) return FALSE;
7114  continue;
7115  }
7116 
7117  do str++; while (--len > 0 && ISSPACE(*str));
7118  if (!len) break;
7119  if (*str == '"') {
7120  for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
7121  if (*str == '\\') {
7122  --len;
7123  ++str;
7124  }
7125  }
7126  vend = str;
7127  if (len) {
7128  --len;
7129  ++str;
7130  }
7131  }
7132  else {
7133  for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
7134  vend = str;
7135  }
7136  if (indicator) {
7137  while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
7138  }
7139  else {
7140  while (len > 0 && (ISSPACE(*str))) --len, str++;
7141  if (len) return FALSE;
7142  }
7143 
7144  n = end - beg;
7145  str_copy(name, beg, n);
7146  s = RSTRING_PTR(name);
7147  for (i = 0; i < n; ++i) {
7148  if (s[i] == '-') s[i] = '_';
7149  }
7150  do {
7151  if (STRNCASECMP(p->name, s, n) == 0 && !p->name[n]) {
7152  n = vend - vbeg;
7153  if (p->length) {
7154  n = (*p->length)(parser, vbeg, n);
7155  }
7156  str_copy(val, vbeg, n);
7157  (*p->func)(parser, p->name, RSTRING_PTR(val));
7158  break;
7159  }
7160  } while (++p < magic_comments + numberof(magic_comments));
7161 #ifdef RIPPER
7162  str_copy(val, vbeg, vend - vbeg);
7163  dispatch2(magic_comment, name, val);
7164 #endif
7165  }
7166 
7167  return TRUE;
7168 }
7169 
7170 static void
7171 set_file_encoding(struct parser_params *parser, const char *str, const char *send)
7172 {
7173  int sep = 0;
7174  const char *beg = str;
7175  VALUE s;
7176 
7177  for (;;) {
7178  if (send - str <= 6) return;
7179  switch (str[6]) {
7180  case 'C': case 'c': str += 6; continue;
7181  case 'O': case 'o': str += 5; continue;
7182  case 'D': case 'd': str += 4; continue;
7183  case 'I': case 'i': str += 3; continue;
7184  case 'N': case 'n': str += 2; continue;
7185  case 'G': case 'g': str += 1; continue;
7186  case '=': case ':':
7187  sep = 1;
7188  str += 6;
7189  break;
7190  default:
7191  str += 6;
7192  if (ISSPACE(*str)) break;
7193  continue;
7194  }
7195  if (STRNCASECMP(str-6, "coding", 6) == 0) break;
7196  }
7197  for (;;) {
7198  do {
7199  if (++str >= send) return;
7200  } while (ISSPACE(*str));
7201  if (sep) break;
7202  if (*str != '=' && *str != ':') return;
7203  sep = 1;
7204  str++;
7205  }
7206  beg = str;
7207  while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
7208  s = rb_str_new(beg, parser_encode_length(parser, beg, str - beg));
7209  parser_set_encode(parser, RSTRING_PTR(s));
7210  rb_str_resize(s, 0);
7211 }
7212 
7213 static void
7214 parser_prepare(struct parser_params *parser)
7215 {
7216  int c = nextc();
7217  switch (c) {
7218  case '#':
7219  if (peek('!')) parser->has_shebang = 1;
7220  break;
7221  case 0xef: /* UTF-8 BOM marker */
7222  if (lex_pend - lex_p >= 2 &&
7223  (unsigned char)lex_p[0] == 0xbb &&
7224  (unsigned char)lex_p[1] == 0xbf) {
7225  parser->enc = rb_utf8_encoding();
7226  lex_p += 2;
7227  lex_pbeg = lex_p;
7228  return;
7229  }
7230  break;
7231  case EOF:
7232  return;
7233  }
7234  pushback(c);
7235  parser->enc = rb_enc_get(lex_lastline);
7236  parser->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
7237 }
7238 
7239 #define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7240 #define IS_END() IS_lex_state(EXPR_END_ANY)
7241 #define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7242 #define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7243 #define IS_LABEL_POSSIBLE() (\
7244  (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7245  IS_ARG())
7246 #define IS_LABEL_SUFFIX(n) (peek_n(':',(n)) && !peek_n(':', (n)+1))
7247 #define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7248 
7249 #ifndef RIPPER
7250 #define ambiguous_operator(op, syn) ( \
7251  rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
7252  rb_warning0("even though it seems like "syn""))
7253 #else
7254 #define ambiguous_operator(op, syn) dispatch2(operator_ambiguous, ripper_intern(op), rb_str_new_cstr(syn))
7255 #endif
7256 #define warn_balanced(op, syn) ((void) \
7257  (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
7258  space_seen && !ISSPACE(c) && \
7259  (ambiguous_operator(op, syn), 0)))
7260 
7261 static VALUE
7262 parse_rational(struct parser_params *parser, char *str, int len, int seen_point)
7263 {
7264  VALUE v;
7265  char *point = &str[seen_point];
7266  size_t fraclen = len-seen_point-1;
7267  memmove(point, point+1, fraclen+1);
7268  v = rb_cstr_to_inum(str, 10, FALSE);
7269  return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
7270 }
7271 
7272 static int
7273 parse_numeric(struct parser_params *parser, int c)
7274 {
7275  int is_float, seen_point, seen_e, nondigit;
7276  int suffix;
7277 
7278  is_float = seen_point = seen_e = nondigit = 0;
7279  SET_LEX_STATE(EXPR_END);
7280  newtok();
7281  if (c == '-' || c == '+') {
7282  tokadd(c);
7283  c = nextc();
7284  }
7285  if (c == '0') {
7286 #define no_digits() do {yyerror("numeric literal without digits"); return 0;} while (0)
7287  int start = toklen();
7288  c = nextc();
7289  if (c == 'x' || c == 'X') {
7290  /* hexadecimal */
7291  c = nextc();
7292  if (c != -1 && ISXDIGIT(c)) {
7293  do {
7294  if (c == '_') {
7295  if (nondigit) break;
7296  nondigit = c;
7297  continue;
7298  }
7299  if (!ISXDIGIT(c)) break;
7300  nondigit = 0;
7301  tokadd(c);
7302  } while ((c = nextc()) != -1);
7303  }
7304  pushback(c);
7305  tokfix();
7306  if (toklen() == start) {
7307  no_digits();
7308  }
7309  else if (nondigit) goto trailing_uc;
7310  suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7311  return set_integer_literal(rb_cstr_to_inum(tok(), 16, FALSE), suffix);
7312  }
7313  if (c == 'b' || c == 'B') {
7314  /* binary */
7315  c = nextc();
7316  if (c == '0' || c == '1') {
7317  do {
7318  if (c == '_') {
7319  if (nondigit) break;
7320  nondigit = c;
7321  continue;
7322  }
7323  if (c != '0' && c != '1') break;
7324  nondigit = 0;
7325  tokadd(c);
7326  } while ((c = nextc()) != -1);
7327  }
7328  pushback(c);
7329  tokfix();
7330  if (toklen() == start) {
7331  no_digits();
7332  }
7333  else if (nondigit) goto trailing_uc;
7334  suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7335  return set_integer_literal(rb_cstr_to_inum(tok(), 2, FALSE), suffix);
7336  }
7337  if (c == 'd' || c == 'D') {
7338  /* decimal */
7339  c = nextc();
7340  if (c != -1 && ISDIGIT(c)) {
7341  do {
7342  if (c == '_') {
7343  if (nondigit) break;
7344  nondigit = c;
7345  continue;
7346  }
7347  if (!ISDIGIT(c)) break;
7348  nondigit = 0;
7349  tokadd(c);
7350  } while ((c = nextc()) != -1);
7351  }
7352  pushback(c);
7353  tokfix();
7354  if (toklen() == start) {
7355  no_digits();
7356  }
7357  else if (nondigit) goto trailing_uc;
7358  suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7359  return set_integer_literal(rb_cstr_to_inum(tok(), 10, FALSE), suffix);
7360  }
7361  if (c == '_') {
7362  /* 0_0 */
7363  goto octal_number;
7364  }
7365  if (c == 'o' || c == 'O') {
7366  /* prefixed octal */
7367  c = nextc();
7368  if (c == -1 || c == '_' || !ISDIGIT(c)) {
7369  no_digits();
7370  }
7371  }
7372  if (c >= '0' && c <= '7') {
7373  /* octal */
7374  octal_number:
7375  do {
7376  if (c == '_') {
7377  if (nondigit) break;
7378  nondigit = c;
7379  continue;
7380  }
7381  if (c < '0' || c > '9') break;
7382  if (c > '7') goto invalid_octal;
7383  nondigit = 0;
7384  tokadd(c);
7385  } while ((c = nextc()) != -1);
7386  if (toklen() > start) {
7387  pushback(c);
7388  tokfix();
7389  if (nondigit) goto trailing_uc;
7390  suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7391  return set_integer_literal(rb_cstr_to_inum(tok(), 8, FALSE), suffix);
7392  }
7393  if (nondigit) {
7394  pushback(c);
7395  goto trailing_uc;
7396  }
7397  }
7398  if (c > '7' && c <= '9') {
7399  invalid_octal:
7400  yyerror("Invalid octal digit");
7401  }
7402  else if (c == '.' || c == 'e' || c == 'E') {
7403  tokadd('0');
7404  }
7405  else {
7406  pushback(c);
7407  suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7408  return set_integer_literal(INT2FIX(0), suffix);
7409  }
7410  }
7411 
7412  for (;;) {
7413  switch (c) {
7414  case '0': case '1': case '2': case '3': case '4':
7415  case '5': case '6': case '7': case '8': case '9':
7416  nondigit = 0;
7417  tokadd(c);
7418  break;
7419 
7420  case '.':
7421  if (nondigit) goto trailing_uc;
7422  if (seen_point || seen_e) {
7423  goto decode_num;
7424  }
7425  else {
7426  int c0 = nextc();
7427  if (c0 == -1 || !ISDIGIT(c0)) {
7428  pushback(c0);
7429  goto decode_num;
7430  }
7431  c = c0;
7432  }
7433  seen_point = toklen();
7434  tokadd('.');
7435  tokadd(c);
7436  is_float++;
7437  nondigit = 0;
7438  break;
7439 
7440  case 'e':
7441  case 'E':
7442  if (nondigit) {
7443  pushback(c);
7444  c = nondigit;
7445  goto decode_num;
7446  }
7447  if (seen_e) {
7448  goto decode_num;
7449  }
7450  nondigit = c;
7451  c = nextc();
7452  if (c != '-' && c != '+' && !ISDIGIT(c)) {
7453  pushback(c);
7454  nondigit = 0;
7455  goto decode_num;
7456  }
7457  tokadd(nondigit);
7458  seen_e++;
7459  is_float++;
7460  tokadd(c);
7461  nondigit = (c == '-' || c == '+') ? c : 0;
7462  break;
7463 
7464  case '_': /* `_' in number just ignored */
7465  if (nondigit) goto decode_num;
7466  nondigit = c;
7467  break;
7468 
7469  default:
7470  goto decode_num;
7471  }
7472  c = nextc();
7473  }
7474 
7475  decode_num:
7476  pushback(c);
7477  if (nondigit) {
7478  char tmp[30];
7479  trailing_uc:
7480  snprintf(tmp, sizeof(tmp), "trailing `%c' in number", nondigit);
7481  yyerror(tmp);
7482  }
7483  tokfix();
7484  if (is_float) {
7485  int type = tFLOAT;
7486  VALUE v;
7487 
7488  suffix = number_literal_suffix(seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
7489  if (suffix & NUM_SUFFIX_R) {
7490  type = tRATIONAL;
7491  v = parse_rational(parser, tok(), toklen(), seen_point);
7492  }
7493  else {
7494  double d = strtod(tok(), 0);
7495  if (errno == ERANGE) {
7496  rb_warning1("Float %s out of range", WARN_S(tok()));
7497  errno = 0;
7498  }
7499  v = DBL2NUM(d);
7500  }
7501  return set_number_literal(v, type, suffix);
7502  }
7503  suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7504  return set_integer_literal(rb_cstr_to_inum(tok(), 10, FALSE), suffix);
7505 }
7506 
7507 static int
7508 parse_qmark(struct parser_params *parser, int space_seen)
7509 {
7510  rb_encoding *enc;
7511  register int c;
7512 
7513  if (IS_END()) {
7514  SET_LEX_STATE(EXPR_VALUE);
7515  return '?';
7516  }
7517  c = nextc();
7518  if (c == -1) {
7519  compile_error(PARSER_ARG "incomplete character syntax");
7520  return 0;
7521  }
7522  if (rb_enc_isspace(c, current_enc)) {
7523  if (!IS_ARG()) {
7524  int c2 = 0;
7525  switch (c) {
7526  case ' ':
7527  c2 = 's';
7528  break;
7529  case '\n':
7530  c2 = 'n';
7531  break;
7532  case '\t':
7533  c2 = 't';
7534  break;
7535  case '\v':
7536  c2 = 'v';
7537  break;
7538  case '\r':
7539  c2 = 'r';
7540  break;
7541  case '\f':
7542  c2 = 'f';
7543  break;
7544  }
7545  if (c2) {
7546  rb_warn1("invalid character syntax; use ?\\%c", WARN_I(c2));
7547  }
7548  }
7549  ternary:
7550  pushback(c);
7551  SET_LEX_STATE(EXPR_VALUE);
7552  return '?';
7553  }
7554  newtok();
7555  enc = current_enc;
7556  if (!parser_isascii()) {
7557  if (tokadd_mbchar(c) == -1) return 0;
7558  }
7559  else if ((rb_enc_isalnum(c, current_enc) || c == '_') &&
7560  lex_p < lex_pend && is_identchar(lex_p, lex_pend, current_enc)) {
7561  if (space_seen) {
7562  const char *start = lex_p - 1, *p = start;
7563  do {
7564  int n = parser_precise_mbclen(parser, p);
7565  if (n < 0) return -1;
7566  p += n;
7567  } while (p < lex_pend && is_identchar(p, lex_pend, current_enc));
7568  rb_warn2("`?' just followed by `%.*s' is interpreted as" \
7569  " a conditional operator, put a space after `?'",
7570  WARN_I((int)(p - start)), WARN_S_L(start, (p - start)));
7571  }
7572  goto ternary;
7573  }
7574  else if (c == '\\') {
7575  if (peek('u')) {
7576  nextc();
7577  if (!parser_tokadd_utf8(parser, &enc, 0, 0, 0))
7578  return 0;
7579  }
7580  else if (!lex_eol_p() && !(c = *lex_p, ISASCII(c))) {
7581  nextc();
7582  if (tokadd_mbchar(c) == -1) return 0;
7583  }
7584  else {
7585  c = read_escape(0, &enc);
7586  tokadd(c);
7587  }
7588  }
7589  else {
7590  tokadd(c);
7591  }
7592  tokfix();
7593  set_yylval_str(STR_NEW3(tok(), toklen(), enc, 0));
7594  SET_LEX_STATE(EXPR_END);
7595  return tCHAR;
7596 }
7597 
7598 static int
7599 parse_percent(struct parser_params *parser, const int space_seen, const enum lex_state_e last_state)
7600 {
7601  register int c;
7602 
7603  if (IS_BEG()) {
7604  int term;
7605  int paren;
7606 
7607  c = nextc();
7608  quotation:
7609  if (c == -1 || !ISALNUM(c)) {
7610  term = c;
7611  c = 'Q';
7612  }
7613  else {
7614  term = nextc();
7615  if (rb_enc_isalnum(term, current_enc) || !parser_isascii()) {
7616  yyerror("unknown type of %string");
7617  return 0;
7618  }
7619  }
7620  if (c == -1 || term == -1) {
7621  compile_error(PARSER_ARG "unterminated quoted string meets end of file");
7622  return 0;
7623  }
7624  paren = term;
7625  if (term == '(') term = ')';
7626  else if (term == '[') term = ']';
7627  else if (term == '{') term = '}';
7628  else if (term == '<') term = '>';
7629  else paren = 0;
7630 
7631  switch (c) {
7632  case 'Q':
7633  lex_strterm = NEW_STRTERM(str_dquote, term, paren);
7634  return tSTRING_BEG;
7635 
7636  case 'q':
7637  lex_strterm = NEW_STRTERM(str_squote, term, paren);
7638  return tSTRING_BEG;
7639 
7640  case 'W':
7641  lex_strterm = NEW_STRTERM(str_dword, term, paren);
7642  do {c = nextc();} while (ISSPACE(c));
7643  pushback(c);
7644  return tWORDS_BEG;
7645 
7646  case 'w':
7647  lex_strterm = NEW_STRTERM(str_sword, term, paren);
7648  do {c = nextc();} while (ISSPACE(c));
7649  pushback(c);
7650  return tQWORDS_BEG;
7651 
7652  case 'I':
7653  lex_strterm = NEW_STRTERM(str_dword, term, paren);
7654  do {c = nextc();} while (ISSPACE(c));
7655  pushback(c);
7656  return tSYMBOLS_BEG;
7657 
7658  case 'i':
7659  lex_strterm = NEW_STRTERM(str_sword, term, paren);
7660  do {c = nextc();} while (ISSPACE(c));
7661  pushback(c);
7662  return tQSYMBOLS_BEG;
7663 
7664  case 'x':
7665  lex_strterm = NEW_STRTERM(str_xquote, term, paren);
7666  return tXSTRING_BEG;
7667 
7668  case 'r':
7669  lex_strterm = NEW_STRTERM(str_regexp, term, paren);
7670  return tREGEXP_BEG;
7671 
7672  case 's':
7673  lex_strterm = NEW_STRTERM(str_ssym, term, paren);
7674  SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
7675  return tSYMBEG;
7676 
7677  default:
7678  yyerror("unknown type of %string");
7679  return 0;
7680  }
7681  }
7682  if ((c = nextc()) == '=') {
7683  set_yylval_id('%');
7684  SET_LEX_STATE(EXPR_BEG);
7685  return tOP_ASGN;
7686  }
7687  if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
7688  goto quotation;
7689  }
7690  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
7691  pushback(c);
7692  warn_balanced("%%", "string literal");
7693  return '%';
7694 }
7695 
7696 static int
7697 tokadd_ident(struct parser_params *parser, int c)
7698 {
7699  do {
7700  if (tokadd_mbchar(c) == -1) return -1;
7701  c = nextc();
7702  } while (parser_is_identchar());
7703  pushback(c);
7704  return 0;
7705 }
7706 
7707 static ID
7708 tokenize_ident(struct parser_params *parser, const enum lex_state_e last_state)
7709 {
7710  ID ident = TOK_INTERN();
7711 
7712  set_yylval_name(ident);
7713 
7714  return ident;
7715 }
7716 
7717 static int
7718 parse_numvar(struct parser_params *parser)
7719 {
7720  size_t len;
7721  int overflow;
7722  unsigned long n = ruby_scan_digits(tok()+1, toklen()-1, 10, &len, &overflow);
7723  const unsigned long nth_ref_max =
7724  ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
7725  /* NTH_REF is left-shifted to be ORed with back-ref flag and
7726  * turned into a Fixnum, in compile.c */
7727 
7728  if (overflow || n > nth_ref_max) {
7729  /* compile_error()? */
7730  rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok()));
7731  return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
7732  }
7733  else {
7734  return (int)n;
7735  }
7736 }
7737 
7738 static int
7739 parse_gvar(struct parser_params *parser, const enum lex_state_e last_state)
7740 {
7741  register int c;
7742 
7743  SET_LEX_STATE(EXPR_END);
7744  newtok();
7745  c = nextc();
7746  switch (c) {
7747  case '_': /* $_: last read line string */
7748  c = nextc();
7749  if (parser_is_identchar()) {
7750  tokadd('$');
7751  tokadd('_');
7752  break;
7753  }
7754  pushback(c);
7755  c = '_';
7756  /* fall through */
7757  case '~': /* $~: match-data */
7758  case '*': /* $*: argv */
7759  case '$': /* $$: pid */
7760  case '?': /* $?: last status */
7761  case '!': /* $!: error string */
7762  case '@': /* $@: error position */
7763  case '/': /* $/: input record separator */
7764  case '\\': /* $\: output record separator */
7765  case ';': /* $;: field separator */
7766  case ',': /* $,: output field separator */
7767  case '.': /* $.: last read line number */
7768  case '=': /* $=: ignorecase */
7769  case ':': /* $:: load path */
7770  case '<': /* $<: reading filename */
7771  case '>': /* $>: default output handle */
7772  case '\"': /* $": already loaded files */
7773  tokadd('$');
7774  tokadd(c);
7775  goto gvar;
7776 
7777  case '-':
7778  tokadd('$');
7779  tokadd(c);
7780  c = nextc();
7781  if (parser_is_identchar()) {
7782  if (tokadd_mbchar(c) == -1) return 0;
7783  }
7784  else {
7785  pushback(c);
7786  pushback('-');
7787  return '$';
7788  }
7789  gvar:
7790  set_yylval_name(TOK_INTERN());
7791  return tGVAR;
7792 
7793  case '&': /* $&: last match */
7794  case '`': /* $`: string before last match */
7795  case '\'': /* $': string after last match */
7796  case '+': /* $+: string matches last paren. */
7797  if (IS_lex_state_for(last_state, EXPR_FNAME)) {
7798  tokadd('$');
7799  tokadd(c);
7800  goto gvar;
7801  }
7802  set_yylval_node(NEW_BACK_REF(c));
7803  return tBACK_REF;
7804 
7805  case '1': case '2': case '3':
7806  case '4': case '5': case '6':
7807  case '7': case '8': case '9':
7808  tokadd('$');
7809  do {
7810  tokadd(c);
7811  c = nextc();
7812  } while (c != -1 && ISDIGIT(c));
7813  pushback(c);
7814  if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
7815  tokfix();
7816  set_yylval_node(NEW_NTH_REF(parse_numvar(parser)));
7817  return tNTH_REF;
7818 
7819  default:
7820  if (!parser_is_identchar()) {
7821  if (c == -1 || ISSPACE(c)) {
7822  compile_error(PARSER_ARG "`$' without identifiers is not allowed as a global variable name");
7823  }
7824  else {
7825  pushback(c);
7826  compile_error(PARSER_ARG "`$%c' is not allowed as a global variable name", c);
7827  }
7828  return 0;
7829  }
7830  case '0':
7831  tokadd('$');
7832  }
7833 
7834  if (tokadd_ident(parser, c)) return 0;
7835  SET_LEX_STATE(EXPR_END);
7836  tokenize_ident(parser, last_state);
7837  return tGVAR;
7838 }
7839 
7840 static int
7841 parse_atmark(struct parser_params *parser, const enum lex_state_e last_state)
7842 {
7843  int result = tIVAR;
7844  register int c = nextc();
7845 
7846  newtok();
7847  tokadd('@');
7848  if (c == '@') {
7849  result = tCVAR;
7850  tokadd('@');
7851  c = nextc();
7852  }
7853  if (c == -1 || ISSPACE(c)) {
7854  if (result == tIVAR) {
7855  compile_error(PARSER_ARG "`@' without identifiers is not allowed as an instance variable name");
7856  }
7857  else {
7858  compile_error(PARSER_ARG "`@@' without identifiers is not allowed as a class variable name");
7859  }
7860  return 0;
7861  }
7862  else if (ISDIGIT(c) || !parser_is_identchar()) {
7863  pushback(c);
7864  if (result == tIVAR) {
7865  compile_error(PARSER_ARG "`@%c' is not allowed as an instance variable name", c);
7866  }
7867  else {
7868  compile_error(PARSER_ARG "`@@%c' is not allowed as a class variable name", c);
7869  }
7870  return 0;
7871  }
7872 
7873  if (tokadd_ident(parser, c)) return 0;
7874  SET_LEX_STATE(EXPR_END);
7875  tokenize_ident(parser, last_state);
7876  return result;
7877 }
7878 
7879 static int
7880 parse_ident(struct parser_params *parser, int c, int cmd_state)
7881 {
7882  int result = 0;
7883  int mb = ENC_CODERANGE_7BIT;
7884  const enum lex_state_e last_state = lex_state;
7885  ID ident;
7886 
7887  do {
7888  if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
7889  if (tokadd_mbchar(c) == -1) return 0;
7890  c = nextc();
7891  } while (parser_is_identchar());
7892  if ((c == '!' || c == '?') && !peek('=')) {
7893  tokadd(c);
7894  }
7895  else {
7896  pushback(c);
7897  }
7898  tokfix();
7899 
7900  if (toklast() == '!' || toklast() == '?') {
7901  result = tFID;
7902  }
7903  else {
7904  if (IS_lex_state(EXPR_FNAME)) {
7905  register int c = nextc();
7906  if (c == '=' && !peek('~') && !peek('>') &&
7907  (!peek('=') || (peek_n('>', 1)))) {
7908  result = tIDENTIFIER;
7909  tokadd(c);
7910  tokfix();
7911  }
7912  else {
7913  pushback(c);
7914  }
7915  }
7916  if (result == 0 && ISUPPER(tok()[0])) {
7917  result = tCONSTANT;
7918  }
7919  else {
7920  result = tIDENTIFIER;
7921  }
7922  }
7923 
7924  if (IS_LABEL_POSSIBLE()) {
7925  if (IS_LABEL_SUFFIX(0)) {
7926  SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
7927  nextc();
7928  set_yylval_name(TOK_INTERN());
7929  return tLABEL;
7930  }
7931  }
7932  if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
7933  const struct kwtable *kw;
7934 
7935  /* See if it is a reserved word. */
7936  kw = rb_reserved_word(tok(), toklen());
7937  if (kw) {
7938  enum lex_state_e state = lex_state;
7939  SET_LEX_STATE(kw->state);
7940  if (IS_lex_state_for(state, EXPR_FNAME)) {
7941  set_yylval_name(rb_intern2(tok(), toklen()));
7942  return kw->id[0];
7943  }
7944  if (IS_lex_state(EXPR_BEG)) {
7945  command_start = TRUE;
7946  }
7947  if (kw->id[0] == keyword_do) {
7948  if (lambda_beginning_p()) {
7949  lpar_beg = 0;
7950  --paren_nest;
7951  return keyword_do_LAMBDA;
7952  }
7953  if (COND_P()) return keyword_do_cond;
7954  if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
7955  return keyword_do_block;
7956  if (IS_lex_state_for(state, (EXPR_BEG | EXPR_ENDARG)))
7957  return keyword_do_block;
7958  return keyword_do;
7959  }
7960  if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
7961  return kw->id[0];
7962  else {
7963  if (kw->id[0] != kw->id[1])
7964  SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
7965  return kw->id[1];
7966  }
7967  }
7968  }
7969 
7970  if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
7971  if (cmd_state) {
7972  SET_LEX_STATE(EXPR_CMDARG);
7973  }
7974  else {
7975  SET_LEX_STATE(EXPR_ARG);
7976  }
7977  }
7978  else if (lex_state == EXPR_FNAME) {
7979  SET_LEX_STATE(EXPR_ENDFN);
7980  }
7981  else {
7982  SET_LEX_STATE(EXPR_END);
7983  }
7984 
7985  ident = tokenize_ident(parser, last_state);
7986  if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
7987  (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
7988  lvar_defined(ident)) {
7989  SET_LEX_STATE(EXPR_END|EXPR_LABEL);
7990  }
7991  return result;
7992 }
7993 
7994 static int
7995 parser_yylex(struct parser_params *parser)
7996 {
7997  register int c;
7998  int space_seen = 0;
7999  int cmd_state;
8000  int label;
8001  enum lex_state_e last_state;
8002  int fallthru = FALSE;
8003  int token_seen = parser->token_seen;
8004 
8005  if (lex_strterm) {
8006  int token;
8007  if (nd_type(lex_strterm) == NODE_HEREDOC) {
8008  token = here_document(lex_strterm);
8009  if (token == tSTRING_END) {
8010  lex_strterm = 0;
8011  SET_LEX_STATE(EXPR_END);
8012  }
8013  }
8014  else {
8015  token = parse_string(lex_strterm);
8016  if ((token == tSTRING_END) && (lex_strterm->nd_func & STR_FUNC_LABEL)) {
8017  if (((IS_lex_state(EXPR_BEG | EXPR_ENDFN) && !COND_P()) || IS_ARG()) &&
8018  IS_LABEL_SUFFIX(0)) {
8019  nextc();
8020  token = tLABEL_END;
8021  }
8022  }
8023  if (token == tSTRING_END || token == tREGEXP_END || token == tLABEL_END) {
8024  const enum lex_state_e next_state =
8025  token == tLABEL_END ? EXPR_BEG|EXPR_LABEL : EXPR_END|EXPR_ENDARG;
8026  rb_gc_force_recycle((VALUE)lex_strterm);
8027  lex_strterm = 0;
8028  SET_LEX_STATE(next_state);
8029  }
8030  }
8031  return token;
8032  }
8033  cmd_state = command_start;
8034  command_start = FALSE;
8035  parser->token_seen = TRUE;
8036  retry:
8037  last_state = lex_state;
8038  switch (c = nextc()) {
8039  case '\0': /* NUL */
8040  case '\004': /* ^D */
8041  case '\032': /* ^Z */
8042  case -1: /* end of script. */
8043  return 0;
8044 
8045  /* white spaces */
8046  case ' ': case '\t': case '\f': case '\r':
8047  case '\13': /* '\v' */
8048  space_seen = 1;
8049 #ifdef RIPPER
8050  while ((c = nextc())) {
8051  switch (c) {
8052  case ' ': case '\t': case '\f': case '\r':
8053  case '\13': /* '\v' */
8054  break;
8055  default:
8056  goto outofloop;
8057  }
8058  }
8059  outofloop:
8060  pushback(c);
8061  dispatch_scan_event(tSP);
8062 #endif
8063  goto retry;
8064 
8065  case '#': /* it's a comment */
8066  parser->token_seen = token_seen;
8067  /* no magic_comment in shebang line */
8068  if (!parser_magic_comment(parser, lex_p, lex_pend - lex_p)) {
8069  if (comment_at_top(parser)) {
8070  set_file_encoding(parser, lex_p, lex_pend);
8071  }
8072  }
8073  lex_p = lex_pend;
8074  dispatch_scan_event(tCOMMENT);
8075  fallthru = TRUE;
8076  /* fall through */
8077  case '\n':
8078  parser->token_seen = token_seen;
8079  c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
8080  !IS_lex_state(EXPR_LABELED));
8081  if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
8082  if (!fallthru) {
8083  dispatch_scan_event(tIGNORED_NL);
8084  }
8085  fallthru = FALSE;
8086  if (!c && parser->in_kwarg) {
8087  goto normal_newline;
8088  }
8089  goto retry;
8090  }
8091  while ((c = nextc())) {
8092  switch (c) {
8093  case ' ': case '\t': case '\f': case '\r':
8094  case '\13': /* '\v' */
8095  space_seen = 1;
8096  break;
8097  case '&':
8098  case '.': {
8099  dispatch_delayed_token(tIGNORED_NL);
8100  if (peek('.') == (c == '&')) {
8101  pushback(c);
8102  dispatch_scan_event(tSP);
8103  goto retry;
8104  }
8105  }
8106  default:
8107  --ruby_sourceline;
8108  lex_nextline = lex_lastline;
8109  case -1: /* EOF no decrement*/
8110  lex_goto_eol(parser);
8111 #ifdef RIPPER
8112  if (c != -1) {
8113  parser->tokp = lex_p;
8114  }
8115 #endif
8116  goto normal_newline;
8117  }
8118  }
8119  normal_newline:
8120  command_start = TRUE;
8121  SET_LEX_STATE(EXPR_BEG);
8122  return '\n';
8123 
8124  case '*':
8125  if ((c = nextc()) == '*') {
8126  if ((c = nextc()) == '=') {
8127  set_yylval_id(tPOW);
8128  SET_LEX_STATE(EXPR_BEG);
8129  return tOP_ASGN;
8130  }
8131  pushback(c);
8132  if (IS_SPCARG(c)) {
8133  rb_warning0("`**' interpreted as argument prefix");
8134  c = tDSTAR;
8135  }
8136  else if (IS_BEG()) {
8137  c = tDSTAR;
8138  }
8139  else {
8140  warn_balanced("**", "argument prefix");
8141  c = tPOW;
8142  }
8143  }
8144  else {
8145  if (c == '=') {
8146  set_yylval_id('*');
8147  SET_LEX_STATE(EXPR_BEG);
8148  return tOP_ASGN;
8149  }
8150  pushback(c);
8151  if (IS_SPCARG(c)) {
8152  rb_warning0("`*' interpreted as argument prefix");
8153  c = tSTAR;
8154  }
8155  else if (IS_BEG()) {
8156  c = tSTAR;
8157  }
8158  else {
8159  warn_balanced("*", "argument prefix");
8160  c = '*';
8161  }
8162  }
8163  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8164  return c;
8165 
8166  case '!':
8167  c = nextc();
8168  if (IS_AFTER_OPERATOR()) {
8169  SET_LEX_STATE(EXPR_ARG);
8170  if (c == '@') {
8171  return '!';
8172  }
8173  }
8174  else {
8175  SET_LEX_STATE(EXPR_BEG);
8176  }
8177  if (c == '=') {
8178  return tNEQ;
8179  }
8180  if (c == '~') {
8181  return tNMATCH;
8182  }
8183  pushback(c);
8184  return '!';
8185 
8186  case '=':
8187  if (was_bol()) {
8188  /* skip embedded rd document */
8189  if (strncmp(lex_p, "begin", 5) == 0 && ISSPACE(lex_p[5])) {
8190  int first_p = TRUE;
8191 
8192  lex_goto_eol(parser);
8193  dispatch_scan_event(tEMBDOC_BEG);
8194  for (;;) {
8195  lex_goto_eol(parser);
8196  if (!first_p) {
8197  dispatch_scan_event(tEMBDOC);
8198  }
8199  first_p = FALSE;
8200  c = nextc();
8201  if (c == -1) {
8202  compile_error(PARSER_ARG "embedded document meets end of file");
8203  return 0;
8204  }
8205  if (c != '=') continue;
8206  if (c == '=' && strncmp(lex_p, "end", 3) == 0 &&
8207  (lex_p + 3 == lex_pend || ISSPACE(lex_p[3]))) {
8208  break;
8209  }
8210  }
8211  lex_goto_eol(parser);
8212  dispatch_scan_event(tEMBDOC_END);
8213  goto retry;
8214  }
8215  }
8216 
8217  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8218  if ((c = nextc()) == '=') {
8219  if ((c = nextc()) == '=') {
8220  return tEQQ;
8221  }
8222  pushback(c);
8223  return tEQ;
8224  }
8225  if (c == '~') {
8226  return tMATCH;
8227  }
8228  else if (c == '>') {
8229  return tASSOC;
8230  }
8231  pushback(c);
8232  return '=';
8233 
8234  case '<':
8235  last_state = lex_state;
8236  c = nextc();
8237  if (c == '<' &&
8238  !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
8239  !IS_END() &&
8240  (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
8241  int token = heredoc_identifier();
8242  if (token) return token;
8243  }
8244  if (IS_AFTER_OPERATOR()) {
8245  SET_LEX_STATE(EXPR_ARG);
8246  }
8247  else {
8248  if (IS_lex_state(EXPR_CLASS))
8249  command_start = TRUE;
8250  SET_LEX_STATE(EXPR_BEG);
8251  }
8252  if (c == '=') {
8253  if ((c = nextc()) == '>') {
8254  return tCMP;
8255  }
8256  pushback(c);
8257  return tLEQ;
8258  }
8259  if (c == '<') {
8260  if ((c = nextc()) == '=') {
8261  set_yylval_id(tLSHFT);
8262  SET_LEX_STATE(EXPR_BEG);
8263  return tOP_ASGN;
8264  }
8265  pushback(c);
8266  warn_balanced("<<", "here document");
8267  return tLSHFT;
8268  }
8269  pushback(c);
8270  return '<';
8271 
8272  case '>':
8273  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8274  if ((c = nextc()) == '=') {
8275  return tGEQ;
8276  }
8277  if (c == '>') {
8278  if ((c = nextc()) == '=') {
8279  set_yylval_id(tRSHFT);
8280  SET_LEX_STATE(EXPR_BEG);
8281  return tOP_ASGN;
8282  }
8283  pushback(c);
8284  return tRSHFT;
8285  }
8286  pushback(c);
8287  return '>';
8288 
8289  case '"':
8290  label = (IS_LABEL_POSSIBLE() ? str_label : 0);
8291  lex_strterm = NEW_STRTERM(str_dquote | label, '"', 0);
8292  return tSTRING_BEG;
8293 
8294  case '`':
8295  if (IS_lex_state(EXPR_FNAME)) {
8296  SET_LEX_STATE(EXPR_ENDFN);
8297  return c;
8298  }
8299  if (IS_lex_state(EXPR_DOT)) {
8300  if (cmd_state)
8301  SET_LEX_STATE(EXPR_CMDARG);
8302  else
8303  SET_LEX_STATE(EXPR_ARG);
8304  return c;
8305  }
8306  lex_strterm = NEW_STRTERM(str_xquote, '`', 0);
8307  return tXSTRING_BEG;
8308 
8309  case '\'':
8310  label = (IS_LABEL_POSSIBLE() ? str_label : 0);
8311  lex_strterm = NEW_STRTERM(str_squote | label, '\'', 0);
8312  return tSTRING_BEG;
8313 
8314  case '?':
8315  return parse_qmark(parser, space_seen);
8316 
8317  case '&':
8318  if ((c = nextc()) == '&') {
8319  SET_LEX_STATE(EXPR_BEG);
8320  if ((c = nextc()) == '=') {
8321  set_yylval_id(tANDOP);
8322  SET_LEX_STATE(EXPR_BEG);
8323  return tOP_ASGN;
8324  }
8325  pushback(c);
8326  return tANDOP;
8327  }
8328  else if (c == '=') {
8329  set_yylval_id('&');
8330  SET_LEX_STATE(EXPR_BEG);
8331  return tOP_ASGN;
8332  }
8333  else if (c == '.') {
8334  SET_LEX_STATE(EXPR_DOT);
8335  return tANDDOT;
8336  }
8337  pushback(c);
8338  if (IS_SPCARG(c)) {
8339  rb_warning0("`&' interpreted as argument prefix");
8340  c = tAMPER;
8341  }
8342  else if (IS_BEG()) {
8343  c = tAMPER;
8344  }
8345  else {
8346  warn_balanced("&", "argument prefix");
8347  c = '&';
8348  }
8349  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8350  return c;
8351 
8352  case '|':
8353  if ((c = nextc()) == '|') {
8354  SET_LEX_STATE(EXPR_BEG);
8355  if ((c = nextc()) == '=') {
8356  set_yylval_id(tOROP);
8357  SET_LEX_STATE(EXPR_BEG);
8358  return tOP_ASGN;
8359  }
8360  pushback(c);
8361  return tOROP;
8362  }
8363  if (c == '=') {
8364  set_yylval_id('|');
8365  SET_LEX_STATE(EXPR_BEG);
8366  return tOP_ASGN;
8367  }
8368  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
8369  pushback(c);
8370  return '|';
8371 
8372  case '+':
8373  c = nextc();
8374  if (IS_AFTER_OPERATOR()) {
8375  SET_LEX_STATE(EXPR_ARG);
8376  if (c == '@') {
8377  return tUPLUS;
8378  }
8379  pushback(c);
8380  return '+';
8381  }
8382  if (c == '=') {
8383  set_yylval_id('+');
8384  SET_LEX_STATE(EXPR_BEG);
8385  return tOP_ASGN;
8386  }
8387  if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous('+'))) {
8388  SET_LEX_STATE(EXPR_BEG);
8389  pushback(c);
8390  if (c != -1 && ISDIGIT(c)) {
8391  return parse_numeric(parser, '+');
8392  }
8393  return tUPLUS;
8394  }
8395  SET_LEX_STATE(EXPR_BEG);
8396  pushback(c);
8397  warn_balanced("+", "unary operator");
8398  return '+';
8399 
8400  case '-':
8401  c = nextc();
8402  if (IS_AFTER_OPERATOR()) {
8403  SET_LEX_STATE(EXPR_ARG);
8404  if (c == '@') {
8405  return tUMINUS;
8406  }
8407  pushback(c);
8408  return '-';
8409  }
8410  if (c == '=') {
8411  set_yylval_id('-');
8412  SET_LEX_STATE(EXPR_BEG);
8413  return tOP_ASGN;
8414  }
8415  if (c == '>') {
8416  SET_LEX_STATE(EXPR_ENDFN);
8417  token_info_push("->");
8418  return tLAMBDA;
8419  }
8420  if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous('-'))) {
8421  SET_LEX_STATE(EXPR_BEG);
8422  pushback(c);
8423  if (c != -1 && ISDIGIT(c)) {
8424  return tUMINUS_NUM;
8425  }
8426  return tUMINUS;
8427  }
8428  SET_LEX_STATE(EXPR_BEG);
8429  pushback(c);
8430  warn_balanced("-", "unary operator");
8431  return '-';
8432 
8433  case '.':
8434  SET_LEX_STATE(EXPR_BEG);
8435  if ((c = nextc()) == '.') {
8436  if ((c = nextc()) == '.') {
8437  return tDOT3;
8438  }
8439  pushback(c);
8440  return tDOT2;
8441  }
8442  pushback(c);
8443  if (c != -1 && ISDIGIT(c)) {
8444  yyerror("no .<digit> floating literal anymore; put 0 before dot");
8445  }
8446  SET_LEX_STATE(EXPR_DOT);
8447  return '.';
8448 
8449  case '0': case '1': case '2': case '3': case '4':
8450  case '5': case '6': case '7': case '8': case '9':
8451  return parse_numeric(parser, c);
8452 
8453  case ')':
8454  case ']':
8455  paren_nest--;
8456  case '}':
8457  COND_LEXPOP();
8458  CMDARG_LEXPOP();
8459  if (c == ')')
8460  SET_LEX_STATE(EXPR_ENDFN);
8461  else
8462  SET_LEX_STATE(EXPR_ENDARG);
8463  if (c == '}') {
8464  if (!brace_nest--) c = tSTRING_DEND;
8465  }
8466  return c;
8467 
8468  case ':':
8469  c = nextc();
8470  if (c == ':') {
8471  if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
8472  SET_LEX_STATE(EXPR_BEG);
8473  return tCOLON3;
8474  }
8475  SET_LEX_STATE(EXPR_DOT);
8476  return tCOLON2;
8477  }
8478  if (IS_END() || ISSPACE(c) || c == '#') {
8479  pushback(c);
8480  warn_balanced(":", "symbol literal");
8481  SET_LEX_STATE(EXPR_BEG);
8482  return ':';
8483  }
8484  switch (c) {
8485  case '\'':
8486  lex_strterm = NEW_STRTERM(str_ssym, c, 0);
8487  break;
8488  case '"':
8489  lex_strterm = NEW_STRTERM(str_dsym, c, 0);
8490  break;
8491  default:
8492  pushback(c);
8493  break;
8494  }
8495  SET_LEX_STATE(EXPR_FNAME);
8496  return tSYMBEG;
8497 
8498  case '/':
8499  if (IS_BEG()) {
8500  lex_strterm = NEW_STRTERM(str_regexp, '/', 0);
8501  return tREGEXP_BEG;
8502  }
8503  if ((c = nextc()) == '=') {
8504  set_yylval_id('/');
8505  SET_LEX_STATE(EXPR_BEG);
8506  return tOP_ASGN;
8507  }
8508  pushback(c);
8509  if (IS_SPCARG(c)) {
8510  (void)arg_ambiguous('/');
8511  lex_strterm = NEW_STRTERM(str_regexp, '/', 0);
8512  return tREGEXP_BEG;
8513  }
8514  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8515  warn_balanced("/", "regexp literal");
8516  return '/';
8517 
8518  case '^':
8519  if ((c = nextc()) == '=') {
8520  set_yylval_id('^');
8521  SET_LEX_STATE(EXPR_BEG);
8522  return tOP_ASGN;
8523  }
8524  SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8525  pushback(c);
8526  return '^';
8527 
8528  case ';':
8529  SET_LEX_STATE(EXPR_BEG);
8530  command_start = TRUE;
8531  return ';';
8532 
8533  case ',':
8534  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
8535  return ',';
8536 
8537  case '~':
8538  if (IS_AFTER_OPERATOR()) {
8539  if ((c = nextc()) != '@') {
8540  pushback(c);
8541  }
8542  SET_LEX_STATE(EXPR_ARG);
8543  }
8544  else {
8545  SET_LEX_STATE(EXPR_BEG);
8546  }
8547  return '~';
8548 
8549  case '(':
8550  if (IS_BEG()) {
8551  c = tLPAREN;
8552  }
8553  else if (IS_SPCARG(-1)) {
8554  c = tLPAREN_ARG;
8555  }
8556  else if (IS_lex_state(EXPR_ENDFN) && space_seen && !lambda_beginning_p()) {
8557  rb_warning0("parentheses after method name is interpreted as "
8558  "an argument list, not a decomposed argument");
8559  }
8560  paren_nest++;
8561  COND_PUSH(0);
8562  CMDARG_PUSH(0);
8563  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
8564  return c;
8565 
8566  case '[':
8567  paren_nest++;
8568  if (IS_AFTER_OPERATOR()) {
8569  SET_LEX_STATE(EXPR_ARG);
8570  if ((c = nextc()) == ']') {
8571  if ((c = nextc()) == '=') {
8572  return tASET;
8573  }
8574  pushback(c);
8575  return tAREF;
8576  }
8577  pushback(c);
8578  lex_state |= EXPR_LABEL;
8579  return '[';
8580  }
8581  else if (IS_BEG()) {
8582  c = tLBRACK;
8583  }
8584  else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
8585  c = tLBRACK;
8586  }
8587  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
8588  COND_PUSH(0);
8589  CMDARG_PUSH(0);
8590  return c;
8591 
8592  case '{':
8593  ++brace_nest;
8594  if (lambda_beginning_p()) {
8595  SET_LEX_STATE(EXPR_BEG);
8596  lpar_beg = 0;
8597  --paren_nest;
8598  COND_PUSH(0);
8599  CMDARG_PUSH(0);
8600  return tLAMBEG;
8601  }
8602  if (IS_lex_state(EXPR_LABELED))
8603  c = tLBRACE; /* hash */
8604  else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
8605  c = '{'; /* block (primary) */
8606  else if (IS_lex_state(EXPR_ENDARG))
8607  c = tLBRACE_ARG; /* block (expr) */
8608  else
8609  c = tLBRACE; /* hash */
8610  COND_PUSH(0);
8611  CMDARG_PUSH(0);
8612  SET_LEX_STATE(EXPR_BEG);
8613  if (c != tLBRACE_ARG) lex_state |= EXPR_LABEL;
8614  if (c != tLBRACE) command_start = TRUE;
8615  return c;
8616 
8617  case '\\':
8618  c = nextc();
8619  if (c == '\n') {
8620  space_seen = 1;
8621  dispatch_scan_event(tSP);
8622  goto retry; /* skip \\n */
8623  }
8624  pushback(c);
8625  return '\\';
8626 
8627  case '%':
8628  return parse_percent(parser, space_seen, last_state);
8629 
8630  case '$':
8631  return parse_gvar(parser, last_state);
8632 
8633  case '@':
8634  return parse_atmark(parser, last_state);
8635 
8636  case '_':
8637  if (was_bol() && whole_match_p("__END__", 7, 0)) {
8638  ruby__end__seen = 1;
8639  parser->eofp = 1;
8640 #ifndef RIPPER
8641  return -1;
8642 #else
8643  lex_goto_eol(parser);
8644  dispatch_scan_event(k__END__);
8645  return 0;
8646 #endif
8647  }
8648  newtok();
8649  break;
8650 
8651  default:
8652  if (!parser_is_identchar()) {
8653  compile_error(PARSER_ARG "Invalid char `\\x%02X' in expression", c);
8654  goto retry;
8655  }
8656 
8657  newtok();
8658  break;
8659  }
8660 
8661  return parse_ident(parser, c, cmd_state);
8662 }
8663 
8664 static int
8665 yylex(YYSTYPE *lval, struct parser_params *parser)
8666 {
8667  int t;
8668 
8669  parser->lval = lval;
8670  lval->val = Qundef;
8671  t = parser_yylex(parser);
8672  if (has_delayed_token())
8673  dispatch_delayed_token(t);
8674  else if (t != 0)
8675  dispatch_scan_event(t);
8676 
8677  return t;
8678 }
8679 
8680 #ifndef RIPPER
8681 static NODE*
8682 node_newnode(struct parser_params *parser, enum node_type type, VALUE a0, VALUE a1, VALUE a2)
8683 {
8684  NODE *n = (rb_node_newnode)(type, a0, a1, a2);
8685  nd_set_line(n, ruby_sourceline);
8686  return n;
8687 }
8688 
8689 static enum node_type
8690 nodetype(NODE *node) /* for debug */
8691 {
8692  return (enum node_type)nd_type(node);
8693 }
8694 
8695 static int
8696 nodeline(NODE *node)
8697 {
8698  return nd_line(node);
8699 }
8700 
8701 static NODE*
8702 newline_node(NODE *node)
8703 {
8704  if (node) {
8705  node = remove_begin(node);
8706  node->flags |= NODE_FL_NEWLINE;
8707  }
8708  return node;
8709 }
8710 
8711 static void
8712 fixpos(NODE *node, NODE *orig)
8713 {
8714  if (!node) return;
8715  if (!orig) return;
8716  if (orig == (NODE*)1) return;
8717  nd_set_line(node, nd_line(orig));
8718 }
8719 
8720 static void
8721 parser_warning(struct parser_params *parser, NODE *node, const char *mesg)
8722 {
8723  rb_compile_warning(ruby_sourcefile, nd_line(node), "%s", mesg);
8724 }
8725 #define parser_warning(node, mesg) parser_warning(parser, (node), (mesg))
8726 
8727 static void
8728 parser_warn(struct parser_params *parser, NODE *node, const char *mesg)
8729 {
8730  rb_compile_warn(ruby_sourcefile, nd_line(node), "%s", mesg);
8731 }
8732 #define parser_warn(node, mesg) parser_warn(parser, (node), (mesg))
8733 
8734 static NODE*
8735 block_append_gen(struct parser_params *parser, NODE *head, NODE *tail)
8736 {
8737  NODE *end, *h = head, *nd;
8738 
8739  if (tail == 0) return head;
8740 
8741  if (h == 0) return tail;
8742  switch (nd_type(h)) {
8743  case NODE_LIT:
8744  case NODE_STR:
8745  case NODE_SELF:
8746  case NODE_TRUE:
8747  case NODE_FALSE:
8748  case NODE_NIL:
8749  parser_warning(h, "unused literal ignored");
8750  return tail;
8751  default:
8752  h = end = NEW_BLOCK(head);
8753  end->nd_end = end;
8754  fixpos(end, head);
8755  head = end;
8756  break;
8757  case NODE_BLOCK:
8758  end = h->nd_end;
8759  break;
8760  }
8761 
8762  nd = end->nd_head;
8763  switch (nd_type(nd)) {
8764  case NODE_RETURN:
8765  case NODE_BREAK:
8766  case NODE_NEXT:
8767  case NODE_REDO:
8768  case NODE_RETRY:
8769  if (RTEST(ruby_verbose)) {
8770  parser_warning(tail, "statement not reached");
8771  }
8772  break;
8773 
8774  default:
8775  break;
8776  }
8777 
8778  if (nd_type(tail) != NODE_BLOCK) {
8779  tail = NEW_BLOCK(tail);
8780  tail->nd_end = tail;
8781  }
8782  end->nd_next = tail;
8783  h->nd_end = tail->nd_end;
8784  return head;
8785 }
8786 
8787 /* append item to the list */
8788 static NODE*
8789 list_append_gen(struct parser_params *parser, NODE *list, NODE *item)
8790 {
8791  NODE *last;
8792 
8793  if (list == 0) return NEW_LIST(item);
8794  if (list->nd_next) {
8795  last = list->nd_next->nd_end;
8796  }
8797  else {
8798  last = list;
8799  }
8800 
8801  list->nd_alen += 1;
8802  last->nd_next = NEW_LIST(item);
8803  list->nd_next->nd_end = last->nd_next;
8804  return list;
8805 }
8806 
8807 /* concat two lists */
8808 static NODE*
8809 list_concat(NODE *head, NODE *tail)
8810 {
8811  NODE *last;
8812 
8813  if (head->nd_next) {
8814  last = head->nd_next->nd_end;
8815  }
8816  else {
8817  last = head;
8818  }
8819 
8820  head->nd_alen += tail->nd_alen;
8821  last->nd_next = tail;
8822  if (tail->nd_next) {
8823  head->nd_next->nd_end = tail->nd_next->nd_end;
8824  }
8825  else {
8826  head->nd_next->nd_end = tail;
8827  }
8828 
8829  return head;
8830 }
8831 
8832 static int
8833 literal_concat0(struct parser_params *parser, VALUE head, VALUE tail)
8834 {
8835  if (NIL_P(tail)) return 1;
8836  if (!rb_enc_compatible(head, tail)) {
8837  compile_error(PARSER_ARG "string literal encodings differ (%s / %s)",
8838  rb_enc_name(rb_enc_get(head)),
8839  rb_enc_name(rb_enc_get(tail)));
8840  rb_str_resize(head, 0);
8841  rb_str_resize(tail, 0);
8842  return 0;
8843  }
8844  rb_str_buf_append(head, tail);
8845  return 1;
8846 }
8847 
8848 /* concat two string literals */
8849 static NODE *
8850 literal_concat_gen(struct parser_params *parser, NODE *head, NODE *tail)
8851 {
8852  enum node_type htype;
8853  NODE *headlast;
8854  VALUE lit;
8855 
8856  if (!head) return tail;
8857  if (!tail) return head;
8858 
8859  htype = nd_type(head);
8860  if (htype == NODE_EVSTR) {
8861  NODE *node = NEW_DSTR(STR_NEW0());
8862  head = list_append(node, head);
8863  htype = NODE_DSTR;
8864  }
8865  if (heredoc_indent > 0) {
8866  switch (htype) {
8867  case NODE_STR:
8868  nd_set_type(head, NODE_DSTR);
8869  case NODE_DSTR:
8870  return list_append(head, tail);
8871  default:
8872  break;
8873  }
8874  }
8875  switch (nd_type(tail)) {
8876  case NODE_STR:
8877  if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
8878  nd_type(headlast) == NODE_STR) {
8879  htype = NODE_STR;
8880  lit = headlast->nd_lit;
8881  }
8882  else {
8883  lit = head->nd_lit;
8884  }
8885  if (htype == NODE_STR) {
8886  if (!literal_concat0(parser, lit, tail->nd_lit)) {
8887  error:
8888  rb_gc_force_recycle((VALUE)head);
8889  rb_gc_force_recycle((VALUE)tail);
8890  return 0;
8891  }
8892  rb_gc_force_recycle((VALUE)tail);
8893  }
8894  else {
8895  list_append(head, tail);
8896  }
8897  break;
8898 
8899  case NODE_DSTR:
8900  if (htype == NODE_STR) {
8901  if (!literal_concat0(parser, head->nd_lit, tail->nd_lit))
8902  goto error;
8903  tail->nd_lit = head->nd_lit;
8904  rb_gc_force_recycle((VALUE)head);
8905  head = tail;
8906  }
8907  else if (NIL_P(tail->nd_lit)) {
8908  append:
8909  head->nd_alen += tail->nd_alen - 1;
8910  head->nd_next->nd_end->nd_next = tail->nd_next;
8911  head->nd_next->nd_end = tail->nd_next->nd_end;
8912  rb_gc_force_recycle((VALUE)tail);
8913  }
8914  else if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
8915  nd_type(headlast) == NODE_STR) {
8916  lit = headlast->nd_lit;
8917  if (!literal_concat0(parser, lit, tail->nd_lit))
8918  goto error;
8919  tail->nd_lit = Qnil;
8920  goto append;
8921  }
8922  else {
8923  nd_set_type(tail, NODE_ARRAY);
8924  tail->nd_head = NEW_STR(tail->nd_lit);
8925  list_concat(head, tail);
8926  }
8927  break;
8928 
8929  case NODE_EVSTR:
8930  if (htype == NODE_STR) {
8931  nd_set_type(head, NODE_DSTR);
8932  head->nd_alen = 1;
8933  }
8934  list_append(head, tail);
8935  break;
8936  }
8937  return head;
8938 }
8939 
8940 static NODE *
8941 evstr2dstr_gen(struct parser_params *parser, NODE *node)
8942 {
8943  if (nd_type(node) == NODE_EVSTR) {
8944  node = list_append(NEW_DSTR(STR_NEW0()), node);
8945  }
8946  return node;
8947 }
8948 
8949 static NODE *
8950 new_evstr_gen(struct parser_params *parser, NODE *node)
8951 {
8952  NODE *head = node;
8953 
8954  if (node) {
8955  switch (nd_type(node)) {
8956  case NODE_STR: case NODE_DSTR: case NODE_EVSTR:
8957  return node;
8958  }
8959  }
8960  return NEW_EVSTR(head);
8961 }
8962 
8963 static NODE *
8964 call_bin_op_gen(struct parser_params *parser, NODE *recv, ID id, NODE *arg1)
8965 {
8966  value_expr(recv);
8967  value_expr(arg1);
8968  return NEW_CALL(recv, id, NEW_LIST(arg1));
8969 }
8970 
8971 static NODE *
8972 call_uni_op_gen(struct parser_params *parser, NODE *recv, ID id)
8973 {
8974  value_expr(recv);
8975  return NEW_CALL(recv, id, 0);
8976 }
8977 
8978 static NODE*
8979 match_op_gen(struct parser_params *parser, NODE *node1, NODE *node2)
8980 {
8981  value_expr(node1);
8982  value_expr(node2);
8983  if (node1) {
8984  switch (nd_type(node1)) {
8985  case NODE_DREGX:
8986  case NODE_DREGX_ONCE:
8987  return NEW_MATCH2(node1, node2);
8988 
8989  case NODE_LIT:
8990  if (RB_TYPE_P(node1->nd_lit, T_REGEXP)) {
8991  return NEW_MATCH2(node1, node2);
8992  }
8993  }
8994  }
8995 
8996  if (node2) {
8997  switch (nd_type(node2)) {
8998  case NODE_DREGX:
8999  case NODE_DREGX_ONCE:
9000  return NEW_MATCH3(node2, node1);
9001 
9002  case NODE_LIT:
9003  if (RB_TYPE_P(node2->nd_lit, T_REGEXP)) {
9004  return NEW_MATCH3(node2, node1);
9005  }
9006  }
9007  }
9008 
9009  return NEW_CALL(node1, tMATCH, NEW_LIST(node2));
9010 }
9011 
9012 # if WARN_PAST_SCOPE
9013 static int
9014 past_dvar_p(struct parser_params *parser, ID id)
9015 {
9016  struct vtable *past = lvtbl->past;
9017  while (past) {
9018  if (vtable_included(past, id)) return 1;
9019  past = past->prev;
9020  }
9021  return 0;
9022 }
9023 # endif
9024 
9025 static NODE*
9026 gettable_gen(struct parser_params *parser, ID id)
9027 {
9028  switch (id) {
9029  case keyword_self:
9030  return NEW_SELF();
9031  case keyword_nil:
9032  return NEW_NIL();
9033  case keyword_true:
9034  return NEW_TRUE();
9035  case keyword_false:
9036  return NEW_FALSE();
9037  case keyword__FILE__:
9038  return NEW_STR(rb_str_dup(ruby_sourcefile_string));
9039  case keyword__LINE__:
9040  return NEW_LIT(INT2FIX(tokline));
9041  case keyword__ENCODING__:
9042  return NEW_LIT(rb_enc_from_encoding(current_enc));
9043  }
9044  switch (id_type(id)) {
9045  case ID_LOCAL:
9046  if (dyna_in_block() && dvar_defined(id)) {
9047  if (id == current_arg) {
9048  rb_warn1("circular argument reference - %"PRIsWARN, rb_id2str(id));
9049  }
9050  return NEW_DVAR(id);
9051  }
9052  if (local_id(id)) {
9053  if (id == current_arg) {
9054  rb_warn1("circular argument reference - %"PRIsWARN, rb_id2str(id));
9055  }
9056  return NEW_LVAR(id);
9057  }
9058 # if WARN_PAST_SCOPE
9059  if (!in_defined && RTEST(ruby_verbose) && past_dvar_p(parser, id)) {
9060  rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
9061  }
9062 # endif
9063  /* method call without arguments */
9064  return NEW_VCALL(id);
9065  case ID_GLOBAL:
9066  return NEW_GVAR(id);
9067  case ID_INSTANCE:
9068  return NEW_IVAR(id);
9069  case ID_CONST:
9070  return NEW_CONST(id);
9071  case ID_CLASS:
9072  return NEW_CVAR(id);
9073  }
9074  compile_error(PARSER_ARG "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
9075  return 0;
9076 }
9077 
9078 static NODE *
9079 kwd_append(NODE *kwlist, NODE *kw)
9080 {
9081  if (kwlist) {
9082  NODE *kws = kwlist;
9083  while (kws->nd_next) {
9084  kws = kws->nd_next;
9085  }
9086  kws->nd_next = kw;
9087  }
9088  return kwlist;
9089 }
9090 
9091 static NODE *
9092 new_regexp_gen(struct parser_params *parser, NODE *node, int options)
9093 {
9094  NODE *list, *prev;
9095 
9096  if (!node) {
9097  return NEW_LIT(reg_compile(STR_NEW0(), options));
9098  }
9099  switch (nd_type(node)) {
9100  case NODE_STR:
9101  {
9102  VALUE src = node->nd_lit;
9103  nd_set_type(node, NODE_LIT);
9104  node->nd_lit = reg_compile(src, options);
9105  }
9106  break;
9107  default:
9108  node = NEW_NODE(NODE_DSTR, STR_NEW0(), 1, NEW_LIST(node));
9109  case NODE_DSTR:
9110  if (options & RE_OPTION_ONCE) {
9111  nd_set_type(node, NODE_DREGX_ONCE);
9112  }
9113  else {
9114  nd_set_type(node, NODE_DREGX);
9115  }
9116  node->nd_cflag = options & RE_OPTION_MASK;
9117  if (!NIL_P(node->nd_lit)) reg_fragment_check(node->nd_lit, options);
9118  for (list = (prev = node)->nd_next; list; list = list->nd_next) {
9119  if (nd_type(list->nd_head) == NODE_STR) {
9120  VALUE tail = list->nd_head->nd_lit;
9121  if (reg_fragment_check(tail, options) && prev && !NIL_P(prev->nd_lit)) {
9122  VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
9123  if (!literal_concat0(parser, lit, tail)) {
9124  node = 0;
9125  break;
9126  }
9127  rb_str_resize(tail, 0);
9128  prev->nd_next = list->nd_next;
9129  rb_gc_force_recycle((VALUE)list->nd_head);
9130  rb_gc_force_recycle((VALUE)list);
9131  list = prev;
9132  }
9133  else {
9134  prev = list;
9135  }
9136  }
9137  else {
9138  prev = 0;
9139  }
9140  }
9141  if (!node->nd_next) {
9142  VALUE src = node->nd_lit;
9143  nd_set_type(node, NODE_LIT);
9144  node->nd_lit = reg_compile(src, options);
9145  }
9146  break;
9147  }
9148  return node;
9149 }
9150 
9151 static NODE *
9152 new_xstring_gen(struct parser_params *parser, NODE *node)
9153 {
9154  if (!node) {
9155  return NEW_XSTR(STR_NEW0());
9156  }
9157  switch (nd_type(node)) {
9158  case NODE_STR:
9159  nd_set_type(node, NODE_XSTR);
9160  break;
9161  case NODE_DSTR:
9162  nd_set_type(node, NODE_DXSTR);
9163  break;
9164  default:
9165  node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node));
9166  break;
9167  }
9168  return node;
9169 }
9170 #else /* !RIPPER */
9171 static int
9172 id_is_var_gen(struct parser_params *parser, ID id)
9173 {
9174  if (is_notop_id(id)) {
9175  switch (id & ID_SCOPE_MASK) {
9176  case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
9177  return 1;
9178  case ID_LOCAL:
9179  if (dyna_in_block() && dvar_defined(id)) return 1;
9180  if (local_id(id)) return 1;
9181  /* method call without arguments */
9182  return 0;
9183  }
9184  }
9185  compile_error(PARSER_ARG "identifier %s is not valid to get", rb_id2str(id));
9186  return 0;
9187 }
9188 
9189 static VALUE
9190 new_regexp_gen(struct parser_params *parser, VALUE re, VALUE opt)
9191 {
9192  VALUE src = 0, err;
9193  int options = 0;
9194  if (ripper_is_node_yylval(re)) {
9195  src = RNODE(re)->nd_cval;
9196  re = RNODE(re)->nd_rval;
9197  }
9198  if (ripper_is_node_yylval(opt)) {
9199  options = (int)RNODE(opt)->nd_tag;
9200  opt = RNODE(opt)->nd_rval;
9201  }
9202  if (src && NIL_P(parser_reg_compile(parser, src, options, &err))) {
9203  compile_error(PARSER_ARG "%"PRIsVALUE, err);
9204  }
9205  return dispatch2(regexp_literal, re, opt);
9206 }
9207 
9208 static VALUE
9209 new_xstring_gen(struct parser_params *parser, VALUE str)
9210 {
9211  return dispatch1(xstring_literal, str);
9212 }
9213 #endif /* !RIPPER */
9214 
9215 static const char lex_state_names[][13] = {
9216  "EXPR_BEG", "EXPR_END", "EXPR_ENDARG", "EXPR_ENDFN", "EXPR_ARG",
9217  "EXPR_CMDARG", "EXPR_MID", "EXPR_FNAME", "EXPR_DOT", "EXPR_CLASS",
9218  "EXPR_LABEL", "EXPR_LABELED","EXPR_FITEM",
9219 };
9220 
9221 static VALUE
9222 append_lex_state_name(enum lex_state_e state, VALUE buf)
9223 {
9224  int i, sep = 0;
9225  unsigned int mask = 1;
9226  static const char none[] = "EXPR_NONE";
9227 
9228  for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
9229  if ((unsigned)state & mask) {
9230  if (sep) {
9231  rb_str_cat(buf, "|", 1);
9232  }
9233  sep = 1;
9234  rb_str_cat_cstr(buf, lex_state_names[i]);
9235  }
9236  }
9237  if (!sep) {
9238  rb_str_cat(buf, none, sizeof(none)-1);
9239  }
9240  return buf;
9241 }
9242 
9243 static enum lex_state_e
9244 trace_lex_state(enum lex_state_e from, enum lex_state_e to, int line)
9245 {
9246  VALUE mesg;
9247  mesg = rb_str_new_cstr("lex_state: ");
9248  append_lex_state_name(from, mesg);
9249  rb_str_cat_cstr(mesg, " -> ");
9250  append_lex_state_name(to, mesg);
9251  rb_str_catf(mesg, " at line %d\n", line);
9252  rb_io_write(rb_stdout, mesg);
9253  return to;
9254 }
9255 
9256 static void
9257 show_bitstack(stack_type stack, const char *name, int line)
9258 {
9259  VALUE mesg = rb_sprintf("%s: ", name);
9260  if (stack == 0) {
9261  rb_str_cat_cstr(mesg, "0");
9262  }
9263  else {
9264  stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
9265  for (; mask && !(stack & mask); mask >>= 1) continue;
9266  for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
9267  }
9268  rb_str_catf(mesg, " at line %d\n", line);
9269  rb_io_write(rb_stdout, mesg);
9270 }
9271 
9272 #ifdef RIPPER
9273 static VALUE
9274 assignable_gen(struct parser_params *parser, VALUE lhs)
9275 #else
9276 static NODE*
9277 assignable_gen(struct parser_params *parser, ID id, NODE *val)
9278 #endif
9279 {
9280 #ifdef RIPPER
9281  ID id = get_id(lhs);
9282 # define assignable_result(x) get_value(lhs)
9283 # define parser_yyerror(parser, x) assign_error_gen(parser, lhs)
9284 #else
9285 # define assignable_result(x) (x)
9286 #endif
9287  if (!id) return assignable_result(0);
9288  switch (id) {
9289  case keyword_self:
9290  yyerror("Can't change the value of self");
9291  goto error;
9292  case keyword_nil:
9293  yyerror("Can't assign to nil");
9294  goto error;
9295  case keyword_true:
9296  yyerror("Can't assign to true");
9297  goto error;
9298  case keyword_false:
9299  yyerror("Can't assign to false");
9300  goto error;
9301  case keyword__FILE__:
9302  yyerror("Can't assign to __FILE__");
9303  goto error;
9304  case keyword__LINE__:
9305  yyerror("Can't assign to __LINE__");
9306  goto error;
9307  case keyword__ENCODING__:
9308  yyerror("Can't assign to __ENCODING__");
9309  goto error;
9310  }
9311  switch (id_type(id)) {
9312  case ID_LOCAL:
9313  if (dyna_in_block()) {
9314  if (dvar_curr(id)) {
9315  return assignable_result(NEW_DASGN_CURR(id, val));
9316  }
9317  else if (dvar_defined(id)) {
9318  return assignable_result(NEW_DASGN(id, val));
9319  }
9320  else if (local_id(id)) {
9321  return assignable_result(NEW_LASGN(id, val));
9322  }
9323  else {
9324  dyna_var(id);
9325  return assignable_result(NEW_DASGN_CURR(id, val));
9326  }
9327  }
9328  else {
9329  if (!local_id(id)) {
9330  local_var(id);
9331  }
9332  return assignable_result(NEW_LASGN(id, val));
9333  }
9334  break;
9335  case ID_GLOBAL:
9336  return assignable_result(NEW_GASGN(id, val));
9337  case ID_INSTANCE:
9338  return assignable_result(NEW_IASGN(id, val));
9339  case ID_CONST:
9340  if (!in_def && !in_single)
9341  return assignable_result(NEW_CDECL(id, val, 0));
9342  yyerror("dynamic constant assignment");
9343  break;
9344  case ID_CLASS:
9345  return assignable_result(NEW_CVASGN(id, val));
9346  default:
9347  compile_error(PARSER_ARG "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
9348  }
9349  error:
9350  return assignable_result(0);
9351 #undef assignable_result
9352 #undef parser_yyerror
9353 }
9354 
9355 static int
9356 is_private_local_id(ID name)
9357 {
9358  VALUE s;
9359  if (name == idUScore) return 1;
9360  if (!is_local_id(name)) return 0;
9361  s = rb_id2str(name);
9362  if (!s) return 0;
9363  return RSTRING_PTR(s)[0] == '_';
9364 }
9365 
9366 #define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9367 
9368 static int
9369 shadowing_lvar_0(struct parser_params *parser, ID name)
9370 {
9371  if (is_private_local_id(name)) return 1;
9372  if (dyna_in_block()) {
9373  if (dvar_curr(name)) {
9374  yyerror("duplicated argument name");
9375  }
9376  else if (dvar_defined_get(name) || local_id(name)) {
9377  rb_warning1("shadowing outer local variable - %"PRIsWARN, rb_id2str(name));
9378  vtable_add(lvtbl->vars, name);
9379  if (lvtbl->used) {
9380  vtable_add(lvtbl->used, (ID)ruby_sourceline | LVAR_USED);
9381  }
9382  return 0;
9383  }
9384  }
9385  else {
9386  if (local_id(name)) {
9387  yyerror("duplicated argument name");
9388  }
9389  }
9390  return 1;
9391 }
9392 
9393 static ID
9394 shadowing_lvar_gen(struct parser_params *parser, ID name)
9395 {
9396  shadowing_lvar_0(parser, name);
9397  return name;
9398 }
9399 
9400 static void
9401 new_bv_gen(struct parser_params *parser, ID name)
9402 {
9403  if (!name) return;
9404  if (!is_local_id(name)) {
9405  compile_error(PARSER_ARG "invalid local variable - %"PRIsVALUE,
9406  rb_id2str(name));
9407  return;
9408  }
9409  if (!shadowing_lvar_0(parser, name)) return;
9410  dyna_var(name);
9411 }
9412 
9413 #ifndef RIPPER
9414 static NODE *
9415 aryset_gen(struct parser_params *parser, NODE *recv, NODE *idx)
9416 {
9417  return NEW_ATTRASGN(recv, tASET, idx);
9418 }
9419 
9420 static void
9421 block_dup_check_gen(struct parser_params *parser, NODE *node1, NODE *node2)
9422 {
9423  if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
9424  compile_error(PARSER_ARG "both block arg and actual block given");
9425  }
9426 }
9427 
9428 static NODE *
9429 attrset_gen(struct parser_params *parser, NODE *recv, ID atype, ID id)
9430 {
9431  if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
9432  return NEW_ATTRASGN(recv, id, 0);
9433 }
9434 
9435 static void
9436 rb_backref_error_gen(struct parser_params *parser, NODE *node)
9437 {
9438  switch (nd_type(node)) {
9439  case NODE_NTH_REF:
9440  compile_error(PARSER_ARG "Can't set variable $%ld", node->nd_nth);
9441  break;
9442  case NODE_BACK_REF:
9443  compile_error(PARSER_ARG "Can't set variable $%c", (int)node->nd_nth);
9444  break;
9445  }
9446 }
9447 
9448 static NODE *
9449 arg_concat_gen(struct parser_params *parser, NODE *node1, NODE *node2)
9450 {
9451  if (!node2) return node1;
9452  switch (nd_type(node1)) {
9453  case NODE_BLOCK_PASS:
9454  if (node1->nd_head)
9455  node1->nd_head = arg_concat(node1->nd_head, node2);
9456  else
9457  node1->nd_head = NEW_LIST(node2);
9458  return node1;
9459  case NODE_ARGSPUSH:
9460  if (nd_type(node2) != NODE_ARRAY) break;
9461  node1->nd_body = list_concat(NEW_LIST(node1->nd_body), node2);
9462  nd_set_type(node1, NODE_ARGSCAT);
9463  return node1;
9464  case NODE_ARGSCAT:
9465  if (nd_type(node2) != NODE_ARRAY ||
9466  nd_type(node1->nd_body) != NODE_ARRAY) break;
9467  node1->nd_body = list_concat(node1->nd_body, node2);
9468  return node1;
9469  }
9470  return NEW_ARGSCAT(node1, node2);
9471 }
9472 
9473 static NODE *
9474 arg_append_gen(struct parser_params *parser, NODE *node1, NODE *node2)
9475 {
9476  if (!node1) return NEW_LIST(node2);
9477  switch (nd_type(node1)) {
9478  case NODE_ARRAY:
9479  return list_append(node1, node2);
9480  case NODE_BLOCK_PASS:
9481  node1->nd_head = arg_append(node1->nd_head, node2);
9482  return node1;
9483  case NODE_ARGSPUSH:
9484  node1->nd_body = list_append(NEW_LIST(node1->nd_body), node2);
9485  nd_set_type(node1, NODE_ARGSCAT);
9486  return node1;
9487  }
9488  return NEW_ARGSPUSH(node1, node2);
9489 }
9490 
9491 static NODE *
9492 splat_array(NODE* node)
9493 {
9494  if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
9495  if (nd_type(node) == NODE_ARRAY) return node;
9496  return 0;
9497 }
9498 
9499 static NODE *
9500 node_assign_gen(struct parser_params *parser, NODE *lhs, NODE *rhs)
9501 {
9502  if (!lhs) return 0;
9503 
9504  switch (nd_type(lhs)) {
9505  case NODE_GASGN:
9506  case NODE_IASGN:
9507  case NODE_IASGN2:
9508  case NODE_LASGN:
9509  case NODE_DASGN:
9510  case NODE_DASGN_CURR:
9511  case NODE_MASGN:
9512  case NODE_CDECL:
9513  case NODE_CVASGN:
9514  lhs->nd_value = rhs;
9515  break;
9516 
9517  case NODE_ATTRASGN:
9518  case NODE_CALL:
9519  lhs->nd_args = arg_append(lhs->nd_args, rhs);
9520  break;
9521 
9522  default:
9523  /* should not happen */
9524  break;
9525  }
9526 
9527  return lhs;
9528 }
9529 
9530 static int
9531 value_expr_gen(struct parser_params *parser, NODE *node)
9532 {
9533  int cond = 0;
9534 
9535  if (!node) {
9536  rb_warning0("empty expression");
9537  }
9538  while (node) {
9539  switch (nd_type(node)) {
9540  case NODE_RETURN:
9541  case NODE_BREAK:
9542  case NODE_NEXT:
9543  case NODE_REDO:
9544  case NODE_RETRY:
9545  if (!cond) yyerror("void value expression");
9546  /* or "control never reach"? */
9547  return FALSE;
9548 
9549  case NODE_BLOCK:
9550  while (node->nd_next) {
9551  node = node->nd_next;
9552  }
9553  node = node->nd_head;
9554  break;
9555 
9556  case NODE_BEGIN:
9557  node = node->nd_body;
9558  break;
9559 
9560  case NODE_IF:
9561  if (!node->nd_body) {
9562  node = node->nd_else;
9563  break;
9564  }
9565  else if (!node->nd_else) {
9566  node = node->nd_body;
9567  break;
9568  }
9569  if (!value_expr(node->nd_body)) return FALSE;
9570  node = node->nd_else;
9571  break;
9572 
9573  case NODE_AND:
9574  case NODE_OR:
9575  cond = 1;
9576  node = node->nd_2nd;
9577  break;
9578 
9579  default:
9580  return TRUE;
9581  }
9582  }
9583 
9584  return TRUE;
9585 }
9586 
9587 static void
9588 void_expr_gen(struct parser_params *parser, NODE *node)
9589 {
9590  const char *useless = 0;
9591 
9592  if (!RTEST(ruby_verbose)) return;
9593 
9594  if (!node) return;
9595  switch (nd_type(node)) {
9596  case NODE_CALL:
9597  switch (node->nd_mid) {
9598  case '+':
9599  case '-':
9600  case '*':
9601  case '/':
9602  case '%':
9603  case tPOW:
9604  case tUPLUS:
9605  case tUMINUS:
9606  case '|':
9607  case '^':
9608  case '&':
9609  case tCMP:
9610  case '>':
9611  case tGEQ:
9612  case '<':
9613  case tLEQ:
9614  case tEQ:
9615  case tNEQ:
9616  useless = rb_id2name(node->nd_mid);
9617  break;
9618  }
9619  break;
9620 
9621  case NODE_LVAR:
9622  case NODE_DVAR:
9623  case NODE_GVAR:
9624  case NODE_IVAR:
9625  case NODE_CVAR:
9626  case NODE_NTH_REF:
9627  case NODE_BACK_REF:
9628  useless = "a variable";
9629  break;
9630  case NODE_CONST:
9631  useless = "a constant";
9632  break;
9633  case NODE_LIT:
9634  case NODE_STR:
9635  case NODE_DSTR:
9636  case NODE_DREGX:
9637  case NODE_DREGX_ONCE:
9638  useless = "a literal";
9639  break;
9640  case NODE_COLON2:
9641  case NODE_COLON3:
9642  useless = "::";
9643  break;
9644  case NODE_DOT2:
9645  useless = "..";
9646  break;
9647  case NODE_DOT3:
9648  useless = "...";
9649  break;
9650  case NODE_SELF:
9651  useless = "self";
9652  break;
9653  case NODE_NIL:
9654  useless = "nil";
9655  break;
9656  case NODE_TRUE:
9657  useless = "true";
9658  break;
9659  case NODE_FALSE:
9660  useless = "false";
9661  break;
9662  case NODE_DEFINED:
9663  useless = "defined?";
9664  break;
9665  }
9666 
9667  if (useless) {
9668  rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
9669  }
9670 }
9671 
9672 static void
9673 void_stmts_gen(struct parser_params *parser, NODE *node)
9674 {
9675  if (!RTEST(ruby_verbose)) return;
9676  if (!node) return;
9677  if (nd_type(node) != NODE_BLOCK) return;
9678 
9679  for (;;) {
9680  if (!node->nd_next) return;
9681  void_expr0(node->nd_head);
9682  node = node->nd_next;
9683  }
9684 }
9685 
9686 static NODE *
9687 remove_begin(NODE *node)
9688 {
9689  NODE **n = &node, *n1 = node;
9690  while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
9691  *n = n1 = n1->nd_body;
9692  }
9693  return node;
9694 }
9695 
9696 static NODE *
9697 remove_begin_all(NODE *node)
9698 {
9699  NODE **n = &node, *n1 = node;
9700  while (n1 && nd_type(n1) == NODE_BEGIN) {
9701  *n = n1 = n1->nd_body;
9702  }
9703  return node;
9704 }
9705 
9706 static void
9707 reduce_nodes_gen(struct parser_params *parser, NODE **body)
9708 {
9709  NODE *node = *body;
9710 
9711  if (!node) {
9712  *body = NEW_NIL();
9713  return;
9714  }
9715 #define subnodes(n1, n2) \
9716  ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
9717  (!node->n2) ? (body = &node->n1, 1) : \
9718  (reduce_nodes(&node->n1), body = &node->n2, 1))
9719 
9720  while (node) {
9721  int newline = (int)(node->flags & NODE_FL_NEWLINE);
9722  switch (nd_type(node)) {
9723  end:
9724  case NODE_NIL:
9725  *body = 0;
9726  return;
9727  case NODE_RETURN:
9728  *body = node = node->nd_stts;
9729  if (newline && node) node->flags |= NODE_FL_NEWLINE;
9730  continue;
9731  case NODE_BEGIN:
9732  *body = node = node->nd_body;
9733  if (newline && node) node->flags |= NODE_FL_NEWLINE;
9734  continue;
9735  case NODE_BLOCK:
9736  body = &node->nd_end->nd_head;
9737  break;
9738  case NODE_IF:
9739  if (subnodes(nd_body, nd_else)) break;
9740  return;
9741  case NODE_CASE:
9742  body = &node->nd_body;
9743  break;
9744  case NODE_WHEN:
9745  if (!subnodes(nd_body, nd_next)) goto end;
9746  break;
9747  case NODE_ENSURE:
9748  if (!subnodes(nd_head, nd_resq)) goto end;
9749  break;
9750  case NODE_RESCUE:
9751  if (node->nd_else) {
9752  body = &node->nd_resq;
9753  break;
9754  }
9755  if (!subnodes(nd_head, nd_resq)) goto end;
9756  break;
9757  default:
9758  return;
9759  }
9760  node = *body;
9761  if (newline && node) node->flags |= NODE_FL_NEWLINE;
9762  }
9763 
9764 #undef subnodes
9765 }
9766 
9767 static int
9768 is_static_content(NODE *node)
9769 {
9770  if (!node) return 1;
9771  switch (nd_type(node)) {
9772  case NODE_HASH:
9773  if (!(node = node->nd_head)) break;
9774  case NODE_ARRAY:
9775  do {
9776  if (!is_static_content(node->nd_head)) return 0;
9777  } while ((node = node->nd_next) != 0);
9778  case NODE_LIT:
9779  case NODE_STR:
9780  case NODE_NIL:
9781  case NODE_TRUE:
9782  case NODE_FALSE:
9783  case NODE_ZARRAY:
9784  break;
9785  default:
9786  return 0;
9787  }
9788  return 1;
9789 }
9790 
9791 static int
9792 assign_in_cond(struct parser_params *parser, NODE *node)
9793 {
9794  switch (nd_type(node)) {
9795  case NODE_MASGN:
9796  case NODE_LASGN:
9797  case NODE_DASGN:
9798  case NODE_DASGN_CURR:
9799  case NODE_GASGN:
9800  case NODE_IASGN:
9801  break;
9802 
9803  default:
9804  return 0;
9805  }
9806 
9807  if (!node->nd_value) return 1;
9808  if (is_static_content(node->nd_value)) {
9809  /* reports always */
9810  parser_warn(node->nd_value, "found = in conditional, should be ==");
9811  }
9812  return 1;
9813 }
9814 
9815 static void
9816 warn_unless_e_option(struct parser_params *parser, NODE *node, const char *str)
9817 {
9818  if (!e_option_supplied(parser)) parser_warn(node, str);
9819 }
9820 
9821 static void
9822 warning_unless_e_option(struct parser_params *parser, NODE *node, const char *str)
9823 {
9824  if (!e_option_supplied(parser)) parser_warning(node, str);
9825 }
9826 
9827 static NODE *cond0(struct parser_params*,NODE*,int);
9828 
9829 static NODE*
9830 range_op(struct parser_params *parser, NODE *node)
9831 {
9832  enum node_type type;
9833 
9834  if (node == 0) return 0;
9835 
9836  type = nd_type(node);
9837  value_expr(node);
9838  if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
9839  warn_unless_e_option(parser, node, "integer literal in conditional range");
9840  return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(rb_intern("$."))));
9841  }
9842  return cond0(parser, node, FALSE);
9843 }
9844 
9845 static int
9846 literal_node(NODE *node)
9847 {
9848  if (!node) return 1; /* same as NODE_NIL */
9849  switch (nd_type(node)) {
9850  case NODE_LIT:
9851  case NODE_STR:
9852  case NODE_DSTR:
9853  case NODE_EVSTR:
9854  case NODE_DREGX:
9855  case NODE_DREGX_ONCE:
9856  case NODE_DSYM:
9857  return 2;
9858  case NODE_TRUE:
9859  case NODE_FALSE:
9860  case NODE_NIL:
9861  return 1;
9862  }
9863  return 0;
9864 }
9865 
9866 static NODE*
9867 cond0(struct parser_params *parser, NODE *node, int method_op)
9868 {
9869  if (node == 0) return 0;
9870  assign_in_cond(parser, node);
9871 
9872  switch (nd_type(node)) {
9873  case NODE_DSTR:
9874  case NODE_EVSTR:
9875  case NODE_STR:
9876  if (!method_op) rb_warn0("string literal in condition");
9877  break;
9878 
9879  case NODE_DREGX:
9880  case NODE_DREGX_ONCE:
9881  if (!method_op)
9882  warning_unless_e_option(parser, node, "regex literal in condition");
9883  return NEW_MATCH2(node, NEW_GVAR(idLASTLINE));
9884 
9885  case NODE_AND:
9886  case NODE_OR:
9887  node->nd_1st = cond0(parser, node->nd_1st, FALSE);
9888  node->nd_2nd = cond0(parser, node->nd_2nd, FALSE);
9889  break;
9890 
9891  case NODE_DOT2:
9892  case NODE_DOT3:
9893  node->nd_beg = range_op(parser, node->nd_beg);
9894  node->nd_end = range_op(parser, node->nd_end);
9895  if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
9896  else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
9897  if (!method_op && !e_option_supplied(parser)) {
9898  int b = literal_node(node->nd_beg);
9899  int e = literal_node(node->nd_end);
9900  if ((b == 1 && e == 1) || (b + e >= 2 && RTEST(ruby_verbose))) {
9901  parser_warn(node, "range literal in condition");
9902  }
9903  }
9904  break;
9905 
9906  case NODE_DSYM:
9907  if (!method_op) parser_warning(node, "literal in condition");
9908  break;
9909 
9910  case NODE_LIT:
9911  if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
9912  if (!method_op)
9913  warn_unless_e_option(parser, node, "regex literal in condition");
9914  nd_set_type(node, NODE_MATCH);
9915  }
9916  else {
9917  if (!method_op)
9918  parser_warning(node, "literal in condition");
9919  }
9920  default:
9921  break;
9922  }
9923  return node;
9924 }
9925 
9926 static NODE*
9927 cond_gen(struct parser_params *parser, NODE *node, int method_op)
9928 {
9929  if (node == 0) return 0;
9930  return cond0(parser, node, method_op);
9931 }
9932 
9933 static NODE*
9934 new_if_gen(struct parser_params *parser, NODE *cc, NODE *left, NODE *right)
9935 {
9936  if (!cc) return right;
9937  cc = cond0(parser, cc, FALSE);
9938  return newline_node(NEW_IF(cc, left, right));
9939 }
9940 
9941 static NODE*
9942 logop_gen(struct parser_params *parser, enum node_type type, NODE *left, NODE *right)
9943 {
9944  value_expr(left);
9945  if (left && (enum node_type)nd_type(left) == type) {
9946  NODE *node = left, *second;
9947  while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
9948  node = second;
9949  }
9950  node->nd_2nd = NEW_NODE(type, second, right, 0);
9951  return left;
9952  }
9953  return NEW_NODE(type, left, right, 0);
9954 }
9955 
9956 static void
9957 no_blockarg(struct parser_params *parser, NODE *node)
9958 {
9959  if (node && nd_type(node) == NODE_BLOCK_PASS) {
9960  compile_error(PARSER_ARG "block argument should not be given");
9961  }
9962 }
9963 
9964 static NODE *
9965 ret_args_gen(struct parser_params *parser, NODE *node)
9966 {
9967  if (node) {
9968  no_blockarg(parser, node);
9969  if (nd_type(node) == NODE_ARRAY) {
9970  if (node->nd_next == 0) {
9971  node = node->nd_head;
9972  }
9973  else {
9974  nd_set_type(node, NODE_VALUES);
9975  }
9976  }
9977  }
9978  return node;
9979 }
9980 
9981 static NODE *
9982 new_yield_gen(struct parser_params *parser, NODE *node)
9983 {
9984  if (node) no_blockarg(parser, node);
9985 
9986  return NEW_YIELD(node);
9987 }
9988 
9989 static VALUE
9990 negate_lit(VALUE lit)
9991 {
9992  int type = TYPE(lit);
9993  switch (type) {
9994  case T_FIXNUM:
9995  lit = LONG2FIX(-FIX2LONG(lit));
9996  break;
9997  case T_BIGNUM:
9998  BIGNUM_NEGATE(lit);
9999  lit = rb_big_norm(lit);
10000  break;
10001  case T_RATIONAL:
10002  RRATIONAL_SET_NUM(lit, negate_lit(RRATIONAL(lit)->num));
10003  break;
10004  case T_COMPLEX:
10005  RCOMPLEX_SET_REAL(lit, negate_lit(RCOMPLEX(lit)->real));
10006  RCOMPLEX_SET_IMAG(lit, negate_lit(RCOMPLEX(lit)->imag));
10007  break;
10008  case T_FLOAT:
10009 #if USE_FLONUM
10010  if (FLONUM_P(lit)) {
10011  lit = DBL2NUM(-RFLOAT_VALUE(lit));
10012  break;
10013  }
10014 #endif
10015  RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
10016  break;
10017  default:
10018  rb_bug("unknown literal type (%d) passed to negate_lit", type);
10019  break;
10020  }
10021  return lit;
10022 }
10023 
10024 static NODE *
10025 arg_blk_pass(NODE *node1, NODE *node2)
10026 {
10027  if (node2) {
10028  node2->nd_head = node1;
10029  return node2;
10030  }
10031  return node1;
10032 }
10033 
10034 
10035 static NODE*
10036 new_args_gen(struct parser_params *parser, NODE *m, NODE *o, ID r, NODE *p, NODE *tail)
10037 {
10038  int saved_line = ruby_sourceline;
10039  struct rb_args_info *args = tail->nd_ainfo;
10040 
10041  args->pre_args_num = m ? rb_long2int(m->nd_plen) : 0;
10042  args->pre_init = m ? m->nd_next : 0;
10043 
10044  args->post_args_num = p ? rb_long2int(p->nd_plen) : 0;
10045  args->post_init = p ? p->nd_next : 0;
10046  args->first_post_arg = p ? p->nd_pid : 0;
10047 
10048  args->rest_arg = r;
10049 
10050  args->opt_args = o;
10051 
10052  ruby_sourceline = saved_line;
10053 
10054  return tail;
10055 }
10056 
10057 static NODE*
10058 new_args_tail_gen(struct parser_params *parser, NODE *k, ID kr, ID b)
10059 {
10060  int saved_line = ruby_sourceline;
10061  struct rb_args_info *args;
10062  NODE *node;
10063 
10064  args = ZALLOC(struct rb_args_info);
10065  node = NEW_NODE(NODE_ARGS, 0, 0, args);
10066 
10067  args->block_arg = b;
10068  args->kw_args = k;
10069 
10070  if (k) {
10071  /*
10072  * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
10073  * variable order: k1, kr1, k2, &b, internal_id, krest
10074  * #=> <reorder>
10075  * variable order: kr1, k1, k2, internal_id, krest, &b
10076  */
10077  ID kw_bits;
10078  NODE *kwn = k;
10079  struct vtable *required_kw_vars = vtable_alloc(NULL);
10080  struct vtable *kw_vars = vtable_alloc(NULL);
10081  int i;
10082 
10083  while (kwn) {
10084  NODE *val_node = kwn->nd_body->nd_value;
10085  ID vid = kwn->nd_body->nd_vid;
10086 
10087  if (val_node == (NODE *)-1) {
10088  vtable_add(required_kw_vars, vid);
10089  }
10090  else {
10091  vtable_add(kw_vars, vid);
10092  }
10093 
10094  kwn = kwn->nd_next;
10095  }
10096 
10097  kw_bits = internal_id();
10098  if (kr && is_junk_id(kr)) vtable_pop(lvtbl->args, 1);
10099  vtable_pop(lvtbl->args, vtable_size(required_kw_vars) + vtable_size(kw_vars) + (b != 0));
10100 
10101  for (i=0; i<vtable_size(required_kw_vars); i++) arg_var(required_kw_vars->tbl[i]);
10102  for (i=0; i<vtable_size(kw_vars); i++) arg_var(kw_vars->tbl[i]);
10103  vtable_free(required_kw_vars);
10104  vtable_free(kw_vars);
10105 
10106  arg_var(kw_bits);
10107  if (kr) arg_var(kr);
10108  if (b) arg_var(b);
10109 
10110  args->kw_rest_arg = NEW_DVAR(kw_bits);
10111  args->kw_rest_arg->nd_cflag = kr;
10112  }
10113  else if (kr) {
10114  if (b) vtable_pop(lvtbl->args, 1); /* reorder */
10115  arg_var(kr);
10116  if (b) arg_var(b);
10117  args->kw_rest_arg = NEW_DVAR(kr);
10118  }
10119 
10120  ruby_sourceline = saved_line;
10121  return node;
10122 }
10123 
10124 static NODE*
10125 dsym_node_gen(struct parser_params *parser, NODE *node)
10126 {
10127  VALUE lit;
10128 
10129  if (!node) {
10130  return NEW_LIT(ID2SYM(idNULL));
10131  }
10132 
10133  switch (nd_type(node)) {
10134  case NODE_DSTR:
10135  nd_set_type(node, NODE_DSYM);
10136  break;
10137  case NODE_STR:
10138  lit = node->nd_lit;
10139  node->nd_lit = ID2SYM(rb_intern_str(lit));
10140  nd_set_type(node, NODE_LIT);
10141  break;
10142  default:
10143  node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node));
10144  break;
10145  }
10146  return node;
10147 }
10148 
10149 static int
10150 append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
10151 {
10152  NODE *node = (NODE *)v;
10153  NODE **result = (NODE **)h;
10154  node->nd_alen = 2;
10155  node->nd_next->nd_end = node->nd_next;
10156  node->nd_next->nd_next = 0;
10157  if (*result)
10158  list_concat(*result, node);
10159  else
10160  *result = node;
10161  return ST_CONTINUE;
10162 }
10163 
10164 static NODE *
10165 remove_duplicate_keys(struct parser_params *parser, NODE *hash)
10166 {
10167  st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
10168  NODE *result = 0;
10169  while (hash && hash->nd_head && hash->nd_next) {
10170  NODE *head = hash->nd_head;
10171  NODE *value = hash->nd_next;
10172  NODE *next = value->nd_next;
10173  VALUE key = (VALUE)head;
10174  st_data_t data;
10175  if (nd_type(head) == NODE_LIT &&
10176  st_lookup(literal_keys, (key = head->nd_lit), &data)) {
10177  rb_compile_warn(ruby_sourcefile, nd_line((NODE *)data),
10178  "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
10179  head->nd_lit, nd_line(head));
10180  head = ((NODE *)data)->nd_next;
10181  head->nd_head = block_append(head->nd_head, value->nd_head);
10182  }
10183  else {
10184  st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
10185  }
10186  hash = next;
10187  }
10188  st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
10189  st_free_table(literal_keys);
10190  if (hash) {
10191  if (!result) result = hash;
10192  else list_concat(result, hash);
10193  }
10194  return result;
10195 }
10196 
10197 static NODE *
10198 new_hash_gen(struct parser_params *parser, NODE *hash)
10199 {
10200  if (hash) hash = remove_duplicate_keys(parser, hash);
10201  return NEW_HASH(hash);
10202 }
10203 #endif /* !RIPPER */
10204 
10205 #ifndef RIPPER
10206 static NODE *
10207 new_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs)
10208 {
10209  NODE *asgn;
10210 
10211  if (lhs) {
10212  ID vid = lhs->nd_vid;
10213  if (op == tOROP) {
10214  lhs->nd_value = rhs;
10215  asgn = NEW_OP_ASGN_OR(gettable(vid), lhs);
10216  if (is_notop_id(vid)) {
10217  switch (id_type(vid)) {
10218  case ID_GLOBAL:
10219  case ID_INSTANCE:
10220  case ID_CLASS:
10221  asgn->nd_aid = vid;
10222  }
10223  }
10224  }
10225  else if (op == tANDOP) {
10226  lhs->nd_value = rhs;
10227  asgn = NEW_OP_ASGN_AND(gettable(vid), lhs);
10228  }
10229  else {
10230  asgn = lhs;
10231  asgn->nd_value = NEW_CALL(gettable(vid), op, NEW_LIST(rhs));
10232  }
10233  }
10234  else {
10235  asgn = NEW_BEGIN(0);
10236  }
10237  return asgn;
10238 }
10239 
10240 static NODE *
10241 new_attr_op_assign_gen(struct parser_params *parser, NODE *lhs,
10242  ID atype, ID attr, ID op, NODE *rhs)
10243 {
10244  NODE *asgn;
10245 
10246  if (op == tOROP) {
10247  op = 0;
10248  }
10249  else if (op == tANDOP) {
10250  op = 1;
10251  }
10252  asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs);
10253  fixpos(asgn, lhs);
10254  return asgn;
10255 }
10256 
10257 static NODE *
10258 new_const_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs)
10259 {
10260  NODE *asgn;
10261 
10262  if (op == tOROP) {
10263  op = 0;
10264  }
10265  else if (op == tANDOP) {
10266  op = 1;
10267  }
10268  if (lhs) {
10269  asgn = NEW_OP_CDECL(lhs, op, rhs);
10270  }
10271  else {
10272  asgn = NEW_BEGIN(0);
10273  }
10274  fixpos(asgn, lhs);
10275  return asgn;
10276 }
10277 
10278 static NODE *
10279 const_decl_gen(struct parser_params *parser, NODE *path)
10280 {
10281  if (in_def || in_single) {
10282  yyerror("dynamic constant assignment");
10283  }
10284  return NEW_CDECL(0, 0, (path));
10285 }
10286 #else
10287 static VALUE
10288 new_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE op, VALUE rhs)
10289 {
10290  return dispatch3(opassign, lhs, op, rhs);
10291 }
10292 
10293 static VALUE
10294 new_attr_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE type, VALUE attr, VALUE op, VALUE rhs)
10295 {
10296  VALUE recv = dispatch3(field, lhs, type, attr);
10297  return dispatch3(opassign, recv, op, rhs);
10298 }
10299 
10300 static VALUE
10301 const_decl_gen(struct parser_params *parser, VALUE path)
10302 {
10303  if (in_def || in_single) {
10304  assign_error(path);
10305  }
10306  return path;
10307 }
10308 
10309 static VALUE
10310 assign_error_gen(struct parser_params *parser, VALUE a)
10311 {
10312  a = dispatch1(assign_error, a);
10313  ripper_error();
10314  return a;
10315 }
10316 #endif
10317 
10318 static void
10319 warn_unused_var(struct parser_params *parser, struct local_vars *local)
10320 {
10321  int i, cnt;
10322  ID *v, *u;
10323 
10324  if (!local->used) return;
10325  v = local->vars->tbl;
10326  u = local->used->tbl;
10327  cnt = local->used->pos;
10328  if (cnt != local->vars->pos) {
10329  rb_bug("local->used->pos != local->vars->pos");
10330  }
10331  for (i = 0; i < cnt; ++i) {
10332  if (!v[i] || (u[i] & LVAR_USED)) continue;
10333  if (is_private_local_id(v[i])) continue;
10334  rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
10335  }
10336 }
10337 
10338 static void
10339 local_push_gen(struct parser_params *parser, int inherit_dvars)
10340 {
10341  struct local_vars *local;
10342 
10343  local = ALLOC(struct local_vars);
10344  local->prev = lvtbl;
10345  local->args = vtable_alloc(0);
10346  local->vars = vtable_alloc(inherit_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
10347  local->used = !(inherit_dvars &&
10348  (ifndef_ripper(compile_for_eval || e_option_supplied(parser))+0)) &&
10349  RTEST(ruby_verbose) ? vtable_alloc(0) : 0;
10350 # if WARN_PAST_SCOPE
10351  local->past = 0;
10352 # endif
10353  local->cmdargs = cmdarg_stack;
10354  CMDARG_SET(0);
10355  lvtbl = local;
10356 }
10357 
10358 static void
10359 local_pop_gen(struct parser_params *parser)
10360 {
10361  struct local_vars *local = lvtbl->prev;
10362  if (lvtbl->used) {
10363  warn_unused_var(parser, lvtbl);
10364  vtable_free(lvtbl->used);
10365  }
10366 # if WARN_PAST_SCOPE
10367  while (lvtbl->past) {
10368  struct vtable *past = lvtbl->past;
10369  lvtbl->past = past->prev;
10370  vtable_free(past);
10371  }
10372 # endif
10373  vtable_free(lvtbl->args);
10374  vtable_free(lvtbl->vars);
10375  CMDARG_SET(lvtbl->cmdargs);
10376  xfree(lvtbl);
10377  lvtbl = local;
10378 }
10379 
10380 #ifndef RIPPER
10381 static ID*
10382 local_tbl_gen(struct parser_params *parser)
10383 {
10384  int cnt_args = vtable_size(lvtbl->args);
10385  int cnt_vars = vtable_size(lvtbl->vars);
10386  int cnt = cnt_args + cnt_vars;
10387  int i, j;
10388  ID *buf;
10389 
10390  if (cnt <= 0) return 0;
10391  buf = ALLOC_N(ID, cnt + 1);
10392  MEMCPY(buf+1, lvtbl->args->tbl, ID, cnt_args);
10393  /* remove IDs duplicated to warn shadowing */
10394  for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) {
10395  ID id = lvtbl->vars->tbl[i];
10396  if (!vtable_included(lvtbl->args, id)) {
10397  buf[j++] = id;
10398  }
10399  }
10400  if (--j < cnt) REALLOC_N(buf, ID, (cnt = j) + 1);
10401  buf[0] = cnt;
10402  return buf;
10403 }
10404 #endif
10405 
10406 static void
10407 arg_var_gen(struct parser_params *parser, ID id)
10408 {
10409  vtable_add(lvtbl->args, id);
10410 }
10411 
10412 static void
10413 local_var_gen(struct parser_params *parser, ID id)
10414 {
10415  vtable_add(lvtbl->vars, id);
10416  if (lvtbl->used) {
10417  vtable_add(lvtbl->used, (ID)ruby_sourceline);
10418  }
10419 }
10420 
10421 static int
10422 local_id_gen(struct parser_params *parser, ID id)
10423 {
10424  struct vtable *vars, *args, *used;
10425 
10426  vars = lvtbl->vars;
10427  args = lvtbl->args;
10428  used = lvtbl->used;
10429 
10430  while (vars && POINTER_P(vars->prev)) {
10431  vars = vars->prev;
10432  args = args->prev;
10433  if (used) used = used->prev;
10434  }
10435 
10436  if (vars && vars->prev == DVARS_INHERIT) {
10437  return rb_local_defined(id, parser->base_block);
10438  }
10439  else if (vtable_included(args, id)) {
10440  return 1;
10441  }
10442  else {
10443  int i = vtable_included(vars, id);
10444  if (i && used) used->tbl[i-1] |= LVAR_USED;
10445  return i != 0;
10446  }
10447 }
10448 
10449 static const struct vtable *
10450 dyna_push_gen(struct parser_params *parser)
10451 {
10452  lvtbl->args = vtable_alloc(lvtbl->args);
10453  lvtbl->vars = vtable_alloc(lvtbl->vars);
10454  if (lvtbl->used) {
10455  lvtbl->used = vtable_alloc(lvtbl->used);
10456  }
10457  return lvtbl->args;
10458 }
10459 
10460 static void
10461 dyna_pop_vtable(struct parser_params *parser, struct vtable **vtblp)
10462 {
10463  struct vtable *tmp = *vtblp;
10464  *vtblp = tmp->prev;
10465 # if WARN_PAST_SCOPE
10466  if (parser->past_scope_enabled) {
10467  tmp->prev = lvtbl->past;
10468  lvtbl->past = tmp;
10469  return;
10470  }
10471 # endif
10472  vtable_free(tmp);
10473 }
10474 
10475 static void
10476 dyna_pop_1(struct parser_params *parser)
10477 {
10478  struct vtable *tmp;
10479 
10480  if ((tmp = lvtbl->used) != 0) {
10481  warn_unused_var(parser, lvtbl);
10482  lvtbl->used = lvtbl->used->prev;
10483  vtable_free(tmp);
10484  }
10485  dyna_pop_vtable(parser, &lvtbl->args);
10486  dyna_pop_vtable(parser, &lvtbl->vars);
10487 }
10488 
10489 static void
10490 dyna_pop_gen(struct parser_params *parser, const struct vtable *lvargs)
10491 {
10492  while (lvtbl->args != lvargs) {
10493  dyna_pop_1(parser);
10494  if (!lvtbl->args) {
10495  struct local_vars *local = lvtbl->prev;
10496  xfree(lvtbl);
10497  lvtbl = local;
10498  }
10499  }
10500  dyna_pop_1(parser);
10501 }
10502 
10503 static int
10504 dyna_in_block_gen(struct parser_params *parser)
10505 {
10506  return POINTER_P(lvtbl->vars) && lvtbl->vars->prev != DVARS_TOPSCOPE;
10507 }
10508 
10509 static int
10510 dvar_defined_gen(struct parser_params *parser, ID id, int get)
10511 {
10512  struct vtable *vars, *args, *used;
10513  int i;
10514 
10515  args = lvtbl->args;
10516  vars = lvtbl->vars;
10517  used = lvtbl->used;
10518 
10519  while (POINTER_P(vars)) {
10520  if (vtable_included(args, id)) {
10521  return 1;
10522  }
10523  if ((i = vtable_included(vars, id)) != 0) {
10524  if (used) used->tbl[i-1] |= LVAR_USED;
10525  return 1;
10526  }
10527  args = args->prev;
10528  vars = vars->prev;
10529  if (get) used = 0;
10530  if (used) used = used->prev;
10531  }
10532 
10533  if (vars == DVARS_INHERIT) {
10534  return rb_dvar_defined(id, parser->base_block);
10535  }
10536 
10537  return 0;
10538 }
10539 
10540 static int
10541 dvar_curr_gen(struct parser_params *parser, ID id)
10542 {
10543  return (vtable_included(lvtbl->args, id) ||
10544  vtable_included(lvtbl->vars, id));
10545 }
10546 
10547 static void
10548 reg_fragment_enc_error(struct parser_params* parser, VALUE str, int c)
10549 {
10550  compile_error(PARSER_ARG
10551  "regexp encoding option '%c' differs from source encoding '%s'",
10552  c, rb_enc_name(rb_enc_get(str)));
10553 }
10554 
10555 #ifndef RIPPER
10556 int
10557 rb_reg_fragment_setenc(struct parser_params* parser, VALUE str, int options)
10558 {
10559  int c = RE_OPTION_ENCODING_IDX(options);
10560 
10561  if (c) {
10562  int opt, idx;
10563  rb_char_to_option_kcode(c, &opt, &idx);
10564  if (idx != ENCODING_GET(str) &&
10565  rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
10566  goto error;
10567  }
10568  ENCODING_SET(str, idx);
10569  }
10570  else if (RE_OPTION_ENCODING_NONE(options)) {
10571  if (!ENCODING_IS_ASCII8BIT(str) &&
10572  rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
10573  c = 'n';
10574  goto error;
10575  }
10576  rb_enc_associate(str, rb_ascii8bit_encoding());
10577  }
10578  else if (current_enc == rb_usascii_encoding()) {
10579  if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
10580  /* raise in re.c */
10581  rb_enc_associate(str, rb_usascii_encoding());
10582  }
10583  else {
10584  rb_enc_associate(str, rb_ascii8bit_encoding());
10585  }
10586  }
10587  return 0;
10588 
10589  error:
10590  return c;
10591 }
10592 
10593 static void
10594 reg_fragment_setenc_gen(struct parser_params* parser, VALUE str, int options)
10595 {
10596  int c = rb_reg_fragment_setenc(parser, str, options);
10597  if (c) reg_fragment_enc_error(parser, str, c);
10598 }
10599 
10600 static int
10601 reg_fragment_check_gen(struct parser_params* parser, VALUE str, int options)
10602 {
10603  VALUE err;
10604  reg_fragment_setenc(str, options);
10605  err = rb_reg_check_preprocess(str);
10606  if (err != Qnil) {
10607  err = rb_obj_as_string(err);
10608  compile_error(PARSER_ARG "%"PRIsVALUE, err);
10609  return 0;
10610  }
10611  return 1;
10612 }
10613 
10614 typedef struct {
10615  struct parser_params* parser;
10616  rb_encoding *enc;
10617  NODE *succ_block;
10618 } reg_named_capture_assign_t;
10619 
10620 static int
10621 reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
10622  int back_num, int *back_refs, OnigRegex regex, void *arg0)
10623 {
10624  reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
10625  struct parser_params* parser = arg->parser;
10626  rb_encoding *enc = arg->enc;
10627  long len = name_end - name;
10628  const char *s = (const char *)name;
10629  ID var;
10630  NODE *node, *succ;
10631 
10632  if (!len || (*name != '_' && ISASCII(*name) && !rb_enc_islower(*name, enc)) ||
10633  (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) ||
10634  !rb_enc_symname2_p(s, len, enc)) {
10635  return ST_CONTINUE;
10636  }
10637  var = intern_cstr(s, len, enc);
10638  node = node_assign(assignable(var, 0), NEW_LIT(ID2SYM(var)));
10639  succ = arg->succ_block;
10640  if (!succ) succ = NEW_BEGIN(0);
10641  succ = block_append(succ, node);
10642  arg->succ_block = succ;
10643  return ST_CONTINUE;
10644 }
10645 
10646 static NODE *
10647 reg_named_capture_assign_gen(struct parser_params* parser, VALUE regexp)
10648 {
10649  reg_named_capture_assign_t arg;
10650 
10651  arg.parser = parser;
10652  arg.enc = rb_enc_get(regexp);
10653  arg.succ_block = 0;
10654  onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
10655 
10656  if (!arg.succ_block) return 0;
10657  return arg.succ_block->nd_next;
10658 }
10659 
10660 static VALUE
10661 parser_reg_compile(struct parser_params* parser, VALUE str, int options)
10662 {
10663  reg_fragment_setenc(str, options);
10664  return rb_parser_reg_compile(parser, str, options);
10665 }
10666 
10667 VALUE
10668 rb_parser_reg_compile(struct parser_params* parser, VALUE str, int options)
10669 {
10670  return rb_reg_compile(str, options & RE_OPTION_MASK, ruby_sourcefile, ruby_sourceline);
10671 }
10672 
10673 static VALUE
10674 reg_compile_gen(struct parser_params* parser, VALUE str, int options)
10675 {
10676  VALUE re;
10677  VALUE err;
10678 
10679  err = rb_errinfo();
10680  re = parser_reg_compile(parser, str, options);
10681  if (NIL_P(re)) {
10682  VALUE m = rb_attr_get(rb_errinfo(), idMesg);
10683  rb_set_errinfo(err);
10684  compile_error(PARSER_ARG "%"PRIsVALUE, m);
10685  return Qnil;
10686  }
10687  return re;
10688 }
10689 #else
10690 static VALUE
10691 parser_reg_compile(struct parser_params* parser, VALUE str, int options, VALUE *errmsg)
10692 {
10693  VALUE err = rb_errinfo();
10694  VALUE re;
10695  int c = rb_reg_fragment_setenc(parser, str, options);
10696  if (c) reg_fragment_enc_error(parser, str, c);
10697  re = rb_parser_reg_compile(parser, str, options);
10698  if (NIL_P(re)) {
10699  *errmsg = rb_attr_get(rb_errinfo(), idMesg);
10700  rb_set_errinfo(err);
10701  }
10702  return re;
10703 }
10704 #endif
10705 
10706 #ifndef RIPPER
10707 NODE*
10708 rb_parser_append_print(VALUE vparser, NODE *node)
10709 {
10710  NODE *prelude = 0;
10711  NODE *scope = node;
10712  struct parser_params *parser;
10713 
10714  if (!node) return node;
10715 
10716  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
10717 
10718  node = node->nd_body;
10719 
10720  if (nd_type(node) == NODE_PRELUDE) {
10721  prelude = node;
10722  node = node->nd_body;
10723  }
10724 
10725  node = block_append(node,
10726  NEW_FCALL(rb_intern("print"),
10727  NEW_ARRAY(NEW_GVAR(idLASTLINE))));
10728  if (prelude) {
10729  prelude->nd_body = node;
10730  scope->nd_body = prelude;
10731  }
10732  else {
10733  scope->nd_body = node;
10734  }
10735 
10736  return scope;
10737 }
10738 
10739 NODE *
10740 rb_parser_while_loop(VALUE vparser, NODE *node, int chop, int split)
10741 {
10742  NODE *prelude = 0;
10743  NODE *scope = node;
10744  struct parser_params *parser;
10745 
10746  if (!node) return node;
10747 
10748  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
10749 
10750  node = node->nd_body;
10751 
10752  if (nd_type(node) == NODE_PRELUDE) {
10753  prelude = node;
10754  node = node->nd_body;
10755  }
10756  if (split) {
10757  node = block_append(NEW_GASGN(rb_intern("$F"),
10758  NEW_CALL(NEW_GVAR(idLASTLINE),
10759  rb_intern("split"), 0)),
10760  node);
10761  }
10762  if (chop) {
10763  node = block_append(NEW_CALL(NEW_GVAR(idLASTLINE),
10764  rb_intern("chop!"), 0), node);
10765  }
10766 
10767  node = NEW_OPT_N(node);
10768 
10769  if (prelude) {
10770  prelude->nd_body = node;
10771  scope->nd_body = prelude;
10772  }
10773  else {
10774  scope->nd_body = node;
10775  }
10776 
10777  return scope;
10778 }
10779 
10780 void
10781 rb_init_parse(void)
10782 {
10783  /* just to suppress unused-function warnings */
10784  (void)nodetype;
10785  (void)nodeline;
10786 }
10787 #endif /* !RIPPER */
10788 
10789 static ID
10790 internal_id_gen(struct parser_params *parser)
10791 {
10792  ID id = (ID)vtable_size(lvtbl->args) + (ID)vtable_size(lvtbl->vars);
10793  id += ((tLAST_TOKEN - ID_INTERNAL) >> ID_SCOPE_SHIFT) + 1;
10794  return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
10795 }
10796 
10797 static void
10798 parser_initialize(struct parser_params *parser)
10799 {
10800  /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
10801  command_start = TRUE;
10802  ruby_sourcefile_string = Qnil;
10803 #ifdef RIPPER
10804  parser->delayed = Qnil;
10805  parser->result = Qnil;
10806  parser->parsing_thread = Qnil;
10807 #else
10808  parser->error_buffer = Qfalse;
10809 #endif
10810  parser->debug_buffer = Qnil;
10811  parser->enc = rb_utf8_encoding();
10812 }
10813 
10814 #ifdef RIPPER
10815 #define parser_mark ripper_parser_mark
10816 #define parser_free ripper_parser_free
10817 #endif
10818 
10819 static void
10820 parser_mark(void *ptr)
10821 {
10822  struct parser_params *parser = (struct parser_params*)ptr;
10823 
10824  rb_gc_mark((VALUE)lex_strterm);
10825  rb_gc_mark(lex_input);
10826  rb_gc_mark(lex_lastline);
10827  rb_gc_mark(lex_nextline);
10828  rb_gc_mark(ruby_sourcefile_string);
10829 #ifndef RIPPER
10830  rb_gc_mark((VALUE)ruby_eval_tree_begin);
10831  rb_gc_mark((VALUE)ruby_eval_tree);
10832  rb_gc_mark(ruby_debug_lines);
10833  rb_gc_mark(parser->compile_option);
10834  rb_gc_mark(parser->error_buffer);
10835 #else
10836  rb_gc_mark(parser->delayed);
10837  rb_gc_mark(parser->value);
10838  rb_gc_mark(parser->result);
10839  rb_gc_mark(parser->parsing_thread);
10840 #endif
10841  rb_gc_mark(parser->debug_buffer);
10842 #ifdef YYMALLOC
10843  rb_gc_mark((VALUE)parser->heap);
10844 #endif
10845 }
10846 
10847 static void
10848 parser_free(void *ptr)
10849 {
10850  struct parser_params *parser = (struct parser_params*)ptr;
10851  struct local_vars *local, *prev;
10852 
10853  if (tokenbuf) {
10854  xfree(tokenbuf);
10855  }
10856  for (local = lvtbl; local; local = prev) {
10857  if (local->vars) xfree(local->vars);
10858  prev = local->prev;
10859  xfree(local);
10860  }
10861  {
10862  token_info *ptinfo;
10863  while ((ptinfo = parser->token_info) != 0) {
10864  parser->token_info = ptinfo->next;
10865  xfree(ptinfo);
10866  }
10867  }
10868  xfree(ptr);
10869 }
10870 
10871 static size_t
10872 parser_memsize(const void *ptr)
10873 {
10874  struct parser_params *parser = (struct parser_params*)ptr;
10875  struct local_vars *local;
10876  size_t size = sizeof(*parser);
10877 
10878  size += toksiz;
10879  for (local = lvtbl; local; local = local->prev) {
10880  size += sizeof(*local);
10881  if (local->vars) size += local->vars->capa * sizeof(ID);
10882  }
10883  return size;
10884 }
10885 
10886 static const rb_data_type_t parser_data_type = {
10887 #ifndef RIPPER
10888  "parser",
10889 #else
10890  "ripper",
10891 #endif
10892  {
10893  parser_mark,
10894  parser_free,
10895  parser_memsize,
10896  },
10897  0, 0, RUBY_TYPED_FREE_IMMEDIATELY
10898 };
10899 
10900 #ifndef RIPPER
10901 #undef rb_reserved_word
10902 
10903 const struct kwtable *
10904 rb_reserved_word(const char *str, unsigned int len)
10905 {
10906  return reserved_word(str, len);
10907 }
10908 
10909 VALUE
10910 rb_parser_new(void)
10911 {
10912  struct parser_params *p;
10913  VALUE parser = TypedData_Make_Struct(0, struct parser_params,
10914  &parser_data_type, p);
10915  parser_initialize(p);
10916  return parser;
10917 }
10918 
10919 VALUE
10920 rb_parser_set_context(VALUE vparser, const struct rb_block *base, int main)
10921 {
10922  struct parser_params *parser;
10923 
10924  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
10925  parser->error_buffer = main ? Qfalse : Qnil;
10926  parser->base_block = base;
10927  in_main = main;
10928  return vparser;
10929 }
10930 #endif
10931 
10932 #ifdef RIPPER
10933 #define rb_parser_end_seen_p ripper_parser_end_seen_p
10934 #define rb_parser_encoding ripper_parser_encoding
10935 #define rb_parser_get_yydebug ripper_parser_get_yydebug
10936 #define rb_parser_set_yydebug ripper_parser_set_yydebug
10937 static VALUE ripper_parser_end_seen_p(VALUE vparser);
10938 static VALUE ripper_parser_encoding(VALUE vparser);
10939 static VALUE ripper_parser_get_yydebug(VALUE self);
10940 static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
10941 
10942 /*
10943  * call-seq:
10944  * ripper#error? -> Boolean
10945  *
10946  * Return true if parsed source has errors.
10947  */
10948 static VALUE
10949 ripper_error_p(VALUE vparser)
10950 {
10951  struct parser_params *parser;
10952 
10953  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
10954  return parser->error_p ? Qtrue : Qfalse;
10955 }
10956 #endif
10957 
10958 /*
10959  * call-seq:
10960  * ripper#end_seen? -> Boolean
10961  *
10962  * Return true if parsed source ended by +\_\_END\_\_+.
10963  */
10964 VALUE
10965 rb_parser_end_seen_p(VALUE vparser)
10966 {
10967  struct parser_params *parser;
10968 
10969  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
10970  return ruby__end__seen ? Qtrue : Qfalse;
10971 }
10972 
10973 /*
10974  * call-seq:
10975  * ripper#encoding -> encoding
10976  *
10977  * Return encoding of the source.
10978  */
10979 VALUE
10980 rb_parser_encoding(VALUE vparser)
10981 {
10982  struct parser_params *parser;
10983 
10984  TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
10985  return rb_enc_from_encoding(current_enc);
10986 }
10987 
10988 /*
10989  * call-seq:
10990  * ripper.yydebug -> true or false
10991  *
10992  * Get yydebug.
10993  */
10994 VALUE
10995 rb_parser_get_yydebug(VALUE self)
10996 {
10997  struct parser_params *parser;
10998 
10999  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11000  return yydebug ? Qtrue : Qfalse;
11001 }
11002 
11003 /*
11004  * call-seq:
11005  * ripper.yydebug = flag
11006  *
11007  * Set yydebug.
11008  */
11009 VALUE
11010 rb_parser_set_yydebug(VALUE self, VALUE flag)
11011 {
11012  struct parser_params *parser;
11013 
11014  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11015  yydebug = RTEST(flag);
11016  return flag;
11017 }
11018 
11019 #ifndef RIPPER
11020 #ifdef YYMALLOC
11021 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
11022 #define NEWHEAP() rb_node_newnode(NODE_ALLOCA, 0, (VALUE)parser->heap, 0)
11023 #define ADD2HEAP(n, c, p) ((parser->heap = (n))->u1.node = (p), \
11024  (n)->u3.cnt = (c), (p))
11025 
11026 void *
11027 rb_parser_malloc(struct parser_params *parser, size_t size)
11028 {
11029  size_t cnt = HEAPCNT(1, size);
11030  NODE *n = NEWHEAP();
11031  void *ptr = xmalloc(size);
11032 
11033  return ADD2HEAP(n, cnt, ptr);
11034 }
11035 
11036 void *
11037 rb_parser_calloc(struct parser_params *parser, size_t nelem, size_t size)
11038 {
11039  size_t cnt = HEAPCNT(nelem, size);
11040  NODE *n = NEWHEAP();
11041  void *ptr = xcalloc(nelem, size);
11042 
11043  return ADD2HEAP(n, cnt, ptr);
11044 }
11045 
11046 void *
11047 rb_parser_realloc(struct parser_params *parser, void *ptr, size_t size)
11048 {
11049  NODE *n;
11050  size_t cnt = HEAPCNT(1, size);
11051 
11052  if (ptr && (n = parser->heap) != NULL) {
11053  do {
11054  if (n->u1.node == ptr) {
11055  n->u1.node = ptr = xrealloc(ptr, size);
11056  if (n->u3.cnt) n->u3.cnt = cnt;
11057  return ptr;
11058  }
11059  } while ((n = n->u2.node) != NULL);
11060  }
11061  n = NEWHEAP();
11062  ptr = xrealloc(ptr, size);
11063  return ADD2HEAP(n, cnt, ptr);
11064 }
11065 
11066 void
11067 rb_parser_free(struct parser_params *parser, void *ptr)
11068 {
11069  NODE **prev = &parser->heap, *n;
11070 
11071  while ((n = *prev) != NULL) {
11072  if (n->u1.node == ptr) {
11073  *prev = n->u2.node;
11074  rb_gc_force_recycle((VALUE)n);
11075  break;
11076  }
11077  prev = &n->u2.node;
11078  }
11079  xfree(ptr);
11080 }
11081 #endif
11082 
11083 void
11084 rb_parser_printf(struct parser_params *parser, const char *fmt, ...)
11085 {
11086  va_list ap;
11087  VALUE mesg = parser->debug_buffer;
11088 
11089  if (NIL_P(mesg)) parser->debug_buffer = mesg = rb_str_new(0, 0);
11090  va_start(ap, fmt);
11091  rb_str_vcatf(mesg, fmt, ap);
11092  va_end(ap);
11093  if (RSTRING_END(mesg)[-1] == '\n') {
11094  rb_io_write(rb_stdout, mesg);
11095  parser->debug_buffer = Qnil;
11096  }
11097 }
11098 
11099 static void
11100 parser_compile_error(struct parser_params *parser, const char *fmt, ...)
11101 {
11102  va_list ap;
11103 
11104  parser->error_p = 1;
11105  va_start(ap, fmt);
11106  parser->error_buffer =
11107  rb_syntax_error_append(parser->error_buffer,
11108  ruby_sourcefile_string,
11109  ruby_sourceline,
11110  rb_long2int(lex_p - lex_pbeg),
11111  current_enc, fmt, ap);
11112  va_end(ap);
11113 }
11114 #endif
11115 
11116 #ifdef RIPPER
11117 #ifdef RIPPER_DEBUG
11118 extern int rb_is_pointer_to_heap(VALUE);
11119 
11120 /* :nodoc: */
11121 static VALUE
11122 ripper_validate_object(VALUE self, VALUE x)
11123 {
11124  if (x == Qfalse) return x;
11125  if (x == Qtrue) return x;
11126  if (x == Qnil) return x;
11127  if (x == Qundef)
11128  rb_raise(rb_eArgError, "Qundef given");
11129  if (FIXNUM_P(x)) return x;
11130  if (SYMBOL_P(x)) return x;
11131  if (!rb_is_pointer_to_heap(x))
11132  rb_raise(rb_eArgError, "invalid pointer: %p", x);
11133  switch (BUILTIN_TYPE(x)) {
11134  case T_STRING:
11135  case T_OBJECT:
11136  case T_ARRAY:
11137  case T_BIGNUM:
11138  case T_FLOAT:
11139  case T_COMPLEX:
11140  case T_RATIONAL:
11141  return x;
11142  case T_NODE:
11143  if (nd_type(x) != NODE_RIPPER) {
11144  rb_raise(rb_eArgError, "NODE given: %p", x);
11145  }
11146  return ((NODE *)x)->nd_rval;
11147  default:
11148  rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
11149  x, rb_obj_classname(x));
11150  }
11151  return x;
11152 }
11153 #endif
11154 
11155 #define validate(x) ((x) = get_value(x))
11156 
11157 static VALUE
11158 ripper_dispatch0(struct parser_params *parser, ID mid)
11159 {
11160  return rb_funcall(parser->value, mid, 0);
11161 }
11162 
11163 static VALUE
11164 ripper_dispatch1(struct parser_params *parser, ID mid, VALUE a)
11165 {
11166  validate(a);
11167  return rb_funcall(parser->value, mid, 1, a);
11168 }
11169 
11170 static VALUE
11171 ripper_dispatch2(struct parser_params *parser, ID mid, VALUE a, VALUE b)
11172 {
11173  validate(a);
11174  validate(b);
11175  return rb_funcall(parser->value, mid, 2, a, b);
11176 }
11177 
11178 static VALUE
11179 ripper_dispatch3(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c)
11180 {
11181  validate(a);
11182  validate(b);
11183  validate(c);
11184  return rb_funcall(parser->value, mid, 3, a, b, c);
11185 }
11186 
11187 static VALUE
11188 ripper_dispatch4(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
11189 {
11190  validate(a);
11191  validate(b);
11192  validate(c);
11193  validate(d);
11194  return rb_funcall(parser->value, mid, 4, a, b, c, d);
11195 }
11196 
11197 static VALUE
11198 ripper_dispatch5(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
11199 {
11200  validate(a);
11201  validate(b);
11202  validate(c);
11203  validate(d);
11204  validate(e);
11205  return rb_funcall(parser->value, mid, 5, a, b, c, d, e);
11206 }
11207 
11208 static VALUE
11209 ripper_dispatch7(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
11210 {
11211  validate(a);
11212  validate(b);
11213  validate(c);
11214  validate(d);
11215  validate(e);
11216  validate(f);
11217  validate(g);
11218  return rb_funcall(parser->value, mid, 7, a, b, c, d, e, f, g);
11219 }
11220 
11221 static const struct kw_assoc {
11222  ID id;
11223  const char *name;
11224 } keyword_to_name[] = {
11225  {keyword_class, "class"},
11226  {keyword_module, "module"},
11227  {keyword_def, "def"},
11228  {keyword_undef, "undef"},
11229  {keyword_begin, "begin"},
11230  {keyword_rescue, "rescue"},
11231  {keyword_ensure, "ensure"},
11232  {keyword_end, "end"},
11233  {keyword_if, "if"},
11234  {keyword_unless, "unless"},
11235  {keyword_then, "then"},
11236  {keyword_elsif, "elsif"},
11237  {keyword_else, "else"},
11238  {keyword_case, "case"},
11239  {keyword_when, "when"},
11240  {keyword_while, "while"},
11241  {keyword_until, "until"},
11242  {keyword_for, "for"},
11243  {keyword_break, "break"},
11244  {keyword_next, "next"},
11245  {keyword_redo, "redo"},
11246  {keyword_retry, "retry"},
11247  {keyword_in, "in"},
11248  {keyword_do, "do"},
11249  {keyword_do_cond, "do"},
11250  {keyword_do_block, "do"},
11251  {keyword_return, "return"},
11252  {keyword_yield, "yield"},
11253  {keyword_super, "super"},
11254  {keyword_self, "self"},
11255  {keyword_nil, "nil"},
11256  {keyword_true, "true"},
11257  {keyword_false, "false"},
11258  {keyword_and, "and"},
11259  {keyword_or, "or"},
11260  {keyword_not, "not"},
11261  {modifier_if, "if"},
11262  {modifier_unless, "unless"},
11263  {modifier_while, "while"},
11264  {modifier_until, "until"},
11265  {modifier_rescue, "rescue"},
11266  {keyword_alias, "alias"},
11267  {keyword_defined, "defined?"},
11268  {keyword_BEGIN, "BEGIN"},
11269  {keyword_END, "END"},
11270  {keyword__LINE__, "__LINE__"},
11271  {keyword__FILE__, "__FILE__"},
11272  {keyword__ENCODING__, "__ENCODING__"},
11273  {0, NULL}
11274 };
11275 
11276 static const char*
11277 keyword_id_to_str(ID id)
11278 {
11279  const struct kw_assoc *a;
11280 
11281  for (a = keyword_to_name; a->id; a++) {
11282  if (a->id == id)
11283  return a->name;
11284  }
11285  return NULL;
11286 }
11287 
11288 #undef ripper_id2sym
11289 static VALUE
11290 ripper_id2sym(ID id)
11291 {
11292  const char *name;
11293  char buf[8];
11294 
11295  if (id == (ID)(signed char)id) {
11296  buf[0] = (char)id;
11297  buf[1] = '\0';
11298  return ID2SYM(rb_intern2(buf, 1));
11299  }
11300  if ((name = keyword_id_to_str(id))) {
11301  return ID2SYM(rb_intern(name));
11302  }
11303  if (!rb_id2str(id)) {
11304  rb_bug("cannot convert ID to string: %ld", (unsigned long)id);
11305  }
11306  return ID2SYM(id);
11307 }
11308 
11309 static ID
11310 ripper_get_id(VALUE v)
11311 {
11312  NODE *nd;
11313  if (!RB_TYPE_P(v, T_NODE)) return 0;
11314  nd = (NODE *)v;
11315  if (nd_type(nd) != NODE_RIPPER) return 0;
11316  return nd->nd_vid;
11317 }
11318 
11319 static VALUE
11320 ripper_get_value(VALUE v)
11321 {
11322  NODE *nd;
11323  if (v == Qundef) return Qnil;
11324  if (!RB_TYPE_P(v, T_NODE)) return v;
11325  nd = (NODE *)v;
11326  if (nd_type(nd) != NODE_RIPPER) return Qnil;
11327  return nd->nd_rval;
11328 }
11329 
11330 static void
11331 ripper_error_gen(struct parser_params *parser)
11332 {
11333  parser->error_p = TRUE;
11334 }
11335 
11336 static void
11337 ripper_compile_error(struct parser_params *parser, const char *fmt, ...)
11338 {
11339  VALUE str;
11340  va_list args;
11341 
11342  va_start(args, fmt);
11343  str = rb_vsprintf(fmt, args);
11344  va_end(args);
11345  rb_funcall(parser->value, rb_intern("compile_error"), 1, str);
11346  ripper_error_gen(parser);
11347 }
11348 
11349 static VALUE
11350 ripper_lex_get_generic(struct parser_params *parser, VALUE src)
11351 {
11352  VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
11353  if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
11354  rb_raise(rb_eTypeError,
11355  "gets returned %"PRIsVALUE" (expected String or nil)",
11356  rb_obj_class(line));
11357  }
11358  return line;
11359 }
11360 
11361 static VALUE
11362 ripper_lex_io_get(struct parser_params *parser, VALUE src)
11363 {
11364  return rb_io_gets(src);
11365 }
11366 
11367 static VALUE
11368 ripper_s_allocate(VALUE klass)
11369 {
11370  struct parser_params *p;
11371  VALUE self = TypedData_Make_Struct(klass, struct parser_params,
11372  &parser_data_type, p);
11373  p->value = self;
11374  return self;
11375 }
11376 
11377 #define ripper_initialized_p(r) ((r)->lex.input != 0)
11378 
11379 /*
11380  * call-seq:
11381  * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
11382  *
11383  * Create a new Ripper object.
11384  * _src_ must be a String, an IO, or an Object which has #gets method.
11385  *
11386  * This method does not starts parsing.
11387  * See also Ripper#parse and Ripper.parse.
11388  */
11389 static VALUE
11390 ripper_initialize(int argc, VALUE *argv, VALUE self)
11391 {
11392  struct parser_params *parser;
11393  VALUE src, fname, lineno;
11394 
11395  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11396  rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
11397  if (RB_TYPE_P(src, T_FILE)) {
11398  lex_gets = ripper_lex_io_get;
11399  }
11400  else if (rb_respond_to(src, id_gets)) {
11401  lex_gets = ripper_lex_get_generic;
11402  }
11403  else {
11404  StringValue(src);
11405  lex_gets = lex_get_str;
11406  }
11407  lex_input = src;
11408  parser->eofp = 0;
11409  if (NIL_P(fname)) {
11410  fname = STR_NEW2("(ripper)");
11411  OBJ_FREEZE(fname);
11412  }
11413  else {
11414  StringValue(fname);
11415  fname = rb_str_new_frozen(fname);
11416  }
11417  parser_initialize(parser);
11418 
11419  ruby_sourcefile_string = fname;
11420  ruby_sourcefile = RSTRING_PTR(fname);
11421  ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
11422 
11423  return Qnil;
11424 }
11425 
11426 struct ripper_args {
11427  struct parser_params *parser;
11428  int argc;
11429  VALUE *argv;
11430 };
11431 
11432 static VALUE
11433 ripper_parse0(VALUE parser_v)
11434 {
11435  struct parser_params *parser;
11436 
11437  TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, parser);
11438  parser_prepare(parser);
11439  ripper_yyparse((void*)parser);
11440  return parser->result;
11441 }
11442 
11443 static VALUE
11444 ripper_ensure(VALUE parser_v)
11445 {
11446  struct parser_params *parser;
11447 
11448  TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, parser);
11449  parser->parsing_thread = Qnil;
11450  return Qnil;
11451 }
11452 
11453 /*
11454  * call-seq:
11455  * ripper#parse
11456  *
11457  * Start parsing and returns the value of the root action.
11458  */
11459 static VALUE
11460 ripper_parse(VALUE self)
11461 {
11462  struct parser_params *parser;
11463 
11464  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11465  if (!ripper_initialized_p(parser)) {
11466  rb_raise(rb_eArgError, "method called for uninitialized object");
11467  }
11468  if (!NIL_P(parser->parsing_thread)) {
11469  if (parser->parsing_thread == rb_thread_current())
11470  rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
11471  else
11472  rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
11473  }
11474  parser->parsing_thread = rb_thread_current();
11475  rb_ensure(ripper_parse0, self, ripper_ensure, self);
11476 
11477  return parser->result;
11478 }
11479 
11480 /*
11481  * call-seq:
11482  * ripper#column -> Integer
11483  *
11484  * Return column number of current parsing line.
11485  * This number starts from 0.
11486  */
11487 static VALUE
11488 ripper_column(VALUE self)
11489 {
11490  struct parser_params *parser;
11491  long col;
11492 
11493  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11494  if (!ripper_initialized_p(parser)) {
11495  rb_raise(rb_eArgError, "method called for uninitialized object");
11496  }
11497  if (NIL_P(parser->parsing_thread)) return Qnil;
11498  col = parser->tokp - lex_pbeg;
11499  return LONG2NUM(col);
11500 }
11501 
11502 /*
11503  * call-seq:
11504  * ripper#filename -> String
11505  *
11506  * Return current parsing filename.
11507  */
11508 static VALUE
11509 ripper_filename(VALUE self)
11510 {
11511  struct parser_params *parser;
11512 
11513  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11514  if (!ripper_initialized_p(parser)) {
11515  rb_raise(rb_eArgError, "method called for uninitialized object");
11516  }
11517  return ruby_sourcefile_string;
11518 }
11519 
11520 /*
11521  * call-seq:
11522  * ripper#lineno -> Integer
11523  *
11524  * Return line number of current parsing line.
11525  * This number starts from 1.
11526  */
11527 static VALUE
11528 ripper_lineno(VALUE self)
11529 {
11530  struct parser_params *parser;
11531 
11532  TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11533  if (!ripper_initialized_p(parser)) {
11534  rb_raise(rb_eArgError, "method called for uninitialized object");
11535  }
11536  if (NIL_P(parser->parsing_thread)) return Qnil;
11537  return INT2NUM(ruby_sourceline);
11538 }
11539 
11540 #ifdef RIPPER_DEBUG
11541 /* :nodoc: */
11542 static VALUE
11543 ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
11544 {
11545  StringValue(msg);
11546  if (obj == Qundef) {
11547  rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
11548  }
11549  return Qnil;
11550 }
11551 
11552 /* :nodoc: */
11553 static VALUE
11554 ripper_value(VALUE self, VALUE obj)
11555 {
11556  return ULONG2NUM(obj);
11557 }
11558 #endif
11559 
11560 
11561 void
11562 Init_ripper(void)
11563 {
11564  ripper_init_eventids1();
11565  ripper_init_eventids2();
11566  id_warn = rb_intern_const("warn");
11567  id_warning = rb_intern_const("warning");
11568  id_gets = rb_intern_const("gets");
11569 
11570  InitVM(ripper);
11571 }
11572 
11573 void
11574 InitVM_ripper(void)
11575 {
11576  VALUE Ripper;
11577 
11578  Ripper = rb_define_class("Ripper", rb_cObject);
11579  /* version of Ripper */
11580  rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
11581  rb_define_alloc_func(Ripper, ripper_s_allocate);
11582  rb_define_method(Ripper, "initialize", ripper_initialize, -1);
11583  rb_define_method(Ripper, "parse", ripper_parse, 0);
11584  rb_define_method(Ripper, "column", ripper_column, 0);
11585  rb_define_method(Ripper, "filename", ripper_filename, 0);
11586  rb_define_method(Ripper, "lineno", ripper_lineno, 0);
11587  rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
11588  rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
11589  rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
11590  rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
11591  rb_define_method(Ripper, "error?", ripper_error_p, 0);
11592 #ifdef RIPPER_DEBUG
11593  rb_define_method(rb_mKernel, "assert_Qundef", ripper_assert_Qundef, 2);
11594  rb_define_method(rb_mKernel, "rawVALUE", ripper_value, 1);
11595  rb_define_method(rb_mKernel, "validate_object", ripper_validate_object, 1);
11596 #endif
11597 
11598  rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
11599  rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
11600 
11601  ripper_init_eventids1_table(Ripper);
11602  ripper_init_eventids2_table(Ripper);
11603 
11604 # if 0
11605  /* Hack to let RDoc document SCRIPT_LINES__ */
11606 
11607  /*
11608  * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
11609  * after the assignment will be added as an Array of lines with the file
11610  * name as the key.
11611  */
11612  rb_define_global_const("SCRIPT_LINES__", Qnil);
11613 #endif
11614 
11615 }
11616 #endif /* RIPPER */