Ruby 3.5.0dev (2025-07-16 revision 37d088ad24d700db4b59f2c44dee2d202faf122b)
parse.y (37d088ad24d700db4b59f2c44dee2d202faf122b)
1/**********************************************************************
2
3 parse.y -
4
5 $Author$
6 created at: Fri May 28 18:02:42 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12%{
13
14#if !YYPURE
15# error needs pure parser
16#endif
17#define YYDEBUG 1
18#define YYERROR_VERBOSE 1
19#define YYSTACK_USE_ALLOCA 0
20
21/* For Ripper */
22#ifdef RUBY_EXTCONF_H
23# include RUBY_EXTCONF_H
24#endif
25
26#include "ruby/internal/config.h"
27
28#include <errno.h>
29
30#ifdef UNIVERSAL_PARSER
31
32#include "internal/ruby_parser.h"
33#include "parser_node.h"
34#include "universal_parser.c"
35
36#ifdef RIPPER
37#define STATIC_ID2SYM p->config->static_id2sym
38#define rb_str_coderange_scan_restartable p->config->str_coderange_scan_restartable
39#endif
40
41#else
42
43#include "internal.h"
44#include "internal/compile.h"
45#include "internal/compilers.h"
46#include "internal/complex.h"
47#include "internal/encoding.h"
48#include "internal/error.h"
49#include "internal/hash.h"
50#include "internal/io.h"
51#include "internal/numeric.h"
52#include "internal/parse.h"
53#include "internal/rational.h"
54#include "internal/re.h"
55#include "internal/ruby_parser.h"
56#include "internal/symbol.h"
57#include "internal/thread.h"
58#include "internal/variable.h"
59#include "node.h"
60#include "parser_node.h"
61#include "probes.h"
62#include "regenc.h"
63#include "ruby/encoding.h"
64#include "ruby/regex.h"
65#include "ruby/ruby.h"
66#include "ruby/st.h"
67#include "ruby/util.h"
68#include "ruby/ractor.h"
69#include "symbol.h"
70
71#ifndef RIPPER
72static VALUE
73syntax_error_new(void)
74{
75 return rb_class_new_instance(0, 0, rb_eSyntaxError);
76}
77#endif
78
79static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable);
80
81#define compile_callback rb_suppress_tracing
82#endif /* !UNIVERSAL_PARSER */
83
84#define NODE_SPECIAL_EMPTY_ARGS ((NODE *)-1)
85#define NODE_EMPTY_ARGS_P(node) ((node) == NODE_SPECIAL_EMPTY_ARGS)
86
87static int rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2);
88
89#ifndef RIPPER
90static rb_parser_string_t *rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *original);
91#endif
92
93static int
94node_integer_cmp(rb_node_integer_t *n1, rb_node_integer_t *n2)
95{
96 return (n1->minus != n2->minus ||
97 n1->base != n2->base ||
98 strcmp(n1->val, n2->val));
99}
100
101static int
102node_float_cmp(rb_node_float_t *n1, rb_node_float_t *n2)
103{
104 return (n1->minus != n2->minus ||
105 strcmp(n1->val, n2->val));
106}
107
108static int
109node_rational_cmp(rb_node_rational_t *n1, rb_node_rational_t *n2)
110{
111 return (n1->minus != n2->minus ||
112 n1->base != n2->base ||
113 n1->seen_point != n2->seen_point ||
114 strcmp(n1->val, n2->val));
115}
116
117static int
118node_imaginary_cmp(rb_node_imaginary_t *n1, rb_node_imaginary_t *n2)
119{
120 return (n1->minus != n2->minus ||
121 n1->base != n2->base ||
122 n1->seen_point != n2->seen_point ||
123 n1->type != n2->type ||
124 strcmp(n1->val, n2->val));
125}
126
127static int
128rb_parser_regx_hash_cmp(rb_node_regx_t *n1, rb_node_regx_t *n2)
129{
130 return (n1->options != n2->options ||
131 rb_parser_string_hash_cmp(n1->string, n2->string));
132}
133
134static st_index_t rb_parser_str_hash(rb_parser_string_t *str);
135static st_index_t rb_char_p_hash(const char *c);
136
137static int
138literal_cmp(st_data_t val, st_data_t lit)
139{
140 if (val == lit) return 0;
141
142 NODE *node_val = RNODE(val);
143 NODE *node_lit = RNODE(lit);
144 enum node_type type_val = nd_type(node_val);
145 enum node_type type_lit = nd_type(node_lit);
146
147 if (type_val != type_lit) {
148 return -1;
149 }
150
151 switch (type_lit) {
152 case NODE_INTEGER:
153 return node_integer_cmp(RNODE_INTEGER(node_val), RNODE_INTEGER(node_lit));
154 case NODE_FLOAT:
155 return node_float_cmp(RNODE_FLOAT(node_val), RNODE_FLOAT(node_lit));
156 case NODE_RATIONAL:
157 return node_rational_cmp(RNODE_RATIONAL(node_val), RNODE_RATIONAL(node_lit));
158 case NODE_IMAGINARY:
159 return node_imaginary_cmp(RNODE_IMAGINARY(node_val), RNODE_IMAGINARY(node_lit));
160 case NODE_STR:
161 return rb_parser_string_hash_cmp(RNODE_STR(node_val)->string, RNODE_STR(node_lit)->string);
162 case NODE_SYM:
163 return rb_parser_string_hash_cmp(RNODE_SYM(node_val)->string, RNODE_SYM(node_lit)->string);
164 case NODE_REGX:
165 return rb_parser_regx_hash_cmp(RNODE_REGX(node_val), RNODE_REGX(node_lit));
166 case NODE_LINE:
167 return node_val->nd_loc.beg_pos.lineno != node_lit->nd_loc.beg_pos.lineno;
168 case NODE_FILE:
169 return rb_parser_string_hash_cmp(RNODE_FILE(node_val)->path, RNODE_FILE(node_lit)->path);
170 case NODE_ENCODING:
171 return RNODE_ENCODING(node_val)->enc != RNODE_ENCODING(node_lit)->enc;
172 default:
173#ifdef UNIVERSAL_PARSER
174 abort();
175#else
176 rb_bug("unexpected node: %s, %s", ruby_node_name(type_val), ruby_node_name(type_lit));
177#endif
178 }
179}
180
181static st_index_t
182literal_hash(st_data_t a)
183{
184 NODE *node = (NODE *)a;
185 enum node_type type = nd_type(node);
186
187 switch (type) {
188 case NODE_INTEGER:
189 return rb_char_p_hash(RNODE_INTEGER(node)->val);
190 case NODE_FLOAT:
191 return rb_char_p_hash(RNODE_FLOAT(node)->val);
192 case NODE_RATIONAL:
193 return rb_char_p_hash(RNODE_RATIONAL(node)->val);
194 case NODE_IMAGINARY:
195 return rb_char_p_hash(RNODE_IMAGINARY(node)->val);
196 case NODE_STR:
197 return rb_parser_str_hash(RNODE_STR(node)->string);
198 case NODE_SYM:
199 return rb_parser_str_hash(RNODE_SYM(node)->string);
200 case NODE_REGX:
201 return rb_parser_str_hash(RNODE_REGX(node)->string);
202 case NODE_LINE:
203 return (st_index_t)node->nd_loc.beg_pos.lineno;
204 case NODE_FILE:
205 return rb_parser_str_hash(RNODE_FILE(node)->path);
206 case NODE_ENCODING:
207 return (st_index_t)RNODE_ENCODING(node)->enc;
208 default:
209#ifdef UNIVERSAL_PARSER
210 abort();
211#else
212 rb_bug("unexpected node: %s", ruby_node_name(type));
213#endif
214 }
215}
216
217static inline int
218parse_isascii(int c)
219{
220 return '\0' <= c && c <= '\x7f';
221}
222
223#undef ISASCII
224#define ISASCII parse_isascii
225
226static inline int
227parse_isspace(int c)
228{
229 return c == ' ' || ('\t' <= c && c <= '\r');
230}
231
232#undef ISSPACE
233#define ISSPACE parse_isspace
234
235static inline int
236parse_iscntrl(int c)
237{
238 return ('\0' <= c && c < ' ') || c == '\x7f';
239}
240
241#undef ISCNTRL
242#define ISCNTRL(c) parse_iscntrl(c)
243
244static inline int
245parse_isupper(int c)
246{
247 return 'A' <= c && c <= 'Z';
248}
249
250static inline int
251parse_islower(int c)
252{
253 return 'a' <= c && c <= 'z';
254}
255
256static inline int
257parse_isalpha(int c)
258{
259 return parse_isupper(c) || parse_islower(c);
260}
261
262#undef ISALPHA
263#define ISALPHA(c) parse_isalpha(c)
264
265static inline int
266parse_isdigit(int c)
267{
268 return '0' <= c && c <= '9';
269}
270
271#undef ISDIGIT
272#define ISDIGIT(c) parse_isdigit(c)
273
274static inline int
275parse_isalnum(int c)
276{
277 return ISALPHA(c) || ISDIGIT(c);
278}
279
280#undef ISALNUM
281#define ISALNUM(c) parse_isalnum(c)
282
283static inline int
284parse_isxdigit(int c)
285{
286 return ISDIGIT(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
287}
288
289#undef ISXDIGIT
290#define ISXDIGIT(c) parse_isxdigit(c)
291
292#include "parser_st.h"
293
294#undef STRCASECMP
295#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
296
297#undef STRNCASECMP
298#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
299
300#ifdef RIPPER
301#include "ripper_init.h"
302#endif
303
304enum rescue_context {
305 before_rescue,
306 after_rescue,
307 after_else,
308 after_ensure,
309};
310
311struct lex_context {
312 unsigned int in_defined: 1;
313 unsigned int in_kwarg: 1;
314 unsigned int in_argdef: 1;
315 unsigned int in_def: 1;
316 unsigned int in_class: 1;
317 BITFIELD(enum rb_parser_shareability, shareable_constant_value, 2);
318 BITFIELD(enum rescue_context, in_rescue, 2);
319 unsigned int cant_return: 1;
320};
321
322typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
323
324#if defined(__GNUC__) && !defined(__clang__)
325// Suppress "parameter passing for argument of type 'struct
326// lex_context' changed" notes. `struct lex_context` is file scope,
327// and has no ABI compatibility issue.
328RBIMPL_WARNING_PUSH()
329RBIMPL_WARNING_IGNORED(-Wpsabi)
330RBIMPL_WARNING_POP()
331// Not sure why effective even after popped.
332#endif
333
334#include "parse.h"
335
336#define NO_LEX_CTXT (struct lex_context){0}
337
338#ifndef WARN_PAST_SCOPE
339# define WARN_PAST_SCOPE 0
340#endif
341
342#define TAB_WIDTH 8
343
344#define yydebug (p->debug) /* disable the global variable definition */
345
346#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__)
347#define YY_LOCATION_PRINT(File, loc, p) \
348 rb_parser_printf(p, "%d.%d-%d.%d", \
349 (loc).beg_pos.lineno, (loc).beg_pos.column,\
350 (loc).end_pos.lineno, (loc).end_pos.column)
351#define YYLLOC_DEFAULT(Current, Rhs, N) \
352 do \
353 if (N) \
354 { \
355 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
356 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
357 } \
358 else \
359 { \
360 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
361 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
362 } \
363 while (0)
364#define YY_(Msgid) \
365 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
366 "nesting too deep" : (Msgid))
367
368#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
369 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
370#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
371 rb_parser_set_location_of_delayed_token(p, &(Current))
372#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
373 rb_parser_set_location_of_heredoc_end(p, &(Current))
374#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
375 rb_parser_set_location_of_dummy_end(p, &(Current))
376#define RUBY_SET_YYLLOC_OF_NONE(Current) \
377 rb_parser_set_location_of_none(p, &(Current))
378#define RUBY_SET_YYLLOC(Current) \
379 rb_parser_set_location(p, &(Current))
380#define RUBY_INIT_YYLLOC() \
381 { \
382 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
383 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
384 }
385
386#define IS_lex_state_for(x, ls) ((x) & (ls))
387#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
388#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
389#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
390
391# define SET_LEX_STATE(ls) \
392 parser_set_lex_state(p, ls, __LINE__)
393static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
394
395typedef VALUE stack_type;
396
397static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
398
399# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
400# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
401# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
402# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
403# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
404
405/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
406 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
407#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
408#define COND_POP() BITSTACK_POP(cond_stack)
409#define COND_P() BITSTACK_SET_P(cond_stack)
410#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
411
412/* A flag to identify keyword_do_block; "do" keyword after command_call.
413 Example: `foo 1, 2 do`. */
414#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
415#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
416#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
417#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
418
419struct vtable {
420 ID *tbl;
421 int pos;
422 int capa;
423 struct vtable *prev;
424};
425
426struct local_vars {
427 struct vtable *args;
428 struct vtable *vars;
429 struct vtable *used;
430# if WARN_PAST_SCOPE
431 struct vtable *past;
432# endif
433 struct local_vars *prev;
434 struct {
435 NODE *outer, *inner, *current;
436 } numparam;
437 NODE *it;
438};
439
440typedef struct rb_locations_lambda_body_t {
441 NODE *node;
442 YYLTYPE opening_loc;
443 YYLTYPE closing_loc;
444} rb_locations_lambda_body_t;
445
446enum {
447 ORDINAL_PARAM = -1,
448 NO_PARAM = 0,
449 NUMPARAM_MAX = 9,
450};
451
452#define DVARS_INHERIT ((void*)1)
453#define DVARS_TOPSCOPE NULL
454#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
455
456typedef struct token_info {
457 const char *token;
458 rb_code_position_t beg;
459 int indent;
460 int nonspc;
461 struct token_info *next;
462} token_info;
463
464typedef struct end_expect_token_locations {
465 const rb_code_position_t *pos;
466 struct end_expect_token_locations *prev;
467} end_expect_token_locations_t;
468
469typedef struct parser_string_buffer_elem {
470 struct parser_string_buffer_elem *next;
471 long len; /* Total length of allocated buf */
472 long used; /* Current usage of buf */
473 rb_parser_string_t *buf[FLEX_ARY_LEN];
474} parser_string_buffer_elem_t;
475
476typedef struct parser_string_buffer {
477 parser_string_buffer_elem_t *head;
478 parser_string_buffer_elem_t *last;
479} parser_string_buffer_t;
480
481#define AFTER_HEREDOC_WITHOUT_TERMINATOR ((rb_parser_string_t *)1)
482
483/*
484 Structure of Lexer Buffer:
485
486 lex.pbeg lex.ptok lex.pcur lex.pend
487 | | | |
488 |------------+------------+------------|
489 |<---------->|
490 token
491*/
492struct parser_params {
493 YYSTYPE *lval;
494 YYLTYPE *yylloc;
495
496 struct {
497 rb_strterm_t *strterm;
498 rb_parser_lex_gets_func *gets;
499 rb_parser_input_data input;
500 parser_string_buffer_t string_buffer;
501 rb_parser_string_t *lastline;
502 rb_parser_string_t *nextline;
503 const char *pbeg;
504 const char *pcur;
505 const char *pend;
506 const char *ptok;
507 enum lex_state_e state;
508 /* track the nest level of any parens "()[]{}" */
509 int paren_nest;
510 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
511 int lpar_beg;
512 /* track the nest level of only braces "{}" */
513 int brace_nest;
514 } lex;
515 stack_type cond_stack;
516 stack_type cmdarg_stack;
517 int tokidx;
518 int toksiz;
519 int heredoc_end;
520 int heredoc_indent;
521 int heredoc_line_indent;
522 char *tokenbuf;
523 struct local_vars *lvtbl;
524 st_table *pvtbl;
525 st_table *pktbl;
526 int line_count;
527 int ruby_sourceline; /* current line no. */
528 const char *ruby_sourcefile; /* current source file */
529 VALUE ruby_sourcefile_string;
530 rb_encoding *enc;
531 token_info *token_info;
532 st_table *case_labels;
533 rb_node_exits_t *exits;
534
535 VALUE debug_buffer;
536 VALUE debug_output;
537
538 struct {
539 rb_parser_string_t *token;
540 int beg_line;
541 int beg_col;
542 int end_line;
543 int end_col;
544 } delayed;
545
546 rb_ast_t *ast;
547 int node_id;
548
549 st_table *warn_duplicate_keys_table;
550
551 int max_numparam;
552 ID it_id;
553
554 struct lex_context ctxt;
555
556 NODE *eval_tree_begin;
557 NODE *eval_tree;
558 const struct rb_iseq_struct *parent_iseq;
559
560#ifdef UNIVERSAL_PARSER
561 const rb_parser_config_t *config;
562#endif
563 /* compile_option */
564 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
565
566 unsigned int command_start:1;
567 unsigned int eofp: 1;
568 unsigned int ruby__end__seen: 1;
569 unsigned int debug: 1;
570 unsigned int has_shebang: 1;
571 unsigned int token_seen: 1;
572 unsigned int token_info_enabled: 1;
573# if WARN_PAST_SCOPE
574 unsigned int past_scope_enabled: 1;
575# endif
576 unsigned int error_p: 1;
577 unsigned int cr_seen: 1;
578
579#ifndef RIPPER
580 /* Ruby core only */
581
582 unsigned int do_print: 1;
583 unsigned int do_loop: 1;
584 unsigned int do_chomp: 1;
585 unsigned int do_split: 1;
586 unsigned int error_tolerant: 1;
587 unsigned int keep_tokens: 1;
588
589 VALUE error_buffer;
590 rb_parser_ary_t *debug_lines;
591 /*
592 * Store specific keyword locations to generate dummy end token.
593 * Refer to the tail of list element.
594 */
595 end_expect_token_locations_t *end_expect_token_locations;
596 /* id for terms */
597 int token_id;
598 /* Array for term tokens */
599 rb_parser_ary_t *tokens;
600#else
601 /* Ripper only */
602
603 VALUE value;
604 VALUE result;
605 VALUE parsing_thread;
606 VALUE s_value; /* Token VALUE */
607 VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
608 VALUE s_value_stack;
609#endif
610};
611
612#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
613#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
614#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
615static int
616numparam_id_p(struct parser_params *p, ID id)
617{
618 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
619 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
620 return idx > 0 && idx <= NUMPARAM_MAX;
621}
622static void numparam_name(struct parser_params *p, ID id);
623
624#ifdef RIPPER
625static void
626after_shift(struct parser_params *p)
627{
628 if (p->debug) {
629 rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
630 }
631 rb_ary_push(p->s_value_stack, p->s_value);
632 p->s_value = Qnil;
633}
634
635static void
636before_reduce(int len, struct parser_params *p)
637{
638 // Initialize $$ with $1.
639 if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
640}
641
642static void
643after_reduce(int len, struct parser_params *p)
644{
645 for (int i = 0; i < len; i++) {
646 VALUE tos = rb_ary_pop(p->s_value_stack);
647 if (p->debug) {
648 rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos);
649 }
650 }
651 if (p->debug) {
652 rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
653 }
654 rb_ary_push(p->s_value_stack, p->s_lvalue);
655 p->s_lvalue = Qnil;
656}
657
658static void
659after_shift_error_token(struct parser_params *p)
660{
661 if (p->debug) {
662 rb_parser_printf(p, "after-shift-error-token:\n");
663 }
664 rb_ary_push(p->s_value_stack, Qnil);
665}
666
667static void
668after_pop_stack(int len, struct parser_params *p)
669{
670 for (int i = 0; i < len; i++) {
671 VALUE tos = rb_ary_pop(p->s_value_stack);
672 if (p->debug) {
673 rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
674 }
675 }
676}
677#else
678static void
679after_shift(struct parser_params *p)
680{
681}
682
683static void
684before_reduce(int len, struct parser_params *p)
685{
686}
687
688static void
689after_reduce(int len, struct parser_params *p)
690{
691}
692
693static void
694after_shift_error_token(struct parser_params *p)
695{
696}
697
698static void
699after_pop_stack(int len, struct parser_params *p)
700{
701}
702#endif
703
704#define intern_cstr(n,l,en) rb_intern3(n,l,en)
705
706#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
707
708#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
709#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
710#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
711#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
712#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
713#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
714
715#ifndef RIPPER
716static inline int
717char_at_end(struct parser_params *p, VALUE str, int when_empty)
718{
719 long len = RSTRING_LEN(str);
720 return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
721}
722#endif
723
724static void
725pop_pvtbl(struct parser_params *p, st_table *tbl)
726{
727 st_free_table(p->pvtbl);
728 p->pvtbl = tbl;
729}
730
731static void
732pop_pktbl(struct parser_params *p, st_table *tbl)
733{
734 if (p->pktbl) st_free_table(p->pktbl);
735 p->pktbl = tbl;
736}
737
738#define STRING_BUF_DEFAULT_LEN 16
739
740static void
741string_buffer_init(struct parser_params *p)
742{
743 parser_string_buffer_t *buf = &p->lex.string_buffer;
744 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN;
745
746 buf->head = buf->last = xmalloc(size);
747 buf->head->len = STRING_BUF_DEFAULT_LEN;
748 buf->head->used = 0;
749 buf->head->next = NULL;
750}
751
752static void
753string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
754{
755 parser_string_buffer_t *buf = &p->lex.string_buffer;
756
757 if (buf->head->used >= buf->head->len) {
758 parser_string_buffer_elem_t *elem;
759 long n = buf->head->len * 2;
760 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n;
761
762 elem = xmalloc(size);
763 elem->len = n;
764 elem->used = 0;
765 elem->next = NULL;
766 buf->last->next = elem;
767 buf->last = elem;
768 }
769 buf->last->buf[buf->last->used++] = str;
770}
771
772static void
773string_buffer_free(struct parser_params *p)
774{
775 parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
776
777 while (elem) {
778 parser_string_buffer_elem_t *next_elem = elem->next;
779
780 for (long i = 0; i < elem->used; i++) {
781 rb_parser_string_free(p, elem->buf[i]);
782 }
783
784 xfree(elem);
785 elem = next_elem;
786 }
787}
788
789#ifndef RIPPER
790static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
791
792static void
793debug_end_expect_token_locations(struct parser_params *p, const char *name)
794{
795 if(p->debug) {
796 VALUE mesg = rb_sprintf("%s: [", name);
797 int i = 0;
798 for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
799 if (i > 0)
800 rb_str_cat_cstr(mesg, ", ");
801 rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
802 i++;
803 }
804 rb_str_cat_cstr(mesg, "]\n");
805 flush_debug_buffer(p, p->debug_output, mesg);
806 }
807}
808
809static void
810push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
811{
812 if(!p->error_tolerant) return;
813
814 end_expect_token_locations_t *locations;
815 locations = ALLOC(end_expect_token_locations_t);
816 locations->pos = pos;
817 locations->prev = p->end_expect_token_locations;
818 p->end_expect_token_locations = locations;
819
820 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
821}
822
823static void
824pop_end_expect_token_locations(struct parser_params *p)
825{
826 if(!p->end_expect_token_locations) return;
827
828 end_expect_token_locations_t *locations = p->end_expect_token_locations->prev;
829 ruby_sized_xfree(p->end_expect_token_locations, sizeof(end_expect_token_locations_t));
830 p->end_expect_token_locations = locations;
831
832 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
833}
834
835static end_expect_token_locations_t *
836peek_end_expect_token_locations(struct parser_params *p)
837{
838 return p->end_expect_token_locations;
839}
840
841static const char *
842parser_token2char(struct parser_params *p, enum yytokentype tok)
843{
844 switch ((int) tok) {
845#define TOKEN2CHAR(tok) case tok: return (#tok);
846#define TOKEN2CHAR2(tok, name) case tok: return (name);
847 TOKEN2CHAR2(' ', "word_sep");
848 TOKEN2CHAR2('!', "!")
849 TOKEN2CHAR2('%', "%");
850 TOKEN2CHAR2('&', "&");
851 TOKEN2CHAR2('*', "*");
852 TOKEN2CHAR2('+', "+");
853 TOKEN2CHAR2('-', "-");
854 TOKEN2CHAR2('/', "/");
855 TOKEN2CHAR2('<', "<");
856 TOKEN2CHAR2('=', "=");
857 TOKEN2CHAR2('>', ">");
858 TOKEN2CHAR2('?', "?");
859 TOKEN2CHAR2('^', "^");
860 TOKEN2CHAR2('|', "|");
861 TOKEN2CHAR2('~', "~");
862 TOKEN2CHAR2(':', ":");
863 TOKEN2CHAR2(',', ",");
864 TOKEN2CHAR2('.', ".");
865 TOKEN2CHAR2(';', ";");
866 TOKEN2CHAR2('`', "`");
867 TOKEN2CHAR2('\n', "nl");
868 TOKEN2CHAR2('{', "\"{\"");
869 TOKEN2CHAR2('}', "\"}\"");
870 TOKEN2CHAR2('[', "\"[\"");
871 TOKEN2CHAR2(']', "\"]\"");
872 TOKEN2CHAR2('(', "\"(\"");
873 TOKEN2CHAR2(')', "\")\"");
874 TOKEN2CHAR2('\\', "backslash");
875 TOKEN2CHAR(keyword_class);
876 TOKEN2CHAR(keyword_module);
877 TOKEN2CHAR(keyword_def);
878 TOKEN2CHAR(keyword_undef);
879 TOKEN2CHAR(keyword_begin);
880 TOKEN2CHAR(keyword_rescue);
881 TOKEN2CHAR(keyword_ensure);
882 TOKEN2CHAR(keyword_end);
883 TOKEN2CHAR(keyword_if);
884 TOKEN2CHAR(keyword_unless);
885 TOKEN2CHAR(keyword_then);
886 TOKEN2CHAR(keyword_elsif);
887 TOKEN2CHAR(keyword_else);
888 TOKEN2CHAR(keyword_case);
889 TOKEN2CHAR(keyword_when);
890 TOKEN2CHAR(keyword_while);
891 TOKEN2CHAR(keyword_until);
892 TOKEN2CHAR(keyword_for);
893 TOKEN2CHAR(keyword_break);
894 TOKEN2CHAR(keyword_next);
895 TOKEN2CHAR(keyword_redo);
896 TOKEN2CHAR(keyword_retry);
897 TOKEN2CHAR(keyword_in);
898 TOKEN2CHAR(keyword_do);
899 TOKEN2CHAR(keyword_do_cond);
900 TOKEN2CHAR(keyword_do_block);
901 TOKEN2CHAR(keyword_do_LAMBDA);
902 TOKEN2CHAR(keyword_return);
903 TOKEN2CHAR(keyword_yield);
904 TOKEN2CHAR(keyword_super);
905 TOKEN2CHAR(keyword_self);
906 TOKEN2CHAR(keyword_nil);
907 TOKEN2CHAR(keyword_true);
908 TOKEN2CHAR(keyword_false);
909 TOKEN2CHAR(keyword_and);
910 TOKEN2CHAR(keyword_or);
911 TOKEN2CHAR(keyword_not);
912 TOKEN2CHAR(modifier_if);
913 TOKEN2CHAR(modifier_unless);
914 TOKEN2CHAR(modifier_while);
915 TOKEN2CHAR(modifier_until);
916 TOKEN2CHAR(modifier_rescue);
917 TOKEN2CHAR(keyword_alias);
918 TOKEN2CHAR(keyword_defined);
919 TOKEN2CHAR(keyword_BEGIN);
920 TOKEN2CHAR(keyword_END);
921 TOKEN2CHAR(keyword__LINE__);
922 TOKEN2CHAR(keyword__FILE__);
923 TOKEN2CHAR(keyword__ENCODING__);
924 TOKEN2CHAR(tIDENTIFIER);
925 TOKEN2CHAR(tFID);
926 TOKEN2CHAR(tGVAR);
927 TOKEN2CHAR(tIVAR);
928 TOKEN2CHAR(tCONSTANT);
929 TOKEN2CHAR(tCVAR);
930 TOKEN2CHAR(tLABEL);
931 TOKEN2CHAR(tINTEGER);
932 TOKEN2CHAR(tFLOAT);
933 TOKEN2CHAR(tRATIONAL);
934 TOKEN2CHAR(tIMAGINARY);
935 TOKEN2CHAR(tCHAR);
936 TOKEN2CHAR(tNTH_REF);
937 TOKEN2CHAR(tBACK_REF);
938 TOKEN2CHAR(tSTRING_CONTENT);
939 TOKEN2CHAR(tREGEXP_END);
940 TOKEN2CHAR(tDUMNY_END);
941 TOKEN2CHAR(tSP);
942 TOKEN2CHAR(tUPLUS);
943 TOKEN2CHAR(tUMINUS);
944 TOKEN2CHAR(tPOW);
945 TOKEN2CHAR(tCMP);
946 TOKEN2CHAR(tEQ);
947 TOKEN2CHAR(tEQQ);
948 TOKEN2CHAR(tNEQ);
949 TOKEN2CHAR(tGEQ);
950 TOKEN2CHAR(tLEQ);
951 TOKEN2CHAR(tANDOP);
952 TOKEN2CHAR(tOROP);
953 TOKEN2CHAR(tMATCH);
954 TOKEN2CHAR(tNMATCH);
955 TOKEN2CHAR(tDOT2);
956 TOKEN2CHAR(tDOT3);
957 TOKEN2CHAR(tBDOT2);
958 TOKEN2CHAR(tBDOT3);
959 TOKEN2CHAR(tAREF);
960 TOKEN2CHAR(tASET);
961 TOKEN2CHAR(tLSHFT);
962 TOKEN2CHAR(tRSHFT);
963 TOKEN2CHAR(tANDDOT);
964 TOKEN2CHAR(tCOLON2);
965 TOKEN2CHAR(tCOLON3);
966 TOKEN2CHAR(tOP_ASGN);
967 TOKEN2CHAR(tASSOC);
968 TOKEN2CHAR(tLPAREN);
969 TOKEN2CHAR(tLPAREN_ARG);
970 TOKEN2CHAR(tLBRACK);
971 TOKEN2CHAR(tLBRACE);
972 TOKEN2CHAR(tLBRACE_ARG);
973 TOKEN2CHAR(tSTAR);
974 TOKEN2CHAR(tDSTAR);
975 TOKEN2CHAR(tAMPER);
976 TOKEN2CHAR(tLAMBDA);
977 TOKEN2CHAR(tSYMBEG);
978 TOKEN2CHAR(tSTRING_BEG);
979 TOKEN2CHAR(tXSTRING_BEG);
980 TOKEN2CHAR(tREGEXP_BEG);
981 TOKEN2CHAR(tWORDS_BEG);
982 TOKEN2CHAR(tQWORDS_BEG);
983 TOKEN2CHAR(tSYMBOLS_BEG);
984 TOKEN2CHAR(tQSYMBOLS_BEG);
985 TOKEN2CHAR(tSTRING_END);
986 TOKEN2CHAR(tSTRING_DEND);
987 TOKEN2CHAR(tSTRING_DBEG);
988 TOKEN2CHAR(tSTRING_DVAR);
989 TOKEN2CHAR(tLAMBEG);
990 TOKEN2CHAR(tLABEL_END);
991 TOKEN2CHAR(tIGNORED_NL);
992 TOKEN2CHAR(tCOMMENT);
993 TOKEN2CHAR(tEMBDOC_BEG);
994 TOKEN2CHAR(tEMBDOC);
995 TOKEN2CHAR(tEMBDOC_END);
996 TOKEN2CHAR(tHEREDOC_BEG);
997 TOKEN2CHAR(tHEREDOC_END);
998 TOKEN2CHAR(k__END__);
999 TOKEN2CHAR(tLOWEST);
1000 TOKEN2CHAR(tUMINUS_NUM);
1001 TOKEN2CHAR(tLAST_TOKEN);
1002#undef TOKEN2CHAR
1003#undef TOKEN2CHAR2
1004 }
1005
1006 rb_bug("parser_token2id: unknown token %d", tok);
1007
1008 UNREACHABLE_RETURN(0);
1009}
1010#else
1011static void
1012push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
1013{
1014}
1015
1016static void
1017pop_end_expect_token_locations(struct parser_params *p)
1018{
1019}
1020#endif
1021
1022RBIMPL_ATTR_NONNULL((1, 2, 3))
1023static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
1024RBIMPL_ATTR_NONNULL((1, 2))
1025static int parser_yyerror0(struct parser_params*, const char*);
1026#define yyerror0(msg) parser_yyerror0(p, (msg))
1027#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
1028#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
1029#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
1030#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
1031#define lex_eol_p(p) lex_eol_n_p(p, 0)
1032#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
1033#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
1034#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
1035
1036static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
1037static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
1038static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
1039static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
1040static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
1041
1042#ifdef RIPPER
1043#define compile_for_eval (0)
1044#else
1045#define compile_for_eval (p->parent_iseq != 0)
1046#endif
1047
1048#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
1049
1050#define CALL_Q_P(q) ((q) == tANDDOT)
1051#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
1052
1053#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
1054
1055static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
1056
1057static inline void
1058rb_discard_node(struct parser_params *p, NODE *n)
1059{
1060 rb_ast_delete_node(p->ast, n);
1061}
1062
1063static rb_node_scope_t *rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1064static 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);
1065static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1066static rb_node_if_t *rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE* if_keyword_loc, const YYLTYPE* then_keyword_loc, const YYLTYPE* end_keyword_loc);
1067static rb_node_unless_t *rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *end_keyword_loc);
1068static rb_node_case_t *rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc);
1069static rb_node_case2_t *rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc);
1070static rb_node_case3_t *rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc);
1071static rb_node_when_t *rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc);
1072static rb_node_in_t *rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc);
1073static rb_node_while_t *rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc);
1074static rb_node_until_t *rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc);
1075static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1076static rb_node_for_t *rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *for_keyword_loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *do_keyword_loc, const YYLTYPE *end_keyword_loc);
1077static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
1078static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
1079static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1080static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
1081static rb_node_resbody_t *rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc);
1082static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
1083static rb_node_and_t *rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1084static rb_node_or_t *rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1085static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
1086static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1087static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1088static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1089static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1090static rb_node_cdecl_t *rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc);
1091static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1092static 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, const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc);
1093static 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, const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc);
1094static 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);
1095static 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);
1096static rb_node_op_cdecl_t *rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, enum rb_parser_shareability shareability, const YYLTYPE *loc);
1097static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1098static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1099static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1100static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1101static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1102static rb_node_super_t *rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc);
1103static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
1104static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1105static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1106static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
1107static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1108static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1109static rb_node_yield_t *rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc);
1110static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1111static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1112static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1113static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1114static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1115static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1116static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1117static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1118static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1119static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1120static rb_node_integer_t * rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc);
1121static rb_node_float_t * rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc);
1122static rb_node_rational_t * rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc);
1123static rb_node_imaginary_t * rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type, const YYLTYPE *loc);
1124static rb_node_str_t *rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1125static rb_node_dstr_t *rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1126static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1127static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1128static rb_node_dxstr_t *rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1129static rb_node_evstr_t *rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1130static rb_node_regx_t *rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc);
1131static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1132static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
1133static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc);
1134static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1135static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1136static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
1137static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1138static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1139static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1140static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1141static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1142static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1143static rb_node_alias_t *rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1144static rb_node_valias_t *rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1145static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
1146static rb_node_class_t *rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *inheritance_operator_loc, const YYLTYPE *end_keyword_loc);
1147static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc);
1148static rb_node_sclass_t *rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc);
1149static rb_node_colon2_t *rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc);
1150static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc);
1151static rb_node_dot2_t *rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1152static rb_node_dot3_t *rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1153static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
1154static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
1155static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc);
1156static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
1157static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
1158static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1159static rb_node_postexe_t *rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1160static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1161static rb_node_dsym_t *rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1162static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1163static rb_node_lambda_t *rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1164static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1165static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
1166static 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);
1167static rb_node_line_t *rb_node_line_new(struct parser_params *p, const YYLTYPE *loc);
1168static rb_node_file_t *rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1169static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
1170
1171#define NEW_SCOPE(a,b,loc) (NODE *)rb_node_scope_new(p,a,b,loc)
1172#define NEW_SCOPE2(t,a,b,loc) (NODE *)rb_node_scope_new2(p,t,a,b,loc)
1173#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
1174#define NEW_IF(c,t,e,loc,ik_loc,tk_loc,ek_loc) (NODE *)rb_node_if_new(p,c,t,e,loc,ik_loc,tk_loc,ek_loc)
1175#define NEW_UNLESS(c,t,e,loc,k_loc,t_loc,e_loc) (NODE *)rb_node_unless_new(p,c,t,e,loc,k_loc,t_loc,e_loc)
1176#define NEW_CASE(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case_new(p,h,b,loc,ck_loc,ek_loc)
1177#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc)
1178#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc)
1179#define NEW_WHEN(c,t,e,loc,k_loc,t_loc) (NODE *)rb_node_when_new(p,c,t,e,loc,k_loc,t_loc)
1180#define NEW_IN(c,t,e,loc) (NODE *)rb_node_in_new(p,c,t,e,loc)
1181#define NEW_WHILE(c,b,n,loc,k_loc,c_loc) (NODE *)rb_node_while_new(p,c,b,n,loc,k_loc,c_loc)
1182#define NEW_UNTIL(c,b,n,loc,k_loc,c_loc) (NODE *)rb_node_until_new(p,c,b,n,loc,k_loc,c_loc)
1183#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
1184#define NEW_FOR(i,b,loc,f_loc,i_loc,d_loc,e_loc) (NODE *)rb_node_for_new(p,i,b,loc,f_loc,i_loc,d_loc,e_loc)
1185#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
1186#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
1187#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
1188#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
1189#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc)
1190#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
1191#define NEW_AND(f,s,loc,op_loc) (NODE *)rb_node_and_new(p,f,s,loc,op_loc)
1192#define NEW_OR(f,s,loc,op_loc) (NODE *)rb_node_or_new(p,f,s,loc,op_loc)
1193#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
1194#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
1195#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
1196#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
1197#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
1198#define NEW_CDECL(v,val,path,share,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,share,loc)
1199#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
1200#define NEW_OP_ASGN1(r,id,idx,rval,loc,c_op_loc,o_loc,c_loc,b_op_loc) (NODE *)rb_node_op_asgn1_new(p,r,id,idx,rval,loc,c_op_loc,o_loc,c_loc,b_op_loc)
1201#define NEW_OP_ASGN2(r,t,i,o,val,loc,c_op_loc,m_loc,b_op_loc) (NODE *)rb_node_op_asgn2_new(p,r,val,i,o,t,loc,c_op_loc,m_loc,b_op_loc)
1202#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
1203#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
1204#define NEW_OP_CDECL(v,op,val,share,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,share,loc)
1205#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
1206#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
1207#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
1208#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
1209#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
1210#define NEW_SUPER(a,loc,k_loc,l_loc,r_loc) (NODE *)rb_node_super_new(p,a,loc,k_loc,l_loc,r_loc)
1211#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
1212#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
1213#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
1214#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1215#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1216#define NEW_RETURN(s,loc,k_loc) (NODE *)rb_node_return_new(p,s,loc,k_loc)
1217#define NEW_YIELD(a,loc,k_loc,l_loc,r_loc) (NODE *)rb_node_yield_new(p,a,loc,k_loc,l_loc,r_loc)
1218#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1219#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1220#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1221#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1222#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1223#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1224#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1225#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1226#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1227#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1228#define NEW_INTEGER(val, base,loc) (NODE *)rb_node_integer_new(p,val,base,loc)
1229#define NEW_FLOAT(val,loc) (NODE *)rb_node_float_new(p,val,loc)
1230#define NEW_RATIONAL(val,base,seen_point,loc) (NODE *)rb_node_rational_new(p,val,base,seen_point,loc)
1231#define NEW_IMAGINARY(val,base,seen_point,numeric_type,loc) (NODE *)rb_node_imaginary_new(p,val,base,seen_point,numeric_type,loc)
1232#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1233#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1234#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1235#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1236#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1237#define NEW_EVSTR(n,loc,o_loc,c_loc) (NODE *)rb_node_evstr_new(p,n,loc,o_loc,c_loc)
1238#define NEW_REGX(str,opts,loc,o_loc,ct_loc,c_loc) (NODE *)rb_node_regx_new(p,str,opts,loc,o_loc,ct_loc,c_loc)
1239#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1240#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1241#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1242#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1243#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1244#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1245#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1246#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1247#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc)
1248#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_loc)
1249#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1250#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1251#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
1252#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
1253#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1254#define NEW_CLASS(n,b,s,loc,ck_loc,io_loc,ek_loc) (NODE *)rb_node_class_new(p,n,b,s,loc,ck_loc,io_loc,ek_loc)
1255#define NEW_MODULE(n,b,loc) (NODE *)rb_node_module_new(p,n,b,loc)
1256#define NEW_SCLASS(r,b,loc) (NODE *)rb_node_sclass_new(p,r,b,loc)
1257#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc)
1258#define NEW_COLON3(i,loc,d_loc,n_loc) (NODE *)rb_node_colon3_new(p,i,loc,d_loc,n_loc)
1259#define NEW_DOT2(b,e,loc,op_loc) (NODE *)rb_node_dot2_new(p,b,e,loc,op_loc)
1260#define NEW_DOT3(b,e,loc,op_loc) (NODE *)rb_node_dot3_new(p,b,e,loc,op_loc)
1261#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
1262#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
1263#define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc)
1264#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
1265#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
1266#define NEW_DEFINED(e,loc) (NODE *)rb_node_defined_new(p,e,loc)
1267#define NEW_POSTEXE(b,loc,k_loc,o_loc,c_loc) (NODE *)rb_node_postexe_new(p,b,loc,k_loc,o_loc,c_loc)
1268#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc)
1269#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1270#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1271#define NEW_LAMBDA(a,b,loc,op_loc,o_loc,c_loc) (NODE *)rb_node_lambda_new(p,a,b,loc,op_loc,o_loc,c_loc)
1272#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1273#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1274#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1275#define NEW_LINE(loc) (NODE *)rb_node_line_new(p,loc)
1276#define NEW_FILE(str,loc) (NODE *)rb_node_file_new(p,str,loc)
1277#define NEW_ENCODING(loc) (NODE *)rb_node_encoding_new(p,loc)
1278#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1279
1280enum internal_node_type {
1281 NODE_INTERNAL_ONLY = NODE_LAST,
1282 NODE_DEF_TEMP,
1283 NODE_EXITS,
1284 NODE_INTERNAL_LAST
1285};
1286
1287static const char *
1288parser_node_name(int node)
1289{
1290 switch (node) {
1291 case NODE_DEF_TEMP:
1292 return "NODE_DEF_TEMP";
1293 case NODE_EXITS:
1294 return "NODE_EXITS";
1295 default:
1296 return ruby_node_name(node);
1297 }
1298}
1299
1300/* This node is parse.y internal */
1301struct RNode_DEF_TEMP {
1302 NODE node;
1303
1304 /* for NODE_DEFN/NODE_DEFS */
1305
1306 struct RNode *nd_def;
1307 ID nd_mid;
1308
1309 struct {
1310 int max_numparam;
1311 NODE *numparam_save;
1312 struct lex_context ctxt;
1313 } save;
1314};
1315
1316#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1317
1318static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1319static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1320static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1321static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1322static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1323
1324#define NEW_BREAK(s,loc,k_loc) (NODE *)rb_node_break_new(p,s,loc,k_loc)
1325#define NEW_NEXT(s,loc,k_loc) (NODE *)rb_node_next_new(p,s,loc,k_loc)
1326#define NEW_REDO(loc,k_loc) (NODE *)rb_node_redo_new(p,loc,k_loc)
1327#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1328
1329/* Make a new internal node, which should not be appeared in the
1330 * result AST and does not have node_id and location. */
1331static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1332#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1333
1334static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1335
1336static int
1337parser_get_node_id(struct parser_params *p)
1338{
1339 int node_id = p->node_id;
1340 p->node_id++;
1341 return node_id;
1342}
1343
1344static void
1345anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1346{
1347 if (id == tANDDOT) {
1348 yyerror1(loc, "&. inside multiple assignment destination");
1349 }
1350}
1351
1352static inline void
1353set_line_body(NODE *body, int line)
1354{
1355 if (!body) return;
1356 switch (nd_type(body)) {
1357 case NODE_RESCUE:
1358 case NODE_ENSURE:
1359 nd_set_line(body, line);
1360 }
1361}
1362
1363static void
1364set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1365{
1366 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1367 nd_set_line(node, beg->end_pos.lineno);
1368}
1369
1370static NODE *
1371last_expr_node(NODE *expr)
1372{
1373 while (expr) {
1374 if (nd_type_p(expr, NODE_BLOCK)) {
1375 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1376 }
1377 else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) {
1378 expr = RNODE_BEGIN(expr)->nd_body;
1379 }
1380 else {
1381 break;
1382 }
1383 }
1384 return expr;
1385}
1386
1387#ifndef RIPPER
1388#define yyparse ruby_yyparse
1389#endif
1390
1391static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1392static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1393#define new_nil(loc) NEW_NIL(loc)
1394static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1395static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1396static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1397static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1398
1399static NODE *newline_node(NODE*);
1400static void fixpos(NODE*,NODE*);
1401
1402static int value_expr_gen(struct parser_params*,NODE*);
1403static void void_expr(struct parser_params*,NODE*);
1404static NODE *remove_begin(NODE*);
1405#define value_expr(node) value_expr_gen(p, (node))
1406static NODE *void_stmts(struct parser_params*,NODE*);
1407static void reduce_nodes(struct parser_params*,NODE**);
1408static void block_dup_check(struct parser_params*,NODE*,NODE*);
1409
1410static NODE *block_append(struct parser_params*,NODE*,NODE*);
1411static NODE *list_append(struct parser_params*,NODE*,NODE*);
1412static NODE *list_concat(NODE*,NODE*);
1413static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1414static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1415static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1416static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1417static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1418static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1419static NODE *str2dstr(struct parser_params*,NODE*);
1420static NODE *evstr2dstr(struct parser_params*,NODE*);
1421static NODE *splat_array(NODE*);
1422static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1423
1424static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1425static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1426static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1427static 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);
1428static 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;}
1429
1430static bool args_info_empty_p(struct rb_args_info *args);
1431static 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*);
1432static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1433static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1434static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1435static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1436static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1437static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1438static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1439
1440static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1441static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1442
1443static NODE* negate_lit(struct parser_params*, NODE*);
1444static void no_blockarg(struct parser_params*,NODE*);
1445static NODE *ret_args(struct parser_params*,NODE*);
1446static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1447static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1448
1449static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1450static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1451
1452static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1453static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1454
1455static VALUE rb_backref_error(struct parser_params*,NODE*);
1456static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1457
1458static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1459static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc);
1460static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc);
1461static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1462static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1463
1464static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1465
1466static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1467static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1468
1469static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1470static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1471
1472static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
1473
1474static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *);
1475
1476#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1477
1478static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1479
1480static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1481
1482static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1483
1484static rb_ast_id_table_t *local_tbl(struct parser_params*);
1485
1486static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1487static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1488
1489static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1490static NODE *heredoc_dedent(struct parser_params*,NODE*);
1491
1492static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1493
1494static rb_locations_lambda_body_t* new_locations_lambda_body(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1495
1496#ifdef RIPPER
1497#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1498#define set_value(val) (p->s_lvalue = val)
1499static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1500static int id_is_var(struct parser_params *p, ID id);
1501#endif
1502
1503RUBY_SYMBOL_EXPORT_BEGIN
1504VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1505int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1506enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1507VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1508void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1509PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1510YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1511YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1512YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1513YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1514YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1515YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1516void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1517RUBY_SYMBOL_EXPORT_END
1518
1519static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1520static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1521static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1522static VALUE formal_argument_error(struct parser_params*, ID);
1523static ID shadowing_lvar(struct parser_params*,ID);
1524static void new_bv(struct parser_params*,ID);
1525
1526static void local_push(struct parser_params*,int);
1527static void local_pop(struct parser_params*);
1528static void local_var(struct parser_params*, ID);
1529static void arg_var(struct parser_params*, ID);
1530static int local_id(struct parser_params *p, ID id);
1531static int local_id_ref(struct parser_params*, ID, ID **);
1532#define internal_id rb_parser_internal_id
1533ID internal_id(struct parser_params*);
1534static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1535static int check_forwarding_args(struct parser_params*);
1536static void add_forwarding_args(struct parser_params *p);
1537static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1538
1539static const struct vtable *dyna_push(struct parser_params *);
1540static void dyna_pop(struct parser_params*, const struct vtable *);
1541static int dyna_in_block(struct parser_params*);
1542#define dyna_var(p, id) local_var(p, id)
1543static int dvar_defined(struct parser_params*, ID);
1544#define dvar_defined_ref rb_parser_dvar_defined_ref
1545int dvar_defined_ref(struct parser_params*, ID, ID**);
1546static int dvar_curr(struct parser_params*,ID);
1547
1548static int lvar_defined(struct parser_params*, ID);
1549
1550static NODE *numparam_push(struct parser_params *p);
1551static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1552
1553#define METHOD_NOT '!'
1554
1555#define idFWD_REST '*'
1556#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1557#define idFWD_BLOCK '&'
1558#define idFWD_ALL idDot3
1559#define arg_FWD_BLOCK idFWD_BLOCK
1560
1561#define RE_ONIG_OPTION_IGNORECASE 1
1562#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1563#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1564#define RE_OPTION_ONCE (1<<16)
1565#define RE_OPTION_ENCODING_SHIFT 8
1566#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1567#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1568#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1569#define RE_OPTION_MASK 0xff
1570#define RE_OPTION_ARG_ENCODING_NONE 32
1571
1572#define CHECK_LITERAL_WHEN (st_table *)1
1573#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1574
1575#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1576RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1577
1578#define TOKEN2ID(tok) ( \
1579 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1580 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1581 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1582 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1583 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1584 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1585 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1586
1587/****** Ripper *******/
1588
1589#ifdef RIPPER
1590
1591#include "eventids1.h"
1592#include "eventids2.h"
1593
1594extern const struct ripper_parser_ids ripper_parser_ids;
1595
1596static VALUE ripper_dispatch0(struct parser_params*,ID);
1597static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1598static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1599static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1600static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1601static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1602static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1603void ripper_error(struct parser_params *p);
1604
1605#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1606#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1607#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1608#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1609#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1610#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1611#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1612
1613#define yyparse ripper_yyparse
1614
1615static VALUE
1616aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1617{
1618 if (!NIL_P(pre_arg)) {
1619 if (!NIL_P(pre_args)) {
1620 rb_ary_unshift(pre_args, pre_arg);
1621 }
1622 else {
1623 pre_args = rb_ary_new_from_args(1, pre_arg);
1624 }
1625 }
1626 return pre_args;
1627}
1628
1629#define ID2VAL(id) STATIC_ID2SYM(id)
1630#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1631#endif /* RIPPER */
1632
1633#define KWD2EID(t, v) keyword_##t
1634
1635static NODE *
1636new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, const YYLTYPE *loc)
1637{
1638 body = remove_begin(body);
1639 reduce_nodes(p, &body);
1640 NODE *n = NEW_SCOPE(args, body, loc);
1641 nd_set_line(n, loc->end_pos.lineno);
1642 set_line_body(body, loc->beg_pos.lineno);
1643 return n;
1644}
1645
1646static NODE *
1647rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1648 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1649{
1650 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1651 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1652 loc.beg_pos = arg_loc->beg_pos;
1653 return NEW_RESCUE(arg, rescue, 0, &loc);
1654}
1655
1656static NODE *add_block_exit(struct parser_params *p, NODE *node);
1657static rb_node_exits_t *init_block_exit(struct parser_params *p);
1658static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1659static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1660static void clear_block_exit(struct parser_params *p, bool error);
1661
1662static void
1663next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1664{
1665 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1666}
1667
1668static void
1669restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1670{
1671 /* See: def_name action */
1672 struct lex_context ctxt = temp->save.ctxt;
1673 p->ctxt.in_def = ctxt.in_def;
1674 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1675 p->ctxt.in_rescue = ctxt.in_rescue;
1676 p->max_numparam = temp->save.max_numparam;
1677 numparam_pop(p, temp->save.numparam_save);
1678 clear_block_exit(p, true);
1679}
1680
1681static void
1682endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1683{
1684 if (is_attrset_id(mid)) {
1685 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1686 }
1687 token_info_drop(p, "def", loc->beg_pos);
1688}
1689
1690#define debug_token_line(p, name, line) do { \
1691 if (p->debug) { \
1692 const char *const pcur = p->lex.pcur; \
1693 const char *const ptok = p->lex.ptok; \
1694 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1695 line, p->ruby_sourceline, \
1696 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1697 } \
1698 } while (0)
1699
1700#define begin_definition(k, loc_beg, loc_end) \
1701 do { \
1702 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1703 /* singleton class */ \
1704 p->ctxt.cant_return = !p->ctxt.in_def; \
1705 p->ctxt.in_def = 0; \
1706 } \
1707 else if (p->ctxt.in_def) { \
1708 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1709 yyerror1(&loc, k " definition in method body"); \
1710 } \
1711 else { \
1712 p->ctxt.cant_return = 1; \
1713 } \
1714 local_push(p, 0); \
1715 } while (0)
1716
1717#ifndef RIPPER
1718# define ifndef_ripper(x) (x)
1719# define ifdef_ripper(r,x) (x)
1720#else
1721# define ifndef_ripper(x)
1722# define ifdef_ripper(r,x) (r)
1723#endif
1724
1725# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1726# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1727# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1728# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1729# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1730# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1731# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1732# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1733# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1734# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1735# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1736# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1737# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1738# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1739# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1740# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1741# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1742# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1743# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1744# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1745#ifdef RIPPER
1746extern const ID id_warn, id_warning, id_gets, id_assoc;
1747# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1748# define WARN_S_L(s,l) STR_NEW(s,l)
1749# define WARN_S(s) STR_NEW2(s)
1750# define WARN_I(i) INT2NUM(i)
1751# define WARN_ID(i) rb_id2str(i)
1752# define PRIsWARN PRIsVALUE
1753# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1754# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1755# ifdef HAVE_VA_ARGS_MACRO
1756# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1757# else
1758# define WARN_CALL rb_funcall
1759# endif
1760# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1761# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1762# ifdef HAVE_VA_ARGS_MACRO
1763# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1764# else
1765# define WARNING_CALL rb_funcall
1766# endif
1767# define compile_error ripper_compile_error
1768#else
1769# define WARN_S_L(s,l) s
1770# define WARN_S(s) s
1771# define WARN_I(i) i
1772# define WARN_ID(i) rb_id2name(i)
1773# define PRIsWARN PRIsVALUE
1774# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1775# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1776# define WARN_CALL rb_compile_warn
1777# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1778# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1779# define WARNING_CALL rb_compile_warning
1780PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1781# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1782#endif
1783
1784#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1785
1786static NODE *
1787add_block_exit(struct parser_params *p, NODE *node)
1788{
1789 if (!node) {
1790 compile_error(p, "unexpected null node");
1791 return 0;
1792 }
1793 switch (nd_type(node)) {
1794 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1795 default:
1796 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1797 return node;
1798 }
1799 if (!p->ctxt.in_defined) {
1800 rb_node_exits_t *exits = p->exits;
1801 if (exits) {
1802 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1803 exits->nd_stts = node;
1804 }
1805 }
1806 return node;
1807}
1808
1809static rb_node_exits_t *
1810init_block_exit(struct parser_params *p)
1811{
1812 rb_node_exits_t *old = p->exits;
1813 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1814 exits->nd_chain = 0;
1815 exits->nd_stts = RNODE(exits);
1816 p->exits = exits;
1817 return old;
1818}
1819
1820static rb_node_exits_t *
1821allow_block_exit(struct parser_params *p)
1822{
1823 rb_node_exits_t *exits = p->exits;
1824 p->exits = 0;
1825 return exits;
1826}
1827
1828static void
1829restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1830{
1831 p->exits = exits;
1832}
1833
1834static void
1835clear_block_exit(struct parser_params *p, bool error)
1836{
1837 rb_node_exits_t *exits = p->exits;
1838 if (!exits) return;
1839 if (error) {
1840 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1841 switch (nd_type(e)) {
1842 case NODE_BREAK:
1843 yyerror1(&e->nd_loc, "Invalid break");
1844 break;
1845 case NODE_NEXT:
1846 yyerror1(&e->nd_loc, "Invalid next");
1847 break;
1848 case NODE_REDO:
1849 yyerror1(&e->nd_loc, "Invalid redo");
1850 break;
1851 default:
1852 yyerror1(&e->nd_loc, "unexpected node");
1853 goto end_checks; /* no nd_chain */
1854 }
1855 }
1856 end_checks:;
1857 }
1858 exits->nd_stts = RNODE(exits);
1859 exits->nd_chain = 0;
1860}
1861
1862#define WARN_EOL(tok) \
1863 (looking_at_eol_p(p) ? \
1864 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1865 (void)0)
1866static int looking_at_eol_p(struct parser_params *p);
1867
1868static NODE *
1869get_nd_value(struct parser_params *p, NODE *node)
1870{
1871 switch (nd_type(node)) {
1872 case NODE_GASGN:
1873 return RNODE_GASGN(node)->nd_value;
1874 case NODE_IASGN:
1875 return RNODE_IASGN(node)->nd_value;
1876 case NODE_LASGN:
1877 return RNODE_LASGN(node)->nd_value;
1878 case NODE_DASGN:
1879 return RNODE_DASGN(node)->nd_value;
1880 case NODE_MASGN:
1881 return RNODE_MASGN(node)->nd_value;
1882 case NODE_CVASGN:
1883 return RNODE_CVASGN(node)->nd_value;
1884 case NODE_CDECL:
1885 return RNODE_CDECL(node)->nd_value;
1886 default:
1887 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1888 return 0;
1889 }
1890}
1891
1892static void
1893set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1894{
1895 switch (nd_type(node)) {
1896 case NODE_CDECL:
1897 RNODE_CDECL(node)->nd_value = rhs;
1898 break;
1899 case NODE_GASGN:
1900 RNODE_GASGN(node)->nd_value = rhs;
1901 break;
1902 case NODE_IASGN:
1903 RNODE_IASGN(node)->nd_value = rhs;
1904 break;
1905 case NODE_LASGN:
1906 RNODE_LASGN(node)->nd_value = rhs;
1907 break;
1908 case NODE_DASGN:
1909 RNODE_DASGN(node)->nd_value = rhs;
1910 break;
1911 case NODE_MASGN:
1912 RNODE_MASGN(node)->nd_value = rhs;
1913 break;
1914 case NODE_CVASGN:
1915 RNODE_CVASGN(node)->nd_value = rhs;
1916 break;
1917 default:
1918 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1919 break;
1920 }
1921}
1922
1923static ID
1924get_nd_vid(struct parser_params *p, NODE *node)
1925{
1926 switch (nd_type(node)) {
1927 case NODE_CDECL:
1928 return RNODE_CDECL(node)->nd_vid;
1929 case NODE_GASGN:
1930 return RNODE_GASGN(node)->nd_vid;
1931 case NODE_IASGN:
1932 return RNODE_IASGN(node)->nd_vid;
1933 case NODE_LASGN:
1934 return RNODE_LASGN(node)->nd_vid;
1935 case NODE_DASGN:
1936 return RNODE_DASGN(node)->nd_vid;
1937 case NODE_CVASGN:
1938 return RNODE_CVASGN(node)->nd_vid;
1939 default:
1940 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1941 return 0;
1942 }
1943}
1944
1945static NODE *
1946get_nd_args(struct parser_params *p, NODE *node)
1947{
1948 switch (nd_type(node)) {
1949 case NODE_CALL:
1950 return RNODE_CALL(node)->nd_args;
1951 case NODE_OPCALL:
1952 return RNODE_OPCALL(node)->nd_args;
1953 case NODE_FCALL:
1954 return RNODE_FCALL(node)->nd_args;
1955 case NODE_QCALL:
1956 return RNODE_QCALL(node)->nd_args;
1957 case NODE_SUPER:
1958 return RNODE_SUPER(node)->nd_args;
1959 case NODE_VCALL:
1960 case NODE_ZSUPER:
1961 case NODE_YIELD:
1962 case NODE_RETURN:
1963 case NODE_BREAK:
1964 case NODE_NEXT:
1965 return 0;
1966 default:
1967 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1968 return 0;
1969 }
1970}
1971
1972static st_index_t
1973djb2(const uint8_t *str, size_t len)
1974{
1975 st_index_t hash = 5381;
1976
1977 for (size_t i = 0; i < len; i++) {
1978 hash = ((hash << 5) + hash) + str[i];
1979 }
1980
1981 return hash;
1982}
1983
1984static st_index_t
1985parser_memhash(const void *ptr, long len)
1986{
1987 return djb2(ptr, len);
1988}
1989
1990#define PARSER_STRING_PTR(str) (str->ptr)
1991#define PARSER_STRING_LEN(str) (str->len)
1992#define PARSER_STRING_END(str) (&str->ptr[str->len])
1993#define STRING_SIZE(str) ((size_t)str->len + 1)
1994#define STRING_TERM_LEN(str) (1)
1995#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
1996#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
1997 SIZED_REALLOC_N(str->ptr, char, (size_t)total + termlen, STRING_SIZE(str)); \
1998 str->len = total; \
1999} while (0)
2000#define STRING_SET_LEN(str, n) do { \
2001 (str)->len = (n); \
2002} while (0)
2003#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2004 ((ptrvar) = str->ptr, \
2005 (lenvar) = str->len)
2006
2007static inline int
2008parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2009{
2010 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2011}
2012
2013static rb_parser_string_t *
2014rb_parser_string_new(rb_parser_t *p, const char *ptr, long len)
2015{
2016 rb_parser_string_t *str;
2017
2018 if (len < 0) {
2019 rb_bug("negative string size (or size too big): %ld", len);
2020 }
2021
2022 str = xcalloc(1, sizeof(rb_parser_string_t));
2023 str->ptr = xcalloc(len + 1, sizeof(char));
2024
2025 if (ptr) {
2026 memcpy(PARSER_STRING_PTR(str), ptr, len);
2027 }
2028 STRING_SET_LEN(str, len);
2029 STRING_TERM_FILL(str);
2030 return str;
2031}
2032
2033static rb_parser_string_t *
2034rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, long len, rb_encoding *enc)
2035{
2036 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2037 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2038 str->enc = enc;
2039 return str;
2040}
2041
2042#ifndef RIPPER
2043rb_parser_string_t *
2044rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2045{
2046 /* Type check */
2047 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2048 RB_GC_GUARD(str);
2049 return ret;
2050}
2051
2052void
2053rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2054{
2055 if (!str) return;
2056 xfree(PARSER_STRING_PTR(str));
2057 xfree(str);
2058}
2059#endif
2060
2061static st_index_t
2062rb_parser_str_hash(rb_parser_string_t *str)
2063{
2064 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2065}
2066
2067static st_index_t
2068rb_char_p_hash(const char *c)
2069{
2070 return parser_memhash((const void *)c, strlen(c));
2071}
2072
2073static size_t
2074rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2075{
2076 return PARSER_STRING_LEN(str);
2077}
2078
2079#ifndef RIPPER
2080static char *
2081rb_parser_string_end(rb_parser_string_t *str)
2082{
2083 return &str->ptr[str->len];
2084}
2085#endif
2086
2087static void
2088rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2089{
2090 str->enc = enc;
2091}
2092
2093static rb_encoding *
2094rb_parser_str_get_encoding(rb_parser_string_t *str)
2095{
2096 return str->enc;
2097}
2098
2099#ifndef RIPPER
2100static bool
2101PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2102{
2103 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2104}
2105#endif
2106
2107static int
2108PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2109{
2110 return str->coderange;
2111}
2112
2113static void
2114PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2115{
2116 str->coderange = coderange;
2117}
2118
2119static void
2120PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2121{
2122 rb_parser_string_set_encoding(str, enc);
2123 PARSER_ENC_CODERANGE_SET(str, cr);
2124}
2125
2126static void
2127PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2128{
2129 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2130}
2131
2132static bool
2133PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2134{
2135 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2136}
2137
2138static bool
2139PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2140{
2141 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2142}
2143
2144static const char *
2145rb_parser_search_nonascii(const char *p, const char *e)
2146{
2147 const char *s = p;
2148
2149 for (; s < e; s++) {
2150 if (*s & 0x80) return s;
2151 }
2152
2153 return NULL;
2154}
2155
2156static int
2157rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc)
2158{
2159 const char *e = ptr + len;
2160
2161 if (enc == rb_ascii8bit_encoding()) {
2162 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2163 ptr = rb_parser_search_nonascii(ptr, e);
2164 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2165 }
2166
2167 /* parser string encoding is always asciicompat */
2168 ptr = rb_parser_search_nonascii(ptr, e);
2169 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2170 for (;;) {
2171 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2172 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2173 ptr += MBCLEN_CHARFOUND_LEN(ret);
2174 if (ptr == e) break;
2175 ptr = rb_parser_search_nonascii(ptr, e);
2176 if (!ptr) break;
2177 }
2178
2179 return RB_PARSER_ENC_CODERANGE_VALID;
2180}
2181
2182static int
2183rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2184{
2185 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2186}
2187
2188static int
2189rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2190{
2191 int cr = PARSER_ENC_CODERANGE(str);
2192
2193 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2194 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2195 PARSER_ENC_CODERANGE_SET(str, cr);
2196 }
2197
2198 return cr;
2199}
2200
2201static rb_parser_string_t *
2202rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2203{
2204 if (rb_parser_str_get_encoding(str) == enc)
2205 return str;
2206 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2207 PARSER_ENC_CODERANGE_CLEAR(str);
2208 }
2209 rb_parser_string_set_encoding(str, enc);
2210 return str;
2211}
2212
2213static bool
2214rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2215{
2216 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2217}
2218
2219static rb_encoding *
2220rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2221{
2222 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2223 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2224
2225 if (enc1 == NULL || enc2 == NULL)
2226 return 0;
2227
2228 if (enc1 == enc2) {
2229 return enc1;
2230 }
2231
2232 if (PARSER_STRING_LEN(str2) == 0)
2233 return enc1;
2234 if (PARSER_STRING_LEN(str1) == 0)
2235 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2236
2237 int cr1, cr2;
2238
2239 cr1 = rb_parser_enc_str_coderange(p, str1);
2240 cr2 = rb_parser_enc_str_coderange(p, str2);
2241
2242 if (cr1 != cr2) {
2243 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2244 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2245 }
2246
2247 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2248 return enc1;
2249 }
2250
2251 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2252 return enc2;
2253 }
2254
2255 return 0;
2256}
2257
2258static void
2259rb_parser_str_modify(rb_parser_string_t *str)
2260{
2261 PARSER_ENC_CODERANGE_CLEAR(str);
2262}
2263
2264static void
2265rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len)
2266{
2267 long capa;
2268 const int termlen = STRING_TERM_LEN(str);
2269
2270 if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2271 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
2272 }
2273
2274 int cr = PARSER_ENC_CODERANGE(str);
2275 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2276 /* Leave unknown. */
2277 }
2278 else if (len > PARSER_STRING_LEN(str)) {
2279 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2280 }
2281 else if (len < PARSER_STRING_LEN(str)) {
2282 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2283 /* ASCII-only string is keeping after truncated. Valid
2284 * and broken may be invalid or valid, leave unknown. */
2285 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2286 }
2287 }
2288
2289 STRING_SET_LEN(str, len);
2290 STRING_TERM_FILL(str);
2291}
2292
2293static rb_parser_string_t *
2294rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len)
2295{
2296 rb_parser_str_modify(str);
2297 if (len == 0) return 0;
2298
2299 long total, olen, off = -1;
2300 char *sptr;
2301 const int termlen = STRING_TERM_LEN(str);
2302
2303 PARSER_STRING_GETMEM(str, sptr, olen);
2304 if (ptr >= sptr && ptr <= sptr + olen) {
2305 off = ptr - sptr;
2306 }
2307
2308 if (olen > LONG_MAX - len) {
2309 compile_error(p, "string sizes too big");
2310 return 0;
2311 }
2312 total = olen + len;
2313 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2314 sptr = PARSER_STRING_PTR(str);
2315 if (off != -1) {
2316 ptr = sptr + off;
2317 }
2318 memcpy(sptr + olen, ptr, len);
2319 STRING_SET_LEN(str, total);
2320 STRING_TERM_FILL(str);
2321
2322 return str;
2323}
2324
2325#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2326#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2327
2328static rb_parser_string_t *
2329rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2330 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2331{
2332 int str_cr, res_cr;
2333 rb_encoding *str_enc, *res_enc;
2334
2335 str_enc = rb_parser_str_get_encoding(str);
2336 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2337
2338 if (str_enc == ptr_enc) {
2339 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2340 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2341 }
2342 }
2343 else {
2344 /* parser string encoding is always asciicompat */
2345 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2346 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2347 }
2348 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2349 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2350 str_cr = rb_parser_enc_str_coderange(p, str);
2351 }
2352 }
2353 }
2354 if (ptr_cr_ret)
2355 *ptr_cr_ret = ptr_cr;
2356
2357 if (str_enc != ptr_enc &&
2358 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2359 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2360 goto incompatible;
2361 }
2362
2363 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2364 res_enc = str_enc;
2365 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2366 }
2367 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2368 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2369 res_enc = str_enc;
2370 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2371 }
2372 else {
2373 res_enc = ptr_enc;
2374 res_cr = ptr_cr;
2375 }
2376 }
2377 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2378 res_enc = str_enc;
2379 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2380 res_cr = str_cr;
2381 else
2382 res_cr = ptr_cr;
2383 }
2384 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2385 res_enc = str_enc;
2386 res_cr = str_cr;
2387 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2388 }
2389
2390 if (len < 0) {
2391 compile_error(p, "negative string size (or size too big)");
2392 }
2393 parser_str_cat(str, ptr, len);
2394 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2395 return str;
2396
2397 incompatible:
2398 compile_error(p, "incompatible character encodings: %s and %s",
2399 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2400 UNREACHABLE_RETURN(0);
2401
2402}
2403
2404static rb_parser_string_t *
2405rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2406 rb_encoding *ptr_enc)
2407{
2408 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2409}
2410
2411static rb_parser_string_t *
2412rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2413{
2414 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2415
2416 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2417 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2418
2419 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2420
2421 return str;
2422}
2423
2424static rb_parser_string_t *
2425rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len)
2426{
2427 if (len < 0) {
2428 rb_bug("negative string size (or size too big)");
2429 }
2430
2431 long slen = PARSER_STRING_LEN(str);
2432
2433 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2434 PARSER_ENC_CODERANGE_CLEAR(str);
2435 }
2436
2437 {
2438 long capa;
2439 const int termlen = STRING_TERM_LEN(str);
2440
2441 if ((capa = slen) < len) {
2442 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2443 }
2444 else if (len == slen) return str;
2445 STRING_SET_LEN(str, len);
2446 STRING_TERM_FILL(str);
2447 }
2448 return str;
2449}
2450
2451# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2452 ((ptrvar) = str->ptr, \
2453 (lenvar) = str->len, \
2454 (encvar) = str->enc)
2455
2456static int
2457rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2458{
2459 long len1, len2;
2460 const char *ptr1, *ptr2;
2461 rb_encoding *enc1, *enc2;
2462
2463 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2464 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2465
2466 return (len1 != len2 ||
2467 enc1 != enc2 ||
2468 memcmp(ptr1, ptr2, len1) != 0);
2469}
2470
2471static void
2472rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2473{
2474 long i;
2475 if (ary->capa < len) {
2476 ary->capa = len;
2477 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2478 for (i = ary->len; i < len; i++) {
2479 ary->data[i] = 0;
2480 }
2481 }
2482}
2483
2484/*
2485 * Do not call this directly.
2486 * Use rb_parser_ary_new_capa_for_XXX() instead.
2487 */
2488static rb_parser_ary_t *
2489parser_ary_new_capa(rb_parser_t *p, long len)
2490{
2491 if (len < 0) {
2492 rb_bug("negative array size (or size too big): %ld", len);
2493 }
2494 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2495 ary->data_type = 0;
2496 ary->len = 0;
2497 ary->capa = len;
2498 if (0 < len) {
2499 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2500 }
2501 else {
2502 ary->data = NULL;
2503 }
2504 return ary;
2505}
2506
2507#ifndef RIPPER
2508static rb_parser_ary_t *
2509rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2510{
2511 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2512 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2513 return ary;
2514}
2515
2516static rb_parser_ary_t *
2517rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2518{
2519 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2520 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2521 return ary;
2522}
2523#endif
2524
2525static rb_parser_ary_t *
2526rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2527{
2528 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2529 ary->data_type = PARSER_ARY_DATA_NODE;
2530 return ary;
2531}
2532
2533/*
2534 * Do not call this directly.
2535 * Use rb_parser_ary_push_XXX() instead.
2536 */
2537static rb_parser_ary_t *
2538parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2539{
2540 if (ary->len == ary->capa) {
2541 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2542 }
2543 ary->data[ary->len++] = val;
2544 return ary;
2545}
2546
2547#ifndef RIPPER
2548static rb_parser_ary_t *
2549rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2550{
2551 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2552 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2553 }
2554 return parser_ary_push(p, ary, val);
2555}
2556
2557static rb_parser_ary_t *
2558rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2559{
2560 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2561 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2562 }
2563 return parser_ary_push(p, ary, val);
2564}
2565#endif
2566
2567static rb_parser_ary_t *
2568rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2569{
2570 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2571 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2572 }
2573 return parser_ary_push(p, ary, val);
2574}
2575
2576#ifndef RIPPER
2577static void
2578rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2579{
2580 if (!token) return;
2581 rb_parser_string_free(p, token->str);
2582 xfree(token);
2583}
2584
2585static void
2586rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2587{
2588# define foreach_ary(ptr) \
2589 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2590 ptr < end_ary_data; ptr++)
2591 switch (ary->data_type) {
2592 case PARSER_ARY_DATA_AST_TOKEN:
2593 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2594 break;
2595 case PARSER_ARY_DATA_SCRIPT_LINE:
2596 foreach_ary(data) {rb_parser_string_free(p, *data);}
2597 break;
2598 case PARSER_ARY_DATA_NODE:
2599 /* Do nothing because nodes are freed when rb_ast_t is freed */
2600 break;
2601 default:
2602 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2603 break;
2604 }
2605# undef foreach_ary
2606 xfree(ary->data);
2607 xfree(ary);
2608}
2609
2610#endif /* !RIPPER */
2611%}
2612
2613%expect 0
2614%define api.pure
2615%define parse.error verbose
2616%printer {
2617 if ((NODE *)$$ == (NODE *)-1) {
2618 rb_parser_printf(p, "NODE_SPECIAL");
2619 }
2620 else if ($$) {
2621 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2622 }
2623} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2624 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2625%printer {
2626 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2627} <id>
2628%printer {
2629 switch (nd_type(RNODE($$))) {
2630 case NODE_INTEGER:
2631 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2632 break;
2633 case NODE_FLOAT:
2634 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2635 break;
2636 case NODE_RATIONAL:
2637 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2638 break;
2639 case NODE_IMAGINARY:
2640 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2641 break;
2642 default:
2643 break;
2644 }
2645} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2646%printer {
2647 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2648} tNTH_REF
2649%printer {
2650 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2651} tBACK_REF
2652
2653%destructor {
2654 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2655} <labels>
2656
2657%lex-param {struct parser_params *p}
2658%parse-param {struct parser_params *p}
2659%initial-action
2660{
2661 RUBY_SET_YYLLOC_OF_NONE(@$);
2662};
2663%after-shift after_shift
2664%before-reduce before_reduce
2665%after-reduce after_reduce
2666%after-shift-error-token after_shift_error_token
2667%after-pop-stack after_pop_stack
2668
2669%union {
2670 NODE *node;
2671 rb_node_fcall_t *node_fcall;
2672 rb_node_args_t *node_args;
2673 rb_node_args_aux_t *node_args_aux;
2674 rb_node_opt_arg_t *node_opt_arg;
2675 rb_node_kw_arg_t *node_kw_arg;
2676 rb_node_block_pass_t *node_block_pass;
2677 rb_node_masgn_t *node_masgn;
2678 rb_node_def_temp_t *node_def_temp;
2679 rb_node_exits_t *node_exits;
2680 struct rb_locations_lambda_body_t *locations_lambda_body;
2681 ID id;
2682 int num;
2683 st_table *tbl;
2684 st_table *labels;
2685 const struct vtable *vars;
2686 struct rb_strterm_struct *strterm;
2687 struct lex_context ctxt;
2688 enum lex_state_e state;
2689}
2690
2691%token <id>
2692 keyword_class "'class'"
2693 keyword_module "'module'"
2694 keyword_def "'def'"
2695 keyword_undef "'undef'"
2696 keyword_begin "'begin'"
2697 keyword_rescue "'rescue'"
2698 keyword_ensure "'ensure'"
2699 keyword_end "'end'"
2700 keyword_if "'if'"
2701 keyword_unless "'unless'"
2702 keyword_then "'then'"
2703 keyword_elsif "'elsif'"
2704 keyword_else "'else'"
2705 keyword_case "'case'"
2706 keyword_when "'when'"
2707 keyword_while "'while'"
2708 keyword_until "'until'"
2709 keyword_for "'for'"
2710 keyword_break "'break'"
2711 keyword_next "'next'"
2712 keyword_redo "'redo'"
2713 keyword_retry "'retry'"
2714 keyword_in "'in'"
2715 keyword_do "'do'"
2716 keyword_do_cond "'do' for condition"
2717 keyword_do_block "'do' for block"
2718 keyword_do_LAMBDA "'do' for lambda"
2719 keyword_return "'return'"
2720 keyword_yield "'yield'"
2721 keyword_super "'super'"
2722 keyword_self "'self'"
2723 keyword_nil "'nil'"
2724 keyword_true "'true'"
2725 keyword_false "'false'"
2726 keyword_and "'and'"
2727 keyword_or "'or'"
2728 keyword_not "'not'"
2729 modifier_if "'if' modifier"
2730 modifier_unless "'unless' modifier"
2731 modifier_while "'while' modifier"
2732 modifier_until "'until' modifier"
2733 modifier_rescue "'rescue' modifier"
2734 keyword_alias "'alias'"
2735 keyword_defined "'defined?'"
2736 keyword_BEGIN "'BEGIN'"
2737 keyword_END "'END'"
2738 keyword__LINE__ "'__LINE__'"
2739 keyword__FILE__ "'__FILE__'"
2740 keyword__ENCODING__ "'__ENCODING__'"
2741
2742%token <id> tIDENTIFIER "local variable or method"
2743%token <id> tFID "method"
2744%token <id> tGVAR "global variable"
2745%token <id> tIVAR "instance variable"
2746%token <id> tCONSTANT "constant"
2747%token <id> tCVAR "class variable"
2748%token <id> tLABEL "label"
2749%token <node> tINTEGER "integer literal"
2750%token <node> tFLOAT "float literal"
2751%token <node> tRATIONAL "rational literal"
2752%token <node> tIMAGINARY "imaginary literal"
2753%token <node> tCHAR "char literal"
2754%token <node> tNTH_REF "numbered reference"
2755%token <node> tBACK_REF "back reference"
2756%token <node> tSTRING_CONTENT "literal content"
2757%token <num> tREGEXP_END
2758%token <num> tDUMNY_END "dummy end"
2759
2760%type <node> singleton singleton_expr strings string string1 xstring regexp
2761%type <node> string_contents xstring_contents regexp_contents string_content
2762%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2763%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2764%type <node_def_temp> defn_head defs_head k_def
2765%type <node_exits> block_open k_while k_until k_for allow_exits
2766%type <node> top_stmts top_stmt begin_block endless_arg endless_command
2767%type <node> bodystmt stmts stmt_or_begin stmt expr arg ternary primary
2768%type <node> command command_call command_call_value method_call
2769%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2770%type <node_fcall> fcall
2771%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2772%type <node> args arg_splat call_args opt_call_args
2773%type <node> paren_args opt_paren_args
2774%type <node_args> args_tail block_args_tail
2775%type <node> command_args aref_args
2776%type <node_block_pass> opt_block_arg block_arg
2777%type <node> var_ref var_lhs
2778%type <node> command_rhs arg_rhs
2779%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2780%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args
2781%type <node_args_aux> f_arg f_arg_item
2782%type <node> f_marg f_rest_marg
2783%type <node_masgn> f_margs
2784%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2785%type <node_args> block_param opt_block_param_def block_param_def opt_block_param
2786%type <id> do bv_decls opt_bv_decl bvar
2787%type <node> lambda brace_body do_body
2788%type <locations_lambda_body> lambda_body
2789%type <node_args> f_larglist
2790%type <node> brace_block cmd_brace_block do_block lhs none fitem
2791%type <node> mlhs_head mlhs_item mlhs_node
2792%type <node_masgn> mlhs mlhs_basic mlhs_inner
2793%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2794%type <node> p_expr p_as p_alt p_expr_basic p_find
2795%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2796%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2797%type <node> p_kwargs p_kwarg p_kw
2798%type <id> keyword_variable user_variable sym operation2 operation3
2799%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
2800%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2801%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2802%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2803%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2804%type <ctxt> p_in_kwarg
2805%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2806%type <num> max_numparam
2807%type <node> numparam
2808%type <id> it_id
2809%token END_OF_INPUT 0 "end-of-input"
2810%token <id> '.'
2811
2812/* escaped chars, should be ignored otherwise */
2813%token <id> '\\' "backslash"
2814%token tSP "escaped space"
2815%token <id> '\t' "escaped horizontal tab"
2816%token <id> '\f' "escaped form feed"
2817%token <id> '\r' "escaped carriage return"
2818%token <id> '\13' "escaped vertical tab"
2819%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2820%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2821%token tPOW RUBY_TOKEN(POW) "**"
2822%token tCMP RUBY_TOKEN(CMP) "<=>"
2823%token tEQ RUBY_TOKEN(EQ) "=="
2824%token tEQQ RUBY_TOKEN(EQQ) "==="
2825%token tNEQ RUBY_TOKEN(NEQ) "!="
2826%token tGEQ RUBY_TOKEN(GEQ) ">="
2827%token tLEQ RUBY_TOKEN(LEQ) "<="
2828%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2829%token tOROP RUBY_TOKEN(OROP) "||"
2830%token tMATCH RUBY_TOKEN(MATCH) "=~"
2831%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2832%token tDOT2 RUBY_TOKEN(DOT2) ".."
2833%token tDOT3 RUBY_TOKEN(DOT3) "..."
2834%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2835%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2836%token tAREF RUBY_TOKEN(AREF) "[]"
2837%token tASET RUBY_TOKEN(ASET) "[]="
2838%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2839%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2840%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2841%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2842%token tCOLON3 ":: at EXPR_BEG"
2843%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2844%token tASSOC "=>"
2845%token tLPAREN "("
2846%token tLPAREN_ARG "( arg"
2847%token tLBRACK "["
2848%token tLBRACE "{"
2849%token tLBRACE_ARG "{ arg"
2850%token tSTAR "*"
2851%token tDSTAR "**arg"
2852%token tAMPER "&"
2853%token <num> tLAMBDA "->"
2854%token tSYMBEG "symbol literal"
2855%token tSTRING_BEG "string literal"
2856%token tXSTRING_BEG "backtick literal"
2857%token tREGEXP_BEG "regexp literal"
2858%token tWORDS_BEG "word list"
2859%token tQWORDS_BEG "verbatim word list"
2860%token tSYMBOLS_BEG "symbol list"
2861%token tQSYMBOLS_BEG "verbatim symbol list"
2862%token tSTRING_END "terminator"
2863%token tSTRING_DEND "'}'"
2864%token <state> tSTRING_DBEG "'#{'"
2865%token tSTRING_DVAR tLAMBEG tLABEL_END
2866
2867%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2868%token tHEREDOC_BEG tHEREDOC_END k__END__
2869
2870/*
2871 * precedence table
2872 */
2873
2874%nonassoc tLOWEST
2875%nonassoc tLBRACE_ARG
2876
2877%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2878%left keyword_or keyword_and
2879%right keyword_not
2880%nonassoc keyword_defined
2881%right '=' tOP_ASGN
2882%left modifier_rescue
2883%right '?' ':'
2884%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2885%left tOROP
2886%left tANDOP
2887%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2888%left '>' tGEQ '<' tLEQ
2889%left '|' '^'
2890%left '&'
2891%left tLSHFT tRSHFT
2892%left '+' '-'
2893%left '*' '/' '%'
2894%right tUMINUS_NUM tUMINUS
2895%right tPOW
2896%right '!' '~' tUPLUS
2897
2898%token tLAST_TOKEN
2899
2900/*
2901 * inlining rules
2902 */
2903%rule %inline ident_or_const
2904 : tIDENTIFIER
2905 | tCONSTANT
2906 ;
2907
2908%rule %inline user_or_keyword_variable
2909 : user_variable
2910 | keyword_variable
2911 ;
2912
2913/*
2914 * parameterizing rules
2915 */
2916%rule asgn(rhs) <node>
2917 : lhs '=' lex_ctxt rhs
2918 {
2919 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2920 /*% ripper: assign!($:1, $:4) %*/
2921 }
2922 ;
2923
2924%rule args_tail_basic(value) <node_args>
2925 : f_kwarg(value) ',' f_kwrest opt_f_block_arg
2926 {
2927 $$ = new_args_tail(p, $1, $3, $4, &@3);
2928 /*% ripper: [$:1, $:3, $:4] %*/
2929 }
2930 | f_kwarg(value) opt_f_block_arg
2931 {
2932 $$ = new_args_tail(p, $1, 0, $2, &@1);
2933 /*% ripper: [$:1, Qnil, $:2] %*/
2934 }
2935 | f_any_kwrest opt_f_block_arg
2936 {
2937 $$ = new_args_tail(p, 0, $1, $2, &@1);
2938 /*% ripper: [Qnil, $:1, $:2] %*/
2939 }
2940 | f_block_arg
2941 {
2942 $$ = new_args_tail(p, 0, 0, $1, &@1);
2943 /*% ripper: [Qnil, Qnil, $:1] %*/
2944 }
2945
2946%rule def_endless_method(bodystmt) <node>
2947 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2948 {
2949 endless_method_name(p, $head->nd_mid, &@head);
2950 restore_defun(p, $head);
2951 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
2952 ($$ = $head->nd_def)->nd_loc = @$;
2953 RNODE_DEFN($$)->nd_defn = $bodystmt;
2954 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2955 /*% ripper: def!($:head, $:args, $:$) %*/
2956 local_pop(p);
2957 }
2958 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2959 {
2960 endless_method_name(p, $head->nd_mid, &@head);
2961 restore_defun(p, $head);
2962 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
2963 ($$ = $head->nd_def)->nd_loc = @$;
2964 RNODE_DEFS($$)->nd_defn = $bodystmt;
2965 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2966 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2967 local_pop(p);
2968 }
2969 ;
2970
2971%rule compstmt(stmts) <node>
2972 : stmts terms?
2973 {
2974 void_stmts(p, $$ = $stmts);
2975 }
2976 ;
2977
2978%rule f_opt(value) <node_opt_arg>
2979 : f_arg_asgn f_eq value
2980 {
2981 p->ctxt.in_argdef = 1;
2982 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
2983 /*% ripper: [$:$, $:3] %*/
2984 }
2985 ;
2986
2987%rule f_opt_arg(value) <node_opt_arg>
2988 : f_opt(value)
2989 {
2990 $$ = $f_opt;
2991 /*% ripper: rb_ary_new3(1, $:1) %*/
2992 }
2993 | f_opt_arg(value) ',' f_opt(value)
2994 {
2995 $$ = opt_arg_append($f_opt_arg, $f_opt);
2996 /*% ripper: rb_ary_push($:1, $:3) %*/
2997 }
2998 ;
2999
3000%rule f_kw(value) <node_kw_arg>
3001 : f_label value
3002 {
3003 p->ctxt.in_argdef = 1;
3004 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3005 /*% ripper: [$:$, $:value] %*/
3006 }
3007 | f_label
3008 {
3009 p->ctxt.in_argdef = 1;
3010 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3011 /*% ripper: [$:$, 0] %*/
3012 }
3013 ;
3014
3015%rule f_kwarg(value) <node_kw_arg>
3016 : f_kw(value)
3017 {
3018 $$ = $f_kw;
3019 /*% ripper: rb_ary_new3(1, $:1) %*/
3020 }
3021 | f_kwarg(value) ',' f_kw(value)
3022 {
3023 $$ = kwd_append($f_kwarg, $f_kw);
3024 /*% ripper: rb_ary_push($:1, $:3) %*/
3025 }
3026 ;
3027
3028%rule mlhs(item) <node>
3029 : item
3030 {
3031 $$ = NEW_LIST($1, &@$);
3032 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3033 }
3034 | mlhs(item) ',' item
3035 {
3036 $$ = list_append(p, $1, $3);
3037 /*% ripper: mlhs_add!($:1, $:3) %*/
3038 }
3039 ;
3040
3041%rule op_asgn(rhs) <node>
3042 : var_lhs tOP_ASGN lex_ctxt rhs
3043 {
3044 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3045 /*% ripper: opassign!($:1, $:2, $:4) %*/
3046 }
3047 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3048 {
3049 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3050 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3051 }
3052 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3053 {
3054 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3055 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3056 }
3057 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3058 {
3059 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3060 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3061 }
3062 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3063 {
3064 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3065 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3066 }
3067 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3068 {
3069 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3070 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3071 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3072 }
3073 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
3074 {
3075 YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3076 $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3077 /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
3078 }
3079 | backref tOP_ASGN lex_ctxt rhs
3080 {
3081 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3082 $$ = NEW_ERROR(&@$);
3083 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3084 }
3085 ;
3086
3087%rule opt_args_tail(tail) <node_args>
3088 : ',' tail
3089 {
3090 $$ = $tail;
3091 /*% ripper: $:2 %*/
3092 }
3093 | /* none */
3094 {
3095 $$ = new_args_tail(p, 0, 0, 0, &@0);
3096 /*% ripper: [Qnil, Qnil, Qnil] %*/
3097 }
3098 ;
3099
3100%rule range_expr(range) <node>
3101 : range tDOT2 range
3102 {
3103 value_expr($1);
3104 value_expr($3);
3105 $$ = NEW_DOT2($1, $3, &@$, &@2);
3106 /*% ripper: dot2!($:1, $:3) %*/
3107 }
3108 | range tDOT3 range
3109 {
3110 value_expr($1);
3111 value_expr($3);
3112 $$ = NEW_DOT3($1, $3, &@$, &@2);
3113 /*% ripper: dot3!($:1, $:3) %*/
3114 }
3115 | range tDOT2
3116 {
3117 value_expr($1);
3118 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3119 /*% ripper: dot2!($:1, Qnil) %*/
3120 }
3121 | range tDOT3
3122 {
3123 value_expr($1);
3124 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3125 /*% ripper: dot3!($:1, Qnil) %*/
3126 }
3127 | tBDOT2 range
3128 {
3129 value_expr($2);
3130 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3131 /*% ripper: dot2!(Qnil, $:2) %*/
3132 }
3133 | tBDOT3 range
3134 {
3135 value_expr($2);
3136 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3137 /*% ripper: dot3!(Qnil, $:2) %*/
3138 }
3139 ;
3140
3141%rule value_expr(value) <node>
3142 : value
3143 {
3144 value_expr($1);
3145 $$ = $1;
3146 }
3147 ;
3148
3149%rule words(begin, word_list) <node>
3150 : begin ' '+ word_list tSTRING_END
3151 {
3152 $$ = make_list($word_list, &@$);
3153 /*% ripper: array!($:3) %*/
3154 }
3155 ;
3156
3157%%
3158program : {
3159 SET_LEX_STATE(EXPR_BEG);
3160 local_push(p, ifndef_ripper(1)+0);
3161 /* jumps are possible in the top-level loop. */
3162 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3163 }
3164 compstmt(top_stmts)
3165 {
3166 if ($2 && !compile_for_eval) {
3167 NODE *node = $2;
3168 /* last expression should not be void */
3169 if (nd_type_p(node, NODE_BLOCK)) {
3170 while (RNODE_BLOCK(node)->nd_next) {
3171 node = RNODE_BLOCK(node)->nd_next;
3172 }
3173 node = RNODE_BLOCK(node)->nd_head;
3174 }
3175 node = remove_begin(node);
3176 void_expr(p, node);
3177 }
3178 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
3179 /*% ripper[final]: program!($:2) %*/
3180 local_pop(p);
3181 }
3182 ;
3183
3184top_stmts : none
3185 {
3186 $$ = NEW_BEGIN(0, &@$);
3187 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3188 }
3189 | top_stmt
3190 {
3191 $$ = newline_node($1);
3192 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3193 }
3194 | top_stmts terms top_stmt
3195 {
3196 $$ = block_append(p, $1, newline_node($3));
3197 /*% ripper: stmts_add!($:1, $:3) %*/
3198 }
3199 ;
3200
3201top_stmt : stmt
3202 {
3203 clear_block_exit(p, true);
3204 $$ = $1;
3205 }
3206 | keyword_BEGIN begin_block
3207 {
3208 $$ = $2;
3209 /*% ripper: $:2 %*/
3210 }
3211 ;
3212
3213block_open : '{' {$$ = init_block_exit(p);};
3214
3215begin_block : block_open compstmt(top_stmts) '}'
3216 {
3217 restore_block_exit(p, $block_open);
3218 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3219 NEW_BEGIN($2, &@$));
3220 $$ = NEW_BEGIN(0, &@$);
3221 /*% ripper: BEGIN!($:2) %*/
3222 }
3223 ;
3224
3225bodystmt : compstmt(stmts)[body]
3226 lex_ctxt[ctxt]
3227 opt_rescue
3228 k_else
3229 {
3230 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3231 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3232 }
3233 compstmt(stmts)[elsebody]
3234 {
3235 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3236 }
3237 opt_ensure
3238 {
3239 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3240 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3241 }
3242 | compstmt(stmts)[body]
3243 lex_ctxt[ctxt]
3244 opt_rescue
3245 {
3246 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3247 }
3248 opt_ensure
3249 {
3250 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3251 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3252 }
3253 ;
3254
3255stmts : none
3256 {
3257 $$ = NEW_BEGIN(0, &@$);
3258 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3259 }
3260 | stmt_or_begin
3261 {
3262 $$ = newline_node($1);
3263 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3264 }
3265 | stmts terms stmt_or_begin
3266 {
3267 $$ = block_append(p, $1, newline_node($3));
3268 /*% ripper: stmts_add!($:1, $:3) %*/
3269 }
3270 ;
3271
3272stmt_or_begin : stmt
3273 | keyword_BEGIN
3274 {
3275 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3276 }
3277 begin_block
3278 {
3279 $$ = $3;
3280 }
3281 ;
3282
3283allow_exits : {$$ = allow_block_exit(p);};
3284
3285k_END : keyword_END lex_ctxt
3286 {
3287 $$ = $2;
3288 p->ctxt.in_rescue = before_rescue;
3289 /*% ripper: $:2 %*/
3290 };
3291
3292stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3293 {
3294 $$ = NEW_ALIAS($2, $4, &@$, &@1);
3295 /*% ripper: alias!($:2, $:4) %*/
3296 }
3297 | keyword_alias tGVAR tGVAR
3298 {
3299 $$ = NEW_VALIAS($2, $3, &@$, &@1);
3300 /*% ripper: var_alias!($:2, $:3) %*/
3301 }
3302 | keyword_alias tGVAR tBACK_REF
3303 {
3304 char buf[2];
3305 buf[0] = '$';
3306 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
3307 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$, &@1);
3308 /*% ripper: var_alias!($:2, $:3) %*/
3309 }
3310 | keyword_alias tGVAR tNTH_REF
3311 {
3312 static const char mesg[] = "can't make alias for the number variables";
3313 /*%%%*/
3314 yyerror1(&@3, mesg);
3315 /*% %*/
3316 $$ = NEW_ERROR(&@$);
3317 /*% ripper[error]: alias_error!(ERR_MESG(), $:3) %*/
3318 }
3319 | keyword_undef undef_list
3320 {
3321 nd_set_first_loc($2, @1.beg_pos);
3322 RNODE_UNDEF($2)->keyword_loc = @1;
3323 $$ = $2;
3324 /*% ripper: undef!($:2) %*/
3325 }
3326 | stmt modifier_if expr_value
3327 {
3328 $$ = new_if(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3329 fixpos($$, $3);
3330 /*% ripper: if_mod!($:3, $:1) %*/
3331 }
3332 | stmt modifier_unless expr_value
3333 {
3334 $$ = new_unless(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3335 fixpos($$, $3);
3336 /*% ripper: unless_mod!($:3, $:1) %*/
3337 }
3338 | stmt modifier_while expr_value
3339 {
3340 clear_block_exit(p, false);
3341 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3342 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3343 }
3344 else {
3345 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3346 }
3347 /*% ripper: while_mod!($:3, $:1) %*/
3348 }
3349 | stmt modifier_until expr_value
3350 {
3351 clear_block_exit(p, false);
3352 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3353 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3354 }
3355 else {
3356 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3357 }
3358 /*% ripper: until_mod!($:3, $:1) %*/
3359 }
3360 | stmt modifier_rescue after_rescue stmt
3361 {
3362 p->ctxt.in_rescue = $3.in_rescue;
3363 NODE *resq;
3364 YYLTYPE loc = code_loc_gen(&@2, &@4);
3365 resq = NEW_RESBODY(0, 0, remove_begin($4), 0, &loc);
3366 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
3367 /*% ripper: rescue_mod!($:1, $:4) %*/
3368 }
3369 | k_END allow_exits '{' compstmt(stmts) '}'
3370 {
3371 if (p->ctxt.in_def) {
3372 rb_warn0("END in method; use at_exit");
3373 }
3374 restore_block_exit(p, $allow_exits);
3375 p->ctxt = $k_END;
3376 {
3377 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, &@$);
3378 $$ = NEW_POSTEXE(scope, &@$, &@1, &@3, &@5);
3379 }
3380 /*% ripper: END!($:compstmt) %*/
3381 }
3382 | command_asgn
3383 | mlhs '=' lex_ctxt command_call_value
3384 {
3385 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3386 /*% ripper: massign!($:1, $:4) %*/
3387 }
3388 | asgn(mrhs)
3389 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
3390 after_rescue stmt[resbody]
3391 {
3392 p->ctxt.in_rescue = $3.in_rescue;
3393 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3394 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3395 loc.beg_pos = @mrhs_arg.beg_pos;
3396 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3397 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
3398 /*% ripper: massign!($:1, rescue_mod!($:4, $:7)) %*/
3399 }
3400 | mlhs '=' lex_ctxt mrhs_arg
3401 {
3402 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3403 /*% ripper: massign!($:1, $:4) %*/
3404 }
3405 | expr
3406 | error
3407 {
3408 (void)yynerrs;
3409 $$ = NEW_ERROR(&@$);
3410 }
3411 ;
3412
3413command_asgn : asgn(command_rhs)
3414 | op_asgn(command_rhs)
3415 | def_endless_method(endless_command)
3416 ;
3417
3418endless_command : command
3419 | endless_command modifier_rescue after_rescue arg
3420 {
3421 p->ctxt.in_rescue = $3.in_rescue;
3422 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3423 /*% ripper: rescue_mod!($:1, $:4) %*/
3424 }
3425 | keyword_not '\n'? endless_command
3426 {
3427 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3428 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3429 }
3430 ;
3431
3432command_rhs : command_call_value %prec tOP_ASGN
3433 | command_call_value modifier_rescue after_rescue stmt
3434 {
3435 p->ctxt.in_rescue = $3.in_rescue;
3436 YYLTYPE loc = code_loc_gen(&@2, &@4);
3437 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3438 /*% ripper: rescue_mod!($:1, $:4) %*/
3439 }
3440 | command_asgn
3441 ;
3442
3443expr : command_call
3444 | expr keyword_and expr
3445 {
3446 $$ = logop(p, idAND, $1, $3, &@2, &@$);
3447 /*% ripper: binary!($:1, ID2VAL(idAND), $:3) %*/
3448 }
3449 | expr keyword_or expr
3450 {
3451 $$ = logop(p, idOR, $1, $3, &@2, &@$);
3452 /*% ripper: binary!($:1, ID2VAL(idOR), $:3) %*/
3453 }
3454 | keyword_not '\n'? expr
3455 {
3456 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3457 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3458 }
3459 | '!' command_call
3460 {
3461 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3462 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
3463 }
3464 | arg tASSOC
3465 {
3466 value_expr($arg);
3467 }
3468 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3469 p_top_expr_body[body]
3470 {
3471 pop_pktbl(p, $p_pktbl);
3472 pop_pvtbl(p, $p_pvtbl);
3473 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3474 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body), &@$, &NULL_LOC, &NULL_LOC);
3475 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3476 }
3477 | arg keyword_in
3478 {
3479 value_expr($arg);
3480 }
3481 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3482 p_top_expr_body[body]
3483 {
3484 pop_pktbl(p, $p_pktbl);
3485 pop_pvtbl(p, $p_pvtbl);
3486 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3487 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body), &@$, &NULL_LOC, &NULL_LOC);
3488 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3489 }
3490 | arg %prec tLBRACE_ARG
3491 ;
3492
3493def_name : fname
3494 {
3495 numparam_name(p, $fname);
3496 local_push(p, 0);
3497 p->ctxt.in_def = 1;
3498 p->ctxt.in_rescue = before_rescue;
3499 p->ctxt.cant_return = 0;
3500 $$ = $fname;
3501 }
3502 ;
3503
3504defn_head : k_def def_name
3505 {
3506 $$ = def_head_save(p, $k_def);
3507 $$->nd_mid = $def_name;
3508 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3509 /*% ripper: $:def_name %*/
3510 }
3511 ;
3512
3513defs_head : k_def singleton dot_or_colon
3514 {
3515 SET_LEX_STATE(EXPR_FNAME);
3516 }
3517 def_name
3518 {
3519 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3520 $$ = def_head_save(p, $k_def);
3521 $$->nd_mid = $def_name;
3522 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3523 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3524 }
3525 ;
3526
3527expr_value : value_expr(expr)
3528 | error
3529 {
3530 $$ = NEW_ERROR(&@$);
3531 }
3532 ;
3533
3534expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3535 {
3536 $$ = $2;
3537 /*% ripper: $:2 %*/
3538 }
3539 ;
3540
3541command_call : command
3542 | block_command
3543 ;
3544
3545command_call_value : value_expr(command_call)
3546 ;
3547
3548block_command : block_call
3549 | block_call call_op2 operation2 command_args
3550 {
3551 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3552 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3553 }
3554 ;
3555
3556cmd_brace_block : tLBRACE_ARG brace_body '}'
3557 {
3558 $$ = $2;
3559 set_embraced_location($$, &@1, &@3);
3560 /*% ripper: $:2 %*/
3561 }
3562 ;
3563
3564fcall : operation
3565 {
3566 $$ = NEW_FCALL($1, 0, &@$);
3567 /*% ripper: $:1 %*/
3568 }
3569 ;
3570
3571command : fcall command_args %prec tLOWEST
3572 {
3573 $1->nd_args = $2;
3574 nd_set_last_loc($1, @2.end_pos);
3575 $$ = (NODE *)$1;
3576 /*% ripper: command!($:1, $:2) %*/
3577 }
3578 | fcall command_args cmd_brace_block
3579 {
3580 block_dup_check(p, $2, $3);
3581 $1->nd_args = $2;
3582 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3583 fixpos($$, RNODE($1));
3584 nd_set_last_loc($1, @2.end_pos);
3585 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3586 }
3587 | primary_value call_op operation2 command_args %prec tLOWEST
3588 {
3589 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3590 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3591 }
3592 | primary_value call_op operation2 command_args cmd_brace_block
3593 {
3594 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3595 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3596 }
3597 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3598 {
3599 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3600 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3601 }
3602 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3603 {
3604 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3605 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3606 }
3607 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3608 {
3609 set_embraced_location($5, &@4, &@6);
3610 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3611 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3612 }
3613 | keyword_super command_args
3614 {
3615 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3616 fixpos($$, $2);
3617 /*% ripper: super!($:2) %*/
3618 }
3619 | k_yield command_args
3620 {
3621 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3622 fixpos($$, $2);
3623 /*% ripper: yield!($:2) %*/
3624 }
3625 | k_return call_args
3626 {
3627 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3628 /*% ripper: return!($:2) %*/
3629 }
3630 | keyword_break call_args
3631 {
3632 NODE *args = 0;
3633 args = ret_args(p, $2);
3634 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3635 /*% ripper: break!($:2) %*/
3636 }
3637 | keyword_next call_args
3638 {
3639 NODE *args = 0;
3640 args = ret_args(p, $2);
3641 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3642 /*% ripper: next!($:2) %*/
3643 }
3644 ;
3645
3646mlhs : mlhs_basic
3647 | tLPAREN mlhs_inner rparen
3648 {
3649 $$ = $2;
3650 /*% ripper: mlhs_paren!($:2) %*/
3651 }
3652 ;
3653
3654mlhs_inner : mlhs_basic
3655 | tLPAREN mlhs_inner rparen
3656 {
3657 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3658 /*% ripper: mlhs_paren!($:2) %*/
3659 }
3660 ;
3661
3662mlhs_basic : mlhs_head
3663 {
3664 $$ = NEW_MASGN($1, 0, &@$);
3665 /*% ripper: $:1 %*/
3666 }
3667 | mlhs_head mlhs_item
3668 {
3669 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3670 /*% ripper: mlhs_add!($:1, $:2) %*/
3671 }
3672 | mlhs_head tSTAR mlhs_node
3673 {
3674 $$ = NEW_MASGN($1, $3, &@$);
3675 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3676 }
3677 | mlhs_head tSTAR mlhs_node ',' mlhs(mlhs_item)
3678 {
3679 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3680 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3681 }
3682 | mlhs_head tSTAR
3683 {
3684 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3685 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3686 }
3687 | mlhs_head tSTAR ',' mlhs(mlhs_item)
3688 {
3689 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3690 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3691 }
3692 | tSTAR mlhs_node
3693 {
3694 $$ = NEW_MASGN(0, $2, &@$);
3695 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3696 }
3697 | tSTAR mlhs_node ',' mlhs(mlhs_item)
3698 {
3699 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3700 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3701 }
3702 | tSTAR
3703 {
3704 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3705 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3706 }
3707 | tSTAR ',' mlhs(mlhs_item)
3708 {
3709 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3710 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3711 }
3712 ;
3713
3714mlhs_item : mlhs_node
3715 | tLPAREN mlhs_inner rparen
3716 {
3717 $$ = (NODE *)$2;
3718 /*% ripper: mlhs_paren!($:2) %*/
3719 }
3720 ;
3721
3722mlhs_head : mlhs_item ','
3723 {
3724 $$ = NEW_LIST($1, &@1);
3725 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3726 }
3727 | mlhs_head mlhs_item ','
3728 {
3729 $$ = list_append(p, $1, $2);
3730 /*% ripper: mlhs_add!($:1, $:2) %*/
3731 }
3732 ;
3733
3734
3735mlhs_node : user_or_keyword_variable
3736 {
3737 /*% ripper: var_field!($:1) %*/
3738 $$ = assignable(p, $1, 0, &@$);
3739 }
3740 | primary_value '[' opt_call_args rbracket
3741 {
3742 $$ = aryset(p, $1, $3, &@$);
3743 /*% ripper: aref_field!($:1, $:3) %*/
3744 }
3745 | primary_value call_op ident_or_const
3746 {
3747 anddot_multiple_assignment_check(p, &@2, $2);
3748 $$ = attrset(p, $1, $2, $3, &@$);
3749 /*% ripper: field!($:1, $:2, $:3) %*/
3750 }
3751 | primary_value tCOLON2 tIDENTIFIER
3752 {
3753 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3754 /*% ripper: const_path_field!($:1, $:3) %*/
3755 }
3756 | primary_value tCOLON2 tCONSTANT
3757 {
3758 /*% ripper: const_path_field!($:1, $:3) %*/
3759 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3760 }
3761 | tCOLON3 tCONSTANT
3762 {
3763 /*% ripper: top_const_field!($:2) %*/
3764 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3765 }
3766 | backref
3767 {
3768 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3769 $$ = NEW_ERROR(&@$);
3770 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3771 }
3772 ;
3773
3774lhs : user_or_keyword_variable
3775 {
3776 /*% ripper: var_field!($:1) %*/
3777 $$ = assignable(p, $1, 0, &@$);
3778 }
3779 | primary_value '[' opt_call_args rbracket
3780 {
3781 $$ = aryset(p, $1, $3, &@$);
3782 /*% ripper: aref_field!($:1, $:3) %*/
3783 }
3784 | primary_value call_op ident_or_const
3785 {
3786 $$ = attrset(p, $1, $2, $3, &@$);
3787 /*% ripper: field!($:1, $:2, $:3) %*/
3788 }
3789 | primary_value tCOLON2 tIDENTIFIER
3790 {
3791 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3792 /*% ripper: field!($:1, $:2, $:3) %*/
3793 }
3794 | primary_value tCOLON2 tCONSTANT
3795 {
3796 /*% ripper: const_path_field!($:1, $:3) %*/
3797 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3798 }
3799 | tCOLON3 tCONSTANT
3800 {
3801 /*% ripper: top_const_field!($:2) %*/
3802 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3803 }
3804 | backref
3805 {
3806 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3807 $$ = NEW_ERROR(&@$);
3808 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3809 }
3810 ;
3811
3812cname : tIDENTIFIER
3813 {
3814 static const char mesg[] = "class/module name must be CONSTANT";
3815 /*%%%*/
3816 yyerror1(&@1, mesg);
3817 /*% %*/
3818 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3819 }
3820 | tCONSTANT
3821 ;
3822
3823cpath : tCOLON3 cname
3824 {
3825 $$ = NEW_COLON3($2, &@$, &@1, &@2);
3826 /*% ripper: top_const_ref!($:2) %*/
3827 }
3828 | cname
3829 {
3830 $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
3831 /*% ripper: const_ref!($:1) %*/
3832 }
3833 | primary_value tCOLON2 cname
3834 {
3835 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
3836 /*% ripper: const_path_ref!($:1, $:3) %*/
3837 }
3838 ;
3839
3840fname : operation
3841 | op
3842 {
3843 SET_LEX_STATE(EXPR_ENDFN);
3844 $$ = $1;
3845 }
3846 | reswords
3847 ;
3848
3849fitem : fname
3850 {
3851 $$ = NEW_SYM(rb_id2str($1), &@$);
3852 /*% ripper: symbol_literal!($:1) %*/
3853 }
3854 | symbol
3855 ;
3856
3857undef_list : fitem
3858 {
3859 $$ = NEW_UNDEF($1, &@$);
3860 /*% ripper: rb_ary_new3(1, $:1) %*/
3861 }
3862 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3863 {
3864 nd_set_last_loc($1, @4.end_pos);
3865 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3866 /*% ripper: rb_ary_push($:1, $:4) %*/
3867 }
3868 ;
3869
3870op : '|' { $$ = '|'; }
3871 | '^' { $$ = '^'; }
3872 | '&' { $$ = '&'; }
3873 | tCMP { $$ = tCMP; }
3874 | tEQ { $$ = tEQ; }
3875 | tEQQ { $$ = tEQQ; }
3876 | tMATCH { $$ = tMATCH; }
3877 | tNMATCH { $$ = tNMATCH; }
3878 | '>' { $$ = '>'; }
3879 | tGEQ { $$ = tGEQ; }
3880 | '<' { $$ = '<'; }
3881 | tLEQ { $$ = tLEQ; }
3882 | tNEQ { $$ = tNEQ; }
3883 | tLSHFT { $$ = tLSHFT; }
3884 | tRSHFT { $$ = tRSHFT; }
3885 | '+' { $$ = '+'; }
3886 | '-' { $$ = '-'; }
3887 | '*' { $$ = '*'; }
3888 | tSTAR { $$ = '*'; }
3889 | '/' { $$ = '/'; }
3890 | '%' { $$ = '%'; }
3891 | tPOW { $$ = tPOW; }
3892 | tDSTAR { $$ = tDSTAR; }
3893 | '!' { $$ = '!'; }
3894 | '~' { $$ = '~'; }
3895 | tUPLUS { $$ = tUPLUS; }
3896 | tUMINUS { $$ = tUMINUS; }
3897 | tAREF { $$ = tAREF; }
3898 | tASET { $$ = tASET; }
3899 | '`' { $$ = '`'; }
3900 ;
3901
3902reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3903 | keyword_BEGIN | keyword_END
3904 | keyword_alias | keyword_and | keyword_begin
3905 | keyword_break | keyword_case | keyword_class | keyword_def
3906 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3907 | keyword_end | keyword_ensure | keyword_false
3908 | keyword_for | keyword_in | keyword_module | keyword_next
3909 | keyword_nil | keyword_not | keyword_or | keyword_redo
3910 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3911 | keyword_super | keyword_then | keyword_true | keyword_undef
3912 | keyword_when | keyword_yield | keyword_if | keyword_unless
3913 | keyword_while | keyword_until
3914 ;
3915
3916arg : asgn(arg_rhs)
3917 | op_asgn(arg_rhs)
3918 | range_expr(arg)
3919 | arg '+' arg
3920 {
3921 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3922 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3923 }
3924 | arg '-' arg
3925 {
3926 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3927 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3928 }
3929 | arg '*' arg
3930 {
3931 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3932 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3933 }
3934 | arg '/' arg
3935 {
3936 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3937 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3938 }
3939 | arg '%' arg
3940 {
3941 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3942 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3943 }
3944 | arg tPOW arg
3945 {
3946 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3947 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3948 }
3949 | tUMINUS_NUM simple_numeric tPOW arg
3950 {
3951 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3952 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3953 }
3954 | tUPLUS arg
3955 {
3956 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3957 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3958 }
3959 | tUMINUS arg
3960 {
3961 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3962 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3963 }
3964 | arg '|' arg
3965 {
3966 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3967 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3968 }
3969 | arg '^' arg
3970 {
3971 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3972 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3973 }
3974 | arg '&' arg
3975 {
3976 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3977 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
3978 }
3979 | arg tCMP arg
3980 {
3981 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3982 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
3983 }
3984 | rel_expr %prec tCMP
3985 | arg tEQ arg
3986 {
3987 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3988 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
3989 }
3990 | arg tEQQ arg
3991 {
3992 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
3993 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
3994 }
3995 | arg tNEQ arg
3996 {
3997 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
3998 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
3999 }
4000 | arg tMATCH arg
4001 {
4002 $$ = match_op(p, $1, $3, &@2, &@$);
4003 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4004 }
4005 | arg tNMATCH arg
4006 {
4007 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4008 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4009 }
4010 | '!' arg
4011 {
4012 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4013 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4014 }
4015 | '~' arg
4016 {
4017 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4018 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4019 }
4020 | arg tLSHFT arg
4021 {
4022 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4023 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4024 }
4025 | arg tRSHFT arg
4026 {
4027 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4028 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4029 }
4030 | arg tANDOP arg
4031 {
4032 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4033 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4034 }
4035 | arg tOROP arg
4036 {
4037 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4038 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4039 }
4040 | keyword_defined '\n'? begin_defined arg
4041 {
4042 p->ctxt.in_defined = $3.in_defined;
4043 $$ = new_defined(p, $4, &@$);
4044 /*% ripper: defined!($:4) %*/
4045 }
4046 | def_endless_method(endless_arg)
4047 | ternary
4048 | primary
4049 ;
4050
4051ternary : arg '?' arg '\n'? ':' arg
4052 {
4053 value_expr($1);
4054 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4055 fixpos($$, $1);
4056 /*% ripper: ifop!($:1, $:3, $:6) %*/
4057 }
4058 ;
4059
4060endless_arg : arg %prec modifier_rescue
4061 | endless_arg modifier_rescue after_rescue arg
4062 {
4063 p->ctxt.in_rescue = $3.in_rescue;
4064 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4065 /*% ripper: rescue_mod!($:1, $:4) %*/
4066 }
4067 | keyword_not '\n'? endless_arg
4068 {
4069 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4070 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4071 }
4072 ;
4073
4074relop : '>' {$$ = '>';}
4075 | '<' {$$ = '<';}
4076 | tGEQ {$$ = idGE;}
4077 | tLEQ {$$ = idLE;}
4078 ;
4079
4080rel_expr : arg relop arg %prec '>'
4081 {
4082 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4083 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4084 }
4085 | rel_expr relop arg %prec '>'
4086 {
4087 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4088 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4089 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4090 }
4091 ;
4092
4093lex_ctxt : none
4094 {
4095 $$ = p->ctxt;
4096 }
4097 ;
4098
4099begin_defined : lex_ctxt
4100 {
4101 p->ctxt.in_defined = 1;
4102 $$ = $1;
4103 }
4104 ;
4105
4106after_rescue : lex_ctxt
4107 {
4108 p->ctxt.in_rescue = after_rescue;
4109 $$ = $1;
4110 }
4111 ;
4112
4113arg_value : value_expr(arg)
4114 ;
4115
4116aref_args : none
4117 | args trailer
4118 | args ',' assocs trailer
4119 {
4120 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4121 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4122 }
4123 | assocs trailer
4124 {
4125 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4126 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4127 }
4128 ;
4129
4130arg_rhs : arg %prec tOP_ASGN
4131 {
4132 value_expr($1);
4133 $$ = $1;
4134 }
4135 | arg modifier_rescue after_rescue arg
4136 {
4137 p->ctxt.in_rescue = $3.in_rescue;
4138 value_expr($1);
4139 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4140 /*% ripper: rescue_mod!($:1, $:4) %*/
4141 }
4142 ;
4143
4144paren_args : '(' opt_call_args rparen
4145 {
4146 $$ = $2;
4147 /*% ripper: arg_paren!($:2) %*/
4148 }
4149 | '(' args ',' args_forward rparen
4150 {
4151 if (!check_forwarding_args(p)) {
4152 $$ = 0;
4153 }
4154 else {
4155 $$ = new_args_forward_call(p, $2, &@4, &@$);
4156 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4157 }
4158 }
4159 | '(' args_forward rparen
4160 {
4161 if (!check_forwarding_args(p)) {
4162 $$ = 0;
4163 }
4164 else {
4165 $$ = new_args_forward_call(p, 0, &@2, &@$);
4166 /*% ripper: arg_paren!($:2) %*/
4167 }
4168 }
4169 ;
4170
4171opt_paren_args : none
4172 | paren_args
4173 {
4174 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4175 }
4176 ;
4177
4178opt_call_args : none
4179 | call_args
4180 | args ','
4181 | args ',' assocs ','
4182 {
4183 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4184 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4185 }
4186 | assocs ','
4187 {
4188 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4189 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4190 }
4191 ;
4192
4193call_args : value_expr(command)
4194 {
4195 $$ = NEW_LIST($1, &@$);
4196 /*% ripper: args_add!(args_new!, $:1) %*/
4197 }
4198 | args opt_block_arg
4199 {
4200 $$ = arg_blk_pass($1, $2);
4201 /*% ripper: args_add_block!($:1, $:2) %*/
4202 }
4203 | assocs opt_block_arg
4204 {
4205 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4206 $$ = arg_blk_pass($$, $2);
4207 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4208 }
4209 | args ',' assocs opt_block_arg
4210 {
4211 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4212 $$ = arg_blk_pass($$, $4);
4213 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4214 }
4215 | block_arg
4216 /*% ripper: args_add_block!(args_new!, $:1) %*/
4217 ;
4218
4219command_args : {
4220 /* If call_args starts with a open paren '(' or '[',
4221 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4222 * but the push must be done after CMDARG_PUSH(1).
4223 * So this code makes them consistent by first cancelling
4224 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4225 * and finally redoing CMDARG_PUSH(0).
4226 */
4227 int lookahead = 0;
4228 switch (yychar) {
4229 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4230 lookahead = 1;
4231 }
4232 if (lookahead) CMDARG_POP();
4233 CMDARG_PUSH(1);
4234 if (lookahead) CMDARG_PUSH(0);
4235 }
4236 call_args
4237 {
4238 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4239 * but the push must be done after CMDARG_POP() in the parser.
4240 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4241 * CMDARG_POP() to pop 1 pushed by command_args,
4242 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4243 */
4244 int lookahead = 0;
4245 switch (yychar) {
4246 case tLBRACE_ARG:
4247 lookahead = 1;
4248 }
4249 if (lookahead) CMDARG_POP();
4250 CMDARG_POP();
4251 if (lookahead) CMDARG_PUSH(0);
4252 $$ = $2;
4253 /*% ripper: $:2 %*/
4254 }
4255 ;
4256
4257block_arg : tAMPER arg_value
4258 {
4259 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4260 /*% ripper: $:2 %*/
4261 }
4262 | tAMPER
4263 {
4264 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4265 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4266 /*% ripper: Qnil %*/
4267 }
4268 ;
4269
4270opt_block_arg : ',' block_arg
4271 {
4272 $$ = $2;
4273 /*% ripper: $:2 %*/
4274 }
4275 | none
4276 {
4277 $$ = 0;
4278 /*% ripper: Qfalse %*/
4279 }
4280 ;
4281
4282/* value */
4283args : arg_value
4284 {
4285 $$ = NEW_LIST($arg_value, &@$);
4286 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4287 }
4288 | arg_splat
4289 {
4290 $$ = $arg_splat;
4291 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4292 }
4293 | args[non_last_args] ',' arg_value
4294 {
4295 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4296 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4297 }
4298 | args[non_last_args] ',' arg_splat
4299 {
4300 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4301 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4302 }
4303 ;
4304
4305/* value */
4306arg_splat : tSTAR arg_value
4307 {
4308 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4309 /*% ripper: $:arg_value %*/
4310 }
4311 | tSTAR /* none */
4312 {
4313 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4314 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4315 /*% ripper: Qnil %*/
4316 }
4317 ;
4318
4319/* value */
4320mrhs_arg : mrhs
4321 | arg_value
4322 ;
4323
4324/* value */
4325mrhs : args ',' arg_value
4326 {
4327 $$ = last_arg_append(p, $args, $arg_value, &@$);
4328 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4329 }
4330 | args ',' tSTAR arg_value
4331 {
4332 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4333 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4334 }
4335 | tSTAR arg_value
4336 {
4337 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4338 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4339 }
4340 ;
4341
4342%rule %inline inline_primary
4343 : literal
4344 | strings
4345 | xstring
4346 | regexp
4347 | words
4348 | qwords
4349 | symbols
4350 | qsymbols
4351 ;
4352
4353primary : inline_primary
4354 | var_ref
4355 | backref
4356 | tFID
4357 {
4358 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
4359 /*% ripper: method_add_arg!(fcall!($:1), args_new!) %*/
4360 }
4361 | k_begin
4362 {
4363 CMDARG_PUSH(0);
4364 }
4365 bodystmt
4366 k_end
4367 {
4368 CMDARG_POP();
4369 set_line_body($3, @1.end_pos.lineno);
4370 $$ = NEW_BEGIN($3, &@$);
4371 nd_set_line($$, @1.end_pos.lineno);
4372 /*% ripper: begin!($:3) %*/
4373 }
4374 | tLPAREN_ARG compstmt(stmts) {SET_LEX_STATE(EXPR_ENDARG);} ')'
4375 {
4376 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4377 $$ = $2;
4378 /*% ripper: paren!($:2) %*/
4379 }
4380 | tLPAREN compstmt(stmts) ')'
4381 {
4382 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4383 $$ = NEW_BLOCK($2, &@$);
4384 /*% ripper: paren!($:2) %*/
4385 }
4386 | primary_value tCOLON2 tCONSTANT
4387 {
4388 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
4389 /*% ripper: const_path_ref!($:1, $:3) %*/
4390 }
4391 | tCOLON3 tCONSTANT
4392 {
4393 $$ = NEW_COLON3($2, &@$, &@1, &@2);
4394 /*% ripper: top_const_ref!($:2) %*/
4395 }
4396 | tLBRACK aref_args ']'
4397 {
4398 $$ = make_list($2, &@$);
4399 /*% ripper: array!($:2) %*/
4400 }
4401 | tLBRACE assoc_list '}'
4402 {
4403 $$ = new_hash(p, $2, &@$);
4404 RNODE_HASH($$)->nd_brace = TRUE;
4405 /*% ripper: hash!($:2) %*/
4406 }
4407 | k_return
4408 {
4409 $$ = NEW_RETURN(0, &@$, &@1);
4410 /*% ripper: return0! %*/
4411 }
4412 | k_yield '(' call_args rparen
4413 {
4414 $$ = NEW_YIELD($3, &@$, &@1, &@2, &@4);
4415 /*% ripper: yield!(paren!($:3)) %*/
4416 }
4417 | k_yield '(' rparen
4418 {
4419 $$ = NEW_YIELD(0, &@$, &@1, &@2, &@3);
4420 /*% ripper: yield!(paren!(args_new!)) %*/
4421 }
4422 | k_yield
4423 {
4424 $$ = NEW_YIELD(0, &@$, &@1, &NULL_LOC, &NULL_LOC);
4425 /*% ripper: yield0! %*/
4426 }
4427 | keyword_defined '\n'? '(' begin_defined expr rparen
4428 {
4429 p->ctxt.in_defined = $4.in_defined;
4430 $$ = new_defined(p, $5, &@$);
4431 /*% ripper: defined!($:5) %*/
4432 }
4433 | keyword_not '(' expr rparen
4434 {
4435 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4436 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4437 }
4438 | keyword_not '(' rparen
4439 {
4440 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
4441 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4442 }
4443 | fcall brace_block
4444 {
4445 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
4446 /*% ripper: method_add_block!(method_add_arg!(fcall!($:1), args_new!), $:2) %*/
4447 }
4448 | method_call
4449 | method_call brace_block
4450 {
4451 block_dup_check(p, get_nd_args(p, $1), $2);
4452 $$ = method_add_block(p, $1, $2, &@$);
4453 /*% ripper: method_add_block!($:1, $:2) %*/
4454 }
4455 | lambda
4456 | k_if expr_value then
4457 compstmt(stmts)
4458 if_tail
4459 k_end
4460 {
4461 if ($5 && nd_type_p($5, NODE_IF))
4462 RNODE_IF($5)->end_keyword_loc = @6;
4463
4464 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4465 fixpos($$, $2);
4466 /*% ripper: if!($:2, $:4, $:5) %*/
4467 }
4468 | k_unless expr_value then
4469 compstmt(stmts)
4470 opt_else
4471 k_end
4472 {
4473 $$ = new_unless(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4474 fixpos($$, $2);
4475 /*% ripper: unless!($:2, $:4, $:5) %*/
4476 }
4477 | k_while expr_value_do
4478 compstmt(stmts)
4479 k_end
4480 {
4481 restore_block_exit(p, $1);
4482 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4483 fixpos($$, $2);
4484 /*% ripper: while!($:2, $:3) %*/
4485 }
4486 | k_until expr_value_do
4487 compstmt(stmts)
4488 k_end
4489 {
4490 restore_block_exit(p, $1);
4491 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4492 fixpos($$, $2);
4493 /*% ripper: until!($:2, $:3) %*/
4494 }
4495 | k_case expr_value terms?
4496 {
4497 $$ = p->case_labels;
4498 p->case_labels = CHECK_LITERAL_WHEN;
4499 }<labels>
4500 case_body
4501 k_end
4502 {
4503 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4504 p->case_labels = $4;
4505 $$ = NEW_CASE($2, $5, &@$, &@1, &@6);
4506 fixpos($$, $2);
4507 /*% ripper: case!($:2, $:5) %*/
4508 }
4509 | k_case terms?
4510 {
4511 $$ = p->case_labels;
4512 p->case_labels = 0;
4513 }<labels>
4514 case_body
4515 k_end
4516 {
4517 if (p->case_labels) st_free_table(p->case_labels);
4518 p->case_labels = $3;
4519 $$ = NEW_CASE2($4, &@$, &@1, &@5);
4520 /*% ripper: case!(Qnil, $:4) %*/
4521 }
4522 | k_case expr_value terms?
4523 p_case_body
4524 k_end
4525 {
4526 $$ = NEW_CASE3($2, $4, &@$, &@1, &@5);
4527 /*% ripper: case!($:2, $:4) %*/
4528 }
4529 | k_for for_var keyword_in
4530 {COND_PUSH(1);} expr_value do {COND_POP();}
4531 compstmt(stmts)
4532 k_end
4533 {
4534 restore_block_exit(p, $k_for);
4535 /*
4536 * for a, b, c in e
4537 * #=>
4538 * e.each{|*x| a, b, c = x}
4539 *
4540 * for a in e
4541 * #=>
4542 * e.each{|x| a, = x}
4543 */
4544 ID id = internal_id(p);
4545 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4546 rb_node_args_t *args;
4547 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4548 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4549 tbl->ids[0] = id; /* internal id */
4550
4551 switch (nd_type($for_var)) {
4552 case NODE_LASGN:
4553 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4554 set_nd_value(p, $for_var, internal_var);
4555 id = 0;
4556 m->nd_plen = 1;
4557 m->nd_next = $for_var;
4558 break;
4559 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4560 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4561 break;
4562 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4563 m->nd_next = node_assign(p, (NODE *)NEW_MASGN(NEW_LIST($for_var, &@for_var), 0, &@for_var), internal_var, NO_LEX_CTXT, &@for_var);
4564 }
4565 /* {|*internal_id| <m> = internal_id; ... } */
4566 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@for_var), &@for_var);
4567 scope = NEW_SCOPE2(tbl, args, $compstmt, &@$);
4568 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4569 $$ = NEW_FOR($5, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4570 fixpos($$, $for_var);
4571 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4572 }
4573 | k_class cpath superclass
4574 {
4575 begin_definition("class", &@k_class, &@cpath);
4576 }
4577 bodystmt
4578 k_end
4579 {
4580 YYLTYPE inheritance_operator_loc = NULL_LOC;
4581 if ($superclass) {
4582 inheritance_operator_loc = @superclass;
4583 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
4584 }
4585 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
4586 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4587 set_line_body($bodystmt, @superclass.end_pos.lineno);
4588 nd_set_line($$, @superclass.end_pos.lineno);
4589 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4590 local_pop(p);
4591 p->ctxt.in_class = $k_class.in_class;
4592 p->ctxt.cant_return = $k_class.cant_return;
4593 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4594 }
4595 | k_class tLSHFT expr_value
4596 {
4597 begin_definition("", &@k_class, &@tLSHFT);
4598 }
4599 term
4600 bodystmt
4601 k_end
4602 {
4603 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$);
4604 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4605 set_line_body($bodystmt, nd_line($expr_value));
4606 fixpos($$, $expr_value);
4607 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4608 local_pop(p);
4609 p->ctxt.in_def = $k_class.in_def;
4610 p->ctxt.in_class = $k_class.in_class;
4611 p->ctxt.cant_return = $k_class.cant_return;
4612 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4613 }
4614 | k_module cpath
4615 {
4616 begin_definition("module", &@k_module, &@cpath);
4617 }
4618 bodystmt
4619 k_end
4620 {
4621 $$ = NEW_MODULE($cpath, $bodystmt, &@$);
4622 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4623 set_line_body($bodystmt, @cpath.end_pos.lineno);
4624 nd_set_line($$, @cpath.end_pos.lineno);
4625 /*% ripper: module!($:cpath, $:bodystmt) %*/
4626 local_pop(p);
4627 p->ctxt.in_class = $k_module.in_class;
4628 p->ctxt.cant_return = $k_module.cant_return;
4629 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4630 }
4631 | defn_head[head]
4632 f_arglist[args]
4633 {
4634 push_end_expect_token_locations(p, &@head.beg_pos);
4635 }
4636 bodystmt
4637 k_end
4638 {
4639 restore_defun(p, $head);
4640 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4641 ($$ = $head->nd_def)->nd_loc = @$;
4642 RNODE_DEFN($$)->nd_defn = $bodystmt;
4643 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4644 local_pop(p);
4645 }
4646 | defs_head[head]
4647 f_arglist[args]
4648 {
4649 push_end_expect_token_locations(p, &@head.beg_pos);
4650 }
4651 bodystmt
4652 k_end
4653 {
4654 restore_defun(p, $head);
4655 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4656 ($$ = $head->nd_def)->nd_loc = @$;
4657 RNODE_DEFS($$)->nd_defn = $bodystmt;
4658 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4659 local_pop(p);
4660 }
4661 | keyword_break
4662 {
4663 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@1));
4664 /*% ripper: break!(args_new!) %*/
4665 }
4666 | keyword_next
4667 {
4668 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@1));
4669 /*% ripper: next!(args_new!) %*/
4670 }
4671 | keyword_redo
4672 {
4673 $$ = add_block_exit(p, NEW_REDO(&@$, &@1));
4674 /*% ripper: redo! %*/
4675 }
4676 | keyword_retry
4677 {
4678 if (!p->ctxt.in_defined) {
4679 switch (p->ctxt.in_rescue) {
4680 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4681 case after_rescue: /* ok */ break;
4682 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4683 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4684 }
4685 }
4686 $$ = NEW_RETRY(&@$);
4687 /*% ripper: retry! %*/
4688 }
4689 ;
4690
4691primary_value : value_expr(primary)
4692 ;
4693
4694k_begin : keyword_begin
4695 {
4696 token_info_push(p, "begin", &@$);
4697 push_end_expect_token_locations(p, &@1.beg_pos);
4698 }
4699 ;
4700
4701k_if : keyword_if
4702 {
4703 WARN_EOL("if");
4704 token_info_push(p, "if", &@$);
4705 if (p->token_info && p->token_info->nonspc &&
4706 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4707 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4708 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4709 beg += rb_strlen_lit("else");
4710 while (beg < tok && ISSPACE(*beg)) beg++;
4711 if (beg == tok) {
4712 p->token_info->nonspc = 0;
4713 }
4714 }
4715 push_end_expect_token_locations(p, &@1.beg_pos);
4716 }
4717 ;
4718
4719k_unless : keyword_unless
4720 {
4721 token_info_push(p, "unless", &@$);
4722 push_end_expect_token_locations(p, &@1.beg_pos);
4723 }
4724 ;
4725
4726k_while : keyword_while allow_exits
4727 {
4728 $$ = $allow_exits;
4729 token_info_push(p, "while", &@$);
4730 push_end_expect_token_locations(p, &@1.beg_pos);
4731 }
4732 ;
4733
4734k_until : keyword_until allow_exits
4735 {
4736 $$ = $allow_exits;
4737 token_info_push(p, "until", &@$);
4738 push_end_expect_token_locations(p, &@1.beg_pos);
4739 }
4740 ;
4741
4742k_case : keyword_case
4743 {
4744 token_info_push(p, "case", &@$);
4745 push_end_expect_token_locations(p, &@1.beg_pos);
4746 }
4747 ;
4748
4749k_for : keyword_for allow_exits
4750 {
4751 $$ = $allow_exits;
4752 token_info_push(p, "for", &@$);
4753 push_end_expect_token_locations(p, &@1.beg_pos);
4754 }
4755 ;
4756
4757k_class : keyword_class
4758 {
4759 token_info_push(p, "class", &@$);
4760 $$ = p->ctxt;
4761 p->ctxt.in_rescue = before_rescue;
4762 push_end_expect_token_locations(p, &@1.beg_pos);
4763 }
4764 ;
4765
4766k_module : keyword_module
4767 {
4768 token_info_push(p, "module", &@$);
4769 $$ = p->ctxt;
4770 p->ctxt.in_rescue = before_rescue;
4771 push_end_expect_token_locations(p, &@1.beg_pos);
4772 }
4773 ;
4774
4775k_def : keyword_def
4776 {
4777 token_info_push(p, "def", &@$);
4778 $$ = NEW_DEF_TEMP(&@$);
4779 p->ctxt.in_argdef = 1;
4780 }
4781 ;
4782
4783k_do : keyword_do
4784 {
4785 token_info_push(p, "do", &@$);
4786 push_end_expect_token_locations(p, &@1.beg_pos);
4787 }
4788 ;
4789
4790k_do_block : keyword_do_block
4791 {
4792 token_info_push(p, "do", &@$);
4793 push_end_expect_token_locations(p, &@1.beg_pos);
4794 }
4795 ;
4796
4797k_rescue : keyword_rescue
4798 {
4799 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4800 $$ = p->ctxt;
4801 p->ctxt.in_rescue = after_rescue;
4802 }
4803 ;
4804
4805k_ensure : keyword_ensure
4806 {
4807 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4808 $$ = p->ctxt;
4809 }
4810 ;
4811
4812k_when : keyword_when
4813 {
4814 token_info_warn(p, "when", p->token_info, 0, &@$);
4815 }
4816 ;
4817
4818k_else : keyword_else
4819 {
4820 token_info *ptinfo_beg = p->token_info;
4821 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4822 token_info_warn(p, "else", p->token_info, same, &@$);
4823 if (same) {
4824 token_info e;
4825 e.next = ptinfo_beg->next;
4826 e.token = "else";
4827 token_info_setup(&e, p->lex.pbeg, &@$);
4828 if (!e.nonspc) *ptinfo_beg = e;
4829 }
4830 }
4831 ;
4832
4833k_elsif : keyword_elsif
4834 {
4835 WARN_EOL("elsif");
4836 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4837 }
4838 ;
4839
4840k_end : keyword_end
4841 {
4842 token_info_pop(p, "end", &@$);
4843 pop_end_expect_token_locations(p);
4844 }
4845 | tDUMNY_END
4846 {
4847 compile_error(p, "syntax error, unexpected end-of-input");
4848 }
4849 ;
4850
4851k_return : keyword_return
4852 {
4853 if (p->ctxt.cant_return && !dyna_in_block(p))
4854 yyerror1(&@1, "Invalid return in class/module body");
4855 }
4856 ;
4857
4858k_yield : keyword_yield
4859 {
4860 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4861 yyerror1(&@1, "Invalid yield");
4862 }
4863 ;
4864
4865then : term
4866 | keyword_then
4867 | term keyword_then
4868 ;
4869
4870do : term
4871 | keyword_do_cond { $$ = keyword_do_cond; }
4872 ;
4873
4874if_tail : opt_else
4875 | k_elsif expr_value then
4876 compstmt(stmts)
4877 if_tail
4878 {
4879 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4880 fixpos($$, $2);
4881 /*% ripper: elsif!($:2, $:4, $:5) %*/
4882 }
4883 ;
4884
4885opt_else : none
4886 | k_else compstmt(stmts)
4887 {
4888 $$ = $2;
4889 /*% ripper: else!($:2) %*/
4890 }
4891 ;
4892
4893for_var : lhs
4894 | mlhs
4895 ;
4896
4897f_marg : f_norm_arg
4898 {
4899 $$ = assignable(p, $1, 0, &@$);
4900 mark_lvar_used(p, $$);
4901 }
4902 | tLPAREN f_margs rparen
4903 {
4904 $$ = (NODE *)$2;
4905 /*% ripper: mlhs_paren!($:2) %*/
4906 }
4907 ;
4908
4909
4910f_margs : mlhs(f_marg)
4911 {
4912 $$ = NEW_MASGN($1, 0, &@$);
4913 /*% ripper: $:1 %*/
4914 }
4915 | mlhs(f_marg) ',' f_rest_marg
4916 {
4917 $$ = NEW_MASGN($1, $3, &@$);
4918 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4919 }
4920 | mlhs(f_marg) ',' f_rest_marg ',' mlhs(f_marg)
4921 {
4922 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4923 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4924 }
4925 | f_rest_marg
4926 {
4927 $$ = NEW_MASGN(0, $1, &@$);
4928 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4929 }
4930 | f_rest_marg ',' mlhs(f_marg)
4931 {
4932 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4933 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4934 }
4935 ;
4936
4937f_rest_marg : tSTAR f_norm_arg
4938 {
4939 /*% ripper: $:2 %*/
4940 $$ = assignable(p, $2, 0, &@$);
4941 mark_lvar_used(p, $$);
4942 }
4943 | tSTAR
4944 {
4945 $$ = NODE_SPECIAL_NO_NAME_REST;
4946 /*% ripper: Qnil %*/
4947 }
4948 ;
4949
4950f_any_kwrest : f_kwrest
4951 | f_no_kwarg
4952 {
4953 $$ = idNil;
4954 /*% ripper: ID2VAL(idNil) %*/
4955 }
4956 ;
4957
4958f_eq : {p->ctxt.in_argdef = 0;} '=';
4959
4960block_args_tail : args_tail_basic(primary_value)
4961 ;
4962
4963excessed_comma : ','
4964 {
4965 /* magic number for rest_id in iseq_set_arguments() */
4966 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4967 /*% ripper: excessed_comma! %*/
4968 }
4969 ;
4970
4971block_param : f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
4972 {
4973 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
4974 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
4975 }
4976 | f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
4977 {
4978 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4979 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
4980 }
4981 | f_arg ',' f_opt_arg(primary_value) opt_args_tail(block_args_tail)
4982 {
4983 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
4984 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
4985 }
4986 | f_arg ',' f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
4987 {
4988 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
4989 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
4990 }
4991 | f_arg ',' f_rest_arg opt_args_tail(block_args_tail)
4992 {
4993 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
4994 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
4995 }
4996 | f_arg excessed_comma
4997 {
4998 $$ = new_args_tail(p, 0, 0, 0, &@2);
4999 $$ = new_args(p, $1, 0, $2, 0, $$, &@$);
5000 /*% ripper: params!($:1, Qnil, $:2, Qnil, Qnil, Qnil, Qnil) %*/
5001 }
5002 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5003 {
5004 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
5005 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
5006 }
5007 | f_arg opt_args_tail(block_args_tail)
5008 {
5009 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
5010 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
5011 }
5012 | f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5013 {
5014 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
5015 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
5016 }
5017 | f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5018 {
5019 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
5020 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
5021 }
5022 | f_opt_arg(primary_value) opt_args_tail(block_args_tail)
5023 {
5024 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
5025 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
5026 }
5027 | f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5028 {
5029 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
5030 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
5031 }
5032 | f_rest_arg opt_args_tail(block_args_tail)
5033 {
5034 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
5035 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
5036 }
5037 | f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5038 {
5039 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
5040 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
5041 }
5042 | block_args_tail
5043 {
5044 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
5045 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
5046 }
5047 ;
5048
5049opt_block_param_def : none
5050 | block_param_def
5051 {
5052 p->command_start = TRUE;
5053 }
5054 ;
5055
5056block_param_def : '|' opt_block_param opt_bv_decl '|'
5057 {
5058 p->max_numparam = ORDINAL_PARAM;
5059 p->ctxt.in_argdef = 0;
5060 $$ = $2;
5061 /*% ripper: block_var!($:2, $:3) %*/
5062 }
5063 ;
5064
5065opt_block_param : /* none */
5066 {
5067 $$ = 0;
5068 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5069 }
5070 | block_param
5071 ;
5072
5073opt_bv_decl : '\n'?
5074 {
5075 $$ = 0;
5076 /*% ripper: Qfalse %*/
5077 }
5078 | '\n'? ';' bv_decls '\n'?
5079 {
5080 $$ = 0;
5081 /*% ripper: $:3 %*/
5082 }
5083 ;
5084
5085bv_decls : bvar
5086 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5087 | bv_decls ',' bvar
5088 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5089 ;
5090
5091bvar : tIDENTIFIER
5092 {
5093 new_bv(p, $1);
5094 /*% ripper: $:1 %*/
5095 }
5096 | f_bad_arg
5097 ;
5098
5099max_numparam : {
5100 $$ = p->max_numparam;
5101 p->max_numparam = 0;
5102 }
5103 ;
5104
5105numparam : {
5106 $$ = numparam_push(p);
5107 }
5108 ;
5109
5110it_id : {
5111 $$ = p->it_id;
5112 p->it_id = 0;
5113 }
5114 ;
5115
5116lambda : tLAMBDA[lpar]
5117 {
5118 token_info_push(p, "->", &@1);
5119 $$ = dyna_push(p);
5120 }[dyna]<vars>
5121 max_numparam numparam it_id allow_exits
5122 f_larglist[args]
5123 {
5124 CMDARG_PUSH(0);
5125 }
5126 lambda_body[body]
5127 {
5128 int max_numparam = p->max_numparam;
5129 ID it_id = p->it_id;
5130 p->lex.lpar_beg = $lpar;
5131 p->max_numparam = $max_numparam;
5132 p->it_id = $it_id;
5133 restore_block_exit(p, $allow_exits);
5134 CMDARG_POP();
5135 $args = args_with_numbered(p, $args, max_numparam, it_id);
5136 {
5137 YYLTYPE loc = code_loc_gen(&@args, &@body);
5138 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5139 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5140 nd_set_line($$, @args.end_pos.lineno);
5141 nd_set_first_loc($$, @1.beg_pos);
5142 xfree($body);
5143 }
5144 /*% ripper: lambda!($:args, $:body) %*/
5145 numparam_pop(p, $numparam);
5146 dyna_pop(p, $dyna);
5147 }
5148 ;
5149
5150f_larglist : '(' f_args opt_bv_decl ')'
5151 {
5152 p->ctxt.in_argdef = 0;
5153 $$ = $f_args;
5154 p->max_numparam = ORDINAL_PARAM;
5155 /*% ripper: paren!($:2) %*/
5156 }
5157 | f_args
5158 {
5159 p->ctxt.in_argdef = 0;
5160 if (!args_info_empty_p(&$1->nd_ainfo))
5161 p->max_numparam = ORDINAL_PARAM;
5162 $$ = $f_args;
5163 }
5164 ;
5165
5166lambda_body : tLAMBEG compstmt(stmts) '}'
5167 {
5168 token_info_pop(p, "}", &@3);
5169 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5170 /*% ripper: $:2 %*/
5171 }
5172 | keyword_do_LAMBDA
5173 {
5174 push_end_expect_token_locations(p, &@1.beg_pos);
5175 }
5176 bodystmt k_end
5177 {
5178 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5179 /*% ripper: $:3 %*/
5180 }
5181 ;
5182
5183do_block : k_do_block do_body k_end
5184 {
5185 $$ = $2;
5186 set_embraced_location($$, &@1, &@3);
5187 /*% ripper: $:2 %*/
5188 }
5189 ;
5190
5191block_call : command do_block
5192 {
5193 if (nd_type_p($1, NODE_YIELD)) {
5194 compile_error(p, "block given to yield");
5195 }
5196 else {
5197 block_dup_check(p, get_nd_args(p, $1), $2);
5198 }
5199 $$ = method_add_block(p, $1, $2, &@$);
5200 fixpos($$, $1);
5201 /*% ripper: method_add_block!($:1, $:2) %*/
5202 }
5203 | block_call call_op2 operation2 opt_paren_args
5204 {
5205 bool has_args = $4 != 0;
5206 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5207 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5208 /*% ripper: call!($:1, $:2, $:3) %*/
5209 if (has_args) {
5210 /*% ripper: method_add_arg!($:$, $:4) %*/
5211 }
5212 }
5213 | block_call call_op2 operation2 opt_paren_args brace_block
5214 {
5215 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5216 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5217 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
5218 if ($5) {
5219 /*% ripper: method_add_block!($:$, $:5) %*/
5220 }
5221 }
5222 | block_call call_op2 operation2 command_args do_block
5223 {
5224 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5225 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5226 }
5227 ;
5228
5229method_call : fcall paren_args
5230 {
5231 $1->nd_args = $2;
5232 $$ = (NODE *)$1;
5233 nd_set_last_loc($1, @2.end_pos);
5234 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5235 }
5236 | primary_value call_op operation2 opt_paren_args
5237 {
5238 bool has_args = $4 != 0;
5239 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5240 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5241 nd_set_line($$, @3.end_pos.lineno);
5242 /*% ripper: call!($:1, $:2, $:3) %*/
5243 if (has_args) {
5244 /*% ripper: method_add_arg!($:$, $:4) %*/
5245 }
5246 }
5247 | primary_value tCOLON2 operation2 paren_args
5248 {
5249 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5250 nd_set_line($$, @3.end_pos.lineno);
5251 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5252 }
5253 | primary_value tCOLON2 operation3
5254 {
5255 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5256 /*% ripper: call!($:1, $:2, $:3) %*/
5257 }
5258 | primary_value call_op2 paren_args
5259 {
5260 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5261 nd_set_line($$, @2.end_pos.lineno);
5262 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5263 }
5264 | keyword_super paren_args
5265 {
5266 rb_code_location_t lparen_loc = @2;
5267 rb_code_location_t rparen_loc = @2;
5268 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5269 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5270
5271 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5272 /*% ripper: super!($:2) %*/
5273 }
5274 | keyword_super
5275 {
5276 $$ = NEW_ZSUPER(&@$);
5277 /*% ripper: zsuper! %*/
5278 }
5279 | primary_value '[' opt_call_args rbracket
5280 {
5281 $$ = NEW_CALL($1, tAREF, $3, &@$);
5282 fixpos($$, $1);
5283 /*% ripper: aref!($:1, $:3) %*/
5284 }
5285 ;
5286
5287brace_block : '{' brace_body '}'
5288 {
5289 $$ = $2;
5290 set_embraced_location($$, &@1, &@3);
5291 /*% ripper: $:2 %*/
5292 }
5293 | k_do do_body k_end
5294 {
5295 $$ = $2;
5296 set_embraced_location($$, &@1, &@3);
5297 /*% ripper: $:2 %*/
5298 }
5299 ;
5300
5301brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5302 max_numparam numparam it_id allow_exits
5303 opt_block_param_def[args] compstmt(stmts)
5304 {
5305 int max_numparam = p->max_numparam;
5306 ID it_id = p->it_id;
5307 p->max_numparam = $max_numparam;
5308 p->it_id = $it_id;
5309 $args = args_with_numbered(p, $args, max_numparam, it_id);
5310 $$ = NEW_ITER($args, $compstmt, &@$);
5311 /*% ripper: brace_block!($:args, $:compstmt) %*/
5312 restore_block_exit(p, $allow_exits);
5313 numparam_pop(p, $numparam);
5314 dyna_pop(p, $dyna);
5315 }
5316 ;
5317
5318do_body : {
5319 $$ = dyna_push(p);
5320 CMDARG_PUSH(0);
5321 }[dyna]<vars>
5322 max_numparam numparam it_id allow_exits
5323 opt_block_param_def[args] bodystmt
5324 {
5325 int max_numparam = p->max_numparam;
5326 ID it_id = p->it_id;
5327 p->max_numparam = $max_numparam;
5328 p->it_id = $it_id;
5329 $args = args_with_numbered(p, $args, max_numparam, it_id);
5330 $$ = NEW_ITER($args, $bodystmt, &@$);
5331 /*% ripper: do_block!($:args, $:bodystmt) %*/
5332 CMDARG_POP();
5333 restore_block_exit(p, $allow_exits);
5334 numparam_pop(p, $numparam);
5335 dyna_pop(p, $dyna);
5336 }
5337 ;
5338
5339case_args : arg_value
5340 {
5341 check_literal_when(p, $arg_value, &@arg_value);
5342 $$ = NEW_LIST($arg_value, &@$);
5343 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5344 }
5345 | tSTAR arg_value
5346 {
5347 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5348 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5349 }
5350 | case_args[non_last_args] ',' arg_value
5351 {
5352 check_literal_when(p, $arg_value, &@arg_value);
5353 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5354 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5355 }
5356 | case_args[non_last_args] ',' tSTAR arg_value
5357 {
5358 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5359 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5360 }
5361 ;
5362
5363case_body : k_when case_args then
5364 compstmt(stmts)
5365 cases
5366 {
5367 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5368 fixpos($$, $2);
5369 /*% ripper: when!($:2, $:4, $:5) %*/
5370 }
5371 ;
5372
5373cases : opt_else
5374 | case_body
5375 ;
5376
5377p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5378p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5379
5380p_in_kwarg : {
5381 $$ = p->ctxt;
5382 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5383 p->command_start = FALSE;
5384 p->ctxt.in_kwarg = 1;
5385 }
5386 ;
5387
5388p_case_body : keyword_in
5389 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5390 p_top_expr[expr] then
5391 {
5392 pop_pktbl(p, $p_pktbl);
5393 pop_pvtbl(p, $p_pvtbl);
5394 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5395 }
5396 compstmt(stmts)
5397 p_cases[cases]
5398 {
5399 $$ = NEW_IN($expr, $compstmt, $cases, &@$);
5400 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5401 }
5402 ;
5403
5404p_cases : opt_else
5405 | p_case_body
5406 ;
5407
5408p_top_expr : p_top_expr_body
5409 | p_top_expr_body modifier_if expr_value
5410 {
5411 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5412 fixpos($$, $3);
5413 /*% ripper: if_mod!($:3, $:1) %*/
5414 }
5415 | p_top_expr_body modifier_unless expr_value
5416 {
5417 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5418 fixpos($$, $3);
5419 /*% ripper: unless_mod!($:3, $:1) %*/
5420 }
5421 ;
5422
5423p_top_expr_body : p_expr
5424 | p_expr ','
5425 {
5426 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5427 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5428 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5429 }
5430 | p_expr ',' p_args
5431 {
5432 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5433 nd_set_first_loc($$, @1.beg_pos);
5434 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5435 }
5436 | p_find
5437 {
5438 $$ = new_find_pattern(p, 0, $1, &@$);
5439 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5440 }
5441 | p_args_tail
5442 {
5443 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5444 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5445 }
5446 | p_kwargs
5447 {
5448 $$ = new_hash_pattern(p, 0, $1, &@$);
5449 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5450 }
5451 ;
5452
5453p_expr : p_as
5454 ;
5455
5456p_as : p_expr tASSOC p_variable
5457 {
5458 NODE *n = NEW_LIST($1, &@$);
5459 n = list_append(p, n, $3);
5460 $$ = new_hash(p, n, &@$);
5461 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5462 }
5463 | p_alt
5464 ;
5465
5466p_alt : p_alt '|' p_expr_basic
5467 {
5468 $$ = NEW_OR($1, $3, &@$, &@2);
5469 /*% ripper: binary!($:1, ID2VAL(idOr), $:3) %*/
5470 }
5471 | p_expr_basic
5472 ;
5473
5474p_lparen : '(' p_pktbl
5475 {
5476 $$ = $2;
5477 /*% ripper: $:2 %*/
5478 }
5479 ;
5480
5481p_lbracket : '[' p_pktbl
5482 {
5483 $$ = $2;
5484 /*% ripper: $:2 %*/
5485 }
5486 ;
5487
5488p_expr_basic : p_value
5489 | p_variable
5490 | p_const p_lparen[p_pktbl] p_args rparen
5491 {
5492 pop_pktbl(p, $p_pktbl);
5493 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5494 nd_set_first_loc($$, @p_const.beg_pos);
5495 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5496 }
5497 | p_const p_lparen[p_pktbl] p_find rparen
5498 {
5499 pop_pktbl(p, $p_pktbl);
5500 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5501 nd_set_first_loc($$, @p_const.beg_pos);
5502 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5503 }
5504 | p_const p_lparen[p_pktbl] p_kwargs rparen
5505 {
5506 pop_pktbl(p, $p_pktbl);
5507 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5508 nd_set_first_loc($$, @p_const.beg_pos);
5509 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5510 }
5511 | p_const '(' rparen
5512 {
5513 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5514 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5515 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5516 }
5517 | p_const p_lbracket[p_pktbl] p_args rbracket
5518 {
5519 pop_pktbl(p, $p_pktbl);
5520 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5521 nd_set_first_loc($$, @p_const.beg_pos);
5522 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5523 }
5524 | p_const p_lbracket[p_pktbl] p_find rbracket
5525 {
5526 pop_pktbl(p, $p_pktbl);
5527 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5528 nd_set_first_loc($$, @p_const.beg_pos);
5529 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5530 }
5531 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5532 {
5533 pop_pktbl(p, $p_pktbl);
5534 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5535 nd_set_first_loc($$, @p_const.beg_pos);
5536 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5537 }
5538 | p_const '[' rbracket
5539 {
5540 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5541 $$ = new_array_pattern(p, $1, 0, $$, &@$);
5542 /*% ripper: aryptn!($:1, Qnil, Qnil, Qnil) %*/
5543 }
5544 | tLBRACK p_args rbracket
5545 {
5546 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5547 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5548 }
5549 | tLBRACK p_find rbracket
5550 {
5551 $$ = new_find_pattern(p, 0, $p_find, &@$);
5552 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5553 }
5554 | tLBRACK rbracket
5555 {
5556 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5557 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5558 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5559 }
5560 | tLBRACE p_pktbl lex_ctxt[ctxt]
5561 {
5562 p->ctxt.in_kwarg = 0;
5563 }
5564 p_kwargs rbrace
5565 {
5566 pop_pktbl(p, $p_pktbl);
5567 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5568 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5569 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5570 }
5571 | tLBRACE rbrace
5572 {
5573 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5574 $$ = new_hash_pattern(p, 0, $$, &@$);
5575 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5576 }
5577 | tLPAREN p_pktbl p_expr rparen
5578 {
5579 pop_pktbl(p, $p_pktbl);
5580 $$ = $p_expr;
5581 /*% ripper: $:p_expr %*/
5582 }
5583 ;
5584
5585p_args : p_expr
5586 {
5587 NODE *pre_args = NEW_LIST($1, &@$);
5588 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5589 /*% ripper: [[$:1], Qnil, Qnil] %*/
5590 }
5591 | p_args_head
5592 {
5593 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5594 /*% ripper: [$:1, Qnil, Qnil] %*/
5595 }
5596 | p_args_head p_arg
5597 {
5598 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5599 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5600 }
5601 | p_args_head p_rest
5602 {
5603 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5604 /*% ripper: [$:1, $:2, Qnil] %*/
5605 }
5606 | p_args_head p_rest ',' p_args_post
5607 {
5608 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5609 /*% ripper: [$:1, $:2, $:4] %*/
5610 }
5611 | p_args_tail
5612 ;
5613
5614p_args_head : p_arg ','
5615 | p_args_head p_arg ','
5616 {
5617 $$ = list_concat($1, $2);
5618 /*% ripper: rb_ary_concat($:1, $:2) %*/
5619 }
5620 ;
5621
5622p_args_tail : p_rest
5623 {
5624 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5625 /*% ripper: [Qnil, $:1, Qnil] %*/
5626 }
5627 | p_rest ',' p_args_post
5628 {
5629 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5630 /*% ripper: [Qnil, $:1, $:3] %*/
5631 }
5632 ;
5633
5634p_find : p_rest ',' p_args_post ',' p_rest
5635 {
5636 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5637 /*% ripper: [$:1, $:3, $:5] %*/
5638 }
5639 ;
5640
5641
5642p_rest : tSTAR tIDENTIFIER
5643 {
5644 error_duplicate_pattern_variable(p, $2, &@2);
5645 /*% ripper: var_field!($:2) %*/
5646 $$ = assignable(p, $2, 0, &@$);
5647 }
5648 | tSTAR
5649 {
5650 $$ = 0;
5651 /*% ripper: var_field!(Qnil) %*/
5652 }
5653 ;
5654
5655p_args_post : p_arg
5656 | p_args_post ',' p_arg
5657 {
5658 $$ = list_concat($1, $3);
5659 /*% ripper: rb_ary_concat($:1, $:3) %*/
5660 }
5661 ;
5662
5663p_arg : p_expr
5664 {
5665 $$ = NEW_LIST($1, &@$);
5666 /*% ripper: [$:1] %*/
5667 }
5668 ;
5669
5670p_kwargs : p_kwarg ',' p_any_kwrest
5671 {
5672 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5673 /*% ripper: [$:1, $:3] %*/
5674 }
5675 | p_kwarg
5676 {
5677 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5678 /*% ripper: [$:1, Qnil] %*/
5679 }
5680 | p_kwarg ','
5681 {
5682 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5683 /*% ripper: [$:1, Qnil] %*/
5684 }
5685 | p_any_kwrest
5686 {
5687 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5688 /*% ripper: [[], $:1] %*/
5689 }
5690 ;
5691
5692p_kwarg : p_kw
5693 /*% ripper[brace]: [$:1] %*/
5694 | p_kwarg ',' p_kw
5695 {
5696 $$ = list_concat($1, $3);
5697 /*% ripper: rb_ary_push($:1, $:3) %*/
5698 }
5699 ;
5700
5701p_kw : p_kw_label p_expr
5702 {
5703 error_duplicate_pattern_key(p, $1, &@1);
5704 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5705 /*% ripper: [$:1, $:2] %*/
5706 }
5707 | p_kw_label
5708 {
5709 error_duplicate_pattern_key(p, $1, &@1);
5710 if ($1 && !is_local_id($1)) {
5711 yyerror1(&@1, "key must be valid as local variables");
5712 }
5713 error_duplicate_pattern_variable(p, $1, &@1);
5714 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5715 /*% ripper: [$:1, Qnil] %*/
5716 }
5717 ;
5718
5719p_kw_label : tLABEL
5720 | tSTRING_BEG string_contents tLABEL_END
5721 {
5722 YYLTYPE loc = code_loc_gen(&@1, &@3);
5723 if (!$2 || nd_type_p($2, NODE_STR)) {
5724 NODE *node = dsym_node(p, $2, &loc);
5725 $$ = rb_sym2id(rb_node_sym_string_val(node));
5726 }
5727 else {
5728 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5729 $$ = rb_intern_str(STR_NEW0());
5730 }
5731 /*% ripper: $:2 %*/
5732 }
5733 ;
5734
5735p_kwrest : kwrest_mark tIDENTIFIER
5736 {
5737 $$ = $2;
5738 /*% ripper: var_field!($:2) %*/
5739 }
5740 | kwrest_mark
5741 {
5742 $$ = 0;
5743 /*% ripper: Qnil %*/
5744 }
5745 ;
5746
5747p_kwnorest : kwrest_mark keyword_nil
5748 {
5749 $$ = 0;
5750 }
5751 ;
5752
5753p_any_kwrest : p_kwrest
5754 | p_kwnorest
5755 {
5756 $$ = idNil;
5757 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5758 }
5759 ;
5760
5761p_value : p_primitive
5762 | range_expr(p_primitive)
5763 | p_var_ref
5764 | p_expr_ref
5765 | p_const
5766 ;
5767
5768p_primitive : inline_primary
5769 | keyword_variable
5770 {
5771 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5772 /*% ripper: var_ref!($:1) %*/
5773 }
5774 | lambda
5775 ;
5776
5777p_variable : tIDENTIFIER
5778 {
5779 error_duplicate_pattern_variable(p, $1, &@1);
5780 /*% ripper: var_field!($:1) %*/
5781 $$ = assignable(p, $1, 0, &@$);
5782 }
5783 ;
5784
5785p_var_ref : '^' tIDENTIFIER
5786 {
5787 NODE *n = gettable(p, $2, &@$);
5788 if (!n) {
5789 n = NEW_ERROR(&@$);
5790 }
5791 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5792 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5793 }
5794 $$ = n;
5795 /*% ripper: var_ref!($:2) %*/
5796 }
5797 | '^' nonlocal_var
5798 {
5799 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5800 /*% ripper: var_ref!($:2) %*/
5801 }
5802 ;
5803
5804p_expr_ref : '^' tLPAREN expr_value rparen
5805 {
5806 $$ = NEW_BLOCK($3, &@$);
5807 /*% ripper: begin!($:3) %*/
5808 }
5809 ;
5810
5811p_const : tCOLON3 cname
5812 {
5813 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5814 /*% ripper: top_const_ref!($:2) %*/
5815 }
5816 | p_const tCOLON2 cname
5817 {
5818 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5819 /*% ripper: const_path_ref!($:1, $:3) %*/
5820 }
5821 | tCONSTANT
5822 {
5823 $$ = gettable(p, $1, &@$);
5824 /*% ripper: var_ref!($:1) %*/
5825 }
5826 ;
5827
5828opt_rescue : k_rescue exc_list exc_var then
5829 compstmt(stmts)
5830 opt_rescue
5831 {
5832 NODE *err = $3;
5833 if ($3) {
5834 err = NEW_ERRINFO(&@3);
5835 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5836 }
5837 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5838 if ($2) {
5839 fixpos($$, $2);
5840 }
5841 else if ($3) {
5842 fixpos($$, $3);
5843 }
5844 else {
5845 fixpos($$, $5);
5846 }
5847 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5848 }
5849 | none
5850 ;
5851
5852exc_list : arg_value
5853 {
5854 $$ = NEW_LIST($1, &@$);
5855 /*% ripper: rb_ary_new3(1, $:1) %*/
5856 }
5857 | mrhs
5858 {
5859 if (!($$ = splat_array($1))) $$ = $1;
5860 }
5861 | none
5862 ;
5863
5864exc_var : tASSOC lhs
5865 {
5866 $$ = $2;
5867 /*% ripper: $:2 %*/
5868 }
5869 | none
5870 ;
5871
5872opt_ensure : k_ensure stmts terms?
5873 {
5874 p->ctxt.in_rescue = $1.in_rescue;
5875 $$ = $2;
5876 void_expr(p, void_stmts(p, $$));
5877 /*% ripper: ensure!($:2) %*/
5878 }
5879 | none
5880 ;
5881
5882literal : numeric
5883 | symbol
5884 ;
5885
5886strings : string
5887 {
5888 if (!$1) {
5889 $$ = NEW_STR(STRING_NEW0(), &@$);
5890 } else {
5891 $$ = evstr2dstr(p, $1);
5892 }
5893 /*% ripper: $:1 %*/
5894 }
5895 ;
5896
5897string : tCHAR
5898 | string1
5899 | string string1
5900 {
5901 $$ = literal_concat(p, $1, $2, &@$);
5902 /*% ripper: string_concat!($:1, $:2) %*/
5903 }
5904 ;
5905
5906string1 : tSTRING_BEG string_contents tSTRING_END
5907 {
5908 $$ = heredoc_dedent(p, $2);
5909 if ($$) nd_set_loc($$, &@$);
5910 /*% ripper: $:2 %*/
5911 if (p->heredoc_indent > 0) {
5912 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5913 p->heredoc_indent = 0;
5914 }
5915 /*% ripper: string_literal!($:$) %*/
5916 }
5917 ;
5918
5919xstring : tXSTRING_BEG xstring_contents tSTRING_END
5920 {
5921 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5922 /*% ripper: $:2 %*/
5923 if (p->heredoc_indent > 0) {
5924 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5925 p->heredoc_indent = 0;
5926 }
5927 /*% ripper: xstring_literal!($:$) %*/
5928 }
5929 ;
5930
5931regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5932 {
5933 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5934 /*% ripper: regexp_literal!($:2, $:3) %*/
5935 }
5936 ;
5937
5938words : words(tWORDS_BEG, word_list)
5939 ;
5940
5941word_list : /* none */
5942 {
5943 $$ = 0;
5944 /*% ripper: words_new! %*/
5945 }
5946 | word_list word ' '+
5947 {
5948 $$ = list_append(p, $1, evstr2dstr(p, $2));
5949 /*% ripper: words_add!($:1, $:2) %*/
5950 }
5951 ;
5952
5953word : string_content
5954 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5955 | word string_content
5956 {
5957 $$ = literal_concat(p, $1, $2, &@$);
5958 /*% ripper: word_add!($:1, $:2) %*/
5959 }
5960 ;
5961
5962symbols : words(tSYMBOLS_BEG, symbol_list)
5963 ;
5964
5965symbol_list : /* none */
5966 {
5967 $$ = 0;
5968 /*% ripper: symbols_new! %*/
5969 }
5970 | symbol_list word ' '+
5971 {
5972 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5973 /*% ripper: symbols_add!($:1, $:2) %*/
5974 }
5975 ;
5976
5977qwords : words(tQWORDS_BEG, qword_list)
5978 ;
5979
5980qsymbols : words(tQSYMBOLS_BEG, qsym_list)
5981 ;
5982
5983qword_list : /* none */
5984 {
5985 $$ = 0;
5986 /*% ripper: qwords_new! %*/
5987 }
5988 | qword_list tSTRING_CONTENT ' '+
5989 {
5990 $$ = list_append(p, $1, $2);
5991 /*% ripper: qwords_add!($:1, $:2) %*/
5992 }
5993 ;
5994
5995qsym_list : /* none */
5996 {
5997 $$ = 0;
5998 /*% ripper: qsymbols_new! %*/
5999 }
6000 | qsym_list tSTRING_CONTENT ' '+
6001 {
6002 $$ = symbol_append(p, $1, $2);
6003 /*% ripper: qsymbols_add!($:1, $:2) %*/
6004 }
6005 ;
6006
6007string_contents : /* none */
6008 {
6009 $$ = 0;
6010 /*% ripper: string_content! %*/
6011 }
6012 | string_contents string_content
6013 {
6014 $$ = literal_concat(p, $1, $2, &@$);
6015 /*% ripper: string_add!($:1, $:2) %*/
6016 }
6017 ;
6018
6019xstring_contents: /* none */
6020 {
6021 $$ = 0;
6022 /*% ripper: xstring_new! %*/
6023 }
6024 | xstring_contents string_content
6025 {
6026 $$ = literal_concat(p, $1, $2, &@$);
6027 /*% ripper: xstring_add!($:1, $:2) %*/
6028 }
6029 ;
6030
6031regexp_contents: /* none */
6032 {
6033 $$ = 0;
6034 /*% ripper: regexp_new! %*/
6035 }
6036 | regexp_contents string_content
6037 {
6038 NODE *head = $1, *tail = $2;
6039 if (!head) {
6040 $$ = tail;
6041 }
6042 else if (!tail) {
6043 $$ = head;
6044 }
6045 else {
6046 switch (nd_type(head)) {
6047 case NODE_STR:
6048 head = str2dstr(p, head);
6049 break;
6050 case NODE_DSTR:
6051 break;
6052 default:
6053 head = list_append(p, NEW_DSTR(0, &@$), head);
6054 break;
6055 }
6056 $$ = list_append(p, head, tail);
6057 }
6058 /*% ripper: regexp_add!($:1, $:2) %*/
6059 }
6060 ;
6061
6062string_content : tSTRING_CONTENT
6063 /*% ripper[brace]: $:1 %*/
6064 | tSTRING_DVAR
6065 {
6066 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6067 $$ = p->lex.strterm;
6068 p->lex.strterm = 0;
6069 SET_LEX_STATE(EXPR_BEG);
6070 }<strterm>
6071 string_dvar
6072 {
6073 p->lex.strterm = $2;
6074 $$ = NEW_EVSTR($3, &@$, &@1, &NULL_LOC);
6075 nd_set_line($$, @3.end_pos.lineno);
6076 /*% ripper: string_dvar!($:3) %*/
6077 }
6078 | tSTRING_DBEG[state]
6079 {
6080 CMDARG_PUSH(0);
6081 COND_PUSH(0);
6082 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6083 $$ = p->lex.strterm;
6084 p->lex.strterm = 0;
6085 SET_LEX_STATE(EXPR_BEG);
6086 }[term]<strterm>
6087 {
6088 $$ = p->lex.brace_nest;
6089 p->lex.brace_nest = 0;
6090 }[brace]<num>
6091 {
6092 $$ = p->heredoc_indent;
6093 p->heredoc_indent = 0;
6094 }[indent]<num>
6095 compstmt(stmts) string_dend
6096 {
6097 COND_POP();
6098 CMDARG_POP();
6099 p->lex.strterm = $term;
6100 SET_LEX_STATE($state);
6101 p->lex.brace_nest = $brace;
6102 p->heredoc_indent = $indent;
6103 p->heredoc_line_indent = -1;
6104 if ($compstmt) nd_unset_fl_newline($compstmt);
6105 $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend);
6106 /*% ripper: string_embexpr!($:compstmt) %*/
6107 }
6108 ;
6109
6110string_dend : tSTRING_DEND
6111 | END_OF_INPUT
6112 ;
6113
6114string_dvar : nonlocal_var
6115 {
6116 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6117 /*% ripper: var_ref!($:1) %*/
6118 }
6119 | backref
6120 ;
6121
6122symbol : ssym
6123 | dsym
6124 ;
6125
6126ssym : tSYMBEG sym
6127 {
6128 SET_LEX_STATE(EXPR_END);
6129 VALUE str = rb_id2str($2);
6130 /*
6131 * TODO:
6132 * set_yylval_noname sets invalid id to yylval.
6133 * This branch can be removed once yylval is changed to
6134 * hold lexed string.
6135 */
6136 if (!str) str = STR_NEW0();
6137 $$ = NEW_SYM(str, &@$);
6138 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6139 }
6140 ;
6141
6142sym : fname
6143 | nonlocal_var
6144 ;
6145
6146dsym : tSYMBEG string_contents tSTRING_END
6147 {
6148 SET_LEX_STATE(EXPR_END);
6149 $$ = dsym_node(p, $2, &@$);
6150 /*% ripper: dyna_symbol!($:2) %*/
6151 }
6152 ;
6153
6154numeric : simple_numeric
6155 | tUMINUS_NUM simple_numeric %prec tLOWEST
6156 {
6157 $$ = $2;
6158 negate_lit(p, $$);
6159 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6160 }
6161 ;
6162
6163simple_numeric : tINTEGER
6164 | tFLOAT
6165 | tRATIONAL
6166 | tIMAGINARY
6167 ;
6168
6169nonlocal_var : tIVAR
6170 | tGVAR
6171 | tCVAR
6172 ;
6173
6174user_variable : ident_or_const
6175 | nonlocal_var
6176 ;
6177
6178keyword_variable : keyword_nil {$$ = KWD2EID(nil, $1);}
6179 | keyword_self {$$ = KWD2EID(self, $1);}
6180 | keyword_true {$$ = KWD2EID(true, $1);}
6181 | keyword_false {$$ = KWD2EID(false, $1);}
6182 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6183 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6184 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6185 ;
6186
6187var_ref : user_variable
6188 {
6189 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6190 if (ifdef_ripper(id_is_var(p, $1), false)) {
6191 /*% ripper: var_ref!($:1) %*/
6192 }
6193 else {
6194 /*% ripper: vcall!($:1) %*/
6195 }
6196 }
6197 | keyword_variable
6198 {
6199 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6200 /*% ripper: var_ref!($:1) %*/
6201 }
6202 ;
6203
6204var_lhs : user_or_keyword_variable
6205 {
6206 /*% ripper: var_field!($:1) %*/
6207 $$ = assignable(p, $1, 0, &@$);
6208 }
6209 ;
6210
6211backref : tNTH_REF
6212 | tBACK_REF
6213 ;
6214
6215superclass : '<'
6216 {
6217 SET_LEX_STATE(EXPR_BEG);
6218 p->command_start = TRUE;
6219 }
6220 expr_value term
6221 {
6222 $$ = $3;
6223 /*% ripper: $:3 %*/
6224 }
6225 | none
6226 ;
6227
6228f_opt_paren_args: f_paren_args
6229 | none
6230 {
6231 p->ctxt.in_argdef = 0;
6232 $$ = new_args_tail(p, 0, 0, 0, &@0);
6233 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6234 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6235 }
6236 ;
6237
6238f_paren_args : '(' f_args rparen
6239 {
6240 $$ = $2;
6241 /*% ripper: paren!($:2) %*/
6242 SET_LEX_STATE(EXPR_BEG);
6243 p->command_start = TRUE;
6244 p->ctxt.in_argdef = 0;
6245 }
6246 ;
6247
6248f_arglist : f_paren_args
6249 | {
6250 $$ = p->ctxt;
6251 p->ctxt.in_kwarg = 1;
6252 p->ctxt.in_argdef = 1;
6253 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6254 }<ctxt>
6255 f_args term
6256 {
6257 p->ctxt.in_kwarg = $1.in_kwarg;
6258 p->ctxt.in_argdef = 0;
6259 $$ = $2;
6260 SET_LEX_STATE(EXPR_BEG);
6261 p->command_start = TRUE;
6262 /*% ripper: $:2 %*/
6263 }
6264 ;
6265
6266args_tail : args_tail_basic(arg_value)
6267 | args_forward
6268 {
6269 ID fwd = $args_forward;
6270 if (lambda_beginning_p() ||
6271 (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest)) {
6272 yyerror0("unexpected ... in lambda argument");
6273 fwd = 0;
6274 }
6275 else {
6276 add_forwarding_args(p);
6277 }
6278 $$ = new_args_tail(p, 0, fwd, arg_FWD_BLOCK, &@1);
6279 $$->nd_ainfo.forwarding = 1;
6280 /*% ripper: [Qnil, $:1, Qnil] %*/
6281 }
6282 ;
6283
6284f_args : f_arg ',' f_opt_arg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6285 {
6286 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
6287 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
6288 }
6289 | f_arg ',' f_opt_arg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6290 {
6291 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6292 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
6293 }
6294 | f_arg ',' f_opt_arg(arg_value) opt_args_tail(args_tail)
6295 {
6296 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
6297 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
6298 }
6299 | f_arg ',' f_opt_arg(arg_value) ',' f_arg opt_args_tail(args_tail)
6300 {
6301 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
6302 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
6303 }
6304 | f_arg ',' f_rest_arg opt_args_tail(args_tail)
6305 {
6306 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
6307 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
6308 }
6309 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6310 {
6311 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
6312 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
6313 }
6314 | f_arg opt_args_tail(args_tail)
6315 {
6316 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
6317 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
6318 }
6319 | f_opt_arg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6320 {
6321 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
6322 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
6323 }
6324 | f_opt_arg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6325 {
6326 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
6327 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
6328 }
6329 | f_opt_arg(arg_value) opt_args_tail(args_tail)
6330 {
6331 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
6332 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
6333 }
6334 | f_opt_arg(arg_value) ',' f_arg opt_args_tail(args_tail)
6335 {
6336 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
6337 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
6338 }
6339 | f_rest_arg opt_args_tail(args_tail)
6340 {
6341 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
6342 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
6343 }
6344 | f_rest_arg ',' f_arg opt_args_tail(args_tail)
6345 {
6346 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
6347 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
6348 }
6349 | args_tail
6350 {
6351 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
6352 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
6353 }
6354 | /* none */
6355 {
6356 $$ = new_args_tail(p, 0, 0, 0, &@0);
6357 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6358 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6359 }
6360 ;
6361
6362args_forward : tBDOT3
6363 {
6364#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
6365 $$ = 0;
6366#else
6367 $$ = idFWD_KWREST;
6368#endif
6369 /*% ripper: args_forward! %*/
6370 }
6371 ;
6372
6373f_bad_arg : tCONSTANT
6374 {
6375 static const char mesg[] = "formal argument cannot be a constant";
6376 /*%%%*/
6377 yyerror1(&@1, mesg);
6378 /*% %*/
6379 $$ = 0;
6380 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6381 }
6382 | tIVAR
6383 {
6384 static const char mesg[] = "formal argument cannot be an instance variable";
6385 /*%%%*/
6386 yyerror1(&@1, mesg);
6387 /*% %*/
6388 $$ = 0;
6389 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6390 }
6391 | tGVAR
6392 {
6393 static const char mesg[] = "formal argument cannot be a global variable";
6394 /*%%%*/
6395 yyerror1(&@1, mesg);
6396 /*% %*/
6397 $$ = 0;
6398 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6399 }
6400 | tCVAR
6401 {
6402 static const char mesg[] = "formal argument cannot be a class variable";
6403 /*%%%*/
6404 yyerror1(&@1, mesg);
6405 /*% %*/
6406 $$ = 0;
6407 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6408 }
6409 ;
6410
6411f_norm_arg : f_bad_arg
6412 | tIDENTIFIER
6413 {
6414 VALUE e = formal_argument_error(p, $$ = $1);
6415 if (e) {
6416 /*% ripper[error]: param_error!(?e, $:1) %*/
6417 }
6418 p->max_numparam = ORDINAL_PARAM;
6419 }
6420 ;
6421
6422f_arg_asgn : f_norm_arg
6423 {
6424 arg_var(p, $1);
6425 $$ = $1;
6426 }
6427 ;
6428
6429f_arg_item : f_arg_asgn
6430 {
6431 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6432 /*% ripper: $:1 %*/
6433 }
6434 | tLPAREN f_margs rparen
6435 {
6436 ID tid = internal_id(p);
6437 YYLTYPE loc;
6438 loc.beg_pos = @2.beg_pos;
6439 loc.end_pos = @2.beg_pos;
6440 arg_var(p, tid);
6441 if (dyna_in_block(p)) {
6442 $2->nd_value = NEW_DVAR(tid, &loc);
6443 }
6444 else {
6445 $2->nd_value = NEW_LVAR(tid, &loc);
6446 }
6447 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6448 $$->nd_next = (NODE *)$2;
6449 /*% ripper: mlhs_paren!($:2) %*/
6450 }
6451 ;
6452
6453f_arg : f_arg_item
6454 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6455 | f_arg ',' f_arg_item
6456 {
6457 $$ = $1;
6458 $$->nd_plen++;
6459 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6460 rb_discard_node(p, (NODE *)$3);
6461 /*% ripper: rb_ary_push($:1, $:3) %*/
6462 }
6463 ;
6464
6465
6466f_label : tLABEL
6467 {
6468 VALUE e = formal_argument_error(p, $$ = $1);
6469 if (e) {
6470 $$ = 0;
6471 /*% ripper[error]: param_error!(?e, $:1) %*/
6472 }
6473 /*
6474 * Workaround for Prism::ParseTest#test_filepath for
6475 * "unparser/corpus/literal/def.txt"
6476 *
6477 * See the discussion on https://github.com/ruby/ruby/pull/9923
6478 */
6479 arg_var(p, ifdef_ripper(0, $1));
6480 /*% ripper: $:1 %*/
6481 p->max_numparam = ORDINAL_PARAM;
6482 p->ctxt.in_argdef = 0;
6483 }
6484 ;
6485
6486kwrest_mark : tPOW
6487 | tDSTAR
6488 ;
6489
6490f_no_kwarg : p_kwnorest
6491 {
6492 /*% ripper: nokw_param!(Qnil) %*/
6493 }
6494 ;
6495
6496f_kwrest : kwrest_mark tIDENTIFIER
6497 {
6498 arg_var(p, shadowing_lvar(p, $2));
6499 $$ = $2;
6500 /*% ripper: kwrest_param!($:2) %*/
6501 }
6502 | kwrest_mark
6503 {
6504 arg_var(p, idFWD_KWREST);
6505 $$ = idFWD_KWREST;
6506 /*% ripper: kwrest_param!(Qnil) %*/
6507 }
6508 ;
6509
6510restarg_mark : '*'
6511 | tSTAR
6512 ;
6513
6514f_rest_arg : restarg_mark tIDENTIFIER
6515 {
6516 arg_var(p, shadowing_lvar(p, $2));
6517 $$ = $2;
6518 /*% ripper: rest_param!($:2) %*/
6519 }
6520 | restarg_mark
6521 {
6522 arg_var(p, idFWD_REST);
6523 $$ = idFWD_REST;
6524 /*% ripper: rest_param!(Qnil) %*/
6525 }
6526 ;
6527
6528blkarg_mark : '&'
6529 | tAMPER
6530 ;
6531
6532f_block_arg : blkarg_mark tIDENTIFIER
6533 {
6534 arg_var(p, shadowing_lvar(p, $2));
6535 $$ = $2;
6536 /*% ripper: blockarg!($:2) %*/
6537 }
6538 | blkarg_mark
6539 {
6540 arg_var(p, idFWD_BLOCK);
6541 $$ = idFWD_BLOCK;
6542 /*% ripper: blockarg!(Qnil) %*/
6543 }
6544 ;
6545
6546opt_f_block_arg : ',' f_block_arg
6547 {
6548 $$ = $2;
6549 /*% ripper: $:2 %*/
6550 }
6551 | none
6552 ;
6553
6554
6555singleton : value_expr(singleton_expr)
6556 {
6557 NODE *expr = last_expr_node($1);
6558 switch (nd_type(expr)) {
6559 case NODE_STR:
6560 case NODE_DSTR:
6561 case NODE_XSTR:
6562 case NODE_DXSTR:
6563 case NODE_REGX:
6564 case NODE_DREGX:
6565 case NODE_SYM:
6566 case NODE_LINE:
6567 case NODE_FILE:
6568 case NODE_ENCODING:
6569 case NODE_INTEGER:
6570 case NODE_FLOAT:
6571 case NODE_RATIONAL:
6572 case NODE_IMAGINARY:
6573 case NODE_DSYM:
6574 case NODE_LIST:
6575 case NODE_ZLIST:
6576 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6577 break;
6578 default:
6579 break;
6580 }
6581 $$ = $1;
6582 }
6583 ;
6584
6585singleton_expr : var_ref
6586 | '('
6587 {
6588 SET_LEX_STATE(EXPR_BEG);
6589 p->ctxt.in_argdef = 0;
6590 }
6591 expr rparen
6592 {
6593 p->ctxt.in_argdef = 1;
6594 $$ = $3;
6595 /*% ripper: paren!($:3) %*/
6596 }
6597 ;
6598
6599assoc_list : none
6600 | assocs trailer
6601 {
6602 $$ = $1;
6603 /*% ripper: assoclist_from_args!($:1) %*/
6604 }
6605 ;
6606
6607assocs : assoc
6608 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6609 | assocs ',' assoc
6610 {
6611 NODE *assocs = $1;
6612 NODE *tail = $3;
6613 if (!assocs) {
6614 assocs = tail;
6615 }
6616 else if (tail) {
6617 if (RNODE_LIST(assocs)->nd_head) {
6618 NODE *n = RNODE_LIST(tail)->nd_next;
6619 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6620 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6621 /* DSTAR */
6622 tail = RNODE_HASH(n)->nd_head;
6623 }
6624 }
6625 if (tail) {
6626 assocs = list_concat(assocs, tail);
6627 }
6628 }
6629 $$ = assocs;
6630 /*% ripper: rb_ary_push($:1, $:3) %*/
6631 }
6632 ;
6633
6634assoc : arg_value tASSOC arg_value
6635 {
6636 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6637 /*% ripper: assoc_new!($:1, $:3) %*/
6638 }
6639 | tLABEL arg_value
6640 {
6641 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6642 /*% ripper: assoc_new!($:1, $:2) %*/
6643 }
6644 | tLABEL
6645 {
6646 NODE *val = gettable(p, $1, &@$);
6647 if (!val) val = NEW_ERROR(&@$);
6648 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6649 /*% ripper: assoc_new!($:1, Qnil) %*/
6650 }
6651 | tSTRING_BEG string_contents tLABEL_END arg_value
6652 {
6653 YYLTYPE loc = code_loc_gen(&@1, &@3);
6654 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6655 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6656 }
6657 | tDSTAR arg_value
6658 {
6659 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6660 /*% ripper: assoc_splat!($:2) %*/
6661 }
6662 | tDSTAR
6663 {
6664 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6665 $$ = list_append(p, NEW_LIST(0, &@$),
6666 NEW_LVAR(idFWD_KWREST, &@$));
6667 /*% ripper: assoc_splat!(Qnil) %*/
6668 }
6669 ;
6670
6671%rule %inline operation : ident_or_const
6672 | tFID
6673 ;
6674
6675operation2 : operation
6676 | op
6677 ;
6678
6679operation3 : tIDENTIFIER
6680 | tFID
6681 | op
6682 ;
6683
6684dot_or_colon : '.'
6685 | tCOLON2
6686 ;
6687
6688call_op : '.'
6689 | tANDDOT
6690 ;
6691
6692call_op2 : call_op
6693 | tCOLON2
6694 ;
6695
6696rparen : '\n'? ')'
6697 ;
6698
6699rbracket : '\n'? ']'
6700 ;
6701
6702rbrace : '\n'? '}'
6703 ;
6704
6705trailer : '\n'?
6706 | ','
6707 ;
6708
6709term : ';' {yyerrok;token_flush(p);}
6710 | '\n'
6711 {
6712 @$.end_pos = @$.beg_pos;
6713 token_flush(p);
6714 }
6715 ;
6716
6717terms : term
6718 | terms ';' {yyerrok;}
6719 ;
6720
6721none : /* none */
6722 {
6723 $$ = 0;
6724 /*% ripper: Qnil %*/
6725 }
6726 ;
6727%%
6728# undef p
6729# undef yylex
6730# undef yylval
6731# define yylval (*p->lval)
6732
6733static int regx_options(struct parser_params*);
6734static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6735static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6736static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6737static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6738
6739#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6740
6741# define set_yylval_node(x) { \
6742 YYLTYPE _cur_loc; \
6743 rb_parser_set_location(p, &_cur_loc); \
6744 yylval.node = (x); \
6745 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6746}
6747# define set_yylval_str(x) \
6748do { \
6749 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6750 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6751} while(0)
6752# define set_yylval_num(x) { \
6753 yylval.num = (x); \
6754 set_parser_s_value(x); \
6755}
6756# define set_yylval_id(x) (yylval.id = (x))
6757# define set_yylval_name(x) { \
6758 (yylval.id = (x)); \
6759 set_parser_s_value(ID2SYM(x)); \
6760}
6761# define yylval_id() (yylval.id)
6762
6763#define set_yylval_noname() set_yylval_id(keyword_nil)
6764#define has_delayed_token(p) (p->delayed.token != NULL)
6765
6766#ifndef RIPPER
6767#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6768#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6769
6770static bool
6771parser_has_token(struct parser_params *p)
6772{
6773 const char *const pcur = p->lex.pcur;
6774 const char *const ptok = p->lex.ptok;
6775 if (p->keep_tokens && (pcur < ptok)) {
6776 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6777 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6778 }
6779 return pcur > ptok;
6780}
6781
6782static const char *
6783escaped_char(int c)
6784{
6785 switch (c) {
6786 case '"': return "\\\"";
6787 case '\\': return "\\\\";
6788 case '\0': return "\\0";
6789 case '\n': return "\\n";
6790 case '\r': return "\\r";
6791 case '\t': return "\\t";
6792 case '\f': return "\\f";
6793 case '\013': return "\\v";
6794 case '\010': return "\\b";
6795 case '\007': return "\\a";
6796 case '\033': return "\\e";
6797 case '\x7f': return "\\c?";
6798 }
6799 return NULL;
6800}
6801
6802static rb_parser_string_t *
6803rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6804{
6805 rb_encoding *enc = p->enc;
6806 const char *ptr = str->ptr;
6807 const char *pend = ptr + str->len;
6808 const char *prev = ptr;
6809 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6810 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6811
6812 while (ptr < pend) {
6813 unsigned int c;
6814 const char *cc;
6815 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6816 if (!MBCLEN_CHARFOUND_P(n)) {
6817 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6818 n = rb_enc_mbminlen(enc);
6819 if (pend < ptr + n)
6820 n = (int)(pend - ptr);
6821 while (n--) {
6822 c = *ptr & 0xf0 >> 4;
6823 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6824 c = *ptr & 0x0f;
6825 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6826 parser_str_cat(result, charbuf, 4);
6827 prev = ++ptr;
6828 }
6829 continue;
6830 }
6831 n = MBCLEN_CHARFOUND_LEN(n);
6832 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6833 ptr += n;
6834 cc = escaped_char(c);
6835 if (cc) {
6836 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6837 parser_str_cat_cstr(result, cc);
6838 prev = ptr;
6839 }
6840 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6841 }
6842 else {
6843 if (ptr - n > prev) {
6844 parser_str_cat(result, prev, ptr - n - prev);
6845 prev = ptr - n;
6846 }
6847 parser_str_cat(result, prev, ptr - prev);
6848 prev = ptr;
6849 }
6850 }
6851 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6852
6853 return result;
6854}
6855
6856static void
6857parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6858{
6859 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6860 token->id = p->token_id;
6861 token->type_name = parser_token2char(p, t);
6862 token->str = str;
6863 token->loc.beg_pos = p->yylloc->beg_pos;
6864 token->loc.end_pos = p->yylloc->end_pos;
6865 rb_parser_ary_push_ast_token(p, p->tokens, token);
6866 p->token_id++;
6867
6868 if (p->debug) {
6869 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6870 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6871 line, token->id, token->type_name, str_escaped->ptr,
6872 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6873 token->loc.end_pos.lineno, token->loc.end_pos.column);
6874 rb_parser_string_free(p, str_escaped);
6875 }
6876}
6877
6878static void
6879parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6880{
6881 debug_token_line(p, "parser_dispatch_scan_event", line);
6882
6883 if (!parser_has_token(p)) return;
6884
6885 RUBY_SET_YYLLOC(*p->yylloc);
6886
6887 if (p->keep_tokens) {
6888 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6889 parser_append_tokens(p, str, t, line);
6890 }
6891
6892 token_flush(p);
6893}
6894
6895#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6896static void
6897parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6898{
6899 debug_token_line(p, "parser_dispatch_delayed_token", line);
6900
6901 if (!has_delayed_token(p)) return;
6902
6903 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6904
6905 if (p->keep_tokens) {
6906 /* p->delayed.token is freed by rb_parser_tokens_free */
6907 parser_append_tokens(p, p->delayed.token, t, line);
6908 } else {
6909 rb_parser_string_free(p, p->delayed.token);
6910 }
6911
6912 p->delayed.token = NULL;
6913}
6914#else
6915#define literal_flush(p, ptr) ((void)(ptr))
6916
6917static int
6918ripper_has_scan_event(struct parser_params *p)
6919{
6920 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6921 return p->lex.pcur > p->lex.ptok;
6922}
6923
6924static VALUE
6925ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6926{
6927 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6928 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6929 RUBY_SET_YYLLOC(*p->yylloc);
6930 token_flush(p);
6931 return rval;
6932}
6933
6934static void
6935ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6936{
6937 if (!ripper_has_scan_event(p)) return;
6938
6939 set_parser_s_value(ripper_scan_event_val(p, t));
6940}
6941#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6942
6943static void
6944ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6945{
6946 /* save and adjust the location to delayed token for callbacks */
6947 int saved_line = p->ruby_sourceline;
6948 const char *saved_tokp = p->lex.ptok;
6949 VALUE s_value, str;
6950
6951 if (!has_delayed_token(p)) return;
6952 p->ruby_sourceline = p->delayed.beg_line;
6953 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6954 str = rb_str_new_mutable_parser_string(p->delayed.token);
6955 rb_parser_string_free(p, p->delayed.token);
6956 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
6957 set_parser_s_value(s_value);
6958 p->delayed.token = NULL;
6959 p->ruby_sourceline = saved_line;
6960 p->lex.ptok = saved_tokp;
6961}
6962#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6963#endif /* RIPPER */
6964
6965static inline int
6966is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6967{
6968 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6969}
6970
6971static inline int
6972parser_is_identchar(struct parser_params *p)
6973{
6974 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
6975}
6976
6977static inline int
6978parser_isascii(struct parser_params *p)
6979{
6980 return ISASCII(*(p->lex.pcur-1));
6981}
6982
6983static void
6984token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
6985{
6986 int column = 1, nonspc = 0, i;
6987 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
6988 if (*ptr == '\t') {
6989 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
6990 }
6991 column++;
6992 if (*ptr != ' ' && *ptr != '\t') {
6993 nonspc = 1;
6994 }
6995 }
6996
6997 ptinfo->beg = loc->beg_pos;
6998 ptinfo->indent = column;
6999 ptinfo->nonspc = nonspc;
7000}
7001
7002static void
7003token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7004{
7005 token_info *ptinfo;
7006
7007 if (!p->token_info_enabled) return;
7008 ptinfo = ALLOC(token_info);
7009 ptinfo->token = token;
7010 ptinfo->next = p->token_info;
7011 token_info_setup(ptinfo, p->lex.pbeg, loc);
7012
7013 p->token_info = ptinfo;
7014}
7015
7016static void
7017token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7018{
7019 token_info *ptinfo_beg = p->token_info;
7020
7021 if (!ptinfo_beg) return;
7022
7023 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7024 token_info_warn(p, token, ptinfo_beg, 1, loc);
7025
7026 p->token_info = ptinfo_beg->next;
7027 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7028}
7029
7030static void
7031token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7032{
7033 token_info *ptinfo_beg = p->token_info;
7034
7035 if (!ptinfo_beg) return;
7036 p->token_info = ptinfo_beg->next;
7037
7038 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7039 ptinfo_beg->beg.column != beg_pos.column ||
7040 strcmp(ptinfo_beg->token, token)) {
7041 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7042 beg_pos.lineno, beg_pos.column, token,
7043 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7044 ptinfo_beg->token);
7045 }
7046
7047 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7048}
7049
7050static void
7051token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7052{
7053 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7054 if (!p->token_info_enabled) return;
7055 if (!ptinfo_beg) return;
7056 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7057 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7058 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7059 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7060 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7061 rb_warn3L(ptinfo_end->beg.lineno,
7062 "mismatched indentations at '%s' with '%s' at %d",
7063 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7064}
7065
7066static int
7067parser_precise_mbclen(struct parser_params *p, const char *ptr)
7068{
7069 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7070 if (!MBCLEN_CHARFOUND_P(len)) {
7071 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7072 return -1;
7073 }
7074 return len;
7075}
7076
7077#ifndef RIPPER
7078static inline void
7079parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7080{
7081 rb_parser_string_t *str;
7082 int lineno = p->ruby_sourceline;
7083 if (!yylloc) {
7084 return;
7085 }
7086 else if (yylloc->beg_pos.lineno == lineno) {
7087 str = p->lex.lastline;
7088 }
7089 else {
7090 return;
7091 }
7092 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7093}
7094
7095static int
7096parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7097{
7098#if 0
7099 YYLTYPE current;
7100
7101 if (!yylloc) {
7102 yylloc = RUBY_SET_YYLLOC(current);
7103 }
7104 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7105 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7106 yylloc = 0;
7107 }
7108#endif
7109 parser_compile_error(p, yylloc, "%s", msg);
7110 parser_show_error_line(p, yylloc);
7111 return 0;
7112}
7113
7114static int
7115parser_yyerror0(struct parser_params *p, const char *msg)
7116{
7117 YYLTYPE current;
7118 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7119}
7120
7121void
7122ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7123{
7124 VALUE mesg;
7125 const int max_line_margin = 30;
7126 const char *ptr, *ptr_end, *pt, *pb;
7127 const char *pre = "", *post = "", *pend;
7128 const char *code = "", *caret = "";
7129 const char *lim;
7130 const char *const pbeg = PARSER_STRING_PTR(str);
7131 char *buf;
7132 long len;
7133 int i;
7134
7135 if (!yylloc) return;
7136 pend = rb_parser_string_end(str);
7137 if (pend > pbeg && pend[-1] == '\n') {
7138 if (--pend > pbeg && pend[-1] == '\r') --pend;
7139 }
7140
7141 pt = pend;
7142 if (lineno == yylloc->end_pos.lineno &&
7143 (pend - pbeg) > yylloc->end_pos.column) {
7144 pt = pbeg + yylloc->end_pos.column;
7145 }
7146
7147 ptr = ptr_end = pt;
7148 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7149 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7150
7151 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7152 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7153
7154 len = ptr_end - ptr;
7155 if (len > 4) {
7156 if (ptr > pbeg) {
7157 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7158 if (ptr > pbeg) pre = "...";
7159 }
7160 if (ptr_end < pend) {
7161 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7162 if (ptr_end < pend) post = "...";
7163 }
7164 }
7165 pb = pbeg;
7166 if (lineno == yylloc->beg_pos.lineno) {
7167 pb += yylloc->beg_pos.column;
7168 if (pb > pt) pb = pt;
7169 }
7170 if (pb < ptr) pb = ptr;
7171 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7172 return;
7173 }
7174 if (RTEST(errbuf)) {
7175 mesg = rb_attr_get(errbuf, idMesg);
7176 if (char_at_end(p, mesg, '\n') != '\n')
7177 rb_str_cat_cstr(mesg, "\n");
7178 }
7179 else {
7180 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7181 }
7182 if (!errbuf && rb_stderr_tty_p()) {
7183#define CSI_BEGIN "\033["
7184#define CSI_SGR "m"
7185 rb_str_catf(mesg,
7186 CSI_BEGIN""CSI_SGR"%s" /* pre */
7187 CSI_BEGIN"1"CSI_SGR"%.*s"
7188 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7189 CSI_BEGIN";1"CSI_SGR"%.*s"
7190 CSI_BEGIN""CSI_SGR"%s" /* post */
7191 "\n",
7192 pre,
7193 (int)(pb - ptr), ptr,
7194 (int)(pt - pb), pb,
7195 (int)(ptr_end - pt), pt,
7196 post);
7197 }
7198 else {
7199 char *p2;
7200
7201 len = ptr_end - ptr;
7202 lim = pt < pend ? pt : pend;
7203 i = (int)(lim - ptr);
7204 buf = ALLOCA_N(char, i+2);
7205 code = ptr;
7206 caret = p2 = buf;
7207 if (ptr <= pb) {
7208 while (ptr < pb) {
7209 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7210 }
7211 *p2++ = '^';
7212 ptr++;
7213 }
7214 if (lim > ptr) {
7215 memset(p2, '~', (lim - ptr));
7216 p2 += (lim - ptr);
7217 }
7218 *p2 = '\0';
7219 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7220 pre, (int)len, code, post,
7221 pre, caret);
7222 }
7223 if (!errbuf) rb_write_error_str(mesg);
7224}
7225#else
7226
7227static int
7228parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7229{
7230 const char *pcur = 0, *ptok = 0;
7231 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7232 p->ruby_sourceline == yylloc->end_pos.lineno) {
7233 pcur = p->lex.pcur;
7234 ptok = p->lex.ptok;
7235 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7236 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7237 }
7238 parser_yyerror0(p, msg);
7239 if (pcur) {
7240 p->lex.ptok = ptok;
7241 p->lex.pcur = pcur;
7242 }
7243 return 0;
7244}
7245
7246static int
7247parser_yyerror0(struct parser_params *p, const char *msg)
7248{
7249 dispatch1(parse_error, STR_NEW2(msg));
7250 ripper_error(p);
7251 return 0;
7252}
7253
7254static inline void
7255parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7256{
7257}
7258#endif /* !RIPPER */
7259
7260static int
7261vtable_size(const struct vtable *tbl)
7262{
7263 if (!DVARS_TERMINAL_P(tbl)) {
7264 return tbl->pos;
7265 }
7266 else {
7267 return 0;
7268 }
7269}
7270
7271static struct vtable *
7272vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7273{
7274 struct vtable *tbl = ALLOC(struct vtable);
7275 tbl->pos = 0;
7276 tbl->capa = 8;
7277 tbl->tbl = ALLOC_N(ID, tbl->capa);
7278 tbl->prev = prev;
7279#ifndef RIPPER
7280 if (p->debug) {
7281 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7282 }
7283#endif
7284 return tbl;
7285}
7286#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7287
7288static void
7289vtable_free_gen(struct parser_params *p, int line, const char *name,
7290 struct vtable *tbl)
7291{
7292#ifndef RIPPER
7293 if (p->debug) {
7294 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7295 }
7296#endif
7297 if (!DVARS_TERMINAL_P(tbl)) {
7298 if (tbl->tbl) {
7299 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7300 }
7301 ruby_sized_xfree(tbl, sizeof(*tbl));
7302 }
7303}
7304#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7305
7306static void
7307vtable_add_gen(struct parser_params *p, int line, const char *name,
7308 struct vtable *tbl, ID id)
7309{
7310#ifndef RIPPER
7311 if (p->debug) {
7312 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7313 line, name, (void *)tbl, rb_id2name(id));
7314 }
7315#endif
7316 if (DVARS_TERMINAL_P(tbl)) {
7317 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7318 return;
7319 }
7320 if (tbl->pos == tbl->capa) {
7321 tbl->capa = tbl->capa * 2;
7322 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7323 }
7324 tbl->tbl[tbl->pos++] = id;
7325}
7326#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7327
7328static void
7329vtable_pop_gen(struct parser_params *p, int line, const char *name,
7330 struct vtable *tbl, int n)
7331{
7332 if (p->debug) {
7333 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7334 line, name, (void *)tbl, n);
7335 }
7336 if (tbl->pos < n) {
7337 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7338 return;
7339 }
7340 tbl->pos -= n;
7341}
7342#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7343
7344static int
7345vtable_included(const struct vtable * tbl, ID id)
7346{
7347 int i;
7348
7349 if (!DVARS_TERMINAL_P(tbl)) {
7350 for (i = 0; i < tbl->pos; i++) {
7351 if (tbl->tbl[i] == id) {
7352 return i+1;
7353 }
7354 }
7355 }
7356 return 0;
7357}
7358
7359static void parser_prepare(struct parser_params *p);
7360
7361static int
7362e_option_supplied(struct parser_params *p)
7363{
7364 return strcmp(p->ruby_sourcefile, "-e") == 0;
7365}
7366
7367#ifndef RIPPER
7368static NODE *parser_append_options(struct parser_params *p, NODE *node);
7369
7370static VALUE
7371yycompile0(VALUE arg)
7372{
7373 int n;
7374 NODE *tree;
7375 struct parser_params *p = (struct parser_params *)arg;
7376 int cov = FALSE;
7377
7378 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7379 cov = TRUE;
7380 }
7381
7382 if (p->debug_lines) {
7383 p->ast->body.script_lines = p->debug_lines;
7384 }
7385
7386 parser_prepare(p);
7387#define RUBY_DTRACE_PARSE_HOOK(name) \
7388 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7389 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7390 }
7391 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7392 n = yyparse(p);
7393 RUBY_DTRACE_PARSE_HOOK(END);
7394
7395 p->debug_lines = 0;
7396
7397 xfree(p->lex.strterm);
7398 p->lex.strterm = 0;
7399 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7400 if (n || p->error_p) {
7401 VALUE mesg = p->error_buffer;
7402 if (!mesg) {
7403 mesg = syntax_error_new();
7404 }
7405 if (!p->error_tolerant) {
7406 rb_set_errinfo(mesg);
7407 return FALSE;
7408 }
7409 }
7410 tree = p->eval_tree;
7411 if (!tree) {
7412 tree = NEW_NIL(&NULL_LOC);
7413 }
7414 else {
7415 rb_parser_ary_t *tokens = p->tokens;
7416 NODE *prelude;
7417 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7418 prelude = block_append(p, p->eval_tree_begin, body);
7419 RNODE_SCOPE(tree)->nd_body = prelude;
7420 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7421 p->ast->body.coverage_enabled = cov;
7422 if (p->keep_tokens) {
7423 p->ast->node_buffer->tokens = tokens;
7424 p->tokens = NULL;
7425 }
7426 }
7427 p->ast->body.root = tree;
7428 p->ast->body.line_count = p->line_count;
7429 return TRUE;
7430}
7431
7432static rb_ast_t *
7433yycompile(struct parser_params *p, VALUE fname, int line)
7434{
7435 rb_ast_t *ast;
7436 if (NIL_P(fname)) {
7437 p->ruby_sourcefile_string = Qnil;
7438 p->ruby_sourcefile = "(none)";
7439 }
7440 else {
7441 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7442 p->ruby_sourcefile = StringValueCStr(fname);
7443 }
7444 p->ruby_sourceline = line - 1;
7445
7446 p->lvtbl = NULL;
7447
7448 p->ast = ast = rb_ast_new();
7449 compile_callback(yycompile0, (VALUE)p);
7450 p->ast = 0;
7451
7452 while (p->lvtbl) {
7453 local_pop(p);
7454 }
7455
7456 return ast;
7457}
7458#endif /* !RIPPER */
7459
7460static rb_encoding *
7461must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7462{
7463 rb_encoding *enc = rb_parser_str_get_encoding(s);
7464 if (!rb_enc_asciicompat(enc)) {
7465 rb_raise(rb_eArgError, "invalid source encoding");
7466 }
7467 return enc;
7468}
7469
7470static rb_parser_string_t *
7471lex_getline(struct parser_params *p)
7472{
7473 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7474 if (!line) return 0;
7475 p->line_count++;
7476 string_buffer_append(p, line);
7477 must_be_ascii_compatible(p, line);
7478 return line;
7479}
7480
7481#ifndef RIPPER
7482rb_ast_t*
7483rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7484{
7485 p->lex.gets = gets;
7486 p->lex.input = input;
7487 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7488
7489 return yycompile(p, fname, line);
7490}
7491#endif /* !RIPPER */
7492
7493#define STR_FUNC_ESCAPE 0x01
7494#define STR_FUNC_EXPAND 0x02
7495#define STR_FUNC_REGEXP 0x04
7496#define STR_FUNC_QWORDS 0x08
7497#define STR_FUNC_SYMBOL 0x10
7498#define STR_FUNC_INDENT 0x20
7499#define STR_FUNC_LABEL 0x40
7500#define STR_FUNC_LIST 0x4000
7501#define STR_FUNC_TERM 0x8000
7502
7503enum string_type {
7504 str_label = STR_FUNC_LABEL,
7505 str_squote = (0),
7506 str_dquote = (STR_FUNC_EXPAND),
7507 str_xquote = (STR_FUNC_EXPAND),
7508 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7509 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7510 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7511 str_ssym = (STR_FUNC_SYMBOL),
7512 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7513};
7514
7515static rb_parser_string_t *
7516parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7517{
7518 rb_parser_string_t *pstr;
7519
7520 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7521
7522 if (!(func & STR_FUNC_REGEXP)) {
7523 if (rb_parser_is_ascii_string(p, pstr)) {
7524 }
7525 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7526 /* everything is valid in ASCII-8BIT */
7527 enc = rb_ascii8bit_encoding();
7528 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7529 }
7530 }
7531
7532 return pstr;
7533}
7534
7535static int
7536strterm_is_heredoc(rb_strterm_t *strterm)
7537{
7538 return strterm->heredoc;
7539}
7540
7541static rb_strterm_t *
7542new_strterm(struct parser_params *p, int func, int term, int paren)
7543{
7544 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7545 strterm->u.literal.func = func;
7546 strterm->u.literal.term = term;
7547 strterm->u.literal.paren = paren;
7548 return strterm;
7549}
7550
7551static rb_strterm_t *
7552new_heredoc(struct parser_params *p)
7553{
7554 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7555 strterm->heredoc = true;
7556 return strterm;
7557}
7558
7559#define peek(p,c) peek_n(p, (c), 0)
7560#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7561#define peekc(p) peekc_n(p, 0)
7562#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7563
7564#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7565static void
7566parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7567{
7568 debug_token_line(p, "add_delayed_token", line);
7569
7570 if (tok < end) {
7571 if (has_delayed_token(p)) {
7572 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7573 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7574 int end_col = (next_line ? 0 : p->delayed.end_col);
7575 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7576 dispatch_delayed_token(p, tSTRING_CONTENT);
7577 }
7578 }
7579 if (!has_delayed_token(p)) {
7580 p->delayed.token = rb_parser_string_new(p, 0, 0);
7581 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7582 p->delayed.beg_line = p->ruby_sourceline;
7583 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7584 }
7585 parser_str_cat(p->delayed.token, tok, end - tok);
7586 p->delayed.end_line = p->ruby_sourceline;
7587 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7588 p->lex.ptok = end;
7589 }
7590}
7591
7592static void
7593set_lastline(struct parser_params *p, rb_parser_string_t *str)
7594{
7595 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7596 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7597 p->lex.lastline = str;
7598}
7599
7600static int
7601nextline(struct parser_params *p, int set_encoding)
7602{
7603 rb_parser_string_t *str = p->lex.nextline;
7604 p->lex.nextline = 0;
7605 if (!str) {
7606 if (p->eofp)
7607 return -1;
7608
7609 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7610 goto end_of_input;
7611 }
7612
7613 if (!p->lex.input || !(str = lex_getline(p))) {
7614 end_of_input:
7615 p->eofp = 1;
7616 lex_goto_eol(p);
7617 return -1;
7618 }
7619#ifndef RIPPER
7620 if (p->debug_lines) {
7621 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7622 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7623 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7624 }
7625#endif
7626 p->cr_seen = FALSE;
7627 }
7628 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7629 /* after here-document without terminator */
7630 goto end_of_input;
7631 }
7632 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7633 if (p->heredoc_end > 0) {
7634 p->ruby_sourceline = p->heredoc_end;
7635 p->heredoc_end = 0;
7636 }
7637 p->ruby_sourceline++;
7638 set_lastline(p, str);
7639 token_flush(p);
7640 return 0;
7641}
7642
7643static int
7644parser_cr(struct parser_params *p, int c)
7645{
7646 if (peek(p, '\n')) {
7647 p->lex.pcur++;
7648 c = '\n';
7649 }
7650 return c;
7651}
7652
7653static inline int
7654nextc0(struct parser_params *p, int set_encoding)
7655{
7656 int c;
7657
7658 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7659 if (nextline(p, set_encoding)) return -1;
7660 }
7661 c = (unsigned char)*p->lex.pcur++;
7662 if (UNLIKELY(c == '\r')) {
7663 c = parser_cr(p, c);
7664 }
7665
7666 return c;
7667}
7668#define nextc(p) nextc0(p, TRUE)
7669
7670static void
7671pushback(struct parser_params *p, int c)
7672{
7673 if (c == -1) return;
7674 p->eofp = 0;
7675 p->lex.pcur--;
7676 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7677 p->lex.pcur--;
7678 }
7679}
7680
7681#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7682
7683#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7684#define tok(p) (p)->tokenbuf
7685#define toklen(p) (p)->tokidx
7686
7687static int
7688looking_at_eol_p(struct parser_params *p)
7689{
7690 const char *ptr = p->lex.pcur;
7691 while (!lex_eol_ptr_p(p, ptr)) {
7692 int c = (unsigned char)*ptr++;
7693 int eol = (c == '\n' || c == '#');
7694 if (eol || !ISSPACE(c)) {
7695 return eol;
7696 }
7697 }
7698 return TRUE;
7699}
7700
7701static char*
7702newtok(struct parser_params *p)
7703{
7704 p->tokidx = 0;
7705 if (!p->tokenbuf) {
7706 p->toksiz = 60;
7707 p->tokenbuf = ALLOC_N(char, 60);
7708 }
7709 if (p->toksiz > 4096) {
7710 p->toksiz = 60;
7711 REALLOC_N(p->tokenbuf, char, 60);
7712 }
7713 return p->tokenbuf;
7714}
7715
7716static char *
7717tokspace(struct parser_params *p, int n)
7718{
7719 p->tokidx += n;
7720
7721 if (p->tokidx >= p->toksiz) {
7722 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7723 REALLOC_N(p->tokenbuf, char, p->toksiz);
7724 }
7725 return &p->tokenbuf[p->tokidx-n];
7726}
7727
7728static void
7729tokadd(struct parser_params *p, int c)
7730{
7731 p->tokenbuf[p->tokidx++] = (char)c;
7732 if (p->tokidx >= p->toksiz) {
7733 p->toksiz *= 2;
7734 REALLOC_N(p->tokenbuf, char, p->toksiz);
7735 }
7736}
7737
7738static int
7739tok_hex(struct parser_params *p, size_t *numlen)
7740{
7741 int c;
7742
7743 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7744 if (!*numlen) {
7745 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7746 yyerror0("invalid hex escape");
7747 dispatch_scan_event(p, tSTRING_CONTENT);
7748 return 0;
7749 }
7750 p->lex.pcur += *numlen;
7751 return c;
7752}
7753
7754#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7755
7756static int
7757escaped_control_code(int c)
7758{
7759 int c2 = 0;
7760 switch (c) {
7761 case ' ':
7762 c2 = 's';
7763 break;
7764 case '\n':
7765 c2 = 'n';
7766 break;
7767 case '\t':
7768 c2 = 't';
7769 break;
7770 case '\v':
7771 c2 = 'v';
7772 break;
7773 case '\r':
7774 c2 = 'r';
7775 break;
7776 case '\f':
7777 c2 = 'f';
7778 break;
7779 }
7780 return c2;
7781}
7782
7783#define WARN_SPACE_CHAR(c, prefix) \
7784 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7785
7786static int
7787tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7788 int regexp_literal, const char *begin)
7789{
7790 const int wide = !begin;
7791 size_t numlen;
7792 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7793
7794 p->lex.pcur += numlen;
7795 if (p->lex.strterm == NULL ||
7796 strterm_is_heredoc(p->lex.strterm) ||
7797 (p->lex.strterm->u.literal.func != str_regexp)) {
7798 if (!begin) begin = p->lex.pcur;
7799 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7800 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7801 yyerror0("invalid Unicode escape");
7802 dispatch_scan_event(p, tSTRING_CONTENT);
7803 return wide && numlen > 0;
7804 }
7805 if (codepoint > 0x10ffff) {
7806 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7807 yyerror0("invalid Unicode codepoint (too large)");
7808 dispatch_scan_event(p, tSTRING_CONTENT);
7809 return wide;
7810 }
7811 if ((codepoint & 0xfffff800) == 0xd800) {
7812 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7813 yyerror0("invalid Unicode codepoint");
7814 dispatch_scan_event(p, tSTRING_CONTENT);
7815 return wide;
7816 }
7817 }
7818 if (regexp_literal) {
7819 tokcopy(p, (int)numlen);
7820 }
7821 else if (codepoint >= 0x80) {
7822 rb_encoding *utf8 = rb_utf8_encoding();
7823 if (*encp && utf8 != *encp) {
7824 YYLTYPE loc = RUBY_INIT_YYLLOC();
7825 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7826 parser_show_error_line(p, &loc);
7827 return wide;
7828 }
7829 *encp = utf8;
7830 tokaddmbc(p, codepoint, *encp);
7831 }
7832 else {
7833 tokadd(p, codepoint);
7834 }
7835 return TRUE;
7836}
7837
7838static int tokadd_mbchar(struct parser_params *p, int c);
7839
7840static int
7841tokskip_mbchar(struct parser_params *p)
7842{
7843 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7844 if (len > 0) {
7845 p->lex.pcur += len - 1;
7846 }
7847 return len;
7848}
7849
7850/* return value is for ?\u3042 */
7851static void
7852tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7853 int term, int symbol_literal, int regexp_literal)
7854{
7855 /*
7856 * If `term` is not -1, then we allow multiple codepoints in \u{}
7857 * upto `term` byte, otherwise we're parsing a character literal.
7858 * And then add the codepoints to the current token.
7859 */
7860 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7861
7862 const int open_brace = '{', close_brace = '}';
7863
7864 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7865
7866 if (peek(p, open_brace)) { /* handle \u{...} form */
7867 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7868 /*
7869 * Skip parsing validation code and copy bytes as-is until term or
7870 * closing brace, in order to correctly handle extended regexps where
7871 * invalid unicode escapes are allowed in comments. The regexp parser
7872 * does its own validation and will catch any issues.
7873 */
7874 tokadd(p, open_brace);
7875 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7876 int c = peekc(p);
7877 if (c == close_brace) {
7878 tokadd(p, c);
7879 ++p->lex.pcur;
7880 break;
7881 }
7882 else if (c == term) {
7883 break;
7884 }
7885 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7886 tokadd(p, c);
7887 c = *++p->lex.pcur;
7888 }
7889 tokadd_mbchar(p, c);
7890 }
7891 }
7892 else {
7893 const char *second = NULL;
7894 int c, last = nextc(p);
7895 if (lex_eol_p(p)) goto unterminated;
7896 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7897 while (c != close_brace) {
7898 if (c == term) goto unterminated;
7899 if (second == multiple_codepoints)
7900 second = p->lex.pcur;
7901 if (regexp_literal) tokadd(p, last);
7902 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7903 break;
7904 }
7905 while (ISSPACE(c = peekc(p))) {
7906 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7907 last = c;
7908 }
7909 if (term == -1 && !second)
7910 second = multiple_codepoints;
7911 }
7912
7913 if (c != close_brace) {
7914 unterminated:
7915 flush_string_content(p, rb_utf8_encoding(), 0);
7916 yyerror0("unterminated Unicode escape");
7917 dispatch_scan_event(p, tSTRING_CONTENT);
7918 return;
7919 }
7920 if (second && second != multiple_codepoints) {
7921 const char *pcur = p->lex.pcur;
7922 p->lex.pcur = second;
7923 dispatch_scan_event(p, tSTRING_CONTENT);
7924 token_flush(p);
7925 p->lex.pcur = pcur;
7926 yyerror0(multiple_codepoints);
7927 token_flush(p);
7928 }
7929
7930 if (regexp_literal) tokadd(p, close_brace);
7931 nextc(p);
7932 }
7933 }
7934 else { /* handle \uxxxx form */
7935 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
7936 token_flush(p);
7937 return;
7938 }
7939 }
7940}
7941
7942#define ESCAPE_CONTROL 1
7943#define ESCAPE_META 2
7944
7945static int
7946read_escape(struct parser_params *p, int flags, const char *begin)
7947{
7948 int c;
7949 size_t numlen;
7950
7951 switch (c = nextc(p)) {
7952 case '\\': /* Backslash */
7953 return c;
7954
7955 case 'n': /* newline */
7956 return '\n';
7957
7958 case 't': /* horizontal tab */
7959 return '\t';
7960
7961 case 'r': /* carriage-return */
7962 return '\r';
7963
7964 case 'f': /* form-feed */
7965 return '\f';
7966
7967 case 'v': /* vertical tab */
7968 return '\13';
7969
7970 case 'a': /* alarm(bell) */
7971 return '\007';
7972
7973 case 'e': /* escape */
7974 return 033;
7975
7976 case '0': case '1': case '2': case '3': /* octal constant */
7977 case '4': case '5': case '6': case '7':
7978 pushback(p, c);
7979 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
7980 p->lex.pcur += numlen;
7981 return c;
7982
7983 case 'x': /* hex constant */
7984 c = tok_hex(p, &numlen);
7985 if (numlen == 0) return 0;
7986 return c;
7987
7988 case 'b': /* backspace */
7989 return '\010';
7990
7991 case 's': /* space */
7992 return ' ';
7993
7994 case 'M':
7995 if (flags & ESCAPE_META) goto eof;
7996 if ((c = nextc(p)) != '-') {
7997 goto eof;
7998 }
7999 if ((c = nextc(p)) == '\\') {
8000 switch (peekc(p)) {
8001 case 'u': case 'U':
8002 nextc(p);
8003 goto eof;
8004 }
8005 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8006 }
8007 else if (c == -1) goto eof;
8008 else if (!ISASCII(c)) {
8009 tokskip_mbchar(p);
8010 goto eof;
8011 }
8012 else {
8013 int c2 = escaped_control_code(c);
8014 if (c2) {
8015 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8016 WARN_SPACE_CHAR(c2, "\\M-");
8017 }
8018 else {
8019 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8020 }
8021 }
8022 else if (ISCNTRL(c)) goto eof;
8023 return ((c & 0xff) | 0x80);
8024 }
8025
8026 case 'C':
8027 if ((c = nextc(p)) != '-') {
8028 goto eof;
8029 }
8030 case 'c':
8031 if (flags & ESCAPE_CONTROL) goto eof;
8032 if ((c = nextc(p))== '\\') {
8033 switch (peekc(p)) {
8034 case 'u': case 'U':
8035 nextc(p);
8036 goto eof;
8037 }
8038 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8039 }
8040 else if (c == '?')
8041 return 0177;
8042 else if (c == -1) goto eof;
8043 else if (!ISASCII(c)) {
8044 tokskip_mbchar(p);
8045 goto eof;
8046 }
8047 else {
8048 int c2 = escaped_control_code(c);
8049 if (c2) {
8050 if (ISCNTRL(c)) {
8051 if (flags & ESCAPE_META) {
8052 WARN_SPACE_CHAR(c2, "\\M-");
8053 }
8054 else {
8055 WARN_SPACE_CHAR(c2, "");
8056 }
8057 }
8058 else {
8059 if (flags & ESCAPE_META) {
8060 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8061 }
8062 else {
8063 WARN_SPACE_CHAR(c2, "\\C-");
8064 }
8065 }
8066 }
8067 else if (ISCNTRL(c)) goto eof;
8068 }
8069 return c & 0x9f;
8070
8071 eof:
8072 case -1:
8073 flush_string_content(p, p->enc, p->lex.pcur - begin);
8074 yyerror0("Invalid escape character syntax");
8075 dispatch_scan_event(p, tSTRING_CONTENT);
8076 return '\0';
8077
8078 default:
8079 if (!ISASCII(c)) {
8080 tokskip_mbchar(p);
8081 goto eof;
8082 }
8083 return c;
8084 }
8085}
8086
8087static void
8088tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8089{
8090 int len = rb_enc_codelen(c, enc);
8091 rb_enc_mbcput(c, tokspace(p, len), enc);
8092}
8093
8094static int
8095tokadd_escape(struct parser_params *p)
8096{
8097 int c;
8098 size_t numlen;
8099 const char *begin = p->lex.pcur;
8100
8101 switch (c = nextc(p)) {
8102 case '\n':
8103 return 0; /* just ignore */
8104
8105 case '0': case '1': case '2': case '3': /* octal constant */
8106 case '4': case '5': case '6': case '7':
8107 {
8108 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8109 if (numlen == 0) goto eof;
8110 p->lex.pcur += numlen;
8111 tokcopy(p, (int)numlen + 1);
8112 }
8113 return 0;
8114
8115 case 'x': /* hex constant */
8116 {
8117 tok_hex(p, &numlen);
8118 if (numlen == 0) return -1;
8119 tokcopy(p, (int)numlen + 2);
8120 }
8121 return 0;
8122
8123 eof:
8124 case -1:
8125 flush_string_content(p, p->enc, p->lex.pcur - begin);
8126 yyerror0("Invalid escape character syntax");
8127 token_flush(p);
8128 return -1;
8129
8130 default:
8131 tokadd(p, '\\');
8132 tokadd(p, c);
8133 }
8134 return 0;
8135}
8136
8137static int
8138char_to_option(int c)
8139{
8140 int val;
8141
8142 switch (c) {
8143 case 'i':
8144 val = RE_ONIG_OPTION_IGNORECASE;
8145 break;
8146 case 'x':
8147 val = RE_ONIG_OPTION_EXTEND;
8148 break;
8149 case 'm':
8150 val = RE_ONIG_OPTION_MULTILINE;
8151 break;
8152 default:
8153 val = 0;
8154 break;
8155 }
8156 return val;
8157}
8158
8159#define ARG_ENCODING_FIXED 16
8160#define ARG_ENCODING_NONE 32
8161#define ENC_ASCII8BIT 1
8162#define ENC_EUC_JP 2
8163#define ENC_Windows_31J 3
8164#define ENC_UTF8 4
8165
8166static int
8167char_to_option_kcode(int c, int *option, int *kcode)
8168{
8169 *option = 0;
8170
8171 switch (c) {
8172 case 'n':
8173 *kcode = ENC_ASCII8BIT;
8174 return (*option = ARG_ENCODING_NONE);
8175 case 'e':
8176 *kcode = ENC_EUC_JP;
8177 break;
8178 case 's':
8179 *kcode = ENC_Windows_31J;
8180 break;
8181 case 'u':
8182 *kcode = ENC_UTF8;
8183 break;
8184 default:
8185 *kcode = -1;
8186 return (*option = char_to_option(c));
8187 }
8188 *option = ARG_ENCODING_FIXED;
8189 return 1;
8190}
8191
8192static int
8193regx_options(struct parser_params *p)
8194{
8195 int kcode = 0;
8196 int kopt = 0;
8197 int options = 0;
8198 int c, opt, kc;
8199
8200 newtok(p);
8201 while (c = nextc(p), ISALPHA(c)) {
8202 if (c == 'o') {
8203 options |= RE_OPTION_ONCE;
8204 }
8205 else if (char_to_option_kcode(c, &opt, &kc)) {
8206 if (kc >= 0) {
8207 if (kc != ENC_ASCII8BIT) kcode = c;
8208 kopt = opt;
8209 }
8210 else {
8211 options |= opt;
8212 }
8213 }
8214 else {
8215 tokadd(p, c);
8216 }
8217 }
8218 options |= kopt;
8219 pushback(p, c);
8220 if (toklen(p)) {
8221 YYLTYPE loc = RUBY_INIT_YYLLOC();
8222 tokfix(p);
8223 compile_error(p, "unknown regexp option%s - %*s",
8224 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8225 parser_show_error_line(p, &loc);
8226 }
8227 return options | RE_OPTION_ENCODING(kcode);
8228}
8229
8230static int
8231tokadd_mbchar(struct parser_params *p, int c)
8232{
8233 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8234 if (len < 0) return -1;
8235 tokadd(p, c);
8236 p->lex.pcur += --len;
8237 if (len > 0) tokcopy(p, len);
8238 return c;
8239}
8240
8241static inline int
8242simple_re_meta(int c)
8243{
8244 switch (c) {
8245 case '$': case '*': case '+': case '.':
8246 case '?': case '^': case '|':
8247 case ')': case ']': case '}': case '>':
8248 return TRUE;
8249 default:
8250 return FALSE;
8251 }
8252}
8253
8254static int
8255parser_update_heredoc_indent(struct parser_params *p, int c)
8256{
8257 if (p->heredoc_line_indent == -1) {
8258 if (c == '\n') p->heredoc_line_indent = 0;
8259 }
8260 else {
8261 if (c == ' ') {
8262 p->heredoc_line_indent++;
8263 return TRUE;
8264 }
8265 else if (c == '\t') {
8266 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8267 p->heredoc_line_indent = w * TAB_WIDTH;
8268 return TRUE;
8269 }
8270 else if (c != '\n') {
8271 if (p->heredoc_indent > p->heredoc_line_indent) {
8272 p->heredoc_indent = p->heredoc_line_indent;
8273 }
8274 p->heredoc_line_indent = -1;
8275 }
8276 else {
8277 /* Whitespace only line has no indentation */
8278 p->heredoc_line_indent = 0;
8279 }
8280 }
8281 return FALSE;
8282}
8283
8284static void
8285parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8286{
8287 YYLTYPE loc = RUBY_INIT_YYLLOC();
8288 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8289 compile_error(p, "%s mixed within %s source", n1, n2);
8290 parser_show_error_line(p, &loc);
8291}
8292
8293static void
8294parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8295{
8296 const char *pos = p->lex.pcur;
8297 p->lex.pcur = beg;
8298 parser_mixed_error(p, enc1, enc2);
8299 p->lex.pcur = pos;
8300}
8301
8302static inline char
8303nibble_char_upper(unsigned int c)
8304{
8305 c &= 0xf;
8306 return c + (c < 10 ? '0' : 'A' - 10);
8307}
8308
8309static int
8310tokadd_string(struct parser_params *p,
8311 int func, int term, int paren, long *nest,
8312 rb_encoding **encp, rb_encoding **enc)
8313{
8314 int c;
8315 bool erred = false;
8316#ifdef RIPPER
8317 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8318 int top_of_line = FALSE;
8319#endif
8320
8321#define mixed_error(enc1, enc2) \
8322 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8323#define mixed_escape(beg, enc1, enc2) \
8324 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8325
8326 while ((c = nextc(p)) != -1) {
8327 if (p->heredoc_indent > 0) {
8328 parser_update_heredoc_indent(p, c);
8329 }
8330#ifdef RIPPER
8331 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8332 pushback(p, c);
8333 break;
8334 }
8335#endif
8336
8337 if (paren && c == paren) {
8338 ++*nest;
8339 }
8340 else if (c == term) {
8341 if (!nest || !*nest) {
8342 pushback(p, c);
8343 break;
8344 }
8345 --*nest;
8346 }
8347 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8348 unsigned char c2 = *p->lex.pcur;
8349 if (c2 == '$' || c2 == '@' || c2 == '{') {
8350 pushback(p, c);
8351 break;
8352 }
8353 }
8354 else if (c == '\\') {
8355 c = nextc(p);
8356 switch (c) {
8357 case '\n':
8358 if (func & STR_FUNC_QWORDS) break;
8359 if (func & STR_FUNC_EXPAND) {
8360 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8361 continue;
8362 if (c == term) {
8363 c = '\\';
8364 goto terminate;
8365 }
8366 }
8367 tokadd(p, '\\');
8368 break;
8369
8370 case '\\':
8371 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8372 break;
8373
8374 case 'u':
8375 if ((func & STR_FUNC_EXPAND) == 0) {
8376 tokadd(p, '\\');
8377 break;
8378 }
8379 tokadd_utf8(p, enc, term,
8380 func & STR_FUNC_SYMBOL,
8381 func & STR_FUNC_REGEXP);
8382 continue;
8383
8384 default:
8385 if (c == -1) return -1;
8386 if (!ISASCII(c)) {
8387 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8388 goto non_ascii;
8389 }
8390 if (func & STR_FUNC_REGEXP) {
8391 switch (c) {
8392 case 'c':
8393 case 'C':
8394 case 'M': {
8395 pushback(p, c);
8396 c = read_escape(p, 0, p->lex.pcur - 1);
8397
8398 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8399 *t++ = '\\';
8400 *t++ = 'x';
8401 *t++ = nibble_char_upper(c >> 4);
8402 *t++ = nibble_char_upper(c);
8403 continue;
8404 }
8405 }
8406
8407 if (c == term && !simple_re_meta(c)) {
8408 tokadd(p, c);
8409 continue;
8410 }
8411 pushback(p, c);
8412 if ((c = tokadd_escape(p)) < 0)
8413 return -1;
8414 if (*enc && *enc != *encp) {
8415 mixed_escape(p->lex.ptok+2, *enc, *encp);
8416 }
8417 continue;
8418 }
8419 else if (func & STR_FUNC_EXPAND) {
8420 pushback(p, c);
8421 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8422 c = read_escape(p, 0, p->lex.pcur - 1);
8423 }
8424 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8425 /* ignore backslashed spaces in %w */
8426 }
8427 else if (c != term && !(paren && c == paren)) {
8428 tokadd(p, '\\');
8429 pushback(p, c);
8430 continue;
8431 }
8432 }
8433 }
8434 else if (!parser_isascii(p)) {
8435 non_ascii:
8436 if (!*enc) {
8437 *enc = *encp;
8438 }
8439 else if (*enc != *encp) {
8440 mixed_error(*enc, *encp);
8441 continue;
8442 }
8443 if (tokadd_mbchar(p, c) == -1) return -1;
8444 continue;
8445 }
8446 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8447 pushback(p, c);
8448 break;
8449 }
8450 if (c & 0x80) {
8451 if (!*enc) {
8452 *enc = *encp;
8453 }
8454 else if (*enc != *encp) {
8455 mixed_error(*enc, *encp);
8456 continue;
8457 }
8458 }
8459 tokadd(p, c);
8460#ifdef RIPPER
8461 top_of_line = (c == '\n');
8462#endif
8463 }
8464 terminate:
8465 if (*enc) *encp = *enc;
8466 return c;
8467}
8468
8469#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8470
8471static void
8472flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8473{
8474 p->lex.pcur -= back;
8475 if (has_delayed_token(p)) {
8476 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8477 if (len > 0) {
8478 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8479 p->delayed.end_line = p->ruby_sourceline;
8480 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8481 }
8482 dispatch_delayed_token(p, tSTRING_CONTENT);
8483 p->lex.ptok = p->lex.pcur;
8484 }
8485 dispatch_scan_event(p, tSTRING_CONTENT);
8486 p->lex.pcur += back;
8487}
8488
8489/* this can be shared with ripper, since it's independent from struct
8490 * parser_params. */
8491#ifndef RIPPER
8492#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8493#define SPECIAL_PUNCT(idx) ( \
8494 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8495 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8496 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8497 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8498 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8499 BIT('0', idx))
8500const uint_least32_t ruby_global_name_punct_bits[] = {
8501 SPECIAL_PUNCT(0),
8502 SPECIAL_PUNCT(1),
8503 SPECIAL_PUNCT(2),
8504};
8505#undef BIT
8506#undef SPECIAL_PUNCT
8507#endif
8508
8509static enum yytokentype
8510parser_peek_variable_name(struct parser_params *p)
8511{
8512 int c;
8513 const char *ptr = p->lex.pcur;
8514
8515 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8516 c = *ptr++;
8517 switch (c) {
8518 case '$':
8519 if ((c = *ptr) == '-') {
8520 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8521 c = *ptr;
8522 }
8523 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8524 return tSTRING_DVAR;
8525 }
8526 break;
8527 case '@':
8528 if ((c = *ptr) == '@') {
8529 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8530 c = *ptr;
8531 }
8532 break;
8533 case '{':
8534 p->lex.pcur = ptr;
8535 p->command_start = TRUE;
8536 yylval.state = p->lex.state;
8537 return tSTRING_DBEG;
8538 default:
8539 return 0;
8540 }
8541 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8542 return tSTRING_DVAR;
8543 return 0;
8544}
8545
8546#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8547#define IS_END() IS_lex_state(EXPR_END_ANY)
8548#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8549#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8550#define IS_LABEL_POSSIBLE() (\
8551 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8552 IS_ARG())
8553#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8554#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8555
8556static inline enum yytokentype
8557parser_string_term(struct parser_params *p, int func)
8558{
8559 xfree(p->lex.strterm);
8560 p->lex.strterm = 0;
8561 if (func & STR_FUNC_REGEXP) {
8562 set_yylval_num(regx_options(p));
8563 dispatch_scan_event(p, tREGEXP_END);
8564 SET_LEX_STATE(EXPR_END);
8565 return tREGEXP_END;
8566 }
8567 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8568 nextc(p);
8569 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8570 return tLABEL_END;
8571 }
8572 SET_LEX_STATE(EXPR_END);
8573 return tSTRING_END;
8574}
8575
8576static enum yytokentype
8577parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8578{
8579 int func = quote->func;
8580 int term = quote->term;
8581 int paren = quote->paren;
8582 int c, space = 0;
8583 rb_encoding *enc = p->enc;
8584 rb_encoding *base_enc = 0;
8585 rb_parser_string_t *lit;
8586
8587 if (func & STR_FUNC_TERM) {
8588 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8589 SET_LEX_STATE(EXPR_END);
8590 xfree(p->lex.strterm);
8591 p->lex.strterm = 0;
8592 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8593 }
8594 c = nextc(p);
8595 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8596 while (c != '\n' && ISSPACE(c = nextc(p)));
8597 space = 1;
8598 }
8599 if (func & STR_FUNC_LIST) {
8600 quote->func &= ~STR_FUNC_LIST;
8601 space = 1;
8602 }
8603 if (c == term && !quote->nest) {
8604 if (func & STR_FUNC_QWORDS) {
8605 quote->func |= STR_FUNC_TERM;
8606 pushback(p, c); /* dispatch the term at tSTRING_END */
8607 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8608 return ' ';
8609 }
8610 return parser_string_term(p, func);
8611 }
8612 if (space) {
8613 if (!ISSPACE(c)) pushback(p, c);
8614 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8615 return ' ';
8616 }
8617 newtok(p);
8618 if ((func & STR_FUNC_EXPAND) && c == '#') {
8619 enum yytokentype t = parser_peek_variable_name(p);
8620 if (t) return t;
8621 tokadd(p, '#');
8622 c = nextc(p);
8623 }
8624 pushback(p, c);
8625 if (tokadd_string(p, func, term, paren, &quote->nest,
8626 &enc, &base_enc) == -1) {
8627 if (p->eofp) {
8628#ifndef RIPPER
8629# define unterminated_literal(mesg) yyerror0(mesg)
8630#else
8631# define unterminated_literal(mesg) compile_error(p, mesg)
8632#endif
8633 literal_flush(p, p->lex.pcur);
8634 if (func & STR_FUNC_QWORDS) {
8635 /* no content to add, bailing out here */
8636 unterminated_literal("unterminated list meets end of file");
8637 xfree(p->lex.strterm);
8638 p->lex.strterm = 0;
8639 return tSTRING_END;
8640 }
8641 if (func & STR_FUNC_REGEXP) {
8642 unterminated_literal("unterminated regexp meets end of file");
8643 }
8644 else {
8645 unterminated_literal("unterminated string meets end of file");
8646 }
8647 quote->func |= STR_FUNC_TERM;
8648 }
8649 }
8650
8651 tokfix(p);
8652 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8653 set_yylval_str(lit);
8654 flush_string_content(p, enc, 0);
8655
8656 return tSTRING_CONTENT;
8657}
8658
8659static enum yytokentype
8660heredoc_identifier(struct parser_params *p)
8661{
8662 /*
8663 * term_len is length of `<<"END"` except `END`,
8664 * in this case term_len is 4 (<, <, " and ").
8665 */
8666 long len, offset = p->lex.pcur - p->lex.pbeg;
8667 int c = nextc(p), term, func = 0, quote = 0;
8668 enum yytokentype token = tSTRING_BEG;
8669 int indent = 0;
8670
8671 if (c == '-') {
8672 c = nextc(p);
8673 func = STR_FUNC_INDENT;
8674 offset++;
8675 }
8676 else if (c == '~') {
8677 c = nextc(p);
8678 func = STR_FUNC_INDENT;
8679 offset++;
8680 indent = INT_MAX;
8681 }
8682 switch (c) {
8683 case '\'':
8684 func |= str_squote; goto quoted;
8685 case '"':
8686 func |= str_dquote; goto quoted;
8687 case '`':
8688 token = tXSTRING_BEG;
8689 func |= str_xquote; goto quoted;
8690
8691 quoted:
8692 quote++;
8693 offset++;
8694 term = c;
8695 len = 0;
8696 while ((c = nextc(p)) != term) {
8697 if (c == -1 || c == '\r' || c == '\n') {
8698 yyerror0("unterminated here document identifier");
8699 return -1;
8700 }
8701 }
8702 break;
8703
8704 default:
8705 if (!parser_is_identchar(p)) {
8706 pushback(p, c);
8707 if (func & STR_FUNC_INDENT) {
8708 pushback(p, indent > 0 ? '~' : '-');
8709 }
8710 return 0;
8711 }
8712 func |= str_dquote;
8713 do {
8714 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8715 if (n < 0) return 0;
8716 p->lex.pcur += --n;
8717 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8718 pushback(p, c);
8719 break;
8720 }
8721
8722 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8723 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8724 yyerror0("too long here document identifier");
8725 dispatch_scan_event(p, tHEREDOC_BEG);
8726 lex_goto_eol(p);
8727
8728 p->lex.strterm = new_heredoc(p);
8729 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8730 here->offset = offset;
8731 here->sourceline = p->ruby_sourceline;
8732 here->length = (unsigned)len;
8733 here->quote = quote;
8734 here->func = func;
8735 here->lastline = p->lex.lastline;
8736
8737 token_flush(p);
8738 p->heredoc_indent = indent;
8739 p->heredoc_line_indent = 0;
8740 return token;
8741}
8742
8743static void
8744heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8745{
8746 rb_parser_string_t *line;
8747 rb_strterm_t *term = p->lex.strterm;
8748
8749 p->lex.strterm = 0;
8750 line = here->lastline;
8751 p->lex.lastline = line;
8752 p->lex.pbeg = PARSER_STRING_PTR(line);
8753 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8754 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8755 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8756 p->heredoc_end = p->ruby_sourceline;
8757 p->ruby_sourceline = (int)here->sourceline;
8758 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8759 p->eofp = 0;
8760 xfree(term);
8761}
8762
8763static int
8764dedent_string_column(const char *str, long len, int width)
8765{
8766 int i, col = 0;
8767
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
8782 return i;
8783}
8784
8785static int
8786dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8787{
8788 char *str;
8789 long len;
8790 int i;
8791
8792 len = PARSER_STRING_LEN(string);
8793 str = PARSER_STRING_PTR(string);
8794
8795 i = dedent_string_column(str, len, width);
8796 if (!i) return 0;
8797
8798 rb_parser_str_modify(string);
8799 str = PARSER_STRING_PTR(string);
8800 if (PARSER_STRING_LEN(string) != len)
8801 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8802 MEMMOVE(str, str + i, char, len - i);
8803 rb_parser_str_set_len(p, string, len - i);
8804 return i;
8805}
8806
8807static NODE *
8808heredoc_dedent(struct parser_params *p, NODE *root)
8809{
8810 NODE *node, *str_node, *prev_node;
8811 int indent = p->heredoc_indent;
8812 rb_parser_string_t *prev_lit = 0;
8813
8814 if (indent <= 0) return root;
8815 if (!root) return root;
8816
8817 prev_node = node = str_node = root;
8818 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8819
8820 while (str_node) {
8821 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8822 if (nd_fl_newline(str_node)) {
8823 dedent_string(p, lit, indent);
8824 }
8825 if (!prev_lit) {
8826 prev_lit = lit;
8827 }
8828 else if (!literal_concat0(p, prev_lit, lit)) {
8829 return 0;
8830 }
8831 else {
8832 NODE *end = RNODE_LIST(node)->as.nd_end;
8833 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8834 if (!node) {
8835 if (nd_type_p(prev_node, NODE_DSTR))
8836 nd_set_type(prev_node, NODE_STR);
8837 break;
8838 }
8839 RNODE_LIST(node)->as.nd_end = end;
8840 goto next_str;
8841 }
8842
8843 str_node = 0;
8844 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8845 next_str:
8846 if (!nd_type_p(node, NODE_LIST)) break;
8847 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8848 enum node_type type = nd_type(str_node);
8849 if (type == NODE_STR || type == NODE_DSTR) break;
8850 prev_lit = 0;
8851 str_node = 0;
8852 }
8853 }
8854 }
8855 return root;
8856}
8857
8858static int
8859whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8860{
8861 const char *beg = p->lex.pbeg;
8862 const char *ptr = p->lex.pend;
8863
8864 if (ptr - beg < len) return FALSE;
8865 if (ptr > beg && ptr[-1] == '\n') {
8866 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8867 if (ptr - beg < len) return FALSE;
8868 }
8869 if (strncmp(eos, ptr -= len, len)) return FALSE;
8870 if (indent) {
8871 while (beg < ptr && ISSPACE(*beg)) beg++;
8872 }
8873 return beg == ptr;
8874}
8875
8876static int
8877word_match_p(struct parser_params *p, const char *word, long len)
8878{
8879 if (strncmp(p->lex.pcur, word, len)) return 0;
8880 if (lex_eol_n_p(p, len)) return 1;
8881 int c = (unsigned char)p->lex.pcur[len];
8882 if (ISSPACE(c)) return 1;
8883 switch (c) {
8884 case '\0': case '\004': case '\032': return 1;
8885 }
8886 return 0;
8887}
8888
8889#define NUM_SUFFIX_R (1<<0)
8890#define NUM_SUFFIX_I (1<<1)
8891#define NUM_SUFFIX_ALL 3
8892
8893static int
8894number_literal_suffix(struct parser_params *p, int mask)
8895{
8896 int c, result = 0;
8897 const char *lastp = p->lex.pcur;
8898
8899 while ((c = nextc(p)) != -1) {
8900 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8901 result |= (mask & NUM_SUFFIX_I);
8902 mask &= ~NUM_SUFFIX_I;
8903 /* r after i, rational of complex is disallowed */
8904 mask &= ~NUM_SUFFIX_R;
8905 continue;
8906 }
8907 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8908 result |= (mask & NUM_SUFFIX_R);
8909 mask &= ~NUM_SUFFIX_R;
8910 continue;
8911 }
8912 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8913 p->lex.pcur = lastp;
8914 literal_flush(p, p->lex.pcur);
8915 return 0;
8916 }
8917 pushback(p, c);
8918 break;
8919 }
8920 return result;
8921}
8922
8923static enum yytokentype
8924set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8925{
8926 enum rb_numeric_type numeric_type = integer_literal;
8927
8928 if (type == tFLOAT) {
8929 numeric_type = float_literal;
8930 }
8931
8932 if (suffix & NUM_SUFFIX_R) {
8933 type = tRATIONAL;
8934 numeric_type = rational_literal;
8935 }
8936 if (suffix & NUM_SUFFIX_I) {
8937 type = tIMAGINARY;
8938 }
8939
8940 switch (type) {
8941 case tINTEGER:
8942 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
8943 break;
8944 case tFLOAT:
8945 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
8946 break;
8947 case tRATIONAL:
8948 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
8949 break;
8950 case tIMAGINARY:
8951 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
8952 (void)numeric_type; /* for ripper */
8953 break;
8954 default:
8955 rb_bug("unexpected token: %d", type);
8956 }
8957 SET_LEX_STATE(EXPR_END);
8958 return type;
8959}
8960
8961#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8962static void
8963parser_dispatch_heredoc_end(struct parser_params *p, int line)
8964{
8965 if (has_delayed_token(p))
8966 dispatch_delayed_token(p, tSTRING_CONTENT);
8967
8968#ifdef RIPPER
8969 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8970 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8971#else
8972 if (p->keep_tokens) {
8973 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
8974 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8975 parser_append_tokens(p, str, tHEREDOC_END, line);
8976 }
8977#endif
8978
8979 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8980 lex_goto_eol(p);
8981 token_flush(p);
8982}
8983
8984static enum yytokentype
8985here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
8986{
8987 int c, func, indent = 0;
8988 const char *eos, *ptr, *ptr_end;
8989 long len;
8990 rb_parser_string_t *str = 0;
8991 rb_encoding *enc = p->enc;
8992 rb_encoding *base_enc = 0;
8993 int bol;
8994#ifdef RIPPER
8995 VALUE s_value;
8996#endif
8997
8998 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
8999 len = here->length;
9000 indent = (func = here->func) & STR_FUNC_INDENT;
9001
9002 if ((c = nextc(p)) == -1) {
9003 error:
9004#ifdef RIPPER
9005 if (!has_delayed_token(p)) {
9006 dispatch_scan_event(p, tSTRING_CONTENT);
9007 }
9008 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9009 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9010 if (!(func & STR_FUNC_REGEXP)) {
9011 int cr = ENC_CODERANGE_UNKNOWN;
9012 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9013 if (cr != ENC_CODERANGE_7BIT &&
9014 rb_is_usascii_enc(p->enc) &&
9015 enc != rb_utf8_encoding()) {
9016 enc = rb_ascii8bit_encoding();
9017 }
9018 }
9019 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9020 }
9021 dispatch_delayed_token(p, tSTRING_CONTENT);
9022 }
9023 else {
9024 dispatch_delayed_token(p, tSTRING_CONTENT);
9025 dispatch_scan_event(p, tSTRING_CONTENT);
9026 }
9027 lex_goto_eol(p);
9028#endif
9029 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9030 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9031 (int)len, eos);
9032 token_flush(p);
9033 SET_LEX_STATE(EXPR_END);
9034 return tSTRING_END;
9035 }
9036 bol = was_bol(p);
9037 if (!bol) {
9038 /* not beginning of line, cannot be the terminator */
9039 }
9040 else if (p->heredoc_line_indent == -1) {
9041 /* `heredoc_line_indent == -1` means
9042 * - "after an interpolation in the same line", or
9043 * - "in a continuing line"
9044 */
9045 p->heredoc_line_indent = 0;
9046 }
9047 else if (whole_match_p(p, eos, len, indent)) {
9048 dispatch_heredoc_end(p);
9049 restore:
9050 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9051 token_flush(p);
9052 SET_LEX_STATE(EXPR_END);
9053 return tSTRING_END;
9054 }
9055
9056 if (!(func & STR_FUNC_EXPAND)) {
9057 do {
9058 ptr = PARSER_STRING_PTR(p->lex.lastline);
9059 ptr_end = p->lex.pend;
9060 if (ptr_end > ptr) {
9061 switch (ptr_end[-1]) {
9062 case '\n':
9063 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9064 ptr_end++;
9065 break;
9066 }
9067 case '\r':
9068 --ptr_end;
9069 }
9070 }
9071
9072 if (p->heredoc_indent > 0) {
9073 long i = 0;
9074 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9075 i++;
9076 p->heredoc_line_indent = 0;
9077 }
9078
9079 if (str)
9080 parser_str_cat(str, ptr, ptr_end - ptr);
9081 else
9082 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9083 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9084 lex_goto_eol(p);
9085 if (p->heredoc_indent > 0) {
9086 goto flush_str;
9087 }
9088 if (nextc(p) == -1) {
9089 if (str) {
9090 rb_parser_string_free(p, str);
9091 str = 0;
9092 }
9093 goto error;
9094 }
9095 } while (!whole_match_p(p, eos, len, indent));
9096 }
9097 else {
9098 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9099 newtok(p);
9100 if (c == '#') {
9101 enum yytokentype t = parser_peek_variable_name(p);
9102 if (p->heredoc_line_indent != -1) {
9103 if (p->heredoc_indent > p->heredoc_line_indent) {
9104 p->heredoc_indent = p->heredoc_line_indent;
9105 }
9106 p->heredoc_line_indent = -1;
9107 }
9108 if (t) return t;
9109 tokadd(p, '#');
9110 c = nextc(p);
9111 }
9112 do {
9113 pushback(p, c);
9114 enc = p->enc;
9115 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9116 if (p->eofp) goto error;
9117 goto restore;
9118 }
9119 if (c != '\n') {
9120 if (c == '\\') p->heredoc_line_indent = -1;
9121 flush:
9122 str = STR_NEW3(tok(p), toklen(p), enc, func);
9123 flush_str:
9124 set_yylval_str(str);
9125#ifndef RIPPER
9126 if (bol) nd_set_fl_newline(yylval.node);
9127#endif
9128 flush_string_content(p, enc, 0);
9129 return tSTRING_CONTENT;
9130 }
9131 tokadd(p, nextc(p));
9132 if (p->heredoc_indent > 0) {
9133 lex_goto_eol(p);
9134 goto flush;
9135 }
9136 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9137 if ((c = nextc(p)) == -1) goto error;
9138 } while (!whole_match_p(p, eos, len, indent));
9139 str = STR_NEW3(tok(p), toklen(p), enc, func);
9140 }
9141 dispatch_heredoc_end(p);
9142 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9143 token_flush(p);
9144 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9145#ifdef RIPPER
9146 /* Preserve s_value for set_yylval_str */
9147 s_value = p->s_value;
9148#endif
9149 set_yylval_str(str);
9150#ifdef RIPPER
9151 set_parser_s_value(s_value);
9152#endif
9153
9154#ifndef RIPPER
9155 if (bol) nd_set_fl_newline(yylval.node);
9156#endif
9157 return tSTRING_CONTENT;
9158}
9159
9160#include "lex.c"
9161
9162static int
9163arg_ambiguous(struct parser_params *p, char c)
9164{
9165#ifndef RIPPER
9166 if (c == '/') {
9167 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after '%c' operator", WARN_I(c));
9168 }
9169 else {
9170 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9171 }
9172#else
9173 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9174#endif
9175 return TRUE;
9176}
9177
9178/* returns true value if formal argument error;
9179 * Qtrue, or error message if ripper */
9180static VALUE
9181formal_argument_error(struct parser_params *p, ID id)
9182{
9183 switch (id_type(id)) {
9184 case ID_LOCAL:
9185 break;
9186#ifndef RIPPER
9187# define ERR(mesg) (yyerror0(mesg), Qtrue)
9188#else
9189# define ERR(mesg) WARN_S(mesg)
9190#endif
9191 case ID_CONST:
9192 return ERR("formal argument cannot be a constant");
9193 case ID_INSTANCE:
9194 return ERR("formal argument cannot be an instance variable");
9195 case ID_GLOBAL:
9196 return ERR("formal argument cannot be a global variable");
9197 case ID_CLASS:
9198 return ERR("formal argument cannot be a class variable");
9199 default:
9200 return ERR("formal argument must be local variable");
9201#undef ERR
9202 }
9203 shadowing_lvar(p, id);
9204
9205 return Qfalse;
9206}
9207
9208static int
9209lvar_defined(struct parser_params *p, ID id)
9210{
9211 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9212}
9213
9214/* emacsen -*- hack */
9215static long
9216parser_encode_length(struct parser_params *p, const char *name, long len)
9217{
9218 long nlen;
9219
9220 if (len > 5 && name[nlen = len - 5] == '-') {
9221 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9222 return nlen;
9223 }
9224 if (len > 4 && name[nlen = len - 4] == '-') {
9225 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9226 return nlen;
9227 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9228 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9229 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9230 return nlen;
9231 }
9232 return len;
9233}
9234
9235static void
9236parser_set_encode(struct parser_params *p, const char *name)
9237{
9238 rb_encoding *enc;
9239 VALUE excargs[3];
9240 int idx = 0;
9241
9242 const char *wrong = 0;
9243 switch (*name) {
9244 case 'e': case 'E': wrong = "external"; break;
9245 case 'i': case 'I': wrong = "internal"; break;
9246 case 'f': case 'F': wrong = "filesystem"; break;
9247 case 'l': case 'L': wrong = "locale"; break;
9248 }
9249 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9250 idx = rb_enc_find_index(name);
9251 if (idx < 0) {
9252 unknown:
9253 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9254 error:
9255 excargs[0] = rb_eArgError;
9256 excargs[2] = rb_make_backtrace();
9257 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9258 VALUE exc = rb_make_exception(3, excargs);
9259 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9260
9261 rb_ast_free(p->ast);
9262 p->ast = NULL;
9263
9264 rb_exc_raise(exc);
9265 }
9266 enc = rb_enc_from_index(idx);
9267 if (!rb_enc_asciicompat(enc)) {
9268 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9269 goto error;
9270 }
9271 p->enc = enc;
9272#ifndef RIPPER
9273 if (p->debug_lines) {
9274 long i;
9275 for (i = 0; i < p->debug_lines->len; i++) {
9276 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9277 }
9278 }
9279#endif
9280}
9281
9282static bool
9283comment_at_top(struct parser_params *p)
9284{
9285 if (p->token_seen) return false;
9286 return (p->line_count == (p->has_shebang ? 2 : 1));
9287}
9288
9289typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9290typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9291
9292static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9293
9294static void
9295magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9296{
9297 if (!comment_at_top(p)) {
9298 return;
9299 }
9300 parser_set_encode(p, val);
9301}
9302
9303static int
9304parser_get_bool(struct parser_params *p, const char *name, const char *val)
9305{
9306 switch (*val) {
9307 case 't': case 'T':
9308 if (STRCASECMP(val, "true") == 0) {
9309 return TRUE;
9310 }
9311 break;
9312 case 'f': case 'F':
9313 if (STRCASECMP(val, "false") == 0) {
9314 return FALSE;
9315 }
9316 break;
9317 }
9318 return parser_invalid_pragma_value(p, name, val);
9319}
9320
9321static int
9322parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9323{
9324 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9325 return -1;
9326}
9327
9328static void
9329parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9330{
9331 int b = parser_get_bool(p, name, val);
9332 if (b >= 0) p->token_info_enabled = b;
9333}
9334
9335static void
9336parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9337{
9338 int b;
9339
9340 if (p->token_seen) {
9341 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9342 return;
9343 }
9344
9345 b = parser_get_bool(p, name, val);
9346 if (b < 0) return;
9347
9348 p->frozen_string_literal = b;
9349}
9350
9351static void
9352parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9353{
9354 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9355 if (*s == ' ' || *s == '\t') continue;
9356 if (*s == '#') break;
9357 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9358 return;
9359 }
9360
9361 switch (*val) {
9362 case 'n': case 'N':
9363 if (STRCASECMP(val, "none") == 0) {
9364 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9365 return;
9366 }
9367 break;
9368 case 'l': case 'L':
9369 if (STRCASECMP(val, "literal") == 0) {
9370 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9371 return;
9372 }
9373 break;
9374 case 'e': case 'E':
9375 if (STRCASECMP(val, "experimental_copy") == 0) {
9376 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9377 return;
9378 }
9379 if (STRCASECMP(val, "experimental_everything") == 0) {
9380 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9381 return;
9382 }
9383 break;
9384 }
9385 parser_invalid_pragma_value(p, name, val);
9386}
9387
9388# if WARN_PAST_SCOPE
9389static void
9390parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9391{
9392 int b = parser_get_bool(p, name, val);
9393 if (b >= 0) p->past_scope_enabled = b;
9394}
9395# endif
9396
9397struct magic_comment {
9398 const char *name;
9399 rb_magic_comment_setter_t func;
9400 rb_magic_comment_length_t length;
9401};
9402
9403static const struct magic_comment magic_comments[] = {
9404 {"coding", magic_comment_encoding, parser_encode_length},
9405 {"encoding", magic_comment_encoding, parser_encode_length},
9406 {"frozen_string_literal", parser_set_frozen_string_literal},
9407 {"shareable_constant_value", parser_set_shareable_constant_value},
9408 {"warn_indent", parser_set_token_info},
9409# if WARN_PAST_SCOPE
9410 {"warn_past_scope", parser_set_past_scope},
9411# endif
9412};
9413
9414static const char *
9415magic_comment_marker(const char *str, long len)
9416{
9417 long i = 2;
9418
9419 while (i < len) {
9420 switch (str[i]) {
9421 case '-':
9422 if (str[i-1] == '*' && str[i-2] == '-') {
9423 return str + i + 1;
9424 }
9425 i += 2;
9426 break;
9427 case '*':
9428 if (i + 1 >= len) return 0;
9429 if (str[i+1] != '-') {
9430 i += 4;
9431 }
9432 else if (str[i-1] != '-') {
9433 i += 2;
9434 }
9435 else {
9436 return str + i + 2;
9437 }
9438 break;
9439 default:
9440 i += 3;
9441 break;
9442 }
9443 }
9444 return 0;
9445}
9446
9447static int
9448parser_magic_comment(struct parser_params *p, const char *str, long len)
9449{
9450 int indicator = 0;
9451 VALUE name = 0, val = 0;
9452 const char *beg, *end, *vbeg, *vend;
9453#define str_copy(_s, _p, _n) ((_s) \
9454 ? (void)(rb_str_resize((_s), (_n)), \
9455 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9456 : (void)((_s) = STR_NEW((_p), (_n))))
9457
9458 if (len <= 7) return FALSE;
9459 if (!!(beg = magic_comment_marker(str, len))) {
9460 if (!(end = magic_comment_marker(beg, str + len - beg)))
9461 return FALSE;
9462 indicator = TRUE;
9463 str = beg;
9464 len = end - beg - 3;
9465 }
9466
9467 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9468 while (len > 0) {
9469 const struct magic_comment *mc = magic_comments;
9470 char *s;
9471 int i;
9472 long n = 0;
9473
9474 for (; len > 0 && *str; str++, --len) {
9475 switch (*str) {
9476 case '\'': case '"': case ':': case ';':
9477 continue;
9478 }
9479 if (!ISSPACE(*str)) break;
9480 }
9481 for (beg = str; len > 0; str++, --len) {
9482 switch (*str) {
9483 case '\'': case '"': case ':': case ';':
9484 break;
9485 default:
9486 if (ISSPACE(*str)) break;
9487 continue;
9488 }
9489 break;
9490 }
9491 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9492 if (!len) break;
9493 if (*str != ':') {
9494 if (!indicator) return FALSE;
9495 continue;
9496 }
9497
9498 do str++; while (--len > 0 && ISSPACE(*str));
9499 if (!len) break;
9500 const char *tok_beg = str;
9501 if (*str == '"') {
9502 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9503 if (*str == '\\') {
9504 --len;
9505 ++str;
9506 }
9507 }
9508 vend = str;
9509 if (len) {
9510 --len;
9511 ++str;
9512 }
9513 }
9514 else {
9515 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9516 vend = str;
9517 }
9518 const char *tok_end = str;
9519 if (indicator) {
9520 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9521 }
9522 else {
9523 while (len > 0 && (ISSPACE(*str))) --len, str++;
9524 if (len) return FALSE;
9525 }
9526
9527 n = end - beg;
9528 str_copy(name, beg, n);
9529 s = RSTRING_PTR(name);
9530 for (i = 0; i < n; ++i) {
9531 if (s[i] == '-') s[i] = '_';
9532 }
9533 do {
9534 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9535 n = vend - vbeg;
9536 if (mc->length) {
9537 n = (*mc->length)(p, vbeg, n);
9538 }
9539 str_copy(val, vbeg, n);
9540 p->lex.ptok = tok_beg;
9541 p->lex.pcur = tok_end;
9542 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9543 break;
9544 }
9545 } while (++mc < magic_comments + numberof(magic_comments));
9546#ifdef RIPPER
9547 str_copy(val, vbeg, vend - vbeg);
9548 dispatch2(magic_comment, name, val);
9549#endif
9550 }
9551
9552 return TRUE;
9553}
9554
9555static void
9556set_file_encoding(struct parser_params *p, const char *str, const char *send)
9557{
9558 int sep = 0;
9559 const char *beg = str;
9560 VALUE s;
9561
9562 for (;;) {
9563 if (send - str <= 6) return;
9564 switch (str[6]) {
9565 case 'C': case 'c': str += 6; continue;
9566 case 'O': case 'o': str += 5; continue;
9567 case 'D': case 'd': str += 4; continue;
9568 case 'I': case 'i': str += 3; continue;
9569 case 'N': case 'n': str += 2; continue;
9570 case 'G': case 'g': str += 1; continue;
9571 case '=': case ':':
9572 sep = 1;
9573 str += 6;
9574 break;
9575 default:
9576 str += 6;
9577 if (ISSPACE(*str)) break;
9578 continue;
9579 }
9580 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9581 sep = 0;
9582 }
9583 for (;;) {
9584 do {
9585 if (++str >= send) return;
9586 } while (ISSPACE(*str));
9587 if (sep) break;
9588 if (*str != '=' && *str != ':') return;
9589 sep = 1;
9590 str++;
9591 }
9592 beg = str;
9593 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9594 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9595 p->lex.ptok = beg;
9596 p->lex.pcur = str;
9597 parser_set_encode(p, RSTRING_PTR(s));
9598 rb_str_resize(s, 0);
9599}
9600
9601static void
9602parser_prepare(struct parser_params *p)
9603{
9604 int c = nextc0(p, FALSE);
9605 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9606 switch (c) {
9607 case '#':
9608 if (peek(p, '!')) p->has_shebang = 1;
9609 break;
9610 case 0xef: /* UTF-8 BOM marker */
9611 if (!lex_eol_n_p(p, 2) &&
9612 (unsigned char)p->lex.pcur[0] == 0xbb &&
9613 (unsigned char)p->lex.pcur[1] == 0xbf) {
9614 p->enc = rb_utf8_encoding();
9615 p->lex.pcur += 2;
9616#ifndef RIPPER
9617 if (p->debug_lines) {
9618 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9619 }
9620#endif
9621 p->lex.pbeg = p->lex.pcur;
9622 token_flush(p);
9623 return;
9624 }
9625 break;
9626 case -1: /* end of script. */
9627 return;
9628 }
9629 pushback(p, c);
9630 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9631}
9632
9633#ifndef RIPPER
9634#define ambiguous_operator(tok, op, syn) ( \
9635 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9636 rb_warning0("even though it seems like "syn""))
9637#else
9638#define ambiguous_operator(tok, op, syn) \
9639 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9640#endif
9641#define warn_balanced(tok, op, syn) ((void) \
9642 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9643 space_seen && !ISSPACE(c) && \
9644 (ambiguous_operator(tok, op, syn), 0)), \
9645 (enum yytokentype)(tok))
9646
9647static enum yytokentype
9648no_digits(struct parser_params *p)
9649{
9650 yyerror0("numeric literal without digits");
9651 if (peek(p, '_')) nextc(p);
9652 /* dummy 0, for tUMINUS_NUM at numeric */
9653 return set_number_literal(p, tINTEGER, 0, 10, 0);
9654}
9655
9656static enum yytokentype
9657parse_numeric(struct parser_params *p, int c)
9658{
9659 int is_float, seen_point, seen_e, nondigit;
9660 int suffix;
9661
9662 is_float = seen_point = seen_e = nondigit = 0;
9663 SET_LEX_STATE(EXPR_END);
9664 newtok(p);
9665 if (c == '-' || c == '+') {
9666 tokadd(p, c);
9667 c = nextc(p);
9668 }
9669 if (c == '0') {
9670 int start = toklen(p);
9671 c = nextc(p);
9672 if (c == 'x' || c == 'X') {
9673 /* hexadecimal */
9674 c = nextc(p);
9675 if (c != -1 && ISXDIGIT(c)) {
9676 do {
9677 if (c == '_') {
9678 if (nondigit) break;
9679 nondigit = c;
9680 continue;
9681 }
9682 if (!ISXDIGIT(c)) break;
9683 nondigit = 0;
9684 tokadd(p, c);
9685 } while ((c = nextc(p)) != -1);
9686 }
9687 pushback(p, c);
9688 tokfix(p);
9689 if (toklen(p) == start) {
9690 return no_digits(p);
9691 }
9692 else if (nondigit) goto trailing_uc;
9693 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9694 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9695 }
9696 if (c == 'b' || c == 'B') {
9697 /* binary */
9698 c = nextc(p);
9699 if (c == '0' || c == '1') {
9700 do {
9701 if (c == '_') {
9702 if (nondigit) break;
9703 nondigit = c;
9704 continue;
9705 }
9706 if (c != '0' && c != '1') break;
9707 nondigit = 0;
9708 tokadd(p, c);
9709 } while ((c = nextc(p)) != -1);
9710 }
9711 pushback(p, c);
9712 tokfix(p);
9713 if (toklen(p) == start) {
9714 return no_digits(p);
9715 }
9716 else if (nondigit) goto trailing_uc;
9717 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9718 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9719 }
9720 if (c == 'd' || c == 'D') {
9721 /* decimal */
9722 c = nextc(p);
9723 if (c != -1 && ISDIGIT(c)) {
9724 do {
9725 if (c == '_') {
9726 if (nondigit) break;
9727 nondigit = c;
9728 continue;
9729 }
9730 if (!ISDIGIT(c)) break;
9731 nondigit = 0;
9732 tokadd(p, c);
9733 } while ((c = nextc(p)) != -1);
9734 }
9735 pushback(p, c);
9736 tokfix(p);
9737 if (toklen(p) == start) {
9738 return no_digits(p);
9739 }
9740 else if (nondigit) goto trailing_uc;
9741 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9742 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9743 }
9744 if (c == '_') {
9745 /* 0_0 */
9746 goto octal_number;
9747 }
9748 if (c == 'o' || c == 'O') {
9749 /* prefixed octal */
9750 c = nextc(p);
9751 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9752 tokfix(p);
9753 return no_digits(p);
9754 }
9755 }
9756 if (c >= '0' && c <= '7') {
9757 /* octal */
9758 octal_number:
9759 do {
9760 if (c == '_') {
9761 if (nondigit) break;
9762 nondigit = c;
9763 continue;
9764 }
9765 if (c < '0' || c > '9') break;
9766 if (c > '7') goto invalid_octal;
9767 nondigit = 0;
9768 tokadd(p, c);
9769 } while ((c = nextc(p)) != -1);
9770 if (toklen(p) > start) {
9771 pushback(p, c);
9772 tokfix(p);
9773 if (nondigit) goto trailing_uc;
9774 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9775 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9776 }
9777 if (nondigit) {
9778 pushback(p, c);
9779 goto trailing_uc;
9780 }
9781 }
9782 if (c > '7' && c <= '9') {
9783 invalid_octal:
9784 yyerror0("Invalid octal digit");
9785 }
9786 else if (c == '.' || c == 'e' || c == 'E') {
9787 tokadd(p, '0');
9788 }
9789 else {
9790 pushback(p, c);
9791 tokfix(p);
9792 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9793 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9794 }
9795 }
9796
9797 for (;;) {
9798 switch (c) {
9799 case '0': case '1': case '2': case '3': case '4':
9800 case '5': case '6': case '7': case '8': case '9':
9801 nondigit = 0;
9802 tokadd(p, c);
9803 break;
9804
9805 case '.':
9806 if (nondigit) goto trailing_uc;
9807 if (seen_point || seen_e) {
9808 goto decode_num;
9809 }
9810 else {
9811 int c0 = nextc(p);
9812 if (c0 == -1 || !ISDIGIT(c0)) {
9813 pushback(p, c0);
9814 goto decode_num;
9815 }
9816 c = c0;
9817 }
9818 seen_point = toklen(p);
9819 tokadd(p, '.');
9820 tokadd(p, c);
9821 is_float++;
9822 nondigit = 0;
9823 break;
9824
9825 case 'e':
9826 case 'E':
9827 if (nondigit) {
9828 pushback(p, c);
9829 c = nondigit;
9830 goto decode_num;
9831 }
9832 if (seen_e) {
9833 goto decode_num;
9834 }
9835 nondigit = c;
9836 c = nextc(p);
9837 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9838 pushback(p, c);
9839 c = nondigit;
9840 nondigit = 0;
9841 goto decode_num;
9842 }
9843 tokadd(p, nondigit);
9844 seen_e++;
9845 is_float++;
9846 tokadd(p, c);
9847 nondigit = (c == '-' || c == '+') ? c : 0;
9848 break;
9849
9850 case '_': /* `_' in number just ignored */
9851 if (nondigit) goto decode_num;
9852 nondigit = c;
9853 break;
9854
9855 default:
9856 goto decode_num;
9857 }
9858 c = nextc(p);
9859 }
9860
9861 decode_num:
9862 pushback(p, c);
9863 if (nondigit) {
9864 trailing_uc:
9865 literal_flush(p, p->lex.pcur - 1);
9866 YYLTYPE loc = RUBY_INIT_YYLLOC();
9867 compile_error(p, "trailing '%c' in number", nondigit);
9868 parser_show_error_line(p, &loc);
9869 }
9870 tokfix(p);
9871 if (is_float) {
9872 enum yytokentype type = tFLOAT;
9873
9874 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9875 if (suffix & NUM_SUFFIX_R) {
9876 type = tRATIONAL;
9877 }
9878 else {
9879 strtod(tok(p), 0);
9880 if (errno == ERANGE) {
9881 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9882 errno = 0;
9883 }
9884 }
9885 return set_number_literal(p, type, suffix, 0, seen_point);
9886 }
9887 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9888 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9889}
9890
9891static enum yytokentype
9892parse_qmark(struct parser_params *p, int space_seen)
9893{
9894 rb_encoding *enc;
9895 register int c;
9896 rb_parser_string_t *lit;
9897 const char *start = p->lex.pcur;
9898
9899 if (IS_END()) {
9900 SET_LEX_STATE(EXPR_VALUE);
9901 return '?';
9902 }
9903 c = nextc(p);
9904 if (c == -1) {
9905 compile_error(p, "incomplete character syntax");
9906 return 0;
9907 }
9908 if (rb_enc_isspace(c, p->enc)) {
9909 if (!IS_ARG()) {
9910 int c2 = escaped_control_code(c);
9911 if (c2) {
9912 WARN_SPACE_CHAR(c2, "?");
9913 }
9914 }
9915 ternary:
9916 pushback(p, c);
9917 SET_LEX_STATE(EXPR_VALUE);
9918 return '?';
9919 }
9920 newtok(p);
9921 enc = p->enc;
9922 int w = parser_precise_mbclen(p, start);
9923 if (is_identchar(p, start, p->lex.pend, p->enc) &&
9924 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
9925 if (space_seen) {
9926 const char *ptr = start;
9927 do {
9928 int n = parser_precise_mbclen(p, ptr);
9929 if (n < 0) return -1;
9930 ptr += n;
9931 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9932 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9933 " a conditional operator, put a space after '?'",
9934 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9935 }
9936 goto ternary;
9937 }
9938 else if (c == '\\') {
9939 if (peek(p, 'u')) {
9940 nextc(p);
9941 enc = rb_utf8_encoding();
9942 tokadd_utf8(p, &enc, -1, 0, 0);
9943 }
9944 else if (!ISASCII(c = peekc(p)) && c != -1) {
9945 nextc(p);
9946 if (tokadd_mbchar(p, c) == -1) return 0;
9947 }
9948 else {
9949 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
9950 tokadd(p, c);
9951 }
9952 }
9953 else {
9954 if (tokadd_mbchar(p, c) == -1) return 0;
9955 }
9956 tokfix(p);
9957 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9958 set_yylval_str(lit);
9959 SET_LEX_STATE(EXPR_END);
9960 return tCHAR;
9961}
9962
9963static enum yytokentype
9964parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9965{
9966 register int c;
9967 const char *ptok = p->lex.pcur;
9968
9969 if (IS_BEG()) {
9970 int term;
9971 int paren;
9972
9973 c = nextc(p);
9974 quotation:
9975 if (c == -1) goto unterminated;
9976 if (!ISALNUM(c)) {
9977 term = c;
9978 if (!ISASCII(c)) goto unknown;
9979 c = 'Q';
9980 }
9981 else {
9982 term = nextc(p);
9983 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
9984 unknown:
9985 pushback(p, term);
9986 c = parser_precise_mbclen(p, p->lex.pcur);
9987 if (c < 0) return 0;
9988 p->lex.pcur += c;
9989 yyerror0("unknown type of %string");
9990 return 0;
9991 }
9992 }
9993 if (term == -1) {
9994 unterminated:
9995 compile_error(p, "unterminated quoted string meets end of file");
9996 return 0;
9997 }
9998 paren = term;
9999 if (term == '(') term = ')';
10000 else if (term == '[') term = ']';
10001 else if (term == '{') term = '}';
10002 else if (term == '<') term = '>';
10003 else paren = 0;
10004
10005 p->lex.ptok = ptok-1;
10006 switch (c) {
10007 case 'Q':
10008 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10009 return tSTRING_BEG;
10010
10011 case 'q':
10012 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10013 return tSTRING_BEG;
10014
10015 case 'W':
10016 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10017 return tWORDS_BEG;
10018
10019 case 'w':
10020 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10021 return tQWORDS_BEG;
10022
10023 case 'I':
10024 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10025 return tSYMBOLS_BEG;
10026
10027 case 'i':
10028 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10029 return tQSYMBOLS_BEG;
10030
10031 case 'x':
10032 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10033 return tXSTRING_BEG;
10034
10035 case 'r':
10036 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10037 return tREGEXP_BEG;
10038
10039 case 's':
10040 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10041 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10042 return tSYMBEG;
10043
10044 default:
10045 yyerror0("unknown type of %string");
10046 return 0;
10047 }
10048 }
10049 if ((c = nextc(p)) == '=') {
10050 set_yylval_id('%');
10051 SET_LEX_STATE(EXPR_BEG);
10052 return tOP_ASGN;
10053 }
10054 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10055 goto quotation;
10056 }
10057 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10058 pushback(p, c);
10059 return warn_balanced('%', "%%", "string literal");
10060}
10061
10062static int
10063tokadd_ident(struct parser_params *p, int c)
10064{
10065 do {
10066 if (tokadd_mbchar(p, c) == -1) return -1;
10067 c = nextc(p);
10068 } while (parser_is_identchar(p));
10069 pushback(p, c);
10070 return 0;
10071}
10072
10073static ID
10074tokenize_ident(struct parser_params *p)
10075{
10076 ID ident = TOK_INTERN();
10077
10078 set_yylval_name(ident);
10079
10080 return ident;
10081}
10082
10083static int
10084parse_numvar(struct parser_params *p)
10085{
10086 size_t len;
10087 int overflow;
10088 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10089 const unsigned long nth_ref_max =
10090 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10091 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10092 * turned into a Fixnum, in compile.c */
10093
10094 if (overflow || n > nth_ref_max) {
10095 /* compile_error()? */
10096 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10097 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10098 }
10099 else {
10100 return (int)n;
10101 }
10102}
10103
10104static enum yytokentype
10105parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10106{
10107 const char *ptr = p->lex.pcur;
10108 register int c;
10109
10110 SET_LEX_STATE(EXPR_END);
10111 p->lex.ptok = ptr - 1; /* from '$' */
10112 newtok(p);
10113 c = nextc(p);
10114 switch (c) {
10115 case '_': /* $_: last read line string */
10116 c = nextc(p);
10117 if (parser_is_identchar(p)) {
10118 tokadd(p, '$');
10119 tokadd(p, '_');
10120 break;
10121 }
10122 pushback(p, c);
10123 c = '_';
10124 /* fall through */
10125 case '~': /* $~: match-data */
10126 case '*': /* $*: argv */
10127 case '$': /* $$: pid */
10128 case '?': /* $?: last status */
10129 case '!': /* $!: error string */
10130 case '@': /* $@: error position */
10131 case '/': /* $/: input record separator */
10132 case '\\': /* $\: output record separator */
10133 case ';': /* $;: field separator */
10134 case ',': /* $,: output field separator */
10135 case '.': /* $.: last read line number */
10136 case '=': /* $=: ignorecase */
10137 case ':': /* $:: load path */
10138 case '<': /* $<: default input handle */
10139 case '>': /* $>: default output handle */
10140 case '\"': /* $": already loaded files */
10141 tokadd(p, '$');
10142 tokadd(p, c);
10143 goto gvar;
10144
10145 case '-':
10146 tokadd(p, '$');
10147 tokadd(p, c);
10148 c = nextc(p);
10149 if (parser_is_identchar(p)) {
10150 if (tokadd_mbchar(p, c) == -1) return 0;
10151 }
10152 else {
10153 pushback(p, c);
10154 pushback(p, '-');
10155 return '$';
10156 }
10157 gvar:
10158 tokenize_ident(p);
10159 return tGVAR;
10160
10161 case '&': /* $&: last match */
10162 case '`': /* $`: string before last match */
10163 case '\'': /* $': string after last match */
10164 case '+': /* $+: string matches last paren. */
10165 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10166 tokadd(p, '$');
10167 tokadd(p, c);
10168 goto gvar;
10169 }
10170 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10171 return tBACK_REF;
10172
10173 case '1': case '2': case '3':
10174 case '4': case '5': case '6':
10175 case '7': case '8': case '9':
10176 tokadd(p, '$');
10177 do {
10178 tokadd(p, c);
10179 c = nextc(p);
10180 } while (c != -1 && ISDIGIT(c));
10181 pushback(p, c);
10182 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10183 tokfix(p);
10184 c = parse_numvar(p);
10185 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10186 return tNTH_REF;
10187
10188 default:
10189 if (!parser_is_identchar(p)) {
10190 YYLTYPE loc = RUBY_INIT_YYLLOC();
10191 if (c == -1 || ISSPACE(c)) {
10192 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10193 }
10194 else {
10195 pushback(p, c);
10196 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10197 }
10198 parser_show_error_line(p, &loc);
10199 set_yylval_noname();
10200 return tGVAR;
10201 }
10202 /* fall through */
10203 case '0':
10204 tokadd(p, '$');
10205 }
10206
10207 if (tokadd_ident(p, c)) return 0;
10208 SET_LEX_STATE(EXPR_END);
10209 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10210 tokenize_ident(p);
10211 }
10212 else {
10213 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10214 set_yylval_noname();
10215 }
10216 return tGVAR;
10217}
10218
10219static bool
10220parser_numbered_param(struct parser_params *p, int n)
10221{
10222 if (n < 0) return false;
10223
10224 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10225 return false;
10226 }
10227 if (p->max_numparam == ORDINAL_PARAM) {
10228 compile_error(p, "ordinary parameter is defined");
10229 return false;
10230 }
10231 struct vtable *args = p->lvtbl->args;
10232 if (p->max_numparam < n) {
10233 p->max_numparam = n;
10234 }
10235 while (n > args->pos) {
10236 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10237 }
10238 return true;
10239}
10240
10241static enum yytokentype
10242parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10243{
10244 const char *ptr = p->lex.pcur;
10245 enum yytokentype result = tIVAR;
10246 register int c = nextc(p);
10247 YYLTYPE loc;
10248
10249 p->lex.ptok = ptr - 1; /* from '@' */
10250 newtok(p);
10251 tokadd(p, '@');
10252 if (c == '@') {
10253 result = tCVAR;
10254 tokadd(p, '@');
10255 c = nextc(p);
10256 }
10257 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10258 if (c == -1 || !parser_is_identchar(p)) {
10259 pushback(p, c);
10260 RUBY_SET_YYLLOC(loc);
10261 if (result == tIVAR) {
10262 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10263 }
10264 else {
10265 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10266 }
10267 parser_show_error_line(p, &loc);
10268 set_yylval_noname();
10269 SET_LEX_STATE(EXPR_END);
10270 return result;
10271 }
10272 else if (ISDIGIT(c)) {
10273 pushback(p, c);
10274 RUBY_SET_YYLLOC(loc);
10275 if (result == tIVAR) {
10276 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10277 }
10278 else {
10279 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10280 }
10281 parser_show_error_line(p, &loc);
10282 set_yylval_noname();
10283 SET_LEX_STATE(EXPR_END);
10284 return result;
10285 }
10286
10287 if (tokadd_ident(p, c)) return 0;
10288 tokenize_ident(p);
10289 return result;
10290}
10291
10292static enum yytokentype
10293parse_ident(struct parser_params *p, int c, int cmd_state)
10294{
10295 enum yytokentype result;
10296 bool is_ascii = true;
10297 const enum lex_state_e last_state = p->lex.state;
10298 ID ident;
10299 int enforce_keyword_end = 0;
10300
10301 do {
10302 if (!ISASCII(c)) is_ascii = false;
10303 if (tokadd_mbchar(p, c) == -1) return 0;
10304 c = nextc(p);
10305 } while (parser_is_identchar(p));
10306 if ((c == '!' || c == '?') && !peek(p, '=')) {
10307 result = tFID;
10308 tokadd(p, c);
10309 }
10310 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10311 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10312 result = tIDENTIFIER;
10313 tokadd(p, c);
10314 }
10315 else {
10316 result = tCONSTANT; /* assume provisionally */
10317 pushback(p, c);
10318 }
10319 tokfix(p);
10320
10321 if (IS_LABEL_POSSIBLE()) {
10322 if (IS_LABEL_SUFFIX(0)) {
10323 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10324 nextc(p);
10325 tokenize_ident(p);
10326 return tLABEL;
10327 }
10328 }
10329
10330#ifndef RIPPER
10331 if (peek_end_expect_token_locations(p)) {
10332 const rb_code_position_t *end_pos;
10333 int lineno, column;
10334 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10335
10336 end_pos = peek_end_expect_token_locations(p)->pos;
10337 lineno = end_pos->lineno;
10338 column = end_pos->column;
10339
10340 if (p->debug) {
10341 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10342 p->ruby_sourceline, beg_pos, lineno, column);
10343 }
10344
10345 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10346 const struct kwtable *kw;
10347
10348 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10349 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10350 enforce_keyword_end = 1;
10351 }
10352 }
10353 }
10354#endif
10355
10356 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10357 const struct kwtable *kw;
10358
10359 /* See if it is a reserved word. */
10360 kw = rb_reserved_word(tok(p), toklen(p));
10361 if (kw) {
10362 enum lex_state_e state = p->lex.state;
10363 if (IS_lex_state_for(state, EXPR_FNAME)) {
10364 SET_LEX_STATE(EXPR_ENDFN);
10365 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10366 return kw->id[0];
10367 }
10368 SET_LEX_STATE(kw->state);
10369 if (IS_lex_state(EXPR_BEG)) {
10370 p->command_start = TRUE;
10371 }
10372 if (kw->id[0] == keyword_do) {
10373 if (lambda_beginning_p()) {
10374 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10375 return keyword_do_LAMBDA;
10376 }
10377 if (COND_P()) return keyword_do_cond;
10378 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10379 return keyword_do_block;
10380 return keyword_do;
10381 }
10382 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10383 return kw->id[0];
10384 else {
10385 if (kw->id[0] != kw->id[1])
10386 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10387 return kw->id[1];
10388 }
10389 }
10390 }
10391
10392 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10393 if (cmd_state) {
10394 SET_LEX_STATE(EXPR_CMDARG);
10395 }
10396 else {
10397 SET_LEX_STATE(EXPR_ARG);
10398 }
10399 }
10400 else if (p->lex.state == EXPR_FNAME) {
10401 SET_LEX_STATE(EXPR_ENDFN);
10402 }
10403 else {
10404 SET_LEX_STATE(EXPR_END);
10405 }
10406
10407 ident = tokenize_ident(p);
10408 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10409 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10410 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10411 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10412 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10413 }
10414 return result;
10415}
10416
10417static void
10418warn_cr(struct parser_params *p)
10419{
10420 if (!p->cr_seen) {
10421 p->cr_seen = TRUE;
10422 /* carried over with p->lex.nextline for nextc() */
10423 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10424 }
10425}
10426
10427static enum yytokentype
10428parser_yylex(struct parser_params *p)
10429{
10430 register int c;
10431 int space_seen = 0;
10432 int cmd_state;
10433 int label;
10434 enum lex_state_e last_state;
10435 int fallthru = FALSE;
10436 int token_seen = p->token_seen;
10437
10438 if (p->lex.strterm) {
10439 if (strterm_is_heredoc(p->lex.strterm)) {
10440 token_flush(p);
10441 return here_document(p, &p->lex.strterm->u.heredoc);
10442 }
10443 else {
10444 token_flush(p);
10445 return parse_string(p, &p->lex.strterm->u.literal);
10446 }
10447 }
10448 cmd_state = p->command_start;
10449 p->command_start = FALSE;
10450 p->token_seen = TRUE;
10451#ifndef RIPPER
10452 token_flush(p);
10453#endif
10454 retry:
10455 last_state = p->lex.state;
10456 switch (c = nextc(p)) {
10457 case '\0': /* NUL */
10458 case '\004': /* ^D */
10459 case '\032': /* ^Z */
10460 case -1: /* end of script. */
10461 p->eofp = 1;
10462#ifndef RIPPER
10463 if (p->end_expect_token_locations) {
10464 pop_end_expect_token_locations(p);
10465 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10466 return tDUMNY_END;
10467 }
10468#endif
10469 /* Set location for end-of-input because dispatch_scan_event is not called. */
10470 RUBY_SET_YYLLOC(*p->yylloc);
10471 return END_OF_INPUT;
10472
10473 /* white spaces */
10474 case '\r':
10475 warn_cr(p);
10476 /* fall through */
10477 case ' ': case '\t': case '\f':
10478 case '\13': /* '\v' */
10479 space_seen = 1;
10480 while ((c = nextc(p))) {
10481 switch (c) {
10482 case '\r':
10483 warn_cr(p);
10484 /* fall through */
10485 case ' ': case '\t': case '\f':
10486 case '\13': /* '\v' */
10487 break;
10488 default:
10489 goto outofloop;
10490 }
10491 }
10492 outofloop:
10493 pushback(p, c);
10494 dispatch_scan_event(p, tSP);
10495#ifndef RIPPER
10496 token_flush(p);
10497#endif
10498 goto retry;
10499
10500 case '#': /* it's a comment */
10501 p->token_seen = token_seen;
10502 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10503 /* no magic_comment in shebang line */
10504 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10505 if (comment_at_top(p)) {
10506 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10507 }
10508 }
10509 p->lex.pcur = pcur, p->lex.ptok = ptok;
10510 lex_goto_eol(p);
10511 dispatch_scan_event(p, tCOMMENT);
10512 fallthru = TRUE;
10513 /* fall through */
10514 case '\n':
10515 p->token_seen = token_seen;
10516 rb_parser_string_t *prevline = p->lex.lastline;
10517 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10518 !IS_lex_state(EXPR_LABELED));
10519 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10520 if (!fallthru) {
10521 dispatch_scan_event(p, tIGNORED_NL);
10522 }
10523 fallthru = FALSE;
10524 if (!c && p->ctxt.in_kwarg) {
10525 goto normal_newline;
10526 }
10527 goto retry;
10528 }
10529 while (1) {
10530 switch (c = nextc(p)) {
10531 case ' ': case '\t': case '\f': case '\r':
10532 case '\13': /* '\v' */
10533 space_seen = 1;
10534 break;
10535 case '#':
10536 pushback(p, c);
10537 if (space_seen) {
10538 dispatch_scan_event(p, tSP);
10539 token_flush(p);
10540 }
10541 goto retry;
10542 case '&':
10543 case '.': {
10544 dispatch_delayed_token(p, tIGNORED_NL);
10545 if (peek(p, '.') == (c == '&')) {
10546 pushback(p, c);
10547 dispatch_scan_event(p, tSP);
10548 goto retry;
10549 }
10550 }
10551 default:
10552 p->ruby_sourceline--;
10553 p->lex.nextline = p->lex.lastline;
10554 set_lastline(p, prevline);
10555 case -1: /* EOF no decrement*/
10556 if (c == -1 && space_seen) {
10557 dispatch_scan_event(p, tSP);
10558 }
10559 lex_goto_eol(p);
10560 if (c != -1) {
10561 token_flush(p);
10562 RUBY_SET_YYLLOC(*p->yylloc);
10563 }
10564 goto normal_newline;
10565 }
10566 }
10567 normal_newline:
10568 p->command_start = TRUE;
10569 SET_LEX_STATE(EXPR_BEG);
10570 return '\n';
10571
10572 case '*':
10573 if ((c = nextc(p)) == '*') {
10574 if ((c = nextc(p)) == '=') {
10575 set_yylval_id(idPow);
10576 SET_LEX_STATE(EXPR_BEG);
10577 return tOP_ASGN;
10578 }
10579 pushback(p, c);
10580 if (IS_SPCARG(c)) {
10581 rb_warning0("'**' interpreted as argument prefix");
10582 c = tDSTAR;
10583 }
10584 else if (IS_BEG()) {
10585 c = tDSTAR;
10586 }
10587 else {
10588 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10589 }
10590 }
10591 else {
10592 if (c == '=') {
10593 set_yylval_id('*');
10594 SET_LEX_STATE(EXPR_BEG);
10595 return tOP_ASGN;
10596 }
10597 pushback(p, c);
10598 if (IS_SPCARG(c)) {
10599 rb_warning0("'*' interpreted as argument prefix");
10600 c = tSTAR;
10601 }
10602 else if (IS_BEG()) {
10603 c = tSTAR;
10604 }
10605 else {
10606 c = warn_balanced('*', "*", "argument prefix");
10607 }
10608 }
10609 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10610 return c;
10611
10612 case '!':
10613 c = nextc(p);
10614 if (IS_AFTER_OPERATOR()) {
10615 SET_LEX_STATE(EXPR_ARG);
10616 if (c == '@') {
10617 return '!';
10618 }
10619 }
10620 else {
10621 SET_LEX_STATE(EXPR_BEG);
10622 }
10623 if (c == '=') {
10624 return tNEQ;
10625 }
10626 if (c == '~') {
10627 return tNMATCH;
10628 }
10629 pushback(p, c);
10630 return '!';
10631
10632 case '=':
10633 if (was_bol(p)) {
10634 /* skip embedded rd document */
10635 if (word_match_p(p, "begin", 5)) {
10636 int first_p = TRUE;
10637
10638 lex_goto_eol(p);
10639 dispatch_scan_event(p, tEMBDOC_BEG);
10640 for (;;) {
10641 lex_goto_eol(p);
10642 if (!first_p) {
10643 dispatch_scan_event(p, tEMBDOC);
10644 }
10645 first_p = FALSE;
10646 c = nextc(p);
10647 if (c == -1) {
10648 compile_error(p, "embedded document meets end of file");
10649 return END_OF_INPUT;
10650 }
10651 if (c == '=' && word_match_p(p, "end", 3)) {
10652 break;
10653 }
10654 pushback(p, c);
10655 }
10656 lex_goto_eol(p);
10657 dispatch_scan_event(p, tEMBDOC_END);
10658 goto retry;
10659 }
10660 }
10661
10662 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10663 if ((c = nextc(p)) == '=') {
10664 if ((c = nextc(p)) == '=') {
10665 return tEQQ;
10666 }
10667 pushback(p, c);
10668 return tEQ;
10669 }
10670 if (c == '~') {
10671 return tMATCH;
10672 }
10673 else if (c == '>') {
10674 return tASSOC;
10675 }
10676 pushback(p, c);
10677 return '=';
10678
10679 case '<':
10680 c = nextc(p);
10681 if (c == '<' &&
10682 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10683 !IS_END() &&
10684 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10685 enum yytokentype token = heredoc_identifier(p);
10686 if (token) return token < 0 ? 0 : token;
10687 }
10688 if (IS_AFTER_OPERATOR()) {
10689 SET_LEX_STATE(EXPR_ARG);
10690 }
10691 else {
10692 if (IS_lex_state(EXPR_CLASS))
10693 p->command_start = TRUE;
10694 SET_LEX_STATE(EXPR_BEG);
10695 }
10696 if (c == '=') {
10697 if ((c = nextc(p)) == '>') {
10698 return tCMP;
10699 }
10700 pushback(p, c);
10701 return tLEQ;
10702 }
10703 if (c == '<') {
10704 if ((c = nextc(p)) == '=') {
10705 set_yylval_id(idLTLT);
10706 SET_LEX_STATE(EXPR_BEG);
10707 return tOP_ASGN;
10708 }
10709 pushback(p, c);
10710 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10711 }
10712 pushback(p, c);
10713 return '<';
10714
10715 case '>':
10716 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10717 if ((c = nextc(p)) == '=') {
10718 return tGEQ;
10719 }
10720 if (c == '>') {
10721 if ((c = nextc(p)) == '=') {
10722 set_yylval_id(idGTGT);
10723 SET_LEX_STATE(EXPR_BEG);
10724 return tOP_ASGN;
10725 }
10726 pushback(p, c);
10727 return tRSHFT;
10728 }
10729 pushback(p, c);
10730 return '>';
10731
10732 case '"':
10733 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10734 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10735 p->lex.ptok = p->lex.pcur-1;
10736 return tSTRING_BEG;
10737
10738 case '`':
10739 if (IS_lex_state(EXPR_FNAME)) {
10740 SET_LEX_STATE(EXPR_ENDFN);
10741 return c;
10742 }
10743 if (IS_lex_state(EXPR_DOT)) {
10744 if (cmd_state)
10745 SET_LEX_STATE(EXPR_CMDARG);
10746 else
10747 SET_LEX_STATE(EXPR_ARG);
10748 return c;
10749 }
10750 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10751 return tXSTRING_BEG;
10752
10753 case '\'':
10754 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10755 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10756 p->lex.ptok = p->lex.pcur-1;
10757 return tSTRING_BEG;
10758
10759 case '?':
10760 return parse_qmark(p, space_seen);
10761
10762 case '&':
10763 if ((c = nextc(p)) == '&') {
10764 SET_LEX_STATE(EXPR_BEG);
10765 if ((c = nextc(p)) == '=') {
10766 set_yylval_id(idANDOP);
10767 SET_LEX_STATE(EXPR_BEG);
10768 return tOP_ASGN;
10769 }
10770 pushback(p, c);
10771 return tANDOP;
10772 }
10773 else if (c == '=') {
10774 set_yylval_id('&');
10775 SET_LEX_STATE(EXPR_BEG);
10776 return tOP_ASGN;
10777 }
10778 else if (c == '.') {
10779 set_yylval_id(idANDDOT);
10780 SET_LEX_STATE(EXPR_DOT);
10781 return tANDDOT;
10782 }
10783 pushback(p, c);
10784 if (IS_SPCARG(c)) {
10785 if ((c != ':') ||
10786 (c = peekc_n(p, 1)) == -1 ||
10787 !(c == '\'' || c == '"' ||
10788 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10789 rb_warning0("'&' interpreted as argument prefix");
10790 }
10791 c = tAMPER;
10792 }
10793 else if (IS_BEG()) {
10794 c = tAMPER;
10795 }
10796 else {
10797 c = warn_balanced('&', "&", "argument prefix");
10798 }
10799 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10800 return c;
10801
10802 case '|':
10803 if ((c = nextc(p)) == '|') {
10804 SET_LEX_STATE(EXPR_BEG);
10805 if ((c = nextc(p)) == '=') {
10806 set_yylval_id(idOROP);
10807 SET_LEX_STATE(EXPR_BEG);
10808 return tOP_ASGN;
10809 }
10810 pushback(p, c);
10811 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10812 c = '|';
10813 pushback(p, '|');
10814 return c;
10815 }
10816 return tOROP;
10817 }
10818 if (c == '=') {
10819 set_yylval_id('|');
10820 SET_LEX_STATE(EXPR_BEG);
10821 return tOP_ASGN;
10822 }
10823 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10824 pushback(p, c);
10825 return '|';
10826
10827 case '+':
10828 c = nextc(p);
10829 if (IS_AFTER_OPERATOR()) {
10830 SET_LEX_STATE(EXPR_ARG);
10831 if (c == '@') {
10832 return tUPLUS;
10833 }
10834 pushback(p, c);
10835 return '+';
10836 }
10837 if (c == '=') {
10838 set_yylval_id('+');
10839 SET_LEX_STATE(EXPR_BEG);
10840 return tOP_ASGN;
10841 }
10842 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10843 SET_LEX_STATE(EXPR_BEG);
10844 pushback(p, c);
10845 if (c != -1 && ISDIGIT(c)) {
10846 return parse_numeric(p, '+');
10847 }
10848 return tUPLUS;
10849 }
10850 SET_LEX_STATE(EXPR_BEG);
10851 pushback(p, c);
10852 return warn_balanced('+', "+", "unary operator");
10853
10854 case '-':
10855 c = nextc(p);
10856 if (IS_AFTER_OPERATOR()) {
10857 SET_LEX_STATE(EXPR_ARG);
10858 if (c == '@') {
10859 return tUMINUS;
10860 }
10861 pushback(p, c);
10862 return '-';
10863 }
10864 if (c == '=') {
10865 set_yylval_id('-');
10866 SET_LEX_STATE(EXPR_BEG);
10867 return tOP_ASGN;
10868 }
10869 if (c == '>') {
10870 SET_LEX_STATE(EXPR_ENDFN);
10871 yylval.num = p->lex.lpar_beg;
10872 p->lex.lpar_beg = p->lex.paren_nest;
10873 return tLAMBDA;
10874 }
10875 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10876 SET_LEX_STATE(EXPR_BEG);
10877 pushback(p, c);
10878 if (c != -1 && ISDIGIT(c)) {
10879 return tUMINUS_NUM;
10880 }
10881 return tUMINUS;
10882 }
10883 SET_LEX_STATE(EXPR_BEG);
10884 pushback(p, c);
10885 return warn_balanced('-', "-", "unary operator");
10886
10887 case '.': {
10888 int is_beg = IS_BEG();
10889 SET_LEX_STATE(EXPR_BEG);
10890 if ((c = nextc(p)) == '.') {
10891 if ((c = nextc(p)) == '.') {
10892 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10893 SET_LEX_STATE(EXPR_ENDARG);
10894 return tBDOT3;
10895 }
10896 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10897 rb_warn0("... at EOL, should be parenthesized?");
10898 }
10899 return is_beg ? tBDOT3 : tDOT3;
10900 }
10901 pushback(p, c);
10902 return is_beg ? tBDOT2 : tDOT2;
10903 }
10904 pushback(p, c);
10905 if (c != -1 && ISDIGIT(c)) {
10906 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10907 parse_numeric(p, '.');
10908 if (ISDIGIT(prev)) {
10909 yyerror0("unexpected fraction part after numeric literal");
10910 }
10911 else {
10912 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10913 }
10914 SET_LEX_STATE(EXPR_END);
10915 p->lex.ptok = p->lex.pcur;
10916 goto retry;
10917 }
10918 set_yylval_id('.');
10919 SET_LEX_STATE(EXPR_DOT);
10920 return '.';
10921 }
10922
10923 case '0': case '1': case '2': case '3': case '4':
10924 case '5': case '6': case '7': case '8': case '9':
10925 return parse_numeric(p, c);
10926
10927 case ')':
10928 COND_POP();
10929 CMDARG_POP();
10930 SET_LEX_STATE(EXPR_ENDFN);
10931 p->lex.paren_nest--;
10932 return c;
10933
10934 case ']':
10935 COND_POP();
10936 CMDARG_POP();
10937 SET_LEX_STATE(EXPR_END);
10938 p->lex.paren_nest--;
10939 return c;
10940
10941 case '}':
10942 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10943 if (!p->lex.brace_nest--) return tSTRING_DEND;
10944 COND_POP();
10945 CMDARG_POP();
10946 SET_LEX_STATE(EXPR_END);
10947 p->lex.paren_nest--;
10948 return c;
10949
10950 case ':':
10951 c = nextc(p);
10952 if (c == ':') {
10953 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10954 SET_LEX_STATE(EXPR_BEG);
10955 return tCOLON3;
10956 }
10957 set_yylval_id(idCOLON2);
10958 SET_LEX_STATE(EXPR_DOT);
10959 return tCOLON2;
10960 }
10961 if (IS_END() || ISSPACE(c) || c == '#') {
10962 pushback(p, c);
10963 c = warn_balanced(':', ":", "symbol literal");
10964 SET_LEX_STATE(EXPR_BEG);
10965 return c;
10966 }
10967 switch (c) {
10968 case '\'':
10969 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
10970 break;
10971 case '"':
10972 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
10973 break;
10974 default:
10975 pushback(p, c);
10976 break;
10977 }
10978 SET_LEX_STATE(EXPR_FNAME);
10979 return tSYMBEG;
10980
10981 case '/':
10982 if (IS_BEG()) {
10983 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10984 return tREGEXP_BEG;
10985 }
10986 if ((c = nextc(p)) == '=') {
10987 set_yylval_id('/');
10988 SET_LEX_STATE(EXPR_BEG);
10989 return tOP_ASGN;
10990 }
10991 pushback(p, c);
10992 if (IS_SPCARG(c)) {
10993 arg_ambiguous(p, '/');
10994 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10995 return tREGEXP_BEG;
10996 }
10997 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10998 return warn_balanced('/', "/", "regexp literal");
10999
11000 case '^':
11001 if ((c = nextc(p)) == '=') {
11002 set_yylval_id('^');
11003 SET_LEX_STATE(EXPR_BEG);
11004 return tOP_ASGN;
11005 }
11006 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11007 pushback(p, c);
11008 return '^';
11009
11010 case ';':
11011 SET_LEX_STATE(EXPR_BEG);
11012 p->command_start = TRUE;
11013 return ';';
11014
11015 case ',':
11016 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11017 return ',';
11018
11019 case '~':
11020 if (IS_AFTER_OPERATOR()) {
11021 if ((c = nextc(p)) != '@') {
11022 pushback(p, c);
11023 }
11024 SET_LEX_STATE(EXPR_ARG);
11025 }
11026 else {
11027 SET_LEX_STATE(EXPR_BEG);
11028 }
11029 return '~';
11030
11031 case '(':
11032 if (IS_BEG()) {
11033 c = tLPAREN;
11034 }
11035 else if (!space_seen) {
11036 /* foo( ... ) => method call, no ambiguity */
11037 }
11038 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11039 c = tLPAREN_ARG;
11040 }
11041 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11042 rb_warning0("parentheses after method name is interpreted as "
11043 "an argument list, not a decomposed argument");
11044 }
11045 p->lex.paren_nest++;
11046 COND_PUSH(0);
11047 CMDARG_PUSH(0);
11048 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11049 return c;
11050
11051 case '[':
11052 p->lex.paren_nest++;
11053 if (IS_AFTER_OPERATOR()) {
11054 if ((c = nextc(p)) == ']') {
11055 p->lex.paren_nest--;
11056 SET_LEX_STATE(EXPR_ARG);
11057 if ((c = nextc(p)) == '=') {
11058 return tASET;
11059 }
11060 pushback(p, c);
11061 return tAREF;
11062 }
11063 pushback(p, c);
11064 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11065 return '[';
11066 }
11067 else if (IS_BEG()) {
11068 c = tLBRACK;
11069 }
11070 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11071 c = tLBRACK;
11072 }
11073 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11074 COND_PUSH(0);
11075 CMDARG_PUSH(0);
11076 return c;
11077
11078 case '{':
11079 ++p->lex.brace_nest;
11080 if (lambda_beginning_p())
11081 c = tLAMBEG;
11082 else if (IS_lex_state(EXPR_LABELED))
11083 c = tLBRACE; /* hash */
11084 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11085 c = '{'; /* block (primary) */
11086 else if (IS_lex_state(EXPR_ENDARG))
11087 c = tLBRACE_ARG; /* block (expr) */
11088 else
11089 c = tLBRACE; /* hash */
11090 if (c != tLBRACE) {
11091 p->command_start = TRUE;
11092 SET_LEX_STATE(EXPR_BEG);
11093 }
11094 else {
11095 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11096 }
11097 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11098 COND_PUSH(0);
11099 CMDARG_PUSH(0);
11100 return c;
11101
11102 case '\\':
11103 c = nextc(p);
11104 if (c == '\n') {
11105 space_seen = 1;
11106 dispatch_scan_event(p, tSP);
11107 goto retry; /* skip \\n */
11108 }
11109 if (c == ' ') return tSP;
11110 if (ISSPACE(c)) return c;
11111 pushback(p, c);
11112 return '\\';
11113
11114 case '%':
11115 return parse_percent(p, space_seen, last_state);
11116
11117 case '$':
11118 return parse_gvar(p, last_state);
11119
11120 case '@':
11121 return parse_atmark(p, last_state);
11122
11123 case '_':
11124 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11125 p->ruby__end__seen = 1;
11126 p->eofp = 1;
11127#ifdef RIPPER
11128 lex_goto_eol(p);
11129 dispatch_scan_event(p, k__END__);
11130#endif
11131 return END_OF_INPUT;
11132 }
11133 newtok(p);
11134 break;
11135
11136 default:
11137 if (!parser_is_identchar(p)) {
11138 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11139 token_flush(p);
11140 goto retry;
11141 }
11142
11143 newtok(p);
11144 break;
11145 }
11146
11147 return parse_ident(p, c, cmd_state);
11148}
11149
11150static enum yytokentype
11151yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11152{
11153 enum yytokentype t;
11154
11155 p->lval = lval;
11156 lval->node = 0;
11157 p->yylloc = yylloc;
11158
11159 t = parser_yylex(p);
11160
11161 if (has_delayed_token(p))
11162 dispatch_delayed_token(p, t);
11163 else if (t != END_OF_INPUT)
11164 dispatch_scan_event(p, t);
11165
11166 return t;
11167}
11168
11169#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11170
11171static NODE*
11172node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11173{
11174 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11175
11176 rb_node_init(n, type);
11177 return n;
11178}
11179
11180static NODE *
11181nd_set_loc(NODE *nd, const YYLTYPE *loc)
11182{
11183 nd->nd_loc = *loc;
11184 nd_set_line(nd, loc->beg_pos.lineno);
11185 return nd;
11186}
11187
11188static NODE*
11189node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11190{
11191 NODE *n = node_new_internal(p, type, size, alignment);
11192
11193 nd_set_loc(n, loc);
11194 nd_set_node_id(n, parser_get_node_id(p));
11195 return n;
11196}
11197
11198#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11199
11200static rb_node_scope_t *
11201rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11202{
11203 rb_ast_id_table_t *nd_tbl;
11204 nd_tbl = local_tbl(p);
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_scope_t *
11214rb_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)
11215{
11216 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11217 n->nd_tbl = nd_tbl;
11218 n->nd_body = nd_body;
11219 n->nd_args = nd_args;
11220
11221 return n;
11222}
11223
11224static rb_node_defn_t *
11225rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11226{
11227 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11228 n->nd_mid = nd_mid;
11229 n->nd_defn = nd_defn;
11230
11231 return n;
11232}
11233
11234static rb_node_defs_t *
11235rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11236{
11237 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11238 n->nd_recv = nd_recv;
11239 n->nd_mid = nd_mid;
11240 n->nd_defn = nd_defn;
11241
11242 return n;
11243}
11244
11245static rb_node_block_t *
11246rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11247{
11248 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11249 n->nd_head = nd_head;
11250 n->nd_end = (NODE *)n;
11251 n->nd_next = 0;
11252
11253 return n;
11254}
11255
11256static rb_node_for_t *
11257rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *for_keyword_loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *do_keyword_loc, const YYLTYPE *end_keyword_loc)
11258{
11259 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11260 n->nd_body = nd_body;
11261 n->nd_iter = nd_iter;
11262 n->for_keyword_loc = *for_keyword_loc;
11263 n->in_keyword_loc = *in_keyword_loc;
11264 n->do_keyword_loc = *do_keyword_loc;
11265 n->end_keyword_loc = *end_keyword_loc;
11266
11267 return n;
11268}
11269
11270static rb_node_for_masgn_t *
11271rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11272{
11273 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11274 n->nd_var = nd_var;
11275
11276 return n;
11277}
11278
11279static rb_node_retry_t *
11280rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11281{
11282 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11283
11284 return n;
11285}
11286
11287static rb_node_begin_t *
11288rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11289{
11290 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11291 n->nd_body = nd_body;
11292
11293 return n;
11294}
11295
11296static rb_node_rescue_t *
11297rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11298{
11299 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11300 n->nd_head = nd_head;
11301 n->nd_resq = nd_resq;
11302 n->nd_else = nd_else;
11303
11304 return n;
11305}
11306
11307static rb_node_resbody_t *
11308rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11309{
11310 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11311 n->nd_args = nd_args;
11312 n->nd_exc_var = nd_exc_var;
11313 n->nd_body = nd_body;
11314 n->nd_next = nd_next;
11315
11316 return n;
11317}
11318
11319static rb_node_ensure_t *
11320rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11321{
11322 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11323 n->nd_head = nd_head;
11324 n->nd_ensr = nd_ensr;
11325
11326 return n;
11327}
11328
11329static rb_node_and_t *
11330rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11331{
11332 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11333 n->nd_1st = nd_1st;
11334 n->nd_2nd = nd_2nd;
11335 n->operator_loc = *operator_loc;
11336
11337 return n;
11338}
11339
11340static rb_node_or_t *
11341rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11342{
11343 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11344 n->nd_1st = nd_1st;
11345 n->nd_2nd = nd_2nd;
11346 n->operator_loc = *operator_loc;
11347
11348 return n;
11349}
11350
11351static rb_node_return_t *
11352rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11353{
11354 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11355 n->nd_stts = nd_stts;
11356 n->keyword_loc = *keyword_loc;
11357 return n;
11358}
11359
11360static rb_node_yield_t *
11361rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11362{
11363 if (nd_head) no_blockarg(p, nd_head);
11364
11365 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11366 n->nd_head = nd_head;
11367 n->keyword_loc = *keyword_loc;
11368 n->lparen_loc = *lparen_loc;
11369 n->rparen_loc = *rparen_loc;
11370
11371 return n;
11372}
11373
11374static rb_node_if_t *
11375rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE* if_keyword_loc, const YYLTYPE* then_keyword_loc, const YYLTYPE* end_keyword_loc)
11376{
11377 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11378 n->nd_cond = nd_cond;
11379 n->nd_body = nd_body;
11380 n->nd_else = nd_else;
11381 n->if_keyword_loc = *if_keyword_loc;
11382 n->then_keyword_loc = *then_keyword_loc;
11383 n->end_keyword_loc = *end_keyword_loc;
11384
11385 return n;
11386}
11387
11388static rb_node_unless_t *
11389rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *end_keyword_loc)
11390{
11391 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11392 n->nd_cond = nd_cond;
11393 n->nd_body = nd_body;
11394 n->nd_else = nd_else;
11395 n->keyword_loc = *keyword_loc;
11396 n->then_keyword_loc = *then_keyword_loc;
11397 n->end_keyword_loc = *end_keyword_loc;
11398
11399 return n;
11400}
11401
11402static rb_node_class_t *
11403rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *inheritance_operator_loc, const YYLTYPE *end_keyword_loc)
11404{
11405 /* Keep the order of node creation */
11406 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11407 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11408 n->nd_cpath = nd_cpath;
11409 n->nd_body = scope;
11410 n->nd_super = nd_super;
11411 n->class_keyword_loc = *class_keyword_loc;
11412 n->inheritance_operator_loc = *inheritance_operator_loc;
11413 n->end_keyword_loc = *end_keyword_loc;
11414
11415 return n;
11416}
11417
11418static rb_node_sclass_t *
11419rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc)
11420{
11421 /* Keep the order of node creation */
11422 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11423 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11424 n->nd_recv = nd_recv;
11425 n->nd_body = scope;
11426
11427 return n;
11428}
11429
11430static rb_node_module_t *
11431rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc)
11432{
11433 /* Keep the order of node creation */
11434 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11435 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11436 n->nd_cpath = nd_cpath;
11437 n->nd_body = scope;
11438
11439 return n;
11440}
11441
11442static rb_node_iter_t *
11443rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11444{
11445 /* Keep the order of node creation */
11446 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11447 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11448 n->nd_body = scope;
11449 n->nd_iter = 0;
11450
11451 return n;
11452}
11453
11454static rb_node_lambda_t *
11455rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
11456{
11457 /* Keep the order of node creation */
11458 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11459 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11460 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11461 n->nd_body = scope;
11462 n->operator_loc = *operator_loc;
11463 n->opening_loc = *opening_loc;
11464 n->closing_loc = *closing_loc;
11465
11466 return n;
11467}
11468
11469static rb_node_case_t *
11470rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11471{
11472 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11473 n->nd_head = nd_head;
11474 n->nd_body = nd_body;
11475 n->case_keyword_loc = *case_keyword_loc;
11476 n->end_keyword_loc = *end_keyword_loc;
11477
11478 return n;
11479}
11480
11481static rb_node_case2_t *
11482rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11483{
11484 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11485 n->nd_head = 0;
11486 n->nd_body = nd_body;
11487 n->case_keyword_loc = *case_keyword_loc;
11488 n->end_keyword_loc = *end_keyword_loc;
11489
11490 return n;
11491}
11492
11493static rb_node_case3_t *
11494rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11495{
11496 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11497 n->nd_head = nd_head;
11498 n->nd_body = nd_body;
11499 n->case_keyword_loc = *case_keyword_loc;
11500 n->end_keyword_loc = *end_keyword_loc;
11501
11502 return n;
11503}
11504
11505static rb_node_when_t *
11506rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc)
11507{
11508 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11509 n->nd_head = nd_head;
11510 n->nd_body = nd_body;
11511 n->nd_next = nd_next;
11512 n->keyword_loc = *keyword_loc;
11513 n->then_keyword_loc = *then_keyword_loc;
11514
11515 return n;
11516}
11517
11518static rb_node_in_t *
11519rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11520{
11521 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11522 n->nd_head = nd_head;
11523 n->nd_body = nd_body;
11524 n->nd_next = nd_next;
11525
11526 return n;
11527}
11528
11529static rb_node_while_t *
11530rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc)
11531{
11532 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11533 n->nd_cond = nd_cond;
11534 n->nd_body = nd_body;
11535 n->nd_state = nd_state;
11536 n->keyword_loc = *keyword_loc;
11537 n->closing_loc = *closing_loc;
11538
11539 return n;
11540}
11541
11542static rb_node_until_t *
11543rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc)
11544{
11545 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11546 n->nd_cond = nd_cond;
11547 n->nd_body = nd_body;
11548 n->nd_state = nd_state;
11549 n->keyword_loc = *keyword_loc;
11550 n->closing_loc = *closing_loc;
11551
11552 return n;
11553}
11554
11555static rb_node_colon2_t *
11556rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11557{
11558 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11559 n->nd_head = nd_head;
11560 n->nd_mid = nd_mid;
11561 n->delimiter_loc = *delimiter_loc;
11562 n->name_loc = *name_loc;
11563
11564 return n;
11565}
11566
11567static rb_node_colon3_t *
11568rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11569{
11570 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11571 n->nd_mid = nd_mid;
11572 n->delimiter_loc = *delimiter_loc;
11573 n->name_loc = *name_loc;
11574
11575 return n;
11576}
11577
11578static rb_node_dot2_t *
11579rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11580{
11581 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11582 n->nd_beg = nd_beg;
11583 n->nd_end = nd_end;
11584 n->operator_loc = *operator_loc;
11585
11586 return n;
11587}
11588
11589static rb_node_dot3_t *
11590rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11591{
11592 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11593 n->nd_beg = nd_beg;
11594 n->nd_end = nd_end;
11595 n->operator_loc = *operator_loc;
11596
11597 return n;
11598}
11599
11600static rb_node_self_t *
11601rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11602{
11603 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11604 n->nd_state = 1;
11605
11606 return n;
11607}
11608
11609static rb_node_nil_t *
11610rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11611{
11612 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11613
11614 return n;
11615}
11616
11617static rb_node_true_t *
11618rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11619{
11620 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11621
11622 return n;
11623}
11624
11625static rb_node_false_t *
11626rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11627{
11628 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11629
11630 return n;
11631}
11632
11633static rb_node_super_t *
11634rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11635 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11636{
11637 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11638 n->nd_args = nd_args;
11639 n->keyword_loc = *keyword_loc;
11640 n->lparen_loc = *lparen_loc;
11641 n->rparen_loc = *rparen_loc;
11642
11643 return n;
11644}
11645
11646static rb_node_zsuper_t *
11647rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11648{
11649 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11650
11651 return n;
11652}
11653
11654static rb_node_match2_t *
11655rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11656{
11657 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11658 n->nd_recv = nd_recv;
11659 n->nd_value = nd_value;
11660 n->nd_args = 0;
11661
11662 return n;
11663}
11664
11665static rb_node_match3_t *
11666rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11667{
11668 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11669 n->nd_recv = nd_recv;
11670 n->nd_value = nd_value;
11671
11672 return n;
11673}
11674
11675/* TODO: Use union for NODE_LIST2 */
11676static rb_node_list_t *
11677rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11678{
11679 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11680 n->nd_head = nd_head;
11681 n->as.nd_alen = 1;
11682 n->nd_next = 0;
11683
11684 return n;
11685}
11686
11687static rb_node_list_t *
11688rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11689{
11690 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11691 n->nd_head = nd_head;
11692 n->as.nd_alen = nd_alen;
11693 n->nd_next = nd_next;
11694
11695 return n;
11696}
11697
11698static rb_node_zlist_t *
11699rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11700{
11701 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11702
11703 return n;
11704}
11705
11706static rb_node_hash_t *
11707rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11708{
11709 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11710 n->nd_head = nd_head;
11711 n->nd_brace = 0;
11712
11713 return n;
11714}
11715
11716static rb_node_masgn_t *
11717rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11718{
11719 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11720 n->nd_head = nd_head;
11721 n->nd_value = 0;
11722 n->nd_args = nd_args;
11723
11724 return n;
11725}
11726
11727static rb_node_gasgn_t *
11728rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11729{
11730 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11731 n->nd_vid = nd_vid;
11732 n->nd_value = nd_value;
11733
11734 return n;
11735}
11736
11737static rb_node_lasgn_t *
11738rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11739{
11740 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11741 n->nd_vid = nd_vid;
11742 n->nd_value = nd_value;
11743
11744 return n;
11745}
11746
11747static rb_node_dasgn_t *
11748rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11749{
11750 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11751 n->nd_vid = nd_vid;
11752 n->nd_value = nd_value;
11753
11754 return n;
11755}
11756
11757static rb_node_iasgn_t *
11758rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11759{
11760 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11761 n->nd_vid = nd_vid;
11762 n->nd_value = nd_value;
11763
11764 return n;
11765}
11766
11767static rb_node_cvasgn_t *
11768rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11769{
11770 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11771 n->nd_vid = nd_vid;
11772 n->nd_value = nd_value;
11773
11774 return n;
11775}
11776
11777static rb_node_op_asgn1_t *
11778rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
11779{
11780 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11781 n->nd_recv = nd_recv;
11782 n->nd_mid = nd_mid;
11783 n->nd_index = index;
11784 n->nd_rvalue = rvalue;
11785 n->call_operator_loc = *call_operator_loc;
11786 n->opening_loc = *opening_loc;
11787 n->closing_loc = *closing_loc;
11788 n->binary_operator_loc = *binary_operator_loc;
11789
11790 return n;
11791}
11792
11793static rb_node_op_asgn2_t *
11794rb_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, const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
11795{
11796 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11797 n->nd_recv = nd_recv;
11798 n->nd_value = nd_value;
11799 n->nd_vid = nd_vid;
11800 n->nd_mid = nd_mid;
11801 n->nd_aid = nd_aid;
11802 n->call_operator_loc = *call_operator_loc;
11803 n->message_loc = *message_loc;
11804 n->binary_operator_loc = *binary_operator_loc;
11805
11806 return n;
11807}
11808
11809static rb_node_op_asgn_or_t *
11810rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11811{
11812 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11813 n->nd_head = nd_head;
11814 n->nd_value = nd_value;
11815
11816 return n;
11817}
11818
11819static rb_node_op_asgn_and_t *
11820rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11821{
11822 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11823 n->nd_head = nd_head;
11824 n->nd_value = nd_value;
11825
11826 return n;
11827}
11828
11829static rb_node_gvar_t *
11830rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11831{
11832 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11833 n->nd_vid = nd_vid;
11834
11835 return n;
11836}
11837
11838static rb_node_lvar_t *
11839rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11840{
11841 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11842 n->nd_vid = nd_vid;
11843
11844 return n;
11845}
11846
11847static rb_node_dvar_t *
11848rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11849{
11850 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11851 n->nd_vid = nd_vid;
11852
11853 return n;
11854}
11855
11856static rb_node_ivar_t *
11857rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11858{
11859 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11860 n->nd_vid = nd_vid;
11861
11862 return n;
11863}
11864
11865static rb_node_const_t *
11866rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11867{
11868 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11869 n->nd_vid = nd_vid;
11870
11871 return n;
11872}
11873
11874static rb_node_cvar_t *
11875rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11876{
11877 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11878 n->nd_vid = nd_vid;
11879
11880 return n;
11881}
11882
11883static rb_node_nth_ref_t *
11884rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11885{
11886 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11887 n->nd_nth = nd_nth;
11888
11889 return n;
11890}
11891
11892static rb_node_back_ref_t *
11893rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11894{
11895 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11896 n->nd_nth = nd_nth;
11897
11898 return n;
11899}
11900
11901static rb_node_integer_t *
11902rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11903{
11904 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11905 n->val = val;
11906 n->minus = FALSE;
11907 n->base = base;
11908
11909 return n;
11910}
11911
11912static rb_node_float_t *
11913rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11914{
11915 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
11916 n->val = val;
11917 n->minus = FALSE;
11918
11919 return n;
11920}
11921
11922static rb_node_rational_t *
11923rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
11924{
11925 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
11926 n->val = val;
11927 n->minus = FALSE;
11928 n->base = base;
11929 n->seen_point = seen_point;
11930
11931 return n;
11932}
11933
11934static rb_node_imaginary_t *
11935rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
11936{
11937 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
11938 n->val = val;
11939 n->minus = FALSE;
11940 n->base = base;
11941 n->seen_point = seen_point;
11942 n->type = numeric_type;
11943
11944 return n;
11945}
11946
11947static rb_node_str_t *
11948rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
11949{
11950 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
11951 n->string = string;
11952
11953 return n;
11954}
11955
11956/* TODO; Use union for NODE_DSTR2 */
11957static rb_node_dstr_t *
11958rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11959{
11960 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
11961 n->string = string;
11962 n->as.nd_alen = nd_alen;
11963 n->nd_next = (rb_node_list_t *)nd_next;
11964
11965 return n;
11966}
11967
11968static rb_node_dstr_t *
11969rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
11970{
11971 return rb_node_dstr_new0(p, string, 1, 0, loc);
11972}
11973
11974static rb_node_xstr_t *
11975rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
11976{
11977 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
11978 n->string = string;
11979
11980 return n;
11981}
11982
11983static rb_node_dxstr_t *
11984rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11985{
11986 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
11987 n->string = string;
11988 n->as.nd_alen = nd_alen;
11989 n->nd_next = (rb_node_list_t *)nd_next;
11990
11991 return n;
11992}
11993
11994static rb_node_sym_t *
11995rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
11996{
11997 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
11998 n->string = rb_str_to_parser_string(p, str);
11999
12000 return n;
12001}
12002
12003static rb_node_dsym_t *
12004rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12005{
12006 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12007 n->string = string;
12008 n->as.nd_alen = nd_alen;
12009 n->nd_next = (rb_node_list_t *)nd_next;
12010
12011 return n;
12012}
12013
12014static rb_node_evstr_t *
12015rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12016{
12017 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12018 n->nd_body = nd_body;
12019 n->opening_loc = *opening_loc;
12020 n->closing_loc = *closing_loc;
12021
12022 return n;
12023}
12024
12025static rb_node_regx_t *
12026rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12027{
12028 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12029 n->string = string;
12030 n->options = options & RE_OPTION_MASK;
12031 n->opening_loc = *opening_loc;
12032 n->content_loc = *content_loc;
12033 n->closing_loc = *closing_loc;
12034
12035 return n;
12036}
12037
12038static rb_node_call_t *
12039rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12040{
12041 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12042 n->nd_recv = nd_recv;
12043 n->nd_mid = nd_mid;
12044 n->nd_args = nd_args;
12045
12046 return n;
12047}
12048
12049static rb_node_opcall_t *
12050rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12051{
12052 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12053 n->nd_recv = nd_recv;
12054 n->nd_mid = nd_mid;
12055 n->nd_args = nd_args;
12056
12057 return n;
12058}
12059
12060static rb_node_fcall_t *
12061rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12062{
12063 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12064 n->nd_mid = nd_mid;
12065 n->nd_args = nd_args;
12066
12067 return n;
12068}
12069
12070static rb_node_qcall_t *
12071rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12072{
12073 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12074 n->nd_recv = nd_recv;
12075 n->nd_mid = nd_mid;
12076 n->nd_args = nd_args;
12077
12078 return n;
12079}
12080
12081static rb_node_vcall_t *
12082rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12083{
12084 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12085 n->nd_mid = nd_mid;
12086
12087 return n;
12088}
12089
12090static rb_node_once_t *
12091rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12092{
12093 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12094 n->nd_body = nd_body;
12095
12096 return n;
12097}
12098
12099static rb_node_args_t *
12100rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12101{
12102 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12103 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12104
12105 return n;
12106}
12107
12108static rb_node_args_aux_t *
12109rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12110{
12111 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12112 n->nd_pid = nd_pid;
12113 n->nd_plen = nd_plen;
12114 n->nd_next = 0;
12115
12116 return n;
12117}
12118
12119static rb_node_opt_arg_t *
12120rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12121{
12122 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12123 n->nd_body = nd_body;
12124 n->nd_next = 0;
12125
12126 return n;
12127}
12128
12129static rb_node_kw_arg_t *
12130rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12131{
12132 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12133 n->nd_body = nd_body;
12134 n->nd_next = 0;
12135
12136 return n;
12137}
12138
12139static rb_node_postarg_t *
12140rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12141{
12142 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12143 n->nd_1st = nd_1st;
12144 n->nd_2nd = nd_2nd;
12145
12146 return n;
12147}
12148
12149static rb_node_argscat_t *
12150rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12151{
12152 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12153 n->nd_head = nd_head;
12154 n->nd_body = nd_body;
12155
12156 return n;
12157}
12158
12159static rb_node_argspush_t *
12160rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12161{
12162 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12163 n->nd_head = nd_head;
12164 n->nd_body = nd_body;
12165
12166 return n;
12167}
12168
12169static rb_node_splat_t *
12170rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12171{
12172 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12173 n->nd_head = nd_head;
12174 n->operator_loc = *operator_loc;
12175
12176 return n;
12177}
12178
12179static rb_node_block_pass_t *
12180rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12181{
12182 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12183 n->forwarding = 0;
12184 n->nd_head = 0;
12185 n->nd_body = nd_body;
12186 n->operator_loc = *operator_loc;
12187
12188 return n;
12189}
12190
12191static rb_node_alias_t *
12192rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12193{
12194 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12195 n->nd_1st = nd_1st;
12196 n->nd_2nd = nd_2nd;
12197 n->keyword_loc = *keyword_loc;
12198
12199 return n;
12200}
12201
12202static rb_node_valias_t *
12203rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12204{
12205 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12206 n->nd_alias = nd_alias;
12207 n->nd_orig = nd_orig;
12208 n->keyword_loc = *keyword_loc;
12209
12210 return n;
12211}
12212
12213static rb_node_undef_t *
12214rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12215{
12216 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12217 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12218 n->keyword_loc = NULL_LOC;
12219 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12220
12221 return n;
12222}
12223
12224static rb_node_errinfo_t *
12225rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12226{
12227 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12228
12229 return n;
12230}
12231
12232static rb_node_defined_t *
12233rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12234{
12235 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12236 n->nd_head = nd_head;
12237
12238 return n;
12239}
12240
12241static rb_node_postexe_t *
12242rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12243{
12244 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12245 n->nd_body = nd_body;
12246 n->keyword_loc = *keyword_loc;
12247 n->opening_loc = *opening_loc;
12248 n->closing_loc = *closing_loc;
12249
12250 return n;
12251}
12252
12253static rb_node_attrasgn_t *
12254rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12255{
12256 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12257 n->nd_recv = nd_recv;
12258 n->nd_mid = nd_mid;
12259 n->nd_args = nd_args;
12260
12261 return n;
12262}
12263
12264static rb_node_aryptn_t *
12265rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12266{
12267 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12268 n->nd_pconst = 0;
12269 n->pre_args = pre_args;
12270 n->rest_arg = rest_arg;
12271 n->post_args = post_args;
12272
12273 return n;
12274}
12275
12276static rb_node_hshptn_t *
12277rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12278{
12279 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12280 n->nd_pconst = nd_pconst;
12281 n->nd_pkwargs = nd_pkwargs;
12282 n->nd_pkwrestarg = nd_pkwrestarg;
12283
12284 return n;
12285}
12286
12287static rb_node_fndptn_t *
12288rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12289{
12290 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12291 n->nd_pconst = 0;
12292 n->pre_rest_arg = pre_rest_arg;
12293 n->args = args;
12294 n->post_rest_arg = post_rest_arg;
12295
12296 return n;
12297}
12298
12299static rb_node_line_t *
12300rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12301{
12302 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12303
12304 return n;
12305}
12306
12307static rb_node_file_t *
12308rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12309{
12310 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12311 n->path = rb_str_to_parser_string(p, str);
12312
12313 return n;
12314}
12315
12316static rb_node_encoding_t *
12317rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12318{
12319 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12320 n->enc = p->enc;
12321
12322 return n;
12323}
12324
12325static rb_node_cdecl_t *
12326rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12327{
12328 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12329 n->nd_vid = nd_vid;
12330 n->nd_value = nd_value;
12331 n->nd_else = nd_else;
12332 n->shareability = shareability;
12333
12334 return n;
12335}
12336
12337static rb_node_op_cdecl_t *
12338rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12339{
12340 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12341 n->nd_head = nd_head;
12342 n->nd_value = nd_value;
12343 n->nd_aid = nd_aid;
12344 n->shareability = shareability;
12345
12346 return n;
12347}
12348
12349static rb_node_error_t *
12350rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12351{
12352 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12353
12354 return n;
12355}
12356
12357static rb_node_break_t *
12358rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12359{
12360 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12361 n->nd_stts = nd_stts;
12362 n->nd_chain = 0;
12363 n->keyword_loc = *keyword_loc;
12364
12365 return n;
12366}
12367
12368static rb_node_next_t *
12369rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12370{
12371 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12372 n->nd_stts = nd_stts;
12373 n->nd_chain = 0;
12374 n->keyword_loc = *keyword_loc;
12375
12376 return n;
12377}
12378
12379static rb_node_redo_t *
12380rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12381{
12382 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12383 n->nd_chain = 0;
12384 n->keyword_loc = *keyword_loc;
12385
12386 return n;
12387}
12388
12389static rb_node_def_temp_t *
12390rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12391{
12392 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12393 n->save.numparam_save = 0;
12394 n->save.max_numparam = 0;
12395 n->save.ctxt = p->ctxt;
12396 n->nd_def = 0;
12397 n->nd_mid = 0;
12398
12399 return n;
12400}
12401
12402static rb_node_def_temp_t *
12403def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12404{
12405 n->save.numparam_save = numparam_push(p);
12406 n->save.max_numparam = p->max_numparam;
12407 return n;
12408}
12409
12410#ifndef RIPPER
12411static enum node_type
12412nodetype(NODE *node) /* for debug */
12413{
12414 return (enum node_type)nd_type(node);
12415}
12416
12417static int
12418nodeline(NODE *node)
12419{
12420 return nd_line(node);
12421}
12422#endif
12423
12424static NODE*
12425newline_node(NODE *node)
12426{
12427 if (node) {
12428 node = remove_begin(node);
12429 nd_set_fl_newline(node);
12430 }
12431 return node;
12432}
12433
12434static void
12435fixpos(NODE *node, NODE *orig)
12436{
12437 if (!node) return;
12438 if (!orig) return;
12439 nd_set_line(node, nd_line(orig));
12440}
12441
12442static NODE*
12443block_append(struct parser_params *p, NODE *head, NODE *tail)
12444{
12445 NODE *end, *h = head, *nd;
12446
12447 if (tail == 0) return head;
12448
12449 if (h == 0) return tail;
12450 switch (nd_type(h)) {
12451 default:
12452 h = end = NEW_BLOCK(head, &head->nd_loc);
12453 head = end;
12454 break;
12455 case NODE_BLOCK:
12456 end = RNODE_BLOCK(h)->nd_end;
12457 break;
12458 }
12459
12460 nd = RNODE_BLOCK(end)->nd_head;
12461 switch (nd_type(nd)) {
12462 case NODE_RETURN:
12463 case NODE_BREAK:
12464 case NODE_NEXT:
12465 case NODE_REDO:
12466 case NODE_RETRY:
12467 rb_warning0L(nd_line(tail), "statement not reached");
12468 break;
12469
12470 default:
12471 break;
12472 }
12473
12474 if (!nd_type_p(tail, NODE_BLOCK)) {
12475 tail = NEW_BLOCK(tail, &tail->nd_loc);
12476 }
12477 RNODE_BLOCK(end)->nd_next = tail;
12478 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12479 nd_set_last_loc(head, nd_last_loc(tail));
12480 return head;
12481}
12482
12483/* append item to the list */
12484static NODE*
12485list_append(struct parser_params *p, NODE *list, NODE *item)
12486{
12487 NODE *last;
12488
12489 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12490 if (RNODE_LIST(list)->nd_next) {
12491 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12492 }
12493 else {
12494 last = list;
12495 }
12496
12497 RNODE_LIST(list)->as.nd_alen += 1;
12498 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12499 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12500
12501 nd_set_last_loc(list, nd_last_loc(item));
12502
12503 return list;
12504}
12505
12506/* concat two lists */
12507static NODE*
12508list_concat(NODE *head, NODE *tail)
12509{
12510 NODE *last;
12511
12512 if (RNODE_LIST(head)->nd_next) {
12513 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12514 }
12515 else {
12516 last = head;
12517 }
12518
12519 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12520 RNODE_LIST(last)->nd_next = tail;
12521 if (RNODE_LIST(tail)->nd_next) {
12522 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12523 }
12524 else {
12525 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12526 }
12527
12528 nd_set_last_loc(head, nd_last_loc(tail));
12529
12530 return head;
12531}
12532
12533static int
12534literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12535{
12536 if (!tail) return 1;
12537 if (!rb_parser_enc_compatible(p, head, tail)) {
12538 compile_error(p, "string literal encodings differ (%s / %s)",
12539 rb_enc_name(rb_parser_str_get_encoding(head)),
12540 rb_enc_name(rb_parser_str_get_encoding(tail)));
12541 rb_parser_str_resize(p, head, 0);
12542 rb_parser_str_resize(p, tail, 0);
12543 return 0;
12544 }
12545 rb_parser_str_buf_append(p, head, tail);
12546 return 1;
12547}
12548
12549static rb_parser_string_t *
12550string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12551{
12552 if (htype != NODE_DSTR) return NULL;
12553 if (RNODE_DSTR(head)->nd_next) {
12554 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12555 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12556 }
12557 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12558 ASSUME(lit);
12559 return lit;
12560}
12561
12562#ifndef RIPPER
12563static rb_parser_string_t *
12564rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12565{
12566 rb_parser_string_t *copy;
12567 if (!orig) return NULL;
12568 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12569 copy->coderange = orig->coderange;
12570 copy->enc = orig->enc;
12571 return copy;
12572}
12573#endif
12574
12575/* concat two string literals */
12576static NODE *
12577literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12578{
12579 enum node_type htype;
12580 rb_parser_string_t *lit;
12581
12582 if (!head) return tail;
12583 if (!tail) return head;
12584
12585 htype = nd_type(head);
12586 if (htype == NODE_EVSTR) {
12587 head = new_dstr(p, head, loc);
12588 htype = NODE_DSTR;
12589 }
12590 if (p->heredoc_indent > 0) {
12591 switch (htype) {
12592 case NODE_STR:
12593 head = str2dstr(p, head);
12594 case NODE_DSTR:
12595 return list_append(p, head, tail);
12596 default:
12597 break;
12598 }
12599 }
12600 switch (nd_type(tail)) {
12601 case NODE_STR:
12602 if ((lit = string_literal_head(p, htype, head)) != false) {
12603 htype = NODE_STR;
12604 }
12605 else {
12606 lit = RNODE_DSTR(head)->string;
12607 }
12608 if (htype == NODE_STR) {
12609 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12610 error:
12611 rb_discard_node(p, head);
12612 rb_discard_node(p, tail);
12613 return 0;
12614 }
12615 rb_discard_node(p, tail);
12616 }
12617 else {
12618 list_append(p, head, tail);
12619 }
12620 break;
12621
12622 case NODE_DSTR:
12623 if (htype == NODE_STR) {
12624 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12625 goto error;
12626 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12627 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12628 RNODE_STR(head)->string = NULL;
12629 rb_discard_node(p, head);
12630 head = tail;
12631 }
12632 else if (!RNODE_DSTR(tail)->string) {
12633 append:
12634 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12635 if (!RNODE_DSTR(head)->nd_next) {
12636 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12637 }
12638 else if (RNODE_DSTR(tail)->nd_next) {
12639 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12640 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12641 }
12642 rb_discard_node(p, tail);
12643 }
12644 else if ((lit = string_literal_head(p, htype, head)) != false) {
12645 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12646 goto error;
12647 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12648 RNODE_DSTR(tail)->string = 0;
12649 goto append;
12650 }
12651 else {
12652 list_concat(head, NEW_LIST2(NEW_STR(RNODE_DSTR(tail)->string, loc), RNODE_DSTR(tail)->as.nd_alen, (NODE *)RNODE_DSTR(tail)->nd_next, loc));
12653 RNODE_DSTR(tail)->string = 0;
12654 }
12655 break;
12656
12657 case NODE_EVSTR:
12658 if (htype == NODE_STR) {
12659 head = str2dstr(p, head);
12660 RNODE_DSTR(head)->as.nd_alen = 1;
12661 }
12662 list_append(p, head, tail);
12663 break;
12664 }
12665 return head;
12666}
12667
12668static void
12669nd_copy_flag(NODE *new_node, NODE *old_node)
12670{
12671 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12672 nd_set_line(new_node, nd_line(old_node));
12673 new_node->nd_loc = old_node->nd_loc;
12674 new_node->node_id = old_node->node_id;
12675}
12676
12677static NODE *
12678str2dstr(struct parser_params *p, NODE *node)
12679{
12680 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12681 nd_copy_flag(new_node, node);
12682 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12683 RNODE_DSTR(new_node)->as.nd_alen = 0;
12684 RNODE_DSTR(new_node)->nd_next = 0;
12685 RNODE_STR(node)->string = 0;
12686
12687 return new_node;
12688}
12689
12690static NODE *
12691str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12692{
12693 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12694 nd_copy_flag(new_node, node);
12695 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12696 RNODE_REGX(new_node)->options = options;
12697 nd_set_loc(new_node, loc);
12698 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12699 RNODE_REGX(new_node)->content_loc = *content_loc;
12700 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12701 RNODE_STR(node)->string = 0;
12702
12703 return new_node;
12704}
12705
12706static NODE *
12707evstr2dstr(struct parser_params *p, NODE *node)
12708{
12709 if (nd_type_p(node, NODE_EVSTR)) {
12710 node = new_dstr(p, node, &node->nd_loc);
12711 }
12712 return node;
12713}
12714
12715static NODE *
12716new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12717{
12718 NODE *head = node;
12719
12720 if (node) {
12721 switch (nd_type(node)) {
12722 case NODE_STR:
12723 return str2dstr(p, node);
12724 case NODE_DSTR:
12725 break;
12726 case NODE_EVSTR:
12727 return node;
12728 }
12729 }
12730 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12731}
12732
12733static NODE *
12734new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12735{
12736 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12737 return list_append(p, dstr, node);
12738}
12739
12740static NODE *
12741call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12742 const YYLTYPE *op_loc, const YYLTYPE *loc)
12743{
12744 NODE *expr;
12745 value_expr(recv);
12746 value_expr(arg1);
12747 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12748 nd_set_line(expr, op_loc->beg_pos.lineno);
12749 return expr;
12750}
12751
12752static NODE *
12753call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12754{
12755 NODE *opcall;
12756 value_expr(recv);
12757 opcall = NEW_OPCALL(recv, id, 0, loc);
12758 nd_set_line(opcall, op_loc->beg_pos.lineno);
12759 return opcall;
12760}
12761
12762static NODE *
12763new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12764{
12765 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12766 nd_set_line(qcall, op_loc->beg_pos.lineno);
12767 return qcall;
12768}
12769
12770static NODE*
12771new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12772{
12773 NODE *ret;
12774 if (block) block_dup_check(p, args, block);
12775 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12776 if (block) ret = method_add_block(p, ret, block, loc);
12777 fixpos(ret, recv);
12778 return ret;
12779}
12780
12781static rb_locations_lambda_body_t*
12782new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12783{
12784 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12785 body->node = node;
12786 body->opening_loc = *opening_loc;
12787 body->closing_loc = *closing_loc;
12788 return body;
12789}
12790
12791#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12792
12793static NODE*
12794last_expr_once_body(NODE *node)
12795{
12796 if (!node) return 0;
12797 return nd_once_body(node);
12798}
12799
12800static NODE*
12801match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12802{
12803 NODE *n;
12804 int line = op_loc->beg_pos.lineno;
12805
12806 value_expr(node1);
12807 value_expr(node2);
12808
12809 if ((n = last_expr_once_body(node1)) != 0) {
12810 switch (nd_type(n)) {
12811 case NODE_DREGX:
12812 {
12813 NODE *match = NEW_MATCH2(node1, node2, loc);
12814 nd_set_line(match, line);
12815 return match;
12816 }
12817
12818 case NODE_REGX:
12819 {
12820 const VALUE lit = rb_node_regx_string_val(n);
12821 if (!NIL_P(lit)) {
12822 NODE *match = NEW_MATCH2(node1, node2, loc);
12823 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12824 nd_set_line(match, line);
12825 return match;
12826 }
12827 }
12828 }
12829 }
12830
12831 if ((n = last_expr_once_body(node2)) != 0) {
12832 NODE *match3;
12833
12834 switch (nd_type(n)) {
12835 case NODE_DREGX:
12836 match3 = NEW_MATCH3(node2, node1, loc);
12837 return match3;
12838 }
12839 }
12840
12841 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12842 nd_set_line(n, line);
12843 return n;
12844}
12845
12846# if WARN_PAST_SCOPE
12847static int
12848past_dvar_p(struct parser_params *p, ID id)
12849{
12850 struct vtable *past = p->lvtbl->past;
12851 while (past) {
12852 if (vtable_included(past, id)) return 1;
12853 past = past->prev;
12854 }
12855 return 0;
12856}
12857# endif
12858
12859static int
12860numparam_nested_p(struct parser_params *p)
12861{
12862 struct local_vars *local = p->lvtbl;
12863 NODE *outer = local->numparam.outer;
12864 NODE *inner = local->numparam.inner;
12865 if (outer || inner) {
12866 NODE *used = outer ? outer : inner;
12867 compile_error(p, "numbered parameter is already used in %s block\n"
12868 "%s:%d: numbered parameter is already used here",
12869 outer ? "outer" : "inner",
12870 p->ruby_sourcefile, nd_line(used));
12871 parser_show_error_line(p, &used->nd_loc);
12872 return 1;
12873 }
12874 return 0;
12875}
12876
12877static int
12878numparam_used_p(struct parser_params *p)
12879{
12880 NODE *numparam = p->lvtbl->numparam.current;
12881 if (numparam) {
12882 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
12883 "%s:%d: numbered parameter is already used here",
12884 p->ruby_sourcefile, nd_line(numparam));
12885 parser_show_error_line(p, &numparam->nd_loc);
12886 return 1;
12887 }
12888 return 0;
12889}
12890
12891static int
12892it_used_p(struct parser_params *p)
12893{
12894 NODE *it = p->lvtbl->it;
12895 if (it) {
12896 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
12897 "%s:%d: 'it' is already used here",
12898 p->ruby_sourcefile, nd_line(it));
12899 parser_show_error_line(p, &it->nd_loc);
12900 return 1;
12901 }
12902 return 0;
12903}
12904
12905static NODE*
12906gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12907{
12908 ID *vidp = NULL;
12909 NODE *node;
12910 switch (id) {
12911 case keyword_self:
12912 return NEW_SELF(loc);
12913 case keyword_nil:
12914 return NEW_NIL(loc);
12915 case keyword_true:
12916 return NEW_TRUE(loc);
12917 case keyword_false:
12918 return NEW_FALSE(loc);
12919 case keyword__FILE__:
12920 {
12921 VALUE file = p->ruby_sourcefile_string;
12922 if (NIL_P(file))
12923 file = rb_str_new(0, 0);
12924 node = NEW_FILE(file, loc);
12925 }
12926 return node;
12927 case keyword__LINE__:
12928 return NEW_LINE(loc);
12929 case keyword__ENCODING__:
12930 return NEW_ENCODING(loc);
12931
12932 }
12933 switch (id_type(id)) {
12934 case ID_LOCAL:
12935 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
12936 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
12937 if (vidp) *vidp |= LVAR_USED;
12938 node = NEW_DVAR(id, loc);
12939 return node;
12940 }
12941 if (local_id_ref(p, id, &vidp)) {
12942 if (vidp) *vidp |= LVAR_USED;
12943 node = NEW_LVAR(id, loc);
12944 return node;
12945 }
12946 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
12947 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
12948 if (numparam_nested_p(p) || it_used_p(p)) return 0;
12949 node = NEW_DVAR(id, loc);
12950 struct local_vars *local = p->lvtbl;
12951 if (!local->numparam.current) local->numparam.current = node;
12952 return node;
12953 }
12954# if WARN_PAST_SCOPE
12955 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
12956 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
12957 }
12958# endif
12959 /* method call without arguments */
12960 if (dyna_in_block(p) && id == rb_intern("it") && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
12961 if (numparam_used_p(p)) return 0;
12962 if (p->max_numparam == ORDINAL_PARAM) {
12963 compile_error(p, "ordinary parameter is defined");
12964 return 0;
12965 }
12966 if (!p->it_id) {
12967 p->it_id = internal_id(p);
12968 vtable_add(p->lvtbl->args, p->it_id);
12969 }
12970 NODE *node = NEW_DVAR(p->it_id, loc);
12971 if (!p->lvtbl->it) p->lvtbl->it = node;
12972 return node;
12973 }
12974 return NEW_VCALL(id, loc);
12975 case ID_GLOBAL:
12976 return NEW_GVAR(id, loc);
12977 case ID_INSTANCE:
12978 return NEW_IVAR(id, loc);
12979 case ID_CONST:
12980 return NEW_CONST(id, loc);
12981 case ID_CLASS:
12982 return NEW_CVAR(id, loc);
12983 }
12984 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
12985 return 0;
12986}
12987
12988static rb_node_opt_arg_t *
12989opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
12990{
12991 rb_node_opt_arg_t *opts = opt_list;
12992 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
12993
12994 while (opts->nd_next) {
12995 opts = opts->nd_next;
12996 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
12997 }
12998 opts->nd_next = opt;
12999
13000 return opt_list;
13001}
13002
13003static rb_node_kw_arg_t *
13004kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13005{
13006 if (kwlist) {
13007 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13008 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13009 }
13010 return kwlist;
13011}
13012
13013static NODE *
13014new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
13015{
13016 NODE *n = expr;
13017 while (n) {
13018 if (nd_type_p(n, NODE_BEGIN)) {
13019 n = RNODE_BEGIN(n)->nd_body;
13020 }
13021 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13022 n = RNODE_BLOCK(n)->nd_head;
13023 }
13024 else {
13025 break;
13026 }
13027 }
13028 return NEW_DEFINED(n, loc);
13029}
13030
13031static NODE*
13032str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13033{
13034 VALUE lit;
13035 rb_parser_string_t *str = RNODE_STR(node)->string;
13036 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13037 yyerror1(loc, "invalid symbol");
13038 lit = STR_NEW0();
13039 }
13040 else {
13041 lit = rb_str_new_parser_string(str);
13042 }
13043 return NEW_SYM(lit, loc);
13044}
13045
13046static NODE*
13047symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13048{
13049 enum node_type type = nd_type(symbol);
13050 switch (type) {
13051 case NODE_DSTR:
13052 nd_set_type(symbol, NODE_DSYM);
13053 break;
13054 case NODE_STR:
13055 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13056 break;
13057 default:
13058 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13059 }
13060 return list_append(p, symbols, symbol);
13061}
13062
13063static void
13064dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13065{
13066 if (dreg->string) {
13067 reg_fragment_setenc(p, dreg->string, options);
13068 }
13069 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13070 NODE *frag = list->nd_head;
13071 if (nd_type_p(frag, NODE_STR)) {
13072 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13073 }
13074 else if (nd_type_p(frag, NODE_DSTR)) {
13075 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13076 }
13077 }
13078}
13079
13080static NODE *
13081new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13082{
13083 if (!node) {
13084 /* Check string is valid regex */
13085 rb_parser_string_t *str = STRING_NEW0();
13086 reg_compile(p, str, options);
13087 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13088 return node;
13089 }
13090 switch (nd_type(node)) {
13091 case NODE_STR:
13092 {
13093 /* Check string is valid regex */
13094 reg_compile(p, RNODE_STR(node)->string, options);
13095 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13096 }
13097 break;
13098 default:
13099 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13100 /* fall through */
13101 case NODE_DSTR:
13102 nd_set_type(node, NODE_DREGX);
13103 nd_set_loc(node, loc);
13104 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13105 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13106 if (dreg->nd_next) {
13107 dregex_fragment_setenc(p, dreg, options);
13108 }
13109 if (options & RE_OPTION_ONCE) {
13110 node = NEW_ONCE(node, loc);
13111 }
13112 break;
13113 }
13114 return node;
13115}
13116
13117static rb_node_kw_arg_t *
13118new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13119{
13120 if (!k) return 0;
13121 return NEW_KW_ARG((k), loc);
13122}
13123
13124static NODE *
13125new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13126{
13127 if (!node) {
13128 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13129 return xstr;
13130 }
13131 switch (nd_type(node)) {
13132 case NODE_STR:
13133 nd_set_type(node, NODE_XSTR);
13134 nd_set_loc(node, loc);
13135 break;
13136 case NODE_DSTR:
13137 nd_set_type(node, NODE_DXSTR);
13138 nd_set_loc(node, loc);
13139 break;
13140 default:
13141 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13142 break;
13143 }
13144 return node;
13145}
13146
13147static const
13148struct st_hash_type literal_type = {
13149 literal_cmp,
13150 literal_hash,
13151};
13152
13153static int nd_type_st_key_enable_p(NODE *node);
13154
13155static void
13156check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13157{
13158 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13159 if (!arg || !p->case_labels) return;
13160 if (!nd_type_st_key_enable_p(arg)) return;
13161
13162 if (p->case_labels == CHECK_LITERAL_WHEN) {
13163 p->case_labels = st_init_table(&literal_type);
13164 }
13165 else {
13166 st_data_t line;
13167 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13168 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13169 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13170 return;
13171 }
13172 }
13173 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13174}
13175
13176#ifdef RIPPER
13177static int
13178id_is_var(struct parser_params *p, ID id)
13179{
13180 if (is_notop_id(id)) {
13181 switch (id & ID_SCOPE_MASK) {
13182 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13183 return 1;
13184 case ID_LOCAL:
13185 if (dyna_in_block(p)) {
13186 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13187 }
13188 if (local_id(p, id)) return 1;
13189 /* method call without arguments */
13190 return 0;
13191 }
13192 }
13193 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13194 return 0;
13195}
13196#endif
13197
13198static inline enum lex_state_e
13199parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13200{
13201 if (p->debug) {
13202 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13203 }
13204 return p->lex.state = ls;
13205}
13206
13207#ifndef RIPPER
13208static void
13209flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13210{
13211 VALUE mesg = p->debug_buffer;
13212
13213 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13214 p->debug_buffer = Qnil;
13215 rb_io_puts(1, &mesg, out);
13216 }
13217 if (!NIL_P(str) && RSTRING_LEN(str)) {
13218 rb_io_write(p->debug_output, str);
13219 }
13220}
13221
13222static const char rb_parser_lex_state_names[][8] = {
13223 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13224 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13225 "LABEL", "LABELED","FITEM",
13226};
13227
13228static VALUE
13229append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13230{
13231 int i, sep = 0;
13232 unsigned int mask = 1;
13233 static const char none[] = "NONE";
13234
13235 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13236 if ((unsigned)state & mask) {
13237 if (sep) {
13238 rb_str_cat(buf, "|", 1);
13239 }
13240 sep = 1;
13241 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13242 }
13243 }
13244 if (!sep) {
13245 rb_str_cat(buf, none, sizeof(none)-1);
13246 }
13247 return buf;
13248}
13249
13250enum lex_state_e
13251rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13252 enum lex_state_e to, int line)
13253{
13254 VALUE mesg;
13255 mesg = rb_str_new_cstr("lex_state: ");
13256 append_lex_state_name(p, from, mesg);
13257 rb_str_cat_cstr(mesg, " -> ");
13258 append_lex_state_name(p, to, mesg);
13259 rb_str_catf(mesg, " at line %d\n", line);
13260 flush_debug_buffer(p, p->debug_output, mesg);
13261 return to;
13262}
13263
13264VALUE
13265rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13266{
13267 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13268}
13269
13270static void
13271append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13272{
13273 if (stack == 0) {
13274 rb_str_cat_cstr(mesg, "0");
13275 }
13276 else {
13277 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13278 for (; mask && !(stack & mask); mask >>= 1) continue;
13279 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13280 }
13281}
13282
13283void
13284rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13285 const char *name, int line)
13286{
13287 VALUE mesg = rb_sprintf("%s: ", name);
13288 append_bitstack_value(p, stack, mesg);
13289 rb_str_catf(mesg, " at line %d\n", line);
13290 flush_debug_buffer(p, p->debug_output, mesg);
13291}
13292
13293void
13294rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13295{
13296 va_list ap;
13297 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13298
13299 va_start(ap, fmt);
13300 rb_str_vcatf(mesg, fmt, ap);
13301 va_end(ap);
13302 yyerror0(RSTRING_PTR(mesg));
13303 RB_GC_GUARD(mesg);
13304
13305 mesg = rb_str_new(0, 0);
13306 append_lex_state_name(p, p->lex.state, mesg);
13307 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13308 rb_str_resize(mesg, 0);
13309 append_bitstack_value(p, p->cond_stack, mesg);
13310 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13311 rb_str_resize(mesg, 0);
13312 append_bitstack_value(p, p->cmdarg_stack, mesg);
13313 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13314 if (p->debug_output == rb_ractor_stdout())
13315 p->debug_output = rb_ractor_stderr();
13316 p->debug = TRUE;
13317}
13318
13319static YYLTYPE *
13320rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13321{
13322 yylloc->beg_pos.lineno = sourceline;
13323 yylloc->beg_pos.column = beg_pos;
13324 yylloc->end_pos.lineno = sourceline;
13325 yylloc->end_pos.column = end_pos;
13326 return yylloc;
13327}
13328
13329YYLTYPE *
13330rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13331{
13332 int sourceline = here->sourceline;
13333 int beg_pos = (int)here->offset - here->quote
13334 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13335 int end_pos = (int)here->offset + here->length + here->quote;
13336
13337 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13338}
13339
13340YYLTYPE *
13341rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13342{
13343 yylloc->beg_pos.lineno = p->delayed.beg_line;
13344 yylloc->beg_pos.column = p->delayed.beg_col;
13345 yylloc->end_pos.lineno = p->delayed.end_line;
13346 yylloc->end_pos.column = p->delayed.end_col;
13347
13348 return yylloc;
13349}
13350
13351YYLTYPE *
13352rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13353{
13354 int sourceline = p->ruby_sourceline;
13355 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13356 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13357 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13358}
13359
13360YYLTYPE *
13361rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13362{
13363 yylloc->end_pos = yylloc->beg_pos;
13364
13365 return yylloc;
13366}
13367
13368YYLTYPE *
13369rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13370{
13371 int sourceline = p->ruby_sourceline;
13372 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13373 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13374 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13375}
13376
13377YYLTYPE *
13378rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13379{
13380 int sourceline = p->ruby_sourceline;
13381 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13382 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13383 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13384}
13385#endif /* !RIPPER */
13386
13387static int
13388assignable0(struct parser_params *p, ID id, const char **err)
13389{
13390 if (!id) return -1;
13391 switch (id) {
13392 case keyword_self:
13393 *err = "Can't change the value of self";
13394 return -1;
13395 case keyword_nil:
13396 *err = "Can't assign to nil";
13397 return -1;
13398 case keyword_true:
13399 *err = "Can't assign to true";
13400 return -1;
13401 case keyword_false:
13402 *err = "Can't assign to false";
13403 return -1;
13404 case keyword__FILE__:
13405 *err = "Can't assign to __FILE__";
13406 return -1;
13407 case keyword__LINE__:
13408 *err = "Can't assign to __LINE__";
13409 return -1;
13410 case keyword__ENCODING__:
13411 *err = "Can't assign to __ENCODING__";
13412 return -1;
13413 }
13414 switch (id_type(id)) {
13415 case ID_LOCAL:
13416 if (dyna_in_block(p)) {
13417 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13418 compile_error(p, "Can't assign to numbered parameter _%d",
13419 NUMPARAM_ID_TO_IDX(id));
13420 return -1;
13421 }
13422 if (dvar_curr(p, id)) return NODE_DASGN;
13423 if (dvar_defined(p, id)) return NODE_DASGN;
13424 if (local_id(p, id)) return NODE_LASGN;
13425 dyna_var(p, id);
13426 return NODE_DASGN;
13427 }
13428 else {
13429 if (!local_id(p, id)) local_var(p, id);
13430 return NODE_LASGN;
13431 }
13432 break;
13433 case ID_GLOBAL: return NODE_GASGN;
13434 case ID_INSTANCE: return NODE_IASGN;
13435 case ID_CONST:
13436 if (!p->ctxt.in_def) return NODE_CDECL;
13437 *err = "dynamic constant assignment";
13438 return -1;
13439 case ID_CLASS: return NODE_CVASGN;
13440 default:
13441 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13442 }
13443 return -1;
13444}
13445
13446static NODE*
13447assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13448{
13449 const char *err = 0;
13450 int node_type = assignable0(p, id, &err);
13451 switch (node_type) {
13452 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13453 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13454 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13455 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13456 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13457 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13458 }
13459/* TODO: FIXME */
13460#ifndef RIPPER
13461 if (err) yyerror1(loc, err);
13462#else
13463 if (err) set_value(assign_error(p, err, p->s_lvalue));
13464#endif
13465 return NEW_ERROR(loc);
13466}
13467
13468static int
13469is_private_local_id(struct parser_params *p, ID name)
13470{
13471 VALUE s;
13472 if (name == idUScore) return 1;
13473 if (!is_local_id(name)) return 0;
13474 s = rb_id2str(name);
13475 if (!s) return 0;
13476 return RSTRING_PTR(s)[0] == '_';
13477}
13478
13479static int
13480shadowing_lvar_0(struct parser_params *p, ID name)
13481{
13482 if (dyna_in_block(p)) {
13483 if (dvar_curr(p, name)) {
13484 if (is_private_local_id(p, name)) return 1;
13485 yyerror0("duplicated argument name");
13486 }
13487 else if (dvar_defined(p, name) || local_id(p, name)) {
13488 vtable_add(p->lvtbl->vars, name);
13489 if (p->lvtbl->used) {
13490 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13491 }
13492 return 0;
13493 }
13494 }
13495 else {
13496 if (local_id(p, name)) {
13497 if (is_private_local_id(p, name)) return 1;
13498 yyerror0("duplicated argument name");
13499 }
13500 }
13501 return 1;
13502}
13503
13504static ID
13505shadowing_lvar(struct parser_params *p, ID name)
13506{
13507 shadowing_lvar_0(p, name);
13508 return name;
13509}
13510
13511static void
13512new_bv(struct parser_params *p, ID name)
13513{
13514 if (!name) return;
13515 if (!is_local_id(name)) {
13516 compile_error(p, "invalid local variable - %"PRIsVALUE,
13517 rb_id2str(name));
13518 return;
13519 }
13520 if (!shadowing_lvar_0(p, name)) return;
13521 dyna_var(p, name);
13522 ID *vidp = 0;
13523 if (dvar_defined_ref(p, name, &vidp)) {
13524 if (vidp) *vidp |= LVAR_USED;
13525 }
13526}
13527
13528static void
13529aryset_check(struct parser_params *p, NODE *args)
13530{
13531 NODE *block = 0, *kwds = 0;
13532 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13533 block = RNODE_BLOCK_PASS(args)->nd_body;
13534 args = RNODE_BLOCK_PASS(args)->nd_head;
13535 }
13536 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13537 args = RNODE_ARGSCAT(args)->nd_body;
13538 }
13539 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13540 kwds = RNODE_ARGSPUSH(args)->nd_body;
13541 }
13542 else {
13543 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13544 next = RNODE_LIST(next)->nd_next) {
13545 kwds = RNODE_LIST(next)->nd_head;
13546 }
13547 }
13548 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13549 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13550 }
13551 if (block) {
13552 yyerror1(&block->nd_loc, "block arg given in index assignment");
13553 }
13554}
13555
13556static NODE *
13557aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13558{
13559 aryset_check(p, idx);
13560 return NEW_ATTRASGN(recv, tASET, idx, loc);
13561}
13562
13563static void
13564block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13565{
13566 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13567 compile_error(p, "both block arg and actual block given");
13568 }
13569}
13570
13571static NODE *
13572attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13573{
13574 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13575 return NEW_ATTRASGN(recv, id, 0, loc);
13576}
13577
13578static VALUE
13579rb_backref_error(struct parser_params *p, NODE *node)
13580{
13581#ifndef RIPPER
13582# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13583#else
13584# define ERR(...) rb_sprintf(__VA_ARGS__)
13585#endif
13586 switch (nd_type(node)) {
13587 case NODE_NTH_REF:
13588 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13589 case NODE_BACK_REF:
13590 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13591 }
13592#undef ERR
13593 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13594}
13595
13596static NODE *
13597arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13598{
13599 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13600 switch (nd_type(node1)) {
13601 case NODE_LIST:
13602 return list_append(p, node1, node2);
13603 case NODE_BLOCK_PASS:
13604 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13605 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13606 return node1;
13607 case NODE_ARGSPUSH:
13608 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13609 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13610 nd_set_type(node1, NODE_ARGSCAT);
13611 return node1;
13612 case NODE_ARGSCAT:
13613 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13614 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13615 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13616 return node1;
13617 }
13618 return NEW_ARGSPUSH(node1, node2, loc);
13619}
13620
13621static NODE *
13622arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13623{
13624 if (!node2) return node1;
13625 switch (nd_type(node1)) {
13626 case NODE_BLOCK_PASS:
13627 if (RNODE_BLOCK_PASS(node1)->nd_head)
13628 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13629 else
13630 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13631 return node1;
13632 case NODE_ARGSPUSH:
13633 if (!nd_type_p(node2, NODE_LIST)) break;
13634 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13635 nd_set_type(node1, NODE_ARGSCAT);
13636 return node1;
13637 case NODE_ARGSCAT:
13638 if (!nd_type_p(node2, NODE_LIST) ||
13639 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13640 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13641 return node1;
13642 }
13643 return NEW_ARGSCAT(node1, node2, loc);
13644}
13645
13646static NODE *
13647last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13648{
13649 NODE *n1;
13650 if ((n1 = splat_array(args)) != 0) {
13651 return list_append(p, n1, last_arg);
13652 }
13653 return arg_append(p, args, last_arg, loc);
13654}
13655
13656static NODE *
13657rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13658{
13659 NODE *n1;
13660 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13661 return list_concat(n1, rest_arg);
13662 }
13663 return arg_concat(p, args, rest_arg, loc);
13664}
13665
13666static NODE *
13667splat_array(NODE* node)
13668{
13669 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13670 if (nd_type_p(node, NODE_LIST)) return node;
13671 return 0;
13672}
13673
13674static void
13675mark_lvar_used(struct parser_params *p, NODE *rhs)
13676{
13677 ID *vidp = NULL;
13678 if (!rhs) return;
13679 switch (nd_type(rhs)) {
13680 case NODE_LASGN:
13681 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13682 if (vidp) *vidp |= LVAR_USED;
13683 }
13684 break;
13685 case NODE_DASGN:
13686 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13687 if (vidp) *vidp |= LVAR_USED;
13688 }
13689 break;
13690#if 0
13691 case NODE_MASGN:
13692 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13693 mark_lvar_used(p, rhs->nd_head);
13694 }
13695 break;
13696#endif
13697 }
13698}
13699
13700static int is_static_content(NODE *node);
13701
13702static NODE *
13703node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13704{
13705 if (!lhs) return 0;
13706
13707 switch (nd_type(lhs)) {
13708 case NODE_CDECL:
13709 case NODE_GASGN:
13710 case NODE_IASGN:
13711 case NODE_LASGN:
13712 case NODE_DASGN:
13713 case NODE_MASGN:
13714 case NODE_CVASGN:
13715 set_nd_value(p, lhs, rhs);
13716 nd_set_loc(lhs, loc);
13717 break;
13718
13719 case NODE_ATTRASGN:
13720 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13721 nd_set_loc(lhs, loc);
13722 break;
13723
13724 default:
13725 /* should not happen */
13726 break;
13727 }
13728
13729 return lhs;
13730}
13731
13732static NODE *
13733value_expr_check(struct parser_params *p, NODE *node)
13734{
13735 NODE *void_node = 0, *vn;
13736
13737 if (!node) {
13738 rb_warning0("empty expression");
13739 }
13740 while (node) {
13741 switch (nd_type(node)) {
13742 case NODE_ENSURE:
13743 vn = RNODE_ENSURE(node)->nd_head;
13744 node = RNODE_ENSURE(node)->nd_ensr;
13745 /* nd_ensr should not be NULL, check it out next */
13746 if (vn && (vn = value_expr_check(p, vn))) {
13747 goto found;
13748 }
13749 break;
13750
13751 case NODE_RESCUE:
13752 /* void only if all children are void */
13753 vn = RNODE_RESCUE(node)->nd_head;
13754 if (!vn || !(vn = value_expr_check(p, vn))) return NULL;
13755 if (!void_node) void_node = vn;
13756 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13757 if (!nd_type_p(r, NODE_RESBODY)) {
13758 compile_error(p, "unexpected node");
13759 return NULL;
13760 }
13761 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13762 void_node = 0;
13763 break;
13764 }
13765 if (!void_node) void_node = vn;
13766 }
13767 node = RNODE_RESCUE(node)->nd_else;
13768 if (!node) return void_node;
13769 break;
13770
13771 case NODE_RETURN:
13772 case NODE_BREAK:
13773 case NODE_NEXT:
13774 case NODE_REDO:
13775 case NODE_RETRY:
13776 goto found;
13777
13778 case NODE_CASE3:
13779 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13780 compile_error(p, "unexpected node");
13781 return NULL;
13782 }
13783 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13784 return NULL;
13785 }
13786 /* single line pattern matching with "=>" operator */
13787 goto found;
13788
13789 case NODE_BLOCK:
13790 while (RNODE_BLOCK(node)->nd_next) {
13791 node = RNODE_BLOCK(node)->nd_next;
13792 }
13793 node = RNODE_BLOCK(node)->nd_head;
13794 break;
13795
13796 case NODE_BEGIN:
13797 node = RNODE_BEGIN(node)->nd_body;
13798 break;
13799
13800 case NODE_IF:
13801 case NODE_UNLESS:
13802 if (!RNODE_IF(node)->nd_body) {
13803 return NULL;
13804 }
13805 else if (!RNODE_IF(node)->nd_else) {
13806 return NULL;
13807 }
13808 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13809 if (!vn) return NULL;
13810 if (!void_node) void_node = vn;
13811 node = RNODE_IF(node)->nd_else;
13812 break;
13813
13814 case NODE_AND:
13815 case NODE_OR:
13816 node = RNODE_AND(node)->nd_1st;
13817 break;
13818
13819 case NODE_LASGN:
13820 case NODE_DASGN:
13821 case NODE_MASGN:
13822 mark_lvar_used(p, node);
13823 return NULL;
13824
13825 default:
13826 return NULL;
13827 }
13828 }
13829
13830 return NULL;
13831
13832 found:
13833 /* return the first found node */
13834 return void_node ? void_node : node;
13835}
13836
13837static int
13838value_expr_gen(struct parser_params *p, NODE *node)
13839{
13840 NODE *void_node = value_expr_check(p, node);
13841 if (void_node) {
13842 yyerror1(&void_node->nd_loc, "void value expression");
13843 /* or "control never reach"? */
13844 return FALSE;
13845 }
13846 return TRUE;
13847}
13848
13849static void
13850void_expr(struct parser_params *p, NODE *node)
13851{
13852 const char *useless = 0;
13853
13854 if (!RTEST(ruby_verbose)) return;
13855
13856 if (!node || !(node = nd_once_body(node))) return;
13857 switch (nd_type(node)) {
13858 case NODE_OPCALL:
13859 switch (RNODE_OPCALL(node)->nd_mid) {
13860 case '+':
13861 case '-':
13862 case '*':
13863 case '/':
13864 case '%':
13865 case tPOW:
13866 case tUPLUS:
13867 case tUMINUS:
13868 case '|':
13869 case '^':
13870 case '&':
13871 case tCMP:
13872 case '>':
13873 case tGEQ:
13874 case '<':
13875 case tLEQ:
13876 case tEQ:
13877 case tNEQ:
13878 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13879 break;
13880 }
13881 break;
13882
13883 case NODE_LVAR:
13884 case NODE_DVAR:
13885 case NODE_GVAR:
13886 case NODE_IVAR:
13887 case NODE_CVAR:
13888 case NODE_NTH_REF:
13889 case NODE_BACK_REF:
13890 useless = "a variable";
13891 break;
13892 case NODE_CONST:
13893 useless = "a constant";
13894 break;
13895 case NODE_SYM:
13896 case NODE_LINE:
13897 case NODE_FILE:
13898 case NODE_ENCODING:
13899 case NODE_INTEGER:
13900 case NODE_FLOAT:
13901 case NODE_RATIONAL:
13902 case NODE_IMAGINARY:
13903 case NODE_STR:
13904 case NODE_DSTR:
13905 case NODE_REGX:
13906 case NODE_DREGX:
13907 useless = "a literal";
13908 break;
13909 case NODE_COLON2:
13910 case NODE_COLON3:
13911 useless = "::";
13912 break;
13913 case NODE_DOT2:
13914 useless = "..";
13915 break;
13916 case NODE_DOT3:
13917 useless = "...";
13918 break;
13919 case NODE_SELF:
13920 useless = "self";
13921 break;
13922 case NODE_NIL:
13923 useless = "nil";
13924 break;
13925 case NODE_TRUE:
13926 useless = "true";
13927 break;
13928 case NODE_FALSE:
13929 useless = "false";
13930 break;
13931 case NODE_DEFINED:
13932 useless = "defined?";
13933 break;
13934 }
13935
13936 if (useless) {
13937 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
13938 }
13939}
13940
13941/* warns useless use of block and returns the last statement node */
13942static NODE *
13943void_stmts(struct parser_params *p, NODE *node)
13944{
13945 NODE *const n = node;
13946 if (!RTEST(ruby_verbose)) return n;
13947 if (!node) return n;
13948 if (!nd_type_p(node, NODE_BLOCK)) return n;
13949
13950 while (RNODE_BLOCK(node)->nd_next) {
13951 void_expr(p, RNODE_BLOCK(node)->nd_head);
13952 node = RNODE_BLOCK(node)->nd_next;
13953 }
13954 return RNODE_BLOCK(node)->nd_head;
13955}
13956
13957static NODE *
13958remove_begin(NODE *node)
13959{
13960 NODE **n = &node, *n1 = node;
13961 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
13962 *n = n1 = RNODE_BEGIN(n1)->nd_body;
13963 }
13964 return node;
13965}
13966
13967static void
13968reduce_nodes(struct parser_params *p, NODE **body)
13969{
13970 NODE *node = *body;
13971
13972 if (!node) {
13973 *body = NEW_NIL(&NULL_LOC);
13974 return;
13975 }
13976#define subnodes(type, n1, n2) \
13977 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
13978 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
13979 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
13980
13981 while (node) {
13982 int newline = (int)nd_fl_newline(node);
13983 switch (nd_type(node)) {
13984 end:
13985 case NODE_NIL:
13986 *body = 0;
13987 return;
13988 case NODE_BEGIN:
13989 *body = node = RNODE_BEGIN(node)->nd_body;
13990 if (newline && node) nd_set_fl_newline(node);
13991 continue;
13992 case NODE_BLOCK:
13993 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
13994 break;
13995 case NODE_IF:
13996 case NODE_UNLESS:
13997 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
13998 return;
13999 case NODE_CASE:
14000 body = &RNODE_CASE(node)->nd_body;
14001 break;
14002 case NODE_WHEN:
14003 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14004 break;
14005 case NODE_ENSURE:
14006 body = &RNODE_ENSURE(node)->nd_head;
14007 break;
14008 case NODE_RESCUE:
14009 newline = 0; // RESBODY should not be a NEWLINE
14010 if (RNODE_RESCUE(node)->nd_else) {
14011 body = &RNODE_RESCUE(node)->nd_resq;
14012 break;
14013 }
14014 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14015 break;
14016 default:
14017 return;
14018 }
14019 node = *body;
14020 if (newline && node) nd_set_fl_newline(node);
14021 }
14022
14023#undef subnodes
14024}
14025
14026static int
14027is_static_content(NODE *node)
14028{
14029 if (!node) return 1;
14030 switch (nd_type(node)) {
14031 case NODE_HASH:
14032 if (!(node = RNODE_HASH(node)->nd_head)) break;
14033 case NODE_LIST:
14034 do {
14035 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14036 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14037 case NODE_SYM:
14038 case NODE_REGX:
14039 case NODE_LINE:
14040 case NODE_FILE:
14041 case NODE_ENCODING:
14042 case NODE_INTEGER:
14043 case NODE_FLOAT:
14044 case NODE_RATIONAL:
14045 case NODE_IMAGINARY:
14046 case NODE_STR:
14047 case NODE_NIL:
14048 case NODE_TRUE:
14049 case NODE_FALSE:
14050 case NODE_ZLIST:
14051 break;
14052 default:
14053 return 0;
14054 }
14055 return 1;
14056}
14057
14058static int
14059assign_in_cond(struct parser_params *p, NODE *node)
14060{
14061 switch (nd_type(node)) {
14062 case NODE_MASGN:
14063 case NODE_LASGN:
14064 case NODE_DASGN:
14065 case NODE_GASGN:
14066 case NODE_IASGN:
14067 case NODE_CVASGN:
14068 case NODE_CDECL:
14069 break;
14070
14071 default:
14072 return 0;
14073 }
14074
14075 if (!get_nd_value(p, node)) return 1;
14076 if (is_static_content(get_nd_value(p, node))) {
14077 /* reports always */
14078 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14079 }
14080 return 1;
14081}
14082
14083enum cond_type {
14084 COND_IN_OP,
14085 COND_IN_COND,
14086 COND_IN_FF
14087};
14088
14089#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14090 switch (t) { \
14091 case COND_IN_OP: break; \
14092 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14093 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14094 } \
14095} while (0)
14096
14097static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14098
14099static NODE*
14100range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14101{
14102 enum node_type type;
14103
14104 if (node == 0) return 0;
14105
14106 type = nd_type(node);
14107 value_expr(node);
14108 if (type == NODE_INTEGER) {
14109 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14110 ID lineno = rb_intern("$.");
14111 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14112 }
14113 return cond0(p, node, COND_IN_FF, loc, true);
14114}
14115
14116static NODE*
14117cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14118{
14119 if (node == 0) return 0;
14120 if (!(node = nd_once_body(node))) return 0;
14121 assign_in_cond(p, node);
14122
14123 switch (nd_type(node)) {
14124 case NODE_BEGIN:
14125 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14126 break;
14127
14128 case NODE_DSTR:
14129 case NODE_EVSTR:
14130 case NODE_STR:
14131 case NODE_FILE:
14132 SWITCH_BY_COND_TYPE(type, warn, "string ");
14133 break;
14134
14135 case NODE_REGX:
14136 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14137 nd_set_type(node, NODE_MATCH);
14138 break;
14139
14140 case NODE_DREGX:
14141 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14142
14143 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14144
14145 case NODE_BLOCK:
14146 {
14147 NODE *end = RNODE_BLOCK(node)->nd_end;
14148 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14149 if (top) top = node == end;
14150 *expr = cond0(p, *expr, type, loc, top);
14151 }
14152 break;
14153
14154 case NODE_AND:
14155 case NODE_OR:
14156 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14157 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14158 break;
14159
14160 case NODE_DOT2:
14161 case NODE_DOT3:
14162 if (!top) break;
14163 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14164 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14165 switch (nd_type(node)) {
14166 case NODE_DOT2:
14167 nd_set_type(node,NODE_FLIP2);
14168 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14169 (void)flip2;
14170 break;
14171 case NODE_DOT3:
14172 nd_set_type(node, NODE_FLIP3);
14173 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14174 (void)flip3;
14175 break;
14176 }
14177 break;
14178
14179 case NODE_SYM:
14180 case NODE_DSYM:
14181 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14182 break;
14183
14184 case NODE_LINE:
14185 case NODE_ENCODING:
14186 case NODE_INTEGER:
14187 case NODE_FLOAT:
14188 case NODE_RATIONAL:
14189 case NODE_IMAGINARY:
14190 SWITCH_BY_COND_TYPE(type, warning, "");
14191 break;
14192
14193 default:
14194 break;
14195 }
14196 return node;
14197}
14198
14199static NODE*
14200cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14201{
14202 if (node == 0) return 0;
14203 return cond0(p, node, COND_IN_COND, loc, true);
14204}
14205
14206static NODE*
14207method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14208{
14209 if (node == 0) return 0;
14210 return cond0(p, node, COND_IN_OP, loc, true);
14211}
14212
14213static NODE*
14214new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14215{
14216 YYLTYPE loc = {*pos, *pos};
14217 return NEW_NIL(&loc);
14218}
14219
14220static NODE*
14221new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc, const YYLTYPE* if_keyword_loc, const YYLTYPE* then_keyword_loc, const YYLTYPE* end_keyword_loc)
14222{
14223 if (!cc) return right;
14224 cc = cond0(p, cc, COND_IN_COND, loc, true);
14225 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14226}
14227
14228static NODE*
14229new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *end_keyword_loc)
14230{
14231 if (!cc) return right;
14232 cc = cond0(p, cc, COND_IN_COND, loc, true);
14233 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14234}
14235
14236#define NEW_AND_OR(type, f, s, loc, op_loc) (type == NODE_AND ? NEW_AND(f,s,loc,op_loc) : NEW_OR(f,s,loc,op_loc))
14237
14238static NODE*
14239logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14240 const YYLTYPE *op_loc, const YYLTYPE *loc)
14241{
14242 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14243 NODE *op;
14244 value_expr(left);
14245 if (left && nd_type_p(left, type)) {
14246 NODE *node = left, *second;
14247 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14248 node = second;
14249 }
14250 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14251 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14252 left->nd_loc.end_pos = loc->end_pos;
14253 return left;
14254 }
14255 op = NEW_AND_OR(type, left, right, loc, op_loc);
14256 nd_set_line(op, op_loc->beg_pos.lineno);
14257 return op;
14258}
14259
14260#undef NEW_AND_OR
14261
14262static void
14263no_blockarg(struct parser_params *p, NODE *node)
14264{
14265 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14266 compile_error(p, "block argument should not be given");
14267 }
14268}
14269
14270static NODE *
14271ret_args(struct parser_params *p, NODE *node)
14272{
14273 if (node) {
14274 no_blockarg(p, node);
14275 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14276 node = RNODE_LIST(node)->nd_head;
14277 }
14278 }
14279 return node;
14280}
14281
14282static NODE*
14283negate_lit(struct parser_params *p, NODE* node)
14284{
14285 switch (nd_type(node)) {
14286 case NODE_INTEGER:
14287 RNODE_INTEGER(node)->minus = TRUE;
14288 break;
14289 case NODE_FLOAT:
14290 RNODE_FLOAT(node)->minus = TRUE;
14291 break;
14292 case NODE_RATIONAL:
14293 RNODE_RATIONAL(node)->minus = TRUE;
14294 break;
14295 case NODE_IMAGINARY:
14296 RNODE_IMAGINARY(node)->minus = TRUE;
14297 break;
14298 }
14299 return node;
14300}
14301
14302static NODE *
14303arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14304{
14305 if (node2) {
14306 if (!node1) return (NODE *)node2;
14307 node2->nd_head = node1;
14308 nd_set_first_lineno(node2, nd_first_lineno(node1));
14309 nd_set_first_column(node2, nd_first_column(node1));
14310 return (NODE *)node2;
14311 }
14312 return node1;
14313}
14314
14315static bool
14316args_info_empty_p(struct rb_args_info *args)
14317{
14318 if (args->pre_args_num) return false;
14319 if (args->post_args_num) return false;
14320 if (args->rest_arg) return false;
14321 if (args->opt_args) return false;
14322 if (args->block_arg) return false;
14323 if (args->kw_args) return false;
14324 if (args->kw_rest_arg) return false;
14325 return true;
14326}
14327
14328static rb_node_args_t *
14329new_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)
14330{
14331 struct rb_args_info *args = &tail->nd_ainfo;
14332
14333 if (args->forwarding) {
14334 if (rest_arg) {
14335 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14336 return tail;
14337 }
14338 rest_arg = idFWD_REST;
14339 }
14340
14341 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14342 args->pre_init = pre_args ? pre_args->nd_next : 0;
14343
14344 args->post_args_num = post_args ? post_args->nd_plen : 0;
14345 args->post_init = post_args ? post_args->nd_next : 0;
14346 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14347
14348 args->rest_arg = rest_arg;
14349
14350 args->opt_args = opt_args;
14351
14352#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14353 args->ruby2_keywords = args->forwarding;
14354#else
14355 args->ruby2_keywords = 0;
14356#endif
14357
14358 nd_set_loc(RNODE(tail), loc);
14359
14360 return tail;
14361}
14362
14363static rb_node_args_t *
14364new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14365{
14366 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14367 struct rb_args_info *args = &node->nd_ainfo;
14368 if (p->error_p) return node;
14369
14370 args->block_arg = block;
14371 args->kw_args = kw_args;
14372
14373 if (kw_args) {
14374 /*
14375 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14376 * variable order: k1, kr1, k2, &b, internal_id, krest
14377 * #=> <reorder>
14378 * variable order: kr1, k1, k2, internal_id, krest, &b
14379 */
14380 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14381 struct vtable *vtargs = p->lvtbl->args;
14382 rb_node_kw_arg_t *kwn = kw_args;
14383
14384 if (block) block = vtargs->tbl[vtargs->pos-1];
14385 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14386 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14387 while (kwn) {
14388 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14389 --kw_vars;
14390 --required_kw_vars;
14391 kwn = kwn->nd_next;
14392 }
14393
14394 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14395 ID vid = get_nd_vid(p, kwn->nd_body);
14396 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14397 *required_kw_vars++ = vid;
14398 }
14399 else {
14400 *kw_vars++ = vid;
14401 }
14402 }
14403
14404 arg_var(p, kw_bits);
14405 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14406 if (block) arg_var(p, block);
14407
14408 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14409 }
14410 else if (kw_rest_arg == idNil) {
14411 args->no_kwarg = 1;
14412 }
14413 else if (kw_rest_arg) {
14414 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14415 }
14416
14417 return node;
14418}
14419
14420static rb_node_args_t *
14421args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14422{
14423 if (max_numparam > NO_PARAM || it_id) {
14424 if (!args) {
14425 YYLTYPE loc = RUBY_INIT_YYLLOC();
14426 args = new_args_tail(p, 0, 0, 0, 0);
14427 nd_set_loc(RNODE(args), &loc);
14428 }
14429 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14430 }
14431 return args;
14432}
14433
14434static NODE*
14435new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14436{
14437 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14438
14439 if (pre_arg) {
14440 NODE *pre_args = NEW_LIST(pre_arg, loc);
14441 if (RNODE_ARYPTN(aryptn)->pre_args) {
14442 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14443 }
14444 else {
14445 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14446 }
14447 }
14448 return aryptn;
14449}
14450
14451static NODE*
14452new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14453{
14454 if (has_rest) {
14455 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14456 }
14457 else {
14458 rest_arg = NULL;
14459 }
14460 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14461
14462 return node;
14463}
14464
14465static NODE*
14466new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14467{
14468 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14469
14470 return fndptn;
14471}
14472
14473static NODE*
14474new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14475{
14476 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14477 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14478 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14479
14480 return node;
14481}
14482
14483static NODE*
14484new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14485{
14486 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14487 return hshptn;
14488}
14489
14490static NODE*
14491new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14492{
14493 NODE *node, *kw_rest_arg_node;
14494
14495 if (kw_rest_arg == idNil) {
14496 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14497 }
14498 else if (kw_rest_arg) {
14499 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14500 }
14501 else {
14502 kw_rest_arg_node = NULL;
14503 }
14504
14505 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14506
14507 return node;
14508}
14509
14510static NODE*
14511dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14512{
14513 if (!node) {
14514 return NEW_SYM(STR_NEW0(), loc);
14515 }
14516
14517 switch (nd_type(node)) {
14518 case NODE_DSTR:
14519 nd_set_type(node, NODE_DSYM);
14520 nd_set_loc(node, loc);
14521 break;
14522 case NODE_STR:
14523 node = str_to_sym_node(p, node, loc);
14524 break;
14525 default:
14526 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14527 break;
14528 }
14529 return node;
14530}
14531
14532static int
14533nd_type_st_key_enable_p(NODE *node)
14534{
14535 switch (nd_type(node)) {
14536 case NODE_INTEGER:
14537 case NODE_FLOAT:
14538 case NODE_RATIONAL:
14539 case NODE_IMAGINARY:
14540 case NODE_STR:
14541 case NODE_SYM:
14542 case NODE_REGX:
14543 case NODE_LINE:
14544 case NODE_FILE:
14545 case NODE_ENCODING:
14546 return true;
14547 default:
14548 return false;
14549 }
14550}
14551
14552static VALUE
14553nd_value(struct parser_params *p, NODE *node)
14554{
14555 switch (nd_type(node)) {
14556 case NODE_STR:
14557 return rb_node_str_string_val(node);
14558 case NODE_INTEGER:
14559 return rb_node_integer_literal_val(node);
14560 case NODE_FLOAT:
14561 return rb_node_float_literal_val(node);
14562 case NODE_RATIONAL:
14563 return rb_node_rational_literal_val(node);
14564 case NODE_IMAGINARY:
14565 return rb_node_imaginary_literal_val(node);
14566 case NODE_SYM:
14567 return rb_node_sym_string_val(node);
14568 case NODE_REGX:
14569 return rb_node_regx_string_val(node);
14570 case NODE_LINE:
14571 return rb_node_line_lineno_val(node);
14572 case NODE_ENCODING:
14573 return rb_node_encoding_val(node);
14574 case NODE_FILE:
14575 return rb_node_file_path_val(node);
14576 default:
14577 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14578 UNREACHABLE_RETURN(0);
14579 }
14580}
14581
14582static void
14583warn_duplicate_keys(struct parser_params *p, NODE *hash)
14584{
14585 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14586 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
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;
14592 st_data_t data;
14593
14594 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14595 if (!head) {
14596 head = value;
14597 }
14598
14599 if (nd_type_st_key_enable_p(head)) {
14600 key = (st_data_t)head;
14601
14602 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14603 rb_warn2L(nd_line((NODE *)data),
14604 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14605 nd_value(p, head), WARN_I(nd_line(head)));
14606 }
14607 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14608 }
14609 hash = next;
14610 }
14611 st_free_table(p->warn_duplicate_keys_table);
14612 p->warn_duplicate_keys_table = NULL;
14613}
14614
14615static NODE *
14616new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14617{
14618 if (hash) warn_duplicate_keys(p, hash);
14619 return NEW_HASH(hash, loc);
14620}
14621
14622static void
14623error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14624{
14625 if (is_private_local_id(p, id)) {
14626 return;
14627 }
14628 if (st_is_member(p->pvtbl, id)) {
14629 yyerror1(loc, "duplicated variable name");
14630 }
14631 else {
14632 st_insert(p->pvtbl, (st_data_t)id, 0);
14633 }
14634}
14635
14636static void
14637error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14638{
14639 if (!p->pktbl) {
14640 p->pktbl = st_init_numtable();
14641 }
14642 else if (st_is_member(p->pktbl, key)) {
14643 yyerror1(loc, "duplicated key name");
14644 return;
14645 }
14646 st_insert(p->pktbl, (st_data_t)key, 0);
14647}
14648
14649static NODE *
14650new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14651{
14652 return NEW_HASH(hash, loc);
14653}
14654
14655static NODE *
14656new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14657{
14658 NODE *asgn;
14659
14660 if (lhs) {
14661 ID vid = get_nd_vid(p, lhs);
14662 YYLTYPE lhs_loc = lhs->nd_loc;
14663 if (op == tOROP) {
14664 set_nd_value(p, lhs, rhs);
14665 nd_set_loc(lhs, loc);
14666 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14667 }
14668 else if (op == tANDOP) {
14669 set_nd_value(p, lhs, rhs);
14670 nd_set_loc(lhs, loc);
14671 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14672 }
14673 else {
14674 asgn = lhs;
14675 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14676 set_nd_value(p, asgn, rhs);
14677 nd_set_loc(asgn, loc);
14678 }
14679 }
14680 else {
14681 asgn = NEW_ERROR(loc);
14682 }
14683 return asgn;
14684}
14685
14686static NODE *
14687new_ary_op_assign(struct parser_params *p, NODE *ary,
14688 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14689 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14690{
14691 NODE *asgn;
14692
14693 aryset_check(p, args);
14694 args = make_list(args, args_loc);
14695 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14696 fixpos(asgn, ary);
14697 return asgn;
14698}
14699
14700static NODE *
14701new_attr_op_assign(struct parser_params *p, NODE *lhs,
14702 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14703 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14704{
14705 NODE *asgn;
14706
14707 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14708 fixpos(asgn, lhs);
14709 return asgn;
14710}
14711
14712static NODE *
14713new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14714{
14715 NODE *asgn;
14716
14717 if (lhs) {
14718 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14719 }
14720 else {
14721 asgn = NEW_ERROR(loc);
14722 }
14723 fixpos(asgn, lhs);
14724 return asgn;
14725}
14726
14727static NODE *
14728const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14729{
14730 if (p->ctxt.in_def) {
14731#ifndef RIPPER
14732 yyerror1(loc, "dynamic constant assignment");
14733#else
14734 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14735#endif
14736 }
14737 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14738}
14739
14740#ifdef RIPPER
14741static VALUE
14742assign_error(struct parser_params *p, const char *mesg, VALUE a)
14743{
14744 a = dispatch2(assign_error, ERR_MESG(), a);
14745 ripper_error(p);
14746 return a;
14747}
14748#endif
14749
14750static NODE *
14751new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14752{
14753 NODE *result = head;
14754 if (rescue) {
14755 NODE *tmp = rescue_else ? rescue_else : rescue;
14756 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14757
14758 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14759 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14760 }
14761 if (ensure) {
14762 result = NEW_ENSURE(result, ensure, loc);
14763 }
14764 fixpos(result, head);
14765 return result;
14766}
14767
14768static void
14769warn_unused_var(struct parser_params *p, struct local_vars *local)
14770{
14771 int cnt;
14772
14773 if (!local->used) return;
14774 cnt = local->used->pos;
14775 if (cnt != local->vars->pos) {
14776 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14777 }
14778#ifndef RIPPER
14779 ID *v = local->vars->tbl;
14780 ID *u = local->used->tbl;
14781 for (int i = 0; i < cnt; ++i) {
14782 if (!v[i] || (u[i] & LVAR_USED)) continue;
14783 if (is_private_local_id(p, v[i])) continue;
14784 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14785 }
14786#endif
14787}
14788
14789static void
14790local_push(struct parser_params *p, int toplevel_scope)
14791{
14792 struct local_vars *local;
14793 int inherits_dvars = toplevel_scope && compile_for_eval;
14794 int warn_unused_vars = RTEST(ruby_verbose);
14795
14796 local = ALLOC(struct local_vars);
14797 local->prev = p->lvtbl;
14798 local->args = vtable_alloc(0);
14799 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14800#ifndef RIPPER
14801 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14802 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14803#endif
14804 local->numparam.outer = 0;
14805 local->numparam.inner = 0;
14806 local->numparam.current = 0;
14807 local->it = 0;
14808 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14809
14810# if WARN_PAST_SCOPE
14811 local->past = 0;
14812# endif
14813 CMDARG_PUSH(0);
14814 COND_PUSH(0);
14815 p->lvtbl = local;
14816}
14817
14818static void
14819vtable_chain_free(struct parser_params *p, struct vtable *table)
14820{
14821 while (!DVARS_TERMINAL_P(table)) {
14822 struct vtable *cur_table = table;
14823 table = cur_table->prev;
14824 vtable_free(cur_table);
14825 }
14826}
14827
14828static void
14829local_free(struct parser_params *p, struct local_vars *local)
14830{
14831 vtable_chain_free(p, local->used);
14832
14833# if WARN_PAST_SCOPE
14834 vtable_chain_free(p, local->past);
14835# endif
14836
14837 vtable_chain_free(p, local->args);
14838 vtable_chain_free(p, local->vars);
14839
14840 ruby_sized_xfree(local, sizeof(struct local_vars));
14841}
14842
14843static void
14844local_pop(struct parser_params *p)
14845{
14846 struct local_vars *local = p->lvtbl->prev;
14847 if (p->lvtbl->used) {
14848 warn_unused_var(p, p->lvtbl);
14849 }
14850
14851 local_free(p, p->lvtbl);
14852 p->lvtbl = local;
14853
14854 CMDARG_POP();
14855 COND_POP();
14856}
14857
14858static rb_ast_id_table_t *
14859local_tbl(struct parser_params *p)
14860{
14861 int cnt_args = vtable_size(p->lvtbl->args);
14862 int cnt_vars = vtable_size(p->lvtbl->vars);
14863 int cnt = cnt_args + cnt_vars;
14864 int i, j;
14865 rb_ast_id_table_t *tbl;
14866
14867 if (cnt <= 0) return 0;
14868 tbl = rb_ast_new_local_table(p->ast, cnt);
14869 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14870 /* remove IDs duplicated to warn shadowing */
14871 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14872 ID id = p->lvtbl->vars->tbl[i];
14873 if (!vtable_included(p->lvtbl->args, id)) {
14874 tbl->ids[j++] = id;
14875 }
14876 }
14877 if (j < cnt) {
14878 tbl = rb_ast_resize_latest_local_table(p->ast, j);
14879 }
14880
14881 return tbl;
14882}
14883
14884static void
14885numparam_name(struct parser_params *p, ID id)
14886{
14887 if (!NUMPARAM_ID_P(id)) return;
14888 compile_error(p, "_%d is reserved for numbered parameter",
14889 NUMPARAM_ID_TO_IDX(id));
14890}
14891
14892static void
14893arg_var(struct parser_params *p, ID id)
14894{
14895 numparam_name(p, id);
14896 vtable_add(p->lvtbl->args, id);
14897}
14898
14899static void
14900local_var(struct parser_params *p, ID id)
14901{
14902 numparam_name(p, id);
14903 vtable_add(p->lvtbl->vars, id);
14904 if (p->lvtbl->used) {
14905 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
14906 }
14907}
14908
14909#ifndef RIPPER
14910int
14911rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
14912{
14913 return rb_local_defined(id, iseq);
14914}
14915#endif
14916
14917static int
14918local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
14919{
14920 struct vtable *vars, *args, *used;
14921
14922 vars = p->lvtbl->vars;
14923 args = p->lvtbl->args;
14924 used = p->lvtbl->used;
14925
14926 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
14927 vars = vars->prev;
14928 args = args->prev;
14929 if (used) used = used->prev;
14930 }
14931
14932 if (vars && vars->prev == DVARS_INHERIT) {
14933 return rb_parser_local_defined(p, id, p->parent_iseq);
14934 }
14935 else if (vtable_included(args, id)) {
14936 return 1;
14937 }
14938 else {
14939 int i = vtable_included(vars, id);
14940 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
14941 return i != 0;
14942 }
14943}
14944
14945static int
14946local_id(struct parser_params *p, ID id)
14947{
14948 return local_id_ref(p, id, NULL);
14949}
14950
14951static int
14952check_forwarding_args(struct parser_params *p)
14953{
14954 if (local_id(p, idFWD_ALL)) return TRUE;
14955 compile_error(p, "unexpected ...");
14956 return FALSE;
14957}
14958
14959static void
14960add_forwarding_args(struct parser_params *p)
14961{
14962 arg_var(p, idFWD_REST);
14963#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14964 arg_var(p, idFWD_KWREST);
14965#endif
14966 arg_var(p, idFWD_BLOCK);
14967 arg_var(p, idFWD_ALL);
14968}
14969
14970static void
14971forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
14972{
14973 bool conflict = false;
14974
14975 struct vtable *vars, *args;
14976
14977 vars = p->lvtbl->vars;
14978 args = p->lvtbl->args;
14979
14980 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
14981 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
14982 vars = vars->prev;
14983 args = args->prev;
14984 }
14985
14986 bool found = false;
14987 if (vars && vars->prev == DVARS_INHERIT && !found) {
14988 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
14989 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
14990 }
14991 else {
14992 found = (vtable_included(args, arg) &&
14993 !(all && vtable_included(args, all)));
14994 }
14995
14996 if (!found) {
14997 compile_error(p, "no anonymous %s parameter", var);
14998 }
14999 else if (conflict) {
15000 compile_error(p, "anonymous %s parameter is also used within block", var);
15001 }
15002}
15003
15004static NODE *
15005new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15006{
15007 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15008#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15009 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15010#endif
15011 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15012 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15013 block->forwarding = TRUE;
15014#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15015 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15016#endif
15017 return arg_blk_pass(args, block);
15018}
15019
15020static NODE *
15021numparam_push(struct parser_params *p)
15022{
15023 struct local_vars *local = p->lvtbl;
15024 NODE *inner = local->numparam.inner;
15025 if (!local->numparam.outer) {
15026 local->numparam.outer = local->numparam.current;
15027 }
15028 local->numparam.inner = 0;
15029 local->numparam.current = 0;
15030 local->it = 0;
15031 return inner;
15032}
15033
15034static void
15035numparam_pop(struct parser_params *p, NODE *prev_inner)
15036{
15037 struct local_vars *local = p->lvtbl;
15038 if (prev_inner) {
15039 /* prefer first one */
15040 local->numparam.inner = prev_inner;
15041 }
15042 else if (local->numparam.current) {
15043 /* current and inner are exclusive */
15044 local->numparam.inner = local->numparam.current;
15045 }
15046 if (p->max_numparam > NO_PARAM) {
15047 /* current and outer are exclusive */
15048 local->numparam.current = local->numparam.outer;
15049 local->numparam.outer = 0;
15050 }
15051 else {
15052 /* no numbered parameter */
15053 local->numparam.current = 0;
15054 }
15055 local->it = 0;
15056}
15057
15058static const struct vtable *
15059dyna_push(struct parser_params *p)
15060{
15061 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15062 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15063 if (p->lvtbl->used) {
15064 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15065 }
15066 return p->lvtbl->args;
15067}
15068
15069static void
15070dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15071{
15072 struct vtable *tmp = *vtblp;
15073 *vtblp = tmp->prev;
15074# if WARN_PAST_SCOPE
15075 if (p->past_scope_enabled) {
15076 tmp->prev = p->lvtbl->past;
15077 p->lvtbl->past = tmp;
15078 return;
15079 }
15080# endif
15081 vtable_free(tmp);
15082}
15083
15084static void
15085dyna_pop_1(struct parser_params *p)
15086{
15087 struct vtable *tmp;
15088
15089 if ((tmp = p->lvtbl->used) != 0) {
15090 warn_unused_var(p, p->lvtbl);
15091 p->lvtbl->used = p->lvtbl->used->prev;
15092 vtable_free(tmp);
15093 }
15094 dyna_pop_vtable(p, &p->lvtbl->args);
15095 dyna_pop_vtable(p, &p->lvtbl->vars);
15096}
15097
15098static void
15099dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15100{
15101 while (p->lvtbl->args != lvargs) {
15102 dyna_pop_1(p);
15103 if (!p->lvtbl->args) {
15104 struct local_vars *local = p->lvtbl->prev;
15105 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15106 p->lvtbl = local;
15107 }
15108 }
15109 dyna_pop_1(p);
15110}
15111
15112static int
15113dyna_in_block(struct parser_params *p)
15114{
15115 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15116}
15117
15118#ifndef RIPPER
15119int
15120dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15121{
15122 struct vtable *vars, *args, *used;
15123 int i;
15124
15125 args = p->lvtbl->args;
15126 vars = p->lvtbl->vars;
15127 used = p->lvtbl->used;
15128
15129 while (!DVARS_TERMINAL_P(vars)) {
15130 if (vtable_included(args, id)) {
15131 return 1;
15132 }
15133 if ((i = vtable_included(vars, id)) != 0) {
15134 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15135 return 1;
15136 }
15137 args = args->prev;
15138 vars = vars->prev;
15139 if (!vidrefp) used = 0;
15140 if (used) used = used->prev;
15141 }
15142
15143 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15144 return rb_dvar_defined(id, p->parent_iseq);
15145 }
15146
15147 return 0;
15148}
15149#endif
15150
15151static int
15152dvar_defined(struct parser_params *p, ID id)
15153{
15154 return dvar_defined_ref(p, id, NULL);
15155}
15156
15157static int
15158dvar_curr(struct parser_params *p, ID id)
15159{
15160 return (vtable_included(p->lvtbl->args, id) ||
15161 vtable_included(p->lvtbl->vars, id));
15162}
15163
15164static void
15165reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15166{
15167 compile_error(p,
15168 "regexp encoding option '%c' differs from source encoding '%s'",
15169 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15170}
15171
15172#ifndef RIPPER
15173static rb_encoding *
15174find_enc(struct parser_params* p, const char *name)
15175{
15176 int idx = rb_enc_find_index(name);
15177 if (idx < 0) {
15178 rb_bug("unknown encoding name: %s", name);
15179 }
15180
15181 return rb_enc_from_index(idx);
15182}
15183
15184static rb_encoding *
15185kcode_to_enc(struct parser_params* p, int kcode)
15186{
15187 rb_encoding *enc;
15188
15189 switch (kcode) {
15190 case ENC_ASCII8BIT:
15191 enc = rb_ascii8bit_encoding();
15192 break;
15193 case ENC_EUC_JP:
15194 enc = find_enc(p, "EUC-JP");
15195 break;
15196 case ENC_Windows_31J:
15197 enc = find_enc(p, "Windows-31J");
15198 break;
15199 case ENC_UTF8:
15200 enc = rb_utf8_encoding();
15201 break;
15202 default:
15203 enc = NULL;
15204 break;
15205 }
15206
15207 return enc;
15208}
15209
15210int
15211rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15212{
15213 int c = RE_OPTION_ENCODING_IDX(options);
15214
15215 if (c) {
15216 int opt, idx;
15217 rb_encoding *enc;
15218
15219 char_to_option_kcode(c, &opt, &idx);
15220 enc = kcode_to_enc(p, idx);
15221 if (enc != rb_parser_str_get_encoding(str) &&
15222 !rb_parser_is_ascii_string(p, str)) {
15223 goto error;
15224 }
15225 rb_parser_string_set_encoding(str, enc);
15226 }
15227 else if (RE_OPTION_ENCODING_NONE(options)) {
15228 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15229 !rb_parser_is_ascii_string(p, str)) {
15230 c = 'n';
15231 goto error;
15232 }
15233 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15234 }
15235 else if (rb_is_usascii_enc(p->enc)) {
15236 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15237 }
15238 return 0;
15239
15240 error:
15241 return c;
15242}
15243#endif
15244
15245static void
15246reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15247{
15248 int c = rb_reg_fragment_setenc(p, str, options);
15249 if (c) reg_fragment_enc_error(p, str, c);
15250}
15251
15252#ifndef UNIVERSAL_PARSER
15253typedef struct {
15254 struct parser_params* parser;
15255 rb_encoding *enc;
15256 NODE *succ_block;
15257 const YYLTYPE *loc;
15258 rb_parser_assignable_func assignable;
15259} reg_named_capture_assign_t;
15260
15261static int
15262reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15263 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15264{
15265 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15266 struct parser_params* p = arg->parser;
15267 rb_encoding *enc = arg->enc;
15268 long len = name_end - name;
15269 const char *s = (const char *)name;
15270
15271 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15272}
15273
15274static NODE *
15275reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15276{
15277 reg_named_capture_assign_t arg;
15278
15279 arg.parser = p;
15280 arg.enc = rb_enc_get(regexp);
15281 arg.succ_block = 0;
15282 arg.loc = loc;
15283 arg.assignable = assignable;
15284 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15285
15286 if (!arg.succ_block) return 0;
15287 return RNODE_BLOCK(arg.succ_block)->nd_next;
15288}
15289#endif
15290
15291#ifndef RIPPER
15292NODE *
15293rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15294{
15295 return assignable(p, id, val, loc);
15296}
15297
15298int
15299rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15300 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15301{
15302 ID var;
15303 NODE *node, *succ;
15304
15305 if (!len) return ST_CONTINUE;
15306 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15307 return ST_CONTINUE;
15308
15309 var = intern_cstr(s, len, enc);
15310 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15311 if (!lvar_defined(p, var)) return ST_CONTINUE;
15312 }
15313 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15314 succ = *succ_block;
15315 if (!succ) succ = NEW_ERROR(loc);
15316 succ = block_append(p, succ, node);
15317 *succ_block = succ;
15318 return ST_CONTINUE;
15319}
15320#endif
15321
15322static VALUE
15323parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15324{
15325 VALUE str2;
15326 reg_fragment_setenc(p, str, options);
15327 str2 = rb_str_new_parser_string(str);
15328 return rb_parser_reg_compile(p, str2, options);
15329}
15330
15331#ifndef RIPPER
15332VALUE
15333rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15334{
15335 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15336}
15337#endif
15338
15339static VALUE
15340reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15341{
15342 VALUE re;
15343 VALUE err;
15344
15345 err = rb_errinfo();
15346 re = parser_reg_compile(p, str, options);
15347 if (NIL_P(re)) {
15348 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15349 rb_set_errinfo(err);
15350 compile_error(p, "%"PRIsVALUE, m);
15351 return Qnil;
15352 }
15353 return re;
15354}
15355
15356#ifndef RIPPER
15357void
15358rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15359{
15360 p->do_print = print;
15361 p->do_loop = loop;
15362 p->do_chomp = chomp;
15363 p->do_split = split;
15364}
15365
15366static NODE *
15367parser_append_options(struct parser_params *p, NODE *node)
15368{
15369 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15370 const YYLTYPE *const LOC = &default_location;
15371
15372 if (p->do_print) {
15373 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15374 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15375 LOC);
15376 node = block_append(p, node, print);
15377 }
15378
15379 if (p->do_loop) {
15380 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15381
15382 if (p->do_split) {
15383 ID ifs = rb_intern("$;");
15384 ID fields = rb_intern("$F");
15385 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15386 NODE *split = NEW_GASGN(fields,
15387 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15388 rb_intern("split"), args, LOC),
15389 LOC);
15390 node = block_append(p, split, node);
15391 }
15392 if (p->do_chomp) {
15393 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15394 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15395 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15396 }
15397
15398 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15399 }
15400
15401 return node;
15402}
15403
15404void
15405rb_init_parse(void)
15406{
15407 /* just to suppress unused-function warnings */
15408 (void)nodetype;
15409 (void)nodeline;
15410}
15411
15412ID
15413internal_id(struct parser_params *p)
15414{
15415 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15416}
15417#endif /* !RIPPER */
15418
15419static void
15420parser_initialize(struct parser_params *p)
15421{
15422 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15423 p->command_start = TRUE;
15424 p->ruby_sourcefile_string = Qnil;
15425 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15426 string_buffer_init(p);
15427 p->node_id = 0;
15428 p->delayed.token = NULL;
15429 p->frozen_string_literal = -1; /* not specified */
15430#ifndef RIPPER
15431 p->error_buffer = Qfalse;
15432 p->end_expect_token_locations = NULL;
15433 p->token_id = 0;
15434 p->tokens = NULL;
15435#else
15436 p->result = Qnil;
15437 p->parsing_thread = Qnil;
15438 p->s_value = Qnil;
15439 p->s_lvalue = Qnil;
15440 p->s_value_stack = rb_ary_new();
15441#endif
15442 p->debug_buffer = Qnil;
15443 p->debug_output = rb_ractor_stdout();
15444 p->enc = rb_utf8_encoding();
15445 p->exits = 0;
15446}
15447
15448#ifdef RIPPER
15449#define rb_ruby_parser_mark ripper_parser_mark
15450#define rb_ruby_parser_free ripper_parser_free
15451#define rb_ruby_parser_memsize ripper_parser_memsize
15452#endif
15453
15454void
15455rb_ruby_parser_mark(void *ptr)
15456{
15457 struct parser_params *p = (struct parser_params*)ptr;
15458
15459 rb_gc_mark(p->ruby_sourcefile_string);
15460#ifndef RIPPER
15461 rb_gc_mark(p->error_buffer);
15462#else
15463 rb_gc_mark(p->value);
15464 rb_gc_mark(p->result);
15465 rb_gc_mark(p->parsing_thread);
15466 rb_gc_mark(p->s_value);
15467 rb_gc_mark(p->s_lvalue);
15468 rb_gc_mark(p->s_value_stack);
15469#endif
15470 rb_gc_mark(p->debug_buffer);
15471 rb_gc_mark(p->debug_output);
15472}
15473
15474void
15475rb_ruby_parser_free(void *ptr)
15476{
15477 struct parser_params *p = (struct parser_params*)ptr;
15478 struct local_vars *local, *prev;
15479
15480 if (p->ast) {
15481 rb_ast_free(p->ast);
15482 }
15483
15484 if (p->warn_duplicate_keys_table) {
15485 st_free_table(p->warn_duplicate_keys_table);
15486 }
15487
15488#ifndef RIPPER
15489 if (p->tokens) {
15490 rb_parser_ary_free(p, p->tokens);
15491 }
15492#endif
15493
15494 if (p->tokenbuf) {
15495 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15496 }
15497
15498 for (local = p->lvtbl; local; local = prev) {
15499 prev = local->prev;
15500 local_free(p, local);
15501 }
15502
15503 {
15504 token_info *ptinfo;
15505 while ((ptinfo = p->token_info) != 0) {
15506 p->token_info = ptinfo->next;
15507 xfree(ptinfo);
15508 }
15509 }
15510 string_buffer_free(p);
15511
15512 if (p->pvtbl) {
15513 st_free_table(p->pvtbl);
15514 }
15515
15516 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15517 st_free_table(p->case_labels);
15518 }
15519
15520 xfree(p->lex.strterm);
15521 p->lex.strterm = 0;
15522
15523 xfree(ptr);
15524}
15525
15526size_t
15527rb_ruby_parser_memsize(const void *ptr)
15528{
15529 struct parser_params *p = (struct parser_params*)ptr;
15530 struct local_vars *local;
15531 size_t size = sizeof(*p);
15532
15533 size += p->toksiz;
15534 for (local = p->lvtbl; local; local = local->prev) {
15535 size += sizeof(*local);
15536 if (local->vars) size += local->vars->capa * sizeof(ID);
15537 }
15538 return size;
15539}
15540
15541#ifndef RIPPER
15542#undef rb_reserved_word
15543
15544const struct kwtable *
15545rb_reserved_word(const char *str, unsigned int len)
15546{
15547 return reserved_word(str, len);
15548}
15549
15550#ifdef UNIVERSAL_PARSER
15551rb_parser_t *
15552rb_ruby_parser_allocate(const rb_parser_config_t *config)
15553{
15554 /* parser_initialize expects fields to be set to 0 */
15555 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15556 p->config = config;
15557 return p;
15558}
15559
15560rb_parser_t *
15561rb_ruby_parser_new(const rb_parser_config_t *config)
15562{
15563 /* parser_initialize expects fields to be set to 0 */
15564 rb_parser_t *p = rb_ruby_parser_allocate(config);
15565 parser_initialize(p);
15566 return p;
15567}
15568#else
15569rb_parser_t *
15570rb_ruby_parser_allocate(void)
15571{
15572 /* parser_initialize expects fields to be set to 0 */
15573 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15574 return p;
15575}
15576
15577rb_parser_t *
15578rb_ruby_parser_new(void)
15579{
15580 /* parser_initialize expects fields to be set to 0 */
15581 rb_parser_t *p = rb_ruby_parser_allocate();
15582 parser_initialize(p);
15583 return p;
15584}
15585#endif
15586
15587rb_parser_t *
15588rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15589{
15590 p->error_buffer = main ? Qfalse : Qnil;
15591 p->parent_iseq = base;
15592 return p;
15593}
15594
15595void
15596rb_ruby_parser_set_script_lines(rb_parser_t *p)
15597{
15598 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15599}
15600
15601void
15602rb_ruby_parser_error_tolerant(rb_parser_t *p)
15603{
15604 p->error_tolerant = 1;
15605}
15606
15607void
15608rb_ruby_parser_keep_tokens(rb_parser_t *p)
15609{
15610 p->keep_tokens = 1;
15611 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15612}
15613
15614rb_encoding *
15615rb_ruby_parser_encoding(rb_parser_t *p)
15616{
15617 return p->enc;
15618}
15619
15620int
15621rb_ruby_parser_end_seen_p(rb_parser_t *p)
15622{
15623 return p->ruby__end__seen;
15624}
15625
15626int
15627rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15628{
15629 p->debug = flag;
15630 return flag;
15631}
15632#endif /* !RIPPER */
15633
15634#ifdef RIPPER
15635int
15636rb_ruby_parser_get_yydebug(rb_parser_t *p)
15637{
15638 return p->debug;
15639}
15640
15641void
15642rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15643{
15644 p->value = value;
15645}
15646
15647int
15648rb_ruby_parser_error_p(rb_parser_t *p)
15649{
15650 return p->error_p;
15651}
15652
15653VALUE
15654rb_ruby_parser_debug_output(rb_parser_t *p)
15655{
15656 return p->debug_output;
15657}
15658
15659void
15660rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15661{
15662 p->debug_output = output;
15663}
15664
15665VALUE
15666rb_ruby_parser_parsing_thread(rb_parser_t *p)
15667{
15668 return p->parsing_thread;
15669}
15670
15671void
15672rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15673{
15674 p->parsing_thread = parsing_thread;
15675}
15676
15677void
15678rb_ruby_parser_ripper_initialize(rb_parser_t *p, rb_parser_lex_gets_func *gets, rb_parser_input_data input, VALUE sourcefile_string, const char *sourcefile, int sourceline)
15679{
15680 p->lex.gets = gets;
15681 p->lex.input = input;
15682 p->eofp = 0;
15683 p->ruby_sourcefile_string = sourcefile_string;
15684 p->ruby_sourcefile = sourcefile;
15685 p->ruby_sourceline = sourceline;
15686}
15687
15688VALUE
15689rb_ruby_parser_result(rb_parser_t *p)
15690{
15691 return p->result;
15692}
15693
15694rb_encoding *
15695rb_ruby_parser_enc(rb_parser_t *p)
15696{
15697 return p->enc;
15698}
15699
15700VALUE
15701rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15702{
15703 return p->ruby_sourcefile_string;
15704}
15705
15706int
15707rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15708{
15709 return p->ruby_sourceline;
15710}
15711
15712int
15713rb_ruby_parser_lex_state(rb_parser_t *p)
15714{
15715 return p->lex.state;
15716}
15717
15718void
15719rb_ruby_ripper_parse0(rb_parser_t *p)
15720{
15721 parser_prepare(p);
15722 p->ast = rb_ast_new();
15723 ripper_yyparse((void*)p);
15724 rb_ast_free(p->ast);
15725 p->ast = 0;
15726 p->eval_tree = 0;
15727 p->eval_tree_begin = 0;
15728}
15729
15730int
15731rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15732{
15733 return dedent_string(p, string, width);
15734}
15735
15736int
15737rb_ruby_ripper_initialized_p(rb_parser_t *p)
15738{
15739 return p->lex.input != 0;
15740}
15741
15742void
15743rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15744{
15745 parser_initialize(p);
15746}
15747
15748long
15749rb_ruby_ripper_column(rb_parser_t *p)
15750{
15751 return p->lex.ptok - p->lex.pbeg;
15752}
15753
15754long
15755rb_ruby_ripper_token_len(rb_parser_t *p)
15756{
15757 return p->lex.pcur - p->lex.ptok;
15758}
15759
15760rb_parser_string_t *
15761rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15762{
15763 return p->lex.lastline;
15764}
15765
15766VALUE
15767rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15768{
15769 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15770}
15771
15772#ifdef UNIVERSAL_PARSER
15773rb_parser_t *
15774rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15775{
15776 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15777 p->config = config;
15778 return p;
15779}
15780#endif
15781
15782struct parser_params*
15783rb_ruby_ripper_parser_allocate(void)
15784{
15785 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15786}
15787#endif /* RIPPER */
15788
15789#ifndef RIPPER
15790void
15791rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15792{
15793 va_list ap;
15794 VALUE mesg = p->debug_buffer;
15795
15796 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15797 va_start(ap, fmt);
15798 rb_str_vcatf(mesg, fmt, ap);
15799 va_end(ap);
15800 if (char_at_end(p, mesg, 0) == '\n') {
15801 rb_io_write(p->debug_output, mesg);
15802 p->debug_buffer = Qnil;
15803 }
15804}
15805
15806static void
15807parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15808{
15809 va_list ap;
15810 int lineno, column;
15811
15812 if (loc) {
15813 lineno = loc->end_pos.lineno;
15814 column = loc->end_pos.column;
15815 }
15816 else {
15817 lineno = p->ruby_sourceline;
15818 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15819 }
15820
15821 rb_io_flush(p->debug_output);
15822 p->error_p = 1;
15823 va_start(ap, fmt);
15824 p->error_buffer =
15825 rb_syntax_error_append(p->error_buffer,
15826 p->ruby_sourcefile_string,
15827 lineno, column,
15828 p->enc, fmt, ap);
15829 va_end(ap);
15830}
15831
15832static size_t
15833count_char(const char *str, int c)
15834{
15835 int n = 0;
15836 while (str[n] == c) ++n;
15837 return n;
15838}
15839
15840/*
15841 * strip enclosing double-quotes, same as the default yytnamerr except
15842 * for that single-quotes matching back-quotes do not stop stripping.
15843 *
15844 * "\"`class' keyword\"" => "`class' keyword"
15845 */
15846size_t
15847rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
15848{
15849 if (*yystr == '"') {
15850 size_t yyn = 0, bquote = 0;
15851 const char *yyp = yystr;
15852
15853 while (*++yyp) {
15854 switch (*yyp) {
15855 case '\'':
15856 if (!bquote) {
15857 bquote = count_char(yyp+1, '\'') + 1;
15858 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
15859 yyn += bquote;
15860 yyp += bquote - 1;
15861 break;
15862 }
15863 else {
15864 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
15865 if (yyres) memcpy(yyres + yyn, yyp, bquote);
15866 yyn += bquote;
15867 yyp += bquote - 1;
15868 bquote = 0;
15869 break;
15870 }
15871 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
15872 if (yyres) memcpy(yyres + yyn, yyp, 3);
15873 yyn += 3;
15874 yyp += 2;
15875 break;
15876 }
15877 goto do_not_strip_quotes;
15878 }
15879
15880 case ',':
15881 goto do_not_strip_quotes;
15882
15883 case '\\':
15884 if (*++yyp != '\\')
15885 goto do_not_strip_quotes;
15886 /* Fall through. */
15887 default:
15888 if (yyres)
15889 yyres[yyn] = *yyp;
15890 yyn++;
15891 break;
15892
15893 case '"':
15894 case '\0':
15895 if (yyres)
15896 yyres[yyn] = '\0';
15897 return yyn;
15898 }
15899 }
15900 do_not_strip_quotes: ;
15901 }
15902
15903 if (!yyres) return strlen(yystr);
15904
15905 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
15906}
15907#endif
15908
15909#ifdef RIPPER
15910#define validate(x) (void)(x)
15911
15912static VALUE
15913ripper_dispatch0(struct parser_params *p, ID mid)
15914{
15915 return rb_funcall(p->value, mid, 0);
15916}
15917
15918static VALUE
15919ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
15920{
15921 validate(a);
15922 return rb_funcall(p->value, mid, 1, a);
15923}
15924
15925static VALUE
15926ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
15927{
15928 validate(a);
15929 validate(b);
15930 return rb_funcall(p->value, mid, 2, a, b);
15931}
15932
15933static VALUE
15934ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
15935{
15936 validate(a);
15937 validate(b);
15938 validate(c);
15939 return rb_funcall(p->value, mid, 3, a, b, c);
15940}
15941
15942static VALUE
15943ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
15944{
15945 validate(a);
15946 validate(b);
15947 validate(c);
15948 validate(d);
15949 return rb_funcall(p->value, mid, 4, a, b, c, d);
15950}
15951
15952static VALUE
15953ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
15954{
15955 validate(a);
15956 validate(b);
15957 validate(c);
15958 validate(d);
15959 validate(e);
15960 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
15961}
15962
15963static VALUE
15964ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
15965{
15966 validate(a);
15967 validate(b);
15968 validate(c);
15969 validate(d);
15970 validate(e);
15971 validate(f);
15972 validate(g);
15973 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
15974}
15975
15976void
15977ripper_error(struct parser_params *p)
15978{
15979 p->error_p = TRUE;
15980}
15981
15982VALUE
15983ripper_value(struct parser_params *p)
15984{
15985 (void)yystpcpy; /* may not used in newer bison */
15986
15987 return p->value;
15988}
15989
15990#endif /* RIPPER */
15991/*
15992 * Local variables:
15993 * mode: c
15994 * c-file-style: "ruby"
15995 * End:
15996 */