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