Ruby 3.5.0dev (2025-01-10 revision 5fab31b15e32622c4b71d1d347a41937e9f9c212)
parse.y (5fab31b15e32622c4b71d1d347a41937e9f9c212)
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_TERMINTOR ((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);
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);
1150static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *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);
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) (NODE *)rb_node_class_new(p,n,b,s,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) (NODE *)rb_node_colon2_new(p,c,i,loc)
1258#define NEW_COLON3(i,loc) (NODE *)rb_node_colon3_new(p,i,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) (NODE *)rb_node_postexe_new(p,b,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 NODE *ret_args(struct parser_params*,NODE*);
1445static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1446static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
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 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 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_marg_list 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 block_param_def
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 mlhs_post
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_primitive_value 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 operation 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%rule %inline inline_operation : ident_or_const
2914 | tFID
2915 ;
2916/*
2917 * parameterizing rules
2918 */
2919%rule asgn(lhs, rhs) <node>
2920 : lhs '=' lex_ctxt rhs
2921 {
2922 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2923 /*% ripper: assign!($:1, $:4) %*/
2924 }
2925 ;
2926
2927%rule def_endless_method(bodystmt) <node>
2928 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2929 {
2930 endless_method_name(p, $head->nd_mid, &@head);
2931 restore_defun(p, $head);
2932 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
2933 ($$ = $head->nd_def)->nd_loc = @$;
2934 RNODE_DEFN($$)->nd_defn = $bodystmt;
2935 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2936 /*% ripper: def!($:head, $:args, $:$) %*/
2937 local_pop(p);
2938 }
2939 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2940 {
2941 endless_method_name(p, $head->nd_mid, &@head);
2942 restore_defun(p, $head);
2943 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
2944 ($$ = $head->nd_def)->nd_loc = @$;
2945 RNODE_DEFS($$)->nd_defn = $bodystmt;
2946 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2947 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2948 local_pop(p);
2949 }
2950 ;
2951
2952%rule compstmt(stmts) <node>
2953 : stmts terms?
2954 {
2955 void_stmts(p, $$ = $stmts);
2956 }
2957 ;
2958
2959%rule f_opt(value) <node_opt_arg>
2960 : f_arg_asgn f_eq value
2961 {
2962 p->ctxt.in_argdef = 1;
2963 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
2964 /*% ripper: [$:$, $:3] %*/
2965 }
2966 ;
2967
2968%rule f_optarg(value) <node_opt_arg>
2969 : f_opt(value)
2970 {
2971 $$ = $f_opt;
2972 /*% ripper: rb_ary_new3(1, $:1) %*/
2973 }
2974 | f_optarg(value) ',' f_opt(value)
2975 {
2976 $$ = opt_arg_append($f_optarg, $f_opt);
2977 /*% ripper: rb_ary_push($:1, $:3) %*/
2978 }
2979 ;
2980
2981%rule f_kw(value) <node_kw_arg>: f_label value
2982 {
2983 p->ctxt.in_argdef = 1;
2984 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
2985 /*% ripper: [$:$, $:value] %*/
2986 }
2987 | f_label
2988 {
2989 p->ctxt.in_argdef = 1;
2990 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
2991 /*% ripper: [$:$, 0] %*/
2992 }
2993 ;
2994
2995%rule f_kwarg(value) <node_kw_arg>
2996 : f_kw(value)
2997 {
2998 $$ = $f_kw;
2999 /*% ripper: rb_ary_new3(1, $:1) %*/
3000 }
3001 | f_kwarg(value) ',' f_kw(value)
3002 {
3003 $$ = kwd_append($f_kwarg, $f_kw);
3004 /*% ripper: rb_ary_push($:1, $:3) %*/
3005 }
3006 ;
3007
3008%rule op_asgn(rhs) <node>
3009 : var_lhs tOP_ASGN lex_ctxt rhs
3010 {
3011 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3012 /*% ripper: opassign!($:1, $:2, $:4) %*/
3013 }
3014 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3015 {
3016 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3017 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3018 }
3019 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3020 {
3021 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3022 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3023 }
3024 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3025 {
3026 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3027 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3028 }
3029 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3030 {
3031 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3032 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3033 }
3034 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3035 {
3036 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3037 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3038 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3039 }
3040 | backref tOP_ASGN lex_ctxt rhs
3041 {
3042 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3043 $$ = NEW_ERROR(&@$);
3044 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3045 }
3046 ;
3047
3048%rule opt_args_tail(tail) <node_args>
3049 : ',' tail
3050 {
3051 $$ = $tail;
3052 /*% ripper: $:2 %*/
3053 }
3054 | /* none */
3055 {
3056 $$ = new_args_tail(p, 0, 0, 0, &@0);
3057 /*% ripper: [Qnil, Qnil, Qnil] %*/
3058 }
3059 ;
3060
3061%rule value_expr(value) <node>
3062 : value
3063 {
3064 value_expr($1);
3065 $$ = $1;
3066 }
3067 ;
3068
3069%rule words(begin, word_list) <node>
3070 : begin ' '+ word_list tSTRING_END
3071 {
3072 $$ = make_list($word_list, &@$);
3073 /*% ripper: array!($:3) %*/
3074 }
3075 ;
3076
3077%%
3078program : {
3079 SET_LEX_STATE(EXPR_BEG);
3080 local_push(p, ifndef_ripper(1)+0);
3081 /* jumps are possible in the top-level loop. */
3082 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3083 }
3084 compstmt(top_stmts)
3085 {
3086 if ($2 && !compile_for_eval) {
3087 NODE *node = $2;
3088 /* last expression should not be void */
3089 if (nd_type_p(node, NODE_BLOCK)) {
3090 while (RNODE_BLOCK(node)->nd_next) {
3091 node = RNODE_BLOCK(node)->nd_next;
3092 }
3093 node = RNODE_BLOCK(node)->nd_head;
3094 }
3095 node = remove_begin(node);
3096 void_expr(p, node);
3097 }
3098 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
3099 /*% ripper[final]: program!($:2) %*/
3100 local_pop(p);
3101 }
3102 ;
3103
3104top_stmts : none
3105 {
3106 $$ = NEW_BEGIN(0, &@$);
3107 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3108 }
3109 | top_stmt
3110 {
3111 $$ = newline_node($1);
3112 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3113 }
3114 | top_stmts terms top_stmt
3115 {
3116 $$ = block_append(p, $1, newline_node($3));
3117 /*% ripper: stmts_add!($:1, $:3) %*/
3118 }
3119 ;
3120
3121top_stmt : stmt
3122 {
3123 clear_block_exit(p, true);
3124 $$ = $1;
3125 }
3126 | keyword_BEGIN begin_block
3127 {
3128 $$ = $2;
3129 /*% ripper: $:2 %*/
3130 }
3131 ;
3132
3133block_open : '{' {$$ = init_block_exit(p);};
3134
3135begin_block : block_open compstmt(top_stmts) '}'
3136 {
3137 restore_block_exit(p, $block_open);
3138 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3139 NEW_BEGIN($2, &@$));
3140 $$ = NEW_BEGIN(0, &@$);
3141 /*% ripper: BEGIN!($:2) %*/
3142 }
3143 ;
3144
3145bodystmt : compstmt(stmts)[body]
3146 lex_ctxt[ctxt]
3147 opt_rescue
3148 k_else
3149 {
3150 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3151 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3152 }
3153 compstmt(stmts)[elsebody]
3154 {
3155 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3156 }
3157 opt_ensure
3158 {
3159 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3160 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3161 }
3162 | compstmt(stmts)[body]
3163 lex_ctxt[ctxt]
3164 opt_rescue
3165 {
3166 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3167 }
3168 opt_ensure
3169 {
3170 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3171 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3172 }
3173 ;
3174
3175stmts : none
3176 {
3177 $$ = NEW_BEGIN(0, &@$);
3178 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3179 }
3180 | stmt_or_begin
3181 {
3182 $$ = newline_node($1);
3183 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3184 }
3185 | stmts terms stmt_or_begin
3186 {
3187 $$ = block_append(p, $1, newline_node($3));
3188 /*% ripper: stmts_add!($:1, $:3) %*/
3189 }
3190 ;
3191
3192stmt_or_begin : stmt
3193 | keyword_BEGIN
3194 {
3195 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3196 }
3197 begin_block
3198 {
3199 $$ = $3;
3200 }
3201 ;
3202
3203allow_exits : {$$ = allow_block_exit(p);};
3204
3205k_END : keyword_END lex_ctxt
3206 {
3207 $$ = $2;
3208 p->ctxt.in_rescue = before_rescue;
3209 /*% ripper: $:2 %*/
3210 };
3211
3212stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3213 {
3214 $$ = NEW_ALIAS($2, $4, &@$, &@1);
3215 /*% ripper: alias!($:2, $:4) %*/
3216 }
3217 | keyword_alias tGVAR tGVAR
3218 {
3219 $$ = NEW_VALIAS($2, $3, &@$, &@1);
3220 /*% ripper: var_alias!($:2, $:3) %*/
3221 }
3222 | keyword_alias tGVAR tBACK_REF
3223 {
3224 char buf[2];
3225 buf[0] = '$';
3226 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
3227 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$, &@1);
3228 /*% ripper: var_alias!($:2, $:3) %*/
3229 }
3230 | keyword_alias tGVAR tNTH_REF
3231 {
3232 static const char mesg[] = "can't make alias for the number variables";
3233 /*%%%*/
3234 yyerror1(&@3, mesg);
3235 /*% %*/
3236 $$ = NEW_ERROR(&@$);
3237 /*% ripper[error]: alias_error!(ERR_MESG(), $:3) %*/
3238 }
3239 | keyword_undef undef_list
3240 {
3241 nd_set_first_loc($2, @1.beg_pos);
3242 RNODE_UNDEF($2)->keyword_loc = @1;
3243 $$ = $2;
3244 /*% ripper: undef!($:2) %*/
3245 }
3246 | stmt modifier_if expr_value
3247 {
3248 $$ = new_if(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3249 fixpos($$, $3);
3250 /*% ripper: if_mod!($:3, $:1) %*/
3251 }
3252 | stmt modifier_unless expr_value
3253 {
3254 $$ = new_unless(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3255 fixpos($$, $3);
3256 /*% ripper: unless_mod!($:3, $:1) %*/
3257 }
3258 | stmt modifier_while expr_value
3259 {
3260 clear_block_exit(p, false);
3261 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3262 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3263 }
3264 else {
3265 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3266 }
3267 /*% ripper: while_mod!($:3, $:1) %*/
3268 }
3269 | stmt modifier_until expr_value
3270 {
3271 clear_block_exit(p, false);
3272 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3273 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3274 }
3275 else {
3276 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3277 }
3278 /*% ripper: until_mod!($:3, $:1) %*/
3279 }
3280 | stmt modifier_rescue after_rescue stmt
3281 {
3282 p->ctxt.in_rescue = $3.in_rescue;
3283 NODE *resq;
3284 YYLTYPE loc = code_loc_gen(&@2, &@4);
3285 resq = NEW_RESBODY(0, 0, remove_begin($4), 0, &loc);
3286 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
3287 /*% ripper: rescue_mod!($:1, $:4) %*/
3288 }
3289 | k_END allow_exits '{' compstmt(stmts) '}'
3290 {
3291 if (p->ctxt.in_def) {
3292 rb_warn0("END in method; use at_exit");
3293 }
3294 restore_block_exit(p, $allow_exits);
3295 p->ctxt = $k_END;
3296 {
3297 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, &@$);
3298 $$ = NEW_POSTEXE(scope, &@$);
3299 }
3300 /*% ripper: END!($:compstmt) %*/
3301 }
3302 | command_asgn
3303 | mlhs '=' lex_ctxt command_call_value
3304 {
3305 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3306 /*% ripper: massign!($:1, $:4) %*/
3307 }
3308 | asgn(lhs, mrhs)
3309 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
3310 after_rescue stmt[resbody]
3311 {
3312 p->ctxt.in_rescue = $3.in_rescue;
3313 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3314 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3315 loc.beg_pos = @mrhs_arg.beg_pos;
3316 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3317 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
3318 /*% ripper: massign!($:1, rescue_mod!($:4, $:7)) %*/
3319 }
3320 | mlhs '=' lex_ctxt mrhs_arg
3321 {
3322 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3323 /*% ripper: massign!($:1, $:4) %*/
3324 }
3325 | expr
3326 | error
3327 {
3328 (void)yynerrs;
3329 $$ = NEW_ERROR(&@$);
3330 }
3331 ;
3332
3333command_asgn : asgn(lhs, command_rhs)
3334 | op_asgn(command_rhs)
3335 | def_endless_method(endless_command)
3336 ;
3337
3338endless_command : command
3339 | endless_command modifier_rescue after_rescue arg
3340 {
3341 p->ctxt.in_rescue = $3.in_rescue;
3342 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3343 /*% ripper: rescue_mod!($:1, $:4) %*/
3344 }
3345 | keyword_not '\n'? endless_command
3346 {
3347 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3348 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3349 }
3350 ;
3351
3352command_rhs : command_call_value %prec tOP_ASGN
3353 | command_call_value modifier_rescue after_rescue stmt
3354 {
3355 p->ctxt.in_rescue = $3.in_rescue;
3356 YYLTYPE loc = code_loc_gen(&@2, &@4);
3357 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3358 /*% ripper: rescue_mod!($:1, $:4) %*/
3359 }
3360 | command_asgn
3361 ;
3362
3363expr : command_call
3364 | expr keyword_and expr
3365 {
3366 $$ = logop(p, idAND, $1, $3, &@2, &@$);
3367 /*% ripper: binary!($:1, ID2VAL(idAND), $:3) %*/
3368 }
3369 | expr keyword_or expr
3370 {
3371 $$ = logop(p, idOR, $1, $3, &@2, &@$);
3372 /*% ripper: binary!($:1, ID2VAL(idOR), $:3) %*/
3373 }
3374 | keyword_not '\n'? expr
3375 {
3376 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3377 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3378 }
3379 | '!' command_call
3380 {
3381 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3382 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
3383 }
3384 | arg tASSOC
3385 {
3386 value_expr($arg);
3387 }
3388 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3389 p_top_expr_body[body]
3390 {
3391 pop_pktbl(p, $p_pktbl);
3392 pop_pvtbl(p, $p_pvtbl);
3393 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3394 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body), &@$, &NULL_LOC, &NULL_LOC);
3395 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3396 }
3397 | arg keyword_in
3398 {
3399 value_expr($arg);
3400 }
3401 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3402 p_top_expr_body[body]
3403 {
3404 pop_pktbl(p, $p_pktbl);
3405 pop_pvtbl(p, $p_pvtbl);
3406 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3407 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body), &@$, &NULL_LOC, &NULL_LOC);
3408 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3409 }
3410 | arg %prec tLBRACE_ARG
3411 ;
3412
3413def_name : fname
3414 {
3415 numparam_name(p, $fname);
3416 local_push(p, 0);
3417 p->ctxt.in_def = 1;
3418 p->ctxt.in_rescue = before_rescue;
3419 p->ctxt.cant_return = 0;
3420 $$ = $fname;
3421 }
3422 ;
3423
3424defn_head : k_def def_name
3425 {
3426 $$ = def_head_save(p, $k_def);
3427 $$->nd_mid = $def_name;
3428 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3429 /*% ripper: $:def_name %*/
3430 }
3431 ;
3432
3433defs_head : k_def singleton dot_or_colon
3434 {
3435 SET_LEX_STATE(EXPR_FNAME);
3436 }
3437 def_name
3438 {
3439 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3440 $$ = def_head_save(p, $k_def);
3441 $$->nd_mid = $def_name;
3442 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3443 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3444 }
3445 ;
3446
3447expr_value : value_expr(expr)
3448 | error
3449 {
3450 $$ = NEW_ERROR(&@$);
3451 }
3452 ;
3453
3454expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3455 {
3456 $$ = $2;
3457 /*% ripper: $:2 %*/
3458 }
3459 ;
3460
3461command_call : command
3462 | block_command
3463 ;
3464
3465command_call_value : value_expr(command_call)
3466 ;
3467
3468block_command : block_call
3469 | block_call call_op2 operation2 command_args
3470 {
3471 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3472 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3473 }
3474 ;
3475
3476cmd_brace_block : tLBRACE_ARG brace_body '}'
3477 {
3478 $$ = $2;
3479 set_embraced_location($$, &@1, &@3);
3480 /*% ripper: $:2 %*/
3481 }
3482 ;
3483
3484fcall : operation
3485 {
3486 $$ = NEW_FCALL($1, 0, &@$);
3487 /*% ripper: $:1 %*/
3488 }
3489 ;
3490
3491command : fcall command_args %prec tLOWEST
3492 {
3493 $1->nd_args = $2;
3494 nd_set_last_loc($1, @2.end_pos);
3495 $$ = (NODE *)$1;
3496 /*% ripper: command!($:1, $:2) %*/
3497 }
3498 | fcall command_args cmd_brace_block
3499 {
3500 block_dup_check(p, $2, $3);
3501 $1->nd_args = $2;
3502 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3503 fixpos($$, RNODE($1));
3504 nd_set_last_loc($1, @2.end_pos);
3505 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3506 }
3507 | primary_value call_op operation2 command_args %prec tLOWEST
3508 {
3509 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3510 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3511 }
3512 | primary_value call_op operation2 command_args cmd_brace_block
3513 {
3514 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3515 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3516 }
3517 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3518 {
3519 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3520 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3521 }
3522 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3523 {
3524 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3525 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3526 }
3527 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3528 {
3529 set_embraced_location($5, &@4, &@6);
3530 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3531 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3532 }
3533 | keyword_super command_args
3534 {
3535 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3536 fixpos($$, $2);
3537 /*% ripper: super!($:2) %*/
3538 }
3539 | k_yield command_args
3540 {
3541 $$ = new_yield(p, $2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3542 fixpos($$, $2);
3543 /*% ripper: yield!($:2) %*/
3544 }
3545 | k_return call_args
3546 {
3547 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3548 /*% ripper: return!($:2) %*/
3549 }
3550 | keyword_break call_args
3551 {
3552 NODE *args = 0;
3553 args = ret_args(p, $2);
3554 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3555 /*% ripper: break!($:2) %*/
3556 }
3557 | keyword_next call_args
3558 {
3559 NODE *args = 0;
3560 args = ret_args(p, $2);
3561 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3562 /*% ripper: next!($:2) %*/
3563 }
3564 ;
3565
3566mlhs : mlhs_basic
3567 | tLPAREN mlhs_inner rparen
3568 {
3569 $$ = $2;
3570 /*% ripper: mlhs_paren!($:2) %*/
3571 }
3572 ;
3573
3574mlhs_inner : mlhs_basic
3575 | tLPAREN mlhs_inner rparen
3576 {
3577 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3578 /*% ripper: mlhs_paren!($:2) %*/
3579 }
3580 ;
3581
3582mlhs_basic : mlhs_head
3583 {
3584 $$ = NEW_MASGN($1, 0, &@$);
3585 /*% ripper: $:1 %*/
3586 }
3587 | mlhs_head mlhs_item
3588 {
3589 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3590 /*% ripper: mlhs_add!($:1, $:2) %*/
3591 }
3592 | mlhs_head tSTAR mlhs_node
3593 {
3594 $$ = NEW_MASGN($1, $3, &@$);
3595 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3596 }
3597 | mlhs_head tSTAR mlhs_node ',' mlhs_post
3598 {
3599 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3600 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3601 }
3602 | mlhs_head tSTAR
3603 {
3604 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3605 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3606 }
3607 | mlhs_head tSTAR ',' mlhs_post
3608 {
3609 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3610 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3611 }
3612 | tSTAR mlhs_node
3613 {
3614 $$ = NEW_MASGN(0, $2, &@$);
3615 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3616 }
3617 | tSTAR mlhs_node ',' mlhs_post
3618 {
3619 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3620 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3621 }
3622 | tSTAR
3623 {
3624 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3625 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3626 }
3627 | tSTAR ',' mlhs_post
3628 {
3629 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3630 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3631 }
3632 ;
3633
3634mlhs_item : mlhs_node
3635 | tLPAREN mlhs_inner rparen
3636 {
3637 $$ = (NODE *)$2;
3638 /*% ripper: mlhs_paren!($:2) %*/
3639 }
3640 ;
3641
3642mlhs_head : mlhs_item ','
3643 {
3644 $$ = NEW_LIST($1, &@1);
3645 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3646 }
3647 | mlhs_head mlhs_item ','
3648 {
3649 $$ = list_append(p, $1, $2);
3650 /*% ripper: mlhs_add!($:1, $:2) %*/
3651 }
3652 ;
3653
3654mlhs_post : mlhs_item
3655 {
3656 $$ = NEW_LIST($1, &@$);
3657 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3658 }
3659 | mlhs_post ',' mlhs_item
3660 {
3661 $$ = list_append(p, $1, $3);
3662 /*% ripper: mlhs_add!($:1, $:3) %*/
3663 }
3664 ;
3665
3666mlhs_node : user_or_keyword_variable
3667 {
3668 /*% ripper: var_field!($:1) %*/
3669 $$ = assignable(p, $1, 0, &@$);
3670 }
3671 | primary_value '[' opt_call_args rbracket
3672 {
3673 $$ = aryset(p, $1, $3, &@$);
3674 /*% ripper: aref_field!($:1, $:3) %*/
3675 }
3676 | primary_value call_op ident_or_const
3677 {
3678 anddot_multiple_assignment_check(p, &@2, $2);
3679 $$ = attrset(p, $1, $2, $3, &@$);
3680 /*% ripper: field!($:1, $:2, $:3) %*/
3681 }
3682 | primary_value tCOLON2 tIDENTIFIER
3683 {
3684 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3685 /*% ripper: const_path_field!($:1, $:3) %*/
3686 }
3687 | primary_value tCOLON2 tCONSTANT
3688 {
3689 /*% ripper: const_path_field!($:1, $:3) %*/
3690 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3691 }
3692 | tCOLON3 tCONSTANT
3693 {
3694 /*% ripper: top_const_field!($:2) %*/
3695 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3696 }
3697 | backref
3698 {
3699 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3700 $$ = NEW_ERROR(&@$);
3701 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3702 }
3703 ;
3704
3705lhs : user_or_keyword_variable
3706 {
3707 /*% ripper: var_field!($:1) %*/
3708 $$ = assignable(p, $1, 0, &@$);
3709 }
3710 | primary_value '[' opt_call_args rbracket
3711 {
3712 $$ = aryset(p, $1, $3, &@$);
3713 /*% ripper: aref_field!($:1, $:3) %*/
3714 }
3715 | primary_value call_op tIDENTIFIER
3716 {
3717 $$ = attrset(p, $1, $2, $3, &@$);
3718 /*% ripper: field!($:1, $:2, $:3) %*/
3719 }
3720 | primary_value tCOLON2 tIDENTIFIER
3721 {
3722 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3723 /*% ripper: field!($:1, $:2, $:3) %*/
3724 }
3725 | primary_value call_op tCONSTANT
3726 {
3727 $$ = attrset(p, $1, $2, $3, &@$);
3728 /*% ripper: field!($:1, $:2, $:3) %*/
3729 }
3730 | primary_value tCOLON2 tCONSTANT
3731 {
3732 /*% ripper: const_path_field!($:1, $:3) %*/
3733 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3734 }
3735 | tCOLON3 tCONSTANT
3736 {
3737 /*% ripper: top_const_field!($:2) %*/
3738 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3739 }
3740 | backref
3741 {
3742 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3743 $$ = NEW_ERROR(&@$);
3744 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3745 }
3746 ;
3747
3748cname : tIDENTIFIER
3749 {
3750 static const char mesg[] = "class/module name must be CONSTANT";
3751 /*%%%*/
3752 yyerror1(&@1, mesg);
3753 /*% %*/
3754 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3755 }
3756 | tCONSTANT
3757 ;
3758
3759cpath : tCOLON3 cname
3760 {
3761 $$ = NEW_COLON3($2, &@$);
3762 /*% ripper: top_const_ref!($:2) %*/
3763 }
3764 | cname
3765 {
3766 $$ = NEW_COLON2(0, $1, &@$);
3767 /*% ripper: const_ref!($:1) %*/
3768 }
3769 | primary_value tCOLON2 cname
3770 {
3771 $$ = NEW_COLON2($1, $3, &@$);
3772 /*% ripper: const_path_ref!($:1, $:3) %*/
3773 }
3774 ;
3775
3776fname : inline_operation
3777 | op
3778 {
3779 SET_LEX_STATE(EXPR_ENDFN);
3780 $$ = $1;
3781 }
3782 | reswords
3783 ;
3784
3785fitem : fname
3786 {
3787 $$ = NEW_SYM(rb_id2str($1), &@$);
3788 /*% ripper: symbol_literal!($:1) %*/
3789 }
3790 | symbol
3791 ;
3792
3793undef_list : fitem
3794 {
3795 $$ = NEW_UNDEF($1, &@$);
3796 /*% ripper: rb_ary_new3(1, $:1) %*/
3797 }
3798 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3799 {
3800 nd_set_last_loc($1, @4.end_pos);
3801 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3802 /*% ripper: rb_ary_push($:1, $:4) %*/
3803 }
3804 ;
3805
3806op : '|' { $$ = '|'; }
3807 | '^' { $$ = '^'; }
3808 | '&' { $$ = '&'; }
3809 | tCMP { $$ = tCMP; }
3810 | tEQ { $$ = tEQ; }
3811 | tEQQ { $$ = tEQQ; }
3812 | tMATCH { $$ = tMATCH; }
3813 | tNMATCH { $$ = tNMATCH; }
3814 | '>' { $$ = '>'; }
3815 | tGEQ { $$ = tGEQ; }
3816 | '<' { $$ = '<'; }
3817 | tLEQ { $$ = tLEQ; }
3818 | tNEQ { $$ = tNEQ; }
3819 | tLSHFT { $$ = tLSHFT; }
3820 | tRSHFT { $$ = tRSHFT; }
3821 | '+' { $$ = '+'; }
3822 | '-' { $$ = '-'; }
3823 | '*' { $$ = '*'; }
3824 | tSTAR { $$ = '*'; }
3825 | '/' { $$ = '/'; }
3826 | '%' { $$ = '%'; }
3827 | tPOW { $$ = tPOW; }
3828 | tDSTAR { $$ = tDSTAR; }
3829 | '!' { $$ = '!'; }
3830 | '~' { $$ = '~'; }
3831 | tUPLUS { $$ = tUPLUS; }
3832 | tUMINUS { $$ = tUMINUS; }
3833 | tAREF { $$ = tAREF; }
3834 | tASET { $$ = tASET; }
3835 | '`' { $$ = '`'; }
3836 ;
3837
3838reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3839 | keyword_BEGIN | keyword_END
3840 | keyword_alias | keyword_and | keyword_begin
3841 | keyword_break | keyword_case | keyword_class | keyword_def
3842 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3843 | keyword_end | keyword_ensure | keyword_false
3844 | keyword_for | keyword_in | keyword_module | keyword_next
3845 | keyword_nil | keyword_not | keyword_or | keyword_redo
3846 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3847 | keyword_super | keyword_then | keyword_true | keyword_undef
3848 | keyword_when | keyword_yield | keyword_if | keyword_unless
3849 | keyword_while | keyword_until
3850 ;
3851
3852arg : asgn(lhs, arg_rhs)
3853 | op_asgn(arg_rhs)
3854 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3855 {
3856 YYLTYPE loc = code_loc_gen(&@1, &@2);
3857 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
3858 /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
3859 }
3860 | arg tDOT2 arg
3861 {
3862 value_expr($1);
3863 value_expr($3);
3864 $$ = NEW_DOT2($1, $3, &@$, &@2);
3865 /*% ripper: dot2!($:1, $:3) %*/
3866 }
3867 | arg tDOT3 arg
3868 {
3869 value_expr($1);
3870 value_expr($3);
3871 $$ = NEW_DOT3($1, $3, &@$, &@2);
3872 /*% ripper: dot3!($:1, $:3) %*/
3873 }
3874 | arg tDOT2
3875 {
3876 value_expr($1);
3877 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3878 /*% ripper: dot2!($:1, Qnil) %*/
3879 }
3880 | arg tDOT3
3881 {
3882 value_expr($1);
3883 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3884 /*% ripper: dot3!($:1, Qnil) %*/
3885 }
3886 | tBDOT2 arg
3887 {
3888 value_expr($2);
3889 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3890 /*% ripper: dot2!(Qnil, $:2) %*/
3891 }
3892 | tBDOT3 arg
3893 {
3894 value_expr($2);
3895 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3896 /*% ripper: dot3!(Qnil, $:2) %*/
3897 }
3898 | arg '+' arg
3899 {
3900 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3901 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3902 }
3903 | arg '-' arg
3904 {
3905 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3906 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3907 }
3908 | arg '*' arg
3909 {
3910 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3911 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3912 }
3913 | arg '/' arg
3914 {
3915 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3916 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3917 }
3918 | arg '%' arg
3919 {
3920 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3921 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3922 }
3923 | arg tPOW arg
3924 {
3925 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3926 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3927 }
3928 | tUMINUS_NUM simple_numeric tPOW arg
3929 {
3930 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3931 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3932 }
3933 | tUPLUS arg
3934 {
3935 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3936 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3937 }
3938 | tUMINUS arg
3939 {
3940 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3941 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3942 }
3943 | arg '|' arg
3944 {
3945 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3946 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3947 }
3948 | arg '^' arg
3949 {
3950 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3951 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3952 }
3953 | arg '&' arg
3954 {
3955 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3956 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
3957 }
3958 | arg tCMP arg
3959 {
3960 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3961 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
3962 }
3963 | rel_expr %prec tCMP
3964 | arg tEQ arg
3965 {
3966 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3967 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
3968 }
3969 | arg tEQQ arg
3970 {
3971 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
3972 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
3973 }
3974 | arg tNEQ arg
3975 {
3976 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
3977 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
3978 }
3979 | arg tMATCH arg
3980 {
3981 $$ = match_op(p, $1, $3, &@2, &@$);
3982 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
3983 }
3984 | arg tNMATCH arg
3985 {
3986 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
3987 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
3988 }
3989 | '!' arg
3990 {
3991 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3992 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
3993 }
3994 | '~' arg
3995 {
3996 $$ = call_uni_op(p, $2, '~', &@1, &@$);
3997 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
3998 }
3999 | arg tLSHFT arg
4000 {
4001 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4002 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4003 }
4004 | arg tRSHFT arg
4005 {
4006 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4007 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4008 }
4009 | arg tANDOP arg
4010 {
4011 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4012 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4013 }
4014 | arg tOROP arg
4015 {
4016 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4017 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4018 }
4019 | keyword_defined '\n'? begin_defined arg
4020 {
4021 p->ctxt.in_defined = $3.in_defined;
4022 $$ = new_defined(p, $4, &@$);
4023 /*% ripper: defined!($:4) %*/
4024 }
4025 | arg '?' arg '\n'? ':' arg
4026 {
4027 value_expr($1);
4028 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4029 fixpos($$, $1);
4030 /*% ripper: ifop!($:1, $:3, $:6) %*/
4031 }
4032 | def_endless_method(endless_arg)
4033 | primary
4034 ;
4035
4036endless_arg : arg %prec modifier_rescue
4037 | endless_arg modifier_rescue after_rescue arg
4038 {
4039 p->ctxt.in_rescue = $3.in_rescue;
4040 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4041 /*% ripper: rescue_mod!($:1, $:4) %*/
4042 }
4043 | keyword_not '\n'? endless_arg
4044 {
4045 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4046 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4047 }
4048 ;
4049
4050relop : '>' {$$ = '>';}
4051 | '<' {$$ = '<';}
4052 | tGEQ {$$ = idGE;}
4053 | tLEQ {$$ = idLE;}
4054 ;
4055
4056rel_expr : arg relop arg %prec '>'
4057 {
4058 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4059 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4060 }
4061 | rel_expr relop arg %prec '>'
4062 {
4063 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4064 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4065 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4066 }
4067 ;
4068
4069lex_ctxt : none
4070 {
4071 $$ = p->ctxt;
4072 }
4073 ;
4074
4075begin_defined : lex_ctxt
4076 {
4077 p->ctxt.in_defined = 1;
4078 $$ = $1;
4079 }
4080 ;
4081
4082after_rescue : lex_ctxt
4083 {
4084 p->ctxt.in_rescue = after_rescue;
4085 $$ = $1;
4086 }
4087 ;
4088
4089arg_value : value_expr(arg)
4090 ;
4091
4092aref_args : none
4093 | args trailer
4094 | args ',' assocs trailer
4095 {
4096 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4097 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4098 }
4099 | assocs trailer
4100 {
4101 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4102 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4103 }
4104 ;
4105
4106arg_rhs : arg %prec tOP_ASGN
4107 {
4108 value_expr($1);
4109 $$ = $1;
4110 }
4111 | arg modifier_rescue after_rescue arg
4112 {
4113 p->ctxt.in_rescue = $3.in_rescue;
4114 value_expr($1);
4115 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4116 /*% ripper: rescue_mod!($:1, $:4) %*/
4117 }
4118 ;
4119
4120paren_args : '(' opt_call_args rparen
4121 {
4122 $$ = $2;
4123 /*% ripper: arg_paren!($:2) %*/
4124 }
4125 | '(' args ',' args_forward rparen
4126 {
4127 if (!check_forwarding_args(p)) {
4128 $$ = 0;
4129 }
4130 else {
4131 $$ = new_args_forward_call(p, $2, &@4, &@$);
4132 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4133 }
4134 }
4135 | '(' args_forward rparen
4136 {
4137 if (!check_forwarding_args(p)) {
4138 $$ = 0;
4139 }
4140 else {
4141 $$ = new_args_forward_call(p, 0, &@2, &@$);
4142 /*% ripper: arg_paren!($:2) %*/
4143 }
4144 }
4145 ;
4146
4147opt_paren_args : none
4148 | paren_args
4149 {
4150 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4151 }
4152 ;
4153
4154opt_call_args : none
4155 | call_args
4156 | args ','
4157 | args ',' assocs ','
4158 {
4159 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4160 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4161 }
4162 | assocs ','
4163 {
4164 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4165 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4166 }
4167 ;
4168
4169call_args : value_expr(command)
4170 {
4171 $$ = NEW_LIST($1, &@$);
4172 /*% ripper: args_add!(args_new!, $:1) %*/
4173 }
4174 | args opt_block_arg
4175 {
4176 $$ = arg_blk_pass($1, $2);
4177 /*% ripper: args_add_block!($:1, $:2) %*/
4178 }
4179 | assocs opt_block_arg
4180 {
4181 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4182 $$ = arg_blk_pass($$, $2);
4183 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4184 }
4185 | args ',' assocs opt_block_arg
4186 {
4187 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4188 $$ = arg_blk_pass($$, $4);
4189 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4190 }
4191 | block_arg
4192 /*% ripper: args_add_block!(args_new!, $:1) %*/
4193 ;
4194
4195command_args : {
4196 /* If call_args starts with a open paren '(' or '[',
4197 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4198 * but the push must be done after CMDARG_PUSH(1).
4199 * So this code makes them consistent by first cancelling
4200 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4201 * and finally redoing CMDARG_PUSH(0).
4202 */
4203 int lookahead = 0;
4204 switch (yychar) {
4205 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4206 lookahead = 1;
4207 }
4208 if (lookahead) CMDARG_POP();
4209 CMDARG_PUSH(1);
4210 if (lookahead) CMDARG_PUSH(0);
4211 }
4212 call_args
4213 {
4214 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4215 * but the push must be done after CMDARG_POP() in the parser.
4216 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4217 * CMDARG_POP() to pop 1 pushed by command_args,
4218 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4219 */
4220 int lookahead = 0;
4221 switch (yychar) {
4222 case tLBRACE_ARG:
4223 lookahead = 1;
4224 }
4225 if (lookahead) CMDARG_POP();
4226 CMDARG_POP();
4227 if (lookahead) CMDARG_PUSH(0);
4228 $$ = $2;
4229 /*% ripper: $:2 %*/
4230 }
4231 ;
4232
4233block_arg : tAMPER arg_value
4234 {
4235 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4236 /*% ripper: $:2 %*/
4237 }
4238 | tAMPER
4239 {
4240 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4241 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4242 /*% ripper: Qnil %*/
4243 }
4244 ;
4245
4246opt_block_arg : ',' block_arg
4247 {
4248 $$ = $2;
4249 /*% ripper: $:2 %*/
4250 }
4251 | none
4252 {
4253 $$ = 0;
4254 /*% ripper: Qfalse %*/
4255 }
4256 ;
4257
4258/* value */
4259args : arg_value
4260 {
4261 $$ = NEW_LIST($arg_value, &@$);
4262 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4263 }
4264 | arg_splat
4265 {
4266 $$ = $arg_splat;
4267 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4268 }
4269 | args[non_last_args] ',' arg_value
4270 {
4271 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4272 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4273 }
4274 | args[non_last_args] ',' arg_splat
4275 {
4276 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4277 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4278 }
4279 ;
4280
4281/* value */
4282arg_splat : tSTAR arg_value
4283 {
4284 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4285 /*% ripper: $:arg_value %*/
4286 }
4287 | tSTAR /* none */
4288 {
4289 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4290 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4291 /*% ripper: Qnil %*/
4292 }
4293 ;
4294
4295/* value */
4296mrhs_arg : mrhs
4297 | arg_value
4298 ;
4299
4300/* value */
4301mrhs : args ',' arg_value
4302 {
4303 $$ = last_arg_append(p, $args, $arg_value, &@$);
4304 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4305 }
4306 | args ',' tSTAR arg_value
4307 {
4308 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4309 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4310 }
4311 | tSTAR arg_value
4312 {
4313 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4314 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4315 }
4316 ;
4317
4318%rule %inline inline_primary
4319 : literal
4320 | strings
4321 | xstring
4322 | regexp
4323 | words
4324 | qwords
4325 | symbols
4326 | qsymbols
4327 ;
4328
4329primary : inline_primary
4330 | var_ref
4331 | backref
4332 | tFID
4333 {
4334 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
4335 /*% ripper: method_add_arg!(fcall!($:1), args_new!) %*/
4336 }
4337 | k_begin
4338 {
4339 CMDARG_PUSH(0);
4340 }
4341 bodystmt
4342 k_end
4343 {
4344 CMDARG_POP();
4345 set_line_body($3, @1.end_pos.lineno);
4346 $$ = NEW_BEGIN($3, &@$);
4347 nd_set_line($$, @1.end_pos.lineno);
4348 /*% ripper: begin!($:3) %*/
4349 }
4350 | tLPAREN_ARG compstmt(stmts) {SET_LEX_STATE(EXPR_ENDARG);} ')'
4351 {
4352 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4353 $$ = $2;
4354 /*% ripper: paren!($:2) %*/
4355 }
4356 | tLPAREN compstmt(stmts) ')'
4357 {
4358 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4359 $$ = NEW_BLOCK($2, &@$);
4360 /*% ripper: paren!($:2) %*/
4361 }
4362 | primary_value tCOLON2 tCONSTANT
4363 {
4364 $$ = NEW_COLON2($1, $3, &@$);
4365 /*% ripper: const_path_ref!($:1, $:3) %*/
4366 }
4367 | tCOLON3 tCONSTANT
4368 {
4369 $$ = NEW_COLON3($2, &@$);
4370 /*% ripper: top_const_ref!($:2) %*/
4371 }
4372 | tLBRACK aref_args ']'
4373 {
4374 $$ = make_list($2, &@$);
4375 /*% ripper: array!($:2) %*/
4376 }
4377 | tLBRACE assoc_list '}'
4378 {
4379 $$ = new_hash(p, $2, &@$);
4380 RNODE_HASH($$)->nd_brace = TRUE;
4381 /*% ripper: hash!($:2) %*/
4382 }
4383 | k_return
4384 {
4385 $$ = NEW_RETURN(0, &@$, &@1);
4386 /*% ripper: return0! %*/
4387 }
4388 | k_yield '(' call_args rparen
4389 {
4390 $$ = new_yield(p, $3, &@$, &@1, &@2, &@4);
4391 /*% ripper: yield!(paren!($:3)) %*/
4392 }
4393 | k_yield '(' rparen
4394 {
4395 $$ = NEW_YIELD(0, &@$, &@1, &@2, &@3);
4396 /*% ripper: yield!(paren!(args_new!)) %*/
4397 }
4398 | k_yield
4399 {
4400 $$ = NEW_YIELD(0, &@$, &@1, &NULL_LOC, &NULL_LOC);
4401 /*% ripper: yield0! %*/
4402 }
4403 | keyword_defined '\n'? '(' begin_defined expr rparen
4404 {
4405 p->ctxt.in_defined = $4.in_defined;
4406 $$ = new_defined(p, $5, &@$);
4407 /*% ripper: defined!($:5) %*/
4408 }
4409 | keyword_not '(' expr rparen
4410 {
4411 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4412 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4413 }
4414 | keyword_not '(' rparen
4415 {
4416 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
4417 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4418 }
4419 | fcall brace_block
4420 {
4421 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
4422 /*% ripper: method_add_block!(method_add_arg!(fcall!($:1), args_new!), $:2) %*/
4423 }
4424 | method_call
4425 | method_call brace_block
4426 {
4427 block_dup_check(p, get_nd_args(p, $1), $2);
4428 $$ = method_add_block(p, $1, $2, &@$);
4429 /*% ripper: method_add_block!($:1, $:2) %*/
4430 }
4431 | lambda
4432 | k_if expr_value then
4433 compstmt(stmts)
4434 if_tail
4435 k_end
4436 {
4437 if ($5 && nd_type_p($5, NODE_IF))
4438 RNODE_IF($5)->end_keyword_loc = @6;
4439
4440 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4441 fixpos($$, $2);
4442 /*% ripper: if!($:2, $:4, $:5) %*/
4443 }
4444 | k_unless expr_value then
4445 compstmt(stmts)
4446 opt_else
4447 k_end
4448 {
4449 $$ = new_unless(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4450 fixpos($$, $2);
4451 /*% ripper: unless!($:2, $:4, $:5) %*/
4452 }
4453 | k_while expr_value_do
4454 compstmt(stmts)
4455 k_end
4456 {
4457 restore_block_exit(p, $1);
4458 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4459 fixpos($$, $2);
4460 /*% ripper: while!($:2, $:3) %*/
4461 }
4462 | k_until expr_value_do
4463 compstmt(stmts)
4464 k_end
4465 {
4466 restore_block_exit(p, $1);
4467 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4468 fixpos($$, $2);
4469 /*% ripper: until!($:2, $:3) %*/
4470 }
4471 | k_case expr_value terms?
4472 {
4473 $$ = p->case_labels;
4474 p->case_labels = CHECK_LITERAL_WHEN;
4475 }<labels>
4476 case_body
4477 k_end
4478 {
4479 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4480 p->case_labels = $4;
4481 $$ = NEW_CASE($2, $5, &@$, &@1, &@6);
4482 fixpos($$, $2);
4483 /*% ripper: case!($:2, $:5) %*/
4484 }
4485 | k_case terms?
4486 {
4487 $$ = p->case_labels;
4488 p->case_labels = 0;
4489 }<labels>
4490 case_body
4491 k_end
4492 {
4493 if (p->case_labels) st_free_table(p->case_labels);
4494 p->case_labels = $3;
4495 $$ = NEW_CASE2($4, &@$, &@1, &@5);
4496 /*% ripper: case!(Qnil, $:4) %*/
4497 }
4498 | k_case expr_value terms?
4499 p_case_body
4500 k_end
4501 {
4502 $$ = NEW_CASE3($2, $4, &@$, &@1, &@5);
4503 /*% ripper: case!($:2, $:4) %*/
4504 }
4505 | k_for for_var keyword_in
4506 {COND_PUSH(1);} expr_value do {COND_POP();}
4507 compstmt(stmts)
4508 k_end
4509 {
4510 restore_block_exit(p, $k_for);
4511 /*
4512 * for a, b, c in e
4513 * #=>
4514 * e.each{|*x| a, b, c = x}
4515 *
4516 * for a in e
4517 * #=>
4518 * e.each{|x| a, = x}
4519 */
4520 ID id = internal_id(p);
4521 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4522 rb_node_args_t *args;
4523 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4524 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4525 tbl->ids[0] = id; /* internal id */
4526
4527 switch (nd_type($for_var)) {
4528 case NODE_LASGN:
4529 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4530 set_nd_value(p, $for_var, internal_var);
4531 id = 0;
4532 m->nd_plen = 1;
4533 m->nd_next = $for_var;
4534 break;
4535 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4536 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4537 break;
4538 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4539 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);
4540 }
4541 /* {|*internal_id| <m> = internal_id; ... } */
4542 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@for_var), &@for_var);
4543 scope = NEW_SCOPE2(tbl, args, $compstmt, &@$);
4544 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4545 $$ = NEW_FOR($5, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4546 fixpos($$, $for_var);
4547 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4548 }
4549 | k_class cpath superclass
4550 {
4551 begin_definition("class", &@k_class, &@cpath);
4552 }
4553 bodystmt
4554 k_end
4555 {
4556 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$);
4557 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4558 set_line_body($bodystmt, @superclass.end_pos.lineno);
4559 nd_set_line($$, @superclass.end_pos.lineno);
4560 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4561 local_pop(p);
4562 p->ctxt.in_class = $k_class.in_class;
4563 p->ctxt.cant_return = $k_class.cant_return;
4564 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4565 }
4566 | k_class tLSHFT expr_value
4567 {
4568 begin_definition("", &@k_class, &@tLSHFT);
4569 }
4570 term
4571 bodystmt
4572 k_end
4573 {
4574 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$);
4575 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4576 set_line_body($bodystmt, nd_line($expr_value));
4577 fixpos($$, $expr_value);
4578 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4579 local_pop(p);
4580 p->ctxt.in_def = $k_class.in_def;
4581 p->ctxt.in_class = $k_class.in_class;
4582 p->ctxt.cant_return = $k_class.cant_return;
4583 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4584 }
4585 | k_module cpath
4586 {
4587 begin_definition("module", &@k_module, &@cpath);
4588 }
4589 bodystmt
4590 k_end
4591 {
4592 $$ = NEW_MODULE($cpath, $bodystmt, &@$);
4593 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4594 set_line_body($bodystmt, @cpath.end_pos.lineno);
4595 nd_set_line($$, @cpath.end_pos.lineno);
4596 /*% ripper: module!($:cpath, $:bodystmt) %*/
4597 local_pop(p);
4598 p->ctxt.in_class = $k_module.in_class;
4599 p->ctxt.cant_return = $k_module.cant_return;
4600 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4601 }
4602 | defn_head[head]
4603 f_arglist[args]
4604 {
4605 push_end_expect_token_locations(p, &@head.beg_pos);
4606 }
4607 bodystmt
4608 k_end
4609 {
4610 restore_defun(p, $head);
4611 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4612 ($$ = $head->nd_def)->nd_loc = @$;
4613 RNODE_DEFN($$)->nd_defn = $bodystmt;
4614 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4615 local_pop(p);
4616 }
4617 | defs_head[head]
4618 f_arglist[args]
4619 {
4620 push_end_expect_token_locations(p, &@head.beg_pos);
4621 }
4622 bodystmt
4623 k_end
4624 {
4625 restore_defun(p, $head);
4626 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4627 ($$ = $head->nd_def)->nd_loc = @$;
4628 RNODE_DEFS($$)->nd_defn = $bodystmt;
4629 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4630 local_pop(p);
4631 }
4632 | keyword_break
4633 {
4634 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@1));
4635 /*% ripper: break!(args_new!) %*/
4636 }
4637 | keyword_next
4638 {
4639 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@1));
4640 /*% ripper: next!(args_new!) %*/
4641 }
4642 | keyword_redo
4643 {
4644 $$ = add_block_exit(p, NEW_REDO(&@$, &@1));
4645 /*% ripper: redo! %*/
4646 }
4647 | keyword_retry
4648 {
4649 if (!p->ctxt.in_defined) {
4650 switch (p->ctxt.in_rescue) {
4651 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4652 case after_rescue: /* ok */ break;
4653 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4654 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4655 }
4656 }
4657 $$ = NEW_RETRY(&@$);
4658 /*% ripper: retry! %*/
4659 }
4660 ;
4661
4662primary_value : value_expr(primary)
4663 ;
4664
4665k_begin : keyword_begin
4666 {
4667 token_info_push(p, "begin", &@$);
4668 push_end_expect_token_locations(p, &@1.beg_pos);
4669 }
4670 ;
4671
4672k_if : keyword_if
4673 {
4674 WARN_EOL("if");
4675 token_info_push(p, "if", &@$);
4676 if (p->token_info && p->token_info->nonspc &&
4677 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4678 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4679 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4680 beg += rb_strlen_lit("else");
4681 while (beg < tok && ISSPACE(*beg)) beg++;
4682 if (beg == tok) {
4683 p->token_info->nonspc = 0;
4684 }
4685 }
4686 push_end_expect_token_locations(p, &@1.beg_pos);
4687 }
4688 ;
4689
4690k_unless : keyword_unless
4691 {
4692 token_info_push(p, "unless", &@$);
4693 push_end_expect_token_locations(p, &@1.beg_pos);
4694 }
4695 ;
4696
4697k_while : keyword_while allow_exits
4698 {
4699 $$ = $allow_exits;
4700 token_info_push(p, "while", &@$);
4701 push_end_expect_token_locations(p, &@1.beg_pos);
4702 }
4703 ;
4704
4705k_until : keyword_until allow_exits
4706 {
4707 $$ = $allow_exits;
4708 token_info_push(p, "until", &@$);
4709 push_end_expect_token_locations(p, &@1.beg_pos);
4710 }
4711 ;
4712
4713k_case : keyword_case
4714 {
4715 token_info_push(p, "case", &@$);
4716 push_end_expect_token_locations(p, &@1.beg_pos);
4717 }
4718 ;
4719
4720k_for : keyword_for allow_exits
4721 {
4722 $$ = $allow_exits;
4723 token_info_push(p, "for", &@$);
4724 push_end_expect_token_locations(p, &@1.beg_pos);
4725 }
4726 ;
4727
4728k_class : keyword_class
4729 {
4730 token_info_push(p, "class", &@$);
4731 $$ = p->ctxt;
4732 p->ctxt.in_rescue = before_rescue;
4733 push_end_expect_token_locations(p, &@1.beg_pos);
4734 }
4735 ;
4736
4737k_module : keyword_module
4738 {
4739 token_info_push(p, "module", &@$);
4740 $$ = p->ctxt;
4741 p->ctxt.in_rescue = before_rescue;
4742 push_end_expect_token_locations(p, &@1.beg_pos);
4743 }
4744 ;
4745
4746k_def : keyword_def
4747 {
4748 token_info_push(p, "def", &@$);
4749 $$ = NEW_DEF_TEMP(&@$);
4750 p->ctxt.in_argdef = 1;
4751 }
4752 ;
4753
4754k_do : keyword_do
4755 {
4756 token_info_push(p, "do", &@$);
4757 push_end_expect_token_locations(p, &@1.beg_pos);
4758 }
4759 ;
4760
4761k_do_block : keyword_do_block
4762 {
4763 token_info_push(p, "do", &@$);
4764 push_end_expect_token_locations(p, &@1.beg_pos);
4765 }
4766 ;
4767
4768k_rescue : keyword_rescue
4769 {
4770 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4771 $$ = p->ctxt;
4772 p->ctxt.in_rescue = after_rescue;
4773 }
4774 ;
4775
4776k_ensure : keyword_ensure
4777 {
4778 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4779 $$ = p->ctxt;
4780 }
4781 ;
4782
4783k_when : keyword_when
4784 {
4785 token_info_warn(p, "when", p->token_info, 0, &@$);
4786 }
4787 ;
4788
4789k_else : keyword_else
4790 {
4791 token_info *ptinfo_beg = p->token_info;
4792 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4793 token_info_warn(p, "else", p->token_info, same, &@$);
4794 if (same) {
4795 token_info e;
4796 e.next = ptinfo_beg->next;
4797 e.token = "else";
4798 token_info_setup(&e, p->lex.pbeg, &@$);
4799 if (!e.nonspc) *ptinfo_beg = e;
4800 }
4801 }
4802 ;
4803
4804k_elsif : keyword_elsif
4805 {
4806 WARN_EOL("elsif");
4807 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4808 }
4809 ;
4810
4811k_end : keyword_end
4812 {
4813 token_info_pop(p, "end", &@$);
4814 pop_end_expect_token_locations(p);
4815 }
4816 | tDUMNY_END
4817 {
4818 compile_error(p, "syntax error, unexpected end-of-input");
4819 }
4820 ;
4821
4822k_return : keyword_return
4823 {
4824 if (p->ctxt.cant_return && !dyna_in_block(p))
4825 yyerror1(&@1, "Invalid return in class/module body");
4826 }
4827 ;
4828
4829k_yield : keyword_yield
4830 {
4831 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4832 yyerror1(&@1, "Invalid yield");
4833 }
4834 ;
4835
4836then : term
4837 | keyword_then
4838 | term keyword_then
4839 ;
4840
4841do : term
4842 | keyword_do_cond { $$ = keyword_do_cond; }
4843 ;
4844
4845if_tail : opt_else
4846 | k_elsif expr_value then
4847 compstmt(stmts)
4848 if_tail
4849 {
4850 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4851 fixpos($$, $2);
4852 /*% ripper: elsif!($:2, $:4, $:5) %*/
4853 }
4854 ;
4855
4856opt_else : none
4857 | k_else compstmt(stmts)
4858 {
4859 $$ = $2;
4860 /*% ripper: else!($:2) %*/
4861 }
4862 ;
4863
4864for_var : lhs
4865 | mlhs
4866 ;
4867
4868f_marg : f_norm_arg
4869 {
4870 $$ = assignable(p, $1, 0, &@$);
4871 mark_lvar_used(p, $$);
4872 }
4873 | tLPAREN f_margs rparen
4874 {
4875 $$ = (NODE *)$2;
4876 /*% ripper: mlhs_paren!($:2) %*/
4877 }
4878 ;
4879
4880f_marg_list : f_marg
4881 {
4882 $$ = NEW_LIST($1, &@$);
4883 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
4884 }
4885 | f_marg_list ',' f_marg
4886 {
4887 $$ = list_append(p, $1, $3);
4888 /*% ripper: mlhs_add!($:1, $:3) %*/
4889 }
4890 ;
4891
4892f_margs : f_marg_list
4893 {
4894 $$ = NEW_MASGN($1, 0, &@$);
4895 /*% ripper: $:1 %*/
4896 }
4897 | f_marg_list ',' f_rest_marg
4898 {
4899 $$ = NEW_MASGN($1, $3, &@$);
4900 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4901 }
4902 | f_marg_list ',' f_rest_marg ',' f_marg_list
4903 {
4904 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4905 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4906 }
4907 | f_rest_marg
4908 {
4909 $$ = NEW_MASGN(0, $1, &@$);
4910 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4911 }
4912 | f_rest_marg ',' f_marg_list
4913 {
4914 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4915 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4916 }
4917 ;
4918
4919f_rest_marg : tSTAR f_norm_arg
4920 {
4921 /*% ripper: $:2 %*/
4922 $$ = assignable(p, $2, 0, &@$);
4923 mark_lvar_used(p, $$);
4924 }
4925 | tSTAR
4926 {
4927 $$ = NODE_SPECIAL_NO_NAME_REST;
4928 /*% ripper: Qnil %*/
4929 }
4930 ;
4931
4932f_any_kwrest : f_kwrest
4933 | f_no_kwarg
4934 {
4935 $$ = idNil;
4936 /*% ripper: ID2VAL(idNil) %*/
4937 }
4938 ;
4939
4940f_eq : {p->ctxt.in_argdef = 0;} '=';
4941
4942block_args_tail : f_kwarg(primary_value) ',' f_kwrest opt_f_block_arg
4943 {
4944 $$ = new_args_tail(p, $1, $3, $4, &@3);
4945 /*% ripper: [$:1, $:3, $:4] %*/
4946 }
4947 | f_kwarg(primary_value) opt_f_block_arg
4948 {
4949 $$ = new_args_tail(p, $1, 0, $2, &@1);
4950 /*% ripper: [$:1, Qnil, $:2] %*/
4951 }
4952 | f_any_kwrest opt_f_block_arg
4953 {
4954 $$ = new_args_tail(p, 0, $1, $2, &@1);
4955 /*% ripper: [Qnil, $:1, $:2] %*/
4956 }
4957 | f_block_arg
4958 {
4959 $$ = new_args_tail(p, 0, 0, $1, &@1);
4960 /*% ripper: [Qnil, Qnil, $:1] %*/
4961 }
4962 ;
4963
4964excessed_comma : ','
4965 {
4966 /* magic number for rest_id in iseq_set_arguments() */
4967 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4968 /*% ripper: excessed_comma! %*/
4969 }
4970 ;
4971
4972block_param : f_arg ',' f_optarg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
4973 {
4974 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
4975 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
4976 }
4977 | f_arg ',' f_optarg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
4978 {
4979 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4980 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
4981 }
4982 | f_arg ',' f_optarg(primary_value) opt_args_tail(block_args_tail)
4983 {
4984 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
4985 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
4986 }
4987 | f_arg ',' f_optarg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
4988 {
4989 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
4990 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
4991 }
4992 | f_arg ',' f_rest_arg opt_args_tail(block_args_tail)
4993 {
4994 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
4995 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
4996 }
4997 | f_arg excessed_comma
4998 {
4999 $$ = new_args_tail(p, 0, 0, 0, &@2);
5000 $$ = new_args(p, $1, 0, $2, 0, $$, &@$);
5001 /*% ripper: params!($:1, Qnil, $:2, Qnil, Qnil, Qnil, Qnil) %*/
5002 }
5003 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5004 {
5005 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
5006 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
5007 }
5008 | f_arg opt_args_tail(block_args_tail)
5009 {
5010 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
5011 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
5012 }
5013 | f_optarg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5014 {
5015 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
5016 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
5017 }
5018 | f_optarg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5019 {
5020 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
5021 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
5022 }
5023 | f_optarg(primary_value) opt_args_tail(block_args_tail)
5024 {
5025 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
5026 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
5027 }
5028 | f_optarg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5029 {
5030 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
5031 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
5032 }
5033 | f_rest_arg opt_args_tail(block_args_tail)
5034 {
5035 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
5036 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
5037 }
5038 | f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5039 {
5040 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
5041 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
5042 }
5043 | block_args_tail
5044 {
5045 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
5046 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
5047 }
5048 ;
5049
5050opt_block_param : none
5051 | block_param_def
5052 {
5053 p->command_start = TRUE;
5054 }
5055 ;
5056
5057block_param_def : '|' opt_bv_decl '|'
5058 {
5059 p->max_numparam = ORDINAL_PARAM;
5060 p->ctxt.in_argdef = 0;
5061 $$ = 0;
5062 /*% ripper: block_var!(params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), $:2) %*/
5063 }
5064 | '|' block_param opt_bv_decl '|'
5065 {
5066 p->max_numparam = ORDINAL_PARAM;
5067 p->ctxt.in_argdef = 0;
5068 $$ = $2;
5069 /*% ripper: block_var!($:2, $:3) %*/
5070 }
5071 ;
5072
5073
5074opt_bv_decl : '\n'?
5075 {
5076 $$ = 0;
5077 /*% ripper: Qfalse %*/
5078 }
5079 | '\n'? ';' bv_decls '\n'?
5080 {
5081 $$ = 0;
5082 /*% ripper: $:3 %*/
5083 }
5084 ;
5085
5086bv_decls : bvar
5087 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5088 | bv_decls ',' bvar
5089 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5090 ;
5091
5092bvar : tIDENTIFIER
5093 {
5094 new_bv(p, $1);
5095 /*% ripper: $:1 %*/
5096 }
5097 | f_bad_arg
5098 {
5099 $$ = 0;
5100 }
5101 ;
5102
5103max_numparam : {
5104 $$ = p->max_numparam;
5105 p->max_numparam = 0;
5106 }
5107 ;
5108
5109numparam : {
5110 $$ = numparam_push(p);
5111 }
5112 ;
5113
5114it_id : {
5115 $$ = p->it_id;
5116 p->it_id = 0;
5117 }
5118 ;
5119
5120lambda : tLAMBDA[lpar]
5121 {
5122 token_info_push(p, "->", &@1);
5123 $$ = dyna_push(p);
5124 }[dyna]<vars>
5125 max_numparam numparam it_id allow_exits
5126 f_larglist[args]
5127 {
5128 CMDARG_PUSH(0);
5129 }
5130 lambda_body[body]
5131 {
5132 int max_numparam = p->max_numparam;
5133 ID it_id = p->it_id;
5134 p->lex.lpar_beg = $lpar;
5135 p->max_numparam = $max_numparam;
5136 p->it_id = $it_id;
5137 restore_block_exit(p, $allow_exits);
5138 CMDARG_POP();
5139 $args = args_with_numbered(p, $args, max_numparam, it_id);
5140 {
5141 YYLTYPE loc = code_loc_gen(&@args, &@body);
5142 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5143 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5144 nd_set_line($$, @args.end_pos.lineno);
5145 nd_set_first_loc($$, @1.beg_pos);
5146 xfree($body);
5147 }
5148 /*% ripper: lambda!($:args, $:body) %*/
5149 numparam_pop(p, $numparam);
5150 dyna_pop(p, $dyna);
5151 }
5152 ;
5153
5154f_larglist : '(' f_args opt_bv_decl ')'
5155 {
5156 p->ctxt.in_argdef = 0;
5157 $$ = $f_args;
5158 p->max_numparam = ORDINAL_PARAM;
5159 /*% ripper: paren!($:2) %*/
5160 }
5161 | f_args
5162 {
5163 p->ctxt.in_argdef = 0;
5164 if (!args_info_empty_p(&$1->nd_ainfo))
5165 p->max_numparam = ORDINAL_PARAM;
5166 $$ = $f_args;
5167 }
5168 ;
5169
5170lambda_body : tLAMBEG compstmt(stmts) '}'
5171 {
5172 token_info_pop(p, "}", &@3);
5173 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5174 /*% ripper: $:2 %*/
5175 }
5176 | keyword_do_LAMBDA
5177 {
5178 push_end_expect_token_locations(p, &@1.beg_pos);
5179 }
5180 bodystmt k_end
5181 {
5182 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5183 /*% ripper: $:3 %*/
5184 }
5185 ;
5186
5187do_block : k_do_block do_body k_end
5188 {
5189 $$ = $2;
5190 set_embraced_location($$, &@1, &@3);
5191 /*% ripper: $:2 %*/
5192 }
5193 ;
5194
5195block_call : command do_block
5196 {
5197 if (nd_type_p($1, NODE_YIELD)) {
5198 compile_error(p, "block given to yield");
5199 }
5200 else {
5201 block_dup_check(p, get_nd_args(p, $1), $2);
5202 }
5203 $$ = method_add_block(p, $1, $2, &@$);
5204 fixpos($$, $1);
5205 /*% ripper: method_add_block!($:1, $:2) %*/
5206 }
5207 | block_call call_op2 operation2 opt_paren_args
5208 {
5209 bool has_args = $4 != 0;
5210 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5211 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5212 /*% ripper: call!($:1, $:2, $:3) %*/
5213 if (has_args) {
5214 /*% ripper: method_add_arg!($:$, $:4) %*/
5215 }
5216 }
5217 | block_call call_op2 operation2 opt_paren_args brace_block
5218 {
5219 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5220 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5221 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
5222 if ($5) {
5223 /*% ripper: method_add_block!($:$, $:5) %*/
5224 }
5225 }
5226 | block_call call_op2 operation2 command_args do_block
5227 {
5228 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5229 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5230 }
5231 ;
5232
5233method_call : fcall paren_args
5234 {
5235 $1->nd_args = $2;
5236 $$ = (NODE *)$1;
5237 nd_set_last_loc($1, @2.end_pos);
5238 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5239 }
5240 | primary_value call_op operation2 opt_paren_args
5241 {
5242 bool has_args = $4 != 0;
5243 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5244 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5245 nd_set_line($$, @3.end_pos.lineno);
5246 /*% ripper: call!($:1, $:2, $:3) %*/
5247 if (has_args) {
5248 /*% ripper: method_add_arg!($:$, $:4) %*/
5249 }
5250 }
5251 | primary_value tCOLON2 operation2 paren_args
5252 {
5253 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5254 nd_set_line($$, @3.end_pos.lineno);
5255 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5256 }
5257 | primary_value tCOLON2 operation3
5258 {
5259 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5260 /*% ripper: call!($:1, $:2, $:3) %*/
5261 }
5262 | primary_value call_op paren_args
5263 {
5264 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5265 nd_set_line($$, @2.end_pos.lineno);
5266 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5267 }
5268 | primary_value tCOLON2 paren_args
5269 {
5270 $$ = new_qcall(p, idCOLON2, $1, idCall, $3, &@2, &@$);
5271 nd_set_line($$, @2.end_pos.lineno);
5272 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5273 }
5274 | keyword_super paren_args
5275 {
5276 rb_code_location_t lparen_loc = @2;
5277 rb_code_location_t rparen_loc = @2;
5278 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5279 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5280
5281 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5282 /*% ripper: super!($:2) %*/
5283 }
5284 | keyword_super
5285 {
5286 $$ = NEW_ZSUPER(&@$);
5287 /*% ripper: zsuper! %*/
5288 }
5289 | primary_value '[' opt_call_args rbracket
5290 {
5291 $$ = NEW_CALL($1, tAREF, $3, &@$);
5292 fixpos($$, $1);
5293 /*% ripper: aref!($:1, $:3) %*/
5294 }
5295 ;
5296
5297brace_block : '{' brace_body '}'
5298 {
5299 $$ = $2;
5300 set_embraced_location($$, &@1, &@3);
5301 /*% ripper: $:2 %*/
5302 }
5303 | k_do do_body k_end
5304 {
5305 $$ = $2;
5306 set_embraced_location($$, &@1, &@3);
5307 /*% ripper: $:2 %*/
5308 }
5309 ;
5310
5311brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5312 max_numparam numparam it_id allow_exits
5313 opt_block_param[args] compstmt(stmts)
5314 {
5315 int max_numparam = p->max_numparam;
5316 ID it_id = p->it_id;
5317 p->max_numparam = $max_numparam;
5318 p->it_id = $it_id;
5319 $args = args_with_numbered(p, $args, max_numparam, it_id);
5320 $$ = NEW_ITER($args, $compstmt, &@$);
5321 /*% ripper: brace_block!($:args, $:compstmt) %*/
5322 restore_block_exit(p, $allow_exits);
5323 numparam_pop(p, $numparam);
5324 dyna_pop(p, $dyna);
5325 }
5326 ;
5327
5328do_body : {
5329 $$ = dyna_push(p);
5330 CMDARG_PUSH(0);
5331 }[dyna]<vars>
5332 max_numparam numparam it_id allow_exits
5333 opt_block_param[args] bodystmt
5334 {
5335 int max_numparam = p->max_numparam;
5336 ID it_id = p->it_id;
5337 p->max_numparam = $max_numparam;
5338 p->it_id = $it_id;
5339 $args = args_with_numbered(p, $args, max_numparam, it_id);
5340 $$ = NEW_ITER($args, $bodystmt, &@$);
5341 /*% ripper: do_block!($:args, $:bodystmt) %*/
5342 CMDARG_POP();
5343 restore_block_exit(p, $allow_exits);
5344 numparam_pop(p, $numparam);
5345 dyna_pop(p, $dyna);
5346 }
5347 ;
5348
5349case_args : arg_value
5350 {
5351 check_literal_when(p, $arg_value, &@arg_value);
5352 $$ = NEW_LIST($arg_value, &@$);
5353 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5354 }
5355 | tSTAR arg_value
5356 {
5357 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5358 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5359 }
5360 | case_args[non_last_args] ',' arg_value
5361 {
5362 check_literal_when(p, $arg_value, &@arg_value);
5363 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5364 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5365 }
5366 | case_args[non_last_args] ',' tSTAR arg_value
5367 {
5368 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5369 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5370 }
5371 ;
5372
5373case_body : k_when case_args then
5374 compstmt(stmts)
5375 cases
5376 {
5377 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5378 fixpos($$, $2);
5379 /*% ripper: when!($:2, $:4, $:5) %*/
5380 }
5381 ;
5382
5383cases : opt_else
5384 | case_body
5385 ;
5386
5387p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5388p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5389
5390p_in_kwarg : {
5391 $$ = p->ctxt;
5392 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5393 p->command_start = FALSE;
5394 p->ctxt.in_kwarg = 1;
5395 }
5396 ;
5397
5398p_case_body : keyword_in
5399 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5400 p_top_expr[expr] then
5401 {
5402 pop_pktbl(p, $p_pktbl);
5403 pop_pvtbl(p, $p_pvtbl);
5404 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5405 }
5406 compstmt(stmts)
5407 p_cases[cases]
5408 {
5409 $$ = NEW_IN($expr, $compstmt, $cases, &@$);
5410 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5411 }
5412 ;
5413
5414p_cases : opt_else
5415 | p_case_body
5416 ;
5417
5418p_top_expr : p_top_expr_body
5419 | p_top_expr_body modifier_if expr_value
5420 {
5421 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5422 fixpos($$, $3);
5423 /*% ripper: if_mod!($:3, $:1) %*/
5424 }
5425 | p_top_expr_body modifier_unless expr_value
5426 {
5427 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5428 fixpos($$, $3);
5429 /*% ripper: unless_mod!($:3, $:1) %*/
5430 }
5431 ;
5432
5433p_top_expr_body : p_expr
5434 | p_expr ','
5435 {
5436 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5437 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5438 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5439 }
5440 | p_expr ',' p_args
5441 {
5442 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5443 nd_set_first_loc($$, @1.beg_pos);
5444 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5445 }
5446 | p_find
5447 {
5448 $$ = new_find_pattern(p, 0, $1, &@$);
5449 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5450 }
5451 | p_args_tail
5452 {
5453 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5454 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5455 }
5456 | p_kwargs
5457 {
5458 $$ = new_hash_pattern(p, 0, $1, &@$);
5459 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5460 }
5461 ;
5462
5463p_expr : p_as
5464 ;
5465
5466p_as : p_expr tASSOC p_variable
5467 {
5468 NODE *n = NEW_LIST($1, &@$);
5469 n = list_append(p, n, $3);
5470 $$ = new_hash(p, n, &@$);
5471 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5472 }
5473 | p_alt
5474 ;
5475
5476p_alt : p_alt '|' p_expr_basic
5477 {
5478 $$ = NEW_OR($1, $3, &@$, &@2);
5479 /*% ripper: binary!($:1, ID2VAL(idOr), $:3) %*/
5480 }
5481 | p_expr_basic
5482 ;
5483
5484p_lparen : '(' p_pktbl
5485 {
5486 $$ = $2;
5487 /*% ripper: $:2 %*/
5488 }
5489 ;
5490
5491p_lbracket : '[' p_pktbl
5492 {
5493 $$ = $2;
5494 /*% ripper: $:2 %*/
5495 }
5496 ;
5497
5498p_expr_basic : p_value
5499 | p_variable
5500 | p_const p_lparen[p_pktbl] p_args rparen
5501 {
5502 pop_pktbl(p, $p_pktbl);
5503 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5504 nd_set_first_loc($$, @p_const.beg_pos);
5505 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5506 }
5507 | p_const p_lparen[p_pktbl] p_find rparen
5508 {
5509 pop_pktbl(p, $p_pktbl);
5510 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5511 nd_set_first_loc($$, @p_const.beg_pos);
5512 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5513 }
5514 | p_const p_lparen[p_pktbl] p_kwargs rparen
5515 {
5516 pop_pktbl(p, $p_pktbl);
5517 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5518 nd_set_first_loc($$, @p_const.beg_pos);
5519 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5520 }
5521 | p_const '(' rparen
5522 {
5523 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5524 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5525 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5526 }
5527 | p_const p_lbracket[p_pktbl] p_args rbracket
5528 {
5529 pop_pktbl(p, $p_pktbl);
5530 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5531 nd_set_first_loc($$, @p_const.beg_pos);
5532 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5533 }
5534 | p_const p_lbracket[p_pktbl] p_find rbracket
5535 {
5536 pop_pktbl(p, $p_pktbl);
5537 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5538 nd_set_first_loc($$, @p_const.beg_pos);
5539 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5540 }
5541 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5542 {
5543 pop_pktbl(p, $p_pktbl);
5544 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5545 nd_set_first_loc($$, @p_const.beg_pos);
5546 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5547 }
5548 | p_const '[' rbracket
5549 {
5550 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5551 $$ = new_array_pattern(p, $1, 0, $$, &@$);
5552 /*% ripper: aryptn!($:1, Qnil, Qnil, Qnil) %*/
5553 }
5554 | tLBRACK p_args rbracket
5555 {
5556 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5557 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5558 }
5559 | tLBRACK p_find rbracket
5560 {
5561 $$ = new_find_pattern(p, 0, $p_find, &@$);
5562 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5563 }
5564 | tLBRACK rbracket
5565 {
5566 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5567 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5568 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5569 }
5570 | tLBRACE p_pktbl lex_ctxt[ctxt]
5571 {
5572 p->ctxt.in_kwarg = 0;
5573 }
5574 p_kwargs rbrace
5575 {
5576 pop_pktbl(p, $p_pktbl);
5577 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5578 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5579 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5580 }
5581 | tLBRACE rbrace
5582 {
5583 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5584 $$ = new_hash_pattern(p, 0, $$, &@$);
5585 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5586 }
5587 | tLPAREN p_pktbl p_expr rparen
5588 {
5589 pop_pktbl(p, $p_pktbl);
5590 $$ = $p_expr;
5591 /*% ripper: $:p_expr %*/
5592 }
5593 ;
5594
5595p_args : p_expr
5596 {
5597 NODE *pre_args = NEW_LIST($1, &@$);
5598 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5599 /*% ripper: [[$:1], Qnil, Qnil] %*/
5600 }
5601 | p_args_head
5602 {
5603 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5604 /*% ripper: [$:1, Qnil, Qnil] %*/
5605 }
5606 | p_args_head p_arg
5607 {
5608 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5609 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5610 }
5611 | p_args_head p_rest
5612 {
5613 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5614 /*% ripper: [$:1, $:2, Qnil] %*/
5615 }
5616 | p_args_head p_rest ',' p_args_post
5617 {
5618 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5619 /*% ripper: [$:1, $:2, $:4] %*/
5620 }
5621 | p_args_tail
5622 ;
5623
5624p_args_head : p_arg ','
5625 | p_args_head p_arg ','
5626 {
5627 $$ = list_concat($1, $2);
5628 /*% ripper: rb_ary_concat($:1, $:2) %*/
5629 }
5630 ;
5631
5632p_args_tail : p_rest
5633 {
5634 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5635 /*% ripper: [Qnil, $:1, Qnil] %*/
5636 }
5637 | p_rest ',' p_args_post
5638 {
5639 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5640 /*% ripper: [Qnil, $:1, $:3] %*/
5641 }
5642 ;
5643
5644p_find : p_rest ',' p_args_post ',' p_rest
5645 {
5646 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5647 /*% ripper: [$:1, $:3, $:5] %*/
5648 }
5649 ;
5650
5651
5652p_rest : tSTAR tIDENTIFIER
5653 {
5654 error_duplicate_pattern_variable(p, $2, &@2);
5655 /*% ripper: var_field!($:2) %*/
5656 $$ = assignable(p, $2, 0, &@$);
5657 }
5658 | tSTAR
5659 {
5660 $$ = 0;
5661 /*% ripper: var_field!(Qnil) %*/
5662 }
5663 ;
5664
5665p_args_post : p_arg
5666 | p_args_post ',' p_arg
5667 {
5668 $$ = list_concat($1, $3);
5669 /*% ripper: rb_ary_concat($:1, $:3) %*/
5670 }
5671 ;
5672
5673p_arg : p_expr
5674 {
5675 $$ = NEW_LIST($1, &@$);
5676 /*% ripper: [$:1] %*/
5677 }
5678 ;
5679
5680p_kwargs : p_kwarg ',' p_any_kwrest
5681 {
5682 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5683 /*% ripper: [$:1, $:3] %*/
5684 }
5685 | p_kwarg
5686 {
5687 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5688 /*% ripper: [$:1, Qnil] %*/
5689 }
5690 | p_kwarg ','
5691 {
5692 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5693 /*% ripper: [$:1, Qnil] %*/
5694 }
5695 | p_any_kwrest
5696 {
5697 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5698 /*% ripper: [[], $:1] %*/
5699 }
5700 ;
5701
5702p_kwarg : p_kw
5703 /*% ripper[brace]: [$:1] %*/
5704 | p_kwarg ',' p_kw
5705 {
5706 $$ = list_concat($1, $3);
5707 /*% ripper: rb_ary_push($:1, $:3) %*/
5708 }
5709 ;
5710
5711p_kw : p_kw_label p_expr
5712 {
5713 error_duplicate_pattern_key(p, $1, &@1);
5714 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5715 /*% ripper: [$:1, $:2] %*/
5716 }
5717 | p_kw_label
5718 {
5719 error_duplicate_pattern_key(p, $1, &@1);
5720 if ($1 && !is_local_id($1)) {
5721 yyerror1(&@1, "key must be valid as local variables");
5722 }
5723 error_duplicate_pattern_variable(p, $1, &@1);
5724 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5725 /*% ripper: [$:1, Qnil] %*/
5726 }
5727 ;
5728
5729p_kw_label : tLABEL
5730 | tSTRING_BEG string_contents tLABEL_END
5731 {
5732 YYLTYPE loc = code_loc_gen(&@1, &@3);
5733 if (!$2 || nd_type_p($2, NODE_STR)) {
5734 NODE *node = dsym_node(p, $2, &loc);
5735 $$ = rb_sym2id(rb_node_sym_string_val(node));
5736 }
5737 else {
5738 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5739 $$ = rb_intern_str(STR_NEW0());
5740 }
5741 /*% ripper: $:2 %*/
5742 }
5743 ;
5744
5745p_kwrest : kwrest_mark tIDENTIFIER
5746 {
5747 $$ = $2;
5748 /*% ripper: var_field!($:2) %*/
5749 }
5750 | kwrest_mark
5751 {
5752 $$ = 0;
5753 /*% ripper: Qnil %*/
5754 }
5755 ;
5756
5757p_kwnorest : kwrest_mark keyword_nil
5758 {
5759 $$ = 0;
5760 }
5761 ;
5762
5763p_any_kwrest : p_kwrest
5764 | p_kwnorest
5765 {
5766 $$ = idNil;
5767 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5768 }
5769 ;
5770
5771p_value : p_primitive
5772 | p_primitive_value tDOT2 p_primitive_value
5773 {
5774 $$ = NEW_DOT2($1, $3, &@$, &@2);
5775 /*% ripper: dot2!($:1, $:3) %*/
5776 }
5777 | p_primitive_value tDOT3 p_primitive_value
5778 {
5779 $$ = NEW_DOT3($1, $3, &@$, &@2);
5780 /*% ripper: dot3!($:1, $:3) %*/
5781 }
5782 | p_primitive_value tDOT2
5783 {
5784 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
5785 /*% ripper: dot2!($:1, Qnil) %*/
5786 }
5787 | p_primitive_value tDOT3
5788 {
5789 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
5790 /*% ripper: dot3!($:1, Qnil) %*/
5791 }
5792 | p_var_ref
5793 | p_expr_ref
5794 | p_const
5795 | tBDOT2 p_primitive_value
5796 {
5797 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
5798 /*% ripper: dot2!(Qnil, $:2) %*/
5799 }
5800 | tBDOT3 p_primitive_value
5801 {
5802 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
5803 /*% ripper: dot3!(Qnil, $:2) %*/
5804 }
5805 ;
5806
5807p_primitive : inline_primary
5808 | keyword_variable
5809 {
5810 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5811 /*% ripper: var_ref!($:1) %*/
5812 }
5813 | lambda
5814 ;
5815
5816p_primitive_value : value_expr(p_primitive)
5817 ;
5818
5819p_variable : tIDENTIFIER
5820 {
5821 error_duplicate_pattern_variable(p, $1, &@1);
5822 /*% ripper: var_field!($:1) %*/
5823 $$ = assignable(p, $1, 0, &@$);
5824 }
5825 ;
5826
5827p_var_ref : '^' tIDENTIFIER
5828 {
5829 NODE *n = gettable(p, $2, &@$);
5830 if (!n) {
5831 n = NEW_ERROR(&@$);
5832 }
5833 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5834 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5835 }
5836 $$ = n;
5837 /*% ripper: var_ref!($:2) %*/
5838 }
5839 | '^' nonlocal_var
5840 {
5841 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5842 /*% ripper: var_ref!($:2) %*/
5843 }
5844 ;
5845
5846p_expr_ref : '^' tLPAREN expr_value rparen
5847 {
5848 $$ = NEW_BLOCK($3, &@$);
5849 /*% ripper: begin!($:3) %*/
5850 }
5851 ;
5852
5853p_const : tCOLON3 cname
5854 {
5855 $$ = NEW_COLON3($2, &@$);
5856 /*% ripper: top_const_ref!($:2) %*/
5857 }
5858 | p_const tCOLON2 cname
5859 {
5860 $$ = NEW_COLON2($1, $3, &@$);
5861 /*% ripper: const_path_ref!($:1, $:3) %*/
5862 }
5863 | tCONSTANT
5864 {
5865 $$ = gettable(p, $1, &@$);
5866 /*% ripper: var_ref!($:1) %*/
5867 }
5868 ;
5869
5870opt_rescue : k_rescue exc_list exc_var then
5871 compstmt(stmts)
5872 opt_rescue
5873 {
5874 NODE *err = $3;
5875 if ($3) {
5876 err = NEW_ERRINFO(&@3);
5877 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5878 }
5879 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5880 if ($2) {
5881 fixpos($$, $2);
5882 }
5883 else if ($3) {
5884 fixpos($$, $3);
5885 }
5886 else {
5887 fixpos($$, $5);
5888 }
5889 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5890 }
5891 | none
5892 ;
5893
5894exc_list : arg_value
5895 {
5896 $$ = NEW_LIST($1, &@$);
5897 /*% ripper: rb_ary_new3(1, $:1) %*/
5898 }
5899 | mrhs
5900 {
5901 if (!($$ = splat_array($1))) $$ = $1;
5902 }
5903 | none
5904 ;
5905
5906exc_var : tASSOC lhs
5907 {
5908 $$ = $2;
5909 /*% ripper: $:2 %*/
5910 }
5911 | none
5912 ;
5913
5914opt_ensure : k_ensure stmts terms?
5915 {
5916 p->ctxt.in_rescue = $1.in_rescue;
5917 $$ = $2;
5918 void_expr(p, void_stmts(p, $$));
5919 /*% ripper: ensure!($:2) %*/
5920 }
5921 | none
5922 ;
5923
5924literal : numeric
5925 | symbol
5926 ;
5927
5928strings : string
5929 {
5930 NODE *node = $1;
5931 if (!node) {
5932 node = NEW_STR(STRING_NEW0(), &@$);
5933 }
5934 else {
5935 node = evstr2dstr(p, node);
5936 }
5937 $$ = node;
5938 /*% ripper: $:1 %*/
5939 }
5940 ;
5941
5942string : tCHAR
5943 | string1
5944 | string string1
5945 {
5946 $$ = literal_concat(p, $1, $2, &@$);
5947 /*% ripper: string_concat!($:1, $:2) %*/
5948 }
5949 ;
5950
5951string1 : tSTRING_BEG string_contents tSTRING_END
5952 {
5953 $$ = heredoc_dedent(p, $2);
5954 if ($$) nd_set_loc($$, &@$);
5955 /*% ripper: $:2 %*/
5956 if (p->heredoc_indent > 0) {
5957 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5958 p->heredoc_indent = 0;
5959 }
5960 /*% ripper: string_literal!($:$) %*/
5961 }
5962 ;
5963
5964xstring : tXSTRING_BEG xstring_contents tSTRING_END
5965 {
5966 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5967 /*% ripper: $:2 %*/
5968 if (p->heredoc_indent > 0) {
5969 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5970 p->heredoc_indent = 0;
5971 }
5972 /*% ripper: xstring_literal!($:$) %*/
5973 }
5974 ;
5975
5976regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5977 {
5978 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5979 /*% ripper: regexp_literal!($:2, $:3) %*/
5980 }
5981 ;
5982
5983words : words(tWORDS_BEG, word_list)
5984 ;
5985
5986word_list : /* none */
5987 {
5988 $$ = 0;
5989 /*% ripper: words_new! %*/
5990 }
5991 | word_list word ' '+
5992 {
5993 $$ = list_append(p, $1, evstr2dstr(p, $2));
5994 /*% ripper: words_add!($:1, $:2) %*/
5995 }
5996 ;
5997
5998word : string_content
5999 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
6000 | word string_content
6001 {
6002 $$ = literal_concat(p, $1, $2, &@$);
6003 /*% ripper: word_add!($:1, $:2) %*/
6004 }
6005 ;
6006
6007symbols : words(tSYMBOLS_BEG, symbol_list)
6008 ;
6009
6010symbol_list : /* none */
6011 {
6012 $$ = 0;
6013 /*% ripper: symbols_new! %*/
6014 }
6015 | symbol_list word ' '+
6016 {
6017 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
6018 /*% ripper: symbols_add!($:1, $:2) %*/
6019 }
6020 ;
6021
6022qwords : words(tQWORDS_BEG, qword_list)
6023 ;
6024
6025qsymbols : words(tQSYMBOLS_BEG, qsym_list)
6026 ;
6027
6028qword_list : /* none */
6029 {
6030 $$ = 0;
6031 /*% ripper: qwords_new! %*/
6032 }
6033 | qword_list tSTRING_CONTENT ' '+
6034 {
6035 $$ = list_append(p, $1, $2);
6036 /*% ripper: qwords_add!($:1, $:2) %*/
6037 }
6038 ;
6039
6040qsym_list : /* none */
6041 {
6042 $$ = 0;
6043 /*% ripper: qsymbols_new! %*/
6044 }
6045 | qsym_list tSTRING_CONTENT ' '+
6046 {
6047 $$ = symbol_append(p, $1, $2);
6048 /*% ripper: qsymbols_add!($:1, $:2) %*/
6049 }
6050 ;
6051
6052string_contents : /* none */
6053 {
6054 $$ = 0;
6055 /*% ripper: string_content! %*/
6056 }
6057 | string_contents string_content
6058 {
6059 $$ = literal_concat(p, $1, $2, &@$);
6060 /*% ripper: string_add!($:1, $:2) %*/
6061 }
6062 ;
6063
6064xstring_contents: /* none */
6065 {
6066 $$ = 0;
6067 /*% ripper: xstring_new! %*/
6068 }
6069 | xstring_contents string_content
6070 {
6071 $$ = literal_concat(p, $1, $2, &@$);
6072 /*% ripper: xstring_add!($:1, $:2) %*/
6073 }
6074 ;
6075
6076regexp_contents: /* none */
6077 {
6078 $$ = 0;
6079 /*% ripper: regexp_new! %*/
6080 }
6081 | regexp_contents string_content
6082 {
6083 NODE *head = $1, *tail = $2;
6084 if (!head) {
6085 $$ = tail;
6086 }
6087 else if (!tail) {
6088 $$ = head;
6089 }
6090 else {
6091 switch (nd_type(head)) {
6092 case NODE_STR:
6093 head = str2dstr(p, head);
6094 break;
6095 case NODE_DSTR:
6096 break;
6097 default:
6098 head = list_append(p, NEW_DSTR(0, &@$), head);
6099 break;
6100 }
6101 $$ = list_append(p, head, tail);
6102 }
6103 /*% ripper: regexp_add!($:1, $:2) %*/
6104 }
6105 ;
6106
6107string_content : tSTRING_CONTENT
6108 /*% ripper[brace]: $:1 %*/
6109 | tSTRING_DVAR
6110 {
6111 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6112 $$ = p->lex.strterm;
6113 p->lex.strterm = 0;
6114 SET_LEX_STATE(EXPR_BEG);
6115 }<strterm>
6116 string_dvar
6117 {
6118 p->lex.strterm = $2;
6119 $$ = NEW_EVSTR($3, &@$, &@1, &NULL_LOC);
6120 nd_set_line($$, @3.end_pos.lineno);
6121 /*% ripper: string_dvar!($:3) %*/
6122 }
6123 | tSTRING_DBEG[state]
6124 {
6125 CMDARG_PUSH(0);
6126 COND_PUSH(0);
6127 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6128 $$ = p->lex.strterm;
6129 p->lex.strterm = 0;
6130 SET_LEX_STATE(EXPR_BEG);
6131 }[term]<strterm>
6132 {
6133 $$ = p->lex.brace_nest;
6134 p->lex.brace_nest = 0;
6135 }[brace]<num>
6136 {
6137 $$ = p->heredoc_indent;
6138 p->heredoc_indent = 0;
6139 }[indent]<num>
6140 compstmt(stmts) string_dend
6141 {
6142 COND_POP();
6143 CMDARG_POP();
6144 p->lex.strterm = $term;
6145 SET_LEX_STATE($state);
6146 p->lex.brace_nest = $brace;
6147 p->heredoc_indent = $indent;
6148 p->heredoc_line_indent = -1;
6149 if ($compstmt) nd_unset_fl_newline($compstmt);
6150 $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend);
6151 /*% ripper: string_embexpr!($:compstmt) %*/
6152 }
6153 ;
6154
6155string_dend : tSTRING_DEND
6156 | END_OF_INPUT
6157 ;
6158
6159string_dvar : nonlocal_var
6160 {
6161 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6162 /*% ripper: var_ref!($:1) %*/
6163 }
6164 | backref
6165 ;
6166
6167symbol : ssym
6168 | dsym
6169 ;
6170
6171ssym : tSYMBEG sym
6172 {
6173 SET_LEX_STATE(EXPR_END);
6174 VALUE str = rb_id2str($2);
6175 /*
6176 * TODO:
6177 * set_yylval_noname sets invalid id to yylval.
6178 * This branch can be removed once yylval is changed to
6179 * hold lexed string.
6180 */
6181 if (!str) str = STR_NEW0();
6182 $$ = NEW_SYM(str, &@$);
6183 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6184 }
6185 ;
6186
6187sym : fname
6188 | nonlocal_var
6189 ;
6190
6191dsym : tSYMBEG string_contents tSTRING_END
6192 {
6193 SET_LEX_STATE(EXPR_END);
6194 $$ = dsym_node(p, $2, &@$);
6195 /*% ripper: dyna_symbol!($:2) %*/
6196 }
6197 ;
6198
6199numeric : simple_numeric
6200 | tUMINUS_NUM simple_numeric %prec tLOWEST
6201 {
6202 $$ = $2;
6203 negate_lit(p, $$);
6204 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6205 }
6206 ;
6207
6208simple_numeric : tINTEGER
6209 | tFLOAT
6210 | tRATIONAL
6211 | tIMAGINARY
6212 ;
6213
6214nonlocal_var : tIVAR
6215 | tGVAR
6216 | tCVAR
6217 ;
6218
6219user_variable : ident_or_const
6220 | nonlocal_var
6221 ;
6222
6223keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6224 | keyword_self {$$ = KWD2EID(self, $1);}
6225 | keyword_true {$$ = KWD2EID(true, $1);}
6226 | keyword_false {$$ = KWD2EID(false, $1);}
6227 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6228 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6229 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6230 ;
6231
6232var_ref : user_variable
6233 {
6234 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6235 if (ifdef_ripper(id_is_var(p, $1), false)) {
6236 /*% ripper: var_ref!($:1) %*/
6237 }
6238 else {
6239 /*% ripper: vcall!($:1) %*/
6240 }
6241 }
6242 | keyword_variable
6243 {
6244 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6245 /*% ripper: var_ref!($:1) %*/
6246 }
6247 ;
6248
6249var_lhs : user_or_keyword_variable
6250 {
6251 /*% ripper: var_field!($:1) %*/
6252 $$ = assignable(p, $1, 0, &@$);
6253 }
6254 ;
6255
6256backref : tNTH_REF
6257 | tBACK_REF
6258 ;
6259
6260superclass : '<'
6261 {
6262 SET_LEX_STATE(EXPR_BEG);
6263 p->command_start = TRUE;
6264 }
6265 expr_value term
6266 {
6267 $$ = $3;
6268 /*% ripper: $:3 %*/
6269 }
6270 | /* none */
6271 {
6272 $$ = 0;
6273 /*% ripper: Qnil %*/
6274 }
6275 ;
6276
6277f_opt_paren_args: f_paren_args
6278 | none
6279 {
6280 p->ctxt.in_argdef = 0;
6281 $$ = new_args_tail(p, 0, 0, 0, &@0);
6282 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6283 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6284 }
6285 ;
6286
6287f_paren_args : '(' f_args rparen
6288 {
6289 $$ = $2;
6290 /*% ripper: paren!($:2) %*/
6291 SET_LEX_STATE(EXPR_BEG);
6292 p->command_start = TRUE;
6293 p->ctxt.in_argdef = 0;
6294 }
6295 ;
6296
6297f_arglist : f_paren_args
6298 | {
6299 $$ = p->ctxt;
6300 p->ctxt.in_kwarg = 1;
6301 p->ctxt.in_argdef = 1;
6302 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6303 }<ctxt>
6304 f_args term
6305 {
6306 p->ctxt.in_kwarg = $1.in_kwarg;
6307 p->ctxt.in_argdef = 0;
6308 $$ = $2;
6309 SET_LEX_STATE(EXPR_BEG);
6310 p->command_start = TRUE;
6311 /*% ripper: $:2 %*/
6312 }
6313 ;
6314
6315args_tail : f_kwarg(arg_value) ',' f_kwrest opt_f_block_arg
6316 {
6317 $$ = new_args_tail(p, $1, $3, $4, &@3);
6318 /*% ripper: [$:1, $:3, $:4] %*/
6319 }
6320 | f_kwarg(arg_value) opt_f_block_arg
6321 {
6322 $$ = new_args_tail(p, $1, 0, $2, &@1);
6323 /*% ripper: [$:1, Qnil, $:2] %*/
6324 }
6325 | f_any_kwrest opt_f_block_arg
6326 {
6327 $$ = new_args_tail(p, 0, $1, $2, &@1);
6328 /*% ripper: [Qnil, $:1, $:2] %*/
6329 }
6330 | f_block_arg
6331 {
6332 $$ = new_args_tail(p, 0, 0, $1, &@1);
6333 /*% ripper: [Qnil, Qnil, $:1] %*/
6334 }
6335 | args_forward
6336 {
6337 ID fwd = $args_forward;
6338 if (lambda_beginning_p() ||
6339 (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest)) {
6340 yyerror0("unexpected ... in lambda argument");
6341 fwd = 0;
6342 }
6343 else {
6344 add_forwarding_args(p);
6345 }
6346 $$ = new_args_tail(p, 0, fwd, arg_FWD_BLOCK, &@1);
6347 $$->nd_ainfo.forwarding = 1;
6348 /*% ripper: [Qnil, $:1, Qnil] %*/
6349 }
6350 ;
6351
6352f_args : f_arg ',' f_optarg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6353 {
6354 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
6355 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
6356 }
6357 | f_arg ',' f_optarg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6358 {
6359 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6360 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
6361 }
6362 | f_arg ',' f_optarg(arg_value) opt_args_tail(args_tail)
6363 {
6364 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
6365 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
6366 }
6367 | f_arg ',' f_optarg(arg_value) ',' f_arg opt_args_tail(args_tail)
6368 {
6369 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
6370 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
6371 }
6372 | f_arg ',' f_rest_arg opt_args_tail(args_tail)
6373 {
6374 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
6375 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
6376 }
6377 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6378 {
6379 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
6380 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
6381 }
6382 | f_arg opt_args_tail(args_tail)
6383 {
6384 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
6385 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
6386 }
6387 | f_optarg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6388 {
6389 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
6390 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
6391 }
6392 | f_optarg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6393 {
6394 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
6395 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
6396 }
6397 | f_optarg(arg_value) opt_args_tail(args_tail)
6398 {
6399 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
6400 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
6401 }
6402 | f_optarg(arg_value) ',' f_arg opt_args_tail(args_tail)
6403 {
6404 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
6405 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
6406 }
6407 | f_rest_arg opt_args_tail(args_tail)
6408 {
6409 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
6410 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
6411 }
6412 | f_rest_arg ',' f_arg opt_args_tail(args_tail)
6413 {
6414 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
6415 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
6416 }
6417 | args_tail
6418 {
6419 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
6420 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
6421 }
6422 | /* none */
6423 {
6424 $$ = new_args_tail(p, 0, 0, 0, &@0);
6425 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6426 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6427 }
6428 ;
6429
6430args_forward : tBDOT3
6431 {
6432#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
6433 $$ = 0;
6434#else
6435 $$ = idFWD_KWREST;
6436#endif
6437 /*% ripper: args_forward! %*/
6438 }
6439 ;
6440
6441f_bad_arg : tCONSTANT
6442 {
6443 static const char mesg[] = "formal argument cannot be a constant";
6444 /*%%%*/
6445 yyerror1(&@1, mesg);
6446 /*% %*/
6447 $$ = 0;
6448 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6449 }
6450 | tIVAR
6451 {
6452 static const char mesg[] = "formal argument cannot be an instance variable";
6453 /*%%%*/
6454 yyerror1(&@1, mesg);
6455 /*% %*/
6456 $$ = 0;
6457 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6458 }
6459 | tGVAR
6460 {
6461 static const char mesg[] = "formal argument cannot be a global variable";
6462 /*%%%*/
6463 yyerror1(&@1, mesg);
6464 /*% %*/
6465 $$ = 0;
6466 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6467 }
6468 | tCVAR
6469 {
6470 static const char mesg[] = "formal argument cannot be a class variable";
6471 /*%%%*/
6472 yyerror1(&@1, mesg);
6473 /*% %*/
6474 $$ = 0;
6475 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6476 }
6477 ;
6478
6479f_norm_arg : f_bad_arg
6480 | tIDENTIFIER
6481 {
6482 VALUE e = formal_argument_error(p, $$ = $1);
6483 if (e) {
6484 /*% ripper[error]: param_error!(?e, $:1) %*/
6485 }
6486 p->max_numparam = ORDINAL_PARAM;
6487 }
6488 ;
6489
6490f_arg_asgn : f_norm_arg
6491 {
6492 ID id = $1;
6493 arg_var(p, id);
6494 $$ = $1;
6495 }
6496 ;
6497
6498f_arg_item : f_arg_asgn
6499 {
6500 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6501 /*% ripper: $:1 %*/
6502 }
6503 | tLPAREN f_margs rparen
6504 {
6505 ID tid = internal_id(p);
6506 YYLTYPE loc;
6507 loc.beg_pos = @2.beg_pos;
6508 loc.end_pos = @2.beg_pos;
6509 arg_var(p, tid);
6510 if (dyna_in_block(p)) {
6511 $2->nd_value = NEW_DVAR(tid, &loc);
6512 }
6513 else {
6514 $2->nd_value = NEW_LVAR(tid, &loc);
6515 }
6516 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6517 $$->nd_next = (NODE *)$2;
6518 /*% ripper: mlhs_paren!($:2) %*/
6519 }
6520 ;
6521
6522f_arg : f_arg_item
6523 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6524 | f_arg ',' f_arg_item
6525 {
6526 $$ = $1;
6527 $$->nd_plen++;
6528 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6529 rb_discard_node(p, (NODE *)$3);
6530 /*% ripper: rb_ary_push($:1, $:3) %*/
6531 }
6532 ;
6533
6534
6535f_label : tLABEL
6536 {
6537 VALUE e = formal_argument_error(p, $$ = $1);
6538 if (e) {
6539 $$ = 0;
6540 /*% ripper[error]: param_error!(?e, $:1) %*/
6541 }
6542 /*
6543 * Workaround for Prism::ParseTest#test_filepath for
6544 * "unparser/corpus/literal/def.txt"
6545 *
6546 * See the discussion on https://github.com/ruby/ruby/pull/9923
6547 */
6548 arg_var(p, ifdef_ripper(0, $1));
6549 /*% ripper: $:1 %*/
6550 p->max_numparam = ORDINAL_PARAM;
6551 p->ctxt.in_argdef = 0;
6552 }
6553 ;
6554
6555kwrest_mark : tPOW
6556 | tDSTAR
6557 ;
6558
6559f_no_kwarg : p_kwnorest
6560 {
6561 /*% ripper: nokw_param!(Qnil) %*/
6562 }
6563 ;
6564
6565f_kwrest : kwrest_mark tIDENTIFIER
6566 {
6567 arg_var(p, shadowing_lvar(p, $2));
6568 $$ = $2;
6569 /*% ripper: kwrest_param!($:2) %*/
6570 }
6571 | kwrest_mark
6572 {
6573 arg_var(p, idFWD_KWREST);
6574 $$ = idFWD_KWREST;
6575 /*% ripper: kwrest_param!(Qnil) %*/
6576 }
6577 ;
6578
6579restarg_mark : '*'
6580 | tSTAR
6581 ;
6582
6583f_rest_arg : restarg_mark tIDENTIFIER
6584 {
6585 arg_var(p, shadowing_lvar(p, $2));
6586 $$ = $2;
6587 /*% ripper: rest_param!($:2) %*/
6588 }
6589 | restarg_mark
6590 {
6591 arg_var(p, idFWD_REST);
6592 $$ = idFWD_REST;
6593 /*% ripper: rest_param!(Qnil) %*/
6594 }
6595 ;
6596
6597blkarg_mark : '&'
6598 | tAMPER
6599 ;
6600
6601f_block_arg : blkarg_mark tIDENTIFIER
6602 {
6603 arg_var(p, shadowing_lvar(p, $2));
6604 $$ = $2;
6605 /*% ripper: blockarg!($:2) %*/
6606 }
6607 | blkarg_mark
6608 {
6609 arg_var(p, idFWD_BLOCK);
6610 $$ = idFWD_BLOCK;
6611 /*% ripper: blockarg!(Qnil) %*/
6612 }
6613 ;
6614
6615opt_f_block_arg : ',' f_block_arg
6616 {
6617 $$ = $2;
6618 /*% ripper: $:2 %*/
6619 }
6620 | none
6621 {
6622 $$ = 0;
6623 /*% ripper: Qnil %*/
6624 }
6625 ;
6626
6627singleton : value_expr(var_ref)
6628 | '('
6629 {
6630 SET_LEX_STATE(EXPR_BEG);
6631 p->ctxt.in_argdef = 0;
6632 }
6633 expr rparen
6634 {
6635 p->ctxt.in_argdef = 1;
6636 NODE *expr = last_expr_node($3);
6637 switch (nd_type(expr)) {
6638 case NODE_STR:
6639 case NODE_DSTR:
6640 case NODE_XSTR:
6641 case NODE_DXSTR:
6642 case NODE_REGX:
6643 case NODE_DREGX:
6644 case NODE_SYM:
6645 case NODE_LINE:
6646 case NODE_FILE:
6647 case NODE_ENCODING:
6648 case NODE_INTEGER:
6649 case NODE_FLOAT:
6650 case NODE_RATIONAL:
6651 case NODE_IMAGINARY:
6652 case NODE_DSYM:
6653 case NODE_LIST:
6654 case NODE_ZLIST:
6655 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6656 break;
6657 default:
6658 value_expr($3);
6659 break;
6660 }
6661 $$ = $3;
6662 /*% ripper: paren!($:3) %*/
6663 }
6664 ;
6665
6666assoc_list : none
6667 | assocs trailer
6668 {
6669 $$ = $1;
6670 /*% ripper: assoclist_from_args!($:1) %*/
6671 }
6672 ;
6673
6674assocs : assoc
6675 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6676 | assocs ',' assoc
6677 {
6678 NODE *assocs = $1;
6679 NODE *tail = $3;
6680 if (!assocs) {
6681 assocs = tail;
6682 }
6683 else if (tail) {
6684 if (RNODE_LIST(assocs)->nd_head) {
6685 NODE *n = RNODE_LIST(tail)->nd_next;
6686 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6687 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6688 /* DSTAR */
6689 tail = RNODE_HASH(n)->nd_head;
6690 }
6691 }
6692 if (tail) {
6693 assocs = list_concat(assocs, tail);
6694 }
6695 }
6696 $$ = assocs;
6697 /*% ripper: rb_ary_push($:1, $:3) %*/
6698 }
6699 ;
6700
6701assoc : arg_value tASSOC arg_value
6702 {
6703 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6704 /*% ripper: assoc_new!($:1, $:3) %*/
6705 }
6706 | tLABEL arg_value
6707 {
6708 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6709 /*% ripper: assoc_new!($:1, $:2) %*/
6710 }
6711 | tLABEL
6712 {
6713 NODE *val = gettable(p, $1, &@$);
6714 if (!val) val = NEW_ERROR(&@$);
6715 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6716 /*% ripper: assoc_new!($:1, Qnil) %*/
6717 }
6718 | tSTRING_BEG string_contents tLABEL_END arg_value
6719 {
6720 YYLTYPE loc = code_loc_gen(&@1, &@3);
6721 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6722 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6723 }
6724 | tDSTAR arg_value
6725 {
6726 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6727 /*% ripper: assoc_splat!($:2) %*/
6728 }
6729 | tDSTAR
6730 {
6731 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6732 $$ = list_append(p, NEW_LIST(0, &@$),
6733 NEW_LVAR(idFWD_KWREST, &@$));
6734 /*% ripper: assoc_splat!(Qnil) %*/
6735 }
6736 ;
6737
6738operation : inline_operation
6739 ;
6740
6741operation2 : operation
6742 | op
6743 ;
6744
6745operation3 : tIDENTIFIER
6746 | tFID
6747 | op
6748 ;
6749
6750dot_or_colon : '.'
6751 | tCOLON2
6752 ;
6753
6754call_op : '.'
6755 | tANDDOT
6756 ;
6757
6758call_op2 : call_op
6759 | tCOLON2
6760 ;
6761
6762rparen : '\n'? ')'
6763 ;
6764
6765rbracket : '\n'? ']'
6766 ;
6767
6768rbrace : '\n'? '}'
6769 ;
6770
6771trailer : '\n'?
6772 | ','
6773 ;
6774
6775term : ';' {yyerrok;token_flush(p);}
6776 | '\n'
6777 {
6778 @$.end_pos = @$.beg_pos;
6779 token_flush(p);
6780 }
6781 ;
6782
6783terms : term
6784 | terms ';' {yyerrok;}
6785 ;
6786
6787none : /* none */
6788 {
6789 $$ = 0;
6790 }
6791 ;
6792%%
6793# undef p
6794# undef yylex
6795# undef yylval
6796# define yylval (*p->lval)
6797
6798static int regx_options(struct parser_params*);
6799static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6800static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6801static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6802static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6803
6804#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6805
6806# define set_yylval_node(x) { \
6807 YYLTYPE _cur_loc; \
6808 rb_parser_set_location(p, &_cur_loc); \
6809 yylval.node = (x); \
6810 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6811}
6812# define set_yylval_str(x) \
6813do { \
6814 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6815 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6816} while(0)
6817# define set_yylval_num(x) { \
6818 yylval.num = (x); \
6819 set_parser_s_value(x); \
6820}
6821# define set_yylval_id(x) (yylval.id = (x))
6822# define set_yylval_name(x) { \
6823 (yylval.id = (x)); \
6824 set_parser_s_value(ID2SYM(x)); \
6825}
6826# define yylval_id() (yylval.id)
6827
6828#define set_yylval_noname() set_yylval_id(keyword_nil)
6829#define has_delayed_token(p) (p->delayed.token != NULL)
6830
6831#ifndef RIPPER
6832#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6833#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6834
6835static bool
6836parser_has_token(struct parser_params *p)
6837{
6838 const char *const pcur = p->lex.pcur;
6839 const char *const ptok = p->lex.ptok;
6840 if (p->keep_tokens && (pcur < ptok)) {
6841 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6842 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6843 }
6844 return pcur > ptok;
6845}
6846
6847static const char *
6848escaped_char(int c)
6849{
6850 switch (c) {
6851 case '"': return "\\\"";
6852 case '\\': return "\\\\";
6853 case '\0': return "\\0";
6854 case '\n': return "\\n";
6855 case '\r': return "\\r";
6856 case '\t': return "\\t";
6857 case '\f': return "\\f";
6858 case '\013': return "\\v";
6859 case '\010': return "\\b";
6860 case '\007': return "\\a";
6861 case '\033': return "\\e";
6862 case '\x7f': return "\\c?";
6863 }
6864 return NULL;
6865}
6866
6867static rb_parser_string_t *
6868rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6869{
6870 rb_encoding *enc = p->enc;
6871 const char *ptr = str->ptr;
6872 const char *pend = ptr + str->len;
6873 const char *prev = ptr;
6874 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6875 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6876
6877 while (ptr < pend) {
6878 unsigned int c;
6879 const char *cc;
6880 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6881 if (!MBCLEN_CHARFOUND_P(n)) {
6882 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6883 n = rb_enc_mbminlen(enc);
6884 if (pend < ptr + n)
6885 n = (int)(pend - ptr);
6886 while (n--) {
6887 c = *ptr & 0xf0 >> 4;
6888 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6889 c = *ptr & 0x0f;
6890 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6891 parser_str_cat(result, charbuf, 4);
6892 prev = ++ptr;
6893 }
6894 continue;
6895 }
6896 n = MBCLEN_CHARFOUND_LEN(n);
6897 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6898 ptr += n;
6899 cc = escaped_char(c);
6900 if (cc) {
6901 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6902 parser_str_cat_cstr(result, cc);
6903 prev = ptr;
6904 }
6905 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6906 }
6907 else {
6908 if (ptr - n > prev) {
6909 parser_str_cat(result, prev, ptr - n - prev);
6910 prev = ptr - n;
6911 }
6912 parser_str_cat(result, prev, ptr - prev);
6913 prev = ptr;
6914 }
6915 }
6916 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6917
6918 return result;
6919}
6920
6921static void
6922parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6923{
6924 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6925 token->id = p->token_id;
6926 token->type_name = parser_token2char(p, t);
6927 token->str = str;
6928 token->loc.beg_pos = p->yylloc->beg_pos;
6929 token->loc.end_pos = p->yylloc->end_pos;
6930 rb_parser_ary_push_ast_token(p, p->tokens, token);
6931 p->token_id++;
6932
6933 if (p->debug) {
6934 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6935 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6936 line, token->id, token->type_name, str_escaped->ptr,
6937 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6938 token->loc.end_pos.lineno, token->loc.end_pos.column);
6939 rb_parser_string_free(p, str_escaped);
6940 }
6941}
6942
6943static void
6944parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6945{
6946 debug_token_line(p, "parser_dispatch_scan_event", line);
6947
6948 if (!parser_has_token(p)) return;
6949
6950 RUBY_SET_YYLLOC(*p->yylloc);
6951
6952 if (p->keep_tokens) {
6953 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6954 parser_append_tokens(p, str, t, line);
6955 }
6956
6957 token_flush(p);
6958}
6959
6960#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6961static void
6962parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6963{
6964 debug_token_line(p, "parser_dispatch_delayed_token", line);
6965
6966 if (!has_delayed_token(p)) return;
6967
6968 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6969
6970 if (p->keep_tokens) {
6971 /* p->delayed.token is freed by rb_parser_tokens_free */
6972 parser_append_tokens(p, p->delayed.token, t, line);
6973 } else {
6974 rb_parser_string_free(p, p->delayed.token);
6975 }
6976
6977 p->delayed.token = NULL;
6978}
6979#else
6980#define literal_flush(p, ptr) ((void)(ptr))
6981
6982static int
6983ripper_has_scan_event(struct parser_params *p)
6984{
6985 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6986 return p->lex.pcur > p->lex.ptok;
6987}
6988
6989static VALUE
6990ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6991{
6992 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6993 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6994 RUBY_SET_YYLLOC(*p->yylloc);
6995 token_flush(p);
6996 return rval;
6997}
6998
6999static void
7000ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
7001{
7002 if (!ripper_has_scan_event(p)) return;
7003
7004 set_parser_s_value(ripper_scan_event_val(p, t));
7005}
7006#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
7007
7008static void
7009ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
7010{
7011 /* save and adjust the location to delayed token for callbacks */
7012 int saved_line = p->ruby_sourceline;
7013 const char *saved_tokp = p->lex.ptok;
7014 VALUE s_value, str;
7015
7016 if (!has_delayed_token(p)) return;
7017 p->ruby_sourceline = p->delayed.beg_line;
7018 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
7019 str = rb_str_new_mutable_parser_string(p->delayed.token);
7020 rb_parser_string_free(p, p->delayed.token);
7021 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
7022 set_parser_s_value(s_value);
7023 p->delayed.token = NULL;
7024 p->ruby_sourceline = saved_line;
7025 p->lex.ptok = saved_tokp;
7026}
7027#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
7028#endif /* RIPPER */
7029
7030static inline int
7031is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
7032{
7033 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
7034}
7035
7036static inline int
7037parser_is_identchar(struct parser_params *p)
7038{
7039 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7040}
7041
7042static inline int
7043parser_isascii(struct parser_params *p)
7044{
7045 return ISASCII(*(p->lex.pcur-1));
7046}
7047
7048static void
7049token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7050{
7051 int column = 1, nonspc = 0, i;
7052 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7053 if (*ptr == '\t') {
7054 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7055 }
7056 column++;
7057 if (*ptr != ' ' && *ptr != '\t') {
7058 nonspc = 1;
7059 }
7060 }
7061
7062 ptinfo->beg = loc->beg_pos;
7063 ptinfo->indent = column;
7064 ptinfo->nonspc = nonspc;
7065}
7066
7067static void
7068token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7069{
7070 token_info *ptinfo;
7071
7072 if (!p->token_info_enabled) return;
7073 ptinfo = ALLOC(token_info);
7074 ptinfo->token = token;
7075 ptinfo->next = p->token_info;
7076 token_info_setup(ptinfo, p->lex.pbeg, loc);
7077
7078 p->token_info = ptinfo;
7079}
7080
7081static void
7082token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7083{
7084 token_info *ptinfo_beg = p->token_info;
7085
7086 if (!ptinfo_beg) return;
7087
7088 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7089 token_info_warn(p, token, ptinfo_beg, 1, loc);
7090
7091 p->token_info = ptinfo_beg->next;
7092 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7093}
7094
7095static void
7096token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7097{
7098 token_info *ptinfo_beg = p->token_info;
7099
7100 if (!ptinfo_beg) return;
7101 p->token_info = ptinfo_beg->next;
7102
7103 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7104 ptinfo_beg->beg.column != beg_pos.column ||
7105 strcmp(ptinfo_beg->token, token)) {
7106 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7107 beg_pos.lineno, beg_pos.column, token,
7108 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7109 ptinfo_beg->token);
7110 }
7111
7112 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7113}
7114
7115static void
7116token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7117{
7118 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7119 if (!p->token_info_enabled) return;
7120 if (!ptinfo_beg) return;
7121 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7122 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7123 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7124 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7125 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7126 rb_warn3L(ptinfo_end->beg.lineno,
7127 "mismatched indentations at '%s' with '%s' at %d",
7128 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7129}
7130
7131static int
7132parser_precise_mbclen(struct parser_params *p, const char *ptr)
7133{
7134 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7135 if (!MBCLEN_CHARFOUND_P(len)) {
7136 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7137 return -1;
7138 }
7139 return len;
7140}
7141
7142#ifndef RIPPER
7143static inline void
7144parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7145{
7146 rb_parser_string_t *str;
7147 int lineno = p->ruby_sourceline;
7148 if (!yylloc) {
7149 return;
7150 }
7151 else if (yylloc->beg_pos.lineno == lineno) {
7152 str = p->lex.lastline;
7153 }
7154 else {
7155 return;
7156 }
7157 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7158}
7159
7160static int
7161parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7162{
7163#if 0
7164 YYLTYPE current;
7165
7166 if (!yylloc) {
7167 yylloc = RUBY_SET_YYLLOC(current);
7168 }
7169 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7170 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7171 yylloc = 0;
7172 }
7173#endif
7174 parser_compile_error(p, yylloc, "%s", msg);
7175 parser_show_error_line(p, yylloc);
7176 return 0;
7177}
7178
7179static int
7180parser_yyerror0(struct parser_params *p, const char *msg)
7181{
7182 YYLTYPE current;
7183 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7184}
7185
7186void
7187ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7188{
7189 VALUE mesg;
7190 const int max_line_margin = 30;
7191 const char *ptr, *ptr_end, *pt, *pb;
7192 const char *pre = "", *post = "", *pend;
7193 const char *code = "", *caret = "";
7194 const char *lim;
7195 const char *const pbeg = PARSER_STRING_PTR(str);
7196 char *buf;
7197 long len;
7198 int i;
7199
7200 if (!yylloc) return;
7201 pend = rb_parser_string_end(str);
7202 if (pend > pbeg && pend[-1] == '\n') {
7203 if (--pend > pbeg && pend[-1] == '\r') --pend;
7204 }
7205
7206 pt = pend;
7207 if (lineno == yylloc->end_pos.lineno &&
7208 (pend - pbeg) > yylloc->end_pos.column) {
7209 pt = pbeg + yylloc->end_pos.column;
7210 }
7211
7212 ptr = ptr_end = pt;
7213 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7214 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7215
7216 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7217 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7218
7219 len = ptr_end - ptr;
7220 if (len > 4) {
7221 if (ptr > pbeg) {
7222 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7223 if (ptr > pbeg) pre = "...";
7224 }
7225 if (ptr_end < pend) {
7226 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7227 if (ptr_end < pend) post = "...";
7228 }
7229 }
7230 pb = pbeg;
7231 if (lineno == yylloc->beg_pos.lineno) {
7232 pb += yylloc->beg_pos.column;
7233 if (pb > pt) pb = pt;
7234 }
7235 if (pb < ptr) pb = ptr;
7236 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7237 return;
7238 }
7239 if (RTEST(errbuf)) {
7240 mesg = rb_attr_get(errbuf, idMesg);
7241 if (char_at_end(p, mesg, '\n') != '\n')
7242 rb_str_cat_cstr(mesg, "\n");
7243 }
7244 else {
7245 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7246 }
7247 if (!errbuf && rb_stderr_tty_p()) {
7248#define CSI_BEGIN "\033["
7249#define CSI_SGR "m"
7250 rb_str_catf(mesg,
7251 CSI_BEGIN""CSI_SGR"%s" /* pre */
7252 CSI_BEGIN"1"CSI_SGR"%.*s"
7253 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7254 CSI_BEGIN";1"CSI_SGR"%.*s"
7255 CSI_BEGIN""CSI_SGR"%s" /* post */
7256 "\n",
7257 pre,
7258 (int)(pb - ptr), ptr,
7259 (int)(pt - pb), pb,
7260 (int)(ptr_end - pt), pt,
7261 post);
7262 }
7263 else {
7264 char *p2;
7265
7266 len = ptr_end - ptr;
7267 lim = pt < pend ? pt : pend;
7268 i = (int)(lim - ptr);
7269 buf = ALLOCA_N(char, i+2);
7270 code = ptr;
7271 caret = p2 = buf;
7272 if (ptr <= pb) {
7273 while (ptr < pb) {
7274 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7275 }
7276 *p2++ = '^';
7277 ptr++;
7278 }
7279 if (lim > ptr) {
7280 memset(p2, '~', (lim - ptr));
7281 p2 += (lim - ptr);
7282 }
7283 *p2 = '\0';
7284 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7285 pre, (int)len, code, post,
7286 pre, caret);
7287 }
7288 if (!errbuf) rb_write_error_str(mesg);
7289}
7290#else
7291
7292static int
7293parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7294{
7295 const char *pcur = 0, *ptok = 0;
7296 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7297 p->ruby_sourceline == yylloc->end_pos.lineno) {
7298 pcur = p->lex.pcur;
7299 ptok = p->lex.ptok;
7300 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7301 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7302 }
7303 parser_yyerror0(p, msg);
7304 if (pcur) {
7305 p->lex.ptok = ptok;
7306 p->lex.pcur = pcur;
7307 }
7308 return 0;
7309}
7310
7311static int
7312parser_yyerror0(struct parser_params *p, const char *msg)
7313{
7314 dispatch1(parse_error, STR_NEW2(msg));
7315 ripper_error(p);
7316 return 0;
7317}
7318
7319static inline void
7320parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7321{
7322}
7323#endif /* !RIPPER */
7324
7325static int
7326vtable_size(const struct vtable *tbl)
7327{
7328 if (!DVARS_TERMINAL_P(tbl)) {
7329 return tbl->pos;
7330 }
7331 else {
7332 return 0;
7333 }
7334}
7335
7336static struct vtable *
7337vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7338{
7339 struct vtable *tbl = ALLOC(struct vtable);
7340 tbl->pos = 0;
7341 tbl->capa = 8;
7342 tbl->tbl = ALLOC_N(ID, tbl->capa);
7343 tbl->prev = prev;
7344#ifndef RIPPER
7345 if (p->debug) {
7346 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7347 }
7348#endif
7349 return tbl;
7350}
7351#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7352
7353static void
7354vtable_free_gen(struct parser_params *p, int line, const char *name,
7355 struct vtable *tbl)
7356{
7357#ifndef RIPPER
7358 if (p->debug) {
7359 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7360 }
7361#endif
7362 if (!DVARS_TERMINAL_P(tbl)) {
7363 if (tbl->tbl) {
7364 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7365 }
7366 ruby_sized_xfree(tbl, sizeof(*tbl));
7367 }
7368}
7369#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7370
7371static void
7372vtable_add_gen(struct parser_params *p, int line, const char *name,
7373 struct vtable *tbl, ID id)
7374{
7375#ifndef RIPPER
7376 if (p->debug) {
7377 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7378 line, name, (void *)tbl, rb_id2name(id));
7379 }
7380#endif
7381 if (DVARS_TERMINAL_P(tbl)) {
7382 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7383 return;
7384 }
7385 if (tbl->pos == tbl->capa) {
7386 tbl->capa = tbl->capa * 2;
7387 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7388 }
7389 tbl->tbl[tbl->pos++] = id;
7390}
7391#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7392
7393static void
7394vtable_pop_gen(struct parser_params *p, int line, const char *name,
7395 struct vtable *tbl, int n)
7396{
7397 if (p->debug) {
7398 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7399 line, name, (void *)tbl, n);
7400 }
7401 if (tbl->pos < n) {
7402 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7403 return;
7404 }
7405 tbl->pos -= n;
7406}
7407#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7408
7409static int
7410vtable_included(const struct vtable * tbl, ID id)
7411{
7412 int i;
7413
7414 if (!DVARS_TERMINAL_P(tbl)) {
7415 for (i = 0; i < tbl->pos; i++) {
7416 if (tbl->tbl[i] == id) {
7417 return i+1;
7418 }
7419 }
7420 }
7421 return 0;
7422}
7423
7424static void parser_prepare(struct parser_params *p);
7425
7426static int
7427e_option_supplied(struct parser_params *p)
7428{
7429 return strcmp(p->ruby_sourcefile, "-e") == 0;
7430}
7431
7432#ifndef RIPPER
7433static NODE *parser_append_options(struct parser_params *p, NODE *node);
7434
7435static VALUE
7436yycompile0(VALUE arg)
7437{
7438 int n;
7439 NODE *tree;
7440 struct parser_params *p = (struct parser_params *)arg;
7441 int cov = FALSE;
7442
7443 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7444 cov = TRUE;
7445 }
7446
7447 if (p->debug_lines) {
7448 p->ast->body.script_lines = p->debug_lines;
7449 }
7450
7451 parser_prepare(p);
7452#define RUBY_DTRACE_PARSE_HOOK(name) \
7453 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7454 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7455 }
7456 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7457 n = yyparse(p);
7458 RUBY_DTRACE_PARSE_HOOK(END);
7459
7460 p->debug_lines = 0;
7461
7462 xfree(p->lex.strterm);
7463 p->lex.strterm = 0;
7464 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7465 if (n || p->error_p) {
7466 VALUE mesg = p->error_buffer;
7467 if (!mesg) {
7468 mesg = syntax_error_new();
7469 }
7470 if (!p->error_tolerant) {
7471 rb_set_errinfo(mesg);
7472 return FALSE;
7473 }
7474 }
7475 tree = p->eval_tree;
7476 if (!tree) {
7477 tree = NEW_NIL(&NULL_LOC);
7478 }
7479 else {
7480 rb_parser_ary_t *tokens = p->tokens;
7481 NODE *prelude;
7482 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7483 prelude = block_append(p, p->eval_tree_begin, body);
7484 RNODE_SCOPE(tree)->nd_body = prelude;
7485 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7486 p->ast->body.coverage_enabled = cov;
7487 if (p->keep_tokens) {
7488 p->ast->node_buffer->tokens = tokens;
7489 p->tokens = NULL;
7490 }
7491 }
7492 p->ast->body.root = tree;
7493 p->ast->body.line_count = p->line_count;
7494 return TRUE;
7495}
7496
7497static rb_ast_t *
7498yycompile(struct parser_params *p, VALUE fname, int line)
7499{
7500 rb_ast_t *ast;
7501 if (NIL_P(fname)) {
7502 p->ruby_sourcefile_string = Qnil;
7503 p->ruby_sourcefile = "(none)";
7504 }
7505 else {
7506 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7507 p->ruby_sourcefile = StringValueCStr(fname);
7508 }
7509 p->ruby_sourceline = line - 1;
7510
7511 p->lvtbl = NULL;
7512
7513 p->ast = ast = rb_ast_new();
7514 compile_callback(yycompile0, (VALUE)p);
7515 p->ast = 0;
7516
7517 while (p->lvtbl) {
7518 local_pop(p);
7519 }
7520
7521 return ast;
7522}
7523#endif /* !RIPPER */
7524
7525static rb_encoding *
7526must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7527{
7528 rb_encoding *enc = rb_parser_str_get_encoding(s);
7529 if (!rb_enc_asciicompat(enc)) {
7530 rb_raise(rb_eArgError, "invalid source encoding");
7531 }
7532 return enc;
7533}
7534
7535static rb_parser_string_t *
7536lex_getline(struct parser_params *p)
7537{
7538 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7539 if (!line) return 0;
7540 p->line_count++;
7541 string_buffer_append(p, line);
7542 must_be_ascii_compatible(p, line);
7543 return line;
7544}
7545
7546#ifndef RIPPER
7547rb_ast_t*
7548rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7549{
7550 p->lex.gets = gets;
7551 p->lex.input = input;
7552 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7553
7554 return yycompile(p, fname, line);
7555}
7556#endif /* !RIPPER */
7557
7558#define STR_FUNC_ESCAPE 0x01
7559#define STR_FUNC_EXPAND 0x02
7560#define STR_FUNC_REGEXP 0x04
7561#define STR_FUNC_QWORDS 0x08
7562#define STR_FUNC_SYMBOL 0x10
7563#define STR_FUNC_INDENT 0x20
7564#define STR_FUNC_LABEL 0x40
7565#define STR_FUNC_LIST 0x4000
7566#define STR_FUNC_TERM 0x8000
7567
7568enum string_type {
7569 str_label = STR_FUNC_LABEL,
7570 str_squote = (0),
7571 str_dquote = (STR_FUNC_EXPAND),
7572 str_xquote = (STR_FUNC_EXPAND),
7573 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7574 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7575 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7576 str_ssym = (STR_FUNC_SYMBOL),
7577 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7578};
7579
7580static rb_parser_string_t *
7581parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7582{
7583 rb_parser_string_t *pstr;
7584
7585 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7586
7587 if (!(func & STR_FUNC_REGEXP)) {
7588 if (rb_parser_is_ascii_string(p, pstr)) {
7589 }
7590 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7591 /* everything is valid in ASCII-8BIT */
7592 enc = rb_ascii8bit_encoding();
7593 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7594 }
7595 }
7596
7597 return pstr;
7598}
7599
7600static int
7601strterm_is_heredoc(rb_strterm_t *strterm)
7602{
7603 return strterm->heredoc;
7604}
7605
7606static rb_strterm_t *
7607new_strterm(struct parser_params *p, int func, int term, int paren)
7608{
7609 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7610 strterm->u.literal.func = func;
7611 strterm->u.literal.term = term;
7612 strterm->u.literal.paren = paren;
7613 return strterm;
7614}
7615
7616static rb_strterm_t *
7617new_heredoc(struct parser_params *p)
7618{
7619 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7620 strterm->heredoc = true;
7621 return strterm;
7622}
7623
7624#define peek(p,c) peek_n(p, (c), 0)
7625#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7626#define peekc(p) peekc_n(p, 0)
7627#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7628
7629#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7630static void
7631parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7632{
7633 debug_token_line(p, "add_delayed_token", line);
7634
7635 if (tok < end) {
7636 if (has_delayed_token(p)) {
7637 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7638 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7639 int end_col = (next_line ? 0 : p->delayed.end_col);
7640 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7641 dispatch_delayed_token(p, tSTRING_CONTENT);
7642 }
7643 }
7644 if (!has_delayed_token(p)) {
7645 p->delayed.token = rb_parser_string_new(p, 0, 0);
7646 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7647 p->delayed.beg_line = p->ruby_sourceline;
7648 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7649 }
7650 parser_str_cat(p->delayed.token, tok, end - tok);
7651 p->delayed.end_line = p->ruby_sourceline;
7652 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7653 p->lex.ptok = end;
7654 }
7655}
7656
7657static void
7658set_lastline(struct parser_params *p, rb_parser_string_t *str)
7659{
7660 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7661 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7662 p->lex.lastline = str;
7663}
7664
7665static int
7666nextline(struct parser_params *p, int set_encoding)
7667{
7668 rb_parser_string_t *str = p->lex.nextline;
7669 p->lex.nextline = 0;
7670 if (!str) {
7671 if (p->eofp)
7672 return -1;
7673
7674 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7675 goto end_of_input;
7676 }
7677
7678 if (!p->lex.input || !(str = lex_getline(p))) {
7679 end_of_input:
7680 p->eofp = 1;
7681 lex_goto_eol(p);
7682 return -1;
7683 }
7684#ifndef RIPPER
7685 if (p->debug_lines) {
7686 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7687 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7688 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7689 }
7690#endif
7691 p->cr_seen = FALSE;
7692 }
7693 else if (str == AFTER_HEREDOC_WITHOUT_TERMINTOR) {
7694 /* after here-document without terminator */
7695 goto end_of_input;
7696 }
7697 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7698 if (p->heredoc_end > 0) {
7699 p->ruby_sourceline = p->heredoc_end;
7700 p->heredoc_end = 0;
7701 }
7702 p->ruby_sourceline++;
7703 set_lastline(p, str);
7704 token_flush(p);
7705 return 0;
7706}
7707
7708static int
7709parser_cr(struct parser_params *p, int c)
7710{
7711 if (peek(p, '\n')) {
7712 p->lex.pcur++;
7713 c = '\n';
7714 }
7715 return c;
7716}
7717
7718static inline int
7719nextc0(struct parser_params *p, int set_encoding)
7720{
7721 int c;
7722
7723 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINTOR)) {
7724 if (nextline(p, set_encoding)) return -1;
7725 }
7726 c = (unsigned char)*p->lex.pcur++;
7727 if (UNLIKELY(c == '\r')) {
7728 c = parser_cr(p, c);
7729 }
7730
7731 return c;
7732}
7733#define nextc(p) nextc0(p, TRUE)
7734
7735static void
7736pushback(struct parser_params *p, int c)
7737{
7738 if (c == -1) return;
7739 p->eofp = 0;
7740 p->lex.pcur--;
7741 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7742 p->lex.pcur--;
7743 }
7744}
7745
7746#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7747
7748#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7749#define tok(p) (p)->tokenbuf
7750#define toklen(p) (p)->tokidx
7751
7752static int
7753looking_at_eol_p(struct parser_params *p)
7754{
7755 const char *ptr = p->lex.pcur;
7756 while (!lex_eol_ptr_p(p, ptr)) {
7757 int c = (unsigned char)*ptr++;
7758 int eol = (c == '\n' || c == '#');
7759 if (eol || !ISSPACE(c)) {
7760 return eol;
7761 }
7762 }
7763 return TRUE;
7764}
7765
7766static char*
7767newtok(struct parser_params *p)
7768{
7769 p->tokidx = 0;
7770 if (!p->tokenbuf) {
7771 p->toksiz = 60;
7772 p->tokenbuf = ALLOC_N(char, 60);
7773 }
7774 if (p->toksiz > 4096) {
7775 p->toksiz = 60;
7776 REALLOC_N(p->tokenbuf, char, 60);
7777 }
7778 return p->tokenbuf;
7779}
7780
7781static char *
7782tokspace(struct parser_params *p, int n)
7783{
7784 p->tokidx += n;
7785
7786 if (p->tokidx >= p->toksiz) {
7787 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7788 REALLOC_N(p->tokenbuf, char, p->toksiz);
7789 }
7790 return &p->tokenbuf[p->tokidx-n];
7791}
7792
7793static void
7794tokadd(struct parser_params *p, int c)
7795{
7796 p->tokenbuf[p->tokidx++] = (char)c;
7797 if (p->tokidx >= p->toksiz) {
7798 p->toksiz *= 2;
7799 REALLOC_N(p->tokenbuf, char, p->toksiz);
7800 }
7801}
7802
7803static int
7804tok_hex(struct parser_params *p, size_t *numlen)
7805{
7806 int c;
7807
7808 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7809 if (!*numlen) {
7810 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7811 yyerror0("invalid hex escape");
7812 dispatch_scan_event(p, tSTRING_CONTENT);
7813 return 0;
7814 }
7815 p->lex.pcur += *numlen;
7816 return c;
7817}
7818
7819#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7820
7821static int
7822escaped_control_code(int c)
7823{
7824 int c2 = 0;
7825 switch (c) {
7826 case ' ':
7827 c2 = 's';
7828 break;
7829 case '\n':
7830 c2 = 'n';
7831 break;
7832 case '\t':
7833 c2 = 't';
7834 break;
7835 case '\v':
7836 c2 = 'v';
7837 break;
7838 case '\r':
7839 c2 = 'r';
7840 break;
7841 case '\f':
7842 c2 = 'f';
7843 break;
7844 }
7845 return c2;
7846}
7847
7848#define WARN_SPACE_CHAR(c, prefix) \
7849 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7850
7851static int
7852tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7853 int regexp_literal, const char *begin)
7854{
7855 const int wide = !begin;
7856 size_t numlen;
7857 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7858
7859 p->lex.pcur += numlen;
7860 if (p->lex.strterm == NULL ||
7861 strterm_is_heredoc(p->lex.strterm) ||
7862 (p->lex.strterm->u.literal.func != str_regexp)) {
7863 if (!begin) begin = p->lex.pcur;
7864 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7865 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7866 yyerror0("invalid Unicode escape");
7867 dispatch_scan_event(p, tSTRING_CONTENT);
7868 return wide && numlen > 0;
7869 }
7870 if (codepoint > 0x10ffff) {
7871 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7872 yyerror0("invalid Unicode codepoint (too large)");
7873 dispatch_scan_event(p, tSTRING_CONTENT);
7874 return wide;
7875 }
7876 if ((codepoint & 0xfffff800) == 0xd800) {
7877 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7878 yyerror0("invalid Unicode codepoint");
7879 dispatch_scan_event(p, tSTRING_CONTENT);
7880 return wide;
7881 }
7882 }
7883 if (regexp_literal) {
7884 tokcopy(p, (int)numlen);
7885 }
7886 else if (codepoint >= 0x80) {
7887 rb_encoding *utf8 = rb_utf8_encoding();
7888 if (*encp && utf8 != *encp) {
7889 YYLTYPE loc = RUBY_INIT_YYLLOC();
7890 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7891 parser_show_error_line(p, &loc);
7892 return wide;
7893 }
7894 *encp = utf8;
7895 tokaddmbc(p, codepoint, *encp);
7896 }
7897 else {
7898 tokadd(p, codepoint);
7899 }
7900 return TRUE;
7901}
7902
7903static int tokadd_mbchar(struct parser_params *p, int c);
7904
7905static int
7906tokskip_mbchar(struct parser_params *p)
7907{
7908 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7909 if (len > 0) {
7910 p->lex.pcur += len - 1;
7911 }
7912 return len;
7913}
7914
7915/* return value is for ?\u3042 */
7916static void
7917tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7918 int term, int symbol_literal, int regexp_literal)
7919{
7920 /*
7921 * If `term` is not -1, then we allow multiple codepoints in \u{}
7922 * upto `term` byte, otherwise we're parsing a character literal.
7923 * And then add the codepoints to the current token.
7924 */
7925 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7926
7927 const int open_brace = '{', close_brace = '}';
7928
7929 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7930
7931 if (peek(p, open_brace)) { /* handle \u{...} form */
7932 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7933 /*
7934 * Skip parsing validation code and copy bytes as-is until term or
7935 * closing brace, in order to correctly handle extended regexps where
7936 * invalid unicode escapes are allowed in comments. The regexp parser
7937 * does its own validation and will catch any issues.
7938 */
7939 tokadd(p, open_brace);
7940 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7941 int c = peekc(p);
7942 if (c == close_brace) {
7943 tokadd(p, c);
7944 ++p->lex.pcur;
7945 break;
7946 }
7947 else if (c == term) {
7948 break;
7949 }
7950 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7951 tokadd(p, c);
7952 c = *++p->lex.pcur;
7953 }
7954 tokadd_mbchar(p, c);
7955 }
7956 }
7957 else {
7958 const char *second = NULL;
7959 int c, last = nextc(p);
7960 if (lex_eol_p(p)) goto unterminated;
7961 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7962 while (c != close_brace) {
7963 if (c == term) goto unterminated;
7964 if (second == multiple_codepoints)
7965 second = p->lex.pcur;
7966 if (regexp_literal) tokadd(p, last);
7967 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7968 break;
7969 }
7970 while (ISSPACE(c = peekc(p))) {
7971 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7972 last = c;
7973 }
7974 if (term == -1 && !second)
7975 second = multiple_codepoints;
7976 }
7977
7978 if (c != close_brace) {
7979 unterminated:
7980 flush_string_content(p, rb_utf8_encoding(), 0);
7981 yyerror0("unterminated Unicode escape");
7982 dispatch_scan_event(p, tSTRING_CONTENT);
7983 return;
7984 }
7985 if (second && second != multiple_codepoints) {
7986 const char *pcur = p->lex.pcur;
7987 p->lex.pcur = second;
7988 dispatch_scan_event(p, tSTRING_CONTENT);
7989 token_flush(p);
7990 p->lex.pcur = pcur;
7991 yyerror0(multiple_codepoints);
7992 token_flush(p);
7993 }
7994
7995 if (regexp_literal) tokadd(p, close_brace);
7996 nextc(p);
7997 }
7998 }
7999 else { /* handle \uxxxx form */
8000 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
8001 token_flush(p);
8002 return;
8003 }
8004 }
8005}
8006
8007#define ESCAPE_CONTROL 1
8008#define ESCAPE_META 2
8009
8010static int
8011read_escape(struct parser_params *p, int flags, const char *begin)
8012{
8013 int c;
8014 size_t numlen;
8015
8016 switch (c = nextc(p)) {
8017 case '\\': /* Backslash */
8018 return c;
8019
8020 case 'n': /* newline */
8021 return '\n';
8022
8023 case 't': /* horizontal tab */
8024 return '\t';
8025
8026 case 'r': /* carriage-return */
8027 return '\r';
8028
8029 case 'f': /* form-feed */
8030 return '\f';
8031
8032 case 'v': /* vertical tab */
8033 return '\13';
8034
8035 case 'a': /* alarm(bell) */
8036 return '\007';
8037
8038 case 'e': /* escape */
8039 return 033;
8040
8041 case '0': case '1': case '2': case '3': /* octal constant */
8042 case '4': case '5': case '6': case '7':
8043 pushback(p, c);
8044 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8045 p->lex.pcur += numlen;
8046 return c;
8047
8048 case 'x': /* hex constant */
8049 c = tok_hex(p, &numlen);
8050 if (numlen == 0) return 0;
8051 return c;
8052
8053 case 'b': /* backspace */
8054 return '\010';
8055
8056 case 's': /* space */
8057 return ' ';
8058
8059 case 'M':
8060 if (flags & ESCAPE_META) goto eof;
8061 if ((c = nextc(p)) != '-') {
8062 goto eof;
8063 }
8064 if ((c = nextc(p)) == '\\') {
8065 switch (peekc(p)) {
8066 case 'u': case 'U':
8067 nextc(p);
8068 goto eof;
8069 }
8070 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8071 }
8072 else if (c == -1) goto eof;
8073 else if (!ISASCII(c)) {
8074 tokskip_mbchar(p);
8075 goto eof;
8076 }
8077 else {
8078 int c2 = escaped_control_code(c);
8079 if (c2) {
8080 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8081 WARN_SPACE_CHAR(c2, "\\M-");
8082 }
8083 else {
8084 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8085 }
8086 }
8087 else if (ISCNTRL(c)) goto eof;
8088 return ((c & 0xff) | 0x80);
8089 }
8090
8091 case 'C':
8092 if ((c = nextc(p)) != '-') {
8093 goto eof;
8094 }
8095 case 'c':
8096 if (flags & ESCAPE_CONTROL) goto eof;
8097 if ((c = nextc(p))== '\\') {
8098 switch (peekc(p)) {
8099 case 'u': case 'U':
8100 nextc(p);
8101 goto eof;
8102 }
8103 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8104 }
8105 else if (c == '?')
8106 return 0177;
8107 else if (c == -1) goto eof;
8108 else if (!ISASCII(c)) {
8109 tokskip_mbchar(p);
8110 goto eof;
8111 }
8112 else {
8113 int c2 = escaped_control_code(c);
8114 if (c2) {
8115 if (ISCNTRL(c)) {
8116 if (flags & ESCAPE_META) {
8117 WARN_SPACE_CHAR(c2, "\\M-");
8118 }
8119 else {
8120 WARN_SPACE_CHAR(c2, "");
8121 }
8122 }
8123 else {
8124 if (flags & ESCAPE_META) {
8125 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8126 }
8127 else {
8128 WARN_SPACE_CHAR(c2, "\\C-");
8129 }
8130 }
8131 }
8132 else if (ISCNTRL(c)) goto eof;
8133 }
8134 return c & 0x9f;
8135
8136 eof:
8137 case -1:
8138 flush_string_content(p, p->enc, p->lex.pcur - begin);
8139 yyerror0("Invalid escape character syntax");
8140 dispatch_scan_event(p, tSTRING_CONTENT);
8141 return '\0';
8142
8143 default:
8144 if (!ISASCII(c)) {
8145 tokskip_mbchar(p);
8146 goto eof;
8147 }
8148 return c;
8149 }
8150}
8151
8152static void
8153tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8154{
8155 int len = rb_enc_codelen(c, enc);
8156 rb_enc_mbcput(c, tokspace(p, len), enc);
8157}
8158
8159static int
8160tokadd_escape(struct parser_params *p)
8161{
8162 int c;
8163 size_t numlen;
8164 const char *begin = p->lex.pcur;
8165
8166 switch (c = nextc(p)) {
8167 case '\n':
8168 return 0; /* just ignore */
8169
8170 case '0': case '1': case '2': case '3': /* octal constant */
8171 case '4': case '5': case '6': case '7':
8172 {
8173 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8174 if (numlen == 0) goto eof;
8175 p->lex.pcur += numlen;
8176 tokcopy(p, (int)numlen + 1);
8177 }
8178 return 0;
8179
8180 case 'x': /* hex constant */
8181 {
8182 tok_hex(p, &numlen);
8183 if (numlen == 0) return -1;
8184 tokcopy(p, (int)numlen + 2);
8185 }
8186 return 0;
8187
8188 eof:
8189 case -1:
8190 flush_string_content(p, p->enc, p->lex.pcur - begin);
8191 yyerror0("Invalid escape character syntax");
8192 token_flush(p);
8193 return -1;
8194
8195 default:
8196 tokadd(p, '\\');
8197 tokadd(p, c);
8198 }
8199 return 0;
8200}
8201
8202static int
8203char_to_option(int c)
8204{
8205 int val;
8206
8207 switch (c) {
8208 case 'i':
8209 val = RE_ONIG_OPTION_IGNORECASE;
8210 break;
8211 case 'x':
8212 val = RE_ONIG_OPTION_EXTEND;
8213 break;
8214 case 'm':
8215 val = RE_ONIG_OPTION_MULTILINE;
8216 break;
8217 default:
8218 val = 0;
8219 break;
8220 }
8221 return val;
8222}
8223
8224#define ARG_ENCODING_FIXED 16
8225#define ARG_ENCODING_NONE 32
8226#define ENC_ASCII8BIT 1
8227#define ENC_EUC_JP 2
8228#define ENC_Windows_31J 3
8229#define ENC_UTF8 4
8230
8231static int
8232char_to_option_kcode(int c, int *option, int *kcode)
8233{
8234 *option = 0;
8235
8236 switch (c) {
8237 case 'n':
8238 *kcode = ENC_ASCII8BIT;
8239 return (*option = ARG_ENCODING_NONE);
8240 case 'e':
8241 *kcode = ENC_EUC_JP;
8242 break;
8243 case 's':
8244 *kcode = ENC_Windows_31J;
8245 break;
8246 case 'u':
8247 *kcode = ENC_UTF8;
8248 break;
8249 default:
8250 *kcode = -1;
8251 return (*option = char_to_option(c));
8252 }
8253 *option = ARG_ENCODING_FIXED;
8254 return 1;
8255}
8256
8257static int
8258regx_options(struct parser_params *p)
8259{
8260 int kcode = 0;
8261 int kopt = 0;
8262 int options = 0;
8263 int c, opt, kc;
8264
8265 newtok(p);
8266 while (c = nextc(p), ISALPHA(c)) {
8267 if (c == 'o') {
8268 options |= RE_OPTION_ONCE;
8269 }
8270 else if (char_to_option_kcode(c, &opt, &kc)) {
8271 if (kc >= 0) {
8272 if (kc != ENC_ASCII8BIT) kcode = c;
8273 kopt = opt;
8274 }
8275 else {
8276 options |= opt;
8277 }
8278 }
8279 else {
8280 tokadd(p, c);
8281 }
8282 }
8283 options |= kopt;
8284 pushback(p, c);
8285 if (toklen(p)) {
8286 YYLTYPE loc = RUBY_INIT_YYLLOC();
8287 tokfix(p);
8288 compile_error(p, "unknown regexp option%s - %*s",
8289 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8290 parser_show_error_line(p, &loc);
8291 }
8292 return options | RE_OPTION_ENCODING(kcode);
8293}
8294
8295static int
8296tokadd_mbchar(struct parser_params *p, int c)
8297{
8298 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8299 if (len < 0) return -1;
8300 tokadd(p, c);
8301 p->lex.pcur += --len;
8302 if (len > 0) tokcopy(p, len);
8303 return c;
8304}
8305
8306static inline int
8307simple_re_meta(int c)
8308{
8309 switch (c) {
8310 case '$': case '*': case '+': case '.':
8311 case '?': case '^': case '|':
8312 case ')': case ']': case '}': case '>':
8313 return TRUE;
8314 default:
8315 return FALSE;
8316 }
8317}
8318
8319static int
8320parser_update_heredoc_indent(struct parser_params *p, int c)
8321{
8322 if (p->heredoc_line_indent == -1) {
8323 if (c == '\n') p->heredoc_line_indent = 0;
8324 }
8325 else {
8326 if (c == ' ') {
8327 p->heredoc_line_indent++;
8328 return TRUE;
8329 }
8330 else if (c == '\t') {
8331 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8332 p->heredoc_line_indent = w * TAB_WIDTH;
8333 return TRUE;
8334 }
8335 else if (c != '\n') {
8336 if (p->heredoc_indent > p->heredoc_line_indent) {
8337 p->heredoc_indent = p->heredoc_line_indent;
8338 }
8339 p->heredoc_line_indent = -1;
8340 }
8341 else {
8342 /* Whitespace only line has no indentation */
8343 p->heredoc_line_indent = 0;
8344 }
8345 }
8346 return FALSE;
8347}
8348
8349static void
8350parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8351{
8352 YYLTYPE loc = RUBY_INIT_YYLLOC();
8353 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8354 compile_error(p, "%s mixed within %s source", n1, n2);
8355 parser_show_error_line(p, &loc);
8356}
8357
8358static void
8359parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8360{
8361 const char *pos = p->lex.pcur;
8362 p->lex.pcur = beg;
8363 parser_mixed_error(p, enc1, enc2);
8364 p->lex.pcur = pos;
8365}
8366
8367static inline char
8368nibble_char_upper(unsigned int c)
8369{
8370 c &= 0xf;
8371 return c + (c < 10 ? '0' : 'A' - 10);
8372}
8373
8374static int
8375tokadd_string(struct parser_params *p,
8376 int func, int term, int paren, long *nest,
8377 rb_encoding **encp, rb_encoding **enc)
8378{
8379 int c;
8380 bool erred = false;
8381#ifdef RIPPER
8382 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8383 int top_of_line = FALSE;
8384#endif
8385
8386#define mixed_error(enc1, enc2) \
8387 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8388#define mixed_escape(beg, enc1, enc2) \
8389 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8390
8391 while ((c = nextc(p)) != -1) {
8392 if (p->heredoc_indent > 0) {
8393 parser_update_heredoc_indent(p, c);
8394 }
8395#ifdef RIPPER
8396 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8397 pushback(p, c);
8398 break;
8399 }
8400#endif
8401
8402 if (paren && c == paren) {
8403 ++*nest;
8404 }
8405 else if (c == term) {
8406 if (!nest || !*nest) {
8407 pushback(p, c);
8408 break;
8409 }
8410 --*nest;
8411 }
8412 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8413 unsigned char c2 = *p->lex.pcur;
8414 if (c2 == '$' || c2 == '@' || c2 == '{') {
8415 pushback(p, c);
8416 break;
8417 }
8418 }
8419 else if (c == '\\') {
8420 c = nextc(p);
8421 switch (c) {
8422 case '\n':
8423 if (func & STR_FUNC_QWORDS) break;
8424 if (func & STR_FUNC_EXPAND) {
8425 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8426 continue;
8427 if (c == term) {
8428 c = '\\';
8429 goto terminate;
8430 }
8431 }
8432 tokadd(p, '\\');
8433 break;
8434
8435 case '\\':
8436 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8437 break;
8438
8439 case 'u':
8440 if ((func & STR_FUNC_EXPAND) == 0) {
8441 tokadd(p, '\\');
8442 break;
8443 }
8444 tokadd_utf8(p, enc, term,
8445 func & STR_FUNC_SYMBOL,
8446 func & STR_FUNC_REGEXP);
8447 continue;
8448
8449 default:
8450 if (c == -1) return -1;
8451 if (!ISASCII(c)) {
8452 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8453 goto non_ascii;
8454 }
8455 if (func & STR_FUNC_REGEXP) {
8456 switch (c) {
8457 case 'c':
8458 case 'C':
8459 case 'M': {
8460 pushback(p, c);
8461 c = read_escape(p, 0, p->lex.pcur - 1);
8462
8463 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8464 *t++ = '\\';
8465 *t++ = 'x';
8466 *t++ = nibble_char_upper(c >> 4);
8467 *t++ = nibble_char_upper(c);
8468 continue;
8469 }
8470 }
8471
8472 if (c == term && !simple_re_meta(c)) {
8473 tokadd(p, c);
8474 continue;
8475 }
8476 pushback(p, c);
8477 if ((c = tokadd_escape(p)) < 0)
8478 return -1;
8479 if (*enc && *enc != *encp) {
8480 mixed_escape(p->lex.ptok+2, *enc, *encp);
8481 }
8482 continue;
8483 }
8484 else if (func & STR_FUNC_EXPAND) {
8485 pushback(p, c);
8486 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8487 c = read_escape(p, 0, p->lex.pcur - 1);
8488 }
8489 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8490 /* ignore backslashed spaces in %w */
8491 }
8492 else if (c != term && !(paren && c == paren)) {
8493 tokadd(p, '\\');
8494 pushback(p, c);
8495 continue;
8496 }
8497 }
8498 }
8499 else if (!parser_isascii(p)) {
8500 non_ascii:
8501 if (!*enc) {
8502 *enc = *encp;
8503 }
8504 else if (*enc != *encp) {
8505 mixed_error(*enc, *encp);
8506 continue;
8507 }
8508 if (tokadd_mbchar(p, c) == -1) return -1;
8509 continue;
8510 }
8511 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8512 pushback(p, c);
8513 break;
8514 }
8515 if (c & 0x80) {
8516 if (!*enc) {
8517 *enc = *encp;
8518 }
8519 else if (*enc != *encp) {
8520 mixed_error(*enc, *encp);
8521 continue;
8522 }
8523 }
8524 tokadd(p, c);
8525#ifdef RIPPER
8526 top_of_line = (c == '\n');
8527#endif
8528 }
8529 terminate:
8530 if (*enc) *encp = *enc;
8531 return c;
8532}
8533
8534#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8535
8536static void
8537flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8538{
8539 p->lex.pcur -= back;
8540 if (has_delayed_token(p)) {
8541 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8542 if (len > 0) {
8543 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8544 p->delayed.end_line = p->ruby_sourceline;
8545 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8546 }
8547 dispatch_delayed_token(p, tSTRING_CONTENT);
8548 p->lex.ptok = p->lex.pcur;
8549 }
8550 dispatch_scan_event(p, tSTRING_CONTENT);
8551 p->lex.pcur += back;
8552}
8553
8554/* this can be shared with ripper, since it's independent from struct
8555 * parser_params. */
8556#ifndef RIPPER
8557#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8558#define SPECIAL_PUNCT(idx) ( \
8559 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8560 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8561 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8562 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8563 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8564 BIT('0', idx))
8565const uint_least32_t ruby_global_name_punct_bits[] = {
8566 SPECIAL_PUNCT(0),
8567 SPECIAL_PUNCT(1),
8568 SPECIAL_PUNCT(2),
8569};
8570#undef BIT
8571#undef SPECIAL_PUNCT
8572#endif
8573
8574static enum yytokentype
8575parser_peek_variable_name(struct parser_params *p)
8576{
8577 int c;
8578 const char *ptr = p->lex.pcur;
8579
8580 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8581 c = *ptr++;
8582 switch (c) {
8583 case '$':
8584 if ((c = *ptr) == '-') {
8585 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8586 c = *ptr;
8587 }
8588 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8589 return tSTRING_DVAR;
8590 }
8591 break;
8592 case '@':
8593 if ((c = *ptr) == '@') {
8594 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8595 c = *ptr;
8596 }
8597 break;
8598 case '{':
8599 p->lex.pcur = ptr;
8600 p->command_start = TRUE;
8601 yylval.state = p->lex.state;
8602 return tSTRING_DBEG;
8603 default:
8604 return 0;
8605 }
8606 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8607 return tSTRING_DVAR;
8608 return 0;
8609}
8610
8611#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8612#define IS_END() IS_lex_state(EXPR_END_ANY)
8613#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8614#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8615#define IS_LABEL_POSSIBLE() (\
8616 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8617 IS_ARG())
8618#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8619#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8620
8621static inline enum yytokentype
8622parser_string_term(struct parser_params *p, int func)
8623{
8624 xfree(p->lex.strterm);
8625 p->lex.strterm = 0;
8626 if (func & STR_FUNC_REGEXP) {
8627 set_yylval_num(regx_options(p));
8628 dispatch_scan_event(p, tREGEXP_END);
8629 SET_LEX_STATE(EXPR_END);
8630 return tREGEXP_END;
8631 }
8632 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8633 nextc(p);
8634 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8635 return tLABEL_END;
8636 }
8637 SET_LEX_STATE(EXPR_END);
8638 return tSTRING_END;
8639}
8640
8641static enum yytokentype
8642parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8643{
8644 int func = quote->func;
8645 int term = quote->term;
8646 int paren = quote->paren;
8647 int c, space = 0;
8648 rb_encoding *enc = p->enc;
8649 rb_encoding *base_enc = 0;
8650 rb_parser_string_t *lit;
8651
8652 if (func & STR_FUNC_TERM) {
8653 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8654 SET_LEX_STATE(EXPR_END);
8655 xfree(p->lex.strterm);
8656 p->lex.strterm = 0;
8657 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8658 }
8659 c = nextc(p);
8660 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8661 while (c != '\n' && ISSPACE(c = nextc(p)));
8662 space = 1;
8663 }
8664 if (func & STR_FUNC_LIST) {
8665 quote->func &= ~STR_FUNC_LIST;
8666 space = 1;
8667 }
8668 if (c == term && !quote->nest) {
8669 if (func & STR_FUNC_QWORDS) {
8670 quote->func |= STR_FUNC_TERM;
8671 pushback(p, c); /* dispatch the term at tSTRING_END */
8672 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8673 return ' ';
8674 }
8675 return parser_string_term(p, func);
8676 }
8677 if (space) {
8678 if (!ISSPACE(c)) pushback(p, c);
8679 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8680 return ' ';
8681 }
8682 newtok(p);
8683 if ((func & STR_FUNC_EXPAND) && c == '#') {
8684 enum yytokentype t = parser_peek_variable_name(p);
8685 if (t) return t;
8686 tokadd(p, '#');
8687 c = nextc(p);
8688 }
8689 pushback(p, c);
8690 if (tokadd_string(p, func, term, paren, &quote->nest,
8691 &enc, &base_enc) == -1) {
8692 if (p->eofp) {
8693#ifndef RIPPER
8694# define unterminated_literal(mesg) yyerror0(mesg)
8695#else
8696# define unterminated_literal(mesg) compile_error(p, mesg)
8697#endif
8698 literal_flush(p, p->lex.pcur);
8699 if (func & STR_FUNC_QWORDS) {
8700 /* no content to add, bailing out here */
8701 unterminated_literal("unterminated list meets end of file");
8702 xfree(p->lex.strterm);
8703 p->lex.strterm = 0;
8704 return tSTRING_END;
8705 }
8706 if (func & STR_FUNC_REGEXP) {
8707 unterminated_literal("unterminated regexp meets end of file");
8708 }
8709 else {
8710 unterminated_literal("unterminated string meets end of file");
8711 }
8712 quote->func |= STR_FUNC_TERM;
8713 }
8714 }
8715
8716 tokfix(p);
8717 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8718 set_yylval_str(lit);
8719 flush_string_content(p, enc, 0);
8720
8721 return tSTRING_CONTENT;
8722}
8723
8724static enum yytokentype
8725heredoc_identifier(struct parser_params *p)
8726{
8727 /*
8728 * term_len is length of `<<"END"` except `END`,
8729 * in this case term_len is 4 (<, <, " and ").
8730 */
8731 long len, offset = p->lex.pcur - p->lex.pbeg;
8732 int c = nextc(p), term, func = 0, quote = 0;
8733 enum yytokentype token = tSTRING_BEG;
8734 int indent = 0;
8735
8736 if (c == '-') {
8737 c = nextc(p);
8738 func = STR_FUNC_INDENT;
8739 offset++;
8740 }
8741 else if (c == '~') {
8742 c = nextc(p);
8743 func = STR_FUNC_INDENT;
8744 offset++;
8745 indent = INT_MAX;
8746 }
8747 switch (c) {
8748 case '\'':
8749 func |= str_squote; goto quoted;
8750 case '"':
8751 func |= str_dquote; goto quoted;
8752 case '`':
8753 token = tXSTRING_BEG;
8754 func |= str_xquote; goto quoted;
8755
8756 quoted:
8757 quote++;
8758 offset++;
8759 term = c;
8760 len = 0;
8761 while ((c = nextc(p)) != term) {
8762 if (c == -1 || c == '\r' || c == '\n') {
8763 yyerror0("unterminated here document identifier");
8764 return -1;
8765 }
8766 }
8767 break;
8768
8769 default:
8770 if (!parser_is_identchar(p)) {
8771 pushback(p, c);
8772 if (func & STR_FUNC_INDENT) {
8773 pushback(p, indent > 0 ? '~' : '-');
8774 }
8775 return 0;
8776 }
8777 func |= str_dquote;
8778 do {
8779 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8780 if (n < 0) return 0;
8781 p->lex.pcur += --n;
8782 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8783 pushback(p, c);
8784 break;
8785 }
8786
8787 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8788 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8789 yyerror0("too long here document identifier");
8790 dispatch_scan_event(p, tHEREDOC_BEG);
8791 lex_goto_eol(p);
8792
8793 p->lex.strterm = new_heredoc(p);
8794 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8795 here->offset = offset;
8796 here->sourceline = p->ruby_sourceline;
8797 here->length = (unsigned)len;
8798 here->quote = quote;
8799 here->func = func;
8800 here->lastline = p->lex.lastline;
8801
8802 token_flush(p);
8803 p->heredoc_indent = indent;
8804 p->heredoc_line_indent = 0;
8805 return token;
8806}
8807
8808static void
8809heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8810{
8811 rb_parser_string_t *line;
8812 rb_strterm_t *term = p->lex.strterm;
8813
8814 p->lex.strterm = 0;
8815 line = here->lastline;
8816 p->lex.lastline = line;
8817 p->lex.pbeg = PARSER_STRING_PTR(line);
8818 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8819 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8820 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8821 p->heredoc_end = p->ruby_sourceline;
8822 p->ruby_sourceline = (int)here->sourceline;
8823 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINTOR;
8824 p->eofp = 0;
8825 xfree(term);
8826}
8827
8828static int
8829dedent_string_column(const char *str, long len, int width)
8830{
8831 int i, col = 0;
8832
8833 for (i = 0; i < len && col < width; i++) {
8834 if (str[i] == ' ') {
8835 col++;
8836 }
8837 else if (str[i] == '\t') {
8838 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8839 if (n > width) break;
8840 col = n;
8841 }
8842 else {
8843 break;
8844 }
8845 }
8846
8847 return i;
8848}
8849
8850static int
8851dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8852{
8853 char *str;
8854 long len;
8855 int i;
8856
8857 len = PARSER_STRING_LEN(string);
8858 str = PARSER_STRING_PTR(string);
8859
8860 i = dedent_string_column(str, len, width);
8861 if (!i) return 0;
8862
8863 rb_parser_str_modify(string);
8864 str = PARSER_STRING_PTR(string);
8865 if (PARSER_STRING_LEN(string) != len)
8866 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8867 MEMMOVE(str, str + i, char, len - i);
8868 rb_parser_str_set_len(p, string, len - i);
8869 return i;
8870}
8871
8872static NODE *
8873heredoc_dedent(struct parser_params *p, NODE *root)
8874{
8875 NODE *node, *str_node, *prev_node;
8876 int indent = p->heredoc_indent;
8877 rb_parser_string_t *prev_lit = 0;
8878
8879 if (indent <= 0) return root;
8880 if (!root) return root;
8881
8882 prev_node = node = str_node = root;
8883 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8884
8885 while (str_node) {
8886 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8887 if (nd_fl_newline(str_node)) {
8888 dedent_string(p, lit, indent);
8889 }
8890 if (!prev_lit) {
8891 prev_lit = lit;
8892 }
8893 else if (!literal_concat0(p, prev_lit, lit)) {
8894 return 0;
8895 }
8896 else {
8897 NODE *end = RNODE_LIST(node)->as.nd_end;
8898 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8899 if (!node) {
8900 if (nd_type_p(prev_node, NODE_DSTR))
8901 nd_set_type(prev_node, NODE_STR);
8902 break;
8903 }
8904 RNODE_LIST(node)->as.nd_end = end;
8905 goto next_str;
8906 }
8907
8908 str_node = 0;
8909 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8910 next_str:
8911 if (!nd_type_p(node, NODE_LIST)) break;
8912 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8913 enum node_type type = nd_type(str_node);
8914 if (type == NODE_STR || type == NODE_DSTR) break;
8915 prev_lit = 0;
8916 str_node = 0;
8917 }
8918 }
8919 }
8920 return root;
8921}
8922
8923static int
8924whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8925{
8926 const char *beg = p->lex.pbeg;
8927 const char *ptr = p->lex.pend;
8928
8929 if (ptr - beg < len) return FALSE;
8930 if (ptr > beg && ptr[-1] == '\n') {
8931 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8932 if (ptr - beg < len) return FALSE;
8933 }
8934 if (strncmp(eos, ptr -= len, len)) return FALSE;
8935 if (indent) {
8936 while (beg < ptr && ISSPACE(*beg)) beg++;
8937 }
8938 return beg == ptr;
8939}
8940
8941static int
8942word_match_p(struct parser_params *p, const char *word, long len)
8943{
8944 if (strncmp(p->lex.pcur, word, len)) return 0;
8945 if (lex_eol_n_p(p, len)) return 1;
8946 int c = (unsigned char)p->lex.pcur[len];
8947 if (ISSPACE(c)) return 1;
8948 switch (c) {
8949 case '\0': case '\004': case '\032': return 1;
8950 }
8951 return 0;
8952}
8953
8954#define NUM_SUFFIX_R (1<<0)
8955#define NUM_SUFFIX_I (1<<1)
8956#define NUM_SUFFIX_ALL 3
8957
8958static int
8959number_literal_suffix(struct parser_params *p, int mask)
8960{
8961 int c, result = 0;
8962 const char *lastp = p->lex.pcur;
8963
8964 while ((c = nextc(p)) != -1) {
8965 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8966 result |= (mask & NUM_SUFFIX_I);
8967 mask &= ~NUM_SUFFIX_I;
8968 /* r after i, rational of complex is disallowed */
8969 mask &= ~NUM_SUFFIX_R;
8970 continue;
8971 }
8972 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8973 result |= (mask & NUM_SUFFIX_R);
8974 mask &= ~NUM_SUFFIX_R;
8975 continue;
8976 }
8977 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8978 p->lex.pcur = lastp;
8979 literal_flush(p, p->lex.pcur);
8980 return 0;
8981 }
8982 pushback(p, c);
8983 break;
8984 }
8985 return result;
8986}
8987
8988static enum yytokentype
8989set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8990{
8991 enum rb_numeric_type numeric_type = integer_literal;
8992
8993 if (type == tFLOAT) {
8994 numeric_type = float_literal;
8995 }
8996
8997 if (suffix & NUM_SUFFIX_R) {
8998 type = tRATIONAL;
8999 numeric_type = rational_literal;
9000 }
9001 if (suffix & NUM_SUFFIX_I) {
9002 type = tIMAGINARY;
9003 }
9004
9005 switch (type) {
9006 case tINTEGER:
9007 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
9008 break;
9009 case tFLOAT:
9010 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
9011 break;
9012 case tRATIONAL:
9013 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
9014 break;
9015 case tIMAGINARY:
9016 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
9017 (void)numeric_type; /* for ripper */
9018 break;
9019 default:
9020 rb_bug("unexpected token: %d", type);
9021 }
9022 SET_LEX_STATE(EXPR_END);
9023 return type;
9024}
9025
9026#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
9027static void
9028parser_dispatch_heredoc_end(struct parser_params *p, int line)
9029{
9030 if (has_delayed_token(p))
9031 dispatch_delayed_token(p, tSTRING_CONTENT);
9032
9033#ifdef RIPPER
9034 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
9035 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9036#else
9037 if (p->keep_tokens) {
9038 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9039 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9040 parser_append_tokens(p, str, tHEREDOC_END, line);
9041 }
9042#endif
9043
9044 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9045 lex_goto_eol(p);
9046 token_flush(p);
9047}
9048
9049static enum yytokentype
9050here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9051{
9052 int c, func, indent = 0;
9053 const char *eos, *ptr, *ptr_end;
9054 long len;
9055 rb_parser_string_t *str = 0;
9056 rb_encoding *enc = p->enc;
9057 rb_encoding *base_enc = 0;
9058 int bol;
9059#ifdef RIPPER
9060 VALUE s_value;
9061#endif
9062
9063 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9064 len = here->length;
9065 indent = (func = here->func) & STR_FUNC_INDENT;
9066
9067 if ((c = nextc(p)) == -1) {
9068 error:
9069#ifdef RIPPER
9070 if (!has_delayed_token(p)) {
9071 dispatch_scan_event(p, tSTRING_CONTENT);
9072 }
9073 else {
9074 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9075 if (!(func & STR_FUNC_REGEXP)) {
9076 int cr = ENC_CODERANGE_UNKNOWN;
9077 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9078 if (cr != ENC_CODERANGE_7BIT &&
9079 rb_is_usascii_enc(p->enc) &&
9080 enc != rb_utf8_encoding()) {
9081 enc = rb_ascii8bit_encoding();
9082 }
9083 }
9084 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9085 }
9086 dispatch_delayed_token(p, tSTRING_CONTENT);
9087 }
9088 lex_goto_eol(p);
9089#endif
9090 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9091 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9092 (int)len, eos);
9093 token_flush(p);
9094 SET_LEX_STATE(EXPR_END);
9095 return tSTRING_END;
9096 }
9097 bol = was_bol(p);
9098 if (!bol) {
9099 /* not beginning of line, cannot be the terminator */
9100 }
9101 else if (p->heredoc_line_indent == -1) {
9102 /* `heredoc_line_indent == -1` means
9103 * - "after an interpolation in the same line", or
9104 * - "in a continuing line"
9105 */
9106 p->heredoc_line_indent = 0;
9107 }
9108 else if (whole_match_p(p, eos, len, indent)) {
9109 dispatch_heredoc_end(p);
9110 restore:
9111 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9112 token_flush(p);
9113 SET_LEX_STATE(EXPR_END);
9114 return tSTRING_END;
9115 }
9116
9117 if (!(func & STR_FUNC_EXPAND)) {
9118 do {
9119 ptr = PARSER_STRING_PTR(p->lex.lastline);
9120 ptr_end = p->lex.pend;
9121 if (ptr_end > ptr) {
9122 switch (ptr_end[-1]) {
9123 case '\n':
9124 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9125 ptr_end++;
9126 break;
9127 }
9128 case '\r':
9129 --ptr_end;
9130 }
9131 }
9132
9133 if (p->heredoc_indent > 0) {
9134 long i = 0;
9135 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9136 i++;
9137 p->heredoc_line_indent = 0;
9138 }
9139
9140 if (str)
9141 parser_str_cat(str, ptr, ptr_end - ptr);
9142 else
9143 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9144 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9145 lex_goto_eol(p);
9146 if (p->heredoc_indent > 0) {
9147 goto flush_str;
9148 }
9149 if (nextc(p) == -1) {
9150 if (str) {
9151 rb_parser_string_free(p, str);
9152 str = 0;
9153 }
9154 goto error;
9155 }
9156 } while (!whole_match_p(p, eos, len, indent));
9157 }
9158 else {
9159 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9160 newtok(p);
9161 if (c == '#') {
9162 enum yytokentype t = parser_peek_variable_name(p);
9163 if (p->heredoc_line_indent != -1) {
9164 if (p->heredoc_indent > p->heredoc_line_indent) {
9165 p->heredoc_indent = p->heredoc_line_indent;
9166 }
9167 p->heredoc_line_indent = -1;
9168 }
9169 if (t) return t;
9170 tokadd(p, '#');
9171 c = nextc(p);
9172 }
9173 do {
9174 pushback(p, c);
9175 enc = p->enc;
9176 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9177 if (p->eofp) goto error;
9178 goto restore;
9179 }
9180 if (c != '\n') {
9181 if (c == '\\') p->heredoc_line_indent = -1;
9182 flush:
9183 str = STR_NEW3(tok(p), toklen(p), enc, func);
9184 flush_str:
9185 set_yylval_str(str);
9186#ifndef RIPPER
9187 if (bol) nd_set_fl_newline(yylval.node);
9188#endif
9189 flush_string_content(p, enc, 0);
9190 return tSTRING_CONTENT;
9191 }
9192 tokadd(p, nextc(p));
9193 if (p->heredoc_indent > 0) {
9194 lex_goto_eol(p);
9195 goto flush;
9196 }
9197 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9198 if ((c = nextc(p)) == -1) goto error;
9199 } while (!whole_match_p(p, eos, len, indent));
9200 str = STR_NEW3(tok(p), toklen(p), enc, func);
9201 }
9202 dispatch_heredoc_end(p);
9203 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9204 token_flush(p);
9205 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9206#ifdef RIPPER
9207 /* Preserve s_value for set_yylval_str */
9208 s_value = p->s_value;
9209#endif
9210 set_yylval_str(str);
9211#ifdef RIPPER
9212 set_parser_s_value(s_value);
9213#endif
9214
9215#ifndef RIPPER
9216 if (bol) nd_set_fl_newline(yylval.node);
9217#endif
9218 return tSTRING_CONTENT;
9219}
9220
9221#include "lex.c"
9222
9223static int
9224arg_ambiguous(struct parser_params *p, char c)
9225{
9226#ifndef RIPPER
9227 if (c == '/') {
9228 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after '%c' operator", WARN_I(c));
9229 }
9230 else {
9231 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9232 }
9233#else
9234 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9235#endif
9236 return TRUE;
9237}
9238
9239/* returns true value if formal argument error;
9240 * Qtrue, or error message if ripper */
9241static VALUE
9242formal_argument_error(struct parser_params *p, ID id)
9243{
9244 switch (id_type(id)) {
9245 case ID_LOCAL:
9246 break;
9247#ifndef RIPPER
9248# define ERR(mesg) (yyerror0(mesg), Qtrue)
9249#else
9250# define ERR(mesg) WARN_S(mesg)
9251#endif
9252 case ID_CONST:
9253 return ERR("formal argument cannot be a constant");
9254 case ID_INSTANCE:
9255 return ERR("formal argument cannot be an instance variable");
9256 case ID_GLOBAL:
9257 return ERR("formal argument cannot be a global variable");
9258 case ID_CLASS:
9259 return ERR("formal argument cannot be a class variable");
9260 default:
9261 return ERR("formal argument must be local variable");
9262#undef ERR
9263 }
9264 shadowing_lvar(p, id);
9265
9266 return Qfalse;
9267}
9268
9269static int
9270lvar_defined(struct parser_params *p, ID id)
9271{
9272 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9273}
9274
9275/* emacsen -*- hack */
9276static long
9277parser_encode_length(struct parser_params *p, const char *name, long len)
9278{
9279 long nlen;
9280
9281 if (len > 5 && name[nlen = len - 5] == '-') {
9282 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9283 return nlen;
9284 }
9285 if (len > 4 && name[nlen = len - 4] == '-') {
9286 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9287 return nlen;
9288 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9289 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9290 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9291 return nlen;
9292 }
9293 return len;
9294}
9295
9296static void
9297parser_set_encode(struct parser_params *p, const char *name)
9298{
9299 rb_encoding *enc;
9300 VALUE excargs[3];
9301 int idx = 0;
9302
9303 const char *wrong = 0;
9304 switch (*name) {
9305 case 'e': case 'E': wrong = "external"; break;
9306 case 'i': case 'I': wrong = "internal"; break;
9307 case 'f': case 'F': wrong = "filesystem"; break;
9308 case 'l': case 'L': wrong = "locale"; break;
9309 }
9310 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9311 idx = rb_enc_find_index(name);
9312 if (idx < 0) {
9313 unknown:
9314 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9315 error:
9316 excargs[0] = rb_eArgError;
9317 excargs[2] = rb_make_backtrace();
9318 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9319 VALUE exc = rb_make_exception(3, excargs);
9320 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9321
9322 rb_ast_free(p->ast);
9323 p->ast = NULL;
9324
9325 rb_exc_raise(exc);
9326 }
9327 enc = rb_enc_from_index(idx);
9328 if (!rb_enc_asciicompat(enc)) {
9329 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9330 goto error;
9331 }
9332 p->enc = enc;
9333#ifndef RIPPER
9334 if (p->debug_lines) {
9335 long i;
9336 for (i = 0; i < p->debug_lines->len; i++) {
9337 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9338 }
9339 }
9340#endif
9341}
9342
9343static bool
9344comment_at_top(struct parser_params *p)
9345{
9346 if (p->token_seen) return false;
9347 return (p->line_count == (p->has_shebang ? 2 : 1));
9348}
9349
9350typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9351typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9352
9353static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9354
9355static void
9356magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9357{
9358 if (!comment_at_top(p)) {
9359 return;
9360 }
9361 parser_set_encode(p, val);
9362}
9363
9364static int
9365parser_get_bool(struct parser_params *p, const char *name, const char *val)
9366{
9367 switch (*val) {
9368 case 't': case 'T':
9369 if (STRCASECMP(val, "true") == 0) {
9370 return TRUE;
9371 }
9372 break;
9373 case 'f': case 'F':
9374 if (STRCASECMP(val, "false") == 0) {
9375 return FALSE;
9376 }
9377 break;
9378 }
9379 return parser_invalid_pragma_value(p, name, val);
9380}
9381
9382static int
9383parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9384{
9385 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9386 return -1;
9387}
9388
9389static void
9390parser_set_token_info(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->token_info_enabled = b;
9394}
9395
9396static void
9397parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9398{
9399 int b;
9400
9401 if (p->token_seen) {
9402 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9403 return;
9404 }
9405
9406 b = parser_get_bool(p, name, val);
9407 if (b < 0) return;
9408
9409 p->frozen_string_literal = b;
9410}
9411
9412static void
9413parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9414{
9415 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9416 if (*s == ' ' || *s == '\t') continue;
9417 if (*s == '#') break;
9418 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9419 return;
9420 }
9421
9422 switch (*val) {
9423 case 'n': case 'N':
9424 if (STRCASECMP(val, "none") == 0) {
9425 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9426 return;
9427 }
9428 break;
9429 case 'l': case 'L':
9430 if (STRCASECMP(val, "literal") == 0) {
9431 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9432 return;
9433 }
9434 break;
9435 case 'e': case 'E':
9436 if (STRCASECMP(val, "experimental_copy") == 0) {
9437 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9438 return;
9439 }
9440 if (STRCASECMP(val, "experimental_everything") == 0) {
9441 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9442 return;
9443 }
9444 break;
9445 }
9446 parser_invalid_pragma_value(p, name, val);
9447}
9448
9449# if WARN_PAST_SCOPE
9450static void
9451parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9452{
9453 int b = parser_get_bool(p, name, val);
9454 if (b >= 0) p->past_scope_enabled = b;
9455}
9456# endif
9457
9458struct magic_comment {
9459 const char *name;
9460 rb_magic_comment_setter_t func;
9461 rb_magic_comment_length_t length;
9462};
9463
9464static const struct magic_comment magic_comments[] = {
9465 {"coding", magic_comment_encoding, parser_encode_length},
9466 {"encoding", magic_comment_encoding, parser_encode_length},
9467 {"frozen_string_literal", parser_set_frozen_string_literal},
9468 {"shareable_constant_value", parser_set_shareable_constant_value},
9469 {"warn_indent", parser_set_token_info},
9470# if WARN_PAST_SCOPE
9471 {"warn_past_scope", parser_set_past_scope},
9472# endif
9473};
9474
9475static const char *
9476magic_comment_marker(const char *str, long len)
9477{
9478 long i = 2;
9479
9480 while (i < len) {
9481 switch (str[i]) {
9482 case '-':
9483 if (str[i-1] == '*' && str[i-2] == '-') {
9484 return str + i + 1;
9485 }
9486 i += 2;
9487 break;
9488 case '*':
9489 if (i + 1 >= len) return 0;
9490 if (str[i+1] != '-') {
9491 i += 4;
9492 }
9493 else if (str[i-1] != '-') {
9494 i += 2;
9495 }
9496 else {
9497 return str + i + 2;
9498 }
9499 break;
9500 default:
9501 i += 3;
9502 break;
9503 }
9504 }
9505 return 0;
9506}
9507
9508static int
9509parser_magic_comment(struct parser_params *p, const char *str, long len)
9510{
9511 int indicator = 0;
9512 VALUE name = 0, val = 0;
9513 const char *beg, *end, *vbeg, *vend;
9514#define str_copy(_s, _p, _n) ((_s) \
9515 ? (void)(rb_str_resize((_s), (_n)), \
9516 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9517 : (void)((_s) = STR_NEW((_p), (_n))))
9518
9519 if (len <= 7) return FALSE;
9520 if (!!(beg = magic_comment_marker(str, len))) {
9521 if (!(end = magic_comment_marker(beg, str + len - beg)))
9522 return FALSE;
9523 indicator = TRUE;
9524 str = beg;
9525 len = end - beg - 3;
9526 }
9527
9528 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9529 while (len > 0) {
9530 const struct magic_comment *mc = magic_comments;
9531 char *s;
9532 int i;
9533 long n = 0;
9534
9535 for (; len > 0 && *str; str++, --len) {
9536 switch (*str) {
9537 case '\'': case '"': case ':': case ';':
9538 continue;
9539 }
9540 if (!ISSPACE(*str)) break;
9541 }
9542 for (beg = str; len > 0; str++, --len) {
9543 switch (*str) {
9544 case '\'': case '"': case ':': case ';':
9545 break;
9546 default:
9547 if (ISSPACE(*str)) break;
9548 continue;
9549 }
9550 break;
9551 }
9552 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9553 if (!len) break;
9554 if (*str != ':') {
9555 if (!indicator) return FALSE;
9556 continue;
9557 }
9558
9559 do str++; while (--len > 0 && ISSPACE(*str));
9560 if (!len) break;
9561 const char *tok_beg = str;
9562 if (*str == '"') {
9563 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9564 if (*str == '\\') {
9565 --len;
9566 ++str;
9567 }
9568 }
9569 vend = str;
9570 if (len) {
9571 --len;
9572 ++str;
9573 }
9574 }
9575 else {
9576 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9577 vend = str;
9578 }
9579 const char *tok_end = str;
9580 if (indicator) {
9581 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9582 }
9583 else {
9584 while (len > 0 && (ISSPACE(*str))) --len, str++;
9585 if (len) return FALSE;
9586 }
9587
9588 n = end - beg;
9589 str_copy(name, beg, n);
9590 s = RSTRING_PTR(name);
9591 for (i = 0; i < n; ++i) {
9592 if (s[i] == '-') s[i] = '_';
9593 }
9594 do {
9595 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9596 n = vend - vbeg;
9597 if (mc->length) {
9598 n = (*mc->length)(p, vbeg, n);
9599 }
9600 str_copy(val, vbeg, n);
9601 p->lex.ptok = tok_beg;
9602 p->lex.pcur = tok_end;
9603 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9604 break;
9605 }
9606 } while (++mc < magic_comments + numberof(magic_comments));
9607#ifdef RIPPER
9608 str_copy(val, vbeg, vend - vbeg);
9609 dispatch2(magic_comment, name, val);
9610#endif
9611 }
9612
9613 return TRUE;
9614}
9615
9616static void
9617set_file_encoding(struct parser_params *p, const char *str, const char *send)
9618{
9619 int sep = 0;
9620 const char *beg = str;
9621 VALUE s;
9622
9623 for (;;) {
9624 if (send - str <= 6) return;
9625 switch (str[6]) {
9626 case 'C': case 'c': str += 6; continue;
9627 case 'O': case 'o': str += 5; continue;
9628 case 'D': case 'd': str += 4; continue;
9629 case 'I': case 'i': str += 3; continue;
9630 case 'N': case 'n': str += 2; continue;
9631 case 'G': case 'g': str += 1; continue;
9632 case '=': case ':':
9633 sep = 1;
9634 str += 6;
9635 break;
9636 default:
9637 str += 6;
9638 if (ISSPACE(*str)) break;
9639 continue;
9640 }
9641 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9642 sep = 0;
9643 }
9644 for (;;) {
9645 do {
9646 if (++str >= send) return;
9647 } while (ISSPACE(*str));
9648 if (sep) break;
9649 if (*str != '=' && *str != ':') return;
9650 sep = 1;
9651 str++;
9652 }
9653 beg = str;
9654 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9655 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9656 p->lex.ptok = beg;
9657 p->lex.pcur = str;
9658 parser_set_encode(p, RSTRING_PTR(s));
9659 rb_str_resize(s, 0);
9660}
9661
9662static void
9663parser_prepare(struct parser_params *p)
9664{
9665 int c = nextc0(p, FALSE);
9666 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9667 switch (c) {
9668 case '#':
9669 if (peek(p, '!')) p->has_shebang = 1;
9670 break;
9671 case 0xef: /* UTF-8 BOM marker */
9672 if (!lex_eol_n_p(p, 2) &&
9673 (unsigned char)p->lex.pcur[0] == 0xbb &&
9674 (unsigned char)p->lex.pcur[1] == 0xbf) {
9675 p->enc = rb_utf8_encoding();
9676 p->lex.pcur += 2;
9677#ifndef RIPPER
9678 if (p->debug_lines) {
9679 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9680 }
9681#endif
9682 p->lex.pbeg = p->lex.pcur;
9683 token_flush(p);
9684 return;
9685 }
9686 break;
9687 case -1: /* end of script. */
9688 return;
9689 }
9690 pushback(p, c);
9691 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9692}
9693
9694#ifndef RIPPER
9695#define ambiguous_operator(tok, op, syn) ( \
9696 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9697 rb_warning0("even though it seems like "syn""))
9698#else
9699#define ambiguous_operator(tok, op, syn) \
9700 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9701#endif
9702#define warn_balanced(tok, op, syn) ((void) \
9703 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9704 space_seen && !ISSPACE(c) && \
9705 (ambiguous_operator(tok, op, syn), 0)), \
9706 (enum yytokentype)(tok))
9707
9708static enum yytokentype
9709no_digits(struct parser_params *p)
9710{
9711 yyerror0("numeric literal without digits");
9712 if (peek(p, '_')) nextc(p);
9713 /* dummy 0, for tUMINUS_NUM at numeric */
9714 return set_number_literal(p, tINTEGER, 0, 10, 0);
9715}
9716
9717static enum yytokentype
9718parse_numeric(struct parser_params *p, int c)
9719{
9720 int is_float, seen_point, seen_e, nondigit;
9721 int suffix;
9722
9723 is_float = seen_point = seen_e = nondigit = 0;
9724 SET_LEX_STATE(EXPR_END);
9725 newtok(p);
9726 if (c == '-' || c == '+') {
9727 tokadd(p, c);
9728 c = nextc(p);
9729 }
9730 if (c == '0') {
9731 int start = toklen(p);
9732 c = nextc(p);
9733 if (c == 'x' || c == 'X') {
9734 /* hexadecimal */
9735 c = nextc(p);
9736 if (c != -1 && ISXDIGIT(c)) {
9737 do {
9738 if (c == '_') {
9739 if (nondigit) break;
9740 nondigit = c;
9741 continue;
9742 }
9743 if (!ISXDIGIT(c)) break;
9744 nondigit = 0;
9745 tokadd(p, c);
9746 } while ((c = nextc(p)) != -1);
9747 }
9748 pushback(p, c);
9749 tokfix(p);
9750 if (toklen(p) == start) {
9751 return no_digits(p);
9752 }
9753 else if (nondigit) goto trailing_uc;
9754 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9755 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9756 }
9757 if (c == 'b' || c == 'B') {
9758 /* binary */
9759 c = nextc(p);
9760 if (c == '0' || c == '1') {
9761 do {
9762 if (c == '_') {
9763 if (nondigit) break;
9764 nondigit = c;
9765 continue;
9766 }
9767 if (c != '0' && c != '1') break;
9768 nondigit = 0;
9769 tokadd(p, c);
9770 } while ((c = nextc(p)) != -1);
9771 }
9772 pushback(p, c);
9773 tokfix(p);
9774 if (toklen(p) == start) {
9775 return no_digits(p);
9776 }
9777 else if (nondigit) goto trailing_uc;
9778 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9779 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9780 }
9781 if (c == 'd' || c == 'D') {
9782 /* decimal */
9783 c = nextc(p);
9784 if (c != -1 && ISDIGIT(c)) {
9785 do {
9786 if (c == '_') {
9787 if (nondigit) break;
9788 nondigit = c;
9789 continue;
9790 }
9791 if (!ISDIGIT(c)) break;
9792 nondigit = 0;
9793 tokadd(p, c);
9794 } while ((c = nextc(p)) != -1);
9795 }
9796 pushback(p, c);
9797 tokfix(p);
9798 if (toklen(p) == start) {
9799 return no_digits(p);
9800 }
9801 else if (nondigit) goto trailing_uc;
9802 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9803 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9804 }
9805 if (c == '_') {
9806 /* 0_0 */
9807 goto octal_number;
9808 }
9809 if (c == 'o' || c == 'O') {
9810 /* prefixed octal */
9811 c = nextc(p);
9812 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9813 tokfix(p);
9814 return no_digits(p);
9815 }
9816 }
9817 if (c >= '0' && c <= '7') {
9818 /* octal */
9819 octal_number:
9820 do {
9821 if (c == '_') {
9822 if (nondigit) break;
9823 nondigit = c;
9824 continue;
9825 }
9826 if (c < '0' || c > '9') break;
9827 if (c > '7') goto invalid_octal;
9828 nondigit = 0;
9829 tokadd(p, c);
9830 } while ((c = nextc(p)) != -1);
9831 if (toklen(p) > start) {
9832 pushback(p, c);
9833 tokfix(p);
9834 if (nondigit) goto trailing_uc;
9835 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9836 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9837 }
9838 if (nondigit) {
9839 pushback(p, c);
9840 goto trailing_uc;
9841 }
9842 }
9843 if (c > '7' && c <= '9') {
9844 invalid_octal:
9845 yyerror0("Invalid octal digit");
9846 }
9847 else if (c == '.' || c == 'e' || c == 'E') {
9848 tokadd(p, '0');
9849 }
9850 else {
9851 pushback(p, c);
9852 tokfix(p);
9853 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9854 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9855 }
9856 }
9857
9858 for (;;) {
9859 switch (c) {
9860 case '0': case '1': case '2': case '3': case '4':
9861 case '5': case '6': case '7': case '8': case '9':
9862 nondigit = 0;
9863 tokadd(p, c);
9864 break;
9865
9866 case '.':
9867 if (nondigit) goto trailing_uc;
9868 if (seen_point || seen_e) {
9869 goto decode_num;
9870 }
9871 else {
9872 int c0 = nextc(p);
9873 if (c0 == -1 || !ISDIGIT(c0)) {
9874 pushback(p, c0);
9875 goto decode_num;
9876 }
9877 c = c0;
9878 }
9879 seen_point = toklen(p);
9880 tokadd(p, '.');
9881 tokadd(p, c);
9882 is_float++;
9883 nondigit = 0;
9884 break;
9885
9886 case 'e':
9887 case 'E':
9888 if (nondigit) {
9889 pushback(p, c);
9890 c = nondigit;
9891 goto decode_num;
9892 }
9893 if (seen_e) {
9894 goto decode_num;
9895 }
9896 nondigit = c;
9897 c = nextc(p);
9898 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9899 pushback(p, c);
9900 c = nondigit;
9901 nondigit = 0;
9902 goto decode_num;
9903 }
9904 tokadd(p, nondigit);
9905 seen_e++;
9906 is_float++;
9907 tokadd(p, c);
9908 nondigit = (c == '-' || c == '+') ? c : 0;
9909 break;
9910
9911 case '_': /* `_' in number just ignored */
9912 if (nondigit) goto decode_num;
9913 nondigit = c;
9914 break;
9915
9916 default:
9917 goto decode_num;
9918 }
9919 c = nextc(p);
9920 }
9921
9922 decode_num:
9923 pushback(p, c);
9924 if (nondigit) {
9925 trailing_uc:
9926 literal_flush(p, p->lex.pcur - 1);
9927 YYLTYPE loc = RUBY_INIT_YYLLOC();
9928 compile_error(p, "trailing '%c' in number", nondigit);
9929 parser_show_error_line(p, &loc);
9930 }
9931 tokfix(p);
9932 if (is_float) {
9933 enum yytokentype type = tFLOAT;
9934
9935 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9936 if (suffix & NUM_SUFFIX_R) {
9937 type = tRATIONAL;
9938 }
9939 else {
9940 strtod(tok(p), 0);
9941 if (errno == ERANGE) {
9942 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9943 errno = 0;
9944 }
9945 }
9946 return set_number_literal(p, type, suffix, 0, seen_point);
9947 }
9948 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9949 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9950}
9951
9952static enum yytokentype
9953parse_qmark(struct parser_params *p, int space_seen)
9954{
9955 rb_encoding *enc;
9956 register int c;
9957 rb_parser_string_t *lit;
9958
9959 if (IS_END()) {
9960 SET_LEX_STATE(EXPR_VALUE);
9961 return '?';
9962 }
9963 c = nextc(p);
9964 if (c == -1) {
9965 compile_error(p, "incomplete character syntax");
9966 return 0;
9967 }
9968 if (rb_enc_isspace(c, p->enc)) {
9969 if (!IS_ARG()) {
9970 int c2 = escaped_control_code(c);
9971 if (c2) {
9972 WARN_SPACE_CHAR(c2, "?");
9973 }
9974 }
9975 ternary:
9976 pushback(p, c);
9977 SET_LEX_STATE(EXPR_VALUE);
9978 return '?';
9979 }
9980 newtok(p);
9981 enc = p->enc;
9982 if (!parser_isascii(p)) {
9983 if (tokadd_mbchar(p, c) == -1) return 0;
9984 }
9985 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
9986 !lex_eol_p(p) && is_identchar(p, p->lex.pcur, p->lex.pend, p->enc)) {
9987 if (space_seen) {
9988 const char *start = p->lex.pcur - 1, *ptr = start;
9989 do {
9990 int n = parser_precise_mbclen(p, ptr);
9991 if (n < 0) return -1;
9992 ptr += n;
9993 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9994 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9995 " a conditional operator, put a space after '?'",
9996 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9997 }
9998 goto ternary;
9999 }
10000 else if (c == '\\') {
10001 if (peek(p, 'u')) {
10002 nextc(p);
10003 enc = rb_utf8_encoding();
10004 tokadd_utf8(p, &enc, -1, 0, 0);
10005 }
10006 else if (!ISASCII(c = peekc(p)) && c != -1) {
10007 nextc(p);
10008 if (tokadd_mbchar(p, c) == -1) return 0;
10009 }
10010 else {
10011 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
10012 tokadd(p, c);
10013 }
10014 }
10015 else {
10016 tokadd(p, c);
10017 }
10018 tokfix(p);
10019 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
10020 set_yylval_str(lit);
10021 SET_LEX_STATE(EXPR_END);
10022 return tCHAR;
10023}
10024
10025static enum yytokentype
10026parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
10027{
10028 register int c;
10029 const char *ptok = p->lex.pcur;
10030
10031 if (IS_BEG()) {
10032 int term;
10033 int paren;
10034
10035 c = nextc(p);
10036 quotation:
10037 if (c == -1) goto unterminated;
10038 if (!ISALNUM(c)) {
10039 term = c;
10040 if (!ISASCII(c)) goto unknown;
10041 c = 'Q';
10042 }
10043 else {
10044 term = nextc(p);
10045 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10046 unknown:
10047 pushback(p, term);
10048 c = parser_precise_mbclen(p, p->lex.pcur);
10049 if (c < 0) return 0;
10050 p->lex.pcur += c;
10051 yyerror0("unknown type of %string");
10052 return 0;
10053 }
10054 }
10055 if (term == -1) {
10056 unterminated:
10057 compile_error(p, "unterminated quoted string meets end of file");
10058 return 0;
10059 }
10060 paren = term;
10061 if (term == '(') term = ')';
10062 else if (term == '[') term = ']';
10063 else if (term == '{') term = '}';
10064 else if (term == '<') term = '>';
10065 else paren = 0;
10066
10067 p->lex.ptok = ptok-1;
10068 switch (c) {
10069 case 'Q':
10070 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10071 return tSTRING_BEG;
10072
10073 case 'q':
10074 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10075 return tSTRING_BEG;
10076
10077 case 'W':
10078 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10079 return tWORDS_BEG;
10080
10081 case 'w':
10082 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10083 return tQWORDS_BEG;
10084
10085 case 'I':
10086 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10087 return tSYMBOLS_BEG;
10088
10089 case 'i':
10090 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10091 return tQSYMBOLS_BEG;
10092
10093 case 'x':
10094 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10095 return tXSTRING_BEG;
10096
10097 case 'r':
10098 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10099 return tREGEXP_BEG;
10100
10101 case 's':
10102 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10103 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10104 return tSYMBEG;
10105
10106 default:
10107 yyerror0("unknown type of %string");
10108 return 0;
10109 }
10110 }
10111 if ((c = nextc(p)) == '=') {
10112 set_yylval_id('%');
10113 SET_LEX_STATE(EXPR_BEG);
10114 return tOP_ASGN;
10115 }
10116 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10117 goto quotation;
10118 }
10119 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10120 pushback(p, c);
10121 return warn_balanced('%', "%%", "string literal");
10122}
10123
10124static int
10125tokadd_ident(struct parser_params *p, int c)
10126{
10127 do {
10128 if (tokadd_mbchar(p, c) == -1) return -1;
10129 c = nextc(p);
10130 } while (parser_is_identchar(p));
10131 pushback(p, c);
10132 return 0;
10133}
10134
10135static ID
10136tokenize_ident(struct parser_params *p)
10137{
10138 ID ident = TOK_INTERN();
10139
10140 set_yylval_name(ident);
10141
10142 return ident;
10143}
10144
10145static int
10146parse_numvar(struct parser_params *p)
10147{
10148 size_t len;
10149 int overflow;
10150 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10151 const unsigned long nth_ref_max =
10152 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10153 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10154 * turned into a Fixnum, in compile.c */
10155
10156 if (overflow || n > nth_ref_max) {
10157 /* compile_error()? */
10158 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10159 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10160 }
10161 else {
10162 return (int)n;
10163 }
10164}
10165
10166static enum yytokentype
10167parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10168{
10169 const char *ptr = p->lex.pcur;
10170 register int c;
10171
10172 SET_LEX_STATE(EXPR_END);
10173 p->lex.ptok = ptr - 1; /* from '$' */
10174 newtok(p);
10175 c = nextc(p);
10176 switch (c) {
10177 case '_': /* $_: last read line string */
10178 c = nextc(p);
10179 if (parser_is_identchar(p)) {
10180 tokadd(p, '$');
10181 tokadd(p, '_');
10182 break;
10183 }
10184 pushback(p, c);
10185 c = '_';
10186 /* fall through */
10187 case '~': /* $~: match-data */
10188 case '*': /* $*: argv */
10189 case '$': /* $$: pid */
10190 case '?': /* $?: last status */
10191 case '!': /* $!: error string */
10192 case '@': /* $@: error position */
10193 case '/': /* $/: input record separator */
10194 case '\\': /* $\: output record separator */
10195 case ';': /* $;: field separator */
10196 case ',': /* $,: output field separator */
10197 case '.': /* $.: last read line number */
10198 case '=': /* $=: ignorecase */
10199 case ':': /* $:: load path */
10200 case '<': /* $<: default input handle */
10201 case '>': /* $>: default output handle */
10202 case '\"': /* $": already loaded files */
10203 tokadd(p, '$');
10204 tokadd(p, c);
10205 goto gvar;
10206
10207 case '-':
10208 tokadd(p, '$');
10209 tokadd(p, c);
10210 c = nextc(p);
10211 if (parser_is_identchar(p)) {
10212 if (tokadd_mbchar(p, c) == -1) return 0;
10213 }
10214 else {
10215 pushback(p, c);
10216 pushback(p, '-');
10217 return '$';
10218 }
10219 gvar:
10220 tokenize_ident(p);
10221 return tGVAR;
10222
10223 case '&': /* $&: last match */
10224 case '`': /* $`: string before last match */
10225 case '\'': /* $': string after last match */
10226 case '+': /* $+: string matches last paren. */
10227 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10228 tokadd(p, '$');
10229 tokadd(p, c);
10230 goto gvar;
10231 }
10232 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10233 return tBACK_REF;
10234
10235 case '1': case '2': case '3':
10236 case '4': case '5': case '6':
10237 case '7': case '8': case '9':
10238 tokadd(p, '$');
10239 do {
10240 tokadd(p, c);
10241 c = nextc(p);
10242 } while (c != -1 && ISDIGIT(c));
10243 pushback(p, c);
10244 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10245 tokfix(p);
10246 c = parse_numvar(p);
10247 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10248 return tNTH_REF;
10249
10250 default:
10251 if (!parser_is_identchar(p)) {
10252 YYLTYPE loc = RUBY_INIT_YYLLOC();
10253 if (c == -1 || ISSPACE(c)) {
10254 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10255 }
10256 else {
10257 pushback(p, c);
10258 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10259 }
10260 parser_show_error_line(p, &loc);
10261 set_yylval_noname();
10262 return tGVAR;
10263 }
10264 /* fall through */
10265 case '0':
10266 tokadd(p, '$');
10267 }
10268
10269 if (tokadd_ident(p, c)) return 0;
10270 SET_LEX_STATE(EXPR_END);
10271 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10272 tokenize_ident(p);
10273 }
10274 else {
10275 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10276 set_yylval_noname();
10277 }
10278 return tGVAR;
10279}
10280
10281static bool
10282parser_numbered_param(struct parser_params *p, int n)
10283{
10284 if (n < 0) return false;
10285
10286 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10287 return false;
10288 }
10289 if (p->max_numparam == ORDINAL_PARAM) {
10290 compile_error(p, "ordinary parameter is defined");
10291 return false;
10292 }
10293 struct vtable *args = p->lvtbl->args;
10294 if (p->max_numparam < n) {
10295 p->max_numparam = n;
10296 }
10297 while (n > args->pos) {
10298 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10299 }
10300 return true;
10301}
10302
10303static enum yytokentype
10304parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10305{
10306 const char *ptr = p->lex.pcur;
10307 enum yytokentype result = tIVAR;
10308 register int c = nextc(p);
10309 YYLTYPE loc;
10310
10311 p->lex.ptok = ptr - 1; /* from '@' */
10312 newtok(p);
10313 tokadd(p, '@');
10314 if (c == '@') {
10315 result = tCVAR;
10316 tokadd(p, '@');
10317 c = nextc(p);
10318 }
10319 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10320 if (c == -1 || !parser_is_identchar(p)) {
10321 pushback(p, c);
10322 RUBY_SET_YYLLOC(loc);
10323 if (result == tIVAR) {
10324 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10325 }
10326 else {
10327 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10328 }
10329 parser_show_error_line(p, &loc);
10330 set_yylval_noname();
10331 SET_LEX_STATE(EXPR_END);
10332 return result;
10333 }
10334 else if (ISDIGIT(c)) {
10335 pushback(p, c);
10336 RUBY_SET_YYLLOC(loc);
10337 if (result == tIVAR) {
10338 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10339 }
10340 else {
10341 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10342 }
10343 parser_show_error_line(p, &loc);
10344 set_yylval_noname();
10345 SET_LEX_STATE(EXPR_END);
10346 return result;
10347 }
10348
10349 if (tokadd_ident(p, c)) return 0;
10350 tokenize_ident(p);
10351 return result;
10352}
10353
10354static enum yytokentype
10355parse_ident(struct parser_params *p, int c, int cmd_state)
10356{
10357 enum yytokentype result;
10358 bool is_ascii = true;
10359 const enum lex_state_e last_state = p->lex.state;
10360 ID ident;
10361 int enforce_keyword_end = 0;
10362
10363 do {
10364 if (!ISASCII(c)) is_ascii = false;
10365 if (tokadd_mbchar(p, c) == -1) return 0;
10366 c = nextc(p);
10367 } while (parser_is_identchar(p));
10368 if ((c == '!' || c == '?') && !peek(p, '=')) {
10369 result = tFID;
10370 tokadd(p, c);
10371 }
10372 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10373 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10374 result = tIDENTIFIER;
10375 tokadd(p, c);
10376 }
10377 else {
10378 result = tCONSTANT; /* assume provisionally */
10379 pushback(p, c);
10380 }
10381 tokfix(p);
10382
10383 if (IS_LABEL_POSSIBLE()) {
10384 if (IS_LABEL_SUFFIX(0)) {
10385 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10386 nextc(p);
10387 tokenize_ident(p);
10388 return tLABEL;
10389 }
10390 }
10391
10392#ifndef RIPPER
10393 if (peek_end_expect_token_locations(p)) {
10394 const rb_code_position_t *end_pos;
10395 int lineno, column;
10396 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10397
10398 end_pos = peek_end_expect_token_locations(p)->pos;
10399 lineno = end_pos->lineno;
10400 column = end_pos->column;
10401
10402 if (p->debug) {
10403 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10404 p->ruby_sourceline, beg_pos, lineno, column);
10405 }
10406
10407 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10408 const struct kwtable *kw;
10409
10410 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10411 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10412 enforce_keyword_end = 1;
10413 }
10414 }
10415 }
10416#endif
10417
10418 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10419 const struct kwtable *kw;
10420
10421 /* See if it is a reserved word. */
10422 kw = rb_reserved_word(tok(p), toklen(p));
10423 if (kw) {
10424 enum lex_state_e state = p->lex.state;
10425 if (IS_lex_state_for(state, EXPR_FNAME)) {
10426 SET_LEX_STATE(EXPR_ENDFN);
10427 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10428 return kw->id[0];
10429 }
10430 SET_LEX_STATE(kw->state);
10431 if (IS_lex_state(EXPR_BEG)) {
10432 p->command_start = TRUE;
10433 }
10434 if (kw->id[0] == keyword_do) {
10435 if (lambda_beginning_p()) {
10436 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10437 return keyword_do_LAMBDA;
10438 }
10439 if (COND_P()) return keyword_do_cond;
10440 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10441 return keyword_do_block;
10442 return keyword_do;
10443 }
10444 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10445 return kw->id[0];
10446 else {
10447 if (kw->id[0] != kw->id[1])
10448 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10449 return kw->id[1];
10450 }
10451 }
10452 }
10453
10454 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10455 if (cmd_state) {
10456 SET_LEX_STATE(EXPR_CMDARG);
10457 }
10458 else {
10459 SET_LEX_STATE(EXPR_ARG);
10460 }
10461 }
10462 else if (p->lex.state == EXPR_FNAME) {
10463 SET_LEX_STATE(EXPR_ENDFN);
10464 }
10465 else {
10466 SET_LEX_STATE(EXPR_END);
10467 }
10468
10469 ident = tokenize_ident(p);
10470 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10471 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10472 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10473 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10474 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10475 }
10476 return result;
10477}
10478
10479static void
10480warn_cr(struct parser_params *p)
10481{
10482 if (!p->cr_seen) {
10483 p->cr_seen = TRUE;
10484 /* carried over with p->lex.nextline for nextc() */
10485 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10486 }
10487}
10488
10489static enum yytokentype
10490parser_yylex(struct parser_params *p)
10491{
10492 register int c;
10493 int space_seen = 0;
10494 int cmd_state;
10495 int label;
10496 enum lex_state_e last_state;
10497 int fallthru = FALSE;
10498 int token_seen = p->token_seen;
10499
10500 if (p->lex.strterm) {
10501 if (strterm_is_heredoc(p->lex.strterm)) {
10502 token_flush(p);
10503 return here_document(p, &p->lex.strterm->u.heredoc);
10504 }
10505 else {
10506 token_flush(p);
10507 return parse_string(p, &p->lex.strterm->u.literal);
10508 }
10509 }
10510 cmd_state = p->command_start;
10511 p->command_start = FALSE;
10512 p->token_seen = TRUE;
10513#ifndef RIPPER
10514 token_flush(p);
10515#endif
10516 retry:
10517 last_state = p->lex.state;
10518 switch (c = nextc(p)) {
10519 case '\0': /* NUL */
10520 case '\004': /* ^D */
10521 case '\032': /* ^Z */
10522 case -1: /* end of script. */
10523 p->eofp = 1;
10524#ifndef RIPPER
10525 if (p->end_expect_token_locations) {
10526 pop_end_expect_token_locations(p);
10527 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10528 return tDUMNY_END;
10529 }
10530#endif
10531 /* Set location for end-of-input because dispatch_scan_event is not called. */
10532 RUBY_SET_YYLLOC(*p->yylloc);
10533 return END_OF_INPUT;
10534
10535 /* white spaces */
10536 case '\r':
10537 warn_cr(p);
10538 /* fall through */
10539 case ' ': case '\t': case '\f':
10540 case '\13': /* '\v' */
10541 space_seen = 1;
10542 while ((c = nextc(p))) {
10543 switch (c) {
10544 case '\r':
10545 warn_cr(p);
10546 /* fall through */
10547 case ' ': case '\t': case '\f':
10548 case '\13': /* '\v' */
10549 break;
10550 default:
10551 goto outofloop;
10552 }
10553 }
10554 outofloop:
10555 pushback(p, c);
10556 dispatch_scan_event(p, tSP);
10557#ifndef RIPPER
10558 token_flush(p);
10559#endif
10560 goto retry;
10561
10562 case '#': /* it's a comment */
10563 p->token_seen = token_seen;
10564 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10565 /* no magic_comment in shebang line */
10566 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10567 if (comment_at_top(p)) {
10568 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10569 }
10570 }
10571 p->lex.pcur = pcur, p->lex.ptok = ptok;
10572 lex_goto_eol(p);
10573 dispatch_scan_event(p, tCOMMENT);
10574 fallthru = TRUE;
10575 /* fall through */
10576 case '\n':
10577 p->token_seen = token_seen;
10578 rb_parser_string_t *prevline = p->lex.lastline;
10579 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10580 !IS_lex_state(EXPR_LABELED));
10581 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10582 if (!fallthru) {
10583 dispatch_scan_event(p, tIGNORED_NL);
10584 }
10585 fallthru = FALSE;
10586 if (!c && p->ctxt.in_kwarg) {
10587 goto normal_newline;
10588 }
10589 goto retry;
10590 }
10591 while (1) {
10592 switch (c = nextc(p)) {
10593 case ' ': case '\t': case '\f': case '\r':
10594 case '\13': /* '\v' */
10595 space_seen = 1;
10596 break;
10597 case '#':
10598 pushback(p, c);
10599 if (space_seen) {
10600 dispatch_scan_event(p, tSP);
10601 token_flush(p);
10602 }
10603 goto retry;
10604 case '&':
10605 case '.': {
10606 dispatch_delayed_token(p, tIGNORED_NL);
10607 if (peek(p, '.') == (c == '&')) {
10608 pushback(p, c);
10609 dispatch_scan_event(p, tSP);
10610 goto retry;
10611 }
10612 }
10613 default:
10614 p->ruby_sourceline--;
10615 p->lex.nextline = p->lex.lastline;
10616 set_lastline(p, prevline);
10617 case -1: /* EOF no decrement*/
10618 if (c == -1 && space_seen) {
10619 dispatch_scan_event(p, tSP);
10620 }
10621 lex_goto_eol(p);
10622 if (c != -1) {
10623 token_flush(p);
10624 RUBY_SET_YYLLOC(*p->yylloc);
10625 }
10626 goto normal_newline;
10627 }
10628 }
10629 normal_newline:
10630 p->command_start = TRUE;
10631 SET_LEX_STATE(EXPR_BEG);
10632 return '\n';
10633
10634 case '*':
10635 if ((c = nextc(p)) == '*') {
10636 if ((c = nextc(p)) == '=') {
10637 set_yylval_id(idPow);
10638 SET_LEX_STATE(EXPR_BEG);
10639 return tOP_ASGN;
10640 }
10641 pushback(p, c);
10642 if (IS_SPCARG(c)) {
10643 rb_warning0("'**' interpreted as argument prefix");
10644 c = tDSTAR;
10645 }
10646 else if (IS_BEG()) {
10647 c = tDSTAR;
10648 }
10649 else {
10650 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10651 }
10652 }
10653 else {
10654 if (c == '=') {
10655 set_yylval_id('*');
10656 SET_LEX_STATE(EXPR_BEG);
10657 return tOP_ASGN;
10658 }
10659 pushback(p, c);
10660 if (IS_SPCARG(c)) {
10661 rb_warning0("'*' interpreted as argument prefix");
10662 c = tSTAR;
10663 }
10664 else if (IS_BEG()) {
10665 c = tSTAR;
10666 }
10667 else {
10668 c = warn_balanced('*', "*", "argument prefix");
10669 }
10670 }
10671 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10672 return c;
10673
10674 case '!':
10675 c = nextc(p);
10676 if (IS_AFTER_OPERATOR()) {
10677 SET_LEX_STATE(EXPR_ARG);
10678 if (c == '@') {
10679 return '!';
10680 }
10681 }
10682 else {
10683 SET_LEX_STATE(EXPR_BEG);
10684 }
10685 if (c == '=') {
10686 return tNEQ;
10687 }
10688 if (c == '~') {
10689 return tNMATCH;
10690 }
10691 pushback(p, c);
10692 return '!';
10693
10694 case '=':
10695 if (was_bol(p)) {
10696 /* skip embedded rd document */
10697 if (word_match_p(p, "begin", 5)) {
10698 int first_p = TRUE;
10699
10700 lex_goto_eol(p);
10701 dispatch_scan_event(p, tEMBDOC_BEG);
10702 for (;;) {
10703 lex_goto_eol(p);
10704 if (!first_p) {
10705 dispatch_scan_event(p, tEMBDOC);
10706 }
10707 first_p = FALSE;
10708 c = nextc(p);
10709 if (c == -1) {
10710 compile_error(p, "embedded document meets end of file");
10711 return END_OF_INPUT;
10712 }
10713 if (c == '=' && word_match_p(p, "end", 3)) {
10714 break;
10715 }
10716 pushback(p, c);
10717 }
10718 lex_goto_eol(p);
10719 dispatch_scan_event(p, tEMBDOC_END);
10720 goto retry;
10721 }
10722 }
10723
10724 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10725 if ((c = nextc(p)) == '=') {
10726 if ((c = nextc(p)) == '=') {
10727 return tEQQ;
10728 }
10729 pushback(p, c);
10730 return tEQ;
10731 }
10732 if (c == '~') {
10733 return tMATCH;
10734 }
10735 else if (c == '>') {
10736 return tASSOC;
10737 }
10738 pushback(p, c);
10739 return '=';
10740
10741 case '<':
10742 c = nextc(p);
10743 if (c == '<' &&
10744 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10745 !IS_END() &&
10746 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10747 enum yytokentype token = heredoc_identifier(p);
10748 if (token) return token < 0 ? 0 : token;
10749 }
10750 if (IS_AFTER_OPERATOR()) {
10751 SET_LEX_STATE(EXPR_ARG);
10752 }
10753 else {
10754 if (IS_lex_state(EXPR_CLASS))
10755 p->command_start = TRUE;
10756 SET_LEX_STATE(EXPR_BEG);
10757 }
10758 if (c == '=') {
10759 if ((c = nextc(p)) == '>') {
10760 return tCMP;
10761 }
10762 pushback(p, c);
10763 return tLEQ;
10764 }
10765 if (c == '<') {
10766 if ((c = nextc(p)) == '=') {
10767 set_yylval_id(idLTLT);
10768 SET_LEX_STATE(EXPR_BEG);
10769 return tOP_ASGN;
10770 }
10771 pushback(p, c);
10772 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10773 }
10774 pushback(p, c);
10775 return '<';
10776
10777 case '>':
10778 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10779 if ((c = nextc(p)) == '=') {
10780 return tGEQ;
10781 }
10782 if (c == '>') {
10783 if ((c = nextc(p)) == '=') {
10784 set_yylval_id(idGTGT);
10785 SET_LEX_STATE(EXPR_BEG);
10786 return tOP_ASGN;
10787 }
10788 pushback(p, c);
10789 return tRSHFT;
10790 }
10791 pushback(p, c);
10792 return '>';
10793
10794 case '"':
10795 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10796 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10797 p->lex.ptok = p->lex.pcur-1;
10798 return tSTRING_BEG;
10799
10800 case '`':
10801 if (IS_lex_state(EXPR_FNAME)) {
10802 SET_LEX_STATE(EXPR_ENDFN);
10803 return c;
10804 }
10805 if (IS_lex_state(EXPR_DOT)) {
10806 if (cmd_state)
10807 SET_LEX_STATE(EXPR_CMDARG);
10808 else
10809 SET_LEX_STATE(EXPR_ARG);
10810 return c;
10811 }
10812 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10813 return tXSTRING_BEG;
10814
10815 case '\'':
10816 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10817 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10818 p->lex.ptok = p->lex.pcur-1;
10819 return tSTRING_BEG;
10820
10821 case '?':
10822 return parse_qmark(p, space_seen);
10823
10824 case '&':
10825 if ((c = nextc(p)) == '&') {
10826 SET_LEX_STATE(EXPR_BEG);
10827 if ((c = nextc(p)) == '=') {
10828 set_yylval_id(idANDOP);
10829 SET_LEX_STATE(EXPR_BEG);
10830 return tOP_ASGN;
10831 }
10832 pushback(p, c);
10833 return tANDOP;
10834 }
10835 else if (c == '=') {
10836 set_yylval_id('&');
10837 SET_LEX_STATE(EXPR_BEG);
10838 return tOP_ASGN;
10839 }
10840 else if (c == '.') {
10841 set_yylval_id(idANDDOT);
10842 SET_LEX_STATE(EXPR_DOT);
10843 return tANDDOT;
10844 }
10845 pushback(p, c);
10846 if (IS_SPCARG(c)) {
10847 if ((c != ':') ||
10848 (c = peekc_n(p, 1)) == -1 ||
10849 !(c == '\'' || c == '"' ||
10850 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10851 rb_warning0("'&' interpreted as argument prefix");
10852 }
10853 c = tAMPER;
10854 }
10855 else if (IS_BEG()) {
10856 c = tAMPER;
10857 }
10858 else {
10859 c = warn_balanced('&', "&", "argument prefix");
10860 }
10861 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10862 return c;
10863
10864 case '|':
10865 if ((c = nextc(p)) == '|') {
10866 SET_LEX_STATE(EXPR_BEG);
10867 if ((c = nextc(p)) == '=') {
10868 set_yylval_id(idOROP);
10869 SET_LEX_STATE(EXPR_BEG);
10870 return tOP_ASGN;
10871 }
10872 pushback(p, c);
10873 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10874 c = '|';
10875 pushback(p, '|');
10876 return c;
10877 }
10878 return tOROP;
10879 }
10880 if (c == '=') {
10881 set_yylval_id('|');
10882 SET_LEX_STATE(EXPR_BEG);
10883 return tOP_ASGN;
10884 }
10885 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10886 pushback(p, c);
10887 return '|';
10888
10889 case '+':
10890 c = nextc(p);
10891 if (IS_AFTER_OPERATOR()) {
10892 SET_LEX_STATE(EXPR_ARG);
10893 if (c == '@') {
10894 return tUPLUS;
10895 }
10896 pushback(p, c);
10897 return '+';
10898 }
10899 if (c == '=') {
10900 set_yylval_id('+');
10901 SET_LEX_STATE(EXPR_BEG);
10902 return tOP_ASGN;
10903 }
10904 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10905 SET_LEX_STATE(EXPR_BEG);
10906 pushback(p, c);
10907 if (c != -1 && ISDIGIT(c)) {
10908 return parse_numeric(p, '+');
10909 }
10910 return tUPLUS;
10911 }
10912 SET_LEX_STATE(EXPR_BEG);
10913 pushback(p, c);
10914 return warn_balanced('+', "+", "unary operator");
10915
10916 case '-':
10917 c = nextc(p);
10918 if (IS_AFTER_OPERATOR()) {
10919 SET_LEX_STATE(EXPR_ARG);
10920 if (c == '@') {
10921 return tUMINUS;
10922 }
10923 pushback(p, c);
10924 return '-';
10925 }
10926 if (c == '=') {
10927 set_yylval_id('-');
10928 SET_LEX_STATE(EXPR_BEG);
10929 return tOP_ASGN;
10930 }
10931 if (c == '>') {
10932 SET_LEX_STATE(EXPR_ENDFN);
10933 yylval.num = p->lex.lpar_beg;
10934 p->lex.lpar_beg = p->lex.paren_nest;
10935 return tLAMBDA;
10936 }
10937 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10938 SET_LEX_STATE(EXPR_BEG);
10939 pushback(p, c);
10940 if (c != -1 && ISDIGIT(c)) {
10941 return tUMINUS_NUM;
10942 }
10943 return tUMINUS;
10944 }
10945 SET_LEX_STATE(EXPR_BEG);
10946 pushback(p, c);
10947 return warn_balanced('-', "-", "unary operator");
10948
10949 case '.': {
10950 int is_beg = IS_BEG();
10951 SET_LEX_STATE(EXPR_BEG);
10952 if ((c = nextc(p)) == '.') {
10953 if ((c = nextc(p)) == '.') {
10954 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10955 SET_LEX_STATE(EXPR_ENDARG);
10956 return tBDOT3;
10957 }
10958 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10959 rb_warn0("... at EOL, should be parenthesized?");
10960 }
10961 return is_beg ? tBDOT3 : tDOT3;
10962 }
10963 pushback(p, c);
10964 return is_beg ? tBDOT2 : tDOT2;
10965 }
10966 pushback(p, c);
10967 if (c != -1 && ISDIGIT(c)) {
10968 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10969 parse_numeric(p, '.');
10970 if (ISDIGIT(prev)) {
10971 yyerror0("unexpected fraction part after numeric literal");
10972 }
10973 else {
10974 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10975 }
10976 SET_LEX_STATE(EXPR_END);
10977 p->lex.ptok = p->lex.pcur;
10978 goto retry;
10979 }
10980 set_yylval_id('.');
10981 SET_LEX_STATE(EXPR_DOT);
10982 return '.';
10983 }
10984
10985 case '0': case '1': case '2': case '3': case '4':
10986 case '5': case '6': case '7': case '8': case '9':
10987 return parse_numeric(p, c);
10988
10989 case ')':
10990 COND_POP();
10991 CMDARG_POP();
10992 SET_LEX_STATE(EXPR_ENDFN);
10993 p->lex.paren_nest--;
10994 return c;
10995
10996 case ']':
10997 COND_POP();
10998 CMDARG_POP();
10999 SET_LEX_STATE(EXPR_END);
11000 p->lex.paren_nest--;
11001 return c;
11002
11003 case '}':
11004 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
11005 if (!p->lex.brace_nest--) return tSTRING_DEND;
11006 COND_POP();
11007 CMDARG_POP();
11008 SET_LEX_STATE(EXPR_END);
11009 p->lex.paren_nest--;
11010 return c;
11011
11012 case ':':
11013 c = nextc(p);
11014 if (c == ':') {
11015 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
11016 SET_LEX_STATE(EXPR_BEG);
11017 return tCOLON3;
11018 }
11019 set_yylval_id(idCOLON2);
11020 SET_LEX_STATE(EXPR_DOT);
11021 return tCOLON2;
11022 }
11023 if (IS_END() || ISSPACE(c) || c == '#') {
11024 pushback(p, c);
11025 c = warn_balanced(':', ":", "symbol literal");
11026 SET_LEX_STATE(EXPR_BEG);
11027 return c;
11028 }
11029 switch (c) {
11030 case '\'':
11031 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11032 break;
11033 case '"':
11034 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11035 break;
11036 default:
11037 pushback(p, c);
11038 break;
11039 }
11040 SET_LEX_STATE(EXPR_FNAME);
11041 return tSYMBEG;
11042
11043 case '/':
11044 if (IS_BEG()) {
11045 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11046 return tREGEXP_BEG;
11047 }
11048 if ((c = nextc(p)) == '=') {
11049 set_yylval_id('/');
11050 SET_LEX_STATE(EXPR_BEG);
11051 return tOP_ASGN;
11052 }
11053 pushback(p, c);
11054 if (IS_SPCARG(c)) {
11055 arg_ambiguous(p, '/');
11056 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11057 return tREGEXP_BEG;
11058 }
11059 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11060 return warn_balanced('/', "/", "regexp literal");
11061
11062 case '^':
11063 if ((c = nextc(p)) == '=') {
11064 set_yylval_id('^');
11065 SET_LEX_STATE(EXPR_BEG);
11066 return tOP_ASGN;
11067 }
11068 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11069 pushback(p, c);
11070 return '^';
11071
11072 case ';':
11073 SET_LEX_STATE(EXPR_BEG);
11074 p->command_start = TRUE;
11075 return ';';
11076
11077 case ',':
11078 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11079 return ',';
11080
11081 case '~':
11082 if (IS_AFTER_OPERATOR()) {
11083 if ((c = nextc(p)) != '@') {
11084 pushback(p, c);
11085 }
11086 SET_LEX_STATE(EXPR_ARG);
11087 }
11088 else {
11089 SET_LEX_STATE(EXPR_BEG);
11090 }
11091 return '~';
11092
11093 case '(':
11094 if (IS_BEG()) {
11095 c = tLPAREN;
11096 }
11097 else if (!space_seen) {
11098 /* foo( ... ) => method call, no ambiguity */
11099 }
11100 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11101 c = tLPAREN_ARG;
11102 }
11103 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11104 rb_warning0("parentheses after method name is interpreted as "
11105 "an argument list, not a decomposed argument");
11106 }
11107 p->lex.paren_nest++;
11108 COND_PUSH(0);
11109 CMDARG_PUSH(0);
11110 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11111 return c;
11112
11113 case '[':
11114 p->lex.paren_nest++;
11115 if (IS_AFTER_OPERATOR()) {
11116 if ((c = nextc(p)) == ']') {
11117 p->lex.paren_nest--;
11118 SET_LEX_STATE(EXPR_ARG);
11119 if ((c = nextc(p)) == '=') {
11120 return tASET;
11121 }
11122 pushback(p, c);
11123 return tAREF;
11124 }
11125 pushback(p, c);
11126 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11127 return '[';
11128 }
11129 else if (IS_BEG()) {
11130 c = tLBRACK;
11131 }
11132 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11133 c = tLBRACK;
11134 }
11135 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11136 COND_PUSH(0);
11137 CMDARG_PUSH(0);
11138 return c;
11139
11140 case '{':
11141 ++p->lex.brace_nest;
11142 if (lambda_beginning_p())
11143 c = tLAMBEG;
11144 else if (IS_lex_state(EXPR_LABELED))
11145 c = tLBRACE; /* hash */
11146 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11147 c = '{'; /* block (primary) */
11148 else if (IS_lex_state(EXPR_ENDARG))
11149 c = tLBRACE_ARG; /* block (expr) */
11150 else
11151 c = tLBRACE; /* hash */
11152 if (c != tLBRACE) {
11153 p->command_start = TRUE;
11154 SET_LEX_STATE(EXPR_BEG);
11155 }
11156 else {
11157 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11158 }
11159 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11160 COND_PUSH(0);
11161 CMDARG_PUSH(0);
11162 return c;
11163
11164 case '\\':
11165 c = nextc(p);
11166 if (c == '\n') {
11167 space_seen = 1;
11168 dispatch_scan_event(p, tSP);
11169 goto retry; /* skip \\n */
11170 }
11171 if (c == ' ') return tSP;
11172 if (ISSPACE(c)) return c;
11173 pushback(p, c);
11174 return '\\';
11175
11176 case '%':
11177 return parse_percent(p, space_seen, last_state);
11178
11179 case '$':
11180 return parse_gvar(p, last_state);
11181
11182 case '@':
11183 return parse_atmark(p, last_state);
11184
11185 case '_':
11186 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11187 p->ruby__end__seen = 1;
11188 p->eofp = 1;
11189#ifdef RIPPER
11190 lex_goto_eol(p);
11191 dispatch_scan_event(p, k__END__);
11192#endif
11193 return END_OF_INPUT;
11194 }
11195 newtok(p);
11196 break;
11197
11198 default:
11199 if (!parser_is_identchar(p)) {
11200 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11201 token_flush(p);
11202 goto retry;
11203 }
11204
11205 newtok(p);
11206 break;
11207 }
11208
11209 return parse_ident(p, c, cmd_state);
11210}
11211
11212static enum yytokentype
11213yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11214{
11215 enum yytokentype t;
11216
11217 p->lval = lval;
11218 lval->node = 0;
11219 p->yylloc = yylloc;
11220
11221 t = parser_yylex(p);
11222
11223 if (has_delayed_token(p))
11224 dispatch_delayed_token(p, t);
11225 else if (t != END_OF_INPUT)
11226 dispatch_scan_event(p, t);
11227
11228 return t;
11229}
11230
11231#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11232
11233static NODE*
11234node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11235{
11236 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11237
11238 rb_node_init(n, type);
11239 return n;
11240}
11241
11242static NODE *
11243nd_set_loc(NODE *nd, const YYLTYPE *loc)
11244{
11245 nd->nd_loc = *loc;
11246 nd_set_line(nd, loc->beg_pos.lineno);
11247 return nd;
11248}
11249
11250static NODE*
11251node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11252{
11253 NODE *n = node_new_internal(p, type, size, alignment);
11254
11255 nd_set_loc(n, loc);
11256 nd_set_node_id(n, parser_get_node_id(p));
11257 return n;
11258}
11259
11260#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11261
11262static rb_node_scope_t *
11263rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11264{
11265 rb_ast_id_table_t *nd_tbl;
11266 nd_tbl = local_tbl(p);
11267 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11268 n->nd_tbl = nd_tbl;
11269 n->nd_body = nd_body;
11270 n->nd_args = nd_args;
11271
11272 return n;
11273}
11274
11275static rb_node_scope_t *
11276rb_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)
11277{
11278 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11279 n->nd_tbl = nd_tbl;
11280 n->nd_body = nd_body;
11281 n->nd_args = nd_args;
11282
11283 return n;
11284}
11285
11286static rb_node_defn_t *
11287rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11288{
11289 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11290 n->nd_mid = nd_mid;
11291 n->nd_defn = nd_defn;
11292
11293 return n;
11294}
11295
11296static rb_node_defs_t *
11297rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11298{
11299 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11300 n->nd_recv = nd_recv;
11301 n->nd_mid = nd_mid;
11302 n->nd_defn = nd_defn;
11303
11304 return n;
11305}
11306
11307static rb_node_block_t *
11308rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11309{
11310 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11311 n->nd_head = nd_head;
11312 n->nd_end = (NODE *)n;
11313 n->nd_next = 0;
11314
11315 return n;
11316}
11317
11318static rb_node_for_t *
11319rb_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)
11320{
11321 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11322 n->nd_body = nd_body;
11323 n->nd_iter = nd_iter;
11324 n->for_keyword_loc = *for_keyword_loc;
11325 n->in_keyword_loc = *in_keyword_loc;
11326 n->do_keyword_loc = *do_keyword_loc;
11327 n->end_keyword_loc = *end_keyword_loc;
11328
11329 return n;
11330}
11331
11332static rb_node_for_masgn_t *
11333rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11334{
11335 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11336 n->nd_var = nd_var;
11337
11338 return n;
11339}
11340
11341static rb_node_retry_t *
11342rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11343{
11344 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11345
11346 return n;
11347}
11348
11349static rb_node_begin_t *
11350rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11351{
11352 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11353 n->nd_body = nd_body;
11354
11355 return n;
11356}
11357
11358static rb_node_rescue_t *
11359rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11360{
11361 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11362 n->nd_head = nd_head;
11363 n->nd_resq = nd_resq;
11364 n->nd_else = nd_else;
11365
11366 return n;
11367}
11368
11369static rb_node_resbody_t *
11370rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11371{
11372 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11373 n->nd_args = nd_args;
11374 n->nd_exc_var = nd_exc_var;
11375 n->nd_body = nd_body;
11376 n->nd_next = nd_next;
11377
11378 return n;
11379}
11380
11381static rb_node_ensure_t *
11382rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11383{
11384 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11385 n->nd_head = nd_head;
11386 n->nd_ensr = nd_ensr;
11387
11388 return n;
11389}
11390
11391static rb_node_and_t *
11392rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11393{
11394 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11395 n->nd_1st = nd_1st;
11396 n->nd_2nd = nd_2nd;
11397 n->operator_loc = *operator_loc;
11398
11399 return n;
11400}
11401
11402static rb_node_or_t *
11403rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11404{
11405 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11406 n->nd_1st = nd_1st;
11407 n->nd_2nd = nd_2nd;
11408 n->operator_loc = *operator_loc;
11409
11410 return n;
11411}
11412
11413static rb_node_return_t *
11414rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11415{
11416 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11417 n->nd_stts = nd_stts;
11418 n->keyword_loc = *keyword_loc;
11419 return n;
11420}
11421
11422static rb_node_yield_t *
11423rb_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)
11424{
11425 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11426 n->nd_head = nd_head;
11427 n->keyword_loc = *keyword_loc;
11428 n->lparen_loc = *lparen_loc;
11429 n->rparen_loc = *rparen_loc;
11430
11431 return n;
11432}
11433
11434static rb_node_if_t *
11435rb_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)
11436{
11437 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11438 n->nd_cond = nd_cond;
11439 n->nd_body = nd_body;
11440 n->nd_else = nd_else;
11441 n->if_keyword_loc = *if_keyword_loc;
11442 n->then_keyword_loc = *then_keyword_loc;
11443 n->end_keyword_loc = *end_keyword_loc;
11444
11445 return n;
11446}
11447
11448static rb_node_unless_t *
11449rb_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)
11450{
11451 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11452 n->nd_cond = nd_cond;
11453 n->nd_body = nd_body;
11454 n->nd_else = nd_else;
11455 n->keyword_loc = *keyword_loc;
11456 n->then_keyword_loc = *then_keyword_loc;
11457 n->end_keyword_loc = *end_keyword_loc;
11458
11459 return n;
11460}
11461
11462static rb_node_class_t *
11463rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc)
11464{
11465 /* Keep the order of node creation */
11466 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11467 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11468 n->nd_cpath = nd_cpath;
11469 n->nd_body = scope;
11470 n->nd_super = nd_super;
11471
11472 return n;
11473}
11474
11475static rb_node_sclass_t *
11476rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc)
11477{
11478 /* Keep the order of node creation */
11479 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11480 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11481 n->nd_recv = nd_recv;
11482 n->nd_body = scope;
11483
11484 return n;
11485}
11486
11487static rb_node_module_t *
11488rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc)
11489{
11490 /* Keep the order of node creation */
11491 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11492 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11493 n->nd_cpath = nd_cpath;
11494 n->nd_body = scope;
11495
11496 return n;
11497}
11498
11499static rb_node_iter_t *
11500rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11501{
11502 /* Keep the order of node creation */
11503 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11504 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11505 n->nd_body = scope;
11506 n->nd_iter = 0;
11507
11508 return n;
11509}
11510
11511static rb_node_lambda_t *
11512rb_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)
11513{
11514 /* Keep the order of node creation */
11515 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11516 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11517 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11518 n->nd_body = scope;
11519 n->operator_loc = *operator_loc;
11520 n->opening_loc = *opening_loc;
11521 n->closing_loc = *closing_loc;
11522
11523 return n;
11524}
11525
11526static rb_node_case_t *
11527rb_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)
11528{
11529 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11530 n->nd_head = nd_head;
11531 n->nd_body = nd_body;
11532 n->case_keyword_loc = *case_keyword_loc;
11533 n->end_keyword_loc = *end_keyword_loc;
11534
11535 return n;
11536}
11537
11538static rb_node_case2_t *
11539rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11540{
11541 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11542 n->nd_head = 0;
11543 n->nd_body = nd_body;
11544 n->case_keyword_loc = *case_keyword_loc;
11545 n->end_keyword_loc = *end_keyword_loc;
11546
11547 return n;
11548}
11549
11550static rb_node_case3_t *
11551rb_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)
11552{
11553 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11554 n->nd_head = nd_head;
11555 n->nd_body = nd_body;
11556 n->case_keyword_loc = *case_keyword_loc;
11557 n->end_keyword_loc = *end_keyword_loc;
11558
11559 return n;
11560}
11561
11562static rb_node_when_t *
11563rb_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)
11564{
11565 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11566 n->nd_head = nd_head;
11567 n->nd_body = nd_body;
11568 n->nd_next = nd_next;
11569 n->keyword_loc = *keyword_loc;
11570 n->then_keyword_loc = *then_keyword_loc;
11571
11572 return n;
11573}
11574
11575static rb_node_in_t *
11576rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11577{
11578 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11579 n->nd_head = nd_head;
11580 n->nd_body = nd_body;
11581 n->nd_next = nd_next;
11582
11583 return n;
11584}
11585
11586static rb_node_while_t *
11587rb_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)
11588{
11589 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11590 n->nd_cond = nd_cond;
11591 n->nd_body = nd_body;
11592 n->nd_state = nd_state;
11593 n->keyword_loc = *keyword_loc;
11594 n->closing_loc = *closing_loc;
11595
11596 return n;
11597}
11598
11599static rb_node_until_t *
11600rb_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)
11601{
11602 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11603 n->nd_cond = nd_cond;
11604 n->nd_body = nd_body;
11605 n->nd_state = nd_state;
11606 n->keyword_loc = *keyword_loc;
11607 n->closing_loc = *closing_loc;
11608
11609 return n;
11610}
11611
11612static rb_node_colon2_t *
11613rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc)
11614{
11615 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11616 n->nd_head = nd_head;
11617 n->nd_mid = nd_mid;
11618
11619 return n;
11620}
11621
11622static rb_node_colon3_t *
11623rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11624{
11625 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11626 n->nd_mid = nd_mid;
11627
11628 return n;
11629}
11630
11631static rb_node_dot2_t *
11632rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11633{
11634 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11635 n->nd_beg = nd_beg;
11636 n->nd_end = nd_end;
11637 n->operator_loc = *operator_loc;
11638
11639 return n;
11640}
11641
11642static rb_node_dot3_t *
11643rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11644{
11645 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11646 n->nd_beg = nd_beg;
11647 n->nd_end = nd_end;
11648 n->operator_loc = *operator_loc;
11649
11650 return n;
11651}
11652
11653static rb_node_self_t *
11654rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11655{
11656 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11657 n->nd_state = 1;
11658
11659 return n;
11660}
11661
11662static rb_node_nil_t *
11663rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11664{
11665 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11666
11667 return n;
11668}
11669
11670static rb_node_true_t *
11671rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11672{
11673 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11674
11675 return n;
11676}
11677
11678static rb_node_false_t *
11679rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11680{
11681 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11682
11683 return n;
11684}
11685
11686static rb_node_super_t *
11687rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11688 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11689{
11690 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11691 n->nd_args = nd_args;
11692 n->keyword_loc = *keyword_loc;
11693 n->lparen_loc = *lparen_loc;
11694 n->rparen_loc = *rparen_loc;
11695
11696 return n;
11697}
11698
11699static rb_node_zsuper_t *
11700rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11701{
11702 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11703
11704 return n;
11705}
11706
11707static rb_node_match2_t *
11708rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11709{
11710 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11711 n->nd_recv = nd_recv;
11712 n->nd_value = nd_value;
11713 n->nd_args = 0;
11714
11715 return n;
11716}
11717
11718static rb_node_match3_t *
11719rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11720{
11721 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11722 n->nd_recv = nd_recv;
11723 n->nd_value = nd_value;
11724
11725 return n;
11726}
11727
11728/* TODO: Use union for NODE_LIST2 */
11729static rb_node_list_t *
11730rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11731{
11732 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11733 n->nd_head = nd_head;
11734 n->as.nd_alen = 1;
11735 n->nd_next = 0;
11736
11737 return n;
11738}
11739
11740static rb_node_list_t *
11741rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11742{
11743 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11744 n->nd_head = nd_head;
11745 n->as.nd_alen = nd_alen;
11746 n->nd_next = nd_next;
11747
11748 return n;
11749}
11750
11751static rb_node_zlist_t *
11752rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11753{
11754 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11755
11756 return n;
11757}
11758
11759static rb_node_hash_t *
11760rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11761{
11762 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11763 n->nd_head = nd_head;
11764 n->nd_brace = 0;
11765
11766 return n;
11767}
11768
11769static rb_node_masgn_t *
11770rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11771{
11772 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11773 n->nd_head = nd_head;
11774 n->nd_value = 0;
11775 n->nd_args = nd_args;
11776
11777 return n;
11778}
11779
11780static rb_node_gasgn_t *
11781rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11782{
11783 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11784 n->nd_vid = nd_vid;
11785 n->nd_value = nd_value;
11786
11787 return n;
11788}
11789
11790static rb_node_lasgn_t *
11791rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11792{
11793 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11794 n->nd_vid = nd_vid;
11795 n->nd_value = nd_value;
11796
11797 return n;
11798}
11799
11800static rb_node_dasgn_t *
11801rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11802{
11803 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11804 n->nd_vid = nd_vid;
11805 n->nd_value = nd_value;
11806
11807 return n;
11808}
11809
11810static rb_node_iasgn_t *
11811rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11812{
11813 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11814 n->nd_vid = nd_vid;
11815 n->nd_value = nd_value;
11816
11817 return n;
11818}
11819
11820static rb_node_cvasgn_t *
11821rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11822{
11823 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11824 n->nd_vid = nd_vid;
11825 n->nd_value = nd_value;
11826
11827 return n;
11828}
11829
11830static rb_node_op_asgn1_t *
11831rb_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)
11832{
11833 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11834 n->nd_recv = nd_recv;
11835 n->nd_mid = nd_mid;
11836 n->nd_index = index;
11837 n->nd_rvalue = rvalue;
11838 n->call_operator_loc = *call_operator_loc;
11839 n->opening_loc = *opening_loc;
11840 n->closing_loc = *closing_loc;
11841 n->binary_operator_loc = *binary_operator_loc;
11842
11843 return n;
11844}
11845
11846static rb_node_op_asgn2_t *
11847rb_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)
11848{
11849 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11850 n->nd_recv = nd_recv;
11851 n->nd_value = nd_value;
11852 n->nd_vid = nd_vid;
11853 n->nd_mid = nd_mid;
11854 n->nd_aid = nd_aid;
11855 n->call_operator_loc = *call_operator_loc;
11856 n->message_loc = *message_loc;
11857 n->binary_operator_loc = *binary_operator_loc;
11858
11859 return n;
11860}
11861
11862static rb_node_op_asgn_or_t *
11863rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11864{
11865 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11866 n->nd_head = nd_head;
11867 n->nd_value = nd_value;
11868
11869 return n;
11870}
11871
11872static rb_node_op_asgn_and_t *
11873rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11874{
11875 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11876 n->nd_head = nd_head;
11877 n->nd_value = nd_value;
11878
11879 return n;
11880}
11881
11882static rb_node_gvar_t *
11883rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11884{
11885 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11886 n->nd_vid = nd_vid;
11887
11888 return n;
11889}
11890
11891static rb_node_lvar_t *
11892rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11893{
11894 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11895 n->nd_vid = nd_vid;
11896
11897 return n;
11898}
11899
11900static rb_node_dvar_t *
11901rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11902{
11903 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11904 n->nd_vid = nd_vid;
11905
11906 return n;
11907}
11908
11909static rb_node_ivar_t *
11910rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11911{
11912 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11913 n->nd_vid = nd_vid;
11914
11915 return n;
11916}
11917
11918static rb_node_const_t *
11919rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11920{
11921 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11922 n->nd_vid = nd_vid;
11923
11924 return n;
11925}
11926
11927static rb_node_cvar_t *
11928rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11929{
11930 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11931 n->nd_vid = nd_vid;
11932
11933 return n;
11934}
11935
11936static rb_node_nth_ref_t *
11937rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11938{
11939 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11940 n->nd_nth = nd_nth;
11941
11942 return n;
11943}
11944
11945static rb_node_back_ref_t *
11946rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11947{
11948 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11949 n->nd_nth = nd_nth;
11950
11951 return n;
11952}
11953
11954static rb_node_integer_t *
11955rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11956{
11957 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11958 n->val = val;
11959 n->minus = FALSE;
11960 n->base = base;
11961
11962 return n;
11963}
11964
11965static rb_node_float_t *
11966rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11967{
11968 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
11969 n->val = val;
11970 n->minus = FALSE;
11971
11972 return n;
11973}
11974
11975static rb_node_rational_t *
11976rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
11977{
11978 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
11979 n->val = val;
11980 n->minus = FALSE;
11981 n->base = base;
11982 n->seen_point = seen_point;
11983
11984 return n;
11985}
11986
11987static rb_node_imaginary_t *
11988rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
11989{
11990 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
11991 n->val = val;
11992 n->minus = FALSE;
11993 n->base = base;
11994 n->seen_point = seen_point;
11995 n->type = numeric_type;
11996
11997 return n;
11998}
11999
12000static rb_node_str_t *
12001rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12002{
12003 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12004 n->string = string;
12005
12006 return n;
12007}
12008
12009/* TODO; Use union for NODE_DSTR2 */
12010static rb_node_dstr_t *
12011rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12012{
12013 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12014 n->string = string;
12015 n->as.nd_alen = nd_alen;
12016 n->nd_next = (rb_node_list_t *)nd_next;
12017
12018 return n;
12019}
12020
12021static rb_node_dstr_t *
12022rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12023{
12024 return rb_node_dstr_new0(p, string, 1, 0, loc);
12025}
12026
12027static rb_node_xstr_t *
12028rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12029{
12030 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12031 n->string = string;
12032
12033 return n;
12034}
12035
12036static rb_node_dxstr_t *
12037rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12038{
12039 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12040 n->string = string;
12041 n->as.nd_alen = nd_alen;
12042 n->nd_next = (rb_node_list_t *)nd_next;
12043
12044 return n;
12045}
12046
12047static rb_node_sym_t *
12048rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12049{
12050 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12051 n->string = rb_str_to_parser_string(p, str);
12052
12053 return n;
12054}
12055
12056static rb_node_dsym_t *
12057rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12058{
12059 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12060 n->string = string;
12061 n->as.nd_alen = nd_alen;
12062 n->nd_next = (rb_node_list_t *)nd_next;
12063
12064 return n;
12065}
12066
12067static rb_node_evstr_t *
12068rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12069{
12070 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12071 n->nd_body = nd_body;
12072 n->opening_loc = *opening_loc;
12073 n->closing_loc = *closing_loc;
12074
12075 return n;
12076}
12077
12078static rb_node_regx_t *
12079rb_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)
12080{
12081 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12082 n->string = string;
12083 n->options = options & RE_OPTION_MASK;
12084 n->opening_loc = *opening_loc;
12085 n->content_loc = *content_loc;
12086 n->closing_loc = *closing_loc;
12087
12088 return n;
12089}
12090
12091static rb_node_call_t *
12092rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12093{
12094 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12095 n->nd_recv = nd_recv;
12096 n->nd_mid = nd_mid;
12097 n->nd_args = nd_args;
12098
12099 return n;
12100}
12101
12102static rb_node_opcall_t *
12103rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12104{
12105 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12106 n->nd_recv = nd_recv;
12107 n->nd_mid = nd_mid;
12108 n->nd_args = nd_args;
12109
12110 return n;
12111}
12112
12113static rb_node_fcall_t *
12114rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12115{
12116 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12117 n->nd_mid = nd_mid;
12118 n->nd_args = nd_args;
12119
12120 return n;
12121}
12122
12123static rb_node_qcall_t *
12124rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12125{
12126 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12127 n->nd_recv = nd_recv;
12128 n->nd_mid = nd_mid;
12129 n->nd_args = nd_args;
12130
12131 return n;
12132}
12133
12134static rb_node_vcall_t *
12135rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12136{
12137 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12138 n->nd_mid = nd_mid;
12139
12140 return n;
12141}
12142
12143static rb_node_once_t *
12144rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12145{
12146 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12147 n->nd_body = nd_body;
12148
12149 return n;
12150}
12151
12152static rb_node_args_t *
12153rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12154{
12155 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12156 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12157
12158 return n;
12159}
12160
12161static rb_node_args_aux_t *
12162rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12163{
12164 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12165 n->nd_pid = nd_pid;
12166 n->nd_plen = nd_plen;
12167 n->nd_next = 0;
12168
12169 return n;
12170}
12171
12172static rb_node_opt_arg_t *
12173rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12174{
12175 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12176 n->nd_body = nd_body;
12177 n->nd_next = 0;
12178
12179 return n;
12180}
12181
12182static rb_node_kw_arg_t *
12183rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12184{
12185 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12186 n->nd_body = nd_body;
12187 n->nd_next = 0;
12188
12189 return n;
12190}
12191
12192static rb_node_postarg_t *
12193rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12194{
12195 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12196 n->nd_1st = nd_1st;
12197 n->nd_2nd = nd_2nd;
12198
12199 return n;
12200}
12201
12202static rb_node_argscat_t *
12203rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12204{
12205 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12206 n->nd_head = nd_head;
12207 n->nd_body = nd_body;
12208
12209 return n;
12210}
12211
12212static rb_node_argspush_t *
12213rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12214{
12215 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12216 n->nd_head = nd_head;
12217 n->nd_body = nd_body;
12218
12219 return n;
12220}
12221
12222static rb_node_splat_t *
12223rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12224{
12225 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12226 n->nd_head = nd_head;
12227 n->operator_loc = *operator_loc;
12228
12229 return n;
12230}
12231
12232static rb_node_block_pass_t *
12233rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12234{
12235 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12236 n->forwarding = 0;
12237 n->nd_head = 0;
12238 n->nd_body = nd_body;
12239 n->operator_loc = *operator_loc;
12240
12241 return n;
12242}
12243
12244static rb_node_alias_t *
12245rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12246{
12247 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12248 n->nd_1st = nd_1st;
12249 n->nd_2nd = nd_2nd;
12250 n->keyword_loc = *keyword_loc;
12251
12252 return n;
12253}
12254
12255static rb_node_valias_t *
12256rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12257{
12258 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12259 n->nd_alias = nd_alias;
12260 n->nd_orig = nd_orig;
12261 n->keyword_loc = *keyword_loc;
12262
12263 return n;
12264}
12265
12266static rb_node_undef_t *
12267rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12268{
12269 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12270 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12271 n->keyword_loc = NULL_LOC;
12272 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12273
12274 return n;
12275}
12276
12277static rb_node_errinfo_t *
12278rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12279{
12280 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12281
12282 return n;
12283}
12284
12285static rb_node_defined_t *
12286rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12287{
12288 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12289 n->nd_head = nd_head;
12290
12291 return n;
12292}
12293
12294static rb_node_postexe_t *
12295rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12296{
12297 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12298 n->nd_body = nd_body;
12299
12300 return n;
12301}
12302
12303static rb_node_attrasgn_t *
12304rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12305{
12306 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12307 n->nd_recv = nd_recv;
12308 n->nd_mid = nd_mid;
12309 n->nd_args = nd_args;
12310
12311 return n;
12312}
12313
12314static rb_node_aryptn_t *
12315rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12316{
12317 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12318 n->nd_pconst = 0;
12319 n->pre_args = pre_args;
12320 n->rest_arg = rest_arg;
12321 n->post_args = post_args;
12322
12323 return n;
12324}
12325
12326static rb_node_hshptn_t *
12327rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12328{
12329 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12330 n->nd_pconst = nd_pconst;
12331 n->nd_pkwargs = nd_pkwargs;
12332 n->nd_pkwrestarg = nd_pkwrestarg;
12333
12334 return n;
12335}
12336
12337static rb_node_fndptn_t *
12338rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12339{
12340 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12341 n->nd_pconst = 0;
12342 n->pre_rest_arg = pre_rest_arg;
12343 n->args = args;
12344 n->post_rest_arg = post_rest_arg;
12345
12346 return n;
12347}
12348
12349static rb_node_line_t *
12350rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12351{
12352 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12353
12354 return n;
12355}
12356
12357static rb_node_file_t *
12358rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12359{
12360 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12361 n->path = rb_str_to_parser_string(p, str);
12362
12363 return n;
12364}
12365
12366static rb_node_encoding_t *
12367rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12368{
12369 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12370 n->enc = p->enc;
12371
12372 return n;
12373}
12374
12375static rb_node_cdecl_t *
12376rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12377{
12378 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12379 n->nd_vid = nd_vid;
12380 n->nd_value = nd_value;
12381 n->nd_else = nd_else;
12382 n->shareability = shareability;
12383
12384 return n;
12385}
12386
12387static rb_node_op_cdecl_t *
12388rb_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)
12389{
12390 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12391 n->nd_head = nd_head;
12392 n->nd_value = nd_value;
12393 n->nd_aid = nd_aid;
12394 n->shareability = shareability;
12395
12396 return n;
12397}
12398
12399static rb_node_error_t *
12400rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12401{
12402 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12403
12404 return n;
12405}
12406
12407static rb_node_break_t *
12408rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12409{
12410 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12411 n->nd_stts = nd_stts;
12412 n->nd_chain = 0;
12413 n->keyword_loc = *keyword_loc;
12414
12415 return n;
12416}
12417
12418static rb_node_next_t *
12419rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12420{
12421 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12422 n->nd_stts = nd_stts;
12423 n->nd_chain = 0;
12424 n->keyword_loc = *keyword_loc;
12425
12426 return n;
12427}
12428
12429static rb_node_redo_t *
12430rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12431{
12432 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12433 n->nd_chain = 0;
12434 n->keyword_loc = *keyword_loc;
12435
12436 return n;
12437}
12438
12439static rb_node_def_temp_t *
12440rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12441{
12442 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12443 n->save.numparam_save = 0;
12444 n->save.max_numparam = 0;
12445 n->save.ctxt = p->ctxt;
12446 n->nd_def = 0;
12447 n->nd_mid = 0;
12448
12449 return n;
12450}
12451
12452static rb_node_def_temp_t *
12453def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12454{
12455 n->save.numparam_save = numparam_push(p);
12456 n->save.max_numparam = p->max_numparam;
12457 return n;
12458}
12459
12460#ifndef RIPPER
12461static enum node_type
12462nodetype(NODE *node) /* for debug */
12463{
12464 return (enum node_type)nd_type(node);
12465}
12466
12467static int
12468nodeline(NODE *node)
12469{
12470 return nd_line(node);
12471}
12472#endif
12473
12474static NODE*
12475newline_node(NODE *node)
12476{
12477 if (node) {
12478 node = remove_begin(node);
12479 nd_set_fl_newline(node);
12480 }
12481 return node;
12482}
12483
12484static void
12485fixpos(NODE *node, NODE *orig)
12486{
12487 if (!node) return;
12488 if (!orig) return;
12489 nd_set_line(node, nd_line(orig));
12490}
12491
12492static NODE*
12493block_append(struct parser_params *p, NODE *head, NODE *tail)
12494{
12495 NODE *end, *h = head, *nd;
12496
12497 if (tail == 0) return head;
12498
12499 if (h == 0) return tail;
12500 switch (nd_type(h)) {
12501 default:
12502 h = end = NEW_BLOCK(head, &head->nd_loc);
12503 head = end;
12504 break;
12505 case NODE_BLOCK:
12506 end = RNODE_BLOCK(h)->nd_end;
12507 break;
12508 }
12509
12510 nd = RNODE_BLOCK(end)->nd_head;
12511 switch (nd_type(nd)) {
12512 case NODE_RETURN:
12513 case NODE_BREAK:
12514 case NODE_NEXT:
12515 case NODE_REDO:
12516 case NODE_RETRY:
12517 rb_warning0L(nd_line(tail), "statement not reached");
12518 break;
12519
12520 default:
12521 break;
12522 }
12523
12524 if (!nd_type_p(tail, NODE_BLOCK)) {
12525 tail = NEW_BLOCK(tail, &tail->nd_loc);
12526 }
12527 RNODE_BLOCK(end)->nd_next = tail;
12528 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12529 nd_set_last_loc(head, nd_last_loc(tail));
12530 return head;
12531}
12532
12533/* append item to the list */
12534static NODE*
12535list_append(struct parser_params *p, NODE *list, NODE *item)
12536{
12537 NODE *last;
12538
12539 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12540 if (RNODE_LIST(list)->nd_next) {
12541 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12542 }
12543 else {
12544 last = list;
12545 }
12546
12547 RNODE_LIST(list)->as.nd_alen += 1;
12548 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12549 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12550
12551 nd_set_last_loc(list, nd_last_loc(item));
12552
12553 return list;
12554}
12555
12556/* concat two lists */
12557static NODE*
12558list_concat(NODE *head, NODE *tail)
12559{
12560 NODE *last;
12561
12562 if (RNODE_LIST(head)->nd_next) {
12563 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12564 }
12565 else {
12566 last = head;
12567 }
12568
12569 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12570 RNODE_LIST(last)->nd_next = tail;
12571 if (RNODE_LIST(tail)->nd_next) {
12572 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12573 }
12574 else {
12575 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12576 }
12577
12578 nd_set_last_loc(head, nd_last_loc(tail));
12579
12580 return head;
12581}
12582
12583static int
12584literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12585{
12586 if (!tail) return 1;
12587 if (!rb_parser_enc_compatible(p, head, tail)) {
12588 compile_error(p, "string literal encodings differ (%s / %s)",
12589 rb_enc_name(rb_parser_str_get_encoding(head)),
12590 rb_enc_name(rb_parser_str_get_encoding(tail)));
12591 rb_parser_str_resize(p, head, 0);
12592 rb_parser_str_resize(p, tail, 0);
12593 return 0;
12594 }
12595 rb_parser_str_buf_append(p, head, tail);
12596 return 1;
12597}
12598
12599static rb_parser_string_t *
12600string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12601{
12602 if (htype != NODE_DSTR) return NULL;
12603 if (RNODE_DSTR(head)->nd_next) {
12604 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12605 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12606 }
12607 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12608 ASSUME(lit);
12609 return lit;
12610}
12611
12612#ifndef RIPPER
12613static rb_parser_string_t *
12614rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12615{
12616 rb_parser_string_t *copy;
12617 if (!orig) return NULL;
12618 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12619 copy->coderange = orig->coderange;
12620 copy->enc = orig->enc;
12621 return copy;
12622}
12623#endif
12624
12625/* concat two string literals */
12626static NODE *
12627literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12628{
12629 enum node_type htype;
12630 rb_parser_string_t *lit;
12631
12632 if (!head) return tail;
12633 if (!tail) return head;
12634
12635 htype = nd_type(head);
12636 if (htype == NODE_EVSTR) {
12637 head = new_dstr(p, head, loc);
12638 htype = NODE_DSTR;
12639 }
12640 if (p->heredoc_indent > 0) {
12641 switch (htype) {
12642 case NODE_STR:
12643 head = str2dstr(p, head);
12644 case NODE_DSTR:
12645 return list_append(p, head, tail);
12646 default:
12647 break;
12648 }
12649 }
12650 switch (nd_type(tail)) {
12651 case NODE_STR:
12652 if ((lit = string_literal_head(p, htype, head)) != false) {
12653 htype = NODE_STR;
12654 }
12655 else {
12656 lit = RNODE_DSTR(head)->string;
12657 }
12658 if (htype == NODE_STR) {
12659 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12660 error:
12661 rb_discard_node(p, head);
12662 rb_discard_node(p, tail);
12663 return 0;
12664 }
12665 rb_discard_node(p, tail);
12666 }
12667 else {
12668 list_append(p, head, tail);
12669 }
12670 break;
12671
12672 case NODE_DSTR:
12673 if (htype == NODE_STR) {
12674 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12675 goto error;
12676 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12677 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12678 RNODE_STR(head)->string = NULL;
12679 rb_discard_node(p, head);
12680 head = tail;
12681 }
12682 else if (!RNODE_DSTR(tail)->string) {
12683 append:
12684 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12685 if (!RNODE_DSTR(head)->nd_next) {
12686 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12687 }
12688 else if (RNODE_DSTR(tail)->nd_next) {
12689 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12690 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12691 }
12692 rb_discard_node(p, tail);
12693 }
12694 else if ((lit = string_literal_head(p, htype, head)) != false) {
12695 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12696 goto error;
12697 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12698 RNODE_DSTR(tail)->string = 0;
12699 goto append;
12700 }
12701 else {
12702 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));
12703 RNODE_DSTR(tail)->string = 0;
12704 }
12705 break;
12706
12707 case NODE_EVSTR:
12708 if (htype == NODE_STR) {
12709 head = str2dstr(p, head);
12710 RNODE_DSTR(head)->as.nd_alen = 1;
12711 }
12712 list_append(p, head, tail);
12713 break;
12714 }
12715 return head;
12716}
12717
12718static void
12719nd_copy_flag(NODE *new_node, NODE *old_node)
12720{
12721 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12722 nd_set_line(new_node, nd_line(old_node));
12723 new_node->nd_loc = old_node->nd_loc;
12724 new_node->node_id = old_node->node_id;
12725}
12726
12727static NODE *
12728str2dstr(struct parser_params *p, NODE *node)
12729{
12730 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12731 nd_copy_flag(new_node, node);
12732 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12733 RNODE_DSTR(new_node)->as.nd_alen = 0;
12734 RNODE_DSTR(new_node)->nd_next = 0;
12735 RNODE_STR(node)->string = 0;
12736
12737 return new_node;
12738}
12739
12740static NODE *
12741str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12742{
12743 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12744 nd_copy_flag(new_node, node);
12745 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12746 RNODE_REGX(new_node)->options = options;
12747 nd_set_loc(new_node, loc);
12748 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12749 RNODE_REGX(new_node)->content_loc = *content_loc;
12750 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12751 RNODE_STR(node)->string = 0;
12752
12753 return new_node;
12754}
12755
12756static NODE *
12757evstr2dstr(struct parser_params *p, NODE *node)
12758{
12759 if (nd_type_p(node, NODE_EVSTR)) {
12760 node = new_dstr(p, node, &node->nd_loc);
12761 }
12762 return node;
12763}
12764
12765static NODE *
12766new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12767{
12768 NODE *head = node;
12769
12770 if (node) {
12771 switch (nd_type(node)) {
12772 case NODE_STR:
12773 return str2dstr(p, node);
12774 case NODE_DSTR:
12775 break;
12776 case NODE_EVSTR:
12777 return node;
12778 }
12779 }
12780 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12781}
12782
12783static NODE *
12784new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12785{
12786 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12787 return list_append(p, dstr, node);
12788}
12789
12790static NODE *
12791call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12792 const YYLTYPE *op_loc, const YYLTYPE *loc)
12793{
12794 NODE *expr;
12795 value_expr(recv);
12796 value_expr(arg1);
12797 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12798 nd_set_line(expr, op_loc->beg_pos.lineno);
12799 return expr;
12800}
12801
12802static NODE *
12803call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12804{
12805 NODE *opcall;
12806 value_expr(recv);
12807 opcall = NEW_OPCALL(recv, id, 0, loc);
12808 nd_set_line(opcall, op_loc->beg_pos.lineno);
12809 return opcall;
12810}
12811
12812static NODE *
12813new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12814{
12815 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12816 nd_set_line(qcall, op_loc->beg_pos.lineno);
12817 return qcall;
12818}
12819
12820static NODE*
12821new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12822{
12823 NODE *ret;
12824 if (block) block_dup_check(p, args, block);
12825 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12826 if (block) ret = method_add_block(p, ret, block, loc);
12827 fixpos(ret, recv);
12828 return ret;
12829}
12830
12831static rb_locations_lambda_body_t*
12832new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12833{
12834 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12835 body->node = node;
12836 body->opening_loc = *opening_loc;
12837 body->closing_loc = *closing_loc;
12838 return body;
12839}
12840
12841#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12842
12843static NODE*
12844last_expr_once_body(NODE *node)
12845{
12846 if (!node) return 0;
12847 return nd_once_body(node);
12848}
12849
12850static NODE*
12851match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12852{
12853 NODE *n;
12854 int line = op_loc->beg_pos.lineno;
12855
12856 value_expr(node1);
12857 value_expr(node2);
12858
12859 if ((n = last_expr_once_body(node1)) != 0) {
12860 switch (nd_type(n)) {
12861 case NODE_DREGX:
12862 {
12863 NODE *match = NEW_MATCH2(node1, node2, loc);
12864 nd_set_line(match, line);
12865 return match;
12866 }
12867
12868 case NODE_REGX:
12869 {
12870 const VALUE lit = rb_node_regx_string_val(n);
12871 if (!NIL_P(lit)) {
12872 NODE *match = NEW_MATCH2(node1, node2, loc);
12873 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12874 nd_set_line(match, line);
12875 return match;
12876 }
12877 }
12878 }
12879 }
12880
12881 if ((n = last_expr_once_body(node2)) != 0) {
12882 NODE *match3;
12883
12884 switch (nd_type(n)) {
12885 case NODE_DREGX:
12886 match3 = NEW_MATCH3(node2, node1, loc);
12887 return match3;
12888 }
12889 }
12890
12891 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12892 nd_set_line(n, line);
12893 return n;
12894}
12895
12896# if WARN_PAST_SCOPE
12897static int
12898past_dvar_p(struct parser_params *p, ID id)
12899{
12900 struct vtable *past = p->lvtbl->past;
12901 while (past) {
12902 if (vtable_included(past, id)) return 1;
12903 past = past->prev;
12904 }
12905 return 0;
12906}
12907# endif
12908
12909static int
12910numparam_nested_p(struct parser_params *p)
12911{
12912 struct local_vars *local = p->lvtbl;
12913 NODE *outer = local->numparam.outer;
12914 NODE *inner = local->numparam.inner;
12915 if (outer || inner) {
12916 NODE *used = outer ? outer : inner;
12917 compile_error(p, "numbered parameter is already used in\n"
12918 "%s:%d: %s block here",
12919 p->ruby_sourcefile, nd_line(used),
12920 outer ? "outer" : "inner");
12921 parser_show_error_line(p, &used->nd_loc);
12922 return 1;
12923 }
12924 return 0;
12925}
12926
12927static int
12928numparam_used_p(struct parser_params *p)
12929{
12930 NODE *numparam = p->lvtbl->numparam.current;
12931 if (numparam) {
12932 compile_error(p, "numbered parameter is already used in\n"
12933 "%s:%d: current block here",
12934 p->ruby_sourcefile, nd_line(numparam));
12935 parser_show_error_line(p, &numparam->nd_loc);
12936 return 1;
12937 }
12938 return 0;
12939}
12940
12941static int
12942it_used_p(struct parser_params *p)
12943{
12944 NODE *it = p->lvtbl->it;
12945 if (it) {
12946 compile_error(p, "'it' is already used in\n"
12947 "%s:%d: current block here",
12948 p->ruby_sourcefile, nd_line(it));
12949 parser_show_error_line(p, &it->nd_loc);
12950 return 1;
12951 }
12952 return 0;
12953}
12954
12955static NODE*
12956gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12957{
12958 ID *vidp = NULL;
12959 NODE *node;
12960 switch (id) {
12961 case keyword_self:
12962 return NEW_SELF(loc);
12963 case keyword_nil:
12964 return NEW_NIL(loc);
12965 case keyword_true:
12966 return NEW_TRUE(loc);
12967 case keyword_false:
12968 return NEW_FALSE(loc);
12969 case keyword__FILE__:
12970 {
12971 VALUE file = p->ruby_sourcefile_string;
12972 if (NIL_P(file))
12973 file = rb_str_new(0, 0);
12974 node = NEW_FILE(file, loc);
12975 }
12976 return node;
12977 case keyword__LINE__:
12978 return NEW_LINE(loc);
12979 case keyword__ENCODING__:
12980 return NEW_ENCODING(loc);
12981
12982 }
12983 switch (id_type(id)) {
12984 case ID_LOCAL:
12985 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
12986 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
12987 if (vidp) *vidp |= LVAR_USED;
12988 node = NEW_DVAR(id, loc);
12989 return node;
12990 }
12991 if (local_id_ref(p, id, &vidp)) {
12992 if (vidp) *vidp |= LVAR_USED;
12993 node = NEW_LVAR(id, loc);
12994 return node;
12995 }
12996 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
12997 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
12998 if (numparam_nested_p(p) || it_used_p(p)) return 0;
12999 node = NEW_DVAR(id, loc);
13000 struct local_vars *local = p->lvtbl;
13001 if (!local->numparam.current) local->numparam.current = node;
13002 return node;
13003 }
13004# if WARN_PAST_SCOPE
13005 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13006 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13007 }
13008# endif
13009 /* method call without arguments */
13010 if (dyna_in_block(p) && id == rb_intern("it") && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13011 if (numparam_used_p(p)) return 0;
13012 if (p->max_numparam == ORDINAL_PARAM) {
13013 compile_error(p, "ordinary parameter is defined");
13014 return 0;
13015 }
13016 if (!p->it_id) {
13017 p->it_id = internal_id(p);
13018 vtable_add(p->lvtbl->args, p->it_id);
13019 }
13020 NODE *node = NEW_DVAR(p->it_id, loc);
13021 if (!p->lvtbl->it) p->lvtbl->it = node;
13022 return node;
13023 }
13024 return NEW_VCALL(id, loc);
13025 case ID_GLOBAL:
13026 return NEW_GVAR(id, loc);
13027 case ID_INSTANCE:
13028 return NEW_IVAR(id, loc);
13029 case ID_CONST:
13030 return NEW_CONST(id, loc);
13031 case ID_CLASS:
13032 return NEW_CVAR(id, loc);
13033 }
13034 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13035 return 0;
13036}
13037
13038static rb_node_opt_arg_t *
13039opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13040{
13041 rb_node_opt_arg_t *opts = opt_list;
13042 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13043
13044 while (opts->nd_next) {
13045 opts = opts->nd_next;
13046 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13047 }
13048 opts->nd_next = opt;
13049
13050 return opt_list;
13051}
13052
13053static rb_node_kw_arg_t *
13054kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13055{
13056 if (kwlist) {
13057 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13058 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13059 }
13060 return kwlist;
13061}
13062
13063static NODE *
13064new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
13065{
13066 NODE *n = expr;
13067 while (n) {
13068 if (nd_type_p(n, NODE_BEGIN)) {
13069 n = RNODE_BEGIN(n)->nd_body;
13070 }
13071 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13072 n = RNODE_BLOCK(n)->nd_head;
13073 }
13074 else {
13075 break;
13076 }
13077 }
13078 return NEW_DEFINED(n, loc);
13079}
13080
13081static NODE*
13082str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13083{
13084 VALUE lit;
13085 rb_parser_string_t *str = RNODE_STR(node)->string;
13086 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13087 yyerror1(loc, "invalid symbol");
13088 lit = STR_NEW0();
13089 }
13090 else {
13091 lit = rb_str_new_parser_string(str);
13092 }
13093 return NEW_SYM(lit, loc);
13094}
13095
13096static NODE*
13097symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13098{
13099 enum node_type type = nd_type(symbol);
13100 switch (type) {
13101 case NODE_DSTR:
13102 nd_set_type(symbol, NODE_DSYM);
13103 break;
13104 case NODE_STR:
13105 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13106 break;
13107 default:
13108 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13109 }
13110 return list_append(p, symbols, symbol);
13111}
13112
13113static void
13114dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13115{
13116 if (dreg->string) {
13117 reg_fragment_setenc(p, dreg->string, options);
13118 }
13119 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13120 NODE *frag = list->nd_head;
13121 if (nd_type_p(frag, NODE_STR)) {
13122 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13123 }
13124 else if (nd_type_p(frag, NODE_DSTR)) {
13125 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13126 }
13127 }
13128}
13129
13130static NODE *
13131new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13132{
13133 if (!node) {
13134 /* Check string is valid regex */
13135 rb_parser_string_t *str = STRING_NEW0();
13136 reg_compile(p, str, options);
13137 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13138 return node;
13139 }
13140 switch (nd_type(node)) {
13141 case NODE_STR:
13142 {
13143 /* Check string is valid regex */
13144 reg_compile(p, RNODE_STR(node)->string, options);
13145 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13146 }
13147 break;
13148 default:
13149 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13150 /* fall through */
13151 case NODE_DSTR:
13152 nd_set_type(node, NODE_DREGX);
13153 nd_set_loc(node, loc);
13154 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13155 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13156 if (dreg->nd_next) {
13157 dregex_fragment_setenc(p, dreg, options);
13158 }
13159 if (options & RE_OPTION_ONCE) {
13160 node = NEW_ONCE(node, loc);
13161 }
13162 break;
13163 }
13164 return node;
13165}
13166
13167static rb_node_kw_arg_t *
13168new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13169{
13170 if (!k) return 0;
13171 return NEW_KW_ARG((k), loc);
13172}
13173
13174static NODE *
13175new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13176{
13177 if (!node) {
13178 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13179 return xstr;
13180 }
13181 switch (nd_type(node)) {
13182 case NODE_STR:
13183 nd_set_type(node, NODE_XSTR);
13184 nd_set_loc(node, loc);
13185 break;
13186 case NODE_DSTR:
13187 nd_set_type(node, NODE_DXSTR);
13188 nd_set_loc(node, loc);
13189 break;
13190 default:
13191 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13192 break;
13193 }
13194 return node;
13195}
13196
13197static const
13198struct st_hash_type literal_type = {
13199 literal_cmp,
13200 literal_hash,
13201};
13202
13203static int nd_type_st_key_enable_p(NODE *node);
13204
13205static void
13206check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13207{
13208 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13209 if (!arg || !p->case_labels) return;
13210 if (!nd_type_st_key_enable_p(arg)) return;
13211
13212 if (p->case_labels == CHECK_LITERAL_WHEN) {
13213 p->case_labels = st_init_table(&literal_type);
13214 }
13215 else {
13216 st_data_t line;
13217 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13218 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13219 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13220 return;
13221 }
13222 }
13223 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13224}
13225
13226#ifdef RIPPER
13227static int
13228id_is_var(struct parser_params *p, ID id)
13229{
13230 if (is_notop_id(id)) {
13231 switch (id & ID_SCOPE_MASK) {
13232 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13233 return 1;
13234 case ID_LOCAL:
13235 if (dyna_in_block(p)) {
13236 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13237 }
13238 if (local_id(p, id)) return 1;
13239 /* method call without arguments */
13240 return 0;
13241 }
13242 }
13243 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13244 return 0;
13245}
13246#endif
13247
13248static inline enum lex_state_e
13249parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13250{
13251 if (p->debug) {
13252 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13253 }
13254 return p->lex.state = ls;
13255}
13256
13257#ifndef RIPPER
13258static void
13259flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13260{
13261 VALUE mesg = p->debug_buffer;
13262
13263 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13264 p->debug_buffer = Qnil;
13265 rb_io_puts(1, &mesg, out);
13266 }
13267 if (!NIL_P(str) && RSTRING_LEN(str)) {
13268 rb_io_write(p->debug_output, str);
13269 }
13270}
13271
13272static const char rb_parser_lex_state_names[][8] = {
13273 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13274 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13275 "LABEL", "LABELED","FITEM",
13276};
13277
13278static VALUE
13279append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13280{
13281 int i, sep = 0;
13282 unsigned int mask = 1;
13283 static const char none[] = "NONE";
13284
13285 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13286 if ((unsigned)state & mask) {
13287 if (sep) {
13288 rb_str_cat(buf, "|", 1);
13289 }
13290 sep = 1;
13291 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13292 }
13293 }
13294 if (!sep) {
13295 rb_str_cat(buf, none, sizeof(none)-1);
13296 }
13297 return buf;
13298}
13299
13300enum lex_state_e
13301rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13302 enum lex_state_e to, int line)
13303{
13304 VALUE mesg;
13305 mesg = rb_str_new_cstr("lex_state: ");
13306 append_lex_state_name(p, from, mesg);
13307 rb_str_cat_cstr(mesg, " -> ");
13308 append_lex_state_name(p, to, mesg);
13309 rb_str_catf(mesg, " at line %d\n", line);
13310 flush_debug_buffer(p, p->debug_output, mesg);
13311 return to;
13312}
13313
13314VALUE
13315rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13316{
13317 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13318}
13319
13320static void
13321append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13322{
13323 if (stack == 0) {
13324 rb_str_cat_cstr(mesg, "0");
13325 }
13326 else {
13327 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13328 for (; mask && !(stack & mask); mask >>= 1) continue;
13329 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13330 }
13331}
13332
13333void
13334rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13335 const char *name, int line)
13336{
13337 VALUE mesg = rb_sprintf("%s: ", name);
13338 append_bitstack_value(p, stack, mesg);
13339 rb_str_catf(mesg, " at line %d\n", line);
13340 flush_debug_buffer(p, p->debug_output, mesg);
13341}
13342
13343void
13344rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13345{
13346 va_list ap;
13347 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13348
13349 va_start(ap, fmt);
13350 rb_str_vcatf(mesg, fmt, ap);
13351 va_end(ap);
13352 yyerror0(RSTRING_PTR(mesg));
13353 RB_GC_GUARD(mesg);
13354
13355 mesg = rb_str_new(0, 0);
13356 append_lex_state_name(p, p->lex.state, mesg);
13357 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13358 rb_str_resize(mesg, 0);
13359 append_bitstack_value(p, p->cond_stack, mesg);
13360 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13361 rb_str_resize(mesg, 0);
13362 append_bitstack_value(p, p->cmdarg_stack, mesg);
13363 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13364 if (p->debug_output == rb_ractor_stdout())
13365 p->debug_output = rb_ractor_stderr();
13366 p->debug = TRUE;
13367}
13368
13369static YYLTYPE *
13370rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13371{
13372 yylloc->beg_pos.lineno = sourceline;
13373 yylloc->beg_pos.column = beg_pos;
13374 yylloc->end_pos.lineno = sourceline;
13375 yylloc->end_pos.column = end_pos;
13376 return yylloc;
13377}
13378
13379YYLTYPE *
13380rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13381{
13382 int sourceline = here->sourceline;
13383 int beg_pos = (int)here->offset - here->quote
13384 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13385 int end_pos = (int)here->offset + here->length + here->quote;
13386
13387 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13388}
13389
13390YYLTYPE *
13391rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13392{
13393 yylloc->beg_pos.lineno = p->delayed.beg_line;
13394 yylloc->beg_pos.column = p->delayed.beg_col;
13395 yylloc->end_pos.lineno = p->delayed.end_line;
13396 yylloc->end_pos.column = p->delayed.end_col;
13397
13398 return yylloc;
13399}
13400
13401YYLTYPE *
13402rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13403{
13404 int sourceline = p->ruby_sourceline;
13405 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13406 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13407 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13408}
13409
13410YYLTYPE *
13411rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13412{
13413 yylloc->end_pos = yylloc->beg_pos;
13414
13415 return yylloc;
13416}
13417
13418YYLTYPE *
13419rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13420{
13421 int sourceline = p->ruby_sourceline;
13422 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13423 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13424 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13425}
13426
13427YYLTYPE *
13428rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13429{
13430 int sourceline = p->ruby_sourceline;
13431 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13432 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13433 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13434}
13435#endif /* !RIPPER */
13436
13437static int
13438assignable0(struct parser_params *p, ID id, const char **err)
13439{
13440 if (!id) return -1;
13441 switch (id) {
13442 case keyword_self:
13443 *err = "Can't change the value of self";
13444 return -1;
13445 case keyword_nil:
13446 *err = "Can't assign to nil";
13447 return -1;
13448 case keyword_true:
13449 *err = "Can't assign to true";
13450 return -1;
13451 case keyword_false:
13452 *err = "Can't assign to false";
13453 return -1;
13454 case keyword__FILE__:
13455 *err = "Can't assign to __FILE__";
13456 return -1;
13457 case keyword__LINE__:
13458 *err = "Can't assign to __LINE__";
13459 return -1;
13460 case keyword__ENCODING__:
13461 *err = "Can't assign to __ENCODING__";
13462 return -1;
13463 }
13464 switch (id_type(id)) {
13465 case ID_LOCAL:
13466 if (dyna_in_block(p)) {
13467 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13468 compile_error(p, "Can't assign to numbered parameter _%d",
13469 NUMPARAM_ID_TO_IDX(id));
13470 return -1;
13471 }
13472 if (dvar_curr(p, id)) return NODE_DASGN;
13473 if (dvar_defined(p, id)) return NODE_DASGN;
13474 if (local_id(p, id)) return NODE_LASGN;
13475 dyna_var(p, id);
13476 return NODE_DASGN;
13477 }
13478 else {
13479 if (!local_id(p, id)) local_var(p, id);
13480 return NODE_LASGN;
13481 }
13482 break;
13483 case ID_GLOBAL: return NODE_GASGN;
13484 case ID_INSTANCE: return NODE_IASGN;
13485 case ID_CONST:
13486 if (!p->ctxt.in_def) return NODE_CDECL;
13487 *err = "dynamic constant assignment";
13488 return -1;
13489 case ID_CLASS: return NODE_CVASGN;
13490 default:
13491 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13492 }
13493 return -1;
13494}
13495
13496static NODE*
13497assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13498{
13499 const char *err = 0;
13500 int node_type = assignable0(p, id, &err);
13501 switch (node_type) {
13502 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13503 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13504 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13505 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13506 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13507 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13508 }
13509/* TODO: FIXME */
13510#ifndef RIPPER
13511 if (err) yyerror1(loc, err);
13512#else
13513 if (err) set_value(assign_error(p, err, p->s_lvalue));
13514#endif
13515 return NEW_ERROR(loc);
13516}
13517
13518static int
13519is_private_local_id(struct parser_params *p, ID name)
13520{
13521 VALUE s;
13522 if (name == idUScore) return 1;
13523 if (!is_local_id(name)) return 0;
13524 s = rb_id2str(name);
13525 if (!s) return 0;
13526 return RSTRING_PTR(s)[0] == '_';
13527}
13528
13529static int
13530shadowing_lvar_0(struct parser_params *p, ID name)
13531{
13532 if (dyna_in_block(p)) {
13533 if (dvar_curr(p, name)) {
13534 if (is_private_local_id(p, name)) return 1;
13535 yyerror0("duplicated argument name");
13536 }
13537 else if (dvar_defined(p, name) || local_id(p, name)) {
13538 vtable_add(p->lvtbl->vars, name);
13539 if (p->lvtbl->used) {
13540 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13541 }
13542 return 0;
13543 }
13544 }
13545 else {
13546 if (local_id(p, name)) {
13547 if (is_private_local_id(p, name)) return 1;
13548 yyerror0("duplicated argument name");
13549 }
13550 }
13551 return 1;
13552}
13553
13554static ID
13555shadowing_lvar(struct parser_params *p, ID name)
13556{
13557 shadowing_lvar_0(p, name);
13558 return name;
13559}
13560
13561static void
13562new_bv(struct parser_params *p, ID name)
13563{
13564 if (!name) return;
13565 if (!is_local_id(name)) {
13566 compile_error(p, "invalid local variable - %"PRIsVALUE,
13567 rb_id2str(name));
13568 return;
13569 }
13570 if (!shadowing_lvar_0(p, name)) return;
13571 dyna_var(p, name);
13572 ID *vidp = 0;
13573 if (dvar_defined_ref(p, name, &vidp)) {
13574 if (vidp) *vidp |= LVAR_USED;
13575 }
13576}
13577
13578static void
13579aryset_check(struct parser_params *p, NODE *args)
13580{
13581 NODE *block = 0, *kwds = 0;
13582 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13583 block = RNODE_BLOCK_PASS(args)->nd_body;
13584 args = RNODE_BLOCK_PASS(args)->nd_head;
13585 }
13586 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13587 args = RNODE_ARGSCAT(args)->nd_body;
13588 }
13589 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13590 kwds = RNODE_ARGSPUSH(args)->nd_body;
13591 }
13592 else {
13593 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13594 next = RNODE_LIST(next)->nd_next) {
13595 kwds = RNODE_LIST(next)->nd_head;
13596 }
13597 }
13598 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13599 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13600 }
13601 if (block) {
13602 yyerror1(&block->nd_loc, "block arg given in index assignment");
13603 }
13604}
13605
13606static NODE *
13607aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13608{
13609 aryset_check(p, idx);
13610 return NEW_ATTRASGN(recv, tASET, idx, loc);
13611}
13612
13613static void
13614block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13615{
13616 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13617 compile_error(p, "both block arg and actual block given");
13618 }
13619}
13620
13621static NODE *
13622attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13623{
13624 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13625 return NEW_ATTRASGN(recv, id, 0, loc);
13626}
13627
13628static VALUE
13629rb_backref_error(struct parser_params *p, NODE *node)
13630{
13631#ifndef RIPPER
13632# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13633#else
13634# define ERR(...) rb_sprintf(__VA_ARGS__)
13635#endif
13636 switch (nd_type(node)) {
13637 case NODE_NTH_REF:
13638 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13639 case NODE_BACK_REF:
13640 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13641 }
13642#undef ERR
13643 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13644}
13645
13646static NODE *
13647arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13648{
13649 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13650 switch (nd_type(node1)) {
13651 case NODE_LIST:
13652 return list_append(p, node1, node2);
13653 case NODE_BLOCK_PASS:
13654 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13655 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13656 return node1;
13657 case NODE_ARGSPUSH:
13658 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13659 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13660 nd_set_type(node1, NODE_ARGSCAT);
13661 return node1;
13662 case NODE_ARGSCAT:
13663 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13664 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13665 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13666 return node1;
13667 }
13668 return NEW_ARGSPUSH(node1, node2, loc);
13669}
13670
13671static NODE *
13672arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13673{
13674 if (!node2) return node1;
13675 switch (nd_type(node1)) {
13676 case NODE_BLOCK_PASS:
13677 if (RNODE_BLOCK_PASS(node1)->nd_head)
13678 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13679 else
13680 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13681 return node1;
13682 case NODE_ARGSPUSH:
13683 if (!nd_type_p(node2, NODE_LIST)) break;
13684 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13685 nd_set_type(node1, NODE_ARGSCAT);
13686 return node1;
13687 case NODE_ARGSCAT:
13688 if (!nd_type_p(node2, NODE_LIST) ||
13689 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13690 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13691 return node1;
13692 }
13693 return NEW_ARGSCAT(node1, node2, loc);
13694}
13695
13696static NODE *
13697last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13698{
13699 NODE *n1;
13700 if ((n1 = splat_array(args)) != 0) {
13701 return list_append(p, n1, last_arg);
13702 }
13703 return arg_append(p, args, last_arg, loc);
13704}
13705
13706static NODE *
13707rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13708{
13709 NODE *n1;
13710 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13711 return list_concat(n1, rest_arg);
13712 }
13713 return arg_concat(p, args, rest_arg, loc);
13714}
13715
13716static NODE *
13717splat_array(NODE* node)
13718{
13719 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13720 if (nd_type_p(node, NODE_LIST)) return node;
13721 return 0;
13722}
13723
13724static void
13725mark_lvar_used(struct parser_params *p, NODE *rhs)
13726{
13727 ID *vidp = NULL;
13728 if (!rhs) return;
13729 switch (nd_type(rhs)) {
13730 case NODE_LASGN:
13731 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13732 if (vidp) *vidp |= LVAR_USED;
13733 }
13734 break;
13735 case NODE_DASGN:
13736 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13737 if (vidp) *vidp |= LVAR_USED;
13738 }
13739 break;
13740#if 0
13741 case NODE_MASGN:
13742 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13743 mark_lvar_used(p, rhs->nd_head);
13744 }
13745 break;
13746#endif
13747 }
13748}
13749
13750static int is_static_content(NODE *node);
13751
13752static NODE *
13753node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13754{
13755 if (!lhs) return 0;
13756
13757 switch (nd_type(lhs)) {
13758 case NODE_CDECL:
13759 case NODE_GASGN:
13760 case NODE_IASGN:
13761 case NODE_LASGN:
13762 case NODE_DASGN:
13763 case NODE_MASGN:
13764 case NODE_CVASGN:
13765 set_nd_value(p, lhs, rhs);
13766 nd_set_loc(lhs, loc);
13767 break;
13768
13769 case NODE_ATTRASGN:
13770 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13771 nd_set_loc(lhs, loc);
13772 break;
13773
13774 default:
13775 /* should not happen */
13776 break;
13777 }
13778
13779 return lhs;
13780}
13781
13782static NODE *
13783value_expr_check(struct parser_params *p, NODE *node)
13784{
13785 NODE *void_node = 0, *vn;
13786
13787 if (!node) {
13788 rb_warning0("empty expression");
13789 }
13790 while (node) {
13791 switch (nd_type(node)) {
13792 case NODE_ENSURE:
13793 vn = RNODE_ENSURE(node)->nd_head;
13794 node = RNODE_ENSURE(node)->nd_ensr;
13795 /* nd_ensr should not be NULL, check it out next */
13796 if (vn && (vn = value_expr_check(p, vn))) {
13797 goto found;
13798 }
13799 break;
13800
13801 case NODE_RESCUE:
13802 /* void only if all children are void */
13803 vn = RNODE_RESCUE(node)->nd_head;
13804 if (!vn || !(vn = value_expr_check(p, vn))) return NULL;
13805 if (!void_node) void_node = vn;
13806 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13807 if (!nd_type_p(r, NODE_RESBODY)) {
13808 compile_error(p, "unexpected node");
13809 return NULL;
13810 }
13811 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13812 void_node = 0;
13813 break;
13814 }
13815 if (!void_node) void_node = vn;
13816 }
13817 node = RNODE_RESCUE(node)->nd_else;
13818 if (!node) return void_node;
13819 break;
13820
13821 case NODE_RETURN:
13822 case NODE_BREAK:
13823 case NODE_NEXT:
13824 case NODE_REDO:
13825 case NODE_RETRY:
13826 goto found;
13827
13828 case NODE_CASE3:
13829 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13830 compile_error(p, "unexpected node");
13831 return NULL;
13832 }
13833 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13834 return NULL;
13835 }
13836 /* single line pattern matching with "=>" operator */
13837 goto found;
13838
13839 case NODE_BLOCK:
13840 while (RNODE_BLOCK(node)->nd_next) {
13841 node = RNODE_BLOCK(node)->nd_next;
13842 }
13843 node = RNODE_BLOCK(node)->nd_head;
13844 break;
13845
13846 case NODE_BEGIN:
13847 node = RNODE_BEGIN(node)->nd_body;
13848 break;
13849
13850 case NODE_IF:
13851 case NODE_UNLESS:
13852 if (!RNODE_IF(node)->nd_body) {
13853 return NULL;
13854 }
13855 else if (!RNODE_IF(node)->nd_else) {
13856 return NULL;
13857 }
13858 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13859 if (!vn) return NULL;
13860 if (!void_node) void_node = vn;
13861 node = RNODE_IF(node)->nd_else;
13862 break;
13863
13864 case NODE_AND:
13865 case NODE_OR:
13866 node = RNODE_AND(node)->nd_1st;
13867 break;
13868
13869 case NODE_LASGN:
13870 case NODE_DASGN:
13871 case NODE_MASGN:
13872 mark_lvar_used(p, node);
13873 return NULL;
13874
13875 default:
13876 return NULL;
13877 }
13878 }
13879
13880 return NULL;
13881
13882 found:
13883 /* return the first found node */
13884 return void_node ? void_node : node;
13885}
13886
13887static int
13888value_expr_gen(struct parser_params *p, NODE *node)
13889{
13890 NODE *void_node = value_expr_check(p, node);
13891 if (void_node) {
13892 yyerror1(&void_node->nd_loc, "void value expression");
13893 /* or "control never reach"? */
13894 return FALSE;
13895 }
13896 return TRUE;
13897}
13898
13899static void
13900void_expr(struct parser_params *p, NODE *node)
13901{
13902 const char *useless = 0;
13903
13904 if (!RTEST(ruby_verbose)) return;
13905
13906 if (!node || !(node = nd_once_body(node))) return;
13907 switch (nd_type(node)) {
13908 case NODE_OPCALL:
13909 switch (RNODE_OPCALL(node)->nd_mid) {
13910 case '+':
13911 case '-':
13912 case '*':
13913 case '/':
13914 case '%':
13915 case tPOW:
13916 case tUPLUS:
13917 case tUMINUS:
13918 case '|':
13919 case '^':
13920 case '&':
13921 case tCMP:
13922 case '>':
13923 case tGEQ:
13924 case '<':
13925 case tLEQ:
13926 case tEQ:
13927 case tNEQ:
13928 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13929 break;
13930 }
13931 break;
13932
13933 case NODE_LVAR:
13934 case NODE_DVAR:
13935 case NODE_GVAR:
13936 case NODE_IVAR:
13937 case NODE_CVAR:
13938 case NODE_NTH_REF:
13939 case NODE_BACK_REF:
13940 useless = "a variable";
13941 break;
13942 case NODE_CONST:
13943 useless = "a constant";
13944 break;
13945 case NODE_SYM:
13946 case NODE_LINE:
13947 case NODE_FILE:
13948 case NODE_ENCODING:
13949 case NODE_INTEGER:
13950 case NODE_FLOAT:
13951 case NODE_RATIONAL:
13952 case NODE_IMAGINARY:
13953 case NODE_STR:
13954 case NODE_DSTR:
13955 case NODE_REGX:
13956 case NODE_DREGX:
13957 useless = "a literal";
13958 break;
13959 case NODE_COLON2:
13960 case NODE_COLON3:
13961 useless = "::";
13962 break;
13963 case NODE_DOT2:
13964 useless = "..";
13965 break;
13966 case NODE_DOT3:
13967 useless = "...";
13968 break;
13969 case NODE_SELF:
13970 useless = "self";
13971 break;
13972 case NODE_NIL:
13973 useless = "nil";
13974 break;
13975 case NODE_TRUE:
13976 useless = "true";
13977 break;
13978 case NODE_FALSE:
13979 useless = "false";
13980 break;
13981 case NODE_DEFINED:
13982 useless = "defined?";
13983 break;
13984 }
13985
13986 if (useless) {
13987 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
13988 }
13989}
13990
13991/* warns useless use of block and returns the last statement node */
13992static NODE *
13993void_stmts(struct parser_params *p, NODE *node)
13994{
13995 NODE *const n = node;
13996 if (!RTEST(ruby_verbose)) return n;
13997 if (!node) return n;
13998 if (!nd_type_p(node, NODE_BLOCK)) return n;
13999
14000 while (RNODE_BLOCK(node)->nd_next) {
14001 void_expr(p, RNODE_BLOCK(node)->nd_head);
14002 node = RNODE_BLOCK(node)->nd_next;
14003 }
14004 return RNODE_BLOCK(node)->nd_head;
14005}
14006
14007static NODE *
14008remove_begin(NODE *node)
14009{
14010 NODE **n = &node, *n1 = node;
14011 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14012 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14013 }
14014 return node;
14015}
14016
14017static void
14018reduce_nodes(struct parser_params *p, NODE **body)
14019{
14020 NODE *node = *body;
14021
14022 if (!node) {
14023 *body = NEW_NIL(&NULL_LOC);
14024 return;
14025 }
14026#define subnodes(type, n1, n2) \
14027 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14028 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14029 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14030
14031 while (node) {
14032 int newline = (int)nd_fl_newline(node);
14033 switch (nd_type(node)) {
14034 end:
14035 case NODE_NIL:
14036 *body = 0;
14037 return;
14038 case NODE_BEGIN:
14039 *body = node = RNODE_BEGIN(node)->nd_body;
14040 if (newline && node) nd_set_fl_newline(node);
14041 continue;
14042 case NODE_BLOCK:
14043 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14044 break;
14045 case NODE_IF:
14046 case NODE_UNLESS:
14047 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14048 return;
14049 case NODE_CASE:
14050 body = &RNODE_CASE(node)->nd_body;
14051 break;
14052 case NODE_WHEN:
14053 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14054 break;
14055 case NODE_ENSURE:
14056 body = &RNODE_ENSURE(node)->nd_head;
14057 break;
14058 case NODE_RESCUE:
14059 newline = 0; // RESBODY should not be a NEWLINE
14060 if (RNODE_RESCUE(node)->nd_else) {
14061 body = &RNODE_RESCUE(node)->nd_resq;
14062 break;
14063 }
14064 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14065 break;
14066 default:
14067 return;
14068 }
14069 node = *body;
14070 if (newline && node) nd_set_fl_newline(node);
14071 }
14072
14073#undef subnodes
14074}
14075
14076static int
14077is_static_content(NODE *node)
14078{
14079 if (!node) return 1;
14080 switch (nd_type(node)) {
14081 case NODE_HASH:
14082 if (!(node = RNODE_HASH(node)->nd_head)) break;
14083 case NODE_LIST:
14084 do {
14085 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14086 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14087 case NODE_SYM:
14088 case NODE_REGX:
14089 case NODE_LINE:
14090 case NODE_FILE:
14091 case NODE_ENCODING:
14092 case NODE_INTEGER:
14093 case NODE_FLOAT:
14094 case NODE_RATIONAL:
14095 case NODE_IMAGINARY:
14096 case NODE_STR:
14097 case NODE_NIL:
14098 case NODE_TRUE:
14099 case NODE_FALSE:
14100 case NODE_ZLIST:
14101 break;
14102 default:
14103 return 0;
14104 }
14105 return 1;
14106}
14107
14108static int
14109assign_in_cond(struct parser_params *p, NODE *node)
14110{
14111 switch (nd_type(node)) {
14112 case NODE_MASGN:
14113 case NODE_LASGN:
14114 case NODE_DASGN:
14115 case NODE_GASGN:
14116 case NODE_IASGN:
14117 case NODE_CVASGN:
14118 case NODE_CDECL:
14119 break;
14120
14121 default:
14122 return 0;
14123 }
14124
14125 if (!get_nd_value(p, node)) return 1;
14126 if (is_static_content(get_nd_value(p, node))) {
14127 /* reports always */
14128 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14129 }
14130 return 1;
14131}
14132
14133enum cond_type {
14134 COND_IN_OP,
14135 COND_IN_COND,
14136 COND_IN_FF
14137};
14138
14139#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14140 switch (t) { \
14141 case COND_IN_OP: break; \
14142 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14143 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14144 } \
14145} while (0)
14146
14147static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14148
14149static NODE*
14150range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14151{
14152 enum node_type type;
14153
14154 if (node == 0) return 0;
14155
14156 type = nd_type(node);
14157 value_expr(node);
14158 if (type == NODE_INTEGER) {
14159 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14160 ID lineno = rb_intern("$.");
14161 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14162 }
14163 return cond0(p, node, COND_IN_FF, loc, true);
14164}
14165
14166static NODE*
14167cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14168{
14169 if (node == 0) return 0;
14170 if (!(node = nd_once_body(node))) return 0;
14171 assign_in_cond(p, node);
14172
14173 switch (nd_type(node)) {
14174 case NODE_BEGIN:
14175 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14176 break;
14177
14178 case NODE_DSTR:
14179 case NODE_EVSTR:
14180 case NODE_STR:
14181 case NODE_FILE:
14182 SWITCH_BY_COND_TYPE(type, warn, "string ");
14183 break;
14184
14185 case NODE_REGX:
14186 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14187 nd_set_type(node, NODE_MATCH);
14188 break;
14189
14190 case NODE_DREGX:
14191 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14192
14193 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14194
14195 case NODE_BLOCK:
14196 {
14197 NODE *end = RNODE_BLOCK(node)->nd_end;
14198 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14199 if (top) top = node == end;
14200 *expr = cond0(p, *expr, type, loc, top);
14201 }
14202 break;
14203
14204 case NODE_AND:
14205 case NODE_OR:
14206 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14207 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14208 break;
14209
14210 case NODE_DOT2:
14211 case NODE_DOT3:
14212 if (!top) break;
14213 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14214 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14215 switch (nd_type(node)) {
14216 case NODE_DOT2:
14217 nd_set_type(node,NODE_FLIP2);
14218 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14219 (void)flip2;
14220 break;
14221 case NODE_DOT3:
14222 nd_set_type(node, NODE_FLIP3);
14223 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14224 (void)flip3;
14225 break;
14226 }
14227 break;
14228
14229 case NODE_SYM:
14230 case NODE_DSYM:
14231 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14232 break;
14233
14234 case NODE_LINE:
14235 SWITCH_BY_COND_TYPE(type, warning, "");
14236 break;
14237
14238 case NODE_ENCODING:
14239 SWITCH_BY_COND_TYPE(type, warning, "");
14240 break;
14241
14242 case NODE_INTEGER:
14243 case NODE_FLOAT:
14244 case NODE_RATIONAL:
14245 case NODE_IMAGINARY:
14246 SWITCH_BY_COND_TYPE(type, warning, "");
14247 break;
14248
14249 default:
14250 break;
14251 }
14252 return node;
14253}
14254
14255static NODE*
14256cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14257{
14258 if (node == 0) return 0;
14259 return cond0(p, node, COND_IN_COND, loc, true);
14260}
14261
14262static NODE*
14263method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14264{
14265 if (node == 0) return 0;
14266 return cond0(p, node, COND_IN_OP, loc, true);
14267}
14268
14269static NODE*
14270new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14271{
14272 YYLTYPE loc = {*pos, *pos};
14273 return NEW_NIL(&loc);
14274}
14275
14276static NODE*
14277new_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)
14278{
14279 if (!cc) return right;
14280 cc = cond0(p, cc, COND_IN_COND, loc, true);
14281 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14282}
14283
14284static NODE*
14285new_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)
14286{
14287 if (!cc) return right;
14288 cc = cond0(p, cc, COND_IN_COND, loc, true);
14289 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14290}
14291
14292#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))
14293
14294static NODE*
14295logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14296 const YYLTYPE *op_loc, const YYLTYPE *loc)
14297{
14298 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14299 NODE *op;
14300 value_expr(left);
14301 if (left && nd_type_p(left, type)) {
14302 NODE *node = left, *second;
14303 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14304 node = second;
14305 }
14306 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14307 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14308 left->nd_loc.end_pos = loc->end_pos;
14309 return left;
14310 }
14311 op = NEW_AND_OR(type, left, right, loc, op_loc);
14312 nd_set_line(op, op_loc->beg_pos.lineno);
14313 return op;
14314}
14315
14316#undef NEW_AND_OR
14317
14318static void
14319no_blockarg(struct parser_params *p, NODE *node)
14320{
14321 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14322 compile_error(p, "block argument should not be given");
14323 }
14324}
14325
14326static NODE *
14327ret_args(struct parser_params *p, NODE *node)
14328{
14329 if (node) {
14330 no_blockarg(p, node);
14331 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14332 node = RNODE_LIST(node)->nd_head;
14333 }
14334 }
14335 return node;
14336}
14337
14338static NODE *
14339new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
14340{
14341 if (node) no_blockarg(p, node);
14342
14343 return NEW_YIELD(node, loc, keyword_loc, lparen_loc, rparen_loc);
14344}
14345
14346static NODE*
14347negate_lit(struct parser_params *p, NODE* node)
14348{
14349 switch (nd_type(node)) {
14350 case NODE_INTEGER:
14351 RNODE_INTEGER(node)->minus = TRUE;
14352 break;
14353 case NODE_FLOAT:
14354 RNODE_FLOAT(node)->minus = TRUE;
14355 break;
14356 case NODE_RATIONAL:
14357 RNODE_RATIONAL(node)->minus = TRUE;
14358 break;
14359 case NODE_IMAGINARY:
14360 RNODE_IMAGINARY(node)->minus = TRUE;
14361 break;
14362 }
14363 return node;
14364}
14365
14366static NODE *
14367arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14368{
14369 if (node2) {
14370 if (!node1) return (NODE *)node2;
14371 node2->nd_head = node1;
14372 nd_set_first_lineno(node2, nd_first_lineno(node1));
14373 nd_set_first_column(node2, nd_first_column(node1));
14374 return (NODE *)node2;
14375 }
14376 return node1;
14377}
14378
14379static bool
14380args_info_empty_p(struct rb_args_info *args)
14381{
14382 if (args->pre_args_num) return false;
14383 if (args->post_args_num) return false;
14384 if (args->rest_arg) return false;
14385 if (args->opt_args) return false;
14386 if (args->block_arg) return false;
14387 if (args->kw_args) return false;
14388 if (args->kw_rest_arg) return false;
14389 return true;
14390}
14391
14392static rb_node_args_t *
14393new_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)
14394{
14395 struct rb_args_info *args = &tail->nd_ainfo;
14396
14397 if (args->forwarding) {
14398 if (rest_arg) {
14399 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14400 return tail;
14401 }
14402 rest_arg = idFWD_REST;
14403 }
14404
14405 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14406 args->pre_init = pre_args ? pre_args->nd_next : 0;
14407
14408 args->post_args_num = post_args ? post_args->nd_plen : 0;
14409 args->post_init = post_args ? post_args->nd_next : 0;
14410 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14411
14412 args->rest_arg = rest_arg;
14413
14414 args->opt_args = opt_args;
14415
14416#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14417 args->ruby2_keywords = args->forwarding;
14418#else
14419 args->ruby2_keywords = 0;
14420#endif
14421
14422 nd_set_loc(RNODE(tail), loc);
14423
14424 return tail;
14425}
14426
14427static rb_node_args_t *
14428new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14429{
14430 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14431 struct rb_args_info *args = &node->nd_ainfo;
14432 if (p->error_p) return node;
14433
14434 args->block_arg = block;
14435 args->kw_args = kw_args;
14436
14437 if (kw_args) {
14438 /*
14439 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14440 * variable order: k1, kr1, k2, &b, internal_id, krest
14441 * #=> <reorder>
14442 * variable order: kr1, k1, k2, internal_id, krest, &b
14443 */
14444 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14445 struct vtable *vtargs = p->lvtbl->args;
14446 rb_node_kw_arg_t *kwn = kw_args;
14447
14448 if (block) block = vtargs->tbl[vtargs->pos-1];
14449 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14450 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14451 while (kwn) {
14452 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14453 --kw_vars;
14454 --required_kw_vars;
14455 kwn = kwn->nd_next;
14456 }
14457
14458 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14459 ID vid = get_nd_vid(p, kwn->nd_body);
14460 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14461 *required_kw_vars++ = vid;
14462 }
14463 else {
14464 *kw_vars++ = vid;
14465 }
14466 }
14467
14468 arg_var(p, kw_bits);
14469 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14470 if (block) arg_var(p, block);
14471
14472 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14473 }
14474 else if (kw_rest_arg == idNil) {
14475 args->no_kwarg = 1;
14476 }
14477 else if (kw_rest_arg) {
14478 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14479 }
14480
14481 return node;
14482}
14483
14484static rb_node_args_t *
14485args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14486{
14487 if (max_numparam > NO_PARAM || it_id) {
14488 if (!args) {
14489 YYLTYPE loc = RUBY_INIT_YYLLOC();
14490 args = new_args_tail(p, 0, 0, 0, 0);
14491 nd_set_loc(RNODE(args), &loc);
14492 }
14493 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14494 }
14495 return args;
14496}
14497
14498static NODE*
14499new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14500{
14501 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14502
14503 if (pre_arg) {
14504 NODE *pre_args = NEW_LIST(pre_arg, loc);
14505 if (RNODE_ARYPTN(aryptn)->pre_args) {
14506 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14507 }
14508 else {
14509 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14510 }
14511 }
14512 return aryptn;
14513}
14514
14515static NODE*
14516new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14517{
14518 if (has_rest) {
14519 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14520 }
14521 else {
14522 rest_arg = NULL;
14523 }
14524 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14525
14526 return node;
14527}
14528
14529static NODE*
14530new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14531{
14532 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14533
14534 return fndptn;
14535}
14536
14537static NODE*
14538new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14539{
14540 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14541 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14542 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14543
14544 return node;
14545}
14546
14547static NODE*
14548new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14549{
14550 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14551 return hshptn;
14552}
14553
14554static NODE*
14555new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14556{
14557 NODE *node, *kw_rest_arg_node;
14558
14559 if (kw_rest_arg == idNil) {
14560 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14561 }
14562 else if (kw_rest_arg) {
14563 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14564 }
14565 else {
14566 kw_rest_arg_node = NULL;
14567 }
14568
14569 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14570
14571 return node;
14572}
14573
14574static NODE*
14575dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14576{
14577 if (!node) {
14578 return NEW_SYM(STR_NEW0(), loc);
14579 }
14580
14581 switch (nd_type(node)) {
14582 case NODE_DSTR:
14583 nd_set_type(node, NODE_DSYM);
14584 nd_set_loc(node, loc);
14585 break;
14586 case NODE_STR:
14587 node = str_to_sym_node(p, node, loc);
14588 break;
14589 default:
14590 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14591 break;
14592 }
14593 return node;
14594}
14595
14596static int
14597nd_type_st_key_enable_p(NODE *node)
14598{
14599 switch (nd_type(node)) {
14600 case NODE_INTEGER:
14601 case NODE_FLOAT:
14602 case NODE_RATIONAL:
14603 case NODE_IMAGINARY:
14604 case NODE_STR:
14605 case NODE_SYM:
14606 case NODE_REGX:
14607 case NODE_LINE:
14608 case NODE_FILE:
14609 case NODE_ENCODING:
14610 return true;
14611 default:
14612 return false;
14613 }
14614}
14615
14616static VALUE
14617nd_value(struct parser_params *p, NODE *node)
14618{
14619 switch (nd_type(node)) {
14620 case NODE_STR:
14621 return rb_node_str_string_val(node);
14622 case NODE_INTEGER:
14623 return rb_node_integer_literal_val(node);
14624 case NODE_FLOAT:
14625 return rb_node_float_literal_val(node);
14626 case NODE_RATIONAL:
14627 return rb_node_rational_literal_val(node);
14628 case NODE_IMAGINARY:
14629 return rb_node_imaginary_literal_val(node);
14630 case NODE_SYM:
14631 return rb_node_sym_string_val(node);
14632 case NODE_REGX:
14633 return rb_node_regx_string_val(node);
14634 case NODE_LINE:
14635 return rb_node_line_lineno_val(node);
14636 case NODE_ENCODING:
14637 return rb_node_encoding_val(node);
14638 case NODE_FILE:
14639 return rb_node_file_path_val(node);
14640 default:
14641 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14642 UNREACHABLE_RETURN(0);
14643 }
14644}
14645
14646static void
14647warn_duplicate_keys(struct parser_params *p, NODE *hash)
14648{
14649 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14650 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14651 while (hash && RNODE_LIST(hash)->nd_next) {
14652 NODE *head = RNODE_LIST(hash)->nd_head;
14653 NODE *value = RNODE_LIST(hash)->nd_next;
14654 NODE *next = RNODE_LIST(value)->nd_next;
14655 st_data_t key;
14656 st_data_t data;
14657
14658 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14659 if (!head) {
14660 head = value;
14661 }
14662
14663 if (nd_type_st_key_enable_p(head)) {
14664 key = (st_data_t)head;
14665
14666 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14667 rb_warn2L(nd_line((NODE *)data),
14668 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14669 nd_value(p, head), WARN_I(nd_line(head)));
14670 }
14671 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14672 }
14673 hash = next;
14674 }
14675 st_free_table(p->warn_duplicate_keys_table);
14676 p->warn_duplicate_keys_table = NULL;
14677}
14678
14679static NODE *
14680new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14681{
14682 if (hash) warn_duplicate_keys(p, hash);
14683 return NEW_HASH(hash, loc);
14684}
14685
14686static void
14687error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14688{
14689 if (is_private_local_id(p, id)) {
14690 return;
14691 }
14692 if (st_is_member(p->pvtbl, id)) {
14693 yyerror1(loc, "duplicated variable name");
14694 }
14695 else {
14696 st_insert(p->pvtbl, (st_data_t)id, 0);
14697 }
14698}
14699
14700static void
14701error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14702{
14703 if (!p->pktbl) {
14704 p->pktbl = st_init_numtable();
14705 }
14706 else if (st_is_member(p->pktbl, key)) {
14707 yyerror1(loc, "duplicated key name");
14708 return;
14709 }
14710 st_insert(p->pktbl, (st_data_t)key, 0);
14711}
14712
14713static NODE *
14714new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14715{
14716 return NEW_HASH(hash, loc);
14717}
14718
14719static NODE *
14720new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14721{
14722 NODE *asgn;
14723
14724 if (lhs) {
14725 ID vid = get_nd_vid(p, lhs);
14726 YYLTYPE lhs_loc = lhs->nd_loc;
14727 if (op == tOROP) {
14728 set_nd_value(p, lhs, rhs);
14729 nd_set_loc(lhs, loc);
14730 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14731 }
14732 else if (op == tANDOP) {
14733 set_nd_value(p, lhs, rhs);
14734 nd_set_loc(lhs, loc);
14735 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14736 }
14737 else {
14738 asgn = lhs;
14739 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14740 set_nd_value(p, asgn, rhs);
14741 nd_set_loc(asgn, loc);
14742 }
14743 }
14744 else {
14745 asgn = NEW_ERROR(loc);
14746 }
14747 return asgn;
14748}
14749
14750static NODE *
14751new_ary_op_assign(struct parser_params *p, NODE *ary,
14752 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14753 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14754{
14755 NODE *asgn;
14756
14757 aryset_check(p, args);
14758 args = make_list(args, args_loc);
14759 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14760 fixpos(asgn, ary);
14761 return asgn;
14762}
14763
14764static NODE *
14765new_attr_op_assign(struct parser_params *p, NODE *lhs,
14766 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14767 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14768{
14769 NODE *asgn;
14770
14771 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14772 fixpos(asgn, lhs);
14773 return asgn;
14774}
14775
14776static NODE *
14777new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14778{
14779 NODE *asgn;
14780
14781 if (lhs) {
14782 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14783 }
14784 else {
14785 asgn = NEW_ERROR(loc);
14786 }
14787 fixpos(asgn, lhs);
14788 return asgn;
14789}
14790
14791static NODE *
14792const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14793{
14794 if (p->ctxt.in_def) {
14795#ifndef RIPPER
14796 yyerror1(loc, "dynamic constant assignment");
14797#else
14798 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14799#endif
14800 }
14801 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14802}
14803
14804#ifdef RIPPER
14805static VALUE
14806assign_error(struct parser_params *p, const char *mesg, VALUE a)
14807{
14808 a = dispatch2(assign_error, ERR_MESG(), a);
14809 ripper_error(p);
14810 return a;
14811}
14812#endif
14813
14814static NODE *
14815new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14816{
14817 NODE *result = head;
14818 if (rescue) {
14819 NODE *tmp = rescue_else ? rescue_else : rescue;
14820 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14821
14822 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14823 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14824 }
14825 if (ensure) {
14826 result = NEW_ENSURE(result, ensure, loc);
14827 }
14828 fixpos(result, head);
14829 return result;
14830}
14831
14832static void
14833warn_unused_var(struct parser_params *p, struct local_vars *local)
14834{
14835 int cnt;
14836
14837 if (!local->used) return;
14838 cnt = local->used->pos;
14839 if (cnt != local->vars->pos) {
14840 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14841 }
14842#ifndef RIPPER
14843 ID *v = local->vars->tbl;
14844 ID *u = local->used->tbl;
14845 for (int i = 0; i < cnt; ++i) {
14846 if (!v[i] || (u[i] & LVAR_USED)) continue;
14847 if (is_private_local_id(p, v[i])) continue;
14848 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14849 }
14850#endif
14851}
14852
14853static void
14854local_push(struct parser_params *p, int toplevel_scope)
14855{
14856 struct local_vars *local;
14857 int inherits_dvars = toplevel_scope && compile_for_eval;
14858 int warn_unused_vars = RTEST(ruby_verbose);
14859
14860 local = ALLOC(struct local_vars);
14861 local->prev = p->lvtbl;
14862 local->args = vtable_alloc(0);
14863 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14864#ifndef RIPPER
14865 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14866 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14867#endif
14868 local->numparam.outer = 0;
14869 local->numparam.inner = 0;
14870 local->numparam.current = 0;
14871 local->it = 0;
14872 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14873
14874# if WARN_PAST_SCOPE
14875 local->past = 0;
14876# endif
14877 CMDARG_PUSH(0);
14878 COND_PUSH(0);
14879 p->lvtbl = local;
14880}
14881
14882static void
14883vtable_chain_free(struct parser_params *p, struct vtable *table)
14884{
14885 while (!DVARS_TERMINAL_P(table)) {
14886 struct vtable *cur_table = table;
14887 table = cur_table->prev;
14888 vtable_free(cur_table);
14889 }
14890}
14891
14892static void
14893local_free(struct parser_params *p, struct local_vars *local)
14894{
14895 vtable_chain_free(p, local->used);
14896
14897# if WARN_PAST_SCOPE
14898 vtable_chain_free(p, local->past);
14899# endif
14900
14901 vtable_chain_free(p, local->args);
14902 vtable_chain_free(p, local->vars);
14903
14904 ruby_sized_xfree(local, sizeof(struct local_vars));
14905}
14906
14907static void
14908local_pop(struct parser_params *p)
14909{
14910 struct local_vars *local = p->lvtbl->prev;
14911 if (p->lvtbl->used) {
14912 warn_unused_var(p, p->lvtbl);
14913 }
14914
14915 local_free(p, p->lvtbl);
14916 p->lvtbl = local;
14917
14918 CMDARG_POP();
14919 COND_POP();
14920}
14921
14922static rb_ast_id_table_t *
14923local_tbl(struct parser_params *p)
14924{
14925 int cnt_args = vtable_size(p->lvtbl->args);
14926 int cnt_vars = vtable_size(p->lvtbl->vars);
14927 int cnt = cnt_args + cnt_vars;
14928 int i, j;
14929 rb_ast_id_table_t *tbl;
14930
14931 if (cnt <= 0) return 0;
14932 tbl = rb_ast_new_local_table(p->ast, cnt);
14933 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14934 /* remove IDs duplicated to warn shadowing */
14935 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14936 ID id = p->lvtbl->vars->tbl[i];
14937 if (!vtable_included(p->lvtbl->args, id)) {
14938 tbl->ids[j++] = id;
14939 }
14940 }
14941 if (j < cnt) {
14942 tbl = rb_ast_resize_latest_local_table(p->ast, j);
14943 }
14944
14945 return tbl;
14946}
14947
14948static void
14949numparam_name(struct parser_params *p, ID id)
14950{
14951 if (!NUMPARAM_ID_P(id)) return;
14952 compile_error(p, "_%d is reserved for numbered parameter",
14953 NUMPARAM_ID_TO_IDX(id));
14954}
14955
14956static void
14957arg_var(struct parser_params *p, ID id)
14958{
14959 numparam_name(p, id);
14960 vtable_add(p->lvtbl->args, id);
14961}
14962
14963static void
14964local_var(struct parser_params *p, ID id)
14965{
14966 numparam_name(p, id);
14967 vtable_add(p->lvtbl->vars, id);
14968 if (p->lvtbl->used) {
14969 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
14970 }
14971}
14972
14973#ifndef RIPPER
14974int
14975rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
14976{
14977 return rb_local_defined(id, iseq);
14978}
14979#endif
14980
14981static int
14982local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
14983{
14984 struct vtable *vars, *args, *used;
14985
14986 vars = p->lvtbl->vars;
14987 args = p->lvtbl->args;
14988 used = p->lvtbl->used;
14989
14990 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
14991 vars = vars->prev;
14992 args = args->prev;
14993 if (used) used = used->prev;
14994 }
14995
14996 if (vars && vars->prev == DVARS_INHERIT) {
14997 return rb_parser_local_defined(p, id, p->parent_iseq);
14998 }
14999 else if (vtable_included(args, id)) {
15000 return 1;
15001 }
15002 else {
15003 int i = vtable_included(vars, id);
15004 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15005 return i != 0;
15006 }
15007}
15008
15009static int
15010local_id(struct parser_params *p, ID id)
15011{
15012 return local_id_ref(p, id, NULL);
15013}
15014
15015static int
15016check_forwarding_args(struct parser_params *p)
15017{
15018 if (local_id(p, idFWD_ALL)) return TRUE;
15019 compile_error(p, "unexpected ...");
15020 return FALSE;
15021}
15022
15023static void
15024add_forwarding_args(struct parser_params *p)
15025{
15026 arg_var(p, idFWD_REST);
15027#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15028 arg_var(p, idFWD_KWREST);
15029#endif
15030 arg_var(p, idFWD_BLOCK);
15031 arg_var(p, idFWD_ALL);
15032}
15033
15034static void
15035forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15036{
15037 bool conflict = false;
15038
15039 struct vtable *vars, *args;
15040
15041 vars = p->lvtbl->vars;
15042 args = p->lvtbl->args;
15043
15044 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15045 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15046 vars = vars->prev;
15047 args = args->prev;
15048 }
15049
15050 bool found = false;
15051 if (vars && vars->prev == DVARS_INHERIT && !found) {
15052 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15053 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15054 }
15055 else {
15056 found = (vtable_included(args, arg) &&
15057 !(all && vtable_included(args, all)));
15058 }
15059
15060 if (!found) {
15061 compile_error(p, "no anonymous %s parameter", var);
15062 }
15063 else if (conflict) {
15064 compile_error(p, "anonymous %s parameter is also used within block", var);
15065 }
15066}
15067
15068static NODE *
15069new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15070{
15071 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15072#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15073 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15074#endif
15075 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15076 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15077 block->forwarding = TRUE;
15078#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15079 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15080#endif
15081 return arg_blk_pass(args, block);
15082}
15083
15084static NODE *
15085numparam_push(struct parser_params *p)
15086{
15087 struct local_vars *local = p->lvtbl;
15088 NODE *inner = local->numparam.inner;
15089 if (!local->numparam.outer) {
15090 local->numparam.outer = local->numparam.current;
15091 }
15092 local->numparam.inner = 0;
15093 local->numparam.current = 0;
15094 local->it = 0;
15095 return inner;
15096}
15097
15098static void
15099numparam_pop(struct parser_params *p, NODE *prev_inner)
15100{
15101 struct local_vars *local = p->lvtbl;
15102 if (prev_inner) {
15103 /* prefer first one */
15104 local->numparam.inner = prev_inner;
15105 }
15106 else if (local->numparam.current) {
15107 /* current and inner are exclusive */
15108 local->numparam.inner = local->numparam.current;
15109 }
15110 if (p->max_numparam > NO_PARAM) {
15111 /* current and outer are exclusive */
15112 local->numparam.current = local->numparam.outer;
15113 local->numparam.outer = 0;
15114 }
15115 else {
15116 /* no numbered parameter */
15117 local->numparam.current = 0;
15118 }
15119 local->it = 0;
15120}
15121
15122static const struct vtable *
15123dyna_push(struct parser_params *p)
15124{
15125 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15126 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15127 if (p->lvtbl->used) {
15128 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15129 }
15130 return p->lvtbl->args;
15131}
15132
15133static void
15134dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15135{
15136 struct vtable *tmp = *vtblp;
15137 *vtblp = tmp->prev;
15138# if WARN_PAST_SCOPE
15139 if (p->past_scope_enabled) {
15140 tmp->prev = p->lvtbl->past;
15141 p->lvtbl->past = tmp;
15142 return;
15143 }
15144# endif
15145 vtable_free(tmp);
15146}
15147
15148static void
15149dyna_pop_1(struct parser_params *p)
15150{
15151 struct vtable *tmp;
15152
15153 if ((tmp = p->lvtbl->used) != 0) {
15154 warn_unused_var(p, p->lvtbl);
15155 p->lvtbl->used = p->lvtbl->used->prev;
15156 vtable_free(tmp);
15157 }
15158 dyna_pop_vtable(p, &p->lvtbl->args);
15159 dyna_pop_vtable(p, &p->lvtbl->vars);
15160}
15161
15162static void
15163dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15164{
15165 while (p->lvtbl->args != lvargs) {
15166 dyna_pop_1(p);
15167 if (!p->lvtbl->args) {
15168 struct local_vars *local = p->lvtbl->prev;
15169 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15170 p->lvtbl = local;
15171 }
15172 }
15173 dyna_pop_1(p);
15174}
15175
15176static int
15177dyna_in_block(struct parser_params *p)
15178{
15179 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15180}
15181
15182#ifndef RIPPER
15183int
15184dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15185{
15186 struct vtable *vars, *args, *used;
15187 int i;
15188
15189 args = p->lvtbl->args;
15190 vars = p->lvtbl->vars;
15191 used = p->lvtbl->used;
15192
15193 while (!DVARS_TERMINAL_P(vars)) {
15194 if (vtable_included(args, id)) {
15195 return 1;
15196 }
15197 if ((i = vtable_included(vars, id)) != 0) {
15198 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15199 return 1;
15200 }
15201 args = args->prev;
15202 vars = vars->prev;
15203 if (!vidrefp) used = 0;
15204 if (used) used = used->prev;
15205 }
15206
15207 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15208 return rb_dvar_defined(id, p->parent_iseq);
15209 }
15210
15211 return 0;
15212}
15213#endif
15214
15215static int
15216dvar_defined(struct parser_params *p, ID id)
15217{
15218 return dvar_defined_ref(p, id, NULL);
15219}
15220
15221static int
15222dvar_curr(struct parser_params *p, ID id)
15223{
15224 return (vtable_included(p->lvtbl->args, id) ||
15225 vtable_included(p->lvtbl->vars, id));
15226}
15227
15228static void
15229reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15230{
15231 compile_error(p,
15232 "regexp encoding option '%c' differs from source encoding '%s'",
15233 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15234}
15235
15236#ifndef RIPPER
15237static rb_encoding *
15238find_enc(struct parser_params* p, const char *name)
15239{
15240 int idx = rb_enc_find_index(name);
15241 if (idx < 0) {
15242 rb_bug("unknown encoding name: %s", name);
15243 }
15244
15245 return rb_enc_from_index(idx);
15246}
15247
15248static rb_encoding *
15249kcode_to_enc(struct parser_params* p, int kcode)
15250{
15251 rb_encoding *enc;
15252
15253 switch (kcode) {
15254 case ENC_ASCII8BIT:
15255 enc = rb_ascii8bit_encoding();
15256 break;
15257 case ENC_EUC_JP:
15258 enc = find_enc(p, "EUC-JP");
15259 break;
15260 case ENC_Windows_31J:
15261 enc = find_enc(p, "Windows-31J");
15262 break;
15263 case ENC_UTF8:
15264 enc = rb_utf8_encoding();
15265 break;
15266 default:
15267 enc = NULL;
15268 break;
15269 }
15270
15271 return enc;
15272}
15273
15274int
15275rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15276{
15277 int c = RE_OPTION_ENCODING_IDX(options);
15278
15279 if (c) {
15280 int opt, idx;
15281 rb_encoding *enc;
15282
15283 char_to_option_kcode(c, &opt, &idx);
15284 enc = kcode_to_enc(p, idx);
15285 if (enc != rb_parser_str_get_encoding(str) &&
15286 !rb_parser_is_ascii_string(p, str)) {
15287 goto error;
15288 }
15289 rb_parser_string_set_encoding(str, enc);
15290 }
15291 else if (RE_OPTION_ENCODING_NONE(options)) {
15292 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15293 !rb_parser_is_ascii_string(p, str)) {
15294 c = 'n';
15295 goto error;
15296 }
15297 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15298 }
15299 else if (rb_is_usascii_enc(p->enc)) {
15300 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15301 }
15302 return 0;
15303
15304 error:
15305 return c;
15306}
15307#endif
15308
15309static void
15310reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15311{
15312 int c = rb_reg_fragment_setenc(p, str, options);
15313 if (c) reg_fragment_enc_error(p, str, c);
15314}
15315
15316#ifndef UNIVERSAL_PARSER
15317typedef struct {
15318 struct parser_params* parser;
15319 rb_encoding *enc;
15320 NODE *succ_block;
15321 const YYLTYPE *loc;
15322 rb_parser_assignable_func assignable;
15323} reg_named_capture_assign_t;
15324
15325static int
15326reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15327 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15328{
15329 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15330 struct parser_params* p = arg->parser;
15331 rb_encoding *enc = arg->enc;
15332 long len = name_end - name;
15333 const char *s = (const char *)name;
15334
15335 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15336}
15337
15338static NODE *
15339reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15340{
15341 reg_named_capture_assign_t arg;
15342
15343 arg.parser = p;
15344 arg.enc = rb_enc_get(regexp);
15345 arg.succ_block = 0;
15346 arg.loc = loc;
15347 arg.assignable = assignable;
15348 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15349
15350 if (!arg.succ_block) return 0;
15351 return RNODE_BLOCK(arg.succ_block)->nd_next;
15352}
15353#endif
15354
15355#ifndef RIPPER
15356NODE *
15357rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15358{
15359 return assignable(p, id, val, loc);
15360}
15361
15362int
15363rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15364 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15365{
15366 ID var;
15367 NODE *node, *succ;
15368
15369 if (!len) return ST_CONTINUE;
15370 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15371 return ST_CONTINUE;
15372
15373 var = intern_cstr(s, len, enc);
15374 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15375 if (!lvar_defined(p, var)) return ST_CONTINUE;
15376 }
15377 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15378 succ = *succ_block;
15379 if (!succ) succ = NEW_ERROR(loc);
15380 succ = block_append(p, succ, node);
15381 *succ_block = succ;
15382 return ST_CONTINUE;
15383}
15384#endif
15385
15386static VALUE
15387parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15388{
15389 VALUE str2;
15390 reg_fragment_setenc(p, str, options);
15391 str2 = rb_str_new_parser_string(str);
15392 return rb_parser_reg_compile(p, str2, options);
15393}
15394
15395#ifndef RIPPER
15396VALUE
15397rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15398{
15399 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15400}
15401#endif
15402
15403static VALUE
15404reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15405{
15406 VALUE re;
15407 VALUE err;
15408
15409 err = rb_errinfo();
15410 re = parser_reg_compile(p, str, options);
15411 if (NIL_P(re)) {
15412 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15413 rb_set_errinfo(err);
15414 compile_error(p, "%"PRIsVALUE, m);
15415 return Qnil;
15416 }
15417 return re;
15418}
15419
15420#ifndef RIPPER
15421void
15422rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15423{
15424 p->do_print = print;
15425 p->do_loop = loop;
15426 p->do_chomp = chomp;
15427 p->do_split = split;
15428}
15429
15430static NODE *
15431parser_append_options(struct parser_params *p, NODE *node)
15432{
15433 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15434 const YYLTYPE *const LOC = &default_location;
15435
15436 if (p->do_print) {
15437 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15438 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15439 LOC);
15440 node = block_append(p, node, print);
15441 }
15442
15443 if (p->do_loop) {
15444 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15445
15446 if (p->do_split) {
15447 ID ifs = rb_intern("$;");
15448 ID fields = rb_intern("$F");
15449 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15450 NODE *split = NEW_GASGN(fields,
15451 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15452 rb_intern("split"), args, LOC),
15453 LOC);
15454 node = block_append(p, split, node);
15455 }
15456 if (p->do_chomp) {
15457 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15458 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15459 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15460 }
15461
15462 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15463 }
15464
15465 return node;
15466}
15467
15468void
15469rb_init_parse(void)
15470{
15471 /* just to suppress unused-function warnings */
15472 (void)nodetype;
15473 (void)nodeline;
15474}
15475
15476ID
15477internal_id(struct parser_params *p)
15478{
15479 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15480}
15481#endif /* !RIPPER */
15482
15483static void
15484parser_initialize(struct parser_params *p)
15485{
15486 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15487 p->command_start = TRUE;
15488 p->ruby_sourcefile_string = Qnil;
15489 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15490 string_buffer_init(p);
15491 p->node_id = 0;
15492 p->delayed.token = NULL;
15493 p->frozen_string_literal = -1; /* not specified */
15494#ifndef RIPPER
15495 p->error_buffer = Qfalse;
15496 p->end_expect_token_locations = NULL;
15497 p->token_id = 0;
15498 p->tokens = NULL;
15499#else
15500 p->result = Qnil;
15501 p->parsing_thread = Qnil;
15502 p->s_value = Qnil;
15503 p->s_lvalue = Qnil;
15504 p->s_value_stack = rb_ary_new();
15505#endif
15506 p->debug_buffer = Qnil;
15507 p->debug_output = rb_ractor_stdout();
15508 p->enc = rb_utf8_encoding();
15509 p->exits = 0;
15510}
15511
15512#ifdef RIPPER
15513#define rb_ruby_parser_mark ripper_parser_mark
15514#define rb_ruby_parser_free ripper_parser_free
15515#define rb_ruby_parser_memsize ripper_parser_memsize
15516#endif
15517
15518void
15519rb_ruby_parser_mark(void *ptr)
15520{
15521 struct parser_params *p = (struct parser_params*)ptr;
15522
15523 rb_gc_mark(p->ruby_sourcefile_string);
15524#ifndef RIPPER
15525 rb_gc_mark(p->error_buffer);
15526#else
15527 rb_gc_mark(p->value);
15528 rb_gc_mark(p->result);
15529 rb_gc_mark(p->parsing_thread);
15530 rb_gc_mark(p->s_value);
15531 rb_gc_mark(p->s_lvalue);
15532 rb_gc_mark(p->s_value_stack);
15533#endif
15534 rb_gc_mark(p->debug_buffer);
15535 rb_gc_mark(p->debug_output);
15536}
15537
15538void
15539rb_ruby_parser_free(void *ptr)
15540{
15541 struct parser_params *p = (struct parser_params*)ptr;
15542 struct local_vars *local, *prev;
15543
15544 if (p->ast) {
15545 rb_ast_free(p->ast);
15546 }
15547
15548 if (p->warn_duplicate_keys_table) {
15549 st_free_table(p->warn_duplicate_keys_table);
15550 }
15551
15552#ifndef RIPPER
15553 if (p->tokens) {
15554 rb_parser_ary_free(p, p->tokens);
15555 }
15556#endif
15557
15558 if (p->tokenbuf) {
15559 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15560 }
15561
15562 for (local = p->lvtbl; local; local = prev) {
15563 prev = local->prev;
15564 local_free(p, local);
15565 }
15566
15567 {
15568 token_info *ptinfo;
15569 while ((ptinfo = p->token_info) != 0) {
15570 p->token_info = ptinfo->next;
15571 xfree(ptinfo);
15572 }
15573 }
15574 string_buffer_free(p);
15575
15576 if (p->pvtbl) {
15577 st_free_table(p->pvtbl);
15578 }
15579
15580 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15581 st_free_table(p->case_labels);
15582 }
15583
15584 xfree(p->lex.strterm);
15585 p->lex.strterm = 0;
15586
15587 xfree(ptr);
15588}
15589
15590size_t
15591rb_ruby_parser_memsize(const void *ptr)
15592{
15593 struct parser_params *p = (struct parser_params*)ptr;
15594 struct local_vars *local;
15595 size_t size = sizeof(*p);
15596
15597 size += p->toksiz;
15598 for (local = p->lvtbl; local; local = local->prev) {
15599 size += sizeof(*local);
15600 if (local->vars) size += local->vars->capa * sizeof(ID);
15601 }
15602 return size;
15603}
15604
15605#ifndef RIPPER
15606#undef rb_reserved_word
15607
15608const struct kwtable *
15609rb_reserved_word(const char *str, unsigned int len)
15610{
15611 return reserved_word(str, len);
15612}
15613
15614#ifdef UNIVERSAL_PARSER
15615rb_parser_t *
15616rb_ruby_parser_allocate(const rb_parser_config_t *config)
15617{
15618 /* parser_initialize expects fields to be set to 0 */
15619 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15620 p->config = config;
15621 return p;
15622}
15623
15624rb_parser_t *
15625rb_ruby_parser_new(const rb_parser_config_t *config)
15626{
15627 /* parser_initialize expects fields to be set to 0 */
15628 rb_parser_t *p = rb_ruby_parser_allocate(config);
15629 parser_initialize(p);
15630 return p;
15631}
15632#else
15633rb_parser_t *
15634rb_ruby_parser_allocate(void)
15635{
15636 /* parser_initialize expects fields to be set to 0 */
15637 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15638 return p;
15639}
15640
15641rb_parser_t *
15642rb_ruby_parser_new(void)
15643{
15644 /* parser_initialize expects fields to be set to 0 */
15645 rb_parser_t *p = rb_ruby_parser_allocate();
15646 parser_initialize(p);
15647 return p;
15648}
15649#endif
15650
15651rb_parser_t *
15652rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15653{
15654 p->error_buffer = main ? Qfalse : Qnil;
15655 p->parent_iseq = base;
15656 return p;
15657}
15658
15659void
15660rb_ruby_parser_set_script_lines(rb_parser_t *p)
15661{
15662 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15663}
15664
15665void
15666rb_ruby_parser_error_tolerant(rb_parser_t *p)
15667{
15668 p->error_tolerant = 1;
15669}
15670
15671void
15672rb_ruby_parser_keep_tokens(rb_parser_t *p)
15673{
15674 p->keep_tokens = 1;
15675 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15676}
15677
15678rb_encoding *
15679rb_ruby_parser_encoding(rb_parser_t *p)
15680{
15681 return p->enc;
15682}
15683
15684int
15685rb_ruby_parser_end_seen_p(rb_parser_t *p)
15686{
15687 return p->ruby__end__seen;
15688}
15689
15690int
15691rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15692{
15693 p->debug = flag;
15694 return flag;
15695}
15696#endif /* !RIPPER */
15697
15698#ifdef RIPPER
15699int
15700rb_ruby_parser_get_yydebug(rb_parser_t *p)
15701{
15702 return p->debug;
15703}
15704
15705void
15706rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15707{
15708 p->value = value;
15709}
15710
15711int
15712rb_ruby_parser_error_p(rb_parser_t *p)
15713{
15714 return p->error_p;
15715}
15716
15717VALUE
15718rb_ruby_parser_debug_output(rb_parser_t *p)
15719{
15720 return p->debug_output;
15721}
15722
15723void
15724rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15725{
15726 p->debug_output = output;
15727}
15728
15729VALUE
15730rb_ruby_parser_parsing_thread(rb_parser_t *p)
15731{
15732 return p->parsing_thread;
15733}
15734
15735void
15736rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15737{
15738 p->parsing_thread = parsing_thread;
15739}
15740
15741void
15742rb_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)
15743{
15744 p->lex.gets = gets;
15745 p->lex.input = input;
15746 p->eofp = 0;
15747 p->ruby_sourcefile_string = sourcefile_string;
15748 p->ruby_sourcefile = sourcefile;
15749 p->ruby_sourceline = sourceline;
15750}
15751
15752VALUE
15753rb_ruby_parser_result(rb_parser_t *p)
15754{
15755 return p->result;
15756}
15757
15758rb_encoding *
15759rb_ruby_parser_enc(rb_parser_t *p)
15760{
15761 return p->enc;
15762}
15763
15764VALUE
15765rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15766{
15767 return p->ruby_sourcefile_string;
15768}
15769
15770int
15771rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15772{
15773 return p->ruby_sourceline;
15774}
15775
15776int
15777rb_ruby_parser_lex_state(rb_parser_t *p)
15778{
15779 return p->lex.state;
15780}
15781
15782void
15783rb_ruby_ripper_parse0(rb_parser_t *p)
15784{
15785 parser_prepare(p);
15786 p->ast = rb_ast_new();
15787 ripper_yyparse((void*)p);
15788 rb_ast_free(p->ast);
15789 p->ast = 0;
15790 p->eval_tree = 0;
15791 p->eval_tree_begin = 0;
15792}
15793
15794int
15795rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15796{
15797 return dedent_string(p, string, width);
15798}
15799
15800int
15801rb_ruby_ripper_initialized_p(rb_parser_t *p)
15802{
15803 return p->lex.input != 0;
15804}
15805
15806void
15807rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15808{
15809 parser_initialize(p);
15810}
15811
15812long
15813rb_ruby_ripper_column(rb_parser_t *p)
15814{
15815 return p->lex.ptok - p->lex.pbeg;
15816}
15817
15818long
15819rb_ruby_ripper_token_len(rb_parser_t *p)
15820{
15821 return p->lex.pcur - p->lex.ptok;
15822}
15823
15824rb_parser_string_t *
15825rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15826{
15827 return p->lex.lastline;
15828}
15829
15830VALUE
15831rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15832{
15833 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15834}
15835
15836#ifdef UNIVERSAL_PARSER
15837rb_parser_t *
15838rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15839{
15840 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15841 p->config = config;
15842 return p;
15843}
15844#endif
15845
15846struct parser_params*
15847rb_ruby_ripper_parser_allocate(void)
15848{
15849 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15850}
15851#endif /* RIPPER */
15852
15853#ifndef RIPPER
15854void
15855rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15856{
15857 va_list ap;
15858 VALUE mesg = p->debug_buffer;
15859
15860 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15861 va_start(ap, fmt);
15862 rb_str_vcatf(mesg, fmt, ap);
15863 va_end(ap);
15864 if (char_at_end(p, mesg, 0) == '\n') {
15865 rb_io_write(p->debug_output, mesg);
15866 p->debug_buffer = Qnil;
15867 }
15868}
15869
15870static void
15871parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15872{
15873 va_list ap;
15874 int lineno, column;
15875
15876 if (loc) {
15877 lineno = loc->end_pos.lineno;
15878 column = loc->end_pos.column;
15879 }
15880 else {
15881 lineno = p->ruby_sourceline;
15882 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15883 }
15884
15885 rb_io_flush(p->debug_output);
15886 p->error_p = 1;
15887 va_start(ap, fmt);
15888 p->error_buffer =
15889 rb_syntax_error_append(p->error_buffer,
15890 p->ruby_sourcefile_string,
15891 lineno, column,
15892 p->enc, fmt, ap);
15893 va_end(ap);
15894}
15895
15896static size_t
15897count_char(const char *str, int c)
15898{
15899 int n = 0;
15900 while (str[n] == c) ++n;
15901 return n;
15902}
15903
15904/*
15905 * strip enclosing double-quotes, same as the default yytnamerr except
15906 * for that single-quotes matching back-quotes do not stop stripping.
15907 *
15908 * "\"`class' keyword\"" => "`class' keyword"
15909 */
15910size_t
15911rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
15912{
15913 if (*yystr == '"') {
15914 size_t yyn = 0, bquote = 0;
15915 const char *yyp = yystr;
15916
15917 while (*++yyp) {
15918 switch (*yyp) {
15919 case '\'':
15920 if (!bquote) {
15921 bquote = count_char(yyp+1, '\'') + 1;
15922 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
15923 yyn += bquote;
15924 yyp += bquote - 1;
15925 break;
15926 }
15927 else {
15928 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
15929 if (yyres) memcpy(yyres + yyn, yyp, bquote);
15930 yyn += bquote;
15931 yyp += bquote - 1;
15932 bquote = 0;
15933 break;
15934 }
15935 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
15936 if (yyres) memcpy(yyres + yyn, yyp, 3);
15937 yyn += 3;
15938 yyp += 2;
15939 break;
15940 }
15941 goto do_not_strip_quotes;
15942 }
15943
15944 case ',':
15945 goto do_not_strip_quotes;
15946
15947 case '\\':
15948 if (*++yyp != '\\')
15949 goto do_not_strip_quotes;
15950 /* Fall through. */
15951 default:
15952 if (yyres)
15953 yyres[yyn] = *yyp;
15954 yyn++;
15955 break;
15956
15957 case '"':
15958 case '\0':
15959 if (yyres)
15960 yyres[yyn] = '\0';
15961 return yyn;
15962 }
15963 }
15964 do_not_strip_quotes: ;
15965 }
15966
15967 if (!yyres) return strlen(yystr);
15968
15969 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
15970}
15971#endif
15972
15973#ifdef RIPPER
15974#define validate(x) (void)(x)
15975
15976static VALUE
15977ripper_dispatch0(struct parser_params *p, ID mid)
15978{
15979 return rb_funcall(p->value, mid, 0);
15980}
15981
15982static VALUE
15983ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
15984{
15985 validate(a);
15986 return rb_funcall(p->value, mid, 1, a);
15987}
15988
15989static VALUE
15990ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
15991{
15992 validate(a);
15993 validate(b);
15994 return rb_funcall(p->value, mid, 2, a, b);
15995}
15996
15997static VALUE
15998ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
15999{
16000 validate(a);
16001 validate(b);
16002 validate(c);
16003 return rb_funcall(p->value, mid, 3, a, b, c);
16004}
16005
16006static VALUE
16007ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16008{
16009 validate(a);
16010 validate(b);
16011 validate(c);
16012 validate(d);
16013 return rb_funcall(p->value, mid, 4, a, b, c, d);
16014}
16015
16016static VALUE
16017ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16018{
16019 validate(a);
16020 validate(b);
16021 validate(c);
16022 validate(d);
16023 validate(e);
16024 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16025}
16026
16027static VALUE
16028ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16029{
16030 validate(a);
16031 validate(b);
16032 validate(c);
16033 validate(d);
16034 validate(e);
16035 validate(f);
16036 validate(g);
16037 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16038}
16039
16040void
16041ripper_error(struct parser_params *p)
16042{
16043 p->error_p = TRUE;
16044}
16045
16046VALUE
16047ripper_value(struct parser_params *p)
16048{
16049 (void)yystpcpy; /* may not used in newer bison */
16050
16051 return p->value;
16052}
16053
16054#endif /* RIPPER */
16055/*
16056 * Local variables:
16057 * mode: c
16058 * c-file-style: "ruby"
16059 * End:
16060 */