Ruby: parse.y Source File

Ruby 3.3.4p94 (2024-07-09 revision be1089c8ec5ba40e09b1553e36b3174bf4014d9d)

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) goto eof;
8051 else if (!ISASCII(c)) {
8052 tokskip_mbchar(p);
8053 goto eof;
8054 }
8055 else {
8056 int c2 = escaped_control_code(c);
8057 if (c2) {
8058 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8059 WARN_SPACE_CHAR(c2, "\\M-");
8060 }
8061 else {
8062 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8063 }
8064 }
8065 else if (ISCNTRL(c)) goto eof;
8066 return ((c & 0xff) | 0x80);
8067 }
8068
8069 case 'C':
8070 if ((c = nextc(p)) != '-') {
8071 goto eof;
8072 }
8073 case 'c':
8074 if (flags & ESCAPE_CONTROL) goto eof;
8075 if ((c = nextc(p))== '\\') {
8076 switch (peekc(p)) {
8077 case 'u': case 'U':
8078 nextc(p);
8079 goto eof;
8080 }
8081 c = read_escape(p, flags|ESCAPE_CONTROL);
8082 }
8083 else if (c == '?')
8084 return 0177;
8085 else if (c == -1) goto eof;
8086 else if (!ISASCII(c)) {
8087 tokskip_mbchar(p);
8088 goto eof;
8089 }
8090 else {
8091 int c2 = escaped_control_code(c);
8092 if (c2) {
8093 if (ISCNTRL(c)) {
8094 if (flags & ESCAPE_META) {
8095 WARN_SPACE_CHAR(c2, "\\M-");
8096 }
8097 else {
8098 WARN_SPACE_CHAR(c2, "");
8099 }
8100 }
8101 else {
8102 if (flags & ESCAPE_META) {
8103 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8104 }
8105 else {
8106 WARN_SPACE_CHAR(c2, "\\C-");
8107 }
8108 }
8109 }
8110 else if (ISCNTRL(c)) goto eof;
8111 }
8112 return c & 0x9f;
8113
8114 eof:
8115 case -1:
8116 yyerror0("Invalid escape character syntax");
8117 dispatch_scan_event(p, tSTRING_CONTENT);
8118 return '\0';
8119
8120 default:
8121 return c;
8122 }
8123}
8124
8125static void
8126tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8127{
8128 int len = rb_enc_codelen(c, enc);
8129 rb_enc_mbcput(c, tokspace(p, len), enc);
8130}
8131
8132static int
8133tokadd_escape(struct parser_params *p)
8134{
8135 int c;
8136 size_t numlen;
8137
8138 switch (c = nextc(p)) {
8139 case '\n':
8140 return 0; /* just ignore */
8141
8142 case '0': case '1': case '2': case '3': /* octal constant */
8143 case '4': case '5': case '6': case '7':
8144 {
8145 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8146 if (numlen == 0) goto eof;
8147 p->lex.pcur += numlen;
8148 tokcopy(p, (int)numlen + 1);
8149 }
8150 return 0;
8151
8152 case 'x': /* hex constant */
8153 {
8154 tok_hex(p, &numlen);
8155 if (numlen == 0) return -1;
8156 tokcopy(p, (int)numlen + 2);
8157 }
8158 return 0;
8159
8160 eof:
8161 case -1:
8162 yyerror0("Invalid escape character syntax");
8163 token_flush(p);
8164 return -1;
8165
8166 default:
8167 tokadd(p, '\\');
8168 tokadd(p, c);
8169 }
8170 return 0;
8171}
8172
8173static int
8174regx_options(struct parser_params *p)
8175{
8176 int kcode = 0;
8177 int kopt = 0;
8178 int options = 0;
8179 int c, opt, kc;
8180
8181 newtok(p);
8182 while (c = nextc(p), ISALPHA(c)) {
8183 if (c == 'o') {
8184 options |= RE_OPTION_ONCE;
8185 }
8186 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
8187 if (kc >= 0) {
8188 if (kc != rb_ascii8bit_encindex()) kcode = c;
8189 kopt = opt;
8190 }
8191 else {
8192 options |= opt;
8193 }
8194 }
8195 else {
8196 tokadd(p, c);
8197 }
8198 }
8199 options |= kopt;
8200 pushback(p, c);
8201 if (toklen(p)) {
8202 YYLTYPE loc = RUBY_INIT_YYLLOC();
8203 tokfix(p);
8204 compile_error(p, "unknown regexp option%s - %*s",
8205 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8206 parser_show_error_line(p, &loc);
8207 }
8208 return options | RE_OPTION_ENCODING(kcode);
8209}
8210
8211static int
8212tokadd_mbchar(struct parser_params *p, int c)
8213{
8214 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8215 if (len < 0) return -1;
8216 tokadd(p, c);
8217 p->lex.pcur += --len;
8218 if (len > 0) tokcopy(p, len);
8219 return c;
8220}
8221
8222static inline int
8223simple_re_meta(int c)
8224{
8225 switch (c) {
8226 case '$': case '*': case '+': case '.':
8227 case '?': case '^': case '|':
8228 case ')': case ']': case '}': case '>':
8229 return TRUE;
8230 default:
8231 return FALSE;
8232 }
8233}
8234
8235static int
8236parser_update_heredoc_indent(struct parser_params *p, int c)
8237{
8238 if (p->heredoc_line_indent == -1) {
8239 if (c == '\n') p->heredoc_line_indent = 0;
8240 }
8241 else {
8242 if (c == ' ') {
8243 p->heredoc_line_indent++;
8244 return TRUE;
8245 }
8246 else if (c == '\t') {
8247 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8248 p->heredoc_line_indent = w * TAB_WIDTH;
8249 return TRUE;
8250 }
8251 else if (c != '\n') {
8252 if (p->heredoc_indent > p->heredoc_line_indent) {
8253 p->heredoc_indent = p->heredoc_line_indent;
8254 }
8255 p->heredoc_line_indent = -1;
8256 }
8257 }
8258 return FALSE;
8259}
8260
8261static void
8262parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8263{
8264 YYLTYPE loc = RUBY_INIT_YYLLOC();
8265 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8266 compile_error(p, "%s mixed within %s source", n1, n2);
8267 parser_show_error_line(p, &loc);
8268}
8269
8270static void
8271parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8272{
8273 const char *pos = p->lex.pcur;
8274 p->lex.pcur = beg;
8275 parser_mixed_error(p, enc1, enc2);
8276 p->lex.pcur = pos;
8277}
8278
8279static inline char
8280nibble_char_upper(unsigned int c)
8281{
8282 c &= 0xf;
8283 return c + (c < 10 ? '0' : 'A' - 10);
8284}
8285
8286static int
8287tokadd_string(struct parser_params *p,
8288 int func, int term, int paren, long *nest,
8289 rb_encoding **encp, rb_encoding **enc)
8290{
8291 int c;
8292 bool erred = false;
8293#ifdef RIPPER
8294 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8295 int top_of_line = FALSE;
8296#endif
8297
8298#define mixed_error(enc1, enc2) \
8299 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8300#define mixed_escape(beg, enc1, enc2) \
8301 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8302
8303 while ((c = nextc(p)) != -1) {
8304 if (p->heredoc_indent > 0) {
8305 parser_update_heredoc_indent(p, c);
8306 }
8307#ifdef RIPPER
8308 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8309 pushback(p, c);
8310 break;
8311 }
8312#endif
8313
8314 if (paren && c == paren) {
8315 ++*nest;
8316 }
8317 else if (c == term) {
8318 if (!nest || !*nest) {
8319 pushback(p, c);
8320 break;
8321 }
8322 --*nest;
8323 }
8324 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8325 unsigned char c2 = *p->lex.pcur;
8326 if (c2 == '$' || c2 == '@' || c2 == '{') {
8327 pushback(p, c);
8328 break;
8329 }
8330 }
8331 else if (c == '\\') {
8332 c = nextc(p);
8333 switch (c) {
8334 case '\n':
8335 if (func & STR_FUNC_QWORDS) break;
8336 if (func & STR_FUNC_EXPAND) {
8337 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8338 continue;
8339 if (c == term) {
8340 c = '\\';
8341 goto terminate;
8342 }
8343 }
8344 tokadd(p, '\\');
8345 break;
8346
8347 case '\\':
8348 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8349 break;
8350
8351 case 'u':
8352 if ((func & STR_FUNC_EXPAND) == 0) {
8353 tokadd(p, '\\');
8354 break;
8355 }
8356 tokadd_utf8(p, enc, term,
8357 func & STR_FUNC_SYMBOL,
8358 func & STR_FUNC_REGEXP);
8359 continue;
8360
8361 default:
8362 if (c == -1) return -1;
8363 if (!ISASCII(c)) {
8364 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8365 goto non_ascii;
8366 }
8367 if (func & STR_FUNC_REGEXP) {
8368 switch (c) {
8369 case 'c':
8370 case 'C':
8371 case 'M': {
8372 pushback(p, c);
8373 c = read_escape(p, 0);
8374
8375 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8376 *t++ = '\\';
8377 *t++ = 'x';
8378 *t++ = nibble_char_upper(c >> 4);
8379 *t++ = nibble_char_upper(c);
8380 continue;
8381 }
8382 }
8383
8384 if (c == term && !simple_re_meta(c)) {
8385 tokadd(p, c);
8386 continue;
8387 }
8388 pushback(p, c);
8389 if ((c = tokadd_escape(p)) < 0)
8390 return -1;
8391 if (*enc && *enc != *encp) {
8392 mixed_escape(p->lex.ptok+2, *enc, *encp);
8393 }
8394 continue;
8395 }
8396 else if (func & STR_FUNC_EXPAND) {
8397 pushback(p, c);
8398 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8399 c = read_escape(p, 0);
8400 }
8401 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8402 /* ignore backslashed spaces in %w */
8403 }
8404 else if (c != term && !(paren && c == paren)) {
8405 tokadd(p, '\\');
8406 pushback(p, c);
8407 continue;
8408 }
8409 }
8410 }
8411 else if (!parser_isascii(p)) {
8412 non_ascii:
8413 if (!*enc) {
8414 *enc = *encp;
8415 }
8416 else if (*enc != *encp) {
8417 mixed_error(*enc, *encp);
8418 continue;
8419 }
8420 if (tokadd_mbchar(p, c) == -1) return -1;
8421 continue;
8422 }
8423 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8424 pushback(p, c);
8425 break;
8426 }
8427 if (c & 0x80) {
8428 if (!*enc) {
8429 *enc = *encp;
8430 }
8431 else if (*enc != *encp) {
8432 mixed_error(*enc, *encp);
8433 continue;
8434 }
8435 }
8436 tokadd(p, c);
8437#ifdef RIPPER
8438 top_of_line = (c == '\n');
8439#endif
8440 }
8441 terminate:
8442 if (*enc) *encp = *enc;
8443 return c;
8444}
8445
8446#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8447
8448#ifdef RIPPER
8449static void
8450flush_string_content(struct parser_params *p, rb_encoding *enc)
8451{
8452 VALUE content = yylval.val;
8453 if (!ripper_is_node_yylval(p, content))
8454 content = ripper_new_yylval(p, 0, 0, content);
8455 if (has_delayed_token(p)) {
8456 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8457 if (len > 0) {
8458 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8459 }
8460 dispatch_delayed_token(p, tSTRING_CONTENT);
8461 p->lex.ptok = p->lex.pcur;
8462 RNODE_RIPPER(content)->nd_rval = yylval.val;
8463 }
8464 dispatch_scan_event(p, tSTRING_CONTENT);
8465 if (yylval.val != content)
8466 RNODE_RIPPER(content)->nd_rval = yylval.val;
8467 yylval.val = content;
8468}
8469#else
8470static void
8471flush_string_content(struct parser_params *p, rb_encoding *enc)
8472{
8473 if (has_delayed_token(p)) {
8474 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8475 if (len > 0) {
8476 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8477 p->delayed.end_line = p->ruby_sourceline;
8478 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8479 }
8480 dispatch_delayed_token(p, tSTRING_CONTENT);
8481 p->lex.ptok = p->lex.pcur;
8482 }
8483 dispatch_scan_event(p, tSTRING_CONTENT);
8484}
8485#endif
8486
8487RUBY_FUNC_EXPORTED const uint_least32_t ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
8488/* this can be shared with ripper, since it's independent from struct
8489 * parser_params. */
8490#ifndef RIPPER
8491#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8492#define SPECIAL_PUNCT(idx) ( \
8493 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8494 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8495 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8496 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8497 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8498 BIT('0', idx))
8499const uint_least32_t ruby_global_name_punct_bits[] = {
8500 SPECIAL_PUNCT(0),
8501 SPECIAL_PUNCT(1),
8502 SPECIAL_PUNCT(2),
8503};
8504#undef BIT
8505#undef SPECIAL_PUNCT
8506#endif
8507
8508static enum yytokentype
8509parser_peek_variable_name(struct parser_params *p)
8510{
8511 int c;
8512 const char *ptr = p->lex.pcur;
8513
8514 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8515 c = *ptr++;
8516 switch (c) {
8517 case '$':
8518 if ((c = *ptr) == '-') {
8519 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8520 c = *ptr;
8521 }
8522 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8523 return tSTRING_DVAR;
8524 }
8525 break;
8526 case '@':
8527 if ((c = *ptr) == '@') {
8528 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8529 c = *ptr;
8530 }
8531 break;
8532 case '{':
8533 p->lex.pcur = ptr;
8534 p->command_start = TRUE;
8535 return tSTRING_DBEG;
8536 default:
8537 return 0;
8538 }
8539 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8540 return tSTRING_DVAR;
8541 return 0;
8542}
8543
8544#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8545#define IS_END() IS_lex_state(EXPR_END_ANY)
8546#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8547#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8548#define IS_LABEL_POSSIBLE() (\
8549 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8550 IS_ARG())
8551#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8552#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8553
8554static inline enum yytokentype
8555parser_string_term(struct parser_params *p, int func)
8556{
8557 xfree(p->lex.strterm);
8558 p->lex.strterm = 0;
8559 if (func & STR_FUNC_REGEXP) {
8560 set_yylval_num(regx_options(p));
8561 dispatch_scan_event(p, tREGEXP_END);
8562 SET_LEX_STATE(EXPR_END);
8563 return tREGEXP_END;
8564 }
8565 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8566 nextc(p);
8567 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8568 return tLABEL_END;
8569 }
8570 SET_LEX_STATE(EXPR_END);
8571 return tSTRING_END;
8572}
8573
8574static enum yytokentype
8575parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8576{
8577 int func = quote->func;
8578 int term = quote->term;
8579 int paren = quote->paren;
8580 int c, space = 0;
8581 rb_encoding *enc = p->enc;
8582 rb_encoding *base_enc = 0;
8583 VALUE lit;
8584
8585 if (func & STR_FUNC_TERM) {
8586 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8587 SET_LEX_STATE(EXPR_END);
8588 xfree(p->lex.strterm);
8589 p->lex.strterm = 0;
8590 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8591 }
8592 c = nextc(p);
8593 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8594 while (c != '\n' && ISSPACE(c = nextc(p)));
8595 space = 1;
8596 }
8597 if (func & STR_FUNC_LIST) {
8598 quote->func &= ~STR_FUNC_LIST;
8599 space = 1;
8600 }
8601 if (c == term && !quote->nest) {
8602 if (func & STR_FUNC_QWORDS) {
8603 quote->func |= STR_FUNC_TERM;
8604 pushback(p, c); /* dispatch the term at tSTRING_END */
8605 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8606 return ' ';
8607 }
8608 return parser_string_term(p, func);
8609 }
8610 if (space) {
8611 if (!ISSPACE(c)) pushback(p, c);
8612 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8613 return ' ';
8614 }
8615 newtok(p);
8616 if ((func & STR_FUNC_EXPAND) && c == '#') {
8617 enum yytokentype t = parser_peek_variable_name(p);
8618 if (t) return t;
8619 tokadd(p, '#');
8620 c = nextc(p);
8621 }
8622 pushback(p, c);
8623 if (tokadd_string(p, func, term, paren, &quote->nest,
8624 &enc, &base_enc) == -1) {
8625 if (p->eofp) {
8626#ifndef RIPPER
8627# define unterminated_literal(mesg) yyerror0(mesg)
8628#else
8629# define unterminated_literal(mesg) compile_error(p, mesg)
8630#endif
8631 literal_flush(p, p->lex.pcur);
8632 if (func & STR_FUNC_QWORDS) {
8633 /* no content to add, bailing out here */
8634 unterminated_literal("unterminated list meets end of file");
8635 xfree(p->lex.strterm);
8636 p->lex.strterm = 0;
8637 return tSTRING_END;
8638 }
8639 if (func & STR_FUNC_REGEXP) {
8640 unterminated_literal("unterminated regexp meets end of file");
8641 }
8642 else {
8643 unterminated_literal("unterminated string meets end of file");
8644 }
8645 quote->func |= STR_FUNC_TERM;
8646 }
8647 }
8648
8649 tokfix(p);
8650 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8651 set_yylval_str(lit);
8652 flush_string_content(p, enc);
8653
8654 return tSTRING_CONTENT;
8655}
8656
8657static enum yytokentype
8658heredoc_identifier(struct parser_params *p)
8659{
8660 /*
8661 * term_len is length of `<<"END"` except `END`,
8662 * in this case term_len is 4 (<, <, " and ").
8663 */
8664 long len, offset = p->lex.pcur - p->lex.pbeg;
8665 int c = nextc(p), term, func = 0, quote = 0;
8666 enum yytokentype token = tSTRING_BEG;
8667 int indent = 0;
8668
8669 if (c == '-') {
8670 c = nextc(p);
8671 func = STR_FUNC_INDENT;
8672 offset++;
8673 }
8674 else if (c == '~') {
8675 c = nextc(p);
8676 func = STR_FUNC_INDENT;
8677 offset++;
8678 indent = INT_MAX;
8679 }
8680 switch (c) {
8681 case '\'':
8682 func |= str_squote; goto quoted;
8683 case '"':
8684 func |= str_dquote; goto quoted;
8685 case '`':
8686 token = tXSTRING_BEG;
8687 func |= str_xquote; goto quoted;
8688
8689 quoted:
8690 quote++;
8691 offset++;
8692 term = c;
8693 len = 0;
8694 while ((c = nextc(p)) != term) {
8695 if (c == -1 || c == '\r' || c == '\n') {
8696 yyerror0("unterminated here document identifier");
8697 return -1;
8698 }
8699 }
8700 break;
8701
8702 default:
8703 if (!parser_is_identchar(p)) {
8704 pushback(p, c);
8705 if (func & STR_FUNC_INDENT) {
8706 pushback(p, indent > 0 ? '~' : '-');
8707 }
8708 return 0;
8709 }
8710 func |= str_dquote;
8711 do {
8712 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8713 if (n < 0) return 0;
8714 p->lex.pcur += --n;
8715 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8716 pushback(p, c);
8717 break;
8718 }
8719
8720 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8721 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8722 yyerror0("too long here document identifier");
8723 dispatch_scan_event(p, tHEREDOC_BEG);
8724 lex_goto_eol(p);
8725
8726 p->lex.strterm = new_heredoc(p);
8727 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8728 here->offset = offset;
8729 here->sourceline = p->ruby_sourceline;
8730 here->length = (unsigned)len;
8731 here->quote = quote;
8732 here->func = func;
8733 here->lastline = p->lex.lastline;
8734 rb_ast_add_mark_object(p->ast, p->lex.lastline);
8735
8736 token_flush(p);
8737 p->heredoc_indent = indent;
8738 p->heredoc_line_indent = 0;
8739 return token;
8740}
8741
8742static void
8743heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8744{
8745 VALUE line;
8746 rb_strterm_t *term = p->lex.strterm;
8747
8748 p->lex.strterm = 0;
8749 line = here->lastline;
8750 p->lex.lastline = line;
8751 p->lex.pbeg = RSTRING_PTR(line);
8752 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
8753 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8754 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8755 p->heredoc_end = p->ruby_sourceline;
8756 p->ruby_sourceline = (int)here->sourceline;
8757 if (p->eofp) p->lex.nextline = Qnil;
8758 p->eofp = 0;
8759 xfree(term);
8760 rb_ast_delete_mark_object(p->ast, line);
8761}
8762
8763static int
8764dedent_string(struct parser_params *p, VALUE string, int width)
8765{
8766 char *str;
8767 long len;
8768 int i, col = 0;
8769
8770 RSTRING_GETMEM(string, str, len);
8771 for (i = 0; i < len && col < width; i++) {
8772 if (str[i] == ' ') {
8773 col++;
8774 }
8775 else if (str[i] == '\t') {
8776 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8777 if (n > width) break;
8778 col = n;
8779 }
8780 else {
8781 break;
8782 }
8783 }
8784 if (!i) return 0;
8785 rb_str_modify(string);
8786 str = RSTRING_PTR(string);
8787 if (RSTRING_LEN(string) != len)
8788 rb_fatal("literal string changed: %+"PRIsVALUE, string);
8789 MEMMOVE(str, str + i, char, len - i);
8790 rb_str_set_len(string, len - i);
8791 return i;
8792}
8793
8794#ifndef RIPPER
8795static NODE *
8796heredoc_dedent(struct parser_params *p, NODE *root)
8797{
8798 NODE *node, *str_node, *prev_node;
8799 int indent = p->heredoc_indent;
8800 VALUE prev_lit = 0;
8801
8802 if (indent <= 0) return root;
8803 p->heredoc_indent = 0;
8804 if (!root) return root;
8805
8806 prev_node = node = str_node = root;
8807 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8808
8809 while (str_node) {
8810 VALUE lit = RNODE_LIT(str_node)->nd_lit;
8811 if (nd_fl_newline(str_node)) {
8812 dedent_string(p, lit, indent);
8813 }
8814 if (!prev_lit) {
8815 prev_lit = lit;
8816 }
8817 else if (!literal_concat0(p, prev_lit, lit)) {
8818 return 0;
8819 }
8820 else {
8821 NODE *end = RNODE_LIST(node)->as.nd_end;
8822 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8823 if (!node) {
8824 if (nd_type_p(prev_node, NODE_DSTR))
8825 nd_set_type(prev_node, NODE_STR);
8826 break;
8827 }
8828 RNODE_LIST(node)->as.nd_end = end;
8829 goto next_str;
8830 }
8831
8832 str_node = 0;
8833 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8834 next_str:
8835 if (!nd_type_p(node, NODE_LIST)) break;
8836 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8837 enum node_type type = nd_type(str_node);
8838 if (type == NODE_STR || type == NODE_DSTR) break;
8839 prev_lit = 0;
8840 str_node = 0;
8841 }
8842 }
8843 }
8844 return root;
8845}
8846#else /* RIPPER */
8847static VALUE
8848heredoc_dedent(struct parser_params *p, VALUE array)
8849{
8850 int indent = p->heredoc_indent;
8851
8852 if (indent <= 0) return array;
8853 p->heredoc_indent = 0;
8854 dispatch2(heredoc_dedent, array, INT2NUM(indent));
8855 return array;
8856}
8857#endif
8858
8859static int
8860whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8861{
8862 const char *beg = p->lex.pbeg;
8863 const char *ptr = p->lex.pend;
8864
8865 if (ptr - beg < len) return FALSE;
8866 if (ptr > beg && ptr[-1] == '\n') {
8867 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8868 if (ptr - beg < len) return FALSE;
8869 }
8870 if (strncmp(eos, ptr -= len, len)) return FALSE;
8871 if (indent) {
8872 while (beg < ptr && ISSPACE(*beg)) beg++;
8873 }
8874 return beg == ptr;
8875}
8876
8877static int
8878word_match_p(struct parser_params *p, const char *word, long len)
8879{
8880 if (strncmp(p->lex.pcur, word, len)) return 0;
8881 if (lex_eol_n_p(p, len)) return 1;
8882 int c = (unsigned char)p->lex.pcur[len];
8883 if (ISSPACE(c)) return 1;
8884 switch (c) {
8885 case '\0': case '\004': case '\032': return 1;
8886 }
8887 return 0;
8888}
8889
8890#define NUM_SUFFIX_R (1<<0)
8891#define NUM_SUFFIX_I (1<<1)
8892#define NUM_SUFFIX_ALL 3
8893
8894static int
8895number_literal_suffix(struct parser_params *p, int mask)
8896{
8897 int c, result = 0;
8898 const char *lastp = p->lex.pcur;
8899
8900 while ((c = nextc(p)) != -1) {
8901 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8902 result |= (mask & NUM_SUFFIX_I);
8903 mask &= ~NUM_SUFFIX_I;
8904 /* r after i, rational of complex is disallowed */
8905 mask &= ~NUM_SUFFIX_R;
8906 continue;
8907 }
8908 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8909 result |= (mask & NUM_SUFFIX_R);
8910 mask &= ~NUM_SUFFIX_R;
8911 continue;
8912 }
8913 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8914 p->lex.pcur = lastp;
8915 literal_flush(p, p->lex.pcur);
8916 return 0;
8917 }
8918 pushback(p, c);
8919 break;
8920 }
8921 return result;
8922}
8923
8924static enum yytokentype
8925set_number_literal(struct parser_params *p, VALUE v,
8926 enum yytokentype type, int suffix)
8927{
8928 if (suffix & NUM_SUFFIX_I) {
8929 v = rb_complex_raw(INT2FIX(0), v);
8930 type = tIMAGINARY;
8931 }
8932 set_yylval_literal(v);
8933 SET_LEX_STATE(EXPR_END);
8934 return type;
8935}
8936
8937static enum yytokentype
8938set_integer_literal(struct parser_params *p, VALUE v, int suffix)
8939{
8940 enum yytokentype type = tINTEGER;
8941 if (suffix & NUM_SUFFIX_R) {
8942 v = rb_rational_raw1(v);
8943 type = tRATIONAL;
8944 }
8945 return set_number_literal(p, v, type, suffix);
8946}
8947
8948#ifdef RIPPER
8949static void
8950dispatch_heredoc_end(struct parser_params *p)
8951{
8952 VALUE str;
8953 if (has_delayed_token(p))
8954 dispatch_delayed_token(p, tSTRING_CONTENT);
8955 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8956 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8957 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8958 lex_goto_eol(p);
8959 token_flush(p);
8960}
8961
8962#else
8963#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8964static void
8965parser_dispatch_heredoc_end(struct parser_params *p, int line)
8966{
8967 if (has_delayed_token(p))
8968 dispatch_delayed_token(p, tSTRING_CONTENT);
8969
8970 if (p->keep_tokens) {
8971 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8972 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8973 parser_append_tokens(p, str, tHEREDOC_END, line);
8974 }
8975
8976 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8977 lex_goto_eol(p);
8978 token_flush(p);
8979}
8980#endif
8981
8982static enum yytokentype
8983here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
8984{
8985 int c, func, indent = 0;
8986 const char *eos, *ptr, *ptr_end;
8987 long len;
8988 VALUE str = 0;
8989 rb_encoding *enc = p->enc;
8990 rb_encoding *base_enc = 0;
8991 int bol;
8992
8993 eos = RSTRING_PTR(here->lastline) + here->offset;
8994 len = here->length;
8995 indent = (func = here->func) & STR_FUNC_INDENT;
8996
8997 if ((c = nextc(p)) == -1) {
8998 error:
8999#ifdef RIPPER
9000 if (!has_delayed_token(p)) {
9001 dispatch_scan_event(p, tSTRING_CONTENT);
9002 }
9003 else {
9004 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9005 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
9006 int cr = ENC_CODERANGE_UNKNOWN;
9007 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9008 if (cr != ENC_CODERANGE_7BIT &&
9009 rb_is_usascii_enc(p->enc) &&
9010 enc != rb_utf8_encoding()) {
9011 enc = rb_ascii8bit_encoding();
9012 }
9013 }
9014 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
9015 }
9016 dispatch_delayed_token(p, tSTRING_CONTENT);
9017 }
9018 lex_goto_eol(p);
9019#endif
9020 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9021 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9022 (int)len, eos);
9023 token_flush(p);
9024 SET_LEX_STATE(EXPR_END);
9025 return tSTRING_END;
9026 }
9027 bol = was_bol(p);
9028 if (!bol) {
9029 /* not beginning of line, cannot be the terminator */
9030 }
9031 else if (p->heredoc_line_indent == -1) {
9032 /* `heredoc_line_indent == -1` means
9033 * - "after an interpolation in the same line", or
9034 * - "in a continuing line"
9035 */
9036 p->heredoc_line_indent = 0;
9037 }
9038 else if (whole_match_p(p, eos, len, indent)) {
9039 dispatch_heredoc_end(p);
9040 restore:
9041 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9042 token_flush(p);
9043 SET_LEX_STATE(EXPR_END);
9044 return tSTRING_END;
9045 }
9046
9047 if (!(func & STR_FUNC_EXPAND)) {
9048 do {
9049 ptr = RSTRING_PTR(p->lex.lastline);
9050 ptr_end = p->lex.pend;
9051 if (ptr_end > ptr) {
9052 switch (ptr_end[-1]) {
9053 case '\n':
9054 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9055 ptr_end++;
9056 break;
9057 }
9058 case '\r':
9059 --ptr_end;
9060 }
9061 }
9062
9063 if (p->heredoc_indent > 0) {
9064 long i = 0;
9065 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9066 i++;
9067 p->heredoc_line_indent = 0;
9068 }
9069
9070 if (str)
9071 rb_str_cat(str, ptr, ptr_end - ptr);
9072 else
9073 str = STR_NEW(ptr, ptr_end - ptr);
9074 if (!lex_eol_ptr_p(p, ptr_end)) rb_str_cat(str, "\n", 1);
9075 lex_goto_eol(p);
9076 if (p->heredoc_indent > 0) {
9077 goto flush_str;
9078 }
9079 if (nextc(p) == -1) {
9080 if (str) {
9081 str = 0;
9082 }
9083 goto error;
9084 }
9085 } while (!whole_match_p(p, eos, len, indent));
9086 }
9087 else {
9088 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9089 newtok(p);
9090 if (c == '#') {
9091 enum yytokentype t = parser_peek_variable_name(p);
9092 if (p->heredoc_line_indent != -1) {
9093 if (p->heredoc_indent > p->heredoc_line_indent) {
9094 p->heredoc_indent = p->heredoc_line_indent;
9095 }
9096 p->heredoc_line_indent = -1;
9097 }
9098 if (t) return t;
9099 tokadd(p, '#');
9100 c = nextc(p);
9101 }
9102 do {
9103 pushback(p, c);
9104 enc = p->enc;
9105 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9106 if (p->eofp) goto error;
9107 goto restore;
9108 }
9109 if (c != '\n') {
9110 if (c == '\\') p->heredoc_line_indent = -1;
9111 flush:
9112 str = STR_NEW3(tok(p), toklen(p), enc, func);
9113 flush_str:
9114 set_yylval_str(str);
9115#ifndef RIPPER
9116 if (bol) nd_set_fl_newline(yylval.node);
9117#endif
9118 flush_string_content(p, enc);
9119 return tSTRING_CONTENT;
9120 }
9121 tokadd(p, nextc(p));
9122 if (p->heredoc_indent > 0) {
9123 lex_goto_eol(p);
9124 goto flush;
9125 }
9126 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9127 if ((c = nextc(p)) == -1) goto error;
9128 } while (!whole_match_p(p, eos, len, indent));
9129 str = STR_NEW3(tok(p), toklen(p), enc, func);
9130 }
9131 dispatch_heredoc_end(p);
9132#ifdef RIPPER
9133 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
9134 yylval.val, str);
9135#endif
9136 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9137 token_flush(p);
9138 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9139 set_yylval_str(str);
9140#ifndef RIPPER
9141 if (bol) nd_set_fl_newline(yylval.node);
9142#endif
9143 return tSTRING_CONTENT;
9144}
9145
9146#include "lex.c"
9147
9148static int
9149arg_ambiguous(struct parser_params *p, char c)
9150{
9151#ifndef RIPPER
9152 if (c == '/') {
9153 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
9154 }
9155 else {
9156 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
9157 }
9158#else
9159 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9160#endif
9161 return TRUE;
9162}
9163
9164static ID
9165#ifndef RIPPER
9166formal_argument(struct parser_params *p, ID lhs)
9167#else
9168formal_argument(struct parser_params *p, VALUE lhs)
9169#endif
9170{
9171 ID id = get_id(lhs);
9172
9173 switch (id_type(id)) {
9174 case ID_LOCAL:
9175 break;
9176#ifndef RIPPER
9177# define ERR(mesg) yyerror0(mesg)
9178#else
9179# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
9180#endif
9181 case ID_CONST:
9182 ERR("formal argument cannot be a constant");
9183 return 0;
9184 case ID_INSTANCE:
9185 ERR("formal argument cannot be an instance variable");
9186 return 0;
9187 case ID_GLOBAL:
9188 ERR("formal argument cannot be a global variable");
9189 return 0;
9190 case ID_CLASS:
9191 ERR("formal argument cannot be a class variable");
9192 return 0;
9193 default:
9194 ERR("formal argument must be local variable");
9195 return 0;
9196#undef ERR
9197 }
9198 shadowing_lvar(p, id);
9199 return lhs;
9200}
9201
9202static int
9203lvar_defined(struct parser_params *p, ID id)
9204{
9205 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9206}
9207
9208/* emacsen -*- hack */
9209static long
9210parser_encode_length(struct parser_params *p, const char *name, long len)
9211{
9212 long nlen;
9213
9214 if (len > 5 && name[nlen = len - 5] == '-') {
9215 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9216 return nlen;
9217 }
9218 if (len > 4 && name[nlen = len - 4] == '-') {
9219 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9220 return nlen;
9221 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9222 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9223 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9224 return nlen;
9225 }
9226 return len;
9227}
9228
9229static void
9230parser_set_encode(struct parser_params *p, const char *name)
9231{
9232 int idx = rb_enc_find_index(name);
9233 rb_encoding *enc;
9234 VALUE excargs[3];
9235
9236 if (idx < 0) {
9237 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9238 error:
9239 excargs[0] = rb_eArgError;
9240 excargs[2] = rb_make_backtrace();
9241 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9242 rb_exc_raise(rb_make_exception(3, excargs));
9243 }
9244 enc = rb_enc_from_index(idx);
9245 if (!rb_enc_asciicompat(enc)) {
9246 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9247 goto error;
9248 }
9249 p->enc = enc;
9250#ifndef RIPPER
9251 if (p->debug_lines) {
9252 VALUE lines = p->debug_lines;
9253 long i, n = RARRAY_LEN(lines);
9254 for (i = 0; i < n; ++i) {
9255 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
9256 }
9257 }
9258#endif
9259}
9260
9261static int
9262comment_at_top(struct parser_params *p)
9263{
9264 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
9265 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
9266 while (ptr < ptr_end) {
9267 if (!ISSPACE(*ptr)) return 0;
9268 ptr++;
9269 }
9270 return 1;
9271}
9272
9273typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9274typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9275
9276static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9277
9278static void
9279magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9280{
9281 if (!comment_at_top(p)) {
9282 return;
9283 }
9284 parser_set_encode(p, val);
9285}
9286
9287static int
9288parser_get_bool(struct parser_params *p, const char *name, const char *val)
9289{
9290 switch (*val) {
9291 case 't': case 'T':
9292 if (STRCASECMP(val, "true") == 0) {
9293 return TRUE;
9294 }
9295 break;
9296 case 'f': case 'F':
9297 if (STRCASECMP(val, "false") == 0) {
9298 return FALSE;
9299 }
9300 break;
9301 }
9302 return parser_invalid_pragma_value(p, name, val);
9303}
9304
9305static int
9306parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9307{
9308 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9309 return -1;
9310}
9311
9312static void
9313parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9314{
9315 int b = parser_get_bool(p, name, val);
9316 if (b >= 0) p->token_info_enabled = b;
9317}
9318
9319static void
9320parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9321{
9322 int b;
9323
9324 if (p->token_seen) {
9325 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
9326 return;
9327 }
9328
9329 b = parser_get_bool(p, name, val);
9330 if (b < 0) return;
9331
9332 p->frozen_string_literal = b;
9333}
9334
9335static void
9336parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9337{
9338 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9339 if (*s == ' ' || *s == '\t') continue;
9340 if (*s == '#') break;
9341 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
9342 return;
9343 }
9344
9345 switch (*val) {
9346 case 'n': case 'N':
9347 if (STRCASECMP(val, "none") == 0) {
9348 p->ctxt.shareable_constant_value = shareable_none;
9349 return;
9350 }
9351 break;
9352 case 'l': case 'L':
9353 if (STRCASECMP(val, "literal") == 0) {
9354 p->ctxt.shareable_constant_value = shareable_literal;
9355 return;
9356 }
9357 break;
9358 case 'e': case 'E':
9359 if (STRCASECMP(val, "experimental_copy") == 0) {
9360 p->ctxt.shareable_constant_value = shareable_copy;
9361 return;
9362 }
9363 if (STRCASECMP(val, "experimental_everything") == 0) {
9364 p->ctxt.shareable_constant_value = shareable_everything;
9365 return;
9366 }
9367 break;
9368 }
9369 parser_invalid_pragma_value(p, name, val);
9370}
9371
9372# if WARN_PAST_SCOPE
9373static void
9374parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9375{
9376 int b = parser_get_bool(p, name, val);
9377 if (b >= 0) p->past_scope_enabled = b;
9378}
9379# endif
9380
9381struct magic_comment {
9382 const char *name;
9383 rb_magic_comment_setter_t func;
9384 rb_magic_comment_length_t length;
9385};
9386
9387static const struct magic_comment magic_comments[] = {
9388 {"coding", magic_comment_encoding, parser_encode_length},
9389 {"encoding", magic_comment_encoding, parser_encode_length},
9390 {"frozen_string_literal", parser_set_frozen_string_literal},
9391 {"shareable_constant_value", parser_set_shareable_constant_value},
9392 {"warn_indent", parser_set_token_info},
9393# if WARN_PAST_SCOPE
9394 {"warn_past_scope", parser_set_past_scope},
9395# endif
9396};
9397
9398static const char *
9399magic_comment_marker(const char *str, long len)
9400{
9401 long i = 2;
9402
9403 while (i < len) {
9404 switch (str[i]) {
9405 case '-':
9406 if (str[i-1] == '*' && str[i-2] == '-') {
9407 return str + i + 1;
9408 }
9409 i += 2;
9410 break;
9411 case '*':
9412 if (i + 1 >= len) return 0;
9413 if (str[i+1] != '-') {
9414 i += 4;
9415 }
9416 else if (str[i-1] != '-') {
9417 i += 2;
9418 }
9419 else {
9420 return str + i + 2;
9421 }
9422 break;
9423 default:
9424 i += 3;
9425 break;
9426 }
9427 }
9428 return 0;
9429}
9430
9431static int
9432parser_magic_comment(struct parser_params *p, const char *str, long len)
9433{
9434 int indicator = 0;
9435 VALUE name = 0, val = 0;
9436 const char *beg, *end, *vbeg, *vend;
9437#define str_copy(_s, _p, _n) ((_s) \
9438 ? (void)(rb_str_resize((_s), (_n)), \
9439 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9440 : (void)((_s) = STR_NEW((_p), (_n))))
9441
9442 if (len <= 7) return FALSE;
9443 if (!!(beg = magic_comment_marker(str, len))) {
9444 if (!(end = magic_comment_marker(beg, str + len - beg)))
9445 return FALSE;
9446 indicator = TRUE;
9447 str = beg;
9448 len = end - beg - 3;
9449 }
9450
9451 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9452 while (len > 0) {
9453 const struct magic_comment *mc = magic_comments;
9454 char *s;
9455 int i;
9456 long n = 0;
9457
9458 for (; len > 0 && *str; str++, --len) {
9459 switch (*str) {
9460 case '\'': case '"': case ':': case ';':
9461 continue;
9462 }
9463 if (!ISSPACE(*str)) break;
9464 }
9465 for (beg = str; len > 0; str++, --len) {
9466 switch (*str) {
9467 case '\'': case '"': case ':': case ';':
9468 break;
9469 default:
9470 if (ISSPACE(*str)) break;
9471 continue;
9472 }
9473 break;
9474 }
9475 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9476 if (!len) break;
9477 if (*str != ':') {
9478 if (!indicator) return FALSE;
9479 continue;
9480 }
9481
9482 do str++; while (--len > 0 && ISSPACE(*str));
9483 if (!len) break;
9484 if (*str == '"') {
9485 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9486 if (*str == '\\') {
9487 --len;
9488 ++str;
9489 }
9490 }
9491 vend = str;
9492 if (len) {
9493 --len;
9494 ++str;
9495 }
9496 }
9497 else {
9498 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9499 vend = str;
9500 }
9501 if (indicator) {
9502 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9503 }
9504 else {
9505 while (len > 0 && (ISSPACE(*str))) --len, str++;
9506 if (len) return FALSE;
9507 }
9508
9509 n = end - beg;
9510 str_copy(name, beg, n);
9511 s = RSTRING_PTR(name);
9512 for (i = 0; i < n; ++i) {
9513 if (s[i] == '-') s[i] = '_';
9514 }
9515 do {
9516 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9517 n = vend - vbeg;
9518 if (mc->length) {
9519 n = (*mc->length)(p, vbeg, n);
9520 }
9521 str_copy(val, vbeg, n);
9522 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9523 break;
9524 }
9525 } while (++mc < magic_comments + numberof(magic_comments));
9526#ifdef RIPPER
9527 str_copy(val, vbeg, vend - vbeg);
9528 dispatch2(magic_comment, name, val);
9529#endif
9530 }
9531
9532 return TRUE;
9533}
9534
9535static void
9536set_file_encoding(struct parser_params *p, const char *str, const char *send)
9537{
9538 int sep = 0;
9539 const char *beg = str;
9540 VALUE s;
9541
9542 for (;;) {
9543 if (send - str <= 6) return;
9544 switch (str[6]) {
9545 case 'C': case 'c': str += 6; continue;
9546 case 'O': case 'o': str += 5; continue;
9547 case 'D': case 'd': str += 4; continue;
9548 case 'I': case 'i': str += 3; continue;
9549 case 'N': case 'n': str += 2; continue;
9550 case 'G': case 'g': str += 1; continue;
9551 case '=': case ':':
9552 sep = 1;
9553 str += 6;
9554 break;
9555 default:
9556 str += 6;
9557 if (ISSPACE(*str)) break;
9558 continue;
9559 }
9560 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9561 sep = 0;
9562 }
9563 for (;;) {
9564 do {
9565 if (++str >= send) return;
9566 } while (ISSPACE(*str));
9567 if (sep) break;
9568 if (*str != '=' && *str != ':') return;
9569 sep = 1;
9570 str++;
9571 }
9572 beg = str;
9573 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9574 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9575 parser_set_encode(p, RSTRING_PTR(s));
9576 rb_str_resize(s, 0);
9577}
9578
9579static void
9580parser_prepare(struct parser_params *p)
9581{
9582 int c = nextc0(p, FALSE);
9583 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9584 switch (c) {
9585 case '#':
9586 if (peek(p, '!')) p->has_shebang = 1;
9587 break;
9588 case 0xef: /* UTF-8 BOM marker */
9589 if (!lex_eol_n_p(p, 2) &&
9590 (unsigned char)p->lex.pcur[0] == 0xbb &&
9591 (unsigned char)p->lex.pcur[1] == 0xbf) {
9592 p->enc = rb_utf8_encoding();
9593 p->lex.pcur += 2;
9594#ifndef RIPPER
9595 if (p->debug_lines) {
9596 rb_enc_associate(p->lex.lastline, p->enc);
9597 }
9598#endif
9599 p->lex.pbeg = p->lex.pcur;
9600 token_flush(p);
9601 return;
9602 }
9603 break;
9604 case EOF:
9605 return;
9606 }
9607 pushback(p, c);
9608 p->enc = rb_enc_get(p->lex.lastline);
9609}
9610
9611#ifndef RIPPER
9612#define ambiguous_operator(tok, op, syn) ( \
9613 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
9614 rb_warning0("even though it seems like "syn""))
9615#else
9616#define ambiguous_operator(tok, op, syn) \
9617 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9618#endif
9619#define warn_balanced(tok, op, syn) ((void) \
9620 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9621 space_seen && !ISSPACE(c) && \
9622 (ambiguous_operator(tok, op, syn), 0)), \
9623 (enum yytokentype)(tok))
9624
9625static VALUE
9626parse_rational(struct parser_params *p, char *str, int len, int seen_point)
9627{
9628 VALUE v;
9629 char *point = &str[seen_point];
9630 size_t fraclen = len-seen_point-1;
9631 memmove(point, point+1, fraclen+1);
9632 v = rb_cstr_to_inum(str, 10, FALSE);
9633 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
9634}
9635
9636static enum yytokentype
9637no_digits(struct parser_params *p)
9638{
9639 yyerror0("numeric literal without digits");
9640 if (peek(p, '_')) nextc(p);
9641 /* dummy 0, for tUMINUS_NUM at numeric */
9642 return set_integer_literal(p, INT2FIX(0), 0);
9643}
9644
9645static enum yytokentype
9646parse_numeric(struct parser_params *p, int c)
9647{
9648 int is_float, seen_point, seen_e, nondigit;
9649 int suffix;
9650
9651 is_float = seen_point = seen_e = nondigit = 0;
9652 SET_LEX_STATE(EXPR_END);
9653 newtok(p);
9654 if (c == '-' || c == '+') {
9655 tokadd(p, c);
9656 c = nextc(p);
9657 }
9658 if (c == '0') {
9659 int start = toklen(p);
9660 c = nextc(p);
9661 if (c == 'x' || c == 'X') {
9662 /* hexadecimal */
9663 c = nextc(p);
9664 if (c != -1 && ISXDIGIT(c)) {
9665 do {
9666 if (c == '_') {
9667 if (nondigit) break;
9668 nondigit = c;
9669 continue;
9670 }
9671 if (!ISXDIGIT(c)) break;
9672 nondigit = 0;
9673 tokadd(p, c);
9674 } while ((c = nextc(p)) != -1);
9675 }
9676 pushback(p, c);
9677 tokfix(p);
9678 if (toklen(p) == start) {
9679 return no_digits(p);
9680 }
9681 else if (nondigit) goto trailing_uc;
9682 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9683 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
9684 }
9685 if (c == 'b' || c == 'B') {
9686 /* binary */
9687 c = nextc(p);
9688 if (c == '0' || c == '1') {
9689 do {
9690 if (c == '_') {
9691 if (nondigit) break;
9692 nondigit = c;
9693 continue;
9694 }
9695 if (c != '0' && c != '1') break;
9696 nondigit = 0;
9697 tokadd(p, c);
9698 } while ((c = nextc(p)) != -1);
9699 }
9700 pushback(p, c);
9701 tokfix(p);
9702 if (toklen(p) == start) {
9703 return no_digits(p);
9704 }
9705 else if (nondigit) goto trailing_uc;
9706 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9707 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
9708 }
9709 if (c == 'd' || c == 'D') {
9710 /* decimal */
9711 c = nextc(p);
9712 if (c != -1 && ISDIGIT(c)) {
9713 do {
9714 if (c == '_') {
9715 if (nondigit) break;
9716 nondigit = c;
9717 continue;
9718 }
9719 if (!ISDIGIT(c)) break;
9720 nondigit = 0;
9721 tokadd(p, c);
9722 } while ((c = nextc(p)) != -1);
9723 }
9724 pushback(p, c);
9725 tokfix(p);
9726 if (toklen(p) == start) {
9727 return no_digits(p);
9728 }
9729 else if (nondigit) goto trailing_uc;
9730 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9731 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9732 }
9733 if (c == '_') {
9734 /* 0_0 */
9735 goto octal_number;
9736 }
9737 if (c == 'o' || c == 'O') {
9738 /* prefixed octal */
9739 c = nextc(p);
9740 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9741 return no_digits(p);
9742 }
9743 }
9744 if (c >= '0' && c <= '7') {
9745 /* octal */
9746 octal_number:
9747 do {
9748 if (c == '_') {
9749 if (nondigit) break;
9750 nondigit = c;
9751 continue;
9752 }
9753 if (c < '0' || c > '9') break;
9754 if (c > '7') goto invalid_octal;
9755 nondigit = 0;
9756 tokadd(p, c);
9757 } while ((c = nextc(p)) != -1);
9758 if (toklen(p) > start) {
9759 pushback(p, c);
9760 tokfix(p);
9761 if (nondigit) goto trailing_uc;
9762 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9763 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
9764 }
9765 if (nondigit) {
9766 pushback(p, c);
9767 goto trailing_uc;
9768 }
9769 }
9770 if (c > '7' && c <= '9') {
9771 invalid_octal:
9772 yyerror0("Invalid octal digit");
9773 }
9774 else if (c == '.' || c == 'e' || c == 'E') {
9775 tokadd(p, '0');
9776 }
9777 else {
9778 pushback(p, c);
9779 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9780 return set_integer_literal(p, INT2FIX(0), suffix);
9781 }
9782 }
9783
9784 for (;;) {
9785 switch (c) {
9786 case '0': case '1': case '2': case '3': case '4':
9787 case '5': case '6': case '7': case '8': case '9':
9788 nondigit = 0;
9789 tokadd(p, c);
9790 break;
9791
9792 case '.':
9793 if (nondigit) goto trailing_uc;
9794 if (seen_point || seen_e) {
9795 goto decode_num;
9796 }
9797 else {
9798 int c0 = nextc(p);
9799 if (c0 == -1 || !ISDIGIT(c0)) {
9800 pushback(p, c0);
9801 goto decode_num;
9802 }
9803 c = c0;
9804 }
9805 seen_point = toklen(p);
9806 tokadd(p, '.');
9807 tokadd(p, c);
9808 is_float++;
9809 nondigit = 0;
9810 break;
9811
9812 case 'e':
9813 case 'E':
9814 if (nondigit) {
9815 pushback(p, c);
9816 c = nondigit;
9817 goto decode_num;
9818 }
9819 if (seen_e) {
9820 goto decode_num;
9821 }
9822 nondigit = c;
9823 c = nextc(p);
9824 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9825 pushback(p, c);
9826 c = nondigit;
9827 nondigit = 0;
9828 goto decode_num;
9829 }
9830 tokadd(p, nondigit);
9831 seen_e++;
9832 is_float++;
9833 tokadd(p, c);
9834 nondigit = (c == '-' || c == '+') ? c : 0;
9835 break;
9836
9837 case '_': /* `_' in number just ignored */
9838 if (nondigit) goto decode_num;
9839 nondigit = c;
9840 break;
9841
9842 default:
9843 goto decode_num;
9844 }
9845 c = nextc(p);
9846 }
9847
9848 decode_num:
9849 pushback(p, c);
9850 if (nondigit) {
9851 trailing_uc:
9852 literal_flush(p, p->lex.pcur - 1);
9853 YYLTYPE loc = RUBY_INIT_YYLLOC();
9854 compile_error(p, "trailing `%c' in number", nondigit);
9855 parser_show_error_line(p, &loc);
9856 }
9857 tokfix(p);
9858 if (is_float) {
9859 enum yytokentype type = tFLOAT;
9860 VALUE v;
9861
9862 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9863 if (suffix & NUM_SUFFIX_R) {
9864 type = tRATIONAL;
9865 v = parse_rational(p, tok(p), toklen(p), seen_point);
9866 }
9867 else {
9868 double d = strtod(tok(p), 0);
9869 if (errno == ERANGE) {
9870 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9871 errno = 0;
9872 }
9873 v = DBL2NUM(d);
9874 }
9875 return set_number_literal(p, v, type, suffix);
9876 }
9877 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9878 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9879}
9880
9881static enum yytokentype
9882parse_qmark(struct parser_params *p, int space_seen)
9883{
9884 rb_encoding *enc;
9885 register int c;
9886 VALUE lit;
9887
9888 if (IS_END()) {
9889 SET_LEX_STATE(EXPR_VALUE);
9890 return '?';
9891 }
9892 c = nextc(p);
9893 if (c == -1) {
9894 compile_error(p, "incomplete character syntax");
9895 return 0;
9896 }
9897 if (rb_enc_isspace(c, p->enc)) {
9898 if (!IS_ARG()) {
9899 int c2 = escaped_control_code(c);
9900 if (c2) {
9901 WARN_SPACE_CHAR(c2, "?");
9902 }
9903 }
9904 ternary:
9905 pushback(p, c);
9906 SET_LEX_STATE(EXPR_VALUE);
9907 return '?';
9908 }
9909 newtok(p);
9910 enc = p->enc;
9911 if (!parser_isascii(p)) {
9912 if (tokadd_mbchar(p, c) == -1) return 0;
9913 }
9914 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
9915 !lex_eol_p(p) && is_identchar(p, p->lex.pcur, p->lex.pend, p->enc)) {
9916 if (space_seen) {
9917 const char *start = p->lex.pcur - 1, *ptr = start;
9918 do {
9919 int n = parser_precise_mbclen(p, ptr);
9920 if (n < 0) return -1;
9921 ptr += n;
9922 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9923 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
9924 " a conditional operator, put a space after `?'",
9925 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9926 }
9927 goto ternary;
9928 }
9929 else if (c == '\\') {
9930 if (peek(p, 'u')) {
9931 nextc(p);
9932 enc = rb_utf8_encoding();
9933 tokadd_utf8(p, &enc, -1, 0, 0);
9934 }
9935 else if (!ISASCII(c = peekc(p))) {
9936 nextc(p);
9937 if (tokadd_mbchar(p, c) == -1) return 0;
9938 }
9939 else {
9940 c = read_escape(p, 0);
9941 tokadd(p, c);
9942 }
9943 }
9944 else {
9945 tokadd(p, c);
9946 }
9947 tokfix(p);
9948 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9949 set_yylval_str(lit);
9950 SET_LEX_STATE(EXPR_END);
9951 return tCHAR;
9952}
9953
9954static enum yytokentype
9955parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9956{
9957 register int c;
9958 const char *ptok = p->lex.pcur;
9959
9960 if (IS_BEG()) {
9961 int term;
9962 int paren;
9963
9964 c = nextc(p);
9965 quotation:
9966 if (c == -1) goto unterminated;
9967 if (!ISALNUM(c)) {
9968 term = c;
9969 if (!ISASCII(c)) goto unknown;
9970 c = 'Q';
9971 }
9972 else {
9973 term = nextc(p);
9974 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
9975 unknown:
9976 pushback(p, term);
9977 c = parser_precise_mbclen(p, p->lex.pcur);
9978 if (c < 0) return 0;
9979 p->lex.pcur += c;
9980 yyerror0("unknown type of %string");
9981 return 0;
9982 }
9983 }
9984 if (term == -1) {
9985 unterminated:
9986 compile_error(p, "unterminated quoted string meets end of file");
9987 return 0;
9988 }
9989 paren = term;
9990 if (term == '(') term = ')';
9991 else if (term == '[') term = ']';
9992 else if (term == '{') term = '}';
9993 else if (term == '<') term = '>';
9994 else paren = 0;
9995
9996 p->lex.ptok = ptok-1;
9997 switch (c) {
9998 case 'Q':
9999 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10000 return tSTRING_BEG;
10001
10002 case 'q':
10003 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10004 return tSTRING_BEG;
10005
10006 case 'W':
10007 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10008 return tWORDS_BEG;
10009
10010 case 'w':
10011 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10012 return tQWORDS_BEG;
10013
10014 case 'I':
10015 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10016 return tSYMBOLS_BEG;
10017
10018 case 'i':
10019 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10020 return tQSYMBOLS_BEG;
10021
10022 case 'x':
10023 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10024 return tXSTRING_BEG;
10025
10026 case 'r':
10027 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10028 return tREGEXP_BEG;
10029
10030 case 's':
10031 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10032 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10033 return tSYMBEG;
10034
10035 default:
10036 yyerror0("unknown type of %string");
10037 return 0;
10038 }
10039 }
10040 if ((c = nextc(p)) == '=') {
10041 set_yylval_id('%');
10042 SET_LEX_STATE(EXPR_BEG);
10043 return tOP_ASGN;
10044 }
10045 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10046 goto quotation;
10047 }
10048 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10049 pushback(p, c);
10050 return warn_balanced('%', "%%", "string literal");
10051}
10052
10053static int
10054tokadd_ident(struct parser_params *p, int c)
10055{
10056 do {
10057 if (tokadd_mbchar(p, c) == -1) return -1;
10058 c = nextc(p);
10059 } while (parser_is_identchar(p));
10060 pushback(p, c);
10061 return 0;
10062}
10063
10064static ID
10065tokenize_ident(struct parser_params *p)
10066{
10067 ID ident = TOK_INTERN();
10068
10069 set_yylval_name(ident);
10070
10071 return ident;
10072}
10073
10074static int
10075parse_numvar(struct parser_params *p)
10076{
10077 size_t len;
10078 int overflow;
10079 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10080 const unsigned long nth_ref_max =
10081 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10082 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10083 * turned into a Fixnum, in compile.c */
10084
10085 if (overflow || n > nth_ref_max) {
10086 /* compile_error()? */
10087 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10088 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10089 }
10090 else {
10091 return (int)n;
10092 }
10093}
10094
10095static enum yytokentype
10096parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10097{
10098 const char *ptr = p->lex.pcur;
10099 register int c;
10100
10101 SET_LEX_STATE(EXPR_END);
10102 p->lex.ptok = ptr - 1; /* from '$' */
10103 newtok(p);
10104 c = nextc(p);
10105 switch (c) {
10106 case '_': /* $_: last read line string */
10107 c = nextc(p);
10108 if (parser_is_identchar(p)) {
10109 tokadd(p, '$');
10110 tokadd(p, '_');
10111 break;
10112 }
10113 pushback(p, c);
10114 c = '_';
10115 /* fall through */
10116 case '~': /* $~: match-data */
10117 case '*': /* $*: argv */
10118 case '$': /* $$: pid */
10119 case '?': /* $?: last status */
10120 case '!': /* $!: error string */
10121 case '@': /* $@: error position */
10122 case '/': /* $/: input record separator */
10123 case '\\': /* $\: output record separator */
10124 case ';': /* $;: field separator */
10125 case ',': /* $,: output field separator */
10126 case '.': /* $.: last read line number */
10127 case '=': /* $=: ignorecase */
10128 case ':': /* $:: load path */
10129 case '<': /* $<: reading filename */
10130 case '>': /* $>: default output handle */
10131 case '\"': /* $": already loaded files */
10132 tokadd(p, '$');
10133 tokadd(p, c);
10134 goto gvar;
10135
10136 case '-':
10137 tokadd(p, '$');
10138 tokadd(p, c);
10139 c = nextc(p);
10140 if (parser_is_identchar(p)) {
10141 if (tokadd_mbchar(p, c) == -1) return 0;
10142 }
10143 else {
10144 pushback(p, c);
10145 pushback(p, '-');
10146 return '$';
10147 }
10148 gvar:
10149 set_yylval_name(TOK_INTERN());
10150 return tGVAR;
10151
10152 case '&': /* $&: last match */
10153 case '`': /* $`: string before last match */
10154 case '\'': /* $': string after last match */
10155 case '+': /* $+: string matches last paren. */
10156 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10157 tokadd(p, '$');
10158 tokadd(p, c);
10159 goto gvar;
10160 }
10161 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10162 return tBACK_REF;
10163
10164 case '1': case '2': case '3':
10165 case '4': case '5': case '6':
10166 case '7': case '8': case '9':
10167 tokadd(p, '$');
10168 do {
10169 tokadd(p, c);
10170 c = nextc(p);
10171 } while (c != -1 && ISDIGIT(c));
10172 pushback(p, c);
10173 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10174 tokfix(p);
10175 c = parse_numvar(p);
10176 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10177 return tNTH_REF;
10178
10179 default:
10180 if (!parser_is_identchar(p)) {
10181 YYLTYPE loc = RUBY_INIT_YYLLOC();
10182 if (c == -1 || ISSPACE(c)) {
10183 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
10184 }
10185 else {
10186 pushback(p, c);
10187 compile_error(p, "`$%c' is not allowed as a global variable name", c);
10188 }
10189 parser_show_error_line(p, &loc);
10190 set_yylval_noname();
10191 return tGVAR;
10192 }
10193 /* fall through */
10194 case '0':
10195 tokadd(p, '$');
10196 }
10197
10198 if (tokadd_ident(p, c)) return 0;
10199 SET_LEX_STATE(EXPR_END);
10200 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10201 tokenize_ident(p);
10202 }
10203 else {
10204 compile_error(p, "`%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10205 set_yylval_noname();
10206 }
10207 return tGVAR;
10208}
10209
10210#ifndef RIPPER
10211static bool
10212parser_numbered_param(struct parser_params *p, int n)
10213{
10214 if (n < 0) return false;
10215
10216 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10217 return false;
10218 }
10219 if (p->max_numparam == ORDINAL_PARAM) {
10220 compile_error(p, "ordinary parameter is defined");
10221 return false;
10222 }
10223 struct vtable *args = p->lvtbl->args;
10224 if (p->max_numparam < n) {
10225 p->max_numparam = n;
10226 }
10227 while (n > args->pos) {
10228 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10229 }
10230 return true;
10231}
10232#endif
10233
10234static enum yytokentype
10235parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10236{
10237 const char *ptr = p->lex.pcur;
10238 enum yytokentype result = tIVAR;
10239 register int c = nextc(p);
10240 YYLTYPE loc;
10241
10242 p->lex.ptok = ptr - 1; /* from '@' */
10243 newtok(p);
10244 tokadd(p, '@');
10245 if (c == '@') {
10246 result = tCVAR;
10247 tokadd(p, '@');
10248 c = nextc(p);
10249 }
10250 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10251 if (c == -1 || !parser_is_identchar(p)) {
10252 pushback(p, c);
10253 RUBY_SET_YYLLOC(loc);
10254 if (result == tIVAR) {
10255 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
10256 }
10257 else {
10258 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
10259 }
10260 parser_show_error_line(p, &loc);
10261 set_yylval_noname();
10262 SET_LEX_STATE(EXPR_END);
10263 return result;
10264 }
10265 else if (ISDIGIT(c)) {
10266 pushback(p, c);
10267 RUBY_SET_YYLLOC(loc);
10268 if (result == tIVAR) {
10269 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
10270 }
10271 else {
10272 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
10273 }
10274 parser_show_error_line(p, &loc);
10275 set_yylval_noname();
10276 SET_LEX_STATE(EXPR_END);
10277 return result;
10278 }
10279
10280 if (tokadd_ident(p, c)) return 0;
10281 tokenize_ident(p);
10282 return result;
10283}
10284
10285static enum yytokentype
10286parse_ident(struct parser_params *p, int c, int cmd_state)
10287{
10288 enum yytokentype result;
10289 int mb = ENC_CODERANGE_7BIT;
10290 const enum lex_state_e last_state = p->lex.state;
10291 ID ident;
10292 int enforce_keyword_end = 0;
10293
10294 do {
10295 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
10296 if (tokadd_mbchar(p, c) == -1) return 0;
10297 c = nextc(p);
10298 } while (parser_is_identchar(p));
10299 if ((c == '!' || c == '?') && !peek(p, '=')) {
10300 result = tFID;
10301 tokadd(p, c);
10302 }
10303 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10304 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10305 result = tIDENTIFIER;
10306 tokadd(p, c);
10307 }
10308 else {
10309 result = tCONSTANT; /* assume provisionally */
10310 pushback(p, c);
10311 }
10312 tokfix(p);
10313
10314 if (IS_LABEL_POSSIBLE()) {
10315 if (IS_LABEL_SUFFIX(0)) {
10316 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10317 nextc(p);
10318 set_yylval_name(TOK_INTERN());
10319 return tLABEL;
10320 }
10321 }
10322
10323#ifndef RIPPER
10324 if (!NIL_P(peek_end_expect_token_locations(p))) {
10325 VALUE end_loc;
10326 int lineno, column;
10327 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10328
10329 end_loc = peek_end_expect_token_locations(p);
10330 lineno = NUM2INT(rb_ary_entry(end_loc, 0));
10331 column = NUM2INT(rb_ary_entry(end_loc, 1));
10332
10333 if (p->debug) {
10334 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10335 p->ruby_sourceline, beg_pos, lineno, column);
10336 }
10337
10338 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10339 const struct kwtable *kw;
10340
10341 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10342 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10343 enforce_keyword_end = 1;
10344 }
10345 }
10346 }
10347#endif
10348
10349 if (mb == ENC_CODERANGE_7BIT && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10350 const struct kwtable *kw;
10351
10352 /* See if it is a reserved word. */
10353 kw = rb_reserved_word(tok(p), toklen(p));
10354 if (kw) {
10355 enum lex_state_e state = p->lex.state;
10356 if (IS_lex_state_for(state, EXPR_FNAME)) {
10357 SET_LEX_STATE(EXPR_ENDFN);
10358 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10359 return kw->id[0];
10360 }
10361 SET_LEX_STATE(kw->state);
10362 if (IS_lex_state(EXPR_BEG)) {
10363 p->command_start = TRUE;
10364 }
10365 if (kw->id[0] == keyword_do) {
10366 if (lambda_beginning_p()) {
10367 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10368 return keyword_do_LAMBDA;
10369 }
10370 if (COND_P()) return keyword_do_cond;
10371 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10372 return keyword_do_block;
10373 return keyword_do;
10374 }
10375 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10376 return kw->id[0];
10377 else {
10378 if (kw->id[0] != kw->id[1])
10379 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10380 return kw->id[1];
10381 }
10382 }
10383 }
10384
10385 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10386 if (cmd_state) {
10387 SET_LEX_STATE(EXPR_CMDARG);
10388 }
10389 else {
10390 SET_LEX_STATE(EXPR_ARG);
10391 }
10392 }
10393 else if (p->lex.state == EXPR_FNAME) {
10394 SET_LEX_STATE(EXPR_ENDFN);
10395 }
10396 else {
10397 SET_LEX_STATE(EXPR_END);
10398 }
10399
10400 ident = tokenize_ident(p);
10401 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10402 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10403 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10404 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10405 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10406 }
10407 return result;
10408}
10409
10410static void
10411warn_cr(struct parser_params *p)
10412{
10413 if (!p->cr_seen) {
10414 p->cr_seen = TRUE;
10415 /* carried over with p->lex.nextline for nextc() */
10416 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10417 }
10418}
10419
10420static enum yytokentype
10421parser_yylex(struct parser_params *p)
10422{
10423 register int c;
10424 int space_seen = 0;
10425 int cmd_state;
10426 int label;
10427 enum lex_state_e last_state;
10428 int fallthru = FALSE;
10429 int token_seen = p->token_seen;
10430
10431 if (p->lex.strterm) {
10432 if (strterm_is_heredoc(p->lex.strterm)) {
10433 token_flush(p);
10434 return here_document(p, &p->lex.strterm->u.heredoc);
10435 }
10436 else {
10437 token_flush(p);
10438 return parse_string(p, &p->lex.strterm->u.literal);
10439 }
10440 }
10441 cmd_state = p->command_start;
10442 p->command_start = FALSE;
10443 p->token_seen = TRUE;
10444#ifndef RIPPER
10445 token_flush(p);
10446#endif
10447 retry:
10448 last_state = p->lex.state;
10449 switch (c = nextc(p)) {
10450 case '\0': /* NUL */
10451 case '\004': /* ^D */
10452 case '\032': /* ^Z */
10453 case -1: /* end of script. */
10454 p->eofp = 1;
10455#ifndef RIPPER
10456 if (!NIL_P(p->end_expect_token_locations) && RARRAY_LEN(p->end_expect_token_locations) > 0) {
10457 pop_end_expect_token_locations(p);
10458 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10459 return tDUMNY_END;
10460 }
10461#endif
10462 /* Set location for end-of-input because dispatch_scan_event is not called. */
10463 RUBY_SET_YYLLOC(*p->yylloc);
10464 return END_OF_INPUT;
10465
10466 /* white spaces */
10467 case '\r':
10468 warn_cr(p);
10469 /* fall through */
10470 case ' ': case '\t': case '\f':
10471 case '\13': /* '\v' */
10472 space_seen = 1;
10473 while ((c = nextc(p))) {
10474 switch (c) {
10475 case '\r':
10476 warn_cr(p);
10477 /* fall through */
10478 case ' ': case '\t': case '\f':
10479 case '\13': /* '\v' */
10480 break;
10481 default:
10482 goto outofloop;
10483 }
10484 }
10485 outofloop:
10486 pushback(p, c);
10487 dispatch_scan_event(p, tSP);
10488#ifndef RIPPER
10489 token_flush(p);
10490#endif
10491 goto retry;
10492
10493 case '#': /* it's a comment */
10494 p->token_seen = token_seen;
10495 /* no magic_comment in shebang line */
10496 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10497 if (comment_at_top(p)) {
10498 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10499 }
10500 }
10501 lex_goto_eol(p);
10502 dispatch_scan_event(p, tCOMMENT);
10503 fallthru = TRUE;
10504 /* fall through */
10505 case '\n':
10506 p->token_seen = token_seen;
10507 VALUE prevline = p->lex.lastline;
10508 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10509 !IS_lex_state(EXPR_LABELED));
10510 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10511 if (!fallthru) {
10512 dispatch_scan_event(p, tIGNORED_NL);
10513 }
10514 fallthru = FALSE;
10515 if (!c && p->ctxt.in_kwarg) {
10516 goto normal_newline;
10517 }
10518 goto retry;
10519 }
10520 while (1) {
10521 switch (c = nextc(p)) {
10522 case ' ': case '\t': case '\f': case '\r':
10523 case '\13': /* '\v' */
10524 space_seen = 1;
10525 break;
10526 case '#':
10527 pushback(p, c);
10528 if (space_seen) {
10529 dispatch_scan_event(p, tSP);
10530 token_flush(p);
10531 }
10532 goto retry;
10533 case '&':
10534 case '.': {
10535 dispatch_delayed_token(p, tIGNORED_NL);
10536 if (peek(p, '.') == (c == '&')) {
10537 pushback(p, c);
10538 dispatch_scan_event(p, tSP);
10539 goto retry;
10540 }
10541 }
10542 default:
10543 p->ruby_sourceline--;
10544 p->lex.nextline = p->lex.lastline;
10545 set_lastline(p, prevline);
10546 case -1: /* EOF no decrement*/
10547 lex_goto_eol(p);
10548 if (c != -1) {
10549 token_flush(p);
10550 RUBY_SET_YYLLOC(*p->yylloc);
10551 }
10552 goto normal_newline;
10553 }
10554 }
10555 normal_newline:
10556 p->command_start = TRUE;
10557 SET_LEX_STATE(EXPR_BEG);
10558 return '\n';
10559
10560 case '*':
10561 if ((c = nextc(p)) == '*') {
10562 if ((c = nextc(p)) == '=') {
10563 set_yylval_id(idPow);
10564 SET_LEX_STATE(EXPR_BEG);
10565 return tOP_ASGN;
10566 }
10567 pushback(p, c);
10568 if (IS_SPCARG(c)) {
10569 rb_warning0("`**' interpreted as argument prefix");
10570 c = tDSTAR;
10571 }
10572 else if (IS_BEG()) {
10573 c = tDSTAR;
10574 }
10575 else {
10576 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10577 }
10578 }
10579 else {
10580 if (c == '=') {
10581 set_yylval_id('*');
10582 SET_LEX_STATE(EXPR_BEG);
10583 return tOP_ASGN;
10584 }
10585 pushback(p, c);
10586 if (IS_SPCARG(c)) {
10587 rb_warning0("`*' interpreted as argument prefix");
10588 c = tSTAR;
10589 }
10590 else if (IS_BEG()) {
10591 c = tSTAR;
10592 }
10593 else {
10594 c = warn_balanced('*', "*", "argument prefix");
10595 }
10596 }
10597 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10598 return c;
10599
10600 case '!':
10601 c = nextc(p);
10602 if (IS_AFTER_OPERATOR()) {
10603 SET_LEX_STATE(EXPR_ARG);
10604 if (c == '@') {
10605 return '!';
10606 }
10607 }
10608 else {
10609 SET_LEX_STATE(EXPR_BEG);
10610 }
10611 if (c == '=') {
10612 return tNEQ;
10613 }
10614 if (c == '~') {
10615 return tNMATCH;
10616 }
10617 pushback(p, c);
10618 return '!';
10619
10620 case '=':
10621 if (was_bol(p)) {
10622 /* skip embedded rd document */
10623 if (word_match_p(p, "begin", 5)) {
10624 int first_p = TRUE;
10625
10626 lex_goto_eol(p);
10627 dispatch_scan_event(p, tEMBDOC_BEG);
10628 for (;;) {
10629 lex_goto_eol(p);
10630 if (!first_p) {
10631 dispatch_scan_event(p, tEMBDOC);
10632 }
10633 first_p = FALSE;
10634 c = nextc(p);
10635 if (c == -1) {
10636 compile_error(p, "embedded document meets end of file");
10637 return END_OF_INPUT;
10638 }
10639 if (c == '=' && word_match_p(p, "end", 3)) {
10640 break;
10641 }
10642 pushback(p, c);
10643 }
10644 lex_goto_eol(p);
10645 dispatch_scan_event(p, tEMBDOC_END);
10646 goto retry;
10647 }
10648 }
10649
10650 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10651 if ((c = nextc(p)) == '=') {
10652 if ((c = nextc(p)) == '=') {
10653 return tEQQ;
10654 }
10655 pushback(p, c);
10656 return tEQ;
10657 }
10658 if (c == '~') {
10659 return tMATCH;
10660 }
10661 else if (c == '>') {
10662 return tASSOC;
10663 }
10664 pushback(p, c);
10665 return '=';
10666
10667 case '<':
10668 c = nextc(p);
10669 if (c == '<' &&
10670 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10671 !IS_END() &&
10672 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10673 enum yytokentype token = heredoc_identifier(p);
10674 if (token) return token < 0 ? 0 : token;
10675 }
10676 if (IS_AFTER_OPERATOR()) {
10677 SET_LEX_STATE(EXPR_ARG);
10678 }
10679 else {
10680 if (IS_lex_state(EXPR_CLASS))
10681 p->command_start = TRUE;
10682 SET_LEX_STATE(EXPR_BEG);
10683 }
10684 if (c == '=') {
10685 if ((c = nextc(p)) == '>') {
10686 return tCMP;
10687 }
10688 pushback(p, c);
10689 return tLEQ;
10690 }
10691 if (c == '<') {
10692 if ((c = nextc(p)) == '=') {
10693 set_yylval_id(idLTLT);
10694 SET_LEX_STATE(EXPR_BEG);
10695 return tOP_ASGN;
10696 }
10697 pushback(p, c);
10698 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10699 }
10700 pushback(p, c);
10701 return '<';
10702
10703 case '>':
10704 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10705 if ((c = nextc(p)) == '=') {
10706 return tGEQ;
10707 }
10708 if (c == '>') {
10709 if ((c = nextc(p)) == '=') {
10710 set_yylval_id(idGTGT);
10711 SET_LEX_STATE(EXPR_BEG);
10712 return tOP_ASGN;
10713 }
10714 pushback(p, c);
10715 return tRSHFT;
10716 }
10717 pushback(p, c);
10718 return '>';
10719
10720 case '"':
10721 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10722 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10723 p->lex.ptok = p->lex.pcur-1;
10724 return tSTRING_BEG;
10725
10726 case '`':
10727 if (IS_lex_state(EXPR_FNAME)) {
10728 SET_LEX_STATE(EXPR_ENDFN);
10729 return c;
10730 }
10731 if (IS_lex_state(EXPR_DOT)) {
10732 if (cmd_state)
10733 SET_LEX_STATE(EXPR_CMDARG);
10734 else
10735 SET_LEX_STATE(EXPR_ARG);
10736 return c;
10737 }
10738 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10739 return tXSTRING_BEG;
10740
10741 case '\'':
10742 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10743 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10744 p->lex.ptok = p->lex.pcur-1;
10745 return tSTRING_BEG;
10746
10747 case '?':
10748 return parse_qmark(p, space_seen);
10749
10750 case '&':
10751 if ((c = nextc(p)) == '&') {
10752 SET_LEX_STATE(EXPR_BEG);
10753 if ((c = nextc(p)) == '=') {
10754 set_yylval_id(idANDOP);
10755 SET_LEX_STATE(EXPR_BEG);
10756 return tOP_ASGN;
10757 }
10758 pushback(p, c);
10759 return tANDOP;
10760 }
10761 else if (c == '=') {
10762 set_yylval_id('&');
10763 SET_LEX_STATE(EXPR_BEG);
10764 return tOP_ASGN;
10765 }
10766 else if (c == '.') {
10767 set_yylval_id(idANDDOT);
10768 SET_LEX_STATE(EXPR_DOT);
10769 return tANDDOT;
10770 }
10771 pushback(p, c);
10772 if (IS_SPCARG(c)) {
10773 if ((c != ':') ||
10774 (c = peekc_n(p, 1)) == -1 ||
10775 !(c == '\'' || c == '"' ||
10776 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10777 rb_warning0("`&' interpreted as argument prefix");
10778 }
10779 c = tAMPER;
10780 }
10781 else if (IS_BEG()) {
10782 c = tAMPER;
10783 }
10784 else {
10785 c = warn_balanced('&', "&", "argument prefix");
10786 }
10787 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10788 return c;
10789
10790 case '|':
10791 if ((c = nextc(p)) == '|') {
10792 SET_LEX_STATE(EXPR_BEG);
10793 if ((c = nextc(p)) == '=') {
10794 set_yylval_id(idOROP);
10795 SET_LEX_STATE(EXPR_BEG);
10796 return tOP_ASGN;
10797 }
10798 pushback(p, c);
10799 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10800 c = '|';
10801 pushback(p, '|');
10802 return c;
10803 }
10804 return tOROP;
10805 }
10806 if (c == '=') {
10807 set_yylval_id('|');
10808 SET_LEX_STATE(EXPR_BEG);
10809 return tOP_ASGN;
10810 }
10811 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10812 pushback(p, c);
10813 return '|';
10814
10815 case '+':
10816 c = nextc(p);
10817 if (IS_AFTER_OPERATOR()) {
10818 SET_LEX_STATE(EXPR_ARG);
10819 if (c == '@') {
10820 return tUPLUS;
10821 }
10822 pushback(p, c);
10823 return '+';
10824 }
10825 if (c == '=') {
10826 set_yylval_id('+');
10827 SET_LEX_STATE(EXPR_BEG);
10828 return tOP_ASGN;
10829 }
10830 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10831 SET_LEX_STATE(EXPR_BEG);
10832 pushback(p, c);
10833 if (c != -1 && ISDIGIT(c)) {
10834 return parse_numeric(p, '+');
10835 }
10836 return tUPLUS;
10837 }
10838 SET_LEX_STATE(EXPR_BEG);
10839 pushback(p, c);
10840 return warn_balanced('+', "+", "unary operator");
10841
10842 case '-':
10843 c = nextc(p);
10844 if (IS_AFTER_OPERATOR()) {
10845 SET_LEX_STATE(EXPR_ARG);
10846 if (c == '@') {
10847 return tUMINUS;
10848 }
10849 pushback(p, c);
10850 return '-';
10851 }
10852 if (c == '=') {
10853 set_yylval_id('-');
10854 SET_LEX_STATE(EXPR_BEG);
10855 return tOP_ASGN;
10856 }
10857 if (c == '>') {
10858 SET_LEX_STATE(EXPR_ENDFN);
10859 return tLAMBDA;
10860 }
10861 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10862 SET_LEX_STATE(EXPR_BEG);
10863 pushback(p, c);
10864 if (c != -1 && ISDIGIT(c)) {
10865 return tUMINUS_NUM;
10866 }
10867 return tUMINUS;
10868 }
10869 SET_LEX_STATE(EXPR_BEG);
10870 pushback(p, c);
10871 return warn_balanced('-', "-", "unary operator");
10872
10873 case '.': {
10874 int is_beg = IS_BEG();
10875 SET_LEX_STATE(EXPR_BEG);
10876 if ((c = nextc(p)) == '.') {
10877 if ((c = nextc(p)) == '.') {
10878 if (p->ctxt.in_argdef) {
10879 SET_LEX_STATE(EXPR_ENDARG);
10880 return tBDOT3;
10881 }
10882 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10883 rb_warn0("... at EOL, should be parenthesized?");
10884 }
10885 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
10886 if (IS_lex_state_for(last_state, EXPR_LABEL))
10887 return tDOT3;
10888 }
10889 return is_beg ? tBDOT3 : tDOT3;
10890 }
10891 pushback(p, c);
10892 return is_beg ? tBDOT2 : tDOT2;
10893 }
10894 pushback(p, c);
10895 if (c != -1 && ISDIGIT(c)) {
10896 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10897 parse_numeric(p, '.');
10898 if (ISDIGIT(prev)) {
10899 yyerror0("unexpected fraction part after numeric literal");
10900 }
10901 else {
10902 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10903 }
10904 SET_LEX_STATE(EXPR_END);
10905 p->lex.ptok = p->lex.pcur;
10906 goto retry;
10907 }
10908 set_yylval_id('.');
10909 SET_LEX_STATE(EXPR_DOT);
10910 return '.';
10911 }
10912
10913 case '0': case '1': case '2': case '3': case '4':
10914 case '5': case '6': case '7': case '8': case '9':
10915 return parse_numeric(p, c);
10916
10917 case ')':
10918 COND_POP();
10919 CMDARG_POP();
10920 SET_LEX_STATE(EXPR_ENDFN);
10921 p->lex.paren_nest--;
10922 return c;
10923
10924 case ']':
10925 COND_POP();
10926 CMDARG_POP();
10927 SET_LEX_STATE(EXPR_END);
10928 p->lex.paren_nest--;
10929 return c;
10930
10931 case '}':
10932 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10933 if (!p->lex.brace_nest--) return tSTRING_DEND;
10934 COND_POP();
10935 CMDARG_POP();
10936 SET_LEX_STATE(EXPR_END);
10937 p->lex.paren_nest--;
10938 return c;
10939
10940 case ':':
10941 c = nextc(p);
10942 if (c == ':') {
10943 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10944 SET_LEX_STATE(EXPR_BEG);
10945 return tCOLON3;
10946 }
10947 set_yylval_id(idCOLON2);
10948 SET_LEX_STATE(EXPR_DOT);
10949 return tCOLON2;
10950 }
10951 if (IS_END() || ISSPACE(c) || c == '#') {
10952 pushback(p, c);
10953 c = warn_balanced(':', ":", "symbol literal");
10954 SET_LEX_STATE(EXPR_BEG);
10955 return c;
10956 }
10957 switch (c) {
10958 case '\'':
10959 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
10960 break;
10961 case '"':
10962 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
10963 break;
10964 default:
10965 pushback(p, c);
10966 break;
10967 }
10968 SET_LEX_STATE(EXPR_FNAME);
10969 return tSYMBEG;
10970
10971 case '/':
10972 if (IS_BEG()) {
10973 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10974 return tREGEXP_BEG;
10975 }
10976 if ((c = nextc(p)) == '=') {
10977 set_yylval_id('/');
10978 SET_LEX_STATE(EXPR_BEG);
10979 return tOP_ASGN;
10980 }
10981 pushback(p, c);
10982 if (IS_SPCARG(c)) {
10983 arg_ambiguous(p, '/');
10984 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10985 return tREGEXP_BEG;
10986 }
10987 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10988 return warn_balanced('/', "/", "regexp literal");
10989
10990 case '^':
10991 if ((c = nextc(p)) == '=') {
10992 set_yylval_id('^');
10993 SET_LEX_STATE(EXPR_BEG);
10994 return tOP_ASGN;
10995 }
10996 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10997 pushback(p, c);
10998 return '^';
10999
11000 case ';':
11001 SET_LEX_STATE(EXPR_BEG);
11002 p->command_start = TRUE;
11003 return ';';
11004
11005 case ',':
11006 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11007 return ',';
11008
11009 case '~':
11010 if (IS_AFTER_OPERATOR()) {
11011 if ((c = nextc(p)) != '@') {
11012 pushback(p, c);
11013 }
11014 SET_LEX_STATE(EXPR_ARG);
11015 }
11016 else {
11017 SET_LEX_STATE(EXPR_BEG);
11018 }
11019 return '~';
11020
11021 case '(':
11022 if (IS_BEG()) {
11023 c = tLPAREN;
11024 }
11025 else if (!space_seen) {
11026 /* foo( ... ) => method call, no ambiguity */
11027 }
11028 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11029 c = tLPAREN_ARG;
11030 }
11031 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11032 rb_warning0("parentheses after method name is interpreted as "
11033 "an argument list, not a decomposed argument");
11034 }
11035 p->lex.paren_nest++;
11036 COND_PUSH(0);
11037 CMDARG_PUSH(0);
11038 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11039 return c;
11040
11041 case '[':
11042 p->lex.paren_nest++;
11043 if (IS_AFTER_OPERATOR()) {
11044 if ((c = nextc(p)) == ']') {
11045 p->lex.paren_nest--;
11046 SET_LEX_STATE(EXPR_ARG);
11047 if ((c = nextc(p)) == '=') {
11048 return tASET;
11049 }
11050 pushback(p, c);
11051 return tAREF;
11052 }
11053 pushback(p, c);
11054 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11055 return '[';
11056 }
11057 else if (IS_BEG()) {
11058 c = tLBRACK;
11059 }
11060 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11061 c = tLBRACK;
11062 }
11063 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11064 COND_PUSH(0);
11065 CMDARG_PUSH(0);
11066 return c;
11067
11068 case '{':
11069 ++p->lex.brace_nest;
11070 if (lambda_beginning_p())
11071 c = tLAMBEG;
11072 else if (IS_lex_state(EXPR_LABELED))
11073 c = tLBRACE; /* hash */
11074 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11075 c = '{'; /* block (primary) */
11076 else if (IS_lex_state(EXPR_ENDARG))
11077 c = tLBRACE_ARG; /* block (expr) */
11078 else
11079 c = tLBRACE; /* hash */
11080 if (c != tLBRACE) {
11081 p->command_start = TRUE;
11082 SET_LEX_STATE(EXPR_BEG);
11083 }
11084 else {
11085 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11086 }
11087 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11088 COND_PUSH(0);
11089 CMDARG_PUSH(0);
11090 return c;
11091
11092 case '\\':
11093 c = nextc(p);
11094 if (c == '\n') {
11095 space_seen = 1;
11096 dispatch_scan_event(p, tSP);
11097 goto retry; /* skip \\n */
11098 }
11099 if (c == ' ') return tSP;
11100 if (ISSPACE(c)) return c;
11101 pushback(p, c);
11102 return '\\';
11103
11104 case '%':
11105 return parse_percent(p, space_seen, last_state);
11106
11107 case '$':
11108 return parse_gvar(p, last_state);
11109
11110 case '@':
11111 return parse_atmark(p, last_state);
11112
11113 case '_':
11114 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11115 p->ruby__end__seen = 1;
11116 p->eofp = 1;
11117#ifdef RIPPER
11118 lex_goto_eol(p);
11119 dispatch_scan_event(p, k__END__);
11120#endif
11121 return END_OF_INPUT;
11122 }
11123 newtok(p);
11124 break;
11125
11126 default:
11127 if (!parser_is_identchar(p)) {
11128 compile_error(p, "Invalid char `\\x%02X' in expression", c);
11129 token_flush(p);
11130 goto retry;
11131 }
11132
11133 newtok(p);
11134 break;
11135 }
11136
11137 return parse_ident(p, c, cmd_state);
11138}
11139
11140static enum yytokentype
11141yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11142{
11143 enum yytokentype t;
11144
11145 p->lval = lval;
11146 lval->val = Qundef;
11147 p->yylloc = yylloc;
11148
11149 t = parser_yylex(p);
11150
11151 if (has_delayed_token(p))
11152 dispatch_delayed_token(p, t);
11153 else if (t != END_OF_INPUT)
11154 dispatch_scan_event(p, t);
11155
11156 return t;
11157}
11158
11159#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11160
11161static NODE*
11162node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11163{
11164 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11165
11166 rb_node_init(n, type);
11167 return n;
11168}
11169
11170static NODE *
11171nd_set_loc(NODE *nd, const YYLTYPE *loc)
11172{
11173 nd->nd_loc = *loc;
11174 nd_set_line(nd, loc->beg_pos.lineno);
11175 return nd;
11176}
11177
11178static NODE*
11179node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11180{
11181 NODE *n = node_new_internal(p, type, size, alignment);
11182
11183 nd_set_loc(n, loc);
11184 nd_set_node_id(n, parser_get_node_id(p));
11185 return n;
11186}
11187
11188#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11189
11190#ifndef RIPPER
11191
11192static rb_node_scope_t *
11193rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11194{
11195 rb_ast_id_table_t *nd_tbl;
11196 nd_tbl = local_tbl(p);
11197 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11198 n->nd_tbl = nd_tbl;
11199 n->nd_body = nd_body;
11200 n->nd_args = nd_args;
11201
11202 return n;
11203}
11204
11205static rb_node_scope_t *
11206rb_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)
11207{
11208 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11209 n->nd_tbl = nd_tbl;
11210 n->nd_body = nd_body;
11211 n->nd_args = nd_args;
11212
11213 return n;
11214}
11215
11216static rb_node_defn_t *
11217rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11218{
11219 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11220 n->nd_mid = nd_mid;
11221 n->nd_defn = nd_defn;
11222
11223 return n;
11224}
11225
11226static rb_node_defs_t *
11227rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11228{
11229 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11230 n->nd_recv = nd_recv;
11231 n->nd_mid = nd_mid;
11232 n->nd_defn = nd_defn;
11233
11234 return n;
11235}
11236
11237static rb_node_block_t *
11238rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11239{
11240 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11241 n->nd_head = nd_head;
11242 n->nd_end = (NODE *)n;
11243 n->nd_next = 0;
11244
11245 return n;
11246}
11247
11248static rb_node_for_t *
11249rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc)
11250{
11251 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11252 n->nd_body = nd_body;
11253 n->nd_iter = nd_iter;
11254
11255 return n;
11256}
11257
11258static rb_node_for_masgn_t *
11259rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11260{
11261 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11262 n->nd_var = nd_var;
11263
11264 return n;
11265}
11266
11267static rb_node_retry_t *
11268rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11269{
11270 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11271
11272 return n;
11273}
11274
11275static rb_node_begin_t *
11276rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11277{
11278 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11279 n->nd_body = nd_body;
11280
11281 return n;
11282}
11283
11284static rb_node_rescue_t *
11285rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11286{
11287 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11288 n->nd_head = nd_head;
11289 n->nd_resq = nd_resq;
11290 n->nd_else = nd_else;
11291
11292 return n;
11293}
11294
11295static rb_node_resbody_t *
11296rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_body, NODE *nd_head, const YYLTYPE *loc)
11297{
11298 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11299 n->nd_head = nd_head;
11300 n->nd_body = nd_body;
11301 n->nd_args = nd_args;
11302
11303 return n;
11304}
11305
11306static rb_node_ensure_t *
11307rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11308{
11309 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11310 n->nd_head = nd_head;
11311 n->nd_resq = 0;
11312 n->nd_ensr = nd_ensr;
11313
11314 return n;
11315}
11316
11317static rb_node_and_t *
11318rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
11319{
11320 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11321 n->nd_1st = nd_1st;
11322 n->nd_2nd = nd_2nd;
11323
11324 return n;
11325}
11326
11327static rb_node_or_t *
11328rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
11329{
11330 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11331 n->nd_1st = nd_1st;
11332 n->nd_2nd = nd_2nd;
11333
11334 return n;
11335}
11336
11337static rb_node_return_t *
11338rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
11339{
11340 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11341 n->nd_stts = nd_stts;
11342 return n;
11343}
11344
11345static rb_node_yield_t *
11346rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11347{
11348 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11349 n->nd_head = nd_head;
11350
11351 return n;
11352}
11353
11354static rb_node_if_t *
11355rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11356{
11357 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11358 n->nd_cond = nd_cond;
11359 n->nd_body = nd_body;
11360 n->nd_else = nd_else;
11361
11362 return n;
11363}
11364
11365static rb_node_unless_t *
11366rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11367{
11368 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11369 n->nd_cond = nd_cond;
11370 n->nd_body = nd_body;
11371 n->nd_else = nd_else;
11372
11373 return n;
11374}
11375
11376static rb_node_class_t *
11377rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc)
11378{
11379 /* Keep the order of node creation */
11380 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11381 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11382 n->nd_cpath = nd_cpath;
11383 n->nd_body = scope;
11384 n->nd_super = nd_super;
11385
11386 return n;
11387}
11388
11389static rb_node_sclass_t *
11390rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc)
11391{
11392 /* Keep the order of node creation */
11393 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11394 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11395 n->nd_recv = nd_recv;
11396 n->nd_body = scope;
11397
11398 return n;
11399}
11400
11401static rb_node_module_t *
11402rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc)
11403{
11404 /* Keep the order of node creation */
11405 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11406 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11407 n->nd_cpath = nd_cpath;
11408 n->nd_body = scope;
11409
11410 return n;
11411}
11412
11413static rb_node_iter_t *
11414rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11415{
11416 /* Keep the order of node creation */
11417 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11418 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11419 n->nd_body = scope;
11420 n->nd_iter = 0;
11421
11422 return n;
11423}
11424
11425static rb_node_lambda_t *
11426rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11427{
11428 /* Keep the order of node creation */
11429 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11430 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, loc);
11431 n->nd_body = scope;
11432
11433 return n;
11434}
11435
11436static rb_node_case_t *
11437rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
11438{
11439 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11440 n->nd_head = nd_head;
11441 n->nd_body = nd_body;
11442
11443 return n;
11444}
11445
11446static rb_node_case2_t *
11447rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11448{
11449 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11450 n->nd_head = 0;
11451 n->nd_body = nd_body;
11452
11453 return n;
11454}
11455
11456static rb_node_case3_t *
11457rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
11458{
11459 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11460 n->nd_head = nd_head;
11461 n->nd_body = nd_body;
11462
11463 return n;
11464}
11465
11466static rb_node_when_t *
11467rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11468{
11469 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11470 n->nd_head = nd_head;
11471 n->nd_body = nd_body;
11472 n->nd_next = nd_next;
11473
11474 return n;
11475}
11476
11477static rb_node_in_t *
11478rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11479{
11480 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11481 n->nd_head = nd_head;
11482 n->nd_body = nd_body;
11483 n->nd_next = nd_next;
11484
11485 return n;
11486}
11487
11488static rb_node_while_t *
11489rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc)
11490{
11491 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11492 n->nd_cond = nd_cond;
11493 n->nd_body = nd_body;
11494 n->nd_state = nd_state;
11495
11496 return n;
11497}
11498
11499static rb_node_until_t *
11500rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc)
11501{
11502 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11503 n->nd_cond = nd_cond;
11504 n->nd_body = nd_body;
11505 n->nd_state = nd_state;
11506
11507 return n;
11508}
11509
11510static rb_node_colon2_t *
11511rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc)
11512{
11513 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11514 n->nd_head = nd_head;
11515 n->nd_mid = nd_mid;
11516
11517 return n;
11518}
11519
11520static rb_node_colon3_t *
11521rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11522{
11523 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11524 n->nd_mid = nd_mid;
11525
11526 return n;
11527}
11528
11529static rb_node_dot2_t *
11530rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11531{
11532 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11533 n->nd_beg = nd_beg;
11534 n->nd_end = nd_end;
11535
11536 return n;
11537}
11538
11539static rb_node_dot3_t *
11540rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11541{
11542 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11543 n->nd_beg = nd_beg;
11544 n->nd_end = nd_end;
11545
11546 return n;
11547}
11548
11549static rb_node_self_t *
11550rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11551{
11552 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11553 n->nd_state = 1;
11554
11555 return n;
11556}
11557
11558static rb_node_nil_t *
11559rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11560{
11561 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11562
11563 return n;
11564}
11565
11566static rb_node_true_t *
11567rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11568{
11569 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11570
11571 return n;
11572}
11573
11574static rb_node_false_t *
11575rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11576{
11577 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11578
11579 return n;
11580}
11581
11582static rb_node_super_t *
11583rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc)
11584{
11585 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11586 n->nd_args = nd_args;
11587
11588 return n;
11589}
11590
11591static rb_node_zsuper_t *
11592rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11593{
11594 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11595
11596 return n;
11597}
11598
11599static rb_node_match2_t *
11600rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11601{
11602 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11603 n->nd_recv = nd_recv;
11604 n->nd_value = nd_value;
11605 n->nd_args = 0;
11606
11607 return n;
11608}
11609
11610static rb_node_match3_t *
11611rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11612{
11613 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11614 n->nd_recv = nd_recv;
11615 n->nd_value = nd_value;
11616
11617 return n;
11618}
11619
11620/* TODO: Use union for NODE_LIST2 */
11621static rb_node_list_t *
11622rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11623{
11624 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11625 n->nd_head = nd_head;
11626 n->as.nd_alen = 1;
11627 n->nd_next = 0;
11628
11629 return n;
11630}
11631
11632static rb_node_list_t *
11633rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11634{
11635 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11636 n->nd_head = nd_head;
11637 n->as.nd_alen = nd_alen;
11638 n->nd_next = nd_next;
11639
11640 return n;
11641}
11642
11643static rb_node_zlist_t *
11644rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11645{
11646 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11647
11648 return n;
11649}
11650
11651static rb_node_hash_t *
11652rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11653{
11654 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11655 n->nd_head = nd_head;
11656 n->nd_brace = 0;
11657
11658 return n;
11659}
11660
11661static rb_node_masgn_t *
11662rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11663{
11664 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11665 n->nd_head = nd_head;
11666 n->nd_value = 0;
11667 n->nd_args = nd_args;
11668
11669 return n;
11670}
11671
11672static rb_node_gasgn_t *
11673rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11674{
11675 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11676 n->nd_vid = nd_vid;
11677 n->nd_value = nd_value;
11678
11679 return n;
11680}
11681
11682static rb_node_lasgn_t *
11683rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11684{
11685 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11686 n->nd_vid = nd_vid;
11687 n->nd_value = nd_value;
11688
11689 return n;
11690}
11691
11692static rb_node_dasgn_t *
11693rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11694{
11695 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11696 n->nd_vid = nd_vid;
11697 n->nd_value = nd_value;
11698
11699 return n;
11700}
11701
11702static rb_node_iasgn_t *
11703rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11704{
11705 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11706 n->nd_vid = nd_vid;
11707 n->nd_value = nd_value;
11708
11709 return n;
11710}
11711
11712static rb_node_cvasgn_t *
11713rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11714{
11715 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11716 n->nd_vid = nd_vid;
11717 n->nd_value = nd_value;
11718
11719 return n;
11720}
11721
11722static rb_node_op_asgn1_t *
11723rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc)
11724{
11725 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11726 n->nd_recv = nd_recv;
11727 n->nd_mid = nd_mid;
11728 n->nd_index = index;
11729 n->nd_rvalue = rvalue;
11730
11731 return n;
11732}
11733
11734static rb_node_op_asgn2_t *
11735rb_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)
11736{
11737 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11738 n->nd_recv = nd_recv;
11739 n->nd_value = nd_value;
11740 n->nd_vid = nd_vid;
11741 n->nd_mid = nd_mid;
11742 n->nd_aid = nd_aid;
11743
11744 return n;
11745}
11746
11747static rb_node_op_asgn_or_t *
11748rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11749{
11750 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11751 n->nd_head = nd_head;
11752 n->nd_value = nd_value;
11753
11754 return n;
11755}
11756
11757static rb_node_op_asgn_and_t *
11758rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11759{
11760 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11761 n->nd_head = nd_head;
11762 n->nd_value = nd_value;
11763
11764 return n;
11765}
11766
11767static rb_node_gvar_t *
11768rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11769{
11770 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11771 n->nd_vid = nd_vid;
11772
11773 return n;
11774}
11775
11776static rb_node_lvar_t *
11777rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11778{
11779 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11780 n->nd_vid = nd_vid;
11781
11782 return n;
11783}
11784
11785static rb_node_dvar_t *
11786rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11787{
11788 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11789 n->nd_vid = nd_vid;
11790
11791 return n;
11792}
11793
11794static rb_node_ivar_t *
11795rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11796{
11797 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11798 n->nd_vid = nd_vid;
11799
11800 return n;
11801}
11802
11803static rb_node_const_t *
11804rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11805{
11806 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11807 n->nd_vid = nd_vid;
11808
11809 return n;
11810}
11811
11812static rb_node_cvar_t *
11813rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11814{
11815 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11816 n->nd_vid = nd_vid;
11817
11818 return n;
11819}
11820
11821static rb_node_nth_ref_t *
11822rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11823{
11824 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11825 n->nd_nth = nd_nth;
11826
11827 return n;
11828}
11829
11830static rb_node_back_ref_t *
11831rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11832{
11833 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11834 n->nd_nth = nd_nth;
11835
11836 return n;
11837}
11838
11839static rb_node_lit_t *
11840rb_node_lit_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11841{
11842 rb_node_lit_t *n = NODE_NEWNODE(NODE_LIT, rb_node_lit_t, loc);
11843 n->nd_lit = nd_lit;
11844
11845 return n;
11846}
11847
11848static rb_node_str_t *
11849rb_node_str_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11850{
11851 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
11852 n->nd_lit = nd_lit;
11853
11854 return n;
11855}
11856
11857/* TODO; Use union for NODE_DSTR2 */
11858static rb_node_dstr_t *
11859rb_node_dstr_new0(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11860{
11861 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
11862 n->nd_lit = nd_lit;
11863 n->as.nd_alen = nd_alen;
11864 n->nd_next = (rb_node_list_t *)nd_next;
11865
11866 return n;
11867}
11868
11869static rb_node_dstr_t *
11870rb_node_dstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11871{
11872 return rb_node_dstr_new0(p, nd_lit, 1, 0, loc);
11873}
11874
11875static rb_node_xstr_t *
11876rb_node_xstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11877{
11878 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
11879 n->nd_lit = nd_lit;
11880
11881 return n;
11882}
11883
11884static rb_node_dxstr_t *
11885rb_node_dxstr_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11886{
11887 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
11888 n->nd_lit = nd_lit;
11889 n->nd_alen = nd_alen;
11890 n->nd_next = (rb_node_list_t *)nd_next;
11891
11892 return n;
11893}
11894
11895static rb_node_dsym_t *
11896rb_node_dsym_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11897{
11898 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
11899 n->nd_lit = nd_lit;
11900 n->nd_alen = nd_alen;
11901 n->nd_next = (rb_node_list_t *)nd_next;
11902
11903 return n;
11904}
11905
11906static rb_node_evstr_t *
11907rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11908{
11909 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
11910 n->nd_body = nd_body;
11911
11912 return n;
11913}
11914
11915static rb_node_call_t *
11916rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11917{
11918 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
11919 n->nd_recv = nd_recv;
11920 n->nd_mid = nd_mid;
11921 n->nd_args = nd_args;
11922
11923 return n;
11924}
11925
11926static rb_node_opcall_t *
11927rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11928{
11929 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
11930 n->nd_recv = nd_recv;
11931 n->nd_mid = nd_mid;
11932 n->nd_args = nd_args;
11933
11934 return n;
11935}
11936
11937static rb_node_fcall_t *
11938rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11939{
11940 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
11941 n->nd_mid = nd_mid;
11942 n->nd_args = nd_args;
11943
11944 return n;
11945}
11946
11947static rb_node_qcall_t *
11948rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11949{
11950 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
11951 n->nd_recv = nd_recv;
11952 n->nd_mid = nd_mid;
11953 n->nd_args = nd_args;
11954
11955 return n;
11956}
11957
11958static rb_node_vcall_t *
11959rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11960{
11961 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
11962 n->nd_mid = nd_mid;
11963
11964 return n;
11965}
11966
11967static rb_node_once_t *
11968rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11969{
11970 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
11971 n->nd_body = nd_body;
11972
11973 return n;
11974}
11975
11976static rb_node_args_t *
11977rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
11978{
11979 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
11980 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
11981
11982 return n;
11983}
11984
11985static rb_node_args_aux_t *
11986rb_node_args_aux_new(struct parser_params *p, ID nd_pid, long nd_plen, const YYLTYPE *loc)
11987{
11988 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
11989 n->nd_pid = nd_pid;
11990 n->nd_plen = nd_plen;
11991 n->nd_next = 0;
11992
11993 return n;
11994}
11995
11996static rb_node_opt_arg_t *
11997rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11998{
11999 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12000 n->nd_body = nd_body;
12001 n->nd_next = 0;
12002
12003 return n;
12004}
12005
12006static rb_node_kw_arg_t *
12007rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12008{
12009 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12010 n->nd_body = nd_body;
12011 n->nd_next = 0;
12012
12013 return n;
12014}
12015
12016static rb_node_postarg_t *
12017rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12018{
12019 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12020 n->nd_1st = nd_1st;
12021 n->nd_2nd = nd_2nd;
12022
12023 return n;
12024}
12025
12026static rb_node_argscat_t *
12027rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12028{
12029 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12030 n->nd_head = nd_head;
12031 n->nd_body = nd_body;
12032
12033 return n;
12034}
12035
12036static rb_node_argspush_t *
12037rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12038{
12039 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12040 n->nd_head = nd_head;
12041 n->nd_body = nd_body;
12042
12043 return n;
12044}
12045
12046static rb_node_splat_t *
12047rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12048{
12049 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12050 n->nd_head = nd_head;
12051
12052 return n;
12053}
12054
12055static rb_node_block_pass_t *
12056rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12057{
12058 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12059 n->nd_head = 0;
12060 n->nd_body = nd_body;
12061
12062 return n;
12063}
12064
12065static rb_node_alias_t *
12066rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12067{
12068 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12069 n->nd_1st = nd_1st;
12070 n->nd_2nd = nd_2nd;
12071
12072 return n;
12073}
12074
12075static rb_node_valias_t *
12076rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc)
12077{
12078 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12079 n->nd_alias = nd_alias;
12080 n->nd_orig = nd_orig;
12081
12082 return n;
12083}
12084
12085static rb_node_undef_t *
12086rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12087{
12088 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12089 n->nd_undef = nd_undef;
12090
12091 return n;
12092}
12093
12094static rb_node_errinfo_t *
12095rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12096{
12097 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12098
12099 return n;
12100}
12101
12102static rb_node_defined_t *
12103rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12104{
12105 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12106 n->nd_head = nd_head;
12107
12108 return n;
12109}
12110
12111static rb_node_postexe_t *
12112rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12113{
12114 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12115 n->nd_body = nd_body;
12116
12117 return n;
12118}
12119
12120static rb_node_attrasgn_t *
12121rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12122{
12123 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12124 n->nd_recv = nd_recv;
12125 n->nd_mid = nd_mid;
12126 n->nd_args = nd_args;
12127
12128 return n;
12129}
12130
12131static rb_node_aryptn_t *
12132rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12133{
12134 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12135 n->nd_pconst = 0;
12136 n->pre_args = pre_args;
12137 n->rest_arg = rest_arg;
12138 n->post_args = post_args;
12139
12140 return n;
12141}
12142
12143static rb_node_hshptn_t *
12144rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12145{
12146 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12147 n->nd_pconst = nd_pconst;
12148 n->nd_pkwargs = nd_pkwargs;
12149 n->nd_pkwrestarg = nd_pkwrestarg;
12150
12151 return n;
12152}
12153
12154static rb_node_fndptn_t *
12155rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12156{
12157 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12158 n->nd_pconst = 0;
12159 n->pre_rest_arg = pre_rest_arg;
12160 n->args = args;
12161 n->post_rest_arg = post_rest_arg;
12162
12163 return n;
12164}
12165
12166static rb_node_cdecl_t *
12167rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, const YYLTYPE *loc)
12168{
12169 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12170 n->nd_vid = nd_vid;
12171 n->nd_value = nd_value;
12172 n->nd_else = nd_else;
12173
12174 return n;
12175}
12176
12177static rb_node_op_cdecl_t *
12178rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, const YYLTYPE *loc)
12179{
12180 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12181 n->nd_head = nd_head;
12182 n->nd_value = nd_value;
12183 n->nd_aid = nd_aid;
12184
12185 return n;
12186}
12187
12188static rb_node_error_t *
12189rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12190{
12191 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12192
12193 return n;
12194}
12195
12196#else
12197
12198static rb_node_ripper_t *
12199rb_node_ripper_new(struct parser_params *p, ID nd_vid, VALUE nd_rval, VALUE nd_cval, const YYLTYPE *loc)
12200{
12201 rb_node_ripper_t *n = NODE_NEWNODE(NODE_RIPPER, rb_node_ripper_t, loc);
12202 n->nd_vid = nd_vid;
12203 n->nd_rval = nd_rval;
12204 n->nd_cval = nd_cval;
12205
12206 return n;
12207}
12208
12209static rb_node_ripper_values_t *
12210rb_node_ripper_values_new(struct parser_params *p, VALUE nd_val1, VALUE nd_val2, VALUE nd_val3, const YYLTYPE *loc)
12211{
12212 rb_node_ripper_values_t *n = NODE_NEWNODE(NODE_RIPPER_VALUES, rb_node_ripper_values_t, loc);
12213 n->nd_val1 = nd_val1;
12214 n->nd_val2 = nd_val2;
12215 n->nd_val3 = nd_val3;
12216
12217 return n;
12218}
12219
12220#endif
12221
12222static rb_node_break_t *
12223rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
12224{
12225 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12226 n->nd_stts = nd_stts;
12227 n->nd_chain = 0;
12228
12229 return n;
12230}
12231
12232static rb_node_next_t *
12233rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
12234{
12235 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12236 n->nd_stts = nd_stts;
12237 n->nd_chain = 0;
12238
12239 return n;
12240}
12241
12242static rb_node_redo_t *
12243rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc)
12244{
12245 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12246 n->nd_chain = 0;
12247
12248 return n;
12249}
12250
12251static rb_node_def_temp_t *
12252rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12253{
12254 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12255 n->save.cur_arg = p->cur_arg;
12256 n->save.numparam_save = 0;
12257 n->save.max_numparam = 0;
12258 n->save.ctxt = p->ctxt;
12259#ifdef RIPPER
12260 n->nd_recv = Qnil;
12261 n->nd_mid = Qnil;
12262 n->dot_or_colon = Qnil;
12263#else
12264 n->nd_def = 0;
12265 n->nd_mid = 0;
12266#endif
12267
12268 return n;
12269}
12270
12271static rb_node_def_temp_t *
12272def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12273{
12274 n->save.numparam_save = numparam_push(p);
12275 n->save.max_numparam = p->max_numparam;
12276 return n;
12277}
12278
12279#ifndef RIPPER
12280static enum node_type
12281nodetype(NODE *node) /* for debug */
12282{
12283 return (enum node_type)nd_type(node);
12284}
12285
12286static int
12287nodeline(NODE *node)
12288{
12289 return nd_line(node);
12290}
12291
12292static NODE*
12293newline_node(NODE *node)
12294{
12295 if (node) {
12296 node = remove_begin(node);
12297 nd_set_fl_newline(node);
12298 }
12299 return node;
12300}
12301
12302static void
12303fixpos(NODE *node, NODE *orig)
12304{
12305 if (!node) return;
12306 if (!orig) return;
12307 nd_set_line(node, nd_line(orig));
12308}
12309
12310static void
12311parser_warning(struct parser_params *p, NODE *node, const char *mesg)
12312{
12313 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
12314}
12315
12316static void
12317parser_warn(struct parser_params *p, NODE *node, const char *mesg)
12318{
12319 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
12320}
12321
12322static NODE*
12323block_append(struct parser_params *p, NODE *head, NODE *tail)
12324{
12325 NODE *end, *h = head, *nd;
12326
12327 if (tail == 0) return head;
12328
12329 if (h == 0) return tail;
12330 switch (nd_type(h)) {
12331 default:
12332 h = end = NEW_BLOCK(head, &head->nd_loc);
12333 head = end;
12334 break;
12335 case NODE_BLOCK:
12336 end = RNODE_BLOCK(h)->nd_end;
12337 break;
12338 }
12339
12340 nd = RNODE_BLOCK(end)->nd_head;
12341 switch (nd_type(nd)) {
12342 case NODE_RETURN:
12343 case NODE_BREAK:
12344 case NODE_NEXT:
12345 case NODE_REDO:
12346 case NODE_RETRY:
12347 if (RTEST(ruby_verbose)) {
12348 parser_warning(p, tail, "statement not reached");
12349 }
12350 break;
12351
12352 default:
12353 break;
12354 }
12355
12356 if (!nd_type_p(tail, NODE_BLOCK)) {
12357 tail = NEW_BLOCK(tail, &tail->nd_loc);
12358 }
12359 RNODE_BLOCK(end)->nd_next = tail;
12360 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12361 nd_set_last_loc(head, nd_last_loc(tail));
12362 return head;
12363}
12364
12365/* append item to the list */
12366static NODE*
12367list_append(struct parser_params *p, NODE *list, NODE *item)
12368{
12369 NODE *last;
12370
12371 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12372 if (RNODE_LIST(list)->nd_next) {
12373 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12374 }
12375 else {
12376 last = list;
12377 }
12378
12379 RNODE_LIST(list)->as.nd_alen += 1;
12380 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12381 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12382
12383 nd_set_last_loc(list, nd_last_loc(item));
12384
12385 return list;
12386}
12387
12388/* concat two lists */
12389static NODE*
12390list_concat(NODE *head, NODE *tail)
12391{
12392 NODE *last;
12393
12394 if (RNODE_LIST(head)->nd_next) {
12395 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12396 }
12397 else {
12398 last = head;
12399 }
12400
12401 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12402 RNODE_LIST(last)->nd_next = tail;
12403 if (RNODE_LIST(tail)->nd_next) {
12404 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12405 }
12406 else {
12407 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12408 }
12409
12410 nd_set_last_loc(head, nd_last_loc(tail));
12411
12412 return head;
12413}
12414
12415static int
12416literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
12417{
12418 if (NIL_P(tail)) return 1;
12419 if (!rb_enc_compatible(head, tail)) {
12420 compile_error(p, "string literal encodings differ (%s / %s)",
12421 rb_enc_name(rb_enc_get(head)),
12422 rb_enc_name(rb_enc_get(tail)));
12423 rb_str_resize(head, 0);
12424 rb_str_resize(tail, 0);
12425 return 0;
12426 }
12427 rb_str_buf_append(head, tail);
12428 return 1;
12429}
12430
12431static VALUE
12432string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12433{
12434 if (htype != NODE_DSTR) return Qfalse;
12435 if (RNODE_DSTR(head)->nd_next) {
12436 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12437 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
12438 }
12439 const VALUE lit = RNODE_DSTR(head)->nd_lit;
12440 ASSUME(lit != Qfalse);
12441 return lit;
12442}
12443
12444/* concat two string literals */
12445static NODE *
12446literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12447{
12448 enum node_type htype;
12449 VALUE lit;
12450
12451 if (!head) return tail;
12452 if (!tail) return head;
12453
12454 htype = nd_type(head);
12455 if (htype == NODE_EVSTR) {
12456 head = new_dstr(p, head, loc);
12457 htype = NODE_DSTR;
12458 }
12459 if (p->heredoc_indent > 0) {
12460 switch (htype) {
12461 case NODE_STR:
12462 head = str2dstr(p, head);
12463 case NODE_DSTR:
12464 return list_append(p, head, tail);
12465 default:
12466 break;
12467 }
12468 }
12469 switch (nd_type(tail)) {
12470 case NODE_STR:
12471 if ((lit = string_literal_head(p, htype, head)) != Qfalse) {
12472 htype = NODE_STR;
12473 }
12474 else {
12475 lit = RNODE_DSTR(head)->nd_lit;
12476 }
12477 if (htype == NODE_STR) {
12478 if (!literal_concat0(p, lit, RNODE_STR(tail)->nd_lit)) {
12479 error:
12480 rb_discard_node(p, head);
12481 rb_discard_node(p, tail);
12482 return 0;
12483 }
12484 rb_discard_node(p, tail);
12485 }
12486 else {
12487 list_append(p, head, tail);
12488 }
12489 break;
12490
12491 case NODE_DSTR:
12492 if (htype == NODE_STR) {
12493 if (!literal_concat0(p, RNODE_STR(head)->nd_lit, RNODE_DSTR(tail)->nd_lit))
12494 goto error;
12495 RNODE_DSTR(tail)->nd_lit = RNODE_STR(head)->nd_lit;
12496 rb_discard_node(p, head);
12497 head = tail;
12498 }
12499 else if (NIL_P(RNODE_DSTR(tail)->nd_lit)) {
12500 append:
12501 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12502 if (!RNODE_DSTR(head)->nd_next) {
12503 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12504 }
12505 else if (RNODE_DSTR(tail)->nd_next) {
12506 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12507 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12508 }
12509 rb_discard_node(p, tail);
12510 }
12511 else if ((lit = string_literal_head(p, htype, head)) != Qfalse) {
12512 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->nd_lit))
12513 goto error;
12514 RNODE_DSTR(tail)->nd_lit = Qnil;
12515 goto append;
12516 }
12517 else {
12518 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));
12519 }
12520 break;
12521
12522 case NODE_EVSTR:
12523 if (htype == NODE_STR) {
12524 head = str2dstr(p, head);
12525 RNODE_DSTR(head)->as.nd_alen = 1;
12526 }
12527 list_append(p, head, tail);
12528 break;
12529 }
12530 return head;
12531}
12532
12533static void
12534nd_copy_flag(NODE *new_node, NODE *old_node)
12535{
12536 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12537 nd_set_line(new_node, nd_line(old_node));
12538 new_node->nd_loc = old_node->nd_loc;
12539 new_node->node_id = old_node->node_id;
12540}
12541
12542static NODE *
12543str2dstr(struct parser_params *p, NODE *node)
12544{
12545 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12546 nd_copy_flag(new_node, node);
12547 RNODE_DSTR(new_node)->nd_lit = RNODE_STR(node)->nd_lit;
12548 RNODE_DSTR(new_node)->as.nd_alen = 0;
12549 RNODE_DSTR(new_node)->nd_next = 0;
12550 RNODE_STR(node)->nd_lit = 0;
12551
12552 return new_node;
12553}
12554
12555static NODE *
12556evstr2dstr(struct parser_params *p, NODE *node)
12557{
12558 if (nd_type_p(node, NODE_EVSTR)) {
12559 node = new_dstr(p, node, &node->nd_loc);
12560 }
12561 return node;
12562}
12563
12564static NODE *
12565new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12566{
12567 NODE *head = node;
12568
12569 if (node) {
12570 switch (nd_type(node)) {
12571 case NODE_STR:
12572 return str2dstr(p, node);
12573 case NODE_DSTR:
12574 break;
12575 case NODE_EVSTR:
12576 return node;
12577 }
12578 }
12579 return NEW_EVSTR(head, loc);
12580}
12581
12582static NODE *
12583new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12584{
12585 VALUE lit = STR_NEW0();
12586 NODE *dstr = NEW_DSTR(lit, loc);
12587 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12588 return list_append(p, dstr, node);
12589}
12590
12591static NODE *
12592call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12593 const YYLTYPE *op_loc, const YYLTYPE *loc)
12594{
12595 NODE *expr;
12596 value_expr(recv);
12597 value_expr(arg1);
12598 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12599 nd_set_line(expr, op_loc->beg_pos.lineno);
12600 return expr;
12601}
12602
12603static NODE *
12604call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12605{
12606 NODE *opcall;
12607 value_expr(recv);
12608 opcall = NEW_OPCALL(recv, id, 0, loc);
12609 nd_set_line(opcall, op_loc->beg_pos.lineno);
12610 return opcall;
12611}
12612
12613static NODE *
12614new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12615{
12616 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12617 nd_set_line(qcall, op_loc->beg_pos.lineno);
12618 return qcall;
12619}
12620
12621static NODE*
12622new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12623{
12624 NODE *ret;
12625 if (block) block_dup_check(p, args, block);
12626 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12627 if (block) ret = method_add_block(p, ret, block, loc);
12628 fixpos(ret, recv);
12629 return ret;
12630}
12631
12632#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12633
12634static NODE*
12635last_expr_once_body(NODE *node)
12636{
12637 if (!node) return 0;
12638 return nd_once_body(node);
12639}
12640
12641static NODE*
12642match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12643{
12644 NODE *n;
12645 int line = op_loc->beg_pos.lineno;
12646
12647 value_expr(node1);
12648 value_expr(node2);
12649
12650 if ((n = last_expr_once_body(node1)) != 0) {
12651 switch (nd_type(n)) {
12652 case NODE_DREGX:
12653 {
12654 NODE *match = NEW_MATCH2(node1, node2, loc);
12655 nd_set_line(match, line);
12656 return match;
12657 }
12658
12659 case NODE_LIT:
12660 if (RB_TYPE_P(RNODE_LIT(n)->nd_lit, T_REGEXP)) {
12661 const VALUE lit = RNODE_LIT(n)->nd_lit;
12662 NODE *match = NEW_MATCH2(node1, node2, loc);
12663 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc);
12664 nd_set_line(match, line);
12665 return match;
12666 }
12667 }
12668 }
12669
12670 if ((n = last_expr_once_body(node2)) != 0) {
12671 NODE *match3;
12672
12673 switch (nd_type(n)) {
12674 case NODE_LIT:
12675 if (!RB_TYPE_P(RNODE_LIT(n)->nd_lit, T_REGEXP)) break;
12676 /* fallthru */
12677 case NODE_DREGX:
12678 match3 = NEW_MATCH3(node2, node1, loc);
12679 return match3;
12680 }
12681 }
12682
12683 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12684 nd_set_line(n, line);
12685 return n;
12686}
12687
12688# if WARN_PAST_SCOPE
12689static int
12690past_dvar_p(struct parser_params *p, ID id)
12691{
12692 struct vtable *past = p->lvtbl->past;
12693 while (past) {
12694 if (vtable_included(past, id)) return 1;
12695 past = past->prev;
12696 }
12697 return 0;
12698}
12699# endif
12700
12701static int
12702numparam_nested_p(struct parser_params *p)
12703{
12704 struct local_vars *local = p->lvtbl;
12705 NODE *outer = local->numparam.outer;
12706 NODE *inner = local->numparam.inner;
12707 if (outer || inner) {
12708 NODE *used = outer ? outer : inner;
12709 compile_error(p, "numbered parameter is already used in\n"
12710 "%s:%d: %s block here",
12711 p->ruby_sourcefile, nd_line(used),
12712 outer ? "outer" : "inner");
12713 parser_show_error_line(p, &used->nd_loc);
12714 return 1;
12715 }
12716 return 0;
12717}
12718
12719static NODE*
12720gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12721{
12722 ID *vidp = NULL;
12723 NODE *node;
12724 switch (id) {
12725 case keyword_self:
12726 return NEW_SELF(loc);
12727 case keyword_nil:
12728 return NEW_NIL(loc);
12729 case keyword_true:
12730 return NEW_TRUE(loc);
12731 case keyword_false:
12732 return NEW_FALSE(loc);
12733 case keyword__FILE__:
12734 {
12735 VALUE file = p->ruby_sourcefile_string;
12736 if (NIL_P(file))
12737 file = rb_str_new(0, 0);
12738 else
12739 file = rb_str_dup(file);
12740 node = NEW_STR(file, loc);
12741 RB_OBJ_WRITTEN(p->ast, Qnil, file);
12742 }
12743 return node;
12744 case keyword__LINE__:
12745 return NEW_LIT(INT2FIX(loc->beg_pos.lineno), loc);
12746 case keyword__ENCODING__:
12747 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
12748 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit);
12749 return node;
12750
12751 }
12752 switch (id_type(id)) {
12753 case ID_LOCAL:
12754 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
12755 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
12756 if (id == p->cur_arg) {
12757 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
12758 return 0;
12759 }
12760 if (vidp) *vidp |= LVAR_USED;
12761 node = NEW_DVAR(id, loc);
12762 return node;
12763 }
12764 if (local_id_ref(p, id, &vidp)) {
12765 if (id == p->cur_arg) {
12766 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
12767 return 0;
12768 }
12769 if (vidp) *vidp |= LVAR_USED;
12770 node = NEW_LVAR(id, loc);
12771 return node;
12772 }
12773 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
12774 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
12775 if (numparam_nested_p(p)) return 0;
12776 node = NEW_DVAR(id, loc);
12777 struct local_vars *local = p->lvtbl;
12778 if (!local->numparam.current) local->numparam.current = node;
12779 return node;
12780 }
12781# if WARN_PAST_SCOPE
12782 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
12783 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
12784 }
12785# endif
12786 /* method call without arguments */
12787 if (dyna_in_block(p) && id == rb_intern("it")
12788 && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))
12789 && p->max_numparam != ORDINAL_PARAM) {
12790 rb_warn0("`it` calls without arguments will refer to the first block param in Ruby 3.4; use it() or self.it");
12791 }
12792 return NEW_VCALL(id, loc);
12793 case ID_GLOBAL:
12794 return NEW_GVAR(id, loc);
12795 case ID_INSTANCE:
12796 return NEW_IVAR(id, loc);
12797 case ID_CONST:
12798 return NEW_CONST(id, loc);
12799 case ID_CLASS:
12800 return NEW_CVAR(id, loc);
12801 }
12802 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
12803 return 0;
12804}
12805
12806static rb_node_opt_arg_t *
12807opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
12808{
12809 rb_node_opt_arg_t *opts = opt_list;
12810 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
12811
12812 while (opts->nd_next) {
12813 opts = opts->nd_next;
12814 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
12815 }
12816 opts->nd_next = opt;
12817
12818 return opt_list;
12819}
12820
12821static rb_node_kw_arg_t *
12822kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
12823{
12824 if (kwlist) {
12825 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
12826 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
12827 }
12828 return kwlist;
12829}
12830
12831static NODE *
12832new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
12833{
12834 NODE *n = expr;
12835 while (n) {
12836 if (nd_type_p(n, NODE_BEGIN)) {
12837 n = RNODE_BEGIN(n)->nd_body;
12838 }
12839 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
12840 n = RNODE_BLOCK(n)->nd_head;
12841 }
12842 else {
12843 break;
12844 }
12845 }
12846 return NEW_DEFINED(n, loc);
12847}
12848
12849static VALUE
12850str_to_sym_check(struct parser_params *p, VALUE lit, const YYLTYPE *loc)
12851{
12852 if (rb_enc_str_coderange(lit) == ENC_CODERANGE_BROKEN) {
12853 yyerror1(loc, "invalid symbol");
12854 lit = STR_NEW0();
12855 }
12856
12857 return lit;
12858}
12859
12860static NODE*
12861symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
12862{
12863 VALUE lit;
12864
12865 enum node_type type = nd_type(symbol);
12866 switch (type) {
12867 case NODE_DSTR:
12868 nd_set_type(symbol, NODE_DSYM);
12869 break;
12870 case NODE_STR:
12871 nd_set_type(symbol, NODE_LIT);
12872 lit = str_to_sym_check(p, RNODE_LIT(symbol)->nd_lit, &RNODE(symbol)->nd_loc);
12873 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(symbol)->nd_lit = rb_str_intern(lit));
12874 break;
12875 default:
12876 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
12877 }
12878 return list_append(p, symbols, symbol);
12879}
12880
12881static NODE *
12882new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
12883{
12884 struct RNode_LIST *list;
12885 NODE *prev;
12886 VALUE lit;
12887
12888 if (!node) {
12889 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
12890 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit);
12891 return node;
12892 }
12893 switch (nd_type(node)) {
12894 case NODE_STR:
12895 {
12896 VALUE src = RNODE_STR(node)->nd_lit;
12897 nd_set_type(node, NODE_LIT);
12898 nd_set_loc(node, loc);
12899 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit = reg_compile(p, src, options));
12900 }
12901 break;
12902 default:
12903 lit = STR_NEW0();
12904 node = NEW_DSTR0(lit, 1, NEW_LIST(node, loc), loc);
12905 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12906 /* fall through */
12907 case NODE_DSTR:
12908 nd_set_type(node, NODE_DREGX);
12909 nd_set_loc(node, loc);
12910 RNODE_DREGX(node)->nd_cflag = options & RE_OPTION_MASK;
12911 if (!NIL_P(RNODE_DREGX(node)->nd_lit)) reg_fragment_check(p, RNODE_DREGX(node)->nd_lit, options);
12912 for (list = RNODE_DREGX(prev = node)->nd_next; list; list = RNODE_LIST(list->nd_next)) {
12913 NODE *frag = list->nd_head;
12914 enum node_type type = nd_type(frag);
12915 if (type == NODE_STR || (type == NODE_DSTR && !RNODE_DSTR(frag)->nd_next)) {
12916 VALUE tail = RNODE_STR(frag)->nd_lit;
12917 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(RNODE_DREGX(prev)->nd_lit)) {
12918 VALUE lit = prev == node ? RNODE_DREGX(prev)->nd_lit : RNODE_LIT(RNODE_LIST(prev)->nd_head)->nd_lit;
12919 if (!literal_concat0(p, lit, tail)) {
12920 return NEW_NIL(loc); /* dummy node on error */
12921 }
12922 rb_str_resize(tail, 0);
12923 RNODE_LIST(prev)->nd_next = list->nd_next;
12924 rb_discard_node(p, list->nd_head);
12925 rb_discard_node(p, (NODE *)list);
12926 list = RNODE_LIST(prev);
12927 }
12928 else {
12929 prev = (NODE *)list;
12930 }
12931 }
12932 else {
12933 prev = 0;
12934 }
12935 }
12936 if (!RNODE_DREGX(node)->nd_next) {
12937 VALUE src = RNODE_DREGX(node)->nd_lit;
12938 VALUE re = reg_compile(p, src, options);
12939 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_DREGX(node)->nd_lit = re);
12940 }
12941 if (options & RE_OPTION_ONCE) {
12942 node = NEW_ONCE(node, loc);
12943 }
12944 break;
12945 }
12946 return node;
12947}
12948
12949static rb_node_kw_arg_t *
12950new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
12951{
12952 if (!k) return 0;
12953 return NEW_KW_ARG((k), loc);
12954}
12955
12956static NODE *
12957new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12958{
12959 if (!node) {
12960 VALUE lit = STR_NEW0();
12961 NODE *xstr = NEW_XSTR(lit, loc);
12962 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12963 return xstr;
12964 }
12965 switch (nd_type(node)) {
12966 case NODE_STR:
12967 nd_set_type(node, NODE_XSTR);
12968 nd_set_loc(node, loc);
12969 break;
12970 case NODE_DSTR:
12971 nd_set_type(node, NODE_DXSTR);
12972 nd_set_loc(node, loc);
12973 break;
12974 default:
12975 node = NEW_DXSTR(Qnil, 1, NEW_LIST(node, loc), loc);
12976 break;
12977 }
12978 return node;
12979}
12980
12981static void
12982check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
12983{
12984 VALUE lit;
12985
12986 if (!arg || !p->case_labels) return;
12987
12988 lit = rb_node_case_when_optimizable_literal(arg);
12989 if (UNDEF_P(lit)) return;
12990 if (nd_type_p(arg, NODE_STR)) {
12991 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(arg)->nd_lit = lit);
12992 }
12993
12994 if (NIL_P(p->case_labels)) {
12995 p->case_labels = rb_obj_hide(rb_hash_new());
12996 }
12997 else {
12998 VALUE line = rb_hash_lookup(p->case_labels, lit);
12999 if (!NIL_P(line)) {
13000 rb_warning1("duplicated `when' clause with line %d is ignored",
13001 WARN_IVAL(line));
13002 return;
13003 }
13004 }
13005 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
13006}
13007
13008#else /* !RIPPER */
13009static int
13010id_is_var(struct parser_params *p, ID id)
13011{
13012 if (is_notop_id(id)) {
13013 switch (id & ID_SCOPE_MASK) {
13014 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13015 return 1;
13016 case ID_LOCAL:
13017 if (dyna_in_block(p)) {
13018 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13019 }
13020 if (local_id(p, id)) return 1;
13021 /* method call without arguments */
13022 return 0;
13023 }
13024 }
13025 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13026 return 0;
13027}
13028
13029static VALUE
13030new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
13031{
13032 VALUE src = 0, err = 0;
13033 int options = 0;
13034 if (ripper_is_node_yylval(p, re)) {
13035 src = RNODE_RIPPER(re)->nd_cval;
13036 re = RNODE_RIPPER(re)->nd_rval;
13037 }
13038 if (ripper_is_node_yylval(p, opt)) {
13039 options = (int)RNODE_RIPPER(opt)->nd_vid;
13040 opt = RNODE_RIPPER(opt)->nd_rval;
13041 }
13042 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
13043 compile_error(p, "%"PRIsVALUE, err);
13044 }
13045 return dispatch2(regexp_literal, re, opt);
13046}
13047#endif /* !RIPPER */
13048
13049static inline enum lex_state_e
13050parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13051{
13052 if (p->debug) {
13053 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13054 }
13055 return p->lex.state = ls;
13056}
13057
13058#ifndef RIPPER
13059static const char rb_parser_lex_state_names[][8] = {
13060 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13061 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13062 "LABEL", "LABELED","FITEM",
13063};
13064
13065static VALUE
13066append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13067{
13068 int i, sep = 0;
13069 unsigned int mask = 1;
13070 static const char none[] = "NONE";
13071
13072 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13073 if ((unsigned)state & mask) {
13074 if (sep) {
13075 rb_str_cat(buf, "|", 1);
13076 }
13077 sep = 1;
13078 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13079 }
13080 }
13081 if (!sep) {
13082 rb_str_cat(buf, none, sizeof(none)-1);
13083 }
13084 return buf;
13085}
13086
13087static void
13088flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13089{
13090 VALUE mesg = p->debug_buffer;
13091
13092 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13093 p->debug_buffer = Qnil;
13094 rb_io_puts(1, &mesg, out);
13095 }
13096 if (!NIL_P(str) && RSTRING_LEN(str)) {
13097 rb_io_write(p->debug_output, str);
13098 }
13099}
13100
13101enum lex_state_e
13102rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13103 enum lex_state_e to, int line)
13104{
13105 VALUE mesg;
13106 mesg = rb_str_new_cstr("lex_state: ");
13107 append_lex_state_name(p, from, mesg);
13108 rb_str_cat_cstr(mesg, " -> ");
13109 append_lex_state_name(p, to, mesg);
13110 rb_str_catf(mesg, " at line %d\n", line);
13111 flush_debug_buffer(p, p->debug_output, mesg);
13112 return to;
13113}
13114
13115VALUE
13116rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13117{
13118 return rb_fstring(append_lex_state_name(p, state, rb_str_new(0, 0)));
13119}
13120
13121static void
13122append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13123{
13124 if (stack == 0) {
13125 rb_str_cat_cstr(mesg, "0");
13126 }
13127 else {
13128 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13129 for (; mask && !(stack & mask); mask >>= 1) continue;
13130 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13131 }
13132}
13133
13134void
13135rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13136 const char *name, int line)
13137{
13138 VALUE mesg = rb_sprintf("%s: ", name);
13139 append_bitstack_value(p, stack, mesg);
13140 rb_str_catf(mesg, " at line %d\n", line);
13141 flush_debug_buffer(p, p->debug_output, mesg);
13142}
13143
13144void
13145rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13146{
13147 va_list ap;
13148 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13149
13150 va_start(ap, fmt);
13151 rb_str_vcatf(mesg, fmt, ap);
13152 va_end(ap);
13153 yyerror0(RSTRING_PTR(mesg));
13154 RB_GC_GUARD(mesg);
13155
13156 mesg = rb_str_new(0, 0);
13157 append_lex_state_name(p, p->lex.state, mesg);
13158 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13159 rb_str_resize(mesg, 0);
13160 append_bitstack_value(p, p->cond_stack, mesg);
13161 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13162 rb_str_resize(mesg, 0);
13163 append_bitstack_value(p, p->cmdarg_stack, mesg);
13164 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13165 if (p->debug_output == rb_ractor_stdout())
13166 p->debug_output = rb_ractor_stderr();
13167 p->debug = TRUE;
13168}
13169
13170static YYLTYPE *
13171rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13172{
13173 yylloc->beg_pos.lineno = sourceline;
13174 yylloc->beg_pos.column = beg_pos;
13175 yylloc->end_pos.lineno = sourceline;
13176 yylloc->end_pos.column = end_pos;
13177 return yylloc;
13178}
13179
13180YYLTYPE *
13181rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13182{
13183 int sourceline = here->sourceline;
13184 int beg_pos = (int)here->offset - here->quote
13185 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13186 int end_pos = (int)here->offset + here->length + here->quote;
13187
13188 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13189}
13190
13191YYLTYPE *
13192rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13193{
13194 yylloc->beg_pos.lineno = p->delayed.beg_line;
13195 yylloc->beg_pos.column = p->delayed.beg_col;
13196 yylloc->end_pos.lineno = p->delayed.end_line;
13197 yylloc->end_pos.column = p->delayed.end_col;
13198
13199 return yylloc;
13200}
13201
13202YYLTYPE *
13203rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13204{
13205 int sourceline = p->ruby_sourceline;
13206 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13207 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13208 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13209}
13210
13211YYLTYPE *
13212rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13213{
13214 yylloc->end_pos = yylloc->beg_pos;
13215
13216 return yylloc;
13217}
13218
13219YYLTYPE *
13220rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13221{
13222 int sourceline = p->ruby_sourceline;
13223 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13224 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13225 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13226}
13227
13228YYLTYPE *
13229rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13230{
13231 int sourceline = p->ruby_sourceline;
13232 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13233 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13234 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13235}
13236#endif /* !RIPPER */
13237
13238static int
13239assignable0(struct parser_params *p, ID id, const char **err)
13240{
13241 if (!id) return -1;
13242 switch (id) {
13243 case keyword_self:
13244 *err = "Can't change the value of self";
13245 return -1;
13246 case keyword_nil:
13247 *err = "Can't assign to nil";
13248 return -1;
13249 case keyword_true:
13250 *err = "Can't assign to true";
13251 return -1;
13252 case keyword_false:
13253 *err = "Can't assign to false";
13254 return -1;
13255 case keyword__FILE__:
13256 *err = "Can't assign to __FILE__";
13257 return -1;
13258 case keyword__LINE__:
13259 *err = "Can't assign to __LINE__";
13260 return -1;
13261 case keyword__ENCODING__:
13262 *err = "Can't assign to __ENCODING__";
13263 return -1;
13264 }
13265 switch (id_type(id)) {
13266 case ID_LOCAL:
13267 if (dyna_in_block(p)) {
13268 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13269 compile_error(p, "Can't assign to numbered parameter _%d",
13270 NUMPARAM_ID_TO_IDX(id));
13271 return -1;
13272 }
13273 if (dvar_curr(p, id)) return NODE_DASGN;
13274 if (dvar_defined(p, id)) return NODE_DASGN;
13275 if (local_id(p, id)) return NODE_LASGN;
13276 dyna_var(p, id);
13277 return NODE_DASGN;
13278 }
13279 else {
13280 if (!local_id(p, id)) local_var(p, id);
13281 return NODE_LASGN;
13282 }
13283 break;
13284 case ID_GLOBAL: return NODE_GASGN;
13285 case ID_INSTANCE: return NODE_IASGN;
13286 case ID_CONST:
13287 if (!p->ctxt.in_def) return NODE_CDECL;
13288 *err = "dynamic constant assignment";
13289 return -1;
13290 case ID_CLASS: return NODE_CVASGN;
13291 default:
13292 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13293 }
13294 return -1;
13295}
13296
13297#ifndef RIPPER
13298static NODE*
13299assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13300{
13301 const char *err = 0;
13302 int node_type = assignable0(p, id, &err);
13303 switch (node_type) {
13304 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13305 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13306 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13307 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13308 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
13309 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13310 }
13311 if (err) yyerror1(loc, err);
13312 return NEW_ERROR(loc);
13313}
13314#else
13315static VALUE
13316assignable(struct parser_params *p, VALUE lhs)
13317{
13318 const char *err = 0;
13319 assignable0(p, get_id(lhs), &err);
13320 if (err) lhs = assign_error(p, err, lhs);
13321 return lhs;
13322}
13323#endif
13324
13325static int
13326is_private_local_id(struct parser_params *p, ID name)
13327{
13328 VALUE s;
13329 if (name == idUScore) return 1;
13330 if (!is_local_id(name)) return 0;
13331 s = rb_id2str(name);
13332 if (!s) return 0;
13333 return RSTRING_PTR(s)[0] == '_';
13334}
13335
13336static int
13337shadowing_lvar_0(struct parser_params *p, ID name)
13338{
13339 if (dyna_in_block(p)) {
13340 if (dvar_curr(p, name)) {
13341 if (is_private_local_id(p, name)) return 1;
13342 yyerror0("duplicated argument name");
13343 }
13344 else if (dvar_defined(p, name) || local_id(p, name)) {
13345 vtable_add(p->lvtbl->vars, name);
13346 if (p->lvtbl->used) {
13347 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13348 }
13349 return 0;
13350 }
13351 }
13352 else {
13353 if (local_id(p, name)) {
13354 if (is_private_local_id(p, name)) return 1;
13355 yyerror0("duplicated argument name");
13356 }
13357 }
13358 return 1;
13359}
13360
13361static ID
13362shadowing_lvar(struct parser_params *p, ID name)
13363{
13364 shadowing_lvar_0(p, name);
13365 return name;
13366}
13367
13368static void
13369new_bv(struct parser_params *p, ID name)
13370{
13371 if (!name) return;
13372 if (!is_local_id(name)) {
13373 compile_error(p, "invalid local variable - %"PRIsVALUE,
13374 rb_id2str(name));
13375 return;
13376 }
13377 if (!shadowing_lvar_0(p, name)) return;
13378 dyna_var(p, name);
13379}
13380
13381#ifndef RIPPER
13382static NODE *
13383aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13384{
13385 return NEW_ATTRASGN(recv, tASET, idx, loc);
13386}
13387
13388static void
13389block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13390{
13391 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13392 compile_error(p, "both block arg and actual block given");
13393 }
13394}
13395
13396static NODE *
13397attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13398{
13399 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13400 return NEW_ATTRASGN(recv, id, 0, loc);
13401}
13402
13403static void
13404rb_backref_error(struct parser_params *p, NODE *node)
13405{
13406 switch (nd_type(node)) {
13407 case NODE_NTH_REF:
13408 compile_error(p, "Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13409 break;
13410 case NODE_BACK_REF:
13411 compile_error(p, "Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13412 break;
13413 }
13414}
13415#else
13416static VALUE
13417backref_error(struct parser_params *p, NODE *ref, VALUE expr)
13418{
13419 VALUE mesg = rb_str_new_cstr("Can't set variable ");
13420 rb_str_append(mesg, RNODE_RIPPER(ref)->nd_cval);
13421 return dispatch2(assign_error, mesg, expr);
13422}
13423#endif
13424
13425#ifndef RIPPER
13426static NODE *
13427arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13428{
13429 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13430 switch (nd_type(node1)) {
13431 case NODE_LIST:
13432 return list_append(p, node1, node2);
13433 case NODE_BLOCK_PASS:
13434 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13435 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13436 return node1;
13437 case NODE_ARGSPUSH:
13438 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13439 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13440 nd_set_type(node1, NODE_ARGSCAT);
13441 return node1;
13442 case NODE_ARGSCAT:
13443 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13444 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13445 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13446 return node1;
13447 }
13448 return NEW_ARGSPUSH(node1, node2, loc);
13449}
13450
13451static NODE *
13452arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13453{
13454 if (!node2) return node1;
13455 switch (nd_type(node1)) {
13456 case NODE_BLOCK_PASS:
13457 if (RNODE_BLOCK_PASS(node1)->nd_head)
13458 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13459 else
13460 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13461 return node1;
13462 case NODE_ARGSPUSH:
13463 if (!nd_type_p(node2, NODE_LIST)) break;
13464 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13465 nd_set_type(node1, NODE_ARGSCAT);
13466 return node1;
13467 case NODE_ARGSCAT:
13468 if (!nd_type_p(node2, NODE_LIST) ||
13469 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13470 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13471 return node1;
13472 }
13473 return NEW_ARGSCAT(node1, node2, loc);
13474}
13475
13476static NODE *
13477last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13478{
13479 NODE *n1;
13480 if ((n1 = splat_array(args)) != 0) {
13481 return list_append(p, n1, last_arg);
13482 }
13483 return arg_append(p, args, last_arg, loc);
13484}
13485
13486static NODE *
13487rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13488{
13489 NODE *n1;
13490 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13491 return list_concat(n1, rest_arg);
13492 }
13493 return arg_concat(p, args, rest_arg, loc);
13494}
13495
13496static NODE *
13497splat_array(NODE* node)
13498{
13499 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13500 if (nd_type_p(node, NODE_LIST)) return node;
13501 return 0;
13502}
13503
13504static void
13505mark_lvar_used(struct parser_params *p, NODE *rhs)
13506{
13507 ID *vidp = NULL;
13508 if (!rhs) return;
13509 switch (nd_type(rhs)) {
13510 case NODE_LASGN:
13511 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13512 if (vidp) *vidp |= LVAR_USED;
13513 }
13514 break;
13515 case NODE_DASGN:
13516 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13517 if (vidp) *vidp |= LVAR_USED;
13518 }
13519 break;
13520#if 0
13521 case NODE_MASGN:
13522 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13523 mark_lvar_used(p, rhs->nd_head);
13524 }
13525 break;
13526#endif
13527 }
13528}
13529
13530static NODE *
13531const_decl_path(struct parser_params *p, NODE **dest)
13532{
13533 NODE *n = *dest;
13534 if (!nd_type_p(n, NODE_CALL)) {
13535 const YYLTYPE *loc = &n->nd_loc;
13536 VALUE path;
13537 if (RNODE_CDECL(n)->nd_vid) {
13538 path = rb_id2str(RNODE_CDECL(n)->nd_vid);
13539 }
13540 else {
13541 n = RNODE_CDECL(n)->nd_else;
13542 path = rb_ary_new();
13543 for (; n && nd_type_p(n, NODE_COLON2); n = RNODE_COLON2(n)->nd_head) {
13544 rb_ary_push(path, rb_id2str(RNODE_COLON2(n)->nd_mid));
13545 }
13546 if (n && nd_type_p(n, NODE_CONST)) {
13547 // Const::Name
13548 rb_ary_push(path, rb_id2str(RNODE_CONST(n)->nd_vid));
13549 }
13550 else if (n && nd_type_p(n, NODE_COLON3)) {
13551 // ::Const::Name
13552 rb_ary_push(path, rb_str_new(0, 0));
13553 }
13554 else {
13555 // expression::Name
13556 rb_ary_push(path, rb_str_new_cstr("..."));
13557 }
13558 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
13559 path = rb_fstring(path);
13560 }
13561 *dest = n = NEW_LIT(path, loc);
13562 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit);
13563 }
13564 return n;
13565}
13566
13567static NODE *
13568make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
13569{
13570 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
13571
13572 if (copy) {
13573 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
13574 NEW_LIST(value, loc), loc);
13575 }
13576 else {
13577 return NEW_CALL(fcore, rb_intern("make_shareable"),
13578 NEW_LIST(value, loc), loc);
13579 }
13580}
13581
13582static NODE *
13583ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
13584{
13585 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
13586 NODE *args = NEW_LIST(value, loc);
13587 args = list_append(p, args, const_decl_path(p, dest));
13588 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
13589}
13590
13591static int is_static_content(NODE *node);
13592
13593static VALUE
13594shareable_literal_value(struct parser_params *p, NODE *node)
13595{
13596 if (!node) return Qnil;
13597 enum node_type type = nd_type(node);
13598 switch (type) {
13599 case NODE_TRUE:
13600 return Qtrue;
13601 case NODE_FALSE:
13602 return Qfalse;
13603 case NODE_NIL:
13604 return Qnil;
13605 case NODE_LIT:
13606 return RNODE_LIT(node)->nd_lit;
13607 default:
13608 return Qundef;
13609 }
13610}
13611
13612#ifndef SHAREABLE_BARE_EXPRESSION
13613#define SHAREABLE_BARE_EXPRESSION 1
13614#endif
13615
13616static NODE *
13617shareable_literal_constant(struct parser_params *p, enum shareability shareable,
13618 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
13619{
13620# define shareable_literal_constant_next(n) \
13621 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
13622 VALUE lit = Qnil;
13623
13624 if (!value) return 0;
13625 enum node_type type = nd_type(value);
13626 switch (type) {
13627 case NODE_TRUE:
13628 case NODE_FALSE:
13629 case NODE_NIL:
13630 case NODE_LIT:
13631 return value;
13632
13633 case NODE_DSTR:
13634 if (shareable == shareable_literal) {
13635 value = NEW_CALL(value, idUMinus, 0, loc);
13636 }
13637 return value;
13638
13639 case NODE_STR:
13640 lit = rb_fstring(RNODE_STR(value)->nd_lit);
13641 nd_set_type(value, NODE_LIT);
13642 RB_OBJ_WRITE(p->ast, &RNODE_LIT(value)->nd_lit, lit);
13643 return value;
13644
13645 case NODE_ZLIST:
13646 lit = rb_ary_new();
13647 OBJ_FREEZE_RAW(lit);
13648 NODE *n = NEW_LIT(lit, loc);
13649 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit);
13650 return n;
13651
13652 case NODE_LIST:
13653 lit = rb_ary_new();
13654 for (NODE *n = value; n; n = RNODE_LIST(n)->nd_next) {
13655 NODE *elt = RNODE_LIST(n)->nd_head;
13656 if (elt) {
13657 elt = shareable_literal_constant_next(elt);
13658 if (elt) {
13659 RNODE_LIST(n)->nd_head = elt;
13660 }
13661 else if (RTEST(lit)) {
13662 rb_ary_clear(lit);
13663 lit = Qfalse;
13664 }
13665 }
13666 if (RTEST(lit)) {
13667 VALUE e = shareable_literal_value(p, elt);
13668 if (!UNDEF_P(e)) {
13669 rb_ary_push(lit, e);
13670 }
13671 else {
13672 rb_ary_clear(lit);
13673 lit = Qnil; /* make shareable at runtime */
13674 }
13675 }
13676 }
13677 break;
13678
13679 case NODE_HASH:
13680 if (!RNODE_HASH(value)->nd_brace) return 0;
13681 lit = rb_hash_new();
13682 for (NODE *n = RNODE_HASH(value)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
13683 NODE *key = RNODE_LIST(n)->nd_head;
13684 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
13685 if (key) {
13686 key = shareable_literal_constant_next(key);
13687 if (key) {
13688 RNODE_LIST(n)->nd_head = key;
13689 }
13690 else if (RTEST(lit)) {
13691 rb_hash_clear(lit);
13692 lit = Qfalse;
13693 }
13694 }
13695 if (val) {
13696 val = shareable_literal_constant_next(val);
13697 if (val) {
13698 RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head = val;
13699 }
13700 else if (RTEST(lit)) {
13701 rb_hash_clear(lit);
13702 lit = Qfalse;
13703 }
13704 }
13705 if (RTEST(lit)) {
13706 VALUE k = shareable_literal_value(p, key);
13707 VALUE v = shareable_literal_value(p, val);
13708 if (!UNDEF_P(k) && !UNDEF_P(v)) {
13709 rb_hash_aset(lit, k, v);
13710 }
13711 else {
13712 rb_hash_clear(lit);
13713 lit = Qnil; /* make shareable at runtime */
13714 }
13715 }
13716 }
13717 break;
13718
13719 default:
13720 if (shareable == shareable_literal &&
13721 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
13722 return ensure_shareable_node(p, dest, value, loc);
13723 }
13724 return 0;
13725 }
13726
13727 /* Array or Hash */
13728 if (!lit) return 0;
13729 if (NIL_P(lit)) {
13730 // if shareable_literal, all elements should have been ensured
13731 // as shareable
13732 value = make_shareable_node(p, value, false, loc);
13733 }
13734 else {
13735 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
13736 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(value)->nd_lit);
13737 }
13738
13739 return value;
13740# undef shareable_literal_constant_next
13741}
13742
13743static NODE *
13744shareable_constant_value(struct parser_params *p, enum shareability shareable,
13745 NODE *lhs, NODE *value, const YYLTYPE *loc)
13746{
13747 if (!value) return 0;
13748 switch (shareable) {
13749 case shareable_none:
13750 return value;
13751
13752 case shareable_literal:
13753 {
13754 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
13755 if (lit) return lit;
13756 return value;
13757 }
13758 break;
13759
13760 case shareable_copy:
13761 case shareable_everything:
13762 {
13763 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
13764 if (lit) return lit;
13765 return make_shareable_node(p, value, shareable == shareable_copy, loc);
13766 }
13767 break;
13768
13769 default:
13770 UNREACHABLE_RETURN(0);
13771 }
13772}
13773
13774static NODE *
13775node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13776{
13777 if (!lhs) return 0;
13778
13779 switch (nd_type(lhs)) {
13780 case NODE_CDECL:
13781 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
13782 /* fallthru */
13783
13784 case NODE_GASGN:
13785 case NODE_IASGN:
13786 case NODE_LASGN:
13787 case NODE_DASGN:
13788 case NODE_MASGN:
13789 case NODE_CVASGN:
13790 set_nd_value(p, lhs, rhs);
13791 nd_set_loc(lhs, loc);
13792 break;
13793
13794 case NODE_ATTRASGN:
13795 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13796 nd_set_loc(lhs, loc);
13797 break;
13798
13799 default:
13800 /* should not happen */
13801 break;
13802 }
13803
13804 return lhs;
13805}
13806
13807static NODE *
13808value_expr_check(struct parser_params *p, NODE *node)
13809{
13810 NODE *void_node = 0, *vn;
13811
13812 if (!node) {
13813 rb_warning0("empty expression");
13814 }
13815 while (node) {
13816 switch (nd_type(node)) {
13817 case NODE_RETURN:
13818 case NODE_BREAK:
13819 case NODE_NEXT:
13820 case NODE_REDO:
13821 case NODE_RETRY:
13822 return void_node ? void_node : node;
13823
13824 case NODE_CASE3:
13825 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13826 compile_error(p, "unexpected node");
13827 return NULL;
13828 }
13829 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13830 return NULL;
13831 }
13832 /* single line pattern matching with "=>" operator */
13833 return void_node ? void_node : node;
13834
13835 case NODE_BLOCK:
13836 while (RNODE_BLOCK(node)->nd_next) {
13837 node = RNODE_BLOCK(node)->nd_next;
13838 }
13839 node = RNODE_BLOCK(node)->nd_head;
13840 break;
13841
13842 case NODE_BEGIN:
13843 node = RNODE_BEGIN(node)->nd_body;
13844 break;
13845
13846 case NODE_IF:
13847 case NODE_UNLESS:
13848 if (!RNODE_IF(node)->nd_body) {
13849 return NULL;
13850 }
13851 else if (!RNODE_IF(node)->nd_else) {
13852 return NULL;
13853 }
13854 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13855 if (!vn) return NULL;
13856 if (!void_node) void_node = vn;
13857 node = RNODE_IF(node)->nd_else;
13858 break;
13859
13860 case NODE_AND:
13861 case NODE_OR:
13862 node = RNODE_AND(node)->nd_1st;
13863 break;
13864
13865 case NODE_LASGN:
13866 case NODE_DASGN:
13867 case NODE_MASGN:
13868 mark_lvar_used(p, node);
13869 return NULL;
13870
13871 default:
13872 return NULL;
13873 }
13874 }
13875
13876 return NULL;
13877}
13878
13879static int
13880value_expr_gen(struct parser_params *p, NODE *node)
13881{
13882 NODE *void_node = value_expr_check(p, node);
13883 if (void_node) {
13884 yyerror1(&void_node->nd_loc, "void value expression");
13885 /* or "control never reach"? */
13886 return FALSE;
13887 }
13888 return TRUE;
13889}
13890
13891static void
13892void_expr(struct parser_params *p, NODE *node)
13893{
13894 const char *useless = 0;
13895
13896 if (!RTEST(ruby_verbose)) return;
13897
13898 if (!node || !(node = nd_once_body(node))) return;
13899 switch (nd_type(node)) {
13900 case NODE_OPCALL:
13901 switch (RNODE_OPCALL(node)->nd_mid) {
13902 case '+':
13903 case '-':
13904 case '*':
13905 case '/':
13906 case '%':
13907 case tPOW:
13908 case tUPLUS:
13909 case tUMINUS:
13910 case '|':
13911 case '^':
13912 case '&':
13913 case tCMP:
13914 case '>':
13915 case tGEQ:
13916 case '<':
13917 case tLEQ:
13918 case tEQ:
13919 case tNEQ:
13920 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13921 break;
13922 }
13923 break;
13924
13925 case NODE_LVAR:
13926 case NODE_DVAR:
13927 case NODE_GVAR:
13928 case NODE_IVAR:
13929 case NODE_CVAR:
13930 case NODE_NTH_REF:
13931 case NODE_BACK_REF:
13932 useless = "a variable";
13933 break;
13934 case NODE_CONST:
13935 useless = "a constant";
13936 break;
13937 case NODE_LIT:
13938 case NODE_STR:
13939 case NODE_DSTR:
13940 case NODE_DREGX:
13941 useless = "a literal";
13942 break;
13943 case NODE_COLON2:
13944 case NODE_COLON3:
13945 useless = "::";
13946 break;
13947 case NODE_DOT2:
13948 useless = "..";
13949 break;
13950 case NODE_DOT3:
13951 useless = "...";
13952 break;
13953 case NODE_SELF:
13954 useless = "self";
13955 break;
13956 case NODE_NIL:
13957 useless = "nil";
13958 break;
13959 case NODE_TRUE:
13960 useless = "true";
13961 break;
13962 case NODE_FALSE:
13963 useless = "false";
13964 break;
13965 case NODE_DEFINED:
13966 useless = "defined?";
13967 break;
13968 }
13969
13970 if (useless) {
13971 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
13972 }
13973}
13974
13975static NODE *
13976void_stmts(struct parser_params *p, NODE *node)
13977{
13978 NODE *const n = node;
13979 if (!RTEST(ruby_verbose)) return n;
13980 if (!node) return n;
13981 if (!nd_type_p(node, NODE_BLOCK)) return n;
13982
13983 while (RNODE_BLOCK(node)->nd_next) {
13984 void_expr(p, RNODE_BLOCK(node)->nd_head);
13985 node = RNODE_BLOCK(node)->nd_next;
13986 }
13987 return n;
13988}
13989
13990static NODE *
13991remove_begin(NODE *node)
13992{
13993 NODE **n = &node, *n1 = node;
13994 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
13995 *n = n1 = RNODE_BEGIN(n1)->nd_body;
13996 }
13997 return node;
13998}
13999
14000static void
14001reduce_nodes(struct parser_params *p, NODE **body)
14002{
14003 NODE *node = *body;
14004
14005 if (!node) {
14006 *body = NEW_NIL(&NULL_LOC);
14007 return;
14008 }
14009#define subnodes(type, n1, n2) \
14010 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14011 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14012 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14013
14014 while (node) {
14015 int newline = (int)(nd_fl_newline(node));
14016 switch (nd_type(node)) {
14017 end:
14018 case NODE_NIL:
14019 *body = 0;
14020 return;
14021 case NODE_RETURN:
14022 *body = node = RNODE_RETURN(node)->nd_stts;
14023 if (newline && node) nd_set_fl_newline(node);
14024 continue;
14025 case NODE_BEGIN:
14026 *body = node = RNODE_BEGIN(node)->nd_body;
14027 if (newline && node) nd_set_fl_newline(node);
14028 continue;
14029 case NODE_BLOCK:
14030 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14031 break;
14032 case NODE_IF:
14033 case NODE_UNLESS:
14034 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14035 return;
14036 case NODE_CASE:
14037 body = &RNODE_CASE(node)->nd_body;
14038 break;
14039 case NODE_WHEN:
14040 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14041 break;
14042 case NODE_ENSURE:
14043 if (!subnodes(RNODE_ENSURE, nd_head, nd_resq)) goto end;
14044 break;
14045 case NODE_RESCUE:
14046 newline = 0; // RESBODY should not be a NEWLINE
14047 if (RNODE_RESCUE(node)->nd_else) {
14048 body = &RNODE_RESCUE(node)->nd_resq;
14049 break;
14050 }
14051 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14052 break;
14053 default:
14054 return;
14055 }
14056 node = *body;
14057 if (newline && node) nd_set_fl_newline(node);
14058 }
14059
14060#undef subnodes
14061}
14062
14063static int
14064is_static_content(NODE *node)
14065{
14066 if (!node) return 1;
14067 switch (nd_type(node)) {
14068 case NODE_HASH:
14069 if (!(node = RNODE_HASH(node)->nd_head)) break;
14070 case NODE_LIST:
14071 do {
14072 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14073 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14074 case NODE_LIT:
14075 case NODE_STR:
14076 case NODE_NIL:
14077 case NODE_TRUE:
14078 case NODE_FALSE:
14079 case NODE_ZLIST:
14080 break;
14081 default:
14082 return 0;
14083 }
14084 return 1;
14085}
14086
14087static int
14088assign_in_cond(struct parser_params *p, NODE *node)
14089{
14090 switch (nd_type(node)) {
14091 case NODE_MASGN:
14092 case NODE_LASGN:
14093 case NODE_DASGN:
14094 case NODE_GASGN:
14095 case NODE_IASGN:
14096 case NODE_CVASGN:
14097 case NODE_CDECL:
14098 break;
14099
14100 default:
14101 return 0;
14102 }
14103
14104 if (!get_nd_value(p, node)) return 1;
14105 if (is_static_content(get_nd_value(p, node))) {
14106 /* reports always */
14107 parser_warn(p, get_nd_value(p, node), "found `= literal' in conditional, should be ==");
14108 }
14109 return 1;
14110}
14111
14112enum cond_type {
14113 COND_IN_OP,
14114 COND_IN_COND,
14115 COND_IN_FF
14116};
14117
14118#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14119 switch (t) { \
14120 case COND_IN_OP: break; \
14121 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14122 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14123 } \
14124} while (0)
14125
14126static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14127
14128static NODE*
14129range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14130{
14131 enum node_type type;
14132
14133 if (node == 0) return 0;
14134
14135 type = nd_type(node);
14136 value_expr(node);
14137 if (type == NODE_LIT && FIXNUM_P(RNODE_LIT(node)->nd_lit)) {
14138 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
14139 ID lineno = rb_intern("$.");
14140 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14141 }
14142 return cond0(p, node, COND_IN_FF, loc, true);
14143}
14144
14145static NODE*
14146cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14147{
14148 if (node == 0) return 0;
14149 if (!(node = nd_once_body(node))) return 0;
14150 assign_in_cond(p, node);
14151
14152 switch (nd_type(node)) {
14153 case NODE_BEGIN:
14154 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14155 break;
14156
14157 case NODE_DSTR:
14158 case NODE_EVSTR:
14159 case NODE_STR:
14160 SWITCH_BY_COND_TYPE(type, warn, "string ");
14161 break;
14162
14163 case NODE_DREGX:
14164 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14165
14166 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14167
14168 case NODE_BLOCK:
14169 {
14170 NODE *end = RNODE_BLOCK(node)->nd_end;
14171 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14172 if (top) top = node == end;
14173 *expr = cond0(p, *expr, type, loc, top);
14174 }
14175 break;
14176
14177 case NODE_AND:
14178 case NODE_OR:
14179 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14180 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14181 break;
14182
14183 case NODE_DOT2:
14184 case NODE_DOT3:
14185 if (!top) break;
14186 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14187 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14188 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
14189 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
14190 break;
14191
14192 case NODE_DSYM:
14193 warn_symbol:
14194 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14195 break;
14196
14197 case NODE_LIT:
14198 if (RB_TYPE_P(RNODE_LIT(node)->nd_lit, T_REGEXP)) {
14199 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14200 nd_set_type(node, NODE_MATCH);
14201 }
14202 else if (RNODE_LIT(node)->nd_lit == Qtrue ||
14203 RNODE_LIT(node)->nd_lit == Qfalse) {
14204 /* booleans are OK, e.g., while true */
14205 }
14206 else if (SYMBOL_P(RNODE_LIT(node)->nd_lit)) {
14207 goto warn_symbol;
14208 }
14209 else {
14210 SWITCH_BY_COND_TYPE(type, warning, "");
14211 }
14212 default:
14213 break;
14214 }
14215 return node;
14216}
14217
14218static NODE*
14219cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14220{
14221 if (node == 0) return 0;
14222 return cond0(p, node, COND_IN_COND, loc, true);
14223}
14224
14225static NODE*
14226method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14227{
14228 if (node == 0) return 0;
14229 return cond0(p, node, COND_IN_OP, loc, true);
14230}
14231
14232static NODE*
14233new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14234{
14235 YYLTYPE loc = {*pos, *pos};
14236 return NEW_NIL(&loc);
14237}
14238
14239static NODE*
14240new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14241{
14242 if (!cc) return right;
14243 cc = cond0(p, cc, COND_IN_COND, loc, true);
14244 return newline_node(NEW_IF(cc, left, right, loc));
14245}
14246
14247static NODE*
14248new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14249{
14250 if (!cc) return right;
14251 cc = cond0(p, cc, COND_IN_COND, loc, true);
14252 return newline_node(NEW_UNLESS(cc, left, right, loc));
14253}
14254
14255#define NEW_AND_OR(type, f, s, loc) (type == NODE_AND ? NEW_AND(f,s,loc) : NEW_OR(f,s,loc))
14256
14257static NODE*
14258logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14259 const YYLTYPE *op_loc, const YYLTYPE *loc)
14260{
14261 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14262 NODE *op;
14263 value_expr(left);
14264 if (left && nd_type_p(left, type)) {
14265 NODE *node = left, *second;
14266 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14267 node = second;
14268 }
14269 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc);
14270 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14271 left->nd_loc.end_pos = loc->end_pos;
14272 return left;
14273 }
14274 op = NEW_AND_OR(type, left, right, loc);
14275 nd_set_line(op, op_loc->beg_pos.lineno);
14276 return op;
14277}
14278
14279#undef NEW_AND_OR
14280
14281static void
14282no_blockarg(struct parser_params *p, NODE *node)
14283{
14284 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14285 compile_error(p, "block argument should not be given");
14286 }
14287}
14288
14289static NODE *
14290ret_args(struct parser_params *p, NODE *node)
14291{
14292 if (node) {
14293 no_blockarg(p, node);
14294 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14295 node = RNODE_LIST(node)->nd_head;
14296 }
14297 }
14298 return node;
14299}
14300
14301static NODE *
14302new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14303{
14304 if (node) no_blockarg(p, node);
14305
14306 return NEW_YIELD(node, loc);
14307}
14308
14309static VALUE
14310negate_lit(struct parser_params *p, VALUE lit)
14311{
14312 if (FIXNUM_P(lit)) {
14313 return LONG2FIX(-FIX2LONG(lit));
14314 }
14315 if (SPECIAL_CONST_P(lit)) {
14316#if USE_FLONUM
14317 if (FLONUM_P(lit)) {
14318 return DBL2NUM(-RFLOAT_VALUE(lit));
14319 }
14320#endif
14321 goto unknown;
14322 }
14323 switch (BUILTIN_TYPE(lit)) {
14324 case T_BIGNUM:
14325 bignum_negate(lit);
14326 lit = rb_big_norm(lit);
14327 break;
14328 case T_RATIONAL:
14329 rational_set_num(lit, negate_lit(p, rational_get_num(lit)));
14330 break;
14331 case T_COMPLEX:
14332 rcomplex_set_real(lit, negate_lit(p, rcomplex_get_real(lit)));
14333 rcomplex_set_imag(lit, negate_lit(p, rcomplex_get_imag(lit)));
14334 break;
14335 case T_FLOAT:
14336 lit = DBL2NUM(-RFLOAT_VALUE(lit));
14337 break;
14338 unknown:
14339 default:
14340 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
14341 rb_builtin_class_name(lit));
14342 break;
14343 }
14344 return lit;
14345}
14346
14347static NODE *
14348arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14349{
14350 if (node2) {
14351 if (!node1) return (NODE *)node2;
14352 node2->nd_head = node1;
14353 nd_set_first_lineno(node2, nd_first_lineno(node1));
14354 nd_set_first_column(node2, nd_first_column(node1));
14355 return (NODE *)node2;
14356 }
14357 return node1;
14358}
14359
14360static bool
14361args_info_empty_p(struct rb_args_info *args)
14362{
14363 if (args->pre_args_num) return false;
14364 if (args->post_args_num) return false;
14365 if (args->rest_arg) return false;
14366 if (args->opt_args) return false;
14367 if (args->block_arg) return false;
14368 if (args->kw_args) return false;
14369 if (args->kw_rest_arg) return false;
14370 return true;
14371}
14372
14373static rb_node_args_t *
14374new_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)
14375{
14376 struct rb_args_info *args = &tail->nd_ainfo;
14377
14378 if (args->forwarding) {
14379 if (rest_arg) {
14380 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14381 return tail;
14382 }
14383 rest_arg = idFWD_REST;
14384 }
14385
14386 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
14387 args->pre_init = pre_args ? pre_args->nd_next : 0;
14388
14389 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
14390 args->post_init = post_args ? post_args->nd_next : 0;
14391 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14392
14393 args->rest_arg = rest_arg;
14394
14395 args->opt_args = opt_args;
14396
14397#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14398 args->ruby2_keywords = args->forwarding;
14399#else
14400 args->ruby2_keywords = 0;
14401#endif
14402
14403 nd_set_loc(RNODE(tail), loc);
14404
14405 return tail;
14406}
14407
14408static rb_node_args_t *
14409new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14410{
14411 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14412 struct rb_args_info *args = &node->nd_ainfo;
14413 if (p->error_p) return node;
14414
14415 args->block_arg = block;
14416 args->kw_args = kw_args;
14417
14418 if (kw_args) {
14419 /*
14420 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14421 * variable order: k1, kr1, k2, &b, internal_id, krest
14422 * #=> <reorder>
14423 * variable order: kr1, k1, k2, internal_id, krest, &b
14424 */
14425 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14426 struct vtable *vtargs = p->lvtbl->args;
14427 rb_node_kw_arg_t *kwn = kw_args;
14428
14429 if (block) block = vtargs->tbl[vtargs->pos-1];
14430 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14431 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14432 while (kwn) {
14433 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14434 --kw_vars;
14435 --required_kw_vars;
14436 kwn = kwn->nd_next;
14437 }
14438
14439 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14440 ID vid = get_nd_vid(p, kwn->nd_body);
14441 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14442 *required_kw_vars++ = vid;
14443 }
14444 else {
14445 *kw_vars++ = vid;
14446 }
14447 }
14448
14449 arg_var(p, kw_bits);
14450 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14451 if (block) arg_var(p, block);
14452
14453 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14454 }
14455 else if (kw_rest_arg == idNil) {
14456 args->no_kwarg = 1;
14457 }
14458 else if (kw_rest_arg) {
14459 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14460 }
14461
14462 return node;
14463}
14464
14465static rb_node_args_t *
14466args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam)
14467{
14468 if (max_numparam > NO_PARAM) {
14469 if (!args) {
14470 YYLTYPE loc = RUBY_INIT_YYLLOC();
14471 args = new_args_tail(p, 0, 0, 0, 0);
14472 nd_set_loc(RNODE(args), &loc);
14473 }
14474 args->nd_ainfo.pre_args_num = max_numparam;
14475 }
14476 return args;
14477}
14478
14479static NODE*
14480new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14481{
14482 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14483
14484 if (pre_arg) {
14485 NODE *pre_args = NEW_LIST(pre_arg, loc);
14486 if (RNODE_ARYPTN(aryptn)->pre_args) {
14487 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14488 }
14489 else {
14490 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14491 }
14492 }
14493 return aryptn;
14494}
14495
14496static NODE*
14497new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14498{
14499 if (has_rest) {
14500 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14501 }
14502 else {
14503 rest_arg = NULL;
14504 }
14505 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14506
14507 return node;
14508}
14509
14510static NODE*
14511new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14512{
14513 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14514
14515 return fndptn;
14516}
14517
14518static NODE*
14519new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14520{
14521 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14522 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14523 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14524
14525 return node;
14526}
14527
14528static NODE*
14529new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14530{
14531 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14532 return hshptn;
14533}
14534
14535static NODE*
14536new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14537{
14538 NODE *node, *kw_rest_arg_node;
14539
14540 if (kw_rest_arg == idNil) {
14541 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14542 }
14543 else if (kw_rest_arg) {
14544 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14545 }
14546 else {
14547 kw_rest_arg_node = NULL;
14548 }
14549
14550 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14551
14552 return node;
14553}
14554
14555static NODE*
14556dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14557{
14558 VALUE lit;
14559
14560 if (!node) {
14561 return NEW_LIT(ID2SYM(idNULL), loc);
14562 }
14563
14564 switch (nd_type(node)) {
14565 case NODE_DSTR:
14566 nd_set_type(node, NODE_DSYM);
14567 nd_set_loc(node, loc);
14568 break;
14569 case NODE_STR:
14570 lit = str_to_sym_check(p, RNODE_STR(node)->nd_lit, &RNODE(node)->nd_loc);
14571 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(node)->nd_lit = ID2SYM(rb_intern_str(lit)));
14572 nd_set_type(node, NODE_LIT);
14573 nd_set_loc(node, loc);
14574 break;
14575 default:
14576 node = NEW_DSYM(Qnil, 1, NEW_LIST(node, loc), loc);
14577 break;
14578 }
14579 return node;
14580}
14581
14582static int
14583append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
14584{
14585 NODE *node = (NODE *)v;
14586 NODE **result = (NODE **)h;
14587 RNODE_LIST(node)->as.nd_alen = 2;
14588 RNODE_LIST(RNODE_LIST(node)->nd_next)->as.nd_end = RNODE_LIST(node)->nd_next;
14589 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next = 0;
14590 if (*result)
14591 list_concat(*result, node);
14592 else
14593 *result = node;
14594 return ST_CONTINUE;
14595}
14596
14597static NODE *
14598remove_duplicate_keys(struct parser_params *p, NODE *hash)
14599{
14600 struct st_hash_type literal_type = {
14601 literal_cmp,
14602 literal_hash,
14603 };
14604
14605 st_table *literal_keys = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14606 NODE *result = 0;
14607 NODE *last_expr = 0;
14608 rb_code_location_t loc = hash->nd_loc;
14609 while (hash && RNODE_LIST(hash)->nd_next) {
14610 NODE *head = RNODE_LIST(hash)->nd_head;
14611 NODE *value = RNODE_LIST(hash)->nd_next;
14612 NODE *next = RNODE_LIST(value)->nd_next;
14613 st_data_t key = (st_data_t)head;
14614 st_data_t data;
14615 RNODE_LIST(value)->nd_next = 0;
14616 if (!head) {
14617 key = (st_data_t)value;
14618 }
14619 else if (nd_type_p(head, NODE_LIT) &&
14620 st_delete(literal_keys, (key = (st_data_t)RNODE_LIT(head)->nd_lit, &key), &data)) {
14621 NODE *dup_value = (RNODE_LIST((NODE *)data))->nd_next;
14622 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
14623 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
14624 RNODE_LIT(head)->nd_lit, nd_line(head));
14625 if (dup_value == last_expr) {
14626 RNODE_LIST(value)->nd_head = block_append(p, RNODE_LIST(dup_value)->nd_head, RNODE_LIST(value)->nd_head);
14627 }
14628 else {
14629 RNODE_LIST(last_expr)->nd_head = block_append(p, RNODE_LIST(dup_value)->nd_head, RNODE_LIST(last_expr)->nd_head);
14630 }
14631 }
14632 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
14633 last_expr = !head || nd_type_p(head, NODE_LIT) ? value : head;
14634 hash = next;
14635 }
14636 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
14637 st_free_table(literal_keys);
14638 if (hash) {
14639 if (!result) result = hash;
14640 else list_concat(result, hash);
14641 }
14642 result->nd_loc = loc;
14643 return result;
14644}
14645
14646static NODE *
14647new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14648{
14649 if (hash) hash = remove_duplicate_keys(p, hash);
14650 return NEW_HASH(hash, loc);
14651}
14652#endif
14653
14654static void
14655error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14656{
14657 if (is_private_local_id(p, id)) {
14658 return;
14659 }
14660 if (st_is_member(p->pvtbl, id)) {
14661 yyerror1(loc, "duplicated variable name");
14662 }
14663 else {
14664 st_insert(p->pvtbl, (st_data_t)id, 0);
14665 }
14666}
14667
14668static void
14669error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14670{
14671 if (!p->pktbl) {
14672 p->pktbl = st_init_numtable();
14673 }
14674 else if (st_is_member(p->pktbl, key)) {
14675 yyerror1(loc, "duplicated key name");
14676 return;
14677 }
14678 st_insert(p->pktbl, (st_data_t)key, 0);
14679}
14680
14681#ifndef RIPPER
14682static NODE *
14683new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14684{
14685 return NEW_HASH(hash, loc);
14686}
14687#endif /* !RIPPER */
14688
14689#ifndef RIPPER
14690static NODE *
14691new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14692{
14693 NODE *asgn;
14694
14695 if (lhs) {
14696 ID vid = get_nd_vid(p, lhs);
14697 YYLTYPE lhs_loc = lhs->nd_loc;
14698 int shareable = ctxt.shareable_constant_value;
14699 if (shareable) {
14700 switch (nd_type(lhs)) {
14701 case NODE_CDECL:
14702 case NODE_COLON2:
14703 case NODE_COLON3:
14704 break;
14705 default:
14706 shareable = 0;
14707 break;
14708 }
14709 }
14710 if (op == tOROP) {
14711 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14712 set_nd_value(p, lhs, rhs);
14713 nd_set_loc(lhs, loc);
14714 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14715 }
14716 else if (op == tANDOP) {
14717 if (shareable) {
14718 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14719 }
14720 set_nd_value(p, lhs, rhs);
14721 nd_set_loc(lhs, loc);
14722 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14723 }
14724 else {
14725 asgn = lhs;
14726 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14727 if (shareable) {
14728 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14729 }
14730 set_nd_value(p, asgn, rhs);
14731 nd_set_loc(asgn, loc);
14732 }
14733 }
14734 else {
14735 asgn = NEW_ERROR(loc);
14736 }
14737 return asgn;
14738}
14739
14740static NODE *
14741new_ary_op_assign(struct parser_params *p, NODE *ary,
14742 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
14743{
14744 NODE *asgn;
14745
14746 args = make_list(args, args_loc);
14747 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc);
14748 fixpos(asgn, ary);
14749 return asgn;
14750}
14751
14752static NODE *
14753new_attr_op_assign(struct parser_params *p, NODE *lhs,
14754 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
14755{
14756 NODE *asgn;
14757
14758 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
14759 fixpos(asgn, lhs);
14760 return asgn;
14761}
14762
14763static NODE *
14764new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14765{
14766 NODE *asgn;
14767
14768 if (lhs) {
14769 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
14770 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
14771 }
14772 else {
14773 asgn = NEW_ERROR(loc);
14774 }
14775 fixpos(asgn, lhs);
14776 return asgn;
14777}
14778
14779static NODE *
14780const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14781{
14782 if (p->ctxt.in_def) {
14783 yyerror1(loc, "dynamic constant assignment");
14784 }
14785 return NEW_CDECL(0, 0, (path), loc);
14786}
14787#else
14788static VALUE
14789const_decl(struct parser_params *p, VALUE path)
14790{
14791 if (p->ctxt.in_def) {
14792 path = assign_error(p, "dynamic constant assignment", path);
14793 }
14794 return path;
14795}
14796
14797static VALUE
14798assign_error(struct parser_params *p, const char *mesg, VALUE a)
14799{
14800 a = dispatch2(assign_error, ERR_MESG(), a);
14801 ripper_error(p);
14802 return a;
14803}
14804
14805static VALUE
14806var_field(struct parser_params *p, VALUE a)
14807{
14808 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
14809}
14810#endif
14811
14812#ifndef RIPPER
14813static NODE *
14814new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14815{
14816 NODE *result = head;
14817 if (rescue) {
14818 NODE *tmp = rescue_else ? rescue_else : rescue;
14819 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14820
14821 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14822 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14823 }
14824 else if (rescue_else) {
14825 result = block_append(p, result, rescue_else);
14826 }
14827 if (ensure) {
14828 result = NEW_ENSURE(result, ensure, loc);
14829 }
14830 fixpos(result, head);
14831 return result;
14832}
14833#endif
14834
14835static void
14836warn_unused_var(struct parser_params *p, struct local_vars *local)
14837{
14838 int cnt;
14839
14840 if (!local->used) return;
14841 cnt = local->used->pos;
14842 if (cnt != local->vars->pos) {
14843 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14844 }
14845#ifndef RIPPER
14846 ID *v = local->vars->tbl;
14847 ID *u = local->used->tbl;
14848 for (int i = 0; i < cnt; ++i) {
14849 if (!v[i] || (u[i] & LVAR_USED)) continue;
14850 if (is_private_local_id(p, v[i])) continue;
14851 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14852 }
14853#endif
14854}
14855
14856static void
14857local_push(struct parser_params *p, int toplevel_scope)
14858{
14859 struct local_vars *local;
14860 int inherits_dvars = toplevel_scope && compile_for_eval;
14861 int warn_unused_vars = RTEST(ruby_verbose);
14862
14863 local = ALLOC(struct local_vars);
14864 local->prev = p->lvtbl;
14865 local->args = vtable_alloc(0);
14866 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14867#ifndef RIPPER
14868 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14869 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14870 local->numparam.outer = 0;
14871 local->numparam.inner = 0;
14872 local->numparam.current = 0;
14873#endif
14874 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14875
14876# if WARN_PAST_SCOPE
14877 local->past = 0;
14878# endif
14879 CMDARG_PUSH(0);
14880 COND_PUSH(0);
14881 p->lvtbl = local;
14882}
14883
14884static void
14885vtable_chain_free(struct parser_params *p, struct vtable *table)
14886{
14887 while (!DVARS_TERMINAL_P(table)) {
14888 struct vtable *cur_table = table;
14889 table = cur_table->prev;
14890 vtable_free(cur_table);
14891 }
14892}
14893
14894static void
14895local_free(struct parser_params *p, struct local_vars *local)
14896{
14897 vtable_chain_free(p, local->used);
14898
14899# if WARN_PAST_SCOPE
14900 vtable_chain_free(p, local->past);
14901# endif
14902
14903 vtable_chain_free(p, local->args);
14904 vtable_chain_free(p, local->vars);
14905
14906 ruby_sized_xfree(local, sizeof(struct local_vars));
14907}
14908
14909static void
14910local_pop(struct parser_params *p)
14911{
14912 struct local_vars *local = p->lvtbl->prev;
14913 if (p->lvtbl->used) {
14914 warn_unused_var(p, p->lvtbl);
14915 }
14916
14917 local_free(p, p->lvtbl);
14918 p->lvtbl = local;
14919
14920 CMDARG_POP();
14921 COND_POP();
14922}
14923
14924#ifndef RIPPER
14925static rb_ast_id_table_t *
14926local_tbl(struct parser_params *p)
14927{
14928 int cnt_args = vtable_size(p->lvtbl->args);
14929 int cnt_vars = vtable_size(p->lvtbl->vars);
14930 int cnt = cnt_args + cnt_vars;
14931 int i, j;
14932 rb_ast_id_table_t *tbl;
14933
14934 if (cnt <= 0) return 0;
14935 tbl = rb_ast_new_local_table(p->ast, cnt);
14936 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14937 /* remove IDs duplicated to warn shadowing */
14938 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14939 ID id = p->lvtbl->vars->tbl[i];
14940 if (!vtable_included(p->lvtbl->args, id)) {
14941 tbl->ids[j++] = id;
14942 }
14943 }
14944 if (j < cnt) {
14945 tbl = rb_ast_resize_latest_local_table(p->ast, j);
14946 }
14947
14948 return tbl;
14949}
14950
14951#endif
14952
14953static void
14954numparam_name(struct parser_params *p, ID id)
14955{
14956 if (!NUMPARAM_ID_P(id)) return;
14957 compile_error(p, "_%d is reserved for numbered parameter",
14958 NUMPARAM_ID_TO_IDX(id));
14959}
14960
14961static void
14962arg_var(struct parser_params *p, ID id)
14963{
14964 numparam_name(p, id);
14965 vtable_add(p->lvtbl->args, id);
14966}
14967
14968static void
14969local_var(struct parser_params *p, ID id)
14970{
14971 numparam_name(p, id);
14972 vtable_add(p->lvtbl->vars, id);
14973 if (p->lvtbl->used) {
14974 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
14975 }
14976}
14977
14978static int
14979local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
14980{
14981 struct vtable *vars, *args, *used;
14982
14983 vars = p->lvtbl->vars;
14984 args = p->lvtbl->args;
14985 used = p->lvtbl->used;
14986
14987 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
14988 vars = vars->prev;
14989 args = args->prev;
14990 if (used) used = used->prev;
14991 }
14992
14993 if (vars && vars->prev == DVARS_INHERIT) {
14994 return rb_local_defined(id, p->parent_iseq);
14995 }
14996 else if (vtable_included(args, id)) {
14997 return 1;
14998 }
14999 else {
15000 int i = vtable_included(vars, id);
15001 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15002 return i != 0;
15003 }
15004}
15005
15006static int
15007local_id(struct parser_params *p, ID id)
15008{
15009 return local_id_ref(p, id, NULL);
15010}
15011
15012static int
15013check_forwarding_args(struct parser_params *p)
15014{
15015 if (local_id(p, idFWD_ALL)) return TRUE;
15016 compile_error(p, "unexpected ...");
15017 return FALSE;
15018}
15019
15020static void
15021add_forwarding_args(struct parser_params *p)
15022{
15023 arg_var(p, idFWD_REST);
15024#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15025 arg_var(p, idFWD_KWREST);
15026#endif
15027 arg_var(p, idFWD_BLOCK);
15028 arg_var(p, idFWD_ALL);
15029}
15030
15031static void
15032forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15033{
15034 bool conflict = false;
15035
15036 struct vtable *vars, *args;
15037
15038 vars = p->lvtbl->vars;
15039 args = p->lvtbl->args;
15040
15041 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15042 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15043 vars = vars->prev;
15044 args = args->prev;
15045 }
15046
15047 bool found = false;
15048 if (vars && vars->prev == DVARS_INHERIT && !found) {
15049 found = (rb_local_defined(arg, p->parent_iseq) &&
15050 !(all && rb_local_defined(all, p->parent_iseq)));
15051 }
15052 else {
15053 found = (vtable_included(args, arg) &&
15054 !(all && vtable_included(args, all)));
15055 }
15056
15057 if (!found) {
15058 compile_error(p, "no anonymous %s parameter", var);
15059 }
15060 else if (conflict) {
15061 compile_error(p, "anonymous %s parameter is also used within block", var);
15062 }
15063}
15064
15065#ifndef RIPPER
15066static NODE *
15067new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15068{
15069 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15070#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15071 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15072#endif
15073 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
15074 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc);
15075#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15076 args = arg_append(p, args, new_hash(p, kwrest, loc), loc);
15077#endif
15078 return arg_blk_pass(args, block);
15079}
15080#endif
15081
15082static NODE *
15083numparam_push(struct parser_params *p)
15084{
15085#ifndef RIPPER
15086 struct local_vars *local = p->lvtbl;
15087 NODE *inner = local->numparam.inner;
15088 if (!local->numparam.outer) {
15089 local->numparam.outer = local->numparam.current;
15090 }
15091 local->numparam.inner = 0;
15092 local->numparam.current = 0;
15093 return inner;
15094#else
15095 return 0;
15096#endif
15097}
15098
15099static void
15100numparam_pop(struct parser_params *p, NODE *prev_inner)
15101{
15102#ifndef RIPPER
15103 struct local_vars *local = p->lvtbl;
15104 if (prev_inner) {
15105 /* prefer first one */
15106 local->numparam.inner = prev_inner;
15107 }
15108 else if (local->numparam.current) {
15109 /* current and inner are exclusive */
15110 local->numparam.inner = local->numparam.current;
15111 }
15112 if (p->max_numparam > NO_PARAM) {
15113 /* current and outer are exclusive */
15114 local->numparam.current = local->numparam.outer;
15115 local->numparam.outer = 0;
15116 }
15117 else {
15118 /* no numbered parameter */
15119 local->numparam.current = 0;
15120 }
15121#endif
15122}
15123
15124static const struct vtable *
15125dyna_push(struct parser_params *p)
15126{
15127 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15128 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15129 if (p->lvtbl->used) {
15130 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15131 }
15132 return p->lvtbl->args;
15133}
15134
15135static void
15136dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15137{
15138 struct vtable *tmp = *vtblp;
15139 *vtblp = tmp->prev;
15140# if WARN_PAST_SCOPE
15141 if (p->past_scope_enabled) {
15142 tmp->prev = p->lvtbl->past;
15143 p->lvtbl->past = tmp;
15144 return;
15145 }
15146# endif
15147 vtable_free(tmp);
15148}
15149
15150static void
15151dyna_pop_1(struct parser_params *p)
15152{
15153 struct vtable *tmp;
15154
15155 if ((tmp = p->lvtbl->used) != 0) {
15156 warn_unused_var(p, p->lvtbl);
15157 p->lvtbl->used = p->lvtbl->used->prev;
15158 vtable_free(tmp);
15159 }
15160 dyna_pop_vtable(p, &p->lvtbl->args);
15161 dyna_pop_vtable(p, &p->lvtbl->vars);
15162}
15163
15164static void
15165dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15166{
15167 while (p->lvtbl->args != lvargs) {
15168 dyna_pop_1(p);
15169 if (!p->lvtbl->args) {
15170 struct local_vars *local = p->lvtbl->prev;
15171 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15172 p->lvtbl = local;
15173 }
15174 }
15175 dyna_pop_1(p);
15176}
15177
15178static int
15179dyna_in_block(struct parser_params *p)
15180{
15181 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15182}
15183
15184static int
15185dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15186{
15187 struct vtable *vars, *args, *used;
15188 int i;
15189
15190 args = p->lvtbl->args;
15191 vars = p->lvtbl->vars;
15192 used = p->lvtbl->used;
15193
15194 while (!DVARS_TERMINAL_P(vars)) {
15195 if (vtable_included(args, id)) {
15196 return 1;
15197 }
15198 if ((i = vtable_included(vars, id)) != 0) {
15199 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15200 return 1;
15201 }
15202 args = args->prev;
15203 vars = vars->prev;
15204 if (!vidrefp) used = 0;
15205 if (used) used = used->prev;
15206 }
15207
15208 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15209 return rb_dvar_defined(id, p->parent_iseq);
15210 }
15211
15212 return 0;
15213}
15214
15215static int
15216dvar_defined(struct parser_params *p, ID id)
15217{
15218 return dvar_defined_ref(p, id, NULL);
15219}
15220
15221static int
15222dvar_curr(struct parser_params *p, ID id)
15223{
15224 return (vtable_included(p->lvtbl->args, id) ||
15225 vtable_included(p->lvtbl->vars, id));
15226}
15227
15228static void
15229reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
15230{
15231 compile_error(p,
15232 "regexp encoding option '%c' differs from source encoding '%s'",
15233 c, rb_enc_name(rb_enc_get(str)));
15234}
15235
15236#ifndef RIPPER
15237int
15238rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
15239{
15240 int c = RE_OPTION_ENCODING_IDX(options);
15241
15242 if (c) {
15243 int opt, idx;
15244 rb_char_to_option_kcode(c, &opt, &idx);
15245 if (idx != ENCODING_GET(str) &&
15246 !is_ascii_string(str)) {
15247 goto error;
15248 }
15249 ENCODING_SET(str, idx);
15250 }
15251 else if (RE_OPTION_ENCODING_NONE(options)) {
15252 if (!ENCODING_IS_ASCII8BIT(str) &&
15253 !is_ascii_string(str)) {
15254 c = 'n';
15255 goto error;
15256 }
15257 rb_enc_associate(str, rb_ascii8bit_encoding());
15258 }
15259 else if (rb_is_usascii_enc(p->enc)) {
15260 if (!is_ascii_string(str)) {
15261 /* raise in re.c */
15262 rb_enc_associate(str, rb_usascii_encoding());
15263 }
15264 else {
15265 rb_enc_associate(str, rb_ascii8bit_encoding());
15266 }
15267 }
15268 return 0;
15269
15270 error:
15271 return c;
15272}
15273
15274static void
15275reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
15276{
15277 int c = rb_reg_fragment_setenc(p, str, options);
15278 if (c) reg_fragment_enc_error(p, str, c);
15279}
15280
15281static int
15282reg_fragment_check(struct parser_params* p, VALUE str, int options)
15283{
15284 VALUE err;
15285 reg_fragment_setenc(p, str, options);
15286 err = rb_reg_check_preprocess(str);
15287 if (err != Qnil) {
15288 err = rb_obj_as_string(err);
15289 compile_error(p, "%"PRIsVALUE, err);
15290 return 0;
15291 }
15292 return 1;
15293}
15294
15295#ifndef UNIVERSAL_PARSER
15296typedef struct {
15297 struct parser_params* parser;
15298 rb_encoding *enc;
15299 NODE *succ_block;
15300 const YYLTYPE *loc;
15301} reg_named_capture_assign_t;
15302
15303static int
15304reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15305 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15306{
15307 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15308 struct parser_params* p = arg->parser;
15309 rb_encoding *enc = arg->enc;
15310 long len = name_end - name;
15311 const char *s = (const char *)name;
15312
15313 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc);
15314}
15315
15316static NODE *
15317reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
15318{
15319 reg_named_capture_assign_t arg;
15320
15321 arg.parser = p;
15322 arg.enc = rb_enc_get(regexp);
15323 arg.succ_block = 0;
15324 arg.loc = loc;
15325 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15326
15327 if (!arg.succ_block) return 0;
15328 return RNODE_BLOCK(arg.succ_block)->nd_next;
15329}
15330#endif
15331
15332int
15333rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15334 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc)
15335{
15336 ID var;
15337 NODE *node, *succ;
15338
15339 if (!len) return ST_CONTINUE;
15340 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15341 return ST_CONTINUE;
15342
15343 var = intern_cstr(s, len, enc);
15344 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15345 if (!lvar_defined(p, var)) return ST_CONTINUE;
15346 }
15347 node = node_assign(p, assignable(p, var, 0, loc), NEW_LIT(ID2SYM(var), loc), NO_LEX_CTXT, loc);
15348 succ = *succ_block;
15349 if (!succ) succ = NEW_ERROR(loc);
15350 succ = block_append(p, succ, node);
15351 *succ_block = succ;
15352 return ST_CONTINUE;
15353}
15354
15355static VALUE
15356parser_reg_compile(struct parser_params* p, VALUE str, int options)
15357{
15358 reg_fragment_setenc(p, str, options);
15359 return rb_parser_reg_compile(p, str, options);
15360}
15361
15362VALUE
15363rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15364{
15365 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15366}
15367
15368static VALUE
15369reg_compile(struct parser_params* p, VALUE str, int options)
15370{
15371 VALUE re;
15372 VALUE err;
15373
15374 err = rb_errinfo();
15375 re = parser_reg_compile(p, str, options);
15376 if (NIL_P(re)) {
15377 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15378 rb_set_errinfo(err);
15379 compile_error(p, "%"PRIsVALUE, m);
15380 return Qnil;
15381 }
15382 return re;
15383}
15384#else
15385static VALUE
15386parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
15387{
15388 VALUE err = rb_errinfo();
15389 VALUE re;
15390 str = ripper_is_node_yylval(p, str) ? RNODE_RIPPER(str)->nd_cval : str;
15391 int c = rb_reg_fragment_setenc(p, str, options);
15392 if (c) reg_fragment_enc_error(p, str, c);
15393 re = rb_parser_reg_compile(p, str, options);
15394 if (NIL_P(re)) {
15395 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
15396 rb_set_errinfo(err);
15397 }
15398 return re;
15399}
15400#endif
15401
15402#ifndef RIPPER
15403void
15404rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15405{
15406 p->do_print = print;
15407 p->do_loop = loop;
15408 p->do_chomp = chomp;
15409 p->do_split = split;
15410}
15411
15412static NODE *
15413parser_append_options(struct parser_params *p, NODE *node)
15414{
15415 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15416 const YYLTYPE *const LOC = &default_location;
15417
15418 if (p->do_print) {
15419 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15420 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15421 LOC);
15422 node = block_append(p, node, print);
15423 }
15424
15425 if (p->do_loop) {
15426 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15427
15428 if (p->do_split) {
15429 ID ifs = rb_intern("$;");
15430 ID fields = rb_intern("$F");
15431 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15432 NODE *split = NEW_GASGN(fields,
15433 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15434 rb_intern("split"), args, LOC),
15435 LOC);
15436 node = block_append(p, split, node);
15437 }
15438 if (p->do_chomp) {
15439 NODE *chomp = NEW_LIT(ID2SYM(rb_intern("chomp")), LOC);
15440 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15441 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15442 }
15443
15444 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC);
15445 }
15446
15447 return node;
15448}
15449
15450void
15451rb_init_parse(void)
15452{
15453 /* just to suppress unused-function warnings */
15454 (void)nodetype;
15455 (void)nodeline;
15456}
15457
15458static ID
15459internal_id(struct parser_params *p)
15460{
15461 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15462}
15463#endif /* !RIPPER */
15464
15465static void
15466parser_initialize(struct parser_params *p)
15467{
15468 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15469 p->command_start = TRUE;
15470 p->ruby_sourcefile_string = Qnil;
15471 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15472 p->node_id = 0;
15473 p->delayed.token = Qnil;
15474 p->frozen_string_literal = -1; /* not specified */
15475#ifdef RIPPER
15476 p->result = Qnil;
15477 p->parsing_thread = Qnil;
15478#else
15479 p->error_buffer = Qfalse;
15480 p->end_expect_token_locations = Qnil;
15481 p->token_id = 0;
15482 p->tokens = Qnil;
15483#endif
15484 p->debug_buffer = Qnil;
15485 p->debug_output = rb_ractor_stdout();
15486 p->enc = rb_utf8_encoding();
15487 p->exits = 0;
15488}
15489
15490#ifdef RIPPER
15491#define rb_ruby_parser_mark ripper_parser_mark
15492#define rb_ruby_parser_free ripper_parser_free
15493#define rb_ruby_parser_memsize ripper_parser_memsize
15494#endif
15495
15496void
15497rb_ruby_parser_mark(void *ptr)
15498{
15499 struct parser_params *p = (struct parser_params*)ptr;
15500
15501 rb_gc_mark(p->lex.input);
15502 rb_gc_mark(p->lex.lastline);
15503 rb_gc_mark(p->lex.nextline);
15504 rb_gc_mark(p->ruby_sourcefile_string);
15505 rb_gc_mark((VALUE)p->ast);
15506 rb_gc_mark(p->case_labels);
15507 rb_gc_mark(p->delayed.token);
15508#ifndef RIPPER
15509 rb_gc_mark(p->debug_lines);
15510 rb_gc_mark(p->error_buffer);
15511 rb_gc_mark(p->end_expect_token_locations);
15512 rb_gc_mark(p->tokens);
15513#else
15514 rb_gc_mark(p->value);
15515 rb_gc_mark(p->result);
15516 rb_gc_mark(p->parsing_thread);
15517#endif
15518 rb_gc_mark(p->debug_buffer);
15519 rb_gc_mark(p->debug_output);
15520#ifdef YYMALLOC
15521 rb_gc_mark((VALUE)p->heap);
15522#endif
15523}
15524
15525void
15526rb_ruby_parser_free(void *ptr)
15527{
15528 struct parser_params *p = (struct parser_params*)ptr;
15529 struct local_vars *local, *prev;
15530#ifdef UNIVERSAL_PARSER
15531 rb_parser_config_t *config = p->config;
15532#endif
15533
15534 if (p->tokenbuf) {
15535 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15536 }
15537
15538 for (local = p->lvtbl; local; local = prev) {
15539 prev = local->prev;
15540 local_free(p, local);
15541 }
15542
15543 {
15544 token_info *ptinfo;
15545 while ((ptinfo = p->token_info) != 0) {
15546 p->token_info = ptinfo->next;
15547 xfree(ptinfo);
15548 }
15549 }
15550
15551 xfree(p->lex.strterm);
15552 p->lex.strterm = 0;
15553
15554 xfree(ptr);
15555
15556#ifdef UNIVERSAL_PARSER
15557 config->counter--;
15558 if (config->counter <= 0) {
15559 rb_ruby_parser_config_free(config);
15560 }
15561#endif
15562}
15563
15564size_t
15565rb_ruby_parser_memsize(const void *ptr)
15566{
15567 struct parser_params *p = (struct parser_params*)ptr;
15568 struct local_vars *local;
15569 size_t size = sizeof(*p);
15570
15571 size += p->toksiz;
15572 for (local = p->lvtbl; local; local = local->prev) {
15573 size += sizeof(*local);
15574 if (local->vars) size += local->vars->capa * sizeof(ID);
15575 }
15576 return size;
15577}
15578
15579#ifdef UNIVERSAL_PARSER
15580rb_parser_config_t *
15581rb_ruby_parser_config_new(void *(*malloc)(size_t size))
15582{
15583 return (rb_parser_config_t *)malloc(sizeof(rb_parser_config_t));
15584}
15585
15586void
15587rb_ruby_parser_config_free(rb_parser_config_t *config)
15588{
15589 config->free(config);
15590}
15591#endif
15592
15593#ifndef UNIVERSAL_PARSER
15594#ifndef RIPPER
15595static const rb_data_type_t parser_data_type = {
15596 "parser",
15597 {
15598 rb_ruby_parser_mark,
15599 rb_ruby_parser_free,
15600 rb_ruby_parser_memsize,
15601 },
15602 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
15603};
15604#endif
15605#endif
15606
15607#ifndef RIPPER
15608#undef rb_reserved_word
15609
15610const struct kwtable *
15611rb_reserved_word(const char *str, unsigned int len)
15612{
15613 return reserved_word(str, len);
15614}
15615
15616#ifdef UNIVERSAL_PARSER
15617rb_parser_t *
15618rb_ruby_parser_allocate(rb_parser_config_t *config)
15619{
15620 /* parser_initialize expects fields to be set to 0 */
15621 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15622 p->config = config;
15623 p->config->counter++;
15624 return p;
15625}
15626
15627rb_parser_t *
15628rb_ruby_parser_new(rb_parser_config_t *config)
15629{
15630 /* parser_initialize expects fields to be set to 0 */
15631 rb_parser_t *p = rb_ruby_parser_allocate(config);
15632 parser_initialize(p);
15633 return p;
15634}
15635#endif
15636
15637rb_parser_t *
15638rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15639{
15640 p->error_buffer = main ? Qfalse : Qnil;
15641 p->parent_iseq = base;
15642 return p;
15643}
15644
15645void
15646rb_ruby_parser_set_script_lines(rb_parser_t *p, VALUE lines)
15647{
15648 if (!RTEST(lines)) {
15649 lines = Qfalse;
15650 }
15651 else if (lines == Qtrue) {
15652 lines = rb_ary_new();
15653 }
15654 else {
15655 Check_Type(lines, T_ARRAY);
15656 rb_ary_modify(lines);
15657 }
15658 p->debug_lines = lines;
15659}
15660
15661void
15662rb_ruby_parser_error_tolerant(rb_parser_t *p)
15663{
15664 p->error_tolerant = 1;
15665 // TODO
15666 p->end_expect_token_locations = rb_ary_new();
15667}
15668
15669void
15670rb_ruby_parser_keep_tokens(rb_parser_t *p)
15671{
15672 p->keep_tokens = 1;
15673 // TODO
15674 p->tokens = rb_ary_new();
15675}
15676
15677#ifndef UNIVERSAL_PARSER
15678rb_ast_t*
15679rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
15680{
15681 struct parser_params *p;
15682
15683 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15684 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15685 return rb_ruby_parser_compile_file_path(p, fname, file, start);
15686}
15687
15688rb_ast_t*
15689rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
15690{
15691 struct parser_params *p;
15692
15693 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15694 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15695 return rb_ruby_parser_compile_generic(p, lex_gets, fname, input, start);
15696}
15697
15698rb_ast_t*
15699rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
15700{
15701 struct parser_params *p;
15702
15703 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15704 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15705 return rb_ruby_parser_compile_string(p, f, s, line);
15706}
15707
15708rb_ast_t*
15709rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
15710{
15711 struct parser_params *p;
15712
15713 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15714 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15715 return rb_ruby_parser_compile_string_path(p, f, s, line);
15716}
15717
15718VALUE
15719rb_parser_encoding(VALUE vparser)
15720{
15721 struct parser_params *p;
15722
15723 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15724 return rb_ruby_parser_encoding(p);
15725}
15726
15727VALUE
15728rb_parser_end_seen_p(VALUE vparser)
15729{
15730 struct parser_params *p;
15731
15732 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15733 return RBOOL(rb_ruby_parser_end_seen_p(p));
15734}
15735
15736void
15737rb_parser_error_tolerant(VALUE vparser)
15738{
15739 struct parser_params *p;
15740
15741 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15742 rb_ruby_parser_error_tolerant(p);
15743}
15744
15745void
15746rb_parser_set_script_lines(VALUE vparser, VALUE lines)
15747{
15748 struct parser_params *p;
15749
15750 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15751 rb_ruby_parser_set_script_lines(p, lines);
15752}
15753
15754void
15755rb_parser_keep_tokens(VALUE vparser)
15756{
15757 struct parser_params *p;
15758
15759 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15760 rb_ruby_parser_keep_tokens(p);
15761}
15762
15763VALUE
15764rb_parser_new(void)
15765{
15766 struct parser_params *p;
15767 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
15768 &parser_data_type, p);
15769 parser_initialize(p);
15770 return parser;
15771}
15772
15773VALUE
15774rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
15775{
15776 struct parser_params *p;
15777
15778 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15779 rb_ruby_parser_set_context(p, base, main);
15780 return vparser;
15781}
15782
15783void
15784rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
15785{
15786 struct parser_params *p;
15787
15788 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15789 rb_ruby_parser_set_options(p, print, loop, chomp, split);
15790}
15791
15792VALUE
15793rb_parser_set_yydebug(VALUE self, VALUE flag)
15794{
15795 struct parser_params *p;
15796
15797 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
15798 rb_ruby_parser_set_yydebug(p, RTEST(flag));
15799 return flag;
15800}
15801#endif /* !UNIVERSAL_PARSER */
15802
15803VALUE
15804rb_ruby_parser_encoding(rb_parser_t *p)
15805{
15806 return rb_enc_from_encoding(p->enc);
15807}
15808
15809int
15810rb_ruby_parser_end_seen_p(rb_parser_t *p)
15811{
15812 return p->ruby__end__seen;
15813}
15814
15815int
15816rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15817{
15818 p->debug = flag;
15819 return flag;
15820}
15821#endif /* !RIPPER */
15822
15823#ifdef RIPPER
15824int
15825rb_ruby_parser_get_yydebug(rb_parser_t *p)
15826{
15827 return p->debug;
15828}
15829
15830void
15831rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15832{
15833 p->value = value;
15834}
15835
15836int
15837rb_ruby_parser_error_p(rb_parser_t *p)
15838{
15839 return p->error_p;
15840}
15841
15842VALUE
15843rb_ruby_parser_debug_output(rb_parser_t *p)
15844{
15845 return p->debug_output;
15846}
15847
15848void
15849rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15850{
15851 p->debug_output = output;
15852}
15853
15854VALUE
15855rb_ruby_parser_parsing_thread(rb_parser_t *p)
15856{
15857 return p->parsing_thread;
15858}
15859
15860void
15861rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15862{
15863 p->parsing_thread = parsing_thread;
15864}
15865
15866void
15867rb_ruby_parser_ripper_initialize(rb_parser_t *p, VALUE (*gets)(struct parser_params*,VALUE), VALUE input, VALUE sourcefile_string, const char *sourcefile, int sourceline)
15868{
15869 p->lex.gets = gets;
15870 p->lex.input = input;
15871 p->eofp = 0;
15872 p->ruby_sourcefile_string = sourcefile_string;
15873 p->ruby_sourcefile = sourcefile;
15874 p->ruby_sourceline = sourceline;
15875}
15876
15877VALUE
15878rb_ruby_parser_result(rb_parser_t *p)
15879{
15880 return p->result;
15881}
15882
15883rb_encoding *
15884rb_ruby_parser_enc(rb_parser_t *p)
15885{
15886 return p->enc;
15887}
15888
15889VALUE
15890rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15891{
15892 return p->ruby_sourcefile_string;
15893}
15894
15895int
15896rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15897{
15898 return p->ruby_sourceline;
15899}
15900
15901int
15902rb_ruby_parser_lex_state(rb_parser_t *p)
15903{
15904 return p->lex.state;
15905}
15906
15907void
15908rb_ruby_ripper_parse0(rb_parser_t *p)
15909{
15910 parser_prepare(p);
15911 p->ast = rb_ast_new();
15912 ripper_yyparse((void*)p);
15913 rb_ast_dispose(p->ast);
15914 p->ast = 0;
15915}
15916
15917int
15918rb_ruby_ripper_dedent_string(rb_parser_t *p, VALUE string, int width)
15919{
15920 return dedent_string(p, string, width);
15921}
15922
15923VALUE
15924rb_ruby_ripper_lex_get_str(rb_parser_t *p, VALUE s)
15925{
15926 return lex_get_str(p, s);
15927}
15928
15929int
15930rb_ruby_ripper_initialized_p(rb_parser_t *p)
15931{
15932 return p->lex.input != 0;
15933}
15934
15935void
15936rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15937{
15938 parser_initialize(p);
15939}
15940
15941long
15942rb_ruby_ripper_column(rb_parser_t *p)
15943{
15944 return p->lex.ptok - p->lex.pbeg;
15945}
15946
15947long
15948rb_ruby_ripper_token_len(rb_parser_t *p)
15949{
15950 return p->lex.pcur - p->lex.ptok;
15951}
15952
15953VALUE
15954rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15955{
15956 return p->lex.lastline;
15957}
15958
15959VALUE
15960rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15961{
15962 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15963}
15964
15965struct parser_params*
15966rb_ruby_ripper_parser_allocate(void)
15967{
15968 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15969}
15970#endif /* RIPPER */
15971
15972#ifndef RIPPER
15973#ifdef YYMALLOC
15974#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
15975/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
15976 * potential memory leak */
15977#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
15978#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
15979 (new)->cnt = (cnt), (ptr))
15980
15981void *
15982rb_parser_malloc(struct parser_params *p, size_t size)
15983{
15984 size_t cnt = HEAPCNT(1, size);
15985 rb_imemo_tmpbuf_t *n = NEWHEAP();
15986 void *ptr = xmalloc(size);
15987
15988 return ADD2HEAP(n, cnt, ptr);
15989}
15990
15991void *
15992rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
15993{
15994 size_t cnt = HEAPCNT(nelem, size);
15995 rb_imemo_tmpbuf_t *n = NEWHEAP();
15996 void *ptr = xcalloc(nelem, size);
15997
15998 return ADD2HEAP(n, cnt, ptr);
15999}
16000
16001void *
16002rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
16003{
16004 rb_imemo_tmpbuf_t *n;
16005 size_t cnt = HEAPCNT(1, size);
16006
16007 if (ptr && (n = p->heap) != NULL) {
16008 do {
16009 if (n->ptr == ptr) {
16010 n->ptr = ptr = xrealloc(ptr, size);
16011 if (n->cnt) n->cnt = cnt;
16012 return ptr;
16013 }
16014 } while ((n = n->next) != NULL);
16015 }
16016 n = NEWHEAP();
16017 ptr = xrealloc(ptr, size);
16018 return ADD2HEAP(n, cnt, ptr);
16019}
16020
16021void
16022rb_parser_free(struct parser_params *p, void *ptr)
16023{
16024 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
16025
16026 while ((n = *prev) != NULL) {
16027 if (n->ptr == ptr) {
16028 *prev = n->next;
16029 break;
16030 }
16031 prev = &n->next;
16032 }
16033}
16034#endif
16035
16036void
16037rb_parser_printf(struct parser_params *p, const char *fmt, ...)
16038{
16039 va_list ap;
16040 VALUE mesg = p->debug_buffer;
16041
16042 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
16043 va_start(ap, fmt);
16044 rb_str_vcatf(mesg, fmt, ap);
16045 va_end(ap);
16046 if (end_with_newline_p(p, mesg)) {
16047 rb_io_write(p->debug_output, mesg);
16048 p->debug_buffer = Qnil;
16049 }
16050}
16051
16052static void
16053parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
16054{
16055 va_list ap;
16056 int lineno, column;
16057
16058 if (loc) {
16059 lineno = loc->end_pos.lineno;
16060 column = loc->end_pos.column;
16061 }
16062 else {
16063 lineno = p->ruby_sourceline;
16064 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
16065 }
16066
16067 rb_io_flush(p->debug_output);
16068 p->error_p = 1;
16069 va_start(ap, fmt);
16070 p->error_buffer =
16071 rb_syntax_error_append(p->error_buffer,
16072 p->ruby_sourcefile_string,
16073 lineno, column,
16074 p->enc, fmt, ap);
16075 va_end(ap);
16076}
16077
16078static size_t
16079count_char(const char *str, int c)
16080{
16081 int n = 0;
16082 while (str[n] == c) ++n;
16083 return n;
16084}
16085
16086/*
16087 * strip enclosing double-quotes, same as the default yytnamerr except
16088 * for that single-quotes matching back-quotes do not stop stripping.
16089 *
16090 * "\"`class' keyword\"" => "`class' keyword"
16091 */
16092RUBY_FUNC_EXPORTED size_t
16093rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16094{
16095 if (*yystr == '"') {
16096 size_t yyn = 0, bquote = 0;
16097 const char *yyp = yystr;
16098
16099 while (*++yyp) {
16100 switch (*yyp) {
16101 case '`':
16102 if (!bquote) {
16103 bquote = count_char(yyp+1, '`') + 1;
16104 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16105 yyn += bquote;
16106 yyp += bquote - 1;
16107 break;
16108 }
16109 goto default_char;
16110
16111 case '\'':
16112 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16113 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16114 yyn += bquote;
16115 yyp += bquote - 1;
16116 bquote = 0;
16117 break;
16118 }
16119 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16120 if (yyres) memcpy(yyres + yyn, yyp, 3);
16121 yyn += 3;
16122 yyp += 2;
16123 break;
16124 }
16125 goto do_not_strip_quotes;
16126
16127 case ',':
16128 goto do_not_strip_quotes;
16129
16130 case '\\':
16131 if (*++yyp != '\\')
16132 goto do_not_strip_quotes;
16133 /* Fall through. */
16134 default_char:
16135 default:
16136 if (yyres)
16137 yyres[yyn] = *yyp;
16138 yyn++;
16139 break;
16140
16141 case '"':
16142 case '\0':
16143 if (yyres)
16144 yyres[yyn] = '\0';
16145 return yyn;
16146 }
16147 }
16148 do_not_strip_quotes: ;
16149 }
16150
16151 if (!yyres) return strlen(yystr);
16152
16153 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16154}
16155#endif
16156
16157#ifdef RIPPER
16158#ifdef RIPPER_DEBUG
16159/* :nodoc: */
16160static VALUE
16161ripper_validate_object(VALUE self, VALUE x)
16162{
16163 if (x == Qfalse) return x;
16164 if (x == Qtrue) return x;
16165 if (NIL_P(x)) return x;
16166 if (UNDEF_P(x))
16167 rb_raise(rb_eArgError, "Qundef given");
16168 if (FIXNUM_P(x)) return x;
16169 if (SYMBOL_P(x)) return x;
16170 switch (BUILTIN_TYPE(x)) {
16171 case T_STRING:
16172 case T_OBJECT:
16173 case T_ARRAY:
16174 case T_BIGNUM:
16175 case T_FLOAT:
16176 case T_COMPLEX:
16177 case T_RATIONAL:
16178 break;
16179 case T_NODE:
16180 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
16181 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
16182 }
16183 x = ((NODE *)x)->nd_rval;
16184 break;
16185 default:
16186 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
16187 (void *)x, rb_obj_classname(x));
16188 }
16189 if (!RBASIC_CLASS(x)) {
16190 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
16191 (void *)x, rb_builtin_type_name(TYPE(x)));
16192 }
16193 return x;
16194}
16195#endif
16196
16197#define validate(x) ((x) = get_value(x))
16198
16199static VALUE
16200ripper_dispatch0(struct parser_params *p, ID mid)
16201{
16202 return rb_funcall(p->value, mid, 0);
16203}
16204
16205static VALUE
16206ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16207{
16208 validate(a);
16209 return rb_funcall(p->value, mid, 1, a);
16210}
16211
16212static VALUE
16213ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16214{
16215 validate(a);
16216 validate(b);
16217 return rb_funcall(p->value, mid, 2, a, b);
16218}
16219
16220static VALUE
16221ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16222{
16223 validate(a);
16224 validate(b);
16225 validate(c);
16226 return rb_funcall(p->value, mid, 3, a, b, c);
16227}
16228
16229static VALUE
16230ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16231{
16232 validate(a);
16233 validate(b);
16234 validate(c);
16235 validate(d);
16236 return rb_funcall(p->value, mid, 4, a, b, c, d);
16237}
16238
16239static VALUE
16240ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16241{
16242 validate(a);
16243 validate(b);
16244 validate(c);
16245 validate(d);
16246 validate(e);
16247 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16248}
16249
16250static VALUE
16251ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16252{
16253 validate(a);
16254 validate(b);
16255 validate(c);
16256 validate(d);
16257 validate(e);
16258 validate(f);
16259 validate(g);
16260 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16261}
16262
16263void
16264ripper_error(struct parser_params *p)
16265{
16266 p->error_p = TRUE;
16267}
16268
16269VALUE
16270ripper_value(struct parser_params *p)
16271{
16272 (void)yystpcpy; /* may not used in newer bison */
16273
16274 return p->value;
16275}
16276
16277#endif /* RIPPER */
16278/*
16279 * Local variables:
16280 * mode: c
16281 * c-file-style: "ruby"
16282 * End:
16283 */