Ruby: ext/ripper/ripper.y Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

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/internal/config.h"
24
25#include <ctype.h>
26#include <errno.h>
27#include <stdio.h>
28
29struct lex_context;
30
31#include "internal.h"
32#include "internal/compile.h"
33#include "internal/compilers.h"
34#include "internal/complex.h"
35#include "internal/error.h"
36#include "internal/hash.h"
37#include "internal/imemo.h"
38#include "internal/io.h"
39#include "internal/numeric.h"
40#include "internal/parse.h"
41#include "internal/rational.h"
42#include "internal/re.h"
43#include "internal/symbol.h"
44#include "internal/thread.h"
45#include "internal/util.h"
46#include "internal/variable.h"
47#include "node.h"
48#include "probes.h"
49#include "regenc.h"
50#include "ruby/encoding.h"
51#include "ruby/regex.h"
52#include "ruby/ruby.h"
53#include "ruby/st.h"
54#include "ruby/util.h"
55#include "ruby/ractor.h"
56#include "symbol.h"
57
58enum shareability {
59 shareable_none,
60 shareable_literal,
61 shareable_copy,
62 shareable_everything,
63};
64
65struct lex_context {
66 unsigned int in_defined: 1;
67 unsigned int in_kwarg: 1;
68 unsigned int in_def: 1;
69 unsigned int in_class: 1;
70 BITFIELD(enum shareability, shareable_constant_value, 2);
71};
72
73#include "parse.h"
74
75#define NO_LEX_CTXT (struct lex_context){0}
76
77#define AREF(ary, i) RARRAY_AREF(ary, i)
78
79#ifndef WARN_PAST_SCOPE
80# define WARN_PAST_SCOPE 0
81#endif
82
83#define TAB_WIDTH 8
84
85#define yydebug (p->debug) /* disable the global variable definition */
86
87#define YYMALLOC(size) rb_parser_malloc(p, (size))
88#define YYREALLOC(ptr, size) rb_parser_realloc(p, (ptr), (size))
89#define YYCALLOC(nelem, size) rb_parser_calloc(p, (nelem), (size))
90#define YYFREE(ptr) rb_parser_free(p, (ptr))
91#define YYFPRINTF rb_parser_printf
92#define YY_LOCATION_PRINT(File, loc) \
93 rb_parser_printf(p, "%d.%d-%d.%d", \
94 (loc).beg_pos.lineno, (loc).beg_pos.column,\
95 (loc).end_pos.lineno, (loc).end_pos.column)
96#define YYLLOC_DEFAULT(Current, Rhs, N) \
97 do \
98 if (N) \
99 { \
100 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
101 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
102 } \
103 else \
104 { \
105 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
106 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
107 } \
108 while (0)
109
110#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
111 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
112#define RUBY_SET_YYLLOC_OF_NONE(Current) \
113 rb_parser_set_location_of_none(p, &(Current))
114#define RUBY_SET_YYLLOC(Current) \
115 rb_parser_set_location(p, &(Current))
116#define RUBY_INIT_YYLLOC() \
117 { \
118 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
119 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
120 }
121
122enum lex_state_bits {
123 EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
124 EXPR_END_bit, /* newline significant, +/- is an operator. */
125 EXPR_ENDARG_bit, /* ditto, and unbound braces. */
126 EXPR_ENDFN_bit, /* ditto, and unbound braces. */
127 EXPR_ARG_bit, /* newline significant, +/- is an operator. */
128 EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
129 EXPR_MID_bit, /* newline significant, +/- is an operator. */
130 EXPR_FNAME_bit, /* ignore newline, no reserved words. */
131 EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
132 EXPR_CLASS_bit, /* immediate after `class', no here document. */
133 EXPR_LABEL_bit, /* flag bit, label is allowed. */
134 EXPR_LABELED_bit, /* flag bit, just after a label. */
135 EXPR_FITEM_bit, /* symbol literal as FNAME. */
136 EXPR_MAX_STATE
137};
138/* examine combinations */
139enum lex_state_e {
140#define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
141 DEF_EXPR(BEG),
142 DEF_EXPR(END),
143 DEF_EXPR(ENDARG),
144 DEF_EXPR(ENDFN),
145 DEF_EXPR(ARG),
146 DEF_EXPR(CMDARG),
147 DEF_EXPR(MID),
148 DEF_EXPR(FNAME),
149 DEF_EXPR(DOT),
150 DEF_EXPR(CLASS),
151 DEF_EXPR(LABEL),
152 DEF_EXPR(LABELED),
153 DEF_EXPR(FITEM),
154 EXPR_VALUE = EXPR_BEG,
155 EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
156 EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
157 EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN),
158 EXPR_NONE = 0
159};
160#define IS_lex_state_for(x, ls) ((x) & (ls))
161#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
162#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
163#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
164
165# define SET_LEX_STATE(ls) \
166 (p->lex.state = \
167 (p->debug ? \
168 rb_parser_trace_lex_state(p, p->lex.state, (ls), __LINE__) : \
169 (enum lex_state_e)(ls)))
170
171typedef VALUE stack_type;
172
173static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
174
175# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
176# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
177# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
178# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
179# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
180
181/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
182 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
183#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
184#define COND_POP() BITSTACK_POP(cond_stack)
185#define COND_P() BITSTACK_SET_P(cond_stack)
186#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
187
188/* A flag to identify keyword_do_block; "do" keyword after command_call.
189 Example: `foo 1, 2 do`. */
190#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
191#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
192#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
193#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
194
195struct vtable {
196 ID *tbl;
197 int pos;
198 int capa;
199 struct vtable *prev;
200};
201
202struct local_vars {
203 struct vtable *args;
204 struct vtable *vars;
205 struct vtable *used;
206# if WARN_PAST_SCOPE
207 struct vtable *past;
208# endif
209 struct local_vars *prev;
210# ifndef RIPPER
211 struct {
212 NODE *outer, *inner, *current;
213 } numparam;
214# endif
215};
216
217enum {
218 ORDINAL_PARAM = -1,
219 NO_PARAM = 0,
220 NUMPARAM_MAX = 9,
221};
222
223#define NUMPARAM_ID_P(id) numparam_id_p(id)
224#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - tNUMPARAM_1 + 1)
225#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 + (idx) - 1))
226static int
227numparam_id_p(ID id)
228{
229 if (!is_local_id(id)) return 0;
230 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
231 return idx > 0 && idx <= NUMPARAM_MAX;
232}
233static void numparam_name(struct parser_params *p, ID id);
234
235#define DVARS_INHERIT ((void*)1)
236#define DVARS_TOPSCOPE NULL
237#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
238
239typedef struct token_info {
240 const char *token;
241 rb_code_position_t beg;
242 int indent;
243 int nonspc;
244 struct token_info *next;
245} token_info;
246
247typedef struct rb_strterm_struct rb_strterm_t;
248
249/*
250 Structure of Lexer Buffer:
251
252 lex.pbeg lex.ptok lex.pcur lex.pend
253 | | | |
254 |------------+------------+------------|
255 |<---------->|
256 token
257*/
258struct parser_params {
259 rb_imemo_tmpbuf_t *heap;
260
261 YYSTYPE *lval;
262
263 struct {
264 rb_strterm_t *strterm;
265 VALUE (*gets)(struct parser_params*,VALUE);
266 VALUE input;
267 VALUE prevline;
268 VALUE lastline;
269 VALUE nextline;
270 const char *pbeg;
271 const char *pcur;
272 const char *pend;
273 const char *ptok;
274 union {
275 long ptr;
276 VALUE (*call)(VALUE, int);
277 } gets_;
278 enum lex_state_e state;
279 /* track the nest level of any parens "()[]{}" */
280 int paren_nest;
281 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
282 int lpar_beg;
283 /* track the nest level of only braces "{}" */
284 int brace_nest;
285 } lex;
286 stack_type cond_stack;
287 stack_type cmdarg_stack;
288 int tokidx;
289 int toksiz;
290 int tokline;
291 int heredoc_end;
292 int heredoc_indent;
293 int heredoc_line_indent;
294 char *tokenbuf;
295 struct local_vars *lvtbl;
296 st_table *pvtbl;
297 st_table *pktbl;
298 int line_count;
299 int ruby_sourceline; /* current line no. */
300 const char *ruby_sourcefile; /* current source file */
301 VALUE ruby_sourcefile_string;
302 rb_encoding *enc;
303 token_info *token_info;
304 VALUE case_labels;
305 VALUE compile_option;
306
307 VALUE debug_buffer;
308 VALUE debug_output;
309
310 ID cur_arg;
311
312 rb_ast_t *ast;
313 int node_id;
314
315 int max_numparam;
316
317 struct lex_context ctxt;
318
319 unsigned int command_start:1;
320 unsigned int eofp: 1;
321 unsigned int ruby__end__seen: 1;
322 unsigned int debug: 1;
323 unsigned int has_shebang: 1;
324 unsigned int token_seen: 1;
325 unsigned int token_info_enabled: 1;
326# if WARN_PAST_SCOPE
327 unsigned int past_scope_enabled: 1;
328# endif
329 unsigned int error_p: 1;
330 unsigned int cr_seen: 1;
331
332#ifndef RIPPER
333 /* Ruby core only */
334
335 unsigned int do_print: 1;
336 unsigned int do_loop: 1;
337 unsigned int do_chomp: 1;
338 unsigned int do_split: 1;
339
340 NODE *eval_tree_begin;
341 NODE *eval_tree;
342 VALUE error_buffer;
343 VALUE debug_lines;
344 const struct rb_iseq_struct *parent_iseq;
345#else
346 /* Ripper only */
347
348 struct {
349 VALUE token;
350 int line;
351 int col;
352 } delayed;
353
354 VALUE value;
355 VALUE result;
356 VALUE parsing_thread;
357#endif
358};
359
360#define intern_cstr(n,l,en) rb_intern3(n,l,en)
361
362#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
363#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
364#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
365#define STR_NEW3(ptr,len,e,func) parser_str_new((ptr),(len),(e),(func),p->enc)
366#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
367
368static st_table *
369push_pvtbl(struct parser_params *p)
370{
371 st_table *tbl = p->pvtbl;
372 p->pvtbl = st_init_numtable();
373 return tbl;
374}
375
376static void
377pop_pvtbl(struct parser_params *p, st_table *tbl)
378{
379 st_free_table(p->pvtbl);
380 p->pvtbl = tbl;
381}
382
383static st_table *
384push_pktbl(struct parser_params *p)
385{
386 st_table *tbl = p->pktbl;
387 p->pktbl = 0;
388 return tbl;
389}
390
391static void
392pop_pktbl(struct parser_params *p, st_table *tbl)
393{
394 if (p->pktbl) st_free_table(p->pktbl);
395 p->pktbl = tbl;
396}
397
398static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
399#define yyerror0(msg) parser_yyerror(p, NULL, (msg))
400#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
401#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
402#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
403
404static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
405static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
406static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
407static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
408static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
409
410#ifdef RIPPER
411#define compile_for_eval (0)
412#else
413#define compile_for_eval (p->parent_iseq != 0)
414#endif
415
416#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
417
418#define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
419#define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
420#define NEW_QCALL(q,r,m,a,loc) NEW_NODE(NODE_CALL_Q(q),r,m,a,loc)
421
422#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
423
424static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
425
426#ifndef RIPPER
427static inline void
428rb_discard_node(struct parser_params *p, NODE *n)
429{
430 rb_ast_delete_node(p->ast, n);
431}
432#endif
433
434#ifdef RIPPER
435static inline VALUE
436add_mark_object(struct parser_params *p, VALUE obj)
437{
438 if (!SPECIAL_CONST_P(obj)
439 && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */
440 ) {
441 rb_ast_add_mark_object(p->ast, obj);
442 }
443 return obj;
444}
445#else
446static NODE* node_newnode_with_locals(struct parser_params *, enum node_type, VALUE, VALUE, const rb_code_location_t*);
447#endif
448
449static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE, const rb_code_location_t*);
450#define rb_node_newnode(type, a1, a2, a3, loc) node_newnode(p, (type), (a1), (a2), (a3), (loc))
451
452static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
453
454static int
455parser_get_node_id(struct parser_params *p)
456{
457 int node_id = p->node_id;
458 p->node_id++;
459 return node_id;
460}
461
462#ifndef RIPPER
463static inline void
464set_line_body(NODE *body, int line)
465{
466 if (!body) return;
467 switch (nd_type(body)) {
468 case NODE_RESCUE:
469 case NODE_ENSURE:
470 nd_set_line(body, line);
471 }
472}
473
474#define yyparse ruby_yyparse
475
476static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
477static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
478#define new_nil(loc) NEW_NIL(loc)
479static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
480static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
481static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
482static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
483
484static NODE *newline_node(NODE*);
485static void fixpos(NODE*,NODE*);
486
487static int value_expr_gen(struct parser_params*,NODE*);
488static void void_expr(struct parser_params*,NODE*);
489static NODE *remove_begin(NODE*);
490static NODE *remove_begin_all(NODE*);
491#define value_expr(node) value_expr_gen(p, (node))
492static NODE *void_stmts(struct parser_params*,NODE*);
493static void reduce_nodes(struct parser_params*,NODE**);
494static void block_dup_check(struct parser_params*,NODE*,NODE*);
495
496static NODE *block_append(struct parser_params*,NODE*,NODE*);
497static NODE *list_append(struct parser_params*,NODE*,NODE*);
498static NODE *list_concat(NODE*,NODE*);
499static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
500static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
501static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
502static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
503static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
504static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
505static NODE *evstr2dstr(struct parser_params*,NODE*);
506static NODE *splat_array(NODE*);
507static void mark_lvar_used(struct parser_params *p, NODE *rhs);
508
509static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
510static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
511static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
512static 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);
513static 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;}
514
515static bool args_info_empty_p(struct rb_args_info *args);
516static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
517static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
518static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
519static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
520static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
521static NODE *new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc);
522static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
523static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
524static void warn_one_line_pattern_matching(struct parser_params *p, NODE *node, NODE *pattern, bool right_assign);
525
526static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
527static NODE *args_with_numbered(struct parser_params*,NODE*,int);
528
529static VALUE negate_lit(struct parser_params*, VALUE);
530static NODE *ret_args(struct parser_params*,NODE*);
531static NODE *arg_blk_pass(NODE*,NODE*);
532static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
533static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
534
535static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
536static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
537
538static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
539static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
540
541static void rb_backref_error(struct parser_params*,NODE*);
542static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
543
544static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
545static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
546static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
547static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
548static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
549
550static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
551
552static NODE *opt_arg_append(NODE*, NODE*);
553static NODE *kwd_append(NODE*, NODE*);
554
555static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
556static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
557
558static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
559
560static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
561
562#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
563
564static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
565
566static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
567
568static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
569
570static ID *local_tbl(struct parser_params*);
571
572static VALUE reg_compile(struct parser_params*, VALUE, int);
573static void reg_fragment_setenc(struct parser_params*, VALUE, int);
574static int reg_fragment_check(struct parser_params*, VALUE, int);
575static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
576
577static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
578static NODE *heredoc_dedent(struct parser_params*,NODE*);
579
580static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
581
582#define get_id(id) (id)
583#define get_value(val) (val)
584#define get_num(num) (num)
585#else /* RIPPER */
586#define NODE_RIPPER NODE_CDECL
587#define NEW_RIPPER(a,b,c,loc) (VALUE)NEW_CDECL(a,b,c,loc)
588
589static inline int ripper_is_node_yylval(VALUE n);
590
591static inline VALUE
592ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
593{
594 if (ripper_is_node_yylval(c)) c = RNODE(c)->nd_cval;
595 add_mark_object(p, b);
596 add_mark_object(p, c);
597 return NEW_RIPPER(a, b, c, &NULL_LOC);
598}
599
600static inline int
601ripper_is_node_yylval(VALUE n)
602{
603 return RB_TYPE_P(n, T_NODE) && nd_type(RNODE(n)) == NODE_RIPPER;
604}
605
606#define value_expr(node) ((void)(node))
607#define remove_begin(node) (node)
608#define void_stmts(p,x) (x)
609#define rb_dvar_defined(id, base) 0
610#define rb_local_defined(id, base) 0
611static ID ripper_get_id(VALUE);
612#define get_id(id) ripper_get_id(id)
613static VALUE ripper_get_value(VALUE);
614#define get_value(val) ripper_get_value(val)
615#define get_num(num) (int)get_id(num)
616static VALUE assignable(struct parser_params*,VALUE);
617static int id_is_var(struct parser_params *p, ID id);
618
619#define method_cond(p,node,loc) (node)
620#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
621#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
622#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
623#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
624
625#define new_nil(loc) Qnil
626
627static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
628
629static VALUE const_decl(struct parser_params *p, VALUE path);
630
631static VALUE var_field(struct parser_params *p, VALUE a);
632static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
633
634static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
635
636static VALUE backref_error(struct parser_params*, NODE *, VALUE);
637#endif /* !RIPPER */
638
639/* forward declaration */
640typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
641
642RUBY_SYMBOL_EXPORT_BEGIN
643VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
644int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
645enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
646VALUE rb_parser_lex_state_name(enum lex_state_e state);
647void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
648PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
649YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
650YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
651YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
652RUBY_SYMBOL_EXPORT_END
653
654static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
655static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
656#ifndef RIPPER
657static ID formal_argument(struct parser_params*, ID);
658#else
659static ID formal_argument(struct parser_params*, VALUE);
660#endif
661static ID shadowing_lvar(struct parser_params*,ID);
662static void new_bv(struct parser_params*,ID);
663
664static void local_push(struct parser_params*,int);
665static void local_pop(struct parser_params*);
666static void local_var(struct parser_params*, ID);
667static void arg_var(struct parser_params*, ID);
668static int local_id(struct parser_params *p, ID id);
669static int local_id_ref(struct parser_params*, ID, ID **);
670#ifndef RIPPER
671static ID internal_id(struct parser_params*);
672static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
673static NODE *new_args_forward_def(struct parser_params*, NODE*, const YYLTYPE*);
674#endif
675static int check_forwarding_args(struct parser_params*);
676static void add_forwarding_args(struct parser_params *p);
677
678static const struct vtable *dyna_push(struct parser_params *);
679static void dyna_pop(struct parser_params*, const struct vtable *);
680static int dyna_in_block(struct parser_params*);
681#define dyna_var(p, id) local_var(p, id)
682static int dvar_defined(struct parser_params*, ID);
683static int dvar_defined_ref(struct parser_params*, ID, ID**);
684static int dvar_curr(struct parser_params*,ID);
685
686static int lvar_defined(struct parser_params*, ID);
687
688static NODE *numparam_push(struct parser_params *p);
689static void numparam_pop(struct parser_params *p, NODE *prev_inner);
690
691#ifdef RIPPER
692# define METHOD_NOT idNOT
693#else
694# define METHOD_NOT '!'
695#endif
696
697#define idFWD_REST '*'
698#ifdef RUBY3_KEYWORDS
699#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
700#else
701#define idFWD_KWREST 0
702#endif
703#define idFWD_BLOCK '&'
704
705#define RE_OPTION_ONCE (1<<16)
706#define RE_OPTION_ENCODING_SHIFT 8
707#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
708#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
709#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
710#define RE_OPTION_MASK 0xff
711#define RE_OPTION_ARG_ENCODING_NONE 32
712
713/* structs for managing terminator of string literal and heredocment */
714typedef struct rb_strterm_literal_struct {
715 union {
716 VALUE dummy;
717 long nest;
718 } u0;
719 union {
720 VALUE dummy;
721 long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
722 } u1;
723 union {
724 VALUE dummy;
725 long paren; /* '(' of `%q(...)` */
726 } u2;
727 union {
728 VALUE dummy;
729 long term; /* ')' of `%q(...)` */
730 } u3;
731} rb_strterm_literal_t;
732
733#define HERETERM_LENGTH_BITS ((SIZEOF_VALUE - 1) * CHAR_BIT - 1)
734
735struct rb_strterm_heredoc_struct {
736 VALUE lastline; /* the string of line that contains `<<"END"` */
737 long offset; /* the column of END in `<<"END"` */
738 int sourceline; /* lineno of the line that contains `<<"END"` */
739 unsigned length /* the length of END in `<<"END"` */
740#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
741 : HERETERM_LENGTH_BITS
742# define HERETERM_LENGTH_MAX ((1U << HERETERM_LENGTH_BITS) - 1)
743#else
744# define HERETERM_LENGTH_MAX UINT_MAX
745#endif
746 ;
747#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
748 unsigned quote: 1;
749 unsigned func: 8;
750#else
751 uint8_t quote;
752 uint8_t func;
753#endif
754};
755STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
756
757#define STRTERM_HEREDOC IMEMO_FL_USER0
758
759struct rb_strterm_struct {
760 VALUE flags;
761 union {
762 rb_strterm_literal_t literal;
763 rb_strterm_heredoc_t heredoc;
764 } u;
765};
766
767#ifndef RIPPER
768void
769rb_strterm_mark(VALUE obj)
770{
771 rb_strterm_t *strterm = (rb_strterm_t*)obj;
772 if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
773 rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
774 rb_gc_mark(heredoc->lastline);
775 }
776}
777#endif
778
779#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
780size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
781
782#define TOKEN2ID(tok) ( \
783 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
784 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
785 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
786 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
787 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
788 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
789 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
790
791/****** Ripper *******/
792
793#ifdef RIPPER
794#define RIPPER_VERSION "0.1.0"
795
796static inline VALUE intern_sym(const char *name);
797
798#include "eventids1.c"
799#include "eventids2.c"
800
801static VALUE ripper_dispatch0(struct parser_params*,ID);
802static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
803static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
804static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
805static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
806static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
807static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
808static void ripper_error(struct parser_params *p);
809
810#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
811#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
812#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
813#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
814#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
815#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
816#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))
817
818#define yyparse ripper_yyparse
819
820#define ID2VAL(id) STATIC_ID2SYM(id)
821#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
822#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
823
824#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
825 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
826
827#define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
828
829static inline VALUE
830new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
831{
832 NODE *t = (NODE *)tail;
833 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value, block = t->u3.value;
834 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, escape_Qundef(block));
835}
836
837static inline VALUE
838new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
839{
840 NODE *t = rb_node_newnode(NODE_ARGS_AUX, kw_args, kw_rest_arg, block, &NULL_LOC);
841 add_mark_object(p, kw_args);
842 add_mark_object(p, kw_rest_arg);
843 add_mark_object(p, block);
844 return (VALUE)t;
845}
846
847static inline VALUE
848args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
849{
850 return args;
851}
852
853static VALUE
854new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
855{
856 NODE *t = (NODE *)aryptn;
857 VALUE pre_args = t->u1.value, rest_arg = t->u2.value, post_args = t->u3.value;
858
859 if (!NIL_P(pre_arg)) {
860 if (!NIL_P(pre_args)) {
861 rb_ary_unshift(pre_args, pre_arg);
862 }
863 else {
864 pre_args = rb_ary_new_from_args(1, pre_arg);
865 }
866 }
867 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
868}
869
870static VALUE
871new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
872{
873 NODE *t;
874
875 if (has_rest) {
876 rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
877 }
878 else {
879 rest_arg = Qnil;
880 }
881
882 t = rb_node_newnode(NODE_ARYPTN, pre_args, rest_arg, post_args, &NULL_LOC);
883 add_mark_object(p, pre_args);
884 add_mark_object(p, rest_arg);
885 add_mark_object(p, post_args);
886 return (VALUE)t;
887}
888
889static VALUE
890new_find_pattern(struct parser_params *p, VALUE constant, VALUE fndptn, const YYLTYPE *loc)
891{
892 NODE *t = (NODE *)fndptn;
893 VALUE pre_rest_arg = t->u1.value, args = t->u2.value, post_rest_arg = t->u3.value;
894
895 return dispatch4(fndptn, constant, pre_rest_arg, args, post_rest_arg);
896}
897
898static VALUE
899new_find_pattern_tail(struct parser_params *p, VALUE pre_rest_arg, VALUE args, VALUE post_rest_arg, const YYLTYPE *loc)
900{
901 NODE *t;
902
903 pre_rest_arg = dispatch1(var_field, pre_rest_arg ? pre_rest_arg : Qnil);
904 post_rest_arg = dispatch1(var_field, post_rest_arg ? post_rest_arg : Qnil);
905
906 t = rb_node_newnode(NODE_FNDPTN, pre_rest_arg, args, post_rest_arg, &NULL_LOC);
907 add_mark_object(p, pre_rest_arg);
908 add_mark_object(p, args);
909 add_mark_object(p, post_rest_arg);
910 return (VALUE)t;
911}
912
913#define new_hash(p,h,l) rb_ary_new_from_args(0)
914
915static VALUE
916new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
917{
918 return ary;
919}
920
921static VALUE
922new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
923{
924 NODE *t = (NODE *)hshptn;
925 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value;
926 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
927}
928
929static VALUE
930new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
931{
932 NODE *t;
933 if (kw_rest_arg) {
934 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
935 }
936 else {
937 kw_rest_arg = Qnil;
938 }
939 t = rb_node_newnode(NODE_HSHPTN, kw_args, kw_rest_arg, 0, &NULL_LOC);
940
941 add_mark_object(p, kw_args);
942 add_mark_object(p, kw_rest_arg);
943 return (VALUE)t;
944}
945
946#define new_defined(p,expr,loc) dispatch1(defined, (expr))
947
948static VALUE heredoc_dedent(struct parser_params*,VALUE);
949
950#else
951#define ID2VAL(id) (id)
952#define TOKEN2VAL(t) ID2VAL(t)
953#define KWD2EID(t, v) keyword_##t
954
955static NODE *
956set_defun_body(struct parser_params *p, NODE *n, NODE *args, NODE *body, const YYLTYPE *loc)
957{
958 body = remove_begin(body);
959 reduce_nodes(p, &body);
960 n->nd_defn = NEW_SCOPE(args, body, loc);
961 n->nd_loc = *loc;
962 nd_set_line(n->nd_defn, loc->end_pos.lineno);
963 set_line_body(body, loc->beg_pos.lineno);
964 return n;
965}
966
967static NODE *
968rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
969 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
970{
971 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
972 rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc);
973 loc.beg_pos = arg_loc->beg_pos;
974 return NEW_RESCUE(arg, rescue, 0, &loc);
975}
976
977#endif /* RIPPER */
978
979static void
980restore_defun(struct parser_params *p, NODE *name)
981{
982 YYSTYPE c = {.val = name->nd_cval};
983 p->cur_arg = name->nd_vid;
984 p->ctxt.in_def = c.ctxt.in_def;
985 p->ctxt.shareable_constant_value = c.ctxt.shareable_constant_value;
986}
987
988static void
989endless_method_name(struct parser_params *p, NODE *defn, const YYLTYPE *loc)
990{
991#ifdef RIPPER
992 defn = defn->nd_defn;
993#endif
994 ID mid = defn->nd_mid;
995 if (is_attrset_id(mid)) {
996 yyerror1(loc, "setter method cannot be defined in an endless method definition");
997 }
998 token_info_drop(p, "def", loc->beg_pos);
999}
1000
1001#ifndef RIPPER
1002# define Qnone 0
1003# define Qnull 0
1004# define ifndef_ripper(x) (x)
1005#else
1006# define Qnone Qnil
1007# define Qnull Qundef
1008# define ifndef_ripper(x)
1009#endif
1010
1011# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1012# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1013# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1014# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1015# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1016# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1017# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1018# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1019# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1020# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1021# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1022# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1023# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1024# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1025# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1026# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1027# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1028# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1029# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1030# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1031#ifdef RIPPER
1032static ID id_warn, id_warning, id_gets, id_assoc;
1033# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1034# define WARN_S_L(s,l) STR_NEW(s,l)
1035# define WARN_S(s) STR_NEW2(s)
1036# define WARN_I(i) INT2NUM(i)
1037# define WARN_ID(i) rb_id2str(i)
1038# define WARN_IVAL(i) i
1039# define PRIsWARN "s"
1040# define rb_warn0L_experimental(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1041# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1042# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1043# ifdef HAVE_VA_ARGS_MACRO
1044# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1045# else
1046# define WARN_CALL rb_funcall
1047# endif
1048# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1049# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1050# ifdef HAVE_VA_ARGS_MACRO
1051# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1052# else
1053# define WARNING_CALL rb_funcall
1054# endif
1055PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1056# define compile_error ripper_compile_error
1057#else
1058# define WARN_S_L(s,l) s
1059# define WARN_S(s) s
1060# define WARN_I(i) i
1061# define WARN_ID(i) rb_id2name(i)
1062# define WARN_IVAL(i) NUM2INT(i)
1063# define PRIsWARN PRIsVALUE
1064# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1065# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1066# define WARN_CALL rb_compile_warn
1067# define rb_warn0L_experimental(l,fmt) rb_category_compile_warn(RB_WARN_CATEGORY_EXPERIMENTAL, WARN_ARGS_L(l, fmt, 1))
1068# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1069# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1070# define WARNING_CALL rb_compile_warning
1071PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1072# define compile_error parser_compile_error
1073#endif
1074
1075#define WARN_EOL(tok) \
1076 (looking_at_eol_p(p) ? \
1077 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
1078 (void)0)
1079static int looking_at_eol_p(struct parser_params *p);
1080%}
1081
1082%expect 0
1083%define api.pure
1084%define parse.error verbose
1085%printer {
1086#ifndef RIPPER
1087 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
1088#else
1089 rb_parser_printf(p, "%"PRIsVALUE, RNODE($$)->nd_rval);
1090#endif
1091} tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN
1092%printer {
1093#ifndef RIPPER
1094 rb_parser_printf(p, "%+"PRIsVALUE, $$->nd_lit);
1095#else
1096 rb_parser_printf(p, "%+"PRIsVALUE, get_value($$));
1097#endif
1098} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
1099%printer {
1100#ifndef RIPPER
1101 rb_parser_printf(p, "$%ld", $$->nd_nth);
1102#else
1103 rb_parser_printf(p, "%"PRIsVALUE, $$);
1104#endif
1105} tNTH_REF
1106%printer {
1107#ifndef RIPPER
1108 rb_parser_printf(p, "$%c", (int)$$->nd_nth);
1109#else
1110 rb_parser_printf(p, "%"PRIsVALUE, $$);
1111#endif
1112} tBACK_REF
1113
1114%lex-param {struct parser_params *p}
1115%parse-param {struct parser_params *p}
1116%initial-action
1117{
1118 RUBY_SET_YYLLOC_OF_NONE(@$);
1119};
1120
1121%union {
1122 VALUE val;
1123 NODE *node;
1124 ID id;
1125 int num;
1126 st_table *tbl;
1127 const struct vtable *vars;
1128 struct rb_strterm_struct *strterm;
1129 struct lex_context ctxt;
1130}
1131
1132%token <val>
1133 keyword_class "`class'"
1134 keyword_module "`module'"
1135 keyword_def "`def'"
1136 keyword_undef "`undef'"
1137 keyword_begin "`begin'"
1138 keyword_rescue "`rescue'"
1139 keyword_ensure "`ensure'"
1140 keyword_end "`end'"
1141 keyword_if "`if'"
1142 keyword_unless "`unless'"
1143 keyword_then "`then'"
1144 keyword_elsif "`elsif'"
1145 keyword_else "`else'"
1146 keyword_case "`case'"
1147 keyword_when "`when'"
1148 keyword_while "`while'"
1149 keyword_until "`until'"
1150 keyword_for "`for'"
1151 keyword_break "`break'"
1152 keyword_next "`next'"
1153 keyword_redo "`redo'"
1154 keyword_retry "`retry'"
1155 keyword_in "`in'"
1156 keyword_do "`do'"
1157 keyword_do_cond "`do' for condition"
1158 keyword_do_block "`do' for block"
1159 keyword_do_LAMBDA "`do' for lambda"
1160 keyword_return "`return'"
1161 keyword_yield "`yield'"
1162 keyword_super "`super'"
1163 keyword_self "`self'"
1164 keyword_nil "`nil'"
1165 keyword_true "`true'"
1166 keyword_false "`false'"
1167 keyword_and "`and'"
1168 keyword_or "`or'"
1169 keyword_not "`not'"
1170 modifier_if "`if' modifier"
1171 modifier_unless "`unless' modifier"
1172 modifier_while "`while' modifier"
1173 modifier_until "`until' modifier"
1174 modifier_rescue "`rescue' modifier"
1175 keyword_alias "`alias'"
1176 keyword_defined "`defined?'"
1177 keyword_BEGIN "`BEGIN'"
1178 keyword_END "`END'"
1179 keyword__LINE__ "`__LINE__'"
1180 keyword__FILE__ "`__FILE__'"
1181 keyword__ENCODING__ "`__ENCODING__'"
1182
1183%token <val> tIDENTIFIER "local variable or method"
1184%token <val> tFID "method"
1185%token <val> tGVAR "global variable"
1186%token <val> tIVAR "instance variable"
1187%token <val> tCONSTANT "constant"
1188%token <val> tCVAR "class variable"
1189%token <val> tLABEL "label"
1190%token <val> tINTEGER "integer literal"
1191%token <val> tFLOAT "float literal"
1192%token <val> tRATIONAL "rational literal"
1193%token <val> tIMAGINARY "imaginary literal"
1194%token <val> tCHAR "char literal"
1195%token <val> tNTH_REF "numbered reference"
1196%token <val> tBACK_REF "back reference"
1197%token <val> tSTRING_CONTENT "literal content"
1198%token <val> tREGEXP_END
1199
1200%type <val> singleton strings string string1 xstring regexp
1201%type <val> string_contents xstring_contents regexp_contents string_content
1202%type <val> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1203%type <val> literal numeric simple_numeric ssym dsym symbol cpath def_name defn_head defs_head
1204%type <val> top_compstmt top_stmts top_stmt begin_block
1205%type <val> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1206%type <val> expr_value expr_value_do arg_value primary_value fcall rel_expr
1207%type <val> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1208%type <val> args call_args opt_call_args
1209%type <val> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1210%type <val> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1211%type <val> command_rhs arg_rhs
1212%type <val> command_asgn mrhs mrhs_arg superclass block_call block_command
1213%type <val> f_block_optarg f_block_opt
1214%type <val> f_arglist f_opt_paren_args f_paren_args f_args f_arg f_arg_item
1215%type <val> f_optarg f_marg f_marg_list f_margs f_rest_marg
1216%type <val> assoc_list assocs assoc undef_list backref string_dvar for_var
1217%type <val> block_param opt_block_param block_param_def f_opt
1218%type <val> f_kwarg f_kw f_block_kwarg f_block_kw
1219%type <val> bv_decls opt_bv_decl bvar
1220%type <val> lambda f_larglist lambda_body brace_body do_body
1221%type <val> brace_block cmd_brace_block do_block lhs none fitem
1222%type <val> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1223%type <val> p_case_body p_cases p_top_expr p_top_expr_body
1224%type <val> p_expr p_as p_alt p_expr_basic p_find
1225%type <val> p_args p_args_head p_args_tail p_args_post p_arg
1226%type <val> p_value p_primitive p_variable p_var_ref p_const
1227%type <val> p_kwargs p_kwarg p_kw
1228%type <val> keyword_variable user_variable sym operation operation2 operation3
1229%type <val> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1230%type <val> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1231%type <val> p_rest p_kwrest p_kwnorest p_any_kwrest p_kw_label
1232%type <val> f_no_kwarg f_any_kwrest args_forward excessed_comma
1233 %type <ctxt> lex_ctxt /* keep <ctxt> in ripper */
1234%token END_OF_INPUT 0 "end-of-input"
1235%token <val> '.'
1236/* escaped chars, should be ignored otherwise */
1237%token <val> '\\' "backslash"
1238%token tSP "escaped space"
1239%token <val> '\t' "escaped horizontal tab"
1240%token <val> '\f' "escaped form feed"
1241%token <val> '\r' "escaped carriage return"
1242%token <val> '\13' "escaped vertical tab"
1243%token tUPLUS 132 "unary+"
1244%token tUMINUS 133 "unary-"
1245%token tPOW 134 "**"
1246%token tCMP 135 "<=>"
1247%token tEQ 140 "=="
1248%token tEQQ 141 "==="
1249%token tNEQ 142 "!="
1250%token tGEQ 139 ">="
1251%token tLEQ 138 "<="
1252%token tANDOP 148 "&&"
1253%token tOROP 149 "||"
1254%token tMATCH 143 "=~"
1255%token tNMATCH 144 "!~"
1256%token tDOT2 128 ".."
1257%token tDOT3 129 "..."
1258%token tBDOT2 130 "(.."
1259%token tBDOT3 131 "(..."
1260%token tAREF 145 "[]"
1261%token tASET 146 "[]="
1262%token tLSHFT 136 "<<"
1263%token tRSHFT 137 ">>"
1264%token <val> tANDDOT 150 "&."
1265%token <val> tCOLON2 147 "::"
1266%token tCOLON3 ":: at EXPR_BEG"
1267%token <val> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1268%token tASSOC "=>"
1269%token tLPAREN "("
1270%token tLPAREN_ARG "( arg"
1271%token tRPAREN ")"
1272%token tLBRACK "["
1273%token tLBRACE "{"
1274%token tLBRACE_ARG "{ arg"
1275%token tSTAR "*"
1276%token tDSTAR "**arg"
1277%token tAMPER "&"
1278%token tLAMBDA "->"
1279%token tSYMBEG "symbol literal"
1280%token tSTRING_BEG "string literal"
1281%token tXSTRING_BEG "backtick literal"
1282%token tREGEXP_BEG "regexp literal"
1283%token tWORDS_BEG "word list"
1284%token tQWORDS_BEG "verbatim word list"
1285%token tSYMBOLS_BEG "symbol list"
1286%token tQSYMBOLS_BEG "verbatim symbol list"
1287%token tSTRING_END "terminator"
1288%token tSTRING_DEND "'}'"
1289%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1290
1291/*
1292 * precedence table
1293 */
1294
1295%nonassoc tLOWEST
1296%nonassoc tLBRACE_ARG
1297
1298%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1299%left keyword_or keyword_and
1300%right keyword_not
1301%nonassoc keyword_defined
1302%right '=' tOP_ASGN
1303%left modifier_rescue
1304%right '?' ':'
1305%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1306%left tOROP
1307%left tANDOP
1308%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1309%left '>' tGEQ '<' tLEQ
1310%left '|' '^'
1311%left '&'
1312%left tLSHFT tRSHFT
1313%left '+' '-'
1314%left '*' '/' '%'
1315%right tUMINUS_NUM tUMINUS
1316%right tPOW
1317%right '!' '~' tUPLUS
1318
1319%token tLAST_TOKEN
1320
1321%%
1322program : {
1323 SET_LEX_STATE(EXPR_BEG);
1324 local_push(p, ifndef_ripper(1)+0);
1325 }
1326 top_compstmt
1327 {
1328#if 0
1329 if ($2 && !compile_for_eval) {
1330 NODE *node = $2;
1331 /* last expression should not be void */
1332 if (nd_type(node) == NODE_BLOCK) {
1333 while (node->nd_next) {
1334 node = node->nd_next;
1335 }
1336 node = node->nd_head;
1337 }
1338 node = remove_begin(node);
1339 void_expr(p, node);
1340 }
1341 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1342#endif
1343 {VALUE v1,v2;v1=$2;v2=dispatch1(program,v1);p->result=v2;}
1344 local_pop(p);
1345 }
1346 ;
1347
1348top_compstmt : top_stmts opt_terms
1349 {
1350 $$ = void_stmts(p, $1);
1351 }
1352 ;
1353
1354top_stmts : none
1355 {
1356#if 0
1357 $$ = NEW_BEGIN(0, &@$);
1358#endif
1359 {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;}
1360 }
1361 | top_stmt
1362 {
1363#if 0
1364 $$ = newline_node($1);
1365#endif
1366 {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1367 }
1368 | top_stmts terms top_stmt
1369 {
1370#if 0
1371 $$ = block_append(p, $1, newline_node($3));
1372#endif
1373 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1374 }
1375 | error top_stmt
1376 {
1377 $$ = remove_begin($2);
1378 }
1379 ;
1380
1381top_stmt : stmt
1382 | keyword_BEGIN begin_block
1383 {
1384 $$ = $2;
1385 }
1386 ;
1387
1388begin_block : '{' top_compstmt '}'
1389 {
1390#if 0
1391 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1392 NEW_BEGIN($2, &@$));
1393 $$ = NEW_BEGIN(0, &@$);
1394#endif
1395 {VALUE v1,v2;v1=$2;v2=dispatch1(BEGIN,v1);$$=v2;}
1396 }
1397 ;
1398
1399bodystmt : compstmt
1400 opt_rescue
1401 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1402 compstmt
1403 opt_ensure
1404 {
1405#if 0
1406 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1407#endif
1408 {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;}
1409 }
1410 | compstmt
1411 opt_rescue
1412 opt_ensure
1413 {
1414#if 0
1415 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1416#endif
1417 {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;}
1418 }
1419 ;
1420
1421compstmt : stmts opt_terms
1422 {
1423 $$ = void_stmts(p, $1);
1424 }
1425 ;
1426
1427stmts : none
1428 {
1429#if 0
1430 $$ = NEW_BEGIN(0, &@$);
1431#endif
1432 {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;}
1433 }
1434 | stmt_or_begin
1435 {
1436#if 0
1437 $$ = newline_node($1);
1438#endif
1439 {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1440 }
1441 | stmts terms stmt_or_begin
1442 {
1443#if 0
1444 $$ = block_append(p, $1, newline_node($3));
1445#endif
1446 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1447 }
1448 | error stmt
1449 {
1450 $$ = remove_begin($2);
1451 }
1452 ;
1453
1454stmt_or_begin : stmt
1455 {
1456 $$ = $1;
1457 }
1458 | keyword_BEGIN
1459 {
1460 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1461 }
1462 begin_block
1463 {
1464 $$ = $3;
1465 }
1466 ;
1467
1468stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1469 {
1470#if 0
1471 $$ = NEW_ALIAS($2, $4, &@$);
1472#endif
1473 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(alias,v1,v2);$$=v3;}
1474 }
1475 | keyword_alias tGVAR tGVAR
1476 {
1477#if 0
1478 $$ = NEW_VALIAS($2, $3, &@$);
1479#endif
1480 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1481 }
1482 | keyword_alias tGVAR tBACK_REF
1483 {
1484#if 0
1485 char buf[2];
1486 buf[0] = '$';
1487 buf[1] = (char)$3->nd_nth;
1488 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1489#endif
1490 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1491 }
1492 | keyword_alias tGVAR tNTH_REF
1493 {
1494 static const char mesg[] = "can't make alias for the number variables";
1495#if 0
1496 yyerror1(&@3, mesg);
1497 $$ = NEW_BEGIN(0, &@$);
1498#endif
1499 {VALUE v1,v2,v3;v1=ERR_MESG();v2=$3;v3=dispatch2(alias_error,v1,v2);$$=v3;}ripper_error(p);
1500 }
1501 | keyword_undef undef_list
1502 {
1503#if 0
1504 $$ = $2;
1505#endif
1506 {VALUE v1,v2;v1=$2;v2=dispatch1(undef,v1);$$=v2;}
1507 }
1508 | stmt modifier_if expr_value
1509 {
1510#if 0
1511 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1512 fixpos($$, $3);
1513#endif
1514 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
1515 }
1516 | stmt modifier_unless expr_value
1517 {
1518#if 0
1519 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1520 fixpos($$, $3);
1521#endif
1522 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
1523 }
1524 | stmt modifier_while expr_value
1525 {
1526#if 0
1527 if ($1 && nd_type($1) == NODE_BEGIN) {
1528 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1529 }
1530 else {
1531 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1532 }
1533#endif
1534 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(while_mod,v1,v2);$$=v3;}
1535 }
1536 | stmt modifier_until expr_value
1537 {
1538#if 0
1539 if ($1 && nd_type($1) == NODE_BEGIN) {
1540 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1541 }
1542 else {
1543 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1544 }
1545#endif
1546 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(until_mod,v1,v2);$$=v3;}
1547 }
1548 | stmt modifier_rescue stmt
1549 {
1550#if 0
1551 NODE *resq;
1552 YYLTYPE loc = code_loc_gen(&@2, &@3);
1553 resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1554 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1555#endif
1556 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1557 }
1558 | keyword_END '{' compstmt '}'
1559 {
1560 if (p->ctxt.in_def) {
1561 rb_warn0("END in method; use at_exit");
1562 }
1563#if 0
1564 {
1565 NODE *scope = NEW_NODE(
1566 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1567 $$ = NEW_POSTEXE(scope, &@$);
1568 }
1569#endif
1570 {VALUE v1,v2;v1=$3;v2=dispatch1(END,v1);$$=v2;}
1571 }
1572 | command_asgn
1573 | mlhs '=' lex_ctxt command_call
1574 {
1575#if 0
1576 value_expr($4);
1577 $$ = node_assign(p, $1, $4, $3, &@$);
1578#endif
1579 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(massign,v1,v2);$$=v3;}
1580 }
1581 | lhs '=' lex_ctxt mrhs
1582 {
1583#if 0
1584 value_expr($4);
1585 $$ = node_assign(p, $1, $4, $3, &@$);
1586#endif
1587 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(assign,v1,v2);$$=v3;}
1588 }
1589 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue stmt
1590 {
1591#if 0
1592 YYLTYPE loc = code_loc_gen(&@5, &@6);
1593 value_expr($4);
1594 $$ = node_assign(p, $1, NEW_RESCUE($4, NEW_RESBODY(0, remove_begin($6), 0, &loc), 0, &@$), $3, &@$);
1595#endif
1596 {VALUE v1,v2,v3,v4,v5,v6;v1=$4;v2=$6;v3=dispatch2(rescue_mod,v1,v2);v4=$1;v5=v3;v6=dispatch2(massign,v4,v5);$$=v6;}
1597 }
1598 | mlhs '=' lex_ctxt mrhs_arg
1599 {
1600#if 0
1601 $$ = node_assign(p, $1, $4, $3, &@$);
1602#endif
1603 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(massign,v1,v2);$$=v3;}
1604 }
1605 | expr
1606 ;
1607
1608command_asgn : lhs '=' lex_ctxt command_rhs
1609 {
1610#if 0
1611 $$ = node_assign(p, $1, $4, $3, &@$);
1612#endif
1613 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(assign,v1,v2);$$=v3;}
1614 }
1615 | var_lhs tOP_ASGN lex_ctxt command_rhs
1616 {
1617#if 0
1618 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
1619#endif
1620 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$4;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
1621 }
1622 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
1623 {
1624#if 0
1625 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
1626#endif
1627 {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=$7;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
1628
1629 }
1630 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1631 {
1632#if 0
1633 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1634#endif
1635 {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=$6;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1636 }
1637 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt command_rhs
1638 {
1639#if 0
1640 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1641#endif
1642 {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=$6;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1643 }
1644 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
1645 {
1646#if 0
1647 YYLTYPE loc = code_loc_gen(&@1, &@3);
1648 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
1649#endif
1650 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);v4=v3;v5=$4;v6=$6;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
1651 }
1652 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1653 {
1654#if 0
1655 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
1656#endif
1657 {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=$6;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1658 }
1659 | backref tOP_ASGN lex_ctxt command_rhs
1660 {
1661#if 0
1662 rb_backref_error(p, $1);
1663 $$ = NEW_BEGIN(0, &@$);
1664#endif
1665 {VALUE v1,v2,v3;v1=var_field(p, $1);v2=$4;v3=dispatch2(assign,v1,v2);$$=backref_error(p, RNODE($1), v3);}ripper_error(p);
1666 }
1667 ;
1668
1669command_rhs : command_call %prec tOP_ASGN
1670 {
1671 value_expr($1);
1672 $$ = $1;
1673 }
1674 | command_call modifier_rescue stmt
1675 {
1676#if 0
1677 YYLTYPE loc = code_loc_gen(&@2, &@3);
1678 value_expr($1);
1679 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1680#endif
1681 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1682 }
1683 | command_asgn
1684 ;
1685
1686expr : command_call
1687 | expr keyword_and expr
1688 {
1689 $$ = logop(p, idAND, $1, $3, &@2, &@$);
1690 }
1691 | expr keyword_or expr
1692 {
1693 $$ = logop(p, idOR, $1, $3, &@2, &@$);
1694 }
1695 | keyword_not opt_nl expr
1696 {
1697 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
1698 }
1699 | '!' command_call
1700 {
1701 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
1702 }
1703 | arg tASSOC
1704 {
1705 value_expr($1);
1706 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1707 p->command_start = FALSE;
1708 $<ctxt>$ = p->ctxt;
1709 p->ctxt.in_kwarg = 1;
1710 }
1711 {$<tbl>$ = push_pvtbl(p);}
1712 p_expr
1713 {pop_pvtbl(p, $<tbl>4);}
1714 {
1715 p->ctxt.in_kwarg = $<ctxt>3.in_kwarg;
1716#if 0
1717 $$ = NEW_CASE3($1, NEW_IN($5, 0, 0, &@5), &@$);
1718 warn_one_line_pattern_matching(p, $$, $5, true);
1719#endif
1720 {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;}
1721 }
1722 | arg keyword_in
1723 {
1724 value_expr($1);
1725 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1726 p->command_start = FALSE;
1727 $<ctxt>$ = p->ctxt;
1728 p->ctxt.in_kwarg = 1;
1729 }
1730 {$<tbl>$ = push_pvtbl(p);}
1731 p_expr
1732 {pop_pvtbl(p, $<tbl>4);}
1733 {
1734 p->ctxt.in_kwarg = $<ctxt>3.in_kwarg;
1735#if 0
1736 $$ = NEW_CASE3($1, NEW_IN($5, NEW_TRUE(&@5), NEW_FALSE(&@5), &@5), &@$);
1737 warn_one_line_pattern_matching(p, $$, $5, false);
1738#endif
1739 {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;}
1740 }
1741 | arg %prec tLBRACE_ARG
1742 ;
1743
1744def_name : fname
1745 {
1746 ID fname = get_id($1);
1747 ID cur_arg = p->cur_arg;
1748 YYSTYPE c = {.ctxt = p->ctxt};
1749 numparam_name(p, fname);
1750 local_push(p, 0);
1751 p->cur_arg = 0;
1752 p->ctxt.in_def = 1;
1753 $<node>$ = NEW_NODE(NODE_SELF, /*vid*/cur_arg, /*mid*/fname, /*cval*/c.val, &@$);
1754#if 0
1755#endif
1756 $$ = NEW_RIPPER(fname, get_value($1), $$, &NULL_LOC);
1757
1758 }
1759 ;
1760
1761defn_head : k_def def_name
1762 {
1763 $$ = $2;
1764#if 0
1765 $$ = NEW_NODE(NODE_DEFN, 0, $$->nd_mid, $$, &@$);
1766#endif
1767 }
1768 ;
1769
1770defs_head : k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} def_name
1771 {
1772 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
1773 $$ = $5;
1774#if 0
1775 $$ = NEW_NODE(NODE_DEFS, $2, $$->nd_mid, $$, &@$);
1776#endif
1777 VALUE ary = rb_ary_new_from_args(3, $2, $3, get_value($$));
1778 add_mark_object(p, ary);
1779 $<node>$->nd_rval = ary;
1780
1781 }
1782 ;
1783
1784expr_value : expr
1785 {
1786 value_expr($1);
1787 $$ = $1;
1788 }
1789 ;
1790
1791expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1792 {
1793 $$ = $2;
1794 }
1795
1796
1797command_call : command
1798 | block_command
1799 ;
1800
1801block_command : block_call
1802 | block_call call_op2 operation2 command_args
1803 {
1804#if 0
1805 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
1806#endif
1807 {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;}
1808 }
1809 ;
1810
1811cmd_brace_block : tLBRACE_ARG brace_body '}'
1812 {
1813 $$ = $2;
1814#if 0
1815 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
1816 nd_set_line($$, @1.end_pos.lineno);
1817#endif
1818 }
1819 ;
1820
1821fcall : operation
1822 {
1823#if 0
1824 $$ = NEW_FCALL($1, 0, &@$);
1825 nd_set_line($$, p->tokline);
1826#endif
1827 $$=$1;
1828 }
1829 ;
1830
1831command : fcall command_args %prec tLOWEST
1832 {
1833#if 0
1834 $1->nd_args = $2;
1835 nd_set_last_loc($1, @2.end_pos);
1836 $$ = $1;
1837#endif
1838 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(command,v1,v2);$$=v3;}
1839 }
1840 | fcall command_args cmd_brace_block
1841 {
1842#if 0
1843 block_dup_check(p, $2, $3);
1844 $1->nd_args = $2;
1845 $$ = method_add_block(p, $1, $3, &@$);
1846 fixpos($$, $1);
1847 nd_set_last_loc($1, @2.end_pos);
1848#endif
1849 {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;}
1850 }
1851 | primary_value call_op operation2 command_args %prec tLOWEST
1852 {
1853#if 0
1854 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
1855#endif
1856 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1857 }
1858 | primary_value call_op operation2 command_args cmd_brace_block
1859 {
1860#if 0
1861 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
1862#endif
1863 {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;}
1864 }
1865 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1866 {
1867#if 0
1868 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
1869#endif
1870 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1871 }
1872 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1873 {
1874#if 0
1875 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
1876#endif
1877 {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;}
1878 }
1879 | keyword_super command_args
1880 {
1881#if 0
1882 $$ = NEW_SUPER($2, &@$);
1883 fixpos($$, $2);
1884#endif
1885 {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
1886 }
1887 | keyword_yield command_args
1888 {
1889#if 0
1890 $$ = new_yield(p, $2, &@$);
1891 fixpos($$, $2);
1892#endif
1893 {VALUE v1,v2;v1=$2;v2=dispatch1(yield,v1);$$=v2;}
1894 }
1895 | k_return call_args
1896 {
1897#if 0
1898 $$ = NEW_RETURN(ret_args(p, $2), &@$);
1899#endif
1900 {VALUE v1,v2;v1=$2;v2=dispatch1(return,v1);$$=v2;}
1901 }
1902 | keyword_break call_args
1903 {
1904#if 0
1905 $$ = NEW_BREAK(ret_args(p, $2), &@$);
1906#endif
1907 {VALUE v1,v2;v1=$2;v2=dispatch1(break,v1);$$=v2;}
1908 }
1909 | keyword_next call_args
1910 {
1911#if 0
1912 $$ = NEW_NEXT(ret_args(p, $2), &@$);
1913#endif
1914 {VALUE v1,v2;v1=$2;v2=dispatch1(next,v1);$$=v2;}
1915 }
1916 ;
1917
1918mlhs : mlhs_basic
1919 | tLPAREN mlhs_inner rparen
1920 {
1921#if 0
1922 $$ = $2;
1923#endif
1924 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1925 }
1926 ;
1927
1928mlhs_inner : mlhs_basic
1929 | tLPAREN mlhs_inner rparen
1930 {
1931#if 0
1932 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1933#endif
1934 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1935 }
1936 ;
1937
1938mlhs_basic : mlhs_head
1939 {
1940#if 0
1941 $$ = NEW_MASGN($1, 0, &@$);
1942#endif
1943 $$=$1;
1944 }
1945 | mlhs_head mlhs_item
1946 {
1947#if 0
1948 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
1949#endif
1950 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1951 }
1952 | mlhs_head tSTAR mlhs_node
1953 {
1954#if 0
1955 $$ = NEW_MASGN($1, $3, &@$);
1956#endif
1957 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1958 }
1959 | mlhs_head tSTAR mlhs_node ',' mlhs_post
1960 {
1961#if 0
1962 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
1963#endif
1964 {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;}
1965 }
1966 | mlhs_head tSTAR
1967 {
1968#if 0
1969 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
1970#endif
1971 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1972 }
1973 | mlhs_head tSTAR ',' mlhs_post
1974 {
1975#if 0
1976 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
1977#endif
1978 {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;}
1979 }
1980 | tSTAR mlhs_node
1981 {
1982#if 0
1983 $$ = NEW_MASGN(0, $2, &@$);
1984#endif
1985 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$2;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1986 }
1987 | tSTAR mlhs_node ',' mlhs_post
1988 {
1989#if 0
1990 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
1991#endif
1992 {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;}
1993 }
1994 | tSTAR
1995 {
1996#if 0
1997 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
1998#endif
1999 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=Qnil;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
2000 }
2001 | tSTAR ',' mlhs_post
2002 {
2003#if 0
2004 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
2005#endif
2006 {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;}
2007 }
2008 ;
2009
2010mlhs_item : mlhs_node
2011 | tLPAREN mlhs_inner rparen
2012 {
2013#if 0
2014 $$ = $2;
2015#endif
2016 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
2017 }
2018 ;
2019
2020mlhs_head : mlhs_item ','
2021 {
2022#if 0
2023 $$ = NEW_LIST($1, &@1);
2024#endif
2025 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
2026 }
2027 | mlhs_head mlhs_item ','
2028 {
2029#if 0
2030 $$ = list_append(p, $1, $2);
2031#endif
2032 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
2033 }
2034 ;
2035
2036mlhs_post : mlhs_item
2037 {
2038#if 0
2039 $$ = NEW_LIST($1, &@$);
2040#endif
2041 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
2042 }
2043 | mlhs_post ',' mlhs_item
2044 {
2045#if 0
2046 $$ = list_append(p, $1, $3);
2047#endif
2048 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
2049 }
2050 ;
2051
2052mlhs_node : user_variable
2053 {
2054#if 0
2055 $$ = assignable(p, $1, 0, &@$);
2056#endif
2057 $$=assignable(p, var_field(p, $1));
2058 }
2059 | keyword_variable
2060 {
2061#if 0
2062 $$ = assignable(p, $1, 0, &@$);
2063#endif
2064 $$=assignable(p, var_field(p, $1));
2065 }
2066 | primary_value '[' opt_call_args rbracket
2067 {
2068#if 0
2069 $$ = aryset(p, $1, $3, &@$);
2070#endif
2071 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
2072 }
2073 | primary_value call_op tIDENTIFIER
2074 {
2075 if ($2 == tANDDOT) {
2076 yyerror1(&@2, "&. inside multiple assignment destination");
2077 }
2078#if 0
2079 $$ = attrset(p, $1, $2, $3, &@$);
2080#endif
2081 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
2082 }
2083 | primary_value tCOLON2 tIDENTIFIER
2084 {
2085#if 0
2086 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2087#endif
2088 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=v3;}
2089 }
2090 | primary_value call_op tCONSTANT
2091 {
2092 if ($2 == tANDDOT) {
2093 yyerror1(&@2, "&. inside multiple assignment destination");
2094 }
2095#if 0
2096 $$ = attrset(p, $1, $2, $3, &@$);
2097#endif
2098 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
2099 }
2100 | primary_value tCOLON2 tCONSTANT
2101 {
2102#if 0
2103 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2104#endif
2105 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
2106 }
2107 | tCOLON3 tCONSTANT
2108 {
2109#if 0
2110 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2111#endif
2112 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
2113 }
2114 | backref
2115 {
2116#if 0
2117 rb_backref_error(p, $1);
2118 $$ = NEW_BEGIN(0, &@$);
2119#endif
2120 $$=backref_error(p, RNODE($1), var_field(p, $1));ripper_error(p);
2121 }
2122 ;
2123
2124lhs : user_variable
2125 {
2126#if 0
2127 $$ = assignable(p, $1, 0, &@$);
2128#endif
2129 $$=assignable(p, var_field(p, $1));
2130 }
2131 | keyword_variable
2132 {
2133#if 0
2134 $$ = assignable(p, $1, 0, &@$);
2135#endif
2136 $$=assignable(p, var_field(p, $1));
2137 }
2138 | primary_value '[' opt_call_args rbracket
2139 {
2140#if 0
2141 $$ = aryset(p, $1, $3, &@$);
2142#endif
2143 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
2144 }
2145 | primary_value call_op tIDENTIFIER
2146 {
2147#if 0
2148 $$ = attrset(p, $1, $2, $3, &@$);
2149#endif
2150 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
2151 }
2152 | primary_value tCOLON2 tIDENTIFIER
2153 {
2154#if 0
2155 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2156#endif
2157 {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
2158 }
2159 | primary_value call_op tCONSTANT
2160 {
2161#if 0
2162 $$ = attrset(p, $1, $2, $3, &@$);
2163#endif
2164 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
2165 }
2166 | primary_value tCOLON2 tCONSTANT
2167 {
2168#if 0
2169 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2170#endif
2171 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
2172 }
2173 | tCOLON3 tCONSTANT
2174 {
2175#if 0
2176 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2177#endif
2178 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
2179 }
2180 | backref
2181 {
2182#if 0
2183 rb_backref_error(p, $1);
2184 $$ = NEW_BEGIN(0, &@$);
2185#endif
2186 $$=backref_error(p, RNODE($1), var_field(p, $1));ripper_error(p);
2187 }
2188 ;
2189
2190cname : tIDENTIFIER
2191 {
2192 static const char mesg[] = "class/module name must be CONSTANT";
2193#if 0
2194 yyerror1(&@1, mesg);
2195#endif
2196 {VALUE v1,v2,v3;v1=ERR_MESG();v2=$1;v3=dispatch2(class_name_error,v1,v2);$$=v3;}ripper_error(p);
2197 }
2198 | tCONSTANT
2199 ;
2200
2201cpath : tCOLON3 cname
2202 {
2203#if 0
2204 $$ = NEW_COLON3($2, &@$);
2205#endif
2206 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2207 }
2208 | cname
2209 {
2210#if 0
2211 $$ = NEW_COLON2(0, $$, &@$);
2212#endif
2213 {VALUE v1,v2;v1=$1;v2=dispatch1(const_ref,v1);$$=v2;}
2214 }
2215 | primary_value tCOLON2 cname
2216 {
2217#if 0
2218 $$ = NEW_COLON2($1, $3, &@$);
2219#endif
2220 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2221 }
2222 ;
2223
2224fname : tIDENTIFIER
2225 | tCONSTANT
2226 | tFID
2227 | op
2228 {
2229 SET_LEX_STATE(EXPR_ENDFN);
2230 $$ = $1;
2231 }
2232 | reswords
2233 ;
2234
2235fitem : fname
2236 {
2237#if 0
2238 $$ = NEW_LIT(ID2SYM($1), &@$);
2239#endif
2240 {VALUE v1,v2;v1=$1;v2=dispatch1(symbol_literal,v1);$$=v2;}
2241 }
2242 | symbol
2243 ;
2244
2245undef_list : fitem
2246 {
2247#if 0
2248 $$ = NEW_UNDEF($1, &@$);
2249#endif
2250 $$=rb_ary_new3(1, get_value($1));
2251 }
2252 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2253 {
2254#if 0
2255 NODE *undef = NEW_UNDEF($4, &@4);
2256 $$ = block_append(p, $1, undef);
2257#endif
2258 $$=rb_ary_push($1, get_value($4));
2259 }
2260 ;
2261
2262op : '|' { ifndef_ripper($$ = '|'); }
2263 | '^' { ifndef_ripper($$ = '^'); }
2264 | '&' { ifndef_ripper($$ = '&'); }
2265 | tCMP { ifndef_ripper($$ = tCMP); }
2266 | tEQ { ifndef_ripper($$ = tEQ); }
2267 | tEQQ { ifndef_ripper($$ = tEQQ); }
2268 | tMATCH { ifndef_ripper($$ = tMATCH); }
2269 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2270 | '>' { ifndef_ripper($$ = '>'); }
2271 | tGEQ { ifndef_ripper($$ = tGEQ); }
2272 | '<' { ifndef_ripper($$ = '<'); }
2273 | tLEQ { ifndef_ripper($$ = tLEQ); }
2274 | tNEQ { ifndef_ripper($$ = tNEQ); }
2275 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2276 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2277 | '+' { ifndef_ripper($$ = '+'); }
2278 | '-' { ifndef_ripper($$ = '-'); }
2279 | '*' { ifndef_ripper($$ = '*'); }
2280 | tSTAR { ifndef_ripper($$ = '*'); }
2281 | '/' { ifndef_ripper($$ = '/'); }
2282 | '%' { ifndef_ripper($$ = '%'); }
2283 | tPOW { ifndef_ripper($$ = tPOW); }
2284 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2285 | '!' { ifndef_ripper($$ = '!'); }
2286 | '~' { ifndef_ripper($$ = '~'); }
2287 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2288 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2289 | tAREF { ifndef_ripper($$ = tAREF); }
2290 | tASET { ifndef_ripper($$ = tASET); }
2291 | '`' { ifndef_ripper($$ = '`'); }
2292 ;
2293
2294reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2295 | keyword_BEGIN | keyword_END
2296 | keyword_alias | keyword_and | keyword_begin
2297 | keyword_break | keyword_case | keyword_class | keyword_def
2298 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2299 | keyword_end | keyword_ensure | keyword_false
2300 | keyword_for | keyword_in | keyword_module | keyword_next
2301 | keyword_nil | keyword_not | keyword_or | keyword_redo
2302 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2303 | keyword_super | keyword_then | keyword_true | keyword_undef
2304 | keyword_when | keyword_yield | keyword_if | keyword_unless
2305 | keyword_while | keyword_until
2306 ;
2307
2308arg : lhs '=' lex_ctxt arg_rhs
2309 {
2310#if 0
2311 $$ = node_assign(p, $1, $4, $3, &@$);
2312#endif
2313 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(assign,v1,v2);$$=v3;}
2314 }
2315 | var_lhs tOP_ASGN lex_ctxt arg_rhs
2316 {
2317#if 0
2318 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
2319#endif
2320 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$4;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
2321 }
2322 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
2323 {
2324#if 0
2325 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
2326#endif
2327 {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=$7;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
2328 }
2329 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2330 {
2331#if 0
2332 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2333#endif
2334 {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=$6;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2335 }
2336 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2337 {
2338#if 0
2339 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2340#endif
2341 {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=$6;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2342 }
2343 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2344 {
2345#if 0
2346 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
2347#endif
2348 {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=$6;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2349 }
2350 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2351 {
2352#if 0
2353 YYLTYPE loc = code_loc_gen(&@1, &@3);
2354 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
2355#endif
2356 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);v4=v3;v5=$4;v6=$6;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
2357 }
2358 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2359 {
2360#if 0
2361 $$ = new_const_op_assign(p, NEW_COLON3($2, &@$), $3, $5, $4, &@$);
2362#endif
2363 {VALUE v1,v2,v3,v4,v5,v6;v1=$2;v2=dispatch1(top_const_field,v1);v3=v2;v4=$3;v5=$5;v6=dispatch3(opassign,v3,v4,v5);$$=v6;}
2364 }
2365 | backref tOP_ASGN lex_ctxt arg_rhs
2366 {
2367#if 0
2368 rb_backref_error(p, $1);
2369 $$ = NEW_BEGIN(0, &@$);
2370#endif
2371 {VALUE v1,v2,v3,v4;v1=var_field(p, $1);v2=$2;v3=$4;v4=dispatch3(opassign,v1,v2,v3);$$=backref_error(p, RNODE($1), v4);}ripper_error(p);
2372 }
2373 | arg tDOT2 arg
2374 {
2375#if 0
2376 value_expr($1);
2377 value_expr($3);
2378 $$ = NEW_DOT2($1, $3, &@$);
2379#endif
2380 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
2381 }
2382 | arg tDOT3 arg
2383 {
2384#if 0
2385 value_expr($1);
2386 value_expr($3);
2387 $$ = NEW_DOT3($1, $3, &@$);
2388#endif
2389 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
2390 }
2391 | arg tDOT2
2392 {
2393#if 0
2394 value_expr($1);
2395 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
2396#endif
2397 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
2398 }
2399 | arg tDOT3
2400 {
2401#if 0
2402 value_expr($1);
2403 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
2404#endif
2405 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
2406 }
2407 | tBDOT2 arg
2408 {
2409#if 0
2410 value_expr($2);
2411 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
2412#endif
2413 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
2414 }
2415 | tBDOT3 arg
2416 {
2417#if 0
2418 value_expr($2);
2419 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
2420#endif
2421 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
2422 }
2423 | arg '+' arg
2424 {
2425 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2426 }
2427 | arg '-' arg
2428 {
2429 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2430 }
2431 | arg '*' arg
2432 {
2433 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2434 }
2435 | arg '/' arg
2436 {
2437 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2438 }
2439 | arg '%' arg
2440 {
2441 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2442 }
2443 | arg tPOW arg
2444 {
2445 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2446 }
2447 | tUMINUS_NUM simple_numeric tPOW arg
2448 {
2449 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2450 }
2451 | tUPLUS arg
2452 {
2453 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2454 }
2455 | tUMINUS arg
2456 {
2457 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2458 }
2459 | arg '|' arg
2460 {
2461 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2462 }
2463 | arg '^' arg
2464 {
2465 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2466 }
2467 | arg '&' arg
2468 {
2469 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2470 }
2471 | arg tCMP arg
2472 {
2473 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2474 }
2475 | rel_expr %prec tCMP
2476 | arg tEQ arg
2477 {
2478 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2479 }
2480 | arg tEQQ arg
2481 {
2482 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2483 }
2484 | arg tNEQ arg
2485 {
2486 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2487 }
2488 | arg tMATCH arg
2489 {
2490 $$ = match_op(p, $1, $3, &@2, &@$);
2491 }
2492 | arg tNMATCH arg
2493 {
2494 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2495 }
2496 | '!' arg
2497 {
2498 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2499 }
2500 | '~' arg
2501 {
2502 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2503 }
2504 | arg tLSHFT arg
2505 {
2506 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2507 }
2508 | arg tRSHFT arg
2509 {
2510 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2511 }
2512 | arg tANDOP arg
2513 {
2514 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2515 }
2516 | arg tOROP arg
2517 {
2518 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2519 }
2520 | keyword_defined opt_nl {p->ctxt.in_defined = 1;} arg
2521 {
2522 p->ctxt.in_defined = 0;
2523 $$ = new_defined(p, $4, &@$);
2524 }
2525 | arg '?' arg opt_nl ':' arg
2526 {
2527#if 0
2528 value_expr($1);
2529 $$ = new_if(p, $1, $3, $6, &@$);
2530 fixpos($$, $1);
2531#endif
2532 {VALUE v1,v2,v3,v4;v1=$1;v2=$3;v3=$6;v4=dispatch3(ifop,v1,v2,v3);$$=v4;}
2533 }
2534 | defn_head f_opt_paren_args '=' arg
2535 {
2536 endless_method_name(p, $<node>1, &@1);
2537 restore_defun(p, $<node>1->nd_defn);
2538#if 0
2539 $$ = set_defun_body(p, $1, $2, $4, &@$);
2540#endif
2541 {VALUE v1,v2,v3,v4;v1=get_value($1);v2=$2;v3=$4;v4=dispatch3(def,v1,v2,v3);$$=v4;}
2542 local_pop(p);
2543 }
2544 | defn_head f_opt_paren_args '=' arg modifier_rescue arg
2545 {
2546 endless_method_name(p, $<node>1, &@1);
2547 restore_defun(p, $<node>1->nd_defn);
2548#if 0
2549 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2550 $$ = set_defun_body(p, $1, $2, $4, &@$);
2551#endif
2552 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$4;v2=$6;v3=dispatch2(rescue_mod,v1,v2);v4=get_value($1);v5=$2;v6=v3;v7=dispatch3(def,v4,v5,v6);$$=v7;}
2553 local_pop(p);
2554 }
2555 | defs_head f_opt_paren_args '=' arg
2556 {
2557 endless_method_name(p, $<node>1, &@1);
2558 restore_defun(p, $<node>1->nd_defn);
2559#if 0
2560 $$ = set_defun_body(p, $1, $2, $4, &@$);
2561#endif
2562 $1 = get_value($1);
2563
2564 {VALUE v1,v2,v3,v4,v5,v6;v1=AREF($1, 0);v2=AREF($1, 1);v3=AREF($1, 2);v4=$2;v5=$4;v6=dispatch5(defs,v1,v2,v3,v4,v5);$$=v6;}
2565 local_pop(p);
2566 }
2567 | defs_head f_opt_paren_args '=' arg modifier_rescue arg
2568 {
2569 endless_method_name(p, $<node>1, &@1);
2570 restore_defun(p, $<node>1->nd_defn);
2571#if 0
2572 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2573 $$ = set_defun_body(p, $1, $2, $4, &@$);
2574#endif
2575 $1 = get_value($1);
2576
2577 {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9;v1=$4;v2=$6;v3=dispatch2(rescue_mod,v1,v2);v4=AREF($1, 0);v5=AREF($1, 1);v6=AREF($1, 2);v7=$2;v8=v3;v9=dispatch5(defs,v4,v5,v6,v7,v8);$$=v9;}
2578 local_pop(p);
2579 }
2580 | primary
2581 {
2582 $$ = $1;
2583 }
2584 ;
2585
2586relop : '>' {$$ = '>';}
2587 | '<' {$$ = '<';}
2588 | tGEQ {$$ = idGE;}
2589 | tLEQ {$$ = idLE;}
2590 ;
2591
2592rel_expr : arg relop arg %prec '>'
2593 {
2594 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2595 }
2596 | rel_expr relop arg %prec '>'
2597 {
2598 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2599 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2600 }
2601 ;
2602
2603lex_ctxt : tSP
2604 {
2605 $$ = p->ctxt;
2606 }
2607 | none
2608 {
2609 $$ = p->ctxt;
2610 }
2611 ;
2612
2613arg_value : arg
2614 {
2615 value_expr($1);
2616 $$ = $1;
2617 }
2618 ;
2619
2620aref_args : none
2621 | args trailer
2622 {
2623 $$ = $1;
2624 }
2625 | args ',' assocs trailer
2626 {
2627#if 0
2628 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2629#endif
2630 {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;}
2631 }
2632 | assocs trailer
2633 {
2634#if 0
2635 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2636#endif
2637 {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;}
2638 }
2639 ;
2640
2641arg_rhs : arg %prec tOP_ASGN
2642 {
2643 value_expr($1);
2644 $$ = $1;
2645 }
2646 | arg modifier_rescue arg
2647 {
2648#if 0
2649 value_expr($1);
2650 $$ = rescued_expr(p, $1, $3, &@1, &@2, &@3);
2651#endif
2652 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
2653 }
2654 ;
2655
2656paren_args : '(' opt_call_args rparen
2657 {
2658#if 0
2659 $$ = $2;
2660#endif
2661 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(arg_paren,v1);$$=v2;}
2662 }
2663 | '(' args ',' args_forward rparen
2664 {
2665 if (!check_forwarding_args(p)) {
2666 $$ = Qnone;
2667 }
2668 else {
2669#if 0
2670 $$ = new_args_forward_call(p, $2, &@4, &@$);
2671#endif
2672 {VALUE v1,v2,v3,v4,v5;v1=$2;v2=$4;v3=dispatch2(args_add,v1,v2);v4=v3;v5=dispatch1(arg_paren,v4);$$=v5;}
2673 }
2674 }
2675 | '(' args_forward rparen
2676 {
2677 if (!check_forwarding_args(p)) {
2678 $$ = Qnone;
2679 }
2680 else {
2681#if 0
2682 $$ = new_args_forward_call(p, 0, &@2, &@$);
2683#endif
2684 {VALUE v1,v2;v1=$2;v2=dispatch1(arg_paren,v1);$$=v2;}
2685 }
2686 }
2687 ;
2688
2689opt_paren_args : none
2690 | paren_args
2691 ;
2692
2693opt_call_args : none
2694 | call_args
2695 | args ','
2696 {
2697 $$ = $1;
2698 }
2699 | args ',' assocs ','
2700 {
2701#if 0
2702 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2703#endif
2704 {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;}
2705 }
2706 | assocs ','
2707 {
2708#if 0
2709 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2710#endif
2711 {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;}
2712 }
2713 ;
2714
2715call_args : command
2716 {
2717#if 0
2718 value_expr($1);
2719 $$ = NEW_LIST($1, &@$);
2720#endif
2721 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2722 }
2723 | args opt_block_arg
2724 {
2725#if 0
2726 $$ = arg_blk_pass($1, $2);
2727#endif
2728 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(args_add_block,v1,v2);$$=v3;}
2729 }
2730 | assocs opt_block_arg
2731 {
2732#if 0
2733 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2734 $$ = arg_blk_pass($$, $2);
2735#endif
2736 {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;}
2737 }
2738 | args ',' assocs opt_block_arg
2739 {
2740#if 0
2741 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2742 $$ = arg_blk_pass($$, $4);
2743#endif
2744 {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;}
2745 }
2746 | block_arg
2747 {{VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add_block,v2,v3);$$=v4;}}
2748 ;
2749
2750command_args : {
2751 /* If call_args starts with a open paren '(' or '[',
2752 * look-ahead reading of the letters calls CMDARG_PUSH(0),
2753 * but the push must be done after CMDARG_PUSH(1).
2754 * So this code makes them consistent by first cancelling
2755 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
2756 * and finally redoing CMDARG_PUSH(0).
2757 */
2758 int lookahead = 0;
2759 switch (yychar) {
2760 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
2761 lookahead = 1;
2762 }
2763 if (lookahead) CMDARG_POP();
2764 CMDARG_PUSH(1);
2765 if (lookahead) CMDARG_PUSH(0);
2766 }
2767 call_args
2768 {
2769 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
2770 * but the push must be done after CMDARG_POP() in the parser.
2771 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
2772 * CMDARG_POP() to pop 1 pushed by command_args,
2773 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
2774 */
2775 int lookahead = 0;
2776 switch (yychar) {
2777 case tLBRACE_ARG:
2778 lookahead = 1;
2779 }
2780 if (lookahead) CMDARG_POP();
2781 CMDARG_POP();
2782 if (lookahead) CMDARG_PUSH(0);
2783 $$ = $2;
2784 }
2785 ;
2786
2787block_arg : tAMPER arg_value
2788 {
2789#if 0
2790 $$ = NEW_BLOCK_PASS($2, &@$);
2791#endif
2792 $$=$2;
2793 }
2794 ;
2795
2796opt_block_arg : ',' block_arg
2797 {
2798 $$ = $2;
2799 }
2800 | none
2801 {
2802 $$ = 0;
2803 }
2804 ;
2805
2806args : arg_value
2807 {
2808#if 0
2809 $$ = NEW_LIST($1, &@$);
2810#endif
2811 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2812 }
2813 | tSTAR arg_value
2814 {
2815#if 0
2816 $$ = NEW_SPLAT($2, &@$);
2817#endif
2818 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
2819 }
2820 | args ',' arg_value
2821 {
2822#if 0
2823 $$ = last_arg_append(p, $1, $3, &@$);
2824#endif
2825 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
2826 }
2827 | args ',' tSTAR arg_value
2828 {
2829#if 0
2830 $$ = rest_arg_append(p, $1, $4, &@$);
2831#endif
2832 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
2833 }
2834 ;
2835
2836mrhs_arg : mrhs
2837 | arg_value
2838 ;
2839
2840mrhs : args ',' arg_value
2841 {
2842#if 0
2843 $$ = last_arg_append(p, $1, $3, &@$);
2844#endif
2845 {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;}
2846 }
2847 | args ',' tSTAR arg_value
2848 {
2849#if 0
2850 $$ = rest_arg_append(p, $1, $4, &@$);
2851#endif
2852 {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;}
2853 }
2854 | tSTAR arg_value
2855 {
2856#if 0
2857 $$ = NEW_SPLAT($2, &@$);
2858#endif
2859 {VALUE v1,v2,v3,v4;v1=dispatch0(mrhs_new);v2=v1;v3=$2;v4=dispatch2(mrhs_add_star,v2,v3);$$=v4;}
2860 }
2861 ;
2862
2863primary : literal
2864 | strings
2865 | xstring
2866 | regexp
2867 | words
2868 | qwords
2869 | symbols
2870 | qsymbols
2871 | var_ref
2872 | backref
2873 | tFID
2874 {
2875#if 0
2876 $$ = NEW_FCALL($1, 0, &@$);
2877#endif
2878 {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;}
2879 }
2880 | k_begin
2881 {
2882 CMDARG_PUSH(0);
2883 }
2884 bodystmt
2885 k_end
2886 {
2887 CMDARG_POP();
2888#if 0
2889 set_line_body($3, @1.end_pos.lineno);
2890 $$ = NEW_BEGIN($3, &@$);
2891 nd_set_line($$, @1.end_pos.lineno);
2892#endif
2893 {VALUE v1,v2;v1=$3;v2=dispatch1(begin,v1);$$=v2;}
2894 }
2895 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2896 {
2897#if 0
2898 $$ = NEW_BEGIN(0, &@$);
2899#endif
2900 {VALUE v1,v2;v1=0;v2=dispatch1(paren,v1);$$=v2;}
2901 }
2902 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2903 {
2904#if 0
2905 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2906 $$ = $2;
2907#endif
2908 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2909 }
2910 | tLPAREN compstmt ')'
2911 {
2912#if 0
2913 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2914 $$ = $2;
2915#endif
2916 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2917 }
2918 | primary_value tCOLON2 tCONSTANT
2919 {
2920#if 0
2921 $$ = NEW_COLON2($1, $3, &@$);
2922#endif
2923 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2924 }
2925 | tCOLON3 tCONSTANT
2926 {
2927#if 0
2928 $$ = NEW_COLON3($2, &@$);
2929#endif
2930 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2931 }
2932 | tLBRACK aref_args ']'
2933 {
2934#if 0
2935 $$ = make_list($2, &@$);
2936#endif
2937 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(array,v1);$$=v2;}
2938 }
2939 | tLBRACE assoc_list '}'
2940 {
2941#if 0
2942 $$ = new_hash(p, $2, &@$);
2943 $$->nd_brace = TRUE;
2944#endif
2945 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(hash,v1);$$=v2;}
2946 }
2947 | k_return
2948 {
2949#if 0
2950 $$ = NEW_RETURN(0, &@$);
2951#endif
2952 {VALUE v1;v1=dispatch0(return0);$$=v1;}
2953 }
2954 | keyword_yield '(' call_args rparen
2955 {
2956#if 0
2957 $$ = new_yield(p, $3, &@$);
2958#endif
2959 {VALUE v1,v2,v3,v4;v1=$3;v2=dispatch1(paren,v1);v3=v2;v4=dispatch1(yield,v3);$$=v4;}
2960 }
2961 | keyword_yield '(' rparen
2962 {
2963#if 0
2964 $$ = NEW_YIELD(0, &@$);
2965#endif
2966 {VALUE v1,v2,v3,v4,v5;v1=dispatch0(args_new);v2=v1;v3=dispatch1(paren,v2);v4=v3;v5=dispatch1(yield,v4);$$=v5;}
2967 }
2968 | keyword_yield
2969 {
2970#if 0
2971 $$ = NEW_YIELD(0, &@$);
2972#endif
2973 {VALUE v1;v1=dispatch0(yield0);$$=v1;}
2974 }
2975 | keyword_defined opt_nl '(' {p->ctxt.in_defined = 1;} expr rparen
2976 {
2977 p->ctxt.in_defined = 0;
2978 $$ = new_defined(p, $5, &@$);
2979 }
2980 | keyword_not '(' expr rparen
2981 {
2982 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2983 }
2984 | keyword_not '(' rparen
2985 {
2986 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
2987 }
2988 | fcall brace_block
2989 {
2990#if 0
2991 $$ = method_add_block(p, $1, $2, &@$);
2992#endif
2993 {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;}
2994 }
2995 | method_call
2996 | method_call brace_block
2997 {
2998#if 0
2999 block_dup_check(p, $1->nd_args, $2);
3000 $$ = method_add_block(p, $1, $2, &@$);
3001#endif
3002 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
3003 }
3004 | lambda
3005 | k_if expr_value then
3006 compstmt
3007 if_tail
3008 k_end
3009 {
3010#if 0
3011 $$ = new_if(p, $2, $4, $5, &@$);
3012 fixpos($$, $2);
3013#endif
3014 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(if,v1,v2,v3);$$=v4;}
3015 }
3016 | k_unless expr_value then
3017 compstmt
3018 opt_else
3019 k_end
3020 {
3021#if 0
3022 $$ = new_unless(p, $2, $4, $5, &@$);
3023 fixpos($$, $2);
3024#endif
3025 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(unless,v1,v2,v3);$$=v4;}
3026 }
3027 | k_while expr_value_do
3028 compstmt
3029 k_end
3030 {
3031#if 0
3032 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
3033 fixpos($$, $2);
3034#endif
3035 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(while,v1,v2);$$=v3;}
3036 }
3037 | k_until expr_value_do
3038 compstmt
3039 k_end
3040 {
3041#if 0
3042 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
3043 fixpos($$, $2);
3044#endif
3045 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(until,v1,v2);$$=v3;}
3046 }
3047 | k_case expr_value opt_terms
3048 {
3049 $<val>$ = p->case_labels;
3050 p->case_labels = Qnil;
3051 }
3052 case_body
3053 k_end
3054 {
3055 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3056 p->case_labels = $<val>4;
3057#if 0
3058 $$ = NEW_CASE($2, $5, &@$);
3059 fixpos($$, $2);
3060#endif
3061 {VALUE v1,v2,v3;v1=$2;v2=$5;v3=dispatch2(case,v1,v2);$$=v3;}
3062 }
3063 | k_case opt_terms
3064 {
3065 $<val>$ = p->case_labels;
3066 p->case_labels = 0;
3067 }
3068 case_body
3069 k_end
3070 {
3071 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3072 p->case_labels = $<val>3;
3073#if 0
3074 $$ = NEW_CASE2($4, &@$);
3075#endif
3076 {VALUE v1,v2,v3;v1=Qnil;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
3077 }
3078 | k_case expr_value opt_terms
3079 p_case_body
3080 k_end
3081 {
3082#if 0
3083 $$ = NEW_CASE3($2, $4, &@$);
3084#endif
3085 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
3086 }
3087 | k_for for_var keyword_in expr_value_do
3088 compstmt
3089 k_end
3090 {
3091#if 0
3092 /*
3093 * for a, b, c in e
3094 * #=>
3095 * e.each{|*x| a, b, c = x}
3096 *
3097 * for a in e
3098 * #=>
3099 * e.each{|x| a, = x}
3100 */
3101 ID id = internal_id(p);
3102 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
3103 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
3104 ID *tbl = ALLOC_N(ID, 3);
3105 tbl[0] = 1 /* length of local var table */; tbl[1] = id /* internal id */;
3106 rb_ast_add_local_table(p->ast, tbl);
3107
3108 switch (nd_type($2)) {
3109 case NODE_LASGN:
3110 case NODE_DASGN:
3111 case NODE_DASGN_CURR: /* e.each {|internal_var| a = internal_var; ... } */
3112 $2->nd_value = internal_var;
3113 id = 0;
3114 m->nd_plen = 1;
3115 m->nd_next = $2;
3116 break;
3117 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
3118 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
3119 break;
3120 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
3121 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
3122 }
3123 /* {|*internal_id| <m> = internal_id; ... } */
3124 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
3125 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
3126 $$ = NEW_FOR($4, scope, &@$);
3127 fixpos($$, $2);
3128#endif
3129 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=$5;v4=dispatch3(for,v1,v2,v3);$$=v4;}
3130 }
3131 | k_class cpath superclass
3132 {
3133 if (p->ctxt.in_def) {
3134 YYLTYPE loc = code_loc_gen(&@1, &@2);
3135 yyerror1(&loc, "class definition in method body");
3136 }
3137 p->ctxt.in_class = 1;
3138 local_push(p, 0);
3139 }
3140 bodystmt
3141 k_end
3142 {
3143#if 0
3144 $$ = NEW_CLASS($2, $5, $3, &@$);
3145 nd_set_line($$->nd_body, @6.end_pos.lineno);
3146 set_line_body($5, @3.end_pos.lineno);
3147 nd_set_line($$, @3.end_pos.lineno);
3148#endif
3149 {VALUE v1,v2,v3,v4;v1=$2;v2=$3;v3=$5;v4=dispatch3(class,v1,v2,v3);$$=v4;}
3150 local_pop(p);
3151 p->ctxt.in_class = $<ctxt>1.in_class;
3152 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3153 }
3154 | k_class tLSHFT expr
3155 {
3156 p->ctxt.in_def = 0;
3157 p->ctxt.in_class = 0;
3158 local_push(p, 0);
3159 }
3160 term
3161 bodystmt
3162 k_end
3163 {
3164#if 0
3165 $$ = NEW_SCLASS($3, $6, &@$);
3166 nd_set_line($$->nd_body, @7.end_pos.lineno);
3167 set_line_body($6, nd_line($3));
3168 fixpos($$, $3);
3169#endif
3170 {VALUE v1,v2,v3;v1=$3;v2=$6;v3=dispatch2(sclass,v1,v2);$$=v3;}
3171 local_pop(p);
3172 p->ctxt.in_def = $<ctxt>1.in_def;
3173 p->ctxt.in_class = $<ctxt>1.in_class;
3174 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3175 }
3176 | k_module cpath
3177 {
3178 if (p->ctxt.in_def) {
3179 YYLTYPE loc = code_loc_gen(&@1, &@2);
3180 yyerror1(&loc, "module definition in method body");
3181 }
3182 p->ctxt.in_class = 1;
3183 local_push(p, 0);
3184 }
3185 bodystmt
3186 k_end
3187 {
3188#if 0
3189 $$ = NEW_MODULE($2, $4, &@$);
3190 nd_set_line($$->nd_body, @5.end_pos.lineno);
3191 set_line_body($4, @2.end_pos.lineno);
3192 nd_set_line($$, @2.end_pos.lineno);
3193#endif
3194 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(module,v1,v2);$$=v3;}
3195 local_pop(p);
3196 p->ctxt.in_class = $<ctxt>1.in_class;
3197 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3198 }
3199 | defn_head
3200 f_arglist
3201 bodystmt
3202 k_end
3203 {
3204 restore_defun(p, $<node>1->nd_defn);
3205#if 0
3206 $$ = set_defun_body(p, $1, $2, $3, &@$);
3207#endif
3208 {VALUE v1,v2,v3,v4;v1=get_value($1);v2=$2;v3=$3;v4=dispatch3(def,v1,v2,v3);$$=v4;}
3209 local_pop(p);
3210 }
3211 | defs_head
3212 f_arglist
3213 bodystmt
3214 k_end
3215 {
3216 restore_defun(p, $<node>1->nd_defn);
3217#if 0
3218 $$ = set_defun_body(p, $1, $2, $3, &@$);
3219#endif
3220 $1 = get_value($1);
3221
3222 {VALUE v1,v2,v3,v4,v5,v6;v1=AREF($1, 0);v2=AREF($1, 1);v3=AREF($1, 2);v4=$2;v5=$3;v6=dispatch5(defs,v1,v2,v3,v4,v5);$$=v6;}
3223 local_pop(p);
3224 }
3225 | keyword_break
3226 {
3227#if 0
3228 $$ = NEW_BREAK(0, &@$);
3229#endif
3230 {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(break,v2);$$=v3;}
3231 }
3232 | keyword_next
3233 {
3234#if 0
3235 $$ = NEW_NEXT(0, &@$);
3236#endif
3237 {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(next,v2);$$=v3;}
3238 }
3239 | keyword_redo
3240 {
3241#if 0
3242 $$ = NEW_REDO(&@$);
3243#endif
3244 {VALUE v1;v1=dispatch0(redo);$$=v1;}
3245 }
3246 | keyword_retry
3247 {
3248#if 0
3249 $$ = NEW_RETRY(&@$);
3250#endif
3251 {VALUE v1;v1=dispatch0(retry);$$=v1;}
3252 }
3253 ;
3254
3255primary_value : primary
3256 {
3257 value_expr($1);
3258 $$ = $1;
3259 }
3260 ;
3261
3262k_begin : keyword_begin
3263 {
3264 token_info_push(p, "begin", &@$);
3265 }
3266 ;
3267
3268k_if : keyword_if
3269 {
3270 WARN_EOL("if");
3271 token_info_push(p, "if", &@$);
3272 if (p->token_info && p->token_info->nonspc &&
3273 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3274 const char *tok = p->lex.ptok;
3275 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3276 beg += rb_strlen_lit("else");
3277 while (beg < tok && ISSPACE(*beg)) beg++;
3278 if (beg == tok) {
3279 p->token_info->nonspc = 0;
3280 }
3281 }
3282 }
3283 ;
3284
3285k_unless : keyword_unless
3286 {
3287 token_info_push(p, "unless", &@$);
3288 }
3289 ;
3290
3291k_while : keyword_while
3292 {
3293 token_info_push(p, "while", &@$);
3294 }
3295 ;
3296
3297k_until : keyword_until
3298 {
3299 token_info_push(p, "until", &@$);
3300 }
3301 ;
3302
3303k_case : keyword_case
3304 {
3305 token_info_push(p, "case", &@$);
3306 }
3307 ;
3308
3309k_for : keyword_for
3310 {
3311 token_info_push(p, "for", &@$);
3312 }
3313 ;
3314
3315k_class : keyword_class
3316 {
3317 token_info_push(p, "class", &@$);
3318 $<ctxt>$ = p->ctxt;
3319 }
3320 ;
3321
3322k_module : keyword_module
3323 {
3324 token_info_push(p, "module", &@$);
3325 $<ctxt>$ = p->ctxt;
3326 }
3327 ;
3328
3329k_def : keyword_def
3330 {
3331 token_info_push(p, "def", &@$);
3332 }
3333 ;
3334
3335k_do : keyword_do
3336 {
3337 token_info_push(p, "do", &@$);
3338 }
3339 ;
3340
3341k_do_block : keyword_do_block
3342 {
3343 token_info_push(p, "do", &@$);
3344 }
3345 ;
3346
3347k_rescue : keyword_rescue
3348 {
3349 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3350 }
3351 ;
3352
3353k_ensure : keyword_ensure
3354 {
3355 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3356 }
3357 ;
3358
3359k_when : keyword_when
3360 {
3361 token_info_warn(p, "when", p->token_info, 0, &@$);
3362 }
3363 ;
3364
3365k_else : keyword_else
3366 {
3367 token_info *ptinfo_beg = p->token_info;
3368 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3369 token_info_warn(p, "else", p->token_info, same, &@$);
3370 if (same) {
3371 token_info e;
3372 e.next = ptinfo_beg->next;
3373 e.token = "else";
3374 token_info_setup(&e, p->lex.pbeg, &@$);
3375 if (!e.nonspc) *ptinfo_beg = e;
3376 }
3377 }
3378 ;
3379
3380k_elsif : keyword_elsif
3381 {
3382 WARN_EOL("elsif");
3383 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3384 }
3385 ;
3386
3387k_end : keyword_end
3388 {
3389 token_info_pop(p, "end", &@$);
3390 }
3391 ;
3392
3393k_return : keyword_return
3394 {
3395 if (p->ctxt.in_class && !p->ctxt.in_def && !dyna_in_block(p))
3396 yyerror1(&@1, "Invalid return in class/module body");
3397 }
3398 ;
3399
3400then : term
3401 | keyword_then
3402 | term keyword_then
3403 ;
3404
3405do : term
3406 | keyword_do_cond
3407 ;
3408
3409if_tail : opt_else
3410 | k_elsif expr_value then
3411 compstmt
3412 if_tail
3413 {
3414#if 0
3415 $$ = new_if(p, $2, $4, $5, &@$);
3416 fixpos($$, $2);
3417#endif
3418 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(elsif,v1,v2,v3);$$=v4;}
3419 }
3420 ;
3421
3422opt_else : none
3423 | k_else compstmt
3424 {
3425#if 0
3426 $$ = $2;
3427#endif
3428 {VALUE v1,v2;v1=$2;v2=dispatch1(else,v1);$$=v2;}
3429 }
3430 ;
3431
3432for_var : lhs
3433 | mlhs
3434 ;
3435
3436f_marg : f_norm_arg
3437 {
3438#if 0
3439 $$ = assignable(p, $1, 0, &@$);
3440 mark_lvar_used(p, $$);
3441#endif
3442 $$=assignable(p, $1);
3443 }
3444 | tLPAREN f_margs rparen
3445 {
3446#if 0
3447 $$ = $2;
3448#endif
3449 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
3450 }
3451 ;
3452
3453f_marg_list : f_marg
3454 {
3455#if 0
3456 $$ = NEW_LIST($1, &@$);
3457#endif
3458 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
3459 }
3460 | f_marg_list ',' f_marg
3461 {
3462#if 0
3463 $$ = list_append(p, $1, $3);
3464#endif
3465 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
3466 }
3467 ;
3468
3469f_margs : f_marg_list
3470 {
3471#if 0
3472 $$ = NEW_MASGN($1, 0, &@$);
3473#endif
3474 $$=$1;
3475 }
3476 | f_marg_list ',' f_rest_marg
3477 {
3478#if 0
3479 $$ = NEW_MASGN($1, $3, &@$);
3480#endif
3481 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
3482 }
3483 | f_marg_list ',' f_rest_marg ',' f_marg_list
3484 {
3485#if 0
3486 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3487#endif
3488 {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;}
3489 }
3490 | f_rest_marg
3491 {
3492#if 0
3493 $$ = NEW_MASGN(0, $1, &@$);
3494#endif
3495 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
3496 }
3497 | f_rest_marg ',' f_marg_list
3498 {
3499#if 0
3500 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3501#endif
3502 {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;}
3503 }
3504 ;
3505
3506f_rest_marg : tSTAR f_norm_arg
3507 {
3508#if 0
3509 $$ = assignable(p, $2, 0, &@$);
3510 mark_lvar_used(p, $$);
3511#endif
3512 $$=assignable(p, $2);
3513 }
3514 | tSTAR
3515 {
3516#if 0
3517 $$ = NODE_SPECIAL_NO_NAME_REST;
3518#endif
3519 $$=Qnil;
3520 }
3521 ;
3522
3523f_any_kwrest : f_kwrest
3524 | f_no_kwarg {$$ = ID2VAL(idNil);}
3525 ;
3526
3527block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3528 {
3529 $$ = new_args_tail(p, $1, $3, $4, &@3);
3530 }
3531 | f_block_kwarg opt_f_block_arg
3532 {
3533 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3534 }
3535 | f_any_kwrest opt_f_block_arg
3536 {
3537 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3538 }
3539 | f_block_arg
3540 {
3541 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3542 }
3543 ;
3544
3545opt_block_args_tail : ',' block_args_tail
3546 {
3547 $$ = $2;
3548 }
3549 | /* none */
3550 {
3551 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3552 }
3553 ;
3554
3555excessed_comma : ','
3556 {
3557 /* magic number for rest_id in iseq_set_arguments() */
3558#if 0
3559 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
3560#endif
3561 {VALUE v1;v1=dispatch0(excessed_comma);$$=v1;}
3562 }
3563 ;
3564
3565block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3566 {
3567 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3568 }
3569 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3570 {
3571 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3572 }
3573 | f_arg ',' f_block_optarg opt_block_args_tail
3574 {
3575 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3576 }
3577 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3578 {
3579 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3580 }
3581 | f_arg ',' f_rest_arg opt_block_args_tail
3582 {
3583 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3584 }
3585 | f_arg excessed_comma
3586 {
3587 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@2);
3588 $$ = new_args(p, $1, Qnone, $2, Qnone, $$, &@$);
3589 }
3590 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3591 {
3592 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
3593 }
3594 | f_arg opt_block_args_tail
3595 {
3596 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
3597 }
3598 | f_block_optarg ',' f_rest_arg opt_block_args_tail
3599 {
3600 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
3601 }
3602 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3603 {
3604 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
3605 }
3606 | f_block_optarg opt_block_args_tail
3607 {
3608 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
3609 }
3610 | f_block_optarg ',' f_arg opt_block_args_tail
3611 {
3612 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
3613 }
3614 | f_rest_arg opt_block_args_tail
3615 {
3616 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
3617 }
3618 | f_rest_arg ',' f_arg opt_block_args_tail
3619 {
3620 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
3621 }
3622 | block_args_tail
3623 {
3624 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
3625 }
3626 ;
3627
3628opt_block_param : none
3629 | block_param_def
3630 {
3631 p->command_start = TRUE;
3632 }
3633 ;
3634
3635block_param_def : '|' opt_bv_decl '|'
3636 {
3637 p->cur_arg = 0;
3638 p->max_numparam = ORDINAL_PARAM;
3639#if 0
3640 $$ = 0;
3641#endif
3642 {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;}
3643 }
3644 | '|' block_param opt_bv_decl '|'
3645 {
3646 p->cur_arg = 0;
3647 p->max_numparam = ORDINAL_PARAM;
3648#if 0
3649 $$ = $2;
3650#endif
3651 {VALUE v1,v2,v3;v1=escape_Qundef($2);v2=escape_Qundef($3);v3=dispatch2(block_var,v1,v2);$$=v3;}
3652 }
3653 ;
3654
3655
3656opt_bv_decl : opt_nl
3657 {
3658 $$ = 0;
3659 }
3660 | opt_nl ';' bv_decls opt_nl
3661 {
3662#if 0
3663 $$ = 0;
3664#endif
3665 $$=$3;
3666 }
3667 ;
3668
3669bv_decls : bvar
3670 {$$=rb_ary_new3(1, get_value($1));}
3671 | bv_decls ',' bvar
3672 {$$=rb_ary_push($1, get_value($3));}
3673 ;
3674
3675bvar : tIDENTIFIER
3676 {
3677 new_bv(p, get_id($1));
3678 $$=get_value($1);
3679 }
3680 | f_bad_arg
3681 {
3682 $$ = 0;
3683 }
3684 ;
3685
3686lambda : tLAMBDA
3687 {
3688 token_info_push(p, "->", &@1);
3689 $<vars>1 = dyna_push(p);
3690 $<num>$ = p->lex.lpar_beg;
3691 p->lex.lpar_beg = p->lex.paren_nest;
3692 }
3693 {
3694 $<num>$ = p->max_numparam;
3695 p->max_numparam = 0;
3696 }
3697 {
3698 $<node>$ = numparam_push(p);
3699 }
3700 f_larglist
3701 {
3702 CMDARG_PUSH(0);
3703 }
3704 lambda_body
3705 {
3706 int max_numparam = p->max_numparam;
3707 p->lex.lpar_beg = $<num>2;
3708 p->max_numparam = $<num>3;
3709 CMDARG_POP();
3710 $5 = args_with_numbered(p, $5, max_numparam);
3711#if 0
3712 {
3713 YYLTYPE loc = code_loc_gen(&@5, &@7);
3714 $$ = NEW_LAMBDA($5, $7, &loc);
3715 nd_set_line($$->nd_body, @7.end_pos.lineno);
3716 nd_set_line($$, @5.end_pos.lineno);
3717 nd_set_first_loc($$, @1.beg_pos);
3718 }
3719#endif
3720 {VALUE v1,v2,v3;v1=$5;v2=$7;v3=dispatch2(lambda,v1,v2);$$=v3;}
3721 numparam_pop(p, $<node>4);
3722 dyna_pop(p, $<vars>1);
3723 }
3724 ;
3725
3726f_larglist : '(' f_args opt_bv_decl ')'
3727 {
3728#if 0
3729 $$ = $2;
3730 p->max_numparam = ORDINAL_PARAM;
3731#endif
3732 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
3733 }
3734 | f_args
3735 {
3736#if 0
3737 if (!args_info_empty_p($1->nd_ainfo))
3738 p->max_numparam = ORDINAL_PARAM;
3739#endif
3740 $$ = $1;
3741 }
3742 ;
3743
3744lambda_body : tLAMBEG compstmt '}'
3745 {
3746 token_info_pop(p, "}", &@3);
3747 $$ = $2;
3748 }
3749 | keyword_do_LAMBDA bodystmt k_end
3750 {
3751 $$ = $2;
3752 }
3753 ;
3754
3755do_block : k_do_block do_body k_end
3756 {
3757 $$ = $2;
3758#if 0
3759 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3760 nd_set_line($$, @1.end_pos.lineno);
3761#endif
3762 }
3763 ;
3764
3765block_call : command do_block
3766 {
3767#if 0
3768 if (nd_type($1) == NODE_YIELD) {
3769 compile_error(p, "block given to yield");
3770 }
3771 else {
3772 block_dup_check(p, $1->nd_args, $2);
3773 }
3774 $$ = method_add_block(p, $1, $2, &@$);
3775 fixpos($$, $1);
3776#endif
3777 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
3778 }
3779 | block_call call_op2 operation2 opt_paren_args
3780 {
3781#if 0
3782 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3783#endif
3784 {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;}
3785 }
3786 | block_call call_op2 operation2 opt_paren_args brace_block
3787 {
3788#if 0
3789 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3790#endif
3791 {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;}
3792 }
3793 | block_call call_op2 operation2 command_args do_block
3794 {
3795#if 0
3796 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3797#endif
3798 {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;}
3799 }
3800 ;
3801
3802method_call : fcall paren_args
3803 {
3804#if 0
3805 $$ = $1;
3806 $$->nd_args = $2;
3807 nd_set_last_loc($1, @2.end_pos);
3808#endif
3809 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(fcall,v1);v3=v2;v4=$2;v5=dispatch2(method_add_arg,v3,v4);$$=v5;}
3810 }
3811 | primary_value call_op operation2 opt_paren_args
3812 {
3813#if 0
3814 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3815 nd_set_line($$, @3.end_pos.lineno);
3816#endif
3817 {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;}
3818 }
3819 | primary_value tCOLON2 operation2 paren_args
3820 {
3821#if 0
3822 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
3823 nd_set_line($$, @3.end_pos.lineno);
3824#endif
3825 {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;}
3826 }
3827 | primary_value tCOLON2 operation3
3828 {
3829#if 0
3830 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
3831#endif
3832 {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(call,v1,v2,v3);$$=v4;}
3833 }
3834 | primary_value call_op paren_args
3835 {
3836#if 0
3837 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
3838 nd_set_line($$, @2.end_pos.lineno);
3839#endif
3840 {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;}
3841 }
3842 | primary_value tCOLON2 paren_args
3843 {
3844#if 0
3845 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
3846 nd_set_line($$, @2.end_pos.lineno);
3847#endif
3848 {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;}
3849 }
3850 | keyword_super paren_args
3851 {
3852#if 0
3853 $$ = NEW_SUPER($2, &@$);
3854#endif
3855 {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
3856 }
3857 | keyword_super
3858 {
3859#if 0
3860 $$ = NEW_ZSUPER(&@$);
3861#endif
3862 {VALUE v1;v1=dispatch0(zsuper);$$=v1;}
3863 }
3864 | primary_value '[' opt_call_args rbracket
3865 {
3866#if 0
3867 if ($1 && nd_type($1) == NODE_SELF)
3868 $$ = NEW_FCALL(tAREF, $3, &@$);
3869 else
3870 $$ = NEW_CALL($1, tAREF, $3, &@$);
3871 fixpos($$, $1);
3872#endif
3873 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref,v1,v2);$$=v3;}
3874 }
3875 ;
3876
3877brace_block : '{' brace_body '}'
3878 {
3879 $$ = $2;
3880#if 0
3881 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3882 nd_set_line($$, @1.end_pos.lineno);
3883#endif
3884 }
3885 | k_do do_body k_end
3886 {
3887 $$ = $2;
3888#if 0
3889 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3890 nd_set_line($$, @1.end_pos.lineno);
3891#endif
3892 }
3893 ;
3894
3895brace_body : {$<vars>$ = dyna_push(p);}
3896 {
3897 $<num>$ = p->max_numparam;
3898 p->max_numparam = 0;
3899 }
3900 {
3901 $<node>$ = numparam_push(p);
3902 }
3903 opt_block_param compstmt
3904 {
3905 int max_numparam = p->max_numparam;
3906 p->max_numparam = $<num>2;
3907 $4 = args_with_numbered(p, $4, max_numparam);
3908#if 0
3909 $$ = NEW_ITER($4, $5, &@$);
3910#endif
3911 {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(brace_block,v1,v2);$$=v3;}
3912 numparam_pop(p, $<node>3);
3913 dyna_pop(p, $<vars>1);
3914 }
3915 ;
3916
3917do_body : {$<vars>$ = dyna_push(p);}
3918 {
3919 $<num>$ = p->max_numparam;
3920 p->max_numparam = 0;
3921 }
3922 {
3923 $<node>$ = numparam_push(p);
3924 CMDARG_PUSH(0);
3925 }
3926 opt_block_param bodystmt
3927 {
3928 int max_numparam = p->max_numparam;
3929 p->max_numparam = $<num>2;
3930 $4 = args_with_numbered(p, $4, max_numparam);
3931#if 0
3932 $$ = NEW_ITER($4, $5, &@$);
3933#endif
3934 {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(do_block,v1,v2);$$=v3;}
3935 CMDARG_POP();
3936 numparam_pop(p, $<node>3);
3937 dyna_pop(p, $<vars>1);
3938 }
3939 ;
3940
3941case_args : arg_value
3942 {
3943#if 0
3944 check_literal_when(p, $1, &@1);
3945 $$ = NEW_LIST($1, &@$);
3946#endif
3947 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
3948 }
3949 | tSTAR arg_value
3950 {
3951#if 0
3952 $$ = NEW_SPLAT($2, &@$);
3953#endif
3954 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
3955 }
3956 | case_args ',' arg_value
3957 {
3958#if 0
3959 check_literal_when(p, $3, &@3);
3960 $$ = last_arg_append(p, $1, $3, &@$);
3961#endif
3962 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
3963 }
3964 | case_args ',' tSTAR arg_value
3965 {
3966#if 0
3967 $$ = rest_arg_append(p, $1, $4, &@$);
3968#endif
3969 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
3970 }
3971 ;
3972
3973case_body : k_when case_args then
3974 compstmt
3975 cases
3976 {
3977#if 0
3978 $$ = NEW_WHEN($2, $4, $5, &@$);
3979 fixpos($$, $2);
3980#endif
3981 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(when,v1,v2,v3);$$=v4;}
3982 }
3983 ;
3984
3985cases : opt_else
3986 | case_body
3987 ;
3988
3989p_case_body : keyword_in
3990 {
3991 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
3992 p->command_start = FALSE;
3993 $<ctxt>1 = p->ctxt;
3994 p->ctxt.in_kwarg = 1;
3995 $<tbl>$ = push_pvtbl(p);
3996 }
3997 {
3998 $<tbl>$ = push_pktbl(p);
3999 }
4000 p_top_expr then
4001 {
4002 pop_pktbl(p, $<tbl>3);
4003 pop_pvtbl(p, $<tbl>2);
4004 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4005 }
4006 compstmt
4007 p_cases
4008 {
4009#if 0
4010 $$ = NEW_IN($4, $7, $8, &@$);
4011#endif
4012 {VALUE v1,v2,v3,v4;v1=$4;v2=$7;v3=escape_Qundef($8);v4=dispatch3(in,v1,v2,v3);$$=v4;}
4013 }
4014 ;
4015
4016p_cases : opt_else
4017 | p_case_body
4018 ;
4019
4020p_top_expr : p_top_expr_body
4021 | p_top_expr_body modifier_if expr_value
4022 {
4023#if 0
4024 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
4025 fixpos($$, $3);
4026#endif
4027 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
4028 }
4029 | p_top_expr_body modifier_unless expr_value
4030 {
4031#if 0
4032 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
4033 fixpos($$, $3);
4034#endif
4035 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
4036 }
4037 ;
4038
4039p_top_expr_body : p_expr
4040 | p_expr ','
4041 {
4042 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4043 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
4044 }
4045 | p_expr ',' p_args
4046 {
4047 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
4048#if 0
4049 nd_set_first_loc($$, @1.beg_pos);
4050#endif
4051
4052 }
4053 | p_find
4054 {
4055 $$ = new_find_pattern(p, Qnone, $1, &@$);
4056 }
4057 | p_args_tail
4058 {
4059 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
4060 }
4061 | p_kwargs
4062 {
4063 $$ = new_hash_pattern(p, Qnone, $1, &@$);
4064 }
4065 ;
4066
4067p_expr : p_as
4068 ;
4069
4070p_as : p_expr tASSOC p_variable
4071 {
4072#if 0
4073 NODE *n = NEW_LIST($1, &@$);
4074 n = list_append(p, n, $3);
4075 $$ = new_hash(p, n, &@$);
4076#endif
4077 {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(id_assoc);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
4078 }
4079 | p_alt
4080 ;
4081
4082p_alt : p_alt '|' p_expr_basic
4083 {
4084#if 0
4085 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
4086#endif
4087 {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(idOr);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
4088 }
4089 | p_expr_basic
4090 ;
4091
4092p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
4093p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
4094
4095p_expr_basic : p_value
4096 | p_const p_lparen p_args rparen
4097 {
4098 pop_pktbl(p, $<tbl>2);
4099 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4100#if 0
4101 nd_set_first_loc($$, @1.beg_pos);
4102#endif
4103
4104 }
4105 | p_const p_lparen p_find rparen
4106 {
4107 pop_pktbl(p, $<tbl>2);
4108 $$ = new_find_pattern(p, $1, $3, &@$);
4109#if 0
4110 nd_set_first_loc($$, @1.beg_pos);
4111#endif
4112
4113 }
4114 | p_const p_lparen p_kwargs rparen
4115 {
4116 pop_pktbl(p, $<tbl>2);
4117 $$ = new_hash_pattern(p, $1, $3, &@$);
4118#if 0
4119 nd_set_first_loc($$, @1.beg_pos);
4120#endif
4121
4122 }
4123 | p_const '(' rparen
4124 {
4125 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4126 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4127 }
4128 | p_const p_lbracket p_args rbracket
4129 {
4130 pop_pktbl(p, $<tbl>2);
4131 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4132#if 0
4133 nd_set_first_loc($$, @1.beg_pos);
4134#endif
4135
4136 }
4137 | p_const p_lbracket p_find rbracket
4138 {
4139 pop_pktbl(p, $<tbl>2);
4140 $$ = new_find_pattern(p, $1, $3, &@$);
4141#if 0
4142 nd_set_first_loc($$, @1.beg_pos);
4143#endif
4144
4145 }
4146 | p_const p_lbracket p_kwargs rbracket
4147 {
4148 pop_pktbl(p, $<tbl>2);
4149 $$ = new_hash_pattern(p, $1, $3, &@$);
4150#if 0
4151 nd_set_first_loc($$, @1.beg_pos);
4152#endif
4153
4154 }
4155 | p_const '[' rbracket
4156 {
4157 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4158 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4159 }
4160 | tLBRACK p_args rbracket
4161 {
4162 $$ = new_array_pattern(p, Qnone, Qnone, $2, &@$);
4163 }
4164 | tLBRACK p_find rbracket
4165 {
4166 $$ = new_find_pattern(p, Qnone, $2, &@$);
4167 }
4168 | tLBRACK rbracket
4169 {
4170 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4171 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
4172 }
4173 | tLBRACE
4174 {
4175 $<tbl>$ = push_pktbl(p);
4176 $<ctxt>1 = p->ctxt;
4177 p->ctxt.in_kwarg = 0;
4178 }
4179 p_kwargs rbrace
4180 {
4181 pop_pktbl(p, $<tbl>2);
4182 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4183 $$ = new_hash_pattern(p, Qnone, $3, &@$);
4184 }
4185 | tLBRACE rbrace
4186 {
4187 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
4188 $$ = new_hash_pattern(p, Qnone, $$, &@$);
4189 }
4190 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
4191 {
4192 pop_pktbl(p, $<tbl>2);
4193 $$ = $3;
4194 }
4195 ;
4196
4197p_args : p_expr
4198 {
4199#if 0
4200 NODE *pre_args = NEW_LIST($1, &@$);
4201 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4202#endif
4203 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
4204
4205 }
4206 | p_args_head
4207 {
4208 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4209 }
4210 | p_args_head p_arg
4211 {
4212#if 0
4213 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
4214#endif
4215 VALUE pre_args = rb_ary_concat($1, get_value($2));
4216 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4217
4218 }
4219 | p_args_head tSTAR tIDENTIFIER
4220 {
4221 $$ = new_array_pattern_tail(p, $1, 1, $3, Qnone, &@$);
4222 }
4223 | p_args_head tSTAR tIDENTIFIER ',' p_args_post
4224 {
4225 $$ = new_array_pattern_tail(p, $1, 1, $3, $5, &@$);
4226 }
4227 | p_args_head tSTAR
4228 {
4229 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4230 }
4231 | p_args_head tSTAR ',' p_args_post
4232 {
4233 $$ = new_array_pattern_tail(p, $1, 1, 0, $4, &@$);
4234 }
4235 | p_args_tail
4236 ;
4237
4238p_args_head : p_arg ','
4239 {
4240 $$ = $1;
4241 }
4242 | p_args_head p_arg ','
4243 {
4244#if 0
4245 $$ = list_concat($1, $2);
4246#endif
4247 $$=rb_ary_concat($1, get_value($2));
4248 }
4249 ;
4250
4251p_args_tail : p_rest
4252 {
4253 $$ = new_array_pattern_tail(p, Qnone, 1, $1, Qnone, &@$);
4254 }
4255 | p_rest ',' p_args_post
4256 {
4257 $$ = new_array_pattern_tail(p, Qnone, 1, $1, $3, &@$);
4258 }
4259 ;
4260
4261p_find : p_rest ',' p_args_post ',' p_rest
4262 {
4263 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
4264
4265 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL))
4266 rb_warn0L_experimental(nd_line($$), "Find pattern is experimental, and the behavior may change in future versions of Ruby!");
4267 }
4268 ;
4269
4270
4271p_rest : tSTAR tIDENTIFIER
4272 {
4273 $$ = $2;
4274 }
4275 | tSTAR
4276 {
4277 $$ = 0;
4278 }
4279 ;
4280
4281p_args_post : p_arg
4282 | p_args_post ',' p_arg
4283 {
4284#if 0
4285 $$ = list_concat($1, $3);
4286#endif
4287 $$=rb_ary_concat($1, get_value($3));
4288 }
4289 ;
4290
4291p_arg : p_expr
4292 {
4293#if 0
4294 $$ = NEW_LIST($1, &@$);
4295#endif
4296 $$=rb_ary_new_from_args(1, get_value($1));
4297 }
4298 ;
4299
4300p_kwargs : p_kwarg ',' p_any_kwrest
4301 {
4302 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4303 }
4304 | p_kwarg
4305 {
4306 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4307 }
4308 | p_kwarg ','
4309 {
4310 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4311 }
4312 | p_any_kwrest
4313 {
4314 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4315 }
4316 ;
4317
4318p_kwarg : p_kw
4319 {$$=rb_ary_new_from_args(1, $1);}
4320 | p_kwarg ',' p_kw
4321 {
4322#if 0
4323 $$ = list_concat($1, $3);
4324#endif
4325 $$=rb_ary_push($1, $3);
4326 }
4327 ;
4328
4329p_kw : p_kw_label p_expr
4330 {
4331 error_duplicate_pattern_key(p, get_id($1), &@1);
4332#if 0
4333 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), $2);
4334#endif
4335 $$=rb_ary_new_from_args(2, get_value($1), get_value($2));
4336 }
4337 | p_kw_label
4338 {
4339 error_duplicate_pattern_key(p, get_id($1), &@1);
4340 if ($1 && !is_local_id(get_id($1))) {
4341 yyerror1(&@1, "key must be valid as local variables");
4342 }
4343 error_duplicate_pattern_variable(p, get_id($1), &@1);
4344#if 0
4345 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4346#endif
4347 $$=rb_ary_new_from_args(2, get_value($1), Qnil);
4348 }
4349 ;
4350
4351p_kw_label : tLABEL
4352 | tSTRING_BEG string_contents tLABEL_END
4353 {
4354 YYLTYPE loc = code_loc_gen(&@1, &@3);
4355#if 0
4356 if (!$2 || nd_type($2) == NODE_STR) {
4357 NODE *node = dsym_node(p, $2, &loc);
4358 $$ = SYM2ID(node->nd_lit);
4359 }
4360#endif
4361 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4362 VALUE label = RNODE($2)->nd_cval;
4363 VALUE rval = RNODE($2)->nd_rval;
4364 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4365 RNODE($$)->nd_loc = loc;
4366 }
4367
4368 else {
4369 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4370 $$ = 0;
4371 }
4372 }
4373 ;
4374
4375p_kwrest : kwrest_mark tIDENTIFIER
4376 {
4377 $$ = $2;
4378 }
4379 | kwrest_mark
4380 {
4381 $$ = 0;
4382 }
4383 ;
4384
4385p_kwnorest : kwrest_mark keyword_nil
4386 {
4387 $$ = 0;
4388 }
4389 ;
4390
4391p_any_kwrest : p_kwrest
4392 | p_kwnorest {$$ = ID2VAL(idNil);}
4393 ;
4394
4395p_value : p_primitive
4396 | p_primitive tDOT2 p_primitive
4397 {
4398#if 0
4399 value_expr($1);
4400 value_expr($3);
4401 $$ = NEW_DOT2($1, $3, &@$);
4402#endif
4403 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
4404 }
4405 | p_primitive tDOT3 p_primitive
4406 {
4407#if 0
4408 value_expr($1);
4409 value_expr($3);
4410 $$ = NEW_DOT3($1, $3, &@$);
4411#endif
4412 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
4413 }
4414 | p_primitive tDOT2
4415 {
4416#if 0
4417 value_expr($1);
4418 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
4419#endif
4420 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
4421 }
4422 | p_primitive tDOT3
4423 {
4424#if 0
4425 value_expr($1);
4426 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
4427#endif
4428 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
4429 }
4430 | p_variable
4431 | p_var_ref
4432 | p_const
4433 | tBDOT2 p_primitive
4434 {
4435#if 0
4436 value_expr($2);
4437 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
4438#endif
4439 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
4440 }
4441 | tBDOT3 p_primitive
4442 {
4443#if 0
4444 value_expr($2);
4445 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
4446#endif
4447 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
4448 }
4449 ;
4450
4451p_primitive : literal
4452 | strings
4453 | xstring
4454 | regexp
4455 | words
4456 | qwords
4457 | symbols
4458 | qsymbols
4459 | keyword_variable
4460 {
4461#if 0
4462 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4463#endif
4464 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4465 }
4466 | lambda
4467 ;
4468
4469p_variable : tIDENTIFIER
4470 {
4471#if 0
4472 error_duplicate_pattern_variable(p, $1, &@1);
4473 $$ = assignable(p, $1, 0, &@$);
4474#endif
4475 $$=assignable(p, var_field(p, $1));
4476 }
4477 ;
4478
4479p_var_ref : '^' tIDENTIFIER
4480 {
4481#if 0
4482 NODE *n = gettable(p, $2, &@$);
4483 if (!(nd_type(n) == NODE_LVAR || nd_type(n) == NODE_DVAR)) {
4484 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4485 }
4486 $$ = n;
4487#endif
4488 {VALUE v1,v2;v1=$2;v2=dispatch1(var_ref,v1);$$=v2;}
4489 }
4490 ;
4491
4492p_const : tCOLON3 cname
4493 {
4494#if 0
4495 $$ = NEW_COLON3($2, &@$);
4496#endif
4497 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
4498 }
4499 | p_const tCOLON2 cname
4500 {
4501#if 0
4502 $$ = NEW_COLON2($1, $3, &@$);
4503#endif
4504 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
4505 }
4506 | tCONSTANT
4507 {
4508#if 0
4509 $$ = gettable(p, $1, &@$);
4510#endif
4511 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4512 }
4513 ;
4514
4515opt_rescue : k_rescue exc_list exc_var then
4516 compstmt
4517 opt_rescue
4518 {
4519#if 0
4520 $$ = NEW_RESBODY($2,
4521 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), NO_LEX_CTXT, &@3), $5) : $5,
4522 $6, &@$);
4523 fixpos($$, $2?$2:$5);
4524#endif
4525 {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;}
4526 }
4527 | none
4528 ;
4529
4530exc_list : arg_value
4531 {
4532#if 0
4533 $$ = NEW_LIST($1, &@$);
4534#endif
4535 $$=rb_ary_new3(1, get_value($1));
4536 }
4537 | mrhs
4538 {
4539#if 0
4540 if (!($$ = splat_array($1))) $$ = $1;
4541#endif
4542 $$=$1;
4543 }
4544 | none
4545 ;
4546
4547exc_var : tASSOC lhs
4548 {
4549 $$ = $2;
4550 }
4551 | none
4552 ;
4553
4554opt_ensure : k_ensure compstmt
4555 {
4556#if 0
4557 $$ = $2;
4558#endif
4559 {VALUE v1,v2;v1=$2;v2=dispatch1(ensure,v1);$$=v2;}
4560 }
4561 | none
4562 ;
4563
4564literal : numeric
4565 | symbol
4566 ;
4567
4568strings : string
4569 {
4570#if 0
4571 NODE *node = $1;
4572 if (!node) {
4573 node = NEW_STR(STR_NEW0(), &@$);
4574 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
4575 }
4576 else {
4577 node = evstr2dstr(p, node);
4578 }
4579 $$ = node;
4580#endif
4581 $$=$1;
4582 }
4583 ;
4584
4585string : tCHAR
4586 | string1
4587 | string string1
4588 {
4589#if 0
4590 $$ = literal_concat(p, $1, $2, &@$);
4591#endif
4592 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_concat,v1,v2);$$=v3;}
4593 }
4594 ;
4595
4596string1 : tSTRING_BEG string_contents tSTRING_END
4597 {
4598#if 0
4599 $$ = heredoc_dedent(p, $2);
4600 if ($$) nd_set_loc($$, &@$);
4601#endif
4602 {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(string_literal,v1);$$=v2;}
4603 }
4604 ;
4605
4606xstring : tXSTRING_BEG xstring_contents tSTRING_END
4607 {
4608#if 0
4609 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4610#endif
4611 {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(xstring_literal,v1);$$=v2;}
4612 }
4613 ;
4614
4615regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4616 {
4617 $$ = new_regexp(p, $2, $3, &@$);
4618 }
4619 ;
4620
4621words : tWORDS_BEG ' ' word_list tSTRING_END
4622 {
4623#if 0
4624 $$ = make_list($3, &@$);
4625#endif
4626 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4627 }
4628 ;
4629
4630word_list : /* none */
4631 {
4632#if 0
4633 $$ = 0;
4634#endif
4635 {VALUE v1;v1=dispatch0(words_new);$$=v1;}
4636 }
4637 | word_list word ' '
4638 {
4639#if 0
4640 $$ = list_append(p, $1, evstr2dstr(p, $2));
4641#endif
4642 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(words_add,v1,v2);$$=v3;}
4643 }
4644 ;
4645
4646word : string_content
4647 {{VALUE v1,v2,v3,v4;v1=dispatch0(word_new);v2=v1;v3=$1;v4=dispatch2(word_add,v2,v3);$$=v4;}}
4648 | word string_content
4649 {
4650#if 0
4651 $$ = literal_concat(p, $1, $2, &@$);
4652#endif
4653 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(word_add,v1,v2);$$=v3;}
4654 }
4655 ;
4656
4657symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4658 {
4659#if 0
4660 $$ = make_list($3, &@$);
4661#endif
4662 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4663 }
4664 ;
4665
4666symbol_list : /* none */
4667 {
4668#if 0
4669 $$ = 0;
4670#endif
4671 {VALUE v1;v1=dispatch0(symbols_new);$$=v1;}
4672 }
4673 | symbol_list word ' '
4674 {
4675#if 0
4676 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4677#endif
4678 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(symbols_add,v1,v2);$$=v3;}
4679 }
4680 ;
4681
4682qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4683 {
4684#if 0
4685 $$ = make_list($3, &@$);
4686#endif
4687 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4688 }
4689 ;
4690
4691qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4692 {
4693#if 0
4694 $$ = make_list($3, &@$);
4695#endif
4696 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4697 }
4698 ;
4699
4700qword_list : /* none */
4701 {
4702#if 0
4703 $$ = 0;
4704#endif
4705 {VALUE v1;v1=dispatch0(qwords_new);$$=v1;}
4706 }
4707 | qword_list tSTRING_CONTENT ' '
4708 {
4709#if 0
4710 $$ = list_append(p, $1, $2);
4711#endif
4712 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qwords_add,v1,v2);$$=v3;}
4713 }
4714 ;
4715
4716qsym_list : /* none */
4717 {
4718#if 0
4719 $$ = 0;
4720#endif
4721 {VALUE v1;v1=dispatch0(qsymbols_new);$$=v1;}
4722 }
4723 | qsym_list tSTRING_CONTENT ' '
4724 {
4725#if 0
4726 $$ = symbol_append(p, $1, $2);
4727#endif
4728 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qsymbols_add,v1,v2);$$=v3;}
4729 }
4730 ;
4731
4732string_contents : /* none */
4733 {
4734#if 0
4735 $$ = 0;
4736#endif
4737 {VALUE v1;v1=dispatch0(string_content);$$=v1;}
4738#if 0
4739#endif
4740 $$ = ripper_new_yylval(p, 0, $$, 0);
4741
4742 }
4743 | string_contents string_content
4744 {
4745#if 0
4746 $$ = literal_concat(p, $1, $2, &@$);
4747#endif
4748 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_add,v1,v2);$$=v3;}
4749#if 0
4750#endif
4751 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
4752 !RNODE($1)->nd_cval) {
4753 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
4754 RNODE($1)->nd_rval = add_mark_object(p, $$);
4755 $$ = $1;
4756 }
4757
4758 }
4759 ;
4760
4761xstring_contents: /* none */
4762 {
4763#if 0
4764 $$ = 0;
4765#endif
4766 {VALUE v1;v1=dispatch0(xstring_new);$$=v1;}
4767 }
4768 | xstring_contents string_content
4769 {
4770#if 0
4771 $$ = literal_concat(p, $1, $2, &@$);
4772#endif
4773 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(xstring_add,v1,v2);$$=v3;}
4774 }
4775 ;
4776
4777regexp_contents: /* none */
4778 {
4779#if 0
4780 $$ = 0;
4781#endif
4782 {VALUE v1;v1=dispatch0(regexp_new);$$=v1;}
4783#if 0
4784#endif
4785 $$ = ripper_new_yylval(p, 0, $$, 0);
4786
4787 }
4788 | regexp_contents string_content
4789 {
4790#if 0
4791 NODE *head = $1, *tail = $2;
4792 if (!head) {
4793 $$ = tail;
4794 }
4795 else if (!tail) {
4796 $$ = head;
4797 }
4798 else {
4799 switch (nd_type(head)) {
4800 case NODE_STR:
4801 nd_set_type(head, NODE_DSTR);
4802 break;
4803 case NODE_DSTR:
4804 break;
4805 default:
4806 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
4807 break;
4808 }
4809 $$ = list_append(p, head, tail);
4810 }
4811#endif
4812 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4813 if (ripper_is_node_yylval(n1)) {
4814 s1 = RNODE(n1)->nd_cval;
4815 n1 = RNODE(n1)->nd_rval;
4816 }
4817 if (ripper_is_node_yylval(n2)) {
4818 s2 = RNODE(n2)->nd_cval;
4819 n2 = RNODE(n2)->nd_rval;
4820 }
4821 $$ = dispatch2(regexp_add, n1, n2);
4822 if (!s1 && s2) {
4823 $$ = ripper_new_yylval(p, 0, $$, s2);
4824 }
4825
4826 }
4827 ;
4828
4829string_content : tSTRING_CONTENT
4830 {$$=ripper_new_yylval(p, 0, get_value($1), $1);}
4831 | tSTRING_DVAR
4832 {
4833 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
4834 $<strterm>$ = p->lex.strterm;
4835 p->lex.strterm = 0;
4836 SET_LEX_STATE(EXPR_BEG);
4837 }
4838 string_dvar
4839 {
4840 p->lex.strterm = $<strterm>2;
4841#if 0
4842 $$ = NEW_EVSTR($3, &@$);
4843 nd_set_line($$, @3.end_pos.lineno);
4844#endif
4845 {VALUE v1,v2;v1=$3;v2=dispatch1(string_dvar,v1);$$=v2;}
4846 }
4847 | tSTRING_DBEG
4848 {
4849 CMDARG_PUSH(0);
4850 COND_PUSH(0);
4851 }
4852 {
4853 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
4854 $<strterm>$ = p->lex.strterm;
4855 p->lex.strterm = 0;
4856 }
4857 {
4858 $<num>$ = p->lex.state;
4859 SET_LEX_STATE(EXPR_BEG);
4860 }
4861 {
4862 $<num>$ = p->lex.brace_nest;
4863 p->lex.brace_nest = 0;
4864 }
4865 {
4866 $<num>$ = p->heredoc_indent;
4867 p->heredoc_indent = 0;
4868 }
4869 compstmt tSTRING_DEND
4870 {
4871 COND_POP();
4872 CMDARG_POP();
4873 p->lex.strterm = $<strterm>3;
4874 SET_LEX_STATE($<num>4);
4875 p->lex.brace_nest = $<num>5;
4876 p->heredoc_indent = $<num>6;
4877 p->heredoc_line_indent = -1;
4878#if 0
4879 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4880 $$ = new_evstr(p, $7, &@$);
4881#endif
4882 {VALUE v1,v2;v1=$7;v2=dispatch1(string_embexpr,v1);$$=v2;}
4883 }
4884 ;
4885
4886string_dvar : tGVAR
4887 {
4888#if 0
4889 $$ = NEW_GVAR($1, &@$);
4890#endif
4891 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4892 }
4893 | tIVAR
4894 {
4895#if 0
4896 $$ = NEW_IVAR($1, &@$);
4897#endif
4898 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4899 }
4900 | tCVAR
4901 {
4902#if 0
4903 $$ = NEW_CVAR($1, &@$);
4904#endif
4905 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4906 }
4907 | backref
4908 ;
4909
4910symbol : ssym
4911 | dsym
4912 ;
4913
4914ssym : tSYMBEG sym
4915 {
4916 SET_LEX_STATE(EXPR_END);
4917#if 0
4918 $$ = NEW_LIT(ID2SYM($2), &@$);
4919#endif
4920 {VALUE v1,v2,v3,v4;v1=$2;v2=dispatch1(symbol,v1);v3=v2;v4=dispatch1(symbol_literal,v3);$$=v4;}
4921 }
4922 ;
4923
4924sym : fname
4925 | tIVAR
4926 | tGVAR
4927 | tCVAR
4928 ;
4929
4930dsym : tSYMBEG string_contents tSTRING_END
4931 {
4932 SET_LEX_STATE(EXPR_END);
4933#if 0
4934 $$ = dsym_node(p, $2, &@$);
4935#endif
4936 {VALUE v1,v2;v1=$2;v2=dispatch1(dyna_symbol,v1);$$=v2;}
4937 }
4938 ;
4939
4940numeric : simple_numeric
4941 | tUMINUS_NUM simple_numeric %prec tLOWEST
4942 {
4943#if 0
4944 $$ = $2;
4945 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
4946#endif
4947 {VALUE v1,v2,v3;v1=ID2VAL(idUMinus);v2=$2;v3=dispatch2(unary,v1,v2);$$=v3;}
4948 }
4949 ;
4950
4951simple_numeric : tINTEGER
4952 | tFLOAT
4953 | tRATIONAL
4954 | tIMAGINARY
4955 ;
4956
4957user_variable : tIDENTIFIER
4958 | tIVAR
4959 | tGVAR
4960 | tCONSTANT
4961 | tCVAR
4962 ;
4963
4964keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
4965 | keyword_self {$$ = KWD2EID(self, $1);}
4966 | keyword_true {$$ = KWD2EID(true, $1);}
4967 | keyword_false {$$ = KWD2EID(false, $1);}
4968 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
4969 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
4970 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
4971 ;
4972
4973var_ref : user_variable
4974 {
4975#if 0
4976 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4977#endif
4978 if (id_is_var(p, get_id($1))) {
4979 $$ = dispatch1(var_ref, $1);
4980 }
4981 else {
4982 $$ = dispatch1(vcall, $1);
4983 }
4984
4985 }
4986 | keyword_variable
4987 {
4988#if 0
4989 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4990#endif
4991 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4992 }
4993 ;
4994
4995var_lhs : user_variable
4996 {
4997#if 0
4998 $$ = assignable(p, $1, 0, &@$);
4999#endif
5000 $$=assignable(p, var_field(p, $1));
5001 }
5002 | keyword_variable
5003 {
5004#if 0
5005 $$ = assignable(p, $1, 0, &@$);
5006#endif
5007 $$=assignable(p, var_field(p, $1));
5008 }
5009 ;
5010
5011backref : tNTH_REF
5012 | tBACK_REF
5013 ;
5014
5015superclass : '<'
5016 {
5017 SET_LEX_STATE(EXPR_BEG);
5018 p->command_start = TRUE;
5019 }
5020 expr_value term
5021 {
5022 $$ = $3;
5023 }
5024 | /* none */
5025 {
5026#if 0
5027 $$ = 0;
5028#endif
5029 $$=Qnil;
5030 }
5031 ;
5032
5033f_opt_paren_args: f_paren_args | none;
5034
5035f_paren_args : '(' f_args rparen
5036 {
5037#if 0
5038 $$ = $2;
5039#endif
5040 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
5041 SET_LEX_STATE(EXPR_BEG);
5042 p->command_start = TRUE;
5043 }
5044 | '(' f_arg ',' args_forward rparen
5045 {
5046 add_forwarding_args(p);
5047#if 0
5048 $$ = new_args_forward_def(p, $2, &@$);
5049#endif
5050 {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9,v10;v1=$2;v2=Qnone;v3=$4;v4=Qnone;v5=Qnone;v6=Qnone;v7=Qnone;v8=dispatch7(params,v1,v2,v3,v4,v5,v6,v7);v9=v8;v10=dispatch1(paren,v9);$$=v10;}
5051 SET_LEX_STATE(EXPR_BEG);
5052 p->command_start = TRUE;
5053 }
5054 | '(' args_forward rparen
5055 {
5056 add_forwarding_args(p);
5057#if 0
5058 $$ = new_args_forward_def(p, 0, &@$);
5059#endif
5060 {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9,v10;v1=Qnone;v2=Qnone;v3=$2;v4=Qnone;v5=Qnone;v6=Qnone;v7=Qnone;v8=dispatch7(params,v1,v2,v3,v4,v5,v6,v7);v9=v8;v10=dispatch1(paren,v9);$$=v10;}
5061 SET_LEX_STATE(EXPR_BEG);
5062 p->command_start = TRUE;
5063 }
5064 ;
5065
5066f_arglist : f_paren_args
5067 | {
5068 $<ctxt>$ = p->ctxt;
5069 p->ctxt.in_kwarg = 1;
5070 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
5071 }
5072 f_args term
5073 {
5074 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
5075 $$ = $2;
5076 SET_LEX_STATE(EXPR_BEG);
5077 p->command_start = TRUE;
5078 }
5079 ;
5080
5081args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
5082 {
5083 $$ = new_args_tail(p, $1, $3, $4, &@3);
5084 }
5085 | f_kwarg opt_f_block_arg
5086 {
5087 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
5088 }
5089 | f_any_kwrest opt_f_block_arg
5090 {
5091 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
5092 }
5093 | f_block_arg
5094 {
5095 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
5096 }
5097 ;
5098
5099opt_args_tail : ',' args_tail
5100 {
5101 $$ = $2;
5102 }
5103 | /* none */
5104 {
5105 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5106 }
5107 ;
5108
5109f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
5110 {
5111 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
5112 }
5113 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5114 {
5115 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
5116 }
5117 | f_arg ',' f_optarg opt_args_tail
5118 {
5119 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
5120 }
5121 | f_arg ',' f_optarg ',' f_arg opt_args_tail
5122 {
5123 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
5124 }
5125 | f_arg ',' f_rest_arg opt_args_tail
5126 {
5127 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
5128 }
5129 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
5130 {
5131 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
5132 }
5133 | f_arg opt_args_tail
5134 {
5135 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
5136 }
5137 | f_optarg ',' f_rest_arg opt_args_tail
5138 {
5139 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
5140 }
5141 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5142 {
5143 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
5144 }
5145 | f_optarg opt_args_tail
5146 {
5147 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
5148 }
5149 | f_optarg ',' f_arg opt_args_tail
5150 {
5151 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
5152 }
5153 | f_rest_arg opt_args_tail
5154 {
5155 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
5156 }
5157 | f_rest_arg ',' f_arg opt_args_tail
5158 {
5159 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
5160 }
5161 | args_tail
5162 {
5163 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
5164 }
5165 | /* none */
5166 {
5167 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5168 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5169 }
5170 ;
5171
5172args_forward : tBDOT3
5173 {
5174#if 0
5175 $$ = idDot3;
5176#endif
5177 {VALUE v1;v1=dispatch0(args_forward);$$=v1;}
5178 }
5179 ;
5180
5181f_bad_arg : tCONSTANT
5182 {
5183 static const char mesg[] = "formal argument cannot be a constant";
5184#if 0
5185 yyerror1(&@1, mesg);
5186 $$ = 0;
5187#endif
5188 {VALUE v1,v2,v3;v1=ERR_MESG();v2=$1;v3=dispatch2(param_error,v1,v2);$$=v3;}ripper_error(p);
5189 }
5190 | tIVAR
5191 {
5192 static const char mesg[] = "formal argument cannot be an instance variable";
5193#if 0
5194 yyerror1(&@1, mesg);
5195 $$ = 0;
5196#endif
5197 {VALUE v1,v2,v3;v1=ERR_MESG();v2=$1;v3=dispatch2(param_error,v1,v2);$$=v3;}ripper_error(p);
5198 }
5199 | tGVAR
5200 {
5201 static const char mesg[] = "formal argument cannot be a global variable";
5202#if 0
5203 yyerror1(&@1, mesg);
5204 $$ = 0;
5205#endif
5206 {VALUE v1,v2,v3;v1=ERR_MESG();v2=$1;v3=dispatch2(param_error,v1,v2);$$=v3;}ripper_error(p);
5207 }
5208 | tCVAR
5209 {
5210 static const char mesg[] = "formal argument cannot be a class variable";
5211#if 0
5212 yyerror1(&@1, mesg);
5213 $$ = 0;
5214#endif
5215 {VALUE v1,v2,v3;v1=ERR_MESG();v2=$1;v3=dispatch2(param_error,v1,v2);$$=v3;}ripper_error(p);
5216 }
5217 ;
5218
5219f_norm_arg : f_bad_arg
5220 | tIDENTIFIER
5221 {
5222 formal_argument(p, $1);
5223 p->max_numparam = ORDINAL_PARAM;
5224 $$ = $1;
5225 }
5226 ;
5227
5228f_arg_asgn : f_norm_arg
5229 {
5230 ID id = get_id($1);
5231 arg_var(p, id);
5232 p->cur_arg = id;
5233 $$ = $1;
5234 }
5235 ;
5236
5237f_arg_item : f_arg_asgn
5238 {
5239 p->cur_arg = 0;
5240#if 0
5241 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5242#endif
5243 $$=get_value($1);
5244 }
5245 | tLPAREN f_margs rparen
5246 {
5247#if 0
5248 ID tid = internal_id(p);
5249 YYLTYPE loc;
5250 loc.beg_pos = @2.beg_pos;
5251 loc.end_pos = @2.beg_pos;
5252 arg_var(p, tid);
5253 if (dyna_in_block(p)) {
5254 $2->nd_value = NEW_DVAR(tid, &loc);
5255 }
5256 else {
5257 $2->nd_value = NEW_LVAR(tid, &loc);
5258 }
5259 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5260 $$->nd_next = $2;
5261#endif
5262 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
5263 }
5264 ;
5265
5266f_arg : f_arg_item
5267 {$$=rb_ary_new3(1, get_value($1));}
5268 | f_arg ',' f_arg_item
5269 {
5270#if 0
5271 $$ = $1;
5272 $$->nd_plen++;
5273 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5274 rb_discard_node(p, $3);
5275#endif
5276 $$=rb_ary_push($1, get_value($3));
5277 }
5278 ;
5279
5280
5281f_label : tLABEL
5282 {
5283 arg_var(p, formal_argument(p, $1));
5284 p->cur_arg = get_id($1);
5285 p->max_numparam = ORDINAL_PARAM;
5286 $$ = $1;
5287 }
5288 ;
5289
5290f_kw : f_label arg_value
5291 {
5292 p->cur_arg = 0;
5293#if 0
5294 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5295#endif
5296 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5297 }
5298 | f_label
5299 {
5300 p->cur_arg = 0;
5301#if 0
5302 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5303#endif
5304 $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5305 }
5306 ;
5307
5308f_block_kw : f_label primary_value
5309 {
5310#if 0
5311 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5312#endif
5313 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5314 }
5315 | f_label
5316 {
5317#if 0
5318 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5319#endif
5320 $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5321 }
5322 ;
5323
5324f_block_kwarg : f_block_kw
5325 {
5326#if 0
5327 $$ = $1;
5328#endif
5329 $$=rb_ary_new3(1, get_value($1));
5330 }
5331 | f_block_kwarg ',' f_block_kw
5332 {
5333#if 0
5334 $$ = kwd_append($1, $3);
5335#endif
5336 $$=rb_ary_push($1, get_value($3));
5337 }
5338 ;
5339
5340
5341f_kwarg : f_kw
5342 {
5343#if 0
5344 $$ = $1;
5345#endif
5346 $$=rb_ary_new3(1, get_value($1));
5347 }
5348 | f_kwarg ',' f_kw
5349 {
5350#if 0
5351 $$ = kwd_append($1, $3);
5352#endif
5353 $$=rb_ary_push($1, get_value($3));
5354 }
5355 ;
5356
5357kwrest_mark : tPOW
5358 | tDSTAR
5359 ;
5360
5361f_no_kwarg : kwrest_mark keyword_nil
5362 {
5363#if 0
5364#endif
5365 {VALUE v1,v2;v1=Qnil;v2=dispatch1(nokw_param,v1);$$=v2;}
5366 }
5367 ;
5368
5369f_kwrest : kwrest_mark tIDENTIFIER
5370 {
5371 arg_var(p, shadowing_lvar(p, get_id($2)));
5372#if 0
5373 $$ = $2;
5374#endif
5375 {VALUE v1,v2;v1=$2;v2=dispatch1(kwrest_param,v1);$$=v2;}
5376 }
5377 | kwrest_mark
5378 {
5379#if 0
5380 $$ = internal_id(p);
5381 arg_var(p, $$);
5382#endif
5383 {VALUE v1,v2;v1=Qnil;v2=dispatch1(kwrest_param,v1);$$=v2;}
5384 }
5385 ;
5386
5387f_opt : f_arg_asgn '=' arg_value
5388 {
5389 p->cur_arg = 0;
5390#if 0
5391 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5392#endif
5393 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5394 }
5395 ;
5396
5397f_block_opt : f_arg_asgn '=' primary_value
5398 {
5399 p->cur_arg = 0;
5400#if 0
5401 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5402#endif
5403 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5404 }
5405 ;
5406
5407f_block_optarg : f_block_opt
5408 {
5409#if 0
5410 $$ = $1;
5411#endif
5412 $$=rb_ary_new3(1, get_value($1));
5413 }
5414 | f_block_optarg ',' f_block_opt
5415 {
5416#if 0
5417 $$ = opt_arg_append($1, $3);
5418#endif
5419 $$=rb_ary_push($1, get_value($3));
5420 }
5421 ;
5422
5423f_optarg : f_opt
5424 {
5425#if 0
5426 $$ = $1;
5427#endif
5428 $$=rb_ary_new3(1, get_value($1));
5429 }
5430 | f_optarg ',' f_opt
5431 {
5432#if 0
5433 $$ = opt_arg_append($1, $3);
5434#endif
5435 $$=rb_ary_push($1, get_value($3));
5436 }
5437 ;
5438
5439restarg_mark : '*'
5440 | tSTAR
5441 ;
5442
5443f_rest_arg : restarg_mark tIDENTIFIER
5444 {
5445 arg_var(p, shadowing_lvar(p, get_id($2)));
5446#if 0
5447 $$ = $2;
5448#endif
5449 {VALUE v1,v2;v1=$2;v2=dispatch1(rest_param,v1);$$=v2;}
5450 }
5451 | restarg_mark
5452 {
5453#if 0
5454 $$ = internal_id(p);
5455 arg_var(p, $$);
5456#endif
5457 {VALUE v1,v2;v1=Qnil;v2=dispatch1(rest_param,v1);$$=v2;}
5458 }
5459 ;
5460
5461blkarg_mark : '&'
5462 | tAMPER
5463 ;
5464
5465f_block_arg : blkarg_mark tIDENTIFIER
5466 {
5467 arg_var(p, shadowing_lvar(p, get_id($2)));
5468#if 0
5469 $$ = $2;
5470#endif
5471 {VALUE v1,v2;v1=$2;v2=dispatch1(blockarg,v1);$$=v2;}
5472 }
5473 ;
5474
5475opt_f_block_arg : ',' f_block_arg
5476 {
5477 $$ = $2;
5478 }
5479 | none
5480 {
5481 $$ = Qnull;
5482 }
5483 ;
5484
5485singleton : var_ref
5486 {
5487 value_expr($1);
5488 $$ = $1;
5489 }
5490 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5491 {
5492#if 0
5493 switch (nd_type($3)) {
5494 case NODE_STR:
5495 case NODE_DSTR:
5496 case NODE_XSTR:
5497 case NODE_DXSTR:
5498 case NODE_DREGX:
5499 case NODE_LIT:
5500 case NODE_LIST:
5501 case NODE_ZLIST:
5502 yyerror1(&@3, "can't define singleton method for literals");
5503 break;
5504 default:
5505 value_expr($3);
5506 break;
5507 }
5508 $$ = $3;
5509#endif
5510 {VALUE v1,v2;v1=$3;v2=dispatch1(paren,v1);$$=v2;}
5511 }
5512 ;
5513
5514assoc_list : none
5515 | assocs trailer
5516 {
5517#if 0
5518 $$ = $1;
5519#endif
5520 {VALUE v1,v2;v1=$1;v2=dispatch1(assoclist_from_args,v1);$$=v2;}
5521 }
5522 ;
5523
5524assocs : assoc
5525 {$$=rb_ary_new3(1, get_value($1));}
5526 | assocs ',' assoc
5527 {
5528#if 0
5529 NODE *assocs = $1;
5530 NODE *tail = $3;
5531 if (!assocs) {
5532 assocs = tail;
5533 }
5534 else if (tail) {
5535 if (assocs->nd_head &&
5536 !tail->nd_head && nd_type(tail->nd_next) == NODE_LIST &&
5537 nd_type(tail->nd_next->nd_head) == NODE_HASH) {
5538 /* DSTAR */
5539 tail = tail->nd_next->nd_head->nd_head;
5540 }
5541 assocs = list_concat(assocs, tail);
5542 }
5543 $$ = assocs;
5544#endif
5545 $$=rb_ary_push($1, get_value($3));
5546 }
5547 ;
5548
5549assoc : arg_value tASSOC arg_value
5550 {
5551#if 0
5552 if (nd_type($1) == NODE_STR) {
5553 nd_set_type($1, NODE_LIT);
5554 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
5555 }
5556 $$ = list_append(p, NEW_LIST($1, &@$), $3);
5557#endif
5558 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5559 }
5560 | tLABEL arg_value
5561 {
5562#if 0
5563 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5564#endif
5565 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5566 }
5567 | tSTRING_BEG string_contents tLABEL_END arg_value
5568 {
5569#if 0
5570 YYLTYPE loc = code_loc_gen(&@1, &@3);
5571 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
5572#endif
5573 {VALUE v1,v2,v3,v4,v5;v1=$2;v2=dispatch1(dyna_symbol,v1);v3=v2;v4=$4;v5=dispatch2(assoc_new,v3,v4);$$=v5;}
5574 }
5575 | tDSTAR arg_value
5576 {
5577#if 0
5578 if (nd_type($2) == NODE_HASH &&
5579 !($2->nd_head && $2->nd_head->nd_alen)) {
5580 static VALUE empty_hash;
5581 if (!empty_hash) {
5582 empty_hash = rb_obj_freeze(rb_hash_new());
5583 rb_gc_register_mark_object(empty_hash);
5584 }
5585 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
5586 }
5587 else
5588 $$ = list_append(p, NEW_LIST(0, &@$), $2);
5589#endif
5590 {VALUE v1,v2;v1=$2;v2=dispatch1(assoc_splat,v1);$$=v2;}
5591 }
5592 ;
5593
5594operation : tIDENTIFIER
5595 | tCONSTANT
5596 | tFID
5597 ;
5598
5599operation2 : tIDENTIFIER
5600 | tCONSTANT
5601 | tFID
5602 | op
5603 ;
5604
5605operation3 : tIDENTIFIER
5606 | tFID
5607 | op
5608 ;
5609
5610dot_or_colon : '.'
5611 | tCOLON2
5612 ;
5613
5614call_op : '.'
5615 | tANDDOT
5616 ;
5617
5618call_op2 : call_op
5619 | tCOLON2
5620 ;
5621
5622opt_terms : /* none */
5623 | terms
5624 ;
5625
5626opt_nl : /* none */
5627 | '\n'
5628 ;
5629
5630rparen : opt_nl ')'
5631 ;
5632
5633rbracket : opt_nl ']'
5634 ;
5635
5636rbrace : opt_nl '}'
5637 ;
5638
5639trailer : /* none */
5640 | '\n'
5641 | ','
5642 ;
5643
5644term : ';' {yyerrok;token_flush(p);}
5645 | '\n' {token_flush(p);}
5646 ;
5647
5648terms : term
5649 | terms ';' {yyerrok;}
5650 ;
5651
5652none : /* none */
5653 {
5654 $$ = Qnull;
5655 }
5656 ;
5657%%
5658# undef p
5659# undef yylex
5660# undef yylval
5661# define yylval (*p->lval)
5662
5663static int regx_options(struct parser_params*);
5664static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5665static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5666static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5667static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
5668
5669#ifndef RIPPER
5670# define set_yylval_node(x) { \
5671 YYLTYPE _cur_loc; \
5672 rb_parser_set_location(p, &_cur_loc); \
5673 yylval.node = (x); \
5674}
5675# define set_yylval_str(x) \
5676do { \
5677 set_yylval_node(NEW_STR(x, &_cur_loc)); \
5678 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5679} while(0)
5680# define set_yylval_literal(x) \
5681do { \
5682 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
5683 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5684} while(0)
5685# define set_yylval_num(x) (yylval.num = (x))
5686# define set_yylval_id(x) (yylval.id = (x))
5687# define set_yylval_name(x) (yylval.id = (x))
5688# define yylval_id() (yylval.id)
5689#else
5690static inline VALUE
5691ripper_yylval_id(struct parser_params *p, ID x)
5692{
5693 return ripper_new_yylval(p, x, ID2SYM(x), 0);
5694}
5695# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
5696# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
5697# define set_yylval_id(x) (void)(x)
5698# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
5699# define set_yylval_literal(x) add_mark_object(p, (x))
5700# define set_yylval_node(x) (yylval.val = ripper_new_yylval(p, 0, 0, STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)))
5701# define yylval_id() yylval.id
5702# define _cur_loc NULL_LOC /* dummy */
5703#endif
5704
5705#define set_yylval_noname() set_yylval_id(keyword_nil)
5706
5707#ifndef RIPPER
5708#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
5709#define dispatch_scan_event(p, t) ((void)0)
5710#define dispatch_delayed_token(p, t) ((void)0)
5711#define has_delayed_token(p) (0)
5712#else
5713#define literal_flush(p, ptr) ((void)(ptr))
5714
5715#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5716
5717static inline VALUE
5718intern_sym(const char *name)
5719{
5720 ID id = rb_intern_const(name);
5721 return ID2SYM(id);
5722}
5723
5724static int
5725ripper_has_scan_event(struct parser_params *p)
5726{
5727 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
5728 return p->lex.pcur > p->lex.ptok;
5729}
5730
5731static VALUE
5732ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
5733{
5734 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
5735 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
5736 token_flush(p);
5737 return rval;
5738}
5739
5740static void
5741ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
5742{
5743 if (!ripper_has_scan_event(p)) return;
5744 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
5745}
5746#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
5747
5748static void
5749ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
5750{
5751 int saved_line = p->ruby_sourceline;
5752 const char *saved_tokp = p->lex.ptok;
5753
5754 if (NIL_P(p->delayed.token)) return;
5755 p->ruby_sourceline = p->delayed.line;
5756 p->lex.ptok = p->lex.pbeg + p->delayed.col;
5757 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
5758 p->delayed.token = Qnil;
5759 p->ruby_sourceline = saved_line;
5760 p->lex.ptok = saved_tokp;
5761}
5762#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
5763#define has_delayed_token(p) (!NIL_P(p->delayed.token))
5764#endif /* RIPPER */
5765
5766static inline int
5767is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
5768{
5769 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
5770}
5771
5772static inline int
5773parser_is_identchar(struct parser_params *p)
5774{
5775 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5776}
5777
5778static inline int
5779parser_isascii(struct parser_params *p)
5780{
5781 return ISASCII(*(p->lex.pcur-1));
5782}
5783
5784static void
5785token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
5786{
5787 int column = 1, nonspc = 0, i;
5788 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
5789 if (*ptr == '\t') {
5790 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5791 }
5792 column++;
5793 if (*ptr != ' ' && *ptr != '\t') {
5794 nonspc = 1;
5795 }
5796 }
5797
5798 ptinfo->beg = loc->beg_pos;
5799 ptinfo->indent = column;
5800 ptinfo->nonspc = nonspc;
5801}
5802
5803static void
5804token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5805{
5806 token_info *ptinfo;
5807
5808 if (!p->token_info_enabled) return;
5809 ptinfo = ALLOC(token_info);
5810 ptinfo->token = token;
5811 ptinfo->next = p->token_info;
5812 token_info_setup(ptinfo, p->lex.pbeg, loc);
5813
5814 p->token_info = ptinfo;
5815}
5816
5817static void
5818token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5819{
5820 token_info *ptinfo_beg = p->token_info;
5821
5822 if (!ptinfo_beg) return;
5823 p->token_info = ptinfo_beg->next;
5824
5825 /* indentation check of matched keywords (begin..end, if..end, etc.) */
5826 token_info_warn(p, token, ptinfo_beg, 1, loc);
5827 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5828}
5829
5830static void
5831token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
5832{
5833 token_info *ptinfo_beg = p->token_info;
5834
5835 if (!ptinfo_beg) return;
5836 p->token_info = ptinfo_beg->next;
5837
5838 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
5839 ptinfo_beg->beg.column != beg_pos.column ||
5840 strcmp(ptinfo_beg->token, token)) {
5841 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
5842 beg_pos.lineno, beg_pos.column, token,
5843 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
5844 ptinfo_beg->token);
5845 }
5846
5847 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5848}
5849
5850static void
5851token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
5852{
5853 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
5854 if (!p->token_info_enabled) return;
5855 if (!ptinfo_beg) return;
5856 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
5857 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
5858 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
5859 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
5860 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
5861 rb_warn3L(ptinfo_end->beg.lineno,
5862 "mismatched indentations at '%s' with '%s' at %d",
5863 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
5864}
5865
5866static int
5867parser_precise_mbclen(struct parser_params *p, const char *ptr)
5868{
5869 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
5870 if (!MBCLEN_CHARFOUND_P(len)) {
5871 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
5872 return -1;
5873 }
5874 return len;
5875}
5876
5877#ifndef RIPPER
5878static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
5879
5880static inline void
5881parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5882{
5883 VALUE str;
5884 int lineno = p->ruby_sourceline;
5885 if (!yylloc) {
5886 return;
5887 }
5888 else if (yylloc->beg_pos.lineno == lineno) {
5889 str = p->lex.lastline;
5890 }
5891 else {
5892 return;
5893 }
5894 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
5895}
5896
5897static int
5898parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5899{
5900 YYLTYPE current;
5901
5902 if (!yylloc) {
5903 yylloc = RUBY_SET_YYLLOC(current);
5904 }
5905 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
5906 p->ruby_sourceline != yylloc->end_pos.lineno) ||
5907 (yylloc->beg_pos.lineno == yylloc->end_pos.lineno &&
5908 yylloc->beg_pos.column == yylloc->end_pos.column)) {
5909 yylloc = 0;
5910 }
5911 compile_error(p, "%s", msg);
5912 parser_show_error_line(p, yylloc);
5913 return 0;
5914}
5915
5916static void
5917ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
5918{
5919 VALUE mesg;
5920 const int max_line_margin = 30;
5921 const char *ptr, *ptr_end, *pt, *pb;
5922 const char *pre = "", *post = "", *pend;
5923 const char *code = "", *caret = "";
5924 const char *lim;
5925 const char *const pbeg = RSTRING_PTR(str);
5926 char *buf;
5927 long len;
5928 int i;
5929
5930 if (!yylloc) return;
5931 pend = RSTRING_END(str);
5932 if (pend > pbeg && pend[-1] == '\n') {
5933 if (--pend > pbeg && pend[-1] == '\r') --pend;
5934 }
5935
5936 pt = pend;
5937 if (lineno == yylloc->end_pos.lineno &&
5938 (pend - pbeg) > yylloc->end_pos.column) {
5939 pt = pbeg + yylloc->end_pos.column;
5940 }
5941
5942 ptr = ptr_end = pt;
5943 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
5944 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
5945
5946 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
5947 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
5948
5949 len = ptr_end - ptr;
5950 if (len > 4) {
5951 if (ptr > pbeg) {
5952 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
5953 if (ptr > pbeg) pre = "...";
5954 }
5955 if (ptr_end < pend) {
5956 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
5957 if (ptr_end < pend) post = "...";
5958 }
5959 }
5960 pb = pbeg;
5961 if (lineno == yylloc->beg_pos.lineno) {
5962 pb += yylloc->beg_pos.column;
5963 if (pb > pt) pb = pt;
5964 }
5965 if (pb < ptr) pb = ptr;
5966 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
5967 return;
5968 }
5969 if (RTEST(errbuf)) {
5970 mesg = rb_attr_get(errbuf, idMesg);
5971 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
5972 rb_str_cat_cstr(mesg, "\n");
5973 }
5974 else {
5975 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
5976 }
5977 if (!errbuf && rb_stderr_tty_p()) {
5978#define CSI_BEGIN "\033["
5979#define CSI_SGR "m"
5980 rb_str_catf(mesg,
5981 CSI_BEGIN""CSI_SGR"%s" /* pre */
5982 CSI_BEGIN"1"CSI_SGR"%.*s"
5983 CSI_BEGIN"1;4"CSI_SGR"%.*s"
5984 CSI_BEGIN";1"CSI_SGR"%.*s"
5985 CSI_BEGIN""CSI_SGR"%s" /* post */
5986 "\n",
5987 pre,
5988 (int)(pb - ptr), ptr,
5989 (int)(pt - pb), pb,
5990 (int)(ptr_end - pt), pt,
5991 post);
5992 }
5993 else {
5994 char *p2;
5995
5996 len = ptr_end - ptr;
5997 lim = pt < pend ? pt : pend;
5998 i = (int)(lim - ptr);
5999 buf = ALLOCA_N(char, i+2);
6000 code = ptr;
6001 caret = p2 = buf;
6002 if (ptr <= pb) {
6003 while (ptr < pb) {
6004 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
6005 }
6006 *p2++ = '^';
6007 ptr++;
6008 }
6009 if (lim > ptr) {
6010 memset(p2, '~', (lim - ptr));
6011 p2 += (lim - ptr);
6012 }
6013 *p2 = '\0';
6014 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
6015 pre, (int)len, code, post,
6016 pre, caret);
6017 }
6018 if (!errbuf) rb_write_error_str(mesg);
6019}
6020#else
6021static int
6022parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6023{
6024 const char *pcur = 0, *ptok = 0;
6025 if (yylloc &&
6026 p->ruby_sourceline == yylloc->beg_pos.lineno &&
6027 p->ruby_sourceline == yylloc->end_pos.lineno) {
6028 pcur = p->lex.pcur;
6029 ptok = p->lex.ptok;
6030 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
6031 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
6032 }
6033 dispatch1(parse_error, STR_NEW2(msg));
6034 ripper_error(p);
6035 if (pcur) {
6036 p->lex.ptok = ptok;
6037 p->lex.pcur = pcur;
6038 }
6039 return 0;
6040}
6041
6042static inline void
6043parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6044{
6045}
6046#endif /* !RIPPER */
6047
6048#ifndef RIPPER
6049static int
6050vtable_size(const struct vtable *tbl)
6051{
6052 if (!DVARS_TERMINAL_P(tbl)) {
6053 return tbl->pos;
6054 }
6055 else {
6056 return 0;
6057 }
6058}
6059#endif
6060
6061static struct vtable *
6062vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
6063{
6064 struct vtable *tbl = ALLOC(struct vtable);
6065 tbl->pos = 0;
6066 tbl->capa = 8;
6067 tbl->tbl = ALLOC_N(ID, tbl->capa);
6068 tbl->prev = prev;
6069#ifndef RIPPER
6070 if (p->debug) {
6071 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
6072 }
6073#endif
6074 return tbl;
6075}
6076#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
6077
6078static void
6079vtable_free_gen(struct parser_params *p, int line, const char *name,
6080 struct vtable *tbl)
6081{
6082#ifndef RIPPER
6083 if (p->debug) {
6084 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
6085 }
6086#endif
6087 if (!DVARS_TERMINAL_P(tbl)) {
6088 if (tbl->tbl) {
6089 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
6090 }
6091 ruby_sized_xfree(tbl, sizeof(*tbl));
6092 }
6093}
6094#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
6095
6096static void
6097vtable_add_gen(struct parser_params *p, int line, const char *name,
6098 struct vtable *tbl, ID id)
6099{
6100#ifndef RIPPER
6101 if (p->debug) {
6102 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
6103 line, name, (void *)tbl, rb_id2name(id));
6104 }
6105#endif
6106 if (DVARS_TERMINAL_P(tbl)) {
6107 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
6108 return;
6109 }
6110 if (tbl->pos == tbl->capa) {
6111 tbl->capa = tbl->capa * 2;
6112 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
6113 }
6114 tbl->tbl[tbl->pos++] = id;
6115}
6116#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
6117
6118#ifndef RIPPER
6119static void
6120vtable_pop_gen(struct parser_params *p, int line, const char *name,
6121 struct vtable *tbl, int n)
6122{
6123 if (p->debug) {
6124 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
6125 line, name, (void *)tbl, n);
6126 }
6127 if (tbl->pos < n) {
6128 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
6129 return;
6130 }
6131 tbl->pos -= n;
6132}
6133#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
6134#endif
6135
6136static int
6137vtable_included(const struct vtable * tbl, ID id)
6138{
6139 int i;
6140
6141 if (!DVARS_TERMINAL_P(tbl)) {
6142 for (i = 0; i < tbl->pos; i++) {
6143 if (tbl->tbl[i] == id) {
6144 return i+1;
6145 }
6146 }
6147 }
6148 return 0;
6149}
6150
6151static void parser_prepare(struct parser_params *p);
6152
6153#ifndef RIPPER
6154static NODE *parser_append_options(struct parser_params *p, NODE *node);
6155
6156static VALUE
6157debug_lines(VALUE fname)
6158{
6159 ID script_lines;
6160 CONST_ID(script_lines, "SCRIPT_LINES__");
6161 if (rb_const_defined_at(rb_cObject, script_lines)) {
6162 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
6163 if (RB_TYPE_P(hash, T_HASH)) {
6164 VALUE lines = rb_ary_new();
6165 rb_hash_aset(hash, fname, lines);
6166 return lines;
6167 }
6168 }
6169 return 0;
6170}
6171
6172static int
6173e_option_supplied(struct parser_params *p)
6174{
6175 return strcmp(p->ruby_sourcefile, "-e") == 0;
6176}
6177
6178static VALUE
6179yycompile0(VALUE arg)
6180{
6181 int n;
6182 NODE *tree;
6183 struct parser_params *p = (struct parser_params *)arg;
6184 VALUE cov = Qfalse;
6185
6186 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
6187 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
6188 if (p->debug_lines && p->ruby_sourceline > 0) {
6189 VALUE str = rb_default_rs;
6190 n = p->ruby_sourceline;
6191 do {
6192 rb_ary_push(p->debug_lines, str);
6193 } while (--n);
6194 }
6195
6196 if (!e_option_supplied(p)) {
6197 cov = Qtrue;
6198 }
6199 }
6200
6201 parser_prepare(p);
6202#define RUBY_DTRACE_PARSE_HOOK(name) \
6203 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
6204 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
6205 }
6206 RUBY_DTRACE_PARSE_HOOK(BEGIN);
6207 n = yyparse(p);
6208 RUBY_DTRACE_PARSE_HOOK(END);
6209 p->debug_lines = 0;
6210
6211 p->lex.strterm = 0;
6212 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
6213 p->lex.prevline = p->lex.lastline = p->lex.nextline = 0;
6214 if (n || p->error_p) {
6215 VALUE mesg = p->error_buffer;
6216 if (!mesg) {
6217 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
6218 }
6219 rb_set_errinfo(mesg);
6220 return FALSE;
6221 }
6222 tree = p->eval_tree;
6223 if (!tree) {
6224 tree = NEW_NIL(&NULL_LOC);
6225 }
6226 else {
6227 VALUE opt = p->compile_option;
6228 NODE *prelude;
6229 NODE *body = parser_append_options(p, tree->nd_body);
6230 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
6231 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
6232 prelude = block_append(p, p->eval_tree_begin, body);
6233 tree->nd_body = prelude;
6234 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
6235 }
6236 p->ast->body.root = tree;
6237 p->ast->body.line_count = p->line_count;
6238 return TRUE;
6239}
6240
6241static rb_ast_t *
6242yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
6243{
6244 rb_ast_t *ast;
6245 if (NIL_P(fname)) {
6246 p->ruby_sourcefile_string = Qnil;
6247 p->ruby_sourcefile = "(none)";
6248 }
6249 else {
6250 p->ruby_sourcefile_string = rb_fstring(fname);
6251 p->ruby_sourcefile = StringValueCStr(fname);
6252 }
6253 p->ruby_sourceline = line - 1;
6254
6255 p->ast = ast = rb_ast_new();
6256 rb_suppress_tracing(yycompile0, (VALUE)p);
6257 p->ast = 0;
6258 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6259
6260 return ast;
6261}
6262#endif /* !RIPPER */
6263
6264static rb_encoding *
6265must_be_ascii_compatible(VALUE s)
6266{
6267 rb_encoding *enc = rb_enc_get(s);
6268 if (!rb_enc_asciicompat(enc)) {
6269 rb_raise(rb_eArgError, "invalid source encoding");
6270 }
6271 return enc;
6272}
6273
6274static VALUE
6275lex_get_str(struct parser_params *p, VALUE s)
6276{
6277 char *beg, *end, *start;
6278 long len;
6279
6280 beg = RSTRING_PTR(s);
6281 len = RSTRING_LEN(s);
6282 start = beg;
6283 if (p->lex.gets_.ptr) {
6284 if (len == p->lex.gets_.ptr) return Qnil;
6285 beg += p->lex.gets_.ptr;
6286 len -= p->lex.gets_.ptr;
6287 }
6288 end = memchr(beg, '\n', len);
6289 if (end) len = ++end - beg;
6290 p->lex.gets_.ptr += len;
6291 return rb_str_subseq(s, beg - start, len);
6292}
6293
6294static VALUE
6295lex_getline(struct parser_params *p)
6296{
6297 VALUE line = (*p->lex.gets)(p, p->lex.input);
6298 if (NIL_P(line)) return line;
6299 must_be_ascii_compatible(line);
6300#ifndef RIPPER
6301 if (p->debug_lines) {
6302 rb_enc_associate(line, p->enc);
6303 rb_ary_push(p->debug_lines, line);
6304 }
6305#endif
6306 p->line_count++;
6307 return line;
6308}
6309
6310static const rb_data_type_t parser_data_type;
6311
6312#ifndef RIPPER
6313static rb_ast_t*
6314parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6315{
6316 struct parser_params *p;
6317
6318 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6319
6320 p->lex.gets = lex_get_str;
6321 p->lex.gets_.ptr = 0;
6322 p->lex.input = rb_str_new_frozen(s);
6323 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6324
6325 return yycompile(vparser, p, fname, line);
6326}
6327
6328rb_ast_t*
6329rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6330{
6331 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6332}
6333
6334rb_ast_t*
6335rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6336{
6337 must_be_ascii_compatible(s);
6338 return parser_compile_string(vparser, f, s, line);
6339}
6340
6341VALUE rb_io_gets_internal(VALUE io);
6342
6343static VALUE
6344lex_io_gets(struct parser_params *p, VALUE io)
6345{
6346 return rb_io_gets_internal(io);
6347}
6348
6349rb_ast_t*
6350rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6351{
6352 struct parser_params *p;
6353
6354 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6355
6356 p->lex.gets = lex_io_gets;
6357 p->lex.input = file;
6358 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6359
6360 return yycompile(vparser, p, fname, start);
6361}
6362
6363static VALUE
6364lex_generic_gets(struct parser_params *p, VALUE input)
6365{
6366 return (*p->lex.gets_.call)(input, p->line_count);
6367}
6368
6369rb_ast_t*
6370rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6371{
6372 struct parser_params *p;
6373
6374 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6375
6376 p->lex.gets = lex_generic_gets;
6377 p->lex.gets_.call = lex_gets;
6378 p->lex.input = input;
6379 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6380
6381 return yycompile(vparser, p, fname, start);
6382}
6383#endif /* !RIPPER */
6384
6385#define STR_FUNC_ESCAPE 0x01
6386#define STR_FUNC_EXPAND 0x02
6387#define STR_FUNC_REGEXP 0x04
6388#define STR_FUNC_QWORDS 0x08
6389#define STR_FUNC_SYMBOL 0x10
6390#define STR_FUNC_INDENT 0x20
6391#define STR_FUNC_LABEL 0x40
6392#define STR_FUNC_LIST 0x4000
6393#define STR_FUNC_TERM 0x8000
6394
6395enum string_type {
6396 str_label = STR_FUNC_LABEL,
6397 str_squote = (0),
6398 str_dquote = (STR_FUNC_EXPAND),
6399 str_xquote = (STR_FUNC_EXPAND),
6400 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6401 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6402 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6403 str_ssym = (STR_FUNC_SYMBOL),
6404 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6405};
6406
6407static VALUE
6408parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6409{
6410 VALUE str;
6411
6412 str = rb_enc_str_new(ptr, len, enc);
6413 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6414 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6415 }
6416 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6417 rb_enc_associate(str, rb_ascii8bit_encoding());
6418 }
6419 }
6420
6421 return str;
6422}
6423
6424#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6425#define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6426#define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6427#define peek(p,c) peek_n(p, (c), 0)
6428#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6429#define peekc(p) peekc_n(p, 0)
6430#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6431
6432#ifdef RIPPER
6433static void
6434add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6435{
6436 if (tok < end) {
6437 if (!has_delayed_token(p)) {
6438 p->delayed.token = rb_str_buf_new(end - tok);
6439 rb_enc_associate(p->delayed.token, p->enc);
6440 p->delayed.line = p->ruby_sourceline;
6441 p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6442 }
6443 rb_str_buf_cat(p->delayed.token, tok, end - tok);
6444 p->lex.ptok = end;
6445 }
6446}
6447#else
6448#define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6449#endif
6450
6451static int
6452nextline(struct parser_params *p)
6453{
6454 VALUE v = p->lex.nextline;
6455 p->lex.nextline = 0;
6456 if (!v) {
6457 if (p->eofp)
6458 return -1;
6459
6460 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6461 goto end_of_input;
6462 }
6463
6464 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6465 end_of_input:
6466 p->eofp = 1;
6467 lex_goto_eol(p);
6468 return -1;
6469 }
6470 p->cr_seen = FALSE;
6471 }
6472 else if (NIL_P(v)) {
6473 /* after here-document without terminator */
6474 goto end_of_input;
6475 }
6476 add_delayed_token(p, p->lex.ptok, p->lex.pend);
6477 if (p->heredoc_end > 0) {
6478 p->ruby_sourceline = p->heredoc_end;
6479 p->heredoc_end = 0;
6480 }
6481 p->ruby_sourceline++;
6482 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
6483 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
6484 token_flush(p);
6485 p->lex.prevline = p->lex.lastline;
6486 p->lex.lastline = v;
6487 return 0;
6488}
6489
6490static int
6491parser_cr(struct parser_params *p, int c)
6492{
6493 if (peek(p, '\n')) {
6494 p->lex.pcur++;
6495 c = '\n';
6496 }
6497 return c;
6498}
6499
6500static inline int
6501nextc(struct parser_params *p)
6502{
6503 int c;
6504
6505 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6506 if (nextline(p)) return -1;
6507 }
6508 c = (unsigned char)*p->lex.pcur++;
6509 if (UNLIKELY(c == '\r')) {
6510 c = parser_cr(p, c);
6511 }
6512
6513 return c;
6514}
6515
6516static void
6517pushback(struct parser_params *p, int c)
6518{
6519 if (c == -1) return;
6520 p->lex.pcur--;
6521 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6522 p->lex.pcur--;
6523 }
6524}
6525
6526#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6527
6528#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6529#define tok(p) (p)->tokenbuf
6530#define toklen(p) (p)->tokidx
6531
6532static int
6533looking_at_eol_p(struct parser_params *p)
6534{
6535 const char *ptr = p->lex.pcur;
6536 while (ptr < p->lex.pend) {
6537 int c = (unsigned char)*ptr++;
6538 int eol = (c == '\n' || c == '#');
6539 if (eol || !ISSPACE(c)) {
6540 return eol;
6541 }
6542 }
6543 return TRUE;
6544}
6545
6546static char*
6547newtok(struct parser_params *p)
6548{
6549 p->tokidx = 0;
6550 p->tokline = p->ruby_sourceline;
6551 if (!p->tokenbuf) {
6552 p->toksiz = 60;
6553 p->tokenbuf = ALLOC_N(char, 60);
6554 }
6555 if (p->toksiz > 4096) {
6556 p->toksiz = 60;
6557 REALLOC_N(p->tokenbuf, char, 60);
6558 }
6559 return p->tokenbuf;
6560}
6561
6562static char *
6563tokspace(struct parser_params *p, int n)
6564{
6565 p->tokidx += n;
6566
6567 if (p->tokidx >= p->toksiz) {
6568 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6569 REALLOC_N(p->tokenbuf, char, p->toksiz);
6570 }
6571 return &p->tokenbuf[p->tokidx-n];
6572}
6573
6574static void
6575tokadd(struct parser_params *p, int c)
6576{
6577 p->tokenbuf[p->tokidx++] = (char)c;
6578 if (p->tokidx >= p->toksiz) {
6579 p->toksiz *= 2;
6580 REALLOC_N(p->tokenbuf, char, p->toksiz);
6581 }
6582}
6583
6584static int
6585tok_hex(struct parser_params *p, size_t *numlen)
6586{
6587 int c;
6588
6589 c = scan_hex(p->lex.pcur, 2, numlen);
6590 if (!*numlen) {
6591 yyerror0("invalid hex escape");
6592 token_flush(p);
6593 return 0;
6594 }
6595 p->lex.pcur += *numlen;
6596 return c;
6597}
6598
6599#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6600
6601static int
6602escaped_control_code(int c)
6603{
6604 int c2 = 0;
6605 switch (c) {
6606 case ' ':
6607 c2 = 's';
6608 break;
6609 case '\n':
6610 c2 = 'n';
6611 break;
6612 case '\t':
6613 c2 = 't';
6614 break;
6615 case '\v':
6616 c2 = 'v';
6617 break;
6618 case '\r':
6619 c2 = 'r';
6620 break;
6621 case '\f':
6622 c2 = 'f';
6623 break;
6624 }
6625 return c2;
6626}
6627
6628#define WARN_SPACE_CHAR(c, prefix) \
6629 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6630
6631static int
6632tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6633 int regexp_literal, int wide)
6634{
6635 size_t numlen;
6636 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6637 literal_flush(p, p->lex.pcur);
6638 p->lex.pcur += numlen;
6639 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6640 yyerror0("invalid Unicode escape");
6641 return wide && numlen > 0;
6642 }
6643 if (codepoint > 0x10ffff) {
6644 yyerror0("invalid Unicode codepoint (too large)");
6645 return wide;
6646 }
6647 if ((codepoint & 0xfffff800) == 0xd800) {
6648 yyerror0("invalid Unicode codepoint");
6649 return wide;
6650 }
6651 if (regexp_literal) {
6652 tokcopy(p, (int)numlen);
6653 }
6654 else if (codepoint >= 0x80) {
6655 rb_encoding *utf8 = rb_utf8_encoding();
6656 if (*encp && utf8 != *encp) {
6657 YYLTYPE loc = RUBY_INIT_YYLLOC();
6658 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6659 parser_show_error_line(p, &loc);
6660 return wide;
6661 }
6662 *encp = utf8;
6663 tokaddmbc(p, codepoint, *encp);
6664 }
6665 else {
6666 tokadd(p, codepoint);
6667 }
6668 return TRUE;
6669}
6670
6671/* return value is for ?\u3042 */
6672static void
6673tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6674 int term, int symbol_literal, int regexp_literal)
6675{
6676 /*
6677 * If `term` is not -1, then we allow multiple codepoints in \u{}
6678 * upto `term` byte, otherwise we're parsing a character literal.
6679 * And then add the codepoints to the current token.
6680 */
6681 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6682
6683 const int open_brace = '{', close_brace = '}';
6684
6685 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6686
6687 if (peek(p, open_brace)) { /* handle \u{...} form */
6688 const char *second = NULL;
6689 int c, last = nextc(p);
6690 if (p->lex.pcur >= p->lex.pend) goto unterminated;
6691 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6692 while (c != close_brace) {
6693 if (c == term) goto unterminated;
6694 if (second == multiple_codepoints)
6695 second = p->lex.pcur;
6696 if (regexp_literal) tokadd(p, last);
6697 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6698 break;
6699 }
6700 while (ISSPACE(c = *p->lex.pcur)) {
6701 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6702 last = c;
6703 }
6704 if (term == -1 && !second)
6705 second = multiple_codepoints;
6706 }
6707
6708 if (c != close_brace) {
6709 unterminated:
6710 token_flush(p);
6711 yyerror0("unterminated Unicode escape");
6712 return;
6713 }
6714 if (second && second != multiple_codepoints) {
6715 const char *pcur = p->lex.pcur;
6716 p->lex.pcur = second;
6717 dispatch_scan_event(p, tSTRING_CONTENT);
6718 token_flush(p);
6719 p->lex.pcur = pcur;
6720 yyerror0(multiple_codepoints);
6721 token_flush(p);
6722 }
6723
6724 if (regexp_literal) tokadd(p, close_brace);
6725 nextc(p);
6726 }
6727 else { /* handle \uxxxx form */
6728 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6729 token_flush(p);
6730 return;
6731 }
6732 }
6733}
6734
6735#define ESCAPE_CONTROL 1
6736#define ESCAPE_META 2
6737
6738static int
6739read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6740{
6741 int c;
6742 size_t numlen;
6743
6744 switch (c = nextc(p)) {
6745 case '\\': /* Backslash */
6746 return c;
6747
6748 case 'n': /* newline */
6749 return '\n';
6750
6751 case 't': /* horizontal tab */
6752 return '\t';
6753
6754 case 'r': /* carriage-return */
6755 return '\r';
6756
6757 case 'f': /* form-feed */
6758 return '\f';
6759
6760 case 'v': /* vertical tab */
6761 return '\13';
6762
6763 case 'a': /* alarm(bell) */
6764 return '\007';
6765
6766 case 'e': /* escape */
6767 return 033;
6768
6769 case '0': case '1': case '2': case '3': /* octal constant */
6770 case '4': case '5': case '6': case '7':
6771 pushback(p, c);
6772 c = scan_oct(p->lex.pcur, 3, &numlen);
6773 p->lex.pcur += numlen;
6774 return c;
6775
6776 case 'x': /* hex constant */
6777 c = tok_hex(p, &numlen);
6778 if (numlen == 0) return 0;
6779 return c;
6780
6781 case 'b': /* backspace */
6782 return '\010';
6783
6784 case 's': /* space */
6785 return ' ';
6786
6787 case 'M':
6788 if (flags & ESCAPE_META) goto eof;
6789 if ((c = nextc(p)) != '-') {
6790 goto eof;
6791 }
6792 if ((c = nextc(p)) == '\\') {
6793 switch (peekc(p)) {
6794 case 'u': case 'U':
6795 nextc(p);
6796 goto eof;
6797 }
6798 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6799 }
6800 else if (c == -1 || !ISASCII(c)) goto eof;
6801 else {
6802 int c2 = escaped_control_code(c);
6803 if (c2) {
6804 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6805 WARN_SPACE_CHAR(c2, "\\M-");
6806 }
6807 else {
6808 WARN_SPACE_CHAR(c2, "\\C-\\M-");
6809 }
6810 }
6811 else if (ISCNTRL(c)) goto eof;
6812 return ((c & 0xff) | 0x80);
6813 }
6814
6815 case 'C':
6816 if ((c = nextc(p)) != '-') {
6817 goto eof;
6818 }
6819 case 'c':
6820 if (flags & ESCAPE_CONTROL) goto eof;
6821 if ((c = nextc(p))== '\\') {
6822 switch (peekc(p)) {
6823 case 'u': case 'U':
6824 nextc(p);
6825 goto eof;
6826 }
6827 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6828 }
6829 else if (c == '?')
6830 return 0177;
6831 else if (c == -1 || !ISASCII(c)) goto eof;
6832 else {
6833 int c2 = escaped_control_code(c);
6834 if (c2) {
6835 if (ISCNTRL(c)) {
6836 if (flags & ESCAPE_META) {
6837 WARN_SPACE_CHAR(c2, "\\M-");
6838 }
6839 else {
6840 WARN_SPACE_CHAR(c2, "");
6841 }
6842 }
6843 else {
6844 if (flags & ESCAPE_META) {
6845 WARN_SPACE_CHAR(c2, "\\M-\\C-");
6846 }
6847 else {
6848 WARN_SPACE_CHAR(c2, "\\C-");
6849 }
6850 }
6851 }
6852 else if (ISCNTRL(c)) goto eof;
6853 }
6854 return c & 0x9f;
6855
6856 eof:
6857 case -1:
6858 yyerror0("Invalid escape character syntax");
6859 token_flush(p);
6860 return '\0';
6861
6862 default:
6863 return c;
6864 }
6865}
6866
6867static void
6868tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
6869{
6870 int len = rb_enc_codelen(c, enc);
6871 rb_enc_mbcput(c, tokspace(p, len), enc);
6872}
6873
6874static int
6875tokadd_escape(struct parser_params *p, rb_encoding **encp)
6876{
6877 int c;
6878 int flags = 0;
6879 size_t numlen;
6880
6881 first:
6882 switch (c = nextc(p)) {
6883 case '\n':
6884 return 0; /* just ignore */
6885
6886 case '0': case '1': case '2': case '3': /* octal constant */
6887 case '4': case '5': case '6': case '7':
6888 {
6889 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
6890 if (numlen == 0) goto eof;
6891 p->lex.pcur += numlen;
6892 tokcopy(p, (int)numlen + 1);
6893 }
6894 return 0;
6895
6896 case 'x': /* hex constant */
6897 {
6898 tok_hex(p, &numlen);
6899 if (numlen == 0) return -1;
6900 tokcopy(p, (int)numlen + 2);
6901 }
6902 return 0;
6903
6904 case 'M':
6905 if (flags & ESCAPE_META) goto eof;
6906 if ((c = nextc(p)) != '-') {
6907 pushback(p, c);
6908 goto eof;
6909 }
6910 tokcopy(p, 3);
6911 flags |= ESCAPE_META;
6912 goto escaped;
6913
6914 case 'C':
6915 if (flags & ESCAPE_CONTROL) goto eof;
6916 if ((c = nextc(p)) != '-') {
6917 pushback(p, c);
6918 goto eof;
6919 }
6920 tokcopy(p, 3);
6921 goto escaped;
6922
6923 case 'c':
6924 if (flags & ESCAPE_CONTROL) goto eof;
6925 tokcopy(p, 2);
6926 flags |= ESCAPE_CONTROL;
6927 escaped:
6928 if ((c = nextc(p)) == '\\') {
6929 goto first;
6930 }
6931 else if (c == -1) goto eof;
6932 tokadd(p, c);
6933 return 0;
6934
6935 eof:
6936 case -1:
6937 yyerror0("Invalid escape character syntax");
6938 token_flush(p);
6939 return -1;
6940
6941 default:
6942 tokadd(p, '\\');
6943 tokadd(p, c);
6944 }
6945 return 0;
6946}
6947
6948static int
6949regx_options(struct parser_params *p)
6950{
6951 int kcode = 0;
6952 int kopt = 0;
6953 int options = 0;
6954 int c, opt, kc;
6955
6956 newtok(p);
6957 while (c = nextc(p), ISALPHA(c)) {
6958 if (c == 'o') {
6959 options |= RE_OPTION_ONCE;
6960 }
6961 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
6962 if (kc >= 0) {
6963 if (kc != rb_ascii8bit_encindex()) kcode = c;
6964 kopt = opt;
6965 }
6966 else {
6967 options |= opt;
6968 }
6969 }
6970 else {
6971 tokadd(p, c);
6972 }
6973 }
6974 options |= kopt;
6975 pushback(p, c);
6976 if (toklen(p)) {
6977 YYLTYPE loc = RUBY_INIT_YYLLOC();
6978 tokfix(p);
6979 compile_error(p, "unknown regexp option%s - %*s",
6980 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
6981 parser_show_error_line(p, &loc);
6982 }
6983 return options | RE_OPTION_ENCODING(kcode);
6984}
6985
6986static int
6987tokadd_mbchar(struct parser_params *p, int c)
6988{
6989 int len = parser_precise_mbclen(p, p->lex.pcur-1);
6990 if (len < 0) return -1;
6991 tokadd(p, c);
6992 p->lex.pcur += --len;
6993 if (len > 0) tokcopy(p, len);
6994 return c;
6995}
6996
6997static inline int
6998simple_re_meta(int c)
6999{
7000 switch (c) {
7001 case '$': case '*': case '+': case '.':
7002 case '?': case '^': case '|':
7003 case ')': case ']': case '}': case '>':
7004 return TRUE;
7005 default:
7006 return FALSE;
7007 }
7008}
7009
7010static int
7011parser_update_heredoc_indent(struct parser_params *p, int c)
7012{
7013 if (p->heredoc_line_indent == -1) {
7014 if (c == '\n') p->heredoc_line_indent = 0;
7015 }
7016 else {
7017 if (c == ' ') {
7018 p->heredoc_line_indent++;
7019 return TRUE;
7020 }
7021 else if (c == '\t') {
7022 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
7023 p->heredoc_line_indent = w * TAB_WIDTH;
7024 return TRUE;
7025 }
7026 else if (c != '\n') {
7027 if (p->heredoc_indent > p->heredoc_line_indent) {
7028 p->heredoc_indent = p->heredoc_line_indent;
7029 }
7030 p->heredoc_line_indent = -1;
7031 }
7032 }
7033 return FALSE;
7034}
7035
7036static void
7037parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
7038{
7039 YYLTYPE loc = RUBY_INIT_YYLLOC();
7040 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
7041 compile_error(p, "%s mixed within %s source", n1, n2);
7042 parser_show_error_line(p, &loc);
7043}
7044
7045static void
7046parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
7047{
7048 const char *pos = p->lex.pcur;
7049 p->lex.pcur = beg;
7050 parser_mixed_error(p, enc1, enc2);
7051 p->lex.pcur = pos;
7052}
7053
7054static int
7055tokadd_string(struct parser_params *p,
7056 int func, int term, int paren, long *nest,
7057 rb_encoding **encp, rb_encoding **enc)
7058{
7059 int c;
7060 bool erred = false;
7061
7062#define mixed_error(enc1, enc2) \
7063 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
7064#define mixed_escape(beg, enc1, enc2) \
7065 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
7066
7067 while ((c = nextc(p)) != -1) {
7068 if (p->heredoc_indent > 0) {
7069 parser_update_heredoc_indent(p, c);
7070 }
7071
7072 if (paren && c == paren) {
7073 ++*nest;
7074 }
7075 else if (c == term) {
7076 if (!nest || !*nest) {
7077 pushback(p, c);
7078 break;
7079 }
7080 --*nest;
7081 }
7082 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
7083 int c2 = *p->lex.pcur;
7084 if (c2 == '$' || c2 == '@' || c2 == '{') {
7085 pushback(p, c);
7086 break;
7087 }
7088 }
7089 else if (c == '\\') {
7090 literal_flush(p, p->lex.pcur - 1);
7091 c = nextc(p);
7092 switch (c) {
7093 case '\n':
7094 if (func & STR_FUNC_QWORDS) break;
7095 if (func & STR_FUNC_EXPAND) {
7096 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
7097 continue;
7098 if (c == term) {
7099 c = '\\';
7100 goto terminate;
7101 }
7102 }
7103 tokadd(p, '\\');
7104 break;
7105
7106 case '\\':
7107 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
7108 break;
7109
7110 case 'u':
7111 if ((func & STR_FUNC_EXPAND) == 0) {
7112 tokadd(p, '\\');
7113 break;
7114 }
7115 tokadd_utf8(p, enc, term,
7116 func & STR_FUNC_SYMBOL,
7117 func & STR_FUNC_REGEXP);
7118 continue;
7119
7120 default:
7121 if (c == -1) return -1;
7122 if (!ISASCII(c)) {
7123 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
7124 goto non_ascii;
7125 }
7126 if (func & STR_FUNC_REGEXP) {
7127 if (c == term && !simple_re_meta(c)) {
7128 tokadd(p, c);
7129 continue;
7130 }
7131 pushback(p, c);
7132 if ((c = tokadd_escape(p, enc)) < 0)
7133 return -1;
7134 if (*enc && *enc != *encp) {
7135 mixed_escape(p->lex.ptok+2, *enc, *encp);
7136 }
7137 continue;
7138 }
7139 else if (func & STR_FUNC_EXPAND) {
7140 pushback(p, c);
7141 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
7142 c = read_escape(p, 0, enc);
7143 }
7144 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7145 /* ignore backslashed spaces in %w */
7146 }
7147 else if (c != term && !(paren && c == paren)) {
7148 tokadd(p, '\\');
7149 pushback(p, c);
7150 continue;
7151 }
7152 }
7153 }
7154 else if (!parser_isascii(p)) {
7155 non_ascii:
7156 if (!*enc) {
7157 *enc = *encp;
7158 }
7159 else if (*enc != *encp) {
7160 mixed_error(*enc, *encp);
7161 continue;
7162 }
7163 if (tokadd_mbchar(p, c) == -1) return -1;
7164 continue;
7165 }
7166 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7167 pushback(p, c);
7168 break;
7169 }
7170 if (c & 0x80) {
7171 if (!*enc) {
7172 *enc = *encp;
7173 }
7174 else if (*enc != *encp) {
7175 mixed_error(*enc, *encp);
7176 continue;
7177 }
7178 }
7179 tokadd(p, c);
7180 }
7181 terminate:
7182 if (*enc) *encp = *enc;
7183 return c;
7184}
7185
7186static inline rb_strterm_t *
7187new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
7188{
7189 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
7190}
7191
7192/* imemo_parser_strterm for literal */
7193#define NEW_STRTERM(func, term, paren) \
7194 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
7195
7196#ifdef RIPPER
7197static void
7198flush_string_content(struct parser_params *p, rb_encoding *enc)
7199{
7200 VALUE content = yylval.val;
7201 if (!ripper_is_node_yylval(content))
7202 content = ripper_new_yylval(p, 0, 0, content);
7203 if (has_delayed_token(p)) {
7204 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7205 if (len > 0) {
7206 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7207 }
7208 dispatch_delayed_token(p, tSTRING_CONTENT);
7209 p->lex.ptok = p->lex.pcur;
7210 RNODE(content)->nd_rval = yylval.val;
7211 }
7212 dispatch_scan_event(p, tSTRING_CONTENT);
7213 if (yylval.val != content)
7214 RNODE(content)->nd_rval = yylval.val;
7215 yylval.val = content;
7216}
7217#else
7218#define flush_string_content(p, enc) ((void)(enc))
7219#endif
7220
7221RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7222/* this can be shared with ripper, since it's independent from struct
7223 * parser_params. */
7224#ifndef RIPPER
7225#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7226#define SPECIAL_PUNCT(idx) ( \
7227 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
7228 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
7229 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
7230 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
7231 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7232 BIT('0', idx))
7233const unsigned int ruby_global_name_punct_bits[] = {
7234 SPECIAL_PUNCT(0),
7235 SPECIAL_PUNCT(1),
7236 SPECIAL_PUNCT(2),
7237};
7238#undef BIT
7239#undef SPECIAL_PUNCT
7240#endif
7241
7242static enum yytokentype
7243parser_peek_variable_name(struct parser_params *p)
7244{
7245 int c;
7246 const char *ptr = p->lex.pcur;
7247
7248 if (ptr + 1 >= p->lex.pend) return 0;
7249 c = *ptr++;
7250 switch (c) {
7251 case '$':
7252 if ((c = *ptr) == '-') {
7253 if (++ptr >= p->lex.pend) return 0;
7254 c = *ptr;
7255 }
7256 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7257 return tSTRING_DVAR;
7258 }
7259 break;
7260 case '@':
7261 if ((c = *ptr) == '@') {
7262 if (++ptr >= p->lex.pend) return 0;
7263 c = *ptr;
7264 }
7265 break;
7266 case '{':
7267 p->lex.pcur = ptr;
7268 p->command_start = TRUE;
7269 return tSTRING_DBEG;
7270 default:
7271 return 0;
7272 }
7273 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7274 return tSTRING_DVAR;
7275 return 0;
7276}
7277
7278#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7279#define IS_END() IS_lex_state(EXPR_END_ANY)
7280#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7281#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7282#define IS_LABEL_POSSIBLE() (\
7283 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7284 IS_ARG())
7285#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7286#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7287
7288static inline enum yytokentype
7289parser_string_term(struct parser_params *p, int func)
7290{
7291 p->lex.strterm = 0;
7292 if (func & STR_FUNC_REGEXP) {
7293 set_yylval_num(regx_options(p));
7294 dispatch_scan_event(p, tREGEXP_END);
7295 SET_LEX_STATE(EXPR_END);
7296 return tREGEXP_END;
7297 }
7298 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7299 nextc(p);
7300 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7301 return tLABEL_END;
7302 }
7303 SET_LEX_STATE(EXPR_END);
7304 return tSTRING_END;
7305}
7306
7307static enum yytokentype
7308parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7309{
7310 int func = (int)quote->u1.func;
7311 int term = (int)quote->u3.term;
7312 int paren = (int)quote->u2.paren;
7313 int c, space = 0;
7314 rb_encoding *enc = p->enc;
7315 rb_encoding *base_enc = 0;
7316 VALUE lit;
7317
7318 if (func & STR_FUNC_TERM) {
7319 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7320 SET_LEX_STATE(EXPR_END);
7321 p->lex.strterm = 0;
7322 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7323 }
7324 c = nextc(p);
7325 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7326 do {c = nextc(p);} while (ISSPACE(c));
7327 space = 1;
7328 }
7329 if (func & STR_FUNC_LIST) {
7330 quote->u1.func &= ~STR_FUNC_LIST;
7331 space = 1;
7332 }
7333 if (c == term && !quote->u0.nest) {
7334 if (func & STR_FUNC_QWORDS) {
7335 quote->u1.func |= STR_FUNC_TERM;
7336 pushback(p, c); /* dispatch the term at tSTRING_END */
7337 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7338 return ' ';
7339 }
7340 return parser_string_term(p, func);
7341 }
7342 if (space) {
7343 pushback(p, c);
7344 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7345 return ' ';
7346 }
7347 newtok(p);
7348 if ((func & STR_FUNC_EXPAND) && c == '#') {
7349 int t = parser_peek_variable_name(p);
7350 if (t) return t;
7351 tokadd(p, '#');
7352 c = nextc(p);
7353 }
7354 pushback(p, c);
7355 if (tokadd_string(p, func, term, paren, &quote->u0.nest,
7356 &enc, &base_enc) == -1) {
7357 if (p->eofp) {
7358#ifndef RIPPER
7359# define unterminated_literal(mesg) yyerror0(mesg)
7360#else
7361# define unterminated_literal(mesg) compile_error(p, mesg)
7362#endif
7363 literal_flush(p, p->lex.pcur);
7364 if (func & STR_FUNC_QWORDS) {
7365 /* no content to add, bailing out here */
7366 unterminated_literal("unterminated list meets end of file");
7367 p->lex.strterm = 0;
7368 return tSTRING_END;
7369 }
7370 if (func & STR_FUNC_REGEXP) {
7371 unterminated_literal("unterminated regexp meets end of file");
7372 }
7373 else {
7374 unterminated_literal("unterminated string meets end of file");
7375 }
7376 quote->u1.func |= STR_FUNC_TERM;
7377 }
7378 }
7379
7380 tokfix(p);
7381 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7382 set_yylval_str(lit);
7383 flush_string_content(p, enc);
7384
7385 return tSTRING_CONTENT;
7386}
7387
7388static enum yytokentype
7389heredoc_identifier(struct parser_params *p)
7390{
7391 /*
7392 * term_len is length of `<<"END"` except `END`,
7393 * in this case term_len is 4 (<, <, " and ").
7394 */
7395 long len, offset = p->lex.pcur - p->lex.pbeg;
7396 int c = nextc(p), term, func = 0, quote = 0;
7397 enum yytokentype token = tSTRING_BEG;
7398 int indent = 0;
7399
7400 if (c == '-') {
7401 c = nextc(p);
7402 func = STR_FUNC_INDENT;
7403 offset++;
7404 }
7405 else if (c == '~') {
7406 c = nextc(p);
7407 func = STR_FUNC_INDENT;
7408 offset++;
7409 indent = INT_MAX;
7410 }
7411 switch (c) {
7412 case '\'':
7413 func |= str_squote; goto quoted;
7414 case '"':
7415 func |= str_dquote; goto quoted;
7416 case '`':
7417 token = tXSTRING_BEG;
7418 func |= str_xquote; goto quoted;
7419
7420 quoted:
7421 quote++;
7422 offset++;
7423 term = c;
7424 len = 0;
7425 while ((c = nextc(p)) != term) {
7426 if (c == -1 || c == '\r' || c == '\n') {
7427 yyerror(NULL, p, "unterminated here document identifier");
7428 return -1;
7429 }
7430 }
7431 break;
7432
7433 default:
7434 if (!parser_is_identchar(p)) {
7435 pushback(p, c);
7436 if (func & STR_FUNC_INDENT) {
7437 pushback(p, indent > 0 ? '~' : '-');
7438 }
7439 return 0;
7440 }
7441 func |= str_dquote;
7442 do {
7443 int n = parser_precise_mbclen(p, p->lex.pcur-1);
7444 if (n < 0) return 0;
7445 p->lex.pcur += --n;
7446 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7447 pushback(p, c);
7448 break;
7449 }
7450
7451 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7452 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7453 yyerror(NULL, p, "too long here document identifier");
7454 dispatch_scan_event(p, tHEREDOC_BEG);
7455 lex_goto_eol(p);
7456
7457 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7458 p->lex.strterm->flags |= STRTERM_HEREDOC;
7459 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7460 here->offset = offset;
7461 here->sourceline = p->ruby_sourceline;
7462 here->length = (int)len;
7463 here->quote = quote;
7464 here->func = func;
7465
7466 token_flush(p);
7467 p->heredoc_indent = indent;
7468 p->heredoc_line_indent = 0;
7469 return token;
7470}
7471
7472static void
7473heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7474{
7475 VALUE line;
7476
7477 p->lex.strterm = 0;
7478 line = here->lastline;
7479 p->lex.lastline = line;
7480 p->lex.pbeg = RSTRING_PTR(line);
7481 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7482 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7483 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7484 p->heredoc_end = p->ruby_sourceline;
7485 p->ruby_sourceline = (int)here->sourceline;
7486 if (p->eofp) p->lex.nextline = Qnil;
7487 p->eofp = 0;
7488}
7489
7490static int
7491dedent_string(VALUE string, int width)
7492{
7493 char *str;
7494 long len;
7495 int i, col = 0;
7496
7497 RSTRING_GETMEM(string, str, len);
7498 for (i = 0; i < len && col < width; i++) {
7499 if (str[i] == ' ') {
7500 col++;
7501 }
7502 else if (str[i] == '\t') {
7503 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7504 if (n > width) break;
7505 col = n;
7506 }
7507 else {
7508 break;
7509 }
7510 }
7511 if (!i) return 0;
7512 rb_str_modify(string);
7513 str = RSTRING_PTR(string);
7514 if (RSTRING_LEN(string) != len)
7515 rb_fatal("literal string changed: %+"PRIsVALUE, string);
7516 MEMMOVE(str, str + i, char, len - i);
7517 rb_str_set_len(string, len - i);
7518 return i;
7519}
7520
7521#ifndef RIPPER
7522static NODE *
7523heredoc_dedent(struct parser_params *p, NODE *root)
7524{
7525 NODE *node, *str_node, *prev_node;
7526 int indent = p->heredoc_indent;
7527 VALUE prev_lit = 0;
7528
7529 if (indent <= 0) return root;
7530 p->heredoc_indent = 0;
7531 if (!root) return root;
7532
7533 prev_node = node = str_node = root;
7534 if (nd_type(root) == NODE_LIST) str_node = root->nd_head;
7535
7536 while (str_node) {
7537 VALUE lit = str_node->nd_lit;
7538 if (str_node->flags & NODE_FL_NEWLINE) {
7539 dedent_string(lit, indent);
7540 }
7541 if (!prev_lit) {
7542 prev_lit = lit;
7543 }
7544 else if (!literal_concat0(p, prev_lit, lit)) {
7545 return 0;
7546 }
7547 else {
7548 NODE *end = node->nd_end;
7549 node = prev_node->nd_next = node->nd_next;
7550 if (!node) {
7551 if (nd_type(prev_node) == NODE_DSTR)
7552 nd_set_type(prev_node, NODE_STR);
7553 break;
7554 }
7555 node->nd_end = end;
7556 goto next_str;
7557 }
7558
7559 str_node = 0;
7560 while ((node = (prev_node = node)->nd_next) != 0) {
7561 next_str:
7562 if (nd_type(node) != NODE_LIST) break;
7563 if ((str_node = node->nd_head) != 0) {
7564 enum node_type type = nd_type(str_node);
7565 if (type == NODE_STR || type == NODE_DSTR) break;
7566 prev_lit = 0;
7567 str_node = 0;
7568 }
7569 }
7570 }
7571 return root;
7572}
7573#else /* RIPPER */
7574static VALUE
7575heredoc_dedent(struct parser_params *p, VALUE array)
7576{
7577 int indent = p->heredoc_indent;
7578
7579 if (indent <= 0) return array;
7580 p->heredoc_indent = 0;
7581 dispatch2(heredoc_dedent, array, INT2NUM(indent));
7582 return array;
7583}
7584
7585/*
7586 * call-seq:
7587 * Ripper.dedent_string(input, width) -> Integer
7588 *
7589 * USE OF RIPPER LIBRARY ONLY.
7590 *
7591 * Strips up to +width+ leading whitespaces from +input+,
7592 * and returns the stripped column width.
7593 */
7594static VALUE
7595parser_dedent_string(VALUE self, VALUE input, VALUE width)
7596{
7597 int wid, col;
7598
7599 StringValue(input);
7600 wid = NUM2UINT(width);
7601 col = dedent_string(input, wid);
7602 return INT2NUM(col);
7603}
7604#endif
7605
7606static int
7607whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7608{
7609 const char *ptr = p->lex.pbeg;
7610 long n;
7611
7612 if (indent) {
7613 while (*ptr && ISSPACE(*ptr)) ptr++;
7614 }
7615 n = p->lex.pend - (ptr + len);
7616 if (n < 0) return FALSE;
7617 if (n > 0 && ptr[len] != '\n') {
7618 if (ptr[len] != '\r') return FALSE;
7619 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7620 }
7621 return strncmp(eos, ptr, len) == 0;
7622}
7623
7624static int
7625word_match_p(struct parser_params *p, const char *word, long len)
7626{
7627 if (strncmp(p->lex.pcur, word, len)) return 0;
7628 if (p->lex.pcur + len == p->lex.pend) return 1;
7629 int c = (unsigned char)p->lex.pcur[len];
7630 if (ISSPACE(c)) return 1;
7631 switch (c) {
7632 case '\0': case '\004': case '\032': return 1;
7633 }
7634 return 0;
7635}
7636
7637#define NUM_SUFFIX_R (1<<0)
7638#define NUM_SUFFIX_I (1<<1)
7639#define NUM_SUFFIX_ALL 3
7640
7641static int
7642number_literal_suffix(struct parser_params *p, int mask)
7643{
7644 int c, result = 0;
7645 const char *lastp = p->lex.pcur;
7646
7647 while ((c = nextc(p)) != -1) {
7648 if ((mask & NUM_SUFFIX_I) && c == 'i') {
7649 result |= (mask & NUM_SUFFIX_I);
7650 mask &= ~NUM_SUFFIX_I;
7651 /* r after i, rational of complex is disallowed */
7652 mask &= ~NUM_SUFFIX_R;
7653 continue;
7654 }
7655 if ((mask & NUM_SUFFIX_R) && c == 'r') {
7656 result |= (mask & NUM_SUFFIX_R);
7657 mask &= ~NUM_SUFFIX_R;
7658 continue;
7659 }
7660 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7661 p->lex.pcur = lastp;
7662 literal_flush(p, p->lex.pcur);
7663 return 0;
7664 }
7665 pushback(p, c);
7666 break;
7667 }
7668 return result;
7669}
7670
7671static enum yytokentype
7672set_number_literal(struct parser_params *p, VALUE v,
7673 enum yytokentype type, int suffix)
7674{
7675 if (suffix & NUM_SUFFIX_I) {
7676 v = rb_complex_raw(INT2FIX(0), v);
7677 type = tIMAGINARY;
7678 }
7679 set_yylval_literal(v);
7680 SET_LEX_STATE(EXPR_END);
7681 return type;
7682}
7683
7684static enum yytokentype
7685set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7686{
7687 enum yytokentype type = tINTEGER;
7688 if (suffix & NUM_SUFFIX_R) {
7689 v = rb_rational_raw1(v);
7690 type = tRATIONAL;
7691 }
7692 return set_number_literal(p, v, type, suffix);
7693}
7694
7695#ifdef RIPPER
7696static void
7697dispatch_heredoc_end(struct parser_params *p)
7698{
7699 VALUE str;
7700 if (has_delayed_token(p))
7701 dispatch_delayed_token(p, tSTRING_CONTENT);
7702 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7703 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7704 lex_goto_eol(p);
7705 token_flush(p);
7706}
7707
7708#else
7709#define dispatch_heredoc_end(p) ((void)0)
7710#endif
7711
7712static enum yytokentype
7713here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7714{
7715 int c, func, indent = 0;
7716 const char *eos, *ptr, *ptr_end;
7717 long len;
7718 VALUE str = 0;
7719 rb_encoding *enc = p->enc;
7720 rb_encoding *base_enc = 0;
7721 int bol;
7722
7723 eos = RSTRING_PTR(here->lastline) + here->offset;
7724 len = here->length;
7725 indent = (func = here->func) & STR_FUNC_INDENT;
7726
7727 if ((c = nextc(p)) == -1) {
7728 error:
7729#ifdef RIPPER
7730 if (!has_delayed_token(p)) {
7731 dispatch_scan_event(p, tSTRING_CONTENT);
7732 }
7733 else {
7734 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7735 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7736 int cr = ENC_CODERANGE_UNKNOWN;
7737 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7738 if (cr != ENC_CODERANGE_7BIT &&
7739 p->enc == rb_usascii_encoding() &&
7740 enc != rb_utf8_encoding()) {
7741 enc = rb_ascii8bit_encoding();
7742 }
7743 }
7744 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7745 }
7746 dispatch_delayed_token(p, tSTRING_CONTENT);
7747 }
7748 lex_goto_eol(p);
7749#endif
7750 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7751 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7752 (int)len, eos);
7753 token_flush(p);
7754 p->lex.strterm = 0;
7755 SET_LEX_STATE(EXPR_END);
7756 return tSTRING_END;
7757 }
7758 bol = was_bol(p);
7759 if (!bol) {
7760 /* not beginning of line, cannot be the terminator */
7761 }
7762 else if (p->heredoc_line_indent == -1) {
7763 /* `heredoc_line_indent == -1` means
7764 * - "after an interpolation in the same line", or
7765 * - "in a continuing line"
7766 */
7767 p->heredoc_line_indent = 0;
7768 }
7769 else if (whole_match_p(p, eos, len, indent)) {
7770 dispatch_heredoc_end(p);
7771 restore:
7772 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7773 token_flush(p);
7774 p->lex.strterm = 0;
7775 SET_LEX_STATE(EXPR_END);
7776 return tSTRING_END;
7777 }
7778
7779 if (!(func & STR_FUNC_EXPAND)) {
7780 do {
7781 ptr = RSTRING_PTR(p->lex.lastline);
7782 ptr_end = p->lex.pend;
7783 if (ptr_end > ptr) {
7784 switch (ptr_end[-1]) {
7785 case '\n':
7786 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7787 ptr_end++;
7788 break;
7789 }
7790 case '\r':
7791 --ptr_end;
7792 }
7793 }
7794
7795 if (p->heredoc_indent > 0) {
7796 long i = 0;
7797 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7798 i++;
7799 p->heredoc_line_indent = 0;
7800 }
7801
7802 if (str)
7803 rb_str_cat(str, ptr, ptr_end - ptr);
7804 else
7805 str = STR_NEW(ptr, ptr_end - ptr);
7806 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7807 lex_goto_eol(p);
7808 if (p->heredoc_indent > 0) {
7809 goto flush_str;
7810 }
7811 if (nextc(p) == -1) {
7812 if (str) {
7813 str = 0;
7814 }
7815 goto error;
7816 }
7817 } while (!whole_match_p(p, eos, len, indent));
7818 }
7819 else {
7820 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7821 newtok(p);
7822 if (c == '#') {
7823 int t = parser_peek_variable_name(p);
7824 if (p->heredoc_line_indent != -1) {
7825 if (p->heredoc_indent > p->heredoc_line_indent) {
7826 p->heredoc_indent = p->heredoc_line_indent;
7827 }
7828 p->heredoc_line_indent = -1;
7829 }
7830 if (t) return t;
7831 tokadd(p, '#');
7832 c = nextc(p);
7833 }
7834 do {
7835 pushback(p, c);
7836 enc = p->enc;
7837 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7838 if (p->eofp) goto error;
7839 goto restore;
7840 }
7841 if (c != '\n') {
7842 if (c == '\\') p->heredoc_line_indent = -1;
7843 flush:
7844 str = STR_NEW3(tok(p), toklen(p), enc, func);
7845 flush_str:
7846 set_yylval_str(str);
7847#ifndef RIPPER
7848 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7849#endif
7850 flush_string_content(p, enc);
7851 return tSTRING_CONTENT;
7852 }
7853 tokadd(p, nextc(p));
7854 if (p->heredoc_indent > 0) {
7855 lex_goto_eol(p);
7856 goto flush;
7857 }
7858 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
7859 if ((c = nextc(p)) == -1) goto error;
7860 } while (!whole_match_p(p, eos, len, indent));
7861 str = STR_NEW3(tok(p), toklen(p), enc, func);
7862 }
7863 dispatch_heredoc_end(p);
7864#ifdef RIPPER
7865 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
7866 yylval.val, str);
7867#endif
7868 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7869 token_flush(p);
7870 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
7871 set_yylval_str(str);
7872#ifndef RIPPER
7873 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7874#endif
7875 return tSTRING_CONTENT;
7876}
7877
7878#include "lex.c"
7879
7880static int
7881arg_ambiguous(struct parser_params *p, char c)
7882{
7883#ifndef RIPPER
7884 if (c == '/') {
7885 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
7886 }
7887 else {
7888 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
7889 }
7890#else
7891 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
7892#endif
7893 return TRUE;
7894}
7895
7896static ID
7897#ifndef RIPPER
7898formal_argument(struct parser_params *p, ID lhs)
7899#else
7900formal_argument(struct parser_params *p, VALUE lhs)
7901#endif
7902{
7903 switch (id_type(get_id(lhs))) {
7904 case ID_LOCAL:
7905 break;
7906#ifndef RIPPER
7907# define ERR(mesg) yyerror0(mesg)
7908#else
7909# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
7910#endif
7911 case ID_CONST:
7912 ERR("formal argument cannot be a constant");
7913 return 0;
7914 case ID_INSTANCE:
7915 ERR("formal argument cannot be an instance variable");
7916 return 0;
7917 case ID_GLOBAL:
7918 ERR("formal argument cannot be a global variable");
7919 return 0;
7920 case ID_CLASS:
7921 ERR("formal argument cannot be a class variable");
7922 return 0;
7923 default:
7924 ERR("formal argument must be local variable");
7925 return 0;
7926#undef ERR
7927 }
7928 shadowing_lvar(p, lhs);
7929 return lhs;
7930}
7931
7932static int
7933lvar_defined(struct parser_params *p, ID id)
7934{
7935 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
7936}
7937
7938/* emacsen -*- hack */
7939static long
7940parser_encode_length(struct parser_params *p, const char *name, long len)
7941{
7942 long nlen;
7943
7944 if (len > 5 && name[nlen = len - 5] == '-') {
7945 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
7946 return nlen;
7947 }
7948 if (len > 4 && name[nlen = len - 4] == '-') {
7949 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
7950 return nlen;
7951 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
7952 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
7953 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
7954 return nlen;
7955 }
7956 return len;
7957}
7958
7959static void
7960parser_set_encode(struct parser_params *p, const char *name)
7961{
7962 int idx = rb_enc_find_index(name);
7963 rb_encoding *enc;
7964 VALUE excargs[3];
7965
7966 if (idx < 0) {
7967 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
7968 error:
7969 excargs[0] = rb_eArgError;
7970 excargs[2] = rb_make_backtrace();
7971 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
7972 rb_exc_raise(rb_make_exception(3, excargs));
7973 }
7974 enc = rb_enc_from_index(idx);
7975 if (!rb_enc_asciicompat(enc)) {
7976 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
7977 goto error;
7978 }
7979 p->enc = enc;
7980#ifndef RIPPER
7981 if (p->debug_lines) {
7982 VALUE lines = p->debug_lines;
7983 long i, n = RARRAY_LEN(lines);
7984 for (i = 0; i < n; ++i) {
7985 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
7986 }
7987 }
7988#endif
7989}
7990
7991static int
7992comment_at_top(struct parser_params *p)
7993{
7994 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
7995 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
7996 while (ptr < ptr_end) {
7997 if (!ISSPACE(*ptr)) return 0;
7998 ptr++;
7999 }
8000 return 1;
8001}
8002
8003typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
8004typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
8005
8006static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
8007
8008static void
8009magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
8010{
8011 if (!comment_at_top(p)) {
8012 return;
8013 }
8014 parser_set_encode(p, val);
8015}
8016
8017static int
8018parser_get_bool(struct parser_params *p, const char *name, const char *val)
8019{
8020 switch (*val) {
8021 case 't': case 'T':
8022 if (STRCASECMP(val, "true") == 0) {
8023 return TRUE;
8024 }
8025 break;
8026 case 'f': case 'F':
8027 if (STRCASECMP(val, "false") == 0) {
8028 return FALSE;
8029 }
8030 break;
8031 }
8032 return parser_invalid_pragma_value(p, name, val);
8033}
8034
8035static int
8036parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
8037{
8038 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
8039 return -1;
8040}
8041
8042static void
8043parser_set_token_info(struct parser_params *p, const char *name, const char *val)
8044{
8045 int b = parser_get_bool(p, name, val);
8046 if (b >= 0) p->token_info_enabled = b;
8047}
8048
8049static void
8050parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
8051{
8052 int b;
8053
8054 if (p->token_seen) {
8055 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
8056 return;
8057 }
8058
8059 b = parser_get_bool(p, name, val);
8060 if (b < 0) return;
8061
8062 if (!p->compile_option)
8063 p->compile_option = rb_obj_hide(rb_ident_hash_new());
8064 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
8065 (b ? Qtrue : Qfalse));
8066}
8067
8068static void
8069parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
8070{
8071 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
8072 if (*s == ' ' || *s == '\t') continue;
8073 if (*s == '#') break;
8074 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
8075 return;
8076 }
8077
8078 switch (*val) {
8079 case 'n': case 'N':
8080 if (STRCASECMP(val, "none") == 0) {
8081 p->ctxt.shareable_constant_value = shareable_none;
8082 return;
8083 }
8084 break;
8085 case 'l': case 'L':
8086 if (STRCASECMP(val, "literal") == 0) {
8087 p->ctxt.shareable_constant_value = shareable_literal;
8088 return;
8089 }
8090 break;
8091 case 'e': case 'E':
8092 if (STRCASECMP(val, "experimental_copy") == 0) {
8093 p->ctxt.shareable_constant_value = shareable_copy;
8094 return;
8095 }
8096 if (STRCASECMP(val, "experimental_everything") == 0) {
8097 p->ctxt.shareable_constant_value = shareable_everything;
8098 return;
8099 }
8100 break;
8101 }
8102 parser_invalid_pragma_value(p, name, val);
8103}
8104
8105# if WARN_PAST_SCOPE
8106static void
8107parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
8108{
8109 int b = parser_get_bool(p, name, val);
8110 if (b >= 0) p->past_scope_enabled = b;
8111}
8112# endif
8113
8114struct magic_comment {
8115 const char *name;
8116 rb_magic_comment_setter_t func;
8117 rb_magic_comment_length_t length;
8118};
8119
8120static const struct magic_comment magic_comments[] = {
8121 {"coding", magic_comment_encoding, parser_encode_length},
8122 {"encoding", magic_comment_encoding, parser_encode_length},
8123 {"frozen_string_literal", parser_set_compile_option_flag},
8124 {"shareable_constant_value", parser_set_shareable_constant_value},
8125 {"warn_indent", parser_set_token_info},
8126# if WARN_PAST_SCOPE
8127 {"warn_past_scope", parser_set_past_scope},
8128# endif
8129};
8130
8131static const char *
8132magic_comment_marker(const char *str, long len)
8133{
8134 long i = 2;
8135
8136 while (i < len) {
8137 switch (str[i]) {
8138 case '-':
8139 if (str[i-1] == '*' && str[i-2] == '-') {
8140 return str + i + 1;
8141 }
8142 i += 2;
8143 break;
8144 case '*':
8145 if (i + 1 >= len) return 0;
8146 if (str[i+1] != '-') {
8147 i += 4;
8148 }
8149 else if (str[i-1] != '-') {
8150 i += 2;
8151 }
8152 else {
8153 return str + i + 2;
8154 }
8155 break;
8156 default:
8157 i += 3;
8158 break;
8159 }
8160 }
8161 return 0;
8162}
8163
8164static int
8165parser_magic_comment(struct parser_params *p, const char *str, long len)
8166{
8167 int indicator = 0;
8168 VALUE name = 0, val = 0;
8169 const char *beg, *end, *vbeg, *vend;
8170#define str_copy(_s, _p, _n) ((_s) \
8171 ? (void)(rb_str_resize((_s), (_n)), \
8172 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
8173 : (void)((_s) = STR_NEW((_p), (_n))))
8174
8175 if (len <= 7) return FALSE;
8176 if (!!(beg = magic_comment_marker(str, len))) {
8177 if (!(end = magic_comment_marker(beg, str + len - beg)))
8178 return FALSE;
8179 indicator = TRUE;
8180 str = beg;
8181 len = end - beg - 3;
8182 }
8183
8184 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
8185 while (len > 0) {
8186 const struct magic_comment *mc = magic_comments;
8187 char *s;
8188 int i;
8189 long n = 0;
8190
8191 for (; len > 0 && *str; str++, --len) {
8192 switch (*str) {
8193 case '\'': case '"': case ':': case ';':
8194 continue;
8195 }
8196 if (!ISSPACE(*str)) break;
8197 }
8198 for (beg = str; len > 0; str++, --len) {
8199 switch (*str) {
8200 case '\'': case '"': case ':': case ';':
8201 break;
8202 default:
8203 if (ISSPACE(*str)) break;
8204 continue;
8205 }
8206 break;
8207 }
8208 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
8209 if (!len) break;
8210 if (*str != ':') {
8211 if (!indicator) return FALSE;
8212 continue;
8213 }
8214
8215 do str++; while (--len > 0 && ISSPACE(*str));
8216 if (!len) break;
8217 if (*str == '"') {
8218 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
8219 if (*str == '\\') {
8220 --len;
8221 ++str;
8222 }
8223 }
8224 vend = str;
8225 if (len) {
8226 --len;
8227 ++str;
8228 }
8229 }
8230 else {
8231 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
8232 vend = str;
8233 }
8234 if (indicator) {
8235 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
8236 }
8237 else {
8238 while (len > 0 && (ISSPACE(*str))) --len, str++;
8239 if (len) return FALSE;
8240 }
8241
8242 n = end - beg;
8243 str_copy(name, beg, n);
8244 s = RSTRING_PTR(name);
8245 for (i = 0; i < n; ++i) {
8246 if (s[i] == '-') s[i] = '_';
8247 }
8248 do {
8249 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
8250 n = vend - vbeg;
8251 if (mc->length) {
8252 n = (*mc->length)(p, vbeg, n);
8253 }
8254 str_copy(val, vbeg, n);
8255 (*mc->func)(p, mc->name, RSTRING_PTR(val));
8256 break;
8257 }
8258 } while (++mc < magic_comments + numberof(magic_comments));
8259#ifdef RIPPER
8260 str_copy(val, vbeg, vend - vbeg);
8261 dispatch2(magic_comment, name, val);
8262#endif
8263 }
8264
8265 return TRUE;
8266}
8267
8268static void
8269set_file_encoding(struct parser_params *p, const char *str, const char *send)
8270{
8271 int sep = 0;
8272 const char *beg = str;
8273 VALUE s;
8274
8275 for (;;) {
8276 if (send - str <= 6) return;
8277 switch (str[6]) {
8278 case 'C': case 'c': str += 6; continue;
8279 case 'O': case 'o': str += 5; continue;
8280 case 'D': case 'd': str += 4; continue;
8281 case 'I': case 'i': str += 3; continue;
8282 case 'N': case 'n': str += 2; continue;
8283 case 'G': case 'g': str += 1; continue;
8284 case '=': case ':':
8285 sep = 1;
8286 str += 6;
8287 break;
8288 default:
8289 str += 6;
8290 if (ISSPACE(*str)) break;
8291 continue;
8292 }
8293 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8294 }
8295 for (;;) {
8296 do {
8297 if (++str >= send) return;
8298 } while (ISSPACE(*str));
8299 if (sep) break;
8300 if (*str != '=' && *str != ':') return;
8301 sep = 1;
8302 str++;
8303 }
8304 beg = str;
8305 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8306 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8307 parser_set_encode(p, RSTRING_PTR(s));
8308 rb_str_resize(s, 0);
8309}
8310
8311static void
8312parser_prepare(struct parser_params *p)
8313{
8314 int c = nextc(p);
8315 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8316 switch (c) {
8317 case '#':
8318 if (peek(p, '!')) p->has_shebang = 1;
8319 break;
8320 case 0xef: /* UTF-8 BOM marker */
8321 if (p->lex.pend - p->lex.pcur >= 2 &&
8322 (unsigned char)p->lex.pcur[0] == 0xbb &&
8323 (unsigned char)p->lex.pcur[1] == 0xbf) {
8324 p->enc = rb_utf8_encoding();
8325 p->lex.pcur += 2;
8326 p->lex.pbeg = p->lex.pcur;
8327 return;
8328 }
8329 break;
8330 case EOF:
8331 return;
8332 }
8333 pushback(p, c);
8334 p->enc = rb_enc_get(p->lex.lastline);
8335}
8336
8337#ifndef RIPPER
8338#define ambiguous_operator(tok, op, syn) ( \
8339 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8340 rb_warning0("even though it seems like "syn""))
8341#else
8342#define ambiguous_operator(tok, op, syn) \
8343 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8344#endif
8345#define warn_balanced(tok, op, syn) ((void) \
8346 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8347 space_seen && !ISSPACE(c) && \
8348 (ambiguous_operator(tok, op, syn), 0)), \
8349 (enum yytokentype)(tok))
8350
8351static VALUE
8352parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8353{
8354 VALUE v;
8355 char *point = &str[seen_point];
8356 size_t fraclen = len-seen_point-1;
8357 memmove(point, point+1, fraclen+1);
8358 v = rb_cstr_to_inum(str, 10, FALSE);
8359 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8360}
8361
8362static enum yytokentype
8363no_digits(struct parser_params *p)
8364{
8365 yyerror0("numeric literal without digits");
8366 if (peek(p, '_')) nextc(p);
8367 /* dummy 0, for tUMINUS_NUM at numeric */
8368 return set_integer_literal(p, INT2FIX(0), 0);
8369}
8370
8371static enum yytokentype
8372parse_numeric(struct parser_params *p, int c)
8373{
8374 int is_float, seen_point, seen_e, nondigit;
8375 int suffix;
8376
8377 is_float = seen_point = seen_e = nondigit = 0;
8378 SET_LEX_STATE(EXPR_END);
8379 newtok(p);
8380 if (c == '-' || c == '+') {
8381 tokadd(p, c);
8382 c = nextc(p);
8383 }
8384 if (c == '0') {
8385 int start = toklen(p);
8386 c = nextc(p);
8387 if (c == 'x' || c == 'X') {
8388 /* hexadecimal */
8389 c = nextc(p);
8390 if (c != -1 && ISXDIGIT(c)) {
8391 do {
8392 if (c == '_') {
8393 if (nondigit) break;
8394 nondigit = c;
8395 continue;
8396 }
8397 if (!ISXDIGIT(c)) break;
8398 nondigit = 0;
8399 tokadd(p, c);
8400 } while ((c = nextc(p)) != -1);
8401 }
8402 pushback(p, c);
8403 tokfix(p);
8404 if (toklen(p) == start) {
8405 return no_digits(p);
8406 }
8407 else if (nondigit) goto trailing_uc;
8408 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8409 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8410 }
8411 if (c == 'b' || c == 'B') {
8412 /* binary */
8413 c = nextc(p);
8414 if (c == '0' || c == '1') {
8415 do {
8416 if (c == '_') {
8417 if (nondigit) break;
8418 nondigit = c;
8419 continue;
8420 }
8421 if (c != '0' && c != '1') break;
8422 nondigit = 0;
8423 tokadd(p, c);
8424 } while ((c = nextc(p)) != -1);
8425 }
8426 pushback(p, c);
8427 tokfix(p);
8428 if (toklen(p) == start) {
8429 return no_digits(p);
8430 }
8431 else if (nondigit) goto trailing_uc;
8432 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8433 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8434 }
8435 if (c == 'd' || c == 'D') {
8436 /* decimal */
8437 c = nextc(p);
8438 if (c != -1 && ISDIGIT(c)) {
8439 do {
8440 if (c == '_') {
8441 if (nondigit) break;
8442 nondigit = c;
8443 continue;
8444 }
8445 if (!ISDIGIT(c)) break;
8446 nondigit = 0;
8447 tokadd(p, c);
8448 } while ((c = nextc(p)) != -1);
8449 }
8450 pushback(p, c);
8451 tokfix(p);
8452 if (toklen(p) == start) {
8453 return no_digits(p);
8454 }
8455 else if (nondigit) goto trailing_uc;
8456 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8457 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8458 }
8459 if (c == '_') {
8460 /* 0_0 */
8461 goto octal_number;
8462 }
8463 if (c == 'o' || c == 'O') {
8464 /* prefixed octal */
8465 c = nextc(p);
8466 if (c == -1 || c == '_' || !ISDIGIT(c)) {
8467 return no_digits(p);
8468 }
8469 }
8470 if (c >= '0' && c <= '7') {
8471 /* octal */
8472 octal_number:
8473 do {
8474 if (c == '_') {
8475 if (nondigit) break;
8476 nondigit = c;
8477 continue;
8478 }
8479 if (c < '0' || c > '9') break;
8480 if (c > '7') goto invalid_octal;
8481 nondigit = 0;
8482 tokadd(p, c);
8483 } while ((c = nextc(p)) != -1);
8484 if (toklen(p) > start) {
8485 pushback(p, c);
8486 tokfix(p);
8487 if (nondigit) goto trailing_uc;
8488 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8489 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8490 }
8491 if (nondigit) {
8492 pushback(p, c);
8493 goto trailing_uc;
8494 }
8495 }
8496 if (c > '7' && c <= '9') {
8497 invalid_octal:
8498 yyerror0("Invalid octal digit");
8499 }
8500 else if (c == '.' || c == 'e' || c == 'E') {
8501 tokadd(p, '0');
8502 }
8503 else {
8504 pushback(p, c);
8505 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8506 return set_integer_literal(p, INT2FIX(0), suffix);
8507 }
8508 }
8509
8510 for (;;) {
8511 switch (c) {
8512 case '0': case '1': case '2': case '3': case '4':
8513 case '5': case '6': case '7': case '8': case '9':
8514 nondigit = 0;
8515 tokadd(p, c);
8516 break;
8517
8518 case '.':
8519 if (nondigit) goto trailing_uc;
8520 if (seen_point || seen_e) {
8521 goto decode_num;
8522 }
8523 else {
8524 int c0 = nextc(p);
8525 if (c0 == -1 || !ISDIGIT(c0)) {
8526 pushback(p, c0);
8527 goto decode_num;
8528 }
8529 c = c0;
8530 }
8531 seen_point = toklen(p);
8532 tokadd(p, '.');
8533 tokadd(p, c);
8534 is_float++;
8535 nondigit = 0;
8536 break;
8537
8538 case 'e':
8539 case 'E':
8540 if (nondigit) {
8541 pushback(p, c);
8542 c = nondigit;
8543 goto decode_num;
8544 }
8545 if (seen_e) {
8546 goto decode_num;
8547 }
8548 nondigit = c;
8549 c = nextc(p);
8550 if (c != '-' && c != '+' && !ISDIGIT(c)) {
8551 pushback(p, c);
8552 nondigit = 0;
8553 goto decode_num;
8554 }
8555 tokadd(p, nondigit);
8556 seen_e++;
8557 is_float++;
8558 tokadd(p, c);
8559 nondigit = (c == '-' || c == '+') ? c : 0;
8560 break;
8561
8562 case '_': /* `_' in number just ignored */
8563 if (nondigit) goto decode_num;
8564 nondigit = c;
8565 break;
8566
8567 default:
8568 goto decode_num;
8569 }
8570 c = nextc(p);
8571 }
8572
8573 decode_num:
8574 pushback(p, c);
8575 if (nondigit) {
8576 trailing_uc:
8577 literal_flush(p, p->lex.pcur - 1);
8578 YYLTYPE loc = RUBY_INIT_YYLLOC();
8579 compile_error(p, "trailing `%c' in number", nondigit);
8580 parser_show_error_line(p, &loc);
8581 }
8582 tokfix(p);
8583 if (is_float) {
8584 enum yytokentype type = tFLOAT;
8585 VALUE v;
8586
8587 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8588 if (suffix & NUM_SUFFIX_R) {
8589 type = tRATIONAL;
8590 v = parse_rational(p, tok(p), toklen(p), seen_point);
8591 }
8592 else {
8593 double d = strtod(tok(p), 0);
8594 if (errno == ERANGE) {
8595 rb_warning1("Float %s out of range", WARN_S(tok(p)));
8596 errno = 0;
8597 }
8598 v = DBL2NUM(d);
8599 }
8600 return set_number_literal(p, v, type, suffix);
8601 }
8602 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8603 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8604}
8605
8606static enum yytokentype
8607parse_qmark(struct parser_params *p, int space_seen)
8608{
8609 rb_encoding *enc;
8610 register int c;
8611 VALUE lit;
8612
8613 if (IS_END()) {
8614 SET_LEX_STATE(EXPR_VALUE);
8615 return '?';
8616 }
8617 c = nextc(p);
8618 if (c == -1) {
8619 compile_error(p, "incomplete character syntax");
8620 return 0;
8621 }
8622 if (rb_enc_isspace(c, p->enc)) {
8623 if (!IS_ARG()) {
8624 int c2 = escaped_control_code(c);
8625 if (c2) {
8626 WARN_SPACE_CHAR(c2, "?");
8627 }
8628 }
8629 ternary:
8630 pushback(p, c);
8631 SET_LEX_STATE(EXPR_VALUE);
8632 return '?';
8633 }
8634 newtok(p);
8635 enc = p->enc;
8636 if (!parser_isascii(p)) {
8637 if (tokadd_mbchar(p, c) == -1) return 0;
8638 }
8639 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8640 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8641 if (space_seen) {
8642 const char *start = p->lex.pcur - 1, *ptr = start;
8643 do {
8644 int n = parser_precise_mbclen(p, ptr);
8645 if (n < 0) return -1;
8646 ptr += n;
8647 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8648 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8649 " a conditional operator, put a space after `?'",
8650 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8651 }
8652 goto ternary;
8653 }
8654 else if (c == '\\') {
8655 if (peek(p, 'u')) {
8656 nextc(p);
8657 enc = rb_utf8_encoding();
8658 tokadd_utf8(p, &enc, -1, 0, 0);
8659 }
8660 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8661 nextc(p);
8662 if (tokadd_mbchar(p, c) == -1) return 0;
8663 }
8664 else {
8665 c = read_escape(p, 0, &enc);
8666 tokadd(p, c);
8667 }
8668 }
8669 else {
8670 tokadd(p, c);
8671 }
8672 tokfix(p);
8673 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8674 set_yylval_str(lit);
8675 SET_LEX_STATE(EXPR_END);
8676 return tCHAR;
8677}
8678
8679static enum yytokentype
8680parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8681{
8682 register int c;
8683 const char *ptok = p->lex.pcur;
8684
8685 if (IS_BEG()) {
8686 int term;
8687 int paren;
8688
8689 c = nextc(p);
8690 quotation:
8691 if (c == -1 || !ISALNUM(c)) {
8692 term = c;
8693 if (!ISASCII(c)) goto unknown;
8694 c = 'Q';
8695 }
8696 else {
8697 term = nextc(p);
8698 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8699 unknown:
8700 pushback(p, term);
8701 c = parser_precise_mbclen(p, p->lex.pcur);
8702 if (c < 0) return 0;
8703 p->lex.pcur += c;
8704 yyerror0("unknown type of %string");
8705 return 0;
8706 }
8707 }
8708 if (term == -1) {
8709 compile_error(p, "unterminated quoted string meets end of file");
8710 return 0;
8711 }
8712 paren = term;
8713 if (term == '(') term = ')';
8714 else if (term == '[') term = ']';
8715 else if (term == '{') term = '}';
8716 else if (term == '<') term = '>';
8717 else paren = 0;
8718
8719 p->lex.ptok = ptok-1;
8720 switch (c) {
8721 case 'Q':
8722 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8723 return tSTRING_BEG;
8724
8725 case 'q':
8726 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8727 return tSTRING_BEG;
8728
8729 case 'W':
8730 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8731 return tWORDS_BEG;
8732
8733 case 'w':
8734 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8735 return tQWORDS_BEG;
8736
8737 case 'I':
8738 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8739 return tSYMBOLS_BEG;
8740
8741 case 'i':
8742 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8743 return tQSYMBOLS_BEG;
8744
8745 case 'x':
8746 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8747 return tXSTRING_BEG;
8748
8749 case 'r':
8750 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8751 return tREGEXP_BEG;
8752
8753 case 's':
8754 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8755 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8756 return tSYMBEG;
8757
8758 default:
8759 yyerror0("unknown type of %string");
8760 return 0;
8761 }
8762 }
8763 if ((c = nextc(p)) == '=') {
8764 set_yylval_id('%');
8765 SET_LEX_STATE(EXPR_BEG);
8766 return tOP_ASGN;
8767 }
8768 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8769 goto quotation;
8770 }
8771 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8772 pushback(p, c);
8773 return warn_balanced('%', "%%", "string literal");
8774}
8775
8776static int
8777tokadd_ident(struct parser_params *p, int c)
8778{
8779 do {
8780 if (tokadd_mbchar(p, c) == -1) return -1;
8781 c = nextc(p);
8782 } while (parser_is_identchar(p));
8783 pushback(p, c);
8784 return 0;
8785}
8786
8787static ID
8788tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8789{
8790 ID ident = TOK_INTERN();
8791
8792 set_yylval_name(ident);
8793
8794 return ident;
8795}
8796
8797static int
8798parse_numvar(struct parser_params *p)
8799{
8800 size_t len;
8801 int overflow;
8802 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8803 const unsigned long nth_ref_max =
8804 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8805 /* NTH_REF is left-shifted to be ORed with back-ref flag and
8806 * turned into a Fixnum, in compile.c */
8807
8808 if (overflow || n > nth_ref_max) {
8809 /* compile_error()? */
8810 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8811 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8812 }
8813 else {
8814 return (int)n;
8815 }
8816}
8817
8818static enum yytokentype
8819parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8820{
8821 const char *ptr = p->lex.pcur;
8822 register int c;
8823
8824 SET_LEX_STATE(EXPR_END);
8825 p->lex.ptok = ptr - 1; /* from '$' */
8826 newtok(p);
8827 c = nextc(p);
8828 switch (c) {
8829 case '_': /* $_: last read line string */
8830 c = nextc(p);
8831 if (parser_is_identchar(p)) {
8832 tokadd(p, '$');
8833 tokadd(p, '_');
8834 break;
8835 }
8836 pushback(p, c);
8837 c = '_';
8838 /* fall through */
8839 case '~': /* $~: match-data */
8840 case '*': /* $*: argv */
8841 case '$': /* $$: pid */
8842 case '?': /* $?: last status */
8843 case '!': /* $!: error string */
8844 case '@': /* $@: error position */
8845 case '/': /* $/: input record separator */
8846 case '\\': /* $\: output record separator */
8847 case ';': /* $;: field separator */
8848 case ',': /* $,: output field separator */
8849 case '.': /* $.: last read line number */
8850 case '=': /* $=: ignorecase */
8851 case ':': /* $:: load path */
8852 case '<': /* $<: reading filename */
8853 case '>': /* $>: default output handle */
8854 case '\"': /* $": already loaded files */
8855 tokadd(p, '$');
8856 tokadd(p, c);
8857 goto gvar;
8858
8859 case '-':
8860 tokadd(p, '$');
8861 tokadd(p, c);
8862 c = nextc(p);
8863 if (parser_is_identchar(p)) {
8864 if (tokadd_mbchar(p, c) == -1) return 0;
8865 }
8866 else {
8867 pushback(p, c);
8868 pushback(p, '-');
8869 return '$';
8870 }
8871 gvar:
8872 set_yylval_name(TOK_INTERN());
8873 return tGVAR;
8874
8875 case '&': /* $&: last match */
8876 case '`': /* $`: string before last match */
8877 case '\'': /* $': string after last match */
8878 case '+': /* $+: string matches last paren. */
8879 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
8880 tokadd(p, '$');
8881 tokadd(p, c);
8882 goto gvar;
8883 }
8884 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
8885 return tBACK_REF;
8886
8887 case '1': case '2': case '3':
8888 case '4': case '5': case '6':
8889 case '7': case '8': case '9':
8890 tokadd(p, '$');
8891 do {
8892 tokadd(p, c);
8893 c = nextc(p);
8894 } while (c != -1 && ISDIGIT(c));
8895 pushback(p, c);
8896 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
8897 tokfix(p);
8898 c = parse_numvar(p);
8899 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
8900 return tNTH_REF;
8901
8902 default:
8903 if (!parser_is_identchar(p)) {
8904 YYLTYPE loc = RUBY_INIT_YYLLOC();
8905 if (c == -1 || ISSPACE(c)) {
8906 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
8907 }
8908 else {
8909 pushback(p, c);
8910 compile_error(p, "`$%c' is not allowed as a global variable name", c);
8911 }
8912 parser_show_error_line(p, &loc);
8913 set_yylval_noname();
8914 return tGVAR;
8915 }
8916 /* fall through */
8917 case '0':
8918 tokadd(p, '$');
8919 }
8920
8921 if (tokadd_ident(p, c)) return 0;
8922 SET_LEX_STATE(EXPR_END);
8923 tokenize_ident(p, last_state);
8924 return tGVAR;
8925}
8926
8927#ifndef RIPPER
8928static bool
8929parser_numbered_param(struct parser_params *p, int n)
8930{
8931 if (n < 0) return false;
8932
8933 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
8934 return false;
8935 }
8936 if (p->max_numparam == ORDINAL_PARAM) {
8937 compile_error(p, "ordinary parameter is defined");
8938 return false;
8939 }
8940 struct vtable *args = p->lvtbl->args;
8941 if (p->max_numparam < n) {
8942 p->max_numparam = n;
8943 }
8944 while (n > args->pos) {
8945 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
8946 }
8947 return true;
8948}
8949#endif
8950
8951static enum yytokentype
8952parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
8953{
8954 const char *ptr = p->lex.pcur;
8955 enum yytokentype result = tIVAR;
8956 register int c = nextc(p);
8957 YYLTYPE loc;
8958
8959 p->lex.ptok = ptr - 1; /* from '@' */
8960 newtok(p);
8961 tokadd(p, '@');
8962 if (c == '@') {
8963 result = tCVAR;
8964 tokadd(p, '@');
8965 c = nextc(p);
8966 }
8967 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
8968 if (c == -1 || !parser_is_identchar(p)) {
8969 pushback(p, c);
8970 RUBY_SET_YYLLOC(loc);
8971 if (result == tIVAR) {
8972 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
8973 }
8974 else {
8975 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
8976 }
8977 parser_show_error_line(p, &loc);
8978 set_yylval_noname();
8979 SET_LEX_STATE(EXPR_END);
8980 return result;
8981 }
8982 else if (ISDIGIT(c)) {
8983 pushback(p, c);
8984 RUBY_SET_YYLLOC(loc);
8985 if (result == tIVAR) {
8986 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
8987 }
8988 else {
8989 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
8990 }
8991 parser_show_error_line(p, &loc);
8992 set_yylval_noname();
8993 SET_LEX_STATE(EXPR_END);
8994 return result;
8995 }
8996
8997 if (tokadd_ident(p, c)) return 0;
8998 tokenize_ident(p, last_state);
8999 return result;
9000}
9001
9002static enum yytokentype
9003parse_ident(struct parser_params *p, int c, int cmd_state)
9004{
9005 enum yytokentype result;
9006 int mb = ENC_CODERANGE_7BIT;
9007 const enum lex_state_e last_state = p->lex.state;
9008 ID ident;
9009
9010 do {
9011 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
9012 if (tokadd_mbchar(p, c) == -1) return 0;
9013 c = nextc(p);
9014 } while (parser_is_identchar(p));
9015 if ((c == '!' || c == '?') && !peek(p, '=')) {
9016 result = tFID;
9017 tokadd(p, c);
9018 }
9019 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
9020 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
9021 result = tIDENTIFIER;
9022 tokadd(p, c);
9023 }
9024 else {
9025 result = tCONSTANT; /* assume provisionally */
9026 pushback(p, c);
9027 }
9028 tokfix(p);
9029
9030 if (IS_LABEL_POSSIBLE()) {
9031 if (IS_LABEL_SUFFIX(0)) {
9032 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
9033 nextc(p);
9034 set_yylval_name(TOK_INTERN());
9035 return tLABEL;
9036 }
9037 }
9038 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
9039 const struct kwtable *kw;
9040
9041 /* See if it is a reserved word. */
9042 kw = rb_reserved_word(tok(p), toklen(p));
9043 if (kw) {
9044 enum lex_state_e state = p->lex.state;
9045 if (IS_lex_state_for(state, EXPR_FNAME)) {
9046 SET_LEX_STATE(EXPR_ENDFN);
9047 set_yylval_name(rb_intern2(tok(p), toklen(p)));
9048 return kw->id[0];
9049 }
9050 SET_LEX_STATE(kw->state);
9051 if (IS_lex_state(EXPR_BEG)) {
9052 p->command_start = TRUE;
9053 }
9054 if (kw->id[0] == keyword_do) {
9055 if (lambda_beginning_p()) {
9056 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
9057 return keyword_do_LAMBDA;
9058 }
9059 if (COND_P()) return keyword_do_cond;
9060 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
9061 return keyword_do_block;
9062 return keyword_do;
9063 }
9064 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
9065 return kw->id[0];
9066 else {
9067 if (kw->id[0] != kw->id[1])
9068 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
9069 return kw->id[1];
9070 }
9071 }
9072 }
9073
9074 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
9075 if (cmd_state) {
9076 SET_LEX_STATE(EXPR_CMDARG);
9077 }
9078 else {
9079 SET_LEX_STATE(EXPR_ARG);
9080 }
9081 }
9082 else if (p->lex.state == EXPR_FNAME) {
9083 SET_LEX_STATE(EXPR_ENDFN);
9084 }
9085 else {
9086 SET_LEX_STATE(EXPR_END);
9087 }
9088
9089 ident = tokenize_ident(p, last_state);
9090 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
9091 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
9092 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
9093 lvar_defined(p, ident)) {
9094 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
9095 }
9096 return result;
9097}
9098
9099static enum yytokentype
9100parser_yylex(struct parser_params *p)
9101{
9102 register int c;
9103 int space_seen = 0;
9104 int cmd_state;
9105 int label;
9106 enum lex_state_e last_state;
9107 int fallthru = FALSE;
9108 int token_seen = p->token_seen;
9109
9110 if (p->lex.strterm) {
9111 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
9112 return here_document(p, &p->lex.strterm->u.heredoc);
9113 }
9114 else {
9115 token_flush(p);
9116 return parse_string(p, &p->lex.strterm->u.literal);
9117 }
9118 }
9119 cmd_state = p->command_start;
9120 p->command_start = FALSE;
9121 p->token_seen = TRUE;
9122 retry:
9123 last_state = p->lex.state;
9124#ifndef RIPPER
9125 token_flush(p);
9126#endif
9127 switch (c = nextc(p)) {
9128 case '\0': /* NUL */
9129 case '\004': /* ^D */
9130 case '\032': /* ^Z */
9131 case -1: /* end of script. */
9132 return 0;
9133
9134 /* white spaces */
9135 case '\r':
9136 if (!p->cr_seen) {
9137 p->cr_seen = TRUE;
9138 /* carried over with p->lex.nextline for nextc() */
9139 rb_warn0("encountered \\r in middle of line, treated as a mere space");
9140 }
9141 /* fall through */
9142 case ' ': case '\t': case '\f':
9143 case '\13': /* '\v' */
9144 space_seen = 1;
9145#ifdef RIPPER
9146 while ((c = nextc(p))) {
9147 switch (c) {
9148 case ' ': case '\t': case '\f': case '\r':
9149 case '\13': /* '\v' */
9150 break;
9151 default:
9152 goto outofloop;
9153 }
9154 }
9155 outofloop:
9156 pushback(p, c);
9157 dispatch_scan_event(p, tSP);
9158#endif
9159 goto retry;
9160
9161 case '#': /* it's a comment */
9162 p->token_seen = token_seen;
9163 /* no magic_comment in shebang line */
9164 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
9165 if (comment_at_top(p)) {
9166 set_file_encoding(p, p->lex.pcur, p->lex.pend);
9167 }
9168 }
9169 lex_goto_eol(p);
9170 dispatch_scan_event(p, tCOMMENT);
9171 fallthru = TRUE;
9172 /* fall through */
9173 case '\n':
9174 p->token_seen = token_seen;
9175 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
9176 !IS_lex_state(EXPR_LABELED));
9177 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
9178 if (!fallthru) {
9179 dispatch_scan_event(p, tIGNORED_NL);
9180 }
9181 fallthru = FALSE;
9182 if (!c && p->ctxt.in_kwarg) {
9183 goto normal_newline;
9184 }
9185 goto retry;
9186 }
9187 while (1) {
9188 switch (c = nextc(p)) {
9189 case ' ': case '\t': case '\f': case '\r':
9190 case '\13': /* '\v' */
9191 space_seen = 1;
9192 break;
9193 case '#':
9194 pushback(p, c);
9195 if (space_seen) dispatch_scan_event(p, tSP);
9196 goto retry;
9197 case '&':
9198 case '.': {
9199 dispatch_delayed_token(p, tIGNORED_NL);
9200 if (peek(p, '.') == (c == '&')) {
9201 pushback(p, c);
9202 dispatch_scan_event(p, tSP);
9203 goto retry;
9204 }
9205 }
9206 default:
9207 p->ruby_sourceline--;
9208 p->lex.nextline = p->lex.lastline;
9209 case -1: /* EOF no decrement*/
9210#ifndef RIPPER
9211 if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
9212 p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
9213 p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
9214 pushback(p, 1); /* always pushback */
9215 p->lex.ptok = p->lex.pcur;
9216#else
9217 lex_goto_eol(p);
9218 if (c != -1) {
9219 p->lex.ptok = p->lex.pcur;
9220 }
9221#endif
9222 goto normal_newline;
9223 }
9224 }
9225 normal_newline:
9226 p->command_start = TRUE;
9227 SET_LEX_STATE(EXPR_BEG);
9228 return '\n';
9229
9230 case '*':
9231 if ((c = nextc(p)) == '*') {
9232 if ((c = nextc(p)) == '=') {
9233 set_yylval_id(idPow);
9234 SET_LEX_STATE(EXPR_BEG);
9235 return tOP_ASGN;
9236 }
9237 pushback(p, c);
9238 if (IS_SPCARG(c)) {
9239 rb_warning0("`**' interpreted as argument prefix");
9240 c = tDSTAR;
9241 }
9242 else if (IS_BEG()) {
9243 c = tDSTAR;
9244 }
9245 else {
9246 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
9247 }
9248 }
9249 else {
9250 if (c == '=') {
9251 set_yylval_id('*');
9252 SET_LEX_STATE(EXPR_BEG);
9253 return tOP_ASGN;
9254 }
9255 pushback(p, c);
9256 if (IS_SPCARG(c)) {
9257 rb_warning0("`*' interpreted as argument prefix");
9258 c = tSTAR;
9259 }
9260 else if (IS_BEG()) {
9261 c = tSTAR;
9262 }
9263 else {
9264 c = warn_balanced('*', "*", "argument prefix");
9265 }
9266 }
9267 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9268 return c;
9269
9270 case '!':
9271 c = nextc(p);
9272 if (IS_AFTER_OPERATOR()) {
9273 SET_LEX_STATE(EXPR_ARG);
9274 if (c == '@') {
9275 return '!';
9276 }
9277 }
9278 else {
9279 SET_LEX_STATE(EXPR_BEG);
9280 }
9281 if (c == '=') {
9282 return tNEQ;
9283 }
9284 if (c == '~') {
9285 return tNMATCH;
9286 }
9287 pushback(p, c);
9288 return '!';
9289
9290 case '=':
9291 if (was_bol(p)) {
9292 /* skip embedded rd document */
9293 if (word_match_p(p, "begin", 5)) {
9294 int first_p = TRUE;
9295
9296 lex_goto_eol(p);
9297 dispatch_scan_event(p, tEMBDOC_BEG);
9298 for (;;) {
9299 lex_goto_eol(p);
9300 if (!first_p) {
9301 dispatch_scan_event(p, tEMBDOC);
9302 }
9303 first_p = FALSE;
9304 c = nextc(p);
9305 if (c == -1) {
9306 compile_error(p, "embedded document meets end of file");
9307 return 0;
9308 }
9309 if (c == '=' && word_match_p(p, "end", 3)) {
9310 break;
9311 }
9312 pushback(p, c);
9313 }
9314 lex_goto_eol(p);
9315 dispatch_scan_event(p, tEMBDOC_END);
9316 goto retry;
9317 }
9318 }
9319
9320 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9321 if ((c = nextc(p)) == '=') {
9322 if ((c = nextc(p)) == '=') {
9323 return tEQQ;
9324 }
9325 pushback(p, c);
9326 return tEQ;
9327 }
9328 if (c == '~') {
9329 return tMATCH;
9330 }
9331 else if (c == '>') {
9332 return tASSOC;
9333 }
9334 pushback(p, c);
9335 return '=';
9336
9337 case '<':
9338 c = nextc(p);
9339 if (c == '<' &&
9340 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9341 !IS_END() &&
9342 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9343 int token = heredoc_identifier(p);
9344 if (token) return token < 0 ? 0 : token;
9345 }
9346 if (IS_AFTER_OPERATOR()) {
9347 SET_LEX_STATE(EXPR_ARG);
9348 }
9349 else {
9350 if (IS_lex_state(EXPR_CLASS))
9351 p->command_start = TRUE;
9352 SET_LEX_STATE(EXPR_BEG);
9353 }
9354 if (c == '=') {
9355 if ((c = nextc(p)) == '>') {
9356 return tCMP;
9357 }
9358 pushback(p, c);
9359 return tLEQ;
9360 }
9361 if (c == '<') {
9362 if ((c = nextc(p)) == '=') {
9363 set_yylval_id(idLTLT);
9364 SET_LEX_STATE(EXPR_BEG);
9365 return tOP_ASGN;
9366 }
9367 pushback(p, c);
9368 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9369 }
9370 pushback(p, c);
9371 return '<';
9372
9373 case '>':
9374 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9375 if ((c = nextc(p)) == '=') {
9376 return tGEQ;
9377 }
9378 if (c == '>') {
9379 if ((c = nextc(p)) == '=') {
9380 set_yylval_id(idGTGT);
9381 SET_LEX_STATE(EXPR_BEG);
9382 return tOP_ASGN;
9383 }
9384 pushback(p, c);
9385 return tRSHFT;
9386 }
9387 pushback(p, c);
9388 return '>';
9389
9390 case '"':
9391 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9392 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9393 p->lex.ptok = p->lex.pcur-1;
9394 return tSTRING_BEG;
9395
9396 case '`':
9397 if (IS_lex_state(EXPR_FNAME)) {
9398 SET_LEX_STATE(EXPR_ENDFN);
9399 return c;
9400 }
9401 if (IS_lex_state(EXPR_DOT)) {
9402 if (cmd_state)
9403 SET_LEX_STATE(EXPR_CMDARG);
9404 else
9405 SET_LEX_STATE(EXPR_ARG);
9406 return c;
9407 }
9408 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9409 return tXSTRING_BEG;
9410
9411 case '\'':
9412 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9413 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9414 p->lex.ptok = p->lex.pcur-1;
9415 return tSTRING_BEG;
9416
9417 case '?':
9418 return parse_qmark(p, space_seen);
9419
9420 case '&':
9421 if ((c = nextc(p)) == '&') {
9422 SET_LEX_STATE(EXPR_BEG);
9423 if ((c = nextc(p)) == '=') {
9424 set_yylval_id(idANDOP);
9425 SET_LEX_STATE(EXPR_BEG);
9426 return tOP_ASGN;
9427 }
9428 pushback(p, c);
9429 return tANDOP;
9430 }
9431 else if (c == '=') {
9432 set_yylval_id('&');
9433 SET_LEX_STATE(EXPR_BEG);
9434 return tOP_ASGN;
9435 }
9436 else if (c == '.') {
9437 set_yylval_id(idANDDOT);
9438 SET_LEX_STATE(EXPR_DOT);
9439 return tANDDOT;
9440 }
9441 pushback(p, c);
9442 if (IS_SPCARG(c)) {
9443 if ((c != ':') ||
9444 (c = peekc_n(p, 1)) == -1 ||
9445 !(c == '\'' || c == '"' ||
9446 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9447 rb_warning0("`&' interpreted as argument prefix");
9448 }
9449 c = tAMPER;
9450 }
9451 else if (IS_BEG()) {
9452 c = tAMPER;
9453 }
9454 else {
9455 c = warn_balanced('&', "&", "argument prefix");
9456 }
9457 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9458 return c;
9459
9460 case '|':
9461 if ((c = nextc(p)) == '|') {
9462 SET_LEX_STATE(EXPR_BEG);
9463 if ((c = nextc(p)) == '=') {
9464 set_yylval_id(idOROP);
9465 SET_LEX_STATE(EXPR_BEG);
9466 return tOP_ASGN;
9467 }
9468 pushback(p, c);
9469 if (IS_lex_state_for(last_state, EXPR_BEG)) {
9470 c = '|';
9471 pushback(p, '|');
9472 return c;
9473 }
9474 return tOROP;
9475 }
9476 if (c == '=') {
9477 set_yylval_id('|');
9478 SET_LEX_STATE(EXPR_BEG);
9479 return tOP_ASGN;
9480 }
9481 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9482 pushback(p, c);
9483 return '|';
9484
9485 case '+':
9486 c = nextc(p);
9487 if (IS_AFTER_OPERATOR()) {
9488 SET_LEX_STATE(EXPR_ARG);
9489 if (c == '@') {
9490 return tUPLUS;
9491 }
9492 pushback(p, c);
9493 return '+';
9494 }
9495 if (c == '=') {
9496 set_yylval_id('+');
9497 SET_LEX_STATE(EXPR_BEG);
9498 return tOP_ASGN;
9499 }
9500 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9501 SET_LEX_STATE(EXPR_BEG);
9502 pushback(p, c);
9503 if (c != -1 && ISDIGIT(c)) {
9504 return parse_numeric(p, '+');
9505 }
9506 return tUPLUS;
9507 }
9508 SET_LEX_STATE(EXPR_BEG);
9509 pushback(p, c);
9510 return warn_balanced('+', "+", "unary operator");
9511
9512 case '-':
9513 c = nextc(p);
9514 if (IS_AFTER_OPERATOR()) {
9515 SET_LEX_STATE(EXPR_ARG);
9516 if (c == '@') {
9517 return tUMINUS;
9518 }
9519 pushback(p, c);
9520 return '-';
9521 }
9522 if (c == '=') {
9523 set_yylval_id('-');
9524 SET_LEX_STATE(EXPR_BEG);
9525 return tOP_ASGN;
9526 }
9527 if (c == '>') {
9528 SET_LEX_STATE(EXPR_ENDFN);
9529 return tLAMBDA;
9530 }
9531 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9532 SET_LEX_STATE(EXPR_BEG);
9533 pushback(p, c);
9534 if (c != -1 && ISDIGIT(c)) {
9535 return tUMINUS_NUM;
9536 }
9537 return tUMINUS;
9538 }
9539 SET_LEX_STATE(EXPR_BEG);
9540 pushback(p, c);
9541 return warn_balanced('-', "-", "unary operator");
9542
9543 case '.': {
9544 int is_beg = IS_BEG();
9545 SET_LEX_STATE(EXPR_BEG);
9546 if ((c = nextc(p)) == '.') {
9547 if ((c = nextc(p)) == '.') {
9548 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9549 rb_warn0("... at EOL, should be parenthesized?");
9550 }
9551 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
9552 if (IS_lex_state_for(last_state, EXPR_LABEL))
9553 return tDOT3;
9554 }
9555 return is_beg ? tBDOT3 : tDOT3;
9556 }
9557 pushback(p, c);
9558 return is_beg ? tBDOT2 : tDOT2;
9559 }
9560 pushback(p, c);
9561 if (c != -1 && ISDIGIT(c)) {
9562 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9563 parse_numeric(p, '.');
9564 if (ISDIGIT(prev)) {
9565 yyerror0("unexpected fraction part after numeric literal");
9566 }
9567 else {
9568 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9569 }
9570 SET_LEX_STATE(EXPR_END);
9571 p->lex.ptok = p->lex.pcur;
9572 goto retry;
9573 }
9574 set_yylval_id('.');
9575 SET_LEX_STATE(EXPR_DOT);
9576 return '.';
9577 }
9578
9579 case '0': case '1': case '2': case '3': case '4':
9580 case '5': case '6': case '7': case '8': case '9':
9581 return parse_numeric(p, c);
9582
9583 case ')':
9584 COND_POP();
9585 CMDARG_POP();
9586 SET_LEX_STATE(EXPR_ENDFN);
9587 p->lex.paren_nest--;
9588 return c;
9589
9590 case ']':
9591 COND_POP();
9592 CMDARG_POP();
9593 SET_LEX_STATE(EXPR_END);
9594 p->lex.paren_nest--;
9595 return c;
9596
9597 case '}':
9598 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9599 if (!p->lex.brace_nest--) return tSTRING_DEND;
9600 COND_POP();
9601 CMDARG_POP();
9602 SET_LEX_STATE(EXPR_END);
9603 p->lex.paren_nest--;
9604 return c;
9605
9606 case ':':
9607 c = nextc(p);
9608 if (c == ':') {
9609 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9610 SET_LEX_STATE(EXPR_BEG);
9611 return tCOLON3;
9612 }
9613 set_yylval_id(idCOLON2);
9614 SET_LEX_STATE(EXPR_DOT);
9615 return tCOLON2;
9616 }
9617 if (IS_END() || ISSPACE(c) || c == '#') {
9618 pushback(p, c);
9619 c = warn_balanced(':', ":", "symbol literal");
9620 SET_LEX_STATE(EXPR_BEG);
9621 return c;
9622 }
9623 switch (c) {
9624 case '\'':
9625 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9626 break;
9627 case '"':
9628 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9629 break;
9630 default:
9631 pushback(p, c);
9632 break;
9633 }
9634 SET_LEX_STATE(EXPR_FNAME);
9635 return tSYMBEG;
9636
9637 case '/':
9638 if (IS_BEG()) {
9639 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9640 return tREGEXP_BEG;
9641 }
9642 if ((c = nextc(p)) == '=') {
9643 set_yylval_id('/');
9644 SET_LEX_STATE(EXPR_BEG);
9645 return tOP_ASGN;
9646 }
9647 pushback(p, c);
9648 if (IS_SPCARG(c)) {
9649 arg_ambiguous(p, '/');
9650 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9651 return tREGEXP_BEG;
9652 }
9653 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9654 return warn_balanced('/', "/", "regexp literal");
9655
9656 case '^':
9657 if ((c = nextc(p)) == '=') {
9658 set_yylval_id('^');
9659 SET_LEX_STATE(EXPR_BEG);
9660 return tOP_ASGN;
9661 }
9662 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9663 pushback(p, c);
9664 return '^';
9665
9666 case ';':
9667 SET_LEX_STATE(EXPR_BEG);
9668 p->command_start = TRUE;
9669 return ';';
9670
9671 case ',':
9672 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9673 return ',';
9674
9675 case '~':
9676 if (IS_AFTER_OPERATOR()) {
9677 if ((c = nextc(p)) != '@') {
9678 pushback(p, c);
9679 }
9680 SET_LEX_STATE(EXPR_ARG);
9681 }
9682 else {
9683 SET_LEX_STATE(EXPR_BEG);
9684 }
9685 return '~';
9686
9687 case '(':
9688 if (IS_BEG()) {
9689 c = tLPAREN;
9690 }
9691 else if (!space_seen) {
9692 /* foo( ... ) => method call, no ambiguity */
9693 }
9694 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9695 c = tLPAREN_ARG;
9696 }
9697 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9698 rb_warning0("parentheses after method name is interpreted as "
9699 "an argument list, not a decomposed argument");
9700 }
9701 p->lex.paren_nest++;
9702 COND_PUSH(0);
9703 CMDARG_PUSH(0);
9704 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9705 return c;
9706
9707 case '[':
9708 p->lex.paren_nest++;
9709 if (IS_AFTER_OPERATOR()) {
9710 if ((c = nextc(p)) == ']') {
9711 p->lex.paren_nest--;
9712 SET_LEX_STATE(EXPR_ARG);
9713 if ((c = nextc(p)) == '=') {
9714 return tASET;
9715 }
9716 pushback(p, c);
9717 return tAREF;
9718 }
9719 pushback(p, c);
9720 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9721 return '[';
9722 }
9723 else if (IS_BEG()) {
9724 c = tLBRACK;
9725 }
9726 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9727 c = tLBRACK;
9728 }
9729 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9730 COND_PUSH(0);
9731 CMDARG_PUSH(0);
9732 return c;
9733
9734 case '{':
9735 ++p->lex.brace_nest;
9736 if (lambda_beginning_p())
9737 c = tLAMBEG;
9738 else if (IS_lex_state(EXPR_LABELED))
9739 c = tLBRACE; /* hash */
9740 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9741 c = '{'; /* block (primary) */
9742 else if (IS_lex_state(EXPR_ENDARG))
9743 c = tLBRACE_ARG; /* block (expr) */
9744 else
9745 c = tLBRACE; /* hash */
9746 if (c != tLBRACE) {
9747 p->command_start = TRUE;
9748 SET_LEX_STATE(EXPR_BEG);
9749 }
9750 else {
9751 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9752 }
9753 ++p->lex.paren_nest; /* after lambda_beginning_p() */
9754 COND_PUSH(0);
9755 CMDARG_PUSH(0);
9756 return c;
9757
9758 case '\\':
9759 c = nextc(p);
9760 if (c == '\n') {
9761 space_seen = 1;
9762 dispatch_scan_event(p, tSP);
9763 goto retry; /* skip \\n */
9764 }
9765 if (c == ' ') return tSP;
9766 if (ISSPACE(c)) return c;
9767 pushback(p, c);
9768 return '\\';
9769
9770 case '%':
9771 return parse_percent(p, space_seen, last_state);
9772
9773 case '$':
9774 return parse_gvar(p, last_state);
9775
9776 case '@':
9777 return parse_atmark(p, last_state);
9778
9779 case '_':
9780 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9781 p->ruby__end__seen = 1;
9782 p->eofp = 1;
9783#ifndef RIPPER
9784 return -1;
9785#else
9786 lex_goto_eol(p);
9787 dispatch_scan_event(p, k__END__);
9788 return 0;
9789#endif
9790 }
9791 newtok(p);
9792 break;
9793
9794 default:
9795 if (!parser_is_identchar(p)) {
9796 compile_error(p, "Invalid char `\\x%02X' in expression", c);
9797 token_flush(p);
9798 goto retry;
9799 }
9800
9801 newtok(p);
9802 break;
9803 }
9804
9805 return parse_ident(p, c, cmd_state);
9806}
9807
9808static enum yytokentype
9809yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9810{
9811 enum yytokentype t;
9812
9813 p->lval = lval;
9814 lval->val = Qundef;
9815 t = parser_yylex(p);
9816
9817 if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9818 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9819 else
9820 RUBY_SET_YYLLOC(*yylloc);
9821
9822 if (has_delayed_token(p))
9823 dispatch_delayed_token(p, t);
9824 else if (t != 0)
9825 dispatch_scan_event(p, t);
9826
9827 return t;
9828}
9829
9830#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9831
9832static NODE*
9833node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9834{
9835 NODE *n = rb_ast_newnode(p->ast, type);
9836
9837 rb_node_init(n, type, a0, a1, a2);
9838
9839 nd_set_loc(n, loc);
9840 nd_set_node_id(n, parser_get_node_id(p));
9841 return n;
9842}
9843
9844static NODE *
9845nd_set_loc(NODE *nd, const YYLTYPE *loc)
9846{
9847 nd->nd_loc = *loc;
9848 nd_set_line(nd, loc->beg_pos.lineno);
9849 return nd;
9850}
9851
9852#ifndef RIPPER
9853static enum node_type
9854nodetype(NODE *node) /* for debug */
9855{
9856 return (enum node_type)nd_type(node);
9857}
9858
9859static int
9860nodeline(NODE *node)
9861{
9862 return nd_line(node);
9863}
9864
9865static NODE*
9866newline_node(NODE *node)
9867{
9868 if (node) {
9869 node = remove_begin(node);
9870 node->flags |= NODE_FL_NEWLINE;
9871 }
9872 return node;
9873}
9874
9875static void
9876fixpos(NODE *node, NODE *orig)
9877{
9878 if (!node) return;
9879 if (!orig) return;
9880 nd_set_line(node, nd_line(orig));
9881}
9882
9883static void
9884parser_warning(struct parser_params *p, NODE *node, const char *mesg)
9885{
9886 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9887}
9888
9889static void
9890parser_warn(struct parser_params *p, NODE *node, const char *mesg)
9891{
9892 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9893}
9894
9895static NODE*
9896block_append(struct parser_params *p, NODE *head, NODE *tail)
9897{
9898 NODE *end, *h = head, *nd;
9899
9900 if (tail == 0) return head;
9901
9902 if (h == 0) return tail;
9903 switch (nd_type(h)) {
9904 case NODE_LIT:
9905 case NODE_STR:
9906 case NODE_SELF:
9907 case NODE_TRUE:
9908 case NODE_FALSE:
9909 case NODE_NIL:
9910 parser_warning(p, h, "unused literal ignored");
9911 return tail;
9912 default:
9913 h = end = NEW_BLOCK(head, &head->nd_loc);
9914 end->nd_end = end;
9915 head = end;
9916 break;
9917 case NODE_BLOCK:
9918 end = h->nd_end;
9919 break;
9920 }
9921
9922 nd = end->nd_head;
9923 switch (nd_type(nd)) {
9924 case NODE_RETURN:
9925 case NODE_BREAK:
9926 case NODE_NEXT:
9927 case NODE_REDO:
9928 case NODE_RETRY:
9929 if (RTEST(ruby_verbose)) {
9930 parser_warning(p, tail, "statement not reached");
9931 }
9932 break;
9933
9934 default:
9935 break;
9936 }
9937
9938 if (nd_type(tail) != NODE_BLOCK) {
9939 tail = NEW_BLOCK(tail, &tail->nd_loc);
9940 tail->nd_end = tail;
9941 }
9942 end->nd_next = tail;
9943 h->nd_end = tail->nd_end;
9944 nd_set_last_loc(head, nd_last_loc(tail));
9945 return head;
9946}
9947
9948/* append item to the list */
9949static NODE*
9950list_append(struct parser_params *p, NODE *list, NODE *item)
9951{
9952 NODE *last;
9953
9954 if (list == 0) return NEW_LIST(item, &item->nd_loc);
9955 if (list->nd_next) {
9956 last = list->nd_next->nd_end;
9957 }
9958 else {
9959 last = list;
9960 }
9961
9962 list->nd_alen += 1;
9963 last->nd_next = NEW_LIST(item, &item->nd_loc);
9964 list->nd_next->nd_end = last->nd_next;
9965
9966 nd_set_last_loc(list, nd_last_loc(item));
9967
9968 return list;
9969}
9970
9971/* concat two lists */
9972static NODE*
9973list_concat(NODE *head, NODE *tail)
9974{
9975 NODE *last;
9976
9977 if (head->nd_next) {
9978 last = head->nd_next->nd_end;
9979 }
9980 else {
9981 last = head;
9982 }
9983
9984 head->nd_alen += tail->nd_alen;
9985 last->nd_next = tail;
9986 if (tail->nd_next) {
9987 head->nd_next->nd_end = tail->nd_next->nd_end;
9988 }
9989 else {
9990 head->nd_next->nd_end = tail;
9991 }
9992
9993 nd_set_last_loc(head, nd_last_loc(tail));
9994
9995 return head;
9996}
9997
9998static int
9999literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
10000{
10001 if (NIL_P(tail)) return 1;
10002 if (!rb_enc_compatible(head, tail)) {
10003 compile_error(p, "string literal encodings differ (%s / %s)",
10004 rb_enc_name(rb_enc_get(head)),
10005 rb_enc_name(rb_enc_get(tail)));
10006 rb_str_resize(head, 0);
10007 rb_str_resize(tail, 0);
10008 return 0;
10009 }
10010 rb_str_buf_append(head, tail);
10011 return 1;
10012}
10013
10014static VALUE
10015string_literal_head(enum node_type htype, NODE *head)
10016{
10017 if (htype != NODE_DSTR) return Qfalse;
10018 if (head->nd_next) {
10019 head = head->nd_next->nd_end->nd_head;
10020 if (!head || nd_type(head) != NODE_STR) return Qfalse;
10021 }
10022 const VALUE lit = head->nd_lit;
10023 ASSUME(lit != Qfalse);
10024 return lit;
10025}
10026
10027/* concat two string literals */
10028static NODE *
10029literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
10030{
10031 enum node_type htype;
10032 VALUE lit;
10033
10034 if (!head) return tail;
10035 if (!tail) return head;
10036
10037 htype = nd_type(head);
10038 if (htype == NODE_EVSTR) {
10039 head = new_dstr(p, head, loc);
10040 htype = NODE_DSTR;
10041 }
10042 if (p->heredoc_indent > 0) {
10043 switch (htype) {
10044 case NODE_STR:
10045 nd_set_type(head, NODE_DSTR);
10046 case NODE_DSTR:
10047 return list_append(p, head, tail);
10048 default:
10049 break;
10050 }
10051 }
10052 switch (nd_type(tail)) {
10053 case NODE_STR:
10054 if ((lit = string_literal_head(htype, head)) != Qfalse) {
10055 htype = NODE_STR;
10056 }
10057 else {
10058 lit = head->nd_lit;
10059 }
10060 if (htype == NODE_STR) {
10061 if (!literal_concat0(p, lit, tail->nd_lit)) {
10062 error:
10063 rb_discard_node(p, head);
10064 rb_discard_node(p, tail);
10065 return 0;
10066 }
10067 rb_discard_node(p, tail);
10068 }
10069 else {
10070 list_append(p, head, tail);
10071 }
10072 break;
10073
10074 case NODE_DSTR:
10075 if (htype == NODE_STR) {
10076 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
10077 goto error;
10078 tail->nd_lit = head->nd_lit;
10079 rb_discard_node(p, head);
10080 head = tail;
10081 }
10082 else if (NIL_P(tail->nd_lit)) {
10083 append:
10084 head->nd_alen += tail->nd_alen - 1;
10085 if (!head->nd_next) {
10086 head->nd_next = tail->nd_next;
10087 }
10088 else if (tail->nd_next) {
10089 head->nd_next->nd_end->nd_next = tail->nd_next;
10090 head->nd_next->nd_end = tail->nd_next->nd_end;
10091 }
10092 rb_discard_node(p, tail);
10093 }
10094 else if ((lit = string_literal_head(htype, head)) != Qfalse) {
10095 if (!literal_concat0(p, lit, tail->nd_lit))
10096 goto error;
10097 tail->nd_lit = Qnil;
10098 goto append;
10099 }
10100 else {
10101 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
10102 }
10103 break;
10104
10105 case NODE_EVSTR:
10106 if (htype == NODE_STR) {
10107 nd_set_type(head, NODE_DSTR);
10108 head->nd_alen = 1;
10109 }
10110 list_append(p, head, tail);
10111 break;
10112 }
10113 return head;
10114}
10115
10116static NODE *
10117evstr2dstr(struct parser_params *p, NODE *node)
10118{
10119 if (nd_type(node) == NODE_EVSTR) {
10120 node = new_dstr(p, node, &node->nd_loc);
10121 }
10122 return node;
10123}
10124
10125static NODE *
10126new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10127{
10128 NODE *head = node;
10129
10130 if (node) {
10131 switch (nd_type(node)) {
10132 case NODE_STR:
10133 nd_set_type(node, NODE_DSTR);
10134 case NODE_DSTR: case NODE_EVSTR:
10135 return node;
10136 }
10137 }
10138 return NEW_EVSTR(head, loc);
10139}
10140
10141static NODE *
10142new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10143{
10144 VALUE lit = STR_NEW0();
10145 NODE *dstr = NEW_DSTR(lit, loc);
10146 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10147 return list_append(p, dstr, node);
10148}
10149
10150static NODE *
10151call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
10152 const YYLTYPE *op_loc, const YYLTYPE *loc)
10153{
10154 NODE *expr;
10155 value_expr(recv);
10156 value_expr(arg1);
10157 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
10158 nd_set_line(expr, op_loc->beg_pos.lineno);
10159 return expr;
10160}
10161
10162static NODE *
10163call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
10164{
10165 NODE *opcall;
10166 value_expr(recv);
10167 opcall = NEW_OPCALL(recv, id, 0, loc);
10168 nd_set_line(opcall, op_loc->beg_pos.lineno);
10169 return opcall;
10170}
10171
10172static NODE *
10173new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
10174{
10175 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
10176 nd_set_line(qcall, op_loc->beg_pos.lineno);
10177 return qcall;
10178}
10179
10180static NODE*
10181new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
10182{
10183 NODE *ret;
10184 if (block) block_dup_check(p, args, block);
10185 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
10186 if (block) ret = method_add_block(p, ret, block, loc);
10187 fixpos(ret, recv);
10188 return ret;
10189}
10190
10191#define nd_once_body(node) (nd_type(node) == NODE_ONCE ? (node)->nd_body : node)
10192static NODE*
10193match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
10194{
10195 NODE *n;
10196 int line = op_loc->beg_pos.lineno;
10197
10198 value_expr(node1);
10199 value_expr(node2);
10200 if (node1 && (n = nd_once_body(node1)) != 0) {
10201 switch (nd_type(n)) {
10202 case NODE_DREGX:
10203 {
10204 NODE *match = NEW_MATCH2(node1, node2, loc);
10205 nd_set_line(match, line);
10206 return match;
10207 }
10208
10209 case NODE_LIT:
10210 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
10211 const VALUE lit = n->nd_lit;
10212 NODE *match = NEW_MATCH2(node1, node2, loc);
10213 match->nd_args = reg_named_capture_assign(p, lit, loc);
10214 nd_set_line(match, line);
10215 return match;
10216 }
10217 }
10218 }
10219
10220 if (node2 && (n = nd_once_body(node2)) != 0) {
10221 NODE *match3;
10222
10223 switch (nd_type(n)) {
10224 case NODE_LIT:
10225 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
10226 /* fallthru */
10227 case NODE_DREGX:
10228 match3 = NEW_MATCH3(node2, node1, loc);
10229 return match3;
10230 }
10231 }
10232
10233 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
10234 nd_set_line(n, line);
10235 return n;
10236}
10237
10238# if WARN_PAST_SCOPE
10239static int
10240past_dvar_p(struct parser_params *p, ID id)
10241{
10242 struct vtable *past = p->lvtbl->past;
10243 while (past) {
10244 if (vtable_included(past, id)) return 1;
10245 past = past->prev;
10246 }
10247 return 0;
10248}
10249# endif
10250
10251static int
10252numparam_nested_p(struct parser_params *p)
10253{
10254 struct local_vars *local = p->lvtbl;
10255 NODE *outer = local->numparam.outer;
10256 NODE *inner = local->numparam.inner;
10257 if (outer || inner) {
10258 NODE *used = outer ? outer : inner;
10259 compile_error(p, "numbered parameter is already used in\n"
10260 "%s:%d: %s block here",
10261 p->ruby_sourcefile, nd_line(used),
10262 outer ? "outer" : "inner");
10263 parser_show_error_line(p, &used->nd_loc);
10264 return 1;
10265 }
10266 return 0;
10267}
10268
10269static NODE*
10270gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
10271{
10272 ID *vidp = NULL;
10273 NODE *node;
10274 switch (id) {
10275 case keyword_self:
10276 return NEW_SELF(loc);
10277 case keyword_nil:
10278 return NEW_NIL(loc);
10279 case keyword_true:
10280 return NEW_TRUE(loc);
10281 case keyword_false:
10282 return NEW_FALSE(loc);
10283 case keyword__FILE__:
10284 {
10285 VALUE file = p->ruby_sourcefile_string;
10286 if (NIL_P(file))
10287 file = rb_str_new(0, 0);
10288 else
10289 file = rb_str_dup(file);
10290 node = NEW_STR(file, loc);
10291 RB_OBJ_WRITTEN(p->ast, Qnil, file);
10292 }
10293 return node;
10294 case keyword__LINE__:
10295 return NEW_LIT(INT2FIX(p->tokline), loc);
10296 case keyword__ENCODING__:
10297 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
10298 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10299 return node;
10300
10301 }
10302 switch (id_type(id)) {
10303 case ID_LOCAL:
10304 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
10305 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
10306 if (id == p->cur_arg) {
10307 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10308 return 0;
10309 }
10310 if (vidp) *vidp |= LVAR_USED;
10311 node = NEW_DVAR(id, loc);
10312 return node;
10313 }
10314 if (local_id_ref(p, id, &vidp)) {
10315 if (id == p->cur_arg) {
10316 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10317 return 0;
10318 }
10319 if (vidp) *vidp |= LVAR_USED;
10320 node = NEW_LVAR(id, loc);
10321 return node;
10322 }
10323 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
10324 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
10325 if (numparam_nested_p(p)) return 0;
10326 node = NEW_DVAR(id, loc);
10327 struct local_vars *local = p->lvtbl;
10328 if (!local->numparam.current) local->numparam.current = node;
10329 return node;
10330 }
10331# if WARN_PAST_SCOPE
10332 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10333 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10334 }
10335# endif
10336 /* method call without arguments */
10337 return NEW_VCALL(id, loc);
10338 case ID_GLOBAL:
10339 return NEW_GVAR(id, loc);
10340 case ID_INSTANCE:
10341 return NEW_IVAR(id, loc);
10342 case ID_CONST:
10343 return NEW_CONST(id, loc);
10344 case ID_CLASS:
10345 return NEW_CVAR(id, loc);
10346 }
10347 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10348 return 0;
10349}
10350
10351static NODE *
10352opt_arg_append(NODE *opt_list, NODE *opt)
10353{
10354 NODE *opts = opt_list;
10355 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10356
10357 while (opts->nd_next) {
10358 opts = opts->nd_next;
10359 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10360 }
10361 opts->nd_next = opt;
10362
10363 return opt_list;
10364}
10365
10366static NODE *
10367kwd_append(NODE *kwlist, NODE *kw)
10368{
10369 if (kwlist) {
10370 NODE *kws = kwlist;
10371 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10372 while (kws->nd_next) {
10373 kws = kws->nd_next;
10374 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10375 }
10376 kws->nd_next = kw;
10377 }
10378 return kwlist;
10379}
10380
10381static NODE *
10382new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10383{
10384 return NEW_DEFINED(remove_begin_all(expr), loc);
10385}
10386
10387static NODE*
10388symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10389{
10390 enum node_type type = nd_type(symbol);
10391 switch (type) {
10392 case NODE_DSTR:
10393 nd_set_type(symbol, NODE_DSYM);
10394 break;
10395 case NODE_STR:
10396 nd_set_type(symbol, NODE_LIT);
10397 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10398 break;
10399 default:
10400 compile_error(p, "unexpected node as symbol: %s", ruby_node_name(type));
10401 }
10402 return list_append(p, symbols, symbol);
10403}
10404
10405static NODE *
10406new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10407{
10408 NODE *list, *prev;
10409 VALUE lit;
10410
10411 if (!node) {
10412 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10413 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10414 return node;
10415 }
10416 switch (nd_type(node)) {
10417 case NODE_STR:
10418 {
10419 VALUE src = node->nd_lit;
10420 nd_set_type(node, NODE_LIT);
10421 nd_set_loc(node, loc);
10422 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10423 }
10424 break;
10425 default:
10426 lit = STR_NEW0();
10427 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10428 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10429 /* fall through */
10430 case NODE_DSTR:
10431 nd_set_type(node, NODE_DREGX);
10432 nd_set_loc(node, loc);
10433 node->nd_cflag = options & RE_OPTION_MASK;
10434 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10435 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10436 NODE *frag = list->nd_head;
10437 enum node_type type = nd_type(frag);
10438 if (type == NODE_STR || (type == NODE_DSTR && !frag->nd_next)) {
10439 VALUE tail = frag->nd_lit;
10440 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10441 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10442 if (!literal_concat0(p, lit, tail)) {
10443 return NEW_NIL(loc); /* dummy node on error */
10444 }
10445 rb_str_resize(tail, 0);
10446 prev->nd_next = list->nd_next;
10447 rb_discard_node(p, list->nd_head);
10448 rb_discard_node(p, list);
10449 list = prev;
10450 }
10451 else {
10452 prev = list;
10453 }
10454 }
10455 else {
10456 prev = 0;
10457 }
10458 }
10459 if (!node->nd_next) {
10460 VALUE src = node->nd_lit;
10461 nd_set_type(node, NODE_LIT);
10462 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10463 }
10464 if (options & RE_OPTION_ONCE) {
10465 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10466 }
10467 break;
10468 }
10469 return node;
10470}
10471
10472static NODE *
10473new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10474{
10475 if (!k) return 0;
10476 return NEW_KW_ARG(0, (k), loc);
10477}
10478
10479static NODE *
10480new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10481{
10482 if (!node) {
10483 VALUE lit = STR_NEW0();
10484 NODE *xstr = NEW_XSTR(lit, loc);
10485 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10486 return xstr;
10487 }
10488 switch (nd_type(node)) {
10489 case NODE_STR:
10490 nd_set_type(node, NODE_XSTR);
10491 nd_set_loc(node, loc);
10492 break;
10493 case NODE_DSTR:
10494 nd_set_type(node, NODE_DXSTR);
10495 nd_set_loc(node, loc);
10496 break;
10497 default:
10498 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10499 break;
10500 }
10501 return node;
10502}
10503
10504static void
10505check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10506{
10507 VALUE lit;
10508
10509 if (!arg || !p->case_labels) return;
10510
10511 lit = rb_node_case_when_optimizable_literal(arg);
10512 if (lit == Qundef) return;
10513 if (nd_type(arg) == NODE_STR) {
10514 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10515 }
10516
10517 if (NIL_P(p->case_labels)) {
10518 p->case_labels = rb_obj_hide(rb_hash_new());
10519 }
10520 else {
10521 VALUE line = rb_hash_lookup(p->case_labels, lit);
10522 if (!NIL_P(line)) {
10523 rb_warning1("duplicated `when' clause with line %d is ignored",
10524 WARN_IVAL(line));
10525 return;
10526 }
10527 }
10528 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10529}
10530
10531#else /* !RIPPER */
10532static int
10533id_is_var(struct parser_params *p, ID id)
10534{
10535 if (is_notop_id(id)) {
10536 switch (id & ID_SCOPE_MASK) {
10537 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10538 return 1;
10539 case ID_LOCAL:
10540 if (dyna_in_block(p)) {
10541 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10542 }
10543 if (local_id(p, id)) return 1;
10544 /* method call without arguments */
10545 return 0;
10546 }
10547 }
10548 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10549 return 0;
10550}
10551
10552static VALUE
10553new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10554{
10555 VALUE src = 0, err;
10556 int options = 0;
10557 if (ripper_is_node_yylval(re)) {
10558 src = RNODE(re)->nd_cval;
10559 re = RNODE(re)->nd_rval;
10560 }
10561 if (ripper_is_node_yylval(opt)) {
10562 options = (int)RNODE(opt)->nd_tag;
10563 opt = RNODE(opt)->nd_rval;
10564 }
10565 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10566 compile_error(p, "%"PRIsVALUE, err);
10567 }
10568 return dispatch2(regexp_literal, re, opt);
10569}
10570#endif /* !RIPPER */
10571
10572
10573#ifndef RIPPER
10574static const char rb_parser_lex_state_names[][8] = {
10575 "BEG", "END", "ENDARG", "ENDFN", "ARG",
10576 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10577 "LABEL", "LABELED","FITEM",
10578};
10579
10580static VALUE
10581append_lex_state_name(enum lex_state_e state, VALUE buf)
10582{
10583 int i, sep = 0;
10584 unsigned int mask = 1;
10585 static const char none[] = "NONE";
10586
10587 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10588 if ((unsigned)state & mask) {
10589 if (sep) {
10590 rb_str_cat(buf, "|", 1);
10591 }
10592 sep = 1;
10593 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10594 }
10595 }
10596 if (!sep) {
10597 rb_str_cat(buf, none, sizeof(none)-1);
10598 }
10599 return buf;
10600}
10601
10602static void
10603flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10604{
10605 VALUE mesg = p->debug_buffer;
10606
10607 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10608 p->debug_buffer = Qnil;
10609 rb_io_puts(1, &mesg, out);
10610 }
10611 if (!NIL_P(str) && RSTRING_LEN(str)) {
10612 rb_io_write(p->debug_output, str);
10613 }
10614}
10615
10616enum lex_state_e
10617rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10618 enum lex_state_e to, int line)
10619{
10620 VALUE mesg;
10621 mesg = rb_str_new_cstr("lex_state: ");
10622 append_lex_state_name(from, mesg);
10623 rb_str_cat_cstr(mesg, " -> ");
10624 append_lex_state_name(to, mesg);
10625 rb_str_catf(mesg, " at line %d\n", line);
10626 flush_debug_buffer(p, p->debug_output, mesg);
10627 return to;
10628}
10629
10630VALUE
10631rb_parser_lex_state_name(enum lex_state_e state)
10632{
10633 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10634}
10635
10636static void
10637append_bitstack_value(stack_type stack, VALUE mesg)
10638{
10639 if (stack == 0) {
10640 rb_str_cat_cstr(mesg, "0");
10641 }
10642 else {
10643 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10644 for (; mask && !(stack & mask); mask >>= 1) continue;
10645 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10646 }
10647}
10648
10649void
10650rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10651 const char *name, int line)
10652{
10653 VALUE mesg = rb_sprintf("%s: ", name);
10654 append_bitstack_value(stack, mesg);
10655 rb_str_catf(mesg, " at line %d\n", line);
10656 flush_debug_buffer(p, p->debug_output, mesg);
10657}
10658
10659void
10660rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10661{
10662 va_list ap;
10663 VALUE mesg = rb_str_new_cstr("internal parser error: ");
10664
10665 va_start(ap, fmt);
10666 rb_str_vcatf(mesg, fmt, ap);
10667 va_end(ap);
10668 parser_yyerror(p, NULL, RSTRING_PTR(mesg));
10669 RB_GC_GUARD(mesg);
10670
10671 mesg = rb_str_new(0, 0);
10672 append_lex_state_name(p->lex.state, mesg);
10673 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10674 rb_str_resize(mesg, 0);
10675 append_bitstack_value(p->cond_stack, mesg);
10676 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10677 rb_str_resize(mesg, 0);
10678 append_bitstack_value(p->cmdarg_stack, mesg);
10679 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10680 if (p->debug_output == rb_ractor_stdout())
10681 p->debug_output = rb_ractor_stderr();
10682 p->debug = TRUE;
10683}
10684
10685YYLTYPE *
10686rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10687{
10688 int sourceline = here->sourceline;
10689 int beg_pos = (int)here->offset - here->quote
10690 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10691 int end_pos = (int)here->offset + here->length + here->quote;
10692
10693 yylloc->beg_pos.lineno = sourceline;
10694 yylloc->beg_pos.column = beg_pos;
10695 yylloc->end_pos.lineno = sourceline;
10696 yylloc->end_pos.column = end_pos;
10697 return yylloc;
10698}
10699
10700YYLTYPE *
10701rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10702{
10703 yylloc->beg_pos.lineno = p->ruby_sourceline;
10704 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10705 yylloc->end_pos.lineno = p->ruby_sourceline;
10706 yylloc->end_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10707 return yylloc;
10708}
10709
10710YYLTYPE *
10711rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10712{
10713 yylloc->beg_pos.lineno = p->ruby_sourceline;
10714 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10715 yylloc->end_pos.lineno = p->ruby_sourceline;
10716 yylloc->end_pos.column = (int)(p->lex.pcur - p->lex.pbeg);
10717 return yylloc;
10718}
10719#endif /* !RIPPER */
10720
10721static void
10722parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp)
10723{
10724 VALUE v;
10725
10726 switch (type) {
10727 case tIDENTIFIER: case tFID: case tGVAR: case tIVAR:
10728 case tCONSTANT: case tCVAR: case tLABEL: case tOP_ASGN:
10729#ifndef RIPPER
10730 v = rb_id2str(valp->id);
10731#else
10732 v = valp->node->nd_rval;
10733#endif
10734 rb_parser_printf(p, "%"PRIsVALUE, v);
10735 break;
10736 case tINTEGER: case tFLOAT: case tRATIONAL: case tIMAGINARY:
10737 case tSTRING_CONTENT: case tCHAR:
10738#ifndef RIPPER
10739 v = valp->node->nd_lit;
10740#else
10741 v = valp->val;
10742#endif
10743 rb_parser_printf(p, "%+"PRIsVALUE, v);
10744 break;
10745 case tNTH_REF:
10746#ifndef RIPPER
10747 rb_parser_printf(p, "$%ld", valp->node->nd_nth);
10748#else
10749 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10750#endif
10751 break;
10752 case tBACK_REF:
10753#ifndef RIPPER
10754 rb_parser_printf(p, "$%c", (int)valp->node->nd_nth);
10755#else
10756 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10757#endif
10758 break;
10759 default:
10760 break;
10761 }
10762}
10763
10764static int
10765assignable0(struct parser_params *p, ID id, const char **err)
10766{
10767 if (!id) return -1;
10768 switch (id) {
10769 case keyword_self:
10770 *err = "Can't change the value of self";
10771 return -1;
10772 case keyword_nil:
10773 *err = "Can't assign to nil";
10774 return -1;
10775 case keyword_true:
10776 *err = "Can't assign to true";
10777 return -1;
10778 case keyword_false:
10779 *err = "Can't assign to false";
10780 return -1;
10781 case keyword__FILE__:
10782 *err = "Can't assign to __FILE__";
10783 return -1;
10784 case keyword__LINE__:
10785 *err = "Can't assign to __LINE__";
10786 return -1;
10787 case keyword__ENCODING__:
10788 *err = "Can't assign to __ENCODING__";
10789 return -1;
10790 }
10791 switch (id_type(id)) {
10792 case ID_LOCAL:
10793 if (dyna_in_block(p)) {
10794 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10795 compile_error(p, "Can't assign to numbered parameter _%d",
10796 NUMPARAM_ID_TO_IDX(id));
10797 return -1;
10798 }
10799 if (dvar_curr(p, id)) return NODE_DASGN_CURR;
10800 if (dvar_defined(p, id)) return NODE_DASGN;
10801 if (local_id(p, id)) return NODE_LASGN;
10802 dyna_var(p, id);
10803 return NODE_DASGN_CURR;
10804 }
10805 else {
10806 if (!local_id(p, id)) local_var(p, id);
10807 return NODE_LASGN;
10808 }
10809 break;
10810 case ID_GLOBAL: return NODE_GASGN;
10811 case ID_INSTANCE: return NODE_IASGN;
10812 case ID_CONST:
10813 if (!p->ctxt.in_def) return NODE_CDECL;
10814 *err = "dynamic constant assignment";
10815 return -1;
10816 case ID_CLASS: return NODE_CVASGN;
10817 default:
10818 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10819 }
10820 return -1;
10821}
10822
10823#ifndef RIPPER
10824static NODE*
10825assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10826{
10827 const char *err = 0;
10828 int node_type = assignable0(p, id, &err);
10829 switch (node_type) {
10830 case NODE_DASGN_CURR: return NEW_DASGN_CURR(id, val, loc);
10831 case NODE_DASGN: return NEW_DASGN(id, val, loc);
10832 case NODE_LASGN: return NEW_LASGN(id, val, loc);
10833 case NODE_GASGN: return NEW_GASGN(id, val, loc);
10834 case NODE_IASGN: return NEW_IASGN(id, val, loc);
10835 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10836 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10837 }
10838 if (err) yyerror1(loc, err);
10839 return NEW_BEGIN(0, loc);
10840}
10841#else
10842static VALUE
10843assignable(struct parser_params *p, VALUE lhs)
10844{
10845 const char *err = 0;
10846 assignable0(p, get_id(lhs), &err);
10847 if (err) lhs = assign_error(p, err, lhs);
10848 return lhs;
10849}
10850#endif
10851
10852static int
10853is_private_local_id(ID name)
10854{
10855 VALUE s;
10856 if (name == idUScore) return 1;
10857 if (!is_local_id(name)) return 0;
10858 s = rb_id2str(name);
10859 if (!s) return 0;
10860 return RSTRING_PTR(s)[0] == '_';
10861}
10862
10863static int
10864shadowing_lvar_0(struct parser_params *p, ID name)
10865{
10866 if (is_private_local_id(name)) return 1;
10867 if (dyna_in_block(p)) {
10868 if (dvar_curr(p, name)) {
10869 yyerror0("duplicated argument name");
10870 }
10871 else if (dvar_defined(p, name) || local_id(p, name)) {
10872 vtable_add(p->lvtbl->vars, name);
10873 if (p->lvtbl->used) {
10874 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
10875 }
10876 return 0;
10877 }
10878 }
10879 else {
10880 if (local_id(p, name)) {
10881 yyerror0("duplicated argument name");
10882 }
10883 }
10884 return 1;
10885}
10886
10887static ID
10888shadowing_lvar(struct parser_params *p, ID name)
10889{
10890 shadowing_lvar_0(p, name);
10891 return name;
10892}
10893
10894static void
10895new_bv(struct parser_params *p, ID name)
10896{
10897 if (!name) return;
10898 if (!is_local_id(name)) {
10899 compile_error(p, "invalid local variable - %"PRIsVALUE,
10900 rb_id2str(name));
10901 return;
10902 }
10903 if (!shadowing_lvar_0(p, name)) return;
10904 dyna_var(p, name);
10905}
10906
10907#ifndef RIPPER
10908static NODE *
10909aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
10910{
10911 return NEW_ATTRASGN(recv, tASET, idx, loc);
10912}
10913
10914static void
10915block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
10916{
10917 if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
10918 compile_error(p, "both block arg and actual block given");
10919 }
10920}
10921
10922static NODE *
10923attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
10924{
10925 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
10926 return NEW_ATTRASGN(recv, id, 0, loc);
10927}
10928
10929static void
10930rb_backref_error(struct parser_params *p, NODE *node)
10931{
10932 switch (nd_type(node)) {
10933 case NODE_NTH_REF:
10934 compile_error(p, "Can't set variable $%ld", node->nd_nth);
10935 break;
10936 case NODE_BACK_REF:
10937 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
10938 break;
10939 }
10940}
10941#else
10942static VALUE
10943backref_error(struct parser_params *p, NODE *ref, VALUE expr)
10944{
10945 VALUE mesg = rb_str_new_cstr("Can't set variable ");
10946 rb_str_append(mesg, ref->nd_cval);
10947 return dispatch2(assign_error, mesg, expr);
10948}
10949#endif
10950
10951#ifndef RIPPER
10952static NODE *
10953arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10954{
10955 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
10956 switch (nd_type(node1)) {
10957 case NODE_LIST:
10958 return list_append(p, node1, node2);
10959 case NODE_BLOCK_PASS:
10960 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
10961 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
10962 return node1;
10963 case NODE_ARGSPUSH:
10964 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
10965 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10966 nd_set_type(node1, NODE_ARGSCAT);
10967 return node1;
10968 case NODE_ARGSCAT:
10969 if (nd_type(node1->nd_body) != NODE_LIST) break;
10970 node1->nd_body = list_append(p, node1->nd_body, node2);
10971 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10972 return node1;
10973 }
10974 return NEW_ARGSPUSH(node1, node2, loc);
10975}
10976
10977static NODE *
10978arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10979{
10980 if (!node2) return node1;
10981 switch (nd_type(node1)) {
10982 case NODE_BLOCK_PASS:
10983 if (node1->nd_head)
10984 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
10985 else
10986 node1->nd_head = NEW_LIST(node2, loc);
10987 return node1;
10988 case NODE_ARGSPUSH:
10989 if (nd_type(node2) != NODE_LIST) break;
10990 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
10991 nd_set_type(node1, NODE_ARGSCAT);
10992 return node1;
10993 case NODE_ARGSCAT:
10994 if (nd_type(node2) != NODE_LIST ||
10995 nd_type(node1->nd_body) != NODE_LIST) break;
10996 node1->nd_body = list_concat(node1->nd_body, node2);
10997 return node1;
10998 }
10999 return NEW_ARGSCAT(node1, node2, loc);
11000}
11001
11002static NODE *
11003last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
11004{
11005 NODE *n1;
11006 if ((n1 = splat_array(args)) != 0) {
11007 return list_append(p, n1, last_arg);
11008 }
11009 return arg_append(p, args, last_arg, loc);
11010}
11011
11012static NODE *
11013rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
11014{
11015 NODE *n1;
11016 if ((nd_type(rest_arg) == NODE_LIST) && (n1 = splat_array(args)) != 0) {
11017 return list_concat(n1, rest_arg);
11018 }
11019 return arg_concat(p, args, rest_arg, loc);
11020}
11021
11022static NODE *
11023splat_array(NODE* node)
11024{
11025 if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
11026 if (nd_type(node) == NODE_LIST) return node;
11027 return 0;
11028}
11029
11030static void
11031mark_lvar_used(struct parser_params *p, NODE *rhs)
11032{
11033 ID *vidp = NULL;
11034 if (!rhs) return;
11035 switch (nd_type(rhs)) {
11036 case NODE_LASGN:
11037 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
11038 if (vidp) *vidp |= LVAR_USED;
11039 }
11040 break;
11041 case NODE_DASGN:
11042 case NODE_DASGN_CURR:
11043 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
11044 if (vidp) *vidp |= LVAR_USED;
11045 }
11046 break;
11047#if 0
11048 case NODE_MASGN:
11049 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
11050 mark_lvar_used(p, rhs->nd_head);
11051 }
11052 break;
11053#endif
11054 }
11055}
11056
11057static NODE *
11058const_decl_path(struct parser_params *p, NODE **dest)
11059{
11060 NODE *n = *dest;
11061 if (nd_type(n) != NODE_CALL) {
11062 const YYLTYPE *loc = &n->nd_loc;
11063 VALUE path;
11064 if (n->nd_vid) {
11065 path = rb_id2str(n->nd_vid);
11066 }
11067 else {
11068 n = n->nd_else;
11069 path = rb_ary_new();
11070 for (; n && nd_type(n) == NODE_COLON2; n = n->nd_head) {
11071 rb_ary_push(path, rb_id2str(n->nd_mid));
11072 }
11073 if (n && nd_type(n) == NODE_CONST) {
11074 // Const::Name
11075 rb_ary_push(path, rb_id2str(n->nd_vid));
11076 }
11077 else if (n && nd_type(n) == NODE_COLON3) {
11078 // ::Const::Name
11079 rb_ary_push(path, rb_str_new(0, 0));
11080 }
11081 else {
11082 // expression::Name
11083 rb_ary_push(path, rb_str_new_cstr("..."));
11084 }
11085 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
11086 path = rb_fstring(path);
11087 }
11088 *dest = n = NEW_LIT(path, loc);
11089 }
11090 return n;
11091}
11092
11093extern VALUE rb_mRubyVMFrozenCore;
11094
11095static NODE *
11096make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
11097{
11098 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11099
11100 if (copy) {
11101 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
11102 NEW_LIST(value, loc), loc);
11103 }
11104 else {
11105 return NEW_CALL(fcore, rb_intern("make_shareable"),
11106 NEW_LIST(value, loc), loc);
11107 }
11108}
11109
11110static NODE *
11111ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
11112{
11113 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11114 NODE *args = NEW_LIST(value, loc);
11115 args = list_append(p, args, const_decl_path(p, dest));
11116 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
11117}
11118
11119static int is_static_content(NODE *node);
11120
11121static VALUE
11122shareable_literal_value(NODE *node)
11123{
11124 if (!node) return Qnil;
11125 enum node_type type = nd_type(node);
11126 switch (type) {
11127 case NODE_TRUE:
11128 return Qtrue;
11129 case NODE_FALSE:
11130 return Qfalse;
11131 case NODE_NIL:
11132 return Qnil;
11133 case NODE_LIT:
11134 return node->nd_lit;
11135 default:
11136 return Qundef;
11137 }
11138}
11139
11140#ifndef SHAREABLE_BARE_EXPRESSION
11141#define SHAREABLE_BARE_EXPRESSION 1
11142#endif
11143
11144static NODE *
11145shareable_literal_constant(struct parser_params *p, enum shareability shareable,
11146 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
11147{
11148# define shareable_literal_constant_next(n) \
11149 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
11150 VALUE lit = Qnil;
11151
11152 if (!value) return 0;
11153 enum node_type type = nd_type(value);
11154 switch (type) {
11155 case NODE_TRUE:
11156 case NODE_FALSE:
11157 case NODE_NIL:
11158 case NODE_LIT:
11159 return value;
11160
11161 case NODE_DSTR:
11162 if (shareable == shareable_literal) {
11163 value = NEW_CALL(value, idUMinus, 0, loc);
11164 }
11165 return value;
11166
11167 case NODE_STR:
11168 lit = rb_fstring(value->nd_lit);
11169 nd_set_type(value, NODE_LIT);
11170 RB_OBJ_WRITE(p->ast, &value->nd_lit, lit);
11171 return value;
11172
11173 case NODE_ZLIST:
11174 lit = rb_ary_new();
11175 OBJ_FREEZE_RAW(lit);
11176 return NEW_LIT(lit, loc);
11177
11178 case NODE_LIST:
11179 lit = rb_ary_new();
11180 for (NODE *n = value; n; n = n->nd_next) {
11181 NODE *elt = n->nd_head;
11182 if (elt) {
11183 elt = shareable_literal_constant_next(elt);
11184 if (elt) {
11185 n->nd_head = elt;
11186 }
11187 else if (RTEST(lit)) {
11188 rb_ary_clear(lit);
11189 lit = Qfalse;
11190 }
11191 }
11192 if (RTEST(lit)) {
11193 VALUE e = shareable_literal_value(elt);
11194 if (e != Qundef) {
11195 rb_ary_push(lit, e);
11196 }
11197 else {
11198 rb_ary_clear(lit);
11199 lit = Qnil; /* make shareable at runtime */
11200 }
11201 }
11202 }
11203 break;
11204
11205 case NODE_HASH:
11206 if (!value->nd_brace) return 0;
11207 lit = rb_hash_new();
11208 for (NODE *n = value->nd_head; n; n = n->nd_next->nd_next) {
11209 NODE *key = n->nd_head;
11210 NODE *val = n->nd_next->nd_head;
11211 if (key) {
11212 key = shareable_literal_constant_next(key);
11213 if (key) {
11214 n->nd_head = key;
11215 }
11216 else if (RTEST(lit)) {
11217 rb_hash_clear(lit);
11218 lit = Qfalse;
11219 }
11220 }
11221 if (val) {
11222 val = shareable_literal_constant_next(val);
11223 if (val) {
11224 n->nd_next->nd_head = val;
11225 }
11226 else if (RTEST(lit)) {
11227 rb_hash_clear(lit);
11228 lit = Qfalse;
11229 }
11230 }
11231 if (RTEST(lit)) {
11232 VALUE k = shareable_literal_value(key);
11233 VALUE v = shareable_literal_value(val);
11234 if (k != Qundef && v != Qundef) {
11235 rb_hash_aset(lit, k, v);
11236 }
11237 else {
11238 rb_hash_clear(lit);
11239 lit = Qnil; /* make shareable at runtime */
11240 }
11241 }
11242 }
11243 break;
11244
11245 default:
11246 if (shareable == shareable_literal &&
11247 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
11248 return ensure_shareable_node(p, dest, value, loc);
11249 }
11250 return 0;
11251 }
11252
11253 /* Array or Hash */
11254 if (!lit) return 0;
11255 if (NIL_P(lit)) {
11256 // if shareable_literal, all elements should have been ensured
11257 // as shareable
11258 value = make_shareable_node(p, value, false, loc);
11259 }
11260 else {
11261 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
11262 }
11263
11264 return value;
11265# undef shareable_literal_constant_next
11266}
11267
11268static NODE *
11269shareable_constant_value(struct parser_params *p, enum shareability shareable,
11270 NODE *lhs, NODE *value, const YYLTYPE *loc)
11271{
11272 if (!value) return 0;
11273 switch (shareable) {
11274 case shareable_none:
11275 return value;
11276
11277 case shareable_literal:
11278 {
11279 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11280 if (lit) return lit;
11281 return value;
11282 }
11283 break;
11284
11285 case shareable_copy:
11286 case shareable_everything:
11287 {
11288 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11289 if (lit) return lit;
11290 return make_shareable_node(p, value, shareable == shareable_copy, loc);
11291 }
11292 break;
11293
11294 default:
11295 UNREACHABLE_RETURN(0);
11296 }
11297}
11298
11299static NODE *
11300node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
11301{
11302 if (!lhs) return 0;
11303
11304 switch (nd_type(lhs)) {
11305 case NODE_CDECL:
11306 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
11307 /* fallthru */
11308
11309 case NODE_GASGN:
11310 case NODE_IASGN:
11311 case NODE_LASGN:
11312 case NODE_DASGN:
11313 case NODE_DASGN_CURR:
11314 case NODE_MASGN:
11315 case NODE_CVASGN:
11316 lhs->nd_value = rhs;
11317 nd_set_loc(lhs, loc);
11318 break;
11319
11320 case NODE_ATTRASGN:
11321 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
11322 nd_set_loc(lhs, loc);
11323 break;
11324
11325 default:
11326 /* should not happen */
11327 break;
11328 }
11329
11330 return lhs;
11331}
11332
11333static NODE *
11334value_expr_check(struct parser_params *p, NODE *node)
11335{
11336 NODE *void_node = 0, *vn;
11337
11338 if (!node) {
11339 rb_warning0("empty expression");
11340 }
11341 while (node) {
11342 switch (nd_type(node)) {
11343 case NODE_RETURN:
11344 case NODE_BREAK:
11345 case NODE_NEXT:
11346 case NODE_REDO:
11347 case NODE_RETRY:
11348 return void_node ? void_node : node;
11349
11350 case NODE_CASE3:
11351 if (!node->nd_body || nd_type(node->nd_body) != NODE_IN) {
11352 compile_error(p, "unexpected node");
11353 return NULL;
11354 }
11355 if (node->nd_body->nd_body) {
11356 return NULL;
11357 }
11358 /* single line pattern matching */
11359 return void_node ? void_node : node;
11360
11361 case NODE_BLOCK:
11362 while (node->nd_next) {
11363 node = node->nd_next;
11364 }
11365 node = node->nd_head;
11366 break;
11367
11368 case NODE_BEGIN:
11369 node = node->nd_body;
11370 break;
11371
11372 case NODE_IF:
11373 case NODE_UNLESS:
11374 if (!node->nd_body) {
11375 return NULL;
11376 }
11377 else if (!node->nd_else) {
11378 return NULL;
11379 }
11380 vn = value_expr_check(p, node->nd_body);
11381 if (!vn) return NULL;
11382 if (!void_node) void_node = vn;
11383 node = node->nd_else;
11384 break;
11385
11386 case NODE_AND:
11387 case NODE_OR:
11388 node = node->nd_1st;
11389 break;
11390
11391 case NODE_LASGN:
11392 case NODE_DASGN:
11393 case NODE_DASGN_CURR:
11394 case NODE_MASGN:
11395 mark_lvar_used(p, node);
11396 return NULL;
11397
11398 default:
11399 return NULL;
11400 }
11401 }
11402
11403 return NULL;
11404}
11405
11406static int
11407value_expr_gen(struct parser_params *p, NODE *node)
11408{
11409 NODE *void_node = value_expr_check(p, node);
11410 if (void_node) {
11411 yyerror1(&void_node->nd_loc, "void value expression");
11412 /* or "control never reach"? */
11413 return FALSE;
11414 }
11415 return TRUE;
11416}
11417static void
11418void_expr(struct parser_params *p, NODE *node)
11419{
11420 const char *useless = 0;
11421
11422 if (!RTEST(ruby_verbose)) return;
11423
11424 if (!node || !(node = nd_once_body(node))) return;
11425 switch (nd_type(node)) {
11426 case NODE_OPCALL:
11427 switch (node->nd_mid) {
11428 case '+':
11429 case '-':
11430 case '*':
11431 case '/':
11432 case '%':
11433 case tPOW:
11434 case tUPLUS:
11435 case tUMINUS:
11436 case '|':
11437 case '^':
11438 case '&':
11439 case tCMP:
11440 case '>':
11441 case tGEQ:
11442 case '<':
11443 case tLEQ:
11444 case tEQ:
11445 case tNEQ:
11446 useless = rb_id2name(node->nd_mid);
11447 break;
11448 }
11449 break;
11450
11451 case NODE_LVAR:
11452 case NODE_DVAR:
11453 case NODE_GVAR:
11454 case NODE_IVAR:
11455 case NODE_CVAR:
11456 case NODE_NTH_REF:
11457 case NODE_BACK_REF:
11458 useless = "a variable";
11459 break;
11460 case NODE_CONST:
11461 useless = "a constant";
11462 break;
11463 case NODE_LIT:
11464 case NODE_STR:
11465 case NODE_DSTR:
11466 case NODE_DREGX:
11467 useless = "a literal";
11468 break;
11469 case NODE_COLON2:
11470 case NODE_COLON3:
11471 useless = "::";
11472 break;
11473 case NODE_DOT2:
11474 useless = "..";
11475 break;
11476 case NODE_DOT3:
11477 useless = "...";
11478 break;
11479 case NODE_SELF:
11480 useless = "self";
11481 break;
11482 case NODE_NIL:
11483 useless = "nil";
11484 break;
11485 case NODE_TRUE:
11486 useless = "true";
11487 break;
11488 case NODE_FALSE:
11489 useless = "false";
11490 break;
11491 case NODE_DEFINED:
11492 useless = "defined?";
11493 break;
11494 }
11495
11496 if (useless) {
11497 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
11498 }
11499}
11500
11501static NODE *
11502void_stmts(struct parser_params *p, NODE *node)
11503{
11504 NODE *const n = node;
11505 if (!RTEST(ruby_verbose)) return n;
11506 if (!node) return n;
11507 if (nd_type(node) != NODE_BLOCK) return n;
11508
11509 while (node->nd_next) {
11510 void_expr(p, node->nd_head);
11511 node = node->nd_next;
11512 }
11513 return n;
11514}
11515
11516static NODE *
11517remove_begin(NODE *node)
11518{
11519 NODE **n = &node, *n1 = node;
11520 while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
11521 *n = n1 = n1->nd_body;
11522 }
11523 return node;
11524}
11525
11526static NODE *
11527remove_begin_all(NODE *node)
11528{
11529 NODE **n = &node, *n1 = node;
11530 while (n1 && nd_type(n1) == NODE_BEGIN) {
11531 *n = n1 = n1->nd_body;
11532 }
11533 return node;
11534}
11535
11536static void
11537reduce_nodes(struct parser_params *p, NODE **body)
11538{
11539 NODE *node = *body;
11540
11541 if (!node) {
11542 *body = NEW_NIL(&NULL_LOC);
11543 return;
11544 }
11545#define subnodes(n1, n2) \
11546 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
11547 (!node->n2) ? (body = &node->n1, 1) : \
11548 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
11549
11550 while (node) {
11551 int newline = (int)(node->flags & NODE_FL_NEWLINE);
11552 switch (nd_type(node)) {
11553 end:
11554 case NODE_NIL:
11555 *body = 0;
11556 return;
11557 case NODE_RETURN:
11558 *body = node = node->nd_stts;
11559 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11560 continue;
11561 case NODE_BEGIN:
11562 *body = node = node->nd_body;
11563 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11564 continue;
11565 case NODE_BLOCK:
11566 body = &node->nd_end->nd_head;
11567 break;
11568 case NODE_IF:
11569 case NODE_UNLESS:
11570 if (subnodes(nd_body, nd_else)) break;
11571 return;
11572 case NODE_CASE:
11573 body = &node->nd_body;
11574 break;
11575 case NODE_WHEN:
11576 if (!subnodes(nd_body, nd_next)) goto end;
11577 break;
11578 case NODE_ENSURE:
11579 if (!subnodes(nd_head, nd_resq)) goto end;
11580 break;
11581 case NODE_RESCUE:
11582 if (node->nd_else) {
11583 body = &node->nd_resq;
11584 break;
11585 }
11586 if (!subnodes(nd_head, nd_resq)) goto end;
11587 break;
11588 default:
11589 return;
11590 }
11591 node = *body;
11592 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11593 }
11594
11595#undef subnodes
11596}
11597
11598static int
11599is_static_content(NODE *node)
11600{
11601 if (!node) return 1;
11602 switch (nd_type(node)) {
11603 case NODE_HASH:
11604 if (!(node = node->nd_head)) break;
11605 case NODE_LIST:
11606 do {
11607 if (!is_static_content(node->nd_head)) return 0;
11608 } while ((node = node->nd_next) != 0);
11609 case NODE_LIT:
11610 case NODE_STR:
11611 case NODE_NIL:
11612 case NODE_TRUE:
11613 case NODE_FALSE:
11614 case NODE_ZLIST:
11615 break;
11616 default:
11617 return 0;
11618 }
11619 return 1;
11620}
11621
11622static int
11623assign_in_cond(struct parser_params *p, NODE *node)
11624{
11625 switch (nd_type(node)) {
11626 case NODE_MASGN:
11627 case NODE_LASGN:
11628 case NODE_DASGN:
11629 case NODE_DASGN_CURR:
11630 case NODE_GASGN:
11631 case NODE_IASGN:
11632 break;
11633
11634 default:
11635 return 0;
11636 }
11637
11638 if (!node->nd_value) return 1;
11639 if (is_static_content(node->nd_value)) {
11640 /* reports always */
11641 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11642 }
11643 return 1;
11644}
11645
11646enum cond_type {
11647 COND_IN_OP,
11648 COND_IN_COND,
11649 COND_IN_FF
11650};
11651
11652#define SWITCH_BY_COND_TYPE(t, w, arg) \
11653 switch (t) { \
11654 case COND_IN_OP: break; \
11655 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11656 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11657 }
11658
11659static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11660
11661static NODE*
11662range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11663{
11664 enum node_type type;
11665
11666 if (node == 0) return 0;
11667
11668 type = nd_type(node);
11669 value_expr(node);
11670 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11671 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11672 ID lineno = rb_intern("$.");
11673 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
11674 }
11675 return cond0(p, node, COND_IN_FF, loc);
11676}
11677
11678static NODE*
11679cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11680{
11681 if (node == 0) return 0;
11682 if (!(node = nd_once_body(node))) return 0;
11683 assign_in_cond(p, node);
11684
11685 switch (nd_type(node)) {
11686 case NODE_DSTR:
11687 case NODE_EVSTR:
11688 case NODE_STR:
11689 SWITCH_BY_COND_TYPE(type, warn, "string ")
11690 break;
11691
11692 case NODE_DREGX:
11693 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11694
11695 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11696
11697 case NODE_AND:
11698 case NODE_OR:
11699 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11700 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11701 break;
11702
11703 case NODE_DOT2:
11704 case NODE_DOT3:
11705 node->nd_beg = range_op(p, node->nd_beg, loc);
11706 node->nd_end = range_op(p, node->nd_end, loc);
11707 if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
11708 else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
11709 break;
11710
11711 case NODE_DSYM:
11712 SWITCH_BY_COND_TYPE(type, warning, "string ")
11713 break;
11714
11715 case NODE_LIT:
11716 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11717 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11718 nd_set_type(node, NODE_MATCH);
11719 }
11720 else if (node->nd_lit == Qtrue ||
11721 node->nd_lit == Qfalse) {
11722 /* booleans are OK, e.g., while true */
11723 }
11724 else {
11725 SWITCH_BY_COND_TYPE(type, warning, "")
11726 }
11727 default:
11728 break;
11729 }
11730 return node;
11731}
11732
11733static NODE*
11734cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11735{
11736 if (node == 0) return 0;
11737 return cond0(p, node, COND_IN_COND, loc);
11738}
11739
11740static NODE*
11741method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11742{
11743 if (node == 0) return 0;
11744 return cond0(p, node, COND_IN_OP, loc);
11745}
11746
11747static NODE*
11748new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
11749{
11750 YYLTYPE loc = {*pos, *pos};
11751 return NEW_NIL(&loc);
11752}
11753
11754static NODE*
11755new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11756{
11757 if (!cc) return right;
11758 cc = cond0(p, cc, COND_IN_COND, loc);
11759 return newline_node(NEW_IF(cc, left, right, loc));
11760}
11761
11762static NODE*
11763new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11764{
11765 if (!cc) return right;
11766 cc = cond0(p, cc, COND_IN_COND, loc);
11767 return newline_node(NEW_UNLESS(cc, left, right, loc));
11768}
11769
11770static NODE*
11771logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11772 const YYLTYPE *op_loc, const YYLTYPE *loc)
11773{
11774 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11775 NODE *op;
11776 value_expr(left);
11777 if (left && (enum node_type)nd_type(left) == type) {
11778 NODE *node = left, *second;
11779 while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
11780 node = second;
11781 }
11782 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11783 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11784 left->nd_loc.end_pos = loc->end_pos;
11785 return left;
11786 }
11787 op = NEW_NODE(type, left, right, 0, loc);
11788 nd_set_line(op, op_loc->beg_pos.lineno);
11789 return op;
11790}
11791
11792static void
11793no_blockarg(struct parser_params *p, NODE *node)
11794{
11795 if (node && nd_type(node) == NODE_BLOCK_PASS) {
11796 compile_error(p, "block argument should not be given");
11797 }
11798}
11799
11800static NODE *
11801ret_args(struct parser_params *p, NODE *node)
11802{
11803 if (node) {
11804 no_blockarg(p, node);
11805 if (nd_type(node) == NODE_LIST) {
11806 if (node->nd_next == 0) {
11807 node = node->nd_head;
11808 }
11809 else {
11810 nd_set_type(node, NODE_VALUES);
11811 }
11812 }
11813 }
11814 return node;
11815}
11816
11817static NODE *
11818new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11819{
11820 if (node) no_blockarg(p, node);
11821
11822 return NEW_YIELD(node, loc);
11823}
11824
11825static VALUE
11826negate_lit(struct parser_params *p, VALUE lit)
11827{
11828 if (FIXNUM_P(lit)) {
11829 return LONG2FIX(-FIX2LONG(lit));
11830 }
11831 if (SPECIAL_CONST_P(lit)) {
11832#if USE_FLONUM
11833 if (FLONUM_P(lit)) {
11834 return DBL2NUM(-RFLOAT_VALUE(lit));
11835 }
11836#endif
11837 goto unknown;
11838 }
11839 switch (BUILTIN_TYPE(lit)) {
11840 case T_BIGNUM:
11841 BIGNUM_NEGATE(lit);
11842 lit = rb_big_norm(lit);
11843 break;
11844 case T_RATIONAL:
11845 RATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11846 break;
11847 case T_COMPLEX:
11848 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11849 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11850 break;
11851 case T_FLOAT:
11852 RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
11853 break;
11854 unknown:
11855 default:
11856 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11857 rb_builtin_class_name(lit));
11858 break;
11859 }
11860 return lit;
11861}
11862
11863static NODE *
11864arg_blk_pass(NODE *node1, NODE *node2)
11865{
11866 if (node2) {
11867 if (!node1) return node2;
11868 node2->nd_head = node1;
11869 nd_set_first_lineno(node2, nd_first_lineno(node1));
11870 nd_set_first_column(node2, nd_first_column(node1));
11871 return node2;
11872 }
11873 return node1;
11874}
11875
11876static bool
11877args_info_empty_p(struct rb_args_info *args)
11878{
11879 if (args->pre_args_num) return false;
11880 if (args->post_args_num) return false;
11881 if (args->rest_arg) return false;
11882 if (args->opt_args) return false;
11883 if (args->block_arg) return false;
11884 if (args->kw_args) return false;
11885 if (args->kw_rest_arg) return false;
11886 return true;
11887}
11888
11889static NODE*
11890new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
11891{
11892 int saved_line = p->ruby_sourceline;
11893 struct rb_args_info *args = tail->nd_ainfo;
11894
11895 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
11896 args->pre_init = pre_args ? pre_args->nd_next : 0;
11897
11898 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
11899 args->post_init = post_args ? post_args->nd_next : 0;
11900 args->first_post_arg = post_args ? post_args->nd_pid : 0;
11901
11902 args->rest_arg = rest_arg;
11903
11904 args->opt_args = opt_args;
11905
11906 args->ruby2_keywords = rest_arg == idFWD_REST;
11907
11908 p->ruby_sourceline = saved_line;
11909 nd_set_loc(tail, loc);
11910
11911 return tail;
11912}
11913
11914static NODE*
11915new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
11916{
11917 int saved_line = p->ruby_sourceline;
11918 NODE *node;
11919 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11920 struct rb_args_info *args = ZALLOC(struct rb_args_info);
11921 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
11922 args->imemo = tmpbuf;
11923 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
11924 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11925 if (p->error_p) return node;
11926
11927 args->block_arg = block;
11928 args->kw_args = kw_args;
11929
11930 if (kw_args) {
11931 /*
11932 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
11933 * variable order: k1, kr1, k2, &b, internal_id, krest
11934 * #=> <reorder>
11935 * variable order: kr1, k1, k2, internal_id, krest, &b
11936 */
11937 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
11938 struct vtable *vtargs = p->lvtbl->args;
11939 NODE *kwn = kw_args;
11940
11941 vtable_pop(vtargs, !!block + !!kw_rest_arg);
11942 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
11943 while (kwn) {
11944 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
11945 --kw_vars;
11946 --required_kw_vars;
11947 kwn = kwn->nd_next;
11948 }
11949
11950 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
11951 ID vid = kwn->nd_body->nd_vid;
11952 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
11953 *required_kw_vars++ = vid;
11954 }
11955 else {
11956 *kw_vars++ = vid;
11957 }
11958 }
11959
11960 arg_var(p, kw_bits);
11961 if (kw_rest_arg) arg_var(p, kw_rest_arg);
11962 if (block) arg_var(p, block);
11963
11964 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
11965 args->kw_rest_arg->nd_cflag = kw_bits;
11966 }
11967 else if (kw_rest_arg == idNil) {
11968 args->no_kwarg = 1;
11969 }
11970 else if (kw_rest_arg) {
11971 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
11972 }
11973
11974 p->ruby_sourceline = saved_line;
11975 return node;
11976}
11977
11978static NODE *
11979args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
11980{
11981 if (max_numparam > NO_PARAM) {
11982 if (!args) {
11983 YYLTYPE loc = RUBY_INIT_YYLLOC();
11984 args = new_args_tail(p, 0, 0, 0, 0);
11985 nd_set_loc(args, &loc);
11986 }
11987 args->nd_ainfo->pre_args_num = max_numparam;
11988 }
11989 return args;
11990}
11991
11992static NODE*
11993new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
11994{
11995 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
11996
11997 aryptn->nd_pconst = constant;
11998
11999 if (pre_arg) {
12000 NODE *pre_args = NEW_LIST(pre_arg, loc);
12001 if (apinfo->pre_args) {
12002 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
12003 }
12004 else {
12005 apinfo->pre_args = pre_args;
12006 }
12007 }
12008 return aryptn;
12009}
12010
12011static NODE*
12012new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
12013{
12014 int saved_line = p->ruby_sourceline;
12015 NODE *node;
12016 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12017 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
12018 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
12019 node = NEW_NODE(NODE_ARYPTN, 0, tmpbuf, apinfo, loc);
12020 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12021
12022 apinfo->pre_args = pre_args;
12023
12024 if (has_rest) {
12025 if (rest_arg) {
12026 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
12027 }
12028 else {
12029 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
12030 }
12031 }
12032 else {
12033 apinfo->rest_arg = NULL;
12034 }
12035
12036 apinfo->post_args = post_args;
12037
12038 p->ruby_sourceline = saved_line;
12039 return node;
12040}
12041
12042static NODE*
12043new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
12044{
12045 fndptn->nd_pconst = constant;
12046
12047 return fndptn;
12048}
12049
12050static NODE*
12051new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc)
12052{
12053 int saved_line = p->ruby_sourceline;
12054 NODE *node;
12055 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12056 struct rb_fnd_pattern_info *fpinfo = ZALLOC(struct rb_fnd_pattern_info);
12057 rb_imemo_tmpbuf_set_ptr(tmpbuf, fpinfo);
12058 node = NEW_NODE(NODE_FNDPTN, 0, tmpbuf, fpinfo, loc);
12059 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12060
12061 fpinfo->pre_rest_arg = pre_rest_arg ? assignable(p, pre_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12062 fpinfo->args = args;
12063 fpinfo->post_rest_arg = post_rest_arg ? assignable(p, post_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12064
12065 p->ruby_sourceline = saved_line;
12066 return node;
12067}
12068
12069static NODE*
12070new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
12071{
12072 hshptn->nd_pconst = constant;
12073 return hshptn;
12074}
12075
12076static NODE*
12077new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
12078{
12079 int saved_line = p->ruby_sourceline;
12080 NODE *node, *kw_rest_arg_node;
12081
12082 if (kw_rest_arg == idNil) {
12083 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
12084 }
12085 else if (kw_rest_arg) {
12086 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
12087 }
12088 else {
12089 kw_rest_arg_node = NULL;
12090 }
12091
12092 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
12093
12094 p->ruby_sourceline = saved_line;
12095 return node;
12096}
12097
12098static void
12099warn_one_line_pattern_matching(struct parser_params *p, NODE *node, NODE *pattern, bool right_assign)
12100{
12101 enum node_type type;
12102 type = nd_type(pattern);
12103
12104 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL) &&
12105 !(right_assign && (type == NODE_LASGN || type == NODE_DASGN || type == NODE_DASGN_CURR)))
12106 rb_warn0L_experimental(nd_line(node), "One-line pattern matching is experimental, and the behavior may change in future versions of Ruby!");
12107}
12108
12109static NODE*
12110dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12111{
12112 VALUE lit;
12113
12114 if (!node) {
12115 return NEW_LIT(ID2SYM(idNULL), loc);
12116 }
12117
12118 switch (nd_type(node)) {
12119 case NODE_DSTR:
12120 nd_set_type(node, NODE_DSYM);
12121 nd_set_loc(node, loc);
12122 break;
12123 case NODE_STR:
12124 lit = node->nd_lit;
12125 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
12126 nd_set_type(node, NODE_LIT);
12127 nd_set_loc(node, loc);
12128 break;
12129 default:
12130 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
12131 break;
12132 }
12133 return node;
12134}
12135
12136static int
12137append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
12138{
12139 NODE *node = (NODE *)v;
12140 NODE **result = (NODE **)h;
12141 node->nd_alen = 2;
12142 node->nd_next->nd_end = node->nd_next;
12143 node->nd_next->nd_next = 0;
12144 if (*result)
12145 list_concat(*result, node);
12146 else
12147 *result = node;
12148 return ST_CONTINUE;
12149}
12150
12151static NODE *
12152remove_duplicate_keys(struct parser_params *p, NODE *hash)
12153{
12154 st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
12155 NODE *result = 0;
12156 rb_code_location_t loc = hash->nd_loc;
12157 while (hash && hash->nd_head && hash->nd_next) {
12158 NODE *head = hash->nd_head;
12159 NODE *value = hash->nd_next;
12160 NODE *next = value->nd_next;
12161 VALUE key = (VALUE)head;
12162 st_data_t data;
12163 if (nd_type(head) == NODE_LIT &&
12164 st_lookup(literal_keys, (key = head->nd_lit), &data)) {
12165 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
12166 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
12167 head->nd_lit, nd_line(head));
12168 head = ((NODE *)data)->nd_next;
12169 head->nd_head = block_append(p, head->nd_head, value->nd_head);
12170 }
12171 else {
12172 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
12173 }
12174 hash = next;
12175 }
12176 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
12177 st_free_table(literal_keys);
12178 if (hash) {
12179 if (!result) result = hash;
12180 else list_concat(result, hash);
12181 }
12182 result->nd_loc = loc;
12183 return result;
12184}
12185
12186static NODE *
12187new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12188{
12189 if (hash) hash = remove_duplicate_keys(p, hash);
12190 return NEW_HASH(hash, loc);
12191}
12192#endif
12193
12194static void
12195error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
12196{
12197 if (is_private_local_id(id)) {
12198 return;
12199 }
12200 if (st_is_member(p->pvtbl, id)) {
12201 yyerror1(loc, "duplicated variable name");
12202 }
12203 else {
12204 st_insert(p->pvtbl, (st_data_t)id, 0);
12205 }
12206}
12207
12208static void
12209error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
12210{
12211 if (!p->pktbl) {
12212 p->pktbl = st_init_numtable();
12213 }
12214 else if (st_is_member(p->pktbl, key)) {
12215 yyerror1(loc, "duplicated key name");
12216 return;
12217 }
12218 st_insert(p->pktbl, (st_data_t)key, 0);
12219}
12220
12221#ifndef RIPPER
12222static NODE *
12223new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12224{
12225 return NEW_HASH(hash, loc);
12226}
12227#endif /* !RIPPER */
12228
12229#ifndef RIPPER
12230static NODE *
12231new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12232{
12233 NODE *asgn;
12234
12235 if (lhs) {
12236 ID vid = lhs->nd_vid;
12237 YYLTYPE lhs_loc = lhs->nd_loc;
12238 int shareable = ctxt.shareable_constant_value;
12239 if (shareable) {
12240 switch (nd_type(lhs)) {
12241 case NODE_CDECL:
12242 case NODE_COLON2:
12243 case NODE_COLON3:
12244 break;
12245 default:
12246 shareable = 0;
12247 break;
12248 }
12249 }
12250 if (op == tOROP) {
12251 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12252 lhs->nd_value = rhs;
12253 nd_set_loc(lhs, loc);
12254 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
12255 if (is_notop_id(vid)) {
12256 switch (id_type(vid)) {
12257 case ID_GLOBAL:
12258 case ID_INSTANCE:
12259 case ID_CLASS:
12260 asgn->nd_aid = vid;
12261 }
12262 }
12263 }
12264 else if (op == tANDOP) {
12265 if (shareable) {
12266 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12267 }
12268 lhs->nd_value = rhs;
12269 nd_set_loc(lhs, loc);
12270 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
12271 }
12272 else {
12273 asgn = lhs;
12274 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
12275 if (shareable) {
12276 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12277 }
12278 asgn->nd_value = rhs;
12279 nd_set_loc(asgn, loc);
12280 }
12281 }
12282 else {
12283 asgn = NEW_BEGIN(0, loc);
12284 }
12285 return asgn;
12286}
12287
12288static NODE *
12289new_ary_op_assign(struct parser_params *p, NODE *ary,
12290 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
12291{
12292 NODE *asgn;
12293
12294 args = make_list(args, args_loc);
12295 if (nd_type(args) == NODE_BLOCK_PASS) {
12296 args = NEW_ARGSCAT(args, rhs, loc);
12297 }
12298 else {
12299 args = arg_concat(p, args, rhs, loc);
12300 }
12301 asgn = NEW_OP_ASGN1(ary, op, args, loc);
12302 fixpos(asgn, ary);
12303 return asgn;
12304}
12305
12306static NODE *
12307new_attr_op_assign(struct parser_params *p, NODE *lhs,
12308 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
12309{
12310 NODE *asgn;
12311
12312 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
12313 fixpos(asgn, lhs);
12314 return asgn;
12315}
12316
12317static NODE *
12318new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12319{
12320 NODE *asgn;
12321
12322 if (lhs) {
12323 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
12324 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
12325 }
12326 else {
12327 asgn = NEW_BEGIN(0, loc);
12328 }
12329 fixpos(asgn, lhs);
12330 return asgn;
12331}
12332
12333static NODE *
12334const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
12335{
12336 if (p->ctxt.in_def) {
12337 yyerror1(loc, "dynamic constant assignment");
12338 }
12339 return NEW_CDECL(0, 0, (path), loc);
12340}
12341#else
12342static VALUE
12343const_decl(struct parser_params *p, VALUE path)
12344{
12345 if (p->ctxt.in_def) {
12346 path = assign_error(p, "dynamic constant assignment", path);
12347 }
12348 return path;
12349}
12350
12351static VALUE
12352assign_error(struct parser_params *p, const char *mesg, VALUE a)
12353{
12354 a = dispatch2(assign_error, ERR_MESG(), a);
12355 ripper_error(p);
12356 return a;
12357}
12358
12359static VALUE
12360var_field(struct parser_params *p, VALUE a)
12361{
12362 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
12363}
12364#endif
12365
12366#ifndef RIPPER
12367static NODE *
12368new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
12369{
12370 NODE *result = head;
12371 if (rescue) {
12372 NODE *tmp = rescue_else ? rescue_else : rescue;
12373 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
12374
12375 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
12376 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
12377 }
12378 else if (rescue_else) {
12379 result = block_append(p, result, rescue_else);
12380 }
12381 if (ensure) {
12382 result = NEW_ENSURE(result, ensure, loc);
12383 }
12384 fixpos(result, head);
12385 return result;
12386}
12387#endif
12388
12389static void
12390warn_unused_var(struct parser_params *p, struct local_vars *local)
12391{
12392 int cnt;
12393
12394 if (!local->used) return;
12395 cnt = local->used->pos;
12396 if (cnt != local->vars->pos) {
12397 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
12398 }
12399#ifndef RIPPER
12400 ID *v = local->vars->tbl;
12401 ID *u = local->used->tbl;
12402 for (int i = 0; i < cnt; ++i) {
12403 if (!v[i] || (u[i] & LVAR_USED)) continue;
12404 if (is_private_local_id(v[i])) continue;
12405 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
12406 }
12407#endif
12408}
12409
12410static void
12411local_push(struct parser_params *p, int toplevel_scope)
12412{
12413 struct local_vars *local;
12414 int inherits_dvars = toplevel_scope && compile_for_eval;
12415 int warn_unused_vars = RTEST(ruby_verbose);
12416
12417 local = ALLOC(struct local_vars);
12418 local->prev = p->lvtbl;
12419 local->args = vtable_alloc(0);
12420 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
12421#ifndef RIPPER
12422 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
12423 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
12424 local->numparam.outer = 0;
12425 local->numparam.inner = 0;
12426 local->numparam.current = 0;
12427#endif
12428 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
12429
12430# if WARN_PAST_SCOPE
12431 local->past = 0;
12432# endif
12433 CMDARG_PUSH(0);
12434 COND_PUSH(0);
12435 p->lvtbl = local;
12436}
12437
12438static void
12439local_pop(struct parser_params *p)
12440{
12441 struct local_vars *local = p->lvtbl->prev;
12442 if (p->lvtbl->used) {
12443 warn_unused_var(p, p->lvtbl);
12444 vtable_free(p->lvtbl->used);
12445 }
12446# if WARN_PAST_SCOPE
12447 while (p->lvtbl->past) {
12448 struct vtable *past = p->lvtbl->past;
12449 p->lvtbl->past = past->prev;
12450 vtable_free(past);
12451 }
12452# endif
12453 vtable_free(p->lvtbl->args);
12454 vtable_free(p->lvtbl->vars);
12455 CMDARG_POP();
12456 COND_POP();
12457 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12458 p->lvtbl = local;
12459}
12460
12461#ifndef RIPPER
12462static ID*
12463local_tbl(struct parser_params *p)
12464{
12465 int cnt_args = vtable_size(p->lvtbl->args);
12466 int cnt_vars = vtable_size(p->lvtbl->vars);
12467 int cnt = cnt_args + cnt_vars;
12468 int i, j;
12469 ID *buf;
12470
12471 if (cnt <= 0) return 0;
12472 buf = ALLOC_N(ID, cnt + 2);
12473 MEMCPY(buf+1, p->lvtbl->args->tbl, ID, cnt_args);
12474 /* remove IDs duplicated to warn shadowing */
12475 for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) {
12476 ID id = p->lvtbl->vars->tbl[i];
12477 if (!vtable_included(p->lvtbl->args, id)) {
12478 buf[j++] = id;
12479 }
12480 }
12481 if (--j < cnt) {
12482 REALLOC_N(buf, ID, (cnt = j) + 2);
12483 }
12484 buf[0] = cnt;
12485 rb_ast_add_local_table(p->ast, buf);
12486
12487 return buf;
12488}
12489
12490static NODE*
12491node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
12492{
12493 ID *a0;
12494 NODE *n;
12495
12496 a0 = local_tbl(p);
12497 n = NEW_NODE(type, a0, a1, a2, loc);
12498 return n;
12499}
12500
12501#endif
12502
12503static void
12504numparam_name(struct parser_params *p, ID id)
12505{
12506 if (!NUMPARAM_ID_P(id)) return;
12507 compile_error(p, "_%d is reserved for numbered parameter",
12508 NUMPARAM_ID_TO_IDX(id));
12509}
12510
12511static void
12512arg_var(struct parser_params *p, ID id)
12513{
12514 numparam_name(p, id);
12515 vtable_add(p->lvtbl->args, id);
12516}
12517
12518static void
12519local_var(struct parser_params *p, ID id)
12520{
12521 numparam_name(p, id);
12522 vtable_add(p->lvtbl->vars, id);
12523 if (p->lvtbl->used) {
12524 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
12525 }
12526}
12527
12528static int
12529local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
12530{
12531 struct vtable *vars, *args, *used;
12532
12533 vars = p->lvtbl->vars;
12534 args = p->lvtbl->args;
12535 used = p->lvtbl->used;
12536
12537 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
12538 vars = vars->prev;
12539 args = args->prev;
12540 if (used) used = used->prev;
12541 }
12542
12543 if (vars && vars->prev == DVARS_INHERIT) {
12544 return rb_local_defined(id, p->parent_iseq);
12545 }
12546 else if (vtable_included(args, id)) {
12547 return 1;
12548 }
12549 else {
12550 int i = vtable_included(vars, id);
12551 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
12552 return i != 0;
12553 }
12554}
12555
12556static int
12557local_id(struct parser_params *p, ID id)
12558{
12559 return local_id_ref(p, id, NULL);
12560}
12561
12562static int
12563check_forwarding_args(struct parser_params *p)
12564{
12565 if (local_id(p, idFWD_REST) &&
12566#if idFWD_KWREST
12567 local_id(p, idFWD_KWREST) &&
12568#endif
12569 local_id(p, idFWD_BLOCK)) return TRUE;
12570 compile_error(p, "unexpected ...");
12571 return FALSE;
12572}
12573
12574static void
12575add_forwarding_args(struct parser_params *p)
12576{
12577 arg_var(p, idFWD_REST);
12578#if idFWD_KWREST
12579 arg_var(p, idFWD_KWREST);
12580#endif
12581 arg_var(p, idFWD_BLOCK);
12582}
12583
12584#ifndef RIPPER
12585static NODE *
12586new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
12587{
12588 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, loc), loc);
12589#if idFWD_KWREST
12590 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
12591#endif
12592 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
12593 NODE *args = leading ? rest_arg_append(p, leading, splat, argsloc) : splat;
12594#if idFWD_KWREST
12595 args = arg_append(p, splat, new_hash(p, kwrest, loc), loc);
12596#endif
12597 return arg_blk_pass(args, block);
12598}
12599
12600static NODE *
12601new_args_forward_def(struct parser_params *p, NODE *leading, const YYLTYPE *loc)
12602{
12603 NODE *n = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, loc);
12604 return new_args(p, leading, Qnone, idFWD_REST, Qnone, n, loc);
12605}
12606#endif
12607
12608static NODE *
12609numparam_push(struct parser_params *p)
12610{
12611#ifndef RIPPER
12612 struct local_vars *local = p->lvtbl;
12613 NODE *inner = local->numparam.inner;
12614 if (!local->numparam.outer) {
12615 local->numparam.outer = local->numparam.current;
12616 }
12617 local->numparam.inner = 0;
12618 local->numparam.current = 0;
12619 return inner;
12620#else
12621 return 0;
12622#endif
12623}
12624
12625static void
12626numparam_pop(struct parser_params *p, NODE *prev_inner)
12627{
12628#ifndef RIPPER
12629 struct local_vars *local = p->lvtbl;
12630 if (prev_inner) {
12631 /* prefer first one */
12632 local->numparam.inner = prev_inner;
12633 }
12634 else if (local->numparam.current) {
12635 /* current and inner are exclusive */
12636 local->numparam.inner = local->numparam.current;
12637 }
12638 if (p->max_numparam > NO_PARAM) {
12639 /* current and outer are exclusive */
12640 local->numparam.current = local->numparam.outer;
12641 local->numparam.outer = 0;
12642 }
12643 else {
12644 /* no numbered parameter */
12645 local->numparam.current = 0;
12646 }
12647#endif
12648}
12649
12650static const struct vtable *
12651dyna_push(struct parser_params *p)
12652{
12653 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
12654 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
12655 if (p->lvtbl->used) {
12656 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
12657 }
12658 return p->lvtbl->args;
12659}
12660
12661static void
12662dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
12663{
12664 struct vtable *tmp = *vtblp;
12665 *vtblp = tmp->prev;
12666# if WARN_PAST_SCOPE
12667 if (p->past_scope_enabled) {
12668 tmp->prev = p->lvtbl->past;
12669 p->lvtbl->past = tmp;
12670 return;
12671 }
12672# endif
12673 vtable_free(tmp);
12674}
12675
12676static void
12677dyna_pop_1(struct parser_params *p)
12678{
12679 struct vtable *tmp;
12680
12681 if ((tmp = p->lvtbl->used) != 0) {
12682 warn_unused_var(p, p->lvtbl);
12683 p->lvtbl->used = p->lvtbl->used->prev;
12684 vtable_free(tmp);
12685 }
12686 dyna_pop_vtable(p, &p->lvtbl->args);
12687 dyna_pop_vtable(p, &p->lvtbl->vars);
12688}
12689
12690static void
12691dyna_pop(struct parser_params *p, const struct vtable *lvargs)
12692{
12693 while (p->lvtbl->args != lvargs) {
12694 dyna_pop_1(p);
12695 if (!p->lvtbl->args) {
12696 struct local_vars *local = p->lvtbl->prev;
12697 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12698 p->lvtbl = local;
12699 }
12700 }
12701 dyna_pop_1(p);
12702}
12703
12704static int
12705dyna_in_block(struct parser_params *p)
12706{
12707 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12708}
12709
12710static int
12711dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12712{
12713 struct vtable *vars, *args, *used;
12714 int i;
12715
12716 args = p->lvtbl->args;
12717 vars = p->lvtbl->vars;
12718 used = p->lvtbl->used;
12719
12720 while (!DVARS_TERMINAL_P(vars)) {
12721 if (vtable_included(args, id)) {
12722 return 1;
12723 }
12724 if ((i = vtable_included(vars, id)) != 0) {
12725 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12726 return 1;
12727 }
12728 args = args->prev;
12729 vars = vars->prev;
12730 if (!vidrefp) used = 0;
12731 if (used) used = used->prev;
12732 }
12733
12734 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
12735 return rb_dvar_defined(id, p->parent_iseq);
12736 }
12737
12738 return 0;
12739}
12740
12741static int
12742dvar_defined(struct parser_params *p, ID id)
12743{
12744 return dvar_defined_ref(p, id, NULL);
12745}
12746
12747static int
12748dvar_curr(struct parser_params *p, ID id)
12749{
12750 return (vtable_included(p->lvtbl->args, id) ||
12751 vtable_included(p->lvtbl->vars, id));
12752}
12753
12754static void
12755reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12756{
12757 compile_error(p,
12758 "regexp encoding option '%c' differs from source encoding '%s'",
12759 c, rb_enc_name(rb_enc_get(str)));
12760}
12761
12762#ifndef RIPPER
12763int
12764rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12765{
12766 int c = RE_OPTION_ENCODING_IDX(options);
12767
12768 if (c) {
12769 int opt, idx;
12770 rb_char_to_option_kcode(c, &opt, &idx);
12771 if (idx != ENCODING_GET(str) &&
12772 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12773 goto error;
12774 }
12775 ENCODING_SET(str, idx);
12776 }
12777 else if (RE_OPTION_ENCODING_NONE(options)) {
12778 if (!ENCODING_IS_ASCII8BIT(str) &&
12779 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12780 c = 'n';
12781 goto error;
12782 }
12783 rb_enc_associate(str, rb_ascii8bit_encoding());
12784 }
12785 else if (p->enc == rb_usascii_encoding()) {
12786 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12787 /* raise in re.c */
12788 rb_enc_associate(str, rb_usascii_encoding());
12789 }
12790 else {
12791 rb_enc_associate(str, rb_ascii8bit_encoding());
12792 }
12793 }
12794 return 0;
12795
12796 error:
12797 return c;
12798}
12799
12800static void
12801reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12802{
12803 int c = rb_reg_fragment_setenc(p, str, options);
12804 if (c) reg_fragment_enc_error(p, str, c);
12805}
12806
12807static int
12808reg_fragment_check(struct parser_params* p, VALUE str, int options)
12809{
12810 VALUE err;
12811 reg_fragment_setenc(p, str, options);
12812 err = rb_reg_check_preprocess(str);
12813 if (err != Qnil) {
12814 err = rb_obj_as_string(err);
12815 compile_error(p, "%"PRIsVALUE, err);
12816 return 0;
12817 }
12818 return 1;
12819}
12820
12821typedef struct {
12822 struct parser_params* parser;
12823 rb_encoding *enc;
12824 NODE *succ_block;
12825 const YYLTYPE *loc;
12826} reg_named_capture_assign_t;
12827
12828static int
12829reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12830 int back_num, int *back_refs, OnigRegex regex, void *arg0)
12831{
12832 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12833 struct parser_params* p = arg->parser;
12834 rb_encoding *enc = arg->enc;
12835 long len = name_end - name;
12836 const char *s = (const char *)name;
12837 ID var;
12838 NODE *node, *succ;
12839
12840 if (!len) return ST_CONTINUE;
12841 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
12842 return ST_CONTINUE;
12843
12844 var = intern_cstr(s, len, enc);
12845 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
12846 if (!lvar_defined(p, var)) return ST_CONTINUE;
12847 }
12848 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc);
12849 succ = arg->succ_block;
12850 if (!succ) succ = NEW_BEGIN(0, arg->loc);
12851 succ = block_append(p, succ, node);
12852 arg->succ_block = succ;
12853 return ST_CONTINUE;
12854}
12855
12856static NODE *
12857reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
12858{
12859 reg_named_capture_assign_t arg;
12860
12861 arg.parser = p;
12862 arg.enc = rb_enc_get(regexp);
12863 arg.succ_block = 0;
12864 arg.loc = loc;
12865 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
12866
12867 if (!arg.succ_block) return 0;
12868 return arg.succ_block->nd_next;
12869}
12870
12871static VALUE
12872parser_reg_compile(struct parser_params* p, VALUE str, int options)
12873{
12874 reg_fragment_setenc(p, str, options);
12875 return rb_parser_reg_compile(p, str, options);
12876}
12877
12878VALUE
12879rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
12880{
12881 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
12882}
12883
12884static VALUE
12885reg_compile(struct parser_params* p, VALUE str, int options)
12886{
12887 VALUE re;
12888 VALUE err;
12889
12890 err = rb_errinfo();
12891 re = parser_reg_compile(p, str, options);
12892 if (NIL_P(re)) {
12893 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
12894 rb_set_errinfo(err);
12895 compile_error(p, "%"PRIsVALUE, m);
12896 return Qnil;
12897 }
12898 return re;
12899}
12900#else
12901static VALUE
12902parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
12903{
12904 VALUE err = rb_errinfo();
12905 VALUE re;
12906 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
12907 int c = rb_reg_fragment_setenc(p, str, options);
12908 if (c) reg_fragment_enc_error(p, str, c);
12909 re = rb_parser_reg_compile(p, str, options);
12910 if (NIL_P(re)) {
12911 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
12912 rb_set_errinfo(err);
12913 }
12914 return re;
12915}
12916#endif
12917
12918#ifndef RIPPER
12919void
12920rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
12921{
12922 struct parser_params *p;
12923 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12924 p->do_print = print;
12925 p->do_loop = loop;
12926 p->do_chomp = chomp;
12927 p->do_split = split;
12928}
12929
12930static NODE *
12931parser_append_options(struct parser_params *p, NODE *node)
12932{
12933 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
12934 const YYLTYPE *const LOC = &default_location;
12935
12936 if (p->do_print) {
12937 NODE *print = NEW_FCALL(rb_intern("print"),
12938 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
12939 LOC);
12940 node = block_append(p, node, print);
12941 }
12942
12943 if (p->do_loop) {
12944 if (p->do_split) {
12945 ID ifs = rb_intern("$;");
12946 ID fields = rb_intern("$F");
12947 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
12948 NODE *split = NEW_GASGN(fields,
12949 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12950 rb_intern("split"), args, LOC),
12951 LOC);
12952 node = block_append(p, split, node);
12953 }
12954 if (p->do_chomp) {
12955 NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12956 rb_intern("chomp!"), 0, LOC);
12957 node = block_append(p, chomp, node);
12958 }
12959
12960 node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
12961 }
12962
12963 return node;
12964}
12965
12966void
12967rb_init_parse(void)
12968{
12969 /* just to suppress unused-function warnings */
12970 (void)nodetype;
12971 (void)nodeline;
12972}
12973
12974static ID
12975internal_id(struct parser_params *p)
12976{
12977 const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
12978 ID id = (ID)vtable_size(p->lvtbl->args) + (ID)vtable_size(p->lvtbl->vars);
12979 id = max_id - id;
12980 return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
12981}
12982#endif /* !RIPPER */
12983
12984static void
12985parser_initialize(struct parser_params *p)
12986{
12987 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
12988 p->command_start = TRUE;
12989 p->ruby_sourcefile_string = Qnil;
12990 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
12991 p->node_id = 0;
12992#ifdef RIPPER
12993 p->delayed.token = Qnil;
12994 p->result = Qnil;
12995 p->parsing_thread = Qnil;
12996#else
12997 p->error_buffer = Qfalse;
12998#endif
12999 p->debug_buffer = Qnil;
13000 p->debug_output = rb_ractor_stdout();
13001 p->enc = rb_utf8_encoding();
13002}
13003
13004#ifdef RIPPER
13005#define parser_mark ripper_parser_mark
13006#define parser_free ripper_parser_free
13007#endif
13008
13009static void
13010parser_mark(void *ptr)
13011{
13012 struct parser_params *p = (struct parser_params*)ptr;
13013
13014 rb_gc_mark(p->lex.input);
13015 rb_gc_mark(p->lex.prevline);
13016 rb_gc_mark(p->lex.lastline);
13017 rb_gc_mark(p->lex.nextline);
13018 rb_gc_mark(p->ruby_sourcefile_string);
13019 rb_gc_mark((VALUE)p->lex.strterm);
13020 rb_gc_mark((VALUE)p->ast);
13021 rb_gc_mark(p->case_labels);
13022#ifndef RIPPER
13023 rb_gc_mark(p->debug_lines);
13024 rb_gc_mark(p->compile_option);
13025 rb_gc_mark(p->error_buffer);
13026#else
13027 rb_gc_mark(p->delayed.token);
13028 rb_gc_mark(p->value);
13029 rb_gc_mark(p->result);
13030 rb_gc_mark(p->parsing_thread);
13031#endif
13032 rb_gc_mark(p->debug_buffer);
13033 rb_gc_mark(p->debug_output);
13034#ifdef YYMALLOC
13035 rb_gc_mark((VALUE)p->heap);
13036#endif
13037}
13038
13039static void
13040parser_free(void *ptr)
13041{
13042 struct parser_params *p = (struct parser_params*)ptr;
13043 struct local_vars *local, *prev;
13044
13045 if (p->tokenbuf) {
13046 ruby_sized_xfree(p->tokenbuf, p->toksiz);
13047 }
13048 for (local = p->lvtbl; local; local = prev) {
13049 if (local->vars) xfree(local->vars);
13050 prev = local->prev;
13051 xfree(local);
13052 }
13053 {
13054 token_info *ptinfo;
13055 while ((ptinfo = p->token_info) != 0) {
13056 p->token_info = ptinfo->next;
13057 xfree(ptinfo);
13058 }
13059 }
13060 xfree(ptr);
13061}
13062
13063static size_t
13064parser_memsize(const void *ptr)
13065{
13066 struct parser_params *p = (struct parser_params*)ptr;
13067 struct local_vars *local;
13068 size_t size = sizeof(*p);
13069
13070 size += p->toksiz;
13071 for (local = p->lvtbl; local; local = local->prev) {
13072 size += sizeof(*local);
13073 if (local->vars) size += local->vars->capa * sizeof(ID);
13074 }
13075 return size;
13076}
13077
13078static const rb_data_type_t parser_data_type = {
13079#ifndef RIPPER
13080 "parser",
13081#else
13082 "ripper",
13083#endif
13084 {
13085 parser_mark,
13086 parser_free,
13087 parser_memsize,
13088 },
13089 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
13090};
13091
13092#ifndef RIPPER
13093#undef rb_reserved_word
13094
13095const struct kwtable *
13096rb_reserved_word(const char *str, unsigned int len)
13097{
13098 return reserved_word(str, len);
13099}
13100
13101VALUE
13102rb_parser_new(void)
13103{
13104 struct parser_params *p;
13105 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
13106 &parser_data_type, p);
13107 parser_initialize(p);
13108 return parser;
13109}
13110
13111VALUE
13112rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
13113{
13114 struct parser_params *p;
13115
13116 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13117 p->error_buffer = main ? Qfalse : Qnil;
13118 p->parent_iseq = base;
13119 return vparser;
13120}
13121#endif
13122
13123#ifdef RIPPER
13124#define rb_parser_end_seen_p ripper_parser_end_seen_p
13125#define rb_parser_encoding ripper_parser_encoding
13126#define rb_parser_get_yydebug ripper_parser_get_yydebug
13127#define rb_parser_set_yydebug ripper_parser_set_yydebug
13128#define rb_parser_get_debug_output ripper_parser_get_debug_output
13129#define rb_parser_set_debug_output ripper_parser_set_debug_output
13130static VALUE ripper_parser_end_seen_p(VALUE vparser);
13131static VALUE ripper_parser_encoding(VALUE vparser);
13132static VALUE ripper_parser_get_yydebug(VALUE self);
13133static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
13134static VALUE ripper_parser_get_debug_output(VALUE self);
13135static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
13136
13137/*
13138 * call-seq:
13139 * ripper.error? -> Boolean
13140 *
13141 * Return true if parsed source has errors.
13142 */
13143static VALUE
13144ripper_error_p(VALUE vparser)
13145{
13146 struct parser_params *p;
13147
13148 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13149 return p->error_p ? Qtrue : Qfalse;
13150}
13151#endif
13152
13153/*
13154 * call-seq:
13155 * ripper.end_seen? -> Boolean
13156 *
13157 * Return true if parsed source ended by +\_\_END\_\_+.
13158 */
13159VALUE
13160rb_parser_end_seen_p(VALUE vparser)
13161{
13162 struct parser_params *p;
13163
13164 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13165 return p->ruby__end__seen ? Qtrue : Qfalse;
13166}
13167
13168/*
13169 * call-seq:
13170 * ripper.encoding -> encoding
13171 *
13172 * Return encoding of the source.
13173 */
13174VALUE
13175rb_parser_encoding(VALUE vparser)
13176{
13177 struct parser_params *p;
13178
13179 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13180 return rb_enc_from_encoding(p->enc);
13181}
13182
13183#ifdef RIPPER
13184/*
13185 * call-seq:
13186 * ripper.yydebug -> true or false
13187 *
13188 * Get yydebug.
13189 */
13190VALUE
13191rb_parser_get_yydebug(VALUE self)
13192{
13193 struct parser_params *p;
13194
13195 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13196 return p->debug ? Qtrue : Qfalse;
13197}
13198#endif
13199
13200/*
13201 * call-seq:
13202 * ripper.yydebug = flag
13203 *
13204 * Set yydebug.
13205 */
13206VALUE
13207rb_parser_set_yydebug(VALUE self, VALUE flag)
13208{
13209 struct parser_params *p;
13210
13211 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13212 p->debug = RTEST(flag);
13213 return flag;
13214}
13215
13216/*
13217 * call-seq:
13218 * ripper.debug_output -> obj
13219 *
13220 * Get debug output.
13221 */
13222VALUE
13223rb_parser_get_debug_output(VALUE self)
13224{
13225 struct parser_params *p;
13226
13227 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13228 return p->debug_output;
13229}
13230
13231/*
13232 * call-seq:
13233 * ripper.debug_output = obj
13234 *
13235 * Set debug output.
13236 */
13237VALUE
13238rb_parser_set_debug_output(VALUE self, VALUE output)
13239{
13240 struct parser_params *p;
13241
13242 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13243 return p->debug_output = output;
13244}
13245
13246#ifndef RIPPER
13247#ifdef YYMALLOC
13248#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
13249/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
13250 * potential memory leak */
13251#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
13252#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
13253 (new)->cnt = (cnt), (ptr))
13254
13255void *
13256rb_parser_malloc(struct parser_params *p, size_t size)
13257{
13258 size_t cnt = HEAPCNT(1, size);
13259 rb_imemo_tmpbuf_t *n = NEWHEAP();
13260 void *ptr = xmalloc(size);
13261
13262 return ADD2HEAP(n, cnt, ptr);
13263}
13264
13265void *
13266rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
13267{
13268 size_t cnt = HEAPCNT(nelem, size);
13269 rb_imemo_tmpbuf_t *n = NEWHEAP();
13270 void *ptr = xcalloc(nelem, size);
13271
13272 return ADD2HEAP(n, cnt, ptr);
13273}
13274
13275void *
13276rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
13277{
13278 rb_imemo_tmpbuf_t *n;
13279 size_t cnt = HEAPCNT(1, size);
13280
13281 if (ptr && (n = p->heap) != NULL) {
13282 do {
13283 if (n->ptr == ptr) {
13284 n->ptr = ptr = xrealloc(ptr, size);
13285 if (n->cnt) n->cnt = cnt;
13286 return ptr;
13287 }
13288 } while ((n = n->next) != NULL);
13289 }
13290 n = NEWHEAP();
13291 ptr = xrealloc(ptr, size);
13292 return ADD2HEAP(n, cnt, ptr);
13293}
13294
13295void
13296rb_parser_free(struct parser_params *p, void *ptr)
13297{
13298 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
13299
13300 while ((n = *prev) != NULL) {
13301 if (n->ptr == ptr) {
13302 *prev = n->next;
13303 rb_gc_force_recycle((VALUE)n);
13304 break;
13305 }
13306 prev = &n->next;
13307 }
13308 xfree(ptr);
13309}
13310#endif
13311
13312void
13313rb_parser_printf(struct parser_params *p, const char *fmt, ...)
13314{
13315 va_list ap;
13316 VALUE mesg = p->debug_buffer;
13317
13318 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
13319 va_start(ap, fmt);
13320 rb_str_vcatf(mesg, fmt, ap);
13321 va_end(ap);
13322 if (RSTRING_END(mesg)[-1] == '\n') {
13323 rb_io_write(p->debug_output, mesg);
13324 p->debug_buffer = Qnil;
13325 }
13326}
13327
13328static void
13329parser_compile_error(struct parser_params *p, const char *fmt, ...)
13330{
13331 va_list ap;
13332
13333 rb_io_flush(p->debug_output);
13334 p->error_p = 1;
13335 va_start(ap, fmt);
13336 p->error_buffer =
13337 rb_syntax_error_append(p->error_buffer,
13338 p->ruby_sourcefile_string,
13339 p->ruby_sourceline,
13340 rb_long2int(p->lex.pcur - p->lex.pbeg),
13341 p->enc, fmt, ap);
13342 va_end(ap);
13343}
13344
13345static size_t
13346count_char(const char *str, int c)
13347{
13348 int n = 0;
13349 while (str[n] == c) ++n;
13350 return n;
13351}
13352
13353/*
13354 * strip enclosing double-quotes, same as the default yytnamerr except
13355 * for that single-quotes matching back-quotes do not stop stripping.
13356 *
13357 * "\"`class' keyword\"" => "`class' keyword"
13358 */
13359RUBY_FUNC_EXPORTED size_t
13360rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
13361{
13362 if (*yystr == '"') {
13363 size_t yyn = 0, bquote = 0;
13364 const char *yyp = yystr;
13365
13366 while (*++yyp) {
13367 switch (*yyp) {
13368 case '`':
13369 if (!bquote) {
13370 bquote = count_char(yyp+1, '`') + 1;
13371 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
13372 yyn += bquote;
13373 yyp += bquote - 1;
13374 break;
13375 }
13376 goto default_char;
13377
13378 case '\'':
13379 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
13380 if (yyres) memcpy(yyres + yyn, yyp, bquote);
13381 yyn += bquote;
13382 yyp += bquote - 1;
13383 bquote = 0;
13384 break;
13385 }
13386 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
13387 if (yyres) memcpy(yyres + yyn, yyp, 3);
13388 yyn += 3;
13389 yyp += 2;
13390 break;
13391 }
13392 goto do_not_strip_quotes;
13393
13394 case ',':
13395 goto do_not_strip_quotes;
13396
13397 case '\\':
13398 if (*++yyp != '\\')
13399 goto do_not_strip_quotes;
13400 /* Fall through. */
13401 default_char:
13402 default:
13403 if (yyres)
13404 yyres[yyn] = *yyp;
13405 yyn++;
13406 break;
13407
13408 case '"':
13409 case '\0':
13410 if (yyres)
13411 yyres[yyn] = '\0';
13412 return yyn;
13413 }
13414 }
13415 do_not_strip_quotes: ;
13416 }
13417
13418 if (!yyres) return strlen(yystr);
13419
13420 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
13421}
13422#endif
13423
13424#ifdef RIPPER
13425#ifdef RIPPER_DEBUG
13426/* :nodoc: */
13427static VALUE
13428ripper_validate_object(VALUE self, VALUE x)
13429{
13430 if (x == Qfalse) return x;
13431 if (x == Qtrue) return x;
13432 if (x == Qnil) return x;
13433 if (x == Qundef)
13434 rb_raise(rb_eArgError, "Qundef given");
13435 if (FIXNUM_P(x)) return x;
13436 if (SYMBOL_P(x)) return x;
13437 switch (BUILTIN_TYPE(x)) {
13438 case T_STRING:
13439 case T_OBJECT:
13440 case T_ARRAY:
13441 case T_BIGNUM:
13442 case T_FLOAT:
13443 case T_COMPLEX:
13444 case T_RATIONAL:
13445 break;
13446 case T_NODE:
13447 if (nd_type((NODE *)x) != NODE_RIPPER) {
13448 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
13449 }
13450 x = ((NODE *)x)->nd_rval;
13451 break;
13452 default:
13453 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
13454 (void *)x, rb_obj_classname(x));
13455 }
13456 if (!RBASIC_CLASS(x)) {
13457 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
13458 (void *)x, rb_builtin_type_name(TYPE(x)));
13459 }
13460 return x;
13461}
13462#endif
13463
13464#define validate(x) ((x) = get_value(x))
13465
13466static VALUE
13467ripper_dispatch0(struct parser_params *p, ID mid)
13468{
13469 return rb_funcall(p->value, mid, 0);
13470}
13471
13472static VALUE
13473ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
13474{
13475 validate(a);
13476 return rb_funcall(p->value, mid, 1, a);
13477}
13478
13479static VALUE
13480ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
13481{
13482 validate(a);
13483 validate(b);
13484 return rb_funcall(p->value, mid, 2, a, b);
13485}
13486
13487static VALUE
13488ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
13489{
13490 validate(a);
13491 validate(b);
13492 validate(c);
13493 return rb_funcall(p->value, mid, 3, a, b, c);
13494}
13495
13496static VALUE
13497ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
13498{
13499 validate(a);
13500 validate(b);
13501 validate(c);
13502 validate(d);
13503 return rb_funcall(p->value, mid, 4, a, b, c, d);
13504}
13505
13506static VALUE
13507ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
13508{
13509 validate(a);
13510 validate(b);
13511 validate(c);
13512 validate(d);
13513 validate(e);
13514 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
13515}
13516
13517static VALUE
13518ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
13519{
13520 validate(a);
13521 validate(b);
13522 validate(c);
13523 validate(d);
13524 validate(e);
13525 validate(f);
13526 validate(g);
13527 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
13528}
13529
13530static ID
13531ripper_get_id(VALUE v)
13532{
13533 NODE *nd;
13534 if (!RB_TYPE_P(v, T_NODE)) return 0;
13535 nd = (NODE *)v;
13536 if (nd_type(nd) != NODE_RIPPER) return 0;
13537 return nd->nd_vid;
13538}
13539
13540static VALUE
13541ripper_get_value(VALUE v)
13542{
13543 NODE *nd;
13544 if (v == Qundef) return Qnil;
13545 if (!RB_TYPE_P(v, T_NODE)) return v;
13546 nd = (NODE *)v;
13547 if (nd_type(nd) != NODE_RIPPER) return Qnil;
13548 return nd->nd_rval;
13549}
13550
13551static void
13552ripper_error(struct parser_params *p)
13553{
13554 p->error_p = TRUE;
13555}
13556
13557static void
13558ripper_compile_error(struct parser_params *p, const char *fmt, ...)
13559{
13560 VALUE str;
13561 va_list args;
13562
13563 va_start(args, fmt);
13564 str = rb_vsprintf(fmt, args);
13565 va_end(args);
13566 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
13567 ripper_error(p);
13568}
13569
13570static VALUE
13571ripper_lex_get_generic(struct parser_params *p, VALUE src)
13572{
13573 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
13574 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
13575 rb_raise(rb_eTypeError,
13576 "gets returned %"PRIsVALUE" (expected String or nil)",
13577 rb_obj_class(line));
13578 }
13579 return line;
13580}
13581
13582static VALUE
13583ripper_lex_io_get(struct parser_params *p, VALUE src)
13584{
13585 return rb_io_gets(src);
13586}
13587
13588static VALUE
13589ripper_s_allocate(VALUE klass)
13590{
13591 struct parser_params *p;
13592 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
13593 &parser_data_type, p);
13594 p->value = self;
13595 return self;
13596}
13597
13598#define ripper_initialized_p(r) ((r)->lex.input != 0)
13599
13600/*
13601 * call-seq:
13602 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
13603 *
13604 * Create a new Ripper object.
13605 * _src_ must be a String, an IO, or an Object which has #gets method.
13606 *
13607 * This method does not starts parsing.
13608 * See also Ripper#parse and Ripper.parse.
13609 */
13610static VALUE
13611ripper_initialize(int argc, VALUE *argv, VALUE self)
13612{
13613 struct parser_params *p;
13614 VALUE src, fname, lineno;
13615
13616 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13617 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
13618 if (RB_TYPE_P(src, T_FILE)) {
13619 p->lex.gets = ripper_lex_io_get;
13620 }
13621 else if (rb_respond_to(src, id_gets)) {
13622 p->lex.gets = ripper_lex_get_generic;
13623 }
13624 else {
13625 StringValue(src);
13626 p->lex.gets = lex_get_str;
13627 }
13628 p->lex.input = src;
13629 p->eofp = 0;
13630 if (NIL_P(fname)) {
13631 fname = STR_NEW2("(ripper)");
13632 OBJ_FREEZE(fname);
13633 }
13634 else {
13635 StringValueCStr(fname);
13636 fname = rb_str_new_frozen(fname);
13637 }
13638 parser_initialize(p);
13639
13640 p->ruby_sourcefile_string = fname;
13641 p->ruby_sourcefile = RSTRING_PTR(fname);
13642 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
13643
13644 return Qnil;
13645}
13646
13647static VALUE
13648ripper_parse0(VALUE parser_v)
13649{
13650 struct parser_params *p;
13651
13652 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13653 parser_prepare(p);
13654 p->ast = rb_ast_new();
13655 ripper_yyparse((void*)p);
13656 rb_ast_dispose(p->ast);
13657 p->ast = 0;
13658 return p->result;
13659}
13660
13661static VALUE
13662ripper_ensure(VALUE parser_v)
13663{
13664 struct parser_params *p;
13665
13666 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13667 p->parsing_thread = Qnil;
13668 return Qnil;
13669}
13670
13671/*
13672 * call-seq:
13673 * ripper.parse
13674 *
13675 * Start parsing and returns the value of the root action.
13676 */
13677static VALUE
13678ripper_parse(VALUE self)
13679{
13680 struct parser_params *p;
13681
13682 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13683 if (!ripper_initialized_p(p)) {
13684 rb_raise(rb_eArgError, "method called for uninitialized object");
13685 }
13686 if (!NIL_P(p->parsing_thread)) {
13687 if (p->parsing_thread == rb_thread_current())
13688 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13689 else
13690 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13691 }
13692 p->parsing_thread = rb_thread_current();
13693 rb_ensure(ripper_parse0, self, ripper_ensure, self);
13694
13695 return p->result;
13696}
13697
13698/*
13699 * call-seq:
13700 * ripper.column -> Integer
13701 *
13702 * Return column number of current parsing line.
13703 * This number starts from 0.
13704 */
13705static VALUE
13706ripper_column(VALUE self)
13707{
13708 struct parser_params *p;
13709 long col;
13710
13711 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13712 if (!ripper_initialized_p(p)) {
13713 rb_raise(rb_eArgError, "method called for uninitialized object");
13714 }
13715 if (NIL_P(p->parsing_thread)) return Qnil;
13716 col = p->lex.ptok - p->lex.pbeg;
13717 return LONG2NUM(col);
13718}
13719
13720/*
13721 * call-seq:
13722 * ripper.filename -> String
13723 *
13724 * Return current parsing filename.
13725 */
13726static VALUE
13727ripper_filename(VALUE self)
13728{
13729 struct parser_params *p;
13730
13731 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13732 if (!ripper_initialized_p(p)) {
13733 rb_raise(rb_eArgError, "method called for uninitialized object");
13734 }
13735 return p->ruby_sourcefile_string;
13736}
13737
13738/*
13739 * call-seq:
13740 * ripper.lineno -> Integer
13741 *
13742 * Return line number of current parsing line.
13743 * This number starts from 1.
13744 */
13745static VALUE
13746ripper_lineno(VALUE self)
13747{
13748 struct parser_params *p;
13749
13750 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13751 if (!ripper_initialized_p(p)) {
13752 rb_raise(rb_eArgError, "method called for uninitialized object");
13753 }
13754 if (NIL_P(p->parsing_thread)) return Qnil;
13755 return INT2NUM(p->ruby_sourceline);
13756}
13757
13758/*
13759 * call-seq:
13760 * ripper.state -> Integer
13761 *
13762 * Return scanner state of current token.
13763 */
13764static VALUE
13765ripper_state(VALUE self)
13766{
13767 struct parser_params *p;
13768
13769 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13770 if (!ripper_initialized_p(p)) {
13771 rb_raise(rb_eArgError, "method called for uninitialized object");
13772 }
13773 if (NIL_P(p->parsing_thread)) return Qnil;
13774 return INT2NUM(p->lex.state);
13775}
13776
13777/*
13778 * call-seq:
13779 * ripper.token -> String
13780 *
13781 * Return the current token string.
13782 */
13783static VALUE
13784ripper_token(VALUE self)
13785{
13786 struct parser_params *p;
13787 long pos, len;
13788
13789 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13790 if (!ripper_initialized_p(p)) {
13791 rb_raise(rb_eArgError, "method called for uninitialized object");
13792 }
13793 if (NIL_P(p->parsing_thread)) return Qnil;
13794 pos = p->lex.ptok - p->lex.pbeg;
13795 len = p->lex.pcur - p->lex.ptok;
13796 return rb_str_subseq(p->lex.lastline, pos, len);
13797}
13798
13799#ifdef RIPPER_DEBUG
13800/* :nodoc: */
13801static VALUE
13802ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13803{
13804 StringValue(msg);
13805 if (obj == Qundef) {
13806 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13807 }
13808 return Qnil;
13809}
13810
13811/* :nodoc: */
13812static VALUE
13813ripper_value(VALUE self, VALUE obj)
13814{
13815 return ULONG2NUM(obj);
13816}
13817#endif
13818
13819/*
13820 * call-seq:
13821 * Ripper.lex_state_name(integer) -> string
13822 *
13823 * Returns a string representation of lex_state.
13824 */
13825static VALUE
13826ripper_lex_state_name(VALUE self, VALUE state)
13827{
13828 return rb_parser_lex_state_name(NUM2INT(state));
13829}
13830
13831void
13832Init_ripper(void)
13833{
13834 ripper_init_eventids1();
13835 ripper_init_eventids2();
13836 id_warn = rb_intern_const("warn");
13837 id_warning = rb_intern_const("warning");
13838 id_gets = rb_intern_const("gets");
13839 id_assoc = rb_intern_const("=>");
13840
13841 (void)yystpcpy; /* may not used in newer bison */
13842
13843 InitVM(ripper);
13844}
13845
13846void
13847InitVM_ripper(void)
13848{
13849 VALUE Ripper;
13850
13851 Ripper = rb_define_class("Ripper", rb_cObject);
13852 /* version of Ripper */
13853 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
13854 rb_define_alloc_func(Ripper, ripper_s_allocate);
13855 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
13856 rb_define_method(Ripper, "parse", ripper_parse, 0);
13857 rb_define_method(Ripper, "column", ripper_column, 0);
13858 rb_define_method(Ripper, "filename", ripper_filename, 0);
13859 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
13860 rb_define_method(Ripper, "state", ripper_state, 0);
13861 rb_define_method(Ripper, "token", ripper_token, 0);
13862 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
13863 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
13864 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
13865 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
13866 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
13867 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
13868 rb_define_method(Ripper, "error?", ripper_error_p, 0);
13869#ifdef RIPPER_DEBUG
13870 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
13871 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
13872 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
13873#endif
13874
13875 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
13876 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
13877
13878 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
13879
13880 /* ignore newline, +/- is a sign. */
13881 rb_define_const(Ripper, "EXPR_BEG", INT2NUM(EXPR_BEG));
13882 /* newline significant, +/- is an operator. */
13883 rb_define_const(Ripper, "EXPR_END", INT2NUM(EXPR_END));
13884 /* ditto, and unbound braces. */
13885 rb_define_const(Ripper, "EXPR_ENDARG", INT2NUM(EXPR_ENDARG));
13886 /* ditto, and unbound braces. */
13887 rb_define_const(Ripper, "EXPR_ENDFN", INT2NUM(EXPR_ENDFN));
13888 /* newline significant, +/- is an operator. */
13889 rb_define_const(Ripper, "EXPR_ARG", INT2NUM(EXPR_ARG));
13890 /* newline significant, +/- is an operator. */
13891 rb_define_const(Ripper, "EXPR_CMDARG", INT2NUM(EXPR_CMDARG));
13892 /* newline significant, +/- is an operator. */
13893 rb_define_const(Ripper, "EXPR_MID", INT2NUM(EXPR_MID));
13894 /* ignore newline, no reserved words. */
13895 rb_define_const(Ripper, "EXPR_FNAME", INT2NUM(EXPR_FNAME));
13896 /* right after `.' or `::', no reserved words. */
13897 rb_define_const(Ripper, "EXPR_DOT", INT2NUM(EXPR_DOT));
13898 /* immediate after `class', no here document. */
13899 rb_define_const(Ripper, "EXPR_CLASS", INT2NUM(EXPR_CLASS));
13900 /* flag bit, label is allowed. */
13901 rb_define_const(Ripper, "EXPR_LABEL", INT2NUM(EXPR_LABEL));
13902 /* flag bit, just after a label. */
13903 rb_define_const(Ripper, "EXPR_LABELED", INT2NUM(EXPR_LABELED));
13904 /* symbol literal as FNAME. */
13905 rb_define_const(Ripper, "EXPR_FITEM", INT2NUM(EXPR_FITEM));
13906 /* equals to +EXPR_BEG+ */
13907 rb_define_const(Ripper, "EXPR_VALUE", INT2NUM(EXPR_VALUE));
13908 /* equals to <tt>(EXPR_BEG | EXPR_MID | EXPR_CLASS)</tt> */
13909 rb_define_const(Ripper, "EXPR_BEG_ANY", INT2NUM(EXPR_BEG_ANY));
13910 /* equals to <tt>(EXPR_ARG | EXPR_CMDARG)</tt> */
13911 rb_define_const(Ripper, "EXPR_ARG_ANY", INT2NUM(EXPR_ARG_ANY));
13912 /* equals to <tt>(EXPR_END | EXPR_ENDARG | EXPR_ENDFN)</tt> */
13913 rb_define_const(Ripper, "EXPR_END_ANY", INT2NUM(EXPR_END_ANY));
13914 /* equals to +0+ */
13915 rb_define_const(Ripper, "EXPR_NONE", INT2NUM(EXPR_NONE));
13916
13917 ripper_init_eventids1_table(Ripper);
13918 ripper_init_eventids2_table(Ripper);
13919
13920# if 0
13921 /* Hack to let RDoc document SCRIPT_LINES__ */
13922
13923 /*
13924 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
13925 * after the assignment will be added as an Array of lines with the file
13926 * name as the key.
13927 */
13928 rb_define_global_const("SCRIPT_LINES__", Qnil);
13929#endif
13930
13931}
13932#endif /* RIPPER */
13933
13934/*
13935 * Local variables:
13936 * mode: c
13937 * c-file-style: "ruby"
13938 * End:
13939 */