Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
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#define YYDEBUG 1
18#define YYERROR_VERBOSE 1
19#define YYSTACK_USE_ALLOCA 0
20#define YYLTYPE rb_code_location_t
21#define YYLTYPE_IS_DECLARED 1
22
23#include "ruby/ruby.h"
24#include "ruby/st.h"
25#include "ruby/encoding.h"
26#include "internal.h"
27#include "node.h"
28#include "parse.h"
29#include "symbol.h"
30#include "regenc.h"
31#include <stdio.h>
32#include <errno.h>
33#include <ctype.h>
34#include "probes.h"
35
36#ifndef WARN_PAST_SCOPE
37# define WARN_PAST_SCOPE 0
38#endif
39
40#define TAB_WIDTH 8
41
42#define yydebug (p->debug) /* disable the global variable definition */
43
44#define YYMALLOC(size) rb_parser_malloc(p, (size))
45#define YYREALLOC(ptr, size) rb_parser_realloc(p, (ptr), (size))
46#define YYCALLOC(nelem, size) rb_parser_calloc(p, (nelem), (size))
47#define YYFREE(ptr) rb_parser_free(p, (ptr))
48#define YYFPRINTF rb_parser_printf
49#define YY_LOCATION_PRINT(File, loc) \
50 rb_parser_printf(p, "%d.%d-%d.%d", \
51 (loc).beg_pos.lineno, (loc).beg_pos.column,\
52 (loc).end_pos.lineno, (loc).end_pos.column)
53#define YYLLOC_DEFAULT(Current, Rhs, N) \
54 do \
55 if (N) \
56 { \
57 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
58 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
59 } \
60 else \
61 { \
62 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
63 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
64 } \
65 while (0)
66
67#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
68 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
69#define RUBY_SET_YYLLOC_OF_NONE(Current) \
70 rb_parser_set_location_of_none(p, &(Current))
71#define RUBY_SET_YYLLOC(Current) \
72 rb_parser_set_location(p, &(Current))
73#define RUBY_INIT_YYLLOC() \
74 { \
75 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
76 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
77 }
78
79enum lex_state_bits {
80 EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
81 EXPR_END_bit, /* newline significant, +/- is an operator. */
82 EXPR_ENDARG_bit, /* ditto, and unbound braces. */
83 EXPR_ENDFN_bit, /* ditto, and unbound braces. */
84 EXPR_ARG_bit, /* newline significant, +/- is an operator. */
85 EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
86 EXPR_MID_bit, /* newline significant, +/- is an operator. */
87 EXPR_FNAME_bit, /* ignore newline, no reserved words. */
88 EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
89 EXPR_CLASS_bit, /* immediate after `class', no here document. */
90 EXPR_LABEL_bit, /* flag bit, label is allowed. */
91 EXPR_LABELED_bit, /* flag bit, just after a label. */
92 EXPR_FITEM_bit, /* symbol literal as FNAME. */
93 EXPR_MAX_STATE
94};
95/* examine combinations */
96enum lex_state_e {
97#define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
98 DEF_EXPR(BEG),
99 DEF_EXPR(END),
100 DEF_EXPR(ENDARG),
101 DEF_EXPR(ENDFN),
102 DEF_EXPR(ARG),
103 DEF_EXPR(CMDARG),
104 DEF_EXPR(MID),
105 DEF_EXPR(FNAME),
106 DEF_EXPR(DOT),
107 DEF_EXPR(CLASS),
108 DEF_EXPR(LABEL),
109 DEF_EXPR(LABELED),
110 DEF_EXPR(FITEM),
111 EXPR_VALUE = EXPR_BEG,
112 EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
113 EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
114 EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN),
115 EXPR_NONE = 0
116};
117#define IS_lex_state_for(x, ls) ((x) & (ls))
118#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
119#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
120#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
121
122# define SET_LEX_STATE(ls) \
123 (p->lex.state = \
124 (p->debug ? \
125 rb_parser_trace_lex_state(p, p->lex.state, (ls), __LINE__) : \
126 (enum lex_state_e)(ls)))
127
128typedef VALUE stack_type;
129
130static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
131
132# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
133# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
134# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
135# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
136# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
137
138/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
139 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
140#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
141#define COND_POP() BITSTACK_POP(cond_stack)
142#define COND_P() BITSTACK_SET_P(cond_stack)
143#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
144
145/* A flag to identify keyword_do_block; "do" keyword after command_call.
146 Example: `foo 1, 2 do`. */
147#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
148#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
149#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
150#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
151
152struct vtable {
153 ID *tbl;
154 int pos;
155 int capa;
156 struct vtable *prev;
157};
158
159struct local_vars {
160 struct vtable *args;
161 struct vtable *vars;
162 struct vtable *used;
163# if WARN_PAST_SCOPE
164 struct vtable *past;
165# endif
166 struct local_vars *prev;
167# ifndef RIPPER
168 struct {
169 NODE *outer, *inner, *current;
170 } numparam;
171# endif
172};
173
174enum {
175 ORDINAL_PARAM = -1,
176 NO_PARAM = 0,
177 NUMPARAM_MAX = 9,
178};
179
180#define NUMPARAM_ID_P(id) numparam_id_p(id)
181#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - tNUMPARAM_1 + 1)
182#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 + (idx) - 1))
183static int
184numparam_id_p(ID id)
185{
186 if (!is_local_id(id)) return 0;
187 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
188 return idx > 0 && idx <= NUMPARAM_MAX;
189}
190static void numparam_name(struct parser_params *p, ID id);
191
192#define DVARS_INHERIT ((void*)1)
193#define DVARS_TOPSCOPE NULL
194#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
195
196typedef struct token_info {
197 const char *token;
198 rb_code_position_t beg;
199 int indent;
200 int nonspc;
201 struct token_info *next;
202} token_info;
203
204typedef struct rb_strterm_struct rb_strterm_t;
205
206/*
207 Structure of Lexer Buffer:
208
209 lex.pbeg lex.ptok lex.pcur lex.pend
210 | | | |
211 |------------+------------+------------|
212 |<---------->|
213 token
214*/
215struct parser_params {
216 rb_imemo_tmpbuf_t *heap;
217
218 YYSTYPE *lval;
219
220 struct {
221 rb_strterm_t *strterm;
222 VALUE (*gets)(struct parser_params*,VALUE);
223 VALUE input;
224 VALUE prevline;
225 VALUE lastline;
226 VALUE nextline;
227 const char *pbeg;
228 const char *pcur;
229 const char *pend;
230 const char *ptok;
231 union {
232 long ptr;
233 VALUE (*call)(VALUE, int);
234 } gets_;
235 enum lex_state_e state;
236 /* track the nest level of any parens "()[]{}" */
237 int paren_nest;
238 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
239 int lpar_beg;
240 /* track the nest level of only braces "{}" */
241 int brace_nest;
242 } lex;
243 stack_type cond_stack;
244 stack_type cmdarg_stack;
245 int tokidx;
246 int toksiz;
247 int tokline;
248 int heredoc_end;
249 int heredoc_indent;
250 int heredoc_line_indent;
251 char *tokenbuf;
252 struct local_vars *lvtbl;
253 st_table *pvtbl;
254 st_table *pktbl;
255 int line_count;
256 int ruby_sourceline; /* current line no. */
257 const char *ruby_sourcefile; /* current source file */
258 VALUE ruby_sourcefile_string;
259 rb_encoding *enc;
260 token_info *token_info;
261 VALUE case_labels;
262 VALUE compile_option;
263
264 VALUE debug_buffer;
265 VALUE debug_output;
266
267 ID cur_arg;
268
269 rb_ast_t *ast;
270 int node_id;
271
272 int max_numparam;
273
274 unsigned int command_start:1;
275 unsigned int eofp: 1;
276 unsigned int ruby__end__seen: 1;
277 unsigned int debug: 1;
278 unsigned int has_shebang: 1;
279 unsigned int in_defined: 1;
280 unsigned int in_kwarg: 1;
281 unsigned int in_def: 1;
282 unsigned int in_class: 1;
283 unsigned int token_seen: 1;
284 unsigned int token_info_enabled: 1;
285# if WARN_PAST_SCOPE
286 unsigned int past_scope_enabled: 1;
287# endif
288 unsigned int error_p: 1;
289 unsigned int cr_seen: 1;
290
291#ifndef RIPPER
292 /* Ruby core only */
293
294 unsigned int do_print: 1;
295 unsigned int do_loop: 1;
296 unsigned int do_chomp: 1;
297 unsigned int do_split: 1;
298 unsigned int warn_location: 1;
299
300 NODE *eval_tree_begin;
301 NODE *eval_tree;
302 VALUE error_buffer;
303 VALUE debug_lines;
304 const struct rb_iseq_struct *parent_iseq;
305#else
306 /* Ripper only */
307
308 struct {
309 VALUE token;
310 int line;
311 int col;
312 } delayed;
313
314 VALUE value;
315 VALUE result;
316 VALUE parsing_thread;
317#endif
318};
319
320#define intern_cstr(n,l,en) rb_intern3(n,l,en)
321
322#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
323#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
324#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
325#define STR_NEW3(ptr,len,e,func) parser_str_new((ptr),(len),(e),(func),p->enc)
326#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
327
328static st_table *
329push_pvtbl(struct parser_params *p)
330{
331 st_table *tbl = p->pvtbl;
332 p->pvtbl = st_init_numtable();
333 return tbl;
334}
335
336static void
337pop_pvtbl(struct parser_params *p, st_table *tbl)
338{
339 st_free_table(p->pvtbl);
340 p->pvtbl = tbl;
341}
342
343static st_table *
344push_pktbl(struct parser_params *p)
345{
346 st_table *tbl = p->pktbl;
347 p->pktbl = 0;
348 return tbl;
349}
350
351static void
352pop_pktbl(struct parser_params *p, st_table *tbl)
353{
354 if (p->pktbl) st_free_table(p->pktbl);
355 p->pktbl = tbl;
356}
357
358static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
359#define yyerror0(msg) parser_yyerror(p, NULL, (msg))
360#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
361#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
362#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
363
364#ifdef RIPPER
365#define compile_for_eval (0)
366#else
367#define compile_for_eval (p->parent_iseq != 0)
368#endif
369
370#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
371
372#define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
373#define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
374#define NEW_QCALL(q,r,m,a,loc) NEW_NODE(NODE_CALL_Q(q),r,m,a,loc)
375
376#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
377
378static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
379
380#ifndef RIPPER
381static inline void
382rb_discard_node(struct parser_params *p, NODE *n)
383{
384 rb_ast_delete_node(p->ast, n);
385}
386#endif
387
388#ifdef RIPPER
389static inline VALUE
390add_mark_object(struct parser_params *p, VALUE obj)
391{
392 if (!SPECIAL_CONST_P(obj)
393 && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */
394 ) {
395 rb_ast_add_mark_object(p->ast, obj);
396 }
397 return obj;
398}
399#else
400static NODE* node_newnode_with_locals(struct parser_params *, enum node_type, VALUE, VALUE, const rb_code_location_t*);
401#endif
402
403static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE, const rb_code_location_t*);
404#define rb_node_newnode(type, a1, a2, a3, loc) node_newnode(p, (type), (a1), (a2), (a3), (loc))
405
406static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
407
408static int
409parser_get_node_id(struct parser_params *p)
410{
411 int node_id = p->node_id;
412 p->node_id++;
413 return node_id;
414}
415
416#ifndef RIPPER
417static inline void
418set_line_body(NODE *body, int line)
419{
420 if (!body) return;
421 switch (nd_type(body)) {
422 case NODE_RESCUE:
423 case NODE_ENSURE:
424 nd_set_line(body, line);
425 }
426}
427
428#define yyparse ruby_yyparse
429
430static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
431static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
432#define new_nil(loc) NEW_NIL(loc)
433static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
434static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
435static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
436
437static NODE *newline_node(NODE*);
438static void fixpos(NODE*,NODE*);
439
440static int value_expr_gen(struct parser_params*,NODE*);
441static void void_expr(struct parser_params*,NODE*);
442static NODE *remove_begin(NODE*);
443static NODE *remove_begin_all(NODE*);
444#define value_expr(node) value_expr_gen(p, (node) = remove_begin(node))
445static NODE *void_stmts(struct parser_params*,NODE*);
446static void reduce_nodes(struct parser_params*,NODE**);
447static void block_dup_check(struct parser_params*,NODE*,NODE*);
448
449static NODE *block_append(struct parser_params*,NODE*,NODE*);
450static NODE *list_append(struct parser_params*,NODE*,NODE*);
451static NODE *list_concat(NODE*,NODE*);
452static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
453static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
454static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
455static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
456static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
457static NODE *evstr2dstr(struct parser_params*,NODE*);
458static NODE *splat_array(NODE*);
459static void mark_lvar_used(struct parser_params *p, NODE *rhs);
460
461static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
462static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
463static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
464static NODE *new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc);
465static NODE *method_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc) {b->nd_iter = m; b->nd_loc = *loc; return b;}
466
467static bool args_info_empty_p(struct rb_args_info *args);
468static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
469static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
470static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
471static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
472static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
473static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
474static NODE *new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc);
475
476static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
477static NODE *args_with_numbered(struct parser_params*,NODE*,int);
478
479static VALUE negate_lit(struct parser_params*, VALUE);
480static NODE *ret_args(struct parser_params*,NODE*);
481static NODE *arg_blk_pass(NODE*,NODE*);
482static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
483static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
484
485static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
486static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
487
488static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
489static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
490
491static void rb_backref_error(struct parser_params*,NODE*);
492static NODE *node_assign(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
493
494static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc);
495static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
496static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
497static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc);
498static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
499
500static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
501
502static NODE *opt_arg_append(NODE*, NODE*);
503static NODE *kwd_append(NODE*, NODE*);
504
505static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
506static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
507
508static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
509
510static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
511
512#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
513
514static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
515
516static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
517
518static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
519
520static ID *local_tbl(struct parser_params*);
521
522static VALUE reg_compile(struct parser_params*, VALUE, int);
523static void reg_fragment_setenc(struct parser_params*, VALUE, int);
524static int reg_fragment_check(struct parser_params*, VALUE, int);
525static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
526
527static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
528static NODE *heredoc_dedent(struct parser_params*,NODE*);
529
530static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
531
532#define get_id(id) (id)
533#define get_value(val) (val)
534#define get_num(num) (num)
535#else /* RIPPER */
536#define NODE_RIPPER NODE_CDECL
537
538static inline int ripper_is_node_yylval(VALUE n);
539
540static inline VALUE
541ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
542{
543 if (ripper_is_node_yylval(c)) c = RNODE(c)->nd_cval;
544 add_mark_object(p, b);
545 add_mark_object(p, c);
546 return (VALUE)NEW_CDECL(a, b, c, &NULL_LOC);
547}
548
549static inline int
550ripper_is_node_yylval(VALUE n)
551{
552 return RB_TYPE_P(n, T_NODE) && nd_type(RNODE(n)) == NODE_RIPPER;
553}
554
555#define value_expr(node) ((void)(node))
556#define remove_begin(node) (node)
557#define void_stmts(p,x) (x)
558#define rb_dvar_defined(id, base) 0
559#define rb_local_defined(id, base) 0
560static ID ripper_get_id(VALUE);
561#define get_id(id) ripper_get_id(id)
562static VALUE ripper_get_value(VALUE);
563#define get_value(val) ripper_get_value(val)
564#define get_num(num) (int)get_id(num)
565static VALUE assignable(struct parser_params*,VALUE);
566static int id_is_var(struct parser_params *p, ID id);
567
568#define method_cond(p,node,loc) (node)
569#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
570#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
571#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
572#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
573
574#define new_nil(loc) Qnil
575
576static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
577
578static VALUE const_decl(struct parser_params *p, VALUE path);
579
580static VALUE var_field(struct parser_params *p, VALUE a);
581static VALUE assign_error(struct parser_params *p, VALUE a);
582
583static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
584
585#endif /* !RIPPER */
586
587/* forward declaration */
588typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
589
590RUBY_SYMBOL_EXPORT_BEGIN
591VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
592int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
593enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
594VALUE rb_parser_lex_state_name(enum lex_state_e state);
595void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
596PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
597YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
598YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
599YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
600RUBY_SYMBOL_EXPORT_END
601
602static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
603static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
604static ID formal_argument(struct parser_params*, ID);
605static ID shadowing_lvar(struct parser_params*,ID);
606static void new_bv(struct parser_params*,ID);
607
608static void local_push(struct parser_params*,int);
609static void local_pop(struct parser_params*);
610static void local_var(struct parser_params*, ID);
611static void arg_var(struct parser_params*, ID);
612static int local_id(struct parser_params *p, ID id);
613static int local_id_ref(struct parser_params*, ID, ID **);
614#ifndef RIPPER
615static ID internal_id(struct parser_params*);
616#endif
617
618static const struct vtable *dyna_push(struct parser_params *);
619static void dyna_pop(struct parser_params*, const struct vtable *);
620static int dyna_in_block(struct parser_params*);
621#define dyna_var(p, id) local_var(p, id)
622static int dvar_defined(struct parser_params*, ID);
623static int dvar_defined_ref(struct parser_params*, ID, ID**);
624static int dvar_curr(struct parser_params*,ID);
625
626static int lvar_defined(struct parser_params*, ID);
627
628static NODE *numparam_push(struct parser_params *p);
629static void numparam_pop(struct parser_params *p, NODE *prev_inner);
630
631#ifdef RIPPER
632# define METHOD_NOT idNOT
633#else
634# define METHOD_NOT '!'
635#endif
636
637#define idFWD_REST '*'
638#ifdef RUBY3_KEYWORDS
639#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
640#else
641#define idFWD_KWREST 0
642#endif
643#define idFWD_BLOCK '&'
644
645#define RE_OPTION_ONCE (1<<16)
646#define RE_OPTION_ENCODING_SHIFT 8
647#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
648#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
649#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
650#define RE_OPTION_MASK 0xff
651#define RE_OPTION_ARG_ENCODING_NONE 32
652
653/* structs for managing terminator of string literal and heredocment */
654typedef struct rb_strterm_literal_struct {
655 union {
656 VALUE dummy;
657 long nest;
658 } u0;
659 union {
660 VALUE dummy;
661 long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
662 } u1;
663 union {
664 VALUE dummy;
665 long paren; /* '(' of `%q(...)` */
666 } u2;
667 union {
668 VALUE dummy;
669 long term; /* ')' of `%q(...)` */
670 } u3;
671} rb_strterm_literal_t;
672
673#define HERETERM_LENGTH_BITS ((SIZEOF_VALUE - 1) * CHAR_BIT - 1)
674
675struct rb_strterm_heredoc_struct {
676 VALUE lastline; /* the string of line that contains `<<"END"` */
677 long offset; /* the column of END in `<<"END"` */
678 int sourceline; /* lineno of the line that contains `<<"END"` */
679 unsigned length /* the length of END in `<<"END"` */
680#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
681 : HERETERM_LENGTH_BITS
682# define HERETERM_LENGTH_MAX ((1U << HERETERM_LENGTH_BITS) - 1)
683#else
684# define HERETERM_LENGTH_MAX UINT_MAX
685#endif
686 ;
687#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
688 unsigned quote: 1;
689 unsigned func: 8;
690#else
691 uint8_t quote;
692 uint8_t func;
693#endif
694};
695STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
696
697#define STRTERM_HEREDOC IMEMO_FL_USER0
698
699struct rb_strterm_struct {
700 VALUE flags;
701 union {
702 rb_strterm_literal_t literal;
703 rb_strterm_heredoc_t heredoc;
704 } u;
705};
706
707#ifndef RIPPER
708void
709rb_strterm_mark(VALUE obj)
710{
711 rb_strterm_t *strterm = (rb_strterm_t*)obj;
712 if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
713 rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
714 rb_gc_mark(heredoc->lastline);
715 }
716}
717#endif
718
719#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
720size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
721
722#define TOKEN2ID(tok) ( \
723 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
724 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
725 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
726 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
727 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
728 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
729 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
730
731/****** Ripper *******/
732
733#ifdef RIPPER
734#define RIPPER_VERSION "0.1.0"
735
736static inline VALUE intern_sym(const char *name);
737
738#include "eventids1.c"
739#include "eventids2.c"
740
741static VALUE ripper_dispatch0(struct parser_params*,ID);
742static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
743static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
744static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
745static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
746static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
747static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
748static void ripper_error(struct parser_params *p);
749
750#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
751#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
752#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
753#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
754#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
755#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
756#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e), (f), (g))
757
758#define yyparse ripper_yyparse
759
760#define ID2VAL(id) STATIC_ID2SYM(id)
761#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
762#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
763
764#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
765 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
766
767#define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
768
769static inline VALUE
770new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
771{
772 NODE *t = (NODE *)tail;
773 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value, block = t->u3.value;
774 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, escape_Qundef(block));
775}
776
777static inline VALUE
778new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
779{
780 NODE *t = rb_node_newnode(NODE_ARGS_AUX, kw_args, kw_rest_arg, block, &NULL_LOC);
781 add_mark_object(p, kw_args);
782 add_mark_object(p, kw_rest_arg);
783 add_mark_object(p, block);
784 return (VALUE)t;
785}
786
787static inline VALUE
788args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
789{
790 return args;
791}
792
793static VALUE
794new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
795{
796 NODE *t = (NODE *)aryptn;
797 struct rb_ary_pattern_info *apinfo = t->nd_apinfo;
798 VALUE pre_args = Qnil, rest_arg = Qnil, post_args = Qnil;
799
800 if (apinfo) {
801 pre_args = rb_ary_entry(apinfo->imemo, 0);
802 rest_arg = rb_ary_entry(apinfo->imemo, 1);
803 post_args = rb_ary_entry(apinfo->imemo, 2);
804 }
805
806 if (!NIL_P(pre_arg)) {
807 if (!NIL_P(pre_args)) {
808 rb_ary_unshift(pre_args, pre_arg);
809 }
810 else {
811 pre_args = rb_ary_new_from_args(1, pre_arg);
812 }
813 }
814 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
815}
816
817static VALUE
818new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
819{
820 NODE *t;
821 struct rb_ary_pattern_info *apinfo;
822
823 if (has_rest) {
824 rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
825 }
826 else {
827 rest_arg = Qnil;
828 }
829
830 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
831 apinfo = ZALLOC(struct rb_ary_pattern_info);
832 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
833 apinfo->imemo = rb_ary_new_from_args(4, pre_args, rest_arg, post_args, tmpbuf);
834
835 t = rb_node_newnode(NODE_ARYPTN, Qnil, Qnil, (VALUE)apinfo, &NULL_LOC);
836 RB_OBJ_WRITTEN(p->ast, Qnil, apinfo->imemo);
837
838 return (VALUE)t;
839}
840
841#define new_hash(p,h,l) rb_ary_new_from_args(0)
842
843static VALUE
844new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
845{
846 return ary;
847}
848
849static VALUE
850new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
851{
852 NODE *t = (NODE *)hshptn;
853 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value;
854 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
855}
856
857static VALUE
858new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
859{
860 NODE *t;
861 if (kw_rest_arg) {
862 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
863 }
864 else {
865 kw_rest_arg = Qnil;
866 }
867 t = rb_node_newnode(NODE_HSHPTN, kw_args, kw_rest_arg, 0, &NULL_LOC);
868
869 add_mark_object(p, kw_args);
870 add_mark_object(p, kw_rest_arg);
871 return (VALUE)t;
872}
873
874#define new_defined(p,expr,loc) dispatch1(defined, (expr))
875
876static VALUE heredoc_dedent(struct parser_params*,VALUE);
877
878#else
879#define ID2VAL(id) (id)
880#define TOKEN2VAL(t) ID2VAL(t)
881#define KWD2EID(t, v) keyword_##t
882#endif /* RIPPER */
883
884#ifndef RIPPER
885# define Qnone 0
886# define Qnull 0
887# define ifndef_ripper(x) (x)
888#else
889# define Qnone Qnil
890# define Qnull Qundef
891# define ifndef_ripper(x)
892#endif
893
894# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
895# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
896# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
897# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
898# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
899# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
900# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
901# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
902# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
903# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
904# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
905# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
906# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
907# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
908# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
909# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
910# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
911# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
912# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
913# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
914#ifdef RIPPER
915static ID id_warn, id_warning, id_gets, id_assoc;
916# define WARN_S_L(s,l) STR_NEW(s,l)
917# define WARN_S(s) STR_NEW2(s)
918# define WARN_I(i) INT2NUM(i)
919# define WARN_ID(i) rb_id2str(i)
920# define WARN_IVAL(i) i
921# define PRIsWARN "s"
922# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
923# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
924# ifdef HAVE_VA_ARGS_MACRO
925# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
926# else
927# define WARN_CALL rb_funcall
928# endif
929# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
930# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
931# ifdef HAVE_VA_ARGS_MACRO
932# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
933# else
934# define WARNING_CALL rb_funcall
935# endif
936PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
937# define compile_error ripper_compile_error
938#else
939# define WARN_S_L(s,l) s
940# define WARN_S(s) s
941# define WARN_I(i) i
942# define WARN_ID(i) rb_id2name(i)
943# define WARN_IVAL(i) NUM2INT(i)
944# define PRIsWARN PRIsVALUE
945# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
946# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
947# define WARN_CALL rb_compile_warn
948# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
949# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
950# define WARNING_CALL rb_compile_warning
951PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
952# define compile_error parser_compile_error
953#endif
954
955static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
956static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
957static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
958static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
959
960#define WARN_EOL(tok) \
961 (looking_at_eol_p(p) ? \
962 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
963 (void)0)
964static int looking_at_eol_p(struct parser_params *p);
965%}
966
967%expect 0
968%define api.pure
969%define parse.error verbose
970%printer {
971#ifndef RIPPER
972 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
973#else
974 rb_parser_printf(p, "%"PRIsVALUE, RNODE($$)->nd_rval);
975#endif
976} tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN
977%printer {
978#ifndef RIPPER
979 rb_parser_printf(p, "%+"PRIsVALUE, $$->nd_lit);
980#else
981 rb_parser_printf(p, "%+"PRIsVALUE, get_value($$));
982#endif
983} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
984%printer {
985#ifndef RIPPER
986 rb_parser_printf(p, "$%ld", $$->nd_nth);
987#else
988 rb_parser_printf(p, "%"PRIsVALUE, $$);
989#endif
990} tNTH_REF
991%printer {
992#ifndef RIPPER
993 rb_parser_printf(p, "$%c", (int)$$->nd_nth);
994#else
995 rb_parser_printf(p, "%"PRIsVALUE, $$);
996#endif
997} tBACK_REF
998
999%lex-param {struct parser_params *p}
1000%parse-param {struct parser_params *p}
1001%initial-action
1002{
1003 RUBY_SET_YYLLOC_OF_NONE(@$);
1004};
1005
1006%union {
1007 VALUE val;
1008 NODE *node;
1009 ID id;
1010 int num;
1011 st_table *tbl;
1012 const struct vtable *vars;
1013 struct rb_strterm_struct *strterm;
1014}
1015
1016%token <val>
1017 keyword_class "`class'"
1018 keyword_module "`module'"
1019 keyword_def "`def'"
1020 keyword_undef "`undef'"
1021 keyword_begin "`begin'"
1022 keyword_rescue "`rescue'"
1023 keyword_ensure "`ensure'"
1024 keyword_end "`end'"
1025 keyword_if "`if'"
1026 keyword_unless "`unless'"
1027 keyword_then "`then'"
1028 keyword_elsif "`elsif'"
1029 keyword_else "`else'"
1030 keyword_case "`case'"
1031 keyword_when "`when'"
1032 keyword_while "`while'"
1033 keyword_until "`until'"
1034 keyword_for "`for'"
1035 keyword_break "`break'"
1036 keyword_next "`next'"
1037 keyword_redo "`redo'"
1038 keyword_retry "`retry'"
1039 keyword_in "`in'"
1040 keyword_do "`do'"
1041 keyword_do_cond "`do' for condition"
1042 keyword_do_block "`do' for block"
1043 keyword_do_LAMBDA "`do' for lambda"
1044 keyword_return "`return'"
1045 keyword_yield "`yield'"
1046 keyword_super "`super'"
1047 keyword_self "`self'"
1048 keyword_nil "`nil'"
1049 keyword_true "`true'"
1050 keyword_false "`false'"
1051 keyword_and "`and'"
1052 keyword_or "`or'"
1053 keyword_not "`not'"
1054 modifier_if "`if' modifier"
1055 modifier_unless "`unless' modifier"
1056 modifier_while "`while' modifier"
1057 modifier_until "`until' modifier"
1058 modifier_rescue "`rescue' modifier"
1059 keyword_alias "`alias'"
1060 keyword_defined "`defined?'"
1061 keyword_BEGIN "`BEGIN'"
1062 keyword_END "`END'"
1063 keyword__LINE__ "`__LINE__'"
1064 keyword__FILE__ "`__FILE__'"
1065 keyword__ENCODING__ "`__ENCODING__'"
1066
1067%token <val> tIDENTIFIER "local variable or method"
1068%token <val> tFID "method"
1069%token <val> tGVAR "global variable"
1070%token <val> tIVAR "instance variable"
1071%token <val> tCONSTANT "constant"
1072%token <val> tCVAR "class variable"
1073%token <val> tLABEL
1074%token <val> tINTEGER "integer literal"
1075%token <val> tFLOAT "float literal"
1076%token <val> tRATIONAL "rational literal"
1077%token <val> tIMAGINARY "imaginary literal"
1078%token <val> tCHAR "char literal"
1079%token <val> tNTH_REF "numbered reference"
1080%token <val> tBACK_REF "back reference"
1081%token <val> tSTRING_CONTENT "literal content"
1082%token <val> tREGEXP_END
1083
1084%type <val> singleton strings string string1 xstring regexp
1085%type <val> string_contents xstring_contents regexp_contents string_content
1086%type <val> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1087%type <val> literal numeric simple_numeric ssym dsym symbol cpath
1088%type <val> top_compstmt top_stmts top_stmt begin_block
1089%type <val> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1090%type <val> expr_value expr_value_do arg_value primary_value fcall rel_expr
1091%type <val> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1092%type <val> args call_args opt_call_args
1093%type <val> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1094%type <val> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1095%type <val> command_rhs arg_rhs
1096%type <val> command_asgn mrhs mrhs_arg superclass block_call block_command
1097%type <val> f_block_optarg f_block_opt
1098%type <val> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs f_rest_marg
1099%type <val> assoc_list assocs assoc undef_list backref string_dvar for_var
1100%type <val> block_param opt_block_param block_param_def f_opt
1101%type <val> f_kwarg f_kw f_block_kwarg f_block_kw
1102%type <val> bv_decls opt_bv_decl bvar
1103%type <val> lambda f_larglist lambda_body brace_body do_body
1104%type <val> brace_block cmd_brace_block do_block lhs none fitem
1105%type <val> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1106%type <val> p_case_body p_cases p_top_expr p_top_expr_body
1107%type <val> p_expr p_as p_alt p_expr_basic
1108%type <val> p_args p_args_head p_args_tail p_args_post p_arg
1109%type <val> p_value p_primitive p_variable p_var_ref p_const
1110%type <val> p_kwargs p_kwarg p_kw
1111%type <val> keyword_variable user_variable sym operation operation2 operation3
1112%type <val> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1113%type <val> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1114%type <val> p_kwrest p_kwnorest p_kw_label
1115%type <val> f_no_kwarg args_forward
1116%token END_OF_INPUT 0 "end-of-input"
1117%token <val> '.'
1118/* escaped chars, should be ignored otherwise */
1119%token <val> '\\' "backslash"
1120%token tSP "escaped space"
1121%token <val> '\t' "escaped horizontal tab"
1122%token <val> '\f' "escaped form feed"
1123%token <val> '\r' "escaped carriage return"
1124%token <val> '\13' "escaped vertical tab"
1125%token tUPLUS 132 "unary+"
1126%token tUMINUS 133 "unary-"
1127%token tPOW 134 "**"
1128%token tCMP 135 "<=>"
1129%token tEQ 140 "=="
1130%token tEQQ 141 "==="
1131%token tNEQ 142 "!="
1132%token tGEQ 139 ">="
1133%token tLEQ 138 "<="
1134%token tANDOP 148 "&&"
1135%token tOROP 149 "||"
1136%token tMATCH 143 "=~"
1137%token tNMATCH 144 "!~"
1138%token tDOT2 128 ".."
1139%token tDOT3 129 "..."
1140%token tBDOT2 130 "(.."
1141%token tBDOT3 131 "(..."
1142%token tAREF 145 "[]"
1143%token tASET 146 "[]="
1144%token tLSHFT 136 "<<"
1145%token tRSHFT 137 ">>"
1146%token <val> tANDDOT 150 "&."
1147%token <val> tCOLON2 147 "::"
1148%token tCOLON3 ":: at EXPR_BEG"
1149%token <val> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1150%token tASSOC "=>"
1151%token tLPAREN "("
1152%token tLPAREN_ARG "( arg"
1153%token tRPAREN ")"
1154%token tLBRACK "["
1155%token tLBRACE "{"
1156%token tLBRACE_ARG "{ arg"
1157%token tSTAR "*"
1158%token tDSTAR "**arg"
1159%token tAMPER "&"
1160%token tLAMBDA "->"
1161%token tSYMBEG "symbol literal"
1162%token tSTRING_BEG "string literal"
1163%token tXSTRING_BEG "backtick literal"
1164%token tREGEXP_BEG "regexp literal"
1165%token tWORDS_BEG "word list"
1166%token tQWORDS_BEG "verbatim word list"
1167%token tSYMBOLS_BEG "symbol list"
1168%token tQSYMBOLS_BEG "verbatim symbol list"
1169%token tSTRING_END "terminator"
1170%token tSTRING_DEND "'}'"
1171%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1172
1173/*
1174 * precedence table
1175 */
1176
1177%nonassoc tLOWEST
1178%nonassoc tLBRACE_ARG
1179
1180%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1181%left keyword_or keyword_and
1182%right keyword_not
1183%nonassoc keyword_defined
1184%right '=' tOP_ASGN
1185%left modifier_rescue
1186%right '?' ':'
1187%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1188%left tOROP
1189%left tANDOP
1190%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1191%left '>' tGEQ '<' tLEQ
1192%left '|' '^'
1193%left '&'
1194%left tLSHFT tRSHFT
1195%left '+' '-'
1196%left '*' '/' '%'
1197%right tUMINUS_NUM tUMINUS
1198%right tPOW
1199%right '!' '~' tUPLUS
1200
1201%token tLAST_TOKEN
1202
1203%%
1204program : {
1205 SET_LEX_STATE(EXPR_BEG);
1206 local_push(p, ifndef_ripper(1)+0);
1207 }
1208 top_compstmt
1209 {
1210#if 0
1211 if ($2 && !compile_for_eval) {
1212 NODE *node = $2;
1213 /* last expression should not be void */
1214 if (nd_type(node) == NODE_BLOCK) {
1215 while (node->nd_next) {
1216 node = node->nd_next;
1217 }
1218 node = node->nd_head;
1219 }
1220 node = remove_begin(node);
1221 void_expr(p, node);
1222 }
1223 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1224#endif
1225 {VALUE v1,v2;v1=$2;v2=dispatch1(program,v1);p->result=v2;}
1226 local_pop(p);
1227 }
1228 ;
1229
1230top_compstmt : top_stmts opt_terms
1231 {
1232 $$ = void_stmts(p, $1);
1233 }
1234 ;
1235
1236top_stmts : none
1237 {
1238#if 0
1239 $$ = NEW_BEGIN(0, &@$);
1240#endif
1241 {VALUE v1,v2,v3,v4,v5;v1=dispatch0(stmts_new);v2=dispatch0(void_stmt);v3=v1;v4=v2;v5=dispatch2(stmts_add,v3,v4);$$=v5;}
1242 }
1243 | top_stmt
1244 {
1245#if 0
1246 $$ = newline_node($1);
1247#endif
1248 {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1249 }
1250 | top_stmts terms top_stmt
1251 {
1252#if 0
1253 $$ = block_append(p, $1, newline_node($3));
1254#endif
1255 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1256 }
1257 | error top_stmt
1258 {
1259 $$ = remove_begin($2);
1260 }
1261 ;
1262
1263top_stmt : stmt
1264 | keyword_BEGIN begin_block
1265 {
1266 $$ = $2;
1267 }
1268 ;
1269
1270begin_block : '{' top_compstmt '}'
1271 {
1272#if 0
1273 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1274 NEW_BEGIN($2, &@$));
1275 $$ = NEW_BEGIN(0, &@$);
1276#endif
1277 {VALUE v1,v2;v1=$2;v2=dispatch1(BEGIN,v1);$$=v2;}
1278 }
1279 ;
1280
1281bodystmt : compstmt
1282 opt_rescue
1283 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1284 compstmt
1285 opt_ensure
1286 {
1287#if 0
1288 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1289#endif
1290 {VALUE v1,v2,v3,v4,v5;v1=escape_Qundef($1);v2=escape_Qundef($2);v3=escape_Qundef($5);v4=escape_Qundef($6);v5=dispatch4(bodystmt,v1,v2,v3,v4);$$=v5;}
1291 }
1292 | compstmt
1293 opt_rescue
1294 opt_ensure
1295 {
1296#if 0
1297 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1298#endif
1299 {VALUE v1,v2,v3,v4,v5;v1=escape_Qundef($1);v2=escape_Qundef($2);v3=Qnil;v4=escape_Qundef($3);v5=dispatch4(bodystmt,v1,v2,v3,v4);$$=v5;}
1300 }
1301 ;
1302
1303compstmt : stmts opt_terms
1304 {
1305 $$ = void_stmts(p, $1);
1306 }
1307 ;
1308
1309stmts : none
1310 {
1311#if 0
1312 $$ = NEW_BEGIN(0, &@$);
1313#endif
1314 {VALUE v1,v2,v3,v4,v5;v1=dispatch0(stmts_new);v2=dispatch0(void_stmt);v3=v1;v4=v2;v5=dispatch2(stmts_add,v3,v4);$$=v5;}
1315 }
1316 | stmt_or_begin
1317 {
1318#if 0
1319 $$ = newline_node($1);
1320#endif
1321 {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1322 }
1323 | stmts terms stmt_or_begin
1324 {
1325#if 0
1326 $$ = block_append(p, $1, newline_node($3));
1327#endif
1328 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1329 }
1330 | error stmt
1331 {
1332 $$ = remove_begin($2);
1333 }
1334 ;
1335
1336stmt_or_begin : stmt
1337 {
1338 $$ = $1;
1339 }
1340 | keyword_BEGIN
1341 {
1342 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1343 }
1344 begin_block
1345 {
1346 $$ = $3;
1347 }
1348 ;
1349
1350stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1351 {
1352#if 0
1353 $$ = NEW_ALIAS($2, $4, &@$);
1354#endif
1355 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(alias,v1,v2);$$=v3;}
1356 }
1357 | keyword_alias tGVAR tGVAR
1358 {
1359#if 0
1360 $$ = NEW_VALIAS($2, $3, &@$);
1361#endif
1362 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1363 }
1364 | keyword_alias tGVAR tBACK_REF
1365 {
1366#if 0
1367 char buf[2];
1368 buf[0] = '$';
1369 buf[1] = (char)$3->nd_nth;
1370 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1371#endif
1372 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1373 }
1374 | keyword_alias tGVAR tNTH_REF
1375 {
1376#if 0
1377 yyerror1(&@3, "can't make alias for the number variables");
1378 $$ = NEW_BEGIN(0, &@$);
1379#endif
1380 {VALUE v1,v2,v3,v4,v5;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);v4=v3;v5=dispatch1(alias_error,v4);$$=v5;}ripper_error(p);
1381 }
1382 | keyword_undef undef_list
1383 {
1384#if 0
1385 $$ = $2;
1386#endif
1387 {VALUE v1,v2;v1=$2;v2=dispatch1(undef,v1);$$=v2;}
1388 }
1389 | stmt modifier_if expr_value
1390 {
1391#if 0
1392 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1393 fixpos($$, $3);
1394#endif
1395 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
1396 }
1397 | stmt modifier_unless expr_value
1398 {
1399#if 0
1400 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1401 fixpos($$, $3);
1402#endif
1403 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
1404 }
1405 | stmt modifier_while expr_value
1406 {
1407#if 0
1408 if ($1 && nd_type($1) == NODE_BEGIN) {
1409 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1410 }
1411 else {
1412 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1413 }
1414#endif
1415 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(while_mod,v1,v2);$$=v3;}
1416 }
1417 | stmt modifier_until expr_value
1418 {
1419#if 0
1420 if ($1 && nd_type($1) == NODE_BEGIN) {
1421 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1422 }
1423 else {
1424 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1425 }
1426#endif
1427 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(until_mod,v1,v2);$$=v3;}
1428 }
1429 | stmt modifier_rescue stmt
1430 {
1431#if 0
1432 NODE *resq;
1433 YYLTYPE loc = code_loc_gen(&@2, &@3);
1434 resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1435 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1436#endif
1437 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1438 }
1439 | keyword_END '{' compstmt '}'
1440 {
1441 if (p->in_def) {
1442 rb_warn0("END in method; use at_exit");
1443 }
1444#if 0
1445 {
1446 NODE *scope = NEW_NODE(
1447 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1448 $$ = NEW_POSTEXE(scope, &@$);
1449 }
1450#endif
1451 {VALUE v1,v2;v1=$3;v2=dispatch1(END,v1);$$=v2;}
1452 }
1453 | command_asgn
1454 | mlhs '=' command_call
1455 {
1456#if 0
1457 value_expr($3);
1458 $$ = node_assign(p, $1, $3, &@$);
1459#endif
1460 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(massign,v1,v2);$$=v3;}
1461 }
1462 | lhs '=' mrhs
1463 {
1464#if 0
1465 value_expr($3);
1466 $$ = node_assign(p, $1, $3, &@$);
1467#endif
1468 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
1469 }
1470 | mlhs '=' mrhs_arg modifier_rescue stmt
1471 {
1472#if 0
1473 YYLTYPE loc = code_loc_gen(&@4, &@5);
1474 value_expr($3);
1475 $$ = node_assign(p, $1, NEW_RESCUE($3, NEW_RESBODY(0, remove_begin($5), 0, &loc), 0, &@$), &@$);
1476#endif
1477 {VALUE v1,v2,v3,v4,v5,v6;v1=$3;v2=$5;v3=dispatch2(rescue_mod,v1,v2);v4=$1;v5=v3;v6=dispatch2(massign,v4,v5);$$=v6;}
1478 }
1479 | mlhs '=' mrhs_arg
1480 {
1481#if 0
1482 $$ = node_assign(p, $1, $3, &@$);
1483#endif
1484 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(massign,v1,v2);$$=v3;}
1485 }
1486 | expr
1487 ;
1488
1489command_asgn : lhs '=' command_rhs
1490 {
1491#if 0
1492 $$ = node_assign(p, $1, $3, &@$);
1493#endif
1494 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
1495 }
1496 | var_lhs tOP_ASGN command_rhs
1497 {
1498#if 0
1499 $$ = new_op_assign(p, $1, $2, $3, &@$);
1500#endif
1501 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
1502 }
1503 | primary_value '[' opt_call_args rbracket tOP_ASGN command_rhs
1504 {
1505#if 0
1506 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
1507#endif
1508 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);v4=v3;v5=$5;v6=$6;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
1509
1510 }
1511 | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
1512 {
1513#if 0
1514 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1515#endif
1516 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1517 }
1518 | primary_value call_op tCONSTANT tOP_ASGN command_rhs
1519 {
1520#if 0
1521 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1522#endif
1523 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1524 }
1525 | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
1526 {
1527#if 0
1528 YYLTYPE loc = code_loc_gen(&@1, &@3);
1529 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
1530#endif
1531 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);v4=v3;v5=$4;v6=$5;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
1532 }
1533 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
1534 {
1535#if 0
1536 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
1537#endif
1538 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1539 }
1540 | backref tOP_ASGN command_rhs
1541 {
1542#if 0
1543 rb_backref_error(p, $1);
1544 $$ = NEW_BEGIN(0, &@$);
1545#endif
1546 {VALUE v1,v2,v3,v4,v5;v1=var_field(p, $1);v2=$3;v3=dispatch2(assign,v1,v2);v4=v3;v5=dispatch1(assign_error,v4);$$=v5;}ripper_error(p);
1547 }
1548 ;
1549
1550command_rhs : command_call %prec tOP_ASGN
1551 {
1552 value_expr($1);
1553 $$ = $1;
1554 }
1555 | command_call modifier_rescue stmt
1556 {
1557#if 0
1558 YYLTYPE loc = code_loc_gen(&@2, &@3);
1559 value_expr($1);
1560 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1561#endif
1562 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1563 }
1564 | command_asgn
1565 ;
1566
1567expr : command_call
1568 | expr keyword_and expr
1569 {
1570 $$ = logop(p, idAND, $1, $3, &@2, &@$);
1571 }
1572 | expr keyword_or expr
1573 {
1574 $$ = logop(p, idOR, $1, $3, &@2, &@$);
1575 }
1576 | keyword_not opt_nl expr
1577 {
1578 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
1579 }
1580 | '!' command_call
1581 {
1582 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
1583 }
1584 | arg keyword_in
1585 {
1586 value_expr($1);
1587 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1588 p->command_start = FALSE;
1589 $<num>$ = p->in_kwarg;
1590 p->in_kwarg = 1;
1591 }
1592 {$<tbl>$ = push_pvtbl(p);}
1593 p_expr
1594 {pop_pvtbl(p, $<tbl>4);}
1595 {
1596 p->in_kwarg = !!$<num>3;
1597#if 0
1598 $$ = new_case3(p, $1, NEW_IN($5, 0, 0, &@5), &@$);
1599#endif
1600 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$5;v2=Qnil;v3=Qnil;v4=dispatch3(in,v1,v2,v3);v5=$1;v6=v4;v7=dispatch2(case,v5,v6);$$=v7;}
1601 }
1602 | arg %prec tLBRACE_ARG
1603 ;
1604
1605expr_value : expr
1606 {
1607 value_expr($1);
1608 $$ = $1;
1609 }
1610 ;
1611
1612expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1613 {
1614 $$ = $2;
1615 }
1616
1617
1618command_call : command
1619 | block_command
1620 ;
1621
1622block_command : block_call
1623 | block_call call_op2 operation2 command_args
1624 {
1625#if 0
1626 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
1627#endif
1628 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
1629 }
1630 ;
1631
1632cmd_brace_block : tLBRACE_ARG brace_body '}'
1633 {
1634 $$ = $2;
1635#if 0
1636 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
1637 nd_set_line($$, @1.end_pos.lineno);
1638#endif
1639 }
1640 ;
1641
1642fcall : operation
1643 {
1644#if 0
1645 $$ = NEW_FCALL($1, 0, &@$);
1646 nd_set_line($$, p->tokline);
1647#endif
1648 $$=$1;
1649 }
1650 ;
1651
1652command : fcall command_args %prec tLOWEST
1653 {
1654#if 0
1655 $1->nd_args = $2;
1656 nd_set_last_loc($1, @2.end_pos);
1657 $$ = $1;
1658#endif
1659 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(command,v1,v2);$$=v3;}
1660 }
1661 | fcall command_args cmd_brace_block
1662 {
1663#if 0
1664 block_dup_check(p, $2, $3);
1665 $1->nd_args = $2;
1666 $$ = method_add_block(p, $1, $3, &@$);
1667 fixpos($$, $1);
1668 nd_set_last_loc($1, @2.end_pos);
1669#endif
1670 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=$2;v3=dispatch2(command,v1,v2);v4=v3;v5=$3;v6=dispatch2(method_add_block,v4,v5);$$=v6;}
1671 }
1672 | primary_value call_op operation2 command_args %prec tLOWEST
1673 {
1674#if 0
1675 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
1676#endif
1677 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1678 }
1679 | primary_value call_op operation2 command_args cmd_brace_block
1680 {
1681#if 0
1682 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
1683#endif
1684 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=dispatch2(method_add_block,v6,v7);$$=v8;}
1685 }
1686 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1687 {
1688#if 0
1689 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
1690#endif
1691 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1692 }
1693 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1694 {
1695#if 0
1696 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
1697#endif
1698 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=dispatch2(method_add_block,v6,v7);$$=v8;}
1699 }
1700 | keyword_super command_args
1701 {
1702#if 0
1703 $$ = NEW_SUPER($2, &@$);
1704 fixpos($$, $2);
1705#endif
1706 {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
1707 }
1708 | keyword_yield command_args
1709 {
1710#if 0
1711 $$ = new_yield(p, $2, &@$);
1712 fixpos($$, $2);
1713#endif
1714 {VALUE v1,v2;v1=$2;v2=dispatch1(yield,v1);$$=v2;}
1715 }
1716 | k_return call_args
1717 {
1718#if 0
1719 $$ = NEW_RETURN(ret_args(p, $2), &@$);
1720#endif
1721 {VALUE v1,v2;v1=$2;v2=dispatch1(return,v1);$$=v2;}
1722 }
1723 | keyword_break call_args
1724 {
1725#if 0
1726 $$ = NEW_BREAK(ret_args(p, $2), &@$);
1727#endif
1728 {VALUE v1,v2;v1=$2;v2=dispatch1(break,v1);$$=v2;}
1729 }
1730 | keyword_next call_args
1731 {
1732#if 0
1733 $$ = NEW_NEXT(ret_args(p, $2), &@$);
1734#endif
1735 {VALUE v1,v2;v1=$2;v2=dispatch1(next,v1);$$=v2;}
1736 }
1737 ;
1738
1739mlhs : mlhs_basic
1740 | tLPAREN mlhs_inner rparen
1741 {
1742#if 0
1743 $$ = $2;
1744#endif
1745 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1746 }
1747 ;
1748
1749mlhs_inner : mlhs_basic
1750 | tLPAREN mlhs_inner rparen
1751 {
1752#if 0
1753 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1754#endif
1755 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1756 }
1757 ;
1758
1759mlhs_basic : mlhs_head
1760 {
1761#if 0
1762 $$ = NEW_MASGN($1, 0, &@$);
1763#endif
1764 $$=$1;
1765 }
1766 | mlhs_head mlhs_item
1767 {
1768#if 0
1769 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
1770#endif
1771 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1772 }
1773 | mlhs_head tSTAR mlhs_node
1774 {
1775#if 0
1776 $$ = NEW_MASGN($1, $3, &@$);
1777#endif
1778 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1779 }
1780 | mlhs_head tSTAR mlhs_node ',' mlhs_post
1781 {
1782#if 0
1783 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
1784#endif
1785 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);v4=v3;v5=$5;v6=dispatch2(mlhs_add_post,v4,v5);$$=v6;}
1786 }
1787 | mlhs_head tSTAR
1788 {
1789#if 0
1790 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
1791#endif
1792 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1793 }
1794 | mlhs_head tSTAR ',' mlhs_post
1795 {
1796#if 0
1797 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
1798#endif
1799 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=Qnil;v3=dispatch2(mlhs_add_star,v1,v2);v4=v3;v5=$4;v6=dispatch2(mlhs_add_post,v4,v5);$$=v6;}
1800 }
1801 | tSTAR mlhs_node
1802 {
1803#if 0
1804 $$ = NEW_MASGN(0, $2, &@$);
1805#endif
1806 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$2;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1807 }
1808 | tSTAR mlhs_node ',' mlhs_post
1809 {
1810#if 0
1811 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
1812#endif
1813 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=dispatch0(mlhs_new);v2=v1;v3=$2;v4=dispatch2(mlhs_add_star,v2,v3);v5=v4;v6=$4;v7=dispatch2(mlhs_add_post,v5,v6);$$=v7;}
1814 }
1815 | tSTAR
1816 {
1817#if 0
1818 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
1819#endif
1820 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=Qnil;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1821 }
1822 | tSTAR ',' mlhs_post
1823 {
1824#if 0
1825 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
1826#endif
1827 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=dispatch0(mlhs_new);v2=v1;v3=Qnil;v4=dispatch2(mlhs_add_star,v2,v3);v5=v4;v6=$3;v7=dispatch2(mlhs_add_post,v5,v6);$$=v7;}
1828 }
1829 ;
1830
1831mlhs_item : mlhs_node
1832 | tLPAREN mlhs_inner rparen
1833 {
1834#if 0
1835 $$ = $2;
1836#endif
1837 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1838 }
1839 ;
1840
1841mlhs_head : mlhs_item ','
1842 {
1843#if 0
1844 $$ = NEW_LIST($1, &@1);
1845#endif
1846 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
1847 }
1848 | mlhs_head mlhs_item ','
1849 {
1850#if 0
1851 $$ = list_append(p, $1, $2);
1852#endif
1853 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1854 }
1855 ;
1856
1857mlhs_post : mlhs_item
1858 {
1859#if 0
1860 $$ = NEW_LIST($1, &@$);
1861#endif
1862 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
1863 }
1864 | mlhs_post ',' mlhs_item
1865 {
1866#if 0
1867 $$ = list_append(p, $1, $3);
1868#endif
1869 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1870 }
1871 ;
1872
1873mlhs_node : user_variable
1874 {
1875#if 0
1876 $$ = assignable(p, $1, 0, &@$);
1877#endif
1878 $$=assignable(p, var_field(p, $1));
1879 }
1880 | keyword_variable
1881 {
1882#if 0
1883 $$ = assignable(p, $1, 0, &@$);
1884#endif
1885 $$=assignable(p, var_field(p, $1));
1886 }
1887 | primary_value '[' opt_call_args rbracket
1888 {
1889#if 0
1890 $$ = aryset(p, $1, $3, &@$);
1891#endif
1892 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
1893 }
1894 | primary_value call_op tIDENTIFIER
1895 {
1896 if ($2 == tANDDOT) {
1897 yyerror1(&@2, "&. inside multiple assignment destination");
1898 }
1899#if 0
1900 $$ = attrset(p, $1, $2, $3, &@$);
1901#endif
1902 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1903 }
1904 | primary_value tCOLON2 tIDENTIFIER
1905 {
1906#if 0
1907 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1908#endif
1909 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=v3;}
1910 }
1911 | primary_value call_op tCONSTANT
1912 {
1913 if ($2 == tANDDOT) {
1914 yyerror1(&@2, "&. inside multiple assignment destination");
1915 }
1916#if 0
1917 $$ = attrset(p, $1, $2, $3, &@$);
1918#endif
1919 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1920 }
1921 | primary_value tCOLON2 tCONSTANT
1922 {
1923#if 0
1924 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1925#endif
1926 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
1927 }
1928 | tCOLON3 tCONSTANT
1929 {
1930#if 0
1931 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1932#endif
1933 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
1934 }
1935 | backref
1936 {
1937#if 0
1938 rb_backref_error(p, $1);
1939 $$ = NEW_BEGIN(0, &@$);
1940#endif
1941 {VALUE v1,v2;v1=var_field(p, $1);v2=dispatch1(assign_error,v1);$$=v2;}ripper_error(p);
1942 }
1943 ;
1944
1945lhs : user_variable
1946 {
1947#if 0
1948 $$ = assignable(p, $1, 0, &@$);
1949#endif
1950 $$=assignable(p, var_field(p, $1));
1951 }
1952 | keyword_variable
1953 {
1954#if 0
1955 $$ = assignable(p, $1, 0, &@$);
1956#endif
1957 $$=assignable(p, var_field(p, $1));
1958 }
1959 | primary_value '[' opt_call_args rbracket
1960 {
1961#if 0
1962 $$ = aryset(p, $1, $3, &@$);
1963#endif
1964 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
1965 }
1966 | primary_value call_op tIDENTIFIER
1967 {
1968#if 0
1969 $$ = attrset(p, $1, $2, $3, &@$);
1970#endif
1971 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1972 }
1973 | primary_value tCOLON2 tIDENTIFIER
1974 {
1975#if 0
1976 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1977#endif
1978 {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1979 }
1980 | primary_value call_op tCONSTANT
1981 {
1982#if 0
1983 $$ = attrset(p, $1, $2, $3, &@$);
1984#endif
1985 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1986 }
1987 | primary_value tCOLON2 tCONSTANT
1988 {
1989#if 0
1990 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1991#endif
1992 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
1993 }
1994 | tCOLON3 tCONSTANT
1995 {
1996#if 0
1997 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1998#endif
1999 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
2000 }
2001 | backref
2002 {
2003#if 0
2004 rb_backref_error(p, $1);
2005 $$ = NEW_BEGIN(0, &@$);
2006#endif
2007 {VALUE v1,v2;v1=var_field(p, $1);v2=dispatch1(assign_error,v1);$$=v2;}ripper_error(p);
2008 }
2009 ;
2010
2011cname : tIDENTIFIER
2012 {
2013#if 0
2014 yyerror1(&@1, "class/module name must be CONSTANT");
2015#endif
2016 {VALUE v1,v2;v1=$1;v2=dispatch1(class_name_error,v1);$$=v2;}ripper_error(p);
2017 }
2018 | tCONSTANT
2019 ;
2020
2021cpath : tCOLON3 cname
2022 {
2023#if 0
2024 $$ = NEW_COLON3($2, &@$);
2025#endif
2026 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2027 }
2028 | cname
2029 {
2030#if 0
2031 $$ = NEW_COLON2(0, $$, &@$);
2032#endif
2033 {VALUE v1,v2;v1=$1;v2=dispatch1(const_ref,v1);$$=v2;}
2034 }
2035 | primary_value tCOLON2 cname
2036 {
2037#if 0
2038 $$ = NEW_COLON2($1, $3, &@$);
2039#endif
2040 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2041 }
2042 ;
2043
2044fname : tIDENTIFIER
2045 | tCONSTANT
2046 | tFID
2047 | op
2048 {
2049 SET_LEX_STATE(EXPR_ENDFN);
2050 $$ = $1;
2051 }
2052 | reswords
2053 ;
2054
2055fitem : fname
2056 {
2057#if 0
2058 $$ = NEW_LIT(ID2SYM($1), &@$);
2059#endif
2060 {VALUE v1,v2;v1=$1;v2=dispatch1(symbol_literal,v1);$$=v2;}
2061 }
2062 | symbol
2063 ;
2064
2065undef_list : fitem
2066 {
2067#if 0
2068 $$ = NEW_UNDEF($1, &@$);
2069#endif
2070 $$=rb_ary_new3(1, get_value($1));
2071 }
2072 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2073 {
2074#if 0
2075 NODE *undef = NEW_UNDEF($4, &@4);
2076 $$ = block_append(p, $1, undef);
2077#endif
2078 $$=rb_ary_push($1, get_value($4));
2079 }
2080 ;
2081
2082op : '|' { ifndef_ripper($$ = '|'); }
2083 | '^' { ifndef_ripper($$ = '^'); }
2084 | '&' { ifndef_ripper($$ = '&'); }
2085 | tCMP { ifndef_ripper($$ = tCMP); }
2086 | tEQ { ifndef_ripper($$ = tEQ); }
2087 | tEQQ { ifndef_ripper($$ = tEQQ); }
2088 | tMATCH { ifndef_ripper($$ = tMATCH); }
2089 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2090 | '>' { ifndef_ripper($$ = '>'); }
2091 | tGEQ { ifndef_ripper($$ = tGEQ); }
2092 | '<' { ifndef_ripper($$ = '<'); }
2093 | tLEQ { ifndef_ripper($$ = tLEQ); }
2094 | tNEQ { ifndef_ripper($$ = tNEQ); }
2095 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2096 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2097 | '+' { ifndef_ripper($$ = '+'); }
2098 | '-' { ifndef_ripper($$ = '-'); }
2099 | '*' { ifndef_ripper($$ = '*'); }
2100 | tSTAR { ifndef_ripper($$ = '*'); }
2101 | '/' { ifndef_ripper($$ = '/'); }
2102 | '%' { ifndef_ripper($$ = '%'); }
2103 | tPOW { ifndef_ripper($$ = tPOW); }
2104 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2105 | '!' { ifndef_ripper($$ = '!'); }
2106 | '~' { ifndef_ripper($$ = '~'); }
2107 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2108 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2109 | tAREF { ifndef_ripper($$ = tAREF); }
2110 | tASET { ifndef_ripper($$ = tASET); }
2111 | '`' { ifndef_ripper($$ = '`'); }
2112 ;
2113
2114reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2115 | keyword_BEGIN | keyword_END
2116 | keyword_alias | keyword_and | keyword_begin
2117 | keyword_break | keyword_case | keyword_class | keyword_def
2118 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2119 | keyword_end | keyword_ensure | keyword_false
2120 | keyword_for | keyword_in | keyword_module | keyword_next
2121 | keyword_nil | keyword_not | keyword_or | keyword_redo
2122 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2123 | keyword_super | keyword_then | keyword_true | keyword_undef
2124 | keyword_when | keyword_yield | keyword_if | keyword_unless
2125 | keyword_while | keyword_until
2126 ;
2127
2128arg : lhs '=' arg_rhs
2129 {
2130#if 0
2131 $$ = node_assign(p, $1, $3, &@$);
2132#endif
2133 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
2134 }
2135 | var_lhs tOP_ASGN arg_rhs
2136 {
2137#if 0
2138 $$ = new_op_assign(p, $1, $2, $3, &@$);
2139#endif
2140 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
2141 }
2142 | primary_value '[' opt_call_args rbracket tOP_ASGN arg_rhs
2143 {
2144#if 0
2145 value_expr($6);
2146 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
2147#endif
2148 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);v4=v3;v5=$5;v6=$6;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
2149 }
2150 | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
2151 {
2152#if 0
2153 value_expr($5);
2154 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2155#endif
2156 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2157 }
2158 | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
2159 {
2160#if 0
2161 value_expr($5);
2162 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2163#endif
2164 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2165 }
2166 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
2167 {
2168#if 0
2169 value_expr($5);
2170 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
2171#endif
2172 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2173 }
2174 | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
2175 {
2176#if 0
2177 YYLTYPE loc = code_loc_gen(&@1, &@3);
2178 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
2179#endif
2180 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);v4=v3;v5=$4;v6=$5;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
2181 }
2182 | tCOLON3 tCONSTANT tOP_ASGN arg_rhs
2183 {
2184#if 0
2185 $$ = new_const_op_assign(p, NEW_COLON3($2, &@$), $3, $4, &@$);
2186#endif
2187 {VALUE v1,v2,v3,v4,v5,v6;v1=$2;v2=dispatch1(top_const_field,v1);v3=v2;v4=$3;v5=$4;v6=dispatch3(opassign,v3,v4,v5);$$=v6;}
2188 }
2189 | backref tOP_ASGN arg_rhs
2190 {
2191#if 0
2192 rb_backref_error(p, $1);
2193 $$ = NEW_BEGIN(0, &@$);
2194#endif
2195 {VALUE v1,v2,v3,v4,v5,v6;v1=var_field(p, $1);v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);v5=v4;v6=dispatch1(assign_error,v5);$$=v6;}ripper_error(p);
2196 }
2197 | arg tDOT2 arg
2198 {
2199#if 0
2200 value_expr($1);
2201 value_expr($3);
2202 $$ = NEW_DOT2($1, $3, &@$);
2203#endif
2204 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
2205 }
2206 | arg tDOT3 arg
2207 {
2208#if 0
2209 value_expr($1);
2210 value_expr($3);
2211 $$ = NEW_DOT3($1, $3, &@$);
2212#endif
2213 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
2214 }
2215 | arg tDOT2
2216 {
2217#if 0
2218 YYLTYPE loc;
2219 loc.beg_pos = @2.end_pos;
2220 loc.end_pos = @2.end_pos;
2221
2222 value_expr($1);
2223 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
2224#endif
2225 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
2226 }
2227 | arg tDOT3
2228 {
2229#if 0
2230 YYLTYPE loc;
2231 loc.beg_pos = @2.end_pos;
2232 loc.end_pos = @2.end_pos;
2233
2234 value_expr($1);
2235 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
2236#endif
2237 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
2238 }
2239 | tBDOT2 arg
2240 {
2241#if 0
2242 YYLTYPE loc;
2243 loc.beg_pos = @1.beg_pos;
2244 loc.end_pos = @1.beg_pos;
2245
2246 value_expr($2);
2247 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
2248#endif
2249 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
2250 }
2251 | tBDOT3 arg
2252 {
2253#if 0
2254 YYLTYPE loc;
2255 loc.beg_pos = @1.beg_pos;
2256 loc.end_pos = @1.beg_pos;
2257
2258 value_expr($2);
2259 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
2260#endif
2261 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
2262 }
2263 | arg '+' arg
2264 {
2265 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2266 }
2267 | arg '-' arg
2268 {
2269 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2270 }
2271 | arg '*' arg
2272 {
2273 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2274 }
2275 | arg '/' arg
2276 {
2277 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2278 }
2279 | arg '%' arg
2280 {
2281 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2282 }
2283 | arg tPOW arg
2284 {
2285 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2286 }
2287 | tUMINUS_NUM simple_numeric tPOW arg
2288 {
2289 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2290 }
2291 | tUPLUS arg
2292 {
2293 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2294 }
2295 | tUMINUS arg
2296 {
2297 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2298 }
2299 | arg '|' arg
2300 {
2301 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2302 }
2303 | arg '^' arg
2304 {
2305 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2306 }
2307 | arg '&' arg
2308 {
2309 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2310 }
2311 | arg tCMP arg
2312 {
2313 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2314 }
2315 | rel_expr %prec tCMP
2316 | arg tEQ arg
2317 {
2318 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2319 }
2320 | arg tEQQ arg
2321 {
2322 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2323 }
2324 | arg tNEQ arg
2325 {
2326 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2327 }
2328 | arg tMATCH arg
2329 {
2330 $$ = match_op(p, $1, $3, &@2, &@$);
2331 }
2332 | arg tNMATCH arg
2333 {
2334 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2335 }
2336 | '!' arg
2337 {
2338 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2339 }
2340 | '~' arg
2341 {
2342 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2343 }
2344 | arg tLSHFT arg
2345 {
2346 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2347 }
2348 | arg tRSHFT arg
2349 {
2350 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2351 }
2352 | arg tANDOP arg
2353 {
2354 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2355 }
2356 | arg tOROP arg
2357 {
2358 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2359 }
2360 | keyword_defined opt_nl {p->in_defined = 1;} arg
2361 {
2362 p->in_defined = 0;
2363 $$ = new_defined(p, $4, &@$);
2364 }
2365 | arg '?' arg opt_nl ':' arg
2366 {
2367#if 0
2368 value_expr($1);
2369 $$ = new_if(p, $1, $3, $6, &@$);
2370 fixpos($$, $1);
2371#endif
2372 {VALUE v1,v2,v3,v4;v1=$1;v2=$3;v3=$6;v4=dispatch3(ifop,v1,v2,v3);$$=v4;}
2373 }
2374 | primary
2375 {
2376 $$ = $1;
2377 }
2378 ;
2379
2380relop : '>' {$$ = '>';}
2381 | '<' {$$ = '<';}
2382 | tGEQ {$$ = idGE;}
2383 | tLEQ {$$ = idLE;}
2384 ;
2385
2386rel_expr : arg relop arg %prec '>'
2387 {
2388 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2389 }
2390 | rel_expr relop arg %prec '>'
2391 {
2392 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2393 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2394 }
2395 ;
2396
2397arg_value : arg
2398 {
2399 value_expr($1);
2400 $$ = $1;
2401 }
2402 ;
2403
2404aref_args : none
2405 | args trailer
2406 {
2407 $$ = $1;
2408 }
2409 | args ',' assocs trailer
2410 {
2411#if 0
2412 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2413#endif
2414 {VALUE v1,v2,v3,v4,v5;v1=$3;v2=dispatch1(bare_assoc_hash,v1);v3=$1;v4=v2;v5=dispatch2(args_add,v3,v4);$$=v5;}
2415 }
2416 | assocs trailer
2417 {
2418#if 0
2419 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2420#endif
2421 {VALUE v1,v2,v3,v4,v5,v6;v1=dispatch0(args_new);v2=$1;v3=dispatch1(bare_assoc_hash,v2);v4=v1;v5=v3;v6=dispatch2(args_add,v4,v5);$$=v6;}
2422 }
2423 ;
2424
2425arg_rhs : arg %prec tOP_ASGN
2426 {
2427 value_expr($1);
2428 $$ = $1;
2429 }
2430 | arg modifier_rescue arg
2431 {
2432#if 0
2433 YYLTYPE loc = code_loc_gen(&@2, &@3);
2434 value_expr($1);
2435 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
2436#endif
2437 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
2438 }
2439 ;
2440
2441paren_args : '(' opt_call_args rparen
2442 {
2443#if 0
2444 $$ = $2;
2445#endif
2446 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(arg_paren,v1);$$=v2;}
2447 }
2448 | '(' args ',' args_forward rparen
2449 {
2450 if (!local_id(p, idFWD_REST) ||
2451#if idFWD_KWREST
2452 !local_id(p, idFWD_KWREST) ||
2453#endif
2454 !local_id(p, idFWD_BLOCK)) {
2455 compile_error(p, "unexpected ...");
2456 $$ = Qnone;
2457 }
2458 else {
2459#if 0
2460 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@4), &@4);
2461#if idFWD_KWREST
2462 NODE *kwrest = list_append(p, NEW_LIST(0, &@4), NEW_LVAR(idFWD_KWREST, &@4));
2463#endif
2464 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@4), &@4);
2465 $$ = rest_arg_append(p, $2, splat, &@$);
2466#if idFWD_KWREST
2467 $$ = arg_append(p, $$, new_hash(p, kwrest, &@4), &@4);
2468#endif
2469 $$ = arg_blk_pass($$, block);
2470#endif
2471 {VALUE v1,v2,v3,v4,v5;v1=$2;v2=$4;v3=dispatch2(args_add,v1,v2);v4=v3;v5=dispatch1(arg_paren,v4);$$=v5;}
2472 }
2473 }
2474 | '(' args_forward rparen
2475 {
2476 if (!local_id(p, idFWD_REST) ||
2477#if idFWD_KWREST
2478 !local_id(p, idFWD_KWREST) ||
2479#endif
2480 !local_id(p, idFWD_BLOCK)) {
2481 compile_error(p, "unexpected ...");
2482 $$ = Qnone;
2483 }
2484 else {
2485#if 0
2486 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@2), &@2);
2487#if idFWD_KWREST
2488 NODE *kwrest = list_append(p, NEW_LIST(0, &@2), NEW_LVAR(idFWD_KWREST, &@2));
2489#endif
2490 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@2), &@2);
2491#if idFWD_KWREST
2492 $$ = arg_append(p, splat, new_hash(p, kwrest, &@2), &@2);
2493#else
2494 $$ = splat;
2495#endif
2496 $$ = arg_blk_pass($$, block);
2497#endif
2498 {VALUE v1,v2;v1=$2;v2=dispatch1(arg_paren,v1);$$=v2;}
2499 }
2500 }
2501 ;
2502
2503opt_paren_args : none
2504 | paren_args
2505 ;
2506
2507opt_call_args : none
2508 | call_args
2509 | args ','
2510 {
2511 $$ = $1;
2512 }
2513 | args ',' assocs ','
2514 {
2515#if 0
2516 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2517#endif
2518 {VALUE v1,v2,v3,v4,v5;v1=$3;v2=dispatch1(bare_assoc_hash,v1);v3=$1;v4=v2;v5=dispatch2(args_add,v3,v4);$$=v5;}
2519 }
2520 | assocs ','
2521 {
2522#if 0
2523 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2524#endif
2525 {VALUE v1,v2,v3,v4,v5,v6;v1=dispatch0(args_new);v2=$1;v3=dispatch1(bare_assoc_hash,v2);v4=v1;v5=v3;v6=dispatch2(args_add,v4,v5);$$=v6;}
2526 }
2527 ;
2528
2529call_args : command
2530 {
2531#if 0
2532 value_expr($1);
2533 $$ = NEW_LIST($1, &@$);
2534#endif
2535 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2536 }
2537 | args opt_block_arg
2538 {
2539#if 0
2540 $$ = arg_blk_pass($1, $2);
2541#endif
2542 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(args_add_block,v1,v2);$$=v3;}
2543 }
2544 | assocs opt_block_arg
2545 {
2546#if 0
2547 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2548 $$ = arg_blk_pass($$, $2);
2549#endif
2550 {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9;v1=dispatch0(args_new);v2=$1;v3=dispatch1(bare_assoc_hash,v2);v4=v1;v5=v3;v6=dispatch2(args_add,v4,v5);v7=v6;v8=$2;v9=dispatch2(args_add_block,v7,v8);$$=v9;}
2551 }
2552 | args ',' assocs opt_block_arg
2553 {
2554#if 0
2555 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2556 $$ = arg_blk_pass($$, $4);
2557#endif
2558 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$3;v2=dispatch1(bare_assoc_hash,v1);v3=$1;v4=v2;v5=dispatch2(args_add,v3,v4);v6=v5;v7=$4;v8=dispatch2(args_add_block,v6,v7);$$=v8;}
2559 }
2560 | block_arg
2561 {{VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add_block,v2,v3);$$=v4;}}
2562 ;
2563
2564command_args : {
2565 /* If call_args starts with a open paren '(' or '[',
2566 * look-ahead reading of the letters calls CMDARG_PUSH(0),
2567 * but the push must be done after CMDARG_PUSH(1).
2568 * So this code makes them consistent by first cancelling
2569 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
2570 * and finally redoing CMDARG_PUSH(0).
2571 */
2572 int lookahead = 0;
2573 switch (yychar) {
2574 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
2575 lookahead = 1;
2576 }
2577 if (lookahead) CMDARG_POP();
2578 CMDARG_PUSH(1);
2579 if (lookahead) CMDARG_PUSH(0);
2580 }
2581 call_args
2582 {
2583 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
2584 * but the push must be done after CMDARG_POP() in the parser.
2585 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
2586 * CMDARG_POP() to pop 1 pushed by command_args,
2587 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
2588 */
2589 int lookahead = 0;
2590 switch (yychar) {
2591 case tLBRACE_ARG:
2592 lookahead = 1;
2593 }
2594 if (lookahead) CMDARG_POP();
2595 CMDARG_POP();
2596 if (lookahead) CMDARG_PUSH(0);
2597 $$ = $2;
2598 }
2599 ;
2600
2601block_arg : tAMPER arg_value
2602 {
2603#if 0
2604 $$ = NEW_BLOCK_PASS($2, &@$);
2605#endif
2606 $$=$2;
2607 }
2608 ;
2609
2610opt_block_arg : ',' block_arg
2611 {
2612 $$ = $2;
2613 }
2614 | none
2615 {
2616 $$ = 0;
2617 }
2618 ;
2619
2620args : arg_value
2621 {
2622#if 0
2623 $$ = NEW_LIST($1, &@$);
2624#endif
2625 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2626 }
2627 | tSTAR arg_value
2628 {
2629#if 0
2630 $$ = NEW_SPLAT($2, &@$);
2631#endif
2632 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
2633 }
2634 | args ',' arg_value
2635 {
2636#if 0
2637 $$ = last_arg_append(p, $1, $3, &@$);
2638#endif
2639 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
2640 }
2641 | args ',' tSTAR arg_value
2642 {
2643#if 0
2644 $$ = rest_arg_append(p, $1, $4, &@$);
2645#endif
2646 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
2647 }
2648 ;
2649
2650mrhs_arg : mrhs
2651 | arg_value
2652 ;
2653
2654mrhs : args ',' arg_value
2655 {
2656#if 0
2657 $$ = last_arg_append(p, $1, $3, &@$);
2658#endif
2659 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(mrhs_new_from_args,v1);v3=v2;v4=$3;v5=dispatch2(mrhs_add,v3,v4);$$=v5;}
2660 }
2661 | args ',' tSTAR arg_value
2662 {
2663#if 0
2664 $$ = rest_arg_append(p, $1, $4, &@$);
2665#endif
2666 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(mrhs_new_from_args,v1);v3=v2;v4=$4;v5=dispatch2(mrhs_add_star,v3,v4);$$=v5;}
2667 }
2668 | tSTAR arg_value
2669 {
2670#if 0
2671 $$ = NEW_SPLAT($2, &@$);
2672#endif
2673 {VALUE v1,v2,v3,v4;v1=dispatch0(mrhs_new);v2=v1;v3=$2;v4=dispatch2(mrhs_add_star,v2,v3);$$=v4;}
2674 }
2675 ;
2676
2677primary : literal
2678 | strings
2679 | xstring
2680 | regexp
2681 | words
2682 | qwords
2683 | symbols
2684 | qsymbols
2685 | var_ref
2686 | backref
2687 | tFID
2688 {
2689#if 0
2690 $$ = NEW_FCALL($1, 0, &@$);
2691#endif
2692 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=dispatch1(fcall,v1);v3=dispatch0(args_new);v4=v2;v5=v3;v6=dispatch2(method_add_arg,v4,v5);$$=v6;}
2693 }
2694 | k_begin
2695 {
2696 CMDARG_PUSH(0);
2697 }
2698 bodystmt
2699 k_end
2700 {
2701 CMDARG_POP();
2702#if 0
2703 set_line_body($3, @1.end_pos.lineno);
2704 $$ = NEW_BEGIN($3, &@$);
2705 nd_set_line($$, @1.end_pos.lineno);
2706#endif
2707 {VALUE v1,v2;v1=$3;v2=dispatch1(begin,v1);$$=v2;}
2708 }
2709 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2710 {
2711#if 0
2712 $$ = NEW_BEGIN(0, &@$);
2713#endif
2714 {VALUE v1,v2;v1=0;v2=dispatch1(paren,v1);$$=v2;}
2715 }
2716 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2717 {
2718#if 0
2719 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2720 $$ = $2;
2721#endif
2722 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2723 }
2724 | tLPAREN compstmt ')'
2725 {
2726#if 0
2727 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2728 $$ = $2;
2729#endif
2730 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2731 }
2732 | primary_value tCOLON2 tCONSTANT
2733 {
2734#if 0
2735 $$ = NEW_COLON2($1, $3, &@$);
2736#endif
2737 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2738 }
2739 | tCOLON3 tCONSTANT
2740 {
2741#if 0
2742 $$ = NEW_COLON3($2, &@$);
2743#endif
2744 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2745 }
2746 | tLBRACK aref_args ']'
2747 {
2748#if 0
2749 $$ = make_list($2, &@$);
2750#endif
2751 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(array,v1);$$=v2;}
2752 }
2753 | tLBRACE assoc_list '}'
2754 {
2755#if 0
2756 $$ = new_hash(p, $2, &@$);
2757 $$->nd_brace = TRUE;
2758#endif
2759 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(hash,v1);$$=v2;}
2760 }
2761 | k_return
2762 {
2763#if 0
2764 $$ = NEW_RETURN(0, &@$);
2765#endif
2766 {VALUE v1;v1=dispatch0(return0);$$=v1;}
2767 }
2768 | keyword_yield '(' call_args rparen
2769 {
2770#if 0
2771 $$ = new_yield(p, $3, &@$);
2772#endif
2773 {VALUE v1,v2,v3,v4;v1=$3;v2=dispatch1(paren,v1);v3=v2;v4=dispatch1(yield,v3);$$=v4;}
2774 }
2775 | keyword_yield '(' rparen
2776 {
2777#if 0
2778 $$ = NEW_YIELD(0, &@$);
2779#endif
2780 {VALUE v1,v2,v3,v4,v5;v1=dispatch0(args_new);v2=v1;v3=dispatch1(paren,v2);v4=v3;v5=dispatch1(yield,v4);$$=v5;}
2781 }
2782 | keyword_yield
2783 {
2784#if 0
2785 $$ = NEW_YIELD(0, &@$);
2786#endif
2787 {VALUE v1;v1=dispatch0(yield0);$$=v1;}
2788 }
2789 | keyword_defined opt_nl '(' {p->in_defined = 1;} expr rparen
2790 {
2791 p->in_defined = 0;
2792 $$ = new_defined(p, $5, &@$);
2793 }
2794 | keyword_not '(' expr rparen
2795 {
2796 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2797 }
2798 | keyword_not '(' rparen
2799 {
2800 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
2801 }
2802 | fcall brace_block
2803 {
2804#if 0
2805 $$ = method_add_block(p, $1, $2, &@$);
2806#endif
2807 {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9;v1=$1;v2=dispatch1(fcall,v1);v3=dispatch0(args_new);v4=v2;v5=v3;v6=dispatch2(method_add_arg,v4,v5);v7=v6;v8=$2;v9=dispatch2(method_add_block,v7,v8);$$=v9;}
2808 }
2809 | method_call
2810 | method_call brace_block
2811 {
2812#if 0
2813 block_dup_check(p, $1->nd_args, $2);
2814 $$ = method_add_block(p, $1, $2, &@$);
2815#endif
2816 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
2817 }
2818 | tLAMBDA
2819 {
2820 token_info_push(p, "->", &@1);
2821 }
2822 lambda
2823 {
2824 $$ = $3;
2825#if 0
2826 nd_set_first_loc($$, @1.beg_pos);
2827#endif
2828 }
2829 | k_if expr_value then
2830 compstmt
2831 if_tail
2832 k_end
2833 {
2834#if 0
2835 $$ = new_if(p, $2, $4, $5, &@$);
2836 fixpos($$, $2);
2837#endif
2838 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(if,v1,v2,v3);$$=v4;}
2839 }
2840 | k_unless expr_value then
2841 compstmt
2842 opt_else
2843 k_end
2844 {
2845#if 0
2846 $$ = new_unless(p, $2, $4, $5, &@$);
2847 fixpos($$, $2);
2848#endif
2849 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(unless,v1,v2,v3);$$=v4;}
2850 }
2851 | k_while expr_value_do
2852 compstmt
2853 k_end
2854 {
2855#if 0
2856 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
2857 fixpos($$, $2);
2858#endif
2859 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(while,v1,v2);$$=v3;}
2860 }
2861 | k_until expr_value_do
2862 compstmt
2863 k_end
2864 {
2865#if 0
2866 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
2867 fixpos($$, $2);
2868#endif
2869 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(until,v1,v2);$$=v3;}
2870 }
2871 | k_case expr_value opt_terms
2872 {
2873 $<val>$ = p->case_labels;
2874 p->case_labels = Qnil;
2875 }
2876 case_body
2877 k_end
2878 {
2879 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2880 p->case_labels = $<val>4;
2881#if 0
2882 $$ = NEW_CASE($2, $5, &@$);
2883 fixpos($$, $2);
2884#endif
2885 {VALUE v1,v2,v3;v1=$2;v2=$5;v3=dispatch2(case,v1,v2);$$=v3;}
2886 }
2887 | k_case opt_terms
2888 {
2889 $<val>$ = p->case_labels;
2890 p->case_labels = 0;
2891 }
2892 case_body
2893 k_end
2894 {
2895 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2896 p->case_labels = $<val>3;
2897#if 0
2898 $$ = NEW_CASE2($4, &@$);
2899#endif
2900 {VALUE v1,v2,v3;v1=Qnil;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
2901 }
2902 | k_case expr_value opt_terms
2903 p_case_body
2904 k_end
2905 {
2906#if 0
2907 $$ = new_case3(p, $2, $4, &@$);
2908#endif
2909 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
2910 }
2911 | k_for for_var keyword_in expr_value_do
2912 compstmt
2913 k_end
2914 {
2915#if 0
2916 /*
2917 * for a, b, c in e
2918 * #=>
2919 * e.each{|*x| a, b, c = x}
2920 *
2921 * for a in e
2922 * #=>
2923 * e.each{|x| a, = x}
2924 */
2925 ID id = internal_id(p);
2926 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
2927 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
2928 ID *tbl = ALLOC_N(ID, 3);
2929 tbl[0] = 1 /* length of local var table */; tbl[1] = id /* internal id */;
2930 rb_ast_add_local_table(p->ast, tbl);
2931
2932 switch (nd_type($2)) {
2933 case NODE_LASGN:
2934 case NODE_DASGN:
2935 case NODE_DASGN_CURR: /* e.each {|internal_var| a = internal_var; ... } */
2936 $2->nd_value = internal_var;
2937 id = 0;
2938 m->nd_plen = 1;
2939 m->nd_next = $2;
2940 break;
2941 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
2942 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), &@2);
2943 break;
2944 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
2945 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, &@2);
2946 }
2947 /* {|*internal_id| <m> = internal_id; ... } */
2948 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
2949 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
2950 $$ = NEW_FOR($4, scope, &@$);
2951 fixpos($$, $2);
2952#endif
2953 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=$5;v4=dispatch3(for,v1,v2,v3);$$=v4;}
2954 }
2955 | k_class cpath superclass
2956 {
2957 if (p->in_def) {
2958 YYLTYPE loc = code_loc_gen(&@1, &@2);
2959 yyerror1(&loc, "class definition in method body");
2960 }
2961 $<num>1 = p->in_class;
2962 p->in_class = 1;
2963 local_push(p, 0);
2964 }
2965 bodystmt
2966 k_end
2967 {
2968#if 0
2969 $$ = NEW_CLASS($2, $5, $3, &@$);
2970 nd_set_line($$->nd_body, @6.end_pos.lineno);
2971 set_line_body($5, @3.end_pos.lineno);
2972 nd_set_line($$, @3.end_pos.lineno);
2973#endif
2974 {VALUE v1,v2,v3,v4;v1=$2;v2=$3;v3=$5;v4=dispatch3(class,v1,v2,v3);$$=v4;}
2975 local_pop(p);
2976 p->in_class = $<num>1 & 1;
2977 }
2978 | k_class tLSHFT expr
2979 {
2980 $<num>$ = (p->in_class << 1) | p->in_def;
2981 p->in_def = 0;
2982 p->in_class = 0;
2983 local_push(p, 0);
2984 }
2985 term
2986 bodystmt
2987 k_end
2988 {
2989#if 0
2990 $$ = NEW_SCLASS($3, $6, &@$);
2991 nd_set_line($$->nd_body, @7.end_pos.lineno);
2992 set_line_body($6, nd_line($3));
2993 fixpos($$, $3);
2994#endif
2995 {VALUE v1,v2,v3;v1=$3;v2=$6;v3=dispatch2(sclass,v1,v2);$$=v3;}
2996 local_pop(p);
2997 p->in_def = $<num>4 & 1;
2998 p->in_class = ($<num>4 >> 1) & 1;
2999 }
3000 | k_module cpath
3001 {
3002 if (p->in_def) {
3003 YYLTYPE loc = code_loc_gen(&@1, &@2);
3004 yyerror1(&loc, "module definition in method body");
3005 }
3006 $<num>1 = p->in_class;
3007 p->in_class = 1;
3008 local_push(p, 0);
3009 }
3010 bodystmt
3011 k_end
3012 {
3013#if 0
3014 $$ = NEW_MODULE($2, $4, &@$);
3015 nd_set_line($$->nd_body, @5.end_pos.lineno);
3016 set_line_body($4, @2.end_pos.lineno);
3017 nd_set_line($$, @2.end_pos.lineno);
3018#endif
3019 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(module,v1,v2);$$=v3;}
3020 local_pop(p);
3021 p->in_class = $<num>1 & 1;
3022 }
3023 | k_def fname
3024 {
3025 numparam_name(p, get_id($2));
3026 local_push(p, 0);
3027 $<id>$ = p->cur_arg;
3028 p->cur_arg = 0;
3029 }
3030 {
3031 $<num>$ = p->in_def;
3032 p->in_def = 1;
3033 }
3034 f_arglist
3035 bodystmt
3036 k_end
3037 {
3038#if 0
3039 NODE *body = remove_begin($6);
3040 reduce_nodes(p, &body);
3041 $$ = NEW_DEFN($2, $5, body, &@$);
3042 nd_set_line($$->nd_defn, @7.end_pos.lineno);
3043 set_line_body(body, @1.beg_pos.lineno);
3044#endif
3045 {VALUE v1,v2,v3,v4;v1=$2;v2=$5;v3=$6;v4=dispatch3(def,v1,v2,v3);$$=v4;}
3046 local_pop(p);
3047 p->in_def = $<num>4 & 1;
3048 p->cur_arg = $<id>3;
3049 }
3050 | k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname
3051 {
3052 numparam_name(p, get_id($5));
3053 $<num>4 = p->in_def;
3054 p->in_def = 1;
3055 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3056 local_push(p, 0);
3057 $<id>$ = p->cur_arg;
3058 p->cur_arg = 0;
3059 }
3060 f_arglist
3061 bodystmt
3062 k_end
3063 {
3064#if 0
3065 NODE *body = remove_begin($8);
3066 reduce_nodes(p, &body);
3067 $$ = NEW_DEFS($2, $5, $7, body, &@$);
3068 nd_set_line($$->nd_defn, @9.end_pos.lineno);
3069 set_line_body(body, @1.beg_pos.lineno);
3070#endif
3071 {VALUE v1,v2,v3,v4,v5,v6;v1=$2;v2=$3;v3=$5;v4=$7;v5=$8;v6=dispatch5(defs,v1,v2,v3,v4,v5);$$=v6;}
3072 local_pop(p);
3073 p->in_def = $<num>4 & 1;
3074 p->cur_arg = $<id>6;
3075 }
3076 | keyword_break
3077 {
3078#if 0
3079 $$ = NEW_BREAK(0, &@$);
3080#endif
3081 {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(break,v2);$$=v3;}
3082 }
3083 | keyword_next
3084 {
3085#if 0
3086 $$ = NEW_NEXT(0, &@$);
3087#endif
3088 {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(next,v2);$$=v3;}
3089 }
3090 | keyword_redo
3091 {
3092#if 0
3093 $$ = NEW_REDO(&@$);
3094#endif
3095 {VALUE v1;v1=dispatch0(redo);$$=v1;}
3096 }
3097 | keyword_retry
3098 {
3099#if 0
3100 $$ = NEW_RETRY(&@$);
3101#endif
3102 {VALUE v1;v1=dispatch0(retry);$$=v1;}
3103 }
3104 ;
3105
3106primary_value : primary
3107 {
3108 value_expr($1);
3109 $$ = $1;
3110 }
3111 ;
3112
3113k_begin : keyword_begin
3114 {
3115 token_info_push(p, "begin", &@$);
3116 }
3117 ;
3118
3119k_if : keyword_if
3120 {
3121 WARN_EOL("if");
3122 token_info_push(p, "if", &@$);
3123 if (p->token_info && p->token_info->nonspc &&
3124 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3125 const char *tok = p->lex.ptok;
3126 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3127 beg += rb_strlen_lit("else");
3128 while (beg < tok && ISSPACE(*beg)) beg++;
3129 if (beg == tok) {
3130 p->token_info->nonspc = 0;
3131 }
3132 }
3133 }
3134 ;
3135
3136k_unless : keyword_unless
3137 {
3138 token_info_push(p, "unless", &@$);
3139 }
3140 ;
3141
3142k_while : keyword_while
3143 {
3144 token_info_push(p, "while", &@$);
3145 }
3146 ;
3147
3148k_until : keyword_until
3149 {
3150 token_info_push(p, "until", &@$);
3151 }
3152 ;
3153
3154k_case : keyword_case
3155 {
3156 token_info_push(p, "case", &@$);
3157 }
3158 ;
3159
3160k_for : keyword_for
3161 {
3162 token_info_push(p, "for", &@$);
3163 }
3164 ;
3165
3166k_class : keyword_class
3167 {
3168 token_info_push(p, "class", &@$);
3169 }
3170 ;
3171
3172k_module : keyword_module
3173 {
3174 token_info_push(p, "module", &@$);
3175 }
3176 ;
3177
3178k_def : keyword_def
3179 {
3180 token_info_push(p, "def", &@$);
3181 }
3182 ;
3183
3184k_do : keyword_do
3185 {
3186 token_info_push(p, "do", &@$);
3187 }
3188 ;
3189
3190k_do_block : keyword_do_block
3191 {
3192 token_info_push(p, "do", &@$);
3193 }
3194 ;
3195
3196k_rescue : keyword_rescue
3197 {
3198 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3199 }
3200 ;
3201
3202k_ensure : keyword_ensure
3203 {
3204 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3205 }
3206 ;
3207
3208k_when : keyword_when
3209 {
3210 token_info_warn(p, "when", p->token_info, 0, &@$);
3211 }
3212 ;
3213
3214k_else : keyword_else
3215 {
3216 token_info *ptinfo_beg = p->token_info;
3217 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3218 token_info_warn(p, "else", p->token_info, same, &@$);
3219 if (same) {
3220 token_info e;
3221 e.next = ptinfo_beg->next;
3222 e.token = "else";
3223 token_info_setup(&e, p->lex.pbeg, &@$);
3224 if (!e.nonspc) *ptinfo_beg = e;
3225 }
3226 }
3227 ;
3228
3229k_elsif : keyword_elsif
3230 {
3231 WARN_EOL("elsif");
3232 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3233 }
3234 ;
3235
3236k_end : keyword_end
3237 {
3238 token_info_pop(p, "end", &@$);
3239 }
3240 ;
3241
3242k_return : keyword_return
3243 {
3244 if (p->in_class && !p->in_def && !dyna_in_block(p))
3245 yyerror1(&@1, "Invalid return in class/module body");
3246 }
3247 ;
3248
3249then : term
3250 | keyword_then
3251 | term keyword_then
3252 ;
3253
3254do : term
3255 | keyword_do_cond
3256 ;
3257
3258if_tail : opt_else
3259 | k_elsif expr_value then
3260 compstmt
3261 if_tail
3262 {
3263#if 0
3264 $$ = new_if(p, $2, $4, $5, &@$);
3265 fixpos($$, $2);
3266#endif
3267 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(elsif,v1,v2,v3);$$=v4;}
3268 }
3269 ;
3270
3271opt_else : none
3272 | k_else compstmt
3273 {
3274#if 0
3275 $$ = $2;
3276#endif
3277 {VALUE v1,v2;v1=$2;v2=dispatch1(else,v1);$$=v2;}
3278 }
3279 ;
3280
3281for_var : lhs
3282 | mlhs
3283 ;
3284
3285f_marg : f_norm_arg
3286 {
3287#if 0
3288 $$ = assignable(p, $1, 0, &@$);
3289 mark_lvar_used(p, $$);
3290#endif
3291 $$=assignable(p, $1);
3292 }
3293 | tLPAREN f_margs rparen
3294 {
3295#if 0
3296 $$ = $2;
3297#endif
3298 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
3299 }
3300 ;
3301
3302f_marg_list : f_marg
3303 {
3304#if 0
3305 $$ = NEW_LIST($1, &@$);
3306#endif
3307 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
3308 }
3309 | f_marg_list ',' f_marg
3310 {
3311#if 0
3312 $$ = list_append(p, $1, $3);
3313#endif
3314 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
3315 }
3316 ;
3317
3318f_margs : f_marg_list
3319 {
3320#if 0
3321 $$ = NEW_MASGN($1, 0, &@$);
3322#endif
3323 $$=$1;
3324 }
3325 | f_marg_list ',' f_rest_marg
3326 {
3327#if 0
3328 $$ = NEW_MASGN($1, $3, &@$);
3329#endif
3330 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
3331 }
3332 | f_marg_list ',' f_rest_marg ',' f_marg_list
3333 {
3334#if 0
3335 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3336#endif
3337 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);v4=v3;v5=$5;v6=dispatch2(mlhs_add_post,v4,v5);$$=v6;}
3338 }
3339 | f_rest_marg
3340 {
3341#if 0
3342 $$ = NEW_MASGN(0, $1, &@$);
3343#endif
3344 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
3345 }
3346 | f_rest_marg ',' f_marg_list
3347 {
3348#if 0
3349 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3350#endif
3351 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add_star,v2,v3);v5=v4;v6=$3;v7=dispatch2(mlhs_add_post,v5,v6);$$=v7;}
3352 }
3353 ;
3354
3355f_rest_marg : tSTAR f_norm_arg
3356 {
3357#if 0
3358 $$ = assignable(p, $2, 0, &@$);
3359 mark_lvar_used(p, $$);
3360#endif
3361 $$=assignable(p, $2);
3362 }
3363 | tSTAR
3364 {
3365#if 0
3366 $$ = NODE_SPECIAL_NO_NAME_REST;
3367#endif
3368 $$=Qnil;
3369 }
3370 ;
3371
3372block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3373 {
3374 $$ = new_args_tail(p, $1, $3, $4, &@3);
3375 }
3376 | f_block_kwarg opt_f_block_arg
3377 {
3378 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3379 }
3380 | f_kwrest opt_f_block_arg
3381 {
3382 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3383 }
3384 | f_no_kwarg opt_f_block_arg
3385 {
3386 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
3387 }
3388 | f_block_arg
3389 {
3390 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3391 }
3392 ;
3393
3394opt_block_args_tail : ',' block_args_tail
3395 {
3396 $$ = $2;
3397 }
3398 | /* none */
3399 {
3400 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3401 }
3402 ;
3403
3404block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3405 {
3406 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3407 }
3408 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3409 {
3410 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3411 }
3412 | f_arg ',' f_block_optarg opt_block_args_tail
3413 {
3414 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3415 }
3416 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3417 {
3418 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3419 }
3420 | f_arg ',' f_rest_arg opt_block_args_tail
3421 {
3422 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3423 }
3424 | f_arg ','
3425 {
3426#if 0
3427 /* magic number for rest_id in iseq_set_arguments() */
3428 $$ = new_args(p, $1, Qnone, NODE_SPECIAL_EXCESSIVE_COMMA, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, &@1), &@$);
3429#endif
3430 {VALUE v1;v1=dispatch0(excessed_comma);$$=new_args(p, $1, Qnone, v1, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, NULL), NULL);}
3431 }
3432 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3433 {
3434 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
3435 }
3436 | f_arg opt_block_args_tail
3437 {
3438 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
3439 }
3440 | f_block_optarg ',' f_rest_arg opt_block_args_tail
3441 {
3442 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
3443 }
3444 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3445 {
3446 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
3447 }
3448 | f_block_optarg opt_block_args_tail
3449 {
3450 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
3451 }
3452 | f_block_optarg ',' f_arg opt_block_args_tail
3453 {
3454 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
3455 }
3456 | f_rest_arg opt_block_args_tail
3457 {
3458 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
3459 }
3460 | f_rest_arg ',' f_arg opt_block_args_tail
3461 {
3462 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
3463 }
3464 | block_args_tail
3465 {
3466 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
3467 }
3468 ;
3469
3470opt_block_param : none
3471 | block_param_def
3472 {
3473 p->command_start = TRUE;
3474 }
3475 ;
3476
3477block_param_def : '|' opt_bv_decl '|'
3478 {
3479 p->cur_arg = 0;
3480 p->max_numparam = ORDINAL_PARAM;
3481#if 0
3482 $$ = 0;
3483#endif
3484 {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11;v1=Qnil;v2=Qnil;v3=Qnil;v4=Qnil;v5=Qnil;v6=Qnil;v7=Qnil;v8=dispatch7(params,v1,v2,v3,v4,v5,v6,v7);v9=v8;v10=escape_Qundef($2);v11=dispatch2(block_var,v9,v10);$$=v11;}
3485 }
3486 | '|' block_param opt_bv_decl '|'
3487 {
3488 p->cur_arg = 0;
3489 p->max_numparam = ORDINAL_PARAM;
3490#if 0
3491 $$ = $2;
3492#endif
3493 {VALUE v1,v2,v3;v1=escape_Qundef($2);v2=escape_Qundef($3);v3=dispatch2(block_var,v1,v2);$$=v3;}
3494 }
3495 ;
3496
3497
3498opt_bv_decl : opt_nl
3499 {
3500 $$ = 0;
3501 }
3502 | opt_nl ';' bv_decls opt_nl
3503 {
3504#if 0
3505 $$ = 0;
3506#endif
3507 $$=$3;
3508 }
3509 ;
3510
3511bv_decls : bvar
3512 {$$=rb_ary_new3(1, get_value($1));}
3513 | bv_decls ',' bvar
3514 {$$=rb_ary_push($1, get_value($3));}
3515 ;
3516
3517bvar : tIDENTIFIER
3518 {
3519 new_bv(p, get_id($1));
3520 $$=get_value($1);
3521 }
3522 | f_bad_arg
3523 {
3524 $$ = 0;
3525 }
3526 ;
3527
3528lambda : {
3529 $<vars>$ = dyna_push(p);
3530 }
3531 {
3532 $<num>$ = p->lex.lpar_beg;
3533 p->lex.lpar_beg = p->lex.paren_nest;
3534 }
3535 {
3536 $<num>$ = p->max_numparam;
3537 p->max_numparam = 0;
3538 }
3539 {
3540 $<node>$ = numparam_push(p);
3541 }
3542 f_larglist
3543 {
3544 CMDARG_PUSH(0);
3545 }
3546 lambda_body
3547 {
3548 int max_numparam = p->max_numparam;
3549 p->lex.lpar_beg = $<num>2;
3550 p->max_numparam = $<num>3;
3551 CMDARG_POP();
3552 $5 = args_with_numbered(p, $5, max_numparam);
3553#if 0
3554 {
3555 YYLTYPE loc = code_loc_gen(&@5, &@7);
3556 $$ = NEW_LAMBDA($5, $7, &loc);
3557 nd_set_line($$->nd_body, @7.end_pos.lineno);
3558 nd_set_line($$, @5.end_pos.lineno);
3559 }
3560#endif
3561 {VALUE v1,v2,v3;v1=$5;v2=$7;v3=dispatch2(lambda,v1,v2);$$=v3;}
3562 numparam_pop(p, $<node>4);
3563 dyna_pop(p, $<vars>1);
3564 }
3565 ;
3566
3567f_larglist : '(' f_args opt_bv_decl ')'
3568 {
3569#if 0
3570 $$ = $2;
3571 p->max_numparam = ORDINAL_PARAM;
3572#endif
3573 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
3574 }
3575 | f_args
3576 {
3577#if 0
3578 if (!args_info_empty_p($1->nd_ainfo))
3579 p->max_numparam = ORDINAL_PARAM;
3580#endif
3581 $$ = $1;
3582 }
3583 ;
3584
3585lambda_body : tLAMBEG compstmt '}'
3586 {
3587 token_info_pop(p, "}", &@3);
3588 $$ = $2;
3589 }
3590 | keyword_do_LAMBDA bodystmt k_end
3591 {
3592 $$ = $2;
3593 }
3594 ;
3595
3596do_block : k_do_block do_body k_end
3597 {
3598 $$ = $2;
3599#if 0
3600 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3601 nd_set_line($$, @1.end_pos.lineno);
3602#endif
3603 }
3604 ;
3605
3606block_call : command do_block
3607 {
3608#if 0
3609 if (nd_type($1) == NODE_YIELD) {
3610 compile_error(p, "block given to yield");
3611 }
3612 else {
3613 block_dup_check(p, $1->nd_args, $2);
3614 }
3615 $$ = method_add_block(p, $1, $2, &@$);
3616 fixpos($$, $1);
3617#endif
3618 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
3619 }
3620 | block_call call_op2 operation2 opt_paren_args
3621 {
3622#if 0
3623 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3624#endif
3625 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=v6==Qundef ? v5 : dispatch2(method_add_arg,v5,v6);$$=v7;}
3626 }
3627 | block_call call_op2 operation2 opt_paren_args brace_block
3628 {
3629#if 0
3630 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3631#endif
3632 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=v7==Qundef ? v6 : dispatch2(method_add_block,v6,v7);$$=v8;}
3633 }
3634 | block_call call_op2 operation2 command_args do_block
3635 {
3636#if 0
3637 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3638#endif
3639 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=dispatch2(method_add_block,v6,v7);$$=v8;}
3640 }
3641 ;
3642
3643method_call : fcall paren_args
3644 {
3645#if 0
3646 $$ = $1;
3647 $$->nd_args = $2;
3648 nd_set_last_loc($1, @2.end_pos);
3649#endif
3650 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(fcall,v1);v3=v2;v4=$2;v5=dispatch2(method_add_arg,v3,v4);$$=v5;}
3651 }
3652 | primary_value call_op operation2 opt_paren_args
3653 {
3654#if 0
3655 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3656 nd_set_line($$, @3.end_pos.lineno);
3657#endif
3658 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=v6==Qundef ? v5 : dispatch2(method_add_arg,v5,v6);$$=v7;}
3659 }
3660 | primary_value tCOLON2 operation2 paren_args
3661 {
3662#if 0
3663 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
3664 nd_set_line($$, @3.end_pos.lineno);
3665#endif
3666 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
3667 }
3668 | primary_value tCOLON2 operation3
3669 {
3670#if 0
3671 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
3672#endif
3673 {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(call,v1,v2,v3);$$=v4;}
3674 }
3675 | primary_value call_op paren_args
3676 {
3677#if 0
3678 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
3679 nd_set_line($$, @2.end_pos.lineno);
3680#endif
3681 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=ID2VAL(idCall);v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$3;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
3682 }
3683 | primary_value tCOLON2 paren_args
3684 {
3685#if 0
3686 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
3687 nd_set_line($$, @2.end_pos.lineno);
3688#endif
3689 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=ID2VAL(idCOLON2);v3=ID2VAL(idCall);v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$3;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
3690 }
3691 | keyword_super paren_args
3692 {
3693#if 0
3694 $$ = NEW_SUPER($2, &@$);
3695#endif
3696 {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
3697 }
3698 | keyword_super
3699 {
3700#if 0
3701 $$ = NEW_ZSUPER(&@$);
3702#endif
3703 {VALUE v1;v1=dispatch0(zsuper);$$=v1;}
3704 }
3705 | primary_value '[' opt_call_args rbracket
3706 {
3707#if 0
3708 if ($1 && nd_type($1) == NODE_SELF)
3709 $$ = NEW_FCALL(tAREF, $3, &@$);
3710 else
3711 $$ = NEW_CALL($1, tAREF, $3, &@$);
3712 fixpos($$, $1);
3713#endif
3714 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref,v1,v2);$$=v3;}
3715 }
3716 ;
3717
3718brace_block : '{' brace_body '}'
3719 {
3720 $$ = $2;
3721#if 0
3722 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3723 nd_set_line($$, @1.end_pos.lineno);
3724#endif
3725 }
3726 | k_do do_body k_end
3727 {
3728 $$ = $2;
3729#if 0
3730 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3731 nd_set_line($$, @1.end_pos.lineno);
3732#endif
3733 }
3734 ;
3735
3736brace_body : {$<vars>$ = dyna_push(p);}
3737 {
3738 $<num>$ = p->max_numparam;
3739 p->max_numparam = 0;
3740 }
3741 {
3742 $<node>$ = numparam_push(p);
3743 }
3744 opt_block_param compstmt
3745 {
3746 int max_numparam = p->max_numparam;
3747 p->max_numparam = $<num>2;
3748 $4 = args_with_numbered(p, $4, max_numparam);
3749#if 0
3750 $$ = NEW_ITER($4, $5, &@$);
3751#endif
3752 {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(brace_block,v1,v2);$$=v3;}
3753 numparam_pop(p, $<node>3);
3754 dyna_pop(p, $<vars>1);
3755 }
3756 ;
3757
3758do_body : {$<vars>$ = dyna_push(p);}
3759 {
3760 $<num>$ = p->max_numparam;
3761 p->max_numparam = 0;
3762 }
3763 {
3764 $<node>$ = numparam_push(p);
3765 CMDARG_PUSH(0);
3766 }
3767 opt_block_param bodystmt
3768 {
3769 int max_numparam = p->max_numparam;
3770 p->max_numparam = $<num>2;
3771 $4 = args_with_numbered(p, $4, max_numparam);
3772#if 0
3773 $$ = NEW_ITER($4, $5, &@$);
3774#endif
3775 {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(do_block,v1,v2);$$=v3;}
3776 CMDARG_POP();
3777 numparam_pop(p, $<node>3);
3778 dyna_pop(p, $<vars>1);
3779 }
3780 ;
3781
3782case_args : arg_value
3783 {
3784#if 0
3785 check_literal_when(p, $1, &@1);
3786 $$ = NEW_LIST($1, &@$);
3787#endif
3788 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
3789 }
3790 | tSTAR arg_value
3791 {
3792#if 0
3793 $$ = NEW_SPLAT($2, &@$);
3794#endif
3795 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
3796 }
3797 | case_args ',' arg_value
3798 {
3799#if 0
3800 check_literal_when(p, $3, &@3);
3801 $$ = last_arg_append(p, $1, $3, &@$);
3802#endif
3803 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
3804 }
3805 | case_args ',' tSTAR arg_value
3806 {
3807#if 0
3808 $$ = rest_arg_append(p, $1, $4, &@$);
3809#endif
3810 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
3811 }
3812 ;
3813
3814case_body : k_when case_args then
3815 compstmt
3816 cases
3817 {
3818#if 0
3819 $$ = NEW_WHEN($2, $4, $5, &@$);
3820 fixpos($$, $2);
3821#endif
3822 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(when,v1,v2,v3);$$=v4;}
3823 }
3824 ;
3825
3826cases : opt_else
3827 | case_body
3828 ;
3829
3830p_case_body : keyword_in
3831 {
3832 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
3833 p->command_start = FALSE;
3834 $<num>$ = p->in_kwarg;
3835 p->in_kwarg = 1;
3836 }
3837 {$<tbl>$ = push_pvtbl(p);}
3838 {$<tbl>$ = push_pktbl(p);}
3839 p_top_expr then
3840 {pop_pktbl(p, $<tbl>4);}
3841 {pop_pvtbl(p, $<tbl>3);}
3842 {
3843 p->in_kwarg = !!$<num>2;
3844 }
3845 compstmt
3846 p_cases
3847 {
3848#if 0
3849 $$ = NEW_IN($5, $10, $11, &@$);
3850#endif
3851 {VALUE v1,v2,v3,v4;v1=$5;v2=$10;v3=escape_Qundef($11);v4=dispatch3(in,v1,v2,v3);$$=v4;}
3852 }
3853 ;
3854
3855p_cases : opt_else
3856 | p_case_body
3857 ;
3858
3859p_top_expr : p_top_expr_body
3860 | p_top_expr_body modifier_if expr_value
3861 {
3862#if 0
3863 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
3864 fixpos($$, $3);
3865#endif
3866 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
3867 }
3868 | p_top_expr_body modifier_unless expr_value
3869 {
3870#if 0
3871 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
3872 fixpos($$, $3);
3873#endif
3874 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
3875 }
3876 ;
3877
3878p_top_expr_body : p_expr
3879 | p_expr ','
3880 {
3881 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
3882 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
3883 }
3884 | p_expr ',' p_args
3885 {
3886 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
3887#if 0
3888 nd_set_first_loc($$, @1.beg_pos);
3889#endif
3890
3891 }
3892 | p_args_tail
3893 {
3894 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
3895 }
3896 | p_kwargs
3897 {
3898 $$ = new_hash_pattern(p, Qnone, $1, &@$);
3899 }
3900 ;
3901
3902p_expr : p_as
3903 ;
3904
3905p_as : p_expr tASSOC p_variable
3906 {
3907#if 0
3908 NODE *n = NEW_LIST($1, &@$);
3909 n = list_append(p, n, $3);
3910 $$ = new_hash(p, n, &@$);
3911#endif
3912 {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(id_assoc);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
3913 }
3914 | p_alt
3915 ;
3916
3917p_alt : p_alt '|' p_expr_basic
3918 {
3919#if 0
3920 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
3921#endif
3922 {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(idOr);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
3923 }
3924 | p_expr_basic
3925 ;
3926
3927p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
3928p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
3929
3930p_expr_basic : p_value
3931 | p_const p_lparen p_args rparen
3932 {
3933 pop_pktbl(p, $<tbl>2);
3934 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3935#if 0
3936 nd_set_first_loc($$, @1.beg_pos);
3937#endif
3938
3939 }
3940 | p_const p_lparen p_kwargs rparen
3941 {
3942 pop_pktbl(p, $<tbl>2);
3943 $$ = new_hash_pattern(p, $1, $3, &@$);
3944#if 0
3945 nd_set_first_loc($$, @1.beg_pos);
3946#endif
3947
3948 }
3949 | p_const '(' rparen
3950 {
3951 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3952 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3953 }
3954 | p_const p_lbracket p_args rbracket
3955 {
3956 pop_pktbl(p, $<tbl>2);
3957 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3958#if 0
3959 nd_set_first_loc($$, @1.beg_pos);
3960#endif
3961
3962 }
3963 | p_const p_lbracket p_kwargs rbracket
3964 {
3965 pop_pktbl(p, $<tbl>2);
3966 $$ = new_hash_pattern(p, $1, $3, &@$);
3967#if 0
3968 nd_set_first_loc($$, @1.beg_pos);
3969#endif
3970
3971 }
3972 | p_const '[' rbracket
3973 {
3974 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3975 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3976 }
3977 | tLBRACK {$<tbl>$ = push_pktbl(p);} p_args rbracket
3978 {
3979 pop_pktbl(p, $<tbl>2);
3980 $$ = new_array_pattern(p, Qnone, Qnone, $3, &@$);
3981 }
3982 | tLBRACK rbracket
3983 {
3984 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3985 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
3986 }
3987 | tLBRACE
3988 {
3989 $<tbl>$ = push_pktbl(p);
3990 $<num>1 = p->in_kwarg;
3991 p->in_kwarg = 0;
3992 }
3993 p_kwargs rbrace
3994 {
3995 pop_pktbl(p, $<tbl>2);
3996 p->in_kwarg = $<num>1;
3997 $$ = new_hash_pattern(p, Qnone, $3, &@$);
3998 }
3999 | tLBRACE rbrace
4000 {
4001 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
4002 $$ = new_hash_pattern(p, Qnone, $$, &@$);
4003 }
4004 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
4005 {
4006 pop_pktbl(p, $<tbl>2);
4007 $$ = $3;
4008 }
4009 ;
4010
4011p_args : p_expr
4012 {
4013#if 0
4014 NODE *pre_args = NEW_LIST($1, &@$);
4015 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4016#endif
4017 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
4018
4019 }
4020 | p_args_head
4021 {
4022 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4023 }
4024 | p_args_head p_arg
4025 {
4026#if 0
4027 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
4028#endif
4029 VALUE pre_args = rb_ary_concat($1, get_value($2));
4030 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4031
4032 }
4033 | p_args_head tSTAR tIDENTIFIER
4034 {
4035 $$ = new_array_pattern_tail(p, $1, 1, $3, Qnone, &@$);
4036 }
4037 | p_args_head tSTAR tIDENTIFIER ',' p_args_post
4038 {
4039 $$ = new_array_pattern_tail(p, $1, 1, $3, $5, &@$);
4040 }
4041 | p_args_head tSTAR
4042 {
4043 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4044 }
4045 | p_args_head tSTAR ',' p_args_post
4046 {
4047 $$ = new_array_pattern_tail(p, $1, 1, 0, $4, &@$);
4048 }
4049 | p_args_tail
4050 ;
4051
4052p_args_head : p_arg ','
4053 {
4054 $$ = $1;
4055 }
4056 | p_args_head p_arg ','
4057 {
4058#if 0
4059 $$ = list_concat($1, $2);
4060#endif
4061 $$=rb_ary_concat($1, get_value($2));
4062 }
4063 ;
4064
4065p_args_tail : tSTAR tIDENTIFIER
4066 {
4067 $$ = new_array_pattern_tail(p, Qnone, 1, $2, Qnone, &@$);
4068 }
4069 | tSTAR tIDENTIFIER ',' p_args_post
4070 {
4071 $$ = new_array_pattern_tail(p, Qnone, 1, $2, $4, &@$);
4072 }
4073 | tSTAR
4074 {
4075 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4076 }
4077 | tSTAR ',' p_args_post
4078 {
4079 $$ = new_array_pattern_tail(p, Qnone, 1, 0, $3, &@$);
4080 }
4081 ;
4082
4083p_args_post : p_arg
4084 | p_args_post ',' p_arg
4085 {
4086#if 0
4087 $$ = list_concat($1, $3);
4088#endif
4089 $$=rb_ary_concat($1, get_value($3));
4090 }
4091 ;
4092
4093p_arg : p_expr
4094 {
4095#if 0
4096 $$ = NEW_LIST($1, &@$);
4097#endif
4098 $$=rb_ary_new_from_args(1, get_value($1));
4099 }
4100 ;
4101
4102p_kwargs : p_kwarg ',' p_kwrest
4103 {
4104 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4105 }
4106 | p_kwarg
4107 {
4108 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4109 }
4110 | p_kwarg ','
4111 {
4112 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4113 }
4114 | p_kwrest
4115 {
4116 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4117 }
4118 | p_kwarg ',' p_kwnorest
4119 {
4120 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), ID2VAL(idNil), &@$);
4121 }
4122 | p_kwnorest
4123 {
4124 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), ID2VAL(idNil), &@$);
4125 }
4126 ;
4127
4128p_kwarg : p_kw
4129 {$$=rb_ary_new_from_args(1, $1);}
4130 | p_kwarg ',' p_kw
4131 {
4132#if 0
4133 $$ = list_concat($1, $3);
4134#endif
4135 $$=rb_ary_push($1, $3);
4136 }
4137 ;
4138
4139p_kw : p_kw_label p_expr
4140 {
4141 error_duplicate_pattern_key(p, get_id($1), &@1);
4142#if 0
4143 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), $2);
4144#endif
4145 $$=rb_ary_new_from_args(2, get_value($1), get_value($2));
4146 }
4147 | p_kw_label
4148 {
4149 error_duplicate_pattern_key(p, get_id($1), &@1);
4150 if ($1 && !is_local_id(get_id($1))) {
4151 yyerror1(&@1, "key must be valid as local variables");
4152 }
4153 error_duplicate_pattern_variable(p, get_id($1), &@1);
4154#if 0
4155 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4156#endif
4157 $$=rb_ary_new_from_args(2, get_value($1), Qnil);
4158 }
4159 ;
4160
4161p_kw_label : tLABEL
4162 | tSTRING_BEG string_contents tLABEL_END
4163 {
4164 YYLTYPE loc = code_loc_gen(&@1, &@3);
4165#if 0
4166 if (!$2 || nd_type($2) == NODE_STR) {
4167 NODE *node = dsym_node(p, $2, &loc);
4168 $$ = SYM2ID(node->nd_lit);
4169 }
4170#endif
4171 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4172 VALUE label = RNODE($2)->nd_cval;
4173 VALUE rval = RNODE($2)->nd_rval;
4174 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4175 RNODE($$)->nd_loc = loc;
4176 }
4177
4178 else {
4179 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4180 $$ = 0;
4181 }
4182 }
4183 ;
4184
4185p_kwrest : kwrest_mark tIDENTIFIER
4186 {
4187 $$ = $2;
4188 }
4189 | kwrest_mark
4190 {
4191 $$ = 0;
4192 }
4193 ;
4194
4195p_kwnorest : kwrest_mark keyword_nil
4196 {
4197 $$ = 0;
4198 }
4199 ;
4200
4201p_value : p_primitive
4202 | p_primitive tDOT2 p_primitive
4203 {
4204#if 0
4205 value_expr($1);
4206 value_expr($3);
4207 $$ = NEW_DOT2($1, $3, &@$);
4208#endif
4209 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
4210 }
4211 | p_primitive tDOT3 p_primitive
4212 {
4213#if 0
4214 value_expr($1);
4215 value_expr($3);
4216 $$ = NEW_DOT3($1, $3, &@$);
4217#endif
4218 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
4219 }
4220 | p_primitive tDOT2
4221 {
4222#if 0
4223 YYLTYPE loc;
4224 loc.beg_pos = @2.end_pos;
4225 loc.end_pos = @2.end_pos;
4226
4227 value_expr($1);
4228 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
4229#endif
4230 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
4231 }
4232 | p_primitive tDOT3
4233 {
4234#if 0
4235 YYLTYPE loc;
4236 loc.beg_pos = @2.end_pos;
4237 loc.end_pos = @2.end_pos;
4238
4239 value_expr($1);
4240 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
4241#endif
4242 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
4243 }
4244 | p_variable
4245 | p_var_ref
4246 | p_const
4247 | tBDOT2 p_primitive
4248 {
4249#if 0
4250 YYLTYPE loc;
4251 loc.beg_pos = @1.beg_pos;
4252 loc.end_pos = @1.beg_pos;
4253
4254 value_expr($2);
4255 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
4256#endif
4257 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
4258 }
4259 | tBDOT3 p_primitive
4260 {
4261#if 0
4262 YYLTYPE loc;
4263 loc.beg_pos = @1.beg_pos;
4264 loc.end_pos = @1.beg_pos;
4265
4266 value_expr($2);
4267 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
4268#endif
4269 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
4270 }
4271 ;
4272
4273p_primitive : literal
4274 | strings
4275 | xstring
4276 | regexp
4277 | words
4278 | qwords
4279 | symbols
4280 | qsymbols
4281 | keyword_variable
4282 {
4283#if 0
4284 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4285#endif
4286 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4287 }
4288 | tLAMBDA
4289 {
4290 token_info_push(p, "->", &@1);
4291 }
4292 lambda
4293 {
4294 $$ = $3;
4295#if 0
4296 nd_set_first_loc($$, @1.beg_pos);
4297#endif
4298 }
4299 ;
4300
4301p_variable : tIDENTIFIER
4302 {
4303#if 0
4304 error_duplicate_pattern_variable(p, $1, &@1);
4305 $$ = assignable(p, $1, 0, &@$);
4306#endif
4307 $$=assignable(p, var_field(p, $1));
4308 }
4309 ;
4310
4311p_var_ref : '^' tIDENTIFIER
4312 {
4313#if 0
4314 NODE *n = gettable(p, $2, &@$);
4315 if (!(nd_type(n) == NODE_LVAR || nd_type(n) == NODE_DVAR)) {
4316 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4317 }
4318 $$ = n;
4319#endif
4320 {VALUE v1,v2;v1=$2;v2=dispatch1(var_ref,v1);$$=v2;}
4321 }
4322 ;
4323
4324p_const : tCOLON3 cname
4325 {
4326#if 0
4327 $$ = NEW_COLON3($2, &@$);
4328#endif
4329 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
4330 }
4331 | p_const tCOLON2 cname
4332 {
4333#if 0
4334 $$ = NEW_COLON2($1, $3, &@$);
4335#endif
4336 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
4337 }
4338 | tCONSTANT
4339 {
4340#if 0
4341 $$ = gettable(p, $1, &@$);
4342#endif
4343 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4344 }
4345 ;
4346
4347opt_rescue : k_rescue exc_list exc_var then
4348 compstmt
4349 opt_rescue
4350 {
4351#if 0
4352 $$ = NEW_RESBODY($2,
4353 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), &@3), $5) : $5,
4354 $6, &@$);
4355 fixpos($$, $2?$2:$5);
4356#endif
4357 {VALUE v1,v2,v3,v4,v5;v1=escape_Qundef($2);v2=escape_Qundef($3);v3=escape_Qundef($5);v4=escape_Qundef($6);v5=dispatch4(rescue,v1,v2,v3,v4);$$=v5;}
4358 }
4359 | none
4360 ;
4361
4362exc_list : arg_value
4363 {
4364#if 0
4365 $$ = NEW_LIST($1, &@$);
4366#endif
4367 $$=rb_ary_new3(1, get_value($1));
4368 }
4369 | mrhs
4370 {
4371#if 0
4372 if (!($$ = splat_array($1))) $$ = $1;
4373#endif
4374 $$=$1;
4375 }
4376 | none
4377 ;
4378
4379exc_var : tASSOC lhs
4380 {
4381 $$ = $2;
4382 }
4383 | none
4384 ;
4385
4386opt_ensure : k_ensure compstmt
4387 {
4388#if 0
4389 $$ = $2;
4390#endif
4391 {VALUE v1,v2;v1=$2;v2=dispatch1(ensure,v1);$$=v2;}
4392 }
4393 | none
4394 ;
4395
4396literal : numeric
4397 | symbol
4398 ;
4399
4400strings : string
4401 {
4402#if 0
4403 NODE *node = $1;
4404 if (!node) {
4405 node = NEW_STR(STR_NEW0(), &@$);
4406 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
4407 }
4408 else {
4409 node = evstr2dstr(p, node);
4410 }
4411 $$ = node;
4412#endif
4413 $$=$1;
4414 }
4415 ;
4416
4417string : tCHAR
4418 | string1
4419 | string string1
4420 {
4421#if 0
4422 $$ = literal_concat(p, $1, $2, &@$);
4423#endif
4424 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_concat,v1,v2);$$=v3;}
4425 }
4426 ;
4427
4428string1 : tSTRING_BEG string_contents tSTRING_END
4429 {
4430#if 0
4431 $$ = heredoc_dedent(p, $2);
4432 if ($$) nd_set_loc($$, &@$);
4433#endif
4434 {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(string_literal,v1);$$=v2;}
4435 }
4436 ;
4437
4438xstring : tXSTRING_BEG xstring_contents tSTRING_END
4439 {
4440#if 0
4441 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4442#endif
4443 {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(xstring_literal,v1);$$=v2;}
4444 }
4445 ;
4446
4447regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4448 {
4449 $$ = new_regexp(p, $2, $3, &@$);
4450 }
4451 ;
4452
4453words : tWORDS_BEG ' ' word_list tSTRING_END
4454 {
4455#if 0
4456 $$ = make_list($3, &@$);
4457#endif
4458 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4459 }
4460 ;
4461
4462word_list : /* none */
4463 {
4464#if 0
4465 $$ = 0;
4466#endif
4467 {VALUE v1;v1=dispatch0(words_new);$$=v1;}
4468 }
4469 | word_list word ' '
4470 {
4471#if 0
4472 $$ = list_append(p, $1, evstr2dstr(p, $2));
4473#endif
4474 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(words_add,v1,v2);$$=v3;}
4475 }
4476 ;
4477
4478word : string_content
4479 {{VALUE v1,v2,v3,v4;v1=dispatch0(word_new);v2=v1;v3=$1;v4=dispatch2(word_add,v2,v3);$$=v4;}}
4480 | word string_content
4481 {
4482#if 0
4483 $$ = literal_concat(p, $1, $2, &@$);
4484#endif
4485 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(word_add,v1,v2);$$=v3;}
4486 }
4487 ;
4488
4489symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4490 {
4491#if 0
4492 $$ = make_list($3, &@$);
4493#endif
4494 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4495 }
4496 ;
4497
4498symbol_list : /* none */
4499 {
4500#if 0
4501 $$ = 0;
4502#endif
4503 {VALUE v1;v1=dispatch0(symbols_new);$$=v1;}
4504 }
4505 | symbol_list word ' '
4506 {
4507#if 0
4508 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4509#endif
4510 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(symbols_add,v1,v2);$$=v3;}
4511 }
4512 ;
4513
4514qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4515 {
4516#if 0
4517 $$ = make_list($3, &@$);
4518#endif
4519 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4520 }
4521 ;
4522
4523qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4524 {
4525#if 0
4526 $$ = make_list($3, &@$);
4527#endif
4528 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4529 }
4530 ;
4531
4532qword_list : /* none */
4533 {
4534#if 0
4535 $$ = 0;
4536#endif
4537 {VALUE v1;v1=dispatch0(qwords_new);$$=v1;}
4538 }
4539 | qword_list tSTRING_CONTENT ' '
4540 {
4541#if 0
4542 $$ = list_append(p, $1, $2);
4543#endif
4544 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qwords_add,v1,v2);$$=v3;}
4545 }
4546 ;
4547
4548qsym_list : /* none */
4549 {
4550#if 0
4551 $$ = 0;
4552#endif
4553 {VALUE v1;v1=dispatch0(qsymbols_new);$$=v1;}
4554 }
4555 | qsym_list tSTRING_CONTENT ' '
4556 {
4557#if 0
4558 $$ = symbol_append(p, $1, $2);
4559#endif
4560 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qsymbols_add,v1,v2);$$=v3;}
4561 }
4562 ;
4563
4564string_contents : /* none */
4565 {
4566#if 0
4567 $$ = 0;
4568#endif
4569 {VALUE v1;v1=dispatch0(string_content);$$=v1;}
4570#if 0
4571#endif
4572 $$ = ripper_new_yylval(p, 0, $$, 0);
4573
4574 }
4575 | string_contents string_content
4576 {
4577#if 0
4578 $$ = literal_concat(p, $1, $2, &@$);
4579#endif
4580 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_add,v1,v2);$$=v3;}
4581#if 0
4582#endif
4583 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
4584 !RNODE($1)->nd_cval) {
4585 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
4586 RNODE($1)->nd_rval = add_mark_object(p, $$);
4587 $$ = $1;
4588 }
4589
4590 }
4591 ;
4592
4593xstring_contents: /* none */
4594 {
4595#if 0
4596 $$ = 0;
4597#endif
4598 {VALUE v1;v1=dispatch0(xstring_new);$$=v1;}
4599 }
4600 | xstring_contents string_content
4601 {
4602#if 0
4603 $$ = literal_concat(p, $1, $2, &@$);
4604#endif
4605 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(xstring_add,v1,v2);$$=v3;}
4606 }
4607 ;
4608
4609regexp_contents: /* none */
4610 {
4611#if 0
4612 $$ = 0;
4613#endif
4614 {VALUE v1;v1=dispatch0(regexp_new);$$=v1;}
4615#if 0
4616#endif
4617 $$ = ripper_new_yylval(p, 0, $$, 0);
4618
4619 }
4620 | regexp_contents string_content
4621 {
4622#if 0
4623 NODE *head = $1, *tail = $2;
4624 if (!head) {
4625 $$ = tail;
4626 }
4627 else if (!tail) {
4628 $$ = head;
4629 }
4630 else {
4631 switch (nd_type(head)) {
4632 case NODE_STR:
4633 nd_set_type(head, NODE_DSTR);
4634 break;
4635 case NODE_DSTR:
4636 break;
4637 default:
4638 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
4639 break;
4640 }
4641 $$ = list_append(p, head, tail);
4642 }
4643#endif
4644 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4645 if (ripper_is_node_yylval(n1)) {
4646 s1 = RNODE(n1)->nd_cval;
4647 n1 = RNODE(n1)->nd_rval;
4648 }
4649 if (ripper_is_node_yylval(n2)) {
4650 s2 = RNODE(n2)->nd_cval;
4651 n2 = RNODE(n2)->nd_rval;
4652 }
4653 $$ = dispatch2(regexp_add, n1, n2);
4654 if (!s1 && s2) {
4655 $$ = ripper_new_yylval(p, 0, $$, s2);
4656 }
4657
4658 }
4659 ;
4660
4661string_content : tSTRING_CONTENT
4662 {$$=ripper_new_yylval(p, 0, get_value($1), $1);}
4663 | tSTRING_DVAR
4664 {
4665 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
4666 $<strterm>$ = p->lex.strterm;
4667 p->lex.strterm = 0;
4668 SET_LEX_STATE(EXPR_BEG);
4669 }
4670 string_dvar
4671 {
4672 p->lex.strterm = $<strterm>2;
4673#if 0
4674 $$ = NEW_EVSTR($3, &@$);
4675 nd_set_line($$, @3.end_pos.lineno);
4676#endif
4677 {VALUE v1,v2;v1=$3;v2=dispatch1(string_dvar,v1);$$=v2;}
4678 }
4679 | tSTRING_DBEG
4680 {
4681 CMDARG_PUSH(0);
4682 COND_PUSH(0);
4683 }
4684 {
4685 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
4686 $<strterm>$ = p->lex.strterm;
4687 p->lex.strterm = 0;
4688 }
4689 {
4690 $<num>$ = p->lex.state;
4691 SET_LEX_STATE(EXPR_BEG);
4692 }
4693 {
4694 $<num>$ = p->lex.brace_nest;
4695 p->lex.brace_nest = 0;
4696 }
4697 {
4698 $<num>$ = p->heredoc_indent;
4699 p->heredoc_indent = 0;
4700 }
4701 compstmt tSTRING_DEND
4702 {
4703 COND_POP();
4704 CMDARG_POP();
4705 p->lex.strterm = $<strterm>3;
4706 SET_LEX_STATE($<num>4);
4707 p->lex.brace_nest = $<num>5;
4708 p->heredoc_indent = $<num>6;
4709 p->heredoc_line_indent = -1;
4710#if 0
4711 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4712 $$ = new_evstr(p, $7, &@$);
4713#endif
4714 {VALUE v1,v2;v1=$7;v2=dispatch1(string_embexpr,v1);$$=v2;}
4715 }
4716 ;
4717
4718string_dvar : tGVAR
4719 {
4720#if 0
4721 $$ = NEW_GVAR($1, &@$);
4722#endif
4723 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4724 }
4725 | tIVAR
4726 {
4727#if 0
4728 $$ = NEW_IVAR($1, &@$);
4729#endif
4730 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4731 }
4732 | tCVAR
4733 {
4734#if 0
4735 $$ = NEW_CVAR($1, &@$);
4736#endif
4737 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4738 }
4739 | backref
4740 ;
4741
4742symbol : ssym
4743 | dsym
4744 ;
4745
4746ssym : tSYMBEG sym
4747 {
4748 SET_LEX_STATE(EXPR_END);
4749#if 0
4750 $$ = NEW_LIT(ID2SYM($2), &@$);
4751#endif
4752 {VALUE v1,v2,v3,v4;v1=$2;v2=dispatch1(symbol,v1);v3=v2;v4=dispatch1(symbol_literal,v3);$$=v4;}
4753 }
4754 ;
4755
4756sym : fname
4757 | tIVAR
4758 | tGVAR
4759 | tCVAR
4760 ;
4761
4762dsym : tSYMBEG string_contents tSTRING_END
4763 {
4764 SET_LEX_STATE(EXPR_END);
4765#if 0
4766 $$ = dsym_node(p, $2, &@$);
4767#endif
4768 {VALUE v1,v2;v1=$2;v2=dispatch1(dyna_symbol,v1);$$=v2;}
4769 }
4770 ;
4771
4772numeric : simple_numeric
4773 | tUMINUS_NUM simple_numeric %prec tLOWEST
4774 {
4775#if 0
4776 $$ = $2;
4777 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
4778#endif
4779 {VALUE v1,v2,v3;v1=ID2VAL(idUMinus);v2=$2;v3=dispatch2(unary,v1,v2);$$=v3;}
4780 }
4781 ;
4782
4783simple_numeric : tINTEGER
4784 | tFLOAT
4785 | tRATIONAL
4786 | tIMAGINARY
4787 ;
4788
4789user_variable : tIDENTIFIER
4790 | tIVAR
4791 | tGVAR
4792 | tCONSTANT
4793 | tCVAR
4794 ;
4795
4796keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
4797 | keyword_self {$$ = KWD2EID(self, $1);}
4798 | keyword_true {$$ = KWD2EID(true, $1);}
4799 | keyword_false {$$ = KWD2EID(false, $1);}
4800 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
4801 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
4802 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
4803 ;
4804
4805var_ref : user_variable
4806 {
4807#if 0
4808 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4809#endif
4810 if (id_is_var(p, get_id($1))) {
4811 $$ = dispatch1(var_ref, $1);
4812 }
4813 else {
4814 $$ = dispatch1(vcall, $1);
4815 }
4816
4817 }
4818 | keyword_variable
4819 {
4820#if 0
4821 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4822#endif
4823 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4824 }
4825 ;
4826
4827var_lhs : user_variable
4828 {
4829#if 0
4830 $$ = assignable(p, $1, 0, &@$);
4831#endif
4832 $$=assignable(p, var_field(p, $1));
4833 }
4834 | keyword_variable
4835 {
4836#if 0
4837 $$ = assignable(p, $1, 0, &@$);
4838#endif
4839 $$=assignable(p, var_field(p, $1));
4840 }
4841 ;
4842
4843backref : tNTH_REF
4844 | tBACK_REF
4845 ;
4846
4847superclass : '<'
4848 {
4849 SET_LEX_STATE(EXPR_BEG);
4850 p->command_start = TRUE;
4851 }
4852 expr_value term
4853 {
4854 $$ = $3;
4855 }
4856 | /* none */
4857 {
4858#if 0
4859 $$ = 0;
4860#endif
4861 $$=Qnil;
4862 }
4863 ;
4864
4865f_arglist : '(' f_args rparen
4866 {
4867#if 0
4868 $$ = $2;
4869#endif
4870 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
4871 SET_LEX_STATE(EXPR_BEG);
4872 p->command_start = TRUE;
4873 }
4874 | '(' f_arg ',' args_forward rparen
4875 {
4876 arg_var(p, idFWD_REST);
4877#if idFWD_KWREST
4878 arg_var(p, idFWD_KWREST);
4879#endif
4880 arg_var(p, idFWD_BLOCK);
4881#if 0
4882 $$ = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, &@4);
4883 $$ = new_args(p, $2, Qnone, idFWD_REST, Qnone, $$, &@4);
4884#endif
4885 {VALUE v1,v2;v1=params_new($2, Qnone, $4, Qnone, Qnone, Qnone, Qnone);v2=dispatch1(paren,v1);$$=v2;}
4886 SET_LEX_STATE(EXPR_BEG);
4887 p->command_start = TRUE;
4888 }
4889 | '(' args_forward rparen
4890 {
4891 arg_var(p, idFWD_REST);
4892#if idFWD_KWREST
4893 arg_var(p, idFWD_KWREST);
4894#endif
4895 arg_var(p, idFWD_BLOCK);
4896#if 0
4897 $$ = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, &@2);
4898 $$ = new_args(p, Qnone, Qnone, idFWD_REST, Qnone, $$, &@2);
4899#endif
4900 {VALUE v1,v2;v1=params_new(Qnone, Qnone, $2, Qnone, Qnone, Qnone, Qnone);v2=dispatch1(paren,v1);$$=v2;}
4901 SET_LEX_STATE(EXPR_BEG);
4902 p->command_start = TRUE;
4903 }
4904 | {
4905 $<num>$ = p->in_kwarg;
4906 p->in_kwarg = 1;
4907 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
4908 }
4909 f_args term
4910 {
4911 p->in_kwarg = !!$<num>1;
4912 $$ = $2;
4913 SET_LEX_STATE(EXPR_BEG);
4914 p->command_start = TRUE;
4915 }
4916 ;
4917
4918args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
4919 {
4920 $$ = new_args_tail(p, $1, $3, $4, &@3);
4921 }
4922 | f_kwarg opt_f_block_arg
4923 {
4924 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
4925 }
4926 | f_kwrest opt_f_block_arg
4927 {
4928 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
4929 }
4930 | f_no_kwarg opt_f_block_arg
4931 {
4932 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
4933 }
4934 | f_block_arg
4935 {
4936 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
4937 }
4938 ;
4939
4940opt_args_tail : ',' args_tail
4941 {
4942 $$ = $2;
4943 }
4944 | /* none */
4945 {
4946 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4947 }
4948 ;
4949
4950f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
4951 {
4952 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
4953 }
4954 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4955 {
4956 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4957 }
4958 | f_arg ',' f_optarg opt_args_tail
4959 {
4960 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
4961 }
4962 | f_arg ',' f_optarg ',' f_arg opt_args_tail
4963 {
4964 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4965 }
4966 | f_arg ',' f_rest_arg opt_args_tail
4967 {
4968 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4969 }
4970 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
4971 {
4972 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4973 }
4974 | f_arg opt_args_tail
4975 {
4976 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4977 }
4978 | f_optarg ',' f_rest_arg opt_args_tail
4979 {
4980 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4981 }
4982 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4983 {
4984 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4985 }
4986 | f_optarg opt_args_tail
4987 {
4988 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4989 }
4990 | f_optarg ',' f_arg opt_args_tail
4991 {
4992 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4993 }
4994 | f_rest_arg opt_args_tail
4995 {
4996 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4997 }
4998 | f_rest_arg ',' f_arg opt_args_tail
4999 {
5000 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
5001 }
5002 | args_tail
5003 {
5004 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
5005 }
5006 | /* none */
5007 {
5008 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5009 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5010 }
5011 ;
5012
5013args_forward : tBDOT3
5014 {
5015#if 0
5016 $$ = idDot3;
5017#endif
5018 {VALUE v1;v1=dispatch0(args_forward);$$=v1;}
5019 }
5020 ;
5021
5022f_bad_arg : tCONSTANT
5023 {
5024#if 0
5025 yyerror1(&@1, "formal argument cannot be a constant");
5026 $$ = 0;
5027#endif
5028 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
5029 }
5030 | tIVAR
5031 {
5032#if 0
5033 yyerror1(&@1, "formal argument cannot be an instance variable");
5034 $$ = 0;
5035#endif
5036 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
5037 }
5038 | tGVAR
5039 {
5040#if 0
5041 yyerror1(&@1, "formal argument cannot be a global variable");
5042 $$ = 0;
5043#endif
5044 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
5045 }
5046 | tCVAR
5047 {
5048#if 0
5049 yyerror1(&@1, "formal argument cannot be a class variable");
5050 $$ = 0;
5051#endif
5052 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
5053 }
5054 ;
5055
5056f_norm_arg : f_bad_arg
5057 | tIDENTIFIER
5058 {
5059 formal_argument(p, get_id($1));
5060 p->max_numparam = ORDINAL_PARAM;
5061 $$ = $1;
5062 }
5063 ;
5064
5065f_arg_asgn : f_norm_arg
5066 {
5067 ID id = get_id($1);
5068 arg_var(p, id);
5069 p->cur_arg = id;
5070 $$ = $1;
5071 }
5072 ;
5073
5074f_arg_item : f_arg_asgn
5075 {
5076 p->cur_arg = 0;
5077#if 0
5078 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5079#endif
5080 $$=get_value($1);
5081 }
5082 | tLPAREN f_margs rparen
5083 {
5084#if 0
5085 ID tid = internal_id(p);
5086 YYLTYPE loc;
5087 loc.beg_pos = @2.beg_pos;
5088 loc.end_pos = @2.beg_pos;
5089 arg_var(p, tid);
5090 if (dyna_in_block(p)) {
5091 $2->nd_value = NEW_DVAR(tid, &loc);
5092 }
5093 else {
5094 $2->nd_value = NEW_LVAR(tid, &loc);
5095 }
5096 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5097 $$->nd_next = $2;
5098#endif
5099 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
5100 }
5101 ;
5102
5103f_arg : f_arg_item
5104 {$$=rb_ary_new3(1, get_value($1));}
5105 | f_arg ',' f_arg_item
5106 {
5107#if 0
5108 $$ = $1;
5109 $$->nd_plen++;
5110 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5111 rb_discard_node(p, $3);
5112#endif
5113 $$=rb_ary_push($1, get_value($3));
5114 }
5115 ;
5116
5117
5118f_label : tLABEL
5119 {
5120 ID id = get_id($1);
5121 arg_var(p, formal_argument(p, id));
5122 p->cur_arg = id;
5123 p->max_numparam = ORDINAL_PARAM;
5124 $$ = $1;
5125 }
5126 ;
5127
5128f_kw : f_label arg_value
5129 {
5130 p->cur_arg = 0;
5131#if 0
5132 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5133#endif
5134 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5135 }
5136 | f_label
5137 {
5138 p->cur_arg = 0;
5139#if 0
5140 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5141#endif
5142 $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5143 }
5144 ;
5145
5146f_block_kw : f_label primary_value
5147 {
5148#if 0
5149 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5150#endif
5151 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5152 }
5153 | f_label
5154 {
5155#if 0
5156 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5157#endif
5158 $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5159 }
5160 ;
5161
5162f_block_kwarg : f_block_kw
5163 {
5164#if 0
5165 $$ = $1;
5166#endif
5167 $$=rb_ary_new3(1, get_value($1));
5168 }
5169 | f_block_kwarg ',' f_block_kw
5170 {
5171#if 0
5172 $$ = kwd_append($1, $3);
5173#endif
5174 $$=rb_ary_push($1, get_value($3));
5175 }
5176 ;
5177
5178
5179f_kwarg : f_kw
5180 {
5181#if 0
5182 $$ = $1;
5183#endif
5184 $$=rb_ary_new3(1, get_value($1));
5185 }
5186 | f_kwarg ',' f_kw
5187 {
5188#if 0
5189 $$ = kwd_append($1, $3);
5190#endif
5191 $$=rb_ary_push($1, get_value($3));
5192 }
5193 ;
5194
5195kwrest_mark : tPOW
5196 | tDSTAR
5197 ;
5198
5199f_no_kwarg : kwrest_mark keyword_nil
5200 {
5201#if 0
5202#endif
5203 {VALUE v1,v2;v1=Qnil;v2=dispatch1(nokw_param,v1);$$=v2;}
5204 }
5205 ;
5206
5207f_kwrest : kwrest_mark tIDENTIFIER
5208 {
5209 arg_var(p, shadowing_lvar(p, get_id($2)));
5210#if 0
5211 $$ = $2;
5212#endif
5213 {VALUE v1,v2;v1=$2;v2=dispatch1(kwrest_param,v1);$$=v2;}
5214 }
5215 | kwrest_mark
5216 {
5217#if 0
5218 $$ = internal_id(p);
5219 arg_var(p, $$);
5220#endif
5221 {VALUE v1,v2;v1=Qnil;v2=dispatch1(kwrest_param,v1);$$=v2;}
5222 }
5223 ;
5224
5225f_opt : f_arg_asgn '=' arg_value
5226 {
5227 p->cur_arg = 0;
5228#if 0
5229 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5230#endif
5231 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5232 }
5233 ;
5234
5235f_block_opt : f_arg_asgn '=' primary_value
5236 {
5237 p->cur_arg = 0;
5238#if 0
5239 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5240#endif
5241 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5242 }
5243 ;
5244
5245f_block_optarg : f_block_opt
5246 {
5247#if 0
5248 $$ = $1;
5249#endif
5250 $$=rb_ary_new3(1, get_value($1));
5251 }
5252 | f_block_optarg ',' f_block_opt
5253 {
5254#if 0
5255 $$ = opt_arg_append($1, $3);
5256#endif
5257 $$=rb_ary_push($1, get_value($3));
5258 }
5259 ;
5260
5261f_optarg : f_opt
5262 {
5263#if 0
5264 $$ = $1;
5265#endif
5266 $$=rb_ary_new3(1, get_value($1));
5267 }
5268 | f_optarg ',' f_opt
5269 {
5270#if 0
5271 $$ = opt_arg_append($1, $3);
5272#endif
5273 $$=rb_ary_push($1, get_value($3));
5274 }
5275 ;
5276
5277restarg_mark : '*'
5278 | tSTAR
5279 ;
5280
5281f_rest_arg : restarg_mark tIDENTIFIER
5282 {
5283 arg_var(p, shadowing_lvar(p, get_id($2)));
5284#if 0
5285 $$ = $2;
5286#endif
5287 {VALUE v1,v2;v1=$2;v2=dispatch1(rest_param,v1);$$=v2;}
5288 }
5289 | restarg_mark
5290 {
5291#if 0
5292 $$ = internal_id(p);
5293 arg_var(p, $$);
5294#endif
5295 {VALUE v1,v2;v1=Qnil;v2=dispatch1(rest_param,v1);$$=v2;}
5296 }
5297 ;
5298
5299blkarg_mark : '&'
5300 | tAMPER
5301 ;
5302
5303f_block_arg : blkarg_mark tIDENTIFIER
5304 {
5305 arg_var(p, shadowing_lvar(p, get_id($2)));
5306#if 0
5307 $$ = $2;
5308#endif
5309 {VALUE v1,v2;v1=$2;v2=dispatch1(blockarg,v1);$$=v2;}
5310 }
5311 ;
5312
5313opt_f_block_arg : ',' f_block_arg
5314 {
5315 $$ = $2;
5316 }
5317 | none
5318 {
5319 $$ = Qnull;
5320 }
5321 ;
5322
5323singleton : var_ref
5324 {
5325 value_expr($1);
5326 $$ = $1;
5327 }
5328 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5329 {
5330#if 0
5331 switch (nd_type($3)) {
5332 case NODE_STR:
5333 case NODE_DSTR:
5334 case NODE_XSTR:
5335 case NODE_DXSTR:
5336 case NODE_DREGX:
5337 case NODE_LIT:
5338 case NODE_LIST:
5339 case NODE_ZLIST:
5340 yyerror1(&@3, "can't define singleton method for literals");
5341 break;
5342 default:
5343 value_expr($3);
5344 break;
5345 }
5346 $$ = $3;
5347#endif
5348 {VALUE v1,v2;v1=$3;v2=dispatch1(paren,v1);$$=v2;}
5349 }
5350 ;
5351
5352assoc_list : none
5353 | assocs trailer
5354 {
5355#if 0
5356 $$ = $1;
5357#endif
5358 {VALUE v1,v2;v1=$1;v2=dispatch1(assoclist_from_args,v1);$$=v2;}
5359 }
5360 ;
5361
5362assocs : assoc
5363 {$$=rb_ary_new3(1, get_value($1));}
5364 | assocs ',' assoc
5365 {
5366#if 0
5367 NODE *assocs = $1;
5368 NODE *tail = $3;
5369 if (!assocs) {
5370 assocs = tail;
5371 }
5372 else if (tail) {
5373 if (assocs->nd_head &&
5374 !tail->nd_head && nd_type(tail->nd_next) == NODE_LIST &&
5375 nd_type(tail->nd_next->nd_head) == NODE_HASH) {
5376 /* DSTAR */
5377 tail = tail->nd_next->nd_head->nd_head;
5378 }
5379 assocs = list_concat(assocs, tail);
5380 }
5381 $$ = assocs;
5382#endif
5383 $$=rb_ary_push($1, get_value($3));
5384 }
5385 ;
5386
5387assoc : arg_value tASSOC arg_value
5388 {
5389#if 0
5390 if (nd_type($1) == NODE_STR) {
5391 nd_set_type($1, NODE_LIT);
5392 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
5393 }
5394 $$ = list_append(p, NEW_LIST($1, &@$), $3);
5395#endif
5396 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5397 }
5398 | tLABEL arg_value
5399 {
5400#if 0
5401 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5402#endif
5403 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5404 }
5405 | tSTRING_BEG string_contents tLABEL_END arg_value
5406 {
5407#if 0
5408 YYLTYPE loc = code_loc_gen(&@1, &@3);
5409 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
5410#endif
5411 {VALUE v1,v2,v3,v4,v5;v1=$2;v2=dispatch1(dyna_symbol,v1);v3=v2;v4=$4;v5=dispatch2(assoc_new,v3,v4);$$=v5;}
5412 }
5413 | tDSTAR arg_value
5414 {
5415#if 0
5416 if (nd_type($2) == NODE_HASH &&
5417 !($2->nd_head && $2->nd_head->nd_alen)) {
5418 static VALUE empty_hash;
5419 if (!empty_hash) {
5420 empty_hash = rb_obj_freeze(rb_hash_new());
5421 rb_gc_register_mark_object(empty_hash);
5422 }
5423 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
5424 }
5425 else
5426 $$ = list_append(p, NEW_LIST(0, &@$), $2);
5427#endif
5428 {VALUE v1,v2;v1=$2;v2=dispatch1(assoc_splat,v1);$$=v2;}
5429 }
5430 ;
5431
5432operation : tIDENTIFIER
5433 | tCONSTANT
5434 | tFID
5435 ;
5436
5437operation2 : tIDENTIFIER
5438 | tCONSTANT
5439 | tFID
5440 | op
5441 ;
5442
5443operation3 : tIDENTIFIER
5444 | tFID
5445 | op
5446 ;
5447
5448dot_or_colon : '.'
5449 | tCOLON2
5450 ;
5451
5452call_op : '.'
5453 | tANDDOT
5454 ;
5455
5456call_op2 : call_op
5457 | tCOLON2
5458 ;
5459
5460opt_terms : /* none */
5461 | terms
5462 ;
5463
5464opt_nl : /* none */
5465 | '\n'
5466 ;
5467
5468rparen : opt_nl ')'
5469 ;
5470
5471rbracket : opt_nl ']'
5472 ;
5473
5474rbrace : opt_nl '}'
5475 ;
5476
5477trailer : /* none */
5478 | '\n'
5479 | ','
5480 ;
5481
5482term : ';' {yyerrok;token_flush(p);}
5483 | '\n' {token_flush(p);}
5484 ;
5485
5486terms : term
5487 | terms ';' {yyerrok;}
5488 ;
5489
5490none : /* none */
5491 {
5492 $$ = Qnull;
5493 }
5494 ;
5495%%
5496# undef p
5497# undef yylex
5498# undef yylval
5499# define yylval (*p->lval)
5500
5501static int regx_options(struct parser_params*);
5502static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5503static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5504static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5505static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
5506
5507#ifndef RIPPER
5508# define set_yylval_node(x) { \
5509 YYLTYPE _cur_loc; \
5510 rb_parser_set_location(p, &_cur_loc); \
5511 yylval.node = (x); \
5512}
5513# define set_yylval_str(x) \
5514do { \
5515 set_yylval_node(NEW_STR(x, &_cur_loc)); \
5516 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5517} while(0)
5518# define set_yylval_literal(x) \
5519do { \
5520 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
5521 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5522} while(0)
5523# define set_yylval_num(x) (yylval.num = (x))
5524# define set_yylval_id(x) (yylval.id = (x))
5525# define set_yylval_name(x) (yylval.id = (x))
5526# define yylval_id() (yylval.id)
5527#else
5528static inline VALUE
5529ripper_yylval_id(struct parser_params *p, ID x)
5530{
5531 return ripper_new_yylval(p, x, ID2SYM(x), 0);
5532}
5533# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
5534# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
5535# define set_yylval_id(x) (void)(x)
5536# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
5537# define set_yylval_literal(x) add_mark_object(p, (x))
5538# define set_yylval_node(x) (void)(x)
5539# define yylval_id() yylval.id
5540# define _cur_loc NULL_LOC /* dummy */
5541#endif
5542
5543#define set_yylval_noname() set_yylval_id(keyword_nil)
5544
5545#ifndef RIPPER
5546#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
5547#define dispatch_scan_event(p, t) ((void)0)
5548#define dispatch_delayed_token(p, t) ((void)0)
5549#define has_delayed_token(p) (0)
5550#else
5551#define literal_flush(p, ptr) ((void)(ptr))
5552
5553#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5554
5555static inline VALUE
5556intern_sym(const char *name)
5557{
5558 ID id = rb_intern_const(name);
5559 return ID2SYM(id);
5560}
5561
5562static int
5563ripper_has_scan_event(struct parser_params *p)
5564{
5565 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
5566 return p->lex.pcur > p->lex.ptok;
5567}
5568
5569static VALUE
5570ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
5571{
5572 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
5573 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
5574 token_flush(p);
5575 return rval;
5576}
5577
5578static void
5579ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
5580{
5581 if (!ripper_has_scan_event(p)) return;
5582 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
5583}
5584#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
5585
5586static void
5587ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
5588{
5589 int saved_line = p->ruby_sourceline;
5590 const char *saved_tokp = p->lex.ptok;
5591
5592 if (NIL_P(p->delayed.token)) return;
5593 p->ruby_sourceline = p->delayed.line;
5594 p->lex.ptok = p->lex.pbeg + p->delayed.col;
5595 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
5596 p->delayed.token = Qnil;
5597 p->ruby_sourceline = saved_line;
5598 p->lex.ptok = saved_tokp;
5599}
5600#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
5601#define has_delayed_token(p) (!NIL_P(p->delayed.token))
5602#endif /* RIPPER */
5603
5604#include "ruby/regex.h"
5605#include "ruby/util.h"
5606
5607static inline int
5608is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
5609{
5610 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
5611}
5612
5613static inline int
5614parser_is_identchar(struct parser_params *p)
5615{
5616 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5617}
5618
5619static inline int
5620parser_isascii(struct parser_params *p)
5621{
5622 return ISASCII(*(p->lex.pcur-1));
5623}
5624
5625static void
5626token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
5627{
5628 int column = 1, nonspc = 0, i;
5629 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
5630 if (*ptr == '\t') {
5631 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5632 }
5633 column++;
5634 if (*ptr != ' ' && *ptr != '\t') {
5635 nonspc = 1;
5636 }
5637 }
5638
5639 ptinfo->beg = loc->beg_pos;
5640 ptinfo->indent = column;
5641 ptinfo->nonspc = nonspc;
5642}
5643
5644static void
5645token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5646{
5647 token_info *ptinfo;
5648
5649 if (!p->token_info_enabled) return;
5650 ptinfo = ALLOC(token_info);
5651 ptinfo->token = token;
5652 ptinfo->next = p->token_info;
5653 token_info_setup(ptinfo, p->lex.pbeg, loc);
5654
5655 p->token_info = ptinfo;
5656}
5657
5658static void
5659token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5660{
5661 token_info *ptinfo_beg = p->token_info;
5662
5663 if (!ptinfo_beg) return;
5664 p->token_info = ptinfo_beg->next;
5665
5666 /* indentation check of matched keywords (begin..end, if..end, etc.) */
5667 token_info_warn(p, token, ptinfo_beg, 1, loc);
5668 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5669}
5670
5671static void
5672token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
5673{
5674 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
5675 if (!p->token_info_enabled) return;
5676 if (!ptinfo_beg) return;
5677 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
5678 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
5679 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
5680 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
5681 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
5682 rb_warn3L(ptinfo_end->beg.lineno,
5683 "mismatched indentations at '%s' with '%s' at %d",
5684 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
5685}
5686
5687static int
5688parser_precise_mbclen(struct parser_params *p, const char *ptr)
5689{
5690 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
5691 if (!MBCLEN_CHARFOUND_P(len)) {
5692 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
5693 return -1;
5694 }
5695 return len;
5696}
5697
5698#ifndef RIPPER
5699static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
5700
5701static inline void
5702parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5703{
5704 VALUE str;
5705 int lineno = p->ruby_sourceline;
5706 if (!yylloc) {
5707 return;
5708 }
5709 else if (yylloc->beg_pos.lineno == lineno) {
5710 str = p->lex.lastline;
5711 }
5712 else {
5713 return;
5714 }
5715 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
5716}
5717
5718static int
5719parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5720{
5721 YYLTYPE current;
5722
5723 if (!yylloc) {
5724 yylloc = RUBY_SET_YYLLOC(current);
5725 }
5726 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
5727 p->ruby_sourceline != yylloc->end_pos.lineno) ||
5728 (yylloc->beg_pos.lineno == yylloc->end_pos.lineno &&
5729 yylloc->beg_pos.column == yylloc->end_pos.column)) {
5730 yylloc = 0;
5731 }
5732 compile_error(p, "%s", msg);
5733 parser_show_error_line(p, yylloc);
5734 return 0;
5735}
5736
5737static void
5738ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
5739{
5740 VALUE mesg;
5741 const int max_line_margin = 30;
5742 const char *ptr, *ptr_end, *pt, *pb;
5743 const char *pre = "", *post = "", *pend;
5744 const char *code = "", *caret = "";
5745 const char *lim;
5746 const char *const pbeg = RSTRING_PTR(str);
5747 char *buf;
5748 long len;
5749 int i;
5750
5751 if (!yylloc) return;
5752 pend = RSTRING_END(str);
5753 if (pend > pbeg && pend[-1] == '\n') {
5754 if (--pend > pbeg && pend[-1] == '\r') --pend;
5755 }
5756
5757 pt = pend;
5758 if (lineno == yylloc->end_pos.lineno &&
5759 (pend - pbeg) > yylloc->end_pos.column) {
5760 pt = pbeg + yylloc->end_pos.column;
5761 }
5762
5763 ptr = ptr_end = pt;
5764 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
5765 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
5766
5767 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
5768 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
5769
5770 len = ptr_end - ptr;
5771 if (len > 4) {
5772 if (ptr > pbeg) {
5773 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
5774 if (ptr > pbeg) pre = "...";
5775 }
5776 if (ptr_end < pend) {
5777 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
5778 if (ptr_end < pend) post = "...";
5779 }
5780 }
5781 pb = pbeg;
5782 if (lineno == yylloc->beg_pos.lineno) {
5783 pb += yylloc->beg_pos.column;
5784 if (pb > pt) pb = pt;
5785 }
5786 if (pb < ptr) pb = ptr;
5787 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
5788 return;
5789 }
5790 if (RTEST(errbuf)) {
5791 mesg = rb_attr_get(errbuf, idMesg);
5792 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
5793 rb_str_cat_cstr(mesg, "\n");
5794 }
5795 else {
5796 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
5797 }
5798 if (!errbuf && rb_stderr_tty_p()) {
5799#define CSI_BEGIN "\033["
5800#define CSI_SGR "m"
5801 rb_str_catf(mesg,
5802 CSI_BEGIN""CSI_SGR"%s" /* pre */
5803 CSI_BEGIN"1"CSI_SGR"%.*s"
5804 CSI_BEGIN"1;4"CSI_SGR"%.*s"
5805 CSI_BEGIN";1"CSI_SGR"%.*s"
5806 CSI_BEGIN""CSI_SGR"%s" /* post */
5807 "\n",
5808 pre,
5809 (int)(pb - ptr), ptr,
5810 (int)(pt - pb), pb,
5811 (int)(ptr_end - pt), pt,
5812 post);
5813 }
5814 else {
5815 char *p2;
5816
5817 len = ptr_end - ptr;
5818 lim = pt < pend ? pt : pend;
5819 i = (int)(lim - ptr);
5820 buf = ALLOCA_N(char, i+2);
5821 code = ptr;
5822 caret = p2 = buf;
5823 if (ptr <= pb) {
5824 while (ptr < pb) {
5825 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
5826 }
5827 *p2++ = '^';
5828 ptr++;
5829 }
5830 if (lim > ptr) {
5831 memset(p2, '~', (lim - ptr));
5832 p2 += (lim - ptr);
5833 }
5834 *p2 = '\0';
5835 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
5836 pre, (int)len, code, post,
5837 pre, caret);
5838 }
5839 if (!errbuf) rb_write_error_str(mesg);
5840}
5841#else
5842static int
5843parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5844{
5845 const char *pcur = 0, *ptok = 0;
5846 if (yylloc &&
5847 p->ruby_sourceline == yylloc->beg_pos.lineno &&
5848 p->ruby_sourceline == yylloc->end_pos.lineno) {
5849 pcur = p->lex.pcur;
5850 ptok = p->lex.ptok;
5851 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
5852 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
5853 }
5854 dispatch1(parse_error, STR_NEW2(msg));
5855 ripper_error(p);
5856 if (pcur) {
5857 p->lex.ptok = ptok;
5858 p->lex.pcur = pcur;
5859 }
5860 return 0;
5861}
5862
5863static inline void
5864parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5865{
5866}
5867#endif /* !RIPPER */
5868
5869#ifndef RIPPER
5870static int
5871vtable_size(const struct vtable *tbl)
5872{
5873 if (!DVARS_TERMINAL_P(tbl)) {
5874 return tbl->pos;
5875 }
5876 else {
5877 return 0;
5878 }
5879}
5880#endif
5881
5882static struct vtable *
5883vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
5884{
5885 struct vtable *tbl = ALLOC(struct vtable);
5886 tbl->pos = 0;
5887 tbl->capa = 8;
5888 tbl->tbl = ALLOC_N(ID, tbl->capa);
5889 tbl->prev = prev;
5890#ifndef RIPPER
5891 if (p->debug) {
5892 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
5893 }
5894#endif
5895 return tbl;
5896}
5897#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
5898
5899static void
5900vtable_free_gen(struct parser_params *p, int line, const char *name,
5901 struct vtable *tbl)
5902{
5903#ifndef RIPPER
5904 if (p->debug) {
5905 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
5906 }
5907#endif
5908 if (!DVARS_TERMINAL_P(tbl)) {
5909 if (tbl->tbl) {
5910 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
5911 }
5912 ruby_sized_xfree(tbl, sizeof(tbl));
5913 }
5914}
5915#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
5916
5917static void
5918vtable_add_gen(struct parser_params *p, int line, const char *name,
5919 struct vtable *tbl, ID id)
5920{
5921#ifndef RIPPER
5922 if (p->debug) {
5923 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
5924 line, name, (void *)tbl, rb_id2name(id));
5925 }
5926#endif
5927 if (DVARS_TERMINAL_P(tbl)) {
5928 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
5929 return;
5930 }
5931 if (tbl->pos == tbl->capa) {
5932 tbl->capa = tbl->capa * 2;
5933 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
5934 }
5935 tbl->tbl[tbl->pos++] = id;
5936}
5937#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
5938
5939#ifndef RIPPER
5940static void
5941vtable_pop_gen(struct parser_params *p, int line, const char *name,
5942 struct vtable *tbl, int n)
5943{
5944 if (p->debug) {
5945 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
5946 line, name, (void *)tbl, n);
5947 }
5948 if (tbl->pos < n) {
5949 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
5950 return;
5951 }
5952 tbl->pos -= n;
5953}
5954#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
5955#endif
5956
5957static int
5958vtable_included(const struct vtable * tbl, ID id)
5959{
5960 int i;
5961
5962 if (!DVARS_TERMINAL_P(tbl)) {
5963 for (i = 0; i < tbl->pos; i++) {
5964 if (tbl->tbl[i] == id) {
5965 return i+1;
5966 }
5967 }
5968 }
5969 return 0;
5970}
5971
5972static void parser_prepare(struct parser_params *p);
5973
5974#ifndef RIPPER
5975static NODE *parser_append_options(struct parser_params *p, NODE *node);
5976
5977static VALUE
5978debug_lines(VALUE fname)
5979{
5980 ID script_lines;
5981 CONST_ID(script_lines, "SCRIPT_LINES__");
5982 if (rb_const_defined_at(rb_cObject, script_lines)) {
5983 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
5984 if (RB_TYPE_P(hash, T_HASH)) {
5985 VALUE lines = rb_ary_new();
5986 rb_hash_aset(hash, fname, lines);
5987 return lines;
5988 }
5989 }
5990 return 0;
5991}
5992
5993static int
5994e_option_supplied(struct parser_params *p)
5995{
5996 return strcmp(p->ruby_sourcefile, "-e") == 0;
5997}
5998
5999static VALUE
6000yycompile0(VALUE arg)
6001{
6002 int n;
6003 NODE *tree;
6004 struct parser_params *p = (struct parser_params *)arg;
6005 VALUE cov = Qfalse;
6006
6007 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
6008 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
6009 if (p->debug_lines && p->ruby_sourceline > 0) {
6010 VALUE str = STR_NEW0();
6011 n = p->ruby_sourceline;
6012 do {
6013 rb_ary_push(p->debug_lines, str);
6014 } while (--n);
6015 }
6016
6017 if (!e_option_supplied(p)) {
6018 cov = Qtrue;
6019 }
6020 }
6021
6022 parser_prepare(p);
6023#define RUBY_DTRACE_PARSE_HOOK(name) \
6024 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
6025 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
6026 }
6027 RUBY_DTRACE_PARSE_HOOK(BEGIN);
6028 n = yyparse(p);
6029 RUBY_DTRACE_PARSE_HOOK(END);
6030 p->debug_lines = 0;
6031
6032 p->lex.strterm = 0;
6033 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
6034 p->lex.prevline = p->lex.lastline = p->lex.nextline = 0;
6035 if (n || p->error_p) {
6036 VALUE mesg = p->error_buffer;
6037 if (!mesg) {
6038 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
6039 }
6040 rb_set_errinfo(mesg);
6041 return FALSE;
6042 }
6043 tree = p->eval_tree;
6044 if (!tree) {
6045 tree = NEW_NIL(&NULL_LOC);
6046 }
6047 else {
6048 VALUE opt = p->compile_option;
6049 NODE *prelude;
6050 NODE *body = parser_append_options(p, tree->nd_body);
6051 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
6052 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
6053 prelude = block_append(p, p->eval_tree_begin, body);
6054 tree->nd_body = prelude;
6055 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
6056 }
6057 p->ast->body.root = tree;
6058 p->ast->body.line_count = p->line_count;
6059 return TRUE;
6060}
6061
6062static rb_ast_t *
6063yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
6064{
6065 rb_ast_t *ast;
6066 if (NIL_P(fname)) {
6067 p->ruby_sourcefile_string = Qnil;
6068 p->ruby_sourcefile = "(none)";
6069 }
6070 else {
6071 p->ruby_sourcefile_string = rb_fstring(fname);
6072 p->ruby_sourcefile = StringValueCStr(fname);
6073 }
6074 p->ruby_sourceline = line - 1;
6075
6076 p->ast = ast = rb_ast_new();
6077 rb_suppress_tracing(yycompile0, (VALUE)p);
6078 p->ast = 0;
6079 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6080
6081 return ast;
6082}
6083#endif /* !RIPPER */
6084
6085static rb_encoding *
6086must_be_ascii_compatible(VALUE s)
6087{
6088 rb_encoding *enc = rb_enc_get(s);
6089 if (!rb_enc_asciicompat(enc)) {
6090 rb_raise(rb_eArgError, "invalid source encoding");
6091 }
6092 return enc;
6093}
6094
6095static VALUE
6096lex_get_str(struct parser_params *p, VALUE s)
6097{
6098 char *beg, *end, *start;
6099 long len;
6100
6101 beg = RSTRING_PTR(s);
6102 len = RSTRING_LEN(s);
6103 start = beg;
6104 if (p->lex.gets_.ptr) {
6105 if (len == p->lex.gets_.ptr) return Qnil;
6106 beg += p->lex.gets_.ptr;
6107 len -= p->lex.gets_.ptr;
6108 }
6109 end = memchr(beg, '\n', len);
6110 if (end) len = ++end - beg;
6111 p->lex.gets_.ptr += len;
6112 return rb_str_subseq(s, beg - start, len);
6113}
6114
6115static VALUE
6116lex_getline(struct parser_params *p)
6117{
6118 VALUE line = (*p->lex.gets)(p, p->lex.input);
6119 if (NIL_P(line)) return line;
6120 must_be_ascii_compatible(line);
6121#ifndef RIPPER
6122 if (p->debug_lines) {
6123 rb_enc_associate(line, p->enc);
6124 rb_ary_push(p->debug_lines, line);
6125 }
6126#endif
6127 p->line_count++;
6128 return line;
6129}
6130
6131static const rb_data_type_t parser_data_type;
6132
6133#ifndef RIPPER
6134static rb_ast_t*
6135parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6136{
6137 struct parser_params *p;
6138
6139 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6140
6141 p->lex.gets = lex_get_str;
6142 p->lex.gets_.ptr = 0;
6143 p->lex.input = rb_str_new_frozen(s);
6144 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6145
6146 return yycompile(vparser, p, fname, line);
6147}
6148
6149rb_ast_t*
6150rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6151{
6152 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6153}
6154
6155rb_ast_t*
6156rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6157{
6158 must_be_ascii_compatible(s);
6159 return parser_compile_string(vparser, f, s, line);
6160}
6161
6162VALUE rb_io_gets_internal(VALUE io);
6163
6164static VALUE
6165lex_io_gets(struct parser_params *p, VALUE io)
6166{
6167 return rb_io_gets_internal(io);
6168}
6169
6170rb_ast_t*
6171rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6172{
6173 struct parser_params *p;
6174
6175 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6176
6177 p->lex.gets = lex_io_gets;
6178 p->lex.input = file;
6179 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6180
6181 return yycompile(vparser, p, fname, start);
6182}
6183
6184static VALUE
6185lex_generic_gets(struct parser_params *p, VALUE input)
6186{
6187 return (*p->lex.gets_.call)(input, p->line_count);
6188}
6189
6190rb_ast_t*
6191rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6192{
6193 struct parser_params *p;
6194
6195 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6196
6197 p->lex.gets = lex_generic_gets;
6198 p->lex.gets_.call = lex_gets;
6199 p->lex.input = input;
6200 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6201
6202 return yycompile(vparser, p, fname, start);
6203}
6204#endif /* !RIPPER */
6205
6206#define STR_FUNC_ESCAPE 0x01
6207#define STR_FUNC_EXPAND 0x02
6208#define STR_FUNC_REGEXP 0x04
6209#define STR_FUNC_QWORDS 0x08
6210#define STR_FUNC_SYMBOL 0x10
6211#define STR_FUNC_INDENT 0x20
6212#define STR_FUNC_LABEL 0x40
6213#define STR_FUNC_LIST 0x4000
6214#define STR_FUNC_TERM 0x8000
6215
6216enum string_type {
6217 str_label = STR_FUNC_LABEL,
6218 str_squote = (0),
6219 str_dquote = (STR_FUNC_EXPAND),
6220 str_xquote = (STR_FUNC_EXPAND),
6221 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6222 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6223 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6224 str_ssym = (STR_FUNC_SYMBOL),
6225 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6226};
6227
6228static VALUE
6229parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6230{
6231 VALUE str;
6232
6233 str = rb_enc_str_new(ptr, len, enc);
6234 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6235 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6236 }
6237 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6238 rb_enc_associate(str, rb_ascii8bit_encoding());
6239 }
6240 }
6241
6242 return str;
6243}
6244
6245#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6246#define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6247#define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6248#define peek(p,c) peek_n(p, (c), 0)
6249#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6250#define peekc(p) peekc_n(p, 0)
6251#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6252
6253#ifdef RIPPER
6254static void
6255add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6256{
6257 if (tok < end) {
6258 if (!has_delayed_token(p)) {
6259 p->delayed.token = rb_str_buf_new(end - tok);
6260 rb_enc_associate(p->delayed.token, p->enc);
6261 p->delayed.line = p->ruby_sourceline;
6262 p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6263 }
6264 rb_str_buf_cat(p->delayed.token, tok, end - tok);
6265 p->lex.ptok = end;
6266 }
6267}
6268#else
6269#define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6270#endif
6271
6272static int
6273nextline(struct parser_params *p)
6274{
6275 VALUE v = p->lex.nextline;
6276 p->lex.nextline = 0;
6277 if (!v) {
6278 if (p->eofp)
6279 return -1;
6280
6281 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6282 goto end_of_input;
6283 }
6284
6285 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6286 end_of_input:
6287 p->eofp = 1;
6288 lex_goto_eol(p);
6289 return -1;
6290 }
6291 p->cr_seen = FALSE;
6292 }
6293 else if (NIL_P(v)) {
6294 /* after here-document without terminator */
6295 goto end_of_input;
6296 }
6297 add_delayed_token(p, p->lex.ptok, p->lex.pend);
6298 if (p->heredoc_end > 0) {
6299 p->ruby_sourceline = p->heredoc_end;
6300 p->heredoc_end = 0;
6301 }
6302 p->ruby_sourceline++;
6303 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
6304 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
6305 token_flush(p);
6306 p->lex.prevline = p->lex.lastline;
6307 p->lex.lastline = v;
6308 return 0;
6309}
6310
6311static int
6312parser_cr(struct parser_params *p, int c)
6313{
6314 if (peek(p, '\n')) {
6315 p->lex.pcur++;
6316 c = '\n';
6317 }
6318 else if (!p->cr_seen) {
6319 p->cr_seen = TRUE;
6320 /* carried over with p->lex.nextline for nextc() */
6321 rb_warn0("encountered \\r in middle of line, treated as a mere space");
6322 }
6323 return c;
6324}
6325
6326static inline int
6327nextc(struct parser_params *p)
6328{
6329 int c;
6330
6331 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6332 if (nextline(p)) return -1;
6333 }
6334 c = (unsigned char)*p->lex.pcur++;
6335 if (UNLIKELY(c == '\r')) {
6336 c = parser_cr(p, c);
6337 }
6338
6339 return c;
6340}
6341
6342static void
6343pushback(struct parser_params *p, int c)
6344{
6345 if (c == -1) return;
6346 p->lex.pcur--;
6347 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6348 p->lex.pcur--;
6349 }
6350}
6351
6352#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6353
6354#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6355#define tok(p) (p)->tokenbuf
6356#define toklen(p) (p)->tokidx
6357
6358static int
6359looking_at_eol_p(struct parser_params *p)
6360{
6361 const char *ptr = p->lex.pcur;
6362 while (ptr < p->lex.pend) {
6363 int c = (unsigned char)*ptr++;
6364 int eol = (c == '\n' || c == '#');
6365 if (eol || !ISSPACE(c)) {
6366 return eol;
6367 }
6368 }
6369 return TRUE;
6370}
6371
6372static char*
6373newtok(struct parser_params *p)
6374{
6375 p->tokidx = 0;
6376 p->tokline = p->ruby_sourceline;
6377 if (!p->tokenbuf) {
6378 p->toksiz = 60;
6379 p->tokenbuf = ALLOC_N(char, 60);
6380 }
6381 if (p->toksiz > 4096) {
6382 p->toksiz = 60;
6383 REALLOC_N(p->tokenbuf, char, 60);
6384 }
6385 return p->tokenbuf;
6386}
6387
6388static char *
6389tokspace(struct parser_params *p, int n)
6390{
6391 p->tokidx += n;
6392
6393 if (p->tokidx >= p->toksiz) {
6394 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6395 REALLOC_N(p->tokenbuf, char, p->toksiz);
6396 }
6397 return &p->tokenbuf[p->tokidx-n];
6398}
6399
6400static void
6401tokadd(struct parser_params *p, int c)
6402{
6403 p->tokenbuf[p->tokidx++] = (char)c;
6404 if (p->tokidx >= p->toksiz) {
6405 p->toksiz *= 2;
6406 REALLOC_N(p->tokenbuf, char, p->toksiz);
6407 }
6408}
6409
6410static int
6411tok_hex(struct parser_params *p, size_t *numlen)
6412{
6413 int c;
6414
6415 c = scan_hex(p->lex.pcur, 2, numlen);
6416 if (!*numlen) {
6417 yyerror0("invalid hex escape");
6418 token_flush(p);
6419 return 0;
6420 }
6421 p->lex.pcur += *numlen;
6422 return c;
6423}
6424
6425#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6426
6427static int
6428escaped_control_code(int c)
6429{
6430 int c2 = 0;
6431 switch (c) {
6432 case ' ':
6433 c2 = 's';
6434 break;
6435 case '\n':
6436 c2 = 'n';
6437 break;
6438 case '\t':
6439 c2 = 't';
6440 break;
6441 case '\v':
6442 c2 = 'v';
6443 break;
6444 case '\r':
6445 c2 = 'r';
6446 break;
6447 case '\f':
6448 c2 = 'f';
6449 break;
6450 }
6451 return c2;
6452}
6453
6454#define WARN_SPACE_CHAR(c, prefix) \
6455 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6456
6457static int
6458tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6459 int regexp_literal, int wide)
6460{
6461 size_t numlen;
6462 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6463 literal_flush(p, p->lex.pcur);
6464 p->lex.pcur += numlen;
6465 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6466 yyerror0("invalid Unicode escape");
6467 return wide && numlen > 0;
6468 }
6469 if (codepoint > 0x10ffff) {
6470 yyerror0("invalid Unicode codepoint (too large)");
6471 return wide;
6472 }
6473 if ((codepoint & 0xfffff800) == 0xd800) {
6474 yyerror0("invalid Unicode codepoint");
6475 return wide;
6476 }
6477 if (regexp_literal) {
6478 tokcopy(p, (int)numlen);
6479 }
6480 else if (codepoint >= 0x80) {
6481 rb_encoding *utf8 = rb_utf8_encoding();
6482 if (*encp && utf8 != *encp) {
6483 YYLTYPE loc = RUBY_INIT_YYLLOC();
6484 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6485 parser_show_error_line(p, &loc);
6486 return wide;
6487 }
6488 *encp = utf8;
6489 tokaddmbc(p, codepoint, *encp);
6490 }
6491 else {
6492 tokadd(p, codepoint);
6493 }
6494 return TRUE;
6495}
6496
6497/* return value is for ?\u3042 */
6498static void
6499tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6500 int term, int symbol_literal, int regexp_literal)
6501{
6502 /*
6503 * If `term` is not -1, then we allow multiple codepoints in \u{}
6504 * upto `term` byte, otherwise we're parsing a character literal.
6505 * And then add the codepoints to the current token.
6506 */
6507 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6508
6509 const int open_brace = '{', close_brace = '}';
6510
6511 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6512
6513 if (peek(p, open_brace)) { /* handle \u{...} form */
6514 const char *second = NULL;
6515 int c, last = nextc(p);
6516 if (p->lex.pcur >= p->lex.pend) goto unterminated;
6517 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6518 while (c != close_brace) {
6519 if (c == term) goto unterminated;
6520 if (second == multiple_codepoints)
6521 second = p->lex.pcur;
6522 if (regexp_literal) tokadd(p, last);
6523 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6524 break;
6525 }
6526 while (ISSPACE(c = *p->lex.pcur)) {
6527 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6528 last = c;
6529 }
6530 if (term == -1 && !second)
6531 second = multiple_codepoints;
6532 }
6533
6534 if (c != close_brace) {
6535 unterminated:
6536 token_flush(p);
6537 yyerror0("unterminated Unicode escape");
6538 return;
6539 }
6540 if (second && second != multiple_codepoints) {
6541 const char *pcur = p->lex.pcur;
6542 p->lex.pcur = second;
6543 dispatch_scan_event(p, tSTRING_CONTENT);
6544 token_flush(p);
6545 p->lex.pcur = pcur;
6546 yyerror0(multiple_codepoints);
6547 token_flush(p);
6548 }
6549
6550 if (regexp_literal) tokadd(p, close_brace);
6551 nextc(p);
6552 }
6553 else { /* handle \uxxxx form */
6554 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6555 token_flush(p);
6556 return;
6557 }
6558 }
6559}
6560
6561#define ESCAPE_CONTROL 1
6562#define ESCAPE_META 2
6563
6564static int
6565read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6566{
6567 int c;
6568 size_t numlen;
6569
6570 switch (c = nextc(p)) {
6571 case '\\': /* Backslash */
6572 return c;
6573
6574 case 'n': /* newline */
6575 return '\n';
6576
6577 case 't': /* horizontal tab */
6578 return '\t';
6579
6580 case 'r': /* carriage-return */
6581 return '\r';
6582
6583 case 'f': /* form-feed */
6584 return '\f';
6585
6586 case 'v': /* vertical tab */
6587 return '\13';
6588
6589 case 'a': /* alarm(bell) */
6590 return '\007';
6591
6592 case 'e': /* escape */
6593 return 033;
6594
6595 case '0': case '1': case '2': case '3': /* octal constant */
6596 case '4': case '5': case '6': case '7':
6597 pushback(p, c);
6598 c = scan_oct(p->lex.pcur, 3, &numlen);
6599 p->lex.pcur += numlen;
6600 return c;
6601
6602 case 'x': /* hex constant */
6603 c = tok_hex(p, &numlen);
6604 if (numlen == 0) return 0;
6605 return c;
6606
6607 case 'b': /* backspace */
6608 return '\010';
6609
6610 case 's': /* space */
6611 return ' ';
6612
6613 case 'M':
6614 if (flags & ESCAPE_META) goto eof;
6615 if ((c = nextc(p)) != '-') {
6616 goto eof;
6617 }
6618 if ((c = nextc(p)) == '\\') {
6619 if (peek(p, 'u')) goto eof;
6620 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6621 }
6622 else if (c == -1 || !ISASCII(c)) goto eof;
6623 else {
6624 int c2 = escaped_control_code(c);
6625 if (c2) {
6626 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6627 WARN_SPACE_CHAR(c2, "\\M-");
6628 }
6629 else {
6630 WARN_SPACE_CHAR(c2, "\\C-\\M-");
6631 }
6632 }
6633 else if (ISCNTRL(c)) goto eof;
6634 return ((c & 0xff) | 0x80);
6635 }
6636
6637 case 'C':
6638 if ((c = nextc(p)) != '-') {
6639 goto eof;
6640 }
6641 case 'c':
6642 if (flags & ESCAPE_CONTROL) goto eof;
6643 if ((c = nextc(p))== '\\') {
6644 if (peek(p, 'u')) goto eof;
6645 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6646 }
6647 else if (c == '?')
6648 return 0177;
6649 else if (c == -1 || !ISASCII(c)) goto eof;
6650 else {
6651 int c2 = escaped_control_code(c);
6652 if (c2) {
6653 if (ISCNTRL(c)) {
6654 if (flags & ESCAPE_META) {
6655 WARN_SPACE_CHAR(c2, "\\M-");
6656 }
6657 else {
6658 WARN_SPACE_CHAR(c2, "");
6659 }
6660 }
6661 else {
6662 if (flags & ESCAPE_META) {
6663 WARN_SPACE_CHAR(c2, "\\M-\\C-");
6664 }
6665 else {
6666 WARN_SPACE_CHAR(c2, "\\C-");
6667 }
6668 }
6669 }
6670 else if (ISCNTRL(c)) goto eof;
6671 }
6672 return c & 0x9f;
6673
6674 eof:
6675 case -1:
6676 yyerror0("Invalid escape character syntax");
6677 token_flush(p);
6678 return '\0';
6679
6680 default:
6681 return c;
6682 }
6683}
6684
6685static void
6686tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
6687{
6688 int len = rb_enc_codelen(c, enc);
6689 rb_enc_mbcput(c, tokspace(p, len), enc);
6690}
6691
6692static int
6693tokadd_escape(struct parser_params *p, rb_encoding **encp)
6694{
6695 int c;
6696 int flags = 0;
6697 size_t numlen;
6698
6699 first:
6700 switch (c = nextc(p)) {
6701 case '\n':
6702 return 0; /* just ignore */
6703
6704 case '0': case '1': case '2': case '3': /* octal constant */
6705 case '4': case '5': case '6': case '7':
6706 {
6707 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
6708 if (numlen == 0) goto eof;
6709 p->lex.pcur += numlen;
6710 tokcopy(p, (int)numlen + 1);
6711 }
6712 return 0;
6713
6714 case 'x': /* hex constant */
6715 {
6716 tok_hex(p, &numlen);
6717 if (numlen == 0) return -1;
6718 tokcopy(p, (int)numlen + 2);
6719 }
6720 return 0;
6721
6722 case 'M':
6723 if (flags & ESCAPE_META) goto eof;
6724 if ((c = nextc(p)) != '-') {
6725 pushback(p, c);
6726 goto eof;
6727 }
6728 tokcopy(p, 3);
6729 flags |= ESCAPE_META;
6730 goto escaped;
6731
6732 case 'C':
6733 if (flags & ESCAPE_CONTROL) goto eof;
6734 if ((c = nextc(p)) != '-') {
6735 pushback(p, c);
6736 goto eof;
6737 }
6738 tokcopy(p, 3);
6739 goto escaped;
6740
6741 case 'c':
6742 if (flags & ESCAPE_CONTROL) goto eof;
6743 tokcopy(p, 2);
6744 flags |= ESCAPE_CONTROL;
6745 escaped:
6746 if ((c = nextc(p)) == '\\') {
6747 goto first;
6748 }
6749 else if (c == -1) goto eof;
6750 tokadd(p, c);
6751 return 0;
6752
6753 eof:
6754 case -1:
6755 yyerror0("Invalid escape character syntax");
6756 token_flush(p);
6757 return -1;
6758
6759 default:
6760 tokadd(p, '\\');
6761 tokadd(p, c);
6762 }
6763 return 0;
6764}
6765
6766static int
6767regx_options(struct parser_params *p)
6768{
6769 int kcode = 0;
6770 int kopt = 0;
6771 int options = 0;
6772 int c, opt, kc;
6773
6774 newtok(p);
6775 while (c = nextc(p), ISALPHA(c)) {
6776 if (c == 'o') {
6777 options |= RE_OPTION_ONCE;
6778 }
6779 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
6780 if (kc >= 0) {
6781 if (kc != rb_ascii8bit_encindex()) kcode = c;
6782 kopt = opt;
6783 }
6784 else {
6785 options |= opt;
6786 }
6787 }
6788 else {
6789 tokadd(p, c);
6790 }
6791 }
6792 options |= kopt;
6793 pushback(p, c);
6794 if (toklen(p)) {
6795 YYLTYPE loc = RUBY_INIT_YYLLOC();
6796 tokfix(p);
6797 compile_error(p, "unknown regexp option%s - %*s",
6798 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
6799 parser_show_error_line(p, &loc);
6800 }
6801 return options | RE_OPTION_ENCODING(kcode);
6802}
6803
6804static int
6805tokadd_mbchar(struct parser_params *p, int c)
6806{
6807 int len = parser_precise_mbclen(p, p->lex.pcur-1);
6808 if (len < 0) return -1;
6809 tokadd(p, c);
6810 p->lex.pcur += --len;
6811 if (len > 0) tokcopy(p, len);
6812 return c;
6813}
6814
6815static inline int
6816simple_re_meta(int c)
6817{
6818 switch (c) {
6819 case '$': case '*': case '+': case '.':
6820 case '?': case '^': case '|':
6821 case ')': case ']': case '}': case '>':
6822 return TRUE;
6823 default:
6824 return FALSE;
6825 }
6826}
6827
6828static int
6829parser_update_heredoc_indent(struct parser_params *p, int c)
6830{
6831 if (p->heredoc_line_indent == -1) {
6832 if (c == '\n') p->heredoc_line_indent = 0;
6833 }
6834 else {
6835 if (c == ' ') {
6836 p->heredoc_line_indent++;
6837 return TRUE;
6838 }
6839 else if (c == '\t') {
6840 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
6841 p->heredoc_line_indent = w * TAB_WIDTH;
6842 return TRUE;
6843 }
6844 else if (c != '\n') {
6845 if (p->heredoc_indent > p->heredoc_line_indent) {
6846 p->heredoc_indent = p->heredoc_line_indent;
6847 }
6848 p->heredoc_line_indent = -1;
6849 }
6850 }
6851 return FALSE;
6852}
6853
6854static void
6855parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
6856{
6857 YYLTYPE loc = RUBY_INIT_YYLLOC();
6858 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
6859 compile_error(p, "%s mixed within %s source", n1, n2);
6860 parser_show_error_line(p, &loc);
6861}
6862
6863static void
6864parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
6865{
6866 const char *pos = p->lex.pcur;
6867 p->lex.pcur = beg;
6868 parser_mixed_error(p, enc1, enc2);
6869 p->lex.pcur = pos;
6870}
6871
6872static int
6873tokadd_string(struct parser_params *p,
6874 int func, int term, int paren, long *nest,
6875 rb_encoding **encp, rb_encoding **enc)
6876{
6877 int c;
6878 bool erred = false;
6879
6880#define mixed_error(enc1, enc2) \
6881 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
6882#define mixed_escape(beg, enc1, enc2) \
6883 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
6884
6885 while ((c = nextc(p)) != -1) {
6886 if (p->heredoc_indent > 0) {
6887 parser_update_heredoc_indent(p, c);
6888 }
6889
6890 if (paren && c == paren) {
6891 ++*nest;
6892 }
6893 else if (c == term) {
6894 if (!nest || !*nest) {
6895 pushback(p, c);
6896 break;
6897 }
6898 --*nest;
6899 }
6900 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
6901 int c2 = *p->lex.pcur;
6902 if (c2 == '$' || c2 == '@' || c2 == '{') {
6903 pushback(p, c);
6904 break;
6905 }
6906 }
6907 else if (c == '\\') {
6908 literal_flush(p, p->lex.pcur - 1);
6909 c = nextc(p);
6910 switch (c) {
6911 case '\n':
6912 if (func & STR_FUNC_QWORDS) break;
6913 if (func & STR_FUNC_EXPAND) {
6914 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
6915 continue;
6916 if (c == term) {
6917 c = '\\';
6918 goto terminate;
6919 }
6920 }
6921 tokadd(p, '\\');
6922 break;
6923
6924 case '\\':
6925 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
6926 break;
6927
6928 case 'u':
6929 if ((func & STR_FUNC_EXPAND) == 0) {
6930 tokadd(p, '\\');
6931 break;
6932 }
6933 tokadd_utf8(p, enc, term,
6934 func & STR_FUNC_SYMBOL,
6935 func & STR_FUNC_REGEXP);
6936 continue;
6937
6938 default:
6939 if (c == -1) return -1;
6940 if (!ISASCII(c)) {
6941 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
6942 goto non_ascii;
6943 }
6944 if (func & STR_FUNC_REGEXP) {
6945 if (c == term && !simple_re_meta(c)) {
6946 tokadd(p, c);
6947 continue;
6948 }
6949 pushback(p, c);
6950 if ((c = tokadd_escape(p, enc)) < 0)
6951 return -1;
6952 if (*enc && *enc != *encp) {
6953 mixed_escape(p->lex.ptok+2, *enc, *encp);
6954 }
6955 continue;
6956 }
6957 else if (func & STR_FUNC_EXPAND) {
6958 pushback(p, c);
6959 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
6960 c = read_escape(p, 0, enc);
6961 }
6962 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6963 /* ignore backslashed spaces in %w */
6964 }
6965 else if (c != term && !(paren && c == paren)) {
6966 tokadd(p, '\\');
6967 pushback(p, c);
6968 continue;
6969 }
6970 }
6971 }
6972 else if (!parser_isascii(p)) {
6973 non_ascii:
6974 if (!*enc) {
6975 *enc = *encp;
6976 }
6977 else if (*enc != *encp) {
6978 mixed_error(*enc, *encp);
6979 continue;
6980 }
6981 if (tokadd_mbchar(p, c) == -1) return -1;
6982 continue;
6983 }
6984 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6985 pushback(p, c);
6986 break;
6987 }
6988 if (c & 0x80) {
6989 if (!*enc) {
6990 *enc = *encp;
6991 }
6992 else if (*enc != *encp) {
6993 mixed_error(*enc, *encp);
6994 continue;
6995 }
6996 }
6997 tokadd(p, c);
6998 }
6999 terminate:
7000 if (*enc) *encp = *enc;
7001 return c;
7002}
7003
7004static inline rb_strterm_t *
7005new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
7006{
7007 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
7008}
7009
7010/* imemo_parser_strterm for literal */
7011#define NEW_STRTERM(func, term, paren) \
7012 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
7013
7014#ifdef RIPPER
7015static void
7016flush_string_content(struct parser_params *p, rb_encoding *enc)
7017{
7018 VALUE content = yylval.val;
7019 if (!ripper_is_node_yylval(content))
7020 content = ripper_new_yylval(p, 0, 0, content);
7021 if (has_delayed_token(p)) {
7022 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7023 if (len > 0) {
7024 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7025 }
7026 dispatch_delayed_token(p, tSTRING_CONTENT);
7027 p->lex.ptok = p->lex.pcur;
7028 RNODE(content)->nd_rval = yylval.val;
7029 }
7030 dispatch_scan_event(p, tSTRING_CONTENT);
7031 if (yylval.val != content)
7032 RNODE(content)->nd_rval = yylval.val;
7033 yylval.val = content;
7034}
7035#else
7036#define flush_string_content(p, enc) ((void)(enc))
7037#endif
7038
7039RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7040/* this can be shared with ripper, since it's independent from struct
7041 * parser_params. */
7042#ifndef RIPPER
7043#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7044#define SPECIAL_PUNCT(idx) ( \
7045 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
7046 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
7047 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
7048 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
7049 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7050 BIT('0', idx))
7051const unsigned int ruby_global_name_punct_bits[] = {
7052 SPECIAL_PUNCT(0),
7053 SPECIAL_PUNCT(1),
7054 SPECIAL_PUNCT(2),
7055};
7056#undef BIT
7057#undef SPECIAL_PUNCT
7058#endif
7059
7060static enum yytokentype
7061parser_peek_variable_name(struct parser_params *p)
7062{
7063 int c;
7064 const char *ptr = p->lex.pcur;
7065
7066 if (ptr + 1 >= p->lex.pend) return 0;
7067 c = *ptr++;
7068 switch (c) {
7069 case '$':
7070 if ((c = *ptr) == '-') {
7071 if (++ptr >= p->lex.pend) return 0;
7072 c = *ptr;
7073 }
7074 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7075 return tSTRING_DVAR;
7076 }
7077 break;
7078 case '@':
7079 if ((c = *ptr) == '@') {
7080 if (++ptr >= p->lex.pend) return 0;
7081 c = *ptr;
7082 }
7083 break;
7084 case '{':
7085 p->lex.pcur = ptr;
7086 p->command_start = TRUE;
7087 return tSTRING_DBEG;
7088 default:
7089 return 0;
7090 }
7091 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7092 return tSTRING_DVAR;
7093 return 0;
7094}
7095
7096#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7097#define IS_END() IS_lex_state(EXPR_END_ANY)
7098#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7099#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7100#define IS_LABEL_POSSIBLE() (\
7101 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7102 IS_ARG())
7103#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7104#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7105
7106static inline enum yytokentype
7107parser_string_term(struct parser_params *p, int func)
7108{
7109 p->lex.strterm = 0;
7110 if (func & STR_FUNC_REGEXP) {
7111 set_yylval_num(regx_options(p));
7112 dispatch_scan_event(p, tREGEXP_END);
7113 SET_LEX_STATE(EXPR_END);
7114 return tREGEXP_END;
7115 }
7116 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7117 nextc(p);
7118 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7119 return tLABEL_END;
7120 }
7121 SET_LEX_STATE(EXPR_END);
7122 return tSTRING_END;
7123}
7124
7125static enum yytokentype
7126parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7127{
7128 int func = (int)quote->u1.func;
7129 int term = (int)quote->u3.term;
7130 int paren = (int)quote->u2.paren;
7131 int c, space = 0;
7132 rb_encoding *enc = p->enc;
7133 rb_encoding *base_enc = 0;
7134 VALUE lit;
7135
7136 if (func & STR_FUNC_TERM) {
7137 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7138 SET_LEX_STATE(EXPR_END);
7139 p->lex.strterm = 0;
7140 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7141 }
7142 c = nextc(p);
7143 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7144 do {c = nextc(p);} while (ISSPACE(c));
7145 space = 1;
7146 }
7147 if (func & STR_FUNC_LIST) {
7148 quote->u1.func &= ~STR_FUNC_LIST;
7149 space = 1;
7150 }
7151 if (c == term && !quote->u0.nest) {
7152 if (func & STR_FUNC_QWORDS) {
7153 quote->u1.func |= STR_FUNC_TERM;
7154 pushback(p, c); /* dispatch the term at tSTRING_END */
7155 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7156 return ' ';
7157 }
7158 return parser_string_term(p, func);
7159 }
7160 if (space) {
7161 pushback(p, c);
7162 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7163 return ' ';
7164 }
7165 newtok(p);
7166 if ((func & STR_FUNC_EXPAND) && c == '#') {
7167 int t = parser_peek_variable_name(p);
7168 if (t) return t;
7169 tokadd(p, '#');
7170 c = nextc(p);
7171 }
7172 pushback(p, c);
7173 if (tokadd_string(p, func, term, paren, &quote->u0.nest,
7174 &enc, &base_enc) == -1) {
7175 if (p->eofp) {
7176#ifndef RIPPER
7177# define unterminated_literal(mesg) yyerror0(mesg)
7178#else
7179# define unterminated_literal(mesg) compile_error(p, mesg)
7180#endif
7181 literal_flush(p, p->lex.pcur);
7182 if (func & STR_FUNC_QWORDS) {
7183 /* no content to add, bailing out here */
7184 unterminated_literal("unterminated list meets end of file");
7185 p->lex.strterm = 0;
7186 return tSTRING_END;
7187 }
7188 if (func & STR_FUNC_REGEXP) {
7189 unterminated_literal("unterminated regexp meets end of file");
7190 }
7191 else {
7192 unterminated_literal("unterminated string meets end of file");
7193 }
7194 quote->u1.func |= STR_FUNC_TERM;
7195 }
7196 }
7197
7198 tokfix(p);
7199 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7200 set_yylval_str(lit);
7201 flush_string_content(p, enc);
7202
7203 return tSTRING_CONTENT;
7204}
7205
7206static enum yytokentype
7207heredoc_identifier(struct parser_params *p)
7208{
7209 /*
7210 * term_len is length of `<<"END"` except `END`,
7211 * in this case term_len is 4 (<, <, " and ").
7212 */
7213 long len, offset = p->lex.pcur - p->lex.pbeg;
7214 int c = nextc(p), term, func = 0, quote = 0;
7215 enum yytokentype token = tSTRING_BEG;
7216 int indent = 0;
7217
7218 if (c == '-') {
7219 c = nextc(p);
7220 func = STR_FUNC_INDENT;
7221 offset++;
7222 }
7223 else if (c == '~') {
7224 c = nextc(p);
7225 func = STR_FUNC_INDENT;
7226 offset++;
7227 indent = INT_MAX;
7228 }
7229 switch (c) {
7230 case '\'':
7231 func |= str_squote; goto quoted;
7232 case '"':
7233 func |= str_dquote; goto quoted;
7234 case '`':
7235 token = tXSTRING_BEG;
7236 func |= str_xquote; goto quoted;
7237
7238 quoted:
7239 quote++;
7240 offset++;
7241 term = c;
7242 len = 0;
7243 while ((c = nextc(p)) != term) {
7244 if (c == -1 || c == '\r' || c == '\n') {
7245 yyerror(NULL, p, "unterminated here document identifier");
7246 return -1;
7247 }
7248 }
7249 break;
7250
7251 default:
7252 if (!parser_is_identchar(p)) {
7253 pushback(p, c);
7254 if (func & STR_FUNC_INDENT) {
7255 pushback(p, indent > 0 ? '~' : '-');
7256 }
7257 return 0;
7258 }
7259 func |= str_dquote;
7260 do {
7261 int n = parser_precise_mbclen(p, p->lex.pcur-1);
7262 if (n < 0) return 0;
7263 p->lex.pcur += --n;
7264 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7265 pushback(p, c);
7266 break;
7267 }
7268
7269 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7270 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7271 yyerror(NULL, p, "too long here document identifier");
7272 dispatch_scan_event(p, tHEREDOC_BEG);
7273 lex_goto_eol(p);
7274
7275 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7276 p->lex.strterm->flags |= STRTERM_HEREDOC;
7277 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7278 here->offset = offset;
7279 here->sourceline = p->ruby_sourceline;
7280 here->length = (int)len;
7281 here->quote = quote;
7282 here->func = func;
7283
7284 token_flush(p);
7285 p->heredoc_indent = indent;
7286 p->heredoc_line_indent = 0;
7287 return token;
7288}
7289
7290static void
7291heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7292{
7293 VALUE line;
7294
7295 p->lex.strterm = 0;
7296 line = here->lastline;
7297 p->lex.lastline = line;
7298 p->lex.pbeg = RSTRING_PTR(line);
7299 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7300 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7301 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7302 p->heredoc_end = p->ruby_sourceline;
7303 p->ruby_sourceline = (int)here->sourceline;
7304 if (p->eofp) p->lex.nextline = Qnil;
7305 p->eofp = 0;
7306}
7307
7308static int
7309dedent_string(VALUE string, int width)
7310{
7311 char *str;
7312 long len;
7313 int i, col = 0;
7314
7315 RSTRING_GETMEM(string, str, len);
7316 for (i = 0; i < len && col < width; i++) {
7317 if (str[i] == ' ') {
7318 col++;
7319 }
7320 else if (str[i] == '\t') {
7321 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7322 if (n > width) break;
7323 col = n;
7324 }
7325 else {
7326 break;
7327 }
7328 }
7329 if (!i) return 0;
7330 rb_str_modify(string);
7331 str = RSTRING_PTR(string);
7332 if (RSTRING_LEN(string) != len)
7333 rb_fatal("literal string changed: %+"PRIsVALUE, string);
7334 MEMMOVE(str, str + i, char, len - i);
7335 rb_str_set_len(string, len - i);
7336 return i;
7337}
7338
7339#ifndef RIPPER
7340static NODE *
7341heredoc_dedent(struct parser_params *p, NODE *root)
7342{
7343 NODE *node, *str_node, *prev_node;
7344 int indent = p->heredoc_indent;
7345 VALUE prev_lit = 0;
7346
7347 if (indent <= 0) return root;
7348 p->heredoc_indent = 0;
7349 if (!root) return root;
7350
7351 prev_node = node = str_node = root;
7352 if (nd_type(root) == NODE_LIST) str_node = root->nd_head;
7353
7354 while (str_node) {
7355 VALUE lit = str_node->nd_lit;
7356 if (str_node->flags & NODE_FL_NEWLINE) {
7357 dedent_string(lit, indent);
7358 }
7359 if (!prev_lit) {
7360 prev_lit = lit;
7361 }
7362 else if (!literal_concat0(p, prev_lit, lit)) {
7363 return 0;
7364 }
7365 else {
7366 NODE *end = node->nd_end;
7367 node = prev_node->nd_next = node->nd_next;
7368 if (!node) {
7369 if (nd_type(prev_node) == NODE_DSTR)
7370 nd_set_type(prev_node, NODE_STR);
7371 break;
7372 }
7373 node->nd_end = end;
7374 goto next_str;
7375 }
7376
7377 str_node = 0;
7378 while ((node = (prev_node = node)->nd_next) != 0) {
7379 next_str:
7380 if (nd_type(node) != NODE_LIST) break;
7381 if ((str_node = node->nd_head) != 0) {
7382 enum node_type type = nd_type(str_node);
7383 if (type == NODE_STR || type == NODE_DSTR) break;
7384 prev_lit = 0;
7385 str_node = 0;
7386 }
7387 }
7388 }
7389 return root;
7390}
7391#else /* RIPPER */
7392static VALUE
7393heredoc_dedent(struct parser_params *p, VALUE array)
7394{
7395 int indent = p->heredoc_indent;
7396
7397 if (indent <= 0) return array;
7398 p->heredoc_indent = 0;
7399 dispatch2(heredoc_dedent, array, INT2NUM(indent));
7400 return array;
7401}
7402
7403/*
7404 * call-seq:
7405 * Ripper.dedent_string(input, width) -> Integer
7406 *
7407 * USE OF RIPPER LIBRARY ONLY.
7408 *
7409 * Strips up to +width+ leading whitespaces from +input+,
7410 * and returns the stripped column width.
7411 */
7412static VALUE
7413parser_dedent_string(VALUE self, VALUE input, VALUE width)
7414{
7415 int wid, col;
7416
7417 StringValue(input);
7418 wid = NUM2UINT(width);
7419 col = dedent_string(input, wid);
7420 return INT2NUM(col);
7421}
7422#endif
7423
7424static int
7425whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7426{
7427 const char *ptr = p->lex.pbeg;
7428 long n;
7429
7430 if (indent) {
7431 while (*ptr && ISSPACE(*ptr)) ptr++;
7432 }
7433 n = p->lex.pend - (ptr + len);
7434 if (n < 0) return FALSE;
7435 if (n > 0 && ptr[len] != '\n') {
7436 if (ptr[len] != '\r') return FALSE;
7437 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7438 }
7439 return strncmp(eos, ptr, len) == 0;
7440}
7441
7442static int
7443word_match_p(struct parser_params *p, const char *word, long len)
7444{
7445 if (strncmp(p->lex.pcur, word, len)) return 0;
7446 if (p->lex.pcur + len == p->lex.pend) return 1;
7447 int c = (unsigned char)p->lex.pcur[len];
7448 if (ISSPACE(c)) return 1;
7449 switch (c) {
7450 case '\0': case '\004': case '\032': return 1;
7451 }
7452 return 0;
7453}
7454
7455#define NUM_SUFFIX_R (1<<0)
7456#define NUM_SUFFIX_I (1<<1)
7457#define NUM_SUFFIX_ALL 3
7458
7459static int
7460number_literal_suffix(struct parser_params *p, int mask)
7461{
7462 int c, result = 0;
7463 const char *lastp = p->lex.pcur;
7464
7465 while ((c = nextc(p)) != -1) {
7466 if ((mask & NUM_SUFFIX_I) && c == 'i') {
7467 result |= (mask & NUM_SUFFIX_I);
7468 mask &= ~NUM_SUFFIX_I;
7469 /* r after i, rational of complex is disallowed */
7470 mask &= ~NUM_SUFFIX_R;
7471 continue;
7472 }
7473 if ((mask & NUM_SUFFIX_R) && c == 'r') {
7474 result |= (mask & NUM_SUFFIX_R);
7475 mask &= ~NUM_SUFFIX_R;
7476 continue;
7477 }
7478 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7479 p->lex.pcur = lastp;
7480 literal_flush(p, p->lex.pcur);
7481 return 0;
7482 }
7483 pushback(p, c);
7484 break;
7485 }
7486 return result;
7487}
7488
7489static enum yytokentype
7490set_number_literal(struct parser_params *p, VALUE v,
7491 enum yytokentype type, int suffix)
7492{
7493 if (suffix & NUM_SUFFIX_I) {
7494 v = rb_complex_raw(INT2FIX(0), v);
7495 type = tIMAGINARY;
7496 }
7497 set_yylval_literal(v);
7498 SET_LEX_STATE(EXPR_END);
7499 return type;
7500}
7501
7502static enum yytokentype
7503set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7504{
7505 enum yytokentype type = tINTEGER;
7506 if (suffix & NUM_SUFFIX_R) {
7507 v = rb_rational_raw1(v);
7508 type = tRATIONAL;
7509 }
7510 return set_number_literal(p, v, type, suffix);
7511}
7512
7513#ifdef RIPPER
7514static void
7515dispatch_heredoc_end(struct parser_params *p)
7516{
7517 VALUE str;
7518 if (has_delayed_token(p))
7519 dispatch_delayed_token(p, tSTRING_CONTENT);
7520 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7521 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7522 lex_goto_eol(p);
7523 token_flush(p);
7524}
7525
7526#else
7527#define dispatch_heredoc_end(p) ((void)0)
7528#endif
7529
7530static enum yytokentype
7531here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7532{
7533 int c, func, indent = 0;
7534 const char *eos, *ptr, *ptr_end;
7535 long len;
7536 VALUE str = 0;
7537 rb_encoding *enc = p->enc;
7538 rb_encoding *base_enc = 0;
7539 int bol;
7540
7541 eos = RSTRING_PTR(here->lastline) + here->offset;
7542 len = here->length;
7543 indent = (func = here->func) & STR_FUNC_INDENT;
7544
7545 if ((c = nextc(p)) == -1) {
7546 error:
7547#ifdef RIPPER
7548 if (!has_delayed_token(p)) {
7549 dispatch_scan_event(p, tSTRING_CONTENT);
7550 }
7551 else {
7552 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7553 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7554 int cr = ENC_CODERANGE_UNKNOWN;
7555 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7556 if (cr != ENC_CODERANGE_7BIT &&
7557 p->enc == rb_usascii_encoding() &&
7558 enc != rb_utf8_encoding()) {
7559 enc = rb_ascii8bit_encoding();
7560 }
7561 }
7562 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7563 }
7564 dispatch_delayed_token(p, tSTRING_CONTENT);
7565 }
7566 lex_goto_eol(p);
7567#endif
7568 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7569 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7570 (int)len, eos);
7571 token_flush(p);
7572 p->lex.strterm = 0;
7573 SET_LEX_STATE(EXPR_END);
7574 return tSTRING_END;
7575 }
7576 bol = was_bol(p);
7577 if (!bol) {
7578 /* not beginning of line, cannot be the terminator */
7579 }
7580 else if (p->heredoc_line_indent == -1) {
7581 /* `heredoc_line_indent == -1` means
7582 * - "after an interpolation in the same line", or
7583 * - "in a continuing line"
7584 */
7585 p->heredoc_line_indent = 0;
7586 }
7587 else if (whole_match_p(p, eos, len, indent)) {
7588 dispatch_heredoc_end(p);
7589 restore:
7590 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7591 token_flush(p);
7592 p->lex.strterm = 0;
7593 SET_LEX_STATE(EXPR_END);
7594 return tSTRING_END;
7595 }
7596
7597 if (!(func & STR_FUNC_EXPAND)) {
7598 do {
7599 ptr = RSTRING_PTR(p->lex.lastline);
7600 ptr_end = p->lex.pend;
7601 if (ptr_end > ptr) {
7602 switch (ptr_end[-1]) {
7603 case '\n':
7604 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7605 ptr_end++;
7606 break;
7607 }
7608 case '\r':
7609 --ptr_end;
7610 }
7611 }
7612
7613 if (p->heredoc_indent > 0) {
7614 long i = 0;
7615 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7616 i++;
7617 p->heredoc_line_indent = 0;
7618 }
7619
7620 if (str)
7621 rb_str_cat(str, ptr, ptr_end - ptr);
7622 else
7623 str = STR_NEW(ptr, ptr_end - ptr);
7624 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7625 lex_goto_eol(p);
7626 if (p->heredoc_indent > 0) {
7627 goto flush_str;
7628 }
7629 if (nextc(p) == -1) {
7630 if (str) {
7631 str = 0;
7632 }
7633 goto error;
7634 }
7635 } while (!whole_match_p(p, eos, len, indent));
7636 }
7637 else {
7638 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7639 newtok(p);
7640 if (c == '#') {
7641 int t = parser_peek_variable_name(p);
7642 if (p->heredoc_line_indent != -1) {
7643 if (p->heredoc_indent > p->heredoc_line_indent) {
7644 p->heredoc_indent = p->heredoc_line_indent;
7645 }
7646 p->heredoc_line_indent = -1;
7647 }
7648 if (t) return t;
7649 tokadd(p, '#');
7650 c = nextc(p);
7651 }
7652 do {
7653 pushback(p, c);
7654 enc = p->enc;
7655 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7656 if (p->eofp) goto error;
7657 goto restore;
7658 }
7659 if (c != '\n') {
7660 if (c == '\\') p->heredoc_line_indent = -1;
7661 flush:
7662 str = STR_NEW3(tok(p), toklen(p), enc, func);
7663 flush_str:
7664 set_yylval_str(str);
7665#ifndef RIPPER
7666 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7667#endif
7668 flush_string_content(p, enc);
7669 return tSTRING_CONTENT;
7670 }
7671 tokadd(p, nextc(p));
7672 if (p->heredoc_indent > 0) {
7673 lex_goto_eol(p);
7674 goto flush;
7675 }
7676 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
7677 if ((c = nextc(p)) == -1) goto error;
7678 } while (!whole_match_p(p, eos, len, indent));
7679 str = STR_NEW3(tok(p), toklen(p), enc, func);
7680 }
7681 dispatch_heredoc_end(p);
7682#ifdef RIPPER
7683 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
7684 yylval.val, str);
7685#endif
7686 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7687 token_flush(p);
7688 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
7689 set_yylval_str(str);
7690#ifndef RIPPER
7691 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7692#endif
7693 return tSTRING_CONTENT;
7694}
7695
7696#include "lex.c"
7697
7698static int
7699arg_ambiguous(struct parser_params *p, char c)
7700{
7701#ifndef RIPPER
7702 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
7703#else
7704 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
7705#endif
7706 return TRUE;
7707}
7708
7709static ID
7710formal_argument(struct parser_params *p, ID lhs)
7711{
7712 switch (id_type(lhs)) {
7713 case ID_LOCAL:
7714 break;
7715#ifndef RIPPER
7716 case ID_CONST:
7717 yyerror0("formal argument cannot be a constant");
7718 return 0;
7719 case ID_INSTANCE:
7720 yyerror0("formal argument cannot be an instance variable");
7721 return 0;
7722 case ID_GLOBAL:
7723 yyerror0("formal argument cannot be a global variable");
7724 return 0;
7725 case ID_CLASS:
7726 yyerror0("formal argument cannot be a class variable");
7727 return 0;
7728 default:
7729 yyerror0("formal argument must be local variable");
7730 return 0;
7731#else
7732 default:
7733 lhs = dispatch1(param_error, lhs);
7734 ripper_error(p);
7735 return 0;
7736#endif
7737 }
7738 shadowing_lvar(p, lhs);
7739 return lhs;
7740}
7741
7742static int
7743lvar_defined(struct parser_params *p, ID id)
7744{
7745 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
7746}
7747
7748/* emacsen -*- hack */
7749static long
7750parser_encode_length(struct parser_params *p, const char *name, long len)
7751{
7752 long nlen;
7753
7754 if (len > 5 && name[nlen = len - 5] == '-') {
7755 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
7756 return nlen;
7757 }
7758 if (len > 4 && name[nlen = len - 4] == '-') {
7759 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
7760 return nlen;
7761 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
7762 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
7763 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
7764 return nlen;
7765 }
7766 return len;
7767}
7768
7769static void
7770parser_set_encode(struct parser_params *p, const char *name)
7771{
7772 int idx = rb_enc_find_index(name);
7773 rb_encoding *enc;
7774 VALUE excargs[3];
7775
7776 if (idx < 0) {
7777 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
7778 error:
7779 excargs[0] = rb_eArgError;
7780 excargs[2] = rb_make_backtrace();
7781 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
7782 rb_exc_raise(rb_make_exception(3, excargs));
7783 }
7784 enc = rb_enc_from_index(idx);
7785 if (!rb_enc_asciicompat(enc)) {
7786 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
7787 goto error;
7788 }
7789 p->enc = enc;
7790#ifndef RIPPER
7791 if (p->debug_lines) {
7792 VALUE lines = p->debug_lines;
7793 long i, n = RARRAY_LEN(lines);
7794 for (i = 0; i < n; ++i) {
7795 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
7796 }
7797 }
7798#endif
7799}
7800
7801static int
7802comment_at_top(struct parser_params *p)
7803{
7804 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
7805 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
7806 while (ptr < ptr_end) {
7807 if (!ISSPACE(*ptr)) return 0;
7808 ptr++;
7809 }
7810 return 1;
7811}
7812
7813typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
7814typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
7815
7816static void
7817magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
7818{
7819 if (!comment_at_top(p)) {
7820 return;
7821 }
7822 parser_set_encode(p, val);
7823}
7824
7825static int
7826parser_get_bool(struct parser_params *p, const char *name, const char *val)
7827{
7828 switch (*val) {
7829 case 't': case 'T':
7830 if (strcasecmp(val, "true") == 0) {
7831 return TRUE;
7832 }
7833 break;
7834 case 'f': case 'F':
7835 if (strcasecmp(val, "false") == 0) {
7836 return FALSE;
7837 }
7838 break;
7839 }
7840 rb_compile_warning(p->ruby_sourcefile, p->ruby_sourceline, "invalid value for %s: %s", name, val);
7841 return -1;
7842}
7843
7844static void
7845parser_set_token_info(struct parser_params *p, const char *name, const char *val)
7846{
7847 int b = parser_get_bool(p, name, val);
7848 if (b >= 0) p->token_info_enabled = b;
7849}
7850
7851static void
7852parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
7853{
7854 int b;
7855
7856 if (p->token_seen) {
7857 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
7858 return;
7859 }
7860
7861 b = parser_get_bool(p, name, val);
7862 if (b < 0) return;
7863
7864 if (!p->compile_option)
7865 p->compile_option = rb_obj_hide(rb_ident_hash_new());
7866 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
7867 (b ? Qtrue : Qfalse));
7868}
7869
7870# if WARN_PAST_SCOPE
7871static void
7872parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
7873{
7874 int b = parser_get_bool(p, name, val);
7875 if (b >= 0) p->past_scope_enabled = b;
7876}
7877# endif
7878
7879struct magic_comment {
7880 const char *name;
7881 rb_magic_comment_setter_t func;
7882 rb_magic_comment_length_t length;
7883};
7884
7885static const struct magic_comment magic_comments[] = {
7886 {"coding", magic_comment_encoding, parser_encode_length},
7887 {"encoding", magic_comment_encoding, parser_encode_length},
7888 {"frozen_string_literal", parser_set_compile_option_flag},
7889 {"warn_indent", parser_set_token_info},
7890# if WARN_PAST_SCOPE
7891 {"warn_past_scope", parser_set_past_scope},
7892# endif
7893};
7894
7895static const char *
7896magic_comment_marker(const char *str, long len)
7897{
7898 long i = 2;
7899
7900 while (i < len) {
7901 switch (str[i]) {
7902 case '-':
7903 if (str[i-1] == '*' && str[i-2] == '-') {
7904 return str + i + 1;
7905 }
7906 i += 2;
7907 break;
7908 case '*':
7909 if (i + 1 >= len) return 0;
7910 if (str[i+1] != '-') {
7911 i += 4;
7912 }
7913 else if (str[i-1] != '-') {
7914 i += 2;
7915 }
7916 else {
7917 return str + i + 2;
7918 }
7919 break;
7920 default:
7921 i += 3;
7922 break;
7923 }
7924 }
7925 return 0;
7926}
7927
7928static int
7929parser_magic_comment(struct parser_params *p, const char *str, long len)
7930{
7931 int indicator = 0;
7932 VALUE name = 0, val = 0;
7933 const char *beg, *end, *vbeg, *vend;
7934#define str_copy(_s, _p, _n) ((_s) \
7935 ? (void)(rb_str_resize((_s), (_n)), \
7936 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
7937 : (void)((_s) = STR_NEW((_p), (_n))))
7938
7939 if (len <= 7) return FALSE;
7940 if (!!(beg = magic_comment_marker(str, len))) {
7941 if (!(end = magic_comment_marker(beg, str + len - beg)))
7942 return FALSE;
7943 indicator = TRUE;
7944 str = beg;
7945 len = end - beg - 3;
7946 }
7947
7948 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
7949 while (len > 0) {
7950 const struct magic_comment *mc = magic_comments;
7951 char *s;
7952 int i;
7953 long n = 0;
7954
7955 for (; len > 0 && *str; str++, --len) {
7956 switch (*str) {
7957 case '\'': case '"': case ':': case ';':
7958 continue;
7959 }
7960 if (!ISSPACE(*str)) break;
7961 }
7962 for (beg = str; len > 0; str++, --len) {
7963 switch (*str) {
7964 case '\'': case '"': case ':': case ';':
7965 break;
7966 default:
7967 if (ISSPACE(*str)) break;
7968 continue;
7969 }
7970 break;
7971 }
7972 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
7973 if (!len) break;
7974 if (*str != ':') {
7975 if (!indicator) return FALSE;
7976 continue;
7977 }
7978
7979 do str++; while (--len > 0 && ISSPACE(*str));
7980 if (!len) break;
7981 if (*str == '"') {
7982 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
7983 if (*str == '\\') {
7984 --len;
7985 ++str;
7986 }
7987 }
7988 vend = str;
7989 if (len) {
7990 --len;
7991 ++str;
7992 }
7993 }
7994 else {
7995 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
7996 vend = str;
7997 }
7998 if (indicator) {
7999 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
8000 }
8001 else {
8002 while (len > 0 && (ISSPACE(*str))) --len, str++;
8003 if (len) return FALSE;
8004 }
8005
8006 n = end - beg;
8007 str_copy(name, beg, n);
8008 s = RSTRING_PTR(name);
8009 for (i = 0; i < n; ++i) {
8010 if (s[i] == '-') s[i] = '_';
8011 }
8012 do {
8013 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
8014 n = vend - vbeg;
8015 if (mc->length) {
8016 n = (*mc->length)(p, vbeg, n);
8017 }
8018 str_copy(val, vbeg, n);
8019 (*mc->func)(p, mc->name, RSTRING_PTR(val));
8020 break;
8021 }
8022 } while (++mc < magic_comments + numberof(magic_comments));
8023#ifdef RIPPER
8024 str_copy(val, vbeg, vend - vbeg);
8025 dispatch2(magic_comment, name, val);
8026#endif
8027 }
8028
8029 return TRUE;
8030}
8031
8032static void
8033set_file_encoding(struct parser_params *p, const char *str, const char *send)
8034{
8035 int sep = 0;
8036 const char *beg = str;
8037 VALUE s;
8038
8039 for (;;) {
8040 if (send - str <= 6) return;
8041 switch (str[6]) {
8042 case 'C': case 'c': str += 6; continue;
8043 case 'O': case 'o': str += 5; continue;
8044 case 'D': case 'd': str += 4; continue;
8045 case 'I': case 'i': str += 3; continue;
8046 case 'N': case 'n': str += 2; continue;
8047 case 'G': case 'g': str += 1; continue;
8048 case '=': case ':':
8049 sep = 1;
8050 str += 6;
8051 break;
8052 default:
8053 str += 6;
8054 if (ISSPACE(*str)) break;
8055 continue;
8056 }
8057 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8058 }
8059 for (;;) {
8060 do {
8061 if (++str >= send) return;
8062 } while (ISSPACE(*str));
8063 if (sep) break;
8064 if (*str != '=' && *str != ':') return;
8065 sep = 1;
8066 str++;
8067 }
8068 beg = str;
8069 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8070 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8071 parser_set_encode(p, RSTRING_PTR(s));
8072 rb_str_resize(s, 0);
8073}
8074
8075static void
8076parser_prepare(struct parser_params *p)
8077{
8078 int c = nextc(p);
8079 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8080 switch (c) {
8081 case '#':
8082 if (peek(p, '!')) p->has_shebang = 1;
8083 break;
8084 case 0xef: /* UTF-8 BOM marker */
8085 if (p->lex.pend - p->lex.pcur >= 2 &&
8086 (unsigned char)p->lex.pcur[0] == 0xbb &&
8087 (unsigned char)p->lex.pcur[1] == 0xbf) {
8088 p->enc = rb_utf8_encoding();
8089 p->lex.pcur += 2;
8090 p->lex.pbeg = p->lex.pcur;
8091 return;
8092 }
8093 break;
8094 case EOF:
8095 return;
8096 }
8097 pushback(p, c);
8098 p->enc = rb_enc_get(p->lex.lastline);
8099}
8100
8101#ifndef RIPPER
8102#define ambiguous_operator(tok, op, syn) ( \
8103 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8104 rb_warning0("even though it seems like "syn""))
8105#else
8106#define ambiguous_operator(tok, op, syn) \
8107 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8108#endif
8109#define warn_balanced(tok, op, syn) ((void) \
8110 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8111 space_seen && !ISSPACE(c) && \
8112 (ambiguous_operator(tok, op, syn), 0)), \
8113 (enum yytokentype)(tok))
8114
8115static VALUE
8116parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8117{
8118 VALUE v;
8119 char *point = &str[seen_point];
8120 size_t fraclen = len-seen_point-1;
8121 memmove(point, point+1, fraclen+1);
8122 v = rb_cstr_to_inum(str, 10, FALSE);
8123 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8124}
8125
8126static enum yytokentype
8127no_digits(struct parser_params *p)
8128{
8129 yyerror0("numeric literal without digits");
8130 if (peek(p, '_')) nextc(p);
8131 /* dummy 0, for tUMINUS_NUM at numeric */
8132 return set_integer_literal(p, INT2FIX(0), 0);
8133}
8134
8135static enum yytokentype
8136parse_numeric(struct parser_params *p, int c)
8137{
8138 int is_float, seen_point, seen_e, nondigit;
8139 int suffix;
8140
8141 is_float = seen_point = seen_e = nondigit = 0;
8142 SET_LEX_STATE(EXPR_END);
8143 newtok(p);
8144 if (c == '-' || c == '+') {
8145 tokadd(p, c);
8146 c = nextc(p);
8147 }
8148 if (c == '0') {
8149 int start = toklen(p);
8150 c = nextc(p);
8151 if (c == 'x' || c == 'X') {
8152 /* hexadecimal */
8153 c = nextc(p);
8154 if (c != -1 && ISXDIGIT(c)) {
8155 do {
8156 if (c == '_') {
8157 if (nondigit) break;
8158 nondigit = c;
8159 continue;
8160 }
8161 if (!ISXDIGIT(c)) break;
8162 nondigit = 0;
8163 tokadd(p, c);
8164 } while ((c = nextc(p)) != -1);
8165 }
8166 pushback(p, c);
8167 tokfix(p);
8168 if (toklen(p) == start) {
8169 return no_digits(p);
8170 }
8171 else if (nondigit) goto trailing_uc;
8172 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8173 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8174 }
8175 if (c == 'b' || c == 'B') {
8176 /* binary */
8177 c = nextc(p);
8178 if (c == '0' || c == '1') {
8179 do {
8180 if (c == '_') {
8181 if (nondigit) break;
8182 nondigit = c;
8183 continue;
8184 }
8185 if (c != '0' && c != '1') break;
8186 nondigit = 0;
8187 tokadd(p, c);
8188 } while ((c = nextc(p)) != -1);
8189 }
8190 pushback(p, c);
8191 tokfix(p);
8192 if (toklen(p) == start) {
8193 return no_digits(p);
8194 }
8195 else if (nondigit) goto trailing_uc;
8196 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8197 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8198 }
8199 if (c == 'd' || c == 'D') {
8200 /* decimal */
8201 c = nextc(p);
8202 if (c != -1 && ISDIGIT(c)) {
8203 do {
8204 if (c == '_') {
8205 if (nondigit) break;
8206 nondigit = c;
8207 continue;
8208 }
8209 if (!ISDIGIT(c)) break;
8210 nondigit = 0;
8211 tokadd(p, c);
8212 } while ((c = nextc(p)) != -1);
8213 }
8214 pushback(p, c);
8215 tokfix(p);
8216 if (toklen(p) == start) {
8217 return no_digits(p);
8218 }
8219 else if (nondigit) goto trailing_uc;
8220 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8221 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8222 }
8223 if (c == '_') {
8224 /* 0_0 */
8225 goto octal_number;
8226 }
8227 if (c == 'o' || c == 'O') {
8228 /* prefixed octal */
8229 c = nextc(p);
8230 if (c == -1 || c == '_' || !ISDIGIT(c)) {
8231 return no_digits(p);
8232 }
8233 }
8234 if (c >= '0' && c <= '7') {
8235 /* octal */
8236 octal_number:
8237 do {
8238 if (c == '_') {
8239 if (nondigit) break;
8240 nondigit = c;
8241 continue;
8242 }
8243 if (c < '0' || c > '9') break;
8244 if (c > '7') goto invalid_octal;
8245 nondigit = 0;
8246 tokadd(p, c);
8247 } while ((c = nextc(p)) != -1);
8248 if (toklen(p) > start) {
8249 pushback(p, c);
8250 tokfix(p);
8251 if (nondigit) goto trailing_uc;
8252 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8253 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8254 }
8255 if (nondigit) {
8256 pushback(p, c);
8257 goto trailing_uc;
8258 }
8259 }
8260 if (c > '7' && c <= '9') {
8261 invalid_octal:
8262 yyerror0("Invalid octal digit");
8263 }
8264 else if (c == '.' || c == 'e' || c == 'E') {
8265 tokadd(p, '0');
8266 }
8267 else {
8268 pushback(p, c);
8269 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8270 return set_integer_literal(p, INT2FIX(0), suffix);
8271 }
8272 }
8273
8274 for (;;) {
8275 switch (c) {
8276 case '0': case '1': case '2': case '3': case '4':
8277 case '5': case '6': case '7': case '8': case '9':
8278 nondigit = 0;
8279 tokadd(p, c);
8280 break;
8281
8282 case '.':
8283 if (nondigit) goto trailing_uc;
8284 if (seen_point || seen_e) {
8285 goto decode_num;
8286 }
8287 else {
8288 int c0 = nextc(p);
8289 if (c0 == -1 || !ISDIGIT(c0)) {
8290 pushback(p, c0);
8291 goto decode_num;
8292 }
8293 c = c0;
8294 }
8295 seen_point = toklen(p);
8296 tokadd(p, '.');
8297 tokadd(p, c);
8298 is_float++;
8299 nondigit = 0;
8300 break;
8301
8302 case 'e':
8303 case 'E':
8304 if (nondigit) {
8305 pushback(p, c);
8306 c = nondigit;
8307 goto decode_num;
8308 }
8309 if (seen_e) {
8310 goto decode_num;
8311 }
8312 nondigit = c;
8313 c = nextc(p);
8314 if (c != '-' && c != '+' && !ISDIGIT(c)) {
8315 pushback(p, c);
8316 nondigit = 0;
8317 goto decode_num;
8318 }
8319 tokadd(p, nondigit);
8320 seen_e++;
8321 is_float++;
8322 tokadd(p, c);
8323 nondigit = (c == '-' || c == '+') ? c : 0;
8324 break;
8325
8326 case '_': /* `_' in number just ignored */
8327 if (nondigit) goto decode_num;
8328 nondigit = c;
8329 break;
8330
8331 default:
8332 goto decode_num;
8333 }
8334 c = nextc(p);
8335 }
8336
8337 decode_num:
8338 pushback(p, c);
8339 if (nondigit) {
8340 trailing_uc:
8341 literal_flush(p, p->lex.pcur - 1);
8342 YYLTYPE loc = RUBY_INIT_YYLLOC();
8343 compile_error(p, "trailing `%c' in number", nondigit);
8344 parser_show_error_line(p, &loc);
8345 }
8346 tokfix(p);
8347 if (is_float) {
8348 enum yytokentype type = tFLOAT;
8349 VALUE v;
8350
8351 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8352 if (suffix & NUM_SUFFIX_R) {
8353 type = tRATIONAL;
8354 v = parse_rational(p, tok(p), toklen(p), seen_point);
8355 }
8356 else {
8357 double d = strtod(tok(p), 0);
8358 if (errno == ERANGE) {
8359 rb_warning1("Float %s out of range", WARN_S(tok(p)));
8360 errno = 0;
8361 }
8362 v = DBL2NUM(d);
8363 }
8364 return set_number_literal(p, v, type, suffix);
8365 }
8366 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8367 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8368}
8369
8370static enum yytokentype
8371parse_qmark(struct parser_params *p, int space_seen)
8372{
8373 rb_encoding *enc;
8374 register int c;
8375 VALUE lit;
8376
8377 if (IS_END()) {
8378 SET_LEX_STATE(EXPR_VALUE);
8379 return '?';
8380 }
8381 c = nextc(p);
8382 if (c == -1) {
8383 compile_error(p, "incomplete character syntax");
8384 return 0;
8385 }
8386 if (rb_enc_isspace(c, p->enc)) {
8387 if (!IS_ARG()) {
8388 int c2 = escaped_control_code(c);
8389 if (c2) {
8390 WARN_SPACE_CHAR(c2, "?");
8391 }
8392 }
8393 ternary:
8394 pushback(p, c);
8395 SET_LEX_STATE(EXPR_VALUE);
8396 return '?';
8397 }
8398 newtok(p);
8399 enc = p->enc;
8400 if (!parser_isascii(p)) {
8401 if (tokadd_mbchar(p, c) == -1) return 0;
8402 }
8403 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8404 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8405 if (space_seen) {
8406 const char *start = p->lex.pcur - 1, *ptr = start;
8407 do {
8408 int n = parser_precise_mbclen(p, ptr);
8409 if (n < 0) return -1;
8410 ptr += n;
8411 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8412 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8413 " a conditional operator, put a space after `?'",
8414 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8415 }
8416 goto ternary;
8417 }
8418 else if (c == '\\') {
8419 if (peek(p, 'u')) {
8420 nextc(p);
8421 enc = rb_utf8_encoding();
8422 tokadd_utf8(p, &enc, -1, 0, 0);
8423 }
8424 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8425 nextc(p);
8426 if (tokadd_mbchar(p, c) == -1) return 0;
8427 }
8428 else {
8429 c = read_escape(p, 0, &enc);
8430 tokadd(p, c);
8431 }
8432 }
8433 else {
8434 tokadd(p, c);
8435 }
8436 tokfix(p);
8437 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8438 set_yylval_str(lit);
8439 SET_LEX_STATE(EXPR_END);
8440 return tCHAR;
8441}
8442
8443static enum yytokentype
8444parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8445{
8446 register int c;
8447 const char *ptok = p->lex.pcur;
8448
8449 if (IS_BEG()) {
8450 int term;
8451 int paren;
8452
8453 c = nextc(p);
8454 quotation:
8455 if (c == -1 || !ISALNUM(c)) {
8456 term = c;
8457 c = 'Q';
8458 }
8459 else {
8460 term = nextc(p);
8461 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8462 yyerror0("unknown type of %string");
8463 return 0;
8464 }
8465 }
8466 if (c == -1 || term == -1) {
8467 compile_error(p, "unterminated quoted string meets end of file");
8468 return 0;
8469 }
8470 paren = term;
8471 if (term == '(') term = ')';
8472 else if (term == '[') term = ']';
8473 else if (term == '{') term = '}';
8474 else if (term == '<') term = '>';
8475 else paren = 0;
8476
8477 p->lex.ptok = ptok-1;
8478 switch (c) {
8479 case 'Q':
8480 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8481 return tSTRING_BEG;
8482
8483 case 'q':
8484 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8485 return tSTRING_BEG;
8486
8487 case 'W':
8488 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8489 return tWORDS_BEG;
8490
8491 case 'w':
8492 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8493 return tQWORDS_BEG;
8494
8495 case 'I':
8496 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8497 return tSYMBOLS_BEG;
8498
8499 case 'i':
8500 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8501 return tQSYMBOLS_BEG;
8502
8503 case 'x':
8504 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8505 return tXSTRING_BEG;
8506
8507 case 'r':
8508 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8509 return tREGEXP_BEG;
8510
8511 case 's':
8512 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8513 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8514 return tSYMBEG;
8515
8516 default:
8517 yyerror0("unknown type of %string");
8518 return 0;
8519 }
8520 }
8521 if ((c = nextc(p)) == '=') {
8522 set_yylval_id('%');
8523 SET_LEX_STATE(EXPR_BEG);
8524 return tOP_ASGN;
8525 }
8526 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8527 goto quotation;
8528 }
8529 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8530 pushback(p, c);
8531 return warn_balanced('%', "%%", "string literal");
8532}
8533
8534static int
8535tokadd_ident(struct parser_params *p, int c)
8536{
8537 do {
8538 if (tokadd_mbchar(p, c) == -1) return -1;
8539 c = nextc(p);
8540 } while (parser_is_identchar(p));
8541 pushback(p, c);
8542 return 0;
8543}
8544
8545static ID
8546tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8547{
8548 ID ident = TOK_INTERN();
8549
8550 set_yylval_name(ident);
8551
8552 return ident;
8553}
8554
8555static int
8556parse_numvar(struct parser_params *p)
8557{
8558 size_t len;
8559 int overflow;
8560 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8561 const unsigned long nth_ref_max =
8562 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8563 /* NTH_REF is left-shifted to be ORed with back-ref flag and
8564 * turned into a Fixnum, in compile.c */
8565
8566 if (overflow || n > nth_ref_max) {
8567 /* compile_error()? */
8568 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8569 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8570 }
8571 else {
8572 return (int)n;
8573 }
8574}
8575
8576static enum yytokentype
8577parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8578{
8579 const char *ptr = p->lex.pcur;
8580 register int c;
8581
8582 SET_LEX_STATE(EXPR_END);
8583 p->lex.ptok = ptr - 1; /* from '$' */
8584 newtok(p);
8585 c = nextc(p);
8586 switch (c) {
8587 case '_': /* $_: last read line string */
8588 c = nextc(p);
8589 if (parser_is_identchar(p)) {
8590 tokadd(p, '$');
8591 tokadd(p, '_');
8592 break;
8593 }
8594 pushback(p, c);
8595 c = '_';
8596 /* fall through */
8597 case '~': /* $~: match-data */
8598 case '*': /* $*: argv */
8599 case '$': /* $$: pid */
8600 case '?': /* $?: last status */
8601 case '!': /* $!: error string */
8602 case '@': /* $@: error position */
8603 case '/': /* $/: input record separator */
8604 case '\\': /* $\: output record separator */
8605 case ';': /* $;: field separator */
8606 case ',': /* $,: output field separator */
8607 case '.': /* $.: last read line number */
8608 case '=': /* $=: ignorecase */
8609 case ':': /* $:: load path */
8610 case '<': /* $<: reading filename */
8611 case '>': /* $>: default output handle */
8612 case '\"': /* $": already loaded files */
8613 tokadd(p, '$');
8614 tokadd(p, c);
8615 goto gvar;
8616
8617 case '-':
8618 tokadd(p, '$');
8619 tokadd(p, c);
8620 c = nextc(p);
8621 if (parser_is_identchar(p)) {
8622 if (tokadd_mbchar(p, c) == -1) return 0;
8623 }
8624 else {
8625 pushback(p, c);
8626 pushback(p, '-');
8627 return '$';
8628 }
8629 gvar:
8630 set_yylval_name(TOK_INTERN());
8631 return tGVAR;
8632
8633 case '&': /* $&: last match */
8634 case '`': /* $`: string before last match */
8635 case '\'': /* $': string after last match */
8636 case '+': /* $+: string matches last paren. */
8637 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
8638 tokadd(p, '$');
8639 tokadd(p, c);
8640 goto gvar;
8641 }
8642 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
8643 return tBACK_REF;
8644
8645 case '1': case '2': case '3':
8646 case '4': case '5': case '6':
8647 case '7': case '8': case '9':
8648 tokadd(p, '$');
8649 do {
8650 tokadd(p, c);
8651 c = nextc(p);
8652 } while (c != -1 && ISDIGIT(c));
8653 pushback(p, c);
8654 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
8655 tokfix(p);
8656 set_yylval_node(NEW_NTH_REF(parse_numvar(p), &_cur_loc));
8657 return tNTH_REF;
8658
8659 default:
8660 if (!parser_is_identchar(p)) {
8661 YYLTYPE loc = RUBY_INIT_YYLLOC();
8662 if (c == -1 || ISSPACE(c)) {
8663 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
8664 }
8665 else {
8666 pushback(p, c);
8667 compile_error(p, "`$%c' is not allowed as a global variable name", c);
8668 }
8669 parser_show_error_line(p, &loc);
8670 set_yylval_noname();
8671 return tGVAR;
8672 }
8673 /* fall through */
8674 case '0':
8675 tokadd(p, '$');
8676 }
8677
8678 if (tokadd_ident(p, c)) return 0;
8679 SET_LEX_STATE(EXPR_END);
8680 tokenize_ident(p, last_state);
8681 return tGVAR;
8682}
8683
8684#ifndef RIPPER
8685static bool
8686parser_numbered_param(struct parser_params *p, int n)
8687{
8688 if (n < 0) return false;
8689
8690 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
8691 return false;
8692 }
8693 if (p->max_numparam == ORDINAL_PARAM) {
8694 compile_error(p, "ordinary parameter is defined");
8695 return false;
8696 }
8697 struct vtable *args = p->lvtbl->args;
8698 if (p->max_numparam < n) {
8699 p->max_numparam = n;
8700 }
8701 while (n > args->pos) {
8702 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
8703 }
8704 return true;
8705}
8706#endif
8707
8708static enum yytokentype
8709parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
8710{
8711 const char *ptr = p->lex.pcur;
8712 enum yytokentype result = tIVAR;
8713 register int c = nextc(p);
8714 YYLTYPE loc;
8715
8716 p->lex.ptok = ptr - 1; /* from '@' */
8717 newtok(p);
8718 tokadd(p, '@');
8719 if (c == '@') {
8720 result = tCVAR;
8721 tokadd(p, '@');
8722 c = nextc(p);
8723 }
8724 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
8725 if (c == -1 || !parser_is_identchar(p)) {
8726 pushback(p, c);
8727 RUBY_SET_YYLLOC(loc);
8728 if (result == tIVAR) {
8729 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
8730 }
8731 else {
8732 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
8733 }
8734 parser_show_error_line(p, &loc);
8735 set_yylval_noname();
8736 SET_LEX_STATE(EXPR_END);
8737 return result;
8738 }
8739 else if (ISDIGIT(c)) {
8740 pushback(p, c);
8741 RUBY_SET_YYLLOC(loc);
8742 if (result == tIVAR) {
8743 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
8744 }
8745 else {
8746 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
8747 }
8748 parser_show_error_line(p, &loc);
8749 set_yylval_noname();
8750 SET_LEX_STATE(EXPR_END);
8751 return result;
8752 }
8753
8754 if (tokadd_ident(p, c)) return 0;
8755 tokenize_ident(p, last_state);
8756 return result;
8757}
8758
8759static enum yytokentype
8760parse_ident(struct parser_params *p, int c, int cmd_state)
8761{
8762 enum yytokentype result;
8763 int mb = ENC_CODERANGE_7BIT;
8764 const enum lex_state_e last_state = p->lex.state;
8765 ID ident;
8766
8767 do {
8768 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
8769 if (tokadd_mbchar(p, c) == -1) return 0;
8770 c = nextc(p);
8771 } while (parser_is_identchar(p));
8772 if ((c == '!' || c == '?') && !peek(p, '=')) {
8773 result = tFID;
8774 tokadd(p, c);
8775 }
8776 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
8777 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
8778 result = tIDENTIFIER;
8779 tokadd(p, c);
8780 }
8781 else {
8782 result = tCONSTANT; /* assume provisionally */
8783 pushback(p, c);
8784 }
8785 tokfix(p);
8786
8787 if (IS_LABEL_POSSIBLE()) {
8788 if (IS_LABEL_SUFFIX(0)) {
8789 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8790 nextc(p);
8791 set_yylval_name(TOK_INTERN());
8792 return tLABEL;
8793 }
8794 }
8795 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
8796 const struct kwtable *kw;
8797
8798 /* See if it is a reserved word. */
8799 kw = rb_reserved_word(tok(p), toklen(p));
8800 if (kw) {
8801 enum lex_state_e state = p->lex.state;
8802 if (IS_lex_state_for(state, EXPR_FNAME)) {
8803 SET_LEX_STATE(EXPR_ENDFN);
8804 set_yylval_name(rb_intern2(tok(p), toklen(p)));
8805 return kw->id[0];
8806 }
8807 SET_LEX_STATE(kw->state);
8808 if (IS_lex_state(EXPR_BEG)) {
8809 p->command_start = TRUE;
8810 }
8811 if (kw->id[0] == keyword_do) {
8812 if (lambda_beginning_p()) {
8813 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
8814 return keyword_do_LAMBDA;
8815 }
8816 if (COND_P()) return keyword_do_cond;
8817 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
8818 return keyword_do_block;
8819 return keyword_do;
8820 }
8821 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
8822 return kw->id[0];
8823 else {
8824 if (kw->id[0] != kw->id[1])
8825 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
8826 return kw->id[1];
8827 }
8828 }
8829 }
8830
8831 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
8832 if (cmd_state) {
8833 SET_LEX_STATE(EXPR_CMDARG);
8834 }
8835 else {
8836 SET_LEX_STATE(EXPR_ARG);
8837 }
8838 }
8839 else if (p->lex.state == EXPR_FNAME) {
8840 SET_LEX_STATE(EXPR_ENDFN);
8841 }
8842 else {
8843 SET_LEX_STATE(EXPR_END);
8844 }
8845
8846 ident = tokenize_ident(p, last_state);
8847 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
8848 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
8849 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
8850 lvar_defined(p, ident)) {
8851 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
8852 }
8853 return result;
8854}
8855
8856static enum yytokentype
8857parser_yylex(struct parser_params *p)
8858{
8859 register int c;
8860 int space_seen = 0;
8861 int cmd_state;
8862 int label;
8863 enum lex_state_e last_state;
8864 int fallthru = FALSE;
8865 int token_seen = p->token_seen;
8866
8867 if (p->lex.strterm) {
8868 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
8869 return here_document(p, &p->lex.strterm->u.heredoc);
8870 }
8871 else {
8872 token_flush(p);
8873 return parse_string(p, &p->lex.strterm->u.literal);
8874 }
8875 }
8876 cmd_state = p->command_start;
8877 p->command_start = FALSE;
8878 p->token_seen = TRUE;
8879 retry:
8880 last_state = p->lex.state;
8881#ifndef RIPPER
8882 token_flush(p);
8883#endif
8884 switch (c = nextc(p)) {
8885 case '\0': /* NUL */
8886 case '\004': /* ^D */
8887 case '\032': /* ^Z */
8888 case -1: /* end of script. */
8889 return 0;
8890
8891 /* white spaces */
8892 case ' ': case '\t': case '\f': case '\r':
8893 case '\13': /* '\v' */
8894 space_seen = 1;
8895#ifdef RIPPER
8896 while ((c = nextc(p))) {
8897 switch (c) {
8898 case ' ': case '\t': case '\f': case '\r':
8899 case '\13': /* '\v' */
8900 break;
8901 default:
8902 goto outofloop;
8903 }
8904 }
8905 outofloop:
8906 pushback(p, c);
8907 dispatch_scan_event(p, tSP);
8908#endif
8909 goto retry;
8910
8911 case '#': /* it's a comment */
8912 p->token_seen = token_seen;
8913 /* no magic_comment in shebang line */
8914 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
8915 if (comment_at_top(p)) {
8916 set_file_encoding(p, p->lex.pcur, p->lex.pend);
8917 }
8918 }
8919 lex_goto_eol(p);
8920 dispatch_scan_event(p, tCOMMENT);
8921 fallthru = TRUE;
8922 /* fall through */
8923 case '\n':
8924 p->token_seen = token_seen;
8925 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
8926 !IS_lex_state(EXPR_LABELED));
8927 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
8928 if (!fallthru) {
8929 dispatch_scan_event(p, tIGNORED_NL);
8930 }
8931 fallthru = FALSE;
8932 if (!c && p->in_kwarg) {
8933 goto normal_newline;
8934 }
8935 goto retry;
8936 }
8937 while (1) {
8938 switch (c = nextc(p)) {
8939 case ' ': case '\t': case '\f': case '\r':
8940 case '\13': /* '\v' */
8941 space_seen = 1;
8942 break;
8943 case '#':
8944 pushback(p, c);
8945 if (space_seen) dispatch_scan_event(p, tSP);
8946 goto retry;
8947 case '&':
8948 case '.': {
8949 dispatch_delayed_token(p, tIGNORED_NL);
8950 if (peek(p, '.') == (c == '&')) {
8951 pushback(p, c);
8952 dispatch_scan_event(p, tSP);
8953 goto retry;
8954 }
8955 }
8956 default:
8957 p->ruby_sourceline--;
8958 p->lex.nextline = p->lex.lastline;
8959 case -1: /* EOF no decrement*/
8960#ifndef RIPPER
8961 if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
8962 p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
8963 p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
8964 pushback(p, 1); /* always pushback */
8965 p->lex.ptok = p->lex.pcur;
8966#else
8967 lex_goto_eol(p);
8968 if (c != -1) {
8969 p->lex.ptok = p->lex.pcur;
8970 }
8971#endif
8972 goto normal_newline;
8973 }
8974 }
8975 normal_newline:
8976 p->command_start = TRUE;
8977 SET_LEX_STATE(EXPR_BEG);
8978 return '\n';
8979
8980 case '*':
8981 if ((c = nextc(p)) == '*') {
8982 if ((c = nextc(p)) == '=') {
8983 set_yylval_id(idPow);
8984 SET_LEX_STATE(EXPR_BEG);
8985 return tOP_ASGN;
8986 }
8987 pushback(p, c);
8988 if (IS_SPCARG(c)) {
8989 rb_warning0("`**' interpreted as argument prefix");
8990 c = tDSTAR;
8991 }
8992 else if (IS_BEG()) {
8993 c = tDSTAR;
8994 }
8995 else {
8996 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
8997 }
8998 }
8999 else {
9000 if (c == '=') {
9001 set_yylval_id('*');
9002 SET_LEX_STATE(EXPR_BEG);
9003 return tOP_ASGN;
9004 }
9005 pushback(p, c);
9006 if (IS_SPCARG(c)) {
9007 rb_warning0("`*' interpreted as argument prefix");
9008 c = tSTAR;
9009 }
9010 else if (IS_BEG()) {
9011 c = tSTAR;
9012 }
9013 else {
9014 c = warn_balanced('*', "*", "argument prefix");
9015 }
9016 }
9017 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9018 return c;
9019
9020 case '!':
9021 c = nextc(p);
9022 if (IS_AFTER_OPERATOR()) {
9023 SET_LEX_STATE(EXPR_ARG);
9024 if (c == '@') {
9025 return '!';
9026 }
9027 }
9028 else {
9029 SET_LEX_STATE(EXPR_BEG);
9030 }
9031 if (c == '=') {
9032 return tNEQ;
9033 }
9034 if (c == '~') {
9035 return tNMATCH;
9036 }
9037 pushback(p, c);
9038 return '!';
9039
9040 case '=':
9041 if (was_bol(p)) {
9042 /* skip embedded rd document */
9043 if (word_match_p(p, "begin", 5)) {
9044 int first_p = TRUE;
9045
9046 lex_goto_eol(p);
9047 dispatch_scan_event(p, tEMBDOC_BEG);
9048 for (;;) {
9049 lex_goto_eol(p);
9050 if (!first_p) {
9051 dispatch_scan_event(p, tEMBDOC);
9052 }
9053 first_p = FALSE;
9054 c = nextc(p);
9055 if (c == -1) {
9056 compile_error(p, "embedded document meets end of file");
9057 return 0;
9058 }
9059 if (c == '=' && word_match_p(p, "end", 3)) {
9060 break;
9061 }
9062 pushback(p, c);
9063 }
9064 lex_goto_eol(p);
9065 dispatch_scan_event(p, tEMBDOC_END);
9066 goto retry;
9067 }
9068 }
9069
9070 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9071 if ((c = nextc(p)) == '=') {
9072 if ((c = nextc(p)) == '=') {
9073 return tEQQ;
9074 }
9075 pushback(p, c);
9076 return tEQ;
9077 }
9078 if (c == '~') {
9079 return tMATCH;
9080 }
9081 else if (c == '>') {
9082 return tASSOC;
9083 }
9084 pushback(p, c);
9085 return '=';
9086
9087 case '<':
9088 c = nextc(p);
9089 if (c == '<' &&
9090 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9091 !IS_END() &&
9092 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9093 int token = heredoc_identifier(p);
9094 if (token) return token < 0 ? 0 : token;
9095 }
9096 if (IS_AFTER_OPERATOR()) {
9097 SET_LEX_STATE(EXPR_ARG);
9098 }
9099 else {
9100 if (IS_lex_state(EXPR_CLASS))
9101 p->command_start = TRUE;
9102 SET_LEX_STATE(EXPR_BEG);
9103 }
9104 if (c == '=') {
9105 if ((c = nextc(p)) == '>') {
9106 return tCMP;
9107 }
9108 pushback(p, c);
9109 return tLEQ;
9110 }
9111 if (c == '<') {
9112 if ((c = nextc(p)) == '=') {
9113 set_yylval_id(idLTLT);
9114 SET_LEX_STATE(EXPR_BEG);
9115 return tOP_ASGN;
9116 }
9117 pushback(p, c);
9118 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9119 }
9120 pushback(p, c);
9121 return '<';
9122
9123 case '>':
9124 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9125 if ((c = nextc(p)) == '=') {
9126 return tGEQ;
9127 }
9128 if (c == '>') {
9129 if ((c = nextc(p)) == '=') {
9130 set_yylval_id(idGTGT);
9131 SET_LEX_STATE(EXPR_BEG);
9132 return tOP_ASGN;
9133 }
9134 pushback(p, c);
9135 return tRSHFT;
9136 }
9137 pushback(p, c);
9138 return '>';
9139
9140 case '"':
9141 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9142 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9143 p->lex.ptok = p->lex.pcur-1;
9144 return tSTRING_BEG;
9145
9146 case '`':
9147 if (IS_lex_state(EXPR_FNAME)) {
9148 SET_LEX_STATE(EXPR_ENDFN);
9149 return c;
9150 }
9151 if (IS_lex_state(EXPR_DOT)) {
9152 if (cmd_state)
9153 SET_LEX_STATE(EXPR_CMDARG);
9154 else
9155 SET_LEX_STATE(EXPR_ARG);
9156 return c;
9157 }
9158 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9159 return tXSTRING_BEG;
9160
9161 case '\'':
9162 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9163 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9164 p->lex.ptok = p->lex.pcur-1;
9165 return tSTRING_BEG;
9166
9167 case '?':
9168 return parse_qmark(p, space_seen);
9169
9170 case '&':
9171 if ((c = nextc(p)) == '&') {
9172 SET_LEX_STATE(EXPR_BEG);
9173 if ((c = nextc(p)) == '=') {
9174 set_yylval_id(idANDOP);
9175 SET_LEX_STATE(EXPR_BEG);
9176 return tOP_ASGN;
9177 }
9178 pushback(p, c);
9179 return tANDOP;
9180 }
9181 else if (c == '=') {
9182 set_yylval_id('&');
9183 SET_LEX_STATE(EXPR_BEG);
9184 return tOP_ASGN;
9185 }
9186 else if (c == '.') {
9187 set_yylval_id(idANDDOT);
9188 SET_LEX_STATE(EXPR_DOT);
9189 return tANDDOT;
9190 }
9191 pushback(p, c);
9192 if (IS_SPCARG(c)) {
9193 if ((c != ':') ||
9194 (c = peekc_n(p, 1)) == -1 ||
9195 !(c == '\'' || c == '"' ||
9196 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9197 rb_warning0("`&' interpreted as argument prefix");
9198 }
9199 c = tAMPER;
9200 }
9201 else if (IS_BEG()) {
9202 c = tAMPER;
9203 }
9204 else {
9205 c = warn_balanced('&', "&", "argument prefix");
9206 }
9207 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9208 return c;
9209
9210 case '|':
9211 if ((c = nextc(p)) == '|') {
9212 SET_LEX_STATE(EXPR_BEG);
9213 if ((c = nextc(p)) == '=') {
9214 set_yylval_id(idOROP);
9215 SET_LEX_STATE(EXPR_BEG);
9216 return tOP_ASGN;
9217 }
9218 pushback(p, c);
9219 if (IS_lex_state_for(last_state, EXPR_BEG)) {
9220 c = '|';
9221 pushback(p, '|');
9222 return c;
9223 }
9224 return tOROP;
9225 }
9226 if (c == '=') {
9227 set_yylval_id('|');
9228 SET_LEX_STATE(EXPR_BEG);
9229 return tOP_ASGN;
9230 }
9231 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9232 pushback(p, c);
9233 return '|';
9234
9235 case '+':
9236 c = nextc(p);
9237 if (IS_AFTER_OPERATOR()) {
9238 SET_LEX_STATE(EXPR_ARG);
9239 if (c == '@') {
9240 return tUPLUS;
9241 }
9242 pushback(p, c);
9243 return '+';
9244 }
9245 if (c == '=') {
9246 set_yylval_id('+');
9247 SET_LEX_STATE(EXPR_BEG);
9248 return tOP_ASGN;
9249 }
9250 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9251 SET_LEX_STATE(EXPR_BEG);
9252 pushback(p, c);
9253 if (c != -1 && ISDIGIT(c)) {
9254 return parse_numeric(p, '+');
9255 }
9256 return tUPLUS;
9257 }
9258 SET_LEX_STATE(EXPR_BEG);
9259 pushback(p, c);
9260 return warn_balanced('+', "+", "unary operator");
9261
9262 case '-':
9263 c = nextc(p);
9264 if (IS_AFTER_OPERATOR()) {
9265 SET_LEX_STATE(EXPR_ARG);
9266 if (c == '@') {
9267 return tUMINUS;
9268 }
9269 pushback(p, c);
9270 return '-';
9271 }
9272 if (c == '=') {
9273 set_yylval_id('-');
9274 SET_LEX_STATE(EXPR_BEG);
9275 return tOP_ASGN;
9276 }
9277 if (c == '>') {
9278 SET_LEX_STATE(EXPR_ENDFN);
9279 return tLAMBDA;
9280 }
9281 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9282 SET_LEX_STATE(EXPR_BEG);
9283 pushback(p, c);
9284 if (c != -1 && ISDIGIT(c)) {
9285 return tUMINUS_NUM;
9286 }
9287 return tUMINUS;
9288 }
9289 SET_LEX_STATE(EXPR_BEG);
9290 pushback(p, c);
9291 return warn_balanced('-', "-", "unary operator");
9292
9293 case '.': {
9294 int is_beg = IS_BEG();
9295 SET_LEX_STATE(EXPR_BEG);
9296 if ((c = nextc(p)) == '.') {
9297 if ((c = nextc(p)) == '.') {
9298 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9299 rb_warn0("... at EOL, should be parenthesized?");
9300 }
9301 return is_beg ? tBDOT3 : tDOT3;
9302 }
9303 pushback(p, c);
9304 return is_beg ? tBDOT2 : tDOT2;
9305 }
9306 pushback(p, c);
9307 if (c != -1 && ISDIGIT(c)) {
9308 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9309 parse_numeric(p, '.');
9310 if (ISDIGIT(prev)) {
9311 yyerror0("unexpected fraction part after numeric literal");
9312 }
9313 else {
9314 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9315 }
9316 SET_LEX_STATE(EXPR_END);
9317 p->lex.ptok = p->lex.pcur;
9318 goto retry;
9319 }
9320 set_yylval_id('.');
9321 SET_LEX_STATE(EXPR_DOT);
9322 return '.';
9323 }
9324
9325 case '0': case '1': case '2': case '3': case '4':
9326 case '5': case '6': case '7': case '8': case '9':
9327 return parse_numeric(p, c);
9328
9329 case ')':
9330 COND_POP();
9331 CMDARG_POP();
9332 SET_LEX_STATE(EXPR_ENDFN);
9333 p->lex.paren_nest--;
9334 return c;
9335
9336 case ']':
9337 COND_POP();
9338 CMDARG_POP();
9339 SET_LEX_STATE(EXPR_END);
9340 p->lex.paren_nest--;
9341 return c;
9342
9343 case '}':
9344 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9345 if (!p->lex.brace_nest--) return tSTRING_DEND;
9346 COND_POP();
9347 CMDARG_POP();
9348 SET_LEX_STATE(EXPR_END);
9349 p->lex.paren_nest--;
9350 return c;
9351
9352 case ':':
9353 c = nextc(p);
9354 if (c == ':') {
9355 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9356 SET_LEX_STATE(EXPR_BEG);
9357 return tCOLON3;
9358 }
9359 set_yylval_id(idCOLON2);
9360 SET_LEX_STATE(EXPR_DOT);
9361 return tCOLON2;
9362 }
9363 if (IS_END() || ISSPACE(c) || c == '#') {
9364 pushback(p, c);
9365 c = warn_balanced(':', ":", "symbol literal");
9366 SET_LEX_STATE(EXPR_BEG);
9367 return c;
9368 }
9369 switch (c) {
9370 case '\'':
9371 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9372 break;
9373 case '"':
9374 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9375 break;
9376 default:
9377 pushback(p, c);
9378 break;
9379 }
9380 SET_LEX_STATE(EXPR_FNAME);
9381 return tSYMBEG;
9382
9383 case '/':
9384 if (IS_BEG()) {
9385 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9386 return tREGEXP_BEG;
9387 }
9388 if ((c = nextc(p)) == '=') {
9389 set_yylval_id('/');
9390 SET_LEX_STATE(EXPR_BEG);
9391 return tOP_ASGN;
9392 }
9393 pushback(p, c);
9394 if (IS_SPCARG(c)) {
9395 arg_ambiguous(p, '/');
9396 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9397 return tREGEXP_BEG;
9398 }
9399 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9400 return warn_balanced('/', "/", "regexp literal");
9401
9402 case '^':
9403 if ((c = nextc(p)) == '=') {
9404 set_yylval_id('^');
9405 SET_LEX_STATE(EXPR_BEG);
9406 return tOP_ASGN;
9407 }
9408 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9409 pushback(p, c);
9410 return '^';
9411
9412 case ';':
9413 SET_LEX_STATE(EXPR_BEG);
9414 p->command_start = TRUE;
9415 return ';';
9416
9417 case ',':
9418 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9419 return ',';
9420
9421 case '~':
9422 if (IS_AFTER_OPERATOR()) {
9423 if ((c = nextc(p)) != '@') {
9424 pushback(p, c);
9425 }
9426 SET_LEX_STATE(EXPR_ARG);
9427 }
9428 else {
9429 SET_LEX_STATE(EXPR_BEG);
9430 }
9431 return '~';
9432
9433 case '(':
9434 if (IS_BEG()) {
9435 c = tLPAREN;
9436 }
9437 else if (!space_seen) {
9438 /* foo( ... ) => method call, no ambiguity */
9439 }
9440 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9441 c = tLPAREN_ARG;
9442 }
9443 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9444 rb_warning0("parentheses after method name is interpreted as "
9445 "an argument list, not a decomposed argument");
9446 }
9447 p->lex.paren_nest++;
9448 COND_PUSH(0);
9449 CMDARG_PUSH(0);
9450 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9451 return c;
9452
9453 case '[':
9454 p->lex.paren_nest++;
9455 if (IS_AFTER_OPERATOR()) {
9456 if ((c = nextc(p)) == ']') {
9457 SET_LEX_STATE(EXPR_ARG);
9458 if ((c = nextc(p)) == '=') {
9459 return tASET;
9460 }
9461 pushback(p, c);
9462 return tAREF;
9463 }
9464 pushback(p, c);
9465 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9466 return '[';
9467 }
9468 else if (IS_BEG()) {
9469 c = tLBRACK;
9470 }
9471 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9472 c = tLBRACK;
9473 }
9474 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9475 COND_PUSH(0);
9476 CMDARG_PUSH(0);
9477 return c;
9478
9479 case '{':
9480 ++p->lex.brace_nest;
9481 if (lambda_beginning_p())
9482 c = tLAMBEG;
9483 else if (IS_lex_state(EXPR_LABELED))
9484 c = tLBRACE; /* hash */
9485 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9486 c = '{'; /* block (primary) */
9487 else if (IS_lex_state(EXPR_ENDARG))
9488 c = tLBRACE_ARG; /* block (expr) */
9489 else
9490 c = tLBRACE; /* hash */
9491 if (c != tLBRACE) {
9492 p->command_start = TRUE;
9493 SET_LEX_STATE(EXPR_BEG);
9494 }
9495 else {
9496 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9497 }
9498 ++p->lex.paren_nest; /* after lambda_beginning_p() */
9499 COND_PUSH(0);
9500 CMDARG_PUSH(0);
9501 return c;
9502
9503 case '\\':
9504 c = nextc(p);
9505 if (c == '\n') {
9506 space_seen = 1;
9507 dispatch_scan_event(p, tSP);
9508 goto retry; /* skip \\n */
9509 }
9510 if (c == ' ') return tSP;
9511 if (ISSPACE(c)) return c;
9512 pushback(p, c);
9513 return '\\';
9514
9515 case '%':
9516 return parse_percent(p, space_seen, last_state);
9517
9518 case '$':
9519 return parse_gvar(p, last_state);
9520
9521 case '@':
9522 return parse_atmark(p, last_state);
9523
9524 case '_':
9525 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9526 p->ruby__end__seen = 1;
9527 p->eofp = 1;
9528#ifndef RIPPER
9529 return -1;
9530#else
9531 lex_goto_eol(p);
9532 dispatch_scan_event(p, k__END__);
9533 return 0;
9534#endif
9535 }
9536 newtok(p);
9537 break;
9538
9539 default:
9540 if (!parser_is_identchar(p)) {
9541 compile_error(p, "Invalid char `\\x%02X' in expression", c);
9542 token_flush(p);
9543 goto retry;
9544 }
9545
9546 newtok(p);
9547 break;
9548 }
9549
9550 return parse_ident(p, c, cmd_state);
9551}
9552
9553static enum yytokentype
9554yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9555{
9556 enum yytokentype t;
9557
9558 p->lval = lval;
9559 lval->val = Qundef;
9560 t = parser_yylex(p);
9561
9562 if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9563 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9564 else
9565 RUBY_SET_YYLLOC(*yylloc);
9566
9567 if (has_delayed_token(p))
9568 dispatch_delayed_token(p, t);
9569 else if (t != 0)
9570 dispatch_scan_event(p, t);
9571
9572 return t;
9573}
9574
9575#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9576
9577static NODE*
9578node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9579{
9580 NODE *n = rb_ast_newnode(p->ast, type);
9581
9582 rb_node_init(n, type, a0, a1, a2);
9583
9584 nd_set_loc(n, loc);
9585 nd_set_node_id(n, parser_get_node_id(p));
9586 return n;
9587}
9588
9589static NODE *
9590nd_set_loc(NODE *nd, const YYLTYPE *loc)
9591{
9592 nd->nd_loc = *loc;
9593 nd_set_line(nd, loc->beg_pos.lineno);
9594 return nd;
9595}
9596
9597#ifndef RIPPER
9598static enum node_type
9599nodetype(NODE *node) /* for debug */
9600{
9601 return (enum node_type)nd_type(node);
9602}
9603
9604static int
9605nodeline(NODE *node)
9606{
9607 return nd_line(node);
9608}
9609
9610static NODE*
9611newline_node(NODE *node)
9612{
9613 if (node) {
9614 node = remove_begin(node);
9615 node->flags |= NODE_FL_NEWLINE;
9616 }
9617 return node;
9618}
9619
9620static void
9621fixpos(NODE *node, NODE *orig)
9622{
9623 if (!node) return;
9624 if (!orig) return;
9625 nd_set_line(node, nd_line(orig));
9626}
9627
9628static void
9629parser_warning(struct parser_params *p, NODE *node, const char *mesg)
9630{
9631 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9632}
9633
9634static void
9635parser_warn(struct parser_params *p, NODE *node, const char *mesg)
9636{
9637 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9638}
9639
9640static NODE*
9641block_append(struct parser_params *p, NODE *head, NODE *tail)
9642{
9643 NODE *end, *h = head, *nd;
9644
9645 if (tail == 0) return head;
9646
9647 if (h == 0) return tail;
9648 switch (nd_type(h)) {
9649 case NODE_LIT:
9650 case NODE_STR:
9651 case NODE_SELF:
9652 case NODE_TRUE:
9653 case NODE_FALSE:
9654 case NODE_NIL:
9655 parser_warning(p, h, "unused literal ignored");
9656 return tail;
9657 default:
9658 h = end = NEW_BLOCK(head, &head->nd_loc);
9659 end->nd_end = end;
9660 head = end;
9661 break;
9662 case NODE_BLOCK:
9663 end = h->nd_end;
9664 break;
9665 }
9666
9667 nd = end->nd_head;
9668 switch (nd_type(nd)) {
9669 case NODE_RETURN:
9670 case NODE_BREAK:
9671 case NODE_NEXT:
9672 case NODE_REDO:
9673 case NODE_RETRY:
9674 if (RTEST(ruby_verbose)) {
9675 parser_warning(p, tail, "statement not reached");
9676 }
9677 break;
9678
9679 default:
9680 break;
9681 }
9682
9683 if (nd_type(tail) != NODE_BLOCK) {
9684 tail = NEW_BLOCK(tail, &tail->nd_loc);
9685 tail->nd_end = tail;
9686 }
9687 end->nd_next = tail;
9688 h->nd_end = tail->nd_end;
9689 nd_set_last_loc(head, nd_last_loc(tail));
9690 return head;
9691}
9692
9693/* append item to the list */
9694static NODE*
9695list_append(struct parser_params *p, NODE *list, NODE *item)
9696{
9697 NODE *last;
9698
9699 if (list == 0) return NEW_LIST(item, &item->nd_loc);
9700 if (list->nd_next) {
9701 last = list->nd_next->nd_end;
9702 }
9703 else {
9704 last = list;
9705 }
9706
9707 list->nd_alen += 1;
9708 last->nd_next = NEW_LIST(item, &item->nd_loc);
9709 list->nd_next->nd_end = last->nd_next;
9710
9711 nd_set_last_loc(list, nd_last_loc(item));
9712
9713 return list;
9714}
9715
9716/* concat two lists */
9717static NODE*
9718list_concat(NODE *head, NODE *tail)
9719{
9720 NODE *last;
9721
9722 if (head->nd_next) {
9723 last = head->nd_next->nd_end;
9724 }
9725 else {
9726 last = head;
9727 }
9728
9729 head->nd_alen += tail->nd_alen;
9730 last->nd_next = tail;
9731 if (tail->nd_next) {
9732 head->nd_next->nd_end = tail->nd_next->nd_end;
9733 }
9734 else {
9735 head->nd_next->nd_end = tail;
9736 }
9737
9738 nd_set_last_loc(head, nd_last_loc(tail));
9739
9740 return head;
9741}
9742
9743static int
9744literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
9745{
9746 if (NIL_P(tail)) return 1;
9747 if (!rb_enc_compatible(head, tail)) {
9748 compile_error(p, "string literal encodings differ (%s / %s)",
9749 rb_enc_name(rb_enc_get(head)),
9750 rb_enc_name(rb_enc_get(tail)));
9751 rb_str_resize(head, 0);
9752 rb_str_resize(tail, 0);
9753 return 0;
9754 }
9755 rb_str_buf_append(head, tail);
9756 return 1;
9757}
9758
9759/* concat two string literals */
9760static NODE *
9761literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
9762{
9763 enum node_type htype;
9764 NODE *headlast;
9765 VALUE lit;
9766
9767 if (!head) return tail;
9768 if (!tail) return head;
9769
9770 htype = nd_type(head);
9771 if (htype == NODE_EVSTR) {
9772 NODE *node = NEW_DSTR(STR_NEW0(), loc);
9773 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9774 head = list_append(p, node, head);
9775 htype = NODE_DSTR;
9776 }
9777 if (p->heredoc_indent > 0) {
9778 switch (htype) {
9779 case NODE_STR:
9780 nd_set_type(head, NODE_DSTR);
9781 case NODE_DSTR:
9782 return list_append(p, head, tail);
9783 default:
9784 break;
9785 }
9786 }
9787 switch (nd_type(tail)) {
9788 case NODE_STR:
9789 if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9790 nd_type(headlast) == NODE_STR) {
9791 htype = NODE_STR;
9792 lit = headlast->nd_lit;
9793 }
9794 else {
9795 lit = head->nd_lit;
9796 }
9797 if (htype == NODE_STR) {
9798 if (!literal_concat0(p, lit, tail->nd_lit)) {
9799 error:
9800 rb_discard_node(p, head);
9801 rb_discard_node(p, tail);
9802 return 0;
9803 }
9804 rb_discard_node(p, tail);
9805 }
9806 else {
9807 list_append(p, head, tail);
9808 }
9809 break;
9810
9811 case NODE_DSTR:
9812 if (htype == NODE_STR) {
9813 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
9814 goto error;
9815 tail->nd_lit = head->nd_lit;
9816 rb_discard_node(p, head);
9817 head = tail;
9818 }
9819 else if (NIL_P(tail->nd_lit)) {
9820 append:
9821 head->nd_alen += tail->nd_alen - 1;
9822 head->nd_next->nd_end->nd_next = tail->nd_next;
9823 head->nd_next->nd_end = tail->nd_next->nd_end;
9824 rb_discard_node(p, tail);
9825 }
9826 else if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9827 nd_type(headlast) == NODE_STR) {
9828 lit = headlast->nd_lit;
9829 if (!literal_concat0(p, lit, tail->nd_lit))
9830 goto error;
9831 tail->nd_lit = Qnil;
9832 goto append;
9833 }
9834 else {
9835 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
9836 }
9837 break;
9838
9839 case NODE_EVSTR:
9840 if (htype == NODE_STR) {
9841 nd_set_type(head, NODE_DSTR);
9842 head->nd_alen = 1;
9843 }
9844 list_append(p, head, tail);
9845 break;
9846 }
9847 return head;
9848}
9849
9850static NODE *
9851evstr2dstr(struct parser_params *p, NODE *node)
9852{
9853 if (nd_type(node) == NODE_EVSTR) {
9854 NODE * dstr = NEW_DSTR(STR_NEW0(), &node->nd_loc);
9855 RB_OBJ_WRITTEN(p->ast, Qnil, dstr->nd_lit);
9856 node = list_append(p, dstr, node);
9857 }
9858 return node;
9859}
9860
9861static NODE *
9862new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
9863{
9864 NODE *head = node;
9865
9866 if (node) {
9867 switch (nd_type(node)) {
9868 case NODE_STR: case NODE_DSTR: case NODE_EVSTR:
9869 return node;
9870 }
9871 }
9872 return NEW_EVSTR(head, loc);
9873}
9874
9875static NODE *
9876call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
9877 const YYLTYPE *op_loc, const YYLTYPE *loc)
9878{
9879 NODE *expr;
9880 value_expr(recv);
9881 value_expr(arg1);
9882 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
9883 nd_set_line(expr, op_loc->beg_pos.lineno);
9884 return expr;
9885}
9886
9887static NODE *
9888call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
9889{
9890 NODE *opcall;
9891 value_expr(recv);
9892 opcall = NEW_OPCALL(recv, id, 0, loc);
9893 nd_set_line(opcall, op_loc->beg_pos.lineno);
9894 return opcall;
9895}
9896
9897static NODE *
9898new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
9899{
9900 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
9901 nd_set_line(qcall, op_loc->beg_pos.lineno);
9902 return qcall;
9903}
9904
9905static NODE*
9906new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
9907{
9908 NODE *ret;
9909 if (block) block_dup_check(p, args, block);
9910 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
9911 if (block) ret = method_add_block(p, ret, block, loc);
9912 fixpos(ret, recv);
9913 return ret;
9914}
9915
9916#define nd_once_body(node) (nd_type(node) == NODE_ONCE ? (node)->nd_body : node)
9917static NODE*
9918match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
9919{
9920 NODE *n;
9921 int line = op_loc->beg_pos.lineno;
9922
9923 value_expr(node1);
9924 value_expr(node2);
9925 if (node1 && (n = nd_once_body(node1)) != 0) {
9926 switch (nd_type(n)) {
9927 case NODE_DREGX:
9928 {
9929 NODE *match = NEW_MATCH2(node1, node2, loc);
9930 nd_set_line(match, line);
9931 return match;
9932 }
9933
9934 case NODE_LIT:
9935 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
9936 const VALUE lit = n->nd_lit;
9937 NODE *match = NEW_MATCH2(node1, node2, loc);
9938 match->nd_args = reg_named_capture_assign(p, lit, loc);
9939 nd_set_line(match, line);
9940 return match;
9941 }
9942 }
9943 }
9944
9945 if (node2 && (n = nd_once_body(node2)) != 0) {
9946 NODE *match3;
9947
9948 switch (nd_type(n)) {
9949 case NODE_LIT:
9950 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
9951 /* fallthru */
9952 case NODE_DREGX:
9953 match3 = NEW_MATCH3(node2, node1, loc);
9954 return match3;
9955 }
9956 }
9957
9958 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
9959 nd_set_line(n, line);
9960 return n;
9961}
9962
9963# if WARN_PAST_SCOPE
9964static int
9965past_dvar_p(struct parser_params *p, ID id)
9966{
9967 struct vtable *past = p->lvtbl->past;
9968 while (past) {
9969 if (vtable_included(past, id)) return 1;
9970 past = past->prev;
9971 }
9972 return 0;
9973}
9974# endif
9975
9976/* As Ripper#warn does not have arguments for the location, so the
9977 * following messages cannot be separated */
9978#define WARN_LOCATION(type) do { \
9979 if (p->warn_location) { \
9980 int line; \
9981 VALUE file = rb_source_location(&line); \
9982 rb_warn3(type" in eval may not return location in binding;" \
9983 " use Binding#source_location instead\n" \
9984 "%"PRIsWARN":%d: warning: in `%"PRIsWARN"'", \
9985 file, WARN_I(line), rb_id2str(rb_frame_this_func())); \
9986 } \
9987} while (0)
9988
9989static int
9990numparam_nested_p(struct parser_params *p)
9991{
9992 struct local_vars *local = p->lvtbl;
9993 NODE *outer = local->numparam.outer;
9994 NODE *inner = local->numparam.inner;
9995 if (outer || inner) {
9996 NODE *used = outer ? outer : inner;
9997 compile_error(p, "numbered parameter is already used in\n"
9998 "%s:%d: %s block here",
9999 p->ruby_sourcefile, nd_line(used),
10000 outer ? "outer" : "inner");
10001 parser_show_error_line(p, &used->nd_loc);
10002 return 1;
10003 }
10004 return 0;
10005}
10006
10007static NODE*
10008gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
10009{
10010 ID *vidp = NULL;
10011 NODE *node;
10012 switch (id) {
10013 case keyword_self:
10014 return NEW_SELF(loc);
10015 case keyword_nil:
10016 return NEW_NIL(loc);
10017 case keyword_true:
10018 return NEW_TRUE(loc);
10019 case keyword_false:
10020 return NEW_FALSE(loc);
10021 case keyword__FILE__:
10022 WARN_LOCATION("__FILE__");
10023 {
10024 VALUE file = p->ruby_sourcefile_string;
10025 if (NIL_P(file))
10026 file = rb_str_new(0, 0);
10027 else
10028 file = rb_str_dup(file);
10029 node = NEW_STR(file, loc);
10030 RB_OBJ_WRITTEN(p->ast, Qnil, file);
10031 }
10032 return node;
10033 case keyword__LINE__:
10034 WARN_LOCATION("__LINE__");
10035 return NEW_LIT(INT2FIX(p->tokline), loc);
10036 case keyword__ENCODING__:
10037 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
10038 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10039 return node;
10040
10041 }
10042 switch (id_type(id)) {
10043 case ID_LOCAL:
10044 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
10045 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
10046 if (id == p->cur_arg) {
10047 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10048 return 0;
10049 }
10050 if (vidp) *vidp |= LVAR_USED;
10051 node = NEW_DVAR(id, loc);
10052 return node;
10053 }
10054 if (local_id_ref(p, id, &vidp)) {
10055 if (id == p->cur_arg) {
10056 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10057 return 0;
10058 }
10059 if (vidp) *vidp |= LVAR_USED;
10060 node = NEW_LVAR(id, loc);
10061 return node;
10062 }
10063 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
10064 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
10065 if (numparam_nested_p(p)) return 0;
10066 node = NEW_DVAR(id, loc);
10067 struct local_vars *local = p->lvtbl;
10068 if (!local->numparam.current) local->numparam.current = node;
10069 return node;
10070 }
10071# if WARN_PAST_SCOPE
10072 if (!p->in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10073 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10074 }
10075# endif
10076 /* method call without arguments */
10077 return NEW_VCALL(id, loc);
10078 case ID_GLOBAL:
10079 return NEW_GVAR(id, loc);
10080 case ID_INSTANCE:
10081 return NEW_IVAR(id, loc);
10082 case ID_CONST:
10083 return NEW_CONST(id, loc);
10084 case ID_CLASS:
10085 return NEW_CVAR(id, loc);
10086 }
10087 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10088 return 0;
10089}
10090
10091static NODE *
10092opt_arg_append(NODE *opt_list, NODE *opt)
10093{
10094 NODE *opts = opt_list;
10095 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10096
10097 while (opts->nd_next) {
10098 opts = opts->nd_next;
10099 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10100 }
10101 opts->nd_next = opt;
10102
10103 return opt_list;
10104}
10105
10106static NODE *
10107kwd_append(NODE *kwlist, NODE *kw)
10108{
10109 if (kwlist) {
10110 NODE *kws = kwlist;
10111 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10112 while (kws->nd_next) {
10113 kws = kws->nd_next;
10114 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10115 }
10116 kws->nd_next = kw;
10117 }
10118 return kwlist;
10119}
10120
10121static NODE *
10122new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10123{
10124 return NEW_DEFINED(remove_begin_all(expr), loc);
10125}
10126
10127static NODE*
10128symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10129{
10130 if (nd_type(symbol) == NODE_DSTR) {
10131 nd_set_type(symbol, NODE_DSYM);
10132 }
10133 else {
10134 nd_set_type(symbol, NODE_LIT);
10135 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10136 }
10137 return list_append(p, symbols, symbol);
10138}
10139
10140static NODE *
10141new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10142{
10143 NODE *list, *prev;
10144 VALUE lit;
10145
10146 if (!node) {
10147 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10148 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10149 return node;
10150 }
10151 switch (nd_type(node)) {
10152 case NODE_STR:
10153 {
10154 VALUE src = node->nd_lit;
10155 nd_set_type(node, NODE_LIT);
10156 nd_set_loc(node, loc);
10157 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10158 }
10159 break;
10160 default:
10161 lit = STR_NEW0();
10162 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10163 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10164 /* fall through */
10165 case NODE_DSTR:
10166 nd_set_type(node, NODE_DREGX);
10167 nd_set_loc(node, loc);
10168 node->nd_cflag = options & RE_OPTION_MASK;
10169 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10170 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10171 if (nd_type(list->nd_head) == NODE_STR) {
10172 VALUE tail = list->nd_head->nd_lit;
10173 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10174 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10175 if (!literal_concat0(p, lit, tail)) {
10176 return NEW_NIL(loc); /* dummy node on error */
10177 }
10178 rb_str_resize(tail, 0);
10179 prev->nd_next = list->nd_next;
10180 rb_discard_node(p, list->nd_head);
10181 rb_discard_node(p, list);
10182 list = prev;
10183 }
10184 else {
10185 prev = list;
10186 }
10187 }
10188 else {
10189 prev = 0;
10190 }
10191 }
10192 if (!node->nd_next) {
10193 VALUE src = node->nd_lit;
10194 nd_set_type(node, NODE_LIT);
10195 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10196 }
10197 if (options & RE_OPTION_ONCE) {
10198 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10199 }
10200 break;
10201 }
10202 return node;
10203}
10204
10205static NODE *
10206new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10207{
10208 if (!k) return 0;
10209 return NEW_KW_ARG(0, (k), loc);
10210}
10211
10212static NODE *
10213new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10214{
10215 if (!node) {
10216 VALUE lit = STR_NEW0();
10217 NODE *xstr = NEW_XSTR(lit, loc);
10218 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10219 return xstr;
10220 }
10221 switch (nd_type(node)) {
10222 case NODE_STR:
10223 nd_set_type(node, NODE_XSTR);
10224 nd_set_loc(node, loc);
10225 break;
10226 case NODE_DSTR:
10227 nd_set_type(node, NODE_DXSTR);
10228 nd_set_loc(node, loc);
10229 break;
10230 default:
10231 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10232 break;
10233 }
10234 return node;
10235}
10236
10237static void
10238check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10239{
10240 VALUE lit;
10241
10242 if (!arg || !p->case_labels) return;
10243
10244 lit = rb_node_case_when_optimizable_literal(arg);
10245 if (lit == Qundef) return;
10246 if (nd_type(arg) == NODE_STR) {
10247 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10248 }
10249
10250 if (NIL_P(p->case_labels)) {
10251 p->case_labels = rb_obj_hide(rb_hash_new());
10252 }
10253 else {
10254 VALUE line = rb_hash_lookup(p->case_labels, lit);
10255 if (!NIL_P(line)) {
10256 rb_warning1("duplicated `when' clause with line %d is ignored",
10257 WARN_IVAL(line));
10258 return;
10259 }
10260 }
10261 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10262}
10263
10264#else /* !RIPPER */
10265static int
10266id_is_var(struct parser_params *p, ID id)
10267{
10268 if (is_notop_id(id)) {
10269 switch (id & ID_SCOPE_MASK) {
10270 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10271 return 1;
10272 case ID_LOCAL:
10273 if (dyna_in_block(p)) {
10274 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10275 }
10276 if (local_id(p, id)) return 1;
10277 /* method call without arguments */
10278 return 0;
10279 }
10280 }
10281 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10282 return 0;
10283}
10284
10285static VALUE
10286new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10287{
10288 VALUE src = 0, err;
10289 int options = 0;
10290 if (ripper_is_node_yylval(re)) {
10291 src = RNODE(re)->nd_cval;
10292 re = RNODE(re)->nd_rval;
10293 }
10294 if (ripper_is_node_yylval(opt)) {
10295 options = (int)RNODE(opt)->nd_tag;
10296 opt = RNODE(opt)->nd_rval;
10297 }
10298 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10299 compile_error(p, "%"PRIsVALUE, err);
10300 }
10301 return dispatch2(regexp_literal, re, opt);
10302}
10303#endif /* !RIPPER */
10304
10305
10306#ifndef RIPPER
10307static const char rb_parser_lex_state_names[][8] = {
10308 "BEG", "END", "ENDARG", "ENDFN", "ARG",
10309 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10310 "LABEL", "LABELED","FITEM",
10311};
10312
10313static VALUE
10314append_lex_state_name(enum lex_state_e state, VALUE buf)
10315{
10316 int i, sep = 0;
10317 unsigned int mask = 1;
10318 static const char none[] = "NONE";
10319
10320 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10321 if ((unsigned)state & mask) {
10322 if (sep) {
10323 rb_str_cat(buf, "|", 1);
10324 }
10325 sep = 1;
10326 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10327 }
10328 }
10329 if (!sep) {
10330 rb_str_cat(buf, none, sizeof(none)-1);
10331 }
10332 return buf;
10333}
10334
10335static void
10336flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10337{
10338 VALUE mesg = p->debug_buffer;
10339
10340 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10341 p->debug_buffer = Qnil;
10342 rb_io_puts(1, &mesg, out);
10343 }
10344 if (!NIL_P(str) && RSTRING_LEN(str)) {
10345 rb_io_write(p->debug_output, str);
10346 }
10347}
10348
10349enum lex_state_e
10350rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10351 enum lex_state_e to, int line)
10352{
10353 VALUE mesg;
10354 mesg = rb_str_new_cstr("lex_state: ");
10355 append_lex_state_name(from, mesg);
10356 rb_str_cat_cstr(mesg, " -> ");
10357 append_lex_state_name(to, mesg);
10358 rb_str_catf(mesg, " at line %d\n", line);
10359 flush_debug_buffer(p, p->debug_output, mesg);
10360 return to;
10361}
10362
10363VALUE
10364rb_parser_lex_state_name(enum lex_state_e state)
10365{
10366 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10367}
10368
10369static void
10370append_bitstack_value(stack_type stack, VALUE mesg)
10371{
10372 if (stack == 0) {
10373 rb_str_cat_cstr(mesg, "0");
10374 }
10375 else {
10376 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10377 for (; mask && !(stack & mask); mask >>= 1) continue;
10378 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10379 }
10380}
10381
10382void
10383rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10384 const char *name, int line)
10385{
10386 VALUE mesg = rb_sprintf("%s: ", name);
10387 append_bitstack_value(stack, mesg);
10388 rb_str_catf(mesg, " at line %d\n", line);
10389 flush_debug_buffer(p, p->debug_output, mesg);
10390}
10391
10392void
10393rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10394{
10395 va_list ap;
10396 VALUE mesg = rb_str_new_cstr("internal parser error: ");
10397
10398 va_start(ap, fmt);
10399 rb_str_vcatf(mesg, fmt, ap);
10400 va_end(ap);
10401 parser_yyerror(p, NULL, RSTRING_PTR(mesg));
10402 RB_GC_GUARD(mesg);
10403
10404 mesg = rb_str_new(0, 0);
10405 append_lex_state_name(p->lex.state, mesg);
10406 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10407 rb_str_resize(mesg, 0);
10408 append_bitstack_value(p->cond_stack, mesg);
10409 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10410 rb_str_resize(mesg, 0);
10411 append_bitstack_value(p->cmdarg_stack, mesg);
10412 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10413 if (p->debug_output == rb_stdout)
10414 p->debug_output = rb_stderr;
10415 p->debug = TRUE;
10416}
10417
10418YYLTYPE *
10419rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10420{
10421 int sourceline = here->sourceline;
10422 int beg_pos = (int)here->offset - here->quote
10423 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10424 int end_pos = (int)here->offset + here->length + here->quote;
10425
10426 yylloc->beg_pos.lineno = sourceline;
10427 yylloc->beg_pos.column = beg_pos;
10428 yylloc->end_pos.lineno = sourceline;
10429 yylloc->end_pos.column = end_pos;
10430 return yylloc;
10431}
10432
10433YYLTYPE *
10434rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10435{
10436 yylloc->beg_pos.lineno = p->ruby_sourceline;
10437 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10438 yylloc->end_pos.lineno = p->ruby_sourceline;
10439 yylloc->end_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10440 return yylloc;
10441}
10442
10443YYLTYPE *
10444rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10445{
10446 yylloc->beg_pos.lineno = p->ruby_sourceline;
10447 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10448 yylloc->end_pos.lineno = p->ruby_sourceline;
10449 yylloc->end_pos.column = (int)(p->lex.pcur - p->lex.pbeg);
10450 return yylloc;
10451}
10452#endif /* !RIPPER */
10453
10454static int
10455assignable0(struct parser_params *p, ID id, const char **err)
10456{
10457 if (!id) return -1;
10458 switch (id) {
10459 case keyword_self:
10460 *err = "Can't change the value of self";
10461 return -1;
10462 case keyword_nil:
10463 *err = "Can't assign to nil";
10464 return -1;
10465 case keyword_true:
10466 *err = "Can't assign to true";
10467 return -1;
10468 case keyword_false:
10469 *err = "Can't assign to false";
10470 return -1;
10471 case keyword__FILE__:
10472 *err = "Can't assign to __FILE__";
10473 return -1;
10474 case keyword__LINE__:
10475 *err = "Can't assign to __LINE__";
10476 return -1;
10477 case keyword__ENCODING__:
10478 *err = "Can't assign to __ENCODING__";
10479 return -1;
10480 }
10481 switch (id_type(id)) {
10482 case ID_LOCAL:
10483 if (dyna_in_block(p)) {
10484 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10485 compile_error(p, "Can't assign to numbered parameter _%d",
10486 NUMPARAM_ID_TO_IDX(id));
10487 return -1;
10488 }
10489 if (dvar_curr(p, id)) return NODE_DASGN_CURR;
10490 if (dvar_defined(p, id)) return NODE_DASGN;
10491 if (local_id(p, id)) return NODE_LASGN;
10492 dyna_var(p, id);
10493 return NODE_DASGN_CURR;
10494 }
10495 else {
10496 if (!local_id(p, id)) local_var(p, id);
10497 return NODE_LASGN;
10498 }
10499 break;
10500 case ID_GLOBAL: return NODE_GASGN;
10501 case ID_INSTANCE: return NODE_IASGN;
10502 case ID_CONST:
10503 if (!p->in_def) return NODE_CDECL;
10504 *err = "dynamic constant assignment";
10505 return -1;
10506 case ID_CLASS: return NODE_CVASGN;
10507 default:
10508 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10509 }
10510 return -1;
10511}
10512
10513#ifndef RIPPER
10514static NODE*
10515assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10516{
10517 const char *err = 0;
10518 int node_type = assignable0(p, id, &err);
10519 switch (node_type) {
10520 case NODE_DASGN_CURR: return NEW_DASGN_CURR(id, val, loc);
10521 case NODE_DASGN: return NEW_DASGN(id, val, loc);
10522 case NODE_LASGN: return NEW_LASGN(id, val, loc);
10523 case NODE_GASGN: return NEW_GASGN(id, val, loc);
10524 case NODE_IASGN: return NEW_IASGN(id, val, loc);
10525 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10526 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10527 }
10528 if (err) yyerror1(loc, err);
10529 return NEW_BEGIN(0, loc);
10530}
10531#else
10532static VALUE
10533assignable(struct parser_params *p, VALUE lhs)
10534{
10535 const char *err = 0;
10536 assignable0(p, get_id(lhs), &err);
10537 if (err) lhs = assign_error(p, lhs);
10538 return lhs;
10539}
10540#endif
10541
10542static int
10543is_private_local_id(ID name)
10544{
10545 VALUE s;
10546 if (name == idUScore) return 1;
10547 if (!is_local_id(name)) return 0;
10548 s = rb_id2str(name);
10549 if (!s) return 0;
10550 return RSTRING_PTR(s)[0] == '_';
10551}
10552
10553static int
10554shadowing_lvar_0(struct parser_params *p, ID name)
10555{
10556 if (is_private_local_id(name)) return 1;
10557 if (dyna_in_block(p)) {
10558 if (dvar_curr(p, name)) {
10559 yyerror0("duplicated argument name");
10560 }
10561 else if (dvar_defined(p, name) || local_id(p, name)) {
10562 vtable_add(p->lvtbl->vars, name);
10563 if (p->lvtbl->used) {
10564 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
10565 }
10566 return 0;
10567 }
10568 }
10569 else {
10570 if (local_id(p, name)) {
10571 yyerror0("duplicated argument name");
10572 }
10573 }
10574 return 1;
10575}
10576
10577static ID
10578shadowing_lvar(struct parser_params *p, ID name)
10579{
10580 shadowing_lvar_0(p, name);
10581 return name;
10582}
10583
10584static void
10585new_bv(struct parser_params *p, ID name)
10586{
10587 if (!name) return;
10588 if (!is_local_id(name)) {
10589 compile_error(p, "invalid local variable - %"PRIsVALUE,
10590 rb_id2str(name));
10591 return;
10592 }
10593 if (!shadowing_lvar_0(p, name)) return;
10594 dyna_var(p, name);
10595}
10596
10597#ifndef RIPPER
10598static NODE *
10599aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
10600{
10601 return NEW_ATTRASGN(recv, tASET, idx, loc);
10602}
10603
10604static void
10605block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
10606{
10607 if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
10608 compile_error(p, "both block arg and actual block given");
10609 }
10610}
10611
10612static NODE *
10613attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
10614{
10615 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
10616 return NEW_ATTRASGN(recv, id, 0, loc);
10617}
10618
10619static void
10620rb_backref_error(struct parser_params *p, NODE *node)
10621{
10622 switch (nd_type(node)) {
10623 case NODE_NTH_REF:
10624 compile_error(p, "Can't set variable $%ld", node->nd_nth);
10625 break;
10626 case NODE_BACK_REF:
10627 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
10628 break;
10629 }
10630}
10631
10632static NODE *
10633arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10634{
10635 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
10636 switch (nd_type(node1)) {
10637 case NODE_LIST:
10638 return list_append(p, node1, node2);
10639 case NODE_BLOCK_PASS:
10640 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
10641 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
10642 return node1;
10643 case NODE_ARGSPUSH:
10644 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
10645 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10646 nd_set_type(node1, NODE_ARGSCAT);
10647 return node1;
10648 case NODE_ARGSCAT:
10649 if (nd_type(node1->nd_body) != NODE_LIST) break;
10650 node1->nd_body = list_append(p, node1->nd_body, node2);
10651 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10652 return node1;
10653 }
10654 return NEW_ARGSPUSH(node1, node2, loc);
10655}
10656
10657static NODE *
10658arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10659{
10660 if (!node2) return node1;
10661 switch (nd_type(node1)) {
10662 case NODE_BLOCK_PASS:
10663 if (node1->nd_head)
10664 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
10665 else
10666 node1->nd_head = NEW_LIST(node2, loc);
10667 return node1;
10668 case NODE_ARGSPUSH:
10669 if (nd_type(node2) != NODE_LIST) break;
10670 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
10671 nd_set_type(node1, NODE_ARGSCAT);
10672 return node1;
10673 case NODE_ARGSCAT:
10674 if (nd_type(node2) != NODE_LIST ||
10675 nd_type(node1->nd_body) != NODE_LIST) break;
10676 node1->nd_body = list_concat(node1->nd_body, node2);
10677 return node1;
10678 }
10679 return NEW_ARGSCAT(node1, node2, loc);
10680}
10681
10682static NODE *
10683last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
10684{
10685 NODE *n1;
10686 if ((n1 = splat_array(args)) != 0) {
10687 return list_append(p, n1, last_arg);
10688 }
10689 return arg_append(p, args, last_arg, loc);
10690}
10691
10692static NODE *
10693rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
10694{
10695 NODE *n1;
10696 if ((nd_type(rest_arg) == NODE_LIST) && (n1 = splat_array(args)) != 0) {
10697 return list_concat(n1, rest_arg);
10698 }
10699 return arg_concat(p, args, rest_arg, loc);
10700}
10701
10702static NODE *
10703splat_array(NODE* node)
10704{
10705 if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
10706 if (nd_type(node) == NODE_LIST) return node;
10707 return 0;
10708}
10709
10710static void
10711mark_lvar_used(struct parser_params *p, NODE *rhs)
10712{
10713 ID *vidp = NULL;
10714 if (!rhs) return;
10715 switch (nd_type(rhs)) {
10716 case NODE_LASGN:
10717 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
10718 if (vidp) *vidp |= LVAR_USED;
10719 }
10720 break;
10721 case NODE_DASGN:
10722 case NODE_DASGN_CURR:
10723 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
10724 if (vidp) *vidp |= LVAR_USED;
10725 }
10726 break;
10727#if 0
10728 case NODE_MASGN:
10729 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
10730 mark_lvar_used(p, rhs->nd_head);
10731 }
10732 break;
10733#endif
10734 }
10735}
10736
10737static NODE *
10738node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, const YYLTYPE *loc)
10739{
10740 if (!lhs) return 0;
10741
10742 switch (nd_type(lhs)) {
10743 case NODE_GASGN:
10744 case NODE_IASGN:
10745 case NODE_LASGN:
10746 case NODE_DASGN:
10747 case NODE_DASGN_CURR:
10748 case NODE_MASGN:
10749 case NODE_CDECL:
10750 case NODE_CVASGN:
10751 lhs->nd_value = rhs;
10752 nd_set_loc(lhs, loc);
10753 break;
10754
10755 case NODE_ATTRASGN:
10756 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
10757 nd_set_loc(lhs, loc);
10758 break;
10759
10760 default:
10761 /* should not happen */
10762 break;
10763 }
10764
10765 return lhs;
10766}
10767
10768static NODE *
10769value_expr_check(struct parser_params *p, NODE *node)
10770{
10771 NODE *void_node = 0, *vn;
10772
10773 if (!node) {
10774 rb_warning0("empty expression");
10775 }
10776 while (node) {
10777 switch (nd_type(node)) {
10778 case NODE_RETURN:
10779 case NODE_BREAK:
10780 case NODE_NEXT:
10781 case NODE_REDO:
10782 case NODE_RETRY:
10783 return void_node ? void_node : node;
10784
10785 case NODE_CASE3:
10786 if (!node->nd_body || nd_type(node->nd_body) != NODE_IN) {
10787 compile_error(p, "unexpected node");
10788 return NULL;
10789 }
10790 if (node->nd_body->nd_body) {
10791 return NULL;
10792 }
10793 /* single line pattern matching */
10794 return void_node ? void_node : node;
10795
10796 case NODE_BLOCK:
10797 while (node->nd_next) {
10798 node = node->nd_next;
10799 }
10800 node = node->nd_head;
10801 break;
10802
10803 case NODE_BEGIN:
10804 node = node->nd_body;
10805 break;
10806
10807 case NODE_IF:
10808 case NODE_UNLESS:
10809 if (!node->nd_body) {
10810 return NULL;
10811 }
10812 else if (!node->nd_else) {
10813 return NULL;
10814 }
10815 vn = value_expr_check(p, node->nd_body);
10816 if (!vn) return NULL;
10817 if (!void_node) void_node = vn;
10818 node = node->nd_else;
10819 break;
10820
10821 case NODE_AND:
10822 case NODE_OR:
10823 node = node->nd_1st;
10824 break;
10825
10826 case NODE_LASGN:
10827 case NODE_DASGN:
10828 case NODE_DASGN_CURR:
10829 case NODE_MASGN:
10830 mark_lvar_used(p, node);
10831 return NULL;
10832
10833 default:
10834 return NULL;
10835 }
10836 }
10837
10838 return NULL;
10839}
10840
10841static int
10842value_expr_gen(struct parser_params *p, NODE *node)
10843{
10844 NODE *void_node = value_expr_check(p, node);
10845 if (void_node) {
10846 yyerror1(&void_node->nd_loc, "void value expression");
10847 /* or "control never reach"? */
10848 return FALSE;
10849 }
10850 return TRUE;
10851}
10852static void
10853void_expr(struct parser_params *p, NODE *node)
10854{
10855 const char *useless = 0;
10856
10857 if (!RTEST(ruby_verbose)) return;
10858
10859 if (!node || !(node = nd_once_body(node))) return;
10860 switch (nd_type(node)) {
10861 case NODE_OPCALL:
10862 switch (node->nd_mid) {
10863 case '+':
10864 case '-':
10865 case '*':
10866 case '/':
10867 case '%':
10868 case tPOW:
10869 case tUPLUS:
10870 case tUMINUS:
10871 case '|':
10872 case '^':
10873 case '&':
10874 case tCMP:
10875 case '>':
10876 case tGEQ:
10877 case '<':
10878 case tLEQ:
10879 case tEQ:
10880 case tNEQ:
10881 useless = rb_id2name(node->nd_mid);
10882 break;
10883 }
10884 break;
10885
10886 case NODE_LVAR:
10887 case NODE_DVAR:
10888 case NODE_GVAR:
10889 case NODE_IVAR:
10890 case NODE_CVAR:
10891 case NODE_NTH_REF:
10892 case NODE_BACK_REF:
10893 useless = "a variable";
10894 break;
10895 case NODE_CONST:
10896 useless = "a constant";
10897 break;
10898 case NODE_LIT:
10899 case NODE_STR:
10900 case NODE_DSTR:
10901 case NODE_DREGX:
10902 useless = "a literal";
10903 break;
10904 case NODE_COLON2:
10905 case NODE_COLON3:
10906 useless = "::";
10907 break;
10908 case NODE_DOT2:
10909 useless = "..";
10910 break;
10911 case NODE_DOT3:
10912 useless = "...";
10913 break;
10914 case NODE_SELF:
10915 useless = "self";
10916 break;
10917 case NODE_NIL:
10918 useless = "nil";
10919 break;
10920 case NODE_TRUE:
10921 useless = "true";
10922 break;
10923 case NODE_FALSE:
10924 useless = "false";
10925 break;
10926 case NODE_DEFINED:
10927 useless = "defined?";
10928 break;
10929 }
10930
10931 if (useless) {
10932 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
10933 }
10934}
10935
10936static NODE *
10937void_stmts(struct parser_params *p, NODE *node)
10938{
10939 NODE *const n = node;
10940 if (!RTEST(ruby_verbose)) return n;
10941 if (!node) return n;
10942 if (nd_type(node) != NODE_BLOCK) return n;
10943
10944 while (node->nd_next) {
10945 void_expr(p, node->nd_head);
10946 node = node->nd_next;
10947 }
10948 return n;
10949}
10950
10951static NODE *
10952remove_begin(NODE *node)
10953{
10954 NODE **n = &node, *n1 = node;
10955 while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
10956 *n = n1 = n1->nd_body;
10957 }
10958 return node;
10959}
10960
10961static NODE *
10962remove_begin_all(NODE *node)
10963{
10964 NODE **n = &node, *n1 = node;
10965 while (n1 && nd_type(n1) == NODE_BEGIN) {
10966 *n = n1 = n1->nd_body;
10967 }
10968 return node;
10969}
10970
10971static void
10972reduce_nodes(struct parser_params *p, NODE **body)
10973{
10974 NODE *node = *body;
10975
10976 if (!node) {
10977 *body = NEW_NIL(&NULL_LOC);
10978 return;
10979 }
10980#define subnodes(n1, n2) \
10981 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
10982 (!node->n2) ? (body = &node->n1, 1) : \
10983 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
10984
10985 while (node) {
10986 int newline = (int)(node->flags & NODE_FL_NEWLINE);
10987 switch (nd_type(node)) {
10988 end:
10989 case NODE_NIL:
10990 *body = 0;
10991 return;
10992 case NODE_RETURN:
10993 *body = node = node->nd_stts;
10994 if (newline && node) node->flags |= NODE_FL_NEWLINE;
10995 continue;
10996 case NODE_BEGIN:
10997 *body = node = node->nd_body;
10998 if (newline && node) node->flags |= NODE_FL_NEWLINE;
10999 continue;
11000 case NODE_BLOCK:
11001 body = &node->nd_end->nd_head;
11002 break;
11003 case NODE_IF:
11004 case NODE_UNLESS:
11005 if (subnodes(nd_body, nd_else)) break;
11006 return;
11007 case NODE_CASE:
11008 body = &node->nd_body;
11009 break;
11010 case NODE_WHEN:
11011 if (!subnodes(nd_body, nd_next)) goto end;
11012 break;
11013 case NODE_ENSURE:
11014 if (!subnodes(nd_head, nd_resq)) goto end;
11015 break;
11016 case NODE_RESCUE:
11017 if (node->nd_else) {
11018 body = &node->nd_resq;
11019 break;
11020 }
11021 if (!subnodes(nd_head, nd_resq)) goto end;
11022 break;
11023 default:
11024 return;
11025 }
11026 node = *body;
11027 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11028 }
11029
11030#undef subnodes
11031}
11032
11033static int
11034is_static_content(NODE *node)
11035{
11036 if (!node) return 1;
11037 switch (nd_type(node)) {
11038 case NODE_HASH:
11039 if (!(node = node->nd_head)) break;
11040 case NODE_LIST:
11041 do {
11042 if (!is_static_content(node->nd_head)) return 0;
11043 } while ((node = node->nd_next) != 0);
11044 case NODE_LIT:
11045 case NODE_STR:
11046 case NODE_NIL:
11047 case NODE_TRUE:
11048 case NODE_FALSE:
11049 case NODE_ZLIST:
11050 break;
11051 default:
11052 return 0;
11053 }
11054 return 1;
11055}
11056
11057static int
11058assign_in_cond(struct parser_params *p, NODE *node)
11059{
11060 switch (nd_type(node)) {
11061 case NODE_MASGN:
11062 case NODE_LASGN:
11063 case NODE_DASGN:
11064 case NODE_DASGN_CURR:
11065 case NODE_GASGN:
11066 case NODE_IASGN:
11067 break;
11068
11069 default:
11070 return 0;
11071 }
11072
11073 if (!node->nd_value) return 1;
11074 if (is_static_content(node->nd_value)) {
11075 /* reports always */
11076 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11077 }
11078 return 1;
11079}
11080
11081enum cond_type {
11082 COND_IN_OP,
11083 COND_IN_COND,
11084 COND_IN_FF
11085};
11086
11087#define SWITCH_BY_COND_TYPE(t, w, arg) \
11088 switch (t) { \
11089 case COND_IN_OP: break; \
11090 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11091 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11092 }
11093
11094static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11095
11096static NODE*
11097range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11098{
11099 enum node_type type;
11100
11101 if (node == 0) return 0;
11102
11103 type = nd_type(node);
11104 value_expr(node);
11105 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11106 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11107 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(rb_intern("$."), loc), loc), loc);
11108 }
11109 return cond0(p, node, COND_IN_FF, loc);
11110}
11111
11112static NODE*
11113cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11114{
11115 if (node == 0) return 0;
11116 if (!(node = nd_once_body(node))) return 0;
11117 assign_in_cond(p, node);
11118
11119 switch (nd_type(node)) {
11120 case NODE_DSTR:
11121 case NODE_EVSTR:
11122 case NODE_STR:
11123 SWITCH_BY_COND_TYPE(type, warn, "string ")
11124 break;
11125
11126 case NODE_DREGX:
11127 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11128
11129 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11130
11131 case NODE_AND:
11132 case NODE_OR:
11133 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11134 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11135 break;
11136
11137 case NODE_DOT2:
11138 case NODE_DOT3:
11139 node->nd_beg = range_op(p, node->nd_beg, loc);
11140 node->nd_end = range_op(p, node->nd_end, loc);
11141 if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
11142 else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
11143 break;
11144
11145 case NODE_DSYM:
11146 SWITCH_BY_COND_TYPE(type, warning, "string ")
11147 break;
11148
11149 case NODE_LIT:
11150 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11151 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11152 nd_set_type(node, NODE_MATCH);
11153 }
11154 else if (node->nd_lit == Qtrue ||
11155 node->nd_lit == Qfalse) {
11156 /* booleans are OK, e.g., while true */
11157 }
11158 else {
11159 SWITCH_BY_COND_TYPE(type, warning, "")
11160 }
11161 default:
11162 break;
11163 }
11164 return node;
11165}
11166
11167static NODE*
11168cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11169{
11170 if (node == 0) return 0;
11171 return cond0(p, node, COND_IN_COND, loc);
11172}
11173
11174static NODE*
11175method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11176{
11177 if (node == 0) return 0;
11178 return cond0(p, node, COND_IN_OP, loc);
11179}
11180
11181static NODE*
11182new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11183{
11184 if (!cc) return right;
11185 cc = cond0(p, cc, COND_IN_COND, loc);
11186 return newline_node(NEW_IF(cc, left, right, loc));
11187}
11188
11189static NODE*
11190new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11191{
11192 if (!cc) return right;
11193 cc = cond0(p, cc, COND_IN_COND, loc);
11194 return newline_node(NEW_UNLESS(cc, left, right, loc));
11195}
11196
11197static NODE*
11198logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11199 const YYLTYPE *op_loc, const YYLTYPE *loc)
11200{
11201 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11202 NODE *op;
11203 value_expr(left);
11204 if (left && (enum node_type)nd_type(left) == type) {
11205 NODE *node = left, *second;
11206 while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
11207 node = second;
11208 }
11209 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11210 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11211 left->nd_loc.end_pos = loc->end_pos;
11212 return left;
11213 }
11214 op = NEW_NODE(type, left, right, 0, loc);
11215 nd_set_line(op, op_loc->beg_pos.lineno);
11216 return op;
11217}
11218
11219static void
11220no_blockarg(struct parser_params *p, NODE *node)
11221{
11222 if (node && nd_type(node) == NODE_BLOCK_PASS) {
11223 compile_error(p, "block argument should not be given");
11224 }
11225}
11226
11227static NODE *
11228ret_args(struct parser_params *p, NODE *node)
11229{
11230 if (node) {
11231 no_blockarg(p, node);
11232 if (nd_type(node) == NODE_LIST) {
11233 if (node->nd_next == 0) {
11234 node = node->nd_head;
11235 }
11236 else {
11237 nd_set_type(node, NODE_VALUES);
11238 }
11239 }
11240 }
11241 return node;
11242}
11243
11244static NODE *
11245new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11246{
11247 if (node) no_blockarg(p, node);
11248
11249 return NEW_YIELD(node, loc);
11250}
11251
11252static VALUE
11253negate_lit(struct parser_params *p, VALUE lit)
11254{
11255 if (FIXNUM_P(lit)) {
11256 return LONG2FIX(-FIX2LONG(lit));
11257 }
11258 if (SPECIAL_CONST_P(lit)) {
11259#if USE_FLONUM
11260 if (FLONUM_P(lit)) {
11261 return DBL2NUM(-RFLOAT_VALUE(lit));
11262 }
11263#endif
11264 goto unknown;
11265 }
11266 switch (BUILTIN_TYPE(lit)) {
11267 case T_BIGNUM:
11268 BIGNUM_NEGATE(lit);
11269 lit = rb_big_norm(lit);
11270 break;
11271 case T_RATIONAL:
11272 RRATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11273 break;
11274 case T_COMPLEX:
11275 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11276 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11277 break;
11278 case T_FLOAT:
11279 RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
11280 break;
11281 unknown:
11282 default:
11283 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11284 rb_builtin_class_name(lit));
11285 break;
11286 }
11287 return lit;
11288}
11289
11290static NODE *
11291arg_blk_pass(NODE *node1, NODE *node2)
11292{
11293 if (node2) {
11294 if (!node1) return node2;
11295 node2->nd_head = node1;
11296 nd_set_first_lineno(node2, nd_first_lineno(node1));
11297 nd_set_first_column(node2, nd_first_column(node1));
11298 return node2;
11299 }
11300 return node1;
11301}
11302
11303static bool
11304args_info_empty_p(struct rb_args_info *args)
11305{
11306 if (args->pre_args_num) return false;
11307 if (args->post_args_num) return false;
11308 if (args->rest_arg) return false;
11309 if (args->opt_args) return false;
11310 if (args->block_arg) return false;
11311 if (args->kw_args) return false;
11312 if (args->kw_rest_arg) return false;
11313 return true;
11314}
11315
11316static NODE*
11317new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
11318{
11319 int saved_line = p->ruby_sourceline;
11320 struct rb_args_info *args = tail->nd_ainfo;
11321
11322 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
11323 args->pre_init = pre_args ? pre_args->nd_next : 0;
11324
11325 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
11326 args->post_init = post_args ? post_args->nd_next : 0;
11327 args->first_post_arg = post_args ? post_args->nd_pid : 0;
11328
11329 args->rest_arg = rest_arg;
11330
11331 args->opt_args = opt_args;
11332
11333 args->ruby2_keywords = rest_arg == idFWD_REST;
11334
11335 p->ruby_sourceline = saved_line;
11336 nd_set_loc(tail, loc);
11337
11338 return tail;
11339}
11340
11341static NODE*
11342new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *loc)
11343{
11344 int saved_line = p->ruby_sourceline;
11345 NODE *node;
11346 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11347 struct rb_args_info *args = ZALLOC(struct rb_args_info);
11348 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
11349 args->imemo = tmpbuf;
11350 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
11351 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11352 if (p->error_p) return node;
11353
11354 args->block_arg = block;
11355 args->kw_args = kw_args;
11356
11357 if (kw_args) {
11358 /*
11359 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
11360 * variable order: k1, kr1, k2, &b, internal_id, krest
11361 * #=> <reorder>
11362 * variable order: kr1, k1, k2, internal_id, krest, &b
11363 */
11364 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
11365 struct vtable *vtargs = p->lvtbl->args;
11366 NODE *kwn = kw_args;
11367
11368 vtable_pop(vtargs, !!block + !!kw_rest_arg);
11369 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
11370 while (kwn) {
11371 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
11372 --kw_vars;
11373 --required_kw_vars;
11374 kwn = kwn->nd_next;
11375 }
11376
11377 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
11378 ID vid = kwn->nd_body->nd_vid;
11379 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
11380 *required_kw_vars++ = vid;
11381 }
11382 else {
11383 *kw_vars++ = vid;
11384 }
11385 }
11386
11387 arg_var(p, kw_bits);
11388 if (kw_rest_arg) arg_var(p, kw_rest_arg);
11389 if (block) arg_var(p, block);
11390
11391 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11392 args->kw_rest_arg->nd_cflag = kw_bits;
11393 }
11394 else if (kw_rest_arg == idNil) {
11395 args->no_kwarg = 1;
11396 }
11397 else if (kw_rest_arg) {
11398 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11399 }
11400
11401 p->ruby_sourceline = saved_line;
11402 return node;
11403}
11404
11405static NODE *
11406args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
11407{
11408 if (max_numparam > NO_PARAM) {
11409 if (!args) {
11410 YYLTYPE loc = RUBY_INIT_YYLLOC();
11411 args = new_args_tail(p, 0, 0, 0, 0);
11412 nd_set_loc(args, &loc);
11413 }
11414 args->nd_ainfo->pre_args_num = max_numparam;
11415 }
11416 return args;
11417}
11418
11419static NODE*
11420new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
11421{
11422 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
11423
11424 aryptn->nd_pconst = constant;
11425
11426 if (pre_arg) {
11427 NODE *pre_args = NEW_LIST(pre_arg, loc);
11428 if (apinfo->pre_args) {
11429 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
11430 }
11431 else {
11432 apinfo->pre_args = pre_args;
11433 }
11434 }
11435 return aryptn;
11436}
11437
11438static NODE*
11439new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
11440{
11441 int saved_line = p->ruby_sourceline;
11442 NODE *node;
11443 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11444 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
11445 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
11446 node = NEW_NODE(NODE_ARYPTN, 0, 0, apinfo, loc);
11447 apinfo->imemo = tmpbuf;
11448 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11449
11450 apinfo->pre_args = pre_args;
11451
11452 if (has_rest) {
11453 if (rest_arg) {
11454 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
11455 }
11456 else {
11457 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
11458 }
11459 }
11460 else {
11461 apinfo->rest_arg = NULL;
11462 }
11463
11464 apinfo->post_args = post_args;
11465
11466 p->ruby_sourceline = saved_line;
11467 return node;
11468}
11469
11470static NODE*
11471new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
11472{
11473 hshptn->nd_pconst = constant;
11474 return hshptn;
11475}
11476
11477static NODE*
11478new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
11479{
11480 int saved_line = p->ruby_sourceline;
11481 NODE *node, *kw_rest_arg_node;
11482
11483 if (kw_rest_arg == idNil) {
11484 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
11485 }
11486 else if (kw_rest_arg) {
11487 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
11488 }
11489 else {
11490 kw_rest_arg_node = NULL;
11491 }
11492
11493 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
11494
11495 p->ruby_sourceline = saved_line;
11496 return node;
11497}
11498
11499static NODE *
11500new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc)
11501{
11502 NODE *node = NEW_CASE3(val, pat, loc);
11503
11504 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL))
11505 rb_warn0L(nd_line(node), "Pattern matching is experimental, and the behavior may change in future versions of Ruby!");
11506 return node;
11507}
11508
11509static NODE*
11510dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11511{
11512 VALUE lit;
11513
11514 if (!node) {
11515 return NEW_LIT(ID2SYM(idNULL), loc);
11516 }
11517
11518 switch (nd_type(node)) {
11519 case NODE_DSTR:
11520 nd_set_type(node, NODE_DSYM);
11521 nd_set_loc(node, loc);
11522 break;
11523 case NODE_STR:
11524 lit = node->nd_lit;
11525 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
11526 nd_set_type(node, NODE_LIT);
11527 nd_set_loc(node, loc);
11528 break;
11529 default:
11530 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
11531 break;
11532 }
11533 return node;
11534}
11535
11536static int
11537append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
11538{
11539 NODE *node = (NODE *)v;
11540 NODE **result = (NODE **)h;
11541 node->nd_alen = 2;
11542 node->nd_next->nd_end = node->nd_next;
11543 node->nd_next->nd_next = 0;
11544 if (*result)
11545 list_concat(*result, node);
11546 else
11547 *result = node;
11548 return ST_CONTINUE;
11549}
11550
11551static NODE *
11552remove_duplicate_keys(struct parser_params *p, NODE *hash)
11553{
11554 st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
11555 NODE *result = 0;
11556 rb_code_location_t loc = hash->nd_loc;
11557 while (hash && hash->nd_head && hash->nd_next) {
11558 NODE *head = hash->nd_head;
11559 NODE *value = hash->nd_next;
11560 NODE *next = value->nd_next;
11561 VALUE key = (VALUE)head;
11562 st_data_t data;
11563 if (nd_type(head) == NODE_LIT &&
11564 st_lookup(literal_keys, (key = head->nd_lit), &data)) {
11565 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
11566 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
11567 head->nd_lit, nd_line(head));
11568 head = ((NODE *)data)->nd_next;
11569 head->nd_head = block_append(p, head->nd_head, value->nd_head);
11570 }
11571 else {
11572 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
11573 }
11574 hash = next;
11575 }
11576 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
11577 st_free_table(literal_keys);
11578 if (hash) {
11579 if (!result) result = hash;
11580 else list_concat(result, hash);
11581 }
11582 result->nd_loc = loc;
11583 return result;
11584}
11585
11586static NODE *
11587new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11588{
11589 if (hash) hash = remove_duplicate_keys(p, hash);
11590 return NEW_HASH(hash, loc);
11591}
11592#endif
11593
11594static void
11595error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
11596{
11597 if (is_private_local_id(id)) {
11598 return;
11599 }
11600 if (st_is_member(p->pvtbl, id)) {
11601 yyerror1(loc, "duplicated variable name");
11602 }
11603 else {
11604 st_insert(p->pvtbl, (st_data_t)id, 0);
11605 }
11606}
11607
11608static void
11609error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
11610{
11611 if (!p->pktbl) {
11612 p->pktbl = st_init_numtable();
11613 }
11614 else if (st_is_member(p->pktbl, key)) {
11615 yyerror1(loc, "duplicated key name");
11616 return;
11617 }
11618 st_insert(p->pktbl, (st_data_t)key, 0);
11619}
11620
11621#ifndef RIPPER
11622static NODE *
11623new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11624{
11625 return NEW_HASH(hash, loc);
11626}
11627#endif /* !RIPPER */
11628
11629#ifndef RIPPER
11630static NODE *
11631new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11632{
11633 NODE *asgn;
11634
11635 if (lhs) {
11636 ID vid = lhs->nd_vid;
11637 YYLTYPE lhs_loc = lhs->nd_loc;
11638 if (op == tOROP) {
11639 lhs->nd_value = rhs;
11640 nd_set_loc(lhs, loc);
11641 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
11642 if (is_notop_id(vid)) {
11643 switch (id_type(vid)) {
11644 case ID_GLOBAL:
11645 case ID_INSTANCE:
11646 case ID_CLASS:
11647 asgn->nd_aid = vid;
11648 }
11649 }
11650 }
11651 else if (op == tANDOP) {
11652 lhs->nd_value = rhs;
11653 nd_set_loc(lhs, loc);
11654 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
11655 }
11656 else {
11657 asgn = lhs;
11658 asgn->nd_value = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
11659 nd_set_loc(asgn, loc);
11660 }
11661 }
11662 else {
11663 asgn = NEW_BEGIN(0, loc);
11664 }
11665 return asgn;
11666}
11667
11668static NODE *
11669new_ary_op_assign(struct parser_params *p, NODE *ary,
11670 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
11671{
11672 NODE *asgn;
11673
11674 args = make_list(args, args_loc);
11675 if (nd_type(args) == NODE_BLOCK_PASS) {
11676 args = NEW_ARGSCAT(args, rhs, loc);
11677 }
11678 else {
11679 args = arg_concat(p, args, rhs, loc);
11680 }
11681 asgn = NEW_OP_ASGN1(ary, op, args, loc);
11682 fixpos(asgn, ary);
11683 return asgn;
11684}
11685
11686static NODE *
11687new_attr_op_assign(struct parser_params *p, NODE *lhs,
11688 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
11689{
11690 NODE *asgn;
11691
11692 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
11693 fixpos(asgn, lhs);
11694 return asgn;
11695}
11696
11697static NODE *
11698new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11699{
11700 NODE *asgn;
11701
11702 if (lhs) {
11703 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
11704 }
11705 else {
11706 asgn = NEW_BEGIN(0, loc);
11707 }
11708 fixpos(asgn, lhs);
11709 return asgn;
11710}
11711
11712static NODE *
11713const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
11714{
11715 if (p->in_def) {
11716 yyerror1(loc, "dynamic constant assignment");
11717 }
11718 return NEW_CDECL(0, 0, (path), loc);
11719}
11720#else
11721static VALUE
11722const_decl(struct parser_params *p, VALUE path)
11723{
11724 if (p->in_def) {
11725 path = dispatch1(assign_error, path);
11726 ripper_error(p);
11727 }
11728 return path;
11729}
11730
11731static VALUE
11732assign_error(struct parser_params *p, VALUE a)
11733{
11734 a = dispatch1(assign_error, a);
11735 ripper_error(p);
11736 return a;
11737}
11738
11739static VALUE
11740var_field(struct parser_params *p, VALUE a)
11741{
11742 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
11743}
11744#endif
11745
11746#ifndef RIPPER
11747static NODE *
11748new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
11749{
11750 NODE *result = head;
11751 if (rescue) {
11752 NODE *tmp = rescue_else ? rescue_else : rescue;
11753 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
11754
11755 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
11756 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
11757 }
11758 else if (rescue_else) {
11759 result = block_append(p, result, rescue_else);
11760 }
11761 if (ensure) {
11762 result = NEW_ENSURE(result, ensure, loc);
11763 }
11764 fixpos(result, head);
11765 return result;
11766}
11767#endif
11768
11769static void
11770warn_unused_var(struct parser_params *p, struct local_vars *local)
11771{
11772 int cnt;
11773
11774 if (!local->used) return;
11775 cnt = local->used->pos;
11776 if (cnt != local->vars->pos) {
11777 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
11778 }
11779#ifndef RIPPER
11780 ID *v = local->vars->tbl;
11781 ID *u = local->used->tbl;
11782 for (int i = 0; i < cnt; ++i) {
11783 if (!v[i] || (u[i] & LVAR_USED)) continue;
11784 if (is_private_local_id(v[i])) continue;
11785 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
11786 }
11787#endif
11788}
11789
11790static void
11791local_push(struct parser_params *p, int toplevel_scope)
11792{
11793 struct local_vars *local;
11794 int inherits_dvars = toplevel_scope && compile_for_eval;
11795 int warn_unused_vars = RTEST(ruby_verbose);
11796
11797 local = ALLOC(struct local_vars);
11798 local->prev = p->lvtbl;
11799 local->args = vtable_alloc(0);
11800 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
11801#ifndef RIPPER
11802 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
11803 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
11804 local->numparam.outer = 0;
11805 local->numparam.inner = 0;
11806 local->numparam.current = 0;
11807#endif
11808 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
11809
11810# if WARN_PAST_SCOPE
11811 local->past = 0;
11812# endif
11813 CMDARG_PUSH(0);
11814 COND_PUSH(0);
11815 p->lvtbl = local;
11816}
11817
11818static void
11819local_pop(struct parser_params *p)
11820{
11821 struct local_vars *local = p->lvtbl->prev;
11822 if (p->lvtbl->used) {
11823 warn_unused_var(p, p->lvtbl);
11824 vtable_free(p->lvtbl->used);
11825 }
11826# if WARN_PAST_SCOPE
11827 while (p->lvtbl->past) {
11828 struct vtable *past = p->lvtbl->past;
11829 p->lvtbl->past = past->prev;
11830 vtable_free(past);
11831 }
11832# endif
11833 vtable_free(p->lvtbl->args);
11834 vtable_free(p->lvtbl->vars);
11835 CMDARG_POP();
11836 COND_POP();
11837 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
11838 p->lvtbl = local;
11839}
11840
11841#ifndef RIPPER
11842static ID*
11843local_tbl(struct parser_params *p)
11844{
11845 int cnt_args = vtable_size(p->lvtbl->args);
11846 int cnt_vars = vtable_size(p->lvtbl->vars);
11847 int cnt = cnt_args + cnt_vars;
11848 int i, j;
11849 ID *buf;
11850
11851 if (cnt <= 0) return 0;
11852 buf = ALLOC_N(ID, cnt + 2);
11853 MEMCPY(buf+1, p->lvtbl->args->tbl, ID, cnt_args);
11854 /* remove IDs duplicated to warn shadowing */
11855 for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) {
11856 ID id = p->lvtbl->vars->tbl[i];
11857 if (!vtable_included(p->lvtbl->args, id)) {
11858 buf[j++] = id;
11859 }
11860 }
11861 if (--j < cnt) {
11862 REALLOC_N(buf, ID, (cnt = j) + 2);
11863 }
11864 buf[0] = cnt;
11865 rb_ast_add_local_table(p->ast, buf);
11866
11867 return buf;
11868}
11869
11870static NODE*
11871node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
11872{
11873 ID *a0;
11874 NODE *n;
11875
11876 a0 = local_tbl(p);
11877 n = NEW_NODE(type, a0, a1, a2, loc);
11878 return n;
11879}
11880
11881#endif
11882
11883static void
11884numparam_name(struct parser_params *p, ID id)
11885{
11886 if (!NUMPARAM_ID_P(id)) return;
11887 rb_warn1("`_%d' is reserved for numbered parameter; consider another name",
11888 WARN_I(NUMPARAM_ID_TO_IDX(id)));
11889}
11890
11891static void
11892arg_var(struct parser_params *p, ID id)
11893{
11894 numparam_name(p, id);
11895 vtable_add(p->lvtbl->args, id);
11896}
11897
11898static void
11899local_var(struct parser_params *p, ID id)
11900{
11901 numparam_name(p, id);
11902 vtable_add(p->lvtbl->vars, id);
11903 if (p->lvtbl->used) {
11904 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
11905 }
11906}
11907
11908static int
11909local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
11910{
11911 struct vtable *vars, *args, *used;
11912
11913 vars = p->lvtbl->vars;
11914 args = p->lvtbl->args;
11915 used = p->lvtbl->used;
11916
11917 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
11918 vars = vars->prev;
11919 args = args->prev;
11920 if (used) used = used->prev;
11921 }
11922
11923 if (vars && vars->prev == DVARS_INHERIT) {
11924 return rb_local_defined(id, p->parent_iseq);
11925 }
11926 else if (vtable_included(args, id)) {
11927 return 1;
11928 }
11929 else {
11930 int i = vtable_included(vars, id);
11931 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
11932 return i != 0;
11933 }
11934}
11935
11936static int
11937local_id(struct parser_params *p, ID id)
11938{
11939 return local_id_ref(p, id, NULL);
11940}
11941
11942static NODE *
11943numparam_push(struct parser_params *p)
11944{
11945#ifndef RIPPER
11946 struct local_vars *local = p->lvtbl;
11947 NODE *inner = local->numparam.inner;
11948 if (!local->numparam.outer) {
11949 local->numparam.outer = local->numparam.current;
11950 }
11951 local->numparam.inner = 0;
11952 local->numparam.current = 0;
11953 return inner;
11954#else
11955 return 0;
11956#endif
11957}
11958
11959static void
11960numparam_pop(struct parser_params *p, NODE *prev_inner)
11961{
11962#ifndef RIPPER
11963 struct local_vars *local = p->lvtbl;
11964 if (prev_inner) {
11965 /* prefer first one */
11966 local->numparam.inner = prev_inner;
11967 }
11968 else if (local->numparam.current) {
11969 /* current and inner are exclusive */
11970 local->numparam.inner = local->numparam.current;
11971 }
11972 if (p->max_numparam > NO_PARAM) {
11973 /* current and outer are exclusive */
11974 local->numparam.current = local->numparam.outer;
11975 local->numparam.outer = 0;
11976 }
11977 else {
11978 /* no numbered parameter */
11979 local->numparam.current = 0;
11980 }
11981#endif
11982}
11983
11984static const struct vtable *
11985dyna_push(struct parser_params *p)
11986{
11987 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
11988 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
11989 if (p->lvtbl->used) {
11990 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
11991 }
11992 return p->lvtbl->args;
11993}
11994
11995static void
11996dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
11997{
11998 struct vtable *tmp = *vtblp;
11999 *vtblp = tmp->prev;
12000# if WARN_PAST_SCOPE
12001 if (p->past_scope_enabled) {
12002 tmp->prev = p->lvtbl->past;
12003 p->lvtbl->past = tmp;
12004 return;
12005 }
12006# endif
12007 vtable_free(tmp);
12008}
12009
12010static void
12011dyna_pop_1(struct parser_params *p)
12012{
12013 struct vtable *tmp;
12014
12015 if ((tmp = p->lvtbl->used) != 0) {
12016 warn_unused_var(p, p->lvtbl);
12017 p->lvtbl->used = p->lvtbl->used->prev;
12018 vtable_free(tmp);
12019 }
12020 dyna_pop_vtable(p, &p->lvtbl->args);
12021 dyna_pop_vtable(p, &p->lvtbl->vars);
12022}
12023
12024static void
12025dyna_pop(struct parser_params *p, const struct vtable *lvargs)
12026{
12027 while (p->lvtbl->args != lvargs) {
12028 dyna_pop_1(p);
12029 if (!p->lvtbl->args) {
12030 struct local_vars *local = p->lvtbl->prev;
12031 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12032 p->lvtbl = local;
12033 }
12034 }
12035 dyna_pop_1(p);
12036}
12037
12038static int
12039dyna_in_block(struct parser_params *p)
12040{
12041 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12042}
12043
12044static int
12045dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12046{
12047 struct vtable *vars, *args, *used;
12048 int i;
12049
12050 args = p->lvtbl->args;
12051 vars = p->lvtbl->vars;
12052 used = p->lvtbl->used;
12053
12054 while (!DVARS_TERMINAL_P(vars)) {
12055 if (vtable_included(args, id)) {
12056 return 1;
12057 }
12058 if ((i = vtable_included(vars, id)) != 0) {
12059 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12060 return 1;
12061 }
12062 args = args->prev;
12063 vars = vars->prev;
12064 if (!vidrefp) used = 0;
12065 if (used) used = used->prev;
12066 }
12067
12068 if (vars == DVARS_INHERIT) {
12069 return rb_dvar_defined(id, p->parent_iseq);
12070 }
12071
12072 return 0;
12073}
12074
12075static int
12076dvar_defined(struct parser_params *p, ID id)
12077{
12078 return dvar_defined_ref(p, id, NULL);
12079}
12080
12081static int
12082dvar_curr(struct parser_params *p, ID id)
12083{
12084 return (vtable_included(p->lvtbl->args, id) ||
12085 vtable_included(p->lvtbl->vars, id));
12086}
12087
12088static void
12089reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12090{
12091 compile_error(p,
12092 "regexp encoding option '%c' differs from source encoding '%s'",
12093 c, rb_enc_name(rb_enc_get(str)));
12094}
12095
12096#ifndef RIPPER
12097int
12098rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12099{
12100 int c = RE_OPTION_ENCODING_IDX(options);
12101
12102 if (c) {
12103 int opt, idx;
12104 rb_char_to_option_kcode(c, &opt, &idx);
12105 if (idx != ENCODING_GET(str) &&
12106 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12107 goto error;
12108 }
12109 ENCODING_SET(str, idx);
12110 }
12111 else if (RE_OPTION_ENCODING_NONE(options)) {
12112 if (!ENCODING_IS_ASCII8BIT(str) &&
12113 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12114 c = 'n';
12115 goto error;
12116 }
12117 rb_enc_associate(str, rb_ascii8bit_encoding());
12118 }
12119 else if (p->enc == rb_usascii_encoding()) {
12120 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12121 /* raise in re.c */
12122 rb_enc_associate(str, rb_usascii_encoding());
12123 }
12124 else {
12125 rb_enc_associate(str, rb_ascii8bit_encoding());
12126 }
12127 }
12128 return 0;
12129
12130 error:
12131 return c;
12132}
12133
12134static void
12135reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12136{
12137 int c = rb_reg_fragment_setenc(p, str, options);
12138 if (c) reg_fragment_enc_error(p, str, c);
12139}
12140
12141static int
12142reg_fragment_check(struct parser_params* p, VALUE str, int options)
12143{
12144 VALUE err;
12145 reg_fragment_setenc(p, str, options);
12146 err = rb_reg_check_preprocess(str);
12147 if (err != Qnil) {
12148 err = rb_obj_as_string(err);
12149 compile_error(p, "%"PRIsVALUE, err);
12150 return 0;
12151 }
12152 return 1;
12153}
12154
12155typedef struct {
12156 struct parser_params* parser;
12157 rb_encoding *enc;
12158 NODE *succ_block;
12159 const YYLTYPE *loc;
12160} reg_named_capture_assign_t;
12161
12162static int
12163reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12164 int back_num, int *back_refs, OnigRegex regex, void *arg0)
12165{
12166 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12167 struct parser_params* p = arg->parser;
12168 rb_encoding *enc = arg->enc;
12169 long len = name_end - name;
12170 const char *s = (const char *)name;
12171 ID var;
12172 NODE *node, *succ;
12173
12174 if (!len) return ST_CONTINUE;
12175 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
12176 return ST_CONTINUE;
12177
12178 var = intern_cstr(s, len, enc);
12179 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
12180 if (!lvar_defined(p, var)) return ST_CONTINUE;
12181 }
12182 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), arg->loc);
12183 succ = arg->succ_block;
12184 if (!succ) succ = NEW_BEGIN(0, arg->loc);
12185 succ = block_append(p, succ, node);
12186 arg->succ_block = succ;
12187 return ST_CONTINUE;
12188}
12189
12190static NODE *
12191reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
12192{
12193 reg_named_capture_assign_t arg;
12194
12195 arg.parser = p;
12196 arg.enc = rb_enc_get(regexp);
12197 arg.succ_block = 0;
12198 arg.loc = loc;
12199 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
12200
12201 if (!arg.succ_block) return 0;
12202 return arg.succ_block->nd_next;
12203}
12204
12205static VALUE
12206parser_reg_compile(struct parser_params* p, VALUE str, int options)
12207{
12208 reg_fragment_setenc(p, str, options);
12209 return rb_parser_reg_compile(p, str, options);
12210}
12211
12212VALUE
12213rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
12214{
12215 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
12216}
12217
12218static VALUE
12219reg_compile(struct parser_params* p, VALUE str, int options)
12220{
12221 VALUE re;
12222 VALUE err;
12223
12224 err = rb_errinfo();
12225 re = parser_reg_compile(p, str, options);
12226 if (NIL_P(re)) {
12227 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
12228 rb_set_errinfo(err);
12229 compile_error(p, "%"PRIsVALUE, m);
12230 return Qnil;
12231 }
12232 return re;
12233}
12234#else
12235static VALUE
12236parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
12237{
12238 VALUE err = rb_errinfo();
12239 VALUE re;
12240 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
12241 int c = rb_reg_fragment_setenc(p, str, options);
12242 if (c) reg_fragment_enc_error(p, str, c);
12243 re = rb_parser_reg_compile(p, str, options);
12244 if (NIL_P(re)) {
12245 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
12246 rb_set_errinfo(err);
12247 }
12248 return re;
12249}
12250#endif
12251
12252#ifndef RIPPER
12253void
12254rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
12255{
12256 struct parser_params *p;
12257 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12258 p->do_print = print;
12259 p->do_loop = loop;
12260 p->do_chomp = chomp;
12261 p->do_split = split;
12262}
12263
12264void
12265rb_parser_warn_location(VALUE vparser, int warn)
12266{
12267 struct parser_params *p;
12268 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12269 p->warn_location = warn;
12270}
12271
12272static NODE *
12273parser_append_options(struct parser_params *p, NODE *node)
12274{
12275 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
12276 const YYLTYPE *const LOC = &default_location;
12277
12278 if (p->do_print) {
12279 NODE *print = NEW_FCALL(rb_intern("print"),
12280 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
12281 LOC);
12282 node = block_append(p, node, print);
12283 }
12284
12285 if (p->do_loop) {
12286 if (p->do_split) {
12287 NODE *args = NEW_LIST(NEW_GVAR(rb_intern("$;"), LOC), LOC);
12288 NODE *split = NEW_GASGN(rb_intern("$F"),
12289 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12290 rb_intern("split"), args, LOC),
12291 LOC);
12292 node = block_append(p, split, node);
12293 }
12294 if (p->do_chomp) {
12295 NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12296 rb_intern("chomp!"), 0, LOC);
12297 node = block_append(p, chomp, node);
12298 }
12299
12300 node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
12301 }
12302
12303 return node;
12304}
12305
12306void
12307rb_init_parse(void)
12308{
12309 /* just to suppress unused-function warnings */
12310 (void)nodetype;
12311 (void)nodeline;
12312}
12313
12314static ID
12315internal_id(struct parser_params *p)
12316{
12317 const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
12318 ID id = (ID)vtable_size(p->lvtbl->args) + (ID)vtable_size(p->lvtbl->vars);
12319 id = max_id - id;
12320 return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
12321}
12322#endif /* !RIPPER */
12323
12324static void
12325parser_initialize(struct parser_params *p)
12326{
12327 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
12328 p->command_start = TRUE;
12329 p->ruby_sourcefile_string = Qnil;
12330 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
12331 p->node_id = 0;
12332#ifdef RIPPER
12333 p->delayed.token = Qnil;
12334 p->result = Qnil;
12335 p->parsing_thread = Qnil;
12336#else
12337 p->error_buffer = Qfalse;
12338#endif
12339 p->debug_buffer = Qnil;
12340 p->debug_output = rb_stdout;
12341 p->enc = rb_utf8_encoding();
12342}
12343
12344#ifdef RIPPER
12345#define parser_mark ripper_parser_mark
12346#define parser_free ripper_parser_free
12347#endif
12348
12349static void
12350parser_mark(void *ptr)
12351{
12352 struct parser_params *p = (struct parser_params*)ptr;
12353
12354 rb_gc_mark(p->lex.input);
12355 rb_gc_mark(p->lex.prevline);
12356 rb_gc_mark(p->lex.lastline);
12357 rb_gc_mark(p->lex.nextline);
12358 rb_gc_mark(p->ruby_sourcefile_string);
12359 rb_gc_mark((VALUE)p->lex.strterm);
12360 rb_gc_mark((VALUE)p->ast);
12361 rb_gc_mark(p->case_labels);
12362#ifndef RIPPER
12363 rb_gc_mark(p->debug_lines);
12364 rb_gc_mark(p->compile_option);
12365 rb_gc_mark(p->error_buffer);
12366#else
12367 rb_gc_mark(p->delayed.token);
12368 rb_gc_mark(p->value);
12369 rb_gc_mark(p->result);
12370 rb_gc_mark(p->parsing_thread);
12371#endif
12372 rb_gc_mark(p->debug_buffer);
12373 rb_gc_mark(p->debug_output);
12374#ifdef YYMALLOC
12375 rb_gc_mark((VALUE)p->heap);
12376#endif
12377}
12378
12379static void
12380parser_free(void *ptr)
12381{
12382 struct parser_params *p = (struct parser_params*)ptr;
12383 struct local_vars *local, *prev;
12384
12385 if (p->tokenbuf) {
12386 ruby_sized_xfree(p->tokenbuf, p->toksiz);
12387 }
12388 for (local = p->lvtbl; local; local = prev) {
12389 if (local->vars) xfree(local->vars);
12390 prev = local->prev;
12391 xfree(local);
12392 }
12393 {
12394 token_info *ptinfo;
12395 while ((ptinfo = p->token_info) != 0) {
12396 p->token_info = ptinfo->next;
12397 xfree(ptinfo);
12398 }
12399 }
12400 xfree(ptr);
12401}
12402
12403static size_t
12404parser_memsize(const void *ptr)
12405{
12406 struct parser_params *p = (struct parser_params*)ptr;
12407 struct local_vars *local;
12408 size_t size = sizeof(*p);
12409
12410 size += p->toksiz;
12411 for (local = p->lvtbl; local; local = local->prev) {
12412 size += sizeof(*local);
12413 if (local->vars) size += local->vars->capa * sizeof(ID);
12414 }
12415 return size;
12416}
12417
12418static const rb_data_type_t parser_data_type = {
12419#ifndef RIPPER
12420 "parser",
12421#else
12422 "ripper",
12423#endif
12424 {
12425 parser_mark,
12426 parser_free,
12427 parser_memsize,
12428 },
12429 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
12430};
12431
12432#ifndef RIPPER
12433#undef rb_reserved_word
12434
12435const struct kwtable *
12436rb_reserved_word(const char *str, unsigned int len)
12437{
12438 return reserved_word(str, len);
12439}
12440
12441VALUE
12442rb_parser_new(void)
12443{
12444 struct parser_params *p;
12445 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
12446 &parser_data_type, p);
12447 parser_initialize(p);
12448 return parser;
12449}
12450
12451VALUE
12452rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
12453{
12454 struct parser_params *p;
12455
12456 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12457 p->error_buffer = main ? Qfalse : Qnil;
12458 p->parent_iseq = base;
12459 return vparser;
12460}
12461#endif
12462
12463#ifdef RIPPER
12464#define rb_parser_end_seen_p ripper_parser_end_seen_p
12465#define rb_parser_encoding ripper_parser_encoding
12466#define rb_parser_get_yydebug ripper_parser_get_yydebug
12467#define rb_parser_set_yydebug ripper_parser_set_yydebug
12468#define rb_parser_get_debug_output ripper_parser_get_debug_output
12469#define rb_parser_set_debug_output ripper_parser_set_debug_output
12470static VALUE ripper_parser_end_seen_p(VALUE vparser);
12471static VALUE ripper_parser_encoding(VALUE vparser);
12472static VALUE ripper_parser_get_yydebug(VALUE self);
12473static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
12474static VALUE ripper_parser_get_debug_output(VALUE self);
12475static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
12476
12477/*
12478 * call-seq:
12479 * ripper.error? -> Boolean
12480 *
12481 * Return true if parsed source has errors.
12482 */
12483static VALUE
12484ripper_error_p(VALUE vparser)
12485{
12486 struct parser_params *p;
12487
12488 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12489 return p->error_p ? Qtrue : Qfalse;
12490}
12491#endif
12492
12493/*
12494 * call-seq:
12495 * ripper.end_seen? -> Boolean
12496 *
12497 * Return true if parsed source ended by +\_\_END\_\_+.
12498 */
12499VALUE
12500rb_parser_end_seen_p(VALUE vparser)
12501{
12502 struct parser_params *p;
12503
12504 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12505 return p->ruby__end__seen ? Qtrue : Qfalse;
12506}
12507
12508/*
12509 * call-seq:
12510 * ripper.encoding -> encoding
12511 *
12512 * Return encoding of the source.
12513 */
12514VALUE
12515rb_parser_encoding(VALUE vparser)
12516{
12517 struct parser_params *p;
12518
12519 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12520 return rb_enc_from_encoding(p->enc);
12521}
12522
12523#ifdef RIPPER
12524/*
12525 * call-seq:
12526 * ripper.yydebug -> true or false
12527 *
12528 * Get yydebug.
12529 */
12530VALUE
12531rb_parser_get_yydebug(VALUE self)
12532{
12533 struct parser_params *p;
12534
12535 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12536 return p->debug ? Qtrue : Qfalse;
12537}
12538#endif
12539
12540/*
12541 * call-seq:
12542 * ripper.yydebug = flag
12543 *
12544 * Set yydebug.
12545 */
12546VALUE
12547rb_parser_set_yydebug(VALUE self, VALUE flag)
12548{
12549 struct parser_params *p;
12550
12551 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12552 p->debug = RTEST(flag);
12553 return flag;
12554}
12555
12556/*
12557 * call-seq:
12558 * ripper.debug_output -> obj
12559 *
12560 * Get debug output.
12561 */
12562VALUE
12563rb_parser_get_debug_output(VALUE self)
12564{
12565 struct parser_params *p;
12566
12567 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12568 return p->debug_output;
12569}
12570
12571/*
12572 * call-seq:
12573 * ripper.debug_output = obj
12574 *
12575 * Set debug output.
12576 */
12577VALUE
12578rb_parser_set_debug_output(VALUE self, VALUE output)
12579{
12580 struct parser_params *p;
12581
12582 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12583 return p->debug_output = output;
12584}
12585
12586#ifndef RIPPER
12587#ifdef YYMALLOC
12588#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
12589/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
12590 * potential memory leak */
12591#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
12592#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
12593 (new)->cnt = (cnt), (ptr))
12594
12595void *
12596rb_parser_malloc(struct parser_params *p, size_t size)
12597{
12598 size_t cnt = HEAPCNT(1, size);
12599 rb_imemo_tmpbuf_t *n = NEWHEAP();
12600 void *ptr = xmalloc(size);
12601
12602 return ADD2HEAP(n, cnt, ptr);
12603}
12604
12605void *
12606rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
12607{
12608 size_t cnt = HEAPCNT(nelem, size);
12609 rb_imemo_tmpbuf_t *n = NEWHEAP();
12610 void *ptr = xcalloc(nelem, size);
12611
12612 return ADD2HEAP(n, cnt, ptr);
12613}
12614
12615void *
12616rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
12617{
12618 rb_imemo_tmpbuf_t *n;
12619 size_t cnt = HEAPCNT(1, size);
12620
12621 if (ptr && (n = p->heap) != NULL) {
12622 do {
12623 if (n->ptr == ptr) {
12624 n->ptr = ptr = xrealloc(ptr, size);
12625 if (n->cnt) n->cnt = cnt;
12626 return ptr;
12627 }
12628 } while ((n = n->next) != NULL);
12629 }
12630 n = NEWHEAP();
12631 ptr = xrealloc(ptr, size);
12632 return ADD2HEAP(n, cnt, ptr);
12633}
12634
12635void
12636rb_parser_free(struct parser_params *p, void *ptr)
12637{
12638 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
12639
12640 while ((n = *prev) != NULL) {
12641 if (n->ptr == ptr) {
12642 *prev = n->next;
12643 rb_gc_force_recycle((VALUE)n);
12644 break;
12645 }
12646 prev = &n->next;
12647 }
12648 xfree(ptr);
12649}
12650#endif
12651
12652void
12653rb_parser_printf(struct parser_params *p, const char *fmt, ...)
12654{
12655 va_list ap;
12656 VALUE mesg = p->debug_buffer;
12657
12658 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
12659 va_start(ap, fmt);
12660 rb_str_vcatf(mesg, fmt, ap);
12661 va_end(ap);
12662 if (RSTRING_END(mesg)[-1] == '\n') {
12663 rb_io_write(p->debug_output, mesg);
12664 p->debug_buffer = Qnil;
12665 }
12666}
12667
12668static void
12669parser_compile_error(struct parser_params *p, const char *fmt, ...)
12670{
12671 va_list ap;
12672
12673 rb_io_flush(p->debug_output);
12674 p->error_p = 1;
12675 va_start(ap, fmt);
12676 p->error_buffer =
12677 rb_syntax_error_append(p->error_buffer,
12678 p->ruby_sourcefile_string,
12679 p->ruby_sourceline,
12680 rb_long2int(p->lex.pcur - p->lex.pbeg),
12681 p->enc, fmt, ap);
12682 va_end(ap);
12683}
12684
12685static size_t
12686count_char(const char *str, int c)
12687{
12688 int n = 0;
12689 while (str[n] == c) ++n;
12690 return n;
12691}
12692
12693/*
12694 * strip enclosing double-quotes, same as the default yytnamerr except
12695 * for that single-quotes matching back-quotes do not stop stripping.
12696 *
12697 * "\"`class' keyword\"" => "`class' keyword"
12698 */
12699RUBY_FUNC_EXPORTED size_t
12700rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
12701{
12702 if (*yystr == '"') {
12703 size_t yyn = 0, bquote = 0;
12704 const char *yyp = yystr;
12705
12706 while (*++yyp) {
12707 switch (*yyp) {
12708 case '`':
12709 if (!bquote) {
12710 bquote = count_char(yyp+1, '`') + 1;
12711 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
12712 yyn += bquote;
12713 yyp += bquote - 1;
12714 break;
12715 }
12716 goto default_char;
12717
12718 case '\'':
12719 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
12720 if (yyres) memcpy(yyres + yyn, yyp, bquote);
12721 yyn += bquote;
12722 yyp += bquote - 1;
12723 bquote = 0;
12724 break;
12725 }
12726 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
12727 if (yyres) memcpy(yyres + yyn, yyp, 3);
12728 yyn += 3;
12729 yyp += 2;
12730 break;
12731 }
12732 goto do_not_strip_quotes;
12733
12734 case ',':
12735 goto do_not_strip_quotes;
12736
12737 case '\\':
12738 if (*++yyp != '\\')
12739 goto do_not_strip_quotes;
12740 /* Fall through. */
12741 default_char:
12742 default:
12743 if (yyres)
12744 yyres[yyn] = *yyp;
12745 yyn++;
12746 break;
12747
12748 case '"':
12749 case '\0':
12750 if (yyres)
12751 yyres[yyn] = '\0';
12752 return yyn;
12753 }
12754 }
12755 do_not_strip_quotes: ;
12756 }
12757
12758 if (!yyres) return strlen(yystr);
12759
12760 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
12761}
12762#endif
12763
12764#ifdef RIPPER
12765#ifdef RIPPER_DEBUG
12766/* :nodoc: */
12767static VALUE
12768ripper_validate_object(VALUE self, VALUE x)
12769{
12770 if (x == Qfalse) return x;
12771 if (x == Qtrue) return x;
12772 if (x == Qnil) return x;
12773 if (x == Qundef)
12774 rb_raise(rb_eArgError, "Qundef given");
12775 if (FIXNUM_P(x)) return x;
12776 if (SYMBOL_P(x)) return x;
12777 switch (BUILTIN_TYPE(x)) {
12778 case T_STRING:
12779 case T_OBJECT:
12780 case T_ARRAY:
12781 case T_BIGNUM:
12782 case T_FLOAT:
12783 case T_COMPLEX:
12784 case T_RATIONAL:
12785 break;
12786 case T_NODE:
12787 if (nd_type((NODE *)x) != NODE_RIPPER) {
12788 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
12789 }
12790 x = ((NODE *)x)->nd_rval;
12791 break;
12792 default:
12793 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
12794 (void *)x, rb_obj_classname(x));
12795 }
12796 if (!RBASIC_CLASS(x)) {
12797 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
12798 (void *)x, rb_builtin_type_name(TYPE(x)));
12799 }
12800 return x;
12801}
12802#endif
12803
12804#define validate(x) ((x) = get_value(x))
12805
12806static VALUE
12807ripper_dispatch0(struct parser_params *p, ID mid)
12808{
12809 return rb_funcall(p->value, mid, 0);
12810}
12811
12812static VALUE
12813ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
12814{
12815 validate(a);
12816 return rb_funcall(p->value, mid, 1, a);
12817}
12818
12819static VALUE
12820ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
12821{
12822 validate(a);
12823 validate(b);
12824 return rb_funcall(p->value, mid, 2, a, b);
12825}
12826
12827static VALUE
12828ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
12829{
12830 validate(a);
12831 validate(b);
12832 validate(c);
12833 return rb_funcall(p->value, mid, 3, a, b, c);
12834}
12835
12836static VALUE
12837ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
12838{
12839 validate(a);
12840 validate(b);
12841 validate(c);
12842 validate(d);
12843 return rb_funcall(p->value, mid, 4, a, b, c, d);
12844}
12845
12846static VALUE
12847ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
12848{
12849 validate(a);
12850 validate(b);
12851 validate(c);
12852 validate(d);
12853 validate(e);
12854 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
12855}
12856
12857static VALUE
12858ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
12859{
12860 validate(a);
12861 validate(b);
12862 validate(c);
12863 validate(d);
12864 validate(e);
12865 validate(f);
12866 validate(g);
12867 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
12868}
12869
12870static ID
12871ripper_get_id(VALUE v)
12872{
12873 NODE *nd;
12874 if (!RB_TYPE_P(v, T_NODE)) return 0;
12875 nd = (NODE *)v;
12876 if (nd_type(nd) != NODE_RIPPER) return 0;
12877 return nd->nd_vid;
12878}
12879
12880static VALUE
12881ripper_get_value(VALUE v)
12882{
12883 NODE *nd;
12884 if (v == Qundef) return Qnil;
12885 if (!RB_TYPE_P(v, T_NODE)) return v;
12886 nd = (NODE *)v;
12887 if (nd_type(nd) != NODE_RIPPER) return Qnil;
12888 return nd->nd_rval;
12889}
12890
12891static void
12892ripper_error(struct parser_params *p)
12893{
12894 p->error_p = TRUE;
12895}
12896
12897static void
12898ripper_compile_error(struct parser_params *p, const char *fmt, ...)
12899{
12900 VALUE str;
12901 va_list args;
12902
12903 va_start(args, fmt);
12904 str = rb_vsprintf(fmt, args);
12905 va_end(args);
12906 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
12907 ripper_error(p);
12908}
12909
12910static VALUE
12911ripper_lex_get_generic(struct parser_params *p, VALUE src)
12912{
12913 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
12914 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
12915 rb_raise(rb_eTypeError,
12916 "gets returned %"PRIsVALUE" (expected String or nil)",
12917 rb_obj_class(line));
12918 }
12919 return line;
12920}
12921
12922static VALUE
12923ripper_lex_io_get(struct parser_params *p, VALUE src)
12924{
12925 return rb_io_gets(src);
12926}
12927
12928static VALUE
12929ripper_s_allocate(VALUE klass)
12930{
12931 struct parser_params *p;
12932 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
12933 &parser_data_type, p);
12934 p->value = self;
12935 return self;
12936}
12937
12938#define ripper_initialized_p(r) ((r)->lex.input != 0)
12939
12940/*
12941 * call-seq:
12942 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
12943 *
12944 * Create a new Ripper object.
12945 * _src_ must be a String, an IO, or an Object which has #gets method.
12946 *
12947 * This method does not starts parsing.
12948 * See also Ripper#parse and Ripper.parse.
12949 */
12950static VALUE
12951ripper_initialize(int argc, VALUE *argv, VALUE self)
12952{
12953 struct parser_params *p;
12954 VALUE src, fname, lineno;
12955
12956 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12957 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
12958 if (RB_TYPE_P(src, T_FILE)) {
12959 p->lex.gets = ripper_lex_io_get;
12960 }
12961 else if (rb_respond_to(src, id_gets)) {
12962 p->lex.gets = ripper_lex_get_generic;
12963 }
12964 else {
12965 StringValue(src);
12966 p->lex.gets = lex_get_str;
12967 }
12968 p->lex.input = src;
12969 p->eofp = 0;
12970 if (NIL_P(fname)) {
12971 fname = STR_NEW2("(ripper)");
12972 OBJ_FREEZE(fname);
12973 }
12974 else {
12975 StringValueCStr(fname);
12976 fname = rb_str_new_frozen(fname);
12977 }
12978 parser_initialize(p);
12979
12980 p->ruby_sourcefile_string = fname;
12981 p->ruby_sourcefile = RSTRING_PTR(fname);
12982 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
12983
12984 return Qnil;
12985}
12986
12987static VALUE
12988ripper_parse0(VALUE parser_v)
12989{
12990 struct parser_params *p;
12991
12992 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
12993 parser_prepare(p);
12994 p->ast = rb_ast_new();
12995 ripper_yyparse((void*)p);
12996 rb_ast_dispose(p->ast);
12997 p->ast = 0;
12998 return p->result;
12999}
13000
13001static VALUE
13002ripper_ensure(VALUE parser_v)
13003{
13004 struct parser_params *p;
13005
13006 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13007 p->parsing_thread = Qnil;
13008 return Qnil;
13009}
13010
13011/*
13012 * call-seq:
13013 * ripper.parse
13014 *
13015 * Start parsing and returns the value of the root action.
13016 */
13017static VALUE
13018ripper_parse(VALUE self)
13019{
13020 struct parser_params *p;
13021
13022 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13023 if (!ripper_initialized_p(p)) {
13024 rb_raise(rb_eArgError, "method called for uninitialized object");
13025 }
13026 if (!NIL_P(p->parsing_thread)) {
13027 if (p->parsing_thread == rb_thread_current())
13028 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13029 else
13030 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13031 }
13032 p->parsing_thread = rb_thread_current();
13033 rb_ensure(ripper_parse0, self, ripper_ensure, self);
13034
13035 return p->result;
13036}
13037
13038/*
13039 * call-seq:
13040 * ripper.column -> Integer
13041 *
13042 * Return column number of current parsing line.
13043 * This number starts from 0.
13044 */
13045static VALUE
13046ripper_column(VALUE self)
13047{
13048 struct parser_params *p;
13049 long col;
13050
13051 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13052 if (!ripper_initialized_p(p)) {
13053 rb_raise(rb_eArgError, "method called for uninitialized object");
13054 }
13055 if (NIL_P(p->parsing_thread)) return Qnil;
13056 col = p->lex.ptok - p->lex.pbeg;
13057 return LONG2NUM(col);
13058}
13059
13060/*
13061 * call-seq:
13062 * ripper.filename -> String
13063 *
13064 * Return current parsing filename.
13065 */
13066static VALUE
13067ripper_filename(VALUE self)
13068{
13069 struct parser_params *p;
13070
13071 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13072 if (!ripper_initialized_p(p)) {
13073 rb_raise(rb_eArgError, "method called for uninitialized object");
13074 }
13075 return p->ruby_sourcefile_string;
13076}
13077
13078/*
13079 * call-seq:
13080 * ripper.lineno -> Integer
13081 *
13082 * Return line number of current parsing line.
13083 * This number starts from 1.
13084 */
13085static VALUE
13086ripper_lineno(VALUE self)
13087{
13088 struct parser_params *p;
13089
13090 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13091 if (!ripper_initialized_p(p)) {
13092 rb_raise(rb_eArgError, "method called for uninitialized object");
13093 }
13094 if (NIL_P(p->parsing_thread)) return Qnil;
13095 return INT2NUM(p->ruby_sourceline);
13096}
13097
13098/*
13099 * call-seq:
13100 * ripper.state -> Integer
13101 *
13102 * Return scanner state of current token.
13103 */
13104static VALUE
13105ripper_state(VALUE self)
13106{
13107 struct parser_params *p;
13108
13109 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13110 if (!ripper_initialized_p(p)) {
13111 rb_raise(rb_eArgError, "method called for uninitialized object");
13112 }
13113 if (NIL_P(p->parsing_thread)) return Qnil;
13114 return INT2NUM(p->lex.state);
13115}
13116
13117/*
13118 * call-seq:
13119 * ripper.token -> String
13120 *
13121 * Return the current token string.
13122 */
13123static VALUE
13124ripper_token(VALUE self)
13125{
13126 struct parser_params *p;
13127 long pos, len;
13128
13129 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13130 if (!ripper_initialized_p(p)) {
13131 rb_raise(rb_eArgError, "method called for uninitialized object");
13132 }
13133 if (NIL_P(p->parsing_thread)) return Qnil;
13134 pos = p->lex.ptok - p->lex.pbeg;
13135 len = p->lex.pcur - p->lex.ptok;
13136 return rb_str_subseq(p->lex.lastline, pos, len);
13137}
13138
13139#ifdef RIPPER_DEBUG
13140/* :nodoc: */
13141static VALUE
13142ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13143{
13144 StringValue(msg);
13145 if (obj == Qundef) {
13146 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13147 }
13148 return Qnil;
13149}
13150
13151/* :nodoc: */
13152static VALUE
13153ripper_value(VALUE self, VALUE obj)
13154{
13155 return ULONG2NUM(obj);
13156}
13157#endif
13158
13159/*
13160 * call-seq:
13161 * Ripper.lex_state_name(integer) -> string
13162 *
13163 * Returns a string representation of lex_state.
13164 */
13165static VALUE
13166ripper_lex_state_name(VALUE self, VALUE state)
13167{
13168 return rb_parser_lex_state_name(NUM2INT(state));
13169}
13170
13171void
13172Init_ripper(void)
13173{
13174 ripper_init_eventids1();
13175 ripper_init_eventids2();
13176 id_warn = rb_intern_const("warn");
13177 id_warning = rb_intern_const("warning");
13178 id_gets = rb_intern_const("gets");
13179 id_assoc = rb_intern_const("=>");
13180
13181 (void)yystpcpy; /* may not used in newer bison */
13182
13183 InitVM(ripper);
13184}
13185
13186void
13187InitVM_ripper(void)
13188{
13189 VALUE Ripper;
13190
13191 Ripper = rb_define_class("Ripper", rb_cObject);
13192 /* version of Ripper */
13193 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
13194 rb_define_alloc_func(Ripper, ripper_s_allocate);
13195 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
13196 rb_define_method(Ripper, "parse", ripper_parse, 0);
13197 rb_define_method(Ripper, "column", ripper_column, 0);
13198 rb_define_method(Ripper, "filename", ripper_filename, 0);
13199 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
13200 rb_define_method(Ripper, "state", ripper_state, 0);
13201 rb_define_method(Ripper, "token", ripper_token, 0);
13202 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
13203 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
13204 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
13205 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
13206 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
13207 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
13208 rb_define_method(Ripper, "error?", ripper_error_p, 0);
13209#ifdef RIPPER_DEBUG
13210 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
13211 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
13212 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
13213#endif
13214
13215 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
13216 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
13217
13218 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
13219
13220 /* ignore newline, +/- is a sign. */
13221 rb_define_const(Ripper, "EXPR_BEG", INT2NUM(EXPR_BEG));
13222 /* newline significant, +/- is an operator. */
13223 rb_define_const(Ripper, "EXPR_END", INT2NUM(EXPR_END));
13224 /* ditto, and unbound braces. */
13225 rb_define_const(Ripper, "EXPR_ENDARG", INT2NUM(EXPR_ENDARG));
13226 /* ditto, and unbound braces. */
13227 rb_define_const(Ripper, "EXPR_ENDFN", INT2NUM(EXPR_ENDFN));
13228 /* newline significant, +/- is an operator. */
13229 rb_define_const(Ripper, "EXPR_ARG", INT2NUM(EXPR_ARG));
13230 /* newline significant, +/- is an operator. */
13231 rb_define_const(Ripper, "EXPR_CMDARG", INT2NUM(EXPR_CMDARG));
13232 /* newline significant, +/- is an operator. */
13233 rb_define_const(Ripper, "EXPR_MID", INT2NUM(EXPR_MID));
13234 /* ignore newline, no reserved words. */
13235 rb_define_const(Ripper, "EXPR_FNAME", INT2NUM(EXPR_FNAME));
13236 /* right after `.' or `::', no reserved words. */
13237 rb_define_const(Ripper, "EXPR_DOT", INT2NUM(EXPR_DOT));
13238 /* immediate after `class', no here document. */
13239 rb_define_const(Ripper, "EXPR_CLASS", INT2NUM(EXPR_CLASS));
13240 /* flag bit, label is allowed. */
13241 rb_define_const(Ripper, "EXPR_LABEL", INT2NUM(EXPR_LABEL));
13242 /* flag bit, just after a label. */
13243 rb_define_const(Ripper, "EXPR_LABELED", INT2NUM(EXPR_LABELED));
13244 /* symbol literal as FNAME. */
13245 rb_define_const(Ripper, "EXPR_FITEM", INT2NUM(EXPR_FITEM));
13246 /* equals to +EXPR_BEG+ */
13247 rb_define_const(Ripper, "EXPR_VALUE", INT2NUM(EXPR_VALUE));
13248 /* equals to <tt>(EXPR_BEG | EXPR_MID | EXPR_CLASS)</tt> */
13249 rb_define_const(Ripper, "EXPR_BEG_ANY", INT2NUM(EXPR_BEG_ANY));
13250 /* equals to <tt>(EXPR_ARG | EXPR_CMDARG)</tt> */
13251 rb_define_const(Ripper, "EXPR_ARG_ANY", INT2NUM(EXPR_ARG_ANY));
13252 /* equals to <tt>(EXPR_END | EXPR_ENDARG | EXPR_ENDFN)</tt> */
13253 rb_define_const(Ripper, "EXPR_END_ANY", INT2NUM(EXPR_END_ANY));
13254 /* equals to +0+ */
13255 rb_define_const(Ripper, "EXPR_NONE", INT2NUM(EXPR_NONE));
13256
13257 ripper_init_eventids1_table(Ripper);
13258 ripper_init_eventids2_table(Ripper);
13259
13260# if 0
13261 /* Hack to let RDoc document SCRIPT_LINES__ */
13262
13263 /*
13264 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
13265 * after the assignment will be added as an Array of lines with the file
13266 * name as the key.
13267 */
13268 rb_define_global_const("SCRIPT_LINES__", Qnil);
13269#endif
13270
13271}
13272#endif /* RIPPER */
13273
13274/*
13275 * Local variables:
13276 * mode: c
13277 * c-file-style: "ruby"
13278 * End:
13279 */