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