Ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e0ba6b95ab71a441357ed5484e33498)
parse.y
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/variable.h"
46#include "node.h"
47#include "probes.h"
48#include "regenc.h"
49#include "ruby/encoding.h"
50#include "ruby/regex.h"
51#include "ruby/ruby.h"
52#include "ruby/st.h"
53#include "ruby/util.h"
54#include "ruby/ractor.h"
55#include "symbol.h"
56
57enum shareability {
58 shareable_none,
59 shareable_literal,
60 shareable_copy,
61 shareable_everything,
62};
63
64struct lex_context {
65 unsigned int in_defined: 1;
66 unsigned int in_kwarg: 1;
67 unsigned int in_argdef: 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#define YY_(Msgid) \
110 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
111 "nesting too deep" : (Msgid))
112
113#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
114 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
115#define RUBY_SET_YYLLOC_OF_NONE(Current) \
116 rb_parser_set_location_of_none(p, &(Current))
117#define RUBY_SET_YYLLOC(Current) \
118 rb_parser_set_location(p, &(Current))
119#define RUBY_INIT_YYLLOC() \
120 { \
121 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
122 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
123 }
124
125enum lex_state_bits {
126 EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
127 EXPR_END_bit, /* newline significant, +/- is an operator. */
128 EXPR_ENDARG_bit, /* ditto, and unbound braces. */
129 EXPR_ENDFN_bit, /* ditto, and unbound braces. */
130 EXPR_ARG_bit, /* newline significant, +/- is an operator. */
131 EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
132 EXPR_MID_bit, /* newline significant, +/- is an operator. */
133 EXPR_FNAME_bit, /* ignore newline, no reserved words. */
134 EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
135 EXPR_CLASS_bit, /* immediate after `class', no here document. */
136 EXPR_LABEL_bit, /* flag bit, label is allowed. */
137 EXPR_LABELED_bit, /* flag bit, just after a label. */
138 EXPR_FITEM_bit, /* symbol literal as FNAME. */
139 EXPR_MAX_STATE
140};
141/* examine combinations */
142enum lex_state_e {
143#define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
144 DEF_EXPR(BEG),
145 DEF_EXPR(END),
146 DEF_EXPR(ENDARG),
147 DEF_EXPR(ENDFN),
148 DEF_EXPR(ARG),
149 DEF_EXPR(CMDARG),
150 DEF_EXPR(MID),
151 DEF_EXPR(FNAME),
152 DEF_EXPR(DOT),
153 DEF_EXPR(CLASS),
154 DEF_EXPR(LABEL),
155 DEF_EXPR(LABELED),
156 DEF_EXPR(FITEM),
157 EXPR_VALUE = EXPR_BEG,
158 EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
159 EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
160 EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN),
161 EXPR_NONE = 0
162};
163#define IS_lex_state_for(x, ls) ((x) & (ls))
164#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
165#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
166#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
167
168# define SET_LEX_STATE(ls) \
169 parser_set_lex_state(p, ls, __LINE__)
170static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
171
172typedef VALUE stack_type;
173
174static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
175
176# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
177# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
178# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
179# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
180# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
181
182/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
183 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
184#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
185#define COND_POP() BITSTACK_POP(cond_stack)
186#define COND_P() BITSTACK_SET_P(cond_stack)
187#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
188
189/* A flag to identify keyword_do_block; "do" keyword after command_call.
190 Example: `foo 1, 2 do`. */
191#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
192#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
193#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
194#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
195
196struct vtable {
197 ID *tbl;
198 int pos;
199 int capa;
200 struct vtable *prev;
201};
202
203struct local_vars {
204 struct vtable *args;
205 struct vtable *vars;
206 struct vtable *used;
207# if WARN_PAST_SCOPE
208 struct vtable *past;
209# endif
210 struct local_vars *prev;
211# ifndef RIPPER
212 struct {
213 NODE *outer, *inner, *current;
214 } numparam;
215# endif
216};
217
218enum {
219 ORDINAL_PARAM = -1,
220 NO_PARAM = 0,
221 NUMPARAM_MAX = 9,
222};
223
224#define NUMPARAM_ID_P(id) numparam_id_p(id)
225#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - tNUMPARAM_1 + 1)
226#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 + (idx) - 1))
227static int
228numparam_id_p(ID id)
229{
230 if (!is_local_id(id)) return 0;
231 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
232 return idx > 0 && idx <= NUMPARAM_MAX;
233}
234static void numparam_name(struct parser_params *p, ID id);
235
236#define DVARS_INHERIT ((void*)1)
237#define DVARS_TOPSCOPE NULL
238#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
239
240typedef struct token_info {
241 const char *token;
242 rb_code_position_t beg;
243 int indent;
244 int nonspc;
245 struct token_info *next;
246} token_info;
247
248typedef struct rb_strterm_struct rb_strterm_t;
249
250/*
251 Structure of Lexer Buffer:
252
253 lex.pbeg lex.ptok lex.pcur lex.pend
254 | | | |
255 |------------+------------+------------|
256 |<---------->|
257 token
258*/
259struct parser_params {
260 rb_imemo_tmpbuf_t *heap;
261
262 YYSTYPE *lval;
263
264 struct {
265 rb_strterm_t *strterm;
266 VALUE (*gets)(struct parser_params*,VALUE);
267 VALUE input;
268 VALUE prevline;
269 VALUE lastline;
270 VALUE nextline;
271 const char *pbeg;
272 const char *pcur;
273 const char *pend;
274 const char *ptok;
275 union {
276 long ptr;
277 VALUE (*call)(VALUE, int);
278 } gets_;
279 enum lex_state_e state;
280 /* track the nest level of any parens "()[]{}" */
281 int paren_nest;
282 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
283 int lpar_beg;
284 /* track the nest level of only braces "{}" */
285 int brace_nest;
286 } lex;
287 stack_type cond_stack;
288 stack_type cmdarg_stack;
289 int tokidx;
290 int toksiz;
291 int tokline;
292 int heredoc_end;
293 int heredoc_indent;
294 int heredoc_line_indent;
295 char *tokenbuf;
296 struct local_vars *lvtbl;
297 st_table *pvtbl;
298 st_table *pktbl;
299 int line_count;
300 int ruby_sourceline; /* current line no. */
301 const char *ruby_sourcefile; /* current source file */
302 VALUE ruby_sourcefile_string;
303 rb_encoding *enc;
304 token_info *token_info;
305 VALUE case_labels;
306 VALUE compile_option;
307
308 VALUE debug_buffer;
309 VALUE debug_output;
310
311 ID cur_arg;
312
313 rb_ast_t *ast;
314 int node_id;
315
316 int max_numparam;
317
318 struct lex_context ctxt;
319
320 unsigned int command_start:1;
321 unsigned int eofp: 1;
322 unsigned int ruby__end__seen: 1;
323 unsigned int debug: 1;
324 unsigned int has_shebang: 1;
325 unsigned int token_seen: 1;
326 unsigned int token_info_enabled: 1;
327# if WARN_PAST_SCOPE
328 unsigned int past_scope_enabled: 1;
329# endif
330 unsigned int error_p: 1;
331 unsigned int cr_seen: 1;
332
333#ifndef RIPPER
334 /* Ruby core only */
335
336 unsigned int do_print: 1;
337 unsigned int do_loop: 1;
338 unsigned int do_chomp: 1;
339 unsigned int do_split: 1;
340 unsigned int keep_script_lines: 1;
341
342 NODE *eval_tree_begin;
343 NODE *eval_tree;
344 VALUE error_buffer;
345 VALUE debug_lines;
346 const struct rb_iseq_struct *parent_iseq;
347#else
348 /* Ripper only */
349
350 struct {
351 VALUE token;
352 int line;
353 int col;
354 } delayed;
355
356 VALUE value;
357 VALUE result;
358 VALUE parsing_thread;
359#endif
360};
361
362#define intern_cstr(n,l,en) rb_intern3(n,l,en)
363
364#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
365#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
366#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
367#define STR_NEW3(ptr,len,e,func) parser_str_new((ptr),(len),(e),(func),p->enc)
368#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
369
370static st_table *
371push_pvtbl(struct parser_params *p)
372{
373 st_table *tbl = p->pvtbl;
374 p->pvtbl = st_init_numtable();
375 return tbl;
376}
377
378static void
379pop_pvtbl(struct parser_params *p, st_table *tbl)
380{
381 st_free_table(p->pvtbl);
382 p->pvtbl = tbl;
383}
384
385static st_table *
386push_pktbl(struct parser_params *p)
387{
388 st_table *tbl = p->pktbl;
389 p->pktbl = 0;
390 return tbl;
391}
392
393static void
394pop_pktbl(struct parser_params *p, st_table *tbl)
395{
396 if (p->pktbl) st_free_table(p->pktbl);
397 p->pktbl = tbl;
398}
399
400RBIMPL_ATTR_NONNULL((1, 2, 3))
401static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
402RBIMPL_ATTR_NONNULL((1, 2))
403static int parser_yyerror0(struct parser_params*, const char*);
404#define yyerror0(msg) parser_yyerror0(p, (msg))
405#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
406#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
407#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
408
409static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
410static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
411static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
412static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
413static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
414
415#ifdef RIPPER
416#define compile_for_eval (0)
417#else
418#define compile_for_eval (p->parent_iseq != 0)
419#endif
420
421#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
422
423#define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
424#define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
425#define NEW_QCALL(q,r,m,a,loc) NEW_NODE(NODE_CALL_Q(q),r,m,a,loc)
426
427#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
428
429#define ANON_BLOCK_ID '&'
430
431static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
432
433#ifndef RIPPER
434static inline void
435rb_discard_node(struct parser_params *p, NODE *n)
436{
437 rb_ast_delete_node(p->ast, n);
438}
439#endif
440
441#ifdef RIPPER
442static inline VALUE
443add_mark_object(struct parser_params *p, VALUE obj)
444{
445 if (!SPECIAL_CONST_P(obj)
446 && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */
447 ) {
448 rb_ast_add_mark_object(p->ast, obj);
449 }
450 return obj;
451}
452#else
453static NODE* node_newnode_with_locals(struct parser_params *, enum node_type, VALUE, VALUE, const rb_code_location_t*);
454#endif
455
456static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE, const rb_code_location_t*);
457#define rb_node_newnode(type, a1, a2, a3, loc) node_newnode(p, (type), (a1), (a2), (a3), (loc))
458
459static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
460
461static int
462parser_get_node_id(struct parser_params *p)
463{
464 int node_id = p->node_id;
465 p->node_id++;
466 return node_id;
467}
468
469#ifndef RIPPER
470static inline void
471set_line_body(NODE *body, int line)
472{
473 if (!body) return;
474 switch (nd_type(body)) {
475 case NODE_RESCUE:
476 case NODE_ENSURE:
477 nd_set_line(body, line);
478 }
479}
480
481#define yyparse ruby_yyparse
482
483static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
484static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
485#define new_nil(loc) NEW_NIL(loc)
486static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
487static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
488static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
489static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
490
491static NODE *newline_node(NODE*);
492static void fixpos(NODE*,NODE*);
493
494static int value_expr_gen(struct parser_params*,NODE*);
495static void void_expr(struct parser_params*,NODE*);
496static NODE *remove_begin(NODE*);
497static NODE *remove_begin_all(NODE*);
498#define value_expr(node) value_expr_gen(p, (node))
499static NODE *void_stmts(struct parser_params*,NODE*);
500static void reduce_nodes(struct parser_params*,NODE**);
501static void block_dup_check(struct parser_params*,NODE*,NODE*);
502
503static NODE *block_append(struct parser_params*,NODE*,NODE*);
504static NODE *list_append(struct parser_params*,NODE*,NODE*);
505static NODE *list_concat(NODE*,NODE*);
506static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
507static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
508static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
509static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
510static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
511static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
512static NODE *evstr2dstr(struct parser_params*,NODE*);
513static NODE *splat_array(NODE*);
514static void mark_lvar_used(struct parser_params *p, NODE *rhs);
515
516static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
517static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
518static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
519static 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);
520static 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;}
521
522static bool args_info_empty_p(struct rb_args_info *args);
523static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
524static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
525static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
526static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
527static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
528static NODE *new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc);
529static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
530static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
531
532static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
533static NODE *args_with_numbered(struct parser_params*,NODE*,int);
534
535static VALUE negate_lit(struct parser_params*, VALUE);
536static NODE *ret_args(struct parser_params*,NODE*);
537static NODE *arg_blk_pass(NODE*,NODE*);
538static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
539static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
540
541static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
542static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
543
544static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
545static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
546
547static void rb_backref_error(struct parser_params*,NODE*);
548static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
549
550static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
551static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
552static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
553static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
554static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
555
556static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
557
558static NODE *opt_arg_append(NODE*, NODE*);
559static NODE *kwd_append(NODE*, NODE*);
560
561static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
562static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
563
564static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
565
566static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
567
568#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
569
570static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
571
572static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
573
574static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
575
576static rb_ast_id_table_t *local_tbl(struct parser_params*);
577
578static VALUE reg_compile(struct parser_params*, VALUE, int);
579static void reg_fragment_setenc(struct parser_params*, VALUE, int);
580static int reg_fragment_check(struct parser_params*, VALUE, int);
581static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
582
583static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
584static NODE *heredoc_dedent(struct parser_params*,NODE*);
585
586static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
587
588#define get_id(id) (id)
589#define get_value(val) (val)
590#define get_num(num) (num)
591#else /* RIPPER */
592#define NODE_RIPPER NODE_CDECL
593#define NEW_RIPPER(a,b,c,loc) (VALUE)NEW_CDECL(a,b,c,loc)
594
595static inline int ripper_is_node_yylval(VALUE n);
596
597static inline VALUE
598ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
599{
600 if (ripper_is_node_yylval(c)) c = RNODE(c)->nd_cval;
601 add_mark_object(p, b);
602 add_mark_object(p, c);
603 return NEW_RIPPER(a, b, c, &NULL_LOC);
604}
605
606static inline int
607ripper_is_node_yylval(VALUE n)
608{
609 return RB_TYPE_P(n, T_NODE) && nd_type_p(RNODE(n), NODE_RIPPER);
610}
611
612#define value_expr(node) ((void)(node))
613#define remove_begin(node) (node)
614#define void_stmts(p,x) (x)
615#define rb_dvar_defined(id, base) 0
616#define rb_local_defined(id, base) 0
617static ID ripper_get_id(VALUE);
618#define get_id(id) ripper_get_id(id)
619static VALUE ripper_get_value(VALUE);
620#define get_value(val) ripper_get_value(val)
621#define get_num(num) (int)get_id(num)
622static VALUE assignable(struct parser_params*,VALUE);
623static int id_is_var(struct parser_params *p, ID id);
624
625#define method_cond(p,node,loc) (node)
626#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
627#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
628#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
629#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
630
631#define new_nil(loc) Qnil
632
633static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
634
635static VALUE const_decl(struct parser_params *p, VALUE path);
636
637static VALUE var_field(struct parser_params *p, VALUE a);
638static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
639
640static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
641
642static VALUE backref_error(struct parser_params*, NODE *, VALUE);
643#endif /* !RIPPER */
644
645/* forward declaration */
646typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
647
648RUBY_SYMBOL_EXPORT_BEGIN
649VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
650int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
651enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
652VALUE rb_parser_lex_state_name(enum lex_state_e state);
653void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
654PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
655YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
656YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
657YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
658RUBY_SYMBOL_EXPORT_END
659
660static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
661static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
662#ifndef RIPPER
663static ID formal_argument(struct parser_params*, ID);
664#else
665static ID formal_argument(struct parser_params*, VALUE);
666#endif
667static ID shadowing_lvar(struct parser_params*,ID);
668static void new_bv(struct parser_params*,ID);
669
670static void local_push(struct parser_params*,int);
671static void local_pop(struct parser_params*);
672static void local_var(struct parser_params*, ID);
673static void arg_var(struct parser_params*, ID);
674static int local_id(struct parser_params *p, ID id);
675static int local_id_ref(struct parser_params*, ID, ID **);
676#ifndef RIPPER
677static ID internal_id(struct parser_params*);
678static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
679#endif
680static int check_forwarding_args(struct parser_params*);
681static void add_forwarding_args(struct parser_params *p);
682
683static const struct vtable *dyna_push(struct parser_params *);
684static void dyna_pop(struct parser_params*, const struct vtable *);
685static int dyna_in_block(struct parser_params*);
686#define dyna_var(p, id) local_var(p, id)
687static int dvar_defined(struct parser_params*, ID);
688static int dvar_defined_ref(struct parser_params*, ID, ID**);
689static int dvar_curr(struct parser_params*,ID);
690
691static int lvar_defined(struct parser_params*, ID);
692
693static NODE *numparam_push(struct parser_params *p);
694static void numparam_pop(struct parser_params *p, NODE *prev_inner);
695
696#ifdef RIPPER
697# define METHOD_NOT idNOT
698#else
699# define METHOD_NOT '!'
700#endif
701
702#define idFWD_REST '*'
703#ifdef RUBY3_KEYWORDS
704#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
705#else
706#define idFWD_KWREST 0
707#endif
708#define idFWD_BLOCK '&'
709
710#define RE_OPTION_ONCE (1<<16)
711#define RE_OPTION_ENCODING_SHIFT 8
712#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
713#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
714#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
715#define RE_OPTION_MASK 0xff
716#define RE_OPTION_ARG_ENCODING_NONE 32
717
718/* structs for managing terminator of string literal and heredocment */
719typedef struct rb_strterm_literal_struct {
720 union {
721 VALUE dummy;
722 long nest;
723 } u0;
724 union {
725 VALUE dummy;
726 long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
727 } u1;
728 union {
729 VALUE dummy;
730 long paren; /* '(' of `%q(...)` */
731 } u2;
732 union {
733 VALUE dummy;
734 long term; /* ')' of `%q(...)` */
735 } u3;
736} rb_strterm_literal_t;
737
738#define HERETERM_LENGTH_BITS ((SIZEOF_VALUE - 1) * CHAR_BIT - 1)
739
740struct rb_strterm_heredoc_struct {
741 VALUE lastline; /* the string of line that contains `<<"END"` */
742 long offset; /* the column of END in `<<"END"` */
743 int sourceline; /* lineno of the line that contains `<<"END"` */
744 unsigned length /* the length of END in `<<"END"` */
745#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
746 : HERETERM_LENGTH_BITS
747# define HERETERM_LENGTH_MAX ((1U << HERETERM_LENGTH_BITS) - 1)
748#else
749# define HERETERM_LENGTH_MAX UINT_MAX
750#endif
751 ;
752#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
753 unsigned quote: 1;
754 unsigned func: 8;
755#else
756 uint8_t quote;
757 uint8_t func;
758#endif
759};
760STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
761
762#define STRTERM_HEREDOC IMEMO_FL_USER0
763
764struct rb_strterm_struct {
765 VALUE flags;
766 union {
767 rb_strterm_literal_t literal;
768 rb_strterm_heredoc_t heredoc;
769 } u;
770};
771
772#ifndef RIPPER
773void
774rb_strterm_mark(VALUE obj)
775{
776 rb_strterm_t *strterm = (rb_strterm_t*)obj;
777 if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
778 rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
779 rb_gc_mark(heredoc->lastline);
780 }
781}
782#endif
783
784#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
785size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
786
787#define TOKEN2ID(tok) ( \
788 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
789 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
790 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
791 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
792 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
793 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
794 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
795
796/****** Ripper *******/
797
798#ifdef RIPPER
799#define RIPPER_VERSION "0.1.0"
800
801static inline VALUE intern_sym(const char *name);
802
803#include "eventids1.c"
804#include "eventids2.c"
805
806static VALUE ripper_dispatch0(struct parser_params*,ID);
807static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
808static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
809static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
810static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
811static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
812static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
813static void ripper_error(struct parser_params *p);
814
815#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
816#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
817#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
818#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
819#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
820#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
821#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))
822
823#define yyparse ripper_yyparse
824
825#define ID2VAL(id) STATIC_ID2SYM(id)
826#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
827#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
828
829#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
830 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
831
832#define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
833
834static inline VALUE
835new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
836{
837 NODE *t = (NODE *)tail;
838 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value, block = t->u3.value;
839 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, escape_Qundef(block));
840}
841
842static inline VALUE
843new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
844{
845 NODE *t = rb_node_newnode(NODE_ARGS_AUX, kw_args, kw_rest_arg, block, &NULL_LOC);
846 add_mark_object(p, kw_args);
847 add_mark_object(p, kw_rest_arg);
848 add_mark_object(p, block);
849 return (VALUE)t;
850}
851
852static inline VALUE
853args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
854{
855 return args;
856}
857
858static VALUE
859new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
860{
861 NODE *t = (NODE *)aryptn;
862 VALUE pre_args = t->u1.value, rest_arg = t->u2.value, post_args = t->u3.value;
863
864 if (!NIL_P(pre_arg)) {
865 if (!NIL_P(pre_args)) {
866 rb_ary_unshift(pre_args, pre_arg);
867 }
868 else {
869 pre_args = rb_ary_new_from_args(1, pre_arg);
870 }
871 }
872 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
873}
874
875static VALUE
876new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
877{
878 NODE *t;
879
880 if (has_rest) {
881 rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
882 }
883 else {
884 rest_arg = Qnil;
885 }
886
887 t = rb_node_newnode(NODE_ARYPTN, pre_args, rest_arg, post_args, &NULL_LOC);
888 add_mark_object(p, pre_args);
889 add_mark_object(p, rest_arg);
890 add_mark_object(p, post_args);
891 return (VALUE)t;
892}
893
894static VALUE
895new_find_pattern(struct parser_params *p, VALUE constant, VALUE fndptn, const YYLTYPE *loc)
896{
897 NODE *t = (NODE *)fndptn;
898 VALUE pre_rest_arg = t->u1.value, args = t->u2.value, post_rest_arg = t->u3.value;
899
900 return dispatch4(fndptn, constant, pre_rest_arg, args, post_rest_arg);
901}
902
903static VALUE
904new_find_pattern_tail(struct parser_params *p, VALUE pre_rest_arg, VALUE args, VALUE post_rest_arg, const YYLTYPE *loc)
905{
906 NODE *t;
907
908 pre_rest_arg = dispatch1(var_field, pre_rest_arg ? pre_rest_arg : Qnil);
909 post_rest_arg = dispatch1(var_field, post_rest_arg ? post_rest_arg : Qnil);
910
911 t = rb_node_newnode(NODE_FNDPTN, pre_rest_arg, args, post_rest_arg, &NULL_LOC);
912 add_mark_object(p, pre_rest_arg);
913 add_mark_object(p, args);
914 add_mark_object(p, post_rest_arg);
915 return (VALUE)t;
916}
917
918#define new_hash(p,h,l) rb_ary_new_from_args(0)
919
920static VALUE
921new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
922{
923 return ary;
924}
925
926static VALUE
927new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
928{
929 NODE *t = (NODE *)hshptn;
930 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value;
931 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
932}
933
934static VALUE
935new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
936{
937 NODE *t;
938 if (kw_rest_arg) {
939 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
940 }
941 else {
942 kw_rest_arg = Qnil;
943 }
944 t = rb_node_newnode(NODE_HSHPTN, kw_args, kw_rest_arg, 0, &NULL_LOC);
945
946 add_mark_object(p, kw_args);
947 add_mark_object(p, kw_rest_arg);
948 return (VALUE)t;
949}
950
951#define new_defined(p,expr,loc) dispatch1(defined, (expr))
952
953static VALUE heredoc_dedent(struct parser_params*,VALUE);
954
955#else
956#define ID2VAL(id) (id)
957#define TOKEN2VAL(t) ID2VAL(t)
958#define KWD2EID(t, v) keyword_##t
959
960static NODE *
961set_defun_body(struct parser_params *p, NODE *n, NODE *args, NODE *body, const YYLTYPE *loc)
962{
963 body = remove_begin(body);
964 reduce_nodes(p, &body);
965 n->nd_defn = NEW_SCOPE(args, body, loc);
966 n->nd_loc = *loc;
967 nd_set_line(n->nd_defn, loc->end_pos.lineno);
968 set_line_body(body, loc->beg_pos.lineno);
969 return n;
970}
971
972static NODE *
973rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
974 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
975{
976 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
977 rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc);
978 loc.beg_pos = arg_loc->beg_pos;
979 return NEW_RESCUE(arg, rescue, 0, &loc);
980}
981
982#endif /* RIPPER */
983
984static void
985restore_defun(struct parser_params *p, NODE *name)
986{
987 YYSTYPE c = {.val = name->nd_cval};
988 p->cur_arg = name->nd_vid;
989 p->ctxt.in_def = c.ctxt.in_def;
990 p->ctxt.shareable_constant_value = c.ctxt.shareable_constant_value;
991}
992
993static void
994endless_method_name(struct parser_params *p, NODE *defn, const YYLTYPE *loc)
995{
996#ifdef RIPPER
997 defn = defn->nd_defn;
998#endif
999 ID mid = defn->nd_mid;
1000 if (is_attrset_id(mid)) {
1001 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1002 }
1003 token_info_drop(p, "def", loc->beg_pos);
1004}
1005
1006#ifndef RIPPER
1007# define Qnone 0
1008# define Qnull 0
1009# define ifndef_ripper(x) (x)
1010#else
1011# define Qnone Qnil
1012# define Qnull Qundef
1013# define ifndef_ripper(x)
1014#endif
1015
1016# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1017# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1018# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1019# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1020# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1021# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1022# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1023# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1024# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1025# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1026# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1027# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1028# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1029# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1030# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1031# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1032# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1033# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1034# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1035# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1036#ifdef RIPPER
1037static ID id_warn, id_warning, id_gets, id_assoc;
1038# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1039# define WARN_S_L(s,l) STR_NEW(s,l)
1040# define WARN_S(s) STR_NEW2(s)
1041# define WARN_I(i) INT2NUM(i)
1042# define WARN_ID(i) rb_id2str(i)
1043# define WARN_IVAL(i) i
1044# define PRIsWARN "s"
1045# define rb_warn0L_experimental(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1046# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1047# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1048# ifdef HAVE_VA_ARGS_MACRO
1049# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1050# else
1051# define WARN_CALL rb_funcall
1052# endif
1053# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1054# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1055# ifdef HAVE_VA_ARGS_MACRO
1056# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1057# else
1058# define WARNING_CALL rb_funcall
1059# endif
1060PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1061# define compile_error ripper_compile_error
1062#else
1063# define WARN_S_L(s,l) s
1064# define WARN_S(s) s
1065# define WARN_I(i) i
1066# define WARN_ID(i) rb_id2name(i)
1067# define WARN_IVAL(i) NUM2INT(i)
1068# define PRIsWARN PRIsVALUE
1069# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1070# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1071# define WARN_CALL rb_compile_warn
1072# define rb_warn0L_experimental(l,fmt) rb_category_compile_warn(RB_WARN_CATEGORY_EXPERIMENTAL, WARN_ARGS_L(l, fmt, 1))
1073# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1074# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1075# define WARNING_CALL rb_compile_warning
1076PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1077# define compile_error parser_compile_error
1078#endif
1079
1080#define WARN_EOL(tok) \
1081 (looking_at_eol_p(p) ? \
1082 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
1083 (void)0)
1084static int looking_at_eol_p(struct parser_params *p);
1085%}
1086
1087%expect 0
1088%define api.pure
1089%define parse.error verbose
1090%printer {
1091#ifndef RIPPER
1092 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
1093#else
1094 rb_parser_printf(p, "%"PRIsVALUE, RNODE($$)->nd_rval);
1095#endif
1096} tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN
1097%printer {
1098#ifndef RIPPER
1099 rb_parser_printf(p, "%+"PRIsVALUE, $$->nd_lit);
1100#else
1101 rb_parser_printf(p, "%+"PRIsVALUE, get_value($$));
1102#endif
1103} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
1104%printer {
1105#ifndef RIPPER
1106 rb_parser_printf(p, "$%ld", $$->nd_nth);
1107#else
1108 rb_parser_printf(p, "%"PRIsVALUE, $$);
1109#endif
1110} tNTH_REF
1111%printer {
1112#ifndef RIPPER
1113 rb_parser_printf(p, "$%c", (int)$$->nd_nth);
1114#else
1115 rb_parser_printf(p, "%"PRIsVALUE, $$);
1116#endif
1117} tBACK_REF
1118
1119%lex-param {struct parser_params *p}
1120%parse-param {struct parser_params *p}
1121%initial-action
1122{
1123 RUBY_SET_YYLLOC_OF_NONE(@$);
1124};
1125
1126%union {
1127 VALUE val;
1128 NODE *node;
1129 ID id;
1130 int num;
1131 st_table *tbl;
1132 const struct vtable *vars;
1133 struct rb_strterm_struct *strterm;
1134 struct lex_context ctxt;
1135}
1136
1137%token <id>
1138 keyword_class "`class'"
1139 keyword_module "`module'"
1140 keyword_def "`def'"
1141 keyword_undef "`undef'"
1142 keyword_begin "`begin'"
1143 keyword_rescue "`rescue'"
1144 keyword_ensure "`ensure'"
1145 keyword_end "`end'"
1146 keyword_if "`if'"
1147 keyword_unless "`unless'"
1148 keyword_then "`then'"
1149 keyword_elsif "`elsif'"
1150 keyword_else "`else'"
1151 keyword_case "`case'"
1152 keyword_when "`when'"
1153 keyword_while "`while'"
1154 keyword_until "`until'"
1155 keyword_for "`for'"
1156 keyword_break "`break'"
1157 keyword_next "`next'"
1158 keyword_redo "`redo'"
1159 keyword_retry "`retry'"
1160 keyword_in "`in'"
1161 keyword_do "`do'"
1162 keyword_do_cond "`do' for condition"
1163 keyword_do_block "`do' for block"
1164 keyword_do_LAMBDA "`do' for lambda"
1165 keyword_return "`return'"
1166 keyword_yield "`yield'"
1167 keyword_super "`super'"
1168 keyword_self "`self'"
1169 keyword_nil "`nil'"
1170 keyword_true "`true'"
1171 keyword_false "`false'"
1172 keyword_and "`and'"
1173 keyword_or "`or'"
1174 keyword_not "`not'"
1175 modifier_if "`if' modifier"
1176 modifier_unless "`unless' modifier"
1177 modifier_while "`while' modifier"
1178 modifier_until "`until' modifier"
1179 modifier_rescue "`rescue' modifier"
1180 keyword_alias "`alias'"
1181 keyword_defined "`defined?'"
1182 keyword_BEGIN "`BEGIN'"
1183 keyword_END "`END'"
1184 keyword__LINE__ "`__LINE__'"
1185 keyword__FILE__ "`__FILE__'"
1186 keyword__ENCODING__ "`__ENCODING__'"
1187
1188%token <id> tIDENTIFIER "local variable or method"
1189%token <id> tFID "method"
1190%token <id> tGVAR "global variable"
1191%token <id> tIVAR "instance variable"
1192%token <id> tCONSTANT "constant"
1193%token <id> tCVAR "class variable"
1194%token <id> tLABEL "label"
1195%token <node> tINTEGER "integer literal"
1196%token <node> tFLOAT "float literal"
1197%token <node> tRATIONAL "rational literal"
1198%token <node> tIMAGINARY "imaginary literal"
1199%token <node> tCHAR "char literal"
1200%token <node> tNTH_REF "numbered reference"
1201%token <node> tBACK_REF "back reference"
1202%token <node> tSTRING_CONTENT "literal content"
1203%token <num> tREGEXP_END
1204
1205%type <node> singleton strings string string1 xstring regexp
1206%type <node> string_contents xstring_contents regexp_contents string_content
1207%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1208%type <node> literal numeric simple_numeric ssym dsym symbol cpath def_name defn_head defs_head
1209%type <node> top_compstmt top_stmts top_stmt begin_block
1210%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1211%type <node> expr_value expr_value_do arg_value primary_value fcall rel_expr
1212%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1213%type <node> args call_args opt_call_args
1214%type <node> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1215%type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1216%type <node> command_rhs arg_rhs
1217%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
1218%type <node> f_block_optarg f_block_opt
1219%type <node> f_arglist f_opt_paren_args f_paren_args f_args f_arg f_arg_item
1220%type <node> f_optarg f_marg f_marg_list f_margs f_rest_marg
1221%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
1222%type <node> block_param opt_block_param block_param_def f_opt
1223%type <node> f_kwarg f_kw f_block_kwarg f_block_kw
1224%type <node> bv_decls opt_bv_decl bvar
1225%type <node> lambda f_larglist lambda_body brace_body do_body
1226%type <node> brace_block cmd_brace_block do_block lhs none fitem
1227%type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1228%type <node> p_case_body p_cases p_top_expr p_top_expr_body
1229%type <node> p_expr p_as p_alt p_expr_basic p_find
1230%type <node> p_args p_args_head p_args_tail p_args_post p_arg
1231%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
1232%type <node> p_kwargs p_kwarg p_kw
1233%type <id> keyword_variable user_variable sym operation operation2 operation3
1234%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1235%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1236%type <id> p_rest p_kwrest p_kwnorest p_any_kwrest p_kw_label
1237%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var
1238 %type <ctxt> lex_ctxt /* keep <ctxt> in ripper */
1239%token END_OF_INPUT 0 "end-of-input"
1240%token <id> '.'
1241/* escaped chars, should be ignored otherwise */
1242%token <id> '\\' "backslash"
1243%token tSP "escaped space"
1244%token <id> '\t' "escaped horizontal tab"
1245%token <id> '\f' "escaped form feed"
1246%token <id> '\r' "escaped carriage return"
1247%token <id> '\13' "escaped vertical tab"
1248%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
1249%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
1250%token tPOW RUBY_TOKEN(POW) "**"
1251%token tCMP RUBY_TOKEN(CMP) "<=>"
1252%token tEQ RUBY_TOKEN(EQ) "=="
1253%token tEQQ RUBY_TOKEN(EQQ) "==="
1254%token tNEQ RUBY_TOKEN(NEQ) "!="
1255%token tGEQ RUBY_TOKEN(GEQ) ">="
1256%token tLEQ RUBY_TOKEN(LEQ) "<="
1257%token tANDOP RUBY_TOKEN(ANDOP) "&&"
1258%token tOROP RUBY_TOKEN(OROP) "||"
1259%token tMATCH RUBY_TOKEN(MATCH) "=~"
1260%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
1261%token tDOT2 RUBY_TOKEN(DOT2) ".."
1262%token tDOT3 RUBY_TOKEN(DOT3) "..."
1263%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
1264%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
1265%token tAREF RUBY_TOKEN(AREF) "[]"
1266%token tASET RUBY_TOKEN(ASET) "[]="
1267%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
1268%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
1269%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
1270%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
1271%token tCOLON3 ":: at EXPR_BEG"
1272%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1273%token tASSOC "=>"
1274%token tLPAREN "("
1275%token tLPAREN_ARG "( arg"
1276%token tRPAREN ")"
1277%token tLBRACK "["
1278%token tLBRACE "{"
1279%token tLBRACE_ARG "{ arg"
1280%token tSTAR "*"
1281%token tDSTAR "**arg"
1282%token tAMPER "&"
1283%token tLAMBDA "->"
1284%token tSYMBEG "symbol literal"
1285%token tSTRING_BEG "string literal"
1286%token tXSTRING_BEG "backtick literal"
1287%token tREGEXP_BEG "regexp literal"
1288%token tWORDS_BEG "word list"
1289%token tQWORDS_BEG "verbatim word list"
1290%token tSYMBOLS_BEG "symbol list"
1291%token tQSYMBOLS_BEG "verbatim symbol list"
1292%token tSTRING_END "terminator"
1293%token tSTRING_DEND "'}'"
1294%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1295
1296/*
1297 * precedence table
1298 */
1299
1300%nonassoc tLOWEST
1301%nonassoc tLBRACE_ARG
1302
1303%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1304%left keyword_or keyword_and
1305%right keyword_not
1306%nonassoc keyword_defined
1307%right '=' tOP_ASGN
1308%left modifier_rescue
1309%right '?' ':'
1310%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1311%left tOROP
1312%left tANDOP
1313%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1314%left '>' tGEQ '<' tLEQ
1315%left '|' '^'
1316%left '&'
1317%left tLSHFT tRSHFT
1318%left '+' '-'
1319%left '*' '/' '%'
1320%right tUMINUS_NUM tUMINUS
1321%right tPOW
1322%right '!' '~' tUPLUS
1323
1324%token tLAST_TOKEN
1325
1326%%
1327program : {
1328 SET_LEX_STATE(EXPR_BEG);
1329 local_push(p, ifndef_ripper(1)+0);
1330 }
1331 top_compstmt
1332 {
1333 /*%%%*/
1334 if ($2 && !compile_for_eval) {
1335 NODE *node = $2;
1336 /* last expression should not be void */
1337 if (nd_type_p(node, NODE_BLOCK)) {
1338 while (node->nd_next) {
1339 node = node->nd_next;
1340 }
1341 node = node->nd_head;
1342 }
1343 node = remove_begin(node);
1344 void_expr(p, node);
1345 }
1346 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1347 /*% %*/
1348 /*% ripper[final]: program!($2) %*/
1349 local_pop(p);
1350 }
1351 ;
1352
1353top_compstmt : top_stmts opt_terms
1354 {
1355 $$ = void_stmts(p, $1);
1356 }
1357 ;
1358
1359top_stmts : none
1360 {
1361 /*%%%*/
1362 $$ = NEW_BEGIN(0, &@$);
1363 /*% %*/
1364 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1365 }
1366 | top_stmt
1367 {
1368 /*%%%*/
1369 $$ = newline_node($1);
1370 /*% %*/
1371 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1372 }
1373 | top_stmts terms top_stmt
1374 {
1375 /*%%%*/
1376 $$ = block_append(p, $1, newline_node($3));
1377 /*% %*/
1378 /*% ripper: stmts_add!($1, $3) %*/
1379 }
1380 | error top_stmt
1381 {
1382 $$ = remove_begin($2);
1383 }
1384 ;
1385
1386top_stmt : stmt
1387 | keyword_BEGIN begin_block
1388 {
1389 $$ = $2;
1390 }
1391 ;
1392
1393begin_block : '{' top_compstmt '}'
1394 {
1395 /*%%%*/
1396 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1397 NEW_BEGIN($2, &@$));
1398 $$ = NEW_BEGIN(0, &@$);
1399 /*% %*/
1400 /*% ripper: BEGIN!($2) %*/
1401 }
1402 ;
1403
1404bodystmt : compstmt
1405 opt_rescue
1406 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1407 compstmt
1408 opt_ensure
1409 {
1410 /*%%%*/
1411 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1412 /*% %*/
1413 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), escape_Qundef($5), escape_Qundef($6)) %*/
1414 }
1415 | compstmt
1416 opt_rescue
1417 opt_ensure
1418 {
1419 /*%%%*/
1420 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1421 /*% %*/
1422 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), Qnil, escape_Qundef($3)) %*/
1423 }
1424 ;
1425
1426compstmt : stmts opt_terms
1427 {
1428 $$ = void_stmts(p, $1);
1429 }
1430 ;
1431
1432stmts : none
1433 {
1434 /*%%%*/
1435 $$ = NEW_BEGIN(0, &@$);
1436 /*% %*/
1437 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1438 }
1439 | stmt_or_begin
1440 {
1441 /*%%%*/
1442 $$ = newline_node($1);
1443 /*% %*/
1444 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1445 }
1446 | stmts terms stmt_or_begin
1447 {
1448 /*%%%*/
1449 $$ = block_append(p, $1, newline_node($3));
1450 /*% %*/
1451 /*% ripper: stmts_add!($1, $3) %*/
1452 }
1453 | error stmt
1454 {
1455 $$ = remove_begin($2);
1456 }
1457 ;
1458
1459stmt_or_begin : stmt
1460 {
1461 $$ = $1;
1462 }
1463 | keyword_BEGIN
1464 {
1465 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1466 }
1467 begin_block
1468 {
1469 $$ = $3;
1470 }
1471 ;
1472
1473stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1474 {
1475 /*%%%*/
1476 $$ = NEW_ALIAS($2, $4, &@$);
1477 /*% %*/
1478 /*% ripper: alias!($2, $4) %*/
1479 }
1480 | keyword_alias tGVAR tGVAR
1481 {
1482 /*%%%*/
1483 $$ = NEW_VALIAS($2, $3, &@$);
1484 /*% %*/
1485 /*% ripper: var_alias!($2, $3) %*/
1486 }
1487 | keyword_alias tGVAR tBACK_REF
1488 {
1489 /*%%%*/
1490 char buf[2];
1491 buf[0] = '$';
1492 buf[1] = (char)$3->nd_nth;
1493 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1494 /*% %*/
1495 /*% ripper: var_alias!($2, $3) %*/
1496 }
1497 | keyword_alias tGVAR tNTH_REF
1498 {
1499 static const char mesg[] = "can't make alias for the number variables";
1500 /*%%%*/
1501 yyerror1(&@3, mesg);
1502 $$ = NEW_BEGIN(0, &@$);
1503 /*% %*/
1504 /*% ripper[error]: alias_error!(ERR_MESG(), $3) %*/
1505 }
1506 | keyword_undef undef_list
1507 {
1508 /*%%%*/
1509 $$ = $2;
1510 /*% %*/
1511 /*% ripper: undef!($2) %*/
1512 }
1513 | stmt modifier_if expr_value
1514 {
1515 /*%%%*/
1516 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1517 fixpos($$, $3);
1518 /*% %*/
1519 /*% ripper: if_mod!($3, $1) %*/
1520 }
1521 | stmt modifier_unless expr_value
1522 {
1523 /*%%%*/
1524 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1525 fixpos($$, $3);
1526 /*% %*/
1527 /*% ripper: unless_mod!($3, $1) %*/
1528 }
1529 | stmt modifier_while expr_value
1530 {
1531 /*%%%*/
1532 if ($1 && nd_type_p($1, NODE_BEGIN)) {
1533 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1534 }
1535 else {
1536 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1537 }
1538 /*% %*/
1539 /*% ripper: while_mod!($3, $1) %*/
1540 }
1541 | stmt modifier_until expr_value
1542 {
1543 /*%%%*/
1544 if ($1 && nd_type_p($1, NODE_BEGIN)) {
1545 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1546 }
1547 else {
1548 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1549 }
1550 /*% %*/
1551 /*% ripper: until_mod!($3, $1) %*/
1552 }
1553 | stmt modifier_rescue stmt
1554 {
1555 /*%%%*/
1556 NODE *resq;
1557 YYLTYPE loc = code_loc_gen(&@2, &@3);
1558 resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1559 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1560 /*% %*/
1561 /*% ripper: rescue_mod!($1, $3) %*/
1562 }
1563 | keyword_END '{' compstmt '}'
1564 {
1565 if (p->ctxt.in_def) {
1566 rb_warn0("END in method; use at_exit");
1567 }
1568 /*%%%*/
1569 {
1570 NODE *scope = NEW_NODE(
1571 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1572 $$ = NEW_POSTEXE(scope, &@$);
1573 }
1574 /*% %*/
1575 /*% ripper: END!($3) %*/
1576 }
1577 | command_asgn
1578 | mlhs '=' lex_ctxt command_call
1579 {
1580 /*%%%*/
1581 value_expr($4);
1582 $$ = node_assign(p, $1, $4, $3, &@$);
1583 /*% %*/
1584 /*% ripper: massign!($1, $4) %*/
1585 }
1586 | lhs '=' lex_ctxt mrhs
1587 {
1588 /*%%%*/
1589 $$ = node_assign(p, $1, $4, $3, &@$);
1590 /*% %*/
1591 /*% ripper: assign!($1, $4) %*/
1592 }
1593 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue stmt
1594 {
1595 /*%%%*/
1596 YYLTYPE loc = code_loc_gen(&@5, &@6);
1597 $$ = node_assign(p, $1, NEW_RESCUE($4, NEW_RESBODY(0, remove_begin($6), 0, &loc), 0, &@$), $3, &@$);
1598 /*% %*/
1599 /*% ripper: massign!($1, rescue_mod!($4, $6)) %*/
1600 }
1601 | mlhs '=' lex_ctxt mrhs_arg
1602 {
1603 /*%%%*/
1604 $$ = node_assign(p, $1, $4, $3, &@$);
1605 /*% %*/
1606 /*% ripper: massign!($1, $4) %*/
1607 }
1608 | expr
1609 ;
1610
1611command_asgn : lhs '=' lex_ctxt command_rhs
1612 {
1613 /*%%%*/
1614 $$ = node_assign(p, $1, $4, $3, &@$);
1615 /*% %*/
1616 /*% ripper: assign!($1, $4) %*/
1617 }
1618 | var_lhs tOP_ASGN lex_ctxt command_rhs
1619 {
1620 /*%%%*/
1621 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
1622 /*% %*/
1623 /*% ripper: opassign!($1, $2, $4) %*/
1624 }
1625 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
1626 {
1627 /*%%%*/
1628 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
1629 /*% %*/
1630 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/
1631
1632 }
1633 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1634 {
1635 /*%%%*/
1636 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1637 /*% %*/
1638 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1639 }
1640 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt command_rhs
1641 {
1642 /*%%%*/
1643 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1644 /*% %*/
1645 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1646 }
1647 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
1648 {
1649 /*%%%*/
1650 YYLTYPE loc = code_loc_gen(&@1, &@3);
1651 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
1652 /*% %*/
1653 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
1654 }
1655 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1656 {
1657 /*%%%*/
1658 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
1659 /*% %*/
1660 /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $6) %*/
1661 }
1662 | defn_head f_opt_paren_args '=' command
1663 {
1664 endless_method_name(p, $<node>1, &@1);
1665 restore_defun(p, $<node>1->nd_defn);
1666 /*%%%*/
1667 $$ = set_defun_body(p, $1, $2, $4, &@$);
1668 /*% %*/
1669 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
1670 /*% ripper: def!(get_value($1), $2, $4) %*/
1671 local_pop(p);
1672 }
1673 | defn_head f_opt_paren_args '=' command modifier_rescue arg
1674 {
1675 endless_method_name(p, $<node>1, &@1);
1676 restore_defun(p, $<node>1->nd_defn);
1677 /*%%%*/
1678 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
1679 $$ = set_defun_body(p, $1, $2, $4, &@$);
1680 /*% %*/
1681 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
1682 /*% ripper: def!(get_value($1), $2, $4) %*/
1683 local_pop(p);
1684 }
1685 | defs_head f_opt_paren_args '=' command
1686 {
1687 endless_method_name(p, $<node>1, &@1);
1688 restore_defun(p, $<node>1->nd_defn);
1689 /*%%%*/
1690 $$ = set_defun_body(p, $1, $2, $4, &@$);
1691 /*%
1692 $1 = get_value($1);
1693 %*/
1694 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
1695 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
1696 local_pop(p);
1697 }
1698 | defs_head f_opt_paren_args '=' command modifier_rescue arg
1699 {
1700 endless_method_name(p, $<node>1, &@1);
1701 restore_defun(p, $<node>1->nd_defn);
1702 /*%%%*/
1703 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
1704 $$ = set_defun_body(p, $1, $2, $4, &@$);
1705 /*%
1706 $1 = get_value($1);
1707 %*/
1708 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
1709 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
1710 local_pop(p);
1711 }
1712 | backref tOP_ASGN lex_ctxt command_rhs
1713 {
1714 /*%%%*/
1715 rb_backref_error(p, $1);
1716 $$ = NEW_BEGIN(0, &@$);
1717 /*% %*/
1718 /*% ripper[error]: backref_error(p, RNODE($1), assign!(var_field(p, $1), $4)) %*/
1719 }
1720 ;
1721
1722command_rhs : command_call %prec tOP_ASGN
1723 {
1724 value_expr($1);
1725 $$ = $1;
1726 }
1727 | command_call modifier_rescue stmt
1728 {
1729 /*%%%*/
1730 YYLTYPE loc = code_loc_gen(&@2, &@3);
1731 value_expr($1);
1732 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1733 /*% %*/
1734 /*% ripper: rescue_mod!($1, $3) %*/
1735 }
1736 | command_asgn
1737 ;
1738
1739expr : command_call
1740 | expr keyword_and expr
1741 {
1742 $$ = logop(p, idAND, $1, $3, &@2, &@$);
1743 }
1744 | expr keyword_or expr
1745 {
1746 $$ = logop(p, idOR, $1, $3, &@2, &@$);
1747 }
1748 | keyword_not opt_nl expr
1749 {
1750 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
1751 }
1752 | '!' command_call
1753 {
1754 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
1755 }
1756 | arg tASSOC
1757 {
1758 value_expr($1);
1759 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1760 p->command_start = FALSE;
1761 $<ctxt>2 = p->ctxt;
1762 p->ctxt.in_kwarg = 1;
1763 $<tbl>$ = push_pvtbl(p);
1764 }
1765 {
1766 $<tbl>$ = push_pktbl(p);
1767 }
1768 p_top_expr_body
1769 {
1770 pop_pktbl(p, $<tbl>4);
1771 pop_pvtbl(p, $<tbl>3);
1772 p->ctxt.in_kwarg = $<ctxt>2.in_kwarg;
1773 /*%%%*/
1774 $$ = NEW_CASE3($1, NEW_IN($5, 0, 0, &@5), &@$);
1775 /*% %*/
1776 /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
1777 }
1778 | arg keyword_in
1779 {
1780 value_expr($1);
1781 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1782 p->command_start = FALSE;
1783 $<ctxt>2 = p->ctxt;
1784 p->ctxt.in_kwarg = 1;
1785 $<tbl>$ = push_pvtbl(p);
1786 }
1787 {
1788 $<tbl>$ = push_pktbl(p);
1789 }
1790 p_top_expr_body
1791 {
1792 pop_pktbl(p, $<tbl>4);
1793 pop_pvtbl(p, $<tbl>3);
1794 p->ctxt.in_kwarg = $<ctxt>2.in_kwarg;
1795 /*%%%*/
1796 $$ = NEW_CASE3($1, NEW_IN($5, NEW_TRUE(&@5), NEW_FALSE(&@5), &@5), &@$);
1797 /*% %*/
1798 /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
1799 }
1800 | arg %prec tLBRACE_ARG
1801 ;
1802
1803def_name : fname
1804 {
1805 ID fname = get_id($1);
1806 ID cur_arg = p->cur_arg;
1807 YYSTYPE c = {.ctxt = p->ctxt};
1808 numparam_name(p, fname);
1809 local_push(p, 0);
1810 p->cur_arg = 0;
1811 p->ctxt.in_def = 1;
1812 $<node>$ = NEW_NODE(NODE_SELF, /*vid*/cur_arg, /*mid*/fname, /*cval*/c.val, &@$);
1813 /*%%%*/
1814 /*%
1815 $$ = NEW_RIPPER(fname, get_value($1), $$, &NULL_LOC);
1816 %*/
1817 }
1818 ;
1819
1820defn_head : k_def def_name
1821 {
1822 $$ = $2;
1823 /*%%%*/
1824 $$ = NEW_NODE(NODE_DEFN, 0, $$->nd_mid, $$, &@$);
1825 /*% %*/
1826 }
1827 ;
1828
1829defs_head : k_def singleton dot_or_colon
1830 {
1831 SET_LEX_STATE(EXPR_FNAME);
1832 p->ctxt.in_argdef = 1;
1833 }
1834 def_name
1835 {
1836 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
1837 $$ = $5;
1838 /*%%%*/
1839 $$ = NEW_NODE(NODE_DEFS, $2, $$->nd_mid, $$, &@$);
1840 /*%
1841 VALUE ary = rb_ary_new_from_args(3, $2, $3, get_value($$));
1842 add_mark_object(p, ary);
1843 $<node>$->nd_rval = ary;
1844 %*/
1845 }
1846 ;
1847
1848expr_value : expr
1849 {
1850 value_expr($1);
1851 $$ = $1;
1852 }
1853 ;
1854
1855expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1856 {
1857 $$ = $2;
1858 }
1859 ;
1860
1861command_call : command
1862 | block_command
1863 ;
1864
1865block_command : block_call
1866 | block_call call_op2 operation2 command_args
1867 {
1868 /*%%%*/
1869 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
1870 /*% %*/
1871 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
1872 }
1873 ;
1874
1875cmd_brace_block : tLBRACE_ARG brace_body '}'
1876 {
1877 $$ = $2;
1878 /*%%%*/
1879 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
1880 nd_set_line($$, @1.end_pos.lineno);
1881 /*% %*/
1882 }
1883 ;
1884
1885fcall : operation
1886 {
1887 /*%%%*/
1888 $$ = NEW_FCALL($1, 0, &@$);
1889 nd_set_line($$, p->tokline);
1890 /*% %*/
1891 /*% ripper: $1 %*/
1892 }
1893 ;
1894
1895command : fcall command_args %prec tLOWEST
1896 {
1897 /*%%%*/
1898 $1->nd_args = $2;
1899 nd_set_last_loc($1, @2.end_pos);
1900 $$ = $1;
1901 /*% %*/
1902 /*% ripper: command!($1, $2) %*/
1903 }
1904 | fcall command_args cmd_brace_block
1905 {
1906 /*%%%*/
1907 block_dup_check(p, $2, $3);
1908 $1->nd_args = $2;
1909 $$ = method_add_block(p, $1, $3, &@$);
1910 fixpos($$, $1);
1911 nd_set_last_loc($1, @2.end_pos);
1912 /*% %*/
1913 /*% ripper: method_add_block!(command!($1, $2), $3) %*/
1914 }
1915 | primary_value call_op operation2 command_args %prec tLOWEST
1916 {
1917 /*%%%*/
1918 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
1919 /*% %*/
1920 /*% ripper: command_call!($1, $2, $3, $4) %*/
1921 }
1922 | primary_value call_op operation2 command_args cmd_brace_block
1923 {
1924 /*%%%*/
1925 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
1926 /*% %*/
1927 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
1928 }
1929 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1930 {
1931 /*%%%*/
1932 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
1933 /*% %*/
1934 /*% ripper: command_call!($1, ID2VAL(idCOLON2), $3, $4) %*/
1935 }
1936 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1937 {
1938 /*%%%*/
1939 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
1940 /*% %*/
1941 /*% ripper: method_add_block!(command_call!($1, ID2VAL(idCOLON2), $3, $4), $5) %*/
1942 }
1943 | keyword_super command_args
1944 {
1945 /*%%%*/
1946 $$ = NEW_SUPER($2, &@$);
1947 fixpos($$, $2);
1948 /*% %*/
1949 /*% ripper: super!($2) %*/
1950 }
1951 | keyword_yield command_args
1952 {
1953 /*%%%*/
1954 $$ = new_yield(p, $2, &@$);
1955 fixpos($$, $2);
1956 /*% %*/
1957 /*% ripper: yield!($2) %*/
1958 }
1959 | k_return call_args
1960 {
1961 /*%%%*/
1962 $$ = NEW_RETURN(ret_args(p, $2), &@$);
1963 /*% %*/
1964 /*% ripper: return!($2) %*/
1965 }
1966 | keyword_break call_args
1967 {
1968 /*%%%*/
1969 $$ = NEW_BREAK(ret_args(p, $2), &@$);
1970 /*% %*/
1971 /*% ripper: break!($2) %*/
1972 }
1973 | keyword_next call_args
1974 {
1975 /*%%%*/
1976 $$ = NEW_NEXT(ret_args(p, $2), &@$);
1977 /*% %*/
1978 /*% ripper: next!($2) %*/
1979 }
1980 ;
1981
1982mlhs : mlhs_basic
1983 | tLPAREN mlhs_inner rparen
1984 {
1985 /*%%%*/
1986 $$ = $2;
1987 /*% %*/
1988 /*% ripper: mlhs_paren!($2) %*/
1989 }
1990 ;
1991
1992mlhs_inner : mlhs_basic
1993 | tLPAREN mlhs_inner rparen
1994 {
1995 /*%%%*/
1996 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1997 /*% %*/
1998 /*% ripper: mlhs_paren!($2) %*/
1999 }
2000 ;
2001
2002mlhs_basic : mlhs_head
2003 {
2004 /*%%%*/
2005 $$ = NEW_MASGN($1, 0, &@$);
2006 /*% %*/
2007 /*% ripper: $1 %*/
2008 }
2009 | mlhs_head mlhs_item
2010 {
2011 /*%%%*/
2012 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
2013 /*% %*/
2014 /*% ripper: mlhs_add!($1, $2) %*/
2015 }
2016 | mlhs_head tSTAR mlhs_node
2017 {
2018 /*%%%*/
2019 $$ = NEW_MASGN($1, $3, &@$);
2020 /*% %*/
2021 /*% ripper: mlhs_add_star!($1, $3) %*/
2022 }
2023 | mlhs_head tSTAR mlhs_node ',' mlhs_post
2024 {
2025 /*%%%*/
2026 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
2027 /*% %*/
2028 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
2029 }
2030 | mlhs_head tSTAR
2031 {
2032 /*%%%*/
2033 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
2034 /*% %*/
2035 /*% ripper: mlhs_add_star!($1, Qnil) %*/
2036 }
2037 | mlhs_head tSTAR ',' mlhs_post
2038 {
2039 /*%%%*/
2040 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
2041 /*% %*/
2042 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, Qnil), $4) %*/
2043 }
2044 | tSTAR mlhs_node
2045 {
2046 /*%%%*/
2047 $$ = NEW_MASGN(0, $2, &@$);
2048 /*% %*/
2049 /*% ripper: mlhs_add_star!(mlhs_new!, $2) %*/
2050 }
2051 | tSTAR mlhs_node ',' mlhs_post
2052 {
2053 /*%%%*/
2054 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
2055 /*% %*/
2056 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $2), $4) %*/
2057 }
2058 | tSTAR
2059 {
2060 /*%%%*/
2061 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
2062 /*% %*/
2063 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
2064 }
2065 | tSTAR ',' mlhs_post
2066 {
2067 /*%%%*/
2068 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
2069 /*% %*/
2070 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $3) %*/
2071 }
2072 ;
2073
2074mlhs_item : mlhs_node
2075 | tLPAREN mlhs_inner rparen
2076 {
2077 /*%%%*/
2078 $$ = $2;
2079 /*% %*/
2080 /*% ripper: mlhs_paren!($2) %*/
2081 }
2082 ;
2083
2084mlhs_head : mlhs_item ','
2085 {
2086 /*%%%*/
2087 $$ = NEW_LIST($1, &@1);
2088 /*% %*/
2089 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2090 }
2091 | mlhs_head mlhs_item ','
2092 {
2093 /*%%%*/
2094 $$ = list_append(p, $1, $2);
2095 /*% %*/
2096 /*% ripper: mlhs_add!($1, $2) %*/
2097 }
2098 ;
2099
2100mlhs_post : mlhs_item
2101 {
2102 /*%%%*/
2103 $$ = NEW_LIST($1, &@$);
2104 /*% %*/
2105 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2106 }
2107 | mlhs_post ',' mlhs_item
2108 {
2109 /*%%%*/
2110 $$ = list_append(p, $1, $3);
2111 /*% %*/
2112 /*% ripper: mlhs_add!($1, $3) %*/
2113 }
2114 ;
2115
2116mlhs_node : user_variable
2117 {
2118 /*%%%*/
2119 $$ = assignable(p, $1, 0, &@$);
2120 /*% %*/
2121 /*% ripper: assignable(p, var_field(p, $1)) %*/
2122 }
2123 | keyword_variable
2124 {
2125 /*%%%*/
2126 $$ = assignable(p, $1, 0, &@$);
2127 /*% %*/
2128 /*% ripper: assignable(p, var_field(p, $1)) %*/
2129 }
2130 | primary_value '[' opt_call_args rbracket
2131 {
2132 /*%%%*/
2133 $$ = aryset(p, $1, $3, &@$);
2134 /*% %*/
2135 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
2136 }
2137 | primary_value call_op tIDENTIFIER
2138 {
2139 if ($2 == tANDDOT) {
2140 yyerror1(&@2, "&. inside multiple assignment destination");
2141 }
2142 /*%%%*/
2143 $$ = attrset(p, $1, $2, $3, &@$);
2144 /*% %*/
2145 /*% ripper: field!($1, $2, $3) %*/
2146 }
2147 | primary_value tCOLON2 tIDENTIFIER
2148 {
2149 /*%%%*/
2150 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2151 /*% %*/
2152 /*% ripper: const_path_field!($1, $3) %*/
2153 }
2154 | primary_value call_op tCONSTANT
2155 {
2156 if ($2 == tANDDOT) {
2157 yyerror1(&@2, "&. inside multiple assignment destination");
2158 }
2159 /*%%%*/
2160 $$ = attrset(p, $1, $2, $3, &@$);
2161 /*% %*/
2162 /*% ripper: field!($1, $2, $3) %*/
2163 }
2164 | primary_value tCOLON2 tCONSTANT
2165 {
2166 /*%%%*/
2167 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2168 /*% %*/
2169 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
2170 }
2171 | tCOLON3 tCONSTANT
2172 {
2173 /*%%%*/
2174 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2175 /*% %*/
2176 /*% ripper: const_decl(p, top_const_field!($2)) %*/
2177 }
2178 | backref
2179 {
2180 /*%%%*/
2181 rb_backref_error(p, $1);
2182 $$ = NEW_BEGIN(0, &@$);
2183 /*% %*/
2184 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
2185 }
2186 ;
2187
2188lhs : user_variable
2189 {
2190 /*%%%*/
2191 $$ = assignable(p, $1, 0, &@$);
2192 /*% %*/
2193 /*% ripper: assignable(p, var_field(p, $1)) %*/
2194 }
2195 | keyword_variable
2196 {
2197 /*%%%*/
2198 $$ = assignable(p, $1, 0, &@$);
2199 /*% %*/
2200 /*% ripper: assignable(p, var_field(p, $1)) %*/
2201 }
2202 | primary_value '[' opt_call_args rbracket
2203 {
2204 /*%%%*/
2205 $$ = aryset(p, $1, $3, &@$);
2206 /*% %*/
2207 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
2208 }
2209 | primary_value call_op tIDENTIFIER
2210 {
2211 /*%%%*/
2212 $$ = attrset(p, $1, $2, $3, &@$);
2213 /*% %*/
2214 /*% ripper: field!($1, $2, $3) %*/
2215 }
2216 | primary_value tCOLON2 tIDENTIFIER
2217 {
2218 /*%%%*/
2219 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2220 /*% %*/
2221 /*% ripper: field!($1, ID2VAL(idCOLON2), $3) %*/
2222 }
2223 | primary_value call_op tCONSTANT
2224 {
2225 /*%%%*/
2226 $$ = attrset(p, $1, $2, $3, &@$);
2227 /*% %*/
2228 /*% ripper: field!($1, $2, $3) %*/
2229 }
2230 | primary_value tCOLON2 tCONSTANT
2231 {
2232 /*%%%*/
2233 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2234 /*% %*/
2235 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
2236 }
2237 | tCOLON3 tCONSTANT
2238 {
2239 /*%%%*/
2240 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2241 /*% %*/
2242 /*% ripper: const_decl(p, top_const_field!($2)) %*/
2243 }
2244 | backref
2245 {
2246 /*%%%*/
2247 rb_backref_error(p, $1);
2248 $$ = NEW_BEGIN(0, &@$);
2249 /*% %*/
2250 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
2251 }
2252 ;
2253
2254cname : tIDENTIFIER
2255 {
2256 static const char mesg[] = "class/module name must be CONSTANT";
2257 /*%%%*/
2258 yyerror1(&@1, mesg);
2259 /*% %*/
2260 /*% ripper[error]: class_name_error!(ERR_MESG(), $1) %*/
2261 }
2262 | tCONSTANT
2263 ;
2264
2265cpath : tCOLON3 cname
2266 {
2267 /*%%%*/
2268 $$ = NEW_COLON3($2, &@$);
2269 /*% %*/
2270 /*% ripper: top_const_ref!($2) %*/
2271 }
2272 | cname
2273 {
2274 /*%%%*/
2275 $$ = NEW_COLON2(0, $$, &@$);
2276 /*% %*/
2277 /*% ripper: const_ref!($1) %*/
2278 }
2279 | primary_value tCOLON2 cname
2280 {
2281 /*%%%*/
2282 $$ = NEW_COLON2($1, $3, &@$);
2283 /*% %*/
2284 /*% ripper: const_path_ref!($1, $3) %*/
2285 }
2286 ;
2287
2288fname : tIDENTIFIER
2289 | tCONSTANT
2290 | tFID
2291 | op
2292 {
2293 SET_LEX_STATE(EXPR_ENDFN);
2294 $$ = $1;
2295 }
2296 | reswords
2297 ;
2298
2299fitem : fname
2300 {
2301 /*%%%*/
2302 $$ = NEW_LIT(ID2SYM($1), &@$);
2303 /*% %*/
2304 /*% ripper: symbol_literal!($1) %*/
2305 }
2306 | symbol
2307 ;
2308
2309undef_list : fitem
2310 {
2311 /*%%%*/
2312 $$ = NEW_UNDEF($1, &@$);
2313 /*% %*/
2314 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
2315 }
2316 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2317 {
2318 /*%%%*/
2319 NODE *undef = NEW_UNDEF($4, &@4);
2320 $$ = block_append(p, $1, undef);
2321 /*% %*/
2322 /*% ripper: rb_ary_push($1, get_value($4)) %*/
2323 }
2324 ;
2325
2326op : '|' { ifndef_ripper($$ = '|'); }
2327 | '^' { ifndef_ripper($$ = '^'); }
2328 | '&' { ifndef_ripper($$ = '&'); }
2329 | tCMP { ifndef_ripper($$ = tCMP); }
2330 | tEQ { ifndef_ripper($$ = tEQ); }
2331 | tEQQ { ifndef_ripper($$ = tEQQ); }
2332 | tMATCH { ifndef_ripper($$ = tMATCH); }
2333 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2334 | '>' { ifndef_ripper($$ = '>'); }
2335 | tGEQ { ifndef_ripper($$ = tGEQ); }
2336 | '<' { ifndef_ripper($$ = '<'); }
2337 | tLEQ { ifndef_ripper($$ = tLEQ); }
2338 | tNEQ { ifndef_ripper($$ = tNEQ); }
2339 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2340 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2341 | '+' { ifndef_ripper($$ = '+'); }
2342 | '-' { ifndef_ripper($$ = '-'); }
2343 | '*' { ifndef_ripper($$ = '*'); }
2344 | tSTAR { ifndef_ripper($$ = '*'); }
2345 | '/' { ifndef_ripper($$ = '/'); }
2346 | '%' { ifndef_ripper($$ = '%'); }
2347 | tPOW { ifndef_ripper($$ = tPOW); }
2348 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2349 | '!' { ifndef_ripper($$ = '!'); }
2350 | '~' { ifndef_ripper($$ = '~'); }
2351 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2352 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2353 | tAREF { ifndef_ripper($$ = tAREF); }
2354 | tASET { ifndef_ripper($$ = tASET); }
2355 | '`' { ifndef_ripper($$ = '`'); }
2356 ;
2357
2358reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2359 | keyword_BEGIN | keyword_END
2360 | keyword_alias | keyword_and | keyword_begin
2361 | keyword_break | keyword_case | keyword_class | keyword_def
2362 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2363 | keyword_end | keyword_ensure | keyword_false
2364 | keyword_for | keyword_in | keyword_module | keyword_next
2365 | keyword_nil | keyword_not | keyword_or | keyword_redo
2366 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2367 | keyword_super | keyword_then | keyword_true | keyword_undef
2368 | keyword_when | keyword_yield | keyword_if | keyword_unless
2369 | keyword_while | keyword_until
2370 ;
2371
2372arg : lhs '=' lex_ctxt arg_rhs
2373 {
2374 /*%%%*/
2375 $$ = node_assign(p, $1, $4, $3, &@$);
2376 /*% %*/
2377 /*% ripper: assign!($1, $4) %*/
2378 }
2379 | var_lhs tOP_ASGN lex_ctxt arg_rhs
2380 {
2381 /*%%%*/
2382 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
2383 /*% %*/
2384 /*% ripper: opassign!($1, $2, $4) %*/
2385 }
2386 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
2387 {
2388 /*%%%*/
2389 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
2390 /*% %*/
2391 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/
2392 }
2393 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2394 {
2395 /*%%%*/
2396 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2397 /*% %*/
2398 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2399 }
2400 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2401 {
2402 /*%%%*/
2403 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2404 /*% %*/
2405 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2406 }
2407 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2408 {
2409 /*%%%*/
2410 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
2411 /*% %*/
2412 /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $6) %*/
2413 }
2414 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2415 {
2416 /*%%%*/
2417 YYLTYPE loc = code_loc_gen(&@1, &@3);
2418 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
2419 /*% %*/
2420 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
2421 }
2422 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2423 {
2424 /*%%%*/
2425 YYLTYPE loc = code_loc_gen(&@1, &@2);
2426 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
2427 /*% %*/
2428 /*% ripper: opassign!(top_const_field!($2), $3, $5) %*/
2429 }
2430 | backref tOP_ASGN lex_ctxt arg_rhs
2431 {
2432 /*%%%*/
2433 rb_backref_error(p, $1);
2434 $$ = NEW_BEGIN(0, &@$);
2435 /*% %*/
2436 /*% ripper[error]: backref_error(p, RNODE($1), opassign!(var_field(p, $1), $2, $4)) %*/
2437 }
2438 | arg tDOT2 arg
2439 {
2440 /*%%%*/
2441 value_expr($1);
2442 value_expr($3);
2443 $$ = NEW_DOT2($1, $3, &@$);
2444 /*% %*/
2445 /*% ripper: dot2!($1, $3) %*/
2446 }
2447 | arg tDOT3 arg
2448 {
2449 /*%%%*/
2450 value_expr($1);
2451 value_expr($3);
2452 $$ = NEW_DOT3($1, $3, &@$);
2453 /*% %*/
2454 /*% ripper: dot3!($1, $3) %*/
2455 }
2456 | arg tDOT2
2457 {
2458 /*%%%*/
2459 value_expr($1);
2460 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
2461 /*% %*/
2462 /*% ripper: dot2!($1, Qnil) %*/
2463 }
2464 | arg tDOT3
2465 {
2466 /*%%%*/
2467 value_expr($1);
2468 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
2469 /*% %*/
2470 /*% ripper: dot3!($1, Qnil) %*/
2471 }
2472 | tBDOT2 arg
2473 {
2474 /*%%%*/
2475 value_expr($2);
2476 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
2477 /*% %*/
2478 /*% ripper: dot2!(Qnil, $2) %*/
2479 }
2480 | tBDOT3 arg
2481 {
2482 /*%%%*/
2483 value_expr($2);
2484 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
2485 /*% %*/
2486 /*% ripper: dot3!(Qnil, $2) %*/
2487 }
2488 | arg '+' arg
2489 {
2490 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2491 }
2492 | arg '-' arg
2493 {
2494 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2495 }
2496 | arg '*' arg
2497 {
2498 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2499 }
2500 | arg '/' arg
2501 {
2502 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2503 }
2504 | arg '%' arg
2505 {
2506 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2507 }
2508 | arg tPOW arg
2509 {
2510 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2511 }
2512 | tUMINUS_NUM simple_numeric tPOW arg
2513 {
2514 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2515 }
2516 | tUPLUS arg
2517 {
2518 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2519 }
2520 | tUMINUS arg
2521 {
2522 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2523 }
2524 | arg '|' arg
2525 {
2526 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2527 }
2528 | arg '^' arg
2529 {
2530 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2531 }
2532 | arg '&' arg
2533 {
2534 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2535 }
2536 | arg tCMP arg
2537 {
2538 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2539 }
2540 | rel_expr %prec tCMP
2541 | arg tEQ arg
2542 {
2543 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2544 }
2545 | arg tEQQ arg
2546 {
2547 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2548 }
2549 | arg tNEQ arg
2550 {
2551 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2552 }
2553 | arg tMATCH arg
2554 {
2555 $$ = match_op(p, $1, $3, &@2, &@$);
2556 }
2557 | arg tNMATCH arg
2558 {
2559 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2560 }
2561 | '!' arg
2562 {
2563 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2564 }
2565 | '~' arg
2566 {
2567 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2568 }
2569 | arg tLSHFT arg
2570 {
2571 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2572 }
2573 | arg tRSHFT arg
2574 {
2575 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2576 }
2577 | arg tANDOP arg
2578 {
2579 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2580 }
2581 | arg tOROP arg
2582 {
2583 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2584 }
2585 | keyword_defined opt_nl {p->ctxt.in_defined = 1;} arg
2586 {
2587 p->ctxt.in_defined = 0;
2588 $$ = new_defined(p, $4, &@$);
2589 }
2590 | arg '?' arg opt_nl ':' arg
2591 {
2592 /*%%%*/
2593 value_expr($1);
2594 $$ = new_if(p, $1, $3, $6, &@$);
2595 fixpos($$, $1);
2596 /*% %*/
2597 /*% ripper: ifop!($1, $3, $6) %*/
2598 }
2599 | defn_head f_opt_paren_args '=' arg
2600 {
2601 endless_method_name(p, $<node>1, &@1);
2602 restore_defun(p, $<node>1->nd_defn);
2603 /*%%%*/
2604 $$ = set_defun_body(p, $1, $2, $4, &@$);
2605 /*% %*/
2606 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
2607 /*% ripper: def!(get_value($1), $2, $4) %*/
2608 local_pop(p);
2609 }
2610 | defn_head f_opt_paren_args '=' arg modifier_rescue arg
2611 {
2612 endless_method_name(p, $<node>1, &@1);
2613 restore_defun(p, $<node>1->nd_defn);
2614 /*%%%*/
2615 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2616 $$ = set_defun_body(p, $1, $2, $4, &@$);
2617 /*% %*/
2618 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
2619 /*% ripper: def!(get_value($1), $2, $4) %*/
2620 local_pop(p);
2621 }
2622 | defs_head f_opt_paren_args '=' arg
2623 {
2624 endless_method_name(p, $<node>1, &@1);
2625 restore_defun(p, $<node>1->nd_defn);
2626 /*%%%*/
2627 $$ = set_defun_body(p, $1, $2, $4, &@$);
2628 /*%
2629 $1 = get_value($1);
2630 %*/
2631 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
2632 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
2633 local_pop(p);
2634 }
2635 | defs_head f_opt_paren_args '=' arg modifier_rescue arg
2636 {
2637 endless_method_name(p, $<node>1, &@1);
2638 restore_defun(p, $<node>1->nd_defn);
2639 /*%%%*/
2640 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2641 $$ = set_defun_body(p, $1, $2, $4, &@$);
2642 /*%
2643 $1 = get_value($1);
2644 %*/
2645 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
2646 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
2647 local_pop(p);
2648 }
2649 | primary
2650 {
2651 $$ = $1;
2652 }
2653 ;
2654
2655relop : '>' {$$ = '>';}
2656 | '<' {$$ = '<';}
2657 | tGEQ {$$ = idGE;}
2658 | tLEQ {$$ = idLE;}
2659 ;
2660
2661rel_expr : arg relop arg %prec '>'
2662 {
2663 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2664 }
2665 | rel_expr relop arg %prec '>'
2666 {
2667 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2668 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2669 }
2670 ;
2671
2672lex_ctxt : tSP
2673 {
2674 $$ = p->ctxt;
2675 }
2676 | none
2677 {
2678 $$ = p->ctxt;
2679 }
2680 ;
2681
2682arg_value : arg
2683 {
2684 value_expr($1);
2685 $$ = $1;
2686 }
2687 ;
2688
2689aref_args : none
2690 | args trailer
2691 {
2692 $$ = $1;
2693 }
2694 | args ',' assocs trailer
2695 {
2696 /*%%%*/
2697 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2698 /*% %*/
2699 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2700 }
2701 | assocs trailer
2702 {
2703 /*%%%*/
2704 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2705 /*% %*/
2706 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2707 }
2708 ;
2709
2710arg_rhs : arg %prec tOP_ASGN
2711 {
2712 value_expr($1);
2713 $$ = $1;
2714 }
2715 | arg modifier_rescue arg
2716 {
2717 /*%%%*/
2718 value_expr($1);
2719 $$ = rescued_expr(p, $1, $3, &@1, &@2, &@3);
2720 /*% %*/
2721 /*% ripper: rescue_mod!($1, $3) %*/
2722 }
2723 ;
2724
2725paren_args : '(' opt_call_args rparen
2726 {
2727 /*%%%*/
2728 $$ = $2;
2729 /*% %*/
2730 /*% ripper: arg_paren!(escape_Qundef($2)) %*/
2731 }
2732 | '(' args ',' args_forward rparen
2733 {
2734 if (!check_forwarding_args(p)) {
2735 $$ = Qnone;
2736 }
2737 else {
2738 /*%%%*/
2739 $$ = new_args_forward_call(p, $2, &@4, &@$);
2740 /*% %*/
2741 /*% ripper: arg_paren!(args_add!($2, $4)) %*/
2742 }
2743 }
2744 | '(' args_forward rparen
2745 {
2746 if (!check_forwarding_args(p)) {
2747 $$ = Qnone;
2748 }
2749 else {
2750 /*%%%*/
2751 $$ = new_args_forward_call(p, 0, &@2, &@$);
2752 /*% %*/
2753 /*% ripper: arg_paren!($2) %*/
2754 }
2755 }
2756 ;
2757
2758opt_paren_args : none
2759 | paren_args
2760 ;
2761
2762opt_call_args : none
2763 | call_args
2764 | args ','
2765 {
2766 $$ = $1;
2767 }
2768 | args ',' assocs ','
2769 {
2770 /*%%%*/
2771 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2772 /*% %*/
2773 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2774 }
2775 | assocs ','
2776 {
2777 /*%%%*/
2778 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2779 /*% %*/
2780 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2781 }
2782 ;
2783
2784call_args : command
2785 {
2786 /*%%%*/
2787 value_expr($1);
2788 $$ = NEW_LIST($1, &@$);
2789 /*% %*/
2790 /*% ripper: args_add!(args_new!, $1) %*/
2791 }
2792 | args opt_block_arg
2793 {
2794 /*%%%*/
2795 $$ = arg_blk_pass($1, $2);
2796 /*% %*/
2797 /*% ripper: args_add_block!($1, $2) %*/
2798 }
2799 | assocs opt_block_arg
2800 {
2801 /*%%%*/
2802 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2803 $$ = arg_blk_pass($$, $2);
2804 /*% %*/
2805 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($1)), $2) %*/
2806 }
2807 | args ',' assocs opt_block_arg
2808 {
2809 /*%%%*/
2810 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2811 $$ = arg_blk_pass($$, $4);
2812 /*% %*/
2813 /*% ripper: args_add_block!(args_add!($1, bare_assoc_hash!($3)), $4) %*/
2814 }
2815 | block_arg
2816 /*% ripper[brace]: args_add_block!(args_new!, $1) %*/
2817 ;
2818
2819command_args : {
2820 /* If call_args starts with a open paren '(' or '[',
2821 * look-ahead reading of the letters calls CMDARG_PUSH(0),
2822 * but the push must be done after CMDARG_PUSH(1).
2823 * So this code makes them consistent by first cancelling
2824 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
2825 * and finally redoing CMDARG_PUSH(0).
2826 */
2827 int lookahead = 0;
2828 switch (yychar) {
2829 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
2830 lookahead = 1;
2831 }
2832 if (lookahead) CMDARG_POP();
2833 CMDARG_PUSH(1);
2834 if (lookahead) CMDARG_PUSH(0);
2835 }
2836 call_args
2837 {
2838 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
2839 * but the push must be done after CMDARG_POP() in the parser.
2840 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
2841 * CMDARG_POP() to pop 1 pushed by command_args,
2842 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
2843 */
2844 int lookahead = 0;
2845 switch (yychar) {
2846 case tLBRACE_ARG:
2847 lookahead = 1;
2848 }
2849 if (lookahead) CMDARG_POP();
2850 CMDARG_POP();
2851 if (lookahead) CMDARG_PUSH(0);
2852 $$ = $2;
2853 }
2854 ;
2855
2856block_arg : tAMPER arg_value
2857 {
2858 /*%%%*/
2859 $$ = NEW_BLOCK_PASS($2, &@$);
2860 /*% %*/
2861 /*% ripper: $2 %*/
2862 }
2863 | tAMPER
2864 {
2865 /*%%%*/
2866 if (!local_id(p, ANON_BLOCK_ID)) {
2867 compile_error(p, "no anonymous block parameter");
2868 }
2869 $$ = NEW_BLOCK_PASS(NEW_LVAR(ANON_BLOCK_ID, &@1), &@$);
2870 /*%
2871 $$ = Qnil;
2872 %*/
2873 }
2874 ;
2875
2876opt_block_arg : ',' block_arg
2877 {
2878 $$ = $2;
2879 }
2880 | none
2881 {
2882 $$ = 0;
2883 }
2884 ;
2885
2886/* value */
2887args : arg_value
2888 {
2889 /*%%%*/
2890 $$ = NEW_LIST($1, &@$);
2891 /*% %*/
2892 /*% ripper: args_add!(args_new!, $1) %*/
2893 }
2894 | tSTAR arg_value
2895 {
2896 /*%%%*/
2897 $$ = NEW_SPLAT($2, &@$);
2898 /*% %*/
2899 /*% ripper: args_add_star!(args_new!, $2) %*/
2900 }
2901 | args ',' arg_value
2902 {
2903 /*%%%*/
2904 $$ = last_arg_append(p, $1, $3, &@$);
2905 /*% %*/
2906 /*% ripper: args_add!($1, $3) %*/
2907 }
2908 | args ',' tSTAR arg_value
2909 {
2910 /*%%%*/
2911 $$ = rest_arg_append(p, $1, $4, &@$);
2912 /*% %*/
2913 /*% ripper: args_add_star!($1, $4) %*/
2914 }
2915 ;
2916
2917/* value */
2918mrhs_arg : mrhs
2919 | arg_value
2920 ;
2921
2922/* value */
2923mrhs : args ',' arg_value
2924 {
2925 /*%%%*/
2926 $$ = last_arg_append(p, $1, $3, &@$);
2927 /*% %*/
2928 /*% ripper: mrhs_add!(mrhs_new_from_args!($1), $3) %*/
2929 }
2930 | args ',' tSTAR arg_value
2931 {
2932 /*%%%*/
2933 $$ = rest_arg_append(p, $1, $4, &@$);
2934 /*% %*/
2935 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($1), $4) %*/
2936 }
2937 | tSTAR arg_value
2938 {
2939 /*%%%*/
2940 $$ = NEW_SPLAT($2, &@$);
2941 /*% %*/
2942 /*% ripper: mrhs_add_star!(mrhs_new!, $2) %*/
2943 }
2944 ;
2945
2946primary : literal
2947 | strings
2948 | xstring
2949 | regexp
2950 | words
2951 | qwords
2952 | symbols
2953 | qsymbols
2954 | var_ref
2955 | backref
2956 | tFID
2957 {
2958 /*%%%*/
2959 $$ = NEW_FCALL($1, 0, &@$);
2960 /*% %*/
2961 /*% ripper: method_add_arg!(fcall!($1), args_new!) %*/
2962 }
2963 | k_begin
2964 {
2965 CMDARG_PUSH(0);
2966 }
2967 bodystmt
2968 k_end
2969 {
2970 CMDARG_POP();
2971 /*%%%*/
2972 set_line_body($3, @1.end_pos.lineno);
2973 $$ = NEW_BEGIN($3, &@$);
2974 nd_set_line($$, @1.end_pos.lineno);
2975 /*% %*/
2976 /*% ripper: begin!($3) %*/
2977 }
2978 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2979 {
2980 /*%%%*/
2981 $$ = NEW_BEGIN(0, &@$);
2982 /*% %*/
2983 /*% ripper: paren!(0) %*/
2984 }
2985 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2986 {
2987 /*%%%*/
2988 if (nd_type_p($2, NODE_SELF)) $2->nd_state = 0;
2989 $$ = $2;
2990 /*% %*/
2991 /*% ripper: paren!($2) %*/
2992 }
2993 | tLPAREN compstmt ')'
2994 {
2995 /*%%%*/
2996 if (nd_type_p($2, NODE_SELF)) $2->nd_state = 0;
2997 $$ = $2;
2998 /*% %*/
2999 /*% ripper: paren!($2) %*/
3000 }
3001 | primary_value tCOLON2 tCONSTANT
3002 {
3003 /*%%%*/
3004 $$ = NEW_COLON2($1, $3, &@$);
3005 /*% %*/
3006 /*% ripper: const_path_ref!($1, $3) %*/
3007 }
3008 | tCOLON3 tCONSTANT
3009 {
3010 /*%%%*/
3011 $$ = NEW_COLON3($2, &@$);
3012 /*% %*/
3013 /*% ripper: top_const_ref!($2) %*/
3014 }
3015 | tLBRACK aref_args ']'
3016 {
3017 /*%%%*/
3018 $$ = make_list($2, &@$);
3019 /*% %*/
3020 /*% ripper: array!(escape_Qundef($2)) %*/
3021 }
3022 | tLBRACE assoc_list '}'
3023 {
3024 /*%%%*/
3025 $$ = new_hash(p, $2, &@$);
3026 $$->nd_brace = TRUE;
3027 /*% %*/
3028 /*% ripper: hash!(escape_Qundef($2)) %*/
3029 }
3030 | k_return
3031 {
3032 /*%%%*/
3033 $$ = NEW_RETURN(0, &@$);
3034 /*% %*/
3035 /*% ripper: return0! %*/
3036 }
3037 | keyword_yield '(' call_args rparen
3038 {
3039 /*%%%*/
3040 $$ = new_yield(p, $3, &@$);
3041 /*% %*/
3042 /*% ripper: yield!(paren!($3)) %*/
3043 }
3044 | keyword_yield '(' rparen
3045 {
3046 /*%%%*/
3047 $$ = NEW_YIELD(0, &@$);
3048 /*% %*/
3049 /*% ripper: yield!(paren!(args_new!)) %*/
3050 }
3051 | keyword_yield
3052 {
3053 /*%%%*/
3054 $$ = NEW_YIELD(0, &@$);
3055 /*% %*/
3056 /*% ripper: yield0! %*/
3057 }
3058 | keyword_defined opt_nl '(' {p->ctxt.in_defined = 1;} expr rparen
3059 {
3060 p->ctxt.in_defined = 0;
3061 $$ = new_defined(p, $5, &@$);
3062 }
3063 | keyword_not '(' expr rparen
3064 {
3065 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3066 }
3067 | keyword_not '(' rparen
3068 {
3069 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
3070 }
3071 | fcall brace_block
3072 {
3073 /*%%%*/
3074 $$ = method_add_block(p, $1, $2, &@$);
3075 /*% %*/
3076 /*% ripper: method_add_block!(method_add_arg!(fcall!($1), args_new!), $2) %*/
3077 }
3078 | method_call
3079 | method_call brace_block
3080 {
3081 /*%%%*/
3082 block_dup_check(p, $1->nd_args, $2);
3083 $$ = method_add_block(p, $1, $2, &@$);
3084 /*% %*/
3085 /*% ripper: method_add_block!($1, $2) %*/
3086 }
3087 | lambda
3088 | k_if expr_value then
3089 compstmt
3090 if_tail
3091 k_end
3092 {
3093 /*%%%*/
3094 $$ = new_if(p, $2, $4, $5, &@$);
3095 fixpos($$, $2);
3096 /*% %*/
3097 /*% ripper: if!($2, $4, escape_Qundef($5)) %*/
3098 }
3099 | k_unless expr_value then
3100 compstmt
3101 opt_else
3102 k_end
3103 {
3104 /*%%%*/
3105 $$ = new_unless(p, $2, $4, $5, &@$);
3106 fixpos($$, $2);
3107 /*% %*/
3108 /*% ripper: unless!($2, $4, escape_Qundef($5)) %*/
3109 }
3110 | k_while expr_value_do
3111 compstmt
3112 k_end
3113 {
3114 /*%%%*/
3115 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
3116 fixpos($$, $2);
3117 /*% %*/
3118 /*% ripper: while!($2, $3) %*/
3119 }
3120 | k_until expr_value_do
3121 compstmt
3122 k_end
3123 {
3124 /*%%%*/
3125 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
3126 fixpos($$, $2);
3127 /*% %*/
3128 /*% ripper: until!($2, $3) %*/
3129 }
3130 | k_case expr_value opt_terms
3131 {
3132 $<val>$ = p->case_labels;
3133 p->case_labels = Qnil;
3134 }
3135 case_body
3136 k_end
3137 {
3138 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3139 p->case_labels = $<val>4;
3140 /*%%%*/
3141 $$ = NEW_CASE($2, $5, &@$);
3142 fixpos($$, $2);
3143 /*% %*/
3144 /*% ripper: case!($2, $5) %*/
3145 }
3146 | k_case opt_terms
3147 {
3148 $<val>$ = p->case_labels;
3149 p->case_labels = 0;
3150 }
3151 case_body
3152 k_end
3153 {
3154 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3155 p->case_labels = $<val>3;
3156 /*%%%*/
3157 $$ = NEW_CASE2($4, &@$);
3158 /*% %*/
3159 /*% ripper: case!(Qnil, $4) %*/
3160 }
3161 | k_case expr_value opt_terms
3162 p_case_body
3163 k_end
3164 {
3165 /*%%%*/
3166 $$ = NEW_CASE3($2, $4, &@$);
3167 /*% %*/
3168 /*% ripper: case!($2, $4) %*/
3169 }
3170 | k_for for_var keyword_in expr_value_do
3171 compstmt
3172 k_end
3173 {
3174 /*%%%*/
3175 /*
3176 * for a, b, c in e
3177 * #=>
3178 * e.each{|*x| a, b, c = x}
3179 *
3180 * for a in e
3181 * #=>
3182 * e.each{|x| a, = x}
3183 */
3184 ID id = internal_id(p);
3185 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
3186 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
3187 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
3188 tbl->ids[0] = id; /* internal id */
3189
3190 switch (nd_type($2)) {
3191 case NODE_LASGN:
3192 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
3193 $2->nd_value = internal_var;
3194 id = 0;
3195 m->nd_plen = 1;
3196 m->nd_next = $2;
3197 break;
3198 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
3199 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
3200 break;
3201 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
3202 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
3203 }
3204 /* {|*internal_id| <m> = internal_id; ... } */
3205 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
3206 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
3207 $$ = NEW_FOR($4, scope, &@$);
3208 fixpos($$, $2);
3209 /*% %*/
3210 /*% ripper: for!($2, $4, $5) %*/
3211 }
3212 | k_class cpath superclass
3213 {
3214 if (p->ctxt.in_def) {
3215 YYLTYPE loc = code_loc_gen(&@1, &@2);
3216 yyerror1(&loc, "class definition in method body");
3217 }
3218 p->ctxt.in_class = 1;
3219 local_push(p, 0);
3220 }
3221 bodystmt
3222 k_end
3223 {
3224 /*%%%*/
3225 $$ = NEW_CLASS($2, $5, $3, &@$);
3226 nd_set_line($$->nd_body, @6.end_pos.lineno);
3227 set_line_body($5, @3.end_pos.lineno);
3228 nd_set_line($$, @3.end_pos.lineno);
3229 /*% %*/
3230 /*% ripper: class!($2, $3, $5) %*/
3231 local_pop(p);
3232 p->ctxt.in_class = $<ctxt>1.in_class;
3233 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3234 }
3235 | k_class tLSHFT expr
3236 {
3237 p->ctxt.in_def = 0;
3238 p->ctxt.in_class = 0;
3239 local_push(p, 0);
3240 }
3241 term
3242 bodystmt
3243 k_end
3244 {
3245 /*%%%*/
3246 $$ = NEW_SCLASS($3, $6, &@$);
3247 nd_set_line($$->nd_body, @7.end_pos.lineno);
3248 set_line_body($6, nd_line($3));
3249 fixpos($$, $3);
3250 /*% %*/
3251 /*% ripper: sclass!($3, $6) %*/
3252 local_pop(p);
3253 p->ctxt.in_def = $<ctxt>1.in_def;
3254 p->ctxt.in_class = $<ctxt>1.in_class;
3255 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3256 }
3257 | k_module cpath
3258 {
3259 if (p->ctxt.in_def) {
3260 YYLTYPE loc = code_loc_gen(&@1, &@2);
3261 yyerror1(&loc, "module definition in method body");
3262 }
3263 p->ctxt.in_class = 1;
3264 local_push(p, 0);
3265 }
3266 bodystmt
3267 k_end
3268 {
3269 /*%%%*/
3270 $$ = NEW_MODULE($2, $4, &@$);
3271 nd_set_line($$->nd_body, @5.end_pos.lineno);
3272 set_line_body($4, @2.end_pos.lineno);
3273 nd_set_line($$, @2.end_pos.lineno);
3274 /*% %*/
3275 /*% ripper: module!($2, $4) %*/
3276 local_pop(p);
3277 p->ctxt.in_class = $<ctxt>1.in_class;
3278 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3279 }
3280 | defn_head
3281 f_arglist
3282 bodystmt
3283 k_end
3284 {
3285 restore_defun(p, $<node>1->nd_defn);
3286 /*%%%*/
3287 $$ = set_defun_body(p, $1, $2, $3, &@$);
3288 /*% %*/
3289 /*% ripper: def!(get_value($1), $2, $3) %*/
3290 local_pop(p);
3291 }
3292 | defs_head
3293 f_arglist
3294 bodystmt
3295 k_end
3296 {
3297 restore_defun(p, $<node>1->nd_defn);
3298 /*%%%*/
3299 $$ = set_defun_body(p, $1, $2, $3, &@$);
3300 /*%
3301 $1 = get_value($1);
3302 %*/
3303 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $3) %*/
3304 local_pop(p);
3305 }
3306 | keyword_break
3307 {
3308 /*%%%*/
3309 $$ = NEW_BREAK(0, &@$);
3310 /*% %*/
3311 /*% ripper: break!(args_new!) %*/
3312 }
3313 | keyword_next
3314 {
3315 /*%%%*/
3316 $$ = NEW_NEXT(0, &@$);
3317 /*% %*/
3318 /*% ripper: next!(args_new!) %*/
3319 }
3320 | keyword_redo
3321 {
3322 /*%%%*/
3323 $$ = NEW_REDO(&@$);
3324 /*% %*/
3325 /*% ripper: redo! %*/
3326 }
3327 | keyword_retry
3328 {
3329 /*%%%*/
3330 $$ = NEW_RETRY(&@$);
3331 /*% %*/
3332 /*% ripper: retry! %*/
3333 }
3334 ;
3335
3336primary_value : primary
3337 {
3338 value_expr($1);
3339 $$ = $1;
3340 }
3341 ;
3342
3343k_begin : keyword_begin
3344 {
3345 token_info_push(p, "begin", &@$);
3346 }
3347 ;
3348
3349k_if : keyword_if
3350 {
3351 WARN_EOL("if");
3352 token_info_push(p, "if", &@$);
3353 if (p->token_info && p->token_info->nonspc &&
3354 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3355 const char *tok = p->lex.ptok;
3356 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3357 beg += rb_strlen_lit("else");
3358 while (beg < tok && ISSPACE(*beg)) beg++;
3359 if (beg == tok) {
3360 p->token_info->nonspc = 0;
3361 }
3362 }
3363 }
3364 ;
3365
3366k_unless : keyword_unless
3367 {
3368 token_info_push(p, "unless", &@$);
3369 }
3370 ;
3371
3372k_while : keyword_while
3373 {
3374 token_info_push(p, "while", &@$);
3375 }
3376 ;
3377
3378k_until : keyword_until
3379 {
3380 token_info_push(p, "until", &@$);
3381 }
3382 ;
3383
3384k_case : keyword_case
3385 {
3386 token_info_push(p, "case", &@$);
3387 }
3388 ;
3389
3390k_for : keyword_for
3391 {
3392 token_info_push(p, "for", &@$);
3393 }
3394 ;
3395
3396k_class : keyword_class
3397 {
3398 token_info_push(p, "class", &@$);
3399 $<ctxt>$ = p->ctxt;
3400 }
3401 ;
3402
3403k_module : keyword_module
3404 {
3405 token_info_push(p, "module", &@$);
3406 $<ctxt>$ = p->ctxt;
3407 }
3408 ;
3409
3410k_def : keyword_def
3411 {
3412 token_info_push(p, "def", &@$);
3413 p->ctxt.in_argdef = 1;
3414 }
3415 ;
3416
3417k_do : keyword_do
3418 {
3419 token_info_push(p, "do", &@$);
3420 }
3421 ;
3422
3423k_do_block : keyword_do_block
3424 {
3425 token_info_push(p, "do", &@$);
3426 }
3427 ;
3428
3429k_rescue : keyword_rescue
3430 {
3431 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3432 }
3433 ;
3434
3435k_ensure : keyword_ensure
3436 {
3437 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3438 }
3439 ;
3440
3441k_when : keyword_when
3442 {
3443 token_info_warn(p, "when", p->token_info, 0, &@$);
3444 }
3445 ;
3446
3447k_else : keyword_else
3448 {
3449 token_info *ptinfo_beg = p->token_info;
3450 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3451 token_info_warn(p, "else", p->token_info, same, &@$);
3452 if (same) {
3453 token_info e;
3454 e.next = ptinfo_beg->next;
3455 e.token = "else";
3456 token_info_setup(&e, p->lex.pbeg, &@$);
3457 if (!e.nonspc) *ptinfo_beg = e;
3458 }
3459 }
3460 ;
3461
3462k_elsif : keyword_elsif
3463 {
3464 WARN_EOL("elsif");
3465 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3466 }
3467 ;
3468
3469k_end : keyword_end
3470 {
3471 token_info_pop(p, "end", &@$);
3472 }
3473 ;
3474
3475k_return : keyword_return
3476 {
3477 if (p->ctxt.in_class && !p->ctxt.in_def && !dyna_in_block(p))
3478 yyerror1(&@1, "Invalid return in class/module body");
3479 }
3480 ;
3481
3482then : term
3483 | keyword_then
3484 | term keyword_then
3485 ;
3486
3487do : term
3488 | keyword_do_cond
3489 ;
3490
3491if_tail : opt_else
3492 | k_elsif expr_value then
3493 compstmt
3494 if_tail
3495 {
3496 /*%%%*/
3497 $$ = new_if(p, $2, $4, $5, &@$);
3498 fixpos($$, $2);
3499 /*% %*/
3500 /*% ripper: elsif!($2, $4, escape_Qundef($5)) %*/
3501 }
3502 ;
3503
3504opt_else : none
3505 | k_else compstmt
3506 {
3507 /*%%%*/
3508 $$ = $2;
3509 /*% %*/
3510 /*% ripper: else!($2) %*/
3511 }
3512 ;
3513
3514for_var : lhs
3515 | mlhs
3516 ;
3517
3518f_marg : f_norm_arg
3519 {
3520 /*%%%*/
3521 $$ = assignable(p, $1, 0, &@$);
3522 mark_lvar_used(p, $$);
3523 /*% %*/
3524 /*% ripper: assignable(p, $1) %*/
3525 }
3526 | tLPAREN f_margs rparen
3527 {
3528 /*%%%*/
3529 $$ = $2;
3530 /*% %*/
3531 /*% ripper: mlhs_paren!($2) %*/
3532 }
3533 ;
3534
3535f_marg_list : f_marg
3536 {
3537 /*%%%*/
3538 $$ = NEW_LIST($1, &@$);
3539 /*% %*/
3540 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
3541 }
3542 | f_marg_list ',' f_marg
3543 {
3544 /*%%%*/
3545 $$ = list_append(p, $1, $3);
3546 /*% %*/
3547 /*% ripper: mlhs_add!($1, $3) %*/
3548 }
3549 ;
3550
3551f_margs : f_marg_list
3552 {
3553 /*%%%*/
3554 $$ = NEW_MASGN($1, 0, &@$);
3555 /*% %*/
3556 /*% ripper: $1 %*/
3557 }
3558 | f_marg_list ',' f_rest_marg
3559 {
3560 /*%%%*/
3561 $$ = NEW_MASGN($1, $3, &@$);
3562 /*% %*/
3563 /*% ripper: mlhs_add_star!($1, $3) %*/
3564 }
3565 | f_marg_list ',' f_rest_marg ',' f_marg_list
3566 {
3567 /*%%%*/
3568 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3569 /*% %*/
3570 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
3571 }
3572 | f_rest_marg
3573 {
3574 /*%%%*/
3575 $$ = NEW_MASGN(0, $1, &@$);
3576 /*% %*/
3577 /*% ripper: mlhs_add_star!(mlhs_new!, $1) %*/
3578 }
3579 | f_rest_marg ',' f_marg_list
3580 {
3581 /*%%%*/
3582 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3583 /*% %*/
3584 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $1), $3) %*/
3585 }
3586 ;
3587
3588f_rest_marg : tSTAR f_norm_arg
3589 {
3590 /*%%%*/
3591 $$ = assignable(p, $2, 0, &@$);
3592 mark_lvar_used(p, $$);
3593 /*% %*/
3594 /*% ripper: assignable(p, $2) %*/
3595 }
3596 | tSTAR
3597 {
3598 /*%%%*/
3599 $$ = NODE_SPECIAL_NO_NAME_REST;
3600 /*% %*/
3601 /*% ripper: Qnil %*/
3602 }
3603 ;
3604
3605f_any_kwrest : f_kwrest
3606 | f_no_kwarg {$$ = ID2VAL(idNil);}
3607 ;
3608
3609f_eq : {p->ctxt.in_argdef = 0;} '=';
3610
3611block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3612 {
3613 $$ = new_args_tail(p, $1, $3, $4, &@3);
3614 }
3615 | f_block_kwarg opt_f_block_arg
3616 {
3617 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3618 }
3619 | f_any_kwrest opt_f_block_arg
3620 {
3621 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3622 }
3623 | f_block_arg
3624 {
3625 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3626 }
3627 ;
3628
3629opt_block_args_tail : ',' block_args_tail
3630 {
3631 $$ = $2;
3632 }
3633 | /* none */
3634 {
3635 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3636 }
3637 ;
3638
3639excessed_comma : ','
3640 {
3641 /* magic number for rest_id in iseq_set_arguments() */
3642 /*%%%*/
3643 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
3644 /*% %*/
3645 /*% ripper: excessed_comma! %*/
3646 }
3647 ;
3648
3649block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3650 {
3651 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3652 }
3653 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3654 {
3655 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3656 }
3657 | f_arg ',' f_block_optarg opt_block_args_tail
3658 {
3659 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3660 }
3661 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3662 {
3663 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3664 }
3665 | f_arg ',' f_rest_arg opt_block_args_tail
3666 {
3667 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3668 }
3669 | f_arg excessed_comma
3670 {
3671 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@2);
3672 $$ = new_args(p, $1, Qnone, $2, Qnone, $$, &@$);
3673 }
3674 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3675 {
3676 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
3677 }
3678 | f_arg opt_block_args_tail
3679 {
3680 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
3681 }
3682 | f_block_optarg ',' f_rest_arg opt_block_args_tail
3683 {
3684 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
3685 }
3686 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3687 {
3688 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
3689 }
3690 | f_block_optarg opt_block_args_tail
3691 {
3692 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
3693 }
3694 | f_block_optarg ',' f_arg opt_block_args_tail
3695 {
3696 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
3697 }
3698 | f_rest_arg opt_block_args_tail
3699 {
3700 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
3701 }
3702 | f_rest_arg ',' f_arg opt_block_args_tail
3703 {
3704 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
3705 }
3706 | block_args_tail
3707 {
3708 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
3709 }
3710 ;
3711
3712opt_block_param : none
3713 | block_param_def
3714 {
3715 p->command_start = TRUE;
3716 }
3717 ;
3718
3719block_param_def : '|' opt_bv_decl '|'
3720 {
3721 p->cur_arg = 0;
3722 p->max_numparam = ORDINAL_PARAM;
3723 p->ctxt.in_argdef = 0;
3724 /*%%%*/
3725 $$ = 0;
3726 /*% %*/
3727 /*% ripper: block_var!(params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), escape_Qundef($2)) %*/
3728 }
3729 | '|' block_param opt_bv_decl '|'
3730 {
3731 p->cur_arg = 0;
3732 p->max_numparam = ORDINAL_PARAM;
3733 p->ctxt.in_argdef = 0;
3734 /*%%%*/
3735 $$ = $2;
3736 /*% %*/
3737 /*% ripper: block_var!(escape_Qundef($2), escape_Qundef($3)) %*/
3738 }
3739 ;
3740
3741
3742opt_bv_decl : opt_nl
3743 {
3744 $$ = 0;
3745 }
3746 | opt_nl ';' bv_decls opt_nl
3747 {
3748 /*%%%*/
3749 $$ = 0;
3750 /*% %*/
3751 /*% ripper: $3 %*/
3752 }
3753 ;
3754
3755bv_decls : bvar
3756 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
3757 | bv_decls ',' bvar
3758 /*% ripper[brace]: rb_ary_push($1, get_value($3)) %*/
3759 ;
3760
3761bvar : tIDENTIFIER
3762 {
3763 new_bv(p, get_id($1));
3764 /*% ripper: get_value($1) %*/
3765 }
3766 | f_bad_arg
3767 {
3768 $$ = 0;
3769 }
3770 ;
3771
3772lambda : tLAMBDA
3773 {
3774 token_info_push(p, "->", &@1);
3775 $<vars>1 = dyna_push(p);
3776 $<num>$ = p->lex.lpar_beg;
3777 p->lex.lpar_beg = p->lex.paren_nest;
3778 }
3779 {
3780 $<num>$ = p->max_numparam;
3781 p->max_numparam = 0;
3782 }
3783 {
3784 $<node>$ = numparam_push(p);
3785 }
3786 f_larglist
3787 {
3788 CMDARG_PUSH(0);
3789 }
3790 lambda_body
3791 {
3792 int max_numparam = p->max_numparam;
3793 p->lex.lpar_beg = $<num>2;
3794 p->max_numparam = $<num>3;
3795 CMDARG_POP();
3796 $5 = args_with_numbered(p, $5, max_numparam);
3797 /*%%%*/
3798 {
3799 YYLTYPE loc = code_loc_gen(&@5, &@7);
3800 $$ = NEW_LAMBDA($5, $7, &loc);
3801 nd_set_line($$->nd_body, @7.end_pos.lineno);
3802 nd_set_line($$, @5.end_pos.lineno);
3803 nd_set_first_loc($$, @1.beg_pos);
3804 }
3805 /*% %*/
3806 /*% ripper: lambda!($5, $7) %*/
3807 numparam_pop(p, $<node>4);
3808 dyna_pop(p, $<vars>1);
3809 }
3810 ;
3811
3812f_larglist : '(' f_args opt_bv_decl ')'
3813 {
3814 p->ctxt.in_argdef = 0;
3815 /*%%%*/
3816 $$ = $2;
3817 p->max_numparam = ORDINAL_PARAM;
3818 /*% %*/
3819 /*% ripper: paren!($2) %*/
3820 }
3821 | f_args
3822 {
3823 p->ctxt.in_argdef = 0;
3824 /*%%%*/
3825 if (!args_info_empty_p($1->nd_ainfo))
3826 p->max_numparam = ORDINAL_PARAM;
3827 /*% %*/
3828 $$ = $1;
3829 }
3830 ;
3831
3832lambda_body : tLAMBEG compstmt '}'
3833 {
3834 token_info_pop(p, "}", &@3);
3835 $$ = $2;
3836 }
3837 | keyword_do_LAMBDA bodystmt k_end
3838 {
3839 $$ = $2;
3840 }
3841 ;
3842
3843do_block : k_do_block do_body k_end
3844 {
3845 $$ = $2;
3846 /*%%%*/
3847 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3848 nd_set_line($$, @1.end_pos.lineno);
3849 /*% %*/
3850 }
3851 ;
3852
3853block_call : command do_block
3854 {
3855 /*%%%*/
3856 if (nd_type_p($1, NODE_YIELD)) {
3857 compile_error(p, "block given to yield");
3858 }
3859 else {
3860 block_dup_check(p, $1->nd_args, $2);
3861 }
3862 $$ = method_add_block(p, $1, $2, &@$);
3863 fixpos($$, $1);
3864 /*% %*/
3865 /*% ripper: method_add_block!($1, $2) %*/
3866 }
3867 | block_call call_op2 operation2 opt_paren_args
3868 {
3869 /*%%%*/
3870 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3871 /*% %*/
3872 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
3873 }
3874 | block_call call_op2 operation2 opt_paren_args brace_block
3875 {
3876 /*%%%*/
3877 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3878 /*% %*/
3879 /*% ripper: opt_event(:method_add_block!, command_call!($1, $2, $3, $4), $5) %*/
3880 }
3881 | block_call call_op2 operation2 command_args do_block
3882 {
3883 /*%%%*/
3884 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3885 /*% %*/
3886 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
3887 }
3888 ;
3889
3890method_call : fcall paren_args
3891 {
3892 /*%%%*/
3893 $$ = $1;
3894 $$->nd_args = $2;
3895 nd_set_last_loc($1, @2.end_pos);
3896 /*% %*/
3897 /*% ripper: method_add_arg!(fcall!($1), $2) %*/
3898 }
3899 | primary_value call_op operation2 opt_paren_args
3900 {
3901 /*%%%*/
3902 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3903 nd_set_line($$, @3.end_pos.lineno);
3904 /*% %*/
3905 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
3906 }
3907 | primary_value tCOLON2 operation2 paren_args
3908 {
3909 /*%%%*/
3910 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
3911 nd_set_line($$, @3.end_pos.lineno);
3912 /*% %*/
3913 /*% ripper: method_add_arg!(call!($1, ID2VAL(idCOLON2), $3), $4) %*/
3914 }
3915 | primary_value tCOLON2 operation3
3916 {
3917 /*%%%*/
3918 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
3919 /*% %*/
3920 /*% ripper: call!($1, ID2VAL(idCOLON2), $3) %*/
3921 }
3922 | primary_value call_op paren_args
3923 {
3924 /*%%%*/
3925 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
3926 nd_set_line($$, @2.end_pos.lineno);
3927 /*% %*/
3928 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
3929 }
3930 | primary_value tCOLON2 paren_args
3931 {
3932 /*%%%*/
3933 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
3934 nd_set_line($$, @2.end_pos.lineno);
3935 /*% %*/
3936 /*% ripper: method_add_arg!(call!($1, ID2VAL(idCOLON2), ID2VAL(idCall)), $3) %*/
3937 }
3938 | keyword_super paren_args
3939 {
3940 /*%%%*/
3941 $$ = NEW_SUPER($2, &@$);
3942 /*% %*/
3943 /*% ripper: super!($2) %*/
3944 }
3945 | keyword_super
3946 {
3947 /*%%%*/
3948 $$ = NEW_ZSUPER(&@$);
3949 /*% %*/
3950 /*% ripper: zsuper! %*/
3951 }
3952 | primary_value '[' opt_call_args rbracket
3953 {
3954 /*%%%*/
3955 if ($1 && nd_type_p($1, NODE_SELF))
3956 $$ = NEW_FCALL(tAREF, $3, &@$);
3957 else
3958 $$ = NEW_CALL($1, tAREF, $3, &@$);
3959 fixpos($$, $1);
3960 /*% %*/
3961 /*% ripper: aref!($1, escape_Qundef($3)) %*/
3962 }
3963 ;
3964
3965brace_block : '{' brace_body '}'
3966 {
3967 $$ = $2;
3968 /*%%%*/
3969 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3970 nd_set_line($$, @1.end_pos.lineno);
3971 /*% %*/
3972 }
3973 | k_do do_body k_end
3974 {
3975 $$ = $2;
3976 /*%%%*/
3977 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3978 nd_set_line($$, @1.end_pos.lineno);
3979 /*% %*/
3980 }
3981 ;
3982
3983brace_body : {$<vars>$ = dyna_push(p);}
3984 {
3985 $<num>$ = p->max_numparam;
3986 p->max_numparam = 0;
3987 }
3988 {
3989 $<node>$ = numparam_push(p);
3990 }
3991 opt_block_param compstmt
3992 {
3993 int max_numparam = p->max_numparam;
3994 p->max_numparam = $<num>2;
3995 $4 = args_with_numbered(p, $4, max_numparam);
3996 /*%%%*/
3997 $$ = NEW_ITER($4, $5, &@$);
3998 /*% %*/
3999 /*% ripper: brace_block!(escape_Qundef($4), $5) %*/
4000 numparam_pop(p, $<node>3);
4001 dyna_pop(p, $<vars>1);
4002 }
4003 ;
4004
4005do_body : {$<vars>$ = dyna_push(p);}
4006 {
4007 $<num>$ = p->max_numparam;
4008 p->max_numparam = 0;
4009 }
4010 {
4011 $<node>$ = numparam_push(p);
4012 CMDARG_PUSH(0);
4013 }
4014 opt_block_param bodystmt
4015 {
4016 int max_numparam = p->max_numparam;
4017 p->max_numparam = $<num>2;
4018 $4 = args_with_numbered(p, $4, max_numparam);
4019 /*%%%*/
4020 $$ = NEW_ITER($4, $5, &@$);
4021 /*% %*/
4022 /*% ripper: do_block!(escape_Qundef($4), $5) %*/
4023 CMDARG_POP();
4024 numparam_pop(p, $<node>3);
4025 dyna_pop(p, $<vars>1);
4026 }
4027 ;
4028
4029case_args : arg_value
4030 {
4031 /*%%%*/
4032 check_literal_when(p, $1, &@1);
4033 $$ = NEW_LIST($1, &@$);
4034 /*% %*/
4035 /*% ripper: args_add!(args_new!, $1) %*/
4036 }
4037 | tSTAR arg_value
4038 {
4039 /*%%%*/
4040 $$ = NEW_SPLAT($2, &@$);
4041 /*% %*/
4042 /*% ripper: args_add_star!(args_new!, $2) %*/
4043 }
4044 | case_args ',' arg_value
4045 {
4046 /*%%%*/
4047 check_literal_when(p, $3, &@3);
4048 $$ = last_arg_append(p, $1, $3, &@$);
4049 /*% %*/
4050 /*% ripper: args_add!($1, $3) %*/
4051 }
4052 | case_args ',' tSTAR arg_value
4053 {
4054 /*%%%*/
4055 $$ = rest_arg_append(p, $1, $4, &@$);
4056 /*% %*/
4057 /*% ripper: args_add_star!($1, $4) %*/
4058 }
4059 ;
4060
4061case_body : k_when case_args then
4062 compstmt
4063 cases
4064 {
4065 /*%%%*/
4066 $$ = NEW_WHEN($2, $4, $5, &@$);
4067 fixpos($$, $2);
4068 /*% %*/
4069 /*% ripper: when!($2, $4, escape_Qundef($5)) %*/
4070 }
4071 ;
4072
4073cases : opt_else
4074 | case_body
4075 ;
4076
4077p_case_body : keyword_in
4078 {
4079 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
4080 p->command_start = FALSE;
4081 $<ctxt>1 = p->ctxt;
4082 p->ctxt.in_kwarg = 1;
4083 $<tbl>$ = push_pvtbl(p);
4084 }
4085 {
4086 $<tbl>$ = push_pktbl(p);
4087 }
4088 p_top_expr then
4089 {
4090 pop_pktbl(p, $<tbl>3);
4091 pop_pvtbl(p, $<tbl>2);
4092 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4093 }
4094 compstmt
4095 p_cases
4096 {
4097 /*%%%*/
4098 $$ = NEW_IN($4, $7, $8, &@$);
4099 /*% %*/
4100 /*% ripper: in!($4, $7, escape_Qundef($8)) %*/
4101 }
4102 ;
4103
4104p_cases : opt_else
4105 | p_case_body
4106 ;
4107
4108p_top_expr : p_top_expr_body
4109 | p_top_expr_body modifier_if expr_value
4110 {
4111 /*%%%*/
4112 $$ = new_if(p, $3, $1, 0, &@$);
4113 fixpos($$, $3);
4114 /*% %*/
4115 /*% ripper: if_mod!($3, $1) %*/
4116 }
4117 | p_top_expr_body modifier_unless expr_value
4118 {
4119 /*%%%*/
4120 $$ = new_unless(p, $3, $1, 0, &@$);
4121 fixpos($$, $3);
4122 /*% %*/
4123 /*% ripper: unless_mod!($3, $1) %*/
4124 }
4125 ;
4126
4127p_top_expr_body : p_expr
4128 | p_expr ','
4129 {
4130 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4131 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
4132 }
4133 | p_expr ',' p_args
4134 {
4135 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
4136 /*%%%*/
4137 nd_set_first_loc($$, @1.beg_pos);
4138 /*%
4139 %*/
4140 }
4141 | p_find
4142 {
4143 $$ = new_find_pattern(p, Qnone, $1, &@$);
4144 }
4145 | p_args_tail
4146 {
4147 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
4148 }
4149 | p_kwargs
4150 {
4151 $$ = new_hash_pattern(p, Qnone, $1, &@$);
4152 }
4153 ;
4154
4155p_expr : p_as
4156 ;
4157
4158p_as : p_expr tASSOC p_variable
4159 {
4160 /*%%%*/
4161 NODE *n = NEW_LIST($1, &@$);
4162 n = list_append(p, n, $3);
4163 $$ = new_hash(p, n, &@$);
4164 /*% %*/
4165 /*% ripper: binary!($1, STATIC_ID2SYM((id_assoc)), $3) %*/
4166 }
4167 | p_alt
4168 ;
4169
4170p_alt : p_alt '|' p_expr_basic
4171 {
4172 /*%%%*/
4173 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
4174 /*% %*/
4175 /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
4176 }
4177 | p_expr_basic
4178 ;
4179
4180p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
4181p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
4182
4183p_expr_basic : p_value
4184 | p_variable
4185 | p_const p_lparen p_args rparen
4186 {
4187 pop_pktbl(p, $<tbl>2);
4188 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4189 /*%%%*/
4190 nd_set_first_loc($$, @1.beg_pos);
4191 /*%
4192 %*/
4193 }
4194 | p_const p_lparen p_find rparen
4195 {
4196 pop_pktbl(p, $<tbl>2);
4197 $$ = new_find_pattern(p, $1, $3, &@$);
4198 /*%%%*/
4199 nd_set_first_loc($$, @1.beg_pos);
4200 /*%
4201 %*/
4202 }
4203 | p_const p_lparen p_kwargs rparen
4204 {
4205 pop_pktbl(p, $<tbl>2);
4206 $$ = new_hash_pattern(p, $1, $3, &@$);
4207 /*%%%*/
4208 nd_set_first_loc($$, @1.beg_pos);
4209 /*%
4210 %*/
4211 }
4212 | p_const '(' rparen
4213 {
4214 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4215 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4216 }
4217 | p_const p_lbracket p_args rbracket
4218 {
4219 pop_pktbl(p, $<tbl>2);
4220 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4221 /*%%%*/
4222 nd_set_first_loc($$, @1.beg_pos);
4223 /*%
4224 %*/
4225 }
4226 | p_const p_lbracket p_find rbracket
4227 {
4228 pop_pktbl(p, $<tbl>2);
4229 $$ = new_find_pattern(p, $1, $3, &@$);
4230 /*%%%*/
4231 nd_set_first_loc($$, @1.beg_pos);
4232 /*%
4233 %*/
4234 }
4235 | p_const p_lbracket p_kwargs rbracket
4236 {
4237 pop_pktbl(p, $<tbl>2);
4238 $$ = new_hash_pattern(p, $1, $3, &@$);
4239 /*%%%*/
4240 nd_set_first_loc($$, @1.beg_pos);
4241 /*%
4242 %*/
4243 }
4244 | p_const '[' rbracket
4245 {
4246 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4247 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4248 }
4249 | tLBRACK p_args rbracket
4250 {
4251 $$ = new_array_pattern(p, Qnone, Qnone, $2, &@$);
4252 }
4253 | tLBRACK p_find rbracket
4254 {
4255 $$ = new_find_pattern(p, Qnone, $2, &@$);
4256 }
4257 | tLBRACK rbracket
4258 {
4259 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4260 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
4261 }
4262 | tLBRACE
4263 {
4264 $<tbl>$ = push_pktbl(p);
4265 $<ctxt>1 = p->ctxt;
4266 p->ctxt.in_kwarg = 0;
4267 }
4268 p_kwargs rbrace
4269 {
4270 pop_pktbl(p, $<tbl>2);
4271 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4272 $$ = new_hash_pattern(p, Qnone, $3, &@$);
4273 }
4274 | tLBRACE rbrace
4275 {
4276 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
4277 $$ = new_hash_pattern(p, Qnone, $$, &@$);
4278 }
4279 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
4280 {
4281 pop_pktbl(p, $<tbl>2);
4282 $$ = $3;
4283 }
4284 ;
4285
4286p_args : p_expr
4287 {
4288 /*%%%*/
4289 NODE *pre_args = NEW_LIST($1, &@$);
4290 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4291 /*%
4292 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
4293 %*/
4294 }
4295 | p_args_head
4296 {
4297 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4298 }
4299 | p_args_head p_arg
4300 {
4301 /*%%%*/
4302 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
4303 /*%
4304 VALUE pre_args = rb_ary_concat($1, get_value($2));
4305 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4306 %*/
4307 }
4308 | p_args_head tSTAR tIDENTIFIER
4309 {
4310 $$ = new_array_pattern_tail(p, $1, 1, $3, Qnone, &@$);
4311 }
4312 | p_args_head tSTAR tIDENTIFIER ',' p_args_post
4313 {
4314 $$ = new_array_pattern_tail(p, $1, 1, $3, $5, &@$);
4315 }
4316 | p_args_head tSTAR
4317 {
4318 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4319 }
4320 | p_args_head tSTAR ',' p_args_post
4321 {
4322 $$ = new_array_pattern_tail(p, $1, 1, 0, $4, &@$);
4323 }
4324 | p_args_tail
4325 ;
4326
4327p_args_head : p_arg ','
4328 {
4329 $$ = $1;
4330 }
4331 | p_args_head p_arg ','
4332 {
4333 /*%%%*/
4334 $$ = list_concat($1, $2);
4335 /*% %*/
4336 /*% ripper: rb_ary_concat($1, get_value($2)) %*/
4337 }
4338 ;
4339
4340p_args_tail : p_rest
4341 {
4342 $$ = new_array_pattern_tail(p, Qnone, 1, $1, Qnone, &@$);
4343 }
4344 | p_rest ',' p_args_post
4345 {
4346 $$ = new_array_pattern_tail(p, Qnone, 1, $1, $3, &@$);
4347 }
4348 ;
4349
4350p_find : p_rest ',' p_args_post ',' p_rest
4351 {
4352 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
4353
4354 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL))
4355 rb_warn0L_experimental(nd_line($$), "Find pattern is experimental, and the behavior may change in future versions of Ruby!");
4356 }
4357 ;
4358
4359
4360p_rest : tSTAR tIDENTIFIER
4361 {
4362 $$ = $2;
4363 }
4364 | tSTAR
4365 {
4366 $$ = 0;
4367 }
4368 ;
4369
4370p_args_post : p_arg
4371 | p_args_post ',' p_arg
4372 {
4373 /*%%%*/
4374 $$ = list_concat($1, $3);
4375 /*% %*/
4376 /*% ripper: rb_ary_concat($1, get_value($3)) %*/
4377 }
4378 ;
4379
4380p_arg : p_expr
4381 {
4382 /*%%%*/
4383 $$ = NEW_LIST($1, &@$);
4384 /*% %*/
4385 /*% ripper: rb_ary_new_from_args(1, get_value($1)) %*/
4386 }
4387 ;
4388
4389p_kwargs : p_kwarg ',' p_any_kwrest
4390 {
4391 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4392 }
4393 | p_kwarg
4394 {
4395 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4396 }
4397 | p_kwarg ','
4398 {
4399 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4400 }
4401 | p_any_kwrest
4402 {
4403 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4404 }
4405 ;
4406
4407p_kwarg : p_kw
4408 /*% ripper[brace]: rb_ary_new_from_args(1, $1) %*/
4409 | p_kwarg ',' p_kw
4410 {
4411 /*%%%*/
4412 $$ = list_concat($1, $3);
4413 /*% %*/
4414 /*% ripper: rb_ary_push($1, $3) %*/
4415 }
4416 ;
4417
4418p_kw : p_kw_label p_expr
4419 {
4420 error_duplicate_pattern_key(p, get_id($1), &@1);
4421 /*%%%*/
4422 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), $2);
4423 /*% %*/
4424 /*% ripper: rb_ary_new_from_args(2, get_value($1), get_value($2)) %*/
4425 }
4426 | p_kw_label
4427 {
4428 error_duplicate_pattern_key(p, get_id($1), &@1);
4429 if ($1 && !is_local_id(get_id($1))) {
4430 yyerror1(&@1, "key must be valid as local variables");
4431 }
4432 error_duplicate_pattern_variable(p, get_id($1), &@1);
4433 /*%%%*/
4434 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4435 /*% %*/
4436 /*% ripper: rb_ary_new_from_args(2, get_value($1), Qnil) %*/
4437 }
4438 ;
4439
4440p_kw_label : tLABEL
4441 | tSTRING_BEG string_contents tLABEL_END
4442 {
4443 YYLTYPE loc = code_loc_gen(&@1, &@3);
4444 /*%%%*/
4445 if (!$2 || nd_type_p($2, NODE_STR)) {
4446 NODE *node = dsym_node(p, $2, &loc);
4447 $$ = SYM2ID(node->nd_lit);
4448 }
4449 /*%
4450 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4451 VALUE label = RNODE($2)->nd_cval;
4452 VALUE rval = RNODE($2)->nd_rval;
4453 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4454 RNODE($$)->nd_loc = loc;
4455 }
4456 %*/
4457 else {
4458 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4459 $$ = 0;
4460 }
4461 }
4462 ;
4463
4464p_kwrest : kwrest_mark tIDENTIFIER
4465 {
4466 $$ = $2;
4467 }
4468 | kwrest_mark
4469 {
4470 $$ = 0;
4471 }
4472 ;
4473
4474p_kwnorest : kwrest_mark keyword_nil
4475 {
4476 $$ = 0;
4477 }
4478 ;
4479
4480p_any_kwrest : p_kwrest
4481 | p_kwnorest {$$ = ID2VAL(idNil);}
4482 ;
4483
4484p_value : p_primitive
4485 | p_primitive tDOT2 p_primitive
4486 {
4487 /*%%%*/
4488 value_expr($1);
4489 value_expr($3);
4490 $$ = NEW_DOT2($1, $3, &@$);
4491 /*% %*/
4492 /*% ripper: dot2!($1, $3) %*/
4493 }
4494 | p_primitive tDOT3 p_primitive
4495 {
4496 /*%%%*/
4497 value_expr($1);
4498 value_expr($3);
4499 $$ = NEW_DOT3($1, $3, &@$);
4500 /*% %*/
4501 /*% ripper: dot3!($1, $3) %*/
4502 }
4503 | p_primitive tDOT2
4504 {
4505 /*%%%*/
4506 value_expr($1);
4507 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
4508 /*% %*/
4509 /*% ripper: dot2!($1, Qnil) %*/
4510 }
4511 | p_primitive tDOT3
4512 {
4513 /*%%%*/
4514 value_expr($1);
4515 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
4516 /*% %*/
4517 /*% ripper: dot3!($1, Qnil) %*/
4518 }
4519 | p_var_ref
4520 | p_expr_ref
4521 | p_const
4522 | tBDOT2 p_primitive
4523 {
4524 /*%%%*/
4525 value_expr($2);
4526 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
4527 /*% %*/
4528 /*% ripper: dot2!(Qnil, $2) %*/
4529 }
4530 | tBDOT3 p_primitive
4531 {
4532 /*%%%*/
4533 value_expr($2);
4534 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
4535 /*% %*/
4536 /*% ripper: dot3!(Qnil, $2) %*/
4537 }
4538 ;
4539
4540p_primitive : literal
4541 | strings
4542 | xstring
4543 | regexp
4544 | words
4545 | qwords
4546 | symbols
4547 | qsymbols
4548 | keyword_variable
4549 {
4550 /*%%%*/
4551 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4552 /*% %*/
4553 /*% ripper: var_ref!($1) %*/
4554 }
4555 | lambda
4556 ;
4557
4558p_variable : tIDENTIFIER
4559 {
4560 /*%%%*/
4561 error_duplicate_pattern_variable(p, $1, &@1);
4562 $$ = assignable(p, $1, 0, &@$);
4563 /*% %*/
4564 /*% ripper: assignable(p, var_field(p, $1)) %*/
4565 }
4566 ;
4567
4568p_var_ref : '^' tIDENTIFIER
4569 {
4570 /*%%%*/
4571 NODE *n = gettable(p, $2, &@$);
4572 if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
4573 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4574 }
4575 $$ = n;
4576 /*% %*/
4577 /*% ripper: var_ref!($2) %*/
4578 }
4579 | '^' nonlocal_var
4580 {
4581 /*%%%*/
4582 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_BEGIN(0, &@$);
4583 /*% %*/
4584 /*% ripper: var_ref!($2) %*/
4585 }
4586 ;
4587
4588p_expr_ref : '^' tLPAREN expr_value ')'
4589 {
4590 /*%%%*/
4591 $$ = NEW_BEGIN($3, &@$);
4592 /*% %*/
4593 /*% ripper: begin!($3) %*/
4594 }
4595 ;
4596
4597p_const : tCOLON3 cname
4598 {
4599 /*%%%*/
4600 $$ = NEW_COLON3($2, &@$);
4601 /*% %*/
4602 /*% ripper: top_const_ref!($2) %*/
4603 }
4604 | p_const tCOLON2 cname
4605 {
4606 /*%%%*/
4607 $$ = NEW_COLON2($1, $3, &@$);
4608 /*% %*/
4609 /*% ripper: const_path_ref!($1, $3) %*/
4610 }
4611 | tCONSTANT
4612 {
4613 /*%%%*/
4614 $$ = gettable(p, $1, &@$);
4615 /*% %*/
4616 /*% ripper: var_ref!($1) %*/
4617 }
4618 ;
4619
4620opt_rescue : k_rescue exc_list exc_var then
4621 compstmt
4622 opt_rescue
4623 {
4624 /*%%%*/
4625 $$ = NEW_RESBODY($2,
4626 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), NO_LEX_CTXT, &@3), $5) : $5,
4627 $6, &@$);
4628 fixpos($$, $2?$2:$5);
4629 /*% %*/
4630 /*% ripper: rescue!(escape_Qundef($2), escape_Qundef($3), escape_Qundef($5), escape_Qundef($6)) %*/
4631 }
4632 | none
4633 ;
4634
4635exc_list : arg_value
4636 {
4637 /*%%%*/
4638 $$ = NEW_LIST($1, &@$);
4639 /*% %*/
4640 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
4641 }
4642 | mrhs
4643 {
4644 /*%%%*/
4645 if (!($$ = splat_array($1))) $$ = $1;
4646 /*% %*/
4647 /*% ripper: $1 %*/
4648 }
4649 | none
4650 ;
4651
4652exc_var : tASSOC lhs
4653 {
4654 $$ = $2;
4655 }
4656 | none
4657 ;
4658
4659opt_ensure : k_ensure compstmt
4660 {
4661 /*%%%*/
4662 $$ = $2;
4663 /*% %*/
4664 /*% ripper: ensure!($2) %*/
4665 }
4666 | none
4667 ;
4668
4669literal : numeric
4670 | symbol
4671 ;
4672
4673strings : string
4674 {
4675 /*%%%*/
4676 NODE *node = $1;
4677 if (!node) {
4678 node = NEW_STR(STR_NEW0(), &@$);
4679 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
4680 }
4681 else {
4682 node = evstr2dstr(p, node);
4683 }
4684 $$ = node;
4685 /*% %*/
4686 /*% ripper: $1 %*/
4687 }
4688 ;
4689
4690string : tCHAR
4691 | string1
4692 | string string1
4693 {
4694 /*%%%*/
4695 $$ = literal_concat(p, $1, $2, &@$);
4696 /*% %*/
4697 /*% ripper: string_concat!($1, $2) %*/
4698 }
4699 ;
4700
4701string1 : tSTRING_BEG string_contents tSTRING_END
4702 {
4703 /*%%%*/
4704 $$ = heredoc_dedent(p, $2);
4705 if ($$) nd_set_loc($$, &@$);
4706 /*% %*/
4707 /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/
4708 }
4709 ;
4710
4711xstring : tXSTRING_BEG xstring_contents tSTRING_END
4712 {
4713 /*%%%*/
4714 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4715 /*% %*/
4716 /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/
4717 }
4718 ;
4719
4720regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4721 {
4722 $$ = new_regexp(p, $2, $3, &@$);
4723 }
4724 ;
4725
4726words : tWORDS_BEG ' ' word_list tSTRING_END
4727 {
4728 /*%%%*/
4729 $$ = make_list($3, &@$);
4730 /*% %*/
4731 /*% ripper: array!($3) %*/
4732 }
4733 ;
4734
4735word_list : /* none */
4736 {
4737 /*%%%*/
4738 $$ = 0;
4739 /*% %*/
4740 /*% ripper: words_new! %*/
4741 }
4742 | word_list word ' '
4743 {
4744 /*%%%*/
4745 $$ = list_append(p, $1, evstr2dstr(p, $2));
4746 /*% %*/
4747 /*% ripper: words_add!($1, $2) %*/
4748 }
4749 ;
4750
4751word : string_content
4752 /*% ripper[brace]: word_add!(word_new!, $1) %*/
4753 | word string_content
4754 {
4755 /*%%%*/
4756 $$ = literal_concat(p, $1, $2, &@$);
4757 /*% %*/
4758 /*% ripper: word_add!($1, $2) %*/
4759 }
4760 ;
4761
4762symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4763 {
4764 /*%%%*/
4765 $$ = make_list($3, &@$);
4766 /*% %*/
4767 /*% ripper: array!($3) %*/
4768 }
4769 ;
4770
4771symbol_list : /* none */
4772 {
4773 /*%%%*/
4774 $$ = 0;
4775 /*% %*/
4776 /*% ripper: symbols_new! %*/
4777 }
4778 | symbol_list word ' '
4779 {
4780 /*%%%*/
4781 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4782 /*% %*/
4783 /*% ripper: symbols_add!($1, $2) %*/
4784 }
4785 ;
4786
4787qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4788 {
4789 /*%%%*/
4790 $$ = make_list($3, &@$);
4791 /*% %*/
4792 /*% ripper: array!($3) %*/
4793 }
4794 ;
4795
4796qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4797 {
4798 /*%%%*/
4799 $$ = make_list($3, &@$);
4800 /*% %*/
4801 /*% ripper: array!($3) %*/
4802 }
4803 ;
4804
4805qword_list : /* none */
4806 {
4807 /*%%%*/
4808 $$ = 0;
4809 /*% %*/
4810 /*% ripper: qwords_new! %*/
4811 }
4812 | qword_list tSTRING_CONTENT ' '
4813 {
4814 /*%%%*/
4815 $$ = list_append(p, $1, $2);
4816 /*% %*/
4817 /*% ripper: qwords_add!($1, $2) %*/
4818 }
4819 ;
4820
4821qsym_list : /* none */
4822 {
4823 /*%%%*/
4824 $$ = 0;
4825 /*% %*/
4826 /*% ripper: qsymbols_new! %*/
4827 }
4828 | qsym_list tSTRING_CONTENT ' '
4829 {
4830 /*%%%*/
4831 $$ = symbol_append(p, $1, $2);
4832 /*% %*/
4833 /*% ripper: qsymbols_add!($1, $2) %*/
4834 }
4835 ;
4836
4837string_contents : /* none */
4838 {
4839 /*%%%*/
4840 $$ = 0;
4841 /*% %*/
4842 /*% ripper: string_content! %*/
4843 /*%%%*/
4844 /*%
4845 $$ = ripper_new_yylval(p, 0, $$, 0);
4846 %*/
4847 }
4848 | string_contents string_content
4849 {
4850 /*%%%*/
4851 $$ = literal_concat(p, $1, $2, &@$);
4852 /*% %*/
4853 /*% ripper: string_add!($1, $2) %*/
4854 /*%%%*/
4855 /*%
4856 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
4857 !RNODE($1)->nd_cval) {
4858 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
4859 RNODE($1)->nd_rval = add_mark_object(p, $$);
4860 $$ = $1;
4861 }
4862 %*/
4863 }
4864 ;
4865
4866xstring_contents: /* none */
4867 {
4868 /*%%%*/
4869 $$ = 0;
4870 /*% %*/
4871 /*% ripper: xstring_new! %*/
4872 }
4873 | xstring_contents string_content
4874 {
4875 /*%%%*/
4876 $$ = literal_concat(p, $1, $2, &@$);
4877 /*% %*/
4878 /*% ripper: xstring_add!($1, $2) %*/
4879 }
4880 ;
4881
4882regexp_contents: /* none */
4883 {
4884 /*%%%*/
4885 $$ = 0;
4886 /*% %*/
4887 /*% ripper: regexp_new! %*/
4888 /*%%%*/
4889 /*%
4890 $$ = ripper_new_yylval(p, 0, $$, 0);
4891 %*/
4892 }
4893 | regexp_contents string_content
4894 {
4895 /*%%%*/
4896 NODE *head = $1, *tail = $2;
4897 if (!head) {
4898 $$ = tail;
4899 }
4900 else if (!tail) {
4901 $$ = head;
4902 }
4903 else {
4904 switch (nd_type(head)) {
4905 case NODE_STR:
4906 nd_set_type(head, NODE_DSTR);
4907 break;
4908 case NODE_DSTR:
4909 break;
4910 default:
4911 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
4912 break;
4913 }
4914 $$ = list_append(p, head, tail);
4915 }
4916 /*%
4917 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4918 if (ripper_is_node_yylval(n1)) {
4919 s1 = RNODE(n1)->nd_cval;
4920 n1 = RNODE(n1)->nd_rval;
4921 }
4922 if (ripper_is_node_yylval(n2)) {
4923 s2 = RNODE(n2)->nd_cval;
4924 n2 = RNODE(n2)->nd_rval;
4925 }
4926 $$ = dispatch2(regexp_add, n1, n2);
4927 if (!s1 && s2) {
4928 $$ = ripper_new_yylval(p, 0, $$, s2);
4929 }
4930 %*/
4931 }
4932 ;
4933
4934string_content : tSTRING_CONTENT
4935 /*% ripper[brace]: ripper_new_yylval(p, 0, get_value($1), $1) %*/
4936 | tSTRING_DVAR
4937 {
4938 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
4939 $<strterm>$ = p->lex.strterm;
4940 p->lex.strterm = 0;
4941 SET_LEX_STATE(EXPR_BEG);
4942 }
4943 string_dvar
4944 {
4945 p->lex.strterm = $<strterm>2;
4946 /*%%%*/
4947 $$ = NEW_EVSTR($3, &@$);
4948 nd_set_line($$, @3.end_pos.lineno);
4949 /*% %*/
4950 /*% ripper: string_dvar!($3) %*/
4951 }
4952 | tSTRING_DBEG
4953 {
4954 CMDARG_PUSH(0);
4955 COND_PUSH(0);
4956 }
4957 {
4958 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
4959 $<strterm>$ = p->lex.strterm;
4960 p->lex.strterm = 0;
4961 }
4962 {
4963 $<num>$ = p->lex.state;
4964 SET_LEX_STATE(EXPR_BEG);
4965 }
4966 {
4967 $<num>$ = p->lex.brace_nest;
4968 p->lex.brace_nest = 0;
4969 }
4970 {
4971 $<num>$ = p->heredoc_indent;
4972 p->heredoc_indent = 0;
4973 }
4974 compstmt tSTRING_DEND
4975 {
4976 COND_POP();
4977 CMDARG_POP();
4978 p->lex.strterm = $<strterm>3;
4979 SET_LEX_STATE($<num>4);
4980 p->lex.brace_nest = $<num>5;
4981 p->heredoc_indent = $<num>6;
4982 p->heredoc_line_indent = -1;
4983 /*%%%*/
4984 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4985 $$ = new_evstr(p, $7, &@$);
4986 /*% %*/
4987 /*% ripper: string_embexpr!($7) %*/
4988 }
4989 ;
4990
4991string_dvar : tGVAR
4992 {
4993 /*%%%*/
4994 $$ = NEW_GVAR($1, &@$);
4995 /*% %*/
4996 /*% ripper: var_ref!($1) %*/
4997 }
4998 | tIVAR
4999 {
5000 /*%%%*/
5001 $$ = NEW_IVAR($1, &@$);
5002 /*% %*/
5003 /*% ripper: var_ref!($1) %*/
5004 }
5005 | tCVAR
5006 {
5007 /*%%%*/
5008 $$ = NEW_CVAR($1, &@$);
5009 /*% %*/
5010 /*% ripper: var_ref!($1) %*/
5011 }
5012 | backref
5013 ;
5014
5015symbol : ssym
5016 | dsym
5017 ;
5018
5019ssym : tSYMBEG sym
5020 {
5021 SET_LEX_STATE(EXPR_END);
5022 /*%%%*/
5023 $$ = NEW_LIT(ID2SYM($2), &@$);
5024 /*% %*/
5025 /*% ripper: symbol_literal!(symbol!($2)) %*/
5026 }
5027 ;
5028
5029sym : fname
5030 | tIVAR
5031 | tGVAR
5032 | tCVAR
5033 ;
5034
5035dsym : tSYMBEG string_contents tSTRING_END
5036 {
5037 SET_LEX_STATE(EXPR_END);
5038 /*%%%*/
5039 $$ = dsym_node(p, $2, &@$);
5040 /*% %*/
5041 /*% ripper: dyna_symbol!($2) %*/
5042 }
5043 ;
5044
5045numeric : simple_numeric
5046 | tUMINUS_NUM simple_numeric %prec tLOWEST
5047 {
5048 /*%%%*/
5049 $$ = $2;
5050 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
5051 /*% %*/
5052 /*% ripper: unary!(ID2VAL(idUMinus), $2) %*/
5053 }
5054 ;
5055
5056simple_numeric : tINTEGER
5057 | tFLOAT
5058 | tRATIONAL
5059 | tIMAGINARY
5060 ;
5061
5062nonlocal_var : tIVAR
5063 | tGVAR
5064 | tCVAR
5065 ;
5066
5067user_variable : tIDENTIFIER
5068 | tIVAR
5069 | tGVAR
5070 | tCONSTANT
5071 | tCVAR
5072 ;
5073
5074keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
5075 | keyword_self {$$ = KWD2EID(self, $1);}
5076 | keyword_true {$$ = KWD2EID(true, $1);}
5077 | keyword_false {$$ = KWD2EID(false, $1);}
5078 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
5079 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
5080 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
5081 ;
5082
5083var_ref : user_variable
5084 {
5085 /*%%%*/
5086 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5087 /*%
5088 if (id_is_var(p, get_id($1))) {
5089 $$ = dispatch1(var_ref, $1);
5090 }
5091 else {
5092 $$ = dispatch1(vcall, $1);
5093 }
5094 %*/
5095 }
5096 | keyword_variable
5097 {
5098 /*%%%*/
5099 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5100 /*% %*/
5101 /*% ripper: var_ref!($1) %*/
5102 }
5103 ;
5104
5105var_lhs : user_variable
5106 {
5107 /*%%%*/
5108 $$ = assignable(p, $1, 0, &@$);
5109 /*% %*/
5110 /*% ripper: assignable(p, var_field(p, $1)) %*/
5111 }
5112 | keyword_variable
5113 {
5114 /*%%%*/
5115 $$ = assignable(p, $1, 0, &@$);
5116 /*% %*/
5117 /*% ripper: assignable(p, var_field(p, $1)) %*/
5118 }
5119 ;
5120
5121backref : tNTH_REF
5122 | tBACK_REF
5123 ;
5124
5125superclass : '<'
5126 {
5127 SET_LEX_STATE(EXPR_BEG);
5128 p->command_start = TRUE;
5129 }
5130 expr_value term
5131 {
5132 $$ = $3;
5133 }
5134 | /* none */
5135 {
5136 /*%%%*/
5137 $$ = 0;
5138 /*% %*/
5139 /*% ripper: Qnil %*/
5140 }
5141 ;
5142
5143f_opt_paren_args: f_paren_args
5144 | none
5145 {
5146 p->ctxt.in_argdef = 0;
5147 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5148 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5149 }
5150 ;
5151
5152f_paren_args : '(' f_args rparen
5153 {
5154 /*%%%*/
5155 $$ = $2;
5156 /*% %*/
5157 /*% ripper: paren!($2) %*/
5158 SET_LEX_STATE(EXPR_BEG);
5159 p->command_start = TRUE;
5160 p->ctxt.in_argdef = 0;
5161 }
5162 ;
5163
5164f_arglist : f_paren_args
5165 | {
5166 $<ctxt>$ = p->ctxt;
5167 p->ctxt.in_kwarg = 1;
5168 p->ctxt.in_argdef = 1;
5169 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
5170 }
5171 f_args term
5172 {
5173 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
5174 p->ctxt.in_argdef = 0;
5175 $$ = $2;
5176 SET_LEX_STATE(EXPR_BEG);
5177 p->command_start = TRUE;
5178 }
5179 ;
5180
5181args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
5182 {
5183 $$ = new_args_tail(p, $1, $3, $4, &@3);
5184 }
5185 | f_kwarg opt_f_block_arg
5186 {
5187 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
5188 }
5189 | f_any_kwrest opt_f_block_arg
5190 {
5191 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
5192 }
5193 | f_block_arg
5194 {
5195 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
5196 }
5197 | args_forward
5198 {
5199 add_forwarding_args(p);
5200 $$ = new_args_tail(p, Qnone, $1, ID2VAL(idFWD_BLOCK), &@1);
5201 }
5202 ;
5203
5204opt_args_tail : ',' args_tail
5205 {
5206 $$ = $2;
5207 }
5208 | /* none */
5209 {
5210 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5211 }
5212 ;
5213
5214f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
5215 {
5216 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
5217 }
5218 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5219 {
5220 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
5221 }
5222 | f_arg ',' f_optarg opt_args_tail
5223 {
5224 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
5225 }
5226 | f_arg ',' f_optarg ',' f_arg opt_args_tail
5227 {
5228 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
5229 }
5230 | f_arg ',' f_rest_arg opt_args_tail
5231 {
5232 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
5233 }
5234 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
5235 {
5236 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
5237 }
5238 | f_arg opt_args_tail
5239 {
5240 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
5241 }
5242 | f_optarg ',' f_rest_arg opt_args_tail
5243 {
5244 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
5245 }
5246 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5247 {
5248 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
5249 }
5250 | f_optarg opt_args_tail
5251 {
5252 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
5253 }
5254 | f_optarg ',' f_arg opt_args_tail
5255 {
5256 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
5257 }
5258 | f_rest_arg opt_args_tail
5259 {
5260 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
5261 }
5262 | f_rest_arg ',' f_arg opt_args_tail
5263 {
5264 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
5265 }
5266 | args_tail
5267 {
5268 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
5269 }
5270 | /* none */
5271 {
5272 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5273 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5274 }
5275 ;
5276
5277args_forward : tBDOT3
5278 {
5279 /*%%%*/
5280 $$ = idFWD_KWREST;
5281 /*% %*/
5282 /*% ripper: args_forward! %*/
5283 }
5284 ;
5285
5286f_bad_arg : tCONSTANT
5287 {
5288 static const char mesg[] = "formal argument cannot be a constant";
5289 /*%%%*/
5290 yyerror1(&@1, mesg);
5291 $$ = 0;
5292 /*% %*/
5293 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5294 }
5295 | tIVAR
5296 {
5297 static const char mesg[] = "formal argument cannot be an instance variable";
5298 /*%%%*/
5299 yyerror1(&@1, mesg);
5300 $$ = 0;
5301 /*% %*/
5302 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5303 }
5304 | tGVAR
5305 {
5306 static const char mesg[] = "formal argument cannot be a global variable";
5307 /*%%%*/
5308 yyerror1(&@1, mesg);
5309 $$ = 0;
5310 /*% %*/
5311 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5312 }
5313 | tCVAR
5314 {
5315 static const char mesg[] = "formal argument cannot be a class variable";
5316 /*%%%*/
5317 yyerror1(&@1, mesg);
5318 $$ = 0;
5319 /*% %*/
5320 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5321 }
5322 ;
5323
5324f_norm_arg : f_bad_arg
5325 | tIDENTIFIER
5326 {
5327 formal_argument(p, $1);
5328 p->max_numparam = ORDINAL_PARAM;
5329 $$ = $1;
5330 }
5331 ;
5332
5333f_arg_asgn : f_norm_arg
5334 {
5335 ID id = get_id($1);
5336 arg_var(p, id);
5337 p->cur_arg = id;
5338 $$ = $1;
5339 }
5340 ;
5341
5342f_arg_item : f_arg_asgn
5343 {
5344 p->cur_arg = 0;
5345 /*%%%*/
5346 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5347 /*% %*/
5348 /*% ripper: get_value($1) %*/
5349 }
5350 | tLPAREN f_margs rparen
5351 {
5352 /*%%%*/
5353 ID tid = internal_id(p);
5354 YYLTYPE loc;
5355 loc.beg_pos = @2.beg_pos;
5356 loc.end_pos = @2.beg_pos;
5357 arg_var(p, tid);
5358 if (dyna_in_block(p)) {
5359 $2->nd_value = NEW_DVAR(tid, &loc);
5360 }
5361 else {
5362 $2->nd_value = NEW_LVAR(tid, &loc);
5363 }
5364 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5365 $$->nd_next = $2;
5366 /*% %*/
5367 /*% ripper: mlhs_paren!($2) %*/
5368 }
5369 ;
5370
5371f_arg : f_arg_item
5372 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5373 | f_arg ',' f_arg_item
5374 {
5375 /*%%%*/
5376 $$ = $1;
5377 $$->nd_plen++;
5378 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5379 rb_discard_node(p, $3);
5380 /*% %*/
5381 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5382 }
5383 ;
5384
5385
5386f_label : tLABEL
5387 {
5388 arg_var(p, formal_argument(p, $1));
5389 p->cur_arg = get_id($1);
5390 p->max_numparam = ORDINAL_PARAM;
5391 p->ctxt.in_argdef = 0;
5392 $$ = $1;
5393 }
5394 ;
5395
5396f_kw : f_label arg_value
5397 {
5398 p->cur_arg = 0;
5399 p->ctxt.in_argdef = 1;
5400 /*%%%*/
5401 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5402 /*% %*/
5403 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5404 }
5405 | f_label
5406 {
5407 p->cur_arg = 0;
5408 p->ctxt.in_argdef = 1;
5409 /*%%%*/
5410 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5411 /*% %*/
5412 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5413 }
5414 ;
5415
5416f_block_kw : f_label primary_value
5417 {
5418 p->ctxt.in_argdef = 1;
5419 /*%%%*/
5420 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5421 /*% %*/
5422 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5423 }
5424 | f_label
5425 {
5426 p->ctxt.in_argdef = 1;
5427 /*%%%*/
5428 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5429 /*% %*/
5430 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5431 }
5432 ;
5433
5434f_block_kwarg : f_block_kw
5435 {
5436 /*%%%*/
5437 $$ = $1;
5438 /*% %*/
5439 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5440 }
5441 | f_block_kwarg ',' f_block_kw
5442 {
5443 /*%%%*/
5444 $$ = kwd_append($1, $3);
5445 /*% %*/
5446 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5447 }
5448 ;
5449
5450
5451f_kwarg : f_kw
5452 {
5453 /*%%%*/
5454 $$ = $1;
5455 /*% %*/
5456 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5457 }
5458 | f_kwarg ',' f_kw
5459 {
5460 /*%%%*/
5461 $$ = kwd_append($1, $3);
5462 /*% %*/
5463 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5464 }
5465 ;
5466
5467kwrest_mark : tPOW
5468 | tDSTAR
5469 ;
5470
5471f_no_kwarg : kwrest_mark keyword_nil
5472 {
5473 /*%%%*/
5474 /*% %*/
5475 /*% ripper: nokw_param!(Qnil) %*/
5476 }
5477 ;
5478
5479f_kwrest : kwrest_mark tIDENTIFIER
5480 {
5481 arg_var(p, shadowing_lvar(p, get_id($2)));
5482 /*%%%*/
5483 $$ = $2;
5484 /*% %*/
5485 /*% ripper: kwrest_param!($2) %*/
5486 }
5487 | kwrest_mark
5488 {
5489 /*%%%*/
5490 $$ = internal_id(p);
5491 arg_var(p, $$);
5492 /*% %*/
5493 /*% ripper: kwrest_param!(Qnil) %*/
5494 }
5495 ;
5496
5497f_opt : f_arg_asgn f_eq arg_value
5498 {
5499 p->cur_arg = 0;
5500 p->ctxt.in_argdef = 1;
5501 /*%%%*/
5502 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5503 /*% %*/
5504 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5505 }
5506 ;
5507
5508f_block_opt : f_arg_asgn f_eq primary_value
5509 {
5510 p->cur_arg = 0;
5511 p->ctxt.in_argdef = 1;
5512 /*%%%*/
5513 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5514 /*% %*/
5515 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5516 }
5517 ;
5518
5519f_block_optarg : f_block_opt
5520 {
5521 /*%%%*/
5522 $$ = $1;
5523 /*% %*/
5524 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5525 }
5526 | f_block_optarg ',' f_block_opt
5527 {
5528 /*%%%*/
5529 $$ = opt_arg_append($1, $3);
5530 /*% %*/
5531 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5532 }
5533 ;
5534
5535f_optarg : f_opt
5536 {
5537 /*%%%*/
5538 $$ = $1;
5539 /*% %*/
5540 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5541 }
5542 | f_optarg ',' f_opt
5543 {
5544 /*%%%*/
5545 $$ = opt_arg_append($1, $3);
5546 /*% %*/
5547 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5548 }
5549 ;
5550
5551restarg_mark : '*'
5552 | tSTAR
5553 ;
5554
5555f_rest_arg : restarg_mark tIDENTIFIER
5556 {
5557 arg_var(p, shadowing_lvar(p, get_id($2)));
5558 /*%%%*/
5559 $$ = $2;
5560 /*% %*/
5561 /*% ripper: rest_param!($2) %*/
5562 }
5563 | restarg_mark
5564 {
5565 /*%%%*/
5566 $$ = internal_id(p);
5567 arg_var(p, $$);
5568 /*% %*/
5569 /*% ripper: rest_param!(Qnil) %*/
5570 }
5571 ;
5572
5573blkarg_mark : '&'
5574 | tAMPER
5575 ;
5576
5577f_block_arg : blkarg_mark tIDENTIFIER
5578 {
5579 arg_var(p, shadowing_lvar(p, get_id($2)));
5580 /*%%%*/
5581 $$ = $2;
5582 /*% %*/
5583 /*% ripper: blockarg!($2) %*/
5584 }
5585 | blkarg_mark
5586 {
5587 /*%%%*/
5588 arg_var(p, shadowing_lvar(p, get_id(ANON_BLOCK_ID)));
5589 /*%
5590 $$ = dispatch1(blockarg, Qnil);
5591 %*/
5592 }
5593 ;
5594
5595opt_f_block_arg : ',' f_block_arg
5596 {
5597 $$ = $2;
5598 }
5599 | none
5600 {
5601 $$ = Qnull;
5602 }
5603 ;
5604
5605singleton : var_ref
5606 {
5607 value_expr($1);
5608 $$ = $1;
5609 }
5610 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5611 {
5612 /*%%%*/
5613 switch (nd_type($3)) {
5614 case NODE_STR:
5615 case NODE_DSTR:
5616 case NODE_XSTR:
5617 case NODE_DXSTR:
5618 case NODE_DREGX:
5619 case NODE_LIT:
5620 case NODE_LIST:
5621 case NODE_ZLIST:
5622 yyerror1(&@3, "can't define singleton method for literals");
5623 break;
5624 default:
5625 value_expr($3);
5626 break;
5627 }
5628 $$ = $3;
5629 /*% %*/
5630 /*% ripper: paren!($3) %*/
5631 }
5632 ;
5633
5634assoc_list : none
5635 | assocs trailer
5636 {
5637 /*%%%*/
5638 $$ = $1;
5639 /*% %*/
5640 /*% ripper: assoclist_from_args!($1) %*/
5641 }
5642 ;
5643
5644assocs : assoc
5645 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5646 | assocs ',' assoc
5647 {
5648 /*%%%*/
5649 NODE *assocs = $1;
5650 NODE *tail = $3;
5651 if (!assocs) {
5652 assocs = tail;
5653 }
5654 else if (tail) {
5655 if (assocs->nd_head &&
5656 !tail->nd_head && nd_type_p(tail->nd_next, NODE_LIST) &&
5657 nd_type_p(tail->nd_next->nd_head, NODE_HASH)) {
5658 /* DSTAR */
5659 tail = tail->nd_next->nd_head->nd_head;
5660 }
5661 assocs = list_concat(assocs, tail);
5662 }
5663 $$ = assocs;
5664 /*% %*/
5665 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5666 }
5667 ;
5668
5669assoc : arg_value tASSOC arg_value
5670 {
5671 /*%%%*/
5672 if (nd_type_p($1, NODE_STR)) {
5673 nd_set_type($1, NODE_LIT);
5674 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
5675 }
5676 $$ = list_append(p, NEW_LIST($1, &@$), $3);
5677 /*% %*/
5678 /*% ripper: assoc_new!($1, $3) %*/
5679 }
5680 | tLABEL arg_value
5681 {
5682 /*%%%*/
5683 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5684 /*% %*/
5685 /*% ripper: assoc_new!($1, $2) %*/
5686 }
5687 | tLABEL
5688 {
5689 /*%%%*/
5690 NODE *val = gettable(p, $1, &@$);
5691 if (!val) val = NEW_BEGIN(0, &@$);
5692 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), val);
5693 /*% %*/
5694 /*% ripper: assoc_new!($1, Qnil) %*/
5695 }
5696 | tSTRING_BEG string_contents tLABEL_END arg_value
5697 {
5698 /*%%%*/
5699 YYLTYPE loc = code_loc_gen(&@1, &@3);
5700 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
5701 /*% %*/
5702 /*% ripper: assoc_new!(dyna_symbol!($2), $4) %*/
5703 }
5704 | tDSTAR arg_value
5705 {
5706 /*%%%*/
5707 if (nd_type_p($2, NODE_HASH) &&
5708 !($2->nd_head && $2->nd_head->nd_alen)) {
5709 static VALUE empty_hash;
5710 if (!empty_hash) {
5711 empty_hash = rb_obj_freeze(rb_hash_new());
5712 rb_gc_register_mark_object(empty_hash);
5713 }
5714 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
5715 }
5716 else
5717 $$ = list_append(p, NEW_LIST(0, &@$), $2);
5718 /*% %*/
5719 /*% ripper: assoc_splat!($2) %*/
5720 }
5721 ;
5722
5723operation : tIDENTIFIER
5724 | tCONSTANT
5725 | tFID
5726 ;
5727
5728operation2 : tIDENTIFIER
5729 | tCONSTANT
5730 | tFID
5731 | op
5732 ;
5733
5734operation3 : tIDENTIFIER
5735 | tFID
5736 | op
5737 ;
5738
5739dot_or_colon : '.'
5740 | tCOLON2
5741 ;
5742
5743call_op : '.'
5744 | tANDDOT
5745 ;
5746
5747call_op2 : call_op
5748 | tCOLON2
5749 ;
5750
5751opt_terms : /* none */
5752 | terms
5753 ;
5754
5755opt_nl : /* none */
5756 | '\n'
5757 ;
5758
5759rparen : opt_nl ')'
5760 ;
5761
5762rbracket : opt_nl ']'
5763 ;
5764
5765rbrace : opt_nl '}'
5766 ;
5767
5768trailer : /* none */
5769 | '\n'
5770 | ','
5771 ;
5772
5773term : ';' {yyerrok;token_flush(p);}
5774 | '\n' {token_flush(p);}
5775 ;
5776
5777terms : term
5778 | terms ';' {yyerrok;}
5779 ;
5780
5781none : /* none */
5782 {
5783 $$ = Qnull;
5784 }
5785 ;
5786%%
5787# undef p
5788# undef yylex
5789# undef yylval
5790# define yylval (*p->lval)
5791
5792static int regx_options(struct parser_params*);
5793static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5794static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5795static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5796static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
5797
5798#ifndef RIPPER
5799# define set_yylval_node(x) { \
5800 YYLTYPE _cur_loc; \
5801 rb_parser_set_location(p, &_cur_loc); \
5802 yylval.node = (x); \
5803}
5804# define set_yylval_str(x) \
5805do { \
5806 set_yylval_node(NEW_STR(x, &_cur_loc)); \
5807 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5808} while(0)
5809# define set_yylval_literal(x) \
5810do { \
5811 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
5812 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5813} while(0)
5814# define set_yylval_num(x) (yylval.num = (x))
5815# define set_yylval_id(x) (yylval.id = (x))
5816# define set_yylval_name(x) (yylval.id = (x))
5817# define yylval_id() (yylval.id)
5818#else
5819static inline VALUE
5820ripper_yylval_id(struct parser_params *p, ID x)
5821{
5822 return ripper_new_yylval(p, x, ID2SYM(x), 0);
5823}
5824# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
5825# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
5826# define set_yylval_id(x) (void)(x)
5827# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
5828# define set_yylval_literal(x) add_mark_object(p, (x))
5829# define set_yylval_node(x) (yylval.val = ripper_new_yylval(p, 0, 0, STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)))
5830# define yylval_id() yylval.id
5831# define _cur_loc NULL_LOC /* dummy */
5832#endif
5833
5834#define set_yylval_noname() set_yylval_id(keyword_nil)
5835
5836#ifndef RIPPER
5837#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
5838#define dispatch_scan_event(p, t) ((void)0)
5839#define dispatch_delayed_token(p, t) ((void)0)
5840#define has_delayed_token(p) (0)
5841#else
5842#define literal_flush(p, ptr) ((void)(ptr))
5843
5844#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5845
5846static inline VALUE
5847intern_sym(const char *name)
5848{
5849 ID id = rb_intern_const(name);
5850 return ID2SYM(id);
5851}
5852
5853static int
5854ripper_has_scan_event(struct parser_params *p)
5855{
5856 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
5857 return p->lex.pcur > p->lex.ptok;
5858}
5859
5860static VALUE
5861ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
5862{
5863 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
5864 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
5865 token_flush(p);
5866 return rval;
5867}
5868
5869static void
5870ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
5871{
5872 if (!ripper_has_scan_event(p)) return;
5873 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
5874}
5875#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
5876
5877static void
5878ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
5879{
5880 int saved_line = p->ruby_sourceline;
5881 const char *saved_tokp = p->lex.ptok;
5882
5883 if (NIL_P(p->delayed.token)) return;
5884 p->ruby_sourceline = p->delayed.line;
5885 p->lex.ptok = p->lex.pbeg + p->delayed.col;
5886 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
5887 p->delayed.token = Qnil;
5888 p->ruby_sourceline = saved_line;
5889 p->lex.ptok = saved_tokp;
5890}
5891#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
5892#define has_delayed_token(p) (!NIL_P(p->delayed.token))
5893#endif /* RIPPER */
5894
5895static inline int
5896is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
5897{
5898 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
5899}
5900
5901static inline int
5902parser_is_identchar(struct parser_params *p)
5903{
5904 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5905}
5906
5907static inline int
5908parser_isascii(struct parser_params *p)
5909{
5910 return ISASCII(*(p->lex.pcur-1));
5911}
5912
5913static void
5914token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
5915{
5916 int column = 1, nonspc = 0, i;
5917 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
5918 if (*ptr == '\t') {
5919 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5920 }
5921 column++;
5922 if (*ptr != ' ' && *ptr != '\t') {
5923 nonspc = 1;
5924 }
5925 }
5926
5927 ptinfo->beg = loc->beg_pos;
5928 ptinfo->indent = column;
5929 ptinfo->nonspc = nonspc;
5930}
5931
5932static void
5933token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5934{
5935 token_info *ptinfo;
5936
5937 if (!p->token_info_enabled) return;
5938 ptinfo = ALLOC(token_info);
5939 ptinfo->token = token;
5940 ptinfo->next = p->token_info;
5941 token_info_setup(ptinfo, p->lex.pbeg, loc);
5942
5943 p->token_info = ptinfo;
5944}
5945
5946static void
5947token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5948{
5949 token_info *ptinfo_beg = p->token_info;
5950
5951 if (!ptinfo_beg) return;
5952 p->token_info = ptinfo_beg->next;
5953
5954 /* indentation check of matched keywords (begin..end, if..end, etc.) */
5955 token_info_warn(p, token, ptinfo_beg, 1, loc);
5956 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5957}
5958
5959static void
5960token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
5961{
5962 token_info *ptinfo_beg = p->token_info;
5963
5964 if (!ptinfo_beg) return;
5965 p->token_info = ptinfo_beg->next;
5966
5967 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
5968 ptinfo_beg->beg.column != beg_pos.column ||
5969 strcmp(ptinfo_beg->token, token)) {
5970 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
5971 beg_pos.lineno, beg_pos.column, token,
5972 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
5973 ptinfo_beg->token);
5974 }
5975
5976 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5977}
5978
5979static void
5980token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
5981{
5982 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
5983 if (!p->token_info_enabled) return;
5984 if (!ptinfo_beg) return;
5985 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
5986 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
5987 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
5988 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
5989 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
5990 rb_warn3L(ptinfo_end->beg.lineno,
5991 "mismatched indentations at '%s' with '%s' at %d",
5992 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
5993}
5994
5995static int
5996parser_precise_mbclen(struct parser_params *p, const char *ptr)
5997{
5998 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
5999 if (!MBCLEN_CHARFOUND_P(len)) {
6000 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
6001 return -1;
6002 }
6003 return len;
6004}
6005
6006#ifndef RIPPER
6007static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
6008
6009static inline void
6010parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6011{
6012 VALUE str;
6013 int lineno = p->ruby_sourceline;
6014 if (!yylloc) {
6015 return;
6016 }
6017 else if (yylloc->beg_pos.lineno == lineno) {
6018 str = p->lex.lastline;
6019 }
6020 else {
6021 return;
6022 }
6023 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
6024}
6025
6026static int
6027parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6028{
6029#if 0
6030 YYLTYPE current;
6031
6032 if (!yylloc) {
6033 yylloc = RUBY_SET_YYLLOC(current);
6034 }
6035 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
6036 p->ruby_sourceline != yylloc->end_pos.lineno)) {
6037 yylloc = 0;
6038 }
6039#endif
6040 compile_error(p, "%s", msg);
6041 parser_show_error_line(p, yylloc);
6042 return 0;
6043}
6044
6045static int
6046parser_yyerror0(struct parser_params *p, const char *msg)
6047{
6048 YYLTYPE current;
6049 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
6050}
6051
6052static void
6053ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
6054{
6055 VALUE mesg;
6056 const int max_line_margin = 30;
6057 const char *ptr, *ptr_end, *pt, *pb;
6058 const char *pre = "", *post = "", *pend;
6059 const char *code = "", *caret = "";
6060 const char *lim;
6061 const char *const pbeg = RSTRING_PTR(str);
6062 char *buf;
6063 long len;
6064 int i;
6065
6066 if (!yylloc) return;
6067 pend = RSTRING_END(str);
6068 if (pend > pbeg && pend[-1] == '\n') {
6069 if (--pend > pbeg && pend[-1] == '\r') --pend;
6070 }
6071
6072 pt = pend;
6073 if (lineno == yylloc->end_pos.lineno &&
6074 (pend - pbeg) > yylloc->end_pos.column) {
6075 pt = pbeg + yylloc->end_pos.column;
6076 }
6077
6078 ptr = ptr_end = pt;
6079 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
6080 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
6081
6082 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
6083 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
6084
6085 len = ptr_end - ptr;
6086 if (len > 4) {
6087 if (ptr > pbeg) {
6088 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
6089 if (ptr > pbeg) pre = "...";
6090 }
6091 if (ptr_end < pend) {
6092 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
6093 if (ptr_end < pend) post = "...";
6094 }
6095 }
6096 pb = pbeg;
6097 if (lineno == yylloc->beg_pos.lineno) {
6098 pb += yylloc->beg_pos.column;
6099 if (pb > pt) pb = pt;
6100 }
6101 if (pb < ptr) pb = ptr;
6102 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
6103 return;
6104 }
6105 if (RTEST(errbuf)) {
6106 mesg = rb_attr_get(errbuf, idMesg);
6107 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
6108 rb_str_cat_cstr(mesg, "\n");
6109 }
6110 else {
6111 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
6112 }
6113 if (!errbuf && rb_stderr_tty_p()) {
6114#define CSI_BEGIN "\033["
6115#define CSI_SGR "m"
6116 rb_str_catf(mesg,
6117 CSI_BEGIN""CSI_SGR"%s" /* pre */
6118 CSI_BEGIN"1"CSI_SGR"%.*s"
6119 CSI_BEGIN"1;4"CSI_SGR"%.*s"
6120 CSI_BEGIN";1"CSI_SGR"%.*s"
6121 CSI_BEGIN""CSI_SGR"%s" /* post */
6122 "\n",
6123 pre,
6124 (int)(pb - ptr), ptr,
6125 (int)(pt - pb), pb,
6126 (int)(ptr_end - pt), pt,
6127 post);
6128 }
6129 else {
6130 char *p2;
6131
6132 len = ptr_end - ptr;
6133 lim = pt < pend ? pt : pend;
6134 i = (int)(lim - ptr);
6135 buf = ALLOCA_N(char, i+2);
6136 code = ptr;
6137 caret = p2 = buf;
6138 if (ptr <= pb) {
6139 while (ptr < pb) {
6140 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
6141 }
6142 *p2++ = '^';
6143 ptr++;
6144 }
6145 if (lim > ptr) {
6146 memset(p2, '~', (lim - ptr));
6147 p2 += (lim - ptr);
6148 }
6149 *p2 = '\0';
6150 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
6151 pre, (int)len, code, post,
6152 pre, caret);
6153 }
6154 if (!errbuf) rb_write_error_str(mesg);
6155}
6156#else
6157static int
6158parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6159{
6160 const char *pcur = 0, *ptok = 0;
6161 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
6162 p->ruby_sourceline == yylloc->end_pos.lineno) {
6163 pcur = p->lex.pcur;
6164 ptok = p->lex.ptok;
6165 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
6166 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
6167 }
6168 parser_yyerror0(p, msg);
6169 if (pcur) {
6170 p->lex.ptok = ptok;
6171 p->lex.pcur = pcur;
6172 }
6173 return 0;
6174}
6175
6176static int
6177parser_yyerror0(struct parser_params *p, const char *msg)
6178{
6179 dispatch1(parse_error, STR_NEW2(msg));
6180 ripper_error(p);
6181 return 0;
6182}
6183
6184static inline void
6185parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6186{
6187}
6188#endif /* !RIPPER */
6189
6190#ifndef RIPPER
6191static int
6192vtable_size(const struct vtable *tbl)
6193{
6194 if (!DVARS_TERMINAL_P(tbl)) {
6195 return tbl->pos;
6196 }
6197 else {
6198 return 0;
6199 }
6200}
6201#endif
6202
6203static struct vtable *
6204vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
6205{
6206 struct vtable *tbl = ALLOC(struct vtable);
6207 tbl->pos = 0;
6208 tbl->capa = 8;
6209 tbl->tbl = ALLOC_N(ID, tbl->capa);
6210 tbl->prev = prev;
6211#ifndef RIPPER
6212 if (p->debug) {
6213 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
6214 }
6215#endif
6216 return tbl;
6217}
6218#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
6219
6220static void
6221vtable_free_gen(struct parser_params *p, int line, const char *name,
6222 struct vtable *tbl)
6223{
6224#ifndef RIPPER
6225 if (p->debug) {
6226 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
6227 }
6228#endif
6229 if (!DVARS_TERMINAL_P(tbl)) {
6230 if (tbl->tbl) {
6231 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
6232 }
6233 ruby_sized_xfree(tbl, sizeof(*tbl));
6234 }
6235}
6236#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
6237
6238static void
6239vtable_add_gen(struct parser_params *p, int line, const char *name,
6240 struct vtable *tbl, ID id)
6241{
6242#ifndef RIPPER
6243 if (p->debug) {
6244 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
6245 line, name, (void *)tbl, rb_id2name(id));
6246 }
6247#endif
6248 if (DVARS_TERMINAL_P(tbl)) {
6249 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
6250 return;
6251 }
6252 if (tbl->pos == tbl->capa) {
6253 tbl->capa = tbl->capa * 2;
6254 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
6255 }
6256 tbl->tbl[tbl->pos++] = id;
6257}
6258#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
6259
6260#ifndef RIPPER
6261static void
6262vtable_pop_gen(struct parser_params *p, int line, const char *name,
6263 struct vtable *tbl, int n)
6264{
6265 if (p->debug) {
6266 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
6267 line, name, (void *)tbl, n);
6268 }
6269 if (tbl->pos < n) {
6270 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
6271 return;
6272 }
6273 tbl->pos -= n;
6274}
6275#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
6276#endif
6277
6278static int
6279vtable_included(const struct vtable * tbl, ID id)
6280{
6281 int i;
6282
6283 if (!DVARS_TERMINAL_P(tbl)) {
6284 for (i = 0; i < tbl->pos; i++) {
6285 if (tbl->tbl[i] == id) {
6286 return i+1;
6287 }
6288 }
6289 }
6290 return 0;
6291}
6292
6293static void parser_prepare(struct parser_params *p);
6294
6295#ifndef RIPPER
6296static NODE *parser_append_options(struct parser_params *p, NODE *node);
6297
6298static VALUE
6299debug_lines(VALUE fname)
6300{
6301 ID script_lines;
6302 CONST_ID(script_lines, "SCRIPT_LINES__");
6303 if (rb_const_defined_at(rb_cObject, script_lines)) {
6304 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
6305 if (RB_TYPE_P(hash, T_HASH)) {
6306 VALUE lines = rb_ary_new();
6307 rb_hash_aset(hash, fname, lines);
6308 return lines;
6309 }
6310 }
6311 return 0;
6312}
6313
6314static int
6315e_option_supplied(struct parser_params *p)
6316{
6317 return strcmp(p->ruby_sourcefile, "-e") == 0;
6318}
6319
6320static VALUE
6321yycompile0(VALUE arg)
6322{
6323 int n;
6324 NODE *tree;
6325 struct parser_params *p = (struct parser_params *)arg;
6326 VALUE cov = Qfalse;
6327
6328 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
6329 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
6330 if (p->debug_lines && p->ruby_sourceline > 0) {
6331 VALUE str = rb_default_rs;
6332 n = p->ruby_sourceline;
6333 do {
6334 rb_ary_push(p->debug_lines, str);
6335 } while (--n);
6336 }
6337
6338 if (!e_option_supplied(p)) {
6339 cov = Qtrue;
6340 }
6341 }
6342
6343 if (p->keep_script_lines || ruby_vm_keep_script_lines) {
6344 if (!p->debug_lines) {
6345 p->debug_lines = rb_ary_new();
6346 }
6347
6348 RB_OBJ_WRITE(p->ast, &p->ast->body.script_lines, p->debug_lines);
6349 }
6350
6351 parser_prepare(p);
6352#define RUBY_DTRACE_PARSE_HOOK(name) \
6353 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
6354 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
6355 }
6356 RUBY_DTRACE_PARSE_HOOK(BEGIN);
6357 n = yyparse(p);
6358 RUBY_DTRACE_PARSE_HOOK(END);
6359 p->debug_lines = 0;
6360
6361 p->lex.strterm = 0;
6362 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
6363 p->lex.prevline = p->lex.lastline = p->lex.nextline = 0;
6364 if (n || p->error_p) {
6365 VALUE mesg = p->error_buffer;
6366 if (!mesg) {
6367 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
6368 }
6369 rb_set_errinfo(mesg);
6370 return FALSE;
6371 }
6372 tree = p->eval_tree;
6373 if (!tree) {
6374 tree = NEW_NIL(&NULL_LOC);
6375 }
6376 else {
6377 VALUE opt = p->compile_option;
6378 NODE *prelude;
6379 NODE *body = parser_append_options(p, tree->nd_body);
6380 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
6381 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
6382 prelude = block_append(p, p->eval_tree_begin, body);
6383 tree->nd_body = prelude;
6384 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
6385 }
6386 p->ast->body.root = tree;
6387 if (!p->ast->body.script_lines) p->ast->body.script_lines = INT2FIX(p->line_count);
6388 return TRUE;
6389}
6390
6391static rb_ast_t *
6392yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
6393{
6394 rb_ast_t *ast;
6395 if (NIL_P(fname)) {
6396 p->ruby_sourcefile_string = Qnil;
6397 p->ruby_sourcefile = "(none)";
6398 }
6399 else {
6400 p->ruby_sourcefile_string = rb_fstring(fname);
6401 p->ruby_sourcefile = StringValueCStr(fname);
6402 }
6403 p->ruby_sourceline = line - 1;
6404
6405 p->lvtbl = NULL;
6406
6407 p->ast = ast = rb_ast_new();
6408 rb_suppress_tracing(yycompile0, (VALUE)p);
6409 p->ast = 0;
6410 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6411
6412 while (p->lvtbl) {
6413 local_pop(p);
6414 }
6415
6416 return ast;
6417}
6418#endif /* !RIPPER */
6419
6420static rb_encoding *
6421must_be_ascii_compatible(VALUE s)
6422{
6423 rb_encoding *enc = rb_enc_get(s);
6424 if (!rb_enc_asciicompat(enc)) {
6425 rb_raise(rb_eArgError, "invalid source encoding");
6426 }
6427 return enc;
6428}
6429
6430static VALUE
6431lex_get_str(struct parser_params *p, VALUE s)
6432{
6433 char *beg, *end, *start;
6434 long len;
6435
6436 beg = RSTRING_PTR(s);
6437 len = RSTRING_LEN(s);
6438 start = beg;
6439 if (p->lex.gets_.ptr) {
6440 if (len == p->lex.gets_.ptr) return Qnil;
6441 beg += p->lex.gets_.ptr;
6442 len -= p->lex.gets_.ptr;
6443 }
6444 end = memchr(beg, '\n', len);
6445 if (end) len = ++end - beg;
6446 p->lex.gets_.ptr += len;
6447 return rb_str_subseq(s, beg - start, len);
6448}
6449
6450static VALUE
6451lex_getline(struct parser_params *p)
6452{
6453 VALUE line = (*p->lex.gets)(p, p->lex.input);
6454 if (NIL_P(line)) return line;
6455 must_be_ascii_compatible(line);
6456 if (RB_OBJ_FROZEN(line)) line = rb_str_dup(line); // needed for RubyVM::AST.of because script_lines in iseq is deep-frozen
6457#ifndef RIPPER
6458 if (p->debug_lines) {
6459 rb_enc_associate(line, p->enc);
6460 rb_ary_push(p->debug_lines, line);
6461 }
6462#endif
6463 p->line_count++;
6464 return line;
6465}
6466
6467static const rb_data_type_t parser_data_type;
6468
6469#ifndef RIPPER
6470static rb_ast_t*
6471parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6472{
6473 struct parser_params *p;
6474
6475 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6476
6477 p->lex.gets = lex_get_str;
6478 p->lex.gets_.ptr = 0;
6479 p->lex.input = rb_str_new_frozen(s);
6480 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6481
6482 return yycompile(vparser, p, fname, line);
6483}
6484
6485rb_ast_t*
6486rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6487{
6488 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6489}
6490
6491rb_ast_t*
6492rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6493{
6494 must_be_ascii_compatible(s);
6495 return parser_compile_string(vparser, f, s, line);
6496}
6497
6498VALUE rb_io_gets_internal(VALUE io);
6499
6500static VALUE
6501lex_io_gets(struct parser_params *p, VALUE io)
6502{
6503 return rb_io_gets_internal(io);
6504}
6505
6506rb_ast_t*
6507rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6508{
6509 struct parser_params *p;
6510
6511 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6512
6513 p->lex.gets = lex_io_gets;
6514 p->lex.input = file;
6515 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6516
6517 return yycompile(vparser, p, fname, start);
6518}
6519
6520static VALUE
6521lex_generic_gets(struct parser_params *p, VALUE input)
6522{
6523 return (*p->lex.gets_.call)(input, p->line_count);
6524}
6525
6526rb_ast_t*
6527rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6528{
6529 struct parser_params *p;
6530
6531 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6532
6533 p->lex.gets = lex_generic_gets;
6534 p->lex.gets_.call = lex_gets;
6535 p->lex.input = input;
6536 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6537
6538 return yycompile(vparser, p, fname, start);
6539}
6540#endif /* !RIPPER */
6541
6542#define STR_FUNC_ESCAPE 0x01
6543#define STR_FUNC_EXPAND 0x02
6544#define STR_FUNC_REGEXP 0x04
6545#define STR_FUNC_QWORDS 0x08
6546#define STR_FUNC_SYMBOL 0x10
6547#define STR_FUNC_INDENT 0x20
6548#define STR_FUNC_LABEL 0x40
6549#define STR_FUNC_LIST 0x4000
6550#define STR_FUNC_TERM 0x8000
6551
6552enum string_type {
6553 str_label = STR_FUNC_LABEL,
6554 str_squote = (0),
6555 str_dquote = (STR_FUNC_EXPAND),
6556 str_xquote = (STR_FUNC_EXPAND),
6557 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6558 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6559 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6560 str_ssym = (STR_FUNC_SYMBOL),
6561 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6562};
6563
6564static VALUE
6565parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6566{
6567 VALUE str;
6568
6569 str = rb_enc_str_new(ptr, len, enc);
6570 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6571 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6572 }
6573 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6574 rb_enc_associate(str, rb_ascii8bit_encoding());
6575 }
6576 }
6577
6578 return str;
6579}
6580
6581#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6582#define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6583#define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6584#define peek(p,c) peek_n(p, (c), 0)
6585#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6586#define peekc(p) peekc_n(p, 0)
6587#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6588
6589#ifdef RIPPER
6590static void
6591add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6592{
6593 if (tok < end) {
6594 if (!has_delayed_token(p)) {
6595 p->delayed.token = rb_str_buf_new(end - tok);
6596 rb_enc_associate(p->delayed.token, p->enc);
6597 p->delayed.line = p->ruby_sourceline;
6598 p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6599 }
6600 rb_str_buf_cat(p->delayed.token, tok, end - tok);
6601 p->lex.ptok = end;
6602 }
6603}
6604#else
6605#define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6606#endif
6607
6608static int
6609nextline(struct parser_params *p)
6610{
6611 VALUE v = p->lex.nextline;
6612 p->lex.nextline = 0;
6613 if (!v) {
6614 if (p->eofp)
6615 return -1;
6616
6617 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6618 goto end_of_input;
6619 }
6620
6621 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6622 end_of_input:
6623 p->eofp = 1;
6624 lex_goto_eol(p);
6625 return -1;
6626 }
6627 p->cr_seen = FALSE;
6628 }
6629 else if (NIL_P(v)) {
6630 /* after here-document without terminator */
6631 goto end_of_input;
6632 }
6633 add_delayed_token(p, p->lex.ptok, p->lex.pend);
6634 if (p->heredoc_end > 0) {
6635 p->ruby_sourceline = p->heredoc_end;
6636 p->heredoc_end = 0;
6637 }
6638 p->ruby_sourceline++;
6639 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
6640 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
6641 token_flush(p);
6642 p->lex.prevline = p->lex.lastline;
6643 p->lex.lastline = v;
6644 return 0;
6645}
6646
6647static int
6648parser_cr(struct parser_params *p, int c)
6649{
6650 if (peek(p, '\n')) {
6651 p->lex.pcur++;
6652 c = '\n';
6653 }
6654 return c;
6655}
6656
6657static inline int
6658nextc(struct parser_params *p)
6659{
6660 int c;
6661
6662 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6663 if (nextline(p)) return -1;
6664 }
6665 c = (unsigned char)*p->lex.pcur++;
6666 if (UNLIKELY(c == '\r')) {
6667 c = parser_cr(p, c);
6668 }
6669
6670 return c;
6671}
6672
6673static void
6674pushback(struct parser_params *p, int c)
6675{
6676 if (c == -1) return;
6677 p->lex.pcur--;
6678 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6679 p->lex.pcur--;
6680 }
6681}
6682
6683#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6684
6685#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6686#define tok(p) (p)->tokenbuf
6687#define toklen(p) (p)->tokidx
6688
6689static int
6690looking_at_eol_p(struct parser_params *p)
6691{
6692 const char *ptr = p->lex.pcur;
6693 while (ptr < p->lex.pend) {
6694 int c = (unsigned char)*ptr++;
6695 int eol = (c == '\n' || c == '#');
6696 if (eol || !ISSPACE(c)) {
6697 return eol;
6698 }
6699 }
6700 return TRUE;
6701}
6702
6703static char*
6704newtok(struct parser_params *p)
6705{
6706 p->tokidx = 0;
6707 p->tokline = p->ruby_sourceline;
6708 if (!p->tokenbuf) {
6709 p->toksiz = 60;
6710 p->tokenbuf = ALLOC_N(char, 60);
6711 }
6712 if (p->toksiz > 4096) {
6713 p->toksiz = 60;
6714 REALLOC_N(p->tokenbuf, char, 60);
6715 }
6716 return p->tokenbuf;
6717}
6718
6719static char *
6720tokspace(struct parser_params *p, int n)
6721{
6722 p->tokidx += n;
6723
6724 if (p->tokidx >= p->toksiz) {
6725 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6726 REALLOC_N(p->tokenbuf, char, p->toksiz);
6727 }
6728 return &p->tokenbuf[p->tokidx-n];
6729}
6730
6731static void
6732tokadd(struct parser_params *p, int c)
6733{
6734 p->tokenbuf[p->tokidx++] = (char)c;
6735 if (p->tokidx >= p->toksiz) {
6736 p->toksiz *= 2;
6737 REALLOC_N(p->tokenbuf, char, p->toksiz);
6738 }
6739}
6740
6741static int
6742tok_hex(struct parser_params *p, size_t *numlen)
6743{
6744 int c;
6745
6746 c = scan_hex(p->lex.pcur, 2, numlen);
6747 if (!*numlen) {
6748 yyerror0("invalid hex escape");
6749 token_flush(p);
6750 return 0;
6751 }
6752 p->lex.pcur += *numlen;
6753 return c;
6754}
6755
6756#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6757
6758static int
6759escaped_control_code(int c)
6760{
6761 int c2 = 0;
6762 switch (c) {
6763 case ' ':
6764 c2 = 's';
6765 break;
6766 case '\n':
6767 c2 = 'n';
6768 break;
6769 case '\t':
6770 c2 = 't';
6771 break;
6772 case '\v':
6773 c2 = 'v';
6774 break;
6775 case '\r':
6776 c2 = 'r';
6777 break;
6778 case '\f':
6779 c2 = 'f';
6780 break;
6781 }
6782 return c2;
6783}
6784
6785#define WARN_SPACE_CHAR(c, prefix) \
6786 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6787
6788static int
6789tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6790 int regexp_literal, int wide)
6791{
6792 size_t numlen;
6793 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6794 literal_flush(p, p->lex.pcur);
6795 p->lex.pcur += numlen;
6796 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6797 yyerror0("invalid Unicode escape");
6798 return wide && numlen > 0;
6799 }
6800 if (codepoint > 0x10ffff) {
6801 yyerror0("invalid Unicode codepoint (too large)");
6802 return wide;
6803 }
6804 if ((codepoint & 0xfffff800) == 0xd800) {
6805 yyerror0("invalid Unicode codepoint");
6806 return wide;
6807 }
6808 if (regexp_literal) {
6809 tokcopy(p, (int)numlen);
6810 }
6811 else if (codepoint >= 0x80) {
6812 rb_encoding *utf8 = rb_utf8_encoding();
6813 if (*encp && utf8 != *encp) {
6814 YYLTYPE loc = RUBY_INIT_YYLLOC();
6815 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6816 parser_show_error_line(p, &loc);
6817 return wide;
6818 }
6819 *encp = utf8;
6820 tokaddmbc(p, codepoint, *encp);
6821 }
6822 else {
6823 tokadd(p, codepoint);
6824 }
6825 return TRUE;
6826}
6827
6828/* return value is for ?\u3042 */
6829static void
6830tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6831 int term, int symbol_literal, int regexp_literal)
6832{
6833 /*
6834 * If `term` is not -1, then we allow multiple codepoints in \u{}
6835 * upto `term` byte, otherwise we're parsing a character literal.
6836 * And then add the codepoints to the current token.
6837 */
6838 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6839
6840 const int open_brace = '{', close_brace = '}';
6841
6842 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6843
6844 if (peek(p, open_brace)) { /* handle \u{...} form */
6845 const char *second = NULL;
6846 int c, last = nextc(p);
6847 if (p->lex.pcur >= p->lex.pend) goto unterminated;
6848 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6849 while (c != close_brace) {
6850 if (c == term) goto unterminated;
6851 if (second == multiple_codepoints)
6852 second = p->lex.pcur;
6853 if (regexp_literal) tokadd(p, last);
6854 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6855 break;
6856 }
6857 while (ISSPACE(c = *p->lex.pcur)) {
6858 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6859 last = c;
6860 }
6861 if (term == -1 && !second)
6862 second = multiple_codepoints;
6863 }
6864
6865 if (c != close_brace) {
6866 unterminated:
6867 token_flush(p);
6868 yyerror0("unterminated Unicode escape");
6869 return;
6870 }
6871 if (second && second != multiple_codepoints) {
6872 const char *pcur = p->lex.pcur;
6873 p->lex.pcur = second;
6874 dispatch_scan_event(p, tSTRING_CONTENT);
6875 token_flush(p);
6876 p->lex.pcur = pcur;
6877 yyerror0(multiple_codepoints);
6878 token_flush(p);
6879 }
6880
6881 if (regexp_literal) tokadd(p, close_brace);
6882 nextc(p);
6883 }
6884 else { /* handle \uxxxx form */
6885 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6886 token_flush(p);
6887 return;
6888 }
6889 }
6890}
6891
6892#define ESCAPE_CONTROL 1
6893#define ESCAPE_META 2
6894
6895static int
6896read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6897{
6898 int c;
6899 size_t numlen;
6900
6901 switch (c = nextc(p)) {
6902 case '\\': /* Backslash */
6903 return c;
6904
6905 case 'n': /* newline */
6906 return '\n';
6907
6908 case 't': /* horizontal tab */
6909 return '\t';
6910
6911 case 'r': /* carriage-return */
6912 return '\r';
6913
6914 case 'f': /* form-feed */
6915 return '\f';
6916
6917 case 'v': /* vertical tab */
6918 return '\13';
6919
6920 case 'a': /* alarm(bell) */
6921 return '\007';
6922
6923 case 'e': /* escape */
6924 return 033;
6925
6926 case '0': case '1': case '2': case '3': /* octal constant */
6927 case '4': case '5': case '6': case '7':
6928 pushback(p, c);
6929 c = scan_oct(p->lex.pcur, 3, &numlen);
6930 p->lex.pcur += numlen;
6931 return c;
6932
6933 case 'x': /* hex constant */
6934 c = tok_hex(p, &numlen);
6935 if (numlen == 0) return 0;
6936 return c;
6937
6938 case 'b': /* backspace */
6939 return '\010';
6940
6941 case 's': /* space */
6942 return ' ';
6943
6944 case 'M':
6945 if (flags & ESCAPE_META) goto eof;
6946 if ((c = nextc(p)) != '-') {
6947 goto eof;
6948 }
6949 if ((c = nextc(p)) == '\\') {
6950 switch (peekc(p)) {
6951 case 'u': case 'U':
6952 nextc(p);
6953 goto eof;
6954 }
6955 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6956 }
6957 else if (c == -1 || !ISASCII(c)) goto eof;
6958 else {
6959 int c2 = escaped_control_code(c);
6960 if (c2) {
6961 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6962 WARN_SPACE_CHAR(c2, "\\M-");
6963 }
6964 else {
6965 WARN_SPACE_CHAR(c2, "\\C-\\M-");
6966 }
6967 }
6968 else if (ISCNTRL(c)) goto eof;
6969 return ((c & 0xff) | 0x80);
6970 }
6971
6972 case 'C':
6973 if ((c = nextc(p)) != '-') {
6974 goto eof;
6975 }
6976 case 'c':
6977 if (flags & ESCAPE_CONTROL) goto eof;
6978 if ((c = nextc(p))== '\\') {
6979 switch (peekc(p)) {
6980 case 'u': case 'U':
6981 nextc(p);
6982 goto eof;
6983 }
6984 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6985 }
6986 else if (c == '?')
6987 return 0177;
6988 else if (c == -1 || !ISASCII(c)) goto eof;
6989 else {
6990 int c2 = escaped_control_code(c);
6991 if (c2) {
6992 if (ISCNTRL(c)) {
6993 if (flags & ESCAPE_META) {
6994 WARN_SPACE_CHAR(c2, "\\M-");
6995 }
6996 else {
6997 WARN_SPACE_CHAR(c2, "");
6998 }
6999 }
7000 else {
7001 if (flags & ESCAPE_META) {
7002 WARN_SPACE_CHAR(c2, "\\M-\\C-");
7003 }
7004 else {
7005 WARN_SPACE_CHAR(c2, "\\C-");
7006 }
7007 }
7008 }
7009 else if (ISCNTRL(c)) goto eof;
7010 }
7011 return c & 0x9f;
7012
7013 eof:
7014 case -1:
7015 yyerror0("Invalid escape character syntax");
7016 token_flush(p);
7017 return '\0';
7018
7019 default:
7020 return c;
7021 }
7022}
7023
7024static void
7025tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
7026{
7027 int len = rb_enc_codelen(c, enc);
7028 rb_enc_mbcput(c, tokspace(p, len), enc);
7029}
7030
7031static int
7032tokadd_escape(struct parser_params *p, rb_encoding **encp)
7033{
7034 int c;
7035 size_t numlen;
7036
7037 switch (c = nextc(p)) {
7038 case '\n':
7039 return 0; /* just ignore */
7040
7041 case '0': case '1': case '2': case '3': /* octal constant */
7042 case '4': case '5': case '6': case '7':
7043 {
7044 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
7045 if (numlen == 0) goto eof;
7046 p->lex.pcur += numlen;
7047 tokcopy(p, (int)numlen + 1);
7048 }
7049 return 0;
7050
7051 case 'x': /* hex constant */
7052 {
7053 tok_hex(p, &numlen);
7054 if (numlen == 0) return -1;
7055 tokcopy(p, (int)numlen + 2);
7056 }
7057 return 0;
7058
7059 eof:
7060 case -1:
7061 yyerror0("Invalid escape character syntax");
7062 token_flush(p);
7063 return -1;
7064
7065 default:
7066 tokadd(p, '\\');
7067 tokadd(p, c);
7068 }
7069 return 0;
7070}
7071
7072static int
7073regx_options(struct parser_params *p)
7074{
7075 int kcode = 0;
7076 int kopt = 0;
7077 int options = 0;
7078 int c, opt, kc;
7079
7080 newtok(p);
7081 while (c = nextc(p), ISALPHA(c)) {
7082 if (c == 'o') {
7083 options |= RE_OPTION_ONCE;
7084 }
7085 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
7086 if (kc >= 0) {
7087 if (kc != rb_ascii8bit_encindex()) kcode = c;
7088 kopt = opt;
7089 }
7090 else {
7091 options |= opt;
7092 }
7093 }
7094 else {
7095 tokadd(p, c);
7096 }
7097 }
7098 options |= kopt;
7099 pushback(p, c);
7100 if (toklen(p)) {
7101 YYLTYPE loc = RUBY_INIT_YYLLOC();
7102 tokfix(p);
7103 compile_error(p, "unknown regexp option%s - %*s",
7104 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
7105 parser_show_error_line(p, &loc);
7106 }
7107 return options | RE_OPTION_ENCODING(kcode);
7108}
7109
7110static int
7111tokadd_mbchar(struct parser_params *p, int c)
7112{
7113 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7114 if (len < 0) return -1;
7115 tokadd(p, c);
7116 p->lex.pcur += --len;
7117 if (len > 0) tokcopy(p, len);
7118 return c;
7119}
7120
7121static inline int
7122simple_re_meta(int c)
7123{
7124 switch (c) {
7125 case '$': case '*': case '+': case '.':
7126 case '?': case '^': case '|':
7127 case ')': case ']': case '}': case '>':
7128 return TRUE;
7129 default:
7130 return FALSE;
7131 }
7132}
7133
7134static int
7135parser_update_heredoc_indent(struct parser_params *p, int c)
7136{
7137 if (p->heredoc_line_indent == -1) {
7138 if (c == '\n') p->heredoc_line_indent = 0;
7139 }
7140 else {
7141 if (c == ' ') {
7142 p->heredoc_line_indent++;
7143 return TRUE;
7144 }
7145 else if (c == '\t') {
7146 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
7147 p->heredoc_line_indent = w * TAB_WIDTH;
7148 return TRUE;
7149 }
7150 else if (c != '\n') {
7151 if (p->heredoc_indent > p->heredoc_line_indent) {
7152 p->heredoc_indent = p->heredoc_line_indent;
7153 }
7154 p->heredoc_line_indent = -1;
7155 }
7156 }
7157 return FALSE;
7158}
7159
7160static void
7161parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
7162{
7163 YYLTYPE loc = RUBY_INIT_YYLLOC();
7164 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
7165 compile_error(p, "%s mixed within %s source", n1, n2);
7166 parser_show_error_line(p, &loc);
7167}
7168
7169static void
7170parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
7171{
7172 const char *pos = p->lex.pcur;
7173 p->lex.pcur = beg;
7174 parser_mixed_error(p, enc1, enc2);
7175 p->lex.pcur = pos;
7176}
7177
7178static int
7179tokadd_string(struct parser_params *p,
7180 int func, int term, int paren, long *nest,
7181 rb_encoding **encp, rb_encoding **enc)
7182{
7183 int c;
7184 bool erred = false;
7185
7186#define mixed_error(enc1, enc2) \
7187 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
7188#define mixed_escape(beg, enc1, enc2) \
7189 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
7190
7191 while ((c = nextc(p)) != -1) {
7192 if (p->heredoc_indent > 0) {
7193 parser_update_heredoc_indent(p, c);
7194 }
7195
7196 if (paren && c == paren) {
7197 ++*nest;
7198 }
7199 else if (c == term) {
7200 if (!nest || !*nest) {
7201 pushback(p, c);
7202 break;
7203 }
7204 --*nest;
7205 }
7206 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
7207 int c2 = *p->lex.pcur;
7208 if (c2 == '$' || c2 == '@' || c2 == '{') {
7209 pushback(p, c);
7210 break;
7211 }
7212 }
7213 else if (c == '\\') {
7214 literal_flush(p, p->lex.pcur - 1);
7215 c = nextc(p);
7216 switch (c) {
7217 case '\n':
7218 if (func & STR_FUNC_QWORDS) break;
7219 if (func & STR_FUNC_EXPAND) {
7220 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
7221 continue;
7222 if (c == term) {
7223 c = '\\';
7224 goto terminate;
7225 }
7226 }
7227 tokadd(p, '\\');
7228 break;
7229
7230 case '\\':
7231 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
7232 break;
7233
7234 case 'u':
7235 if ((func & STR_FUNC_EXPAND) == 0) {
7236 tokadd(p, '\\');
7237 break;
7238 }
7239 tokadd_utf8(p, enc, term,
7240 func & STR_FUNC_SYMBOL,
7241 func & STR_FUNC_REGEXP);
7242 continue;
7243
7244 default:
7245 if (c == -1) return -1;
7246 if (!ISASCII(c)) {
7247 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
7248 goto non_ascii;
7249 }
7250 if (func & STR_FUNC_REGEXP) {
7251 switch (c) {
7252 case 'c':
7253 case 'C':
7254 case 'M': {
7255 pushback(p, c);
7256 c = read_escape(p, 0, enc);
7257
7258 int i;
7259 char escbuf[5];
7260 snprintf(escbuf, sizeof(escbuf), "\\x%02X", c);
7261 for (i = 0; i < 4; i++) {
7262 tokadd(p, escbuf[i]);
7263 }
7264 continue;
7265 }
7266 }
7267
7268 if (c == term && !simple_re_meta(c)) {
7269 tokadd(p, c);
7270 continue;
7271 }
7272 pushback(p, c);
7273 if ((c = tokadd_escape(p, enc)) < 0)
7274 return -1;
7275 if (*enc && *enc != *encp) {
7276 mixed_escape(p->lex.ptok+2, *enc, *encp);
7277 }
7278 continue;
7279 }
7280 else if (func & STR_FUNC_EXPAND) {
7281 pushback(p, c);
7282 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
7283 c = read_escape(p, 0, enc);
7284 }
7285 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7286 /* ignore backslashed spaces in %w */
7287 }
7288 else if (c != term && !(paren && c == paren)) {
7289 tokadd(p, '\\');
7290 pushback(p, c);
7291 continue;
7292 }
7293 }
7294 }
7295 else if (!parser_isascii(p)) {
7296 non_ascii:
7297 if (!*enc) {
7298 *enc = *encp;
7299 }
7300 else if (*enc != *encp) {
7301 mixed_error(*enc, *encp);
7302 continue;
7303 }
7304 if (tokadd_mbchar(p, c) == -1) return -1;
7305 continue;
7306 }
7307 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7308 pushback(p, c);
7309 break;
7310 }
7311 if (c & 0x80) {
7312 if (!*enc) {
7313 *enc = *encp;
7314 }
7315 else if (*enc != *encp) {
7316 mixed_error(*enc, *encp);
7317 continue;
7318 }
7319 }
7320 tokadd(p, c);
7321 }
7322 terminate:
7323 if (*enc) *encp = *enc;
7324 return c;
7325}
7326
7327static inline rb_strterm_t *
7328new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
7329{
7330 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
7331}
7332
7333/* imemo_parser_strterm for literal */
7334#define NEW_STRTERM(func, term, paren) \
7335 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
7336
7337#ifdef RIPPER
7338static void
7339flush_string_content(struct parser_params *p, rb_encoding *enc)
7340{
7341 VALUE content = yylval.val;
7342 if (!ripper_is_node_yylval(content))
7343 content = ripper_new_yylval(p, 0, 0, content);
7344 if (has_delayed_token(p)) {
7345 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7346 if (len > 0) {
7347 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7348 }
7349 dispatch_delayed_token(p, tSTRING_CONTENT);
7350 p->lex.ptok = p->lex.pcur;
7351 RNODE(content)->nd_rval = yylval.val;
7352 }
7353 dispatch_scan_event(p, tSTRING_CONTENT);
7354 if (yylval.val != content)
7355 RNODE(content)->nd_rval = yylval.val;
7356 yylval.val = content;
7357}
7358#else
7359#define flush_string_content(p, enc) ((void)(enc))
7360#endif
7361
7362RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7363/* this can be shared with ripper, since it's independent from struct
7364 * parser_params. */
7365#ifndef RIPPER
7366#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7367#define SPECIAL_PUNCT(idx) ( \
7368 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
7369 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
7370 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
7371 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
7372 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7373 BIT('0', idx))
7374const unsigned int ruby_global_name_punct_bits[] = {
7375 SPECIAL_PUNCT(0),
7376 SPECIAL_PUNCT(1),
7377 SPECIAL_PUNCT(2),
7378};
7379#undef BIT
7380#undef SPECIAL_PUNCT
7381#endif
7382
7383static enum yytokentype
7384parser_peek_variable_name(struct parser_params *p)
7385{
7386 int c;
7387 const char *ptr = p->lex.pcur;
7388
7389 if (ptr + 1 >= p->lex.pend) return 0;
7390 c = *ptr++;
7391 switch (c) {
7392 case '$':
7393 if ((c = *ptr) == '-') {
7394 if (++ptr >= p->lex.pend) return 0;
7395 c = *ptr;
7396 }
7397 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7398 return tSTRING_DVAR;
7399 }
7400 break;
7401 case '@':
7402 if ((c = *ptr) == '@') {
7403 if (++ptr >= p->lex.pend) return 0;
7404 c = *ptr;
7405 }
7406 break;
7407 case '{':
7408 p->lex.pcur = ptr;
7409 p->command_start = TRUE;
7410 return tSTRING_DBEG;
7411 default:
7412 return 0;
7413 }
7414 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7415 return tSTRING_DVAR;
7416 return 0;
7417}
7418
7419#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7420#define IS_END() IS_lex_state(EXPR_END_ANY)
7421#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7422#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7423#define IS_LABEL_POSSIBLE() (\
7424 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7425 IS_ARG())
7426#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7427#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7428
7429static inline enum yytokentype
7430parser_string_term(struct parser_params *p, int func)
7431{
7432 p->lex.strterm = 0;
7433 if (func & STR_FUNC_REGEXP) {
7434 set_yylval_num(regx_options(p));
7435 dispatch_scan_event(p, tREGEXP_END);
7436 SET_LEX_STATE(EXPR_END);
7437 return tREGEXP_END;
7438 }
7439 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7440 nextc(p);
7441 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7442 return tLABEL_END;
7443 }
7444 SET_LEX_STATE(EXPR_END);
7445 return tSTRING_END;
7446}
7447
7448static enum yytokentype
7449parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7450{
7451 int func = (int)quote->u1.func;
7452 int term = (int)quote->u3.term;
7453 int paren = (int)quote->u2.paren;
7454 int c, space = 0;
7455 rb_encoding *enc = p->enc;
7456 rb_encoding *base_enc = 0;
7457 VALUE lit;
7458
7459 if (func & STR_FUNC_TERM) {
7460 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7461 SET_LEX_STATE(EXPR_END);
7462 p->lex.strterm = 0;
7463 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7464 }
7465 c = nextc(p);
7466 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7467 do {c = nextc(p);} while (ISSPACE(c));
7468 space = 1;
7469 }
7470 if (func & STR_FUNC_LIST) {
7471 quote->u1.func &= ~STR_FUNC_LIST;
7472 space = 1;
7473 }
7474 if (c == term && !quote->u0.nest) {
7475 if (func & STR_FUNC_QWORDS) {
7476 quote->u1.func |= STR_FUNC_TERM;
7477 pushback(p, c); /* dispatch the term at tSTRING_END */
7478 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7479 return ' ';
7480 }
7481 return parser_string_term(p, func);
7482 }
7483 if (space) {
7484 pushback(p, c);
7485 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7486 return ' ';
7487 }
7488 newtok(p);
7489 if ((func & STR_FUNC_EXPAND) && c == '#') {
7490 int t = parser_peek_variable_name(p);
7491 if (t) return t;
7492 tokadd(p, '#');
7493 c = nextc(p);
7494 }
7495 pushback(p, c);
7496 if (tokadd_string(p, func, term, paren, &quote->u0.nest,
7497 &enc, &base_enc) == -1) {
7498 if (p->eofp) {
7499#ifndef RIPPER
7500# define unterminated_literal(mesg) yyerror0(mesg)
7501#else
7502# define unterminated_literal(mesg) compile_error(p, mesg)
7503#endif
7504 literal_flush(p, p->lex.pcur);
7505 if (func & STR_FUNC_QWORDS) {
7506 /* no content to add, bailing out here */
7507 unterminated_literal("unterminated list meets end of file");
7508 p->lex.strterm = 0;
7509 return tSTRING_END;
7510 }
7511 if (func & STR_FUNC_REGEXP) {
7512 unterminated_literal("unterminated regexp meets end of file");
7513 }
7514 else {
7515 unterminated_literal("unterminated string meets end of file");
7516 }
7517 quote->u1.func |= STR_FUNC_TERM;
7518 }
7519 }
7520
7521 tokfix(p);
7522 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7523 set_yylval_str(lit);
7524 flush_string_content(p, enc);
7525
7526 return tSTRING_CONTENT;
7527}
7528
7529static enum yytokentype
7530heredoc_identifier(struct parser_params *p)
7531{
7532 /*
7533 * term_len is length of `<<"END"` except `END`,
7534 * in this case term_len is 4 (<, <, " and ").
7535 */
7536 long len, offset = p->lex.pcur - p->lex.pbeg;
7537 int c = nextc(p), term, func = 0, quote = 0;
7538 enum yytokentype token = tSTRING_BEG;
7539 int indent = 0;
7540
7541 if (c == '-') {
7542 c = nextc(p);
7543 func = STR_FUNC_INDENT;
7544 offset++;
7545 }
7546 else if (c == '~') {
7547 c = nextc(p);
7548 func = STR_FUNC_INDENT;
7549 offset++;
7550 indent = INT_MAX;
7551 }
7552 switch (c) {
7553 case '\'':
7554 func |= str_squote; goto quoted;
7555 case '"':
7556 func |= str_dquote; goto quoted;
7557 case '`':
7558 token = tXSTRING_BEG;
7559 func |= str_xquote; goto quoted;
7560
7561 quoted:
7562 quote++;
7563 offset++;
7564 term = c;
7565 len = 0;
7566 while ((c = nextc(p)) != term) {
7567 if (c == -1 || c == '\r' || c == '\n') {
7568 yyerror0("unterminated here document identifier");
7569 return -1;
7570 }
7571 }
7572 break;
7573
7574 default:
7575 if (!parser_is_identchar(p)) {
7576 pushback(p, c);
7577 if (func & STR_FUNC_INDENT) {
7578 pushback(p, indent > 0 ? '~' : '-');
7579 }
7580 return 0;
7581 }
7582 func |= str_dquote;
7583 do {
7584 int n = parser_precise_mbclen(p, p->lex.pcur-1);
7585 if (n < 0) return 0;
7586 p->lex.pcur += --n;
7587 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7588 pushback(p, c);
7589 break;
7590 }
7591
7592 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7593 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7594 yyerror0("too long here document identifier");
7595 dispatch_scan_event(p, tHEREDOC_BEG);
7596 lex_goto_eol(p);
7597
7598 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7599 p->lex.strterm->flags |= STRTERM_HEREDOC;
7600 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7601 here->offset = offset;
7602 here->sourceline = p->ruby_sourceline;
7603 here->length = (int)len;
7604 here->quote = quote;
7605 here->func = func;
7606
7607 token_flush(p);
7608 p->heredoc_indent = indent;
7609 p->heredoc_line_indent = 0;
7610 return token;
7611}
7612
7613static void
7614heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7615{
7616 VALUE line;
7617
7618 p->lex.strterm = 0;
7619 line = here->lastline;
7620 p->lex.lastline = line;
7621 p->lex.pbeg = RSTRING_PTR(line);
7622 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7623 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7624 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7625 p->heredoc_end = p->ruby_sourceline;
7626 p->ruby_sourceline = (int)here->sourceline;
7627 if (p->eofp) p->lex.nextline = Qnil;
7628 p->eofp = 0;
7629}
7630
7631static int
7632dedent_string(VALUE string, int width)
7633{
7634 char *str;
7635 long len;
7636 int i, col = 0;
7637
7638 RSTRING_GETMEM(string, str, len);
7639 for (i = 0; i < len && col < width; i++) {
7640 if (str[i] == ' ') {
7641 col++;
7642 }
7643 else if (str[i] == '\t') {
7644 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7645 if (n > width) break;
7646 col = n;
7647 }
7648 else {
7649 break;
7650 }
7651 }
7652 if (!i) return 0;
7653 rb_str_modify(string);
7654 str = RSTRING_PTR(string);
7655 if (RSTRING_LEN(string) != len)
7656 rb_fatal("literal string changed: %+"PRIsVALUE, string);
7657 MEMMOVE(str, str + i, char, len - i);
7658 rb_str_set_len(string, len - i);
7659 return i;
7660}
7661
7662#ifndef RIPPER
7663static NODE *
7664heredoc_dedent(struct parser_params *p, NODE *root)
7665{
7666 NODE *node, *str_node, *prev_node;
7667 int indent = p->heredoc_indent;
7668 VALUE prev_lit = 0;
7669
7670 if (indent <= 0) return root;
7671 p->heredoc_indent = 0;
7672 if (!root) return root;
7673
7674 prev_node = node = str_node = root;
7675 if (nd_type_p(root, NODE_LIST)) str_node = root->nd_head;
7676
7677 while (str_node) {
7678 VALUE lit = str_node->nd_lit;
7679 if (str_node->flags & NODE_FL_NEWLINE) {
7680 dedent_string(lit, indent);
7681 }
7682 if (!prev_lit) {
7683 prev_lit = lit;
7684 }
7685 else if (!literal_concat0(p, prev_lit, lit)) {
7686 return 0;
7687 }
7688 else {
7689 NODE *end = node->nd_end;
7690 node = prev_node->nd_next = node->nd_next;
7691 if (!node) {
7692 if (nd_type_p(prev_node, NODE_DSTR))
7693 nd_set_type(prev_node, NODE_STR);
7694 break;
7695 }
7696 node->nd_end = end;
7697 goto next_str;
7698 }
7699
7700 str_node = 0;
7701 while ((node = (prev_node = node)->nd_next) != 0) {
7702 next_str:
7703 if (!nd_type_p(node, NODE_LIST)) break;
7704 if ((str_node = node->nd_head) != 0) {
7705 enum node_type type = nd_type(str_node);
7706 if (type == NODE_STR || type == NODE_DSTR) break;
7707 prev_lit = 0;
7708 str_node = 0;
7709 }
7710 }
7711 }
7712 return root;
7713}
7714#else /* RIPPER */
7715static VALUE
7716heredoc_dedent(struct parser_params *p, VALUE array)
7717{
7718 int indent = p->heredoc_indent;
7719
7720 if (indent <= 0) return array;
7721 p->heredoc_indent = 0;
7722 dispatch2(heredoc_dedent, array, INT2NUM(indent));
7723 return array;
7724}
7725
7726/*
7727 * call-seq:
7728 * Ripper.dedent_string(input, width) -> Integer
7729 *
7730 * USE OF RIPPER LIBRARY ONLY.
7731 *
7732 * Strips up to +width+ leading whitespaces from +input+,
7733 * and returns the stripped column width.
7734 */
7735static VALUE
7736parser_dedent_string(VALUE self, VALUE input, VALUE width)
7737{
7738 int wid, col;
7739
7740 StringValue(input);
7741 wid = NUM2UINT(width);
7742 col = dedent_string(input, wid);
7743 return INT2NUM(col);
7744}
7745#endif
7746
7747static int
7748whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7749{
7750 const char *ptr = p->lex.pbeg;
7751 long n;
7752
7753 if (indent) {
7754 while (*ptr && ISSPACE(*ptr)) ptr++;
7755 }
7756 n = p->lex.pend - (ptr + len);
7757 if (n < 0) return FALSE;
7758 if (n > 0 && ptr[len] != '\n') {
7759 if (ptr[len] != '\r') return FALSE;
7760 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7761 }
7762 return strncmp(eos, ptr, len) == 0;
7763}
7764
7765static int
7766word_match_p(struct parser_params *p, const char *word, long len)
7767{
7768 if (strncmp(p->lex.pcur, word, len)) return 0;
7769 if (p->lex.pcur + len == p->lex.pend) return 1;
7770 int c = (unsigned char)p->lex.pcur[len];
7771 if (ISSPACE(c)) return 1;
7772 switch (c) {
7773 case '\0': case '\004': case '\032': return 1;
7774 }
7775 return 0;
7776}
7777
7778#define NUM_SUFFIX_R (1<<0)
7779#define NUM_SUFFIX_I (1<<1)
7780#define NUM_SUFFIX_ALL 3
7781
7782static int
7783number_literal_suffix(struct parser_params *p, int mask)
7784{
7785 int c, result = 0;
7786 const char *lastp = p->lex.pcur;
7787
7788 while ((c = nextc(p)) != -1) {
7789 if ((mask & NUM_SUFFIX_I) && c == 'i') {
7790 result |= (mask & NUM_SUFFIX_I);
7791 mask &= ~NUM_SUFFIX_I;
7792 /* r after i, rational of complex is disallowed */
7793 mask &= ~NUM_SUFFIX_R;
7794 continue;
7795 }
7796 if ((mask & NUM_SUFFIX_R) && c == 'r') {
7797 result |= (mask & NUM_SUFFIX_R);
7798 mask &= ~NUM_SUFFIX_R;
7799 continue;
7800 }
7801 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7802 p->lex.pcur = lastp;
7803 literal_flush(p, p->lex.pcur);
7804 return 0;
7805 }
7806 pushback(p, c);
7807 break;
7808 }
7809 return result;
7810}
7811
7812static enum yytokentype
7813set_number_literal(struct parser_params *p, VALUE v,
7814 enum yytokentype type, int suffix)
7815{
7816 if (suffix & NUM_SUFFIX_I) {
7817 v = rb_complex_raw(INT2FIX(0), v);
7818 type = tIMAGINARY;
7819 }
7820 set_yylval_literal(v);
7821 SET_LEX_STATE(EXPR_END);
7822 return type;
7823}
7824
7825static enum yytokentype
7826set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7827{
7828 enum yytokentype type = tINTEGER;
7829 if (suffix & NUM_SUFFIX_R) {
7830 v = rb_rational_raw1(v);
7831 type = tRATIONAL;
7832 }
7833 return set_number_literal(p, v, type, suffix);
7834}
7835
7836#ifdef RIPPER
7837static void
7838dispatch_heredoc_end(struct parser_params *p)
7839{
7840 VALUE str;
7841 if (has_delayed_token(p))
7842 dispatch_delayed_token(p, tSTRING_CONTENT);
7843 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7844 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7845 lex_goto_eol(p);
7846 token_flush(p);
7847}
7848
7849#else
7850#define dispatch_heredoc_end(p) ((void)0)
7851#endif
7852
7853static enum yytokentype
7854here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7855{
7856 int c, func, indent = 0;
7857 const char *eos, *ptr, *ptr_end;
7858 long len;
7859 VALUE str = 0;
7860 rb_encoding *enc = p->enc;
7861 rb_encoding *base_enc = 0;
7862 int bol;
7863
7864 eos = RSTRING_PTR(here->lastline) + here->offset;
7865 len = here->length;
7866 indent = (func = here->func) & STR_FUNC_INDENT;
7867
7868 if ((c = nextc(p)) == -1) {
7869 error:
7870#ifdef RIPPER
7871 if (!has_delayed_token(p)) {
7872 dispatch_scan_event(p, tSTRING_CONTENT);
7873 }
7874 else {
7875 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7876 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7877 int cr = ENC_CODERANGE_UNKNOWN;
7878 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7879 if (cr != ENC_CODERANGE_7BIT &&
7880 p->enc == rb_usascii_encoding() &&
7881 enc != rb_utf8_encoding()) {
7882 enc = rb_ascii8bit_encoding();
7883 }
7884 }
7885 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7886 }
7887 dispatch_delayed_token(p, tSTRING_CONTENT);
7888 }
7889 lex_goto_eol(p);
7890#endif
7891 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7892 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7893 (int)len, eos);
7894 token_flush(p);
7895 p->lex.strterm = 0;
7896 SET_LEX_STATE(EXPR_END);
7897 return tSTRING_END;
7898 }
7899 bol = was_bol(p);
7900 if (!bol) {
7901 /* not beginning of line, cannot be the terminator */
7902 }
7903 else if (p->heredoc_line_indent == -1) {
7904 /* `heredoc_line_indent == -1` means
7905 * - "after an interpolation in the same line", or
7906 * - "in a continuing line"
7907 */
7908 p->heredoc_line_indent = 0;
7909 }
7910 else if (whole_match_p(p, eos, len, indent)) {
7911 dispatch_heredoc_end(p);
7912 restore:
7913 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7914 token_flush(p);
7915 p->lex.strterm = 0;
7916 SET_LEX_STATE(EXPR_END);
7917 return tSTRING_END;
7918 }
7919
7920 if (!(func & STR_FUNC_EXPAND)) {
7921 do {
7922 ptr = RSTRING_PTR(p->lex.lastline);
7923 ptr_end = p->lex.pend;
7924 if (ptr_end > ptr) {
7925 switch (ptr_end[-1]) {
7926 case '\n':
7927 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7928 ptr_end++;
7929 break;
7930 }
7931 case '\r':
7932 --ptr_end;
7933 }
7934 }
7935
7936 if (p->heredoc_indent > 0) {
7937 long i = 0;
7938 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7939 i++;
7940 p->heredoc_line_indent = 0;
7941 }
7942
7943 if (str)
7944 rb_str_cat(str, ptr, ptr_end - ptr);
7945 else
7946 str = STR_NEW(ptr, ptr_end - ptr);
7947 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7948 lex_goto_eol(p);
7949 if (p->heredoc_indent > 0) {
7950 goto flush_str;
7951 }
7952 if (nextc(p) == -1) {
7953 if (str) {
7954 str = 0;
7955 }
7956 goto error;
7957 }
7958 } while (!whole_match_p(p, eos, len, indent));
7959 }
7960 else {
7961 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7962 newtok(p);
7963 if (c == '#') {
7964 int t = parser_peek_variable_name(p);
7965 if (p->heredoc_line_indent != -1) {
7966 if (p->heredoc_indent > p->heredoc_line_indent) {
7967 p->heredoc_indent = p->heredoc_line_indent;
7968 }
7969 p->heredoc_line_indent = -1;
7970 }
7971 if (t) return t;
7972 tokadd(p, '#');
7973 c = nextc(p);
7974 }
7975 do {
7976 pushback(p, c);
7977 enc = p->enc;
7978 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7979 if (p->eofp) goto error;
7980 goto restore;
7981 }
7982 if (c != '\n') {
7983 if (c == '\\') p->heredoc_line_indent = -1;
7984 flush:
7985 str = STR_NEW3(tok(p), toklen(p), enc, func);
7986 flush_str:
7987 set_yylval_str(str);
7988#ifndef RIPPER
7989 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7990#endif
7991 flush_string_content(p, enc);
7992 return tSTRING_CONTENT;
7993 }
7994 tokadd(p, nextc(p));
7995 if (p->heredoc_indent > 0) {
7996 lex_goto_eol(p);
7997 goto flush;
7998 }
7999 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
8000 if ((c = nextc(p)) == -1) goto error;
8001 } while (!whole_match_p(p, eos, len, indent));
8002 str = STR_NEW3(tok(p), toklen(p), enc, func);
8003 }
8004 dispatch_heredoc_end(p);
8005#ifdef RIPPER
8006 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
8007 yylval.val, str);
8008#endif
8009 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8010 token_flush(p);
8011 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
8012 set_yylval_str(str);
8013#ifndef RIPPER
8014 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8015#endif
8016 return tSTRING_CONTENT;
8017}
8018
8019#include "lex.c"
8020
8021static int
8022arg_ambiguous(struct parser_params *p, char c)
8023{
8024#ifndef RIPPER
8025 if (c == '/') {
8026 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
8027 }
8028 else {
8029 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
8030 }
8031#else
8032 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
8033#endif
8034 return TRUE;
8035}
8036
8037static ID
8038#ifndef RIPPER
8039formal_argument(struct parser_params *p, ID lhs)
8040#else
8041formal_argument(struct parser_params *p, VALUE lhs)
8042#endif
8043{
8044 ID id = get_id(lhs);
8045
8046 switch (id_type(id)) {
8047 case ID_LOCAL:
8048 break;
8049#ifndef RIPPER
8050# define ERR(mesg) yyerror0(mesg)
8051#else
8052# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
8053#endif
8054 case ID_CONST:
8055 ERR("formal argument cannot be a constant");
8056 return 0;
8057 case ID_INSTANCE:
8058 ERR("formal argument cannot be an instance variable");
8059 return 0;
8060 case ID_GLOBAL:
8061 ERR("formal argument cannot be a global variable");
8062 return 0;
8063 case ID_CLASS:
8064 ERR("formal argument cannot be a class variable");
8065 return 0;
8066 default:
8067 ERR("formal argument must be local variable");
8068 return 0;
8069#undef ERR
8070 }
8071 shadowing_lvar(p, id);
8072 return lhs;
8073}
8074
8075static int
8076lvar_defined(struct parser_params *p, ID id)
8077{
8078 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
8079}
8080
8081/* emacsen -*- hack */
8082static long
8083parser_encode_length(struct parser_params *p, const char *name, long len)
8084{
8085 long nlen;
8086
8087 if (len > 5 && name[nlen = len - 5] == '-') {
8088 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
8089 return nlen;
8090 }
8091 if (len > 4 && name[nlen = len - 4] == '-') {
8092 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
8093 return nlen;
8094 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
8095 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
8096 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
8097 return nlen;
8098 }
8099 return len;
8100}
8101
8102static void
8103parser_set_encode(struct parser_params *p, const char *name)
8104{
8105 int idx = rb_enc_find_index(name);
8106 rb_encoding *enc;
8107 VALUE excargs[3];
8108
8109 if (idx < 0) {
8110 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
8111 error:
8112 excargs[0] = rb_eArgError;
8113 excargs[2] = rb_make_backtrace();
8114 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
8115 rb_exc_raise(rb_make_exception(3, excargs));
8116 }
8117 enc = rb_enc_from_index(idx);
8118 if (!rb_enc_asciicompat(enc)) {
8119 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
8120 goto error;
8121 }
8122 p->enc = enc;
8123#ifndef RIPPER
8124 if (p->debug_lines) {
8125 VALUE lines = p->debug_lines;
8126 long i, n = RARRAY_LEN(lines);
8127 for (i = 0; i < n; ++i) {
8128 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
8129 }
8130 }
8131#endif
8132}
8133
8134static int
8135comment_at_top(struct parser_params *p)
8136{
8137 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
8138 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
8139 while (ptr < ptr_end) {
8140 if (!ISSPACE(*ptr)) return 0;
8141 ptr++;
8142 }
8143 return 1;
8144}
8145
8146typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
8147typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
8148
8149static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
8150
8151static void
8152magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
8153{
8154 if (!comment_at_top(p)) {
8155 return;
8156 }
8157 parser_set_encode(p, val);
8158}
8159
8160static int
8161parser_get_bool(struct parser_params *p, const char *name, const char *val)
8162{
8163 switch (*val) {
8164 case 't': case 'T':
8165 if (STRCASECMP(val, "true") == 0) {
8166 return TRUE;
8167 }
8168 break;
8169 case 'f': case 'F':
8170 if (STRCASECMP(val, "false") == 0) {
8171 return FALSE;
8172 }
8173 break;
8174 }
8175 return parser_invalid_pragma_value(p, name, val);
8176}
8177
8178static int
8179parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
8180{
8181 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
8182 return -1;
8183}
8184
8185static void
8186parser_set_token_info(struct parser_params *p, const char *name, const char *val)
8187{
8188 int b = parser_get_bool(p, name, val);
8189 if (b >= 0) p->token_info_enabled = b;
8190}
8191
8192static void
8193parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
8194{
8195 int b;
8196
8197 if (p->token_seen) {
8198 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
8199 return;
8200 }
8201
8202 b = parser_get_bool(p, name, val);
8203 if (b < 0) return;
8204
8205 if (!p->compile_option)
8206 p->compile_option = rb_obj_hide(rb_ident_hash_new());
8207 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
8208 RBOOL(b));
8209}
8210
8211static void
8212parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
8213{
8214 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
8215 if (*s == ' ' || *s == '\t') continue;
8216 if (*s == '#') break;
8217 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
8218 return;
8219 }
8220
8221 switch (*val) {
8222 case 'n': case 'N':
8223 if (STRCASECMP(val, "none") == 0) {
8224 p->ctxt.shareable_constant_value = shareable_none;
8225 return;
8226 }
8227 break;
8228 case 'l': case 'L':
8229 if (STRCASECMP(val, "literal") == 0) {
8230 p->ctxt.shareable_constant_value = shareable_literal;
8231 return;
8232 }
8233 break;
8234 case 'e': case 'E':
8235 if (STRCASECMP(val, "experimental_copy") == 0) {
8236 p->ctxt.shareable_constant_value = shareable_copy;
8237 return;
8238 }
8239 if (STRCASECMP(val, "experimental_everything") == 0) {
8240 p->ctxt.shareable_constant_value = shareable_everything;
8241 return;
8242 }
8243 break;
8244 }
8245 parser_invalid_pragma_value(p, name, val);
8246}
8247
8248# if WARN_PAST_SCOPE
8249static void
8250parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
8251{
8252 int b = parser_get_bool(p, name, val);
8253 if (b >= 0) p->past_scope_enabled = b;
8254}
8255# endif
8256
8257struct magic_comment {
8258 const char *name;
8259 rb_magic_comment_setter_t func;
8260 rb_magic_comment_length_t length;
8261};
8262
8263static const struct magic_comment magic_comments[] = {
8264 {"coding", magic_comment_encoding, parser_encode_length},
8265 {"encoding", magic_comment_encoding, parser_encode_length},
8266 {"frozen_string_literal", parser_set_compile_option_flag},
8267 {"shareable_constant_value", parser_set_shareable_constant_value},
8268 {"warn_indent", parser_set_token_info},
8269# if WARN_PAST_SCOPE
8270 {"warn_past_scope", parser_set_past_scope},
8271# endif
8272};
8273
8274static const char *
8275magic_comment_marker(const char *str, long len)
8276{
8277 long i = 2;
8278
8279 while (i < len) {
8280 switch (str[i]) {
8281 case '-':
8282 if (str[i-1] == '*' && str[i-2] == '-') {
8283 return str + i + 1;
8284 }
8285 i += 2;
8286 break;
8287 case '*':
8288 if (i + 1 >= len) return 0;
8289 if (str[i+1] != '-') {
8290 i += 4;
8291 }
8292 else if (str[i-1] != '-') {
8293 i += 2;
8294 }
8295 else {
8296 return str + i + 2;
8297 }
8298 break;
8299 default:
8300 i += 3;
8301 break;
8302 }
8303 }
8304 return 0;
8305}
8306
8307static int
8308parser_magic_comment(struct parser_params *p, const char *str, long len)
8309{
8310 int indicator = 0;
8311 VALUE name = 0, val = 0;
8312 const char *beg, *end, *vbeg, *vend;
8313#define str_copy(_s, _p, _n) ((_s) \
8314 ? (void)(rb_str_resize((_s), (_n)), \
8315 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
8316 : (void)((_s) = STR_NEW((_p), (_n))))
8317
8318 if (len <= 7) return FALSE;
8319 if (!!(beg = magic_comment_marker(str, len))) {
8320 if (!(end = magic_comment_marker(beg, str + len - beg)))
8321 return FALSE;
8322 indicator = TRUE;
8323 str = beg;
8324 len = end - beg - 3;
8325 }
8326
8327 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
8328 while (len > 0) {
8329 const struct magic_comment *mc = magic_comments;
8330 char *s;
8331 int i;
8332 long n = 0;
8333
8334 for (; len > 0 && *str; str++, --len) {
8335 switch (*str) {
8336 case '\'': case '"': case ':': case ';':
8337 continue;
8338 }
8339 if (!ISSPACE(*str)) break;
8340 }
8341 for (beg = str; len > 0; str++, --len) {
8342 switch (*str) {
8343 case '\'': case '"': case ':': case ';':
8344 break;
8345 default:
8346 if (ISSPACE(*str)) break;
8347 continue;
8348 }
8349 break;
8350 }
8351 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
8352 if (!len) break;
8353 if (*str != ':') {
8354 if (!indicator) return FALSE;
8355 continue;
8356 }
8357
8358 do str++; while (--len > 0 && ISSPACE(*str));
8359 if (!len) break;
8360 if (*str == '"') {
8361 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
8362 if (*str == '\\') {
8363 --len;
8364 ++str;
8365 }
8366 }
8367 vend = str;
8368 if (len) {
8369 --len;
8370 ++str;
8371 }
8372 }
8373 else {
8374 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
8375 vend = str;
8376 }
8377 if (indicator) {
8378 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
8379 }
8380 else {
8381 while (len > 0 && (ISSPACE(*str))) --len, str++;
8382 if (len) return FALSE;
8383 }
8384
8385 n = end - beg;
8386 str_copy(name, beg, n);
8387 s = RSTRING_PTR(name);
8388 for (i = 0; i < n; ++i) {
8389 if (s[i] == '-') s[i] = '_';
8390 }
8391 do {
8392 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
8393 n = vend - vbeg;
8394 if (mc->length) {
8395 n = (*mc->length)(p, vbeg, n);
8396 }
8397 str_copy(val, vbeg, n);
8398 (*mc->func)(p, mc->name, RSTRING_PTR(val));
8399 break;
8400 }
8401 } while (++mc < magic_comments + numberof(magic_comments));
8402#ifdef RIPPER
8403 str_copy(val, vbeg, vend - vbeg);
8404 dispatch2(magic_comment, name, val);
8405#endif
8406 }
8407
8408 return TRUE;
8409}
8410
8411static void
8412set_file_encoding(struct parser_params *p, const char *str, const char *send)
8413{
8414 int sep = 0;
8415 const char *beg = str;
8416 VALUE s;
8417
8418 for (;;) {
8419 if (send - str <= 6) return;
8420 switch (str[6]) {
8421 case 'C': case 'c': str += 6; continue;
8422 case 'O': case 'o': str += 5; continue;
8423 case 'D': case 'd': str += 4; continue;
8424 case 'I': case 'i': str += 3; continue;
8425 case 'N': case 'n': str += 2; continue;
8426 case 'G': case 'g': str += 1; continue;
8427 case '=': case ':':
8428 sep = 1;
8429 str += 6;
8430 break;
8431 default:
8432 str += 6;
8433 if (ISSPACE(*str)) break;
8434 continue;
8435 }
8436 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8437 sep = 0;
8438 }
8439 for (;;) {
8440 do {
8441 if (++str >= send) return;
8442 } while (ISSPACE(*str));
8443 if (sep) break;
8444 if (*str != '=' && *str != ':') return;
8445 sep = 1;
8446 str++;
8447 }
8448 beg = str;
8449 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8450 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8451 parser_set_encode(p, RSTRING_PTR(s));
8452 rb_str_resize(s, 0);
8453}
8454
8455static void
8456parser_prepare(struct parser_params *p)
8457{
8458 int c = nextc(p);
8459 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8460 switch (c) {
8461 case '#':
8462 if (peek(p, '!')) p->has_shebang = 1;
8463 break;
8464 case 0xef: /* UTF-8 BOM marker */
8465 if (p->lex.pend - p->lex.pcur >= 2 &&
8466 (unsigned char)p->lex.pcur[0] == 0xbb &&
8467 (unsigned char)p->lex.pcur[1] == 0xbf) {
8468 p->enc = rb_utf8_encoding();
8469 p->lex.pcur += 2;
8470 p->lex.pbeg = p->lex.pcur;
8471 return;
8472 }
8473 break;
8474 case EOF:
8475 return;
8476 }
8477 pushback(p, c);
8478 p->enc = rb_enc_get(p->lex.lastline);
8479}
8480
8481#ifndef RIPPER
8482#define ambiguous_operator(tok, op, syn) ( \
8483 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8484 rb_warning0("even though it seems like "syn""))
8485#else
8486#define ambiguous_operator(tok, op, syn) \
8487 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8488#endif
8489#define warn_balanced(tok, op, syn) ((void) \
8490 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8491 space_seen && !ISSPACE(c) && \
8492 (ambiguous_operator(tok, op, syn), 0)), \
8493 (enum yytokentype)(tok))
8494
8495static VALUE
8496parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8497{
8498 VALUE v;
8499 char *point = &str[seen_point];
8500 size_t fraclen = len-seen_point-1;
8501 memmove(point, point+1, fraclen+1);
8502 v = rb_cstr_to_inum(str, 10, FALSE);
8503 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8504}
8505
8506static enum yytokentype
8507no_digits(struct parser_params *p)
8508{
8509 yyerror0("numeric literal without digits");
8510 if (peek(p, '_')) nextc(p);
8511 /* dummy 0, for tUMINUS_NUM at numeric */
8512 return set_integer_literal(p, INT2FIX(0), 0);
8513}
8514
8515static enum yytokentype
8516parse_numeric(struct parser_params *p, int c)
8517{
8518 int is_float, seen_point, seen_e, nondigit;
8519 int suffix;
8520
8521 is_float = seen_point = seen_e = nondigit = 0;
8522 SET_LEX_STATE(EXPR_END);
8523 newtok(p);
8524 if (c == '-' || c == '+') {
8525 tokadd(p, c);
8526 c = nextc(p);
8527 }
8528 if (c == '0') {
8529 int start = toklen(p);
8530 c = nextc(p);
8531 if (c == 'x' || c == 'X') {
8532 /* hexadecimal */
8533 c = nextc(p);
8534 if (c != -1 && ISXDIGIT(c)) {
8535 do {
8536 if (c == '_') {
8537 if (nondigit) break;
8538 nondigit = c;
8539 continue;
8540 }
8541 if (!ISXDIGIT(c)) break;
8542 nondigit = 0;
8543 tokadd(p, c);
8544 } while ((c = nextc(p)) != -1);
8545 }
8546 pushback(p, c);
8547 tokfix(p);
8548 if (toklen(p) == start) {
8549 return no_digits(p);
8550 }
8551 else if (nondigit) goto trailing_uc;
8552 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8553 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8554 }
8555 if (c == 'b' || c == 'B') {
8556 /* binary */
8557 c = nextc(p);
8558 if (c == '0' || c == '1') {
8559 do {
8560 if (c == '_') {
8561 if (nondigit) break;
8562 nondigit = c;
8563 continue;
8564 }
8565 if (c != '0' && c != '1') break;
8566 nondigit = 0;
8567 tokadd(p, c);
8568 } while ((c = nextc(p)) != -1);
8569 }
8570 pushback(p, c);
8571 tokfix(p);
8572 if (toklen(p) == start) {
8573 return no_digits(p);
8574 }
8575 else if (nondigit) goto trailing_uc;
8576 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8577 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8578 }
8579 if (c == 'd' || c == 'D') {
8580 /* decimal */
8581 c = nextc(p);
8582 if (c != -1 && ISDIGIT(c)) {
8583 do {
8584 if (c == '_') {
8585 if (nondigit) break;
8586 nondigit = c;
8587 continue;
8588 }
8589 if (!ISDIGIT(c)) break;
8590 nondigit = 0;
8591 tokadd(p, c);
8592 } while ((c = nextc(p)) != -1);
8593 }
8594 pushback(p, c);
8595 tokfix(p);
8596 if (toklen(p) == start) {
8597 return no_digits(p);
8598 }
8599 else if (nondigit) goto trailing_uc;
8600 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8601 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8602 }
8603 if (c == '_') {
8604 /* 0_0 */
8605 goto octal_number;
8606 }
8607 if (c == 'o' || c == 'O') {
8608 /* prefixed octal */
8609 c = nextc(p);
8610 if (c == -1 || c == '_' || !ISDIGIT(c)) {
8611 return no_digits(p);
8612 }
8613 }
8614 if (c >= '0' && c <= '7') {
8615 /* octal */
8616 octal_number:
8617 do {
8618 if (c == '_') {
8619 if (nondigit) break;
8620 nondigit = c;
8621 continue;
8622 }
8623 if (c < '0' || c > '9') break;
8624 if (c > '7') goto invalid_octal;
8625 nondigit = 0;
8626 tokadd(p, c);
8627 } while ((c = nextc(p)) != -1);
8628 if (toklen(p) > start) {
8629 pushback(p, c);
8630 tokfix(p);
8631 if (nondigit) goto trailing_uc;
8632 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8633 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8634 }
8635 if (nondigit) {
8636 pushback(p, c);
8637 goto trailing_uc;
8638 }
8639 }
8640 if (c > '7' && c <= '9') {
8641 invalid_octal:
8642 yyerror0("Invalid octal digit");
8643 }
8644 else if (c == '.' || c == 'e' || c == 'E') {
8645 tokadd(p, '0');
8646 }
8647 else {
8648 pushback(p, c);
8649 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8650 return set_integer_literal(p, INT2FIX(0), suffix);
8651 }
8652 }
8653
8654 for (;;) {
8655 switch (c) {
8656 case '0': case '1': case '2': case '3': case '4':
8657 case '5': case '6': case '7': case '8': case '9':
8658 nondigit = 0;
8659 tokadd(p, c);
8660 break;
8661
8662 case '.':
8663 if (nondigit) goto trailing_uc;
8664 if (seen_point || seen_e) {
8665 goto decode_num;
8666 }
8667 else {
8668 int c0 = nextc(p);
8669 if (c0 == -1 || !ISDIGIT(c0)) {
8670 pushback(p, c0);
8671 goto decode_num;
8672 }
8673 c = c0;
8674 }
8675 seen_point = toklen(p);
8676 tokadd(p, '.');
8677 tokadd(p, c);
8678 is_float++;
8679 nondigit = 0;
8680 break;
8681
8682 case 'e':
8683 case 'E':
8684 if (nondigit) {
8685 pushback(p, c);
8686 c = nondigit;
8687 goto decode_num;
8688 }
8689 if (seen_e) {
8690 goto decode_num;
8691 }
8692 nondigit = c;
8693 c = nextc(p);
8694 if (c != '-' && c != '+' && !ISDIGIT(c)) {
8695 pushback(p, c);
8696 nondigit = 0;
8697 goto decode_num;
8698 }
8699 tokadd(p, nondigit);
8700 seen_e++;
8701 is_float++;
8702 tokadd(p, c);
8703 nondigit = (c == '-' || c == '+') ? c : 0;
8704 break;
8705
8706 case '_': /* `_' in number just ignored */
8707 if (nondigit) goto decode_num;
8708 nondigit = c;
8709 break;
8710
8711 default:
8712 goto decode_num;
8713 }
8714 c = nextc(p);
8715 }
8716
8717 decode_num:
8718 pushback(p, c);
8719 if (nondigit) {
8720 trailing_uc:
8721 literal_flush(p, p->lex.pcur - 1);
8722 YYLTYPE loc = RUBY_INIT_YYLLOC();
8723 compile_error(p, "trailing `%c' in number", nondigit);
8724 parser_show_error_line(p, &loc);
8725 }
8726 tokfix(p);
8727 if (is_float) {
8728 enum yytokentype type = tFLOAT;
8729 VALUE v;
8730
8731 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8732 if (suffix & NUM_SUFFIX_R) {
8733 type = tRATIONAL;
8734 v = parse_rational(p, tok(p), toklen(p), seen_point);
8735 }
8736 else {
8737 double d = strtod(tok(p), 0);
8738 if (errno == ERANGE) {
8739 rb_warning1("Float %s out of range", WARN_S(tok(p)));
8740 errno = 0;
8741 }
8742 v = DBL2NUM(d);
8743 }
8744 return set_number_literal(p, v, type, suffix);
8745 }
8746 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8747 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8748}
8749
8750static enum yytokentype
8751parse_qmark(struct parser_params *p, int space_seen)
8752{
8753 rb_encoding *enc;
8754 register int c;
8755 VALUE lit;
8756
8757 if (IS_END()) {
8758 SET_LEX_STATE(EXPR_VALUE);
8759 return '?';
8760 }
8761 c = nextc(p);
8762 if (c == -1) {
8763 compile_error(p, "incomplete character syntax");
8764 return 0;
8765 }
8766 if (rb_enc_isspace(c, p->enc)) {
8767 if (!IS_ARG()) {
8768 int c2 = escaped_control_code(c);
8769 if (c2) {
8770 WARN_SPACE_CHAR(c2, "?");
8771 }
8772 }
8773 ternary:
8774 pushback(p, c);
8775 SET_LEX_STATE(EXPR_VALUE);
8776 return '?';
8777 }
8778 newtok(p);
8779 enc = p->enc;
8780 if (!parser_isascii(p)) {
8781 if (tokadd_mbchar(p, c) == -1) return 0;
8782 }
8783 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8784 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8785 if (space_seen) {
8786 const char *start = p->lex.pcur - 1, *ptr = start;
8787 do {
8788 int n = parser_precise_mbclen(p, ptr);
8789 if (n < 0) return -1;
8790 ptr += n;
8791 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8792 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8793 " a conditional operator, put a space after `?'",
8794 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8795 }
8796 goto ternary;
8797 }
8798 else if (c == '\\') {
8799 if (peek(p, 'u')) {
8800 nextc(p);
8801 enc = rb_utf8_encoding();
8802 tokadd_utf8(p, &enc, -1, 0, 0);
8803 }
8804 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8805 nextc(p);
8806 if (tokadd_mbchar(p, c) == -1) return 0;
8807 }
8808 else {
8809 c = read_escape(p, 0, &enc);
8810 tokadd(p, c);
8811 }
8812 }
8813 else {
8814 tokadd(p, c);
8815 }
8816 tokfix(p);
8817 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8818 set_yylval_str(lit);
8819 SET_LEX_STATE(EXPR_END);
8820 return tCHAR;
8821}
8822
8823static enum yytokentype
8824parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8825{
8826 register int c;
8827 const char *ptok = p->lex.pcur;
8828
8829 if (IS_BEG()) {
8830 int term;
8831 int paren;
8832
8833 c = nextc(p);
8834 quotation:
8835 if (c == -1) goto unterminated;
8836 if (!ISALNUM(c)) {
8837 term = c;
8838 if (!ISASCII(c)) goto unknown;
8839 c = 'Q';
8840 }
8841 else {
8842 term = nextc(p);
8843 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8844 unknown:
8845 pushback(p, term);
8846 c = parser_precise_mbclen(p, p->lex.pcur);
8847 if (c < 0) return 0;
8848 p->lex.pcur += c;
8849 yyerror0("unknown type of %string");
8850 return 0;
8851 }
8852 }
8853 if (term == -1) {
8854 unterminated:
8855 compile_error(p, "unterminated quoted string meets end of file");
8856 return 0;
8857 }
8858 paren = term;
8859 if (term == '(') term = ')';
8860 else if (term == '[') term = ']';
8861 else if (term == '{') term = '}';
8862 else if (term == '<') term = '>';
8863 else paren = 0;
8864
8865 p->lex.ptok = ptok-1;
8866 switch (c) {
8867 case 'Q':
8868 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8869 return tSTRING_BEG;
8870
8871 case 'q':
8872 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8873 return tSTRING_BEG;
8874
8875 case 'W':
8876 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8877 return tWORDS_BEG;
8878
8879 case 'w':
8880 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8881 return tQWORDS_BEG;
8882
8883 case 'I':
8884 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8885 return tSYMBOLS_BEG;
8886
8887 case 'i':
8888 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8889 return tQSYMBOLS_BEG;
8890
8891 case 'x':
8892 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8893 return tXSTRING_BEG;
8894
8895 case 'r':
8896 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8897 return tREGEXP_BEG;
8898
8899 case 's':
8900 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8901 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8902 return tSYMBEG;
8903
8904 default:
8905 yyerror0("unknown type of %string");
8906 return 0;
8907 }
8908 }
8909 if ((c = nextc(p)) == '=') {
8910 set_yylval_id('%');
8911 SET_LEX_STATE(EXPR_BEG);
8912 return tOP_ASGN;
8913 }
8914 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8915 goto quotation;
8916 }
8917 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8918 pushback(p, c);
8919 return warn_balanced('%', "%%", "string literal");
8920}
8921
8922static int
8923tokadd_ident(struct parser_params *p, int c)
8924{
8925 do {
8926 if (tokadd_mbchar(p, c) == -1) return -1;
8927 c = nextc(p);
8928 } while (parser_is_identchar(p));
8929 pushback(p, c);
8930 return 0;
8931}
8932
8933static ID
8934tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8935{
8936 ID ident = TOK_INTERN();
8937
8938 set_yylval_name(ident);
8939
8940 return ident;
8941}
8942
8943static int
8944parse_numvar(struct parser_params *p)
8945{
8946 size_t len;
8947 int overflow;
8948 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8949 const unsigned long nth_ref_max =
8950 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8951 /* NTH_REF is left-shifted to be ORed with back-ref flag and
8952 * turned into a Fixnum, in compile.c */
8953
8954 if (overflow || n > nth_ref_max) {
8955 /* compile_error()? */
8956 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8957 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8958 }
8959 else {
8960 return (int)n;
8961 }
8962}
8963
8964static enum yytokentype
8965parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8966{
8967 const char *ptr = p->lex.pcur;
8968 register int c;
8969
8970 SET_LEX_STATE(EXPR_END);
8971 p->lex.ptok = ptr - 1; /* from '$' */
8972 newtok(p);
8973 c = nextc(p);
8974 switch (c) {
8975 case '_': /* $_: last read line string */
8976 c = nextc(p);
8977 if (parser_is_identchar(p)) {
8978 tokadd(p, '$');
8979 tokadd(p, '_');
8980 break;
8981 }
8982 pushback(p, c);
8983 c = '_';
8984 /* fall through */
8985 case '~': /* $~: match-data */
8986 case '*': /* $*: argv */
8987 case '$': /* $$: pid */
8988 case '?': /* $?: last status */
8989 case '!': /* $!: error string */
8990 case '@': /* $@: error position */
8991 case '/': /* $/: input record separator */
8992 case '\\': /* $\: output record separator */
8993 case ';': /* $;: field separator */
8994 case ',': /* $,: output field separator */
8995 case '.': /* $.: last read line number */
8996 case '=': /* $=: ignorecase */
8997 case ':': /* $:: load path */
8998 case '<': /* $<: reading filename */
8999 case '>': /* $>: default output handle */
9000 case '\"': /* $": already loaded files */
9001 tokadd(p, '$');
9002 tokadd(p, c);
9003 goto gvar;
9004
9005 case '-':
9006 tokadd(p, '$');
9007 tokadd(p, c);
9008 c = nextc(p);
9009 if (parser_is_identchar(p)) {
9010 if (tokadd_mbchar(p, c) == -1) return 0;
9011 }
9012 else {
9013 pushback(p, c);
9014 pushback(p, '-');
9015 return '$';
9016 }
9017 gvar:
9018 set_yylval_name(TOK_INTERN());
9019 return tGVAR;
9020
9021 case '&': /* $&: last match */
9022 case '`': /* $`: string before last match */
9023 case '\'': /* $': string after last match */
9024 case '+': /* $+: string matches last paren. */
9025 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
9026 tokadd(p, '$');
9027 tokadd(p, c);
9028 goto gvar;
9029 }
9030 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
9031 return tBACK_REF;
9032
9033 case '1': case '2': case '3':
9034 case '4': case '5': case '6':
9035 case '7': case '8': case '9':
9036 tokadd(p, '$');
9037 do {
9038 tokadd(p, c);
9039 c = nextc(p);
9040 } while (c != -1 && ISDIGIT(c));
9041 pushback(p, c);
9042 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
9043 tokfix(p);
9044 c = parse_numvar(p);
9045 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
9046 return tNTH_REF;
9047
9048 default:
9049 if (!parser_is_identchar(p)) {
9050 YYLTYPE loc = RUBY_INIT_YYLLOC();
9051 if (c == -1 || ISSPACE(c)) {
9052 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
9053 }
9054 else {
9055 pushback(p, c);
9056 compile_error(p, "`$%c' is not allowed as a global variable name", c);
9057 }
9058 parser_show_error_line(p, &loc);
9059 set_yylval_noname();
9060 return tGVAR;
9061 }
9062 /* fall through */
9063 case '0':
9064 tokadd(p, '$');
9065 }
9066
9067 if (tokadd_ident(p, c)) return 0;
9068 SET_LEX_STATE(EXPR_END);
9069 tokenize_ident(p, last_state);
9070 return tGVAR;
9071}
9072
9073#ifndef RIPPER
9074static bool
9075parser_numbered_param(struct parser_params *p, int n)
9076{
9077 if (n < 0) return false;
9078
9079 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
9080 return false;
9081 }
9082 if (p->max_numparam == ORDINAL_PARAM) {
9083 compile_error(p, "ordinary parameter is defined");
9084 return false;
9085 }
9086 struct vtable *args = p->lvtbl->args;
9087 if (p->max_numparam < n) {
9088 p->max_numparam = n;
9089 }
9090 while (n > args->pos) {
9091 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
9092 }
9093 return true;
9094}
9095#endif
9096
9097static enum yytokentype
9098parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
9099{
9100 const char *ptr = p->lex.pcur;
9101 enum yytokentype result = tIVAR;
9102 register int c = nextc(p);
9103 YYLTYPE loc;
9104
9105 p->lex.ptok = ptr - 1; /* from '@' */
9106 newtok(p);
9107 tokadd(p, '@');
9108 if (c == '@') {
9109 result = tCVAR;
9110 tokadd(p, '@');
9111 c = nextc(p);
9112 }
9113 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
9114 if (c == -1 || !parser_is_identchar(p)) {
9115 pushback(p, c);
9116 RUBY_SET_YYLLOC(loc);
9117 if (result == tIVAR) {
9118 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
9119 }
9120 else {
9121 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
9122 }
9123 parser_show_error_line(p, &loc);
9124 set_yylval_noname();
9125 SET_LEX_STATE(EXPR_END);
9126 return result;
9127 }
9128 else if (ISDIGIT(c)) {
9129 pushback(p, c);
9130 RUBY_SET_YYLLOC(loc);
9131 if (result == tIVAR) {
9132 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
9133 }
9134 else {
9135 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
9136 }
9137 parser_show_error_line(p, &loc);
9138 set_yylval_noname();
9139 SET_LEX_STATE(EXPR_END);
9140 return result;
9141 }
9142
9143 if (tokadd_ident(p, c)) return 0;
9144 tokenize_ident(p, last_state);
9145 return result;
9146}
9147
9148static enum yytokentype
9149parse_ident(struct parser_params *p, int c, int cmd_state)
9150{
9151 enum yytokentype result;
9152 int mb = ENC_CODERANGE_7BIT;
9153 const enum lex_state_e last_state = p->lex.state;
9154 ID ident;
9155
9156 do {
9157 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
9158 if (tokadd_mbchar(p, c) == -1) return 0;
9159 c = nextc(p);
9160 } while (parser_is_identchar(p));
9161 if ((c == '!' || c == '?') && !peek(p, '=')) {
9162 result = tFID;
9163 tokadd(p, c);
9164 }
9165 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
9166 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
9167 result = tIDENTIFIER;
9168 tokadd(p, c);
9169 }
9170 else {
9171 result = tCONSTANT; /* assume provisionally */
9172 pushback(p, c);
9173 }
9174 tokfix(p);
9175
9176 if (IS_LABEL_POSSIBLE()) {
9177 if (IS_LABEL_SUFFIX(0)) {
9178 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
9179 nextc(p);
9180 set_yylval_name(TOK_INTERN());
9181 return tLABEL;
9182 }
9183 }
9184 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
9185 const struct kwtable *kw;
9186
9187 /* See if it is a reserved word. */
9188 kw = rb_reserved_word(tok(p), toklen(p));
9189 if (kw) {
9190 enum lex_state_e state = p->lex.state;
9191 if (IS_lex_state_for(state, EXPR_FNAME)) {
9192 SET_LEX_STATE(EXPR_ENDFN);
9193 set_yylval_name(rb_intern2(tok(p), toklen(p)));
9194 return kw->id[0];
9195 }
9196 SET_LEX_STATE(kw->state);
9197 if (IS_lex_state(EXPR_BEG)) {
9198 p->command_start = TRUE;
9199 }
9200 if (kw->id[0] == keyword_do) {
9201 if (lambda_beginning_p()) {
9202 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
9203 return keyword_do_LAMBDA;
9204 }
9205 if (COND_P()) return keyword_do_cond;
9206 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
9207 return keyword_do_block;
9208 return keyword_do;
9209 }
9210 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
9211 return kw->id[0];
9212 else {
9213 if (kw->id[0] != kw->id[1])
9214 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
9215 return kw->id[1];
9216 }
9217 }
9218 }
9219
9220 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
9221 if (cmd_state) {
9222 SET_LEX_STATE(EXPR_CMDARG);
9223 }
9224 else {
9225 SET_LEX_STATE(EXPR_ARG);
9226 }
9227 }
9228 else if (p->lex.state == EXPR_FNAME) {
9229 SET_LEX_STATE(EXPR_ENDFN);
9230 }
9231 else {
9232 SET_LEX_STATE(EXPR_END);
9233 }
9234
9235 ident = tokenize_ident(p, last_state);
9236 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
9237 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
9238 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
9239 lvar_defined(p, ident)) {
9240 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
9241 }
9242 return result;
9243}
9244
9245static enum yytokentype
9246parser_yylex(struct parser_params *p)
9247{
9248 register int c;
9249 int space_seen = 0;
9250 int cmd_state;
9251 int label;
9252 enum lex_state_e last_state;
9253 int fallthru = FALSE;
9254 int token_seen = p->token_seen;
9255
9256 if (p->lex.strterm) {
9257 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
9258 return here_document(p, &p->lex.strterm->u.heredoc);
9259 }
9260 else {
9261 token_flush(p);
9262 return parse_string(p, &p->lex.strterm->u.literal);
9263 }
9264 }
9265 cmd_state = p->command_start;
9266 p->command_start = FALSE;
9267 p->token_seen = TRUE;
9268 retry:
9269 last_state = p->lex.state;
9270#ifndef RIPPER
9271 token_flush(p);
9272#endif
9273 switch (c = nextc(p)) {
9274 case '\0': /* NUL */
9275 case '\004': /* ^D */
9276 case '\032': /* ^Z */
9277 case -1: /* end of script. */
9278 return 0;
9279
9280 /* white spaces */
9281 case '\r':
9282 if (!p->cr_seen) {
9283 p->cr_seen = TRUE;
9284 /* carried over with p->lex.nextline for nextc() */
9285 rb_warn0("encountered \\r in middle of line, treated as a mere space");
9286 }
9287 /* fall through */
9288 case ' ': case '\t': case '\f':
9289 case '\13': /* '\v' */
9290 space_seen = 1;
9291#ifdef RIPPER
9292 while ((c = nextc(p))) {
9293 switch (c) {
9294 case ' ': case '\t': case '\f': case '\r':
9295 case '\13': /* '\v' */
9296 break;
9297 default:
9298 goto outofloop;
9299 }
9300 }
9301 outofloop:
9302 pushback(p, c);
9303 dispatch_scan_event(p, tSP);
9304#endif
9305 goto retry;
9306
9307 case '#': /* it's a comment */
9308 p->token_seen = token_seen;
9309 /* no magic_comment in shebang line */
9310 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
9311 if (comment_at_top(p)) {
9312 set_file_encoding(p, p->lex.pcur, p->lex.pend);
9313 }
9314 }
9315 lex_goto_eol(p);
9316 dispatch_scan_event(p, tCOMMENT);
9317 fallthru = TRUE;
9318 /* fall through */
9319 case '\n':
9320 p->token_seen = token_seen;
9321 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
9322 !IS_lex_state(EXPR_LABELED));
9323 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
9324 if (!fallthru) {
9325 dispatch_scan_event(p, tIGNORED_NL);
9326 }
9327 fallthru = FALSE;
9328 if (!c && p->ctxt.in_kwarg) {
9329 goto normal_newline;
9330 }
9331 goto retry;
9332 }
9333 while (1) {
9334 switch (c = nextc(p)) {
9335 case ' ': case '\t': case '\f': case '\r':
9336 case '\13': /* '\v' */
9337 space_seen = 1;
9338 break;
9339 case '#':
9340 pushback(p, c);
9341 if (space_seen) dispatch_scan_event(p, tSP);
9342 goto retry;
9343 case '&':
9344 case '.': {
9345 dispatch_delayed_token(p, tIGNORED_NL);
9346 if (peek(p, '.') == (c == '&')) {
9347 pushback(p, c);
9348 dispatch_scan_event(p, tSP);
9349 goto retry;
9350 }
9351 }
9352 default:
9353 p->ruby_sourceline--;
9354 p->lex.nextline = p->lex.lastline;
9355 case -1: /* EOF no decrement*/
9356#ifndef RIPPER
9357 if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
9358 p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
9359 p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
9360 pushback(p, 1); /* always pushback */
9361 p->lex.ptok = p->lex.pcur;
9362#else
9363 lex_goto_eol(p);
9364 if (c != -1) {
9365 p->lex.ptok = p->lex.pcur;
9366 }
9367#endif
9368 goto normal_newline;
9369 }
9370 }
9371 normal_newline:
9372 p->command_start = TRUE;
9373 SET_LEX_STATE(EXPR_BEG);
9374 return '\n';
9375
9376 case '*':
9377 if ((c = nextc(p)) == '*') {
9378 if ((c = nextc(p)) == '=') {
9379 set_yylval_id(idPow);
9380 SET_LEX_STATE(EXPR_BEG);
9381 return tOP_ASGN;
9382 }
9383 pushback(p, c);
9384 if (IS_SPCARG(c)) {
9385 rb_warning0("`**' interpreted as argument prefix");
9386 c = tDSTAR;
9387 }
9388 else if (IS_BEG()) {
9389 c = tDSTAR;
9390 }
9391 else {
9392 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
9393 }
9394 }
9395 else {
9396 if (c == '=') {
9397 set_yylval_id('*');
9398 SET_LEX_STATE(EXPR_BEG);
9399 return tOP_ASGN;
9400 }
9401 pushback(p, c);
9402 if (IS_SPCARG(c)) {
9403 rb_warning0("`*' interpreted as argument prefix");
9404 c = tSTAR;
9405 }
9406 else if (IS_BEG()) {
9407 c = tSTAR;
9408 }
9409 else {
9410 c = warn_balanced('*', "*", "argument prefix");
9411 }
9412 }
9413 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9414 return c;
9415
9416 case '!':
9417 c = nextc(p);
9418 if (IS_AFTER_OPERATOR()) {
9419 SET_LEX_STATE(EXPR_ARG);
9420 if (c == '@') {
9421 return '!';
9422 }
9423 }
9424 else {
9425 SET_LEX_STATE(EXPR_BEG);
9426 }
9427 if (c == '=') {
9428 return tNEQ;
9429 }
9430 if (c == '~') {
9431 return tNMATCH;
9432 }
9433 pushback(p, c);
9434 return '!';
9435
9436 case '=':
9437 if (was_bol(p)) {
9438 /* skip embedded rd document */
9439 if (word_match_p(p, "begin", 5)) {
9440 int first_p = TRUE;
9441
9442 lex_goto_eol(p);
9443 dispatch_scan_event(p, tEMBDOC_BEG);
9444 for (;;) {
9445 lex_goto_eol(p);
9446 if (!first_p) {
9447 dispatch_scan_event(p, tEMBDOC);
9448 }
9449 first_p = FALSE;
9450 c = nextc(p);
9451 if (c == -1) {
9452 compile_error(p, "embedded document meets end of file");
9453 return 0;
9454 }
9455 if (c == '=' && word_match_p(p, "end", 3)) {
9456 break;
9457 }
9458 pushback(p, c);
9459 }
9460 lex_goto_eol(p);
9461 dispatch_scan_event(p, tEMBDOC_END);
9462 goto retry;
9463 }
9464 }
9465
9466 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9467 if ((c = nextc(p)) == '=') {
9468 if ((c = nextc(p)) == '=') {
9469 return tEQQ;
9470 }
9471 pushback(p, c);
9472 return tEQ;
9473 }
9474 if (c == '~') {
9475 return tMATCH;
9476 }
9477 else if (c == '>') {
9478 return tASSOC;
9479 }
9480 pushback(p, c);
9481 return '=';
9482
9483 case '<':
9484 c = nextc(p);
9485 if (c == '<' &&
9486 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9487 !IS_END() &&
9488 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9489 int token = heredoc_identifier(p);
9490 if (token) return token < 0 ? 0 : token;
9491 }
9492 if (IS_AFTER_OPERATOR()) {
9493 SET_LEX_STATE(EXPR_ARG);
9494 }
9495 else {
9496 if (IS_lex_state(EXPR_CLASS))
9497 p->command_start = TRUE;
9498 SET_LEX_STATE(EXPR_BEG);
9499 }
9500 if (c == '=') {
9501 if ((c = nextc(p)) == '>') {
9502 return tCMP;
9503 }
9504 pushback(p, c);
9505 return tLEQ;
9506 }
9507 if (c == '<') {
9508 if ((c = nextc(p)) == '=') {
9509 set_yylval_id(idLTLT);
9510 SET_LEX_STATE(EXPR_BEG);
9511 return tOP_ASGN;
9512 }
9513 pushback(p, c);
9514 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9515 }
9516 pushback(p, c);
9517 return '<';
9518
9519 case '>':
9520 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9521 if ((c = nextc(p)) == '=') {
9522 return tGEQ;
9523 }
9524 if (c == '>') {
9525 if ((c = nextc(p)) == '=') {
9526 set_yylval_id(idGTGT);
9527 SET_LEX_STATE(EXPR_BEG);
9528 return tOP_ASGN;
9529 }
9530 pushback(p, c);
9531 return tRSHFT;
9532 }
9533 pushback(p, c);
9534 return '>';
9535
9536 case '"':
9537 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9538 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9539 p->lex.ptok = p->lex.pcur-1;
9540 return tSTRING_BEG;
9541
9542 case '`':
9543 if (IS_lex_state(EXPR_FNAME)) {
9544 SET_LEX_STATE(EXPR_ENDFN);
9545 return c;
9546 }
9547 if (IS_lex_state(EXPR_DOT)) {
9548 if (cmd_state)
9549 SET_LEX_STATE(EXPR_CMDARG);
9550 else
9551 SET_LEX_STATE(EXPR_ARG);
9552 return c;
9553 }
9554 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9555 return tXSTRING_BEG;
9556
9557 case '\'':
9558 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9559 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9560 p->lex.ptok = p->lex.pcur-1;
9561 return tSTRING_BEG;
9562
9563 case '?':
9564 return parse_qmark(p, space_seen);
9565
9566 case '&':
9567 if ((c = nextc(p)) == '&') {
9568 SET_LEX_STATE(EXPR_BEG);
9569 if ((c = nextc(p)) == '=') {
9570 set_yylval_id(idANDOP);
9571 SET_LEX_STATE(EXPR_BEG);
9572 return tOP_ASGN;
9573 }
9574 pushback(p, c);
9575 return tANDOP;
9576 }
9577 else if (c == '=') {
9578 set_yylval_id('&');
9579 SET_LEX_STATE(EXPR_BEG);
9580 return tOP_ASGN;
9581 }
9582 else if (c == '.') {
9583 set_yylval_id(idANDDOT);
9584 SET_LEX_STATE(EXPR_DOT);
9585 return tANDDOT;
9586 }
9587 pushback(p, c);
9588 if (IS_SPCARG(c)) {
9589 if ((c != ':') ||
9590 (c = peekc_n(p, 1)) == -1 ||
9591 !(c == '\'' || c == '"' ||
9592 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9593 rb_warning0("`&' interpreted as argument prefix");
9594 }
9595 c = tAMPER;
9596 }
9597 else if (IS_BEG()) {
9598 c = tAMPER;
9599 }
9600 else {
9601 c = warn_balanced('&', "&", "argument prefix");
9602 }
9603 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9604 return c;
9605
9606 case '|':
9607 if ((c = nextc(p)) == '|') {
9608 SET_LEX_STATE(EXPR_BEG);
9609 if ((c = nextc(p)) == '=') {
9610 set_yylval_id(idOROP);
9611 SET_LEX_STATE(EXPR_BEG);
9612 return tOP_ASGN;
9613 }
9614 pushback(p, c);
9615 if (IS_lex_state_for(last_state, EXPR_BEG)) {
9616 c = '|';
9617 pushback(p, '|');
9618 return c;
9619 }
9620 return tOROP;
9621 }
9622 if (c == '=') {
9623 set_yylval_id('|');
9624 SET_LEX_STATE(EXPR_BEG);
9625 return tOP_ASGN;
9626 }
9627 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9628 pushback(p, c);
9629 return '|';
9630
9631 case '+':
9632 c = nextc(p);
9633 if (IS_AFTER_OPERATOR()) {
9634 SET_LEX_STATE(EXPR_ARG);
9635 if (c == '@') {
9636 return tUPLUS;
9637 }
9638 pushback(p, c);
9639 return '+';
9640 }
9641 if (c == '=') {
9642 set_yylval_id('+');
9643 SET_LEX_STATE(EXPR_BEG);
9644 return tOP_ASGN;
9645 }
9646 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9647 SET_LEX_STATE(EXPR_BEG);
9648 pushback(p, c);
9649 if (c != -1 && ISDIGIT(c)) {
9650 return parse_numeric(p, '+');
9651 }
9652 return tUPLUS;
9653 }
9654 SET_LEX_STATE(EXPR_BEG);
9655 pushback(p, c);
9656 return warn_balanced('+', "+", "unary operator");
9657
9658 case '-':
9659 c = nextc(p);
9660 if (IS_AFTER_OPERATOR()) {
9661 SET_LEX_STATE(EXPR_ARG);
9662 if (c == '@') {
9663 return tUMINUS;
9664 }
9665 pushback(p, c);
9666 return '-';
9667 }
9668 if (c == '=') {
9669 set_yylval_id('-');
9670 SET_LEX_STATE(EXPR_BEG);
9671 return tOP_ASGN;
9672 }
9673 if (c == '>') {
9674 SET_LEX_STATE(EXPR_ENDFN);
9675 return tLAMBDA;
9676 }
9677 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9678 SET_LEX_STATE(EXPR_BEG);
9679 pushback(p, c);
9680 if (c != -1 && ISDIGIT(c)) {
9681 return tUMINUS_NUM;
9682 }
9683 return tUMINUS;
9684 }
9685 SET_LEX_STATE(EXPR_BEG);
9686 pushback(p, c);
9687 return warn_balanced('-', "-", "unary operator");
9688
9689 case '.': {
9690 int is_beg = IS_BEG();
9691 SET_LEX_STATE(EXPR_BEG);
9692 if ((c = nextc(p)) == '.') {
9693 if ((c = nextc(p)) == '.') {
9694 if (p->ctxt.in_argdef) {
9695 SET_LEX_STATE(EXPR_ENDARG);
9696 return tBDOT3;
9697 }
9698 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9699 rb_warn0("... at EOL, should be parenthesized?");
9700 }
9701 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
9702 if (IS_lex_state_for(last_state, EXPR_LABEL))
9703 return tDOT3;
9704 }
9705 return is_beg ? tBDOT3 : tDOT3;
9706 }
9707 pushback(p, c);
9708 return is_beg ? tBDOT2 : tDOT2;
9709 }
9710 pushback(p, c);
9711 if (c != -1 && ISDIGIT(c)) {
9712 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9713 parse_numeric(p, '.');
9714 if (ISDIGIT(prev)) {
9715 yyerror0("unexpected fraction part after numeric literal");
9716 }
9717 else {
9718 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9719 }
9720 SET_LEX_STATE(EXPR_END);
9721 p->lex.ptok = p->lex.pcur;
9722 goto retry;
9723 }
9724 set_yylval_id('.');
9725 SET_LEX_STATE(EXPR_DOT);
9726 return '.';
9727 }
9728
9729 case '0': case '1': case '2': case '3': case '4':
9730 case '5': case '6': case '7': case '8': case '9':
9731 return parse_numeric(p, c);
9732
9733 case ')':
9734 COND_POP();
9735 CMDARG_POP();
9736 SET_LEX_STATE(EXPR_ENDFN);
9737 p->lex.paren_nest--;
9738 return c;
9739
9740 case ']':
9741 COND_POP();
9742 CMDARG_POP();
9743 SET_LEX_STATE(EXPR_END);
9744 p->lex.paren_nest--;
9745 return c;
9746
9747 case '}':
9748 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9749 if (!p->lex.brace_nest--) return tSTRING_DEND;
9750 COND_POP();
9751 CMDARG_POP();
9752 SET_LEX_STATE(EXPR_END);
9753 p->lex.paren_nest--;
9754 return c;
9755
9756 case ':':
9757 c = nextc(p);
9758 if (c == ':') {
9759 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9760 SET_LEX_STATE(EXPR_BEG);
9761 return tCOLON3;
9762 }
9763 set_yylval_id(idCOLON2);
9764 SET_LEX_STATE(EXPR_DOT);
9765 return tCOLON2;
9766 }
9767 if (IS_END() || ISSPACE(c) || c == '#') {
9768 pushback(p, c);
9769 c = warn_balanced(':', ":", "symbol literal");
9770 SET_LEX_STATE(EXPR_BEG);
9771 return c;
9772 }
9773 switch (c) {
9774 case '\'':
9775 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9776 break;
9777 case '"':
9778 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9779 break;
9780 default:
9781 pushback(p, c);
9782 break;
9783 }
9784 SET_LEX_STATE(EXPR_FNAME);
9785 return tSYMBEG;
9786
9787 case '/':
9788 if (IS_BEG()) {
9789 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9790 return tREGEXP_BEG;
9791 }
9792 if ((c = nextc(p)) == '=') {
9793 set_yylval_id('/');
9794 SET_LEX_STATE(EXPR_BEG);
9795 return tOP_ASGN;
9796 }
9797 pushback(p, c);
9798 if (IS_SPCARG(c)) {
9799 arg_ambiguous(p, '/');
9800 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9801 return tREGEXP_BEG;
9802 }
9803 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9804 return warn_balanced('/', "/", "regexp literal");
9805
9806 case '^':
9807 if ((c = nextc(p)) == '=') {
9808 set_yylval_id('^');
9809 SET_LEX_STATE(EXPR_BEG);
9810 return tOP_ASGN;
9811 }
9812 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9813 pushback(p, c);
9814 return '^';
9815
9816 case ';':
9817 SET_LEX_STATE(EXPR_BEG);
9818 p->command_start = TRUE;
9819 return ';';
9820
9821 case ',':
9822 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9823 return ',';
9824
9825 case '~':
9826 if (IS_AFTER_OPERATOR()) {
9827 if ((c = nextc(p)) != '@') {
9828 pushback(p, c);
9829 }
9830 SET_LEX_STATE(EXPR_ARG);
9831 }
9832 else {
9833 SET_LEX_STATE(EXPR_BEG);
9834 }
9835 return '~';
9836
9837 case '(':
9838 if (IS_BEG()) {
9839 c = tLPAREN;
9840 }
9841 else if (!space_seen) {
9842 /* foo( ... ) => method call, no ambiguity */
9843 }
9844 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9845 c = tLPAREN_ARG;
9846 }
9847 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9848 rb_warning0("parentheses after method name is interpreted as "
9849 "an argument list, not a decomposed argument");
9850 }
9851 p->lex.paren_nest++;
9852 COND_PUSH(0);
9853 CMDARG_PUSH(0);
9854 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9855 return c;
9856
9857 case '[':
9858 p->lex.paren_nest++;
9859 if (IS_AFTER_OPERATOR()) {
9860 if ((c = nextc(p)) == ']') {
9861 p->lex.paren_nest--;
9862 SET_LEX_STATE(EXPR_ARG);
9863 if ((c = nextc(p)) == '=') {
9864 return tASET;
9865 }
9866 pushback(p, c);
9867 return tAREF;
9868 }
9869 pushback(p, c);
9870 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9871 return '[';
9872 }
9873 else if (IS_BEG()) {
9874 c = tLBRACK;
9875 }
9876 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9877 c = tLBRACK;
9878 }
9879 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9880 COND_PUSH(0);
9881 CMDARG_PUSH(0);
9882 return c;
9883
9884 case '{':
9885 ++p->lex.brace_nest;
9886 if (lambda_beginning_p())
9887 c = tLAMBEG;
9888 else if (IS_lex_state(EXPR_LABELED))
9889 c = tLBRACE; /* hash */
9890 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9891 c = '{'; /* block (primary) */
9892 else if (IS_lex_state(EXPR_ENDARG))
9893 c = tLBRACE_ARG; /* block (expr) */
9894 else
9895 c = tLBRACE; /* hash */
9896 if (c != tLBRACE) {
9897 p->command_start = TRUE;
9898 SET_LEX_STATE(EXPR_BEG);
9899 }
9900 else {
9901 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9902 }
9903 ++p->lex.paren_nest; /* after lambda_beginning_p() */
9904 COND_PUSH(0);
9905 CMDARG_PUSH(0);
9906 return c;
9907
9908 case '\\':
9909 c = nextc(p);
9910 if (c == '\n') {
9911 space_seen = 1;
9912 dispatch_scan_event(p, tSP);
9913 goto retry; /* skip \\n */
9914 }
9915 if (c == ' ') return tSP;
9916 if (ISSPACE(c)) return c;
9917 pushback(p, c);
9918 return '\\';
9919
9920 case '%':
9921 return parse_percent(p, space_seen, last_state);
9922
9923 case '$':
9924 return parse_gvar(p, last_state);
9925
9926 case '@':
9927 return parse_atmark(p, last_state);
9928
9929 case '_':
9930 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9931 p->ruby__end__seen = 1;
9932 p->eofp = 1;
9933#ifndef RIPPER
9934 return -1;
9935#else
9936 lex_goto_eol(p);
9937 dispatch_scan_event(p, k__END__);
9938 return 0;
9939#endif
9940 }
9941 newtok(p);
9942 break;
9943
9944 default:
9945 if (!parser_is_identchar(p)) {
9946 compile_error(p, "Invalid char `\\x%02X' in expression", c);
9947 token_flush(p);
9948 goto retry;
9949 }
9950
9951 newtok(p);
9952 break;
9953 }
9954
9955 return parse_ident(p, c, cmd_state);
9956}
9957
9958static enum yytokentype
9959yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9960{
9961 enum yytokentype t;
9962
9963 p->lval = lval;
9964 lval->val = Qundef;
9965 t = parser_yylex(p);
9966
9967 if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9968 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9969 else
9970 RUBY_SET_YYLLOC(*yylloc);
9971
9972 if (has_delayed_token(p))
9973 dispatch_delayed_token(p, t);
9974 else if (t != 0)
9975 dispatch_scan_event(p, t);
9976
9977 return t;
9978}
9979
9980#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9981
9982static NODE*
9983node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9984{
9985 NODE *n = rb_ast_newnode(p->ast, type);
9986
9987 rb_node_init(n, type, a0, a1, a2);
9988
9989 nd_set_loc(n, loc);
9990 nd_set_node_id(n, parser_get_node_id(p));
9991 return n;
9992}
9993
9994static NODE *
9995nd_set_loc(NODE *nd, const YYLTYPE *loc)
9996{
9997 nd->nd_loc = *loc;
9998 nd_set_line(nd, loc->beg_pos.lineno);
9999 return nd;
10000}
10001
10002#ifndef RIPPER
10003static enum node_type
10004nodetype(NODE *node) /* for debug */
10005{
10006 return (enum node_type)nd_type(node);
10007}
10008
10009static int
10010nodeline(NODE *node)
10011{
10012 return nd_line(node);
10013}
10014
10015static NODE*
10016newline_node(NODE *node)
10017{
10018 if (node) {
10019 node = remove_begin(node);
10020 node->flags |= NODE_FL_NEWLINE;
10021 }
10022 return node;
10023}
10024
10025static void
10026fixpos(NODE *node, NODE *orig)
10027{
10028 if (!node) return;
10029 if (!orig) return;
10030 nd_set_line(node, nd_line(orig));
10031}
10032
10033static void
10034parser_warning(struct parser_params *p, NODE *node, const char *mesg)
10035{
10036 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10037}
10038
10039static void
10040parser_warn(struct parser_params *p, NODE *node, const char *mesg)
10041{
10042 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10043}
10044
10045static NODE*
10046block_append(struct parser_params *p, NODE *head, NODE *tail)
10047{
10048 NODE *end, *h = head, *nd;
10049
10050 if (tail == 0) return head;
10051
10052 if (h == 0) return tail;
10053 switch (nd_type(h)) {
10054 case NODE_LIT:
10055 case NODE_STR:
10056 case NODE_SELF:
10057 case NODE_TRUE:
10058 case NODE_FALSE:
10059 case NODE_NIL:
10060 parser_warning(p, h, "unused literal ignored");
10061 return tail;
10062 default:
10063 h = end = NEW_BLOCK(head, &head->nd_loc);
10064 end->nd_end = end;
10065 head = end;
10066 break;
10067 case NODE_BLOCK:
10068 end = h->nd_end;
10069 break;
10070 }
10071
10072 nd = end->nd_head;
10073 switch (nd_type(nd)) {
10074 case NODE_RETURN:
10075 case NODE_BREAK:
10076 case NODE_NEXT:
10077 case NODE_REDO:
10078 case NODE_RETRY:
10079 if (RTEST(ruby_verbose)) {
10080 parser_warning(p, tail, "statement not reached");
10081 }
10082 break;
10083
10084 default:
10085 break;
10086 }
10087
10088 if (!nd_type_p(tail, NODE_BLOCK)) {
10089 tail = NEW_BLOCK(tail, &tail->nd_loc);
10090 tail->nd_end = tail;
10091 }
10092 end->nd_next = tail;
10093 h->nd_end = tail->nd_end;
10094 nd_set_last_loc(head, nd_last_loc(tail));
10095 return head;
10096}
10097
10098/* append item to the list */
10099static NODE*
10100list_append(struct parser_params *p, NODE *list, NODE *item)
10101{
10102 NODE *last;
10103
10104 if (list == 0) return NEW_LIST(item, &item->nd_loc);
10105 if (list->nd_next) {
10106 last = list->nd_next->nd_end;
10107 }
10108 else {
10109 last = list;
10110 }
10111
10112 list->nd_alen += 1;
10113 last->nd_next = NEW_LIST(item, &item->nd_loc);
10114 list->nd_next->nd_end = last->nd_next;
10115
10116 nd_set_last_loc(list, nd_last_loc(item));
10117
10118 return list;
10119}
10120
10121/* concat two lists */
10122static NODE*
10123list_concat(NODE *head, NODE *tail)
10124{
10125 NODE *last;
10126
10127 if (head->nd_next) {
10128 last = head->nd_next->nd_end;
10129 }
10130 else {
10131 last = head;
10132 }
10133
10134 head->nd_alen += tail->nd_alen;
10135 last->nd_next = tail;
10136 if (tail->nd_next) {
10137 head->nd_next->nd_end = tail->nd_next->nd_end;
10138 }
10139 else {
10140 head->nd_next->nd_end = tail;
10141 }
10142
10143 nd_set_last_loc(head, nd_last_loc(tail));
10144
10145 return head;
10146}
10147
10148static int
10149literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
10150{
10151 if (NIL_P(tail)) return 1;
10152 if (!rb_enc_compatible(head, tail)) {
10153 compile_error(p, "string literal encodings differ (%s / %s)",
10154 rb_enc_name(rb_enc_get(head)),
10155 rb_enc_name(rb_enc_get(tail)));
10156 rb_str_resize(head, 0);
10157 rb_str_resize(tail, 0);
10158 return 0;
10159 }
10160 rb_str_buf_append(head, tail);
10161 return 1;
10162}
10163
10164static VALUE
10165string_literal_head(enum node_type htype, NODE *head)
10166{
10167 if (htype != NODE_DSTR) return Qfalse;
10168 if (head->nd_next) {
10169 head = head->nd_next->nd_end->nd_head;
10170 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
10171 }
10172 const VALUE lit = head->nd_lit;
10173 ASSUME(lit != Qfalse);
10174 return lit;
10175}
10176
10177/* concat two string literals */
10178static NODE *
10179literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
10180{
10181 enum node_type htype;
10182 VALUE lit;
10183
10184 if (!head) return tail;
10185 if (!tail) return head;
10186
10187 htype = nd_type(head);
10188 if (htype == NODE_EVSTR) {
10189 head = new_dstr(p, head, loc);
10190 htype = NODE_DSTR;
10191 }
10192 if (p->heredoc_indent > 0) {
10193 switch (htype) {
10194 case NODE_STR:
10195 nd_set_type(head, NODE_DSTR);
10196 case NODE_DSTR:
10197 return list_append(p, head, tail);
10198 default:
10199 break;
10200 }
10201 }
10202 switch (nd_type(tail)) {
10203 case NODE_STR:
10204 if ((lit = string_literal_head(htype, head)) != Qfalse) {
10205 htype = NODE_STR;
10206 }
10207 else {
10208 lit = head->nd_lit;
10209 }
10210 if (htype == NODE_STR) {
10211 if (!literal_concat0(p, lit, tail->nd_lit)) {
10212 error:
10213 rb_discard_node(p, head);
10214 rb_discard_node(p, tail);
10215 return 0;
10216 }
10217 rb_discard_node(p, tail);
10218 }
10219 else {
10220 list_append(p, head, tail);
10221 }
10222 break;
10223
10224 case NODE_DSTR:
10225 if (htype == NODE_STR) {
10226 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
10227 goto error;
10228 tail->nd_lit = head->nd_lit;
10229 rb_discard_node(p, head);
10230 head = tail;
10231 }
10232 else if (NIL_P(tail->nd_lit)) {
10233 append:
10234 head->nd_alen += tail->nd_alen - 1;
10235 if (!head->nd_next) {
10236 head->nd_next = tail->nd_next;
10237 }
10238 else if (tail->nd_next) {
10239 head->nd_next->nd_end->nd_next = tail->nd_next;
10240 head->nd_next->nd_end = tail->nd_next->nd_end;
10241 }
10242 rb_discard_node(p, tail);
10243 }
10244 else if ((lit = string_literal_head(htype, head)) != Qfalse) {
10245 if (!literal_concat0(p, lit, tail->nd_lit))
10246 goto error;
10247 tail->nd_lit = Qnil;
10248 goto append;
10249 }
10250 else {
10251 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
10252 }
10253 break;
10254
10255 case NODE_EVSTR:
10256 if (htype == NODE_STR) {
10257 nd_set_type(head, NODE_DSTR);
10258 head->nd_alen = 1;
10259 }
10260 list_append(p, head, tail);
10261 break;
10262 }
10263 return head;
10264}
10265
10266static NODE *
10267evstr2dstr(struct parser_params *p, NODE *node)
10268{
10269 if (nd_type_p(node, NODE_EVSTR)) {
10270 node = new_dstr(p, node, &node->nd_loc);
10271 }
10272 return node;
10273}
10274
10275static NODE *
10276new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10277{
10278 NODE *head = node;
10279
10280 if (node) {
10281 switch (nd_type(node)) {
10282 case NODE_STR:
10283 nd_set_type(node, NODE_DSTR);
10284 return node;
10285 case NODE_DSTR:
10286 break;
10287 case NODE_EVSTR:
10288 return node;
10289 }
10290 }
10291 return NEW_EVSTR(head, loc);
10292}
10293
10294static NODE *
10295new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10296{
10297 VALUE lit = STR_NEW0();
10298 NODE *dstr = NEW_DSTR(lit, loc);
10299 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10300 return list_append(p, dstr, node);
10301}
10302
10303static NODE *
10304call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
10305 const YYLTYPE *op_loc, const YYLTYPE *loc)
10306{
10307 NODE *expr;
10308 value_expr(recv);
10309 value_expr(arg1);
10310 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
10311 nd_set_line(expr, op_loc->beg_pos.lineno);
10312 return expr;
10313}
10314
10315static NODE *
10316call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
10317{
10318 NODE *opcall;
10319 value_expr(recv);
10320 opcall = NEW_OPCALL(recv, id, 0, loc);
10321 nd_set_line(opcall, op_loc->beg_pos.lineno);
10322 return opcall;
10323}
10324
10325static NODE *
10326new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
10327{
10328 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
10329 nd_set_line(qcall, op_loc->beg_pos.lineno);
10330 return qcall;
10331}
10332
10333static NODE*
10334new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
10335{
10336 NODE *ret;
10337 if (block) block_dup_check(p, args, block);
10338 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
10339 if (block) ret = method_add_block(p, ret, block, loc);
10340 fixpos(ret, recv);
10341 return ret;
10342}
10343
10344#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? (node)->nd_body : node)
10345static NODE*
10346match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
10347{
10348 NODE *n;
10349 int line = op_loc->beg_pos.lineno;
10350
10351 value_expr(node1);
10352 value_expr(node2);
10353 if (node1 && (n = nd_once_body(node1)) != 0) {
10354 switch (nd_type(n)) {
10355 case NODE_DREGX:
10356 {
10357 NODE *match = NEW_MATCH2(node1, node2, loc);
10358 nd_set_line(match, line);
10359 return match;
10360 }
10361
10362 case NODE_LIT:
10363 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
10364 const VALUE lit = n->nd_lit;
10365 NODE *match = NEW_MATCH2(node1, node2, loc);
10366 match->nd_args = reg_named_capture_assign(p, lit, loc);
10367 nd_set_line(match, line);
10368 return match;
10369 }
10370 }
10371 }
10372
10373 if (node2 && (n = nd_once_body(node2)) != 0) {
10374 NODE *match3;
10375
10376 switch (nd_type(n)) {
10377 case NODE_LIT:
10378 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
10379 /* fallthru */
10380 case NODE_DREGX:
10381 match3 = NEW_MATCH3(node2, node1, loc);
10382 return match3;
10383 }
10384 }
10385
10386 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
10387 nd_set_line(n, line);
10388 return n;
10389}
10390
10391# if WARN_PAST_SCOPE
10392static int
10393past_dvar_p(struct parser_params *p, ID id)
10394{
10395 struct vtable *past = p->lvtbl->past;
10396 while (past) {
10397 if (vtable_included(past, id)) return 1;
10398 past = past->prev;
10399 }
10400 return 0;
10401}
10402# endif
10403
10404static int
10405numparam_nested_p(struct parser_params *p)
10406{
10407 struct local_vars *local = p->lvtbl;
10408 NODE *outer = local->numparam.outer;
10409 NODE *inner = local->numparam.inner;
10410 if (outer || inner) {
10411 NODE *used = outer ? outer : inner;
10412 compile_error(p, "numbered parameter is already used in\n"
10413 "%s:%d: %s block here",
10414 p->ruby_sourcefile, nd_line(used),
10415 outer ? "outer" : "inner");
10416 parser_show_error_line(p, &used->nd_loc);
10417 return 1;
10418 }
10419 return 0;
10420}
10421
10422static NODE*
10423gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
10424{
10425 ID *vidp = NULL;
10426 NODE *node;
10427 switch (id) {
10428 case keyword_self:
10429 return NEW_SELF(loc);
10430 case keyword_nil:
10431 return NEW_NIL(loc);
10432 case keyword_true:
10433 return NEW_TRUE(loc);
10434 case keyword_false:
10435 return NEW_FALSE(loc);
10436 case keyword__FILE__:
10437 {
10438 VALUE file = p->ruby_sourcefile_string;
10439 if (NIL_P(file))
10440 file = rb_str_new(0, 0);
10441 else
10442 file = rb_str_dup(file);
10443 node = NEW_STR(file, loc);
10444 RB_OBJ_WRITTEN(p->ast, Qnil, file);
10445 }
10446 return node;
10447 case keyword__LINE__:
10448 return NEW_LIT(INT2FIX(p->tokline), loc);
10449 case keyword__ENCODING__:
10450 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
10451 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10452 return node;
10453
10454 }
10455 switch (id_type(id)) {
10456 case ID_LOCAL:
10457 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
10458 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
10459 if (id == p->cur_arg) {
10460 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10461 return 0;
10462 }
10463 if (vidp) *vidp |= LVAR_USED;
10464 node = NEW_DVAR(id, loc);
10465 return node;
10466 }
10467 if (local_id_ref(p, id, &vidp)) {
10468 if (id == p->cur_arg) {
10469 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10470 return 0;
10471 }
10472 if (vidp) *vidp |= LVAR_USED;
10473 node = NEW_LVAR(id, loc);
10474 return node;
10475 }
10476 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
10477 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
10478 if (numparam_nested_p(p)) return 0;
10479 node = NEW_DVAR(id, loc);
10480 struct local_vars *local = p->lvtbl;
10481 if (!local->numparam.current) local->numparam.current = node;
10482 return node;
10483 }
10484# if WARN_PAST_SCOPE
10485 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10486 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10487 }
10488# endif
10489 /* method call without arguments */
10490 return NEW_VCALL(id, loc);
10491 case ID_GLOBAL:
10492 return NEW_GVAR(id, loc);
10493 case ID_INSTANCE:
10494 return NEW_IVAR(id, loc);
10495 case ID_CONST:
10496 return NEW_CONST(id, loc);
10497 case ID_CLASS:
10498 return NEW_CVAR(id, loc);
10499 }
10500 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10501 return 0;
10502}
10503
10504static NODE *
10505opt_arg_append(NODE *opt_list, NODE *opt)
10506{
10507 NODE *opts = opt_list;
10508 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10509
10510 while (opts->nd_next) {
10511 opts = opts->nd_next;
10512 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10513 }
10514 opts->nd_next = opt;
10515
10516 return opt_list;
10517}
10518
10519static NODE *
10520kwd_append(NODE *kwlist, NODE *kw)
10521{
10522 if (kwlist) {
10523 NODE *kws = kwlist;
10524 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10525 while (kws->nd_next) {
10526 kws = kws->nd_next;
10527 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10528 }
10529 kws->nd_next = kw;
10530 }
10531 return kwlist;
10532}
10533
10534static NODE *
10535new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10536{
10537 return NEW_DEFINED(remove_begin_all(expr), loc);
10538}
10539
10540static NODE*
10541symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10542{
10543 enum node_type type = nd_type(symbol);
10544 switch (type) {
10545 case NODE_DSTR:
10546 nd_set_type(symbol, NODE_DSYM);
10547 break;
10548 case NODE_STR:
10549 nd_set_type(symbol, NODE_LIT);
10550 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10551 break;
10552 default:
10553 compile_error(p, "unexpected node as symbol: %s", ruby_node_name(type));
10554 }
10555 return list_append(p, symbols, symbol);
10556}
10557
10558static NODE *
10559new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10560{
10561 NODE *list, *prev;
10562 VALUE lit;
10563
10564 if (!node) {
10565 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10566 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10567 return node;
10568 }
10569 switch (nd_type(node)) {
10570 case NODE_STR:
10571 {
10572 VALUE src = node->nd_lit;
10573 nd_set_type(node, NODE_LIT);
10574 nd_set_loc(node, loc);
10575 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10576 }
10577 break;
10578 default:
10579 lit = STR_NEW0();
10580 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10581 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10582 /* fall through */
10583 case NODE_DSTR:
10584 nd_set_type(node, NODE_DREGX);
10585 nd_set_loc(node, loc);
10586 node->nd_cflag = options & RE_OPTION_MASK;
10587 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10588 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10589 NODE *frag = list->nd_head;
10590 enum node_type type = nd_type(frag);
10591 if (type == NODE_STR || (type == NODE_DSTR && !frag->nd_next)) {
10592 VALUE tail = frag->nd_lit;
10593 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10594 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10595 if (!literal_concat0(p, lit, tail)) {
10596 return NEW_NIL(loc); /* dummy node on error */
10597 }
10598 rb_str_resize(tail, 0);
10599 prev->nd_next = list->nd_next;
10600 rb_discard_node(p, list->nd_head);
10601 rb_discard_node(p, list);
10602 list = prev;
10603 }
10604 else {
10605 prev = list;
10606 }
10607 }
10608 else {
10609 prev = 0;
10610 }
10611 }
10612 if (!node->nd_next) {
10613 VALUE src = node->nd_lit;
10614 nd_set_type(node, NODE_LIT);
10615 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10616 }
10617 if (options & RE_OPTION_ONCE) {
10618 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10619 }
10620 break;
10621 }
10622 return node;
10623}
10624
10625static NODE *
10626new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10627{
10628 if (!k) return 0;
10629 return NEW_KW_ARG(0, (k), loc);
10630}
10631
10632static NODE *
10633new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10634{
10635 if (!node) {
10636 VALUE lit = STR_NEW0();
10637 NODE *xstr = NEW_XSTR(lit, loc);
10638 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10639 return xstr;
10640 }
10641 switch (nd_type(node)) {
10642 case NODE_STR:
10643 nd_set_type(node, NODE_XSTR);
10644 nd_set_loc(node, loc);
10645 break;
10646 case NODE_DSTR:
10647 nd_set_type(node, NODE_DXSTR);
10648 nd_set_loc(node, loc);
10649 break;
10650 default:
10651 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10652 break;
10653 }
10654 return node;
10655}
10656
10657static void
10658check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10659{
10660 VALUE lit;
10661
10662 if (!arg || !p->case_labels) return;
10663
10664 lit = rb_node_case_when_optimizable_literal(arg);
10665 if (lit == Qundef) return;
10666 if (nd_type_p(arg, NODE_STR)) {
10667 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10668 }
10669
10670 if (NIL_P(p->case_labels)) {
10671 p->case_labels = rb_obj_hide(rb_hash_new());
10672 }
10673 else {
10674 VALUE line = rb_hash_lookup(p->case_labels, lit);
10675 if (!NIL_P(line)) {
10676 rb_warning1("duplicated `when' clause with line %d is ignored",
10677 WARN_IVAL(line));
10678 return;
10679 }
10680 }
10681 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10682}
10683
10684#else /* !RIPPER */
10685static int
10686id_is_var(struct parser_params *p, ID id)
10687{
10688 if (is_notop_id(id)) {
10689 switch (id & ID_SCOPE_MASK) {
10690 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10691 return 1;
10692 case ID_LOCAL:
10693 if (dyna_in_block(p)) {
10694 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10695 }
10696 if (local_id(p, id)) return 1;
10697 /* method call without arguments */
10698 return 0;
10699 }
10700 }
10701 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10702 return 0;
10703}
10704
10705static VALUE
10706new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10707{
10708 VALUE src = 0, err;
10709 int options = 0;
10710 if (ripper_is_node_yylval(re)) {
10711 src = RNODE(re)->nd_cval;
10712 re = RNODE(re)->nd_rval;
10713 }
10714 if (ripper_is_node_yylval(opt)) {
10715 options = (int)RNODE(opt)->nd_tag;
10716 opt = RNODE(opt)->nd_rval;
10717 }
10718 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10719 compile_error(p, "%"PRIsVALUE, err);
10720 }
10721 return dispatch2(regexp_literal, re, opt);
10722}
10723#endif /* !RIPPER */
10724
10725static inline enum lex_state_e
10726parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
10727{
10728 if (p->debug) {
10729 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
10730 }
10731 return p->lex.state = ls;
10732}
10733
10734#ifndef RIPPER
10735static const char rb_parser_lex_state_names[][8] = {
10736 "BEG", "END", "ENDARG", "ENDFN", "ARG",
10737 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10738 "LABEL", "LABELED","FITEM",
10739};
10740
10741static VALUE
10742append_lex_state_name(enum lex_state_e state, VALUE buf)
10743{
10744 int i, sep = 0;
10745 unsigned int mask = 1;
10746 static const char none[] = "NONE";
10747
10748 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10749 if ((unsigned)state & mask) {
10750 if (sep) {
10751 rb_str_cat(buf, "|", 1);
10752 }
10753 sep = 1;
10754 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10755 }
10756 }
10757 if (!sep) {
10758 rb_str_cat(buf, none, sizeof(none)-1);
10759 }
10760 return buf;
10761}
10762
10763static void
10764flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10765{
10766 VALUE mesg = p->debug_buffer;
10767
10768 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10769 p->debug_buffer = Qnil;
10770 rb_io_puts(1, &mesg, out);
10771 }
10772 if (!NIL_P(str) && RSTRING_LEN(str)) {
10773 rb_io_write(p->debug_output, str);
10774 }
10775}
10776
10777enum lex_state_e
10778rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10779 enum lex_state_e to, int line)
10780{
10781 VALUE mesg;
10782 mesg = rb_str_new_cstr("lex_state: ");
10783 append_lex_state_name(from, mesg);
10784 rb_str_cat_cstr(mesg, " -> ");
10785 append_lex_state_name(to, mesg);
10786 rb_str_catf(mesg, " at line %d\n", line);
10787 flush_debug_buffer(p, p->debug_output, mesg);
10788 return to;
10789}
10790
10791VALUE
10792rb_parser_lex_state_name(enum lex_state_e state)
10793{
10794 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10795}
10796
10797static void
10798append_bitstack_value(stack_type stack, VALUE mesg)
10799{
10800 if (stack == 0) {
10801 rb_str_cat_cstr(mesg, "0");
10802 }
10803 else {
10804 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10805 for (; mask && !(stack & mask); mask >>= 1) continue;
10806 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10807 }
10808}
10809
10810void
10811rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10812 const char *name, int line)
10813{
10814 VALUE mesg = rb_sprintf("%s: ", name);
10815 append_bitstack_value(stack, mesg);
10816 rb_str_catf(mesg, " at line %d\n", line);
10817 flush_debug_buffer(p, p->debug_output, mesg);
10818}
10819
10820void
10821rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10822{
10823 va_list ap;
10824 VALUE mesg = rb_str_new_cstr("internal parser error: ");
10825
10826 va_start(ap, fmt);
10827 rb_str_vcatf(mesg, fmt, ap);
10828 va_end(ap);
10829 yyerror0(RSTRING_PTR(mesg));
10830 RB_GC_GUARD(mesg);
10831
10832 mesg = rb_str_new(0, 0);
10833 append_lex_state_name(p->lex.state, mesg);
10834 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10835 rb_str_resize(mesg, 0);
10836 append_bitstack_value(p->cond_stack, mesg);
10837 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10838 rb_str_resize(mesg, 0);
10839 append_bitstack_value(p->cmdarg_stack, mesg);
10840 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10841 if (p->debug_output == rb_ractor_stdout())
10842 p->debug_output = rb_ractor_stderr();
10843 p->debug = TRUE;
10844}
10845
10846static YYLTYPE *
10847rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
10848{
10849 yylloc->beg_pos.lineno = sourceline;
10850 yylloc->beg_pos.column = beg_pos;
10851 yylloc->end_pos.lineno = sourceline;
10852 yylloc->end_pos.column = end_pos;
10853 return yylloc;
10854}
10855
10856YYLTYPE *
10857rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10858{
10859 int sourceline = here->sourceline;
10860 int beg_pos = (int)here->offset - here->quote
10861 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10862 int end_pos = (int)here->offset + here->length + here->quote;
10863
10864 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
10865}
10866
10867YYLTYPE *
10868rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10869{
10870 int sourceline = p->ruby_sourceline;
10871 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10872 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
10873 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
10874}
10875
10876YYLTYPE *
10877rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10878{
10879 int sourceline = p->ruby_sourceline;
10880 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10881 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
10882 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
10883}
10884#endif /* !RIPPER */
10885
10886static int
10887assignable0(struct parser_params *p, ID id, const char **err)
10888{
10889 if (!id) return -1;
10890 switch (id) {
10891 case keyword_self:
10892 *err = "Can't change the value of self";
10893 return -1;
10894 case keyword_nil:
10895 *err = "Can't assign to nil";
10896 return -1;
10897 case keyword_true:
10898 *err = "Can't assign to true";
10899 return -1;
10900 case keyword_false:
10901 *err = "Can't assign to false";
10902 return -1;
10903 case keyword__FILE__:
10904 *err = "Can't assign to __FILE__";
10905 return -1;
10906 case keyword__LINE__:
10907 *err = "Can't assign to __LINE__";
10908 return -1;
10909 case keyword__ENCODING__:
10910 *err = "Can't assign to __ENCODING__";
10911 return -1;
10912 }
10913 switch (id_type(id)) {
10914 case ID_LOCAL:
10915 if (dyna_in_block(p)) {
10916 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10917 compile_error(p, "Can't assign to numbered parameter _%d",
10918 NUMPARAM_ID_TO_IDX(id));
10919 return -1;
10920 }
10921 if (dvar_curr(p, id)) return NODE_DASGN;
10922 if (dvar_defined(p, id)) return NODE_DASGN;
10923 if (local_id(p, id)) return NODE_LASGN;
10924 dyna_var(p, id);
10925 return NODE_DASGN;
10926 }
10927 else {
10928 if (!local_id(p, id)) local_var(p, id);
10929 return NODE_LASGN;
10930 }
10931 break;
10932 case ID_GLOBAL: return NODE_GASGN;
10933 case ID_INSTANCE: return NODE_IASGN;
10934 case ID_CONST:
10935 if (!p->ctxt.in_def) return NODE_CDECL;
10936 *err = "dynamic constant assignment";
10937 return -1;
10938 case ID_CLASS: return NODE_CVASGN;
10939 default:
10940 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10941 }
10942 return -1;
10943}
10944
10945#ifndef RIPPER
10946static NODE*
10947assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10948{
10949 const char *err = 0;
10950 int node_type = assignable0(p, id, &err);
10951 switch (node_type) {
10952 case NODE_DASGN: return NEW_DASGN(id, val, loc);
10953 case NODE_LASGN: return NEW_LASGN(id, val, loc);
10954 case NODE_GASGN: return NEW_GASGN(id, val, loc);
10955 case NODE_IASGN: return NEW_IASGN(id, val, loc);
10956 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10957 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10958 }
10959 if (err) yyerror1(loc, err);
10960 return NEW_BEGIN(0, loc);
10961}
10962#else
10963static VALUE
10964assignable(struct parser_params *p, VALUE lhs)
10965{
10966 const char *err = 0;
10967 assignable0(p, get_id(lhs), &err);
10968 if (err) lhs = assign_error(p, err, lhs);
10969 return lhs;
10970}
10971#endif
10972
10973static int
10974is_private_local_id(ID name)
10975{
10976 VALUE s;
10977 if (name == idUScore) return 1;
10978 if (!is_local_id(name)) return 0;
10979 s = rb_id2str(name);
10980 if (!s) return 0;
10981 return RSTRING_PTR(s)[0] == '_';
10982}
10983
10984static int
10985shadowing_lvar_0(struct parser_params *p, ID name)
10986{
10987 if (is_private_local_id(name)) return 1;
10988 if (dyna_in_block(p)) {
10989 if (dvar_curr(p, name)) {
10990 yyerror0("duplicated argument name");
10991 }
10992 else if (dvar_defined(p, name) || local_id(p, name)) {
10993 vtable_add(p->lvtbl->vars, name);
10994 if (p->lvtbl->used) {
10995 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
10996 }
10997 return 0;
10998 }
10999 }
11000 else {
11001 if (local_id(p, name)) {
11002 yyerror0("duplicated argument name");
11003 }
11004 }
11005 return 1;
11006}
11007
11008static ID
11009shadowing_lvar(struct parser_params *p, ID name)
11010{
11011 shadowing_lvar_0(p, name);
11012 return name;
11013}
11014
11015static void
11016new_bv(struct parser_params *p, ID name)
11017{
11018 if (!name) return;
11019 if (!is_local_id(name)) {
11020 compile_error(p, "invalid local variable - %"PRIsVALUE,
11021 rb_id2str(name));
11022 return;
11023 }
11024 if (!shadowing_lvar_0(p, name)) return;
11025 dyna_var(p, name);
11026}
11027
11028#ifndef RIPPER
11029static NODE *
11030aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
11031{
11032 return NEW_ATTRASGN(recv, tASET, idx, loc);
11033}
11034
11035static void
11036block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
11037{
11038 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
11039 compile_error(p, "both block arg and actual block given");
11040 }
11041}
11042
11043static NODE *
11044attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
11045{
11046 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
11047 return NEW_ATTRASGN(recv, id, 0, loc);
11048}
11049
11050static void
11051rb_backref_error(struct parser_params *p, NODE *node)
11052{
11053 switch (nd_type(node)) {
11054 case NODE_NTH_REF:
11055 compile_error(p, "Can't set variable $%ld", node->nd_nth);
11056 break;
11057 case NODE_BACK_REF:
11058 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
11059 break;
11060 }
11061}
11062#else
11063static VALUE
11064backref_error(struct parser_params *p, NODE *ref, VALUE expr)
11065{
11066 VALUE mesg = rb_str_new_cstr("Can't set variable ");
11067 rb_str_append(mesg, ref->nd_cval);
11068 return dispatch2(assign_error, mesg, expr);
11069}
11070#endif
11071
11072#ifndef RIPPER
11073static NODE *
11074arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11075{
11076 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
11077 switch (nd_type(node1)) {
11078 case NODE_LIST:
11079 return list_append(p, node1, node2);
11080 case NODE_BLOCK_PASS:
11081 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
11082 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
11083 return node1;
11084 case NODE_ARGSPUSH:
11085 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
11086 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11087 nd_set_type(node1, NODE_ARGSCAT);
11088 return node1;
11089 case NODE_ARGSCAT:
11090 if (!nd_type_p(node1->nd_body, NODE_LIST)) break;
11091 node1->nd_body = list_append(p, node1->nd_body, node2);
11092 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11093 return node1;
11094 }
11095 return NEW_ARGSPUSH(node1, node2, loc);
11096}
11097
11098static NODE *
11099arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11100{
11101 if (!node2) return node1;
11102 switch (nd_type(node1)) {
11103 case NODE_BLOCK_PASS:
11104 if (node1->nd_head)
11105 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
11106 else
11107 node1->nd_head = NEW_LIST(node2, loc);
11108 return node1;
11109 case NODE_ARGSPUSH:
11110 if (!nd_type_p(node2, NODE_LIST)) break;
11111 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
11112 nd_set_type(node1, NODE_ARGSCAT);
11113 return node1;
11114 case NODE_ARGSCAT:
11115 if (!nd_type_p(node2, NODE_LIST) ||
11116 !nd_type_p(node1->nd_body, NODE_LIST)) break;
11117 node1->nd_body = list_concat(node1->nd_body, node2);
11118 return node1;
11119 }
11120 return NEW_ARGSCAT(node1, node2, loc);
11121}
11122
11123static NODE *
11124last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
11125{
11126 NODE *n1;
11127 if ((n1 = splat_array(args)) != 0) {
11128 return list_append(p, n1, last_arg);
11129 }
11130 return arg_append(p, args, last_arg, loc);
11131}
11132
11133static NODE *
11134rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
11135{
11136 NODE *n1;
11137 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
11138 return list_concat(n1, rest_arg);
11139 }
11140 return arg_concat(p, args, rest_arg, loc);
11141}
11142
11143static NODE *
11144splat_array(NODE* node)
11145{
11146 if (nd_type_p(node, NODE_SPLAT)) node = node->nd_head;
11147 if (nd_type_p(node, NODE_LIST)) return node;
11148 return 0;
11149}
11150
11151static void
11152mark_lvar_used(struct parser_params *p, NODE *rhs)
11153{
11154 ID *vidp = NULL;
11155 if (!rhs) return;
11156 switch (nd_type(rhs)) {
11157 case NODE_LASGN:
11158 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
11159 if (vidp) *vidp |= LVAR_USED;
11160 }
11161 break;
11162 case NODE_DASGN:
11163 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
11164 if (vidp) *vidp |= LVAR_USED;
11165 }
11166 break;
11167#if 0
11168 case NODE_MASGN:
11169 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
11170 mark_lvar_used(p, rhs->nd_head);
11171 }
11172 break;
11173#endif
11174 }
11175}
11176
11177static NODE *
11178const_decl_path(struct parser_params *p, NODE **dest)
11179{
11180 NODE *n = *dest;
11181 if (!nd_type_p(n, NODE_CALL)) {
11182 const YYLTYPE *loc = &n->nd_loc;
11183 VALUE path;
11184 if (n->nd_vid) {
11185 path = rb_id2str(n->nd_vid);
11186 }
11187 else {
11188 n = n->nd_else;
11189 path = rb_ary_new();
11190 for (; n && nd_type_p(n, NODE_COLON2); n = n->nd_head) {
11191 rb_ary_push(path, rb_id2str(n->nd_mid));
11192 }
11193 if (n && nd_type_p(n, NODE_CONST)) {
11194 // Const::Name
11195 rb_ary_push(path, rb_id2str(n->nd_vid));
11196 }
11197 else if (n && nd_type_p(n, NODE_COLON3)) {
11198 // ::Const::Name
11199 rb_ary_push(path, rb_str_new(0, 0));
11200 }
11201 else {
11202 // expression::Name
11203 rb_ary_push(path, rb_str_new_cstr("..."));
11204 }
11205 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
11206 path = rb_fstring(path);
11207 }
11208 *dest = n = NEW_LIT(path, loc);
11209 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11210 }
11211 return n;
11212}
11213
11214extern VALUE rb_mRubyVMFrozenCore;
11215
11216static NODE *
11217make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
11218{
11219 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11220
11221 if (copy) {
11222 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
11223 NEW_LIST(value, loc), loc);
11224 }
11225 else {
11226 return NEW_CALL(fcore, rb_intern("make_shareable"),
11227 NEW_LIST(value, loc), loc);
11228 }
11229}
11230
11231static NODE *
11232ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
11233{
11234 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11235 NODE *args = NEW_LIST(value, loc);
11236 args = list_append(p, args, const_decl_path(p, dest));
11237 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
11238}
11239
11240static int is_static_content(NODE *node);
11241
11242static VALUE
11243shareable_literal_value(NODE *node)
11244{
11245 if (!node) return Qnil;
11246 enum node_type type = nd_type(node);
11247 switch (type) {
11248 case NODE_TRUE:
11249 return Qtrue;
11250 case NODE_FALSE:
11251 return Qfalse;
11252 case NODE_NIL:
11253 return Qnil;
11254 case NODE_LIT:
11255 return node->nd_lit;
11256 default:
11257 return Qundef;
11258 }
11259}
11260
11261#ifndef SHAREABLE_BARE_EXPRESSION
11262#define SHAREABLE_BARE_EXPRESSION 1
11263#endif
11264
11265static NODE *
11266shareable_literal_constant(struct parser_params *p, enum shareability shareable,
11267 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
11268{
11269# define shareable_literal_constant_next(n) \
11270 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
11271 VALUE lit = Qnil;
11272
11273 if (!value) return 0;
11274 enum node_type type = nd_type(value);
11275 switch (type) {
11276 case NODE_TRUE:
11277 case NODE_FALSE:
11278 case NODE_NIL:
11279 case NODE_LIT:
11280 return value;
11281
11282 case NODE_DSTR:
11283 if (shareable == shareable_literal) {
11284 value = NEW_CALL(value, idUMinus, 0, loc);
11285 }
11286 return value;
11287
11288 case NODE_STR:
11289 lit = rb_fstring(value->nd_lit);
11290 nd_set_type(value, NODE_LIT);
11291 RB_OBJ_WRITE(p->ast, &value->nd_lit, lit);
11292 return value;
11293
11294 case NODE_ZLIST:
11295 lit = rb_ary_new();
11296 OBJ_FREEZE_RAW(lit);
11297 NODE *n = NEW_LIT(lit, loc);
11298 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11299 return n;
11300
11301 case NODE_LIST:
11302 lit = rb_ary_new();
11303 for (NODE *n = value; n; n = n->nd_next) {
11304 NODE *elt = n->nd_head;
11305 if (elt) {
11306 elt = shareable_literal_constant_next(elt);
11307 if (elt) {
11308 n->nd_head = elt;
11309 }
11310 else if (RTEST(lit)) {
11311 rb_ary_clear(lit);
11312 lit = Qfalse;
11313 }
11314 }
11315 if (RTEST(lit)) {
11316 VALUE e = shareable_literal_value(elt);
11317 if (e != Qundef) {
11318 rb_ary_push(lit, e);
11319 }
11320 else {
11321 rb_ary_clear(lit);
11322 lit = Qnil; /* make shareable at runtime */
11323 }
11324 }
11325 }
11326 break;
11327
11328 case NODE_HASH:
11329 if (!value->nd_brace) return 0;
11330 lit = rb_hash_new();
11331 for (NODE *n = value->nd_head; n; n = n->nd_next->nd_next) {
11332 NODE *key = n->nd_head;
11333 NODE *val = n->nd_next->nd_head;
11334 if (key) {
11335 key = shareable_literal_constant_next(key);
11336 if (key) {
11337 n->nd_head = key;
11338 }
11339 else if (RTEST(lit)) {
11340 rb_hash_clear(lit);
11341 lit = Qfalse;
11342 }
11343 }
11344 if (val) {
11345 val = shareable_literal_constant_next(val);
11346 if (val) {
11347 n->nd_next->nd_head = val;
11348 }
11349 else if (RTEST(lit)) {
11350 rb_hash_clear(lit);
11351 lit = Qfalse;
11352 }
11353 }
11354 if (RTEST(lit)) {
11355 VALUE k = shareable_literal_value(key);
11356 VALUE v = shareable_literal_value(val);
11357 if (k != Qundef && v != Qundef) {
11358 rb_hash_aset(lit, k, v);
11359 }
11360 else {
11361 rb_hash_clear(lit);
11362 lit = Qnil; /* make shareable at runtime */
11363 }
11364 }
11365 }
11366 break;
11367
11368 default:
11369 if (shareable == shareable_literal &&
11370 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
11371 return ensure_shareable_node(p, dest, value, loc);
11372 }
11373 return 0;
11374 }
11375
11376 /* Array or Hash */
11377 if (!lit) return 0;
11378 if (NIL_P(lit)) {
11379 // if shareable_literal, all elements should have been ensured
11380 // as shareable
11381 value = make_shareable_node(p, value, false, loc);
11382 }
11383 else {
11384 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
11385 RB_OBJ_WRITTEN(p->ast, Qnil, value->nd_lit);
11386 }
11387
11388 return value;
11389# undef shareable_literal_constant_next
11390}
11391
11392static NODE *
11393shareable_constant_value(struct parser_params *p, enum shareability shareable,
11394 NODE *lhs, NODE *value, const YYLTYPE *loc)
11395{
11396 if (!value) return 0;
11397 switch (shareable) {
11398 case shareable_none:
11399 return value;
11400
11401 case shareable_literal:
11402 {
11403 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11404 if (lit) return lit;
11405 return value;
11406 }
11407 break;
11408
11409 case shareable_copy:
11410 case shareable_everything:
11411 {
11412 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11413 if (lit) return lit;
11414 return make_shareable_node(p, value, shareable == shareable_copy, loc);
11415 }
11416 break;
11417
11418 default:
11419 UNREACHABLE_RETURN(0);
11420 }
11421}
11422
11423static NODE *
11424node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
11425{
11426 if (!lhs) return 0;
11427
11428 switch (nd_type(lhs)) {
11429 case NODE_CDECL:
11430 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
11431 /* fallthru */
11432
11433 case NODE_GASGN:
11434 case NODE_IASGN:
11435 case NODE_LASGN:
11436 case NODE_DASGN:
11437 case NODE_MASGN:
11438 case NODE_CVASGN:
11439 lhs->nd_value = rhs;
11440 nd_set_loc(lhs, loc);
11441 break;
11442
11443 case NODE_ATTRASGN:
11444 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
11445 nd_set_loc(lhs, loc);
11446 break;
11447
11448 default:
11449 /* should not happen */
11450 break;
11451 }
11452
11453 return lhs;
11454}
11455
11456static NODE *
11457value_expr_check(struct parser_params *p, NODE *node)
11458{
11459 NODE *void_node = 0, *vn;
11460
11461 if (!node) {
11462 rb_warning0("empty expression");
11463 }
11464 while (node) {
11465 switch (nd_type(node)) {
11466 case NODE_RETURN:
11467 case NODE_BREAK:
11468 case NODE_NEXT:
11469 case NODE_REDO:
11470 case NODE_RETRY:
11471 return void_node ? void_node : node;
11472
11473 case NODE_CASE3:
11474 if (!node->nd_body || !nd_type_p(node->nd_body, NODE_IN)) {
11475 compile_error(p, "unexpected node");
11476 return NULL;
11477 }
11478 if (node->nd_body->nd_body) {
11479 return NULL;
11480 }
11481 /* single line pattern matching */
11482 return void_node ? void_node : node;
11483
11484 case NODE_BLOCK:
11485 while (node->nd_next) {
11486 node = node->nd_next;
11487 }
11488 node = node->nd_head;
11489 break;
11490
11491 case NODE_BEGIN:
11492 node = node->nd_body;
11493 break;
11494
11495 case NODE_IF:
11496 case NODE_UNLESS:
11497 if (!node->nd_body) {
11498 return NULL;
11499 }
11500 else if (!node->nd_else) {
11501 return NULL;
11502 }
11503 vn = value_expr_check(p, node->nd_body);
11504 if (!vn) return NULL;
11505 if (!void_node) void_node = vn;
11506 node = node->nd_else;
11507 break;
11508
11509 case NODE_AND:
11510 case NODE_OR:
11511 node = node->nd_1st;
11512 break;
11513
11514 case NODE_LASGN:
11515 case NODE_DASGN:
11516 case NODE_MASGN:
11517 mark_lvar_used(p, node);
11518 return NULL;
11519
11520 default:
11521 return NULL;
11522 }
11523 }
11524
11525 return NULL;
11526}
11527
11528static int
11529value_expr_gen(struct parser_params *p, NODE *node)
11530{
11531 NODE *void_node = value_expr_check(p, node);
11532 if (void_node) {
11533 yyerror1(&void_node->nd_loc, "void value expression");
11534 /* or "control never reach"? */
11535 return FALSE;
11536 }
11537 return TRUE;
11538}
11539static void
11540void_expr(struct parser_params *p, NODE *node)
11541{
11542 const char *useless = 0;
11543
11544 if (!RTEST(ruby_verbose)) return;
11545
11546 if (!node || !(node = nd_once_body(node))) return;
11547 switch (nd_type(node)) {
11548 case NODE_OPCALL:
11549 switch (node->nd_mid) {
11550 case '+':
11551 case '-':
11552 case '*':
11553 case '/':
11554 case '%':
11555 case tPOW:
11556 case tUPLUS:
11557 case tUMINUS:
11558 case '|':
11559 case '^':
11560 case '&':
11561 case tCMP:
11562 case '>':
11563 case tGEQ:
11564 case '<':
11565 case tLEQ:
11566 case tEQ:
11567 case tNEQ:
11568 useless = rb_id2name(node->nd_mid);
11569 break;
11570 }
11571 break;
11572
11573 case NODE_LVAR:
11574 case NODE_DVAR:
11575 case NODE_GVAR:
11576 case NODE_IVAR:
11577 case NODE_CVAR:
11578 case NODE_NTH_REF:
11579 case NODE_BACK_REF:
11580 useless = "a variable";
11581 break;
11582 case NODE_CONST:
11583 useless = "a constant";
11584 break;
11585 case NODE_LIT:
11586 case NODE_STR:
11587 case NODE_DSTR:
11588 case NODE_DREGX:
11589 useless = "a literal";
11590 break;
11591 case NODE_COLON2:
11592 case NODE_COLON3:
11593 useless = "::";
11594 break;
11595 case NODE_DOT2:
11596 useless = "..";
11597 break;
11598 case NODE_DOT3:
11599 useless = "...";
11600 break;
11601 case NODE_SELF:
11602 useless = "self";
11603 break;
11604 case NODE_NIL:
11605 useless = "nil";
11606 break;
11607 case NODE_TRUE:
11608 useless = "true";
11609 break;
11610 case NODE_FALSE:
11611 useless = "false";
11612 break;
11613 case NODE_DEFINED:
11614 useless = "defined?";
11615 break;
11616 }
11617
11618 if (useless) {
11619 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
11620 }
11621}
11622
11623static NODE *
11624void_stmts(struct parser_params *p, NODE *node)
11625{
11626 NODE *const n = node;
11627 if (!RTEST(ruby_verbose)) return n;
11628 if (!node) return n;
11629 if (!nd_type_p(node, NODE_BLOCK)) return n;
11630
11631 while (node->nd_next) {
11632 void_expr(p, node->nd_head);
11633 node = node->nd_next;
11634 }
11635 return n;
11636}
11637
11638static NODE *
11639remove_begin(NODE *node)
11640{
11641 NODE **n = &node, *n1 = node;
11642 while (n1 && nd_type_p(n1, NODE_BEGIN) && n1->nd_body) {
11643 *n = n1 = n1->nd_body;
11644 }
11645 return node;
11646}
11647
11648static NODE *
11649remove_begin_all(NODE *node)
11650{
11651 NODE **n = &node, *n1 = node;
11652 while (n1 && nd_type_p(n1, NODE_BEGIN)) {
11653 *n = n1 = n1->nd_body;
11654 }
11655 return node;
11656}
11657
11658static void
11659reduce_nodes(struct parser_params *p, NODE **body)
11660{
11661 NODE *node = *body;
11662
11663 if (!node) {
11664 *body = NEW_NIL(&NULL_LOC);
11665 return;
11666 }
11667#define subnodes(n1, n2) \
11668 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
11669 (!node->n2) ? (body = &node->n1, 1) : \
11670 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
11671
11672 while (node) {
11673 int newline = (int)(node->flags & NODE_FL_NEWLINE);
11674 switch (nd_type(node)) {
11675 end:
11676 case NODE_NIL:
11677 *body = 0;
11678 return;
11679 case NODE_RETURN:
11680 *body = node = node->nd_stts;
11681 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11682 continue;
11683 case NODE_BEGIN:
11684 *body = node = node->nd_body;
11685 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11686 continue;
11687 case NODE_BLOCK:
11688 body = &node->nd_end->nd_head;
11689 break;
11690 case NODE_IF:
11691 case NODE_UNLESS:
11692 if (subnodes(nd_body, nd_else)) break;
11693 return;
11694 case NODE_CASE:
11695 body = &node->nd_body;
11696 break;
11697 case NODE_WHEN:
11698 if (!subnodes(nd_body, nd_next)) goto end;
11699 break;
11700 case NODE_ENSURE:
11701 if (!subnodes(nd_head, nd_resq)) goto end;
11702 break;
11703 case NODE_RESCUE:
11704 if (node->nd_else) {
11705 body = &node->nd_resq;
11706 break;
11707 }
11708 if (!subnodes(nd_head, nd_resq)) goto end;
11709 break;
11710 default:
11711 return;
11712 }
11713 node = *body;
11714 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11715 }
11716
11717#undef subnodes
11718}
11719
11720static int
11721is_static_content(NODE *node)
11722{
11723 if (!node) return 1;
11724 switch (nd_type(node)) {
11725 case NODE_HASH:
11726 if (!(node = node->nd_head)) break;
11727 case NODE_LIST:
11728 do {
11729 if (!is_static_content(node->nd_head)) return 0;
11730 } while ((node = node->nd_next) != 0);
11731 case NODE_LIT:
11732 case NODE_STR:
11733 case NODE_NIL:
11734 case NODE_TRUE:
11735 case NODE_FALSE:
11736 case NODE_ZLIST:
11737 break;
11738 default:
11739 return 0;
11740 }
11741 return 1;
11742}
11743
11744static int
11745assign_in_cond(struct parser_params *p, NODE *node)
11746{
11747 switch (nd_type(node)) {
11748 case NODE_MASGN:
11749 case NODE_LASGN:
11750 case NODE_DASGN:
11751 case NODE_GASGN:
11752 case NODE_IASGN:
11753 break;
11754
11755 default:
11756 return 0;
11757 }
11758
11759 if (!node->nd_value) return 1;
11760 if (is_static_content(node->nd_value)) {
11761 /* reports always */
11762 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11763 }
11764 return 1;
11765}
11766
11767enum cond_type {
11768 COND_IN_OP,
11769 COND_IN_COND,
11770 COND_IN_FF
11771};
11772
11773#define SWITCH_BY_COND_TYPE(t, w, arg) \
11774 switch (t) { \
11775 case COND_IN_OP: break; \
11776 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11777 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11778 }
11779
11780static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11781
11782static NODE*
11783range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11784{
11785 enum node_type type;
11786
11787 if (node == 0) return 0;
11788
11789 type = nd_type(node);
11790 value_expr(node);
11791 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11792 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11793 ID lineno = rb_intern("$.");
11794 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
11795 }
11796 return cond0(p, node, COND_IN_FF, loc);
11797}
11798
11799static NODE*
11800cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11801{
11802 if (node == 0) return 0;
11803 if (!(node = nd_once_body(node))) return 0;
11804 assign_in_cond(p, node);
11805
11806 switch (nd_type(node)) {
11807 case NODE_DSTR:
11808 case NODE_EVSTR:
11809 case NODE_STR:
11810 SWITCH_BY_COND_TYPE(type, warn, "string ")
11811 break;
11812
11813 case NODE_DREGX:
11814 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11815
11816 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11817
11818 case NODE_AND:
11819 case NODE_OR:
11820 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11821 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11822 break;
11823
11824 case NODE_DOT2:
11825 case NODE_DOT3:
11826 node->nd_beg = range_op(p, node->nd_beg, loc);
11827 node->nd_end = range_op(p, node->nd_end, loc);
11828 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
11829 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
11830 break;
11831
11832 case NODE_DSYM:
11833 warn_symbol:
11834 SWITCH_BY_COND_TYPE(type, warning, "symbol ")
11835 break;
11836
11837 case NODE_LIT:
11838 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11839 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11840 nd_set_type(node, NODE_MATCH);
11841 }
11842 else if (node->nd_lit == Qtrue ||
11843 node->nd_lit == Qfalse) {
11844 /* booleans are OK, e.g., while true */
11845 }
11846 else if (SYMBOL_P(node->nd_lit)) {
11847 goto warn_symbol;
11848 }
11849 else {
11850 SWITCH_BY_COND_TYPE(type, warning, "")
11851 }
11852 default:
11853 break;
11854 }
11855 return node;
11856}
11857
11858static NODE*
11859cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11860{
11861 if (node == 0) return 0;
11862 return cond0(p, node, COND_IN_COND, loc);
11863}
11864
11865static NODE*
11866method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11867{
11868 if (node == 0) return 0;
11869 return cond0(p, node, COND_IN_OP, loc);
11870}
11871
11872static NODE*
11873new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
11874{
11875 YYLTYPE loc = {*pos, *pos};
11876 return NEW_NIL(&loc);
11877}
11878
11879static NODE*
11880new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11881{
11882 if (!cc) return right;
11883 cc = cond0(p, cc, COND_IN_COND, loc);
11884 return newline_node(NEW_IF(cc, left, right, loc));
11885}
11886
11887static NODE*
11888new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11889{
11890 if (!cc) return right;
11891 cc = cond0(p, cc, COND_IN_COND, loc);
11892 return newline_node(NEW_UNLESS(cc, left, right, loc));
11893}
11894
11895static NODE*
11896logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11897 const YYLTYPE *op_loc, const YYLTYPE *loc)
11898{
11899 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11900 NODE *op;
11901 value_expr(left);
11902 if (left && nd_type_p(left, type)) {
11903 NODE *node = left, *second;
11904 while ((second = node->nd_2nd) != 0 && nd_type_p(second, type)) {
11905 node = second;
11906 }
11907 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11908 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11909 left->nd_loc.end_pos = loc->end_pos;
11910 return left;
11911 }
11912 op = NEW_NODE(type, left, right, 0, loc);
11913 nd_set_line(op, op_loc->beg_pos.lineno);
11914 return op;
11915}
11916
11917static void
11918no_blockarg(struct parser_params *p, NODE *node)
11919{
11920 if (node && nd_type_p(node, NODE_BLOCK_PASS)) {
11921 compile_error(p, "block argument should not be given");
11922 }
11923}
11924
11925static NODE *
11926ret_args(struct parser_params *p, NODE *node)
11927{
11928 if (node) {
11929 no_blockarg(p, node);
11930 if (nd_type_p(node, NODE_LIST)) {
11931 if (node->nd_next == 0) {
11932 node = node->nd_head;
11933 }
11934 else {
11935 nd_set_type(node, NODE_VALUES);
11936 }
11937 }
11938 }
11939 return node;
11940}
11941
11942static NODE *
11943new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11944{
11945 if (node) no_blockarg(p, node);
11946
11947 return NEW_YIELD(node, loc);
11948}
11949
11950static VALUE
11951negate_lit(struct parser_params *p, VALUE lit)
11952{
11953 if (FIXNUM_P(lit)) {
11954 return LONG2FIX(-FIX2LONG(lit));
11955 }
11956 if (SPECIAL_CONST_P(lit)) {
11957#if USE_FLONUM
11958 if (FLONUM_P(lit)) {
11959 return DBL2NUM(-RFLOAT_VALUE(lit));
11960 }
11961#endif
11962 goto unknown;
11963 }
11964 switch (BUILTIN_TYPE(lit)) {
11965 case T_BIGNUM:
11966 BIGNUM_NEGATE(lit);
11967 lit = rb_big_norm(lit);
11968 break;
11969 case T_RATIONAL:
11970 RATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11971 break;
11972 case T_COMPLEX:
11973 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11974 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11975 break;
11976 case T_FLOAT:
11977 lit = DBL2NUM(-RFLOAT_VALUE(lit));
11978 break;
11979 unknown:
11980 default:
11981 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11982 rb_builtin_class_name(lit));
11983 break;
11984 }
11985 return lit;
11986}
11987
11988static NODE *
11989arg_blk_pass(NODE *node1, NODE *node2)
11990{
11991 if (node2) {
11992 if (!node1) return node2;
11993 node2->nd_head = node1;
11994 nd_set_first_lineno(node2, nd_first_lineno(node1));
11995 nd_set_first_column(node2, nd_first_column(node1));
11996 return node2;
11997 }
11998 return node1;
11999}
12000
12001static bool
12002args_info_empty_p(struct rb_args_info *args)
12003{
12004 if (args->pre_args_num) return false;
12005 if (args->post_args_num) return false;
12006 if (args->rest_arg) return false;
12007 if (args->opt_args) return false;
12008 if (args->block_arg) return false;
12009 if (args->kw_args) return false;
12010 if (args->kw_rest_arg) return false;
12011 return true;
12012}
12013
12014static NODE*
12015new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
12016{
12017 int saved_line = p->ruby_sourceline;
12018 struct rb_args_info *args = tail->nd_ainfo;
12019
12020 if (args->block_arg == idFWD_BLOCK) {
12021 if (rest_arg) {
12022 yyerror1(&tail->nd_loc, "... after rest argument");
12023 return tail;
12024 }
12025 rest_arg = idFWD_REST;
12026 }
12027
12028 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
12029 args->pre_init = pre_args ? pre_args->nd_next : 0;
12030
12031 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
12032 args->post_init = post_args ? post_args->nd_next : 0;
12033 args->first_post_arg = post_args ? post_args->nd_pid : 0;
12034
12035 args->rest_arg = rest_arg;
12036
12037 args->opt_args = opt_args;
12038
12039 args->ruby2_keywords = rest_arg == idFWD_REST;
12040
12041 p->ruby_sourceline = saved_line;
12042 nd_set_loc(tail, loc);
12043
12044 return tail;
12045}
12046
12047static NODE*
12048new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
12049{
12050 int saved_line = p->ruby_sourceline;
12051 NODE *node;
12052 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12053 struct rb_args_info *args = ZALLOC(struct rb_args_info);
12054 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
12055 args->imemo = tmpbuf;
12056 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
12057 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12058 if (p->error_p) return node;
12059
12060 args->block_arg = block;
12061 args->kw_args = kw_args;
12062
12063 if (kw_args) {
12064 /*
12065 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
12066 * variable order: k1, kr1, k2, &b, internal_id, krest
12067 * #=> <reorder>
12068 * variable order: kr1, k1, k2, internal_id, krest, &b
12069 */
12070 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
12071 struct vtable *vtargs = p->lvtbl->args;
12072 NODE *kwn = kw_args;
12073
12074 if (block) block = vtargs->tbl[vtargs->pos-1];
12075 vtable_pop(vtargs, !!block + !!kw_rest_arg);
12076 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
12077 while (kwn) {
12078 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
12079 --kw_vars;
12080 --required_kw_vars;
12081 kwn = kwn->nd_next;
12082 }
12083
12084 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
12085 ID vid = kwn->nd_body->nd_vid;
12086 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
12087 *required_kw_vars++ = vid;
12088 }
12089 else {
12090 *kw_vars++ = vid;
12091 }
12092 }
12093
12094 arg_var(p, kw_bits);
12095 if (kw_rest_arg) arg_var(p, kw_rest_arg);
12096 if (block) arg_var(p, block);
12097
12098 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12099 args->kw_rest_arg->nd_cflag = kw_bits;
12100 }
12101 else if (kw_rest_arg == idNil) {
12102 args->no_kwarg = 1;
12103 }
12104 else if (kw_rest_arg) {
12105 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12106 }
12107
12108 p->ruby_sourceline = saved_line;
12109 return node;
12110}
12111
12112static NODE *
12113args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
12114{
12115 if (max_numparam > NO_PARAM) {
12116 if (!args) {
12117 YYLTYPE loc = RUBY_INIT_YYLLOC();
12118 args = new_args_tail(p, 0, 0, 0, 0);
12119 nd_set_loc(args, &loc);
12120 }
12121 args->nd_ainfo->pre_args_num = max_numparam;
12122 }
12123 return args;
12124}
12125
12126static NODE*
12127new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
12128{
12129 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
12130
12131 aryptn->nd_pconst = constant;
12132
12133 if (pre_arg) {
12134 NODE *pre_args = NEW_LIST(pre_arg, loc);
12135 if (apinfo->pre_args) {
12136 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
12137 }
12138 else {
12139 apinfo->pre_args = pre_args;
12140 }
12141 }
12142 return aryptn;
12143}
12144
12145static NODE*
12146new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
12147{
12148 int saved_line = p->ruby_sourceline;
12149 NODE *node;
12150 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12151 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
12152 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
12153 node = NEW_NODE(NODE_ARYPTN, 0, tmpbuf, apinfo, loc);
12154 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12155
12156 apinfo->pre_args = pre_args;
12157
12158 if (has_rest) {
12159 if (rest_arg) {
12160 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
12161 }
12162 else {
12163 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
12164 }
12165 }
12166 else {
12167 apinfo->rest_arg = NULL;
12168 }
12169
12170 apinfo->post_args = post_args;
12171
12172 p->ruby_sourceline = saved_line;
12173 return node;
12174}
12175
12176static NODE*
12177new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
12178{
12179 fndptn->nd_pconst = constant;
12180
12181 return fndptn;
12182}
12183
12184static NODE*
12185new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc)
12186{
12187 int saved_line = p->ruby_sourceline;
12188 NODE *node;
12189 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12190 struct rb_fnd_pattern_info *fpinfo = ZALLOC(struct rb_fnd_pattern_info);
12191 rb_imemo_tmpbuf_set_ptr(tmpbuf, fpinfo);
12192 node = NEW_NODE(NODE_FNDPTN, 0, tmpbuf, fpinfo, loc);
12193 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12194
12195 fpinfo->pre_rest_arg = pre_rest_arg ? assignable(p, pre_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12196 fpinfo->args = args;
12197 fpinfo->post_rest_arg = post_rest_arg ? assignable(p, post_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12198
12199 p->ruby_sourceline = saved_line;
12200 return node;
12201}
12202
12203static NODE*
12204new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
12205{
12206 hshptn->nd_pconst = constant;
12207 return hshptn;
12208}
12209
12210static NODE*
12211new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
12212{
12213 int saved_line = p->ruby_sourceline;
12214 NODE *node, *kw_rest_arg_node;
12215
12216 if (kw_rest_arg == idNil) {
12217 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
12218 }
12219 else if (kw_rest_arg) {
12220 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
12221 }
12222 else {
12223 kw_rest_arg_node = NULL;
12224 }
12225
12226 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
12227
12228 p->ruby_sourceline = saved_line;
12229 return node;
12230}
12231
12232static NODE*
12233dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12234{
12235 VALUE lit;
12236
12237 if (!node) {
12238 return NEW_LIT(ID2SYM(idNULL), loc);
12239 }
12240
12241 switch (nd_type(node)) {
12242 case NODE_DSTR:
12243 nd_set_type(node, NODE_DSYM);
12244 nd_set_loc(node, loc);
12245 break;
12246 case NODE_STR:
12247 lit = node->nd_lit;
12248 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
12249 nd_set_type(node, NODE_LIT);
12250 nd_set_loc(node, loc);
12251 break;
12252 default:
12253 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
12254 break;
12255 }
12256 return node;
12257}
12258
12259static int
12260append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
12261{
12262 NODE *node = (NODE *)v;
12263 NODE **result = (NODE **)h;
12264 node->nd_alen = 2;
12265 node->nd_next->nd_end = node->nd_next;
12266 node->nd_next->nd_next = 0;
12267 if (*result)
12268 list_concat(*result, node);
12269 else
12270 *result = node;
12271 return ST_CONTINUE;
12272}
12273
12274static bool
12275hash_literal_key_p(VALUE k)
12276{
12277 switch (OBJ_BUILTIN_TYPE(k)) {
12278 case T_NODE:
12279 return false;
12280 default:
12281 return true;
12282 }
12283}
12284
12285static int
12286literal_cmp(VALUE val, VALUE lit)
12287{
12288 if (val == lit) return 0;
12289 if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
12290 return rb_iseq_cdhash_cmp(val, lit);
12291}
12292
12293static st_index_t
12294literal_hash(VALUE a)
12295{
12296 if (!hash_literal_key_p(a)) return (st_index_t)a;
12297 return rb_iseq_cdhash_hash(a);
12298}
12299
12300static const struct st_hash_type literal_type = {
12301 literal_cmp,
12302 literal_hash,
12303};
12304
12305static NODE *
12306remove_duplicate_keys(struct parser_params *p, NODE *hash)
12307{
12308 st_table *literal_keys = st_init_table_with_size(&literal_type, hash->nd_alen / 2);
12309 NODE *result = 0;
12310 NODE *last_expr = 0;
12311 rb_code_location_t loc = hash->nd_loc;
12312 while (hash && hash->nd_head && hash->nd_next) {
12313 NODE *head = hash->nd_head;
12314 NODE *value = hash->nd_next;
12315 NODE *next = value->nd_next;
12316 st_data_t key = (st_data_t)head;
12317 st_data_t data;
12318 value->nd_next = 0;
12319 if (nd_type_p(head, NODE_LIT) &&
12320 st_delete(literal_keys, (key = (st_data_t)head->nd_lit, &key), &data)) {
12321 NODE *dup_value = ((NODE *)data)->nd_next;
12322 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
12323 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
12324 head->nd_lit, nd_line(head));
12325 if (dup_value == last_expr) {
12326 value->nd_head = block_append(p, dup_value->nd_head, value->nd_head);
12327 }
12328 else {
12329 last_expr->nd_head = block_append(p, dup_value->nd_head, last_expr->nd_head);
12330 }
12331 }
12332 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
12333 last_expr = nd_type_p(head, NODE_LIT) ? value : head;
12334 hash = next;
12335 }
12336 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
12337 st_free_table(literal_keys);
12338 if (hash) {
12339 if (!result) result = hash;
12340 else list_concat(result, hash);
12341 }
12342 result->nd_loc = loc;
12343 return result;
12344}
12345
12346static NODE *
12347new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12348{
12349 if (hash) hash = remove_duplicate_keys(p, hash);
12350 return NEW_HASH(hash, loc);
12351}
12352#endif
12353
12354static void
12355error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
12356{
12357 if (is_private_local_id(id)) {
12358 return;
12359 }
12360 if (st_is_member(p->pvtbl, id)) {
12361 yyerror1(loc, "duplicated variable name");
12362 }
12363 else {
12364 st_insert(p->pvtbl, (st_data_t)id, 0);
12365 }
12366}
12367
12368static void
12369error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
12370{
12371 if (!p->pktbl) {
12372 p->pktbl = st_init_numtable();
12373 }
12374 else if (st_is_member(p->pktbl, key)) {
12375 yyerror1(loc, "duplicated key name");
12376 return;
12377 }
12378 st_insert(p->pktbl, (st_data_t)key, 0);
12379}
12380
12381#ifndef RIPPER
12382static NODE *
12383new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12384{
12385 return NEW_HASH(hash, loc);
12386}
12387#endif /* !RIPPER */
12388
12389#ifndef RIPPER
12390static NODE *
12391new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12392{
12393 NODE *asgn;
12394
12395 if (lhs) {
12396 ID vid = lhs->nd_vid;
12397 YYLTYPE lhs_loc = lhs->nd_loc;
12398 int shareable = ctxt.shareable_constant_value;
12399 if (shareable) {
12400 switch (nd_type(lhs)) {
12401 case NODE_CDECL:
12402 case NODE_COLON2:
12403 case NODE_COLON3:
12404 break;
12405 default:
12406 shareable = 0;
12407 break;
12408 }
12409 }
12410 if (op == tOROP) {
12411 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12412 lhs->nd_value = rhs;
12413 nd_set_loc(lhs, loc);
12414 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
12415 if (is_notop_id(vid)) {
12416 switch (id_type(vid)) {
12417 case ID_GLOBAL:
12418 case ID_INSTANCE:
12419 case ID_CLASS:
12420 asgn->nd_aid = vid;
12421 }
12422 }
12423 }
12424 else if (op == tANDOP) {
12425 if (shareable) {
12426 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12427 }
12428 lhs->nd_value = rhs;
12429 nd_set_loc(lhs, loc);
12430 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
12431 }
12432 else {
12433 asgn = lhs;
12434 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
12435 if (shareable) {
12436 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12437 }
12438 asgn->nd_value = rhs;
12439 nd_set_loc(asgn, loc);
12440 }
12441 }
12442 else {
12443 asgn = NEW_BEGIN(0, loc);
12444 }
12445 return asgn;
12446}
12447
12448static NODE *
12449new_ary_op_assign(struct parser_params *p, NODE *ary,
12450 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
12451{
12452 NODE *asgn;
12453
12454 args = make_list(args, args_loc);
12455 if (nd_type_p(args, NODE_BLOCK_PASS)) {
12456 args = NEW_ARGSCAT(args, rhs, loc);
12457 }
12458 else {
12459 args = arg_concat(p, args, rhs, loc);
12460 }
12461 asgn = NEW_OP_ASGN1(ary, op, args, loc);
12462 fixpos(asgn, ary);
12463 return asgn;
12464}
12465
12466static NODE *
12467new_attr_op_assign(struct parser_params *p, NODE *lhs,
12468 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
12469{
12470 NODE *asgn;
12471
12472 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
12473 fixpos(asgn, lhs);
12474 return asgn;
12475}
12476
12477static NODE *
12478new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12479{
12480 NODE *asgn;
12481
12482 if (lhs) {
12483 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
12484 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
12485 }
12486 else {
12487 asgn = NEW_BEGIN(0, loc);
12488 }
12489 fixpos(asgn, lhs);
12490 return asgn;
12491}
12492
12493static NODE *
12494const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
12495{
12496 if (p->ctxt.in_def) {
12497 yyerror1(loc, "dynamic constant assignment");
12498 }
12499 return NEW_CDECL(0, 0, (path), loc);
12500}
12501#else
12502static VALUE
12503const_decl(struct parser_params *p, VALUE path)
12504{
12505 if (p->ctxt.in_def) {
12506 path = assign_error(p, "dynamic constant assignment", path);
12507 }
12508 return path;
12509}
12510
12511static VALUE
12512assign_error(struct parser_params *p, const char *mesg, VALUE a)
12513{
12514 a = dispatch2(assign_error, ERR_MESG(), a);
12515 ripper_error(p);
12516 return a;
12517}
12518
12519static VALUE
12520var_field(struct parser_params *p, VALUE a)
12521{
12522 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
12523}
12524#endif
12525
12526#ifndef RIPPER
12527static NODE *
12528new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
12529{
12530 NODE *result = head;
12531 if (rescue) {
12532 NODE *tmp = rescue_else ? rescue_else : rescue;
12533 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
12534
12535 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
12536 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
12537 }
12538 else if (rescue_else) {
12539 result = block_append(p, result, rescue_else);
12540 }
12541 if (ensure) {
12542 result = NEW_ENSURE(result, ensure, loc);
12543 }
12544 fixpos(result, head);
12545 return result;
12546}
12547#endif
12548
12549static void
12550warn_unused_var(struct parser_params *p, struct local_vars *local)
12551{
12552 int cnt;
12553
12554 if (!local->used) return;
12555 cnt = local->used->pos;
12556 if (cnt != local->vars->pos) {
12557 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
12558 }
12559#ifndef RIPPER
12560 ID *v = local->vars->tbl;
12561 ID *u = local->used->tbl;
12562 for (int i = 0; i < cnt; ++i) {
12563 if (!v[i] || (u[i] & LVAR_USED)) continue;
12564 if (is_private_local_id(v[i])) continue;
12565 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
12566 }
12567#endif
12568}
12569
12570static void
12571local_push(struct parser_params *p, int toplevel_scope)
12572{
12573 struct local_vars *local;
12574 int inherits_dvars = toplevel_scope && compile_for_eval;
12575 int warn_unused_vars = RTEST(ruby_verbose);
12576
12577 local = ALLOC(struct local_vars);
12578 local->prev = p->lvtbl;
12579 local->args = vtable_alloc(0);
12580 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
12581#ifndef RIPPER
12582 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
12583 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
12584 local->numparam.outer = 0;
12585 local->numparam.inner = 0;
12586 local->numparam.current = 0;
12587#endif
12588 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
12589
12590# if WARN_PAST_SCOPE
12591 local->past = 0;
12592# endif
12593 CMDARG_PUSH(0);
12594 COND_PUSH(0);
12595 p->lvtbl = local;
12596}
12597
12598static void
12599local_pop(struct parser_params *p)
12600{
12601 struct local_vars *local = p->lvtbl->prev;
12602 if (p->lvtbl->used) {
12603 warn_unused_var(p, p->lvtbl);
12604 vtable_free(p->lvtbl->used);
12605 }
12606# if WARN_PAST_SCOPE
12607 while (p->lvtbl->past) {
12608 struct vtable *past = p->lvtbl->past;
12609 p->lvtbl->past = past->prev;
12610 vtable_free(past);
12611 }
12612# endif
12613 vtable_free(p->lvtbl->args);
12614 vtable_free(p->lvtbl->vars);
12615 CMDARG_POP();
12616 COND_POP();
12617 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12618 p->lvtbl = local;
12619}
12620
12621#ifndef RIPPER
12622static rb_ast_id_table_t *
12623local_tbl(struct parser_params *p)
12624{
12625 int cnt_args = vtable_size(p->lvtbl->args);
12626 int cnt_vars = vtable_size(p->lvtbl->vars);
12627 int cnt = cnt_args + cnt_vars;
12628 int i, j;
12629 rb_ast_id_table_t *tbl;
12630
12631 if (cnt <= 0) return 0;
12632 tbl = rb_ast_new_local_table(p->ast, cnt);
12633 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
12634 /* remove IDs duplicated to warn shadowing */
12635 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
12636 ID id = p->lvtbl->vars->tbl[i];
12637 if (!vtable_included(p->lvtbl->args, id)) {
12638 tbl->ids[j++] = id;
12639 }
12640 }
12641 if (j < cnt) {
12642 tbl = rb_ast_resize_latest_local_table(p->ast, j);
12643 }
12644
12645 return tbl;
12646}
12647
12648static NODE*
12649node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
12650{
12651 rb_ast_id_table_t *a0;
12652 NODE *n;
12653
12654 a0 = local_tbl(p);
12655 n = NEW_NODE(type, a0, a1, a2, loc);
12656 return n;
12657}
12658
12659#endif
12660
12661static void
12662numparam_name(struct parser_params *p, ID id)
12663{
12664 if (!NUMPARAM_ID_P(id)) return;
12665 compile_error(p, "_%d is reserved for numbered parameter",
12666 NUMPARAM_ID_TO_IDX(id));
12667}
12668
12669static void
12670arg_var(struct parser_params *p, ID id)
12671{
12672 numparam_name(p, id);
12673 vtable_add(p->lvtbl->args, id);
12674}
12675
12676static void
12677local_var(struct parser_params *p, ID id)
12678{
12679 numparam_name(p, id);
12680 vtable_add(p->lvtbl->vars, id);
12681 if (p->lvtbl->used) {
12682 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
12683 }
12684}
12685
12686static int
12687local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
12688{
12689 struct vtable *vars, *args, *used;
12690
12691 vars = p->lvtbl->vars;
12692 args = p->lvtbl->args;
12693 used = p->lvtbl->used;
12694
12695 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
12696 vars = vars->prev;
12697 args = args->prev;
12698 if (used) used = used->prev;
12699 }
12700
12701 if (vars && vars->prev == DVARS_INHERIT) {
12702 return rb_local_defined(id, p->parent_iseq);
12703 }
12704 else if (vtable_included(args, id)) {
12705 return 1;
12706 }
12707 else {
12708 int i = vtable_included(vars, id);
12709 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
12710 return i != 0;
12711 }
12712}
12713
12714static int
12715local_id(struct parser_params *p, ID id)
12716{
12717 return local_id_ref(p, id, NULL);
12718}
12719
12720static int
12721check_forwarding_args(struct parser_params *p)
12722{
12723 if (local_id(p, idFWD_REST) &&
12724#if idFWD_KWREST
12725 local_id(p, idFWD_KWREST) &&
12726#endif
12727 local_id(p, idFWD_BLOCK)) return TRUE;
12728 compile_error(p, "unexpected ...");
12729 return FALSE;
12730}
12731
12732static void
12733add_forwarding_args(struct parser_params *p)
12734{
12735 arg_var(p, idFWD_REST);
12736#if idFWD_KWREST
12737 arg_var(p, idFWD_KWREST);
12738#endif
12739 arg_var(p, idFWD_BLOCK);
12740}
12741
12742#ifndef RIPPER
12743static NODE *
12744new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
12745{
12746 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, loc), loc);
12747#if idFWD_KWREST
12748 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
12749#endif
12750 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
12751 NODE *args = leading ? rest_arg_append(p, leading, splat, argsloc) : splat;
12752#if idFWD_KWREST
12753 args = arg_append(p, splat, new_hash(p, kwrest, loc), loc);
12754#endif
12755 return arg_blk_pass(args, block);
12756}
12757#endif
12758
12759static NODE *
12760numparam_push(struct parser_params *p)
12761{
12762#ifndef RIPPER
12763 struct local_vars *local = p->lvtbl;
12764 NODE *inner = local->numparam.inner;
12765 if (!local->numparam.outer) {
12766 local->numparam.outer = local->numparam.current;
12767 }
12768 local->numparam.inner = 0;
12769 local->numparam.current = 0;
12770 return inner;
12771#else
12772 return 0;
12773#endif
12774}
12775
12776static void
12777numparam_pop(struct parser_params *p, NODE *prev_inner)
12778{
12779#ifndef RIPPER
12780 struct local_vars *local = p->lvtbl;
12781 if (prev_inner) {
12782 /* prefer first one */
12783 local->numparam.inner = prev_inner;
12784 }
12785 else if (local->numparam.current) {
12786 /* current and inner are exclusive */
12787 local->numparam.inner = local->numparam.current;
12788 }
12789 if (p->max_numparam > NO_PARAM) {
12790 /* current and outer are exclusive */
12791 local->numparam.current = local->numparam.outer;
12792 local->numparam.outer = 0;
12793 }
12794 else {
12795 /* no numbered parameter */
12796 local->numparam.current = 0;
12797 }
12798#endif
12799}
12800
12801static const struct vtable *
12802dyna_push(struct parser_params *p)
12803{
12804 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
12805 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
12806 if (p->lvtbl->used) {
12807 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
12808 }
12809 return p->lvtbl->args;
12810}
12811
12812static void
12813dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
12814{
12815 struct vtable *tmp = *vtblp;
12816 *vtblp = tmp->prev;
12817# if WARN_PAST_SCOPE
12818 if (p->past_scope_enabled) {
12819 tmp->prev = p->lvtbl->past;
12820 p->lvtbl->past = tmp;
12821 return;
12822 }
12823# endif
12824 vtable_free(tmp);
12825}
12826
12827static void
12828dyna_pop_1(struct parser_params *p)
12829{
12830 struct vtable *tmp;
12831
12832 if ((tmp = p->lvtbl->used) != 0) {
12833 warn_unused_var(p, p->lvtbl);
12834 p->lvtbl->used = p->lvtbl->used->prev;
12835 vtable_free(tmp);
12836 }
12837 dyna_pop_vtable(p, &p->lvtbl->args);
12838 dyna_pop_vtable(p, &p->lvtbl->vars);
12839}
12840
12841static void
12842dyna_pop(struct parser_params *p, const struct vtable *lvargs)
12843{
12844 while (p->lvtbl->args != lvargs) {
12845 dyna_pop_1(p);
12846 if (!p->lvtbl->args) {
12847 struct local_vars *local = p->lvtbl->prev;
12848 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12849 p->lvtbl = local;
12850 }
12851 }
12852 dyna_pop_1(p);
12853}
12854
12855static int
12856dyna_in_block(struct parser_params *p)
12857{
12858 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12859}
12860
12861static int
12862dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12863{
12864 struct vtable *vars, *args, *used;
12865 int i;
12866
12867 args = p->lvtbl->args;
12868 vars = p->lvtbl->vars;
12869 used = p->lvtbl->used;
12870
12871 while (!DVARS_TERMINAL_P(vars)) {
12872 if (vtable_included(args, id)) {
12873 return 1;
12874 }
12875 if ((i = vtable_included(vars, id)) != 0) {
12876 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12877 return 1;
12878 }
12879 args = args->prev;
12880 vars = vars->prev;
12881 if (!vidrefp) used = 0;
12882 if (used) used = used->prev;
12883 }
12884
12885 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
12886 return rb_dvar_defined(id, p->parent_iseq);
12887 }
12888
12889 return 0;
12890}
12891
12892static int
12893dvar_defined(struct parser_params *p, ID id)
12894{
12895 return dvar_defined_ref(p, id, NULL);
12896}
12897
12898static int
12899dvar_curr(struct parser_params *p, ID id)
12900{
12901 return (vtable_included(p->lvtbl->args, id) ||
12902 vtable_included(p->lvtbl->vars, id));
12903}
12904
12905static void
12906reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12907{
12908 compile_error(p,
12909 "regexp encoding option '%c' differs from source encoding '%s'",
12910 c, rb_enc_name(rb_enc_get(str)));
12911}
12912
12913#ifndef RIPPER
12914int
12915rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12916{
12917 int c = RE_OPTION_ENCODING_IDX(options);
12918
12919 if (c) {
12920 int opt, idx;
12921 rb_char_to_option_kcode(c, &opt, &idx);
12922 if (idx != ENCODING_GET(str) &&
12923 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12924 goto error;
12925 }
12926 ENCODING_SET(str, idx);
12927 }
12928 else if (RE_OPTION_ENCODING_NONE(options)) {
12929 if (!ENCODING_IS_ASCII8BIT(str) &&
12930 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12931 c = 'n';
12932 goto error;
12933 }
12934 rb_enc_associate(str, rb_ascii8bit_encoding());
12935 }
12936 else if (p->enc == rb_usascii_encoding()) {
12937 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12938 /* raise in re.c */
12939 rb_enc_associate(str, rb_usascii_encoding());
12940 }
12941 else {
12942 rb_enc_associate(str, rb_ascii8bit_encoding());
12943 }
12944 }
12945 return 0;
12946
12947 error:
12948 return c;
12949}
12950
12951static void
12952reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12953{
12954 int c = rb_reg_fragment_setenc(p, str, options);
12955 if (c) reg_fragment_enc_error(p, str, c);
12956}
12957
12958static int
12959reg_fragment_check(struct parser_params* p, VALUE str, int options)
12960{
12961 VALUE err;
12962 reg_fragment_setenc(p, str, options);
12963 err = rb_reg_check_preprocess(str);
12964 if (err != Qnil) {
12965 err = rb_obj_as_string(err);
12966 compile_error(p, "%"PRIsVALUE, err);
12967 return 0;
12968 }
12969 return 1;
12970}
12971
12972typedef struct {
12973 struct parser_params* parser;
12974 rb_encoding *enc;
12975 NODE *succ_block;
12976 const YYLTYPE *loc;
12977} reg_named_capture_assign_t;
12978
12979static int
12980reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12981 int back_num, int *back_refs, OnigRegex regex, void *arg0)
12982{
12983 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12984 struct parser_params* p = arg->parser;
12985 rb_encoding *enc = arg->enc;
12986 long len = name_end - name;
12987 const char *s = (const char *)name;
12988 ID var;
12989 NODE *node, *succ;
12990
12991 if (!len) return ST_CONTINUE;
12992 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
12993 return ST_CONTINUE;
12994
12995 var = intern_cstr(s, len, enc);
12996 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
12997 if (!lvar_defined(p, var)) return ST_CONTINUE;
12998 }
12999 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc);
13000 succ = arg->succ_block;
13001 if (!succ) succ = NEW_BEGIN(0, arg->loc);
13002 succ = block_append(p, succ, node);
13003 arg->succ_block = succ;
13004 return ST_CONTINUE;
13005}
13006
13007static NODE *
13008reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
13009{
13010 reg_named_capture_assign_t arg;
13011
13012 arg.parser = p;
13013 arg.enc = rb_enc_get(regexp);
13014 arg.succ_block = 0;
13015 arg.loc = loc;
13016 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
13017
13018 if (!arg.succ_block) return 0;
13019 return arg.succ_block->nd_next;
13020}
13021
13022static VALUE
13023parser_reg_compile(struct parser_params* p, VALUE str, int options)
13024{
13025 reg_fragment_setenc(p, str, options);
13026 return rb_parser_reg_compile(p, str, options);
13027}
13028
13029VALUE
13030rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
13031{
13032 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
13033}
13034
13035static VALUE
13036reg_compile(struct parser_params* p, VALUE str, int options)
13037{
13038 VALUE re;
13039 VALUE err;
13040
13041 err = rb_errinfo();
13042 re = parser_reg_compile(p, str, options);
13043 if (NIL_P(re)) {
13044 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
13045 rb_set_errinfo(err);
13046 compile_error(p, "%"PRIsVALUE, m);
13047 return Qnil;
13048 }
13049 return re;
13050}
13051#else
13052static VALUE
13053parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
13054{
13055 VALUE err = rb_errinfo();
13056 VALUE re;
13057 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
13058 int c = rb_reg_fragment_setenc(p, str, options);
13059 if (c) reg_fragment_enc_error(p, str, c);
13060 re = rb_parser_reg_compile(p, str, options);
13061 if (NIL_P(re)) {
13062 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
13063 rb_set_errinfo(err);
13064 }
13065 return re;
13066}
13067#endif
13068
13069#ifndef RIPPER
13070void
13071rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
13072{
13073 struct parser_params *p;
13074 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13075 p->do_print = print;
13076 p->do_loop = loop;
13077 p->do_chomp = chomp;
13078 p->do_split = split;
13079}
13080
13081static NODE *
13082parser_append_options(struct parser_params *p, NODE *node)
13083{
13084 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
13085 const YYLTYPE *const LOC = &default_location;
13086
13087 if (p->do_print) {
13088 NODE *print = NEW_FCALL(rb_intern("print"),
13089 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
13090 LOC);
13091 node = block_append(p, node, print);
13092 }
13093
13094 if (p->do_loop) {
13095 if (p->do_split) {
13096 ID ifs = rb_intern("$;");
13097 ID fields = rb_intern("$F");
13098 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
13099 NODE *split = NEW_GASGN(fields,
13100 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13101 rb_intern("split"), args, LOC),
13102 LOC);
13103 node = block_append(p, split, node);
13104 }
13105 if (p->do_chomp) {
13106 NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13107 rb_intern("chomp!"), 0, LOC);
13108 node = block_append(p, chomp, node);
13109 }
13110
13111 node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
13112 }
13113
13114 return node;
13115}
13116
13117void
13118rb_init_parse(void)
13119{
13120 /* just to suppress unused-function warnings */
13121 (void)nodetype;
13122 (void)nodeline;
13123}
13124
13125static ID
13126internal_id(struct parser_params *p)
13127{
13128 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
13129}
13130#endif /* !RIPPER */
13131
13132static void
13133parser_initialize(struct parser_params *p)
13134{
13135 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
13136 p->command_start = TRUE;
13137 p->ruby_sourcefile_string = Qnil;
13138 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
13139 p->node_id = 0;
13140#ifdef RIPPER
13141 p->delayed.token = Qnil;
13142 p->result = Qnil;
13143 p->parsing_thread = Qnil;
13144#else
13145 p->error_buffer = Qfalse;
13146#endif
13147 p->debug_buffer = Qnil;
13148 p->debug_output = rb_ractor_stdout();
13149 p->enc = rb_utf8_encoding();
13150}
13151
13152#ifdef RIPPER
13153#define parser_mark ripper_parser_mark
13154#define parser_free ripper_parser_free
13155#endif
13156
13157static void
13158parser_mark(void *ptr)
13159{
13160 struct parser_params *p = (struct parser_params*)ptr;
13161
13162 rb_gc_mark(p->lex.input);
13163 rb_gc_mark(p->lex.prevline);
13164 rb_gc_mark(p->lex.lastline);
13165 rb_gc_mark(p->lex.nextline);
13166 rb_gc_mark(p->ruby_sourcefile_string);
13167 rb_gc_mark((VALUE)p->lex.strterm);
13168 rb_gc_mark((VALUE)p->ast);
13169 rb_gc_mark(p->case_labels);
13170#ifndef RIPPER
13171 rb_gc_mark(p->debug_lines);
13172 rb_gc_mark(p->compile_option);
13173 rb_gc_mark(p->error_buffer);
13174#else
13175 rb_gc_mark(p->delayed.token);
13176 rb_gc_mark(p->value);
13177 rb_gc_mark(p->result);
13178 rb_gc_mark(p->parsing_thread);
13179#endif
13180 rb_gc_mark(p->debug_buffer);
13181 rb_gc_mark(p->debug_output);
13182#ifdef YYMALLOC
13183 rb_gc_mark((VALUE)p->heap);
13184#endif
13185}
13186
13187static void
13188parser_free(void *ptr)
13189{
13190 struct parser_params *p = (struct parser_params*)ptr;
13191 struct local_vars *local, *prev;
13192
13193 if (p->tokenbuf) {
13194 ruby_sized_xfree(p->tokenbuf, p->toksiz);
13195 }
13196 for (local = p->lvtbl; local; local = prev) {
13197 if (local->vars) xfree(local->vars);
13198 prev = local->prev;
13199 xfree(local);
13200 }
13201 {
13202 token_info *ptinfo;
13203 while ((ptinfo = p->token_info) != 0) {
13204 p->token_info = ptinfo->next;
13205 xfree(ptinfo);
13206 }
13207 }
13208 xfree(ptr);
13209}
13210
13211static size_t
13212parser_memsize(const void *ptr)
13213{
13214 struct parser_params *p = (struct parser_params*)ptr;
13215 struct local_vars *local;
13216 size_t size = sizeof(*p);
13217
13218 size += p->toksiz;
13219 for (local = p->lvtbl; local; local = local->prev) {
13220 size += sizeof(*local);
13221 if (local->vars) size += local->vars->capa * sizeof(ID);
13222 }
13223 return size;
13224}
13225
13226static const rb_data_type_t parser_data_type = {
13227#ifndef RIPPER
13228 "parser",
13229#else
13230 "ripper",
13231#endif
13232 {
13233 parser_mark,
13234 parser_free,
13235 parser_memsize,
13236 },
13237 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
13238};
13239
13240#ifndef RIPPER
13241#undef rb_reserved_word
13242
13243const struct kwtable *
13244rb_reserved_word(const char *str, unsigned int len)
13245{
13246 return reserved_word(str, len);
13247}
13248
13249VALUE
13250rb_parser_new(void)
13251{
13252 struct parser_params *p;
13253 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
13254 &parser_data_type, p);
13255 parser_initialize(p);
13256 return parser;
13257}
13258
13259VALUE
13260rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
13261{
13262 struct parser_params *p;
13263
13264 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13265 p->error_buffer = main ? Qfalse : Qnil;
13266 p->parent_iseq = base;
13267 return vparser;
13268}
13269
13270void
13271rb_parser_keep_script_lines(VALUE vparser)
13272{
13273 struct parser_params *p;
13274
13275 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13276 p->keep_script_lines = 1;
13277}
13278#endif
13279
13280#ifdef RIPPER
13281#define rb_parser_end_seen_p ripper_parser_end_seen_p
13282#define rb_parser_encoding ripper_parser_encoding
13283#define rb_parser_get_yydebug ripper_parser_get_yydebug
13284#define rb_parser_set_yydebug ripper_parser_set_yydebug
13285#define rb_parser_get_debug_output ripper_parser_get_debug_output
13286#define rb_parser_set_debug_output ripper_parser_set_debug_output
13287static VALUE ripper_parser_end_seen_p(VALUE vparser);
13288static VALUE ripper_parser_encoding(VALUE vparser);
13289static VALUE ripper_parser_get_yydebug(VALUE self);
13290static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
13291static VALUE ripper_parser_get_debug_output(VALUE self);
13292static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
13293
13294/*
13295 * call-seq:
13296 * ripper.error? -> Boolean
13297 *
13298 * Return true if parsed source has errors.
13299 */
13300static VALUE
13301ripper_error_p(VALUE vparser)
13302{
13303 struct parser_params *p;
13304
13305 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13306 return RBOOL(p->error_p);
13307}
13308#endif
13309
13310/*
13311 * call-seq:
13312 * ripper.end_seen? -> Boolean
13313 *
13314 * Return true if parsed source ended by +\_\_END\_\_+.
13315 */
13316VALUE
13317rb_parser_end_seen_p(VALUE vparser)
13318{
13319 struct parser_params *p;
13320
13321 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13322 return RBOOL(p->ruby__end__seen);
13323}
13324
13325/*
13326 * call-seq:
13327 * ripper.encoding -> encoding
13328 *
13329 * Return encoding of the source.
13330 */
13331VALUE
13332rb_parser_encoding(VALUE vparser)
13333{
13334 struct parser_params *p;
13335
13336 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13337 return rb_enc_from_encoding(p->enc);
13338}
13339
13340#ifdef RIPPER
13341/*
13342 * call-seq:
13343 * ripper.yydebug -> true or false
13344 *
13345 * Get yydebug.
13346 */
13347VALUE
13348rb_parser_get_yydebug(VALUE self)
13349{
13350 struct parser_params *p;
13351
13352 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13353 return RBOOL(p->debug);
13354}
13355#endif
13356
13357/*
13358 * call-seq:
13359 * ripper.yydebug = flag
13360 *
13361 * Set yydebug.
13362 */
13363VALUE
13364rb_parser_set_yydebug(VALUE self, VALUE flag)
13365{
13366 struct parser_params *p;
13367
13368 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13369 p->debug = RTEST(flag);
13370 return flag;
13371}
13372
13373/*
13374 * call-seq:
13375 * ripper.debug_output -> obj
13376 *
13377 * Get debug output.
13378 */
13379VALUE
13380rb_parser_get_debug_output(VALUE self)
13381{
13382 struct parser_params *p;
13383
13384 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13385 return p->debug_output;
13386}
13387
13388/*
13389 * call-seq:
13390 * ripper.debug_output = obj
13391 *
13392 * Set debug output.
13393 */
13394VALUE
13395rb_parser_set_debug_output(VALUE self, VALUE output)
13396{
13397 struct parser_params *p;
13398
13399 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13400 return p->debug_output = output;
13401}
13402
13403#ifndef RIPPER
13404#ifdef YYMALLOC
13405#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
13406/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
13407 * potential memory leak */
13408#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
13409#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
13410 (new)->cnt = (cnt), (ptr))
13411
13412void *
13413rb_parser_malloc(struct parser_params *p, size_t size)
13414{
13415 size_t cnt = HEAPCNT(1, size);
13416 rb_imemo_tmpbuf_t *n = NEWHEAP();
13417 void *ptr = xmalloc(size);
13418
13419 return ADD2HEAP(n, cnt, ptr);
13420}
13421
13422void *
13423rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
13424{
13425 size_t cnt = HEAPCNT(nelem, size);
13426 rb_imemo_tmpbuf_t *n = NEWHEAP();
13427 void *ptr = xcalloc(nelem, size);
13428
13429 return ADD2HEAP(n, cnt, ptr);
13430}
13431
13432void *
13433rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
13434{
13435 rb_imemo_tmpbuf_t *n;
13436 size_t cnt = HEAPCNT(1, size);
13437
13438 if (ptr && (n = p->heap) != NULL) {
13439 do {
13440 if (n->ptr == ptr) {
13441 n->ptr = ptr = xrealloc(ptr, size);
13442 if (n->cnt) n->cnt = cnt;
13443 return ptr;
13444 }
13445 } while ((n = n->next) != NULL);
13446 }
13447 n = NEWHEAP();
13448 ptr = xrealloc(ptr, size);
13449 return ADD2HEAP(n, cnt, ptr);
13450}
13451
13452void
13453rb_parser_free(struct parser_params *p, void *ptr)
13454{
13455 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
13456
13457 while ((n = *prev) != NULL) {
13458 if (n->ptr == ptr) {
13459 *prev = n->next;
13460 break;
13461 }
13462 prev = &n->next;
13463 }
13464}
13465#endif
13466
13467void
13468rb_parser_printf(struct parser_params *p, const char *fmt, ...)
13469{
13470 va_list ap;
13471 VALUE mesg = p->debug_buffer;
13472
13473 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
13474 va_start(ap, fmt);
13475 rb_str_vcatf(mesg, fmt, ap);
13476 va_end(ap);
13477 if (RSTRING_END(mesg)[-1] == '\n') {
13478 rb_io_write(p->debug_output, mesg);
13479 p->debug_buffer = Qnil;
13480 }
13481}
13482
13483static void
13484parser_compile_error(struct parser_params *p, const char *fmt, ...)
13485{
13486 va_list ap;
13487
13488 rb_io_flush(p->debug_output);
13489 p->error_p = 1;
13490 va_start(ap, fmt);
13491 p->error_buffer =
13492 rb_syntax_error_append(p->error_buffer,
13493 p->ruby_sourcefile_string,
13494 p->ruby_sourceline,
13495 rb_long2int(p->lex.pcur - p->lex.pbeg),
13496 p->enc, fmt, ap);
13497 va_end(ap);
13498}
13499
13500static size_t
13501count_char(const char *str, int c)
13502{
13503 int n = 0;
13504 while (str[n] == c) ++n;
13505 return n;
13506}
13507
13508/*
13509 * strip enclosing double-quotes, same as the default yytnamerr except
13510 * for that single-quotes matching back-quotes do not stop stripping.
13511 *
13512 * "\"`class' keyword\"" => "`class' keyword"
13513 */
13514RUBY_FUNC_EXPORTED size_t
13515rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
13516{
13517 if (*yystr == '"') {
13518 size_t yyn = 0, bquote = 0;
13519 const char *yyp = yystr;
13520
13521 while (*++yyp) {
13522 switch (*yyp) {
13523 case '`':
13524 if (!bquote) {
13525 bquote = count_char(yyp+1, '`') + 1;
13526 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
13527 yyn += bquote;
13528 yyp += bquote - 1;
13529 break;
13530 }
13531 goto default_char;
13532
13533 case '\'':
13534 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
13535 if (yyres) memcpy(yyres + yyn, yyp, bquote);
13536 yyn += bquote;
13537 yyp += bquote - 1;
13538 bquote = 0;
13539 break;
13540 }
13541 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
13542 if (yyres) memcpy(yyres + yyn, yyp, 3);
13543 yyn += 3;
13544 yyp += 2;
13545 break;
13546 }
13547 goto do_not_strip_quotes;
13548
13549 case ',':
13550 goto do_not_strip_quotes;
13551
13552 case '\\':
13553 if (*++yyp != '\\')
13554 goto do_not_strip_quotes;
13555 /* Fall through. */
13556 default_char:
13557 default:
13558 if (yyres)
13559 yyres[yyn] = *yyp;
13560 yyn++;
13561 break;
13562
13563 case '"':
13564 case '\0':
13565 if (yyres)
13566 yyres[yyn] = '\0';
13567 return yyn;
13568 }
13569 }
13570 do_not_strip_quotes: ;
13571 }
13572
13573 if (!yyres) return strlen(yystr);
13574
13575 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
13576}
13577#endif
13578
13579#ifdef RIPPER
13580#ifdef RIPPER_DEBUG
13581/* :nodoc: */
13582static VALUE
13583ripper_validate_object(VALUE self, VALUE x)
13584{
13585 if (x == Qfalse) return x;
13586 if (x == Qtrue) return x;
13587 if (x == Qnil) return x;
13588 if (x == Qundef)
13589 rb_raise(rb_eArgError, "Qundef given");
13590 if (FIXNUM_P(x)) return x;
13591 if (SYMBOL_P(x)) return x;
13592 switch (BUILTIN_TYPE(x)) {
13593 case T_STRING:
13594 case T_OBJECT:
13595 case T_ARRAY:
13596 case T_BIGNUM:
13597 case T_FLOAT:
13598 case T_COMPLEX:
13599 case T_RATIONAL:
13600 break;
13601 case T_NODE:
13602 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
13603 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
13604 }
13605 x = ((NODE *)x)->nd_rval;
13606 break;
13607 default:
13608 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
13609 (void *)x, rb_obj_classname(x));
13610 }
13611 if (!RBASIC_CLASS(x)) {
13612 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
13613 (void *)x, rb_builtin_type_name(TYPE(x)));
13614 }
13615 return x;
13616}
13617#endif
13618
13619#define validate(x) ((x) = get_value(x))
13620
13621static VALUE
13622ripper_dispatch0(struct parser_params *p, ID mid)
13623{
13624 return rb_funcall(p->value, mid, 0);
13625}
13626
13627static VALUE
13628ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
13629{
13630 validate(a);
13631 return rb_funcall(p->value, mid, 1, a);
13632}
13633
13634static VALUE
13635ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
13636{
13637 validate(a);
13638 validate(b);
13639 return rb_funcall(p->value, mid, 2, a, b);
13640}
13641
13642static VALUE
13643ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
13644{
13645 validate(a);
13646 validate(b);
13647 validate(c);
13648 return rb_funcall(p->value, mid, 3, a, b, c);
13649}
13650
13651static VALUE
13652ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
13653{
13654 validate(a);
13655 validate(b);
13656 validate(c);
13657 validate(d);
13658 return rb_funcall(p->value, mid, 4, a, b, c, d);
13659}
13660
13661static VALUE
13662ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
13663{
13664 validate(a);
13665 validate(b);
13666 validate(c);
13667 validate(d);
13668 validate(e);
13669 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
13670}
13671
13672static VALUE
13673ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
13674{
13675 validate(a);
13676 validate(b);
13677 validate(c);
13678 validate(d);
13679 validate(e);
13680 validate(f);
13681 validate(g);
13682 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
13683}
13684
13685static ID
13686ripper_get_id(VALUE v)
13687{
13688 NODE *nd;
13689 if (!RB_TYPE_P(v, T_NODE)) return 0;
13690 nd = (NODE *)v;
13691 if (!nd_type_p(nd, NODE_RIPPER)) return 0;
13692 return nd->nd_vid;
13693}
13694
13695static VALUE
13696ripper_get_value(VALUE v)
13697{
13698 NODE *nd;
13699 if (v == Qundef) return Qnil;
13700 if (!RB_TYPE_P(v, T_NODE)) return v;
13701 nd = (NODE *)v;
13702 if (!nd_type_p(nd, NODE_RIPPER)) return Qnil;
13703 return nd->nd_rval;
13704}
13705
13706static void
13707ripper_error(struct parser_params *p)
13708{
13709 p->error_p = TRUE;
13710}
13711
13712static void
13713ripper_compile_error(struct parser_params *p, const char *fmt, ...)
13714{
13715 VALUE str;
13716 va_list args;
13717
13718 va_start(args, fmt);
13719 str = rb_vsprintf(fmt, args);
13720 va_end(args);
13721 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
13722 ripper_error(p);
13723}
13724
13725static VALUE
13726ripper_lex_get_generic(struct parser_params *p, VALUE src)
13727{
13728 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
13729 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
13730 rb_raise(rb_eTypeError,
13731 "gets returned %"PRIsVALUE" (expected String or nil)",
13732 rb_obj_class(line));
13733 }
13734 return line;
13735}
13736
13737static VALUE
13738ripper_lex_io_get(struct parser_params *p, VALUE src)
13739{
13740 return rb_io_gets(src);
13741}
13742
13743static VALUE
13744ripper_s_allocate(VALUE klass)
13745{
13746 struct parser_params *p;
13747 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
13748 &parser_data_type, p);
13749 p->value = self;
13750 return self;
13751}
13752
13753#define ripper_initialized_p(r) ((r)->lex.input != 0)
13754
13755/*
13756 * call-seq:
13757 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
13758 *
13759 * Create a new Ripper object.
13760 * _src_ must be a String, an IO, or an Object which has #gets method.
13761 *
13762 * This method does not starts parsing.
13763 * See also Ripper#parse and Ripper.parse.
13764 */
13765static VALUE
13766ripper_initialize(int argc, VALUE *argv, VALUE self)
13767{
13768 struct parser_params *p;
13769 VALUE src, fname, lineno;
13770
13771 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13772 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
13773 if (RB_TYPE_P(src, T_FILE)) {
13774 p->lex.gets = ripper_lex_io_get;
13775 }
13776 else if (rb_respond_to(src, id_gets)) {
13777 p->lex.gets = ripper_lex_get_generic;
13778 }
13779 else {
13780 StringValue(src);
13781 p->lex.gets = lex_get_str;
13782 }
13783 p->lex.input = src;
13784 p->eofp = 0;
13785 if (NIL_P(fname)) {
13786 fname = STR_NEW2("(ripper)");
13787 OBJ_FREEZE(fname);
13788 }
13789 else {
13790 StringValueCStr(fname);
13791 fname = rb_str_new_frozen(fname);
13792 }
13793 parser_initialize(p);
13794
13795 p->ruby_sourcefile_string = fname;
13796 p->ruby_sourcefile = RSTRING_PTR(fname);
13797 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
13798
13799 return Qnil;
13800}
13801
13802static VALUE
13803ripper_parse0(VALUE parser_v)
13804{
13805 struct parser_params *p;
13806
13807 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13808 parser_prepare(p);
13809 p->ast = rb_ast_new();
13810 ripper_yyparse((void*)p);
13811 rb_ast_dispose(p->ast);
13812 p->ast = 0;
13813 return p->result;
13814}
13815
13816static VALUE
13817ripper_ensure(VALUE parser_v)
13818{
13819 struct parser_params *p;
13820
13821 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13822 p->parsing_thread = Qnil;
13823 return Qnil;
13824}
13825
13826/*
13827 * call-seq:
13828 * ripper.parse
13829 *
13830 * Start parsing and returns the value of the root action.
13831 */
13832static VALUE
13833ripper_parse(VALUE self)
13834{
13835 struct parser_params *p;
13836
13837 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13838 if (!ripper_initialized_p(p)) {
13839 rb_raise(rb_eArgError, "method called for uninitialized object");
13840 }
13841 if (!NIL_P(p->parsing_thread)) {
13842 if (p->parsing_thread == rb_thread_current())
13843 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13844 else
13845 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13846 }
13847 p->parsing_thread = rb_thread_current();
13848 rb_ensure(ripper_parse0, self, ripper_ensure, self);
13849
13850 return p->result;
13851}
13852
13853/*
13854 * call-seq:
13855 * ripper.column -> Integer
13856 *
13857 * Return column number of current parsing line.
13858 * This number starts from 0.
13859 */
13860static VALUE
13861ripper_column(VALUE self)
13862{
13863 struct parser_params *p;
13864 long col;
13865
13866 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13867 if (!ripper_initialized_p(p)) {
13868 rb_raise(rb_eArgError, "method called for uninitialized object");
13869 }
13870 if (NIL_P(p->parsing_thread)) return Qnil;
13871 col = p->lex.ptok - p->lex.pbeg;
13872 return LONG2NUM(col);
13873}
13874
13875/*
13876 * call-seq:
13877 * ripper.filename -> String
13878 *
13879 * Return current parsing filename.
13880 */
13881static VALUE
13882ripper_filename(VALUE self)
13883{
13884 struct parser_params *p;
13885
13886 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13887 if (!ripper_initialized_p(p)) {
13888 rb_raise(rb_eArgError, "method called for uninitialized object");
13889 }
13890 return p->ruby_sourcefile_string;
13891}
13892
13893/*
13894 * call-seq:
13895 * ripper.lineno -> Integer
13896 *
13897 * Return line number of current parsing line.
13898 * This number starts from 1.
13899 */
13900static VALUE
13901ripper_lineno(VALUE self)
13902{
13903 struct parser_params *p;
13904
13905 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13906 if (!ripper_initialized_p(p)) {
13907 rb_raise(rb_eArgError, "method called for uninitialized object");
13908 }
13909 if (NIL_P(p->parsing_thread)) return Qnil;
13910 return INT2NUM(p->ruby_sourceline);
13911}
13912
13913/*
13914 * call-seq:
13915 * ripper.state -> Integer
13916 *
13917 * Return scanner state of current token.
13918 */
13919static VALUE
13920ripper_state(VALUE self)
13921{
13922 struct parser_params *p;
13923
13924 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13925 if (!ripper_initialized_p(p)) {
13926 rb_raise(rb_eArgError, "method called for uninitialized object");
13927 }
13928 if (NIL_P(p->parsing_thread)) return Qnil;
13929 return INT2NUM(p->lex.state);
13930}
13931
13932/*
13933 * call-seq:
13934 * ripper.token -> String
13935 *
13936 * Return the current token string.
13937 */
13938static VALUE
13939ripper_token(VALUE self)
13940{
13941 struct parser_params *p;
13942 long pos, len;
13943
13944 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13945 if (!ripper_initialized_p(p)) {
13946 rb_raise(rb_eArgError, "method called for uninitialized object");
13947 }
13948 if (NIL_P(p->parsing_thread)) return Qnil;
13949 pos = p->lex.ptok - p->lex.pbeg;
13950 len = p->lex.pcur - p->lex.ptok;
13951 return rb_str_subseq(p->lex.lastline, pos, len);
13952}
13953
13954#ifdef RIPPER_DEBUG
13955/* :nodoc: */
13956static VALUE
13957ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13958{
13959 StringValue(msg);
13960 if (obj == Qundef) {
13961 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13962 }
13963 return Qnil;
13964}
13965
13966/* :nodoc: */
13967static VALUE
13968ripper_value(VALUE self, VALUE obj)
13969{
13970 return ULONG2NUM(obj);
13971}
13972#endif
13973
13974/*
13975 * call-seq:
13976 * Ripper.lex_state_name(integer) -> string
13977 *
13978 * Returns a string representation of lex_state.
13979 */
13980static VALUE
13981ripper_lex_state_name(VALUE self, VALUE state)
13982{
13983 return rb_parser_lex_state_name(NUM2INT(state));
13984}
13985
13986void
13987Init_ripper(void)
13988{
13989 ripper_init_eventids1();
13990 ripper_init_eventids2();
13991 id_warn = rb_intern_const("warn");
13992 id_warning = rb_intern_const("warning");
13993 id_gets = rb_intern_const("gets");
13994 id_assoc = rb_intern_const("=>");
13995
13996 (void)yystpcpy; /* may not used in newer bison */
13997
13998 InitVM(ripper);
13999}
14000
14001void
14002InitVM_ripper(void)
14003{
14004 VALUE Ripper;
14005
14006 Ripper = rb_define_class("Ripper", rb_cObject);
14007 /* version of Ripper */
14008 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
14009 rb_define_alloc_func(Ripper, ripper_s_allocate);
14010 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
14011 rb_define_method(Ripper, "parse", ripper_parse, 0);
14012 rb_define_method(Ripper, "column", ripper_column, 0);
14013 rb_define_method(Ripper, "filename", ripper_filename, 0);
14014 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
14015 rb_define_method(Ripper, "state", ripper_state, 0);
14016 rb_define_method(Ripper, "token", ripper_token, 0);
14017 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
14018 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
14019 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
14020 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
14021 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
14022 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
14023 rb_define_method(Ripper, "error?", ripper_error_p, 0);
14024#ifdef RIPPER_DEBUG
14025 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
14026 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
14027 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
14028#endif
14029
14030 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
14031 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
14032
14033 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
14034
14035<% @exprs.each do |expr, desc| -%>
14036 /* <%=desc%> */
14037 rb_define_const(Ripper, "<%=expr%>", INT2NUM(<%=expr%>));
14038<% end %>
14039 ripper_init_eventids1_table(Ripper);
14040 ripper_init_eventids2_table(Ripper);
14041
14042# if 0
14043 /* Hack to let RDoc document SCRIPT_LINES__ */
14044
14045 /*
14046 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
14047 * after the assignment will be added as an Array of lines with the file
14048 * name as the key.
14049 */
14050 rb_define_global_const("SCRIPT_LINES__", Qnil);
14051#endif
14052
14053}
14054#endif /* RIPPER */
14055
14056/*
14057 * Local variables:
14058 * mode: c
14059 * c-file-style: "ruby"
14060 * End:
14061 */