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