Ruby 3.1.4p223 (2023-03-30 revision HEAD)
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 p->line_count++;
6458 return line;
6459}
6460
6461static const rb_data_type_t parser_data_type;
6462
6463#ifndef RIPPER
6464static rb_ast_t*
6465parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6466{
6467 struct parser_params *p;
6468
6469 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6470
6471 p->lex.gets = lex_get_str;
6472 p->lex.gets_.ptr = 0;
6473 p->lex.input = rb_str_new_frozen(s);
6474 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6475
6476 return yycompile(vparser, p, fname, line);
6477}
6478
6479rb_ast_t*
6480rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6481{
6482 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6483}
6484
6485rb_ast_t*
6486rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6487{
6488 must_be_ascii_compatible(s);
6489 return parser_compile_string(vparser, f, s, line);
6490}
6491
6492VALUE rb_io_gets_internal(VALUE io);
6493
6494static VALUE
6495lex_io_gets(struct parser_params *p, VALUE io)
6496{
6497 return rb_io_gets_internal(io);
6498}
6499
6500rb_ast_t*
6501rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6502{
6503 struct parser_params *p;
6504
6505 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6506
6507 p->lex.gets = lex_io_gets;
6508 p->lex.input = file;
6509 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6510
6511 return yycompile(vparser, p, fname, start);
6512}
6513
6514static VALUE
6515lex_generic_gets(struct parser_params *p, VALUE input)
6516{
6517 return (*p->lex.gets_.call)(input, p->line_count);
6518}
6519
6520rb_ast_t*
6521rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6522{
6523 struct parser_params *p;
6524
6525 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6526
6527 p->lex.gets = lex_generic_gets;
6528 p->lex.gets_.call = lex_gets;
6529 p->lex.input = input;
6530 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6531
6532 return yycompile(vparser, p, fname, start);
6533}
6534#endif /* !RIPPER */
6535
6536#define STR_FUNC_ESCAPE 0x01
6537#define STR_FUNC_EXPAND 0x02
6538#define STR_FUNC_REGEXP 0x04
6539#define STR_FUNC_QWORDS 0x08
6540#define STR_FUNC_SYMBOL 0x10
6541#define STR_FUNC_INDENT 0x20
6542#define STR_FUNC_LABEL 0x40
6543#define STR_FUNC_LIST 0x4000
6544#define STR_FUNC_TERM 0x8000
6545
6546enum string_type {
6547 str_label = STR_FUNC_LABEL,
6548 str_squote = (0),
6549 str_dquote = (STR_FUNC_EXPAND),
6550 str_xquote = (STR_FUNC_EXPAND),
6551 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6552 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6553 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6554 str_ssym = (STR_FUNC_SYMBOL),
6555 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6556};
6557
6558static VALUE
6559parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6560{
6561 VALUE str;
6562
6563 str = rb_enc_str_new(ptr, len, enc);
6564 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6565 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6566 }
6567 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6568 rb_enc_associate(str, rb_ascii8bit_encoding());
6569 }
6570 }
6571
6572 return str;
6573}
6574
6575#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6576#define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6577#define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6578#define peek(p,c) peek_n(p, (c), 0)
6579#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6580#define peekc(p) peekc_n(p, 0)
6581#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6582
6583#ifdef RIPPER
6584static void
6585add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6586{
6587 if (tok < end) {
6588 if (!has_delayed_token(p)) {
6589 p->delayed.token = rb_str_buf_new(end - tok);
6590 rb_enc_associate(p->delayed.token, p->enc);
6591 p->delayed.line = p->ruby_sourceline;
6592 p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6593 }
6594 rb_str_buf_cat(p->delayed.token, tok, end - tok);
6595 p->lex.ptok = end;
6596 }
6597}
6598#else
6599#define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6600#endif
6601
6602static int
6603nextline(struct parser_params *p, int set_encoding)
6604{
6605 VALUE v = p->lex.nextline;
6606 p->lex.nextline = 0;
6607 if (!v) {
6608 if (p->eofp)
6609 return -1;
6610
6611 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6612 goto end_of_input;
6613 }
6614
6615 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6616 end_of_input:
6617 p->eofp = 1;
6618 lex_goto_eol(p);
6619 return -1;
6620 }
6621#ifndef RIPPER
6622 if (p->debug_lines) {
6623 if (set_encoding) rb_enc_associate(v, p->enc);
6624 rb_ary_push(p->debug_lines, v);
6625 }
6626#endif
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
6658nextc0(struct parser_params *p, int set_encoding)
6659{
6660 int c;
6661
6662 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6663 if (nextline(p, set_encoding)) 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#define nextc(p) nextc0(p, TRUE)
6673
6674static void
6675pushback(struct parser_params *p, int c)
6676{
6677 if (c == -1) return;
6678 p->lex.pcur--;
6679 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6680 p->lex.pcur--;
6681 }
6682}
6683
6684#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6685
6686#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6687#define tok(p) (p)->tokenbuf
6688#define toklen(p) (p)->tokidx
6689
6690static int
6691looking_at_eol_p(struct parser_params *p)
6692{
6693 const char *ptr = p->lex.pcur;
6694 while (ptr < p->lex.pend) {
6695 int c = (unsigned char)*ptr++;
6696 int eol = (c == '\n' || c == '#');
6697 if (eol || !ISSPACE(c)) {
6698 return eol;
6699 }
6700 }
6701 return TRUE;
6702}
6703
6704static char*
6705newtok(struct parser_params *p)
6706{
6707 p->tokidx = 0;
6708 p->tokline = p->ruby_sourceline;
6709 if (!p->tokenbuf) {
6710 p->toksiz = 60;
6711 p->tokenbuf = ALLOC_N(char, 60);
6712 }
6713 if (p->toksiz > 4096) {
6714 p->toksiz = 60;
6715 REALLOC_N(p->tokenbuf, char, 60);
6716 }
6717 return p->tokenbuf;
6718}
6719
6720static char *
6721tokspace(struct parser_params *p, int n)
6722{
6723 p->tokidx += n;
6724
6725 if (p->tokidx >= p->toksiz) {
6726 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6727 REALLOC_N(p->tokenbuf, char, p->toksiz);
6728 }
6729 return &p->tokenbuf[p->tokidx-n];
6730}
6731
6732static void
6733tokadd(struct parser_params *p, int c)
6734{
6735 p->tokenbuf[p->tokidx++] = (char)c;
6736 if (p->tokidx >= p->toksiz) {
6737 p->toksiz *= 2;
6738 REALLOC_N(p->tokenbuf, char, p->toksiz);
6739 }
6740}
6741
6742static int
6743tok_hex(struct parser_params *p, size_t *numlen)
6744{
6745 int c;
6746
6747 c = scan_hex(p->lex.pcur, 2, numlen);
6748 if (!*numlen) {
6749 yyerror0("invalid hex escape");
6750 token_flush(p);
6751 return 0;
6752 }
6753 p->lex.pcur += *numlen;
6754 return c;
6755}
6756
6757#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6758
6759static int
6760escaped_control_code(int c)
6761{
6762 int c2 = 0;
6763 switch (c) {
6764 case ' ':
6765 c2 = 's';
6766 break;
6767 case '\n':
6768 c2 = 'n';
6769 break;
6770 case '\t':
6771 c2 = 't';
6772 break;
6773 case '\v':
6774 c2 = 'v';
6775 break;
6776 case '\r':
6777 c2 = 'r';
6778 break;
6779 case '\f':
6780 c2 = 'f';
6781 break;
6782 }
6783 return c2;
6784}
6785
6786#define WARN_SPACE_CHAR(c, prefix) \
6787 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6788
6789static int
6790tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6791 int regexp_literal, int wide)
6792{
6793 size_t numlen;
6794 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6795 literal_flush(p, p->lex.pcur);
6796 p->lex.pcur += numlen;
6797 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6798 yyerror0("invalid Unicode escape");
6799 return wide && numlen > 0;
6800 }
6801 if (codepoint > 0x10ffff) {
6802 yyerror0("invalid Unicode codepoint (too large)");
6803 return wide;
6804 }
6805 if ((codepoint & 0xfffff800) == 0xd800) {
6806 yyerror0("invalid Unicode codepoint");
6807 return wide;
6808 }
6809 if (regexp_literal) {
6810 tokcopy(p, (int)numlen);
6811 }
6812 else if (codepoint >= 0x80) {
6813 rb_encoding *utf8 = rb_utf8_encoding();
6814 if (*encp && utf8 != *encp) {
6815 YYLTYPE loc = RUBY_INIT_YYLLOC();
6816 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6817 parser_show_error_line(p, &loc);
6818 return wide;
6819 }
6820 *encp = utf8;
6821 tokaddmbc(p, codepoint, *encp);
6822 }
6823 else {
6824 tokadd(p, codepoint);
6825 }
6826 return TRUE;
6827}
6828
6829/* return value is for ?\u3042 */
6830static void
6831tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6832 int term, int symbol_literal, int regexp_literal)
6833{
6834 /*
6835 * If `term` is not -1, then we allow multiple codepoints in \u{}
6836 * upto `term` byte, otherwise we're parsing a character literal.
6837 * And then add the codepoints to the current token.
6838 */
6839 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6840
6841 const int open_brace = '{', close_brace = '}';
6842
6843 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6844
6845 if (peek(p, open_brace)) { /* handle \u{...} form */
6846 const char *second = NULL;
6847 int c, last = nextc(p);
6848 if (p->lex.pcur >= p->lex.pend) goto unterminated;
6849 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6850 while (c != close_brace) {
6851 if (c == term) goto unterminated;
6852 if (second == multiple_codepoints)
6853 second = p->lex.pcur;
6854 if (regexp_literal) tokadd(p, last);
6855 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6856 break;
6857 }
6858 while (ISSPACE(c = *p->lex.pcur)) {
6859 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6860 last = c;
6861 }
6862 if (term == -1 && !second)
6863 second = multiple_codepoints;
6864 }
6865
6866 if (c != close_brace) {
6867 unterminated:
6868 token_flush(p);
6869 yyerror0("unterminated Unicode escape");
6870 return;
6871 }
6872 if (second && second != multiple_codepoints) {
6873 const char *pcur = p->lex.pcur;
6874 p->lex.pcur = second;
6875 dispatch_scan_event(p, tSTRING_CONTENT);
6876 token_flush(p);
6877 p->lex.pcur = pcur;
6878 yyerror0(multiple_codepoints);
6879 token_flush(p);
6880 }
6881
6882 if (regexp_literal) tokadd(p, close_brace);
6883 nextc(p);
6884 }
6885 else { /* handle \uxxxx form */
6886 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6887 token_flush(p);
6888 return;
6889 }
6890 }
6891}
6892
6893#define ESCAPE_CONTROL 1
6894#define ESCAPE_META 2
6895
6896static int
6897read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6898{
6899 int c;
6900 size_t numlen;
6901
6902 switch (c = nextc(p)) {
6903 case '\\': /* Backslash */
6904 return c;
6905
6906 case 'n': /* newline */
6907 return '\n';
6908
6909 case 't': /* horizontal tab */
6910 return '\t';
6911
6912 case 'r': /* carriage-return */
6913 return '\r';
6914
6915 case 'f': /* form-feed */
6916 return '\f';
6917
6918 case 'v': /* vertical tab */
6919 return '\13';
6920
6921 case 'a': /* alarm(bell) */
6922 return '\007';
6923
6924 case 'e': /* escape */
6925 return 033;
6926
6927 case '0': case '1': case '2': case '3': /* octal constant */
6928 case '4': case '5': case '6': case '7':
6929 pushback(p, c);
6930 c = scan_oct(p->lex.pcur, 3, &numlen);
6931 p->lex.pcur += numlen;
6932 return c;
6933
6934 case 'x': /* hex constant */
6935 c = tok_hex(p, &numlen);
6936 if (numlen == 0) return 0;
6937 return c;
6938
6939 case 'b': /* backspace */
6940 return '\010';
6941
6942 case 's': /* space */
6943 return ' ';
6944
6945 case 'M':
6946 if (flags & ESCAPE_META) goto eof;
6947 if ((c = nextc(p)) != '-') {
6948 goto eof;
6949 }
6950 if ((c = nextc(p)) == '\\') {
6951 switch (peekc(p)) {
6952 case 'u': case 'U':
6953 nextc(p);
6954 goto eof;
6955 }
6956 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6957 }
6958 else if (c == -1 || !ISASCII(c)) goto eof;
6959 else {
6960 int c2 = escaped_control_code(c);
6961 if (c2) {
6962 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6963 WARN_SPACE_CHAR(c2, "\\M-");
6964 }
6965 else {
6966 WARN_SPACE_CHAR(c2, "\\C-\\M-");
6967 }
6968 }
6969 else if (ISCNTRL(c)) goto eof;
6970 return ((c & 0xff) | 0x80);
6971 }
6972
6973 case 'C':
6974 if ((c = nextc(p)) != '-') {
6975 goto eof;
6976 }
6977 case 'c':
6978 if (flags & ESCAPE_CONTROL) goto eof;
6979 if ((c = nextc(p))== '\\') {
6980 switch (peekc(p)) {
6981 case 'u': case 'U':
6982 nextc(p);
6983 goto eof;
6984 }
6985 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6986 }
6987 else if (c == '?')
6988 return 0177;
6989 else if (c == -1 || !ISASCII(c)) goto eof;
6990 else {
6991 int c2 = escaped_control_code(c);
6992 if (c2) {
6993 if (ISCNTRL(c)) {
6994 if (flags & ESCAPE_META) {
6995 WARN_SPACE_CHAR(c2, "\\M-");
6996 }
6997 else {
6998 WARN_SPACE_CHAR(c2, "");
6999 }
7000 }
7001 else {
7002 if (flags & ESCAPE_META) {
7003 WARN_SPACE_CHAR(c2, "\\M-\\C-");
7004 }
7005 else {
7006 WARN_SPACE_CHAR(c2, "\\C-");
7007 }
7008 }
7009 }
7010 else if (ISCNTRL(c)) goto eof;
7011 }
7012 return c & 0x9f;
7013
7014 eof:
7015 case -1:
7016 yyerror0("Invalid escape character syntax");
7017 token_flush(p);
7018 return '\0';
7019
7020 default:
7021 return c;
7022 }
7023}
7024
7025static void
7026tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
7027{
7028 int len = rb_enc_codelen(c, enc);
7029 rb_enc_mbcput(c, tokspace(p, len), enc);
7030}
7031
7032static int
7033tokadd_escape(struct parser_params *p, rb_encoding **encp)
7034{
7035 int c;
7036 size_t numlen;
7037
7038 switch (c = nextc(p)) {
7039 case '\n':
7040 return 0; /* just ignore */
7041
7042 case '0': case '1': case '2': case '3': /* octal constant */
7043 case '4': case '5': case '6': case '7':
7044 {
7045 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
7046 if (numlen == 0) goto eof;
7047 p->lex.pcur += numlen;
7048 tokcopy(p, (int)numlen + 1);
7049 }
7050 return 0;
7051
7052 case 'x': /* hex constant */
7053 {
7054 tok_hex(p, &numlen);
7055 if (numlen == 0) return -1;
7056 tokcopy(p, (int)numlen + 2);
7057 }
7058 return 0;
7059
7060 eof:
7061 case -1:
7062 yyerror0("Invalid escape character syntax");
7063 token_flush(p);
7064 return -1;
7065
7066 default:
7067 tokadd(p, '\\');
7068 tokadd(p, c);
7069 }
7070 return 0;
7071}
7072
7073static int
7074regx_options(struct parser_params *p)
7075{
7076 int kcode = 0;
7077 int kopt = 0;
7078 int options = 0;
7079 int c, opt, kc;
7080
7081 newtok(p);
7082 while (c = nextc(p), ISALPHA(c)) {
7083 if (c == 'o') {
7084 options |= RE_OPTION_ONCE;
7085 }
7086 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
7087 if (kc >= 0) {
7088 if (kc != rb_ascii8bit_encindex()) kcode = c;
7089 kopt = opt;
7090 }
7091 else {
7092 options |= opt;
7093 }
7094 }
7095 else {
7096 tokadd(p, c);
7097 }
7098 }
7099 options |= kopt;
7100 pushback(p, c);
7101 if (toklen(p)) {
7102 YYLTYPE loc = RUBY_INIT_YYLLOC();
7103 tokfix(p);
7104 compile_error(p, "unknown regexp option%s - %*s",
7105 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
7106 parser_show_error_line(p, &loc);
7107 }
7108 return options | RE_OPTION_ENCODING(kcode);
7109}
7110
7111static int
7112tokadd_mbchar(struct parser_params *p, int c)
7113{
7114 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7115 if (len < 0) return -1;
7116 tokadd(p, c);
7117 p->lex.pcur += --len;
7118 if (len > 0) tokcopy(p, len);
7119 return c;
7120}
7121
7122static inline int
7123simple_re_meta(int c)
7124{
7125 switch (c) {
7126 case '$': case '*': case '+': case '.':
7127 case '?': case '^': case '|':
7128 case ')': case ']': case '}': case '>':
7129 return TRUE;
7130 default:
7131 return FALSE;
7132 }
7133}
7134
7135static int
7136parser_update_heredoc_indent(struct parser_params *p, int c)
7137{
7138 if (p->heredoc_line_indent == -1) {
7139 if (c == '\n') p->heredoc_line_indent = 0;
7140 }
7141 else {
7142 if (c == ' ') {
7143 p->heredoc_line_indent++;
7144 return TRUE;
7145 }
7146 else if (c == '\t') {
7147 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
7148 p->heredoc_line_indent = w * TAB_WIDTH;
7149 return TRUE;
7150 }
7151 else if (c != '\n') {
7152 if (p->heredoc_indent > p->heredoc_line_indent) {
7153 p->heredoc_indent = p->heredoc_line_indent;
7154 }
7155 p->heredoc_line_indent = -1;
7156 }
7157 }
7158 return FALSE;
7159}
7160
7161static void
7162parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
7163{
7164 YYLTYPE loc = RUBY_INIT_YYLLOC();
7165 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
7166 compile_error(p, "%s mixed within %s source", n1, n2);
7167 parser_show_error_line(p, &loc);
7168}
7169
7170static void
7171parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
7172{
7173 const char *pos = p->lex.pcur;
7174 p->lex.pcur = beg;
7175 parser_mixed_error(p, enc1, enc2);
7176 p->lex.pcur = pos;
7177}
7178
7179static int
7180tokadd_string(struct parser_params *p,
7181 int func, int term, int paren, long *nest,
7182 rb_encoding **encp, rb_encoding **enc)
7183{
7184 int c;
7185 bool erred = false;
7186
7187#define mixed_error(enc1, enc2) \
7188 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
7189#define mixed_escape(beg, enc1, enc2) \
7190 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
7191
7192 while ((c = nextc(p)) != -1) {
7193 if (p->heredoc_indent > 0) {
7194 parser_update_heredoc_indent(p, c);
7195 }
7196
7197 if (paren && c == paren) {
7198 ++*nest;
7199 }
7200 else if (c == term) {
7201 if (!nest || !*nest) {
7202 pushback(p, c);
7203 break;
7204 }
7205 --*nest;
7206 }
7207 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
7208 int c2 = *p->lex.pcur;
7209 if (c2 == '$' || c2 == '@' || c2 == '{') {
7210 pushback(p, c);
7211 break;
7212 }
7213 }
7214 else if (c == '\\') {
7215 literal_flush(p, p->lex.pcur - 1);
7216 c = nextc(p);
7217 switch (c) {
7218 case '\n':
7219 if (func & STR_FUNC_QWORDS) break;
7220 if (func & STR_FUNC_EXPAND) {
7221 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
7222 continue;
7223 if (c == term) {
7224 c = '\\';
7225 goto terminate;
7226 }
7227 }
7228 tokadd(p, '\\');
7229 break;
7230
7231 case '\\':
7232 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
7233 break;
7234
7235 case 'u':
7236 if ((func & STR_FUNC_EXPAND) == 0) {
7237 tokadd(p, '\\');
7238 break;
7239 }
7240 tokadd_utf8(p, enc, term,
7241 func & STR_FUNC_SYMBOL,
7242 func & STR_FUNC_REGEXP);
7243 continue;
7244
7245 default:
7246 if (c == -1) return -1;
7247 if (!ISASCII(c)) {
7248 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
7249 goto non_ascii;
7250 }
7251 if (func & STR_FUNC_REGEXP) {
7252 switch (c) {
7253 case 'c':
7254 case 'C':
7255 case 'M': {
7256 pushback(p, c);
7257 c = read_escape(p, 0, enc);
7258
7259 int i;
7260 char escbuf[5];
7261 snprintf(escbuf, sizeof(escbuf), "\\x%02X", c);
7262 for (i = 0; i < 4; i++) {
7263 tokadd(p, escbuf[i]);
7264 }
7265 continue;
7266 }
7267 }
7268
7269 if (c == term && !simple_re_meta(c)) {
7270 tokadd(p, c);
7271 continue;
7272 }
7273 pushback(p, c);
7274 if ((c = tokadd_escape(p, enc)) < 0)
7275 return -1;
7276 if (*enc && *enc != *encp) {
7277 mixed_escape(p->lex.ptok+2, *enc, *encp);
7278 }
7279 continue;
7280 }
7281 else if (func & STR_FUNC_EXPAND) {
7282 pushback(p, c);
7283 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
7284 c = read_escape(p, 0, enc);
7285 }
7286 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7287 /* ignore backslashed spaces in %w */
7288 }
7289 else if (c != term && !(paren && c == paren)) {
7290 tokadd(p, '\\');
7291 pushback(p, c);
7292 continue;
7293 }
7294 }
7295 }
7296 else if (!parser_isascii(p)) {
7297 non_ascii:
7298 if (!*enc) {
7299 *enc = *encp;
7300 }
7301 else if (*enc != *encp) {
7302 mixed_error(*enc, *encp);
7303 continue;
7304 }
7305 if (tokadd_mbchar(p, c) == -1) return -1;
7306 continue;
7307 }
7308 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7309 pushback(p, c);
7310 break;
7311 }
7312 if (c & 0x80) {
7313 if (!*enc) {
7314 *enc = *encp;
7315 }
7316 else if (*enc != *encp) {
7317 mixed_error(*enc, *encp);
7318 continue;
7319 }
7320 }
7321 tokadd(p, c);
7322 }
7323 terminate:
7324 if (*enc) *encp = *enc;
7325 return c;
7326}
7327
7328static inline rb_strterm_t *
7329new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
7330{
7331 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
7332}
7333
7334/* imemo_parser_strterm for literal */
7335#define NEW_STRTERM(func, term, paren) \
7336 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
7337
7338#ifdef RIPPER
7339static void
7340flush_string_content(struct parser_params *p, rb_encoding *enc)
7341{
7342 VALUE content = yylval.val;
7343 if (!ripper_is_node_yylval(content))
7344 content = ripper_new_yylval(p, 0, 0, content);
7345 if (has_delayed_token(p)) {
7346 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7347 if (len > 0) {
7348 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7349 }
7350 dispatch_delayed_token(p, tSTRING_CONTENT);
7351 p->lex.ptok = p->lex.pcur;
7352 RNODE(content)->nd_rval = yylval.val;
7353 }
7354 dispatch_scan_event(p, tSTRING_CONTENT);
7355 if (yylval.val != content)
7356 RNODE(content)->nd_rval = yylval.val;
7357 yylval.val = content;
7358}
7359#else
7360#define flush_string_content(p, enc) ((void)(enc))
7361#endif
7362
7363RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7364/* this can be shared with ripper, since it's independent from struct
7365 * parser_params. */
7366#ifndef RIPPER
7367#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7368#define SPECIAL_PUNCT(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('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7374 BIT('0', idx))
7375const unsigned int ruby_global_name_punct_bits[] = {
7376 SPECIAL_PUNCT(0),
7377 SPECIAL_PUNCT(1),
7378 SPECIAL_PUNCT(2),
7379};
7380#undef BIT
7381#undef SPECIAL_PUNCT
7382#endif
7383
7384static enum yytokentype
7385parser_peek_variable_name(struct parser_params *p)
7386{
7387 int c;
7388 const char *ptr = p->lex.pcur;
7389
7390 if (ptr + 1 >= p->lex.pend) return 0;
7391 c = *ptr++;
7392 switch (c) {
7393 case '$':
7394 if ((c = *ptr) == '-') {
7395 if (++ptr >= p->lex.pend) return 0;
7396 c = *ptr;
7397 }
7398 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7399 return tSTRING_DVAR;
7400 }
7401 break;
7402 case '@':
7403 if ((c = *ptr) == '@') {
7404 if (++ptr >= p->lex.pend) return 0;
7405 c = *ptr;
7406 }
7407 break;
7408 case '{':
7409 p->lex.pcur = ptr;
7410 p->command_start = TRUE;
7411 return tSTRING_DBEG;
7412 default:
7413 return 0;
7414 }
7415 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7416 return tSTRING_DVAR;
7417 return 0;
7418}
7419
7420#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7421#define IS_END() IS_lex_state(EXPR_END_ANY)
7422#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7423#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7424#define IS_LABEL_POSSIBLE() (\
7425 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7426 IS_ARG())
7427#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7428#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7429
7430static inline enum yytokentype
7431parser_string_term(struct parser_params *p, int func)
7432{
7433 p->lex.strterm = 0;
7434 if (func & STR_FUNC_REGEXP) {
7435 set_yylval_num(regx_options(p));
7436 dispatch_scan_event(p, tREGEXP_END);
7437 SET_LEX_STATE(EXPR_END);
7438 return tREGEXP_END;
7439 }
7440 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7441 nextc(p);
7442 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7443 return tLABEL_END;
7444 }
7445 SET_LEX_STATE(EXPR_END);
7446 return tSTRING_END;
7447}
7448
7449static enum yytokentype
7450parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7451{
7452 int func = (int)quote->u1.func;
7453 int term = (int)quote->u3.term;
7454 int paren = (int)quote->u2.paren;
7455 int c, space = 0;
7456 rb_encoding *enc = p->enc;
7457 rb_encoding *base_enc = 0;
7458 VALUE lit;
7459
7460 if (func & STR_FUNC_TERM) {
7461 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7462 SET_LEX_STATE(EXPR_END);
7463 p->lex.strterm = 0;
7464 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7465 }
7466 c = nextc(p);
7467 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7468 do {c = nextc(p);} while (ISSPACE(c));
7469 space = 1;
7470 }
7471 if (func & STR_FUNC_LIST) {
7472 quote->u1.func &= ~STR_FUNC_LIST;
7473 space = 1;
7474 }
7475 if (c == term && !quote->u0.nest) {
7476 if (func & STR_FUNC_QWORDS) {
7477 quote->u1.func |= STR_FUNC_TERM;
7478 pushback(p, c); /* dispatch the term at tSTRING_END */
7479 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7480 return ' ';
7481 }
7482 return parser_string_term(p, func);
7483 }
7484 if (space) {
7485 pushback(p, c);
7486 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7487 return ' ';
7488 }
7489 newtok(p);
7490 if ((func & STR_FUNC_EXPAND) && c == '#') {
7491 int t = parser_peek_variable_name(p);
7492 if (t) return t;
7493 tokadd(p, '#');
7494 c = nextc(p);
7495 }
7496 pushback(p, c);
7497 if (tokadd_string(p, func, term, paren, &quote->u0.nest,
7498 &enc, &base_enc) == -1) {
7499 if (p->eofp) {
7500#ifndef RIPPER
7501# define unterminated_literal(mesg) yyerror0(mesg)
7502#else
7503# define unterminated_literal(mesg) compile_error(p, mesg)
7504#endif
7505 literal_flush(p, p->lex.pcur);
7506 if (func & STR_FUNC_QWORDS) {
7507 /* no content to add, bailing out here */
7508 unterminated_literal("unterminated list meets end of file");
7509 p->lex.strterm = 0;
7510 return tSTRING_END;
7511 }
7512 if (func & STR_FUNC_REGEXP) {
7513 unterminated_literal("unterminated regexp meets end of file");
7514 }
7515 else {
7516 unterminated_literal("unterminated string meets end of file");
7517 }
7518 quote->u1.func |= STR_FUNC_TERM;
7519 }
7520 }
7521
7522 tokfix(p);
7523 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7524 set_yylval_str(lit);
7525 flush_string_content(p, enc);
7526
7527 return tSTRING_CONTENT;
7528}
7529
7530static enum yytokentype
7531heredoc_identifier(struct parser_params *p)
7532{
7533 /*
7534 * term_len is length of `<<"END"` except `END`,
7535 * in this case term_len is 4 (<, <, " and ").
7536 */
7537 long len, offset = p->lex.pcur - p->lex.pbeg;
7538 int c = nextc(p), term, func = 0, quote = 0;
7539 enum yytokentype token = tSTRING_BEG;
7540 int indent = 0;
7541
7542 if (c == '-') {
7543 c = nextc(p);
7544 func = STR_FUNC_INDENT;
7545 offset++;
7546 }
7547 else if (c == '~') {
7548 c = nextc(p);
7549 func = STR_FUNC_INDENT;
7550 offset++;
7551 indent = INT_MAX;
7552 }
7553 switch (c) {
7554 case '\'':
7555 func |= str_squote; goto quoted;
7556 case '"':
7557 func |= str_dquote; goto quoted;
7558 case '`':
7559 token = tXSTRING_BEG;
7560 func |= str_xquote; goto quoted;
7561
7562 quoted:
7563 quote++;
7564 offset++;
7565 term = c;
7566 len = 0;
7567 while ((c = nextc(p)) != term) {
7568 if (c == -1 || c == '\r' || c == '\n') {
7569 yyerror0("unterminated here document identifier");
7570 return -1;
7571 }
7572 }
7573 break;
7574
7575 default:
7576 if (!parser_is_identchar(p)) {
7577 pushback(p, c);
7578 if (func & STR_FUNC_INDENT) {
7579 pushback(p, indent > 0 ? '~' : '-');
7580 }
7581 return 0;
7582 }
7583 func |= str_dquote;
7584 do {
7585 int n = parser_precise_mbclen(p, p->lex.pcur-1);
7586 if (n < 0) return 0;
7587 p->lex.pcur += --n;
7588 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7589 pushback(p, c);
7590 break;
7591 }
7592
7593 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7594 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7595 yyerror0("too long here document identifier");
7596 dispatch_scan_event(p, tHEREDOC_BEG);
7597 lex_goto_eol(p);
7598
7599 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7600 p->lex.strterm->flags |= STRTERM_HEREDOC;
7601 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7602 here->offset = offset;
7603 here->sourceline = p->ruby_sourceline;
7604 here->length = (int)len;
7605 here->quote = quote;
7606 here->func = func;
7607
7608 token_flush(p);
7609 p->heredoc_indent = indent;
7610 p->heredoc_line_indent = 0;
7611 return token;
7612}
7613
7614static void
7615heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7616{
7617 VALUE line;
7618
7619 p->lex.strterm = 0;
7620 line = here->lastline;
7621 p->lex.lastline = line;
7622 p->lex.pbeg = RSTRING_PTR(line);
7623 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7624 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7625 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7626 p->heredoc_end = p->ruby_sourceline;
7627 p->ruby_sourceline = (int)here->sourceline;
7628 if (p->eofp) p->lex.nextline = Qnil;
7629 p->eofp = 0;
7630}
7631
7632static int
7633dedent_string(VALUE string, int width)
7634{
7635 char *str;
7636 long len;
7637 int i, col = 0;
7638
7639 RSTRING_GETMEM(string, str, len);
7640 for (i = 0; i < len && col < width; i++) {
7641 if (str[i] == ' ') {
7642 col++;
7643 }
7644 else if (str[i] == '\t') {
7645 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7646 if (n > width) break;
7647 col = n;
7648 }
7649 else {
7650 break;
7651 }
7652 }
7653 if (!i) return 0;
7654 rb_str_modify(string);
7655 str = RSTRING_PTR(string);
7656 if (RSTRING_LEN(string) != len)
7657 rb_fatal("literal string changed: %+"PRIsVALUE, string);
7658 MEMMOVE(str, str + i, char, len - i);
7659 rb_str_set_len(string, len - i);
7660 return i;
7661}
7662
7663#ifndef RIPPER
7664static NODE *
7665heredoc_dedent(struct parser_params *p, NODE *root)
7666{
7667 NODE *node, *str_node, *prev_node;
7668 int indent = p->heredoc_indent;
7669 VALUE prev_lit = 0;
7670
7671 if (indent <= 0) return root;
7672 p->heredoc_indent = 0;
7673 if (!root) return root;
7674
7675 prev_node = node = str_node = root;
7676 if (nd_type_p(root, NODE_LIST)) str_node = root->nd_head;
7677
7678 while (str_node) {
7679 VALUE lit = str_node->nd_lit;
7680 if (str_node->flags & NODE_FL_NEWLINE) {
7681 dedent_string(lit, indent);
7682 }
7683 if (!prev_lit) {
7684 prev_lit = lit;
7685 }
7686 else if (!literal_concat0(p, prev_lit, lit)) {
7687 return 0;
7688 }
7689 else {
7690 NODE *end = node->nd_end;
7691 node = prev_node->nd_next = node->nd_next;
7692 if (!node) {
7693 if (nd_type_p(prev_node, NODE_DSTR))
7694 nd_set_type(prev_node, NODE_STR);
7695 break;
7696 }
7697 node->nd_end = end;
7698 goto next_str;
7699 }
7700
7701 str_node = 0;
7702 while ((node = (prev_node = node)->nd_next) != 0) {
7703 next_str:
7704 if (!nd_type_p(node, NODE_LIST)) break;
7705 if ((str_node = node->nd_head) != 0) {
7706 enum node_type type = nd_type(str_node);
7707 if (type == NODE_STR || type == NODE_DSTR) break;
7708 prev_lit = 0;
7709 str_node = 0;
7710 }
7711 }
7712 }
7713 return root;
7714}
7715#else /* RIPPER */
7716static VALUE
7717heredoc_dedent(struct parser_params *p, VALUE array)
7718{
7719 int indent = p->heredoc_indent;
7720
7721 if (indent <= 0) return array;
7722 p->heredoc_indent = 0;
7723 dispatch2(heredoc_dedent, array, INT2NUM(indent));
7724 return array;
7725}
7726
7727/*
7728 * call-seq:
7729 * Ripper.dedent_string(input, width) -> Integer
7730 *
7731 * USE OF RIPPER LIBRARY ONLY.
7732 *
7733 * Strips up to +width+ leading whitespaces from +input+,
7734 * and returns the stripped column width.
7735 */
7736static VALUE
7737parser_dedent_string(VALUE self, VALUE input, VALUE width)
7738{
7739 int wid, col;
7740
7741 StringValue(input);
7742 wid = NUM2UINT(width);
7743 col = dedent_string(input, wid);
7744 return INT2NUM(col);
7745}
7746#endif
7747
7748static int
7749whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7750{
7751 const char *ptr = p->lex.pbeg;
7752 long n;
7753
7754 if (indent) {
7755 while (*ptr && ISSPACE(*ptr)) ptr++;
7756 }
7757 n = p->lex.pend - (ptr + len);
7758 if (n < 0) return FALSE;
7759 if (n > 0 && ptr[len] != '\n') {
7760 if (ptr[len] != '\r') return FALSE;
7761 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7762 }
7763 return strncmp(eos, ptr, len) == 0;
7764}
7765
7766static int
7767word_match_p(struct parser_params *p, const char *word, long len)
7768{
7769 if (strncmp(p->lex.pcur, word, len)) return 0;
7770 if (p->lex.pcur + len == p->lex.pend) return 1;
7771 int c = (unsigned char)p->lex.pcur[len];
7772 if (ISSPACE(c)) return 1;
7773 switch (c) {
7774 case '\0': case '\004': case '\032': return 1;
7775 }
7776 return 0;
7777}
7778
7779#define NUM_SUFFIX_R (1<<0)
7780#define NUM_SUFFIX_I (1<<1)
7781#define NUM_SUFFIX_ALL 3
7782
7783static int
7784number_literal_suffix(struct parser_params *p, int mask)
7785{
7786 int c, result = 0;
7787 const char *lastp = p->lex.pcur;
7788
7789 while ((c = nextc(p)) != -1) {
7790 if ((mask & NUM_SUFFIX_I) && c == 'i') {
7791 result |= (mask & NUM_SUFFIX_I);
7792 mask &= ~NUM_SUFFIX_I;
7793 /* r after i, rational of complex is disallowed */
7794 mask &= ~NUM_SUFFIX_R;
7795 continue;
7796 }
7797 if ((mask & NUM_SUFFIX_R) && c == 'r') {
7798 result |= (mask & NUM_SUFFIX_R);
7799 mask &= ~NUM_SUFFIX_R;
7800 continue;
7801 }
7802 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7803 p->lex.pcur = lastp;
7804 literal_flush(p, p->lex.pcur);
7805 return 0;
7806 }
7807 pushback(p, c);
7808 break;
7809 }
7810 return result;
7811}
7812
7813static enum yytokentype
7814set_number_literal(struct parser_params *p, VALUE v,
7815 enum yytokentype type, int suffix)
7816{
7817 if (suffix & NUM_SUFFIX_I) {
7818 v = rb_complex_raw(INT2FIX(0), v);
7819 type = tIMAGINARY;
7820 }
7821 set_yylval_literal(v);
7822 SET_LEX_STATE(EXPR_END);
7823 return type;
7824}
7825
7826static enum yytokentype
7827set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7828{
7829 enum yytokentype type = tINTEGER;
7830 if (suffix & NUM_SUFFIX_R) {
7831 v = rb_rational_raw1(v);
7832 type = tRATIONAL;
7833 }
7834 return set_number_literal(p, v, type, suffix);
7835}
7836
7837#ifdef RIPPER
7838static void
7839dispatch_heredoc_end(struct parser_params *p)
7840{
7841 VALUE str;
7842 if (has_delayed_token(p))
7843 dispatch_delayed_token(p, tSTRING_CONTENT);
7844 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7845 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7846 lex_goto_eol(p);
7847 token_flush(p);
7848}
7849
7850#else
7851#define dispatch_heredoc_end(p) ((void)0)
7852#endif
7853
7854static enum yytokentype
7855here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7856{
7857 int c, func, indent = 0;
7858 const char *eos, *ptr, *ptr_end;
7859 long len;
7860 VALUE str = 0;
7861 rb_encoding *enc = p->enc;
7862 rb_encoding *base_enc = 0;
7863 int bol;
7864
7865 eos = RSTRING_PTR(here->lastline) + here->offset;
7866 len = here->length;
7867 indent = (func = here->func) & STR_FUNC_INDENT;
7868
7869 if ((c = nextc(p)) == -1) {
7870 error:
7871#ifdef RIPPER
7872 if (!has_delayed_token(p)) {
7873 dispatch_scan_event(p, tSTRING_CONTENT);
7874 }
7875 else {
7876 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7877 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7878 int cr = ENC_CODERANGE_UNKNOWN;
7879 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7880 if (cr != ENC_CODERANGE_7BIT &&
7881 p->enc == rb_usascii_encoding() &&
7882 enc != rb_utf8_encoding()) {
7883 enc = rb_ascii8bit_encoding();
7884 }
7885 }
7886 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7887 }
7888 dispatch_delayed_token(p, tSTRING_CONTENT);
7889 }
7890 lex_goto_eol(p);
7891#endif
7892 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7893 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7894 (int)len, eos);
7895 token_flush(p);
7896 p->lex.strterm = 0;
7897 SET_LEX_STATE(EXPR_END);
7898 return tSTRING_END;
7899 }
7900 bol = was_bol(p);
7901 if (!bol) {
7902 /* not beginning of line, cannot be the terminator */
7903 }
7904 else if (p->heredoc_line_indent == -1) {
7905 /* `heredoc_line_indent == -1` means
7906 * - "after an interpolation in the same line", or
7907 * - "in a continuing line"
7908 */
7909 p->heredoc_line_indent = 0;
7910 }
7911 else if (whole_match_p(p, eos, len, indent)) {
7912 dispatch_heredoc_end(p);
7913 restore:
7914 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7915 token_flush(p);
7916 p->lex.strterm = 0;
7917 SET_LEX_STATE(EXPR_END);
7918 return tSTRING_END;
7919 }
7920
7921 if (!(func & STR_FUNC_EXPAND)) {
7922 do {
7923 ptr = RSTRING_PTR(p->lex.lastline);
7924 ptr_end = p->lex.pend;
7925 if (ptr_end > ptr) {
7926 switch (ptr_end[-1]) {
7927 case '\n':
7928 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7929 ptr_end++;
7930 break;
7931 }
7932 case '\r':
7933 --ptr_end;
7934 }
7935 }
7936
7937 if (p->heredoc_indent > 0) {
7938 long i = 0;
7939 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7940 i++;
7941 p->heredoc_line_indent = 0;
7942 }
7943
7944 if (str)
7945 rb_str_cat(str, ptr, ptr_end - ptr);
7946 else
7947 str = STR_NEW(ptr, ptr_end - ptr);
7948 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7949 lex_goto_eol(p);
7950 if (p->heredoc_indent > 0) {
7951 goto flush_str;
7952 }
7953 if (nextc(p) == -1) {
7954 if (str) {
7955 str = 0;
7956 }
7957 goto error;
7958 }
7959 } while (!whole_match_p(p, eos, len, indent));
7960 }
7961 else {
7962 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7963 newtok(p);
7964 if (c == '#') {
7965 int t = parser_peek_variable_name(p);
7966 if (p->heredoc_line_indent != -1) {
7967 if (p->heredoc_indent > p->heredoc_line_indent) {
7968 p->heredoc_indent = p->heredoc_line_indent;
7969 }
7970 p->heredoc_line_indent = -1;
7971 }
7972 if (t) return t;
7973 tokadd(p, '#');
7974 c = nextc(p);
7975 }
7976 do {
7977 pushback(p, c);
7978 enc = p->enc;
7979 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7980 if (p->eofp) goto error;
7981 goto restore;
7982 }
7983 if (c != '\n') {
7984 if (c == '\\') p->heredoc_line_indent = -1;
7985 flush:
7986 str = STR_NEW3(tok(p), toklen(p), enc, func);
7987 flush_str:
7988 set_yylval_str(str);
7989#ifndef RIPPER
7990 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7991#endif
7992 flush_string_content(p, enc);
7993 return tSTRING_CONTENT;
7994 }
7995 tokadd(p, nextc(p));
7996 if (p->heredoc_indent > 0) {
7997 lex_goto_eol(p);
7998 goto flush;
7999 }
8000 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
8001 if ((c = nextc(p)) == -1) goto error;
8002 } while (!whole_match_p(p, eos, len, indent));
8003 str = STR_NEW3(tok(p), toklen(p), enc, func);
8004 }
8005 dispatch_heredoc_end(p);
8006#ifdef RIPPER
8007 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
8008 yylval.val, str);
8009#endif
8010 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8011 token_flush(p);
8012 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
8013 set_yylval_str(str);
8014#ifndef RIPPER
8015 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8016#endif
8017 return tSTRING_CONTENT;
8018}
8019
8020#include "lex.c"
8021
8022static int
8023arg_ambiguous(struct parser_params *p, char c)
8024{
8025#ifndef RIPPER
8026 if (c == '/') {
8027 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
8028 }
8029 else {
8030 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
8031 }
8032#else
8033 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
8034#endif
8035 return TRUE;
8036}
8037
8038static ID
8039#ifndef RIPPER
8040formal_argument(struct parser_params *p, ID lhs)
8041#else
8042formal_argument(struct parser_params *p, VALUE lhs)
8043#endif
8044{
8045 ID id = get_id(lhs);
8046
8047 switch (id_type(id)) {
8048 case ID_LOCAL:
8049 break;
8050#ifndef RIPPER
8051# define ERR(mesg) yyerror0(mesg)
8052#else
8053# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
8054#endif
8055 case ID_CONST:
8056 ERR("formal argument cannot be a constant");
8057 return 0;
8058 case ID_INSTANCE:
8059 ERR("formal argument cannot be an instance variable");
8060 return 0;
8061 case ID_GLOBAL:
8062 ERR("formal argument cannot be a global variable");
8063 return 0;
8064 case ID_CLASS:
8065 ERR("formal argument cannot be a class variable");
8066 return 0;
8067 default:
8068 ERR("formal argument must be local variable");
8069 return 0;
8070#undef ERR
8071 }
8072 shadowing_lvar(p, id);
8073 return lhs;
8074}
8075
8076static int
8077lvar_defined(struct parser_params *p, ID id)
8078{
8079 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
8080}
8081
8082/* emacsen -*- hack */
8083static long
8084parser_encode_length(struct parser_params *p, const char *name, long len)
8085{
8086 long nlen;
8087
8088 if (len > 5 && name[nlen = len - 5] == '-') {
8089 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
8090 return nlen;
8091 }
8092 if (len > 4 && name[nlen = len - 4] == '-') {
8093 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
8094 return nlen;
8095 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
8096 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
8097 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
8098 return nlen;
8099 }
8100 return len;
8101}
8102
8103static void
8104parser_set_encode(struct parser_params *p, const char *name)
8105{
8106 int idx = rb_enc_find_index(name);
8107 rb_encoding *enc;
8108 VALUE excargs[3];
8109
8110 if (idx < 0) {
8111 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
8112 error:
8113 excargs[0] = rb_eArgError;
8114 excargs[2] = rb_make_backtrace();
8115 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
8116 rb_exc_raise(rb_make_exception(3, excargs));
8117 }
8118 enc = rb_enc_from_index(idx);
8119 if (!rb_enc_asciicompat(enc)) {
8120 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
8121 goto error;
8122 }
8123 p->enc = enc;
8124#ifndef RIPPER
8125 if (p->debug_lines) {
8126 VALUE lines = p->debug_lines;
8127 long i, n = RARRAY_LEN(lines);
8128 for (i = 0; i < n; ++i) {
8129 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
8130 }
8131 }
8132#endif
8133}
8134
8135static int
8136comment_at_top(struct parser_params *p)
8137{
8138 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
8139 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
8140 while (ptr < ptr_end) {
8141 if (!ISSPACE(*ptr)) return 0;
8142 ptr++;
8143 }
8144 return 1;
8145}
8146
8147typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
8148typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
8149
8150static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
8151
8152static void
8153magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
8154{
8155 if (!comment_at_top(p)) {
8156 return;
8157 }
8158 parser_set_encode(p, val);
8159}
8160
8161static int
8162parser_get_bool(struct parser_params *p, const char *name, const char *val)
8163{
8164 switch (*val) {
8165 case 't': case 'T':
8166 if (STRCASECMP(val, "true") == 0) {
8167 return TRUE;
8168 }
8169 break;
8170 case 'f': case 'F':
8171 if (STRCASECMP(val, "false") == 0) {
8172 return FALSE;
8173 }
8174 break;
8175 }
8176 return parser_invalid_pragma_value(p, name, val);
8177}
8178
8179static int
8180parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
8181{
8182 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
8183 return -1;
8184}
8185
8186static void
8187parser_set_token_info(struct parser_params *p, const char *name, const char *val)
8188{
8189 int b = parser_get_bool(p, name, val);
8190 if (b >= 0) p->token_info_enabled = b;
8191}
8192
8193static void
8194parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
8195{
8196 int b;
8197
8198 if (p->token_seen) {
8199 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
8200 return;
8201 }
8202
8203 b = parser_get_bool(p, name, val);
8204 if (b < 0) return;
8205
8206 if (!p->compile_option)
8207 p->compile_option = rb_obj_hide(rb_ident_hash_new());
8208 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
8209 RBOOL(b));
8210}
8211
8212static void
8213parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
8214{
8215 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
8216 if (*s == ' ' || *s == '\t') continue;
8217 if (*s == '#') break;
8218 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
8219 return;
8220 }
8221
8222 switch (*val) {
8223 case 'n': case 'N':
8224 if (STRCASECMP(val, "none") == 0) {
8225 p->ctxt.shareable_constant_value = shareable_none;
8226 return;
8227 }
8228 break;
8229 case 'l': case 'L':
8230 if (STRCASECMP(val, "literal") == 0) {
8231 p->ctxt.shareable_constant_value = shareable_literal;
8232 return;
8233 }
8234 break;
8235 case 'e': case 'E':
8236 if (STRCASECMP(val, "experimental_copy") == 0) {
8237 p->ctxt.shareable_constant_value = shareable_copy;
8238 return;
8239 }
8240 if (STRCASECMP(val, "experimental_everything") == 0) {
8241 p->ctxt.shareable_constant_value = shareable_everything;
8242 return;
8243 }
8244 break;
8245 }
8246 parser_invalid_pragma_value(p, name, val);
8247}
8248
8249# if WARN_PAST_SCOPE
8250static void
8251parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
8252{
8253 int b = parser_get_bool(p, name, val);
8254 if (b >= 0) p->past_scope_enabled = b;
8255}
8256# endif
8257
8258struct magic_comment {
8259 const char *name;
8260 rb_magic_comment_setter_t func;
8261 rb_magic_comment_length_t length;
8262};
8263
8264static const struct magic_comment magic_comments[] = {
8265 {"coding", magic_comment_encoding, parser_encode_length},
8266 {"encoding", magic_comment_encoding, parser_encode_length},
8267 {"frozen_string_literal", parser_set_compile_option_flag},
8268 {"shareable_constant_value", parser_set_shareable_constant_value},
8269 {"warn_indent", parser_set_token_info},
8270# if WARN_PAST_SCOPE
8271 {"warn_past_scope", parser_set_past_scope},
8272# endif
8273};
8274
8275static const char *
8276magic_comment_marker(const char *str, long len)
8277{
8278 long i = 2;
8279
8280 while (i < len) {
8281 switch (str[i]) {
8282 case '-':
8283 if (str[i-1] == '*' && str[i-2] == '-') {
8284 return str + i + 1;
8285 }
8286 i += 2;
8287 break;
8288 case '*':
8289 if (i + 1 >= len) return 0;
8290 if (str[i+1] != '-') {
8291 i += 4;
8292 }
8293 else if (str[i-1] != '-') {
8294 i += 2;
8295 }
8296 else {
8297 return str + i + 2;
8298 }
8299 break;
8300 default:
8301 i += 3;
8302 break;
8303 }
8304 }
8305 return 0;
8306}
8307
8308static int
8309parser_magic_comment(struct parser_params *p, const char *str, long len)
8310{
8311 int indicator = 0;
8312 VALUE name = 0, val = 0;
8313 const char *beg, *end, *vbeg, *vend;
8314#define str_copy(_s, _p, _n) ((_s) \
8315 ? (void)(rb_str_resize((_s), (_n)), \
8316 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
8317 : (void)((_s) = STR_NEW((_p), (_n))))
8318
8319 if (len <= 7) return FALSE;
8320 if (!!(beg = magic_comment_marker(str, len))) {
8321 if (!(end = magic_comment_marker(beg, str + len - beg)))
8322 return FALSE;
8323 indicator = TRUE;
8324 str = beg;
8325 len = end - beg - 3;
8326 }
8327
8328 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
8329 while (len > 0) {
8330 const struct magic_comment *mc = magic_comments;
8331 char *s;
8332 int i;
8333 long n = 0;
8334
8335 for (; len > 0 && *str; str++, --len) {
8336 switch (*str) {
8337 case '\'': case '"': case ':': case ';':
8338 continue;
8339 }
8340 if (!ISSPACE(*str)) break;
8341 }
8342 for (beg = str; len > 0; str++, --len) {
8343 switch (*str) {
8344 case '\'': case '"': case ':': case ';':
8345 break;
8346 default:
8347 if (ISSPACE(*str)) break;
8348 continue;
8349 }
8350 break;
8351 }
8352 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
8353 if (!len) break;
8354 if (*str != ':') {
8355 if (!indicator) return FALSE;
8356 continue;
8357 }
8358
8359 do str++; while (--len > 0 && ISSPACE(*str));
8360 if (!len) break;
8361 if (*str == '"') {
8362 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
8363 if (*str == '\\') {
8364 --len;
8365 ++str;
8366 }
8367 }
8368 vend = str;
8369 if (len) {
8370 --len;
8371 ++str;
8372 }
8373 }
8374 else {
8375 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
8376 vend = str;
8377 }
8378 if (indicator) {
8379 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
8380 }
8381 else {
8382 while (len > 0 && (ISSPACE(*str))) --len, str++;
8383 if (len) return FALSE;
8384 }
8385
8386 n = end - beg;
8387 str_copy(name, beg, n);
8388 s = RSTRING_PTR(name);
8389 for (i = 0; i < n; ++i) {
8390 if (s[i] == '-') s[i] = '_';
8391 }
8392 do {
8393 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
8394 n = vend - vbeg;
8395 if (mc->length) {
8396 n = (*mc->length)(p, vbeg, n);
8397 }
8398 str_copy(val, vbeg, n);
8399 (*mc->func)(p, mc->name, RSTRING_PTR(val));
8400 break;
8401 }
8402 } while (++mc < magic_comments + numberof(magic_comments));
8403#ifdef RIPPER
8404 str_copy(val, vbeg, vend - vbeg);
8405 dispatch2(magic_comment, name, val);
8406#endif
8407 }
8408
8409 return TRUE;
8410}
8411
8412static void
8413set_file_encoding(struct parser_params *p, const char *str, const char *send)
8414{
8415 int sep = 0;
8416 const char *beg = str;
8417 VALUE s;
8418
8419 for (;;) {
8420 if (send - str <= 6) return;
8421 switch (str[6]) {
8422 case 'C': case 'c': str += 6; continue;
8423 case 'O': case 'o': str += 5; continue;
8424 case 'D': case 'd': str += 4; continue;
8425 case 'I': case 'i': str += 3; continue;
8426 case 'N': case 'n': str += 2; continue;
8427 case 'G': case 'g': str += 1; continue;
8428 case '=': case ':':
8429 sep = 1;
8430 str += 6;
8431 break;
8432 default:
8433 str += 6;
8434 if (ISSPACE(*str)) break;
8435 continue;
8436 }
8437 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8438 sep = 0;
8439 }
8440 for (;;) {
8441 do {
8442 if (++str >= send) return;
8443 } while (ISSPACE(*str));
8444 if (sep) break;
8445 if (*str != '=' && *str != ':') return;
8446 sep = 1;
8447 str++;
8448 }
8449 beg = str;
8450 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8451 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8452 parser_set_encode(p, RSTRING_PTR(s));
8453 rb_str_resize(s, 0);
8454}
8455
8456static void
8457parser_prepare(struct parser_params *p)
8458{
8459 int c = nextc0(p, FALSE);
8460 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8461 switch (c) {
8462 case '#':
8463 if (peek(p, '!')) p->has_shebang = 1;
8464 break;
8465 case 0xef: /* UTF-8 BOM marker */
8466 if (p->lex.pend - p->lex.pcur >= 2 &&
8467 (unsigned char)p->lex.pcur[0] == 0xbb &&
8468 (unsigned char)p->lex.pcur[1] == 0xbf) {
8469 p->enc = rb_utf8_encoding();
8470 p->lex.pcur += 2;
8471#ifndef RIPPER
8472 if (p->debug_lines) {
8473 rb_enc_associate(p->lex.lastline, p->enc);
8474 }
8475#endif
8476 p->lex.pbeg = p->lex.pcur;
8477 return;
8478 }
8479 break;
8480 case EOF:
8481 return;
8482 }
8483 pushback(p, c);
8484 p->enc = rb_enc_get(p->lex.lastline);
8485}
8486
8487#ifndef RIPPER
8488#define ambiguous_operator(tok, op, syn) ( \
8489 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8490 rb_warning0("even though it seems like "syn""))
8491#else
8492#define ambiguous_operator(tok, op, syn) \
8493 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8494#endif
8495#define warn_balanced(tok, op, syn) ((void) \
8496 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8497 space_seen && !ISSPACE(c) && \
8498 (ambiguous_operator(tok, op, syn), 0)), \
8499 (enum yytokentype)(tok))
8500
8501static VALUE
8502parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8503{
8504 VALUE v;
8505 char *point = &str[seen_point];
8506 size_t fraclen = len-seen_point-1;
8507 memmove(point, point+1, fraclen+1);
8508 v = rb_cstr_to_inum(str, 10, FALSE);
8509 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8510}
8511
8512static enum yytokentype
8513no_digits(struct parser_params *p)
8514{
8515 yyerror0("numeric literal without digits");
8516 if (peek(p, '_')) nextc(p);
8517 /* dummy 0, for tUMINUS_NUM at numeric */
8518 return set_integer_literal(p, INT2FIX(0), 0);
8519}
8520
8521static enum yytokentype
8522parse_numeric(struct parser_params *p, int c)
8523{
8524 int is_float, seen_point, seen_e, nondigit;
8525 int suffix;
8526
8527 is_float = seen_point = seen_e = nondigit = 0;
8528 SET_LEX_STATE(EXPR_END);
8529 newtok(p);
8530 if (c == '-' || c == '+') {
8531 tokadd(p, c);
8532 c = nextc(p);
8533 }
8534 if (c == '0') {
8535 int start = toklen(p);
8536 c = nextc(p);
8537 if (c == 'x' || c == 'X') {
8538 /* hexadecimal */
8539 c = nextc(p);
8540 if (c != -1 && ISXDIGIT(c)) {
8541 do {
8542 if (c == '_') {
8543 if (nondigit) break;
8544 nondigit = c;
8545 continue;
8546 }
8547 if (!ISXDIGIT(c)) break;
8548 nondigit = 0;
8549 tokadd(p, c);
8550 } while ((c = nextc(p)) != -1);
8551 }
8552 pushback(p, c);
8553 tokfix(p);
8554 if (toklen(p) == start) {
8555 return no_digits(p);
8556 }
8557 else if (nondigit) goto trailing_uc;
8558 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8559 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8560 }
8561 if (c == 'b' || c == 'B') {
8562 /* binary */
8563 c = nextc(p);
8564 if (c == '0' || c == '1') {
8565 do {
8566 if (c == '_') {
8567 if (nondigit) break;
8568 nondigit = c;
8569 continue;
8570 }
8571 if (c != '0' && c != '1') break;
8572 nondigit = 0;
8573 tokadd(p, c);
8574 } while ((c = nextc(p)) != -1);
8575 }
8576 pushback(p, c);
8577 tokfix(p);
8578 if (toklen(p) == start) {
8579 return no_digits(p);
8580 }
8581 else if (nondigit) goto trailing_uc;
8582 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8583 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8584 }
8585 if (c == 'd' || c == 'D') {
8586 /* decimal */
8587 c = nextc(p);
8588 if (c != -1 && ISDIGIT(c)) {
8589 do {
8590 if (c == '_') {
8591 if (nondigit) break;
8592 nondigit = c;
8593 continue;
8594 }
8595 if (!ISDIGIT(c)) break;
8596 nondigit = 0;
8597 tokadd(p, c);
8598 } while ((c = nextc(p)) != -1);
8599 }
8600 pushback(p, c);
8601 tokfix(p);
8602 if (toklen(p) == start) {
8603 return no_digits(p);
8604 }
8605 else if (nondigit) goto trailing_uc;
8606 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8607 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8608 }
8609 if (c == '_') {
8610 /* 0_0 */
8611 goto octal_number;
8612 }
8613 if (c == 'o' || c == 'O') {
8614 /* prefixed octal */
8615 c = nextc(p);
8616 if (c == -1 || c == '_' || !ISDIGIT(c)) {
8617 return no_digits(p);
8618 }
8619 }
8620 if (c >= '0' && c <= '7') {
8621 /* octal */
8622 octal_number:
8623 do {
8624 if (c == '_') {
8625 if (nondigit) break;
8626 nondigit = c;
8627 continue;
8628 }
8629 if (c < '0' || c > '9') break;
8630 if (c > '7') goto invalid_octal;
8631 nondigit = 0;
8632 tokadd(p, c);
8633 } while ((c = nextc(p)) != -1);
8634 if (toklen(p) > start) {
8635 pushback(p, c);
8636 tokfix(p);
8637 if (nondigit) goto trailing_uc;
8638 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8639 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8640 }
8641 if (nondigit) {
8642 pushback(p, c);
8643 goto trailing_uc;
8644 }
8645 }
8646 if (c > '7' && c <= '9') {
8647 invalid_octal:
8648 yyerror0("Invalid octal digit");
8649 }
8650 else if (c == '.' || c == 'e' || c == 'E') {
8651 tokadd(p, '0');
8652 }
8653 else {
8654 pushback(p, c);
8655 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8656 return set_integer_literal(p, INT2FIX(0), suffix);
8657 }
8658 }
8659
8660 for (;;) {
8661 switch (c) {
8662 case '0': case '1': case '2': case '3': case '4':
8663 case '5': case '6': case '7': case '8': case '9':
8664 nondigit = 0;
8665 tokadd(p, c);
8666 break;
8667
8668 case '.':
8669 if (nondigit) goto trailing_uc;
8670 if (seen_point || seen_e) {
8671 goto decode_num;
8672 }
8673 else {
8674 int c0 = nextc(p);
8675 if (c0 == -1 || !ISDIGIT(c0)) {
8676 pushback(p, c0);
8677 goto decode_num;
8678 }
8679 c = c0;
8680 }
8681 seen_point = toklen(p);
8682 tokadd(p, '.');
8683 tokadd(p, c);
8684 is_float++;
8685 nondigit = 0;
8686 break;
8687
8688 case 'e':
8689 case 'E':
8690 if (nondigit) {
8691 pushback(p, c);
8692 c = nondigit;
8693 goto decode_num;
8694 }
8695 if (seen_e) {
8696 goto decode_num;
8697 }
8698 nondigit = c;
8699 c = nextc(p);
8700 if (c != '-' && c != '+' && !ISDIGIT(c)) {
8701 pushback(p, c);
8702 nondigit = 0;
8703 goto decode_num;
8704 }
8705 tokadd(p, nondigit);
8706 seen_e++;
8707 is_float++;
8708 tokadd(p, c);
8709 nondigit = (c == '-' || c == '+') ? c : 0;
8710 break;
8711
8712 case '_': /* `_' in number just ignored */
8713 if (nondigit) goto decode_num;
8714 nondigit = c;
8715 break;
8716
8717 default:
8718 goto decode_num;
8719 }
8720 c = nextc(p);
8721 }
8722
8723 decode_num:
8724 pushback(p, c);
8725 if (nondigit) {
8726 trailing_uc:
8727 literal_flush(p, p->lex.pcur - 1);
8728 YYLTYPE loc = RUBY_INIT_YYLLOC();
8729 compile_error(p, "trailing `%c' in number", nondigit);
8730 parser_show_error_line(p, &loc);
8731 }
8732 tokfix(p);
8733 if (is_float) {
8734 enum yytokentype type = tFLOAT;
8735 VALUE v;
8736
8737 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8738 if (suffix & NUM_SUFFIX_R) {
8739 type = tRATIONAL;
8740 v = parse_rational(p, tok(p), toklen(p), seen_point);
8741 }
8742 else {
8743 double d = strtod(tok(p), 0);
8744 if (errno == ERANGE) {
8745 rb_warning1("Float %s out of range", WARN_S(tok(p)));
8746 errno = 0;
8747 }
8748 v = DBL2NUM(d);
8749 }
8750 return set_number_literal(p, v, type, suffix);
8751 }
8752 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8753 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8754}
8755
8756static enum yytokentype
8757parse_qmark(struct parser_params *p, int space_seen)
8758{
8759 rb_encoding *enc;
8760 register int c;
8761 VALUE lit;
8762
8763 if (IS_END()) {
8764 SET_LEX_STATE(EXPR_VALUE);
8765 return '?';
8766 }
8767 c = nextc(p);
8768 if (c == -1) {
8769 compile_error(p, "incomplete character syntax");
8770 return 0;
8771 }
8772 if (rb_enc_isspace(c, p->enc)) {
8773 if (!IS_ARG()) {
8774 int c2 = escaped_control_code(c);
8775 if (c2) {
8776 WARN_SPACE_CHAR(c2, "?");
8777 }
8778 }
8779 ternary:
8780 pushback(p, c);
8781 SET_LEX_STATE(EXPR_VALUE);
8782 return '?';
8783 }
8784 newtok(p);
8785 enc = p->enc;
8786 if (!parser_isascii(p)) {
8787 if (tokadd_mbchar(p, c) == -1) return 0;
8788 }
8789 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8790 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8791 if (space_seen) {
8792 const char *start = p->lex.pcur - 1, *ptr = start;
8793 do {
8794 int n = parser_precise_mbclen(p, ptr);
8795 if (n < 0) return -1;
8796 ptr += n;
8797 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8798 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8799 " a conditional operator, put a space after `?'",
8800 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8801 }
8802 goto ternary;
8803 }
8804 else if (c == '\\') {
8805 if (peek(p, 'u')) {
8806 nextc(p);
8807 enc = rb_utf8_encoding();
8808 tokadd_utf8(p, &enc, -1, 0, 0);
8809 }
8810 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8811 nextc(p);
8812 if (tokadd_mbchar(p, c) == -1) return 0;
8813 }
8814 else {
8815 c = read_escape(p, 0, &enc);
8816 tokadd(p, c);
8817 }
8818 }
8819 else {
8820 tokadd(p, c);
8821 }
8822 tokfix(p);
8823 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8824 set_yylval_str(lit);
8825 SET_LEX_STATE(EXPR_END);
8826 return tCHAR;
8827}
8828
8829static enum yytokentype
8830parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8831{
8832 register int c;
8833 const char *ptok = p->lex.pcur;
8834
8835 if (IS_BEG()) {
8836 int term;
8837 int paren;
8838
8839 c = nextc(p);
8840 quotation:
8841 if (c == -1) goto unterminated;
8842 if (!ISALNUM(c)) {
8843 term = c;
8844 if (!ISASCII(c)) goto unknown;
8845 c = 'Q';
8846 }
8847 else {
8848 term = nextc(p);
8849 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8850 unknown:
8851 pushback(p, term);
8852 c = parser_precise_mbclen(p, p->lex.pcur);
8853 if (c < 0) return 0;
8854 p->lex.pcur += c;
8855 yyerror0("unknown type of %string");
8856 return 0;
8857 }
8858 }
8859 if (term == -1) {
8860 unterminated:
8861 compile_error(p, "unterminated quoted string meets end of file");
8862 return 0;
8863 }
8864 paren = term;
8865 if (term == '(') term = ')';
8866 else if (term == '[') term = ']';
8867 else if (term == '{') term = '}';
8868 else if (term == '<') term = '>';
8869 else paren = 0;
8870
8871 p->lex.ptok = ptok-1;
8872 switch (c) {
8873 case 'Q':
8874 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8875 return tSTRING_BEG;
8876
8877 case 'q':
8878 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8879 return tSTRING_BEG;
8880
8881 case 'W':
8882 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8883 return tWORDS_BEG;
8884
8885 case 'w':
8886 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8887 return tQWORDS_BEG;
8888
8889 case 'I':
8890 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8891 return tSYMBOLS_BEG;
8892
8893 case 'i':
8894 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8895 return tQSYMBOLS_BEG;
8896
8897 case 'x':
8898 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8899 return tXSTRING_BEG;
8900
8901 case 'r':
8902 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8903 return tREGEXP_BEG;
8904
8905 case 's':
8906 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8907 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8908 return tSYMBEG;
8909
8910 default:
8911 yyerror0("unknown type of %string");
8912 return 0;
8913 }
8914 }
8915 if ((c = nextc(p)) == '=') {
8916 set_yylval_id('%');
8917 SET_LEX_STATE(EXPR_BEG);
8918 return tOP_ASGN;
8919 }
8920 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8921 goto quotation;
8922 }
8923 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8924 pushback(p, c);
8925 return warn_balanced('%', "%%", "string literal");
8926}
8927
8928static int
8929tokadd_ident(struct parser_params *p, int c)
8930{
8931 do {
8932 if (tokadd_mbchar(p, c) == -1) return -1;
8933 c = nextc(p);
8934 } while (parser_is_identchar(p));
8935 pushback(p, c);
8936 return 0;
8937}
8938
8939static ID
8940tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8941{
8942 ID ident = TOK_INTERN();
8943
8944 set_yylval_name(ident);
8945
8946 return ident;
8947}
8948
8949static int
8950parse_numvar(struct parser_params *p)
8951{
8952 size_t len;
8953 int overflow;
8954 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8955 const unsigned long nth_ref_max =
8956 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8957 /* NTH_REF is left-shifted to be ORed with back-ref flag and
8958 * turned into a Fixnum, in compile.c */
8959
8960 if (overflow || n > nth_ref_max) {
8961 /* compile_error()? */
8962 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8963 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8964 }
8965 else {
8966 return (int)n;
8967 }
8968}
8969
8970static enum yytokentype
8971parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8972{
8973 const char *ptr = p->lex.pcur;
8974 register int c;
8975
8976 SET_LEX_STATE(EXPR_END);
8977 p->lex.ptok = ptr - 1; /* from '$' */
8978 newtok(p);
8979 c = nextc(p);
8980 switch (c) {
8981 case '_': /* $_: last read line string */
8982 c = nextc(p);
8983 if (parser_is_identchar(p)) {
8984 tokadd(p, '$');
8985 tokadd(p, '_');
8986 break;
8987 }
8988 pushback(p, c);
8989 c = '_';
8990 /* fall through */
8991 case '~': /* $~: match-data */
8992 case '*': /* $*: argv */
8993 case '$': /* $$: pid */
8994 case '?': /* $?: last status */
8995 case '!': /* $!: error string */
8996 case '@': /* $@: error position */
8997 case '/': /* $/: input record separator */
8998 case '\\': /* $\: output record separator */
8999 case ';': /* $;: field separator */
9000 case ',': /* $,: output field separator */
9001 case '.': /* $.: last read line number */
9002 case '=': /* $=: ignorecase */
9003 case ':': /* $:: load path */
9004 case '<': /* $<: reading filename */
9005 case '>': /* $>: default output handle */
9006 case '\"': /* $": already loaded files */
9007 tokadd(p, '$');
9008 tokadd(p, c);
9009 goto gvar;
9010
9011 case '-':
9012 tokadd(p, '$');
9013 tokadd(p, c);
9014 c = nextc(p);
9015 if (parser_is_identchar(p)) {
9016 if (tokadd_mbchar(p, c) == -1) return 0;
9017 }
9018 else {
9019 pushback(p, c);
9020 pushback(p, '-');
9021 return '$';
9022 }
9023 gvar:
9024 set_yylval_name(TOK_INTERN());
9025 return tGVAR;
9026
9027 case '&': /* $&: last match */
9028 case '`': /* $`: string before last match */
9029 case '\'': /* $': string after last match */
9030 case '+': /* $+: string matches last paren. */
9031 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
9032 tokadd(p, '$');
9033 tokadd(p, c);
9034 goto gvar;
9035 }
9036 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
9037 return tBACK_REF;
9038
9039 case '1': case '2': case '3':
9040 case '4': case '5': case '6':
9041 case '7': case '8': case '9':
9042 tokadd(p, '$');
9043 do {
9044 tokadd(p, c);
9045 c = nextc(p);
9046 } while (c != -1 && ISDIGIT(c));
9047 pushback(p, c);
9048 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
9049 tokfix(p);
9050 c = parse_numvar(p);
9051 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
9052 return tNTH_REF;
9053
9054 default:
9055 if (!parser_is_identchar(p)) {
9056 YYLTYPE loc = RUBY_INIT_YYLLOC();
9057 if (c == -1 || ISSPACE(c)) {
9058 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
9059 }
9060 else {
9061 pushback(p, c);
9062 compile_error(p, "`$%c' is not allowed as a global variable name", c);
9063 }
9064 parser_show_error_line(p, &loc);
9065 set_yylval_noname();
9066 return tGVAR;
9067 }
9068 /* fall through */
9069 case '0':
9070 tokadd(p, '$');
9071 }
9072
9073 if (tokadd_ident(p, c)) return 0;
9074 SET_LEX_STATE(EXPR_END);
9075 tokenize_ident(p, last_state);
9076 return tGVAR;
9077}
9078
9079#ifndef RIPPER
9080static bool
9081parser_numbered_param(struct parser_params *p, int n)
9082{
9083 if (n < 0) return false;
9084
9085 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
9086 return false;
9087 }
9088 if (p->max_numparam == ORDINAL_PARAM) {
9089 compile_error(p, "ordinary parameter is defined");
9090 return false;
9091 }
9092 struct vtable *args = p->lvtbl->args;
9093 if (p->max_numparam < n) {
9094 p->max_numparam = n;
9095 }
9096 while (n > args->pos) {
9097 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
9098 }
9099 return true;
9100}
9101#endif
9102
9103static enum yytokentype
9104parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
9105{
9106 const char *ptr = p->lex.pcur;
9107 enum yytokentype result = tIVAR;
9108 register int c = nextc(p);
9109 YYLTYPE loc;
9110
9111 p->lex.ptok = ptr - 1; /* from '@' */
9112 newtok(p);
9113 tokadd(p, '@');
9114 if (c == '@') {
9115 result = tCVAR;
9116 tokadd(p, '@');
9117 c = nextc(p);
9118 }
9119 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
9120 if (c == -1 || !parser_is_identchar(p)) {
9121 pushback(p, c);
9122 RUBY_SET_YYLLOC(loc);
9123 if (result == tIVAR) {
9124 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
9125 }
9126 else {
9127 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
9128 }
9129 parser_show_error_line(p, &loc);
9130 set_yylval_noname();
9131 SET_LEX_STATE(EXPR_END);
9132 return result;
9133 }
9134 else if (ISDIGIT(c)) {
9135 pushback(p, c);
9136 RUBY_SET_YYLLOC(loc);
9137 if (result == tIVAR) {
9138 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
9139 }
9140 else {
9141 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
9142 }
9143 parser_show_error_line(p, &loc);
9144 set_yylval_noname();
9145 SET_LEX_STATE(EXPR_END);
9146 return result;
9147 }
9148
9149 if (tokadd_ident(p, c)) return 0;
9150 tokenize_ident(p, last_state);
9151 return result;
9152}
9153
9154static enum yytokentype
9155parse_ident(struct parser_params *p, int c, int cmd_state)
9156{
9157 enum yytokentype result;
9158 int mb = ENC_CODERANGE_7BIT;
9159 const enum lex_state_e last_state = p->lex.state;
9160 ID ident;
9161
9162 do {
9163 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
9164 if (tokadd_mbchar(p, c) == -1) return 0;
9165 c = nextc(p);
9166 } while (parser_is_identchar(p));
9167 if ((c == '!' || c == '?') && !peek(p, '=')) {
9168 result = tFID;
9169 tokadd(p, c);
9170 }
9171 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
9172 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
9173 result = tIDENTIFIER;
9174 tokadd(p, c);
9175 }
9176 else {
9177 result = tCONSTANT; /* assume provisionally */
9178 pushback(p, c);
9179 }
9180 tokfix(p);
9181
9182 if (IS_LABEL_POSSIBLE()) {
9183 if (IS_LABEL_SUFFIX(0)) {
9184 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
9185 nextc(p);
9186 set_yylval_name(TOK_INTERN());
9187 return tLABEL;
9188 }
9189 }
9190 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
9191 const struct kwtable *kw;
9192
9193 /* See if it is a reserved word. */
9194 kw = rb_reserved_word(tok(p), toklen(p));
9195 if (kw) {
9196 enum lex_state_e state = p->lex.state;
9197 if (IS_lex_state_for(state, EXPR_FNAME)) {
9198 SET_LEX_STATE(EXPR_ENDFN);
9199 set_yylval_name(rb_intern2(tok(p), toklen(p)));
9200 return kw->id[0];
9201 }
9202 SET_LEX_STATE(kw->state);
9203 if (IS_lex_state(EXPR_BEG)) {
9204 p->command_start = TRUE;
9205 }
9206 if (kw->id[0] == keyword_do) {
9207 if (lambda_beginning_p()) {
9208 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
9209 return keyword_do_LAMBDA;
9210 }
9211 if (COND_P()) return keyword_do_cond;
9212 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
9213 return keyword_do_block;
9214 return keyword_do;
9215 }
9216 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
9217 return kw->id[0];
9218 else {
9219 if (kw->id[0] != kw->id[1])
9220 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
9221 return kw->id[1];
9222 }
9223 }
9224 }
9225
9226 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
9227 if (cmd_state) {
9228 SET_LEX_STATE(EXPR_CMDARG);
9229 }
9230 else {
9231 SET_LEX_STATE(EXPR_ARG);
9232 }
9233 }
9234 else if (p->lex.state == EXPR_FNAME) {
9235 SET_LEX_STATE(EXPR_ENDFN);
9236 }
9237 else {
9238 SET_LEX_STATE(EXPR_END);
9239 }
9240
9241 ident = tokenize_ident(p, last_state);
9242 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
9243 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
9244 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
9245 lvar_defined(p, ident)) {
9246 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
9247 }
9248 return result;
9249}
9250
9251static enum yytokentype
9252parser_yylex(struct parser_params *p)
9253{
9254 register int c;
9255 int space_seen = 0;
9256 int cmd_state;
9257 int label;
9258 enum lex_state_e last_state;
9259 int fallthru = FALSE;
9260 int token_seen = p->token_seen;
9261
9262 if (p->lex.strterm) {
9263 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
9264 return here_document(p, &p->lex.strterm->u.heredoc);
9265 }
9266 else {
9267 token_flush(p);
9268 return parse_string(p, &p->lex.strterm->u.literal);
9269 }
9270 }
9271 cmd_state = p->command_start;
9272 p->command_start = FALSE;
9273 p->token_seen = TRUE;
9274 retry:
9275 last_state = p->lex.state;
9276#ifndef RIPPER
9277 token_flush(p);
9278#endif
9279 switch (c = nextc(p)) {
9280 case '\0': /* NUL */
9281 case '\004': /* ^D */
9282 case '\032': /* ^Z */
9283 case -1: /* end of script. */
9284 return 0;
9285
9286 /* white spaces */
9287 case '\r':
9288 if (!p->cr_seen) {
9289 p->cr_seen = TRUE;
9290 /* carried over with p->lex.nextline for nextc() */
9291 rb_warn0("encountered \\r in middle of line, treated as a mere space");
9292 }
9293 /* fall through */
9294 case ' ': case '\t': case '\f':
9295 case '\13': /* '\v' */
9296 space_seen = 1;
9297#ifdef RIPPER
9298 while ((c = nextc(p))) {
9299 switch (c) {
9300 case ' ': case '\t': case '\f': case '\r':
9301 case '\13': /* '\v' */
9302 break;
9303 default:
9304 goto outofloop;
9305 }
9306 }
9307 outofloop:
9308 pushback(p, c);
9309 dispatch_scan_event(p, tSP);
9310#endif
9311 goto retry;
9312
9313 case '#': /* it's a comment */
9314 p->token_seen = token_seen;
9315 /* no magic_comment in shebang line */
9316 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
9317 if (comment_at_top(p)) {
9318 set_file_encoding(p, p->lex.pcur, p->lex.pend);
9319 }
9320 }
9321 lex_goto_eol(p);
9322 dispatch_scan_event(p, tCOMMENT);
9323 fallthru = TRUE;
9324 /* fall through */
9325 case '\n':
9326 p->token_seen = token_seen;
9327 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
9328 !IS_lex_state(EXPR_LABELED));
9329 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
9330 if (!fallthru) {
9331 dispatch_scan_event(p, tIGNORED_NL);
9332 }
9333 fallthru = FALSE;
9334 if (!c && p->ctxt.in_kwarg) {
9335 goto normal_newline;
9336 }
9337 goto retry;
9338 }
9339 while (1) {
9340 switch (c = nextc(p)) {
9341 case ' ': case '\t': case '\f': case '\r':
9342 case '\13': /* '\v' */
9343 space_seen = 1;
9344 break;
9345 case '#':
9346 pushback(p, c);
9347 if (space_seen) dispatch_scan_event(p, tSP);
9348 goto retry;
9349 case '&':
9350 case '.': {
9351 dispatch_delayed_token(p, tIGNORED_NL);
9352 if (peek(p, '.') == (c == '&')) {
9353 pushback(p, c);
9354 dispatch_scan_event(p, tSP);
9355 goto retry;
9356 }
9357 }
9358 default:
9359 p->ruby_sourceline--;
9360 p->lex.nextline = p->lex.lastline;
9361 case -1: /* EOF no decrement*/
9362#ifndef RIPPER
9363 if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
9364 p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
9365 p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
9366 pushback(p, 1); /* always pushback */
9367 p->lex.ptok = p->lex.pcur;
9368#else
9369 lex_goto_eol(p);
9370 if (c != -1) {
9371 p->lex.ptok = p->lex.pcur;
9372 }
9373#endif
9374 goto normal_newline;
9375 }
9376 }
9377 normal_newline:
9378 p->command_start = TRUE;
9379 SET_LEX_STATE(EXPR_BEG);
9380 return '\n';
9381
9382 case '*':
9383 if ((c = nextc(p)) == '*') {
9384 if ((c = nextc(p)) == '=') {
9385 set_yylval_id(idPow);
9386 SET_LEX_STATE(EXPR_BEG);
9387 return tOP_ASGN;
9388 }
9389 pushback(p, c);
9390 if (IS_SPCARG(c)) {
9391 rb_warning0("`**' interpreted as argument prefix");
9392 c = tDSTAR;
9393 }
9394 else if (IS_BEG()) {
9395 c = tDSTAR;
9396 }
9397 else {
9398 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
9399 }
9400 }
9401 else {
9402 if (c == '=') {
9403 set_yylval_id('*');
9404 SET_LEX_STATE(EXPR_BEG);
9405 return tOP_ASGN;
9406 }
9407 pushback(p, c);
9408 if (IS_SPCARG(c)) {
9409 rb_warning0("`*' interpreted as argument prefix");
9410 c = tSTAR;
9411 }
9412 else if (IS_BEG()) {
9413 c = tSTAR;
9414 }
9415 else {
9416 c = warn_balanced('*', "*", "argument prefix");
9417 }
9418 }
9419 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9420 return c;
9421
9422 case '!':
9423 c = nextc(p);
9424 if (IS_AFTER_OPERATOR()) {
9425 SET_LEX_STATE(EXPR_ARG);
9426 if (c == '@') {
9427 return '!';
9428 }
9429 }
9430 else {
9431 SET_LEX_STATE(EXPR_BEG);
9432 }
9433 if (c == '=') {
9434 return tNEQ;
9435 }
9436 if (c == '~') {
9437 return tNMATCH;
9438 }
9439 pushback(p, c);
9440 return '!';
9441
9442 case '=':
9443 if (was_bol(p)) {
9444 /* skip embedded rd document */
9445 if (word_match_p(p, "begin", 5)) {
9446 int first_p = TRUE;
9447
9448 lex_goto_eol(p);
9449 dispatch_scan_event(p, tEMBDOC_BEG);
9450 for (;;) {
9451 lex_goto_eol(p);
9452 if (!first_p) {
9453 dispatch_scan_event(p, tEMBDOC);
9454 }
9455 first_p = FALSE;
9456 c = nextc(p);
9457 if (c == -1) {
9458 compile_error(p, "embedded document meets end of file");
9459 return 0;
9460 }
9461 if (c == '=' && word_match_p(p, "end", 3)) {
9462 break;
9463 }
9464 pushback(p, c);
9465 }
9466 lex_goto_eol(p);
9467 dispatch_scan_event(p, tEMBDOC_END);
9468 goto retry;
9469 }
9470 }
9471
9472 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9473 if ((c = nextc(p)) == '=') {
9474 if ((c = nextc(p)) == '=') {
9475 return tEQQ;
9476 }
9477 pushback(p, c);
9478 return tEQ;
9479 }
9480 if (c == '~') {
9481 return tMATCH;
9482 }
9483 else if (c == '>') {
9484 return tASSOC;
9485 }
9486 pushback(p, c);
9487 return '=';
9488
9489 case '<':
9490 c = nextc(p);
9491 if (c == '<' &&
9492 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9493 !IS_END() &&
9494 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9495 int token = heredoc_identifier(p);
9496 if (token) return token < 0 ? 0 : token;
9497 }
9498 if (IS_AFTER_OPERATOR()) {
9499 SET_LEX_STATE(EXPR_ARG);
9500 }
9501 else {
9502 if (IS_lex_state(EXPR_CLASS))
9503 p->command_start = TRUE;
9504 SET_LEX_STATE(EXPR_BEG);
9505 }
9506 if (c == '=') {
9507 if ((c = nextc(p)) == '>') {
9508 return tCMP;
9509 }
9510 pushback(p, c);
9511 return tLEQ;
9512 }
9513 if (c == '<') {
9514 if ((c = nextc(p)) == '=') {
9515 set_yylval_id(idLTLT);
9516 SET_LEX_STATE(EXPR_BEG);
9517 return tOP_ASGN;
9518 }
9519 pushback(p, c);
9520 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9521 }
9522 pushback(p, c);
9523 return '<';
9524
9525 case '>':
9526 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9527 if ((c = nextc(p)) == '=') {
9528 return tGEQ;
9529 }
9530 if (c == '>') {
9531 if ((c = nextc(p)) == '=') {
9532 set_yylval_id(idGTGT);
9533 SET_LEX_STATE(EXPR_BEG);
9534 return tOP_ASGN;
9535 }
9536 pushback(p, c);
9537 return tRSHFT;
9538 }
9539 pushback(p, c);
9540 return '>';
9541
9542 case '"':
9543 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9544 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9545 p->lex.ptok = p->lex.pcur-1;
9546 return tSTRING_BEG;
9547
9548 case '`':
9549 if (IS_lex_state(EXPR_FNAME)) {
9550 SET_LEX_STATE(EXPR_ENDFN);
9551 return c;
9552 }
9553 if (IS_lex_state(EXPR_DOT)) {
9554 if (cmd_state)
9555 SET_LEX_STATE(EXPR_CMDARG);
9556 else
9557 SET_LEX_STATE(EXPR_ARG);
9558 return c;
9559 }
9560 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9561 return tXSTRING_BEG;
9562
9563 case '\'':
9564 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9565 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9566 p->lex.ptok = p->lex.pcur-1;
9567 return tSTRING_BEG;
9568
9569 case '?':
9570 return parse_qmark(p, space_seen);
9571
9572 case '&':
9573 if ((c = nextc(p)) == '&') {
9574 SET_LEX_STATE(EXPR_BEG);
9575 if ((c = nextc(p)) == '=') {
9576 set_yylval_id(idANDOP);
9577 SET_LEX_STATE(EXPR_BEG);
9578 return tOP_ASGN;
9579 }
9580 pushback(p, c);
9581 return tANDOP;
9582 }
9583 else if (c == '=') {
9584 set_yylval_id('&');
9585 SET_LEX_STATE(EXPR_BEG);
9586 return tOP_ASGN;
9587 }
9588 else if (c == '.') {
9589 set_yylval_id(idANDDOT);
9590 SET_LEX_STATE(EXPR_DOT);
9591 return tANDDOT;
9592 }
9593 pushback(p, c);
9594 if (IS_SPCARG(c)) {
9595 if ((c != ':') ||
9596 (c = peekc_n(p, 1)) == -1 ||
9597 !(c == '\'' || c == '"' ||
9598 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9599 rb_warning0("`&' interpreted as argument prefix");
9600 }
9601 c = tAMPER;
9602 }
9603 else if (IS_BEG()) {
9604 c = tAMPER;
9605 }
9606 else {
9607 c = warn_balanced('&', "&", "argument prefix");
9608 }
9609 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9610 return c;
9611
9612 case '|':
9613 if ((c = nextc(p)) == '|') {
9614 SET_LEX_STATE(EXPR_BEG);
9615 if ((c = nextc(p)) == '=') {
9616 set_yylval_id(idOROP);
9617 SET_LEX_STATE(EXPR_BEG);
9618 return tOP_ASGN;
9619 }
9620 pushback(p, c);
9621 if (IS_lex_state_for(last_state, EXPR_BEG)) {
9622 c = '|';
9623 pushback(p, '|');
9624 return c;
9625 }
9626 return tOROP;
9627 }
9628 if (c == '=') {
9629 set_yylval_id('|');
9630 SET_LEX_STATE(EXPR_BEG);
9631 return tOP_ASGN;
9632 }
9633 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9634 pushback(p, c);
9635 return '|';
9636
9637 case '+':
9638 c = nextc(p);
9639 if (IS_AFTER_OPERATOR()) {
9640 SET_LEX_STATE(EXPR_ARG);
9641 if (c == '@') {
9642 return tUPLUS;
9643 }
9644 pushback(p, c);
9645 return '+';
9646 }
9647 if (c == '=') {
9648 set_yylval_id('+');
9649 SET_LEX_STATE(EXPR_BEG);
9650 return tOP_ASGN;
9651 }
9652 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9653 SET_LEX_STATE(EXPR_BEG);
9654 pushback(p, c);
9655 if (c != -1 && ISDIGIT(c)) {
9656 return parse_numeric(p, '+');
9657 }
9658 return tUPLUS;
9659 }
9660 SET_LEX_STATE(EXPR_BEG);
9661 pushback(p, c);
9662 return warn_balanced('+', "+", "unary operator");
9663
9664 case '-':
9665 c = nextc(p);
9666 if (IS_AFTER_OPERATOR()) {
9667 SET_LEX_STATE(EXPR_ARG);
9668 if (c == '@') {
9669 return tUMINUS;
9670 }
9671 pushback(p, c);
9672 return '-';
9673 }
9674 if (c == '=') {
9675 set_yylval_id('-');
9676 SET_LEX_STATE(EXPR_BEG);
9677 return tOP_ASGN;
9678 }
9679 if (c == '>') {
9680 SET_LEX_STATE(EXPR_ENDFN);
9681 return tLAMBDA;
9682 }
9683 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9684 SET_LEX_STATE(EXPR_BEG);
9685 pushback(p, c);
9686 if (c != -1 && ISDIGIT(c)) {
9687 return tUMINUS_NUM;
9688 }
9689 return tUMINUS;
9690 }
9691 SET_LEX_STATE(EXPR_BEG);
9692 pushback(p, c);
9693 return warn_balanced('-', "-", "unary operator");
9694
9695 case '.': {
9696 int is_beg = IS_BEG();
9697 SET_LEX_STATE(EXPR_BEG);
9698 if ((c = nextc(p)) == '.') {
9699 if ((c = nextc(p)) == '.') {
9700 if (p->ctxt.in_argdef) {
9701 SET_LEX_STATE(EXPR_ENDARG);
9702 return tBDOT3;
9703 }
9704 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9705 rb_warn0("... at EOL, should be parenthesized?");
9706 }
9707 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
9708 if (IS_lex_state_for(last_state, EXPR_LABEL))
9709 return tDOT3;
9710 }
9711 return is_beg ? tBDOT3 : tDOT3;
9712 }
9713 pushback(p, c);
9714 return is_beg ? tBDOT2 : tDOT2;
9715 }
9716 pushback(p, c);
9717 if (c != -1 && ISDIGIT(c)) {
9718 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9719 parse_numeric(p, '.');
9720 if (ISDIGIT(prev)) {
9721 yyerror0("unexpected fraction part after numeric literal");
9722 }
9723 else {
9724 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9725 }
9726 SET_LEX_STATE(EXPR_END);
9727 p->lex.ptok = p->lex.pcur;
9728 goto retry;
9729 }
9730 set_yylval_id('.');
9731 SET_LEX_STATE(EXPR_DOT);
9732 return '.';
9733 }
9734
9735 case '0': case '1': case '2': case '3': case '4':
9736 case '5': case '6': case '7': case '8': case '9':
9737 return parse_numeric(p, c);
9738
9739 case ')':
9740 COND_POP();
9741 CMDARG_POP();
9742 SET_LEX_STATE(EXPR_ENDFN);
9743 p->lex.paren_nest--;
9744 return c;
9745
9746 case ']':
9747 COND_POP();
9748 CMDARG_POP();
9749 SET_LEX_STATE(EXPR_END);
9750 p->lex.paren_nest--;
9751 return c;
9752
9753 case '}':
9754 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9755 if (!p->lex.brace_nest--) return tSTRING_DEND;
9756 COND_POP();
9757 CMDARG_POP();
9758 SET_LEX_STATE(EXPR_END);
9759 p->lex.paren_nest--;
9760 return c;
9761
9762 case ':':
9763 c = nextc(p);
9764 if (c == ':') {
9765 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9766 SET_LEX_STATE(EXPR_BEG);
9767 return tCOLON3;
9768 }
9769 set_yylval_id(idCOLON2);
9770 SET_LEX_STATE(EXPR_DOT);
9771 return tCOLON2;
9772 }
9773 if (IS_END() || ISSPACE(c) || c == '#') {
9774 pushback(p, c);
9775 c = warn_balanced(':', ":", "symbol literal");
9776 SET_LEX_STATE(EXPR_BEG);
9777 return c;
9778 }
9779 switch (c) {
9780 case '\'':
9781 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9782 break;
9783 case '"':
9784 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9785 break;
9786 default:
9787 pushback(p, c);
9788 break;
9789 }
9790 SET_LEX_STATE(EXPR_FNAME);
9791 return tSYMBEG;
9792
9793 case '/':
9794 if (IS_BEG()) {
9795 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9796 return tREGEXP_BEG;
9797 }
9798 if ((c = nextc(p)) == '=') {
9799 set_yylval_id('/');
9800 SET_LEX_STATE(EXPR_BEG);
9801 return tOP_ASGN;
9802 }
9803 pushback(p, c);
9804 if (IS_SPCARG(c)) {
9805 arg_ambiguous(p, '/');
9806 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9807 return tREGEXP_BEG;
9808 }
9809 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9810 return warn_balanced('/', "/", "regexp literal");
9811
9812 case '^':
9813 if ((c = nextc(p)) == '=') {
9814 set_yylval_id('^');
9815 SET_LEX_STATE(EXPR_BEG);
9816 return tOP_ASGN;
9817 }
9818 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9819 pushback(p, c);
9820 return '^';
9821
9822 case ';':
9823 SET_LEX_STATE(EXPR_BEG);
9824 p->command_start = TRUE;
9825 return ';';
9826
9827 case ',':
9828 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9829 return ',';
9830
9831 case '~':
9832 if (IS_AFTER_OPERATOR()) {
9833 if ((c = nextc(p)) != '@') {
9834 pushback(p, c);
9835 }
9836 SET_LEX_STATE(EXPR_ARG);
9837 }
9838 else {
9839 SET_LEX_STATE(EXPR_BEG);
9840 }
9841 return '~';
9842
9843 case '(':
9844 if (IS_BEG()) {
9845 c = tLPAREN;
9846 }
9847 else if (!space_seen) {
9848 /* foo( ... ) => method call, no ambiguity */
9849 }
9850 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9851 c = tLPAREN_ARG;
9852 }
9853 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9854 rb_warning0("parentheses after method name is interpreted as "
9855 "an argument list, not a decomposed argument");
9856 }
9857 p->lex.paren_nest++;
9858 COND_PUSH(0);
9859 CMDARG_PUSH(0);
9860 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9861 return c;
9862
9863 case '[':
9864 p->lex.paren_nest++;
9865 if (IS_AFTER_OPERATOR()) {
9866 if ((c = nextc(p)) == ']') {
9867 p->lex.paren_nest--;
9868 SET_LEX_STATE(EXPR_ARG);
9869 if ((c = nextc(p)) == '=') {
9870 return tASET;
9871 }
9872 pushback(p, c);
9873 return tAREF;
9874 }
9875 pushback(p, c);
9876 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9877 return '[';
9878 }
9879 else if (IS_BEG()) {
9880 c = tLBRACK;
9881 }
9882 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9883 c = tLBRACK;
9884 }
9885 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9886 COND_PUSH(0);
9887 CMDARG_PUSH(0);
9888 return c;
9889
9890 case '{':
9891 ++p->lex.brace_nest;
9892 if (lambda_beginning_p())
9893 c = tLAMBEG;
9894 else if (IS_lex_state(EXPR_LABELED))
9895 c = tLBRACE; /* hash */
9896 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9897 c = '{'; /* block (primary) */
9898 else if (IS_lex_state(EXPR_ENDARG))
9899 c = tLBRACE_ARG; /* block (expr) */
9900 else
9901 c = tLBRACE; /* hash */
9902 if (c != tLBRACE) {
9903 p->command_start = TRUE;
9904 SET_LEX_STATE(EXPR_BEG);
9905 }
9906 else {
9907 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9908 }
9909 ++p->lex.paren_nest; /* after lambda_beginning_p() */
9910 COND_PUSH(0);
9911 CMDARG_PUSH(0);
9912 return c;
9913
9914 case '\\':
9915 c = nextc(p);
9916 if (c == '\n') {
9917 space_seen = 1;
9918 dispatch_scan_event(p, tSP);
9919 goto retry; /* skip \\n */
9920 }
9921 if (c == ' ') return tSP;
9922 if (ISSPACE(c)) return c;
9923 pushback(p, c);
9924 return '\\';
9925
9926 case '%':
9927 return parse_percent(p, space_seen, last_state);
9928
9929 case '$':
9930 return parse_gvar(p, last_state);
9931
9932 case '@':
9933 return parse_atmark(p, last_state);
9934
9935 case '_':
9936 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9937 p->ruby__end__seen = 1;
9938 p->eofp = 1;
9939#ifndef RIPPER
9940 return -1;
9941#else
9942 lex_goto_eol(p);
9943 dispatch_scan_event(p, k__END__);
9944 return 0;
9945#endif
9946 }
9947 newtok(p);
9948 break;
9949
9950 default:
9951 if (!parser_is_identchar(p)) {
9952 compile_error(p, "Invalid char `\\x%02X' in expression", c);
9953 token_flush(p);
9954 goto retry;
9955 }
9956
9957 newtok(p);
9958 break;
9959 }
9960
9961 return parse_ident(p, c, cmd_state);
9962}
9963
9964static enum yytokentype
9965yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9966{
9967 enum yytokentype t;
9968
9969 p->lval = lval;
9970 lval->val = Qundef;
9971 t = parser_yylex(p);
9972
9973 if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9974 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9975 else
9976 RUBY_SET_YYLLOC(*yylloc);
9977
9978 if (has_delayed_token(p))
9979 dispatch_delayed_token(p, t);
9980 else if (t != 0)
9981 dispatch_scan_event(p, t);
9982
9983 return t;
9984}
9985
9986#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9987
9988static NODE*
9989node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9990{
9991 NODE *n = rb_ast_newnode(p->ast, type);
9992
9993 rb_node_init(n, type, a0, a1, a2);
9994
9995 nd_set_loc(n, loc);
9996 nd_set_node_id(n, parser_get_node_id(p));
9997 return n;
9998}
9999
10000static NODE *
10001nd_set_loc(NODE *nd, const YYLTYPE *loc)
10002{
10003 nd->nd_loc = *loc;
10004 nd_set_line(nd, loc->beg_pos.lineno);
10005 return nd;
10006}
10007
10008#ifndef RIPPER
10009static enum node_type
10010nodetype(NODE *node) /* for debug */
10011{
10012 return (enum node_type)nd_type(node);
10013}
10014
10015static int
10016nodeline(NODE *node)
10017{
10018 return nd_line(node);
10019}
10020
10021static NODE*
10022newline_node(NODE *node)
10023{
10024 if (node) {
10025 node = remove_begin(node);
10026 node->flags |= NODE_FL_NEWLINE;
10027 }
10028 return node;
10029}
10030
10031static void
10032fixpos(NODE *node, NODE *orig)
10033{
10034 if (!node) return;
10035 if (!orig) return;
10036 nd_set_line(node, nd_line(orig));
10037}
10038
10039static void
10040parser_warning(struct parser_params *p, NODE *node, const char *mesg)
10041{
10042 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10043}
10044
10045static void
10046parser_warn(struct parser_params *p, NODE *node, const char *mesg)
10047{
10048 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10049}
10050
10051static NODE*
10052block_append(struct parser_params *p, NODE *head, NODE *tail)
10053{
10054 NODE *end, *h = head, *nd;
10055
10056 if (tail == 0) return head;
10057
10058 if (h == 0) return tail;
10059 switch (nd_type(h)) {
10060 case NODE_LIT:
10061 case NODE_STR:
10062 case NODE_SELF:
10063 case NODE_TRUE:
10064 case NODE_FALSE:
10065 case NODE_NIL:
10066 parser_warning(p, h, "unused literal ignored");
10067 return tail;
10068 default:
10069 h = end = NEW_BLOCK(head, &head->nd_loc);
10070 end->nd_end = end;
10071 head = end;
10072 break;
10073 case NODE_BLOCK:
10074 end = h->nd_end;
10075 break;
10076 }
10077
10078 nd = end->nd_head;
10079 switch (nd_type(nd)) {
10080 case NODE_RETURN:
10081 case NODE_BREAK:
10082 case NODE_NEXT:
10083 case NODE_REDO:
10084 case NODE_RETRY:
10085 if (RTEST(ruby_verbose)) {
10086 parser_warning(p, tail, "statement not reached");
10087 }
10088 break;
10089
10090 default:
10091 break;
10092 }
10093
10094 if (!nd_type_p(tail, NODE_BLOCK)) {
10095 tail = NEW_BLOCK(tail, &tail->nd_loc);
10096 tail->nd_end = tail;
10097 }
10098 end->nd_next = tail;
10099 h->nd_end = tail->nd_end;
10100 nd_set_last_loc(head, nd_last_loc(tail));
10101 return head;
10102}
10103
10104/* append item to the list */
10105static NODE*
10106list_append(struct parser_params *p, NODE *list, NODE *item)
10107{
10108 NODE *last;
10109
10110 if (list == 0) return NEW_LIST(item, &item->nd_loc);
10111 if (list->nd_next) {
10112 last = list->nd_next->nd_end;
10113 }
10114 else {
10115 last = list;
10116 }
10117
10118 list->nd_alen += 1;
10119 last->nd_next = NEW_LIST(item, &item->nd_loc);
10120 list->nd_next->nd_end = last->nd_next;
10121
10122 nd_set_last_loc(list, nd_last_loc(item));
10123
10124 return list;
10125}
10126
10127/* concat two lists */
10128static NODE*
10129list_concat(NODE *head, NODE *tail)
10130{
10131 NODE *last;
10132
10133 if (head->nd_next) {
10134 last = head->nd_next->nd_end;
10135 }
10136 else {
10137 last = head;
10138 }
10139
10140 head->nd_alen += tail->nd_alen;
10141 last->nd_next = tail;
10142 if (tail->nd_next) {
10143 head->nd_next->nd_end = tail->nd_next->nd_end;
10144 }
10145 else {
10146 head->nd_next->nd_end = tail;
10147 }
10148
10149 nd_set_last_loc(head, nd_last_loc(tail));
10150
10151 return head;
10152}
10153
10154static int
10155literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
10156{
10157 if (NIL_P(tail)) return 1;
10158 if (!rb_enc_compatible(head, tail)) {
10159 compile_error(p, "string literal encodings differ (%s / %s)",
10160 rb_enc_name(rb_enc_get(head)),
10161 rb_enc_name(rb_enc_get(tail)));
10162 rb_str_resize(head, 0);
10163 rb_str_resize(tail, 0);
10164 return 0;
10165 }
10166 rb_str_buf_append(head, tail);
10167 return 1;
10168}
10169
10170static VALUE
10171string_literal_head(enum node_type htype, NODE *head)
10172{
10173 if (htype != NODE_DSTR) return Qfalse;
10174 if (head->nd_next) {
10175 head = head->nd_next->nd_end->nd_head;
10176 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
10177 }
10178 const VALUE lit = head->nd_lit;
10179 ASSUME(lit != Qfalse);
10180 return lit;
10181}
10182
10183/* concat two string literals */
10184static NODE *
10185literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
10186{
10187 enum node_type htype;
10188 VALUE lit;
10189
10190 if (!head) return tail;
10191 if (!tail) return head;
10192
10193 htype = nd_type(head);
10194 if (htype == NODE_EVSTR) {
10195 head = new_dstr(p, head, loc);
10196 htype = NODE_DSTR;
10197 }
10198 if (p->heredoc_indent > 0) {
10199 switch (htype) {
10200 case NODE_STR:
10201 nd_set_type(head, NODE_DSTR);
10202 case NODE_DSTR:
10203 return list_append(p, head, tail);
10204 default:
10205 break;
10206 }
10207 }
10208 switch (nd_type(tail)) {
10209 case NODE_STR:
10210 if ((lit = string_literal_head(htype, head)) != Qfalse) {
10211 htype = NODE_STR;
10212 }
10213 else {
10214 lit = head->nd_lit;
10215 }
10216 if (htype == NODE_STR) {
10217 if (!literal_concat0(p, lit, tail->nd_lit)) {
10218 error:
10219 rb_discard_node(p, head);
10220 rb_discard_node(p, tail);
10221 return 0;
10222 }
10223 rb_discard_node(p, tail);
10224 }
10225 else {
10226 list_append(p, head, tail);
10227 }
10228 break;
10229
10230 case NODE_DSTR:
10231 if (htype == NODE_STR) {
10232 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
10233 goto error;
10234 tail->nd_lit = head->nd_lit;
10235 rb_discard_node(p, head);
10236 head = tail;
10237 }
10238 else if (NIL_P(tail->nd_lit)) {
10239 append:
10240 head->nd_alen += tail->nd_alen - 1;
10241 if (!head->nd_next) {
10242 head->nd_next = tail->nd_next;
10243 }
10244 else if (tail->nd_next) {
10245 head->nd_next->nd_end->nd_next = tail->nd_next;
10246 head->nd_next->nd_end = tail->nd_next->nd_end;
10247 }
10248 rb_discard_node(p, tail);
10249 }
10250 else if ((lit = string_literal_head(htype, head)) != Qfalse) {
10251 if (!literal_concat0(p, lit, tail->nd_lit))
10252 goto error;
10253 tail->nd_lit = Qnil;
10254 goto append;
10255 }
10256 else {
10257 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
10258 }
10259 break;
10260
10261 case NODE_EVSTR:
10262 if (htype == NODE_STR) {
10263 nd_set_type(head, NODE_DSTR);
10264 head->nd_alen = 1;
10265 }
10266 list_append(p, head, tail);
10267 break;
10268 }
10269 return head;
10270}
10271
10272static NODE *
10273evstr2dstr(struct parser_params *p, NODE *node)
10274{
10275 if (nd_type_p(node, NODE_EVSTR)) {
10276 node = new_dstr(p, node, &node->nd_loc);
10277 }
10278 return node;
10279}
10280
10281static NODE *
10282new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10283{
10284 NODE *head = node;
10285
10286 if (node) {
10287 switch (nd_type(node)) {
10288 case NODE_STR:
10289 nd_set_type(node, NODE_DSTR);
10290 return node;
10291 case NODE_DSTR:
10292 break;
10293 case NODE_EVSTR:
10294 return node;
10295 }
10296 }
10297 return NEW_EVSTR(head, loc);
10298}
10299
10300static NODE *
10301new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10302{
10303 VALUE lit = STR_NEW0();
10304 NODE *dstr = NEW_DSTR(lit, loc);
10305 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10306 return list_append(p, dstr, node);
10307}
10308
10309static NODE *
10310call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
10311 const YYLTYPE *op_loc, const YYLTYPE *loc)
10312{
10313 NODE *expr;
10314 value_expr(recv);
10315 value_expr(arg1);
10316 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
10317 nd_set_line(expr, op_loc->beg_pos.lineno);
10318 return expr;
10319}
10320
10321static NODE *
10322call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
10323{
10324 NODE *opcall;
10325 value_expr(recv);
10326 opcall = NEW_OPCALL(recv, id, 0, loc);
10327 nd_set_line(opcall, op_loc->beg_pos.lineno);
10328 return opcall;
10329}
10330
10331static NODE *
10332new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
10333{
10334 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
10335 nd_set_line(qcall, op_loc->beg_pos.lineno);
10336 return qcall;
10337}
10338
10339static NODE*
10340new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
10341{
10342 NODE *ret;
10343 if (block) block_dup_check(p, args, block);
10344 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
10345 if (block) ret = method_add_block(p, ret, block, loc);
10346 fixpos(ret, recv);
10347 return ret;
10348}
10349
10350#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? (node)->nd_body : node)
10351static NODE*
10352match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
10353{
10354 NODE *n;
10355 int line = op_loc->beg_pos.lineno;
10356
10357 value_expr(node1);
10358 value_expr(node2);
10359 if (node1 && (n = nd_once_body(node1)) != 0) {
10360 switch (nd_type(n)) {
10361 case NODE_DREGX:
10362 {
10363 NODE *match = NEW_MATCH2(node1, node2, loc);
10364 nd_set_line(match, line);
10365 return match;
10366 }
10367
10368 case NODE_LIT:
10369 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
10370 const VALUE lit = n->nd_lit;
10371 NODE *match = NEW_MATCH2(node1, node2, loc);
10372 match->nd_args = reg_named_capture_assign(p, lit, loc);
10373 nd_set_line(match, line);
10374 return match;
10375 }
10376 }
10377 }
10378
10379 if (node2 && (n = nd_once_body(node2)) != 0) {
10380 NODE *match3;
10381
10382 switch (nd_type(n)) {
10383 case NODE_LIT:
10384 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
10385 /* fallthru */
10386 case NODE_DREGX:
10387 match3 = NEW_MATCH3(node2, node1, loc);
10388 return match3;
10389 }
10390 }
10391
10392 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
10393 nd_set_line(n, line);
10394 return n;
10395}
10396
10397# if WARN_PAST_SCOPE
10398static int
10399past_dvar_p(struct parser_params *p, ID id)
10400{
10401 struct vtable *past = p->lvtbl->past;
10402 while (past) {
10403 if (vtable_included(past, id)) return 1;
10404 past = past->prev;
10405 }
10406 return 0;
10407}
10408# endif
10409
10410static int
10411numparam_nested_p(struct parser_params *p)
10412{
10413 struct local_vars *local = p->lvtbl;
10414 NODE *outer = local->numparam.outer;
10415 NODE *inner = local->numparam.inner;
10416 if (outer || inner) {
10417 NODE *used = outer ? outer : inner;
10418 compile_error(p, "numbered parameter is already used in\n"
10419 "%s:%d: %s block here",
10420 p->ruby_sourcefile, nd_line(used),
10421 outer ? "outer" : "inner");
10422 parser_show_error_line(p, &used->nd_loc);
10423 return 1;
10424 }
10425 return 0;
10426}
10427
10428static NODE*
10429gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
10430{
10431 ID *vidp = NULL;
10432 NODE *node;
10433 switch (id) {
10434 case keyword_self:
10435 return NEW_SELF(loc);
10436 case keyword_nil:
10437 return NEW_NIL(loc);
10438 case keyword_true:
10439 return NEW_TRUE(loc);
10440 case keyword_false:
10441 return NEW_FALSE(loc);
10442 case keyword__FILE__:
10443 {
10444 VALUE file = p->ruby_sourcefile_string;
10445 if (NIL_P(file))
10446 file = rb_str_new(0, 0);
10447 else
10448 file = rb_str_dup(file);
10449 node = NEW_STR(file, loc);
10450 RB_OBJ_WRITTEN(p->ast, Qnil, file);
10451 }
10452 return node;
10453 case keyword__LINE__:
10454 return NEW_LIT(INT2FIX(p->tokline), loc);
10455 case keyword__ENCODING__:
10456 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
10457 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10458 return node;
10459
10460 }
10461 switch (id_type(id)) {
10462 case ID_LOCAL:
10463 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
10464 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
10465 if (id == p->cur_arg) {
10466 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10467 return 0;
10468 }
10469 if (vidp) *vidp |= LVAR_USED;
10470 node = NEW_DVAR(id, loc);
10471 return node;
10472 }
10473 if (local_id_ref(p, id, &vidp)) {
10474 if (id == p->cur_arg) {
10475 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10476 return 0;
10477 }
10478 if (vidp) *vidp |= LVAR_USED;
10479 node = NEW_LVAR(id, loc);
10480 return node;
10481 }
10482 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
10483 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
10484 if (numparam_nested_p(p)) return 0;
10485 node = NEW_DVAR(id, loc);
10486 struct local_vars *local = p->lvtbl;
10487 if (!local->numparam.current) local->numparam.current = node;
10488 return node;
10489 }
10490# if WARN_PAST_SCOPE
10491 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10492 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10493 }
10494# endif
10495 /* method call without arguments */
10496 return NEW_VCALL(id, loc);
10497 case ID_GLOBAL:
10498 return NEW_GVAR(id, loc);
10499 case ID_INSTANCE:
10500 return NEW_IVAR(id, loc);
10501 case ID_CONST:
10502 return NEW_CONST(id, loc);
10503 case ID_CLASS:
10504 return NEW_CVAR(id, loc);
10505 }
10506 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10507 return 0;
10508}
10509
10510static NODE *
10511opt_arg_append(NODE *opt_list, NODE *opt)
10512{
10513 NODE *opts = opt_list;
10514 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10515
10516 while (opts->nd_next) {
10517 opts = opts->nd_next;
10518 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10519 }
10520 opts->nd_next = opt;
10521
10522 return opt_list;
10523}
10524
10525static NODE *
10526kwd_append(NODE *kwlist, NODE *kw)
10527{
10528 if (kwlist) {
10529 NODE *kws = kwlist;
10530 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10531 while (kws->nd_next) {
10532 kws = kws->nd_next;
10533 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10534 }
10535 kws->nd_next = kw;
10536 }
10537 return kwlist;
10538}
10539
10540static NODE *
10541new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10542{
10543 return NEW_DEFINED(remove_begin_all(expr), loc);
10544}
10545
10546static NODE*
10547symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10548{
10549 enum node_type type = nd_type(symbol);
10550 switch (type) {
10551 case NODE_DSTR:
10552 nd_set_type(symbol, NODE_DSYM);
10553 break;
10554 case NODE_STR:
10555 nd_set_type(symbol, NODE_LIT);
10556 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10557 break;
10558 default:
10559 compile_error(p, "unexpected node as symbol: %s", ruby_node_name(type));
10560 }
10561 return list_append(p, symbols, symbol);
10562}
10563
10564static NODE *
10565new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10566{
10567 NODE *list, *prev;
10568 VALUE lit;
10569
10570 if (!node) {
10571 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10572 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10573 return node;
10574 }
10575 switch (nd_type(node)) {
10576 case NODE_STR:
10577 {
10578 VALUE src = node->nd_lit;
10579 nd_set_type(node, NODE_LIT);
10580 nd_set_loc(node, loc);
10581 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10582 }
10583 break;
10584 default:
10585 lit = STR_NEW0();
10586 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10587 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10588 /* fall through */
10589 case NODE_DSTR:
10590 nd_set_type(node, NODE_DREGX);
10591 nd_set_loc(node, loc);
10592 node->nd_cflag = options & RE_OPTION_MASK;
10593 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10594 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10595 NODE *frag = list->nd_head;
10596 enum node_type type = nd_type(frag);
10597 if (type == NODE_STR || (type == NODE_DSTR && !frag->nd_next)) {
10598 VALUE tail = frag->nd_lit;
10599 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10600 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10601 if (!literal_concat0(p, lit, tail)) {
10602 return NEW_NIL(loc); /* dummy node on error */
10603 }
10604 rb_str_resize(tail, 0);
10605 prev->nd_next = list->nd_next;
10606 rb_discard_node(p, list->nd_head);
10607 rb_discard_node(p, list);
10608 list = prev;
10609 }
10610 else {
10611 prev = list;
10612 }
10613 }
10614 else {
10615 prev = 0;
10616 }
10617 }
10618 if (!node->nd_next) {
10619 VALUE src = node->nd_lit;
10620 nd_set_type(node, NODE_LIT);
10621 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10622 }
10623 if (options & RE_OPTION_ONCE) {
10624 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10625 }
10626 break;
10627 }
10628 return node;
10629}
10630
10631static NODE *
10632new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10633{
10634 if (!k) return 0;
10635 return NEW_KW_ARG(0, (k), loc);
10636}
10637
10638static NODE *
10639new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10640{
10641 if (!node) {
10642 VALUE lit = STR_NEW0();
10643 NODE *xstr = NEW_XSTR(lit, loc);
10644 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10645 return xstr;
10646 }
10647 switch (nd_type(node)) {
10648 case NODE_STR:
10649 nd_set_type(node, NODE_XSTR);
10650 nd_set_loc(node, loc);
10651 break;
10652 case NODE_DSTR:
10653 nd_set_type(node, NODE_DXSTR);
10654 nd_set_loc(node, loc);
10655 break;
10656 default:
10657 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10658 break;
10659 }
10660 return node;
10661}
10662
10663static void
10664check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10665{
10666 VALUE lit;
10667
10668 if (!arg || !p->case_labels) return;
10669
10670 lit = rb_node_case_when_optimizable_literal(arg);
10671 if (lit == Qundef) return;
10672 if (nd_type_p(arg, NODE_STR)) {
10673 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10674 }
10675
10676 if (NIL_P(p->case_labels)) {
10677 p->case_labels = rb_obj_hide(rb_hash_new());
10678 }
10679 else {
10680 VALUE line = rb_hash_lookup(p->case_labels, lit);
10681 if (!NIL_P(line)) {
10682 rb_warning1("duplicated `when' clause with line %d is ignored",
10683 WARN_IVAL(line));
10684 return;
10685 }
10686 }
10687 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10688}
10689
10690#else /* !RIPPER */
10691static int
10692id_is_var(struct parser_params *p, ID id)
10693{
10694 if (is_notop_id(id)) {
10695 switch (id & ID_SCOPE_MASK) {
10696 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10697 return 1;
10698 case ID_LOCAL:
10699 if (dyna_in_block(p)) {
10700 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10701 }
10702 if (local_id(p, id)) return 1;
10703 /* method call without arguments */
10704 return 0;
10705 }
10706 }
10707 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10708 return 0;
10709}
10710
10711static VALUE
10712new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10713{
10714 VALUE src = 0, err;
10715 int options = 0;
10716 if (ripper_is_node_yylval(re)) {
10717 src = RNODE(re)->nd_cval;
10718 re = RNODE(re)->nd_rval;
10719 }
10720 if (ripper_is_node_yylval(opt)) {
10721 options = (int)RNODE(opt)->nd_tag;
10722 opt = RNODE(opt)->nd_rval;
10723 }
10724 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10725 compile_error(p, "%"PRIsVALUE, err);
10726 }
10727 return dispatch2(regexp_literal, re, opt);
10728}
10729#endif /* !RIPPER */
10730
10731static inline enum lex_state_e
10732parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
10733{
10734 if (p->debug) {
10735 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
10736 }
10737 return p->lex.state = ls;
10738}
10739
10740#ifndef RIPPER
10741static const char rb_parser_lex_state_names[][8] = {
10742 "BEG", "END", "ENDARG", "ENDFN", "ARG",
10743 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10744 "LABEL", "LABELED","FITEM",
10745};
10746
10747static VALUE
10748append_lex_state_name(enum lex_state_e state, VALUE buf)
10749{
10750 int i, sep = 0;
10751 unsigned int mask = 1;
10752 static const char none[] = "NONE";
10753
10754 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10755 if ((unsigned)state & mask) {
10756 if (sep) {
10757 rb_str_cat(buf, "|", 1);
10758 }
10759 sep = 1;
10760 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10761 }
10762 }
10763 if (!sep) {
10764 rb_str_cat(buf, none, sizeof(none)-1);
10765 }
10766 return buf;
10767}
10768
10769static void
10770flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10771{
10772 VALUE mesg = p->debug_buffer;
10773
10774 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10775 p->debug_buffer = Qnil;
10776 rb_io_puts(1, &mesg, out);
10777 }
10778 if (!NIL_P(str) && RSTRING_LEN(str)) {
10779 rb_io_write(p->debug_output, str);
10780 }
10781}
10782
10783enum lex_state_e
10784rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10785 enum lex_state_e to, int line)
10786{
10787 VALUE mesg;
10788 mesg = rb_str_new_cstr("lex_state: ");
10789 append_lex_state_name(from, mesg);
10790 rb_str_cat_cstr(mesg, " -> ");
10791 append_lex_state_name(to, mesg);
10792 rb_str_catf(mesg, " at line %d\n", line);
10793 flush_debug_buffer(p, p->debug_output, mesg);
10794 return to;
10795}
10796
10797VALUE
10798rb_parser_lex_state_name(enum lex_state_e state)
10799{
10800 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10801}
10802
10803static void
10804append_bitstack_value(stack_type stack, VALUE mesg)
10805{
10806 if (stack == 0) {
10807 rb_str_cat_cstr(mesg, "0");
10808 }
10809 else {
10810 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10811 for (; mask && !(stack & mask); mask >>= 1) continue;
10812 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10813 }
10814}
10815
10816void
10817rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10818 const char *name, int line)
10819{
10820 VALUE mesg = rb_sprintf("%s: ", name);
10821 append_bitstack_value(stack, mesg);
10822 rb_str_catf(mesg, " at line %d\n", line);
10823 flush_debug_buffer(p, p->debug_output, mesg);
10824}
10825
10826void
10827rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10828{
10829 va_list ap;
10830 VALUE mesg = rb_str_new_cstr("internal parser error: ");
10831
10832 va_start(ap, fmt);
10833 rb_str_vcatf(mesg, fmt, ap);
10834 va_end(ap);
10835 yyerror0(RSTRING_PTR(mesg));
10836 RB_GC_GUARD(mesg);
10837
10838 mesg = rb_str_new(0, 0);
10839 append_lex_state_name(p->lex.state, mesg);
10840 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10841 rb_str_resize(mesg, 0);
10842 append_bitstack_value(p->cond_stack, mesg);
10843 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10844 rb_str_resize(mesg, 0);
10845 append_bitstack_value(p->cmdarg_stack, mesg);
10846 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10847 if (p->debug_output == rb_ractor_stdout())
10848 p->debug_output = rb_ractor_stderr();
10849 p->debug = TRUE;
10850}
10851
10852static YYLTYPE *
10853rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
10854{
10855 yylloc->beg_pos.lineno = sourceline;
10856 yylloc->beg_pos.column = beg_pos;
10857 yylloc->end_pos.lineno = sourceline;
10858 yylloc->end_pos.column = end_pos;
10859 return yylloc;
10860}
10861
10862YYLTYPE *
10863rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10864{
10865 int sourceline = here->sourceline;
10866 int beg_pos = (int)here->offset - here->quote
10867 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10868 int end_pos = (int)here->offset + here->length + here->quote;
10869
10870 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
10871}
10872
10873YYLTYPE *
10874rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10875{
10876 int sourceline = p->ruby_sourceline;
10877 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10878 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
10879 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
10880}
10881
10882YYLTYPE *
10883rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10884{
10885 int sourceline = p->ruby_sourceline;
10886 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10887 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
10888 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
10889}
10890#endif /* !RIPPER */
10891
10892static int
10893assignable0(struct parser_params *p, ID id, const char **err)
10894{
10895 if (!id) return -1;
10896 switch (id) {
10897 case keyword_self:
10898 *err = "Can't change the value of self";
10899 return -1;
10900 case keyword_nil:
10901 *err = "Can't assign to nil";
10902 return -1;
10903 case keyword_true:
10904 *err = "Can't assign to true";
10905 return -1;
10906 case keyword_false:
10907 *err = "Can't assign to false";
10908 return -1;
10909 case keyword__FILE__:
10910 *err = "Can't assign to __FILE__";
10911 return -1;
10912 case keyword__LINE__:
10913 *err = "Can't assign to __LINE__";
10914 return -1;
10915 case keyword__ENCODING__:
10916 *err = "Can't assign to __ENCODING__";
10917 return -1;
10918 }
10919 switch (id_type(id)) {
10920 case ID_LOCAL:
10921 if (dyna_in_block(p)) {
10922 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10923 compile_error(p, "Can't assign to numbered parameter _%d",
10924 NUMPARAM_ID_TO_IDX(id));
10925 return -1;
10926 }
10927 if (dvar_curr(p, id)) return NODE_DASGN;
10928 if (dvar_defined(p, id)) return NODE_DASGN;
10929 if (local_id(p, id)) return NODE_LASGN;
10930 dyna_var(p, id);
10931 return NODE_DASGN;
10932 }
10933 else {
10934 if (!local_id(p, id)) local_var(p, id);
10935 return NODE_LASGN;
10936 }
10937 break;
10938 case ID_GLOBAL: return NODE_GASGN;
10939 case ID_INSTANCE: return NODE_IASGN;
10940 case ID_CONST:
10941 if (!p->ctxt.in_def) return NODE_CDECL;
10942 *err = "dynamic constant assignment";
10943 return -1;
10944 case ID_CLASS: return NODE_CVASGN;
10945 default:
10946 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10947 }
10948 return -1;
10949}
10950
10951#ifndef RIPPER
10952static NODE*
10953assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10954{
10955 const char *err = 0;
10956 int node_type = assignable0(p, id, &err);
10957 switch (node_type) {
10958 case NODE_DASGN: return NEW_DASGN(id, val, loc);
10959 case NODE_LASGN: return NEW_LASGN(id, val, loc);
10960 case NODE_GASGN: return NEW_GASGN(id, val, loc);
10961 case NODE_IASGN: return NEW_IASGN(id, val, loc);
10962 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10963 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10964 }
10965 if (err) yyerror1(loc, err);
10966 return NEW_BEGIN(0, loc);
10967}
10968#else
10969static VALUE
10970assignable(struct parser_params *p, VALUE lhs)
10971{
10972 const char *err = 0;
10973 assignable0(p, get_id(lhs), &err);
10974 if (err) lhs = assign_error(p, err, lhs);
10975 return lhs;
10976}
10977#endif
10978
10979static int
10980is_private_local_id(ID name)
10981{
10982 VALUE s;
10983 if (name == idUScore) return 1;
10984 if (!is_local_id(name)) return 0;
10985 s = rb_id2str(name);
10986 if (!s) return 0;
10987 return RSTRING_PTR(s)[0] == '_';
10988}
10989
10990static int
10991shadowing_lvar_0(struct parser_params *p, ID name)
10992{
10993 if (dyna_in_block(p)) {
10994 if (dvar_curr(p, name)) {
10995 if (is_private_local_id(name)) return 1;
10996 yyerror0("duplicated argument name");
10997 }
10998 else if (dvar_defined(p, name) || local_id(p, name)) {
10999 vtable_add(p->lvtbl->vars, name);
11000 if (p->lvtbl->used) {
11001 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
11002 }
11003 return 0;
11004 }
11005 }
11006 else {
11007 if (local_id(p, name)) {
11008 if (is_private_local_id(name)) return 1;
11009 yyerror0("duplicated argument name");
11010 }
11011 }
11012 return 1;
11013}
11014
11015static ID
11016shadowing_lvar(struct parser_params *p, ID name)
11017{
11018 shadowing_lvar_0(p, name);
11019 return name;
11020}
11021
11022static void
11023new_bv(struct parser_params *p, ID name)
11024{
11025 if (!name) return;
11026 if (!is_local_id(name)) {
11027 compile_error(p, "invalid local variable - %"PRIsVALUE,
11028 rb_id2str(name));
11029 return;
11030 }
11031 if (!shadowing_lvar_0(p, name)) return;
11032 dyna_var(p, name);
11033}
11034
11035#ifndef RIPPER
11036static NODE *
11037aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
11038{
11039 return NEW_ATTRASGN(recv, tASET, idx, loc);
11040}
11041
11042static void
11043block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
11044{
11045 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
11046 compile_error(p, "both block arg and actual block given");
11047 }
11048}
11049
11050static NODE *
11051attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
11052{
11053 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
11054 return NEW_ATTRASGN(recv, id, 0, loc);
11055}
11056
11057static void
11058rb_backref_error(struct parser_params *p, NODE *node)
11059{
11060 switch (nd_type(node)) {
11061 case NODE_NTH_REF:
11062 compile_error(p, "Can't set variable $%ld", node->nd_nth);
11063 break;
11064 case NODE_BACK_REF:
11065 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
11066 break;
11067 }
11068}
11069#else
11070static VALUE
11071backref_error(struct parser_params *p, NODE *ref, VALUE expr)
11072{
11073 VALUE mesg = rb_str_new_cstr("Can't set variable ");
11074 rb_str_append(mesg, ref->nd_cval);
11075 return dispatch2(assign_error, mesg, expr);
11076}
11077#endif
11078
11079#ifndef RIPPER
11080static NODE *
11081arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11082{
11083 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
11084 switch (nd_type(node1)) {
11085 case NODE_LIST:
11086 return list_append(p, node1, node2);
11087 case NODE_BLOCK_PASS:
11088 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
11089 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
11090 return node1;
11091 case NODE_ARGSPUSH:
11092 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
11093 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11094 nd_set_type(node1, NODE_ARGSCAT);
11095 return node1;
11096 case NODE_ARGSCAT:
11097 if (!nd_type_p(node1->nd_body, NODE_LIST)) break;
11098 node1->nd_body = list_append(p, node1->nd_body, node2);
11099 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11100 return node1;
11101 }
11102 return NEW_ARGSPUSH(node1, node2, loc);
11103}
11104
11105static NODE *
11106arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11107{
11108 if (!node2) return node1;
11109 switch (nd_type(node1)) {
11110 case NODE_BLOCK_PASS:
11111 if (node1->nd_head)
11112 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
11113 else
11114 node1->nd_head = NEW_LIST(node2, loc);
11115 return node1;
11116 case NODE_ARGSPUSH:
11117 if (!nd_type_p(node2, NODE_LIST)) break;
11118 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
11119 nd_set_type(node1, NODE_ARGSCAT);
11120 return node1;
11121 case NODE_ARGSCAT:
11122 if (!nd_type_p(node2, NODE_LIST) ||
11123 !nd_type_p(node1->nd_body, NODE_LIST)) break;
11124 node1->nd_body = list_concat(node1->nd_body, node2);
11125 return node1;
11126 }
11127 return NEW_ARGSCAT(node1, node2, loc);
11128}
11129
11130static NODE *
11131last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
11132{
11133 NODE *n1;
11134 if ((n1 = splat_array(args)) != 0) {
11135 return list_append(p, n1, last_arg);
11136 }
11137 return arg_append(p, args, last_arg, loc);
11138}
11139
11140static NODE *
11141rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
11142{
11143 NODE *n1;
11144 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
11145 return list_concat(n1, rest_arg);
11146 }
11147 return arg_concat(p, args, rest_arg, loc);
11148}
11149
11150static NODE *
11151splat_array(NODE* node)
11152{
11153 if (nd_type_p(node, NODE_SPLAT)) node = node->nd_head;
11154 if (nd_type_p(node, NODE_LIST)) return node;
11155 return 0;
11156}
11157
11158static void
11159mark_lvar_used(struct parser_params *p, NODE *rhs)
11160{
11161 ID *vidp = NULL;
11162 if (!rhs) return;
11163 switch (nd_type(rhs)) {
11164 case NODE_LASGN:
11165 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
11166 if (vidp) *vidp |= LVAR_USED;
11167 }
11168 break;
11169 case NODE_DASGN:
11170 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
11171 if (vidp) *vidp |= LVAR_USED;
11172 }
11173 break;
11174#if 0
11175 case NODE_MASGN:
11176 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
11177 mark_lvar_used(p, rhs->nd_head);
11178 }
11179 break;
11180#endif
11181 }
11182}
11183
11184static NODE *
11185const_decl_path(struct parser_params *p, NODE **dest)
11186{
11187 NODE *n = *dest;
11188 if (!nd_type_p(n, NODE_CALL)) {
11189 const YYLTYPE *loc = &n->nd_loc;
11190 VALUE path;
11191 if (n->nd_vid) {
11192 path = rb_id2str(n->nd_vid);
11193 }
11194 else {
11195 n = n->nd_else;
11196 path = rb_ary_new();
11197 for (; n && nd_type_p(n, NODE_COLON2); n = n->nd_head) {
11198 rb_ary_push(path, rb_id2str(n->nd_mid));
11199 }
11200 if (n && nd_type_p(n, NODE_CONST)) {
11201 // Const::Name
11202 rb_ary_push(path, rb_id2str(n->nd_vid));
11203 }
11204 else if (n && nd_type_p(n, NODE_COLON3)) {
11205 // ::Const::Name
11206 rb_ary_push(path, rb_str_new(0, 0));
11207 }
11208 else {
11209 // expression::Name
11210 rb_ary_push(path, rb_str_new_cstr("..."));
11211 }
11212 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
11213 path = rb_fstring(path);
11214 }
11215 *dest = n = NEW_LIT(path, loc);
11216 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11217 }
11218 return n;
11219}
11220
11221extern VALUE rb_mRubyVMFrozenCore;
11222
11223static NODE *
11224make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
11225{
11226 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11227
11228 if (copy) {
11229 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
11230 NEW_LIST(value, loc), loc);
11231 }
11232 else {
11233 return NEW_CALL(fcore, rb_intern("make_shareable"),
11234 NEW_LIST(value, loc), loc);
11235 }
11236}
11237
11238static NODE *
11239ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
11240{
11241 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11242 NODE *args = NEW_LIST(value, loc);
11243 args = list_append(p, args, const_decl_path(p, dest));
11244 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
11245}
11246
11247static int is_static_content(NODE *node);
11248
11249static VALUE
11250shareable_literal_value(NODE *node)
11251{
11252 if (!node) return Qnil;
11253 enum node_type type = nd_type(node);
11254 switch (type) {
11255 case NODE_TRUE:
11256 return Qtrue;
11257 case NODE_FALSE:
11258 return Qfalse;
11259 case NODE_NIL:
11260 return Qnil;
11261 case NODE_LIT:
11262 return node->nd_lit;
11263 default:
11264 return Qundef;
11265 }
11266}
11267
11268#ifndef SHAREABLE_BARE_EXPRESSION
11269#define SHAREABLE_BARE_EXPRESSION 1
11270#endif
11271
11272static NODE *
11273shareable_literal_constant(struct parser_params *p, enum shareability shareable,
11274 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
11275{
11276# define shareable_literal_constant_next(n) \
11277 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
11278 VALUE lit = Qnil;
11279
11280 if (!value) return 0;
11281 enum node_type type = nd_type(value);
11282 switch (type) {
11283 case NODE_TRUE:
11284 case NODE_FALSE:
11285 case NODE_NIL:
11286 case NODE_LIT:
11287 return value;
11288
11289 case NODE_DSTR:
11290 if (shareable == shareable_literal) {
11291 value = NEW_CALL(value, idUMinus, 0, loc);
11292 }
11293 return value;
11294
11295 case NODE_STR:
11296 lit = rb_fstring(value->nd_lit);
11297 nd_set_type(value, NODE_LIT);
11298 RB_OBJ_WRITE(p->ast, &value->nd_lit, lit);
11299 return value;
11300
11301 case NODE_ZLIST:
11302 lit = rb_ary_new();
11303 OBJ_FREEZE_RAW(lit);
11304 NODE *n = NEW_LIT(lit, loc);
11305 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11306 return n;
11307
11308 case NODE_LIST:
11309 lit = rb_ary_new();
11310 for (NODE *n = value; n; n = n->nd_next) {
11311 NODE *elt = n->nd_head;
11312 if (elt) {
11313 elt = shareable_literal_constant_next(elt);
11314 if (elt) {
11315 n->nd_head = elt;
11316 }
11317 else if (RTEST(lit)) {
11318 rb_ary_clear(lit);
11319 lit = Qfalse;
11320 }
11321 }
11322 if (RTEST(lit)) {
11323 VALUE e = shareable_literal_value(elt);
11324 if (e != Qundef) {
11325 rb_ary_push(lit, e);
11326 }
11327 else {
11328 rb_ary_clear(lit);
11329 lit = Qnil; /* make shareable at runtime */
11330 }
11331 }
11332 }
11333 break;
11334
11335 case NODE_HASH:
11336 if (!value->nd_brace) return 0;
11337 lit = rb_hash_new();
11338 for (NODE *n = value->nd_head; n; n = n->nd_next->nd_next) {
11339 NODE *key = n->nd_head;
11340 NODE *val = n->nd_next->nd_head;
11341 if (key) {
11342 key = shareable_literal_constant_next(key);
11343 if (key) {
11344 n->nd_head = key;
11345 }
11346 else if (RTEST(lit)) {
11347 rb_hash_clear(lit);
11348 lit = Qfalse;
11349 }
11350 }
11351 if (val) {
11352 val = shareable_literal_constant_next(val);
11353 if (val) {
11354 n->nd_next->nd_head = val;
11355 }
11356 else if (RTEST(lit)) {
11357 rb_hash_clear(lit);
11358 lit = Qfalse;
11359 }
11360 }
11361 if (RTEST(lit)) {
11362 VALUE k = shareable_literal_value(key);
11363 VALUE v = shareable_literal_value(val);
11364 if (k != Qundef && v != Qundef) {
11365 rb_hash_aset(lit, k, v);
11366 }
11367 else {
11368 rb_hash_clear(lit);
11369 lit = Qnil; /* make shareable at runtime */
11370 }
11371 }
11372 }
11373 break;
11374
11375 default:
11376 if (shareable == shareable_literal &&
11377 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
11378 return ensure_shareable_node(p, dest, value, loc);
11379 }
11380 return 0;
11381 }
11382
11383 /* Array or Hash */
11384 if (!lit) return 0;
11385 if (NIL_P(lit)) {
11386 // if shareable_literal, all elements should have been ensured
11387 // as shareable
11388 value = make_shareable_node(p, value, false, loc);
11389 }
11390 else {
11391 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
11392 RB_OBJ_WRITTEN(p->ast, Qnil, value->nd_lit);
11393 }
11394
11395 return value;
11396# undef shareable_literal_constant_next
11397}
11398
11399static NODE *
11400shareable_constant_value(struct parser_params *p, enum shareability shareable,
11401 NODE *lhs, NODE *value, const YYLTYPE *loc)
11402{
11403 if (!value) return 0;
11404 switch (shareable) {
11405 case shareable_none:
11406 return value;
11407
11408 case shareable_literal:
11409 {
11410 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11411 if (lit) return lit;
11412 return value;
11413 }
11414 break;
11415
11416 case shareable_copy:
11417 case shareable_everything:
11418 {
11419 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11420 if (lit) return lit;
11421 return make_shareable_node(p, value, shareable == shareable_copy, loc);
11422 }
11423 break;
11424
11425 default:
11426 UNREACHABLE_RETURN(0);
11427 }
11428}
11429
11430static NODE *
11431node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
11432{
11433 if (!lhs) return 0;
11434
11435 switch (nd_type(lhs)) {
11436 case NODE_CDECL:
11437 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
11438 /* fallthru */
11439
11440 case NODE_GASGN:
11441 case NODE_IASGN:
11442 case NODE_LASGN:
11443 case NODE_DASGN:
11444 case NODE_MASGN:
11445 case NODE_CVASGN:
11446 lhs->nd_value = rhs;
11447 nd_set_loc(lhs, loc);
11448 break;
11449
11450 case NODE_ATTRASGN:
11451 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
11452 nd_set_loc(lhs, loc);
11453 break;
11454
11455 default:
11456 /* should not happen */
11457 break;
11458 }
11459
11460 return lhs;
11461}
11462
11463static NODE *
11464value_expr_check(struct parser_params *p, NODE *node)
11465{
11466 NODE *void_node = 0, *vn;
11467
11468 if (!node) {
11469 rb_warning0("empty expression");
11470 }
11471 while (node) {
11472 switch (nd_type(node)) {
11473 case NODE_RETURN:
11474 case NODE_BREAK:
11475 case NODE_NEXT:
11476 case NODE_REDO:
11477 case NODE_RETRY:
11478 return void_node ? void_node : node;
11479
11480 case NODE_CASE3:
11481 if (!node->nd_body || !nd_type_p(node->nd_body, NODE_IN)) {
11482 compile_error(p, "unexpected node");
11483 return NULL;
11484 }
11485 if (node->nd_body->nd_body) {
11486 return NULL;
11487 }
11488 /* single line pattern matching */
11489 return void_node ? void_node : node;
11490
11491 case NODE_BLOCK:
11492 while (node->nd_next) {
11493 node = node->nd_next;
11494 }
11495 node = node->nd_head;
11496 break;
11497
11498 case NODE_BEGIN:
11499 node = node->nd_body;
11500 break;
11501
11502 case NODE_IF:
11503 case NODE_UNLESS:
11504 if (!node->nd_body) {
11505 return NULL;
11506 }
11507 else if (!node->nd_else) {
11508 return NULL;
11509 }
11510 vn = value_expr_check(p, node->nd_body);
11511 if (!vn) return NULL;
11512 if (!void_node) void_node = vn;
11513 node = node->nd_else;
11514 break;
11515
11516 case NODE_AND:
11517 case NODE_OR:
11518 node = node->nd_1st;
11519 break;
11520
11521 case NODE_LASGN:
11522 case NODE_DASGN:
11523 case NODE_MASGN:
11524 mark_lvar_used(p, node);
11525 return NULL;
11526
11527 default:
11528 return NULL;
11529 }
11530 }
11531
11532 return NULL;
11533}
11534
11535static int
11536value_expr_gen(struct parser_params *p, NODE *node)
11537{
11538 NODE *void_node = value_expr_check(p, node);
11539 if (void_node) {
11540 yyerror1(&void_node->nd_loc, "void value expression");
11541 /* or "control never reach"? */
11542 return FALSE;
11543 }
11544 return TRUE;
11545}
11546static void
11547void_expr(struct parser_params *p, NODE *node)
11548{
11549 const char *useless = 0;
11550
11551 if (!RTEST(ruby_verbose)) return;
11552
11553 if (!node || !(node = nd_once_body(node))) return;
11554 switch (nd_type(node)) {
11555 case NODE_OPCALL:
11556 switch (node->nd_mid) {
11557 case '+':
11558 case '-':
11559 case '*':
11560 case '/':
11561 case '%':
11562 case tPOW:
11563 case tUPLUS:
11564 case tUMINUS:
11565 case '|':
11566 case '^':
11567 case '&':
11568 case tCMP:
11569 case '>':
11570 case tGEQ:
11571 case '<':
11572 case tLEQ:
11573 case tEQ:
11574 case tNEQ:
11575 useless = rb_id2name(node->nd_mid);
11576 break;
11577 }
11578 break;
11579
11580 case NODE_LVAR:
11581 case NODE_DVAR:
11582 case NODE_GVAR:
11583 case NODE_IVAR:
11584 case NODE_CVAR:
11585 case NODE_NTH_REF:
11586 case NODE_BACK_REF:
11587 useless = "a variable";
11588 break;
11589 case NODE_CONST:
11590 useless = "a constant";
11591 break;
11592 case NODE_LIT:
11593 case NODE_STR:
11594 case NODE_DSTR:
11595 case NODE_DREGX:
11596 useless = "a literal";
11597 break;
11598 case NODE_COLON2:
11599 case NODE_COLON3:
11600 useless = "::";
11601 break;
11602 case NODE_DOT2:
11603 useless = "..";
11604 break;
11605 case NODE_DOT3:
11606 useless = "...";
11607 break;
11608 case NODE_SELF:
11609 useless = "self";
11610 break;
11611 case NODE_NIL:
11612 useless = "nil";
11613 break;
11614 case NODE_TRUE:
11615 useless = "true";
11616 break;
11617 case NODE_FALSE:
11618 useless = "false";
11619 break;
11620 case NODE_DEFINED:
11621 useless = "defined?";
11622 break;
11623 }
11624
11625 if (useless) {
11626 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
11627 }
11628}
11629
11630static NODE *
11631void_stmts(struct parser_params *p, NODE *node)
11632{
11633 NODE *const n = node;
11634 if (!RTEST(ruby_verbose)) return n;
11635 if (!node) return n;
11636 if (!nd_type_p(node, NODE_BLOCK)) return n;
11637
11638 while (node->nd_next) {
11639 void_expr(p, node->nd_head);
11640 node = node->nd_next;
11641 }
11642 return n;
11643}
11644
11645static NODE *
11646remove_begin(NODE *node)
11647{
11648 NODE **n = &node, *n1 = node;
11649 while (n1 && nd_type_p(n1, NODE_BEGIN) && n1->nd_body) {
11650 *n = n1 = n1->nd_body;
11651 }
11652 return node;
11653}
11654
11655static NODE *
11656remove_begin_all(NODE *node)
11657{
11658 NODE **n = &node, *n1 = node;
11659 while (n1 && nd_type_p(n1, NODE_BEGIN)) {
11660 *n = n1 = n1->nd_body;
11661 }
11662 return node;
11663}
11664
11665static void
11666reduce_nodes(struct parser_params *p, NODE **body)
11667{
11668 NODE *node = *body;
11669
11670 if (!node) {
11671 *body = NEW_NIL(&NULL_LOC);
11672 return;
11673 }
11674#define subnodes(n1, n2) \
11675 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
11676 (!node->n2) ? (body = &node->n1, 1) : \
11677 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
11678
11679 while (node) {
11680 int newline = (int)(node->flags & NODE_FL_NEWLINE);
11681 switch (nd_type(node)) {
11682 end:
11683 case NODE_NIL:
11684 *body = 0;
11685 return;
11686 case NODE_RETURN:
11687 *body = node = node->nd_stts;
11688 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11689 continue;
11690 case NODE_BEGIN:
11691 *body = node = node->nd_body;
11692 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11693 continue;
11694 case NODE_BLOCK:
11695 body = &node->nd_end->nd_head;
11696 break;
11697 case NODE_IF:
11698 case NODE_UNLESS:
11699 if (subnodes(nd_body, nd_else)) break;
11700 return;
11701 case NODE_CASE:
11702 body = &node->nd_body;
11703 break;
11704 case NODE_WHEN:
11705 if (!subnodes(nd_body, nd_next)) goto end;
11706 break;
11707 case NODE_ENSURE:
11708 if (!subnodes(nd_head, nd_resq)) goto end;
11709 break;
11710 case NODE_RESCUE:
11711 if (node->nd_else) {
11712 body = &node->nd_resq;
11713 break;
11714 }
11715 if (!subnodes(nd_head, nd_resq)) goto end;
11716 break;
11717 default:
11718 return;
11719 }
11720 node = *body;
11721 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11722 }
11723
11724#undef subnodes
11725}
11726
11727static int
11728is_static_content(NODE *node)
11729{
11730 if (!node) return 1;
11731 switch (nd_type(node)) {
11732 case NODE_HASH:
11733 if (!(node = node->nd_head)) break;
11734 case NODE_LIST:
11735 do {
11736 if (!is_static_content(node->nd_head)) return 0;
11737 } while ((node = node->nd_next) != 0);
11738 case NODE_LIT:
11739 case NODE_STR:
11740 case NODE_NIL:
11741 case NODE_TRUE:
11742 case NODE_FALSE:
11743 case NODE_ZLIST:
11744 break;
11745 default:
11746 return 0;
11747 }
11748 return 1;
11749}
11750
11751static int
11752assign_in_cond(struct parser_params *p, NODE *node)
11753{
11754 switch (nd_type(node)) {
11755 case NODE_MASGN:
11756 case NODE_LASGN:
11757 case NODE_DASGN:
11758 case NODE_GASGN:
11759 case NODE_IASGN:
11760 break;
11761
11762 default:
11763 return 0;
11764 }
11765
11766 if (!node->nd_value) return 1;
11767 if (is_static_content(node->nd_value)) {
11768 /* reports always */
11769 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11770 }
11771 return 1;
11772}
11773
11774enum cond_type {
11775 COND_IN_OP,
11776 COND_IN_COND,
11777 COND_IN_FF
11778};
11779
11780#define SWITCH_BY_COND_TYPE(t, w, arg) \
11781 switch (t) { \
11782 case COND_IN_OP: break; \
11783 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11784 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11785 }
11786
11787static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11788
11789static NODE*
11790range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11791{
11792 enum node_type type;
11793
11794 if (node == 0) return 0;
11795
11796 type = nd_type(node);
11797 value_expr(node);
11798 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11799 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11800 ID lineno = rb_intern("$.");
11801 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
11802 }
11803 return cond0(p, node, COND_IN_FF, loc);
11804}
11805
11806static NODE*
11807cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11808{
11809 if (node == 0) return 0;
11810 if (!(node = nd_once_body(node))) return 0;
11811 assign_in_cond(p, node);
11812
11813 switch (nd_type(node)) {
11814 case NODE_DSTR:
11815 case NODE_EVSTR:
11816 case NODE_STR:
11817 SWITCH_BY_COND_TYPE(type, warn, "string ")
11818 break;
11819
11820 case NODE_DREGX:
11821 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11822
11823 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11824
11825 case NODE_AND:
11826 case NODE_OR:
11827 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11828 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11829 break;
11830
11831 case NODE_DOT2:
11832 case NODE_DOT3:
11833 node->nd_beg = range_op(p, node->nd_beg, loc);
11834 node->nd_end = range_op(p, node->nd_end, loc);
11835 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
11836 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
11837 break;
11838
11839 case NODE_DSYM:
11840 warn_symbol:
11841 SWITCH_BY_COND_TYPE(type, warning, "symbol ")
11842 break;
11843
11844 case NODE_LIT:
11845 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11846 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11847 nd_set_type(node, NODE_MATCH);
11848 }
11849 else if (node->nd_lit == Qtrue ||
11850 node->nd_lit == Qfalse) {
11851 /* booleans are OK, e.g., while true */
11852 }
11853 else if (SYMBOL_P(node->nd_lit)) {
11854 goto warn_symbol;
11855 }
11856 else {
11857 SWITCH_BY_COND_TYPE(type, warning, "")
11858 }
11859 default:
11860 break;
11861 }
11862 return node;
11863}
11864
11865static NODE*
11866cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11867{
11868 if (node == 0) return 0;
11869 return cond0(p, node, COND_IN_COND, loc);
11870}
11871
11872static NODE*
11873method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11874{
11875 if (node == 0) return 0;
11876 return cond0(p, node, COND_IN_OP, loc);
11877}
11878
11879static NODE*
11880new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
11881{
11882 YYLTYPE loc = {*pos, *pos};
11883 return NEW_NIL(&loc);
11884}
11885
11886static NODE*
11887new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11888{
11889 if (!cc) return right;
11890 cc = cond0(p, cc, COND_IN_COND, loc);
11891 return newline_node(NEW_IF(cc, left, right, loc));
11892}
11893
11894static NODE*
11895new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11896{
11897 if (!cc) return right;
11898 cc = cond0(p, cc, COND_IN_COND, loc);
11899 return newline_node(NEW_UNLESS(cc, left, right, loc));
11900}
11901
11902static NODE*
11903logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11904 const YYLTYPE *op_loc, const YYLTYPE *loc)
11905{
11906 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11907 NODE *op;
11908 value_expr(left);
11909 if (left && nd_type_p(left, type)) {
11910 NODE *node = left, *second;
11911 while ((second = node->nd_2nd) != 0 && nd_type_p(second, type)) {
11912 node = second;
11913 }
11914 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11915 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11916 left->nd_loc.end_pos = loc->end_pos;
11917 return left;
11918 }
11919 op = NEW_NODE(type, left, right, 0, loc);
11920 nd_set_line(op, op_loc->beg_pos.lineno);
11921 return op;
11922}
11923
11924static void
11925no_blockarg(struct parser_params *p, NODE *node)
11926{
11927 if (node && nd_type_p(node, NODE_BLOCK_PASS)) {
11928 compile_error(p, "block argument should not be given");
11929 }
11930}
11931
11932static NODE *
11933ret_args(struct parser_params *p, NODE *node)
11934{
11935 if (node) {
11936 no_blockarg(p, node);
11937 if (nd_type_p(node, NODE_LIST)) {
11938 if (node->nd_next == 0) {
11939 node = node->nd_head;
11940 }
11941 else {
11942 nd_set_type(node, NODE_VALUES);
11943 }
11944 }
11945 }
11946 return node;
11947}
11948
11949static NODE *
11950new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11951{
11952 if (node) no_blockarg(p, node);
11953
11954 return NEW_YIELD(node, loc);
11955}
11956
11957static VALUE
11958negate_lit(struct parser_params *p, VALUE lit)
11959{
11960 if (FIXNUM_P(lit)) {
11961 return LONG2FIX(-FIX2LONG(lit));
11962 }
11963 if (SPECIAL_CONST_P(lit)) {
11964#if USE_FLONUM
11965 if (FLONUM_P(lit)) {
11966 return DBL2NUM(-RFLOAT_VALUE(lit));
11967 }
11968#endif
11969 goto unknown;
11970 }
11971 switch (BUILTIN_TYPE(lit)) {
11972 case T_BIGNUM:
11973 BIGNUM_NEGATE(lit);
11974 lit = rb_big_norm(lit);
11975 break;
11976 case T_RATIONAL:
11977 RATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11978 break;
11979 case T_COMPLEX:
11980 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11981 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11982 break;
11983 case T_FLOAT:
11984 lit = DBL2NUM(-RFLOAT_VALUE(lit));
11985 break;
11986 unknown:
11987 default:
11988 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11989 rb_builtin_class_name(lit));
11990 break;
11991 }
11992 return lit;
11993}
11994
11995static NODE *
11996arg_blk_pass(NODE *node1, NODE *node2)
11997{
11998 if (node2) {
11999 if (!node1) return node2;
12000 node2->nd_head = node1;
12001 nd_set_first_lineno(node2, nd_first_lineno(node1));
12002 nd_set_first_column(node2, nd_first_column(node1));
12003 return node2;
12004 }
12005 return node1;
12006}
12007
12008static bool
12009args_info_empty_p(struct rb_args_info *args)
12010{
12011 if (args->pre_args_num) return false;
12012 if (args->post_args_num) return false;
12013 if (args->rest_arg) return false;
12014 if (args->opt_args) return false;
12015 if (args->block_arg) return false;
12016 if (args->kw_args) return false;
12017 if (args->kw_rest_arg) return false;
12018 return true;
12019}
12020
12021static NODE*
12022new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
12023{
12024 int saved_line = p->ruby_sourceline;
12025 struct rb_args_info *args = tail->nd_ainfo;
12026
12027 if (args->block_arg == idFWD_BLOCK) {
12028 if (rest_arg) {
12029 yyerror1(&tail->nd_loc, "... after rest argument");
12030 return tail;
12031 }
12032 rest_arg = idFWD_REST;
12033 }
12034
12035 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
12036 args->pre_init = pre_args ? pre_args->nd_next : 0;
12037
12038 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
12039 args->post_init = post_args ? post_args->nd_next : 0;
12040 args->first_post_arg = post_args ? post_args->nd_pid : 0;
12041
12042 args->rest_arg = rest_arg;
12043
12044 args->opt_args = opt_args;
12045
12046 args->ruby2_keywords = rest_arg == idFWD_REST;
12047
12048 p->ruby_sourceline = saved_line;
12049 nd_set_loc(tail, loc);
12050
12051 return tail;
12052}
12053
12054static NODE*
12055new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
12056{
12057 int saved_line = p->ruby_sourceline;
12058 NODE *node;
12059 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12060 struct rb_args_info *args = ZALLOC(struct rb_args_info);
12061 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
12062 args->imemo = tmpbuf;
12063 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
12064 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12065 if (p->error_p) return node;
12066
12067 args->block_arg = block;
12068 args->kw_args = kw_args;
12069
12070 if (kw_args) {
12071 /*
12072 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
12073 * variable order: k1, kr1, k2, &b, internal_id, krest
12074 * #=> <reorder>
12075 * variable order: kr1, k1, k2, internal_id, krest, &b
12076 */
12077 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
12078 struct vtable *vtargs = p->lvtbl->args;
12079 NODE *kwn = kw_args;
12080
12081 if (block) block = vtargs->tbl[vtargs->pos-1];
12082 vtable_pop(vtargs, !!block + !!kw_rest_arg);
12083 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
12084 while (kwn) {
12085 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
12086 --kw_vars;
12087 --required_kw_vars;
12088 kwn = kwn->nd_next;
12089 }
12090
12091 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
12092 ID vid = kwn->nd_body->nd_vid;
12093 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
12094 *required_kw_vars++ = vid;
12095 }
12096 else {
12097 *kw_vars++ = vid;
12098 }
12099 }
12100
12101 arg_var(p, kw_bits);
12102 if (kw_rest_arg) arg_var(p, kw_rest_arg);
12103 if (block) arg_var(p, block);
12104
12105 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12106 args->kw_rest_arg->nd_cflag = kw_bits;
12107 }
12108 else if (kw_rest_arg == idNil) {
12109 args->no_kwarg = 1;
12110 }
12111 else if (kw_rest_arg) {
12112 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12113 }
12114
12115 p->ruby_sourceline = saved_line;
12116 return node;
12117}
12118
12119static NODE *
12120args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
12121{
12122 if (max_numparam > NO_PARAM) {
12123 if (!args) {
12124 YYLTYPE loc = RUBY_INIT_YYLLOC();
12125 args = new_args_tail(p, 0, 0, 0, 0);
12126 nd_set_loc(args, &loc);
12127 }
12128 args->nd_ainfo->pre_args_num = max_numparam;
12129 }
12130 return args;
12131}
12132
12133static NODE*
12134new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
12135{
12136 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
12137
12138 aryptn->nd_pconst = constant;
12139
12140 if (pre_arg) {
12141 NODE *pre_args = NEW_LIST(pre_arg, loc);
12142 if (apinfo->pre_args) {
12143 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
12144 }
12145 else {
12146 apinfo->pre_args = pre_args;
12147 }
12148 }
12149 return aryptn;
12150}
12151
12152static NODE*
12153new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
12154{
12155 int saved_line = p->ruby_sourceline;
12156 NODE *node;
12157 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12158 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
12159 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
12160 node = NEW_NODE(NODE_ARYPTN, 0, tmpbuf, apinfo, loc);
12161 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12162
12163 apinfo->pre_args = pre_args;
12164
12165 if (has_rest) {
12166 if (rest_arg) {
12167 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
12168 }
12169 else {
12170 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
12171 }
12172 }
12173 else {
12174 apinfo->rest_arg = NULL;
12175 }
12176
12177 apinfo->post_args = post_args;
12178
12179 p->ruby_sourceline = saved_line;
12180 return node;
12181}
12182
12183static NODE*
12184new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
12185{
12186 fndptn->nd_pconst = constant;
12187
12188 return fndptn;
12189}
12190
12191static NODE*
12192new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc)
12193{
12194 int saved_line = p->ruby_sourceline;
12195 NODE *node;
12196 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12197 struct rb_fnd_pattern_info *fpinfo = ZALLOC(struct rb_fnd_pattern_info);
12198 rb_imemo_tmpbuf_set_ptr(tmpbuf, fpinfo);
12199 node = NEW_NODE(NODE_FNDPTN, 0, tmpbuf, fpinfo, loc);
12200 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12201
12202 fpinfo->pre_rest_arg = pre_rest_arg ? assignable(p, pre_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12203 fpinfo->args = args;
12204 fpinfo->post_rest_arg = post_rest_arg ? assignable(p, post_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12205
12206 p->ruby_sourceline = saved_line;
12207 return node;
12208}
12209
12210static NODE*
12211new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
12212{
12213 hshptn->nd_pconst = constant;
12214 return hshptn;
12215}
12216
12217static NODE*
12218new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
12219{
12220 int saved_line = p->ruby_sourceline;
12221 NODE *node, *kw_rest_arg_node;
12222
12223 if (kw_rest_arg == idNil) {
12224 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
12225 }
12226 else if (kw_rest_arg) {
12227 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
12228 }
12229 else {
12230 kw_rest_arg_node = NULL;
12231 }
12232
12233 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
12234
12235 p->ruby_sourceline = saved_line;
12236 return node;
12237}
12238
12239static NODE*
12240dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12241{
12242 VALUE lit;
12243
12244 if (!node) {
12245 return NEW_LIT(ID2SYM(idNULL), loc);
12246 }
12247
12248 switch (nd_type(node)) {
12249 case NODE_DSTR:
12250 nd_set_type(node, NODE_DSYM);
12251 nd_set_loc(node, loc);
12252 break;
12253 case NODE_STR:
12254 lit = node->nd_lit;
12255 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
12256 nd_set_type(node, NODE_LIT);
12257 nd_set_loc(node, loc);
12258 break;
12259 default:
12260 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
12261 break;
12262 }
12263 return node;
12264}
12265
12266static int
12267append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
12268{
12269 NODE *node = (NODE *)v;
12270 NODE **result = (NODE **)h;
12271 node->nd_alen = 2;
12272 node->nd_next->nd_end = node->nd_next;
12273 node->nd_next->nd_next = 0;
12274 if (*result)
12275 list_concat(*result, node);
12276 else
12277 *result = node;
12278 return ST_CONTINUE;
12279}
12280
12281static bool
12282hash_literal_key_p(VALUE k)
12283{
12284 switch (OBJ_BUILTIN_TYPE(k)) {
12285 case T_NODE:
12286 return false;
12287 default:
12288 return true;
12289 }
12290}
12291
12292static int
12293literal_cmp(VALUE val, VALUE lit)
12294{
12295 if (val == lit) return 0;
12296 if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
12297 return rb_iseq_cdhash_cmp(val, lit);
12298}
12299
12300static st_index_t
12301literal_hash(VALUE a)
12302{
12303 if (!hash_literal_key_p(a)) return (st_index_t)a;
12304 return rb_iseq_cdhash_hash(a);
12305}
12306
12307static const struct st_hash_type literal_type = {
12308 literal_cmp,
12309 literal_hash,
12310};
12311
12312static NODE *
12313remove_duplicate_keys(struct parser_params *p, NODE *hash)
12314{
12315 st_table *literal_keys = st_init_table_with_size(&literal_type, hash->nd_alen / 2);
12316 NODE *result = 0;
12317 NODE *last_expr = 0;
12318 rb_code_location_t loc = hash->nd_loc;
12319 while (hash && hash->nd_head && hash->nd_next) {
12320 NODE *head = hash->nd_head;
12321 NODE *value = hash->nd_next;
12322 NODE *next = value->nd_next;
12323 st_data_t key = (st_data_t)head;
12324 st_data_t data;
12325 value->nd_next = 0;
12326 if (nd_type_p(head, NODE_LIT) &&
12327 st_delete(literal_keys, (key = (st_data_t)head->nd_lit, &key), &data)) {
12328 NODE *dup_value = ((NODE *)data)->nd_next;
12329 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
12330 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
12331 head->nd_lit, nd_line(head));
12332 if (dup_value == last_expr) {
12333 value->nd_head = block_append(p, dup_value->nd_head, value->nd_head);
12334 }
12335 else {
12336 last_expr->nd_head = block_append(p, dup_value->nd_head, last_expr->nd_head);
12337 }
12338 }
12339 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
12340 last_expr = nd_type_p(head, NODE_LIT) ? value : head;
12341 hash = next;
12342 }
12343 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
12344 st_free_table(literal_keys);
12345 if (hash) {
12346 if (!result) result = hash;
12347 else list_concat(result, hash);
12348 }
12349 result->nd_loc = loc;
12350 return result;
12351}
12352
12353static NODE *
12354new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12355{
12356 if (hash) hash = remove_duplicate_keys(p, hash);
12357 return NEW_HASH(hash, loc);
12358}
12359#endif
12360
12361static void
12362error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
12363{
12364 if (is_private_local_id(id)) {
12365 return;
12366 }
12367 if (st_is_member(p->pvtbl, id)) {
12368 yyerror1(loc, "duplicated variable name");
12369 }
12370 else {
12371 st_insert(p->pvtbl, (st_data_t)id, 0);
12372 }
12373}
12374
12375static void
12376error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
12377{
12378 if (!p->pktbl) {
12379 p->pktbl = st_init_numtable();
12380 }
12381 else if (st_is_member(p->pktbl, key)) {
12382 yyerror1(loc, "duplicated key name");
12383 return;
12384 }
12385 st_insert(p->pktbl, (st_data_t)key, 0);
12386}
12387
12388#ifndef RIPPER
12389static NODE *
12390new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12391{
12392 return NEW_HASH(hash, loc);
12393}
12394#endif /* !RIPPER */
12395
12396#ifndef RIPPER
12397static NODE *
12398new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12399{
12400 NODE *asgn;
12401
12402 if (lhs) {
12403 ID vid = lhs->nd_vid;
12404 YYLTYPE lhs_loc = lhs->nd_loc;
12405 int shareable = ctxt.shareable_constant_value;
12406 if (shareable) {
12407 switch (nd_type(lhs)) {
12408 case NODE_CDECL:
12409 case NODE_COLON2:
12410 case NODE_COLON3:
12411 break;
12412 default:
12413 shareable = 0;
12414 break;
12415 }
12416 }
12417 if (op == tOROP) {
12418 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12419 lhs->nd_value = rhs;
12420 nd_set_loc(lhs, loc);
12421 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
12422 if (is_notop_id(vid)) {
12423 switch (id_type(vid)) {
12424 case ID_GLOBAL:
12425 case ID_INSTANCE:
12426 case ID_CLASS:
12427 asgn->nd_aid = vid;
12428 }
12429 }
12430 }
12431 else if (op == tANDOP) {
12432 if (shareable) {
12433 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12434 }
12435 lhs->nd_value = rhs;
12436 nd_set_loc(lhs, loc);
12437 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
12438 }
12439 else {
12440 asgn = lhs;
12441 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
12442 if (shareable) {
12443 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12444 }
12445 asgn->nd_value = rhs;
12446 nd_set_loc(asgn, loc);
12447 }
12448 }
12449 else {
12450 asgn = NEW_BEGIN(0, loc);
12451 }
12452 return asgn;
12453}
12454
12455static NODE *
12456new_ary_op_assign(struct parser_params *p, NODE *ary,
12457 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
12458{
12459 NODE *asgn;
12460
12461 args = make_list(args, args_loc);
12462 if (nd_type_p(args, NODE_BLOCK_PASS)) {
12463 args = NEW_ARGSCAT(args, rhs, loc);
12464 }
12465 else {
12466 args = arg_concat(p, args, rhs, loc);
12467 }
12468 asgn = NEW_OP_ASGN1(ary, op, args, loc);
12469 fixpos(asgn, ary);
12470 return asgn;
12471}
12472
12473static NODE *
12474new_attr_op_assign(struct parser_params *p, NODE *lhs,
12475 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
12476{
12477 NODE *asgn;
12478
12479 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
12480 fixpos(asgn, lhs);
12481 return asgn;
12482}
12483
12484static NODE *
12485new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12486{
12487 NODE *asgn;
12488
12489 if (lhs) {
12490 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
12491 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
12492 }
12493 else {
12494 asgn = NEW_BEGIN(0, loc);
12495 }
12496 fixpos(asgn, lhs);
12497 return asgn;
12498}
12499
12500static NODE *
12501const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
12502{
12503 if (p->ctxt.in_def) {
12504 yyerror1(loc, "dynamic constant assignment");
12505 }
12506 return NEW_CDECL(0, 0, (path), loc);
12507}
12508#else
12509static VALUE
12510const_decl(struct parser_params *p, VALUE path)
12511{
12512 if (p->ctxt.in_def) {
12513 path = assign_error(p, "dynamic constant assignment", path);
12514 }
12515 return path;
12516}
12517
12518static VALUE
12519assign_error(struct parser_params *p, const char *mesg, VALUE a)
12520{
12521 a = dispatch2(assign_error, ERR_MESG(), a);
12522 ripper_error(p);
12523 return a;
12524}
12525
12526static VALUE
12527var_field(struct parser_params *p, VALUE a)
12528{
12529 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
12530}
12531#endif
12532
12533#ifndef RIPPER
12534static NODE *
12535new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
12536{
12537 NODE *result = head;
12538 if (rescue) {
12539 NODE *tmp = rescue_else ? rescue_else : rescue;
12540 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
12541
12542 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
12543 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
12544 }
12545 else if (rescue_else) {
12546 result = block_append(p, result, rescue_else);
12547 }
12548 if (ensure) {
12549 result = NEW_ENSURE(result, ensure, loc);
12550 }
12551 fixpos(result, head);
12552 return result;
12553}
12554#endif
12555
12556static void
12557warn_unused_var(struct parser_params *p, struct local_vars *local)
12558{
12559 int cnt;
12560
12561 if (!local->used) return;
12562 cnt = local->used->pos;
12563 if (cnt != local->vars->pos) {
12564 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
12565 }
12566#ifndef RIPPER
12567 ID *v = local->vars->tbl;
12568 ID *u = local->used->tbl;
12569 for (int i = 0; i < cnt; ++i) {
12570 if (!v[i] || (u[i] & LVAR_USED)) continue;
12571 if (is_private_local_id(v[i])) continue;
12572 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
12573 }
12574#endif
12575}
12576
12577static void
12578local_push(struct parser_params *p, int toplevel_scope)
12579{
12580 struct local_vars *local;
12581 int inherits_dvars = toplevel_scope && compile_for_eval;
12582 int warn_unused_vars = RTEST(ruby_verbose);
12583
12584 local = ALLOC(struct local_vars);
12585 local->prev = p->lvtbl;
12586 local->args = vtable_alloc(0);
12587 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
12588#ifndef RIPPER
12589 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
12590 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
12591 local->numparam.outer = 0;
12592 local->numparam.inner = 0;
12593 local->numparam.current = 0;
12594#endif
12595 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
12596
12597# if WARN_PAST_SCOPE
12598 local->past = 0;
12599# endif
12600 CMDARG_PUSH(0);
12601 COND_PUSH(0);
12602 p->lvtbl = local;
12603}
12604
12605static void
12606local_pop(struct parser_params *p)
12607{
12608 struct local_vars *local = p->lvtbl->prev;
12609 if (p->lvtbl->used) {
12610 warn_unused_var(p, p->lvtbl);
12611 vtable_free(p->lvtbl->used);
12612 }
12613# if WARN_PAST_SCOPE
12614 while (p->lvtbl->past) {
12615 struct vtable *past = p->lvtbl->past;
12616 p->lvtbl->past = past->prev;
12617 vtable_free(past);
12618 }
12619# endif
12620 vtable_free(p->lvtbl->args);
12621 vtable_free(p->lvtbl->vars);
12622 CMDARG_POP();
12623 COND_POP();
12624 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12625 p->lvtbl = local;
12626}
12627
12628#ifndef RIPPER
12629static rb_ast_id_table_t *
12630local_tbl(struct parser_params *p)
12631{
12632 int cnt_args = vtable_size(p->lvtbl->args);
12633 int cnt_vars = vtable_size(p->lvtbl->vars);
12634 int cnt = cnt_args + cnt_vars;
12635 int i, j;
12636 rb_ast_id_table_t *tbl;
12637
12638 if (cnt <= 0) return 0;
12639 tbl = rb_ast_new_local_table(p->ast, cnt);
12640 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
12641 /* remove IDs duplicated to warn shadowing */
12642 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
12643 ID id = p->lvtbl->vars->tbl[i];
12644 if (!vtable_included(p->lvtbl->args, id)) {
12645 tbl->ids[j++] = id;
12646 }
12647 }
12648 if (j < cnt) {
12649 tbl = rb_ast_resize_latest_local_table(p->ast, j);
12650 }
12651
12652 return tbl;
12653}
12654
12655static NODE*
12656node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
12657{
12658 rb_ast_id_table_t *a0;
12659 NODE *n;
12660
12661 a0 = local_tbl(p);
12662 n = NEW_NODE(type, a0, a1, a2, loc);
12663 return n;
12664}
12665
12666#endif
12667
12668static void
12669numparam_name(struct parser_params *p, ID id)
12670{
12671 if (!NUMPARAM_ID_P(id)) return;
12672 compile_error(p, "_%d is reserved for numbered parameter",
12673 NUMPARAM_ID_TO_IDX(id));
12674}
12675
12676static void
12677arg_var(struct parser_params *p, ID id)
12678{
12679 numparam_name(p, id);
12680 vtable_add(p->lvtbl->args, id);
12681}
12682
12683static void
12684local_var(struct parser_params *p, ID id)
12685{
12686 numparam_name(p, id);
12687 vtable_add(p->lvtbl->vars, id);
12688 if (p->lvtbl->used) {
12689 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
12690 }
12691}
12692
12693static int
12694local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
12695{
12696 struct vtable *vars, *args, *used;
12697
12698 vars = p->lvtbl->vars;
12699 args = p->lvtbl->args;
12700 used = p->lvtbl->used;
12701
12702 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
12703 vars = vars->prev;
12704 args = args->prev;
12705 if (used) used = used->prev;
12706 }
12707
12708 if (vars && vars->prev == DVARS_INHERIT) {
12709 return rb_local_defined(id, p->parent_iseq);
12710 }
12711 else if (vtable_included(args, id)) {
12712 return 1;
12713 }
12714 else {
12715 int i = vtable_included(vars, id);
12716 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
12717 return i != 0;
12718 }
12719}
12720
12721static int
12722local_id(struct parser_params *p, ID id)
12723{
12724 return local_id_ref(p, id, NULL);
12725}
12726
12727static int
12728check_forwarding_args(struct parser_params *p)
12729{
12730 if (local_id(p, idFWD_REST) &&
12731#if idFWD_KWREST
12732 local_id(p, idFWD_KWREST) &&
12733#endif
12734 local_id(p, idFWD_BLOCK)) return TRUE;
12735 compile_error(p, "unexpected ...");
12736 return FALSE;
12737}
12738
12739static void
12740add_forwarding_args(struct parser_params *p)
12741{
12742 arg_var(p, idFWD_REST);
12743#if idFWD_KWREST
12744 arg_var(p, idFWD_KWREST);
12745#endif
12746 arg_var(p, idFWD_BLOCK);
12747}
12748
12749#ifndef RIPPER
12750static NODE *
12751new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
12752{
12753 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, loc), loc);
12754#if idFWD_KWREST
12755 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
12756#endif
12757 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
12758 NODE *args = leading ? rest_arg_append(p, leading, splat, argsloc) : splat;
12759#if idFWD_KWREST
12760 args = arg_append(p, splat, new_hash(p, kwrest, loc), loc);
12761#endif
12762 return arg_blk_pass(args, block);
12763}
12764#endif
12765
12766static NODE *
12767numparam_push(struct parser_params *p)
12768{
12769#ifndef RIPPER
12770 struct local_vars *local = p->lvtbl;
12771 NODE *inner = local->numparam.inner;
12772 if (!local->numparam.outer) {
12773 local->numparam.outer = local->numparam.current;
12774 }
12775 local->numparam.inner = 0;
12776 local->numparam.current = 0;
12777 return inner;
12778#else
12779 return 0;
12780#endif
12781}
12782
12783static void
12784numparam_pop(struct parser_params *p, NODE *prev_inner)
12785{
12786#ifndef RIPPER
12787 struct local_vars *local = p->lvtbl;
12788 if (prev_inner) {
12789 /* prefer first one */
12790 local->numparam.inner = prev_inner;
12791 }
12792 else if (local->numparam.current) {
12793 /* current and inner are exclusive */
12794 local->numparam.inner = local->numparam.current;
12795 }
12796 if (p->max_numparam > NO_PARAM) {
12797 /* current and outer are exclusive */
12798 local->numparam.current = local->numparam.outer;
12799 local->numparam.outer = 0;
12800 }
12801 else {
12802 /* no numbered parameter */
12803 local->numparam.current = 0;
12804 }
12805#endif
12806}
12807
12808static const struct vtable *
12809dyna_push(struct parser_params *p)
12810{
12811 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
12812 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
12813 if (p->lvtbl->used) {
12814 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
12815 }
12816 return p->lvtbl->args;
12817}
12818
12819static void
12820dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
12821{
12822 struct vtable *tmp = *vtblp;
12823 *vtblp = tmp->prev;
12824# if WARN_PAST_SCOPE
12825 if (p->past_scope_enabled) {
12826 tmp->prev = p->lvtbl->past;
12827 p->lvtbl->past = tmp;
12828 return;
12829 }
12830# endif
12831 vtable_free(tmp);
12832}
12833
12834static void
12835dyna_pop_1(struct parser_params *p)
12836{
12837 struct vtable *tmp;
12838
12839 if ((tmp = p->lvtbl->used) != 0) {
12840 warn_unused_var(p, p->lvtbl);
12841 p->lvtbl->used = p->lvtbl->used->prev;
12842 vtable_free(tmp);
12843 }
12844 dyna_pop_vtable(p, &p->lvtbl->args);
12845 dyna_pop_vtable(p, &p->lvtbl->vars);
12846}
12847
12848static void
12849dyna_pop(struct parser_params *p, const struct vtable *lvargs)
12850{
12851 while (p->lvtbl->args != lvargs) {
12852 dyna_pop_1(p);
12853 if (!p->lvtbl->args) {
12854 struct local_vars *local = p->lvtbl->prev;
12855 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12856 p->lvtbl = local;
12857 }
12858 }
12859 dyna_pop_1(p);
12860}
12861
12862static int
12863dyna_in_block(struct parser_params *p)
12864{
12865 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12866}
12867
12868static int
12869dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12870{
12871 struct vtable *vars, *args, *used;
12872 int i;
12873
12874 args = p->lvtbl->args;
12875 vars = p->lvtbl->vars;
12876 used = p->lvtbl->used;
12877
12878 while (!DVARS_TERMINAL_P(vars)) {
12879 if (vtable_included(args, id)) {
12880 return 1;
12881 }
12882 if ((i = vtable_included(vars, id)) != 0) {
12883 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12884 return 1;
12885 }
12886 args = args->prev;
12887 vars = vars->prev;
12888 if (!vidrefp) used = 0;
12889 if (used) used = used->prev;
12890 }
12891
12892 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
12893 return rb_dvar_defined(id, p->parent_iseq);
12894 }
12895
12896 return 0;
12897}
12898
12899static int
12900dvar_defined(struct parser_params *p, ID id)
12901{
12902 return dvar_defined_ref(p, id, NULL);
12903}
12904
12905static int
12906dvar_curr(struct parser_params *p, ID id)
12907{
12908 return (vtable_included(p->lvtbl->args, id) ||
12909 vtable_included(p->lvtbl->vars, id));
12910}
12911
12912static void
12913reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12914{
12915 compile_error(p,
12916 "regexp encoding option '%c' differs from source encoding '%s'",
12917 c, rb_enc_name(rb_enc_get(str)));
12918}
12919
12920#ifndef RIPPER
12921int
12922rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12923{
12924 int c = RE_OPTION_ENCODING_IDX(options);
12925
12926 if (c) {
12927 int opt, idx;
12928 rb_char_to_option_kcode(c, &opt, &idx);
12929 if (idx != ENCODING_GET(str) &&
12930 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12931 goto error;
12932 }
12933 ENCODING_SET(str, idx);
12934 }
12935 else if (RE_OPTION_ENCODING_NONE(options)) {
12936 if (!ENCODING_IS_ASCII8BIT(str) &&
12937 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12938 c = 'n';
12939 goto error;
12940 }
12941 rb_enc_associate(str, rb_ascii8bit_encoding());
12942 }
12943 else if (p->enc == rb_usascii_encoding()) {
12944 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12945 /* raise in re.c */
12946 rb_enc_associate(str, rb_usascii_encoding());
12947 }
12948 else {
12949 rb_enc_associate(str, rb_ascii8bit_encoding());
12950 }
12951 }
12952 return 0;
12953
12954 error:
12955 return c;
12956}
12957
12958static void
12959reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12960{
12961 int c = rb_reg_fragment_setenc(p, str, options);
12962 if (c) reg_fragment_enc_error(p, str, c);
12963}
12964
12965static int
12966reg_fragment_check(struct parser_params* p, VALUE str, int options)
12967{
12968 VALUE err;
12969 reg_fragment_setenc(p, str, options);
12970 err = rb_reg_check_preprocess(str);
12971 if (err != Qnil) {
12972 err = rb_obj_as_string(err);
12973 compile_error(p, "%"PRIsVALUE, err);
12974 return 0;
12975 }
12976 return 1;
12977}
12978
12979typedef struct {
12980 struct parser_params* parser;
12981 rb_encoding *enc;
12982 NODE *succ_block;
12983 const YYLTYPE *loc;
12984} reg_named_capture_assign_t;
12985
12986static int
12987reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12988 int back_num, int *back_refs, OnigRegex regex, void *arg0)
12989{
12990 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12991 struct parser_params* p = arg->parser;
12992 rb_encoding *enc = arg->enc;
12993 long len = name_end - name;
12994 const char *s = (const char *)name;
12995 ID var;
12996 NODE *node, *succ;
12997
12998 if (!len) return ST_CONTINUE;
12999 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
13000 return ST_CONTINUE;
13001
13002 var = intern_cstr(s, len, enc);
13003 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
13004 if (!lvar_defined(p, var)) return ST_CONTINUE;
13005 }
13006 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc);
13007 succ = arg->succ_block;
13008 if (!succ) succ = NEW_BEGIN(0, arg->loc);
13009 succ = block_append(p, succ, node);
13010 arg->succ_block = succ;
13011 return ST_CONTINUE;
13012}
13013
13014static NODE *
13015reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
13016{
13017 reg_named_capture_assign_t arg;
13018
13019 arg.parser = p;
13020 arg.enc = rb_enc_get(regexp);
13021 arg.succ_block = 0;
13022 arg.loc = loc;
13023 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
13024
13025 if (!arg.succ_block) return 0;
13026 return arg.succ_block->nd_next;
13027}
13028
13029static VALUE
13030parser_reg_compile(struct parser_params* p, VALUE str, int options)
13031{
13032 reg_fragment_setenc(p, str, options);
13033 return rb_parser_reg_compile(p, str, options);
13034}
13035
13036VALUE
13037rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
13038{
13039 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
13040}
13041
13042static VALUE
13043reg_compile(struct parser_params* p, VALUE str, int options)
13044{
13045 VALUE re;
13046 VALUE err;
13047
13048 err = rb_errinfo();
13049 re = parser_reg_compile(p, str, options);
13050 if (NIL_P(re)) {
13051 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
13052 rb_set_errinfo(err);
13053 compile_error(p, "%"PRIsVALUE, m);
13054 return Qnil;
13055 }
13056 return re;
13057}
13058#else
13059static VALUE
13060parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
13061{
13062 VALUE err = rb_errinfo();
13063 VALUE re;
13064 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
13065 int c = rb_reg_fragment_setenc(p, str, options);
13066 if (c) reg_fragment_enc_error(p, str, c);
13067 re = rb_parser_reg_compile(p, str, options);
13068 if (NIL_P(re)) {
13069 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
13070 rb_set_errinfo(err);
13071 }
13072 return re;
13073}
13074#endif
13075
13076#ifndef RIPPER
13077void
13078rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
13079{
13080 struct parser_params *p;
13081 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13082 p->do_print = print;
13083 p->do_loop = loop;
13084 p->do_chomp = chomp;
13085 p->do_split = split;
13086}
13087
13088static NODE *
13089parser_append_options(struct parser_params *p, NODE *node)
13090{
13091 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
13092 const YYLTYPE *const LOC = &default_location;
13093
13094 if (p->do_print) {
13095 NODE *print = NEW_FCALL(rb_intern("print"),
13096 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
13097 LOC);
13098 node = block_append(p, node, print);
13099 }
13100
13101 if (p->do_loop) {
13102 if (p->do_split) {
13103 ID ifs = rb_intern("$;");
13104 ID fields = rb_intern("$F");
13105 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
13106 NODE *split = NEW_GASGN(fields,
13107 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13108 rb_intern("split"), args, LOC),
13109 LOC);
13110 node = block_append(p, split, node);
13111 }
13112 if (p->do_chomp) {
13113 NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13114 rb_intern("chomp!"), 0, LOC);
13115 node = block_append(p, chomp, node);
13116 }
13117
13118 node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
13119 }
13120
13121 return node;
13122}
13123
13124void
13125rb_init_parse(void)
13126{
13127 /* just to suppress unused-function warnings */
13128 (void)nodetype;
13129 (void)nodeline;
13130}
13131
13132static ID
13133internal_id(struct parser_params *p)
13134{
13135 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
13136}
13137#endif /* !RIPPER */
13138
13139static void
13140parser_initialize(struct parser_params *p)
13141{
13142 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
13143 p->command_start = TRUE;
13144 p->ruby_sourcefile_string = Qnil;
13145 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
13146 p->node_id = 0;
13147#ifdef RIPPER
13148 p->delayed.token = Qnil;
13149 p->result = Qnil;
13150 p->parsing_thread = Qnil;
13151#else
13152 p->error_buffer = Qfalse;
13153#endif
13154 p->debug_buffer = Qnil;
13155 p->debug_output = rb_ractor_stdout();
13156 p->enc = rb_utf8_encoding();
13157}
13158
13159#ifdef RIPPER
13160#define parser_mark ripper_parser_mark
13161#define parser_free ripper_parser_free
13162#endif
13163
13164static void
13165parser_mark(void *ptr)
13166{
13167 struct parser_params *p = (struct parser_params*)ptr;
13168
13169 rb_gc_mark(p->lex.input);
13170 rb_gc_mark(p->lex.prevline);
13171 rb_gc_mark(p->lex.lastline);
13172 rb_gc_mark(p->lex.nextline);
13173 rb_gc_mark(p->ruby_sourcefile_string);
13174 rb_gc_mark((VALUE)p->lex.strterm);
13175 rb_gc_mark((VALUE)p->ast);
13176 rb_gc_mark(p->case_labels);
13177#ifndef RIPPER
13178 rb_gc_mark(p->debug_lines);
13179 rb_gc_mark(p->compile_option);
13180 rb_gc_mark(p->error_buffer);
13181#else
13182 rb_gc_mark(p->delayed.token);
13183 rb_gc_mark(p->value);
13184 rb_gc_mark(p->result);
13185 rb_gc_mark(p->parsing_thread);
13186#endif
13187 rb_gc_mark(p->debug_buffer);
13188 rb_gc_mark(p->debug_output);
13189#ifdef YYMALLOC
13190 rb_gc_mark((VALUE)p->heap);
13191#endif
13192}
13193
13194static void
13195parser_free(void *ptr)
13196{
13197 struct parser_params *p = (struct parser_params*)ptr;
13198 struct local_vars *local, *prev;
13199
13200 if (p->tokenbuf) {
13201 ruby_sized_xfree(p->tokenbuf, p->toksiz);
13202 }
13203 for (local = p->lvtbl; local; local = prev) {
13204 if (local->vars) xfree(local->vars);
13205 prev = local->prev;
13206 xfree(local);
13207 }
13208 {
13209 token_info *ptinfo;
13210 while ((ptinfo = p->token_info) != 0) {
13211 p->token_info = ptinfo->next;
13212 xfree(ptinfo);
13213 }
13214 }
13215 xfree(ptr);
13216}
13217
13218static size_t
13219parser_memsize(const void *ptr)
13220{
13221 struct parser_params *p = (struct parser_params*)ptr;
13222 struct local_vars *local;
13223 size_t size = sizeof(*p);
13224
13225 size += p->toksiz;
13226 for (local = p->lvtbl; local; local = local->prev) {
13227 size += sizeof(*local);
13228 if (local->vars) size += local->vars->capa * sizeof(ID);
13229 }
13230 return size;
13231}
13232
13233static const rb_data_type_t parser_data_type = {
13234#ifndef RIPPER
13235 "parser",
13236#else
13237 "ripper",
13238#endif
13239 {
13240 parser_mark,
13241 parser_free,
13242 parser_memsize,
13243 },
13244 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
13245};
13246
13247#ifndef RIPPER
13248#undef rb_reserved_word
13249
13250const struct kwtable *
13251rb_reserved_word(const char *str, unsigned int len)
13252{
13253 return reserved_word(str, len);
13254}
13255
13256VALUE
13257rb_parser_new(void)
13258{
13259 struct parser_params *p;
13260 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
13261 &parser_data_type, p);
13262 parser_initialize(p);
13263 return parser;
13264}
13265
13266VALUE
13267rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
13268{
13269 struct parser_params *p;
13270
13271 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13272 p->error_buffer = main ? Qfalse : Qnil;
13273 p->parent_iseq = base;
13274 return vparser;
13275}
13276
13277void
13278rb_parser_keep_script_lines(VALUE vparser)
13279{
13280 struct parser_params *p;
13281
13282 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13283 p->keep_script_lines = 1;
13284}
13285#endif
13286
13287#ifdef RIPPER
13288#define rb_parser_end_seen_p ripper_parser_end_seen_p
13289#define rb_parser_encoding ripper_parser_encoding
13290#define rb_parser_get_yydebug ripper_parser_get_yydebug
13291#define rb_parser_set_yydebug ripper_parser_set_yydebug
13292#define rb_parser_get_debug_output ripper_parser_get_debug_output
13293#define rb_parser_set_debug_output ripper_parser_set_debug_output
13294static VALUE ripper_parser_end_seen_p(VALUE vparser);
13295static VALUE ripper_parser_encoding(VALUE vparser);
13296static VALUE ripper_parser_get_yydebug(VALUE self);
13297static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
13298static VALUE ripper_parser_get_debug_output(VALUE self);
13299static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
13300
13301/*
13302 * call-seq:
13303 * ripper.error? -> Boolean
13304 *
13305 * Return true if parsed source has errors.
13306 */
13307static VALUE
13308ripper_error_p(VALUE vparser)
13309{
13310 struct parser_params *p;
13311
13312 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13313 return RBOOL(p->error_p);
13314}
13315#endif
13316
13317/*
13318 * call-seq:
13319 * ripper.end_seen? -> Boolean
13320 *
13321 * Return true if parsed source ended by +\_\_END\_\_+.
13322 */
13323VALUE
13324rb_parser_end_seen_p(VALUE vparser)
13325{
13326 struct parser_params *p;
13327
13328 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13329 return RBOOL(p->ruby__end__seen);
13330}
13331
13332/*
13333 * call-seq:
13334 * ripper.encoding -> encoding
13335 *
13336 * Return encoding of the source.
13337 */
13338VALUE
13339rb_parser_encoding(VALUE vparser)
13340{
13341 struct parser_params *p;
13342
13343 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13344 return rb_enc_from_encoding(p->enc);
13345}
13346
13347#ifdef RIPPER
13348/*
13349 * call-seq:
13350 * ripper.yydebug -> true or false
13351 *
13352 * Get yydebug.
13353 */
13354VALUE
13355rb_parser_get_yydebug(VALUE self)
13356{
13357 struct parser_params *p;
13358
13359 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13360 return RBOOL(p->debug);
13361}
13362#endif
13363
13364/*
13365 * call-seq:
13366 * ripper.yydebug = flag
13367 *
13368 * Set yydebug.
13369 */
13370VALUE
13371rb_parser_set_yydebug(VALUE self, VALUE flag)
13372{
13373 struct parser_params *p;
13374
13375 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13376 p->debug = RTEST(flag);
13377 return flag;
13378}
13379
13380/*
13381 * call-seq:
13382 * ripper.debug_output -> obj
13383 *
13384 * Get debug output.
13385 */
13386VALUE
13387rb_parser_get_debug_output(VALUE self)
13388{
13389 struct parser_params *p;
13390
13391 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13392 return p->debug_output;
13393}
13394
13395/*
13396 * call-seq:
13397 * ripper.debug_output = obj
13398 *
13399 * Set debug output.
13400 */
13401VALUE
13402rb_parser_set_debug_output(VALUE self, VALUE output)
13403{
13404 struct parser_params *p;
13405
13406 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13407 return p->debug_output = output;
13408}
13409
13410#ifndef RIPPER
13411#ifdef YYMALLOC
13412#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
13413/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
13414 * potential memory leak */
13415#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
13416#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
13417 (new)->cnt = (cnt), (ptr))
13418
13419void *
13420rb_parser_malloc(struct parser_params *p, size_t size)
13421{
13422 size_t cnt = HEAPCNT(1, size);
13423 rb_imemo_tmpbuf_t *n = NEWHEAP();
13424 void *ptr = xmalloc(size);
13425
13426 return ADD2HEAP(n, cnt, ptr);
13427}
13428
13429void *
13430rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
13431{
13432 size_t cnt = HEAPCNT(nelem, size);
13433 rb_imemo_tmpbuf_t *n = NEWHEAP();
13434 void *ptr = xcalloc(nelem, size);
13435
13436 return ADD2HEAP(n, cnt, ptr);
13437}
13438
13439void *
13440rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
13441{
13442 rb_imemo_tmpbuf_t *n;
13443 size_t cnt = HEAPCNT(1, size);
13444
13445 if (ptr && (n = p->heap) != NULL) {
13446 do {
13447 if (n->ptr == ptr) {
13448 n->ptr = ptr = xrealloc(ptr, size);
13449 if (n->cnt) n->cnt = cnt;
13450 return ptr;
13451 }
13452 } while ((n = n->next) != NULL);
13453 }
13454 n = NEWHEAP();
13455 ptr = xrealloc(ptr, size);
13456 return ADD2HEAP(n, cnt, ptr);
13457}
13458
13459void
13460rb_parser_free(struct parser_params *p, void *ptr)
13461{
13462 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
13463
13464 while ((n = *prev) != NULL) {
13465 if (n->ptr == ptr) {
13466 *prev = n->next;
13467 break;
13468 }
13469 prev = &n->next;
13470 }
13471}
13472#endif
13473
13474void
13475rb_parser_printf(struct parser_params *p, const char *fmt, ...)
13476{
13477 va_list ap;
13478 VALUE mesg = p->debug_buffer;
13479
13480 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
13481 va_start(ap, fmt);
13482 rb_str_vcatf(mesg, fmt, ap);
13483 va_end(ap);
13484 if (RSTRING_END(mesg)[-1] == '\n') {
13485 rb_io_write(p->debug_output, mesg);
13486 p->debug_buffer = Qnil;
13487 }
13488}
13489
13490static void
13491parser_compile_error(struct parser_params *p, const char *fmt, ...)
13492{
13493 va_list ap;
13494
13495 rb_io_flush(p->debug_output);
13496 p->error_p = 1;
13497 va_start(ap, fmt);
13498 p->error_buffer =
13499 rb_syntax_error_append(p->error_buffer,
13500 p->ruby_sourcefile_string,
13501 p->ruby_sourceline,
13502 rb_long2int(p->lex.pcur - p->lex.pbeg),
13503 p->enc, fmt, ap);
13504 va_end(ap);
13505}
13506
13507static size_t
13508count_char(const char *str, int c)
13509{
13510 int n = 0;
13511 while (str[n] == c) ++n;
13512 return n;
13513}
13514
13515/*
13516 * strip enclosing double-quotes, same as the default yytnamerr except
13517 * for that single-quotes matching back-quotes do not stop stripping.
13518 *
13519 * "\"`class' keyword\"" => "`class' keyword"
13520 */
13521RUBY_FUNC_EXPORTED size_t
13522rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
13523{
13524 if (*yystr == '"') {
13525 size_t yyn = 0, bquote = 0;
13526 const char *yyp = yystr;
13527
13528 while (*++yyp) {
13529 switch (*yyp) {
13530 case '`':
13531 if (!bquote) {
13532 bquote = count_char(yyp+1, '`') + 1;
13533 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
13534 yyn += bquote;
13535 yyp += bquote - 1;
13536 break;
13537 }
13538 goto default_char;
13539
13540 case '\'':
13541 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
13542 if (yyres) memcpy(yyres + yyn, yyp, bquote);
13543 yyn += bquote;
13544 yyp += bquote - 1;
13545 bquote = 0;
13546 break;
13547 }
13548 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
13549 if (yyres) memcpy(yyres + yyn, yyp, 3);
13550 yyn += 3;
13551 yyp += 2;
13552 break;
13553 }
13554 goto do_not_strip_quotes;
13555
13556 case ',':
13557 goto do_not_strip_quotes;
13558
13559 case '\\':
13560 if (*++yyp != '\\')
13561 goto do_not_strip_quotes;
13562 /* Fall through. */
13563 default_char:
13564 default:
13565 if (yyres)
13566 yyres[yyn] = *yyp;
13567 yyn++;
13568 break;
13569
13570 case '"':
13571 case '\0':
13572 if (yyres)
13573 yyres[yyn] = '\0';
13574 return yyn;
13575 }
13576 }
13577 do_not_strip_quotes: ;
13578 }
13579
13580 if (!yyres) return strlen(yystr);
13581
13582 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
13583}
13584#endif
13585
13586#ifdef RIPPER
13587#ifdef RIPPER_DEBUG
13588/* :nodoc: */
13589static VALUE
13590ripper_validate_object(VALUE self, VALUE x)
13591{
13592 if (x == Qfalse) return x;
13593 if (x == Qtrue) return x;
13594 if (x == Qnil) return x;
13595 if (x == Qundef)
13596 rb_raise(rb_eArgError, "Qundef given");
13597 if (FIXNUM_P(x)) return x;
13598 if (SYMBOL_P(x)) return x;
13599 switch (BUILTIN_TYPE(x)) {
13600 case T_STRING:
13601 case T_OBJECT:
13602 case T_ARRAY:
13603 case T_BIGNUM:
13604 case T_FLOAT:
13605 case T_COMPLEX:
13606 case T_RATIONAL:
13607 break;
13608 case T_NODE:
13609 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
13610 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
13611 }
13612 x = ((NODE *)x)->nd_rval;
13613 break;
13614 default:
13615 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
13616 (void *)x, rb_obj_classname(x));
13617 }
13618 if (!RBASIC_CLASS(x)) {
13619 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
13620 (void *)x, rb_builtin_type_name(TYPE(x)));
13621 }
13622 return x;
13623}
13624#endif
13625
13626#define validate(x) ((x) = get_value(x))
13627
13628static VALUE
13629ripper_dispatch0(struct parser_params *p, ID mid)
13630{
13631 return rb_funcall(p->value, mid, 0);
13632}
13633
13634static VALUE
13635ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
13636{
13637 validate(a);
13638 return rb_funcall(p->value, mid, 1, a);
13639}
13640
13641static VALUE
13642ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
13643{
13644 validate(a);
13645 validate(b);
13646 return rb_funcall(p->value, mid, 2, a, b);
13647}
13648
13649static VALUE
13650ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
13651{
13652 validate(a);
13653 validate(b);
13654 validate(c);
13655 return rb_funcall(p->value, mid, 3, a, b, c);
13656}
13657
13658static VALUE
13659ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
13660{
13661 validate(a);
13662 validate(b);
13663 validate(c);
13664 validate(d);
13665 return rb_funcall(p->value, mid, 4, a, b, c, d);
13666}
13667
13668static VALUE
13669ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
13670{
13671 validate(a);
13672 validate(b);
13673 validate(c);
13674 validate(d);
13675 validate(e);
13676 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
13677}
13678
13679static VALUE
13680ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
13681{
13682 validate(a);
13683 validate(b);
13684 validate(c);
13685 validate(d);
13686 validate(e);
13687 validate(f);
13688 validate(g);
13689 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
13690}
13691
13692static ID
13693ripper_get_id(VALUE v)
13694{
13695 NODE *nd;
13696 if (!RB_TYPE_P(v, T_NODE)) return 0;
13697 nd = (NODE *)v;
13698 if (!nd_type_p(nd, NODE_RIPPER)) return 0;
13699 return nd->nd_vid;
13700}
13701
13702static VALUE
13703ripper_get_value(VALUE v)
13704{
13705 NODE *nd;
13706 if (v == Qundef) return Qnil;
13707 if (!RB_TYPE_P(v, T_NODE)) return v;
13708 nd = (NODE *)v;
13709 if (!nd_type_p(nd, NODE_RIPPER)) return Qnil;
13710 return nd->nd_rval;
13711}
13712
13713static void
13714ripper_error(struct parser_params *p)
13715{
13716 p->error_p = TRUE;
13717}
13718
13719static void
13720ripper_compile_error(struct parser_params *p, const char *fmt, ...)
13721{
13722 VALUE str;
13723 va_list args;
13724
13725 va_start(args, fmt);
13726 str = rb_vsprintf(fmt, args);
13727 va_end(args);
13728 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
13729 ripper_error(p);
13730}
13731
13732static VALUE
13733ripper_lex_get_generic(struct parser_params *p, VALUE src)
13734{
13735 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
13736 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
13737 rb_raise(rb_eTypeError,
13738 "gets returned %"PRIsVALUE" (expected String or nil)",
13739 rb_obj_class(line));
13740 }
13741 return line;
13742}
13743
13744static VALUE
13745ripper_lex_io_get(struct parser_params *p, VALUE src)
13746{
13747 return rb_io_gets(src);
13748}
13749
13750static VALUE
13751ripper_s_allocate(VALUE klass)
13752{
13753 struct parser_params *p;
13754 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
13755 &parser_data_type, p);
13756 p->value = self;
13757 return self;
13758}
13759
13760#define ripper_initialized_p(r) ((r)->lex.input != 0)
13761
13762/*
13763 * call-seq:
13764 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
13765 *
13766 * Create a new Ripper object.
13767 * _src_ must be a String, an IO, or an Object which has #gets method.
13768 *
13769 * This method does not starts parsing.
13770 * See also Ripper#parse and Ripper.parse.
13771 */
13772static VALUE
13773ripper_initialize(int argc, VALUE *argv, VALUE self)
13774{
13775 struct parser_params *p;
13776 VALUE src, fname, lineno;
13777
13778 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13779 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
13780 if (RB_TYPE_P(src, T_FILE)) {
13781 p->lex.gets = ripper_lex_io_get;
13782 }
13783 else if (rb_respond_to(src, id_gets)) {
13784 p->lex.gets = ripper_lex_get_generic;
13785 }
13786 else {
13787 StringValue(src);
13788 p->lex.gets = lex_get_str;
13789 }
13790 p->lex.input = src;
13791 p->eofp = 0;
13792 if (NIL_P(fname)) {
13793 fname = STR_NEW2("(ripper)");
13794 OBJ_FREEZE(fname);
13795 }
13796 else {
13797 StringValueCStr(fname);
13798 fname = rb_str_new_frozen(fname);
13799 }
13800 parser_initialize(p);
13801
13802 p->ruby_sourcefile_string = fname;
13803 p->ruby_sourcefile = RSTRING_PTR(fname);
13804 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
13805
13806 return Qnil;
13807}
13808
13809static VALUE
13810ripper_parse0(VALUE parser_v)
13811{
13812 struct parser_params *p;
13813
13814 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13815 parser_prepare(p);
13816 p->ast = rb_ast_new();
13817 ripper_yyparse((void*)p);
13818 rb_ast_dispose(p->ast);
13819 p->ast = 0;
13820 return p->result;
13821}
13822
13823static VALUE
13824ripper_ensure(VALUE parser_v)
13825{
13826 struct parser_params *p;
13827
13828 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13829 p->parsing_thread = Qnil;
13830 return Qnil;
13831}
13832
13833/*
13834 * call-seq:
13835 * ripper.parse
13836 *
13837 * Start parsing and returns the value of the root action.
13838 */
13839static VALUE
13840ripper_parse(VALUE self)
13841{
13842 struct parser_params *p;
13843
13844 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13845 if (!ripper_initialized_p(p)) {
13846 rb_raise(rb_eArgError, "method called for uninitialized object");
13847 }
13848 if (!NIL_P(p->parsing_thread)) {
13849 if (p->parsing_thread == rb_thread_current())
13850 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13851 else
13852 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13853 }
13854 p->parsing_thread = rb_thread_current();
13855 rb_ensure(ripper_parse0, self, ripper_ensure, self);
13856
13857 return p->result;
13858}
13859
13860/*
13861 * call-seq:
13862 * ripper.column -> Integer
13863 *
13864 * Return column number of current parsing line.
13865 * This number starts from 0.
13866 */
13867static VALUE
13868ripper_column(VALUE self)
13869{
13870 struct parser_params *p;
13871 long col;
13872
13873 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13874 if (!ripper_initialized_p(p)) {
13875 rb_raise(rb_eArgError, "method called for uninitialized object");
13876 }
13877 if (NIL_P(p->parsing_thread)) return Qnil;
13878 col = p->lex.ptok - p->lex.pbeg;
13879 return LONG2NUM(col);
13880}
13881
13882/*
13883 * call-seq:
13884 * ripper.filename -> String
13885 *
13886 * Return current parsing filename.
13887 */
13888static VALUE
13889ripper_filename(VALUE self)
13890{
13891 struct parser_params *p;
13892
13893 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13894 if (!ripper_initialized_p(p)) {
13895 rb_raise(rb_eArgError, "method called for uninitialized object");
13896 }
13897 return p->ruby_sourcefile_string;
13898}
13899
13900/*
13901 * call-seq:
13902 * ripper.lineno -> Integer
13903 *
13904 * Return line number of current parsing line.
13905 * This number starts from 1.
13906 */
13907static VALUE
13908ripper_lineno(VALUE self)
13909{
13910 struct parser_params *p;
13911
13912 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13913 if (!ripper_initialized_p(p)) {
13914 rb_raise(rb_eArgError, "method called for uninitialized object");
13915 }
13916 if (NIL_P(p->parsing_thread)) return Qnil;
13917 return INT2NUM(p->ruby_sourceline);
13918}
13919
13920/*
13921 * call-seq:
13922 * ripper.state -> Integer
13923 *
13924 * Return scanner state of current token.
13925 */
13926static VALUE
13927ripper_state(VALUE self)
13928{
13929 struct parser_params *p;
13930
13931 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13932 if (!ripper_initialized_p(p)) {
13933 rb_raise(rb_eArgError, "method called for uninitialized object");
13934 }
13935 if (NIL_P(p->parsing_thread)) return Qnil;
13936 return INT2NUM(p->lex.state);
13937}
13938
13939/*
13940 * call-seq:
13941 * ripper.token -> String
13942 *
13943 * Return the current token string.
13944 */
13945static VALUE
13946ripper_token(VALUE self)
13947{
13948 struct parser_params *p;
13949 long pos, len;
13950
13951 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13952 if (!ripper_initialized_p(p)) {
13953 rb_raise(rb_eArgError, "method called for uninitialized object");
13954 }
13955 if (NIL_P(p->parsing_thread)) return Qnil;
13956 pos = p->lex.ptok - p->lex.pbeg;
13957 len = p->lex.pcur - p->lex.ptok;
13958 return rb_str_subseq(p->lex.lastline, pos, len);
13959}
13960
13961#ifdef RIPPER_DEBUG
13962/* :nodoc: */
13963static VALUE
13964ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13965{
13966 StringValue(msg);
13967 if (obj == Qundef) {
13968 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13969 }
13970 return Qnil;
13971}
13972
13973/* :nodoc: */
13974static VALUE
13975ripper_value(VALUE self, VALUE obj)
13976{
13977 return ULONG2NUM(obj);
13978}
13979#endif
13980
13981/*
13982 * call-seq:
13983 * Ripper.lex_state_name(integer) -> string
13984 *
13985 * Returns a string representation of lex_state.
13986 */
13987static VALUE
13988ripper_lex_state_name(VALUE self, VALUE state)
13989{
13990 return rb_parser_lex_state_name(NUM2INT(state));
13991}
13992
13993void
13994Init_ripper(void)
13995{
13996 ripper_init_eventids1();
13997 ripper_init_eventids2();
13998 id_warn = rb_intern_const("warn");
13999 id_warning = rb_intern_const("warning");
14000 id_gets = rb_intern_const("gets");
14001 id_assoc = rb_intern_const("=>");
14002
14003 (void)yystpcpy; /* may not used in newer bison */
14004
14005 InitVM(ripper);
14006}
14007
14008void
14009InitVM_ripper(void)
14010{
14011 VALUE Ripper;
14012
14013 Ripper = rb_define_class("Ripper", rb_cObject);
14014 /* version of Ripper */
14015 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
14016 rb_define_alloc_func(Ripper, ripper_s_allocate);
14017 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
14018 rb_define_method(Ripper, "parse", ripper_parse, 0);
14019 rb_define_method(Ripper, "column", ripper_column, 0);
14020 rb_define_method(Ripper, "filename", ripper_filename, 0);
14021 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
14022 rb_define_method(Ripper, "state", ripper_state, 0);
14023 rb_define_method(Ripper, "token", ripper_token, 0);
14024 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
14025 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
14026 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
14027 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
14028 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
14029 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
14030 rb_define_method(Ripper, "error?", ripper_error_p, 0);
14031#ifdef RIPPER_DEBUG
14032 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
14033 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
14034 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
14035#endif
14036
14037 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
14038 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
14039
14040 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
14041
14042<% @exprs.each do |expr, desc| -%>
14043 /* <%=desc%> */
14044 rb_define_const(Ripper, "<%=expr%>", INT2NUM(<%=expr%>));
14045<% end %>
14046 ripper_init_eventids1_table(Ripper);
14047 ripper_init_eventids2_table(Ripper);
14048
14049# if 0
14050 /* Hack to let RDoc document SCRIPT_LINES__ */
14051
14052 /*
14053 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
14054 * after the assignment will be added as an Array of lines with the file
14055 * name as the key.
14056 */
14057 rb_define_global_const("SCRIPT_LINES__", Qnil);
14058#endif
14059
14060}
14061#endif /* RIPPER */
14062
14063/*
14064 * Local variables:
14065 * mode: c
14066 * c-file-style: "ruby"
14067 * End:
14068 */