Ruby 2.7.6p219 (2022-04-12 revision c9c2245c0a25176072e02db9254f0e0c84c805cd)
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 YYPRINT(out, tok, val) parser_token_value_print(p, (tok), &(val))
50#define YY_LOCATION_PRINT(File, loc) \
51 rb_parser_printf(p, "%d.%d-%d.%d", \
52 (loc).beg_pos.lineno, (loc).beg_pos.column,\
53 (loc).end_pos.lineno, (loc).end_pos.column)
54#define YYLLOC_DEFAULT(Current, Rhs, N) \
55 do \
56 if (N) \
57 { \
58 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
59 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
60 } \
61 else \
62 { \
63 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
64 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
65 } \
66 while (0)
67
68#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
69 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
70#define RUBY_SET_YYLLOC_OF_NONE(Current) \
71 rb_parser_set_location_of_none(p, &(Current))
72#define RUBY_SET_YYLLOC(Current) \
73 rb_parser_set_location(p, &(Current))
74#define RUBY_INIT_YYLLOC() \
75 { \
76 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
77 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
78 }
79
80enum lex_state_bits {
81 EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
82 EXPR_END_bit, /* newline significant, +/- is an operator. */
83 EXPR_ENDARG_bit, /* ditto, and unbound braces. */
84 EXPR_ENDFN_bit, /* ditto, and unbound braces. */
85 EXPR_ARG_bit, /* newline significant, +/- is an operator. */
86 EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
87 EXPR_MID_bit, /* newline significant, +/- is an operator. */
88 EXPR_FNAME_bit, /* ignore newline, no reserved words. */
89 EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
90 EXPR_CLASS_bit, /* immediate after `class', no here document. */
91 EXPR_LABEL_bit, /* flag bit, label is allowed. */
92 EXPR_LABELED_bit, /* flag bit, just after a label. */
93 EXPR_FITEM_bit, /* symbol literal as FNAME. */
94 EXPR_MAX_STATE
95};
96/* examine combinations */
97enum lex_state_e {
98#define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
99 DEF_EXPR(BEG),
100 DEF_EXPR(END),
101 DEF_EXPR(ENDARG),
102 DEF_EXPR(ENDFN),
103 DEF_EXPR(ARG),
104 DEF_EXPR(CMDARG),
105 DEF_EXPR(MID),
106 DEF_EXPR(FNAME),
107 DEF_EXPR(DOT),
108 DEF_EXPR(CLASS),
109 DEF_EXPR(LABEL),
110 DEF_EXPR(LABELED),
111 DEF_EXPR(FITEM),
112 EXPR_VALUE = EXPR_BEG,
113 EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
114 EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
115 EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN),
116 EXPR_NONE = 0
117};
118#define IS_lex_state_for(x, ls) ((x) & (ls))
119#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
120#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
121#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
122
123# define SET_LEX_STATE(ls) \
124 (p->lex.state = \
125 (p->debug ? \
126 rb_parser_trace_lex_state(p, p->lex.state, (ls), __LINE__) : \
127 (enum lex_state_e)(ls)))
128
129typedef VALUE stack_type;
130
131static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
132
133# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
134# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
135# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
136# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
137# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
138
139/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
140 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
141#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
142#define COND_POP() BITSTACK_POP(cond_stack)
143#define COND_P() BITSTACK_SET_P(cond_stack)
144#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
145
146/* A flag to identify keyword_do_block; "do" keyword after command_call.
147 Example: `foo 1, 2 do`. */
148#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
149#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
150#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
151#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
152
153struct vtable {
154 ID *tbl;
155 int pos;
156 int capa;
157 struct vtable *prev;
158};
159
160struct local_vars {
161 struct vtable *args;
162 struct vtable *vars;
163 struct vtable *used;
164# if WARN_PAST_SCOPE
165 struct vtable *past;
166# endif
167 struct local_vars *prev;
168# ifndef RIPPER
169 struct {
170 NODE *outer, *inner, *current;
171 } numparam;
172# endif
173};
174
175enum {
176 ORDINAL_PARAM = -1,
177 NO_PARAM = 0,
178 NUMPARAM_MAX = 9,
179};
180
181#define NUMPARAM_ID_P(id) numparam_id_p(id)
182#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - tNUMPARAM_1 + 1)
183#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 + (idx) - 1))
184static int
185numparam_id_p(ID id)
186{
187 if (!is_local_id(id)) return 0;
188 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
189 return idx > 0 && idx <= NUMPARAM_MAX;
190}
191static void numparam_name(struct parser_params *p, ID id);
192
193#define DVARS_INHERIT ((void*)1)
194#define DVARS_TOPSCOPE NULL
195#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
196
197typedef struct token_info {
198 const char *token;
199 rb_code_position_t beg;
200 int indent;
201 int nonspc;
202 struct token_info *next;
203} token_info;
204
205typedef struct rb_strterm_struct rb_strterm_t;
206
207/*
208 Structure of Lexer Buffer:
209
210 lex.pbeg lex.ptok lex.pcur lex.pend
211 | | | |
212 |------------+------------+------------|
213 |<---------->|
214 token
215*/
216struct parser_params {
217 rb_imemo_tmpbuf_t *heap;
218
219 YYSTYPE *lval;
220
221 struct {
222 rb_strterm_t *strterm;
223 VALUE (*gets)(struct parser_params*,VALUE);
224 VALUE input;
225 VALUE prevline;
226 VALUE lastline;
227 VALUE nextline;
228 const char *pbeg;
229 const char *pcur;
230 const char *pend;
231 const char *ptok;
232 union {
233 long ptr;
234 VALUE (*call)(VALUE, int);
235 } gets_;
236 enum lex_state_e state;
237 /* track the nest level of any parens "()[]{}" */
238 int paren_nest;
239 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
240 int lpar_beg;
241 /* track the nest level of only braces "{}" */
242 int brace_nest;
243 } lex;
244 stack_type cond_stack;
245 stack_type cmdarg_stack;
246 int tokidx;
247 int toksiz;
248 int tokline;
249 int heredoc_end;
250 int heredoc_indent;
251 int heredoc_line_indent;
252 char *tokenbuf;
253 struct local_vars *lvtbl;
254 st_table *pvtbl;
255 st_table *pktbl;
256 int line_count;
257 int ruby_sourceline; /* current line no. */
258 const char *ruby_sourcefile; /* current source file */
259 VALUE ruby_sourcefile_string;
260 rb_encoding *enc;
261 token_info *token_info;
262 VALUE case_labels;
263 VALUE compile_option;
264
265 VALUE debug_buffer;
266 VALUE debug_output;
267
268 ID cur_arg;
269
270 rb_ast_t *ast;
271 int node_id;
272
273 int max_numparam;
274
275 unsigned int command_start:1;
276 unsigned int eofp: 1;
277 unsigned int ruby__end__seen: 1;
278 unsigned int debug: 1;
279 unsigned int has_shebang: 1;
280 unsigned int in_defined: 1;
281 unsigned int in_kwarg: 1;
282 unsigned int in_def: 1;
283 unsigned int in_class: 1;
284 unsigned int token_seen: 1;
285 unsigned int token_info_enabled: 1;
286# if WARN_PAST_SCOPE
287 unsigned int past_scope_enabled: 1;
288# endif
289 unsigned int error_p: 1;
290 unsigned int cr_seen: 1;
291
292#ifndef RIPPER
293 /* Ruby core only */
294
295 unsigned int do_print: 1;
296 unsigned int do_loop: 1;
297 unsigned int do_chomp: 1;
298 unsigned int do_split: 1;
299 unsigned int warn_location: 1;
300
301 NODE *eval_tree_begin;
302 NODE *eval_tree;
303 VALUE error_buffer;
304 VALUE debug_lines;
305 const struct rb_iseq_struct *parent_iseq;
306#else
307 /* Ripper only */
308
309 struct {
310 VALUE token;
311 int line;
312 int col;
313 } delayed;
314
315 VALUE value;
316 VALUE result;
317 VALUE parsing_thread;
318#endif
319};
320
321#define intern_cstr(n,l,en) rb_intern3(n,l,en)
322
323#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
324#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
325#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
326#define STR_NEW3(ptr,len,e,func) parser_str_new((ptr),(len),(e),(func),p->enc)
327#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
328
329static st_table *
330push_pvtbl(struct parser_params *p)
331{
332 st_table *tbl = p->pvtbl;
333 p->pvtbl = st_init_numtable();
334 return tbl;
335}
336
337static void
338pop_pvtbl(struct parser_params *p, st_table *tbl)
339{
340 st_free_table(p->pvtbl);
341 p->pvtbl = tbl;
342}
343
344static st_table *
345push_pktbl(struct parser_params *p)
346{
347 st_table *tbl = p->pktbl;
348 p->pktbl = 0;
349 return tbl;
350}
351
352static void
353pop_pktbl(struct parser_params *p, st_table *tbl)
354{
355 if (p->pktbl) st_free_table(p->pktbl);
356 p->pktbl = tbl;
357}
358
359static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
360#define yyerror0(msg) parser_yyerror(p, NULL, (msg))
361#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
362#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
363#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
364
365#ifdef RIPPER
366#define compile_for_eval (0)
367#else
368#define compile_for_eval (p->parent_iseq != 0)
369#endif
370
371#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
372
373#define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
374#define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
375#define NEW_QCALL(q,r,m,a,loc) NEW_NODE(NODE_CALL_Q(q),r,m,a,loc)
376
377#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
378
379static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
380
381#ifndef RIPPER
382static inline void
383rb_discard_node(struct parser_params *p, NODE *n)
384{
385 rb_ast_delete_node(p->ast, n);
386}
387#endif
388
389#ifdef RIPPER
390static inline VALUE
391add_mark_object(struct parser_params *p, VALUE obj)
392{
393 if (!SPECIAL_CONST_P(obj)
394 && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */
395 ) {
396 rb_ast_add_mark_object(p->ast, obj);
397 }
398 return obj;
399}
400#else
401static NODE* node_newnode_with_locals(struct parser_params *, enum node_type, VALUE, VALUE, const rb_code_location_t*);
402#endif
403
404static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE, const rb_code_location_t*);
405#define rb_node_newnode(type, a1, a2, a3, loc) node_newnode(p, (type), (a1), (a2), (a3), (loc))
406
407static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
408
409static int
410parser_get_node_id(struct parser_params *p)
411{
412 int node_id = p->node_id;
413 p->node_id++;
414 return node_id;
415}
416
417#ifndef RIPPER
418static inline void
419set_line_body(NODE *body, int line)
420{
421 if (!body) return;
422 switch (nd_type(body)) {
423 case NODE_RESCUE:
424 case NODE_ENSURE:
425 nd_set_line(body, line);
426 }
427}
428
429#define yyparse ruby_yyparse
430
431static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
432static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
433#define new_nil(loc) NEW_NIL(loc)
434static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
435static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
436static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
437
438static NODE *newline_node(NODE*);
439static void fixpos(NODE*,NODE*);
440
441static int value_expr_gen(struct parser_params*,NODE*);
442static void void_expr(struct parser_params*,NODE*);
443static NODE *remove_begin(NODE*);
444static NODE *remove_begin_all(NODE*);
445#define value_expr(node) value_expr_gen(p, (node) = remove_begin(node))
446static NODE *void_stmts(struct parser_params*,NODE*);
447static void reduce_nodes(struct parser_params*,NODE**);
448static void block_dup_check(struct parser_params*,NODE*,NODE*);
449
450static NODE *block_append(struct parser_params*,NODE*,NODE*);
451static NODE *list_append(struct parser_params*,NODE*,NODE*);
452static NODE *list_concat(NODE*,NODE*);
453static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
454static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
455static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
456static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
457static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
458static NODE *evstr2dstr(struct parser_params*,NODE*);
459static NODE *splat_array(NODE*);
460static void mark_lvar_used(struct parser_params *p, NODE *rhs);
461
462static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
463static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
464static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
465static 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);
466static 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;}
467
468static bool args_info_empty_p(struct rb_args_info *args);
469static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
470static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
471static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
472static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
473static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
474static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
475static NODE *new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc);
476
477static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
478static NODE *args_with_numbered(struct parser_params*,NODE*,int);
479
480static VALUE negate_lit(struct parser_params*, VALUE);
481static NODE *ret_args(struct parser_params*,NODE*);
482static NODE *arg_blk_pass(NODE*,NODE*);
483static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
484static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
485
486static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
487static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
488
489static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
490static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
491
492static void rb_backref_error(struct parser_params*,NODE*);
493static NODE *node_assign(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
494
495static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc);
496static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
497static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
498static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc);
499static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
500
501static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
502
503static NODE *opt_arg_append(NODE*, NODE*);
504static NODE *kwd_append(NODE*, NODE*);
505
506static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
507static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
508
509static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
510
511static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
512
513#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
514
515static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
516
517static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
518
519static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
520
521static ID *local_tbl(struct parser_params*);
522
523static VALUE reg_compile(struct parser_params*, VALUE, int);
524static void reg_fragment_setenc(struct parser_params*, VALUE, int);
525static int reg_fragment_check(struct parser_params*, VALUE, int);
526static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
527
528static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
529static NODE *heredoc_dedent(struct parser_params*,NODE*);
530
531static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
532
533#define get_id(id) (id)
534#define get_value(val) (val)
535#define get_num(num) (num)
536#else /* RIPPER */
537#define NODE_RIPPER NODE_CDECL
538
539static inline int ripper_is_node_yylval(VALUE n);
540
541static inline VALUE
542ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
543{
544 if (ripper_is_node_yylval(c)) c = RNODE(c)->nd_cval;
545 add_mark_object(p, b);
546 add_mark_object(p, c);
547 return (VALUE)NEW_CDECL(a, b, c, &NULL_LOC);
548}
549
550static inline int
551ripper_is_node_yylval(VALUE n)
552{
553 return RB_TYPE_P(n, T_NODE) && nd_type(RNODE(n)) == NODE_RIPPER;
554}
555
556#define value_expr(node) ((void)(node))
557#define remove_begin(node) (node)
558#define void_stmts(p,x) (x)
559#define rb_dvar_defined(id, base) 0
560#define rb_local_defined(id, base) 0
561static ID ripper_get_id(VALUE);
562#define get_id(id) ripper_get_id(id)
563static VALUE ripper_get_value(VALUE);
564#define get_value(val) ripper_get_value(val)
565#define get_num(num) (int)get_id(num)
566static VALUE assignable(struct parser_params*,VALUE);
567static int id_is_var(struct parser_params *p, ID id);
568
569#define method_cond(p,node,loc) (node)
570#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
571#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
572#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
573#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
574
575#define new_nil(loc) Qnil
576
577static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
578
579static VALUE const_decl(struct parser_params *p, VALUE path);
580
581static VALUE var_field(struct parser_params *p, VALUE a);
582static VALUE assign_error(struct parser_params *p, VALUE a);
583
584static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
585
586#endif /* !RIPPER */
587
588/* forward declaration */
589typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
590
591RUBY_SYMBOL_EXPORT_BEGIN
592VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
593int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
594enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
595VALUE rb_parser_lex_state_name(enum lex_state_e state);
596void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
597PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
598YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
599YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
600YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
601RUBY_SYMBOL_EXPORT_END
602
603static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
604static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
605static void parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp);
606static ID formal_argument(struct parser_params*, ID);
607static ID shadowing_lvar(struct parser_params*,ID);
608static void new_bv(struct parser_params*,ID);
609
610static void local_push(struct parser_params*,int);
611static void local_pop(struct parser_params*);
612static void local_var(struct parser_params*, ID);
613static void arg_var(struct parser_params*, ID);
614static int local_id(struct parser_params *p, ID id);
615static int local_id_ref(struct parser_params*, ID, ID **);
616#ifndef RIPPER
617static ID internal_id(struct parser_params*);
618#endif
619
620static const struct vtable *dyna_push(struct parser_params *);
621static void dyna_pop(struct parser_params*, const struct vtable *);
622static int dyna_in_block(struct parser_params*);
623#define dyna_var(p, id) local_var(p, id)
624static int dvar_defined(struct parser_params*, ID);
625static int dvar_defined_ref(struct parser_params*, ID, ID**);
626static int dvar_curr(struct parser_params*,ID);
627
628static int lvar_defined(struct parser_params*, ID);
629
630static NODE *numparam_push(struct parser_params *p);
631static void numparam_pop(struct parser_params *p, NODE *prev_inner);
632
633#ifdef RIPPER
634# define METHOD_NOT idNOT
635#else
636# define METHOD_NOT '!'
637#endif
638
639#define idFWD_REST '*'
640#ifdef RUBY3_KEYWORDS
641#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
642#else
643#define idFWD_KWREST 0
644#endif
645#define idFWD_BLOCK '&'
646
647#define RE_OPTION_ONCE (1<<16)
648#define RE_OPTION_ENCODING_SHIFT 8
649#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
650#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
651#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
652#define RE_OPTION_MASK 0xff
653#define RE_OPTION_ARG_ENCODING_NONE 32
654
655/* structs for managing terminator of string literal and heredocment */
656typedef struct rb_strterm_literal_struct {
657 union {
658 VALUE dummy;
659 long nest;
660 } u0;
661 union {
662 VALUE dummy;
663 long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
664 } u1;
665 union {
666 VALUE dummy;
667 long paren; /* '(' of `%q(...)` */
668 } u2;
669 union {
670 VALUE dummy;
671 long term; /* ')' of `%q(...)` */
672 } u3;
673} rb_strterm_literal_t;
674
675#define HERETERM_LENGTH_BITS ((SIZEOF_VALUE - 1) * CHAR_BIT - 1)
676
677struct rb_strterm_heredoc_struct {
678 VALUE lastline; /* the string of line that contains `<<"END"` */
679 long offset; /* the column of END in `<<"END"` */
680 int sourceline; /* lineno of the line that contains `<<"END"` */
681 unsigned length /* the length of END in `<<"END"` */
682#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
683 : HERETERM_LENGTH_BITS
684# define HERETERM_LENGTH_MAX ((1U << HERETERM_LENGTH_BITS) - 1)
685#else
686# define HERETERM_LENGTH_MAX UINT_MAX
687#endif
688 ;
689#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
690 unsigned quote: 1;
691 unsigned func: 8;
692#else
693 uint8_t quote;
694 uint8_t func;
695#endif
696};
697STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
698
699#define STRTERM_HEREDOC IMEMO_FL_USER0
700
701struct rb_strterm_struct {
702 VALUE flags;
703 union {
704 rb_strterm_literal_t literal;
705 rb_strterm_heredoc_t heredoc;
706 } u;
707};
708
709#ifndef RIPPER
710void
711rb_strterm_mark(VALUE obj)
712{
713 rb_strterm_t *strterm = (rb_strterm_t*)obj;
714 if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
715 rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
716 rb_gc_mark(heredoc->lastline);
717 }
718}
719#endif
720
721#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
722size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
723
724#define TOKEN2ID(tok) ( \
725 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
726 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
727 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
728 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
729 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
730 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
731 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
732
733/****** Ripper *******/
734
735#ifdef RIPPER
736#define RIPPER_VERSION "0.1.0"
737
738static inline VALUE intern_sym(const char *name);
739
740#include "eventids1.c"
741#include "eventids2.c"
742
743static VALUE ripper_dispatch0(struct parser_params*,ID);
744static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
745static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
746static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
747static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
748static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
749static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
750static void ripper_error(struct parser_params *p);
751
752#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
753#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
754#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
755#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
756#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
757#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
758#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))
759
760#define yyparse ripper_yyparse
761
762#define ID2VAL(id) STATIC_ID2SYM(id)
763#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
764#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
765
766#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
767 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
768
769#define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
770
771static inline VALUE
772new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
773{
774 NODE *t = (NODE *)tail;
775 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value, block = t->u3.value;
776 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, escape_Qundef(block));
777}
778
779static inline VALUE
780new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
781{
782 NODE *t = rb_node_newnode(NODE_ARGS_AUX, kw_args, kw_rest_arg, block, &NULL_LOC);
783 add_mark_object(p, kw_args);
784 add_mark_object(p, kw_rest_arg);
785 add_mark_object(p, block);
786 return (VALUE)t;
787}
788
789static inline VALUE
790args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
791{
792 return args;
793}
794
795static VALUE
796new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
797{
798 NODE *t = (NODE *)aryptn;
799 struct rb_ary_pattern_info *apinfo = t->nd_apinfo;
800 VALUE pre_args = Qnil, rest_arg = Qnil, post_args = Qnil;
801
802 if (apinfo) {
803 pre_args = rb_ary_entry(apinfo->imemo, 0);
804 rest_arg = rb_ary_entry(apinfo->imemo, 1);
805 post_args = rb_ary_entry(apinfo->imemo, 2);
806 }
807
808 if (!NIL_P(pre_arg)) {
809 if (!NIL_P(pre_args)) {
810 rb_ary_unshift(pre_args, pre_arg);
811 }
812 else {
813 pre_args = rb_ary_new_from_args(1, pre_arg);
814 }
815 }
816 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
817}
818
819static VALUE
820new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
821{
822 NODE *t;
823 struct rb_ary_pattern_info *apinfo;
824
825 if (has_rest) {
826 rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
827 }
828 else {
829 rest_arg = Qnil;
830 }
831
832 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
833 apinfo = ZALLOC(struct rb_ary_pattern_info);
834 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
835 apinfo->imemo = rb_ary_new_from_args(4, pre_args, rest_arg, post_args, tmpbuf);
836
837 t = rb_node_newnode(NODE_ARYPTN, Qnil, Qnil, (VALUE)apinfo, &NULL_LOC);
838 RB_OBJ_WRITTEN(p->ast, Qnil, apinfo->imemo);
839
840 return (VALUE)t;
841}
842
843#define new_hash(p,h,l) rb_ary_new_from_args(0)
844
845static VALUE
846new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
847{
848 return ary;
849}
850
851static VALUE
852new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
853{
854 NODE *t = (NODE *)hshptn;
855 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value;
856 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
857}
858
859static VALUE
860new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
861{
862 NODE *t;
863 if (kw_rest_arg) {
864 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
865 }
866 else {
867 kw_rest_arg = Qnil;
868 }
869 t = rb_node_newnode(NODE_HSHPTN, kw_args, kw_rest_arg, 0, &NULL_LOC);
870
871 add_mark_object(p, kw_args);
872 add_mark_object(p, kw_rest_arg);
873 return (VALUE)t;
874}
875
876#define new_defined(p,expr,loc) dispatch1(defined, (expr))
877
878static VALUE heredoc_dedent(struct parser_params*,VALUE);
879
880#else
881#define ID2VAL(id) (id)
882#define TOKEN2VAL(t) ID2VAL(t)
883#define KWD2EID(t, v) keyword_##t
884#endif /* RIPPER */
885
886#ifndef RIPPER
887# define Qnone 0
888# define Qnull 0
889# define ifndef_ripper(x) (x)
890#else
891# define Qnone Qnil
892# define Qnull Qundef
893# define ifndef_ripper(x)
894#endif
895
896# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
897# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
898# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
899# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
900# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
901# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
902# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
903# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
904# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
905# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
906# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
907# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
908# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
909# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
910# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
911# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
912# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
913# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
914# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
915# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
916#ifdef RIPPER
917static ID id_warn, id_warning, id_gets, id_assoc;
918# define WARN_S_L(s,l) STR_NEW(s,l)
919# define WARN_S(s) STR_NEW2(s)
920# define WARN_I(i) INT2NUM(i)
921# define WARN_ID(i) rb_id2str(i)
922# define WARN_IVAL(i) i
923# define PRIsWARN "s"
924# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
925# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
926# ifdef HAVE_VA_ARGS_MACRO
927# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
928# else
929# define WARN_CALL rb_funcall
930# endif
931# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
932# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
933# ifdef HAVE_VA_ARGS_MACRO
934# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
935# else
936# define WARNING_CALL rb_funcall
937# endif
938PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
939# define compile_error ripper_compile_error
940#else
941# define WARN_S_L(s,l) s
942# define WARN_S(s) s
943# define WARN_I(i) i
944# define WARN_ID(i) rb_id2name(i)
945# define WARN_IVAL(i) NUM2INT(i)
946# define PRIsWARN PRIsVALUE
947# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
948# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
949# define WARN_CALL rb_compile_warn
950# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
951# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
952# define WARNING_CALL rb_compile_warning
953PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
954# define compile_error parser_compile_error
955#endif
956
957static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
958static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
959static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
960static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
961
962#define WARN_EOL(tok) \
963 (looking_at_eol_p(p) ? \
964 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
965 (void)0)
966static int looking_at_eol_p(struct parser_params *p);
967%}
968
969%expect 0
970%define api.pure
971%define parse.error verbose
972%lex-param {struct parser_params *p}
973%parse-param {struct parser_params *p}
974%initial-action
975{
976 RUBY_SET_YYLLOC_OF_NONE(@$);
977};
978
979%union {
980 VALUE val;
981 NODE *node;
982 ID id;
983 int num;
984 st_table *tbl;
985 const struct vtable *vars;
986 struct rb_strterm_struct *strterm;
987}
988
989%token <val>
990 keyword_class "`class'"
991 keyword_module "`module'"
992 keyword_def "`def'"
993 keyword_undef "`undef'"
994 keyword_begin "`begin'"
995 keyword_rescue "`rescue'"
996 keyword_ensure "`ensure'"
997 keyword_end "`end'"
998 keyword_if "`if'"
999 keyword_unless "`unless'"
1000 keyword_then "`then'"
1001 keyword_elsif "`elsif'"
1002 keyword_else "`else'"
1003 keyword_case "`case'"
1004 keyword_when "`when'"
1005 keyword_while "`while'"
1006 keyword_until "`until'"
1007 keyword_for "`for'"
1008 keyword_break "`break'"
1009 keyword_next "`next'"
1010 keyword_redo "`redo'"
1011 keyword_retry "`retry'"
1012 keyword_in "`in'"
1013 keyword_do "`do'"
1014 keyword_do_cond "`do' for condition"
1015 keyword_do_block "`do' for block"
1016 keyword_do_LAMBDA "`do' for lambda"
1017 keyword_return "`return'"
1018 keyword_yield "`yield'"
1019 keyword_super "`super'"
1020 keyword_self "`self'"
1021 keyword_nil "`nil'"
1022 keyword_true "`true'"
1023 keyword_false "`false'"
1024 keyword_and "`and'"
1025 keyword_or "`or'"
1026 keyword_not "`not'"
1027 modifier_if "`if' modifier"
1028 modifier_unless "`unless' modifier"
1029 modifier_while "`while' modifier"
1030 modifier_until "`until' modifier"
1031 modifier_rescue "`rescue' modifier"
1032 keyword_alias "`alias'"
1033 keyword_defined "`defined?'"
1034 keyword_BEGIN "`BEGIN'"
1035 keyword_END "`END'"
1036 keyword__LINE__ "`__LINE__'"
1037 keyword__FILE__ "`__FILE__'"
1038 keyword__ENCODING__ "`__ENCODING__'"
1039
1040%token <val> tIDENTIFIER "local variable or method"
1041%token <val> tFID "method"
1042%token <val> tGVAR "global variable"
1043%token <val> tIVAR "instance variable"
1044%token <val> tCONSTANT "constant"
1045%token <val> tCVAR "class variable"
1046%token <val> tLABEL
1047%token <val> tINTEGER "integer literal"
1048%token <val> tFLOAT "float literal"
1049%token <val> tRATIONAL "rational literal"
1050%token <val> tIMAGINARY "imaginary literal"
1051%token <val> tCHAR "char literal"
1052%token <val> tNTH_REF "numbered reference"
1053%token <val> tBACK_REF "back reference"
1054%token <val> tSTRING_CONTENT "literal content"
1055%token <val> tREGEXP_END
1056
1057%type <val> singleton strings string string1 xstring regexp
1058%type <val> string_contents xstring_contents regexp_contents string_content
1059%type <val> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1060%type <val> literal numeric simple_numeric ssym dsym symbol cpath
1061%type <val> top_compstmt top_stmts top_stmt begin_block
1062%type <val> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1063%type <val> expr_value expr_value_do arg_value primary_value fcall rel_expr
1064%type <val> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1065%type <val> args call_args opt_call_args
1066%type <val> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1067%type <val> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1068%type <val> command_rhs arg_rhs
1069%type <val> command_asgn mrhs mrhs_arg superclass block_call block_command
1070%type <val> f_block_optarg f_block_opt
1071%type <val> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs f_rest_marg
1072%type <val> assoc_list assocs assoc undef_list backref string_dvar for_var
1073%type <val> block_param opt_block_param block_param_def f_opt
1074%type <val> f_kwarg f_kw f_block_kwarg f_block_kw
1075%type <val> bv_decls opt_bv_decl bvar
1076%type <val> lambda f_larglist lambda_body brace_body do_body
1077%type <val> brace_block cmd_brace_block do_block lhs none fitem
1078%type <val> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1079%type <val> p_case_body p_cases p_top_expr p_top_expr_body
1080%type <val> p_expr p_as p_alt p_expr_basic
1081%type <val> p_args p_args_head p_args_tail p_args_post p_arg
1082%type <val> p_value p_primitive p_variable p_var_ref p_const
1083%type <val> p_kwargs p_kwarg p_kw
1084%type <val> keyword_variable user_variable sym operation operation2 operation3
1085%type <val> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1086%type <val> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1087%type <val> p_kwrest p_kwnorest p_kw_label
1088%type <val> f_no_kwarg args_forward
1089%token END_OF_INPUT 0 "end-of-input"
1090%token <val> '.'
1091/* escaped chars, should be ignored otherwise */
1092%token <val> '\\' "backslash"
1093%token tSP "escaped space"
1094%token <val> '\t' "escaped horizontal tab"
1095%token <val> '\f' "escaped form feed"
1096%token <val> '\r' "escaped carriage return"
1097%token <val> '\13' "escaped vertical tab"
1098%token tUPLUS 132 "unary+"
1099%token tUMINUS 133 "unary-"
1100%token tPOW 134 "**"
1101%token tCMP 135 "<=>"
1102%token tEQ 140 "=="
1103%token tEQQ 141 "==="
1104%token tNEQ 142 "!="
1105%token tGEQ 139 ">="
1106%token tLEQ 138 "<="
1107%token tANDOP 148 "&&"
1108%token tOROP 149 "||"
1109%token tMATCH 143 "=~"
1110%token tNMATCH 144 "!~"
1111%token tDOT2 128 ".."
1112%token tDOT3 129 "..."
1113%token tBDOT2 130 "(.."
1114%token tBDOT3 131 "(..."
1115%token tAREF 145 "[]"
1116%token tASET 146 "[]="
1117%token tLSHFT 136 "<<"
1118%token tRSHFT 137 ">>"
1119%token <val> tANDDOT 150 "&."
1120%token <val> tCOLON2 147 "::"
1121%token tCOLON3 ":: at EXPR_BEG"
1122%token <val> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1123%token tASSOC "=>"
1124%token tLPAREN "("
1125%token tLPAREN_ARG "( arg"
1126%token tRPAREN ")"
1127%token tLBRACK "["
1128%token tLBRACE "{"
1129%token tLBRACE_ARG "{ arg"
1130%token tSTAR "*"
1131%token tDSTAR "**arg"
1132%token tAMPER "&"
1133%token tLAMBDA "->"
1134%token tSYMBEG "symbol literal"
1135%token tSTRING_BEG "string literal"
1136%token tXSTRING_BEG "backtick literal"
1137%token tREGEXP_BEG "regexp literal"
1138%token tWORDS_BEG "word list"
1139%token tQWORDS_BEG "verbatim word list"
1140%token tSYMBOLS_BEG "symbol list"
1141%token tQSYMBOLS_BEG "verbatim symbol list"
1142%token tSTRING_END "terminator"
1143%token tSTRING_DEND "'}'"
1144%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1145
1146/*
1147 * precedence table
1148 */
1149
1150%nonassoc tLOWEST
1151%nonassoc tLBRACE_ARG
1152
1153%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1154%left keyword_or keyword_and
1155%right keyword_not
1156%nonassoc keyword_defined
1157%right '=' tOP_ASGN
1158%left modifier_rescue
1159%right '?' ':'
1160%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1161%left tOROP
1162%left tANDOP
1163%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1164%left '>' tGEQ '<' tLEQ
1165%left '|' '^'
1166%left '&'
1167%left tLSHFT tRSHFT
1168%left '+' '-'
1169%left '*' '/' '%'
1170%right tUMINUS_NUM tUMINUS
1171%right tPOW
1172%right '!' '~' tUPLUS
1173
1174%token tLAST_TOKEN
1175
1176%%
1177program : {
1178 SET_LEX_STATE(EXPR_BEG);
1179 local_push(p, ifndef_ripper(1)+0);
1180 }
1181 top_compstmt
1182 {
1183#if 0
1184 if ($2 && !compile_for_eval) {
1185 NODE *node = $2;
1186 /* last expression should not be void */
1187 if (nd_type(node) == NODE_BLOCK) {
1188 while (node->nd_next) {
1189 node = node->nd_next;
1190 }
1191 node = node->nd_head;
1192 }
1193 node = remove_begin(node);
1194 void_expr(p, node);
1195 }
1196 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1197#endif
1198 {VALUE v1,v2;v1=$2;v2=dispatch1(program,v1);p->result=v2;}
1199 local_pop(p);
1200 }
1201 ;
1202
1203top_compstmt : top_stmts opt_terms
1204 {
1205 $$ = void_stmts(p, $1);
1206 }
1207 ;
1208
1209top_stmts : none
1210 {
1211#if 0
1212 $$ = NEW_BEGIN(0, &@$);
1213#endif
1214 {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;}
1215 }
1216 | top_stmt
1217 {
1218#if 0
1219 $$ = newline_node($1);
1220#endif
1221 {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1222 }
1223 | top_stmts terms top_stmt
1224 {
1225#if 0
1226 $$ = block_append(p, $1, newline_node($3));
1227#endif
1228 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1229 }
1230 | error top_stmt
1231 {
1232 $$ = remove_begin($2);
1233 }
1234 ;
1235
1236top_stmt : stmt
1237 | keyword_BEGIN begin_block
1238 {
1239 $$ = $2;
1240 }
1241 ;
1242
1243begin_block : '{' top_compstmt '}'
1244 {
1245#if 0
1246 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1247 NEW_BEGIN($2, &@$));
1248 $$ = NEW_BEGIN(0, &@$);
1249#endif
1250 {VALUE v1,v2;v1=$2;v2=dispatch1(BEGIN,v1);$$=v2;}
1251 }
1252 ;
1253
1254bodystmt : compstmt
1255 opt_rescue
1256 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1257 compstmt
1258 opt_ensure
1259 {
1260#if 0
1261 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1262#endif
1263 {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;}
1264 }
1265 | compstmt
1266 opt_rescue
1267 opt_ensure
1268 {
1269#if 0
1270 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1271#endif
1272 {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;}
1273 }
1274 ;
1275
1276compstmt : stmts opt_terms
1277 {
1278 $$ = void_stmts(p, $1);
1279 }
1280 ;
1281
1282stmts : none
1283 {
1284#if 0
1285 $$ = NEW_BEGIN(0, &@$);
1286#endif
1287 {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;}
1288 }
1289 | stmt_or_begin
1290 {
1291#if 0
1292 $$ = newline_node($1);
1293#endif
1294 {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1295 }
1296 | stmts terms stmt_or_begin
1297 {
1298#if 0
1299 $$ = block_append(p, $1, newline_node($3));
1300#endif
1301 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1302 }
1303 | error stmt
1304 {
1305 $$ = remove_begin($2);
1306 }
1307 ;
1308
1309stmt_or_begin : stmt
1310 {
1311 $$ = $1;
1312 }
1313 | keyword_BEGIN
1314 {
1315 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1316 }
1317 begin_block
1318 {
1319 $$ = $3;
1320 }
1321 ;
1322
1323stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1324 {
1325#if 0
1326 $$ = NEW_ALIAS($2, $4, &@$);
1327#endif
1328 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(alias,v1,v2);$$=v3;}
1329 }
1330 | keyword_alias tGVAR tGVAR
1331 {
1332#if 0
1333 $$ = NEW_VALIAS($2, $3, &@$);
1334#endif
1335 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1336 }
1337 | keyword_alias tGVAR tBACK_REF
1338 {
1339#if 0
1340 char buf[2];
1341 buf[0] = '$';
1342 buf[1] = (char)$3->nd_nth;
1343 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1344#endif
1345 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1346 }
1347 | keyword_alias tGVAR tNTH_REF
1348 {
1349#if 0
1350 yyerror1(&@3, "can't make alias for the number variables");
1351 $$ = NEW_BEGIN(0, &@$);
1352#endif
1353 {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);
1354 }
1355 | keyword_undef undef_list
1356 {
1357#if 0
1358 $$ = $2;
1359#endif
1360 {VALUE v1,v2;v1=$2;v2=dispatch1(undef,v1);$$=v2;}
1361 }
1362 | stmt modifier_if expr_value
1363 {
1364#if 0
1365 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1366 fixpos($$, $3);
1367#endif
1368 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
1369 }
1370 | stmt modifier_unless expr_value
1371 {
1372#if 0
1373 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1374 fixpos($$, $3);
1375#endif
1376 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
1377 }
1378 | stmt modifier_while expr_value
1379 {
1380#if 0
1381 if ($1 && nd_type($1) == NODE_BEGIN) {
1382 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1383 }
1384 else {
1385 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1386 }
1387#endif
1388 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(while_mod,v1,v2);$$=v3;}
1389 }
1390 | stmt modifier_until expr_value
1391 {
1392#if 0
1393 if ($1 && nd_type($1) == NODE_BEGIN) {
1394 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1395 }
1396 else {
1397 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1398 }
1399#endif
1400 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(until_mod,v1,v2);$$=v3;}
1401 }
1402 | stmt modifier_rescue stmt
1403 {
1404#if 0
1405 NODE *resq;
1406 YYLTYPE loc = code_loc_gen(&@2, &@3);
1407 resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1408 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1409#endif
1410 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1411 }
1412 | keyword_END '{' compstmt '}'
1413 {
1414 if (p->in_def) {
1415 rb_warn0("END in method; use at_exit");
1416 }
1417#if 0
1418 {
1419 NODE *scope = NEW_NODE(
1420 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1421 $$ = NEW_POSTEXE(scope, &@$);
1422 }
1423#endif
1424 {VALUE v1,v2;v1=$3;v2=dispatch1(END,v1);$$=v2;}
1425 }
1426 | command_asgn
1427 | mlhs '=' command_call
1428 {
1429#if 0
1430 value_expr($3);
1431 $$ = node_assign(p, $1, $3, &@$);
1432#endif
1433 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(massign,v1,v2);$$=v3;}
1434 }
1435 | lhs '=' mrhs
1436 {
1437#if 0
1438 value_expr($3);
1439 $$ = node_assign(p, $1, $3, &@$);
1440#endif
1441 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
1442 }
1443 | mlhs '=' mrhs_arg modifier_rescue stmt
1444 {
1445#if 0
1446 YYLTYPE loc = code_loc_gen(&@4, &@5);
1447 value_expr($3);
1448 $$ = node_assign(p, $1, NEW_RESCUE($3, NEW_RESBODY(0, remove_begin($5), 0, &loc), 0, &@$), &@$);
1449#endif
1450 {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;}
1451 }
1452 | mlhs '=' mrhs_arg
1453 {
1454#if 0
1455 $$ = node_assign(p, $1, $3, &@$);
1456#endif
1457 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(massign,v1,v2);$$=v3;}
1458 }
1459 | expr
1460 ;
1461
1462command_asgn : lhs '=' command_rhs
1463 {
1464#if 0
1465 $$ = node_assign(p, $1, $3, &@$);
1466#endif
1467 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
1468 }
1469 | var_lhs tOP_ASGN command_rhs
1470 {
1471#if 0
1472 $$ = new_op_assign(p, $1, $2, $3, &@$);
1473#endif
1474 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
1475 }
1476 | primary_value '[' opt_call_args rbracket tOP_ASGN command_rhs
1477 {
1478#if 0
1479 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
1480#endif
1481 {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;}
1482
1483 }
1484 | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
1485 {
1486#if 0
1487 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1488#endif
1489 {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;}
1490 }
1491 | primary_value call_op tCONSTANT tOP_ASGN command_rhs
1492 {
1493#if 0
1494 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1495#endif
1496 {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;}
1497 }
1498 | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
1499 {
1500#if 0
1501 YYLTYPE loc = code_loc_gen(&@1, &@3);
1502 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
1503#endif
1504 {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;}
1505 }
1506 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
1507 {
1508#if 0
1509 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
1510#endif
1511 {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;}
1512 }
1513 | backref tOP_ASGN command_rhs
1514 {
1515#if 0
1516 rb_backref_error(p, $1);
1517 $$ = NEW_BEGIN(0, &@$);
1518#endif
1519 {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);
1520 }
1521 ;
1522
1523command_rhs : command_call %prec tOP_ASGN
1524 {
1525 value_expr($1);
1526 $$ = $1;
1527 }
1528 | command_call modifier_rescue stmt
1529 {
1530#if 0
1531 YYLTYPE loc = code_loc_gen(&@2, &@3);
1532 value_expr($1);
1533 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1534#endif
1535 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1536 }
1537 | command_asgn
1538 ;
1539
1540expr : command_call
1541 | expr keyword_and expr
1542 {
1543 $$ = logop(p, idAND, $1, $3, &@2, &@$);
1544 }
1545 | expr keyword_or expr
1546 {
1547 $$ = logop(p, idOR, $1, $3, &@2, &@$);
1548 }
1549 | keyword_not opt_nl expr
1550 {
1551 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
1552 }
1553 | '!' command_call
1554 {
1555 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
1556 }
1557 | arg keyword_in
1558 {
1559 value_expr($1);
1560 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1561 p->command_start = FALSE;
1562 $<num>$ = p->in_kwarg;
1563 p->in_kwarg = 1;
1564 }
1565 {$<tbl>$ = push_pvtbl(p);}
1566 p_expr
1567 {pop_pvtbl(p, $<tbl>4);}
1568 {
1569 p->in_kwarg = !!$<num>3;
1570#if 0
1571 $$ = new_case3(p, $1, NEW_IN($5, 0, 0, &@5), &@$);
1572#endif
1573 {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;}
1574 }
1575 | arg %prec tLBRACE_ARG
1576 ;
1577
1578expr_value : expr
1579 {
1580 value_expr($1);
1581 $$ = $1;
1582 }
1583 ;
1584
1585expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1586 {
1587 $$ = $2;
1588 }
1589
1590
1591command_call : command
1592 | block_command
1593 ;
1594
1595block_command : block_call
1596 | block_call call_op2 operation2 command_args
1597 {
1598#if 0
1599 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
1600#endif
1601 {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;}
1602 }
1603 ;
1604
1605cmd_brace_block : tLBRACE_ARG brace_body '}'
1606 {
1607 $$ = $2;
1608#if 0
1609 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
1610 nd_set_line($$, @1.end_pos.lineno);
1611#endif
1612 }
1613 ;
1614
1615fcall : operation
1616 {
1617#if 0
1618 $$ = NEW_FCALL($1, 0, &@$);
1619 nd_set_line($$, p->tokline);
1620#endif
1621 $$=$1;
1622 }
1623 ;
1624
1625command : fcall command_args %prec tLOWEST
1626 {
1627#if 0
1628 $1->nd_args = $2;
1629 nd_set_last_loc($1, @2.end_pos);
1630 $$ = $1;
1631#endif
1632 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(command,v1,v2);$$=v3;}
1633 }
1634 | fcall command_args cmd_brace_block
1635 {
1636#if 0
1637 block_dup_check(p, $2, $3);
1638 $1->nd_args = $2;
1639 $$ = method_add_block(p, $1, $3, &@$);
1640 fixpos($$, $1);
1641 nd_set_last_loc($1, @2.end_pos);
1642#endif
1643 {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;}
1644 }
1645 | primary_value call_op operation2 command_args %prec tLOWEST
1646 {
1647#if 0
1648 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
1649#endif
1650 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1651 }
1652 | primary_value call_op operation2 command_args cmd_brace_block
1653 {
1654#if 0
1655 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
1656#endif
1657 {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;}
1658 }
1659 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1660 {
1661#if 0
1662 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
1663#endif
1664 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1665 }
1666 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1667 {
1668#if 0
1669 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
1670#endif
1671 {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;}
1672 }
1673 | keyword_super command_args
1674 {
1675#if 0
1676 $$ = NEW_SUPER($2, &@$);
1677 fixpos($$, $2);
1678#endif
1679 {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
1680 }
1681 | keyword_yield command_args
1682 {
1683#if 0
1684 $$ = new_yield(p, $2, &@$);
1685 fixpos($$, $2);
1686#endif
1687 {VALUE v1,v2;v1=$2;v2=dispatch1(yield,v1);$$=v2;}
1688 }
1689 | k_return call_args
1690 {
1691#if 0
1692 $$ = NEW_RETURN(ret_args(p, $2), &@$);
1693#endif
1694 {VALUE v1,v2;v1=$2;v2=dispatch1(return,v1);$$=v2;}
1695 }
1696 | keyword_break call_args
1697 {
1698#if 0
1699 $$ = NEW_BREAK(ret_args(p, $2), &@$);
1700#endif
1701 {VALUE v1,v2;v1=$2;v2=dispatch1(break,v1);$$=v2;}
1702 }
1703 | keyword_next call_args
1704 {
1705#if 0
1706 $$ = NEW_NEXT(ret_args(p, $2), &@$);
1707#endif
1708 {VALUE v1,v2;v1=$2;v2=dispatch1(next,v1);$$=v2;}
1709 }
1710 ;
1711
1712mlhs : mlhs_basic
1713 | tLPAREN mlhs_inner rparen
1714 {
1715#if 0
1716 $$ = $2;
1717#endif
1718 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1719 }
1720 ;
1721
1722mlhs_inner : mlhs_basic
1723 | tLPAREN mlhs_inner rparen
1724 {
1725#if 0
1726 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1727#endif
1728 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1729 }
1730 ;
1731
1732mlhs_basic : mlhs_head
1733 {
1734#if 0
1735 $$ = NEW_MASGN($1, 0, &@$);
1736#endif
1737 $$=$1;
1738 }
1739 | mlhs_head mlhs_item
1740 {
1741#if 0
1742 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
1743#endif
1744 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1745 }
1746 | mlhs_head tSTAR mlhs_node
1747 {
1748#if 0
1749 $$ = NEW_MASGN($1, $3, &@$);
1750#endif
1751 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1752 }
1753 | mlhs_head tSTAR mlhs_node ',' mlhs_post
1754 {
1755#if 0
1756 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
1757#endif
1758 {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;}
1759 }
1760 | mlhs_head tSTAR
1761 {
1762#if 0
1763 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
1764#endif
1765 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1766 }
1767 | mlhs_head tSTAR ',' mlhs_post
1768 {
1769#if 0
1770 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
1771#endif
1772 {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;}
1773 }
1774 | tSTAR mlhs_node
1775 {
1776#if 0
1777 $$ = NEW_MASGN(0, $2, &@$);
1778#endif
1779 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$2;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1780 }
1781 | tSTAR mlhs_node ',' mlhs_post
1782 {
1783#if 0
1784 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
1785#endif
1786 {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;}
1787 }
1788 | tSTAR
1789 {
1790#if 0
1791 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
1792#endif
1793 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=Qnil;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1794 }
1795 | tSTAR ',' mlhs_post
1796 {
1797#if 0
1798 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
1799#endif
1800 {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;}
1801 }
1802 ;
1803
1804mlhs_item : mlhs_node
1805 | tLPAREN mlhs_inner rparen
1806 {
1807#if 0
1808 $$ = $2;
1809#endif
1810 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1811 }
1812 ;
1813
1814mlhs_head : mlhs_item ','
1815 {
1816#if 0
1817 $$ = NEW_LIST($1, &@1);
1818#endif
1819 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
1820 }
1821 | mlhs_head mlhs_item ','
1822 {
1823#if 0
1824 $$ = list_append(p, $1, $2);
1825#endif
1826 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1827 }
1828 ;
1829
1830mlhs_post : mlhs_item
1831 {
1832#if 0
1833 $$ = NEW_LIST($1, &@$);
1834#endif
1835 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
1836 }
1837 | mlhs_post ',' mlhs_item
1838 {
1839#if 0
1840 $$ = list_append(p, $1, $3);
1841#endif
1842 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1843 }
1844 ;
1845
1846mlhs_node : user_variable
1847 {
1848#if 0
1849 $$ = assignable(p, $1, 0, &@$);
1850#endif
1851 $$=assignable(p, var_field(p, $1));
1852 }
1853 | keyword_variable
1854 {
1855#if 0
1856 $$ = assignable(p, $1, 0, &@$);
1857#endif
1858 $$=assignable(p, var_field(p, $1));
1859 }
1860 | primary_value '[' opt_call_args rbracket
1861 {
1862#if 0
1863 $$ = aryset(p, $1, $3, &@$);
1864#endif
1865 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
1866 }
1867 | primary_value call_op tIDENTIFIER
1868 {
1869 if ($2 == tANDDOT) {
1870 yyerror1(&@2, "&. inside multiple assignment destination");
1871 }
1872#if 0
1873 $$ = attrset(p, $1, $2, $3, &@$);
1874#endif
1875 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1876 }
1877 | primary_value tCOLON2 tIDENTIFIER
1878 {
1879#if 0
1880 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1881#endif
1882 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=v3;}
1883 }
1884 | primary_value call_op tCONSTANT
1885 {
1886 if ($2 == tANDDOT) {
1887 yyerror1(&@2, "&. inside multiple assignment destination");
1888 }
1889#if 0
1890 $$ = attrset(p, $1, $2, $3, &@$);
1891#endif
1892 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1893 }
1894 | primary_value tCOLON2 tCONSTANT
1895 {
1896#if 0
1897 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1898#endif
1899 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
1900 }
1901 | tCOLON3 tCONSTANT
1902 {
1903#if 0
1904 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1905#endif
1906 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
1907 }
1908 | backref
1909 {
1910#if 0
1911 rb_backref_error(p, $1);
1912 $$ = NEW_BEGIN(0, &@$);
1913#endif
1914 {VALUE v1,v2;v1=var_field(p, $1);v2=dispatch1(assign_error,v1);$$=v2;}ripper_error(p);
1915 }
1916 ;
1917
1918lhs : user_variable
1919 {
1920#if 0
1921 $$ = assignable(p, $1, 0, &@$);
1922#endif
1923 $$=assignable(p, var_field(p, $1));
1924 }
1925 | keyword_variable
1926 {
1927#if 0
1928 $$ = assignable(p, $1, 0, &@$);
1929#endif
1930 $$=assignable(p, var_field(p, $1));
1931 }
1932 | primary_value '[' opt_call_args rbracket
1933 {
1934#if 0
1935 $$ = aryset(p, $1, $3, &@$);
1936#endif
1937 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
1938 }
1939 | primary_value call_op tIDENTIFIER
1940 {
1941#if 0
1942 $$ = attrset(p, $1, $2, $3, &@$);
1943#endif
1944 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1945 }
1946 | primary_value tCOLON2 tIDENTIFIER
1947 {
1948#if 0
1949 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1950#endif
1951 {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1952 }
1953 | primary_value call_op tCONSTANT
1954 {
1955#if 0
1956 $$ = attrset(p, $1, $2, $3, &@$);
1957#endif
1958 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1959 }
1960 | primary_value tCOLON2 tCONSTANT
1961 {
1962#if 0
1963 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1964#endif
1965 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
1966 }
1967 | tCOLON3 tCONSTANT
1968 {
1969#if 0
1970 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1971#endif
1972 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
1973 }
1974 | backref
1975 {
1976#if 0
1977 rb_backref_error(p, $1);
1978 $$ = NEW_BEGIN(0, &@$);
1979#endif
1980 {VALUE v1,v2;v1=var_field(p, $1);v2=dispatch1(assign_error,v1);$$=v2;}ripper_error(p);
1981 }
1982 ;
1983
1984cname : tIDENTIFIER
1985 {
1986#if 0
1987 yyerror1(&@1, "class/module name must be CONSTANT");
1988#endif
1989 {VALUE v1,v2;v1=$1;v2=dispatch1(class_name_error,v1);$$=v2;}ripper_error(p);
1990 }
1991 | tCONSTANT
1992 ;
1993
1994cpath : tCOLON3 cname
1995 {
1996#if 0
1997 $$ = NEW_COLON3($2, &@$);
1998#endif
1999 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2000 }
2001 | cname
2002 {
2003#if 0
2004 $$ = NEW_COLON2(0, $$, &@$);
2005#endif
2006 {VALUE v1,v2;v1=$1;v2=dispatch1(const_ref,v1);$$=v2;}
2007 }
2008 | primary_value tCOLON2 cname
2009 {
2010#if 0
2011 $$ = NEW_COLON2($1, $3, &@$);
2012#endif
2013 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2014 }
2015 ;
2016
2017fname : tIDENTIFIER
2018 | tCONSTANT
2019 | tFID
2020 | op
2021 {
2022 SET_LEX_STATE(EXPR_ENDFN);
2023 $$ = $1;
2024 }
2025 | reswords
2026 ;
2027
2028fitem : fname
2029 {
2030#if 0
2031 $$ = NEW_LIT(ID2SYM($1), &@$);
2032#endif
2033 {VALUE v1,v2;v1=$1;v2=dispatch1(symbol_literal,v1);$$=v2;}
2034 }
2035 | symbol
2036 ;
2037
2038undef_list : fitem
2039 {
2040#if 0
2041 $$ = NEW_UNDEF($1, &@$);
2042#endif
2043 $$=rb_ary_new3(1, get_value($1));
2044 }
2045 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2046 {
2047#if 0
2048 NODE *undef = NEW_UNDEF($4, &@4);
2049 $$ = block_append(p, $1, undef);
2050#endif
2051 $$=rb_ary_push($1, get_value($4));
2052 }
2053 ;
2054
2055op : '|' { ifndef_ripper($$ = '|'); }
2056 | '^' { ifndef_ripper($$ = '^'); }
2057 | '&' { ifndef_ripper($$ = '&'); }
2058 | tCMP { ifndef_ripper($$ = tCMP); }
2059 | tEQ { ifndef_ripper($$ = tEQ); }
2060 | tEQQ { ifndef_ripper($$ = tEQQ); }
2061 | tMATCH { ifndef_ripper($$ = tMATCH); }
2062 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2063 | '>' { ifndef_ripper($$ = '>'); }
2064 | tGEQ { ifndef_ripper($$ = tGEQ); }
2065 | '<' { ifndef_ripper($$ = '<'); }
2066 | tLEQ { ifndef_ripper($$ = tLEQ); }
2067 | tNEQ { ifndef_ripper($$ = tNEQ); }
2068 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2069 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2070 | '+' { ifndef_ripper($$ = '+'); }
2071 | '-' { ifndef_ripper($$ = '-'); }
2072 | '*' { ifndef_ripper($$ = '*'); }
2073 | tSTAR { ifndef_ripper($$ = '*'); }
2074 | '/' { ifndef_ripper($$ = '/'); }
2075 | '%' { ifndef_ripper($$ = '%'); }
2076 | tPOW { ifndef_ripper($$ = tPOW); }
2077 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2078 | '!' { ifndef_ripper($$ = '!'); }
2079 | '~' { ifndef_ripper($$ = '~'); }
2080 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2081 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2082 | tAREF { ifndef_ripper($$ = tAREF); }
2083 | tASET { ifndef_ripper($$ = tASET); }
2084 | '`' { ifndef_ripper($$ = '`'); }
2085 ;
2086
2087reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2088 | keyword_BEGIN | keyword_END
2089 | keyword_alias | keyword_and | keyword_begin
2090 | keyword_break | keyword_case | keyword_class | keyword_def
2091 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2092 | keyword_end | keyword_ensure | keyword_false
2093 | keyword_for | keyword_in | keyword_module | keyword_next
2094 | keyword_nil | keyword_not | keyword_or | keyword_redo
2095 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2096 | keyword_super | keyword_then | keyword_true | keyword_undef
2097 | keyword_when | keyword_yield | keyword_if | keyword_unless
2098 | keyword_while | keyword_until
2099 ;
2100
2101arg : lhs '=' arg_rhs
2102 {
2103#if 0
2104 $$ = node_assign(p, $1, $3, &@$);
2105#endif
2106 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
2107 }
2108 | var_lhs tOP_ASGN arg_rhs
2109 {
2110#if 0
2111 $$ = new_op_assign(p, $1, $2, $3, &@$);
2112#endif
2113 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
2114 }
2115 | primary_value '[' opt_call_args rbracket tOP_ASGN arg_rhs
2116 {
2117#if 0
2118 value_expr($6);
2119 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
2120#endif
2121 {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;}
2122 }
2123 | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
2124 {
2125#if 0
2126 value_expr($5);
2127 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2128#endif
2129 {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;}
2130 }
2131 | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
2132 {
2133#if 0
2134 value_expr($5);
2135 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2136#endif
2137 {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;}
2138 }
2139 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
2140 {
2141#if 0
2142 value_expr($5);
2143 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
2144#endif
2145 {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;}
2146 }
2147 | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
2148 {
2149#if 0
2150 YYLTYPE loc = code_loc_gen(&@1, &@3);
2151 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
2152#endif
2153 {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;}
2154 }
2155 | tCOLON3 tCONSTANT tOP_ASGN arg_rhs
2156 {
2157#if 0
2158 $$ = new_const_op_assign(p, NEW_COLON3($2, &@$), $3, $4, &@$);
2159#endif
2160 {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;}
2161 }
2162 | backref tOP_ASGN arg_rhs
2163 {
2164#if 0
2165 rb_backref_error(p, $1);
2166 $$ = NEW_BEGIN(0, &@$);
2167#endif
2168 {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);
2169 }
2170 | arg tDOT2 arg
2171 {
2172#if 0
2173 value_expr($1);
2174 value_expr($3);
2175 $$ = NEW_DOT2($1, $3, &@$);
2176#endif
2177 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
2178 }
2179 | arg tDOT3 arg
2180 {
2181#if 0
2182 value_expr($1);
2183 value_expr($3);
2184 $$ = NEW_DOT3($1, $3, &@$);
2185#endif
2186 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
2187 }
2188 | arg tDOT2
2189 {
2190#if 0
2191 YYLTYPE loc;
2192 loc.beg_pos = @2.end_pos;
2193 loc.end_pos = @2.end_pos;
2194
2195 value_expr($1);
2196 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
2197#endif
2198 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
2199 }
2200 | arg tDOT3
2201 {
2202#if 0
2203 YYLTYPE loc;
2204 loc.beg_pos = @2.end_pos;
2205 loc.end_pos = @2.end_pos;
2206
2207 value_expr($1);
2208 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
2209#endif
2210 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
2211 }
2212 | tBDOT2 arg
2213 {
2214#if 0
2215 YYLTYPE loc;
2216 loc.beg_pos = @1.beg_pos;
2217 loc.end_pos = @1.beg_pos;
2218
2219 value_expr($2);
2220 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
2221#endif
2222 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
2223 }
2224 | tBDOT3 arg
2225 {
2226#if 0
2227 YYLTYPE loc;
2228 loc.beg_pos = @1.beg_pos;
2229 loc.end_pos = @1.beg_pos;
2230
2231 value_expr($2);
2232 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
2233#endif
2234 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
2235 }
2236 | arg '+' arg
2237 {
2238 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2239 }
2240 | arg '-' arg
2241 {
2242 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2243 }
2244 | arg '*' arg
2245 {
2246 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2247 }
2248 | arg '/' arg
2249 {
2250 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2251 }
2252 | arg '%' arg
2253 {
2254 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2255 }
2256 | arg tPOW arg
2257 {
2258 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2259 }
2260 | tUMINUS_NUM simple_numeric tPOW arg
2261 {
2262 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2263 }
2264 | tUPLUS arg
2265 {
2266 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2267 }
2268 | tUMINUS arg
2269 {
2270 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2271 }
2272 | arg '|' arg
2273 {
2274 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2275 }
2276 | arg '^' arg
2277 {
2278 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2279 }
2280 | arg '&' arg
2281 {
2282 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2283 }
2284 | arg tCMP arg
2285 {
2286 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2287 }
2288 | rel_expr %prec tCMP
2289 | arg tEQ arg
2290 {
2291 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2292 }
2293 | arg tEQQ arg
2294 {
2295 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2296 }
2297 | arg tNEQ arg
2298 {
2299 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2300 }
2301 | arg tMATCH arg
2302 {
2303 $$ = match_op(p, $1, $3, &@2, &@$);
2304 }
2305 | arg tNMATCH arg
2306 {
2307 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2308 }
2309 | '!' arg
2310 {
2311 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2312 }
2313 | '~' arg
2314 {
2315 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2316 }
2317 | arg tLSHFT arg
2318 {
2319 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2320 }
2321 | arg tRSHFT arg
2322 {
2323 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2324 }
2325 | arg tANDOP arg
2326 {
2327 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2328 }
2329 | arg tOROP arg
2330 {
2331 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2332 }
2333 | keyword_defined opt_nl {p->in_defined = 1;} arg
2334 {
2335 p->in_defined = 0;
2336 $$ = new_defined(p, $4, &@$);
2337 }
2338 | arg '?' arg opt_nl ':' arg
2339 {
2340#if 0
2341 value_expr($1);
2342 $$ = new_if(p, $1, $3, $6, &@$);
2343 fixpos($$, $1);
2344#endif
2345 {VALUE v1,v2,v3,v4;v1=$1;v2=$3;v3=$6;v4=dispatch3(ifop,v1,v2,v3);$$=v4;}
2346 }
2347 | primary
2348 {
2349 $$ = $1;
2350 }
2351 ;
2352
2353relop : '>' {$$ = '>';}
2354 | '<' {$$ = '<';}
2355 | tGEQ {$$ = idGE;}
2356 | tLEQ {$$ = idLE;}
2357 ;
2358
2359rel_expr : arg relop arg %prec '>'
2360 {
2361 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2362 }
2363 | rel_expr relop arg %prec '>'
2364 {
2365 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2366 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2367 }
2368 ;
2369
2370arg_value : arg
2371 {
2372 value_expr($1);
2373 $$ = $1;
2374 }
2375 ;
2376
2377aref_args : none
2378 | args trailer
2379 {
2380 $$ = $1;
2381 }
2382 | args ',' assocs trailer
2383 {
2384#if 0
2385 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2386#endif
2387 {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;}
2388 }
2389 | assocs trailer
2390 {
2391#if 0
2392 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2393#endif
2394 {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;}
2395 }
2396 ;
2397
2398arg_rhs : arg %prec tOP_ASGN
2399 {
2400 value_expr($1);
2401 $$ = $1;
2402 }
2403 | arg modifier_rescue arg
2404 {
2405#if 0
2406 YYLTYPE loc = code_loc_gen(&@2, &@3);
2407 value_expr($1);
2408 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
2409#endif
2410 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
2411 }
2412 ;
2413
2414paren_args : '(' opt_call_args rparen
2415 {
2416#if 0
2417 $$ = $2;
2418#endif
2419 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(arg_paren,v1);$$=v2;}
2420 }
2421 | '(' args ',' args_forward rparen
2422 {
2423 if (!local_id(p, idFWD_REST) ||
2424#if idFWD_KWREST
2425 !local_id(p, idFWD_KWREST) ||
2426#endif
2427 !local_id(p, idFWD_BLOCK)) {
2428 compile_error(p, "unexpected ...");
2429 $$ = Qnone;
2430 }
2431 else {
2432#if 0
2433 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@4), &@4);
2434#if idFWD_KWREST
2435 NODE *kwrest = list_append(p, NEW_LIST(0, &@4), NEW_LVAR(idFWD_KWREST, &@4));
2436#endif
2437 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@4), &@4);
2438 $$ = rest_arg_append(p, $2, splat, &@$);
2439#if idFWD_KWREST
2440 $$ = arg_append(p, $$, new_hash(p, kwrest, &@4), &@4);
2441#endif
2442 $$ = arg_blk_pass($$, block);
2443#endif
2444 {VALUE v1,v2,v3,v4,v5;v1=$2;v2=$4;v3=dispatch2(args_add,v1,v2);v4=v3;v5=dispatch1(arg_paren,v4);$$=v5;}
2445 }
2446 }
2447 | '(' args_forward rparen
2448 {
2449 if (!local_id(p, idFWD_REST) ||
2450#if idFWD_KWREST
2451 !local_id(p, idFWD_KWREST) ||
2452#endif
2453 !local_id(p, idFWD_BLOCK)) {
2454 compile_error(p, "unexpected ...");
2455 $$ = Qnone;
2456 }
2457 else {
2458#if 0
2459 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@2), &@2);
2460#if idFWD_KWREST
2461 NODE *kwrest = list_append(p, NEW_LIST(0, &@2), NEW_LVAR(idFWD_KWREST, &@2));
2462#endif
2463 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@2), &@2);
2464#if idFWD_KWREST
2465 $$ = arg_append(p, splat, new_hash(p, kwrest, &@2), &@2);
2466#else
2467 $$ = splat;
2468#endif
2469 $$ = arg_blk_pass($$, block);
2470#endif
2471 {VALUE v1,v2;v1=$2;v2=dispatch1(arg_paren,v1);$$=v2;}
2472 }
2473 }
2474 ;
2475
2476opt_paren_args : none
2477 | paren_args
2478 ;
2479
2480opt_call_args : none
2481 | call_args
2482 | args ','
2483 {
2484 $$ = $1;
2485 }
2486 | args ',' assocs ','
2487 {
2488#if 0
2489 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2490#endif
2491 {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;}
2492 }
2493 | assocs ','
2494 {
2495#if 0
2496 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2497#endif
2498 {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;}
2499 }
2500 ;
2501
2502call_args : command
2503 {
2504#if 0
2505 value_expr($1);
2506 $$ = NEW_LIST($1, &@$);
2507#endif
2508 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2509 }
2510 | args opt_block_arg
2511 {
2512#if 0
2513 $$ = arg_blk_pass($1, $2);
2514#endif
2515 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(args_add_block,v1,v2);$$=v3;}
2516 }
2517 | assocs opt_block_arg
2518 {
2519#if 0
2520 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2521 $$ = arg_blk_pass($$, $2);
2522#endif
2523 {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;}
2524 }
2525 | args ',' assocs opt_block_arg
2526 {
2527#if 0
2528 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2529 $$ = arg_blk_pass($$, $4);
2530#endif
2531 {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;}
2532 }
2533 | block_arg
2534 {{VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add_block,v2,v3);$$=v4;}}
2535 ;
2536
2537command_args : {
2538 /* If call_args starts with a open paren '(' or '[',
2539 * look-ahead reading of the letters calls CMDARG_PUSH(0),
2540 * but the push must be done after CMDARG_PUSH(1).
2541 * So this code makes them consistent by first cancelling
2542 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
2543 * and finally redoing CMDARG_PUSH(0).
2544 */
2545 int lookahead = 0;
2546 switch (yychar) {
2547 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
2548 lookahead = 1;
2549 }
2550 if (lookahead) CMDARG_POP();
2551 CMDARG_PUSH(1);
2552 if (lookahead) CMDARG_PUSH(0);
2553 }
2554 call_args
2555 {
2556 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
2557 * but the push must be done after CMDARG_POP() in the parser.
2558 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
2559 * CMDARG_POP() to pop 1 pushed by command_args,
2560 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
2561 */
2562 int lookahead = 0;
2563 switch (yychar) {
2564 case tLBRACE_ARG:
2565 lookahead = 1;
2566 }
2567 if (lookahead) CMDARG_POP();
2568 CMDARG_POP();
2569 if (lookahead) CMDARG_PUSH(0);
2570 $$ = $2;
2571 }
2572 ;
2573
2574block_arg : tAMPER arg_value
2575 {
2576#if 0
2577 $$ = NEW_BLOCK_PASS($2, &@$);
2578#endif
2579 $$=$2;
2580 }
2581 ;
2582
2583opt_block_arg : ',' block_arg
2584 {
2585 $$ = $2;
2586 }
2587 | none
2588 {
2589 $$ = 0;
2590 }
2591 ;
2592
2593args : arg_value
2594 {
2595#if 0
2596 $$ = NEW_LIST($1, &@$);
2597#endif
2598 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2599 }
2600 | tSTAR arg_value
2601 {
2602#if 0
2603 $$ = NEW_SPLAT($2, &@$);
2604#endif
2605 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
2606 }
2607 | args ',' arg_value
2608 {
2609#if 0
2610 $$ = last_arg_append(p, $1, $3, &@$);
2611#endif
2612 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
2613 }
2614 | args ',' tSTAR arg_value
2615 {
2616#if 0
2617 $$ = rest_arg_append(p, $1, $4, &@$);
2618#endif
2619 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
2620 }
2621 ;
2622
2623mrhs_arg : mrhs
2624 | arg_value
2625 ;
2626
2627mrhs : args ',' arg_value
2628 {
2629#if 0
2630 $$ = last_arg_append(p, $1, $3, &@$);
2631#endif
2632 {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;}
2633 }
2634 | args ',' tSTAR arg_value
2635 {
2636#if 0
2637 $$ = rest_arg_append(p, $1, $4, &@$);
2638#endif
2639 {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;}
2640 }
2641 | tSTAR arg_value
2642 {
2643#if 0
2644 $$ = NEW_SPLAT($2, &@$);
2645#endif
2646 {VALUE v1,v2,v3,v4;v1=dispatch0(mrhs_new);v2=v1;v3=$2;v4=dispatch2(mrhs_add_star,v2,v3);$$=v4;}
2647 }
2648 ;
2649
2650primary : literal
2651 | strings
2652 | xstring
2653 | regexp
2654 | words
2655 | qwords
2656 | symbols
2657 | qsymbols
2658 | var_ref
2659 | backref
2660 | tFID
2661 {
2662#if 0
2663 $$ = NEW_FCALL($1, 0, &@$);
2664#endif
2665 {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;}
2666 }
2667 | k_begin
2668 {
2669 CMDARG_PUSH(0);
2670 }
2671 bodystmt
2672 k_end
2673 {
2674 CMDARG_POP();
2675#if 0
2676 set_line_body($3, @1.end_pos.lineno);
2677 $$ = NEW_BEGIN($3, &@$);
2678 nd_set_line($$, @1.end_pos.lineno);
2679#endif
2680 {VALUE v1,v2;v1=$3;v2=dispatch1(begin,v1);$$=v2;}
2681 }
2682 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2683 {
2684#if 0
2685 $$ = NEW_BEGIN(0, &@$);
2686#endif
2687 {VALUE v1,v2;v1=0;v2=dispatch1(paren,v1);$$=v2;}
2688 }
2689 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2690 {
2691#if 0
2692 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2693 $$ = $2;
2694#endif
2695 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2696 }
2697 | tLPAREN compstmt ')'
2698 {
2699#if 0
2700 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2701 $$ = $2;
2702#endif
2703 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2704 }
2705 | primary_value tCOLON2 tCONSTANT
2706 {
2707#if 0
2708 $$ = NEW_COLON2($1, $3, &@$);
2709#endif
2710 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2711 }
2712 | tCOLON3 tCONSTANT
2713 {
2714#if 0
2715 $$ = NEW_COLON3($2, &@$);
2716#endif
2717 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2718 }
2719 | tLBRACK aref_args ']'
2720 {
2721#if 0
2722 $$ = make_list($2, &@$);
2723#endif
2724 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(array,v1);$$=v2;}
2725 }
2726 | tLBRACE assoc_list '}'
2727 {
2728#if 0
2729 $$ = new_hash(p, $2, &@$);
2730 $$->nd_brace = TRUE;
2731#endif
2732 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(hash,v1);$$=v2;}
2733 }
2734 | k_return
2735 {
2736#if 0
2737 $$ = NEW_RETURN(0, &@$);
2738#endif
2739 {VALUE v1;v1=dispatch0(return0);$$=v1;}
2740 }
2741 | keyword_yield '(' call_args rparen
2742 {
2743#if 0
2744 $$ = new_yield(p, $3, &@$);
2745#endif
2746 {VALUE v1,v2,v3,v4;v1=$3;v2=dispatch1(paren,v1);v3=v2;v4=dispatch1(yield,v3);$$=v4;}
2747 }
2748 | keyword_yield '(' rparen
2749 {
2750#if 0
2751 $$ = NEW_YIELD(0, &@$);
2752#endif
2753 {VALUE v1,v2,v3,v4,v5;v1=dispatch0(args_new);v2=v1;v3=dispatch1(paren,v2);v4=v3;v5=dispatch1(yield,v4);$$=v5;}
2754 }
2755 | keyword_yield
2756 {
2757#if 0
2758 $$ = NEW_YIELD(0, &@$);
2759#endif
2760 {VALUE v1;v1=dispatch0(yield0);$$=v1;}
2761 }
2762 | keyword_defined opt_nl '(' {p->in_defined = 1;} expr rparen
2763 {
2764 p->in_defined = 0;
2765 $$ = new_defined(p, $5, &@$);
2766 }
2767 | keyword_not '(' expr rparen
2768 {
2769 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2770 }
2771 | keyword_not '(' rparen
2772 {
2773 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
2774 }
2775 | fcall brace_block
2776 {
2777#if 0
2778 $$ = method_add_block(p, $1, $2, &@$);
2779#endif
2780 {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;}
2781 }
2782 | method_call
2783 | method_call brace_block
2784 {
2785#if 0
2786 block_dup_check(p, $1->nd_args, $2);
2787 $$ = method_add_block(p, $1, $2, &@$);
2788#endif
2789 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
2790 }
2791 | tLAMBDA
2792 {
2793 token_info_push(p, "->", &@1);
2794 }
2795 lambda
2796 {
2797 $$ = $3;
2798#if 0
2799 nd_set_first_loc($$, @1.beg_pos);
2800#endif
2801 }
2802 | k_if expr_value then
2803 compstmt
2804 if_tail
2805 k_end
2806 {
2807#if 0
2808 $$ = new_if(p, $2, $4, $5, &@$);
2809 fixpos($$, $2);
2810#endif
2811 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(if,v1,v2,v3);$$=v4;}
2812 }
2813 | k_unless expr_value then
2814 compstmt
2815 opt_else
2816 k_end
2817 {
2818#if 0
2819 $$ = new_unless(p, $2, $4, $5, &@$);
2820 fixpos($$, $2);
2821#endif
2822 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(unless,v1,v2,v3);$$=v4;}
2823 }
2824 | k_while expr_value_do
2825 compstmt
2826 k_end
2827 {
2828#if 0
2829 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
2830 fixpos($$, $2);
2831#endif
2832 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(while,v1,v2);$$=v3;}
2833 }
2834 | k_until expr_value_do
2835 compstmt
2836 k_end
2837 {
2838#if 0
2839 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
2840 fixpos($$, $2);
2841#endif
2842 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(until,v1,v2);$$=v3;}
2843 }
2844 | k_case expr_value opt_terms
2845 {
2846 $<val>$ = p->case_labels;
2847 p->case_labels = Qnil;
2848 }
2849 case_body
2850 k_end
2851 {
2852 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2853 p->case_labels = $<val>4;
2854#if 0
2855 $$ = NEW_CASE($2, $5, &@$);
2856 fixpos($$, $2);
2857#endif
2858 {VALUE v1,v2,v3;v1=$2;v2=$5;v3=dispatch2(case,v1,v2);$$=v3;}
2859 }
2860 | k_case opt_terms
2861 {
2862 $<val>$ = p->case_labels;
2863 p->case_labels = 0;
2864 }
2865 case_body
2866 k_end
2867 {
2868 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2869 p->case_labels = $<val>3;
2870#if 0
2871 $$ = NEW_CASE2($4, &@$);
2872#endif
2873 {VALUE v1,v2,v3;v1=Qnil;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
2874 }
2875 | k_case expr_value opt_terms
2876 p_case_body
2877 k_end
2878 {
2879#if 0
2880 $$ = new_case3(p, $2, $4, &@$);
2881#endif
2882 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
2883 }
2884 | k_for for_var keyword_in expr_value_do
2885 compstmt
2886 k_end
2887 {
2888#if 0
2889 /*
2890 * for a, b, c in e
2891 * #=>
2892 * e.each{|*x| a, b, c = x}
2893 *
2894 * for a in e
2895 * #=>
2896 * e.each{|x| a, = x}
2897 */
2898 ID id = internal_id(p);
2899 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
2900 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
2901 ID *tbl = ALLOC_N(ID, 3);
2902 tbl[0] = 1 /* length of local var table */; tbl[1] = id /* internal id */;
2903 rb_ast_add_local_table(p->ast, tbl);
2904
2905 switch (nd_type($2)) {
2906 case NODE_LASGN:
2907 case NODE_DASGN:
2908 case NODE_DASGN_CURR: /* e.each {|internal_var| a = internal_var; ... } */
2909 $2->nd_value = internal_var;
2910 id = 0;
2911 m->nd_plen = 1;
2912 m->nd_next = $2;
2913 break;
2914 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
2915 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), &@2);
2916 break;
2917 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
2918 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, &@2);
2919 }
2920 /* {|*internal_id| <m> = internal_id; ... } */
2921 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
2922 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
2923 $$ = NEW_FOR($4, scope, &@$);
2924 fixpos($$, $2);
2925#endif
2926 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=$5;v4=dispatch3(for,v1,v2,v3);$$=v4;}
2927 }
2928 | k_class cpath superclass
2929 {
2930 if (p->in_def) {
2931 YYLTYPE loc = code_loc_gen(&@1, &@2);
2932 yyerror1(&loc, "class definition in method body");
2933 }
2934 $<num>1 = p->in_class;
2935 p->in_class = 1;
2936 local_push(p, 0);
2937 }
2938 bodystmt
2939 k_end
2940 {
2941#if 0
2942 $$ = NEW_CLASS($2, $5, $3, &@$);
2943 nd_set_line($$->nd_body, @6.end_pos.lineno);
2944 set_line_body($5, @3.end_pos.lineno);
2945 nd_set_line($$, @3.end_pos.lineno);
2946#endif
2947 {VALUE v1,v2,v3,v4;v1=$2;v2=$3;v3=$5;v4=dispatch3(class,v1,v2,v3);$$=v4;}
2948 local_pop(p);
2949 p->in_class = $<num>1 & 1;
2950 }
2951 | k_class tLSHFT expr
2952 {
2953 $<num>$ = (p->in_class << 1) | p->in_def;
2954 p->in_def = 0;
2955 p->in_class = 0;
2956 local_push(p, 0);
2957 }
2958 term
2959 bodystmt
2960 k_end
2961 {
2962#if 0
2963 $$ = NEW_SCLASS($3, $6, &@$);
2964 nd_set_line($$->nd_body, @7.end_pos.lineno);
2965 set_line_body($6, nd_line($3));
2966 fixpos($$, $3);
2967#endif
2968 {VALUE v1,v2,v3;v1=$3;v2=$6;v3=dispatch2(sclass,v1,v2);$$=v3;}
2969 local_pop(p);
2970 p->in_def = $<num>4 & 1;
2971 p->in_class = ($<num>4 >> 1) & 1;
2972 }
2973 | k_module cpath
2974 {
2975 if (p->in_def) {
2976 YYLTYPE loc = code_loc_gen(&@1, &@2);
2977 yyerror1(&loc, "module definition in method body");
2978 }
2979 $<num>1 = p->in_class;
2980 p->in_class = 1;
2981 local_push(p, 0);
2982 }
2983 bodystmt
2984 k_end
2985 {
2986#if 0
2987 $$ = NEW_MODULE($2, $4, &@$);
2988 nd_set_line($$->nd_body, @5.end_pos.lineno);
2989 set_line_body($4, @2.end_pos.lineno);
2990 nd_set_line($$, @2.end_pos.lineno);
2991#endif
2992 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(module,v1,v2);$$=v3;}
2993 local_pop(p);
2994 p->in_class = $<num>1 & 1;
2995 }
2996 | k_def fname
2997 {
2998 numparam_name(p, get_id($2));
2999 local_push(p, 0);
3000 $<id>$ = p->cur_arg;
3001 p->cur_arg = 0;
3002 }
3003 {
3004 $<num>$ = p->in_def;
3005 p->in_def = 1;
3006 }
3007 f_arglist
3008 bodystmt
3009 k_end
3010 {
3011#if 0
3012 NODE *body = remove_begin($6);
3013 reduce_nodes(p, &body);
3014 $$ = NEW_DEFN($2, $5, body, &@$);
3015 nd_set_line($$->nd_defn, @7.end_pos.lineno);
3016 set_line_body(body, @1.beg_pos.lineno);
3017#endif
3018 {VALUE v1,v2,v3,v4;v1=$2;v2=$5;v3=$6;v4=dispatch3(def,v1,v2,v3);$$=v4;}
3019 local_pop(p);
3020 p->in_def = $<num>4 & 1;
3021 p->cur_arg = $<id>3;
3022 }
3023 | k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname
3024 {
3025 numparam_name(p, get_id($5));
3026 $<num>4 = p->in_def;
3027 p->in_def = 1;
3028 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3029 local_push(p, 0);
3030 $<id>$ = p->cur_arg;
3031 p->cur_arg = 0;
3032 }
3033 f_arglist
3034 bodystmt
3035 k_end
3036 {
3037#if 0
3038 NODE *body = remove_begin($8);
3039 reduce_nodes(p, &body);
3040 $$ = NEW_DEFS($2, $5, $7, body, &@$);
3041 nd_set_line($$->nd_defn, @9.end_pos.lineno);
3042 set_line_body(body, @1.beg_pos.lineno);
3043#endif
3044 {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;}
3045 local_pop(p);
3046 p->in_def = $<num>4 & 1;
3047 p->cur_arg = $<id>6;
3048 }
3049 | keyword_break
3050 {
3051#if 0
3052 $$ = NEW_BREAK(0, &@$);
3053#endif
3054 {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(break,v2);$$=v3;}
3055 }
3056 | keyword_next
3057 {
3058#if 0
3059 $$ = NEW_NEXT(0, &@$);
3060#endif
3061 {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(next,v2);$$=v3;}
3062 }
3063 | keyword_redo
3064 {
3065#if 0
3066 $$ = NEW_REDO(&@$);
3067#endif
3068 {VALUE v1;v1=dispatch0(redo);$$=v1;}
3069 }
3070 | keyword_retry
3071 {
3072#if 0
3073 $$ = NEW_RETRY(&@$);
3074#endif
3075 {VALUE v1;v1=dispatch0(retry);$$=v1;}
3076 }
3077 ;
3078
3079primary_value : primary
3080 {
3081 value_expr($1);
3082 $$ = $1;
3083 }
3084 ;
3085
3086k_begin : keyword_begin
3087 {
3088 token_info_push(p, "begin", &@$);
3089 }
3090 ;
3091
3092k_if : keyword_if
3093 {
3094 WARN_EOL("if");
3095 token_info_push(p, "if", &@$);
3096 if (p->token_info && p->token_info->nonspc &&
3097 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3098 const char *tok = p->lex.ptok;
3099 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3100 beg += rb_strlen_lit("else");
3101 while (beg < tok && ISSPACE(*beg)) beg++;
3102 if (beg == tok) {
3103 p->token_info->nonspc = 0;
3104 }
3105 }
3106 }
3107 ;
3108
3109k_unless : keyword_unless
3110 {
3111 token_info_push(p, "unless", &@$);
3112 }
3113 ;
3114
3115k_while : keyword_while
3116 {
3117 token_info_push(p, "while", &@$);
3118 }
3119 ;
3120
3121k_until : keyword_until
3122 {
3123 token_info_push(p, "until", &@$);
3124 }
3125 ;
3126
3127k_case : keyword_case
3128 {
3129 token_info_push(p, "case", &@$);
3130 }
3131 ;
3132
3133k_for : keyword_for
3134 {
3135 token_info_push(p, "for", &@$);
3136 }
3137 ;
3138
3139k_class : keyword_class
3140 {
3141 token_info_push(p, "class", &@$);
3142 }
3143 ;
3144
3145k_module : keyword_module
3146 {
3147 token_info_push(p, "module", &@$);
3148 }
3149 ;
3150
3151k_def : keyword_def
3152 {
3153 token_info_push(p, "def", &@$);
3154 }
3155 ;
3156
3157k_do : keyword_do
3158 {
3159 token_info_push(p, "do", &@$);
3160 }
3161 ;
3162
3163k_do_block : keyword_do_block
3164 {
3165 token_info_push(p, "do", &@$);
3166 }
3167 ;
3168
3169k_rescue : keyword_rescue
3170 {
3171 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3172 }
3173 ;
3174
3175k_ensure : keyword_ensure
3176 {
3177 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3178 }
3179 ;
3180
3181k_when : keyword_when
3182 {
3183 token_info_warn(p, "when", p->token_info, 0, &@$);
3184 }
3185 ;
3186
3187k_else : keyword_else
3188 {
3189 token_info *ptinfo_beg = p->token_info;
3190 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3191 token_info_warn(p, "else", p->token_info, same, &@$);
3192 if (same) {
3193 token_info e;
3194 e.next = ptinfo_beg->next;
3195 e.token = "else";
3196 token_info_setup(&e, p->lex.pbeg, &@$);
3197 if (!e.nonspc) *ptinfo_beg = e;
3198 }
3199 }
3200 ;
3201
3202k_elsif : keyword_elsif
3203 {
3204 WARN_EOL("elsif");
3205 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3206 }
3207 ;
3208
3209k_end : keyword_end
3210 {
3211 token_info_pop(p, "end", &@$);
3212 }
3213 ;
3214
3215k_return : keyword_return
3216 {
3217 if (p->in_class && !p->in_def && !dyna_in_block(p))
3218 yyerror1(&@1, "Invalid return in class/module body");
3219 }
3220 ;
3221
3222then : term
3223 | keyword_then
3224 | term keyword_then
3225 ;
3226
3227do : term
3228 | keyword_do_cond
3229 ;
3230
3231if_tail : opt_else
3232 | k_elsif expr_value then
3233 compstmt
3234 if_tail
3235 {
3236#if 0
3237 $$ = new_if(p, $2, $4, $5, &@$);
3238 fixpos($$, $2);
3239#endif
3240 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(elsif,v1,v2,v3);$$=v4;}
3241 }
3242 ;
3243
3244opt_else : none
3245 | k_else compstmt
3246 {
3247#if 0
3248 $$ = $2;
3249#endif
3250 {VALUE v1,v2;v1=$2;v2=dispatch1(else,v1);$$=v2;}
3251 }
3252 ;
3253
3254for_var : lhs
3255 | mlhs
3256 ;
3257
3258f_marg : f_norm_arg
3259 {
3260#if 0
3261 $$ = assignable(p, $1, 0, &@$);
3262 mark_lvar_used(p, $$);
3263#endif
3264 $$=assignable(p, $1);
3265 }
3266 | tLPAREN f_margs rparen
3267 {
3268#if 0
3269 $$ = $2;
3270#endif
3271 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
3272 }
3273 ;
3274
3275f_marg_list : f_marg
3276 {
3277#if 0
3278 $$ = NEW_LIST($1, &@$);
3279#endif
3280 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
3281 }
3282 | f_marg_list ',' f_marg
3283 {
3284#if 0
3285 $$ = list_append(p, $1, $3);
3286#endif
3287 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
3288 }
3289 ;
3290
3291f_margs : f_marg_list
3292 {
3293#if 0
3294 $$ = NEW_MASGN($1, 0, &@$);
3295#endif
3296 $$=$1;
3297 }
3298 | f_marg_list ',' f_rest_marg
3299 {
3300#if 0
3301 $$ = NEW_MASGN($1, $3, &@$);
3302#endif
3303 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
3304 }
3305 | f_marg_list ',' f_rest_marg ',' f_marg_list
3306 {
3307#if 0
3308 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3309#endif
3310 {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;}
3311 }
3312 | f_rest_marg
3313 {
3314#if 0
3315 $$ = NEW_MASGN(0, $1, &@$);
3316#endif
3317 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
3318 }
3319 | f_rest_marg ',' f_marg_list
3320 {
3321#if 0
3322 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3323#endif
3324 {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;}
3325 }
3326 ;
3327
3328f_rest_marg : tSTAR f_norm_arg
3329 {
3330#if 0
3331 $$ = assignable(p, $2, 0, &@$);
3332 mark_lvar_used(p, $$);
3333#endif
3334 $$=assignable(p, $2);
3335 }
3336 | tSTAR
3337 {
3338#if 0
3339 $$ = NODE_SPECIAL_NO_NAME_REST;
3340#endif
3341 $$=Qnil;
3342 }
3343 ;
3344
3345block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3346 {
3347 $$ = new_args_tail(p, $1, $3, $4, &@3);
3348 }
3349 | f_block_kwarg opt_f_block_arg
3350 {
3351 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3352 }
3353 | f_kwrest opt_f_block_arg
3354 {
3355 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3356 }
3357 | f_no_kwarg opt_f_block_arg
3358 {
3359 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
3360 }
3361 | f_block_arg
3362 {
3363 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3364 }
3365 ;
3366
3367opt_block_args_tail : ',' block_args_tail
3368 {
3369 $$ = $2;
3370 }
3371 | /* none */
3372 {
3373 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3374 }
3375 ;
3376
3377block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3378 {
3379 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3380 }
3381 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3382 {
3383 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3384 }
3385 | f_arg ',' f_block_optarg opt_block_args_tail
3386 {
3387 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3388 }
3389 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3390 {
3391 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3392 }
3393 | f_arg ',' f_rest_arg opt_block_args_tail
3394 {
3395 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3396 }
3397 | f_arg ','
3398 {
3399#if 0
3400 /* magic number for rest_id in iseq_set_arguments() */
3401 $$ = new_args(p, $1, Qnone, NODE_SPECIAL_EXCESSIVE_COMMA, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, &@1), &@$);
3402#endif
3403 {VALUE v1;v1=dispatch0(excessed_comma);$$=new_args(p, $1, Qnone, v1, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, NULL), NULL);}
3404 }
3405 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3406 {
3407 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
3408 }
3409 | f_arg opt_block_args_tail
3410 {
3411 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
3412 }
3413 | f_block_optarg ',' f_rest_arg opt_block_args_tail
3414 {
3415 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
3416 }
3417 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3418 {
3419 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
3420 }
3421 | f_block_optarg opt_block_args_tail
3422 {
3423 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
3424 }
3425 | f_block_optarg ',' f_arg opt_block_args_tail
3426 {
3427 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
3428 }
3429 | f_rest_arg opt_block_args_tail
3430 {
3431 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
3432 }
3433 | f_rest_arg ',' f_arg opt_block_args_tail
3434 {
3435 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
3436 }
3437 | block_args_tail
3438 {
3439 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
3440 }
3441 ;
3442
3443opt_block_param : none
3444 | block_param_def
3445 {
3446 p->command_start = TRUE;
3447 }
3448 ;
3449
3450block_param_def : '|' opt_bv_decl '|'
3451 {
3452 p->cur_arg = 0;
3453 p->max_numparam = ORDINAL_PARAM;
3454#if 0
3455 $$ = 0;
3456#endif
3457 {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;}
3458 }
3459 | '|' block_param opt_bv_decl '|'
3460 {
3461 p->cur_arg = 0;
3462 p->max_numparam = ORDINAL_PARAM;
3463#if 0
3464 $$ = $2;
3465#endif
3466 {VALUE v1,v2,v3;v1=escape_Qundef($2);v2=escape_Qundef($3);v3=dispatch2(block_var,v1,v2);$$=v3;}
3467 }
3468 ;
3469
3470
3471opt_bv_decl : opt_nl
3472 {
3473 $$ = 0;
3474 }
3475 | opt_nl ';' bv_decls opt_nl
3476 {
3477#if 0
3478 $$ = 0;
3479#endif
3480 $$=$3;
3481 }
3482 ;
3483
3484bv_decls : bvar
3485 {$$=rb_ary_new3(1, get_value($1));}
3486 | bv_decls ',' bvar
3487 {$$=rb_ary_push($1, get_value($3));}
3488 ;
3489
3490bvar : tIDENTIFIER
3491 {
3492 new_bv(p, get_id($1));
3493 $$=get_value($1);
3494 }
3495 | f_bad_arg
3496 {
3497 $$ = 0;
3498 }
3499 ;
3500
3501lambda : {
3502 $<vars>$ = dyna_push(p);
3503 }
3504 {
3505 $<num>$ = p->lex.lpar_beg;
3506 p->lex.lpar_beg = p->lex.paren_nest;
3507 }
3508 {
3509 $<num>$ = p->max_numparam;
3510 p->max_numparam = 0;
3511 }
3512 {
3513 $<node>$ = numparam_push(p);
3514 }
3515 f_larglist
3516 {
3517 CMDARG_PUSH(0);
3518 }
3519 lambda_body
3520 {
3521 int max_numparam = p->max_numparam;
3522 p->lex.lpar_beg = $<num>2;
3523 p->max_numparam = $<num>3;
3524 CMDARG_POP();
3525 $5 = args_with_numbered(p, $5, max_numparam);
3526#if 0
3527 {
3528 YYLTYPE loc = code_loc_gen(&@5, &@7);
3529 $$ = NEW_LAMBDA($5, $7, &loc);
3530 nd_set_line($$->nd_body, @7.end_pos.lineno);
3531 nd_set_line($$, @5.end_pos.lineno);
3532 }
3533#endif
3534 {VALUE v1,v2,v3;v1=$5;v2=$7;v3=dispatch2(lambda,v1,v2);$$=v3;}
3535 numparam_pop(p, $<node>4);
3536 dyna_pop(p, $<vars>1);
3537 }
3538 ;
3539
3540f_larglist : '(' f_args opt_bv_decl ')'
3541 {
3542#if 0
3543 $$ = $2;
3544 p->max_numparam = ORDINAL_PARAM;
3545#endif
3546 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
3547 }
3548 | f_args
3549 {
3550#if 0
3551 if (!args_info_empty_p($1->nd_ainfo))
3552 p->max_numparam = ORDINAL_PARAM;
3553#endif
3554 $$ = $1;
3555 }
3556 ;
3557
3558lambda_body : tLAMBEG compstmt '}'
3559 {
3560 token_info_pop(p, "}", &@3);
3561 $$ = $2;
3562 }
3563 | keyword_do_LAMBDA bodystmt k_end
3564 {
3565 $$ = $2;
3566 }
3567 ;
3568
3569do_block : k_do_block do_body k_end
3570 {
3571 $$ = $2;
3572#if 0
3573 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3574 nd_set_line($$, @1.end_pos.lineno);
3575#endif
3576 }
3577 ;
3578
3579block_call : command do_block
3580 {
3581#if 0
3582 if (nd_type($1) == NODE_YIELD) {
3583 compile_error(p, "block given to yield");
3584 }
3585 else {
3586 block_dup_check(p, $1->nd_args, $2);
3587 }
3588 $$ = method_add_block(p, $1, $2, &@$);
3589 fixpos($$, $1);
3590#endif
3591 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
3592 }
3593 | block_call call_op2 operation2 opt_paren_args
3594 {
3595#if 0
3596 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3597#endif
3598 {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;}
3599 }
3600 | block_call call_op2 operation2 opt_paren_args brace_block
3601 {
3602#if 0
3603 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3604#endif
3605 {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;}
3606 }
3607 | block_call call_op2 operation2 command_args do_block
3608 {
3609#if 0
3610 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3611#endif
3612 {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;}
3613 }
3614 ;
3615
3616method_call : fcall paren_args
3617 {
3618#if 0
3619 $$ = $1;
3620 $$->nd_args = $2;
3621 nd_set_last_loc($1, @2.end_pos);
3622#endif
3623 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(fcall,v1);v3=v2;v4=$2;v5=dispatch2(method_add_arg,v3,v4);$$=v5;}
3624 }
3625 | primary_value call_op operation2 opt_paren_args
3626 {
3627#if 0
3628 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3629 nd_set_line($$, @3.end_pos.lineno);
3630#endif
3631 {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;}
3632 }
3633 | primary_value tCOLON2 operation2 paren_args
3634 {
3635#if 0
3636 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
3637 nd_set_line($$, @3.end_pos.lineno);
3638#endif
3639 {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;}
3640 }
3641 | primary_value tCOLON2 operation3
3642 {
3643#if 0
3644 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
3645#endif
3646 {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(call,v1,v2,v3);$$=v4;}
3647 }
3648 | primary_value call_op paren_args
3649 {
3650#if 0
3651 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
3652 nd_set_line($$, @2.end_pos.lineno);
3653#endif
3654 {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;}
3655 }
3656 | primary_value tCOLON2 paren_args
3657 {
3658#if 0
3659 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
3660 nd_set_line($$, @2.end_pos.lineno);
3661#endif
3662 {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;}
3663 }
3664 | keyword_super paren_args
3665 {
3666#if 0
3667 $$ = NEW_SUPER($2, &@$);
3668#endif
3669 {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
3670 }
3671 | keyword_super
3672 {
3673#if 0
3674 $$ = NEW_ZSUPER(&@$);
3675#endif
3676 {VALUE v1;v1=dispatch0(zsuper);$$=v1;}
3677 }
3678 | primary_value '[' opt_call_args rbracket
3679 {
3680#if 0
3681 if ($1 && nd_type($1) == NODE_SELF)
3682 $$ = NEW_FCALL(tAREF, $3, &@$);
3683 else
3684 $$ = NEW_CALL($1, tAREF, $3, &@$);
3685 fixpos($$, $1);
3686#endif
3687 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref,v1,v2);$$=v3;}
3688 }
3689 ;
3690
3691brace_block : '{' brace_body '}'
3692 {
3693 $$ = $2;
3694#if 0
3695 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3696 nd_set_line($$, @1.end_pos.lineno);
3697#endif
3698 }
3699 | k_do do_body k_end
3700 {
3701 $$ = $2;
3702#if 0
3703 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3704 nd_set_line($$, @1.end_pos.lineno);
3705#endif
3706 }
3707 ;
3708
3709brace_body : {$<vars>$ = dyna_push(p);}
3710 {
3711 $<num>$ = p->max_numparam;
3712 p->max_numparam = 0;
3713 }
3714 {
3715 $<node>$ = numparam_push(p);
3716 }
3717 opt_block_param compstmt
3718 {
3719 int max_numparam = p->max_numparam;
3720 p->max_numparam = $<num>2;
3721 $4 = args_with_numbered(p, $4, max_numparam);
3722#if 0
3723 $$ = NEW_ITER($4, $5, &@$);
3724#endif
3725 {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(brace_block,v1,v2);$$=v3;}
3726 numparam_pop(p, $<node>3);
3727 dyna_pop(p, $<vars>1);
3728 }
3729 ;
3730
3731do_body : {$<vars>$ = dyna_push(p);}
3732 {
3733 $<num>$ = p->max_numparam;
3734 p->max_numparam = 0;
3735 }
3736 {
3737 $<node>$ = numparam_push(p);
3738 CMDARG_PUSH(0);
3739 }
3740 opt_block_param bodystmt
3741 {
3742 int max_numparam = p->max_numparam;
3743 p->max_numparam = $<num>2;
3744 $4 = args_with_numbered(p, $4, max_numparam);
3745#if 0
3746 $$ = NEW_ITER($4, $5, &@$);
3747#endif
3748 {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(do_block,v1,v2);$$=v3;}
3749 CMDARG_POP();
3750 numparam_pop(p, $<node>3);
3751 dyna_pop(p, $<vars>1);
3752 }
3753 ;
3754
3755case_args : arg_value
3756 {
3757#if 0
3758 check_literal_when(p, $1, &@1);
3759 $$ = NEW_LIST($1, &@$);
3760#endif
3761 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
3762 }
3763 | tSTAR arg_value
3764 {
3765#if 0
3766 $$ = NEW_SPLAT($2, &@$);
3767#endif
3768 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
3769 }
3770 | case_args ',' arg_value
3771 {
3772#if 0
3773 check_literal_when(p, $3, &@3);
3774 $$ = last_arg_append(p, $1, $3, &@$);
3775#endif
3776 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
3777 }
3778 | case_args ',' tSTAR arg_value
3779 {
3780#if 0
3781 $$ = rest_arg_append(p, $1, $4, &@$);
3782#endif
3783 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
3784 }
3785 ;
3786
3787case_body : k_when case_args then
3788 compstmt
3789 cases
3790 {
3791#if 0
3792 $$ = NEW_WHEN($2, $4, $5, &@$);
3793 fixpos($$, $2);
3794#endif
3795 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(when,v1,v2,v3);$$=v4;}
3796 }
3797 ;
3798
3799cases : opt_else
3800 | case_body
3801 ;
3802
3803p_case_body : keyword_in
3804 {
3805 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
3806 p->command_start = FALSE;
3807 $<num>$ = p->in_kwarg;
3808 p->in_kwarg = 1;
3809 }
3810 {$<tbl>$ = push_pvtbl(p);}
3811 {$<tbl>$ = push_pktbl(p);}
3812 p_top_expr then
3813 {pop_pktbl(p, $<tbl>4);}
3814 {pop_pvtbl(p, $<tbl>3);}
3815 {
3816 p->in_kwarg = !!$<num>2;
3817 }
3818 compstmt
3819 p_cases
3820 {
3821#if 0
3822 $$ = NEW_IN($5, $10, $11, &@$);
3823#endif
3824 {VALUE v1,v2,v3,v4;v1=$5;v2=$10;v3=escape_Qundef($11);v4=dispatch3(in,v1,v2,v3);$$=v4;}
3825 }
3826 ;
3827
3828p_cases : opt_else
3829 | p_case_body
3830 ;
3831
3832p_top_expr : p_top_expr_body
3833 | p_top_expr_body modifier_if expr_value
3834 {
3835#if 0
3836 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
3837 fixpos($$, $3);
3838#endif
3839 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
3840 }
3841 | p_top_expr_body modifier_unless expr_value
3842 {
3843#if 0
3844 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
3845 fixpos($$, $3);
3846#endif
3847 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
3848 }
3849 ;
3850
3851p_top_expr_body : p_expr
3852 | p_expr ','
3853 {
3854 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
3855 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
3856 }
3857 | p_expr ',' p_args
3858 {
3859 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
3860#if 0
3861 nd_set_first_loc($$, @1.beg_pos);
3862#endif
3863
3864 }
3865 | p_args_tail
3866 {
3867 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
3868 }
3869 | p_kwargs
3870 {
3871 $$ = new_hash_pattern(p, Qnone, $1, &@$);
3872 }
3873 ;
3874
3875p_expr : p_as
3876 ;
3877
3878p_as : p_expr tASSOC p_variable
3879 {
3880#if 0
3881 NODE *n = NEW_LIST($1, &@$);
3882 n = list_append(p, n, $3);
3883 $$ = new_hash(p, n, &@$);
3884#endif
3885 {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(id_assoc);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
3886 }
3887 | p_alt
3888 ;
3889
3890p_alt : p_alt '|' p_expr_basic
3891 {
3892#if 0
3893 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
3894#endif
3895 {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(idOr);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
3896 }
3897 | p_expr_basic
3898 ;
3899
3900p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
3901p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
3902
3903p_expr_basic : p_value
3904 | p_const p_lparen p_args rparen
3905 {
3906 pop_pktbl(p, $<tbl>2);
3907 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3908#if 0
3909 nd_set_first_loc($$, @1.beg_pos);
3910#endif
3911
3912 }
3913 | p_const p_lparen p_kwargs rparen
3914 {
3915 pop_pktbl(p, $<tbl>2);
3916 $$ = new_hash_pattern(p, $1, $3, &@$);
3917#if 0
3918 nd_set_first_loc($$, @1.beg_pos);
3919#endif
3920
3921 }
3922 | p_const '(' rparen
3923 {
3924 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3925 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3926 }
3927 | p_const p_lbracket p_args rbracket
3928 {
3929 pop_pktbl(p, $<tbl>2);
3930 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3931#if 0
3932 nd_set_first_loc($$, @1.beg_pos);
3933#endif
3934
3935 }
3936 | p_const p_lbracket p_kwargs rbracket
3937 {
3938 pop_pktbl(p, $<tbl>2);
3939 $$ = new_hash_pattern(p, $1, $3, &@$);
3940#if 0
3941 nd_set_first_loc($$, @1.beg_pos);
3942#endif
3943
3944 }
3945 | p_const '[' rbracket
3946 {
3947 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3948 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3949 }
3950 | tLBRACK {$<tbl>$ = push_pktbl(p);} p_args rbracket
3951 {
3952 pop_pktbl(p, $<tbl>2);
3953 $$ = new_array_pattern(p, Qnone, Qnone, $3, &@$);
3954 }
3955 | tLBRACK rbracket
3956 {
3957 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3958 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
3959 }
3960 | tLBRACE
3961 {
3962 $<tbl>$ = push_pktbl(p);
3963 $<num>1 = p->in_kwarg;
3964 p->in_kwarg = 0;
3965 }
3966 p_kwargs rbrace
3967 {
3968 pop_pktbl(p, $<tbl>2);
3969 p->in_kwarg = $<num>1;
3970 $$ = new_hash_pattern(p, Qnone, $3, &@$);
3971 }
3972 | tLBRACE rbrace
3973 {
3974 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
3975 $$ = new_hash_pattern(p, Qnone, $$, &@$);
3976 }
3977 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
3978 {
3979 pop_pktbl(p, $<tbl>2);
3980 $$ = $3;
3981 }
3982 ;
3983
3984p_args : p_expr
3985 {
3986#if 0
3987 NODE *pre_args = NEW_LIST($1, &@$);
3988 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
3989#endif
3990 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
3991
3992 }
3993 | p_args_head
3994 {
3995 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
3996 }
3997 | p_args_head p_arg
3998 {
3999#if 0
4000 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
4001#endif
4002 VALUE pre_args = rb_ary_concat($1, get_value($2));
4003 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4004
4005 }
4006 | p_args_head tSTAR tIDENTIFIER
4007 {
4008 $$ = new_array_pattern_tail(p, $1, 1, $3, Qnone, &@$);
4009 }
4010 | p_args_head tSTAR tIDENTIFIER ',' p_args_post
4011 {
4012 $$ = new_array_pattern_tail(p, $1, 1, $3, $5, &@$);
4013 }
4014 | p_args_head tSTAR
4015 {
4016 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4017 }
4018 | p_args_head tSTAR ',' p_args_post
4019 {
4020 $$ = new_array_pattern_tail(p, $1, 1, 0, $4, &@$);
4021 }
4022 | p_args_tail
4023 ;
4024
4025p_args_head : p_arg ','
4026 {
4027 $$ = $1;
4028 }
4029 | p_args_head p_arg ','
4030 {
4031#if 0
4032 $$ = list_concat($1, $2);
4033#endif
4034 $$=rb_ary_concat($1, get_value($2));
4035 }
4036 ;
4037
4038p_args_tail : tSTAR tIDENTIFIER
4039 {
4040 $$ = new_array_pattern_tail(p, Qnone, 1, $2, Qnone, &@$);
4041 }
4042 | tSTAR tIDENTIFIER ',' p_args_post
4043 {
4044 $$ = new_array_pattern_tail(p, Qnone, 1, $2, $4, &@$);
4045 }
4046 | tSTAR
4047 {
4048 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4049 }
4050 | tSTAR ',' p_args_post
4051 {
4052 $$ = new_array_pattern_tail(p, Qnone, 1, 0, $3, &@$);
4053 }
4054 ;
4055
4056p_args_post : p_arg
4057 | p_args_post ',' p_arg
4058 {
4059#if 0
4060 $$ = list_concat($1, $3);
4061#endif
4062 $$=rb_ary_concat($1, get_value($3));
4063 }
4064 ;
4065
4066p_arg : p_expr
4067 {
4068#if 0
4069 $$ = NEW_LIST($1, &@$);
4070#endif
4071 $$=rb_ary_new_from_args(1, get_value($1));
4072 }
4073 ;
4074
4075p_kwargs : p_kwarg ',' p_kwrest
4076 {
4077 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4078 }
4079 | p_kwarg
4080 {
4081 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4082 }
4083 | p_kwarg ','
4084 {
4085 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4086 }
4087 | p_kwrest
4088 {
4089 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4090 }
4091 | p_kwarg ',' p_kwnorest
4092 {
4093 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), ID2VAL(idNil), &@$);
4094 }
4095 | p_kwnorest
4096 {
4097 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), ID2VAL(idNil), &@$);
4098 }
4099 ;
4100
4101p_kwarg : p_kw
4102 {$$=rb_ary_new_from_args(1, $1);}
4103 | p_kwarg ',' p_kw
4104 {
4105#if 0
4106 $$ = list_concat($1, $3);
4107#endif
4108 $$=rb_ary_push($1, $3);
4109 }
4110 ;
4111
4112p_kw : p_kw_label p_expr
4113 {
4114 error_duplicate_pattern_key(p, get_id($1), &@1);
4115#if 0
4116 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), $2);
4117#endif
4118 $$=rb_ary_new_from_args(2, get_value($1), get_value($2));
4119 }
4120 | p_kw_label
4121 {
4122 error_duplicate_pattern_key(p, get_id($1), &@1);
4123 if ($1 && !is_local_id(get_id($1))) {
4124 yyerror1(&@1, "key must be valid as local variables");
4125 }
4126 error_duplicate_pattern_variable(p, get_id($1), &@1);
4127#if 0
4128 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4129#endif
4130 $$=rb_ary_new_from_args(2, get_value($1), Qnil);
4131 }
4132 ;
4133
4134p_kw_label : tLABEL
4135 | tSTRING_BEG string_contents tLABEL_END
4136 {
4137 YYLTYPE loc = code_loc_gen(&@1, &@3);
4138#if 0
4139 if (!$2 || nd_type($2) == NODE_STR) {
4140 NODE *node = dsym_node(p, $2, &loc);
4141 $$ = SYM2ID(node->nd_lit);
4142 }
4143#endif
4144 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4145 VALUE label = RNODE($2)->nd_cval;
4146 VALUE rval = RNODE($2)->nd_rval;
4147 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4148 RNODE($$)->nd_loc = loc;
4149 }
4150
4151 else {
4152 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4153 $$ = 0;
4154 }
4155 }
4156 ;
4157
4158p_kwrest : kwrest_mark tIDENTIFIER
4159 {
4160 $$ = $2;
4161 }
4162 | kwrest_mark
4163 {
4164 $$ = 0;
4165 }
4166 ;
4167
4168p_kwnorest : kwrest_mark keyword_nil
4169 {
4170 $$ = 0;
4171 }
4172 ;
4173
4174p_value : p_primitive
4175 | p_primitive tDOT2 p_primitive
4176 {
4177#if 0
4178 value_expr($1);
4179 value_expr($3);
4180 $$ = NEW_DOT2($1, $3, &@$);
4181#endif
4182 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
4183 }
4184 | p_primitive tDOT3 p_primitive
4185 {
4186#if 0
4187 value_expr($1);
4188 value_expr($3);
4189 $$ = NEW_DOT3($1, $3, &@$);
4190#endif
4191 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
4192 }
4193 | p_primitive tDOT2
4194 {
4195#if 0
4196 YYLTYPE loc;
4197 loc.beg_pos = @2.end_pos;
4198 loc.end_pos = @2.end_pos;
4199
4200 value_expr($1);
4201 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
4202#endif
4203 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
4204 }
4205 | p_primitive tDOT3
4206 {
4207#if 0
4208 YYLTYPE loc;
4209 loc.beg_pos = @2.end_pos;
4210 loc.end_pos = @2.end_pos;
4211
4212 value_expr($1);
4213 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
4214#endif
4215 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
4216 }
4217 | p_variable
4218 | p_var_ref
4219 | p_const
4220 | tBDOT2 p_primitive
4221 {
4222#if 0
4223 YYLTYPE loc;
4224 loc.beg_pos = @1.beg_pos;
4225 loc.end_pos = @1.beg_pos;
4226
4227 value_expr($2);
4228 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
4229#endif
4230 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
4231 }
4232 | tBDOT3 p_primitive
4233 {
4234#if 0
4235 YYLTYPE loc;
4236 loc.beg_pos = @1.beg_pos;
4237 loc.end_pos = @1.beg_pos;
4238
4239 value_expr($2);
4240 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
4241#endif
4242 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
4243 }
4244 ;
4245
4246p_primitive : literal
4247 | strings
4248 | xstring
4249 | regexp
4250 | words
4251 | qwords
4252 | symbols
4253 | qsymbols
4254 | keyword_variable
4255 {
4256#if 0
4257 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4258#endif
4259 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4260 }
4261 | tLAMBDA
4262 {
4263 token_info_push(p, "->", &@1);
4264 }
4265 lambda
4266 {
4267 $$ = $3;
4268#if 0
4269 nd_set_first_loc($$, @1.beg_pos);
4270#endif
4271 }
4272 ;
4273
4274p_variable : tIDENTIFIER
4275 {
4276#if 0
4277 error_duplicate_pattern_variable(p, $1, &@1);
4278 $$ = assignable(p, $1, 0, &@$);
4279#endif
4280 $$=assignable(p, var_field(p, $1));
4281 }
4282 ;
4283
4284p_var_ref : '^' tIDENTIFIER
4285 {
4286#if 0
4287 NODE *n = gettable(p, $2, &@$);
4288 if (!(nd_type(n) == NODE_LVAR || nd_type(n) == NODE_DVAR)) {
4289 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4290 }
4291 $$ = n;
4292#endif
4293 {VALUE v1,v2;v1=$2;v2=dispatch1(var_ref,v1);$$=v2;}
4294 }
4295 ;
4296
4297p_const : tCOLON3 cname
4298 {
4299#if 0
4300 $$ = NEW_COLON3($2, &@$);
4301#endif
4302 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
4303 }
4304 | p_const tCOLON2 cname
4305 {
4306#if 0
4307 $$ = NEW_COLON2($1, $3, &@$);
4308#endif
4309 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
4310 }
4311 | tCONSTANT
4312 {
4313#if 0
4314 $$ = gettable(p, $1, &@$);
4315#endif
4316 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4317 }
4318 ;
4319
4320opt_rescue : k_rescue exc_list exc_var then
4321 compstmt
4322 opt_rescue
4323 {
4324#if 0
4325 $$ = NEW_RESBODY($2,
4326 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), &@3), $5) : $5,
4327 $6, &@$);
4328 fixpos($$, $2?$2:$5);
4329#endif
4330 {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;}
4331 }
4332 | none
4333 ;
4334
4335exc_list : arg_value
4336 {
4337#if 0
4338 $$ = NEW_LIST($1, &@$);
4339#endif
4340 $$=rb_ary_new3(1, get_value($1));
4341 }
4342 | mrhs
4343 {
4344#if 0
4345 if (!($$ = splat_array($1))) $$ = $1;
4346#endif
4347 $$=$1;
4348 }
4349 | none
4350 ;
4351
4352exc_var : tASSOC lhs
4353 {
4354 $$ = $2;
4355 }
4356 | none
4357 ;
4358
4359opt_ensure : k_ensure compstmt
4360 {
4361#if 0
4362 $$ = $2;
4363#endif
4364 {VALUE v1,v2;v1=$2;v2=dispatch1(ensure,v1);$$=v2;}
4365 }
4366 | none
4367 ;
4368
4369literal : numeric
4370 | symbol
4371 ;
4372
4373strings : string
4374 {
4375#if 0
4376 NODE *node = $1;
4377 if (!node) {
4378 node = NEW_STR(STR_NEW0(), &@$);
4379 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
4380 }
4381 else {
4382 node = evstr2dstr(p, node);
4383 }
4384 $$ = node;
4385#endif
4386 $$=$1;
4387 }
4388 ;
4389
4390string : tCHAR
4391 | string1
4392 | string string1
4393 {
4394#if 0
4395 $$ = literal_concat(p, $1, $2, &@$);
4396#endif
4397 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_concat,v1,v2);$$=v3;}
4398 }
4399 ;
4400
4401string1 : tSTRING_BEG string_contents tSTRING_END
4402 {
4403#if 0
4404 $$ = heredoc_dedent(p, $2);
4405 if ($$) nd_set_loc($$, &@$);
4406#endif
4407 {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(string_literal,v1);$$=v2;}
4408 }
4409 ;
4410
4411xstring : tXSTRING_BEG xstring_contents tSTRING_END
4412 {
4413#if 0
4414 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4415#endif
4416 {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(xstring_literal,v1);$$=v2;}
4417 }
4418 ;
4419
4420regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4421 {
4422 $$ = new_regexp(p, $2, $3, &@$);
4423 }
4424 ;
4425
4426words : tWORDS_BEG ' ' word_list tSTRING_END
4427 {
4428#if 0
4429 $$ = make_list($3, &@$);
4430#endif
4431 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4432 }
4433 ;
4434
4435word_list : /* none */
4436 {
4437#if 0
4438 $$ = 0;
4439#endif
4440 {VALUE v1;v1=dispatch0(words_new);$$=v1;}
4441 }
4442 | word_list word ' '
4443 {
4444#if 0
4445 $$ = list_append(p, $1, evstr2dstr(p, $2));
4446#endif
4447 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(words_add,v1,v2);$$=v3;}
4448 }
4449 ;
4450
4451word : string_content
4452 {{VALUE v1,v2,v3,v4;v1=dispatch0(word_new);v2=v1;v3=$1;v4=dispatch2(word_add,v2,v3);$$=v4;}}
4453 | word string_content
4454 {
4455#if 0
4456 $$ = literal_concat(p, $1, $2, &@$);
4457#endif
4458 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(word_add,v1,v2);$$=v3;}
4459 }
4460 ;
4461
4462symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4463 {
4464#if 0
4465 $$ = make_list($3, &@$);
4466#endif
4467 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4468 }
4469 ;
4470
4471symbol_list : /* none */
4472 {
4473#if 0
4474 $$ = 0;
4475#endif
4476 {VALUE v1;v1=dispatch0(symbols_new);$$=v1;}
4477 }
4478 | symbol_list word ' '
4479 {
4480#if 0
4481 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4482#endif
4483 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(symbols_add,v1,v2);$$=v3;}
4484 }
4485 ;
4486
4487qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4488 {
4489#if 0
4490 $$ = make_list($3, &@$);
4491#endif
4492 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4493 }
4494 ;
4495
4496qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4497 {
4498#if 0
4499 $$ = make_list($3, &@$);
4500#endif
4501 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4502 }
4503 ;
4504
4505qword_list : /* none */
4506 {
4507#if 0
4508 $$ = 0;
4509#endif
4510 {VALUE v1;v1=dispatch0(qwords_new);$$=v1;}
4511 }
4512 | qword_list tSTRING_CONTENT ' '
4513 {
4514#if 0
4515 $$ = list_append(p, $1, $2);
4516#endif
4517 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qwords_add,v1,v2);$$=v3;}
4518 }
4519 ;
4520
4521qsym_list : /* none */
4522 {
4523#if 0
4524 $$ = 0;
4525#endif
4526 {VALUE v1;v1=dispatch0(qsymbols_new);$$=v1;}
4527 }
4528 | qsym_list tSTRING_CONTENT ' '
4529 {
4530#if 0
4531 $$ = symbol_append(p, $1, $2);
4532#endif
4533 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qsymbols_add,v1,v2);$$=v3;}
4534 }
4535 ;
4536
4537string_contents : /* none */
4538 {
4539#if 0
4540 $$ = 0;
4541#endif
4542 {VALUE v1;v1=dispatch0(string_content);$$=v1;}
4543#if 0
4544#endif
4545 $$ = ripper_new_yylval(p, 0, $$, 0);
4546
4547 }
4548 | string_contents string_content
4549 {
4550#if 0
4551 $$ = literal_concat(p, $1, $2, &@$);
4552#endif
4553 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_add,v1,v2);$$=v3;}
4554#if 0
4555#endif
4556 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
4557 !RNODE($1)->nd_cval) {
4558 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
4559 RNODE($1)->nd_rval = add_mark_object(p, $$);
4560 $$ = $1;
4561 }
4562
4563 }
4564 ;
4565
4566xstring_contents: /* none */
4567 {
4568#if 0
4569 $$ = 0;
4570#endif
4571 {VALUE v1;v1=dispatch0(xstring_new);$$=v1;}
4572 }
4573 | xstring_contents string_content
4574 {
4575#if 0
4576 $$ = literal_concat(p, $1, $2, &@$);
4577#endif
4578 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(xstring_add,v1,v2);$$=v3;}
4579 }
4580 ;
4581
4582regexp_contents: /* none */
4583 {
4584#if 0
4585 $$ = 0;
4586#endif
4587 {VALUE v1;v1=dispatch0(regexp_new);$$=v1;}
4588#if 0
4589#endif
4590 $$ = ripper_new_yylval(p, 0, $$, 0);
4591
4592 }
4593 | regexp_contents string_content
4594 {
4595#if 0
4596 NODE *head = $1, *tail = $2;
4597 if (!head) {
4598 $$ = tail;
4599 }
4600 else if (!tail) {
4601 $$ = head;
4602 }
4603 else {
4604 switch (nd_type(head)) {
4605 case NODE_STR:
4606 nd_set_type(head, NODE_DSTR);
4607 break;
4608 case NODE_DSTR:
4609 break;
4610 default:
4611 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
4612 break;
4613 }
4614 $$ = list_append(p, head, tail);
4615 }
4616#endif
4617 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4618 if (ripper_is_node_yylval(n1)) {
4619 s1 = RNODE(n1)->nd_cval;
4620 n1 = RNODE(n1)->nd_rval;
4621 }
4622 if (ripper_is_node_yylval(n2)) {
4623 s2 = RNODE(n2)->nd_cval;
4624 n2 = RNODE(n2)->nd_rval;
4625 }
4626 $$ = dispatch2(regexp_add, n1, n2);
4627 if (!s1 && s2) {
4628 $$ = ripper_new_yylval(p, 0, $$, s2);
4629 }
4630
4631 }
4632 ;
4633
4634string_content : tSTRING_CONTENT
4635 {$$=ripper_new_yylval(p, 0, get_value($1), $1);}
4636 | tSTRING_DVAR
4637 {
4638 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
4639 $<strterm>$ = p->lex.strterm;
4640 p->lex.strterm = 0;
4641 SET_LEX_STATE(EXPR_BEG);
4642 }
4643 string_dvar
4644 {
4645 p->lex.strterm = $<strterm>2;
4646#if 0
4647 $$ = NEW_EVSTR($3, &@$);
4648 nd_set_line($$, @3.end_pos.lineno);
4649#endif
4650 {VALUE v1,v2;v1=$3;v2=dispatch1(string_dvar,v1);$$=v2;}
4651 }
4652 | tSTRING_DBEG
4653 {
4654 CMDARG_PUSH(0);
4655 COND_PUSH(0);
4656 }
4657 {
4658 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
4659 $<strterm>$ = p->lex.strterm;
4660 p->lex.strterm = 0;
4661 }
4662 {
4663 $<num>$ = p->lex.state;
4664 SET_LEX_STATE(EXPR_BEG);
4665 }
4666 {
4667 $<num>$ = p->lex.brace_nest;
4668 p->lex.brace_nest = 0;
4669 }
4670 {
4671 $<num>$ = p->heredoc_indent;
4672 p->heredoc_indent = 0;
4673 }
4674 compstmt tSTRING_DEND
4675 {
4676 COND_POP();
4677 CMDARG_POP();
4678 p->lex.strterm = $<strterm>3;
4679 SET_LEX_STATE($<num>4);
4680 p->lex.brace_nest = $<num>5;
4681 p->heredoc_indent = $<num>6;
4682 p->heredoc_line_indent = -1;
4683#if 0
4684 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4685 $$ = new_evstr(p, $7, &@$);
4686#endif
4687 {VALUE v1,v2;v1=$7;v2=dispatch1(string_embexpr,v1);$$=v2;}
4688 }
4689 ;
4690
4691string_dvar : tGVAR
4692 {
4693#if 0
4694 $$ = NEW_GVAR($1, &@$);
4695#endif
4696 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4697 }
4698 | tIVAR
4699 {
4700#if 0
4701 $$ = NEW_IVAR($1, &@$);
4702#endif
4703 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4704 }
4705 | tCVAR
4706 {
4707#if 0
4708 $$ = NEW_CVAR($1, &@$);
4709#endif
4710 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4711 }
4712 | backref
4713 ;
4714
4715symbol : ssym
4716 | dsym
4717 ;
4718
4719ssym : tSYMBEG sym
4720 {
4721 SET_LEX_STATE(EXPR_END);
4722#if 0
4723 $$ = NEW_LIT(ID2SYM($2), &@$);
4724#endif
4725 {VALUE v1,v2,v3,v4;v1=$2;v2=dispatch1(symbol,v1);v3=v2;v4=dispatch1(symbol_literal,v3);$$=v4;}
4726 }
4727 ;
4728
4729sym : fname
4730 | tIVAR
4731 | tGVAR
4732 | tCVAR
4733 ;
4734
4735dsym : tSYMBEG string_contents tSTRING_END
4736 {
4737 SET_LEX_STATE(EXPR_END);
4738#if 0
4739 $$ = dsym_node(p, $2, &@$);
4740#endif
4741 {VALUE v1,v2;v1=$2;v2=dispatch1(dyna_symbol,v1);$$=v2;}
4742 }
4743 ;
4744
4745numeric : simple_numeric
4746 | tUMINUS_NUM simple_numeric %prec tLOWEST
4747 {
4748#if 0
4749 $$ = $2;
4750 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
4751#endif
4752 {VALUE v1,v2,v3;v1=ID2VAL(idUMinus);v2=$2;v3=dispatch2(unary,v1,v2);$$=v3;}
4753 }
4754 ;
4755
4756simple_numeric : tINTEGER
4757 | tFLOAT
4758 | tRATIONAL
4759 | tIMAGINARY
4760 ;
4761
4762user_variable : tIDENTIFIER
4763 | tIVAR
4764 | tGVAR
4765 | tCONSTANT
4766 | tCVAR
4767 ;
4768
4769keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
4770 | keyword_self {$$ = KWD2EID(self, $1);}
4771 | keyword_true {$$ = KWD2EID(true, $1);}
4772 | keyword_false {$$ = KWD2EID(false, $1);}
4773 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
4774 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
4775 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
4776 ;
4777
4778var_ref : user_variable
4779 {
4780#if 0
4781 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4782#endif
4783 if (id_is_var(p, get_id($1))) {
4784 $$ = dispatch1(var_ref, $1);
4785 }
4786 else {
4787 $$ = dispatch1(vcall, $1);
4788 }
4789
4790 }
4791 | keyword_variable
4792 {
4793#if 0
4794 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4795#endif
4796 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4797 }
4798 ;
4799
4800var_lhs : user_variable
4801 {
4802#if 0
4803 $$ = assignable(p, $1, 0, &@$);
4804#endif
4805 $$=assignable(p, var_field(p, $1));
4806 }
4807 | keyword_variable
4808 {
4809#if 0
4810 $$ = assignable(p, $1, 0, &@$);
4811#endif
4812 $$=assignable(p, var_field(p, $1));
4813 }
4814 ;
4815
4816backref : tNTH_REF
4817 | tBACK_REF
4818 ;
4819
4820superclass : '<'
4821 {
4822 SET_LEX_STATE(EXPR_BEG);
4823 p->command_start = TRUE;
4824 }
4825 expr_value term
4826 {
4827 $$ = $3;
4828 }
4829 | /* none */
4830 {
4831#if 0
4832 $$ = 0;
4833#endif
4834 $$=Qnil;
4835 }
4836 ;
4837
4838f_arglist : '(' f_args rparen
4839 {
4840#if 0
4841 $$ = $2;
4842#endif
4843 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
4844 SET_LEX_STATE(EXPR_BEG);
4845 p->command_start = TRUE;
4846 }
4847 | '(' f_arg ',' args_forward rparen
4848 {
4849 arg_var(p, idFWD_REST);
4850#if idFWD_KWREST
4851 arg_var(p, idFWD_KWREST);
4852#endif
4853 arg_var(p, idFWD_BLOCK);
4854#if 0
4855 $$ = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, &@4);
4856 $$ = new_args(p, $2, Qnone, idFWD_REST, Qnone, $$, &@4);
4857#endif
4858 {VALUE v1,v2;v1=params_new($2, Qnone, $4, Qnone, Qnone, Qnone, Qnone);v2=dispatch1(paren,v1);$$=v2;}
4859 SET_LEX_STATE(EXPR_BEG);
4860 p->command_start = TRUE;
4861 }
4862 | '(' args_forward rparen
4863 {
4864 arg_var(p, idFWD_REST);
4865#if idFWD_KWREST
4866 arg_var(p, idFWD_KWREST);
4867#endif
4868 arg_var(p, idFWD_BLOCK);
4869#if 0
4870 $$ = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, &@2);
4871 $$ = new_args(p, Qnone, Qnone, idFWD_REST, Qnone, $$, &@2);
4872#endif
4873 {VALUE v1,v2;v1=params_new(Qnone, Qnone, $2, Qnone, Qnone, Qnone, Qnone);v2=dispatch1(paren,v1);$$=v2;}
4874 SET_LEX_STATE(EXPR_BEG);
4875 p->command_start = TRUE;
4876 }
4877 | {
4878 $<num>$ = p->in_kwarg;
4879 p->in_kwarg = 1;
4880 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
4881 }
4882 f_args term
4883 {
4884 p->in_kwarg = !!$<num>1;
4885 $$ = $2;
4886 SET_LEX_STATE(EXPR_BEG);
4887 p->command_start = TRUE;
4888 }
4889 ;
4890
4891args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
4892 {
4893 $$ = new_args_tail(p, $1, $3, $4, &@3);
4894 }
4895 | f_kwarg opt_f_block_arg
4896 {
4897 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
4898 }
4899 | f_kwrest opt_f_block_arg
4900 {
4901 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
4902 }
4903 | f_no_kwarg opt_f_block_arg
4904 {
4905 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
4906 }
4907 | f_block_arg
4908 {
4909 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
4910 }
4911 ;
4912
4913opt_args_tail : ',' args_tail
4914 {
4915 $$ = $2;
4916 }
4917 | /* none */
4918 {
4919 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4920 }
4921 ;
4922
4923f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
4924 {
4925 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
4926 }
4927 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4928 {
4929 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4930 }
4931 | f_arg ',' f_optarg opt_args_tail
4932 {
4933 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
4934 }
4935 | f_arg ',' f_optarg ',' f_arg opt_args_tail
4936 {
4937 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4938 }
4939 | f_arg ',' f_rest_arg opt_args_tail
4940 {
4941 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4942 }
4943 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
4944 {
4945 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4946 }
4947 | f_arg opt_args_tail
4948 {
4949 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4950 }
4951 | f_optarg ',' f_rest_arg opt_args_tail
4952 {
4953 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4954 }
4955 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4956 {
4957 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4958 }
4959 | f_optarg opt_args_tail
4960 {
4961 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4962 }
4963 | f_optarg ',' f_arg opt_args_tail
4964 {
4965 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4966 }
4967 | f_rest_arg opt_args_tail
4968 {
4969 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4970 }
4971 | f_rest_arg ',' f_arg opt_args_tail
4972 {
4973 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4974 }
4975 | args_tail
4976 {
4977 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4978 }
4979 | /* none */
4980 {
4981 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4982 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
4983 }
4984 ;
4985
4986args_forward : tBDOT3
4987 {
4988#if 0
4989 $$ = idDot3;
4990#endif
4991 {VALUE v1;v1=dispatch0(args_forward);$$=v1;}
4992 }
4993 ;
4994
4995f_bad_arg : tCONSTANT
4996 {
4997#if 0
4998 yyerror1(&@1, "formal argument cannot be a constant");
4999 $$ = 0;
5000#endif
5001 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
5002 }
5003 | tIVAR
5004 {
5005#if 0
5006 yyerror1(&@1, "formal argument cannot be an instance variable");
5007 $$ = 0;
5008#endif
5009 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
5010 }
5011 | tGVAR
5012 {
5013#if 0
5014 yyerror1(&@1, "formal argument cannot be a global variable");
5015 $$ = 0;
5016#endif
5017 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
5018 }
5019 | tCVAR
5020 {
5021#if 0
5022 yyerror1(&@1, "formal argument cannot be a class variable");
5023 $$ = 0;
5024#endif
5025 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
5026 }
5027 ;
5028
5029f_norm_arg : f_bad_arg
5030 | tIDENTIFIER
5031 {
5032 formal_argument(p, get_id($1));
5033 p->max_numparam = ORDINAL_PARAM;
5034 $$ = $1;
5035 }
5036 ;
5037
5038f_arg_asgn : f_norm_arg
5039 {
5040 ID id = get_id($1);
5041 arg_var(p, id);
5042 p->cur_arg = id;
5043 $$ = $1;
5044 }
5045 ;
5046
5047f_arg_item : f_arg_asgn
5048 {
5049 p->cur_arg = 0;
5050#if 0
5051 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5052#endif
5053 $$=get_value($1);
5054 }
5055 | tLPAREN f_margs rparen
5056 {
5057#if 0
5058 ID tid = internal_id(p);
5059 YYLTYPE loc;
5060 loc.beg_pos = @2.beg_pos;
5061 loc.end_pos = @2.beg_pos;
5062 arg_var(p, tid);
5063 if (dyna_in_block(p)) {
5064 $2->nd_value = NEW_DVAR(tid, &loc);
5065 }
5066 else {
5067 $2->nd_value = NEW_LVAR(tid, &loc);
5068 }
5069 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5070 $$->nd_next = $2;
5071#endif
5072 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
5073 }
5074 ;
5075
5076f_arg : f_arg_item
5077 {$$=rb_ary_new3(1, get_value($1));}
5078 | f_arg ',' f_arg_item
5079 {
5080#if 0
5081 $$ = $1;
5082 $$->nd_plen++;
5083 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5084 rb_discard_node(p, $3);
5085#endif
5086 $$=rb_ary_push($1, get_value($3));
5087 }
5088 ;
5089
5090
5091f_label : tLABEL
5092 {
5093 ID id = get_id($1);
5094 arg_var(p, formal_argument(p, id));
5095 p->cur_arg = id;
5096 p->max_numparam = ORDINAL_PARAM;
5097 $$ = $1;
5098 }
5099 ;
5100
5101f_kw : f_label arg_value
5102 {
5103 p->cur_arg = 0;
5104#if 0
5105 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5106#endif
5107 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5108 }
5109 | f_label
5110 {
5111 p->cur_arg = 0;
5112#if 0
5113 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5114#endif
5115 $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5116 }
5117 ;
5118
5119f_block_kw : f_label primary_value
5120 {
5121#if 0
5122 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5123#endif
5124 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5125 }
5126 | f_label
5127 {
5128#if 0
5129 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5130#endif
5131 $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5132 }
5133 ;
5134
5135f_block_kwarg : f_block_kw
5136 {
5137#if 0
5138 $$ = $1;
5139#endif
5140 $$=rb_ary_new3(1, get_value($1));
5141 }
5142 | f_block_kwarg ',' f_block_kw
5143 {
5144#if 0
5145 $$ = kwd_append($1, $3);
5146#endif
5147 $$=rb_ary_push($1, get_value($3));
5148 }
5149 ;
5150
5151
5152f_kwarg : f_kw
5153 {
5154#if 0
5155 $$ = $1;
5156#endif
5157 $$=rb_ary_new3(1, get_value($1));
5158 }
5159 | f_kwarg ',' f_kw
5160 {
5161#if 0
5162 $$ = kwd_append($1, $3);
5163#endif
5164 $$=rb_ary_push($1, get_value($3));
5165 }
5166 ;
5167
5168kwrest_mark : tPOW
5169 | tDSTAR
5170 ;
5171
5172f_no_kwarg : kwrest_mark keyword_nil
5173 {
5174#if 0
5175#endif
5176 {VALUE v1,v2;v1=Qnil;v2=dispatch1(nokw_param,v1);$$=v2;}
5177 }
5178 ;
5179
5180f_kwrest : kwrest_mark tIDENTIFIER
5181 {
5182 arg_var(p, shadowing_lvar(p, get_id($2)));
5183#if 0
5184 $$ = $2;
5185#endif
5186 {VALUE v1,v2;v1=$2;v2=dispatch1(kwrest_param,v1);$$=v2;}
5187 }
5188 | kwrest_mark
5189 {
5190#if 0
5191 $$ = internal_id(p);
5192 arg_var(p, $$);
5193#endif
5194 {VALUE v1,v2;v1=Qnil;v2=dispatch1(kwrest_param,v1);$$=v2;}
5195 }
5196 ;
5197
5198f_opt : f_arg_asgn '=' arg_value
5199 {
5200 p->cur_arg = 0;
5201#if 0
5202 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5203#endif
5204 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5205 }
5206 ;
5207
5208f_block_opt : f_arg_asgn '=' primary_value
5209 {
5210 p->cur_arg = 0;
5211#if 0
5212 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5213#endif
5214 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5215 }
5216 ;
5217
5218f_block_optarg : f_block_opt
5219 {
5220#if 0
5221 $$ = $1;
5222#endif
5223 $$=rb_ary_new3(1, get_value($1));
5224 }
5225 | f_block_optarg ',' f_block_opt
5226 {
5227#if 0
5228 $$ = opt_arg_append($1, $3);
5229#endif
5230 $$=rb_ary_push($1, get_value($3));
5231 }
5232 ;
5233
5234f_optarg : f_opt
5235 {
5236#if 0
5237 $$ = $1;
5238#endif
5239 $$=rb_ary_new3(1, get_value($1));
5240 }
5241 | f_optarg ',' f_opt
5242 {
5243#if 0
5244 $$ = opt_arg_append($1, $3);
5245#endif
5246 $$=rb_ary_push($1, get_value($3));
5247 }
5248 ;
5249
5250restarg_mark : '*'
5251 | tSTAR
5252 ;
5253
5254f_rest_arg : restarg_mark tIDENTIFIER
5255 {
5256 arg_var(p, shadowing_lvar(p, get_id($2)));
5257#if 0
5258 $$ = $2;
5259#endif
5260 {VALUE v1,v2;v1=$2;v2=dispatch1(rest_param,v1);$$=v2;}
5261 }
5262 | restarg_mark
5263 {
5264#if 0
5265 $$ = internal_id(p);
5266 arg_var(p, $$);
5267#endif
5268 {VALUE v1,v2;v1=Qnil;v2=dispatch1(rest_param,v1);$$=v2;}
5269 }
5270 ;
5271
5272blkarg_mark : '&'
5273 | tAMPER
5274 ;
5275
5276f_block_arg : blkarg_mark tIDENTIFIER
5277 {
5278 arg_var(p, shadowing_lvar(p, get_id($2)));
5279#if 0
5280 $$ = $2;
5281#endif
5282 {VALUE v1,v2;v1=$2;v2=dispatch1(blockarg,v1);$$=v2;}
5283 }
5284 ;
5285
5286opt_f_block_arg : ',' f_block_arg
5287 {
5288 $$ = $2;
5289 }
5290 | none
5291 {
5292 $$ = Qnull;
5293 }
5294 ;
5295
5296singleton : var_ref
5297 {
5298 value_expr($1);
5299 $$ = $1;
5300 }
5301 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5302 {
5303#if 0
5304 switch (nd_type($3)) {
5305 case NODE_STR:
5306 case NODE_DSTR:
5307 case NODE_XSTR:
5308 case NODE_DXSTR:
5309 case NODE_DREGX:
5310 case NODE_LIT:
5311 case NODE_LIST:
5312 case NODE_ZLIST:
5313 yyerror1(&@3, "can't define singleton method for literals");
5314 break;
5315 default:
5316 value_expr($3);
5317 break;
5318 }
5319 $$ = $3;
5320#endif
5321 {VALUE v1,v2;v1=$3;v2=dispatch1(paren,v1);$$=v2;}
5322 }
5323 ;
5324
5325assoc_list : none
5326 | assocs trailer
5327 {
5328#if 0
5329 $$ = $1;
5330#endif
5331 {VALUE v1,v2;v1=$1;v2=dispatch1(assoclist_from_args,v1);$$=v2;}
5332 }
5333 ;
5334
5335assocs : assoc
5336 {$$=rb_ary_new3(1, get_value($1));}
5337 | assocs ',' assoc
5338 {
5339#if 0
5340 NODE *assocs = $1;
5341 NODE *tail = $3;
5342 if (!assocs) {
5343 assocs = tail;
5344 }
5345 else if (tail) {
5346 if (assocs->nd_head &&
5347 !tail->nd_head && nd_type(tail->nd_next) == NODE_LIST &&
5348 nd_type(tail->nd_next->nd_head) == NODE_HASH) {
5349 /* DSTAR */
5350 tail = tail->nd_next->nd_head->nd_head;
5351 }
5352 assocs = list_concat(assocs, tail);
5353 }
5354 $$ = assocs;
5355#endif
5356 $$=rb_ary_push($1, get_value($3));
5357 }
5358 ;
5359
5360assoc : arg_value tASSOC arg_value
5361 {
5362#if 0
5363 if (nd_type($1) == NODE_STR) {
5364 nd_set_type($1, NODE_LIT);
5365 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
5366 }
5367 $$ = list_append(p, NEW_LIST($1, &@$), $3);
5368#endif
5369 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5370 }
5371 | tLABEL arg_value
5372 {
5373#if 0
5374 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5375#endif
5376 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5377 }
5378 | tSTRING_BEG string_contents tLABEL_END arg_value
5379 {
5380#if 0
5381 YYLTYPE loc = code_loc_gen(&@1, &@3);
5382 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
5383#endif
5384 {VALUE v1,v2,v3,v4,v5;v1=$2;v2=dispatch1(dyna_symbol,v1);v3=v2;v4=$4;v5=dispatch2(assoc_new,v3,v4);$$=v5;}
5385 }
5386 | tDSTAR arg_value
5387 {
5388#if 0
5389 if (nd_type($2) == NODE_HASH &&
5390 !($2->nd_head && $2->nd_head->nd_alen)) {
5391 static VALUE empty_hash;
5392 if (!empty_hash) {
5393 empty_hash = rb_obj_freeze(rb_hash_new());
5394 rb_gc_register_mark_object(empty_hash);
5395 }
5396 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
5397 }
5398 else
5399 $$ = list_append(p, NEW_LIST(0, &@$), $2);
5400#endif
5401 {VALUE v1,v2;v1=$2;v2=dispatch1(assoc_splat,v1);$$=v2;}
5402 }
5403 ;
5404
5405operation : tIDENTIFIER
5406 | tCONSTANT
5407 | tFID
5408 ;
5409
5410operation2 : tIDENTIFIER
5411 | tCONSTANT
5412 | tFID
5413 | op
5414 ;
5415
5416operation3 : tIDENTIFIER
5417 | tFID
5418 | op
5419 ;
5420
5421dot_or_colon : '.'
5422 | tCOLON2
5423 ;
5424
5425call_op : '.'
5426 | tANDDOT
5427 ;
5428
5429call_op2 : call_op
5430 | tCOLON2
5431 ;
5432
5433opt_terms : /* none */
5434 | terms
5435 ;
5436
5437opt_nl : /* none */
5438 | '\n'
5439 ;
5440
5441rparen : opt_nl ')'
5442 ;
5443
5444rbracket : opt_nl ']'
5445 ;
5446
5447rbrace : opt_nl '}'
5448 ;
5449
5450trailer : /* none */
5451 | '\n'
5452 | ','
5453 ;
5454
5455term : ';' {yyerrok;token_flush(p);}
5456 | '\n' {token_flush(p);}
5457 ;
5458
5459terms : term
5460 | terms ';' {yyerrok;}
5461 ;
5462
5463none : /* none */
5464 {
5465 $$ = Qnull;
5466 }
5467 ;
5468%%
5469# undef p
5470# undef yylex
5471# undef yylval
5472# define yylval (*p->lval)
5473
5474static int regx_options(struct parser_params*);
5475static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5476static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5477static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5478static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
5479
5480#ifndef RIPPER
5481# define set_yylval_node(x) { \
5482 YYLTYPE _cur_loc; \
5483 rb_parser_set_location(p, &_cur_loc); \
5484 yylval.node = (x); \
5485}
5486# define set_yylval_str(x) \
5487do { \
5488 set_yylval_node(NEW_STR(x, &_cur_loc)); \
5489 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5490} while(0)
5491# define set_yylval_literal(x) \
5492do { \
5493 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
5494 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5495} while(0)
5496# define set_yylval_num(x) (yylval.num = (x))
5497# define set_yylval_id(x) (yylval.id = (x))
5498# define set_yylval_name(x) (yylval.id = (x))
5499# define yylval_id() (yylval.id)
5500#else
5501static inline VALUE
5502ripper_yylval_id(struct parser_params *p, ID x)
5503{
5504 return ripper_new_yylval(p, x, ID2SYM(x), 0);
5505}
5506# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
5507# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
5508# define set_yylval_id(x) (void)(x)
5509# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
5510# define set_yylval_literal(x) add_mark_object(p, (x))
5511# define set_yylval_node(x) (void)(x)
5512# define yylval_id() yylval.id
5513# define _cur_loc NULL_LOC /* dummy */
5514#endif
5515
5516#define set_yylval_noname() set_yylval_id(keyword_nil)
5517
5518#ifndef RIPPER
5519#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
5520#define dispatch_scan_event(p, t) ((void)0)
5521#define dispatch_delayed_token(p, t) ((void)0)
5522#define has_delayed_token(p) (0)
5523#else
5524#define literal_flush(p, ptr) ((void)(ptr))
5525
5526#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5527
5528static inline VALUE
5529intern_sym(const char *name)
5530{
5531 ID id = rb_intern_const(name);
5532 return ID2SYM(id);
5533}
5534
5535static int
5536ripper_has_scan_event(struct parser_params *p)
5537{
5538 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
5539 return p->lex.pcur > p->lex.ptok;
5540}
5541
5542static VALUE
5543ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
5544{
5545 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
5546 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
5547 token_flush(p);
5548 return rval;
5549}
5550
5551static void
5552ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
5553{
5554 if (!ripper_has_scan_event(p)) return;
5555 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
5556}
5557#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
5558
5559static void
5560ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
5561{
5562 int saved_line = p->ruby_sourceline;
5563 const char *saved_tokp = p->lex.ptok;
5564
5565 if (NIL_P(p->delayed.token)) return;
5566 p->ruby_sourceline = p->delayed.line;
5567 p->lex.ptok = p->lex.pbeg + p->delayed.col;
5568 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
5569 p->delayed.token = Qnil;
5570 p->ruby_sourceline = saved_line;
5571 p->lex.ptok = saved_tokp;
5572}
5573#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
5574#define has_delayed_token(p) (!NIL_P(p->delayed.token))
5575#endif /* RIPPER */
5576
5577#include "ruby/regex.h"
5578#include "ruby/util.h"
5579
5580static inline int
5581is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
5582{
5583 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
5584}
5585
5586static inline int
5587parser_is_identchar(struct parser_params *p)
5588{
5589 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5590}
5591
5592static inline int
5593parser_isascii(struct parser_params *p)
5594{
5595 return ISASCII(*(p->lex.pcur-1));
5596}
5597
5598static void
5599token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
5600{
5601 int column = 1, nonspc = 0, i;
5602 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
5603 if (*ptr == '\t') {
5604 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5605 }
5606 column++;
5607 if (*ptr != ' ' && *ptr != '\t') {
5608 nonspc = 1;
5609 }
5610 }
5611
5612 ptinfo->beg = loc->beg_pos;
5613 ptinfo->indent = column;
5614 ptinfo->nonspc = nonspc;
5615}
5616
5617static void
5618token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5619{
5620 token_info *ptinfo;
5621
5622 if (!p->token_info_enabled) return;
5623 ptinfo = ALLOC(token_info);
5624 ptinfo->token = token;
5625 ptinfo->next = p->token_info;
5626 token_info_setup(ptinfo, p->lex.pbeg, loc);
5627
5628 p->token_info = ptinfo;
5629}
5630
5631static void
5632token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5633{
5634 token_info *ptinfo_beg = p->token_info;
5635
5636 if (!ptinfo_beg) return;
5637 p->token_info = ptinfo_beg->next;
5638
5639 /* indentation check of matched keywords (begin..end, if..end, etc.) */
5640 token_info_warn(p, token, ptinfo_beg, 1, loc);
5641 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5642}
5643
5644static void
5645token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
5646{
5647 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
5648 if (!p->token_info_enabled) return;
5649 if (!ptinfo_beg) return;
5650 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
5651 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
5652 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
5653 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
5654 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
5655 rb_warn3L(ptinfo_end->beg.lineno,
5656 "mismatched indentations at '%s' with '%s' at %d",
5657 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
5658}
5659
5660static int
5661parser_precise_mbclen(struct parser_params *p, const char *ptr)
5662{
5663 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
5664 if (!MBCLEN_CHARFOUND_P(len)) {
5665 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
5666 return -1;
5667 }
5668 return len;
5669}
5670
5671#ifndef RIPPER
5672static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
5673
5674static inline void
5675parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5676{
5677 VALUE str;
5678 int lineno = p->ruby_sourceline;
5679 if (!yylloc) {
5680 return;
5681 }
5682 else if (yylloc->beg_pos.lineno == lineno) {
5683 str = p->lex.lastline;
5684 }
5685 else {
5686 return;
5687 }
5688 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
5689}
5690
5691static int
5692parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5693{
5694 YYLTYPE current;
5695
5696 if (!yylloc) {
5697 yylloc = RUBY_SET_YYLLOC(current);
5698 }
5699 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
5700 p->ruby_sourceline != yylloc->end_pos.lineno) ||
5701 (yylloc->beg_pos.lineno == yylloc->end_pos.lineno &&
5702 yylloc->beg_pos.column == yylloc->end_pos.column)) {
5703 yylloc = 0;
5704 }
5705 compile_error(p, "%s", msg);
5706 parser_show_error_line(p, yylloc);
5707 return 0;
5708}
5709
5710static void
5711ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
5712{
5713 VALUE mesg;
5714 const int max_line_margin = 30;
5715 const char *ptr, *ptr_end, *pt, *pb;
5716 const char *pre = "", *post = "", *pend;
5717 const char *code = "", *caret = "";
5718 const char *lim;
5719 const char *const pbeg = RSTRING_PTR(str);
5720 char *buf;
5721 long len;
5722 int i;
5723
5724 if (!yylloc) return;
5725 pend = RSTRING_END(str);
5726 if (pend > pbeg && pend[-1] == '\n') {
5727 if (--pend > pbeg && pend[-1] == '\r') --pend;
5728 }
5729
5730 pt = pend;
5731 if (lineno == yylloc->end_pos.lineno &&
5732 (pend - pbeg) > yylloc->end_pos.column) {
5733 pt = pbeg + yylloc->end_pos.column;
5734 }
5735
5736 ptr = ptr_end = pt;
5737 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
5738 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
5739
5740 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
5741 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
5742
5743 len = ptr_end - ptr;
5744 if (len > 4) {
5745 if (ptr > pbeg) {
5746 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
5747 if (ptr > pbeg) pre = "...";
5748 }
5749 if (ptr_end < pend) {
5750 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
5751 if (ptr_end < pend) post = "...";
5752 }
5753 }
5754 pb = pbeg;
5755 if (lineno == yylloc->beg_pos.lineno) {
5756 pb += yylloc->beg_pos.column;
5757 if (pb > pt) pb = pt;
5758 }
5759 if (pb < ptr) pb = ptr;
5760 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
5761 return;
5762 }
5763 if (RTEST(errbuf)) {
5764 mesg = rb_attr_get(errbuf, idMesg);
5765 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
5766 rb_str_cat_cstr(mesg, "\n");
5767 }
5768 else {
5769 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
5770 }
5771 if (!errbuf && rb_stderr_tty_p()) {
5772#define CSI_BEGIN "\033["
5773#define CSI_SGR "m"
5774 rb_str_catf(mesg,
5775 CSI_BEGIN""CSI_SGR"%s" /* pre */
5776 CSI_BEGIN"1"CSI_SGR"%.*s"
5777 CSI_BEGIN"1;4"CSI_SGR"%.*s"
5778 CSI_BEGIN";1"CSI_SGR"%.*s"
5779 CSI_BEGIN""CSI_SGR"%s" /* post */
5780 "\n",
5781 pre,
5782 (int)(pb - ptr), ptr,
5783 (int)(pt - pb), pb,
5784 (int)(ptr_end - pt), pt,
5785 post);
5786 }
5787 else {
5788 char *p2;
5789
5790 len = ptr_end - ptr;
5791 lim = pt < pend ? pt : pend;
5792 i = (int)(lim - ptr);
5793 buf = ALLOCA_N(char, i+2);
5794 code = ptr;
5795 caret = p2 = buf;
5796 if (ptr <= pb) {
5797 while (ptr < pb) {
5798 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
5799 }
5800 *p2++ = '^';
5801 ptr++;
5802 }
5803 if (lim > ptr) {
5804 memset(p2, '~', (lim - ptr));
5805 p2 += (lim - ptr);
5806 }
5807 *p2 = '\0';
5808 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
5809 pre, (int)len, code, post,
5810 pre, caret);
5811 }
5812 if (!errbuf) rb_write_error_str(mesg);
5813}
5814#else
5815static int
5816parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5817{
5818 const char *pcur = 0, *ptok = 0;
5819 if (yylloc &&
5820 p->ruby_sourceline == yylloc->beg_pos.lineno &&
5821 p->ruby_sourceline == yylloc->end_pos.lineno) {
5822 pcur = p->lex.pcur;
5823 ptok = p->lex.ptok;
5824 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
5825 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
5826 }
5827 dispatch1(parse_error, STR_NEW2(msg));
5828 ripper_error(p);
5829 if (pcur) {
5830 p->lex.ptok = ptok;
5831 p->lex.pcur = pcur;
5832 }
5833 return 0;
5834}
5835
5836static inline void
5837parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5838{
5839}
5840#endif /* !RIPPER */
5841
5842#ifndef RIPPER
5843static int
5844vtable_size(const struct vtable *tbl)
5845{
5846 if (!DVARS_TERMINAL_P(tbl)) {
5847 return tbl->pos;
5848 }
5849 else {
5850 return 0;
5851 }
5852}
5853#endif
5854
5855static struct vtable *
5856vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
5857{
5858 struct vtable *tbl = ALLOC(struct vtable);
5859 tbl->pos = 0;
5860 tbl->capa = 8;
5861 tbl->tbl = ALLOC_N(ID, tbl->capa);
5862 tbl->prev = prev;
5863#ifndef RIPPER
5864 if (p->debug) {
5865 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
5866 }
5867#endif
5868 return tbl;
5869}
5870#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
5871
5872static void
5873vtable_free_gen(struct parser_params *p, int line, const char *name,
5874 struct vtable *tbl)
5875{
5876#ifndef RIPPER
5877 if (p->debug) {
5878 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
5879 }
5880#endif
5881 if (!DVARS_TERMINAL_P(tbl)) {
5882 if (tbl->tbl) {
5883 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
5884 }
5885 ruby_sized_xfree(tbl, sizeof(tbl));
5886 }
5887}
5888#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
5889
5890static void
5891vtable_add_gen(struct parser_params *p, int line, const char *name,
5892 struct vtable *tbl, ID id)
5893{
5894#ifndef RIPPER
5895 if (p->debug) {
5896 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
5897 line, name, (void *)tbl, rb_id2name(id));
5898 }
5899#endif
5900 if (DVARS_TERMINAL_P(tbl)) {
5901 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
5902 return;
5903 }
5904 if (tbl->pos == tbl->capa) {
5905 tbl->capa = tbl->capa * 2;
5906 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
5907 }
5908 tbl->tbl[tbl->pos++] = id;
5909}
5910#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
5911
5912#ifndef RIPPER
5913static void
5914vtable_pop_gen(struct parser_params *p, int line, const char *name,
5915 struct vtable *tbl, int n)
5916{
5917 if (p->debug) {
5918 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
5919 line, name, (void *)tbl, n);
5920 }
5921 if (tbl->pos < n) {
5922 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
5923 return;
5924 }
5925 tbl->pos -= n;
5926}
5927#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
5928#endif
5929
5930static int
5931vtable_included(const struct vtable * tbl, ID id)
5932{
5933 int i;
5934
5935 if (!DVARS_TERMINAL_P(tbl)) {
5936 for (i = 0; i < tbl->pos; i++) {
5937 if (tbl->tbl[i] == id) {
5938 return i+1;
5939 }
5940 }
5941 }
5942 return 0;
5943}
5944
5945static void parser_prepare(struct parser_params *p);
5946
5947#ifndef RIPPER
5948static NODE *parser_append_options(struct parser_params *p, NODE *node);
5949
5950static VALUE
5951debug_lines(VALUE fname)
5952{
5953 ID script_lines;
5954 CONST_ID(script_lines, "SCRIPT_LINES__");
5955 if (rb_const_defined_at(rb_cObject, script_lines)) {
5956 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
5957 if (RB_TYPE_P(hash, T_HASH)) {
5958 VALUE lines = rb_ary_new();
5959 rb_hash_aset(hash, fname, lines);
5960 return lines;
5961 }
5962 }
5963 return 0;
5964}
5965
5966static int
5967e_option_supplied(struct parser_params *p)
5968{
5969 return strcmp(p->ruby_sourcefile, "-e") == 0;
5970}
5971
5972static VALUE
5973yycompile0(VALUE arg)
5974{
5975 int n;
5976 NODE *tree;
5977 struct parser_params *p = (struct parser_params *)arg;
5978 VALUE cov = Qfalse;
5979
5980 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
5981 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
5982 if (p->debug_lines && p->ruby_sourceline > 0) {
5983 VALUE str = STR_NEW0();
5984 n = p->ruby_sourceline;
5985 do {
5986 rb_ary_push(p->debug_lines, str);
5987 } while (--n);
5988 }
5989
5990 if (!e_option_supplied(p)) {
5991 cov = Qtrue;
5992 }
5993 }
5994
5995 parser_prepare(p);
5996#define RUBY_DTRACE_PARSE_HOOK(name) \
5997 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
5998 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
5999 }
6000 RUBY_DTRACE_PARSE_HOOK(BEGIN);
6001 n = yyparse(p);
6002 RUBY_DTRACE_PARSE_HOOK(END);
6003 p->debug_lines = 0;
6004
6005 p->lex.strterm = 0;
6006 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
6007 p->lex.prevline = p->lex.lastline = p->lex.nextline = 0;
6008 if (n || p->error_p) {
6009 VALUE mesg = p->error_buffer;
6010 if (!mesg) {
6011 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
6012 }
6013 rb_set_errinfo(mesg);
6014 return FALSE;
6015 }
6016 tree = p->eval_tree;
6017 if (!tree) {
6018 tree = NEW_NIL(&NULL_LOC);
6019 }
6020 else {
6021 VALUE opt = p->compile_option;
6022 NODE *prelude;
6023 NODE *body = parser_append_options(p, tree->nd_body);
6024 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
6025 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
6026 prelude = block_append(p, p->eval_tree_begin, body);
6027 tree->nd_body = prelude;
6028 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
6029 }
6030 p->ast->body.root = tree;
6031 p->ast->body.line_count = p->line_count;
6032 return TRUE;
6033}
6034
6035static rb_ast_t *
6036yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
6037{
6038 rb_ast_t *ast;
6039 if (NIL_P(fname)) {
6040 p->ruby_sourcefile_string = Qnil;
6041 p->ruby_sourcefile = "(none)";
6042 }
6043 else {
6044 p->ruby_sourcefile_string = rb_fstring(fname);
6045 p->ruby_sourcefile = StringValueCStr(fname);
6046 }
6047 p->ruby_sourceline = line - 1;
6048
6049 p->ast = ast = rb_ast_new();
6050 rb_suppress_tracing(yycompile0, (VALUE)p);
6051 p->ast = 0;
6052 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6053
6054 return ast;
6055}
6056#endif /* !RIPPER */
6057
6058static rb_encoding *
6059must_be_ascii_compatible(VALUE s)
6060{
6061 rb_encoding *enc = rb_enc_get(s);
6062 if (!rb_enc_asciicompat(enc)) {
6063 rb_raise(rb_eArgError, "invalid source encoding");
6064 }
6065 return enc;
6066}
6067
6068static VALUE
6069lex_get_str(struct parser_params *p, VALUE s)
6070{
6071 char *beg, *end, *start;
6072 long len;
6073
6074 beg = RSTRING_PTR(s);
6075 len = RSTRING_LEN(s);
6076 start = beg;
6077 if (p->lex.gets_.ptr) {
6078 if (len == p->lex.gets_.ptr) return Qnil;
6079 beg += p->lex.gets_.ptr;
6080 len -= p->lex.gets_.ptr;
6081 }
6082 end = memchr(beg, '\n', len);
6083 if (end) len = ++end - beg;
6084 p->lex.gets_.ptr += len;
6085 return rb_str_subseq(s, beg - start, len);
6086}
6087
6088static VALUE
6089lex_getline(struct parser_params *p)
6090{
6091 VALUE line = (*p->lex.gets)(p, p->lex.input);
6092 if (NIL_P(line)) return line;
6093 must_be_ascii_compatible(line);
6094#ifndef RIPPER
6095 if (p->debug_lines) {
6096 rb_enc_associate(line, p->enc);
6097 rb_ary_push(p->debug_lines, line);
6098 }
6099#endif
6100 p->line_count++;
6101 return line;
6102}
6103
6104static const rb_data_type_t parser_data_type;
6105
6106#ifndef RIPPER
6107static rb_ast_t*
6108parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6109{
6110 struct parser_params *p;
6111
6112 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6113
6114 p->lex.gets = lex_get_str;
6115 p->lex.gets_.ptr = 0;
6116 p->lex.input = rb_str_new_frozen(s);
6117 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6118
6119 return yycompile(vparser, p, fname, line);
6120}
6121
6122rb_ast_t*
6123rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6124{
6125 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6126}
6127
6128rb_ast_t*
6129rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6130{
6131 must_be_ascii_compatible(s);
6132 return parser_compile_string(vparser, f, s, line);
6133}
6134
6135VALUE rb_io_gets_internal(VALUE io);
6136
6137static VALUE
6138lex_io_gets(struct parser_params *p, VALUE io)
6139{
6140 return rb_io_gets_internal(io);
6141}
6142
6143rb_ast_t*
6144rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6145{
6146 struct parser_params *p;
6147
6148 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6149
6150 p->lex.gets = lex_io_gets;
6151 p->lex.input = file;
6152 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6153
6154 return yycompile(vparser, p, fname, start);
6155}
6156
6157static VALUE
6158lex_generic_gets(struct parser_params *p, VALUE input)
6159{
6160 return (*p->lex.gets_.call)(input, p->line_count);
6161}
6162
6163rb_ast_t*
6164rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6165{
6166 struct parser_params *p;
6167
6168 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6169
6170 p->lex.gets = lex_generic_gets;
6171 p->lex.gets_.call = lex_gets;
6172 p->lex.input = input;
6173 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6174
6175 return yycompile(vparser, p, fname, start);
6176}
6177#endif /* !RIPPER */
6178
6179#define STR_FUNC_ESCAPE 0x01
6180#define STR_FUNC_EXPAND 0x02
6181#define STR_FUNC_REGEXP 0x04
6182#define STR_FUNC_QWORDS 0x08
6183#define STR_FUNC_SYMBOL 0x10
6184#define STR_FUNC_INDENT 0x20
6185#define STR_FUNC_LABEL 0x40
6186#define STR_FUNC_LIST 0x4000
6187#define STR_FUNC_TERM 0x8000
6188
6189enum string_type {
6190 str_label = STR_FUNC_LABEL,
6191 str_squote = (0),
6192 str_dquote = (STR_FUNC_EXPAND),
6193 str_xquote = (STR_FUNC_EXPAND),
6194 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6195 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6196 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6197 str_ssym = (STR_FUNC_SYMBOL),
6198 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6199};
6200
6201static VALUE
6202parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6203{
6204 VALUE str;
6205
6206 str = rb_enc_str_new(ptr, len, enc);
6207 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6208 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6209 }
6210 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6211 rb_enc_associate(str, rb_ascii8bit_encoding());
6212 }
6213 }
6214
6215 return str;
6216}
6217
6218#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6219#define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6220#define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6221#define peek(p,c) peek_n(p, (c), 0)
6222#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6223#define peekc(p) peekc_n(p, 0)
6224#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6225
6226#ifdef RIPPER
6227static void
6228add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6229{
6230 if (tok < end) {
6231 if (!has_delayed_token(p)) {
6232 p->delayed.token = rb_str_buf_new(end - tok);
6233 rb_enc_associate(p->delayed.token, p->enc);
6234 p->delayed.line = p->ruby_sourceline;
6235 p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6236 }
6237 rb_str_buf_cat(p->delayed.token, tok, end - tok);
6238 p->lex.ptok = end;
6239 }
6240}
6241#else
6242#define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6243#endif
6244
6245static int
6246nextline(struct parser_params *p)
6247{
6248 VALUE v = p->lex.nextline;
6249 p->lex.nextline = 0;
6250 if (!v) {
6251 if (p->eofp)
6252 return -1;
6253
6254 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6255 goto end_of_input;
6256 }
6257
6258 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6259 end_of_input:
6260 p->eofp = 1;
6261 lex_goto_eol(p);
6262 return -1;
6263 }
6264 p->cr_seen = FALSE;
6265 }
6266 else if (NIL_P(v)) {
6267 /* after here-document without terminator */
6268 goto end_of_input;
6269 }
6270 add_delayed_token(p, p->lex.ptok, p->lex.pend);
6271 if (p->heredoc_end > 0) {
6272 p->ruby_sourceline = p->heredoc_end;
6273 p->heredoc_end = 0;
6274 }
6275 p->ruby_sourceline++;
6276 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
6277 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
6278 token_flush(p);
6279 p->lex.prevline = p->lex.lastline;
6280 p->lex.lastline = v;
6281 return 0;
6282}
6283
6284static int
6285parser_cr(struct parser_params *p, int c)
6286{
6287 if (peek(p, '\n')) {
6288 p->lex.pcur++;
6289 c = '\n';
6290 }
6291 else if (!p->cr_seen) {
6292 p->cr_seen = TRUE;
6293 /* carried over with p->lex.nextline for nextc() */
6294 rb_warn0("encountered \\r in middle of line, treated as a mere space");
6295 }
6296 return c;
6297}
6298
6299static inline int
6300nextc(struct parser_params *p)
6301{
6302 int c;
6303
6304 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6305 if (nextline(p)) return -1;
6306 }
6307 c = (unsigned char)*p->lex.pcur++;
6308 if (UNLIKELY(c == '\r')) {
6309 c = parser_cr(p, c);
6310 }
6311
6312 return c;
6313}
6314
6315static void
6316pushback(struct parser_params *p, int c)
6317{
6318 if (c == -1) return;
6319 p->lex.pcur--;
6320 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6321 p->lex.pcur--;
6322 }
6323}
6324
6325#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6326
6327#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6328#define tok(p) (p)->tokenbuf
6329#define toklen(p) (p)->tokidx
6330
6331static int
6332looking_at_eol_p(struct parser_params *p)
6333{
6334 const char *ptr = p->lex.pcur;
6335 while (ptr < p->lex.pend) {
6336 int c = (unsigned char)*ptr++;
6337 int eol = (c == '\n' || c == '#');
6338 if (eol || !ISSPACE(c)) {
6339 return eol;
6340 }
6341 }
6342 return TRUE;
6343}
6344
6345static char*
6346newtok(struct parser_params *p)
6347{
6348 p->tokidx = 0;
6349 p->tokline = p->ruby_sourceline;
6350 if (!p->tokenbuf) {
6351 p->toksiz = 60;
6352 p->tokenbuf = ALLOC_N(char, 60);
6353 }
6354 if (p->toksiz > 4096) {
6355 p->toksiz = 60;
6356 REALLOC_N(p->tokenbuf, char, 60);
6357 }
6358 return p->tokenbuf;
6359}
6360
6361static char *
6362tokspace(struct parser_params *p, int n)
6363{
6364 p->tokidx += n;
6365
6366 if (p->tokidx >= p->toksiz) {
6367 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6368 REALLOC_N(p->tokenbuf, char, p->toksiz);
6369 }
6370 return &p->tokenbuf[p->tokidx-n];
6371}
6372
6373static void
6374tokadd(struct parser_params *p, int c)
6375{
6376 p->tokenbuf[p->tokidx++] = (char)c;
6377 if (p->tokidx >= p->toksiz) {
6378 p->toksiz *= 2;
6379 REALLOC_N(p->tokenbuf, char, p->toksiz);
6380 }
6381}
6382
6383static int
6384tok_hex(struct parser_params *p, size_t *numlen)
6385{
6386 int c;
6387
6388 c = scan_hex(p->lex.pcur, 2, numlen);
6389 if (!*numlen) {
6390 yyerror0("invalid hex escape");
6391 token_flush(p);
6392 return 0;
6393 }
6394 p->lex.pcur += *numlen;
6395 return c;
6396}
6397
6398#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6399
6400static int
6401escaped_control_code(int c)
6402{
6403 int c2 = 0;
6404 switch (c) {
6405 case ' ':
6406 c2 = 's';
6407 break;
6408 case '\n':
6409 c2 = 'n';
6410 break;
6411 case '\t':
6412 c2 = 't';
6413 break;
6414 case '\v':
6415 c2 = 'v';
6416 break;
6417 case '\r':
6418 c2 = 'r';
6419 break;
6420 case '\f':
6421 c2 = 'f';
6422 break;
6423 }
6424 return c2;
6425}
6426
6427#define WARN_SPACE_CHAR(c, prefix) \
6428 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6429
6430static int
6431tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6432 int regexp_literal, int wide)
6433{
6434 size_t numlen;
6435 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6436 literal_flush(p, p->lex.pcur);
6437 p->lex.pcur += numlen;
6438 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6439 yyerror0("invalid Unicode escape");
6440 return wide && numlen > 0;
6441 }
6442 if (codepoint > 0x10ffff) {
6443 yyerror0("invalid Unicode codepoint (too large)");
6444 return wide;
6445 }
6446 if ((codepoint & 0xfffff800) == 0xd800) {
6447 yyerror0("invalid Unicode codepoint");
6448 return wide;
6449 }
6450 if (regexp_literal) {
6451 tokcopy(p, (int)numlen);
6452 }
6453 else if (codepoint >= 0x80) {
6454 rb_encoding *utf8 = rb_utf8_encoding();
6455 if (*encp && utf8 != *encp) {
6456 YYLTYPE loc = RUBY_INIT_YYLLOC();
6457 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6458 parser_show_error_line(p, &loc);
6459 return wide;
6460 }
6461 *encp = utf8;
6462 tokaddmbc(p, codepoint, *encp);
6463 }
6464 else {
6465 tokadd(p, codepoint);
6466 }
6467 return TRUE;
6468}
6469
6470/* return value is for ?\u3042 */
6471static void
6472tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6473 int term, int symbol_literal, int regexp_literal)
6474{
6475 /*
6476 * If `term` is not -1, then we allow multiple codepoints in \u{}
6477 * upto `term` byte, otherwise we're parsing a character literal.
6478 * And then add the codepoints to the current token.
6479 */
6480 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6481
6482 const int open_brace = '{', close_brace = '}';
6483
6484 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6485
6486 if (peek(p, open_brace)) { /* handle \u{...} form */
6487 const char *second = NULL;
6488 int c, last = nextc(p);
6489 if (p->lex.pcur >= p->lex.pend) goto unterminated;
6490 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6491 while (c != close_brace) {
6492 if (c == term) goto unterminated;
6493 if (second == multiple_codepoints)
6494 second = p->lex.pcur;
6495 if (regexp_literal) tokadd(p, last);
6496 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6497 break;
6498 }
6499 while (ISSPACE(c = *p->lex.pcur)) {
6500 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6501 last = c;
6502 }
6503 if (term == -1 && !second)
6504 second = multiple_codepoints;
6505 }
6506
6507 if (c != close_brace) {
6508 unterminated:
6509 token_flush(p);
6510 yyerror0("unterminated Unicode escape");
6511 return;
6512 }
6513 if (second && second != multiple_codepoints) {
6514 const char *pcur = p->lex.pcur;
6515 p->lex.pcur = second;
6516 dispatch_scan_event(p, tSTRING_CONTENT);
6517 token_flush(p);
6518 p->lex.pcur = pcur;
6519 yyerror0(multiple_codepoints);
6520 token_flush(p);
6521 }
6522
6523 if (regexp_literal) tokadd(p, close_brace);
6524 nextc(p);
6525 }
6526 else { /* handle \uxxxx form */
6527 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6528 token_flush(p);
6529 return;
6530 }
6531 }
6532}
6533
6534#define ESCAPE_CONTROL 1
6535#define ESCAPE_META 2
6536
6537static int
6538read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6539{
6540 int c;
6541 size_t numlen;
6542
6543 switch (c = nextc(p)) {
6544 case '\\': /* Backslash */
6545 return c;
6546
6547 case 'n': /* newline */
6548 return '\n';
6549
6550 case 't': /* horizontal tab */
6551 return '\t';
6552
6553 case 'r': /* carriage-return */
6554 return '\r';
6555
6556 case 'f': /* form-feed */
6557 return '\f';
6558
6559 case 'v': /* vertical tab */
6560 return '\13';
6561
6562 case 'a': /* alarm(bell) */
6563 return '\007';
6564
6565 case 'e': /* escape */
6566 return 033;
6567
6568 case '0': case '1': case '2': case '3': /* octal constant */
6569 case '4': case '5': case '6': case '7':
6570 pushback(p, c);
6571 c = scan_oct(p->lex.pcur, 3, &numlen);
6572 p->lex.pcur += numlen;
6573 return c;
6574
6575 case 'x': /* hex constant */
6576 c = tok_hex(p, &numlen);
6577 if (numlen == 0) return 0;
6578 return c;
6579
6580 case 'b': /* backspace */
6581 return '\010';
6582
6583 case 's': /* space */
6584 return ' ';
6585
6586 case 'M':
6587 if (flags & ESCAPE_META) goto eof;
6588 if ((c = nextc(p)) != '-') {
6589 goto eof;
6590 }
6591 if ((c = nextc(p)) == '\\') {
6592 if (peek(p, 'u')) goto eof;
6593 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6594 }
6595 else if (c == -1 || !ISASCII(c)) goto eof;
6596 else {
6597 int c2 = escaped_control_code(c);
6598 if (c2) {
6599 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6600 WARN_SPACE_CHAR(c2, "\\M-");
6601 }
6602 else {
6603 WARN_SPACE_CHAR(c2, "\\C-\\M-");
6604 }
6605 }
6606 else if (ISCNTRL(c)) goto eof;
6607 return ((c & 0xff) | 0x80);
6608 }
6609
6610 case 'C':
6611 if ((c = nextc(p)) != '-') {
6612 goto eof;
6613 }
6614 case 'c':
6615 if (flags & ESCAPE_CONTROL) goto eof;
6616 if ((c = nextc(p))== '\\') {
6617 if (peek(p, 'u')) goto eof;
6618 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6619 }
6620 else if (c == '?')
6621 return 0177;
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)) {
6627 if (flags & ESCAPE_META) {
6628 WARN_SPACE_CHAR(c2, "\\M-");
6629 }
6630 else {
6631 WARN_SPACE_CHAR(c2, "");
6632 }
6633 }
6634 else {
6635 if (flags & ESCAPE_META) {
6636 WARN_SPACE_CHAR(c2, "\\M-\\C-");
6637 }
6638 else {
6639 WARN_SPACE_CHAR(c2, "\\C-");
6640 }
6641 }
6642 }
6643 else if (ISCNTRL(c)) goto eof;
6644 }
6645 return c & 0x9f;
6646
6647 eof:
6648 case -1:
6649 yyerror0("Invalid escape character syntax");
6650 token_flush(p);
6651 return '\0';
6652
6653 default:
6654 return c;
6655 }
6656}
6657
6658static void
6659tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
6660{
6661 int len = rb_enc_codelen(c, enc);
6662 rb_enc_mbcput(c, tokspace(p, len), enc);
6663}
6664
6665static int
6666tokadd_escape(struct parser_params *p, rb_encoding **encp)
6667{
6668 int c;
6669 int flags = 0;
6670 size_t numlen;
6671
6672 first:
6673 switch (c = nextc(p)) {
6674 case '\n':
6675 return 0; /* just ignore */
6676
6677 case '0': case '1': case '2': case '3': /* octal constant */
6678 case '4': case '5': case '6': case '7':
6679 {
6680 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
6681 if (numlen == 0) goto eof;
6682 p->lex.pcur += numlen;
6683 tokcopy(p, (int)numlen + 1);
6684 }
6685 return 0;
6686
6687 case 'x': /* hex constant */
6688 {
6689 tok_hex(p, &numlen);
6690 if (numlen == 0) return -1;
6691 tokcopy(p, (int)numlen + 2);
6692 }
6693 return 0;
6694
6695 case 'M':
6696 if (flags & ESCAPE_META) goto eof;
6697 if ((c = nextc(p)) != '-') {
6698 pushback(p, c);
6699 goto eof;
6700 }
6701 tokcopy(p, 3);
6702 flags |= ESCAPE_META;
6703 goto escaped;
6704
6705 case 'C':
6706 if (flags & ESCAPE_CONTROL) goto eof;
6707 if ((c = nextc(p)) != '-') {
6708 pushback(p, c);
6709 goto eof;
6710 }
6711 tokcopy(p, 3);
6712 goto escaped;
6713
6714 case 'c':
6715 if (flags & ESCAPE_CONTROL) goto eof;
6716 tokcopy(p, 2);
6717 flags |= ESCAPE_CONTROL;
6718 escaped:
6719 if ((c = nextc(p)) == '\\') {
6720 goto first;
6721 }
6722 else if (c == -1) goto eof;
6723 tokadd(p, c);
6724 return 0;
6725
6726 eof:
6727 case -1:
6728 yyerror0("Invalid escape character syntax");
6729 token_flush(p);
6730 return -1;
6731
6732 default:
6733 tokadd(p, '\\');
6734 tokadd(p, c);
6735 }
6736 return 0;
6737}
6738
6739static int
6740regx_options(struct parser_params *p)
6741{
6742 int kcode = 0;
6743 int kopt = 0;
6744 int options = 0;
6745 int c, opt, kc;
6746
6747 newtok(p);
6748 while (c = nextc(p), ISALPHA(c)) {
6749 if (c == 'o') {
6750 options |= RE_OPTION_ONCE;
6751 }
6752 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
6753 if (kc >= 0) {
6754 if (kc != rb_ascii8bit_encindex()) kcode = c;
6755 kopt = opt;
6756 }
6757 else {
6758 options |= opt;
6759 }
6760 }
6761 else {
6762 tokadd(p, c);
6763 }
6764 }
6765 options |= kopt;
6766 pushback(p, c);
6767 if (toklen(p)) {
6768 YYLTYPE loc = RUBY_INIT_YYLLOC();
6769 tokfix(p);
6770 compile_error(p, "unknown regexp option%s - %*s",
6771 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
6772 parser_show_error_line(p, &loc);
6773 }
6774 return options | RE_OPTION_ENCODING(kcode);
6775}
6776
6777static int
6778tokadd_mbchar(struct parser_params *p, int c)
6779{
6780 int len = parser_precise_mbclen(p, p->lex.pcur-1);
6781 if (len < 0) return -1;
6782 tokadd(p, c);
6783 p->lex.pcur += --len;
6784 if (len > 0) tokcopy(p, len);
6785 return c;
6786}
6787
6788static inline int
6789simple_re_meta(int c)
6790{
6791 switch (c) {
6792 case '$': case '*': case '+': case '.':
6793 case '?': case '^': case '|':
6794 case ')': case ']': case '}': case '>':
6795 return TRUE;
6796 default:
6797 return FALSE;
6798 }
6799}
6800
6801static int
6802parser_update_heredoc_indent(struct parser_params *p, int c)
6803{
6804 if (p->heredoc_line_indent == -1) {
6805 if (c == '\n') p->heredoc_line_indent = 0;
6806 }
6807 else {
6808 if (c == ' ') {
6809 p->heredoc_line_indent++;
6810 return TRUE;
6811 }
6812 else if (c == '\t') {
6813 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
6814 p->heredoc_line_indent = w * TAB_WIDTH;
6815 return TRUE;
6816 }
6817 else if (c != '\n') {
6818 if (p->heredoc_indent > p->heredoc_line_indent) {
6819 p->heredoc_indent = p->heredoc_line_indent;
6820 }
6821 p->heredoc_line_indent = -1;
6822 }
6823 }
6824 return FALSE;
6825}
6826
6827static void
6828parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
6829{
6830 YYLTYPE loc = RUBY_INIT_YYLLOC();
6831 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
6832 compile_error(p, "%s mixed within %s source", n1, n2);
6833 parser_show_error_line(p, &loc);
6834}
6835
6836static void
6837parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
6838{
6839 const char *pos = p->lex.pcur;
6840 p->lex.pcur = beg;
6841 parser_mixed_error(p, enc1, enc2);
6842 p->lex.pcur = pos;
6843}
6844
6845static int
6846tokadd_string(struct parser_params *p,
6847 int func, int term, int paren, long *nest,
6848 rb_encoding **encp, rb_encoding **enc)
6849{
6850 int c;
6851 bool erred = false;
6852
6853#define mixed_error(enc1, enc2) \
6854 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
6855#define mixed_escape(beg, enc1, enc2) \
6856 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
6857
6858 while ((c = nextc(p)) != -1) {
6859 if (p->heredoc_indent > 0) {
6860 parser_update_heredoc_indent(p, c);
6861 }
6862
6863 if (paren && c == paren) {
6864 ++*nest;
6865 }
6866 else if (c == term) {
6867 if (!nest || !*nest) {
6868 pushback(p, c);
6869 break;
6870 }
6871 --*nest;
6872 }
6873 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
6874 int c2 = *p->lex.pcur;
6875 if (c2 == '$' || c2 == '@' || c2 == '{') {
6876 pushback(p, c);
6877 break;
6878 }
6879 }
6880 else if (c == '\\') {
6881 literal_flush(p, p->lex.pcur - 1);
6882 c = nextc(p);
6883 switch (c) {
6884 case '\n':
6885 if (func & STR_FUNC_QWORDS) break;
6886 if (func & STR_FUNC_EXPAND) {
6887 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
6888 continue;
6889 if (c == term) {
6890 c = '\\';
6891 goto terminate;
6892 }
6893 }
6894 tokadd(p, '\\');
6895 break;
6896
6897 case '\\':
6898 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
6899 break;
6900
6901 case 'u':
6902 if ((func & STR_FUNC_EXPAND) == 0) {
6903 tokadd(p, '\\');
6904 break;
6905 }
6906 tokadd_utf8(p, enc, term,
6907 func & STR_FUNC_SYMBOL,
6908 func & STR_FUNC_REGEXP);
6909 continue;
6910
6911 default:
6912 if (c == -1) return -1;
6913 if (!ISASCII(c)) {
6914 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
6915 goto non_ascii;
6916 }
6917 if (func & STR_FUNC_REGEXP) {
6918 if (c == term && !simple_re_meta(c)) {
6919 tokadd(p, c);
6920 continue;
6921 }
6922 pushback(p, c);
6923 if ((c = tokadd_escape(p, enc)) < 0)
6924 return -1;
6925 if (*enc && *enc != *encp) {
6926 mixed_escape(p->lex.ptok+2, *enc, *encp);
6927 }
6928 continue;
6929 }
6930 else if (func & STR_FUNC_EXPAND) {
6931 pushback(p, c);
6932 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
6933 c = read_escape(p, 0, enc);
6934 }
6935 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6936 /* ignore backslashed spaces in %w */
6937 }
6938 else if (c != term && !(paren && c == paren)) {
6939 tokadd(p, '\\');
6940 pushback(p, c);
6941 continue;
6942 }
6943 }
6944 }
6945 else if (!parser_isascii(p)) {
6946 non_ascii:
6947 if (!*enc) {
6948 *enc = *encp;
6949 }
6950 else if (*enc != *encp) {
6951 mixed_error(*enc, *encp);
6952 continue;
6953 }
6954 if (tokadd_mbchar(p, c) == -1) return -1;
6955 continue;
6956 }
6957 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6958 pushback(p, c);
6959 break;
6960 }
6961 if (c & 0x80) {
6962 if (!*enc) {
6963 *enc = *encp;
6964 }
6965 else if (*enc != *encp) {
6966 mixed_error(*enc, *encp);
6967 continue;
6968 }
6969 }
6970 tokadd(p, c);
6971 }
6972 terminate:
6973 if (*enc) *encp = *enc;
6974 return c;
6975}
6976
6977static inline rb_strterm_t *
6978new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
6979{
6980 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
6981}
6982
6983/* imemo_parser_strterm for literal */
6984#define NEW_STRTERM(func, term, paren) \
6985 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
6986
6987#ifdef RIPPER
6988static void
6989flush_string_content(struct parser_params *p, rb_encoding *enc)
6990{
6991 VALUE content = yylval.val;
6992 if (!ripper_is_node_yylval(content))
6993 content = ripper_new_yylval(p, 0, 0, content);
6994 if (has_delayed_token(p)) {
6995 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
6996 if (len > 0) {
6997 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
6998 }
6999 dispatch_delayed_token(p, tSTRING_CONTENT);
7000 p->lex.ptok = p->lex.pcur;
7001 RNODE(content)->nd_rval = yylval.val;
7002 }
7003 dispatch_scan_event(p, tSTRING_CONTENT);
7004 if (yylval.val != content)
7005 RNODE(content)->nd_rval = yylval.val;
7006 yylval.val = content;
7007}
7008#else
7009#define flush_string_content(p, enc) ((void)(enc))
7010#endif
7011
7012RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7013/* this can be shared with ripper, since it's independent from struct
7014 * parser_params. */
7015#ifndef RIPPER
7016#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7017#define SPECIAL_PUNCT(idx) ( \
7018 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
7019 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
7020 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
7021 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
7022 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7023 BIT('0', idx))
7024const unsigned int ruby_global_name_punct_bits[] = {
7025 SPECIAL_PUNCT(0),
7026 SPECIAL_PUNCT(1),
7027 SPECIAL_PUNCT(2),
7028};
7029#undef BIT
7030#undef SPECIAL_PUNCT
7031#endif
7032
7033static enum yytokentype
7034parser_peek_variable_name(struct parser_params *p)
7035{
7036 int c;
7037 const char *ptr = p->lex.pcur;
7038
7039 if (ptr + 1 >= p->lex.pend) return 0;
7040 c = *ptr++;
7041 switch (c) {
7042 case '$':
7043 if ((c = *ptr) == '-') {
7044 if (++ptr >= p->lex.pend) return 0;
7045 c = *ptr;
7046 }
7047 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7048 return tSTRING_DVAR;
7049 }
7050 break;
7051 case '@':
7052 if ((c = *ptr) == '@') {
7053 if (++ptr >= p->lex.pend) return 0;
7054 c = *ptr;
7055 }
7056 break;
7057 case '{':
7058 p->lex.pcur = ptr;
7059 p->command_start = TRUE;
7060 return tSTRING_DBEG;
7061 default:
7062 return 0;
7063 }
7064 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7065 return tSTRING_DVAR;
7066 return 0;
7067}
7068
7069#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7070#define IS_END() IS_lex_state(EXPR_END_ANY)
7071#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7072#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7073#define IS_LABEL_POSSIBLE() (\
7074 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7075 IS_ARG())
7076#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7077#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7078
7079static inline enum yytokentype
7080parser_string_term(struct parser_params *p, int func)
7081{
7082 p->lex.strterm = 0;
7083 if (func & STR_FUNC_REGEXP) {
7084 set_yylval_num(regx_options(p));
7085 dispatch_scan_event(p, tREGEXP_END);
7086 SET_LEX_STATE(EXPR_END);
7087 return tREGEXP_END;
7088 }
7089 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7090 nextc(p);
7091 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7092 return tLABEL_END;
7093 }
7094 SET_LEX_STATE(EXPR_END);
7095 return tSTRING_END;
7096}
7097
7098static enum yytokentype
7099parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7100{
7101 int func = (int)quote->u1.func;
7102 int term = (int)quote->u3.term;
7103 int paren = (int)quote->u2.paren;
7104 int c, space = 0;
7105 rb_encoding *enc = p->enc;
7106 rb_encoding *base_enc = 0;
7107 VALUE lit;
7108
7109 if (func & STR_FUNC_TERM) {
7110 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7111 SET_LEX_STATE(EXPR_END);
7112 p->lex.strterm = 0;
7113 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7114 }
7115 c = nextc(p);
7116 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7117 do {c = nextc(p);} while (ISSPACE(c));
7118 space = 1;
7119 }
7120 if (func & STR_FUNC_LIST) {
7121 quote->u1.func &= ~STR_FUNC_LIST;
7122 space = 1;
7123 }
7124 if (c == term && !quote->u0.nest) {
7125 if (func & STR_FUNC_QWORDS) {
7126 quote->u1.func |= STR_FUNC_TERM;
7127 pushback(p, c); /* dispatch the term at tSTRING_END */
7128 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7129 return ' ';
7130 }
7131 return parser_string_term(p, func);
7132 }
7133 if (space) {
7134 pushback(p, c);
7135 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7136 return ' ';
7137 }
7138 newtok(p);
7139 if ((func & STR_FUNC_EXPAND) && c == '#') {
7140 int t = parser_peek_variable_name(p);
7141 if (t) return t;
7142 tokadd(p, '#');
7143 c = nextc(p);
7144 }
7145 pushback(p, c);
7146 if (tokadd_string(p, func, term, paren, &quote->u0.nest,
7147 &enc, &base_enc) == -1) {
7148 if (p->eofp) {
7149#ifndef RIPPER
7150# define unterminated_literal(mesg) yyerror0(mesg)
7151#else
7152# define unterminated_literal(mesg) compile_error(p, mesg)
7153#endif
7154 literal_flush(p, p->lex.pcur);
7155 if (func & STR_FUNC_QWORDS) {
7156 /* no content to add, bailing out here */
7157 unterminated_literal("unterminated list meets end of file");
7158 p->lex.strterm = 0;
7159 return tSTRING_END;
7160 }
7161 if (func & STR_FUNC_REGEXP) {
7162 unterminated_literal("unterminated regexp meets end of file");
7163 }
7164 else {
7165 unterminated_literal("unterminated string meets end of file");
7166 }
7167 quote->u1.func |= STR_FUNC_TERM;
7168 }
7169 }
7170
7171 tokfix(p);
7172 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7173 set_yylval_str(lit);
7174 flush_string_content(p, enc);
7175
7176 return tSTRING_CONTENT;
7177}
7178
7179static enum yytokentype
7180heredoc_identifier(struct parser_params *p)
7181{
7182 /*
7183 * term_len is length of `<<"END"` except `END`,
7184 * in this case term_len is 4 (<, <, " and ").
7185 */
7186 long len, offset = p->lex.pcur - p->lex.pbeg;
7187 int c = nextc(p), term, func = 0, quote = 0;
7188 enum yytokentype token = tSTRING_BEG;
7189 int indent = 0;
7190
7191 if (c == '-') {
7192 c = nextc(p);
7193 func = STR_FUNC_INDENT;
7194 offset++;
7195 }
7196 else if (c == '~') {
7197 c = nextc(p);
7198 func = STR_FUNC_INDENT;
7199 offset++;
7200 indent = INT_MAX;
7201 }
7202 switch (c) {
7203 case '\'':
7204 func |= str_squote; goto quoted;
7205 case '"':
7206 func |= str_dquote; goto quoted;
7207 case '`':
7208 token = tXSTRING_BEG;
7209 func |= str_xquote; goto quoted;
7210
7211 quoted:
7212 quote++;
7213 offset++;
7214 term = c;
7215 len = 0;
7216 while ((c = nextc(p)) != term) {
7217 if (c == -1 || c == '\r' || c == '\n') {
7218 yyerror(NULL, p, "unterminated here document identifier");
7219 return -1;
7220 }
7221 }
7222 break;
7223
7224 default:
7225 if (!parser_is_identchar(p)) {
7226 pushback(p, c);
7227 if (func & STR_FUNC_INDENT) {
7228 pushback(p, indent > 0 ? '~' : '-');
7229 }
7230 return 0;
7231 }
7232 func |= str_dquote;
7233 do {
7234 int n = parser_precise_mbclen(p, p->lex.pcur-1);
7235 if (n < 0) return 0;
7236 p->lex.pcur += --n;
7237 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7238 pushback(p, c);
7239 break;
7240 }
7241
7242 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7243 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7244 yyerror(NULL, p, "too long here document identifier");
7245 dispatch_scan_event(p, tHEREDOC_BEG);
7246 lex_goto_eol(p);
7247
7248 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7249 p->lex.strterm->flags |= STRTERM_HEREDOC;
7250 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7251 here->offset = offset;
7252 here->sourceline = p->ruby_sourceline;
7253 here->length = (int)len;
7254 here->quote = quote;
7255 here->func = func;
7256
7257 token_flush(p);
7258 p->heredoc_indent = indent;
7259 p->heredoc_line_indent = 0;
7260 return token;
7261}
7262
7263static void
7264heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7265{
7266 VALUE line;
7267
7268 p->lex.strterm = 0;
7269 line = here->lastline;
7270 p->lex.lastline = line;
7271 p->lex.pbeg = RSTRING_PTR(line);
7272 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7273 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7274 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7275 p->heredoc_end = p->ruby_sourceline;
7276 p->ruby_sourceline = (int)here->sourceline;
7277 if (p->eofp) p->lex.nextline = Qnil;
7278 p->eofp = 0;
7279}
7280
7281static int
7282dedent_string(VALUE string, int width)
7283{
7284 char *str;
7285 long len;
7286 int i, col = 0;
7287
7288 RSTRING_GETMEM(string, str, len);
7289 for (i = 0; i < len && col < width; i++) {
7290 if (str[i] == ' ') {
7291 col++;
7292 }
7293 else if (str[i] == '\t') {
7294 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7295 if (n > width) break;
7296 col = n;
7297 }
7298 else {
7299 break;
7300 }
7301 }
7302 if (!i) return 0;
7303 rb_str_modify(string);
7304 str = RSTRING_PTR(string);
7305 if (RSTRING_LEN(string) != len)
7306 rb_fatal("literal string changed: %+"PRIsVALUE, string);
7307 MEMMOVE(str, str + i, char, len - i);
7308 rb_str_set_len(string, len - i);
7309 return i;
7310}
7311
7312#ifndef RIPPER
7313static NODE *
7314heredoc_dedent(struct parser_params *p, NODE *root)
7315{
7316 NODE *node, *str_node, *prev_node;
7317 int indent = p->heredoc_indent;
7318 VALUE prev_lit = 0;
7319
7320 if (indent <= 0) return root;
7321 p->heredoc_indent = 0;
7322 if (!root) return root;
7323
7324 prev_node = node = str_node = root;
7325 if (nd_type(root) == NODE_LIST) str_node = root->nd_head;
7326
7327 while (str_node) {
7328 VALUE lit = str_node->nd_lit;
7329 if (str_node->flags & NODE_FL_NEWLINE) {
7330 dedent_string(lit, indent);
7331 }
7332 if (!prev_lit) {
7333 prev_lit = lit;
7334 }
7335 else if (!literal_concat0(p, prev_lit, lit)) {
7336 return 0;
7337 }
7338 else {
7339 NODE *end = node->nd_end;
7340 node = prev_node->nd_next = node->nd_next;
7341 if (!node) {
7342 if (nd_type(prev_node) == NODE_DSTR)
7343 nd_set_type(prev_node, NODE_STR);
7344 break;
7345 }
7346 node->nd_end = end;
7347 goto next_str;
7348 }
7349
7350 str_node = 0;
7351 while ((node = (prev_node = node)->nd_next) != 0) {
7352 next_str:
7353 if (nd_type(node) != NODE_LIST) break;
7354 if ((str_node = node->nd_head) != 0) {
7355 enum node_type type = nd_type(str_node);
7356 if (type == NODE_STR || type == NODE_DSTR) break;
7357 prev_lit = 0;
7358 str_node = 0;
7359 }
7360 }
7361 }
7362 return root;
7363}
7364#else /* RIPPER */
7365static VALUE
7366heredoc_dedent(struct parser_params *p, VALUE array)
7367{
7368 int indent = p->heredoc_indent;
7369
7370 if (indent <= 0) return array;
7371 p->heredoc_indent = 0;
7372 dispatch2(heredoc_dedent, array, INT2NUM(indent));
7373 return array;
7374}
7375
7376/*
7377 * call-seq:
7378 * Ripper.dedent_string(input, width) -> Integer
7379 *
7380 * USE OF RIPPER LIBRARY ONLY.
7381 *
7382 * Strips up to +width+ leading whitespaces from +input+,
7383 * and returns the stripped column width.
7384 */
7385static VALUE
7386parser_dedent_string(VALUE self, VALUE input, VALUE width)
7387{
7388 int wid, col;
7389
7390 StringValue(input);
7391 wid = NUM2UINT(width);
7392 col = dedent_string(input, wid);
7393 return INT2NUM(col);
7394}
7395#endif
7396
7397static int
7398whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7399{
7400 const char *ptr = p->lex.pbeg;
7401 long n;
7402
7403 if (indent) {
7404 while (*ptr && ISSPACE(*ptr)) ptr++;
7405 }
7406 n = p->lex.pend - (ptr + len);
7407 if (n < 0) return FALSE;
7408 if (n > 0 && ptr[len] != '\n') {
7409 if (ptr[len] != '\r') return FALSE;
7410 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7411 }
7412 return strncmp(eos, ptr, len) == 0;
7413}
7414
7415static int
7416word_match_p(struct parser_params *p, const char *word, long len)
7417{
7418 if (strncmp(p->lex.pcur, word, len)) return 0;
7419 if (p->lex.pcur + len == p->lex.pend) return 1;
7420 int c = (unsigned char)p->lex.pcur[len];
7421 if (ISSPACE(c)) return 1;
7422 switch (c) {
7423 case '\0': case '\004': case '\032': return 1;
7424 }
7425 return 0;
7426}
7427
7428#define NUM_SUFFIX_R (1<<0)
7429#define NUM_SUFFIX_I (1<<1)
7430#define NUM_SUFFIX_ALL 3
7431
7432static int
7433number_literal_suffix(struct parser_params *p, int mask)
7434{
7435 int c, result = 0;
7436 const char *lastp = p->lex.pcur;
7437
7438 while ((c = nextc(p)) != -1) {
7439 if ((mask & NUM_SUFFIX_I) && c == 'i') {
7440 result |= (mask & NUM_SUFFIX_I);
7441 mask &= ~NUM_SUFFIX_I;
7442 /* r after i, rational of complex is disallowed */
7443 mask &= ~NUM_SUFFIX_R;
7444 continue;
7445 }
7446 if ((mask & NUM_SUFFIX_R) && c == 'r') {
7447 result |= (mask & NUM_SUFFIX_R);
7448 mask &= ~NUM_SUFFIX_R;
7449 continue;
7450 }
7451 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7452 p->lex.pcur = lastp;
7453 literal_flush(p, p->lex.pcur);
7454 return 0;
7455 }
7456 pushback(p, c);
7457 break;
7458 }
7459 return result;
7460}
7461
7462static enum yytokentype
7463set_number_literal(struct parser_params *p, VALUE v,
7464 enum yytokentype type, int suffix)
7465{
7466 if (suffix & NUM_SUFFIX_I) {
7467 v = rb_complex_raw(INT2FIX(0), v);
7468 type = tIMAGINARY;
7469 }
7470 set_yylval_literal(v);
7471 SET_LEX_STATE(EXPR_END);
7472 return type;
7473}
7474
7475static enum yytokentype
7476set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7477{
7478 enum yytokentype type = tINTEGER;
7479 if (suffix & NUM_SUFFIX_R) {
7480 v = rb_rational_raw1(v);
7481 type = tRATIONAL;
7482 }
7483 return set_number_literal(p, v, type, suffix);
7484}
7485
7486#ifdef RIPPER
7487static void
7488dispatch_heredoc_end(struct parser_params *p)
7489{
7490 VALUE str;
7491 if (has_delayed_token(p))
7492 dispatch_delayed_token(p, tSTRING_CONTENT);
7493 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7494 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7495 lex_goto_eol(p);
7496 token_flush(p);
7497}
7498
7499#else
7500#define dispatch_heredoc_end(p) ((void)0)
7501#endif
7502
7503static enum yytokentype
7504here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7505{
7506 int c, func, indent = 0;
7507 const char *eos, *ptr, *ptr_end;
7508 long len;
7509 VALUE str = 0;
7510 rb_encoding *enc = p->enc;
7511 rb_encoding *base_enc = 0;
7512 int bol;
7513
7514 eos = RSTRING_PTR(here->lastline) + here->offset;
7515 len = here->length;
7516 indent = (func = here->func) & STR_FUNC_INDENT;
7517
7518 if ((c = nextc(p)) == -1) {
7519 error:
7520#ifdef RIPPER
7521 if (!has_delayed_token(p)) {
7522 dispatch_scan_event(p, tSTRING_CONTENT);
7523 }
7524 else {
7525 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7526 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7527 int cr = ENC_CODERANGE_UNKNOWN;
7528 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7529 if (cr != ENC_CODERANGE_7BIT &&
7530 p->enc == rb_usascii_encoding() &&
7531 enc != rb_utf8_encoding()) {
7532 enc = rb_ascii8bit_encoding();
7533 }
7534 }
7535 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7536 }
7537 dispatch_delayed_token(p, tSTRING_CONTENT);
7538 }
7539 lex_goto_eol(p);
7540#endif
7541 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7542 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7543 (int)len, eos);
7544 token_flush(p);
7545 p->lex.strterm = 0;
7546 SET_LEX_STATE(EXPR_END);
7547 return tSTRING_END;
7548 }
7549 bol = was_bol(p);
7550 if (!bol) {
7551 /* not beginning of line, cannot be the terminator */
7552 }
7553 else if (p->heredoc_line_indent == -1) {
7554 /* `heredoc_line_indent == -1` means
7555 * - "after an interpolation in the same line", or
7556 * - "in a continuing line"
7557 */
7558 p->heredoc_line_indent = 0;
7559 }
7560 else if (whole_match_p(p, eos, len, indent)) {
7561 dispatch_heredoc_end(p);
7562 restore:
7563 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7564 token_flush(p);
7565 p->lex.strterm = 0;
7566 SET_LEX_STATE(EXPR_END);
7567 return tSTRING_END;
7568 }
7569
7570 if (!(func & STR_FUNC_EXPAND)) {
7571 do {
7572 ptr = RSTRING_PTR(p->lex.lastline);
7573 ptr_end = p->lex.pend;
7574 if (ptr_end > ptr) {
7575 switch (ptr_end[-1]) {
7576 case '\n':
7577 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7578 ptr_end++;
7579 break;
7580 }
7581 case '\r':
7582 --ptr_end;
7583 }
7584 }
7585
7586 if (p->heredoc_indent > 0) {
7587 long i = 0;
7588 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7589 i++;
7590 p->heredoc_line_indent = 0;
7591 }
7592
7593 if (str)
7594 rb_str_cat(str, ptr, ptr_end - ptr);
7595 else
7596 str = STR_NEW(ptr, ptr_end - ptr);
7597 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7598 lex_goto_eol(p);
7599 if (p->heredoc_indent > 0) {
7600 goto flush_str;
7601 }
7602 if (nextc(p) == -1) {
7603 if (str) {
7604 str = 0;
7605 }
7606 goto error;
7607 }
7608 } while (!whole_match_p(p, eos, len, indent));
7609 }
7610 else {
7611 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7612 newtok(p);
7613 if (c == '#') {
7614 int t = parser_peek_variable_name(p);
7615 if (p->heredoc_line_indent != -1) {
7616 if (p->heredoc_indent > p->heredoc_line_indent) {
7617 p->heredoc_indent = p->heredoc_line_indent;
7618 }
7619 p->heredoc_line_indent = -1;
7620 }
7621 if (t) return t;
7622 tokadd(p, '#');
7623 c = nextc(p);
7624 }
7625 do {
7626 pushback(p, c);
7627 enc = p->enc;
7628 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7629 if (p->eofp) goto error;
7630 goto restore;
7631 }
7632 if (c != '\n') {
7633 if (c == '\\') p->heredoc_line_indent = -1;
7634 flush:
7635 str = STR_NEW3(tok(p), toklen(p), enc, func);
7636 flush_str:
7637 set_yylval_str(str);
7638#ifndef RIPPER
7639 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7640#endif
7641 flush_string_content(p, enc);
7642 return tSTRING_CONTENT;
7643 }
7644 tokadd(p, nextc(p));
7645 if (p->heredoc_indent > 0) {
7646 lex_goto_eol(p);
7647 goto flush;
7648 }
7649 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
7650 if ((c = nextc(p)) == -1) goto error;
7651 } while (!whole_match_p(p, eos, len, indent));
7652 str = STR_NEW3(tok(p), toklen(p), enc, func);
7653 }
7654 dispatch_heredoc_end(p);
7655#ifdef RIPPER
7656 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
7657 yylval.val, str);
7658#endif
7659 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7660 token_flush(p);
7661 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
7662 set_yylval_str(str);
7663#ifndef RIPPER
7664 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7665#endif
7666 return tSTRING_CONTENT;
7667}
7668
7669#include "lex.c"
7670
7671static int
7672arg_ambiguous(struct parser_params *p, char c)
7673{
7674#ifndef RIPPER
7675 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
7676#else
7677 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
7678#endif
7679 return TRUE;
7680}
7681
7682static ID
7683formal_argument(struct parser_params *p, ID lhs)
7684{
7685 switch (id_type(lhs)) {
7686 case ID_LOCAL:
7687 break;
7688#ifndef RIPPER
7689 case ID_CONST:
7690 yyerror0("formal argument cannot be a constant");
7691 return 0;
7692 case ID_INSTANCE:
7693 yyerror0("formal argument cannot be an instance variable");
7694 return 0;
7695 case ID_GLOBAL:
7696 yyerror0("formal argument cannot be a global variable");
7697 return 0;
7698 case ID_CLASS:
7699 yyerror0("formal argument cannot be a class variable");
7700 return 0;
7701 default:
7702 yyerror0("formal argument must be local variable");
7703 return 0;
7704#else
7705 default:
7706 lhs = dispatch1(param_error, lhs);
7707 ripper_error(p);
7708 return 0;
7709#endif
7710 }
7711 shadowing_lvar(p, lhs);
7712 return lhs;
7713}
7714
7715static int
7716lvar_defined(struct parser_params *p, ID id)
7717{
7718 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
7719}
7720
7721/* emacsen -*- hack */
7722static long
7723parser_encode_length(struct parser_params *p, const char *name, long len)
7724{
7725 long nlen;
7726
7727 if (len > 5 && name[nlen = len - 5] == '-') {
7728 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
7729 return nlen;
7730 }
7731 if (len > 4 && name[nlen = len - 4] == '-') {
7732 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
7733 return nlen;
7734 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
7735 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
7736 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
7737 return nlen;
7738 }
7739 return len;
7740}
7741
7742static void
7743parser_set_encode(struct parser_params *p, const char *name)
7744{
7745 int idx = rb_enc_find_index(name);
7746 rb_encoding *enc;
7747 VALUE excargs[3];
7748
7749 if (idx < 0) {
7750 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
7751 error:
7752 excargs[0] = rb_eArgError;
7753 excargs[2] = rb_make_backtrace();
7754 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
7755 rb_exc_raise(rb_make_exception(3, excargs));
7756 }
7757 enc = rb_enc_from_index(idx);
7758 if (!rb_enc_asciicompat(enc)) {
7759 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
7760 goto error;
7761 }
7762 p->enc = enc;
7763#ifndef RIPPER
7764 if (p->debug_lines) {
7765 VALUE lines = p->debug_lines;
7766 long i, n = RARRAY_LEN(lines);
7767 for (i = 0; i < n; ++i) {
7768 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
7769 }
7770 }
7771#endif
7772}
7773
7774static int
7775comment_at_top(struct parser_params *p)
7776{
7777 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
7778 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
7779 while (ptr < ptr_end) {
7780 if (!ISSPACE(*ptr)) return 0;
7781 ptr++;
7782 }
7783 return 1;
7784}
7785
7786typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
7787typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
7788
7789static void
7790magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
7791{
7792 if (!comment_at_top(p)) {
7793 return;
7794 }
7795 parser_set_encode(p, val);
7796}
7797
7798static int
7799parser_get_bool(struct parser_params *p, const char *name, const char *val)
7800{
7801 switch (*val) {
7802 case 't': case 'T':
7803 if (strcasecmp(val, "true") == 0) {
7804 return TRUE;
7805 }
7806 break;
7807 case 'f': case 'F':
7808 if (strcasecmp(val, "false") == 0) {
7809 return FALSE;
7810 }
7811 break;
7812 }
7813 rb_compile_warning(p->ruby_sourcefile, p->ruby_sourceline, "invalid value for %s: %s", name, val);
7814 return -1;
7815}
7816
7817static void
7818parser_set_token_info(struct parser_params *p, const char *name, const char *val)
7819{
7820 int b = parser_get_bool(p, name, val);
7821 if (b >= 0) p->token_info_enabled = b;
7822}
7823
7824static void
7825parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
7826{
7827 int b;
7828
7829 if (p->token_seen) {
7830 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
7831 return;
7832 }
7833
7834 b = parser_get_bool(p, name, val);
7835 if (b < 0) return;
7836
7837 if (!p->compile_option)
7838 p->compile_option = rb_obj_hide(rb_ident_hash_new());
7839 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
7840 (b ? Qtrue : Qfalse));
7841}
7842
7843# if WARN_PAST_SCOPE
7844static void
7845parser_set_past_scope(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->past_scope_enabled = b;
7849}
7850# endif
7851
7852struct magic_comment {
7853 const char *name;
7854 rb_magic_comment_setter_t func;
7855 rb_magic_comment_length_t length;
7856};
7857
7858static const struct magic_comment magic_comments[] = {
7859 {"coding", magic_comment_encoding, parser_encode_length},
7860 {"encoding", magic_comment_encoding, parser_encode_length},
7861 {"frozen_string_literal", parser_set_compile_option_flag},
7862 {"warn_indent", parser_set_token_info},
7863# if WARN_PAST_SCOPE
7864 {"warn_past_scope", parser_set_past_scope},
7865# endif
7866};
7867
7868static const char *
7869magic_comment_marker(const char *str, long len)
7870{
7871 long i = 2;
7872
7873 while (i < len) {
7874 switch (str[i]) {
7875 case '-':
7876 if (str[i-1] == '*' && str[i-2] == '-') {
7877 return str + i + 1;
7878 }
7879 i += 2;
7880 break;
7881 case '*':
7882 if (i + 1 >= len) return 0;
7883 if (str[i+1] != '-') {
7884 i += 4;
7885 }
7886 else if (str[i-1] != '-') {
7887 i += 2;
7888 }
7889 else {
7890 return str + i + 2;
7891 }
7892 break;
7893 default:
7894 i += 3;
7895 break;
7896 }
7897 }
7898 return 0;
7899}
7900
7901static int
7902parser_magic_comment(struct parser_params *p, const char *str, long len)
7903{
7904 int indicator = 0;
7905 VALUE name = 0, val = 0;
7906 const char *beg, *end, *vbeg, *vend;
7907#define str_copy(_s, _p, _n) ((_s) \
7908 ? (void)(rb_str_resize((_s), (_n)), \
7909 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
7910 : (void)((_s) = STR_NEW((_p), (_n))))
7911
7912 if (len <= 7) return FALSE;
7913 if (!!(beg = magic_comment_marker(str, len))) {
7914 if (!(end = magic_comment_marker(beg, str + len - beg)))
7915 return FALSE;
7916 indicator = TRUE;
7917 str = beg;
7918 len = end - beg - 3;
7919 }
7920
7921 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
7922 while (len > 0) {
7923 const struct magic_comment *mc = magic_comments;
7924 char *s;
7925 int i;
7926 long n = 0;
7927
7928 for (; len > 0 && *str; str++, --len) {
7929 switch (*str) {
7930 case '\'': case '"': case ':': case ';':
7931 continue;
7932 }
7933 if (!ISSPACE(*str)) break;
7934 }
7935 for (beg = str; len > 0; str++, --len) {
7936 switch (*str) {
7937 case '\'': case '"': case ':': case ';':
7938 break;
7939 default:
7940 if (ISSPACE(*str)) break;
7941 continue;
7942 }
7943 break;
7944 }
7945 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
7946 if (!len) break;
7947 if (*str != ':') {
7948 if (!indicator) return FALSE;
7949 continue;
7950 }
7951
7952 do str++; while (--len > 0 && ISSPACE(*str));
7953 if (!len) break;
7954 if (*str == '"') {
7955 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
7956 if (*str == '\\') {
7957 --len;
7958 ++str;
7959 }
7960 }
7961 vend = str;
7962 if (len) {
7963 --len;
7964 ++str;
7965 }
7966 }
7967 else {
7968 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
7969 vend = str;
7970 }
7971 if (indicator) {
7972 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
7973 }
7974 else {
7975 while (len > 0 && (ISSPACE(*str))) --len, str++;
7976 if (len) return FALSE;
7977 }
7978
7979 n = end - beg;
7980 str_copy(name, beg, n);
7981 s = RSTRING_PTR(name);
7982 for (i = 0; i < n; ++i) {
7983 if (s[i] == '-') s[i] = '_';
7984 }
7985 do {
7986 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
7987 n = vend - vbeg;
7988 if (mc->length) {
7989 n = (*mc->length)(p, vbeg, n);
7990 }
7991 str_copy(val, vbeg, n);
7992 (*mc->func)(p, mc->name, RSTRING_PTR(val));
7993 break;
7994 }
7995 } while (++mc < magic_comments + numberof(magic_comments));
7996#ifdef RIPPER
7997 str_copy(val, vbeg, vend - vbeg);
7998 dispatch2(magic_comment, name, val);
7999#endif
8000 }
8001
8002 return TRUE;
8003}
8004
8005static void
8006set_file_encoding(struct parser_params *p, const char *str, const char *send)
8007{
8008 int sep = 0;
8009 const char *beg = str;
8010 VALUE s;
8011
8012 for (;;) {
8013 if (send - str <= 6) return;
8014 switch (str[6]) {
8015 case 'C': case 'c': str += 6; continue;
8016 case 'O': case 'o': str += 5; continue;
8017 case 'D': case 'd': str += 4; continue;
8018 case 'I': case 'i': str += 3; continue;
8019 case 'N': case 'n': str += 2; continue;
8020 case 'G': case 'g': str += 1; continue;
8021 case '=': case ':':
8022 sep = 1;
8023 str += 6;
8024 break;
8025 default:
8026 str += 6;
8027 if (ISSPACE(*str)) break;
8028 continue;
8029 }
8030 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8031 }
8032 for (;;) {
8033 do {
8034 if (++str >= send) return;
8035 } while (ISSPACE(*str));
8036 if (sep) break;
8037 if (*str != '=' && *str != ':') return;
8038 sep = 1;
8039 str++;
8040 }
8041 beg = str;
8042 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8043 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8044 parser_set_encode(p, RSTRING_PTR(s));
8045 rb_str_resize(s, 0);
8046}
8047
8048static void
8049parser_prepare(struct parser_params *p)
8050{
8051 int c = nextc(p);
8052 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8053 switch (c) {
8054 case '#':
8055 if (peek(p, '!')) p->has_shebang = 1;
8056 break;
8057 case 0xef: /* UTF-8 BOM marker */
8058 if (p->lex.pend - p->lex.pcur >= 2 &&
8059 (unsigned char)p->lex.pcur[0] == 0xbb &&
8060 (unsigned char)p->lex.pcur[1] == 0xbf) {
8061 p->enc = rb_utf8_encoding();
8062 p->lex.pcur += 2;
8063 p->lex.pbeg = p->lex.pcur;
8064 return;
8065 }
8066 break;
8067 case EOF:
8068 return;
8069 }
8070 pushback(p, c);
8071 p->enc = rb_enc_get(p->lex.lastline);
8072}
8073
8074#ifndef RIPPER
8075#define ambiguous_operator(tok, op, syn) ( \
8076 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8077 rb_warning0("even though it seems like "syn""))
8078#else
8079#define ambiguous_operator(tok, op, syn) \
8080 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8081#endif
8082#define warn_balanced(tok, op, syn) ((void) \
8083 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8084 space_seen && !ISSPACE(c) && \
8085 (ambiguous_operator(tok, op, syn), 0)), \
8086 (enum yytokentype)(tok))
8087
8088static VALUE
8089parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8090{
8091 VALUE v;
8092 char *point = &str[seen_point];
8093 size_t fraclen = len-seen_point-1;
8094 memmove(point, point+1, fraclen+1);
8095 v = rb_cstr_to_inum(str, 10, FALSE);
8096 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8097}
8098
8099static enum yytokentype
8100no_digits(struct parser_params *p)
8101{
8102 yyerror0("numeric literal without digits");
8103 if (peek(p, '_')) nextc(p);
8104 /* dummy 0, for tUMINUS_NUM at numeric */
8105 return set_integer_literal(p, INT2FIX(0), 0);
8106}
8107
8108static enum yytokentype
8109parse_numeric(struct parser_params *p, int c)
8110{
8111 int is_float, seen_point, seen_e, nondigit;
8112 int suffix;
8113
8114 is_float = seen_point = seen_e = nondigit = 0;
8115 SET_LEX_STATE(EXPR_END);
8116 newtok(p);
8117 if (c == '-' || c == '+') {
8118 tokadd(p, c);
8119 c = nextc(p);
8120 }
8121 if (c == '0') {
8122 int start = toklen(p);
8123 c = nextc(p);
8124 if (c == 'x' || c == 'X') {
8125 /* hexadecimal */
8126 c = nextc(p);
8127 if (c != -1 && ISXDIGIT(c)) {
8128 do {
8129 if (c == '_') {
8130 if (nondigit) break;
8131 nondigit = c;
8132 continue;
8133 }
8134 if (!ISXDIGIT(c)) break;
8135 nondigit = 0;
8136 tokadd(p, c);
8137 } while ((c = nextc(p)) != -1);
8138 }
8139 pushback(p, c);
8140 tokfix(p);
8141 if (toklen(p) == start) {
8142 return no_digits(p);
8143 }
8144 else if (nondigit) goto trailing_uc;
8145 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8146 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8147 }
8148 if (c == 'b' || c == 'B') {
8149 /* binary */
8150 c = nextc(p);
8151 if (c == '0' || c == '1') {
8152 do {
8153 if (c == '_') {
8154 if (nondigit) break;
8155 nondigit = c;
8156 continue;
8157 }
8158 if (c != '0' && c != '1') break;
8159 nondigit = 0;
8160 tokadd(p, c);
8161 } while ((c = nextc(p)) != -1);
8162 }
8163 pushback(p, c);
8164 tokfix(p);
8165 if (toklen(p) == start) {
8166 return no_digits(p);
8167 }
8168 else if (nondigit) goto trailing_uc;
8169 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8170 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8171 }
8172 if (c == 'd' || c == 'D') {
8173 /* decimal */
8174 c = nextc(p);
8175 if (c != -1 && ISDIGIT(c)) {
8176 do {
8177 if (c == '_') {
8178 if (nondigit) break;
8179 nondigit = c;
8180 continue;
8181 }
8182 if (!ISDIGIT(c)) break;
8183 nondigit = 0;
8184 tokadd(p, c);
8185 } while ((c = nextc(p)) != -1);
8186 }
8187 pushback(p, c);
8188 tokfix(p);
8189 if (toklen(p) == start) {
8190 return no_digits(p);
8191 }
8192 else if (nondigit) goto trailing_uc;
8193 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8194 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8195 }
8196 if (c == '_') {
8197 /* 0_0 */
8198 goto octal_number;
8199 }
8200 if (c == 'o' || c == 'O') {
8201 /* prefixed octal */
8202 c = nextc(p);
8203 if (c == -1 || c == '_' || !ISDIGIT(c)) {
8204 return no_digits(p);
8205 }
8206 }
8207 if (c >= '0' && c <= '7') {
8208 /* octal */
8209 octal_number:
8210 do {
8211 if (c == '_') {
8212 if (nondigit) break;
8213 nondigit = c;
8214 continue;
8215 }
8216 if (c < '0' || c > '9') break;
8217 if (c > '7') goto invalid_octal;
8218 nondigit = 0;
8219 tokadd(p, c);
8220 } while ((c = nextc(p)) != -1);
8221 if (toklen(p) > start) {
8222 pushback(p, c);
8223 tokfix(p);
8224 if (nondigit) goto trailing_uc;
8225 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8226 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8227 }
8228 if (nondigit) {
8229 pushback(p, c);
8230 goto trailing_uc;
8231 }
8232 }
8233 if (c > '7' && c <= '9') {
8234 invalid_octal:
8235 yyerror0("Invalid octal digit");
8236 }
8237 else if (c == '.' || c == 'e' || c == 'E') {
8238 tokadd(p, '0');
8239 }
8240 else {
8241 pushback(p, c);
8242 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8243 return set_integer_literal(p, INT2FIX(0), suffix);
8244 }
8245 }
8246
8247 for (;;) {
8248 switch (c) {
8249 case '0': case '1': case '2': case '3': case '4':
8250 case '5': case '6': case '7': case '8': case '9':
8251 nondigit = 0;
8252 tokadd(p, c);
8253 break;
8254
8255 case '.':
8256 if (nondigit) goto trailing_uc;
8257 if (seen_point || seen_e) {
8258 goto decode_num;
8259 }
8260 else {
8261 int c0 = nextc(p);
8262 if (c0 == -1 || !ISDIGIT(c0)) {
8263 pushback(p, c0);
8264 goto decode_num;
8265 }
8266 c = c0;
8267 }
8268 seen_point = toklen(p);
8269 tokadd(p, '.');
8270 tokadd(p, c);
8271 is_float++;
8272 nondigit = 0;
8273 break;
8274
8275 case 'e':
8276 case 'E':
8277 if (nondigit) {
8278 pushback(p, c);
8279 c = nondigit;
8280 goto decode_num;
8281 }
8282 if (seen_e) {
8283 goto decode_num;
8284 }
8285 nondigit = c;
8286 c = nextc(p);
8287 if (c != '-' && c != '+' && !ISDIGIT(c)) {
8288 pushback(p, c);
8289 nondigit = 0;
8290 goto decode_num;
8291 }
8292 tokadd(p, nondigit);
8293 seen_e++;
8294 is_float++;
8295 tokadd(p, c);
8296 nondigit = (c == '-' || c == '+') ? c : 0;
8297 break;
8298
8299 case '_': /* `_' in number just ignored */
8300 if (nondigit) goto decode_num;
8301 nondigit = c;
8302 break;
8303
8304 default:
8305 goto decode_num;
8306 }
8307 c = nextc(p);
8308 }
8309
8310 decode_num:
8311 pushback(p, c);
8312 if (nondigit) {
8313 trailing_uc:
8314 literal_flush(p, p->lex.pcur - 1);
8315 YYLTYPE loc = RUBY_INIT_YYLLOC();
8316 compile_error(p, "trailing `%c' in number", nondigit);
8317 parser_show_error_line(p, &loc);
8318 }
8319 tokfix(p);
8320 if (is_float) {
8321 enum yytokentype type = tFLOAT;
8322 VALUE v;
8323
8324 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8325 if (suffix & NUM_SUFFIX_R) {
8326 type = tRATIONAL;
8327 v = parse_rational(p, tok(p), toklen(p), seen_point);
8328 }
8329 else {
8330 double d = strtod(tok(p), 0);
8331 if (errno == ERANGE) {
8332 rb_warning1("Float %s out of range", WARN_S(tok(p)));
8333 errno = 0;
8334 }
8335 v = DBL2NUM(d);
8336 }
8337 return set_number_literal(p, v, type, suffix);
8338 }
8339 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8340 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8341}
8342
8343static enum yytokentype
8344parse_qmark(struct parser_params *p, int space_seen)
8345{
8346 rb_encoding *enc;
8347 register int c;
8348 VALUE lit;
8349
8350 if (IS_END()) {
8351 SET_LEX_STATE(EXPR_VALUE);
8352 return '?';
8353 }
8354 c = nextc(p);
8355 if (c == -1) {
8356 compile_error(p, "incomplete character syntax");
8357 return 0;
8358 }
8359 if (rb_enc_isspace(c, p->enc)) {
8360 if (!IS_ARG()) {
8361 int c2 = escaped_control_code(c);
8362 if (c2) {
8363 WARN_SPACE_CHAR(c2, "?");
8364 }
8365 }
8366 ternary:
8367 pushback(p, c);
8368 SET_LEX_STATE(EXPR_VALUE);
8369 return '?';
8370 }
8371 newtok(p);
8372 enc = p->enc;
8373 if (!parser_isascii(p)) {
8374 if (tokadd_mbchar(p, c) == -1) return 0;
8375 }
8376 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8377 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8378 if (space_seen) {
8379 const char *start = p->lex.pcur - 1, *ptr = start;
8380 do {
8381 int n = parser_precise_mbclen(p, ptr);
8382 if (n < 0) return -1;
8383 ptr += n;
8384 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8385 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8386 " a conditional operator, put a space after `?'",
8387 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8388 }
8389 goto ternary;
8390 }
8391 else if (c == '\\') {
8392 if (peek(p, 'u')) {
8393 nextc(p);
8394 enc = rb_utf8_encoding();
8395 tokadd_utf8(p, &enc, -1, 0, 0);
8396 }
8397 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8398 nextc(p);
8399 if (tokadd_mbchar(p, c) == -1) return 0;
8400 }
8401 else {
8402 c = read_escape(p, 0, &enc);
8403 tokadd(p, c);
8404 }
8405 }
8406 else {
8407 tokadd(p, c);
8408 }
8409 tokfix(p);
8410 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8411 set_yylval_str(lit);
8412 SET_LEX_STATE(EXPR_END);
8413 return tCHAR;
8414}
8415
8416static enum yytokentype
8417parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8418{
8419 register int c;
8420 const char *ptok = p->lex.pcur;
8421
8422 if (IS_BEG()) {
8423 int term;
8424 int paren;
8425
8426 c = nextc(p);
8427 quotation:
8428 if (c == -1 || !ISALNUM(c)) {
8429 term = c;
8430 c = 'Q';
8431 }
8432 else {
8433 term = nextc(p);
8434 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8435 yyerror0("unknown type of %string");
8436 return 0;
8437 }
8438 }
8439 if (c == -1 || term == -1) {
8440 compile_error(p, "unterminated quoted string meets end of file");
8441 return 0;
8442 }
8443 paren = term;
8444 if (term == '(') term = ')';
8445 else if (term == '[') term = ']';
8446 else if (term == '{') term = '}';
8447 else if (term == '<') term = '>';
8448 else paren = 0;
8449
8450 p->lex.ptok = ptok-1;
8451 switch (c) {
8452 case 'Q':
8453 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8454 return tSTRING_BEG;
8455
8456 case 'q':
8457 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8458 return tSTRING_BEG;
8459
8460 case 'W':
8461 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8462 return tWORDS_BEG;
8463
8464 case 'w':
8465 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8466 return tQWORDS_BEG;
8467
8468 case 'I':
8469 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8470 return tSYMBOLS_BEG;
8471
8472 case 'i':
8473 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8474 return tQSYMBOLS_BEG;
8475
8476 case 'x':
8477 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8478 return tXSTRING_BEG;
8479
8480 case 'r':
8481 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8482 return tREGEXP_BEG;
8483
8484 case 's':
8485 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8486 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8487 return tSYMBEG;
8488
8489 default:
8490 yyerror0("unknown type of %string");
8491 return 0;
8492 }
8493 }
8494 if ((c = nextc(p)) == '=') {
8495 set_yylval_id('%');
8496 SET_LEX_STATE(EXPR_BEG);
8497 return tOP_ASGN;
8498 }
8499 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8500 goto quotation;
8501 }
8502 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8503 pushback(p, c);
8504 return warn_balanced('%', "%%", "string literal");
8505}
8506
8507static int
8508tokadd_ident(struct parser_params *p, int c)
8509{
8510 do {
8511 if (tokadd_mbchar(p, c) == -1) return -1;
8512 c = nextc(p);
8513 } while (parser_is_identchar(p));
8514 pushback(p, c);
8515 return 0;
8516}
8517
8518static ID
8519tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8520{
8521 ID ident = TOK_INTERN();
8522
8523 set_yylval_name(ident);
8524
8525 return ident;
8526}
8527
8528static int
8529parse_numvar(struct parser_params *p)
8530{
8531 size_t len;
8532 int overflow;
8533 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8534 const unsigned long nth_ref_max =
8535 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8536 /* NTH_REF is left-shifted to be ORed with back-ref flag and
8537 * turned into a Fixnum, in compile.c */
8538
8539 if (overflow || n > nth_ref_max) {
8540 /* compile_error()? */
8541 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8542 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8543 }
8544 else {
8545 return (int)n;
8546 }
8547}
8548
8549static enum yytokentype
8550parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8551{
8552 const char *ptr = p->lex.pcur;
8553 register int c;
8554
8555 SET_LEX_STATE(EXPR_END);
8556 p->lex.ptok = ptr - 1; /* from '$' */
8557 newtok(p);
8558 c = nextc(p);
8559 switch (c) {
8560 case '_': /* $_: last read line string */
8561 c = nextc(p);
8562 if (parser_is_identchar(p)) {
8563 tokadd(p, '$');
8564 tokadd(p, '_');
8565 break;
8566 }
8567 pushback(p, c);
8568 c = '_';
8569 /* fall through */
8570 case '~': /* $~: match-data */
8571 case '*': /* $*: argv */
8572 case '$': /* $$: pid */
8573 case '?': /* $?: last status */
8574 case '!': /* $!: error string */
8575 case '@': /* $@: error position */
8576 case '/': /* $/: input record separator */
8577 case '\\': /* $\: output record separator */
8578 case ';': /* $;: field separator */
8579 case ',': /* $,: output field separator */
8580 case '.': /* $.: last read line number */
8581 case '=': /* $=: ignorecase */
8582 case ':': /* $:: load path */
8583 case '<': /* $<: reading filename */
8584 case '>': /* $>: default output handle */
8585 case '\"': /* $": already loaded files */
8586 tokadd(p, '$');
8587 tokadd(p, c);
8588 goto gvar;
8589
8590 case '-':
8591 tokadd(p, '$');
8592 tokadd(p, c);
8593 c = nextc(p);
8594 if (parser_is_identchar(p)) {
8595 if (tokadd_mbchar(p, c) == -1) return 0;
8596 }
8597 else {
8598 pushback(p, c);
8599 pushback(p, '-');
8600 return '$';
8601 }
8602 gvar:
8603 set_yylval_name(TOK_INTERN());
8604 return tGVAR;
8605
8606 case '&': /* $&: last match */
8607 case '`': /* $`: string before last match */
8608 case '\'': /* $': string after last match */
8609 case '+': /* $+: string matches last paren. */
8610 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
8611 tokadd(p, '$');
8612 tokadd(p, c);
8613 goto gvar;
8614 }
8615 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
8616 return tBACK_REF;
8617
8618 case '1': case '2': case '3':
8619 case '4': case '5': case '6':
8620 case '7': case '8': case '9':
8621 tokadd(p, '$');
8622 do {
8623 tokadd(p, c);
8624 c = nextc(p);
8625 } while (c != -1 && ISDIGIT(c));
8626 pushback(p, c);
8627 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
8628 tokfix(p);
8629 set_yylval_node(NEW_NTH_REF(parse_numvar(p), &_cur_loc));
8630 return tNTH_REF;
8631
8632 default:
8633 if (!parser_is_identchar(p)) {
8634 YYLTYPE loc = RUBY_INIT_YYLLOC();
8635 if (c == -1 || ISSPACE(c)) {
8636 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
8637 }
8638 else {
8639 pushback(p, c);
8640 compile_error(p, "`$%c' is not allowed as a global variable name", c);
8641 }
8642 parser_show_error_line(p, &loc);
8643 set_yylval_noname();
8644 return tGVAR;
8645 }
8646 /* fall through */
8647 case '0':
8648 tokadd(p, '$');
8649 }
8650
8651 if (tokadd_ident(p, c)) return 0;
8652 SET_LEX_STATE(EXPR_END);
8653 tokenize_ident(p, last_state);
8654 return tGVAR;
8655}
8656
8657#ifndef RIPPER
8658static bool
8659parser_numbered_param(struct parser_params *p, int n)
8660{
8661 if (n < 0) return false;
8662
8663 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
8664 return false;
8665 }
8666 if (p->max_numparam == ORDINAL_PARAM) {
8667 compile_error(p, "ordinary parameter is defined");
8668 return false;
8669 }
8670 struct vtable *args = p->lvtbl->args;
8671 if (p->max_numparam < n) {
8672 p->max_numparam = n;
8673 }
8674 while (n > args->pos) {
8675 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
8676 }
8677 return true;
8678}
8679#endif
8680
8681static enum yytokentype
8682parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
8683{
8684 const char *ptr = p->lex.pcur;
8685 enum yytokentype result = tIVAR;
8686 register int c = nextc(p);
8687 YYLTYPE loc;
8688
8689 p->lex.ptok = ptr - 1; /* from '@' */
8690 newtok(p);
8691 tokadd(p, '@');
8692 if (c == '@') {
8693 result = tCVAR;
8694 tokadd(p, '@');
8695 c = nextc(p);
8696 }
8697 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
8698 if (c == -1 || !parser_is_identchar(p)) {
8699 pushback(p, c);
8700 RUBY_SET_YYLLOC(loc);
8701 if (result == tIVAR) {
8702 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
8703 }
8704 else {
8705 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
8706 }
8707 parser_show_error_line(p, &loc);
8708 set_yylval_noname();
8709 SET_LEX_STATE(EXPR_END);
8710 return result;
8711 }
8712 else if (ISDIGIT(c)) {
8713 pushback(p, c);
8714 RUBY_SET_YYLLOC(loc);
8715 if (result == tIVAR) {
8716 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
8717 }
8718 else {
8719 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
8720 }
8721 parser_show_error_line(p, &loc);
8722 set_yylval_noname();
8723 SET_LEX_STATE(EXPR_END);
8724 return result;
8725 }
8726
8727 if (tokadd_ident(p, c)) return 0;
8728 tokenize_ident(p, last_state);
8729 return result;
8730}
8731
8732static enum yytokentype
8733parse_ident(struct parser_params *p, int c, int cmd_state)
8734{
8735 enum yytokentype result;
8736 int mb = ENC_CODERANGE_7BIT;
8737 const enum lex_state_e last_state = p->lex.state;
8738 ID ident;
8739
8740 do {
8741 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
8742 if (tokadd_mbchar(p, c) == -1) return 0;
8743 c = nextc(p);
8744 } while (parser_is_identchar(p));
8745 if ((c == '!' || c == '?') && !peek(p, '=')) {
8746 result = tFID;
8747 tokadd(p, c);
8748 }
8749 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
8750 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
8751 result = tIDENTIFIER;
8752 tokadd(p, c);
8753 }
8754 else {
8755 result = tCONSTANT; /* assume provisionally */
8756 pushback(p, c);
8757 }
8758 tokfix(p);
8759
8760 if (IS_LABEL_POSSIBLE()) {
8761 if (IS_LABEL_SUFFIX(0)) {
8762 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8763 nextc(p);
8764 set_yylval_name(TOK_INTERN());
8765 return tLABEL;
8766 }
8767 }
8768 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
8769 const struct kwtable *kw;
8770
8771 /* See if it is a reserved word. */
8772 kw = rb_reserved_word(tok(p), toklen(p));
8773 if (kw) {
8774 enum lex_state_e state = p->lex.state;
8775 if (IS_lex_state_for(state, EXPR_FNAME)) {
8776 SET_LEX_STATE(EXPR_ENDFN);
8777 set_yylval_name(rb_intern2(tok(p), toklen(p)));
8778 return kw->id[0];
8779 }
8780 SET_LEX_STATE(kw->state);
8781 if (IS_lex_state(EXPR_BEG)) {
8782 p->command_start = TRUE;
8783 }
8784 if (kw->id[0] == keyword_do) {
8785 if (lambda_beginning_p()) {
8786 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
8787 return keyword_do_LAMBDA;
8788 }
8789 if (COND_P()) return keyword_do_cond;
8790 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
8791 return keyword_do_block;
8792 return keyword_do;
8793 }
8794 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
8795 return kw->id[0];
8796 else {
8797 if (kw->id[0] != kw->id[1])
8798 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
8799 return kw->id[1];
8800 }
8801 }
8802 }
8803
8804 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
8805 if (cmd_state) {
8806 SET_LEX_STATE(EXPR_CMDARG);
8807 }
8808 else {
8809 SET_LEX_STATE(EXPR_ARG);
8810 }
8811 }
8812 else if (p->lex.state == EXPR_FNAME) {
8813 SET_LEX_STATE(EXPR_ENDFN);
8814 }
8815 else {
8816 SET_LEX_STATE(EXPR_END);
8817 }
8818
8819 ident = tokenize_ident(p, last_state);
8820 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
8821 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
8822 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
8823 lvar_defined(p, ident)) {
8824 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
8825 }
8826 return result;
8827}
8828
8829static enum yytokentype
8830parser_yylex(struct parser_params *p)
8831{
8832 register int c;
8833 int space_seen = 0;
8834 int cmd_state;
8835 int label;
8836 enum lex_state_e last_state;
8837 int fallthru = FALSE;
8838 int token_seen = p->token_seen;
8839
8840 if (p->lex.strterm) {
8841 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
8842 return here_document(p, &p->lex.strterm->u.heredoc);
8843 }
8844 else {
8845 token_flush(p);
8846 return parse_string(p, &p->lex.strterm->u.literal);
8847 }
8848 }
8849 cmd_state = p->command_start;
8850 p->command_start = FALSE;
8851 p->token_seen = TRUE;
8852 retry:
8853 last_state = p->lex.state;
8854#ifndef RIPPER
8855 token_flush(p);
8856#endif
8857 switch (c = nextc(p)) {
8858 case '\0': /* NUL */
8859 case '\004': /* ^D */
8860 case '\032': /* ^Z */
8861 case -1: /* end of script. */
8862 return 0;
8863
8864 /* white spaces */
8865 case ' ': case '\t': case '\f': case '\r':
8866 case '\13': /* '\v' */
8867 space_seen = 1;
8868#ifdef RIPPER
8869 while ((c = nextc(p))) {
8870 switch (c) {
8871 case ' ': case '\t': case '\f': case '\r':
8872 case '\13': /* '\v' */
8873 break;
8874 default:
8875 goto outofloop;
8876 }
8877 }
8878 outofloop:
8879 pushback(p, c);
8880 dispatch_scan_event(p, tSP);
8881#endif
8882 goto retry;
8883
8884 case '#': /* it's a comment */
8885 p->token_seen = token_seen;
8886 /* no magic_comment in shebang line */
8887 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
8888 if (comment_at_top(p)) {
8889 set_file_encoding(p, p->lex.pcur, p->lex.pend);
8890 }
8891 }
8892 lex_goto_eol(p);
8893 dispatch_scan_event(p, tCOMMENT);
8894 fallthru = TRUE;
8895 /* fall through */
8896 case '\n':
8897 p->token_seen = token_seen;
8898 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
8899 !IS_lex_state(EXPR_LABELED));
8900 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
8901 if (!fallthru) {
8902 dispatch_scan_event(p, tIGNORED_NL);
8903 }
8904 fallthru = FALSE;
8905 if (!c && p->in_kwarg) {
8906 goto normal_newline;
8907 }
8908 goto retry;
8909 }
8910 while (1) {
8911 switch (c = nextc(p)) {
8912 case ' ': case '\t': case '\f': case '\r':
8913 case '\13': /* '\v' */
8914 space_seen = 1;
8915 break;
8916 case '#':
8917 pushback(p, c);
8918 if (space_seen) dispatch_scan_event(p, tSP);
8919 goto retry;
8920 case '&':
8921 case '.': {
8922 dispatch_delayed_token(p, tIGNORED_NL);
8923 if (peek(p, '.') == (c == '&')) {
8924 pushback(p, c);
8925 dispatch_scan_event(p, tSP);
8926 goto retry;
8927 }
8928 }
8929 default:
8930 p->ruby_sourceline--;
8931 p->lex.nextline = p->lex.lastline;
8932 case -1: /* EOF no decrement*/
8933#ifndef RIPPER
8934 if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
8935 p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
8936 p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
8937 pushback(p, 1); /* always pushback */
8938 p->lex.ptok = p->lex.pcur;
8939#else
8940 lex_goto_eol(p);
8941 if (c != -1) {
8942 p->lex.ptok = p->lex.pcur;
8943 }
8944#endif
8945 goto normal_newline;
8946 }
8947 }
8948 normal_newline:
8949 p->command_start = TRUE;
8950 SET_LEX_STATE(EXPR_BEG);
8951 return '\n';
8952
8953 case '*':
8954 if ((c = nextc(p)) == '*') {
8955 if ((c = nextc(p)) == '=') {
8956 set_yylval_id(idPow);
8957 SET_LEX_STATE(EXPR_BEG);
8958 return tOP_ASGN;
8959 }
8960 pushback(p, c);
8961 if (IS_SPCARG(c)) {
8962 rb_warning0("`**' interpreted as argument prefix");
8963 c = tDSTAR;
8964 }
8965 else if (IS_BEG()) {
8966 c = tDSTAR;
8967 }
8968 else {
8969 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
8970 }
8971 }
8972 else {
8973 if (c == '=') {
8974 set_yylval_id('*');
8975 SET_LEX_STATE(EXPR_BEG);
8976 return tOP_ASGN;
8977 }
8978 pushback(p, c);
8979 if (IS_SPCARG(c)) {
8980 rb_warning0("`*' interpreted as argument prefix");
8981 c = tSTAR;
8982 }
8983 else if (IS_BEG()) {
8984 c = tSTAR;
8985 }
8986 else {
8987 c = warn_balanced('*', "*", "argument prefix");
8988 }
8989 }
8990 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8991 return c;
8992
8993 case '!':
8994 c = nextc(p);
8995 if (IS_AFTER_OPERATOR()) {
8996 SET_LEX_STATE(EXPR_ARG);
8997 if (c == '@') {
8998 return '!';
8999 }
9000 }
9001 else {
9002 SET_LEX_STATE(EXPR_BEG);
9003 }
9004 if (c == '=') {
9005 return tNEQ;
9006 }
9007 if (c == '~') {
9008 return tNMATCH;
9009 }
9010 pushback(p, c);
9011 return '!';
9012
9013 case '=':
9014 if (was_bol(p)) {
9015 /* skip embedded rd document */
9016 if (word_match_p(p, "begin", 5)) {
9017 int first_p = TRUE;
9018
9019 lex_goto_eol(p);
9020 dispatch_scan_event(p, tEMBDOC_BEG);
9021 for (;;) {
9022 lex_goto_eol(p);
9023 if (!first_p) {
9024 dispatch_scan_event(p, tEMBDOC);
9025 }
9026 first_p = FALSE;
9027 c = nextc(p);
9028 if (c == -1) {
9029 compile_error(p, "embedded document meets end of file");
9030 return 0;
9031 }
9032 if (c == '=' && word_match_p(p, "end", 3)) {
9033 break;
9034 }
9035 pushback(p, c);
9036 }
9037 lex_goto_eol(p);
9038 dispatch_scan_event(p, tEMBDOC_END);
9039 goto retry;
9040 }
9041 }
9042
9043 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9044 if ((c = nextc(p)) == '=') {
9045 if ((c = nextc(p)) == '=') {
9046 return tEQQ;
9047 }
9048 pushback(p, c);
9049 return tEQ;
9050 }
9051 if (c == '~') {
9052 return tMATCH;
9053 }
9054 else if (c == '>') {
9055 return tASSOC;
9056 }
9057 pushback(p, c);
9058 return '=';
9059
9060 case '<':
9061 c = nextc(p);
9062 if (c == '<' &&
9063 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9064 !IS_END() &&
9065 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9066 int token = heredoc_identifier(p);
9067 if (token) return token < 0 ? 0 : token;
9068 }
9069 if (IS_AFTER_OPERATOR()) {
9070 SET_LEX_STATE(EXPR_ARG);
9071 }
9072 else {
9073 if (IS_lex_state(EXPR_CLASS))
9074 p->command_start = TRUE;
9075 SET_LEX_STATE(EXPR_BEG);
9076 }
9077 if (c == '=') {
9078 if ((c = nextc(p)) == '>') {
9079 return tCMP;
9080 }
9081 pushback(p, c);
9082 return tLEQ;
9083 }
9084 if (c == '<') {
9085 if ((c = nextc(p)) == '=') {
9086 set_yylval_id(idLTLT);
9087 SET_LEX_STATE(EXPR_BEG);
9088 return tOP_ASGN;
9089 }
9090 pushback(p, c);
9091 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9092 }
9093 pushback(p, c);
9094 return '<';
9095
9096 case '>':
9097 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9098 if ((c = nextc(p)) == '=') {
9099 return tGEQ;
9100 }
9101 if (c == '>') {
9102 if ((c = nextc(p)) == '=') {
9103 set_yylval_id(idGTGT);
9104 SET_LEX_STATE(EXPR_BEG);
9105 return tOP_ASGN;
9106 }
9107 pushback(p, c);
9108 return tRSHFT;
9109 }
9110 pushback(p, c);
9111 return '>';
9112
9113 case '"':
9114 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9115 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9116 p->lex.ptok = p->lex.pcur-1;
9117 return tSTRING_BEG;
9118
9119 case '`':
9120 if (IS_lex_state(EXPR_FNAME)) {
9121 SET_LEX_STATE(EXPR_ENDFN);
9122 return c;
9123 }
9124 if (IS_lex_state(EXPR_DOT)) {
9125 if (cmd_state)
9126 SET_LEX_STATE(EXPR_CMDARG);
9127 else
9128 SET_LEX_STATE(EXPR_ARG);
9129 return c;
9130 }
9131 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9132 return tXSTRING_BEG;
9133
9134 case '\'':
9135 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9136 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9137 p->lex.ptok = p->lex.pcur-1;
9138 return tSTRING_BEG;
9139
9140 case '?':
9141 return parse_qmark(p, space_seen);
9142
9143 case '&':
9144 if ((c = nextc(p)) == '&') {
9145 SET_LEX_STATE(EXPR_BEG);
9146 if ((c = nextc(p)) == '=') {
9147 set_yylval_id(idANDOP);
9148 SET_LEX_STATE(EXPR_BEG);
9149 return tOP_ASGN;
9150 }
9151 pushback(p, c);
9152 return tANDOP;
9153 }
9154 else if (c == '=') {
9155 set_yylval_id('&');
9156 SET_LEX_STATE(EXPR_BEG);
9157 return tOP_ASGN;
9158 }
9159 else if (c == '.') {
9160 set_yylval_id(idANDDOT);
9161 SET_LEX_STATE(EXPR_DOT);
9162 return tANDDOT;
9163 }
9164 pushback(p, c);
9165 if (IS_SPCARG(c)) {
9166 if ((c != ':') ||
9167 (c = peekc_n(p, 1)) == -1 ||
9168 !(c == '\'' || c == '"' ||
9169 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9170 rb_warning0("`&' interpreted as argument prefix");
9171 }
9172 c = tAMPER;
9173 }
9174 else if (IS_BEG()) {
9175 c = tAMPER;
9176 }
9177 else {
9178 c = warn_balanced('&', "&", "argument prefix");
9179 }
9180 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9181 return c;
9182
9183 case '|':
9184 if ((c = nextc(p)) == '|') {
9185 SET_LEX_STATE(EXPR_BEG);
9186 if ((c = nextc(p)) == '=') {
9187 set_yylval_id(idOROP);
9188 SET_LEX_STATE(EXPR_BEG);
9189 return tOP_ASGN;
9190 }
9191 pushback(p, c);
9192 if (IS_lex_state_for(last_state, EXPR_BEG)) {
9193 c = '|';
9194 pushback(p, '|');
9195 return c;
9196 }
9197 return tOROP;
9198 }
9199 if (c == '=') {
9200 set_yylval_id('|');
9201 SET_LEX_STATE(EXPR_BEG);
9202 return tOP_ASGN;
9203 }
9204 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9205 pushback(p, c);
9206 return '|';
9207
9208 case '+':
9209 c = nextc(p);
9210 if (IS_AFTER_OPERATOR()) {
9211 SET_LEX_STATE(EXPR_ARG);
9212 if (c == '@') {
9213 return tUPLUS;
9214 }
9215 pushback(p, c);
9216 return '+';
9217 }
9218 if (c == '=') {
9219 set_yylval_id('+');
9220 SET_LEX_STATE(EXPR_BEG);
9221 return tOP_ASGN;
9222 }
9223 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9224 SET_LEX_STATE(EXPR_BEG);
9225 pushback(p, c);
9226 if (c != -1 && ISDIGIT(c)) {
9227 return parse_numeric(p, '+');
9228 }
9229 return tUPLUS;
9230 }
9231 SET_LEX_STATE(EXPR_BEG);
9232 pushback(p, c);
9233 return warn_balanced('+', "+", "unary operator");
9234
9235 case '-':
9236 c = nextc(p);
9237 if (IS_AFTER_OPERATOR()) {
9238 SET_LEX_STATE(EXPR_ARG);
9239 if (c == '@') {
9240 return tUMINUS;
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 (c == '>') {
9251 SET_LEX_STATE(EXPR_ENDFN);
9252 return tLAMBDA;
9253 }
9254 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9255 SET_LEX_STATE(EXPR_BEG);
9256 pushback(p, c);
9257 if (c != -1 && ISDIGIT(c)) {
9258 return tUMINUS_NUM;
9259 }
9260 return tUMINUS;
9261 }
9262 SET_LEX_STATE(EXPR_BEG);
9263 pushback(p, c);
9264 return warn_balanced('-', "-", "unary operator");
9265
9266 case '.': {
9267 int is_beg = IS_BEG();
9268 SET_LEX_STATE(EXPR_BEG);
9269 if ((c = nextc(p)) == '.') {
9270 if ((c = nextc(p)) == '.') {
9271 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9272 rb_warn0("... at EOL, should be parenthesized?");
9273 }
9274 return is_beg ? tBDOT3 : tDOT3;
9275 }
9276 pushback(p, c);
9277 return is_beg ? tBDOT2 : tDOT2;
9278 }
9279 pushback(p, c);
9280 if (c != -1 && ISDIGIT(c)) {
9281 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9282 parse_numeric(p, '.');
9283 if (ISDIGIT(prev)) {
9284 yyerror0("unexpected fraction part after numeric literal");
9285 }
9286 else {
9287 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9288 }
9289 SET_LEX_STATE(EXPR_END);
9290 p->lex.ptok = p->lex.pcur;
9291 goto retry;
9292 }
9293 set_yylval_id('.');
9294 SET_LEX_STATE(EXPR_DOT);
9295 return '.';
9296 }
9297
9298 case '0': case '1': case '2': case '3': case '4':
9299 case '5': case '6': case '7': case '8': case '9':
9300 return parse_numeric(p, c);
9301
9302 case ')':
9303 COND_POP();
9304 CMDARG_POP();
9305 SET_LEX_STATE(EXPR_ENDFN);
9306 p->lex.paren_nest--;
9307 return c;
9308
9309 case ']':
9310 COND_POP();
9311 CMDARG_POP();
9312 SET_LEX_STATE(EXPR_END);
9313 p->lex.paren_nest--;
9314 return c;
9315
9316 case '}':
9317 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9318 if (!p->lex.brace_nest--) return tSTRING_DEND;
9319 COND_POP();
9320 CMDARG_POP();
9321 SET_LEX_STATE(EXPR_END);
9322 p->lex.paren_nest--;
9323 return c;
9324
9325 case ':':
9326 c = nextc(p);
9327 if (c == ':') {
9328 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9329 SET_LEX_STATE(EXPR_BEG);
9330 return tCOLON3;
9331 }
9332 set_yylval_id(idCOLON2);
9333 SET_LEX_STATE(EXPR_DOT);
9334 return tCOLON2;
9335 }
9336 if (IS_END() || ISSPACE(c) || c == '#') {
9337 pushback(p, c);
9338 c = warn_balanced(':', ":", "symbol literal");
9339 SET_LEX_STATE(EXPR_BEG);
9340 return c;
9341 }
9342 switch (c) {
9343 case '\'':
9344 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9345 break;
9346 case '"':
9347 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9348 break;
9349 default:
9350 pushback(p, c);
9351 break;
9352 }
9353 SET_LEX_STATE(EXPR_FNAME);
9354 return tSYMBEG;
9355
9356 case '/':
9357 if (IS_BEG()) {
9358 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9359 return tREGEXP_BEG;
9360 }
9361 if ((c = nextc(p)) == '=') {
9362 set_yylval_id('/');
9363 SET_LEX_STATE(EXPR_BEG);
9364 return tOP_ASGN;
9365 }
9366 pushback(p, c);
9367 if (IS_SPCARG(c)) {
9368 arg_ambiguous(p, '/');
9369 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9370 return tREGEXP_BEG;
9371 }
9372 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9373 return warn_balanced('/', "/", "regexp literal");
9374
9375 case '^':
9376 if ((c = nextc(p)) == '=') {
9377 set_yylval_id('^');
9378 SET_LEX_STATE(EXPR_BEG);
9379 return tOP_ASGN;
9380 }
9381 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9382 pushback(p, c);
9383 return '^';
9384
9385 case ';':
9386 SET_LEX_STATE(EXPR_BEG);
9387 p->command_start = TRUE;
9388 return ';';
9389
9390 case ',':
9391 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9392 return ',';
9393
9394 case '~':
9395 if (IS_AFTER_OPERATOR()) {
9396 if ((c = nextc(p)) != '@') {
9397 pushback(p, c);
9398 }
9399 SET_LEX_STATE(EXPR_ARG);
9400 }
9401 else {
9402 SET_LEX_STATE(EXPR_BEG);
9403 }
9404 return '~';
9405
9406 case '(':
9407 if (IS_BEG()) {
9408 c = tLPAREN;
9409 }
9410 else if (!space_seen) {
9411 /* foo( ... ) => method call, no ambiguity */
9412 }
9413 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9414 c = tLPAREN_ARG;
9415 }
9416 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9417 rb_warning0("parentheses after method name is interpreted as "
9418 "an argument list, not a decomposed argument");
9419 }
9420 p->lex.paren_nest++;
9421 COND_PUSH(0);
9422 CMDARG_PUSH(0);
9423 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9424 return c;
9425
9426 case '[':
9427 p->lex.paren_nest++;
9428 if (IS_AFTER_OPERATOR()) {
9429 if ((c = nextc(p)) == ']') {
9430 SET_LEX_STATE(EXPR_ARG);
9431 if ((c = nextc(p)) == '=') {
9432 return tASET;
9433 }
9434 pushback(p, c);
9435 return tAREF;
9436 }
9437 pushback(p, c);
9438 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9439 return '[';
9440 }
9441 else if (IS_BEG()) {
9442 c = tLBRACK;
9443 }
9444 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9445 c = tLBRACK;
9446 }
9447 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9448 COND_PUSH(0);
9449 CMDARG_PUSH(0);
9450 return c;
9451
9452 case '{':
9453 ++p->lex.brace_nest;
9454 if (lambda_beginning_p())
9455 c = tLAMBEG;
9456 else if (IS_lex_state(EXPR_LABELED))
9457 c = tLBRACE; /* hash */
9458 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9459 c = '{'; /* block (primary) */
9460 else if (IS_lex_state(EXPR_ENDARG))
9461 c = tLBRACE_ARG; /* block (expr) */
9462 else
9463 c = tLBRACE; /* hash */
9464 if (c != tLBRACE) {
9465 p->command_start = TRUE;
9466 SET_LEX_STATE(EXPR_BEG);
9467 }
9468 else {
9469 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9470 }
9471 ++p->lex.paren_nest; /* after lambda_beginning_p() */
9472 COND_PUSH(0);
9473 CMDARG_PUSH(0);
9474 return c;
9475
9476 case '\\':
9477 c = nextc(p);
9478 if (c == '\n') {
9479 space_seen = 1;
9480 dispatch_scan_event(p, tSP);
9481 goto retry; /* skip \\n */
9482 }
9483 if (c == ' ') return tSP;
9484 if (ISSPACE(c)) return c;
9485 pushback(p, c);
9486 return '\\';
9487
9488 case '%':
9489 return parse_percent(p, space_seen, last_state);
9490
9491 case '$':
9492 return parse_gvar(p, last_state);
9493
9494 case '@':
9495 return parse_atmark(p, last_state);
9496
9497 case '_':
9498 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9499 p->ruby__end__seen = 1;
9500 p->eofp = 1;
9501#ifndef RIPPER
9502 return -1;
9503#else
9504 lex_goto_eol(p);
9505 dispatch_scan_event(p, k__END__);
9506 return 0;
9507#endif
9508 }
9509 newtok(p);
9510 break;
9511
9512 default:
9513 if (!parser_is_identchar(p)) {
9514 compile_error(p, "Invalid char `\\x%02X' in expression", c);
9515 token_flush(p);
9516 goto retry;
9517 }
9518
9519 newtok(p);
9520 break;
9521 }
9522
9523 return parse_ident(p, c, cmd_state);
9524}
9525
9526static enum yytokentype
9527yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9528{
9529 enum yytokentype t;
9530
9531 p->lval = lval;
9532 lval->val = Qundef;
9533 t = parser_yylex(p);
9534
9535 if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9536 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9537 else
9538 RUBY_SET_YYLLOC(*yylloc);
9539
9540 if (has_delayed_token(p))
9541 dispatch_delayed_token(p, t);
9542 else if (t != 0)
9543 dispatch_scan_event(p, t);
9544
9545 return t;
9546}
9547
9548#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9549
9550static NODE*
9551node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9552{
9553 NODE *n = rb_ast_newnode(p->ast, type);
9554
9555 rb_node_init(n, type, a0, a1, a2);
9556
9557 nd_set_loc(n, loc);
9558 nd_set_node_id(n, parser_get_node_id(p));
9559 return n;
9560}
9561
9562static NODE *
9563nd_set_loc(NODE *nd, const YYLTYPE *loc)
9564{
9565 nd->nd_loc = *loc;
9566 nd_set_line(nd, loc->beg_pos.lineno);
9567 return nd;
9568}
9569
9570#ifndef RIPPER
9571static enum node_type
9572nodetype(NODE *node) /* for debug */
9573{
9574 return (enum node_type)nd_type(node);
9575}
9576
9577static int
9578nodeline(NODE *node)
9579{
9580 return nd_line(node);
9581}
9582
9583static NODE*
9584newline_node(NODE *node)
9585{
9586 if (node) {
9587 node = remove_begin(node);
9588 node->flags |= NODE_FL_NEWLINE;
9589 }
9590 return node;
9591}
9592
9593static void
9594fixpos(NODE *node, NODE *orig)
9595{
9596 if (!node) return;
9597 if (!orig) return;
9598 nd_set_line(node, nd_line(orig));
9599}
9600
9601static void
9602parser_warning(struct parser_params *p, NODE *node, const char *mesg)
9603{
9604 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9605}
9606
9607static void
9608parser_warn(struct parser_params *p, NODE *node, const char *mesg)
9609{
9610 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9611}
9612
9613static NODE*
9614block_append(struct parser_params *p, NODE *head, NODE *tail)
9615{
9616 NODE *end, *h = head, *nd;
9617
9618 if (tail == 0) return head;
9619
9620 if (h == 0) return tail;
9621 switch (nd_type(h)) {
9622 case NODE_LIT:
9623 case NODE_STR:
9624 case NODE_SELF:
9625 case NODE_TRUE:
9626 case NODE_FALSE:
9627 case NODE_NIL:
9628 parser_warning(p, h, "unused literal ignored");
9629 return tail;
9630 default:
9631 h = end = NEW_BLOCK(head, &head->nd_loc);
9632 end->nd_end = end;
9633 head = end;
9634 break;
9635 case NODE_BLOCK:
9636 end = h->nd_end;
9637 break;
9638 }
9639
9640 nd = end->nd_head;
9641 switch (nd_type(nd)) {
9642 case NODE_RETURN:
9643 case NODE_BREAK:
9644 case NODE_NEXT:
9645 case NODE_REDO:
9646 case NODE_RETRY:
9647 if (RTEST(ruby_verbose)) {
9648 parser_warning(p, tail, "statement not reached");
9649 }
9650 break;
9651
9652 default:
9653 break;
9654 }
9655
9656 if (nd_type(tail) != NODE_BLOCK) {
9657 tail = NEW_BLOCK(tail, &tail->nd_loc);
9658 tail->nd_end = tail;
9659 }
9660 end->nd_next = tail;
9661 h->nd_end = tail->nd_end;
9662 nd_set_last_loc(head, nd_last_loc(tail));
9663 return head;
9664}
9665
9666/* append item to the list */
9667static NODE*
9668list_append(struct parser_params *p, NODE *list, NODE *item)
9669{
9670 NODE *last;
9671
9672 if (list == 0) return NEW_LIST(item, &item->nd_loc);
9673 if (list->nd_next) {
9674 last = list->nd_next->nd_end;
9675 }
9676 else {
9677 last = list;
9678 }
9679
9680 list->nd_alen += 1;
9681 last->nd_next = NEW_LIST(item, &item->nd_loc);
9682 list->nd_next->nd_end = last->nd_next;
9683
9684 nd_set_last_loc(list, nd_last_loc(item));
9685
9686 return list;
9687}
9688
9689/* concat two lists */
9690static NODE*
9691list_concat(NODE *head, NODE *tail)
9692{
9693 NODE *last;
9694
9695 if (head->nd_next) {
9696 last = head->nd_next->nd_end;
9697 }
9698 else {
9699 last = head;
9700 }
9701
9702 head->nd_alen += tail->nd_alen;
9703 last->nd_next = tail;
9704 if (tail->nd_next) {
9705 head->nd_next->nd_end = tail->nd_next->nd_end;
9706 }
9707 else {
9708 head->nd_next->nd_end = tail;
9709 }
9710
9711 nd_set_last_loc(head, nd_last_loc(tail));
9712
9713 return head;
9714}
9715
9716static int
9717literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
9718{
9719 if (NIL_P(tail)) return 1;
9720 if (!rb_enc_compatible(head, tail)) {
9721 compile_error(p, "string literal encodings differ (%s / %s)",
9722 rb_enc_name(rb_enc_get(head)),
9723 rb_enc_name(rb_enc_get(tail)));
9724 rb_str_resize(head, 0);
9725 rb_str_resize(tail, 0);
9726 return 0;
9727 }
9728 rb_str_buf_append(head, tail);
9729 return 1;
9730}
9731
9732/* concat two string literals */
9733static NODE *
9734literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
9735{
9736 enum node_type htype;
9737 NODE *headlast;
9738 VALUE lit;
9739
9740 if (!head) return tail;
9741 if (!tail) return head;
9742
9743 htype = nd_type(head);
9744 if (htype == NODE_EVSTR) {
9745 NODE *node = NEW_DSTR(STR_NEW0(), loc);
9746 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9747 head = list_append(p, node, head);
9748 htype = NODE_DSTR;
9749 }
9750 if (p->heredoc_indent > 0) {
9751 switch (htype) {
9752 case NODE_STR:
9753 nd_set_type(head, NODE_DSTR);
9754 case NODE_DSTR:
9755 return list_append(p, head, tail);
9756 default:
9757 break;
9758 }
9759 }
9760 switch (nd_type(tail)) {
9761 case NODE_STR:
9762 if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9763 nd_type(headlast) == NODE_STR) {
9764 htype = NODE_STR;
9765 lit = headlast->nd_lit;
9766 }
9767 else {
9768 lit = head->nd_lit;
9769 }
9770 if (htype == NODE_STR) {
9771 if (!literal_concat0(p, lit, tail->nd_lit)) {
9772 error:
9773 rb_discard_node(p, head);
9774 rb_discard_node(p, tail);
9775 return 0;
9776 }
9777 rb_discard_node(p, tail);
9778 }
9779 else {
9780 list_append(p, head, tail);
9781 }
9782 break;
9783
9784 case NODE_DSTR:
9785 if (htype == NODE_STR) {
9786 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
9787 goto error;
9788 tail->nd_lit = head->nd_lit;
9789 rb_discard_node(p, head);
9790 head = tail;
9791 }
9792 else if (NIL_P(tail->nd_lit)) {
9793 append:
9794 head->nd_alen += tail->nd_alen - 1;
9795 head->nd_next->nd_end->nd_next = tail->nd_next;
9796 head->nd_next->nd_end = tail->nd_next->nd_end;
9797 rb_discard_node(p, tail);
9798 }
9799 else if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9800 nd_type(headlast) == NODE_STR) {
9801 lit = headlast->nd_lit;
9802 if (!literal_concat0(p, lit, tail->nd_lit))
9803 goto error;
9804 tail->nd_lit = Qnil;
9805 goto append;
9806 }
9807 else {
9808 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
9809 }
9810 break;
9811
9812 case NODE_EVSTR:
9813 if (htype == NODE_STR) {
9814 nd_set_type(head, NODE_DSTR);
9815 head->nd_alen = 1;
9816 }
9817 list_append(p, head, tail);
9818 break;
9819 }
9820 return head;
9821}
9822
9823static NODE *
9824evstr2dstr(struct parser_params *p, NODE *node)
9825{
9826 if (nd_type(node) == NODE_EVSTR) {
9827 NODE * dstr = NEW_DSTR(STR_NEW0(), &node->nd_loc);
9828 RB_OBJ_WRITTEN(p->ast, Qnil, dstr->nd_lit);
9829 node = list_append(p, dstr, node);
9830 }
9831 return node;
9832}
9833
9834static NODE *
9835new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
9836{
9837 NODE *head = node;
9838
9839 if (node) {
9840 switch (nd_type(node)) {
9841 case NODE_STR: case NODE_DSTR: case NODE_EVSTR:
9842 return node;
9843 }
9844 }
9845 return NEW_EVSTR(head, loc);
9846}
9847
9848static NODE *
9849call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
9850 const YYLTYPE *op_loc, const YYLTYPE *loc)
9851{
9852 NODE *expr;
9853 value_expr(recv);
9854 value_expr(arg1);
9855 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
9856 nd_set_line(expr, op_loc->beg_pos.lineno);
9857 return expr;
9858}
9859
9860static NODE *
9861call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
9862{
9863 NODE *opcall;
9864 value_expr(recv);
9865 opcall = NEW_OPCALL(recv, id, 0, loc);
9866 nd_set_line(opcall, op_loc->beg_pos.lineno);
9867 return opcall;
9868}
9869
9870static NODE *
9871new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
9872{
9873 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
9874 nd_set_line(qcall, op_loc->beg_pos.lineno);
9875 return qcall;
9876}
9877
9878static NODE*
9879new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
9880{
9881 NODE *ret;
9882 if (block) block_dup_check(p, args, block);
9883 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
9884 if (block) ret = method_add_block(p, ret, block, loc);
9885 fixpos(ret, recv);
9886 return ret;
9887}
9888
9889#define nd_once_body(node) (nd_type(node) == NODE_ONCE ? (node)->nd_body : node)
9890static NODE*
9891match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
9892{
9893 NODE *n;
9894 int line = op_loc->beg_pos.lineno;
9895
9896 value_expr(node1);
9897 value_expr(node2);
9898 if (node1 && (n = nd_once_body(node1)) != 0) {
9899 switch (nd_type(n)) {
9900 case NODE_DREGX:
9901 {
9902 NODE *match = NEW_MATCH2(node1, node2, loc);
9903 nd_set_line(match, line);
9904 return match;
9905 }
9906
9907 case NODE_LIT:
9908 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
9909 const VALUE lit = n->nd_lit;
9910 NODE *match = NEW_MATCH2(node1, node2, loc);
9911 match->nd_args = reg_named_capture_assign(p, lit, loc);
9912 nd_set_line(match, line);
9913 return match;
9914 }
9915 }
9916 }
9917
9918 if (node2 && (n = nd_once_body(node2)) != 0) {
9919 NODE *match3;
9920
9921 switch (nd_type(n)) {
9922 case NODE_LIT:
9923 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
9924 /* fallthru */
9925 case NODE_DREGX:
9926 match3 = NEW_MATCH3(node2, node1, loc);
9927 return match3;
9928 }
9929 }
9930
9931 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
9932 nd_set_line(n, line);
9933 return n;
9934}
9935
9936# if WARN_PAST_SCOPE
9937static int
9938past_dvar_p(struct parser_params *p, ID id)
9939{
9940 struct vtable *past = p->lvtbl->past;
9941 while (past) {
9942 if (vtable_included(past, id)) return 1;
9943 past = past->prev;
9944 }
9945 return 0;
9946}
9947# endif
9948
9949/* As Ripper#warn does not have arguments for the location, so the
9950 * following messages cannot be separated */
9951#define WARN_LOCATION(type) do { \
9952 if (p->warn_location) { \
9953 int line; \
9954 VALUE file = rb_source_location(&line); \
9955 rb_warn3(type" in eval may not return location in binding;" \
9956 " use Binding#source_location instead\n" \
9957 "%"PRIsWARN":%d: warning: in `%"PRIsWARN"'", \
9958 file, WARN_I(line), rb_id2str(rb_frame_this_func())); \
9959 } \
9960} while (0)
9961
9962static int
9963numparam_nested_p(struct parser_params *p)
9964{
9965 struct local_vars *local = p->lvtbl;
9966 NODE *outer = local->numparam.outer;
9967 NODE *inner = local->numparam.inner;
9968 if (outer || inner) {
9969 NODE *used = outer ? outer : inner;
9970 compile_error(p, "numbered parameter is already used in\n"
9971 "%s:%d: %s block here",
9972 p->ruby_sourcefile, nd_line(used),
9973 outer ? "outer" : "inner");
9974 parser_show_error_line(p, &used->nd_loc);
9975 return 1;
9976 }
9977 return 0;
9978}
9979
9980static NODE*
9981gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
9982{
9983 ID *vidp = NULL;
9984 NODE *node;
9985 switch (id) {
9986 case keyword_self:
9987 return NEW_SELF(loc);
9988 case keyword_nil:
9989 return NEW_NIL(loc);
9990 case keyword_true:
9991 return NEW_TRUE(loc);
9992 case keyword_false:
9993 return NEW_FALSE(loc);
9994 case keyword__FILE__:
9995 WARN_LOCATION("__FILE__");
9996 {
9997 VALUE file = p->ruby_sourcefile_string;
9998 if (NIL_P(file))
9999 file = rb_str_new(0, 0);
10000 else
10001 file = rb_str_dup(file);
10002 node = NEW_STR(file, loc);
10003 RB_OBJ_WRITTEN(p->ast, Qnil, file);
10004 }
10005 return node;
10006 case keyword__LINE__:
10007 WARN_LOCATION("__LINE__");
10008 return NEW_LIT(INT2FIX(p->tokline), loc);
10009 case keyword__ENCODING__:
10010 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
10011 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10012 return node;
10013
10014 }
10015 switch (id_type(id)) {
10016 case ID_LOCAL:
10017 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
10018 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
10019 if (id == p->cur_arg) {
10020 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10021 return 0;
10022 }
10023 if (vidp) *vidp |= LVAR_USED;
10024 node = NEW_DVAR(id, loc);
10025 return node;
10026 }
10027 if (local_id_ref(p, id, &vidp)) {
10028 if (id == p->cur_arg) {
10029 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10030 return 0;
10031 }
10032 if (vidp) *vidp |= LVAR_USED;
10033 node = NEW_LVAR(id, loc);
10034 return node;
10035 }
10036 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
10037 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
10038 if (numparam_nested_p(p)) return 0;
10039 node = NEW_DVAR(id, loc);
10040 struct local_vars *local = p->lvtbl;
10041 if (!local->numparam.current) local->numparam.current = node;
10042 return node;
10043 }
10044# if WARN_PAST_SCOPE
10045 if (!p->in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10046 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10047 }
10048# endif
10049 /* method call without arguments */
10050 return NEW_VCALL(id, loc);
10051 case ID_GLOBAL:
10052 return NEW_GVAR(id, loc);
10053 case ID_INSTANCE:
10054 return NEW_IVAR(id, loc);
10055 case ID_CONST:
10056 return NEW_CONST(id, loc);
10057 case ID_CLASS:
10058 return NEW_CVAR(id, loc);
10059 }
10060 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10061 return 0;
10062}
10063
10064static NODE *
10065opt_arg_append(NODE *opt_list, NODE *opt)
10066{
10067 NODE *opts = opt_list;
10068 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10069
10070 while (opts->nd_next) {
10071 opts = opts->nd_next;
10072 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10073 }
10074 opts->nd_next = opt;
10075
10076 return opt_list;
10077}
10078
10079static NODE *
10080kwd_append(NODE *kwlist, NODE *kw)
10081{
10082 if (kwlist) {
10083 NODE *kws = kwlist;
10084 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10085 while (kws->nd_next) {
10086 kws = kws->nd_next;
10087 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10088 }
10089 kws->nd_next = kw;
10090 }
10091 return kwlist;
10092}
10093
10094static NODE *
10095new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10096{
10097 return NEW_DEFINED(remove_begin_all(expr), loc);
10098}
10099
10100static NODE*
10101symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10102{
10103 if (nd_type(symbol) == NODE_DSTR) {
10104 nd_set_type(symbol, NODE_DSYM);
10105 }
10106 else {
10107 nd_set_type(symbol, NODE_LIT);
10108 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10109 }
10110 return list_append(p, symbols, symbol);
10111}
10112
10113static NODE *
10114new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10115{
10116 NODE *list, *prev;
10117 VALUE lit;
10118
10119 if (!node) {
10120 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10121 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10122 return node;
10123 }
10124 switch (nd_type(node)) {
10125 case NODE_STR:
10126 {
10127 VALUE src = node->nd_lit;
10128 nd_set_type(node, NODE_LIT);
10129 nd_set_loc(node, loc);
10130 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10131 }
10132 break;
10133 default:
10134 lit = STR_NEW0();
10135 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10136 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10137 /* fall through */
10138 case NODE_DSTR:
10139 nd_set_type(node, NODE_DREGX);
10140 nd_set_loc(node, loc);
10141 node->nd_cflag = options & RE_OPTION_MASK;
10142 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10143 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10144 if (nd_type(list->nd_head) == NODE_STR) {
10145 VALUE tail = list->nd_head->nd_lit;
10146 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10147 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10148 if (!literal_concat0(p, lit, tail)) {
10149 return NEW_NIL(loc); /* dummy node on error */
10150 }
10151 rb_str_resize(tail, 0);
10152 prev->nd_next = list->nd_next;
10153 rb_discard_node(p, list->nd_head);
10154 rb_discard_node(p, list);
10155 list = prev;
10156 }
10157 else {
10158 prev = list;
10159 }
10160 }
10161 else {
10162 prev = 0;
10163 }
10164 }
10165 if (!node->nd_next) {
10166 VALUE src = node->nd_lit;
10167 nd_set_type(node, NODE_LIT);
10168 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10169 }
10170 if (options & RE_OPTION_ONCE) {
10171 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10172 }
10173 break;
10174 }
10175 return node;
10176}
10177
10178static NODE *
10179new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10180{
10181 if (!k) return 0;
10182 return NEW_KW_ARG(0, (k), loc);
10183}
10184
10185static NODE *
10186new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10187{
10188 if (!node) {
10189 VALUE lit = STR_NEW0();
10190 NODE *xstr = NEW_XSTR(lit, loc);
10191 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10192 return xstr;
10193 }
10194 switch (nd_type(node)) {
10195 case NODE_STR:
10196 nd_set_type(node, NODE_XSTR);
10197 nd_set_loc(node, loc);
10198 break;
10199 case NODE_DSTR:
10200 nd_set_type(node, NODE_DXSTR);
10201 nd_set_loc(node, loc);
10202 break;
10203 default:
10204 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10205 break;
10206 }
10207 return node;
10208}
10209
10210static void
10211check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10212{
10213 VALUE lit;
10214
10215 if (!arg || !p->case_labels) return;
10216
10217 lit = rb_node_case_when_optimizable_literal(arg);
10218 if (lit == Qundef) return;
10219 if (nd_type(arg) == NODE_STR) {
10220 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10221 }
10222
10223 if (NIL_P(p->case_labels)) {
10224 p->case_labels = rb_obj_hide(rb_hash_new());
10225 }
10226 else {
10227 VALUE line = rb_hash_lookup(p->case_labels, lit);
10228 if (!NIL_P(line)) {
10229 rb_warning1("duplicated `when' clause with line %d is ignored",
10230 WARN_IVAL(line));
10231 return;
10232 }
10233 }
10234 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10235}
10236
10237#else /* !RIPPER */
10238static int
10239id_is_var(struct parser_params *p, ID id)
10240{
10241 if (is_notop_id(id)) {
10242 switch (id & ID_SCOPE_MASK) {
10243 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10244 return 1;
10245 case ID_LOCAL:
10246 if (dyna_in_block(p)) {
10247 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10248 }
10249 if (local_id(p, id)) return 1;
10250 /* method call without arguments */
10251 return 0;
10252 }
10253 }
10254 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10255 return 0;
10256}
10257
10258static VALUE
10259new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10260{
10261 VALUE src = 0, err;
10262 int options = 0;
10263 if (ripper_is_node_yylval(re)) {
10264 src = RNODE(re)->nd_cval;
10265 re = RNODE(re)->nd_rval;
10266 }
10267 if (ripper_is_node_yylval(opt)) {
10268 options = (int)RNODE(opt)->nd_tag;
10269 opt = RNODE(opt)->nd_rval;
10270 }
10271 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10272 compile_error(p, "%"PRIsVALUE, err);
10273 }
10274 return dispatch2(regexp_literal, re, opt);
10275}
10276#endif /* !RIPPER */
10277
10278
10279#ifndef RIPPER
10280static const char rb_parser_lex_state_names[][8] = {
10281 "BEG", "END", "ENDARG", "ENDFN", "ARG",
10282 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10283 "LABEL", "LABELED","FITEM",
10284};
10285
10286static VALUE
10287append_lex_state_name(enum lex_state_e state, VALUE buf)
10288{
10289 int i, sep = 0;
10290 unsigned int mask = 1;
10291 static const char none[] = "NONE";
10292
10293 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10294 if ((unsigned)state & mask) {
10295 if (sep) {
10296 rb_str_cat(buf, "|", 1);
10297 }
10298 sep = 1;
10299 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10300 }
10301 }
10302 if (!sep) {
10303 rb_str_cat(buf, none, sizeof(none)-1);
10304 }
10305 return buf;
10306}
10307
10308static void
10309flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10310{
10311 VALUE mesg = p->debug_buffer;
10312
10313 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10314 p->debug_buffer = Qnil;
10315 rb_io_puts(1, &mesg, out);
10316 }
10317 if (!NIL_P(str) && RSTRING_LEN(str)) {
10318 rb_io_write(p->debug_output, str);
10319 }
10320}
10321
10322enum lex_state_e
10323rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10324 enum lex_state_e to, int line)
10325{
10326 VALUE mesg;
10327 mesg = rb_str_new_cstr("lex_state: ");
10328 append_lex_state_name(from, mesg);
10329 rb_str_cat_cstr(mesg, " -> ");
10330 append_lex_state_name(to, mesg);
10331 rb_str_catf(mesg, " at line %d\n", line);
10332 flush_debug_buffer(p, p->debug_output, mesg);
10333 return to;
10334}
10335
10336VALUE
10337rb_parser_lex_state_name(enum lex_state_e state)
10338{
10339 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10340}
10341
10342static void
10343append_bitstack_value(stack_type stack, VALUE mesg)
10344{
10345 if (stack == 0) {
10346 rb_str_cat_cstr(mesg, "0");
10347 }
10348 else {
10349 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10350 for (; mask && !(stack & mask); mask >>= 1) continue;
10351 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10352 }
10353}
10354
10355void
10356rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10357 const char *name, int line)
10358{
10359 VALUE mesg = rb_sprintf("%s: ", name);
10360 append_bitstack_value(stack, mesg);
10361 rb_str_catf(mesg, " at line %d\n", line);
10362 flush_debug_buffer(p, p->debug_output, mesg);
10363}
10364
10365void
10366rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10367{
10368 va_list ap;
10369 VALUE mesg = rb_str_new_cstr("internal parser error: ");
10370
10371 va_start(ap, fmt);
10372 rb_str_vcatf(mesg, fmt, ap);
10373 va_end(ap);
10374 parser_yyerror(p, NULL, RSTRING_PTR(mesg));
10375 RB_GC_GUARD(mesg);
10376
10377 mesg = rb_str_new(0, 0);
10378 append_lex_state_name(p->lex.state, mesg);
10379 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10380 rb_str_resize(mesg, 0);
10381 append_bitstack_value(p->cond_stack, mesg);
10382 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10383 rb_str_resize(mesg, 0);
10384 append_bitstack_value(p->cmdarg_stack, mesg);
10385 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10386 if (p->debug_output == rb_stdout)
10387 p->debug_output = rb_stderr;
10388 p->debug = TRUE;
10389}
10390
10391YYLTYPE *
10392rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10393{
10394 int sourceline = here->sourceline;
10395 int beg_pos = (int)here->offset - here->quote
10396 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10397 int end_pos = (int)here->offset + here->length + here->quote;
10398
10399 yylloc->beg_pos.lineno = sourceline;
10400 yylloc->beg_pos.column = beg_pos;
10401 yylloc->end_pos.lineno = sourceline;
10402 yylloc->end_pos.column = end_pos;
10403 return yylloc;
10404}
10405
10406YYLTYPE *
10407rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10408{
10409 yylloc->beg_pos.lineno = p->ruby_sourceline;
10410 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10411 yylloc->end_pos.lineno = p->ruby_sourceline;
10412 yylloc->end_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10413 return yylloc;
10414}
10415
10416YYLTYPE *
10417rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10418{
10419 yylloc->beg_pos.lineno = p->ruby_sourceline;
10420 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10421 yylloc->end_pos.lineno = p->ruby_sourceline;
10422 yylloc->end_pos.column = (int)(p->lex.pcur - p->lex.pbeg);
10423 return yylloc;
10424}
10425#endif /* !RIPPER */
10426
10427static void
10428parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp)
10429{
10430 VALUE v;
10431
10432 switch (type) {
10433 case tIDENTIFIER: case tFID: case tGVAR: case tIVAR:
10434 case tCONSTANT: case tCVAR: case tLABEL: case tOP_ASGN:
10435#ifndef RIPPER
10436 v = rb_id2str(valp->id);
10437#else
10438 v = valp->node->nd_rval;
10439#endif
10440 rb_parser_printf(p, "%"PRIsVALUE, v);
10441 break;
10442 case tINTEGER: case tFLOAT: case tRATIONAL: case tIMAGINARY:
10443 case tSTRING_CONTENT: case tCHAR:
10444#ifndef RIPPER
10445 v = valp->node->nd_lit;
10446#else
10447 v = valp->val;
10448#endif
10449 rb_parser_printf(p, "%+"PRIsVALUE, v);
10450 break;
10451 case tNTH_REF:
10452#ifndef RIPPER
10453 rb_parser_printf(p, "$%ld", valp->node->nd_nth);
10454#else
10455 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10456#endif
10457 break;
10458 case tBACK_REF:
10459#ifndef RIPPER
10460 rb_parser_printf(p, "$%c", (int)valp->node->nd_nth);
10461#else
10462 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10463#endif
10464 break;
10465 default:
10466 break;
10467 }
10468}
10469
10470static int
10471assignable0(struct parser_params *p, ID id, const char **err)
10472{
10473 if (!id) return -1;
10474 switch (id) {
10475 case keyword_self:
10476 *err = "Can't change the value of self";
10477 return -1;
10478 case keyword_nil:
10479 *err = "Can't assign to nil";
10480 return -1;
10481 case keyword_true:
10482 *err = "Can't assign to true";
10483 return -1;
10484 case keyword_false:
10485 *err = "Can't assign to false";
10486 return -1;
10487 case keyword__FILE__:
10488 *err = "Can't assign to __FILE__";
10489 return -1;
10490 case keyword__LINE__:
10491 *err = "Can't assign to __LINE__";
10492 return -1;
10493 case keyword__ENCODING__:
10494 *err = "Can't assign to __ENCODING__";
10495 return -1;
10496 }
10497 switch (id_type(id)) {
10498 case ID_LOCAL:
10499 if (dyna_in_block(p)) {
10500 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10501 compile_error(p, "Can't assign to numbered parameter _%d",
10502 NUMPARAM_ID_TO_IDX(id));
10503 return -1;
10504 }
10505 if (dvar_curr(p, id)) return NODE_DASGN_CURR;
10506 if (dvar_defined(p, id)) return NODE_DASGN;
10507 if (local_id(p, id)) return NODE_LASGN;
10508 dyna_var(p, id);
10509 return NODE_DASGN_CURR;
10510 }
10511 else {
10512 if (!local_id(p, id)) local_var(p, id);
10513 return NODE_LASGN;
10514 }
10515 break;
10516 case ID_GLOBAL: return NODE_GASGN;
10517 case ID_INSTANCE: return NODE_IASGN;
10518 case ID_CONST:
10519 if (!p->in_def) return NODE_CDECL;
10520 *err = "dynamic constant assignment";
10521 return -1;
10522 case ID_CLASS: return NODE_CVASGN;
10523 default:
10524 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10525 }
10526 return -1;
10527}
10528
10529#ifndef RIPPER
10530static NODE*
10531assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10532{
10533 const char *err = 0;
10534 int node_type = assignable0(p, id, &err);
10535 switch (node_type) {
10536 case NODE_DASGN_CURR: return NEW_DASGN_CURR(id, val, loc);
10537 case NODE_DASGN: return NEW_DASGN(id, val, loc);
10538 case NODE_LASGN: return NEW_LASGN(id, val, loc);
10539 case NODE_GASGN: return NEW_GASGN(id, val, loc);
10540 case NODE_IASGN: return NEW_IASGN(id, val, loc);
10541 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10542 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10543 }
10544 if (err) yyerror1(loc, err);
10545 return NEW_BEGIN(0, loc);
10546}
10547#else
10548static VALUE
10549assignable(struct parser_params *p, VALUE lhs)
10550{
10551 const char *err = 0;
10552 assignable0(p, get_id(lhs), &err);
10553 if (err) lhs = assign_error(p, lhs);
10554 return lhs;
10555}
10556#endif
10557
10558static int
10559is_private_local_id(ID name)
10560{
10561 VALUE s;
10562 if (name == idUScore) return 1;
10563 if (!is_local_id(name)) return 0;
10564 s = rb_id2str(name);
10565 if (!s) return 0;
10566 return RSTRING_PTR(s)[0] == '_';
10567}
10568
10569static int
10570shadowing_lvar_0(struct parser_params *p, ID name)
10571{
10572 if (is_private_local_id(name)) return 1;
10573 if (dyna_in_block(p)) {
10574 if (dvar_curr(p, name)) {
10575 yyerror0("duplicated argument name");
10576 }
10577 else if (dvar_defined(p, name) || local_id(p, name)) {
10578 vtable_add(p->lvtbl->vars, name);
10579 if (p->lvtbl->used) {
10580 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
10581 }
10582 return 0;
10583 }
10584 }
10585 else {
10586 if (local_id(p, name)) {
10587 yyerror0("duplicated argument name");
10588 }
10589 }
10590 return 1;
10591}
10592
10593static ID
10594shadowing_lvar(struct parser_params *p, ID name)
10595{
10596 shadowing_lvar_0(p, name);
10597 return name;
10598}
10599
10600static void
10601new_bv(struct parser_params *p, ID name)
10602{
10603 if (!name) return;
10604 if (!is_local_id(name)) {
10605 compile_error(p, "invalid local variable - %"PRIsVALUE,
10606 rb_id2str(name));
10607 return;
10608 }
10609 if (!shadowing_lvar_0(p, name)) return;
10610 dyna_var(p, name);
10611}
10612
10613#ifndef RIPPER
10614static NODE *
10615aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
10616{
10617 return NEW_ATTRASGN(recv, tASET, idx, loc);
10618}
10619
10620static void
10621block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
10622{
10623 if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
10624 compile_error(p, "both block arg and actual block given");
10625 }
10626}
10627
10628static NODE *
10629attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
10630{
10631 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
10632 return NEW_ATTRASGN(recv, id, 0, loc);
10633}
10634
10635static void
10636rb_backref_error(struct parser_params *p, NODE *node)
10637{
10638 switch (nd_type(node)) {
10639 case NODE_NTH_REF:
10640 compile_error(p, "Can't set variable $%ld", node->nd_nth);
10641 break;
10642 case NODE_BACK_REF:
10643 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
10644 break;
10645 }
10646}
10647
10648static NODE *
10649arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10650{
10651 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
10652 switch (nd_type(node1)) {
10653 case NODE_LIST:
10654 return list_append(p, node1, node2);
10655 case NODE_BLOCK_PASS:
10656 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
10657 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
10658 return node1;
10659 case NODE_ARGSPUSH:
10660 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
10661 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10662 nd_set_type(node1, NODE_ARGSCAT);
10663 return node1;
10664 case NODE_ARGSCAT:
10665 if (nd_type(node1->nd_body) != NODE_LIST) break;
10666 node1->nd_body = list_append(p, node1->nd_body, node2);
10667 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10668 return node1;
10669 }
10670 return NEW_ARGSPUSH(node1, node2, loc);
10671}
10672
10673static NODE *
10674arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10675{
10676 if (!node2) return node1;
10677 switch (nd_type(node1)) {
10678 case NODE_BLOCK_PASS:
10679 if (node1->nd_head)
10680 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
10681 else
10682 node1->nd_head = NEW_LIST(node2, loc);
10683 return node1;
10684 case NODE_ARGSPUSH:
10685 if (nd_type(node2) != NODE_LIST) break;
10686 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
10687 nd_set_type(node1, NODE_ARGSCAT);
10688 return node1;
10689 case NODE_ARGSCAT:
10690 if (nd_type(node2) != NODE_LIST ||
10691 nd_type(node1->nd_body) != NODE_LIST) break;
10692 node1->nd_body = list_concat(node1->nd_body, node2);
10693 return node1;
10694 }
10695 return NEW_ARGSCAT(node1, node2, loc);
10696}
10697
10698static NODE *
10699last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
10700{
10701 NODE *n1;
10702 if ((n1 = splat_array(args)) != 0) {
10703 return list_append(p, n1, last_arg);
10704 }
10705 return arg_append(p, args, last_arg, loc);
10706}
10707
10708static NODE *
10709rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
10710{
10711 NODE *n1;
10712 if ((nd_type(rest_arg) == NODE_LIST) && (n1 = splat_array(args)) != 0) {
10713 return list_concat(n1, rest_arg);
10714 }
10715 return arg_concat(p, args, rest_arg, loc);
10716}
10717
10718static NODE *
10719splat_array(NODE* node)
10720{
10721 if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
10722 if (nd_type(node) == NODE_LIST) return node;
10723 return 0;
10724}
10725
10726static void
10727mark_lvar_used(struct parser_params *p, NODE *rhs)
10728{
10729 ID *vidp = NULL;
10730 if (!rhs) return;
10731 switch (nd_type(rhs)) {
10732 case NODE_LASGN:
10733 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
10734 if (vidp) *vidp |= LVAR_USED;
10735 }
10736 break;
10737 case NODE_DASGN:
10738 case NODE_DASGN_CURR:
10739 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
10740 if (vidp) *vidp |= LVAR_USED;
10741 }
10742 break;
10743#if 0
10744 case NODE_MASGN:
10745 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
10746 mark_lvar_used(p, rhs->nd_head);
10747 }
10748 break;
10749#endif
10750 }
10751}
10752
10753static NODE *
10754node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, const YYLTYPE *loc)
10755{
10756 if (!lhs) return 0;
10757
10758 switch (nd_type(lhs)) {
10759 case NODE_GASGN:
10760 case NODE_IASGN:
10761 case NODE_LASGN:
10762 case NODE_DASGN:
10763 case NODE_DASGN_CURR:
10764 case NODE_MASGN:
10765 case NODE_CDECL:
10766 case NODE_CVASGN:
10767 lhs->nd_value = rhs;
10768 nd_set_loc(lhs, loc);
10769 break;
10770
10771 case NODE_ATTRASGN:
10772 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
10773 nd_set_loc(lhs, loc);
10774 break;
10775
10776 default:
10777 /* should not happen */
10778 break;
10779 }
10780
10781 return lhs;
10782}
10783
10784static NODE *
10785value_expr_check(struct parser_params *p, NODE *node)
10786{
10787 NODE *void_node = 0, *vn;
10788
10789 if (!node) {
10790 rb_warning0("empty expression");
10791 }
10792 while (node) {
10793 switch (nd_type(node)) {
10794 case NODE_RETURN:
10795 case NODE_BREAK:
10796 case NODE_NEXT:
10797 case NODE_REDO:
10798 case NODE_RETRY:
10799 return void_node ? void_node : node;
10800
10801 case NODE_CASE3:
10802 if (!node->nd_body || nd_type(node->nd_body) != NODE_IN) {
10803 compile_error(p, "unexpected node");
10804 return NULL;
10805 }
10806 if (node->nd_body->nd_body) {
10807 return NULL;
10808 }
10809 /* single line pattern matching */
10810 return void_node ? void_node : node;
10811
10812 case NODE_BLOCK:
10813 while (node->nd_next) {
10814 node = node->nd_next;
10815 }
10816 node = node->nd_head;
10817 break;
10818
10819 case NODE_BEGIN:
10820 node = node->nd_body;
10821 break;
10822
10823 case NODE_IF:
10824 case NODE_UNLESS:
10825 if (!node->nd_body) {
10826 return NULL;
10827 }
10828 else if (!node->nd_else) {
10829 return NULL;
10830 }
10831 vn = value_expr_check(p, node->nd_body);
10832 if (!vn) return NULL;
10833 if (!void_node) void_node = vn;
10834 node = node->nd_else;
10835 break;
10836
10837 case NODE_AND:
10838 case NODE_OR:
10839 node = node->nd_1st;
10840 break;
10841
10842 case NODE_LASGN:
10843 case NODE_DASGN:
10844 case NODE_DASGN_CURR:
10845 case NODE_MASGN:
10846 mark_lvar_used(p, node);
10847 return NULL;
10848
10849 default:
10850 return NULL;
10851 }
10852 }
10853
10854 return NULL;
10855}
10856
10857static int
10858value_expr_gen(struct parser_params *p, NODE *node)
10859{
10860 NODE *void_node = value_expr_check(p, node);
10861 if (void_node) {
10862 yyerror1(&void_node->nd_loc, "void value expression");
10863 /* or "control never reach"? */
10864 return FALSE;
10865 }
10866 return TRUE;
10867}
10868static void
10869void_expr(struct parser_params *p, NODE *node)
10870{
10871 const char *useless = 0;
10872
10873 if (!RTEST(ruby_verbose)) return;
10874
10875 if (!node || !(node = nd_once_body(node))) return;
10876 switch (nd_type(node)) {
10877 case NODE_OPCALL:
10878 switch (node->nd_mid) {
10879 case '+':
10880 case '-':
10881 case '*':
10882 case '/':
10883 case '%':
10884 case tPOW:
10885 case tUPLUS:
10886 case tUMINUS:
10887 case '|':
10888 case '^':
10889 case '&':
10890 case tCMP:
10891 case '>':
10892 case tGEQ:
10893 case '<':
10894 case tLEQ:
10895 case tEQ:
10896 case tNEQ:
10897 useless = rb_id2name(node->nd_mid);
10898 break;
10899 }
10900 break;
10901
10902 case NODE_LVAR:
10903 case NODE_DVAR:
10904 case NODE_GVAR:
10905 case NODE_IVAR:
10906 case NODE_CVAR:
10907 case NODE_NTH_REF:
10908 case NODE_BACK_REF:
10909 useless = "a variable";
10910 break;
10911 case NODE_CONST:
10912 useless = "a constant";
10913 break;
10914 case NODE_LIT:
10915 case NODE_STR:
10916 case NODE_DSTR:
10917 case NODE_DREGX:
10918 useless = "a literal";
10919 break;
10920 case NODE_COLON2:
10921 case NODE_COLON3:
10922 useless = "::";
10923 break;
10924 case NODE_DOT2:
10925 useless = "..";
10926 break;
10927 case NODE_DOT3:
10928 useless = "...";
10929 break;
10930 case NODE_SELF:
10931 useless = "self";
10932 break;
10933 case NODE_NIL:
10934 useless = "nil";
10935 break;
10936 case NODE_TRUE:
10937 useless = "true";
10938 break;
10939 case NODE_FALSE:
10940 useless = "false";
10941 break;
10942 case NODE_DEFINED:
10943 useless = "defined?";
10944 break;
10945 }
10946
10947 if (useless) {
10948 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
10949 }
10950}
10951
10952static NODE *
10953void_stmts(struct parser_params *p, NODE *node)
10954{
10955 NODE *const n = node;
10956 if (!RTEST(ruby_verbose)) return n;
10957 if (!node) return n;
10958 if (nd_type(node) != NODE_BLOCK) return n;
10959
10960 while (node->nd_next) {
10961 void_expr(p, node->nd_head);
10962 node = node->nd_next;
10963 }
10964 return n;
10965}
10966
10967static NODE *
10968remove_begin(NODE *node)
10969{
10970 NODE **n = &node, *n1 = node;
10971 while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
10972 *n = n1 = n1->nd_body;
10973 }
10974 return node;
10975}
10976
10977static NODE *
10978remove_begin_all(NODE *node)
10979{
10980 NODE **n = &node, *n1 = node;
10981 while (n1 && nd_type(n1) == NODE_BEGIN) {
10982 *n = n1 = n1->nd_body;
10983 }
10984 return node;
10985}
10986
10987static void
10988reduce_nodes(struct parser_params *p, NODE **body)
10989{
10990 NODE *node = *body;
10991
10992 if (!node) {
10993 *body = NEW_NIL(&NULL_LOC);
10994 return;
10995 }
10996#define subnodes(n1, n2) \
10997 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
10998 (!node->n2) ? (body = &node->n1, 1) : \
10999 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
11000
11001 while (node) {
11002 int newline = (int)(node->flags & NODE_FL_NEWLINE);
11003 switch (nd_type(node)) {
11004 end:
11005 case NODE_NIL:
11006 *body = 0;
11007 return;
11008 case NODE_RETURN:
11009 *body = node = node->nd_stts;
11010 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11011 continue;
11012 case NODE_BEGIN:
11013 *body = node = node->nd_body;
11014 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11015 continue;
11016 case NODE_BLOCK:
11017 body = &node->nd_end->nd_head;
11018 break;
11019 case NODE_IF:
11020 case NODE_UNLESS:
11021 if (subnodes(nd_body, nd_else)) break;
11022 return;
11023 case NODE_CASE:
11024 body = &node->nd_body;
11025 break;
11026 case NODE_WHEN:
11027 if (!subnodes(nd_body, nd_next)) goto end;
11028 break;
11029 case NODE_ENSURE:
11030 if (!subnodes(nd_head, nd_resq)) goto end;
11031 break;
11032 case NODE_RESCUE:
11033 if (node->nd_else) {
11034 body = &node->nd_resq;
11035 break;
11036 }
11037 if (!subnodes(nd_head, nd_resq)) goto end;
11038 break;
11039 default:
11040 return;
11041 }
11042 node = *body;
11043 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11044 }
11045
11046#undef subnodes
11047}
11048
11049static int
11050is_static_content(NODE *node)
11051{
11052 if (!node) return 1;
11053 switch (nd_type(node)) {
11054 case NODE_HASH:
11055 if (!(node = node->nd_head)) break;
11056 case NODE_LIST:
11057 do {
11058 if (!is_static_content(node->nd_head)) return 0;
11059 } while ((node = node->nd_next) != 0);
11060 case NODE_LIT:
11061 case NODE_STR:
11062 case NODE_NIL:
11063 case NODE_TRUE:
11064 case NODE_FALSE:
11065 case NODE_ZLIST:
11066 break;
11067 default:
11068 return 0;
11069 }
11070 return 1;
11071}
11072
11073static int
11074assign_in_cond(struct parser_params *p, NODE *node)
11075{
11076 switch (nd_type(node)) {
11077 case NODE_MASGN:
11078 case NODE_LASGN:
11079 case NODE_DASGN:
11080 case NODE_DASGN_CURR:
11081 case NODE_GASGN:
11082 case NODE_IASGN:
11083 break;
11084
11085 default:
11086 return 0;
11087 }
11088
11089 if (!node->nd_value) return 1;
11090 if (is_static_content(node->nd_value)) {
11091 /* reports always */
11092 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11093 }
11094 return 1;
11095}
11096
11097enum cond_type {
11098 COND_IN_OP,
11099 COND_IN_COND,
11100 COND_IN_FF
11101};
11102
11103#define SWITCH_BY_COND_TYPE(t, w, arg) \
11104 switch (t) { \
11105 case COND_IN_OP: break; \
11106 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11107 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11108 }
11109
11110static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11111
11112static NODE*
11113range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11114{
11115 enum node_type type;
11116
11117 if (node == 0) return 0;
11118
11119 type = nd_type(node);
11120 value_expr(node);
11121 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11122 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11123 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(rb_intern("$."), loc), loc), loc);
11124 }
11125 return cond0(p, node, COND_IN_FF, loc);
11126}
11127
11128static NODE*
11129cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11130{
11131 if (node == 0) return 0;
11132 if (!(node = nd_once_body(node))) return 0;
11133 assign_in_cond(p, node);
11134
11135 switch (nd_type(node)) {
11136 case NODE_DSTR:
11137 case NODE_EVSTR:
11138 case NODE_STR:
11139 SWITCH_BY_COND_TYPE(type, warn, "string ")
11140 break;
11141
11142 case NODE_DREGX:
11143 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11144
11145 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11146
11147 case NODE_AND:
11148 case NODE_OR:
11149 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11150 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11151 break;
11152
11153 case NODE_DOT2:
11154 case NODE_DOT3:
11155 node->nd_beg = range_op(p, node->nd_beg, loc);
11156 node->nd_end = range_op(p, node->nd_end, loc);
11157 if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
11158 else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
11159 break;
11160
11161 case NODE_DSYM:
11162 SWITCH_BY_COND_TYPE(type, warning, "string ")
11163 break;
11164
11165 case NODE_LIT:
11166 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11167 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11168 nd_set_type(node, NODE_MATCH);
11169 }
11170 else if (node->nd_lit == Qtrue ||
11171 node->nd_lit == Qfalse) {
11172 /* booleans are OK, e.g., while true */
11173 }
11174 else {
11175 SWITCH_BY_COND_TYPE(type, warning, "")
11176 }
11177 default:
11178 break;
11179 }
11180 return node;
11181}
11182
11183static NODE*
11184cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11185{
11186 if (node == 0) return 0;
11187 return cond0(p, node, COND_IN_COND, loc);
11188}
11189
11190static NODE*
11191method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11192{
11193 if (node == 0) return 0;
11194 return cond0(p, node, COND_IN_OP, loc);
11195}
11196
11197static NODE*
11198new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11199{
11200 if (!cc) return right;
11201 cc = cond0(p, cc, COND_IN_COND, loc);
11202 return newline_node(NEW_IF(cc, left, right, loc));
11203}
11204
11205static NODE*
11206new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11207{
11208 if (!cc) return right;
11209 cc = cond0(p, cc, COND_IN_COND, loc);
11210 return newline_node(NEW_UNLESS(cc, left, right, loc));
11211}
11212
11213static NODE*
11214logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11215 const YYLTYPE *op_loc, const YYLTYPE *loc)
11216{
11217 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11218 NODE *op;
11219 value_expr(left);
11220 if (left && (enum node_type)nd_type(left) == type) {
11221 NODE *node = left, *second;
11222 while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
11223 node = second;
11224 }
11225 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11226 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11227 left->nd_loc.end_pos = loc->end_pos;
11228 return left;
11229 }
11230 op = NEW_NODE(type, left, right, 0, loc);
11231 nd_set_line(op, op_loc->beg_pos.lineno);
11232 return op;
11233}
11234
11235static void
11236no_blockarg(struct parser_params *p, NODE *node)
11237{
11238 if (node && nd_type(node) == NODE_BLOCK_PASS) {
11239 compile_error(p, "block argument should not be given");
11240 }
11241}
11242
11243static NODE *
11244ret_args(struct parser_params *p, NODE *node)
11245{
11246 if (node) {
11247 no_blockarg(p, node);
11248 if (nd_type(node) == NODE_LIST) {
11249 if (node->nd_next == 0) {
11250 node = node->nd_head;
11251 }
11252 else {
11253 nd_set_type(node, NODE_VALUES);
11254 }
11255 }
11256 }
11257 return node;
11258}
11259
11260static NODE *
11261new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11262{
11263 if (node) no_blockarg(p, node);
11264
11265 return NEW_YIELD(node, loc);
11266}
11267
11268static VALUE
11269negate_lit(struct parser_params *p, VALUE lit)
11270{
11271 if (FIXNUM_P(lit)) {
11272 return LONG2FIX(-FIX2LONG(lit));
11273 }
11274 if (SPECIAL_CONST_P(lit)) {
11275#if USE_FLONUM
11276 if (FLONUM_P(lit)) {
11277 return DBL2NUM(-RFLOAT_VALUE(lit));
11278 }
11279#endif
11280 goto unknown;
11281 }
11282 switch (BUILTIN_TYPE(lit)) {
11283 case T_BIGNUM:
11284 BIGNUM_NEGATE(lit);
11285 lit = rb_big_norm(lit);
11286 break;
11287 case T_RATIONAL:
11288 RRATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11289 break;
11290 case T_COMPLEX:
11291 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11292 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11293 break;
11294 case T_FLOAT:
11295 RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
11296 break;
11297 unknown:
11298 default:
11299 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11300 rb_builtin_class_name(lit));
11301 break;
11302 }
11303 return lit;
11304}
11305
11306static NODE *
11307arg_blk_pass(NODE *node1, NODE *node2)
11308{
11309 if (node2) {
11310 if (!node1) return node2;
11311 node2->nd_head = node1;
11312 nd_set_first_lineno(node2, nd_first_lineno(node1));
11313 nd_set_first_column(node2, nd_first_column(node1));
11314 return node2;
11315 }
11316 return node1;
11317}
11318
11319static bool
11320args_info_empty_p(struct rb_args_info *args)
11321{
11322 if (args->pre_args_num) return false;
11323 if (args->post_args_num) return false;
11324 if (args->rest_arg) return false;
11325 if (args->opt_args) return false;
11326 if (args->block_arg) return false;
11327 if (args->kw_args) return false;
11328 if (args->kw_rest_arg) return false;
11329 return true;
11330}
11331
11332static NODE*
11333new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
11334{
11335 int saved_line = p->ruby_sourceline;
11336 struct rb_args_info *args = tail->nd_ainfo;
11337
11338 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
11339 args->pre_init = pre_args ? pre_args->nd_next : 0;
11340
11341 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
11342 args->post_init = post_args ? post_args->nd_next : 0;
11343 args->first_post_arg = post_args ? post_args->nd_pid : 0;
11344
11345 args->rest_arg = rest_arg;
11346
11347 args->opt_args = opt_args;
11348
11349 args->ruby2_keywords = rest_arg == idFWD_REST;
11350
11351 p->ruby_sourceline = saved_line;
11352 nd_set_loc(tail, loc);
11353
11354 return tail;
11355}
11356
11357static NODE*
11358new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *loc)
11359{
11360 int saved_line = p->ruby_sourceline;
11361 NODE *node;
11362 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11363 struct rb_args_info *args = ZALLOC(struct rb_args_info);
11364 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
11365 args->imemo = tmpbuf;
11366 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
11367 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11368 if (p->error_p) return node;
11369
11370 args->block_arg = block;
11371 args->kw_args = kw_args;
11372
11373 if (kw_args) {
11374 /*
11375 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
11376 * variable order: k1, kr1, k2, &b, internal_id, krest
11377 * #=> <reorder>
11378 * variable order: kr1, k1, k2, internal_id, krest, &b
11379 */
11380 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
11381 struct vtable *vtargs = p->lvtbl->args;
11382 NODE *kwn = kw_args;
11383
11384 vtable_pop(vtargs, !!block + !!kw_rest_arg);
11385 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
11386 while (kwn) {
11387 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
11388 --kw_vars;
11389 --required_kw_vars;
11390 kwn = kwn->nd_next;
11391 }
11392
11393 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
11394 ID vid = kwn->nd_body->nd_vid;
11395 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
11396 *required_kw_vars++ = vid;
11397 }
11398 else {
11399 *kw_vars++ = vid;
11400 }
11401 }
11402
11403 arg_var(p, kw_bits);
11404 if (kw_rest_arg) arg_var(p, kw_rest_arg);
11405 if (block) arg_var(p, block);
11406
11407 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11408 args->kw_rest_arg->nd_cflag = kw_bits;
11409 }
11410 else if (kw_rest_arg == idNil) {
11411 args->no_kwarg = 1;
11412 }
11413 else if (kw_rest_arg) {
11414 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11415 }
11416
11417 p->ruby_sourceline = saved_line;
11418 return node;
11419}
11420
11421static NODE *
11422args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
11423{
11424 if (max_numparam > NO_PARAM) {
11425 if (!args) {
11426 YYLTYPE loc = RUBY_INIT_YYLLOC();
11427 args = new_args_tail(p, 0, 0, 0, 0);
11428 nd_set_loc(args, &loc);
11429 }
11430 args->nd_ainfo->pre_args_num = max_numparam;
11431 }
11432 return args;
11433}
11434
11435static NODE*
11436new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
11437{
11438 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
11439
11440 aryptn->nd_pconst = constant;
11441
11442 if (pre_arg) {
11443 NODE *pre_args = NEW_LIST(pre_arg, loc);
11444 if (apinfo->pre_args) {
11445 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
11446 }
11447 else {
11448 apinfo->pre_args = pre_args;
11449 }
11450 }
11451 return aryptn;
11452}
11453
11454static NODE*
11455new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
11456{
11457 int saved_line = p->ruby_sourceline;
11458 NODE *node;
11459 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11460 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
11461 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
11462 node = NEW_NODE(NODE_ARYPTN, 0, 0, apinfo, loc);
11463 apinfo->imemo = tmpbuf;
11464 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11465
11466 apinfo->pre_args = pre_args;
11467
11468 if (has_rest) {
11469 if (rest_arg) {
11470 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
11471 }
11472 else {
11473 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
11474 }
11475 }
11476 else {
11477 apinfo->rest_arg = NULL;
11478 }
11479
11480 apinfo->post_args = post_args;
11481
11482 p->ruby_sourceline = saved_line;
11483 return node;
11484}
11485
11486static NODE*
11487new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
11488{
11489 hshptn->nd_pconst = constant;
11490 return hshptn;
11491}
11492
11493static NODE*
11494new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
11495{
11496 int saved_line = p->ruby_sourceline;
11497 NODE *node, *kw_rest_arg_node;
11498
11499 if (kw_rest_arg == idNil) {
11500 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
11501 }
11502 else if (kw_rest_arg) {
11503 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
11504 }
11505 else {
11506 kw_rest_arg_node = NULL;
11507 }
11508
11509 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
11510
11511 p->ruby_sourceline = saved_line;
11512 return node;
11513}
11514
11515static NODE *
11516new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc)
11517{
11518 NODE *node = NEW_CASE3(val, pat, loc);
11519
11520 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL))
11521 rb_warn0L(nd_line(node), "Pattern matching is experimental, and the behavior may change in future versions of Ruby!");
11522 return node;
11523}
11524
11525static NODE*
11526dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11527{
11528 VALUE lit;
11529
11530 if (!node) {
11531 return NEW_LIT(ID2SYM(idNULL), loc);
11532 }
11533
11534 switch (nd_type(node)) {
11535 case NODE_DSTR:
11536 nd_set_type(node, NODE_DSYM);
11537 nd_set_loc(node, loc);
11538 break;
11539 case NODE_STR:
11540 lit = node->nd_lit;
11541 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
11542 nd_set_type(node, NODE_LIT);
11543 nd_set_loc(node, loc);
11544 break;
11545 default:
11546 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
11547 break;
11548 }
11549 return node;
11550}
11551
11552static int
11553append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
11554{
11555 NODE *node = (NODE *)v;
11556 NODE **result = (NODE **)h;
11557 node->nd_alen = 2;
11558 node->nd_next->nd_end = node->nd_next;
11559 node->nd_next->nd_next = 0;
11560 if (*result)
11561 list_concat(*result, node);
11562 else
11563 *result = node;
11564 return ST_CONTINUE;
11565}
11566
11567static NODE *
11568remove_duplicate_keys(struct parser_params *p, NODE *hash)
11569{
11570 st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
11571 NODE *result = 0;
11572 rb_code_location_t loc = hash->nd_loc;
11573 while (hash && hash->nd_head && hash->nd_next) {
11574 NODE *head = hash->nd_head;
11575 NODE *value = hash->nd_next;
11576 NODE *next = value->nd_next;
11577 VALUE key = (VALUE)head;
11578 st_data_t data;
11579 if (nd_type(head) == NODE_LIT &&
11580 st_lookup(literal_keys, (key = head->nd_lit), &data)) {
11581 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
11582 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
11583 head->nd_lit, nd_line(head));
11584 head = ((NODE *)data)->nd_next;
11585 head->nd_head = block_append(p, head->nd_head, value->nd_head);
11586 }
11587 else {
11588 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
11589 }
11590 hash = next;
11591 }
11592 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
11593 st_free_table(literal_keys);
11594 if (hash) {
11595 if (!result) result = hash;
11596 else list_concat(result, hash);
11597 }
11598 result->nd_loc = loc;
11599 return result;
11600}
11601
11602static NODE *
11603new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11604{
11605 if (hash) hash = remove_duplicate_keys(p, hash);
11606 return NEW_HASH(hash, loc);
11607}
11608#endif
11609
11610static void
11611error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
11612{
11613 if (is_private_local_id(id)) {
11614 return;
11615 }
11616 if (st_is_member(p->pvtbl, id)) {
11617 yyerror1(loc, "duplicated variable name");
11618 }
11619 else {
11620 st_insert(p->pvtbl, (st_data_t)id, 0);
11621 }
11622}
11623
11624static void
11625error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
11626{
11627 if (!p->pktbl) {
11628 p->pktbl = st_init_numtable();
11629 }
11630 else if (st_is_member(p->pktbl, key)) {
11631 yyerror1(loc, "duplicated key name");
11632 return;
11633 }
11634 st_insert(p->pktbl, (st_data_t)key, 0);
11635}
11636
11637#ifndef RIPPER
11638static NODE *
11639new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11640{
11641 return NEW_HASH(hash, loc);
11642}
11643#endif /* !RIPPER */
11644
11645#ifndef RIPPER
11646static NODE *
11647new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11648{
11649 NODE *asgn;
11650
11651 if (lhs) {
11652 ID vid = lhs->nd_vid;
11653 YYLTYPE lhs_loc = lhs->nd_loc;
11654 if (op == tOROP) {
11655 lhs->nd_value = rhs;
11656 nd_set_loc(lhs, loc);
11657 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
11658 if (is_notop_id(vid)) {
11659 switch (id_type(vid)) {
11660 case ID_GLOBAL:
11661 case ID_INSTANCE:
11662 case ID_CLASS:
11663 asgn->nd_aid = vid;
11664 }
11665 }
11666 }
11667 else if (op == tANDOP) {
11668 lhs->nd_value = rhs;
11669 nd_set_loc(lhs, loc);
11670 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
11671 }
11672 else {
11673 asgn = lhs;
11674 asgn->nd_value = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
11675 nd_set_loc(asgn, loc);
11676 }
11677 }
11678 else {
11679 asgn = NEW_BEGIN(0, loc);
11680 }
11681 return asgn;
11682}
11683
11684static NODE *
11685new_ary_op_assign(struct parser_params *p, NODE *ary,
11686 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
11687{
11688 NODE *asgn;
11689
11690 args = make_list(args, args_loc);
11691 if (nd_type(args) == NODE_BLOCK_PASS) {
11692 args = NEW_ARGSCAT(args, rhs, loc);
11693 }
11694 else {
11695 args = arg_concat(p, args, rhs, loc);
11696 }
11697 asgn = NEW_OP_ASGN1(ary, op, args, loc);
11698 fixpos(asgn, ary);
11699 return asgn;
11700}
11701
11702static NODE *
11703new_attr_op_assign(struct parser_params *p, NODE *lhs,
11704 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
11705{
11706 NODE *asgn;
11707
11708 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
11709 fixpos(asgn, lhs);
11710 return asgn;
11711}
11712
11713static NODE *
11714new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11715{
11716 NODE *asgn;
11717
11718 if (lhs) {
11719 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
11720 }
11721 else {
11722 asgn = NEW_BEGIN(0, loc);
11723 }
11724 fixpos(asgn, lhs);
11725 return asgn;
11726}
11727
11728static NODE *
11729const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
11730{
11731 if (p->in_def) {
11732 yyerror1(loc, "dynamic constant assignment");
11733 }
11734 return NEW_CDECL(0, 0, (path), loc);
11735}
11736#else
11737static VALUE
11738const_decl(struct parser_params *p, VALUE path)
11739{
11740 if (p->in_def) {
11741 path = dispatch1(assign_error, path);
11742 ripper_error(p);
11743 }
11744 return path;
11745}
11746
11747static VALUE
11748assign_error(struct parser_params *p, VALUE a)
11749{
11750 a = dispatch1(assign_error, a);
11751 ripper_error(p);
11752 return a;
11753}
11754
11755static VALUE
11756var_field(struct parser_params *p, VALUE a)
11757{
11758 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
11759}
11760#endif
11761
11762#ifndef RIPPER
11763static NODE *
11764new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
11765{
11766 NODE *result = head;
11767 if (rescue) {
11768 NODE *tmp = rescue_else ? rescue_else : rescue;
11769 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
11770
11771 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
11772 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
11773 }
11774 else if (rescue_else) {
11775 result = block_append(p, result, rescue_else);
11776 }
11777 if (ensure) {
11778 result = NEW_ENSURE(result, ensure, loc);
11779 }
11780 fixpos(result, head);
11781 return result;
11782}
11783#endif
11784
11785static void
11786warn_unused_var(struct parser_params *p, struct local_vars *local)
11787{
11788 int cnt;
11789
11790 if (!local->used) return;
11791 cnt = local->used->pos;
11792 if (cnt != local->vars->pos) {
11793 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
11794 }
11795#ifndef RIPPER
11796 ID *v = local->vars->tbl;
11797 ID *u = local->used->tbl;
11798 for (int i = 0; i < cnt; ++i) {
11799 if (!v[i] || (u[i] & LVAR_USED)) continue;
11800 if (is_private_local_id(v[i])) continue;
11801 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
11802 }
11803#endif
11804}
11805
11806static void
11807local_push(struct parser_params *p, int toplevel_scope)
11808{
11809 struct local_vars *local;
11810 int inherits_dvars = toplevel_scope && compile_for_eval;
11811 int warn_unused_vars = RTEST(ruby_verbose);
11812
11813 local = ALLOC(struct local_vars);
11814 local->prev = p->lvtbl;
11815 local->args = vtable_alloc(0);
11816 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
11817#ifndef RIPPER
11818 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
11819 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
11820 local->numparam.outer = 0;
11821 local->numparam.inner = 0;
11822 local->numparam.current = 0;
11823#endif
11824 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
11825
11826# if WARN_PAST_SCOPE
11827 local->past = 0;
11828# endif
11829 CMDARG_PUSH(0);
11830 COND_PUSH(0);
11831 p->lvtbl = local;
11832}
11833
11834static void
11835local_pop(struct parser_params *p)
11836{
11837 struct local_vars *local = p->lvtbl->prev;
11838 if (p->lvtbl->used) {
11839 warn_unused_var(p, p->lvtbl);
11840 vtable_free(p->lvtbl->used);
11841 }
11842# if WARN_PAST_SCOPE
11843 while (p->lvtbl->past) {
11844 struct vtable *past = p->lvtbl->past;
11845 p->lvtbl->past = past->prev;
11846 vtable_free(past);
11847 }
11848# endif
11849 vtable_free(p->lvtbl->args);
11850 vtable_free(p->lvtbl->vars);
11851 CMDARG_POP();
11852 COND_POP();
11853 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
11854 p->lvtbl = local;
11855}
11856
11857#ifndef RIPPER
11858static ID*
11859local_tbl(struct parser_params *p)
11860{
11861 int cnt_args = vtable_size(p->lvtbl->args);
11862 int cnt_vars = vtable_size(p->lvtbl->vars);
11863 int cnt = cnt_args + cnt_vars;
11864 int i, j;
11865 ID *buf;
11866
11867 if (cnt <= 0) return 0;
11868 buf = ALLOC_N(ID, cnt + 2);
11869 MEMCPY(buf+1, p->lvtbl->args->tbl, ID, cnt_args);
11870 /* remove IDs duplicated to warn shadowing */
11871 for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) {
11872 ID id = p->lvtbl->vars->tbl[i];
11873 if (!vtable_included(p->lvtbl->args, id)) {
11874 buf[j++] = id;
11875 }
11876 }
11877 if (--j < cnt) {
11878 REALLOC_N(buf, ID, (cnt = j) + 2);
11879 }
11880 buf[0] = cnt;
11881 rb_ast_add_local_table(p->ast, buf);
11882
11883 return buf;
11884}
11885
11886static NODE*
11887node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
11888{
11889 ID *a0;
11890 NODE *n;
11891
11892 a0 = local_tbl(p);
11893 n = NEW_NODE(type, a0, a1, a2, loc);
11894 return n;
11895}
11896
11897#endif
11898
11899static void
11900numparam_name(struct parser_params *p, ID id)
11901{
11902 if (!NUMPARAM_ID_P(id)) return;
11903 rb_warn1("`_%d' is reserved for numbered parameter; consider another name",
11904 WARN_I(NUMPARAM_ID_TO_IDX(id)));
11905}
11906
11907static void
11908arg_var(struct parser_params *p, ID id)
11909{
11910 numparam_name(p, id);
11911 vtable_add(p->lvtbl->args, id);
11912}
11913
11914static void
11915local_var(struct parser_params *p, ID id)
11916{
11917 numparam_name(p, id);
11918 vtable_add(p->lvtbl->vars, id);
11919 if (p->lvtbl->used) {
11920 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
11921 }
11922}
11923
11924static int
11925local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
11926{
11927 struct vtable *vars, *args, *used;
11928
11929 vars = p->lvtbl->vars;
11930 args = p->lvtbl->args;
11931 used = p->lvtbl->used;
11932
11933 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
11934 vars = vars->prev;
11935 args = args->prev;
11936 if (used) used = used->prev;
11937 }
11938
11939 if (vars && vars->prev == DVARS_INHERIT) {
11940 return rb_local_defined(id, p->parent_iseq);
11941 }
11942 else if (vtable_included(args, id)) {
11943 return 1;
11944 }
11945 else {
11946 int i = vtable_included(vars, id);
11947 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
11948 return i != 0;
11949 }
11950}
11951
11952static int
11953local_id(struct parser_params *p, ID id)
11954{
11955 return local_id_ref(p, id, NULL);
11956}
11957
11958static NODE *
11959numparam_push(struct parser_params *p)
11960{
11961#ifndef RIPPER
11962 struct local_vars *local = p->lvtbl;
11963 NODE *inner = local->numparam.inner;
11964 if (!local->numparam.outer) {
11965 local->numparam.outer = local->numparam.current;
11966 }
11967 local->numparam.inner = 0;
11968 local->numparam.current = 0;
11969 return inner;
11970#else
11971 return 0;
11972#endif
11973}
11974
11975static void
11976numparam_pop(struct parser_params *p, NODE *prev_inner)
11977{
11978#ifndef RIPPER
11979 struct local_vars *local = p->lvtbl;
11980 if (prev_inner) {
11981 /* prefer first one */
11982 local->numparam.inner = prev_inner;
11983 }
11984 else if (local->numparam.current) {
11985 /* current and inner are exclusive */
11986 local->numparam.inner = local->numparam.current;
11987 }
11988 if (p->max_numparam > NO_PARAM) {
11989 /* current and outer are exclusive */
11990 local->numparam.current = local->numparam.outer;
11991 local->numparam.outer = 0;
11992 }
11993 else {
11994 /* no numbered parameter */
11995 local->numparam.current = 0;
11996 }
11997#endif
11998}
11999
12000static const struct vtable *
12001dyna_push(struct parser_params *p)
12002{
12003 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
12004 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
12005 if (p->lvtbl->used) {
12006 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
12007 }
12008 return p->lvtbl->args;
12009}
12010
12011static void
12012dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
12013{
12014 struct vtable *tmp = *vtblp;
12015 *vtblp = tmp->prev;
12016# if WARN_PAST_SCOPE
12017 if (p->past_scope_enabled) {
12018 tmp->prev = p->lvtbl->past;
12019 p->lvtbl->past = tmp;
12020 return;
12021 }
12022# endif
12023 vtable_free(tmp);
12024}
12025
12026static void
12027dyna_pop_1(struct parser_params *p)
12028{
12029 struct vtable *tmp;
12030
12031 if ((tmp = p->lvtbl->used) != 0) {
12032 warn_unused_var(p, p->lvtbl);
12033 p->lvtbl->used = p->lvtbl->used->prev;
12034 vtable_free(tmp);
12035 }
12036 dyna_pop_vtable(p, &p->lvtbl->args);
12037 dyna_pop_vtable(p, &p->lvtbl->vars);
12038}
12039
12040static void
12041dyna_pop(struct parser_params *p, const struct vtable *lvargs)
12042{
12043 while (p->lvtbl->args != lvargs) {
12044 dyna_pop_1(p);
12045 if (!p->lvtbl->args) {
12046 struct local_vars *local = p->lvtbl->prev;
12047 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12048 p->lvtbl = local;
12049 }
12050 }
12051 dyna_pop_1(p);
12052}
12053
12054static int
12055dyna_in_block(struct parser_params *p)
12056{
12057 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12058}
12059
12060static int
12061dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12062{
12063 struct vtable *vars, *args, *used;
12064 int i;
12065
12066 args = p->lvtbl->args;
12067 vars = p->lvtbl->vars;
12068 used = p->lvtbl->used;
12069
12070 while (!DVARS_TERMINAL_P(vars)) {
12071 if (vtable_included(args, id)) {
12072 return 1;
12073 }
12074 if ((i = vtable_included(vars, id)) != 0) {
12075 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12076 return 1;
12077 }
12078 args = args->prev;
12079 vars = vars->prev;
12080 if (!vidrefp) used = 0;
12081 if (used) used = used->prev;
12082 }
12083
12084 if (vars == DVARS_INHERIT) {
12085 return rb_dvar_defined(id, p->parent_iseq);
12086 }
12087
12088 return 0;
12089}
12090
12091static int
12092dvar_defined(struct parser_params *p, ID id)
12093{
12094 return dvar_defined_ref(p, id, NULL);
12095}
12096
12097static int
12098dvar_curr(struct parser_params *p, ID id)
12099{
12100 return (vtable_included(p->lvtbl->args, id) ||
12101 vtable_included(p->lvtbl->vars, id));
12102}
12103
12104static void
12105reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12106{
12107 compile_error(p,
12108 "regexp encoding option '%c' differs from source encoding '%s'",
12109 c, rb_enc_name(rb_enc_get(str)));
12110}
12111
12112#ifndef RIPPER
12113int
12114rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12115{
12116 int c = RE_OPTION_ENCODING_IDX(options);
12117
12118 if (c) {
12119 int opt, idx;
12120 rb_char_to_option_kcode(c, &opt, &idx);
12121 if (idx != ENCODING_GET(str) &&
12122 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12123 goto error;
12124 }
12125 ENCODING_SET(str, idx);
12126 }
12127 else if (RE_OPTION_ENCODING_NONE(options)) {
12128 if (!ENCODING_IS_ASCII8BIT(str) &&
12129 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12130 c = 'n';
12131 goto error;
12132 }
12133 rb_enc_associate(str, rb_ascii8bit_encoding());
12134 }
12135 else if (p->enc == rb_usascii_encoding()) {
12136 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12137 /* raise in re.c */
12138 rb_enc_associate(str, rb_usascii_encoding());
12139 }
12140 else {
12141 rb_enc_associate(str, rb_ascii8bit_encoding());
12142 }
12143 }
12144 return 0;
12145
12146 error:
12147 return c;
12148}
12149
12150static void
12151reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12152{
12153 int c = rb_reg_fragment_setenc(p, str, options);
12154 if (c) reg_fragment_enc_error(p, str, c);
12155}
12156
12157static int
12158reg_fragment_check(struct parser_params* p, VALUE str, int options)
12159{
12160 VALUE err;
12161 reg_fragment_setenc(p, str, options);
12162 err = rb_reg_check_preprocess(str);
12163 if (err != Qnil) {
12164 err = rb_obj_as_string(err);
12165 compile_error(p, "%"PRIsVALUE, err);
12166 return 0;
12167 }
12168 return 1;
12169}
12170
12171typedef struct {
12172 struct parser_params* parser;
12173 rb_encoding *enc;
12174 NODE *succ_block;
12175 const YYLTYPE *loc;
12176} reg_named_capture_assign_t;
12177
12178static int
12179reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12180 int back_num, int *back_refs, OnigRegex regex, void *arg0)
12181{
12182 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12183 struct parser_params* p = arg->parser;
12184 rb_encoding *enc = arg->enc;
12185 long len = name_end - name;
12186 const char *s = (const char *)name;
12187 ID var;
12188 NODE *node, *succ;
12189
12190 if (!len) return ST_CONTINUE;
12191 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
12192 return ST_CONTINUE;
12193
12194 var = intern_cstr(s, len, enc);
12195 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
12196 if (!lvar_defined(p, var)) return ST_CONTINUE;
12197 }
12198 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), arg->loc);
12199 succ = arg->succ_block;
12200 if (!succ) succ = NEW_BEGIN(0, arg->loc);
12201 succ = block_append(p, succ, node);
12202 arg->succ_block = succ;
12203 return ST_CONTINUE;
12204}
12205
12206static NODE *
12207reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
12208{
12209 reg_named_capture_assign_t arg;
12210
12211 arg.parser = p;
12212 arg.enc = rb_enc_get(regexp);
12213 arg.succ_block = 0;
12214 arg.loc = loc;
12215 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
12216
12217 if (!arg.succ_block) return 0;
12218 return arg.succ_block->nd_next;
12219}
12220
12221static VALUE
12222parser_reg_compile(struct parser_params* p, VALUE str, int options)
12223{
12224 reg_fragment_setenc(p, str, options);
12225 return rb_parser_reg_compile(p, str, options);
12226}
12227
12228VALUE
12229rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
12230{
12231 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
12232}
12233
12234static VALUE
12235reg_compile(struct parser_params* p, VALUE str, int options)
12236{
12237 VALUE re;
12238 VALUE err;
12239
12240 err = rb_errinfo();
12241 re = parser_reg_compile(p, str, options);
12242 if (NIL_P(re)) {
12243 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
12244 rb_set_errinfo(err);
12245 compile_error(p, "%"PRIsVALUE, m);
12246 return Qnil;
12247 }
12248 return re;
12249}
12250#else
12251static VALUE
12252parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
12253{
12254 VALUE err = rb_errinfo();
12255 VALUE re;
12256 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
12257 int c = rb_reg_fragment_setenc(p, str, options);
12258 if (c) reg_fragment_enc_error(p, str, c);
12259 re = rb_parser_reg_compile(p, str, options);
12260 if (NIL_P(re)) {
12261 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
12262 rb_set_errinfo(err);
12263 }
12264 return re;
12265}
12266#endif
12267
12268#ifndef RIPPER
12269void
12270rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
12271{
12272 struct parser_params *p;
12273 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12274 p->do_print = print;
12275 p->do_loop = loop;
12276 p->do_chomp = chomp;
12277 p->do_split = split;
12278}
12279
12280void
12281rb_parser_warn_location(VALUE vparser, int warn)
12282{
12283 struct parser_params *p;
12284 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12285 p->warn_location = warn;
12286}
12287
12288static NODE *
12289parser_append_options(struct parser_params *p, NODE *node)
12290{
12291 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
12292 const YYLTYPE *const LOC = &default_location;
12293
12294 if (p->do_print) {
12295 NODE *print = NEW_FCALL(rb_intern("print"),
12296 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
12297 LOC);
12298 node = block_append(p, node, print);
12299 }
12300
12301 if (p->do_loop) {
12302 if (p->do_split) {
12303 NODE *args = NEW_LIST(NEW_GVAR(rb_intern("$;"), LOC), LOC);
12304 NODE *split = NEW_GASGN(rb_intern("$F"),
12305 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12306 rb_intern("split"), args, LOC),
12307 LOC);
12308 node = block_append(p, split, node);
12309 }
12310 if (p->do_chomp) {
12311 NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12312 rb_intern("chomp!"), 0, LOC);
12313 node = block_append(p, chomp, node);
12314 }
12315
12316 node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
12317 }
12318
12319 return node;
12320}
12321
12322void
12323rb_init_parse(void)
12324{
12325 /* just to suppress unused-function warnings */
12326 (void)nodetype;
12327 (void)nodeline;
12328}
12329
12330static ID
12331internal_id(struct parser_params *p)
12332{
12333 const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
12334 ID id = (ID)vtable_size(p->lvtbl->args) + (ID)vtable_size(p->lvtbl->vars);
12335 id = max_id - id;
12336 return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
12337}
12338#endif /* !RIPPER */
12339
12340static void
12341parser_initialize(struct parser_params *p)
12342{
12343 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
12344 p->command_start = TRUE;
12345 p->ruby_sourcefile_string = Qnil;
12346 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
12347 p->node_id = 0;
12348#ifdef RIPPER
12349 p->delayed.token = Qnil;
12350 p->result = Qnil;
12351 p->parsing_thread = Qnil;
12352#else
12353 p->error_buffer = Qfalse;
12354#endif
12355 p->debug_buffer = Qnil;
12356 p->debug_output = rb_stdout;
12357 p->enc = rb_utf8_encoding();
12358}
12359
12360#ifdef RIPPER
12361#define parser_mark ripper_parser_mark
12362#define parser_free ripper_parser_free
12363#endif
12364
12365static void
12366parser_mark(void *ptr)
12367{
12368 struct parser_params *p = (struct parser_params*)ptr;
12369
12370 rb_gc_mark(p->lex.input);
12371 rb_gc_mark(p->lex.prevline);
12372 rb_gc_mark(p->lex.lastline);
12373 rb_gc_mark(p->lex.nextline);
12374 rb_gc_mark(p->ruby_sourcefile_string);
12375 rb_gc_mark((VALUE)p->lex.strterm);
12376 rb_gc_mark((VALUE)p->ast);
12377 rb_gc_mark(p->case_labels);
12378#ifndef RIPPER
12379 rb_gc_mark(p->debug_lines);
12380 rb_gc_mark(p->compile_option);
12381 rb_gc_mark(p->error_buffer);
12382#else
12383 rb_gc_mark(p->delayed.token);
12384 rb_gc_mark(p->value);
12385 rb_gc_mark(p->result);
12386 rb_gc_mark(p->parsing_thread);
12387#endif
12388 rb_gc_mark(p->debug_buffer);
12389 rb_gc_mark(p->debug_output);
12390#ifdef YYMALLOC
12391 rb_gc_mark((VALUE)p->heap);
12392#endif
12393}
12394
12395static void
12396parser_free(void *ptr)
12397{
12398 struct parser_params *p = (struct parser_params*)ptr;
12399 struct local_vars *local, *prev;
12400
12401 if (p->tokenbuf) {
12402 ruby_sized_xfree(p->tokenbuf, p->toksiz);
12403 }
12404 for (local = p->lvtbl; local; local = prev) {
12405 if (local->vars) xfree(local->vars);
12406 prev = local->prev;
12407 xfree(local);
12408 }
12409 {
12410 token_info *ptinfo;
12411 while ((ptinfo = p->token_info) != 0) {
12412 p->token_info = ptinfo->next;
12413 xfree(ptinfo);
12414 }
12415 }
12416 xfree(ptr);
12417}
12418
12419static size_t
12420parser_memsize(const void *ptr)
12421{
12422 struct parser_params *p = (struct parser_params*)ptr;
12423 struct local_vars *local;
12424 size_t size = sizeof(*p);
12425
12426 size += p->toksiz;
12427 for (local = p->lvtbl; local; local = local->prev) {
12428 size += sizeof(*local);
12429 if (local->vars) size += local->vars->capa * sizeof(ID);
12430 }
12431 return size;
12432}
12433
12434static const rb_data_type_t parser_data_type = {
12435#ifndef RIPPER
12436 "parser",
12437#else
12438 "ripper",
12439#endif
12440 {
12441 parser_mark,
12442 parser_free,
12443 parser_memsize,
12444 },
12445 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
12446};
12447
12448#ifndef RIPPER
12449#undef rb_reserved_word
12450
12451const struct kwtable *
12452rb_reserved_word(const char *str, unsigned int len)
12453{
12454 return reserved_word(str, len);
12455}
12456
12457VALUE
12458rb_parser_new(void)
12459{
12460 struct parser_params *p;
12461 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
12462 &parser_data_type, p);
12463 parser_initialize(p);
12464 return parser;
12465}
12466
12467VALUE
12468rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
12469{
12470 struct parser_params *p;
12471
12472 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12473 p->error_buffer = main ? Qfalse : Qnil;
12474 p->parent_iseq = base;
12475 return vparser;
12476}
12477#endif
12478
12479#ifdef RIPPER
12480#define rb_parser_end_seen_p ripper_parser_end_seen_p
12481#define rb_parser_encoding ripper_parser_encoding
12482#define rb_parser_get_yydebug ripper_parser_get_yydebug
12483#define rb_parser_set_yydebug ripper_parser_set_yydebug
12484#define rb_parser_get_debug_output ripper_parser_get_debug_output
12485#define rb_parser_set_debug_output ripper_parser_set_debug_output
12486static VALUE ripper_parser_end_seen_p(VALUE vparser);
12487static VALUE ripper_parser_encoding(VALUE vparser);
12488static VALUE ripper_parser_get_yydebug(VALUE self);
12489static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
12490static VALUE ripper_parser_get_debug_output(VALUE self);
12491static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
12492
12493/*
12494 * call-seq:
12495 * ripper.error? -> Boolean
12496 *
12497 * Return true if parsed source has errors.
12498 */
12499static VALUE
12500ripper_error_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->error_p ? Qtrue : Qfalse;
12506}
12507#endif
12508
12509/*
12510 * call-seq:
12511 * ripper.end_seen? -> Boolean
12512 *
12513 * Return true if parsed source ended by +\_\_END\_\_+.
12514 */
12515VALUE
12516rb_parser_end_seen_p(VALUE vparser)
12517{
12518 struct parser_params *p;
12519
12520 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12521 return p->ruby__end__seen ? Qtrue : Qfalse;
12522}
12523
12524/*
12525 * call-seq:
12526 * ripper.encoding -> encoding
12527 *
12528 * Return encoding of the source.
12529 */
12530VALUE
12531rb_parser_encoding(VALUE vparser)
12532{
12533 struct parser_params *p;
12534
12535 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12536 return rb_enc_from_encoding(p->enc);
12537}
12538
12539#ifdef RIPPER
12540/*
12541 * call-seq:
12542 * ripper.yydebug -> true or false
12543 *
12544 * Get yydebug.
12545 */
12546VALUE
12547rb_parser_get_yydebug(VALUE self)
12548{
12549 struct parser_params *p;
12550
12551 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12552 return p->debug ? Qtrue : Qfalse;
12553}
12554#endif
12555
12556/*
12557 * call-seq:
12558 * ripper.yydebug = flag
12559 *
12560 * Set yydebug.
12561 */
12562VALUE
12563rb_parser_set_yydebug(VALUE self, VALUE flag)
12564{
12565 struct parser_params *p;
12566
12567 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12568 p->debug = RTEST(flag);
12569 return flag;
12570}
12571
12572/*
12573 * call-seq:
12574 * ripper.debug_output -> obj
12575 *
12576 * Get debug output.
12577 */
12578VALUE
12579rb_parser_get_debug_output(VALUE self)
12580{
12581 struct parser_params *p;
12582
12583 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12584 return p->debug_output;
12585}
12586
12587/*
12588 * call-seq:
12589 * ripper.debug_output = obj
12590 *
12591 * Set debug output.
12592 */
12593VALUE
12594rb_parser_set_debug_output(VALUE self, VALUE output)
12595{
12596 struct parser_params *p;
12597
12598 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12599 return p->debug_output = output;
12600}
12601
12602#ifndef RIPPER
12603#ifdef YYMALLOC
12604#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
12605/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
12606 * potential memory leak */
12607#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
12608#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
12609 (new)->cnt = (cnt), (ptr))
12610
12611void *
12612rb_parser_malloc(struct parser_params *p, size_t size)
12613{
12614 size_t cnt = HEAPCNT(1, size);
12615 rb_imemo_tmpbuf_t *n = NEWHEAP();
12616 void *ptr = xmalloc(size);
12617
12618 return ADD2HEAP(n, cnt, ptr);
12619}
12620
12621void *
12622rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
12623{
12624 size_t cnt = HEAPCNT(nelem, size);
12625 rb_imemo_tmpbuf_t *n = NEWHEAP();
12626 void *ptr = xcalloc(nelem, size);
12627
12628 return ADD2HEAP(n, cnt, ptr);
12629}
12630
12631void *
12632rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
12633{
12634 rb_imemo_tmpbuf_t *n;
12635 size_t cnt = HEAPCNT(1, size);
12636
12637 if (ptr && (n = p->heap) != NULL) {
12638 do {
12639 if (n->ptr == ptr) {
12640 n->ptr = ptr = xrealloc(ptr, size);
12641 if (n->cnt) n->cnt = cnt;
12642 return ptr;
12643 }
12644 } while ((n = n->next) != NULL);
12645 }
12646 n = NEWHEAP();
12647 ptr = xrealloc(ptr, size);
12648 return ADD2HEAP(n, cnt, ptr);
12649}
12650
12651void
12652rb_parser_free(struct parser_params *p, void *ptr)
12653{
12654 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
12655
12656 while ((n = *prev) != NULL) {
12657 if (n->ptr == ptr) {
12658 *prev = n->next;
12659 rb_gc_force_recycle((VALUE)n);
12660 break;
12661 }
12662 prev = &n->next;
12663 }
12664 xfree(ptr);
12665}
12666#endif
12667
12668void
12669rb_parser_printf(struct parser_params *p, const char *fmt, ...)
12670{
12671 va_list ap;
12672 VALUE mesg = p->debug_buffer;
12673
12674 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
12675 va_start(ap, fmt);
12676 rb_str_vcatf(mesg, fmt, ap);
12677 va_end(ap);
12678 if (RSTRING_END(mesg)[-1] == '\n') {
12679 rb_io_write(p->debug_output, mesg);
12680 p->debug_buffer = Qnil;
12681 }
12682}
12683
12684static void
12685parser_compile_error(struct parser_params *p, const char *fmt, ...)
12686{
12687 va_list ap;
12688
12689 rb_io_flush(p->debug_output);
12690 p->error_p = 1;
12691 va_start(ap, fmt);
12692 p->error_buffer =
12693 rb_syntax_error_append(p->error_buffer,
12694 p->ruby_sourcefile_string,
12695 p->ruby_sourceline,
12696 rb_long2int(p->lex.pcur - p->lex.pbeg),
12697 p->enc, fmt, ap);
12698 va_end(ap);
12699}
12700
12701static size_t
12702count_char(const char *str, int c)
12703{
12704 int n = 0;
12705 while (str[n] == c) ++n;
12706 return n;
12707}
12708
12709/*
12710 * strip enclosing double-quotes, same as the default yytnamerr except
12711 * for that single-quotes matching back-quotes do not stop stripping.
12712 *
12713 * "\"`class' keyword\"" => "`class' keyword"
12714 */
12715RUBY_FUNC_EXPORTED size_t
12716rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
12717{
12718 if (*yystr == '"') {
12719 size_t yyn = 0, bquote = 0;
12720 const char *yyp = yystr;
12721
12722 while (*++yyp) {
12723 switch (*yyp) {
12724 case '`':
12725 if (!bquote) {
12726 bquote = count_char(yyp+1, '`') + 1;
12727 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
12728 yyn += bquote;
12729 yyp += bquote - 1;
12730 break;
12731 }
12732 goto default_char;
12733
12734 case '\'':
12735 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
12736 if (yyres) memcpy(yyres + yyn, yyp, bquote);
12737 yyn += bquote;
12738 yyp += bquote - 1;
12739 bquote = 0;
12740 break;
12741 }
12742 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
12743 if (yyres) memcpy(yyres + yyn, yyp, 3);
12744 yyn += 3;
12745 yyp += 2;
12746 break;
12747 }
12748 goto do_not_strip_quotes;
12749
12750 case ',':
12751 goto do_not_strip_quotes;
12752
12753 case '\\':
12754 if (*++yyp != '\\')
12755 goto do_not_strip_quotes;
12756 /* Fall through. */
12757 default_char:
12758 default:
12759 if (yyres)
12760 yyres[yyn] = *yyp;
12761 yyn++;
12762 break;
12763
12764 case '"':
12765 case '\0':
12766 if (yyres)
12767 yyres[yyn] = '\0';
12768 return yyn;
12769 }
12770 }
12771 do_not_strip_quotes: ;
12772 }
12773
12774 if (!yyres) return strlen(yystr);
12775
12776 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
12777}
12778#endif
12779
12780#ifdef RIPPER
12781#ifdef RIPPER_DEBUG
12782/* :nodoc: */
12783static VALUE
12784ripper_validate_object(VALUE self, VALUE x)
12785{
12786 if (x == Qfalse) return x;
12787 if (x == Qtrue) return x;
12788 if (x == Qnil) return x;
12789 if (x == Qundef)
12790 rb_raise(rb_eArgError, "Qundef given");
12791 if (FIXNUM_P(x)) return x;
12792 if (SYMBOL_P(x)) return x;
12793 switch (BUILTIN_TYPE(x)) {
12794 case T_STRING:
12795 case T_OBJECT:
12796 case T_ARRAY:
12797 case T_BIGNUM:
12798 case T_FLOAT:
12799 case T_COMPLEX:
12800 case T_RATIONAL:
12801 break;
12802 case T_NODE:
12803 if (nd_type((NODE *)x) != NODE_RIPPER) {
12804 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
12805 }
12806 x = ((NODE *)x)->nd_rval;
12807 break;
12808 default:
12809 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
12810 (void *)x, rb_obj_classname(x));
12811 }
12812 if (!RBASIC_CLASS(x)) {
12813 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
12814 (void *)x, rb_builtin_type_name(TYPE(x)));
12815 }
12816 return x;
12817}
12818#endif
12819
12820#define validate(x) ((x) = get_value(x))
12821
12822static VALUE
12823ripper_dispatch0(struct parser_params *p, ID mid)
12824{
12825 return rb_funcall(p->value, mid, 0);
12826}
12827
12828static VALUE
12829ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
12830{
12831 validate(a);
12832 return rb_funcall(p->value, mid, 1, a);
12833}
12834
12835static VALUE
12836ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
12837{
12838 validate(a);
12839 validate(b);
12840 return rb_funcall(p->value, mid, 2, a, b);
12841}
12842
12843static VALUE
12844ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
12845{
12846 validate(a);
12847 validate(b);
12848 validate(c);
12849 return rb_funcall(p->value, mid, 3, a, b, c);
12850}
12851
12852static VALUE
12853ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
12854{
12855 validate(a);
12856 validate(b);
12857 validate(c);
12858 validate(d);
12859 return rb_funcall(p->value, mid, 4, a, b, c, d);
12860}
12861
12862static VALUE
12863ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
12864{
12865 validate(a);
12866 validate(b);
12867 validate(c);
12868 validate(d);
12869 validate(e);
12870 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
12871}
12872
12873static VALUE
12874ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
12875{
12876 validate(a);
12877 validate(b);
12878 validate(c);
12879 validate(d);
12880 validate(e);
12881 validate(f);
12882 validate(g);
12883 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
12884}
12885
12886static ID
12887ripper_get_id(VALUE v)
12888{
12889 NODE *nd;
12890 if (!RB_TYPE_P(v, T_NODE)) return 0;
12891 nd = (NODE *)v;
12892 if (nd_type(nd) != NODE_RIPPER) return 0;
12893 return nd->nd_vid;
12894}
12895
12896static VALUE
12897ripper_get_value(VALUE v)
12898{
12899 NODE *nd;
12900 if (v == Qundef) return Qnil;
12901 if (!RB_TYPE_P(v, T_NODE)) return v;
12902 nd = (NODE *)v;
12903 if (nd_type(nd) != NODE_RIPPER) return Qnil;
12904 return nd->nd_rval;
12905}
12906
12907static void
12908ripper_error(struct parser_params *p)
12909{
12910 p->error_p = TRUE;
12911}
12912
12913static void
12914ripper_compile_error(struct parser_params *p, const char *fmt, ...)
12915{
12916 VALUE str;
12917 va_list args;
12918
12919 va_start(args, fmt);
12920 str = rb_vsprintf(fmt, args);
12921 va_end(args);
12922 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
12923 ripper_error(p);
12924}
12925
12926static VALUE
12927ripper_lex_get_generic(struct parser_params *p, VALUE src)
12928{
12929 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
12930 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
12931 rb_raise(rb_eTypeError,
12932 "gets returned %"PRIsVALUE" (expected String or nil)",
12933 rb_obj_class(line));
12934 }
12935 return line;
12936}
12937
12938static VALUE
12939ripper_lex_io_get(struct parser_params *p, VALUE src)
12940{
12941 return rb_io_gets(src);
12942}
12943
12944static VALUE
12945ripper_s_allocate(VALUE klass)
12946{
12947 struct parser_params *p;
12948 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
12949 &parser_data_type, p);
12950 p->value = self;
12951 return self;
12952}
12953
12954#define ripper_initialized_p(r) ((r)->lex.input != 0)
12955
12956/*
12957 * call-seq:
12958 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
12959 *
12960 * Create a new Ripper object.
12961 * _src_ must be a String, an IO, or an Object which has #gets method.
12962 *
12963 * This method does not starts parsing.
12964 * See also Ripper#parse and Ripper.parse.
12965 */
12966static VALUE
12967ripper_initialize(int argc, VALUE *argv, VALUE self)
12968{
12969 struct parser_params *p;
12970 VALUE src, fname, lineno;
12971
12972 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12973 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
12974 if (RB_TYPE_P(src, T_FILE)) {
12975 p->lex.gets = ripper_lex_io_get;
12976 }
12977 else if (rb_respond_to(src, id_gets)) {
12978 p->lex.gets = ripper_lex_get_generic;
12979 }
12980 else {
12981 StringValue(src);
12982 p->lex.gets = lex_get_str;
12983 }
12984 p->lex.input = src;
12985 p->eofp = 0;
12986 if (NIL_P(fname)) {
12987 fname = STR_NEW2("(ripper)");
12988 OBJ_FREEZE(fname);
12989 }
12990 else {
12991 StringValueCStr(fname);
12992 fname = rb_str_new_frozen(fname);
12993 }
12994 parser_initialize(p);
12995
12996 p->ruby_sourcefile_string = fname;
12997 p->ruby_sourcefile = RSTRING_PTR(fname);
12998 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
12999
13000 return Qnil;
13001}
13002
13003static VALUE
13004ripper_parse0(VALUE parser_v)
13005{
13006 struct parser_params *p;
13007
13008 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13009 parser_prepare(p);
13010 p->ast = rb_ast_new();
13011 ripper_yyparse((void*)p);
13012 rb_ast_dispose(p->ast);
13013 p->ast = 0;
13014 return p->result;
13015}
13016
13017static VALUE
13018ripper_ensure(VALUE parser_v)
13019{
13020 struct parser_params *p;
13021
13022 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13023 p->parsing_thread = Qnil;
13024 return Qnil;
13025}
13026
13027/*
13028 * call-seq:
13029 * ripper.parse
13030 *
13031 * Start parsing and returns the value of the root action.
13032 */
13033static VALUE
13034ripper_parse(VALUE self)
13035{
13036 struct parser_params *p;
13037
13038 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13039 if (!ripper_initialized_p(p)) {
13040 rb_raise(rb_eArgError, "method called for uninitialized object");
13041 }
13042 if (!NIL_P(p->parsing_thread)) {
13043 if (p->parsing_thread == rb_thread_current())
13044 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13045 else
13046 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13047 }
13048 p->parsing_thread = rb_thread_current();
13049 rb_ensure(ripper_parse0, self, ripper_ensure, self);
13050
13051 return p->result;
13052}
13053
13054/*
13055 * call-seq:
13056 * ripper.column -> Integer
13057 *
13058 * Return column number of current parsing line.
13059 * This number starts from 0.
13060 */
13061static VALUE
13062ripper_column(VALUE self)
13063{
13064 struct parser_params *p;
13065 long col;
13066
13067 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13068 if (!ripper_initialized_p(p)) {
13069 rb_raise(rb_eArgError, "method called for uninitialized object");
13070 }
13071 if (NIL_P(p->parsing_thread)) return Qnil;
13072 col = p->lex.ptok - p->lex.pbeg;
13073 return LONG2NUM(col);
13074}
13075
13076/*
13077 * call-seq:
13078 * ripper.filename -> String
13079 *
13080 * Return current parsing filename.
13081 */
13082static VALUE
13083ripper_filename(VALUE self)
13084{
13085 struct parser_params *p;
13086
13087 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13088 if (!ripper_initialized_p(p)) {
13089 rb_raise(rb_eArgError, "method called for uninitialized object");
13090 }
13091 return p->ruby_sourcefile_string;
13092}
13093
13094/*
13095 * call-seq:
13096 * ripper.lineno -> Integer
13097 *
13098 * Return line number of current parsing line.
13099 * This number starts from 1.
13100 */
13101static VALUE
13102ripper_lineno(VALUE self)
13103{
13104 struct parser_params *p;
13105
13106 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13107 if (!ripper_initialized_p(p)) {
13108 rb_raise(rb_eArgError, "method called for uninitialized object");
13109 }
13110 if (NIL_P(p->parsing_thread)) return Qnil;
13111 return INT2NUM(p->ruby_sourceline);
13112}
13113
13114/*
13115 * call-seq:
13116 * ripper.state -> Integer
13117 *
13118 * Return scanner state of current token.
13119 */
13120static VALUE
13121ripper_state(VALUE self)
13122{
13123 struct parser_params *p;
13124
13125 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13126 if (!ripper_initialized_p(p)) {
13127 rb_raise(rb_eArgError, "method called for uninitialized object");
13128 }
13129 if (NIL_P(p->parsing_thread)) return Qnil;
13130 return INT2NUM(p->lex.state);
13131}
13132
13133/*
13134 * call-seq:
13135 * ripper.token -> String
13136 *
13137 * Return the current token string.
13138 */
13139static VALUE
13140ripper_token(VALUE self)
13141{
13142 struct parser_params *p;
13143 long pos, len;
13144
13145 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13146 if (!ripper_initialized_p(p)) {
13147 rb_raise(rb_eArgError, "method called for uninitialized object");
13148 }
13149 if (NIL_P(p->parsing_thread)) return Qnil;
13150 pos = p->lex.ptok - p->lex.pbeg;
13151 len = p->lex.pcur - p->lex.ptok;
13152 return rb_str_subseq(p->lex.lastline, pos, len);
13153}
13154
13155#ifdef RIPPER_DEBUG
13156/* :nodoc: */
13157static VALUE
13158ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13159{
13160 StringValue(msg);
13161 if (obj == Qundef) {
13162 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13163 }
13164 return Qnil;
13165}
13166
13167/* :nodoc: */
13168static VALUE
13169ripper_value(VALUE self, VALUE obj)
13170{
13171 return ULONG2NUM(obj);
13172}
13173#endif
13174
13175/*
13176 * call-seq:
13177 * Ripper.lex_state_name(integer) -> string
13178 *
13179 * Returns a string representation of lex_state.
13180 */
13181static VALUE
13182ripper_lex_state_name(VALUE self, VALUE state)
13183{
13184 return rb_parser_lex_state_name(NUM2INT(state));
13185}
13186
13187void
13188Init_ripper(void)
13189{
13190 ripper_init_eventids1();
13191 ripper_init_eventids2();
13192 id_warn = rb_intern_const("warn");
13193 id_warning = rb_intern_const("warning");
13194 id_gets = rb_intern_const("gets");
13195 id_assoc = rb_intern_const("=>");
13196
13197 (void)yystpcpy; /* may not used in newer bison */
13198
13199 InitVM(ripper);
13200}
13201
13202void
13203InitVM_ripper(void)
13204{
13205 VALUE Ripper;
13206
13207 Ripper = rb_define_class("Ripper", rb_cObject);
13208 /* version of Ripper */
13209 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
13210 rb_define_alloc_func(Ripper, ripper_s_allocate);
13211 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
13212 rb_define_method(Ripper, "parse", ripper_parse, 0);
13213 rb_define_method(Ripper, "column", ripper_column, 0);
13214 rb_define_method(Ripper, "filename", ripper_filename, 0);
13215 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
13216 rb_define_method(Ripper, "state", ripper_state, 0);
13217 rb_define_method(Ripper, "token", ripper_token, 0);
13218 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
13219 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
13220 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
13221 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
13222 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
13223 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
13224 rb_define_method(Ripper, "error?", ripper_error_p, 0);
13225#ifdef RIPPER_DEBUG
13226 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
13227 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
13228 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
13229#endif
13230
13231 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
13232 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
13233
13234 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
13235
13236 /* ignore newline, +/- is a sign. */
13237 rb_define_const(Ripper, "EXPR_BEG", INT2NUM(EXPR_BEG));
13238 /* newline significant, +/- is an operator. */
13239 rb_define_const(Ripper, "EXPR_END", INT2NUM(EXPR_END));
13240 /* ditto, and unbound braces. */
13241 rb_define_const(Ripper, "EXPR_ENDARG", INT2NUM(EXPR_ENDARG));
13242 /* ditto, and unbound braces. */
13243 rb_define_const(Ripper, "EXPR_ENDFN", INT2NUM(EXPR_ENDFN));
13244 /* newline significant, +/- is an operator. */
13245 rb_define_const(Ripper, "EXPR_ARG", INT2NUM(EXPR_ARG));
13246 /* newline significant, +/- is an operator. */
13247 rb_define_const(Ripper, "EXPR_CMDARG", INT2NUM(EXPR_CMDARG));
13248 /* newline significant, +/- is an operator. */
13249 rb_define_const(Ripper, "EXPR_MID", INT2NUM(EXPR_MID));
13250 /* ignore newline, no reserved words. */
13251 rb_define_const(Ripper, "EXPR_FNAME", INT2NUM(EXPR_FNAME));
13252 /* right after `.' or `::', no reserved words. */
13253 rb_define_const(Ripper, "EXPR_DOT", INT2NUM(EXPR_DOT));
13254 /* immediate after `class', no here document. */
13255 rb_define_const(Ripper, "EXPR_CLASS", INT2NUM(EXPR_CLASS));
13256 /* flag bit, label is allowed. */
13257 rb_define_const(Ripper, "EXPR_LABEL", INT2NUM(EXPR_LABEL));
13258 /* flag bit, just after a label. */
13259 rb_define_const(Ripper, "EXPR_LABELED", INT2NUM(EXPR_LABELED));
13260 /* symbol literal as FNAME. */
13261 rb_define_const(Ripper, "EXPR_FITEM", INT2NUM(EXPR_FITEM));
13262 /* equals to +EXPR_BEG+ */
13263 rb_define_const(Ripper, "EXPR_VALUE", INT2NUM(EXPR_VALUE));
13264 /* equals to <tt>(EXPR_BEG | EXPR_MID | EXPR_CLASS)</tt> */
13265 rb_define_const(Ripper, "EXPR_BEG_ANY", INT2NUM(EXPR_BEG_ANY));
13266 /* equals to <tt>(EXPR_ARG | EXPR_CMDARG)</tt> */
13267 rb_define_const(Ripper, "EXPR_ARG_ANY", INT2NUM(EXPR_ARG_ANY));
13268 /* equals to <tt>(EXPR_END | EXPR_ENDARG | EXPR_ENDFN)</tt> */
13269 rb_define_const(Ripper, "EXPR_END_ANY", INT2NUM(EXPR_END_ANY));
13270 /* equals to +0+ */
13271 rb_define_const(Ripper, "EXPR_NONE", INT2NUM(EXPR_NONE));
13272
13273 ripper_init_eventids1_table(Ripper);
13274 ripper_init_eventids2_table(Ripper);
13275
13276# if 0
13277 /* Hack to let RDoc document SCRIPT_LINES__ */
13278
13279 /*
13280 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
13281 * after the assignment will be added as an Array of lines with the file
13282 * name as the key.
13283 */
13284 rb_define_global_const("SCRIPT_LINES__", Qnil);
13285#endif
13286
13287}
13288#endif /* RIPPER */
13289
13290/*
13291 * Local variables:
13292 * mode: c
13293 * c-file-style: "ruby"
13294 * End:
13295 */