Ruby 3.3.2p78 (2024-05-30 revision e5a195edf62fe1bf7146a191da13fa1c4fecbd71)
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%require "3.0"
13
14%{
15
16#if !YYPURE
17# error needs pure parser
18#endif
19#define YYDEBUG 1
20#define YYERROR_VERBOSE 1
21#define YYSTACK_USE_ALLOCA 0
22#define YYLTYPE rb_code_location_t
23#define YYLTYPE_IS_DECLARED 1
24
25/* For Ripper */
26#ifdef RUBY_EXTCONF_H
27# include RUBY_EXTCONF_H
28#endif
29
30#include "ruby/internal/config.h"
31
32#include <errno.h>
33
34#ifdef UNIVERSAL_PARSER
35
36#include "internal/ruby_parser.h"
37#include "parser_node.h"
38#include "universal_parser.c"
39
40#ifdef RIPPER
41#undef T_NODE
42#define T_NODE 0x1b
43#define STATIC_ID2SYM p->config->static_id2sym
44#define rb_str_coderange_scan_restartable p->config->str_coderange_scan_restartable
45#endif
46
47#else
48
49#include "internal.h"
50#include "internal/compile.h"
51#include "internal/compilers.h"
52#include "internal/complex.h"
53#include "internal/encoding.h"
54#include "internal/error.h"
55#include "internal/hash.h"
56#include "internal/imemo.h"
57#include "internal/io.h"
58#include "internal/numeric.h"
59#include "internal/parse.h"
60#include "internal/rational.h"
61#include "internal/re.h"
62#include "internal/ruby_parser.h"
63#include "internal/symbol.h"
64#include "internal/thread.h"
65#include "internal/variable.h"
66#include "node.h"
67#include "parser_node.h"
68#include "probes.h"
69#include "regenc.h"
70#include "ruby/encoding.h"
71#include "ruby/regex.h"
72#include "ruby/ruby.h"
73#include "ruby/st.h"
74#include "ruby/util.h"
75#include "ruby/ractor.h"
76#include "symbol.h"
77
78#ifndef RIPPER
79static void
80bignum_negate(VALUE b)
81{
82 BIGNUM_NEGATE(b);
83}
84
85static void
86rational_set_num(VALUE r, VALUE n)
87{
88 RATIONAL_SET_NUM(r, n);
89}
90
91static VALUE
92rational_get_num(VALUE obj)
93{
94 return RRATIONAL(obj)->num;
95}
96
97static void
98rcomplex_set_real(VALUE cmp, VALUE r)
99{
100 RCOMPLEX_SET_REAL(cmp, r);
101}
102
103static VALUE
104rcomplex_get_real(VALUE obj)
105{
106 return RCOMPLEX(obj)->real;
107}
108
109static void
110rcomplex_set_imag(VALUE cmp, VALUE i)
111{
112 RCOMPLEX_SET_IMAG(cmp, i);
113}
114
115static VALUE
116rcomplex_get_imag(VALUE obj)
117{
118 return RCOMPLEX(obj)->imag;
119}
120
121static bool
122hash_literal_key_p(VALUE k)
123{
124 switch (OBJ_BUILTIN_TYPE(k)) {
125 case T_NODE:
126 return false;
127 default:
128 return true;
129 }
130}
131
132static int
133literal_cmp(VALUE val, VALUE lit)
134{
135 if (val == lit) return 0;
136 if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
137 return rb_iseq_cdhash_cmp(val, lit);
138}
139
140static st_index_t
141literal_hash(VALUE a)
142{
143 if (!hash_literal_key_p(a)) return (st_index_t)a;
144 return rb_iseq_cdhash_hash(a);
145}
146
147static VALUE
148syntax_error_new(void)
149{
150 return rb_class_new_instance(0, 0, rb_eSyntaxError);
151}
152
153static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
154#endif /* !RIPPER */
155
156#define compile_callback rb_suppress_tracing
157VALUE rb_io_gets_internal(VALUE io);
158
159VALUE rb_node_case_when_optimizable_literal(const NODE *const node);
160#endif /* !UNIVERSAL_PARSER */
161
162static inline int
163parse_isascii(int c)
164{
165 return '\0' <= c && c <= '\x7f';
166}
167
168#undef ISASCII
169#define ISASCII parse_isascii
170
171static inline int
172parse_isspace(int c)
173{
174 return c == ' ' || ('\t' <= c && c <= '\r');
175}
176
177#undef ISSPACE
178#define ISSPACE parse_isspace
179
180static inline int
181parse_iscntrl(int c)
182{
183 return ('\0' <= c && c < ' ') || c == '\x7f';
184}
185
186#undef ISCNTRL
187#define ISCNTRL(c) parse_iscntrl(c)
188
189static inline int
190parse_isupper(int c)
191{
192 return 'A' <= c && c <= 'Z';
193}
194
195static inline int
196parse_islower(int c)
197{
198 return 'a' <= c && c <= 'z';
199}
200
201static inline int
202parse_isalpha(int c)
203{
204 return parse_isupper(c) || parse_islower(c);
205}
206
207#undef ISALPHA
208#define ISALPHA(c) parse_isalpha(c)
209
210static inline int
211parse_isdigit(int c)
212{
213 return '0' <= c && c <= '9';
214}
215
216#undef ISDIGIT
217#define ISDIGIT(c) parse_isdigit(c)
218
219static inline int
220parse_isalnum(int c)
221{
222 return parse_isalpha(c) || parse_isdigit(c);
223}
224
225#undef ISALNUM
226#define ISALNUM(c) parse_isalnum(c)
227
228static inline int
229parse_isxdigit(int c)
230{
231 return parse_isdigit(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
232}
233
234#undef ISXDIGIT
235#define ISXDIGIT(c) parse_isxdigit(c)
236
237#include "parser_st.h"
238
239#undef STRCASECMP
240#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
241
242#undef STRNCASECMP
243#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
244
245#ifdef RIPPER
246#include "ripper_init.h"
247#endif
248
249enum shareability {
250 shareable_none,
251 shareable_literal,
252 shareable_copy,
253 shareable_everything,
254};
255
256enum rescue_context {
257 before_rescue,
258 after_rescue,
259 after_else,
260 after_ensure,
261};
262
263struct lex_context {
264 unsigned int in_defined: 1;
265 unsigned int in_kwarg: 1;
266 unsigned int in_argdef: 1;
267 unsigned int in_def: 1;
268 unsigned int in_class: 1;
269 BITFIELD(enum shareability, shareable_constant_value, 2);
270 BITFIELD(enum rescue_context, in_rescue, 2);
271};
272
273typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
274typedef struct RNode_EXITS rb_node_exits_t;
275
276#if defined(__GNUC__) && !defined(__clang__)
277// Suppress "parameter passing for argument of type 'struct
278// lex_context' changed" notes. `struct lex_context` is file scope,
279// and has no ABI compatibility issue.
280RBIMPL_WARNING_PUSH()
281RBIMPL_WARNING_IGNORED(-Wpsabi)
282RBIMPL_WARNING_POP()
283// Not sure why effective even after popped.
284#endif
285
286#include "parse.h"
287
288#define NO_LEX_CTXT (struct lex_context){0}
289
290#define AREF(ary, i) RARRAY_AREF(ary, i)
291
292#ifndef WARN_PAST_SCOPE
293# define WARN_PAST_SCOPE 0
294#endif
295
296#define TAB_WIDTH 8
297
298#define yydebug (p->debug) /* disable the global variable definition */
299
300#define YYMALLOC(size) rb_parser_malloc(p, (size))
301#define YYREALLOC(ptr, size) rb_parser_realloc(p, (ptr), (size))
302#define YYCALLOC(nelem, size) rb_parser_calloc(p, (nelem), (size))
303#define YYFREE(ptr) rb_parser_free(p, (ptr))
304#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__)
305#define YY_LOCATION_PRINT(File, loc, p) \
306 rb_parser_printf(p, "%d.%d-%d.%d", \
307 (loc).beg_pos.lineno, (loc).beg_pos.column,\
308 (loc).end_pos.lineno, (loc).end_pos.column)
309#define YYLLOC_DEFAULT(Current, Rhs, N) \
310 do \
311 if (N) \
312 { \
313 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
314 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
315 } \
316 else \
317 { \
318 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
319 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
320 } \
321 while (0)
322#define YY_(Msgid) \
323 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
324 "nesting too deep" : (Msgid))
325
326#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
327 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
328#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
329 rb_parser_set_location_of_delayed_token(p, &(Current))
330#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
331 rb_parser_set_location_of_heredoc_end(p, &(Current))
332#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
333 rb_parser_set_location_of_dummy_end(p, &(Current))
334#define RUBY_SET_YYLLOC_OF_NONE(Current) \
335 rb_parser_set_location_of_none(p, &(Current))
336#define RUBY_SET_YYLLOC(Current) \
337 rb_parser_set_location(p, &(Current))
338#define RUBY_INIT_YYLLOC() \
339 { \
340 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
341 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
342 }
343
344#define IS_lex_state_for(x, ls) ((x) & (ls))
345#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
346#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
347#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
348
349# define SET_LEX_STATE(ls) \
350 parser_set_lex_state(p, ls, __LINE__)
351static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
352
353typedef VALUE stack_type;
354
355static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
356
357# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
358# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
359# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
360# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
361# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
362
363/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
364 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
365#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
366#define COND_POP() BITSTACK_POP(cond_stack)
367#define COND_P() BITSTACK_SET_P(cond_stack)
368#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
369
370/* A flag to identify keyword_do_block; "do" keyword after command_call.
371 Example: `foo 1, 2 do`. */
372#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
373#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
374#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
375#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
376
377struct vtable {
378 ID *tbl;
379 int pos;
380 int capa;
381 struct vtable *prev;
382};
383
384struct local_vars {
385 struct vtable *args;
386 struct vtable *vars;
387 struct vtable *used;
388# if WARN_PAST_SCOPE
389 struct vtable *past;
390# endif
391 struct local_vars *prev;
392# ifndef RIPPER
393 struct {
394 NODE *outer, *inner, *current;
395 } numparam;
396# endif
397};
398
399enum {
400 ORDINAL_PARAM = -1,
401 NO_PARAM = 0,
402 NUMPARAM_MAX = 9,
403};
404
405#define DVARS_INHERIT ((void*)1)
406#define DVARS_TOPSCOPE NULL
407#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
408
409typedef struct token_info {
410 const char *token;
411 rb_code_position_t beg;
412 int indent;
413 int nonspc;
414 struct token_info *next;
415} token_info;
416
417/*
418 Structure of Lexer Buffer:
419
420 lex.pbeg lex.ptok lex.pcur lex.pend
421 | | | |
422 |------------+------------+------------|
423 |<---------->|
424 token
425*/
426struct parser_params {
427 rb_imemo_tmpbuf_t *heap;
428
429 YYSTYPE *lval;
430 YYLTYPE *yylloc;
431
432 struct {
433 rb_strterm_t *strterm;
434 VALUE (*gets)(struct parser_params*,VALUE);
435 VALUE input;
436 VALUE lastline;
437 VALUE nextline;
438 const char *pbeg;
439 const char *pcur;
440 const char *pend;
441 const char *ptok;
442 union {
443 long ptr;
444 VALUE (*call)(VALUE, int);
445 } gets_;
446 enum lex_state_e state;
447 /* track the nest level of any parens "()[]{}" */
448 int paren_nest;
449 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
450 int lpar_beg;
451 /* track the nest level of only braces "{}" */
452 int brace_nest;
453 } lex;
454 stack_type cond_stack;
455 stack_type cmdarg_stack;
456 int tokidx;
457 int toksiz;
458 int heredoc_end;
459 int heredoc_indent;
460 int heredoc_line_indent;
461 char *tokenbuf;
462 struct local_vars *lvtbl;
463 st_table *pvtbl;
464 st_table *pktbl;
465 int line_count;
466 int ruby_sourceline; /* current line no. */
467 const char *ruby_sourcefile; /* current source file */
468 VALUE ruby_sourcefile_string;
469 rb_encoding *enc;
470 token_info *token_info;
471 VALUE case_labels;
472 rb_node_exits_t *exits;
473
474 VALUE debug_buffer;
475 VALUE debug_output;
476
477 struct {
478 VALUE token;
479 int beg_line;
480 int beg_col;
481 int end_line;
482 int end_col;
483 } delayed;
484
485 ID cur_arg;
486
487 rb_ast_t *ast;
488 int node_id;
489
490 int max_numparam;
491
492 struct lex_context ctxt;
493
494#ifdef UNIVERSAL_PARSER
495 rb_parser_config_t *config;
496#endif
497 /* compile_option */
498 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
499
500 unsigned int command_start:1;
501 unsigned int eofp: 1;
502 unsigned int ruby__end__seen: 1;
503 unsigned int debug: 1;
504 unsigned int has_shebang: 1;
505 unsigned int token_seen: 1;
506 unsigned int token_info_enabled: 1;
507# if WARN_PAST_SCOPE
508 unsigned int past_scope_enabled: 1;
509# endif
510 unsigned int error_p: 1;
511 unsigned int cr_seen: 1;
512
513#ifndef RIPPER
514 /* Ruby core only */
515
516 unsigned int do_print: 1;
517 unsigned int do_loop: 1;
518 unsigned int do_chomp: 1;
519 unsigned int do_split: 1;
520 unsigned int error_tolerant: 1;
521 unsigned int keep_tokens: 1;
522
523 NODE *eval_tree_begin;
524 NODE *eval_tree;
525 VALUE error_buffer;
526 VALUE debug_lines;
527 const struct rb_iseq_struct *parent_iseq;
528 /* store specific keyword locations to generate dummy end token */
529 VALUE end_expect_token_locations;
530 /* id for terms */
531 int token_id;
532 /* Array for term tokens */
533 VALUE tokens;
534#else
535 /* Ripper only */
536
537 VALUE value;
538 VALUE result;
539 VALUE parsing_thread;
540#endif
541};
542
543#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
544#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
545#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
546static int
547numparam_id_p(struct parser_params *p, ID id)
548{
549 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
550 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
551 return idx > 0 && idx <= NUMPARAM_MAX;
552}
553static void numparam_name(struct parser_params *p, ID id);
554
555
556#define intern_cstr(n,l,en) rb_intern3(n,l,en)
557
558#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
559#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
560#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
561#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
562#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
563#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
564
565static inline bool
566end_with_newline_p(struct parser_params *p, VALUE str)
567{
568 return RSTRING_LEN(str) > 0 && RSTRING_END(str)[-1] == '\n';
569}
570
571static void
572pop_pvtbl(struct parser_params *p, st_table *tbl)
573{
574 st_free_table(p->pvtbl);
575 p->pvtbl = tbl;
576}
577
578static void
579pop_pktbl(struct parser_params *p, st_table *tbl)
580{
581 if (p->pktbl) st_free_table(p->pktbl);
582 p->pktbl = tbl;
583}
584
585#ifndef RIPPER
586static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
587
588static void
589debug_end_expect_token_locations(struct parser_params *p, const char *name)
590{
591 if(p->debug) {
592 VALUE mesg = rb_sprintf("%s: ", name);
593 rb_str_catf(mesg, " %"PRIsVALUE"\n", p->end_expect_token_locations);
594 flush_debug_buffer(p, p->debug_output, mesg);
595 }
596}
597
598static void
599push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
600{
601 if(NIL_P(p->end_expect_token_locations)) return;
602 rb_ary_push(p->end_expect_token_locations, rb_ary_new_from_args(2, INT2NUM(pos->lineno), INT2NUM(pos->column)));
603 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
604}
605
606static void
607pop_end_expect_token_locations(struct parser_params *p)
608{
609 if(NIL_P(p->end_expect_token_locations)) return;
610 rb_ary_pop(p->end_expect_token_locations);
611 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
612}
613
614static VALUE
615peek_end_expect_token_locations(struct parser_params *p)
616{
617 if(NIL_P(p->end_expect_token_locations)) return Qnil;
618 return rb_ary_last(0, 0, p->end_expect_token_locations);
619}
620
621static ID
622parser_token2id(struct parser_params *p, enum yytokentype tok)
623{
624 switch ((int) tok) {
625#define TOKEN2ID(tok) case tok: return rb_intern(#tok);
626#define TOKEN2ID2(tok, name) case tok: return rb_intern(name);
627 TOKEN2ID2(' ', "words_sep")
628 TOKEN2ID2('!', "!")
629 TOKEN2ID2('%', "%");
630 TOKEN2ID2('&', "&");
631 TOKEN2ID2('*', "*");
632 TOKEN2ID2('+', "+");
633 TOKEN2ID2('-', "-");
634 TOKEN2ID2('/', "/");
635 TOKEN2ID2('<', "<");
636 TOKEN2ID2('=', "=");
637 TOKEN2ID2('>', ">");
638 TOKEN2ID2('?', "?");
639 TOKEN2ID2('^', "^");
640 TOKEN2ID2('|', "|");
641 TOKEN2ID2('~', "~");
642 TOKEN2ID2(':', ":");
643 TOKEN2ID2(',', ",");
644 TOKEN2ID2('.', ".");
645 TOKEN2ID2(';', ";");
646 TOKEN2ID2('`', "`");
647 TOKEN2ID2('\n', "nl");
648 TOKEN2ID2('{', "{");
649 TOKEN2ID2('}', "}");
650 TOKEN2ID2('[', "[");
651 TOKEN2ID2(']', "]");
652 TOKEN2ID2('(', "(");
653 TOKEN2ID2(')', ")");
654 TOKEN2ID2('\\', "backslash");
655 TOKEN2ID(keyword_class);
656 TOKEN2ID(keyword_module);
657 TOKEN2ID(keyword_def);
658 TOKEN2ID(keyword_undef);
659 TOKEN2ID(keyword_begin);
660 TOKEN2ID(keyword_rescue);
661 TOKEN2ID(keyword_ensure);
662 TOKEN2ID(keyword_end);
663 TOKEN2ID(keyword_if);
664 TOKEN2ID(keyword_unless);
665 TOKEN2ID(keyword_then);
666 TOKEN2ID(keyword_elsif);
667 TOKEN2ID(keyword_else);
668 TOKEN2ID(keyword_case);
669 TOKEN2ID(keyword_when);
670 TOKEN2ID(keyword_while);
671 TOKEN2ID(keyword_until);
672 TOKEN2ID(keyword_for);
673 TOKEN2ID(keyword_break);
674 TOKEN2ID(keyword_next);
675 TOKEN2ID(keyword_redo);
676 TOKEN2ID(keyword_retry);
677 TOKEN2ID(keyword_in);
678 TOKEN2ID(keyword_do);
679 TOKEN2ID(keyword_do_cond);
680 TOKEN2ID(keyword_do_block);
681 TOKEN2ID(keyword_do_LAMBDA);
682 TOKEN2ID(keyword_return);
683 TOKEN2ID(keyword_yield);
684 TOKEN2ID(keyword_super);
685 TOKEN2ID(keyword_self);
686 TOKEN2ID(keyword_nil);
687 TOKEN2ID(keyword_true);
688 TOKEN2ID(keyword_false);
689 TOKEN2ID(keyword_and);
690 TOKEN2ID(keyword_or);
691 TOKEN2ID(keyword_not);
692 TOKEN2ID(modifier_if);
693 TOKEN2ID(modifier_unless);
694 TOKEN2ID(modifier_while);
695 TOKEN2ID(modifier_until);
696 TOKEN2ID(modifier_rescue);
697 TOKEN2ID(keyword_alias);
698 TOKEN2ID(keyword_defined);
699 TOKEN2ID(keyword_BEGIN);
700 TOKEN2ID(keyword_END);
701 TOKEN2ID(keyword__LINE__);
702 TOKEN2ID(keyword__FILE__);
703 TOKEN2ID(keyword__ENCODING__);
704 TOKEN2ID(tIDENTIFIER);
705 TOKEN2ID(tFID);
706 TOKEN2ID(tGVAR);
707 TOKEN2ID(tIVAR);
708 TOKEN2ID(tCONSTANT);
709 TOKEN2ID(tCVAR);
710 TOKEN2ID(tLABEL);
711 TOKEN2ID(tINTEGER);
712 TOKEN2ID(tFLOAT);
713 TOKEN2ID(tRATIONAL);
714 TOKEN2ID(tIMAGINARY);
715 TOKEN2ID(tCHAR);
716 TOKEN2ID(tNTH_REF);
717 TOKEN2ID(tBACK_REF);
718 TOKEN2ID(tSTRING_CONTENT);
719 TOKEN2ID(tREGEXP_END);
720 TOKEN2ID(tDUMNY_END);
721 TOKEN2ID(tSP);
722 TOKEN2ID(tUPLUS);
723 TOKEN2ID(tUMINUS);
724 TOKEN2ID(tPOW);
725 TOKEN2ID(tCMP);
726 TOKEN2ID(tEQ);
727 TOKEN2ID(tEQQ);
728 TOKEN2ID(tNEQ);
729 TOKEN2ID(tGEQ);
730 TOKEN2ID(tLEQ);
731 TOKEN2ID(tANDOP);
732 TOKEN2ID(tOROP);
733 TOKEN2ID(tMATCH);
734 TOKEN2ID(tNMATCH);
735 TOKEN2ID(tDOT2);
736 TOKEN2ID(tDOT3);
737 TOKEN2ID(tBDOT2);
738 TOKEN2ID(tBDOT3);
739 TOKEN2ID(tAREF);
740 TOKEN2ID(tASET);
741 TOKEN2ID(tLSHFT);
742 TOKEN2ID(tRSHFT);
743 TOKEN2ID(tANDDOT);
744 TOKEN2ID(tCOLON2);
745 TOKEN2ID(tCOLON3);
746 TOKEN2ID(tOP_ASGN);
747 TOKEN2ID(tASSOC);
748 TOKEN2ID(tLPAREN);
749 TOKEN2ID(tLPAREN_ARG);
750 TOKEN2ID(tRPAREN);
751 TOKEN2ID(tLBRACK);
752 TOKEN2ID(tLBRACE);
753 TOKEN2ID(tLBRACE_ARG);
754 TOKEN2ID(tSTAR);
755 TOKEN2ID(tDSTAR);
756 TOKEN2ID(tAMPER);
757 TOKEN2ID(tLAMBDA);
758 TOKEN2ID(tSYMBEG);
759 TOKEN2ID(tSTRING_BEG);
760 TOKEN2ID(tXSTRING_BEG);
761 TOKEN2ID(tREGEXP_BEG);
762 TOKEN2ID(tWORDS_BEG);
763 TOKEN2ID(tQWORDS_BEG);
764 TOKEN2ID(tSYMBOLS_BEG);
765 TOKEN2ID(tQSYMBOLS_BEG);
766 TOKEN2ID(tSTRING_END);
767 TOKEN2ID(tSTRING_DEND);
768 TOKEN2ID(tSTRING_DBEG);
769 TOKEN2ID(tSTRING_DVAR);
770 TOKEN2ID(tLAMBEG);
771 TOKEN2ID(tLABEL_END);
772 TOKEN2ID(tIGNORED_NL);
773 TOKEN2ID(tCOMMENT);
774 TOKEN2ID(tEMBDOC_BEG);
775 TOKEN2ID(tEMBDOC);
776 TOKEN2ID(tEMBDOC_END);
777 TOKEN2ID(tHEREDOC_BEG);
778 TOKEN2ID(tHEREDOC_END);
779 TOKEN2ID(k__END__);
780 TOKEN2ID(tLOWEST);
781 TOKEN2ID(tUMINUS_NUM);
782 TOKEN2ID(tLAST_TOKEN);
783#undef TOKEN2ID
784#undef TOKEN2ID2
785 }
786
787 rb_bug("parser_token2id: unknown token %d", tok);
788
789 UNREACHABLE_RETURN(0);
790}
791
792#endif
793
794RBIMPL_ATTR_NONNULL((1, 2, 3))
795static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
796RBIMPL_ATTR_NONNULL((1, 2))
797static int parser_yyerror0(struct parser_params*, const char*);
798#define yyerror0(msg) parser_yyerror0(p, (msg))
799#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
800#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
801#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
802#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
803#define lex_eol_p(p) lex_eol_n_p(p, 0)
804#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
805#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
806#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
807
808static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
809static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
810static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
811static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
812static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
813
814#ifdef RIPPER
815#define compile_for_eval (0)
816#else
817#define compile_for_eval (p->parent_iseq != 0)
818#endif
819
820#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
821
822#define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
823#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
824
825#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
826
827static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
828
829#ifndef RIPPER
830static inline void
831rb_discard_node(struct parser_params *p, NODE *n)
832{
833 rb_ast_delete_node(p->ast, n);
834}
835#endif
836
837#ifdef RIPPER
838static inline VALUE
839add_mark_object(struct parser_params *p, VALUE obj)
840{
841 if (!SPECIAL_CONST_P(obj)
842 && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */
843 ) {
844 rb_ast_add_mark_object(p->ast, obj);
845 }
846 return obj;
847}
848
849static rb_node_ripper_t *rb_node_ripper_new(struct parser_params *p, ID a, VALUE b, VALUE c, const YYLTYPE *loc);
850static rb_node_ripper_values_t *rb_node_ripper_values_new(struct parser_params *p, VALUE a, VALUE b, VALUE c, const YYLTYPE *loc);
851#define NEW_RIPPER(a,b,c,loc) (VALUE)rb_node_ripper_new(p,a,b,c,loc)
852#define NEW_RIPPER_VALUES(a,b,c,loc) (VALUE)rb_node_ripper_values_new(p,a,b,c,loc)
853
854#else
855static rb_node_scope_t *rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
856static rb_node_scope_t *rb_node_scope_new2(struct parser_params *p, rb_ast_id_table_t *nd_tbl, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
857static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
858static rb_node_if_t *rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc);
859static rb_node_unless_t *rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc);
860static rb_node_case_t *rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
861static rb_node_case2_t *rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
862static rb_node_case3_t *rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
863static rb_node_when_t *rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc);
864static rb_node_in_t *rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc);
865static rb_node_while_t *rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc);
866static rb_node_until_t *rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc);
867static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
868static rb_node_for_t *rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc);
869static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
870static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
871static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
872static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
873static rb_node_resbody_t *rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_body, NODE *nd_head, const YYLTYPE *loc);
874static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
875static rb_node_and_t *rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
876static rb_node_or_t *rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
877static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
878static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
879static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
880static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
881static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
882static rb_node_cdecl_t *rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, const YYLTYPE *loc);
883static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
884static rb_node_op_asgn1_t *rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc);
885static rb_node_op_asgn2_t *rb_node_op_asgn2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, ID nd_vid, ID nd_mid, bool nd_aid, const YYLTYPE *loc);
886static rb_node_op_asgn_or_t *rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc);
887static rb_node_op_asgn_and_t *rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc);
888static rb_node_op_cdecl_t *rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, const YYLTYPE *loc);
889static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
890static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
891static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
892static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
893static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
894static rb_node_super_t *rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc);
895static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
896static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
897static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
898static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
899static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
900static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc);
901static rb_node_yield_t *rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
902static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
903static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
904static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
905static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
906static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
907static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
908static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
909static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
910static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
911static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
912static rb_node_lit_t *rb_node_lit_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc);
913static rb_node_str_t *rb_node_str_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc);
914static rb_node_dstr_t *rb_node_dstr_new0(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
915static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc);
916static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc);
917static rb_node_dxstr_t *rb_node_dxstr_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
918static rb_node_evstr_t *rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
919static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
920static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
921static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, long nd_plen, const YYLTYPE *loc);
922static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
923static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
924static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
925static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
926static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
927static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
928static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
929static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
930static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
931static rb_node_alias_t *rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
932static rb_node_valias_t *rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc);
933static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
934static rb_node_class_t *rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc);
935static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc);
936static rb_node_sclass_t *rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc);
937static rb_node_colon2_t *rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc);
938static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
939static rb_node_dot2_t *rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc);
940static rb_node_dot3_t *rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc);
941static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
942static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
943static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc);
944static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
945static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
946static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
947static rb_node_postexe_t *rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
948static rb_node_dsym_t *rb_node_dsym_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
949static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
950static rb_node_lambda_t *rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
951static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
952static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
953static rb_node_fndptn_t *rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
954static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
955
956#define NEW_SCOPE(a,b,loc) (NODE *)rb_node_scope_new(p,a,b,loc)
957#define NEW_SCOPE2(t,a,b,loc) (NODE *)rb_node_scope_new2(p,t,a,b,loc)
958#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
959#define NEW_IF(c,t,e,loc) (NODE *)rb_node_if_new(p,c,t,e,loc)
960#define NEW_UNLESS(c,t,e,loc) (NODE *)rb_node_unless_new(p,c,t,e,loc)
961#define NEW_CASE(h,b,loc) (NODE *)rb_node_case_new(p,h,b,loc)
962#define NEW_CASE2(b,loc) (NODE *)rb_node_case2_new(p,b,loc)
963#define NEW_CASE3(h,b,loc) (NODE *)rb_node_case3_new(p,h,b,loc)
964#define NEW_WHEN(c,t,e,loc) (NODE *)rb_node_when_new(p,c,t,e,loc)
965#define NEW_IN(c,t,e,loc) (NODE *)rb_node_in_new(p,c,t,e,loc)
966#define NEW_WHILE(c,b,n,loc) (NODE *)rb_node_while_new(p,c,b,n,loc)
967#define NEW_UNTIL(c,b,n,loc) (NODE *)rb_node_until_new(p,c,b,n,loc)
968#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
969#define NEW_FOR(i,b,loc) (NODE *)rb_node_for_new(p,i,b,loc)
970#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
971#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
972#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
973#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
974#define NEW_RESBODY(a,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,ex,n,loc)
975#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
976#define NEW_AND(f,s,loc) (NODE *)rb_node_and_new(p,f,s,loc)
977#define NEW_OR(f,s,loc) (NODE *)rb_node_or_new(p,f,s,loc)
978#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
979#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
980#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
981#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
982#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
983#define NEW_CDECL(v,val,path,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,loc)
984#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
985#define NEW_OP_ASGN1(r,id,idx,rval,loc) (NODE *)rb_node_op_asgn1_new(p,r,id,idx,rval,loc)
986#define NEW_OP_ASGN2(r,t,i,o,val,loc) (NODE *)rb_node_op_asgn2_new(p,r,val,i,o,t,loc)
987#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
988#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
989#define NEW_OP_CDECL(v,op,val,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,loc)
990#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
991#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
992#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
993#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
994#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
995#define NEW_SUPER(a,loc) (NODE *)rb_node_super_new(p,a,loc)
996#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
997#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
998#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
999#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1000#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1001#define NEW_RETURN(s,loc) (NODE *)rb_node_return_new(p,s,loc)
1002#define NEW_YIELD(a,loc) (NODE *)rb_node_yield_new(p,a,loc)
1003#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1004#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1005#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1006#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1007#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1008#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1009#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1010#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1011#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1012#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1013#define NEW_LIT(l,loc) (NODE *)rb_node_lit_new(p,l,loc)
1014#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1015#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1016#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1017#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1018#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1019#define NEW_EVSTR(n,loc) (NODE *)rb_node_evstr_new(p,n,loc)
1020#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1021#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1022#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1023#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1024#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1025#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1026#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1027#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1028#define NEW_SPLAT(a,loc) (NODE *)rb_node_splat_new(p,a,loc)
1029#define NEW_BLOCK_PASS(b,loc) rb_node_block_pass_new(p,b,loc)
1030#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1031#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1032#define NEW_ALIAS(n,o,loc) (NODE *)rb_node_alias_new(p,n,o,loc)
1033#define NEW_VALIAS(n,o,loc) (NODE *)rb_node_valias_new(p,n,o,loc)
1034#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1035#define NEW_CLASS(n,b,s,loc) (NODE *)rb_node_class_new(p,n,b,s,loc)
1036#define NEW_MODULE(n,b,loc) (NODE *)rb_node_module_new(p,n,b,loc)
1037#define NEW_SCLASS(r,b,loc) (NODE *)rb_node_sclass_new(p,r,b,loc)
1038#define NEW_COLON2(c,i,loc) (NODE *)rb_node_colon2_new(p,c,i,loc)
1039#define NEW_COLON3(i,loc) (NODE *)rb_node_colon3_new(p,i,loc)
1040#define NEW_DOT2(b,e,loc) (NODE *)rb_node_dot2_new(p,b,e,loc)
1041#define NEW_DOT3(b,e,loc) (NODE *)rb_node_dot3_new(p,b,e,loc)
1042#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
1043#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
1044#define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc)
1045#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
1046#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
1047#define NEW_DEFINED(e,loc) (NODE *)rb_node_defined_new(p,e,loc)
1048#define NEW_POSTEXE(b,loc) (NODE *)rb_node_postexe_new(p,b,loc)
1049#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1050#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1051#define NEW_LAMBDA(a,b,loc) (NODE *)rb_node_lambda_new(p,a,b,loc)
1052#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1053#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1054#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1055#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1056
1057#endif
1058
1059enum internal_node_type {
1060 NODE_INTERNAL_ONLY = NODE_LAST,
1061 NODE_DEF_TEMP,
1062 NODE_EXITS,
1063 NODE_INTERNAL_LAST
1064};
1065
1066static const char *
1067parser_node_name(int node)
1068{
1069 switch (node) {
1070 case NODE_DEF_TEMP:
1071 return "NODE_DEF_TEMP";
1072 case NODE_EXITS:
1073 return "NODE_EXITS";
1074 default:
1075 return ruby_node_name(node);
1076 }
1077}
1078
1079/* This node is parse.y internal */
1080struct RNode_DEF_TEMP {
1081 NODE node;
1082
1083 /* for NODE_DEFN/NODE_DEFS */
1084#ifndef RIPPER
1085 struct RNode *nd_def;
1086 ID nd_mid;
1087#else
1088 VALUE nd_recv;
1089 VALUE nd_mid;
1090 VALUE dot_or_colon;
1091#endif
1092
1093 struct {
1094 ID cur_arg;
1095 int max_numparam;
1096 NODE *numparam_save;
1097 struct lex_context ctxt;
1098 } save;
1099};
1100
1101#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1102
1103static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc);
1104static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc);
1105static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc);
1106static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1107static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1108
1109#define NEW_BREAK(s,loc) (NODE *)rb_node_break_new(p,s,loc)
1110#define NEW_NEXT(s,loc) (NODE *)rb_node_next_new(p,s,loc)
1111#define NEW_REDO(loc) (NODE *)rb_node_redo_new(p,loc)
1112#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1113
1114/* Make a new internal node, which should not be appeared in the
1115 * result AST and does not have node_id and location. */
1116static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1117#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1118
1119static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1120
1121static int
1122parser_get_node_id(struct parser_params *p)
1123{
1124 int node_id = p->node_id;
1125 p->node_id++;
1126 return node_id;
1127}
1128
1129static void
1130anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1131{
1132 if (id == tANDDOT) {
1133 yyerror1(loc, "&. inside multiple assignment destination");
1134 }
1135}
1136
1137#ifndef RIPPER
1138static inline void
1139set_line_body(NODE *body, int line)
1140{
1141 if (!body) return;
1142 switch (nd_type(body)) {
1143 case NODE_RESCUE:
1144 case NODE_ENSURE:
1145 nd_set_line(body, line);
1146 }
1147}
1148
1149static void
1150set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1151{
1152 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1153 nd_set_line(node, beg->end_pos.lineno);
1154}
1155
1156static NODE *
1157last_expr_node(NODE *expr)
1158{
1159 while (expr) {
1160 if (nd_type_p(expr, NODE_BLOCK)) {
1161 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1162 }
1163 else if (nd_type_p(expr, NODE_BEGIN)) {
1164 expr = RNODE_BEGIN(expr)->nd_body;
1165 }
1166 else {
1167 break;
1168 }
1169 }
1170 return expr;
1171}
1172
1173#define yyparse ruby_yyparse
1174
1175static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1176static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1177#define new_nil(loc) NEW_NIL(loc)
1178static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1179static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
1180static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
1181static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1182
1183static NODE *newline_node(NODE*);
1184static void fixpos(NODE*,NODE*);
1185
1186static int value_expr_gen(struct parser_params*,NODE*);
1187static void void_expr(struct parser_params*,NODE*);
1188static NODE *remove_begin(NODE*);
1189#define value_expr(node) value_expr_gen(p, (node))
1190static NODE *void_stmts(struct parser_params*,NODE*);
1191static void reduce_nodes(struct parser_params*,NODE**);
1192static void block_dup_check(struct parser_params*,NODE*,NODE*);
1193
1194static NODE *block_append(struct parser_params*,NODE*,NODE*);
1195static NODE *list_append(struct parser_params*,NODE*,NODE*);
1196static NODE *list_concat(NODE*,NODE*);
1197static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1198static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1199static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1200static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1201static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
1202static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1203static NODE *str2dstr(struct parser_params*,NODE*);
1204static NODE *evstr2dstr(struct parser_params*,NODE*);
1205static NODE *splat_array(NODE*);
1206static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1207
1208static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1209static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1210static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1211static 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);
1212static NODE *method_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc) {RNODE_ITER(b)->nd_iter = m; b->nd_loc = *loc; return b;}
1213
1214static bool args_info_empty_p(struct rb_args_info *args);
1215static rb_node_args_t *new_args(struct parser_params*,rb_node_args_aux_t*,rb_node_opt_arg_t*,ID,rb_node_args_aux_t*,rb_node_args_t*,const YYLTYPE*);
1216static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1217static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1218static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1219static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1220static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1221static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1222static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1223
1224static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1225static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int);
1226
1227static VALUE negate_lit(struct parser_params*, VALUE);
1228static NODE *ret_args(struct parser_params*,NODE*);
1229static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1230static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
1231static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1232
1233static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1234static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1235
1236static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1237static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1238
1239static void rb_backref_error(struct parser_params*,NODE*);
1240static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1241
1242static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1243static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
1244static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
1245static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1246static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1247
1248static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1249
1250static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1251static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1252
1253static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1254static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1255
1256static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
1257
1258static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
1259
1260#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1261
1262static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1263
1264static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1265
1266static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1267
1268static rb_ast_id_table_t *local_tbl(struct parser_params*);
1269
1270static VALUE reg_compile(struct parser_params*, VALUE, int);
1271static void reg_fragment_setenc(struct parser_params*, VALUE, int);
1272static int reg_fragment_check(struct parser_params*, VALUE, int);
1273
1274static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
1275static NODE *heredoc_dedent(struct parser_params*,NODE*);
1276
1277static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1278
1279#define get_id(id) (id)
1280#define get_value(val) (val)
1281#define get_num(num) (num)
1282#else /* RIPPER */
1283
1284static inline int ripper_is_node_yylval(struct parser_params *p, VALUE n);
1285
1286static inline VALUE
1287ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
1288{
1289 if (ripper_is_node_yylval(p, c)) c = RNODE_RIPPER(c)->nd_cval;
1290 add_mark_object(p, b);
1291 add_mark_object(p, c);
1292 return NEW_RIPPER(a, b, c, &NULL_LOC);
1293}
1294
1295static inline VALUE
1296ripper_new_yylval2(struct parser_params *p, VALUE a, VALUE b, VALUE c)
1297{
1298 add_mark_object(p, a);
1299 add_mark_object(p, b);
1300 add_mark_object(p, c);
1301 return NEW_RIPPER_VALUES(a, b, c, &NULL_LOC);
1302}
1303
1304static inline int
1305ripper_is_node_yylval(struct parser_params *p, VALUE n)
1306{
1307 return RB_TYPE_P(n, T_NODE) && nd_type_p(RNODE(n), NODE_RIPPER);
1308}
1309
1310#define value_expr(node) ((void)(node))
1311#define remove_begin(node) (node)
1312#define void_stmts(p,x) (x)
1313#undef rb_dvar_defined
1314#define rb_dvar_defined(id, base) 0
1315#undef rb_local_defined
1316#define rb_local_defined(id, base) 0
1317#define get_id(id) ripper_get_id(id)
1318#define get_value(val) ripper_get_value(val)
1319#define get_num(num) (int)get_id(num)
1320static VALUE assignable(struct parser_params*,VALUE);
1321static int id_is_var(struct parser_params *p, ID id);
1322
1323#define method_cond(p,node,loc) (node)
1324#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
1325#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
1326#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
1327#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
1328
1329#define new_nil(loc) Qnil
1330
1331static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
1332
1333static VALUE const_decl(struct parser_params *p, VALUE path);
1334
1335static VALUE var_field(struct parser_params *p, VALUE a);
1336static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1337
1338static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
1339
1340static VALUE backref_error(struct parser_params*, NODE *, VALUE);
1341#endif /* !RIPPER */
1342
1343RUBY_SYMBOL_EXPORT_BEGIN
1344VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1345int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
1346enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1347VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1348void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1349PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1350YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1351YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1352YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1353YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1354YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1355YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1356RUBY_SYMBOL_EXPORT_END
1357
1358static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1359static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1360#ifndef RIPPER
1361static ID formal_argument(struct parser_params*, ID);
1362#else
1363static ID formal_argument(struct parser_params*, VALUE);
1364#endif
1365static ID shadowing_lvar(struct parser_params*,ID);
1366static void new_bv(struct parser_params*,ID);
1367
1368static void local_push(struct parser_params*,int);
1369static void local_pop(struct parser_params*);
1370static void local_var(struct parser_params*, ID);
1371static void arg_var(struct parser_params*, ID);
1372static int local_id(struct parser_params *p, ID id);
1373static int local_id_ref(struct parser_params*, ID, ID **);
1374#ifndef RIPPER
1375static ID internal_id(struct parser_params*);
1376static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1377#endif
1378static int check_forwarding_args(struct parser_params*);
1379static void add_forwarding_args(struct parser_params *p);
1380static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1381
1382static const struct vtable *dyna_push(struct parser_params *);
1383static void dyna_pop(struct parser_params*, const struct vtable *);
1384static int dyna_in_block(struct parser_params*);
1385#define dyna_var(p, id) local_var(p, id)
1386static int dvar_defined(struct parser_params*, ID);
1387static int dvar_defined_ref(struct parser_params*, ID, ID**);
1388static int dvar_curr(struct parser_params*,ID);
1389
1390static int lvar_defined(struct parser_params*, ID);
1391
1392static NODE *numparam_push(struct parser_params *p);
1393static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1394
1395#ifdef RIPPER
1396# define METHOD_NOT idNOT
1397#else
1398# define METHOD_NOT '!'
1399#endif
1400
1401#define idFWD_REST '*'
1402#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1403#define idFWD_BLOCK '&'
1404#define idFWD_ALL idDot3
1405#ifdef RIPPER
1406#define arg_FWD_BLOCK Qnone
1407#else
1408#define arg_FWD_BLOCK idFWD_BLOCK
1409#endif
1410#define FORWARD_ARGS_WITH_RUBY2_KEYWORDS
1411
1412#define RE_OPTION_ONCE (1<<16)
1413#define RE_OPTION_ENCODING_SHIFT 8
1414#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1415#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1416#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1417#define RE_OPTION_MASK 0xff
1418#define RE_OPTION_ARG_ENCODING_NONE 32
1419
1420#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1421size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1422
1423#define TOKEN2ID(tok) ( \
1424 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1425 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1426 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1427 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1428 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1429 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1430 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1431
1432/****** Ripper *******/
1433
1434#ifdef RIPPER
1435
1436#include "eventids1.h"
1437#include "eventids2.h"
1438
1439extern const struct ripper_parser_ids ripper_parser_ids;
1440
1441static VALUE ripper_dispatch0(struct parser_params*,ID);
1442static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1443static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1444static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1445static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1446static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1447static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1448void ripper_error(struct parser_params *p);
1449
1450#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
1451#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
1452#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
1453#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
1454#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
1455#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
1456#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))
1457
1458#define yyparse ripper_yyparse
1459
1460#define ID2VAL(id) STATIC_ID2SYM(id)
1461#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1462#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
1463
1464#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
1465 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
1466
1467static inline VALUE
1468new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
1469{
1470 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(tail);
1471 VALUE kw_args = t->nd_val1, kw_rest_arg = t->nd_val2, block = t->nd_val3;
1472 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, block);
1473}
1474
1475static inline VALUE
1476new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
1477{
1478 return ripper_new_yylval2(p, kw_args, kw_rest_arg, block);
1479}
1480
1481static inline VALUE
1482args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
1483{
1484 return args;
1485}
1486
1487static VALUE
1488new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
1489{
1490 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(aryptn);
1491 VALUE pre_args = t->nd_val1, rest_arg = t->nd_val2, post_args = t->nd_val3;
1492
1493 if (!NIL_P(pre_arg)) {
1494 if (!NIL_P(pre_args)) {
1495 rb_ary_unshift(pre_args, pre_arg);
1496 }
1497 else {
1498 pre_args = rb_ary_new_from_args(1, pre_arg);
1499 }
1500 }
1501 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
1502}
1503
1504static VALUE
1505new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
1506{
1507 return ripper_new_yylval2(p, pre_args, rest_arg, post_args);
1508}
1509
1510static VALUE
1511new_find_pattern(struct parser_params *p, VALUE constant, VALUE fndptn, const YYLTYPE *loc)
1512{
1513 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(fndptn);
1514 VALUE pre_rest_arg = t->nd_val1, args = t->nd_val2, post_rest_arg = t->nd_val3;
1515
1516 return dispatch4(fndptn, constant, pre_rest_arg, args, post_rest_arg);
1517}
1518
1519static VALUE
1520new_find_pattern_tail(struct parser_params *p, VALUE pre_rest_arg, VALUE args, VALUE post_rest_arg, const YYLTYPE *loc)
1521{
1522 return ripper_new_yylval2(p, pre_rest_arg, args, post_rest_arg);
1523}
1524
1525#define new_hash(p,h,l) rb_ary_new_from_args(0)
1526
1527static VALUE
1528new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
1529{
1530 return ary;
1531}
1532
1533static VALUE
1534new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
1535{
1536 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(hshptn);
1537 VALUE kw_args = t->nd_val1, kw_rest_arg = t->nd_val2;
1538 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
1539}
1540
1541static VALUE
1542new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
1543{
1544 if (kw_rest_arg) {
1545 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
1546 }
1547 else {
1548 kw_rest_arg = Qnil;
1549 }
1550 return ripper_new_yylval2(p, kw_args, kw_rest_arg, Qnil);
1551}
1552
1553#define new_defined(p,expr,loc) dispatch1(defined, (expr))
1554
1555static VALUE heredoc_dedent(struct parser_params*,VALUE);
1556
1557#else
1558#define ID2VAL(id) (id)
1559#define TOKEN2VAL(t) ID2VAL(t)
1560#define KWD2EID(t, v) keyword_##t
1561
1562static NODE *
1563new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, const YYLTYPE *loc)
1564{
1565 body = remove_begin(body);
1566 reduce_nodes(p, &body);
1567 NODE *n = NEW_SCOPE(args, body, loc);
1568 nd_set_line(n, loc->end_pos.lineno);
1569 set_line_body(body, loc->beg_pos.lineno);
1570 return n;
1571}
1572
1573static NODE *
1574rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1575 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1576{
1577 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1578 rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc);
1579 loc.beg_pos = arg_loc->beg_pos;
1580 return NEW_RESCUE(arg, rescue, 0, &loc);
1581}
1582
1583#endif /* RIPPER */
1584
1585static NODE *add_block_exit(struct parser_params *p, NODE *node);
1586static rb_node_exits_t *init_block_exit(struct parser_params *p);
1587static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1588static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1589static void clear_block_exit(struct parser_params *p, bool error);
1590
1591static void
1592next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1593{
1594 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1595}
1596
1597static void
1598restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1599{
1600 /* See: def_name action */
1601 struct lex_context ctxt = temp->save.ctxt;
1602 p->cur_arg = temp->save.cur_arg;
1603 p->ctxt.in_def = ctxt.in_def;
1604 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1605 p->ctxt.in_rescue = ctxt.in_rescue;
1606 p->max_numparam = temp->save.max_numparam;
1607 numparam_pop(p, temp->save.numparam_save);
1608 clear_block_exit(p, true);
1609}
1610
1611static void
1612endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1613{
1614 if (is_attrset_id(mid)) {
1615 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1616 }
1617 token_info_drop(p, "def", loc->beg_pos);
1618}
1619
1620#define debug_token_line(p, name, line) do { \
1621 if (p->debug) { \
1622 const char *const pcur = p->lex.pcur; \
1623 const char *const ptok = p->lex.ptok; \
1624 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1625 line, p->ruby_sourceline, \
1626 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1627 } \
1628 } while (0)
1629
1630#define begin_definition(k, loc_beg, loc_end) \
1631 do { \
1632 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1633 p->ctxt.in_def = 0; \
1634 } \
1635 else if (p->ctxt.in_def) { \
1636 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1637 yyerror1(&loc, k " definition in method body"); \
1638 } \
1639 local_push(p, 0); \
1640 } while (0)
1641
1642#ifndef RIPPER
1643# define Qnone 0
1644# define Qnull 0
1645# define ifndef_ripper(x) (x)
1646#else
1647# define Qnone Qnil
1648# define Qnull Qundef
1649# define ifndef_ripper(x)
1650#endif
1651
1652# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1653# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1654# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1655# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1656# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1657# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1658# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1659# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1660# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1661# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1662# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1663# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1664# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1665# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1666# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1667# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1668# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1669# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1670# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1671# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1672#ifdef RIPPER
1673extern const ID id_warn, id_warning, id_gets, id_assoc;
1674# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1675# define WARN_S_L(s,l) STR_NEW(s,l)
1676# define WARN_S(s) STR_NEW2(s)
1677# define WARN_I(i) INT2NUM(i)
1678# define WARN_ID(i) rb_id2str(i)
1679# define WARN_IVAL(i) i
1680# define PRIsWARN "s"
1681# define rb_warn0L_experimental(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1682# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1683# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1684# ifdef HAVE_VA_ARGS_MACRO
1685# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1686# else
1687# define WARN_CALL rb_funcall
1688# endif
1689# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1690# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1691# ifdef HAVE_VA_ARGS_MACRO
1692# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1693# else
1694# define WARNING_CALL rb_funcall
1695# endif
1696# define compile_error ripper_compile_error
1697#else
1698# define WARN_S_L(s,l) s
1699# define WARN_S(s) s
1700# define WARN_I(i) i
1701# define WARN_ID(i) rb_id2name(i)
1702# define WARN_IVAL(i) NUM2INT(i)
1703# define PRIsWARN PRIsVALUE
1704# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1705# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1706# define WARN_CALL rb_compile_warn
1707# define rb_warn0L_experimental(l,fmt) rb_category_compile_warn(RB_WARN_CATEGORY_EXPERIMENTAL, WARN_ARGS_L(l, fmt, 1))
1708# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1709# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1710# define WARNING_CALL rb_compile_warning
1711PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1712# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1713#endif
1714
1715struct RNode_EXITS {
1716 NODE node;
1717
1718 NODE *nd_chain; /* Assume NODE_BREAK, NODE_NEXT, NODE_REDO have nd_chain here */
1719 NODE *nd_end;
1720};
1721
1722#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1723
1724static NODE *
1725add_block_exit(struct parser_params *p, NODE *node)
1726{
1727 if (!node) {
1728 compile_error(p, "unexpected null node");
1729 return 0;
1730 }
1731 switch (nd_type(node)) {
1732 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1733 default:
1734 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1735 return node;
1736 }
1737 if (!p->ctxt.in_defined) {
1738 rb_node_exits_t *exits = p->exits;
1739 if (exits) {
1740 RNODE_EXITS(exits->nd_end)->nd_chain = node;
1741 exits->nd_end = node;
1742 }
1743 }
1744 return node;
1745}
1746
1747static rb_node_exits_t *
1748init_block_exit(struct parser_params *p)
1749{
1750 rb_node_exits_t *old = p->exits;
1751 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1752 exits->nd_chain = 0;
1753 exits->nd_end = RNODE(exits);
1754 p->exits = exits;
1755 return old;
1756}
1757
1758static rb_node_exits_t *
1759allow_block_exit(struct parser_params *p)
1760{
1761 rb_node_exits_t *exits = p->exits;
1762 p->exits = 0;
1763 return exits;
1764}
1765
1766static void
1767restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1768{
1769 p->exits = exits;
1770}
1771
1772static void
1773clear_block_exit(struct parser_params *p, bool error)
1774{
1775 rb_node_exits_t *exits = p->exits;
1776 if (!exits) return;
1777 if (error && !compile_for_eval) {
1778 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1779 switch (nd_type(e)) {
1780 case NODE_BREAK:
1781 yyerror1(&e->nd_loc, "Invalid break");
1782 break;
1783 case NODE_NEXT:
1784 yyerror1(&e->nd_loc, "Invalid next");
1785 break;
1786 case NODE_REDO:
1787 yyerror1(&e->nd_loc, "Invalid redo");
1788 break;
1789 default:
1790 yyerror1(&e->nd_loc, "unexpected node");
1791 goto end_checks; /* no nd_chain */
1792 }
1793 }
1794 end_checks:;
1795 }
1796 exits->nd_end = RNODE(exits);
1797 exits->nd_chain = 0;
1798}
1799
1800#define WARN_EOL(tok) \
1801 (looking_at_eol_p(p) ? \
1802 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
1803 (void)0)
1804static int looking_at_eol_p(struct parser_params *p);
1805
1806#ifndef RIPPER
1807static NODE *
1808get_nd_value(struct parser_params *p, NODE *node)
1809{
1810 switch (nd_type(node)) {
1811 case NODE_GASGN:
1812 return RNODE_GASGN(node)->nd_value;
1813 case NODE_IASGN:
1814 return RNODE_IASGN(node)->nd_value;
1815 case NODE_LASGN:
1816 return RNODE_LASGN(node)->nd_value;
1817 case NODE_DASGN:
1818 return RNODE_DASGN(node)->nd_value;
1819 case NODE_MASGN:
1820 return RNODE_MASGN(node)->nd_value;
1821 case NODE_CVASGN:
1822 return RNODE_CVASGN(node)->nd_value;
1823 case NODE_CDECL:
1824 return RNODE_CDECL(node)->nd_value;
1825 default:
1826 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1827 return 0;
1828 }
1829}
1830
1831static void
1832set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1833{
1834 switch (nd_type(node)) {
1835 case NODE_CDECL:
1836 RNODE_CDECL(node)->nd_value = rhs;
1837 break;
1838 case NODE_GASGN:
1839 RNODE_GASGN(node)->nd_value = rhs;
1840 break;
1841 case NODE_IASGN:
1842 RNODE_IASGN(node)->nd_value = rhs;
1843 break;
1844 case NODE_LASGN:
1845 RNODE_LASGN(node)->nd_value = rhs;
1846 break;
1847 case NODE_DASGN:
1848 RNODE_DASGN(node)->nd_value = rhs;
1849 break;
1850 case NODE_MASGN:
1851 RNODE_MASGN(node)->nd_value = rhs;
1852 break;
1853 case NODE_CVASGN:
1854 RNODE_CVASGN(node)->nd_value = rhs;
1855 break;
1856 default:
1857 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1858 break;
1859 }
1860}
1861
1862static ID
1863get_nd_vid(struct parser_params *p, NODE *node)
1864{
1865 switch (nd_type(node)) {
1866 case NODE_CDECL:
1867 return RNODE_CDECL(node)->nd_vid;
1868 case NODE_GASGN:
1869 return RNODE_GASGN(node)->nd_vid;
1870 case NODE_IASGN:
1871 return RNODE_IASGN(node)->nd_vid;
1872 case NODE_LASGN:
1873 return RNODE_LASGN(node)->nd_vid;
1874 case NODE_DASGN:
1875 return RNODE_DASGN(node)->nd_vid;
1876 case NODE_CVASGN:
1877 return RNODE_CVASGN(node)->nd_vid;
1878 default:
1879 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1880 return 0;
1881 }
1882}
1883
1884static NODE *
1885get_nd_args(struct parser_params *p, NODE *node)
1886{
1887 switch (nd_type(node)) {
1888 case NODE_CALL:
1889 return RNODE_CALL(node)->nd_args;
1890 case NODE_OPCALL:
1891 return RNODE_OPCALL(node)->nd_args;
1892 case NODE_FCALL:
1893 return RNODE_FCALL(node)->nd_args;
1894 case NODE_QCALL:
1895 return RNODE_QCALL(node)->nd_args;
1896 case NODE_VCALL:
1897 case NODE_SUPER:
1898 case NODE_ZSUPER:
1899 case NODE_YIELD:
1900 case NODE_RETURN:
1901 case NODE_BREAK:
1902 case NODE_NEXT:
1903 return 0;
1904 default:
1905 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1906 return 0;
1907 }
1908}
1909#endif
1910%}
1911
1912%expect 0
1913%define api.pure
1914%define parse.error verbose
1915%printer {
1916#ifndef RIPPER
1917 if ((NODE *)$$ == (NODE *)-1) {
1918 rb_parser_printf(p, "NODE_SPECIAL");
1919 }
1920 else if ($$) {
1921 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
1922 }
1923#else
1924#endif
1925} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg> <node_kw_arg> <node_block_pass>
1926%printer {
1927#ifndef RIPPER
1928 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
1929#else
1930 rb_parser_printf(p, "%"PRIsVALUE, RNODE_RIPPER($$)->nd_rval);
1931#endif
1932} tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN
1933%printer {
1934#ifndef RIPPER
1935 rb_parser_printf(p, "%+"PRIsVALUE, RNODE_LIT($$)->nd_lit);
1936#else
1937 rb_parser_printf(p, "%+"PRIsVALUE, get_value($$));
1938#endif
1939} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
1940%printer {
1941#ifndef RIPPER
1942 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
1943#else
1944 rb_parser_printf(p, "%"PRIsVALUE, $$);
1945#endif
1946} tNTH_REF
1947%printer {
1948#ifndef RIPPER
1949 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
1950#else
1951 rb_parser_printf(p, "%"PRIsVALUE, $$);
1952#endif
1953} tBACK_REF
1954
1955%lex-param {struct parser_params *p}
1956%parse-param {struct parser_params *p}
1957%initial-action
1958{
1959 RUBY_SET_YYLLOC_OF_NONE(@$);
1960};
1961
1962%union {
1963 VALUE val;
1964 NODE *node;
1965 rb_node_fcall_t *node_fcall;
1966 rb_node_args_t *node_args;
1967 rb_node_args_aux_t *node_args_aux;
1968 rb_node_opt_arg_t *node_opt_arg;
1969 rb_node_kw_arg_t *node_kw_arg;
1970 rb_node_block_pass_t *node_block_pass;
1971 rb_node_masgn_t *node_masgn;
1972 rb_node_def_temp_t *node_def_temp;
1973 rb_node_exits_t *node_exits;
1974 ID id;
1975 int num;
1976 st_table *tbl;
1977 const struct vtable *vars;
1978 struct rb_strterm_struct *strterm;
1979 struct lex_context ctxt;
1980}
1981
1982%token <id>
1983 keyword_class "`class'"
1984 keyword_module "`module'"
1985 keyword_def "`def'"
1986 keyword_undef "`undef'"
1987 keyword_begin "`begin'"
1988 keyword_rescue "`rescue'"
1989 keyword_ensure "`ensure'"
1990 keyword_end "`end'"
1991 keyword_if "`if'"
1992 keyword_unless "`unless'"
1993 keyword_then "`then'"
1994 keyword_elsif "`elsif'"
1995 keyword_else "`else'"
1996 keyword_case "`case'"
1997 keyword_when "`when'"
1998 keyword_while "`while'"
1999 keyword_until "`until'"
2000 keyword_for "`for'"
2001 keyword_break "`break'"
2002 keyword_next "`next'"
2003 keyword_redo "`redo'"
2004 keyword_retry "`retry'"
2005 keyword_in "`in'"
2006 keyword_do "`do'"
2007 keyword_do_cond "`do' for condition"
2008 keyword_do_block "`do' for block"
2009 keyword_do_LAMBDA "`do' for lambda"
2010 keyword_return "`return'"
2011 keyword_yield "`yield'"
2012 keyword_super "`super'"
2013 keyword_self "`self'"
2014 keyword_nil "`nil'"
2015 keyword_true "`true'"
2016 keyword_false "`false'"
2017 keyword_and "`and'"
2018 keyword_or "`or'"
2019 keyword_not "`not'"
2020 modifier_if "`if' modifier"
2021 modifier_unless "`unless' modifier"
2022 modifier_while "`while' modifier"
2023 modifier_until "`until' modifier"
2024 modifier_rescue "`rescue' modifier"
2025 keyword_alias "`alias'"
2026 keyword_defined "`defined?'"
2027 keyword_BEGIN "`BEGIN'"
2028 keyword_END "`END'"
2029 keyword__LINE__ "`__LINE__'"
2030 keyword__FILE__ "`__FILE__'"
2031 keyword__ENCODING__ "`__ENCODING__'"
2032
2033%token <id> tIDENTIFIER "local variable or method"
2034%token <id> tFID "method"
2035%token <id> tGVAR "global variable"
2036%token <id> tIVAR "instance variable"
2037%token <id> tCONSTANT "constant"
2038%token <id> tCVAR "class variable"
2039%token <id> tLABEL "label"
2040%token <node> tINTEGER "integer literal"
2041%token <node> tFLOAT "float literal"
2042%token <node> tRATIONAL "rational literal"
2043%token <node> tIMAGINARY "imaginary literal"
2044%token <node> tCHAR "char literal"
2045%token <node> tNTH_REF "numbered reference"
2046%token <node> tBACK_REF "back reference"
2047%token <node> tSTRING_CONTENT "literal content"
2048%token <num> tREGEXP_END
2049%token <num> tDUMNY_END "dummy end"
2050
2051%type <node> singleton strings string string1 xstring regexp
2052%type <node> string_contents xstring_contents regexp_contents string_content
2053%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2054%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2055/*ripper*/ %type <node_def_temp> defn_head defs_head k_def
2056/*ripper*/ %type <node_exits> block_open k_while k_until k_for allow_exits
2057%type <node> top_compstmt top_stmts top_stmt begin_block endless_arg endless_command
2058%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
2059%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2060%type <node_fcall> fcall
2061%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2062%type <node> args arg_splat call_args opt_call_args
2063%type <node> paren_args opt_paren_args
2064%type <node_args> args_tail opt_args_tail block_args_tail opt_block_args_tail
2065%type <node> command_args aref_args
2066%type <node_block_pass> opt_block_arg block_arg
2067%type <node> var_ref var_lhs
2068%type <node> command_rhs arg_rhs
2069%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2070%type <node_opt_arg> f_block_optarg f_block_opt
2071%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args
2072%type <node_args_aux> f_arg f_arg_item
2073%type <node_opt_arg> f_optarg
2074%type <node> f_marg f_marg_list f_rest_marg
2075%type <node_masgn> f_margs
2076%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2077%type <node_args> block_param opt_block_param block_param_def
2078%type <node_opt_arg> f_opt
2079%type <node_kw_arg> f_kwarg f_kw f_block_kwarg f_block_kw
2080%type <node> bv_decls opt_bv_decl bvar
2081%type <node> lambda lambda_body brace_body do_body
2082%type <node_args> f_larglist
2083%type <node> brace_block cmd_brace_block do_block lhs none fitem
2084%type <node> mlhs_head mlhs_item mlhs_node mlhs_post
2085%type <node_masgn> mlhs mlhs_basic mlhs_inner
2086%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2087%type <node> p_expr p_as p_alt p_expr_basic p_find
2088%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2089%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2090%type <node> p_kwargs p_kwarg p_kw
2091%type <id> keyword_variable user_variable sym operation operation2 operation3
2092%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
2093%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2094%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2095%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2096%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2097%type <ctxt> p_in_kwarg
2098%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2099/* ripper */ %type <num> max_numparam
2100/* ripper */ %type <node> numparam
2101%token END_OF_INPUT 0 "end-of-input"
2102%token <id> '.'
2103
2104/* escaped chars, should be ignored otherwise */
2105%token <id> '\\' "backslash"
2106%token tSP "escaped space"
2107%token <id> '\t' "escaped horizontal tab"
2108%token <id> '\f' "escaped form feed"
2109%token <id> '\r' "escaped carriage return"
2110%token <id> '\13' "escaped vertical tab"
2111%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2112%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2113%token tPOW RUBY_TOKEN(POW) "**"
2114%token tCMP RUBY_TOKEN(CMP) "<=>"
2115%token tEQ RUBY_TOKEN(EQ) "=="
2116%token tEQQ RUBY_TOKEN(EQQ) "==="
2117%token tNEQ RUBY_TOKEN(NEQ) "!="
2118%token tGEQ RUBY_TOKEN(GEQ) ">="
2119%token tLEQ RUBY_TOKEN(LEQ) "<="
2120%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2121%token tOROP RUBY_TOKEN(OROP) "||"
2122%token tMATCH RUBY_TOKEN(MATCH) "=~"
2123%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2124%token tDOT2 RUBY_TOKEN(DOT2) ".."
2125%token tDOT3 RUBY_TOKEN(DOT3) "..."
2126%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2127%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2128%token tAREF RUBY_TOKEN(AREF) "[]"
2129%token tASET RUBY_TOKEN(ASET) "[]="
2130%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2131%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2132%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2133%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2134%token tCOLON3 ":: at EXPR_BEG"
2135%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2136%token tASSOC "=>"
2137%token tLPAREN "("
2138%token tLPAREN_ARG "( arg"
2139%token tRPAREN ")"
2140%token tLBRACK "["
2141%token tLBRACE "{"
2142%token tLBRACE_ARG "{ arg"
2143%token tSTAR "*"
2144%token tDSTAR "**arg"
2145%token tAMPER "&"
2146%token tLAMBDA "->"
2147%token tSYMBEG "symbol literal"
2148%token tSTRING_BEG "string literal"
2149%token tXSTRING_BEG "backtick literal"
2150%token tREGEXP_BEG "regexp literal"
2151%token tWORDS_BEG "word list"
2152%token tQWORDS_BEG "verbatim word list"
2153%token tSYMBOLS_BEG "symbol list"
2154%token tQSYMBOLS_BEG "verbatim symbol list"
2155%token tSTRING_END "terminator"
2156%token tSTRING_DEND "'}'"
2157%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
2158
2159%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2160%token tHEREDOC_BEG tHEREDOC_END k__END__
2161
2162/*
2163 * precedence table
2164 */
2165
2166%nonassoc tLOWEST
2167%nonassoc tLBRACE_ARG
2168
2169%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2170%left keyword_or keyword_and
2171%right keyword_not
2172%nonassoc keyword_defined
2173%right '=' tOP_ASGN
2174%left modifier_rescue
2175%right '?' ':'
2176%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2177%left tOROP
2178%left tANDOP
2179%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2180%left '>' tGEQ '<' tLEQ
2181%left '|' '^'
2182%left '&'
2183%left tLSHFT tRSHFT
2184%left '+' '-'
2185%left '*' '/' '%'
2186%right tUMINUS_NUM tUMINUS
2187%right tPOW
2188%right '!' '~' tUPLUS
2189
2190%token tLAST_TOKEN
2191
2192%%
2193program : {
2194 SET_LEX_STATE(EXPR_BEG);
2195 local_push(p, ifndef_ripper(1)+0);
2196 /* jumps are possible in the top-level loop. */
2197 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
2198 }
2199 top_compstmt
2200 {
2201 /*%%%*/
2202 if ($2 && !compile_for_eval) {
2203 NODE *node = $2;
2204 /* last expression should not be void */
2205 if (nd_type_p(node, NODE_BLOCK)) {
2206 while (RNODE_BLOCK(node)->nd_next) {
2207 node = RNODE_BLOCK(node)->nd_next;
2208 }
2209 node = RNODE_BLOCK(node)->nd_head;
2210 }
2211 node = remove_begin(node);
2212 void_expr(p, node);
2213 }
2214 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
2215 /*% %*/
2216 /*% ripper[final]: program!($2) %*/
2217 local_pop(p);
2218 }
2219 ;
2220
2221top_compstmt : top_stmts opt_terms
2222 {
2223 $$ = void_stmts(p, $1);
2224 }
2225 ;
2226
2227top_stmts : none
2228 {
2229 /*%%%*/
2230 $$ = NEW_BEGIN(0, &@$);
2231 /*% %*/
2232 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
2233 }
2234 | top_stmt
2235 {
2236 /*%%%*/
2237 $$ = newline_node($1);
2238 /*% %*/
2239 /*% ripper: stmts_add!(stmts_new!, $1) %*/
2240 }
2241 | top_stmts terms top_stmt
2242 {
2243 /*%%%*/
2244 $$ = block_append(p, $1, newline_node($3));
2245 /*% %*/
2246 /*% ripper: stmts_add!($1, $3) %*/
2247 }
2248 ;
2249
2250top_stmt : stmt
2251 {
2252 clear_block_exit(p, true);
2253 $$ = $1;
2254 }
2255 | keyword_BEGIN begin_block
2256 {
2257 $$ = $2;
2258 }
2259 ;
2260
2261block_open : '{' {$$ = init_block_exit(p);};
2262
2263begin_block : block_open top_compstmt '}'
2264 {
2265 restore_block_exit(p, $block_open);
2266 /*%%%*/
2267 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
2268 NEW_BEGIN($2, &@$));
2269 $$ = NEW_BEGIN(0, &@$);
2270 /*% %*/
2271 /*% ripper: BEGIN!($2) %*/
2272 }
2273 ;
2274
2275bodystmt : compstmt[body]
2276 lex_ctxt[ctxt]
2277 opt_rescue
2278 k_else
2279 {
2280 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
2281 next_rescue_context(&p->ctxt, &$ctxt, after_else);
2282 }
2283 compstmt[elsebody]
2284 {
2285 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
2286 }
2287 opt_ensure
2288 {
2289 /*%%%*/
2290 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
2291 /*% %*/
2292 /*% ripper: bodystmt!($body, $opt_rescue, $elsebody, $opt_ensure) %*/
2293 }
2294 | compstmt[body]
2295 lex_ctxt[ctxt]
2296 opt_rescue
2297 {
2298 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
2299 }
2300 opt_ensure
2301 {
2302 /*%%%*/
2303 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
2304 /*% %*/
2305 /*% ripper: bodystmt!($body, $opt_rescue, Qnil, $opt_ensure) %*/
2306 }
2307 ;
2308
2309compstmt : stmts opt_terms
2310 {
2311 $$ = void_stmts(p, $1);
2312 }
2313 ;
2314
2315stmts : none
2316 {
2317 /*%%%*/
2318 $$ = NEW_BEGIN(0, &@$);
2319 /*% %*/
2320 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
2321 }
2322 | stmt_or_begin
2323 {
2324 /*%%%*/
2325 $$ = newline_node($1);
2326 /*% %*/
2327 /*% ripper: stmts_add!(stmts_new!, $1) %*/
2328 }
2329 | stmts terms stmt_or_begin
2330 {
2331 /*%%%*/
2332 $$ = block_append(p, $1, newline_node($3));
2333 /*% %*/
2334 /*% ripper: stmts_add!($1, $3) %*/
2335 }
2336 ;
2337
2338stmt_or_begin : stmt
2339 {
2340 $$ = $1;
2341 }
2342 | keyword_BEGIN
2343 {
2344 yyerror1(&@1, "BEGIN is permitted only at toplevel");
2345 }
2346 begin_block
2347 {
2348 $$ = $3;
2349 }
2350 ;
2351
2352allow_exits : {$$ = allow_block_exit(p);};
2353
2354k_END : keyword_END lex_ctxt
2355 {
2356 $$ = $2;
2357 p->ctxt.in_rescue = before_rescue;
2358 };
2359
2360stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2361 {
2362 /*%%%*/
2363 $$ = NEW_ALIAS($2, $4, &@$);
2364 /*% %*/
2365 /*% ripper: alias!($2, $4) %*/
2366 }
2367 | keyword_alias tGVAR tGVAR
2368 {
2369 /*%%%*/
2370 $$ = NEW_VALIAS($2, $3, &@$);
2371 /*% %*/
2372 /*% ripper: var_alias!($2, $3) %*/
2373 }
2374 | keyword_alias tGVAR tBACK_REF
2375 {
2376 /*%%%*/
2377 char buf[2];
2378 buf[0] = '$';
2379 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
2380 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
2381 /*% %*/
2382 /*% ripper: var_alias!($2, $3) %*/
2383 }
2384 | keyword_alias tGVAR tNTH_REF
2385 {
2386 static const char mesg[] = "can't make alias for the number variables";
2387 /*%%%*/
2388 yyerror1(&@3, mesg);
2389 $$ = NEW_ERROR(&@$);
2390 /*% %*/
2391 /*% ripper[error]: alias_error!(ERR_MESG(), $3) %*/
2392 }
2393 | keyword_undef undef_list
2394 {
2395 /*%%%*/
2396 $$ = $2;
2397 /*% %*/
2398 /*% ripper: undef!($2) %*/
2399 }
2400 | stmt modifier_if expr_value
2401 {
2402 /*%%%*/
2403 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
2404 fixpos($$, $3);
2405 /*% %*/
2406 /*% ripper: if_mod!($3, $1) %*/
2407 }
2408 | stmt modifier_unless expr_value
2409 {
2410 /*%%%*/
2411 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
2412 fixpos($$, $3);
2413 /*% %*/
2414 /*% ripper: unless_mod!($3, $1) %*/
2415 }
2416 | stmt modifier_while expr_value
2417 {
2418 clear_block_exit(p, false);
2419 /*%%%*/
2420 if ($1 && nd_type_p($1, NODE_BEGIN)) {
2421 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$);
2422 }
2423 else {
2424 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
2425 }
2426 /*% %*/
2427 /*% ripper: while_mod!($3, $1) %*/
2428 }
2429 | stmt modifier_until expr_value
2430 {
2431 clear_block_exit(p, false);
2432 /*%%%*/
2433 if ($1 && nd_type_p($1, NODE_BEGIN)) {
2434 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$);
2435 }
2436 else {
2437 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
2438 }
2439 /*% %*/
2440 /*% ripper: until_mod!($3, $1) %*/
2441 }
2442 | stmt modifier_rescue after_rescue stmt
2443 {
2444 p->ctxt.in_rescue = $3.in_rescue;
2445 /*%%%*/
2446 NODE *resq;
2447 YYLTYPE loc = code_loc_gen(&@2, &@4);
2448 resq = NEW_RESBODY(0, remove_begin($4), 0, &loc);
2449 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
2450 /*% %*/
2451 /*% ripper: rescue_mod!($1, $4) %*/
2452 }
2453 | k_END allow_exits '{' compstmt '}'
2454 {
2455 if (p->ctxt.in_def) {
2456 rb_warn0("END in method; use at_exit");
2457 }
2458 restore_block_exit(p, $allow_exits);
2459 p->ctxt = $k_END;
2460 /*%%%*/
2461 {
2462 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, &@$);
2463 $$ = NEW_POSTEXE(scope, &@$);
2464 }
2465 /*% %*/
2466 /*% ripper: END!($compstmt) %*/
2467 }
2468 | command_asgn
2469 | mlhs '=' lex_ctxt command_call
2470 {
2471 /*%%%*/
2472 value_expr($4);
2473 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
2474 /*% %*/
2475 /*% ripper: massign!($1, $4) %*/
2476 }
2477 | lhs '=' lex_ctxt mrhs
2478 {
2479 /*%%%*/
2480 $$ = node_assign(p, $1, $4, $3, &@$);
2481 /*% %*/
2482 /*% ripper: assign!($1, $4) %*/
2483 }
2484 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
2485 after_rescue stmt[resbody]
2486 {
2487 p->ctxt.in_rescue = $3.in_rescue;
2488 /*%%%*/
2489 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
2490 $resbody = NEW_RESBODY(0, remove_begin($resbody), 0, &loc);
2491 loc.beg_pos = @mrhs_arg.beg_pos;
2492 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
2493 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
2494 /*% %*/
2495 /*% ripper: massign!($1, rescue_mod!($4, $7)) %*/
2496 }
2497 | mlhs '=' lex_ctxt mrhs_arg
2498 {
2499 /*%%%*/
2500 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
2501 /*% %*/
2502 /*% ripper: massign!($1, $4) %*/
2503 }
2504 | expr
2505 | error
2506 {
2507 (void)yynerrs;
2508 /*%%%*/
2509 $$ = NEW_ERROR(&@$);
2510 /*% %*/
2511 }
2512 ;
2513
2514command_asgn : lhs '=' lex_ctxt command_rhs
2515 {
2516 /*%%%*/
2517 $$ = node_assign(p, $1, $4, $3, &@$);
2518 /*% %*/
2519 /*% ripper: assign!($1, $4) %*/
2520 }
2521 | var_lhs tOP_ASGN lex_ctxt command_rhs
2522 {
2523 /*%%%*/
2524 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
2525 /*% %*/
2526 /*% ripper: opassign!($1, $2, $4) %*/
2527 }
2528 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
2529 {
2530 /*%%%*/
2531 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
2532 /*% %*/
2533 /*% ripper: opassign!(aref_field!($1, $3), $5, $7) %*/
2534
2535 }
2536 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
2537 {
2538 /*%%%*/
2539 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2540 /*% %*/
2541 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2542 }
2543 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt command_rhs
2544 {
2545 /*%%%*/
2546 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2547 /*% %*/
2548 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2549 }
2550 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
2551 {
2552 /*%%%*/
2553 YYLTYPE loc = code_loc_gen(&@1, &@3);
2554 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
2555 /*% %*/
2556 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
2557 }
2558 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
2559 {
2560 /*%%%*/
2561 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
2562 /*% %*/
2563 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2564 }
2565 | defn_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
2566 {
2567 endless_method_name(p, get_id($head->nd_mid), &@head);
2568 restore_defun(p, $head);
2569 /*%%%*/
2570 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
2571 ($$ = $head->nd_def)->nd_loc = @$;
2572 RNODE_DEFN($$)->nd_defn = $bodystmt;
2573 /*% %*/
2574 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
2575 /*% ripper: def!($head->nd_mid, $args, $bodystmt) %*/
2576 local_pop(p);
2577 }
2578 | defs_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
2579 {
2580 endless_method_name(p, get_id($head->nd_mid), &@head);
2581 restore_defun(p, $head);
2582 /*%%%*/
2583 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
2584 ($$ = $head->nd_def)->nd_loc = @$;
2585 RNODE_DEFS($$)->nd_defn = $bodystmt;
2586 /*% %*/
2587 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
2588 /*% ripper: defs!($head->nd_recv, $head->dot_or_colon, $head->nd_mid, $args, $bodystmt) %*/
2589 local_pop(p);
2590 }
2591 | backref tOP_ASGN lex_ctxt command_rhs
2592 {
2593 /*%%%*/
2594 rb_backref_error(p, $1);
2595 $$ = NEW_ERROR(&@$);
2596 /*% %*/
2597 /*% ripper[error]: backref_error(p, RNODE($1), assign!(var_field(p, $1), $4)) %*/
2598 }
2599 ;
2600
2601endless_command : command
2602 | endless_command modifier_rescue after_rescue arg
2603 {
2604 p->ctxt.in_rescue = $3.in_rescue;
2605 /*%%%*/
2606 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
2607 /*% %*/
2608 /*% ripper: rescue_mod!($1, $4) %*/
2609 }
2610 | keyword_not opt_nl endless_command
2611 {
2612 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2613 }
2614 ;
2615
2616command_rhs : command_call %prec tOP_ASGN
2617 {
2618 value_expr($1);
2619 $$ = $1;
2620 }
2621 | command_call modifier_rescue after_rescue stmt
2622 {
2623 p->ctxt.in_rescue = $3.in_rescue;
2624 /*%%%*/
2625 YYLTYPE loc = code_loc_gen(&@2, &@4);
2626 value_expr($1);
2627 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($4), 0, &loc), 0, &@$);
2628 /*% %*/
2629 /*% ripper: rescue_mod!($1, $4) %*/
2630 }
2631 | command_asgn
2632 ;
2633
2634expr : command_call
2635 | expr keyword_and expr
2636 {
2637 $$ = logop(p, idAND, $1, $3, &@2, &@$);
2638 }
2639 | expr keyword_or expr
2640 {
2641 $$ = logop(p, idOR, $1, $3, &@2, &@$);
2642 }
2643 | keyword_not opt_nl expr
2644 {
2645 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2646 }
2647 | '!' command_call
2648 {
2649 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2650 }
2651 | arg tASSOC
2652 {
2653 value_expr($arg);
2654 }
2655 p_in_kwarg[ctxt] p_pvtbl p_pktbl
2656 p_top_expr_body[body]
2657 {
2658 pop_pktbl(p, $p_pktbl);
2659 pop_pvtbl(p, $p_pvtbl);
2660 p->ctxt.in_kwarg = $ctxt.in_kwarg;
2661 /*%%%*/
2662 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body), &@$);
2663 /*% %*/
2664 /*% ripper: case!($arg, in!($body, Qnil, Qnil)) %*/
2665 }
2666 | arg keyword_in
2667 {
2668 value_expr($arg);
2669 }
2670 p_in_kwarg[ctxt] p_pvtbl p_pktbl
2671 p_top_expr_body[body]
2672 {
2673 pop_pktbl(p, $p_pktbl);
2674 pop_pvtbl(p, $p_pvtbl);
2675 p->ctxt.in_kwarg = $ctxt.in_kwarg;
2676 /*%%%*/
2677 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body), &@$);
2678 /*% %*/
2679 /*% ripper: case!($arg, in!($body, Qnil, Qnil)) %*/
2680 }
2681 | arg %prec tLBRACE_ARG
2682 ;
2683
2684def_name : fname
2685 {
2686 ID fname = get_id($1);
2687 numparam_name(p, fname);
2688 local_push(p, 0);
2689 p->cur_arg = 0;
2690 p->ctxt.in_def = 1;
2691 p->ctxt.in_rescue = before_rescue;
2692 $$ = $1;
2693 }
2694 ;
2695
2696defn_head : k_def def_name
2697 {
2698 $$ = def_head_save(p, $k_def);
2699 $$->nd_mid = $def_name;
2700 /*%%%*/
2701 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
2702 /*%
2703 add_mark_object(p, $def_name);
2704 %*/
2705 }
2706 ;
2707
2708defs_head : k_def singleton dot_or_colon
2709 {
2710 SET_LEX_STATE(EXPR_FNAME);
2711 p->ctxt.in_argdef = 1;
2712 }
2713 def_name
2714 {
2715 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
2716 $$ = def_head_save(p, $k_def);
2717 $$->nd_mid = $def_name;
2718 /*%%%*/
2719 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
2720 /*%
2721 add_mark_object(p, $def_name);
2722 $$->nd_recv = add_mark_object(p, $singleton);
2723 $$->dot_or_colon = add_mark_object(p, $dot_or_colon);
2724 %*/
2725 }
2726 ;
2727
2728expr_value : expr
2729 {
2730 value_expr($1);
2731 $$ = $1;
2732 }
2733 | error
2734 {
2735 /*%%%*/
2736 $$ = NEW_ERROR(&@$);
2737 /*% %*/
2738 }
2739 ;
2740
2741expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
2742 {
2743 $$ = $2;
2744 }
2745 ;
2746
2747command_call : command
2748 | block_command
2749 ;
2750
2751block_command : block_call
2752 | block_call call_op2 operation2 command_args
2753 {
2754 /*%%%*/
2755 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
2756 /*% %*/
2757 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
2758 }
2759 ;
2760
2761cmd_brace_block : tLBRACE_ARG brace_body '}'
2762 {
2763 $$ = $2;
2764 /*%%%*/
2765 set_embraced_location($$, &@1, &@3);
2766 /*% %*/
2767 }
2768 ;
2769
2770fcall : operation
2771 {
2772 /*%%%*/
2773 $$ = NEW_FCALL($1, 0, &@$);
2774 /*% %*/
2775 /*% ripper: $1 %*/
2776 }
2777 ;
2778
2779command : fcall command_args %prec tLOWEST
2780 {
2781 /*%%%*/
2782 $1->nd_args = $2;
2783 nd_set_last_loc($1, @2.end_pos);
2784 $$ = (NODE *)$1;
2785 /*% %*/
2786 /*% ripper: command!($1, $2) %*/
2787 }
2788 | fcall command_args cmd_brace_block
2789 {
2790 /*%%%*/
2791 block_dup_check(p, $2, $3);
2792 $1->nd_args = $2;
2793 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
2794 fixpos($$, RNODE($1));
2795 nd_set_last_loc($1, @2.end_pos);
2796 /*% %*/
2797 /*% ripper: method_add_block!(command!($1, $2), $3) %*/
2798 }
2799 | primary_value call_op operation2 command_args %prec tLOWEST
2800 {
2801 /*%%%*/
2802 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
2803 /*% %*/
2804 /*% ripper: command_call!($1, $2, $3, $4) %*/
2805 }
2806 | primary_value call_op operation2 command_args cmd_brace_block
2807 {
2808 /*%%%*/
2809 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
2810 /*% %*/
2811 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
2812 }
2813 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
2814 {
2815 /*%%%*/
2816 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
2817 /*% %*/
2818 /*% ripper: command_call!($1, $2, $3, $4) %*/
2819 }
2820 | primary_value tCOLON2 operation2 command_args cmd_brace_block
2821 {
2822 /*%%%*/
2823 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
2824 /*% %*/
2825 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
2826 }
2827 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
2828 {
2829 /*%%%*/
2830 set_embraced_location($5, &@4, &@6);
2831 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, $5, &@3, &@$);
2832 /*% %*/
2833 /*% ripper: method_add_block!(command_call!($1, $2, $3, Qnull), $5) %*/
2834 }
2835 | keyword_super command_args
2836 {
2837 /*%%%*/
2838 $$ = NEW_SUPER($2, &@$);
2839 fixpos($$, $2);
2840 /*% %*/
2841 /*% ripper: super!($2) %*/
2842 }
2843 | k_yield command_args
2844 {
2845 /*%%%*/
2846 $$ = new_yield(p, $2, &@$);
2847 fixpos($$, $2);
2848 /*% %*/
2849 /*% ripper: yield!($2) %*/
2850 }
2851 | k_return call_args
2852 {
2853 /*%%%*/
2854 $$ = NEW_RETURN(ret_args(p, $2), &@$);
2855 /*% %*/
2856 /*% ripper: return!($2) %*/
2857 }
2858 | keyword_break call_args
2859 {
2860 NODE *args = 0;
2861 /*%%%*/
2862 args = ret_args(p, $2);
2863 /*% %*/
2864 $<node>$ = add_block_exit(p, NEW_BREAK(args, &@$));
2865 /*% ripper: break!($2) %*/
2866 }
2867 | keyword_next call_args
2868 {
2869 NODE *args = 0;
2870 /*%%%*/
2871 args = ret_args(p, $2);
2872 /*% %*/
2873 $<node>$ = add_block_exit(p, NEW_NEXT(args, &@$));
2874 /*% ripper: next!($2) %*/
2875 }
2876 ;
2877
2878mlhs : mlhs_basic
2879 | tLPAREN mlhs_inner rparen
2880 {
2881 /*%%%*/
2882 $$ = $2;
2883 /*% %*/
2884 /*% ripper: mlhs_paren!($2) %*/
2885 }
2886 ;
2887
2888mlhs_inner : mlhs_basic
2889 | tLPAREN mlhs_inner rparen
2890 {
2891 /*%%%*/
2892 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
2893 /*% %*/
2894 /*% ripper: mlhs_paren!($2) %*/
2895 }
2896 ;
2897
2898mlhs_basic : mlhs_head
2899 {
2900 /*%%%*/
2901 $$ = NEW_MASGN($1, 0, &@$);
2902 /*% %*/
2903 /*% ripper: $1 %*/
2904 }
2905 | mlhs_head mlhs_item
2906 {
2907 /*%%%*/
2908 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
2909 /*% %*/
2910 /*% ripper: mlhs_add!($1, $2) %*/
2911 }
2912 | mlhs_head tSTAR mlhs_node
2913 {
2914 /*%%%*/
2915 $$ = NEW_MASGN($1, $3, &@$);
2916 /*% %*/
2917 /*% ripper: mlhs_add_star!($1, $3) %*/
2918 }
2919 | mlhs_head tSTAR mlhs_node ',' mlhs_post
2920 {
2921 /*%%%*/
2922 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
2923 /*% %*/
2924 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
2925 }
2926 | mlhs_head tSTAR
2927 {
2928 /*%%%*/
2929 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
2930 /*% %*/
2931 /*% ripper: mlhs_add_star!($1, Qnil) %*/
2932 }
2933 | mlhs_head tSTAR ',' mlhs_post
2934 {
2935 /*%%%*/
2936 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
2937 /*% %*/
2938 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, Qnil), $4) %*/
2939 }
2940 | tSTAR mlhs_node
2941 {
2942 /*%%%*/
2943 $$ = NEW_MASGN(0, $2, &@$);
2944 /*% %*/
2945 /*% ripper: mlhs_add_star!(mlhs_new!, $2) %*/
2946 }
2947 | tSTAR mlhs_node ',' mlhs_post
2948 {
2949 /*%%%*/
2950 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
2951 /*% %*/
2952 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $2), $4) %*/
2953 }
2954 | tSTAR
2955 {
2956 /*%%%*/
2957 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
2958 /*% %*/
2959 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
2960 }
2961 | tSTAR ',' mlhs_post
2962 {
2963 /*%%%*/
2964 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
2965 /*% %*/
2966 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $3) %*/
2967 }
2968 ;
2969
2970mlhs_item : mlhs_node
2971 | tLPAREN mlhs_inner rparen
2972 {
2973 /*%%%*/
2974 $$ = (NODE *)$2;
2975 /*% %*/
2976 /*% ripper: mlhs_paren!($2) %*/
2977 }
2978 ;
2979
2980mlhs_head : mlhs_item ','
2981 {
2982 /*%%%*/
2983 $$ = NEW_LIST($1, &@1);
2984 /*% %*/
2985 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2986 }
2987 | mlhs_head mlhs_item ','
2988 {
2989 /*%%%*/
2990 $$ = list_append(p, $1, $2);
2991 /*% %*/
2992 /*% ripper: mlhs_add!($1, $2) %*/
2993 }
2994 ;
2995
2996mlhs_post : mlhs_item
2997 {
2998 /*%%%*/
2999 $$ = NEW_LIST($1, &@$);
3000 /*% %*/
3001 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
3002 }
3003 | mlhs_post ',' mlhs_item
3004 {
3005 /*%%%*/
3006 $$ = list_append(p, $1, $3);
3007 /*% %*/
3008 /*% ripper: mlhs_add!($1, $3) %*/
3009 }
3010 ;
3011
3012mlhs_node : user_variable
3013 {
3014 /*%%%*/
3015 $$ = assignable(p, $1, 0, &@$);
3016 /*% %*/
3017 /*% ripper: assignable(p, var_field(p, $1)) %*/
3018 }
3019 | keyword_variable
3020 {
3021 /*%%%*/
3022 $$ = assignable(p, $1, 0, &@$);
3023 /*% %*/
3024 /*% ripper: assignable(p, var_field(p, $1)) %*/
3025 }
3026 | primary_value '[' opt_call_args rbracket
3027 {
3028 /*%%%*/
3029 $$ = aryset(p, $1, $3, &@$);
3030 /*% %*/
3031 /*% ripper: aref_field!($1, $3) %*/
3032 }
3033 | primary_value call_op tIDENTIFIER
3034 {
3035 anddot_multiple_assignment_check(p, &@2, $2);
3036 /*%%%*/
3037 $$ = attrset(p, $1, $2, $3, &@$);
3038 /*% %*/
3039 /*% ripper: field!($1, $2, $3) %*/
3040 }
3041 | primary_value tCOLON2 tIDENTIFIER
3042 {
3043 /*%%%*/
3044 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3045 /*% %*/
3046 /*% ripper: const_path_field!($1, $3) %*/
3047 }
3048 | primary_value call_op tCONSTANT
3049 {
3050 anddot_multiple_assignment_check(p, &@2, $2);
3051 /*%%%*/
3052 $$ = attrset(p, $1, $2, $3, &@$);
3053 /*% %*/
3054 /*% ripper: field!($1, $2, $3) %*/
3055 }
3056 | primary_value tCOLON2 tCONSTANT
3057 {
3058 /*%%%*/
3059 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3060 /*% %*/
3061 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
3062 }
3063 | tCOLON3 tCONSTANT
3064 {
3065 /*%%%*/
3066 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3067 /*% %*/
3068 /*% ripper: const_decl(p, top_const_field!($2)) %*/
3069 }
3070 | backref
3071 {
3072 /*%%%*/
3073 rb_backref_error(p, $1);
3074 $$ = NEW_ERROR(&@$);
3075 /*% %*/
3076 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
3077 }
3078 ;
3079
3080lhs : user_variable
3081 {
3082 /*%%%*/
3083 $$ = assignable(p, $1, 0, &@$);
3084 /*% %*/
3085 /*% ripper: assignable(p, var_field(p, $1)) %*/
3086 }
3087 | keyword_variable
3088 {
3089 /*%%%*/
3090 $$ = assignable(p, $1, 0, &@$);
3091 /*% %*/
3092 /*% ripper: assignable(p, var_field(p, $1)) %*/
3093 }
3094 | primary_value '[' opt_call_args rbracket
3095 {
3096 /*%%%*/
3097 $$ = aryset(p, $1, $3, &@$);
3098 /*% %*/
3099 /*% ripper: aref_field!($1, $3) %*/
3100 }
3101 | primary_value call_op tIDENTIFIER
3102 {
3103 /*%%%*/
3104 $$ = attrset(p, $1, $2, $3, &@$);
3105 /*% %*/
3106 /*% ripper: field!($1, $2, $3) %*/
3107 }
3108 | primary_value tCOLON2 tIDENTIFIER
3109 {
3110 /*%%%*/
3111 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3112 /*% %*/
3113 /*% ripper: field!($1, $2, $3) %*/
3114 }
3115 | primary_value call_op tCONSTANT
3116 {
3117 /*%%%*/
3118 $$ = attrset(p, $1, $2, $3, &@$);
3119 /*% %*/
3120 /*% ripper: field!($1, $2, $3) %*/
3121 }
3122 | primary_value tCOLON2 tCONSTANT
3123 {
3124 /*%%%*/
3125 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3126 /*% %*/
3127 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
3128 }
3129 | tCOLON3 tCONSTANT
3130 {
3131 /*%%%*/
3132 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3133 /*% %*/
3134 /*% ripper: const_decl(p, top_const_field!($2)) %*/
3135 }
3136 | backref
3137 {
3138 /*%%%*/
3139 rb_backref_error(p, $1);
3140 $$ = NEW_ERROR(&@$);
3141 /*% %*/
3142 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
3143 }
3144 ;
3145
3146cname : tIDENTIFIER
3147 {
3148 static const char mesg[] = "class/module name must be CONSTANT";
3149 /*%%%*/
3150 yyerror1(&@1, mesg);
3151 /*% %*/
3152 /*% ripper[error]: class_name_error!(ERR_MESG(), $1) %*/
3153 }
3154 | tCONSTANT
3155 ;
3156
3157cpath : tCOLON3 cname
3158 {
3159 /*%%%*/
3160 $$ = NEW_COLON3($2, &@$);
3161 /*% %*/
3162 /*% ripper: top_const_ref!($2) %*/
3163 }
3164 | cname
3165 {
3166 /*%%%*/
3167 $$ = NEW_COLON2(0, $1, &@$);
3168 /*% %*/
3169 /*% ripper: const_ref!($1) %*/
3170 }
3171 | primary_value tCOLON2 cname
3172 {
3173 /*%%%*/
3174 $$ = NEW_COLON2($1, $3, &@$);
3175 /*% %*/
3176 /*% ripper: const_path_ref!($1, $3) %*/
3177 }
3178 ;
3179
3180fname : tIDENTIFIER
3181 | tCONSTANT
3182 | tFID
3183 | op
3184 {
3185 SET_LEX_STATE(EXPR_ENDFN);
3186 $$ = $1;
3187 }
3188 | reswords
3189 ;
3190
3191fitem : fname
3192 {
3193 /*%%%*/
3194 $$ = NEW_LIT(ID2SYM($1), &@$);
3195 /*% %*/
3196 /*% ripper: symbol_literal!($1) %*/
3197 }
3198 | symbol
3199 ;
3200
3201undef_list : fitem
3202 {
3203 /*%%%*/
3204 $$ = NEW_UNDEF($1, &@$);
3205 /*% %*/
3206 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
3207 }
3208 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3209 {
3210 /*%%%*/
3211 NODE *undef = NEW_UNDEF($4, &@4);
3212 $$ = block_append(p, $1, undef);
3213 /*% %*/
3214 /*% ripper: rb_ary_push($1, get_value($4)) %*/
3215 }
3216 ;
3217
3218op : '|' { ifndef_ripper($$ = '|'); }
3219 | '^' { ifndef_ripper($$ = '^'); }
3220 | '&' { ifndef_ripper($$ = '&'); }
3221 | tCMP { ifndef_ripper($$ = tCMP); }
3222 | tEQ { ifndef_ripper($$ = tEQ); }
3223 | tEQQ { ifndef_ripper($$ = tEQQ); }
3224 | tMATCH { ifndef_ripper($$ = tMATCH); }
3225 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
3226 | '>' { ifndef_ripper($$ = '>'); }
3227 | tGEQ { ifndef_ripper($$ = tGEQ); }
3228 | '<' { ifndef_ripper($$ = '<'); }
3229 | tLEQ { ifndef_ripper($$ = tLEQ); }
3230 | tNEQ { ifndef_ripper($$ = tNEQ); }
3231 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
3232 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
3233 | '+' { ifndef_ripper($$ = '+'); }
3234 | '-' { ifndef_ripper($$ = '-'); }
3235 | '*' { ifndef_ripper($$ = '*'); }
3236 | tSTAR { ifndef_ripper($$ = '*'); }
3237 | '/' { ifndef_ripper($$ = '/'); }
3238 | '%' { ifndef_ripper($$ = '%'); }
3239 | tPOW { ifndef_ripper($$ = tPOW); }
3240 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
3241 | '!' { ifndef_ripper($$ = '!'); }
3242 | '~' { ifndef_ripper($$ = '~'); }
3243 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
3244 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
3245 | tAREF { ifndef_ripper($$ = tAREF); }
3246 | tASET { ifndef_ripper($$ = tASET); }
3247 | '`' { ifndef_ripper($$ = '`'); }
3248 ;
3249
3250reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3251 | keyword_BEGIN | keyword_END
3252 | keyword_alias | keyword_and | keyword_begin
3253 | keyword_break | keyword_case | keyword_class | keyword_def
3254 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3255 | keyword_end | keyword_ensure | keyword_false
3256 | keyword_for | keyword_in | keyword_module | keyword_next
3257 | keyword_nil | keyword_not | keyword_or | keyword_redo
3258 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3259 | keyword_super | keyword_then | keyword_true | keyword_undef
3260 | keyword_when | keyword_yield | keyword_if | keyword_unless
3261 | keyword_while | keyword_until
3262 ;
3263
3264arg : lhs '=' lex_ctxt arg_rhs
3265 {
3266 /*%%%*/
3267 $$ = node_assign(p, $1, $4, $3, &@$);
3268 /*% %*/
3269 /*% ripper: assign!($1, $4) %*/
3270 }
3271 | var_lhs tOP_ASGN lex_ctxt arg_rhs
3272 {
3273 /*%%%*/
3274 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
3275 /*% %*/
3276 /*% ripper: opassign!($1, $2, $4) %*/
3277 }
3278 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
3279 {
3280 /*%%%*/
3281 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
3282 /*% %*/
3283 /*% ripper: opassign!(aref_field!($1, $3), $5, $7) %*/
3284 }
3285 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3286 {
3287 /*%%%*/
3288 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
3289 /*% %*/
3290 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
3291 }
3292 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3293 {
3294 /*%%%*/
3295 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
3296 /*% %*/
3297 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
3298 }
3299 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3300 {
3301 /*%%%*/
3302 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
3303 /*% %*/
3304 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
3305 }
3306 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3307 {
3308 /*%%%*/
3309 YYLTYPE loc = code_loc_gen(&@1, &@3);
3310 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
3311 /*% %*/
3312 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
3313 }
3314 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3315 {
3316 /*%%%*/
3317 YYLTYPE loc = code_loc_gen(&@1, &@2);
3318 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
3319 /*% %*/
3320 /*% ripper: opassign!(top_const_field!($2), $3, $5) %*/
3321 }
3322 | backref tOP_ASGN lex_ctxt arg_rhs
3323 {
3324 /*%%%*/
3325 rb_backref_error(p, $1);
3326 $$ = NEW_ERROR(&@$);
3327 /*% %*/
3328 /*% ripper[error]: backref_error(p, RNODE($1), opassign!(var_field(p, $1), $2, $4)) %*/
3329 }
3330 | arg tDOT2 arg
3331 {
3332 /*%%%*/
3333 value_expr($1);
3334 value_expr($3);
3335 $$ = NEW_DOT2($1, $3, &@$);
3336 /*% %*/
3337 /*% ripper: dot2!($1, $3) %*/
3338 }
3339 | arg tDOT3 arg
3340 {
3341 /*%%%*/
3342 value_expr($1);
3343 value_expr($3);
3344 $$ = NEW_DOT3($1, $3, &@$);
3345 /*% %*/
3346 /*% ripper: dot3!($1, $3) %*/
3347 }
3348 | arg tDOT2
3349 {
3350 /*%%%*/
3351 value_expr($1);
3352 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
3353 /*% %*/
3354 /*% ripper: dot2!($1, Qnil) %*/
3355 }
3356 | arg tDOT3
3357 {
3358 /*%%%*/
3359 value_expr($1);
3360 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
3361 /*% %*/
3362 /*% ripper: dot3!($1, Qnil) %*/
3363 }
3364 | tBDOT2 arg
3365 {
3366 /*%%%*/
3367 value_expr($2);
3368 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
3369 /*% %*/
3370 /*% ripper: dot2!(Qnil, $2) %*/
3371 }
3372 | tBDOT3 arg
3373 {
3374 /*%%%*/
3375 value_expr($2);
3376 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
3377 /*% %*/
3378 /*% ripper: dot3!(Qnil, $2) %*/
3379 }
3380 | arg '+' arg
3381 {
3382 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3383 }
3384 | arg '-' arg
3385 {
3386 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3387 }
3388 | arg '*' arg
3389 {
3390 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3391 }
3392 | arg '/' arg
3393 {
3394 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3395 }
3396 | arg '%' arg
3397 {
3398 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3399 }
3400 | arg tPOW arg
3401 {
3402 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3403 }
3404 | tUMINUS_NUM simple_numeric tPOW arg
3405 {
3406 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3407 }
3408 | tUPLUS arg
3409 {
3410 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3411 }
3412 | tUMINUS arg
3413 {
3414 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3415 }
3416 | arg '|' arg
3417 {
3418 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3419 }
3420 | arg '^' arg
3421 {
3422 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3423 }
3424 | arg '&' arg
3425 {
3426 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3427 }
3428 | arg tCMP arg
3429 {
3430 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3431 }
3432 | rel_expr %prec tCMP
3433 | arg tEQ arg
3434 {
3435 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3436 }
3437 | arg tEQQ arg
3438 {
3439 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
3440 }
3441 | arg tNEQ arg
3442 {
3443 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
3444 }
3445 | arg tMATCH arg
3446 {
3447 $$ = match_op(p, $1, $3, &@2, &@$);
3448 }
3449 | arg tNMATCH arg
3450 {
3451 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
3452 }
3453 | '!' arg
3454 {
3455 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3456 }
3457 | '~' arg
3458 {
3459 $$ = call_uni_op(p, $2, '~', &@1, &@$);
3460 }
3461 | arg tLSHFT arg
3462 {
3463 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
3464 }
3465 | arg tRSHFT arg
3466 {
3467 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
3468 }
3469 | arg tANDOP arg
3470 {
3471 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
3472 }
3473 | arg tOROP arg
3474 {
3475 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
3476 }
3477 | keyword_defined opt_nl begin_defined arg
3478 {
3479 p->ctxt.in_defined = $3.in_defined;
3480 $$ = new_defined(p, $4, &@$);
3481 }
3482 | arg '?' arg opt_nl ':' arg
3483 {
3484 /*%%%*/
3485 value_expr($1);
3486 $$ = new_if(p, $1, $3, $6, &@$);
3487 fixpos($$, $1);
3488 /*% %*/
3489 /*% ripper: ifop!($1, $3, $6) %*/
3490 }
3491 | defn_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
3492 {
3493 endless_method_name(p, get_id($head->nd_mid), &@head);
3494 restore_defun(p, $head);
3495 /*%%%*/
3496 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3497 ($$ = $head->nd_def)->nd_loc = @$;
3498 RNODE_DEFN($$)->nd_defn = $bodystmt;
3499 /*% %*/
3500 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
3501 /*% ripper: def!($head->nd_mid, $args, $bodystmt) %*/
3502 local_pop(p);
3503 }
3504 | defs_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
3505 {
3506 endless_method_name(p, get_id($head->nd_mid), &@head);
3507 restore_defun(p, $head);
3508 /*%%%*/
3509 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3510 ($$ = $head->nd_def)->nd_loc = @$;
3511 RNODE_DEFS($$)->nd_defn = $bodystmt;
3512 /*% %*/
3513 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
3514 /*% ripper: defs!($head->nd_recv, $head->dot_or_colon, $head->nd_mid, $args, $bodystmt) %*/
3515 local_pop(p);
3516 }
3517 | primary
3518 {
3519 $$ = $1;
3520 }
3521 ;
3522
3523endless_arg : arg %prec modifier_rescue
3524 | endless_arg modifier_rescue after_rescue arg
3525 {
3526 p->ctxt.in_rescue = $3.in_rescue;
3527 /*%%%*/
3528 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3529 /*% %*/
3530 /*% ripper: rescue_mod!($1, $4) %*/
3531 }
3532 | keyword_not opt_nl endless_arg
3533 {
3534 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3535 }
3536 ;
3537
3538relop : '>' {$$ = '>';}
3539 | '<' {$$ = '<';}
3540 | tGEQ {$$ = idGE;}
3541 | tLEQ {$$ = idLE;}
3542 ;
3543
3544rel_expr : arg relop arg %prec '>'
3545 {
3546 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
3547 }
3548 | rel_expr relop arg %prec '>'
3549 {
3550 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
3551 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
3552 }
3553 ;
3554
3555lex_ctxt : none
3556 {
3557 $$ = p->ctxt;
3558 }
3559 ;
3560
3561begin_defined : lex_ctxt
3562 {
3563 p->ctxt.in_defined = 1;
3564 $$ = $1;
3565 }
3566 ;
3567
3568after_rescue : lex_ctxt
3569 {
3570 p->ctxt.in_rescue = after_rescue;
3571 $$ = $1;
3572 }
3573 ;
3574
3575arg_value : arg
3576 {
3577 value_expr($1);
3578 $$ = $1;
3579 }
3580 ;
3581
3582aref_args : none
3583 | args trailer
3584 {
3585 $$ = $1;
3586 }
3587 | args ',' assocs trailer
3588 {
3589 /*%%%*/
3590 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3591 /*% %*/
3592 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
3593 }
3594 | assocs trailer
3595 {
3596 /*%%%*/
3597 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
3598 /*% %*/
3599 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
3600 }
3601 ;
3602
3603arg_rhs : arg %prec tOP_ASGN
3604 {
3605 value_expr($1);
3606 $$ = $1;
3607 }
3608 | arg modifier_rescue after_rescue arg
3609 {
3610 p->ctxt.in_rescue = $3.in_rescue;
3611 /*%%%*/
3612 value_expr($1);
3613 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3614 /*% %*/
3615 /*% ripper: rescue_mod!($1, $4) %*/
3616 }
3617 ;
3618
3619paren_args : '(' opt_call_args rparen
3620 {
3621 /*%%%*/
3622 $$ = $2;
3623 /*% %*/
3624 /*% ripper: arg_paren!($2) %*/
3625 }
3626 | '(' args ',' args_forward rparen
3627 {
3628 if (!check_forwarding_args(p)) {
3629 $$ = Qnone;
3630 }
3631 else {
3632 /*%%%*/
3633 $$ = new_args_forward_call(p, $2, &@4, &@$);
3634 /*% %*/
3635 /*% ripper: arg_paren!(args_add!($2, $4)) %*/
3636 }
3637 }
3638 | '(' args_forward rparen
3639 {
3640 if (!check_forwarding_args(p)) {
3641 $$ = Qnone;
3642 }
3643 else {
3644 /*%%%*/
3645 $$ = new_args_forward_call(p, 0, &@2, &@$);
3646 /*% %*/
3647 /*% ripper: arg_paren!($2) %*/
3648 }
3649 }
3650 ;
3651
3652opt_paren_args : none
3653 | paren_args
3654 ;
3655
3656opt_call_args : none
3657 | call_args
3658 | args ','
3659 {
3660 $$ = $1;
3661 }
3662 | args ',' assocs ','
3663 {
3664 /*%%%*/
3665 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3666 /*% %*/
3667 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
3668 }
3669 | assocs ','
3670 {
3671 /*%%%*/
3672 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3673 /*% %*/
3674 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
3675 }
3676 ;
3677
3678call_args : command
3679 {
3680 /*%%%*/
3681 value_expr($1);
3682 $$ = NEW_LIST($1, &@$);
3683 /*% %*/
3684 /*% ripper: args_add!(args_new!, $1) %*/
3685 }
3686 | args opt_block_arg
3687 {
3688 /*%%%*/
3689 $$ = arg_blk_pass($1, $2);
3690 /*% %*/
3691 /*% ripper: args_add_block!($1, $2) %*/
3692 }
3693 | assocs opt_block_arg
3694 {
3695 /*%%%*/
3696 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3697 $$ = arg_blk_pass($$, $2);
3698 /*% %*/
3699 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($1)), $2) %*/
3700 }
3701 | args ',' assocs opt_block_arg
3702 {
3703 /*%%%*/
3704 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3705 $$ = arg_blk_pass($$, $4);
3706 /*% %*/
3707 /*% ripper: args_add_block!(args_add!($1, bare_assoc_hash!($3)), $4) %*/
3708 }
3709 | block_arg
3710 /*% ripper[brace]: args_add_block!(args_new!, $1) %*/
3711 ;
3712
3713command_args : {
3714 /* If call_args starts with a open paren '(' or '[',
3715 * look-ahead reading of the letters calls CMDARG_PUSH(0),
3716 * but the push must be done after CMDARG_PUSH(1).
3717 * So this code makes them consistent by first cancelling
3718 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
3719 * and finally redoing CMDARG_PUSH(0).
3720 */
3721 int lookahead = 0;
3722 switch (yychar) {
3723 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
3724 lookahead = 1;
3725 }
3726 if (lookahead) CMDARG_POP();
3727 CMDARG_PUSH(1);
3728 if (lookahead) CMDARG_PUSH(0);
3729 }
3730 call_args
3731 {
3732 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
3733 * but the push must be done after CMDARG_POP() in the parser.
3734 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
3735 * CMDARG_POP() to pop 1 pushed by command_args,
3736 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
3737 */
3738 int lookahead = 0;
3739 switch (yychar) {
3740 case tLBRACE_ARG:
3741 lookahead = 1;
3742 }
3743 if (lookahead) CMDARG_POP();
3744 CMDARG_POP();
3745 if (lookahead) CMDARG_PUSH(0);
3746 $$ = $2;
3747 }
3748 ;
3749
3750block_arg : tAMPER arg_value
3751 {
3752 /*%%%*/
3753 $$ = NEW_BLOCK_PASS($2, &@$);
3754 /*% %*/
3755 /*% ripper: $2 %*/
3756 }
3757 | tAMPER
3758 {
3759 forwarding_arg_check(p, idFWD_BLOCK, 0, "block");
3760 /*%%%*/
3761 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$);
3762 /*% %*/
3763 /*% ripper: Qnil %*/
3764 }
3765 ;
3766
3767opt_block_arg : ',' block_arg
3768 {
3769 $$ = $2;
3770 }
3771 | none
3772 {
3773 $$ = 0;
3774 }
3775 ;
3776
3777/* value */
3778args : arg_value
3779 {
3780 /*%%%*/
3781 $$ = NEW_LIST($1, &@$);
3782 /*% %*/
3783 /*% ripper: args_add!(args_new!, $1) %*/
3784 }
3785 | arg_splat
3786 {
3787 /*%%%*/
3788 $$ = NEW_SPLAT($arg_splat, &@$);
3789 /*% %*/
3790 /*% ripper: args_add_star!(args_new!, $arg_splat) %*/
3791 }
3792 | args ',' arg_value
3793 {
3794 /*%%%*/
3795 $$ = last_arg_append(p, $1, $3, &@$);
3796 /*% %*/
3797 /*% ripper: args_add!($1, $3) %*/
3798 }
3799 | args ',' arg_splat
3800 {
3801 /*%%%*/
3802 $$ = rest_arg_append(p, $1, $3, &@$);
3803 /*% %*/
3804 /*% ripper: args_add_star!($1, $3) %*/
3805 }
3806 ;
3807
3808/* value */
3809arg_splat : tSTAR arg_value
3810 {
3811 $$ = $2;
3812 }
3813 | tSTAR /* none */
3814 {
3815 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
3816 /*%%%*/
3817 $$ = NEW_LVAR(idFWD_REST, &@1);
3818 /*% %*/
3819 /*% ripper: Qnil %*/
3820 }
3821 ;
3822
3823/* value */
3824mrhs_arg : mrhs
3825 | arg_value
3826 ;
3827
3828/* value */
3829mrhs : args ',' arg_value
3830 {
3831 /*%%%*/
3832 $$ = last_arg_append(p, $1, $3, &@$);
3833 /*% %*/
3834 /*% ripper: mrhs_add!(mrhs_new_from_args!($1), $3) %*/
3835 }
3836 | args ',' tSTAR arg_value
3837 {
3838 /*%%%*/
3839 $$ = rest_arg_append(p, $1, $4, &@$);
3840 /*% %*/
3841 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($1), $4) %*/
3842 }
3843 | tSTAR arg_value
3844 {
3845 /*%%%*/
3846 $$ = NEW_SPLAT($2, &@$);
3847 /*% %*/
3848 /*% ripper: mrhs_add_star!(mrhs_new!, $2) %*/
3849 }
3850 ;
3851
3852primary : literal
3853 | strings
3854 | xstring
3855 | regexp
3856 | words
3857 | qwords
3858 | symbols
3859 | qsymbols
3860 | var_ref
3861 | backref
3862 | tFID
3863 {
3864 /*%%%*/
3865 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
3866 /*% %*/
3867 /*% ripper: method_add_arg!(fcall!($1), args_new!) %*/
3868 }
3869 | k_begin
3870 {
3871 CMDARG_PUSH(0);
3872 }
3873 bodystmt
3874 k_end
3875 {
3876 CMDARG_POP();
3877 /*%%%*/
3878 set_line_body($3, @1.end_pos.lineno);
3879 $$ = NEW_BEGIN($3, &@$);
3880 nd_set_line($$, @1.end_pos.lineno);
3881 /*% %*/
3882 /*% ripper: begin!($3) %*/
3883 }
3884 | tLPAREN_ARG compstmt {SET_LEX_STATE(EXPR_ENDARG);} ')'
3885 {
3886 /*%%%*/
3887 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
3888 $$ = $2;
3889 /*% %*/
3890 /*% ripper: paren!($2) %*/
3891 }
3892 | tLPAREN compstmt ')'
3893 {
3894 /*%%%*/
3895 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
3896 $$ = NEW_BLOCK($2, &@$);
3897 /*% %*/
3898 /*% ripper: paren!($2) %*/
3899 }
3900 | primary_value tCOLON2 tCONSTANT
3901 {
3902 /*%%%*/
3903 $$ = NEW_COLON2($1, $3, &@$);
3904 /*% %*/
3905 /*% ripper: const_path_ref!($1, $3) %*/
3906 }
3907 | tCOLON3 tCONSTANT
3908 {
3909 /*%%%*/
3910 $$ = NEW_COLON3($2, &@$);
3911 /*% %*/
3912 /*% ripper: top_const_ref!($2) %*/
3913 }
3914 | tLBRACK aref_args ']'
3915 {
3916 /*%%%*/
3917 $$ = make_list($2, &@$);
3918 /*% %*/
3919 /*% ripper: array!($2) %*/
3920 }
3921 | tLBRACE assoc_list '}'
3922 {
3923 /*%%%*/
3924 $$ = new_hash(p, $2, &@$);
3925 RNODE_HASH($$)->nd_brace = TRUE;
3926 /*% %*/
3927 /*% ripper: hash!($2) %*/
3928 }
3929 | k_return
3930 {
3931 /*%%%*/
3932 $$ = NEW_RETURN(0, &@$);
3933 /*% %*/
3934 /*% ripper: return0! %*/
3935 }
3936 | k_yield '(' call_args rparen
3937 {
3938 /*%%%*/
3939 $$ = new_yield(p, $3, &@$);
3940 /*% %*/
3941 /*% ripper: yield!(paren!($3)) %*/
3942 }
3943 | k_yield '(' rparen
3944 {
3945 /*%%%*/
3946 $$ = NEW_YIELD(0, &@$);
3947 /*% %*/
3948 /*% ripper: yield!(paren!(args_new!)) %*/
3949 }
3950 | k_yield
3951 {
3952 /*%%%*/
3953 $$ = NEW_YIELD(0, &@$);
3954 /*% %*/
3955 /*% ripper: yield0! %*/
3956 }
3957 | keyword_defined opt_nl '(' begin_defined expr rparen
3958 {
3959 p->ctxt.in_defined = $4.in_defined;
3960 $$ = new_defined(p, $5, &@$);
3961 }
3962 | keyword_not '(' expr rparen
3963 {
3964 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3965 }
3966 | keyword_not '(' rparen
3967 {
3968 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
3969 }
3970 | fcall brace_block
3971 {
3972 /*%%%*/
3973 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
3974 /*% %*/
3975 /*% ripper: method_add_block!(method_add_arg!(fcall!($1), args_new!), $2) %*/
3976 }
3977 | method_call
3978 | method_call brace_block
3979 {
3980 /*%%%*/
3981 block_dup_check(p, get_nd_args(p, $1), $2);
3982 $$ = method_add_block(p, $1, $2, &@$);
3983 /*% %*/
3984 /*% ripper: method_add_block!($1, $2) %*/
3985 }
3986 | lambda
3987 | k_if expr_value then
3988 compstmt
3989 if_tail
3990 k_end
3991 {
3992 /*%%%*/
3993 $$ = new_if(p, $2, $4, $5, &@$);
3994 fixpos($$, $2);
3995 /*% %*/
3996 /*% ripper: if!($2, $4, $5) %*/
3997 }
3998 | k_unless expr_value then
3999 compstmt
4000 opt_else
4001 k_end
4002 {
4003 /*%%%*/
4004 $$ = new_unless(p, $2, $4, $5, &@$);
4005 fixpos($$, $2);
4006 /*% %*/
4007 /*% ripper: unless!($2, $4, $5) %*/
4008 }
4009 | k_while expr_value_do
4010 compstmt
4011 k_end
4012 {
4013 restore_block_exit(p, $1);
4014 /*%%%*/
4015 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
4016 fixpos($$, $2);
4017 /*% %*/
4018 /*% ripper: while!($2, $3) %*/
4019 }
4020 | k_until expr_value_do
4021 compstmt
4022 k_end
4023 {
4024 restore_block_exit(p, $1);
4025 /*%%%*/
4026 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
4027 fixpos($$, $2);
4028 /*% %*/
4029 /*% ripper: until!($2, $3) %*/
4030 }
4031 | k_case expr_value opt_terms
4032 {
4033 $<val>$ = p->case_labels;
4034 p->case_labels = Qnil;
4035 }
4036 case_body
4037 k_end
4038 {
4039 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
4040 p->case_labels = $<val>4;
4041 /*%%%*/
4042 $$ = NEW_CASE($2, $5, &@$);
4043 fixpos($$, $2);
4044 /*% %*/
4045 /*% ripper: case!($2, $5) %*/
4046 }
4047 | k_case opt_terms
4048 {
4049 $<val>$ = p->case_labels;
4050 p->case_labels = 0;
4051 }
4052 case_body
4053 k_end
4054 {
4055 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
4056 p->case_labels = $<val>3;
4057 /*%%%*/
4058 $$ = NEW_CASE2($4, &@$);
4059 /*% %*/
4060 /*% ripper: case!(Qnil, $4) %*/
4061 }
4062 | k_case expr_value opt_terms
4063 p_case_body
4064 k_end
4065 {
4066 /*%%%*/
4067 $$ = NEW_CASE3($2, $4, &@$);
4068 /*% %*/
4069 /*% ripper: case!($2, $4) %*/
4070 }
4071 | k_for for_var keyword_in expr_value_do
4072 compstmt
4073 k_end
4074 {
4075 restore_block_exit(p, $1);
4076 /*%%%*/
4077 /*
4078 * for a, b, c in e
4079 * #=>
4080 * e.each{|*x| a, b, c = x}
4081 *
4082 * for a in e
4083 * #=>
4084 * e.each{|x| a, = x}
4085 */
4086 ID id = internal_id(p);
4087 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4088 rb_node_args_t *args;
4089 NODE *scope, *internal_var = NEW_DVAR(id, &@2);
4090 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4091 tbl->ids[0] = id; /* internal id */
4092
4093 switch (nd_type($2)) {
4094 case NODE_LASGN:
4095 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4096 set_nd_value(p, $2, internal_var);
4097 id = 0;
4098 m->nd_plen = 1;
4099 m->nd_next = $2;
4100 break;
4101 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4102 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
4103 break;
4104 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4105 m->nd_next = node_assign(p, (NODE *)NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
4106 }
4107 /* {|*internal_id| <m> = internal_id; ... } */
4108 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
4109 scope = NEW_SCOPE2(tbl, args, $5, &@$);
4110 $$ = NEW_FOR($4, scope, &@$);
4111 fixpos($$, $2);
4112 /*% %*/
4113 /*% ripper: for!($2, $4, $5) %*/
4114 }
4115 | k_class cpath superclass
4116 {
4117 begin_definition("class", &@k_class, &@cpath);
4118 }
4119 bodystmt
4120 k_end
4121 {
4122 /*%%%*/
4123 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$);
4124 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4125 set_line_body($bodystmt, @superclass.end_pos.lineno);
4126 nd_set_line($$, @superclass.end_pos.lineno);
4127 /*% %*/
4128 /*% ripper: class!($cpath, $superclass, $bodystmt) %*/
4129 local_pop(p);
4130 p->ctxt.in_class = $k_class.in_class;
4131 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4132 }
4133 | k_class tLSHFT expr_value
4134 {
4135 begin_definition("", &@k_class, &@tLSHFT);
4136 }
4137 term
4138 bodystmt
4139 k_end
4140 {
4141 /*%%%*/
4142 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$);
4143 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4144 set_line_body($bodystmt, nd_line($expr_value));
4145 fixpos($$, $expr_value);
4146 /*% %*/
4147 /*% ripper: sclass!($expr_value, $bodystmt) %*/
4148 local_pop(p);
4149 p->ctxt.in_def = $k_class.in_def;
4150 p->ctxt.in_class = $k_class.in_class;
4151 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4152 }
4153 | k_module cpath
4154 {
4155 begin_definition("module", &@k_module, &@cpath);
4156 }
4157 bodystmt
4158 k_end
4159 {
4160 /*%%%*/
4161 $$ = NEW_MODULE($cpath, $bodystmt, &@$);
4162 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4163 set_line_body($bodystmt, @cpath.end_pos.lineno);
4164 nd_set_line($$, @cpath.end_pos.lineno);
4165 /*% %*/
4166 /*% ripper: module!($cpath, $bodystmt) %*/
4167 local_pop(p);
4168 p->ctxt.in_class = $k_module.in_class;
4169 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4170 }
4171 | defn_head[head]
4172 f_arglist[args]
4173 {
4174 /*%%%*/
4175 push_end_expect_token_locations(p, &@head.beg_pos);
4176 /*% %*/
4177 }
4178 bodystmt
4179 k_end
4180 {
4181 restore_defun(p, $head);
4182 /*%%%*/
4183 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4184 ($$ = $head->nd_def)->nd_loc = @$;
4185 RNODE_DEFN($$)->nd_defn = $bodystmt;
4186 /*% %*/
4187 /*% ripper: def!($head->nd_mid, $args, $bodystmt) %*/
4188 local_pop(p);
4189 }
4190 | defs_head[head]
4191 f_arglist[args]
4192 {
4193 /*%%%*/
4194 push_end_expect_token_locations(p, &@head.beg_pos);
4195 /*% %*/
4196 }
4197 bodystmt
4198 k_end
4199 {
4200 restore_defun(p, $head);
4201 /*%%%*/
4202 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4203 ($$ = $head->nd_def)->nd_loc = @$;
4204 RNODE_DEFS($$)->nd_defn = $bodystmt;
4205 /*% %*/
4206 /*% ripper: defs!($head->nd_recv, $head->dot_or_colon, $head->nd_mid, $args, $bodystmt) %*/
4207 local_pop(p);
4208 }
4209 | keyword_break
4210 {
4211 $<node>$ = add_block_exit(p, NEW_BREAK(0, &@$));
4212 /*% ripper: break!(args_new!) %*/
4213 }
4214 | keyword_next
4215 {
4216 $<node>$ = add_block_exit(p, NEW_NEXT(0, &@$));
4217 /*% ripper: next!(args_new!) %*/
4218 }
4219 | keyword_redo
4220 {
4221 $<node>$ = add_block_exit(p, NEW_REDO(&@$));
4222 /*% ripper: redo! %*/
4223 }
4224 | keyword_retry
4225 {
4226 if (!p->ctxt.in_defined) {
4227 switch (p->ctxt.in_rescue) {
4228 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4229 case after_rescue: /* ok */ break;
4230 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4231 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4232 }
4233 }
4234 /*%%%*/
4235 $$ = NEW_RETRY(&@$);
4236 /*% %*/
4237 /*% ripper: retry! %*/
4238 }
4239 ;
4240
4241primary_value : primary
4242 {
4243 value_expr($1);
4244 $$ = $1;
4245 }
4246 ;
4247
4248k_begin : keyword_begin
4249 {
4250 token_info_push(p, "begin", &@$);
4251 /*%%%*/
4252 push_end_expect_token_locations(p, &@1.beg_pos);
4253 /*% %*/
4254 }
4255 ;
4256
4257k_if : keyword_if
4258 {
4259 WARN_EOL("if");
4260 token_info_push(p, "if", &@$);
4261 if (p->token_info && p->token_info->nonspc &&
4262 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4263 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4264 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4265 beg += rb_strlen_lit("else");
4266 while (beg < tok && ISSPACE(*beg)) beg++;
4267 if (beg == tok) {
4268 p->token_info->nonspc = 0;
4269 }
4270 }
4271 /*%%%*/
4272 push_end_expect_token_locations(p, &@1.beg_pos);
4273 /*% %*/
4274 }
4275 ;
4276
4277k_unless : keyword_unless
4278 {
4279 token_info_push(p, "unless", &@$);
4280 /*%%%*/
4281 push_end_expect_token_locations(p, &@1.beg_pos);
4282 /*% %*/
4283 }
4284 ;
4285
4286k_while : keyword_while allow_exits
4287 {
4288 $$ = $allow_exits;
4289 token_info_push(p, "while", &@$);
4290 /*%%%*/
4291 push_end_expect_token_locations(p, &@1.beg_pos);
4292 /*% %*/
4293 }
4294 ;
4295
4296k_until : keyword_until allow_exits
4297 {
4298 $$ = $allow_exits;
4299 token_info_push(p, "until", &@$);
4300 /*%%%*/
4301 push_end_expect_token_locations(p, &@1.beg_pos);
4302 /*% %*/
4303 }
4304 ;
4305
4306k_case : keyword_case
4307 {
4308 token_info_push(p, "case", &@$);
4309 /*%%%*/
4310 push_end_expect_token_locations(p, &@1.beg_pos);
4311 /*% %*/
4312 }
4313 ;
4314
4315k_for : keyword_for allow_exits
4316 {
4317 $$ = $allow_exits;
4318 token_info_push(p, "for", &@$);
4319 /*%%%*/
4320 push_end_expect_token_locations(p, &@1.beg_pos);
4321 /*% %*/
4322 }
4323 ;
4324
4325k_class : keyword_class
4326 {
4327 token_info_push(p, "class", &@$);
4328 $$ = p->ctxt;
4329 p->ctxt.in_rescue = before_rescue;
4330 /*%%%*/
4331 push_end_expect_token_locations(p, &@1.beg_pos);
4332 /*% %*/
4333 }
4334 ;
4335
4336k_module : keyword_module
4337 {
4338 token_info_push(p, "module", &@$);
4339 $$ = p->ctxt;
4340 p->ctxt.in_rescue = before_rescue;
4341 /*%%%*/
4342 push_end_expect_token_locations(p, &@1.beg_pos);
4343 /*% %*/
4344 }
4345 ;
4346
4347k_def : keyword_def
4348 {
4349 token_info_push(p, "def", &@$);
4350 $$ = NEW_DEF_TEMP(&@$);
4351 p->ctxt.in_argdef = 1;
4352 }
4353 ;
4354
4355k_do : keyword_do
4356 {
4357 token_info_push(p, "do", &@$);
4358 /*%%%*/
4359 push_end_expect_token_locations(p, &@1.beg_pos);
4360 /*% %*/
4361 }
4362 ;
4363
4364k_do_block : keyword_do_block
4365 {
4366 token_info_push(p, "do", &@$);
4367 /*%%%*/
4368 push_end_expect_token_locations(p, &@1.beg_pos);
4369 /*% %*/
4370 }
4371 ;
4372
4373k_rescue : keyword_rescue
4374 {
4375 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4376 $$ = p->ctxt;
4377 p->ctxt.in_rescue = after_rescue;
4378 }
4379 ;
4380
4381k_ensure : keyword_ensure
4382 {
4383 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4384 $$ = p->ctxt;
4385 }
4386 ;
4387
4388k_when : keyword_when
4389 {
4390 token_info_warn(p, "when", p->token_info, 0, &@$);
4391 }
4392 ;
4393
4394k_else : keyword_else
4395 {
4396 token_info *ptinfo_beg = p->token_info;
4397 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4398 token_info_warn(p, "else", p->token_info, same, &@$);
4399 if (same) {
4400 token_info e;
4401 e.next = ptinfo_beg->next;
4402 e.token = "else";
4403 token_info_setup(&e, p->lex.pbeg, &@$);
4404 if (!e.nonspc) *ptinfo_beg = e;
4405 }
4406 }
4407 ;
4408
4409k_elsif : keyword_elsif
4410 {
4411 WARN_EOL("elsif");
4412 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4413 }
4414 ;
4415
4416k_end : keyword_end
4417 {
4418 token_info_pop(p, "end", &@$);
4419 /*%%%*/
4420 pop_end_expect_token_locations(p);
4421 /*% %*/
4422 }
4423 | tDUMNY_END
4424 {
4425 compile_error(p, "syntax error, unexpected end-of-input");
4426 }
4427 ;
4428
4429k_return : keyword_return
4430 {
4431 if (p->ctxt.in_class && !p->ctxt.in_def && !dyna_in_block(p))
4432 yyerror1(&@1, "Invalid return in class/module body");
4433 }
4434 ;
4435
4436k_yield : keyword_yield
4437 {
4438 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4439 yyerror1(&@1, "Invalid yield");
4440 }
4441 ;
4442
4443then : term
4444 | keyword_then
4445 | term keyword_then
4446 ;
4447
4448do : term
4449 | keyword_do_cond
4450 ;
4451
4452if_tail : opt_else
4453 | k_elsif expr_value then
4454 compstmt
4455 if_tail
4456 {
4457 /*%%%*/
4458 $$ = new_if(p, $2, $4, $5, &@$);
4459 fixpos($$, $2);
4460 /*% %*/
4461 /*% ripper: elsif!($2, $4, $5) %*/
4462 }
4463 ;
4464
4465opt_else : none
4466 | k_else compstmt
4467 {
4468 /*%%%*/
4469 $$ = $2;
4470 /*% %*/
4471 /*% ripper: else!($2) %*/
4472 }
4473 ;
4474
4475for_var : lhs
4476 | mlhs
4477 ;
4478
4479f_marg : f_norm_arg
4480 {
4481 /*%%%*/
4482 $$ = assignable(p, $1, 0, &@$);
4483 mark_lvar_used(p, $$);
4484 /*% %*/
4485 /*% ripper: assignable(p, $1) %*/
4486 }
4487 | tLPAREN f_margs rparen
4488 {
4489 /*%%%*/
4490 $$ = (NODE *)$2;
4491 /*% %*/
4492 /*% ripper: mlhs_paren!($2) %*/
4493 }
4494 ;
4495
4496f_marg_list : f_marg
4497 {
4498 /*%%%*/
4499 $$ = NEW_LIST($1, &@$);
4500 /*% %*/
4501 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
4502 }
4503 | f_marg_list ',' f_marg
4504 {
4505 /*%%%*/
4506 $$ = list_append(p, $1, $3);
4507 /*% %*/
4508 /*% ripper: mlhs_add!($1, $3) %*/
4509 }
4510 ;
4511
4512f_margs : f_marg_list
4513 {
4514 /*%%%*/
4515 $$ = NEW_MASGN($1, 0, &@$);
4516 /*% %*/
4517 /*% ripper: $1 %*/
4518 }
4519 | f_marg_list ',' f_rest_marg
4520 {
4521 /*%%%*/
4522 $$ = NEW_MASGN($1, $3, &@$);
4523 /*% %*/
4524 /*% ripper: mlhs_add_star!($1, $3) %*/
4525 }
4526 | f_marg_list ',' f_rest_marg ',' f_marg_list
4527 {
4528 /*%%%*/
4529 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4530 /*% %*/
4531 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
4532 }
4533 | f_rest_marg
4534 {
4535 /*%%%*/
4536 $$ = NEW_MASGN(0, $1, &@$);
4537 /*% %*/
4538 /*% ripper: mlhs_add_star!(mlhs_new!, $1) %*/
4539 }
4540 | f_rest_marg ',' f_marg_list
4541 {
4542 /*%%%*/
4543 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4544 /*% %*/
4545 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $1), $3) %*/
4546 }
4547 ;
4548
4549f_rest_marg : tSTAR f_norm_arg
4550 {
4551 /*%%%*/
4552 $$ = assignable(p, $2, 0, &@$);
4553 mark_lvar_used(p, $$);
4554 /*% %*/
4555 /*% ripper: assignable(p, $2) %*/
4556 }
4557 | tSTAR
4558 {
4559 /*%%%*/
4560 $$ = NODE_SPECIAL_NO_NAME_REST;
4561 /*% %*/
4562 /*% ripper: Qnil %*/
4563 }
4564 ;
4565
4566f_any_kwrest : f_kwrest
4567 | f_no_kwarg {$$ = ID2VAL(idNil);}
4568 ;
4569
4570f_eq : {p->ctxt.in_argdef = 0;} '=';
4571
4572block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
4573 {
4574 $$ = new_args_tail(p, $1, $3, $4, &@3);
4575 }
4576 | f_block_kwarg opt_f_block_arg
4577 {
4578 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
4579 }
4580 | f_any_kwrest opt_f_block_arg
4581 {
4582 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
4583 }
4584 | f_block_arg
4585 {
4586 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
4587 }
4588 ;
4589
4590opt_block_args_tail : ',' block_args_tail
4591 {
4592 $$ = $2;
4593 }
4594 | /* none */
4595 {
4596 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4597 }
4598 ;
4599
4600excessed_comma : ','
4601 {
4602 /* magic number for rest_id in iseq_set_arguments() */
4603 /*%%%*/
4604 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4605 /*% %*/
4606 /*% ripper: excessed_comma! %*/
4607 }
4608 ;
4609
4610block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
4611 {
4612 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
4613 }
4614 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
4615 {
4616 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4617 }
4618 | f_arg ',' f_block_optarg opt_block_args_tail
4619 {
4620 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
4621 }
4622 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
4623 {
4624 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4625 }
4626 | f_arg ',' f_rest_arg opt_block_args_tail
4627 {
4628 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4629 }
4630 | f_arg excessed_comma
4631 {
4632 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@2);
4633 $$ = new_args(p, $1, Qnone, $2, Qnone, $$, &@$);
4634 }
4635 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
4636 {
4637 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4638 }
4639 | f_arg opt_block_args_tail
4640 {
4641 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4642 }
4643 | f_block_optarg ',' f_rest_arg opt_block_args_tail
4644 {
4645 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4646 }
4647 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
4648 {
4649 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4650 }
4651 | f_block_optarg opt_block_args_tail
4652 {
4653 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4654 }
4655 | f_block_optarg ',' f_arg opt_block_args_tail
4656 {
4657 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4658 }
4659 | f_rest_arg opt_block_args_tail
4660 {
4661 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4662 }
4663 | f_rest_arg ',' f_arg opt_block_args_tail
4664 {
4665 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4666 }
4667 | block_args_tail
4668 {
4669 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4670 }
4671 ;
4672
4673opt_block_param : none
4674 | block_param_def
4675 {
4676 p->command_start = TRUE;
4677 }
4678 ;
4679
4680block_param_def : '|' opt_bv_decl '|'
4681 {
4682 p->cur_arg = 0;
4683 p->max_numparam = ORDINAL_PARAM;
4684 p->ctxt.in_argdef = 0;
4685 /*%%%*/
4686 $$ = 0;
4687 /*% %*/
4688 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
4689 /*% ripper: block_var!($$, $2) %*/
4690 }
4691 | '|' block_param opt_bv_decl '|'
4692 {
4693 p->cur_arg = 0;
4694 p->max_numparam = ORDINAL_PARAM;
4695 p->ctxt.in_argdef = 0;
4696 /*%%%*/
4697 $$ = $2;
4698 /*% %*/
4699 /*% ripper: block_var!($2, $3) %*/
4700 }
4701 ;
4702
4703
4704opt_bv_decl : opt_nl
4705 {
4706 $$ = 0;
4707 }
4708 | opt_nl ';' bv_decls opt_nl
4709 {
4710 /*%%%*/
4711 $$ = 0;
4712 /*% %*/
4713 /*% ripper: $3 %*/
4714 }
4715 ;
4716
4717bv_decls : bvar
4718 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
4719 | bv_decls ',' bvar
4720 /*% ripper[brace]: rb_ary_push($1, get_value($3)) %*/
4721 ;
4722
4723bvar : tIDENTIFIER
4724 {
4725 new_bv(p, get_id($1));
4726 /*% ripper: get_value($1) %*/
4727 }
4728 | f_bad_arg
4729 {
4730 $$ = 0;
4731 }
4732 ;
4733
4734max_numparam : {
4735 $$ = p->max_numparam;
4736 p->max_numparam = 0;
4737 }
4738 ;
4739
4740numparam : {
4741 $$ = numparam_push(p);
4742 }
4743 ;
4744
4745lambda : tLAMBDA[dyna]
4746 {
4747 token_info_push(p, "->", &@1);
4748 $<vars>dyna = dyna_push(p);
4749 $<num>$ = p->lex.lpar_beg;
4750 p->lex.lpar_beg = p->lex.paren_nest;
4751 }[lpar]
4752 max_numparam numparam allow_exits
4753 f_larglist[args]
4754 {
4755 CMDARG_PUSH(0);
4756 }
4757 lambda_body[body]
4758 {
4759 int max_numparam = p->max_numparam;
4760 p->lex.lpar_beg = $<num>lpar;
4761 p->max_numparam = $max_numparam;
4762 restore_block_exit(p, $allow_exits);
4763 CMDARG_POP();
4764 $args = args_with_numbered(p, $args, max_numparam);
4765 /*%%%*/
4766 {
4767 YYLTYPE loc = code_loc_gen(&@args, &@body);
4768 $$ = NEW_LAMBDA($args, $body, &loc);
4769 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
4770 nd_set_line($$, @args.end_pos.lineno);
4771 nd_set_first_loc($$, @1.beg_pos);
4772 }
4773 /*% %*/
4774 /*% ripper: lambda!($args, $body) %*/
4775 numparam_pop(p, $numparam);
4776 dyna_pop(p, $<vars>dyna);
4777 }
4778 ;
4779
4780f_larglist : '(' f_args opt_bv_decl ')'
4781 {
4782 p->ctxt.in_argdef = 0;
4783 /*%%%*/
4784 $$ = $2;
4785 p->max_numparam = ORDINAL_PARAM;
4786 /*% %*/
4787 /*% ripper: paren!($2) %*/
4788 }
4789 | f_args
4790 {
4791 p->ctxt.in_argdef = 0;
4792 /*%%%*/
4793 if (!args_info_empty_p(&$1->nd_ainfo))
4794 p->max_numparam = ORDINAL_PARAM;
4795 /*% %*/
4796 $$ = $1;
4797 }
4798 ;
4799
4800lambda_body : tLAMBEG compstmt '}'
4801 {
4802 token_info_pop(p, "}", &@3);
4803 $$ = $2;
4804 }
4805 | keyword_do_LAMBDA
4806 {
4807 /*%%%*/
4808 push_end_expect_token_locations(p, &@1.beg_pos);
4809 /*% %*/
4810 }
4811 bodystmt k_end
4812 {
4813 $$ = $3;
4814 }
4815 ;
4816
4817do_block : k_do_block do_body k_end
4818 {
4819 $$ = $2;
4820 /*%%%*/
4821 set_embraced_location($$, &@1, &@3);
4822 /*% %*/
4823 }
4824 ;
4825
4826block_call : command do_block
4827 {
4828 /*%%%*/
4829 if (nd_type_p($1, NODE_YIELD)) {
4830 compile_error(p, "block given to yield");
4831 }
4832 else {
4833 block_dup_check(p, get_nd_args(p, $1), $2);
4834 }
4835 $$ = method_add_block(p, $1, $2, &@$);
4836 fixpos($$, $1);
4837 /*% %*/
4838 /*% ripper: method_add_block!($1, $2) %*/
4839 }
4840 | block_call call_op2 operation2 opt_paren_args
4841 {
4842 /*%%%*/
4843 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4844 /*% %*/
4845 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4846 }
4847 | block_call call_op2 operation2 opt_paren_args brace_block
4848 {
4849 /*%%%*/
4850 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4851 /*% %*/
4852 /*% ripper: opt_event(:method_add_block!, command_call!($1, $2, $3, $4), $5) %*/
4853 }
4854 | block_call call_op2 operation2 command_args do_block
4855 {
4856 /*%%%*/
4857 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4858 /*% %*/
4859 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
4860 }
4861 ;
4862
4863method_call : fcall paren_args
4864 {
4865 /*%%%*/
4866 $1->nd_args = $2;
4867 $$ = (NODE *)$1;
4868 nd_set_last_loc($1, @2.end_pos);
4869 /*% %*/
4870 /*% ripper: method_add_arg!(fcall!($1), $2) %*/
4871 }
4872 | primary_value call_op operation2 opt_paren_args
4873 {
4874 /*%%%*/
4875 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4876 nd_set_line($$, @3.end_pos.lineno);
4877 /*% %*/
4878 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4879 }
4880 | primary_value tCOLON2 operation2 paren_args
4881 {
4882 /*%%%*/
4883 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
4884 nd_set_line($$, @3.end_pos.lineno);
4885 /*% %*/
4886 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
4887 }
4888 | primary_value tCOLON2 operation3
4889 {
4890 /*%%%*/
4891 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
4892 /*% %*/
4893 /*% ripper: call!($1, $2, $3) %*/
4894 }
4895 | primary_value call_op paren_args
4896 {
4897 /*%%%*/
4898 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
4899 nd_set_line($$, @2.end_pos.lineno);
4900 /*% %*/
4901 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
4902 }
4903 | primary_value tCOLON2 paren_args
4904 {
4905 /*%%%*/
4906 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
4907 nd_set_line($$, @2.end_pos.lineno);
4908 /*% %*/
4909 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
4910 }
4911 | keyword_super paren_args
4912 {
4913 /*%%%*/
4914 $$ = NEW_SUPER($2, &@$);
4915 /*% %*/
4916 /*% ripper: super!($2) %*/
4917 }
4918 | keyword_super
4919 {
4920 /*%%%*/
4921 $$ = NEW_ZSUPER(&@$);
4922 /*% %*/
4923 /*% ripper: zsuper! %*/
4924 }
4925 | primary_value '[' opt_call_args rbracket
4926 {
4927 /*%%%*/
4928 $$ = NEW_CALL($1, tAREF, $3, &@$);
4929 fixpos($$, $1);
4930 /*% %*/
4931 /*% ripper: aref!($1, $3) %*/
4932 }
4933 ;
4934
4935brace_block : '{' brace_body '}'
4936 {
4937 $$ = $2;
4938 /*%%%*/
4939 set_embraced_location($$, &@1, &@3);
4940 /*% %*/
4941 }
4942 | k_do do_body k_end
4943 {
4944 $$ = $2;
4945 /*%%%*/
4946 set_embraced_location($$, &@1, &@3);
4947 /*% %*/
4948 }
4949 ;
4950
4951brace_body : {$<vars>$ = dyna_push(p);}[dyna]
4952 max_numparam numparam allow_exits
4953 opt_block_param[args] compstmt
4954 {
4955 int max_numparam = p->max_numparam;
4956 p->max_numparam = $max_numparam;
4957 $args = args_with_numbered(p, $args, max_numparam);
4958 /*%%%*/
4959 $$ = NEW_ITER($args, $compstmt, &@$);
4960 /*% %*/
4961 /*% ripper: brace_block!($args, $compstmt) %*/
4962 restore_block_exit(p, $allow_exits);
4963 numparam_pop(p, $numparam);
4964 dyna_pop(p, $<vars>dyna);
4965 }
4966 ;
4967
4968do_body : {
4969 $<vars>$ = dyna_push(p);
4970 CMDARG_PUSH(0);
4971 }[dyna]
4972 max_numparam numparam allow_exits
4973 opt_block_param[args] bodystmt
4974 {
4975 int max_numparam = p->max_numparam;
4976 p->max_numparam = $max_numparam;
4977 $args = args_with_numbered(p, $args, max_numparam);
4978 /*%%%*/
4979 $$ = NEW_ITER($args, $bodystmt, &@$);
4980 /*% %*/
4981 /*% ripper: do_block!($args, $bodystmt) %*/
4982 CMDARG_POP();
4983 restore_block_exit(p, $allow_exits);
4984 numparam_pop(p, $numparam);
4985 dyna_pop(p, $<vars>dyna);
4986 }
4987 ;
4988
4989case_args : arg_value
4990 {
4991 /*%%%*/
4992 check_literal_when(p, $1, &@1);
4993 $$ = NEW_LIST($1, &@$);
4994 /*% %*/
4995 /*% ripper: args_add!(args_new!, $1) %*/
4996 }
4997 | tSTAR arg_value
4998 {
4999 /*%%%*/
5000 $$ = NEW_SPLAT($2, &@$);
5001 /*% %*/
5002 /*% ripper: args_add_star!(args_new!, $2) %*/
5003 }
5004 | case_args ',' arg_value
5005 {
5006 /*%%%*/
5007 check_literal_when(p, $3, &@3);
5008 $$ = last_arg_append(p, $1, $3, &@$);
5009 /*% %*/
5010 /*% ripper: args_add!($1, $3) %*/
5011 }
5012 | case_args ',' tSTAR arg_value
5013 {
5014 /*%%%*/
5015 $$ = rest_arg_append(p, $1, $4, &@$);
5016 /*% %*/
5017 /*% ripper: args_add_star!($1, $4) %*/
5018 }
5019 ;
5020
5021case_body : k_when case_args then
5022 compstmt
5023 cases
5024 {
5025 /*%%%*/
5026 $$ = NEW_WHEN($2, $4, $5, &@$);
5027 fixpos($$, $2);
5028 /*% %*/
5029 /*% ripper: when!($2, $4, $5) %*/
5030 }
5031 ;
5032
5033cases : opt_else
5034 | case_body
5035 ;
5036
5037p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5038p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5039
5040p_in_kwarg : {
5041 $$ = p->ctxt;
5042 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5043 p->command_start = FALSE;
5044 p->ctxt.in_kwarg = 1;
5045 }
5046 ;
5047
5048p_case_body : keyword_in
5049 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5050 p_top_expr[expr] then
5051 {
5052 pop_pktbl(p, $p_pktbl);
5053 pop_pvtbl(p, $p_pvtbl);
5054 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5055 }
5056 compstmt
5057 p_cases[cases]
5058 {
5059 /*%%%*/
5060 $$ = NEW_IN($expr, $compstmt, $cases, &@$);
5061 /*% %*/
5062 /*% ripper: in!($expr, $compstmt, $cases) %*/
5063 }
5064 ;
5065
5066p_cases : opt_else
5067 | p_case_body
5068 ;
5069
5070p_top_expr : p_top_expr_body
5071 | p_top_expr_body modifier_if expr_value
5072 {
5073 /*%%%*/
5074 $$ = new_if(p, $3, $1, 0, &@$);
5075 fixpos($$, $3);
5076 /*% %*/
5077 /*% ripper: if_mod!($3, $1) %*/
5078 }
5079 | p_top_expr_body modifier_unless expr_value
5080 {
5081 /*%%%*/
5082 $$ = new_unless(p, $3, $1, 0, &@$);
5083 fixpos($$, $3);
5084 /*% %*/
5085 /*% ripper: unless_mod!($3, $1) %*/
5086 }
5087 ;
5088
5089p_top_expr_body : p_expr
5090 | p_expr ','
5091 {
5092 $$ = new_array_pattern_tail(p, Qnone, 1, Qnone, Qnone, &@$);
5093 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
5094 }
5095 | p_expr ',' p_args
5096 {
5097 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
5098 /*%%%*/
5099 nd_set_first_loc($$, @1.beg_pos);
5100 /*%
5101 %*/
5102 }
5103 | p_find
5104 {
5105 $$ = new_find_pattern(p, Qnone, $1, &@$);
5106 }
5107 | p_args_tail
5108 {
5109 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
5110 }
5111 | p_kwargs
5112 {
5113 $$ = new_hash_pattern(p, Qnone, $1, &@$);
5114 }
5115 ;
5116
5117p_expr : p_as
5118 ;
5119
5120p_as : p_expr tASSOC p_variable
5121 {
5122 /*%%%*/
5123 NODE *n = NEW_LIST($1, &@$);
5124 n = list_append(p, n, $3);
5125 $$ = new_hash(p, n, &@$);
5126 /*% %*/
5127 /*% ripper: binary!($1, STATIC_ID2SYM((id_assoc)), $3) %*/
5128 }
5129 | p_alt
5130 ;
5131
5132p_alt : p_alt '|' p_expr_basic
5133 {
5134 /*%%%*/
5135 $$ = NEW_OR($1, $3, &@$);
5136 /*% %*/
5137 /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
5138 }
5139 | p_expr_basic
5140 ;
5141
5142p_lparen : '(' p_pktbl { $$ = $2;};
5143p_lbracket : '[' p_pktbl { $$ = $2;};
5144
5145p_expr_basic : p_value
5146 | p_variable
5147 | p_const p_lparen[p_pktbl] p_args rparen
5148 {
5149 pop_pktbl(p, $p_pktbl);
5150 $$ = new_array_pattern(p, $p_const, Qnone, $p_args, &@$);
5151 /*%%%*/
5152 nd_set_first_loc($$, @p_const.beg_pos);
5153 /*%
5154 %*/
5155 }
5156 | p_const p_lparen[p_pktbl] p_find rparen
5157 {
5158 pop_pktbl(p, $p_pktbl);
5159 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5160 /*%%%*/
5161 nd_set_first_loc($$, @p_const.beg_pos);
5162 /*%
5163 %*/
5164 }
5165 | p_const p_lparen[p_pktbl] p_kwargs rparen
5166 {
5167 pop_pktbl(p, $p_pktbl);
5168 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5169 /*%%%*/
5170 nd_set_first_loc($$, @p_const.beg_pos);
5171 /*%
5172 %*/
5173 }
5174 | p_const '(' rparen
5175 {
5176 $$ = new_array_pattern_tail(p, Qnone, 0, Qnone, Qnone, &@$);
5177 $$ = new_array_pattern(p, $p_const, Qnone, $$, &@$);
5178 }
5179 | p_const p_lbracket[p_pktbl] p_args rbracket
5180 {
5181 pop_pktbl(p, $p_pktbl);
5182 $$ = new_array_pattern(p, $p_const, Qnone, $p_args, &@$);
5183 /*%%%*/
5184 nd_set_first_loc($$, @p_const.beg_pos);
5185 /*%
5186 %*/
5187 }
5188 | p_const p_lbracket[p_pktbl] p_find rbracket
5189 {
5190 pop_pktbl(p, $p_pktbl);
5191 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5192 /*%%%*/
5193 nd_set_first_loc($$, @p_const.beg_pos);
5194 /*%
5195 %*/
5196 }
5197 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5198 {
5199 pop_pktbl(p, $p_pktbl);
5200 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5201 /*%%%*/
5202 nd_set_first_loc($$, @p_const.beg_pos);
5203 /*%
5204 %*/
5205 }
5206 | p_const '[' rbracket
5207 {
5208 $$ = new_array_pattern_tail(p, Qnone, 0, Qnone, Qnone, &@$);
5209 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
5210 }
5211 | tLBRACK p_args rbracket
5212 {
5213 $$ = new_array_pattern(p, Qnone, Qnone, $p_args, &@$);
5214 }
5215 | tLBRACK p_find rbracket
5216 {
5217 $$ = new_find_pattern(p, Qnone, $p_find, &@$);
5218 }
5219 | tLBRACK rbracket
5220 {
5221 $$ = new_array_pattern_tail(p, Qnone, 0, Qnone, Qnone, &@$);
5222 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
5223 }
5224 | tLBRACE p_pktbl lex_ctxt[ctxt]
5225 {
5226 p->ctxt.in_kwarg = 0;
5227 }
5228 p_kwargs rbrace
5229 {
5230 pop_pktbl(p, $p_pktbl);
5231 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5232 $$ = new_hash_pattern(p, Qnone, $p_kwargs, &@$);
5233 }
5234 | tLBRACE rbrace
5235 {
5236 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
5237 $$ = new_hash_pattern(p, Qnone, $$, &@$);
5238 }
5239 | tLPAREN p_pktbl p_expr rparen
5240 {
5241 pop_pktbl(p, $p_pktbl);
5242 $$ = $p_expr;
5243 }
5244 ;
5245
5246p_args : p_expr
5247 {
5248 /*%%%*/
5249 NODE *pre_args = NEW_LIST($1, &@$);
5250 $$ = new_array_pattern_tail(p, pre_args, 0, Qnone, Qnone, &@$);
5251 /*%
5252 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, Qnone, Qnone, &@$);
5253 %*/
5254 }
5255 | p_args_head
5256 {
5257 $$ = new_array_pattern_tail(p, $1, 1, Qnone, Qnone, &@$);
5258 }
5259 | p_args_head p_arg
5260 {
5261 /*%%%*/
5262 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, Qnone, Qnone, &@$);
5263 /*%
5264 VALUE pre_args = rb_ary_concat($1, get_value($2));
5265 $$ = new_array_pattern_tail(p, pre_args, 0, Qnone, Qnone, &@$);
5266 %*/
5267 }
5268 | p_args_head p_rest
5269 {
5270 $$ = new_array_pattern_tail(p, $1, 1, $2, Qnone, &@$);
5271 }
5272 | p_args_head p_rest ',' p_args_post
5273 {
5274 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5275 }
5276 | p_args_tail
5277 ;
5278
5279p_args_head : p_arg ','
5280 {
5281 $$ = $1;
5282 }
5283 | p_args_head p_arg ','
5284 {
5285 /*%%%*/
5286 $$ = list_concat($1, $2);
5287 /*% %*/
5288 /*% ripper: rb_ary_concat($1, get_value($2)) %*/
5289 }
5290 ;
5291
5292p_args_tail : p_rest
5293 {
5294 $$ = new_array_pattern_tail(p, Qnone, 1, $1, Qnone, &@$);
5295 }
5296 | p_rest ',' p_args_post
5297 {
5298 $$ = new_array_pattern_tail(p, Qnone, 1, $1, $3, &@$);
5299 }
5300 ;
5301
5302p_find : p_rest ',' p_args_post ',' p_rest
5303 {
5304 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5305 }
5306 ;
5307
5308
5309p_rest : tSTAR tIDENTIFIER
5310 {
5311 /*%%%*/
5312 error_duplicate_pattern_variable(p, $2, &@2);
5313 $$ = assignable(p, $2, 0, &@$);
5314 /*% %*/
5315 /*% ripper: assignable(p, var_field(p, $2)) %*/
5316 }
5317 | tSTAR
5318 {
5319 /*%%%*/
5320 $$ = 0;
5321 /*% %*/
5322 /*% ripper: var_field(p, Qnil) %*/
5323 }
5324 ;
5325
5326p_args_post : p_arg
5327 | p_args_post ',' p_arg
5328 {
5329 /*%%%*/
5330 $$ = list_concat($1, $3);
5331 /*% %*/
5332 /*% ripper: rb_ary_concat($1, get_value($3)) %*/
5333 }
5334 ;
5335
5336p_arg : p_expr
5337 {
5338 /*%%%*/
5339 $$ = NEW_LIST($1, &@$);
5340 /*% %*/
5341 /*% ripper: rb_ary_new_from_args(1, get_value($1)) %*/
5342 }
5343 ;
5344
5345p_kwargs : p_kwarg ',' p_any_kwrest
5346 {
5347 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5348 }
5349 | p_kwarg
5350 {
5351 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5352 }
5353 | p_kwarg ','
5354 {
5355 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5356 }
5357 | p_any_kwrest
5358 {
5359 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
5360 }
5361 ;
5362
5363p_kwarg : p_kw
5364 /*% ripper[brace]: rb_ary_new_from_args(1, $1) %*/
5365 | p_kwarg ',' p_kw
5366 {
5367 /*%%%*/
5368 $$ = list_concat($1, $3);
5369 /*% %*/
5370 /*% ripper: rb_ary_push($1, $3) %*/
5371 }
5372 ;
5373
5374p_kw : p_kw_label p_expr
5375 {
5376 error_duplicate_pattern_key(p, get_id($1), &@1);
5377 /*%%%*/
5378 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5379 /*% %*/
5380 /*% ripper: rb_ary_new_from_args(2, get_value($1), get_value($2)) %*/
5381 }
5382 | p_kw_label
5383 {
5384 error_duplicate_pattern_key(p, get_id($1), &@1);
5385 if ($1 && !is_local_id(get_id($1))) {
5386 yyerror1(&@1, "key must be valid as local variables");
5387 }
5388 error_duplicate_pattern_variable(p, get_id($1), &@1);
5389 /*%%%*/
5390 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
5391 /*% %*/
5392 /*% ripper: rb_ary_new_from_args(2, get_value(assignable(p, $1)), Qnil) %*/
5393 }
5394 ;
5395
5396p_kw_label : tLABEL
5397 | tSTRING_BEG string_contents tLABEL_END
5398 {
5399 YYLTYPE loc = code_loc_gen(&@1, &@3);
5400 /*%%%*/
5401 if (!$2 || nd_type_p($2, NODE_STR)) {
5402 NODE *node = dsym_node(p, $2, &loc);
5403 $$ = SYM2ID(RNODE_LIT(node)->nd_lit);
5404 }
5405 /*%
5406 if (ripper_is_node_yylval(p, $2) && RNODE_RIPPER($2)->nd_cval) {
5407 VALUE label = RNODE_RIPPER($2)->nd_cval;
5408 VALUE rval = RNODE_RIPPER($2)->nd_rval;
5409 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
5410 RNODE($$)->nd_loc = loc;
5411 }
5412 %*/
5413 else {
5414 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5415 $$ = 0;
5416 }
5417 }
5418 ;
5419
5420p_kwrest : kwrest_mark tIDENTIFIER
5421 {
5422 $$ = $2;
5423 }
5424 | kwrest_mark
5425 {
5426 $$ = 0;
5427 }
5428 ;
5429
5430p_kwnorest : kwrest_mark keyword_nil
5431 {
5432 $$ = 0;
5433 }
5434 ;
5435
5436p_any_kwrest : p_kwrest
5437 | p_kwnorest {$$ = ID2VAL(idNil);}
5438 ;
5439
5440p_value : p_primitive
5441 | p_primitive tDOT2 p_primitive
5442 {
5443 /*%%%*/
5444 value_expr($1);
5445 value_expr($3);
5446 $$ = NEW_DOT2($1, $3, &@$);
5447 /*% %*/
5448 /*% ripper: dot2!($1, $3) %*/
5449 }
5450 | p_primitive tDOT3 p_primitive
5451 {
5452 /*%%%*/
5453 value_expr($1);
5454 value_expr($3);
5455 $$ = NEW_DOT3($1, $3, &@$);
5456 /*% %*/
5457 /*% ripper: dot3!($1, $3) %*/
5458 }
5459 | p_primitive tDOT2
5460 {
5461 /*%%%*/
5462 value_expr($1);
5463 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
5464 /*% %*/
5465 /*% ripper: dot2!($1, Qnil) %*/
5466 }
5467 | p_primitive tDOT3
5468 {
5469 /*%%%*/
5470 value_expr($1);
5471 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
5472 /*% %*/
5473 /*% ripper: dot3!($1, Qnil) %*/
5474 }
5475 | p_var_ref
5476 | p_expr_ref
5477 | p_const
5478 | tBDOT2 p_primitive
5479 {
5480 /*%%%*/
5481 value_expr($2);
5482 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
5483 /*% %*/
5484 /*% ripper: dot2!(Qnil, $2) %*/
5485 }
5486 | tBDOT3 p_primitive
5487 {
5488 /*%%%*/
5489 value_expr($2);
5490 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
5491 /*% %*/
5492 /*% ripper: dot3!(Qnil, $2) %*/
5493 }
5494 ;
5495
5496p_primitive : literal
5497 | strings
5498 | xstring
5499 | regexp
5500 | words
5501 | qwords
5502 | symbols
5503 | qsymbols
5504 | keyword_variable
5505 {
5506 /*%%%*/
5507 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5508 /*% %*/
5509 /*% ripper: var_ref!($1) %*/
5510 }
5511 | lambda
5512 ;
5513
5514p_variable : tIDENTIFIER
5515 {
5516 /*%%%*/
5517 error_duplicate_pattern_variable(p, $1, &@1);
5518 $$ = assignable(p, $1, 0, &@$);
5519 /*% %*/
5520 /*% ripper: assignable(p, var_field(p, $1)) %*/
5521 }
5522 ;
5523
5524p_var_ref : '^' tIDENTIFIER
5525 {
5526 /*%%%*/
5527 NODE *n = gettable(p, $2, &@$);
5528 if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5529 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5530 }
5531 $$ = n;
5532 /*% %*/
5533 /*% ripper: var_ref!($2) %*/
5534 }
5535 | '^' nonlocal_var
5536 {
5537 /*%%%*/
5538 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_BEGIN(0, &@$);
5539 /*% %*/
5540 /*% ripper: var_ref!($2) %*/
5541 }
5542 ;
5543
5544p_expr_ref : '^' tLPAREN expr_value rparen
5545 {
5546 /*%%%*/
5547 $$ = NEW_BLOCK($3, &@$);
5548 /*% %*/
5549 /*% ripper: begin!($3) %*/
5550 }
5551 ;
5552
5553p_const : tCOLON3 cname
5554 {
5555 /*%%%*/
5556 $$ = NEW_COLON3($2, &@$);
5557 /*% %*/
5558 /*% ripper: top_const_ref!($2) %*/
5559 }
5560 | p_const tCOLON2 cname
5561 {
5562 /*%%%*/
5563 $$ = NEW_COLON2($1, $3, &@$);
5564 /*% %*/
5565 /*% ripper: const_path_ref!($1, $3) %*/
5566 }
5567 | tCONSTANT
5568 {
5569 /*%%%*/
5570 $$ = gettable(p, $1, &@$);
5571 /*% %*/
5572 /*% ripper: var_ref!($1) %*/
5573 }
5574 ;
5575
5576opt_rescue : k_rescue exc_list exc_var then
5577 compstmt
5578 opt_rescue
5579 {
5580 /*%%%*/
5581 NODE *body = $5;
5582 if ($3) {
5583 NODE *err = NEW_ERRINFO(&@3);
5584 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5585 body = block_append(p, err, body);
5586 }
5587 $$ = NEW_RESBODY($2, body, $6, &@$);
5588 if ($2) {
5589 fixpos($$, $2);
5590 }
5591 else if ($3) {
5592 fixpos($$, $3);
5593 }
5594 else {
5595 fixpos($$, $5);
5596 }
5597 /*% %*/
5598 /*% ripper: rescue!($2, $3, $5, $6) %*/
5599 }
5600 | none
5601 ;
5602
5603exc_list : arg_value
5604 {
5605 /*%%%*/
5606 $$ = NEW_LIST($1, &@$);
5607 /*% %*/
5608 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5609 }
5610 | mrhs
5611 {
5612 /*%%%*/
5613 if (!($$ = splat_array($1))) $$ = $1;
5614 /*% %*/
5615 /*% ripper: $1 %*/
5616 }
5617 | none
5618 ;
5619
5620exc_var : tASSOC lhs
5621 {
5622 $$ = $2;
5623 }
5624 | none
5625 ;
5626
5627opt_ensure : k_ensure compstmt
5628 {
5629 p->ctxt.in_rescue = $1.in_rescue;
5630 /*%%%*/
5631 $$ = $2;
5632 /*% %*/
5633 /*% ripper: ensure!($2) %*/
5634 }
5635 | none
5636 ;
5637
5638literal : numeric
5639 | symbol
5640 ;
5641
5642strings : string
5643 {
5644 /*%%%*/
5645 NODE *node = $1;
5646 if (!node) {
5647 node = NEW_STR(STR_NEW0(), &@$);
5648 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(node)->nd_lit);
5649 }
5650 else {
5651 node = evstr2dstr(p, node);
5652 }
5653 $$ = node;
5654 /*% %*/
5655 /*% ripper: $1 %*/
5656 }
5657 ;
5658
5659string : tCHAR
5660 | string1
5661 | string string1
5662 {
5663 /*%%%*/
5664 $$ = literal_concat(p, $1, $2, &@$);
5665 /*% %*/
5666 /*% ripper: string_concat!($1, $2) %*/
5667 }
5668 ;
5669
5670string1 : tSTRING_BEG string_contents tSTRING_END
5671 {
5672 /*%%%*/
5673 $$ = heredoc_dedent(p, $2);
5674 if ($$) nd_set_loc($$, &@$);
5675 /*% %*/
5676 /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/
5677 }
5678 ;
5679
5680xstring : tXSTRING_BEG xstring_contents tSTRING_END
5681 {
5682 /*%%%*/
5683 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5684 /*% %*/
5685 /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/
5686 }
5687 ;
5688
5689regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5690 {
5691 $$ = new_regexp(p, $2, $3, &@$);
5692 }
5693 ;
5694
5695words_sep : ' ' {}
5696 | words_sep ' '
5697 ;
5698
5699words : tWORDS_BEG words_sep word_list tSTRING_END
5700 {
5701 /*%%%*/
5702 $$ = make_list($3, &@$);
5703 /*% %*/
5704 /*% ripper: array!($3) %*/
5705 }
5706 ;
5707
5708word_list : /* none */
5709 {
5710 /*%%%*/
5711 $$ = 0;
5712 /*% %*/
5713 /*% ripper: words_new! %*/
5714 }
5715 | word_list word words_sep
5716 {
5717 /*%%%*/
5718 $$ = list_append(p, $1, evstr2dstr(p, $2));
5719 /*% %*/
5720 /*% ripper: words_add!($1, $2) %*/
5721 }
5722 ;
5723
5724word : string_content
5725 /*% ripper[brace]: word_add!(word_new!, $1) %*/
5726 | word string_content
5727 {
5728 /*%%%*/
5729 $$ = literal_concat(p, $1, $2, &@$);
5730 /*% %*/
5731 /*% ripper: word_add!($1, $2) %*/
5732 }
5733 ;
5734
5735symbols : tSYMBOLS_BEG words_sep symbol_list tSTRING_END
5736 {
5737 /*%%%*/
5738 $$ = make_list($3, &@$);
5739 /*% %*/
5740 /*% ripper: array!($3) %*/
5741 }
5742 ;
5743
5744symbol_list : /* none */
5745 {
5746 /*%%%*/
5747 $$ = 0;
5748 /*% %*/
5749 /*% ripper: symbols_new! %*/
5750 }
5751 | symbol_list word words_sep
5752 {
5753 /*%%%*/
5754 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5755 /*% %*/
5756 /*% ripper: symbols_add!($1, $2) %*/
5757 }
5758 ;
5759
5760qwords : tQWORDS_BEG words_sep qword_list tSTRING_END
5761 {
5762 /*%%%*/
5763 $$ = make_list($3, &@$);
5764 /*% %*/
5765 /*% ripper: array!($3) %*/
5766 }
5767 ;
5768
5769qsymbols : tQSYMBOLS_BEG words_sep qsym_list tSTRING_END
5770 {
5771 /*%%%*/
5772 $$ = make_list($3, &@$);
5773 /*% %*/
5774 /*% ripper: array!($3) %*/
5775 }
5776 ;
5777
5778qword_list : /* none */
5779 {
5780 /*%%%*/
5781 $$ = 0;
5782 /*% %*/
5783 /*% ripper: qwords_new! %*/
5784 }
5785 | qword_list tSTRING_CONTENT words_sep
5786 {
5787 /*%%%*/
5788 $$ = list_append(p, $1, $2);
5789 /*% %*/
5790 /*% ripper: qwords_add!($1, $2) %*/
5791 }
5792 ;
5793
5794qsym_list : /* none */
5795 {
5796 /*%%%*/
5797 $$ = 0;
5798 /*% %*/
5799 /*% ripper: qsymbols_new! %*/
5800 }
5801 | qsym_list tSTRING_CONTENT words_sep
5802 {
5803 /*%%%*/
5804 $$ = symbol_append(p, $1, $2);
5805 /*% %*/
5806 /*% ripper: qsymbols_add!($1, $2) %*/
5807 }
5808 ;
5809
5810string_contents : /* none */
5811 {
5812 /*%%%*/
5813 $$ = 0;
5814 /*% %*/
5815 /*% ripper: string_content! %*/
5816 /*%%%*/
5817 /*%
5818 $$ = ripper_new_yylval(p, 0, $$, 0);
5819 %*/
5820 }
5821 | string_contents string_content
5822 {
5823 /*%%%*/
5824 $$ = literal_concat(p, $1, $2, &@$);
5825 /*% %*/
5826 /*% ripper: string_add!($1, $2) %*/
5827 /*%%%*/
5828 /*%
5829 if (ripper_is_node_yylval(p, $1) && ripper_is_node_yylval(p, $2) &&
5830 !RNODE_RIPPER($1)->nd_cval) {
5831 RNODE_RIPPER($1)->nd_cval = RNODE_RIPPER($2)->nd_cval;
5832 RNODE_RIPPER($1)->nd_rval = add_mark_object(p, $$);
5833 $$ = $1;
5834 }
5835 %*/
5836 }
5837 ;
5838
5839xstring_contents: /* none */
5840 {
5841 /*%%%*/
5842 $$ = 0;
5843 /*% %*/
5844 /*% ripper: xstring_new! %*/
5845 }
5846 | xstring_contents string_content
5847 {
5848 /*%%%*/
5849 $$ = literal_concat(p, $1, $2, &@$);
5850 /*% %*/
5851 /*% ripper: xstring_add!($1, $2) %*/
5852 }
5853 ;
5854
5855regexp_contents: /* none */
5856 {
5857 /*%%%*/
5858 $$ = 0;
5859 /*% %*/
5860 /*% ripper: regexp_new! %*/
5861 /*%%%*/
5862 /*%
5863 $$ = ripper_new_yylval(p, 0, $$, 0);
5864 %*/
5865 }
5866 | regexp_contents string_content
5867 {
5868 /*%%%*/
5869 NODE *head = $1, *tail = $2;
5870 if (!head) {
5871 $$ = tail;
5872 }
5873 else if (!tail) {
5874 $$ = head;
5875 }
5876 else {
5877 switch (nd_type(head)) {
5878 case NODE_STR:
5879 head = str2dstr(p, head);
5880 break;
5881 case NODE_DSTR:
5882 break;
5883 default:
5884 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
5885 break;
5886 }
5887 $$ = list_append(p, head, tail);
5888 }
5889 /*%
5890 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
5891 if (ripper_is_node_yylval(p, n1)) {
5892 s1 = RNODE_RIPPER(n1)->nd_cval;
5893 n1 = RNODE_RIPPER(n1)->nd_rval;
5894 }
5895 if (ripper_is_node_yylval(p, n2)) {
5896 s2 = RNODE_RIPPER(n2)->nd_cval;
5897 n2 = RNODE_RIPPER(n2)->nd_rval;
5898 }
5899 $$ = dispatch2(regexp_add, n1, n2);
5900 if (!s1 && s2) {
5901 $$ = ripper_new_yylval(p, 0, $$, s2);
5902 }
5903 %*/
5904 }
5905 ;
5906
5907string_content : tSTRING_CONTENT
5908 /*% ripper[brace]: ripper_new_yylval(p, 0, get_value($1), $1) %*/
5909 | tSTRING_DVAR
5910 {
5911 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
5912 $<strterm>$ = p->lex.strterm;
5913 p->lex.strterm = 0;
5914 SET_LEX_STATE(EXPR_BEG);
5915 }
5916 string_dvar
5917 {
5918 p->lex.strterm = $<strterm>2;
5919 /*%%%*/
5920 $$ = NEW_EVSTR($3, &@$);
5921 nd_set_line($$, @3.end_pos.lineno);
5922 /*% %*/
5923 /*% ripper: string_dvar!($3) %*/
5924 }
5925 | tSTRING_DBEG[term]
5926 {
5927 CMDARG_PUSH(0);
5928 COND_PUSH(0);
5929 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
5930 $<strterm>term = p->lex.strterm;
5931 p->lex.strterm = 0;
5932 $<num>$ = p->lex.state;
5933 SET_LEX_STATE(EXPR_BEG);
5934 }[state]
5935 {
5936 $<num>$ = p->lex.brace_nest;
5937 p->lex.brace_nest = 0;
5938 }[brace]
5939 {
5940 $<num>$ = p->heredoc_indent;
5941 p->heredoc_indent = 0;
5942 }[indent]
5943 compstmt string_dend
5944 {
5945 COND_POP();
5946 CMDARG_POP();
5947 p->lex.strterm = $<strterm>term;
5948 SET_LEX_STATE($<num>state);
5949 p->lex.brace_nest = $<num>brace;
5950 p->heredoc_indent = $<num>indent;
5951 p->heredoc_line_indent = -1;
5952 /*%%%*/
5953 if ($compstmt) nd_unset_fl_newline($compstmt);
5954 $$ = new_evstr(p, $compstmt, &@$);
5955 /*% %*/
5956 /*% ripper: string_embexpr!($compstmt) %*/
5957 }
5958 ;
5959
5960string_dend : tSTRING_DEND
5961 | END_OF_INPUT
5962 ;
5963
5964string_dvar : nonlocal_var
5965 {
5966 /*%%%*/
5967 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5968 /*% %*/
5969 /*% ripper: var_ref!($1) %*/
5970 }
5971 | backref
5972 ;
5973
5974symbol : ssym
5975 | dsym
5976 ;
5977
5978ssym : tSYMBEG sym
5979 {
5980 SET_LEX_STATE(EXPR_END);
5981 /*%%%*/
5982 $$ = NEW_LIT(ID2SYM($2), &@$);
5983 /*% %*/
5984 /*% ripper: symbol_literal!(symbol!($2)) %*/
5985 }
5986 ;
5987
5988sym : fname
5989 | nonlocal_var
5990 ;
5991
5992dsym : tSYMBEG string_contents tSTRING_END
5993 {
5994 SET_LEX_STATE(EXPR_END);
5995 /*%%%*/
5996 $$ = dsym_node(p, $2, &@$);
5997 /*% %*/
5998 /*% ripper: dyna_symbol!($2) %*/
5999 }
6000 ;
6001
6002numeric : simple_numeric
6003 | tUMINUS_NUM simple_numeric %prec tLOWEST
6004 {
6005 /*%%%*/
6006 $$ = $2;
6007 RB_OBJ_WRITE(p->ast, &RNODE_LIT($$)->nd_lit, negate_lit(p, RNODE_LIT($$)->nd_lit));
6008 /*% %*/
6009 /*% ripper: unary!(ID2VAL(idUMinus), $2) %*/
6010 }
6011 ;
6012
6013simple_numeric : tINTEGER
6014 | tFLOAT
6015 | tRATIONAL
6016 | tIMAGINARY
6017 ;
6018
6019nonlocal_var : tIVAR
6020 | tGVAR
6021 | tCVAR
6022 ;
6023
6024user_variable : tIDENTIFIER
6025 | tCONSTANT
6026 | nonlocal_var
6027 ;
6028
6029keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6030 | keyword_self {$$ = KWD2EID(self, $1);}
6031 | keyword_true {$$ = KWD2EID(true, $1);}
6032 | keyword_false {$$ = KWD2EID(false, $1);}
6033 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6034 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6035 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6036 ;
6037
6038var_ref : user_variable
6039 {
6040 /*%%%*/
6041 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
6042 /*%
6043 if (id_is_var(p, get_id($1))) {
6044 $$ = dispatch1(var_ref, $1);
6045 }
6046 else {
6047 $$ = dispatch1(vcall, $1);
6048 }
6049 %*/
6050 }
6051 | keyword_variable
6052 {
6053 /*%%%*/
6054 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6055 /*% %*/
6056 /*% ripper: var_ref!($1) %*/
6057 }
6058 ;
6059
6060var_lhs : user_variable
6061 {
6062 /*%%%*/
6063 $$ = assignable(p, $1, 0, &@$);
6064 /*% %*/
6065 /*% ripper: assignable(p, var_field(p, $1)) %*/
6066 }
6067 | keyword_variable
6068 {
6069 /*%%%*/
6070 $$ = assignable(p, $1, 0, &@$);
6071 /*% %*/
6072 /*% ripper: assignable(p, var_field(p, $1)) %*/
6073 }
6074 ;
6075
6076backref : tNTH_REF
6077 | tBACK_REF
6078 ;
6079
6080superclass : '<'
6081 {
6082 SET_LEX_STATE(EXPR_BEG);
6083 p->command_start = TRUE;
6084 }
6085 expr_value term
6086 {
6087 $$ = $3;
6088 }
6089 | /* none */
6090 {
6091 /*%%%*/
6092 $$ = 0;
6093 /*% %*/
6094 /*% ripper: Qnil %*/
6095 }
6096 ;
6097
6098f_opt_paren_args: f_paren_args
6099 | none
6100 {
6101 p->ctxt.in_argdef = 0;
6102 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
6103 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
6104 }
6105 ;
6106
6107f_paren_args : '(' f_args rparen
6108 {
6109 /*%%%*/
6110 $$ = $2;
6111 /*% %*/
6112 /*% ripper: paren!($2) %*/
6113 SET_LEX_STATE(EXPR_BEG);
6114 p->command_start = TRUE;
6115 p->ctxt.in_argdef = 0;
6116 }
6117 ;
6118
6119f_arglist : f_paren_args
6120 | {
6121 $<ctxt>$ = p->ctxt;
6122 p->ctxt.in_kwarg = 1;
6123 p->ctxt.in_argdef = 1;
6124 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6125 }
6126 f_args term
6127 {
6128 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
6129 p->ctxt.in_argdef = 0;
6130 $$ = $2;
6131 SET_LEX_STATE(EXPR_BEG);
6132 p->command_start = TRUE;
6133 }
6134 ;
6135
6136args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
6137 {
6138 $$ = new_args_tail(p, $1, $3, $4, &@3);
6139 }
6140 | f_kwarg opt_f_block_arg
6141 {
6142 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
6143 }
6144 | f_any_kwrest opt_f_block_arg
6145 {
6146 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
6147 }
6148 | f_block_arg
6149 {
6150 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
6151 }
6152 | args_forward
6153 {
6154 add_forwarding_args(p);
6155 $$ = new_args_tail(p, Qnone, $1, arg_FWD_BLOCK, &@1);
6156 /*%%%*/
6157 $$->nd_ainfo.forwarding = 1;
6158 /*% %*/
6159 }
6160 ;
6161
6162opt_args_tail : ',' args_tail
6163 {
6164 $$ = $2;
6165 }
6166 | /* none */
6167 {
6168 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
6169 }
6170 ;
6171
6172f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
6173 {
6174 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
6175 }
6176 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
6177 {
6178 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6179 }
6180 | f_arg ',' f_optarg opt_args_tail
6181 {
6182 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
6183 }
6184 | f_arg ',' f_optarg ',' f_arg opt_args_tail
6185 {
6186 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
6187 }
6188 | f_arg ',' f_rest_arg opt_args_tail
6189 {
6190 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
6191 }
6192 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
6193 {
6194 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
6195 }
6196 | f_arg opt_args_tail
6197 {
6198 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
6199 }
6200 | f_optarg ',' f_rest_arg opt_args_tail
6201 {
6202 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
6203 }
6204 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
6205 {
6206 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
6207 }
6208 | f_optarg opt_args_tail
6209 {
6210 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
6211 }
6212 | f_optarg ',' f_arg opt_args_tail
6213 {
6214 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
6215 }
6216 | f_rest_arg opt_args_tail
6217 {
6218 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
6219 }
6220 | f_rest_arg ',' f_arg opt_args_tail
6221 {
6222 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
6223 }
6224 | args_tail
6225 {
6226 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
6227 }
6228 | /* none */
6229 {
6230 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
6231 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
6232 }
6233 ;
6234
6235args_forward : tBDOT3
6236 {
6237 /*%%%*/
6238#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
6239 $$ = 0;
6240#else
6241 $$ = idFWD_KWREST;
6242#endif
6243 /*% %*/
6244 /*% ripper: args_forward! %*/
6245 }
6246 ;
6247
6248f_bad_arg : tCONSTANT
6249 {
6250 static const char mesg[] = "formal argument cannot be a constant";
6251 /*%%%*/
6252 yyerror1(&@1, mesg);
6253 $$ = 0;
6254 /*% %*/
6255 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6256 }
6257 | tIVAR
6258 {
6259 static const char mesg[] = "formal argument cannot be an instance variable";
6260 /*%%%*/
6261 yyerror1(&@1, mesg);
6262 $$ = 0;
6263 /*% %*/
6264 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6265 }
6266 | tGVAR
6267 {
6268 static const char mesg[] = "formal argument cannot be a global variable";
6269 /*%%%*/
6270 yyerror1(&@1, mesg);
6271 $$ = 0;
6272 /*% %*/
6273 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6274 }
6275 | tCVAR
6276 {
6277 static const char mesg[] = "formal argument cannot be a class variable";
6278 /*%%%*/
6279 yyerror1(&@1, mesg);
6280 $$ = 0;
6281 /*% %*/
6282 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6283 }
6284 ;
6285
6286f_norm_arg : f_bad_arg
6287 | tIDENTIFIER
6288 {
6289 formal_argument(p, $1);
6290 p->max_numparam = ORDINAL_PARAM;
6291 $$ = $1;
6292 }
6293 ;
6294
6295f_arg_asgn : f_norm_arg
6296 {
6297 ID id = get_id($1);
6298 arg_var(p, id);
6299 p->cur_arg = id;
6300 $$ = $1;
6301 }
6302 ;
6303
6304f_arg_item : f_arg_asgn
6305 {
6306 p->cur_arg = 0;
6307 /*%%%*/
6308 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6309 /*% %*/
6310 /*% ripper: get_value($1) %*/
6311 }
6312 | tLPAREN f_margs rparen
6313 {
6314 /*%%%*/
6315 ID tid = internal_id(p);
6316 YYLTYPE loc;
6317 loc.beg_pos = @2.beg_pos;
6318 loc.end_pos = @2.beg_pos;
6319 arg_var(p, tid);
6320 if (dyna_in_block(p)) {
6321 $2->nd_value = NEW_DVAR(tid, &loc);
6322 }
6323 else {
6324 $2->nd_value = NEW_LVAR(tid, &loc);
6325 }
6326 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6327 $$->nd_next = (NODE *)$2;
6328 /*% %*/
6329 /*% ripper: mlhs_paren!($2) %*/
6330 }
6331 ;
6332
6333f_arg : f_arg_item
6334 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
6335 | f_arg ',' f_arg_item
6336 {
6337 /*%%%*/
6338 $$ = $1;
6339 $$->nd_plen++;
6340 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6341 rb_discard_node(p, (NODE *)$3);
6342 /*% %*/
6343 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6344 }
6345 ;
6346
6347
6348f_label : tLABEL
6349 {
6350 arg_var(p, formal_argument(p, $1));
6351 p->cur_arg = get_id($1);
6352 p->max_numparam = ORDINAL_PARAM;
6353 p->ctxt.in_argdef = 0;
6354 $$ = $1;
6355 }
6356 ;
6357
6358f_kw : f_label arg_value
6359 {
6360 p->cur_arg = 0;
6361 p->ctxt.in_argdef = 1;
6362 /*%%%*/
6363 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6364 /*% %*/
6365 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
6366 }
6367 | f_label
6368 {
6369 p->cur_arg = 0;
6370 p->ctxt.in_argdef = 1;
6371 /*%%%*/
6372 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6373 /*% %*/
6374 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
6375 }
6376 ;
6377
6378f_block_kw : f_label primary_value
6379 {
6380 p->ctxt.in_argdef = 1;
6381 /*%%%*/
6382 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6383 /*% %*/
6384 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
6385 }
6386 | f_label
6387 {
6388 p->ctxt.in_argdef = 1;
6389 /*%%%*/
6390 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6391 /*% %*/
6392 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
6393 }
6394 ;
6395
6396f_block_kwarg : f_block_kw
6397 {
6398 /*%%%*/
6399 $$ = $1;
6400 /*% %*/
6401 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6402 }
6403 | f_block_kwarg ',' f_block_kw
6404 {
6405 /*%%%*/
6406 $$ = kwd_append($1, $3);
6407 /*% %*/
6408 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6409 }
6410 ;
6411
6412
6413f_kwarg : f_kw
6414 {
6415 /*%%%*/
6416 $$ = $1;
6417 /*% %*/
6418 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6419 }
6420 | f_kwarg ',' f_kw
6421 {
6422 /*%%%*/
6423 $$ = kwd_append($1, $3);
6424 /*% %*/
6425 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6426 }
6427 ;
6428
6429kwrest_mark : tPOW
6430 | tDSTAR
6431 ;
6432
6433f_no_kwarg : p_kwnorest
6434 {
6435 /*%%%*/
6436 /*% %*/
6437 /*% ripper: nokw_param!(Qnil) %*/
6438 }
6439 ;
6440
6441f_kwrest : kwrest_mark tIDENTIFIER
6442 {
6443 arg_var(p, shadowing_lvar(p, get_id($2)));
6444 /*%%%*/
6445 $$ = $2;
6446 /*% %*/
6447 /*% ripper: kwrest_param!($2) %*/
6448 }
6449 | kwrest_mark
6450 {
6451 arg_var(p, idFWD_KWREST);
6452 /*%%%*/
6453 $$ = idFWD_KWREST;
6454 /*% %*/
6455 /*% ripper: kwrest_param!(Qnil) %*/
6456 }
6457 ;
6458
6459f_opt : f_arg_asgn f_eq arg_value
6460 {
6461 p->cur_arg = 0;
6462 p->ctxt.in_argdef = 1;
6463 /*%%%*/
6464 $$ = NEW_OPT_ARG(assignable(p, $1, $3, &@$), &@$);
6465 /*% %*/
6466 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
6467 }
6468 ;
6469
6470f_block_opt : f_arg_asgn f_eq primary_value
6471 {
6472 p->cur_arg = 0;
6473 p->ctxt.in_argdef = 1;
6474 /*%%%*/
6475 $$ = NEW_OPT_ARG(assignable(p, $1, $3, &@$), &@$);
6476 /*% %*/
6477 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
6478 }
6479 ;
6480
6481f_block_optarg : f_block_opt
6482 {
6483 /*%%%*/
6484 $$ = $1;
6485 /*% %*/
6486 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6487 }
6488 | f_block_optarg ',' f_block_opt
6489 {
6490 /*%%%*/
6491 $$ = opt_arg_append($1, $3);
6492 /*% %*/
6493 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6494 }
6495 ;
6496
6497f_optarg : f_opt
6498 {
6499 /*%%%*/
6500 $$ = $1;
6501 /*% %*/
6502 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6503 }
6504 | f_optarg ',' f_opt
6505 {
6506 /*%%%*/
6507 $$ = opt_arg_append($1, $3);
6508 /*% %*/
6509 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6510 }
6511 ;
6512
6513restarg_mark : '*'
6514 | tSTAR
6515 ;
6516
6517f_rest_arg : restarg_mark tIDENTIFIER
6518 {
6519 arg_var(p, shadowing_lvar(p, get_id($2)));
6520 /*%%%*/
6521 $$ = $2;
6522 /*% %*/
6523 /*% ripper: rest_param!($2) %*/
6524 }
6525 | restarg_mark
6526 {
6527 arg_var(p, idFWD_REST);
6528 /*%%%*/
6529 $$ = idFWD_REST;
6530 /*% %*/
6531 /*% ripper: rest_param!(Qnil) %*/
6532 }
6533 ;
6534
6535blkarg_mark : '&'
6536 | tAMPER
6537 ;
6538
6539f_block_arg : blkarg_mark tIDENTIFIER
6540 {
6541 arg_var(p, shadowing_lvar(p, get_id($2)));
6542 /*%%%*/
6543 $$ = $2;
6544 /*% %*/
6545 /*% ripper: blockarg!($2) %*/
6546 }
6547 | blkarg_mark
6548 {
6549 arg_var(p, idFWD_BLOCK);
6550 /*%%%*/
6551 $$ = idFWD_BLOCK;
6552 /*% %*/
6553 /*% ripper: blockarg!(Qnil) %*/
6554 }
6555 ;
6556
6557opt_f_block_arg : ',' f_block_arg
6558 {
6559 $$ = $2;
6560 }
6561 | none
6562 {
6563 $$ = Qnull;
6564 }
6565 ;
6566
6567singleton : var_ref
6568 {
6569 value_expr($1);
6570 $$ = $1;
6571 }
6572 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
6573 {
6574 /*%%%*/
6575 NODE *expr = last_expr_node($3);
6576 switch (nd_type(expr)) {
6577 case NODE_STR:
6578 case NODE_DSTR:
6579 case NODE_XSTR:
6580 case NODE_DXSTR:
6581 case NODE_DREGX:
6582 case NODE_LIT:
6583 case NODE_DSYM:
6584 case NODE_LIST:
6585 case NODE_ZLIST:
6586 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6587 break;
6588 default:
6589 value_expr($3);
6590 break;
6591 }
6592 $$ = $3;
6593 /*% %*/
6594 /*% ripper: paren!($3) %*/
6595 }
6596 ;
6597
6598assoc_list : none
6599 | assocs trailer
6600 {
6601 /*%%%*/
6602 $$ = $1;
6603 /*% %*/
6604 /*% ripper: assoclist_from_args!($1) %*/
6605 }
6606 ;
6607
6608assocs : assoc
6609 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
6610 | assocs ',' assoc
6611 {
6612 /*%%%*/
6613 NODE *assocs = $1;
6614 NODE *tail = $3;
6615 if (!assocs) {
6616 assocs = tail;
6617 }
6618 else if (tail) {
6619 if (RNODE_LIST(assocs)->nd_head &&
6620 !RNODE_LIST(tail)->nd_head && nd_type_p(RNODE_LIST(tail)->nd_next, NODE_LIST) &&
6621 nd_type_p(RNODE_LIST(RNODE_LIST(tail)->nd_next)->nd_head, NODE_HASH)) {
6622 /* DSTAR */
6623 tail = RNODE_HASH(RNODE_LIST(RNODE_LIST(tail)->nd_next)->nd_head)->nd_head;
6624 }
6625 assocs = list_concat(assocs, tail);
6626 }
6627 $$ = assocs;
6628 /*% %*/
6629 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6630 }
6631 ;
6632
6633assoc : arg_value tASSOC arg_value
6634 {
6635 /*%%%*/
6636 if (nd_type_p($1, NODE_STR)) {
6637 nd_set_type($1, NODE_LIT);
6638 RB_OBJ_WRITE(p->ast, &RNODE_LIT($1)->nd_lit, rb_fstring(RNODE_LIT($1)->nd_lit));
6639 }
6640 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6641 /*% %*/
6642 /*% ripper: assoc_new!($1, $3) %*/
6643 }
6644 | tLABEL arg_value
6645 {
6646 /*%%%*/
6647 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
6648 /*% %*/
6649 /*% ripper: assoc_new!($1, $2) %*/
6650 }
6651 | tLABEL
6652 {
6653 /*%%%*/
6654 NODE *val = gettable(p, $1, &@$);
6655 if (!val) val = NEW_ERROR(&@$);
6656 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), val);
6657 /*% %*/
6658 /*% ripper: assoc_new!($1, Qnil) %*/
6659 }
6660 | tSTRING_BEG string_contents tLABEL_END arg_value
6661 {
6662 /*%%%*/
6663 YYLTYPE loc = code_loc_gen(&@1, &@3);
6664 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6665 /*% %*/
6666 /*% ripper: assoc_new!(dyna_symbol!($2), $4) %*/
6667 }
6668 | tDSTAR arg_value
6669 {
6670 /*%%%*/
6671 if (nd_type_p($2, NODE_HASH) &&
6672 !(RNODE_HASH($2)->nd_head && RNODE_LIST(RNODE_HASH($2)->nd_head)->as.nd_alen)) {
6673 static VALUE empty_hash;
6674 if (!empty_hash) {
6675 empty_hash = rb_obj_freeze(rb_hash_new());
6676 rb_gc_register_mark_object(empty_hash);
6677 }
6678 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
6679 }
6680 else
6681 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6682 /*% %*/
6683 /*% ripper: assoc_splat!($2) %*/
6684 }
6685 | tDSTAR
6686 {
6687 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6688 /*%%%*/
6689 $$ = list_append(p, NEW_LIST(0, &@$),
6690 NEW_LVAR(idFWD_KWREST, &@$));
6691 /*% %*/
6692 /*% ripper: assoc_splat!(Qnil) %*/
6693 }
6694 ;
6695
6696operation : tIDENTIFIER
6697 | tCONSTANT
6698 | tFID
6699 ;
6700
6701operation2 : operation
6702 | op
6703 ;
6704
6705operation3 : tIDENTIFIER
6706 | tFID
6707 | op
6708 ;
6709
6710dot_or_colon : '.'
6711 | tCOLON2
6712 ;
6713
6714call_op : '.'
6715 | tANDDOT
6716 ;
6717
6718call_op2 : call_op
6719 | tCOLON2
6720 ;
6721
6722opt_terms : /* none */
6723 | terms
6724 ;
6725
6726opt_nl : /* none */
6727 | '\n'
6728 ;
6729
6730rparen : opt_nl ')'
6731 ;
6732
6733rbracket : opt_nl ']'
6734 ;
6735
6736rbrace : opt_nl '}'
6737 ;
6738
6739trailer : opt_nl
6740 | ','
6741 ;
6742
6743term : ';' {yyerrok;token_flush(p);}
6744 | '\n'
6745 {
6746 @$.end_pos = @$.beg_pos;
6747 token_flush(p);
6748 }
6749 ;
6750
6751terms : term
6752 | terms ';' {yyerrok;}
6753 ;
6754
6755none : /* none */
6756 {
6757 $$ = Qnull;
6758 }
6759 ;
6760%%
6761# undef p
6762# undef yylex
6763# undef yylval
6764# define yylval (*p->lval)
6765
6766static int regx_options(struct parser_params*);
6767static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6768static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6769static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6770static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6771
6772#ifndef RIPPER
6773# define set_yylval_node(x) { \
6774 YYLTYPE _cur_loc; \
6775 rb_parser_set_location(p, &_cur_loc); \
6776 yylval.node = (x); \
6777}
6778# define set_yylval_str(x) \
6779do { \
6780 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6781 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6782} while(0)
6783# define set_yylval_literal(x) \
6784do { \
6785 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
6786 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6787} while(0)
6788# define set_yylval_num(x) (yylval.num = (x))
6789# define set_yylval_id(x) (yylval.id = (x))
6790# define set_yylval_name(x) (yylval.id = (x))
6791# define yylval_id() (yylval.id)
6792#else
6793static inline VALUE
6794ripper_yylval_id(struct parser_params *p, ID x)
6795{
6796 return ripper_new_yylval(p, x, ID2SYM(x), 0);
6797}
6798# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
6799# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
6800# define set_yylval_id(x) (void)(x)
6801# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
6802# define set_yylval_literal(x) add_mark_object(p, (x))
6803# define set_yylval_node(x) (yylval.val = ripper_new_yylval(p, 0, 0, STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)))
6804# define yylval_id() yylval.id
6805# define _cur_loc NULL_LOC /* dummy */
6806#endif
6807
6808#define set_yylval_noname() set_yylval_id(keyword_nil)
6809#define has_delayed_token(p) (!NIL_P(p->delayed.token))
6810
6811#ifndef RIPPER
6812#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6813#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6814
6815static bool
6816parser_has_token(struct parser_params *p)
6817{
6818 const char *const pcur = p->lex.pcur;
6819 const char *const ptok = p->lex.ptok;
6820 if (p->keep_tokens && (pcur < ptok)) {
6821 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6822 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6823 }
6824 return pcur > ptok;
6825}
6826
6827static VALUE
6828code_loc_to_ary(struct parser_params *p, const rb_code_location_t *loc)
6829{
6830 VALUE ary = rb_ary_new_from_args(4,
6831 INT2NUM(loc->beg_pos.lineno), INT2NUM(loc->beg_pos.column),
6832 INT2NUM(loc->end_pos.lineno), INT2NUM(loc->end_pos.column));
6833 rb_obj_freeze(ary);
6834
6835 return ary;
6836}
6837
6838static void
6839parser_append_tokens(struct parser_params *p, VALUE str, enum yytokentype t, int line)
6840{
6841 VALUE ary;
6842 int token_id;
6843
6844 ary = rb_ary_new2(4);
6845 token_id = p->token_id;
6846 rb_ary_push(ary, INT2FIX(token_id));
6847 rb_ary_push(ary, ID2SYM(parser_token2id(p, t)));
6848 rb_ary_push(ary, str);
6849 rb_ary_push(ary, code_loc_to_ary(p, p->yylloc));
6850 rb_obj_freeze(ary);
6851 rb_ary_push(p->tokens, ary);
6852 p->token_id++;
6853
6854 if (p->debug) {
6855 rb_parser_printf(p, "Append tokens (line: %d) %"PRIsVALUE"\n", line, ary);
6856 }
6857}
6858
6859static void
6860parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6861{
6862 debug_token_line(p, "parser_dispatch_scan_event", line);
6863
6864 if (!parser_has_token(p)) return;
6865
6866 RUBY_SET_YYLLOC(*p->yylloc);
6867
6868 if (p->keep_tokens) {
6869 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6870 parser_append_tokens(p, str, t, line);
6871 }
6872
6873 token_flush(p);
6874}
6875
6876#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6877static void
6878parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6879{
6880 debug_token_line(p, "parser_dispatch_delayed_token", line);
6881
6882 if (!has_delayed_token(p)) return;
6883
6884 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6885
6886 if (p->keep_tokens) {
6887 parser_append_tokens(p, p->delayed.token, t, line);
6888 }
6889
6890 p->delayed.token = Qnil;
6891}
6892#else
6893#define literal_flush(p, ptr) ((void)(ptr))
6894
6895#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &RNODE_RIPPER(yylval.node)->nd_rval : &yylval.val))
6896
6897static int
6898ripper_has_scan_event(struct parser_params *p)
6899{
6900 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6901 return p->lex.pcur > p->lex.ptok;
6902}
6903
6904static VALUE
6905ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6906{
6907 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6908 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6909 RUBY_SET_YYLLOC(*p->yylloc);
6910 token_flush(p);
6911 return rval;
6912}
6913
6914static void
6915ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6916{
6917 if (!ripper_has_scan_event(p)) return;
6918 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
6919}
6920#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6921
6922static void
6923ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6924{
6925 /* save and adjust the location to delayed token for callbacks */
6926 int saved_line = p->ruby_sourceline;
6927 const char *saved_tokp = p->lex.ptok;
6928
6929 if (!has_delayed_token(p)) return;
6930 p->ruby_sourceline = p->delayed.beg_line;
6931 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6932 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
6933 p->delayed.token = Qnil;
6934 p->ruby_sourceline = saved_line;
6935 p->lex.ptok = saved_tokp;
6936}
6937#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6938#endif /* RIPPER */
6939
6940static inline int
6941is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6942{
6943 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6944}
6945
6946static inline int
6947parser_is_identchar(struct parser_params *p)
6948{
6949 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
6950}
6951
6952static inline int
6953parser_isascii(struct parser_params *p)
6954{
6955 return ISASCII(*(p->lex.pcur-1));
6956}
6957
6958static void
6959token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
6960{
6961 int column = 1, nonspc = 0, i;
6962 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
6963 if (*ptr == '\t') {
6964 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
6965 }
6966 column++;
6967 if (*ptr != ' ' && *ptr != '\t') {
6968 nonspc = 1;
6969 }
6970 }
6971
6972 ptinfo->beg = loc->beg_pos;
6973 ptinfo->indent = column;
6974 ptinfo->nonspc = nonspc;
6975}
6976
6977static void
6978token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6979{
6980 token_info *ptinfo;
6981
6982 if (!p->token_info_enabled) return;
6983 ptinfo = ALLOC(token_info);
6984 ptinfo->token = token;
6985 ptinfo->next = p->token_info;
6986 token_info_setup(ptinfo, p->lex.pbeg, loc);
6987
6988 p->token_info = ptinfo;
6989}
6990
6991static void
6992token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6993{
6994 token_info *ptinfo_beg = p->token_info;
6995
6996 if (!ptinfo_beg) return;
6997 p->token_info = ptinfo_beg->next;
6998
6999 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7000 token_info_warn(p, token, ptinfo_beg, 1, loc);
7001 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7002}
7003
7004static void
7005token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7006{
7007 token_info *ptinfo_beg = p->token_info;
7008
7009 if (!ptinfo_beg) return;
7010 p->token_info = ptinfo_beg->next;
7011
7012 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7013 ptinfo_beg->beg.column != beg_pos.column ||
7014 strcmp(ptinfo_beg->token, token)) {
7015 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7016 beg_pos.lineno, beg_pos.column, token,
7017 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7018 ptinfo_beg->token);
7019 }
7020
7021 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7022}
7023
7024static void
7025token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7026{
7027 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7028 if (!p->token_info_enabled) return;
7029 if (!ptinfo_beg) return;
7030 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7031 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7032 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7033 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7034 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7035 rb_warn3L(ptinfo_end->beg.lineno,
7036 "mismatched indentations at '%s' with '%s' at %d",
7037 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7038}
7039
7040static int
7041parser_precise_mbclen(struct parser_params *p, const char *ptr)
7042{
7043 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7044 if (!MBCLEN_CHARFOUND_P(len)) {
7045 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7046 return -1;
7047 }
7048 return len;
7049}
7050
7051#ifndef RIPPER
7052static void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
7053
7054static inline void
7055parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7056{
7057 VALUE str;
7058 int lineno = p->ruby_sourceline;
7059 if (!yylloc) {
7060 return;
7061 }
7062 else if (yylloc->beg_pos.lineno == lineno) {
7063 str = p->lex.lastline;
7064 }
7065 else {
7066 return;
7067 }
7068 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7069}
7070
7071static int
7072parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7073{
7074#if 0
7075 YYLTYPE current;
7076
7077 if (!yylloc) {
7078 yylloc = RUBY_SET_YYLLOC(current);
7079 }
7080 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7081 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7082 yylloc = 0;
7083 }
7084#endif
7085 parser_compile_error(p, yylloc, "%s", msg);
7086 parser_show_error_line(p, yylloc);
7087 return 0;
7088}
7089
7090static int
7091parser_yyerror0(struct parser_params *p, const char *msg)
7092{
7093 YYLTYPE current;
7094 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7095}
7096
7097static void
7098ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
7099{
7100 VALUE mesg;
7101 const int max_line_margin = 30;
7102 const char *ptr, *ptr_end, *pt, *pb;
7103 const char *pre = "", *post = "", *pend;
7104 const char *code = "", *caret = "";
7105 const char *lim;
7106 const char *const pbeg = RSTRING_PTR(str);
7107 char *buf;
7108 long len;
7109 int i;
7110
7111 if (!yylloc) return;
7112 pend = RSTRING_END(str);
7113 if (pend > pbeg && pend[-1] == '\n') {
7114 if (--pend > pbeg && pend[-1] == '\r') --pend;
7115 }
7116
7117 pt = pend;
7118 if (lineno == yylloc->end_pos.lineno &&
7119 (pend - pbeg) > yylloc->end_pos.column) {
7120 pt = pbeg + yylloc->end_pos.column;
7121 }
7122
7123 ptr = ptr_end = pt;
7124 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7125 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7126
7127 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7128 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7129
7130 len = ptr_end - ptr;
7131 if (len > 4) {
7132 if (ptr > pbeg) {
7133 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
7134 if (ptr > pbeg) pre = "...";
7135 }
7136 if (ptr_end < pend) {
7137 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
7138 if (ptr_end < pend) post = "...";
7139 }
7140 }
7141 pb = pbeg;
7142 if (lineno == yylloc->beg_pos.lineno) {
7143 pb += yylloc->beg_pos.column;
7144 if (pb > pt) pb = pt;
7145 }
7146 if (pb < ptr) pb = ptr;
7147 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7148 return;
7149 }
7150 if (RTEST(errbuf)) {
7151 mesg = rb_attr_get(errbuf, idMesg);
7152 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
7153 rb_str_cat_cstr(mesg, "\n");
7154 }
7155 else {
7156 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
7157 }
7158 if (!errbuf && rb_stderr_tty_p()) {
7159#define CSI_BEGIN "\033["
7160#define CSI_SGR "m"
7161 rb_str_catf(mesg,
7162 CSI_BEGIN""CSI_SGR"%s" /* pre */
7163 CSI_BEGIN"1"CSI_SGR"%.*s"
7164 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7165 CSI_BEGIN";1"CSI_SGR"%.*s"
7166 CSI_BEGIN""CSI_SGR"%s" /* post */
7167 "\n",
7168 pre,
7169 (int)(pb - ptr), ptr,
7170 (int)(pt - pb), pb,
7171 (int)(ptr_end - pt), pt,
7172 post);
7173 }
7174 else {
7175 char *p2;
7176
7177 len = ptr_end - ptr;
7178 lim = pt < pend ? pt : pend;
7179 i = (int)(lim - ptr);
7180 buf = ALLOCA_N(char, i+2);
7181 code = ptr;
7182 caret = p2 = buf;
7183 if (ptr <= pb) {
7184 while (ptr < pb) {
7185 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7186 }
7187 *p2++ = '^';
7188 ptr++;
7189 }
7190 if (lim > ptr) {
7191 memset(p2, '~', (lim - ptr));
7192 p2 += (lim - ptr);
7193 }
7194 *p2 = '\0';
7195 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7196 pre, (int)len, code, post,
7197 pre, caret);
7198 }
7199 if (!errbuf) rb_write_error_str(mesg);
7200}
7201#else
7202static int
7203parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7204{
7205 const char *pcur = 0, *ptok = 0;
7206 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7207 p->ruby_sourceline == yylloc->end_pos.lineno) {
7208 pcur = p->lex.pcur;
7209 ptok = p->lex.ptok;
7210 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7211 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7212 }
7213 parser_yyerror0(p, msg);
7214 if (pcur) {
7215 p->lex.ptok = ptok;
7216 p->lex.pcur = pcur;
7217 }
7218 return 0;
7219}
7220
7221static int
7222parser_yyerror0(struct parser_params *p, const char *msg)
7223{
7224 dispatch1(parse_error, STR_NEW2(msg));
7225 ripper_error(p);
7226 return 0;
7227}
7228
7229static inline void
7230parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7231{
7232}
7233#endif /* !RIPPER */
7234
7235#ifndef RIPPER
7236static int
7237vtable_size(const struct vtable *tbl)
7238{
7239 if (!DVARS_TERMINAL_P(tbl)) {
7240 return tbl->pos;
7241 }
7242 else {
7243 return 0;
7244 }
7245}
7246#endif
7247
7248static struct vtable *
7249vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7250{
7251 struct vtable *tbl = ALLOC(struct vtable);
7252 tbl->pos = 0;
7253 tbl->capa = 8;
7254 tbl->tbl = ALLOC_N(ID, tbl->capa);
7255 tbl->prev = prev;
7256#ifndef RIPPER
7257 if (p->debug) {
7258 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7259 }
7260#endif
7261 return tbl;
7262}
7263#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7264
7265static void
7266vtable_free_gen(struct parser_params *p, int line, const char *name,
7267 struct vtable *tbl)
7268{
7269#ifndef RIPPER
7270 if (p->debug) {
7271 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7272 }
7273#endif
7274 if (!DVARS_TERMINAL_P(tbl)) {
7275 if (tbl->tbl) {
7276 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7277 }
7278 ruby_sized_xfree(tbl, sizeof(*tbl));
7279 }
7280}
7281#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7282
7283static void
7284vtable_add_gen(struct parser_params *p, int line, const char *name,
7285 struct vtable *tbl, ID id)
7286{
7287#ifndef RIPPER
7288 if (p->debug) {
7289 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7290 line, name, (void *)tbl, rb_id2name(id));
7291 }
7292#endif
7293 if (DVARS_TERMINAL_P(tbl)) {
7294 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7295 return;
7296 }
7297 if (tbl->pos == tbl->capa) {
7298 tbl->capa = tbl->capa * 2;
7299 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7300 }
7301 tbl->tbl[tbl->pos++] = id;
7302}
7303#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7304
7305#ifndef RIPPER
7306static void
7307vtable_pop_gen(struct parser_params *p, int line, const char *name,
7308 struct vtable *tbl, int n)
7309{
7310 if (p->debug) {
7311 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7312 line, name, (void *)tbl, n);
7313 }
7314 if (tbl->pos < n) {
7315 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7316 return;
7317 }
7318 tbl->pos -= n;
7319}
7320#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7321#endif
7322
7323static int
7324vtable_included(const struct vtable * tbl, ID id)
7325{
7326 int i;
7327
7328 if (!DVARS_TERMINAL_P(tbl)) {
7329 for (i = 0; i < tbl->pos; i++) {
7330 if (tbl->tbl[i] == id) {
7331 return i+1;
7332 }
7333 }
7334 }
7335 return 0;
7336}
7337
7338static void parser_prepare(struct parser_params *p);
7339
7340#ifndef RIPPER
7341static NODE *parser_append_options(struct parser_params *p, NODE *node);
7342
7343static int
7344e_option_supplied(struct parser_params *p)
7345{
7346 return strcmp(p->ruby_sourcefile, "-e") == 0;
7347}
7348
7349static VALUE
7350yycompile0(VALUE arg)
7351{
7352 int n;
7353 NODE *tree;
7354 struct parser_params *p = (struct parser_params *)arg;
7355 int cov = FALSE;
7356
7357 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
7358 if (p->debug_lines && p->ruby_sourceline > 0) {
7359 VALUE str = rb_default_rs;
7360 n = p->ruby_sourceline;
7361 do {
7362 rb_ary_push(p->debug_lines, str);
7363 } while (--n);
7364 }
7365
7366 if (!e_option_supplied(p)) {
7367 cov = TRUE;
7368 }
7369 }
7370
7371 if (p->debug_lines) {
7372 RB_OBJ_WRITE(p->ast, &p->ast->body.script_lines, p->debug_lines);
7373 }
7374
7375 parser_prepare(p);
7376#define RUBY_DTRACE_PARSE_HOOK(name) \
7377 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7378 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7379 }
7380 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7381 n = yyparse(p);
7382 RUBY_DTRACE_PARSE_HOOK(END);
7383 p->debug_lines = 0;
7384
7385 p->lex.strterm = 0;
7386 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7387 if (n || p->error_p) {
7388 VALUE mesg = p->error_buffer;
7389 if (!mesg) {
7390 mesg = syntax_error_new();
7391 }
7392 if (!p->error_tolerant) {
7393 rb_set_errinfo(mesg);
7394 return FALSE;
7395 }
7396 }
7397 tree = p->eval_tree;
7398 if (!tree) {
7399 tree = NEW_NIL(&NULL_LOC);
7400 }
7401 else {
7402 VALUE tokens = p->tokens;
7403 NODE *prelude;
7404 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7405 prelude = block_append(p, p->eval_tree_begin, body);
7406 RNODE_SCOPE(tree)->nd_body = prelude;
7407 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7408 p->ast->body.coverage_enabled = cov;
7409 if (p->keep_tokens) {
7410 rb_obj_freeze(tokens);
7411 rb_ast_set_tokens(p->ast, tokens);
7412 }
7413 }
7414 p->ast->body.root = tree;
7415 if (!p->ast->body.script_lines) p->ast->body.script_lines = INT2FIX(p->line_count);
7416 return TRUE;
7417}
7418
7419static rb_ast_t *
7420yycompile(struct parser_params *p, VALUE fname, int line)
7421{
7422 rb_ast_t *ast;
7423 if (NIL_P(fname)) {
7424 p->ruby_sourcefile_string = Qnil;
7425 p->ruby_sourcefile = "(none)";
7426 }
7427 else {
7428 p->ruby_sourcefile_string = rb_fstring(fname);
7429 p->ruby_sourcefile = StringValueCStr(fname);
7430 }
7431 p->ruby_sourceline = line - 1;
7432
7433 p->lvtbl = NULL;
7434
7435 p->ast = ast = rb_ast_new();
7436 compile_callback(yycompile0, (VALUE)p);
7437 p->ast = 0;
7438
7439 while (p->lvtbl) {
7440 local_pop(p);
7441 }
7442
7443 return ast;
7444}
7445#endif /* !RIPPER */
7446
7447static rb_encoding *
7448must_be_ascii_compatible(struct parser_params *p, VALUE s)
7449{
7450 rb_encoding *enc = rb_enc_get(s);
7451 if (!rb_enc_asciicompat(enc)) {
7452 rb_raise(rb_eArgError, "invalid source encoding");
7453 }
7454 return enc;
7455}
7456
7457static VALUE
7458lex_get_str(struct parser_params *p, VALUE s)
7459{
7460 char *beg, *end, *start;
7461 long len;
7462
7463 beg = RSTRING_PTR(s);
7464 len = RSTRING_LEN(s);
7465 start = beg;
7466 if (p->lex.gets_.ptr) {
7467 if (len == p->lex.gets_.ptr) return Qnil;
7468 beg += p->lex.gets_.ptr;
7469 len -= p->lex.gets_.ptr;
7470 }
7471 end = memchr(beg, '\n', len);
7472 if (end) len = ++end - beg;
7473 p->lex.gets_.ptr += len;
7474 return rb_str_subseq(s, beg - start, len);
7475}
7476
7477static VALUE
7478lex_getline(struct parser_params *p)
7479{
7480 VALUE line = (*p->lex.gets)(p, p->lex.input);
7481 if (NIL_P(line)) return line;
7482 must_be_ascii_compatible(p, line);
7483 if (RB_OBJ_FROZEN(line)) line = rb_str_dup(line); // needed for RubyVM::AST.of because script_lines in iseq is deep-frozen
7484 p->line_count++;
7485 return line;
7486}
7487
7488#ifndef RIPPER
7489static rb_ast_t*
7490parser_compile_string(rb_parser_t *p, VALUE fname, VALUE s, int line)
7491{
7492 p->lex.gets = lex_get_str;
7493 p->lex.gets_.ptr = 0;
7494 p->lex.input = rb_str_new_frozen(s);
7495 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7496
7497 return yycompile(p, fname, line);
7498}
7499
7500rb_ast_t*
7501rb_ruby_parser_compile_string_path(rb_parser_t *p, VALUE f, VALUE s, int line)
7502{
7503 must_be_ascii_compatible(p, s);
7504 return parser_compile_string(p, f, s, line);
7505}
7506
7507rb_ast_t*
7508rb_ruby_parser_compile_string(rb_parser_t *p, const char *f, VALUE s, int line)
7509{
7510 return rb_ruby_parser_compile_string_path(p, rb_filesystem_str_new_cstr(f), s, line);
7511}
7512
7513static VALUE
7514lex_io_gets(struct parser_params *p, VALUE io)
7515{
7516 return rb_io_gets_internal(io);
7517}
7518
7519rb_ast_t*
7520rb_ruby_parser_compile_file_path(rb_parser_t *p, VALUE fname, VALUE file, int start)
7521{
7522 p->lex.gets = lex_io_gets;
7523 p->lex.input = file;
7524 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7525
7526 return yycompile(p, fname, start);
7527}
7528
7529static VALUE
7530lex_generic_gets(struct parser_params *p, VALUE input)
7531{
7532 return (*p->lex.gets_.call)(input, p->line_count);
7533}
7534
7535rb_ast_t*
7536rb_ruby_parser_compile_generic(rb_parser_t *p, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
7537{
7538 p->lex.gets = lex_generic_gets;
7539 p->lex.gets_.call = lex_gets;
7540 p->lex.input = input;
7541 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7542
7543 return yycompile(p, fname, start);
7544}
7545#endif /* !RIPPER */
7546
7547#define STR_FUNC_ESCAPE 0x01
7548#define STR_FUNC_EXPAND 0x02
7549#define STR_FUNC_REGEXP 0x04
7550#define STR_FUNC_QWORDS 0x08
7551#define STR_FUNC_SYMBOL 0x10
7552#define STR_FUNC_INDENT 0x20
7553#define STR_FUNC_LABEL 0x40
7554#define STR_FUNC_LIST 0x4000
7555#define STR_FUNC_TERM 0x8000
7556
7557enum string_type {
7558 str_label = STR_FUNC_LABEL,
7559 str_squote = (0),
7560 str_dquote = (STR_FUNC_EXPAND),
7561 str_xquote = (STR_FUNC_EXPAND),
7562 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7563 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7564 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7565 str_ssym = (STR_FUNC_SYMBOL),
7566 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7567};
7568
7569static VALUE
7570parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7571{
7572 VALUE str;
7573
7574 str = rb_enc_str_new(ptr, len, enc);
7575 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7576 if (is_ascii_string(str)) {
7577 }
7578 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7579 rb_enc_associate(str, rb_ascii8bit_encoding());
7580 }
7581 }
7582
7583 return str;
7584}
7585
7586static int
7587strterm_is_heredoc(rb_strterm_t *strterm)
7588{
7589 return strterm->flags & STRTERM_HEREDOC;
7590}
7591
7592static rb_strterm_t *
7593new_strterm(struct parser_params *p, int func, int term, int paren)
7594{
7595 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7596 strterm->u.literal.func = func;
7597 strterm->u.literal.term = term;
7598 strterm->u.literal.paren = paren;
7599 return strterm;
7600}
7601
7602static rb_strterm_t *
7603new_heredoc(struct parser_params *p)
7604{
7605 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7606 strterm->flags |= STRTERM_HEREDOC;
7607 return strterm;
7608}
7609
7610#define peek(p,c) peek_n(p, (c), 0)
7611#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7612#define peekc(p) peekc_n(p, 0)
7613#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7614
7615static void
7616add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7617{
7618#ifndef RIPPER
7619 debug_token_line(p, "add_delayed_token", line);
7620#endif
7621
7622 if (tok < end) {
7623 if (has_delayed_token(p)) {
7624 bool next_line = end_with_newline_p(p, p->delayed.token);
7625 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7626 int end_col = (next_line ? 0 : p->delayed.end_col);
7627 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7628 dispatch_delayed_token(p, tSTRING_CONTENT);
7629 }
7630 }
7631 if (!has_delayed_token(p)) {
7632 p->delayed.token = rb_str_buf_new(end - tok);
7633 rb_enc_associate(p->delayed.token, p->enc);
7634 p->delayed.beg_line = p->ruby_sourceline;
7635 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7636 }
7637 rb_str_buf_cat(p->delayed.token, tok, end - tok);
7638 p->delayed.end_line = p->ruby_sourceline;
7639 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7640 p->lex.ptok = end;
7641 }
7642}
7643
7644static void
7645set_lastline(struct parser_params *p, VALUE v)
7646{
7647 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
7648 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
7649 p->lex.lastline = v;
7650}
7651
7652static int
7653nextline(struct parser_params *p, int set_encoding)
7654{
7655 VALUE v = p->lex.nextline;
7656 p->lex.nextline = 0;
7657 if (!v) {
7658 if (p->eofp)
7659 return -1;
7660
7661 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7662 goto end_of_input;
7663 }
7664
7665 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
7666 end_of_input:
7667 p->eofp = 1;
7668 lex_goto_eol(p);
7669 return -1;
7670 }
7671#ifndef RIPPER
7672 if (p->debug_lines) {
7673 if (set_encoding) rb_enc_associate(v, p->enc);
7674 rb_ary_push(p->debug_lines, v);
7675 }
7676#endif
7677 p->cr_seen = FALSE;
7678 }
7679 else if (NIL_P(v)) {
7680 /* after here-document without terminator */
7681 goto end_of_input;
7682 }
7683 add_delayed_token(p, p->lex.ptok, p->lex.pend, __LINE__);
7684 if (p->heredoc_end > 0) {
7685 p->ruby_sourceline = p->heredoc_end;
7686 p->heredoc_end = 0;
7687 }
7688 p->ruby_sourceline++;
7689 set_lastline(p, v);
7690 token_flush(p);
7691 return 0;
7692}
7693
7694static int
7695parser_cr(struct parser_params *p, int c)
7696{
7697 if (peek(p, '\n')) {
7698 p->lex.pcur++;
7699 c = '\n';
7700 }
7701 return c;
7702}
7703
7704static inline int
7705nextc0(struct parser_params *p, int set_encoding)
7706{
7707 int c;
7708
7709 if (UNLIKELY(lex_eol_p(p) || p->eofp || RTEST(p->lex.nextline))) {
7710 if (nextline(p, set_encoding)) return -1;
7711 }
7712 c = (unsigned char)*p->lex.pcur++;
7713 if (UNLIKELY(c == '\r')) {
7714 c = parser_cr(p, c);
7715 }
7716
7717 return c;
7718}
7719#define nextc(p) nextc0(p, TRUE)
7720
7721static void
7722pushback(struct parser_params *p, int c)
7723{
7724 if (c == -1) return;
7725 p->eofp = 0;
7726 p->lex.pcur--;
7727 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7728 p->lex.pcur--;
7729 }
7730}
7731
7732#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7733
7734#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7735#define tok(p) (p)->tokenbuf
7736#define toklen(p) (p)->tokidx
7737
7738static int
7739looking_at_eol_p(struct parser_params *p)
7740{
7741 const char *ptr = p->lex.pcur;
7742 while (!lex_eol_ptr_p(p, ptr)) {
7743 int c = (unsigned char)*ptr++;
7744 int eol = (c == '\n' || c == '#');
7745 if (eol || !ISSPACE(c)) {
7746 return eol;
7747 }
7748 }
7749 return TRUE;
7750}
7751
7752static char*
7753newtok(struct parser_params *p)
7754{
7755 p->tokidx = 0;
7756 if (!p->tokenbuf) {
7757 p->toksiz = 60;
7758 p->tokenbuf = ALLOC_N(char, 60);
7759 }
7760 if (p->toksiz > 4096) {
7761 p->toksiz = 60;
7762 REALLOC_N(p->tokenbuf, char, 60);
7763 }
7764 return p->tokenbuf;
7765}
7766
7767static char *
7768tokspace(struct parser_params *p, int n)
7769{
7770 p->tokidx += n;
7771
7772 if (p->tokidx >= p->toksiz) {
7773 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7774 REALLOC_N(p->tokenbuf, char, p->toksiz);
7775 }
7776 return &p->tokenbuf[p->tokidx-n];
7777}
7778
7779static void
7780tokadd(struct parser_params *p, int c)
7781{
7782 p->tokenbuf[p->tokidx++] = (char)c;
7783 if (p->tokidx >= p->toksiz) {
7784 p->toksiz *= 2;
7785 REALLOC_N(p->tokenbuf, char, p->toksiz);
7786 }
7787}
7788
7789static int
7790tok_hex(struct parser_params *p, size_t *numlen)
7791{
7792 int c;
7793
7794 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7795 if (!*numlen) {
7796 yyerror0("invalid hex escape");
7797 dispatch_scan_event(p, tSTRING_CONTENT);
7798 return 0;
7799 }
7800 p->lex.pcur += *numlen;
7801 return c;
7802}
7803
7804#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7805
7806static int
7807escaped_control_code(int c)
7808{
7809 int c2 = 0;
7810 switch (c) {
7811 case ' ':
7812 c2 = 's';
7813 break;
7814 case '\n':
7815 c2 = 'n';
7816 break;
7817 case '\t':
7818 c2 = 't';
7819 break;
7820 case '\v':
7821 c2 = 'v';
7822 break;
7823 case '\r':
7824 c2 = 'r';
7825 break;
7826 case '\f':
7827 c2 = 'f';
7828 break;
7829 }
7830 return c2;
7831}
7832
7833#define WARN_SPACE_CHAR(c, prefix) \
7834 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
7835
7836static int
7837tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7838 int regexp_literal, int wide)
7839{
7840 size_t numlen;
7841 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7842 p->lex.pcur += numlen;
7843 if (p->lex.strterm == NULL ||
7844 strterm_is_heredoc(p->lex.strterm) ||
7845 (p->lex.strterm->u.literal.func != str_regexp)) {
7846 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7847 literal_flush(p, p->lex.pcur);
7848 yyerror0("invalid Unicode escape");
7849 return wide && numlen > 0;
7850 }
7851 if (codepoint > 0x10ffff) {
7852 literal_flush(p, p->lex.pcur);
7853 yyerror0("invalid Unicode codepoint (too large)");
7854 return wide;
7855 }
7856 if ((codepoint & 0xfffff800) == 0xd800) {
7857 literal_flush(p, p->lex.pcur);
7858 yyerror0("invalid Unicode codepoint");
7859 return wide;
7860 }
7861 }
7862 if (regexp_literal) {
7863 tokcopy(p, (int)numlen);
7864 }
7865 else if (codepoint >= 0x80) {
7866 rb_encoding *utf8 = rb_utf8_encoding();
7867 if (*encp && utf8 != *encp) {
7868 YYLTYPE loc = RUBY_INIT_YYLLOC();
7869 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7870 parser_show_error_line(p, &loc);
7871 return wide;
7872 }
7873 *encp = utf8;
7874 tokaddmbc(p, codepoint, *encp);
7875 }
7876 else {
7877 tokadd(p, codepoint);
7878 }
7879 return TRUE;
7880}
7881
7882static int tokadd_mbchar(struct parser_params *p, int c);
7883
7884static int
7885tokskip_mbchar(struct parser_params *p)
7886{
7887 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7888 if (len > 0) {
7889 p->lex.pcur += len - 1;
7890 }
7891 return len;
7892}
7893
7894/* return value is for ?\u3042 */
7895static void
7896tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7897 int term, int symbol_literal, int regexp_literal)
7898{
7899 /*
7900 * If `term` is not -1, then we allow multiple codepoints in \u{}
7901 * upto `term` byte, otherwise we're parsing a character literal.
7902 * And then add the codepoints to the current token.
7903 */
7904 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7905
7906 const int open_brace = '{', close_brace = '}';
7907
7908 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7909
7910 if (peek(p, open_brace)) { /* handle \u{...} form */
7911 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7912 /*
7913 * Skip parsing validation code and copy bytes as-is until term or
7914 * closing brace, in order to correctly handle extended regexps where
7915 * invalid unicode escapes are allowed in comments. The regexp parser
7916 * does its own validation and will catch any issues.
7917 */
7918 tokadd(p, open_brace);
7919 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7920 int c = peekc(p);
7921 if (c == close_brace) {
7922 tokadd(p, c);
7923 ++p->lex.pcur;
7924 break;
7925 }
7926 else if (c == term) {
7927 break;
7928 }
7929 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7930 tokadd(p, c);
7931 c = *++p->lex.pcur;
7932 }
7933 tokadd_mbchar(p, c);
7934 }
7935 }
7936 else {
7937 const char *second = NULL;
7938 int c, last = nextc(p);
7939 if (lex_eol_p(p)) goto unterminated;
7940 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7941 while (c != close_brace) {
7942 if (c == term) goto unterminated;
7943 if (second == multiple_codepoints)
7944 second = p->lex.pcur;
7945 if (regexp_literal) tokadd(p, last);
7946 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
7947 break;
7948 }
7949 while (ISSPACE(c = peekc(p))) {
7950 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7951 last = c;
7952 }
7953 if (term == -1 && !second)
7954 second = multiple_codepoints;
7955 }
7956
7957 if (c != close_brace) {
7958 unterminated:
7959 token_flush(p);
7960 yyerror0("unterminated Unicode escape");
7961 return;
7962 }
7963 if (second && second != multiple_codepoints) {
7964 const char *pcur = p->lex.pcur;
7965 p->lex.pcur = second;
7966 dispatch_scan_event(p, tSTRING_CONTENT);
7967 token_flush(p);
7968 p->lex.pcur = pcur;
7969 yyerror0(multiple_codepoints);
7970 token_flush(p);
7971 }
7972
7973 if (regexp_literal) tokadd(p, close_brace);
7974 nextc(p);
7975 }
7976 }
7977 else { /* handle \uxxxx form */
7978 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
7979 token_flush(p);
7980 return;
7981 }
7982 }
7983}
7984
7985#define ESCAPE_CONTROL 1
7986#define ESCAPE_META 2
7987
7988static int
7989read_escape(struct parser_params *p, int flags)
7990{
7991 int c;
7992 size_t numlen;
7993
7994 switch (c = nextc(p)) {
7995 case '\\': /* Backslash */
7996 return c;
7997
7998 case 'n': /* newline */
7999 return '\n';
8000
8001 case 't': /* horizontal tab */
8002 return '\t';
8003
8004 case 'r': /* carriage-return */
8005 return '\r';
8006
8007 case 'f': /* form-feed */
8008 return '\f';
8009
8010 case 'v': /* vertical tab */
8011 return '\13';
8012
8013 case 'a': /* alarm(bell) */
8014 return '\007';
8015
8016 case 'e': /* escape */
8017 return 033;
8018
8019 case '0': case '1': case '2': case '3': /* octal constant */
8020 case '4': case '5': case '6': case '7':
8021 pushback(p, c);
8022 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8023 p->lex.pcur += numlen;
8024 return c;
8025
8026 case 'x': /* hex constant */
8027 c = tok_hex(p, &numlen);
8028 if (numlen == 0) return 0;
8029 return c;
8030
8031 case 'b': /* backspace */
8032 return '\010';
8033
8034 case 's': /* space */
8035 return ' ';
8036
8037 case 'M':
8038 if (flags & ESCAPE_META) goto eof;
8039 if ((c = nextc(p)) != '-') {
8040 goto eof;
8041 }
8042 if ((c = nextc(p)) == '\\') {
8043 switch (peekc(p)) {
8044 case 'u': case 'U':
8045 nextc(p);
8046 goto eof;
8047 }
8048 return read_escape(p, flags|ESCAPE_META) | 0x80;
8049 }
8050 else if (c == -1 || !ISASCII(c)) goto eof;
8051 else {
8052 int c2 = escaped_control_code(c);
8053 if (c2) {
8054 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8055 WARN_SPACE_CHAR(c2, "\\M-");
8056 }
8057 else {
8058 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8059 }
8060 }
8061 else if (ISCNTRL(c)) goto eof;
8062 return ((c & 0xff) | 0x80);
8063 }
8064
8065 case 'C':
8066 if ((c = nextc(p)) != '-') {
8067 goto eof;
8068 }
8069 case 'c':
8070 if (flags & ESCAPE_CONTROL) goto eof;
8071 if ((c = nextc(p))== '\\') {
8072 switch (peekc(p)) {
8073 case 'u': case 'U':
8074 nextc(p);
8075 goto eof;
8076 }
8077 c = read_escape(p, flags|ESCAPE_CONTROL);
8078 }
8079 else if (c == '?')
8080 return 0177;
8081 else if (c == -1) goto eof;
8082 else if (!ISASCII(c)) {
8083 tokskip_mbchar(p);
8084 goto eof;
8085 }
8086 else {
8087 int c2 = escaped_control_code(c);
8088 if (c2) {
8089 if (ISCNTRL(c)) {
8090 if (flags & ESCAPE_META) {
8091 WARN_SPACE_CHAR(c2, "\\M-");
8092 }
8093 else {
8094 WARN_SPACE_CHAR(c2, "");
8095 }
8096 }
8097 else {
8098 if (flags & ESCAPE_META) {
8099 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8100 }
8101 else {
8102 WARN_SPACE_CHAR(c2, "\\C-");
8103 }
8104 }
8105 }
8106 else if (ISCNTRL(c)) goto eof;
8107 }
8108 return c & 0x9f;
8109
8110 eof:
8111 case -1:
8112 yyerror0("Invalid escape character syntax");
8113 dispatch_scan_event(p, tSTRING_CONTENT);
8114 return '\0';
8115
8116 default:
8117 return c;
8118 }
8119}
8120
8121static void
8122tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8123{
8124 int len = rb_enc_codelen(c, enc);
8125 rb_enc_mbcput(c, tokspace(p, len), enc);
8126}
8127
8128static int
8129tokadd_escape(struct parser_params *p)
8130{
8131 int c;
8132 size_t numlen;
8133
8134 switch (c = nextc(p)) {
8135 case '\n':
8136 return 0; /* just ignore */
8137
8138 case '0': case '1': case '2': case '3': /* octal constant */
8139 case '4': case '5': case '6': case '7':
8140 {
8141 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8142 if (numlen == 0) goto eof;
8143 p->lex.pcur += numlen;
8144 tokcopy(p, (int)numlen + 1);
8145 }
8146 return 0;
8147
8148 case 'x': /* hex constant */
8149 {
8150 tok_hex(p, &numlen);
8151 if (numlen == 0) return -1;
8152 tokcopy(p, (int)numlen + 2);
8153 }
8154 return 0;
8155
8156 eof:
8157 case -1:
8158 yyerror0("Invalid escape character syntax");
8159 token_flush(p);
8160 return -1;
8161
8162 default:
8163 tokadd(p, '\\');
8164 tokadd(p, c);
8165 }
8166 return 0;
8167}
8168
8169static int
8170regx_options(struct parser_params *p)
8171{
8172 int kcode = 0;
8173 int kopt = 0;
8174 int options = 0;
8175 int c, opt, kc;
8176
8177 newtok(p);
8178 while (c = nextc(p), ISALPHA(c)) {
8179 if (c == 'o') {
8180 options |= RE_OPTION_ONCE;
8181 }
8182 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
8183 if (kc >= 0) {
8184 if (kc != rb_ascii8bit_encindex()) kcode = c;
8185 kopt = opt;
8186 }
8187 else {
8188 options |= opt;
8189 }
8190 }
8191 else {
8192 tokadd(p, c);
8193 }
8194 }
8195 options |= kopt;
8196 pushback(p, c);
8197 if (toklen(p)) {
8198 YYLTYPE loc = RUBY_INIT_YYLLOC();
8199 tokfix(p);
8200 compile_error(p, "unknown regexp option%s - %*s",
8201 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8202 parser_show_error_line(p, &loc);
8203 }
8204 return options | RE_OPTION_ENCODING(kcode);
8205}
8206
8207static int
8208tokadd_mbchar(struct parser_params *p, int c)
8209{
8210 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8211 if (len < 0) return -1;
8212 tokadd(p, c);
8213 p->lex.pcur += --len;
8214 if (len > 0) tokcopy(p, len);
8215 return c;
8216}
8217
8218static inline int
8219simple_re_meta(int c)
8220{
8221 switch (c) {
8222 case '$': case '*': case '+': case '.':
8223 case '?': case '^': case '|':
8224 case ')': case ']': case '}': case '>':
8225 return TRUE;
8226 default:
8227 return FALSE;
8228 }
8229}
8230
8231static int
8232parser_update_heredoc_indent(struct parser_params *p, int c)
8233{
8234 if (p->heredoc_line_indent == -1) {
8235 if (c == '\n') p->heredoc_line_indent = 0;
8236 }
8237 else {
8238 if (c == ' ') {
8239 p->heredoc_line_indent++;
8240 return TRUE;
8241 }
8242 else if (c == '\t') {
8243 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8244 p->heredoc_line_indent = w * TAB_WIDTH;
8245 return TRUE;
8246 }
8247 else if (c != '\n') {
8248 if (p->heredoc_indent > p->heredoc_line_indent) {
8249 p->heredoc_indent = p->heredoc_line_indent;
8250 }
8251 p->heredoc_line_indent = -1;
8252 }
8253 }
8254 return FALSE;
8255}
8256
8257static void
8258parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8259{
8260 YYLTYPE loc = RUBY_INIT_YYLLOC();
8261 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8262 compile_error(p, "%s mixed within %s source", n1, n2);
8263 parser_show_error_line(p, &loc);
8264}
8265
8266static void
8267parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8268{
8269 const char *pos = p->lex.pcur;
8270 p->lex.pcur = beg;
8271 parser_mixed_error(p, enc1, enc2);
8272 p->lex.pcur = pos;
8273}
8274
8275static inline char
8276nibble_char_upper(unsigned int c)
8277{
8278 c &= 0xf;
8279 return c + (c < 10 ? '0' : 'A' - 10);
8280}
8281
8282static int
8283tokadd_string(struct parser_params *p,
8284 int func, int term, int paren, long *nest,
8285 rb_encoding **encp, rb_encoding **enc)
8286{
8287 int c;
8288 bool erred = false;
8289#ifdef RIPPER
8290 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8291 int top_of_line = FALSE;
8292#endif
8293
8294#define mixed_error(enc1, enc2) \
8295 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8296#define mixed_escape(beg, enc1, enc2) \
8297 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8298
8299 while ((c = nextc(p)) != -1) {
8300 if (p->heredoc_indent > 0) {
8301 parser_update_heredoc_indent(p, c);
8302 }
8303#ifdef RIPPER
8304 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8305 pushback(p, c);
8306 break;
8307 }
8308#endif
8309
8310 if (paren && c == paren) {
8311 ++*nest;
8312 }
8313 else if (c == term) {
8314 if (!nest || !*nest) {
8315 pushback(p, c);
8316 break;
8317 }
8318 --*nest;
8319 }
8320 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8321 unsigned char c2 = *p->lex.pcur;
8322 if (c2 == '$' || c2 == '@' || c2 == '{') {
8323 pushback(p, c);
8324 break;
8325 }
8326 }
8327 else if (c == '\\') {
8328 c = nextc(p);
8329 switch (c) {
8330 case '\n':
8331 if (func & STR_FUNC_QWORDS) break;
8332 if (func & STR_FUNC_EXPAND) {
8333 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8334 continue;
8335 if (c == term) {
8336 c = '\\';
8337 goto terminate;
8338 }
8339 }
8340 tokadd(p, '\\');
8341 break;
8342
8343 case '\\':
8344 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8345 break;
8346
8347 case 'u':
8348 if ((func & STR_FUNC_EXPAND) == 0) {
8349 tokadd(p, '\\');
8350 break;
8351 }
8352 tokadd_utf8(p, enc, term,
8353 func & STR_FUNC_SYMBOL,
8354 func & STR_FUNC_REGEXP);
8355 continue;
8356
8357 default:
8358 if (c == -1) return -1;
8359 if (!ISASCII(c)) {
8360 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8361 goto non_ascii;
8362 }
8363 if (func & STR_FUNC_REGEXP) {
8364 switch (c) {
8365 case 'c':
8366 case 'C':
8367 case 'M': {
8368 pushback(p, c);
8369 c = read_escape(p, 0);
8370
8371 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8372 *t++ = '\\';
8373 *t++ = 'x';
8374 *t++ = nibble_char_upper(c >> 4);
8375 *t++ = nibble_char_upper(c);
8376 continue;
8377 }
8378 }
8379
8380 if (c == term && !simple_re_meta(c)) {
8381 tokadd(p, c);
8382 continue;
8383 }
8384 pushback(p, c);
8385 if ((c = tokadd_escape(p)) < 0)
8386 return -1;
8387 if (*enc && *enc != *encp) {
8388 mixed_escape(p->lex.ptok+2, *enc, *encp);
8389 }
8390 continue;
8391 }
8392 else if (func & STR_FUNC_EXPAND) {
8393 pushback(p, c);
8394 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8395 c = read_escape(p, 0);
8396 }
8397 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8398 /* ignore backslashed spaces in %w */
8399 }
8400 else if (c != term && !(paren && c == paren)) {
8401 tokadd(p, '\\');
8402 pushback(p, c);
8403 continue;
8404 }
8405 }
8406 }
8407 else if (!parser_isascii(p)) {
8408 non_ascii:
8409 if (!*enc) {
8410 *enc = *encp;
8411 }
8412 else if (*enc != *encp) {
8413 mixed_error(*enc, *encp);
8414 continue;
8415 }
8416 if (tokadd_mbchar(p, c) == -1) return -1;
8417 continue;
8418 }
8419 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8420 pushback(p, c);
8421 break;
8422 }
8423 if (c & 0x80) {
8424 if (!*enc) {
8425 *enc = *encp;
8426 }
8427 else if (*enc != *encp) {
8428 mixed_error(*enc, *encp);
8429 continue;
8430 }
8431 }
8432 tokadd(p, c);
8433#ifdef RIPPER
8434 top_of_line = (c == '\n');
8435#endif
8436 }
8437 terminate:
8438 if (*enc) *encp = *enc;
8439 return c;
8440}
8441
8442#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8443
8444#ifdef RIPPER
8445static void
8446flush_string_content(struct parser_params *p, rb_encoding *enc)
8447{
8448 VALUE content = yylval.val;
8449 if (!ripper_is_node_yylval(p, content))
8450 content = ripper_new_yylval(p, 0, 0, content);
8451 if (has_delayed_token(p)) {
8452 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8453 if (len > 0) {
8454 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8455 }
8456 dispatch_delayed_token(p, tSTRING_CONTENT);
8457 p->lex.ptok = p->lex.pcur;
8458 RNODE_RIPPER(content)->nd_rval = yylval.val;
8459 }
8460 dispatch_scan_event(p, tSTRING_CONTENT);
8461 if (yylval.val != content)
8462 RNODE_RIPPER(content)->nd_rval = yylval.val;
8463 yylval.val = content;
8464}
8465#else
8466static void
8467flush_string_content(struct parser_params *p, rb_encoding *enc)
8468{
8469 if (has_delayed_token(p)) {
8470 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8471 if (len > 0) {
8472 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8473 p->delayed.end_line = p->ruby_sourceline;
8474 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8475 }
8476 dispatch_delayed_token(p, tSTRING_CONTENT);
8477 p->lex.ptok = p->lex.pcur;
8478 }
8479 dispatch_scan_event(p, tSTRING_CONTENT);
8480}
8481#endif
8482
8483RUBY_FUNC_EXPORTED const uint_least32_t ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
8484/* this can be shared with ripper, since it's independent from struct
8485 * parser_params. */
8486#ifndef RIPPER
8487#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8488#define SPECIAL_PUNCT(idx) ( \
8489 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8490 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8491 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8492 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8493 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8494 BIT('0', idx))
8495const uint_least32_t ruby_global_name_punct_bits[] = {
8496 SPECIAL_PUNCT(0),
8497 SPECIAL_PUNCT(1),
8498 SPECIAL_PUNCT(2),
8499};
8500#undef BIT
8501#undef SPECIAL_PUNCT
8502#endif
8503
8504static enum yytokentype
8505parser_peek_variable_name(struct parser_params *p)
8506{
8507 int c;
8508 const char *ptr = p->lex.pcur;
8509
8510 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8511 c = *ptr++;
8512 switch (c) {
8513 case '$':
8514 if ((c = *ptr) == '-') {
8515 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8516 c = *ptr;
8517 }
8518 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8519 return tSTRING_DVAR;
8520 }
8521 break;
8522 case '@':
8523 if ((c = *ptr) == '@') {
8524 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8525 c = *ptr;
8526 }
8527 break;
8528 case '{':
8529 p->lex.pcur = ptr;
8530 p->command_start = TRUE;
8531 return tSTRING_DBEG;
8532 default:
8533 return 0;
8534 }
8535 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8536 return tSTRING_DVAR;
8537 return 0;
8538}
8539
8540#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8541#define IS_END() IS_lex_state(EXPR_END_ANY)
8542#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8543#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8544#define IS_LABEL_POSSIBLE() (\
8545 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8546 IS_ARG())
8547#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8548#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8549
8550static inline enum yytokentype
8551parser_string_term(struct parser_params *p, int func)
8552{
8553 xfree(p->lex.strterm);
8554 p->lex.strterm = 0;
8555 if (func & STR_FUNC_REGEXP) {
8556 set_yylval_num(regx_options(p));
8557 dispatch_scan_event(p, tREGEXP_END);
8558 SET_LEX_STATE(EXPR_END);
8559 return tREGEXP_END;
8560 }
8561 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8562 nextc(p);
8563 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8564 return tLABEL_END;
8565 }
8566 SET_LEX_STATE(EXPR_END);
8567 return tSTRING_END;
8568}
8569
8570static enum yytokentype
8571parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8572{
8573 int func = quote->func;
8574 int term = quote->term;
8575 int paren = quote->paren;
8576 int c, space = 0;
8577 rb_encoding *enc = p->enc;
8578 rb_encoding *base_enc = 0;
8579 VALUE lit;
8580
8581 if (func & STR_FUNC_TERM) {
8582 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8583 SET_LEX_STATE(EXPR_END);
8584 xfree(p->lex.strterm);
8585 p->lex.strterm = 0;
8586 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8587 }
8588 c = nextc(p);
8589 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8590 while (c != '\n' && ISSPACE(c = nextc(p)));
8591 space = 1;
8592 }
8593 if (func & STR_FUNC_LIST) {
8594 quote->func &= ~STR_FUNC_LIST;
8595 space = 1;
8596 }
8597 if (c == term && !quote->nest) {
8598 if (func & STR_FUNC_QWORDS) {
8599 quote->func |= STR_FUNC_TERM;
8600 pushback(p, c); /* dispatch the term at tSTRING_END */
8601 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8602 return ' ';
8603 }
8604 return parser_string_term(p, func);
8605 }
8606 if (space) {
8607 if (!ISSPACE(c)) pushback(p, c);
8608 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8609 return ' ';
8610 }
8611 newtok(p);
8612 if ((func & STR_FUNC_EXPAND) && c == '#') {
8613 enum yytokentype t = parser_peek_variable_name(p);
8614 if (t) return t;
8615 tokadd(p, '#');
8616 c = nextc(p);
8617 }
8618 pushback(p, c);
8619 if (tokadd_string(p, func, term, paren, &quote->nest,
8620 &enc, &base_enc) == -1) {
8621 if (p->eofp) {
8622#ifndef RIPPER
8623# define unterminated_literal(mesg) yyerror0(mesg)
8624#else
8625# define unterminated_literal(mesg) compile_error(p, mesg)
8626#endif
8627 literal_flush(p, p->lex.pcur);
8628 if (func & STR_FUNC_QWORDS) {
8629 /* no content to add, bailing out here */
8630 unterminated_literal("unterminated list meets end of file");
8631 xfree(p->lex.strterm);
8632 p->lex.strterm = 0;
8633 return tSTRING_END;
8634 }
8635 if (func & STR_FUNC_REGEXP) {
8636 unterminated_literal("unterminated regexp meets end of file");
8637 }
8638 else {
8639 unterminated_literal("unterminated string meets end of file");
8640 }
8641 quote->func |= STR_FUNC_TERM;
8642 }
8643 }
8644
8645 tokfix(p);
8646 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8647 set_yylval_str(lit);
8648 flush_string_content(p, enc);
8649
8650 return tSTRING_CONTENT;
8651}
8652
8653static enum yytokentype
8654heredoc_identifier(struct parser_params *p)
8655{
8656 /*
8657 * term_len is length of `<<"END"` except `END`,
8658 * in this case term_len is 4 (<, <, " and ").
8659 */
8660 long len, offset = p->lex.pcur - p->lex.pbeg;
8661 int c = nextc(p), term, func = 0, quote = 0;
8662 enum yytokentype token = tSTRING_BEG;
8663 int indent = 0;
8664
8665 if (c == '-') {
8666 c = nextc(p);
8667 func = STR_FUNC_INDENT;
8668 offset++;
8669 }
8670 else if (c == '~') {
8671 c = nextc(p);
8672 func = STR_FUNC_INDENT;
8673 offset++;
8674 indent = INT_MAX;
8675 }
8676 switch (c) {
8677 case '\'':
8678 func |= str_squote; goto quoted;
8679 case '"':
8680 func |= str_dquote; goto quoted;
8681 case '`':
8682 token = tXSTRING_BEG;
8683 func |= str_xquote; goto quoted;
8684
8685 quoted:
8686 quote++;
8687 offset++;
8688 term = c;
8689 len = 0;
8690 while ((c = nextc(p)) != term) {
8691 if (c == -1 || c == '\r' || c == '\n') {
8692 yyerror0("unterminated here document identifier");
8693 return -1;
8694 }
8695 }
8696 break;
8697
8698 default:
8699 if (!parser_is_identchar(p)) {
8700 pushback(p, c);
8701 if (func & STR_FUNC_INDENT) {
8702 pushback(p, indent > 0 ? '~' : '-');
8703 }
8704 return 0;
8705 }
8706 func |= str_dquote;
8707 do {
8708 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8709 if (n < 0) return 0;
8710 p->lex.pcur += --n;
8711 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8712 pushback(p, c);
8713 break;
8714 }
8715
8716 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8717 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8718 yyerror0("too long here document identifier");
8719 dispatch_scan_event(p, tHEREDOC_BEG);
8720 lex_goto_eol(p);
8721
8722 p->lex.strterm = new_heredoc(p);
8723 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8724 here->offset = offset;
8725 here->sourceline = p->ruby_sourceline;
8726 here->length = (unsigned)len;
8727 here->quote = quote;
8728 here->func = func;
8729 here->lastline = p->lex.lastline;
8730 rb_ast_add_mark_object(p->ast, p->lex.lastline);
8731
8732 token_flush(p);
8733 p->heredoc_indent = indent;
8734 p->heredoc_line_indent = 0;
8735 return token;
8736}
8737
8738static void
8739heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8740{
8741 VALUE line;
8742 rb_strterm_t *term = p->lex.strterm;
8743
8744 p->lex.strterm = 0;
8745 line = here->lastline;
8746 p->lex.lastline = line;
8747 p->lex.pbeg = RSTRING_PTR(line);
8748 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
8749 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8750 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8751 p->heredoc_end = p->ruby_sourceline;
8752 p->ruby_sourceline = (int)here->sourceline;
8753 if (p->eofp) p->lex.nextline = Qnil;
8754 p->eofp = 0;
8755 xfree(term);
8756 rb_ast_delete_mark_object(p->ast, line);
8757}
8758
8759static int
8760dedent_string(struct parser_params *p, VALUE string, int width)
8761{
8762 char *str;
8763 long len;
8764 int i, col = 0;
8765
8766 RSTRING_GETMEM(string, str, len);
8767 for (i = 0; i < len && col < width; i++) {
8768 if (str[i] == ' ') {
8769 col++;
8770 }
8771 else if (str[i] == '\t') {
8772 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8773 if (n > width) break;
8774 col = n;
8775 }
8776 else {
8777 break;
8778 }
8779 }
8780 if (!i) return 0;
8781 rb_str_modify(string);
8782 str = RSTRING_PTR(string);
8783 if (RSTRING_LEN(string) != len)
8784 rb_fatal("literal string changed: %+"PRIsVALUE, string);
8785 MEMMOVE(str, str + i, char, len - i);
8786 rb_str_set_len(string, len - i);
8787 return i;
8788}
8789
8790#ifndef RIPPER
8791static NODE *
8792heredoc_dedent(struct parser_params *p, NODE *root)
8793{
8794 NODE *node, *str_node, *prev_node;
8795 int indent = p->heredoc_indent;
8796 VALUE prev_lit = 0;
8797
8798 if (indent <= 0) return root;
8799 p->heredoc_indent = 0;
8800 if (!root) return root;
8801
8802 prev_node = node = str_node = root;
8803 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8804
8805 while (str_node) {
8806 VALUE lit = RNODE_LIT(str_node)->nd_lit;
8807 if (nd_fl_newline(str_node)) {
8808 dedent_string(p, lit, indent);
8809 }
8810 if (!prev_lit) {
8811 prev_lit = lit;
8812 }
8813 else if (!literal_concat0(p, prev_lit, lit)) {
8814 return 0;
8815 }
8816 else {
8817 NODE *end = RNODE_LIST(node)->as.nd_end;
8818 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8819 if (!node) {
8820 if (nd_type_p(prev_node, NODE_DSTR))
8821 nd_set_type(prev_node, NODE_STR);
8822 break;
8823 }
8824 RNODE_LIST(node)->as.nd_end = end;
8825 goto next_str;
8826 }
8827
8828 str_node = 0;
8829 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8830 next_str:
8831 if (!nd_type_p(node, NODE_LIST)) break;
8832 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8833 enum node_type type = nd_type(str_node);
8834 if (type == NODE_STR || type == NODE_DSTR) break;
8835 prev_lit = 0;
8836 str_node = 0;
8837 }
8838 }
8839 }
8840 return root;
8841}
8842#else /* RIPPER */
8843static VALUE
8844heredoc_dedent(struct parser_params *p, VALUE array)
8845{
8846 int indent = p->heredoc_indent;
8847
8848 if (indent <= 0) return array;
8849 p->heredoc_indent = 0;
8850 dispatch2(heredoc_dedent, array, INT2NUM(indent));
8851 return array;
8852}
8853#endif
8854
8855static int
8856whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8857{
8858 const char *beg = p->lex.pbeg;
8859 const char *ptr = p->lex.pend;
8860
8861 if (ptr - beg < len) return FALSE;
8862 if (ptr > beg && ptr[-1] == '\n') {
8863 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8864 if (ptr - beg < len) return FALSE;
8865 }
8866 if (strncmp(eos, ptr -= len, len)) return FALSE;
8867 if (indent) {
8868 while (beg < ptr && ISSPACE(*beg)) beg++;
8869 }
8870 return beg == ptr;
8871}
8872
8873static int
8874word_match_p(struct parser_params *p, const char *word, long len)
8875{
8876 if (strncmp(p->lex.pcur, word, len)) return 0;
8877 if (lex_eol_n_p(p, len)) return 1;
8878 int c = (unsigned char)p->lex.pcur[len];
8879 if (ISSPACE(c)) return 1;
8880 switch (c) {
8881 case '\0': case '\004': case '\032': return 1;
8882 }
8883 return 0;
8884}
8885
8886#define NUM_SUFFIX_R (1<<0)
8887#define NUM_SUFFIX_I (1<<1)
8888#define NUM_SUFFIX_ALL 3
8889
8890static int
8891number_literal_suffix(struct parser_params *p, int mask)
8892{
8893 int c, result = 0;
8894 const char *lastp = p->lex.pcur;
8895
8896 while ((c = nextc(p)) != -1) {
8897 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8898 result |= (mask & NUM_SUFFIX_I);
8899 mask &= ~NUM_SUFFIX_I;
8900 /* r after i, rational of complex is disallowed */
8901 mask &= ~NUM_SUFFIX_R;
8902 continue;
8903 }
8904 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8905 result |= (mask & NUM_SUFFIX_R);
8906 mask &= ~NUM_SUFFIX_R;
8907 continue;
8908 }
8909 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8910 p->lex.pcur = lastp;
8911 literal_flush(p, p->lex.pcur);
8912 return 0;
8913 }
8914 pushback(p, c);
8915 break;
8916 }
8917 return result;
8918}
8919
8920static enum yytokentype
8921set_number_literal(struct parser_params *p, VALUE v,
8922 enum yytokentype type, int suffix)
8923{
8924 if (suffix & NUM_SUFFIX_I) {
8925 v = rb_complex_raw(INT2FIX(0), v);
8926 type = tIMAGINARY;
8927 }
8928 set_yylval_literal(v);
8929 SET_LEX_STATE(EXPR_END);
8930 return type;
8931}
8932
8933static enum yytokentype
8934set_integer_literal(struct parser_params *p, VALUE v, int suffix)
8935{
8936 enum yytokentype type = tINTEGER;
8937 if (suffix & NUM_SUFFIX_R) {
8938 v = rb_rational_raw1(v);
8939 type = tRATIONAL;
8940 }
8941 return set_number_literal(p, v, type, suffix);
8942}
8943
8944#ifdef RIPPER
8945static void
8946dispatch_heredoc_end(struct parser_params *p)
8947{
8948 VALUE str;
8949 if (has_delayed_token(p))
8950 dispatch_delayed_token(p, tSTRING_CONTENT);
8951 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8952 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8953 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8954 lex_goto_eol(p);
8955 token_flush(p);
8956}
8957
8958#else
8959#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8960static void
8961parser_dispatch_heredoc_end(struct parser_params *p, int line)
8962{
8963 if (has_delayed_token(p))
8964 dispatch_delayed_token(p, tSTRING_CONTENT);
8965
8966 if (p->keep_tokens) {
8967 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8968 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8969 parser_append_tokens(p, str, tHEREDOC_END, line);
8970 }
8971
8972 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8973 lex_goto_eol(p);
8974 token_flush(p);
8975}
8976#endif
8977
8978static enum yytokentype
8979here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
8980{
8981 int c, func, indent = 0;
8982 const char *eos, *ptr, *ptr_end;
8983 long len;
8984 VALUE str = 0;
8985 rb_encoding *enc = p->enc;
8986 rb_encoding *base_enc = 0;
8987 int bol;
8988
8989 eos = RSTRING_PTR(here->lastline) + here->offset;
8990 len = here->length;
8991 indent = (func = here->func) & STR_FUNC_INDENT;
8992
8993 if ((c = nextc(p)) == -1) {
8994 error:
8995#ifdef RIPPER
8996 if (!has_delayed_token(p)) {
8997 dispatch_scan_event(p, tSTRING_CONTENT);
8998 }
8999 else {
9000 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9001 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
9002 int cr = ENC_CODERANGE_UNKNOWN;
9003 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9004 if (cr != ENC_CODERANGE_7BIT &&
9005 rb_is_usascii_enc(p->enc) &&
9006 enc != rb_utf8_encoding()) {
9007 enc = rb_ascii8bit_encoding();
9008 }
9009 }
9010 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
9011 }
9012 dispatch_delayed_token(p, tSTRING_CONTENT);
9013 }
9014 lex_goto_eol(p);
9015#endif
9016 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9017 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9018 (int)len, eos);
9019 token_flush(p);
9020 SET_LEX_STATE(EXPR_END);
9021 return tSTRING_END;
9022 }
9023 bol = was_bol(p);
9024 if (!bol) {
9025 /* not beginning of line, cannot be the terminator */
9026 }
9027 else if (p->heredoc_line_indent == -1) {
9028 /* `heredoc_line_indent == -1` means
9029 * - "after an interpolation in the same line", or
9030 * - "in a continuing line"
9031 */
9032 p->heredoc_line_indent = 0;
9033 }
9034 else if (whole_match_p(p, eos, len, indent)) {
9035 dispatch_heredoc_end(p);
9036 restore:
9037 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9038 token_flush(p);
9039 SET_LEX_STATE(EXPR_END);
9040 return tSTRING_END;
9041 }
9042
9043 if (!(func & STR_FUNC_EXPAND)) {
9044 do {
9045 ptr = RSTRING_PTR(p->lex.lastline);
9046 ptr_end = p->lex.pend;
9047 if (ptr_end > ptr) {
9048 switch (ptr_end[-1]) {
9049 case '\n':
9050 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9051 ptr_end++;
9052 break;
9053 }
9054 case '\r':
9055 --ptr_end;
9056 }
9057 }
9058
9059 if (p->heredoc_indent > 0) {
9060 long i = 0;
9061 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9062 i++;
9063 p->heredoc_line_indent = 0;
9064 }
9065
9066 if (str)
9067 rb_str_cat(str, ptr, ptr_end - ptr);
9068 else
9069 str = STR_NEW(ptr, ptr_end - ptr);
9070 if (!lex_eol_ptr_p(p, ptr_end)) rb_str_cat(str, "\n", 1);
9071 lex_goto_eol(p);
9072 if (p->heredoc_indent > 0) {
9073 goto flush_str;
9074 }
9075 if (nextc(p) == -1) {
9076 if (str) {
9077 str = 0;
9078 }
9079 goto error;
9080 }
9081 } while (!whole_match_p(p, eos, len, indent));
9082 }
9083 else {
9084 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9085 newtok(p);
9086 if (c == '#') {
9087 enum yytokentype t = parser_peek_variable_name(p);
9088 if (p->heredoc_line_indent != -1) {
9089 if (p->heredoc_indent > p->heredoc_line_indent) {
9090 p->heredoc_indent = p->heredoc_line_indent;
9091 }
9092 p->heredoc_line_indent = -1;
9093 }
9094 if (t) return t;
9095 tokadd(p, '#');
9096 c = nextc(p);
9097 }
9098 do {
9099 pushback(p, c);
9100 enc = p->enc;
9101 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9102 if (p->eofp) goto error;
9103 goto restore;
9104 }
9105 if (c != '\n') {
9106 if (c == '\\') p->heredoc_line_indent = -1;
9107 flush:
9108 str = STR_NEW3(tok(p), toklen(p), enc, func);
9109 flush_str:
9110 set_yylval_str(str);
9111#ifndef RIPPER
9112 if (bol) nd_set_fl_newline(yylval.node);
9113#endif
9114 flush_string_content(p, enc);
9115 return tSTRING_CONTENT;
9116 }
9117 tokadd(p, nextc(p));
9118 if (p->heredoc_indent > 0) {
9119 lex_goto_eol(p);
9120 goto flush;
9121 }
9122 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9123 if ((c = nextc(p)) == -1) goto error;
9124 } while (!whole_match_p(p, eos, len, indent));
9125 str = STR_NEW3(tok(p), toklen(p), enc, func);
9126 }
9127 dispatch_heredoc_end(p);
9128#ifdef RIPPER
9129 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
9130 yylval.val, str);
9131#endif
9132 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9133 token_flush(p);
9134 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9135 set_yylval_str(str);
9136#ifndef RIPPER
9137 if (bol) nd_set_fl_newline(yylval.node);
9138#endif
9139 return tSTRING_CONTENT;
9140}
9141
9142#include "lex.c"
9143
9144static int
9145arg_ambiguous(struct parser_params *p, char c)
9146{
9147#ifndef RIPPER
9148 if (c == '/') {
9149 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
9150 }
9151 else {
9152 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
9153 }
9154#else
9155 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9156#endif
9157 return TRUE;
9158}
9159
9160static ID
9161#ifndef RIPPER
9162formal_argument(struct parser_params *p, ID lhs)
9163#else
9164formal_argument(struct parser_params *p, VALUE lhs)
9165#endif
9166{
9167 ID id = get_id(lhs);
9168
9169 switch (id_type(id)) {
9170 case ID_LOCAL:
9171 break;
9172#ifndef RIPPER
9173# define ERR(mesg) yyerror0(mesg)
9174#else
9175# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
9176#endif
9177 case ID_CONST:
9178 ERR("formal argument cannot be a constant");
9179 return 0;
9180 case ID_INSTANCE:
9181 ERR("formal argument cannot be an instance variable");
9182 return 0;
9183 case ID_GLOBAL:
9184 ERR("formal argument cannot be a global variable");
9185 return 0;
9186 case ID_CLASS:
9187 ERR("formal argument cannot be a class variable");
9188 return 0;
9189 default:
9190 ERR("formal argument must be local variable");
9191 return 0;
9192#undef ERR
9193 }
9194 shadowing_lvar(p, id);
9195 return lhs;
9196}
9197
9198static int
9199lvar_defined(struct parser_params *p, ID id)
9200{
9201 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9202}
9203
9204/* emacsen -*- hack */
9205static long
9206parser_encode_length(struct parser_params *p, const char *name, long len)
9207{
9208 long nlen;
9209
9210 if (len > 5 && name[nlen = len - 5] == '-') {
9211 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9212 return nlen;
9213 }
9214 if (len > 4 && name[nlen = len - 4] == '-') {
9215 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9216 return nlen;
9217 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9218 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9219 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9220 return nlen;
9221 }
9222 return len;
9223}
9224
9225static void
9226parser_set_encode(struct parser_params *p, const char *name)
9227{
9228 int idx = rb_enc_find_index(name);
9229 rb_encoding *enc;
9230 VALUE excargs[3];
9231
9232 if (idx < 0) {
9233 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9234 error:
9235 excargs[0] = rb_eArgError;
9236 excargs[2] = rb_make_backtrace();
9237 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9238 rb_exc_raise(rb_make_exception(3, excargs));
9239 }
9240 enc = rb_enc_from_index(idx);
9241 if (!rb_enc_asciicompat(enc)) {
9242 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9243 goto error;
9244 }
9245 p->enc = enc;
9246#ifndef RIPPER
9247 if (p->debug_lines) {
9248 VALUE lines = p->debug_lines;
9249 long i, n = RARRAY_LEN(lines);
9250 for (i = 0; i < n; ++i) {
9251 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
9252 }
9253 }
9254#endif
9255}
9256
9257static int
9258comment_at_top(struct parser_params *p)
9259{
9260 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
9261 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
9262 while (ptr < ptr_end) {
9263 if (!ISSPACE(*ptr)) return 0;
9264 ptr++;
9265 }
9266 return 1;
9267}
9268
9269typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9270typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9271
9272static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9273
9274static void
9275magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9276{
9277 if (!comment_at_top(p)) {
9278 return;
9279 }
9280 parser_set_encode(p, val);
9281}
9282
9283static int
9284parser_get_bool(struct parser_params *p, const char *name, const char *val)
9285{
9286 switch (*val) {
9287 case 't': case 'T':
9288 if (STRCASECMP(val, "true") == 0) {
9289 return TRUE;
9290 }
9291 break;
9292 case 'f': case 'F':
9293 if (STRCASECMP(val, "false") == 0) {
9294 return FALSE;
9295 }
9296 break;
9297 }
9298 return parser_invalid_pragma_value(p, name, val);
9299}
9300
9301static int
9302parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9303{
9304 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9305 return -1;
9306}
9307
9308static void
9309parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9310{
9311 int b = parser_get_bool(p, name, val);
9312 if (b >= 0) p->token_info_enabled = b;
9313}
9314
9315static void
9316parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9317{
9318 int b;
9319
9320 if (p->token_seen) {
9321 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
9322 return;
9323 }
9324
9325 b = parser_get_bool(p, name, val);
9326 if (b < 0) return;
9327
9328 p->frozen_string_literal = b;
9329}
9330
9331static void
9332parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9333{
9334 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9335 if (*s == ' ' || *s == '\t') continue;
9336 if (*s == '#') break;
9337 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
9338 return;
9339 }
9340
9341 switch (*val) {
9342 case 'n': case 'N':
9343 if (STRCASECMP(val, "none") == 0) {
9344 p->ctxt.shareable_constant_value = shareable_none;
9345 return;
9346 }
9347 break;
9348 case 'l': case 'L':
9349 if (STRCASECMP(val, "literal") == 0) {
9350 p->ctxt.shareable_constant_value = shareable_literal;
9351 return;
9352 }
9353 break;
9354 case 'e': case 'E':
9355 if (STRCASECMP(val, "experimental_copy") == 0) {
9356 p->ctxt.shareable_constant_value = shareable_copy;
9357 return;
9358 }
9359 if (STRCASECMP(val, "experimental_everything") == 0) {
9360 p->ctxt.shareable_constant_value = shareable_everything;
9361 return;
9362 }
9363 break;
9364 }
9365 parser_invalid_pragma_value(p, name, val);
9366}
9367
9368# if WARN_PAST_SCOPE
9369static void
9370parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9371{
9372 int b = parser_get_bool(p, name, val);
9373 if (b >= 0) p->past_scope_enabled = b;
9374}
9375# endif
9376
9377struct magic_comment {
9378 const char *name;
9379 rb_magic_comment_setter_t func;
9380 rb_magic_comment_length_t length;
9381};
9382
9383static const struct magic_comment magic_comments[] = {
9384 {"coding", magic_comment_encoding, parser_encode_length},
9385 {"encoding", magic_comment_encoding, parser_encode_length},
9386 {"frozen_string_literal", parser_set_frozen_string_literal},
9387 {"shareable_constant_value", parser_set_shareable_constant_value},
9388 {"warn_indent", parser_set_token_info},
9389# if WARN_PAST_SCOPE
9390 {"warn_past_scope", parser_set_past_scope},
9391# endif
9392};
9393
9394static const char *
9395magic_comment_marker(const char *str, long len)
9396{
9397 long i = 2;
9398
9399 while (i < len) {
9400 switch (str[i]) {
9401 case '-':
9402 if (str[i-1] == '*' && str[i-2] == '-') {
9403 return str + i + 1;
9404 }
9405 i += 2;
9406 break;
9407 case '*':
9408 if (i + 1 >= len) return 0;
9409 if (str[i+1] != '-') {
9410 i += 4;
9411 }
9412 else if (str[i-1] != '-') {
9413 i += 2;
9414 }
9415 else {
9416 return str + i + 2;
9417 }
9418 break;
9419 default:
9420 i += 3;
9421 break;
9422 }
9423 }
9424 return 0;
9425}
9426
9427static int
9428parser_magic_comment(struct parser_params *p, const char *str, long len)
9429{
9430 int indicator = 0;
9431 VALUE name = 0, val = 0;
9432 const char *beg, *end, *vbeg, *vend;
9433#define str_copy(_s, _p, _n) ((_s) \
9434 ? (void)(rb_str_resize((_s), (_n)), \
9435 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9436 : (void)((_s) = STR_NEW((_p), (_n))))
9437
9438 if (len <= 7) return FALSE;
9439 if (!!(beg = magic_comment_marker(str, len))) {
9440 if (!(end = magic_comment_marker(beg, str + len - beg)))
9441 return FALSE;
9442 indicator = TRUE;
9443 str = beg;
9444 len = end - beg - 3;
9445 }
9446
9447 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9448 while (len > 0) {
9449 const struct magic_comment *mc = magic_comments;
9450 char *s;
9451 int i;
9452 long n = 0;
9453
9454 for (; len > 0 && *str; str++, --len) {
9455 switch (*str) {
9456 case '\'': case '"': case ':': case ';':
9457 continue;
9458 }
9459 if (!ISSPACE(*str)) break;
9460 }
9461 for (beg = str; len > 0; str++, --len) {
9462 switch (*str) {
9463 case '\'': case '"': case ':': case ';':
9464 break;
9465 default:
9466 if (ISSPACE(*str)) break;
9467 continue;
9468 }
9469 break;
9470 }
9471 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9472 if (!len) break;
9473 if (*str != ':') {
9474 if (!indicator) return FALSE;
9475 continue;
9476 }
9477
9478 do str++; while (--len > 0 && ISSPACE(*str));
9479 if (!len) break;
9480 if (*str == '"') {
9481 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9482 if (*str == '\\') {
9483 --len;
9484 ++str;
9485 }
9486 }
9487 vend = str;
9488 if (len) {
9489 --len;
9490 ++str;
9491 }
9492 }
9493 else {
9494 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9495 vend = str;
9496 }
9497 if (indicator) {
9498 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9499 }
9500 else {
9501 while (len > 0 && (ISSPACE(*str))) --len, str++;
9502 if (len) return FALSE;
9503 }
9504
9505 n = end - beg;
9506 str_copy(name, beg, n);
9507 s = RSTRING_PTR(name);
9508 for (i = 0; i < n; ++i) {
9509 if (s[i] == '-') s[i] = '_';
9510 }
9511 do {
9512 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9513 n = vend - vbeg;
9514 if (mc->length) {
9515 n = (*mc->length)(p, vbeg, n);
9516 }
9517 str_copy(val, vbeg, n);
9518 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9519 break;
9520 }
9521 } while (++mc < magic_comments + numberof(magic_comments));
9522#ifdef RIPPER
9523 str_copy(val, vbeg, vend - vbeg);
9524 dispatch2(magic_comment, name, val);
9525#endif
9526 }
9527
9528 return TRUE;
9529}
9530
9531static void
9532set_file_encoding(struct parser_params *p, const char *str, const char *send)
9533{
9534 int sep = 0;
9535 const char *beg = str;
9536 VALUE s;
9537
9538 for (;;) {
9539 if (send - str <= 6) return;
9540 switch (str[6]) {
9541 case 'C': case 'c': str += 6; continue;
9542 case 'O': case 'o': str += 5; continue;
9543 case 'D': case 'd': str += 4; continue;
9544 case 'I': case 'i': str += 3; continue;
9545 case 'N': case 'n': str += 2; continue;
9546 case 'G': case 'g': str += 1; continue;
9547 case '=': case ':':
9548 sep = 1;
9549 str += 6;
9550 break;
9551 default:
9552 str += 6;
9553 if (ISSPACE(*str)) break;
9554 continue;
9555 }
9556 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9557 sep = 0;
9558 }
9559 for (;;) {
9560 do {
9561 if (++str >= send) return;
9562 } while (ISSPACE(*str));
9563 if (sep) break;
9564 if (*str != '=' && *str != ':') return;
9565 sep = 1;
9566 str++;
9567 }
9568 beg = str;
9569 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9570 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9571 parser_set_encode(p, RSTRING_PTR(s));
9572 rb_str_resize(s, 0);
9573}
9574
9575static void
9576parser_prepare(struct parser_params *p)
9577{
9578 int c = nextc0(p, FALSE);
9579 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9580 switch (c) {
9581 case '#':
9582 if (peek(p, '!')) p->has_shebang = 1;
9583 break;
9584 case 0xef: /* UTF-8 BOM marker */
9585 if (!lex_eol_n_p(p, 2) &&
9586 (unsigned char)p->lex.pcur[0] == 0xbb &&
9587 (unsigned char)p->lex.pcur[1] == 0xbf) {
9588 p->enc = rb_utf8_encoding();
9589 p->lex.pcur += 2;
9590#ifndef RIPPER
9591 if (p->debug_lines) {
9592 rb_enc_associate(p->lex.lastline, p->enc);
9593 }
9594#endif
9595 p->lex.pbeg = p->lex.pcur;
9596 token_flush(p);
9597 return;
9598 }
9599 break;
9600 case EOF:
9601 return;
9602 }
9603 pushback(p, c);
9604 p->enc = rb_enc_get(p->lex.lastline);
9605}
9606
9607#ifndef RIPPER
9608#define ambiguous_operator(tok, op, syn) ( \
9609 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
9610 rb_warning0("even though it seems like "syn""))
9611#else
9612#define ambiguous_operator(tok, op, syn) \
9613 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9614#endif
9615#define warn_balanced(tok, op, syn) ((void) \
9616 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9617 space_seen && !ISSPACE(c) && \
9618 (ambiguous_operator(tok, op, syn), 0)), \
9619 (enum yytokentype)(tok))
9620
9621static VALUE
9622parse_rational(struct parser_params *p, char *str, int len, int seen_point)
9623{
9624 VALUE v;
9625 char *point = &str[seen_point];
9626 size_t fraclen = len-seen_point-1;
9627 memmove(point, point+1, fraclen+1);
9628 v = rb_cstr_to_inum(str, 10, FALSE);
9629 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
9630}
9631
9632static enum yytokentype
9633no_digits(struct parser_params *p)
9634{
9635 yyerror0("numeric literal without digits");
9636 if (peek(p, '_')) nextc(p);
9637 /* dummy 0, for tUMINUS_NUM at numeric */
9638 return set_integer_literal(p, INT2FIX(0), 0);
9639}
9640
9641static enum yytokentype
9642parse_numeric(struct parser_params *p, int c)
9643{
9644 int is_float, seen_point, seen_e, nondigit;
9645 int suffix;
9646
9647 is_float = seen_point = seen_e = nondigit = 0;
9648 SET_LEX_STATE(EXPR_END);
9649 newtok(p);
9650 if (c == '-' || c == '+') {
9651 tokadd(p, c);
9652 c = nextc(p);
9653 }
9654 if (c == '0') {
9655 int start = toklen(p);
9656 c = nextc(p);
9657 if (c == 'x' || c == 'X') {
9658 /* hexadecimal */
9659 c = nextc(p);
9660 if (c != -1 && ISXDIGIT(c)) {
9661 do {
9662 if (c == '_') {
9663 if (nondigit) break;
9664 nondigit = c;
9665 continue;
9666 }
9667 if (!ISXDIGIT(c)) break;
9668 nondigit = 0;
9669 tokadd(p, c);
9670 } while ((c = nextc(p)) != -1);
9671 }
9672 pushback(p, c);
9673 tokfix(p);
9674 if (toklen(p) == start) {
9675 return no_digits(p);
9676 }
9677 else if (nondigit) goto trailing_uc;
9678 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9679 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
9680 }
9681 if (c == 'b' || c == 'B') {
9682 /* binary */
9683 c = nextc(p);
9684 if (c == '0' || c == '1') {
9685 do {
9686 if (c == '_') {
9687 if (nondigit) break;
9688 nondigit = c;
9689 continue;
9690 }
9691 if (c != '0' && c != '1') break;
9692 nondigit = 0;
9693 tokadd(p, c);
9694 } while ((c = nextc(p)) != -1);
9695 }
9696 pushback(p, c);
9697 tokfix(p);
9698 if (toklen(p) == start) {
9699 return no_digits(p);
9700 }
9701 else if (nondigit) goto trailing_uc;
9702 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9703 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
9704 }
9705 if (c == 'd' || c == 'D') {
9706 /* decimal */
9707 c = nextc(p);
9708 if (c != -1 && ISDIGIT(c)) {
9709 do {
9710 if (c == '_') {
9711 if (nondigit) break;
9712 nondigit = c;
9713 continue;
9714 }
9715 if (!ISDIGIT(c)) break;
9716 nondigit = 0;
9717 tokadd(p, c);
9718 } while ((c = nextc(p)) != -1);
9719 }
9720 pushback(p, c);
9721 tokfix(p);
9722 if (toklen(p) == start) {
9723 return no_digits(p);
9724 }
9725 else if (nondigit) goto trailing_uc;
9726 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9727 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9728 }
9729 if (c == '_') {
9730 /* 0_0 */
9731 goto octal_number;
9732 }
9733 if (c == 'o' || c == 'O') {
9734 /* prefixed octal */
9735 c = nextc(p);
9736 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9737 return no_digits(p);
9738 }
9739 }
9740 if (c >= '0' && c <= '7') {
9741 /* octal */
9742 octal_number:
9743 do {
9744 if (c == '_') {
9745 if (nondigit) break;
9746 nondigit = c;
9747 continue;
9748 }
9749 if (c < '0' || c > '9') break;
9750 if (c > '7') goto invalid_octal;
9751 nondigit = 0;
9752 tokadd(p, c);
9753 } while ((c = nextc(p)) != -1);
9754 if (toklen(p) > start) {
9755 pushback(p, c);
9756 tokfix(p);
9757 if (nondigit) goto trailing_uc;
9758 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9759 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
9760 }
9761 if (nondigit) {
9762 pushback(p, c);
9763 goto trailing_uc;
9764 }
9765 }
9766 if (c > '7' && c <= '9') {
9767 invalid_octal:
9768 yyerror0("Invalid octal digit");
9769 }
9770 else if (c == '.' || c == 'e' || c == 'E') {
9771 tokadd(p, '0');
9772 }
9773 else {
9774 pushback(p, c);
9775 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9776 return set_integer_literal(p, INT2FIX(0), suffix);
9777 }
9778 }
9779
9780 for (;;) {
9781 switch (c) {
9782 case '0': case '1': case '2': case '3': case '4':
9783 case '5': case '6': case '7': case '8': case '9':
9784 nondigit = 0;
9785 tokadd(p, c);
9786 break;
9787
9788 case '.':
9789 if (nondigit) goto trailing_uc;
9790 if (seen_point || seen_e) {
9791 goto decode_num;
9792 }
9793 else {
9794 int c0 = nextc(p);
9795 if (c0 == -1 || !ISDIGIT(c0)) {
9796 pushback(p, c0);
9797 goto decode_num;
9798 }
9799 c = c0;
9800 }
9801 seen_point = toklen(p);
9802 tokadd(p, '.');
9803 tokadd(p, c);
9804 is_float++;
9805 nondigit = 0;
9806 break;
9807
9808 case 'e':
9809 case 'E':
9810 if (nondigit) {
9811 pushback(p, c);
9812 c = nondigit;
9813 goto decode_num;
9814 }
9815 if (seen_e) {
9816 goto decode_num;
9817 }
9818 nondigit = c;
9819 c = nextc(p);
9820 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9821 pushback(p, c);
9822 c = nondigit;
9823 nondigit = 0;
9824 goto decode_num;
9825 }
9826 tokadd(p, nondigit);
9827 seen_e++;
9828 is_float++;
9829 tokadd(p, c);
9830 nondigit = (c == '-' || c == '+') ? c : 0;
9831 break;
9832
9833 case '_': /* `_' in number just ignored */
9834 if (nondigit) goto decode_num;
9835 nondigit = c;
9836 break;
9837
9838 default:
9839 goto decode_num;
9840 }
9841 c = nextc(p);
9842 }
9843
9844 decode_num:
9845 pushback(p, c);
9846 if (nondigit) {
9847 trailing_uc:
9848 literal_flush(p, p->lex.pcur - 1);
9849 YYLTYPE loc = RUBY_INIT_YYLLOC();
9850 compile_error(p, "trailing `%c' in number", nondigit);
9851 parser_show_error_line(p, &loc);
9852 }
9853 tokfix(p);
9854 if (is_float) {
9855 enum yytokentype type = tFLOAT;
9856 VALUE v;
9857
9858 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9859 if (suffix & NUM_SUFFIX_R) {
9860 type = tRATIONAL;
9861 v = parse_rational(p, tok(p), toklen(p), seen_point);
9862 }
9863 else {
9864 double d = strtod(tok(p), 0);
9865 if (errno == ERANGE) {
9866 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9867 errno = 0;
9868 }
9869 v = DBL2NUM(d);
9870 }
9871 return set_number_literal(p, v, type, suffix);
9872 }
9873 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9874 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9875}
9876
9877static enum yytokentype
9878parse_qmark(struct parser_params *p, int space_seen)
9879{
9880 rb_encoding *enc;
9881 register int c;
9882 VALUE lit;
9883
9884 if (IS_END()) {
9885 SET_LEX_STATE(EXPR_VALUE);
9886 return '?';
9887 }
9888 c = nextc(p);
9889 if (c == -1) {
9890 compile_error(p, "incomplete character syntax");
9891 return 0;
9892 }
9893 if (rb_enc_isspace(c, p->enc)) {
9894 if (!IS_ARG()) {
9895 int c2 = escaped_control_code(c);
9896 if (c2) {
9897 WARN_SPACE_CHAR(c2, "?");
9898 }
9899 }
9900 ternary:
9901 pushback(p, c);
9902 SET_LEX_STATE(EXPR_VALUE);
9903 return '?';
9904 }
9905 newtok(p);
9906 enc = p->enc;
9907 if (!parser_isascii(p)) {
9908 if (tokadd_mbchar(p, c) == -1) return 0;
9909 }
9910 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
9911 !lex_eol_p(p) && is_identchar(p, p->lex.pcur, p->lex.pend, p->enc)) {
9912 if (space_seen) {
9913 const char *start = p->lex.pcur - 1, *ptr = start;
9914 do {
9915 int n = parser_precise_mbclen(p, ptr);
9916 if (n < 0) return -1;
9917 ptr += n;
9918 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9919 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
9920 " a conditional operator, put a space after `?'",
9921 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9922 }
9923 goto ternary;
9924 }
9925 else if (c == '\\') {
9926 if (peek(p, 'u')) {
9927 nextc(p);
9928 enc = rb_utf8_encoding();
9929 tokadd_utf8(p, &enc, -1, 0, 0);
9930 }
9931 else if (!ISASCII(c = peekc(p))) {
9932 nextc(p);
9933 if (tokadd_mbchar(p, c) == -1) return 0;
9934 }
9935 else {
9936 c = read_escape(p, 0);
9937 tokadd(p, c);
9938 }
9939 }
9940 else {
9941 tokadd(p, c);
9942 }
9943 tokfix(p);
9944 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9945 set_yylval_str(lit);
9946 SET_LEX_STATE(EXPR_END);
9947 return tCHAR;
9948}
9949
9950static enum yytokentype
9951parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9952{
9953 register int c;
9954 const char *ptok = p->lex.pcur;
9955
9956 if (IS_BEG()) {
9957 int term;
9958 int paren;
9959
9960 c = nextc(p);
9961 quotation:
9962 if (c == -1) goto unterminated;
9963 if (!ISALNUM(c)) {
9964 term = c;
9965 if (!ISASCII(c)) goto unknown;
9966 c = 'Q';
9967 }
9968 else {
9969 term = nextc(p);
9970 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
9971 unknown:
9972 pushback(p, term);
9973 c = parser_precise_mbclen(p, p->lex.pcur);
9974 if (c < 0) return 0;
9975 p->lex.pcur += c;
9976 yyerror0("unknown type of %string");
9977 return 0;
9978 }
9979 }
9980 if (term == -1) {
9981 unterminated:
9982 compile_error(p, "unterminated quoted string meets end of file");
9983 return 0;
9984 }
9985 paren = term;
9986 if (term == '(') term = ')';
9987 else if (term == '[') term = ']';
9988 else if (term == '{') term = '}';
9989 else if (term == '<') term = '>';
9990 else paren = 0;
9991
9992 p->lex.ptok = ptok-1;
9993 switch (c) {
9994 case 'Q':
9995 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
9996 return tSTRING_BEG;
9997
9998 case 'q':
9999 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10000 return tSTRING_BEG;
10001
10002 case 'W':
10003 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10004 return tWORDS_BEG;
10005
10006 case 'w':
10007 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10008 return tQWORDS_BEG;
10009
10010 case 'I':
10011 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10012 return tSYMBOLS_BEG;
10013
10014 case 'i':
10015 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10016 return tQSYMBOLS_BEG;
10017
10018 case 'x':
10019 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10020 return tXSTRING_BEG;
10021
10022 case 'r':
10023 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10024 return tREGEXP_BEG;
10025
10026 case 's':
10027 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10028 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10029 return tSYMBEG;
10030
10031 default:
10032 yyerror0("unknown type of %string");
10033 return 0;
10034 }
10035 }
10036 if ((c = nextc(p)) == '=') {
10037 set_yylval_id('%');
10038 SET_LEX_STATE(EXPR_BEG);
10039 return tOP_ASGN;
10040 }
10041 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10042 goto quotation;
10043 }
10044 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10045 pushback(p, c);
10046 return warn_balanced('%', "%%", "string literal");
10047}
10048
10049static int
10050tokadd_ident(struct parser_params *p, int c)
10051{
10052 do {
10053 if (tokadd_mbchar(p, c) == -1) return -1;
10054 c = nextc(p);
10055 } while (parser_is_identchar(p));
10056 pushback(p, c);
10057 return 0;
10058}
10059
10060static ID
10061tokenize_ident(struct parser_params *p)
10062{
10063 ID ident = TOK_INTERN();
10064
10065 set_yylval_name(ident);
10066
10067 return ident;
10068}
10069
10070static int
10071parse_numvar(struct parser_params *p)
10072{
10073 size_t len;
10074 int overflow;
10075 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10076 const unsigned long nth_ref_max =
10077 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10078 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10079 * turned into a Fixnum, in compile.c */
10080
10081 if (overflow || n > nth_ref_max) {
10082 /* compile_error()? */
10083 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10084 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10085 }
10086 else {
10087 return (int)n;
10088 }
10089}
10090
10091static enum yytokentype
10092parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10093{
10094 const char *ptr = p->lex.pcur;
10095 register int c;
10096
10097 SET_LEX_STATE(EXPR_END);
10098 p->lex.ptok = ptr - 1; /* from '$' */
10099 newtok(p);
10100 c = nextc(p);
10101 switch (c) {
10102 case '_': /* $_: last read line string */
10103 c = nextc(p);
10104 if (parser_is_identchar(p)) {
10105 tokadd(p, '$');
10106 tokadd(p, '_');
10107 break;
10108 }
10109 pushback(p, c);
10110 c = '_';
10111 /* fall through */
10112 case '~': /* $~: match-data */
10113 case '*': /* $*: argv */
10114 case '$': /* $$: pid */
10115 case '?': /* $?: last status */
10116 case '!': /* $!: error string */
10117 case '@': /* $@: error position */
10118 case '/': /* $/: input record separator */
10119 case '\\': /* $\: output record separator */
10120 case ';': /* $;: field separator */
10121 case ',': /* $,: output field separator */
10122 case '.': /* $.: last read line number */
10123 case '=': /* $=: ignorecase */
10124 case ':': /* $:: load path */
10125 case '<': /* $<: reading filename */
10126 case '>': /* $>: default output handle */
10127 case '\"': /* $": already loaded files */
10128 tokadd(p, '$');
10129 tokadd(p, c);
10130 goto gvar;
10131
10132 case '-':
10133 tokadd(p, '$');
10134 tokadd(p, c);
10135 c = nextc(p);
10136 if (parser_is_identchar(p)) {
10137 if (tokadd_mbchar(p, c) == -1) return 0;
10138 }
10139 else {
10140 pushback(p, c);
10141 pushback(p, '-');
10142 return '$';
10143 }
10144 gvar:
10145 set_yylval_name(TOK_INTERN());
10146 return tGVAR;
10147
10148 case '&': /* $&: last match */
10149 case '`': /* $`: string before last match */
10150 case '\'': /* $': string after last match */
10151 case '+': /* $+: string matches last paren. */
10152 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10153 tokadd(p, '$');
10154 tokadd(p, c);
10155 goto gvar;
10156 }
10157 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10158 return tBACK_REF;
10159
10160 case '1': case '2': case '3':
10161 case '4': case '5': case '6':
10162 case '7': case '8': case '9':
10163 tokadd(p, '$');
10164 do {
10165 tokadd(p, c);
10166 c = nextc(p);
10167 } while (c != -1 && ISDIGIT(c));
10168 pushback(p, c);
10169 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10170 tokfix(p);
10171 c = parse_numvar(p);
10172 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10173 return tNTH_REF;
10174
10175 default:
10176 if (!parser_is_identchar(p)) {
10177 YYLTYPE loc = RUBY_INIT_YYLLOC();
10178 if (c == -1 || ISSPACE(c)) {
10179 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
10180 }
10181 else {
10182 pushback(p, c);
10183 compile_error(p, "`$%c' is not allowed as a global variable name", c);
10184 }
10185 parser_show_error_line(p, &loc);
10186 set_yylval_noname();
10187 return tGVAR;
10188 }
10189 /* fall through */
10190 case '0':
10191 tokadd(p, '$');
10192 }
10193
10194 if (tokadd_ident(p, c)) return 0;
10195 SET_LEX_STATE(EXPR_END);
10196 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10197 tokenize_ident(p);
10198 }
10199 else {
10200 compile_error(p, "`%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10201 set_yylval_noname();
10202 }
10203 return tGVAR;
10204}
10205
10206#ifndef RIPPER
10207static bool
10208parser_numbered_param(struct parser_params *p, int n)
10209{
10210 if (n < 0) return false;
10211
10212 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10213 return false;
10214 }
10215 if (p->max_numparam == ORDINAL_PARAM) {
10216 compile_error(p, "ordinary parameter is defined");
10217 return false;
10218 }
10219 struct vtable *args = p->lvtbl->args;
10220 if (p->max_numparam < n) {
10221 p->max_numparam = n;
10222 }
10223 while (n > args->pos) {
10224 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10225 }
10226 return true;
10227}
10228#endif
10229
10230static enum yytokentype
10231parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10232{
10233 const char *ptr = p->lex.pcur;
10234 enum yytokentype result = tIVAR;
10235 register int c = nextc(p);
10236 YYLTYPE loc;
10237
10238 p->lex.ptok = ptr - 1; /* from '@' */
10239 newtok(p);
10240 tokadd(p, '@');
10241 if (c == '@') {
10242 result = tCVAR;
10243 tokadd(p, '@');
10244 c = nextc(p);
10245 }
10246 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10247 if (c == -1 || !parser_is_identchar(p)) {
10248 pushback(p, c);
10249 RUBY_SET_YYLLOC(loc);
10250 if (result == tIVAR) {
10251 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
10252 }
10253 else {
10254 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
10255 }
10256 parser_show_error_line(p, &loc);
10257 set_yylval_noname();
10258 SET_LEX_STATE(EXPR_END);
10259 return result;
10260 }
10261 else if (ISDIGIT(c)) {
10262 pushback(p, c);
10263 RUBY_SET_YYLLOC(loc);
10264 if (result == tIVAR) {
10265 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
10266 }
10267 else {
10268 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
10269 }
10270 parser_show_error_line(p, &loc);
10271 set_yylval_noname();
10272 SET_LEX_STATE(EXPR_END);
10273 return result;
10274 }
10275
10276 if (tokadd_ident(p, c)) return 0;
10277 tokenize_ident(p);
10278 return result;
10279}
10280
10281static enum yytokentype
10282parse_ident(struct parser_params *p, int c, int cmd_state)
10283{
10284 enum yytokentype result;
10285 int mb = ENC_CODERANGE_7BIT;
10286 const enum lex_state_e last_state = p->lex.state;
10287 ID ident;
10288 int enforce_keyword_end = 0;
10289
10290 do {
10291 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
10292 if (tokadd_mbchar(p, c) == -1) return 0;
10293 c = nextc(p);
10294 } while (parser_is_identchar(p));
10295 if ((c == '!' || c == '?') && !peek(p, '=')) {
10296 result = tFID;
10297 tokadd(p, c);
10298 }
10299 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10300 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10301 result = tIDENTIFIER;
10302 tokadd(p, c);
10303 }
10304 else {
10305 result = tCONSTANT; /* assume provisionally */
10306 pushback(p, c);
10307 }
10308 tokfix(p);
10309
10310 if (IS_LABEL_POSSIBLE()) {
10311 if (IS_LABEL_SUFFIX(0)) {
10312 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10313 nextc(p);
10314 set_yylval_name(TOK_INTERN());
10315 return tLABEL;
10316 }
10317 }
10318
10319#ifndef RIPPER
10320 if (!NIL_P(peek_end_expect_token_locations(p))) {
10321 VALUE end_loc;
10322 int lineno, column;
10323 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10324
10325 end_loc = peek_end_expect_token_locations(p);
10326 lineno = NUM2INT(rb_ary_entry(end_loc, 0));
10327 column = NUM2INT(rb_ary_entry(end_loc, 1));
10328
10329 if (p->debug) {
10330 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10331 p->ruby_sourceline, beg_pos, lineno, column);
10332 }
10333
10334 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10335 const struct kwtable *kw;
10336
10337 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10338 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10339 enforce_keyword_end = 1;
10340 }
10341 }
10342 }
10343#endif
10344
10345 if (mb == ENC_CODERANGE_7BIT && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10346 const struct kwtable *kw;
10347
10348 /* See if it is a reserved word. */
10349 kw = rb_reserved_word(tok(p), toklen(p));
10350 if (kw) {
10351 enum lex_state_e state = p->lex.state;
10352 if (IS_lex_state_for(state, EXPR_FNAME)) {
10353 SET_LEX_STATE(EXPR_ENDFN);
10354 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10355 return kw->id[0];
10356 }
10357 SET_LEX_STATE(kw->state);
10358 if (IS_lex_state(EXPR_BEG)) {
10359 p->command_start = TRUE;
10360 }
10361 if (kw->id[0] == keyword_do) {
10362 if (lambda_beginning_p()) {
10363 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10364 return keyword_do_LAMBDA;
10365 }
10366 if (COND_P()) return keyword_do_cond;
10367 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10368 return keyword_do_block;
10369 return keyword_do;
10370 }
10371 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10372 return kw->id[0];
10373 else {
10374 if (kw->id[0] != kw->id[1])
10375 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10376 return kw->id[1];
10377 }
10378 }
10379 }
10380
10381 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10382 if (cmd_state) {
10383 SET_LEX_STATE(EXPR_CMDARG);
10384 }
10385 else {
10386 SET_LEX_STATE(EXPR_ARG);
10387 }
10388 }
10389 else if (p->lex.state == EXPR_FNAME) {
10390 SET_LEX_STATE(EXPR_ENDFN);
10391 }
10392 else {
10393 SET_LEX_STATE(EXPR_END);
10394 }
10395
10396 ident = tokenize_ident(p);
10397 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10398 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10399 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10400 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10401 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10402 }
10403 return result;
10404}
10405
10406static void
10407warn_cr(struct parser_params *p)
10408{
10409 if (!p->cr_seen) {
10410 p->cr_seen = TRUE;
10411 /* carried over with p->lex.nextline for nextc() */
10412 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10413 }
10414}
10415
10416static enum yytokentype
10417parser_yylex(struct parser_params *p)
10418{
10419 register int c;
10420 int space_seen = 0;
10421 int cmd_state;
10422 int label;
10423 enum lex_state_e last_state;
10424 int fallthru = FALSE;
10425 int token_seen = p->token_seen;
10426
10427 if (p->lex.strterm) {
10428 if (strterm_is_heredoc(p->lex.strterm)) {
10429 token_flush(p);
10430 return here_document(p, &p->lex.strterm->u.heredoc);
10431 }
10432 else {
10433 token_flush(p);
10434 return parse_string(p, &p->lex.strterm->u.literal);
10435 }
10436 }
10437 cmd_state = p->command_start;
10438 p->command_start = FALSE;
10439 p->token_seen = TRUE;
10440#ifndef RIPPER
10441 token_flush(p);
10442#endif
10443 retry:
10444 last_state = p->lex.state;
10445 switch (c = nextc(p)) {
10446 case '\0': /* NUL */
10447 case '\004': /* ^D */
10448 case '\032': /* ^Z */
10449 case -1: /* end of script. */
10450 p->eofp = 1;
10451#ifndef RIPPER
10452 if (!NIL_P(p->end_expect_token_locations) && RARRAY_LEN(p->end_expect_token_locations) > 0) {
10453 pop_end_expect_token_locations(p);
10454 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10455 return tDUMNY_END;
10456 }
10457#endif
10458 /* Set location for end-of-input because dispatch_scan_event is not called. */
10459 RUBY_SET_YYLLOC(*p->yylloc);
10460 return END_OF_INPUT;
10461
10462 /* white spaces */
10463 case '\r':
10464 warn_cr(p);
10465 /* fall through */
10466 case ' ': case '\t': case '\f':
10467 case '\13': /* '\v' */
10468 space_seen = 1;
10469 while ((c = nextc(p))) {
10470 switch (c) {
10471 case '\r':
10472 warn_cr(p);
10473 /* fall through */
10474 case ' ': case '\t': case '\f':
10475 case '\13': /* '\v' */
10476 break;
10477 default:
10478 goto outofloop;
10479 }
10480 }
10481 outofloop:
10482 pushback(p, c);
10483 dispatch_scan_event(p, tSP);
10484#ifndef RIPPER
10485 token_flush(p);
10486#endif
10487 goto retry;
10488
10489 case '#': /* it's a comment */
10490 p->token_seen = token_seen;
10491 /* no magic_comment in shebang line */
10492 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10493 if (comment_at_top(p)) {
10494 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10495 }
10496 }
10497 lex_goto_eol(p);
10498 dispatch_scan_event(p, tCOMMENT);
10499 fallthru = TRUE;
10500 /* fall through */
10501 case '\n':
10502 p->token_seen = token_seen;
10503 VALUE prevline = p->lex.lastline;
10504 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10505 !IS_lex_state(EXPR_LABELED));
10506 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10507 if (!fallthru) {
10508 dispatch_scan_event(p, tIGNORED_NL);
10509 }
10510 fallthru = FALSE;
10511 if (!c && p->ctxt.in_kwarg) {
10512 goto normal_newline;
10513 }
10514 goto retry;
10515 }
10516 while (1) {
10517 switch (c = nextc(p)) {
10518 case ' ': case '\t': case '\f': case '\r':
10519 case '\13': /* '\v' */
10520 space_seen = 1;
10521 break;
10522 case '#':
10523 pushback(p, c);
10524 if (space_seen) {
10525 dispatch_scan_event(p, tSP);
10526 token_flush(p);
10527 }
10528 goto retry;
10529 case '&':
10530 case '.': {
10531 dispatch_delayed_token(p, tIGNORED_NL);
10532 if (peek(p, '.') == (c == '&')) {
10533 pushback(p, c);
10534 dispatch_scan_event(p, tSP);
10535 goto retry;
10536 }
10537 }
10538 default:
10539 p->ruby_sourceline--;
10540 p->lex.nextline = p->lex.lastline;
10541 set_lastline(p, prevline);
10542 case -1: /* EOF no decrement*/
10543 lex_goto_eol(p);
10544 if (c != -1) {
10545 token_flush(p);
10546 RUBY_SET_YYLLOC(*p->yylloc);
10547 }
10548 goto normal_newline;
10549 }
10550 }
10551 normal_newline:
10552 p->command_start = TRUE;
10553 SET_LEX_STATE(EXPR_BEG);
10554 return '\n';
10555
10556 case '*':
10557 if ((c = nextc(p)) == '*') {
10558 if ((c = nextc(p)) == '=') {
10559 set_yylval_id(idPow);
10560 SET_LEX_STATE(EXPR_BEG);
10561 return tOP_ASGN;
10562 }
10563 pushback(p, c);
10564 if (IS_SPCARG(c)) {
10565 rb_warning0("`**' interpreted as argument prefix");
10566 c = tDSTAR;
10567 }
10568 else if (IS_BEG()) {
10569 c = tDSTAR;
10570 }
10571 else {
10572 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10573 }
10574 }
10575 else {
10576 if (c == '=') {
10577 set_yylval_id('*');
10578 SET_LEX_STATE(EXPR_BEG);
10579 return tOP_ASGN;
10580 }
10581 pushback(p, c);
10582 if (IS_SPCARG(c)) {
10583 rb_warning0("`*' interpreted as argument prefix");
10584 c = tSTAR;
10585 }
10586 else if (IS_BEG()) {
10587 c = tSTAR;
10588 }
10589 else {
10590 c = warn_balanced('*', "*", "argument prefix");
10591 }
10592 }
10593 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10594 return c;
10595
10596 case '!':
10597 c = nextc(p);
10598 if (IS_AFTER_OPERATOR()) {
10599 SET_LEX_STATE(EXPR_ARG);
10600 if (c == '@') {
10601 return '!';
10602 }
10603 }
10604 else {
10605 SET_LEX_STATE(EXPR_BEG);
10606 }
10607 if (c == '=') {
10608 return tNEQ;
10609 }
10610 if (c == '~') {
10611 return tNMATCH;
10612 }
10613 pushback(p, c);
10614 return '!';
10615
10616 case '=':
10617 if (was_bol(p)) {
10618 /* skip embedded rd document */
10619 if (word_match_p(p, "begin", 5)) {
10620 int first_p = TRUE;
10621
10622 lex_goto_eol(p);
10623 dispatch_scan_event(p, tEMBDOC_BEG);
10624 for (;;) {
10625 lex_goto_eol(p);
10626 if (!first_p) {
10627 dispatch_scan_event(p, tEMBDOC);
10628 }
10629 first_p = FALSE;
10630 c = nextc(p);
10631 if (c == -1) {
10632 compile_error(p, "embedded document meets end of file");
10633 return END_OF_INPUT;
10634 }
10635 if (c == '=' && word_match_p(p, "end", 3)) {
10636 break;
10637 }
10638 pushback(p, c);
10639 }
10640 lex_goto_eol(p);
10641 dispatch_scan_event(p, tEMBDOC_END);
10642 goto retry;
10643 }
10644 }
10645
10646 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10647 if ((c = nextc(p)) == '=') {
10648 if ((c = nextc(p)) == '=') {
10649 return tEQQ;
10650 }
10651 pushback(p, c);
10652 return tEQ;
10653 }
10654 if (c == '~') {
10655 return tMATCH;
10656 }
10657 else if (c == '>') {
10658 return tASSOC;
10659 }
10660 pushback(p, c);
10661 return '=';
10662
10663 case '<':
10664 c = nextc(p);
10665 if (c == '<' &&
10666 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10667 !IS_END() &&
10668 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10669 enum yytokentype token = heredoc_identifier(p);
10670 if (token) return token < 0 ? 0 : token;
10671 }
10672 if (IS_AFTER_OPERATOR()) {
10673 SET_LEX_STATE(EXPR_ARG);
10674 }
10675 else {
10676 if (IS_lex_state(EXPR_CLASS))
10677 p->command_start = TRUE;
10678 SET_LEX_STATE(EXPR_BEG);
10679 }
10680 if (c == '=') {
10681 if ((c = nextc(p)) == '>') {
10682 return tCMP;
10683 }
10684 pushback(p, c);
10685 return tLEQ;
10686 }
10687 if (c == '<') {
10688 if ((c = nextc(p)) == '=') {
10689 set_yylval_id(idLTLT);
10690 SET_LEX_STATE(EXPR_BEG);
10691 return tOP_ASGN;
10692 }
10693 pushback(p, c);
10694 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10695 }
10696 pushback(p, c);
10697 return '<';
10698
10699 case '>':
10700 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10701 if ((c = nextc(p)) == '=') {
10702 return tGEQ;
10703 }
10704 if (c == '>') {
10705 if ((c = nextc(p)) == '=') {
10706 set_yylval_id(idGTGT);
10707 SET_LEX_STATE(EXPR_BEG);
10708 return tOP_ASGN;
10709 }
10710 pushback(p, c);
10711 return tRSHFT;
10712 }
10713 pushback(p, c);
10714 return '>';
10715
10716 case '"':
10717 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10718 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10719 p->lex.ptok = p->lex.pcur-1;
10720 return tSTRING_BEG;
10721
10722 case '`':
10723 if (IS_lex_state(EXPR_FNAME)) {
10724 SET_LEX_STATE(EXPR_ENDFN);
10725 return c;
10726 }
10727 if (IS_lex_state(EXPR_DOT)) {
10728 if (cmd_state)
10729 SET_LEX_STATE(EXPR_CMDARG);
10730 else
10731 SET_LEX_STATE(EXPR_ARG);
10732 return c;
10733 }
10734 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10735 return tXSTRING_BEG;
10736
10737 case '\'':
10738 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10739 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10740 p->lex.ptok = p->lex.pcur-1;
10741 return tSTRING_BEG;
10742
10743 case '?':
10744 return parse_qmark(p, space_seen);
10745
10746 case '&':
10747 if ((c = nextc(p)) == '&') {
10748 SET_LEX_STATE(EXPR_BEG);
10749 if ((c = nextc(p)) == '=') {
10750 set_yylval_id(idANDOP);
10751 SET_LEX_STATE(EXPR_BEG);
10752 return tOP_ASGN;
10753 }
10754 pushback(p, c);
10755 return tANDOP;
10756 }
10757 else if (c == '=') {
10758 set_yylval_id('&');
10759 SET_LEX_STATE(EXPR_BEG);
10760 return tOP_ASGN;
10761 }
10762 else if (c == '.') {
10763 set_yylval_id(idANDDOT);
10764 SET_LEX_STATE(EXPR_DOT);
10765 return tANDDOT;
10766 }
10767 pushback(p, c);
10768 if (IS_SPCARG(c)) {
10769 if ((c != ':') ||
10770 (c = peekc_n(p, 1)) == -1 ||
10771 !(c == '\'' || c == '"' ||
10772 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10773 rb_warning0("`&' interpreted as argument prefix");
10774 }
10775 c = tAMPER;
10776 }
10777 else if (IS_BEG()) {
10778 c = tAMPER;
10779 }
10780 else {
10781 c = warn_balanced('&', "&", "argument prefix");
10782 }
10783 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10784 return c;
10785
10786 case '|':
10787 if ((c = nextc(p)) == '|') {
10788 SET_LEX_STATE(EXPR_BEG);
10789 if ((c = nextc(p)) == '=') {
10790 set_yylval_id(idOROP);
10791 SET_LEX_STATE(EXPR_BEG);
10792 return tOP_ASGN;
10793 }
10794 pushback(p, c);
10795 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10796 c = '|';
10797 pushback(p, '|');
10798 return c;
10799 }
10800 return tOROP;
10801 }
10802 if (c == '=') {
10803 set_yylval_id('|');
10804 SET_LEX_STATE(EXPR_BEG);
10805 return tOP_ASGN;
10806 }
10807 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10808 pushback(p, c);
10809 return '|';
10810
10811 case '+':
10812 c = nextc(p);
10813 if (IS_AFTER_OPERATOR()) {
10814 SET_LEX_STATE(EXPR_ARG);
10815 if (c == '@') {
10816 return tUPLUS;
10817 }
10818 pushback(p, c);
10819 return '+';
10820 }
10821 if (c == '=') {
10822 set_yylval_id('+');
10823 SET_LEX_STATE(EXPR_BEG);
10824 return tOP_ASGN;
10825 }
10826 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10827 SET_LEX_STATE(EXPR_BEG);
10828 pushback(p, c);
10829 if (c != -1 && ISDIGIT(c)) {
10830 return parse_numeric(p, '+');
10831 }
10832 return tUPLUS;
10833 }
10834 SET_LEX_STATE(EXPR_BEG);
10835 pushback(p, c);
10836 return warn_balanced('+', "+", "unary operator");
10837
10838 case '-':
10839 c = nextc(p);
10840 if (IS_AFTER_OPERATOR()) {
10841 SET_LEX_STATE(EXPR_ARG);
10842 if (c == '@') {
10843 return tUMINUS;
10844 }
10845 pushback(p, c);
10846 return '-';
10847 }
10848 if (c == '=') {
10849 set_yylval_id('-');
10850 SET_LEX_STATE(EXPR_BEG);
10851 return tOP_ASGN;
10852 }
10853 if (c == '>') {
10854 SET_LEX_STATE(EXPR_ENDFN);
10855 return tLAMBDA;
10856 }
10857 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10858 SET_LEX_STATE(EXPR_BEG);
10859 pushback(p, c);
10860 if (c != -1 && ISDIGIT(c)) {
10861 return tUMINUS_NUM;
10862 }
10863 return tUMINUS;
10864 }
10865 SET_LEX_STATE(EXPR_BEG);
10866 pushback(p, c);
10867 return warn_balanced('-', "-", "unary operator");
10868
10869 case '.': {
10870 int is_beg = IS_BEG();
10871 SET_LEX_STATE(EXPR_BEG);
10872 if ((c = nextc(p)) == '.') {
10873 if ((c = nextc(p)) == '.') {
10874 if (p->ctxt.in_argdef) {
10875 SET_LEX_STATE(EXPR_ENDARG);
10876 return tBDOT3;
10877 }
10878 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10879 rb_warn0("... at EOL, should be parenthesized?");
10880 }
10881 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
10882 if (IS_lex_state_for(last_state, EXPR_LABEL))
10883 return tDOT3;
10884 }
10885 return is_beg ? tBDOT3 : tDOT3;
10886 }
10887 pushback(p, c);
10888 return is_beg ? tBDOT2 : tDOT2;
10889 }
10890 pushback(p, c);
10891 if (c != -1 && ISDIGIT(c)) {
10892 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10893 parse_numeric(p, '.');
10894 if (ISDIGIT(prev)) {
10895 yyerror0("unexpected fraction part after numeric literal");
10896 }
10897 else {
10898 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10899 }
10900 SET_LEX_STATE(EXPR_END);
10901 p->lex.ptok = p->lex.pcur;
10902 goto retry;
10903 }
10904 set_yylval_id('.');
10905 SET_LEX_STATE(EXPR_DOT);
10906 return '.';
10907 }
10908
10909 case '0': case '1': case '2': case '3': case '4':
10910 case '5': case '6': case '7': case '8': case '9':
10911 return parse_numeric(p, c);
10912
10913 case ')':
10914 COND_POP();
10915 CMDARG_POP();
10916 SET_LEX_STATE(EXPR_ENDFN);
10917 p->lex.paren_nest--;
10918 return c;
10919
10920 case ']':
10921 COND_POP();
10922 CMDARG_POP();
10923 SET_LEX_STATE(EXPR_END);
10924 p->lex.paren_nest--;
10925 return c;
10926
10927 case '}':
10928 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10929 if (!p->lex.brace_nest--) return tSTRING_DEND;
10930 COND_POP();
10931 CMDARG_POP();
10932 SET_LEX_STATE(EXPR_END);
10933 p->lex.paren_nest--;
10934 return c;
10935
10936 case ':':
10937 c = nextc(p);
10938 if (c == ':') {
10939 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10940 SET_LEX_STATE(EXPR_BEG);
10941 return tCOLON3;
10942 }
10943 set_yylval_id(idCOLON2);
10944 SET_LEX_STATE(EXPR_DOT);
10945 return tCOLON2;
10946 }
10947 if (IS_END() || ISSPACE(c) || c == '#') {
10948 pushback(p, c);
10949 c = warn_balanced(':', ":", "symbol literal");
10950 SET_LEX_STATE(EXPR_BEG);
10951 return c;
10952 }
10953 switch (c) {
10954 case '\'':
10955 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
10956 break;
10957 case '"':
10958 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
10959 break;
10960 default:
10961 pushback(p, c);
10962 break;
10963 }
10964 SET_LEX_STATE(EXPR_FNAME);
10965 return tSYMBEG;
10966
10967 case '/':
10968 if (IS_BEG()) {
10969 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10970 return tREGEXP_BEG;
10971 }
10972 if ((c = nextc(p)) == '=') {
10973 set_yylval_id('/');
10974 SET_LEX_STATE(EXPR_BEG);
10975 return tOP_ASGN;
10976 }
10977 pushback(p, c);
10978 if (IS_SPCARG(c)) {
10979 arg_ambiguous(p, '/');
10980 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10981 return tREGEXP_BEG;
10982 }
10983 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10984 return warn_balanced('/', "/", "regexp literal");
10985
10986 case '^':
10987 if ((c = nextc(p)) == '=') {
10988 set_yylval_id('^');
10989 SET_LEX_STATE(EXPR_BEG);
10990 return tOP_ASGN;
10991 }
10992 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10993 pushback(p, c);
10994 return '^';
10995
10996 case ';':
10997 SET_LEX_STATE(EXPR_BEG);
10998 p->command_start = TRUE;
10999 return ';';
11000
11001 case ',':
11002 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11003 return ',';
11004
11005 case '~':
11006 if (IS_AFTER_OPERATOR()) {
11007 if ((c = nextc(p)) != '@') {
11008 pushback(p, c);
11009 }
11010 SET_LEX_STATE(EXPR_ARG);
11011 }
11012 else {
11013 SET_LEX_STATE(EXPR_BEG);
11014 }
11015 return '~';
11016
11017 case '(':
11018 if (IS_BEG()) {
11019 c = tLPAREN;
11020 }
11021 else if (!space_seen) {
11022 /* foo( ... ) => method call, no ambiguity */
11023 }
11024 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11025 c = tLPAREN_ARG;
11026 }
11027 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11028 rb_warning0("parentheses after method name is interpreted as "
11029 "an argument list, not a decomposed argument");
11030 }
11031 p->lex.paren_nest++;
11032 COND_PUSH(0);
11033 CMDARG_PUSH(0);
11034 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11035 return c;
11036
11037 case '[':
11038 p->lex.paren_nest++;
11039 if (IS_AFTER_OPERATOR()) {
11040 if ((c = nextc(p)) == ']') {
11041 p->lex.paren_nest--;
11042 SET_LEX_STATE(EXPR_ARG);
11043 if ((c = nextc(p)) == '=') {
11044 return tASET;
11045 }
11046 pushback(p, c);
11047 return tAREF;
11048 }
11049 pushback(p, c);
11050 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11051 return '[';
11052 }
11053 else if (IS_BEG()) {
11054 c = tLBRACK;
11055 }
11056 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11057 c = tLBRACK;
11058 }
11059 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11060 COND_PUSH(0);
11061 CMDARG_PUSH(0);
11062 return c;
11063
11064 case '{':
11065 ++p->lex.brace_nest;
11066 if (lambda_beginning_p())
11067 c = tLAMBEG;
11068 else if (IS_lex_state(EXPR_LABELED))
11069 c = tLBRACE; /* hash */
11070 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11071 c = '{'; /* block (primary) */
11072 else if (IS_lex_state(EXPR_ENDARG))
11073 c = tLBRACE_ARG; /* block (expr) */
11074 else
11075 c = tLBRACE; /* hash */
11076 if (c != tLBRACE) {
11077 p->command_start = TRUE;
11078 SET_LEX_STATE(EXPR_BEG);
11079 }
11080 else {
11081 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11082 }
11083 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11084 COND_PUSH(0);
11085 CMDARG_PUSH(0);
11086 return c;
11087
11088 case '\\':
11089 c = nextc(p);
11090 if (c == '\n') {
11091 space_seen = 1;
11092 dispatch_scan_event(p, tSP);
11093 goto retry; /* skip \\n */
11094 }
11095 if (c == ' ') return tSP;
11096 if (ISSPACE(c)) return c;
11097 pushback(p, c);
11098 return '\\';
11099
11100 case '%':
11101 return parse_percent(p, space_seen, last_state);
11102
11103 case '$':
11104 return parse_gvar(p, last_state);
11105
11106 case '@':
11107 return parse_atmark(p, last_state);
11108
11109 case '_':
11110 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11111 p->ruby__end__seen = 1;
11112 p->eofp = 1;
11113#ifdef RIPPER
11114 lex_goto_eol(p);
11115 dispatch_scan_event(p, k__END__);
11116#endif
11117 return END_OF_INPUT;
11118 }
11119 newtok(p);
11120 break;
11121
11122 default:
11123 if (!parser_is_identchar(p)) {
11124 compile_error(p, "Invalid char `\\x%02X' in expression", c);
11125 token_flush(p);
11126 goto retry;
11127 }
11128
11129 newtok(p);
11130 break;
11131 }
11132
11133 return parse_ident(p, c, cmd_state);
11134}
11135
11136static enum yytokentype
11137yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11138{
11139 enum yytokentype t;
11140
11141 p->lval = lval;
11142 lval->val = Qundef;
11143 p->yylloc = yylloc;
11144
11145 t = parser_yylex(p);
11146
11147 if (has_delayed_token(p))
11148 dispatch_delayed_token(p, t);
11149 else if (t != END_OF_INPUT)
11150 dispatch_scan_event(p, t);
11151
11152 return t;
11153}
11154
11155#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11156
11157static NODE*
11158node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11159{
11160 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11161
11162 rb_node_init(n, type);
11163 return n;
11164}
11165
11166static NODE *
11167nd_set_loc(NODE *nd, const YYLTYPE *loc)
11168{
11169 nd->nd_loc = *loc;
11170 nd_set_line(nd, loc->beg_pos.lineno);
11171 return nd;
11172}
11173
11174static NODE*
11175node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11176{
11177 NODE *n = node_new_internal(p, type, size, alignment);
11178
11179 nd_set_loc(n, loc);
11180 nd_set_node_id(n, parser_get_node_id(p));
11181 return n;
11182}
11183
11184#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11185
11186#ifndef RIPPER
11187
11188static rb_node_scope_t *
11189rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11190{
11191 rb_ast_id_table_t *nd_tbl;
11192 nd_tbl = local_tbl(p);
11193 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11194 n->nd_tbl = nd_tbl;
11195 n->nd_body = nd_body;
11196 n->nd_args = nd_args;
11197
11198 return n;
11199}
11200
11201static rb_node_scope_t *
11202rb_node_scope_new2(struct parser_params *p, rb_ast_id_table_t *nd_tbl, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11203{
11204 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11205 n->nd_tbl = nd_tbl;
11206 n->nd_body = nd_body;
11207 n->nd_args = nd_args;
11208
11209 return n;
11210}
11211
11212static rb_node_defn_t *
11213rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11214{
11215 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11216 n->nd_mid = nd_mid;
11217 n->nd_defn = nd_defn;
11218
11219 return n;
11220}
11221
11222static rb_node_defs_t *
11223rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11224{
11225 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11226 n->nd_recv = nd_recv;
11227 n->nd_mid = nd_mid;
11228 n->nd_defn = nd_defn;
11229
11230 return n;
11231}
11232
11233static rb_node_block_t *
11234rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11235{
11236 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11237 n->nd_head = nd_head;
11238 n->nd_end = (NODE *)n;
11239 n->nd_next = 0;
11240
11241 return n;
11242}
11243
11244static rb_node_for_t *
11245rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc)
11246{
11247 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11248 n->nd_body = nd_body;
11249 n->nd_iter = nd_iter;
11250
11251 return n;
11252}
11253
11254static rb_node_for_masgn_t *
11255rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11256{
11257 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11258 n->nd_var = nd_var;
11259
11260 return n;
11261}
11262
11263static rb_node_retry_t *
11264rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11265{
11266 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11267
11268 return n;
11269}
11270
11271static rb_node_begin_t *
11272rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11273{
11274 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11275 n->nd_body = nd_body;
11276
11277 return n;
11278}
11279
11280static rb_node_rescue_t *
11281rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11282{
11283 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11284 n->nd_head = nd_head;
11285 n->nd_resq = nd_resq;
11286 n->nd_else = nd_else;
11287
11288 return n;
11289}
11290
11291static rb_node_resbody_t *
11292rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_body, NODE *nd_head, const YYLTYPE *loc)
11293{
11294 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11295 n->nd_head = nd_head;
11296 n->nd_body = nd_body;
11297 n->nd_args = nd_args;
11298
11299 return n;
11300}
11301
11302static rb_node_ensure_t *
11303rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11304{
11305 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11306 n->nd_head = nd_head;
11307 n->nd_resq = 0;
11308 n->nd_ensr = nd_ensr;
11309
11310 return n;
11311}
11312
11313static rb_node_and_t *
11314rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
11315{
11316 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11317 n->nd_1st = nd_1st;
11318 n->nd_2nd = nd_2nd;
11319
11320 return n;
11321}
11322
11323static rb_node_or_t *
11324rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
11325{
11326 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11327 n->nd_1st = nd_1st;
11328 n->nd_2nd = nd_2nd;
11329
11330 return n;
11331}
11332
11333static rb_node_return_t *
11334rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
11335{
11336 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11337 n->nd_stts = nd_stts;
11338 return n;
11339}
11340
11341static rb_node_yield_t *
11342rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11343{
11344 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11345 n->nd_head = nd_head;
11346
11347 return n;
11348}
11349
11350static rb_node_if_t *
11351rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11352{
11353 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11354 n->nd_cond = nd_cond;
11355 n->nd_body = nd_body;
11356 n->nd_else = nd_else;
11357
11358 return n;
11359}
11360
11361static rb_node_unless_t *
11362rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11363{
11364 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11365 n->nd_cond = nd_cond;
11366 n->nd_body = nd_body;
11367 n->nd_else = nd_else;
11368
11369 return n;
11370}
11371
11372static rb_node_class_t *
11373rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc)
11374{
11375 /* Keep the order of node creation */
11376 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11377 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11378 n->nd_cpath = nd_cpath;
11379 n->nd_body = scope;
11380 n->nd_super = nd_super;
11381
11382 return n;
11383}
11384
11385static rb_node_sclass_t *
11386rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc)
11387{
11388 /* Keep the order of node creation */
11389 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11390 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11391 n->nd_recv = nd_recv;
11392 n->nd_body = scope;
11393
11394 return n;
11395}
11396
11397static rb_node_module_t *
11398rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc)
11399{
11400 /* Keep the order of node creation */
11401 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11402 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11403 n->nd_cpath = nd_cpath;
11404 n->nd_body = scope;
11405
11406 return n;
11407}
11408
11409static rb_node_iter_t *
11410rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11411{
11412 /* Keep the order of node creation */
11413 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11414 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11415 n->nd_body = scope;
11416 n->nd_iter = 0;
11417
11418 return n;
11419}
11420
11421static rb_node_lambda_t *
11422rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11423{
11424 /* Keep the order of node creation */
11425 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11426 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, loc);
11427 n->nd_body = scope;
11428
11429 return n;
11430}
11431
11432static rb_node_case_t *
11433rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
11434{
11435 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11436 n->nd_head = nd_head;
11437 n->nd_body = nd_body;
11438
11439 return n;
11440}
11441
11442static rb_node_case2_t *
11443rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11444{
11445 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11446 n->nd_head = 0;
11447 n->nd_body = nd_body;
11448
11449 return n;
11450}
11451
11452static rb_node_case3_t *
11453rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
11454{
11455 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11456 n->nd_head = nd_head;
11457 n->nd_body = nd_body;
11458
11459 return n;
11460}
11461
11462static rb_node_when_t *
11463rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11464{
11465 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11466 n->nd_head = nd_head;
11467 n->nd_body = nd_body;
11468 n->nd_next = nd_next;
11469
11470 return n;
11471}
11472
11473static rb_node_in_t *
11474rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11475{
11476 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11477 n->nd_head = nd_head;
11478 n->nd_body = nd_body;
11479 n->nd_next = nd_next;
11480
11481 return n;
11482}
11483
11484static rb_node_while_t *
11485rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc)
11486{
11487 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11488 n->nd_cond = nd_cond;
11489 n->nd_body = nd_body;
11490 n->nd_state = nd_state;
11491
11492 return n;
11493}
11494
11495static rb_node_until_t *
11496rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc)
11497{
11498 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11499 n->nd_cond = nd_cond;
11500 n->nd_body = nd_body;
11501 n->nd_state = nd_state;
11502
11503 return n;
11504}
11505
11506static rb_node_colon2_t *
11507rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc)
11508{
11509 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11510 n->nd_head = nd_head;
11511 n->nd_mid = nd_mid;
11512
11513 return n;
11514}
11515
11516static rb_node_colon3_t *
11517rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11518{
11519 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11520 n->nd_mid = nd_mid;
11521
11522 return n;
11523}
11524
11525static rb_node_dot2_t *
11526rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11527{
11528 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11529 n->nd_beg = nd_beg;
11530 n->nd_end = nd_end;
11531
11532 return n;
11533}
11534
11535static rb_node_dot3_t *
11536rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11537{
11538 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11539 n->nd_beg = nd_beg;
11540 n->nd_end = nd_end;
11541
11542 return n;
11543}
11544
11545static rb_node_self_t *
11546rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11547{
11548 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11549 n->nd_state = 1;
11550
11551 return n;
11552}
11553
11554static rb_node_nil_t *
11555rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11556{
11557 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11558
11559 return n;
11560}
11561
11562static rb_node_true_t *
11563rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11564{
11565 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11566
11567 return n;
11568}
11569
11570static rb_node_false_t *
11571rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11572{
11573 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11574
11575 return n;
11576}
11577
11578static rb_node_super_t *
11579rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc)
11580{
11581 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11582 n->nd_args = nd_args;
11583
11584 return n;
11585}
11586
11587static rb_node_zsuper_t *
11588rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11589{
11590 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11591
11592 return n;
11593}
11594
11595static rb_node_match2_t *
11596rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11597{
11598 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11599 n->nd_recv = nd_recv;
11600 n->nd_value = nd_value;
11601 n->nd_args = 0;
11602
11603 return n;
11604}
11605
11606static rb_node_match3_t *
11607rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11608{
11609 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11610 n->nd_recv = nd_recv;
11611 n->nd_value = nd_value;
11612
11613 return n;
11614}
11615
11616/* TODO: Use union for NODE_LIST2 */
11617static rb_node_list_t *
11618rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11619{
11620 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11621 n->nd_head = nd_head;
11622 n->as.nd_alen = 1;
11623 n->nd_next = 0;
11624
11625 return n;
11626}
11627
11628static rb_node_list_t *
11629rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11630{
11631 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11632 n->nd_head = nd_head;
11633 n->as.nd_alen = nd_alen;
11634 n->nd_next = nd_next;
11635
11636 return n;
11637}
11638
11639static rb_node_zlist_t *
11640rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11641{
11642 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11643
11644 return n;
11645}
11646
11647static rb_node_hash_t *
11648rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11649{
11650 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11651 n->nd_head = nd_head;
11652 n->nd_brace = 0;
11653
11654 return n;
11655}
11656
11657static rb_node_masgn_t *
11658rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11659{
11660 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11661 n->nd_head = nd_head;
11662 n->nd_value = 0;
11663 n->nd_args = nd_args;
11664
11665 return n;
11666}
11667
11668static rb_node_gasgn_t *
11669rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11670{
11671 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11672 n->nd_vid = nd_vid;
11673 n->nd_value = nd_value;
11674
11675 return n;
11676}
11677
11678static rb_node_lasgn_t *
11679rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11680{
11681 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11682 n->nd_vid = nd_vid;
11683 n->nd_value = nd_value;
11684
11685 return n;
11686}
11687
11688static rb_node_dasgn_t *
11689rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11690{
11691 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11692 n->nd_vid = nd_vid;
11693 n->nd_value = nd_value;
11694
11695 return n;
11696}
11697
11698static rb_node_iasgn_t *
11699rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11700{
11701 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11702 n->nd_vid = nd_vid;
11703 n->nd_value = nd_value;
11704
11705 return n;
11706}
11707
11708static rb_node_cvasgn_t *
11709rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11710{
11711 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11712 n->nd_vid = nd_vid;
11713 n->nd_value = nd_value;
11714
11715 return n;
11716}
11717
11718static rb_node_op_asgn1_t *
11719rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc)
11720{
11721 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11722 n->nd_recv = nd_recv;
11723 n->nd_mid = nd_mid;
11724 n->nd_index = index;
11725 n->nd_rvalue = rvalue;
11726
11727 return n;
11728}
11729
11730static rb_node_op_asgn2_t *
11731rb_node_op_asgn2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, ID nd_vid, ID nd_mid, bool nd_aid, const YYLTYPE *loc)
11732{
11733 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11734 n->nd_recv = nd_recv;
11735 n->nd_value = nd_value;
11736 n->nd_vid = nd_vid;
11737 n->nd_mid = nd_mid;
11738 n->nd_aid = nd_aid;
11739
11740 return n;
11741}
11742
11743static rb_node_op_asgn_or_t *
11744rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11745{
11746 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11747 n->nd_head = nd_head;
11748 n->nd_value = nd_value;
11749
11750 return n;
11751}
11752
11753static rb_node_op_asgn_and_t *
11754rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11755{
11756 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11757 n->nd_head = nd_head;
11758 n->nd_value = nd_value;
11759
11760 return n;
11761}
11762
11763static rb_node_gvar_t *
11764rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11765{
11766 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11767 n->nd_vid = nd_vid;
11768
11769 return n;
11770}
11771
11772static rb_node_lvar_t *
11773rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11774{
11775 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11776 n->nd_vid = nd_vid;
11777
11778 return n;
11779}
11780
11781static rb_node_dvar_t *
11782rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11783{
11784 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11785 n->nd_vid = nd_vid;
11786
11787 return n;
11788}
11789
11790static rb_node_ivar_t *
11791rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11792{
11793 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11794 n->nd_vid = nd_vid;
11795
11796 return n;
11797}
11798
11799static rb_node_const_t *
11800rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11801{
11802 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11803 n->nd_vid = nd_vid;
11804
11805 return n;
11806}
11807
11808static rb_node_cvar_t *
11809rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11810{
11811 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11812 n->nd_vid = nd_vid;
11813
11814 return n;
11815}
11816
11817static rb_node_nth_ref_t *
11818rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11819{
11820 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11821 n->nd_nth = nd_nth;
11822
11823 return n;
11824}
11825
11826static rb_node_back_ref_t *
11827rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11828{
11829 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11830 n->nd_nth = nd_nth;
11831
11832 return n;
11833}
11834
11835static rb_node_lit_t *
11836rb_node_lit_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11837{
11838 rb_node_lit_t *n = NODE_NEWNODE(NODE_LIT, rb_node_lit_t, loc);
11839 n->nd_lit = nd_lit;
11840
11841 return n;
11842}
11843
11844static rb_node_str_t *
11845rb_node_str_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11846{
11847 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
11848 n->nd_lit = nd_lit;
11849
11850 return n;
11851}
11852
11853/* TODO; Use union for NODE_DSTR2 */
11854static rb_node_dstr_t *
11855rb_node_dstr_new0(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11856{
11857 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
11858 n->nd_lit = nd_lit;
11859 n->as.nd_alen = nd_alen;
11860 n->nd_next = (rb_node_list_t *)nd_next;
11861
11862 return n;
11863}
11864
11865static rb_node_dstr_t *
11866rb_node_dstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11867{
11868 return rb_node_dstr_new0(p, nd_lit, 1, 0, loc);
11869}
11870
11871static rb_node_xstr_t *
11872rb_node_xstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11873{
11874 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
11875 n->nd_lit = nd_lit;
11876
11877 return n;
11878}
11879
11880static rb_node_dxstr_t *
11881rb_node_dxstr_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11882{
11883 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
11884 n->nd_lit = nd_lit;
11885 n->nd_alen = nd_alen;
11886 n->nd_next = (rb_node_list_t *)nd_next;
11887
11888 return n;
11889}
11890
11891static rb_node_dsym_t *
11892rb_node_dsym_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11893{
11894 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
11895 n->nd_lit = nd_lit;
11896 n->nd_alen = nd_alen;
11897 n->nd_next = (rb_node_list_t *)nd_next;
11898
11899 return n;
11900}
11901
11902static rb_node_evstr_t *
11903rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11904{
11905 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
11906 n->nd_body = nd_body;
11907
11908 return n;
11909}
11910
11911static rb_node_call_t *
11912rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11913{
11914 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
11915 n->nd_recv = nd_recv;
11916 n->nd_mid = nd_mid;
11917 n->nd_args = nd_args;
11918
11919 return n;
11920}
11921
11922static rb_node_opcall_t *
11923rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11924{
11925 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
11926 n->nd_recv = nd_recv;
11927 n->nd_mid = nd_mid;
11928 n->nd_args = nd_args;
11929
11930 return n;
11931}
11932
11933static rb_node_fcall_t *
11934rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11935{
11936 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
11937 n->nd_mid = nd_mid;
11938 n->nd_args = nd_args;
11939
11940 return n;
11941}
11942
11943static rb_node_qcall_t *
11944rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11945{
11946 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
11947 n->nd_recv = nd_recv;
11948 n->nd_mid = nd_mid;
11949 n->nd_args = nd_args;
11950
11951 return n;
11952}
11953
11954static rb_node_vcall_t *
11955rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11956{
11957 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
11958 n->nd_mid = nd_mid;
11959
11960 return n;
11961}
11962
11963static rb_node_once_t *
11964rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11965{
11966 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
11967 n->nd_body = nd_body;
11968
11969 return n;
11970}
11971
11972static rb_node_args_t *
11973rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
11974{
11975 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
11976 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
11977
11978 return n;
11979}
11980
11981static rb_node_args_aux_t *
11982rb_node_args_aux_new(struct parser_params *p, ID nd_pid, long nd_plen, const YYLTYPE *loc)
11983{
11984 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
11985 n->nd_pid = nd_pid;
11986 n->nd_plen = nd_plen;
11987 n->nd_next = 0;
11988
11989 return n;
11990}
11991
11992static rb_node_opt_arg_t *
11993rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11994{
11995 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
11996 n->nd_body = nd_body;
11997 n->nd_next = 0;
11998
11999 return n;
12000}
12001
12002static rb_node_kw_arg_t *
12003rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12004{
12005 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12006 n->nd_body = nd_body;
12007 n->nd_next = 0;
12008
12009 return n;
12010}
12011
12012static rb_node_postarg_t *
12013rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12014{
12015 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12016 n->nd_1st = nd_1st;
12017 n->nd_2nd = nd_2nd;
12018
12019 return n;
12020}
12021
12022static rb_node_argscat_t *
12023rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12024{
12025 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12026 n->nd_head = nd_head;
12027 n->nd_body = nd_body;
12028
12029 return n;
12030}
12031
12032static rb_node_argspush_t *
12033rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12034{
12035 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12036 n->nd_head = nd_head;
12037 n->nd_body = nd_body;
12038
12039 return n;
12040}
12041
12042static rb_node_splat_t *
12043rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12044{
12045 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12046 n->nd_head = nd_head;
12047
12048 return n;
12049}
12050
12051static rb_node_block_pass_t *
12052rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12053{
12054 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12055 n->nd_head = 0;
12056 n->nd_body = nd_body;
12057
12058 return n;
12059}
12060
12061static rb_node_alias_t *
12062rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12063{
12064 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12065 n->nd_1st = nd_1st;
12066 n->nd_2nd = nd_2nd;
12067
12068 return n;
12069}
12070
12071static rb_node_valias_t *
12072rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc)
12073{
12074 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12075 n->nd_alias = nd_alias;
12076 n->nd_orig = nd_orig;
12077
12078 return n;
12079}
12080
12081static rb_node_undef_t *
12082rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12083{
12084 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12085 n->nd_undef = nd_undef;
12086
12087 return n;
12088}
12089
12090static rb_node_errinfo_t *
12091rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12092{
12093 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12094
12095 return n;
12096}
12097
12098static rb_node_defined_t *
12099rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12100{
12101 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12102 n->nd_head = nd_head;
12103
12104 return n;
12105}
12106
12107static rb_node_postexe_t *
12108rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12109{
12110 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12111 n->nd_body = nd_body;
12112
12113 return n;
12114}
12115
12116static rb_node_attrasgn_t *
12117rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12118{
12119 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12120 n->nd_recv = nd_recv;
12121 n->nd_mid = nd_mid;
12122 n->nd_args = nd_args;
12123
12124 return n;
12125}
12126
12127static rb_node_aryptn_t *
12128rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12129{
12130 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12131 n->nd_pconst = 0;
12132 n->pre_args = pre_args;
12133 n->rest_arg = rest_arg;
12134 n->post_args = post_args;
12135
12136 return n;
12137}
12138
12139static rb_node_hshptn_t *
12140rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12141{
12142 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12143 n->nd_pconst = nd_pconst;
12144 n->nd_pkwargs = nd_pkwargs;
12145 n->nd_pkwrestarg = nd_pkwrestarg;
12146
12147 return n;
12148}
12149
12150static rb_node_fndptn_t *
12151rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12152{
12153 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12154 n->nd_pconst = 0;
12155 n->pre_rest_arg = pre_rest_arg;
12156 n->args = args;
12157 n->post_rest_arg = post_rest_arg;
12158
12159 return n;
12160}
12161
12162static rb_node_cdecl_t *
12163rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, const YYLTYPE *loc)
12164{
12165 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12166 n->nd_vid = nd_vid;
12167 n->nd_value = nd_value;
12168 n->nd_else = nd_else;
12169
12170 return n;
12171}
12172
12173static rb_node_op_cdecl_t *
12174rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, const YYLTYPE *loc)
12175{
12176 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12177 n->nd_head = nd_head;
12178 n->nd_value = nd_value;
12179 n->nd_aid = nd_aid;
12180
12181 return n;
12182}
12183
12184static rb_node_error_t *
12185rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12186{
12187 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12188
12189 return n;
12190}
12191
12192#else
12193
12194static rb_node_ripper_t *
12195rb_node_ripper_new(struct parser_params *p, ID nd_vid, VALUE nd_rval, VALUE nd_cval, const YYLTYPE *loc)
12196{
12197 rb_node_ripper_t *n = NODE_NEWNODE(NODE_RIPPER, rb_node_ripper_t, loc);
12198 n->nd_vid = nd_vid;
12199 n->nd_rval = nd_rval;
12200 n->nd_cval = nd_cval;
12201
12202 return n;
12203}
12204
12205static rb_node_ripper_values_t *
12206rb_node_ripper_values_new(struct parser_params *p, VALUE nd_val1, VALUE nd_val2, VALUE nd_val3, const YYLTYPE *loc)
12207{
12208 rb_node_ripper_values_t *n = NODE_NEWNODE(NODE_RIPPER_VALUES, rb_node_ripper_values_t, loc);
12209 n->nd_val1 = nd_val1;
12210 n->nd_val2 = nd_val2;
12211 n->nd_val3 = nd_val3;
12212
12213 return n;
12214}
12215
12216#endif
12217
12218static rb_node_break_t *
12219rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
12220{
12221 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12222 n->nd_stts = nd_stts;
12223 n->nd_chain = 0;
12224
12225 return n;
12226}
12227
12228static rb_node_next_t *
12229rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
12230{
12231 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12232 n->nd_stts = nd_stts;
12233 n->nd_chain = 0;
12234
12235 return n;
12236}
12237
12238static rb_node_redo_t *
12239rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc)
12240{
12241 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12242 n->nd_chain = 0;
12243
12244 return n;
12245}
12246
12247static rb_node_def_temp_t *
12248rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12249{
12250 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12251 n->save.cur_arg = p->cur_arg;
12252 n->save.numparam_save = 0;
12253 n->save.max_numparam = 0;
12254 n->save.ctxt = p->ctxt;
12255#ifdef RIPPER
12256 n->nd_recv = Qnil;
12257 n->nd_mid = Qnil;
12258 n->dot_or_colon = Qnil;
12259#else
12260 n->nd_def = 0;
12261 n->nd_mid = 0;
12262#endif
12263
12264 return n;
12265}
12266
12267static rb_node_def_temp_t *
12268def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12269{
12270 n->save.numparam_save = numparam_push(p);
12271 n->save.max_numparam = p->max_numparam;
12272 return n;
12273}
12274
12275#ifndef RIPPER
12276static enum node_type
12277nodetype(NODE *node) /* for debug */
12278{
12279 return (enum node_type)nd_type(node);
12280}
12281
12282static int
12283nodeline(NODE *node)
12284{
12285 return nd_line(node);
12286}
12287
12288static NODE*
12289newline_node(NODE *node)
12290{
12291 if (node) {
12292 node = remove_begin(node);
12293 nd_set_fl_newline(node);
12294 }
12295 return node;
12296}
12297
12298static void
12299fixpos(NODE *node, NODE *orig)
12300{
12301 if (!node) return;
12302 if (!orig) return;
12303 nd_set_line(node, nd_line(orig));
12304}
12305
12306static void
12307parser_warning(struct parser_params *p, NODE *node, const char *mesg)
12308{
12309 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
12310}
12311
12312static void
12313parser_warn(struct parser_params *p, NODE *node, const char *mesg)
12314{
12315 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
12316}
12317
12318static NODE*
12319block_append(struct parser_params *p, NODE *head, NODE *tail)
12320{
12321 NODE *end, *h = head, *nd;
12322
12323 if (tail == 0) return head;
12324
12325 if (h == 0) return tail;
12326 switch (nd_type(h)) {
12327 default:
12328 h = end = NEW_BLOCK(head, &head->nd_loc);
12329 head = end;
12330 break;
12331 case NODE_BLOCK:
12332 end = RNODE_BLOCK(h)->nd_end;
12333 break;
12334 }
12335
12336 nd = RNODE_BLOCK(end)->nd_head;
12337 switch (nd_type(nd)) {
12338 case NODE_RETURN:
12339 case NODE_BREAK:
12340 case NODE_NEXT:
12341 case NODE_REDO:
12342 case NODE_RETRY:
12343 if (RTEST(ruby_verbose)) {
12344 parser_warning(p, tail, "statement not reached");
12345 }
12346 break;
12347
12348 default:
12349 break;
12350 }
12351
12352 if (!nd_type_p(tail, NODE_BLOCK)) {
12353 tail = NEW_BLOCK(tail, &tail->nd_loc);
12354 }
12355 RNODE_BLOCK(end)->nd_next = tail;
12356 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12357 nd_set_last_loc(head, nd_last_loc(tail));
12358 return head;
12359}
12360
12361/* append item to the list */
12362static NODE*
12363list_append(struct parser_params *p, NODE *list, NODE *item)
12364{
12365 NODE *last;
12366
12367 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12368 if (RNODE_LIST(list)->nd_next) {
12369 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12370 }
12371 else {
12372 last = list;
12373 }
12374
12375 RNODE_LIST(list)->as.nd_alen += 1;
12376 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12377 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12378
12379 nd_set_last_loc(list, nd_last_loc(item));
12380
12381 return list;
12382}
12383
12384/* concat two lists */
12385static NODE*
12386list_concat(NODE *head, NODE *tail)
12387{
12388 NODE *last;
12389
12390 if (RNODE_LIST(head)->nd_next) {
12391 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12392 }
12393 else {
12394 last = head;
12395 }
12396
12397 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12398 RNODE_LIST(last)->nd_next = tail;
12399 if (RNODE_LIST(tail)->nd_next) {
12400 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12401 }
12402 else {
12403 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12404 }
12405
12406 nd_set_last_loc(head, nd_last_loc(tail));
12407
12408 return head;
12409}
12410
12411static int
12412literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
12413{
12414 if (NIL_P(tail)) return 1;
12415 if (!rb_enc_compatible(head, tail)) {
12416 compile_error(p, "string literal encodings differ (%s / %s)",
12417 rb_enc_name(rb_enc_get(head)),
12418 rb_enc_name(rb_enc_get(tail)));
12419 rb_str_resize(head, 0);
12420 rb_str_resize(tail, 0);
12421 return 0;
12422 }
12423 rb_str_buf_append(head, tail);
12424 return 1;
12425}
12426
12427static VALUE
12428string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12429{
12430 if (htype != NODE_DSTR) return Qfalse;
12431 if (RNODE_DSTR(head)->nd_next) {
12432 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12433 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
12434 }
12435 const VALUE lit = RNODE_DSTR(head)->nd_lit;
12436 ASSUME(lit != Qfalse);
12437 return lit;
12438}
12439
12440/* concat two string literals */
12441static NODE *
12442literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12443{
12444 enum node_type htype;
12445 VALUE lit;
12446
12447 if (!head) return tail;
12448 if (!tail) return head;
12449
12450 htype = nd_type(head);
12451 if (htype == NODE_EVSTR) {
12452 head = new_dstr(p, head, loc);
12453 htype = NODE_DSTR;
12454 }
12455 if (p->heredoc_indent > 0) {
12456 switch (htype) {
12457 case NODE_STR:
12458 head = str2dstr(p, head);
12459 case NODE_DSTR:
12460 return list_append(p, head, tail);
12461 default:
12462 break;
12463 }
12464 }
12465 switch (nd_type(tail)) {
12466 case NODE_STR:
12467 if ((lit = string_literal_head(p, htype, head)) != Qfalse) {
12468 htype = NODE_STR;
12469 }
12470 else {
12471 lit = RNODE_DSTR(head)->nd_lit;
12472 }
12473 if (htype == NODE_STR) {
12474 if (!literal_concat0(p, lit, RNODE_STR(tail)->nd_lit)) {
12475 error:
12476 rb_discard_node(p, head);
12477 rb_discard_node(p, tail);
12478 return 0;
12479 }
12480 rb_discard_node(p, tail);
12481 }
12482 else {
12483 list_append(p, head, tail);
12484 }
12485 break;
12486
12487 case NODE_DSTR:
12488 if (htype == NODE_STR) {
12489 if (!literal_concat0(p, RNODE_STR(head)->nd_lit, RNODE_DSTR(tail)->nd_lit))
12490 goto error;
12491 RNODE_DSTR(tail)->nd_lit = RNODE_STR(head)->nd_lit;
12492 rb_discard_node(p, head);
12493 head = tail;
12494 }
12495 else if (NIL_P(RNODE_DSTR(tail)->nd_lit)) {
12496 append:
12497 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12498 if (!RNODE_DSTR(head)->nd_next) {
12499 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12500 }
12501 else if (RNODE_DSTR(tail)->nd_next) {
12502 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12503 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12504 }
12505 rb_discard_node(p, tail);
12506 }
12507 else if ((lit = string_literal_head(p, htype, head)) != Qfalse) {
12508 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->nd_lit))
12509 goto error;
12510 RNODE_DSTR(tail)->nd_lit = Qnil;
12511 goto append;
12512 }
12513 else {
12514 list_concat(head, NEW_LIST2(NEW_STR(RNODE_DSTR(tail)->nd_lit, loc), RNODE_DSTR(tail)->as.nd_alen, (NODE *)RNODE_DSTR(tail)->nd_next, loc));
12515 }
12516 break;
12517
12518 case NODE_EVSTR:
12519 if (htype == NODE_STR) {
12520 head = str2dstr(p, head);
12521 RNODE_DSTR(head)->as.nd_alen = 1;
12522 }
12523 list_append(p, head, tail);
12524 break;
12525 }
12526 return head;
12527}
12528
12529static void
12530nd_copy_flag(NODE *new_node, NODE *old_node)
12531{
12532 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12533 nd_set_line(new_node, nd_line(old_node));
12534 new_node->nd_loc = old_node->nd_loc;
12535 new_node->node_id = old_node->node_id;
12536}
12537
12538static NODE *
12539str2dstr(struct parser_params *p, NODE *node)
12540{
12541 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12542 nd_copy_flag(new_node, node);
12543 RNODE_DSTR(new_node)->nd_lit = RNODE_STR(node)->nd_lit;
12544 RNODE_DSTR(new_node)->as.nd_alen = 0;
12545 RNODE_DSTR(new_node)->nd_next = 0;
12546 RNODE_STR(node)->nd_lit = 0;
12547
12548 return new_node;
12549}
12550
12551static NODE *
12552evstr2dstr(struct parser_params *p, NODE *node)
12553{
12554 if (nd_type_p(node, NODE_EVSTR)) {
12555 node = new_dstr(p, node, &node->nd_loc);
12556 }
12557 return node;
12558}
12559
12560static NODE *
12561new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12562{
12563 NODE *head = node;
12564
12565 if (node) {
12566 switch (nd_type(node)) {
12567 case NODE_STR:
12568 return str2dstr(p, node);
12569 case NODE_DSTR:
12570 break;
12571 case NODE_EVSTR:
12572 return node;
12573 }
12574 }
12575 return NEW_EVSTR(head, loc);
12576}
12577
12578static NODE *
12579new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12580{
12581 VALUE lit = STR_NEW0();
12582 NODE *dstr = NEW_DSTR(lit, loc);
12583 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12584 return list_append(p, dstr, node);
12585}
12586
12587static NODE *
12588call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12589 const YYLTYPE *op_loc, const YYLTYPE *loc)
12590{
12591 NODE *expr;
12592 value_expr(recv);
12593 value_expr(arg1);
12594 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12595 nd_set_line(expr, op_loc->beg_pos.lineno);
12596 return expr;
12597}
12598
12599static NODE *
12600call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12601{
12602 NODE *opcall;
12603 value_expr(recv);
12604 opcall = NEW_OPCALL(recv, id, 0, loc);
12605 nd_set_line(opcall, op_loc->beg_pos.lineno);
12606 return opcall;
12607}
12608
12609static NODE *
12610new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12611{
12612 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12613 nd_set_line(qcall, op_loc->beg_pos.lineno);
12614 return qcall;
12615}
12616
12617static NODE*
12618new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12619{
12620 NODE *ret;
12621 if (block) block_dup_check(p, args, block);
12622 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12623 if (block) ret = method_add_block(p, ret, block, loc);
12624 fixpos(ret, recv);
12625 return ret;
12626}
12627
12628#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12629
12630static NODE*
12631last_expr_once_body(NODE *node)
12632{
12633 if (!node) return 0;
12634 return nd_once_body(node);
12635}
12636
12637static NODE*
12638match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12639{
12640 NODE *n;
12641 int line = op_loc->beg_pos.lineno;
12642
12643 value_expr(node1);
12644 value_expr(node2);
12645
12646 if ((n = last_expr_once_body(node1)) != 0) {
12647 switch (nd_type(n)) {
12648 case NODE_DREGX:
12649 {
12650 NODE *match = NEW_MATCH2(node1, node2, loc);
12651 nd_set_line(match, line);
12652 return match;
12653 }
12654
12655 case NODE_LIT:
12656 if (RB_TYPE_P(RNODE_LIT(n)->nd_lit, T_REGEXP)) {
12657 const VALUE lit = RNODE_LIT(n)->nd_lit;
12658 NODE *match = NEW_MATCH2(node1, node2, loc);
12659 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc);
12660 nd_set_line(match, line);
12661 return match;
12662 }
12663 }
12664 }
12665
12666 if ((n = last_expr_once_body(node2)) != 0) {
12667 NODE *match3;
12668
12669 switch (nd_type(n)) {
12670 case NODE_LIT:
12671 if (!RB_TYPE_P(RNODE_LIT(n)->nd_lit, T_REGEXP)) break;
12672 /* fallthru */
12673 case NODE_DREGX:
12674 match3 = NEW_MATCH3(node2, node1, loc);
12675 return match3;
12676 }
12677 }
12678
12679 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12680 nd_set_line(n, line);
12681 return n;
12682}
12683
12684# if WARN_PAST_SCOPE
12685static int
12686past_dvar_p(struct parser_params *p, ID id)
12687{
12688 struct vtable *past = p->lvtbl->past;
12689 while (past) {
12690 if (vtable_included(past, id)) return 1;
12691 past = past->prev;
12692 }
12693 return 0;
12694}
12695# endif
12696
12697static int
12698numparam_nested_p(struct parser_params *p)
12699{
12700 struct local_vars *local = p->lvtbl;
12701 NODE *outer = local->numparam.outer;
12702 NODE *inner = local->numparam.inner;
12703 if (outer || inner) {
12704 NODE *used = outer ? outer : inner;
12705 compile_error(p, "numbered parameter is already used in\n"
12706 "%s:%d: %s block here",
12707 p->ruby_sourcefile, nd_line(used),
12708 outer ? "outer" : "inner");
12709 parser_show_error_line(p, &used->nd_loc);
12710 return 1;
12711 }
12712 return 0;
12713}
12714
12715static NODE*
12716gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12717{
12718 ID *vidp = NULL;
12719 NODE *node;
12720 switch (id) {
12721 case keyword_self:
12722 return NEW_SELF(loc);
12723 case keyword_nil:
12724 return NEW_NIL(loc);
12725 case keyword_true:
12726 return NEW_TRUE(loc);
12727 case keyword_false:
12728 return NEW_FALSE(loc);
12729 case keyword__FILE__:
12730 {
12731 VALUE file = p->ruby_sourcefile_string;
12732 if (NIL_P(file))
12733 file = rb_str_new(0, 0);
12734 else
12735 file = rb_str_dup(file);
12736 node = NEW_STR(file, loc);
12737 RB_OBJ_WRITTEN(p->ast, Qnil, file);
12738 }
12739 return node;
12740 case keyword__LINE__:
12741 return NEW_LIT(INT2FIX(loc->beg_pos.lineno), loc);
12742 case keyword__ENCODING__:
12743 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
12744 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit);
12745 return node;
12746
12747 }
12748 switch (id_type(id)) {
12749 case ID_LOCAL:
12750 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
12751 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
12752 if (id == p->cur_arg) {
12753 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
12754 return 0;
12755 }
12756 if (vidp) *vidp |= LVAR_USED;
12757 node = NEW_DVAR(id, loc);
12758 return node;
12759 }
12760 if (local_id_ref(p, id, &vidp)) {
12761 if (id == p->cur_arg) {
12762 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
12763 return 0;
12764 }
12765 if (vidp) *vidp |= LVAR_USED;
12766 node = NEW_LVAR(id, loc);
12767 return node;
12768 }
12769 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
12770 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
12771 if (numparam_nested_p(p)) return 0;
12772 node = NEW_DVAR(id, loc);
12773 struct local_vars *local = p->lvtbl;
12774 if (!local->numparam.current) local->numparam.current = node;
12775 return node;
12776 }
12777# if WARN_PAST_SCOPE
12778 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
12779 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
12780 }
12781# endif
12782 /* method call without arguments */
12783 if (dyna_in_block(p) && id == rb_intern("it")
12784 && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))
12785 && p->max_numparam != ORDINAL_PARAM) {
12786 rb_warn0("`it` calls without arguments will refer to the first block param in Ruby 3.4; use it() or self.it");
12787 }
12788 return NEW_VCALL(id, loc);
12789 case ID_GLOBAL:
12790 return NEW_GVAR(id, loc);
12791 case ID_INSTANCE:
12792 return NEW_IVAR(id, loc);
12793 case ID_CONST:
12794 return NEW_CONST(id, loc);
12795 case ID_CLASS:
12796 return NEW_CVAR(id, loc);
12797 }
12798 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
12799 return 0;
12800}
12801
12802static rb_node_opt_arg_t *
12803opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
12804{
12805 rb_node_opt_arg_t *opts = opt_list;
12806 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
12807
12808 while (opts->nd_next) {
12809 opts = opts->nd_next;
12810 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
12811 }
12812 opts->nd_next = opt;
12813
12814 return opt_list;
12815}
12816
12817static rb_node_kw_arg_t *
12818kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
12819{
12820 if (kwlist) {
12821 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
12822 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
12823 }
12824 return kwlist;
12825}
12826
12827static NODE *
12828new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
12829{
12830 NODE *n = expr;
12831 while (n) {
12832 if (nd_type_p(n, NODE_BEGIN)) {
12833 n = RNODE_BEGIN(n)->nd_body;
12834 }
12835 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
12836 n = RNODE_BLOCK(n)->nd_head;
12837 }
12838 else {
12839 break;
12840 }
12841 }
12842 return NEW_DEFINED(n, loc);
12843}
12844
12845static NODE*
12846symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
12847{
12848 enum node_type type = nd_type(symbol);
12849 switch (type) {
12850 case NODE_DSTR:
12851 nd_set_type(symbol, NODE_DSYM);
12852 break;
12853 case NODE_STR:
12854 nd_set_type(symbol, NODE_LIT);
12855 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(symbol)->nd_lit = rb_str_intern(RNODE_LIT(symbol)->nd_lit));
12856 break;
12857 default:
12858 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
12859 }
12860 return list_append(p, symbols, symbol);
12861}
12862
12863static NODE *
12864new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
12865{
12866 struct RNode_LIST *list;
12867 NODE *prev;
12868 VALUE lit;
12869
12870 if (!node) {
12871 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
12872 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit);
12873 return node;
12874 }
12875 switch (nd_type(node)) {
12876 case NODE_STR:
12877 {
12878 VALUE src = RNODE_STR(node)->nd_lit;
12879 nd_set_type(node, NODE_LIT);
12880 nd_set_loc(node, loc);
12881 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit = reg_compile(p, src, options));
12882 }
12883 break;
12884 default:
12885 lit = STR_NEW0();
12886 node = NEW_DSTR0(lit, 1, NEW_LIST(node, loc), loc);
12887 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12888 /* fall through */
12889 case NODE_DSTR:
12890 nd_set_type(node, NODE_DREGX);
12891 nd_set_loc(node, loc);
12892 RNODE_DREGX(node)->nd_cflag = options & RE_OPTION_MASK;
12893 if (!NIL_P(RNODE_DREGX(node)->nd_lit)) reg_fragment_check(p, RNODE_DREGX(node)->nd_lit, options);
12894 for (list = RNODE_DREGX(prev = node)->nd_next; list; list = RNODE_LIST(list->nd_next)) {
12895 NODE *frag = list->nd_head;
12896 enum node_type type = nd_type(frag);
12897 if (type == NODE_STR || (type == NODE_DSTR && !RNODE_DSTR(frag)->nd_next)) {
12898 VALUE tail = RNODE_STR(frag)->nd_lit;
12899 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(RNODE_DREGX(prev)->nd_lit)) {
12900 VALUE lit = prev == node ? RNODE_DREGX(prev)->nd_lit : RNODE_LIT(RNODE_LIST(prev)->nd_head)->nd_lit;
12901 if (!literal_concat0(p, lit, tail)) {
12902 return NEW_NIL(loc); /* dummy node on error */
12903 }
12904 rb_str_resize(tail, 0);
12905 RNODE_LIST(prev)->nd_next = list->nd_next;
12906 rb_discard_node(p, list->nd_head);
12907 rb_discard_node(p, (NODE *)list);
12908 list = RNODE_LIST(prev);
12909 }
12910 else {
12911 prev = (NODE *)list;
12912 }
12913 }
12914 else {
12915 prev = 0;
12916 }
12917 }
12918 if (!RNODE_DREGX(node)->nd_next) {
12919 VALUE src = RNODE_DREGX(node)->nd_lit;
12920 VALUE re = reg_compile(p, src, options);
12921 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_DREGX(node)->nd_lit = re);
12922 }
12923 if (options & RE_OPTION_ONCE) {
12924 node = NEW_ONCE(node, loc);
12925 }
12926 break;
12927 }
12928 return node;
12929}
12930
12931static rb_node_kw_arg_t *
12932new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
12933{
12934 if (!k) return 0;
12935 return NEW_KW_ARG((k), loc);
12936}
12937
12938static NODE *
12939new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12940{
12941 if (!node) {
12942 VALUE lit = STR_NEW0();
12943 NODE *xstr = NEW_XSTR(lit, loc);
12944 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12945 return xstr;
12946 }
12947 switch (nd_type(node)) {
12948 case NODE_STR:
12949 nd_set_type(node, NODE_XSTR);
12950 nd_set_loc(node, loc);
12951 break;
12952 case NODE_DSTR:
12953 nd_set_type(node, NODE_DXSTR);
12954 nd_set_loc(node, loc);
12955 break;
12956 default:
12957 node = NEW_DXSTR(Qnil, 1, NEW_LIST(node, loc), loc);
12958 break;
12959 }
12960 return node;
12961}
12962
12963static void
12964check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
12965{
12966 VALUE lit;
12967
12968 if (!arg || !p->case_labels) return;
12969
12970 lit = rb_node_case_when_optimizable_literal(arg);
12971 if (UNDEF_P(lit)) return;
12972 if (nd_type_p(arg, NODE_STR)) {
12973 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(arg)->nd_lit = lit);
12974 }
12975
12976 if (NIL_P(p->case_labels)) {
12977 p->case_labels = rb_obj_hide(rb_hash_new());
12978 }
12979 else {
12980 VALUE line = rb_hash_lookup(p->case_labels, lit);
12981 if (!NIL_P(line)) {
12982 rb_warning1("duplicated `when' clause with line %d is ignored",
12983 WARN_IVAL(line));
12984 return;
12985 }
12986 }
12987 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
12988}
12989
12990#else /* !RIPPER */
12991static int
12992id_is_var(struct parser_params *p, ID id)
12993{
12994 if (is_notop_id(id)) {
12995 switch (id & ID_SCOPE_MASK) {
12996 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
12997 return 1;
12998 case ID_LOCAL:
12999 if (dyna_in_block(p)) {
13000 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13001 }
13002 if (local_id(p, id)) return 1;
13003 /* method call without arguments */
13004 return 0;
13005 }
13006 }
13007 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13008 return 0;
13009}
13010
13011static VALUE
13012new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
13013{
13014 VALUE src = 0, err = 0;
13015 int options = 0;
13016 if (ripper_is_node_yylval(p, re)) {
13017 src = RNODE_RIPPER(re)->nd_cval;
13018 re = RNODE_RIPPER(re)->nd_rval;
13019 }
13020 if (ripper_is_node_yylval(p, opt)) {
13021 options = (int)RNODE_RIPPER(opt)->nd_vid;
13022 opt = RNODE_RIPPER(opt)->nd_rval;
13023 }
13024 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
13025 compile_error(p, "%"PRIsVALUE, err);
13026 }
13027 return dispatch2(regexp_literal, re, opt);
13028}
13029#endif /* !RIPPER */
13030
13031static inline enum lex_state_e
13032parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13033{
13034 if (p->debug) {
13035 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13036 }
13037 return p->lex.state = ls;
13038}
13039
13040#ifndef RIPPER
13041static const char rb_parser_lex_state_names[][8] = {
13042 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13043 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13044 "LABEL", "LABELED","FITEM",
13045};
13046
13047static VALUE
13048append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13049{
13050 int i, sep = 0;
13051 unsigned int mask = 1;
13052 static const char none[] = "NONE";
13053
13054 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13055 if ((unsigned)state & mask) {
13056 if (sep) {
13057 rb_str_cat(buf, "|", 1);
13058 }
13059 sep = 1;
13060 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13061 }
13062 }
13063 if (!sep) {
13064 rb_str_cat(buf, none, sizeof(none)-1);
13065 }
13066 return buf;
13067}
13068
13069static void
13070flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13071{
13072 VALUE mesg = p->debug_buffer;
13073
13074 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13075 p->debug_buffer = Qnil;
13076 rb_io_puts(1, &mesg, out);
13077 }
13078 if (!NIL_P(str) && RSTRING_LEN(str)) {
13079 rb_io_write(p->debug_output, str);
13080 }
13081}
13082
13083enum lex_state_e
13084rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13085 enum lex_state_e to, int line)
13086{
13087 VALUE mesg;
13088 mesg = rb_str_new_cstr("lex_state: ");
13089 append_lex_state_name(p, from, mesg);
13090 rb_str_cat_cstr(mesg, " -> ");
13091 append_lex_state_name(p, to, mesg);
13092 rb_str_catf(mesg, " at line %d\n", line);
13093 flush_debug_buffer(p, p->debug_output, mesg);
13094 return to;
13095}
13096
13097VALUE
13098rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13099{
13100 return rb_fstring(append_lex_state_name(p, state, rb_str_new(0, 0)));
13101}
13102
13103static void
13104append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13105{
13106 if (stack == 0) {
13107 rb_str_cat_cstr(mesg, "0");
13108 }
13109 else {
13110 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13111 for (; mask && !(stack & mask); mask >>= 1) continue;
13112 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13113 }
13114}
13115
13116void
13117rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13118 const char *name, int line)
13119{
13120 VALUE mesg = rb_sprintf("%s: ", name);
13121 append_bitstack_value(p, stack, mesg);
13122 rb_str_catf(mesg, " at line %d\n", line);
13123 flush_debug_buffer(p, p->debug_output, mesg);
13124}
13125
13126void
13127rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13128{
13129 va_list ap;
13130 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13131
13132 va_start(ap, fmt);
13133 rb_str_vcatf(mesg, fmt, ap);
13134 va_end(ap);
13135 yyerror0(RSTRING_PTR(mesg));
13136 RB_GC_GUARD(mesg);
13137
13138 mesg = rb_str_new(0, 0);
13139 append_lex_state_name(p, p->lex.state, mesg);
13140 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13141 rb_str_resize(mesg, 0);
13142 append_bitstack_value(p, p->cond_stack, mesg);
13143 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13144 rb_str_resize(mesg, 0);
13145 append_bitstack_value(p, p->cmdarg_stack, mesg);
13146 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13147 if (p->debug_output == rb_ractor_stdout())
13148 p->debug_output = rb_ractor_stderr();
13149 p->debug = TRUE;
13150}
13151
13152static YYLTYPE *
13153rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13154{
13155 yylloc->beg_pos.lineno = sourceline;
13156 yylloc->beg_pos.column = beg_pos;
13157 yylloc->end_pos.lineno = sourceline;
13158 yylloc->end_pos.column = end_pos;
13159 return yylloc;
13160}
13161
13162YYLTYPE *
13163rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13164{
13165 int sourceline = here->sourceline;
13166 int beg_pos = (int)here->offset - here->quote
13167 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13168 int end_pos = (int)here->offset + here->length + here->quote;
13169
13170 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13171}
13172
13173YYLTYPE *
13174rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13175{
13176 yylloc->beg_pos.lineno = p->delayed.beg_line;
13177 yylloc->beg_pos.column = p->delayed.beg_col;
13178 yylloc->end_pos.lineno = p->delayed.end_line;
13179 yylloc->end_pos.column = p->delayed.end_col;
13180
13181 return yylloc;
13182}
13183
13184YYLTYPE *
13185rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13186{
13187 int sourceline = p->ruby_sourceline;
13188 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13189 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13190 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13191}
13192
13193YYLTYPE *
13194rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13195{
13196 yylloc->end_pos = yylloc->beg_pos;
13197
13198 return yylloc;
13199}
13200
13201YYLTYPE *
13202rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13203{
13204 int sourceline = p->ruby_sourceline;
13205 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13206 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13207 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13208}
13209
13210YYLTYPE *
13211rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13212{
13213 int sourceline = p->ruby_sourceline;
13214 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13215 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13216 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13217}
13218#endif /* !RIPPER */
13219
13220static int
13221assignable0(struct parser_params *p, ID id, const char **err)
13222{
13223 if (!id) return -1;
13224 switch (id) {
13225 case keyword_self:
13226 *err = "Can't change the value of self";
13227 return -1;
13228 case keyword_nil:
13229 *err = "Can't assign to nil";
13230 return -1;
13231 case keyword_true:
13232 *err = "Can't assign to true";
13233 return -1;
13234 case keyword_false:
13235 *err = "Can't assign to false";
13236 return -1;
13237 case keyword__FILE__:
13238 *err = "Can't assign to __FILE__";
13239 return -1;
13240 case keyword__LINE__:
13241 *err = "Can't assign to __LINE__";
13242 return -1;
13243 case keyword__ENCODING__:
13244 *err = "Can't assign to __ENCODING__";
13245 return -1;
13246 }
13247 switch (id_type(id)) {
13248 case ID_LOCAL:
13249 if (dyna_in_block(p)) {
13250 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13251 compile_error(p, "Can't assign to numbered parameter _%d",
13252 NUMPARAM_ID_TO_IDX(id));
13253 return -1;
13254 }
13255 if (dvar_curr(p, id)) return NODE_DASGN;
13256 if (dvar_defined(p, id)) return NODE_DASGN;
13257 if (local_id(p, id)) return NODE_LASGN;
13258 dyna_var(p, id);
13259 return NODE_DASGN;
13260 }
13261 else {
13262 if (!local_id(p, id)) local_var(p, id);
13263 return NODE_LASGN;
13264 }
13265 break;
13266 case ID_GLOBAL: return NODE_GASGN;
13267 case ID_INSTANCE: return NODE_IASGN;
13268 case ID_CONST:
13269 if (!p->ctxt.in_def) return NODE_CDECL;
13270 *err = "dynamic constant assignment";
13271 return -1;
13272 case ID_CLASS: return NODE_CVASGN;
13273 default:
13274 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13275 }
13276 return -1;
13277}
13278
13279#ifndef RIPPER
13280static NODE*
13281assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13282{
13283 const char *err = 0;
13284 int node_type = assignable0(p, id, &err);
13285 switch (node_type) {
13286 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13287 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13288 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13289 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13290 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
13291 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13292 }
13293 if (err) yyerror1(loc, err);
13294 return NEW_ERROR(loc);
13295}
13296#else
13297static VALUE
13298assignable(struct parser_params *p, VALUE lhs)
13299{
13300 const char *err = 0;
13301 assignable0(p, get_id(lhs), &err);
13302 if (err) lhs = assign_error(p, err, lhs);
13303 return lhs;
13304}
13305#endif
13306
13307static int
13308is_private_local_id(struct parser_params *p, ID name)
13309{
13310 VALUE s;
13311 if (name == idUScore) return 1;
13312 if (!is_local_id(name)) return 0;
13313 s = rb_id2str(name);
13314 if (!s) return 0;
13315 return RSTRING_PTR(s)[0] == '_';
13316}
13317
13318static int
13319shadowing_lvar_0(struct parser_params *p, ID name)
13320{
13321 if (dyna_in_block(p)) {
13322 if (dvar_curr(p, name)) {
13323 if (is_private_local_id(p, name)) return 1;
13324 yyerror0("duplicated argument name");
13325 }
13326 else if (dvar_defined(p, name) || local_id(p, name)) {
13327 vtable_add(p->lvtbl->vars, name);
13328 if (p->lvtbl->used) {
13329 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13330 }
13331 return 0;
13332 }
13333 }
13334 else {
13335 if (local_id(p, name)) {
13336 if (is_private_local_id(p, name)) return 1;
13337 yyerror0("duplicated argument name");
13338 }
13339 }
13340 return 1;
13341}
13342
13343static ID
13344shadowing_lvar(struct parser_params *p, ID name)
13345{
13346 shadowing_lvar_0(p, name);
13347 return name;
13348}
13349
13350static void
13351new_bv(struct parser_params *p, ID name)
13352{
13353 if (!name) return;
13354 if (!is_local_id(name)) {
13355 compile_error(p, "invalid local variable - %"PRIsVALUE,
13356 rb_id2str(name));
13357 return;
13358 }
13359 if (!shadowing_lvar_0(p, name)) return;
13360 dyna_var(p, name);
13361}
13362
13363#ifndef RIPPER
13364static NODE *
13365aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13366{
13367 return NEW_ATTRASGN(recv, tASET, idx, loc);
13368}
13369
13370static void
13371block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13372{
13373 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13374 compile_error(p, "both block arg and actual block given");
13375 }
13376}
13377
13378static NODE *
13379attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13380{
13381 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13382 return NEW_ATTRASGN(recv, id, 0, loc);
13383}
13384
13385static void
13386rb_backref_error(struct parser_params *p, NODE *node)
13387{
13388 switch (nd_type(node)) {
13389 case NODE_NTH_REF:
13390 compile_error(p, "Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13391 break;
13392 case NODE_BACK_REF:
13393 compile_error(p, "Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13394 break;
13395 }
13396}
13397#else
13398static VALUE
13399backref_error(struct parser_params *p, NODE *ref, VALUE expr)
13400{
13401 VALUE mesg = rb_str_new_cstr("Can't set variable ");
13402 rb_str_append(mesg, RNODE_RIPPER(ref)->nd_cval);
13403 return dispatch2(assign_error, mesg, expr);
13404}
13405#endif
13406
13407#ifndef RIPPER
13408static NODE *
13409arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13410{
13411 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13412 switch (nd_type(node1)) {
13413 case NODE_LIST:
13414 return list_append(p, node1, node2);
13415 case NODE_BLOCK_PASS:
13416 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13417 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13418 return node1;
13419 case NODE_ARGSPUSH:
13420 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13421 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13422 nd_set_type(node1, NODE_ARGSCAT);
13423 return node1;
13424 case NODE_ARGSCAT:
13425 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13426 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13427 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13428 return node1;
13429 }
13430 return NEW_ARGSPUSH(node1, node2, loc);
13431}
13432
13433static NODE *
13434arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13435{
13436 if (!node2) return node1;
13437 switch (nd_type(node1)) {
13438 case NODE_BLOCK_PASS:
13439 if (RNODE_BLOCK_PASS(node1)->nd_head)
13440 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13441 else
13442 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13443 return node1;
13444 case NODE_ARGSPUSH:
13445 if (!nd_type_p(node2, NODE_LIST)) break;
13446 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13447 nd_set_type(node1, NODE_ARGSCAT);
13448 return node1;
13449 case NODE_ARGSCAT:
13450 if (!nd_type_p(node2, NODE_LIST) ||
13451 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13452 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13453 return node1;
13454 }
13455 return NEW_ARGSCAT(node1, node2, loc);
13456}
13457
13458static NODE *
13459last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13460{
13461 NODE *n1;
13462 if ((n1 = splat_array(args)) != 0) {
13463 return list_append(p, n1, last_arg);
13464 }
13465 return arg_append(p, args, last_arg, loc);
13466}
13467
13468static NODE *
13469rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13470{
13471 NODE *n1;
13472 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13473 return list_concat(n1, rest_arg);
13474 }
13475 return arg_concat(p, args, rest_arg, loc);
13476}
13477
13478static NODE *
13479splat_array(NODE* node)
13480{
13481 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13482 if (nd_type_p(node, NODE_LIST)) return node;
13483 return 0;
13484}
13485
13486static void
13487mark_lvar_used(struct parser_params *p, NODE *rhs)
13488{
13489 ID *vidp = NULL;
13490 if (!rhs) return;
13491 switch (nd_type(rhs)) {
13492 case NODE_LASGN:
13493 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13494 if (vidp) *vidp |= LVAR_USED;
13495 }
13496 break;
13497 case NODE_DASGN:
13498 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13499 if (vidp) *vidp |= LVAR_USED;
13500 }
13501 break;
13502#if 0
13503 case NODE_MASGN:
13504 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13505 mark_lvar_used(p, rhs->nd_head);
13506 }
13507 break;
13508#endif
13509 }
13510}
13511
13512static NODE *
13513const_decl_path(struct parser_params *p, NODE **dest)
13514{
13515 NODE *n = *dest;
13516 if (!nd_type_p(n, NODE_CALL)) {
13517 const YYLTYPE *loc = &n->nd_loc;
13518 VALUE path;
13519 if (RNODE_CDECL(n)->nd_vid) {
13520 path = rb_id2str(RNODE_CDECL(n)->nd_vid);
13521 }
13522 else {
13523 n = RNODE_CDECL(n)->nd_else;
13524 path = rb_ary_new();
13525 for (; n && nd_type_p(n, NODE_COLON2); n = RNODE_COLON2(n)->nd_head) {
13526 rb_ary_push(path, rb_id2str(RNODE_COLON2(n)->nd_mid));
13527 }
13528 if (n && nd_type_p(n, NODE_CONST)) {
13529 // Const::Name
13530 rb_ary_push(path, rb_id2str(RNODE_CONST(n)->nd_vid));
13531 }
13532 else if (n && nd_type_p(n, NODE_COLON3)) {
13533 // ::Const::Name
13534 rb_ary_push(path, rb_str_new(0, 0));
13535 }
13536 else {
13537 // expression::Name
13538 rb_ary_push(path, rb_str_new_cstr("..."));
13539 }
13540 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
13541 path = rb_fstring(path);
13542 }
13543 *dest = n = NEW_LIT(path, loc);
13544 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit);
13545 }
13546 return n;
13547}
13548
13549static NODE *
13550make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
13551{
13552 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
13553
13554 if (copy) {
13555 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
13556 NEW_LIST(value, loc), loc);
13557 }
13558 else {
13559 return NEW_CALL(fcore, rb_intern("make_shareable"),
13560 NEW_LIST(value, loc), loc);
13561 }
13562}
13563
13564static NODE *
13565ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
13566{
13567 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
13568 NODE *args = NEW_LIST(value, loc);
13569 args = list_append(p, args, const_decl_path(p, dest));
13570 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
13571}
13572
13573static int is_static_content(NODE *node);
13574
13575static VALUE
13576shareable_literal_value(struct parser_params *p, NODE *node)
13577{
13578 if (!node) return Qnil;
13579 enum node_type type = nd_type(node);
13580 switch (type) {
13581 case NODE_TRUE:
13582 return Qtrue;
13583 case NODE_FALSE:
13584 return Qfalse;
13585 case NODE_NIL:
13586 return Qnil;
13587 case NODE_LIT:
13588 return RNODE_LIT(node)->nd_lit;
13589 default:
13590 return Qundef;
13591 }
13592}
13593
13594#ifndef SHAREABLE_BARE_EXPRESSION
13595#define SHAREABLE_BARE_EXPRESSION 1
13596#endif
13597
13598static NODE *
13599shareable_literal_constant(struct parser_params *p, enum shareability shareable,
13600 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
13601{
13602# define shareable_literal_constant_next(n) \
13603 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
13604 VALUE lit = Qnil;
13605
13606 if (!value) return 0;
13607 enum node_type type = nd_type(value);
13608 switch (type) {
13609 case NODE_TRUE:
13610 case NODE_FALSE:
13611 case NODE_NIL:
13612 case NODE_LIT:
13613 return value;
13614
13615 case NODE_DSTR:
13616 if (shareable == shareable_literal) {
13617 value = NEW_CALL(value, idUMinus, 0, loc);
13618 }
13619 return value;
13620
13621 case NODE_STR:
13622 lit = rb_fstring(RNODE_STR(value)->nd_lit);
13623 nd_set_type(value, NODE_LIT);
13624 RB_OBJ_WRITE(p->ast, &RNODE_LIT(value)->nd_lit, lit);
13625 return value;
13626
13627 case NODE_ZLIST:
13628 lit = rb_ary_new();
13629 OBJ_FREEZE_RAW(lit);
13630 NODE *n = NEW_LIT(lit, loc);
13631 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit);
13632 return n;
13633
13634 case NODE_LIST:
13635 lit = rb_ary_new();
13636 for (NODE *n = value; n; n = RNODE_LIST(n)->nd_next) {
13637 NODE *elt = RNODE_LIST(n)->nd_head;
13638 if (elt) {
13639 elt = shareable_literal_constant_next(elt);
13640 if (elt) {
13641 RNODE_LIST(n)->nd_head = elt;
13642 }
13643 else if (RTEST(lit)) {
13644 rb_ary_clear(lit);
13645 lit = Qfalse;
13646 }
13647 }
13648 if (RTEST(lit)) {
13649 VALUE e = shareable_literal_value(p, elt);
13650 if (!UNDEF_P(e)) {
13651 rb_ary_push(lit, e);
13652 }
13653 else {
13654 rb_ary_clear(lit);
13655 lit = Qnil; /* make shareable at runtime */
13656 }
13657 }
13658 }
13659 break;
13660
13661 case NODE_HASH:
13662 if (!RNODE_HASH(value)->nd_brace) return 0;
13663 lit = rb_hash_new();
13664 for (NODE *n = RNODE_HASH(value)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
13665 NODE *key = RNODE_LIST(n)->nd_head;
13666 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
13667 if (key) {
13668 key = shareable_literal_constant_next(key);
13669 if (key) {
13670 RNODE_LIST(n)->nd_head = key;
13671 }
13672 else if (RTEST(lit)) {
13673 rb_hash_clear(lit);
13674 lit = Qfalse;
13675 }
13676 }
13677 if (val) {
13678 val = shareable_literal_constant_next(val);
13679 if (val) {
13680 RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head = val;
13681 }
13682 else if (RTEST(lit)) {
13683 rb_hash_clear(lit);
13684 lit = Qfalse;
13685 }
13686 }
13687 if (RTEST(lit)) {
13688 VALUE k = shareable_literal_value(p, key);
13689 VALUE v = shareable_literal_value(p, val);
13690 if (!UNDEF_P(k) && !UNDEF_P(v)) {
13691 rb_hash_aset(lit, k, v);
13692 }
13693 else {
13694 rb_hash_clear(lit);
13695 lit = Qnil; /* make shareable at runtime */
13696 }
13697 }
13698 }
13699 break;
13700
13701 default:
13702 if (shareable == shareable_literal &&
13703 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
13704 return ensure_shareable_node(p, dest, value, loc);
13705 }
13706 return 0;
13707 }
13708
13709 /* Array or Hash */
13710 if (!lit) return 0;
13711 if (NIL_P(lit)) {
13712 // if shareable_literal, all elements should have been ensured
13713 // as shareable
13714 value = make_shareable_node(p, value, false, loc);
13715 }
13716 else {
13717 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
13718 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(value)->nd_lit);
13719 }
13720
13721 return value;
13722# undef shareable_literal_constant_next
13723}
13724
13725static NODE *
13726shareable_constant_value(struct parser_params *p, enum shareability shareable,
13727 NODE *lhs, NODE *value, const YYLTYPE *loc)
13728{
13729 if (!value) return 0;
13730 switch (shareable) {
13731 case shareable_none:
13732 return value;
13733
13734 case shareable_literal:
13735 {
13736 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
13737 if (lit) return lit;
13738 return value;
13739 }
13740 break;
13741
13742 case shareable_copy:
13743 case shareable_everything:
13744 {
13745 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
13746 if (lit) return lit;
13747 return make_shareable_node(p, value, shareable == shareable_copy, loc);
13748 }
13749 break;
13750
13751 default:
13752 UNREACHABLE_RETURN(0);
13753 }
13754}
13755
13756static NODE *
13757node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13758{
13759 if (!lhs) return 0;
13760
13761 switch (nd_type(lhs)) {
13762 case NODE_CDECL:
13763 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
13764 /* fallthru */
13765
13766 case NODE_GASGN:
13767 case NODE_IASGN:
13768 case NODE_LASGN:
13769 case NODE_DASGN:
13770 case NODE_MASGN:
13771 case NODE_CVASGN:
13772 set_nd_value(p, lhs, rhs);
13773 nd_set_loc(lhs, loc);
13774 break;
13775
13776 case NODE_ATTRASGN:
13777 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13778 nd_set_loc(lhs, loc);
13779 break;
13780
13781 default:
13782 /* should not happen */
13783 break;
13784 }
13785
13786 return lhs;
13787}
13788
13789static NODE *
13790value_expr_check(struct parser_params *p, NODE *node)
13791{
13792 NODE *void_node = 0, *vn;
13793
13794 if (!node) {
13795 rb_warning0("empty expression");
13796 }
13797 while (node) {
13798 switch (nd_type(node)) {
13799 case NODE_RETURN:
13800 case NODE_BREAK:
13801 case NODE_NEXT:
13802 case NODE_REDO:
13803 case NODE_RETRY:
13804 return void_node ? void_node : node;
13805
13806 case NODE_CASE3:
13807 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13808 compile_error(p, "unexpected node");
13809 return NULL;
13810 }
13811 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13812 return NULL;
13813 }
13814 /* single line pattern matching with "=>" operator */
13815 return void_node ? void_node : node;
13816
13817 case NODE_BLOCK:
13818 while (RNODE_BLOCK(node)->nd_next) {
13819 node = RNODE_BLOCK(node)->nd_next;
13820 }
13821 node = RNODE_BLOCK(node)->nd_head;
13822 break;
13823
13824 case NODE_BEGIN:
13825 node = RNODE_BEGIN(node)->nd_body;
13826 break;
13827
13828 case NODE_IF:
13829 case NODE_UNLESS:
13830 if (!RNODE_IF(node)->nd_body) {
13831 return NULL;
13832 }
13833 else if (!RNODE_IF(node)->nd_else) {
13834 return NULL;
13835 }
13836 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13837 if (!vn) return NULL;
13838 if (!void_node) void_node = vn;
13839 node = RNODE_IF(node)->nd_else;
13840 break;
13841
13842 case NODE_AND:
13843 case NODE_OR:
13844 node = RNODE_AND(node)->nd_1st;
13845 break;
13846
13847 case NODE_LASGN:
13848 case NODE_DASGN:
13849 case NODE_MASGN:
13850 mark_lvar_used(p, node);
13851 return NULL;
13852
13853 default:
13854 return NULL;
13855 }
13856 }
13857
13858 return NULL;
13859}
13860
13861static int
13862value_expr_gen(struct parser_params *p, NODE *node)
13863{
13864 NODE *void_node = value_expr_check(p, node);
13865 if (void_node) {
13866 yyerror1(&void_node->nd_loc, "void value expression");
13867 /* or "control never reach"? */
13868 return FALSE;
13869 }
13870 return TRUE;
13871}
13872
13873static void
13874void_expr(struct parser_params *p, NODE *node)
13875{
13876 const char *useless = 0;
13877
13878 if (!RTEST(ruby_verbose)) return;
13879
13880 if (!node || !(node = nd_once_body(node))) return;
13881 switch (nd_type(node)) {
13882 case NODE_OPCALL:
13883 switch (RNODE_OPCALL(node)->nd_mid) {
13884 case '+':
13885 case '-':
13886 case '*':
13887 case '/':
13888 case '%':
13889 case tPOW:
13890 case tUPLUS:
13891 case tUMINUS:
13892 case '|':
13893 case '^':
13894 case '&':
13895 case tCMP:
13896 case '>':
13897 case tGEQ:
13898 case '<':
13899 case tLEQ:
13900 case tEQ:
13901 case tNEQ:
13902 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13903 break;
13904 }
13905 break;
13906
13907 case NODE_LVAR:
13908 case NODE_DVAR:
13909 case NODE_GVAR:
13910 case NODE_IVAR:
13911 case NODE_CVAR:
13912 case NODE_NTH_REF:
13913 case NODE_BACK_REF:
13914 useless = "a variable";
13915 break;
13916 case NODE_CONST:
13917 useless = "a constant";
13918 break;
13919 case NODE_LIT:
13920 case NODE_STR:
13921 case NODE_DSTR:
13922 case NODE_DREGX:
13923 useless = "a literal";
13924 break;
13925 case NODE_COLON2:
13926 case NODE_COLON3:
13927 useless = "::";
13928 break;
13929 case NODE_DOT2:
13930 useless = "..";
13931 break;
13932 case NODE_DOT3:
13933 useless = "...";
13934 break;
13935 case NODE_SELF:
13936 useless = "self";
13937 break;
13938 case NODE_NIL:
13939 useless = "nil";
13940 break;
13941 case NODE_TRUE:
13942 useless = "true";
13943 break;
13944 case NODE_FALSE:
13945 useless = "false";
13946 break;
13947 case NODE_DEFINED:
13948 useless = "defined?";
13949 break;
13950 }
13951
13952 if (useless) {
13953 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
13954 }
13955}
13956
13957static NODE *
13958void_stmts(struct parser_params *p, NODE *node)
13959{
13960 NODE *const n = node;
13961 if (!RTEST(ruby_verbose)) return n;
13962 if (!node) return n;
13963 if (!nd_type_p(node, NODE_BLOCK)) return n;
13964
13965 while (RNODE_BLOCK(node)->nd_next) {
13966 void_expr(p, RNODE_BLOCK(node)->nd_head);
13967 node = RNODE_BLOCK(node)->nd_next;
13968 }
13969 return n;
13970}
13971
13972static NODE *
13973remove_begin(NODE *node)
13974{
13975 NODE **n = &node, *n1 = node;
13976 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
13977 *n = n1 = RNODE_BEGIN(n1)->nd_body;
13978 }
13979 return node;
13980}
13981
13982static void
13983reduce_nodes(struct parser_params *p, NODE **body)
13984{
13985 NODE *node = *body;
13986
13987 if (!node) {
13988 *body = NEW_NIL(&NULL_LOC);
13989 return;
13990 }
13991#define subnodes(type, n1, n2) \
13992 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
13993 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
13994 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
13995
13996 while (node) {
13997 int newline = (int)(nd_fl_newline(node));
13998 switch (nd_type(node)) {
13999 end:
14000 case NODE_NIL:
14001 *body = 0;
14002 return;
14003 case NODE_RETURN:
14004 *body = node = RNODE_RETURN(node)->nd_stts;
14005 if (newline && node) nd_set_fl_newline(node);
14006 continue;
14007 case NODE_BEGIN:
14008 *body = node = RNODE_BEGIN(node)->nd_body;
14009 if (newline && node) nd_set_fl_newline(node);
14010 continue;
14011 case NODE_BLOCK:
14012 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14013 break;
14014 case NODE_IF:
14015 case NODE_UNLESS:
14016 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14017 return;
14018 case NODE_CASE:
14019 body = &RNODE_CASE(node)->nd_body;
14020 break;
14021 case NODE_WHEN:
14022 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14023 break;
14024 case NODE_ENSURE:
14025 if (!subnodes(RNODE_ENSURE, nd_head, nd_resq)) goto end;
14026 break;
14027 case NODE_RESCUE:
14028 newline = 0; // RESBODY should not be a NEWLINE
14029 if (RNODE_RESCUE(node)->nd_else) {
14030 body = &RNODE_RESCUE(node)->nd_resq;
14031 break;
14032 }
14033 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14034 break;
14035 default:
14036 return;
14037 }
14038 node = *body;
14039 if (newline && node) nd_set_fl_newline(node);
14040 }
14041
14042#undef subnodes
14043}
14044
14045static int
14046is_static_content(NODE *node)
14047{
14048 if (!node) return 1;
14049 switch (nd_type(node)) {
14050 case NODE_HASH:
14051 if (!(node = RNODE_HASH(node)->nd_head)) break;
14052 case NODE_LIST:
14053 do {
14054 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14055 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14056 case NODE_LIT:
14057 case NODE_STR:
14058 case NODE_NIL:
14059 case NODE_TRUE:
14060 case NODE_FALSE:
14061 case NODE_ZLIST:
14062 break;
14063 default:
14064 return 0;
14065 }
14066 return 1;
14067}
14068
14069static int
14070assign_in_cond(struct parser_params *p, NODE *node)
14071{
14072 switch (nd_type(node)) {
14073 case NODE_MASGN:
14074 case NODE_LASGN:
14075 case NODE_DASGN:
14076 case NODE_GASGN:
14077 case NODE_IASGN:
14078 case NODE_CVASGN:
14079 case NODE_CDECL:
14080 break;
14081
14082 default:
14083 return 0;
14084 }
14085
14086 if (!get_nd_value(p, node)) return 1;
14087 if (is_static_content(get_nd_value(p, node))) {
14088 /* reports always */
14089 parser_warn(p, get_nd_value(p, node), "found `= literal' in conditional, should be ==");
14090 }
14091 return 1;
14092}
14093
14094enum cond_type {
14095 COND_IN_OP,
14096 COND_IN_COND,
14097 COND_IN_FF
14098};
14099
14100#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14101 switch (t) { \
14102 case COND_IN_OP: break; \
14103 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14104 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14105 } \
14106} while (0)
14107
14108static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14109
14110static NODE*
14111range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14112{
14113 enum node_type type;
14114
14115 if (node == 0) return 0;
14116
14117 type = nd_type(node);
14118 value_expr(node);
14119 if (type == NODE_LIT && FIXNUM_P(RNODE_LIT(node)->nd_lit)) {
14120 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
14121 ID lineno = rb_intern("$.");
14122 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14123 }
14124 return cond0(p, node, COND_IN_FF, loc, true);
14125}
14126
14127static NODE*
14128cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14129{
14130 if (node == 0) return 0;
14131 if (!(node = nd_once_body(node))) return 0;
14132 assign_in_cond(p, node);
14133
14134 switch (nd_type(node)) {
14135 case NODE_BEGIN:
14136 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14137 break;
14138
14139 case NODE_DSTR:
14140 case NODE_EVSTR:
14141 case NODE_STR:
14142 SWITCH_BY_COND_TYPE(type, warn, "string ");
14143 break;
14144
14145 case NODE_DREGX:
14146 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14147
14148 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14149
14150 case NODE_BLOCK:
14151 {
14152 NODE *end = RNODE_BLOCK(node)->nd_end;
14153 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14154 if (top) top = node == end;
14155 *expr = cond0(p, *expr, type, loc, top);
14156 }
14157 break;
14158
14159 case NODE_AND:
14160 case NODE_OR:
14161 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14162 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14163 break;
14164
14165 case NODE_DOT2:
14166 case NODE_DOT3:
14167 if (!top) break;
14168 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14169 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14170 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
14171 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
14172 break;
14173
14174 case NODE_DSYM:
14175 warn_symbol:
14176 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14177 break;
14178
14179 case NODE_LIT:
14180 if (RB_TYPE_P(RNODE_LIT(node)->nd_lit, T_REGEXP)) {
14181 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14182 nd_set_type(node, NODE_MATCH);
14183 }
14184 else if (RNODE_LIT(node)->nd_lit == Qtrue ||
14185 RNODE_LIT(node)->nd_lit == Qfalse) {
14186 /* booleans are OK, e.g., while true */
14187 }
14188 else if (SYMBOL_P(RNODE_LIT(node)->nd_lit)) {
14189 goto warn_symbol;
14190 }
14191 else {
14192 SWITCH_BY_COND_TYPE(type, warning, "");
14193 }
14194 default:
14195 break;
14196 }
14197 return node;
14198}
14199
14200static NODE*
14201cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14202{
14203 if (node == 0) return 0;
14204 return cond0(p, node, COND_IN_COND, loc, true);
14205}
14206
14207static NODE*
14208method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14209{
14210 if (node == 0) return 0;
14211 return cond0(p, node, COND_IN_OP, loc, true);
14212}
14213
14214static NODE*
14215new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14216{
14217 YYLTYPE loc = {*pos, *pos};
14218 return NEW_NIL(&loc);
14219}
14220
14221static NODE*
14222new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14223{
14224 if (!cc) return right;
14225 cc = cond0(p, cc, COND_IN_COND, loc, true);
14226 return newline_node(NEW_IF(cc, left, right, loc));
14227}
14228
14229static NODE*
14230new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14231{
14232 if (!cc) return right;
14233 cc = cond0(p, cc, COND_IN_COND, loc, true);
14234 return newline_node(NEW_UNLESS(cc, left, right, loc));
14235}
14236
14237#define NEW_AND_OR(type, f, s, loc) (type == NODE_AND ? NEW_AND(f,s,loc) : NEW_OR(f,s,loc))
14238
14239static NODE*
14240logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14241 const YYLTYPE *op_loc, const YYLTYPE *loc)
14242{
14243 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14244 NODE *op;
14245 value_expr(left);
14246 if (left && nd_type_p(left, type)) {
14247 NODE *node = left, *second;
14248 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14249 node = second;
14250 }
14251 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc);
14252 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14253 left->nd_loc.end_pos = loc->end_pos;
14254 return left;
14255 }
14256 op = NEW_AND_OR(type, left, right, loc);
14257 nd_set_line(op, op_loc->beg_pos.lineno);
14258 return op;
14259}
14260
14261#undef NEW_AND_OR
14262
14263static void
14264no_blockarg(struct parser_params *p, NODE *node)
14265{
14266 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14267 compile_error(p, "block argument should not be given");
14268 }
14269}
14270
14271static NODE *
14272ret_args(struct parser_params *p, NODE *node)
14273{
14274 if (node) {
14275 no_blockarg(p, node);
14276 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14277 node = RNODE_LIST(node)->nd_head;
14278 }
14279 }
14280 return node;
14281}
14282
14283static NODE *
14284new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14285{
14286 if (node) no_blockarg(p, node);
14287
14288 return NEW_YIELD(node, loc);
14289}
14290
14291static VALUE
14292negate_lit(struct parser_params *p, VALUE lit)
14293{
14294 if (FIXNUM_P(lit)) {
14295 return LONG2FIX(-FIX2LONG(lit));
14296 }
14297 if (SPECIAL_CONST_P(lit)) {
14298#if USE_FLONUM
14299 if (FLONUM_P(lit)) {
14300 return DBL2NUM(-RFLOAT_VALUE(lit));
14301 }
14302#endif
14303 goto unknown;
14304 }
14305 switch (BUILTIN_TYPE(lit)) {
14306 case T_BIGNUM:
14307 bignum_negate(lit);
14308 lit = rb_big_norm(lit);
14309 break;
14310 case T_RATIONAL:
14311 rational_set_num(lit, negate_lit(p, rational_get_num(lit)));
14312 break;
14313 case T_COMPLEX:
14314 rcomplex_set_real(lit, negate_lit(p, rcomplex_get_real(lit)));
14315 rcomplex_set_imag(lit, negate_lit(p, rcomplex_get_imag(lit)));
14316 break;
14317 case T_FLOAT:
14318 lit = DBL2NUM(-RFLOAT_VALUE(lit));
14319 break;
14320 unknown:
14321 default:
14322 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
14323 rb_builtin_class_name(lit));
14324 break;
14325 }
14326 return lit;
14327}
14328
14329static NODE *
14330arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14331{
14332 if (node2) {
14333 if (!node1) return (NODE *)node2;
14334 node2->nd_head = node1;
14335 nd_set_first_lineno(node2, nd_first_lineno(node1));
14336 nd_set_first_column(node2, nd_first_column(node1));
14337 return (NODE *)node2;
14338 }
14339 return node1;
14340}
14341
14342static bool
14343args_info_empty_p(struct rb_args_info *args)
14344{
14345 if (args->pre_args_num) return false;
14346 if (args->post_args_num) return false;
14347 if (args->rest_arg) return false;
14348 if (args->opt_args) return false;
14349 if (args->block_arg) return false;
14350 if (args->kw_args) return false;
14351 if (args->kw_rest_arg) return false;
14352 return true;
14353}
14354
14355static rb_node_args_t *
14356new_args(struct parser_params *p, rb_node_args_aux_t *pre_args, rb_node_opt_arg_t *opt_args, ID rest_arg, rb_node_args_aux_t *post_args, rb_node_args_t *tail, const YYLTYPE *loc)
14357{
14358 struct rb_args_info *args = &tail->nd_ainfo;
14359
14360 if (args->forwarding) {
14361 if (rest_arg) {
14362 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14363 return tail;
14364 }
14365 rest_arg = idFWD_REST;
14366 }
14367
14368 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
14369 args->pre_init = pre_args ? pre_args->nd_next : 0;
14370
14371 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
14372 args->post_init = post_args ? post_args->nd_next : 0;
14373 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14374
14375 args->rest_arg = rest_arg;
14376
14377 args->opt_args = opt_args;
14378
14379#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14380 args->ruby2_keywords = args->forwarding;
14381#else
14382 args->ruby2_keywords = 0;
14383#endif
14384
14385 nd_set_loc(RNODE(tail), loc);
14386
14387 return tail;
14388}
14389
14390static rb_node_args_t *
14391new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14392{
14393 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14394 struct rb_args_info *args = &node->nd_ainfo;
14395 if (p->error_p) return node;
14396
14397 args->block_arg = block;
14398 args->kw_args = kw_args;
14399
14400 if (kw_args) {
14401 /*
14402 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14403 * variable order: k1, kr1, k2, &b, internal_id, krest
14404 * #=> <reorder>
14405 * variable order: kr1, k1, k2, internal_id, krest, &b
14406 */
14407 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14408 struct vtable *vtargs = p->lvtbl->args;
14409 rb_node_kw_arg_t *kwn = kw_args;
14410
14411 if (block) block = vtargs->tbl[vtargs->pos-1];
14412 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14413 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14414 while (kwn) {
14415 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14416 --kw_vars;
14417 --required_kw_vars;
14418 kwn = kwn->nd_next;
14419 }
14420
14421 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14422 ID vid = get_nd_vid(p, kwn->nd_body);
14423 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14424 *required_kw_vars++ = vid;
14425 }
14426 else {
14427 *kw_vars++ = vid;
14428 }
14429 }
14430
14431 arg_var(p, kw_bits);
14432 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14433 if (block) arg_var(p, block);
14434
14435 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14436 }
14437 else if (kw_rest_arg == idNil) {
14438 args->no_kwarg = 1;
14439 }
14440 else if (kw_rest_arg) {
14441 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14442 }
14443
14444 return node;
14445}
14446
14447static rb_node_args_t *
14448args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam)
14449{
14450 if (max_numparam > NO_PARAM) {
14451 if (!args) {
14452 YYLTYPE loc = RUBY_INIT_YYLLOC();
14453 args = new_args_tail(p, 0, 0, 0, 0);
14454 nd_set_loc(RNODE(args), &loc);
14455 }
14456 args->nd_ainfo.pre_args_num = max_numparam;
14457 }
14458 return args;
14459}
14460
14461static NODE*
14462new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14463{
14464 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14465
14466 if (pre_arg) {
14467 NODE *pre_args = NEW_LIST(pre_arg, loc);
14468 if (RNODE_ARYPTN(aryptn)->pre_args) {
14469 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14470 }
14471 else {
14472 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14473 }
14474 }
14475 return aryptn;
14476}
14477
14478static NODE*
14479new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14480{
14481 if (has_rest) {
14482 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14483 }
14484 else {
14485 rest_arg = NULL;
14486 }
14487 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14488
14489 return node;
14490}
14491
14492static NODE*
14493new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14494{
14495 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14496
14497 return fndptn;
14498}
14499
14500static NODE*
14501new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14502{
14503 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14504 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14505 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14506
14507 return node;
14508}
14509
14510static NODE*
14511new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14512{
14513 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14514 return hshptn;
14515}
14516
14517static NODE*
14518new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14519{
14520 NODE *node, *kw_rest_arg_node;
14521
14522 if (kw_rest_arg == idNil) {
14523 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14524 }
14525 else if (kw_rest_arg) {
14526 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14527 }
14528 else {
14529 kw_rest_arg_node = NULL;
14530 }
14531
14532 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14533
14534 return node;
14535}
14536
14537static NODE*
14538dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14539{
14540 VALUE lit;
14541
14542 if (!node) {
14543 return NEW_LIT(ID2SYM(idNULL), loc);
14544 }
14545
14546 switch (nd_type(node)) {
14547 case NODE_DSTR:
14548 nd_set_type(node, NODE_DSYM);
14549 nd_set_loc(node, loc);
14550 break;
14551 case NODE_STR:
14552 lit = RNODE_STR(node)->nd_lit;
14553 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(node)->nd_lit = ID2SYM(rb_intern_str(lit)));
14554 nd_set_type(node, NODE_LIT);
14555 nd_set_loc(node, loc);
14556 break;
14557 default:
14558 node = NEW_DSYM(Qnil, 1, NEW_LIST(node, loc), loc);
14559 break;
14560 }
14561 return node;
14562}
14563
14564static int
14565append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
14566{
14567 NODE *node = (NODE *)v;
14568 NODE **result = (NODE **)h;
14569 RNODE_LIST(node)->as.nd_alen = 2;
14570 RNODE_LIST(RNODE_LIST(node)->nd_next)->as.nd_end = RNODE_LIST(node)->nd_next;
14571 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next = 0;
14572 if (*result)
14573 list_concat(*result, node);
14574 else
14575 *result = node;
14576 return ST_CONTINUE;
14577}
14578
14579static NODE *
14580remove_duplicate_keys(struct parser_params *p, NODE *hash)
14581{
14582 struct st_hash_type literal_type = {
14583 literal_cmp,
14584 literal_hash,
14585 };
14586
14587 st_table *literal_keys = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14588 NODE *result = 0;
14589 NODE *last_expr = 0;
14590 rb_code_location_t loc = hash->nd_loc;
14591 while (hash && RNODE_LIST(hash)->nd_next) {
14592 NODE *head = RNODE_LIST(hash)->nd_head;
14593 NODE *value = RNODE_LIST(hash)->nd_next;
14594 NODE *next = RNODE_LIST(value)->nd_next;
14595 st_data_t key = (st_data_t)head;
14596 st_data_t data;
14597 RNODE_LIST(value)->nd_next = 0;
14598 if (!head) {
14599 key = (st_data_t)value;
14600 }
14601 else if (nd_type_p(head, NODE_LIT) &&
14602 st_delete(literal_keys, (key = (st_data_t)RNODE_LIT(head)->nd_lit, &key), &data)) {
14603 NODE *dup_value = (RNODE_LIST((NODE *)data))->nd_next;
14604 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
14605 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
14606 RNODE_LIT(head)->nd_lit, nd_line(head));
14607 if (dup_value == last_expr) {
14608 RNODE_LIST(value)->nd_head = block_append(p, RNODE_LIST(dup_value)->nd_head, RNODE_LIST(value)->nd_head);
14609 }
14610 else {
14611 RNODE_LIST(last_expr)->nd_head = block_append(p, RNODE_LIST(dup_value)->nd_head, RNODE_LIST(last_expr)->nd_head);
14612 }
14613 }
14614 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
14615 last_expr = !head || nd_type_p(head, NODE_LIT) ? value : head;
14616 hash = next;
14617 }
14618 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
14619 st_free_table(literal_keys);
14620 if (hash) {
14621 if (!result) result = hash;
14622 else list_concat(result, hash);
14623 }
14624 result->nd_loc = loc;
14625 return result;
14626}
14627
14628static NODE *
14629new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14630{
14631 if (hash) hash = remove_duplicate_keys(p, hash);
14632 return NEW_HASH(hash, loc);
14633}
14634#endif
14635
14636static void
14637error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14638{
14639 if (is_private_local_id(p, id)) {
14640 return;
14641 }
14642 if (st_is_member(p->pvtbl, id)) {
14643 yyerror1(loc, "duplicated variable name");
14644 }
14645 else {
14646 st_insert(p->pvtbl, (st_data_t)id, 0);
14647 }
14648}
14649
14650static void
14651error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14652{
14653 if (!p->pktbl) {
14654 p->pktbl = st_init_numtable();
14655 }
14656 else if (st_is_member(p->pktbl, key)) {
14657 yyerror1(loc, "duplicated key name");
14658 return;
14659 }
14660 st_insert(p->pktbl, (st_data_t)key, 0);
14661}
14662
14663#ifndef RIPPER
14664static NODE *
14665new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14666{
14667 return NEW_HASH(hash, loc);
14668}
14669#endif /* !RIPPER */
14670
14671#ifndef RIPPER
14672static NODE *
14673new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14674{
14675 NODE *asgn;
14676
14677 if (lhs) {
14678 ID vid = get_nd_vid(p, lhs);
14679 YYLTYPE lhs_loc = lhs->nd_loc;
14680 int shareable = ctxt.shareable_constant_value;
14681 if (shareable) {
14682 switch (nd_type(lhs)) {
14683 case NODE_CDECL:
14684 case NODE_COLON2:
14685 case NODE_COLON3:
14686 break;
14687 default:
14688 shareable = 0;
14689 break;
14690 }
14691 }
14692 if (op == tOROP) {
14693 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14694 set_nd_value(p, lhs, rhs);
14695 nd_set_loc(lhs, loc);
14696 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14697 }
14698 else if (op == tANDOP) {
14699 if (shareable) {
14700 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14701 }
14702 set_nd_value(p, lhs, rhs);
14703 nd_set_loc(lhs, loc);
14704 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14705 }
14706 else {
14707 asgn = lhs;
14708 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14709 if (shareable) {
14710 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14711 }
14712 set_nd_value(p, asgn, rhs);
14713 nd_set_loc(asgn, loc);
14714 }
14715 }
14716 else {
14717 asgn = NEW_ERROR(loc);
14718 }
14719 return asgn;
14720}
14721
14722static NODE *
14723new_ary_op_assign(struct parser_params *p, NODE *ary,
14724 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
14725{
14726 NODE *asgn;
14727
14728 args = make_list(args, args_loc);
14729 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc);
14730 fixpos(asgn, ary);
14731 return asgn;
14732}
14733
14734static NODE *
14735new_attr_op_assign(struct parser_params *p, NODE *lhs,
14736 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
14737{
14738 NODE *asgn;
14739
14740 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
14741 fixpos(asgn, lhs);
14742 return asgn;
14743}
14744
14745static NODE *
14746new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14747{
14748 NODE *asgn;
14749
14750 if (lhs) {
14751 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
14752 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
14753 }
14754 else {
14755 asgn = NEW_ERROR(loc);
14756 }
14757 fixpos(asgn, lhs);
14758 return asgn;
14759}
14760
14761static NODE *
14762const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14763{
14764 if (p->ctxt.in_def) {
14765 yyerror1(loc, "dynamic constant assignment");
14766 }
14767 return NEW_CDECL(0, 0, (path), loc);
14768}
14769#else
14770static VALUE
14771const_decl(struct parser_params *p, VALUE path)
14772{
14773 if (p->ctxt.in_def) {
14774 path = assign_error(p, "dynamic constant assignment", path);
14775 }
14776 return path;
14777}
14778
14779static VALUE
14780assign_error(struct parser_params *p, const char *mesg, VALUE a)
14781{
14782 a = dispatch2(assign_error, ERR_MESG(), a);
14783 ripper_error(p);
14784 return a;
14785}
14786
14787static VALUE
14788var_field(struct parser_params *p, VALUE a)
14789{
14790 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
14791}
14792#endif
14793
14794#ifndef RIPPER
14795static NODE *
14796new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14797{
14798 NODE *result = head;
14799 if (rescue) {
14800 NODE *tmp = rescue_else ? rescue_else : rescue;
14801 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14802
14803 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14804 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14805 }
14806 else if (rescue_else) {
14807 result = block_append(p, result, rescue_else);
14808 }
14809 if (ensure) {
14810 result = NEW_ENSURE(result, ensure, loc);
14811 }
14812 fixpos(result, head);
14813 return result;
14814}
14815#endif
14816
14817static void
14818warn_unused_var(struct parser_params *p, struct local_vars *local)
14819{
14820 int cnt;
14821
14822 if (!local->used) return;
14823 cnt = local->used->pos;
14824 if (cnt != local->vars->pos) {
14825 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14826 }
14827#ifndef RIPPER
14828 ID *v = local->vars->tbl;
14829 ID *u = local->used->tbl;
14830 for (int i = 0; i < cnt; ++i) {
14831 if (!v[i] || (u[i] & LVAR_USED)) continue;
14832 if (is_private_local_id(p, v[i])) continue;
14833 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14834 }
14835#endif
14836}
14837
14838static void
14839local_push(struct parser_params *p, int toplevel_scope)
14840{
14841 struct local_vars *local;
14842 int inherits_dvars = toplevel_scope && compile_for_eval;
14843 int warn_unused_vars = RTEST(ruby_verbose);
14844
14845 local = ALLOC(struct local_vars);
14846 local->prev = p->lvtbl;
14847 local->args = vtable_alloc(0);
14848 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14849#ifndef RIPPER
14850 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14851 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14852 local->numparam.outer = 0;
14853 local->numparam.inner = 0;
14854 local->numparam.current = 0;
14855#endif
14856 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14857
14858# if WARN_PAST_SCOPE
14859 local->past = 0;
14860# endif
14861 CMDARG_PUSH(0);
14862 COND_PUSH(0);
14863 p->lvtbl = local;
14864}
14865
14866static void
14867vtable_chain_free(struct parser_params *p, struct vtable *table)
14868{
14869 while (!DVARS_TERMINAL_P(table)) {
14870 struct vtable *cur_table = table;
14871 table = cur_table->prev;
14872 vtable_free(cur_table);
14873 }
14874}
14875
14876static void
14877local_free(struct parser_params *p, struct local_vars *local)
14878{
14879 vtable_chain_free(p, local->used);
14880
14881# if WARN_PAST_SCOPE
14882 vtable_chain_free(p, local->past);
14883# endif
14884
14885 vtable_chain_free(p, local->args);
14886 vtable_chain_free(p, local->vars);
14887
14888 ruby_sized_xfree(local, sizeof(struct local_vars));
14889}
14890
14891static void
14892local_pop(struct parser_params *p)
14893{
14894 struct local_vars *local = p->lvtbl->prev;
14895 if (p->lvtbl->used) {
14896 warn_unused_var(p, p->lvtbl);
14897 }
14898
14899 local_free(p, p->lvtbl);
14900 p->lvtbl = local;
14901
14902 CMDARG_POP();
14903 COND_POP();
14904}
14905
14906#ifndef RIPPER
14907static rb_ast_id_table_t *
14908local_tbl(struct parser_params *p)
14909{
14910 int cnt_args = vtable_size(p->lvtbl->args);
14911 int cnt_vars = vtable_size(p->lvtbl->vars);
14912 int cnt = cnt_args + cnt_vars;
14913 int i, j;
14914 rb_ast_id_table_t *tbl;
14915
14916 if (cnt <= 0) return 0;
14917 tbl = rb_ast_new_local_table(p->ast, cnt);
14918 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14919 /* remove IDs duplicated to warn shadowing */
14920 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14921 ID id = p->lvtbl->vars->tbl[i];
14922 if (!vtable_included(p->lvtbl->args, id)) {
14923 tbl->ids[j++] = id;
14924 }
14925 }
14926 if (j < cnt) {
14927 tbl = rb_ast_resize_latest_local_table(p->ast, j);
14928 }
14929
14930 return tbl;
14931}
14932
14933#endif
14934
14935static void
14936numparam_name(struct parser_params *p, ID id)
14937{
14938 if (!NUMPARAM_ID_P(id)) return;
14939 compile_error(p, "_%d is reserved for numbered parameter",
14940 NUMPARAM_ID_TO_IDX(id));
14941}
14942
14943static void
14944arg_var(struct parser_params *p, ID id)
14945{
14946 numparam_name(p, id);
14947 vtable_add(p->lvtbl->args, id);
14948}
14949
14950static void
14951local_var(struct parser_params *p, ID id)
14952{
14953 numparam_name(p, id);
14954 vtable_add(p->lvtbl->vars, id);
14955 if (p->lvtbl->used) {
14956 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
14957 }
14958}
14959
14960static int
14961local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
14962{
14963 struct vtable *vars, *args, *used;
14964
14965 vars = p->lvtbl->vars;
14966 args = p->lvtbl->args;
14967 used = p->lvtbl->used;
14968
14969 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
14970 vars = vars->prev;
14971 args = args->prev;
14972 if (used) used = used->prev;
14973 }
14974
14975 if (vars && vars->prev == DVARS_INHERIT) {
14976 return rb_local_defined(id, p->parent_iseq);
14977 }
14978 else if (vtable_included(args, id)) {
14979 return 1;
14980 }
14981 else {
14982 int i = vtable_included(vars, id);
14983 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
14984 return i != 0;
14985 }
14986}
14987
14988static int
14989local_id(struct parser_params *p, ID id)
14990{
14991 return local_id_ref(p, id, NULL);
14992}
14993
14994static int
14995check_forwarding_args(struct parser_params *p)
14996{
14997 if (local_id(p, idFWD_ALL)) return TRUE;
14998 compile_error(p, "unexpected ...");
14999 return FALSE;
15000}
15001
15002static void
15003add_forwarding_args(struct parser_params *p)
15004{
15005 arg_var(p, idFWD_REST);
15006#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15007 arg_var(p, idFWD_KWREST);
15008#endif
15009 arg_var(p, idFWD_BLOCK);
15010 arg_var(p, idFWD_ALL);
15011}
15012
15013static void
15014forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15015{
15016 bool conflict = false;
15017
15018 struct vtable *vars, *args;
15019
15020 vars = p->lvtbl->vars;
15021 args = p->lvtbl->args;
15022
15023 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15024 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15025 vars = vars->prev;
15026 args = args->prev;
15027 }
15028
15029 bool found = false;
15030 if (vars && vars->prev == DVARS_INHERIT && !found) {
15031 found = (rb_local_defined(arg, p->parent_iseq) &&
15032 !(all && rb_local_defined(all, p->parent_iseq)));
15033 }
15034 else {
15035 found = (vtable_included(args, arg) &&
15036 !(all && vtable_included(args, all)));
15037 }
15038
15039 if (!found) {
15040 compile_error(p, "no anonymous %s parameter", var);
15041 }
15042 else if (conflict) {
15043 compile_error(p, "anonymous %s parameter is also used within block", var);
15044 }
15045}
15046
15047#ifndef RIPPER
15048static NODE *
15049new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15050{
15051 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15052#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15053 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15054#endif
15055 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
15056 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc);
15057#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15058 args = arg_append(p, args, new_hash(p, kwrest, loc), loc);
15059#endif
15060 return arg_blk_pass(args, block);
15061}
15062#endif
15063
15064static NODE *
15065numparam_push(struct parser_params *p)
15066{
15067#ifndef RIPPER
15068 struct local_vars *local = p->lvtbl;
15069 NODE *inner = local->numparam.inner;
15070 if (!local->numparam.outer) {
15071 local->numparam.outer = local->numparam.current;
15072 }
15073 local->numparam.inner = 0;
15074 local->numparam.current = 0;
15075 return inner;
15076#else
15077 return 0;
15078#endif
15079}
15080
15081static void
15082numparam_pop(struct parser_params *p, NODE *prev_inner)
15083{
15084#ifndef RIPPER
15085 struct local_vars *local = p->lvtbl;
15086 if (prev_inner) {
15087 /* prefer first one */
15088 local->numparam.inner = prev_inner;
15089 }
15090 else if (local->numparam.current) {
15091 /* current and inner are exclusive */
15092 local->numparam.inner = local->numparam.current;
15093 }
15094 if (p->max_numparam > NO_PARAM) {
15095 /* current and outer are exclusive */
15096 local->numparam.current = local->numparam.outer;
15097 local->numparam.outer = 0;
15098 }
15099 else {
15100 /* no numbered parameter */
15101 local->numparam.current = 0;
15102 }
15103#endif
15104}
15105
15106static const struct vtable *
15107dyna_push(struct parser_params *p)
15108{
15109 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15110 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15111 if (p->lvtbl->used) {
15112 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15113 }
15114 return p->lvtbl->args;
15115}
15116
15117static void
15118dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15119{
15120 struct vtable *tmp = *vtblp;
15121 *vtblp = tmp->prev;
15122# if WARN_PAST_SCOPE
15123 if (p->past_scope_enabled) {
15124 tmp->prev = p->lvtbl->past;
15125 p->lvtbl->past = tmp;
15126 return;
15127 }
15128# endif
15129 vtable_free(tmp);
15130}
15131
15132static void
15133dyna_pop_1(struct parser_params *p)
15134{
15135 struct vtable *tmp;
15136
15137 if ((tmp = p->lvtbl->used) != 0) {
15138 warn_unused_var(p, p->lvtbl);
15139 p->lvtbl->used = p->lvtbl->used->prev;
15140 vtable_free(tmp);
15141 }
15142 dyna_pop_vtable(p, &p->lvtbl->args);
15143 dyna_pop_vtable(p, &p->lvtbl->vars);
15144}
15145
15146static void
15147dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15148{
15149 while (p->lvtbl->args != lvargs) {
15150 dyna_pop_1(p);
15151 if (!p->lvtbl->args) {
15152 struct local_vars *local = p->lvtbl->prev;
15153 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15154 p->lvtbl = local;
15155 }
15156 }
15157 dyna_pop_1(p);
15158}
15159
15160static int
15161dyna_in_block(struct parser_params *p)
15162{
15163 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15164}
15165
15166static int
15167dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15168{
15169 struct vtable *vars, *args, *used;
15170 int i;
15171
15172 args = p->lvtbl->args;
15173 vars = p->lvtbl->vars;
15174 used = p->lvtbl->used;
15175
15176 while (!DVARS_TERMINAL_P(vars)) {
15177 if (vtable_included(args, id)) {
15178 return 1;
15179 }
15180 if ((i = vtable_included(vars, id)) != 0) {
15181 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15182 return 1;
15183 }
15184 args = args->prev;
15185 vars = vars->prev;
15186 if (!vidrefp) used = 0;
15187 if (used) used = used->prev;
15188 }
15189
15190 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15191 return rb_dvar_defined(id, p->parent_iseq);
15192 }
15193
15194 return 0;
15195}
15196
15197static int
15198dvar_defined(struct parser_params *p, ID id)
15199{
15200 return dvar_defined_ref(p, id, NULL);
15201}
15202
15203static int
15204dvar_curr(struct parser_params *p, ID id)
15205{
15206 return (vtable_included(p->lvtbl->args, id) ||
15207 vtable_included(p->lvtbl->vars, id));
15208}
15209
15210static void
15211reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
15212{
15213 compile_error(p,
15214 "regexp encoding option '%c' differs from source encoding '%s'",
15215 c, rb_enc_name(rb_enc_get(str)));
15216}
15217
15218#ifndef RIPPER
15219int
15220rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
15221{
15222 int c = RE_OPTION_ENCODING_IDX(options);
15223
15224 if (c) {
15225 int opt, idx;
15226 rb_char_to_option_kcode(c, &opt, &idx);
15227 if (idx != ENCODING_GET(str) &&
15228 !is_ascii_string(str)) {
15229 goto error;
15230 }
15231 ENCODING_SET(str, idx);
15232 }
15233 else if (RE_OPTION_ENCODING_NONE(options)) {
15234 if (!ENCODING_IS_ASCII8BIT(str) &&
15235 !is_ascii_string(str)) {
15236 c = 'n';
15237 goto error;
15238 }
15239 rb_enc_associate(str, rb_ascii8bit_encoding());
15240 }
15241 else if (rb_is_usascii_enc(p->enc)) {
15242 if (!is_ascii_string(str)) {
15243 /* raise in re.c */
15244 rb_enc_associate(str, rb_usascii_encoding());
15245 }
15246 else {
15247 rb_enc_associate(str, rb_ascii8bit_encoding());
15248 }
15249 }
15250 return 0;
15251
15252 error:
15253 return c;
15254}
15255
15256static void
15257reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
15258{
15259 int c = rb_reg_fragment_setenc(p, str, options);
15260 if (c) reg_fragment_enc_error(p, str, c);
15261}
15262
15263static int
15264reg_fragment_check(struct parser_params* p, VALUE str, int options)
15265{
15266 VALUE err;
15267 reg_fragment_setenc(p, str, options);
15268 err = rb_reg_check_preprocess(str);
15269 if (err != Qnil) {
15270 err = rb_obj_as_string(err);
15271 compile_error(p, "%"PRIsVALUE, err);
15272 return 0;
15273 }
15274 return 1;
15275}
15276
15277#ifndef UNIVERSAL_PARSER
15278typedef struct {
15279 struct parser_params* parser;
15280 rb_encoding *enc;
15281 NODE *succ_block;
15282 const YYLTYPE *loc;
15283} reg_named_capture_assign_t;
15284
15285static int
15286reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15287 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15288{
15289 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15290 struct parser_params* p = arg->parser;
15291 rb_encoding *enc = arg->enc;
15292 long len = name_end - name;
15293 const char *s = (const char *)name;
15294
15295 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc);
15296}
15297
15298static NODE *
15299reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
15300{
15301 reg_named_capture_assign_t arg;
15302
15303 arg.parser = p;
15304 arg.enc = rb_enc_get(regexp);
15305 arg.succ_block = 0;
15306 arg.loc = loc;
15307 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15308
15309 if (!arg.succ_block) return 0;
15310 return RNODE_BLOCK(arg.succ_block)->nd_next;
15311}
15312#endif
15313
15314int
15315rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15316 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc)
15317{
15318 ID var;
15319 NODE *node, *succ;
15320
15321 if (!len) return ST_CONTINUE;
15322 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15323 return ST_CONTINUE;
15324
15325 var = intern_cstr(s, len, enc);
15326 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15327 if (!lvar_defined(p, var)) return ST_CONTINUE;
15328 }
15329 node = node_assign(p, assignable(p, var, 0, loc), NEW_LIT(ID2SYM(var), loc), NO_LEX_CTXT, loc);
15330 succ = *succ_block;
15331 if (!succ) succ = NEW_ERROR(loc);
15332 succ = block_append(p, succ, node);
15333 *succ_block = succ;
15334 return ST_CONTINUE;
15335}
15336
15337static VALUE
15338parser_reg_compile(struct parser_params* p, VALUE str, int options)
15339{
15340 reg_fragment_setenc(p, str, options);
15341 return rb_parser_reg_compile(p, str, options);
15342}
15343
15344VALUE
15345rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15346{
15347 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15348}
15349
15350static VALUE
15351reg_compile(struct parser_params* p, VALUE str, int options)
15352{
15353 VALUE re;
15354 VALUE err;
15355
15356 err = rb_errinfo();
15357 re = parser_reg_compile(p, str, options);
15358 if (NIL_P(re)) {
15359 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15360 rb_set_errinfo(err);
15361 compile_error(p, "%"PRIsVALUE, m);
15362 return Qnil;
15363 }
15364 return re;
15365}
15366#else
15367static VALUE
15368parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
15369{
15370 VALUE err = rb_errinfo();
15371 VALUE re;
15372 str = ripper_is_node_yylval(p, str) ? RNODE_RIPPER(str)->nd_cval : str;
15373 int c = rb_reg_fragment_setenc(p, str, options);
15374 if (c) reg_fragment_enc_error(p, str, c);
15375 re = rb_parser_reg_compile(p, str, options);
15376 if (NIL_P(re)) {
15377 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
15378 rb_set_errinfo(err);
15379 }
15380 return re;
15381}
15382#endif
15383
15384#ifndef RIPPER
15385void
15386rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15387{
15388 p->do_print = print;
15389 p->do_loop = loop;
15390 p->do_chomp = chomp;
15391 p->do_split = split;
15392}
15393
15394static NODE *
15395parser_append_options(struct parser_params *p, NODE *node)
15396{
15397 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15398 const YYLTYPE *const LOC = &default_location;
15399
15400 if (p->do_print) {
15401 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15402 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15403 LOC);
15404 node = block_append(p, node, print);
15405 }
15406
15407 if (p->do_loop) {
15408 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15409
15410 if (p->do_split) {
15411 ID ifs = rb_intern("$;");
15412 ID fields = rb_intern("$F");
15413 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15414 NODE *split = NEW_GASGN(fields,
15415 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15416 rb_intern("split"), args, LOC),
15417 LOC);
15418 node = block_append(p, split, node);
15419 }
15420 if (p->do_chomp) {
15421 NODE *chomp = NEW_LIT(ID2SYM(rb_intern("chomp")), LOC);
15422 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15423 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15424 }
15425
15426 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC);
15427 }
15428
15429 return node;
15430}
15431
15432void
15433rb_init_parse(void)
15434{
15435 /* just to suppress unused-function warnings */
15436 (void)nodetype;
15437 (void)nodeline;
15438}
15439
15440static ID
15441internal_id(struct parser_params *p)
15442{
15443 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15444}
15445#endif /* !RIPPER */
15446
15447static void
15448parser_initialize(struct parser_params *p)
15449{
15450 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15451 p->command_start = TRUE;
15452 p->ruby_sourcefile_string = Qnil;
15453 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15454 p->node_id = 0;
15455 p->delayed.token = Qnil;
15456 p->frozen_string_literal = -1; /* not specified */
15457#ifdef RIPPER
15458 p->result = Qnil;
15459 p->parsing_thread = Qnil;
15460#else
15461 p->error_buffer = Qfalse;
15462 p->end_expect_token_locations = Qnil;
15463 p->token_id = 0;
15464 p->tokens = Qnil;
15465#endif
15466 p->debug_buffer = Qnil;
15467 p->debug_output = rb_ractor_stdout();
15468 p->enc = rb_utf8_encoding();
15469 p->exits = 0;
15470}
15471
15472#ifdef RIPPER
15473#define rb_ruby_parser_mark ripper_parser_mark
15474#define rb_ruby_parser_free ripper_parser_free
15475#define rb_ruby_parser_memsize ripper_parser_memsize
15476#endif
15477
15478void
15479rb_ruby_parser_mark(void *ptr)
15480{
15481 struct parser_params *p = (struct parser_params*)ptr;
15482
15483 rb_gc_mark(p->lex.input);
15484 rb_gc_mark(p->lex.lastline);
15485 rb_gc_mark(p->lex.nextline);
15486 rb_gc_mark(p->ruby_sourcefile_string);
15487 rb_gc_mark((VALUE)p->ast);
15488 rb_gc_mark(p->case_labels);
15489 rb_gc_mark(p->delayed.token);
15490#ifndef RIPPER
15491 rb_gc_mark(p->debug_lines);
15492 rb_gc_mark(p->error_buffer);
15493 rb_gc_mark(p->end_expect_token_locations);
15494 rb_gc_mark(p->tokens);
15495#else
15496 rb_gc_mark(p->value);
15497 rb_gc_mark(p->result);
15498 rb_gc_mark(p->parsing_thread);
15499#endif
15500 rb_gc_mark(p->debug_buffer);
15501 rb_gc_mark(p->debug_output);
15502#ifdef YYMALLOC
15503 rb_gc_mark((VALUE)p->heap);
15504#endif
15505}
15506
15507void
15508rb_ruby_parser_free(void *ptr)
15509{
15510 struct parser_params *p = (struct parser_params*)ptr;
15511 struct local_vars *local, *prev;
15512#ifdef UNIVERSAL_PARSER
15513 rb_parser_config_t *config = p->config;
15514#endif
15515
15516 if (p->tokenbuf) {
15517 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15518 }
15519
15520 for (local = p->lvtbl; local; local = prev) {
15521 prev = local->prev;
15522 local_free(p, local);
15523 }
15524
15525 {
15526 token_info *ptinfo;
15527 while ((ptinfo = p->token_info) != 0) {
15528 p->token_info = ptinfo->next;
15529 xfree(ptinfo);
15530 }
15531 }
15532 xfree(ptr);
15533
15534#ifdef UNIVERSAL_PARSER
15535 config->counter--;
15536 if (config->counter <= 0) {
15537 rb_ruby_parser_config_free(config);
15538 }
15539#endif
15540}
15541
15542size_t
15543rb_ruby_parser_memsize(const void *ptr)
15544{
15545 struct parser_params *p = (struct parser_params*)ptr;
15546 struct local_vars *local;
15547 size_t size = sizeof(*p);
15548
15549 size += p->toksiz;
15550 for (local = p->lvtbl; local; local = local->prev) {
15551 size += sizeof(*local);
15552 if (local->vars) size += local->vars->capa * sizeof(ID);
15553 }
15554 return size;
15555}
15556
15557#ifdef UNIVERSAL_PARSER
15558rb_parser_config_t *
15559rb_ruby_parser_config_new(void *(*malloc)(size_t size))
15560{
15561 return (rb_parser_config_t *)malloc(sizeof(rb_parser_config_t));
15562}
15563
15564void
15565rb_ruby_parser_config_free(rb_parser_config_t *config)
15566{
15567 config->free(config);
15568}
15569#endif
15570
15571#ifndef UNIVERSAL_PARSER
15572#ifndef RIPPER
15573static const rb_data_type_t parser_data_type = {
15574 "parser",
15575 {
15576 rb_ruby_parser_mark,
15577 rb_ruby_parser_free,
15578 rb_ruby_parser_memsize,
15579 },
15580 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
15581};
15582#endif
15583#endif
15584
15585#ifndef RIPPER
15586#undef rb_reserved_word
15587
15588const struct kwtable *
15589rb_reserved_word(const char *str, unsigned int len)
15590{
15591 return reserved_word(str, len);
15592}
15593
15594#ifdef UNIVERSAL_PARSER
15595rb_parser_t *
15596rb_ruby_parser_allocate(rb_parser_config_t *config)
15597{
15598 /* parser_initialize expects fields to be set to 0 */
15599 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15600 p->config = config;
15601 p->config->counter++;
15602 return p;
15603}
15604
15605rb_parser_t *
15606rb_ruby_parser_new(rb_parser_config_t *config)
15607{
15608 /* parser_initialize expects fields to be set to 0 */
15609 rb_parser_t *p = rb_ruby_parser_allocate(config);
15610 parser_initialize(p);
15611 return p;
15612}
15613#endif
15614
15615rb_parser_t *
15616rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15617{
15618 p->error_buffer = main ? Qfalse : Qnil;
15619 p->parent_iseq = base;
15620 return p;
15621}
15622
15623void
15624rb_ruby_parser_set_script_lines(rb_parser_t *p, VALUE lines)
15625{
15626 if (!RTEST(lines)) {
15627 lines = Qfalse;
15628 }
15629 else if (lines == Qtrue) {
15630 lines = rb_ary_new();
15631 }
15632 else {
15633 Check_Type(lines, T_ARRAY);
15634 rb_ary_modify(lines);
15635 }
15636 p->debug_lines = lines;
15637}
15638
15639void
15640rb_ruby_parser_error_tolerant(rb_parser_t *p)
15641{
15642 p->error_tolerant = 1;
15643 // TODO
15644 p->end_expect_token_locations = rb_ary_new();
15645}
15646
15647void
15648rb_ruby_parser_keep_tokens(rb_parser_t *p)
15649{
15650 p->keep_tokens = 1;
15651 // TODO
15652 p->tokens = rb_ary_new();
15653}
15654
15655#ifndef UNIVERSAL_PARSER
15656rb_ast_t*
15657rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
15658{
15659 struct parser_params *p;
15660
15661 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15662 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15663 return rb_ruby_parser_compile_file_path(p, fname, file, start);
15664}
15665
15666rb_ast_t*
15667rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
15668{
15669 struct parser_params *p;
15670
15671 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15672 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15673 return rb_ruby_parser_compile_generic(p, lex_gets, fname, input, start);
15674}
15675
15676rb_ast_t*
15677rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
15678{
15679 struct parser_params *p;
15680
15681 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15682 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15683 return rb_ruby_parser_compile_string(p, f, s, line);
15684}
15685
15686rb_ast_t*
15687rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
15688{
15689 struct parser_params *p;
15690
15691 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15692 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15693 return rb_ruby_parser_compile_string_path(p, f, s, line);
15694}
15695
15696VALUE
15697rb_parser_encoding(VALUE vparser)
15698{
15699 struct parser_params *p;
15700
15701 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15702 return rb_ruby_parser_encoding(p);
15703}
15704
15705VALUE
15706rb_parser_end_seen_p(VALUE vparser)
15707{
15708 struct parser_params *p;
15709
15710 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15711 return RBOOL(rb_ruby_parser_end_seen_p(p));
15712}
15713
15714void
15715rb_parser_error_tolerant(VALUE vparser)
15716{
15717 struct parser_params *p;
15718
15719 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15720 rb_ruby_parser_error_tolerant(p);
15721}
15722
15723void
15724rb_parser_set_script_lines(VALUE vparser, VALUE lines)
15725{
15726 struct parser_params *p;
15727
15728 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15729 rb_ruby_parser_set_script_lines(p, lines);
15730}
15731
15732void
15733rb_parser_keep_tokens(VALUE vparser)
15734{
15735 struct parser_params *p;
15736
15737 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15738 rb_ruby_parser_keep_tokens(p);
15739}
15740
15741VALUE
15742rb_parser_new(void)
15743{
15744 struct parser_params *p;
15745 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
15746 &parser_data_type, p);
15747 parser_initialize(p);
15748 return parser;
15749}
15750
15751VALUE
15752rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
15753{
15754 struct parser_params *p;
15755
15756 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15757 rb_ruby_parser_set_context(p, base, main);
15758 return vparser;
15759}
15760
15761void
15762rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
15763{
15764 struct parser_params *p;
15765
15766 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15767 rb_ruby_parser_set_options(p, print, loop, chomp, split);
15768}
15769
15770VALUE
15771rb_parser_set_yydebug(VALUE self, VALUE flag)
15772{
15773 struct parser_params *p;
15774
15775 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
15776 rb_ruby_parser_set_yydebug(p, RTEST(flag));
15777 return flag;
15778}
15779#endif /* !UNIVERSAL_PARSER */
15780
15781VALUE
15782rb_ruby_parser_encoding(rb_parser_t *p)
15783{
15784 return rb_enc_from_encoding(p->enc);
15785}
15786
15787int
15788rb_ruby_parser_end_seen_p(rb_parser_t *p)
15789{
15790 return p->ruby__end__seen;
15791}
15792
15793int
15794rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15795{
15796 p->debug = flag;
15797 return flag;
15798}
15799#endif /* !RIPPER */
15800
15801#ifdef RIPPER
15802int
15803rb_ruby_parser_get_yydebug(rb_parser_t *p)
15804{
15805 return p->debug;
15806}
15807
15808void
15809rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15810{
15811 p->value = value;
15812}
15813
15814int
15815rb_ruby_parser_error_p(rb_parser_t *p)
15816{
15817 return p->error_p;
15818}
15819
15820VALUE
15821rb_ruby_parser_debug_output(rb_parser_t *p)
15822{
15823 return p->debug_output;
15824}
15825
15826void
15827rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15828{
15829 p->debug_output = output;
15830}
15831
15832VALUE
15833rb_ruby_parser_parsing_thread(rb_parser_t *p)
15834{
15835 return p->parsing_thread;
15836}
15837
15838void
15839rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15840{
15841 p->parsing_thread = parsing_thread;
15842}
15843
15844void
15845rb_ruby_parser_ripper_initialize(rb_parser_t *p, VALUE (*gets)(struct parser_params*,VALUE), VALUE input, VALUE sourcefile_string, const char *sourcefile, int sourceline)
15846{
15847 p->lex.gets = gets;
15848 p->lex.input = input;
15849 p->eofp = 0;
15850 p->ruby_sourcefile_string = sourcefile_string;
15851 p->ruby_sourcefile = sourcefile;
15852 p->ruby_sourceline = sourceline;
15853}
15854
15855VALUE
15856rb_ruby_parser_result(rb_parser_t *p)
15857{
15858 return p->result;
15859}
15860
15861rb_encoding *
15862rb_ruby_parser_enc(rb_parser_t *p)
15863{
15864 return p->enc;
15865}
15866
15867VALUE
15868rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15869{
15870 return p->ruby_sourcefile_string;
15871}
15872
15873int
15874rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15875{
15876 return p->ruby_sourceline;
15877}
15878
15879int
15880rb_ruby_parser_lex_state(rb_parser_t *p)
15881{
15882 return p->lex.state;
15883}
15884
15885void
15886rb_ruby_ripper_parse0(rb_parser_t *p)
15887{
15888 parser_prepare(p);
15889 p->ast = rb_ast_new();
15890 ripper_yyparse((void*)p);
15891 rb_ast_dispose(p->ast);
15892 p->ast = 0;
15893}
15894
15895int
15896rb_ruby_ripper_dedent_string(rb_parser_t *p, VALUE string, int width)
15897{
15898 return dedent_string(p, string, width);
15899}
15900
15901VALUE
15902rb_ruby_ripper_lex_get_str(rb_parser_t *p, VALUE s)
15903{
15904 return lex_get_str(p, s);
15905}
15906
15907int
15908rb_ruby_ripper_initialized_p(rb_parser_t *p)
15909{
15910 return p->lex.input != 0;
15911}
15912
15913void
15914rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15915{
15916 parser_initialize(p);
15917}
15918
15919long
15920rb_ruby_ripper_column(rb_parser_t *p)
15921{
15922 return p->lex.ptok - p->lex.pbeg;
15923}
15924
15925long
15926rb_ruby_ripper_token_len(rb_parser_t *p)
15927{
15928 return p->lex.pcur - p->lex.ptok;
15929}
15930
15931VALUE
15932rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15933{
15934 return p->lex.lastline;
15935}
15936
15937VALUE
15938rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15939{
15940 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15941}
15942
15943struct parser_params*
15944rb_ruby_ripper_parser_allocate(void)
15945{
15946 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15947}
15948#endif /* RIPPER */
15949
15950#ifndef RIPPER
15951#ifdef YYMALLOC
15952#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
15953/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
15954 * potential memory leak */
15955#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
15956#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
15957 (new)->cnt = (cnt), (ptr))
15958
15959void *
15960rb_parser_malloc(struct parser_params *p, size_t size)
15961{
15962 size_t cnt = HEAPCNT(1, size);
15963 rb_imemo_tmpbuf_t *n = NEWHEAP();
15964 void *ptr = xmalloc(size);
15965
15966 return ADD2HEAP(n, cnt, ptr);
15967}
15968
15969void *
15970rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
15971{
15972 size_t cnt = HEAPCNT(nelem, size);
15973 rb_imemo_tmpbuf_t *n = NEWHEAP();
15974 void *ptr = xcalloc(nelem, size);
15975
15976 return ADD2HEAP(n, cnt, ptr);
15977}
15978
15979void *
15980rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
15981{
15982 rb_imemo_tmpbuf_t *n;
15983 size_t cnt = HEAPCNT(1, size);
15984
15985 if (ptr && (n = p->heap) != NULL) {
15986 do {
15987 if (n->ptr == ptr) {
15988 n->ptr = ptr = xrealloc(ptr, size);
15989 if (n->cnt) n->cnt = cnt;
15990 return ptr;
15991 }
15992 } while ((n = n->next) != NULL);
15993 }
15994 n = NEWHEAP();
15995 ptr = xrealloc(ptr, size);
15996 return ADD2HEAP(n, cnt, ptr);
15997}
15998
15999void
16000rb_parser_free(struct parser_params *p, void *ptr)
16001{
16002 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
16003
16004 while ((n = *prev) != NULL) {
16005 if (n->ptr == ptr) {
16006 *prev = n->next;
16007 break;
16008 }
16009 prev = &n->next;
16010 }
16011}
16012#endif
16013
16014void
16015rb_parser_printf(struct parser_params *p, const char *fmt, ...)
16016{
16017 va_list ap;
16018 VALUE mesg = p->debug_buffer;
16019
16020 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
16021 va_start(ap, fmt);
16022 rb_str_vcatf(mesg, fmt, ap);
16023 va_end(ap);
16024 if (end_with_newline_p(p, mesg)) {
16025 rb_io_write(p->debug_output, mesg);
16026 p->debug_buffer = Qnil;
16027 }
16028}
16029
16030static void
16031parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
16032{
16033 va_list ap;
16034 int lineno, column;
16035
16036 if (loc) {
16037 lineno = loc->end_pos.lineno;
16038 column = loc->end_pos.column;
16039 }
16040 else {
16041 lineno = p->ruby_sourceline;
16042 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
16043 }
16044
16045 rb_io_flush(p->debug_output);
16046 p->error_p = 1;
16047 va_start(ap, fmt);
16048 p->error_buffer =
16049 rb_syntax_error_append(p->error_buffer,
16050 p->ruby_sourcefile_string,
16051 lineno, column,
16052 p->enc, fmt, ap);
16053 va_end(ap);
16054}
16055
16056static size_t
16057count_char(const char *str, int c)
16058{
16059 int n = 0;
16060 while (str[n] == c) ++n;
16061 return n;
16062}
16063
16064/*
16065 * strip enclosing double-quotes, same as the default yytnamerr except
16066 * for that single-quotes matching back-quotes do not stop stripping.
16067 *
16068 * "\"`class' keyword\"" => "`class' keyword"
16069 */
16070RUBY_FUNC_EXPORTED size_t
16071rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16072{
16073 if (*yystr == '"') {
16074 size_t yyn = 0, bquote = 0;
16075 const char *yyp = yystr;
16076
16077 while (*++yyp) {
16078 switch (*yyp) {
16079 case '`':
16080 if (!bquote) {
16081 bquote = count_char(yyp+1, '`') + 1;
16082 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16083 yyn += bquote;
16084 yyp += bquote - 1;
16085 break;
16086 }
16087 goto default_char;
16088
16089 case '\'':
16090 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16091 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16092 yyn += bquote;
16093 yyp += bquote - 1;
16094 bquote = 0;
16095 break;
16096 }
16097 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16098 if (yyres) memcpy(yyres + yyn, yyp, 3);
16099 yyn += 3;
16100 yyp += 2;
16101 break;
16102 }
16103 goto do_not_strip_quotes;
16104
16105 case ',':
16106 goto do_not_strip_quotes;
16107
16108 case '\\':
16109 if (*++yyp != '\\')
16110 goto do_not_strip_quotes;
16111 /* Fall through. */
16112 default_char:
16113 default:
16114 if (yyres)
16115 yyres[yyn] = *yyp;
16116 yyn++;
16117 break;
16118
16119 case '"':
16120 case '\0':
16121 if (yyres)
16122 yyres[yyn] = '\0';
16123 return yyn;
16124 }
16125 }
16126 do_not_strip_quotes: ;
16127 }
16128
16129 if (!yyres) return strlen(yystr);
16130
16131 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16132}
16133#endif
16134
16135#ifdef RIPPER
16136#ifdef RIPPER_DEBUG
16137/* :nodoc: */
16138static VALUE
16139ripper_validate_object(VALUE self, VALUE x)
16140{
16141 if (x == Qfalse) return x;
16142 if (x == Qtrue) return x;
16143 if (NIL_P(x)) return x;
16144 if (UNDEF_P(x))
16145 rb_raise(rb_eArgError, "Qundef given");
16146 if (FIXNUM_P(x)) return x;
16147 if (SYMBOL_P(x)) return x;
16148 switch (BUILTIN_TYPE(x)) {
16149 case T_STRING:
16150 case T_OBJECT:
16151 case T_ARRAY:
16152 case T_BIGNUM:
16153 case T_FLOAT:
16154 case T_COMPLEX:
16155 case T_RATIONAL:
16156 break;
16157 case T_NODE:
16158 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
16159 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
16160 }
16161 x = ((NODE *)x)->nd_rval;
16162 break;
16163 default:
16164 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
16165 (void *)x, rb_obj_classname(x));
16166 }
16167 if (!RBASIC_CLASS(x)) {
16168 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
16169 (void *)x, rb_builtin_type_name(TYPE(x)));
16170 }
16171 return x;
16172}
16173#endif
16174
16175#define validate(x) ((x) = get_value(x))
16176
16177static VALUE
16178ripper_dispatch0(struct parser_params *p, ID mid)
16179{
16180 return rb_funcall(p->value, mid, 0);
16181}
16182
16183static VALUE
16184ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16185{
16186 validate(a);
16187 return rb_funcall(p->value, mid, 1, a);
16188}
16189
16190static VALUE
16191ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16192{
16193 validate(a);
16194 validate(b);
16195 return rb_funcall(p->value, mid, 2, a, b);
16196}
16197
16198static VALUE
16199ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16200{
16201 validate(a);
16202 validate(b);
16203 validate(c);
16204 return rb_funcall(p->value, mid, 3, a, b, c);
16205}
16206
16207static VALUE
16208ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16209{
16210 validate(a);
16211 validate(b);
16212 validate(c);
16213 validate(d);
16214 return rb_funcall(p->value, mid, 4, a, b, c, d);
16215}
16216
16217static VALUE
16218ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16219{
16220 validate(a);
16221 validate(b);
16222 validate(c);
16223 validate(d);
16224 validate(e);
16225 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16226}
16227
16228static VALUE
16229ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16230{
16231 validate(a);
16232 validate(b);
16233 validate(c);
16234 validate(d);
16235 validate(e);
16236 validate(f);
16237 validate(g);
16238 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16239}
16240
16241void
16242ripper_error(struct parser_params *p)
16243{
16244 p->error_p = TRUE;
16245}
16246
16247VALUE
16248ripper_value(struct parser_params *p)
16249{
16250 (void)yystpcpy; /* may not used in newer bison */
16251
16252 return p->value;
16253}
16254
16255#endif /* RIPPER */
16256/*
16257 * Local variables:
16258 * mode: c
16259 * c-file-style: "ruby"
16260 * End:
16261 */