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