Ruby 3.3.0p0 (2023-12-25 revision 5124f9ac7513eb590c37717337c430cb93caa151)
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*);
1189static NODE *remove_begin_all(NODE*);
1190#define value_expr(node) value_expr_gen(p, (node))
1191static NODE *void_stmts(struct parser_params*,NODE*);
1192static void reduce_nodes(struct parser_params*,NODE**);
1193static void block_dup_check(struct parser_params*,NODE*,NODE*);
1194
1195static NODE *block_append(struct parser_params*,NODE*,NODE*);
1196static NODE *list_append(struct parser_params*,NODE*,NODE*);
1197static NODE *list_concat(NODE*,NODE*);
1198static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1199static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1200static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1201static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1202static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
1203static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1204static NODE *str2dstr(struct parser_params*,NODE*);
1205static NODE *evstr2dstr(struct parser_params*,NODE*);
1206static NODE *splat_array(NODE*);
1207static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1208
1209static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1210static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1211static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1212static 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);
1213static 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;}
1214
1215static bool args_info_empty_p(struct rb_args_info *args);
1216static 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*);
1217static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1218static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1219static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1220static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1221static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1222static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1223static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1224
1225static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1226static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int);
1227
1228static VALUE negate_lit(struct parser_params*, VALUE);
1229static NODE *ret_args(struct parser_params*,NODE*);
1230static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1231static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
1232static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1233
1234static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1235static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1236
1237static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1238static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1239
1240static void rb_backref_error(struct parser_params*,NODE*);
1241static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1242
1243static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1244static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
1245static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
1246static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1247static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1248
1249static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1250
1251static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1252static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1253
1254static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1255static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1256
1257static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
1258
1259static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
1260
1261#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1262
1263static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1264
1265static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1266
1267static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1268
1269static rb_ast_id_table_t *local_tbl(struct parser_params*);
1270
1271static VALUE reg_compile(struct parser_params*, VALUE, int);
1272static void reg_fragment_setenc(struct parser_params*, VALUE, int);
1273static int reg_fragment_check(struct parser_params*, VALUE, int);
1274
1275static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
1276static NODE *heredoc_dedent(struct parser_params*,NODE*);
1277
1278static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1279
1280#define get_id(id) (id)
1281#define get_value(val) (val)
1282#define get_num(num) (num)
1283#else /* RIPPER */
1284
1285static inline int ripper_is_node_yylval(struct parser_params *p, VALUE n);
1286
1287static inline VALUE
1288ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
1289{
1290 if (ripper_is_node_yylval(p, c)) c = RNODE_RIPPER(c)->nd_cval;
1291 add_mark_object(p, b);
1292 add_mark_object(p, c);
1293 return NEW_RIPPER(a, b, c, &NULL_LOC);
1294}
1295
1296static inline VALUE
1297ripper_new_yylval2(struct parser_params *p, VALUE a, VALUE b, VALUE c)
1298{
1299 add_mark_object(p, a);
1300 add_mark_object(p, b);
1301 add_mark_object(p, c);
1302 return NEW_RIPPER_VALUES(a, b, c, &NULL_LOC);
1303}
1304
1305static inline int
1306ripper_is_node_yylval(struct parser_params *p, VALUE n)
1307{
1308 return RB_TYPE_P(n, T_NODE) && nd_type_p(RNODE(n), NODE_RIPPER);
1309}
1310
1311#define value_expr(node) ((void)(node))
1312#define remove_begin(node) (node)
1313#define void_stmts(p,x) (x)
1314#undef rb_dvar_defined
1315#define rb_dvar_defined(id, base) 0
1316#undef rb_local_defined
1317#define rb_local_defined(id, base) 0
1318#define get_id(id) ripper_get_id(id)
1319#define get_value(val) ripper_get_value(val)
1320#define get_num(num) (int)get_id(num)
1321static VALUE assignable(struct parser_params*,VALUE);
1322static int id_is_var(struct parser_params *p, ID id);
1323
1324#define method_cond(p,node,loc) (node)
1325#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
1326#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
1327#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
1328#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
1329
1330#define new_nil(loc) Qnil
1331
1332static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
1333
1334static VALUE const_decl(struct parser_params *p, VALUE path);
1335
1336static VALUE var_field(struct parser_params *p, VALUE a);
1337static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1338
1339static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
1340
1341static VALUE backref_error(struct parser_params*, NODE *, VALUE);
1342#endif /* !RIPPER */
1343
1344RUBY_SYMBOL_EXPORT_BEGIN
1345VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1346int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
1347enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1348VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1349void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1350PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1351YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1352YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1353YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1354YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1355YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1356YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1357RUBY_SYMBOL_EXPORT_END
1358
1359static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1360static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1361#ifndef RIPPER
1362static ID formal_argument(struct parser_params*, ID);
1363#else
1364static ID formal_argument(struct parser_params*, VALUE);
1365#endif
1366static ID shadowing_lvar(struct parser_params*,ID);
1367static void new_bv(struct parser_params*,ID);
1368
1369static void local_push(struct parser_params*,int);
1370static void local_pop(struct parser_params*);
1371static void local_var(struct parser_params*, ID);
1372static void arg_var(struct parser_params*, ID);
1373static int local_id(struct parser_params *p, ID id);
1374static int local_id_ref(struct parser_params*, ID, ID **);
1375#ifndef RIPPER
1376static ID internal_id(struct parser_params*);
1377static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1378#endif
1379static int check_forwarding_args(struct parser_params*);
1380static void add_forwarding_args(struct parser_params *p);
1381static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1382
1383static const struct vtable *dyna_push(struct parser_params *);
1384static void dyna_pop(struct parser_params*, const struct vtable *);
1385static int dyna_in_block(struct parser_params*);
1386#define dyna_var(p, id) local_var(p, id)
1387static int dvar_defined(struct parser_params*, ID);
1388static int dvar_defined_ref(struct parser_params*, ID, ID**);
1389static int dvar_curr(struct parser_params*,ID);
1390
1391static int lvar_defined(struct parser_params*, ID);
1392
1393static NODE *numparam_push(struct parser_params *p);
1394static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1395
1396#ifdef RIPPER
1397# define METHOD_NOT idNOT
1398#else
1399# define METHOD_NOT '!'
1400#endif
1401
1402#define idFWD_REST '*'
1403#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1404#define idFWD_BLOCK '&'
1405#define idFWD_ALL idDot3
1406#ifdef RIPPER
1407#define arg_FWD_BLOCK Qnone
1408#else
1409#define arg_FWD_BLOCK idFWD_BLOCK
1410#endif
1411#define FORWARD_ARGS_WITH_RUBY2_KEYWORDS
1412
1413#define RE_OPTION_ONCE (1<<16)
1414#define RE_OPTION_ENCODING_SHIFT 8
1415#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1416#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1417#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1418#define RE_OPTION_MASK 0xff
1419#define RE_OPTION_ARG_ENCODING_NONE 32
1420
1421#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1422size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1423
1424#define TOKEN2ID(tok) ( \
1425 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1426 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1427 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1428 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1429 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1430 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1431 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1432
1433/****** Ripper *******/
1434
1435#ifdef RIPPER
1436
1437#include "eventids1.h"
1438#include "eventids2.h"
1439
1440extern const struct ripper_parser_ids ripper_parser_ids;
1441
1442static VALUE ripper_dispatch0(struct parser_params*,ID);
1443static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1444static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1445static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1446static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1447static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1448static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1449void ripper_error(struct parser_params *p);
1450
1451#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
1452#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
1453#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
1454#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
1455#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
1456#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
1457#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))
1458
1459#define yyparse ripper_yyparse
1460
1461#define ID2VAL(id) STATIC_ID2SYM(id)
1462#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1463#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
1464
1465#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
1466 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
1467
1468static inline VALUE
1469new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
1470{
1471 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(tail);
1472 VALUE kw_args = t->nd_val1, kw_rest_arg = t->nd_val2, block = t->nd_val3;
1473 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, block);
1474}
1475
1476static inline VALUE
1477new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
1478{
1479 return ripper_new_yylval2(p, kw_args, kw_rest_arg, block);
1480}
1481
1482static inline VALUE
1483args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
1484{
1485 return args;
1486}
1487
1488static VALUE
1489new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
1490{
1491 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(aryptn);
1492 VALUE pre_args = t->nd_val1, rest_arg = t->nd_val2, post_args = t->nd_val3;
1493
1494 if (!NIL_P(pre_arg)) {
1495 if (!NIL_P(pre_args)) {
1496 rb_ary_unshift(pre_args, pre_arg);
1497 }
1498 else {
1499 pre_args = rb_ary_new_from_args(1, pre_arg);
1500 }
1501 }
1502 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
1503}
1504
1505static VALUE
1506new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
1507{
1508 return ripper_new_yylval2(p, pre_args, rest_arg, post_args);
1509}
1510
1511static VALUE
1512new_find_pattern(struct parser_params *p, VALUE constant, VALUE fndptn, const YYLTYPE *loc)
1513{
1514 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(fndptn);
1515 VALUE pre_rest_arg = t->nd_val1, args = t->nd_val2, post_rest_arg = t->nd_val3;
1516
1517 return dispatch4(fndptn, constant, pre_rest_arg, args, post_rest_arg);
1518}
1519
1520static VALUE
1521new_find_pattern_tail(struct parser_params *p, VALUE pre_rest_arg, VALUE args, VALUE post_rest_arg, const YYLTYPE *loc)
1522{
1523 return ripper_new_yylval2(p, pre_rest_arg, args, post_rest_arg);
1524}
1525
1526#define new_hash(p,h,l) rb_ary_new_from_args(0)
1527
1528static VALUE
1529new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
1530{
1531 return ary;
1532}
1533
1534static VALUE
1535new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
1536{
1537 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(hshptn);
1538 VALUE kw_args = t->nd_val1, kw_rest_arg = t->nd_val2;
1539 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
1540}
1541
1542static VALUE
1543new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
1544{
1545 if (kw_rest_arg) {
1546 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
1547 }
1548 else {
1549 kw_rest_arg = Qnil;
1550 }
1551 return ripper_new_yylval2(p, kw_args, kw_rest_arg, Qnil);
1552}
1553
1554#define new_defined(p,expr,loc) dispatch1(defined, (expr))
1555
1556static VALUE heredoc_dedent(struct parser_params*,VALUE);
1557
1558#else
1559#define ID2VAL(id) (id)
1560#define TOKEN2VAL(t) ID2VAL(t)
1561#define KWD2EID(t, v) keyword_##t
1562
1563static NODE *
1564new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, const YYLTYPE *loc)
1565{
1566 body = remove_begin(body);
1567 reduce_nodes(p, &body);
1568 NODE *n = NEW_SCOPE(args, body, loc);
1569 nd_set_line(n, loc->end_pos.lineno);
1570 set_line_body(body, loc->beg_pos.lineno);
1571 return n;
1572}
1573
1574static NODE *
1575rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1576 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1577{
1578 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1579 rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc);
1580 loc.beg_pos = arg_loc->beg_pos;
1581 return NEW_RESCUE(arg, rescue, 0, &loc);
1582}
1583
1584#endif /* RIPPER */
1585
1586static NODE *add_block_exit(struct parser_params *p, NODE *node);
1587static rb_node_exits_t *init_block_exit(struct parser_params *p);
1588static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1589static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1590static void clear_block_exit(struct parser_params *p, bool error);
1591
1592static void
1593next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1594{
1595 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1596}
1597
1598static void
1599restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1600{
1601 /* See: def_name action */
1602 struct lex_context ctxt = temp->save.ctxt;
1603 p->cur_arg = temp->save.cur_arg;
1604 p->ctxt.in_def = ctxt.in_def;
1605 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1606 p->ctxt.in_rescue = ctxt.in_rescue;
1607 p->max_numparam = temp->save.max_numparam;
1608 numparam_pop(p, temp->save.numparam_save);
1609 clear_block_exit(p, true);
1610}
1611
1612static void
1613endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1614{
1615 if (is_attrset_id(mid)) {
1616 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1617 }
1618 token_info_drop(p, "def", loc->beg_pos);
1619}
1620
1621#define debug_token_line(p, name, line) do { \
1622 if (p->debug) { \
1623 const char *const pcur = p->lex.pcur; \
1624 const char *const ptok = p->lex.ptok; \
1625 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1626 line, p->ruby_sourceline, \
1627 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1628 } \
1629 } while (0)
1630
1631#define begin_definition(k, loc_beg, loc_end) \
1632 do { \
1633 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1634 p->ctxt.in_def = 0; \
1635 } \
1636 else if (p->ctxt.in_def) { \
1637 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1638 yyerror1(&loc, k " definition in method body"); \
1639 } \
1640 local_push(p, 0); \
1641 } while (0)
1642
1643#ifndef RIPPER
1644# define Qnone 0
1645# define Qnull 0
1646# define ifndef_ripper(x) (x)
1647#else
1648# define Qnone Qnil
1649# define Qnull Qundef
1650# define ifndef_ripper(x)
1651#endif
1652
1653# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1654# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1655# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1656# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1657# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1658# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1659# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1660# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1661# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1662# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1663# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1664# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1665# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1666# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1667# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1668# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1669# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1670# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1671# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1672# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1673#ifdef RIPPER
1674extern const ID id_warn, id_warning, id_gets, id_assoc;
1675# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1676# define WARN_S_L(s,l) STR_NEW(s,l)
1677# define WARN_S(s) STR_NEW2(s)
1678# define WARN_I(i) INT2NUM(i)
1679# define WARN_ID(i) rb_id2str(i)
1680# define WARN_IVAL(i) i
1681# define PRIsWARN "s"
1682# define rb_warn0L_experimental(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1683# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1684# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1685# ifdef HAVE_VA_ARGS_MACRO
1686# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1687# else
1688# define WARN_CALL rb_funcall
1689# endif
1690# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1691# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1692# ifdef HAVE_VA_ARGS_MACRO
1693# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1694# else
1695# define WARNING_CALL rb_funcall
1696# endif
1697# define compile_error ripper_compile_error
1698#else
1699# define WARN_S_L(s,l) s
1700# define WARN_S(s) s
1701# define WARN_I(i) i
1702# define WARN_ID(i) rb_id2name(i)
1703# define WARN_IVAL(i) NUM2INT(i)
1704# define PRIsWARN PRIsVALUE
1705# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1706# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1707# define WARN_CALL rb_compile_warn
1708# define rb_warn0L_experimental(l,fmt) rb_category_compile_warn(RB_WARN_CATEGORY_EXPERIMENTAL, WARN_ARGS_L(l, fmt, 1))
1709# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1710# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1711# define WARNING_CALL rb_compile_warning
1712PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1713# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1714#endif
1715
1716struct RNode_EXITS {
1717 NODE node;
1718
1719 NODE *nd_chain; /* Assume NODE_BREAK, NODE_NEXT, NODE_REDO have nd_chain here */
1720 NODE *nd_end;
1721};
1722
1723#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1724
1725static NODE *
1726add_block_exit(struct parser_params *p, NODE *node)
1727{
1728 if (!node) {
1729 compile_error(p, "unexpected null node");
1730 return 0;
1731 }
1732 switch (nd_type(node)) {
1733 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1734 default:
1735 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1736 return node;
1737 }
1738 if (!p->ctxt.in_defined) {
1739 rb_node_exits_t *exits = p->exits;
1740 if (exits) {
1741 RNODE_EXITS(exits->nd_end)->nd_chain = node;
1742 exits->nd_end = node;
1743 }
1744 }
1745 return node;
1746}
1747
1748static rb_node_exits_t *
1749init_block_exit(struct parser_params *p)
1750{
1751 rb_node_exits_t *old = p->exits;
1752 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1753 exits->nd_chain = 0;
1754 exits->nd_end = RNODE(exits);
1755 p->exits = exits;
1756 return old;
1757}
1758
1759static rb_node_exits_t *
1760allow_block_exit(struct parser_params *p)
1761{
1762 rb_node_exits_t *exits = p->exits;
1763 p->exits = 0;
1764 return exits;
1765}
1766
1767static void
1768restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1769{
1770 p->exits = exits;
1771}
1772
1773static void
1774clear_block_exit(struct parser_params *p, bool error)
1775{
1776 rb_node_exits_t *exits = p->exits;
1777 if (!exits) return;
1778 if (error && !compile_for_eval) {
1779 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1780 switch (nd_type(e)) {
1781 case NODE_BREAK:
1782 yyerror1(&e->nd_loc, "Invalid break");
1783 break;
1784 case NODE_NEXT:
1785 yyerror1(&e->nd_loc, "Invalid next");
1786 break;
1787 case NODE_REDO:
1788 yyerror1(&e->nd_loc, "Invalid redo");
1789 break;
1790 default:
1791 yyerror1(&e->nd_loc, "unexpected node");
1792 goto end_checks; /* no nd_chain */
1793 }
1794 }
1795 end_checks:;
1796 }
1797 exits->nd_end = RNODE(exits);
1798 exits->nd_chain = 0;
1799}
1800
1801#define WARN_EOL(tok) \
1802 (looking_at_eol_p(p) ? \
1803 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
1804 (void)0)
1805static int looking_at_eol_p(struct parser_params *p);
1806
1807#ifndef RIPPER
1808static NODE *
1809get_nd_value(struct parser_params *p, NODE *node)
1810{
1811 switch (nd_type(node)) {
1812 case NODE_GASGN:
1813 return RNODE_GASGN(node)->nd_value;
1814 case NODE_IASGN:
1815 return RNODE_IASGN(node)->nd_value;
1816 case NODE_LASGN:
1817 return RNODE_LASGN(node)->nd_value;
1818 case NODE_DASGN:
1819 return RNODE_DASGN(node)->nd_value;
1820 case NODE_MASGN:
1821 return RNODE_MASGN(node)->nd_value;
1822 case NODE_CVASGN:
1823 return RNODE_CVASGN(node)->nd_value;
1824 case NODE_CDECL:
1825 return RNODE_CDECL(node)->nd_value;
1826 default:
1827 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1828 return 0;
1829 }
1830}
1831
1832static void
1833set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1834{
1835 switch (nd_type(node)) {
1836 case NODE_CDECL:
1837 RNODE_CDECL(node)->nd_value = rhs;
1838 break;
1839 case NODE_GASGN:
1840 RNODE_GASGN(node)->nd_value = rhs;
1841 break;
1842 case NODE_IASGN:
1843 RNODE_IASGN(node)->nd_value = rhs;
1844 break;
1845 case NODE_LASGN:
1846 RNODE_LASGN(node)->nd_value = rhs;
1847 break;
1848 case NODE_DASGN:
1849 RNODE_DASGN(node)->nd_value = rhs;
1850 break;
1851 case NODE_MASGN:
1852 RNODE_MASGN(node)->nd_value = rhs;
1853 break;
1854 case NODE_CVASGN:
1855 RNODE_CVASGN(node)->nd_value = rhs;
1856 break;
1857 default:
1858 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1859 break;
1860 }
1861}
1862
1863static ID
1864get_nd_vid(struct parser_params *p, NODE *node)
1865{
1866 switch (nd_type(node)) {
1867 case NODE_CDECL:
1868 return RNODE_CDECL(node)->nd_vid;
1869 case NODE_GASGN:
1870 return RNODE_GASGN(node)->nd_vid;
1871 case NODE_IASGN:
1872 return RNODE_IASGN(node)->nd_vid;
1873 case NODE_LASGN:
1874 return RNODE_LASGN(node)->nd_vid;
1875 case NODE_DASGN:
1876 return RNODE_DASGN(node)->nd_vid;
1877 case NODE_CVASGN:
1878 return RNODE_CVASGN(node)->nd_vid;
1879 default:
1880 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1881 return 0;
1882 }
1883}
1884
1885static NODE *
1886get_nd_args(struct parser_params *p, NODE *node)
1887{
1888 switch (nd_type(node)) {
1889 case NODE_CALL:
1890 return RNODE_CALL(node)->nd_args;
1891 case NODE_OPCALL:
1892 return RNODE_OPCALL(node)->nd_args;
1893 case NODE_FCALL:
1894 return RNODE_FCALL(node)->nd_args;
1895 case NODE_QCALL:
1896 return RNODE_QCALL(node)->nd_args;
1897 case NODE_VCALL:
1898 case NODE_SUPER:
1899 case NODE_ZSUPER:
1900 case NODE_YIELD:
1901 case NODE_RETURN:
1902 case NODE_BREAK:
1903 case NODE_NEXT:
1904 return 0;
1905 default:
1906 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1907 return 0;
1908 }
1909}
1910#endif
1911%}
1912
1913%expect 0
1914%define api.pure
1915%define parse.error verbose
1916%printer {
1917#ifndef RIPPER
1918 if ((NODE *)$$ == (NODE *)-1) {
1919 rb_parser_printf(p, "NODE_SPECIAL");
1920 }
1921 else if ($$) {
1922 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
1923 }
1924#else
1925#endif
1926} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg> <node_kw_arg> <node_block_pass>
1927%printer {
1928#ifndef RIPPER
1929 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
1930#else
1931 rb_parser_printf(p, "%"PRIsVALUE, RNODE_RIPPER($$)->nd_rval);
1932#endif
1933} tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN
1934%printer {
1935#ifndef RIPPER
1936 rb_parser_printf(p, "%+"PRIsVALUE, RNODE_LIT($$)->nd_lit);
1937#else
1938 rb_parser_printf(p, "%+"PRIsVALUE, get_value($$));
1939#endif
1940} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
1941%printer {
1942#ifndef RIPPER
1943 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
1944#else
1945 rb_parser_printf(p, "%"PRIsVALUE, $$);
1946#endif
1947} tNTH_REF
1948%printer {
1949#ifndef RIPPER
1950 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
1951#else
1952 rb_parser_printf(p, "%"PRIsVALUE, $$);
1953#endif
1954} tBACK_REF
1955
1956%lex-param {struct parser_params *p}
1957%parse-param {struct parser_params *p}
1958%initial-action
1959{
1960 RUBY_SET_YYLLOC_OF_NONE(@$);
1961};
1962
1963%union {
1964 VALUE val;
1965 NODE *node;
1966 rb_node_fcall_t *node_fcall;
1967 rb_node_args_t *node_args;
1968 rb_node_args_aux_t *node_args_aux;
1969 rb_node_opt_arg_t *node_opt_arg;
1970 rb_node_kw_arg_t *node_kw_arg;
1971 rb_node_block_pass_t *node_block_pass;
1972 rb_node_masgn_t *node_masgn;
1973 rb_node_def_temp_t *node_def_temp;
1974 rb_node_exits_t *node_exits;
1975 ID id;
1976 int num;
1977 st_table *tbl;
1978 const struct vtable *vars;
1979 struct rb_strterm_struct *strterm;
1980 struct lex_context ctxt;
1981}
1982
1983%token <id>
1984 keyword_class "`class'"
1985 keyword_module "`module'"
1986 keyword_def "`def'"
1987 keyword_undef "`undef'"
1988 keyword_begin "`begin'"
1989 keyword_rescue "`rescue'"
1990 keyword_ensure "`ensure'"
1991 keyword_end "`end'"
1992 keyword_if "`if'"
1993 keyword_unless "`unless'"
1994 keyword_then "`then'"
1995 keyword_elsif "`elsif'"
1996 keyword_else "`else'"
1997 keyword_case "`case'"
1998 keyword_when "`when'"
1999 keyword_while "`while'"
2000 keyword_until "`until'"
2001 keyword_for "`for'"
2002 keyword_break "`break'"
2003 keyword_next "`next'"
2004 keyword_redo "`redo'"
2005 keyword_retry "`retry'"
2006 keyword_in "`in'"
2007 keyword_do "`do'"
2008 keyword_do_cond "`do' for condition"
2009 keyword_do_block "`do' for block"
2010 keyword_do_LAMBDA "`do' for lambda"
2011 keyword_return "`return'"
2012 keyword_yield "`yield'"
2013 keyword_super "`super'"
2014 keyword_self "`self'"
2015 keyword_nil "`nil'"
2016 keyword_true "`true'"
2017 keyword_false "`false'"
2018 keyword_and "`and'"
2019 keyword_or "`or'"
2020 keyword_not "`not'"
2021 modifier_if "`if' modifier"
2022 modifier_unless "`unless' modifier"
2023 modifier_while "`while' modifier"
2024 modifier_until "`until' modifier"
2025 modifier_rescue "`rescue' modifier"
2026 keyword_alias "`alias'"
2027 keyword_defined "`defined?'"
2028 keyword_BEGIN "`BEGIN'"
2029 keyword_END "`END'"
2030 keyword__LINE__ "`__LINE__'"
2031 keyword__FILE__ "`__FILE__'"
2032 keyword__ENCODING__ "`__ENCODING__'"
2033
2034%token <id> tIDENTIFIER "local variable or method"
2035%token <id> tFID "method"
2036%token <id> tGVAR "global variable"
2037%token <id> tIVAR "instance variable"
2038%token <id> tCONSTANT "constant"
2039%token <id> tCVAR "class variable"
2040%token <id> tLABEL "label"
2041%token <node> tINTEGER "integer literal"
2042%token <node> tFLOAT "float literal"
2043%token <node> tRATIONAL "rational literal"
2044%token <node> tIMAGINARY "imaginary literal"
2045%token <node> tCHAR "char literal"
2046%token <node> tNTH_REF "numbered reference"
2047%token <node> tBACK_REF "back reference"
2048%token <node> tSTRING_CONTENT "literal content"
2049%token <num> tREGEXP_END
2050%token <num> tDUMNY_END "dummy end"
2051
2052%type <node> singleton strings string string1 xstring regexp
2053%type <node> string_contents xstring_contents regexp_contents string_content
2054%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2055%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2056/*ripper*/ %type <node_def_temp> defn_head defs_head k_def
2057/*ripper*/ %type <node_exits> block_open k_while k_until k_for allow_exits
2058%type <node> top_compstmt top_stmts top_stmt begin_block endless_arg endless_command
2059%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
2060%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2061%type <node_fcall> fcall
2062%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2063%type <node> args arg_splat call_args opt_call_args
2064%type <node> paren_args opt_paren_args
2065%type <node_args> args_tail opt_args_tail block_args_tail opt_block_args_tail
2066%type <node> command_args aref_args
2067%type <node_block_pass> opt_block_arg block_arg
2068%type <node> var_ref var_lhs
2069%type <node> command_rhs arg_rhs
2070%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2071%type <node_opt_arg> f_block_optarg f_block_opt
2072%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args
2073%type <node_args_aux> f_arg f_arg_item
2074%type <node_opt_arg> f_optarg
2075%type <node> f_marg f_marg_list f_rest_marg
2076%type <node_masgn> f_margs
2077%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2078%type <node_args> block_param opt_block_param block_param_def
2079%type <node_opt_arg> f_opt
2080%type <node_kw_arg> f_kwarg f_kw f_block_kwarg f_block_kw
2081%type <node> bv_decls opt_bv_decl bvar
2082%type <node> lambda lambda_body brace_body do_body
2083%type <node_args> f_larglist
2084%type <node> brace_block cmd_brace_block do_block lhs none fitem
2085%type <node> mlhs_head mlhs_item mlhs_node mlhs_post
2086%type <node_masgn> mlhs mlhs_basic mlhs_inner
2087%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2088%type <node> p_expr p_as p_alt p_expr_basic p_find
2089%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2090%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2091%type <node> p_kwargs p_kwarg p_kw
2092%type <id> keyword_variable user_variable sym operation operation2 operation3
2093%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
2094%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2095%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2096%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2097%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2098%type <ctxt> p_in_kwarg
2099%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2100/* ripper */ %type <num> max_numparam
2101/* ripper */ %type <node> numparam
2102%token END_OF_INPUT 0 "end-of-input"
2103%token <id> '.'
2104
2105/* escaped chars, should be ignored otherwise */
2106%token <id> '\\' "backslash"
2107%token tSP "escaped space"
2108%token <id> '\t' "escaped horizontal tab"
2109%token <id> '\f' "escaped form feed"
2110%token <id> '\r' "escaped carriage return"
2111%token <id> '\13' "escaped vertical tab"
2112%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2113%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2114%token tPOW RUBY_TOKEN(POW) "**"
2115%token tCMP RUBY_TOKEN(CMP) "<=>"
2116%token tEQ RUBY_TOKEN(EQ) "=="
2117%token tEQQ RUBY_TOKEN(EQQ) "==="
2118%token tNEQ RUBY_TOKEN(NEQ) "!="
2119%token tGEQ RUBY_TOKEN(GEQ) ">="
2120%token tLEQ RUBY_TOKEN(LEQ) "<="
2121%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2122%token tOROP RUBY_TOKEN(OROP) "||"
2123%token tMATCH RUBY_TOKEN(MATCH) "=~"
2124%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2125%token tDOT2 RUBY_TOKEN(DOT2) ".."
2126%token tDOT3 RUBY_TOKEN(DOT3) "..."
2127%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2128%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2129%token tAREF RUBY_TOKEN(AREF) "[]"
2130%token tASET RUBY_TOKEN(ASET) "[]="
2131%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2132%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2133%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2134%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2135%token tCOLON3 ":: at EXPR_BEG"
2136%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2137%token tASSOC "=>"
2138%token tLPAREN "("
2139%token tLPAREN_ARG "( arg"
2140%token tRPAREN ")"
2141%token tLBRACK "["
2142%token tLBRACE "{"
2143%token tLBRACE_ARG "{ arg"
2144%token tSTAR "*"
2145%token tDSTAR "**arg"
2146%token tAMPER "&"
2147%token tLAMBDA "->"
2148%token tSYMBEG "symbol literal"
2149%token tSTRING_BEG "string literal"
2150%token tXSTRING_BEG "backtick literal"
2151%token tREGEXP_BEG "regexp literal"
2152%token tWORDS_BEG "word list"
2153%token tQWORDS_BEG "verbatim word list"
2154%token tSYMBOLS_BEG "symbol list"
2155%token tQSYMBOLS_BEG "verbatim symbol list"
2156%token tSTRING_END "terminator"
2157%token tSTRING_DEND "'}'"
2158%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
2159
2160%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2161%token tHEREDOC_BEG tHEREDOC_END k__END__
2162
2163/*
2164 * precedence table
2165 */
2166
2167%nonassoc tLOWEST
2168%nonassoc tLBRACE_ARG
2169
2170%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2171%left keyword_or keyword_and
2172%right keyword_not
2173%nonassoc keyword_defined
2174%right '=' tOP_ASGN
2175%left modifier_rescue
2176%right '?' ':'
2177%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2178%left tOROP
2179%left tANDOP
2180%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2181%left '>' tGEQ '<' tLEQ
2182%left '|' '^'
2183%left '&'
2184%left tLSHFT tRSHFT
2185%left '+' '-'
2186%left '*' '/' '%'
2187%right tUMINUS_NUM tUMINUS
2188%right tPOW
2189%right '!' '~' tUPLUS
2190
2191%token tLAST_TOKEN
2192
2193%%
2194program : {
2195 SET_LEX_STATE(EXPR_BEG);
2196 local_push(p, ifndef_ripper(1)+0);
2197 /* jumps are possible in the top-level loop. */
2198 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
2199 }
2200 top_compstmt
2201 {
2202 /*%%%*/
2203 if ($2 && !compile_for_eval) {
2204 NODE *node = $2;
2205 /* last expression should not be void */
2206 if (nd_type_p(node, NODE_BLOCK)) {
2207 while (RNODE_BLOCK(node)->nd_next) {
2208 node = RNODE_BLOCK(node)->nd_next;
2209 }
2210 node = RNODE_BLOCK(node)->nd_head;
2211 }
2212 node = remove_begin(node);
2213 void_expr(p, node);
2214 }
2215 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
2216 /*% %*/
2217 /*% ripper[final]: program!($2) %*/
2218 local_pop(p);
2219 }
2220 ;
2221
2222top_compstmt : top_stmts opt_terms
2223 {
2224 $$ = void_stmts(p, $1);
2225 }
2226 ;
2227
2228top_stmts : none
2229 {
2230 /*%%%*/
2231 $$ = NEW_BEGIN(0, &@$);
2232 /*% %*/
2233 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
2234 }
2235 | top_stmt
2236 {
2237 /*%%%*/
2238 $$ = newline_node($1);
2239 /*% %*/
2240 /*% ripper: stmts_add!(stmts_new!, $1) %*/
2241 }
2242 | top_stmts terms top_stmt
2243 {
2244 /*%%%*/
2245 $$ = block_append(p, $1, newline_node($3));
2246 /*% %*/
2247 /*% ripper: stmts_add!($1, $3) %*/
2248 }
2249 ;
2250
2251top_stmt : stmt
2252 {
2253 clear_block_exit(p, true);
2254 $$ = $1;
2255 }
2256 | keyword_BEGIN begin_block
2257 {
2258 $$ = $2;
2259 }
2260 ;
2261
2262block_open : '{' {$$ = init_block_exit(p);};
2263
2264begin_block : block_open top_compstmt '}'
2265 {
2266 restore_block_exit(p, $block_open);
2267 /*%%%*/
2268 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
2269 NEW_BEGIN($2, &@$));
2270 $$ = NEW_BEGIN(0, &@$);
2271 /*% %*/
2272 /*% ripper: BEGIN!($2) %*/
2273 }
2274 ;
2275
2276bodystmt : compstmt[body]
2277 lex_ctxt[ctxt]
2278 opt_rescue
2279 k_else
2280 {
2281 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
2282 next_rescue_context(&p->ctxt, &$ctxt, after_else);
2283 }
2284 compstmt[elsebody]
2285 {
2286 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
2287 }
2288 opt_ensure
2289 {
2290 /*%%%*/
2291 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
2292 /*% %*/
2293 /*% ripper: bodystmt!($body, $opt_rescue, $elsebody, $opt_ensure) %*/
2294 }
2295 | compstmt[body]
2296 lex_ctxt[ctxt]
2297 opt_rescue
2298 {
2299 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
2300 }
2301 opt_ensure
2302 {
2303 /*%%%*/
2304 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
2305 /*% %*/
2306 /*% ripper: bodystmt!($body, $opt_rescue, Qnil, $opt_ensure) %*/
2307 }
2308 ;
2309
2310compstmt : stmts opt_terms
2311 {
2312 $$ = void_stmts(p, $1);
2313 }
2314 ;
2315
2316stmts : none
2317 {
2318 /*%%%*/
2319 $$ = NEW_BEGIN(0, &@$);
2320 /*% %*/
2321 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
2322 }
2323 | stmt_or_begin
2324 {
2325 /*%%%*/
2326 $$ = newline_node($1);
2327 /*% %*/
2328 /*% ripper: stmts_add!(stmts_new!, $1) %*/
2329 }
2330 | stmts terms stmt_or_begin
2331 {
2332 /*%%%*/
2333 $$ = block_append(p, $1, newline_node($3));
2334 /*% %*/
2335 /*% ripper: stmts_add!($1, $3) %*/
2336 }
2337 ;
2338
2339stmt_or_begin : stmt
2340 {
2341 $$ = $1;
2342 }
2343 | keyword_BEGIN
2344 {
2345 yyerror1(&@1, "BEGIN is permitted only at toplevel");
2346 }
2347 begin_block
2348 {
2349 $$ = $3;
2350 }
2351 ;
2352
2353allow_exits : {$$ = allow_block_exit(p);};
2354
2355k_END : keyword_END lex_ctxt
2356 {
2357 $$ = $2;
2358 p->ctxt.in_rescue = before_rescue;
2359 };
2360
2361stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2362 {
2363 /*%%%*/
2364 $$ = NEW_ALIAS($2, $4, &@$);
2365 /*% %*/
2366 /*% ripper: alias!($2, $4) %*/
2367 }
2368 | keyword_alias tGVAR tGVAR
2369 {
2370 /*%%%*/
2371 $$ = NEW_VALIAS($2, $3, &@$);
2372 /*% %*/
2373 /*% ripper: var_alias!($2, $3) %*/
2374 }
2375 | keyword_alias tGVAR tBACK_REF
2376 {
2377 /*%%%*/
2378 char buf[2];
2379 buf[0] = '$';
2380 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
2381 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
2382 /*% %*/
2383 /*% ripper: var_alias!($2, $3) %*/
2384 }
2385 | keyword_alias tGVAR tNTH_REF
2386 {
2387 static const char mesg[] = "can't make alias for the number variables";
2388 /*%%%*/
2389 yyerror1(&@3, mesg);
2390 $$ = NEW_BEGIN(0, &@$);
2391 /*% %*/
2392 /*% ripper[error]: alias_error!(ERR_MESG(), $3) %*/
2393 }
2394 | keyword_undef undef_list
2395 {
2396 /*%%%*/
2397 $$ = $2;
2398 /*% %*/
2399 /*% ripper: undef!($2) %*/
2400 }
2401 | stmt modifier_if expr_value
2402 {
2403 /*%%%*/
2404 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
2405 fixpos($$, $3);
2406 /*% %*/
2407 /*% ripper: if_mod!($3, $1) %*/
2408 }
2409 | stmt modifier_unless expr_value
2410 {
2411 /*%%%*/
2412 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
2413 fixpos($$, $3);
2414 /*% %*/
2415 /*% ripper: unless_mod!($3, $1) %*/
2416 }
2417 | stmt modifier_while expr_value
2418 {
2419 clear_block_exit(p, false);
2420 /*%%%*/
2421 if ($1 && nd_type_p($1, NODE_BEGIN)) {
2422 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$);
2423 }
2424 else {
2425 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
2426 }
2427 /*% %*/
2428 /*% ripper: while_mod!($3, $1) %*/
2429 }
2430 | stmt modifier_until expr_value
2431 {
2432 clear_block_exit(p, false);
2433 /*%%%*/
2434 if ($1 && nd_type_p($1, NODE_BEGIN)) {
2435 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$);
2436 }
2437 else {
2438 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
2439 }
2440 /*% %*/
2441 /*% ripper: until_mod!($3, $1) %*/
2442 }
2443 | stmt modifier_rescue after_rescue stmt
2444 {
2445 p->ctxt.in_rescue = $3.in_rescue;
2446 /*%%%*/
2447 NODE *resq;
2448 YYLTYPE loc = code_loc_gen(&@2, &@4);
2449 resq = NEW_RESBODY(0, remove_begin($4), 0, &loc);
2450 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
2451 /*% %*/
2452 /*% ripper: rescue_mod!($1, $4) %*/
2453 }
2454 | k_END allow_exits '{' compstmt '}'
2455 {
2456 if (p->ctxt.in_def) {
2457 rb_warn0("END in method; use at_exit");
2458 }
2459 restore_block_exit(p, $allow_exits);
2460 p->ctxt = $k_END;
2461 /*%%%*/
2462 {
2463 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, &@$);
2464 $$ = NEW_POSTEXE(scope, &@$);
2465 }
2466 /*% %*/
2467 /*% ripper: END!($compstmt) %*/
2468 }
2469 | command_asgn
2470 | mlhs '=' lex_ctxt command_call
2471 {
2472 /*%%%*/
2473 value_expr($4);
2474 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
2475 /*% %*/
2476 /*% ripper: massign!($1, $4) %*/
2477 }
2478 | lhs '=' lex_ctxt mrhs
2479 {
2480 /*%%%*/
2481 $$ = node_assign(p, $1, $4, $3, &@$);
2482 /*% %*/
2483 /*% ripper: assign!($1, $4) %*/
2484 }
2485 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
2486 after_rescue stmt[resbody]
2487 {
2488 p->ctxt.in_rescue = $3.in_rescue;
2489 /*%%%*/
2490 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
2491 $resbody = NEW_RESBODY(0, remove_begin($resbody), 0, &loc);
2492 loc.beg_pos = @mrhs_arg.beg_pos;
2493 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
2494 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
2495 /*% %*/
2496 /*% ripper: massign!($1, rescue_mod!($4, $7)) %*/
2497 }
2498 | mlhs '=' lex_ctxt mrhs_arg
2499 {
2500 /*%%%*/
2501 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
2502 /*% %*/
2503 /*% ripper: massign!($1, $4) %*/
2504 }
2505 | expr
2506 | error
2507 {
2508 (void)yynerrs;
2509 /*%%%*/
2510 $$ = NEW_ERROR(&@$);
2511 /*% %*/
2512 }
2513 ;
2514
2515command_asgn : lhs '=' lex_ctxt command_rhs
2516 {
2517 /*%%%*/
2518 $$ = node_assign(p, $1, $4, $3, &@$);
2519 /*% %*/
2520 /*% ripper: assign!($1, $4) %*/
2521 }
2522 | var_lhs tOP_ASGN lex_ctxt command_rhs
2523 {
2524 /*%%%*/
2525 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
2526 /*% %*/
2527 /*% ripper: opassign!($1, $2, $4) %*/
2528 }
2529 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
2530 {
2531 /*%%%*/
2532 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
2533 /*% %*/
2534 /*% ripper: opassign!(aref_field!($1, $3), $5, $7) %*/
2535
2536 }
2537 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
2538 {
2539 /*%%%*/
2540 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2541 /*% %*/
2542 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2543 }
2544 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt command_rhs
2545 {
2546 /*%%%*/
2547 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2548 /*% %*/
2549 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2550 }
2551 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
2552 {
2553 /*%%%*/
2554 YYLTYPE loc = code_loc_gen(&@1, &@3);
2555 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
2556 /*% %*/
2557 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
2558 }
2559 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
2560 {
2561 /*%%%*/
2562 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
2563 /*% %*/
2564 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2565 }
2566 | defn_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
2567 {
2568 endless_method_name(p, get_id($head->nd_mid), &@head);
2569 restore_defun(p, $head);
2570 /*%%%*/
2571 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
2572 ($$ = $head->nd_def)->nd_loc = @$;
2573 RNODE_DEFN($$)->nd_defn = $bodystmt;
2574 /*% %*/
2575 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
2576 /*% ripper: def!($head->nd_mid, $args, $bodystmt) %*/
2577 local_pop(p);
2578 }
2579 | defs_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
2580 {
2581 endless_method_name(p, get_id($head->nd_mid), &@head);
2582 restore_defun(p, $head);
2583 /*%%%*/
2584 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
2585 ($$ = $head->nd_def)->nd_loc = @$;
2586 RNODE_DEFS($$)->nd_defn = $bodystmt;
2587 /*% %*/
2588 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
2589 /*% ripper: defs!($head->nd_recv, $head->dot_or_colon, $head->nd_mid, $args, $bodystmt) %*/
2590 local_pop(p);
2591 }
2592 | backref tOP_ASGN lex_ctxt command_rhs
2593 {
2594 /*%%%*/
2595 rb_backref_error(p, $1);
2596 $$ = NEW_BEGIN(0, &@$);
2597 /*% %*/
2598 /*% ripper[error]: backref_error(p, RNODE($1), assign!(var_field(p, $1), $4)) %*/
2599 }
2600 ;
2601
2602endless_command : command
2603 | endless_command modifier_rescue after_rescue arg
2604 {
2605 p->ctxt.in_rescue = $3.in_rescue;
2606 /*%%%*/
2607 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
2608 /*% %*/
2609 /*% ripper: rescue_mod!($1, $4) %*/
2610 }
2611 | keyword_not opt_nl endless_command
2612 {
2613 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2614 }
2615 ;
2616
2617command_rhs : command_call %prec tOP_ASGN
2618 {
2619 value_expr($1);
2620 $$ = $1;
2621 }
2622 | command_call modifier_rescue after_rescue stmt
2623 {
2624 p->ctxt.in_rescue = $3.in_rescue;
2625 /*%%%*/
2626 YYLTYPE loc = code_loc_gen(&@2, &@4);
2627 value_expr($1);
2628 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($4), 0, &loc), 0, &@$);
2629 /*% %*/
2630 /*% ripper: rescue_mod!($1, $4) %*/
2631 }
2632 | command_asgn
2633 ;
2634
2635expr : command_call
2636 | expr keyword_and expr
2637 {
2638 $$ = logop(p, idAND, $1, $3, &@2, &@$);
2639 }
2640 | expr keyword_or expr
2641 {
2642 $$ = logop(p, idOR, $1, $3, &@2, &@$);
2643 }
2644 | keyword_not opt_nl expr
2645 {
2646 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2647 }
2648 | '!' command_call
2649 {
2650 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2651 }
2652 | arg tASSOC
2653 {
2654 value_expr($arg);
2655 }
2656 p_in_kwarg[ctxt] p_pvtbl p_pktbl
2657 p_top_expr_body[body]
2658 {
2659 pop_pktbl(p, $p_pktbl);
2660 pop_pvtbl(p, $p_pvtbl);
2661 p->ctxt.in_kwarg = $ctxt.in_kwarg;
2662 /*%%%*/
2663 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body), &@$);
2664 /*% %*/
2665 /*% ripper: case!($arg, in!($body, Qnil, Qnil)) %*/
2666 }
2667 | arg keyword_in
2668 {
2669 value_expr($arg);
2670 }
2671 p_in_kwarg[ctxt] p_pvtbl p_pktbl
2672 p_top_expr_body[body]
2673 {
2674 pop_pktbl(p, $p_pktbl);
2675 pop_pvtbl(p, $p_pvtbl);
2676 p->ctxt.in_kwarg = $ctxt.in_kwarg;
2677 /*%%%*/
2678 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body), &@$);
2679 /*% %*/
2680 /*% ripper: case!($arg, in!($body, Qnil, Qnil)) %*/
2681 }
2682 | arg %prec tLBRACE_ARG
2683 ;
2684
2685def_name : fname
2686 {
2687 ID fname = get_id($1);
2688 numparam_name(p, fname);
2689 local_push(p, 0);
2690 p->cur_arg = 0;
2691 p->ctxt.in_def = 1;
2692 p->ctxt.in_rescue = before_rescue;
2693 $$ = $1;
2694 }
2695 ;
2696
2697defn_head : k_def def_name
2698 {
2699 $$ = def_head_save(p, $k_def);
2700 $$->nd_mid = $def_name;
2701 /*%%%*/
2702 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
2703 /*%
2704 add_mark_object(p, $def_name);
2705 %*/
2706 }
2707 ;
2708
2709defs_head : k_def singleton dot_or_colon
2710 {
2711 SET_LEX_STATE(EXPR_FNAME);
2712 p->ctxt.in_argdef = 1;
2713 }
2714 def_name
2715 {
2716 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
2717 $$ = def_head_save(p, $k_def);
2718 $$->nd_mid = $def_name;
2719 /*%%%*/
2720 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
2721 /*%
2722 add_mark_object(p, $def_name);
2723 $$->nd_recv = add_mark_object(p, $singleton);
2724 $$->dot_or_colon = add_mark_object(p, $dot_or_colon);
2725 %*/
2726 }
2727 ;
2728
2729expr_value : expr
2730 {
2731 value_expr($1);
2732 $$ = $1;
2733 }
2734 | error
2735 {
2736 /*%%%*/
2737 $$ = NEW_ERROR(&@$);
2738 /*% %*/
2739 }
2740 ;
2741
2742expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
2743 {
2744 $$ = $2;
2745 }
2746 ;
2747
2748command_call : command
2749 | block_command
2750 ;
2751
2752block_command : block_call
2753 | block_call call_op2 operation2 command_args
2754 {
2755 /*%%%*/
2756 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
2757 /*% %*/
2758 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
2759 }
2760 ;
2761
2762cmd_brace_block : tLBRACE_ARG brace_body '}'
2763 {
2764 $$ = $2;
2765 /*%%%*/
2766 set_embraced_location($$, &@1, &@3);
2767 /*% %*/
2768 }
2769 ;
2770
2771fcall : operation
2772 {
2773 /*%%%*/
2774 $$ = NEW_FCALL($1, 0, &@$);
2775 /*% %*/
2776 /*% ripper: $1 %*/
2777 }
2778 ;
2779
2780command : fcall command_args %prec tLOWEST
2781 {
2782 /*%%%*/
2783 $1->nd_args = $2;
2784 nd_set_last_loc($1, @2.end_pos);
2785 $$ = (NODE *)$1;
2786 /*% %*/
2787 /*% ripper: command!($1, $2) %*/
2788 }
2789 | fcall command_args cmd_brace_block
2790 {
2791 /*%%%*/
2792 block_dup_check(p, $2, $3);
2793 $1->nd_args = $2;
2794 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
2795 fixpos($$, RNODE($1));
2796 nd_set_last_loc($1, @2.end_pos);
2797 /*% %*/
2798 /*% ripper: method_add_block!(command!($1, $2), $3) %*/
2799 }
2800 | primary_value call_op operation2 command_args %prec tLOWEST
2801 {
2802 /*%%%*/
2803 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
2804 /*% %*/
2805 /*% ripper: command_call!($1, $2, $3, $4) %*/
2806 }
2807 | primary_value call_op operation2 command_args cmd_brace_block
2808 {
2809 /*%%%*/
2810 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
2811 /*% %*/
2812 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
2813 }
2814 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
2815 {
2816 /*%%%*/
2817 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
2818 /*% %*/
2819 /*% ripper: command_call!($1, $2, $3, $4) %*/
2820 }
2821 | primary_value tCOLON2 operation2 command_args cmd_brace_block
2822 {
2823 /*%%%*/
2824 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
2825 /*% %*/
2826 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
2827 }
2828 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
2829 {
2830 /*%%%*/
2831 set_embraced_location($5, &@4, &@6);
2832 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, $5, &@3, &@$);
2833 /*% %*/
2834 /*% ripper: method_add_block!(command_call!($1, $2, $3, Qnull), $5) %*/
2835 }
2836 | keyword_super command_args
2837 {
2838 /*%%%*/
2839 $$ = NEW_SUPER($2, &@$);
2840 fixpos($$, $2);
2841 /*% %*/
2842 /*% ripper: super!($2) %*/
2843 }
2844 | k_yield command_args
2845 {
2846 /*%%%*/
2847 $$ = new_yield(p, $2, &@$);
2848 fixpos($$, $2);
2849 /*% %*/
2850 /*% ripper: yield!($2) %*/
2851 }
2852 | k_return call_args
2853 {
2854 /*%%%*/
2855 $$ = NEW_RETURN(ret_args(p, $2), &@$);
2856 /*% %*/
2857 /*% ripper: return!($2) %*/
2858 }
2859 | keyword_break call_args
2860 {
2861 NODE *args = 0;
2862 /*%%%*/
2863 args = ret_args(p, $2);
2864 /*% %*/
2865 $<node>$ = add_block_exit(p, NEW_BREAK(args, &@$));
2866 /*% ripper: break!($2) %*/
2867 }
2868 | keyword_next call_args
2869 {
2870 NODE *args = 0;
2871 /*%%%*/
2872 args = ret_args(p, $2);
2873 /*% %*/
2874 $<node>$ = add_block_exit(p, NEW_NEXT(args, &@$));
2875 /*% ripper: next!($2) %*/
2876 }
2877 ;
2878
2879mlhs : mlhs_basic
2880 | tLPAREN mlhs_inner rparen
2881 {
2882 /*%%%*/
2883 $$ = $2;
2884 /*% %*/
2885 /*% ripper: mlhs_paren!($2) %*/
2886 }
2887 ;
2888
2889mlhs_inner : mlhs_basic
2890 | tLPAREN mlhs_inner rparen
2891 {
2892 /*%%%*/
2893 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
2894 /*% %*/
2895 /*% ripper: mlhs_paren!($2) %*/
2896 }
2897 ;
2898
2899mlhs_basic : mlhs_head
2900 {
2901 /*%%%*/
2902 $$ = NEW_MASGN($1, 0, &@$);
2903 /*% %*/
2904 /*% ripper: $1 %*/
2905 }
2906 | mlhs_head mlhs_item
2907 {
2908 /*%%%*/
2909 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
2910 /*% %*/
2911 /*% ripper: mlhs_add!($1, $2) %*/
2912 }
2913 | mlhs_head tSTAR mlhs_node
2914 {
2915 /*%%%*/
2916 $$ = NEW_MASGN($1, $3, &@$);
2917 /*% %*/
2918 /*% ripper: mlhs_add_star!($1, $3) %*/
2919 }
2920 | mlhs_head tSTAR mlhs_node ',' mlhs_post
2921 {
2922 /*%%%*/
2923 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
2924 /*% %*/
2925 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
2926 }
2927 | mlhs_head tSTAR
2928 {
2929 /*%%%*/
2930 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
2931 /*% %*/
2932 /*% ripper: mlhs_add_star!($1, Qnil) %*/
2933 }
2934 | mlhs_head tSTAR ',' mlhs_post
2935 {
2936 /*%%%*/
2937 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
2938 /*% %*/
2939 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, Qnil), $4) %*/
2940 }
2941 | tSTAR mlhs_node
2942 {
2943 /*%%%*/
2944 $$ = NEW_MASGN(0, $2, &@$);
2945 /*% %*/
2946 /*% ripper: mlhs_add_star!(mlhs_new!, $2) %*/
2947 }
2948 | tSTAR mlhs_node ',' mlhs_post
2949 {
2950 /*%%%*/
2951 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
2952 /*% %*/
2953 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $2), $4) %*/
2954 }
2955 | tSTAR
2956 {
2957 /*%%%*/
2958 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
2959 /*% %*/
2960 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
2961 }
2962 | tSTAR ',' mlhs_post
2963 {
2964 /*%%%*/
2965 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
2966 /*% %*/
2967 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $3) %*/
2968 }
2969 ;
2970
2971mlhs_item : mlhs_node
2972 | tLPAREN mlhs_inner rparen
2973 {
2974 /*%%%*/
2975 $$ = (NODE *)$2;
2976 /*% %*/
2977 /*% ripper: mlhs_paren!($2) %*/
2978 }
2979 ;
2980
2981mlhs_head : mlhs_item ','
2982 {
2983 /*%%%*/
2984 $$ = NEW_LIST($1, &@1);
2985 /*% %*/
2986 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2987 }
2988 | mlhs_head mlhs_item ','
2989 {
2990 /*%%%*/
2991 $$ = list_append(p, $1, $2);
2992 /*% %*/
2993 /*% ripper: mlhs_add!($1, $2) %*/
2994 }
2995 ;
2996
2997mlhs_post : mlhs_item
2998 {
2999 /*%%%*/
3000 $$ = NEW_LIST($1, &@$);
3001 /*% %*/
3002 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
3003 }
3004 | mlhs_post ',' mlhs_item
3005 {
3006 /*%%%*/
3007 $$ = list_append(p, $1, $3);
3008 /*% %*/
3009 /*% ripper: mlhs_add!($1, $3) %*/
3010 }
3011 ;
3012
3013mlhs_node : user_variable
3014 {
3015 /*%%%*/
3016 $$ = assignable(p, $1, 0, &@$);
3017 /*% %*/
3018 /*% ripper: assignable(p, var_field(p, $1)) %*/
3019 }
3020 | keyword_variable
3021 {
3022 /*%%%*/
3023 $$ = assignable(p, $1, 0, &@$);
3024 /*% %*/
3025 /*% ripper: assignable(p, var_field(p, $1)) %*/
3026 }
3027 | primary_value '[' opt_call_args rbracket
3028 {
3029 /*%%%*/
3030 $$ = aryset(p, $1, $3, &@$);
3031 /*% %*/
3032 /*% ripper: aref_field!($1, $3) %*/
3033 }
3034 | primary_value call_op tIDENTIFIER
3035 {
3036 anddot_multiple_assignment_check(p, &@2, $2);
3037 /*%%%*/
3038 $$ = attrset(p, $1, $2, $3, &@$);
3039 /*% %*/
3040 /*% ripper: field!($1, $2, $3) %*/
3041 }
3042 | primary_value tCOLON2 tIDENTIFIER
3043 {
3044 /*%%%*/
3045 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3046 /*% %*/
3047 /*% ripper: const_path_field!($1, $3) %*/
3048 }
3049 | primary_value call_op tCONSTANT
3050 {
3051 anddot_multiple_assignment_check(p, &@2, $2);
3052 /*%%%*/
3053 $$ = attrset(p, $1, $2, $3, &@$);
3054 /*% %*/
3055 /*% ripper: field!($1, $2, $3) %*/
3056 }
3057 | primary_value tCOLON2 tCONSTANT
3058 {
3059 /*%%%*/
3060 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3061 /*% %*/
3062 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
3063 }
3064 | tCOLON3 tCONSTANT
3065 {
3066 /*%%%*/
3067 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3068 /*% %*/
3069 /*% ripper: const_decl(p, top_const_field!($2)) %*/
3070 }
3071 | backref
3072 {
3073 /*%%%*/
3074 rb_backref_error(p, $1);
3075 $$ = NEW_BEGIN(0, &@$);
3076 /*% %*/
3077 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
3078 }
3079 ;
3080
3081lhs : user_variable
3082 {
3083 /*%%%*/
3084 $$ = assignable(p, $1, 0, &@$);
3085 /*% %*/
3086 /*% ripper: assignable(p, var_field(p, $1)) %*/
3087 }
3088 | keyword_variable
3089 {
3090 /*%%%*/
3091 $$ = assignable(p, $1, 0, &@$);
3092 /*% %*/
3093 /*% ripper: assignable(p, var_field(p, $1)) %*/
3094 }
3095 | primary_value '[' opt_call_args rbracket
3096 {
3097 /*%%%*/
3098 $$ = aryset(p, $1, $3, &@$);
3099 /*% %*/
3100 /*% ripper: aref_field!($1, $3) %*/
3101 }
3102 | primary_value call_op tIDENTIFIER
3103 {
3104 /*%%%*/
3105 $$ = attrset(p, $1, $2, $3, &@$);
3106 /*% %*/
3107 /*% ripper: field!($1, $2, $3) %*/
3108 }
3109 | primary_value tCOLON2 tIDENTIFIER
3110 {
3111 /*%%%*/
3112 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3113 /*% %*/
3114 /*% ripper: field!($1, $2, $3) %*/
3115 }
3116 | primary_value call_op tCONSTANT
3117 {
3118 /*%%%*/
3119 $$ = attrset(p, $1, $2, $3, &@$);
3120 /*% %*/
3121 /*% ripper: field!($1, $2, $3) %*/
3122 }
3123 | primary_value tCOLON2 tCONSTANT
3124 {
3125 /*%%%*/
3126 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3127 /*% %*/
3128 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
3129 }
3130 | tCOLON3 tCONSTANT
3131 {
3132 /*%%%*/
3133 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3134 /*% %*/
3135 /*% ripper: const_decl(p, top_const_field!($2)) %*/
3136 }
3137 | backref
3138 {
3139 /*%%%*/
3140 rb_backref_error(p, $1);
3141 $$ = NEW_BEGIN(0, &@$);
3142 /*% %*/
3143 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
3144 }
3145 ;
3146
3147cname : tIDENTIFIER
3148 {
3149 static const char mesg[] = "class/module name must be CONSTANT";
3150 /*%%%*/
3151 yyerror1(&@1, mesg);
3152 /*% %*/
3153 /*% ripper[error]: class_name_error!(ERR_MESG(), $1) %*/
3154 }
3155 | tCONSTANT
3156 ;
3157
3158cpath : tCOLON3 cname
3159 {
3160 /*%%%*/
3161 $$ = NEW_COLON3($2, &@$);
3162 /*% %*/
3163 /*% ripper: top_const_ref!($2) %*/
3164 }
3165 | cname
3166 {
3167 /*%%%*/
3168 $$ = NEW_COLON2(0, $1, &@$);
3169 /*% %*/
3170 /*% ripper: const_ref!($1) %*/
3171 }
3172 | primary_value tCOLON2 cname
3173 {
3174 /*%%%*/
3175 $$ = NEW_COLON2($1, $3, &@$);
3176 /*% %*/
3177 /*% ripper: const_path_ref!($1, $3) %*/
3178 }
3179 ;
3180
3181fname : tIDENTIFIER
3182 | tCONSTANT
3183 | tFID
3184 | op
3185 {
3186 SET_LEX_STATE(EXPR_ENDFN);
3187 $$ = $1;
3188 }
3189 | reswords
3190 ;
3191
3192fitem : fname
3193 {
3194 /*%%%*/
3195 $$ = NEW_LIT(ID2SYM($1), &@$);
3196 /*% %*/
3197 /*% ripper: symbol_literal!($1) %*/
3198 }
3199 | symbol
3200 ;
3201
3202undef_list : fitem
3203 {
3204 /*%%%*/
3205 $$ = NEW_UNDEF($1, &@$);
3206 /*% %*/
3207 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
3208 }
3209 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3210 {
3211 /*%%%*/
3212 NODE *undef = NEW_UNDEF($4, &@4);
3213 $$ = block_append(p, $1, undef);
3214 /*% %*/
3215 /*% ripper: rb_ary_push($1, get_value($4)) %*/
3216 }
3217 ;
3218
3219op : '|' { ifndef_ripper($$ = '|'); }
3220 | '^' { ifndef_ripper($$ = '^'); }
3221 | '&' { ifndef_ripper($$ = '&'); }
3222 | tCMP { ifndef_ripper($$ = tCMP); }
3223 | tEQ { ifndef_ripper($$ = tEQ); }
3224 | tEQQ { ifndef_ripper($$ = tEQQ); }
3225 | tMATCH { ifndef_ripper($$ = tMATCH); }
3226 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
3227 | '>' { ifndef_ripper($$ = '>'); }
3228 | tGEQ { ifndef_ripper($$ = tGEQ); }
3229 | '<' { ifndef_ripper($$ = '<'); }
3230 | tLEQ { ifndef_ripper($$ = tLEQ); }
3231 | tNEQ { ifndef_ripper($$ = tNEQ); }
3232 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
3233 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
3234 | '+' { ifndef_ripper($$ = '+'); }
3235 | '-' { ifndef_ripper($$ = '-'); }
3236 | '*' { ifndef_ripper($$ = '*'); }
3237 | tSTAR { ifndef_ripper($$ = '*'); }
3238 | '/' { ifndef_ripper($$ = '/'); }
3239 | '%' { ifndef_ripper($$ = '%'); }
3240 | tPOW { ifndef_ripper($$ = tPOW); }
3241 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
3242 | '!' { ifndef_ripper($$ = '!'); }
3243 | '~' { ifndef_ripper($$ = '~'); }
3244 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
3245 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
3246 | tAREF { ifndef_ripper($$ = tAREF); }
3247 | tASET { ifndef_ripper($$ = tASET); }
3248 | '`' { ifndef_ripper($$ = '`'); }
3249 ;
3250
3251reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3252 | keyword_BEGIN | keyword_END
3253 | keyword_alias | keyword_and | keyword_begin
3254 | keyword_break | keyword_case | keyword_class | keyword_def
3255 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3256 | keyword_end | keyword_ensure | keyword_false
3257 | keyword_for | keyword_in | keyword_module | keyword_next
3258 | keyword_nil | keyword_not | keyword_or | keyword_redo
3259 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3260 | keyword_super | keyword_then | keyword_true | keyword_undef
3261 | keyword_when | keyword_yield | keyword_if | keyword_unless
3262 | keyword_while | keyword_until
3263 ;
3264
3265arg : lhs '=' lex_ctxt arg_rhs
3266 {
3267 /*%%%*/
3268 $$ = node_assign(p, $1, $4, $3, &@$);
3269 /*% %*/
3270 /*% ripper: assign!($1, $4) %*/
3271 }
3272 | var_lhs tOP_ASGN lex_ctxt arg_rhs
3273 {
3274 /*%%%*/
3275 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
3276 /*% %*/
3277 /*% ripper: opassign!($1, $2, $4) %*/
3278 }
3279 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
3280 {
3281 /*%%%*/
3282 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
3283 /*% %*/
3284 /*% ripper: opassign!(aref_field!($1, $3), $5, $7) %*/
3285 }
3286 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3287 {
3288 /*%%%*/
3289 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
3290 /*% %*/
3291 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
3292 }
3293 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3294 {
3295 /*%%%*/
3296 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
3297 /*% %*/
3298 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
3299 }
3300 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3301 {
3302 /*%%%*/
3303 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
3304 /*% %*/
3305 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
3306 }
3307 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3308 {
3309 /*%%%*/
3310 YYLTYPE loc = code_loc_gen(&@1, &@3);
3311 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
3312 /*% %*/
3313 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
3314 }
3315 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3316 {
3317 /*%%%*/
3318 YYLTYPE loc = code_loc_gen(&@1, &@2);
3319 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
3320 /*% %*/
3321 /*% ripper: opassign!(top_const_field!($2), $3, $5) %*/
3322 }
3323 | backref tOP_ASGN lex_ctxt arg_rhs
3324 {
3325 /*%%%*/
3326 rb_backref_error(p, $1);
3327 $$ = NEW_BEGIN(0, &@$);
3328 /*% %*/
3329 /*% ripper[error]: backref_error(p, RNODE($1), opassign!(var_field(p, $1), $2, $4)) %*/
3330 }
3331 | arg tDOT2 arg
3332 {
3333 /*%%%*/
3334 value_expr($1);
3335 value_expr($3);
3336 $$ = NEW_DOT2($1, $3, &@$);
3337 /*% %*/
3338 /*% ripper: dot2!($1, $3) %*/
3339 }
3340 | arg tDOT3 arg
3341 {
3342 /*%%%*/
3343 value_expr($1);
3344 value_expr($3);
3345 $$ = NEW_DOT3($1, $3, &@$);
3346 /*% %*/
3347 /*% ripper: dot3!($1, $3) %*/
3348 }
3349 | arg tDOT2
3350 {
3351 /*%%%*/
3352 value_expr($1);
3353 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
3354 /*% %*/
3355 /*% ripper: dot2!($1, Qnil) %*/
3356 }
3357 | arg tDOT3
3358 {
3359 /*%%%*/
3360 value_expr($1);
3361 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
3362 /*% %*/
3363 /*% ripper: dot3!($1, Qnil) %*/
3364 }
3365 | tBDOT2 arg
3366 {
3367 /*%%%*/
3368 value_expr($2);
3369 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
3370 /*% %*/
3371 /*% ripper: dot2!(Qnil, $2) %*/
3372 }
3373 | tBDOT3 arg
3374 {
3375 /*%%%*/
3376 value_expr($2);
3377 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
3378 /*% %*/
3379 /*% ripper: dot3!(Qnil, $2) %*/
3380 }
3381 | arg '+' arg
3382 {
3383 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3384 }
3385 | arg '-' arg
3386 {
3387 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3388 }
3389 | arg '*' arg
3390 {
3391 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3392 }
3393 | arg '/' arg
3394 {
3395 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3396 }
3397 | arg '%' arg
3398 {
3399 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3400 }
3401 | arg tPOW arg
3402 {
3403 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3404 }
3405 | tUMINUS_NUM simple_numeric tPOW arg
3406 {
3407 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3408 }
3409 | tUPLUS arg
3410 {
3411 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3412 }
3413 | tUMINUS arg
3414 {
3415 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3416 }
3417 | arg '|' arg
3418 {
3419 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3420 }
3421 | arg '^' arg
3422 {
3423 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3424 }
3425 | arg '&' arg
3426 {
3427 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3428 }
3429 | arg tCMP arg
3430 {
3431 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3432 }
3433 | rel_expr %prec tCMP
3434 | arg tEQ arg
3435 {
3436 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3437 }
3438 | arg tEQQ arg
3439 {
3440 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
3441 }
3442 | arg tNEQ arg
3443 {
3444 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
3445 }
3446 | arg tMATCH arg
3447 {
3448 $$ = match_op(p, $1, $3, &@2, &@$);
3449 }
3450 | arg tNMATCH arg
3451 {
3452 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
3453 }
3454 | '!' arg
3455 {
3456 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3457 }
3458 | '~' arg
3459 {
3460 $$ = call_uni_op(p, $2, '~', &@1, &@$);
3461 }
3462 | arg tLSHFT arg
3463 {
3464 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
3465 }
3466 | arg tRSHFT arg
3467 {
3468 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
3469 }
3470 | arg tANDOP arg
3471 {
3472 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
3473 }
3474 | arg tOROP arg
3475 {
3476 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
3477 }
3478 | keyword_defined opt_nl begin_defined arg
3479 {
3480 p->ctxt.in_defined = $3.in_defined;
3481 $$ = new_defined(p, $4, &@$);
3482 }
3483 | arg '?' arg opt_nl ':' arg
3484 {
3485 /*%%%*/
3486 value_expr($1);
3487 $$ = new_if(p, $1, $3, $6, &@$);
3488 fixpos($$, $1);
3489 /*% %*/
3490 /*% ripper: ifop!($1, $3, $6) %*/
3491 }
3492 | defn_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
3493 {
3494 endless_method_name(p, get_id($head->nd_mid), &@head);
3495 restore_defun(p, $head);
3496 /*%%%*/
3497 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3498 ($$ = $head->nd_def)->nd_loc = @$;
3499 RNODE_DEFN($$)->nd_defn = $bodystmt;
3500 /*% %*/
3501 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
3502 /*% ripper: def!($head->nd_mid, $args, $bodystmt) %*/
3503 local_pop(p);
3504 }
3505 | defs_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
3506 {
3507 endless_method_name(p, get_id($head->nd_mid), &@head);
3508 restore_defun(p, $head);
3509 /*%%%*/
3510 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3511 ($$ = $head->nd_def)->nd_loc = @$;
3512 RNODE_DEFS($$)->nd_defn = $bodystmt;
3513 /*% %*/
3514 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
3515 /*% ripper: defs!($head->nd_recv, $head->dot_or_colon, $head->nd_mid, $args, $bodystmt) %*/
3516 local_pop(p);
3517 }
3518 | primary
3519 {
3520 $$ = $1;
3521 }
3522 ;
3523
3524endless_arg : arg %prec modifier_rescue
3525 | endless_arg modifier_rescue after_rescue arg
3526 {
3527 p->ctxt.in_rescue = $3.in_rescue;
3528 /*%%%*/
3529 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3530 /*% %*/
3531 /*% ripper: rescue_mod!($1, $4) %*/
3532 }
3533 | keyword_not opt_nl endless_arg
3534 {
3535 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3536 }
3537 ;
3538
3539relop : '>' {$$ = '>';}
3540 | '<' {$$ = '<';}
3541 | tGEQ {$$ = idGE;}
3542 | tLEQ {$$ = idLE;}
3543 ;
3544
3545rel_expr : arg relop arg %prec '>'
3546 {
3547 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
3548 }
3549 | rel_expr relop arg %prec '>'
3550 {
3551 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
3552 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
3553 }
3554 ;
3555
3556lex_ctxt : none
3557 {
3558 $$ = p->ctxt;
3559 }
3560 ;
3561
3562begin_defined : lex_ctxt
3563 {
3564 p->ctxt.in_defined = 1;
3565 $$ = $1;
3566 }
3567 ;
3568
3569after_rescue : lex_ctxt
3570 {
3571 p->ctxt.in_rescue = after_rescue;
3572 $$ = $1;
3573 }
3574 ;
3575
3576arg_value : arg
3577 {
3578 value_expr($1);
3579 $$ = $1;
3580 }
3581 ;
3582
3583aref_args : none
3584 | args trailer
3585 {
3586 $$ = $1;
3587 }
3588 | args ',' assocs trailer
3589 {
3590 /*%%%*/
3591 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3592 /*% %*/
3593 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
3594 }
3595 | assocs trailer
3596 {
3597 /*%%%*/
3598 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
3599 /*% %*/
3600 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
3601 }
3602 ;
3603
3604arg_rhs : arg %prec tOP_ASGN
3605 {
3606 value_expr($1);
3607 $$ = $1;
3608 }
3609 | arg modifier_rescue after_rescue arg
3610 {
3611 p->ctxt.in_rescue = $3.in_rescue;
3612 /*%%%*/
3613 value_expr($1);
3614 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3615 /*% %*/
3616 /*% ripper: rescue_mod!($1, $4) %*/
3617 }
3618 ;
3619
3620paren_args : '(' opt_call_args rparen
3621 {
3622 /*%%%*/
3623 $$ = $2;
3624 /*% %*/
3625 /*% ripper: arg_paren!($2) %*/
3626 }
3627 | '(' args ',' args_forward rparen
3628 {
3629 if (!check_forwarding_args(p)) {
3630 $$ = Qnone;
3631 }
3632 else {
3633 /*%%%*/
3634 $$ = new_args_forward_call(p, $2, &@4, &@$);
3635 /*% %*/
3636 /*% ripper: arg_paren!(args_add!($2, $4)) %*/
3637 }
3638 }
3639 | '(' args_forward rparen
3640 {
3641 if (!check_forwarding_args(p)) {
3642 $$ = Qnone;
3643 }
3644 else {
3645 /*%%%*/
3646 $$ = new_args_forward_call(p, 0, &@2, &@$);
3647 /*% %*/
3648 /*% ripper: arg_paren!($2) %*/
3649 }
3650 }
3651 ;
3652
3653opt_paren_args : none
3654 | paren_args
3655 ;
3656
3657opt_call_args : none
3658 | call_args
3659 | args ','
3660 {
3661 $$ = $1;
3662 }
3663 | args ',' assocs ','
3664 {
3665 /*%%%*/
3666 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3667 /*% %*/
3668 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
3669 }
3670 | assocs ','
3671 {
3672 /*%%%*/
3673 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3674 /*% %*/
3675 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
3676 }
3677 ;
3678
3679call_args : command
3680 {
3681 /*%%%*/
3682 value_expr($1);
3683 $$ = NEW_LIST($1, &@$);
3684 /*% %*/
3685 /*% ripper: args_add!(args_new!, $1) %*/
3686 }
3687 | args opt_block_arg
3688 {
3689 /*%%%*/
3690 $$ = arg_blk_pass($1, $2);
3691 /*% %*/
3692 /*% ripper: args_add_block!($1, $2) %*/
3693 }
3694 | assocs opt_block_arg
3695 {
3696 /*%%%*/
3697 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3698 $$ = arg_blk_pass($$, $2);
3699 /*% %*/
3700 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($1)), $2) %*/
3701 }
3702 | args ',' assocs opt_block_arg
3703 {
3704 /*%%%*/
3705 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3706 $$ = arg_blk_pass($$, $4);
3707 /*% %*/
3708 /*% ripper: args_add_block!(args_add!($1, bare_assoc_hash!($3)), $4) %*/
3709 }
3710 | block_arg
3711 /*% ripper[brace]: args_add_block!(args_new!, $1) %*/
3712 ;
3713
3714command_args : {
3715 /* If call_args starts with a open paren '(' or '[',
3716 * look-ahead reading of the letters calls CMDARG_PUSH(0),
3717 * but the push must be done after CMDARG_PUSH(1).
3718 * So this code makes them consistent by first cancelling
3719 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
3720 * and finally redoing CMDARG_PUSH(0).
3721 */
3722 int lookahead = 0;
3723 switch (yychar) {
3724 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
3725 lookahead = 1;
3726 }
3727 if (lookahead) CMDARG_POP();
3728 CMDARG_PUSH(1);
3729 if (lookahead) CMDARG_PUSH(0);
3730 }
3731 call_args
3732 {
3733 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
3734 * but the push must be done after CMDARG_POP() in the parser.
3735 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
3736 * CMDARG_POP() to pop 1 pushed by command_args,
3737 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
3738 */
3739 int lookahead = 0;
3740 switch (yychar) {
3741 case tLBRACE_ARG:
3742 lookahead = 1;
3743 }
3744 if (lookahead) CMDARG_POP();
3745 CMDARG_POP();
3746 if (lookahead) CMDARG_PUSH(0);
3747 $$ = $2;
3748 }
3749 ;
3750
3751block_arg : tAMPER arg_value
3752 {
3753 /*%%%*/
3754 $$ = NEW_BLOCK_PASS($2, &@$);
3755 /*% %*/
3756 /*% ripper: $2 %*/
3757 }
3758 | tAMPER
3759 {
3760 forwarding_arg_check(p, idFWD_BLOCK, 0, "block");
3761 /*%%%*/
3762 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$);
3763 /*% %*/
3764 /*% ripper: Qnil %*/
3765 }
3766 ;
3767
3768opt_block_arg : ',' block_arg
3769 {
3770 $$ = $2;
3771 }
3772 | none
3773 {
3774 $$ = 0;
3775 }
3776 ;
3777
3778/* value */
3779args : arg_value
3780 {
3781 /*%%%*/
3782 $$ = NEW_LIST($1, &@$);
3783 /*% %*/
3784 /*% ripper: args_add!(args_new!, $1) %*/
3785 }
3786 | arg_splat
3787 {
3788 /*%%%*/
3789 $$ = NEW_SPLAT($arg_splat, &@$);
3790 /*% %*/
3791 /*% ripper: args_add_star!(args_new!, $arg_splat) %*/
3792 }
3793 | args ',' arg_value
3794 {
3795 /*%%%*/
3796 $$ = last_arg_append(p, $1, $3, &@$);
3797 /*% %*/
3798 /*% ripper: args_add!($1, $3) %*/
3799 }
3800 | args ',' arg_splat
3801 {
3802 /*%%%*/
3803 $$ = rest_arg_append(p, $args, $arg_splat, &@$);
3804 /*% %*/
3805 /*% ripper: args_add_star!($args, $arg_splat) %*/
3806 }
3807 ;
3808
3809/* value */
3810arg_splat : tSTAR arg_value
3811 {
3812 $$ = $2;
3813 }
3814 | tSTAR /* none */
3815 {
3816 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
3817 /*%%%*/
3818 $$ = NEW_LVAR(idFWD_REST, &@1);
3819 /*% %*/
3820 /*% ripper: Qnil %*/
3821 }
3822 ;
3823
3824/* value */
3825mrhs_arg : mrhs
3826 | arg_value
3827 ;
3828
3829/* value */
3830mrhs : args ',' arg_value
3831 {
3832 /*%%%*/
3833 $$ = last_arg_append(p, $1, $3, &@$);
3834 /*% %*/
3835 /*% ripper: mrhs_add!(mrhs_new_from_args!($1), $3) %*/
3836 }
3837 | args ',' tSTAR arg_value
3838 {
3839 /*%%%*/
3840 $$ = rest_arg_append(p, $1, $4, &@$);
3841 /*% %*/
3842 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($1), $4) %*/
3843 }
3844 | tSTAR arg_value
3845 {
3846 /*%%%*/
3847 $$ = NEW_SPLAT($2, &@$);
3848 /*% %*/
3849 /*% ripper: mrhs_add_star!(mrhs_new!, $2) %*/
3850 }
3851 ;
3852
3853primary : literal
3854 | strings
3855 | xstring
3856 | regexp
3857 | words
3858 | qwords
3859 | symbols
3860 | qsymbols
3861 | var_ref
3862 | backref
3863 | tFID
3864 {
3865 /*%%%*/
3866 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
3867 /*% %*/
3868 /*% ripper: method_add_arg!(fcall!($1), args_new!) %*/
3869 }
3870 | k_begin
3871 {
3872 CMDARG_PUSH(0);
3873 }
3874 bodystmt
3875 k_end
3876 {
3877 CMDARG_POP();
3878 /*%%%*/
3879 set_line_body($3, @1.end_pos.lineno);
3880 $$ = NEW_BEGIN($3, &@$);
3881 nd_set_line($$, @1.end_pos.lineno);
3882 /*% %*/
3883 /*% ripper: begin!($3) %*/
3884 }
3885 | tLPAREN_ARG compstmt {SET_LEX_STATE(EXPR_ENDARG);} ')'
3886 {
3887 /*%%%*/
3888 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
3889 $$ = $2;
3890 /*% %*/
3891 /*% ripper: paren!($2) %*/
3892 }
3893 | tLPAREN compstmt ')'
3894 {
3895 /*%%%*/
3896 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
3897 $$ = NEW_BEGIN($2, &@$);
3898 /*% %*/
3899 /*% ripper: paren!($2) %*/
3900 }
3901 | primary_value tCOLON2 tCONSTANT
3902 {
3903 /*%%%*/
3904 $$ = NEW_COLON2($1, $3, &@$);
3905 /*% %*/
3906 /*% ripper: const_path_ref!($1, $3) %*/
3907 }
3908 | tCOLON3 tCONSTANT
3909 {
3910 /*%%%*/
3911 $$ = NEW_COLON3($2, &@$);
3912 /*% %*/
3913 /*% ripper: top_const_ref!($2) %*/
3914 }
3915 | tLBRACK aref_args ']'
3916 {
3917 /*%%%*/
3918 $$ = make_list($2, &@$);
3919 /*% %*/
3920 /*% ripper: array!($2) %*/
3921 }
3922 | tLBRACE assoc_list '}'
3923 {
3924 /*%%%*/
3925 $$ = new_hash(p, $2, &@$);
3926 RNODE_HASH($$)->nd_brace = TRUE;
3927 /*% %*/
3928 /*% ripper: hash!($2) %*/
3929 }
3930 | k_return
3931 {
3932 /*%%%*/
3933 $$ = NEW_RETURN(0, &@$);
3934 /*% %*/
3935 /*% ripper: return0! %*/
3936 }
3937 | k_yield '(' call_args rparen
3938 {
3939 /*%%%*/
3940 $$ = new_yield(p, $3, &@$);
3941 /*% %*/
3942 /*% ripper: yield!(paren!($3)) %*/
3943 }
3944 | k_yield '(' rparen
3945 {
3946 /*%%%*/
3947 $$ = NEW_YIELD(0, &@$);
3948 /*% %*/
3949 /*% ripper: yield!(paren!(args_new!)) %*/
3950 }
3951 | k_yield
3952 {
3953 /*%%%*/
3954 $$ = NEW_YIELD(0, &@$);
3955 /*% %*/
3956 /*% ripper: yield0! %*/
3957 }
3958 | keyword_defined opt_nl '(' begin_defined expr rparen
3959 {
3960 p->ctxt.in_defined = $4.in_defined;
3961 $$ = new_defined(p, $5, &@$);
3962 }
3963 | keyword_not '(' expr rparen
3964 {
3965 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3966 }
3967 | keyword_not '(' rparen
3968 {
3969 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
3970 }
3971 | fcall brace_block
3972 {
3973 /*%%%*/
3974 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
3975 /*% %*/
3976 /*% ripper: method_add_block!(method_add_arg!(fcall!($1), args_new!), $2) %*/
3977 }
3978 | method_call
3979 | method_call brace_block
3980 {
3981 /*%%%*/
3982 block_dup_check(p, get_nd_args(p, $1), $2);
3983 $$ = method_add_block(p, $1, $2, &@$);
3984 /*% %*/
3985 /*% ripper: method_add_block!($1, $2) %*/
3986 }
3987 | lambda
3988 | k_if expr_value then
3989 compstmt
3990 if_tail
3991 k_end
3992 {
3993 /*%%%*/
3994 $$ = new_if(p, $2, $4, $5, &@$);
3995 fixpos($$, $2);
3996 /*% %*/
3997 /*% ripper: if!($2, $4, $5) %*/
3998 }
3999 | k_unless expr_value then
4000 compstmt
4001 opt_else
4002 k_end
4003 {
4004 /*%%%*/
4005 $$ = new_unless(p, $2, $4, $5, &@$);
4006 fixpos($$, $2);
4007 /*% %*/
4008 /*% ripper: unless!($2, $4, $5) %*/
4009 }
4010 | k_while expr_value_do
4011 compstmt
4012 k_end
4013 {
4014 restore_block_exit(p, $1);
4015 /*%%%*/
4016 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
4017 fixpos($$, $2);
4018 /*% %*/
4019 /*% ripper: while!($2, $3) %*/
4020 }
4021 | k_until expr_value_do
4022 compstmt
4023 k_end
4024 {
4025 restore_block_exit(p, $1);
4026 /*%%%*/
4027 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
4028 fixpos($$, $2);
4029 /*% %*/
4030 /*% ripper: until!($2, $3) %*/
4031 }
4032 | k_case expr_value opt_terms
4033 {
4034 $<val>$ = p->case_labels;
4035 p->case_labels = Qnil;
4036 }
4037 case_body
4038 k_end
4039 {
4040 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
4041 p->case_labels = $<val>4;
4042 /*%%%*/
4043 $$ = NEW_CASE($2, $5, &@$);
4044 fixpos($$, $2);
4045 /*% %*/
4046 /*% ripper: case!($2, $5) %*/
4047 }
4048 | k_case opt_terms
4049 {
4050 $<val>$ = p->case_labels;
4051 p->case_labels = 0;
4052 }
4053 case_body
4054 k_end
4055 {
4056 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
4057 p->case_labels = $<val>3;
4058 /*%%%*/
4059 $$ = NEW_CASE2($4, &@$);
4060 /*% %*/
4061 /*% ripper: case!(Qnil, $4) %*/
4062 }
4063 | k_case expr_value opt_terms
4064 p_case_body
4065 k_end
4066 {
4067 /*%%%*/
4068 $$ = NEW_CASE3($2, $4, &@$);
4069 /*% %*/
4070 /*% ripper: case!($2, $4) %*/
4071 }
4072 | k_for for_var keyword_in expr_value_do
4073 compstmt
4074 k_end
4075 {
4076 restore_block_exit(p, $1);
4077 /*%%%*/
4078 /*
4079 * for a, b, c in e
4080 * #=>
4081 * e.each{|*x| a, b, c = x}
4082 *
4083 * for a in e
4084 * #=>
4085 * e.each{|x| a, = x}
4086 */
4087 ID id = internal_id(p);
4088 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4089 rb_node_args_t *args;
4090 NODE *scope, *internal_var = NEW_DVAR(id, &@2);
4091 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4092 tbl->ids[0] = id; /* internal id */
4093
4094 switch (nd_type($2)) {
4095 case NODE_LASGN:
4096 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4097 set_nd_value(p, $2, internal_var);
4098 id = 0;
4099 m->nd_plen = 1;
4100 m->nd_next = $2;
4101 break;
4102 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4103 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
4104 break;
4105 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4106 m->nd_next = node_assign(p, (NODE *)NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
4107 }
4108 /* {|*internal_id| <m> = internal_id; ... } */
4109 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
4110 scope = NEW_SCOPE2(tbl, args, $5, &@$);
4111 $$ = NEW_FOR($4, scope, &@$);
4112 fixpos($$, $2);
4113 /*% %*/
4114 /*% ripper: for!($2, $4, $5) %*/
4115 }
4116 | k_class cpath superclass
4117 {
4118 begin_definition("class", &@k_class, &@cpath);
4119 }
4120 bodystmt
4121 k_end
4122 {
4123 /*%%%*/
4124 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$);
4125 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4126 set_line_body($bodystmt, @superclass.end_pos.lineno);
4127 nd_set_line($$, @superclass.end_pos.lineno);
4128 /*% %*/
4129 /*% ripper: class!($cpath, $superclass, $bodystmt) %*/
4130 local_pop(p);
4131 p->ctxt.in_class = $k_class.in_class;
4132 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4133 }
4134 | k_class tLSHFT expr_value
4135 {
4136 begin_definition("", &@k_class, &@tLSHFT);
4137 }
4138 term
4139 bodystmt
4140 k_end
4141 {
4142 /*%%%*/
4143 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$);
4144 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4145 set_line_body($bodystmt, nd_line($expr_value));
4146 fixpos($$, $expr_value);
4147 /*% %*/
4148 /*% ripper: sclass!($expr_value, $bodystmt) %*/
4149 local_pop(p);
4150 p->ctxt.in_def = $k_class.in_def;
4151 p->ctxt.in_class = $k_class.in_class;
4152 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4153 }
4154 | k_module cpath
4155 {
4156 begin_definition("module", &@k_module, &@cpath);
4157 }
4158 bodystmt
4159 k_end
4160 {
4161 /*%%%*/
4162 $$ = NEW_MODULE($cpath, $bodystmt, &@$);
4163 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4164 set_line_body($bodystmt, @cpath.end_pos.lineno);
4165 nd_set_line($$, @cpath.end_pos.lineno);
4166 /*% %*/
4167 /*% ripper: module!($cpath, $bodystmt) %*/
4168 local_pop(p);
4169 p->ctxt.in_class = $k_module.in_class;
4170 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4171 }
4172 | defn_head[head]
4173 f_arglist[args]
4174 {
4175 /*%%%*/
4176 push_end_expect_token_locations(p, &@head.beg_pos);
4177 /*% %*/
4178 }
4179 bodystmt
4180 k_end
4181 {
4182 restore_defun(p, $head);
4183 /*%%%*/
4184 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4185 ($$ = $head->nd_def)->nd_loc = @$;
4186 RNODE_DEFN($$)->nd_defn = $bodystmt;
4187 /*% %*/
4188 /*% ripper: def!($head->nd_mid, $args, $bodystmt) %*/
4189 local_pop(p);
4190 }
4191 | defs_head[head]
4192 f_arglist[args]
4193 {
4194 /*%%%*/
4195 push_end_expect_token_locations(p, &@head.beg_pos);
4196 /*% %*/
4197 }
4198 bodystmt
4199 k_end
4200 {
4201 restore_defun(p, $head);
4202 /*%%%*/
4203 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4204 ($$ = $head->nd_def)->nd_loc = @$;
4205 RNODE_DEFS($$)->nd_defn = $bodystmt;
4206 /*% %*/
4207 /*% ripper: defs!($head->nd_recv, $head->dot_or_colon, $head->nd_mid, $args, $bodystmt) %*/
4208 local_pop(p);
4209 }
4210 | keyword_break
4211 {
4212 $<node>$ = add_block_exit(p, NEW_BREAK(0, &@$));
4213 /*% ripper: break!(args_new!) %*/
4214 }
4215 | keyword_next
4216 {
4217 $<node>$ = add_block_exit(p, NEW_NEXT(0, &@$));
4218 /*% ripper: next!(args_new!) %*/
4219 }
4220 | keyword_redo
4221 {
4222 $<node>$ = add_block_exit(p, NEW_REDO(&@$));
4223 /*% ripper: redo! %*/
4224 }
4225 | keyword_retry
4226 {
4227 if (!p->ctxt.in_defined) {
4228 switch (p->ctxt.in_rescue) {
4229 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4230 case after_rescue: /* ok */ break;
4231 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4232 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4233 }
4234 }
4235 /*%%%*/
4236 $$ = NEW_RETRY(&@$);
4237 /*% %*/
4238 /*% ripper: retry! %*/
4239 }
4240 ;
4241
4242primary_value : primary
4243 {
4244 value_expr($1);
4245 $$ = $1;
4246 }
4247 ;
4248
4249k_begin : keyword_begin
4250 {
4251 token_info_push(p, "begin", &@$);
4252 /*%%%*/
4253 push_end_expect_token_locations(p, &@1.beg_pos);
4254 /*% %*/
4255 }
4256 ;
4257
4258k_if : keyword_if
4259 {
4260 WARN_EOL("if");
4261 token_info_push(p, "if", &@$);
4262 if (p->token_info && p->token_info->nonspc &&
4263 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4264 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4265 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4266 beg += rb_strlen_lit("else");
4267 while (beg < tok && ISSPACE(*beg)) beg++;
4268 if (beg == tok) {
4269 p->token_info->nonspc = 0;
4270 }
4271 }
4272 /*%%%*/
4273 push_end_expect_token_locations(p, &@1.beg_pos);
4274 /*% %*/
4275 }
4276 ;
4277
4278k_unless : keyword_unless
4279 {
4280 token_info_push(p, "unless", &@$);
4281 /*%%%*/
4282 push_end_expect_token_locations(p, &@1.beg_pos);
4283 /*% %*/
4284 }
4285 ;
4286
4287k_while : keyword_while allow_exits
4288 {
4289 $$ = $allow_exits;
4290 token_info_push(p, "while", &@$);
4291 /*%%%*/
4292 push_end_expect_token_locations(p, &@1.beg_pos);
4293 /*% %*/
4294 }
4295 ;
4296
4297k_until : keyword_until allow_exits
4298 {
4299 $$ = $allow_exits;
4300 token_info_push(p, "until", &@$);
4301 /*%%%*/
4302 push_end_expect_token_locations(p, &@1.beg_pos);
4303 /*% %*/
4304 }
4305 ;
4306
4307k_case : keyword_case
4308 {
4309 token_info_push(p, "case", &@$);
4310 /*%%%*/
4311 push_end_expect_token_locations(p, &@1.beg_pos);
4312 /*% %*/
4313 }
4314 ;
4315
4316k_for : keyword_for allow_exits
4317 {
4318 $$ = $allow_exits;
4319 token_info_push(p, "for", &@$);
4320 /*%%%*/
4321 push_end_expect_token_locations(p, &@1.beg_pos);
4322 /*% %*/
4323 }
4324 ;
4325
4326k_class : keyword_class
4327 {
4328 token_info_push(p, "class", &@$);
4329 $$ = p->ctxt;
4330 p->ctxt.in_rescue = before_rescue;
4331 /*%%%*/
4332 push_end_expect_token_locations(p, &@1.beg_pos);
4333 /*% %*/
4334 }
4335 ;
4336
4337k_module : keyword_module
4338 {
4339 token_info_push(p, "module", &@$);
4340 $$ = p->ctxt;
4341 p->ctxt.in_rescue = before_rescue;
4342 /*%%%*/
4343 push_end_expect_token_locations(p, &@1.beg_pos);
4344 /*% %*/
4345 }
4346 ;
4347
4348k_def : keyword_def
4349 {
4350 token_info_push(p, "def", &@$);
4351 $$ = NEW_DEF_TEMP(&@$);
4352 p->ctxt.in_argdef = 1;
4353 }
4354 ;
4355
4356k_do : keyword_do
4357 {
4358 token_info_push(p, "do", &@$);
4359 /*%%%*/
4360 push_end_expect_token_locations(p, &@1.beg_pos);
4361 /*% %*/
4362 }
4363 ;
4364
4365k_do_block : keyword_do_block
4366 {
4367 token_info_push(p, "do", &@$);
4368 /*%%%*/
4369 push_end_expect_token_locations(p, &@1.beg_pos);
4370 /*% %*/
4371 }
4372 ;
4373
4374k_rescue : keyword_rescue
4375 {
4376 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4377 $$ = p->ctxt;
4378 p->ctxt.in_rescue = after_rescue;
4379 }
4380 ;
4381
4382k_ensure : keyword_ensure
4383 {
4384 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4385 $$ = p->ctxt;
4386 }
4387 ;
4388
4389k_when : keyword_when
4390 {
4391 token_info_warn(p, "when", p->token_info, 0, &@$);
4392 }
4393 ;
4394
4395k_else : keyword_else
4396 {
4397 token_info *ptinfo_beg = p->token_info;
4398 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4399 token_info_warn(p, "else", p->token_info, same, &@$);
4400 if (same) {
4401 token_info e;
4402 e.next = ptinfo_beg->next;
4403 e.token = "else";
4404 token_info_setup(&e, p->lex.pbeg, &@$);
4405 if (!e.nonspc) *ptinfo_beg = e;
4406 }
4407 }
4408 ;
4409
4410k_elsif : keyword_elsif
4411 {
4412 WARN_EOL("elsif");
4413 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4414 }
4415 ;
4416
4417k_end : keyword_end
4418 {
4419 token_info_pop(p, "end", &@$);
4420 /*%%%*/
4421 pop_end_expect_token_locations(p);
4422 /*% %*/
4423 }
4424 | tDUMNY_END
4425 {
4426 compile_error(p, "syntax error, unexpected end-of-input");
4427 }
4428 ;
4429
4430k_return : keyword_return
4431 {
4432 if (p->ctxt.in_class && !p->ctxt.in_def && !dyna_in_block(p))
4433 yyerror1(&@1, "Invalid return in class/module body");
4434 }
4435 ;
4436
4437k_yield : keyword_yield
4438 {
4439 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4440 yyerror1(&@1, "Invalid yield");
4441 }
4442 ;
4443
4444then : term
4445 | keyword_then
4446 | term keyword_then
4447 ;
4448
4449do : term
4450 | keyword_do_cond
4451 ;
4452
4453if_tail : opt_else
4454 | k_elsif expr_value then
4455 compstmt
4456 if_tail
4457 {
4458 /*%%%*/
4459 $$ = new_if(p, $2, $4, $5, &@$);
4460 fixpos($$, $2);
4461 /*% %*/
4462 /*% ripper: elsif!($2, $4, $5) %*/
4463 }
4464 ;
4465
4466opt_else : none
4467 | k_else compstmt
4468 {
4469 /*%%%*/
4470 $$ = $2;
4471 /*% %*/
4472 /*% ripper: else!($2) %*/
4473 }
4474 ;
4475
4476for_var : lhs
4477 | mlhs
4478 ;
4479
4480f_marg : f_norm_arg
4481 {
4482 /*%%%*/
4483 $$ = assignable(p, $1, 0, &@$);
4484 mark_lvar_used(p, $$);
4485 /*% %*/
4486 /*% ripper: assignable(p, $1) %*/
4487 }
4488 | tLPAREN f_margs rparen
4489 {
4490 /*%%%*/
4491 $$ = (NODE *)$2;
4492 /*% %*/
4493 /*% ripper: mlhs_paren!($2) %*/
4494 }
4495 ;
4496
4497f_marg_list : f_marg
4498 {
4499 /*%%%*/
4500 $$ = NEW_LIST($1, &@$);
4501 /*% %*/
4502 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
4503 }
4504 | f_marg_list ',' f_marg
4505 {
4506 /*%%%*/
4507 $$ = list_append(p, $1, $3);
4508 /*% %*/
4509 /*% ripper: mlhs_add!($1, $3) %*/
4510 }
4511 ;
4512
4513f_margs : f_marg_list
4514 {
4515 /*%%%*/
4516 $$ = NEW_MASGN($1, 0, &@$);
4517 /*% %*/
4518 /*% ripper: $1 %*/
4519 }
4520 | f_marg_list ',' f_rest_marg
4521 {
4522 /*%%%*/
4523 $$ = NEW_MASGN($1, $3, &@$);
4524 /*% %*/
4525 /*% ripper: mlhs_add_star!($1, $3) %*/
4526 }
4527 | f_marg_list ',' f_rest_marg ',' f_marg_list
4528 {
4529 /*%%%*/
4530 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4531 /*% %*/
4532 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
4533 }
4534 | f_rest_marg
4535 {
4536 /*%%%*/
4537 $$ = NEW_MASGN(0, $1, &@$);
4538 /*% %*/
4539 /*% ripper: mlhs_add_star!(mlhs_new!, $1) %*/
4540 }
4541 | f_rest_marg ',' f_marg_list
4542 {
4543 /*%%%*/
4544 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4545 /*% %*/
4546 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $1), $3) %*/
4547 }
4548 ;
4549
4550f_rest_marg : tSTAR f_norm_arg
4551 {
4552 /*%%%*/
4553 $$ = assignable(p, $2, 0, &@$);
4554 mark_lvar_used(p, $$);
4555 /*% %*/
4556 /*% ripper: assignable(p, $2) %*/
4557 }
4558 | tSTAR
4559 {
4560 /*%%%*/
4561 $$ = NODE_SPECIAL_NO_NAME_REST;
4562 /*% %*/
4563 /*% ripper: Qnil %*/
4564 }
4565 ;
4566
4567f_any_kwrest : f_kwrest
4568 | f_no_kwarg {$$ = ID2VAL(idNil);}
4569 ;
4570
4571f_eq : {p->ctxt.in_argdef = 0;} '=';
4572
4573block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
4574 {
4575 $$ = new_args_tail(p, $1, $3, $4, &@3);
4576 }
4577 | f_block_kwarg opt_f_block_arg
4578 {
4579 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
4580 }
4581 | f_any_kwrest opt_f_block_arg
4582 {
4583 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
4584 }
4585 | f_block_arg
4586 {
4587 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
4588 }
4589 ;
4590
4591opt_block_args_tail : ',' block_args_tail
4592 {
4593 $$ = $2;
4594 }
4595 | /* none */
4596 {
4597 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4598 }
4599 ;
4600
4601excessed_comma : ','
4602 {
4603 /* magic number for rest_id in iseq_set_arguments() */
4604 /*%%%*/
4605 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4606 /*% %*/
4607 /*% ripper: excessed_comma! %*/
4608 }
4609 ;
4610
4611block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
4612 {
4613 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
4614 }
4615 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
4616 {
4617 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4618 }
4619 | f_arg ',' f_block_optarg opt_block_args_tail
4620 {
4621 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
4622 }
4623 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
4624 {
4625 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4626 }
4627 | f_arg ',' f_rest_arg opt_block_args_tail
4628 {
4629 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4630 }
4631 | f_arg excessed_comma
4632 {
4633 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@2);
4634 $$ = new_args(p, $1, Qnone, $2, Qnone, $$, &@$);
4635 }
4636 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
4637 {
4638 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4639 }
4640 | f_arg opt_block_args_tail
4641 {
4642 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4643 }
4644 | f_block_optarg ',' f_rest_arg opt_block_args_tail
4645 {
4646 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4647 }
4648 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
4649 {
4650 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4651 }
4652 | f_block_optarg opt_block_args_tail
4653 {
4654 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4655 }
4656 | f_block_optarg ',' f_arg opt_block_args_tail
4657 {
4658 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4659 }
4660 | f_rest_arg opt_block_args_tail
4661 {
4662 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4663 }
4664 | f_rest_arg ',' f_arg opt_block_args_tail
4665 {
4666 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4667 }
4668 | block_args_tail
4669 {
4670 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4671 }
4672 ;
4673
4674opt_block_param : none
4675 | block_param_def
4676 {
4677 p->command_start = TRUE;
4678 }
4679 ;
4680
4681block_param_def : '|' opt_bv_decl '|'
4682 {
4683 p->cur_arg = 0;
4684 p->max_numparam = ORDINAL_PARAM;
4685 p->ctxt.in_argdef = 0;
4686 /*%%%*/
4687 $$ = 0;
4688 /*% %*/
4689 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
4690 /*% ripper: block_var!($$, $2) %*/
4691 }
4692 | '|' block_param opt_bv_decl '|'
4693 {
4694 p->cur_arg = 0;
4695 p->max_numparam = ORDINAL_PARAM;
4696 p->ctxt.in_argdef = 0;
4697 /*%%%*/
4698 $$ = $2;
4699 /*% %*/
4700 /*% ripper: block_var!($2, $3) %*/
4701 }
4702 ;
4703
4704
4705opt_bv_decl : opt_nl
4706 {
4707 $$ = 0;
4708 }
4709 | opt_nl ';' bv_decls opt_nl
4710 {
4711 /*%%%*/
4712 $$ = 0;
4713 /*% %*/
4714 /*% ripper: $3 %*/
4715 }
4716 ;
4717
4718bv_decls : bvar
4719 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
4720 | bv_decls ',' bvar
4721 /*% ripper[brace]: rb_ary_push($1, get_value($3)) %*/
4722 ;
4723
4724bvar : tIDENTIFIER
4725 {
4726 new_bv(p, get_id($1));
4727 /*% ripper: get_value($1) %*/
4728 }
4729 | f_bad_arg
4730 {
4731 $$ = 0;
4732 }
4733 ;
4734
4735max_numparam : {
4736 $$ = p->max_numparam;
4737 p->max_numparam = 0;
4738 }
4739 ;
4740
4741numparam : {
4742 $$ = numparam_push(p);
4743 }
4744 ;
4745
4746lambda : tLAMBDA[dyna]
4747 {
4748 token_info_push(p, "->", &@1);
4749 $<vars>dyna = dyna_push(p);
4750 $<num>$ = p->lex.lpar_beg;
4751 p->lex.lpar_beg = p->lex.paren_nest;
4752 }[lpar]
4753 max_numparam numparam allow_exits
4754 f_larglist[args]
4755 {
4756 CMDARG_PUSH(0);
4757 }
4758 lambda_body[body]
4759 {
4760 int max_numparam = p->max_numparam;
4761 p->lex.lpar_beg = $<num>lpar;
4762 p->max_numparam = $max_numparam;
4763 restore_block_exit(p, $allow_exits);
4764 CMDARG_POP();
4765 $args = args_with_numbered(p, $args, max_numparam);
4766 /*%%%*/
4767 {
4768 YYLTYPE loc = code_loc_gen(&@args, &@body);
4769 $$ = NEW_LAMBDA($args, $body, &loc);
4770 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
4771 nd_set_line($$, @args.end_pos.lineno);
4772 nd_set_first_loc($$, @1.beg_pos);
4773 }
4774 /*% %*/
4775 /*% ripper: lambda!($args, $body) %*/
4776 numparam_pop(p, $numparam);
4777 dyna_pop(p, $<vars>dyna);
4778 }
4779 ;
4780
4781f_larglist : '(' f_args opt_bv_decl ')'
4782 {
4783 p->ctxt.in_argdef = 0;
4784 /*%%%*/
4785 $$ = $2;
4786 p->max_numparam = ORDINAL_PARAM;
4787 /*% %*/
4788 /*% ripper: paren!($2) %*/
4789 }
4790 | f_args
4791 {
4792 p->ctxt.in_argdef = 0;
4793 /*%%%*/
4794 if (!args_info_empty_p(&$1->nd_ainfo))
4795 p->max_numparam = ORDINAL_PARAM;
4796 /*% %*/
4797 $$ = $1;
4798 }
4799 ;
4800
4801lambda_body : tLAMBEG compstmt '}'
4802 {
4803 token_info_pop(p, "}", &@3);
4804 $$ = $2;
4805 }
4806 | keyword_do_LAMBDA
4807 {
4808 /*%%%*/
4809 push_end_expect_token_locations(p, &@1.beg_pos);
4810 /*% %*/
4811 }
4812 bodystmt k_end
4813 {
4814 $$ = $3;
4815 }
4816 ;
4817
4818do_block : k_do_block do_body k_end
4819 {
4820 $$ = $2;
4821 /*%%%*/
4822 set_embraced_location($$, &@1, &@3);
4823 /*% %*/
4824 }
4825 ;
4826
4827block_call : command do_block
4828 {
4829 /*%%%*/
4830 if (nd_type_p($1, NODE_YIELD)) {
4831 compile_error(p, "block given to yield");
4832 }
4833 else {
4834 block_dup_check(p, get_nd_args(p, $1), $2);
4835 }
4836 $$ = method_add_block(p, $1, $2, &@$);
4837 fixpos($$, $1);
4838 /*% %*/
4839 /*% ripper: method_add_block!($1, $2) %*/
4840 }
4841 | block_call call_op2 operation2 opt_paren_args
4842 {
4843 /*%%%*/
4844 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4845 /*% %*/
4846 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4847 }
4848 | block_call call_op2 operation2 opt_paren_args brace_block
4849 {
4850 /*%%%*/
4851 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4852 /*% %*/
4853 /*% ripper: opt_event(:method_add_block!, command_call!($1, $2, $3, $4), $5) %*/
4854 }
4855 | block_call call_op2 operation2 command_args do_block
4856 {
4857 /*%%%*/
4858 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4859 /*% %*/
4860 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
4861 }
4862 ;
4863
4864method_call : fcall paren_args
4865 {
4866 /*%%%*/
4867 $1->nd_args = $2;
4868 $$ = (NODE *)$1;
4869 nd_set_last_loc($1, @2.end_pos);
4870 /*% %*/
4871 /*% ripper: method_add_arg!(fcall!($1), $2) %*/
4872 }
4873 | primary_value call_op operation2 opt_paren_args
4874 {
4875 /*%%%*/
4876 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4877 nd_set_line($$, @3.end_pos.lineno);
4878 /*% %*/
4879 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4880 }
4881 | primary_value tCOLON2 operation2 paren_args
4882 {
4883 /*%%%*/
4884 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
4885 nd_set_line($$, @3.end_pos.lineno);
4886 /*% %*/
4887 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
4888 }
4889 | primary_value tCOLON2 operation3
4890 {
4891 /*%%%*/
4892 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
4893 /*% %*/
4894 /*% ripper: call!($1, $2, $3) %*/
4895 }
4896 | primary_value call_op paren_args
4897 {
4898 /*%%%*/
4899 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
4900 nd_set_line($$, @2.end_pos.lineno);
4901 /*% %*/
4902 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
4903 }
4904 | primary_value tCOLON2 paren_args
4905 {
4906 /*%%%*/
4907 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
4908 nd_set_line($$, @2.end_pos.lineno);
4909 /*% %*/
4910 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
4911 }
4912 | keyword_super paren_args
4913 {
4914 /*%%%*/
4915 $$ = NEW_SUPER($2, &@$);
4916 /*% %*/
4917 /*% ripper: super!($2) %*/
4918 }
4919 | keyword_super
4920 {
4921 /*%%%*/
4922 $$ = NEW_ZSUPER(&@$);
4923 /*% %*/
4924 /*% ripper: zsuper! %*/
4925 }
4926 | primary_value '[' opt_call_args rbracket
4927 {
4928 /*%%%*/
4929 $$ = NEW_CALL($1, tAREF, $3, &@$);
4930 fixpos($$, $1);
4931 /*% %*/
4932 /*% ripper: aref!($1, $3) %*/
4933 }
4934 ;
4935
4936brace_block : '{' brace_body '}'
4937 {
4938 $$ = $2;
4939 /*%%%*/
4940 set_embraced_location($$, &@1, &@3);
4941 /*% %*/
4942 }
4943 | k_do do_body k_end
4944 {
4945 $$ = $2;
4946 /*%%%*/
4947 set_embraced_location($$, &@1, &@3);
4948 /*% %*/
4949 }
4950 ;
4951
4952brace_body : {$<vars>$ = dyna_push(p);}[dyna]
4953 max_numparam numparam allow_exits
4954 opt_block_param[args] compstmt
4955 {
4956 int max_numparam = p->max_numparam;
4957 p->max_numparam = $max_numparam;
4958 $args = args_with_numbered(p, $args, max_numparam);
4959 /*%%%*/
4960 $$ = NEW_ITER($args, $compstmt, &@$);
4961 /*% %*/
4962 /*% ripper: brace_block!($args, $compstmt) %*/
4963 restore_block_exit(p, $allow_exits);
4964 numparam_pop(p, $numparam);
4965 dyna_pop(p, $<vars>dyna);
4966 }
4967 ;
4968
4969do_body : {
4970 $<vars>$ = dyna_push(p);
4971 CMDARG_PUSH(0);
4972 }[dyna]
4973 max_numparam numparam allow_exits
4974 opt_block_param[args] bodystmt
4975 {
4976 int max_numparam = p->max_numparam;
4977 p->max_numparam = $max_numparam;
4978 $args = args_with_numbered(p, $args, max_numparam);
4979 /*%%%*/
4980 $$ = NEW_ITER($args, $bodystmt, &@$);
4981 /*% %*/
4982 /*% ripper: do_block!($args, $bodystmt) %*/
4983 CMDARG_POP();
4984 restore_block_exit(p, $allow_exits);
4985 numparam_pop(p, $numparam);
4986 dyna_pop(p, $<vars>dyna);
4987 }
4988 ;
4989
4990case_args : arg_value
4991 {
4992 /*%%%*/
4993 check_literal_when(p, $1, &@1);
4994 $$ = NEW_LIST($1, &@$);
4995 /*% %*/
4996 /*% ripper: args_add!(args_new!, $1) %*/
4997 }
4998 | tSTAR arg_value
4999 {
5000 /*%%%*/
5001 $$ = NEW_SPLAT($2, &@$);
5002 /*% %*/
5003 /*% ripper: args_add_star!(args_new!, $2) %*/
5004 }
5005 | case_args ',' arg_value
5006 {
5007 /*%%%*/
5008 check_literal_when(p, $3, &@3);
5009 $$ = last_arg_append(p, $1, $3, &@$);
5010 /*% %*/
5011 /*% ripper: args_add!($1, $3) %*/
5012 }
5013 | case_args ',' tSTAR arg_value
5014 {
5015 /*%%%*/
5016 $$ = rest_arg_append(p, $1, $4, &@$);
5017 /*% %*/
5018 /*% ripper: args_add_star!($1, $4) %*/
5019 }
5020 ;
5021
5022case_body : k_when case_args then
5023 compstmt
5024 cases
5025 {
5026 /*%%%*/
5027 $$ = NEW_WHEN($2, $4, $5, &@$);
5028 fixpos($$, $2);
5029 /*% %*/
5030 /*% ripper: when!($2, $4, $5) %*/
5031 }
5032 ;
5033
5034cases : opt_else
5035 | case_body
5036 ;
5037
5038p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5039p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5040
5041p_in_kwarg : {
5042 $$ = p->ctxt;
5043 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5044 p->command_start = FALSE;
5045 p->ctxt.in_kwarg = 1;
5046 }
5047 ;
5048
5049p_case_body : keyword_in
5050 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5051 p_top_expr[expr] then
5052 {
5053 pop_pktbl(p, $p_pktbl);
5054 pop_pvtbl(p, $p_pvtbl);
5055 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5056 }
5057 compstmt
5058 p_cases[cases]
5059 {
5060 /*%%%*/
5061 $$ = NEW_IN($expr, $compstmt, $cases, &@$);
5062 /*% %*/
5063 /*% ripper: in!($expr, $compstmt, $cases) %*/
5064 }
5065 ;
5066
5067p_cases : opt_else
5068 | p_case_body
5069 ;
5070
5071p_top_expr : p_top_expr_body
5072 | p_top_expr_body modifier_if expr_value
5073 {
5074 /*%%%*/
5075 $$ = new_if(p, $3, $1, 0, &@$);
5076 fixpos($$, $3);
5077 /*% %*/
5078 /*% ripper: if_mod!($3, $1) %*/
5079 }
5080 | p_top_expr_body modifier_unless expr_value
5081 {
5082 /*%%%*/
5083 $$ = new_unless(p, $3, $1, 0, &@$);
5084 fixpos($$, $3);
5085 /*% %*/
5086 /*% ripper: unless_mod!($3, $1) %*/
5087 }
5088 ;
5089
5090p_top_expr_body : p_expr
5091 | p_expr ','
5092 {
5093 $$ = new_array_pattern_tail(p, Qnone, 1, Qnone, Qnone, &@$);
5094 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
5095 }
5096 | p_expr ',' p_args
5097 {
5098 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
5099 /*%%%*/
5100 nd_set_first_loc($$, @1.beg_pos);
5101 /*%
5102 %*/
5103 }
5104 | p_find
5105 {
5106 $$ = new_find_pattern(p, Qnone, $1, &@$);
5107 }
5108 | p_args_tail
5109 {
5110 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
5111 }
5112 | p_kwargs
5113 {
5114 $$ = new_hash_pattern(p, Qnone, $1, &@$);
5115 }
5116 ;
5117
5118p_expr : p_as
5119 ;
5120
5121p_as : p_expr tASSOC p_variable
5122 {
5123 /*%%%*/
5124 NODE *n = NEW_LIST($1, &@$);
5125 n = list_append(p, n, $3);
5126 $$ = new_hash(p, n, &@$);
5127 /*% %*/
5128 /*% ripper: binary!($1, STATIC_ID2SYM((id_assoc)), $3) %*/
5129 }
5130 | p_alt
5131 ;
5132
5133p_alt : p_alt '|' p_expr_basic
5134 {
5135 /*%%%*/
5136 $$ = NEW_OR($1, $3, &@$);
5137 /*% %*/
5138 /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
5139 }
5140 | p_expr_basic
5141 ;
5142
5143p_lparen : '(' p_pktbl { $$ = $2;};
5144p_lbracket : '[' p_pktbl { $$ = $2;};
5145
5146p_expr_basic : p_value
5147 | p_variable
5148 | p_const p_lparen[p_pktbl] p_args rparen
5149 {
5150 pop_pktbl(p, $p_pktbl);
5151 $$ = new_array_pattern(p, $p_const, Qnone, $p_args, &@$);
5152 /*%%%*/
5153 nd_set_first_loc($$, @p_const.beg_pos);
5154 /*%
5155 %*/
5156 }
5157 | p_const p_lparen[p_pktbl] p_find rparen
5158 {
5159 pop_pktbl(p, $p_pktbl);
5160 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5161 /*%%%*/
5162 nd_set_first_loc($$, @p_const.beg_pos);
5163 /*%
5164 %*/
5165 }
5166 | p_const p_lparen[p_pktbl] p_kwargs rparen
5167 {
5168 pop_pktbl(p, $p_pktbl);
5169 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5170 /*%%%*/
5171 nd_set_first_loc($$, @p_const.beg_pos);
5172 /*%
5173 %*/
5174 }
5175 | p_const '(' rparen
5176 {
5177 $$ = new_array_pattern_tail(p, Qnone, 0, Qnone, Qnone, &@$);
5178 $$ = new_array_pattern(p, $p_const, Qnone, $$, &@$);
5179 }
5180 | p_const p_lbracket[p_pktbl] p_args rbracket
5181 {
5182 pop_pktbl(p, $p_pktbl);
5183 $$ = new_array_pattern(p, $p_const, Qnone, $p_args, &@$);
5184 /*%%%*/
5185 nd_set_first_loc($$, @p_const.beg_pos);
5186 /*%
5187 %*/
5188 }
5189 | p_const p_lbracket[p_pktbl] p_find rbracket
5190 {
5191 pop_pktbl(p, $p_pktbl);
5192 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5193 /*%%%*/
5194 nd_set_first_loc($$, @p_const.beg_pos);
5195 /*%
5196 %*/
5197 }
5198 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5199 {
5200 pop_pktbl(p, $p_pktbl);
5201 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5202 /*%%%*/
5203 nd_set_first_loc($$, @p_const.beg_pos);
5204 /*%
5205 %*/
5206 }
5207 | p_const '[' rbracket
5208 {
5209 $$ = new_array_pattern_tail(p, Qnone, 0, Qnone, Qnone, &@$);
5210 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
5211 }
5212 | tLBRACK p_args rbracket
5213 {
5214 $$ = new_array_pattern(p, Qnone, Qnone, $p_args, &@$);
5215 }
5216 | tLBRACK p_find rbracket
5217 {
5218 $$ = new_find_pattern(p, Qnone, $p_find, &@$);
5219 }
5220 | tLBRACK rbracket
5221 {
5222 $$ = new_array_pattern_tail(p, Qnone, 0, Qnone, Qnone, &@$);
5223 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
5224 }
5225 | tLBRACE p_pktbl lex_ctxt[ctxt]
5226 {
5227 p->ctxt.in_kwarg = 0;
5228 }
5229 p_kwargs rbrace
5230 {
5231 pop_pktbl(p, $p_pktbl);
5232 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5233 $$ = new_hash_pattern(p, Qnone, $p_kwargs, &@$);
5234 }
5235 | tLBRACE rbrace
5236 {
5237 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
5238 $$ = new_hash_pattern(p, Qnone, $$, &@$);
5239 }
5240 | tLPAREN p_pktbl p_expr rparen
5241 {
5242 pop_pktbl(p, $p_pktbl);
5243 $$ = $p_expr;
5244 }
5245 ;
5246
5247p_args : p_expr
5248 {
5249 /*%%%*/
5250 NODE *pre_args = NEW_LIST($1, &@$);
5251 $$ = new_array_pattern_tail(p, pre_args, 0, Qnone, Qnone, &@$);
5252 /*%
5253 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, Qnone, Qnone, &@$);
5254 %*/
5255 }
5256 | p_args_head
5257 {
5258 $$ = new_array_pattern_tail(p, $1, 1, Qnone, Qnone, &@$);
5259 }
5260 | p_args_head p_arg
5261 {
5262 /*%%%*/
5263 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, Qnone, Qnone, &@$);
5264 /*%
5265 VALUE pre_args = rb_ary_concat($1, get_value($2));
5266 $$ = new_array_pattern_tail(p, pre_args, 0, Qnone, Qnone, &@$);
5267 %*/
5268 }
5269 | p_args_head p_rest
5270 {
5271 $$ = new_array_pattern_tail(p, $1, 1, $2, Qnone, &@$);
5272 }
5273 | p_args_head p_rest ',' p_args_post
5274 {
5275 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5276 }
5277 | p_args_tail
5278 ;
5279
5280p_args_head : p_arg ','
5281 {
5282 $$ = $1;
5283 }
5284 | p_args_head p_arg ','
5285 {
5286 /*%%%*/
5287 $$ = list_concat($1, $2);
5288 /*% %*/
5289 /*% ripper: rb_ary_concat($1, get_value($2)) %*/
5290 }
5291 ;
5292
5293p_args_tail : p_rest
5294 {
5295 $$ = new_array_pattern_tail(p, Qnone, 1, $1, Qnone, &@$);
5296 }
5297 | p_rest ',' p_args_post
5298 {
5299 $$ = new_array_pattern_tail(p, Qnone, 1, $1, $3, &@$);
5300 }
5301 ;
5302
5303p_find : p_rest ',' p_args_post ',' p_rest
5304 {
5305 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5306 }
5307 ;
5308
5309
5310p_rest : tSTAR tIDENTIFIER
5311 {
5312 /*%%%*/
5313 error_duplicate_pattern_variable(p, $2, &@2);
5314 $$ = assignable(p, $2, 0, &@$);
5315 /*% %*/
5316 /*% ripper: assignable(p, var_field(p, $2)) %*/
5317 }
5318 | tSTAR
5319 {
5320 /*%%%*/
5321 $$ = 0;
5322 /*% %*/
5323 /*% ripper: var_field(p, Qnil) %*/
5324 }
5325 ;
5326
5327p_args_post : p_arg
5328 | p_args_post ',' p_arg
5329 {
5330 /*%%%*/
5331 $$ = list_concat($1, $3);
5332 /*% %*/
5333 /*% ripper: rb_ary_concat($1, get_value($3)) %*/
5334 }
5335 ;
5336
5337p_arg : p_expr
5338 {
5339 /*%%%*/
5340 $$ = NEW_LIST($1, &@$);
5341 /*% %*/
5342 /*% ripper: rb_ary_new_from_args(1, get_value($1)) %*/
5343 }
5344 ;
5345
5346p_kwargs : p_kwarg ',' p_any_kwrest
5347 {
5348 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5349 }
5350 | p_kwarg
5351 {
5352 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5353 }
5354 | p_kwarg ','
5355 {
5356 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5357 }
5358 | p_any_kwrest
5359 {
5360 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
5361 }
5362 ;
5363
5364p_kwarg : p_kw
5365 /*% ripper[brace]: rb_ary_new_from_args(1, $1) %*/
5366 | p_kwarg ',' p_kw
5367 {
5368 /*%%%*/
5369 $$ = list_concat($1, $3);
5370 /*% %*/
5371 /*% ripper: rb_ary_push($1, $3) %*/
5372 }
5373 ;
5374
5375p_kw : p_kw_label p_expr
5376 {
5377 error_duplicate_pattern_key(p, get_id($1), &@1);
5378 /*%%%*/
5379 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5380 /*% %*/
5381 /*% ripper: rb_ary_new_from_args(2, get_value($1), get_value($2)) %*/
5382 }
5383 | p_kw_label
5384 {
5385 error_duplicate_pattern_key(p, get_id($1), &@1);
5386 if ($1 && !is_local_id(get_id($1))) {
5387 yyerror1(&@1, "key must be valid as local variables");
5388 }
5389 error_duplicate_pattern_variable(p, get_id($1), &@1);
5390 /*%%%*/
5391 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
5392 /*% %*/
5393 /*% ripper: rb_ary_new_from_args(2, get_value(assignable(p, $1)), Qnil) %*/
5394 }
5395 ;
5396
5397p_kw_label : tLABEL
5398 | tSTRING_BEG string_contents tLABEL_END
5399 {
5400 YYLTYPE loc = code_loc_gen(&@1, &@3);
5401 /*%%%*/
5402 if (!$2 || nd_type_p($2, NODE_STR)) {
5403 NODE *node = dsym_node(p, $2, &loc);
5404 $$ = SYM2ID(RNODE_LIT(node)->nd_lit);
5405 }
5406 /*%
5407 if (ripper_is_node_yylval(p, $2) && RNODE_RIPPER($2)->nd_cval) {
5408 VALUE label = RNODE_RIPPER($2)->nd_cval;
5409 VALUE rval = RNODE_RIPPER($2)->nd_rval;
5410 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
5411 RNODE($$)->nd_loc = loc;
5412 }
5413 %*/
5414 else {
5415 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5416 $$ = 0;
5417 }
5418 }
5419 ;
5420
5421p_kwrest : kwrest_mark tIDENTIFIER
5422 {
5423 $$ = $2;
5424 }
5425 | kwrest_mark
5426 {
5427 $$ = 0;
5428 }
5429 ;
5430
5431p_kwnorest : kwrest_mark keyword_nil
5432 {
5433 $$ = 0;
5434 }
5435 ;
5436
5437p_any_kwrest : p_kwrest
5438 | p_kwnorest {$$ = ID2VAL(idNil);}
5439 ;
5440
5441p_value : p_primitive
5442 | p_primitive tDOT2 p_primitive
5443 {
5444 /*%%%*/
5445 value_expr($1);
5446 value_expr($3);
5447 $$ = NEW_DOT2($1, $3, &@$);
5448 /*% %*/
5449 /*% ripper: dot2!($1, $3) %*/
5450 }
5451 | p_primitive tDOT3 p_primitive
5452 {
5453 /*%%%*/
5454 value_expr($1);
5455 value_expr($3);
5456 $$ = NEW_DOT3($1, $3, &@$);
5457 /*% %*/
5458 /*% ripper: dot3!($1, $3) %*/
5459 }
5460 | p_primitive tDOT2
5461 {
5462 /*%%%*/
5463 value_expr($1);
5464 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
5465 /*% %*/
5466 /*% ripper: dot2!($1, Qnil) %*/
5467 }
5468 | p_primitive tDOT3
5469 {
5470 /*%%%*/
5471 value_expr($1);
5472 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
5473 /*% %*/
5474 /*% ripper: dot3!($1, Qnil) %*/
5475 }
5476 | p_var_ref
5477 | p_expr_ref
5478 | p_const
5479 | tBDOT2 p_primitive
5480 {
5481 /*%%%*/
5482 value_expr($2);
5483 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
5484 /*% %*/
5485 /*% ripper: dot2!(Qnil, $2) %*/
5486 }
5487 | tBDOT3 p_primitive
5488 {
5489 /*%%%*/
5490 value_expr($2);
5491 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
5492 /*% %*/
5493 /*% ripper: dot3!(Qnil, $2) %*/
5494 }
5495 ;
5496
5497p_primitive : literal
5498 | strings
5499 | xstring
5500 | regexp
5501 | words
5502 | qwords
5503 | symbols
5504 | qsymbols
5505 | keyword_variable
5506 {
5507 /*%%%*/
5508 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5509 /*% %*/
5510 /*% ripper: var_ref!($1) %*/
5511 }
5512 | lambda
5513 ;
5514
5515p_variable : tIDENTIFIER
5516 {
5517 /*%%%*/
5518 error_duplicate_pattern_variable(p, $1, &@1);
5519 $$ = assignable(p, $1, 0, &@$);
5520 /*% %*/
5521 /*% ripper: assignable(p, var_field(p, $1)) %*/
5522 }
5523 ;
5524
5525p_var_ref : '^' tIDENTIFIER
5526 {
5527 /*%%%*/
5528 NODE *n = gettable(p, $2, &@$);
5529 if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5530 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5531 }
5532 $$ = n;
5533 /*% %*/
5534 /*% ripper: var_ref!($2) %*/
5535 }
5536 | '^' nonlocal_var
5537 {
5538 /*%%%*/
5539 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_BEGIN(0, &@$);
5540 /*% %*/
5541 /*% ripper: var_ref!($2) %*/
5542 }
5543 ;
5544
5545p_expr_ref : '^' tLPAREN expr_value rparen
5546 {
5547 /*%%%*/
5548 $$ = NEW_BEGIN($3, &@$);
5549 /*% %*/
5550 /*% ripper: begin!($3) %*/
5551 }
5552 ;
5553
5554p_const : tCOLON3 cname
5555 {
5556 /*%%%*/
5557 $$ = NEW_COLON3($2, &@$);
5558 /*% %*/
5559 /*% ripper: top_const_ref!($2) %*/
5560 }
5561 | p_const tCOLON2 cname
5562 {
5563 /*%%%*/
5564 $$ = NEW_COLON2($1, $3, &@$);
5565 /*% %*/
5566 /*% ripper: const_path_ref!($1, $3) %*/
5567 }
5568 | tCONSTANT
5569 {
5570 /*%%%*/
5571 $$ = gettable(p, $1, &@$);
5572 /*% %*/
5573 /*% ripper: var_ref!($1) %*/
5574 }
5575 ;
5576
5577opt_rescue : k_rescue exc_list exc_var then
5578 compstmt
5579 opt_rescue
5580 {
5581 /*%%%*/
5582 NODE *body = $5;
5583 if ($3) {
5584 NODE *err = NEW_ERRINFO(&@3);
5585 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5586 body = block_append(p, err, body);
5587 }
5588 $$ = NEW_RESBODY($2, body, $6, &@$);
5589 if ($2) {
5590 fixpos($$, $2);
5591 }
5592 else if ($3) {
5593 fixpos($$, $3);
5594 }
5595 else {
5596 fixpos($$, $5);
5597 }
5598 /*% %*/
5599 /*% ripper: rescue!($2, $3, $5, $6) %*/
5600 }
5601 | none
5602 ;
5603
5604exc_list : arg_value
5605 {
5606 /*%%%*/
5607 $$ = NEW_LIST($1, &@$);
5608 /*% %*/
5609 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5610 }
5611 | mrhs
5612 {
5613 /*%%%*/
5614 if (!($$ = splat_array($1))) $$ = $1;
5615 /*% %*/
5616 /*% ripper: $1 %*/
5617 }
5618 | none
5619 ;
5620
5621exc_var : tASSOC lhs
5622 {
5623 $$ = $2;
5624 }
5625 | none
5626 ;
5627
5628opt_ensure : k_ensure compstmt
5629 {
5630 p->ctxt.in_rescue = $1.in_rescue;
5631 /*%%%*/
5632 $$ = $2;
5633 /*% %*/
5634 /*% ripper: ensure!($2) %*/
5635 }
5636 | none
5637 ;
5638
5639literal : numeric
5640 | symbol
5641 ;
5642
5643strings : string
5644 {
5645 /*%%%*/
5646 NODE *node = $1;
5647 if (!node) {
5648 node = NEW_STR(STR_NEW0(), &@$);
5649 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(node)->nd_lit);
5650 }
5651 else {
5652 node = evstr2dstr(p, node);
5653 }
5654 $$ = node;
5655 /*% %*/
5656 /*% ripper: $1 %*/
5657 }
5658 ;
5659
5660string : tCHAR
5661 | string1
5662 | string string1
5663 {
5664 /*%%%*/
5665 $$ = literal_concat(p, $1, $2, &@$);
5666 /*% %*/
5667 /*% ripper: string_concat!($1, $2) %*/
5668 }
5669 ;
5670
5671string1 : tSTRING_BEG string_contents tSTRING_END
5672 {
5673 /*%%%*/
5674 $$ = heredoc_dedent(p, $2);
5675 if ($$) nd_set_loc($$, &@$);
5676 /*% %*/
5677 /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/
5678 }
5679 ;
5680
5681xstring : tXSTRING_BEG xstring_contents tSTRING_END
5682 {
5683 /*%%%*/
5684 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5685 /*% %*/
5686 /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/
5687 }
5688 ;
5689
5690regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5691 {
5692 $$ = new_regexp(p, $2, $3, &@$);
5693 }
5694 ;
5695
5696words_sep : ' ' {}
5697 | words_sep ' '
5698 ;
5699
5700words : tWORDS_BEG words_sep word_list tSTRING_END
5701 {
5702 /*%%%*/
5703 $$ = make_list($3, &@$);
5704 /*% %*/
5705 /*% ripper: array!($3) %*/
5706 }
5707 ;
5708
5709word_list : /* none */
5710 {
5711 /*%%%*/
5712 $$ = 0;
5713 /*% %*/
5714 /*% ripper: words_new! %*/
5715 }
5716 | word_list word words_sep
5717 {
5718 /*%%%*/
5719 $$ = list_append(p, $1, evstr2dstr(p, $2));
5720 /*% %*/
5721 /*% ripper: words_add!($1, $2) %*/
5722 }
5723 ;
5724
5725word : string_content
5726 /*% ripper[brace]: word_add!(word_new!, $1) %*/
5727 | word string_content
5728 {
5729 /*%%%*/
5730 $$ = literal_concat(p, $1, $2, &@$);
5731 /*% %*/
5732 /*% ripper: word_add!($1, $2) %*/
5733 }
5734 ;
5735
5736symbols : tSYMBOLS_BEG words_sep symbol_list tSTRING_END
5737 {
5738 /*%%%*/
5739 $$ = make_list($3, &@$);
5740 /*% %*/
5741 /*% ripper: array!($3) %*/
5742 }
5743 ;
5744
5745symbol_list : /* none */
5746 {
5747 /*%%%*/
5748 $$ = 0;
5749 /*% %*/
5750 /*% ripper: symbols_new! %*/
5751 }
5752 | symbol_list word words_sep
5753 {
5754 /*%%%*/
5755 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5756 /*% %*/
5757 /*% ripper: symbols_add!($1, $2) %*/
5758 }
5759 ;
5760
5761qwords : tQWORDS_BEG words_sep qword_list tSTRING_END
5762 {
5763 /*%%%*/
5764 $$ = make_list($3, &@$);
5765 /*% %*/
5766 /*% ripper: array!($3) %*/
5767 }
5768 ;
5769
5770qsymbols : tQSYMBOLS_BEG words_sep qsym_list tSTRING_END
5771 {
5772 /*%%%*/
5773 $$ = make_list($3, &@$);
5774 /*% %*/
5775 /*% ripper: array!($3) %*/
5776 }
5777 ;
5778
5779qword_list : /* none */
5780 {
5781 /*%%%*/
5782 $$ = 0;
5783 /*% %*/
5784 /*% ripper: qwords_new! %*/
5785 }
5786 | qword_list tSTRING_CONTENT words_sep
5787 {
5788 /*%%%*/
5789 $$ = list_append(p, $1, $2);
5790 /*% %*/
5791 /*% ripper: qwords_add!($1, $2) %*/
5792 }
5793 ;
5794
5795qsym_list : /* none */
5796 {
5797 /*%%%*/
5798 $$ = 0;
5799 /*% %*/
5800 /*% ripper: qsymbols_new! %*/
5801 }
5802 | qsym_list tSTRING_CONTENT words_sep
5803 {
5804 /*%%%*/
5805 $$ = symbol_append(p, $1, $2);
5806 /*% %*/
5807 /*% ripper: qsymbols_add!($1, $2) %*/
5808 }
5809 ;
5810
5811string_contents : /* none */
5812 {
5813 /*%%%*/
5814 $$ = 0;
5815 /*% %*/
5816 /*% ripper: string_content! %*/
5817 /*%%%*/
5818 /*%
5819 $$ = ripper_new_yylval(p, 0, $$, 0);
5820 %*/
5821 }
5822 | string_contents string_content
5823 {
5824 /*%%%*/
5825 $$ = literal_concat(p, $1, $2, &@$);
5826 /*% %*/
5827 /*% ripper: string_add!($1, $2) %*/
5828 /*%%%*/
5829 /*%
5830 if (ripper_is_node_yylval(p, $1) && ripper_is_node_yylval(p, $2) &&
5831 !RNODE_RIPPER($1)->nd_cval) {
5832 RNODE_RIPPER($1)->nd_cval = RNODE_RIPPER($2)->nd_cval;
5833 RNODE_RIPPER($1)->nd_rval = add_mark_object(p, $$);
5834 $$ = $1;
5835 }
5836 %*/
5837 }
5838 ;
5839
5840xstring_contents: /* none */
5841 {
5842 /*%%%*/
5843 $$ = 0;
5844 /*% %*/
5845 /*% ripper: xstring_new! %*/
5846 }
5847 | xstring_contents string_content
5848 {
5849 /*%%%*/
5850 $$ = literal_concat(p, $1, $2, &@$);
5851 /*% %*/
5852 /*% ripper: xstring_add!($1, $2) %*/
5853 }
5854 ;
5855
5856regexp_contents: /* none */
5857 {
5858 /*%%%*/
5859 $$ = 0;
5860 /*% %*/
5861 /*% ripper: regexp_new! %*/
5862 /*%%%*/
5863 /*%
5864 $$ = ripper_new_yylval(p, 0, $$, 0);
5865 %*/
5866 }
5867 | regexp_contents string_content
5868 {
5869 /*%%%*/
5870 NODE *head = $1, *tail = $2;
5871 if (!head) {
5872 $$ = tail;
5873 }
5874 else if (!tail) {
5875 $$ = head;
5876 }
5877 else {
5878 switch (nd_type(head)) {
5879 case NODE_STR:
5880 head = str2dstr(p, head);
5881 break;
5882 case NODE_DSTR:
5883 break;
5884 default:
5885 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
5886 break;
5887 }
5888 $$ = list_append(p, head, tail);
5889 }
5890 /*%
5891 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
5892 if (ripper_is_node_yylval(p, n1)) {
5893 s1 = RNODE_RIPPER(n1)->nd_cval;
5894 n1 = RNODE_RIPPER(n1)->nd_rval;
5895 }
5896 if (ripper_is_node_yylval(p, n2)) {
5897 s2 = RNODE_RIPPER(n2)->nd_cval;
5898 n2 = RNODE_RIPPER(n2)->nd_rval;
5899 }
5900 $$ = dispatch2(regexp_add, n1, n2);
5901 if (!s1 && s2) {
5902 $$ = ripper_new_yylval(p, 0, $$, s2);
5903 }
5904 %*/
5905 }
5906 ;
5907
5908string_content : tSTRING_CONTENT
5909 /*% ripper[brace]: ripper_new_yylval(p, 0, get_value($1), $1) %*/
5910 | tSTRING_DVAR
5911 {
5912 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
5913 $<strterm>$ = p->lex.strterm;
5914 p->lex.strterm = 0;
5915 SET_LEX_STATE(EXPR_BEG);
5916 }
5917 string_dvar
5918 {
5919 p->lex.strterm = $<strterm>2;
5920 /*%%%*/
5921 $$ = NEW_EVSTR($3, &@$);
5922 nd_set_line($$, @3.end_pos.lineno);
5923 /*% %*/
5924 /*% ripper: string_dvar!($3) %*/
5925 }
5926 | tSTRING_DBEG[term]
5927 {
5928 CMDARG_PUSH(0);
5929 COND_PUSH(0);
5930 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
5931 $<strterm>term = p->lex.strterm;
5932 p->lex.strterm = 0;
5933 $<num>$ = p->lex.state;
5934 SET_LEX_STATE(EXPR_BEG);
5935 }[state]
5936 {
5937 $<num>$ = p->lex.brace_nest;
5938 p->lex.brace_nest = 0;
5939 }[brace]
5940 {
5941 $<num>$ = p->heredoc_indent;
5942 p->heredoc_indent = 0;
5943 }[indent]
5944 compstmt string_dend
5945 {
5946 COND_POP();
5947 CMDARG_POP();
5948 p->lex.strterm = $<strterm>term;
5949 SET_LEX_STATE($<num>state);
5950 p->lex.brace_nest = $<num>brace;
5951 p->heredoc_indent = $<num>indent;
5952 p->heredoc_line_indent = -1;
5953 /*%%%*/
5954 if ($compstmt) nd_unset_fl_newline($compstmt);
5955 $$ = new_evstr(p, $compstmt, &@$);
5956 /*% %*/
5957 /*% ripper: string_embexpr!($compstmt) %*/
5958 }
5959 ;
5960
5961string_dend : tSTRING_DEND
5962 | END_OF_INPUT
5963 ;
5964
5965string_dvar : nonlocal_var
5966 {
5967 /*%%%*/
5968 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5969 /*% %*/
5970 /*% ripper: var_ref!($1) %*/
5971 }
5972 | backref
5973 ;
5974
5975symbol : ssym
5976 | dsym
5977 ;
5978
5979ssym : tSYMBEG sym
5980 {
5981 SET_LEX_STATE(EXPR_END);
5982 /*%%%*/
5983 $$ = NEW_LIT(ID2SYM($2), &@$);
5984 /*% %*/
5985 /*% ripper: symbol_literal!(symbol!($2)) %*/
5986 }
5987 ;
5988
5989sym : fname
5990 | nonlocal_var
5991 ;
5992
5993dsym : tSYMBEG string_contents tSTRING_END
5994 {
5995 SET_LEX_STATE(EXPR_END);
5996 /*%%%*/
5997 $$ = dsym_node(p, $2, &@$);
5998 /*% %*/
5999 /*% ripper: dyna_symbol!($2) %*/
6000 }
6001 ;
6002
6003numeric : simple_numeric
6004 | tUMINUS_NUM simple_numeric %prec tLOWEST
6005 {
6006 /*%%%*/
6007 $$ = $2;
6008 RB_OBJ_WRITE(p->ast, &RNODE_LIT($$)->nd_lit, negate_lit(p, RNODE_LIT($$)->nd_lit));
6009 /*% %*/
6010 /*% ripper: unary!(ID2VAL(idUMinus), $2) %*/
6011 }
6012 ;
6013
6014simple_numeric : tINTEGER
6015 | tFLOAT
6016 | tRATIONAL
6017 | tIMAGINARY
6018 ;
6019
6020nonlocal_var : tIVAR
6021 | tGVAR
6022 | tCVAR
6023 ;
6024
6025user_variable : tIDENTIFIER
6026 | tCONSTANT
6027 | nonlocal_var
6028 ;
6029
6030keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6031 | keyword_self {$$ = KWD2EID(self, $1);}
6032 | keyword_true {$$ = KWD2EID(true, $1);}
6033 | keyword_false {$$ = KWD2EID(false, $1);}
6034 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6035 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6036 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6037 ;
6038
6039var_ref : user_variable
6040 {
6041 /*%%%*/
6042 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
6043 /*%
6044 if (id_is_var(p, get_id($1))) {
6045 $$ = dispatch1(var_ref, $1);
6046 }
6047 else {
6048 $$ = dispatch1(vcall, $1);
6049 }
6050 %*/
6051 }
6052 | keyword_variable
6053 {
6054 /*%%%*/
6055 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
6056 /*% %*/
6057 /*% ripper: var_ref!($1) %*/
6058 }
6059 ;
6060
6061var_lhs : user_variable
6062 {
6063 /*%%%*/
6064 $$ = assignable(p, $1, 0, &@$);
6065 /*% %*/
6066 /*% ripper: assignable(p, var_field(p, $1)) %*/
6067 }
6068 | keyword_variable
6069 {
6070 /*%%%*/
6071 $$ = assignable(p, $1, 0, &@$);
6072 /*% %*/
6073 /*% ripper: assignable(p, var_field(p, $1)) %*/
6074 }
6075 ;
6076
6077backref : tNTH_REF
6078 | tBACK_REF
6079 ;
6080
6081superclass : '<'
6082 {
6083 SET_LEX_STATE(EXPR_BEG);
6084 p->command_start = TRUE;
6085 }
6086 expr_value term
6087 {
6088 $$ = $3;
6089 }
6090 | /* none */
6091 {
6092 /*%%%*/
6093 $$ = 0;
6094 /*% %*/
6095 /*% ripper: Qnil %*/
6096 }
6097 ;
6098
6099f_opt_paren_args: f_paren_args
6100 | none
6101 {
6102 p->ctxt.in_argdef = 0;
6103 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
6104 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
6105 }
6106 ;
6107
6108f_paren_args : '(' f_args rparen
6109 {
6110 /*%%%*/
6111 $$ = $2;
6112 /*% %*/
6113 /*% ripper: paren!($2) %*/
6114 SET_LEX_STATE(EXPR_BEG);
6115 p->command_start = TRUE;
6116 p->ctxt.in_argdef = 0;
6117 }
6118 ;
6119
6120f_arglist : f_paren_args
6121 | {
6122 $<ctxt>$ = p->ctxt;
6123 p->ctxt.in_kwarg = 1;
6124 p->ctxt.in_argdef = 1;
6125 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6126 }
6127 f_args term
6128 {
6129 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
6130 p->ctxt.in_argdef = 0;
6131 $$ = $2;
6132 SET_LEX_STATE(EXPR_BEG);
6133 p->command_start = TRUE;
6134 }
6135 ;
6136
6137args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
6138 {
6139 $$ = new_args_tail(p, $1, $3, $4, &@3);
6140 }
6141 | f_kwarg opt_f_block_arg
6142 {
6143 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
6144 }
6145 | f_any_kwrest opt_f_block_arg
6146 {
6147 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
6148 }
6149 | f_block_arg
6150 {
6151 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
6152 }
6153 | args_forward
6154 {
6155 add_forwarding_args(p);
6156 $$ = new_args_tail(p, Qnone, $1, arg_FWD_BLOCK, &@1);
6157 /*%%%*/
6158 $$->nd_ainfo.forwarding = 1;
6159 /*% %*/
6160 }
6161 ;
6162
6163opt_args_tail : ',' args_tail
6164 {
6165 $$ = $2;
6166 }
6167 | /* none */
6168 {
6169 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
6170 }
6171 ;
6172
6173f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
6174 {
6175 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
6176 }
6177 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
6178 {
6179 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6180 }
6181 | f_arg ',' f_optarg opt_args_tail
6182 {
6183 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
6184 }
6185 | f_arg ',' f_optarg ',' f_arg opt_args_tail
6186 {
6187 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
6188 }
6189 | f_arg ',' f_rest_arg opt_args_tail
6190 {
6191 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
6192 }
6193 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
6194 {
6195 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
6196 }
6197 | f_arg opt_args_tail
6198 {
6199 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
6200 }
6201 | f_optarg ',' f_rest_arg opt_args_tail
6202 {
6203 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
6204 }
6205 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
6206 {
6207 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
6208 }
6209 | f_optarg opt_args_tail
6210 {
6211 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
6212 }
6213 | f_optarg ',' f_arg opt_args_tail
6214 {
6215 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
6216 }
6217 | f_rest_arg opt_args_tail
6218 {
6219 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
6220 }
6221 | f_rest_arg ',' f_arg opt_args_tail
6222 {
6223 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
6224 }
6225 | args_tail
6226 {
6227 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
6228 }
6229 | /* none */
6230 {
6231 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
6232 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
6233 }
6234 ;
6235
6236args_forward : tBDOT3
6237 {
6238 /*%%%*/
6239#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
6240 $$ = 0;
6241#else
6242 $$ = idFWD_KWREST;
6243#endif
6244 /*% %*/
6245 /*% ripper: args_forward! %*/
6246 }
6247 ;
6248
6249f_bad_arg : tCONSTANT
6250 {
6251 static const char mesg[] = "formal argument cannot be a constant";
6252 /*%%%*/
6253 yyerror1(&@1, mesg);
6254 $$ = 0;
6255 /*% %*/
6256 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6257 }
6258 | tIVAR
6259 {
6260 static const char mesg[] = "formal argument cannot be an instance variable";
6261 /*%%%*/
6262 yyerror1(&@1, mesg);
6263 $$ = 0;
6264 /*% %*/
6265 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6266 }
6267 | tGVAR
6268 {
6269 static const char mesg[] = "formal argument cannot be a global variable";
6270 /*%%%*/
6271 yyerror1(&@1, mesg);
6272 $$ = 0;
6273 /*% %*/
6274 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6275 }
6276 | tCVAR
6277 {
6278 static const char mesg[] = "formal argument cannot be a class variable";
6279 /*%%%*/
6280 yyerror1(&@1, mesg);
6281 $$ = 0;
6282 /*% %*/
6283 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6284 }
6285 ;
6286
6287f_norm_arg : f_bad_arg
6288 | tIDENTIFIER
6289 {
6290 formal_argument(p, $1);
6291 p->max_numparam = ORDINAL_PARAM;
6292 $$ = $1;
6293 }
6294 ;
6295
6296f_arg_asgn : f_norm_arg
6297 {
6298 ID id = get_id($1);
6299 arg_var(p, id);
6300 p->cur_arg = id;
6301 $$ = $1;
6302 }
6303 ;
6304
6305f_arg_item : f_arg_asgn
6306 {
6307 p->cur_arg = 0;
6308 /*%%%*/
6309 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6310 /*% %*/
6311 /*% ripper: get_value($1) %*/
6312 }
6313 | tLPAREN f_margs rparen
6314 {
6315 /*%%%*/
6316 ID tid = internal_id(p);
6317 YYLTYPE loc;
6318 loc.beg_pos = @2.beg_pos;
6319 loc.end_pos = @2.beg_pos;
6320 arg_var(p, tid);
6321 if (dyna_in_block(p)) {
6322 $2->nd_value = NEW_DVAR(tid, &loc);
6323 }
6324 else {
6325 $2->nd_value = NEW_LVAR(tid, &loc);
6326 }
6327 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6328 $$->nd_next = (NODE *)$2;
6329 /*% %*/
6330 /*% ripper: mlhs_paren!($2) %*/
6331 }
6332 ;
6333
6334f_arg : f_arg_item
6335 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
6336 | f_arg ',' f_arg_item
6337 {
6338 /*%%%*/
6339 $$ = $1;
6340 $$->nd_plen++;
6341 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6342 rb_discard_node(p, (NODE *)$3);
6343 /*% %*/
6344 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6345 }
6346 ;
6347
6348
6349f_label : tLABEL
6350 {
6351 arg_var(p, formal_argument(p, $1));
6352 p->cur_arg = get_id($1);
6353 p->max_numparam = ORDINAL_PARAM;
6354 p->ctxt.in_argdef = 0;
6355 $$ = $1;
6356 }
6357 ;
6358
6359f_kw : f_label arg_value
6360 {
6361 p->cur_arg = 0;
6362 p->ctxt.in_argdef = 1;
6363 /*%%%*/
6364 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6365 /*% %*/
6366 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
6367 }
6368 | f_label
6369 {
6370 p->cur_arg = 0;
6371 p->ctxt.in_argdef = 1;
6372 /*%%%*/
6373 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6374 /*% %*/
6375 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
6376 }
6377 ;
6378
6379f_block_kw : f_label primary_value
6380 {
6381 p->ctxt.in_argdef = 1;
6382 /*%%%*/
6383 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6384 /*% %*/
6385 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
6386 }
6387 | f_label
6388 {
6389 p->ctxt.in_argdef = 1;
6390 /*%%%*/
6391 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6392 /*% %*/
6393 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
6394 }
6395 ;
6396
6397f_block_kwarg : f_block_kw
6398 {
6399 /*%%%*/
6400 $$ = $1;
6401 /*% %*/
6402 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6403 }
6404 | f_block_kwarg ',' f_block_kw
6405 {
6406 /*%%%*/
6407 $$ = kwd_append($1, $3);
6408 /*% %*/
6409 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6410 }
6411 ;
6412
6413
6414f_kwarg : f_kw
6415 {
6416 /*%%%*/
6417 $$ = $1;
6418 /*% %*/
6419 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6420 }
6421 | f_kwarg ',' f_kw
6422 {
6423 /*%%%*/
6424 $$ = kwd_append($1, $3);
6425 /*% %*/
6426 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6427 }
6428 ;
6429
6430kwrest_mark : tPOW
6431 | tDSTAR
6432 ;
6433
6434f_no_kwarg : p_kwnorest
6435 {
6436 /*%%%*/
6437 /*% %*/
6438 /*% ripper: nokw_param!(Qnil) %*/
6439 }
6440 ;
6441
6442f_kwrest : kwrest_mark tIDENTIFIER
6443 {
6444 arg_var(p, shadowing_lvar(p, get_id($2)));
6445 /*%%%*/
6446 $$ = $2;
6447 /*% %*/
6448 /*% ripper: kwrest_param!($2) %*/
6449 }
6450 | kwrest_mark
6451 {
6452 arg_var(p, idFWD_KWREST);
6453 /*%%%*/
6454 $$ = idFWD_KWREST;
6455 /*% %*/
6456 /*% ripper: kwrest_param!(Qnil) %*/
6457 }
6458 ;
6459
6460f_opt : f_arg_asgn f_eq arg_value
6461 {
6462 p->cur_arg = 0;
6463 p->ctxt.in_argdef = 1;
6464 /*%%%*/
6465 $$ = NEW_OPT_ARG(assignable(p, $1, $3, &@$), &@$);
6466 /*% %*/
6467 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
6468 }
6469 ;
6470
6471f_block_opt : f_arg_asgn f_eq primary_value
6472 {
6473 p->cur_arg = 0;
6474 p->ctxt.in_argdef = 1;
6475 /*%%%*/
6476 $$ = NEW_OPT_ARG(assignable(p, $1, $3, &@$), &@$);
6477 /*% %*/
6478 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
6479 }
6480 ;
6481
6482f_block_optarg : f_block_opt
6483 {
6484 /*%%%*/
6485 $$ = $1;
6486 /*% %*/
6487 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6488 }
6489 | f_block_optarg ',' f_block_opt
6490 {
6491 /*%%%*/
6492 $$ = opt_arg_append($1, $3);
6493 /*% %*/
6494 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6495 }
6496 ;
6497
6498f_optarg : f_opt
6499 {
6500 /*%%%*/
6501 $$ = $1;
6502 /*% %*/
6503 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6504 }
6505 | f_optarg ',' f_opt
6506 {
6507 /*%%%*/
6508 $$ = opt_arg_append($1, $3);
6509 /*% %*/
6510 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6511 }
6512 ;
6513
6514restarg_mark : '*'
6515 | tSTAR
6516 ;
6517
6518f_rest_arg : restarg_mark tIDENTIFIER
6519 {
6520 arg_var(p, shadowing_lvar(p, get_id($2)));
6521 /*%%%*/
6522 $$ = $2;
6523 /*% %*/
6524 /*% ripper: rest_param!($2) %*/
6525 }
6526 | restarg_mark
6527 {
6528 arg_var(p, idFWD_REST);
6529 /*%%%*/
6530 $$ = idFWD_REST;
6531 /*% %*/
6532 /*% ripper: rest_param!(Qnil) %*/
6533 }
6534 ;
6535
6536blkarg_mark : '&'
6537 | tAMPER
6538 ;
6539
6540f_block_arg : blkarg_mark tIDENTIFIER
6541 {
6542 arg_var(p, shadowing_lvar(p, get_id($2)));
6543 /*%%%*/
6544 $$ = $2;
6545 /*% %*/
6546 /*% ripper: blockarg!($2) %*/
6547 }
6548 | blkarg_mark
6549 {
6550 arg_var(p, idFWD_BLOCK);
6551 /*%%%*/
6552 $$ = idFWD_BLOCK;
6553 /*% %*/
6554 /*% ripper: blockarg!(Qnil) %*/
6555 }
6556 ;
6557
6558opt_f_block_arg : ',' f_block_arg
6559 {
6560 $$ = $2;
6561 }
6562 | none
6563 {
6564 $$ = Qnull;
6565 }
6566 ;
6567
6568singleton : var_ref
6569 {
6570 value_expr($1);
6571 $$ = $1;
6572 }
6573 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
6574 {
6575 /*%%%*/
6576 NODE *expr = last_expr_node($3);
6577 switch (nd_type(expr)) {
6578 case NODE_STR:
6579 case NODE_DSTR:
6580 case NODE_XSTR:
6581 case NODE_DXSTR:
6582 case NODE_DREGX:
6583 case NODE_LIT:
6584 case NODE_DSYM:
6585 case NODE_LIST:
6586 case NODE_ZLIST:
6587 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6588 break;
6589 default:
6590 value_expr($3);
6591 break;
6592 }
6593 $$ = $3;
6594 /*% %*/
6595 /*% ripper: paren!($3) %*/
6596 }
6597 ;
6598
6599assoc_list : none
6600 | assocs trailer
6601 {
6602 /*%%%*/
6603 $$ = $1;
6604 /*% %*/
6605 /*% ripper: assoclist_from_args!($1) %*/
6606 }
6607 ;
6608
6609assocs : assoc
6610 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
6611 | assocs ',' assoc
6612 {
6613 /*%%%*/
6614 NODE *assocs = $1;
6615 NODE *tail = $3;
6616 if (!assocs) {
6617 assocs = tail;
6618 }
6619 else if (tail) {
6620 if (RNODE_LIST(assocs)->nd_head &&
6621 !RNODE_LIST(tail)->nd_head && nd_type_p(RNODE_LIST(tail)->nd_next, NODE_LIST) &&
6622 nd_type_p(RNODE_LIST(RNODE_LIST(tail)->nd_next)->nd_head, NODE_HASH)) {
6623 /* DSTAR */
6624 tail = RNODE_HASH(RNODE_LIST(RNODE_LIST(tail)->nd_next)->nd_head)->nd_head;
6625 }
6626 assocs = list_concat(assocs, tail);
6627 }
6628 $$ = assocs;
6629 /*% %*/
6630 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6631 }
6632 ;
6633
6634assoc : arg_value tASSOC arg_value
6635 {
6636 /*%%%*/
6637 if (nd_type_p($1, NODE_STR)) {
6638 nd_set_type($1, NODE_LIT);
6639 RB_OBJ_WRITE(p->ast, &RNODE_LIT($1)->nd_lit, rb_fstring(RNODE_LIT($1)->nd_lit));
6640 }
6641 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6642 /*% %*/
6643 /*% ripper: assoc_new!($1, $3) %*/
6644 }
6645 | tLABEL arg_value
6646 {
6647 /*%%%*/
6648 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
6649 /*% %*/
6650 /*% ripper: assoc_new!($1, $2) %*/
6651 }
6652 | tLABEL
6653 {
6654 /*%%%*/
6655 NODE *val = gettable(p, $1, &@$);
6656 if (!val) val = NEW_BEGIN(0, &@$);
6657 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), val);
6658 /*% %*/
6659 /*% ripper: assoc_new!($1, Qnil) %*/
6660 }
6661 | tSTRING_BEG string_contents tLABEL_END arg_value
6662 {
6663 /*%%%*/
6664 YYLTYPE loc = code_loc_gen(&@1, &@3);
6665 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6666 /*% %*/
6667 /*% ripper: assoc_new!(dyna_symbol!($2), $4) %*/
6668 }
6669 | tDSTAR arg_value
6670 {
6671 /*%%%*/
6672 if (nd_type_p($2, NODE_HASH) &&
6673 !(RNODE_HASH($2)->nd_head && RNODE_LIST(RNODE_HASH($2)->nd_head)->as.nd_alen)) {
6674 static VALUE empty_hash;
6675 if (!empty_hash) {
6676 empty_hash = rb_obj_freeze(rb_hash_new());
6677 rb_gc_register_mark_object(empty_hash);
6678 }
6679 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
6680 }
6681 else
6682 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6683 /*% %*/
6684 /*% ripper: assoc_splat!($2) %*/
6685 }
6686 | tDSTAR
6687 {
6688 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6689 /*%%%*/
6690 $$ = list_append(p, NEW_LIST(0, &@$),
6691 NEW_LVAR(idFWD_KWREST, &@$));
6692 /*% %*/
6693 /*% ripper: assoc_splat!(Qnil) %*/
6694 }
6695 ;
6696
6697operation : tIDENTIFIER
6698 | tCONSTANT
6699 | tFID
6700 ;
6701
6702operation2 : operation
6703 | op
6704 ;
6705
6706operation3 : tIDENTIFIER
6707 | tFID
6708 | op
6709 ;
6710
6711dot_or_colon : '.'
6712 | tCOLON2
6713 ;
6714
6715call_op : '.'
6716 | tANDDOT
6717 ;
6718
6719call_op2 : call_op
6720 | tCOLON2
6721 ;
6722
6723opt_terms : /* none */
6724 | terms
6725 ;
6726
6727opt_nl : /* none */
6728 | '\n'
6729 ;
6730
6731rparen : opt_nl ')'
6732 ;
6733
6734rbracket : opt_nl ']'
6735 ;
6736
6737rbrace : opt_nl '}'
6738 ;
6739
6740trailer : opt_nl
6741 | ','
6742 ;
6743
6744term : ';' {yyerrok;token_flush(p);}
6745 | '\n'
6746 {
6747 @$.end_pos = @$.beg_pos;
6748 token_flush(p);
6749 }
6750 ;
6751
6752terms : term
6753 | terms ';' {yyerrok;}
6754 ;
6755
6756none : /* none */
6757 {
6758 $$ = Qnull;
6759 }
6760 ;
6761%%
6762# undef p
6763# undef yylex
6764# undef yylval
6765# define yylval (*p->lval)
6766
6767static int regx_options(struct parser_params*);
6768static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6769static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6770static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6771static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6772
6773#ifndef RIPPER
6774# define set_yylval_node(x) { \
6775 YYLTYPE _cur_loc; \
6776 rb_parser_set_location(p, &_cur_loc); \
6777 yylval.node = (x); \
6778}
6779# define set_yylval_str(x) \
6780do { \
6781 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6782 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6783} while(0)
6784# define set_yylval_literal(x) \
6785do { \
6786 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
6787 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6788} while(0)
6789# define set_yylval_num(x) (yylval.num = (x))
6790# define set_yylval_id(x) (yylval.id = (x))
6791# define set_yylval_name(x) (yylval.id = (x))
6792# define yylval_id() (yylval.id)
6793#else
6794static inline VALUE
6795ripper_yylval_id(struct parser_params *p, ID x)
6796{
6797 return ripper_new_yylval(p, x, ID2SYM(x), 0);
6798}
6799# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
6800# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
6801# define set_yylval_id(x) (void)(x)
6802# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
6803# define set_yylval_literal(x) add_mark_object(p, (x))
6804# define set_yylval_node(x) (yylval.val = ripper_new_yylval(p, 0, 0, STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)))
6805# define yylval_id() yylval.id
6806# define _cur_loc NULL_LOC /* dummy */
6807#endif
6808
6809#define set_yylval_noname() set_yylval_id(keyword_nil)
6810#define has_delayed_token(p) (!NIL_P(p->delayed.token))
6811
6812#ifndef RIPPER
6813#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6814#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6815
6816static bool
6817parser_has_token(struct parser_params *p)
6818{
6819 const char *const pcur = p->lex.pcur;
6820 const char *const ptok = p->lex.ptok;
6821 if (p->keep_tokens && (pcur < ptok)) {
6822 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6823 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6824 }
6825 return pcur > ptok;
6826}
6827
6828static VALUE
6829code_loc_to_ary(struct parser_params *p, const rb_code_location_t *loc)
6830{
6831 VALUE ary = rb_ary_new_from_args(4,
6832 INT2NUM(loc->beg_pos.lineno), INT2NUM(loc->beg_pos.column),
6833 INT2NUM(loc->end_pos.lineno), INT2NUM(loc->end_pos.column));
6834 rb_obj_freeze(ary);
6835
6836 return ary;
6837}
6838
6839static void
6840parser_append_tokens(struct parser_params *p, VALUE str, enum yytokentype t, int line)
6841{
6842 VALUE ary;
6843 int token_id;
6844
6845 ary = rb_ary_new2(4);
6846 token_id = p->token_id;
6847 rb_ary_push(ary, INT2FIX(token_id));
6848 rb_ary_push(ary, ID2SYM(parser_token2id(p, t)));
6849 rb_ary_push(ary, str);
6850 rb_ary_push(ary, code_loc_to_ary(p, p->yylloc));
6851 rb_obj_freeze(ary);
6852 rb_ary_push(p->tokens, ary);
6853 p->token_id++;
6854
6855 if (p->debug) {
6856 rb_parser_printf(p, "Append tokens (line: %d) %"PRIsVALUE"\n", line, ary);
6857 }
6858}
6859
6860static void
6861parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6862{
6863 debug_token_line(p, "parser_dispatch_scan_event", line);
6864
6865 if (!parser_has_token(p)) return;
6866
6867 RUBY_SET_YYLLOC(*p->yylloc);
6868
6869 if (p->keep_tokens) {
6870 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6871 parser_append_tokens(p, str, t, line);
6872 }
6873
6874 token_flush(p);
6875}
6876
6877#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6878static void
6879parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6880{
6881 debug_token_line(p, "parser_dispatch_delayed_token", line);
6882
6883 if (!has_delayed_token(p)) return;
6884
6885 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6886
6887 if (p->keep_tokens) {
6888 parser_append_tokens(p, p->delayed.token, t, line);
6889 }
6890
6891 p->delayed.token = Qnil;
6892}
6893#else
6894#define literal_flush(p, ptr) ((void)(ptr))
6895
6896#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &RNODE_RIPPER(yylval.node)->nd_rval : &yylval.val))
6897
6898static int
6899ripper_has_scan_event(struct parser_params *p)
6900{
6901 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6902 return p->lex.pcur > p->lex.ptok;
6903}
6904
6905static VALUE
6906ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6907{
6908 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6909 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6910 RUBY_SET_YYLLOC(*p->yylloc);
6911 token_flush(p);
6912 return rval;
6913}
6914
6915static void
6916ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6917{
6918 if (!ripper_has_scan_event(p)) return;
6919 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
6920}
6921#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6922
6923static void
6924ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6925{
6926 /* save and adjust the location to delayed token for callbacks */
6927 int saved_line = p->ruby_sourceline;
6928 const char *saved_tokp = p->lex.ptok;
6929
6930 if (!has_delayed_token(p)) return;
6931 p->ruby_sourceline = p->delayed.beg_line;
6932 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6933 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
6934 p->delayed.token = Qnil;
6935 p->ruby_sourceline = saved_line;
6936 p->lex.ptok = saved_tokp;
6937}
6938#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6939#endif /* RIPPER */
6940
6941static inline int
6942is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6943{
6944 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6945}
6946
6947static inline int
6948parser_is_identchar(struct parser_params *p)
6949{
6950 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
6951}
6952
6953static inline int
6954parser_isascii(struct parser_params *p)
6955{
6956 return ISASCII(*(p->lex.pcur-1));
6957}
6958
6959static void
6960token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
6961{
6962 int column = 1, nonspc = 0, i;
6963 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
6964 if (*ptr == '\t') {
6965 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
6966 }
6967 column++;
6968 if (*ptr != ' ' && *ptr != '\t') {
6969 nonspc = 1;
6970 }
6971 }
6972
6973 ptinfo->beg = loc->beg_pos;
6974 ptinfo->indent = column;
6975 ptinfo->nonspc = nonspc;
6976}
6977
6978static void
6979token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6980{
6981 token_info *ptinfo;
6982
6983 if (!p->token_info_enabled) return;
6984 ptinfo = ALLOC(token_info);
6985 ptinfo->token = token;
6986 ptinfo->next = p->token_info;
6987 token_info_setup(ptinfo, p->lex.pbeg, loc);
6988
6989 p->token_info = ptinfo;
6990}
6991
6992static void
6993token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6994{
6995 token_info *ptinfo_beg = p->token_info;
6996
6997 if (!ptinfo_beg) return;
6998 p->token_info = ptinfo_beg->next;
6999
7000 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7001 token_info_warn(p, token, ptinfo_beg, 1, loc);
7002 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7003}
7004
7005static void
7006token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7007{
7008 token_info *ptinfo_beg = p->token_info;
7009
7010 if (!ptinfo_beg) return;
7011 p->token_info = ptinfo_beg->next;
7012
7013 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7014 ptinfo_beg->beg.column != beg_pos.column ||
7015 strcmp(ptinfo_beg->token, token)) {
7016 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7017 beg_pos.lineno, beg_pos.column, token,
7018 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7019 ptinfo_beg->token);
7020 }
7021
7022 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7023}
7024
7025static void
7026token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7027{
7028 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7029 if (!p->token_info_enabled) return;
7030 if (!ptinfo_beg) return;
7031 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7032 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7033 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7034 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7035 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7036 rb_warn3L(ptinfo_end->beg.lineno,
7037 "mismatched indentations at '%s' with '%s' at %d",
7038 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7039}
7040
7041static int
7042parser_precise_mbclen(struct parser_params *p, const char *ptr)
7043{
7044 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7045 if (!MBCLEN_CHARFOUND_P(len)) {
7046 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7047 return -1;
7048 }
7049 return len;
7050}
7051
7052#ifndef RIPPER
7053static void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
7054
7055static inline void
7056parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7057{
7058 VALUE str;
7059 int lineno = p->ruby_sourceline;
7060 if (!yylloc) {
7061 return;
7062 }
7063 else if (yylloc->beg_pos.lineno == lineno) {
7064 str = p->lex.lastline;
7065 }
7066 else {
7067 return;
7068 }
7069 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7070}
7071
7072static int
7073parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7074{
7075#if 0
7076 YYLTYPE current;
7077
7078 if (!yylloc) {
7079 yylloc = RUBY_SET_YYLLOC(current);
7080 }
7081 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7082 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7083 yylloc = 0;
7084 }
7085#endif
7086 parser_compile_error(p, yylloc, "%s", msg);
7087 parser_show_error_line(p, yylloc);
7088 return 0;
7089}
7090
7091static int
7092parser_yyerror0(struct parser_params *p, const char *msg)
7093{
7094 YYLTYPE current;
7095 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7096}
7097
7098static void
7099ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
7100{
7101 VALUE mesg;
7102 const int max_line_margin = 30;
7103 const char *ptr, *ptr_end, *pt, *pb;
7104 const char *pre = "", *post = "", *pend;
7105 const char *code = "", *caret = "";
7106 const char *lim;
7107 const char *const pbeg = RSTRING_PTR(str);
7108 char *buf;
7109 long len;
7110 int i;
7111
7112 if (!yylloc) return;
7113 pend = RSTRING_END(str);
7114 if (pend > pbeg && pend[-1] == '\n') {
7115 if (--pend > pbeg && pend[-1] == '\r') --pend;
7116 }
7117
7118 pt = pend;
7119 if (lineno == yylloc->end_pos.lineno &&
7120 (pend - pbeg) > yylloc->end_pos.column) {
7121 pt = pbeg + yylloc->end_pos.column;
7122 }
7123
7124 ptr = ptr_end = pt;
7125 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7126 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7127
7128 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7129 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7130
7131 len = ptr_end - ptr;
7132 if (len > 4) {
7133 if (ptr > pbeg) {
7134 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
7135 if (ptr > pbeg) pre = "...";
7136 }
7137 if (ptr_end < pend) {
7138 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
7139 if (ptr_end < pend) post = "...";
7140 }
7141 }
7142 pb = pbeg;
7143 if (lineno == yylloc->beg_pos.lineno) {
7144 pb += yylloc->beg_pos.column;
7145 if (pb > pt) pb = pt;
7146 }
7147 if (pb < ptr) pb = ptr;
7148 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7149 return;
7150 }
7151 if (RTEST(errbuf)) {
7152 mesg = rb_attr_get(errbuf, idMesg);
7153 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
7154 rb_str_cat_cstr(mesg, "\n");
7155 }
7156 else {
7157 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
7158 }
7159 if (!errbuf && rb_stderr_tty_p()) {
7160#define CSI_BEGIN "\033["
7161#define CSI_SGR "m"
7162 rb_str_catf(mesg,
7163 CSI_BEGIN""CSI_SGR"%s" /* pre */
7164 CSI_BEGIN"1"CSI_SGR"%.*s"
7165 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7166 CSI_BEGIN";1"CSI_SGR"%.*s"
7167 CSI_BEGIN""CSI_SGR"%s" /* post */
7168 "\n",
7169 pre,
7170 (int)(pb - ptr), ptr,
7171 (int)(pt - pb), pb,
7172 (int)(ptr_end - pt), pt,
7173 post);
7174 }
7175 else {
7176 char *p2;
7177
7178 len = ptr_end - ptr;
7179 lim = pt < pend ? pt : pend;
7180 i = (int)(lim - ptr);
7181 buf = ALLOCA_N(char, i+2);
7182 code = ptr;
7183 caret = p2 = buf;
7184 if (ptr <= pb) {
7185 while (ptr < pb) {
7186 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7187 }
7188 *p2++ = '^';
7189 ptr++;
7190 }
7191 if (lim > ptr) {
7192 memset(p2, '~', (lim - ptr));
7193 p2 += (lim - ptr);
7194 }
7195 *p2 = '\0';
7196 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7197 pre, (int)len, code, post,
7198 pre, caret);
7199 }
7200 if (!errbuf) rb_write_error_str(mesg);
7201}
7202#else
7203static int
7204parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7205{
7206 const char *pcur = 0, *ptok = 0;
7207 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7208 p->ruby_sourceline == yylloc->end_pos.lineno) {
7209 pcur = p->lex.pcur;
7210 ptok = p->lex.ptok;
7211 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7212 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7213 }
7214 parser_yyerror0(p, msg);
7215 if (pcur) {
7216 p->lex.ptok = ptok;
7217 p->lex.pcur = pcur;
7218 }
7219 return 0;
7220}
7221
7222static int
7223parser_yyerror0(struct parser_params *p, const char *msg)
7224{
7225 dispatch1(parse_error, STR_NEW2(msg));
7226 ripper_error(p);
7227 return 0;
7228}
7229
7230static inline void
7231parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7232{
7233}
7234#endif /* !RIPPER */
7235
7236#ifndef RIPPER
7237static int
7238vtable_size(const struct vtable *tbl)
7239{
7240 if (!DVARS_TERMINAL_P(tbl)) {
7241 return tbl->pos;
7242 }
7243 else {
7244 return 0;
7245 }
7246}
7247#endif
7248
7249static struct vtable *
7250vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7251{
7252 struct vtable *tbl = ALLOC(struct vtable);
7253 tbl->pos = 0;
7254 tbl->capa = 8;
7255 tbl->tbl = ALLOC_N(ID, tbl->capa);
7256 tbl->prev = prev;
7257#ifndef RIPPER
7258 if (p->debug) {
7259 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7260 }
7261#endif
7262 return tbl;
7263}
7264#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7265
7266static void
7267vtable_free_gen(struct parser_params *p, int line, const char *name,
7268 struct vtable *tbl)
7269{
7270#ifndef RIPPER
7271 if (p->debug) {
7272 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7273 }
7274#endif
7275 if (!DVARS_TERMINAL_P(tbl)) {
7276 if (tbl->tbl) {
7277 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7278 }
7279 ruby_sized_xfree(tbl, sizeof(*tbl));
7280 }
7281}
7282#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7283
7284static void
7285vtable_add_gen(struct parser_params *p, int line, const char *name,
7286 struct vtable *tbl, ID id)
7287{
7288#ifndef RIPPER
7289 if (p->debug) {
7290 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7291 line, name, (void *)tbl, rb_id2name(id));
7292 }
7293#endif
7294 if (DVARS_TERMINAL_P(tbl)) {
7295 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7296 return;
7297 }
7298 if (tbl->pos == tbl->capa) {
7299 tbl->capa = tbl->capa * 2;
7300 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7301 }
7302 tbl->tbl[tbl->pos++] = id;
7303}
7304#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7305
7306#ifndef RIPPER
7307static void
7308vtable_pop_gen(struct parser_params *p, int line, const char *name,
7309 struct vtable *tbl, int n)
7310{
7311 if (p->debug) {
7312 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7313 line, name, (void *)tbl, n);
7314 }
7315 if (tbl->pos < n) {
7316 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7317 return;
7318 }
7319 tbl->pos -= n;
7320}
7321#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7322#endif
7323
7324static int
7325vtable_included(const struct vtable * tbl, ID id)
7326{
7327 int i;
7328
7329 if (!DVARS_TERMINAL_P(tbl)) {
7330 for (i = 0; i < tbl->pos; i++) {
7331 if (tbl->tbl[i] == id) {
7332 return i+1;
7333 }
7334 }
7335 }
7336 return 0;
7337}
7338
7339static void parser_prepare(struct parser_params *p);
7340
7341#ifndef RIPPER
7342static NODE *parser_append_options(struct parser_params *p, NODE *node);
7343
7344static int
7345e_option_supplied(struct parser_params *p)
7346{
7347 return strcmp(p->ruby_sourcefile, "-e") == 0;
7348}
7349
7350static VALUE
7351yycompile0(VALUE arg)
7352{
7353 int n;
7354 NODE *tree;
7355 struct parser_params *p = (struct parser_params *)arg;
7356 int cov = FALSE;
7357
7358 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
7359 if (p->debug_lines && p->ruby_sourceline > 0) {
7360 VALUE str = rb_default_rs;
7361 n = p->ruby_sourceline;
7362 do {
7363 rb_ary_push(p->debug_lines, str);
7364 } while (--n);
7365 }
7366
7367 if (!e_option_supplied(p)) {
7368 cov = TRUE;
7369 }
7370 }
7371
7372 if (p->debug_lines) {
7373 RB_OBJ_WRITE(p->ast, &p->ast->body.script_lines, p->debug_lines);
7374 }
7375
7376 parser_prepare(p);
7377#define RUBY_DTRACE_PARSE_HOOK(name) \
7378 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7379 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7380 }
7381 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7382 n = yyparse(p);
7383 RUBY_DTRACE_PARSE_HOOK(END);
7384 p->debug_lines = 0;
7385
7386 p->lex.strterm = 0;
7387 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7388 if (n || p->error_p) {
7389 VALUE mesg = p->error_buffer;
7390 if (!mesg) {
7391 mesg = syntax_error_new();
7392 }
7393 if (!p->error_tolerant) {
7394 rb_set_errinfo(mesg);
7395 return FALSE;
7396 }
7397 }
7398 tree = p->eval_tree;
7399 if (!tree) {
7400 tree = NEW_NIL(&NULL_LOC);
7401 }
7402 else {
7403 VALUE tokens = p->tokens;
7404 NODE *prelude;
7405 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7406 prelude = block_append(p, p->eval_tree_begin, body);
7407 RNODE_SCOPE(tree)->nd_body = prelude;
7408 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7409 p->ast->body.coverage_enabled = cov;
7410 if (p->keep_tokens) {
7411 rb_obj_freeze(tokens);
7412 rb_ast_set_tokens(p->ast, tokens);
7413 }
7414 }
7415 p->ast->body.root = tree;
7416 if (!p->ast->body.script_lines) p->ast->body.script_lines = INT2FIX(p->line_count);
7417 return TRUE;
7418}
7419
7420static rb_ast_t *
7421yycompile(struct parser_params *p, VALUE fname, int line)
7422{
7423 rb_ast_t *ast;
7424 if (NIL_P(fname)) {
7425 p->ruby_sourcefile_string = Qnil;
7426 p->ruby_sourcefile = "(none)";
7427 }
7428 else {
7429 p->ruby_sourcefile_string = rb_fstring(fname);
7430 p->ruby_sourcefile = StringValueCStr(fname);
7431 }
7432 p->ruby_sourceline = line - 1;
7433
7434 p->lvtbl = NULL;
7435
7436 p->ast = ast = rb_ast_new();
7437 compile_callback(yycompile0, (VALUE)p);
7438 p->ast = 0;
7439
7440 while (p->lvtbl) {
7441 local_pop(p);
7442 }
7443
7444 return ast;
7445}
7446#endif /* !RIPPER */
7447
7448static rb_encoding *
7449must_be_ascii_compatible(struct parser_params *p, VALUE s)
7450{
7451 rb_encoding *enc = rb_enc_get(s);
7452 if (!rb_enc_asciicompat(enc)) {
7453 rb_raise(rb_eArgError, "invalid source encoding");
7454 }
7455 return enc;
7456}
7457
7458static VALUE
7459lex_get_str(struct parser_params *p, VALUE s)
7460{
7461 char *beg, *end, *start;
7462 long len;
7463
7464 beg = RSTRING_PTR(s);
7465 len = RSTRING_LEN(s);
7466 start = beg;
7467 if (p->lex.gets_.ptr) {
7468 if (len == p->lex.gets_.ptr) return Qnil;
7469 beg += p->lex.gets_.ptr;
7470 len -= p->lex.gets_.ptr;
7471 }
7472 end = memchr(beg, '\n', len);
7473 if (end) len = ++end - beg;
7474 p->lex.gets_.ptr += len;
7475 return rb_str_subseq(s, beg - start, len);
7476}
7477
7478static VALUE
7479lex_getline(struct parser_params *p)
7480{
7481 VALUE line = (*p->lex.gets)(p, p->lex.input);
7482 if (NIL_P(line)) return line;
7483 must_be_ascii_compatible(p, line);
7484 if (RB_OBJ_FROZEN(line)) line = rb_str_dup(line); // needed for RubyVM::AST.of because script_lines in iseq is deep-frozen
7485 p->line_count++;
7486 return line;
7487}
7488
7489#ifndef RIPPER
7490static rb_ast_t*
7491parser_compile_string(rb_parser_t *p, VALUE fname, VALUE s, int line)
7492{
7493 p->lex.gets = lex_get_str;
7494 p->lex.gets_.ptr = 0;
7495 p->lex.input = rb_str_new_frozen(s);
7496 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7497
7498 return yycompile(p, fname, line);
7499}
7500
7501rb_ast_t*
7502rb_ruby_parser_compile_string_path(rb_parser_t *p, VALUE f, VALUE s, int line)
7503{
7504 must_be_ascii_compatible(p, s);
7505 return parser_compile_string(p, f, s, line);
7506}
7507
7508rb_ast_t*
7509rb_ruby_parser_compile_string(rb_parser_t *p, const char *f, VALUE s, int line)
7510{
7511 return rb_ruby_parser_compile_string_path(p, rb_filesystem_str_new_cstr(f), s, line);
7512}
7513
7514static VALUE
7515lex_io_gets(struct parser_params *p, VALUE io)
7516{
7517 return rb_io_gets_internal(io);
7518}
7519
7520rb_ast_t*
7521rb_ruby_parser_compile_file_path(rb_parser_t *p, VALUE fname, VALUE file, int start)
7522{
7523 p->lex.gets = lex_io_gets;
7524 p->lex.input = file;
7525 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7526
7527 return yycompile(p, fname, start);
7528}
7529
7530static VALUE
7531lex_generic_gets(struct parser_params *p, VALUE input)
7532{
7533 return (*p->lex.gets_.call)(input, p->line_count);
7534}
7535
7536rb_ast_t*
7537rb_ruby_parser_compile_generic(rb_parser_t *p, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
7538{
7539 p->lex.gets = lex_generic_gets;
7540 p->lex.gets_.call = lex_gets;
7541 p->lex.input = input;
7542 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7543
7544 return yycompile(p, fname, start);
7545}
7546#endif /* !RIPPER */
7547
7548#define STR_FUNC_ESCAPE 0x01
7549#define STR_FUNC_EXPAND 0x02
7550#define STR_FUNC_REGEXP 0x04
7551#define STR_FUNC_QWORDS 0x08
7552#define STR_FUNC_SYMBOL 0x10
7553#define STR_FUNC_INDENT 0x20
7554#define STR_FUNC_LABEL 0x40
7555#define STR_FUNC_LIST 0x4000
7556#define STR_FUNC_TERM 0x8000
7557
7558enum string_type {
7559 str_label = STR_FUNC_LABEL,
7560 str_squote = (0),
7561 str_dquote = (STR_FUNC_EXPAND),
7562 str_xquote = (STR_FUNC_EXPAND),
7563 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7564 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7565 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7566 str_ssym = (STR_FUNC_SYMBOL),
7567 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7568};
7569
7570static VALUE
7571parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7572{
7573 VALUE str;
7574
7575 str = rb_enc_str_new(ptr, len, enc);
7576 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7577 if (is_ascii_string(str)) {
7578 }
7579 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7580 rb_enc_associate(str, rb_ascii8bit_encoding());
7581 }
7582 }
7583
7584 return str;
7585}
7586
7587static int
7588strterm_is_heredoc(rb_strterm_t *strterm)
7589{
7590 return strterm->flags & STRTERM_HEREDOC;
7591}
7592
7593static rb_strterm_t *
7594new_strterm(struct parser_params *p, int func, int term, int paren)
7595{
7596 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7597 strterm->u.literal.func = func;
7598 strterm->u.literal.term = term;
7599 strterm->u.literal.paren = paren;
7600 return strterm;
7601}
7602
7603static rb_strterm_t *
7604new_heredoc(struct parser_params *p)
7605{
7606 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7607 strterm->flags |= STRTERM_HEREDOC;
7608 return strterm;
7609}
7610
7611#define peek(p,c) peek_n(p, (c), 0)
7612#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7613#define peekc(p) peekc_n(p, 0)
7614#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7615
7616static void
7617add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7618{
7619#ifndef RIPPER
7620 debug_token_line(p, "add_delayed_token", line);
7621#endif
7622
7623 if (tok < end) {
7624 if (has_delayed_token(p)) {
7625 bool next_line = end_with_newline_p(p, p->delayed.token);
7626 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7627 int end_col = (next_line ? 0 : p->delayed.end_col);
7628 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7629 dispatch_delayed_token(p, tSTRING_CONTENT);
7630 }
7631 }
7632 if (!has_delayed_token(p)) {
7633 p->delayed.token = rb_str_buf_new(end - tok);
7634 rb_enc_associate(p->delayed.token, p->enc);
7635 p->delayed.beg_line = p->ruby_sourceline;
7636 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7637 }
7638 rb_str_buf_cat(p->delayed.token, tok, end - tok);
7639 p->delayed.end_line = p->ruby_sourceline;
7640 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7641 p->lex.ptok = end;
7642 }
7643}
7644
7645static void
7646set_lastline(struct parser_params *p, VALUE v)
7647{
7648 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
7649 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
7650 p->lex.lastline = v;
7651}
7652
7653static int
7654nextline(struct parser_params *p, int set_encoding)
7655{
7656 VALUE v = p->lex.nextline;
7657 p->lex.nextline = 0;
7658 if (!v) {
7659 if (p->eofp)
7660 return -1;
7661
7662 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7663 goto end_of_input;
7664 }
7665
7666 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
7667 end_of_input:
7668 p->eofp = 1;
7669 lex_goto_eol(p);
7670 return -1;
7671 }
7672#ifndef RIPPER
7673 if (p->debug_lines) {
7674 if (set_encoding) rb_enc_associate(v, p->enc);
7675 rb_ary_push(p->debug_lines, v);
7676 }
7677#endif
7678 p->cr_seen = FALSE;
7679 }
7680 else if (NIL_P(v)) {
7681 /* after here-document without terminator */
7682 goto end_of_input;
7683 }
7684 add_delayed_token(p, p->lex.ptok, p->lex.pend, __LINE__);
7685 if (p->heredoc_end > 0) {
7686 p->ruby_sourceline = p->heredoc_end;
7687 p->heredoc_end = 0;
7688 }
7689 p->ruby_sourceline++;
7690 set_lastline(p, v);
7691 token_flush(p);
7692 return 0;
7693}
7694
7695static int
7696parser_cr(struct parser_params *p, int c)
7697{
7698 if (peek(p, '\n')) {
7699 p->lex.pcur++;
7700 c = '\n';
7701 }
7702 return c;
7703}
7704
7705static inline int
7706nextc0(struct parser_params *p, int set_encoding)
7707{
7708 int c;
7709
7710 if (UNLIKELY(lex_eol_p(p) || p->eofp || RTEST(p->lex.nextline))) {
7711 if (nextline(p, set_encoding)) return -1;
7712 }
7713 c = (unsigned char)*p->lex.pcur++;
7714 if (UNLIKELY(c == '\r')) {
7715 c = parser_cr(p, c);
7716 }
7717
7718 return c;
7719}
7720#define nextc(p) nextc0(p, TRUE)
7721
7722static void
7723pushback(struct parser_params *p, int c)
7724{
7725 if (c == -1) return;
7726 p->eofp = 0;
7727 p->lex.pcur--;
7728 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7729 p->lex.pcur--;
7730 }
7731}
7732
7733#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7734
7735#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7736#define tok(p) (p)->tokenbuf
7737#define toklen(p) (p)->tokidx
7738
7739static int
7740looking_at_eol_p(struct parser_params *p)
7741{
7742 const char *ptr = p->lex.pcur;
7743 while (!lex_eol_ptr_p(p, ptr)) {
7744 int c = (unsigned char)*ptr++;
7745 int eol = (c == '\n' || c == '#');
7746 if (eol || !ISSPACE(c)) {
7747 return eol;
7748 }
7749 }
7750 return TRUE;
7751}
7752
7753static char*
7754newtok(struct parser_params *p)
7755{
7756 p->tokidx = 0;
7757 if (!p->tokenbuf) {
7758 p->toksiz = 60;
7759 p->tokenbuf = ALLOC_N(char, 60);
7760 }
7761 if (p->toksiz > 4096) {
7762 p->toksiz = 60;
7763 REALLOC_N(p->tokenbuf, char, 60);
7764 }
7765 return p->tokenbuf;
7766}
7767
7768static char *
7769tokspace(struct parser_params *p, int n)
7770{
7771 p->tokidx += n;
7772
7773 if (p->tokidx >= p->toksiz) {
7774 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7775 REALLOC_N(p->tokenbuf, char, p->toksiz);
7776 }
7777 return &p->tokenbuf[p->tokidx-n];
7778}
7779
7780static void
7781tokadd(struct parser_params *p, int c)
7782{
7783 p->tokenbuf[p->tokidx++] = (char)c;
7784 if (p->tokidx >= p->toksiz) {
7785 p->toksiz *= 2;
7786 REALLOC_N(p->tokenbuf, char, p->toksiz);
7787 }
7788}
7789
7790static int
7791tok_hex(struct parser_params *p, size_t *numlen)
7792{
7793 int c;
7794
7795 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7796 if (!*numlen) {
7797 yyerror0("invalid hex escape");
7798 dispatch_scan_event(p, tSTRING_CONTENT);
7799 return 0;
7800 }
7801 p->lex.pcur += *numlen;
7802 return c;
7803}
7804
7805#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7806
7807static int
7808escaped_control_code(int c)
7809{
7810 int c2 = 0;
7811 switch (c) {
7812 case ' ':
7813 c2 = 's';
7814 break;
7815 case '\n':
7816 c2 = 'n';
7817 break;
7818 case '\t':
7819 c2 = 't';
7820 break;
7821 case '\v':
7822 c2 = 'v';
7823 break;
7824 case '\r':
7825 c2 = 'r';
7826 break;
7827 case '\f':
7828 c2 = 'f';
7829 break;
7830 }
7831 return c2;
7832}
7833
7834#define WARN_SPACE_CHAR(c, prefix) \
7835 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
7836
7837static int
7838tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7839 int regexp_literal, int wide)
7840{
7841 size_t numlen;
7842 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7843 p->lex.pcur += numlen;
7844 if (p->lex.strterm == NULL ||
7845 strterm_is_heredoc(p->lex.strterm) ||
7846 (p->lex.strterm->u.literal.func != str_regexp)) {
7847 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7848 literal_flush(p, p->lex.pcur);
7849 yyerror0("invalid Unicode escape");
7850 return wide && numlen > 0;
7851 }
7852 if (codepoint > 0x10ffff) {
7853 literal_flush(p, p->lex.pcur);
7854 yyerror0("invalid Unicode codepoint (too large)");
7855 return wide;
7856 }
7857 if ((codepoint & 0xfffff800) == 0xd800) {
7858 literal_flush(p, p->lex.pcur);
7859 yyerror0("invalid Unicode codepoint");
7860 return wide;
7861 }
7862 }
7863 if (regexp_literal) {
7864 tokcopy(p, (int)numlen);
7865 }
7866 else if (codepoint >= 0x80) {
7867 rb_encoding *utf8 = rb_utf8_encoding();
7868 if (*encp && utf8 != *encp) {
7869 YYLTYPE loc = RUBY_INIT_YYLLOC();
7870 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7871 parser_show_error_line(p, &loc);
7872 return wide;
7873 }
7874 *encp = utf8;
7875 tokaddmbc(p, codepoint, *encp);
7876 }
7877 else {
7878 tokadd(p, codepoint);
7879 }
7880 return TRUE;
7881}
7882
7883static int tokadd_mbchar(struct parser_params *p, int c);
7884
7885static int
7886tokskip_mbchar(struct parser_params *p)
7887{
7888 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7889 if (len > 0) {
7890 p->lex.pcur += len - 1;
7891 }
7892 return len;
7893}
7894
7895/* return value is for ?\u3042 */
7896static void
7897tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7898 int term, int symbol_literal, int regexp_literal)
7899{
7900 /*
7901 * If `term` is not -1, then we allow multiple codepoints in \u{}
7902 * upto `term` byte, otherwise we're parsing a character literal.
7903 * And then add the codepoints to the current token.
7904 */
7905 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7906
7907 const int open_brace = '{', close_brace = '}';
7908
7909 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7910
7911 if (peek(p, open_brace)) { /* handle \u{...} form */
7912 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7913 /*
7914 * Skip parsing validation code and copy bytes as-is until term or
7915 * closing brace, in order to correctly handle extended regexps where
7916 * invalid unicode escapes are allowed in comments. The regexp parser
7917 * does its own validation and will catch any issues.
7918 */
7919 tokadd(p, open_brace);
7920 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7921 int c = peekc(p);
7922 if (c == close_brace) {
7923 tokadd(p, c);
7924 ++p->lex.pcur;
7925 break;
7926 }
7927 else if (c == term) {
7928 break;
7929 }
7930 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7931 tokadd(p, c);
7932 c = *++p->lex.pcur;
7933 }
7934 tokadd_mbchar(p, c);
7935 }
7936 }
7937 else {
7938 const char *second = NULL;
7939 int c, last = nextc(p);
7940 if (lex_eol_p(p)) goto unterminated;
7941 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7942 while (c != close_brace) {
7943 if (c == term) goto unterminated;
7944 if (second == multiple_codepoints)
7945 second = p->lex.pcur;
7946 if (regexp_literal) tokadd(p, last);
7947 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
7948 break;
7949 }
7950 while (ISSPACE(c = peekc(p))) {
7951 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7952 last = c;
7953 }
7954 if (term == -1 && !second)
7955 second = multiple_codepoints;
7956 }
7957
7958 if (c != close_brace) {
7959 unterminated:
7960 token_flush(p);
7961 yyerror0("unterminated Unicode escape");
7962 return;
7963 }
7964 if (second && second != multiple_codepoints) {
7965 const char *pcur = p->lex.pcur;
7966 p->lex.pcur = second;
7967 dispatch_scan_event(p, tSTRING_CONTENT);
7968 token_flush(p);
7969 p->lex.pcur = pcur;
7970 yyerror0(multiple_codepoints);
7971 token_flush(p);
7972 }
7973
7974 if (regexp_literal) tokadd(p, close_brace);
7975 nextc(p);
7976 }
7977 }
7978 else { /* handle \uxxxx form */
7979 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
7980 token_flush(p);
7981 return;
7982 }
7983 }
7984}
7985
7986#define ESCAPE_CONTROL 1
7987#define ESCAPE_META 2
7988
7989static int
7990read_escape(struct parser_params *p, int flags)
7991{
7992 int c;
7993 size_t numlen;
7994
7995 switch (c = nextc(p)) {
7996 case '\\': /* Backslash */
7997 return c;
7998
7999 case 'n': /* newline */
8000 return '\n';
8001
8002 case 't': /* horizontal tab */
8003 return '\t';
8004
8005 case 'r': /* carriage-return */
8006 return '\r';
8007
8008 case 'f': /* form-feed */
8009 return '\f';
8010
8011 case 'v': /* vertical tab */
8012 return '\13';
8013
8014 case 'a': /* alarm(bell) */
8015 return '\007';
8016
8017 case 'e': /* escape */
8018 return 033;
8019
8020 case '0': case '1': case '2': case '3': /* octal constant */
8021 case '4': case '5': case '6': case '7':
8022 pushback(p, c);
8023 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8024 p->lex.pcur += numlen;
8025 return c;
8026
8027 case 'x': /* hex constant */
8028 c = tok_hex(p, &numlen);
8029 if (numlen == 0) return 0;
8030 return c;
8031
8032 case 'b': /* backspace */
8033 return '\010';
8034
8035 case 's': /* space */
8036 return ' ';
8037
8038 case 'M':
8039 if (flags & ESCAPE_META) goto eof;
8040 if ((c = nextc(p)) != '-') {
8041 goto eof;
8042 }
8043 if ((c = nextc(p)) == '\\') {
8044 switch (peekc(p)) {
8045 case 'u': case 'U':
8046 nextc(p);
8047 goto eof;
8048 }
8049 return read_escape(p, flags|ESCAPE_META) | 0x80;
8050 }
8051 else if (c == -1 || !ISASCII(c)) goto eof;
8052 else {
8053 int c2 = escaped_control_code(c);
8054 if (c2) {
8055 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8056 WARN_SPACE_CHAR(c2, "\\M-");
8057 }
8058 else {
8059 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8060 }
8061 }
8062 else if (ISCNTRL(c)) goto eof;
8063 return ((c & 0xff) | 0x80);
8064 }
8065
8066 case 'C':
8067 if ((c = nextc(p)) != '-') {
8068 goto eof;
8069 }
8070 case 'c':
8071 if (flags & ESCAPE_CONTROL) goto eof;
8072 if ((c = nextc(p))== '\\') {
8073 switch (peekc(p)) {
8074 case 'u': case 'U':
8075 nextc(p);
8076 goto eof;
8077 }
8078 c = read_escape(p, flags|ESCAPE_CONTROL);
8079 }
8080 else if (c == '?')
8081 return 0177;
8082 else if (c == -1) goto eof;
8083 else if (!ISASCII(c)) {
8084 tokskip_mbchar(p);
8085 goto eof;
8086 }
8087 else {
8088 int c2 = escaped_control_code(c);
8089 if (c2) {
8090 if (ISCNTRL(c)) {
8091 if (flags & ESCAPE_META) {
8092 WARN_SPACE_CHAR(c2, "\\M-");
8093 }
8094 else {
8095 WARN_SPACE_CHAR(c2, "");
8096 }
8097 }
8098 else {
8099 if (flags & ESCAPE_META) {
8100 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8101 }
8102 else {
8103 WARN_SPACE_CHAR(c2, "\\C-");
8104 }
8105 }
8106 }
8107 else if (ISCNTRL(c)) goto eof;
8108 }
8109 return c & 0x9f;
8110
8111 eof:
8112 case -1:
8113 yyerror0("Invalid escape character syntax");
8114 dispatch_scan_event(p, tSTRING_CONTENT);
8115 return '\0';
8116
8117 default:
8118 return c;
8119 }
8120}
8121
8122static void
8123tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8124{
8125 int len = rb_enc_codelen(c, enc);
8126 rb_enc_mbcput(c, tokspace(p, len), enc);
8127}
8128
8129static int
8130tokadd_escape(struct parser_params *p)
8131{
8132 int c;
8133 size_t numlen;
8134
8135 switch (c = nextc(p)) {
8136 case '\n':
8137 return 0; /* just ignore */
8138
8139 case '0': case '1': case '2': case '3': /* octal constant */
8140 case '4': case '5': case '6': case '7':
8141 {
8142 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8143 if (numlen == 0) goto eof;
8144 p->lex.pcur += numlen;
8145 tokcopy(p, (int)numlen + 1);
8146 }
8147 return 0;
8148
8149 case 'x': /* hex constant */
8150 {
8151 tok_hex(p, &numlen);
8152 if (numlen == 0) return -1;
8153 tokcopy(p, (int)numlen + 2);
8154 }
8155 return 0;
8156
8157 eof:
8158 case -1:
8159 yyerror0("Invalid escape character syntax");
8160 token_flush(p);
8161 return -1;
8162
8163 default:
8164 tokadd(p, '\\');
8165 tokadd(p, c);
8166 }
8167 return 0;
8168}
8169
8170static int
8171regx_options(struct parser_params *p)
8172{
8173 int kcode = 0;
8174 int kopt = 0;
8175 int options = 0;
8176 int c, opt, kc;
8177
8178 newtok(p);
8179 while (c = nextc(p), ISALPHA(c)) {
8180 if (c == 'o') {
8181 options |= RE_OPTION_ONCE;
8182 }
8183 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
8184 if (kc >= 0) {
8185 if (kc != rb_ascii8bit_encindex()) kcode = c;
8186 kopt = opt;
8187 }
8188 else {
8189 options |= opt;
8190 }
8191 }
8192 else {
8193 tokadd(p, c);
8194 }
8195 }
8196 options |= kopt;
8197 pushback(p, c);
8198 if (toklen(p)) {
8199 YYLTYPE loc = RUBY_INIT_YYLLOC();
8200 tokfix(p);
8201 compile_error(p, "unknown regexp option%s - %*s",
8202 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8203 parser_show_error_line(p, &loc);
8204 }
8205 return options | RE_OPTION_ENCODING(kcode);
8206}
8207
8208static int
8209tokadd_mbchar(struct parser_params *p, int c)
8210{
8211 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8212 if (len < 0) return -1;
8213 tokadd(p, c);
8214 p->lex.pcur += --len;
8215 if (len > 0) tokcopy(p, len);
8216 return c;
8217}
8218
8219static inline int
8220simple_re_meta(int c)
8221{
8222 switch (c) {
8223 case '$': case '*': case '+': case '.':
8224 case '?': case '^': case '|':
8225 case ')': case ']': case '}': case '>':
8226 return TRUE;
8227 default:
8228 return FALSE;
8229 }
8230}
8231
8232static int
8233parser_update_heredoc_indent(struct parser_params *p, int c)
8234{
8235 if (p->heredoc_line_indent == -1) {
8236 if (c == '\n') p->heredoc_line_indent = 0;
8237 }
8238 else {
8239 if (c == ' ') {
8240 p->heredoc_line_indent++;
8241 return TRUE;
8242 }
8243 else if (c == '\t') {
8244 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8245 p->heredoc_line_indent = w * TAB_WIDTH;
8246 return TRUE;
8247 }
8248 else if (c != '\n') {
8249 if (p->heredoc_indent > p->heredoc_line_indent) {
8250 p->heredoc_indent = p->heredoc_line_indent;
8251 }
8252 p->heredoc_line_indent = -1;
8253 }
8254 }
8255 return FALSE;
8256}
8257
8258static void
8259parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8260{
8261 YYLTYPE loc = RUBY_INIT_YYLLOC();
8262 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8263 compile_error(p, "%s mixed within %s source", n1, n2);
8264 parser_show_error_line(p, &loc);
8265}
8266
8267static void
8268parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8269{
8270 const char *pos = p->lex.pcur;
8271 p->lex.pcur = beg;
8272 parser_mixed_error(p, enc1, enc2);
8273 p->lex.pcur = pos;
8274}
8275
8276static inline char
8277nibble_char_upper(unsigned int c)
8278{
8279 c &= 0xf;
8280 return c + (c < 10 ? '0' : 'A' - 10);
8281}
8282
8283static int
8284tokadd_string(struct parser_params *p,
8285 int func, int term, int paren, long *nest,
8286 rb_encoding **encp, rb_encoding **enc)
8287{
8288 int c;
8289 bool erred = false;
8290#ifdef RIPPER
8291 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8292 int top_of_line = FALSE;
8293#endif
8294
8295#define mixed_error(enc1, enc2) \
8296 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8297#define mixed_escape(beg, enc1, enc2) \
8298 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8299
8300 while ((c = nextc(p)) != -1) {
8301 if (p->heredoc_indent > 0) {
8302 parser_update_heredoc_indent(p, c);
8303 }
8304#ifdef RIPPER
8305 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8306 pushback(p, c);
8307 break;
8308 }
8309#endif
8310
8311 if (paren && c == paren) {
8312 ++*nest;
8313 }
8314 else if (c == term) {
8315 if (!nest || !*nest) {
8316 pushback(p, c);
8317 break;
8318 }
8319 --*nest;
8320 }
8321 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8322 unsigned char c2 = *p->lex.pcur;
8323 if (c2 == '$' || c2 == '@' || c2 == '{') {
8324 pushback(p, c);
8325 break;
8326 }
8327 }
8328 else if (c == '\\') {
8329 c = nextc(p);
8330 switch (c) {
8331 case '\n':
8332 if (func & STR_FUNC_QWORDS) break;
8333 if (func & STR_FUNC_EXPAND) {
8334 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8335 continue;
8336 if (c == term) {
8337 c = '\\';
8338 goto terminate;
8339 }
8340 }
8341 tokadd(p, '\\');
8342 break;
8343
8344 case '\\':
8345 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8346 break;
8347
8348 case 'u':
8349 if ((func & STR_FUNC_EXPAND) == 0) {
8350 tokadd(p, '\\');
8351 break;
8352 }
8353 tokadd_utf8(p, enc, term,
8354 func & STR_FUNC_SYMBOL,
8355 func & STR_FUNC_REGEXP);
8356 continue;
8357
8358 default:
8359 if (c == -1) return -1;
8360 if (!ISASCII(c)) {
8361 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8362 goto non_ascii;
8363 }
8364 if (func & STR_FUNC_REGEXP) {
8365 switch (c) {
8366 case 'c':
8367 case 'C':
8368 case 'M': {
8369 pushback(p, c);
8370 c = read_escape(p, 0);
8371
8372 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8373 *t++ = '\\';
8374 *t++ = 'x';
8375 *t++ = nibble_char_upper(c >> 4);
8376 *t++ = nibble_char_upper(c);
8377 continue;
8378 }
8379 }
8380
8381 if (c == term && !simple_re_meta(c)) {
8382 tokadd(p, c);
8383 continue;
8384 }
8385 pushback(p, c);
8386 if ((c = tokadd_escape(p)) < 0)
8387 return -1;
8388 if (*enc && *enc != *encp) {
8389 mixed_escape(p->lex.ptok+2, *enc, *encp);
8390 }
8391 continue;
8392 }
8393 else if (func & STR_FUNC_EXPAND) {
8394 pushback(p, c);
8395 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8396 c = read_escape(p, 0);
8397 }
8398 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8399 /* ignore backslashed spaces in %w */
8400 }
8401 else if (c != term && !(paren && c == paren)) {
8402 tokadd(p, '\\');
8403 pushback(p, c);
8404 continue;
8405 }
8406 }
8407 }
8408 else if (!parser_isascii(p)) {
8409 non_ascii:
8410 if (!*enc) {
8411 *enc = *encp;
8412 }
8413 else if (*enc != *encp) {
8414 mixed_error(*enc, *encp);
8415 continue;
8416 }
8417 if (tokadd_mbchar(p, c) == -1) return -1;
8418 continue;
8419 }
8420 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8421 pushback(p, c);
8422 break;
8423 }
8424 if (c & 0x80) {
8425 if (!*enc) {
8426 *enc = *encp;
8427 }
8428 else if (*enc != *encp) {
8429 mixed_error(*enc, *encp);
8430 continue;
8431 }
8432 }
8433 tokadd(p, c);
8434#ifdef RIPPER
8435 top_of_line = (c == '\n');
8436#endif
8437 }
8438 terminate:
8439 if (*enc) *encp = *enc;
8440 return c;
8441}
8442
8443#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8444
8445#ifdef RIPPER
8446static void
8447flush_string_content(struct parser_params *p, rb_encoding *enc)
8448{
8449 VALUE content = yylval.val;
8450 if (!ripper_is_node_yylval(p, content))
8451 content = ripper_new_yylval(p, 0, 0, content);
8452 if (has_delayed_token(p)) {
8453 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8454 if (len > 0) {
8455 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8456 }
8457 dispatch_delayed_token(p, tSTRING_CONTENT);
8458 p->lex.ptok = p->lex.pcur;
8459 RNODE_RIPPER(content)->nd_rval = yylval.val;
8460 }
8461 dispatch_scan_event(p, tSTRING_CONTENT);
8462 if (yylval.val != content)
8463 RNODE_RIPPER(content)->nd_rval = yylval.val;
8464 yylval.val = content;
8465}
8466#else
8467static void
8468flush_string_content(struct parser_params *p, rb_encoding *enc)
8469{
8470 if (has_delayed_token(p)) {
8471 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8472 if (len > 0) {
8473 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8474 p->delayed.end_line = p->ruby_sourceline;
8475 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8476 }
8477 dispatch_delayed_token(p, tSTRING_CONTENT);
8478 p->lex.ptok = p->lex.pcur;
8479 }
8480 dispatch_scan_event(p, tSTRING_CONTENT);
8481}
8482#endif
8483
8484RUBY_FUNC_EXPORTED const uint_least32_t ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
8485/* this can be shared with ripper, since it's independent from struct
8486 * parser_params. */
8487#ifndef RIPPER
8488#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8489#define SPECIAL_PUNCT(idx) ( \
8490 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8491 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8492 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8493 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8494 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8495 BIT('0', idx))
8496const uint_least32_t ruby_global_name_punct_bits[] = {
8497 SPECIAL_PUNCT(0),
8498 SPECIAL_PUNCT(1),
8499 SPECIAL_PUNCT(2),
8500};
8501#undef BIT
8502#undef SPECIAL_PUNCT
8503#endif
8504
8505static enum yytokentype
8506parser_peek_variable_name(struct parser_params *p)
8507{
8508 int c;
8509 const char *ptr = p->lex.pcur;
8510
8511 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8512 c = *ptr++;
8513 switch (c) {
8514 case '$':
8515 if ((c = *ptr) == '-') {
8516 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8517 c = *ptr;
8518 }
8519 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8520 return tSTRING_DVAR;
8521 }
8522 break;
8523 case '@':
8524 if ((c = *ptr) == '@') {
8525 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8526 c = *ptr;
8527 }
8528 break;
8529 case '{':
8530 p->lex.pcur = ptr;
8531 p->command_start = TRUE;
8532 return tSTRING_DBEG;
8533 default:
8534 return 0;
8535 }
8536 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8537 return tSTRING_DVAR;
8538 return 0;
8539}
8540
8541#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8542#define IS_END() IS_lex_state(EXPR_END_ANY)
8543#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8544#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8545#define IS_LABEL_POSSIBLE() (\
8546 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8547 IS_ARG())
8548#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8549#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8550
8551static inline enum yytokentype
8552parser_string_term(struct parser_params *p, int func)
8553{
8554 xfree(p->lex.strterm);
8555 p->lex.strterm = 0;
8556 if (func & STR_FUNC_REGEXP) {
8557 set_yylval_num(regx_options(p));
8558 dispatch_scan_event(p, tREGEXP_END);
8559 SET_LEX_STATE(EXPR_END);
8560 return tREGEXP_END;
8561 }
8562 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8563 nextc(p);
8564 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8565 return tLABEL_END;
8566 }
8567 SET_LEX_STATE(EXPR_END);
8568 return tSTRING_END;
8569}
8570
8571static enum yytokentype
8572parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8573{
8574 int func = quote->func;
8575 int term = quote->term;
8576 int paren = quote->paren;
8577 int c, space = 0;
8578 rb_encoding *enc = p->enc;
8579 rb_encoding *base_enc = 0;
8580 VALUE lit;
8581
8582 if (func & STR_FUNC_TERM) {
8583 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8584 SET_LEX_STATE(EXPR_END);
8585 xfree(p->lex.strterm);
8586 p->lex.strterm = 0;
8587 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8588 }
8589 c = nextc(p);
8590 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8591 while (c != '\n' && ISSPACE(c = nextc(p)));
8592 space = 1;
8593 }
8594 if (func & STR_FUNC_LIST) {
8595 quote->func &= ~STR_FUNC_LIST;
8596 space = 1;
8597 }
8598 if (c == term && !quote->nest) {
8599 if (func & STR_FUNC_QWORDS) {
8600 quote->func |= STR_FUNC_TERM;
8601 pushback(p, c); /* dispatch the term at tSTRING_END */
8602 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8603 return ' ';
8604 }
8605 return parser_string_term(p, func);
8606 }
8607 if (space) {
8608 if (!ISSPACE(c)) pushback(p, c);
8609 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8610 return ' ';
8611 }
8612 newtok(p);
8613 if ((func & STR_FUNC_EXPAND) && c == '#') {
8614 enum yytokentype t = parser_peek_variable_name(p);
8615 if (t) return t;
8616 tokadd(p, '#');
8617 c = nextc(p);
8618 }
8619 pushback(p, c);
8620 if (tokadd_string(p, func, term, paren, &quote->nest,
8621 &enc, &base_enc) == -1) {
8622 if (p->eofp) {
8623#ifndef RIPPER
8624# define unterminated_literal(mesg) yyerror0(mesg)
8625#else
8626# define unterminated_literal(mesg) compile_error(p, mesg)
8627#endif
8628 literal_flush(p, p->lex.pcur);
8629 if (func & STR_FUNC_QWORDS) {
8630 /* no content to add, bailing out here */
8631 unterminated_literal("unterminated list meets end of file");
8632 xfree(p->lex.strterm);
8633 p->lex.strterm = 0;
8634 return tSTRING_END;
8635 }
8636 if (func & STR_FUNC_REGEXP) {
8637 unterminated_literal("unterminated regexp meets end of file");
8638 }
8639 else {
8640 unterminated_literal("unterminated string meets end of file");
8641 }
8642 quote->func |= STR_FUNC_TERM;
8643 }
8644 }
8645
8646 tokfix(p);
8647 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8648 set_yylval_str(lit);
8649 flush_string_content(p, enc);
8650
8651 return tSTRING_CONTENT;
8652}
8653
8654static enum yytokentype
8655heredoc_identifier(struct parser_params *p)
8656{
8657 /*
8658 * term_len is length of `<<"END"` except `END`,
8659 * in this case term_len is 4 (<, <, " and ").
8660 */
8661 long len, offset = p->lex.pcur - p->lex.pbeg;
8662 int c = nextc(p), term, func = 0, quote = 0;
8663 enum yytokentype token = tSTRING_BEG;
8664 int indent = 0;
8665
8666 if (c == '-') {
8667 c = nextc(p);
8668 func = STR_FUNC_INDENT;
8669 offset++;
8670 }
8671 else if (c == '~') {
8672 c = nextc(p);
8673 func = STR_FUNC_INDENT;
8674 offset++;
8675 indent = INT_MAX;
8676 }
8677 switch (c) {
8678 case '\'':
8679 func |= str_squote; goto quoted;
8680 case '"':
8681 func |= str_dquote; goto quoted;
8682 case '`':
8683 token = tXSTRING_BEG;
8684 func |= str_xquote; goto quoted;
8685
8686 quoted:
8687 quote++;
8688 offset++;
8689 term = c;
8690 len = 0;
8691 while ((c = nextc(p)) != term) {
8692 if (c == -1 || c == '\r' || c == '\n') {
8693 yyerror0("unterminated here document identifier");
8694 return -1;
8695 }
8696 }
8697 break;
8698
8699 default:
8700 if (!parser_is_identchar(p)) {
8701 pushback(p, c);
8702 if (func & STR_FUNC_INDENT) {
8703 pushback(p, indent > 0 ? '~' : '-');
8704 }
8705 return 0;
8706 }
8707 func |= str_dquote;
8708 do {
8709 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8710 if (n < 0) return 0;
8711 p->lex.pcur += --n;
8712 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8713 pushback(p, c);
8714 break;
8715 }
8716
8717 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8718 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8719 yyerror0("too long here document identifier");
8720 dispatch_scan_event(p, tHEREDOC_BEG);
8721 lex_goto_eol(p);
8722
8723 p->lex.strterm = new_heredoc(p);
8724 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8725 here->offset = offset;
8726 here->sourceline = p->ruby_sourceline;
8727 here->length = (unsigned)len;
8728 here->quote = quote;
8729 here->func = func;
8730 here->lastline = p->lex.lastline;
8731 rb_ast_add_mark_object(p->ast, p->lex.lastline);
8732
8733 token_flush(p);
8734 p->heredoc_indent = indent;
8735 p->heredoc_line_indent = 0;
8736 return token;
8737}
8738
8739static void
8740heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8741{
8742 VALUE line;
8743 rb_strterm_t *term = p->lex.strterm;
8744
8745 p->lex.strterm = 0;
8746 line = here->lastline;
8747 p->lex.lastline = line;
8748 p->lex.pbeg = RSTRING_PTR(line);
8749 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
8750 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8751 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8752 p->heredoc_end = p->ruby_sourceline;
8753 p->ruby_sourceline = (int)here->sourceline;
8754 if (p->eofp) p->lex.nextline = Qnil;
8755 p->eofp = 0;
8756 xfree(term);
8757 rb_ast_delete_mark_object(p->ast, line);
8758}
8759
8760static int
8761dedent_string(struct parser_params *p, VALUE string, int width)
8762{
8763 char *str;
8764 long len;
8765 int i, col = 0;
8766
8767 RSTRING_GETMEM(string, str, len);
8768 for (i = 0; i < len && col < width; i++) {
8769 if (str[i] == ' ') {
8770 col++;
8771 }
8772 else if (str[i] == '\t') {
8773 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8774 if (n > width) break;
8775 col = n;
8776 }
8777 else {
8778 break;
8779 }
8780 }
8781 if (!i) return 0;
8782 rb_str_modify(string);
8783 str = RSTRING_PTR(string);
8784 if (RSTRING_LEN(string) != len)
8785 rb_fatal("literal string changed: %+"PRIsVALUE, string);
8786 MEMMOVE(str, str + i, char, len - i);
8787 rb_str_set_len(string, len - i);
8788 return i;
8789}
8790
8791#ifndef RIPPER
8792static NODE *
8793heredoc_dedent(struct parser_params *p, NODE *root)
8794{
8795 NODE *node, *str_node, *prev_node;
8796 int indent = p->heredoc_indent;
8797 VALUE prev_lit = 0;
8798
8799 if (indent <= 0) return root;
8800 p->heredoc_indent = 0;
8801 if (!root) return root;
8802
8803 prev_node = node = str_node = root;
8804 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8805
8806 while (str_node) {
8807 VALUE lit = RNODE_LIT(str_node)->nd_lit;
8808 if (nd_fl_newline(str_node)) {
8809 dedent_string(p, lit, indent);
8810 }
8811 if (!prev_lit) {
8812 prev_lit = lit;
8813 }
8814 else if (!literal_concat0(p, prev_lit, lit)) {
8815 return 0;
8816 }
8817 else {
8818 NODE *end = RNODE_LIST(node)->as.nd_end;
8819 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8820 if (!node) {
8821 if (nd_type_p(prev_node, NODE_DSTR))
8822 nd_set_type(prev_node, NODE_STR);
8823 break;
8824 }
8825 RNODE_LIST(node)->as.nd_end = end;
8826 goto next_str;
8827 }
8828
8829 str_node = 0;
8830 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8831 next_str:
8832 if (!nd_type_p(node, NODE_LIST)) break;
8833 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8834 enum node_type type = nd_type(str_node);
8835 if (type == NODE_STR || type == NODE_DSTR) break;
8836 prev_lit = 0;
8837 str_node = 0;
8838 }
8839 }
8840 }
8841 return root;
8842}
8843#else /* RIPPER */
8844static VALUE
8845heredoc_dedent(struct parser_params *p, VALUE array)
8846{
8847 int indent = p->heredoc_indent;
8848
8849 if (indent <= 0) return array;
8850 p->heredoc_indent = 0;
8851 dispatch2(heredoc_dedent, array, INT2NUM(indent));
8852 return array;
8853}
8854#endif
8855
8856static int
8857whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8858{
8859 const char *beg = p->lex.pbeg;
8860 const char *ptr = p->lex.pend;
8861
8862 if (ptr - beg < len) return FALSE;
8863 if (ptr > beg && ptr[-1] == '\n') {
8864 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8865 if (ptr - beg < len) return FALSE;
8866 }
8867 if (strncmp(eos, ptr -= len, len)) return FALSE;
8868 if (indent) {
8869 while (beg < ptr && ISSPACE(*beg)) beg++;
8870 }
8871 return beg == ptr;
8872}
8873
8874static int
8875word_match_p(struct parser_params *p, const char *word, long len)
8876{
8877 if (strncmp(p->lex.pcur, word, len)) return 0;
8878 if (lex_eol_n_p(p, len)) return 1;
8879 int c = (unsigned char)p->lex.pcur[len];
8880 if (ISSPACE(c)) return 1;
8881 switch (c) {
8882 case '\0': case '\004': case '\032': return 1;
8883 }
8884 return 0;
8885}
8886
8887#define NUM_SUFFIX_R (1<<0)
8888#define NUM_SUFFIX_I (1<<1)
8889#define NUM_SUFFIX_ALL 3
8890
8891static int
8892number_literal_suffix(struct parser_params *p, int mask)
8893{
8894 int c, result = 0;
8895 const char *lastp = p->lex.pcur;
8896
8897 while ((c = nextc(p)) != -1) {
8898 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8899 result |= (mask & NUM_SUFFIX_I);
8900 mask &= ~NUM_SUFFIX_I;
8901 /* r after i, rational of complex is disallowed */
8902 mask &= ~NUM_SUFFIX_R;
8903 continue;
8904 }
8905 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8906 result |= (mask & NUM_SUFFIX_R);
8907 mask &= ~NUM_SUFFIX_R;
8908 continue;
8909 }
8910 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8911 p->lex.pcur = lastp;
8912 literal_flush(p, p->lex.pcur);
8913 return 0;
8914 }
8915 pushback(p, c);
8916 break;
8917 }
8918 return result;
8919}
8920
8921static enum yytokentype
8922set_number_literal(struct parser_params *p, VALUE v,
8923 enum yytokentype type, int suffix)
8924{
8925 if (suffix & NUM_SUFFIX_I) {
8926 v = rb_complex_raw(INT2FIX(0), v);
8927 type = tIMAGINARY;
8928 }
8929 set_yylval_literal(v);
8930 SET_LEX_STATE(EXPR_END);
8931 return type;
8932}
8933
8934static enum yytokentype
8935set_integer_literal(struct parser_params *p, VALUE v, int suffix)
8936{
8937 enum yytokentype type = tINTEGER;
8938 if (suffix & NUM_SUFFIX_R) {
8939 v = rb_rational_raw1(v);
8940 type = tRATIONAL;
8941 }
8942 return set_number_literal(p, v, type, suffix);
8943}
8944
8945#ifdef RIPPER
8946static void
8947dispatch_heredoc_end(struct parser_params *p)
8948{
8949 VALUE str;
8950 if (has_delayed_token(p))
8951 dispatch_delayed_token(p, tSTRING_CONTENT);
8952 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8953 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8954 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8955 lex_goto_eol(p);
8956 token_flush(p);
8957}
8958
8959#else
8960#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8961static void
8962parser_dispatch_heredoc_end(struct parser_params *p, int line)
8963{
8964 if (has_delayed_token(p))
8965 dispatch_delayed_token(p, tSTRING_CONTENT);
8966
8967 if (p->keep_tokens) {
8968 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8969 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8970 parser_append_tokens(p, str, tHEREDOC_END, line);
8971 }
8972
8973 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8974 lex_goto_eol(p);
8975 token_flush(p);
8976}
8977#endif
8978
8979static enum yytokentype
8980here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
8981{
8982 int c, func, indent = 0;
8983 const char *eos, *ptr, *ptr_end;
8984 long len;
8985 VALUE str = 0;
8986 rb_encoding *enc = p->enc;
8987 rb_encoding *base_enc = 0;
8988 int bol;
8989
8990 eos = RSTRING_PTR(here->lastline) + here->offset;
8991 len = here->length;
8992 indent = (func = here->func) & STR_FUNC_INDENT;
8993
8994 if ((c = nextc(p)) == -1) {
8995 error:
8996#ifdef RIPPER
8997 if (!has_delayed_token(p)) {
8998 dispatch_scan_event(p, tSTRING_CONTENT);
8999 }
9000 else {
9001 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9002 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
9003 int cr = ENC_CODERANGE_UNKNOWN;
9004 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9005 if (cr != ENC_CODERANGE_7BIT &&
9006 rb_is_usascii_enc(p->enc) &&
9007 enc != rb_utf8_encoding()) {
9008 enc = rb_ascii8bit_encoding();
9009 }
9010 }
9011 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
9012 }
9013 dispatch_delayed_token(p, tSTRING_CONTENT);
9014 }
9015 lex_goto_eol(p);
9016#endif
9017 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9018 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9019 (int)len, eos);
9020 token_flush(p);
9021 SET_LEX_STATE(EXPR_END);
9022 return tSTRING_END;
9023 }
9024 bol = was_bol(p);
9025 if (!bol) {
9026 /* not beginning of line, cannot be the terminator */
9027 }
9028 else if (p->heredoc_line_indent == -1) {
9029 /* `heredoc_line_indent == -1` means
9030 * - "after an interpolation in the same line", or
9031 * - "in a continuing line"
9032 */
9033 p->heredoc_line_indent = 0;
9034 }
9035 else if (whole_match_p(p, eos, len, indent)) {
9036 dispatch_heredoc_end(p);
9037 restore:
9038 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9039 token_flush(p);
9040 SET_LEX_STATE(EXPR_END);
9041 return tSTRING_END;
9042 }
9043
9044 if (!(func & STR_FUNC_EXPAND)) {
9045 do {
9046 ptr = RSTRING_PTR(p->lex.lastline);
9047 ptr_end = p->lex.pend;
9048 if (ptr_end > ptr) {
9049 switch (ptr_end[-1]) {
9050 case '\n':
9051 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9052 ptr_end++;
9053 break;
9054 }
9055 case '\r':
9056 --ptr_end;
9057 }
9058 }
9059
9060 if (p->heredoc_indent > 0) {
9061 long i = 0;
9062 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9063 i++;
9064 p->heredoc_line_indent = 0;
9065 }
9066
9067 if (str)
9068 rb_str_cat(str, ptr, ptr_end - ptr);
9069 else
9070 str = STR_NEW(ptr, ptr_end - ptr);
9071 if (!lex_eol_ptr_p(p, ptr_end)) rb_str_cat(str, "\n", 1);
9072 lex_goto_eol(p);
9073 if (p->heredoc_indent > 0) {
9074 goto flush_str;
9075 }
9076 if (nextc(p) == -1) {
9077 if (str) {
9078 str = 0;
9079 }
9080 goto error;
9081 }
9082 } while (!whole_match_p(p, eos, len, indent));
9083 }
9084 else {
9085 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9086 newtok(p);
9087 if (c == '#') {
9088 enum yytokentype t = parser_peek_variable_name(p);
9089 if (p->heredoc_line_indent != -1) {
9090 if (p->heredoc_indent > p->heredoc_line_indent) {
9091 p->heredoc_indent = p->heredoc_line_indent;
9092 }
9093 p->heredoc_line_indent = -1;
9094 }
9095 if (t) return t;
9096 tokadd(p, '#');
9097 c = nextc(p);
9098 }
9099 do {
9100 pushback(p, c);
9101 enc = p->enc;
9102 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9103 if (p->eofp) goto error;
9104 goto restore;
9105 }
9106 if (c != '\n') {
9107 if (c == '\\') p->heredoc_line_indent = -1;
9108 flush:
9109 str = STR_NEW3(tok(p), toklen(p), enc, func);
9110 flush_str:
9111 set_yylval_str(str);
9112#ifndef RIPPER
9113 if (bol) nd_set_fl_newline(yylval.node);
9114#endif
9115 flush_string_content(p, enc);
9116 return tSTRING_CONTENT;
9117 }
9118 tokadd(p, nextc(p));
9119 if (p->heredoc_indent > 0) {
9120 lex_goto_eol(p);
9121 goto flush;
9122 }
9123 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9124 if ((c = nextc(p)) == -1) goto error;
9125 } while (!whole_match_p(p, eos, len, indent));
9126 str = STR_NEW3(tok(p), toklen(p), enc, func);
9127 }
9128 dispatch_heredoc_end(p);
9129#ifdef RIPPER
9130 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
9131 yylval.val, str);
9132#endif
9133 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9134 token_flush(p);
9135 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9136 set_yylval_str(str);
9137#ifndef RIPPER
9138 if (bol) nd_set_fl_newline(yylval.node);
9139#endif
9140 return tSTRING_CONTENT;
9141}
9142
9143#include "lex.c"
9144
9145static int
9146arg_ambiguous(struct parser_params *p, char c)
9147{
9148#ifndef RIPPER
9149 if (c == '/') {
9150 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
9151 }
9152 else {
9153 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
9154 }
9155#else
9156 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9157#endif
9158 return TRUE;
9159}
9160
9161static ID
9162#ifndef RIPPER
9163formal_argument(struct parser_params *p, ID lhs)
9164#else
9165formal_argument(struct parser_params *p, VALUE lhs)
9166#endif
9167{
9168 ID id = get_id(lhs);
9169
9170 switch (id_type(id)) {
9171 case ID_LOCAL:
9172 break;
9173#ifndef RIPPER
9174# define ERR(mesg) yyerror0(mesg)
9175#else
9176# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
9177#endif
9178 case ID_CONST:
9179 ERR("formal argument cannot be a constant");
9180 return 0;
9181 case ID_INSTANCE:
9182 ERR("formal argument cannot be an instance variable");
9183 return 0;
9184 case ID_GLOBAL:
9185 ERR("formal argument cannot be a global variable");
9186 return 0;
9187 case ID_CLASS:
9188 ERR("formal argument cannot be a class variable");
9189 return 0;
9190 default:
9191 ERR("formal argument must be local variable");
9192 return 0;
9193#undef ERR
9194 }
9195 shadowing_lvar(p, id);
9196 return lhs;
9197}
9198
9199static int
9200lvar_defined(struct parser_params *p, ID id)
9201{
9202 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9203}
9204
9205/* emacsen -*- hack */
9206static long
9207parser_encode_length(struct parser_params *p, const char *name, long len)
9208{
9209 long nlen;
9210
9211 if (len > 5 && name[nlen = len - 5] == '-') {
9212 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9213 return nlen;
9214 }
9215 if (len > 4 && name[nlen = len - 4] == '-') {
9216 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9217 return nlen;
9218 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9219 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9220 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9221 return nlen;
9222 }
9223 return len;
9224}
9225
9226static void
9227parser_set_encode(struct parser_params *p, const char *name)
9228{
9229 int idx = rb_enc_find_index(name);
9230 rb_encoding *enc;
9231 VALUE excargs[3];
9232
9233 if (idx < 0) {
9234 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9235 error:
9236 excargs[0] = rb_eArgError;
9237 excargs[2] = rb_make_backtrace();
9238 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9239 rb_exc_raise(rb_make_exception(3, excargs));
9240 }
9241 enc = rb_enc_from_index(idx);
9242 if (!rb_enc_asciicompat(enc)) {
9243 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9244 goto error;
9245 }
9246 p->enc = enc;
9247#ifndef RIPPER
9248 if (p->debug_lines) {
9249 VALUE lines = p->debug_lines;
9250 long i, n = RARRAY_LEN(lines);
9251 for (i = 0; i < n; ++i) {
9252 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
9253 }
9254 }
9255#endif
9256}
9257
9258static int
9259comment_at_top(struct parser_params *p)
9260{
9261 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
9262 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
9263 while (ptr < ptr_end) {
9264 if (!ISSPACE(*ptr)) return 0;
9265 ptr++;
9266 }
9267 return 1;
9268}
9269
9270typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9271typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9272
9273static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9274
9275static void
9276magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9277{
9278 if (!comment_at_top(p)) {
9279 return;
9280 }
9281 parser_set_encode(p, val);
9282}
9283
9284static int
9285parser_get_bool(struct parser_params *p, const char *name, const char *val)
9286{
9287 switch (*val) {
9288 case 't': case 'T':
9289 if (STRCASECMP(val, "true") == 0) {
9290 return TRUE;
9291 }
9292 break;
9293 case 'f': case 'F':
9294 if (STRCASECMP(val, "false") == 0) {
9295 return FALSE;
9296 }
9297 break;
9298 }
9299 return parser_invalid_pragma_value(p, name, val);
9300}
9301
9302static int
9303parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9304{
9305 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9306 return -1;
9307}
9308
9309static void
9310parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9311{
9312 int b = parser_get_bool(p, name, val);
9313 if (b >= 0) p->token_info_enabled = b;
9314}
9315
9316static void
9317parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9318{
9319 int b;
9320
9321 if (p->token_seen) {
9322 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
9323 return;
9324 }
9325
9326 b = parser_get_bool(p, name, val);
9327 if (b < 0) return;
9328
9329 p->frozen_string_literal = b;
9330}
9331
9332static void
9333parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9334{
9335 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9336 if (*s == ' ' || *s == '\t') continue;
9337 if (*s == '#') break;
9338 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
9339 return;
9340 }
9341
9342 switch (*val) {
9343 case 'n': case 'N':
9344 if (STRCASECMP(val, "none") == 0) {
9345 p->ctxt.shareable_constant_value = shareable_none;
9346 return;
9347 }
9348 break;
9349 case 'l': case 'L':
9350 if (STRCASECMP(val, "literal") == 0) {
9351 p->ctxt.shareable_constant_value = shareable_literal;
9352 return;
9353 }
9354 break;
9355 case 'e': case 'E':
9356 if (STRCASECMP(val, "experimental_copy") == 0) {
9357 p->ctxt.shareable_constant_value = shareable_copy;
9358 return;
9359 }
9360 if (STRCASECMP(val, "experimental_everything") == 0) {
9361 p->ctxt.shareable_constant_value = shareable_everything;
9362 return;
9363 }
9364 break;
9365 }
9366 parser_invalid_pragma_value(p, name, val);
9367}
9368
9369# if WARN_PAST_SCOPE
9370static void
9371parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9372{
9373 int b = parser_get_bool(p, name, val);
9374 if (b >= 0) p->past_scope_enabled = b;
9375}
9376# endif
9377
9378struct magic_comment {
9379 const char *name;
9380 rb_magic_comment_setter_t func;
9381 rb_magic_comment_length_t length;
9382};
9383
9384static const struct magic_comment magic_comments[] = {
9385 {"coding", magic_comment_encoding, parser_encode_length},
9386 {"encoding", magic_comment_encoding, parser_encode_length},
9387 {"frozen_string_literal", parser_set_frozen_string_literal},
9388 {"shareable_constant_value", parser_set_shareable_constant_value},
9389 {"warn_indent", parser_set_token_info},
9390# if WARN_PAST_SCOPE
9391 {"warn_past_scope", parser_set_past_scope},
9392# endif
9393};
9394
9395static const char *
9396magic_comment_marker(const char *str, long len)
9397{
9398 long i = 2;
9399
9400 while (i < len) {
9401 switch (str[i]) {
9402 case '-':
9403 if (str[i-1] == '*' && str[i-2] == '-') {
9404 return str + i + 1;
9405 }
9406 i += 2;
9407 break;
9408 case '*':
9409 if (i + 1 >= len) return 0;
9410 if (str[i+1] != '-') {
9411 i += 4;
9412 }
9413 else if (str[i-1] != '-') {
9414 i += 2;
9415 }
9416 else {
9417 return str + i + 2;
9418 }
9419 break;
9420 default:
9421 i += 3;
9422 break;
9423 }
9424 }
9425 return 0;
9426}
9427
9428static int
9429parser_magic_comment(struct parser_params *p, const char *str, long len)
9430{
9431 int indicator = 0;
9432 VALUE name = 0, val = 0;
9433 const char *beg, *end, *vbeg, *vend;
9434#define str_copy(_s, _p, _n) ((_s) \
9435 ? (void)(rb_str_resize((_s), (_n)), \
9436 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9437 : (void)((_s) = STR_NEW((_p), (_n))))
9438
9439 if (len <= 7) return FALSE;
9440 if (!!(beg = magic_comment_marker(str, len))) {
9441 if (!(end = magic_comment_marker(beg, str + len - beg)))
9442 return FALSE;
9443 indicator = TRUE;
9444 str = beg;
9445 len = end - beg - 3;
9446 }
9447
9448 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9449 while (len > 0) {
9450 const struct magic_comment *mc = magic_comments;
9451 char *s;
9452 int i;
9453 long n = 0;
9454
9455 for (; len > 0 && *str; str++, --len) {
9456 switch (*str) {
9457 case '\'': case '"': case ':': case ';':
9458 continue;
9459 }
9460 if (!ISSPACE(*str)) break;
9461 }
9462 for (beg = str; len > 0; str++, --len) {
9463 switch (*str) {
9464 case '\'': case '"': case ':': case ';':
9465 break;
9466 default:
9467 if (ISSPACE(*str)) break;
9468 continue;
9469 }
9470 break;
9471 }
9472 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9473 if (!len) break;
9474 if (*str != ':') {
9475 if (!indicator) return FALSE;
9476 continue;
9477 }
9478
9479 do str++; while (--len > 0 && ISSPACE(*str));
9480 if (!len) break;
9481 if (*str == '"') {
9482 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9483 if (*str == '\\') {
9484 --len;
9485 ++str;
9486 }
9487 }
9488 vend = str;
9489 if (len) {
9490 --len;
9491 ++str;
9492 }
9493 }
9494 else {
9495 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9496 vend = str;
9497 }
9498 if (indicator) {
9499 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9500 }
9501 else {
9502 while (len > 0 && (ISSPACE(*str))) --len, str++;
9503 if (len) return FALSE;
9504 }
9505
9506 n = end - beg;
9507 str_copy(name, beg, n);
9508 s = RSTRING_PTR(name);
9509 for (i = 0; i < n; ++i) {
9510 if (s[i] == '-') s[i] = '_';
9511 }
9512 do {
9513 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9514 n = vend - vbeg;
9515 if (mc->length) {
9516 n = (*mc->length)(p, vbeg, n);
9517 }
9518 str_copy(val, vbeg, n);
9519 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9520 break;
9521 }
9522 } while (++mc < magic_comments + numberof(magic_comments));
9523#ifdef RIPPER
9524 str_copy(val, vbeg, vend - vbeg);
9525 dispatch2(magic_comment, name, val);
9526#endif
9527 }
9528
9529 return TRUE;
9530}
9531
9532static void
9533set_file_encoding(struct parser_params *p, const char *str, const char *send)
9534{
9535 int sep = 0;
9536 const char *beg = str;
9537 VALUE s;
9538
9539 for (;;) {
9540 if (send - str <= 6) return;
9541 switch (str[6]) {
9542 case 'C': case 'c': str += 6; continue;
9543 case 'O': case 'o': str += 5; continue;
9544 case 'D': case 'd': str += 4; continue;
9545 case 'I': case 'i': str += 3; continue;
9546 case 'N': case 'n': str += 2; continue;
9547 case 'G': case 'g': str += 1; continue;
9548 case '=': case ':':
9549 sep = 1;
9550 str += 6;
9551 break;
9552 default:
9553 str += 6;
9554 if (ISSPACE(*str)) break;
9555 continue;
9556 }
9557 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9558 sep = 0;
9559 }
9560 for (;;) {
9561 do {
9562 if (++str >= send) return;
9563 } while (ISSPACE(*str));
9564 if (sep) break;
9565 if (*str != '=' && *str != ':') return;
9566 sep = 1;
9567 str++;
9568 }
9569 beg = str;
9570 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9571 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9572 parser_set_encode(p, RSTRING_PTR(s));
9573 rb_str_resize(s, 0);
9574}
9575
9576static void
9577parser_prepare(struct parser_params *p)
9578{
9579 int c = nextc0(p, FALSE);
9580 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9581 switch (c) {
9582 case '#':
9583 if (peek(p, '!')) p->has_shebang = 1;
9584 break;
9585 case 0xef: /* UTF-8 BOM marker */
9586 if (!lex_eol_n_p(p, 2) &&
9587 (unsigned char)p->lex.pcur[0] == 0xbb &&
9588 (unsigned char)p->lex.pcur[1] == 0xbf) {
9589 p->enc = rb_utf8_encoding();
9590 p->lex.pcur += 2;
9591#ifndef RIPPER
9592 if (p->debug_lines) {
9593 rb_enc_associate(p->lex.lastline, p->enc);
9594 }
9595#endif
9596 p->lex.pbeg = p->lex.pcur;
9597 token_flush(p);
9598 return;
9599 }
9600 break;
9601 case EOF:
9602 return;
9603 }
9604 pushback(p, c);
9605 p->enc = rb_enc_get(p->lex.lastline);
9606}
9607
9608#ifndef RIPPER
9609#define ambiguous_operator(tok, op, syn) ( \
9610 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
9611 rb_warning0("even though it seems like "syn""))
9612#else
9613#define ambiguous_operator(tok, op, syn) \
9614 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9615#endif
9616#define warn_balanced(tok, op, syn) ((void) \
9617 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9618 space_seen && !ISSPACE(c) && \
9619 (ambiguous_operator(tok, op, syn), 0)), \
9620 (enum yytokentype)(tok))
9621
9622static VALUE
9623parse_rational(struct parser_params *p, char *str, int len, int seen_point)
9624{
9625 VALUE v;
9626 char *point = &str[seen_point];
9627 size_t fraclen = len-seen_point-1;
9628 memmove(point, point+1, fraclen+1);
9629 v = rb_cstr_to_inum(str, 10, FALSE);
9630 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
9631}
9632
9633static enum yytokentype
9634no_digits(struct parser_params *p)
9635{
9636 yyerror0("numeric literal without digits");
9637 if (peek(p, '_')) nextc(p);
9638 /* dummy 0, for tUMINUS_NUM at numeric */
9639 return set_integer_literal(p, INT2FIX(0), 0);
9640}
9641
9642static enum yytokentype
9643parse_numeric(struct parser_params *p, int c)
9644{
9645 int is_float, seen_point, seen_e, nondigit;
9646 int suffix;
9647
9648 is_float = seen_point = seen_e = nondigit = 0;
9649 SET_LEX_STATE(EXPR_END);
9650 newtok(p);
9651 if (c == '-' || c == '+') {
9652 tokadd(p, c);
9653 c = nextc(p);
9654 }
9655 if (c == '0') {
9656 int start = toklen(p);
9657 c = nextc(p);
9658 if (c == 'x' || c == 'X') {
9659 /* hexadecimal */
9660 c = nextc(p);
9661 if (c != -1 && ISXDIGIT(c)) {
9662 do {
9663 if (c == '_') {
9664 if (nondigit) break;
9665 nondigit = c;
9666 continue;
9667 }
9668 if (!ISXDIGIT(c)) break;
9669 nondigit = 0;
9670 tokadd(p, c);
9671 } while ((c = nextc(p)) != -1);
9672 }
9673 pushback(p, c);
9674 tokfix(p);
9675 if (toklen(p) == start) {
9676 return no_digits(p);
9677 }
9678 else if (nondigit) goto trailing_uc;
9679 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9680 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
9681 }
9682 if (c == 'b' || c == 'B') {
9683 /* binary */
9684 c = nextc(p);
9685 if (c == '0' || c == '1') {
9686 do {
9687 if (c == '_') {
9688 if (nondigit) break;
9689 nondigit = c;
9690 continue;
9691 }
9692 if (c != '0' && c != '1') break;
9693 nondigit = 0;
9694 tokadd(p, c);
9695 } while ((c = nextc(p)) != -1);
9696 }
9697 pushback(p, c);
9698 tokfix(p);
9699 if (toklen(p) == start) {
9700 return no_digits(p);
9701 }
9702 else if (nondigit) goto trailing_uc;
9703 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9704 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
9705 }
9706 if (c == 'd' || c == 'D') {
9707 /* decimal */
9708 c = nextc(p);
9709 if (c != -1 && ISDIGIT(c)) {
9710 do {
9711 if (c == '_') {
9712 if (nondigit) break;
9713 nondigit = c;
9714 continue;
9715 }
9716 if (!ISDIGIT(c)) break;
9717 nondigit = 0;
9718 tokadd(p, c);
9719 } while ((c = nextc(p)) != -1);
9720 }
9721 pushback(p, c);
9722 tokfix(p);
9723 if (toklen(p) == start) {
9724 return no_digits(p);
9725 }
9726 else if (nondigit) goto trailing_uc;
9727 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9728 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9729 }
9730 if (c == '_') {
9731 /* 0_0 */
9732 goto octal_number;
9733 }
9734 if (c == 'o' || c == 'O') {
9735 /* prefixed octal */
9736 c = nextc(p);
9737 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9738 return no_digits(p);
9739 }
9740 }
9741 if (c >= '0' && c <= '7') {
9742 /* octal */
9743 octal_number:
9744 do {
9745 if (c == '_') {
9746 if (nondigit) break;
9747 nondigit = c;
9748 continue;
9749 }
9750 if (c < '0' || c > '9') break;
9751 if (c > '7') goto invalid_octal;
9752 nondigit = 0;
9753 tokadd(p, c);
9754 } while ((c = nextc(p)) != -1);
9755 if (toklen(p) > start) {
9756 pushback(p, c);
9757 tokfix(p);
9758 if (nondigit) goto trailing_uc;
9759 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9760 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
9761 }
9762 if (nondigit) {
9763 pushback(p, c);
9764 goto trailing_uc;
9765 }
9766 }
9767 if (c > '7' && c <= '9') {
9768 invalid_octal:
9769 yyerror0("Invalid octal digit");
9770 }
9771 else if (c == '.' || c == 'e' || c == 'E') {
9772 tokadd(p, '0');
9773 }
9774 else {
9775 pushback(p, c);
9776 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9777 return set_integer_literal(p, INT2FIX(0), suffix);
9778 }
9779 }
9780
9781 for (;;) {
9782 switch (c) {
9783 case '0': case '1': case '2': case '3': case '4':
9784 case '5': case '6': case '7': case '8': case '9':
9785 nondigit = 0;
9786 tokadd(p, c);
9787 break;
9788
9789 case '.':
9790 if (nondigit) goto trailing_uc;
9791 if (seen_point || seen_e) {
9792 goto decode_num;
9793 }
9794 else {
9795 int c0 = nextc(p);
9796 if (c0 == -1 || !ISDIGIT(c0)) {
9797 pushback(p, c0);
9798 goto decode_num;
9799 }
9800 c = c0;
9801 }
9802 seen_point = toklen(p);
9803 tokadd(p, '.');
9804 tokadd(p, c);
9805 is_float++;
9806 nondigit = 0;
9807 break;
9808
9809 case 'e':
9810 case 'E':
9811 if (nondigit) {
9812 pushback(p, c);
9813 c = nondigit;
9814 goto decode_num;
9815 }
9816 if (seen_e) {
9817 goto decode_num;
9818 }
9819 nondigit = c;
9820 c = nextc(p);
9821 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9822 pushback(p, c);
9823 c = nondigit;
9824 nondigit = 0;
9825 goto decode_num;
9826 }
9827 tokadd(p, nondigit);
9828 seen_e++;
9829 is_float++;
9830 tokadd(p, c);
9831 nondigit = (c == '-' || c == '+') ? c : 0;
9832 break;
9833
9834 case '_': /* `_' in number just ignored */
9835 if (nondigit) goto decode_num;
9836 nondigit = c;
9837 break;
9838
9839 default:
9840 goto decode_num;
9841 }
9842 c = nextc(p);
9843 }
9844
9845 decode_num:
9846 pushback(p, c);
9847 if (nondigit) {
9848 trailing_uc:
9849 literal_flush(p, p->lex.pcur - 1);
9850 YYLTYPE loc = RUBY_INIT_YYLLOC();
9851 compile_error(p, "trailing `%c' in number", nondigit);
9852 parser_show_error_line(p, &loc);
9853 }
9854 tokfix(p);
9855 if (is_float) {
9856 enum yytokentype type = tFLOAT;
9857 VALUE v;
9858
9859 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9860 if (suffix & NUM_SUFFIX_R) {
9861 type = tRATIONAL;
9862 v = parse_rational(p, tok(p), toklen(p), seen_point);
9863 }
9864 else {
9865 double d = strtod(tok(p), 0);
9866 if (errno == ERANGE) {
9867 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9868 errno = 0;
9869 }
9870 v = DBL2NUM(d);
9871 }
9872 return set_number_literal(p, v, type, suffix);
9873 }
9874 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9875 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9876}
9877
9878static enum yytokentype
9879parse_qmark(struct parser_params *p, int space_seen)
9880{
9881 rb_encoding *enc;
9882 register int c;
9883 VALUE lit;
9884
9885 if (IS_END()) {
9886 SET_LEX_STATE(EXPR_VALUE);
9887 return '?';
9888 }
9889 c = nextc(p);
9890 if (c == -1) {
9891 compile_error(p, "incomplete character syntax");
9892 return 0;
9893 }
9894 if (rb_enc_isspace(c, p->enc)) {
9895 if (!IS_ARG()) {
9896 int c2 = escaped_control_code(c);
9897 if (c2) {
9898 WARN_SPACE_CHAR(c2, "?");
9899 }
9900 }
9901 ternary:
9902 pushback(p, c);
9903 SET_LEX_STATE(EXPR_VALUE);
9904 return '?';
9905 }
9906 newtok(p);
9907 enc = p->enc;
9908 if (!parser_isascii(p)) {
9909 if (tokadd_mbchar(p, c) == -1) return 0;
9910 }
9911 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
9912 !lex_eol_p(p) && is_identchar(p, p->lex.pcur, p->lex.pend, p->enc)) {
9913 if (space_seen) {
9914 const char *start = p->lex.pcur - 1, *ptr = start;
9915 do {
9916 int n = parser_precise_mbclen(p, ptr);
9917 if (n < 0) return -1;
9918 ptr += n;
9919 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9920 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
9921 " a conditional operator, put a space after `?'",
9922 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9923 }
9924 goto ternary;
9925 }
9926 else if (c == '\\') {
9927 if (peek(p, 'u')) {
9928 nextc(p);
9929 enc = rb_utf8_encoding();
9930 tokadd_utf8(p, &enc, -1, 0, 0);
9931 }
9932 else if (!ISASCII(c = peekc(p))) {
9933 nextc(p);
9934 if (tokadd_mbchar(p, c) == -1) return 0;
9935 }
9936 else {
9937 c = read_escape(p, 0);
9938 tokadd(p, c);
9939 }
9940 }
9941 else {
9942 tokadd(p, c);
9943 }
9944 tokfix(p);
9945 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9946 set_yylval_str(lit);
9947 SET_LEX_STATE(EXPR_END);
9948 return tCHAR;
9949}
9950
9951static enum yytokentype
9952parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9953{
9954 register int c;
9955 const char *ptok = p->lex.pcur;
9956
9957 if (IS_BEG()) {
9958 int term;
9959 int paren;
9960
9961 c = nextc(p);
9962 quotation:
9963 if (c == -1) goto unterminated;
9964 if (!ISALNUM(c)) {
9965 term = c;
9966 if (!ISASCII(c)) goto unknown;
9967 c = 'Q';
9968 }
9969 else {
9970 term = nextc(p);
9971 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
9972 unknown:
9973 pushback(p, term);
9974 c = parser_precise_mbclen(p, p->lex.pcur);
9975 if (c < 0) return 0;
9976 p->lex.pcur += c;
9977 yyerror0("unknown type of %string");
9978 return 0;
9979 }
9980 }
9981 if (term == -1) {
9982 unterminated:
9983 compile_error(p, "unterminated quoted string meets end of file");
9984 return 0;
9985 }
9986 paren = term;
9987 if (term == '(') term = ')';
9988 else if (term == '[') term = ']';
9989 else if (term == '{') term = '}';
9990 else if (term == '<') term = '>';
9991 else paren = 0;
9992
9993 p->lex.ptok = ptok-1;
9994 switch (c) {
9995 case 'Q':
9996 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
9997 return tSTRING_BEG;
9998
9999 case 'q':
10000 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10001 return tSTRING_BEG;
10002
10003 case 'W':
10004 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10005 return tWORDS_BEG;
10006
10007 case 'w':
10008 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10009 return tQWORDS_BEG;
10010
10011 case 'I':
10012 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10013 return tSYMBOLS_BEG;
10014
10015 case 'i':
10016 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10017 return tQSYMBOLS_BEG;
10018
10019 case 'x':
10020 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10021 return tXSTRING_BEG;
10022
10023 case 'r':
10024 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10025 return tREGEXP_BEG;
10026
10027 case 's':
10028 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10029 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10030 return tSYMBEG;
10031
10032 default:
10033 yyerror0("unknown type of %string");
10034 return 0;
10035 }
10036 }
10037 if ((c = nextc(p)) == '=') {
10038 set_yylval_id('%');
10039 SET_LEX_STATE(EXPR_BEG);
10040 return tOP_ASGN;
10041 }
10042 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10043 goto quotation;
10044 }
10045 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10046 pushback(p, c);
10047 return warn_balanced('%', "%%", "string literal");
10048}
10049
10050static int
10051tokadd_ident(struct parser_params *p, int c)
10052{
10053 do {
10054 if (tokadd_mbchar(p, c) == -1) return -1;
10055 c = nextc(p);
10056 } while (parser_is_identchar(p));
10057 pushback(p, c);
10058 return 0;
10059}
10060
10061static ID
10062tokenize_ident(struct parser_params *p)
10063{
10064 ID ident = TOK_INTERN();
10065
10066 set_yylval_name(ident);
10067
10068 return ident;
10069}
10070
10071static int
10072parse_numvar(struct parser_params *p)
10073{
10074 size_t len;
10075 int overflow;
10076 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10077 const unsigned long nth_ref_max =
10078 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10079 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10080 * turned into a Fixnum, in compile.c */
10081
10082 if (overflow || n > nth_ref_max) {
10083 /* compile_error()? */
10084 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10085 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10086 }
10087 else {
10088 return (int)n;
10089 }
10090}
10091
10092static enum yytokentype
10093parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10094{
10095 const char *ptr = p->lex.pcur;
10096 register int c;
10097
10098 SET_LEX_STATE(EXPR_END);
10099 p->lex.ptok = ptr - 1; /* from '$' */
10100 newtok(p);
10101 c = nextc(p);
10102 switch (c) {
10103 case '_': /* $_: last read line string */
10104 c = nextc(p);
10105 if (parser_is_identchar(p)) {
10106 tokadd(p, '$');
10107 tokadd(p, '_');
10108 break;
10109 }
10110 pushback(p, c);
10111 c = '_';
10112 /* fall through */
10113 case '~': /* $~: match-data */
10114 case '*': /* $*: argv */
10115 case '$': /* $$: pid */
10116 case '?': /* $?: last status */
10117 case '!': /* $!: error string */
10118 case '@': /* $@: error position */
10119 case '/': /* $/: input record separator */
10120 case '\\': /* $\: output record separator */
10121 case ';': /* $;: field separator */
10122 case ',': /* $,: output field separator */
10123 case '.': /* $.: last read line number */
10124 case '=': /* $=: ignorecase */
10125 case ':': /* $:: load path */
10126 case '<': /* $<: reading filename */
10127 case '>': /* $>: default output handle */
10128 case '\"': /* $": already loaded files */
10129 tokadd(p, '$');
10130 tokadd(p, c);
10131 goto gvar;
10132
10133 case '-':
10134 tokadd(p, '$');
10135 tokadd(p, c);
10136 c = nextc(p);
10137 if (parser_is_identchar(p)) {
10138 if (tokadd_mbchar(p, c) == -1) return 0;
10139 }
10140 else {
10141 pushback(p, c);
10142 pushback(p, '-');
10143 return '$';
10144 }
10145 gvar:
10146 set_yylval_name(TOK_INTERN());
10147 return tGVAR;
10148
10149 case '&': /* $&: last match */
10150 case '`': /* $`: string before last match */
10151 case '\'': /* $': string after last match */
10152 case '+': /* $+: string matches last paren. */
10153 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10154 tokadd(p, '$');
10155 tokadd(p, c);
10156 goto gvar;
10157 }
10158 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10159 return tBACK_REF;
10160
10161 case '1': case '2': case '3':
10162 case '4': case '5': case '6':
10163 case '7': case '8': case '9':
10164 tokadd(p, '$');
10165 do {
10166 tokadd(p, c);
10167 c = nextc(p);
10168 } while (c != -1 && ISDIGIT(c));
10169 pushback(p, c);
10170 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10171 tokfix(p);
10172 c = parse_numvar(p);
10173 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10174 return tNTH_REF;
10175
10176 default:
10177 if (!parser_is_identchar(p)) {
10178 YYLTYPE loc = RUBY_INIT_YYLLOC();
10179 if (c == -1 || ISSPACE(c)) {
10180 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
10181 }
10182 else {
10183 pushback(p, c);
10184 compile_error(p, "`$%c' is not allowed as a global variable name", c);
10185 }
10186 parser_show_error_line(p, &loc);
10187 set_yylval_noname();
10188 return tGVAR;
10189 }
10190 /* fall through */
10191 case '0':
10192 tokadd(p, '$');
10193 }
10194
10195 if (tokadd_ident(p, c)) return 0;
10196 SET_LEX_STATE(EXPR_END);
10197 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10198 tokenize_ident(p);
10199 }
10200 else {
10201 compile_error(p, "`%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10202 set_yylval_noname();
10203 }
10204 return tGVAR;
10205}
10206
10207#ifndef RIPPER
10208static bool
10209parser_numbered_param(struct parser_params *p, int n)
10210{
10211 if (n < 0) return false;
10212
10213 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10214 return false;
10215 }
10216 if (p->max_numparam == ORDINAL_PARAM) {
10217 compile_error(p, "ordinary parameter is defined");
10218 return false;
10219 }
10220 struct vtable *args = p->lvtbl->args;
10221 if (p->max_numparam < n) {
10222 p->max_numparam = n;
10223 }
10224 while (n > args->pos) {
10225 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10226 }
10227 return true;
10228}
10229#endif
10230
10231static enum yytokentype
10232parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10233{
10234 const char *ptr = p->lex.pcur;
10235 enum yytokentype result = tIVAR;
10236 register int c = nextc(p);
10237 YYLTYPE loc;
10238
10239 p->lex.ptok = ptr - 1; /* from '@' */
10240 newtok(p);
10241 tokadd(p, '@');
10242 if (c == '@') {
10243 result = tCVAR;
10244 tokadd(p, '@');
10245 c = nextc(p);
10246 }
10247 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10248 if (c == -1 || !parser_is_identchar(p)) {
10249 pushback(p, c);
10250 RUBY_SET_YYLLOC(loc);
10251 if (result == tIVAR) {
10252 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
10253 }
10254 else {
10255 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
10256 }
10257 parser_show_error_line(p, &loc);
10258 set_yylval_noname();
10259 SET_LEX_STATE(EXPR_END);
10260 return result;
10261 }
10262 else if (ISDIGIT(c)) {
10263 pushback(p, c);
10264 RUBY_SET_YYLLOC(loc);
10265 if (result == tIVAR) {
10266 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
10267 }
10268 else {
10269 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
10270 }
10271 parser_show_error_line(p, &loc);
10272 set_yylval_noname();
10273 SET_LEX_STATE(EXPR_END);
10274 return result;
10275 }
10276
10277 if (tokadd_ident(p, c)) return 0;
10278 tokenize_ident(p);
10279 return result;
10280}
10281
10282static enum yytokentype
10283parse_ident(struct parser_params *p, int c, int cmd_state)
10284{
10285 enum yytokentype result;
10286 int mb = ENC_CODERANGE_7BIT;
10287 const enum lex_state_e last_state = p->lex.state;
10288 ID ident;
10289 int enforce_keyword_end = 0;
10290
10291 do {
10292 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
10293 if (tokadd_mbchar(p, c) == -1) return 0;
10294 c = nextc(p);
10295 } while (parser_is_identchar(p));
10296 if ((c == '!' || c == '?') && !peek(p, '=')) {
10297 result = tFID;
10298 tokadd(p, c);
10299 }
10300 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10301 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10302 result = tIDENTIFIER;
10303 tokadd(p, c);
10304 }
10305 else {
10306 result = tCONSTANT; /* assume provisionally */
10307 pushback(p, c);
10308 }
10309 tokfix(p);
10310
10311 if (IS_LABEL_POSSIBLE()) {
10312 if (IS_LABEL_SUFFIX(0)) {
10313 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10314 nextc(p);
10315 set_yylval_name(TOK_INTERN());
10316 return tLABEL;
10317 }
10318 }
10319
10320#ifndef RIPPER
10321 if (!NIL_P(peek_end_expect_token_locations(p))) {
10322 VALUE end_loc;
10323 int lineno, column;
10324 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10325
10326 end_loc = peek_end_expect_token_locations(p);
10327 lineno = NUM2INT(rb_ary_entry(end_loc, 0));
10328 column = NUM2INT(rb_ary_entry(end_loc, 1));
10329
10330 if (p->debug) {
10331 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10332 p->ruby_sourceline, beg_pos, lineno, column);
10333 }
10334
10335 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10336 const struct kwtable *kw;
10337
10338 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10339 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10340 enforce_keyword_end = 1;
10341 }
10342 }
10343 }
10344#endif
10345
10346 if (mb == ENC_CODERANGE_7BIT && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10347 const struct kwtable *kw;
10348
10349 /* See if it is a reserved word. */
10350 kw = rb_reserved_word(tok(p), toklen(p));
10351 if (kw) {
10352 enum lex_state_e state = p->lex.state;
10353 if (IS_lex_state_for(state, EXPR_FNAME)) {
10354 SET_LEX_STATE(EXPR_ENDFN);
10355 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10356 return kw->id[0];
10357 }
10358 SET_LEX_STATE(kw->state);
10359 if (IS_lex_state(EXPR_BEG)) {
10360 p->command_start = TRUE;
10361 }
10362 if (kw->id[0] == keyword_do) {
10363 if (lambda_beginning_p()) {
10364 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10365 return keyword_do_LAMBDA;
10366 }
10367 if (COND_P()) return keyword_do_cond;
10368 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10369 return keyword_do_block;
10370 return keyword_do;
10371 }
10372 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10373 return kw->id[0];
10374 else {
10375 if (kw->id[0] != kw->id[1])
10376 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10377 return kw->id[1];
10378 }
10379 }
10380 }
10381
10382 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10383 if (cmd_state) {
10384 SET_LEX_STATE(EXPR_CMDARG);
10385 }
10386 else {
10387 SET_LEX_STATE(EXPR_ARG);
10388 }
10389 }
10390 else if (p->lex.state == EXPR_FNAME) {
10391 SET_LEX_STATE(EXPR_ENDFN);
10392 }
10393 else {
10394 SET_LEX_STATE(EXPR_END);
10395 }
10396
10397 ident = tokenize_ident(p);
10398 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10399 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10400 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10401 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10402 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10403 }
10404 return result;
10405}
10406
10407static void
10408warn_cr(struct parser_params *p)
10409{
10410 if (!p->cr_seen) {
10411 p->cr_seen = TRUE;
10412 /* carried over with p->lex.nextline for nextc() */
10413 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10414 }
10415}
10416
10417static enum yytokentype
10418parser_yylex(struct parser_params *p)
10419{
10420 register int c;
10421 int space_seen = 0;
10422 int cmd_state;
10423 int label;
10424 enum lex_state_e last_state;
10425 int fallthru = FALSE;
10426 int token_seen = p->token_seen;
10427
10428 if (p->lex.strterm) {
10429 if (strterm_is_heredoc(p->lex.strterm)) {
10430 token_flush(p);
10431 return here_document(p, &p->lex.strterm->u.heredoc);
10432 }
10433 else {
10434 token_flush(p);
10435 return parse_string(p, &p->lex.strterm->u.literal);
10436 }
10437 }
10438 cmd_state = p->command_start;
10439 p->command_start = FALSE;
10440 p->token_seen = TRUE;
10441#ifndef RIPPER
10442 token_flush(p);
10443#endif
10444 retry:
10445 last_state = p->lex.state;
10446 switch (c = nextc(p)) {
10447 case '\0': /* NUL */
10448 case '\004': /* ^D */
10449 case '\032': /* ^Z */
10450 case -1: /* end of script. */
10451 p->eofp = 1;
10452#ifndef RIPPER
10453 if (!NIL_P(p->end_expect_token_locations) && RARRAY_LEN(p->end_expect_token_locations) > 0) {
10454 pop_end_expect_token_locations(p);
10455 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10456 return tDUMNY_END;
10457 }
10458#endif
10459 /* Set location for end-of-input because dispatch_scan_event is not called. */
10460 RUBY_SET_YYLLOC(*p->yylloc);
10461 return END_OF_INPUT;
10462
10463 /* white spaces */
10464 case '\r':
10465 warn_cr(p);
10466 /* fall through */
10467 case ' ': case '\t': case '\f':
10468 case '\13': /* '\v' */
10469 space_seen = 1;
10470 while ((c = nextc(p))) {
10471 switch (c) {
10472 case '\r':
10473 warn_cr(p);
10474 /* fall through */
10475 case ' ': case '\t': case '\f':
10476 case '\13': /* '\v' */
10477 break;
10478 default:
10479 goto outofloop;
10480 }
10481 }
10482 outofloop:
10483 pushback(p, c);
10484 dispatch_scan_event(p, tSP);
10485#ifndef RIPPER
10486 token_flush(p);
10487#endif
10488 goto retry;
10489
10490 case '#': /* it's a comment */
10491 p->token_seen = token_seen;
10492 /* no magic_comment in shebang line */
10493 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10494 if (comment_at_top(p)) {
10495 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10496 }
10497 }
10498 lex_goto_eol(p);
10499 dispatch_scan_event(p, tCOMMENT);
10500 fallthru = TRUE;
10501 /* fall through */
10502 case '\n':
10503 p->token_seen = token_seen;
10504 VALUE prevline = p->lex.lastline;
10505 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10506 !IS_lex_state(EXPR_LABELED));
10507 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10508 if (!fallthru) {
10509 dispatch_scan_event(p, tIGNORED_NL);
10510 }
10511 fallthru = FALSE;
10512 if (!c && p->ctxt.in_kwarg) {
10513 goto normal_newline;
10514 }
10515 goto retry;
10516 }
10517 while (1) {
10518 switch (c = nextc(p)) {
10519 case ' ': case '\t': case '\f': case '\r':
10520 case '\13': /* '\v' */
10521 space_seen = 1;
10522 break;
10523 case '#':
10524 pushback(p, c);
10525 if (space_seen) {
10526 dispatch_scan_event(p, tSP);
10527 token_flush(p);
10528 }
10529 goto retry;
10530 case '&':
10531 case '.': {
10532 dispatch_delayed_token(p, tIGNORED_NL);
10533 if (peek(p, '.') == (c == '&')) {
10534 pushback(p, c);
10535 dispatch_scan_event(p, tSP);
10536 goto retry;
10537 }
10538 }
10539 default:
10540 p->ruby_sourceline--;
10541 p->lex.nextline = p->lex.lastline;
10542 set_lastline(p, prevline);
10543 case -1: /* EOF no decrement*/
10544 lex_goto_eol(p);
10545 if (c != -1) {
10546 token_flush(p);
10547 RUBY_SET_YYLLOC(*p->yylloc);
10548 }
10549 goto normal_newline;
10550 }
10551 }
10552 normal_newline:
10553 p->command_start = TRUE;
10554 SET_LEX_STATE(EXPR_BEG);
10555 return '\n';
10556
10557 case '*':
10558 if ((c = nextc(p)) == '*') {
10559 if ((c = nextc(p)) == '=') {
10560 set_yylval_id(idPow);
10561 SET_LEX_STATE(EXPR_BEG);
10562 return tOP_ASGN;
10563 }
10564 pushback(p, c);
10565 if (IS_SPCARG(c)) {
10566 rb_warning0("`**' interpreted as argument prefix");
10567 c = tDSTAR;
10568 }
10569 else if (IS_BEG()) {
10570 c = tDSTAR;
10571 }
10572 else {
10573 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10574 }
10575 }
10576 else {
10577 if (c == '=') {
10578 set_yylval_id('*');
10579 SET_LEX_STATE(EXPR_BEG);
10580 return tOP_ASGN;
10581 }
10582 pushback(p, c);
10583 if (IS_SPCARG(c)) {
10584 rb_warning0("`*' interpreted as argument prefix");
10585 c = tSTAR;
10586 }
10587 else if (IS_BEG()) {
10588 c = tSTAR;
10589 }
10590 else {
10591 c = warn_balanced('*', "*", "argument prefix");
10592 }
10593 }
10594 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10595 return c;
10596
10597 case '!':
10598 c = nextc(p);
10599 if (IS_AFTER_OPERATOR()) {
10600 SET_LEX_STATE(EXPR_ARG);
10601 if (c == '@') {
10602 return '!';
10603 }
10604 }
10605 else {
10606 SET_LEX_STATE(EXPR_BEG);
10607 }
10608 if (c == '=') {
10609 return tNEQ;
10610 }
10611 if (c == '~') {
10612 return tNMATCH;
10613 }
10614 pushback(p, c);
10615 return '!';
10616
10617 case '=':
10618 if (was_bol(p)) {
10619 /* skip embedded rd document */
10620 if (word_match_p(p, "begin", 5)) {
10621 int first_p = TRUE;
10622
10623 lex_goto_eol(p);
10624 dispatch_scan_event(p, tEMBDOC_BEG);
10625 for (;;) {
10626 lex_goto_eol(p);
10627 if (!first_p) {
10628 dispatch_scan_event(p, tEMBDOC);
10629 }
10630 first_p = FALSE;
10631 c = nextc(p);
10632 if (c == -1) {
10633 compile_error(p, "embedded document meets end of file");
10634 return END_OF_INPUT;
10635 }
10636 if (c == '=' && word_match_p(p, "end", 3)) {
10637 break;
10638 }
10639 pushback(p, c);
10640 }
10641 lex_goto_eol(p);
10642 dispatch_scan_event(p, tEMBDOC_END);
10643 goto retry;
10644 }
10645 }
10646
10647 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10648 if ((c = nextc(p)) == '=') {
10649 if ((c = nextc(p)) == '=') {
10650 return tEQQ;
10651 }
10652 pushback(p, c);
10653 return tEQ;
10654 }
10655 if (c == '~') {
10656 return tMATCH;
10657 }
10658 else if (c == '>') {
10659 return tASSOC;
10660 }
10661 pushback(p, c);
10662 return '=';
10663
10664 case '<':
10665 c = nextc(p);
10666 if (c == '<' &&
10667 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10668 !IS_END() &&
10669 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10670 enum yytokentype token = heredoc_identifier(p);
10671 if (token) return token < 0 ? 0 : token;
10672 }
10673 if (IS_AFTER_OPERATOR()) {
10674 SET_LEX_STATE(EXPR_ARG);
10675 }
10676 else {
10677 if (IS_lex_state(EXPR_CLASS))
10678 p->command_start = TRUE;
10679 SET_LEX_STATE(EXPR_BEG);
10680 }
10681 if (c == '=') {
10682 if ((c = nextc(p)) == '>') {
10683 return tCMP;
10684 }
10685 pushback(p, c);
10686 return tLEQ;
10687 }
10688 if (c == '<') {
10689 if ((c = nextc(p)) == '=') {
10690 set_yylval_id(idLTLT);
10691 SET_LEX_STATE(EXPR_BEG);
10692 return tOP_ASGN;
10693 }
10694 pushback(p, c);
10695 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10696 }
10697 pushback(p, c);
10698 return '<';
10699
10700 case '>':
10701 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10702 if ((c = nextc(p)) == '=') {
10703 return tGEQ;
10704 }
10705 if (c == '>') {
10706 if ((c = nextc(p)) == '=') {
10707 set_yylval_id(idGTGT);
10708 SET_LEX_STATE(EXPR_BEG);
10709 return tOP_ASGN;
10710 }
10711 pushback(p, c);
10712 return tRSHFT;
10713 }
10714 pushback(p, c);
10715 return '>';
10716
10717 case '"':
10718 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10719 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10720 p->lex.ptok = p->lex.pcur-1;
10721 return tSTRING_BEG;
10722
10723 case '`':
10724 if (IS_lex_state(EXPR_FNAME)) {
10725 SET_LEX_STATE(EXPR_ENDFN);
10726 return c;
10727 }
10728 if (IS_lex_state(EXPR_DOT)) {
10729 if (cmd_state)
10730 SET_LEX_STATE(EXPR_CMDARG);
10731 else
10732 SET_LEX_STATE(EXPR_ARG);
10733 return c;
10734 }
10735 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10736 return tXSTRING_BEG;
10737
10738 case '\'':
10739 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10740 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10741 p->lex.ptok = p->lex.pcur-1;
10742 return tSTRING_BEG;
10743
10744 case '?':
10745 return parse_qmark(p, space_seen);
10746
10747 case '&':
10748 if ((c = nextc(p)) == '&') {
10749 SET_LEX_STATE(EXPR_BEG);
10750 if ((c = nextc(p)) == '=') {
10751 set_yylval_id(idANDOP);
10752 SET_LEX_STATE(EXPR_BEG);
10753 return tOP_ASGN;
10754 }
10755 pushback(p, c);
10756 return tANDOP;
10757 }
10758 else if (c == '=') {
10759 set_yylval_id('&');
10760 SET_LEX_STATE(EXPR_BEG);
10761 return tOP_ASGN;
10762 }
10763 else if (c == '.') {
10764 set_yylval_id(idANDDOT);
10765 SET_LEX_STATE(EXPR_DOT);
10766 return tANDDOT;
10767 }
10768 pushback(p, c);
10769 if (IS_SPCARG(c)) {
10770 if ((c != ':') ||
10771 (c = peekc_n(p, 1)) == -1 ||
10772 !(c == '\'' || c == '"' ||
10773 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10774 rb_warning0("`&' interpreted as argument prefix");
10775 }
10776 c = tAMPER;
10777 }
10778 else if (IS_BEG()) {
10779 c = tAMPER;
10780 }
10781 else {
10782 c = warn_balanced('&', "&", "argument prefix");
10783 }
10784 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10785 return c;
10786
10787 case '|':
10788 if ((c = nextc(p)) == '|') {
10789 SET_LEX_STATE(EXPR_BEG);
10790 if ((c = nextc(p)) == '=') {
10791 set_yylval_id(idOROP);
10792 SET_LEX_STATE(EXPR_BEG);
10793 return tOP_ASGN;
10794 }
10795 pushback(p, c);
10796 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10797 c = '|';
10798 pushback(p, '|');
10799 return c;
10800 }
10801 return tOROP;
10802 }
10803 if (c == '=') {
10804 set_yylval_id('|');
10805 SET_LEX_STATE(EXPR_BEG);
10806 return tOP_ASGN;
10807 }
10808 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10809 pushback(p, c);
10810 return '|';
10811
10812 case '+':
10813 c = nextc(p);
10814 if (IS_AFTER_OPERATOR()) {
10815 SET_LEX_STATE(EXPR_ARG);
10816 if (c == '@') {
10817 return tUPLUS;
10818 }
10819 pushback(p, c);
10820 return '+';
10821 }
10822 if (c == '=') {
10823 set_yylval_id('+');
10824 SET_LEX_STATE(EXPR_BEG);
10825 return tOP_ASGN;
10826 }
10827 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10828 SET_LEX_STATE(EXPR_BEG);
10829 pushback(p, c);
10830 if (c != -1 && ISDIGIT(c)) {
10831 return parse_numeric(p, '+');
10832 }
10833 return tUPLUS;
10834 }
10835 SET_LEX_STATE(EXPR_BEG);
10836 pushback(p, c);
10837 return warn_balanced('+', "+", "unary operator");
10838
10839 case '-':
10840 c = nextc(p);
10841 if (IS_AFTER_OPERATOR()) {
10842 SET_LEX_STATE(EXPR_ARG);
10843 if (c == '@') {
10844 return tUMINUS;
10845 }
10846 pushback(p, c);
10847 return '-';
10848 }
10849 if (c == '=') {
10850 set_yylval_id('-');
10851 SET_LEX_STATE(EXPR_BEG);
10852 return tOP_ASGN;
10853 }
10854 if (c == '>') {
10855 SET_LEX_STATE(EXPR_ENDFN);
10856 return tLAMBDA;
10857 }
10858 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10859 SET_LEX_STATE(EXPR_BEG);
10860 pushback(p, c);
10861 if (c != -1 && ISDIGIT(c)) {
10862 return tUMINUS_NUM;
10863 }
10864 return tUMINUS;
10865 }
10866 SET_LEX_STATE(EXPR_BEG);
10867 pushback(p, c);
10868 return warn_balanced('-', "-", "unary operator");
10869
10870 case '.': {
10871 int is_beg = IS_BEG();
10872 SET_LEX_STATE(EXPR_BEG);
10873 if ((c = nextc(p)) == '.') {
10874 if ((c = nextc(p)) == '.') {
10875 if (p->ctxt.in_argdef) {
10876 SET_LEX_STATE(EXPR_ENDARG);
10877 return tBDOT3;
10878 }
10879 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10880 rb_warn0("... at EOL, should be parenthesized?");
10881 }
10882 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
10883 if (IS_lex_state_for(last_state, EXPR_LABEL))
10884 return tDOT3;
10885 }
10886 return is_beg ? tBDOT3 : tDOT3;
10887 }
10888 pushback(p, c);
10889 return is_beg ? tBDOT2 : tDOT2;
10890 }
10891 pushback(p, c);
10892 if (c != -1 && ISDIGIT(c)) {
10893 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10894 parse_numeric(p, '.');
10895 if (ISDIGIT(prev)) {
10896 yyerror0("unexpected fraction part after numeric literal");
10897 }
10898 else {
10899 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10900 }
10901 SET_LEX_STATE(EXPR_END);
10902 p->lex.ptok = p->lex.pcur;
10903 goto retry;
10904 }
10905 set_yylval_id('.');
10906 SET_LEX_STATE(EXPR_DOT);
10907 return '.';
10908 }
10909
10910 case '0': case '1': case '2': case '3': case '4':
10911 case '5': case '6': case '7': case '8': case '9':
10912 return parse_numeric(p, c);
10913
10914 case ')':
10915 COND_POP();
10916 CMDARG_POP();
10917 SET_LEX_STATE(EXPR_ENDFN);
10918 p->lex.paren_nest--;
10919 return c;
10920
10921 case ']':
10922 COND_POP();
10923 CMDARG_POP();
10924 SET_LEX_STATE(EXPR_END);
10925 p->lex.paren_nest--;
10926 return c;
10927
10928 case '}':
10929 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10930 if (!p->lex.brace_nest--) return tSTRING_DEND;
10931 COND_POP();
10932 CMDARG_POP();
10933 SET_LEX_STATE(EXPR_END);
10934 p->lex.paren_nest--;
10935 return c;
10936
10937 case ':':
10938 c = nextc(p);
10939 if (c == ':') {
10940 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10941 SET_LEX_STATE(EXPR_BEG);
10942 return tCOLON3;
10943 }
10944 set_yylval_id(idCOLON2);
10945 SET_LEX_STATE(EXPR_DOT);
10946 return tCOLON2;
10947 }
10948 if (IS_END() || ISSPACE(c) || c == '#') {
10949 pushback(p, c);
10950 c = warn_balanced(':', ":", "symbol literal");
10951 SET_LEX_STATE(EXPR_BEG);
10952 return c;
10953 }
10954 switch (c) {
10955 case '\'':
10956 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
10957 break;
10958 case '"':
10959 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
10960 break;
10961 default:
10962 pushback(p, c);
10963 break;
10964 }
10965 SET_LEX_STATE(EXPR_FNAME);
10966 return tSYMBEG;
10967
10968 case '/':
10969 if (IS_BEG()) {
10970 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10971 return tREGEXP_BEG;
10972 }
10973 if ((c = nextc(p)) == '=') {
10974 set_yylval_id('/');
10975 SET_LEX_STATE(EXPR_BEG);
10976 return tOP_ASGN;
10977 }
10978 pushback(p, c);
10979 if (IS_SPCARG(c)) {
10980 arg_ambiguous(p, '/');
10981 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10982 return tREGEXP_BEG;
10983 }
10984 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10985 return warn_balanced('/', "/", "regexp literal");
10986
10987 case '^':
10988 if ((c = nextc(p)) == '=') {
10989 set_yylval_id('^');
10990 SET_LEX_STATE(EXPR_BEG);
10991 return tOP_ASGN;
10992 }
10993 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10994 pushback(p, c);
10995 return '^';
10996
10997 case ';':
10998 SET_LEX_STATE(EXPR_BEG);
10999 p->command_start = TRUE;
11000 return ';';
11001
11002 case ',':
11003 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11004 return ',';
11005
11006 case '~':
11007 if (IS_AFTER_OPERATOR()) {
11008 if ((c = nextc(p)) != '@') {
11009 pushback(p, c);
11010 }
11011 SET_LEX_STATE(EXPR_ARG);
11012 }
11013 else {
11014 SET_LEX_STATE(EXPR_BEG);
11015 }
11016 return '~';
11017
11018 case '(':
11019 if (IS_BEG()) {
11020 c = tLPAREN;
11021 }
11022 else if (!space_seen) {
11023 /* foo( ... ) => method call, no ambiguity */
11024 }
11025 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11026 c = tLPAREN_ARG;
11027 }
11028 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11029 rb_warning0("parentheses after method name is interpreted as "
11030 "an argument list, not a decomposed argument");
11031 }
11032 p->lex.paren_nest++;
11033 COND_PUSH(0);
11034 CMDARG_PUSH(0);
11035 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11036 return c;
11037
11038 case '[':
11039 p->lex.paren_nest++;
11040 if (IS_AFTER_OPERATOR()) {
11041 if ((c = nextc(p)) == ']') {
11042 p->lex.paren_nest--;
11043 SET_LEX_STATE(EXPR_ARG);
11044 if ((c = nextc(p)) == '=') {
11045 return tASET;
11046 }
11047 pushback(p, c);
11048 return tAREF;
11049 }
11050 pushback(p, c);
11051 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11052 return '[';
11053 }
11054 else if (IS_BEG()) {
11055 c = tLBRACK;
11056 }
11057 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11058 c = tLBRACK;
11059 }
11060 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11061 COND_PUSH(0);
11062 CMDARG_PUSH(0);
11063 return c;
11064
11065 case '{':
11066 ++p->lex.brace_nest;
11067 if (lambda_beginning_p())
11068 c = tLAMBEG;
11069 else if (IS_lex_state(EXPR_LABELED))
11070 c = tLBRACE; /* hash */
11071 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11072 c = '{'; /* block (primary) */
11073 else if (IS_lex_state(EXPR_ENDARG))
11074 c = tLBRACE_ARG; /* block (expr) */
11075 else
11076 c = tLBRACE; /* hash */
11077 if (c != tLBRACE) {
11078 p->command_start = TRUE;
11079 SET_LEX_STATE(EXPR_BEG);
11080 }
11081 else {
11082 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11083 }
11084 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11085 COND_PUSH(0);
11086 CMDARG_PUSH(0);
11087 return c;
11088
11089 case '\\':
11090 c = nextc(p);
11091 if (c == '\n') {
11092 space_seen = 1;
11093 dispatch_scan_event(p, tSP);
11094 goto retry; /* skip \\n */
11095 }
11096 if (c == ' ') return tSP;
11097 if (ISSPACE(c)) return c;
11098 pushback(p, c);
11099 return '\\';
11100
11101 case '%':
11102 return parse_percent(p, space_seen, last_state);
11103
11104 case '$':
11105 return parse_gvar(p, last_state);
11106
11107 case '@':
11108 return parse_atmark(p, last_state);
11109
11110 case '_':
11111 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11112 p->ruby__end__seen = 1;
11113 p->eofp = 1;
11114#ifdef RIPPER
11115 lex_goto_eol(p);
11116 dispatch_scan_event(p, k__END__);
11117#endif
11118 return END_OF_INPUT;
11119 }
11120 newtok(p);
11121 break;
11122
11123 default:
11124 if (!parser_is_identchar(p)) {
11125 compile_error(p, "Invalid char `\\x%02X' in expression", c);
11126 token_flush(p);
11127 goto retry;
11128 }
11129
11130 newtok(p);
11131 break;
11132 }
11133
11134 return parse_ident(p, c, cmd_state);
11135}
11136
11137static enum yytokentype
11138yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11139{
11140 enum yytokentype t;
11141
11142 p->lval = lval;
11143 lval->val = Qundef;
11144 p->yylloc = yylloc;
11145
11146 t = parser_yylex(p);
11147
11148 if (has_delayed_token(p))
11149 dispatch_delayed_token(p, t);
11150 else if (t != END_OF_INPUT)
11151 dispatch_scan_event(p, t);
11152
11153 return t;
11154}
11155
11156#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11157
11158static NODE*
11159node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11160{
11161 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11162
11163 rb_node_init(n, type);
11164 return n;
11165}
11166
11167static NODE *
11168nd_set_loc(NODE *nd, const YYLTYPE *loc)
11169{
11170 nd->nd_loc = *loc;
11171 nd_set_line(nd, loc->beg_pos.lineno);
11172 return nd;
11173}
11174
11175static NODE*
11176node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11177{
11178 NODE *n = node_new_internal(p, type, size, alignment);
11179
11180 nd_set_loc(n, loc);
11181 nd_set_node_id(n, parser_get_node_id(p));
11182 return n;
11183}
11184
11185#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11186
11187#ifndef RIPPER
11188
11189static rb_node_scope_t *
11190rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11191{
11192 rb_ast_id_table_t *nd_tbl;
11193 nd_tbl = local_tbl(p);
11194 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11195 n->nd_tbl = nd_tbl;
11196 n->nd_body = nd_body;
11197 n->nd_args = nd_args;
11198
11199 return n;
11200}
11201
11202static rb_node_scope_t *
11203rb_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)
11204{
11205 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11206 n->nd_tbl = nd_tbl;
11207 n->nd_body = nd_body;
11208 n->nd_args = nd_args;
11209
11210 return n;
11211}
11212
11213static rb_node_defn_t *
11214rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11215{
11216 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11217 n->nd_mid = nd_mid;
11218 n->nd_defn = nd_defn;
11219
11220 return n;
11221}
11222
11223static rb_node_defs_t *
11224rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11225{
11226 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11227 n->nd_recv = nd_recv;
11228 n->nd_mid = nd_mid;
11229 n->nd_defn = nd_defn;
11230
11231 return n;
11232}
11233
11234static rb_node_block_t *
11235rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11236{
11237 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11238 n->nd_head = nd_head;
11239 n->nd_end = 0;
11240 n->nd_next = 0;
11241
11242 return n;
11243}
11244
11245static rb_node_for_t *
11246rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc)
11247{
11248 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11249 n->nd_body = nd_body;
11250 n->nd_iter = nd_iter;
11251
11252 return n;
11253}
11254
11255static rb_node_for_masgn_t *
11256rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11257{
11258 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11259 n->nd_var = nd_var;
11260
11261 return n;
11262}
11263
11264static rb_node_retry_t *
11265rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11266{
11267 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11268
11269 return n;
11270}
11271
11272static rb_node_begin_t *
11273rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11274{
11275 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11276 n->nd_body = nd_body;
11277
11278 return n;
11279}
11280
11281static rb_node_rescue_t *
11282rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11283{
11284 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11285 n->nd_head = nd_head;
11286 n->nd_resq = nd_resq;
11287 n->nd_else = nd_else;
11288
11289 return n;
11290}
11291
11292static rb_node_resbody_t *
11293rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_body, NODE *nd_head, const YYLTYPE *loc)
11294{
11295 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11296 n->nd_head = nd_head;
11297 n->nd_body = nd_body;
11298 n->nd_args = nd_args;
11299
11300 return n;
11301}
11302
11303static rb_node_ensure_t *
11304rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11305{
11306 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11307 n->nd_head = nd_head;
11308 n->nd_resq = 0;
11309 n->nd_ensr = nd_ensr;
11310
11311 return n;
11312}
11313
11314static rb_node_and_t *
11315rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
11316{
11317 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11318 n->nd_1st = nd_1st;
11319 n->nd_2nd = nd_2nd;
11320
11321 return n;
11322}
11323
11324static rb_node_or_t *
11325rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
11326{
11327 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11328 n->nd_1st = nd_1st;
11329 n->nd_2nd = nd_2nd;
11330
11331 return n;
11332}
11333
11334static rb_node_return_t *
11335rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
11336{
11337 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11338 n->nd_stts = nd_stts;
11339 return n;
11340}
11341
11342static rb_node_yield_t *
11343rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11344{
11345 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11346 n->nd_head = nd_head;
11347
11348 return n;
11349}
11350
11351static rb_node_if_t *
11352rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11353{
11354 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11355 n->nd_cond = nd_cond;
11356 n->nd_body = nd_body;
11357 n->nd_else = nd_else;
11358
11359 return n;
11360}
11361
11362static rb_node_unless_t *
11363rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11364{
11365 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11366 n->nd_cond = nd_cond;
11367 n->nd_body = nd_body;
11368 n->nd_else = nd_else;
11369
11370 return n;
11371}
11372
11373static rb_node_class_t *
11374rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc)
11375{
11376 /* Keep the order of node creation */
11377 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11378 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11379 n->nd_cpath = nd_cpath;
11380 n->nd_body = scope;
11381 n->nd_super = nd_super;
11382
11383 return n;
11384}
11385
11386static rb_node_sclass_t *
11387rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc)
11388{
11389 /* Keep the order of node creation */
11390 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11391 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11392 n->nd_recv = nd_recv;
11393 n->nd_body = scope;
11394
11395 return n;
11396}
11397
11398static rb_node_module_t *
11399rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc)
11400{
11401 /* Keep the order of node creation */
11402 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11403 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11404 n->nd_cpath = nd_cpath;
11405 n->nd_body = scope;
11406
11407 return n;
11408}
11409
11410static rb_node_iter_t *
11411rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11412{
11413 /* Keep the order of node creation */
11414 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11415 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11416 n->nd_body = scope;
11417 n->nd_iter = 0;
11418
11419 return n;
11420}
11421
11422static rb_node_lambda_t *
11423rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11424{
11425 /* Keep the order of node creation */
11426 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11427 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, loc);
11428 n->nd_body = scope;
11429
11430 return n;
11431}
11432
11433static rb_node_case_t *
11434rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
11435{
11436 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11437 n->nd_head = nd_head;
11438 n->nd_body = nd_body;
11439
11440 return n;
11441}
11442
11443static rb_node_case2_t *
11444rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11445{
11446 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11447 n->nd_head = 0;
11448 n->nd_body = nd_body;
11449
11450 return n;
11451}
11452
11453static rb_node_case3_t *
11454rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
11455{
11456 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11457 n->nd_head = nd_head;
11458 n->nd_body = nd_body;
11459
11460 return n;
11461}
11462
11463static rb_node_when_t *
11464rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11465{
11466 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11467 n->nd_head = nd_head;
11468 n->nd_body = nd_body;
11469 n->nd_next = nd_next;
11470
11471 return n;
11472}
11473
11474static rb_node_in_t *
11475rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11476{
11477 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11478 n->nd_head = nd_head;
11479 n->nd_body = nd_body;
11480 n->nd_next = nd_next;
11481
11482 return n;
11483}
11484
11485static rb_node_while_t *
11486rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc)
11487{
11488 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11489 n->nd_cond = nd_cond;
11490 n->nd_body = nd_body;
11491 n->nd_state = nd_state;
11492
11493 return n;
11494}
11495
11496static rb_node_until_t *
11497rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc)
11498{
11499 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11500 n->nd_cond = nd_cond;
11501 n->nd_body = nd_body;
11502 n->nd_state = nd_state;
11503
11504 return n;
11505}
11506
11507static rb_node_colon2_t *
11508rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc)
11509{
11510 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11511 n->nd_head = nd_head;
11512 n->nd_mid = nd_mid;
11513
11514 return n;
11515}
11516
11517static rb_node_colon3_t *
11518rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11519{
11520 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11521 n->nd_mid = nd_mid;
11522
11523 return n;
11524}
11525
11526static rb_node_dot2_t *
11527rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11528{
11529 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11530 n->nd_beg = nd_beg;
11531 n->nd_end = nd_end;
11532
11533 return n;
11534}
11535
11536static rb_node_dot3_t *
11537rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11538{
11539 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11540 n->nd_beg = nd_beg;
11541 n->nd_end = nd_end;
11542
11543 return n;
11544}
11545
11546static rb_node_self_t *
11547rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11548{
11549 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11550 n->nd_state = 1;
11551
11552 return n;
11553}
11554
11555static rb_node_nil_t *
11556rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11557{
11558 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11559
11560 return n;
11561}
11562
11563static rb_node_true_t *
11564rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11565{
11566 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11567
11568 return n;
11569}
11570
11571static rb_node_false_t *
11572rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11573{
11574 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11575
11576 return n;
11577}
11578
11579static rb_node_super_t *
11580rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc)
11581{
11582 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11583 n->nd_args = nd_args;
11584
11585 return n;
11586}
11587
11588static rb_node_zsuper_t *
11589rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11590{
11591 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11592
11593 return n;
11594}
11595
11596static rb_node_match2_t *
11597rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11598{
11599 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11600 n->nd_recv = nd_recv;
11601 n->nd_value = nd_value;
11602 n->nd_args = 0;
11603
11604 return n;
11605}
11606
11607static rb_node_match3_t *
11608rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11609{
11610 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11611 n->nd_recv = nd_recv;
11612 n->nd_value = nd_value;
11613
11614 return n;
11615}
11616
11617/* TODO: Use union for NODE_LIST2 */
11618static rb_node_list_t *
11619rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11620{
11621 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11622 n->nd_head = nd_head;
11623 n->as.nd_alen = 1;
11624 n->nd_next = 0;
11625
11626 return n;
11627}
11628
11629static rb_node_list_t *
11630rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11631{
11632 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11633 n->nd_head = nd_head;
11634 n->as.nd_alen = nd_alen;
11635 n->nd_next = nd_next;
11636
11637 return n;
11638}
11639
11640static rb_node_zlist_t *
11641rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11642{
11643 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11644
11645 return n;
11646}
11647
11648static rb_node_hash_t *
11649rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11650{
11651 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11652 n->nd_head = nd_head;
11653 n->nd_brace = 0;
11654
11655 return n;
11656}
11657
11658static rb_node_masgn_t *
11659rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11660{
11661 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11662 n->nd_head = nd_head;
11663 n->nd_value = 0;
11664 n->nd_args = nd_args;
11665
11666 return n;
11667}
11668
11669static rb_node_gasgn_t *
11670rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11671{
11672 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11673 n->nd_vid = nd_vid;
11674 n->nd_value = nd_value;
11675
11676 return n;
11677}
11678
11679static rb_node_lasgn_t *
11680rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11681{
11682 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11683 n->nd_vid = nd_vid;
11684 n->nd_value = nd_value;
11685
11686 return n;
11687}
11688
11689static rb_node_dasgn_t *
11690rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11691{
11692 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11693 n->nd_vid = nd_vid;
11694 n->nd_value = nd_value;
11695
11696 return n;
11697}
11698
11699static rb_node_iasgn_t *
11700rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11701{
11702 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11703 n->nd_vid = nd_vid;
11704 n->nd_value = nd_value;
11705
11706 return n;
11707}
11708
11709static rb_node_cvasgn_t *
11710rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11711{
11712 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11713 n->nd_vid = nd_vid;
11714 n->nd_value = nd_value;
11715
11716 return n;
11717}
11718
11719static rb_node_op_asgn1_t *
11720rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc)
11721{
11722 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11723 n->nd_recv = nd_recv;
11724 n->nd_mid = nd_mid;
11725 n->nd_index = index;
11726 n->nd_rvalue = rvalue;
11727
11728 return n;
11729}
11730
11731static rb_node_op_asgn2_t *
11732rb_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)
11733{
11734 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11735 n->nd_recv = nd_recv;
11736 n->nd_value = nd_value;
11737 n->nd_vid = nd_vid;
11738 n->nd_mid = nd_mid;
11739 n->nd_aid = nd_aid;
11740
11741 return n;
11742}
11743
11744static rb_node_op_asgn_or_t *
11745rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11746{
11747 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11748 n->nd_head = nd_head;
11749 n->nd_value = nd_value;
11750
11751 return n;
11752}
11753
11754static rb_node_op_asgn_and_t *
11755rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11756{
11757 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11758 n->nd_head = nd_head;
11759 n->nd_value = nd_value;
11760
11761 return n;
11762}
11763
11764static rb_node_gvar_t *
11765rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11766{
11767 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11768 n->nd_vid = nd_vid;
11769
11770 return n;
11771}
11772
11773static rb_node_lvar_t *
11774rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11775{
11776 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11777 n->nd_vid = nd_vid;
11778
11779 return n;
11780}
11781
11782static rb_node_dvar_t *
11783rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11784{
11785 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11786 n->nd_vid = nd_vid;
11787
11788 return n;
11789}
11790
11791static rb_node_ivar_t *
11792rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11793{
11794 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11795 n->nd_vid = nd_vid;
11796
11797 return n;
11798}
11799
11800static rb_node_const_t *
11801rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11802{
11803 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11804 n->nd_vid = nd_vid;
11805
11806 return n;
11807}
11808
11809static rb_node_cvar_t *
11810rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11811{
11812 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11813 n->nd_vid = nd_vid;
11814
11815 return n;
11816}
11817
11818static rb_node_nth_ref_t *
11819rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11820{
11821 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11822 n->nd_nth = nd_nth;
11823
11824 return n;
11825}
11826
11827static rb_node_back_ref_t *
11828rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11829{
11830 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11831 n->nd_nth = nd_nth;
11832
11833 return n;
11834}
11835
11836static rb_node_lit_t *
11837rb_node_lit_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11838{
11839 rb_node_lit_t *n = NODE_NEWNODE(NODE_LIT, rb_node_lit_t, loc);
11840 n->nd_lit = nd_lit;
11841
11842 return n;
11843}
11844
11845static rb_node_str_t *
11846rb_node_str_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11847{
11848 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
11849 n->nd_lit = nd_lit;
11850
11851 return n;
11852}
11853
11854/* TODO; Use union for NODE_DSTR2 */
11855static rb_node_dstr_t *
11856rb_node_dstr_new0(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11857{
11858 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
11859 n->nd_lit = nd_lit;
11860 n->as.nd_alen = nd_alen;
11861 n->nd_next = (rb_node_list_t *)nd_next;
11862
11863 return n;
11864}
11865
11866static rb_node_dstr_t *
11867rb_node_dstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11868{
11869 return rb_node_dstr_new0(p, nd_lit, 1, 0, loc);
11870}
11871
11872static rb_node_xstr_t *
11873rb_node_xstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11874{
11875 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
11876 n->nd_lit = nd_lit;
11877
11878 return n;
11879}
11880
11881static rb_node_dxstr_t *
11882rb_node_dxstr_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11883{
11884 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
11885 n->nd_lit = nd_lit;
11886 n->nd_alen = nd_alen;
11887 n->nd_next = (rb_node_list_t *)nd_next;
11888
11889 return n;
11890}
11891
11892static rb_node_dsym_t *
11893rb_node_dsym_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11894{
11895 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
11896 n->nd_lit = nd_lit;
11897 n->nd_alen = nd_alen;
11898 n->nd_next = (rb_node_list_t *)nd_next;
11899
11900 return n;
11901}
11902
11903static rb_node_evstr_t *
11904rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11905{
11906 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
11907 n->nd_body = nd_body;
11908
11909 return n;
11910}
11911
11912static rb_node_call_t *
11913rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11914{
11915 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
11916 n->nd_recv = nd_recv;
11917 n->nd_mid = nd_mid;
11918 n->nd_args = nd_args;
11919
11920 return n;
11921}
11922
11923static rb_node_opcall_t *
11924rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11925{
11926 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
11927 n->nd_recv = nd_recv;
11928 n->nd_mid = nd_mid;
11929 n->nd_args = nd_args;
11930
11931 return n;
11932}
11933
11934static rb_node_fcall_t *
11935rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11936{
11937 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
11938 n->nd_mid = nd_mid;
11939 n->nd_args = nd_args;
11940
11941 return n;
11942}
11943
11944static rb_node_qcall_t *
11945rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11946{
11947 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
11948 n->nd_recv = nd_recv;
11949 n->nd_mid = nd_mid;
11950 n->nd_args = nd_args;
11951
11952 return n;
11953}
11954
11955static rb_node_vcall_t *
11956rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11957{
11958 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
11959 n->nd_mid = nd_mid;
11960
11961 return n;
11962}
11963
11964static rb_node_once_t *
11965rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11966{
11967 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
11968 n->nd_body = nd_body;
11969
11970 return n;
11971}
11972
11973static rb_node_args_t *
11974rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
11975{
11976 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
11977 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
11978
11979 return n;
11980}
11981
11982static rb_node_args_aux_t *
11983rb_node_args_aux_new(struct parser_params *p, ID nd_pid, long nd_plen, const YYLTYPE *loc)
11984{
11985 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
11986 n->nd_pid = nd_pid;
11987 n->nd_plen = nd_plen;
11988 n->nd_next = 0;
11989
11990 return n;
11991}
11992
11993static rb_node_opt_arg_t *
11994rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11995{
11996 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
11997 n->nd_body = nd_body;
11998 n->nd_next = 0;
11999
12000 return n;
12001}
12002
12003static rb_node_kw_arg_t *
12004rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12005{
12006 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12007 n->nd_body = nd_body;
12008 n->nd_next = 0;
12009
12010 return n;
12011}
12012
12013static rb_node_postarg_t *
12014rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12015{
12016 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12017 n->nd_1st = nd_1st;
12018 n->nd_2nd = nd_2nd;
12019
12020 return n;
12021}
12022
12023static rb_node_argscat_t *
12024rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12025{
12026 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12027 n->nd_head = nd_head;
12028 n->nd_body = nd_body;
12029
12030 return n;
12031}
12032
12033static rb_node_argspush_t *
12034rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12035{
12036 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12037 n->nd_head = nd_head;
12038 n->nd_body = nd_body;
12039
12040 return n;
12041}
12042
12043static rb_node_splat_t *
12044rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12045{
12046 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12047 n->nd_head = nd_head;
12048
12049 return n;
12050}
12051
12052static rb_node_block_pass_t *
12053rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12054{
12055 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12056 n->nd_head = 0;
12057 n->nd_body = nd_body;
12058
12059 return n;
12060}
12061
12062static rb_node_alias_t *
12063rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12064{
12065 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12066 n->nd_1st = nd_1st;
12067 n->nd_2nd = nd_2nd;
12068
12069 return n;
12070}
12071
12072static rb_node_valias_t *
12073rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc)
12074{
12075 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12076 n->nd_alias = nd_alias;
12077 n->nd_orig = nd_orig;
12078
12079 return n;
12080}
12081
12082static rb_node_undef_t *
12083rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12084{
12085 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12086 n->nd_undef = nd_undef;
12087
12088 return n;
12089}
12090
12091static rb_node_errinfo_t *
12092rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12093{
12094 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12095
12096 return n;
12097}
12098
12099static rb_node_defined_t *
12100rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12101{
12102 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12103 n->nd_head = nd_head;
12104
12105 return n;
12106}
12107
12108static rb_node_postexe_t *
12109rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12110{
12111 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12112 n->nd_body = nd_body;
12113
12114 return n;
12115}
12116
12117static rb_node_attrasgn_t *
12118rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12119{
12120 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12121 n->nd_recv = nd_recv;
12122 n->nd_mid = nd_mid;
12123 n->nd_args = nd_args;
12124
12125 return n;
12126}
12127
12128static rb_node_aryptn_t *
12129rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12130{
12131 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12132 n->nd_pconst = 0;
12133 n->pre_args = pre_args;
12134 n->rest_arg = rest_arg;
12135 n->post_args = post_args;
12136
12137 return n;
12138}
12139
12140static rb_node_hshptn_t *
12141rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12142{
12143 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12144 n->nd_pconst = nd_pconst;
12145 n->nd_pkwargs = nd_pkwargs;
12146 n->nd_pkwrestarg = nd_pkwrestarg;
12147
12148 return n;
12149}
12150
12151static rb_node_fndptn_t *
12152rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12153{
12154 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12155 n->nd_pconst = 0;
12156 n->pre_rest_arg = pre_rest_arg;
12157 n->args = args;
12158 n->post_rest_arg = post_rest_arg;
12159
12160 return n;
12161}
12162
12163static rb_node_cdecl_t *
12164rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, const YYLTYPE *loc)
12165{
12166 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12167 n->nd_vid = nd_vid;
12168 n->nd_value = nd_value;
12169 n->nd_else = nd_else;
12170
12171 return n;
12172}
12173
12174static rb_node_op_cdecl_t *
12175rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, const YYLTYPE *loc)
12176{
12177 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12178 n->nd_head = nd_head;
12179 n->nd_value = nd_value;
12180 n->nd_aid = nd_aid;
12181
12182 return n;
12183}
12184
12185static rb_node_error_t *
12186rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12187{
12188 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12189
12190 return n;
12191}
12192
12193#else
12194
12195static rb_node_ripper_t *
12196rb_node_ripper_new(struct parser_params *p, ID nd_vid, VALUE nd_rval, VALUE nd_cval, const YYLTYPE *loc)
12197{
12198 rb_node_ripper_t *n = NODE_NEWNODE(NODE_RIPPER, rb_node_ripper_t, loc);
12199 n->nd_vid = nd_vid;
12200 n->nd_rval = nd_rval;
12201 n->nd_cval = nd_cval;
12202
12203 return n;
12204}
12205
12206static rb_node_ripper_values_t *
12207rb_node_ripper_values_new(struct parser_params *p, VALUE nd_val1, VALUE nd_val2, VALUE nd_val3, const YYLTYPE *loc)
12208{
12209 rb_node_ripper_values_t *n = NODE_NEWNODE(NODE_RIPPER_VALUES, rb_node_ripper_values_t, loc);
12210 n->nd_val1 = nd_val1;
12211 n->nd_val2 = nd_val2;
12212 n->nd_val3 = nd_val3;
12213
12214 return n;
12215}
12216
12217#endif
12218
12219static rb_node_break_t *
12220rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
12221{
12222 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12223 n->nd_stts = nd_stts;
12224 n->nd_chain = 0;
12225
12226 return n;
12227}
12228
12229static rb_node_next_t *
12230rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
12231{
12232 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12233 n->nd_stts = nd_stts;
12234 n->nd_chain = 0;
12235
12236 return n;
12237}
12238
12239static rb_node_redo_t *
12240rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc)
12241{
12242 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12243 n->nd_chain = 0;
12244
12245 return n;
12246}
12247
12248static rb_node_def_temp_t *
12249rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12250{
12251 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12252 n->save.cur_arg = p->cur_arg;
12253 n->save.numparam_save = 0;
12254 n->save.max_numparam = 0;
12255 n->save.ctxt = p->ctxt;
12256#ifdef RIPPER
12257 n->nd_recv = Qnil;
12258 n->nd_mid = Qnil;
12259 n->dot_or_colon = Qnil;
12260#else
12261 n->nd_def = 0;
12262 n->nd_mid = 0;
12263#endif
12264
12265 return n;
12266}
12267
12268static rb_node_def_temp_t *
12269def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12270{
12271 n->save.numparam_save = numparam_push(p);
12272 n->save.max_numparam = p->max_numparam;
12273 return n;
12274}
12275
12276#ifndef RIPPER
12277static enum node_type
12278nodetype(NODE *node) /* for debug */
12279{
12280 return (enum node_type)nd_type(node);
12281}
12282
12283static int
12284nodeline(NODE *node)
12285{
12286 return nd_line(node);
12287}
12288
12289static NODE*
12290newline_node(NODE *node)
12291{
12292 if (node) {
12293 node = remove_begin(node);
12294 nd_set_fl_newline(node);
12295 }
12296 return node;
12297}
12298
12299static void
12300fixpos(NODE *node, NODE *orig)
12301{
12302 if (!node) return;
12303 if (!orig) return;
12304 nd_set_line(node, nd_line(orig));
12305}
12306
12307static void
12308parser_warning(struct parser_params *p, NODE *node, const char *mesg)
12309{
12310 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
12311}
12312
12313static void
12314parser_warn(struct parser_params *p, NODE *node, const char *mesg)
12315{
12316 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
12317}
12318
12319static NODE*
12320block_append(struct parser_params *p, NODE *head, NODE *tail)
12321{
12322 NODE *end, *h = head, *nd;
12323
12324 if (tail == 0) return head;
12325
12326 if (h == 0) return tail;
12327 switch (nd_type(h)) {
12328 default:
12329 h = end = NEW_BLOCK(head, &head->nd_loc);
12330 RNODE_BLOCK(end)->nd_end = end;
12331 head = end;
12332 break;
12333 case NODE_BLOCK:
12334 end = RNODE_BLOCK(h)->nd_end;
12335 break;
12336 }
12337
12338 nd = RNODE_BLOCK(end)->nd_head;
12339 switch (nd_type(nd)) {
12340 case NODE_RETURN:
12341 case NODE_BREAK:
12342 case NODE_NEXT:
12343 case NODE_REDO:
12344 case NODE_RETRY:
12345 if (RTEST(ruby_verbose)) {
12346 parser_warning(p, tail, "statement not reached");
12347 }
12348 break;
12349
12350 default:
12351 break;
12352 }
12353
12354 if (!nd_type_p(tail, NODE_BLOCK)) {
12355 tail = NEW_BLOCK(tail, &tail->nd_loc);
12356 RNODE_BLOCK(tail)->nd_end = tail;
12357 }
12358 RNODE_BLOCK(end)->nd_next = tail;
12359 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12360 nd_set_last_loc(head, nd_last_loc(tail));
12361 return head;
12362}
12363
12364/* append item to the list */
12365static NODE*
12366list_append(struct parser_params *p, NODE *list, NODE *item)
12367{
12368 NODE *last;
12369
12370 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12371 if (RNODE_LIST(list)->nd_next) {
12372 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12373 }
12374 else {
12375 last = list;
12376 }
12377
12378 RNODE_LIST(list)->as.nd_alen += 1;
12379 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12380 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12381
12382 nd_set_last_loc(list, nd_last_loc(item));
12383
12384 return list;
12385}
12386
12387/* concat two lists */
12388static NODE*
12389list_concat(NODE *head, NODE *tail)
12390{
12391 NODE *last;
12392
12393 if (RNODE_LIST(head)->nd_next) {
12394 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12395 }
12396 else {
12397 last = head;
12398 }
12399
12400 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12401 RNODE_LIST(last)->nd_next = tail;
12402 if (RNODE_LIST(tail)->nd_next) {
12403 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12404 }
12405 else {
12406 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12407 }
12408
12409 nd_set_last_loc(head, nd_last_loc(tail));
12410
12411 return head;
12412}
12413
12414static int
12415literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
12416{
12417 if (NIL_P(tail)) return 1;
12418 if (!rb_enc_compatible(head, tail)) {
12419 compile_error(p, "string literal encodings differ (%s / %s)",
12420 rb_enc_name(rb_enc_get(head)),
12421 rb_enc_name(rb_enc_get(tail)));
12422 rb_str_resize(head, 0);
12423 rb_str_resize(tail, 0);
12424 return 0;
12425 }
12426 rb_str_buf_append(head, tail);
12427 return 1;
12428}
12429
12430static VALUE
12431string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12432{
12433 if (htype != NODE_DSTR) return Qfalse;
12434 if (RNODE_DSTR(head)->nd_next) {
12435 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12436 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
12437 }
12438 const VALUE lit = RNODE_DSTR(head)->nd_lit;
12439 ASSUME(lit != Qfalse);
12440 return lit;
12441}
12442
12443/* concat two string literals */
12444static NODE *
12445literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12446{
12447 enum node_type htype;
12448 VALUE lit;
12449
12450 if (!head) return tail;
12451 if (!tail) return head;
12452
12453 htype = nd_type(head);
12454 if (htype == NODE_EVSTR) {
12455 head = new_dstr(p, head, loc);
12456 htype = NODE_DSTR;
12457 }
12458 if (p->heredoc_indent > 0) {
12459 switch (htype) {
12460 case NODE_STR:
12461 head = str2dstr(p, head);
12462 case NODE_DSTR:
12463 return list_append(p, head, tail);
12464 default:
12465 break;
12466 }
12467 }
12468 switch (nd_type(tail)) {
12469 case NODE_STR:
12470 if ((lit = string_literal_head(p, htype, head)) != Qfalse) {
12471 htype = NODE_STR;
12472 }
12473 else {
12474 lit = RNODE_DSTR(head)->nd_lit;
12475 }
12476 if (htype == NODE_STR) {
12477 if (!literal_concat0(p, lit, RNODE_STR(tail)->nd_lit)) {
12478 error:
12479 rb_discard_node(p, head);
12480 rb_discard_node(p, tail);
12481 return 0;
12482 }
12483 rb_discard_node(p, tail);
12484 }
12485 else {
12486 list_append(p, head, tail);
12487 }
12488 break;
12489
12490 case NODE_DSTR:
12491 if (htype == NODE_STR) {
12492 if (!literal_concat0(p, RNODE_STR(head)->nd_lit, RNODE_DSTR(tail)->nd_lit))
12493 goto error;
12494 RNODE_DSTR(tail)->nd_lit = RNODE_STR(head)->nd_lit;
12495 rb_discard_node(p, head);
12496 head = tail;
12497 }
12498 else if (NIL_P(RNODE_DSTR(tail)->nd_lit)) {
12499 append:
12500 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12501 if (!RNODE_DSTR(head)->nd_next) {
12502 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12503 }
12504 else if (RNODE_DSTR(tail)->nd_next) {
12505 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12506 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12507 }
12508 rb_discard_node(p, tail);
12509 }
12510 else if ((lit = string_literal_head(p, htype, head)) != Qfalse) {
12511 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->nd_lit))
12512 goto error;
12513 RNODE_DSTR(tail)->nd_lit = Qnil;
12514 goto append;
12515 }
12516 else {
12517 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));
12518 }
12519 break;
12520
12521 case NODE_EVSTR:
12522 if (htype == NODE_STR) {
12523 head = str2dstr(p, head);
12524 RNODE_DSTR(head)->as.nd_alen = 1;
12525 }
12526 list_append(p, head, tail);
12527 break;
12528 }
12529 return head;
12530}
12531
12532static void
12533nd_copy_flag(NODE *new_node, NODE *old_node)
12534{
12535 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12536 nd_set_line(new_node, nd_line(old_node));
12537 new_node->nd_loc = old_node->nd_loc;
12538 new_node->node_id = old_node->node_id;
12539}
12540
12541static NODE *
12542str2dstr(struct parser_params *p, NODE *node)
12543{
12544 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12545 nd_copy_flag(new_node, node);
12546 RNODE_DSTR(new_node)->nd_lit = RNODE_STR(node)->nd_lit;
12547 RNODE_DSTR(new_node)->as.nd_alen = 0;
12548 RNODE_DSTR(new_node)->nd_next = 0;
12549 RNODE_STR(node)->nd_lit = 0;
12550
12551 return new_node;
12552}
12553
12554static NODE *
12555evstr2dstr(struct parser_params *p, NODE *node)
12556{
12557 if (nd_type_p(node, NODE_EVSTR)) {
12558 node = new_dstr(p, node, &node->nd_loc);
12559 }
12560 return node;
12561}
12562
12563static NODE *
12564new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12565{
12566 NODE *head = node;
12567
12568 if (node) {
12569 switch (nd_type(node)) {
12570 case NODE_STR:
12571 return str2dstr(p, node);
12572 case NODE_DSTR:
12573 break;
12574 case NODE_EVSTR:
12575 return node;
12576 }
12577 }
12578 return NEW_EVSTR(head, loc);
12579}
12580
12581static NODE *
12582new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12583{
12584 VALUE lit = STR_NEW0();
12585 NODE *dstr = NEW_DSTR(lit, loc);
12586 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12587 return list_append(p, dstr, node);
12588}
12589
12590static NODE *
12591call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12592 const YYLTYPE *op_loc, const YYLTYPE *loc)
12593{
12594 NODE *expr;
12595 value_expr(recv);
12596 value_expr(arg1);
12597 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12598 nd_set_line(expr, op_loc->beg_pos.lineno);
12599 return expr;
12600}
12601
12602static NODE *
12603call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12604{
12605 NODE *opcall;
12606 value_expr(recv);
12607 opcall = NEW_OPCALL(recv, id, 0, loc);
12608 nd_set_line(opcall, op_loc->beg_pos.lineno);
12609 return opcall;
12610}
12611
12612static NODE *
12613new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12614{
12615 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12616 nd_set_line(qcall, op_loc->beg_pos.lineno);
12617 return qcall;
12618}
12619
12620static NODE*
12621new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12622{
12623 NODE *ret;
12624 if (block) block_dup_check(p, args, block);
12625 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12626 if (block) ret = method_add_block(p, ret, block, loc);
12627 fixpos(ret, recv);
12628 return ret;
12629}
12630
12631#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12632
12633static NODE*
12634last_expr_once_body(NODE *node)
12635{
12636 if (!node) return 0;
12637 return nd_once_body(node);
12638}
12639
12640static NODE*
12641match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12642{
12643 NODE *n;
12644 int line = op_loc->beg_pos.lineno;
12645
12646 value_expr(node1);
12647 value_expr(node2);
12648
12649 if ((n = last_expr_once_body(node1)) != 0) {
12650 switch (nd_type(n)) {
12651 case NODE_DREGX:
12652 {
12653 NODE *match = NEW_MATCH2(node1, node2, loc);
12654 nd_set_line(match, line);
12655 return match;
12656 }
12657
12658 case NODE_LIT:
12659 if (RB_TYPE_P(RNODE_LIT(n)->nd_lit, T_REGEXP)) {
12660 const VALUE lit = RNODE_LIT(n)->nd_lit;
12661 NODE *match = NEW_MATCH2(node1, node2, loc);
12662 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc);
12663 nd_set_line(match, line);
12664 return match;
12665 }
12666 }
12667 }
12668
12669 if ((n = last_expr_once_body(node2)) != 0) {
12670 NODE *match3;
12671
12672 switch (nd_type(n)) {
12673 case NODE_LIT:
12674 if (!RB_TYPE_P(RNODE_LIT(n)->nd_lit, T_REGEXP)) break;
12675 /* fallthru */
12676 case NODE_DREGX:
12677 match3 = NEW_MATCH3(node2, node1, loc);
12678 return match3;
12679 }
12680 }
12681
12682 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12683 nd_set_line(n, line);
12684 return n;
12685}
12686
12687# if WARN_PAST_SCOPE
12688static int
12689past_dvar_p(struct parser_params *p, ID id)
12690{
12691 struct vtable *past = p->lvtbl->past;
12692 while (past) {
12693 if (vtable_included(past, id)) return 1;
12694 past = past->prev;
12695 }
12696 return 0;
12697}
12698# endif
12699
12700static int
12701numparam_nested_p(struct parser_params *p)
12702{
12703 struct local_vars *local = p->lvtbl;
12704 NODE *outer = local->numparam.outer;
12705 NODE *inner = local->numparam.inner;
12706 if (outer || inner) {
12707 NODE *used = outer ? outer : inner;
12708 compile_error(p, "numbered parameter is already used in\n"
12709 "%s:%d: %s block here",
12710 p->ruby_sourcefile, nd_line(used),
12711 outer ? "outer" : "inner");
12712 parser_show_error_line(p, &used->nd_loc);
12713 return 1;
12714 }
12715 return 0;
12716}
12717
12718static NODE*
12719gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12720{
12721 ID *vidp = NULL;
12722 NODE *node;
12723 switch (id) {
12724 case keyword_self:
12725 return NEW_SELF(loc);
12726 case keyword_nil:
12727 return NEW_NIL(loc);
12728 case keyword_true:
12729 return NEW_TRUE(loc);
12730 case keyword_false:
12731 return NEW_FALSE(loc);
12732 case keyword__FILE__:
12733 {
12734 VALUE file = p->ruby_sourcefile_string;
12735 if (NIL_P(file))
12736 file = rb_str_new(0, 0);
12737 else
12738 file = rb_str_dup(file);
12739 node = NEW_STR(file, loc);
12740 RB_OBJ_WRITTEN(p->ast, Qnil, file);
12741 }
12742 return node;
12743 case keyword__LINE__:
12744 return NEW_LIT(INT2FIX(loc->beg_pos.lineno), loc);
12745 case keyword__ENCODING__:
12746 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
12747 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit);
12748 return node;
12749
12750 }
12751 switch (id_type(id)) {
12752 case ID_LOCAL:
12753 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
12754 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
12755 if (id == p->cur_arg) {
12756 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
12757 return 0;
12758 }
12759 if (vidp) *vidp |= LVAR_USED;
12760 node = NEW_DVAR(id, loc);
12761 return node;
12762 }
12763 if (local_id_ref(p, id, &vidp)) {
12764 if (id == p->cur_arg) {
12765 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
12766 return 0;
12767 }
12768 if (vidp) *vidp |= LVAR_USED;
12769 node = NEW_LVAR(id, loc);
12770 return node;
12771 }
12772 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
12773 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
12774 if (numparam_nested_p(p)) return 0;
12775 node = NEW_DVAR(id, loc);
12776 struct local_vars *local = p->lvtbl;
12777 if (!local->numparam.current) local->numparam.current = node;
12778 return node;
12779 }
12780# if WARN_PAST_SCOPE
12781 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
12782 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
12783 }
12784# endif
12785 /* method call without arguments */
12786 if (dyna_in_block(p) && id == rb_intern("it")
12787 && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))
12788 && p->max_numparam != ORDINAL_PARAM) {
12789 rb_warn0("`it` calls without arguments will refer to the first block param in Ruby 3.4; use it() or self.it");
12790 }
12791 return NEW_VCALL(id, loc);
12792 case ID_GLOBAL:
12793 return NEW_GVAR(id, loc);
12794 case ID_INSTANCE:
12795 return NEW_IVAR(id, loc);
12796 case ID_CONST:
12797 return NEW_CONST(id, loc);
12798 case ID_CLASS:
12799 return NEW_CVAR(id, loc);
12800 }
12801 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
12802 return 0;
12803}
12804
12805static rb_node_opt_arg_t *
12806opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
12807{
12808 rb_node_opt_arg_t *opts = opt_list;
12809 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
12810
12811 while (opts->nd_next) {
12812 opts = opts->nd_next;
12813 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
12814 }
12815 opts->nd_next = opt;
12816
12817 return opt_list;
12818}
12819
12820static rb_node_kw_arg_t *
12821kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
12822{
12823 if (kwlist) {
12824 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
12825 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
12826 }
12827 return kwlist;
12828}
12829
12830static NODE *
12831new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
12832{
12833 return NEW_DEFINED(remove_begin_all(expr), loc);
12834}
12835
12836static NODE*
12837symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
12838{
12839 enum node_type type = nd_type(symbol);
12840 switch (type) {
12841 case NODE_DSTR:
12842 nd_set_type(symbol, NODE_DSYM);
12843 break;
12844 case NODE_STR:
12845 nd_set_type(symbol, NODE_LIT);
12846 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(symbol)->nd_lit = rb_str_intern(RNODE_LIT(symbol)->nd_lit));
12847 break;
12848 default:
12849 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
12850 }
12851 return list_append(p, symbols, symbol);
12852}
12853
12854static NODE *
12855new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
12856{
12857 struct RNode_LIST *list;
12858 NODE *prev;
12859 VALUE lit;
12860
12861 if (!node) {
12862 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
12863 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit);
12864 return node;
12865 }
12866 switch (nd_type(node)) {
12867 case NODE_STR:
12868 {
12869 VALUE src = RNODE_STR(node)->nd_lit;
12870 nd_set_type(node, NODE_LIT);
12871 nd_set_loc(node, loc);
12872 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit = reg_compile(p, src, options));
12873 }
12874 break;
12875 default:
12876 lit = STR_NEW0();
12877 node = NEW_DSTR0(lit, 1, NEW_LIST(node, loc), loc);
12878 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12879 /* fall through */
12880 case NODE_DSTR:
12881 nd_set_type(node, NODE_DREGX);
12882 nd_set_loc(node, loc);
12883 RNODE_DREGX(node)->nd_cflag = options & RE_OPTION_MASK;
12884 if (!NIL_P(RNODE_DREGX(node)->nd_lit)) reg_fragment_check(p, RNODE_DREGX(node)->nd_lit, options);
12885 for (list = RNODE_DREGX(prev = node)->nd_next; list; list = RNODE_LIST(list->nd_next)) {
12886 NODE *frag = list->nd_head;
12887 enum node_type type = nd_type(frag);
12888 if (type == NODE_STR || (type == NODE_DSTR && !RNODE_DSTR(frag)->nd_next)) {
12889 VALUE tail = RNODE_STR(frag)->nd_lit;
12890 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(RNODE_DREGX(prev)->nd_lit)) {
12891 VALUE lit = prev == node ? RNODE_DREGX(prev)->nd_lit : RNODE_LIT(RNODE_LIST(prev)->nd_head)->nd_lit;
12892 if (!literal_concat0(p, lit, tail)) {
12893 return NEW_NIL(loc); /* dummy node on error */
12894 }
12895 rb_str_resize(tail, 0);
12896 RNODE_LIST(prev)->nd_next = list->nd_next;
12897 rb_discard_node(p, list->nd_head);
12898 rb_discard_node(p, (NODE *)list);
12899 list = RNODE_LIST(prev);
12900 }
12901 else {
12902 prev = (NODE *)list;
12903 }
12904 }
12905 else {
12906 prev = 0;
12907 }
12908 }
12909 if (!RNODE_DREGX(node)->nd_next) {
12910 VALUE src = RNODE_DREGX(node)->nd_lit;
12911 VALUE re = reg_compile(p, src, options);
12912 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_DREGX(node)->nd_lit = re);
12913 }
12914 if (options & RE_OPTION_ONCE) {
12915 node = NEW_ONCE(node, loc);
12916 }
12917 break;
12918 }
12919 return node;
12920}
12921
12922static rb_node_kw_arg_t *
12923new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
12924{
12925 if (!k) return 0;
12926 return NEW_KW_ARG((k), loc);
12927}
12928
12929static NODE *
12930new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12931{
12932 if (!node) {
12933 VALUE lit = STR_NEW0();
12934 NODE *xstr = NEW_XSTR(lit, loc);
12935 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12936 return xstr;
12937 }
12938 switch (nd_type(node)) {
12939 case NODE_STR:
12940 nd_set_type(node, NODE_XSTR);
12941 nd_set_loc(node, loc);
12942 break;
12943 case NODE_DSTR:
12944 nd_set_type(node, NODE_DXSTR);
12945 nd_set_loc(node, loc);
12946 break;
12947 default:
12948 node = NEW_DXSTR(Qnil, 1, NEW_LIST(node, loc), loc);
12949 break;
12950 }
12951 return node;
12952}
12953
12954static void
12955check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
12956{
12957 VALUE lit;
12958
12959 if (!arg || !p->case_labels) return;
12960
12961 lit = rb_node_case_when_optimizable_literal(arg);
12962 if (UNDEF_P(lit)) return;
12963 if (nd_type_p(arg, NODE_STR)) {
12964 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(arg)->nd_lit = lit);
12965 }
12966
12967 if (NIL_P(p->case_labels)) {
12968 p->case_labels = rb_obj_hide(rb_hash_new());
12969 }
12970 else {
12971 VALUE line = rb_hash_lookup(p->case_labels, lit);
12972 if (!NIL_P(line)) {
12973 rb_warning1("duplicated `when' clause with line %d is ignored",
12974 WARN_IVAL(line));
12975 return;
12976 }
12977 }
12978 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
12979}
12980
12981#else /* !RIPPER */
12982static int
12983id_is_var(struct parser_params *p, ID id)
12984{
12985 if (is_notop_id(id)) {
12986 switch (id & ID_SCOPE_MASK) {
12987 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
12988 return 1;
12989 case ID_LOCAL:
12990 if (dyna_in_block(p)) {
12991 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
12992 }
12993 if (local_id(p, id)) return 1;
12994 /* method call without arguments */
12995 return 0;
12996 }
12997 }
12998 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
12999 return 0;
13000}
13001
13002static VALUE
13003new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
13004{
13005 VALUE src = 0, err = 0;
13006 int options = 0;
13007 if (ripper_is_node_yylval(p, re)) {
13008 src = RNODE_RIPPER(re)->nd_cval;
13009 re = RNODE_RIPPER(re)->nd_rval;
13010 }
13011 if (ripper_is_node_yylval(p, opt)) {
13012 options = (int)RNODE_RIPPER(opt)->nd_vid;
13013 opt = RNODE_RIPPER(opt)->nd_rval;
13014 }
13015 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
13016 compile_error(p, "%"PRIsVALUE, err);
13017 }
13018 return dispatch2(regexp_literal, re, opt);
13019}
13020#endif /* !RIPPER */
13021
13022static inline enum lex_state_e
13023parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13024{
13025 if (p->debug) {
13026 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13027 }
13028 return p->lex.state = ls;
13029}
13030
13031#ifndef RIPPER
13032static const char rb_parser_lex_state_names[][8] = {
13033 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13034 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13035 "LABEL", "LABELED","FITEM",
13036};
13037
13038static VALUE
13039append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13040{
13041 int i, sep = 0;
13042 unsigned int mask = 1;
13043 static const char none[] = "NONE";
13044
13045 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13046 if ((unsigned)state & mask) {
13047 if (sep) {
13048 rb_str_cat(buf, "|", 1);
13049 }
13050 sep = 1;
13051 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13052 }
13053 }
13054 if (!sep) {
13055 rb_str_cat(buf, none, sizeof(none)-1);
13056 }
13057 return buf;
13058}
13059
13060static void
13061flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13062{
13063 VALUE mesg = p->debug_buffer;
13064
13065 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13066 p->debug_buffer = Qnil;
13067 rb_io_puts(1, &mesg, out);
13068 }
13069 if (!NIL_P(str) && RSTRING_LEN(str)) {
13070 rb_io_write(p->debug_output, str);
13071 }
13072}
13073
13074enum lex_state_e
13075rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13076 enum lex_state_e to, int line)
13077{
13078 VALUE mesg;
13079 mesg = rb_str_new_cstr("lex_state: ");
13080 append_lex_state_name(p, from, mesg);
13081 rb_str_cat_cstr(mesg, " -> ");
13082 append_lex_state_name(p, to, mesg);
13083 rb_str_catf(mesg, " at line %d\n", line);
13084 flush_debug_buffer(p, p->debug_output, mesg);
13085 return to;
13086}
13087
13088VALUE
13089rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13090{
13091 return rb_fstring(append_lex_state_name(p, state, rb_str_new(0, 0)));
13092}
13093
13094static void
13095append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13096{
13097 if (stack == 0) {
13098 rb_str_cat_cstr(mesg, "0");
13099 }
13100 else {
13101 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13102 for (; mask && !(stack & mask); mask >>= 1) continue;
13103 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13104 }
13105}
13106
13107void
13108rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13109 const char *name, int line)
13110{
13111 VALUE mesg = rb_sprintf("%s: ", name);
13112 append_bitstack_value(p, stack, mesg);
13113 rb_str_catf(mesg, " at line %d\n", line);
13114 flush_debug_buffer(p, p->debug_output, mesg);
13115}
13116
13117void
13118rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13119{
13120 va_list ap;
13121 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13122
13123 va_start(ap, fmt);
13124 rb_str_vcatf(mesg, fmt, ap);
13125 va_end(ap);
13126 yyerror0(RSTRING_PTR(mesg));
13127 RB_GC_GUARD(mesg);
13128
13129 mesg = rb_str_new(0, 0);
13130 append_lex_state_name(p, p->lex.state, mesg);
13131 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13132 rb_str_resize(mesg, 0);
13133 append_bitstack_value(p, p->cond_stack, mesg);
13134 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13135 rb_str_resize(mesg, 0);
13136 append_bitstack_value(p, p->cmdarg_stack, mesg);
13137 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13138 if (p->debug_output == rb_ractor_stdout())
13139 p->debug_output = rb_ractor_stderr();
13140 p->debug = TRUE;
13141}
13142
13143static YYLTYPE *
13144rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13145{
13146 yylloc->beg_pos.lineno = sourceline;
13147 yylloc->beg_pos.column = beg_pos;
13148 yylloc->end_pos.lineno = sourceline;
13149 yylloc->end_pos.column = end_pos;
13150 return yylloc;
13151}
13152
13153YYLTYPE *
13154rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13155{
13156 int sourceline = here->sourceline;
13157 int beg_pos = (int)here->offset - here->quote
13158 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13159 int end_pos = (int)here->offset + here->length + here->quote;
13160
13161 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13162}
13163
13164YYLTYPE *
13165rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13166{
13167 yylloc->beg_pos.lineno = p->delayed.beg_line;
13168 yylloc->beg_pos.column = p->delayed.beg_col;
13169 yylloc->end_pos.lineno = p->delayed.end_line;
13170 yylloc->end_pos.column = p->delayed.end_col;
13171
13172 return yylloc;
13173}
13174
13175YYLTYPE *
13176rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13177{
13178 int sourceline = p->ruby_sourceline;
13179 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13180 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13181 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13182}
13183
13184YYLTYPE *
13185rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13186{
13187 yylloc->end_pos = yylloc->beg_pos;
13188
13189 return yylloc;
13190}
13191
13192YYLTYPE *
13193rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13194{
13195 int sourceline = p->ruby_sourceline;
13196 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13197 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13198 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13199}
13200
13201YYLTYPE *
13202rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13203{
13204 int sourceline = p->ruby_sourceline;
13205 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13206 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13207 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13208}
13209#endif /* !RIPPER */
13210
13211static int
13212assignable0(struct parser_params *p, ID id, const char **err)
13213{
13214 if (!id) return -1;
13215 switch (id) {
13216 case keyword_self:
13217 *err = "Can't change the value of self";
13218 return -1;
13219 case keyword_nil:
13220 *err = "Can't assign to nil";
13221 return -1;
13222 case keyword_true:
13223 *err = "Can't assign to true";
13224 return -1;
13225 case keyword_false:
13226 *err = "Can't assign to false";
13227 return -1;
13228 case keyword__FILE__:
13229 *err = "Can't assign to __FILE__";
13230 return -1;
13231 case keyword__LINE__:
13232 *err = "Can't assign to __LINE__";
13233 return -1;
13234 case keyword__ENCODING__:
13235 *err = "Can't assign to __ENCODING__";
13236 return -1;
13237 }
13238 switch (id_type(id)) {
13239 case ID_LOCAL:
13240 if (dyna_in_block(p)) {
13241 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13242 compile_error(p, "Can't assign to numbered parameter _%d",
13243 NUMPARAM_ID_TO_IDX(id));
13244 return -1;
13245 }
13246 if (dvar_curr(p, id)) return NODE_DASGN;
13247 if (dvar_defined(p, id)) return NODE_DASGN;
13248 if (local_id(p, id)) return NODE_LASGN;
13249 dyna_var(p, id);
13250 return NODE_DASGN;
13251 }
13252 else {
13253 if (!local_id(p, id)) local_var(p, id);
13254 return NODE_LASGN;
13255 }
13256 break;
13257 case ID_GLOBAL: return NODE_GASGN;
13258 case ID_INSTANCE: return NODE_IASGN;
13259 case ID_CONST:
13260 if (!p->ctxt.in_def) return NODE_CDECL;
13261 *err = "dynamic constant assignment";
13262 return -1;
13263 case ID_CLASS: return NODE_CVASGN;
13264 default:
13265 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13266 }
13267 return -1;
13268}
13269
13270#ifndef RIPPER
13271static NODE*
13272assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13273{
13274 const char *err = 0;
13275 int node_type = assignable0(p, id, &err);
13276 switch (node_type) {
13277 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13278 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13279 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13280 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13281 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
13282 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13283 }
13284 if (err) yyerror1(loc, err);
13285 return NEW_BEGIN(0, loc);
13286}
13287#else
13288static VALUE
13289assignable(struct parser_params *p, VALUE lhs)
13290{
13291 const char *err = 0;
13292 assignable0(p, get_id(lhs), &err);
13293 if (err) lhs = assign_error(p, err, lhs);
13294 return lhs;
13295}
13296#endif
13297
13298static int
13299is_private_local_id(struct parser_params *p, ID name)
13300{
13301 VALUE s;
13302 if (name == idUScore) return 1;
13303 if (!is_local_id(name)) return 0;
13304 s = rb_id2str(name);
13305 if (!s) return 0;
13306 return RSTRING_PTR(s)[0] == '_';
13307}
13308
13309static int
13310shadowing_lvar_0(struct parser_params *p, ID name)
13311{
13312 if (dyna_in_block(p)) {
13313 if (dvar_curr(p, name)) {
13314 if (is_private_local_id(p, name)) return 1;
13315 yyerror0("duplicated argument name");
13316 }
13317 else if (dvar_defined(p, name) || local_id(p, name)) {
13318 vtable_add(p->lvtbl->vars, name);
13319 if (p->lvtbl->used) {
13320 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13321 }
13322 return 0;
13323 }
13324 }
13325 else {
13326 if (local_id(p, name)) {
13327 if (is_private_local_id(p, name)) return 1;
13328 yyerror0("duplicated argument name");
13329 }
13330 }
13331 return 1;
13332}
13333
13334static ID
13335shadowing_lvar(struct parser_params *p, ID name)
13336{
13337 shadowing_lvar_0(p, name);
13338 return name;
13339}
13340
13341static void
13342new_bv(struct parser_params *p, ID name)
13343{
13344 if (!name) return;
13345 if (!is_local_id(name)) {
13346 compile_error(p, "invalid local variable - %"PRIsVALUE,
13347 rb_id2str(name));
13348 return;
13349 }
13350 if (!shadowing_lvar_0(p, name)) return;
13351 dyna_var(p, name);
13352}
13353
13354#ifndef RIPPER
13355static NODE *
13356aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13357{
13358 return NEW_ATTRASGN(recv, tASET, idx, loc);
13359}
13360
13361static void
13362block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13363{
13364 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13365 compile_error(p, "both block arg and actual block given");
13366 }
13367}
13368
13369static NODE *
13370attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13371{
13372 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13373 return NEW_ATTRASGN(recv, id, 0, loc);
13374}
13375
13376static void
13377rb_backref_error(struct parser_params *p, NODE *node)
13378{
13379 switch (nd_type(node)) {
13380 case NODE_NTH_REF:
13381 compile_error(p, "Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13382 break;
13383 case NODE_BACK_REF:
13384 compile_error(p, "Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13385 break;
13386 }
13387}
13388#else
13389static VALUE
13390backref_error(struct parser_params *p, NODE *ref, VALUE expr)
13391{
13392 VALUE mesg = rb_str_new_cstr("Can't set variable ");
13393 rb_str_append(mesg, RNODE_RIPPER(ref)->nd_cval);
13394 return dispatch2(assign_error, mesg, expr);
13395}
13396#endif
13397
13398#ifndef RIPPER
13399static NODE *
13400arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13401{
13402 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13403 switch (nd_type(node1)) {
13404 case NODE_LIST:
13405 return list_append(p, node1, node2);
13406 case NODE_BLOCK_PASS:
13407 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13408 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13409 return node1;
13410 case NODE_ARGSPUSH:
13411 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13412 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13413 nd_set_type(node1, NODE_ARGSCAT);
13414 return node1;
13415 case NODE_ARGSCAT:
13416 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13417 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13418 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13419 return node1;
13420 }
13421 return NEW_ARGSPUSH(node1, node2, loc);
13422}
13423
13424static NODE *
13425arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13426{
13427 if (!node2) return node1;
13428 switch (nd_type(node1)) {
13429 case NODE_BLOCK_PASS:
13430 if (RNODE_BLOCK_PASS(node1)->nd_head)
13431 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13432 else
13433 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13434 return node1;
13435 case NODE_ARGSPUSH:
13436 if (!nd_type_p(node2, NODE_LIST)) break;
13437 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13438 nd_set_type(node1, NODE_ARGSCAT);
13439 return node1;
13440 case NODE_ARGSCAT:
13441 if (!nd_type_p(node2, NODE_LIST) ||
13442 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13443 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13444 return node1;
13445 }
13446 return NEW_ARGSCAT(node1, node2, loc);
13447}
13448
13449static NODE *
13450last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13451{
13452 NODE *n1;
13453 if ((n1 = splat_array(args)) != 0) {
13454 return list_append(p, n1, last_arg);
13455 }
13456 return arg_append(p, args, last_arg, loc);
13457}
13458
13459static NODE *
13460rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13461{
13462 NODE *n1;
13463 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13464 return list_concat(n1, rest_arg);
13465 }
13466 return arg_concat(p, args, rest_arg, loc);
13467}
13468
13469static NODE *
13470splat_array(NODE* node)
13471{
13472 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13473 if (nd_type_p(node, NODE_LIST)) return node;
13474 return 0;
13475}
13476
13477static void
13478mark_lvar_used(struct parser_params *p, NODE *rhs)
13479{
13480 ID *vidp = NULL;
13481 if (!rhs) return;
13482 switch (nd_type(rhs)) {
13483 case NODE_LASGN:
13484 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13485 if (vidp) *vidp |= LVAR_USED;
13486 }
13487 break;
13488 case NODE_DASGN:
13489 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13490 if (vidp) *vidp |= LVAR_USED;
13491 }
13492 break;
13493#if 0
13494 case NODE_MASGN:
13495 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13496 mark_lvar_used(p, rhs->nd_head);
13497 }
13498 break;
13499#endif
13500 }
13501}
13502
13503static NODE *
13504const_decl_path(struct parser_params *p, NODE **dest)
13505{
13506 NODE *n = *dest;
13507 if (!nd_type_p(n, NODE_CALL)) {
13508 const YYLTYPE *loc = &n->nd_loc;
13509 VALUE path;
13510 if (RNODE_CDECL(n)->nd_vid) {
13511 path = rb_id2str(RNODE_CDECL(n)->nd_vid);
13512 }
13513 else {
13514 n = RNODE_CDECL(n)->nd_else;
13515 path = rb_ary_new();
13516 for (; n && nd_type_p(n, NODE_COLON2); n = RNODE_COLON2(n)->nd_head) {
13517 rb_ary_push(path, rb_id2str(RNODE_COLON2(n)->nd_mid));
13518 }
13519 if (n && nd_type_p(n, NODE_CONST)) {
13520 // Const::Name
13521 rb_ary_push(path, rb_id2str(RNODE_CONST(n)->nd_vid));
13522 }
13523 else if (n && nd_type_p(n, NODE_COLON3)) {
13524 // ::Const::Name
13525 rb_ary_push(path, rb_str_new(0, 0));
13526 }
13527 else {
13528 // expression::Name
13529 rb_ary_push(path, rb_str_new_cstr("..."));
13530 }
13531 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
13532 path = rb_fstring(path);
13533 }
13534 *dest = n = NEW_LIT(path, loc);
13535 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit);
13536 }
13537 return n;
13538}
13539
13540static NODE *
13541make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
13542{
13543 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
13544
13545 if (copy) {
13546 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
13547 NEW_LIST(value, loc), loc);
13548 }
13549 else {
13550 return NEW_CALL(fcore, rb_intern("make_shareable"),
13551 NEW_LIST(value, loc), loc);
13552 }
13553}
13554
13555static NODE *
13556ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
13557{
13558 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
13559 NODE *args = NEW_LIST(value, loc);
13560 args = list_append(p, args, const_decl_path(p, dest));
13561 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
13562}
13563
13564static int is_static_content(NODE *node);
13565
13566static VALUE
13567shareable_literal_value(struct parser_params *p, NODE *node)
13568{
13569 if (!node) return Qnil;
13570 enum node_type type = nd_type(node);
13571 switch (type) {
13572 case NODE_TRUE:
13573 return Qtrue;
13574 case NODE_FALSE:
13575 return Qfalse;
13576 case NODE_NIL:
13577 return Qnil;
13578 case NODE_LIT:
13579 return RNODE_LIT(node)->nd_lit;
13580 default:
13581 return Qundef;
13582 }
13583}
13584
13585#ifndef SHAREABLE_BARE_EXPRESSION
13586#define SHAREABLE_BARE_EXPRESSION 1
13587#endif
13588
13589static NODE *
13590shareable_literal_constant(struct parser_params *p, enum shareability shareable,
13591 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
13592{
13593# define shareable_literal_constant_next(n) \
13594 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
13595 VALUE lit = Qnil;
13596
13597 if (!value) return 0;
13598 enum node_type type = nd_type(value);
13599 switch (type) {
13600 case NODE_TRUE:
13601 case NODE_FALSE:
13602 case NODE_NIL:
13603 case NODE_LIT:
13604 return value;
13605
13606 case NODE_DSTR:
13607 if (shareable == shareable_literal) {
13608 value = NEW_CALL(value, idUMinus, 0, loc);
13609 }
13610 return value;
13611
13612 case NODE_STR:
13613 lit = rb_fstring(RNODE_STR(value)->nd_lit);
13614 nd_set_type(value, NODE_LIT);
13615 RB_OBJ_WRITE(p->ast, &RNODE_LIT(value)->nd_lit, lit);
13616 return value;
13617
13618 case NODE_ZLIST:
13619 lit = rb_ary_new();
13620 OBJ_FREEZE_RAW(lit);
13621 NODE *n = NEW_LIT(lit, loc);
13622 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit);
13623 return n;
13624
13625 case NODE_LIST:
13626 lit = rb_ary_new();
13627 for (NODE *n = value; n; n = RNODE_LIST(n)->nd_next) {
13628 NODE *elt = RNODE_LIST(n)->nd_head;
13629 if (elt) {
13630 elt = shareable_literal_constant_next(elt);
13631 if (elt) {
13632 RNODE_LIST(n)->nd_head = elt;
13633 }
13634 else if (RTEST(lit)) {
13635 rb_ary_clear(lit);
13636 lit = Qfalse;
13637 }
13638 }
13639 if (RTEST(lit)) {
13640 VALUE e = shareable_literal_value(p, elt);
13641 if (!UNDEF_P(e)) {
13642 rb_ary_push(lit, e);
13643 }
13644 else {
13645 rb_ary_clear(lit);
13646 lit = Qnil; /* make shareable at runtime */
13647 }
13648 }
13649 }
13650 break;
13651
13652 case NODE_HASH:
13653 if (!RNODE_HASH(value)->nd_brace) return 0;
13654 lit = rb_hash_new();
13655 for (NODE *n = RNODE_HASH(value)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
13656 NODE *key = RNODE_LIST(n)->nd_head;
13657 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
13658 if (key) {
13659 key = shareable_literal_constant_next(key);
13660 if (key) {
13661 RNODE_LIST(n)->nd_head = key;
13662 }
13663 else if (RTEST(lit)) {
13664 rb_hash_clear(lit);
13665 lit = Qfalse;
13666 }
13667 }
13668 if (val) {
13669 val = shareable_literal_constant_next(val);
13670 if (val) {
13671 RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head = val;
13672 }
13673 else if (RTEST(lit)) {
13674 rb_hash_clear(lit);
13675 lit = Qfalse;
13676 }
13677 }
13678 if (RTEST(lit)) {
13679 VALUE k = shareable_literal_value(p, key);
13680 VALUE v = shareable_literal_value(p, val);
13681 if (!UNDEF_P(k) && !UNDEF_P(v)) {
13682 rb_hash_aset(lit, k, v);
13683 }
13684 else {
13685 rb_hash_clear(lit);
13686 lit = Qnil; /* make shareable at runtime */
13687 }
13688 }
13689 }
13690 break;
13691
13692 default:
13693 if (shareable == shareable_literal &&
13694 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
13695 return ensure_shareable_node(p, dest, value, loc);
13696 }
13697 return 0;
13698 }
13699
13700 /* Array or Hash */
13701 if (!lit) return 0;
13702 if (NIL_P(lit)) {
13703 // if shareable_literal, all elements should have been ensured
13704 // as shareable
13705 value = make_shareable_node(p, value, false, loc);
13706 }
13707 else {
13708 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
13709 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(value)->nd_lit);
13710 }
13711
13712 return value;
13713# undef shareable_literal_constant_next
13714}
13715
13716static NODE *
13717shareable_constant_value(struct parser_params *p, enum shareability shareable,
13718 NODE *lhs, NODE *value, const YYLTYPE *loc)
13719{
13720 if (!value) return 0;
13721 switch (shareable) {
13722 case shareable_none:
13723 return value;
13724
13725 case shareable_literal:
13726 {
13727 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
13728 if (lit) return lit;
13729 return value;
13730 }
13731 break;
13732
13733 case shareable_copy:
13734 case shareable_everything:
13735 {
13736 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
13737 if (lit) return lit;
13738 return make_shareable_node(p, value, shareable == shareable_copy, loc);
13739 }
13740 break;
13741
13742 default:
13743 UNREACHABLE_RETURN(0);
13744 }
13745}
13746
13747static NODE *
13748node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13749{
13750 if (!lhs) return 0;
13751
13752 switch (nd_type(lhs)) {
13753 case NODE_CDECL:
13754 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
13755 /* fallthru */
13756
13757 case NODE_GASGN:
13758 case NODE_IASGN:
13759 case NODE_LASGN:
13760 case NODE_DASGN:
13761 case NODE_MASGN:
13762 case NODE_CVASGN:
13763 set_nd_value(p, lhs, rhs);
13764 nd_set_loc(lhs, loc);
13765 break;
13766
13767 case NODE_ATTRASGN:
13768 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13769 nd_set_loc(lhs, loc);
13770 break;
13771
13772 default:
13773 /* should not happen */
13774 break;
13775 }
13776
13777 return lhs;
13778}
13779
13780static NODE *
13781value_expr_check(struct parser_params *p, NODE *node)
13782{
13783 NODE *void_node = 0, *vn;
13784
13785 if (!node) {
13786 rb_warning0("empty expression");
13787 }
13788 while (node) {
13789 switch (nd_type(node)) {
13790 case NODE_RETURN:
13791 case NODE_BREAK:
13792 case NODE_NEXT:
13793 case NODE_REDO:
13794 case NODE_RETRY:
13795 return void_node ? void_node : node;
13796
13797 case NODE_CASE3:
13798 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13799 compile_error(p, "unexpected node");
13800 return NULL;
13801 }
13802 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13803 return NULL;
13804 }
13805 /* single line pattern matching with "=>" operator */
13806 return void_node ? void_node : node;
13807
13808 case NODE_BLOCK:
13809 while (RNODE_BLOCK(node)->nd_next) {
13810 node = RNODE_BLOCK(node)->nd_next;
13811 }
13812 node = RNODE_BLOCK(node)->nd_head;
13813 break;
13814
13815 case NODE_BEGIN:
13816 node = RNODE_BEGIN(node)->nd_body;
13817 break;
13818
13819 case NODE_IF:
13820 case NODE_UNLESS:
13821 if (!RNODE_IF(node)->nd_body) {
13822 return NULL;
13823 }
13824 else if (!RNODE_IF(node)->nd_else) {
13825 return NULL;
13826 }
13827 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13828 if (!vn) return NULL;
13829 if (!void_node) void_node = vn;
13830 node = RNODE_IF(node)->nd_else;
13831 break;
13832
13833 case NODE_AND:
13834 case NODE_OR:
13835 node = RNODE_AND(node)->nd_1st;
13836 break;
13837
13838 case NODE_LASGN:
13839 case NODE_DASGN:
13840 case NODE_MASGN:
13841 mark_lvar_used(p, node);
13842 return NULL;
13843
13844 default:
13845 return NULL;
13846 }
13847 }
13848
13849 return NULL;
13850}
13851
13852static int
13853value_expr_gen(struct parser_params *p, NODE *node)
13854{
13855 NODE *void_node = value_expr_check(p, node);
13856 if (void_node) {
13857 yyerror1(&void_node->nd_loc, "void value expression");
13858 /* or "control never reach"? */
13859 return FALSE;
13860 }
13861 return TRUE;
13862}
13863
13864static void
13865void_expr(struct parser_params *p, NODE *node)
13866{
13867 const char *useless = 0;
13868
13869 if (!RTEST(ruby_verbose)) return;
13870
13871 if (!node || !(node = nd_once_body(node))) return;
13872 switch (nd_type(node)) {
13873 case NODE_OPCALL:
13874 switch (RNODE_OPCALL(node)->nd_mid) {
13875 case '+':
13876 case '-':
13877 case '*':
13878 case '/':
13879 case '%':
13880 case tPOW:
13881 case tUPLUS:
13882 case tUMINUS:
13883 case '|':
13884 case '^':
13885 case '&':
13886 case tCMP:
13887 case '>':
13888 case tGEQ:
13889 case '<':
13890 case tLEQ:
13891 case tEQ:
13892 case tNEQ:
13893 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13894 break;
13895 }
13896 break;
13897
13898 case NODE_LVAR:
13899 case NODE_DVAR:
13900 case NODE_GVAR:
13901 case NODE_IVAR:
13902 case NODE_CVAR:
13903 case NODE_NTH_REF:
13904 case NODE_BACK_REF:
13905 useless = "a variable";
13906 break;
13907 case NODE_CONST:
13908 useless = "a constant";
13909 break;
13910 case NODE_LIT:
13911 case NODE_STR:
13912 case NODE_DSTR:
13913 case NODE_DREGX:
13914 useless = "a literal";
13915 break;
13916 case NODE_COLON2:
13917 case NODE_COLON3:
13918 useless = "::";
13919 break;
13920 case NODE_DOT2:
13921 useless = "..";
13922 break;
13923 case NODE_DOT3:
13924 useless = "...";
13925 break;
13926 case NODE_SELF:
13927 useless = "self";
13928 break;
13929 case NODE_NIL:
13930 useless = "nil";
13931 break;
13932 case NODE_TRUE:
13933 useless = "true";
13934 break;
13935 case NODE_FALSE:
13936 useless = "false";
13937 break;
13938 case NODE_DEFINED:
13939 useless = "defined?";
13940 break;
13941 }
13942
13943 if (useless) {
13944 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
13945 }
13946}
13947
13948static NODE *
13949void_stmts(struct parser_params *p, NODE *node)
13950{
13951 NODE *const n = node;
13952 if (!RTEST(ruby_verbose)) return n;
13953 if (!node) return n;
13954 if (!nd_type_p(node, NODE_BLOCK)) return n;
13955
13956 while (RNODE_BLOCK(node)->nd_next) {
13957 void_expr(p, RNODE_BLOCK(node)->nd_head);
13958 node = RNODE_BLOCK(node)->nd_next;
13959 }
13960 return n;
13961}
13962
13963static NODE *
13964remove_begin(NODE *node)
13965{
13966 NODE **n = &node, *n1 = node;
13967 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
13968 *n = n1 = RNODE_BEGIN(n1)->nd_body;
13969 }
13970 return node;
13971}
13972
13973static NODE *
13974remove_begin_all(NODE *node)
13975{
13976 NODE **n = &node, *n1 = node;
13977 while (n1 && nd_type_p(n1, NODE_BEGIN)) {
13978 *n = n1 = RNODE_BEGIN(n1)->nd_body;
13979 }
13980 return node;
13981}
13982
13983static void
13984reduce_nodes(struct parser_params *p, NODE **body)
13985{
13986 NODE *node = *body;
13987
13988 if (!node) {
13989 *body = NEW_NIL(&NULL_LOC);
13990 return;
13991 }
13992#define subnodes(type, n1, n2) \
13993 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
13994 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
13995 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
13996
13997 while (node) {
13998 int newline = (int)(nd_fl_newline(node));
13999 switch (nd_type(node)) {
14000 end:
14001 case NODE_NIL:
14002 *body = 0;
14003 return;
14004 case NODE_RETURN:
14005 *body = node = RNODE_RETURN(node)->nd_stts;
14006 if (newline && node) nd_set_fl_newline(node);
14007 continue;
14008 case NODE_BEGIN:
14009 *body = node = RNODE_BEGIN(node)->nd_body;
14010 if (newline && node) nd_set_fl_newline(node);
14011 continue;
14012 case NODE_BLOCK:
14013 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14014 break;
14015 case NODE_IF:
14016 case NODE_UNLESS:
14017 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14018 return;
14019 case NODE_CASE:
14020 body = &RNODE_CASE(node)->nd_body;
14021 break;
14022 case NODE_WHEN:
14023 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14024 break;
14025 case NODE_ENSURE:
14026 if (!subnodes(RNODE_ENSURE, nd_head, nd_resq)) goto end;
14027 break;
14028 case NODE_RESCUE:
14029 newline = 0; // RESBODY should not be a NEWLINE
14030 if (RNODE_RESCUE(node)->nd_else) {
14031 body = &RNODE_RESCUE(node)->nd_resq;
14032 break;
14033 }
14034 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14035 break;
14036 default:
14037 return;
14038 }
14039 node = *body;
14040 if (newline && node) nd_set_fl_newline(node);
14041 }
14042
14043#undef subnodes
14044}
14045
14046static int
14047is_static_content(NODE *node)
14048{
14049 if (!node) return 1;
14050 switch (nd_type(node)) {
14051 case NODE_HASH:
14052 if (!(node = RNODE_HASH(node)->nd_head)) break;
14053 case NODE_LIST:
14054 do {
14055 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14056 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14057 case NODE_LIT:
14058 case NODE_STR:
14059 case NODE_NIL:
14060 case NODE_TRUE:
14061 case NODE_FALSE:
14062 case NODE_ZLIST:
14063 break;
14064 default:
14065 return 0;
14066 }
14067 return 1;
14068}
14069
14070static int
14071assign_in_cond(struct parser_params *p, NODE *node)
14072{
14073 switch (nd_type(node)) {
14074 case NODE_MASGN:
14075 case NODE_LASGN:
14076 case NODE_DASGN:
14077 case NODE_GASGN:
14078 case NODE_IASGN:
14079 case NODE_CVASGN:
14080 case NODE_CDECL:
14081 break;
14082
14083 default:
14084 return 0;
14085 }
14086
14087 if (!get_nd_value(p, node)) return 1;
14088 if (is_static_content(get_nd_value(p, node))) {
14089 /* reports always */
14090 parser_warn(p, get_nd_value(p, node), "found `= literal' in conditional, should be ==");
14091 }
14092 return 1;
14093}
14094
14095enum cond_type {
14096 COND_IN_OP,
14097 COND_IN_COND,
14098 COND_IN_FF
14099};
14100
14101#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14102 switch (t) { \
14103 case COND_IN_OP: break; \
14104 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14105 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14106 } \
14107} while (0)
14108
14109static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14110
14111static NODE*
14112range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14113{
14114 enum node_type type;
14115
14116 if (node == 0) return 0;
14117
14118 type = nd_type(node);
14119 value_expr(node);
14120 if (type == NODE_LIT && FIXNUM_P(RNODE_LIT(node)->nd_lit)) {
14121 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
14122 ID lineno = rb_intern("$.");
14123 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14124 }
14125 return cond0(p, node, COND_IN_FF, loc, true);
14126}
14127
14128static NODE*
14129cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14130{
14131 if (node == 0) return 0;
14132 if (!(node = nd_once_body(node))) return 0;
14133 assign_in_cond(p, node);
14134
14135 switch (nd_type(node)) {
14136 case NODE_BEGIN:
14137 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14138 break;
14139
14140 case NODE_DSTR:
14141 case NODE_EVSTR:
14142 case NODE_STR:
14143 SWITCH_BY_COND_TYPE(type, warn, "string ");
14144 break;
14145
14146 case NODE_DREGX:
14147 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14148
14149 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14150
14151 case NODE_BLOCK:
14152 RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head = cond0(p, RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head, type, loc, false);
14153 break;
14154
14155 case NODE_AND:
14156 case NODE_OR:
14157 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14158 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14159 break;
14160
14161 case NODE_DOT2:
14162 case NODE_DOT3:
14163 if (!top) break;
14164 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14165 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14166 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
14167 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
14168 break;
14169
14170 case NODE_DSYM:
14171 warn_symbol:
14172 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14173 break;
14174
14175 case NODE_LIT:
14176 if (RB_TYPE_P(RNODE_LIT(node)->nd_lit, T_REGEXP)) {
14177 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14178 nd_set_type(node, NODE_MATCH);
14179 }
14180 else if (RNODE_LIT(node)->nd_lit == Qtrue ||
14181 RNODE_LIT(node)->nd_lit == Qfalse) {
14182 /* booleans are OK, e.g., while true */
14183 }
14184 else if (SYMBOL_P(RNODE_LIT(node)->nd_lit)) {
14185 goto warn_symbol;
14186 }
14187 else {
14188 SWITCH_BY_COND_TYPE(type, warning, "");
14189 }
14190 default:
14191 break;
14192 }
14193 return node;
14194}
14195
14196static NODE*
14197cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14198{
14199 if (node == 0) return 0;
14200 return cond0(p, node, COND_IN_COND, loc, true);
14201}
14202
14203static NODE*
14204method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14205{
14206 if (node == 0) return 0;
14207 return cond0(p, node, COND_IN_OP, loc, true);
14208}
14209
14210static NODE*
14211new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14212{
14213 YYLTYPE loc = {*pos, *pos};
14214 return NEW_NIL(&loc);
14215}
14216
14217static NODE*
14218new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14219{
14220 if (!cc) return right;
14221 cc = cond0(p, cc, COND_IN_COND, loc, true);
14222 return newline_node(NEW_IF(cc, left, right, loc));
14223}
14224
14225static NODE*
14226new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14227{
14228 if (!cc) return right;
14229 cc = cond0(p, cc, COND_IN_COND, loc, true);
14230 return newline_node(NEW_UNLESS(cc, left, right, loc));
14231}
14232
14233#define NEW_AND_OR(type, f, s, loc) (type == NODE_AND ? NEW_AND(f,s,loc) : NEW_OR(f,s,loc))
14234
14235static NODE*
14236logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14237 const YYLTYPE *op_loc, const YYLTYPE *loc)
14238{
14239 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14240 NODE *op;
14241 value_expr(left);
14242 if (left && nd_type_p(left, type)) {
14243 NODE *node = left, *second;
14244 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14245 node = second;
14246 }
14247 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc);
14248 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14249 left->nd_loc.end_pos = loc->end_pos;
14250 return left;
14251 }
14252 op = NEW_AND_OR(type, left, right, loc);
14253 nd_set_line(op, op_loc->beg_pos.lineno);
14254 return op;
14255}
14256
14257#undef NEW_AND_OR
14258
14259static void
14260no_blockarg(struct parser_params *p, NODE *node)
14261{
14262 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14263 compile_error(p, "block argument should not be given");
14264 }
14265}
14266
14267static NODE *
14268ret_args(struct parser_params *p, NODE *node)
14269{
14270 if (node) {
14271 no_blockarg(p, node);
14272 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14273 node = RNODE_LIST(node)->nd_head;
14274 }
14275 }
14276 return node;
14277}
14278
14279static NODE *
14280new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14281{
14282 if (node) no_blockarg(p, node);
14283
14284 return NEW_YIELD(node, loc);
14285}
14286
14287static VALUE
14288negate_lit(struct parser_params *p, VALUE lit)
14289{
14290 if (FIXNUM_P(lit)) {
14291 return LONG2FIX(-FIX2LONG(lit));
14292 }
14293 if (SPECIAL_CONST_P(lit)) {
14294#if USE_FLONUM
14295 if (FLONUM_P(lit)) {
14296 return DBL2NUM(-RFLOAT_VALUE(lit));
14297 }
14298#endif
14299 goto unknown;
14300 }
14301 switch (BUILTIN_TYPE(lit)) {
14302 case T_BIGNUM:
14303 bignum_negate(lit);
14304 lit = rb_big_norm(lit);
14305 break;
14306 case T_RATIONAL:
14307 rational_set_num(lit, negate_lit(p, rational_get_num(lit)));
14308 break;
14309 case T_COMPLEX:
14310 rcomplex_set_real(lit, negate_lit(p, rcomplex_get_real(lit)));
14311 rcomplex_set_imag(lit, negate_lit(p, rcomplex_get_imag(lit)));
14312 break;
14313 case T_FLOAT:
14314 lit = DBL2NUM(-RFLOAT_VALUE(lit));
14315 break;
14316 unknown:
14317 default:
14318 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
14319 rb_builtin_class_name(lit));
14320 break;
14321 }
14322 return lit;
14323}
14324
14325static NODE *
14326arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14327{
14328 if (node2) {
14329 if (!node1) return (NODE *)node2;
14330 node2->nd_head = node1;
14331 nd_set_first_lineno(node2, nd_first_lineno(node1));
14332 nd_set_first_column(node2, nd_first_column(node1));
14333 return (NODE *)node2;
14334 }
14335 return node1;
14336}
14337
14338static bool
14339args_info_empty_p(struct rb_args_info *args)
14340{
14341 if (args->pre_args_num) return false;
14342 if (args->post_args_num) return false;
14343 if (args->rest_arg) return false;
14344 if (args->opt_args) return false;
14345 if (args->block_arg) return false;
14346 if (args->kw_args) return false;
14347 if (args->kw_rest_arg) return false;
14348 return true;
14349}
14350
14351static rb_node_args_t *
14352new_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)
14353{
14354 struct rb_args_info *args = &tail->nd_ainfo;
14355
14356 if (args->forwarding) {
14357 if (rest_arg) {
14358 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14359 return tail;
14360 }
14361 rest_arg = idFWD_REST;
14362 }
14363
14364 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
14365 args->pre_init = pre_args ? pre_args->nd_next : 0;
14366
14367 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
14368 args->post_init = post_args ? post_args->nd_next : 0;
14369 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14370
14371 args->rest_arg = rest_arg;
14372
14373 args->opt_args = opt_args;
14374
14375#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14376 args->ruby2_keywords = args->forwarding;
14377#else
14378 args->ruby2_keywords = 0;
14379#endif
14380
14381 nd_set_loc(RNODE(tail), loc);
14382
14383 return tail;
14384}
14385
14386static rb_node_args_t *
14387new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14388{
14389 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14390 struct rb_args_info *args = &node->nd_ainfo;
14391 if (p->error_p) return node;
14392
14393 args->block_arg = block;
14394 args->kw_args = kw_args;
14395
14396 if (kw_args) {
14397 /*
14398 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14399 * variable order: k1, kr1, k2, &b, internal_id, krest
14400 * #=> <reorder>
14401 * variable order: kr1, k1, k2, internal_id, krest, &b
14402 */
14403 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14404 struct vtable *vtargs = p->lvtbl->args;
14405 rb_node_kw_arg_t *kwn = kw_args;
14406
14407 if (block) block = vtargs->tbl[vtargs->pos-1];
14408 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14409 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14410 while (kwn) {
14411 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14412 --kw_vars;
14413 --required_kw_vars;
14414 kwn = kwn->nd_next;
14415 }
14416
14417 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14418 ID vid = get_nd_vid(p, kwn->nd_body);
14419 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14420 *required_kw_vars++ = vid;
14421 }
14422 else {
14423 *kw_vars++ = vid;
14424 }
14425 }
14426
14427 arg_var(p, kw_bits);
14428 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14429 if (block) arg_var(p, block);
14430
14431 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14432 }
14433 else if (kw_rest_arg == idNil) {
14434 args->no_kwarg = 1;
14435 }
14436 else if (kw_rest_arg) {
14437 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14438 }
14439
14440 return node;
14441}
14442
14443static rb_node_args_t *
14444args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam)
14445{
14446 if (max_numparam > NO_PARAM) {
14447 if (!args) {
14448 YYLTYPE loc = RUBY_INIT_YYLLOC();
14449 args = new_args_tail(p, 0, 0, 0, 0);
14450 nd_set_loc(RNODE(args), &loc);
14451 }
14452 args->nd_ainfo.pre_args_num = max_numparam;
14453 }
14454 return args;
14455}
14456
14457static NODE*
14458new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14459{
14460 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14461
14462 if (pre_arg) {
14463 NODE *pre_args = NEW_LIST(pre_arg, loc);
14464 if (RNODE_ARYPTN(aryptn)->pre_args) {
14465 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14466 }
14467 else {
14468 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14469 }
14470 }
14471 return aryptn;
14472}
14473
14474static NODE*
14475new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14476{
14477 if (has_rest) {
14478 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14479 }
14480 else {
14481 rest_arg = NULL;
14482 }
14483 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14484
14485 return node;
14486}
14487
14488static NODE*
14489new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14490{
14491 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14492
14493 return fndptn;
14494}
14495
14496static NODE*
14497new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14498{
14499 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14500 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14501 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14502
14503 return node;
14504}
14505
14506static NODE*
14507new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14508{
14509 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14510 return hshptn;
14511}
14512
14513static NODE*
14514new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14515{
14516 NODE *node, *kw_rest_arg_node;
14517
14518 if (kw_rest_arg == idNil) {
14519 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14520 }
14521 else if (kw_rest_arg) {
14522 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14523 }
14524 else {
14525 kw_rest_arg_node = NULL;
14526 }
14527
14528 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14529
14530 return node;
14531}
14532
14533static NODE*
14534dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14535{
14536 VALUE lit;
14537
14538 if (!node) {
14539 return NEW_LIT(ID2SYM(idNULL), loc);
14540 }
14541
14542 switch (nd_type(node)) {
14543 case NODE_DSTR:
14544 nd_set_type(node, NODE_DSYM);
14545 nd_set_loc(node, loc);
14546 break;
14547 case NODE_STR:
14548 lit = RNODE_STR(node)->nd_lit;
14549 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(node)->nd_lit = ID2SYM(rb_intern_str(lit)));
14550 nd_set_type(node, NODE_LIT);
14551 nd_set_loc(node, loc);
14552 break;
14553 default:
14554 node = NEW_DSYM(Qnil, 1, NEW_LIST(node, loc), loc);
14555 break;
14556 }
14557 return node;
14558}
14559
14560static int
14561append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
14562{
14563 NODE *node = (NODE *)v;
14564 NODE **result = (NODE **)h;
14565 RNODE_LIST(node)->as.nd_alen = 2;
14566 RNODE_LIST(RNODE_LIST(node)->nd_next)->as.nd_end = RNODE_LIST(node)->nd_next;
14567 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next = 0;
14568 if (*result)
14569 list_concat(*result, node);
14570 else
14571 *result = node;
14572 return ST_CONTINUE;
14573}
14574
14575static NODE *
14576remove_duplicate_keys(struct parser_params *p, NODE *hash)
14577{
14578 struct st_hash_type literal_type = {
14579 literal_cmp,
14580 literal_hash,
14581 };
14582
14583 st_table *literal_keys = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14584 NODE *result = 0;
14585 NODE *last_expr = 0;
14586 rb_code_location_t loc = hash->nd_loc;
14587 while (hash && RNODE_LIST(hash)->nd_next) {
14588 NODE *head = RNODE_LIST(hash)->nd_head;
14589 NODE *value = RNODE_LIST(hash)->nd_next;
14590 NODE *next = RNODE_LIST(value)->nd_next;
14591 st_data_t key = (st_data_t)head;
14592 st_data_t data;
14593 RNODE_LIST(value)->nd_next = 0;
14594 if (!head) {
14595 key = (st_data_t)value;
14596 }
14597 else if (nd_type_p(head, NODE_LIT) &&
14598 st_delete(literal_keys, (key = (st_data_t)RNODE_LIT(head)->nd_lit, &key), &data)) {
14599 NODE *dup_value = (RNODE_LIST((NODE *)data))->nd_next;
14600 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
14601 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
14602 RNODE_LIT(head)->nd_lit, nd_line(head));
14603 if (dup_value == last_expr) {
14604 RNODE_LIST(value)->nd_head = block_append(p, RNODE_LIST(dup_value)->nd_head, RNODE_LIST(value)->nd_head);
14605 }
14606 else {
14607 RNODE_LIST(last_expr)->nd_head = block_append(p, RNODE_LIST(dup_value)->nd_head, RNODE_LIST(last_expr)->nd_head);
14608 }
14609 }
14610 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
14611 last_expr = !head || nd_type_p(head, NODE_LIT) ? value : head;
14612 hash = next;
14613 }
14614 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
14615 st_free_table(literal_keys);
14616 if (hash) {
14617 if (!result) result = hash;
14618 else list_concat(result, hash);
14619 }
14620 result->nd_loc = loc;
14621 return result;
14622}
14623
14624static NODE *
14625new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14626{
14627 if (hash) hash = remove_duplicate_keys(p, hash);
14628 return NEW_HASH(hash, loc);
14629}
14630#endif
14631
14632static void
14633error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14634{
14635 if (is_private_local_id(p, id)) {
14636 return;
14637 }
14638 if (st_is_member(p->pvtbl, id)) {
14639 yyerror1(loc, "duplicated variable name");
14640 }
14641 else {
14642 st_insert(p->pvtbl, (st_data_t)id, 0);
14643 }
14644}
14645
14646static void
14647error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14648{
14649 if (!p->pktbl) {
14650 p->pktbl = st_init_numtable();
14651 }
14652 else if (st_is_member(p->pktbl, key)) {
14653 yyerror1(loc, "duplicated key name");
14654 return;
14655 }
14656 st_insert(p->pktbl, (st_data_t)key, 0);
14657}
14658
14659#ifndef RIPPER
14660static NODE *
14661new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14662{
14663 return NEW_HASH(hash, loc);
14664}
14665#endif /* !RIPPER */
14666
14667#ifndef RIPPER
14668static NODE *
14669new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14670{
14671 NODE *asgn;
14672
14673 if (lhs) {
14674 ID vid = get_nd_vid(p, lhs);
14675 YYLTYPE lhs_loc = lhs->nd_loc;
14676 int shareable = ctxt.shareable_constant_value;
14677 if (shareable) {
14678 switch (nd_type(lhs)) {
14679 case NODE_CDECL:
14680 case NODE_COLON2:
14681 case NODE_COLON3:
14682 break;
14683 default:
14684 shareable = 0;
14685 break;
14686 }
14687 }
14688 if (op == tOROP) {
14689 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14690 set_nd_value(p, lhs, rhs);
14691 nd_set_loc(lhs, loc);
14692 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14693 }
14694 else if (op == tANDOP) {
14695 if (shareable) {
14696 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14697 }
14698 set_nd_value(p, lhs, rhs);
14699 nd_set_loc(lhs, loc);
14700 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14701 }
14702 else {
14703 asgn = lhs;
14704 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14705 if (shareable) {
14706 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14707 }
14708 set_nd_value(p, asgn, rhs);
14709 nd_set_loc(asgn, loc);
14710 }
14711 }
14712 else {
14713 asgn = NEW_BEGIN(0, loc);
14714 }
14715 return asgn;
14716}
14717
14718static NODE *
14719new_ary_op_assign(struct parser_params *p, NODE *ary,
14720 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
14721{
14722 NODE *asgn;
14723
14724 args = make_list(args, args_loc);
14725 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc);
14726 fixpos(asgn, ary);
14727 return asgn;
14728}
14729
14730static NODE *
14731new_attr_op_assign(struct parser_params *p, NODE *lhs,
14732 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
14733{
14734 NODE *asgn;
14735
14736 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
14737 fixpos(asgn, lhs);
14738 return asgn;
14739}
14740
14741static NODE *
14742new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14743{
14744 NODE *asgn;
14745
14746 if (lhs) {
14747 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
14748 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
14749 }
14750 else {
14751 asgn = NEW_BEGIN(0, loc);
14752 }
14753 fixpos(asgn, lhs);
14754 return asgn;
14755}
14756
14757static NODE *
14758const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14759{
14760 if (p->ctxt.in_def) {
14761 yyerror1(loc, "dynamic constant assignment");
14762 }
14763 return NEW_CDECL(0, 0, (path), loc);
14764}
14765#else
14766static VALUE
14767const_decl(struct parser_params *p, VALUE path)
14768{
14769 if (p->ctxt.in_def) {
14770 path = assign_error(p, "dynamic constant assignment", path);
14771 }
14772 return path;
14773}
14774
14775static VALUE
14776assign_error(struct parser_params *p, const char *mesg, VALUE a)
14777{
14778 a = dispatch2(assign_error, ERR_MESG(), a);
14779 ripper_error(p);
14780 return a;
14781}
14782
14783static VALUE
14784var_field(struct parser_params *p, VALUE a)
14785{
14786 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
14787}
14788#endif
14789
14790#ifndef RIPPER
14791static NODE *
14792new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14793{
14794 NODE *result = head;
14795 if (rescue) {
14796 NODE *tmp = rescue_else ? rescue_else : rescue;
14797 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14798
14799 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14800 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14801 }
14802 else if (rescue_else) {
14803 result = block_append(p, result, rescue_else);
14804 }
14805 if (ensure) {
14806 result = NEW_ENSURE(result, ensure, loc);
14807 }
14808 fixpos(result, head);
14809 return result;
14810}
14811#endif
14812
14813static void
14814warn_unused_var(struct parser_params *p, struct local_vars *local)
14815{
14816 int cnt;
14817
14818 if (!local->used) return;
14819 cnt = local->used->pos;
14820 if (cnt != local->vars->pos) {
14821 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14822 }
14823#ifndef RIPPER
14824 ID *v = local->vars->tbl;
14825 ID *u = local->used->tbl;
14826 for (int i = 0; i < cnt; ++i) {
14827 if (!v[i] || (u[i] & LVAR_USED)) continue;
14828 if (is_private_local_id(p, v[i])) continue;
14829 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14830 }
14831#endif
14832}
14833
14834static void
14835local_push(struct parser_params *p, int toplevel_scope)
14836{
14837 struct local_vars *local;
14838 int inherits_dvars = toplevel_scope && compile_for_eval;
14839 int warn_unused_vars = RTEST(ruby_verbose);
14840
14841 local = ALLOC(struct local_vars);
14842 local->prev = p->lvtbl;
14843 local->args = vtable_alloc(0);
14844 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14845#ifndef RIPPER
14846 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14847 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14848 local->numparam.outer = 0;
14849 local->numparam.inner = 0;
14850 local->numparam.current = 0;
14851#endif
14852 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14853
14854# if WARN_PAST_SCOPE
14855 local->past = 0;
14856# endif
14857 CMDARG_PUSH(0);
14858 COND_PUSH(0);
14859 p->lvtbl = local;
14860}
14861
14862static void
14863vtable_chain_free(struct parser_params *p, struct vtable *table)
14864{
14865 while (!DVARS_TERMINAL_P(table)) {
14866 struct vtable *cur_table = table;
14867 table = cur_table->prev;
14868 vtable_free(cur_table);
14869 }
14870}
14871
14872static void
14873local_free(struct parser_params *p, struct local_vars *local)
14874{
14875 vtable_chain_free(p, local->used);
14876
14877# if WARN_PAST_SCOPE
14878 vtable_chain_free(p, local->past);
14879# endif
14880
14881 vtable_chain_free(p, local->args);
14882 vtable_chain_free(p, local->vars);
14883
14884 ruby_sized_xfree(local, sizeof(struct local_vars));
14885}
14886
14887static void
14888local_pop(struct parser_params *p)
14889{
14890 struct local_vars *local = p->lvtbl->prev;
14891 if (p->lvtbl->used) {
14892 warn_unused_var(p, p->lvtbl);
14893 }
14894
14895 local_free(p, p->lvtbl);
14896 p->lvtbl = local;
14897
14898 CMDARG_POP();
14899 COND_POP();
14900}
14901
14902#ifndef RIPPER
14903static rb_ast_id_table_t *
14904local_tbl(struct parser_params *p)
14905{
14906 int cnt_args = vtable_size(p->lvtbl->args);
14907 int cnt_vars = vtable_size(p->lvtbl->vars);
14908 int cnt = cnt_args + cnt_vars;
14909 int i, j;
14910 rb_ast_id_table_t *tbl;
14911
14912 if (cnt <= 0) return 0;
14913 tbl = rb_ast_new_local_table(p->ast, cnt);
14914 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14915 /* remove IDs duplicated to warn shadowing */
14916 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14917 ID id = p->lvtbl->vars->tbl[i];
14918 if (!vtable_included(p->lvtbl->args, id)) {
14919 tbl->ids[j++] = id;
14920 }
14921 }
14922 if (j < cnt) {
14923 tbl = rb_ast_resize_latest_local_table(p->ast, j);
14924 }
14925
14926 return tbl;
14927}
14928
14929#endif
14930
14931static void
14932numparam_name(struct parser_params *p, ID id)
14933{
14934 if (!NUMPARAM_ID_P(id)) return;
14935 compile_error(p, "_%d is reserved for numbered parameter",
14936 NUMPARAM_ID_TO_IDX(id));
14937}
14938
14939static void
14940arg_var(struct parser_params *p, ID id)
14941{
14942 numparam_name(p, id);
14943 vtable_add(p->lvtbl->args, id);
14944}
14945
14946static void
14947local_var(struct parser_params *p, ID id)
14948{
14949 numparam_name(p, id);
14950 vtable_add(p->lvtbl->vars, id);
14951 if (p->lvtbl->used) {
14952 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
14953 }
14954}
14955
14956static int
14957local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
14958{
14959 struct vtable *vars, *args, *used;
14960
14961 vars = p->lvtbl->vars;
14962 args = p->lvtbl->args;
14963 used = p->lvtbl->used;
14964
14965 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
14966 vars = vars->prev;
14967 args = args->prev;
14968 if (used) used = used->prev;
14969 }
14970
14971 if (vars && vars->prev == DVARS_INHERIT) {
14972 return rb_local_defined(id, p->parent_iseq);
14973 }
14974 else if (vtable_included(args, id)) {
14975 return 1;
14976 }
14977 else {
14978 int i = vtable_included(vars, id);
14979 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
14980 return i != 0;
14981 }
14982}
14983
14984static int
14985local_id(struct parser_params *p, ID id)
14986{
14987 return local_id_ref(p, id, NULL);
14988}
14989
14990static int
14991check_forwarding_args(struct parser_params *p)
14992{
14993 if (local_id(p, idFWD_ALL)) return TRUE;
14994 compile_error(p, "unexpected ...");
14995 return FALSE;
14996}
14997
14998static void
14999add_forwarding_args(struct parser_params *p)
15000{
15001 arg_var(p, idFWD_REST);
15002#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15003 arg_var(p, idFWD_KWREST);
15004#endif
15005 arg_var(p, idFWD_BLOCK);
15006 arg_var(p, idFWD_ALL);
15007}
15008
15009static void
15010forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15011{
15012 bool conflict = false;
15013
15014 struct vtable *vars, *args;
15015
15016 vars = p->lvtbl->vars;
15017 args = p->lvtbl->args;
15018
15019 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15020 vars = vars->prev;
15021 args = args->prev;
15022 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15023 }
15024
15025 bool found = false;
15026 if (vars && vars->prev == DVARS_INHERIT) {
15027 found = (rb_local_defined(arg, p->parent_iseq) &&
15028 !(all && rb_local_defined(all, p->parent_iseq)));
15029 }
15030 else {
15031 found = (vtable_included(args, arg) &&
15032 !(all && vtable_included(args, all)));
15033 }
15034
15035 if (!found) {
15036 compile_error(p, "no anonymous %s parameter", var);
15037 }
15038 else if (conflict) {
15039 compile_error(p, "anonymous %s parameter is also used within block", var);
15040 }
15041}
15042
15043#ifndef RIPPER
15044static NODE *
15045new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15046{
15047 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15048#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15049 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15050#endif
15051 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
15052 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc);
15053#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15054 args = arg_append(p, args, new_hash(p, kwrest, loc), loc);
15055#endif
15056 return arg_blk_pass(args, block);
15057}
15058#endif
15059
15060static NODE *
15061numparam_push(struct parser_params *p)
15062{
15063#ifndef RIPPER
15064 struct local_vars *local = p->lvtbl;
15065 NODE *inner = local->numparam.inner;
15066 if (!local->numparam.outer) {
15067 local->numparam.outer = local->numparam.current;
15068 }
15069 local->numparam.inner = 0;
15070 local->numparam.current = 0;
15071 return inner;
15072#else
15073 return 0;
15074#endif
15075}
15076
15077static void
15078numparam_pop(struct parser_params *p, NODE *prev_inner)
15079{
15080#ifndef RIPPER
15081 struct local_vars *local = p->lvtbl;
15082 if (prev_inner) {
15083 /* prefer first one */
15084 local->numparam.inner = prev_inner;
15085 }
15086 else if (local->numparam.current) {
15087 /* current and inner are exclusive */
15088 local->numparam.inner = local->numparam.current;
15089 }
15090 if (p->max_numparam > NO_PARAM) {
15091 /* current and outer are exclusive */
15092 local->numparam.current = local->numparam.outer;
15093 local->numparam.outer = 0;
15094 }
15095 else {
15096 /* no numbered parameter */
15097 local->numparam.current = 0;
15098 }
15099#endif
15100}
15101
15102static const struct vtable *
15103dyna_push(struct parser_params *p)
15104{
15105 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15106 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15107 if (p->lvtbl->used) {
15108 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15109 }
15110 return p->lvtbl->args;
15111}
15112
15113static void
15114dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15115{
15116 struct vtable *tmp = *vtblp;
15117 *vtblp = tmp->prev;
15118# if WARN_PAST_SCOPE
15119 if (p->past_scope_enabled) {
15120 tmp->prev = p->lvtbl->past;
15121 p->lvtbl->past = tmp;
15122 return;
15123 }
15124# endif
15125 vtable_free(tmp);
15126}
15127
15128static void
15129dyna_pop_1(struct parser_params *p)
15130{
15131 struct vtable *tmp;
15132
15133 if ((tmp = p->lvtbl->used) != 0) {
15134 warn_unused_var(p, p->lvtbl);
15135 p->lvtbl->used = p->lvtbl->used->prev;
15136 vtable_free(tmp);
15137 }
15138 dyna_pop_vtable(p, &p->lvtbl->args);
15139 dyna_pop_vtable(p, &p->lvtbl->vars);
15140}
15141
15142static void
15143dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15144{
15145 while (p->lvtbl->args != lvargs) {
15146 dyna_pop_1(p);
15147 if (!p->lvtbl->args) {
15148 struct local_vars *local = p->lvtbl->prev;
15149 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15150 p->lvtbl = local;
15151 }
15152 }
15153 dyna_pop_1(p);
15154}
15155
15156static int
15157dyna_in_block(struct parser_params *p)
15158{
15159 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15160}
15161
15162static int
15163dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15164{
15165 struct vtable *vars, *args, *used;
15166 int i;
15167
15168 args = p->lvtbl->args;
15169 vars = p->lvtbl->vars;
15170 used = p->lvtbl->used;
15171
15172 while (!DVARS_TERMINAL_P(vars)) {
15173 if (vtable_included(args, id)) {
15174 return 1;
15175 }
15176 if ((i = vtable_included(vars, id)) != 0) {
15177 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15178 return 1;
15179 }
15180 args = args->prev;
15181 vars = vars->prev;
15182 if (!vidrefp) used = 0;
15183 if (used) used = used->prev;
15184 }
15185
15186 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15187 return rb_dvar_defined(id, p->parent_iseq);
15188 }
15189
15190 return 0;
15191}
15192
15193static int
15194dvar_defined(struct parser_params *p, ID id)
15195{
15196 return dvar_defined_ref(p, id, NULL);
15197}
15198
15199static int
15200dvar_curr(struct parser_params *p, ID id)
15201{
15202 return (vtable_included(p->lvtbl->args, id) ||
15203 vtable_included(p->lvtbl->vars, id));
15204}
15205
15206static void
15207reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
15208{
15209 compile_error(p,
15210 "regexp encoding option '%c' differs from source encoding '%s'",
15211 c, rb_enc_name(rb_enc_get(str)));
15212}
15213
15214#ifndef RIPPER
15215int
15216rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
15217{
15218 int c = RE_OPTION_ENCODING_IDX(options);
15219
15220 if (c) {
15221 int opt, idx;
15222 rb_char_to_option_kcode(c, &opt, &idx);
15223 if (idx != ENCODING_GET(str) &&
15224 !is_ascii_string(str)) {
15225 goto error;
15226 }
15227 ENCODING_SET(str, idx);
15228 }
15229 else if (RE_OPTION_ENCODING_NONE(options)) {
15230 if (!ENCODING_IS_ASCII8BIT(str) &&
15231 !is_ascii_string(str)) {
15232 c = 'n';
15233 goto error;
15234 }
15235 rb_enc_associate(str, rb_ascii8bit_encoding());
15236 }
15237 else if (rb_is_usascii_enc(p->enc)) {
15238 if (!is_ascii_string(str)) {
15239 /* raise in re.c */
15240 rb_enc_associate(str, rb_usascii_encoding());
15241 }
15242 else {
15243 rb_enc_associate(str, rb_ascii8bit_encoding());
15244 }
15245 }
15246 return 0;
15247
15248 error:
15249 return c;
15250}
15251
15252static void
15253reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
15254{
15255 int c = rb_reg_fragment_setenc(p, str, options);
15256 if (c) reg_fragment_enc_error(p, str, c);
15257}
15258
15259static int
15260reg_fragment_check(struct parser_params* p, VALUE str, int options)
15261{
15262 VALUE err;
15263 reg_fragment_setenc(p, str, options);
15264 err = rb_reg_check_preprocess(str);
15265 if (err != Qnil) {
15266 err = rb_obj_as_string(err);
15267 compile_error(p, "%"PRIsVALUE, err);
15268 return 0;
15269 }
15270 return 1;
15271}
15272
15273#ifndef UNIVERSAL_PARSER
15274typedef struct {
15275 struct parser_params* parser;
15276 rb_encoding *enc;
15277 NODE *succ_block;
15278 const YYLTYPE *loc;
15279} reg_named_capture_assign_t;
15280
15281static int
15282reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15283 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15284{
15285 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15286 struct parser_params* p = arg->parser;
15287 rb_encoding *enc = arg->enc;
15288 long len = name_end - name;
15289 const char *s = (const char *)name;
15290
15291 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc);
15292}
15293
15294static NODE *
15295reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
15296{
15297 reg_named_capture_assign_t arg;
15298
15299 arg.parser = p;
15300 arg.enc = rb_enc_get(regexp);
15301 arg.succ_block = 0;
15302 arg.loc = loc;
15303 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15304
15305 if (!arg.succ_block) return 0;
15306 return RNODE_BLOCK(arg.succ_block)->nd_next;
15307}
15308#endif
15309
15310int
15311rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15312 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc)
15313{
15314 ID var;
15315 NODE *node, *succ;
15316
15317 if (!len) return ST_CONTINUE;
15318 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15319 return ST_CONTINUE;
15320
15321 var = intern_cstr(s, len, enc);
15322 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15323 if (!lvar_defined(p, var)) return ST_CONTINUE;
15324 }
15325 node = node_assign(p, assignable(p, var, 0, loc), NEW_LIT(ID2SYM(var), loc), NO_LEX_CTXT, loc);
15326 succ = *succ_block;
15327 if (!succ) succ = NEW_BEGIN(0, loc);
15328 succ = block_append(p, succ, node);
15329 *succ_block = succ;
15330 return ST_CONTINUE;
15331}
15332
15333static VALUE
15334parser_reg_compile(struct parser_params* p, VALUE str, int options)
15335{
15336 reg_fragment_setenc(p, str, options);
15337 return rb_parser_reg_compile(p, str, options);
15338}
15339
15340VALUE
15341rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15342{
15343 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15344}
15345
15346static VALUE
15347reg_compile(struct parser_params* p, VALUE str, int options)
15348{
15349 VALUE re;
15350 VALUE err;
15351
15352 err = rb_errinfo();
15353 re = parser_reg_compile(p, str, options);
15354 if (NIL_P(re)) {
15355 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15356 rb_set_errinfo(err);
15357 compile_error(p, "%"PRIsVALUE, m);
15358 return Qnil;
15359 }
15360 return re;
15361}
15362#else
15363static VALUE
15364parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
15365{
15366 VALUE err = rb_errinfo();
15367 VALUE re;
15368 str = ripper_is_node_yylval(p, str) ? RNODE_RIPPER(str)->nd_cval : str;
15369 int c = rb_reg_fragment_setenc(p, str, options);
15370 if (c) reg_fragment_enc_error(p, str, c);
15371 re = rb_parser_reg_compile(p, str, options);
15372 if (NIL_P(re)) {
15373 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
15374 rb_set_errinfo(err);
15375 }
15376 return re;
15377}
15378#endif
15379
15380#ifndef RIPPER
15381void
15382rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15383{
15384 p->do_print = print;
15385 p->do_loop = loop;
15386 p->do_chomp = chomp;
15387 p->do_split = split;
15388}
15389
15390static NODE *
15391parser_append_options(struct parser_params *p, NODE *node)
15392{
15393 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15394 const YYLTYPE *const LOC = &default_location;
15395
15396 if (p->do_print) {
15397 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15398 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15399 LOC);
15400 node = block_append(p, node, print);
15401 }
15402
15403 if (p->do_loop) {
15404 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15405
15406 if (p->do_split) {
15407 ID ifs = rb_intern("$;");
15408 ID fields = rb_intern("$F");
15409 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15410 NODE *split = NEW_GASGN(fields,
15411 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15412 rb_intern("split"), args, LOC),
15413 LOC);
15414 node = block_append(p, split, node);
15415 }
15416 if (p->do_chomp) {
15417 NODE *chomp = NEW_LIT(ID2SYM(rb_intern("chomp")), LOC);
15418 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15419 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15420 }
15421
15422 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC);
15423 }
15424
15425 return node;
15426}
15427
15428void
15429rb_init_parse(void)
15430{
15431 /* just to suppress unused-function warnings */
15432 (void)nodetype;
15433 (void)nodeline;
15434}
15435
15436static ID
15437internal_id(struct parser_params *p)
15438{
15439 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15440}
15441#endif /* !RIPPER */
15442
15443static void
15444parser_initialize(struct parser_params *p)
15445{
15446 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15447 p->command_start = TRUE;
15448 p->ruby_sourcefile_string = Qnil;
15449 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15450 p->node_id = 0;
15451 p->delayed.token = Qnil;
15452 p->frozen_string_literal = -1; /* not specified */
15453#ifdef RIPPER
15454 p->result = Qnil;
15455 p->parsing_thread = Qnil;
15456#else
15457 p->error_buffer = Qfalse;
15458 p->end_expect_token_locations = Qnil;
15459 p->token_id = 0;
15460 p->tokens = Qnil;
15461#endif
15462 p->debug_buffer = Qnil;
15463 p->debug_output = rb_ractor_stdout();
15464 p->enc = rb_utf8_encoding();
15465 p->exits = 0;
15466}
15467
15468#ifdef RIPPER
15469#define rb_ruby_parser_mark ripper_parser_mark
15470#define rb_ruby_parser_free ripper_parser_free
15471#define rb_ruby_parser_memsize ripper_parser_memsize
15472#endif
15473
15474void
15475rb_ruby_parser_mark(void *ptr)
15476{
15477 struct parser_params *p = (struct parser_params*)ptr;
15478
15479 rb_gc_mark(p->lex.input);
15480 rb_gc_mark(p->lex.lastline);
15481 rb_gc_mark(p->lex.nextline);
15482 rb_gc_mark(p->ruby_sourcefile_string);
15483 rb_gc_mark((VALUE)p->ast);
15484 rb_gc_mark(p->case_labels);
15485 rb_gc_mark(p->delayed.token);
15486#ifndef RIPPER
15487 rb_gc_mark(p->debug_lines);
15488 rb_gc_mark(p->error_buffer);
15489 rb_gc_mark(p->end_expect_token_locations);
15490 rb_gc_mark(p->tokens);
15491#else
15492 rb_gc_mark(p->value);
15493 rb_gc_mark(p->result);
15494 rb_gc_mark(p->parsing_thread);
15495#endif
15496 rb_gc_mark(p->debug_buffer);
15497 rb_gc_mark(p->debug_output);
15498#ifdef YYMALLOC
15499 rb_gc_mark((VALUE)p->heap);
15500#endif
15501}
15502
15503void
15504rb_ruby_parser_free(void *ptr)
15505{
15506 struct parser_params *p = (struct parser_params*)ptr;
15507 struct local_vars *local, *prev;
15508#ifdef UNIVERSAL_PARSER
15509 rb_parser_config_t *config = p->config;
15510#endif
15511
15512 if (p->tokenbuf) {
15513 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15514 }
15515
15516 for (local = p->lvtbl; local; local = prev) {
15517 prev = local->prev;
15518 local_free(p, local);
15519 }
15520
15521 {
15522 token_info *ptinfo;
15523 while ((ptinfo = p->token_info) != 0) {
15524 p->token_info = ptinfo->next;
15525 xfree(ptinfo);
15526 }
15527 }
15528 xfree(ptr);
15529
15530#ifdef UNIVERSAL_PARSER
15531 config->counter--;
15532 if (config->counter <= 0) {
15533 rb_ruby_parser_config_free(config);
15534 }
15535#endif
15536}
15537
15538size_t
15539rb_ruby_parser_memsize(const void *ptr)
15540{
15541 struct parser_params *p = (struct parser_params*)ptr;
15542 struct local_vars *local;
15543 size_t size = sizeof(*p);
15544
15545 size += p->toksiz;
15546 for (local = p->lvtbl; local; local = local->prev) {
15547 size += sizeof(*local);
15548 if (local->vars) size += local->vars->capa * sizeof(ID);
15549 }
15550 return size;
15551}
15552
15553#ifdef UNIVERSAL_PARSER
15554rb_parser_config_t *
15555rb_ruby_parser_config_new(void *(*malloc)(size_t size))
15556{
15557 return (rb_parser_config_t *)malloc(sizeof(rb_parser_config_t));
15558}
15559
15560void
15561rb_ruby_parser_config_free(rb_parser_config_t *config)
15562{
15563 config->free(config);
15564}
15565#endif
15566
15567#ifndef UNIVERSAL_PARSER
15568#ifndef RIPPER
15569static const rb_data_type_t parser_data_type = {
15570 "parser",
15571 {
15572 rb_ruby_parser_mark,
15573 rb_ruby_parser_free,
15574 rb_ruby_parser_memsize,
15575 },
15576 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
15577};
15578#endif
15579#endif
15580
15581#ifndef RIPPER
15582#undef rb_reserved_word
15583
15584const struct kwtable *
15585rb_reserved_word(const char *str, unsigned int len)
15586{
15587 return reserved_word(str, len);
15588}
15589
15590#ifdef UNIVERSAL_PARSER
15591rb_parser_t *
15592rb_ruby_parser_allocate(rb_parser_config_t *config)
15593{
15594 /* parser_initialize expects fields to be set to 0 */
15595 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15596 p->config = config;
15597 p->config->counter++;
15598 return p;
15599}
15600
15601rb_parser_t *
15602rb_ruby_parser_new(rb_parser_config_t *config)
15603{
15604 /* parser_initialize expects fields to be set to 0 */
15605 rb_parser_t *p = rb_ruby_parser_allocate(config);
15606 parser_initialize(p);
15607 return p;
15608}
15609#endif
15610
15611rb_parser_t *
15612rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15613{
15614 p->error_buffer = main ? Qfalse : Qnil;
15615 p->parent_iseq = base;
15616 return p;
15617}
15618
15619void
15620rb_ruby_parser_set_script_lines(rb_parser_t *p, VALUE lines)
15621{
15622 if (!RTEST(lines)) {
15623 lines = Qfalse;
15624 }
15625 else if (lines == Qtrue) {
15626 lines = rb_ary_new();
15627 }
15628 else {
15629 Check_Type(lines, T_ARRAY);
15630 rb_ary_modify(lines);
15631 }
15632 p->debug_lines = lines;
15633}
15634
15635void
15636rb_ruby_parser_error_tolerant(rb_parser_t *p)
15637{
15638 p->error_tolerant = 1;
15639 // TODO
15640 p->end_expect_token_locations = rb_ary_new();
15641}
15642
15643void
15644rb_ruby_parser_keep_tokens(rb_parser_t *p)
15645{
15646 p->keep_tokens = 1;
15647 // TODO
15648 p->tokens = rb_ary_new();
15649}
15650
15651#ifndef UNIVERSAL_PARSER
15652rb_ast_t*
15653rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
15654{
15655 struct parser_params *p;
15656
15657 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15658 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15659 return rb_ruby_parser_compile_file_path(p, fname, file, start);
15660}
15661
15662rb_ast_t*
15663rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
15664{
15665 struct parser_params *p;
15666
15667 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15668 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15669 return rb_ruby_parser_compile_generic(p, lex_gets, fname, input, start);
15670}
15671
15672rb_ast_t*
15673rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
15674{
15675 struct parser_params *p;
15676
15677 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15678 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15679 return rb_ruby_parser_compile_string(p, f, s, line);
15680}
15681
15682rb_ast_t*
15683rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
15684{
15685 struct parser_params *p;
15686
15687 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15688 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15689 return rb_ruby_parser_compile_string_path(p, f, s, line);
15690}
15691
15692VALUE
15693rb_parser_encoding(VALUE vparser)
15694{
15695 struct parser_params *p;
15696
15697 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15698 return rb_ruby_parser_encoding(p);
15699}
15700
15701VALUE
15702rb_parser_end_seen_p(VALUE vparser)
15703{
15704 struct parser_params *p;
15705
15706 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15707 return RBOOL(rb_ruby_parser_end_seen_p(p));
15708}
15709
15710void
15711rb_parser_error_tolerant(VALUE vparser)
15712{
15713 struct parser_params *p;
15714
15715 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15716 rb_ruby_parser_error_tolerant(p);
15717}
15718
15719void
15720rb_parser_set_script_lines(VALUE vparser, VALUE lines)
15721{
15722 struct parser_params *p;
15723
15724 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15725 rb_ruby_parser_set_script_lines(p, lines);
15726}
15727
15728void
15729rb_parser_keep_tokens(VALUE vparser)
15730{
15731 struct parser_params *p;
15732
15733 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15734 rb_ruby_parser_keep_tokens(p);
15735}
15736
15737VALUE
15738rb_parser_new(void)
15739{
15740 struct parser_params *p;
15741 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
15742 &parser_data_type, p);
15743 parser_initialize(p);
15744 return parser;
15745}
15746
15747VALUE
15748rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
15749{
15750 struct parser_params *p;
15751
15752 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15753 rb_ruby_parser_set_context(p, base, main);
15754 return vparser;
15755}
15756
15757void
15758rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
15759{
15760 struct parser_params *p;
15761
15762 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15763 rb_ruby_parser_set_options(p, print, loop, chomp, split);
15764}
15765
15766VALUE
15767rb_parser_set_yydebug(VALUE self, VALUE flag)
15768{
15769 struct parser_params *p;
15770
15771 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
15772 rb_ruby_parser_set_yydebug(p, RTEST(flag));
15773 return flag;
15774}
15775#endif /* !UNIVERSAL_PARSER */
15776
15777VALUE
15778rb_ruby_parser_encoding(rb_parser_t *p)
15779{
15780 return rb_enc_from_encoding(p->enc);
15781}
15782
15783int
15784rb_ruby_parser_end_seen_p(rb_parser_t *p)
15785{
15786 return p->ruby__end__seen;
15787}
15788
15789int
15790rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15791{
15792 p->debug = flag;
15793 return flag;
15794}
15795#endif /* !RIPPER */
15796
15797#ifdef RIPPER
15798int
15799rb_ruby_parser_get_yydebug(rb_parser_t *p)
15800{
15801 return p->debug;
15802}
15803
15804void
15805rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15806{
15807 p->value = value;
15808}
15809
15810int
15811rb_ruby_parser_error_p(rb_parser_t *p)
15812{
15813 return p->error_p;
15814}
15815
15816VALUE
15817rb_ruby_parser_debug_output(rb_parser_t *p)
15818{
15819 return p->debug_output;
15820}
15821
15822void
15823rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15824{
15825 p->debug_output = output;
15826}
15827
15828VALUE
15829rb_ruby_parser_parsing_thread(rb_parser_t *p)
15830{
15831 return p->parsing_thread;
15832}
15833
15834void
15835rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15836{
15837 p->parsing_thread = parsing_thread;
15838}
15839
15840void
15841rb_ruby_parser_ripper_initialize(rb_parser_t *p, VALUE (*gets)(struct parser_params*,VALUE), VALUE input, VALUE sourcefile_string, const char *sourcefile, int sourceline)
15842{
15843 p->lex.gets = gets;
15844 p->lex.input = input;
15845 p->eofp = 0;
15846 p->ruby_sourcefile_string = sourcefile_string;
15847 p->ruby_sourcefile = sourcefile;
15848 p->ruby_sourceline = sourceline;
15849}
15850
15851VALUE
15852rb_ruby_parser_result(rb_parser_t *p)
15853{
15854 return p->result;
15855}
15856
15857rb_encoding *
15858rb_ruby_parser_enc(rb_parser_t *p)
15859{
15860 return p->enc;
15861}
15862
15863VALUE
15864rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15865{
15866 return p->ruby_sourcefile_string;
15867}
15868
15869int
15870rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15871{
15872 return p->ruby_sourceline;
15873}
15874
15875int
15876rb_ruby_parser_lex_state(rb_parser_t *p)
15877{
15878 return p->lex.state;
15879}
15880
15881void
15882rb_ruby_ripper_parse0(rb_parser_t *p)
15883{
15884 parser_prepare(p);
15885 p->ast = rb_ast_new();
15886 ripper_yyparse((void*)p);
15887 rb_ast_dispose(p->ast);
15888 p->ast = 0;
15889}
15890
15891int
15892rb_ruby_ripper_dedent_string(rb_parser_t *p, VALUE string, int width)
15893{
15894 return dedent_string(p, string, width);
15895}
15896
15897VALUE
15898rb_ruby_ripper_lex_get_str(rb_parser_t *p, VALUE s)
15899{
15900 return lex_get_str(p, s);
15901}
15902
15903int
15904rb_ruby_ripper_initialized_p(rb_parser_t *p)
15905{
15906 return p->lex.input != 0;
15907}
15908
15909void
15910rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15911{
15912 parser_initialize(p);
15913}
15914
15915long
15916rb_ruby_ripper_column(rb_parser_t *p)
15917{
15918 return p->lex.ptok - p->lex.pbeg;
15919}
15920
15921long
15922rb_ruby_ripper_token_len(rb_parser_t *p)
15923{
15924 return p->lex.pcur - p->lex.ptok;
15925}
15926
15927VALUE
15928rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15929{
15930 return p->lex.lastline;
15931}
15932
15933VALUE
15934rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15935{
15936 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15937}
15938
15939struct parser_params*
15940rb_ruby_ripper_parser_allocate(void)
15941{
15942 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15943}
15944#endif /* RIPPER */
15945
15946#ifndef RIPPER
15947#ifdef YYMALLOC
15948#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
15949/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
15950 * potential memory leak */
15951#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
15952#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
15953 (new)->cnt = (cnt), (ptr))
15954
15955void *
15956rb_parser_malloc(struct parser_params *p, size_t size)
15957{
15958 size_t cnt = HEAPCNT(1, size);
15959 rb_imemo_tmpbuf_t *n = NEWHEAP();
15960 void *ptr = xmalloc(size);
15961
15962 return ADD2HEAP(n, cnt, ptr);
15963}
15964
15965void *
15966rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
15967{
15968 size_t cnt = HEAPCNT(nelem, size);
15969 rb_imemo_tmpbuf_t *n = NEWHEAP();
15970 void *ptr = xcalloc(nelem, size);
15971
15972 return ADD2HEAP(n, cnt, ptr);
15973}
15974
15975void *
15976rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
15977{
15978 rb_imemo_tmpbuf_t *n;
15979 size_t cnt = HEAPCNT(1, size);
15980
15981 if (ptr && (n = p->heap) != NULL) {
15982 do {
15983 if (n->ptr == ptr) {
15984 n->ptr = ptr = xrealloc(ptr, size);
15985 if (n->cnt) n->cnt = cnt;
15986 return ptr;
15987 }
15988 } while ((n = n->next) != NULL);
15989 }
15990 n = NEWHEAP();
15991 ptr = xrealloc(ptr, size);
15992 return ADD2HEAP(n, cnt, ptr);
15993}
15994
15995void
15996rb_parser_free(struct parser_params *p, void *ptr)
15997{
15998 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
15999
16000 while ((n = *prev) != NULL) {
16001 if (n->ptr == ptr) {
16002 *prev = n->next;
16003 break;
16004 }
16005 prev = &n->next;
16006 }
16007}
16008#endif
16009
16010void
16011rb_parser_printf(struct parser_params *p, const char *fmt, ...)
16012{
16013 va_list ap;
16014 VALUE mesg = p->debug_buffer;
16015
16016 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
16017 va_start(ap, fmt);
16018 rb_str_vcatf(mesg, fmt, ap);
16019 va_end(ap);
16020 if (end_with_newline_p(p, mesg)) {
16021 rb_io_write(p->debug_output, mesg);
16022 p->debug_buffer = Qnil;
16023 }
16024}
16025
16026static void
16027parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
16028{
16029 va_list ap;
16030 int lineno, column;
16031
16032 if (loc) {
16033 lineno = loc->end_pos.lineno;
16034 column = loc->end_pos.column;
16035 }
16036 else {
16037 lineno = p->ruby_sourceline;
16038 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
16039 }
16040
16041 rb_io_flush(p->debug_output);
16042 p->error_p = 1;
16043 va_start(ap, fmt);
16044 p->error_buffer =
16045 rb_syntax_error_append(p->error_buffer,
16046 p->ruby_sourcefile_string,
16047 lineno, column,
16048 p->enc, fmt, ap);
16049 va_end(ap);
16050}
16051
16052static size_t
16053count_char(const char *str, int c)
16054{
16055 int n = 0;
16056 while (str[n] == c) ++n;
16057 return n;
16058}
16059
16060/*
16061 * strip enclosing double-quotes, same as the default yytnamerr except
16062 * for that single-quotes matching back-quotes do not stop stripping.
16063 *
16064 * "\"`class' keyword\"" => "`class' keyword"
16065 */
16066RUBY_FUNC_EXPORTED size_t
16067rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16068{
16069 if (*yystr == '"') {
16070 size_t yyn = 0, bquote = 0;
16071 const char *yyp = yystr;
16072
16073 while (*++yyp) {
16074 switch (*yyp) {
16075 case '`':
16076 if (!bquote) {
16077 bquote = count_char(yyp+1, '`') + 1;
16078 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16079 yyn += bquote;
16080 yyp += bquote - 1;
16081 break;
16082 }
16083 goto default_char;
16084
16085 case '\'':
16086 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16087 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16088 yyn += bquote;
16089 yyp += bquote - 1;
16090 bquote = 0;
16091 break;
16092 }
16093 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16094 if (yyres) memcpy(yyres + yyn, yyp, 3);
16095 yyn += 3;
16096 yyp += 2;
16097 break;
16098 }
16099 goto do_not_strip_quotes;
16100
16101 case ',':
16102 goto do_not_strip_quotes;
16103
16104 case '\\':
16105 if (*++yyp != '\\')
16106 goto do_not_strip_quotes;
16107 /* Fall through. */
16108 default_char:
16109 default:
16110 if (yyres)
16111 yyres[yyn] = *yyp;
16112 yyn++;
16113 break;
16114
16115 case '"':
16116 case '\0':
16117 if (yyres)
16118 yyres[yyn] = '\0';
16119 return yyn;
16120 }
16121 }
16122 do_not_strip_quotes: ;
16123 }
16124
16125 if (!yyres) return strlen(yystr);
16126
16127 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16128}
16129#endif
16130
16131#ifdef RIPPER
16132#ifdef RIPPER_DEBUG
16133/* :nodoc: */
16134static VALUE
16135ripper_validate_object(VALUE self, VALUE x)
16136{
16137 if (x == Qfalse) return x;
16138 if (x == Qtrue) return x;
16139 if (NIL_P(x)) return x;
16140 if (UNDEF_P(x))
16141 rb_raise(rb_eArgError, "Qundef given");
16142 if (FIXNUM_P(x)) return x;
16143 if (SYMBOL_P(x)) return x;
16144 switch (BUILTIN_TYPE(x)) {
16145 case T_STRING:
16146 case T_OBJECT:
16147 case T_ARRAY:
16148 case T_BIGNUM:
16149 case T_FLOAT:
16150 case T_COMPLEX:
16151 case T_RATIONAL:
16152 break;
16153 case T_NODE:
16154 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
16155 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
16156 }
16157 x = ((NODE *)x)->nd_rval;
16158 break;
16159 default:
16160 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
16161 (void *)x, rb_obj_classname(x));
16162 }
16163 if (!RBASIC_CLASS(x)) {
16164 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
16165 (void *)x, rb_builtin_type_name(TYPE(x)));
16166 }
16167 return x;
16168}
16169#endif
16170
16171#define validate(x) ((x) = get_value(x))
16172
16173static VALUE
16174ripper_dispatch0(struct parser_params *p, ID mid)
16175{
16176 return rb_funcall(p->value, mid, 0);
16177}
16178
16179static VALUE
16180ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16181{
16182 validate(a);
16183 return rb_funcall(p->value, mid, 1, a);
16184}
16185
16186static VALUE
16187ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16188{
16189 validate(a);
16190 validate(b);
16191 return rb_funcall(p->value, mid, 2, a, b);
16192}
16193
16194static VALUE
16195ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16196{
16197 validate(a);
16198 validate(b);
16199 validate(c);
16200 return rb_funcall(p->value, mid, 3, a, b, c);
16201}
16202
16203static VALUE
16204ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16205{
16206 validate(a);
16207 validate(b);
16208 validate(c);
16209 validate(d);
16210 return rb_funcall(p->value, mid, 4, a, b, c, d);
16211}
16212
16213static VALUE
16214ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16215{
16216 validate(a);
16217 validate(b);
16218 validate(c);
16219 validate(d);
16220 validate(e);
16221 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16222}
16223
16224static VALUE
16225ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16226{
16227 validate(a);
16228 validate(b);
16229 validate(c);
16230 validate(d);
16231 validate(e);
16232 validate(f);
16233 validate(g);
16234 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16235}
16236
16237void
16238ripper_error(struct parser_params *p)
16239{
16240 p->error_p = TRUE;
16241}
16242
16243VALUE
16244ripper_value(struct parser_params *p)
16245{
16246 (void)yystpcpy; /* may not used in newer bison */
16247
16248 return p->value;
16249}
16250
16251#endif /* RIPPER */
16252/*
16253 * Local variables:
16254 * mode: c
16255 * c-file-style: "ruby"
16256 * End:
16257 */