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