Ruby 3.5.0dev (2025-04-12 revision 3628e9e30d3809da67eb38b9d19fa99f8a999eaa)
parse.y (3628e9e30d3809da67eb38b9d19fa99f8a999eaa)
1/**********************************************************************
2
3 parse.y -
4
5 $Author$
6 created at: Fri May 28 18:02:42 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12%{
13
14#if !YYPURE
15# error needs pure parser
16#endif
17#define YYDEBUG 1
18#define YYERROR_VERBOSE 1
19#define YYSTACK_USE_ALLOCA 0
20
21/* For Ripper */
22#ifdef RUBY_EXTCONF_H
23# include RUBY_EXTCONF_H
24#endif
25
26#include "ruby/internal/config.h"
27
28#include <errno.h>
29
30#ifdef UNIVERSAL_PARSER
31
32#include "internal/ruby_parser.h"
33#include "parser_node.h"
34#include "universal_parser.c"
35
36#ifdef RIPPER
37#define STATIC_ID2SYM p->config->static_id2sym
38#define rb_str_coderange_scan_restartable p->config->str_coderange_scan_restartable
39#endif
40
41#else
42
43#include "internal.h"
44#include "internal/compile.h"
45#include "internal/compilers.h"
46#include "internal/complex.h"
47#include "internal/encoding.h"
48#include "internal/error.h"
49#include "internal/hash.h"
50#include "internal/io.h"
51#include "internal/numeric.h"
52#include "internal/parse.h"
53#include "internal/rational.h"
54#include "internal/re.h"
55#include "internal/ruby_parser.h"
56#include "internal/symbol.h"
57#include "internal/thread.h"
58#include "internal/variable.h"
59#include "node.h"
60#include "parser_node.h"
61#include "probes.h"
62#include "regenc.h"
63#include "ruby/encoding.h"
64#include "ruby/regex.h"
65#include "ruby/ruby.h"
66#include "ruby/st.h"
67#include "ruby/util.h"
68#include "ruby/ractor.h"
69#include "symbol.h"
70
71#ifndef RIPPER
72static VALUE
73syntax_error_new(void)
74{
75 return rb_class_new_instance(0, 0, rb_eSyntaxError);
76}
77#endif
78
79static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable);
80
81#define compile_callback rb_suppress_tracing
82#endif /* !UNIVERSAL_PARSER */
83
84#define NODE_SPECIAL_EMPTY_ARGS ((NODE *)-1)
85#define NODE_EMPTY_ARGS_P(node) ((node) == NODE_SPECIAL_EMPTY_ARGS)
86
87static int rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2);
88
89#ifndef RIPPER
90static rb_parser_string_t *rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *original);
91#endif
92
93static int
94node_integer_cmp(rb_node_integer_t *n1, rb_node_integer_t *n2)
95{
96 return (n1->minus != n2->minus ||
97 n1->base != n2->base ||
98 strcmp(n1->val, n2->val));
99}
100
101static int
102node_float_cmp(rb_node_float_t *n1, rb_node_float_t *n2)
103{
104 return (n1->minus != n2->minus ||
105 strcmp(n1->val, n2->val));
106}
107
108static int
109node_rational_cmp(rb_node_rational_t *n1, rb_node_rational_t *n2)
110{
111 return (n1->minus != n2->minus ||
112 n1->base != n2->base ||
113 n1->seen_point != n2->seen_point ||
114 strcmp(n1->val, n2->val));
115}
116
117static int
118node_imaginary_cmp(rb_node_imaginary_t *n1, rb_node_imaginary_t *n2)
119{
120 return (n1->minus != n2->minus ||
121 n1->base != n2->base ||
122 n1->seen_point != n2->seen_point ||
123 n1->type != n2->type ||
124 strcmp(n1->val, n2->val));
125}
126
127static int
128rb_parser_regx_hash_cmp(rb_node_regx_t *n1, rb_node_regx_t *n2)
129{
130 return (n1->options != n2->options ||
131 rb_parser_string_hash_cmp(n1->string, n2->string));
132}
133
134static st_index_t rb_parser_str_hash(rb_parser_string_t *str);
135static st_index_t rb_char_p_hash(const char *c);
136
137static int
138literal_cmp(st_data_t val, st_data_t lit)
139{
140 if (val == lit) return 0;
141
142 NODE *node_val = RNODE(val);
143 NODE *node_lit = RNODE(lit);
144 enum node_type type_val = nd_type(node_val);
145 enum node_type type_lit = nd_type(node_lit);
146
147 if (type_val != type_lit) {
148 return -1;
149 }
150
151 switch (type_lit) {
152 case NODE_INTEGER:
153 return node_integer_cmp(RNODE_INTEGER(node_val), RNODE_INTEGER(node_lit));
154 case NODE_FLOAT:
155 return node_float_cmp(RNODE_FLOAT(node_val), RNODE_FLOAT(node_lit));
156 case NODE_RATIONAL:
157 return node_rational_cmp(RNODE_RATIONAL(node_val), RNODE_RATIONAL(node_lit));
158 case NODE_IMAGINARY:
159 return node_imaginary_cmp(RNODE_IMAGINARY(node_val), RNODE_IMAGINARY(node_lit));
160 case NODE_STR:
161 return rb_parser_string_hash_cmp(RNODE_STR(node_val)->string, RNODE_STR(node_lit)->string);
162 case NODE_SYM:
163 return rb_parser_string_hash_cmp(RNODE_SYM(node_val)->string, RNODE_SYM(node_lit)->string);
164 case NODE_REGX:
165 return rb_parser_regx_hash_cmp(RNODE_REGX(node_val), RNODE_REGX(node_lit));
166 case NODE_LINE:
167 return node_val->nd_loc.beg_pos.lineno != node_lit->nd_loc.beg_pos.lineno;
168 case NODE_FILE:
169 return rb_parser_string_hash_cmp(RNODE_FILE(node_val)->path, RNODE_FILE(node_lit)->path);
170 case NODE_ENCODING:
171 return RNODE_ENCODING(node_val)->enc != RNODE_ENCODING(node_lit)->enc;
172 default:
173#ifdef UNIVERSAL_PARSER
174 abort();
175#else
176 rb_bug("unexpected node: %s, %s", ruby_node_name(type_val), ruby_node_name(type_lit));
177#endif
178 }
179}
180
181static st_index_t
182literal_hash(st_data_t a)
183{
184 NODE *node = (NODE *)a;
185 enum node_type type = nd_type(node);
186
187 switch (type) {
188 case NODE_INTEGER:
189 return rb_char_p_hash(RNODE_INTEGER(node)->val);
190 case NODE_FLOAT:
191 return rb_char_p_hash(RNODE_FLOAT(node)->val);
192 case NODE_RATIONAL:
193 return rb_char_p_hash(RNODE_RATIONAL(node)->val);
194 case NODE_IMAGINARY:
195 return rb_char_p_hash(RNODE_IMAGINARY(node)->val);
196 case NODE_STR:
197 return rb_parser_str_hash(RNODE_STR(node)->string);
198 case NODE_SYM:
199 return rb_parser_str_hash(RNODE_SYM(node)->string);
200 case NODE_REGX:
201 return rb_parser_str_hash(RNODE_REGX(node)->string);
202 case NODE_LINE:
203 return (st_index_t)node->nd_loc.beg_pos.lineno;
204 case NODE_FILE:
205 return rb_parser_str_hash(RNODE_FILE(node)->path);
206 case NODE_ENCODING:
207 return (st_index_t)RNODE_ENCODING(node)->enc;
208 default:
209#ifdef UNIVERSAL_PARSER
210 abort();
211#else
212 rb_bug("unexpected node: %s", ruby_node_name(type));
213#endif
214 }
215}
216
217static inline int
218parse_isascii(int c)
219{
220 return '\0' <= c && c <= '\x7f';
221}
222
223#undef ISASCII
224#define ISASCII parse_isascii
225
226static inline int
227parse_isspace(int c)
228{
229 return c == ' ' || ('\t' <= c && c <= '\r');
230}
231
232#undef ISSPACE
233#define ISSPACE parse_isspace
234
235static inline int
236parse_iscntrl(int c)
237{
238 return ('\0' <= c && c < ' ') || c == '\x7f';
239}
240
241#undef ISCNTRL
242#define ISCNTRL(c) parse_iscntrl(c)
243
244static inline int
245parse_isupper(int c)
246{
247 return 'A' <= c && c <= 'Z';
248}
249
250static inline int
251parse_islower(int c)
252{
253 return 'a' <= c && c <= 'z';
254}
255
256static inline int
257parse_isalpha(int c)
258{
259 return parse_isupper(c) || parse_islower(c);
260}
261
262#undef ISALPHA
263#define ISALPHA(c) parse_isalpha(c)
264
265static inline int
266parse_isdigit(int c)
267{
268 return '0' <= c && c <= '9';
269}
270
271#undef ISDIGIT
272#define ISDIGIT(c) parse_isdigit(c)
273
274static inline int
275parse_isalnum(int c)
276{
277 return ISALPHA(c) || ISDIGIT(c);
278}
279
280#undef ISALNUM
281#define ISALNUM(c) parse_isalnum(c)
282
283static inline int
284parse_isxdigit(int c)
285{
286 return ISDIGIT(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
287}
288
289#undef ISXDIGIT
290#define ISXDIGIT(c) parse_isxdigit(c)
291
292#include "parser_st.h"
293
294#undef STRCASECMP
295#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
296
297#undef STRNCASECMP
298#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
299
300#ifdef RIPPER
301#include "ripper_init.h"
302#endif
303
304enum rescue_context {
305 before_rescue,
306 after_rescue,
307 after_else,
308 after_ensure,
309};
310
311struct lex_context {
312 unsigned int in_defined: 1;
313 unsigned int in_kwarg: 1;
314 unsigned int in_argdef: 1;
315 unsigned int in_def: 1;
316 unsigned int in_class: 1;
317 BITFIELD(enum rb_parser_shareability, shareable_constant_value, 2);
318 BITFIELD(enum rescue_context, in_rescue, 2);
319 unsigned int cant_return: 1;
320};
321
322typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
323
324#if defined(__GNUC__) && !defined(__clang__)
325// Suppress "parameter passing for argument of type 'struct
326// lex_context' changed" notes. `struct lex_context` is file scope,
327// and has no ABI compatibility issue.
328RBIMPL_WARNING_PUSH()
329RBIMPL_WARNING_IGNORED(-Wpsabi)
330RBIMPL_WARNING_POP()
331// Not sure why effective even after popped.
332#endif
333
334#include "parse.h"
335
336#define NO_LEX_CTXT (struct lex_context){0}
337
338#ifndef WARN_PAST_SCOPE
339# define WARN_PAST_SCOPE 0
340#endif
341
342#define TAB_WIDTH 8
343
344#define yydebug (p->debug) /* disable the global variable definition */
345
346#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__)
347#define YY_LOCATION_PRINT(File, loc, p) \
348 rb_parser_printf(p, "%d.%d-%d.%d", \
349 (loc).beg_pos.lineno, (loc).beg_pos.column,\
350 (loc).end_pos.lineno, (loc).end_pos.column)
351#define YYLLOC_DEFAULT(Current, Rhs, N) \
352 do \
353 if (N) \
354 { \
355 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
356 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
357 } \
358 else \
359 { \
360 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
361 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
362 } \
363 while (0)
364#define YY_(Msgid) \
365 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
366 "nesting too deep" : (Msgid))
367
368#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
369 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
370#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
371 rb_parser_set_location_of_delayed_token(p, &(Current))
372#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
373 rb_parser_set_location_of_heredoc_end(p, &(Current))
374#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
375 rb_parser_set_location_of_dummy_end(p, &(Current))
376#define RUBY_SET_YYLLOC_OF_NONE(Current) \
377 rb_parser_set_location_of_none(p, &(Current))
378#define RUBY_SET_YYLLOC(Current) \
379 rb_parser_set_location(p, &(Current))
380#define RUBY_INIT_YYLLOC() \
381 { \
382 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
383 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
384 }
385
386#define IS_lex_state_for(x, ls) ((x) & (ls))
387#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
388#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
389#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
390
391# define SET_LEX_STATE(ls) \
392 parser_set_lex_state(p, ls, __LINE__)
393static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
394
395typedef VALUE stack_type;
396
397static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
398
399# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
400# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
401# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
402# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
403# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
404
405/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
406 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
407#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
408#define COND_POP() BITSTACK_POP(cond_stack)
409#define COND_P() BITSTACK_SET_P(cond_stack)
410#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
411
412/* A flag to identify keyword_do_block; "do" keyword after command_call.
413 Example: `foo 1, 2 do`. */
414#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
415#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
416#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
417#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
418
419struct vtable {
420 ID *tbl;
421 int pos;
422 int capa;
423 struct vtable *prev;
424};
425
426struct local_vars {
427 struct vtable *args;
428 struct vtable *vars;
429 struct vtable *used;
430# if WARN_PAST_SCOPE
431 struct vtable *past;
432# endif
433 struct local_vars *prev;
434 struct {
435 NODE *outer, *inner, *current;
436 } numparam;
437 NODE *it;
438};
439
440typedef struct rb_locations_lambda_body_t {
441 NODE *node;
442 YYLTYPE opening_loc;
443 YYLTYPE closing_loc;
444} rb_locations_lambda_body_t;
445
446enum {
447 ORDINAL_PARAM = -1,
448 NO_PARAM = 0,
449 NUMPARAM_MAX = 9,
450};
451
452#define DVARS_INHERIT ((void*)1)
453#define DVARS_TOPSCOPE NULL
454#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
455
456typedef struct token_info {
457 const char *token;
458 rb_code_position_t beg;
459 int indent;
460 int nonspc;
461 struct token_info *next;
462} token_info;
463
464typedef struct end_expect_token_locations {
465 const rb_code_position_t *pos;
466 struct end_expect_token_locations *prev;
467} end_expect_token_locations_t;
468
469typedef struct parser_string_buffer_elem {
470 struct parser_string_buffer_elem *next;
471 long len; /* Total length of allocated buf */
472 long used; /* Current usage of buf */
473 rb_parser_string_t *buf[FLEX_ARY_LEN];
474} parser_string_buffer_elem_t;
475
476typedef struct parser_string_buffer {
477 parser_string_buffer_elem_t *head;
478 parser_string_buffer_elem_t *last;
479} parser_string_buffer_t;
480
481#define AFTER_HEREDOC_WITHOUT_TERMINATOR ((rb_parser_string_t *)1)
482
483/*
484 Structure of Lexer Buffer:
485
486 lex.pbeg lex.ptok lex.pcur lex.pend
487 | | | |
488 |------------+------------+------------|
489 |<---------->|
490 token
491*/
492struct parser_params {
493 YYSTYPE *lval;
494 YYLTYPE *yylloc;
495
496 struct {
497 rb_strterm_t *strterm;
498 rb_parser_lex_gets_func *gets;
499 rb_parser_input_data input;
500 parser_string_buffer_t string_buffer;
501 rb_parser_string_t *lastline;
502 rb_parser_string_t *nextline;
503 const char *pbeg;
504 const char *pcur;
505 const char *pend;
506 const char *ptok;
507 enum lex_state_e state;
508 /* track the nest level of any parens "()[]{}" */
509 int paren_nest;
510 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
511 int lpar_beg;
512 /* track the nest level of only braces "{}" */
513 int brace_nest;
514 } lex;
515 stack_type cond_stack;
516 stack_type cmdarg_stack;
517 int tokidx;
518 int toksiz;
519 int heredoc_end;
520 int heredoc_indent;
521 int heredoc_line_indent;
522 char *tokenbuf;
523 struct local_vars *lvtbl;
524 st_table *pvtbl;
525 st_table *pktbl;
526 int line_count;
527 int ruby_sourceline; /* current line no. */
528 const char *ruby_sourcefile; /* current source file */
529 VALUE ruby_sourcefile_string;
530 rb_encoding *enc;
531 token_info *token_info;
532 st_table *case_labels;
533 rb_node_exits_t *exits;
534
535 VALUE debug_buffer;
536 VALUE debug_output;
537
538 struct {
539 rb_parser_string_t *token;
540 int beg_line;
541 int beg_col;
542 int end_line;
543 int end_col;
544 } delayed;
545
546 rb_ast_t *ast;
547 int node_id;
548
549 st_table *warn_duplicate_keys_table;
550
551 int max_numparam;
552 ID it_id;
553
554 struct lex_context ctxt;
555
556 NODE *eval_tree_begin;
557 NODE *eval_tree;
558 const struct rb_iseq_struct *parent_iseq;
559
560#ifdef UNIVERSAL_PARSER
561 const rb_parser_config_t *config;
562#endif
563 /* compile_option */
564 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
565
566 unsigned int command_start:1;
567 unsigned int eofp: 1;
568 unsigned int ruby__end__seen: 1;
569 unsigned int debug: 1;
570 unsigned int has_shebang: 1;
571 unsigned int token_seen: 1;
572 unsigned int token_info_enabled: 1;
573# if WARN_PAST_SCOPE
574 unsigned int past_scope_enabled: 1;
575# endif
576 unsigned int error_p: 1;
577 unsigned int cr_seen: 1;
578
579#ifndef RIPPER
580 /* Ruby core only */
581
582 unsigned int do_print: 1;
583 unsigned int do_loop: 1;
584 unsigned int do_chomp: 1;
585 unsigned int do_split: 1;
586 unsigned int error_tolerant: 1;
587 unsigned int keep_tokens: 1;
588
589 VALUE error_buffer;
590 rb_parser_ary_t *debug_lines;
591 /*
592 * Store specific keyword locations to generate dummy end token.
593 * Refer to the tail of list element.
594 */
595 end_expect_token_locations_t *end_expect_token_locations;
596 /* id for terms */
597 int token_id;
598 /* Array for term tokens */
599 rb_parser_ary_t *tokens;
600#else
601 /* Ripper only */
602
603 VALUE value;
604 VALUE result;
605 VALUE parsing_thread;
606 VALUE s_value; /* Token VALUE */
607 VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
608 VALUE s_value_stack;
609#endif
610};
611
612#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
613#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
614#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
615static int
616numparam_id_p(struct parser_params *p, ID id)
617{
618 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
619 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
620 return idx > 0 && idx <= NUMPARAM_MAX;
621}
622static void numparam_name(struct parser_params *p, ID id);
623
624#ifdef RIPPER
625static void
626after_shift(struct parser_params *p)
627{
628 if (p->debug) {
629 rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
630 }
631 rb_ary_push(p->s_value_stack, p->s_value);
632 p->s_value = Qnil;
633}
634
635static void
636before_reduce(int len, struct parser_params *p)
637{
638 // Initialize $$ with $1.
639 if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
640}
641
642static void
643after_reduce(int len, struct parser_params *p)
644{
645 for (int i = 0; i < len; i++) {
646 VALUE tos = rb_ary_pop(p->s_value_stack);
647 if (p->debug) {
648 rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos);
649 }
650 }
651 if (p->debug) {
652 rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
653 }
654 rb_ary_push(p->s_value_stack, p->s_lvalue);
655 p->s_lvalue = Qnil;
656}
657
658static void
659after_shift_error_token(struct parser_params *p)
660{
661 if (p->debug) {
662 rb_parser_printf(p, "after-shift-error-token:\n");
663 }
664 rb_ary_push(p->s_value_stack, Qnil);
665}
666
667static void
668after_pop_stack(int len, struct parser_params *p)
669{
670 for (int i = 0; i < len; i++) {
671 VALUE tos = rb_ary_pop(p->s_value_stack);
672 if (p->debug) {
673 rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
674 }
675 }
676}
677#else
678static void
679after_shift(struct parser_params *p)
680{
681}
682
683static void
684before_reduce(int len, struct parser_params *p)
685{
686}
687
688static void
689after_reduce(int len, struct parser_params *p)
690{
691}
692
693static void
694after_shift_error_token(struct parser_params *p)
695{
696}
697
698static void
699after_pop_stack(int len, struct parser_params *p)
700{
701}
702#endif
703
704#define intern_cstr(n,l,en) rb_intern3(n,l,en)
705
706#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
707
708#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
709#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
710#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
711#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
712#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
713#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
714
715#ifndef RIPPER
716static inline int
717char_at_end(struct parser_params *p, VALUE str, int when_empty)
718{
719 long len = RSTRING_LEN(str);
720 return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
721}
722#endif
723
724static void
725pop_pvtbl(struct parser_params *p, st_table *tbl)
726{
727 st_free_table(p->pvtbl);
728 p->pvtbl = tbl;
729}
730
731static void
732pop_pktbl(struct parser_params *p, st_table *tbl)
733{
734 if (p->pktbl) st_free_table(p->pktbl);
735 p->pktbl = tbl;
736}
737
738#define STRING_BUF_DEFAULT_LEN 16
739
740static void
741string_buffer_init(struct parser_params *p)
742{
743 parser_string_buffer_t *buf = &p->lex.string_buffer;
744 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN;
745
746 buf->head = buf->last = xmalloc(size);
747 buf->head->len = STRING_BUF_DEFAULT_LEN;
748 buf->head->used = 0;
749 buf->head->next = NULL;
750}
751
752static void
753string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
754{
755 parser_string_buffer_t *buf = &p->lex.string_buffer;
756
757 if (buf->head->used >= buf->head->len) {
758 parser_string_buffer_elem_t *elem;
759 long n = buf->head->len * 2;
760 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n;
761
762 elem = xmalloc(size);
763 elem->len = n;
764 elem->used = 0;
765 elem->next = NULL;
766 buf->last->next = elem;
767 buf->last = elem;
768 }
769 buf->last->buf[buf->last->used++] = str;
770}
771
772static void
773string_buffer_free(struct parser_params *p)
774{
775 parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
776
777 while (elem) {
778 parser_string_buffer_elem_t *next_elem = elem->next;
779
780 for (long i = 0; i < elem->used; i++) {
781 rb_parser_string_free(p, elem->buf[i]);
782 }
783
784 xfree(elem);
785 elem = next_elem;
786 }
787}
788
789#ifndef RIPPER
790static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
791
792static void
793debug_end_expect_token_locations(struct parser_params *p, const char *name)
794{
795 if(p->debug) {
796 VALUE mesg = rb_sprintf("%s: [", name);
797 int i = 0;
798 for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
799 if (i > 0)
800 rb_str_cat_cstr(mesg, ", ");
801 rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
802 i++;
803 }
804 rb_str_cat_cstr(mesg, "]\n");
805 flush_debug_buffer(p, p->debug_output, mesg);
806 }
807}
808
809static void
810push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
811{
812 if(!p->error_tolerant) return;
813
814 end_expect_token_locations_t *locations;
815 locations = ALLOC(end_expect_token_locations_t);
816 locations->pos = pos;
817 locations->prev = p->end_expect_token_locations;
818 p->end_expect_token_locations = locations;
819
820 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
821}
822
823static void
824pop_end_expect_token_locations(struct parser_params *p)
825{
826 if(!p->end_expect_token_locations) return;
827
828 end_expect_token_locations_t *locations = p->end_expect_token_locations->prev;
829 ruby_sized_xfree(p->end_expect_token_locations, sizeof(end_expect_token_locations_t));
830 p->end_expect_token_locations = locations;
831
832 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
833}
834
835static end_expect_token_locations_t *
836peek_end_expect_token_locations(struct parser_params *p)
837{
838 return p->end_expect_token_locations;
839}
840
841static const char *
842parser_token2char(struct parser_params *p, enum yytokentype tok)
843{
844 switch ((int) tok) {
845#define TOKEN2CHAR(tok) case tok: return (#tok);
846#define TOKEN2CHAR2(tok, name) case tok: return (name);
847 TOKEN2CHAR2(' ', "word_sep");
848 TOKEN2CHAR2('!', "!")
849 TOKEN2CHAR2('%', "%");
850 TOKEN2CHAR2('&', "&");
851 TOKEN2CHAR2('*', "*");
852 TOKEN2CHAR2('+', "+");
853 TOKEN2CHAR2('-', "-");
854 TOKEN2CHAR2('/', "/");
855 TOKEN2CHAR2('<', "<");
856 TOKEN2CHAR2('=', "=");
857 TOKEN2CHAR2('>', ">");
858 TOKEN2CHAR2('?', "?");
859 TOKEN2CHAR2('^', "^");
860 TOKEN2CHAR2('|', "|");
861 TOKEN2CHAR2('~', "~");
862 TOKEN2CHAR2(':', ":");
863 TOKEN2CHAR2(',', ",");
864 TOKEN2CHAR2('.', ".");
865 TOKEN2CHAR2(';', ";");
866 TOKEN2CHAR2('`', "`");
867 TOKEN2CHAR2('\n', "nl");
868 TOKEN2CHAR2('{', "\"{\"");
869 TOKEN2CHAR2('}', "\"}\"");
870 TOKEN2CHAR2('[', "\"[\"");
871 TOKEN2CHAR2(']', "\"]\"");
872 TOKEN2CHAR2('(', "\"(\"");
873 TOKEN2CHAR2(')', "\")\"");
874 TOKEN2CHAR2('\\', "backslash");
875 TOKEN2CHAR(keyword_class);
876 TOKEN2CHAR(keyword_module);
877 TOKEN2CHAR(keyword_def);
878 TOKEN2CHAR(keyword_undef);
879 TOKEN2CHAR(keyword_begin);
880 TOKEN2CHAR(keyword_rescue);
881 TOKEN2CHAR(keyword_ensure);
882 TOKEN2CHAR(keyword_end);
883 TOKEN2CHAR(keyword_if);
884 TOKEN2CHAR(keyword_unless);
885 TOKEN2CHAR(keyword_then);
886 TOKEN2CHAR(keyword_elsif);
887 TOKEN2CHAR(keyword_else);
888 TOKEN2CHAR(keyword_case);
889 TOKEN2CHAR(keyword_when);
890 TOKEN2CHAR(keyword_while);
891 TOKEN2CHAR(keyword_until);
892 TOKEN2CHAR(keyword_for);
893 TOKEN2CHAR(keyword_break);
894 TOKEN2CHAR(keyword_next);
895 TOKEN2CHAR(keyword_redo);
896 TOKEN2CHAR(keyword_retry);
897 TOKEN2CHAR(keyword_in);
898 TOKEN2CHAR(keyword_do);
899 TOKEN2CHAR(keyword_do_cond);
900 TOKEN2CHAR(keyword_do_block);
901 TOKEN2CHAR(keyword_do_LAMBDA);
902 TOKEN2CHAR(keyword_return);
903 TOKEN2CHAR(keyword_yield);
904 TOKEN2CHAR(keyword_super);
905 TOKEN2CHAR(keyword_self);
906 TOKEN2CHAR(keyword_nil);
907 TOKEN2CHAR(keyword_true);
908 TOKEN2CHAR(keyword_false);
909 TOKEN2CHAR(keyword_and);
910 TOKEN2CHAR(keyword_or);
911 TOKEN2CHAR(keyword_not);
912 TOKEN2CHAR(modifier_if);
913 TOKEN2CHAR(modifier_unless);
914 TOKEN2CHAR(modifier_while);
915 TOKEN2CHAR(modifier_until);
916 TOKEN2CHAR(modifier_rescue);
917 TOKEN2CHAR(keyword_alias);
918 TOKEN2CHAR(keyword_defined);
919 TOKEN2CHAR(keyword_BEGIN);
920 TOKEN2CHAR(keyword_END);
921 TOKEN2CHAR(keyword__LINE__);
922 TOKEN2CHAR(keyword__FILE__);
923 TOKEN2CHAR(keyword__ENCODING__);
924 TOKEN2CHAR(tIDENTIFIER);
925 TOKEN2CHAR(tFID);
926 TOKEN2CHAR(tGVAR);
927 TOKEN2CHAR(tIVAR);
928 TOKEN2CHAR(tCONSTANT);
929 TOKEN2CHAR(tCVAR);
930 TOKEN2CHAR(tLABEL);
931 TOKEN2CHAR(tINTEGER);
932 TOKEN2CHAR(tFLOAT);
933 TOKEN2CHAR(tRATIONAL);
934 TOKEN2CHAR(tIMAGINARY);
935 TOKEN2CHAR(tCHAR);
936 TOKEN2CHAR(tNTH_REF);
937 TOKEN2CHAR(tBACK_REF);
938 TOKEN2CHAR(tSTRING_CONTENT);
939 TOKEN2CHAR(tREGEXP_END);
940 TOKEN2CHAR(tDUMNY_END);
941 TOKEN2CHAR(tSP);
942 TOKEN2CHAR(tUPLUS);
943 TOKEN2CHAR(tUMINUS);
944 TOKEN2CHAR(tPOW);
945 TOKEN2CHAR(tCMP);
946 TOKEN2CHAR(tEQ);
947 TOKEN2CHAR(tEQQ);
948 TOKEN2CHAR(tNEQ);
949 TOKEN2CHAR(tGEQ);
950 TOKEN2CHAR(tLEQ);
951 TOKEN2CHAR(tANDOP);
952 TOKEN2CHAR(tOROP);
953 TOKEN2CHAR(tMATCH);
954 TOKEN2CHAR(tNMATCH);
955 TOKEN2CHAR(tDOT2);
956 TOKEN2CHAR(tDOT3);
957 TOKEN2CHAR(tBDOT2);
958 TOKEN2CHAR(tBDOT3);
959 TOKEN2CHAR(tAREF);
960 TOKEN2CHAR(tASET);
961 TOKEN2CHAR(tLSHFT);
962 TOKEN2CHAR(tRSHFT);
963 TOKEN2CHAR(tANDDOT);
964 TOKEN2CHAR(tCOLON2);
965 TOKEN2CHAR(tCOLON3);
966 TOKEN2CHAR(tOP_ASGN);
967 TOKEN2CHAR(tASSOC);
968 TOKEN2CHAR(tLPAREN);
969 TOKEN2CHAR(tLPAREN_ARG);
970 TOKEN2CHAR(tLBRACK);
971 TOKEN2CHAR(tLBRACE);
972 TOKEN2CHAR(tLBRACE_ARG);
973 TOKEN2CHAR(tSTAR);
974 TOKEN2CHAR(tDSTAR);
975 TOKEN2CHAR(tAMPER);
976 TOKEN2CHAR(tLAMBDA);
977 TOKEN2CHAR(tSYMBEG);
978 TOKEN2CHAR(tSTRING_BEG);
979 TOKEN2CHAR(tXSTRING_BEG);
980 TOKEN2CHAR(tREGEXP_BEG);
981 TOKEN2CHAR(tWORDS_BEG);
982 TOKEN2CHAR(tQWORDS_BEG);
983 TOKEN2CHAR(tSYMBOLS_BEG);
984 TOKEN2CHAR(tQSYMBOLS_BEG);
985 TOKEN2CHAR(tSTRING_END);
986 TOKEN2CHAR(tSTRING_DEND);
987 TOKEN2CHAR(tSTRING_DBEG);
988 TOKEN2CHAR(tSTRING_DVAR);
989 TOKEN2CHAR(tLAMBEG);
990 TOKEN2CHAR(tLABEL_END);
991 TOKEN2CHAR(tIGNORED_NL);
992 TOKEN2CHAR(tCOMMENT);
993 TOKEN2CHAR(tEMBDOC_BEG);
994 TOKEN2CHAR(tEMBDOC);
995 TOKEN2CHAR(tEMBDOC_END);
996 TOKEN2CHAR(tHEREDOC_BEG);
997 TOKEN2CHAR(tHEREDOC_END);
998 TOKEN2CHAR(k__END__);
999 TOKEN2CHAR(tLOWEST);
1000 TOKEN2CHAR(tUMINUS_NUM);
1001 TOKEN2CHAR(tLAST_TOKEN);
1002#undef TOKEN2CHAR
1003#undef TOKEN2CHAR2
1004 }
1005
1006 rb_bug("parser_token2id: unknown token %d", tok);
1007
1008 UNREACHABLE_RETURN(0);
1009}
1010#else
1011static void
1012push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
1013{
1014}
1015
1016static void
1017pop_end_expect_token_locations(struct parser_params *p)
1018{
1019}
1020#endif
1021
1022RBIMPL_ATTR_NONNULL((1, 2, 3))
1023static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
1024RBIMPL_ATTR_NONNULL((1, 2))
1025static int parser_yyerror0(struct parser_params*, const char*);
1026#define yyerror0(msg) parser_yyerror0(p, (msg))
1027#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
1028#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
1029#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
1030#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
1031#define lex_eol_p(p) lex_eol_n_p(p, 0)
1032#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
1033#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
1034#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
1035
1036static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
1037static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
1038static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
1039static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
1040static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
1041
1042#ifdef RIPPER
1043#define compile_for_eval (0)
1044#else
1045#define compile_for_eval (p->parent_iseq != 0)
1046#endif
1047
1048#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
1049
1050#define CALL_Q_P(q) ((q) == tANDDOT)
1051#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
1052
1053#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
1054
1055static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
1056
1057static inline void
1058rb_discard_node(struct parser_params *p, NODE *n)
1059{
1060 rb_ast_delete_node(p->ast, n);
1061}
1062
1063static rb_node_scope_t *rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1064static rb_node_scope_t *rb_node_scope_new2(struct parser_params *p, rb_ast_id_table_t *nd_tbl, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1065static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1066static rb_node_if_t *rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE* if_keyword_loc, const YYLTYPE* then_keyword_loc, const YYLTYPE* end_keyword_loc);
1067static rb_node_unless_t *rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *end_keyword_loc);
1068static rb_node_case_t *rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc);
1069static rb_node_case2_t *rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc);
1070static rb_node_case3_t *rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc);
1071static rb_node_when_t *rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc);
1072static rb_node_in_t *rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc);
1073static rb_node_while_t *rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc);
1074static rb_node_until_t *rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc);
1075static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1076static rb_node_for_t *rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *for_keyword_loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *do_keyword_loc, const YYLTYPE *end_keyword_loc);
1077static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
1078static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
1079static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1080static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
1081static rb_node_resbody_t *rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc);
1082static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
1083static rb_node_and_t *rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1084static rb_node_or_t *rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1085static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
1086static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1087static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1088static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1089static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1090static rb_node_cdecl_t *rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc);
1091static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1092static rb_node_op_asgn1_t *rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc);
1093static rb_node_op_asgn2_t *rb_node_op_asgn2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, ID nd_vid, ID nd_mid, bool nd_aid, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc);
1094static rb_node_op_asgn_or_t *rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc);
1095static rb_node_op_asgn_and_t *rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc);
1096static rb_node_op_cdecl_t *rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, enum rb_parser_shareability shareability, const YYLTYPE *loc);
1097static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1098static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1099static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1100static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1101static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1102static rb_node_super_t *rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc);
1103static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
1104static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1105static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1106static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
1107static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1108static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1109static rb_node_yield_t *rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc);
1110static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1111static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1112static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1113static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1114static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1115static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1116static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1117static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1118static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1119static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1120static rb_node_integer_t * rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc);
1121static rb_node_float_t * rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc);
1122static rb_node_rational_t * rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc);
1123static rb_node_imaginary_t * rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type, const YYLTYPE *loc);
1124static rb_node_str_t *rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1125static rb_node_dstr_t *rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1126static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1127static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1128static rb_node_dxstr_t *rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1129static rb_node_evstr_t *rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1130static rb_node_regx_t *rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc);
1131static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1132static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
1133static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc);
1134static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1135static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1136static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
1137static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1138static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1139static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1140static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1141static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1142static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1143static rb_node_alias_t *rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1144static rb_node_valias_t *rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1145static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
1146static rb_node_class_t *rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *inheritance_operator_loc, const YYLTYPE *end_keyword_loc);
1147static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc);
1148static rb_node_sclass_t *rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc);
1149static rb_node_colon2_t *rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc);
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, const YYLTYPE *keyword_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1160static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1161static rb_node_dsym_t *rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1162static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1163static rb_node_lambda_t *rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1164static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1165static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
1166static rb_node_fndptn_t *rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1167static rb_node_line_t *rb_node_line_new(struct parser_params *p, const YYLTYPE *loc);
1168static rb_node_file_t *rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1169static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
1170
1171#define NEW_SCOPE(a,b,loc) (NODE *)rb_node_scope_new(p,a,b,loc)
1172#define NEW_SCOPE2(t,a,b,loc) (NODE *)rb_node_scope_new2(p,t,a,b,loc)
1173#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
1174#define NEW_IF(c,t,e,loc,ik_loc,tk_loc,ek_loc) (NODE *)rb_node_if_new(p,c,t,e,loc,ik_loc,tk_loc,ek_loc)
1175#define NEW_UNLESS(c,t,e,loc,k_loc,t_loc,e_loc) (NODE *)rb_node_unless_new(p,c,t,e,loc,k_loc,t_loc,e_loc)
1176#define NEW_CASE(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case_new(p,h,b,loc,ck_loc,ek_loc)
1177#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc)
1178#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc)
1179#define NEW_WHEN(c,t,e,loc,k_loc,t_loc) (NODE *)rb_node_when_new(p,c,t,e,loc,k_loc,t_loc)
1180#define NEW_IN(c,t,e,loc) (NODE *)rb_node_in_new(p,c,t,e,loc)
1181#define NEW_WHILE(c,b,n,loc,k_loc,c_loc) (NODE *)rb_node_while_new(p,c,b,n,loc,k_loc,c_loc)
1182#define NEW_UNTIL(c,b,n,loc,k_loc,c_loc) (NODE *)rb_node_until_new(p,c,b,n,loc,k_loc,c_loc)
1183#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
1184#define NEW_FOR(i,b,loc,f_loc,i_loc,d_loc,e_loc) (NODE *)rb_node_for_new(p,i,b,loc,f_loc,i_loc,d_loc,e_loc)
1185#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
1186#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
1187#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
1188#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
1189#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc)
1190#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
1191#define NEW_AND(f,s,loc,op_loc) (NODE *)rb_node_and_new(p,f,s,loc,op_loc)
1192#define NEW_OR(f,s,loc,op_loc) (NODE *)rb_node_or_new(p,f,s,loc,op_loc)
1193#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
1194#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
1195#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
1196#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
1197#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
1198#define NEW_CDECL(v,val,path,share,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,share,loc)
1199#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
1200#define NEW_OP_ASGN1(r,id,idx,rval,loc,c_op_loc,o_loc,c_loc,b_op_loc) (NODE *)rb_node_op_asgn1_new(p,r,id,idx,rval,loc,c_op_loc,o_loc,c_loc,b_op_loc)
1201#define NEW_OP_ASGN2(r,t,i,o,val,loc,c_op_loc,m_loc,b_op_loc) (NODE *)rb_node_op_asgn2_new(p,r,val,i,o,t,loc,c_op_loc,m_loc,b_op_loc)
1202#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
1203#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
1204#define NEW_OP_CDECL(v,op,val,share,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,share,loc)
1205#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
1206#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
1207#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
1208#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
1209#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
1210#define NEW_SUPER(a,loc,k_loc,l_loc,r_loc) (NODE *)rb_node_super_new(p,a,loc,k_loc,l_loc,r_loc)
1211#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
1212#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
1213#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
1214#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1215#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1216#define NEW_RETURN(s,loc,k_loc) (NODE *)rb_node_return_new(p,s,loc,k_loc)
1217#define NEW_YIELD(a,loc,k_loc,l_loc,r_loc) (NODE *)rb_node_yield_new(p,a,loc,k_loc,l_loc,r_loc)
1218#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1219#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1220#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1221#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1222#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1223#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1224#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1225#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1226#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1227#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1228#define NEW_INTEGER(val, base,loc) (NODE *)rb_node_integer_new(p,val,base,loc)
1229#define NEW_FLOAT(val,loc) (NODE *)rb_node_float_new(p,val,loc)
1230#define NEW_RATIONAL(val,base,seen_point,loc) (NODE *)rb_node_rational_new(p,val,base,seen_point,loc)
1231#define NEW_IMAGINARY(val,base,seen_point,numeric_type,loc) (NODE *)rb_node_imaginary_new(p,val,base,seen_point,numeric_type,loc)
1232#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1233#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1234#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1235#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1236#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1237#define NEW_EVSTR(n,loc,o_loc,c_loc) (NODE *)rb_node_evstr_new(p,n,loc,o_loc,c_loc)
1238#define NEW_REGX(str,opts,loc,o_loc,ct_loc,c_loc) (NODE *)rb_node_regx_new(p,str,opts,loc,o_loc,ct_loc,c_loc)
1239#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1240#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1241#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1242#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1243#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1244#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1245#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1246#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1247#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc)
1248#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_loc)
1249#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1250#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1251#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
1252#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
1253#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1254#define NEW_CLASS(n,b,s,loc,ck_loc,io_loc,ek_loc) (NODE *)rb_node_class_new(p,n,b,s,loc,ck_loc,io_loc,ek_loc)
1255#define NEW_MODULE(n,b,loc) (NODE *)rb_node_module_new(p,n,b,loc)
1256#define NEW_SCLASS(r,b,loc) (NODE *)rb_node_sclass_new(p,r,b,loc)
1257#define NEW_COLON2(c,i,loc) (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,k_loc,o_loc,c_loc) (NODE *)rb_node_postexe_new(p,b,loc,k_loc,o_loc,c_loc)
1268#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc)
1269#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1270#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1271#define NEW_LAMBDA(a,b,loc,op_loc,o_loc,c_loc) (NODE *)rb_node_lambda_new(p,a,b,loc,op_loc,o_loc,c_loc)
1272#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1273#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1274#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1275#define NEW_LINE(loc) (NODE *)rb_node_line_new(p,loc)
1276#define NEW_FILE(str,loc) (NODE *)rb_node_file_new(p,str,loc)
1277#define NEW_ENCODING(loc) (NODE *)rb_node_encoding_new(p,loc)
1278#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1279
1280enum internal_node_type {
1281 NODE_INTERNAL_ONLY = NODE_LAST,
1282 NODE_DEF_TEMP,
1283 NODE_EXITS,
1284 NODE_INTERNAL_LAST
1285};
1286
1287static const char *
1288parser_node_name(int node)
1289{
1290 switch (node) {
1291 case NODE_DEF_TEMP:
1292 return "NODE_DEF_TEMP";
1293 case NODE_EXITS:
1294 return "NODE_EXITS";
1295 default:
1296 return ruby_node_name(node);
1297 }
1298}
1299
1300/* This node is parse.y internal */
1301struct RNode_DEF_TEMP {
1302 NODE node;
1303
1304 /* for NODE_DEFN/NODE_DEFS */
1305
1306 struct RNode *nd_def;
1307 ID nd_mid;
1308
1309 struct {
1310 int max_numparam;
1311 NODE *numparam_save;
1312 struct lex_context ctxt;
1313 } save;
1314};
1315
1316#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1317
1318static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1319static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1320static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1321static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1322static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1323
1324#define NEW_BREAK(s,loc,k_loc) (NODE *)rb_node_break_new(p,s,loc,k_loc)
1325#define NEW_NEXT(s,loc,k_loc) (NODE *)rb_node_next_new(p,s,loc,k_loc)
1326#define NEW_REDO(loc,k_loc) (NODE *)rb_node_redo_new(p,loc,k_loc)
1327#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1328
1329/* Make a new internal node, which should not be appeared in the
1330 * result AST and does not have node_id and location. */
1331static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1332#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1333
1334static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1335
1336static int
1337parser_get_node_id(struct parser_params *p)
1338{
1339 int node_id = p->node_id;
1340 p->node_id++;
1341 return node_id;
1342}
1343
1344static void
1345anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1346{
1347 if (id == tANDDOT) {
1348 yyerror1(loc, "&. inside multiple assignment destination");
1349 }
1350}
1351
1352static inline void
1353set_line_body(NODE *body, int line)
1354{
1355 if (!body) return;
1356 switch (nd_type(body)) {
1357 case NODE_RESCUE:
1358 case NODE_ENSURE:
1359 nd_set_line(body, line);
1360 }
1361}
1362
1363static void
1364set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1365{
1366 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1367 nd_set_line(node, beg->end_pos.lineno);
1368}
1369
1370static NODE *
1371last_expr_node(NODE *expr)
1372{
1373 while (expr) {
1374 if (nd_type_p(expr, NODE_BLOCK)) {
1375 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1376 }
1377 else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) {
1378 expr = RNODE_BEGIN(expr)->nd_body;
1379 }
1380 else {
1381 break;
1382 }
1383 }
1384 return expr;
1385}
1386
1387#ifndef RIPPER
1388#define yyparse ruby_yyparse
1389#endif
1390
1391static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1392static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1393#define new_nil(loc) NEW_NIL(loc)
1394static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1395static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1396static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1397static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1398
1399static NODE *newline_node(NODE*);
1400static void fixpos(NODE*,NODE*);
1401
1402static int value_expr_gen(struct parser_params*,NODE*);
1403static void void_expr(struct parser_params*,NODE*);
1404static NODE *remove_begin(NODE*);
1405#define value_expr(node) value_expr_gen(p, (node))
1406static NODE *void_stmts(struct parser_params*,NODE*);
1407static void reduce_nodes(struct parser_params*,NODE**);
1408static void block_dup_check(struct parser_params*,NODE*,NODE*);
1409
1410static NODE *block_append(struct parser_params*,NODE*,NODE*);
1411static NODE *list_append(struct parser_params*,NODE*,NODE*);
1412static NODE *list_concat(NODE*,NODE*);
1413static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1414static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1415static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1416static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1417static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1418static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1419static NODE *str2dstr(struct parser_params*,NODE*);
1420static NODE *evstr2dstr(struct parser_params*,NODE*);
1421static NODE *splat_array(NODE*);
1422static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1423
1424static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1425static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1426static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1427static NODE *new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc);
1428static NODE *method_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc) {RNODE_ITER(b)->nd_iter = m; b->nd_loc = *loc; return b;}
1429
1430static bool args_info_empty_p(struct rb_args_info *args);
1431static rb_node_args_t *new_args(struct parser_params*,rb_node_args_aux_t*,rb_node_opt_arg_t*,ID,rb_node_args_aux_t*,rb_node_args_t*,const YYLTYPE*);
1432static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1433static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1434static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1435static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1436static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1437static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1438static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1439
1440static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1441static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1442
1443static NODE* negate_lit(struct parser_params*, NODE*);
1444static void no_blockarg(struct parser_params*,NODE*);
1445static NODE *ret_args(struct parser_params*,NODE*);
1446static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1447static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1448
1449static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1450static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1451
1452static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1453static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1454
1455static VALUE rb_backref_error(struct parser_params*,NODE*);
1456static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1457
1458static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1459static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc);
1460static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc);
1461static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1462static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1463
1464static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1465
1466static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1467static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1468
1469static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1470static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1471
1472static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
1473
1474static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *);
1475
1476#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1477
1478static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1479
1480static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1481
1482static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1483
1484static rb_ast_id_table_t *local_tbl(struct parser_params*);
1485
1486static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1487static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1488
1489static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1490static NODE *heredoc_dedent(struct parser_params*,NODE*);
1491
1492static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1493
1494static rb_locations_lambda_body_t* new_locations_lambda_body(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1495
1496#ifdef RIPPER
1497#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1498#define set_value(val) (p->s_lvalue = val)
1499static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1500static int id_is_var(struct parser_params *p, ID id);
1501#endif
1502
1503RUBY_SYMBOL_EXPORT_BEGIN
1504VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1505int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1506enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1507VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1508void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1509PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1510YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1511YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1512YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1513YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1514YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1515YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1516void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1517RUBY_SYMBOL_EXPORT_END
1518
1519static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1520static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1521static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1522static VALUE formal_argument_error(struct parser_params*, ID);
1523static ID shadowing_lvar(struct parser_params*,ID);
1524static void new_bv(struct parser_params*,ID);
1525
1526static void local_push(struct parser_params*,int);
1527static void local_pop(struct parser_params*);
1528static void local_var(struct parser_params*, ID);
1529static void arg_var(struct parser_params*, ID);
1530static int local_id(struct parser_params *p, ID id);
1531static int local_id_ref(struct parser_params*, ID, ID **);
1532#define internal_id rb_parser_internal_id
1533ID internal_id(struct parser_params*);
1534static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1535static int check_forwarding_args(struct parser_params*);
1536static void add_forwarding_args(struct parser_params *p);
1537static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1538
1539static const struct vtable *dyna_push(struct parser_params *);
1540static void dyna_pop(struct parser_params*, const struct vtable *);
1541static int dyna_in_block(struct parser_params*);
1542#define dyna_var(p, id) local_var(p, id)
1543static int dvar_defined(struct parser_params*, ID);
1544#define dvar_defined_ref rb_parser_dvar_defined_ref
1545int dvar_defined_ref(struct parser_params*, ID, ID**);
1546static int dvar_curr(struct parser_params*,ID);
1547
1548static int lvar_defined(struct parser_params*, ID);
1549
1550static NODE *numparam_push(struct parser_params *p);
1551static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1552
1553#define METHOD_NOT '!'
1554
1555#define idFWD_REST '*'
1556#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1557#define idFWD_BLOCK '&'
1558#define idFWD_ALL idDot3
1559#define arg_FWD_BLOCK idFWD_BLOCK
1560
1561#define RE_ONIG_OPTION_IGNORECASE 1
1562#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1563#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1564#define RE_OPTION_ONCE (1<<16)
1565#define RE_OPTION_ENCODING_SHIFT 8
1566#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1567#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1568#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1569#define RE_OPTION_MASK 0xff
1570#define RE_OPTION_ARG_ENCODING_NONE 32
1571
1572#define CHECK_LITERAL_WHEN (st_table *)1
1573#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1574
1575#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1576RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1577
1578#define TOKEN2ID(tok) ( \
1579 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1580 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1581 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1582 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1583 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1584 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1585 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1586
1587/****** Ripper *******/
1588
1589#ifdef RIPPER
1590
1591#include "eventids1.h"
1592#include "eventids2.h"
1593
1594extern const struct ripper_parser_ids ripper_parser_ids;
1595
1596static VALUE ripper_dispatch0(struct parser_params*,ID);
1597static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1598static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1599static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1600static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1601static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1602static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1603void ripper_error(struct parser_params *p);
1604
1605#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1606#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1607#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1608#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1609#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1610#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1611#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1612
1613#define yyparse ripper_yyparse
1614
1615static VALUE
1616aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1617{
1618 if (!NIL_P(pre_arg)) {
1619 if (!NIL_P(pre_args)) {
1620 rb_ary_unshift(pre_args, pre_arg);
1621 }
1622 else {
1623 pre_args = rb_ary_new_from_args(1, pre_arg);
1624 }
1625 }
1626 return pre_args;
1627}
1628
1629#define ID2VAL(id) STATIC_ID2SYM(id)
1630#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1631#endif /* RIPPER */
1632
1633#define KWD2EID(t, v) keyword_##t
1634
1635static NODE *
1636new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, const YYLTYPE *loc)
1637{
1638 body = remove_begin(body);
1639 reduce_nodes(p, &body);
1640 NODE *n = NEW_SCOPE(args, body, loc);
1641 nd_set_line(n, loc->end_pos.lineno);
1642 set_line_body(body, loc->beg_pos.lineno);
1643 return n;
1644}
1645
1646static NODE *
1647rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1648 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1649{
1650 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1651 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1652 loc.beg_pos = arg_loc->beg_pos;
1653 return NEW_RESCUE(arg, rescue, 0, &loc);
1654}
1655
1656static NODE *add_block_exit(struct parser_params *p, NODE *node);
1657static rb_node_exits_t *init_block_exit(struct parser_params *p);
1658static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1659static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1660static void clear_block_exit(struct parser_params *p, bool error);
1661
1662static void
1663next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1664{
1665 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1666}
1667
1668static void
1669restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1670{
1671 /* See: def_name action */
1672 struct lex_context ctxt = temp->save.ctxt;
1673 p->ctxt.in_def = ctxt.in_def;
1674 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1675 p->ctxt.in_rescue = ctxt.in_rescue;
1676 p->max_numparam = temp->save.max_numparam;
1677 numparam_pop(p, temp->save.numparam_save);
1678 clear_block_exit(p, true);
1679}
1680
1681static void
1682endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1683{
1684 if (is_attrset_id(mid)) {
1685 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1686 }
1687 token_info_drop(p, "def", loc->beg_pos);
1688}
1689
1690#define debug_token_line(p, name, line) do { \
1691 if (p->debug) { \
1692 const char *const pcur = p->lex.pcur; \
1693 const char *const ptok = p->lex.ptok; \
1694 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1695 line, p->ruby_sourceline, \
1696 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1697 } \
1698 } while (0)
1699
1700#define begin_definition(k, loc_beg, loc_end) \
1701 do { \
1702 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1703 /* singleton class */ \
1704 p->ctxt.cant_return = !p->ctxt.in_def; \
1705 p->ctxt.in_def = 0; \
1706 } \
1707 else if (p->ctxt.in_def) { \
1708 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1709 yyerror1(&loc, k " definition in method body"); \
1710 } \
1711 else { \
1712 p->ctxt.cant_return = 1; \
1713 } \
1714 local_push(p, 0); \
1715 } while (0)
1716
1717#ifndef RIPPER
1718# define ifndef_ripper(x) (x)
1719# define ifdef_ripper(r,x) (x)
1720#else
1721# define ifndef_ripper(x)
1722# define ifdef_ripper(r,x) (r)
1723#endif
1724
1725# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1726# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1727# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1728# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1729# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1730# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1731# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1732# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1733# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1734# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1735# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1736# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1737# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1738# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1739# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1740# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1741# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1742# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1743# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1744# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1745#ifdef RIPPER
1746extern const ID id_warn, id_warning, id_gets, id_assoc;
1747# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1748# define WARN_S_L(s,l) STR_NEW(s,l)
1749# define WARN_S(s) STR_NEW2(s)
1750# define WARN_I(i) INT2NUM(i)
1751# define WARN_ID(i) rb_id2str(i)
1752# define PRIsWARN PRIsVALUE
1753# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1754# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1755# ifdef HAVE_VA_ARGS_MACRO
1756# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1757# else
1758# define WARN_CALL rb_funcall
1759# endif
1760# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1761# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1762# ifdef HAVE_VA_ARGS_MACRO
1763# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1764# else
1765# define WARNING_CALL rb_funcall
1766# endif
1767# define compile_error ripper_compile_error
1768#else
1769# define WARN_S_L(s,l) s
1770# define WARN_S(s) s
1771# define WARN_I(i) i
1772# define WARN_ID(i) rb_id2name(i)
1773# define PRIsWARN PRIsVALUE
1774# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1775# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1776# define WARN_CALL rb_compile_warn
1777# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1778# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1779# define WARNING_CALL rb_compile_warning
1780PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1781# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1782#endif
1783
1784#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1785
1786static NODE *
1787add_block_exit(struct parser_params *p, NODE *node)
1788{
1789 if (!node) {
1790 compile_error(p, "unexpected null node");
1791 return 0;
1792 }
1793 switch (nd_type(node)) {
1794 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1795 default:
1796 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1797 return node;
1798 }
1799 if (!p->ctxt.in_defined) {
1800 rb_node_exits_t *exits = p->exits;
1801 if (exits) {
1802 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1803 exits->nd_stts = node;
1804 }
1805 }
1806 return node;
1807}
1808
1809static rb_node_exits_t *
1810init_block_exit(struct parser_params *p)
1811{
1812 rb_node_exits_t *old = p->exits;
1813 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1814 exits->nd_chain = 0;
1815 exits->nd_stts = RNODE(exits);
1816 p->exits = exits;
1817 return old;
1818}
1819
1820static rb_node_exits_t *
1821allow_block_exit(struct parser_params *p)
1822{
1823 rb_node_exits_t *exits = p->exits;
1824 p->exits = 0;
1825 return exits;
1826}
1827
1828static void
1829restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1830{
1831 p->exits = exits;
1832}
1833
1834static void
1835clear_block_exit(struct parser_params *p, bool error)
1836{
1837 rb_node_exits_t *exits = p->exits;
1838 if (!exits) return;
1839 if (error) {
1840 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1841 switch (nd_type(e)) {
1842 case NODE_BREAK:
1843 yyerror1(&e->nd_loc, "Invalid break");
1844 break;
1845 case NODE_NEXT:
1846 yyerror1(&e->nd_loc, "Invalid next");
1847 break;
1848 case NODE_REDO:
1849 yyerror1(&e->nd_loc, "Invalid redo");
1850 break;
1851 default:
1852 yyerror1(&e->nd_loc, "unexpected node");
1853 goto end_checks; /* no nd_chain */
1854 }
1855 }
1856 end_checks:;
1857 }
1858 exits->nd_stts = RNODE(exits);
1859 exits->nd_chain = 0;
1860}
1861
1862#define WARN_EOL(tok) \
1863 (looking_at_eol_p(p) ? \
1864 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1865 (void)0)
1866static int looking_at_eol_p(struct parser_params *p);
1867
1868static NODE *
1869get_nd_value(struct parser_params *p, NODE *node)
1870{
1871 switch (nd_type(node)) {
1872 case NODE_GASGN:
1873 return RNODE_GASGN(node)->nd_value;
1874 case NODE_IASGN:
1875 return RNODE_IASGN(node)->nd_value;
1876 case NODE_LASGN:
1877 return RNODE_LASGN(node)->nd_value;
1878 case NODE_DASGN:
1879 return RNODE_DASGN(node)->nd_value;
1880 case NODE_MASGN:
1881 return RNODE_MASGN(node)->nd_value;
1882 case NODE_CVASGN:
1883 return RNODE_CVASGN(node)->nd_value;
1884 case NODE_CDECL:
1885 return RNODE_CDECL(node)->nd_value;
1886 default:
1887 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1888 return 0;
1889 }
1890}
1891
1892static void
1893set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1894{
1895 switch (nd_type(node)) {
1896 case NODE_CDECL:
1897 RNODE_CDECL(node)->nd_value = rhs;
1898 break;
1899 case NODE_GASGN:
1900 RNODE_GASGN(node)->nd_value = rhs;
1901 break;
1902 case NODE_IASGN:
1903 RNODE_IASGN(node)->nd_value = rhs;
1904 break;
1905 case NODE_LASGN:
1906 RNODE_LASGN(node)->nd_value = rhs;
1907 break;
1908 case NODE_DASGN:
1909 RNODE_DASGN(node)->nd_value = rhs;
1910 break;
1911 case NODE_MASGN:
1912 RNODE_MASGN(node)->nd_value = rhs;
1913 break;
1914 case NODE_CVASGN:
1915 RNODE_CVASGN(node)->nd_value = rhs;
1916 break;
1917 default:
1918 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1919 break;
1920 }
1921}
1922
1923static ID
1924get_nd_vid(struct parser_params *p, NODE *node)
1925{
1926 switch (nd_type(node)) {
1927 case NODE_CDECL:
1928 return RNODE_CDECL(node)->nd_vid;
1929 case NODE_GASGN:
1930 return RNODE_GASGN(node)->nd_vid;
1931 case NODE_IASGN:
1932 return RNODE_IASGN(node)->nd_vid;
1933 case NODE_LASGN:
1934 return RNODE_LASGN(node)->nd_vid;
1935 case NODE_DASGN:
1936 return RNODE_DASGN(node)->nd_vid;
1937 case NODE_CVASGN:
1938 return RNODE_CVASGN(node)->nd_vid;
1939 default:
1940 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1941 return 0;
1942 }
1943}
1944
1945static NODE *
1946get_nd_args(struct parser_params *p, NODE *node)
1947{
1948 switch (nd_type(node)) {
1949 case NODE_CALL:
1950 return RNODE_CALL(node)->nd_args;
1951 case NODE_OPCALL:
1952 return RNODE_OPCALL(node)->nd_args;
1953 case NODE_FCALL:
1954 return RNODE_FCALL(node)->nd_args;
1955 case NODE_QCALL:
1956 return RNODE_QCALL(node)->nd_args;
1957 case NODE_SUPER:
1958 return RNODE_SUPER(node)->nd_args;
1959 case NODE_VCALL:
1960 case NODE_ZSUPER:
1961 case NODE_YIELD:
1962 case NODE_RETURN:
1963 case NODE_BREAK:
1964 case NODE_NEXT:
1965 return 0;
1966 default:
1967 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1968 return 0;
1969 }
1970}
1971
1972static st_index_t
1973djb2(const uint8_t *str, size_t len)
1974{
1975 st_index_t hash = 5381;
1976
1977 for (size_t i = 0; i < len; i++) {
1978 hash = ((hash << 5) + hash) + str[i];
1979 }
1980
1981 return hash;
1982}
1983
1984static st_index_t
1985parser_memhash(const void *ptr, long len)
1986{
1987 return djb2(ptr, len);
1988}
1989
1990#define PARSER_STRING_PTR(str) (str->ptr)
1991#define PARSER_STRING_LEN(str) (str->len)
1992#define PARSER_STRING_END(str) (&str->ptr[str->len])
1993#define STRING_SIZE(str) ((size_t)str->len + 1)
1994#define STRING_TERM_LEN(str) (1)
1995#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
1996#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
1997 SIZED_REALLOC_N(str->ptr, char, (size_t)total + termlen, STRING_SIZE(str)); \
1998 str->len = total; \
1999} while (0)
2000#define STRING_SET_LEN(str, n) do { \
2001 (str)->len = (n); \
2002} while (0)
2003#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2004 ((ptrvar) = str->ptr, \
2005 (lenvar) = str->len)
2006
2007static inline int
2008parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2009{
2010 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2011}
2012
2013static rb_parser_string_t *
2014rb_parser_string_new(rb_parser_t *p, const char *ptr, long len)
2015{
2016 rb_parser_string_t *str;
2017
2018 if (len < 0) {
2019 rb_bug("negative string size (or size too big): %ld", len);
2020 }
2021
2022 str = xcalloc(1, sizeof(rb_parser_string_t));
2023 str->ptr = xcalloc(len + 1, sizeof(char));
2024
2025 if (ptr) {
2026 memcpy(PARSER_STRING_PTR(str), ptr, len);
2027 }
2028 STRING_SET_LEN(str, len);
2029 STRING_TERM_FILL(str);
2030 return str;
2031}
2032
2033static rb_parser_string_t *
2034rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, long len, rb_encoding *enc)
2035{
2036 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2037 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2038 str->enc = enc;
2039 return str;
2040}
2041
2042#ifndef RIPPER
2043rb_parser_string_t *
2044rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2045{
2046 /* Type check */
2047 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2048 RB_GC_GUARD(str);
2049 return ret;
2050}
2051
2052void
2053rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2054{
2055 if (!str) return;
2056 xfree(PARSER_STRING_PTR(str));
2057 xfree(str);
2058}
2059#endif
2060
2061static st_index_t
2062rb_parser_str_hash(rb_parser_string_t *str)
2063{
2064 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2065}
2066
2067static st_index_t
2068rb_char_p_hash(const char *c)
2069{
2070 return parser_memhash((const void *)c, strlen(c));
2071}
2072
2073static size_t
2074rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2075{
2076 return PARSER_STRING_LEN(str);
2077}
2078
2079#ifndef RIPPER
2080static char *
2081rb_parser_string_end(rb_parser_string_t *str)
2082{
2083 return &str->ptr[str->len];
2084}
2085#endif
2086
2087static void
2088rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2089{
2090 str->enc = enc;
2091}
2092
2093static rb_encoding *
2094rb_parser_str_get_encoding(rb_parser_string_t *str)
2095{
2096 return str->enc;
2097}
2098
2099#ifndef RIPPER
2100static bool
2101PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2102{
2103 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2104}
2105#endif
2106
2107static int
2108PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2109{
2110 return str->coderange;
2111}
2112
2113static void
2114PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2115{
2116 str->coderange = coderange;
2117}
2118
2119static void
2120PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2121{
2122 rb_parser_string_set_encoding(str, enc);
2123 PARSER_ENC_CODERANGE_SET(str, cr);
2124}
2125
2126static void
2127PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2128{
2129 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2130}
2131
2132static bool
2133PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2134{
2135 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2136}
2137
2138static bool
2139PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2140{
2141 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2142}
2143
2144static const char *
2145rb_parser_search_nonascii(const char *p, const char *e)
2146{
2147 const char *s = p;
2148
2149 for (; s < e; s++) {
2150 if (*s & 0x80) return s;
2151 }
2152
2153 return NULL;
2154}
2155
2156static int
2157rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc)
2158{
2159 const char *e = ptr + len;
2160
2161 if (enc == rb_ascii8bit_encoding()) {
2162 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2163 ptr = rb_parser_search_nonascii(ptr, e);
2164 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2165 }
2166
2167 /* parser string encoding is always asciicompat */
2168 ptr = rb_parser_search_nonascii(ptr, e);
2169 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2170 for (;;) {
2171 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2172 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2173 ptr += MBCLEN_CHARFOUND_LEN(ret);
2174 if (ptr == e) break;
2175 ptr = rb_parser_search_nonascii(ptr, e);
2176 if (!ptr) break;
2177 }
2178
2179 return RB_PARSER_ENC_CODERANGE_VALID;
2180}
2181
2182static int
2183rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2184{
2185 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2186}
2187
2188static int
2189rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2190{
2191 int cr = PARSER_ENC_CODERANGE(str);
2192
2193 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2194 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2195 PARSER_ENC_CODERANGE_SET(str, cr);
2196 }
2197
2198 return cr;
2199}
2200
2201static rb_parser_string_t *
2202rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2203{
2204 if (rb_parser_str_get_encoding(str) == enc)
2205 return str;
2206 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2207 PARSER_ENC_CODERANGE_CLEAR(str);
2208 }
2209 rb_parser_string_set_encoding(str, enc);
2210 return str;
2211}
2212
2213static bool
2214rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2215{
2216 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2217}
2218
2219static rb_encoding *
2220rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2221{
2222 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2223 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2224
2225 if (enc1 == NULL || enc2 == NULL)
2226 return 0;
2227
2228 if (enc1 == enc2) {
2229 return enc1;
2230 }
2231
2232 if (PARSER_STRING_LEN(str2) == 0)
2233 return enc1;
2234 if (PARSER_STRING_LEN(str1) == 0)
2235 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2236
2237 int cr1, cr2;
2238
2239 cr1 = rb_parser_enc_str_coderange(p, str1);
2240 cr2 = rb_parser_enc_str_coderange(p, str2);
2241
2242 if (cr1 != cr2) {
2243 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2244 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2245 }
2246
2247 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2248 return enc1;
2249 }
2250
2251 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2252 return enc2;
2253 }
2254
2255 return 0;
2256}
2257
2258static void
2259rb_parser_str_modify(rb_parser_string_t *str)
2260{
2261 PARSER_ENC_CODERANGE_CLEAR(str);
2262}
2263
2264static void
2265rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len)
2266{
2267 long capa;
2268 const int termlen = STRING_TERM_LEN(str);
2269
2270 if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2271 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
2272 }
2273
2274 int cr = PARSER_ENC_CODERANGE(str);
2275 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2276 /* Leave unknown. */
2277 }
2278 else if (len > PARSER_STRING_LEN(str)) {
2279 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2280 }
2281 else if (len < PARSER_STRING_LEN(str)) {
2282 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2283 /* ASCII-only string is keeping after truncated. Valid
2284 * and broken may be invalid or valid, leave unknown. */
2285 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2286 }
2287 }
2288
2289 STRING_SET_LEN(str, len);
2290 STRING_TERM_FILL(str);
2291}
2292
2293static rb_parser_string_t *
2294rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len)
2295{
2296 rb_parser_str_modify(str);
2297 if (len == 0) return 0;
2298
2299 long total, olen, off = -1;
2300 char *sptr;
2301 const int termlen = STRING_TERM_LEN(str);
2302
2303 PARSER_STRING_GETMEM(str, sptr, olen);
2304 if (ptr >= sptr && ptr <= sptr + olen) {
2305 off = ptr - sptr;
2306 }
2307
2308 if (olen > LONG_MAX - len) {
2309 compile_error(p, "string sizes too big");
2310 return 0;
2311 }
2312 total = olen + len;
2313 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2314 sptr = PARSER_STRING_PTR(str);
2315 if (off != -1) {
2316 ptr = sptr + off;
2317 }
2318 memcpy(sptr + olen, ptr, len);
2319 STRING_SET_LEN(str, total);
2320 STRING_TERM_FILL(str);
2321
2322 return str;
2323}
2324
2325#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2326#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2327
2328static rb_parser_string_t *
2329rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2330 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2331{
2332 int str_cr, res_cr;
2333 rb_encoding *str_enc, *res_enc;
2334
2335 str_enc = rb_parser_str_get_encoding(str);
2336 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2337
2338 if (str_enc == ptr_enc) {
2339 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2340 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2341 }
2342 }
2343 else {
2344 /* parser string encoding is always asciicompat */
2345 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2346 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2347 }
2348 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2349 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2350 str_cr = rb_parser_enc_str_coderange(p, str);
2351 }
2352 }
2353 }
2354 if (ptr_cr_ret)
2355 *ptr_cr_ret = ptr_cr;
2356
2357 if (str_enc != ptr_enc &&
2358 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2359 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2360 goto incompatible;
2361 }
2362
2363 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2364 res_enc = str_enc;
2365 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2366 }
2367 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2368 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2369 res_enc = str_enc;
2370 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2371 }
2372 else {
2373 res_enc = ptr_enc;
2374 res_cr = ptr_cr;
2375 }
2376 }
2377 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2378 res_enc = str_enc;
2379 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2380 res_cr = str_cr;
2381 else
2382 res_cr = ptr_cr;
2383 }
2384 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2385 res_enc = str_enc;
2386 res_cr = str_cr;
2387 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2388 }
2389
2390 if (len < 0) {
2391 compile_error(p, "negative string size (or size too big)");
2392 }
2393 parser_str_cat(str, ptr, len);
2394 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2395 return str;
2396
2397 incompatible:
2398 compile_error(p, "incompatible character encodings: %s and %s",
2399 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2400 UNREACHABLE_RETURN(0);
2401
2402}
2403
2404static rb_parser_string_t *
2405rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2406 rb_encoding *ptr_enc)
2407{
2408 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2409}
2410
2411static rb_parser_string_t *
2412rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2413{
2414 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2415
2416 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2417 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2418
2419 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2420
2421 return str;
2422}
2423
2424static rb_parser_string_t *
2425rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len)
2426{
2427 if (len < 0) {
2428 rb_bug("negative string size (or size too big)");
2429 }
2430
2431 long slen = PARSER_STRING_LEN(str);
2432
2433 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2434 PARSER_ENC_CODERANGE_CLEAR(str);
2435 }
2436
2437 {
2438 long capa;
2439 const int termlen = STRING_TERM_LEN(str);
2440
2441 if ((capa = slen) < len) {
2442 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2443 }
2444 else if (len == slen) return str;
2445 STRING_SET_LEN(str, len);
2446 STRING_TERM_FILL(str);
2447 }
2448 return str;
2449}
2450
2451# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2452 ((ptrvar) = str->ptr, \
2453 (lenvar) = str->len, \
2454 (encvar) = str->enc)
2455
2456static int
2457rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2458{
2459 long len1, len2;
2460 const char *ptr1, *ptr2;
2461 rb_encoding *enc1, *enc2;
2462
2463 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2464 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2465
2466 return (len1 != len2 ||
2467 enc1 != enc2 ||
2468 memcmp(ptr1, ptr2, len1) != 0);
2469}
2470
2471static void
2472rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2473{
2474 long i;
2475 if (ary->capa < len) {
2476 ary->capa = len;
2477 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2478 for (i = ary->len; i < len; i++) {
2479 ary->data[i] = 0;
2480 }
2481 }
2482}
2483
2484/*
2485 * Do not call this directly.
2486 * Use rb_parser_ary_new_capa_for_XXX() instead.
2487 */
2488static rb_parser_ary_t *
2489parser_ary_new_capa(rb_parser_t *p, long len)
2490{
2491 if (len < 0) {
2492 rb_bug("negative array size (or size too big): %ld", len);
2493 }
2494 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2495 ary->data_type = 0;
2496 ary->len = 0;
2497 ary->capa = len;
2498 if (0 < len) {
2499 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2500 }
2501 else {
2502 ary->data = NULL;
2503 }
2504 return ary;
2505}
2506
2507#ifndef RIPPER
2508static rb_parser_ary_t *
2509rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2510{
2511 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2512 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2513 return ary;
2514}
2515
2516static rb_parser_ary_t *
2517rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2518{
2519 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2520 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2521 return ary;
2522}
2523#endif
2524
2525static rb_parser_ary_t *
2526rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2527{
2528 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2529 ary->data_type = PARSER_ARY_DATA_NODE;
2530 return ary;
2531}
2532
2533/*
2534 * Do not call this directly.
2535 * Use rb_parser_ary_push_XXX() instead.
2536 */
2537static rb_parser_ary_t *
2538parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2539{
2540 if (ary->len == ary->capa) {
2541 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2542 }
2543 ary->data[ary->len++] = val;
2544 return ary;
2545}
2546
2547#ifndef RIPPER
2548static rb_parser_ary_t *
2549rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2550{
2551 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2552 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2553 }
2554 return parser_ary_push(p, ary, val);
2555}
2556
2557static rb_parser_ary_t *
2558rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2559{
2560 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2561 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2562 }
2563 return parser_ary_push(p, ary, val);
2564}
2565#endif
2566
2567static rb_parser_ary_t *
2568rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2569{
2570 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2571 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2572 }
2573 return parser_ary_push(p, ary, val);
2574}
2575
2576#ifndef RIPPER
2577static void
2578rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2579{
2580 if (!token) return;
2581 rb_parser_string_free(p, token->str);
2582 xfree(token);
2583}
2584
2585static void
2586rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2587{
2588# define foreach_ary(ptr) \
2589 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2590 ptr < end_ary_data; ptr++)
2591 switch (ary->data_type) {
2592 case PARSER_ARY_DATA_AST_TOKEN:
2593 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2594 break;
2595 case PARSER_ARY_DATA_SCRIPT_LINE:
2596 foreach_ary(data) {rb_parser_string_free(p, *data);}
2597 break;
2598 case PARSER_ARY_DATA_NODE:
2599 /* Do nothing because nodes are freed when rb_ast_t is freed */
2600 break;
2601 default:
2602 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2603 break;
2604 }
2605# undef foreach_ary
2606 xfree(ary->data);
2607 xfree(ary);
2608}
2609
2610#endif /* !RIPPER */
2611%}
2612
2613%expect 0
2614%define api.pure
2615%define parse.error verbose
2616%printer {
2617 if ((NODE *)$$ == (NODE *)-1) {
2618 rb_parser_printf(p, "NODE_SPECIAL");
2619 }
2620 else if ($$) {
2621 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2622 }
2623} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2624 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2625%printer {
2626 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2627} <id>
2628%printer {
2629 switch (nd_type(RNODE($$))) {
2630 case NODE_INTEGER:
2631 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2632 break;
2633 case NODE_FLOAT:
2634 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2635 break;
2636 case NODE_RATIONAL:
2637 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2638 break;
2639 case NODE_IMAGINARY:
2640 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2641 break;
2642 default:
2643 break;
2644 }
2645} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2646%printer {
2647 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2648} tNTH_REF
2649%printer {
2650 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2651} tBACK_REF
2652
2653%destructor {
2654 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2655} <labels>
2656
2657%lex-param {struct parser_params *p}
2658%parse-param {struct parser_params *p}
2659%initial-action
2660{
2661 RUBY_SET_YYLLOC_OF_NONE(@$);
2662};
2663%after-shift after_shift
2664%before-reduce before_reduce
2665%after-reduce after_reduce
2666%after-shift-error-token after_shift_error_token
2667%after-pop-stack after_pop_stack
2668
2669%union {
2670 NODE *node;
2671 rb_node_fcall_t *node_fcall;
2672 rb_node_args_t *node_args;
2673 rb_node_args_aux_t *node_args_aux;
2674 rb_node_opt_arg_t *node_opt_arg;
2675 rb_node_kw_arg_t *node_kw_arg;
2676 rb_node_block_pass_t *node_block_pass;
2677 rb_node_masgn_t *node_masgn;
2678 rb_node_def_temp_t *node_def_temp;
2679 rb_node_exits_t *node_exits;
2680 struct rb_locations_lambda_body_t *locations_lambda_body;
2681 ID id;
2682 int num;
2683 st_table *tbl;
2684 st_table *labels;
2685 const struct vtable *vars;
2686 struct rb_strterm_struct *strterm;
2687 struct lex_context ctxt;
2688 enum lex_state_e state;
2689}
2690
2691%token <id>
2692 keyword_class "'class'"
2693 keyword_module "'module'"
2694 keyword_def "'def'"
2695 keyword_undef "'undef'"
2696 keyword_begin "'begin'"
2697 keyword_rescue "'rescue'"
2698 keyword_ensure "'ensure'"
2699 keyword_end "'end'"
2700 keyword_if "'if'"
2701 keyword_unless "'unless'"
2702 keyword_then "'then'"
2703 keyword_elsif "'elsif'"
2704 keyword_else "'else'"
2705 keyword_case "'case'"
2706 keyword_when "'when'"
2707 keyword_while "'while'"
2708 keyword_until "'until'"
2709 keyword_for "'for'"
2710 keyword_break "'break'"
2711 keyword_next "'next'"
2712 keyword_redo "'redo'"
2713 keyword_retry "'retry'"
2714 keyword_in "'in'"
2715 keyword_do "'do'"
2716 keyword_do_cond "'do' for condition"
2717 keyword_do_block "'do' for block"
2718 keyword_do_LAMBDA "'do' for lambda"
2719 keyword_return "'return'"
2720 keyword_yield "'yield'"
2721 keyword_super "'super'"
2722 keyword_self "'self'"
2723 keyword_nil "'nil'"
2724 keyword_true "'true'"
2725 keyword_false "'false'"
2726 keyword_and "'and'"
2727 keyword_or "'or'"
2728 keyword_not "'not'"
2729 modifier_if "'if' modifier"
2730 modifier_unless "'unless' modifier"
2731 modifier_while "'while' modifier"
2732 modifier_until "'until' modifier"
2733 modifier_rescue "'rescue' modifier"
2734 keyword_alias "'alias'"
2735 keyword_defined "'defined?'"
2736 keyword_BEGIN "'BEGIN'"
2737 keyword_END "'END'"
2738 keyword__LINE__ "'__LINE__'"
2739 keyword__FILE__ "'__FILE__'"
2740 keyword__ENCODING__ "'__ENCODING__'"
2741
2742%token <id> tIDENTIFIER "local variable or method"
2743%token <id> tFID "method"
2744%token <id> tGVAR "global variable"
2745%token <id> tIVAR "instance variable"
2746%token <id> tCONSTANT "constant"
2747%token <id> tCVAR "class variable"
2748%token <id> tLABEL "label"
2749%token <node> tINTEGER "integer literal"
2750%token <node> tFLOAT "float literal"
2751%token <node> tRATIONAL "rational literal"
2752%token <node> tIMAGINARY "imaginary literal"
2753%token <node> tCHAR "char literal"
2754%token <node> tNTH_REF "numbered reference"
2755%token <node> tBACK_REF "back reference"
2756%token <node> tSTRING_CONTENT "literal content"
2757%token <num> tREGEXP_END
2758%token <num> tDUMNY_END "dummy end"
2759
2760%type <node> singleton singleton_expr strings string string1 xstring regexp
2761%type <node> string_contents xstring_contents regexp_contents string_content
2762%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2763%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2764%type <node_def_temp> defn_head defs_head k_def
2765%type <node_exits> block_open k_while k_until k_for allow_exits
2766%type <node> top_stmts top_stmt begin_block endless_arg endless_command
2767%type <node> bodystmt stmts stmt_or_begin stmt expr arg primary
2768%type <node> command command_call command_call_value method_call
2769%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2770%type <node_fcall> fcall
2771%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2772%type <node> args arg_splat call_args opt_call_args
2773%type <node> paren_args opt_paren_args
2774%type <node_args> args_tail block_args_tail
2775%type <node> command_args aref_args
2776%type <node_block_pass> opt_block_arg block_arg
2777%type <node> var_ref var_lhs
2778%type <node> command_rhs arg_rhs
2779%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2780%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args
2781%type <node_args_aux> f_arg f_arg_item
2782%type <node> f_marg f_rest_marg
2783%type <node_masgn> f_margs
2784%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2785%type <node_args> block_param opt_block_param_def block_param_def opt_block_param
2786%type <id> do bv_decls opt_bv_decl bvar
2787%type <node> lambda brace_body do_body
2788%type <locations_lambda_body> lambda_body
2789%type <node_args> f_larglist
2790%type <node> brace_block cmd_brace_block do_block lhs none fitem
2791%type <node> mlhs_head mlhs_item mlhs_node
2792%type <node_masgn> mlhs mlhs_basic mlhs_inner
2793%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2794%type <node> p_expr p_as p_alt p_expr_basic p_find
2795%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2796%type <node> p_value p_primitive p_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 operation2 operation3
2799%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
2800%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2801%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2802%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2803%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2804%type <ctxt> p_in_kwarg
2805%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2806%type <num> max_numparam
2807%type <node> numparam
2808%type <id> it_id
2809%token END_OF_INPUT 0 "end-of-input"
2810%token <id> '.'
2811
2812/* escaped chars, should be ignored otherwise */
2813%token <id> '\\' "backslash"
2814%token tSP "escaped space"
2815%token <id> '\t' "escaped horizontal tab"
2816%token <id> '\f' "escaped form feed"
2817%token <id> '\r' "escaped carriage return"
2818%token <id> '\13' "escaped vertical tab"
2819%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2820%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2821%token tPOW RUBY_TOKEN(POW) "**"
2822%token tCMP RUBY_TOKEN(CMP) "<=>"
2823%token tEQ RUBY_TOKEN(EQ) "=="
2824%token tEQQ RUBY_TOKEN(EQQ) "==="
2825%token tNEQ RUBY_TOKEN(NEQ) "!="
2826%token tGEQ RUBY_TOKEN(GEQ) ">="
2827%token tLEQ RUBY_TOKEN(LEQ) "<="
2828%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2829%token tOROP RUBY_TOKEN(OROP) "||"
2830%token tMATCH RUBY_TOKEN(MATCH) "=~"
2831%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2832%token tDOT2 RUBY_TOKEN(DOT2) ".."
2833%token tDOT3 RUBY_TOKEN(DOT3) "..."
2834%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2835%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2836%token tAREF RUBY_TOKEN(AREF) "[]"
2837%token tASET RUBY_TOKEN(ASET) "[]="
2838%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2839%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2840%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2841%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2842%token tCOLON3 ":: at EXPR_BEG"
2843%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2844%token tASSOC "=>"
2845%token tLPAREN "("
2846%token tLPAREN_ARG "( arg"
2847%token tLBRACK "["
2848%token tLBRACE "{"
2849%token tLBRACE_ARG "{ arg"
2850%token tSTAR "*"
2851%token tDSTAR "**arg"
2852%token tAMPER "&"
2853%token <num> tLAMBDA "->"
2854%token tSYMBEG "symbol literal"
2855%token tSTRING_BEG "string literal"
2856%token tXSTRING_BEG "backtick literal"
2857%token tREGEXP_BEG "regexp literal"
2858%token tWORDS_BEG "word list"
2859%token tQWORDS_BEG "verbatim word list"
2860%token tSYMBOLS_BEG "symbol list"
2861%token tQSYMBOLS_BEG "verbatim symbol list"
2862%token tSTRING_END "terminator"
2863%token tSTRING_DEND "'}'"
2864%token <state> tSTRING_DBEG "'#{'"
2865%token tSTRING_DVAR tLAMBEG tLABEL_END
2866
2867%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2868%token tHEREDOC_BEG tHEREDOC_END k__END__
2869
2870/*
2871 * precedence table
2872 */
2873
2874%nonassoc tLOWEST
2875%nonassoc tLBRACE_ARG
2876
2877%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2878%left keyword_or keyword_and
2879%right keyword_not
2880%nonassoc keyword_defined
2881%right '=' tOP_ASGN
2882%left modifier_rescue
2883%right '?' ':'
2884%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2885%left tOROP
2886%left tANDOP
2887%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2888%left '>' tGEQ '<' tLEQ
2889%left '|' '^'
2890%left '&'
2891%left tLSHFT tRSHFT
2892%left '+' '-'
2893%left '*' '/' '%'
2894%right tUMINUS_NUM tUMINUS
2895%right tPOW
2896%right '!' '~' tUPLUS
2897
2898%token tLAST_TOKEN
2899
2900/*
2901 * inlining rules
2902 */
2903%rule %inline ident_or_const
2904 : tIDENTIFIER
2905 | tCONSTANT
2906 ;
2907
2908%rule %inline user_or_keyword_variable
2909 : user_variable
2910 | keyword_variable
2911 ;
2912
2913/*
2914 * parameterizing rules
2915 */
2916%rule asgn(lhs, rhs) <node>
2917 : lhs '=' lex_ctxt rhs
2918 {
2919 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2920 /*% ripper: assign!($:1, $:4) %*/
2921 }
2922 ;
2923
2924%rule args_tail_basic(value) <node_args>
2925 : f_kwarg(value) ',' f_kwrest opt_f_block_arg
2926 {
2927 $$ = new_args_tail(p, $1, $3, $4, &@3);
2928 /*% ripper: [$:1, $:3, $:4] %*/
2929 }
2930 | f_kwarg(value) opt_f_block_arg
2931 {
2932 $$ = new_args_tail(p, $1, 0, $2, &@1);
2933 /*% ripper: [$:1, Qnil, $:2] %*/
2934 }
2935 | f_any_kwrest opt_f_block_arg
2936 {
2937 $$ = new_args_tail(p, 0, $1, $2, &@1);
2938 /*% ripper: [Qnil, $:1, $:2] %*/
2939 }
2940 | f_block_arg
2941 {
2942 $$ = new_args_tail(p, 0, 0, $1, &@1);
2943 /*% ripper: [Qnil, Qnil, $:1] %*/
2944 }
2945
2946%rule def_endless_method(bodystmt) <node>
2947 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2948 {
2949 endless_method_name(p, $head->nd_mid, &@head);
2950 restore_defun(p, $head);
2951 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
2952 ($$ = $head->nd_def)->nd_loc = @$;
2953 RNODE_DEFN($$)->nd_defn = $bodystmt;
2954 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2955 /*% ripper: def!($:head, $:args, $:$) %*/
2956 local_pop(p);
2957 }
2958 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2959 {
2960 endless_method_name(p, $head->nd_mid, &@head);
2961 restore_defun(p, $head);
2962 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
2963 ($$ = $head->nd_def)->nd_loc = @$;
2964 RNODE_DEFS($$)->nd_defn = $bodystmt;
2965 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2966 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2967 local_pop(p);
2968 }
2969 ;
2970
2971%rule compstmt(stmts) <node>
2972 : stmts terms?
2973 {
2974 void_stmts(p, $$ = $stmts);
2975 }
2976 ;
2977
2978%rule f_opt(value) <node_opt_arg>
2979 : f_arg_asgn f_eq value
2980 {
2981 p->ctxt.in_argdef = 1;
2982 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
2983 /*% ripper: [$:$, $:3] %*/
2984 }
2985 ;
2986
2987%rule f_optarg(value) <node_opt_arg>
2988 : f_opt(value)
2989 {
2990 $$ = $f_opt;
2991 /*% ripper: rb_ary_new3(1, $:1) %*/
2992 }
2993 | f_optarg(value) ',' f_opt(value)
2994 {
2995 $$ = opt_arg_append($f_optarg, $f_opt);
2996 /*% ripper: rb_ary_push($:1, $:3) %*/
2997 }
2998 ;
2999
3000%rule f_kw(value) <node_kw_arg>
3001 : f_label value
3002 {
3003 p->ctxt.in_argdef = 1;
3004 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3005 /*% ripper: [$:$, $:value] %*/
3006 }
3007 | f_label
3008 {
3009 p->ctxt.in_argdef = 1;
3010 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3011 /*% ripper: [$:$, 0] %*/
3012 }
3013 ;
3014
3015%rule f_kwarg(value) <node_kw_arg>
3016 : f_kw(value)
3017 {
3018 $$ = $f_kw;
3019 /*% ripper: rb_ary_new3(1, $:1) %*/
3020 }
3021 | f_kwarg(value) ',' f_kw(value)
3022 {
3023 $$ = kwd_append($f_kwarg, $f_kw);
3024 /*% ripper: rb_ary_push($:1, $:3) %*/
3025 }
3026 ;
3027
3028%rule mlhs(item) <node>
3029 : item
3030 {
3031 $$ = NEW_LIST($1, &@$);
3032 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3033 }
3034 | mlhs(item) ',' item
3035 {
3036 $$ = list_append(p, $1, $3);
3037 /*% ripper: mlhs_add!($:1, $:3) %*/
3038 }
3039 ;
3040
3041%rule op_asgn(rhs) <node>
3042 : var_lhs tOP_ASGN lex_ctxt rhs
3043 {
3044 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3045 /*% ripper: opassign!($:1, $:2, $:4) %*/
3046 }
3047 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3048 {
3049 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3050 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3051 }
3052 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3053 {
3054 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3055 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3056 }
3057 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3058 {
3059 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3060 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3061 }
3062 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3063 {
3064 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3065 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3066 }
3067 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3068 {
3069 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3070 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3071 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3072 }
3073 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
3074 {
3075 YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3076 $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3077 /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
3078 }
3079 | backref tOP_ASGN lex_ctxt rhs
3080 {
3081 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3082 $$ = NEW_ERROR(&@$);
3083 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3084 }
3085 ;
3086
3087%rule opt_args_tail(tail) <node_args>
3088 : ',' tail
3089 {
3090 $$ = $tail;
3091 /*% ripper: $:2 %*/
3092 }
3093 | /* none */
3094 {
3095 $$ = new_args_tail(p, 0, 0, 0, &@0);
3096 /*% ripper: [Qnil, Qnil, Qnil] %*/
3097 }
3098 ;
3099
3100%rule value_expr(value) <node>
3101 : value
3102 {
3103 value_expr($1);
3104 $$ = $1;
3105 }
3106 ;
3107
3108%rule words(begin, word_list) <node>
3109 : begin ' '+ word_list tSTRING_END
3110 {
3111 $$ = make_list($word_list, &@$);
3112 /*% ripper: array!($:3) %*/
3113 }
3114 ;
3115
3116%%
3117program : {
3118 SET_LEX_STATE(EXPR_BEG);
3119 local_push(p, ifndef_ripper(1)+0);
3120 /* jumps are possible in the top-level loop. */
3121 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3122 }
3123 compstmt(top_stmts)
3124 {
3125 if ($2 && !compile_for_eval) {
3126 NODE *node = $2;
3127 /* last expression should not be void */
3128 if (nd_type_p(node, NODE_BLOCK)) {
3129 while (RNODE_BLOCK(node)->nd_next) {
3130 node = RNODE_BLOCK(node)->nd_next;
3131 }
3132 node = RNODE_BLOCK(node)->nd_head;
3133 }
3134 node = remove_begin(node);
3135 void_expr(p, node);
3136 }
3137 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
3138 /*% ripper[final]: program!($:2) %*/
3139 local_pop(p);
3140 }
3141 ;
3142
3143top_stmts : none
3144 {
3145 $$ = NEW_BEGIN(0, &@$);
3146 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3147 }
3148 | top_stmt
3149 {
3150 $$ = newline_node($1);
3151 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3152 }
3153 | top_stmts terms top_stmt
3154 {
3155 $$ = block_append(p, $1, newline_node($3));
3156 /*% ripper: stmts_add!($:1, $:3) %*/
3157 }
3158 ;
3159
3160top_stmt : stmt
3161 {
3162 clear_block_exit(p, true);
3163 $$ = $1;
3164 }
3165 | keyword_BEGIN begin_block
3166 {
3167 $$ = $2;
3168 /*% ripper: $:2 %*/
3169 }
3170 ;
3171
3172block_open : '{' {$$ = init_block_exit(p);};
3173
3174begin_block : block_open compstmt(top_stmts) '}'
3175 {
3176 restore_block_exit(p, $block_open);
3177 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3178 NEW_BEGIN($2, &@$));
3179 $$ = NEW_BEGIN(0, &@$);
3180 /*% ripper: BEGIN!($:2) %*/
3181 }
3182 ;
3183
3184bodystmt : compstmt(stmts)[body]
3185 lex_ctxt[ctxt]
3186 opt_rescue
3187 k_else
3188 {
3189 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3190 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3191 }
3192 compstmt(stmts)[elsebody]
3193 {
3194 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3195 }
3196 opt_ensure
3197 {
3198 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3199 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3200 }
3201 | compstmt(stmts)[body]
3202 lex_ctxt[ctxt]
3203 opt_rescue
3204 {
3205 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3206 }
3207 opt_ensure
3208 {
3209 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3210 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3211 }
3212 ;
3213
3214stmts : none
3215 {
3216 $$ = NEW_BEGIN(0, &@$);
3217 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3218 }
3219 | stmt_or_begin
3220 {
3221 $$ = newline_node($1);
3222 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3223 }
3224 | stmts terms stmt_or_begin
3225 {
3226 $$ = block_append(p, $1, newline_node($3));
3227 /*% ripper: stmts_add!($:1, $:3) %*/
3228 }
3229 ;
3230
3231stmt_or_begin : stmt
3232 | keyword_BEGIN
3233 {
3234 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3235 }
3236 begin_block
3237 {
3238 $$ = $3;
3239 }
3240 ;
3241
3242allow_exits : {$$ = allow_block_exit(p);};
3243
3244k_END : keyword_END lex_ctxt
3245 {
3246 $$ = $2;
3247 p->ctxt.in_rescue = before_rescue;
3248 /*% ripper: $:2 %*/
3249 };
3250
3251stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3252 {
3253 $$ = NEW_ALIAS($2, $4, &@$, &@1);
3254 /*% ripper: alias!($:2, $:4) %*/
3255 }
3256 | keyword_alias tGVAR tGVAR
3257 {
3258 $$ = NEW_VALIAS($2, $3, &@$, &@1);
3259 /*% ripper: var_alias!($:2, $:3) %*/
3260 }
3261 | keyword_alias tGVAR tBACK_REF
3262 {
3263 char buf[2];
3264 buf[0] = '$';
3265 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
3266 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$, &@1);
3267 /*% ripper: var_alias!($:2, $:3) %*/
3268 }
3269 | keyword_alias tGVAR tNTH_REF
3270 {
3271 static const char mesg[] = "can't make alias for the number variables";
3272 /*%%%*/
3273 yyerror1(&@3, mesg);
3274 /*% %*/
3275 $$ = NEW_ERROR(&@$);
3276 /*% ripper[error]: alias_error!(ERR_MESG(), $:3) %*/
3277 }
3278 | keyword_undef undef_list
3279 {
3280 nd_set_first_loc($2, @1.beg_pos);
3281 RNODE_UNDEF($2)->keyword_loc = @1;
3282 $$ = $2;
3283 /*% ripper: undef!($:2) %*/
3284 }
3285 | stmt modifier_if expr_value
3286 {
3287 $$ = new_if(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3288 fixpos($$, $3);
3289 /*% ripper: if_mod!($:3, $:1) %*/
3290 }
3291 | stmt modifier_unless expr_value
3292 {
3293 $$ = new_unless(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3294 fixpos($$, $3);
3295 /*% ripper: unless_mod!($:3, $:1) %*/
3296 }
3297 | stmt modifier_while expr_value
3298 {
3299 clear_block_exit(p, false);
3300 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3301 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3302 }
3303 else {
3304 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3305 }
3306 /*% ripper: while_mod!($:3, $:1) %*/
3307 }
3308 | stmt modifier_until expr_value
3309 {
3310 clear_block_exit(p, false);
3311 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3312 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3313 }
3314 else {
3315 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3316 }
3317 /*% ripper: until_mod!($:3, $:1) %*/
3318 }
3319 | stmt modifier_rescue after_rescue stmt
3320 {
3321 p->ctxt.in_rescue = $3.in_rescue;
3322 NODE *resq;
3323 YYLTYPE loc = code_loc_gen(&@2, &@4);
3324 resq = NEW_RESBODY(0, 0, remove_begin($4), 0, &loc);
3325 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
3326 /*% ripper: rescue_mod!($:1, $:4) %*/
3327 }
3328 | k_END allow_exits '{' compstmt(stmts) '}'
3329 {
3330 if (p->ctxt.in_def) {
3331 rb_warn0("END in method; use at_exit");
3332 }
3333 restore_block_exit(p, $allow_exits);
3334 p->ctxt = $k_END;
3335 {
3336 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, &@$);
3337 $$ = NEW_POSTEXE(scope, &@$, &@1, &@3, &@5);
3338 }
3339 /*% ripper: END!($:compstmt) %*/
3340 }
3341 | command_asgn
3342 | mlhs '=' lex_ctxt command_call_value
3343 {
3344 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3345 /*% ripper: massign!($:1, $:4) %*/
3346 }
3347 | asgn(lhs, mrhs)
3348 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
3349 after_rescue stmt[resbody]
3350 {
3351 p->ctxt.in_rescue = $3.in_rescue;
3352 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3353 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3354 loc.beg_pos = @mrhs_arg.beg_pos;
3355 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3356 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
3357 /*% ripper: massign!($:1, rescue_mod!($:4, $:7)) %*/
3358 }
3359 | mlhs '=' lex_ctxt mrhs_arg
3360 {
3361 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3362 /*% ripper: massign!($:1, $:4) %*/
3363 }
3364 | expr
3365 | error
3366 {
3367 (void)yynerrs;
3368 $$ = NEW_ERROR(&@$);
3369 }
3370 ;
3371
3372command_asgn : asgn(lhs, command_rhs)
3373 | op_asgn(command_rhs)
3374 | def_endless_method(endless_command)
3375 ;
3376
3377endless_command : command
3378 | endless_command modifier_rescue after_rescue arg
3379 {
3380 p->ctxt.in_rescue = $3.in_rescue;
3381 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3382 /*% ripper: rescue_mod!($:1, $:4) %*/
3383 }
3384 | keyword_not '\n'? endless_command
3385 {
3386 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3387 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3388 }
3389 ;
3390
3391command_rhs : command_call_value %prec tOP_ASGN
3392 | command_call_value modifier_rescue after_rescue stmt
3393 {
3394 p->ctxt.in_rescue = $3.in_rescue;
3395 YYLTYPE loc = code_loc_gen(&@2, &@4);
3396 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3397 /*% ripper: rescue_mod!($:1, $:4) %*/
3398 }
3399 | command_asgn
3400 ;
3401
3402expr : command_call
3403 | expr keyword_and expr
3404 {
3405 $$ = logop(p, idAND, $1, $3, &@2, &@$);
3406 /*% ripper: binary!($:1, ID2VAL(idAND), $:3) %*/
3407 }
3408 | expr keyword_or expr
3409 {
3410 $$ = logop(p, idOR, $1, $3, &@2, &@$);
3411 /*% ripper: binary!($:1, ID2VAL(idOR), $:3) %*/
3412 }
3413 | keyword_not '\n'? expr
3414 {
3415 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3416 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3417 }
3418 | '!' command_call
3419 {
3420 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3421 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
3422 }
3423 | arg tASSOC
3424 {
3425 value_expr($arg);
3426 }
3427 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3428 p_top_expr_body[body]
3429 {
3430 pop_pktbl(p, $p_pktbl);
3431 pop_pvtbl(p, $p_pvtbl);
3432 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3433 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body), &@$, &NULL_LOC, &NULL_LOC);
3434 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3435 }
3436 | arg keyword_in
3437 {
3438 value_expr($arg);
3439 }
3440 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3441 p_top_expr_body[body]
3442 {
3443 pop_pktbl(p, $p_pktbl);
3444 pop_pvtbl(p, $p_pvtbl);
3445 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3446 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body), &@$, &NULL_LOC, &NULL_LOC);
3447 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3448 }
3449 | arg %prec tLBRACE_ARG
3450 ;
3451
3452def_name : fname
3453 {
3454 numparam_name(p, $fname);
3455 local_push(p, 0);
3456 p->ctxt.in_def = 1;
3457 p->ctxt.in_rescue = before_rescue;
3458 p->ctxt.cant_return = 0;
3459 $$ = $fname;
3460 }
3461 ;
3462
3463defn_head : k_def def_name
3464 {
3465 $$ = def_head_save(p, $k_def);
3466 $$->nd_mid = $def_name;
3467 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3468 /*% ripper: $:def_name %*/
3469 }
3470 ;
3471
3472defs_head : k_def singleton dot_or_colon
3473 {
3474 SET_LEX_STATE(EXPR_FNAME);
3475 }
3476 def_name
3477 {
3478 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3479 $$ = def_head_save(p, $k_def);
3480 $$->nd_mid = $def_name;
3481 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3482 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3483 }
3484 ;
3485
3486expr_value : value_expr(expr)
3487 | error
3488 {
3489 $$ = NEW_ERROR(&@$);
3490 }
3491 ;
3492
3493expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3494 {
3495 $$ = $2;
3496 /*% ripper: $:2 %*/
3497 }
3498 ;
3499
3500command_call : command
3501 | block_command
3502 ;
3503
3504command_call_value : value_expr(command_call)
3505 ;
3506
3507block_command : block_call
3508 | block_call call_op2 operation2 command_args
3509 {
3510 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3511 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3512 }
3513 ;
3514
3515cmd_brace_block : tLBRACE_ARG brace_body '}'
3516 {
3517 $$ = $2;
3518 set_embraced_location($$, &@1, &@3);
3519 /*% ripper: $:2 %*/
3520 }
3521 ;
3522
3523fcall : operation
3524 {
3525 $$ = NEW_FCALL($1, 0, &@$);
3526 /*% ripper: $:1 %*/
3527 }
3528 ;
3529
3530command : fcall command_args %prec tLOWEST
3531 {
3532 $1->nd_args = $2;
3533 nd_set_last_loc($1, @2.end_pos);
3534 $$ = (NODE *)$1;
3535 /*% ripper: command!($:1, $:2) %*/
3536 }
3537 | fcall command_args cmd_brace_block
3538 {
3539 block_dup_check(p, $2, $3);
3540 $1->nd_args = $2;
3541 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3542 fixpos($$, RNODE($1));
3543 nd_set_last_loc($1, @2.end_pos);
3544 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3545 }
3546 | primary_value call_op operation2 command_args %prec tLOWEST
3547 {
3548 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3549 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3550 }
3551 | primary_value call_op operation2 command_args cmd_brace_block
3552 {
3553 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3554 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3555 }
3556 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3557 {
3558 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3559 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3560 }
3561 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3562 {
3563 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3564 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3565 }
3566 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3567 {
3568 set_embraced_location($5, &@4, &@6);
3569 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3570 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3571 }
3572 | keyword_super command_args
3573 {
3574 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3575 fixpos($$, $2);
3576 /*% ripper: super!($:2) %*/
3577 }
3578 | k_yield command_args
3579 {
3580 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3581 fixpos($$, $2);
3582 /*% ripper: yield!($:2) %*/
3583 }
3584 | k_return call_args
3585 {
3586 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3587 /*% ripper: return!($:2) %*/
3588 }
3589 | keyword_break call_args
3590 {
3591 NODE *args = 0;
3592 args = ret_args(p, $2);
3593 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3594 /*% ripper: break!($:2) %*/
3595 }
3596 | keyword_next call_args
3597 {
3598 NODE *args = 0;
3599 args = ret_args(p, $2);
3600 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3601 /*% ripper: next!($:2) %*/
3602 }
3603 ;
3604
3605mlhs : mlhs_basic
3606 | tLPAREN mlhs_inner rparen
3607 {
3608 $$ = $2;
3609 /*% ripper: mlhs_paren!($:2) %*/
3610 }
3611 ;
3612
3613mlhs_inner : mlhs_basic
3614 | tLPAREN mlhs_inner rparen
3615 {
3616 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3617 /*% ripper: mlhs_paren!($:2) %*/
3618 }
3619 ;
3620
3621mlhs_basic : mlhs_head
3622 {
3623 $$ = NEW_MASGN($1, 0, &@$);
3624 /*% ripper: $:1 %*/
3625 }
3626 | mlhs_head mlhs_item
3627 {
3628 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3629 /*% ripper: mlhs_add!($:1, $:2) %*/
3630 }
3631 | mlhs_head tSTAR mlhs_node
3632 {
3633 $$ = NEW_MASGN($1, $3, &@$);
3634 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3635 }
3636 | mlhs_head tSTAR mlhs_node ',' mlhs(mlhs_item)
3637 {
3638 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3639 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3640 }
3641 | mlhs_head tSTAR
3642 {
3643 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3644 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3645 }
3646 | mlhs_head tSTAR ',' mlhs(mlhs_item)
3647 {
3648 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3649 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3650 }
3651 | tSTAR mlhs_node
3652 {
3653 $$ = NEW_MASGN(0, $2, &@$);
3654 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3655 }
3656 | tSTAR mlhs_node ',' mlhs(mlhs_item)
3657 {
3658 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3659 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3660 }
3661 | tSTAR
3662 {
3663 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3664 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3665 }
3666 | tSTAR ',' mlhs(mlhs_item)
3667 {
3668 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3669 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3670 }
3671 ;
3672
3673mlhs_item : mlhs_node
3674 | tLPAREN mlhs_inner rparen
3675 {
3676 $$ = (NODE *)$2;
3677 /*% ripper: mlhs_paren!($:2) %*/
3678 }
3679 ;
3680
3681mlhs_head : mlhs_item ','
3682 {
3683 $$ = NEW_LIST($1, &@1);
3684 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3685 }
3686 | mlhs_head mlhs_item ','
3687 {
3688 $$ = list_append(p, $1, $2);
3689 /*% ripper: mlhs_add!($:1, $:2) %*/
3690 }
3691 ;
3692
3693
3694mlhs_node : user_or_keyword_variable
3695 {
3696 /*% ripper: var_field!($:1) %*/
3697 $$ = assignable(p, $1, 0, &@$);
3698 }
3699 | primary_value '[' opt_call_args rbracket
3700 {
3701 $$ = aryset(p, $1, $3, &@$);
3702 /*% ripper: aref_field!($:1, $:3) %*/
3703 }
3704 | primary_value call_op ident_or_const
3705 {
3706 anddot_multiple_assignment_check(p, &@2, $2);
3707 $$ = attrset(p, $1, $2, $3, &@$);
3708 /*% ripper: field!($:1, $:2, $:3) %*/
3709 }
3710 | primary_value tCOLON2 tIDENTIFIER
3711 {
3712 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3713 /*% ripper: const_path_field!($:1, $:3) %*/
3714 }
3715 | primary_value tCOLON2 tCONSTANT
3716 {
3717 /*% ripper: const_path_field!($:1, $:3) %*/
3718 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3719 }
3720 | tCOLON3 tCONSTANT
3721 {
3722 /*% ripper: top_const_field!($:2) %*/
3723 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3724 }
3725 | backref
3726 {
3727 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3728 $$ = NEW_ERROR(&@$);
3729 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3730 }
3731 ;
3732
3733lhs : user_or_keyword_variable
3734 {
3735 /*% ripper: var_field!($:1) %*/
3736 $$ = assignable(p, $1, 0, &@$);
3737 }
3738 | primary_value '[' opt_call_args rbracket
3739 {
3740 $$ = aryset(p, $1, $3, &@$);
3741 /*% ripper: aref_field!($:1, $:3) %*/
3742 }
3743 | primary_value call_op ident_or_const
3744 {
3745 $$ = attrset(p, $1, $2, $3, &@$);
3746 /*% ripper: field!($:1, $:2, $:3) %*/
3747 }
3748 | primary_value tCOLON2 tIDENTIFIER
3749 {
3750 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3751 /*% ripper: field!($:1, $:2, $:3) %*/
3752 }
3753 | primary_value tCOLON2 tCONSTANT
3754 {
3755 /*% ripper: const_path_field!($:1, $:3) %*/
3756 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3757 }
3758 | tCOLON3 tCONSTANT
3759 {
3760 /*% ripper: top_const_field!($:2) %*/
3761 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3762 }
3763 | backref
3764 {
3765 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3766 $$ = NEW_ERROR(&@$);
3767 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3768 }
3769 ;
3770
3771cname : tIDENTIFIER
3772 {
3773 static const char mesg[] = "class/module name must be CONSTANT";
3774 /*%%%*/
3775 yyerror1(&@1, mesg);
3776 /*% %*/
3777 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3778 }
3779 | tCONSTANT
3780 ;
3781
3782cpath : tCOLON3 cname
3783 {
3784 $$ = NEW_COLON3($2, &@$);
3785 /*% ripper: top_const_ref!($:2) %*/
3786 }
3787 | cname
3788 {
3789 $$ = NEW_COLON2(0, $1, &@$);
3790 /*% ripper: const_ref!($:1) %*/
3791 }
3792 | primary_value tCOLON2 cname
3793 {
3794 $$ = NEW_COLON2($1, $3, &@$);
3795 /*% ripper: const_path_ref!($:1, $:3) %*/
3796 }
3797 ;
3798
3799fname : operation
3800 | op
3801 {
3802 SET_LEX_STATE(EXPR_ENDFN);
3803 $$ = $1;
3804 }
3805 | reswords
3806 ;
3807
3808fitem : fname
3809 {
3810 $$ = NEW_SYM(rb_id2str($1), &@$);
3811 /*% ripper: symbol_literal!($:1) %*/
3812 }
3813 | symbol
3814 ;
3815
3816undef_list : fitem
3817 {
3818 $$ = NEW_UNDEF($1, &@$);
3819 /*% ripper: rb_ary_new3(1, $:1) %*/
3820 }
3821 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3822 {
3823 nd_set_last_loc($1, @4.end_pos);
3824 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3825 /*% ripper: rb_ary_push($:1, $:4) %*/
3826 }
3827 ;
3828
3829op : '|' { $$ = '|'; }
3830 | '^' { $$ = '^'; }
3831 | '&' { $$ = '&'; }
3832 | tCMP { $$ = tCMP; }
3833 | tEQ { $$ = tEQ; }
3834 | tEQQ { $$ = tEQQ; }
3835 | tMATCH { $$ = tMATCH; }
3836 | tNMATCH { $$ = tNMATCH; }
3837 | '>' { $$ = '>'; }
3838 | tGEQ { $$ = tGEQ; }
3839 | '<' { $$ = '<'; }
3840 | tLEQ { $$ = tLEQ; }
3841 | tNEQ { $$ = tNEQ; }
3842 | tLSHFT { $$ = tLSHFT; }
3843 | tRSHFT { $$ = tRSHFT; }
3844 | '+' { $$ = '+'; }
3845 | '-' { $$ = '-'; }
3846 | '*' { $$ = '*'; }
3847 | tSTAR { $$ = '*'; }
3848 | '/' { $$ = '/'; }
3849 | '%' { $$ = '%'; }
3850 | tPOW { $$ = tPOW; }
3851 | tDSTAR { $$ = tDSTAR; }
3852 | '!' { $$ = '!'; }
3853 | '~' { $$ = '~'; }
3854 | tUPLUS { $$ = tUPLUS; }
3855 | tUMINUS { $$ = tUMINUS; }
3856 | tAREF { $$ = tAREF; }
3857 | tASET { $$ = tASET; }
3858 | '`' { $$ = '`'; }
3859 ;
3860
3861reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3862 | keyword_BEGIN | keyword_END
3863 | keyword_alias | keyword_and | keyword_begin
3864 | keyword_break | keyword_case | keyword_class | keyword_def
3865 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3866 | keyword_end | keyword_ensure | keyword_false
3867 | keyword_for | keyword_in | keyword_module | keyword_next
3868 | keyword_nil | keyword_not | keyword_or | keyword_redo
3869 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3870 | keyword_super | keyword_then | keyword_true | keyword_undef
3871 | keyword_when | keyword_yield | keyword_if | keyword_unless
3872 | keyword_while | keyword_until
3873 ;
3874
3875arg : asgn(lhs, arg_rhs)
3876 | op_asgn(arg_rhs)
3877 | arg tDOT2 arg
3878 {
3879 value_expr($1);
3880 value_expr($3);
3881 $$ = NEW_DOT2($1, $3, &@$, &@2);
3882 /*% ripper: dot2!($:1, $:3) %*/
3883 }
3884 | arg tDOT3 arg
3885 {
3886 value_expr($1);
3887 value_expr($3);
3888 $$ = NEW_DOT3($1, $3, &@$, &@2);
3889 /*% ripper: dot3!($:1, $:3) %*/
3890 }
3891 | arg tDOT2
3892 {
3893 value_expr($1);
3894 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3895 /*% ripper: dot2!($:1, Qnil) %*/
3896 }
3897 | arg tDOT3
3898 {
3899 value_expr($1);
3900 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3901 /*% ripper: dot3!($:1, Qnil) %*/
3902 }
3903 | tBDOT2 arg
3904 {
3905 value_expr($2);
3906 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3907 /*% ripper: dot2!(Qnil, $:2) %*/
3908 }
3909 | tBDOT3 arg
3910 {
3911 value_expr($2);
3912 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3913 /*% ripper: dot3!(Qnil, $:2) %*/
3914 }
3915 | arg '+' arg
3916 {
3917 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3918 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3919 }
3920 | arg '-' arg
3921 {
3922 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3923 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3924 }
3925 | arg '*' arg
3926 {
3927 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3928 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3929 }
3930 | arg '/' arg
3931 {
3932 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3933 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3934 }
3935 | arg '%' arg
3936 {
3937 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3938 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3939 }
3940 | arg tPOW arg
3941 {
3942 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3943 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3944 }
3945 | tUMINUS_NUM simple_numeric tPOW arg
3946 {
3947 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3948 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3949 }
3950 | tUPLUS arg
3951 {
3952 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3953 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3954 }
3955 | tUMINUS arg
3956 {
3957 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3958 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3959 }
3960 | arg '|' arg
3961 {
3962 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3963 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3964 }
3965 | arg '^' arg
3966 {
3967 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3968 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3969 }
3970 | arg '&' arg
3971 {
3972 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3973 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
3974 }
3975 | arg tCMP arg
3976 {
3977 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3978 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
3979 }
3980 | rel_expr %prec tCMP
3981 | arg tEQ arg
3982 {
3983 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3984 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
3985 }
3986 | arg tEQQ arg
3987 {
3988 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
3989 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
3990 }
3991 | arg tNEQ arg
3992 {
3993 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
3994 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
3995 }
3996 | arg tMATCH arg
3997 {
3998 $$ = match_op(p, $1, $3, &@2, &@$);
3999 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4000 }
4001 | arg tNMATCH arg
4002 {
4003 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4004 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4005 }
4006 | '!' arg
4007 {
4008 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4009 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4010 }
4011 | '~' arg
4012 {
4013 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4014 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4015 }
4016 | arg tLSHFT arg
4017 {
4018 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4019 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4020 }
4021 | arg tRSHFT arg
4022 {
4023 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4024 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4025 }
4026 | arg tANDOP arg
4027 {
4028 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4029 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4030 }
4031 | arg tOROP arg
4032 {
4033 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4034 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4035 }
4036 | keyword_defined '\n'? begin_defined arg
4037 {
4038 p->ctxt.in_defined = $3.in_defined;
4039 $$ = new_defined(p, $4, &@$);
4040 /*% ripper: defined!($:4) %*/
4041 }
4042 | arg '?' arg '\n'? ':' arg
4043 {
4044 value_expr($1);
4045 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4046 fixpos($$, $1);
4047 /*% ripper: ifop!($:1, $:3, $:6) %*/
4048 }
4049 | def_endless_method(endless_arg)
4050 | primary
4051 ;
4052
4053endless_arg : arg %prec modifier_rescue
4054 | endless_arg modifier_rescue after_rescue arg
4055 {
4056 p->ctxt.in_rescue = $3.in_rescue;
4057 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4058 /*% ripper: rescue_mod!($:1, $:4) %*/
4059 }
4060 | keyword_not '\n'? endless_arg
4061 {
4062 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4063 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4064 }
4065 ;
4066
4067relop : '>' {$$ = '>';}
4068 | '<' {$$ = '<';}
4069 | tGEQ {$$ = idGE;}
4070 | tLEQ {$$ = idLE;}
4071 ;
4072
4073rel_expr : arg relop arg %prec '>'
4074 {
4075 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4076 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4077 }
4078 | rel_expr relop arg %prec '>'
4079 {
4080 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4081 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4082 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4083 }
4084 ;
4085
4086lex_ctxt : none
4087 {
4088 $$ = p->ctxt;
4089 }
4090 ;
4091
4092begin_defined : lex_ctxt
4093 {
4094 p->ctxt.in_defined = 1;
4095 $$ = $1;
4096 }
4097 ;
4098
4099after_rescue : lex_ctxt
4100 {
4101 p->ctxt.in_rescue = after_rescue;
4102 $$ = $1;
4103 }
4104 ;
4105
4106arg_value : value_expr(arg)
4107 ;
4108
4109aref_args : none
4110 | args trailer
4111 | args ',' assocs trailer
4112 {
4113 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4114 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4115 }
4116 | assocs trailer
4117 {
4118 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4119 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4120 }
4121 ;
4122
4123arg_rhs : arg %prec tOP_ASGN
4124 {
4125 value_expr($1);
4126 $$ = $1;
4127 }
4128 | arg modifier_rescue after_rescue arg
4129 {
4130 p->ctxt.in_rescue = $3.in_rescue;
4131 value_expr($1);
4132 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4133 /*% ripper: rescue_mod!($:1, $:4) %*/
4134 }
4135 ;
4136
4137paren_args : '(' opt_call_args rparen
4138 {
4139 $$ = $2;
4140 /*% ripper: arg_paren!($:2) %*/
4141 }
4142 | '(' args ',' args_forward rparen
4143 {
4144 if (!check_forwarding_args(p)) {
4145 $$ = 0;
4146 }
4147 else {
4148 $$ = new_args_forward_call(p, $2, &@4, &@$);
4149 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4150 }
4151 }
4152 | '(' args_forward rparen
4153 {
4154 if (!check_forwarding_args(p)) {
4155 $$ = 0;
4156 }
4157 else {
4158 $$ = new_args_forward_call(p, 0, &@2, &@$);
4159 /*% ripper: arg_paren!($:2) %*/
4160 }
4161 }
4162 ;
4163
4164opt_paren_args : none
4165 | paren_args
4166 {
4167 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4168 }
4169 ;
4170
4171opt_call_args : none
4172 | call_args
4173 | args ','
4174 | args ',' assocs ','
4175 {
4176 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4177 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4178 }
4179 | assocs ','
4180 {
4181 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4182 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4183 }
4184 ;
4185
4186call_args : value_expr(command)
4187 {
4188 $$ = NEW_LIST($1, &@$);
4189 /*% ripper: args_add!(args_new!, $:1) %*/
4190 }
4191 | args opt_block_arg
4192 {
4193 $$ = arg_blk_pass($1, $2);
4194 /*% ripper: args_add_block!($:1, $:2) %*/
4195 }
4196 | assocs opt_block_arg
4197 {
4198 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4199 $$ = arg_blk_pass($$, $2);
4200 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4201 }
4202 | args ',' assocs opt_block_arg
4203 {
4204 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4205 $$ = arg_blk_pass($$, $4);
4206 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4207 }
4208 | block_arg
4209 /*% ripper: args_add_block!(args_new!, $:1) %*/
4210 ;
4211
4212command_args : {
4213 /* If call_args starts with a open paren '(' or '[',
4214 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4215 * but the push must be done after CMDARG_PUSH(1).
4216 * So this code makes them consistent by first cancelling
4217 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4218 * and finally redoing CMDARG_PUSH(0).
4219 */
4220 int lookahead = 0;
4221 switch (yychar) {
4222 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4223 lookahead = 1;
4224 }
4225 if (lookahead) CMDARG_POP();
4226 CMDARG_PUSH(1);
4227 if (lookahead) CMDARG_PUSH(0);
4228 }
4229 call_args
4230 {
4231 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4232 * but the push must be done after CMDARG_POP() in the parser.
4233 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4234 * CMDARG_POP() to pop 1 pushed by command_args,
4235 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4236 */
4237 int lookahead = 0;
4238 switch (yychar) {
4239 case tLBRACE_ARG:
4240 lookahead = 1;
4241 }
4242 if (lookahead) CMDARG_POP();
4243 CMDARG_POP();
4244 if (lookahead) CMDARG_PUSH(0);
4245 $$ = $2;
4246 /*% ripper: $:2 %*/
4247 }
4248 ;
4249
4250block_arg : tAMPER arg_value
4251 {
4252 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4253 /*% ripper: $:2 %*/
4254 }
4255 | tAMPER
4256 {
4257 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4258 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4259 /*% ripper: Qnil %*/
4260 }
4261 ;
4262
4263opt_block_arg : ',' block_arg
4264 {
4265 $$ = $2;
4266 /*% ripper: $:2 %*/
4267 }
4268 | none
4269 {
4270 $$ = 0;
4271 /*% ripper: Qfalse %*/
4272 }
4273 ;
4274
4275/* value */
4276args : arg_value
4277 {
4278 $$ = NEW_LIST($arg_value, &@$);
4279 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4280 }
4281 | arg_splat
4282 {
4283 $$ = $arg_splat;
4284 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4285 }
4286 | args[non_last_args] ',' arg_value
4287 {
4288 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4289 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4290 }
4291 | args[non_last_args] ',' arg_splat
4292 {
4293 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4294 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4295 }
4296 ;
4297
4298/* value */
4299arg_splat : tSTAR arg_value
4300 {
4301 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4302 /*% ripper: $:arg_value %*/
4303 }
4304 | tSTAR /* none */
4305 {
4306 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4307 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4308 /*% ripper: Qnil %*/
4309 }
4310 ;
4311
4312/* value */
4313mrhs_arg : mrhs
4314 | arg_value
4315 ;
4316
4317/* value */
4318mrhs : args ',' arg_value
4319 {
4320 $$ = last_arg_append(p, $args, $arg_value, &@$);
4321 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4322 }
4323 | args ',' tSTAR arg_value
4324 {
4325 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4326 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4327 }
4328 | tSTAR arg_value
4329 {
4330 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4331 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4332 }
4333 ;
4334
4335%rule %inline inline_primary
4336 : literal
4337 | strings
4338 | xstring
4339 | regexp
4340 | words
4341 | qwords
4342 | symbols
4343 | qsymbols
4344 ;
4345
4346primary : inline_primary
4347 | var_ref
4348 | backref
4349 | tFID
4350 {
4351 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
4352 /*% ripper: method_add_arg!(fcall!($:1), args_new!) %*/
4353 }
4354 | k_begin
4355 {
4356 CMDARG_PUSH(0);
4357 }
4358 bodystmt
4359 k_end
4360 {
4361 CMDARG_POP();
4362 set_line_body($3, @1.end_pos.lineno);
4363 $$ = NEW_BEGIN($3, &@$);
4364 nd_set_line($$, @1.end_pos.lineno);
4365 /*% ripper: begin!($:3) %*/
4366 }
4367 | tLPAREN_ARG compstmt(stmts) {SET_LEX_STATE(EXPR_ENDARG);} ')'
4368 {
4369 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4370 $$ = $2;
4371 /*% ripper: paren!($:2) %*/
4372 }
4373 | tLPAREN compstmt(stmts) ')'
4374 {
4375 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4376 $$ = NEW_BLOCK($2, &@$);
4377 /*% ripper: paren!($:2) %*/
4378 }
4379 | primary_value tCOLON2 tCONSTANT
4380 {
4381 $$ = NEW_COLON2($1, $3, &@$);
4382 /*% ripper: const_path_ref!($:1, $:3) %*/
4383 }
4384 | tCOLON3 tCONSTANT
4385 {
4386 $$ = NEW_COLON3($2, &@$);
4387 /*% ripper: top_const_ref!($:2) %*/
4388 }
4389 | tLBRACK aref_args ']'
4390 {
4391 $$ = make_list($2, &@$);
4392 /*% ripper: array!($:2) %*/
4393 }
4394 | tLBRACE assoc_list '}'
4395 {
4396 $$ = new_hash(p, $2, &@$);
4397 RNODE_HASH($$)->nd_brace = TRUE;
4398 /*% ripper: hash!($:2) %*/
4399 }
4400 | k_return
4401 {
4402 $$ = NEW_RETURN(0, &@$, &@1);
4403 /*% ripper: return0! %*/
4404 }
4405 | k_yield '(' call_args rparen
4406 {
4407 $$ = NEW_YIELD($3, &@$, &@1, &@2, &@4);
4408 /*% ripper: yield!(paren!($:3)) %*/
4409 }
4410 | k_yield '(' rparen
4411 {
4412 $$ = NEW_YIELD(0, &@$, &@1, &@2, &@3);
4413 /*% ripper: yield!(paren!(args_new!)) %*/
4414 }
4415 | k_yield
4416 {
4417 $$ = NEW_YIELD(0, &@$, &@1, &NULL_LOC, &NULL_LOC);
4418 /*% ripper: yield0! %*/
4419 }
4420 | keyword_defined '\n'? '(' begin_defined expr rparen
4421 {
4422 p->ctxt.in_defined = $4.in_defined;
4423 $$ = new_defined(p, $5, &@$);
4424 /*% ripper: defined!($:5) %*/
4425 }
4426 | keyword_not '(' expr rparen
4427 {
4428 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4429 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4430 }
4431 | keyword_not '(' rparen
4432 {
4433 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
4434 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4435 }
4436 | fcall brace_block
4437 {
4438 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
4439 /*% ripper: method_add_block!(method_add_arg!(fcall!($:1), args_new!), $:2) %*/
4440 }
4441 | method_call
4442 | method_call brace_block
4443 {
4444 block_dup_check(p, get_nd_args(p, $1), $2);
4445 $$ = method_add_block(p, $1, $2, &@$);
4446 /*% ripper: method_add_block!($:1, $:2) %*/
4447 }
4448 | lambda
4449 | k_if expr_value then
4450 compstmt(stmts)
4451 if_tail
4452 k_end
4453 {
4454 if ($5 && nd_type_p($5, NODE_IF))
4455 RNODE_IF($5)->end_keyword_loc = @6;
4456
4457 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4458 fixpos($$, $2);
4459 /*% ripper: if!($:2, $:4, $:5) %*/
4460 }
4461 | k_unless expr_value then
4462 compstmt(stmts)
4463 opt_else
4464 k_end
4465 {
4466 $$ = new_unless(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4467 fixpos($$, $2);
4468 /*% ripper: unless!($:2, $:4, $:5) %*/
4469 }
4470 | k_while expr_value_do
4471 compstmt(stmts)
4472 k_end
4473 {
4474 restore_block_exit(p, $1);
4475 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4476 fixpos($$, $2);
4477 /*% ripper: while!($:2, $:3) %*/
4478 }
4479 | k_until expr_value_do
4480 compstmt(stmts)
4481 k_end
4482 {
4483 restore_block_exit(p, $1);
4484 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4485 fixpos($$, $2);
4486 /*% ripper: until!($:2, $:3) %*/
4487 }
4488 | k_case expr_value terms?
4489 {
4490 $$ = p->case_labels;
4491 p->case_labels = CHECK_LITERAL_WHEN;
4492 }<labels>
4493 case_body
4494 k_end
4495 {
4496 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4497 p->case_labels = $4;
4498 $$ = NEW_CASE($2, $5, &@$, &@1, &@6);
4499 fixpos($$, $2);
4500 /*% ripper: case!($:2, $:5) %*/
4501 }
4502 | k_case terms?
4503 {
4504 $$ = p->case_labels;
4505 p->case_labels = 0;
4506 }<labels>
4507 case_body
4508 k_end
4509 {
4510 if (p->case_labels) st_free_table(p->case_labels);
4511 p->case_labels = $3;
4512 $$ = NEW_CASE2($4, &@$, &@1, &@5);
4513 /*% ripper: case!(Qnil, $:4) %*/
4514 }
4515 | k_case expr_value terms?
4516 p_case_body
4517 k_end
4518 {
4519 $$ = NEW_CASE3($2, $4, &@$, &@1, &@5);
4520 /*% ripper: case!($:2, $:4) %*/
4521 }
4522 | k_for for_var keyword_in
4523 {COND_PUSH(1);} expr_value do {COND_POP();}
4524 compstmt(stmts)
4525 k_end
4526 {
4527 restore_block_exit(p, $k_for);
4528 /*
4529 * for a, b, c in e
4530 * #=>
4531 * e.each{|*x| a, b, c = x}
4532 *
4533 * for a in e
4534 * #=>
4535 * e.each{|x| a, = x}
4536 */
4537 ID id = internal_id(p);
4538 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4539 rb_node_args_t *args;
4540 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4541 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4542 tbl->ids[0] = id; /* internal id */
4543
4544 switch (nd_type($for_var)) {
4545 case NODE_LASGN:
4546 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4547 set_nd_value(p, $for_var, internal_var);
4548 id = 0;
4549 m->nd_plen = 1;
4550 m->nd_next = $for_var;
4551 break;
4552 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4553 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4554 break;
4555 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4556 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);
4557 }
4558 /* {|*internal_id| <m> = internal_id; ... } */
4559 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@for_var), &@for_var);
4560 scope = NEW_SCOPE2(tbl, args, $compstmt, &@$);
4561 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4562 $$ = NEW_FOR($5, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4563 fixpos($$, $for_var);
4564 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4565 }
4566 | k_class cpath superclass
4567 {
4568 begin_definition("class", &@k_class, &@cpath);
4569 }
4570 bodystmt
4571 k_end
4572 {
4573 YYLTYPE inheritance_operator_loc = NULL_LOC;
4574 if ($superclass) {
4575 inheritance_operator_loc = @superclass;
4576 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
4577 }
4578 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
4579 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4580 set_line_body($bodystmt, @superclass.end_pos.lineno);
4581 nd_set_line($$, @superclass.end_pos.lineno);
4582 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4583 local_pop(p);
4584 p->ctxt.in_class = $k_class.in_class;
4585 p->ctxt.cant_return = $k_class.cant_return;
4586 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4587 }
4588 | k_class tLSHFT expr_value
4589 {
4590 begin_definition("", &@k_class, &@tLSHFT);
4591 }
4592 term
4593 bodystmt
4594 k_end
4595 {
4596 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$);
4597 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4598 set_line_body($bodystmt, nd_line($expr_value));
4599 fixpos($$, $expr_value);
4600 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4601 local_pop(p);
4602 p->ctxt.in_def = $k_class.in_def;
4603 p->ctxt.in_class = $k_class.in_class;
4604 p->ctxt.cant_return = $k_class.cant_return;
4605 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4606 }
4607 | k_module cpath
4608 {
4609 begin_definition("module", &@k_module, &@cpath);
4610 }
4611 bodystmt
4612 k_end
4613 {
4614 $$ = NEW_MODULE($cpath, $bodystmt, &@$);
4615 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4616 set_line_body($bodystmt, @cpath.end_pos.lineno);
4617 nd_set_line($$, @cpath.end_pos.lineno);
4618 /*% ripper: module!($:cpath, $:bodystmt) %*/
4619 local_pop(p);
4620 p->ctxt.in_class = $k_module.in_class;
4621 p->ctxt.cant_return = $k_module.cant_return;
4622 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4623 }
4624 | defn_head[head]
4625 f_arglist[args]
4626 {
4627 push_end_expect_token_locations(p, &@head.beg_pos);
4628 }
4629 bodystmt
4630 k_end
4631 {
4632 restore_defun(p, $head);
4633 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4634 ($$ = $head->nd_def)->nd_loc = @$;
4635 RNODE_DEFN($$)->nd_defn = $bodystmt;
4636 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4637 local_pop(p);
4638 }
4639 | defs_head[head]
4640 f_arglist[args]
4641 {
4642 push_end_expect_token_locations(p, &@head.beg_pos);
4643 }
4644 bodystmt
4645 k_end
4646 {
4647 restore_defun(p, $head);
4648 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4649 ($$ = $head->nd_def)->nd_loc = @$;
4650 RNODE_DEFS($$)->nd_defn = $bodystmt;
4651 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4652 local_pop(p);
4653 }
4654 | keyword_break
4655 {
4656 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@1));
4657 /*% ripper: break!(args_new!) %*/
4658 }
4659 | keyword_next
4660 {
4661 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@1));
4662 /*% ripper: next!(args_new!) %*/
4663 }
4664 | keyword_redo
4665 {
4666 $$ = add_block_exit(p, NEW_REDO(&@$, &@1));
4667 /*% ripper: redo! %*/
4668 }
4669 | keyword_retry
4670 {
4671 if (!p->ctxt.in_defined) {
4672 switch (p->ctxt.in_rescue) {
4673 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4674 case after_rescue: /* ok */ break;
4675 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4676 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4677 }
4678 }
4679 $$ = NEW_RETRY(&@$);
4680 /*% ripper: retry! %*/
4681 }
4682 ;
4683
4684primary_value : value_expr(primary)
4685 ;
4686
4687k_begin : keyword_begin
4688 {
4689 token_info_push(p, "begin", &@$);
4690 push_end_expect_token_locations(p, &@1.beg_pos);
4691 }
4692 ;
4693
4694k_if : keyword_if
4695 {
4696 WARN_EOL("if");
4697 token_info_push(p, "if", &@$);
4698 if (p->token_info && p->token_info->nonspc &&
4699 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4700 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4701 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4702 beg += rb_strlen_lit("else");
4703 while (beg < tok && ISSPACE(*beg)) beg++;
4704 if (beg == tok) {
4705 p->token_info->nonspc = 0;
4706 }
4707 }
4708 push_end_expect_token_locations(p, &@1.beg_pos);
4709 }
4710 ;
4711
4712k_unless : keyword_unless
4713 {
4714 token_info_push(p, "unless", &@$);
4715 push_end_expect_token_locations(p, &@1.beg_pos);
4716 }
4717 ;
4718
4719k_while : keyword_while allow_exits
4720 {
4721 $$ = $allow_exits;
4722 token_info_push(p, "while", &@$);
4723 push_end_expect_token_locations(p, &@1.beg_pos);
4724 }
4725 ;
4726
4727k_until : keyword_until allow_exits
4728 {
4729 $$ = $allow_exits;
4730 token_info_push(p, "until", &@$);
4731 push_end_expect_token_locations(p, &@1.beg_pos);
4732 }
4733 ;
4734
4735k_case : keyword_case
4736 {
4737 token_info_push(p, "case", &@$);
4738 push_end_expect_token_locations(p, &@1.beg_pos);
4739 }
4740 ;
4741
4742k_for : keyword_for allow_exits
4743 {
4744 $$ = $allow_exits;
4745 token_info_push(p, "for", &@$);
4746 push_end_expect_token_locations(p, &@1.beg_pos);
4747 }
4748 ;
4749
4750k_class : keyword_class
4751 {
4752 token_info_push(p, "class", &@$);
4753 $$ = p->ctxt;
4754 p->ctxt.in_rescue = before_rescue;
4755 push_end_expect_token_locations(p, &@1.beg_pos);
4756 }
4757 ;
4758
4759k_module : keyword_module
4760 {
4761 token_info_push(p, "module", &@$);
4762 $$ = p->ctxt;
4763 p->ctxt.in_rescue = before_rescue;
4764 push_end_expect_token_locations(p, &@1.beg_pos);
4765 }
4766 ;
4767
4768k_def : keyword_def
4769 {
4770 token_info_push(p, "def", &@$);
4771 $$ = NEW_DEF_TEMP(&@$);
4772 p->ctxt.in_argdef = 1;
4773 }
4774 ;
4775
4776k_do : keyword_do
4777 {
4778 token_info_push(p, "do", &@$);
4779 push_end_expect_token_locations(p, &@1.beg_pos);
4780 }
4781 ;
4782
4783k_do_block : keyword_do_block
4784 {
4785 token_info_push(p, "do", &@$);
4786 push_end_expect_token_locations(p, &@1.beg_pos);
4787 }
4788 ;
4789
4790k_rescue : keyword_rescue
4791 {
4792 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4793 $$ = p->ctxt;
4794 p->ctxt.in_rescue = after_rescue;
4795 }
4796 ;
4797
4798k_ensure : keyword_ensure
4799 {
4800 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4801 $$ = p->ctxt;
4802 }
4803 ;
4804
4805k_when : keyword_when
4806 {
4807 token_info_warn(p, "when", p->token_info, 0, &@$);
4808 }
4809 ;
4810
4811k_else : keyword_else
4812 {
4813 token_info *ptinfo_beg = p->token_info;
4814 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4815 token_info_warn(p, "else", p->token_info, same, &@$);
4816 if (same) {
4817 token_info e;
4818 e.next = ptinfo_beg->next;
4819 e.token = "else";
4820 token_info_setup(&e, p->lex.pbeg, &@$);
4821 if (!e.nonspc) *ptinfo_beg = e;
4822 }
4823 }
4824 ;
4825
4826k_elsif : keyword_elsif
4827 {
4828 WARN_EOL("elsif");
4829 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4830 }
4831 ;
4832
4833k_end : keyword_end
4834 {
4835 token_info_pop(p, "end", &@$);
4836 pop_end_expect_token_locations(p);
4837 }
4838 | tDUMNY_END
4839 {
4840 compile_error(p, "syntax error, unexpected end-of-input");
4841 }
4842 ;
4843
4844k_return : keyword_return
4845 {
4846 if (p->ctxt.cant_return && !dyna_in_block(p))
4847 yyerror1(&@1, "Invalid return in class/module body");
4848 }
4849 ;
4850
4851k_yield : keyword_yield
4852 {
4853 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4854 yyerror1(&@1, "Invalid yield");
4855 }
4856 ;
4857
4858then : term
4859 | keyword_then
4860 | term keyword_then
4861 ;
4862
4863do : term
4864 | keyword_do_cond { $$ = keyword_do_cond; }
4865 ;
4866
4867if_tail : opt_else
4868 | k_elsif expr_value then
4869 compstmt(stmts)
4870 if_tail
4871 {
4872 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4873 fixpos($$, $2);
4874 /*% ripper: elsif!($:2, $:4, $:5) %*/
4875 }
4876 ;
4877
4878opt_else : none
4879 | k_else compstmt(stmts)
4880 {
4881 $$ = $2;
4882 /*% ripper: else!($:2) %*/
4883 }
4884 ;
4885
4886for_var : lhs
4887 | mlhs
4888 ;
4889
4890f_marg : f_norm_arg
4891 {
4892 $$ = assignable(p, $1, 0, &@$);
4893 mark_lvar_used(p, $$);
4894 }
4895 | tLPAREN f_margs rparen
4896 {
4897 $$ = (NODE *)$2;
4898 /*% ripper: mlhs_paren!($:2) %*/
4899 }
4900 ;
4901
4902
4903f_margs : mlhs(f_marg)
4904 {
4905 $$ = NEW_MASGN($1, 0, &@$);
4906 /*% ripper: $:1 %*/
4907 }
4908 | mlhs(f_marg) ',' f_rest_marg
4909 {
4910 $$ = NEW_MASGN($1, $3, &@$);
4911 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4912 }
4913 | mlhs(f_marg) ',' f_rest_marg ',' mlhs(f_marg)
4914 {
4915 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4916 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4917 }
4918 | f_rest_marg
4919 {
4920 $$ = NEW_MASGN(0, $1, &@$);
4921 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4922 }
4923 | f_rest_marg ',' mlhs(f_marg)
4924 {
4925 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4926 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4927 }
4928 ;
4929
4930f_rest_marg : tSTAR f_norm_arg
4931 {
4932 /*% ripper: $:2 %*/
4933 $$ = assignable(p, $2, 0, &@$);
4934 mark_lvar_used(p, $$);
4935 }
4936 | tSTAR
4937 {
4938 $$ = NODE_SPECIAL_NO_NAME_REST;
4939 /*% ripper: Qnil %*/
4940 }
4941 ;
4942
4943f_any_kwrest : f_kwrest
4944 | f_no_kwarg
4945 {
4946 $$ = idNil;
4947 /*% ripper: ID2VAL(idNil) %*/
4948 }
4949 ;
4950
4951f_eq : {p->ctxt.in_argdef = 0;} '=';
4952
4953block_args_tail : args_tail_basic(primary_value)
4954 ;
4955
4956excessed_comma : ','
4957 {
4958 /* magic number for rest_id in iseq_set_arguments() */
4959 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4960 /*% ripper: excessed_comma! %*/
4961 }
4962 ;
4963
4964block_param : f_arg ',' f_optarg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
4965 {
4966 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
4967 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
4968 }
4969 | f_arg ',' f_optarg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
4970 {
4971 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4972 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
4973 }
4974 | f_arg ',' f_optarg(primary_value) opt_args_tail(block_args_tail)
4975 {
4976 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
4977 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
4978 }
4979 | f_arg ',' f_optarg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
4980 {
4981 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
4982 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
4983 }
4984 | f_arg ',' f_rest_arg opt_args_tail(block_args_tail)
4985 {
4986 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
4987 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
4988 }
4989 | f_arg excessed_comma
4990 {
4991 $$ = new_args_tail(p, 0, 0, 0, &@2);
4992 $$ = new_args(p, $1, 0, $2, 0, $$, &@$);
4993 /*% ripper: params!($:1, Qnil, $:2, Qnil, Qnil, Qnil, Qnil) %*/
4994 }
4995 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
4996 {
4997 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
4998 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
4999 }
5000 | f_arg opt_args_tail(block_args_tail)
5001 {
5002 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
5003 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
5004 }
5005 | f_optarg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5006 {
5007 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
5008 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
5009 }
5010 | f_optarg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5011 {
5012 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
5013 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
5014 }
5015 | f_optarg(primary_value) opt_args_tail(block_args_tail)
5016 {
5017 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
5018 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
5019 }
5020 | f_optarg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5021 {
5022 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
5023 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
5024 }
5025 | f_rest_arg opt_args_tail(block_args_tail)
5026 {
5027 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
5028 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
5029 }
5030 | f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5031 {
5032 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
5033 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
5034 }
5035 | block_args_tail
5036 {
5037 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
5038 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
5039 }
5040 ;
5041
5042opt_block_param_def : none
5043 | block_param_def
5044 {
5045 p->command_start = TRUE;
5046 }
5047 ;
5048
5049block_param_def : '|' opt_block_param opt_bv_decl '|'
5050 {
5051 p->max_numparam = ORDINAL_PARAM;
5052 p->ctxt.in_argdef = 0;
5053 $$ = $2;
5054 /*% ripper: block_var!($:2, $:3) %*/
5055 }
5056 ;
5057
5058opt_block_param : /* none */
5059 {
5060 $$ = 0;
5061 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5062 }
5063 | block_param
5064 ;
5065
5066opt_bv_decl : '\n'?
5067 {
5068 $$ = 0;
5069 /*% ripper: Qfalse %*/
5070 }
5071 | '\n'? ';' bv_decls '\n'?
5072 {
5073 $$ = 0;
5074 /*% ripper: $:3 %*/
5075 }
5076 ;
5077
5078bv_decls : bvar
5079 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5080 | bv_decls ',' bvar
5081 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5082 ;
5083
5084bvar : tIDENTIFIER
5085 {
5086 new_bv(p, $1);
5087 /*% ripper: $:1 %*/
5088 }
5089 | f_bad_arg
5090 ;
5091
5092max_numparam : {
5093 $$ = p->max_numparam;
5094 p->max_numparam = 0;
5095 }
5096 ;
5097
5098numparam : {
5099 $$ = numparam_push(p);
5100 }
5101 ;
5102
5103it_id : {
5104 $$ = p->it_id;
5105 p->it_id = 0;
5106 }
5107 ;
5108
5109lambda : tLAMBDA[lpar]
5110 {
5111 token_info_push(p, "->", &@1);
5112 $$ = dyna_push(p);
5113 }[dyna]<vars>
5114 max_numparam numparam it_id allow_exits
5115 f_larglist[args]
5116 {
5117 CMDARG_PUSH(0);
5118 }
5119 lambda_body[body]
5120 {
5121 int max_numparam = p->max_numparam;
5122 ID it_id = p->it_id;
5123 p->lex.lpar_beg = $lpar;
5124 p->max_numparam = $max_numparam;
5125 p->it_id = $it_id;
5126 restore_block_exit(p, $allow_exits);
5127 CMDARG_POP();
5128 $args = args_with_numbered(p, $args, max_numparam, it_id);
5129 {
5130 YYLTYPE loc = code_loc_gen(&@args, &@body);
5131 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5132 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5133 nd_set_line($$, @args.end_pos.lineno);
5134 nd_set_first_loc($$, @1.beg_pos);
5135 xfree($body);
5136 }
5137 /*% ripper: lambda!($:args, $:body) %*/
5138 numparam_pop(p, $numparam);
5139 dyna_pop(p, $dyna);
5140 }
5141 ;
5142
5143f_larglist : '(' f_args opt_bv_decl ')'
5144 {
5145 p->ctxt.in_argdef = 0;
5146 $$ = $f_args;
5147 p->max_numparam = ORDINAL_PARAM;
5148 /*% ripper: paren!($:2) %*/
5149 }
5150 | f_args
5151 {
5152 p->ctxt.in_argdef = 0;
5153 if (!args_info_empty_p(&$1->nd_ainfo))
5154 p->max_numparam = ORDINAL_PARAM;
5155 $$ = $f_args;
5156 }
5157 ;
5158
5159lambda_body : tLAMBEG compstmt(stmts) '}'
5160 {
5161 token_info_pop(p, "}", &@3);
5162 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5163 /*% ripper: $:2 %*/
5164 }
5165 | keyword_do_LAMBDA
5166 {
5167 push_end_expect_token_locations(p, &@1.beg_pos);
5168 }
5169 bodystmt k_end
5170 {
5171 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5172 /*% ripper: $:3 %*/
5173 }
5174 ;
5175
5176do_block : k_do_block do_body k_end
5177 {
5178 $$ = $2;
5179 set_embraced_location($$, &@1, &@3);
5180 /*% ripper: $:2 %*/
5181 }
5182 ;
5183
5184block_call : command do_block
5185 {
5186 if (nd_type_p($1, NODE_YIELD)) {
5187 compile_error(p, "block given to yield");
5188 }
5189 else {
5190 block_dup_check(p, get_nd_args(p, $1), $2);
5191 }
5192 $$ = method_add_block(p, $1, $2, &@$);
5193 fixpos($$, $1);
5194 /*% ripper: method_add_block!($:1, $:2) %*/
5195 }
5196 | block_call call_op2 operation2 opt_paren_args
5197 {
5198 bool has_args = $4 != 0;
5199 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5200 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5201 /*% ripper: call!($:1, $:2, $:3) %*/
5202 if (has_args) {
5203 /*% ripper: method_add_arg!($:$, $:4) %*/
5204 }
5205 }
5206 | block_call call_op2 operation2 opt_paren_args brace_block
5207 {
5208 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5209 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5210 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
5211 if ($5) {
5212 /*% ripper: method_add_block!($:$, $:5) %*/
5213 }
5214 }
5215 | block_call call_op2 operation2 command_args do_block
5216 {
5217 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5218 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5219 }
5220 ;
5221
5222method_call : fcall paren_args
5223 {
5224 $1->nd_args = $2;
5225 $$ = (NODE *)$1;
5226 nd_set_last_loc($1, @2.end_pos);
5227 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5228 }
5229 | primary_value call_op operation2 opt_paren_args
5230 {
5231 bool has_args = $4 != 0;
5232 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5233 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5234 nd_set_line($$, @3.end_pos.lineno);
5235 /*% ripper: call!($:1, $:2, $:3) %*/
5236 if (has_args) {
5237 /*% ripper: method_add_arg!($:$, $:4) %*/
5238 }
5239 }
5240 | primary_value tCOLON2 operation2 paren_args
5241 {
5242 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5243 nd_set_line($$, @3.end_pos.lineno);
5244 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5245 }
5246 | primary_value tCOLON2 operation3
5247 {
5248 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5249 /*% ripper: call!($:1, $:2, $:3) %*/
5250 }
5251 | primary_value call_op paren_args
5252 {
5253 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5254 nd_set_line($$, @2.end_pos.lineno);
5255 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5256 }
5257 | primary_value tCOLON2 paren_args
5258 {
5259 $$ = new_qcall(p, idCOLON2, $1, idCall, $3, &@2, &@$);
5260 nd_set_line($$, @2.end_pos.lineno);
5261 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5262 }
5263 | keyword_super paren_args
5264 {
5265 rb_code_location_t lparen_loc = @2;
5266 rb_code_location_t rparen_loc = @2;
5267 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5268 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5269
5270 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5271 /*% ripper: super!($:2) %*/
5272 }
5273 | keyword_super
5274 {
5275 $$ = NEW_ZSUPER(&@$);
5276 /*% ripper: zsuper! %*/
5277 }
5278 | primary_value '[' opt_call_args rbracket
5279 {
5280 $$ = NEW_CALL($1, tAREF, $3, &@$);
5281 fixpos($$, $1);
5282 /*% ripper: aref!($:1, $:3) %*/
5283 }
5284 ;
5285
5286brace_block : '{' brace_body '}'
5287 {
5288 $$ = $2;
5289 set_embraced_location($$, &@1, &@3);
5290 /*% ripper: $:2 %*/
5291 }
5292 | k_do do_body k_end
5293 {
5294 $$ = $2;
5295 set_embraced_location($$, &@1, &@3);
5296 /*% ripper: $:2 %*/
5297 }
5298 ;
5299
5300brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5301 max_numparam numparam it_id allow_exits
5302 opt_block_param_def[args] compstmt(stmts)
5303 {
5304 int max_numparam = p->max_numparam;
5305 ID it_id = p->it_id;
5306 p->max_numparam = $max_numparam;
5307 p->it_id = $it_id;
5308 $args = args_with_numbered(p, $args, max_numparam, it_id);
5309 $$ = NEW_ITER($args, $compstmt, &@$);
5310 /*% ripper: brace_block!($:args, $:compstmt) %*/
5311 restore_block_exit(p, $allow_exits);
5312 numparam_pop(p, $numparam);
5313 dyna_pop(p, $dyna);
5314 }
5315 ;
5316
5317do_body : {
5318 $$ = dyna_push(p);
5319 CMDARG_PUSH(0);
5320 }[dyna]<vars>
5321 max_numparam numparam it_id allow_exits
5322 opt_block_param_def[args] bodystmt
5323 {
5324 int max_numparam = p->max_numparam;
5325 ID it_id = p->it_id;
5326 p->max_numparam = $max_numparam;
5327 p->it_id = $it_id;
5328 $args = args_with_numbered(p, $args, max_numparam, it_id);
5329 $$ = NEW_ITER($args, $bodystmt, &@$);
5330 /*% ripper: do_block!($:args, $:bodystmt) %*/
5331 CMDARG_POP();
5332 restore_block_exit(p, $allow_exits);
5333 numparam_pop(p, $numparam);
5334 dyna_pop(p, $dyna);
5335 }
5336 ;
5337
5338case_args : arg_value
5339 {
5340 check_literal_when(p, $arg_value, &@arg_value);
5341 $$ = NEW_LIST($arg_value, &@$);
5342 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5343 }
5344 | tSTAR arg_value
5345 {
5346 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5347 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5348 }
5349 | case_args[non_last_args] ',' arg_value
5350 {
5351 check_literal_when(p, $arg_value, &@arg_value);
5352 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5353 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5354 }
5355 | case_args[non_last_args] ',' tSTAR arg_value
5356 {
5357 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5358 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5359 }
5360 ;
5361
5362case_body : k_when case_args then
5363 compstmt(stmts)
5364 cases
5365 {
5366 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5367 fixpos($$, $2);
5368 /*% ripper: when!($:2, $:4, $:5) %*/
5369 }
5370 ;
5371
5372cases : opt_else
5373 | case_body
5374 ;
5375
5376p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5377p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5378
5379p_in_kwarg : {
5380 $$ = p->ctxt;
5381 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5382 p->command_start = FALSE;
5383 p->ctxt.in_kwarg = 1;
5384 }
5385 ;
5386
5387p_case_body : keyword_in
5388 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5389 p_top_expr[expr] then
5390 {
5391 pop_pktbl(p, $p_pktbl);
5392 pop_pvtbl(p, $p_pvtbl);
5393 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5394 }
5395 compstmt(stmts)
5396 p_cases[cases]
5397 {
5398 $$ = NEW_IN($expr, $compstmt, $cases, &@$);
5399 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5400 }
5401 ;
5402
5403p_cases : opt_else
5404 | p_case_body
5405 ;
5406
5407p_top_expr : p_top_expr_body
5408 | p_top_expr_body modifier_if expr_value
5409 {
5410 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5411 fixpos($$, $3);
5412 /*% ripper: if_mod!($:3, $:1) %*/
5413 }
5414 | p_top_expr_body modifier_unless expr_value
5415 {
5416 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5417 fixpos($$, $3);
5418 /*% ripper: unless_mod!($:3, $:1) %*/
5419 }
5420 ;
5421
5422p_top_expr_body : p_expr
5423 | p_expr ','
5424 {
5425 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5426 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5427 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5428 }
5429 | p_expr ',' p_args
5430 {
5431 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5432 nd_set_first_loc($$, @1.beg_pos);
5433 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5434 }
5435 | p_find
5436 {
5437 $$ = new_find_pattern(p, 0, $1, &@$);
5438 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5439 }
5440 | p_args_tail
5441 {
5442 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5443 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5444 }
5445 | p_kwargs
5446 {
5447 $$ = new_hash_pattern(p, 0, $1, &@$);
5448 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5449 }
5450 ;
5451
5452p_expr : p_as
5453 ;
5454
5455p_as : p_expr tASSOC p_variable
5456 {
5457 NODE *n = NEW_LIST($1, &@$);
5458 n = list_append(p, n, $3);
5459 $$ = new_hash(p, n, &@$);
5460 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5461 }
5462 | p_alt
5463 ;
5464
5465p_alt : p_alt '|' p_expr_basic
5466 {
5467 $$ = NEW_OR($1, $3, &@$, &@2);
5468 /*% ripper: binary!($:1, ID2VAL(idOr), $:3) %*/
5469 }
5470 | p_expr_basic
5471 ;
5472
5473p_lparen : '(' p_pktbl
5474 {
5475 $$ = $2;
5476 /*% ripper: $:2 %*/
5477 }
5478 ;
5479
5480p_lbracket : '[' p_pktbl
5481 {
5482 $$ = $2;
5483 /*% ripper: $:2 %*/
5484 }
5485 ;
5486
5487p_expr_basic : p_value
5488 | p_variable
5489 | p_const p_lparen[p_pktbl] p_args rparen
5490 {
5491 pop_pktbl(p, $p_pktbl);
5492 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5493 nd_set_first_loc($$, @p_const.beg_pos);
5494 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5495 }
5496 | p_const p_lparen[p_pktbl] p_find rparen
5497 {
5498 pop_pktbl(p, $p_pktbl);
5499 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5500 nd_set_first_loc($$, @p_const.beg_pos);
5501 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5502 }
5503 | p_const p_lparen[p_pktbl] p_kwargs rparen
5504 {
5505 pop_pktbl(p, $p_pktbl);
5506 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5507 nd_set_first_loc($$, @p_const.beg_pos);
5508 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5509 }
5510 | p_const '(' rparen
5511 {
5512 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5513 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5514 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5515 }
5516 | p_const p_lbracket[p_pktbl] p_args rbracket
5517 {
5518 pop_pktbl(p, $p_pktbl);
5519 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5520 nd_set_first_loc($$, @p_const.beg_pos);
5521 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5522 }
5523 | p_const p_lbracket[p_pktbl] p_find rbracket
5524 {
5525 pop_pktbl(p, $p_pktbl);
5526 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5527 nd_set_first_loc($$, @p_const.beg_pos);
5528 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5529 }
5530 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5531 {
5532 pop_pktbl(p, $p_pktbl);
5533 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5534 nd_set_first_loc($$, @p_const.beg_pos);
5535 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5536 }
5537 | p_const '[' rbracket
5538 {
5539 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5540 $$ = new_array_pattern(p, $1, 0, $$, &@$);
5541 /*% ripper: aryptn!($:1, Qnil, Qnil, Qnil) %*/
5542 }
5543 | tLBRACK p_args rbracket
5544 {
5545 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5546 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5547 }
5548 | tLBRACK p_find rbracket
5549 {
5550 $$ = new_find_pattern(p, 0, $p_find, &@$);
5551 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5552 }
5553 | tLBRACK rbracket
5554 {
5555 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5556 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5557 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5558 }
5559 | tLBRACE p_pktbl lex_ctxt[ctxt]
5560 {
5561 p->ctxt.in_kwarg = 0;
5562 }
5563 p_kwargs rbrace
5564 {
5565 pop_pktbl(p, $p_pktbl);
5566 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5567 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5568 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5569 }
5570 | tLBRACE rbrace
5571 {
5572 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5573 $$ = new_hash_pattern(p, 0, $$, &@$);
5574 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5575 }
5576 | tLPAREN p_pktbl p_expr rparen
5577 {
5578 pop_pktbl(p, $p_pktbl);
5579 $$ = $p_expr;
5580 /*% ripper: $:p_expr %*/
5581 }
5582 ;
5583
5584p_args : p_expr
5585 {
5586 NODE *pre_args = NEW_LIST($1, &@$);
5587 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5588 /*% ripper: [[$:1], Qnil, Qnil] %*/
5589 }
5590 | p_args_head
5591 {
5592 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5593 /*% ripper: [$:1, Qnil, Qnil] %*/
5594 }
5595 | p_args_head p_arg
5596 {
5597 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5598 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5599 }
5600 | p_args_head p_rest
5601 {
5602 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5603 /*% ripper: [$:1, $:2, Qnil] %*/
5604 }
5605 | p_args_head p_rest ',' p_args_post
5606 {
5607 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5608 /*% ripper: [$:1, $:2, $:4] %*/
5609 }
5610 | p_args_tail
5611 ;
5612
5613p_args_head : p_arg ','
5614 | p_args_head p_arg ','
5615 {
5616 $$ = list_concat($1, $2);
5617 /*% ripper: rb_ary_concat($:1, $:2) %*/
5618 }
5619 ;
5620
5621p_args_tail : p_rest
5622 {
5623 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5624 /*% ripper: [Qnil, $:1, Qnil] %*/
5625 }
5626 | p_rest ',' p_args_post
5627 {
5628 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5629 /*% ripper: [Qnil, $:1, $:3] %*/
5630 }
5631 ;
5632
5633p_find : p_rest ',' p_args_post ',' p_rest
5634 {
5635 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5636 /*% ripper: [$:1, $:3, $:5] %*/
5637 }
5638 ;
5639
5640
5641p_rest : tSTAR tIDENTIFIER
5642 {
5643 error_duplicate_pattern_variable(p, $2, &@2);
5644 /*% ripper: var_field!($:2) %*/
5645 $$ = assignable(p, $2, 0, &@$);
5646 }
5647 | tSTAR
5648 {
5649 $$ = 0;
5650 /*% ripper: var_field!(Qnil) %*/
5651 }
5652 ;
5653
5654p_args_post : p_arg
5655 | p_args_post ',' p_arg
5656 {
5657 $$ = list_concat($1, $3);
5658 /*% ripper: rb_ary_concat($:1, $:3) %*/
5659 }
5660 ;
5661
5662p_arg : p_expr
5663 {
5664 $$ = NEW_LIST($1, &@$);
5665 /*% ripper: [$:1] %*/
5666 }
5667 ;
5668
5669p_kwargs : p_kwarg ',' p_any_kwrest
5670 {
5671 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5672 /*% ripper: [$:1, $:3] %*/
5673 }
5674 | p_kwarg
5675 {
5676 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5677 /*% ripper: [$:1, Qnil] %*/
5678 }
5679 | p_kwarg ','
5680 {
5681 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5682 /*% ripper: [$:1, Qnil] %*/
5683 }
5684 | p_any_kwrest
5685 {
5686 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5687 /*% ripper: [[], $:1] %*/
5688 }
5689 ;
5690
5691p_kwarg : p_kw
5692 /*% ripper[brace]: [$:1] %*/
5693 | p_kwarg ',' p_kw
5694 {
5695 $$ = list_concat($1, $3);
5696 /*% ripper: rb_ary_push($:1, $:3) %*/
5697 }
5698 ;
5699
5700p_kw : p_kw_label p_expr
5701 {
5702 error_duplicate_pattern_key(p, $1, &@1);
5703 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5704 /*% ripper: [$:1, $:2] %*/
5705 }
5706 | p_kw_label
5707 {
5708 error_duplicate_pattern_key(p, $1, &@1);
5709 if ($1 && !is_local_id($1)) {
5710 yyerror1(&@1, "key must be valid as local variables");
5711 }
5712 error_duplicate_pattern_variable(p, $1, &@1);
5713 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5714 /*% ripper: [$:1, Qnil] %*/
5715 }
5716 ;
5717
5718p_kw_label : tLABEL
5719 | tSTRING_BEG string_contents tLABEL_END
5720 {
5721 YYLTYPE loc = code_loc_gen(&@1, &@3);
5722 if (!$2 || nd_type_p($2, NODE_STR)) {
5723 NODE *node = dsym_node(p, $2, &loc);
5724 $$ = rb_sym2id(rb_node_sym_string_val(node));
5725 }
5726 else {
5727 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5728 $$ = rb_intern_str(STR_NEW0());
5729 }
5730 /*% ripper: $:2 %*/
5731 }
5732 ;
5733
5734p_kwrest : kwrest_mark tIDENTIFIER
5735 {
5736 $$ = $2;
5737 /*% ripper: var_field!($:2) %*/
5738 }
5739 | kwrest_mark
5740 {
5741 $$ = 0;
5742 /*% ripper: Qnil %*/
5743 }
5744 ;
5745
5746p_kwnorest : kwrest_mark keyword_nil
5747 {
5748 $$ = 0;
5749 }
5750 ;
5751
5752p_any_kwrest : p_kwrest
5753 | p_kwnorest
5754 {
5755 $$ = idNil;
5756 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5757 }
5758 ;
5759
5760p_value : p_primitive
5761 | p_primitive_value tDOT2 p_primitive_value
5762 {
5763 $$ = NEW_DOT2($1, $3, &@$, &@2);
5764 /*% ripper: dot2!($:1, $:3) %*/
5765 }
5766 | p_primitive_value tDOT3 p_primitive_value
5767 {
5768 $$ = NEW_DOT3($1, $3, &@$, &@2);
5769 /*% ripper: dot3!($:1, $:3) %*/
5770 }
5771 | p_primitive_value tDOT2
5772 {
5773 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
5774 /*% ripper: dot2!($:1, Qnil) %*/
5775 }
5776 | p_primitive_value tDOT3
5777 {
5778 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
5779 /*% ripper: dot3!($:1, Qnil) %*/
5780 }
5781 | p_var_ref
5782 | p_expr_ref
5783 | p_const
5784 | tBDOT2 p_primitive_value
5785 {
5786 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
5787 /*% ripper: dot2!(Qnil, $:2) %*/
5788 }
5789 | tBDOT3 p_primitive_value
5790 {
5791 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
5792 /*% ripper: dot3!(Qnil, $:2) %*/
5793 }
5794 ;
5795
5796p_primitive : inline_primary
5797 | keyword_variable
5798 {
5799 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5800 /*% ripper: var_ref!($:1) %*/
5801 }
5802 | lambda
5803 ;
5804
5805p_primitive_value : value_expr(p_primitive)
5806 ;
5807
5808p_variable : tIDENTIFIER
5809 {
5810 error_duplicate_pattern_variable(p, $1, &@1);
5811 /*% ripper: var_field!($:1) %*/
5812 $$ = assignable(p, $1, 0, &@$);
5813 }
5814 ;
5815
5816p_var_ref : '^' tIDENTIFIER
5817 {
5818 NODE *n = gettable(p, $2, &@$);
5819 if (!n) {
5820 n = NEW_ERROR(&@$);
5821 }
5822 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5823 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5824 }
5825 $$ = n;
5826 /*% ripper: var_ref!($:2) %*/
5827 }
5828 | '^' nonlocal_var
5829 {
5830 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5831 /*% ripper: var_ref!($:2) %*/
5832 }
5833 ;
5834
5835p_expr_ref : '^' tLPAREN expr_value rparen
5836 {
5837 $$ = NEW_BLOCK($3, &@$);
5838 /*% ripper: begin!($:3) %*/
5839 }
5840 ;
5841
5842p_const : tCOLON3 cname
5843 {
5844 $$ = NEW_COLON3($2, &@$);
5845 /*% ripper: top_const_ref!($:2) %*/
5846 }
5847 | p_const tCOLON2 cname
5848 {
5849 $$ = NEW_COLON2($1, $3, &@$);
5850 /*% ripper: const_path_ref!($:1, $:3) %*/
5851 }
5852 | tCONSTANT
5853 {
5854 $$ = gettable(p, $1, &@$);
5855 /*% ripper: var_ref!($:1) %*/
5856 }
5857 ;
5858
5859opt_rescue : k_rescue exc_list exc_var then
5860 compstmt(stmts)
5861 opt_rescue
5862 {
5863 NODE *err = $3;
5864 if ($3) {
5865 err = NEW_ERRINFO(&@3);
5866 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5867 }
5868 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5869 if ($2) {
5870 fixpos($$, $2);
5871 }
5872 else if ($3) {
5873 fixpos($$, $3);
5874 }
5875 else {
5876 fixpos($$, $5);
5877 }
5878 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5879 }
5880 | none
5881 ;
5882
5883exc_list : arg_value
5884 {
5885 $$ = NEW_LIST($1, &@$);
5886 /*% ripper: rb_ary_new3(1, $:1) %*/
5887 }
5888 | mrhs
5889 {
5890 if (!($$ = splat_array($1))) $$ = $1;
5891 }
5892 | none
5893 ;
5894
5895exc_var : tASSOC lhs
5896 {
5897 $$ = $2;
5898 /*% ripper: $:2 %*/
5899 }
5900 | none
5901 ;
5902
5903opt_ensure : k_ensure stmts terms?
5904 {
5905 p->ctxt.in_rescue = $1.in_rescue;
5906 $$ = $2;
5907 void_expr(p, void_stmts(p, $$));
5908 /*% ripper: ensure!($:2) %*/
5909 }
5910 | none
5911 ;
5912
5913literal : numeric
5914 | symbol
5915 ;
5916
5917strings : string
5918 {
5919 if (!$1) {
5920 $$ = NEW_STR(STRING_NEW0(), &@$);
5921 } else {
5922 $$ = evstr2dstr(p, $1);
5923 }
5924 /*% ripper: $:1 %*/
5925 }
5926 ;
5927
5928string : tCHAR
5929 | string1
5930 | string string1
5931 {
5932 $$ = literal_concat(p, $1, $2, &@$);
5933 /*% ripper: string_concat!($:1, $:2) %*/
5934 }
5935 ;
5936
5937string1 : tSTRING_BEG string_contents tSTRING_END
5938 {
5939 $$ = heredoc_dedent(p, $2);
5940 if ($$) nd_set_loc($$, &@$);
5941 /*% ripper: $:2 %*/
5942 if (p->heredoc_indent > 0) {
5943 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5944 p->heredoc_indent = 0;
5945 }
5946 /*% ripper: string_literal!($:$) %*/
5947 }
5948 ;
5949
5950xstring : tXSTRING_BEG xstring_contents tSTRING_END
5951 {
5952 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5953 /*% ripper: $:2 %*/
5954 if (p->heredoc_indent > 0) {
5955 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5956 p->heredoc_indent = 0;
5957 }
5958 /*% ripper: xstring_literal!($:$) %*/
5959 }
5960 ;
5961
5962regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5963 {
5964 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5965 /*% ripper: regexp_literal!($:2, $:3) %*/
5966 }
5967 ;
5968
5969words : words(tWORDS_BEG, word_list)
5970 ;
5971
5972word_list : /* none */
5973 {
5974 $$ = 0;
5975 /*% ripper: words_new! %*/
5976 }
5977 | word_list word ' '+
5978 {
5979 $$ = list_append(p, $1, evstr2dstr(p, $2));
5980 /*% ripper: words_add!($:1, $:2) %*/
5981 }
5982 ;
5983
5984word : string_content
5985 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5986 | word string_content
5987 {
5988 $$ = literal_concat(p, $1, $2, &@$);
5989 /*% ripper: word_add!($:1, $:2) %*/
5990 }
5991 ;
5992
5993symbols : words(tSYMBOLS_BEG, symbol_list)
5994 ;
5995
5996symbol_list : /* none */
5997 {
5998 $$ = 0;
5999 /*% ripper: symbols_new! %*/
6000 }
6001 | symbol_list word ' '+
6002 {
6003 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
6004 /*% ripper: symbols_add!($:1, $:2) %*/
6005 }
6006 ;
6007
6008qwords : words(tQWORDS_BEG, qword_list)
6009 ;
6010
6011qsymbols : words(tQSYMBOLS_BEG, qsym_list)
6012 ;
6013
6014qword_list : /* none */
6015 {
6016 $$ = 0;
6017 /*% ripper: qwords_new! %*/
6018 }
6019 | qword_list tSTRING_CONTENT ' '+
6020 {
6021 $$ = list_append(p, $1, $2);
6022 /*% ripper: qwords_add!($:1, $:2) %*/
6023 }
6024 ;
6025
6026qsym_list : /* none */
6027 {
6028 $$ = 0;
6029 /*% ripper: qsymbols_new! %*/
6030 }
6031 | qsym_list tSTRING_CONTENT ' '+
6032 {
6033 $$ = symbol_append(p, $1, $2);
6034 /*% ripper: qsymbols_add!($:1, $:2) %*/
6035 }
6036 ;
6037
6038string_contents : /* none */
6039 {
6040 $$ = 0;
6041 /*% ripper: string_content! %*/
6042 }
6043 | string_contents string_content
6044 {
6045 $$ = literal_concat(p, $1, $2, &@$);
6046 /*% ripper: string_add!($:1, $:2) %*/
6047 }
6048 ;
6049
6050xstring_contents: /* none */
6051 {
6052 $$ = 0;
6053 /*% ripper: xstring_new! %*/
6054 }
6055 | xstring_contents string_content
6056 {
6057 $$ = literal_concat(p, $1, $2, &@$);
6058 /*% ripper: xstring_add!($:1, $:2) %*/
6059 }
6060 ;
6061
6062regexp_contents: /* none */
6063 {
6064 $$ = 0;
6065 /*% ripper: regexp_new! %*/
6066 }
6067 | regexp_contents string_content
6068 {
6069 NODE *head = $1, *tail = $2;
6070 if (!head) {
6071 $$ = tail;
6072 }
6073 else if (!tail) {
6074 $$ = head;
6075 }
6076 else {
6077 switch (nd_type(head)) {
6078 case NODE_STR:
6079 head = str2dstr(p, head);
6080 break;
6081 case NODE_DSTR:
6082 break;
6083 default:
6084 head = list_append(p, NEW_DSTR(0, &@$), head);
6085 break;
6086 }
6087 $$ = list_append(p, head, tail);
6088 }
6089 /*% ripper: regexp_add!($:1, $:2) %*/
6090 }
6091 ;
6092
6093string_content : tSTRING_CONTENT
6094 /*% ripper[brace]: $:1 %*/
6095 | tSTRING_DVAR
6096 {
6097 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6098 $$ = p->lex.strterm;
6099 p->lex.strterm = 0;
6100 SET_LEX_STATE(EXPR_BEG);
6101 }<strterm>
6102 string_dvar
6103 {
6104 p->lex.strterm = $2;
6105 $$ = NEW_EVSTR($3, &@$, &@1, &NULL_LOC);
6106 nd_set_line($$, @3.end_pos.lineno);
6107 /*% ripper: string_dvar!($:3) %*/
6108 }
6109 | tSTRING_DBEG[state]
6110 {
6111 CMDARG_PUSH(0);
6112 COND_PUSH(0);
6113 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6114 $$ = p->lex.strterm;
6115 p->lex.strterm = 0;
6116 SET_LEX_STATE(EXPR_BEG);
6117 }[term]<strterm>
6118 {
6119 $$ = p->lex.brace_nest;
6120 p->lex.brace_nest = 0;
6121 }[brace]<num>
6122 {
6123 $$ = p->heredoc_indent;
6124 p->heredoc_indent = 0;
6125 }[indent]<num>
6126 compstmt(stmts) string_dend
6127 {
6128 COND_POP();
6129 CMDARG_POP();
6130 p->lex.strterm = $term;
6131 SET_LEX_STATE($state);
6132 p->lex.brace_nest = $brace;
6133 p->heredoc_indent = $indent;
6134 p->heredoc_line_indent = -1;
6135 if ($compstmt) nd_unset_fl_newline($compstmt);
6136 $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend);
6137 /*% ripper: string_embexpr!($:compstmt) %*/
6138 }
6139 ;
6140
6141string_dend : tSTRING_DEND
6142 | END_OF_INPUT
6143 ;
6144
6145string_dvar : nonlocal_var
6146 {
6147 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6148 /*% ripper: var_ref!($:1) %*/
6149 }
6150 | backref
6151 ;
6152
6153symbol : ssym
6154 | dsym
6155 ;
6156
6157ssym : tSYMBEG sym
6158 {
6159 SET_LEX_STATE(EXPR_END);
6160 VALUE str = rb_id2str($2);
6161 /*
6162 * TODO:
6163 * set_yylval_noname sets invalid id to yylval.
6164 * This branch can be removed once yylval is changed to
6165 * hold lexed string.
6166 */
6167 if (!str) str = STR_NEW0();
6168 $$ = NEW_SYM(str, &@$);
6169 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6170 }
6171 ;
6172
6173sym : fname
6174 | nonlocal_var
6175 ;
6176
6177dsym : tSYMBEG string_contents tSTRING_END
6178 {
6179 SET_LEX_STATE(EXPR_END);
6180 $$ = dsym_node(p, $2, &@$);
6181 /*% ripper: dyna_symbol!($:2) %*/
6182 }
6183 ;
6184
6185numeric : simple_numeric
6186 | tUMINUS_NUM simple_numeric %prec tLOWEST
6187 {
6188 $$ = $2;
6189 negate_lit(p, $$);
6190 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6191 }
6192 ;
6193
6194simple_numeric : tINTEGER
6195 | tFLOAT
6196 | tRATIONAL
6197 | tIMAGINARY
6198 ;
6199
6200nonlocal_var : tIVAR
6201 | tGVAR
6202 | tCVAR
6203 ;
6204
6205user_variable : ident_or_const
6206 | nonlocal_var
6207 ;
6208
6209keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6210 | keyword_self {$$ = KWD2EID(self, $1);}
6211 | keyword_true {$$ = KWD2EID(true, $1);}
6212 | keyword_false {$$ = KWD2EID(false, $1);}
6213 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6214 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6215 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6216 ;
6217
6218var_ref : user_variable
6219 {
6220 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6221 if (ifdef_ripper(id_is_var(p, $1), false)) {
6222 /*% ripper: var_ref!($:1) %*/
6223 }
6224 else {
6225 /*% ripper: vcall!($:1) %*/
6226 }
6227 }
6228 | keyword_variable
6229 {
6230 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6231 /*% ripper: var_ref!($:1) %*/
6232 }
6233 ;
6234
6235var_lhs : user_or_keyword_variable
6236 {
6237 /*% ripper: var_field!($:1) %*/
6238 $$ = assignable(p, $1, 0, &@$);
6239 }
6240 ;
6241
6242backref : tNTH_REF
6243 | tBACK_REF
6244 ;
6245
6246superclass : '<'
6247 {
6248 SET_LEX_STATE(EXPR_BEG);
6249 p->command_start = TRUE;
6250 }
6251 expr_value term
6252 {
6253 $$ = $3;
6254 /*% ripper: $:3 %*/
6255 }
6256 | none
6257 ;
6258
6259f_opt_paren_args: f_paren_args
6260 | none
6261 {
6262 p->ctxt.in_argdef = 0;
6263 $$ = new_args_tail(p, 0, 0, 0, &@0);
6264 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6265 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6266 }
6267 ;
6268
6269f_paren_args : '(' f_args rparen
6270 {
6271 $$ = $2;
6272 /*% ripper: paren!($:2) %*/
6273 SET_LEX_STATE(EXPR_BEG);
6274 p->command_start = TRUE;
6275 p->ctxt.in_argdef = 0;
6276 }
6277 ;
6278
6279f_arglist : f_paren_args
6280 | {
6281 $$ = p->ctxt;
6282 p->ctxt.in_kwarg = 1;
6283 p->ctxt.in_argdef = 1;
6284 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6285 }<ctxt>
6286 f_args term
6287 {
6288 p->ctxt.in_kwarg = $1.in_kwarg;
6289 p->ctxt.in_argdef = 0;
6290 $$ = $2;
6291 SET_LEX_STATE(EXPR_BEG);
6292 p->command_start = TRUE;
6293 /*% ripper: $:2 %*/
6294 }
6295 ;
6296
6297args_tail : args_tail_basic(arg_value)
6298 | args_forward
6299 {
6300 ID fwd = $args_forward;
6301 if (lambda_beginning_p() ||
6302 (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest)) {
6303 yyerror0("unexpected ... in lambda argument");
6304 fwd = 0;
6305 }
6306 else {
6307 add_forwarding_args(p);
6308 }
6309 $$ = new_args_tail(p, 0, fwd, arg_FWD_BLOCK, &@1);
6310 $$->nd_ainfo.forwarding = 1;
6311 /*% ripper: [Qnil, $:1, Qnil] %*/
6312 }
6313 ;
6314
6315f_args : f_arg ',' f_optarg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6316 {
6317 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
6318 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
6319 }
6320 | f_arg ',' f_optarg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6321 {
6322 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6323 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
6324 }
6325 | f_arg ',' f_optarg(arg_value) opt_args_tail(args_tail)
6326 {
6327 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
6328 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
6329 }
6330 | f_arg ',' f_optarg(arg_value) ',' f_arg opt_args_tail(args_tail)
6331 {
6332 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
6333 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
6334 }
6335 | f_arg ',' f_rest_arg opt_args_tail(args_tail)
6336 {
6337 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
6338 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
6339 }
6340 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6341 {
6342 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
6343 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
6344 }
6345 | f_arg opt_args_tail(args_tail)
6346 {
6347 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
6348 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
6349 }
6350 | f_optarg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6351 {
6352 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
6353 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
6354 }
6355 | f_optarg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6356 {
6357 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
6358 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
6359 }
6360 | f_optarg(arg_value) opt_args_tail(args_tail)
6361 {
6362 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
6363 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
6364 }
6365 | f_optarg(arg_value) ',' f_arg opt_args_tail(args_tail)
6366 {
6367 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
6368 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
6369 }
6370 | f_rest_arg opt_args_tail(args_tail)
6371 {
6372 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
6373 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
6374 }
6375 | f_rest_arg ',' f_arg opt_args_tail(args_tail)
6376 {
6377 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
6378 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
6379 }
6380 | args_tail
6381 {
6382 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
6383 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
6384 }
6385 | /* none */
6386 {
6387 $$ = new_args_tail(p, 0, 0, 0, &@0);
6388 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6389 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6390 }
6391 ;
6392
6393args_forward : tBDOT3
6394 {
6395#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
6396 $$ = 0;
6397#else
6398 $$ = idFWD_KWREST;
6399#endif
6400 /*% ripper: args_forward! %*/
6401 }
6402 ;
6403
6404f_bad_arg : tCONSTANT
6405 {
6406 static const char mesg[] = "formal argument cannot be a constant";
6407 /*%%%*/
6408 yyerror1(&@1, mesg);
6409 /*% %*/
6410 $$ = 0;
6411 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6412 }
6413 | tIVAR
6414 {
6415 static const char mesg[] = "formal argument cannot be an instance variable";
6416 /*%%%*/
6417 yyerror1(&@1, mesg);
6418 /*% %*/
6419 $$ = 0;
6420 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6421 }
6422 | tGVAR
6423 {
6424 static const char mesg[] = "formal argument cannot be a global variable";
6425 /*%%%*/
6426 yyerror1(&@1, mesg);
6427 /*% %*/
6428 $$ = 0;
6429 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6430 }
6431 | tCVAR
6432 {
6433 static const char mesg[] = "formal argument cannot be a class variable";
6434 /*%%%*/
6435 yyerror1(&@1, mesg);
6436 /*% %*/
6437 $$ = 0;
6438 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6439 }
6440 ;
6441
6442f_norm_arg : f_bad_arg
6443 | tIDENTIFIER
6444 {
6445 VALUE e = formal_argument_error(p, $$ = $1);
6446 if (e) {
6447 /*% ripper[error]: param_error!(?e, $:1) %*/
6448 }
6449 p->max_numparam = ORDINAL_PARAM;
6450 }
6451 ;
6452
6453f_arg_asgn : f_norm_arg
6454 {
6455 arg_var(p, $1);
6456 $$ = $1;
6457 }
6458 ;
6459
6460f_arg_item : f_arg_asgn
6461 {
6462 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6463 /*% ripper: $:1 %*/
6464 }
6465 | tLPAREN f_margs rparen
6466 {
6467 ID tid = internal_id(p);
6468 YYLTYPE loc;
6469 loc.beg_pos = @2.beg_pos;
6470 loc.end_pos = @2.beg_pos;
6471 arg_var(p, tid);
6472 if (dyna_in_block(p)) {
6473 $2->nd_value = NEW_DVAR(tid, &loc);
6474 }
6475 else {
6476 $2->nd_value = NEW_LVAR(tid, &loc);
6477 }
6478 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6479 $$->nd_next = (NODE *)$2;
6480 /*% ripper: mlhs_paren!($:2) %*/
6481 }
6482 ;
6483
6484f_arg : f_arg_item
6485 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6486 | f_arg ',' f_arg_item
6487 {
6488 $$ = $1;
6489 $$->nd_plen++;
6490 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6491 rb_discard_node(p, (NODE *)$3);
6492 /*% ripper: rb_ary_push($:1, $:3) %*/
6493 }
6494 ;
6495
6496
6497f_label : tLABEL
6498 {
6499 VALUE e = formal_argument_error(p, $$ = $1);
6500 if (e) {
6501 $$ = 0;
6502 /*% ripper[error]: param_error!(?e, $:1) %*/
6503 }
6504 /*
6505 * Workaround for Prism::ParseTest#test_filepath for
6506 * "unparser/corpus/literal/def.txt"
6507 *
6508 * See the discussion on https://github.com/ruby/ruby/pull/9923
6509 */
6510 arg_var(p, ifdef_ripper(0, $1));
6511 /*% ripper: $:1 %*/
6512 p->max_numparam = ORDINAL_PARAM;
6513 p->ctxt.in_argdef = 0;
6514 }
6515 ;
6516
6517kwrest_mark : tPOW
6518 | tDSTAR
6519 ;
6520
6521f_no_kwarg : p_kwnorest
6522 {
6523 /*% ripper: nokw_param!(Qnil) %*/
6524 }
6525 ;
6526
6527f_kwrest : kwrest_mark tIDENTIFIER
6528 {
6529 arg_var(p, shadowing_lvar(p, $2));
6530 $$ = $2;
6531 /*% ripper: kwrest_param!($:2) %*/
6532 }
6533 | kwrest_mark
6534 {
6535 arg_var(p, idFWD_KWREST);
6536 $$ = idFWD_KWREST;
6537 /*% ripper: kwrest_param!(Qnil) %*/
6538 }
6539 ;
6540
6541restarg_mark : '*'
6542 | tSTAR
6543 ;
6544
6545f_rest_arg : restarg_mark tIDENTIFIER
6546 {
6547 arg_var(p, shadowing_lvar(p, $2));
6548 $$ = $2;
6549 /*% ripper: rest_param!($:2) %*/
6550 }
6551 | restarg_mark
6552 {
6553 arg_var(p, idFWD_REST);
6554 $$ = idFWD_REST;
6555 /*% ripper: rest_param!(Qnil) %*/
6556 }
6557 ;
6558
6559blkarg_mark : '&'
6560 | tAMPER
6561 ;
6562
6563f_block_arg : blkarg_mark tIDENTIFIER
6564 {
6565 arg_var(p, shadowing_lvar(p, $2));
6566 $$ = $2;
6567 /*% ripper: blockarg!($:2) %*/
6568 }
6569 | blkarg_mark
6570 {
6571 arg_var(p, idFWD_BLOCK);
6572 $$ = idFWD_BLOCK;
6573 /*% ripper: blockarg!(Qnil) %*/
6574 }
6575 ;
6576
6577opt_f_block_arg : ',' f_block_arg
6578 {
6579 $$ = $2;
6580 /*% ripper: $:2 %*/
6581 }
6582 | none
6583 ;
6584
6585
6586singleton : value_expr(singleton_expr)
6587 {
6588 NODE *expr = last_expr_node($1);
6589 switch (nd_type(expr)) {
6590 case NODE_STR:
6591 case NODE_DSTR:
6592 case NODE_XSTR:
6593 case NODE_DXSTR:
6594 case NODE_REGX:
6595 case NODE_DREGX:
6596 case NODE_SYM:
6597 case NODE_LINE:
6598 case NODE_FILE:
6599 case NODE_ENCODING:
6600 case NODE_INTEGER:
6601 case NODE_FLOAT:
6602 case NODE_RATIONAL:
6603 case NODE_IMAGINARY:
6604 case NODE_DSYM:
6605 case NODE_LIST:
6606 case NODE_ZLIST:
6607 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6608 break;
6609 default:
6610 break;
6611 }
6612 $$ = $1;
6613 }
6614 ;
6615
6616singleton_expr : var_ref
6617 | '('
6618 {
6619 SET_LEX_STATE(EXPR_BEG);
6620 p->ctxt.in_argdef = 0;
6621 }
6622 expr rparen
6623 {
6624 p->ctxt.in_argdef = 1;
6625 $$ = $3;
6626 /*% ripper: paren!($:3) %*/
6627 }
6628 ;
6629
6630assoc_list : none
6631 | assocs trailer
6632 {
6633 $$ = $1;
6634 /*% ripper: assoclist_from_args!($:1) %*/
6635 }
6636 ;
6637
6638assocs : assoc
6639 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6640 | assocs ',' assoc
6641 {
6642 NODE *assocs = $1;
6643 NODE *tail = $3;
6644 if (!assocs) {
6645 assocs = tail;
6646 }
6647 else if (tail) {
6648 if (RNODE_LIST(assocs)->nd_head) {
6649 NODE *n = RNODE_LIST(tail)->nd_next;
6650 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6651 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6652 /* DSTAR */
6653 tail = RNODE_HASH(n)->nd_head;
6654 }
6655 }
6656 if (tail) {
6657 assocs = list_concat(assocs, tail);
6658 }
6659 }
6660 $$ = assocs;
6661 /*% ripper: rb_ary_push($:1, $:3) %*/
6662 }
6663 ;
6664
6665assoc : arg_value tASSOC arg_value
6666 {
6667 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6668 /*% ripper: assoc_new!($:1, $:3) %*/
6669 }
6670 | tLABEL arg_value
6671 {
6672 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6673 /*% ripper: assoc_new!($:1, $:2) %*/
6674 }
6675 | tLABEL
6676 {
6677 NODE *val = gettable(p, $1, &@$);
6678 if (!val) val = NEW_ERROR(&@$);
6679 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6680 /*% ripper: assoc_new!($:1, Qnil) %*/
6681 }
6682 | tSTRING_BEG string_contents tLABEL_END arg_value
6683 {
6684 YYLTYPE loc = code_loc_gen(&@1, &@3);
6685 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6686 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6687 }
6688 | tDSTAR arg_value
6689 {
6690 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6691 /*% ripper: assoc_splat!($:2) %*/
6692 }
6693 | tDSTAR
6694 {
6695 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6696 $$ = list_append(p, NEW_LIST(0, &@$),
6697 NEW_LVAR(idFWD_KWREST, &@$));
6698 /*% ripper: assoc_splat!(Qnil) %*/
6699 }
6700 ;
6701
6702%rule %inline operation : ident_or_const
6703 | tFID
6704 ;
6705
6706operation2 : operation
6707 | op
6708 ;
6709
6710operation3 : tIDENTIFIER
6711 | tFID
6712 | op
6713 ;
6714
6715dot_or_colon : '.'
6716 | tCOLON2
6717 ;
6718
6719call_op : '.'
6720 | tANDDOT
6721 ;
6722
6723call_op2 : call_op
6724 | tCOLON2
6725 ;
6726
6727rparen : '\n'? ')'
6728 ;
6729
6730rbracket : '\n'? ']'
6731 ;
6732
6733rbrace : '\n'? '}'
6734 ;
6735
6736trailer : '\n'?
6737 | ','
6738 ;
6739
6740term : ';' {yyerrok;token_flush(p);}
6741 | '\n'
6742 {
6743 @$.end_pos = @$.beg_pos;
6744 token_flush(p);
6745 }
6746 ;
6747
6748terms : term
6749 | terms ';' {yyerrok;}
6750 ;
6751
6752none : /* none */
6753 {
6754 $$ = 0;
6755 /*% ripper: Qnil %*/
6756 }
6757 ;
6758%%
6759# undef p
6760# undef yylex
6761# undef yylval
6762# define yylval (*p->lval)
6763
6764static int regx_options(struct parser_params*);
6765static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6766static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6767static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6768static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6769
6770#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6771
6772# define set_yylval_node(x) { \
6773 YYLTYPE _cur_loc; \
6774 rb_parser_set_location(p, &_cur_loc); \
6775 yylval.node = (x); \
6776 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6777}
6778# define set_yylval_str(x) \
6779do { \
6780 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6781 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6782} while(0)
6783# define set_yylval_num(x) { \
6784 yylval.num = (x); \
6785 set_parser_s_value(x); \
6786}
6787# define set_yylval_id(x) (yylval.id = (x))
6788# define set_yylval_name(x) { \
6789 (yylval.id = (x)); \
6790 set_parser_s_value(ID2SYM(x)); \
6791}
6792# define yylval_id() (yylval.id)
6793
6794#define set_yylval_noname() set_yylval_id(keyword_nil)
6795#define has_delayed_token(p) (p->delayed.token != NULL)
6796
6797#ifndef RIPPER
6798#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6799#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6800
6801static bool
6802parser_has_token(struct parser_params *p)
6803{
6804 const char *const pcur = p->lex.pcur;
6805 const char *const ptok = p->lex.ptok;
6806 if (p->keep_tokens && (pcur < ptok)) {
6807 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6808 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6809 }
6810 return pcur > ptok;
6811}
6812
6813static const char *
6814escaped_char(int c)
6815{
6816 switch (c) {
6817 case '"': return "\\\"";
6818 case '\\': return "\\\\";
6819 case '\0': return "\\0";
6820 case '\n': return "\\n";
6821 case '\r': return "\\r";
6822 case '\t': return "\\t";
6823 case '\f': return "\\f";
6824 case '\013': return "\\v";
6825 case '\010': return "\\b";
6826 case '\007': return "\\a";
6827 case '\033': return "\\e";
6828 case '\x7f': return "\\c?";
6829 }
6830 return NULL;
6831}
6832
6833static rb_parser_string_t *
6834rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6835{
6836 rb_encoding *enc = p->enc;
6837 const char *ptr = str->ptr;
6838 const char *pend = ptr + str->len;
6839 const char *prev = ptr;
6840 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6841 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6842
6843 while (ptr < pend) {
6844 unsigned int c;
6845 const char *cc;
6846 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6847 if (!MBCLEN_CHARFOUND_P(n)) {
6848 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6849 n = rb_enc_mbminlen(enc);
6850 if (pend < ptr + n)
6851 n = (int)(pend - ptr);
6852 while (n--) {
6853 c = *ptr & 0xf0 >> 4;
6854 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6855 c = *ptr & 0x0f;
6856 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6857 parser_str_cat(result, charbuf, 4);
6858 prev = ++ptr;
6859 }
6860 continue;
6861 }
6862 n = MBCLEN_CHARFOUND_LEN(n);
6863 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6864 ptr += n;
6865 cc = escaped_char(c);
6866 if (cc) {
6867 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6868 parser_str_cat_cstr(result, cc);
6869 prev = ptr;
6870 }
6871 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6872 }
6873 else {
6874 if (ptr - n > prev) {
6875 parser_str_cat(result, prev, ptr - n - prev);
6876 prev = ptr - n;
6877 }
6878 parser_str_cat(result, prev, ptr - prev);
6879 prev = ptr;
6880 }
6881 }
6882 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6883
6884 return result;
6885}
6886
6887static void
6888parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6889{
6890 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6891 token->id = p->token_id;
6892 token->type_name = parser_token2char(p, t);
6893 token->str = str;
6894 token->loc.beg_pos = p->yylloc->beg_pos;
6895 token->loc.end_pos = p->yylloc->end_pos;
6896 rb_parser_ary_push_ast_token(p, p->tokens, token);
6897 p->token_id++;
6898
6899 if (p->debug) {
6900 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6901 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6902 line, token->id, token->type_name, str_escaped->ptr,
6903 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6904 token->loc.end_pos.lineno, token->loc.end_pos.column);
6905 rb_parser_string_free(p, str_escaped);
6906 }
6907}
6908
6909static void
6910parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6911{
6912 debug_token_line(p, "parser_dispatch_scan_event", line);
6913
6914 if (!parser_has_token(p)) return;
6915
6916 RUBY_SET_YYLLOC(*p->yylloc);
6917
6918 if (p->keep_tokens) {
6919 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6920 parser_append_tokens(p, str, t, line);
6921 }
6922
6923 token_flush(p);
6924}
6925
6926#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6927static void
6928parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6929{
6930 debug_token_line(p, "parser_dispatch_delayed_token", line);
6931
6932 if (!has_delayed_token(p)) return;
6933
6934 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6935
6936 if (p->keep_tokens) {
6937 /* p->delayed.token is freed by rb_parser_tokens_free */
6938 parser_append_tokens(p, p->delayed.token, t, line);
6939 } else {
6940 rb_parser_string_free(p, p->delayed.token);
6941 }
6942
6943 p->delayed.token = NULL;
6944}
6945#else
6946#define literal_flush(p, ptr) ((void)(ptr))
6947
6948static int
6949ripper_has_scan_event(struct parser_params *p)
6950{
6951 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6952 return p->lex.pcur > p->lex.ptok;
6953}
6954
6955static VALUE
6956ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6957{
6958 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6959 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6960 RUBY_SET_YYLLOC(*p->yylloc);
6961 token_flush(p);
6962 return rval;
6963}
6964
6965static void
6966ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6967{
6968 if (!ripper_has_scan_event(p)) return;
6969
6970 set_parser_s_value(ripper_scan_event_val(p, t));
6971}
6972#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6973
6974static void
6975ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6976{
6977 /* save and adjust the location to delayed token for callbacks */
6978 int saved_line = p->ruby_sourceline;
6979 const char *saved_tokp = p->lex.ptok;
6980 VALUE s_value, str;
6981
6982 if (!has_delayed_token(p)) return;
6983 p->ruby_sourceline = p->delayed.beg_line;
6984 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6985 str = rb_str_new_mutable_parser_string(p->delayed.token);
6986 rb_parser_string_free(p, p->delayed.token);
6987 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
6988 set_parser_s_value(s_value);
6989 p->delayed.token = NULL;
6990 p->ruby_sourceline = saved_line;
6991 p->lex.ptok = saved_tokp;
6992}
6993#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6994#endif /* RIPPER */
6995
6996static inline int
6997is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6998{
6999 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
7000}
7001
7002static inline int
7003parser_is_identchar(struct parser_params *p)
7004{
7005 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7006}
7007
7008static inline int
7009parser_isascii(struct parser_params *p)
7010{
7011 return ISASCII(*(p->lex.pcur-1));
7012}
7013
7014static void
7015token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7016{
7017 int column = 1, nonspc = 0, i;
7018 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7019 if (*ptr == '\t') {
7020 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7021 }
7022 column++;
7023 if (*ptr != ' ' && *ptr != '\t') {
7024 nonspc = 1;
7025 }
7026 }
7027
7028 ptinfo->beg = loc->beg_pos;
7029 ptinfo->indent = column;
7030 ptinfo->nonspc = nonspc;
7031}
7032
7033static void
7034token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7035{
7036 token_info *ptinfo;
7037
7038 if (!p->token_info_enabled) return;
7039 ptinfo = ALLOC(token_info);
7040 ptinfo->token = token;
7041 ptinfo->next = p->token_info;
7042 token_info_setup(ptinfo, p->lex.pbeg, loc);
7043
7044 p->token_info = ptinfo;
7045}
7046
7047static void
7048token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7049{
7050 token_info *ptinfo_beg = p->token_info;
7051
7052 if (!ptinfo_beg) return;
7053
7054 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7055 token_info_warn(p, token, ptinfo_beg, 1, loc);
7056
7057 p->token_info = ptinfo_beg->next;
7058 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7059}
7060
7061static void
7062token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7063{
7064 token_info *ptinfo_beg = p->token_info;
7065
7066 if (!ptinfo_beg) return;
7067 p->token_info = ptinfo_beg->next;
7068
7069 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7070 ptinfo_beg->beg.column != beg_pos.column ||
7071 strcmp(ptinfo_beg->token, token)) {
7072 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7073 beg_pos.lineno, beg_pos.column, token,
7074 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7075 ptinfo_beg->token);
7076 }
7077
7078 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7079}
7080
7081static void
7082token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7083{
7084 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7085 if (!p->token_info_enabled) return;
7086 if (!ptinfo_beg) return;
7087 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7088 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7089 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7090 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7091 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7092 rb_warn3L(ptinfo_end->beg.lineno,
7093 "mismatched indentations at '%s' with '%s' at %d",
7094 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7095}
7096
7097static int
7098parser_precise_mbclen(struct parser_params *p, const char *ptr)
7099{
7100 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7101 if (!MBCLEN_CHARFOUND_P(len)) {
7102 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7103 return -1;
7104 }
7105 return len;
7106}
7107
7108#ifndef RIPPER
7109static inline void
7110parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7111{
7112 rb_parser_string_t *str;
7113 int lineno = p->ruby_sourceline;
7114 if (!yylloc) {
7115 return;
7116 }
7117 else if (yylloc->beg_pos.lineno == lineno) {
7118 str = p->lex.lastline;
7119 }
7120 else {
7121 return;
7122 }
7123 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7124}
7125
7126static int
7127parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7128{
7129#if 0
7130 YYLTYPE current;
7131
7132 if (!yylloc) {
7133 yylloc = RUBY_SET_YYLLOC(current);
7134 }
7135 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7136 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7137 yylloc = 0;
7138 }
7139#endif
7140 parser_compile_error(p, yylloc, "%s", msg);
7141 parser_show_error_line(p, yylloc);
7142 return 0;
7143}
7144
7145static int
7146parser_yyerror0(struct parser_params *p, const char *msg)
7147{
7148 YYLTYPE current;
7149 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7150}
7151
7152void
7153ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7154{
7155 VALUE mesg;
7156 const int max_line_margin = 30;
7157 const char *ptr, *ptr_end, *pt, *pb;
7158 const char *pre = "", *post = "", *pend;
7159 const char *code = "", *caret = "";
7160 const char *lim;
7161 const char *const pbeg = PARSER_STRING_PTR(str);
7162 char *buf;
7163 long len;
7164 int i;
7165
7166 if (!yylloc) return;
7167 pend = rb_parser_string_end(str);
7168 if (pend > pbeg && pend[-1] == '\n') {
7169 if (--pend > pbeg && pend[-1] == '\r') --pend;
7170 }
7171
7172 pt = pend;
7173 if (lineno == yylloc->end_pos.lineno &&
7174 (pend - pbeg) > yylloc->end_pos.column) {
7175 pt = pbeg + yylloc->end_pos.column;
7176 }
7177
7178 ptr = ptr_end = pt;
7179 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7180 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7181
7182 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7183 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7184
7185 len = ptr_end - ptr;
7186 if (len > 4) {
7187 if (ptr > pbeg) {
7188 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7189 if (ptr > pbeg) pre = "...";
7190 }
7191 if (ptr_end < pend) {
7192 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7193 if (ptr_end < pend) post = "...";
7194 }
7195 }
7196 pb = pbeg;
7197 if (lineno == yylloc->beg_pos.lineno) {
7198 pb += yylloc->beg_pos.column;
7199 if (pb > pt) pb = pt;
7200 }
7201 if (pb < ptr) pb = ptr;
7202 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7203 return;
7204 }
7205 if (RTEST(errbuf)) {
7206 mesg = rb_attr_get(errbuf, idMesg);
7207 if (char_at_end(p, mesg, '\n') != '\n')
7208 rb_str_cat_cstr(mesg, "\n");
7209 }
7210 else {
7211 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7212 }
7213 if (!errbuf && rb_stderr_tty_p()) {
7214#define CSI_BEGIN "\033["
7215#define CSI_SGR "m"
7216 rb_str_catf(mesg,
7217 CSI_BEGIN""CSI_SGR"%s" /* pre */
7218 CSI_BEGIN"1"CSI_SGR"%.*s"
7219 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7220 CSI_BEGIN";1"CSI_SGR"%.*s"
7221 CSI_BEGIN""CSI_SGR"%s" /* post */
7222 "\n",
7223 pre,
7224 (int)(pb - ptr), ptr,
7225 (int)(pt - pb), pb,
7226 (int)(ptr_end - pt), pt,
7227 post);
7228 }
7229 else {
7230 char *p2;
7231
7232 len = ptr_end - ptr;
7233 lim = pt < pend ? pt : pend;
7234 i = (int)(lim - ptr);
7235 buf = ALLOCA_N(char, i+2);
7236 code = ptr;
7237 caret = p2 = buf;
7238 if (ptr <= pb) {
7239 while (ptr < pb) {
7240 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7241 }
7242 *p2++ = '^';
7243 ptr++;
7244 }
7245 if (lim > ptr) {
7246 memset(p2, '~', (lim - ptr));
7247 p2 += (lim - ptr);
7248 }
7249 *p2 = '\0';
7250 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7251 pre, (int)len, code, post,
7252 pre, caret);
7253 }
7254 if (!errbuf) rb_write_error_str(mesg);
7255}
7256#else
7257
7258static int
7259parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7260{
7261 const char *pcur = 0, *ptok = 0;
7262 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7263 p->ruby_sourceline == yylloc->end_pos.lineno) {
7264 pcur = p->lex.pcur;
7265 ptok = p->lex.ptok;
7266 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7267 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7268 }
7269 parser_yyerror0(p, msg);
7270 if (pcur) {
7271 p->lex.ptok = ptok;
7272 p->lex.pcur = pcur;
7273 }
7274 return 0;
7275}
7276
7277static int
7278parser_yyerror0(struct parser_params *p, const char *msg)
7279{
7280 dispatch1(parse_error, STR_NEW2(msg));
7281 ripper_error(p);
7282 return 0;
7283}
7284
7285static inline void
7286parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7287{
7288}
7289#endif /* !RIPPER */
7290
7291static int
7292vtable_size(const struct vtable *tbl)
7293{
7294 if (!DVARS_TERMINAL_P(tbl)) {
7295 return tbl->pos;
7296 }
7297 else {
7298 return 0;
7299 }
7300}
7301
7302static struct vtable *
7303vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7304{
7305 struct vtable *tbl = ALLOC(struct vtable);
7306 tbl->pos = 0;
7307 tbl->capa = 8;
7308 tbl->tbl = ALLOC_N(ID, tbl->capa);
7309 tbl->prev = prev;
7310#ifndef RIPPER
7311 if (p->debug) {
7312 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7313 }
7314#endif
7315 return tbl;
7316}
7317#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7318
7319static void
7320vtable_free_gen(struct parser_params *p, int line, const char *name,
7321 struct vtable *tbl)
7322{
7323#ifndef RIPPER
7324 if (p->debug) {
7325 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7326 }
7327#endif
7328 if (!DVARS_TERMINAL_P(tbl)) {
7329 if (tbl->tbl) {
7330 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7331 }
7332 ruby_sized_xfree(tbl, sizeof(*tbl));
7333 }
7334}
7335#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7336
7337static void
7338vtable_add_gen(struct parser_params *p, int line, const char *name,
7339 struct vtable *tbl, ID id)
7340{
7341#ifndef RIPPER
7342 if (p->debug) {
7343 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7344 line, name, (void *)tbl, rb_id2name(id));
7345 }
7346#endif
7347 if (DVARS_TERMINAL_P(tbl)) {
7348 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7349 return;
7350 }
7351 if (tbl->pos == tbl->capa) {
7352 tbl->capa = tbl->capa * 2;
7353 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7354 }
7355 tbl->tbl[tbl->pos++] = id;
7356}
7357#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7358
7359static void
7360vtable_pop_gen(struct parser_params *p, int line, const char *name,
7361 struct vtable *tbl, int n)
7362{
7363 if (p->debug) {
7364 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7365 line, name, (void *)tbl, n);
7366 }
7367 if (tbl->pos < n) {
7368 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7369 return;
7370 }
7371 tbl->pos -= n;
7372}
7373#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7374
7375static int
7376vtable_included(const struct vtable * tbl, ID id)
7377{
7378 int i;
7379
7380 if (!DVARS_TERMINAL_P(tbl)) {
7381 for (i = 0; i < tbl->pos; i++) {
7382 if (tbl->tbl[i] == id) {
7383 return i+1;
7384 }
7385 }
7386 }
7387 return 0;
7388}
7389
7390static void parser_prepare(struct parser_params *p);
7391
7392static int
7393e_option_supplied(struct parser_params *p)
7394{
7395 return strcmp(p->ruby_sourcefile, "-e") == 0;
7396}
7397
7398#ifndef RIPPER
7399static NODE *parser_append_options(struct parser_params *p, NODE *node);
7400
7401static VALUE
7402yycompile0(VALUE arg)
7403{
7404 int n;
7405 NODE *tree;
7406 struct parser_params *p = (struct parser_params *)arg;
7407 int cov = FALSE;
7408
7409 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7410 cov = TRUE;
7411 }
7412
7413 if (p->debug_lines) {
7414 p->ast->body.script_lines = p->debug_lines;
7415 }
7416
7417 parser_prepare(p);
7418#define RUBY_DTRACE_PARSE_HOOK(name) \
7419 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7420 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7421 }
7422 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7423 n = yyparse(p);
7424 RUBY_DTRACE_PARSE_HOOK(END);
7425
7426 p->debug_lines = 0;
7427
7428 xfree(p->lex.strterm);
7429 p->lex.strterm = 0;
7430 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7431 if (n || p->error_p) {
7432 VALUE mesg = p->error_buffer;
7433 if (!mesg) {
7434 mesg = syntax_error_new();
7435 }
7436 if (!p->error_tolerant) {
7437 rb_set_errinfo(mesg);
7438 return FALSE;
7439 }
7440 }
7441 tree = p->eval_tree;
7442 if (!tree) {
7443 tree = NEW_NIL(&NULL_LOC);
7444 }
7445 else {
7446 rb_parser_ary_t *tokens = p->tokens;
7447 NODE *prelude;
7448 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7449 prelude = block_append(p, p->eval_tree_begin, body);
7450 RNODE_SCOPE(tree)->nd_body = prelude;
7451 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7452 p->ast->body.coverage_enabled = cov;
7453 if (p->keep_tokens) {
7454 p->ast->node_buffer->tokens = tokens;
7455 p->tokens = NULL;
7456 }
7457 }
7458 p->ast->body.root = tree;
7459 p->ast->body.line_count = p->line_count;
7460 return TRUE;
7461}
7462
7463static rb_ast_t *
7464yycompile(struct parser_params *p, VALUE fname, int line)
7465{
7466 rb_ast_t *ast;
7467 if (NIL_P(fname)) {
7468 p->ruby_sourcefile_string = Qnil;
7469 p->ruby_sourcefile = "(none)";
7470 }
7471 else {
7472 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7473 p->ruby_sourcefile = StringValueCStr(fname);
7474 }
7475 p->ruby_sourceline = line - 1;
7476
7477 p->lvtbl = NULL;
7478
7479 p->ast = ast = rb_ast_new();
7480 compile_callback(yycompile0, (VALUE)p);
7481 p->ast = 0;
7482
7483 while (p->lvtbl) {
7484 local_pop(p);
7485 }
7486
7487 return ast;
7488}
7489#endif /* !RIPPER */
7490
7491static rb_encoding *
7492must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7493{
7494 rb_encoding *enc = rb_parser_str_get_encoding(s);
7495 if (!rb_enc_asciicompat(enc)) {
7496 rb_raise(rb_eArgError, "invalid source encoding");
7497 }
7498 return enc;
7499}
7500
7501static rb_parser_string_t *
7502lex_getline(struct parser_params *p)
7503{
7504 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7505 if (!line) return 0;
7506 p->line_count++;
7507 string_buffer_append(p, line);
7508 must_be_ascii_compatible(p, line);
7509 return line;
7510}
7511
7512#ifndef RIPPER
7513rb_ast_t*
7514rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7515{
7516 p->lex.gets = gets;
7517 p->lex.input = input;
7518 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7519
7520 return yycompile(p, fname, line);
7521}
7522#endif /* !RIPPER */
7523
7524#define STR_FUNC_ESCAPE 0x01
7525#define STR_FUNC_EXPAND 0x02
7526#define STR_FUNC_REGEXP 0x04
7527#define STR_FUNC_QWORDS 0x08
7528#define STR_FUNC_SYMBOL 0x10
7529#define STR_FUNC_INDENT 0x20
7530#define STR_FUNC_LABEL 0x40
7531#define STR_FUNC_LIST 0x4000
7532#define STR_FUNC_TERM 0x8000
7533
7534enum string_type {
7535 str_label = STR_FUNC_LABEL,
7536 str_squote = (0),
7537 str_dquote = (STR_FUNC_EXPAND),
7538 str_xquote = (STR_FUNC_EXPAND),
7539 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7540 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7541 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7542 str_ssym = (STR_FUNC_SYMBOL),
7543 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7544};
7545
7546static rb_parser_string_t *
7547parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7548{
7549 rb_parser_string_t *pstr;
7550
7551 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7552
7553 if (!(func & STR_FUNC_REGEXP)) {
7554 if (rb_parser_is_ascii_string(p, pstr)) {
7555 }
7556 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7557 /* everything is valid in ASCII-8BIT */
7558 enc = rb_ascii8bit_encoding();
7559 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7560 }
7561 }
7562
7563 return pstr;
7564}
7565
7566static int
7567strterm_is_heredoc(rb_strterm_t *strterm)
7568{
7569 return strterm->heredoc;
7570}
7571
7572static rb_strterm_t *
7573new_strterm(struct parser_params *p, int func, int term, int paren)
7574{
7575 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7576 strterm->u.literal.func = func;
7577 strterm->u.literal.term = term;
7578 strterm->u.literal.paren = paren;
7579 return strterm;
7580}
7581
7582static rb_strterm_t *
7583new_heredoc(struct parser_params *p)
7584{
7585 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7586 strterm->heredoc = true;
7587 return strterm;
7588}
7589
7590#define peek(p,c) peek_n(p, (c), 0)
7591#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7592#define peekc(p) peekc_n(p, 0)
7593#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7594
7595#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7596static void
7597parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7598{
7599 debug_token_line(p, "add_delayed_token", line);
7600
7601 if (tok < end) {
7602 if (has_delayed_token(p)) {
7603 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7604 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7605 int end_col = (next_line ? 0 : p->delayed.end_col);
7606 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7607 dispatch_delayed_token(p, tSTRING_CONTENT);
7608 }
7609 }
7610 if (!has_delayed_token(p)) {
7611 p->delayed.token = rb_parser_string_new(p, 0, 0);
7612 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7613 p->delayed.beg_line = p->ruby_sourceline;
7614 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7615 }
7616 parser_str_cat(p->delayed.token, tok, end - tok);
7617 p->delayed.end_line = p->ruby_sourceline;
7618 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7619 p->lex.ptok = end;
7620 }
7621}
7622
7623static void
7624set_lastline(struct parser_params *p, rb_parser_string_t *str)
7625{
7626 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7627 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7628 p->lex.lastline = str;
7629}
7630
7631static int
7632nextline(struct parser_params *p, int set_encoding)
7633{
7634 rb_parser_string_t *str = p->lex.nextline;
7635 p->lex.nextline = 0;
7636 if (!str) {
7637 if (p->eofp)
7638 return -1;
7639
7640 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7641 goto end_of_input;
7642 }
7643
7644 if (!p->lex.input || !(str = lex_getline(p))) {
7645 end_of_input:
7646 p->eofp = 1;
7647 lex_goto_eol(p);
7648 return -1;
7649 }
7650#ifndef RIPPER
7651 if (p->debug_lines) {
7652 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7653 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7654 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7655 }
7656#endif
7657 p->cr_seen = FALSE;
7658 }
7659 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7660 /* after here-document without terminator */
7661 goto end_of_input;
7662 }
7663 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7664 if (p->heredoc_end > 0) {
7665 p->ruby_sourceline = p->heredoc_end;
7666 p->heredoc_end = 0;
7667 }
7668 p->ruby_sourceline++;
7669 set_lastline(p, str);
7670 token_flush(p);
7671 return 0;
7672}
7673
7674static int
7675parser_cr(struct parser_params *p, int c)
7676{
7677 if (peek(p, '\n')) {
7678 p->lex.pcur++;
7679 c = '\n';
7680 }
7681 return c;
7682}
7683
7684static inline int
7685nextc0(struct parser_params *p, int set_encoding)
7686{
7687 int c;
7688
7689 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7690 if (nextline(p, set_encoding)) return -1;
7691 }
7692 c = (unsigned char)*p->lex.pcur++;
7693 if (UNLIKELY(c == '\r')) {
7694 c = parser_cr(p, c);
7695 }
7696
7697 return c;
7698}
7699#define nextc(p) nextc0(p, TRUE)
7700
7701static void
7702pushback(struct parser_params *p, int c)
7703{
7704 if (c == -1) return;
7705 p->eofp = 0;
7706 p->lex.pcur--;
7707 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7708 p->lex.pcur--;
7709 }
7710}
7711
7712#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7713
7714#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7715#define tok(p) (p)->tokenbuf
7716#define toklen(p) (p)->tokidx
7717
7718static int
7719looking_at_eol_p(struct parser_params *p)
7720{
7721 const char *ptr = p->lex.pcur;
7722 while (!lex_eol_ptr_p(p, ptr)) {
7723 int c = (unsigned char)*ptr++;
7724 int eol = (c == '\n' || c == '#');
7725 if (eol || !ISSPACE(c)) {
7726 return eol;
7727 }
7728 }
7729 return TRUE;
7730}
7731
7732static char*
7733newtok(struct parser_params *p)
7734{
7735 p->tokidx = 0;
7736 if (!p->tokenbuf) {
7737 p->toksiz = 60;
7738 p->tokenbuf = ALLOC_N(char, 60);
7739 }
7740 if (p->toksiz > 4096) {
7741 p->toksiz = 60;
7742 REALLOC_N(p->tokenbuf, char, 60);
7743 }
7744 return p->tokenbuf;
7745}
7746
7747static char *
7748tokspace(struct parser_params *p, int n)
7749{
7750 p->tokidx += n;
7751
7752 if (p->tokidx >= p->toksiz) {
7753 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7754 REALLOC_N(p->tokenbuf, char, p->toksiz);
7755 }
7756 return &p->tokenbuf[p->tokidx-n];
7757}
7758
7759static void
7760tokadd(struct parser_params *p, int c)
7761{
7762 p->tokenbuf[p->tokidx++] = (char)c;
7763 if (p->tokidx >= p->toksiz) {
7764 p->toksiz *= 2;
7765 REALLOC_N(p->tokenbuf, char, p->toksiz);
7766 }
7767}
7768
7769static int
7770tok_hex(struct parser_params *p, size_t *numlen)
7771{
7772 int c;
7773
7774 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7775 if (!*numlen) {
7776 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7777 yyerror0("invalid hex escape");
7778 dispatch_scan_event(p, tSTRING_CONTENT);
7779 return 0;
7780 }
7781 p->lex.pcur += *numlen;
7782 return c;
7783}
7784
7785#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7786
7787static int
7788escaped_control_code(int c)
7789{
7790 int c2 = 0;
7791 switch (c) {
7792 case ' ':
7793 c2 = 's';
7794 break;
7795 case '\n':
7796 c2 = 'n';
7797 break;
7798 case '\t':
7799 c2 = 't';
7800 break;
7801 case '\v':
7802 c2 = 'v';
7803 break;
7804 case '\r':
7805 c2 = 'r';
7806 break;
7807 case '\f':
7808 c2 = 'f';
7809 break;
7810 }
7811 return c2;
7812}
7813
7814#define WARN_SPACE_CHAR(c, prefix) \
7815 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7816
7817static int
7818tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7819 int regexp_literal, const char *begin)
7820{
7821 const int wide = !begin;
7822 size_t numlen;
7823 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7824
7825 p->lex.pcur += numlen;
7826 if (p->lex.strterm == NULL ||
7827 strterm_is_heredoc(p->lex.strterm) ||
7828 (p->lex.strterm->u.literal.func != str_regexp)) {
7829 if (!begin) begin = p->lex.pcur;
7830 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7831 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7832 yyerror0("invalid Unicode escape");
7833 dispatch_scan_event(p, tSTRING_CONTENT);
7834 return wide && numlen > 0;
7835 }
7836 if (codepoint > 0x10ffff) {
7837 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7838 yyerror0("invalid Unicode codepoint (too large)");
7839 dispatch_scan_event(p, tSTRING_CONTENT);
7840 return wide;
7841 }
7842 if ((codepoint & 0xfffff800) == 0xd800) {
7843 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7844 yyerror0("invalid Unicode codepoint");
7845 dispatch_scan_event(p, tSTRING_CONTENT);
7846 return wide;
7847 }
7848 }
7849 if (regexp_literal) {
7850 tokcopy(p, (int)numlen);
7851 }
7852 else if (codepoint >= 0x80) {
7853 rb_encoding *utf8 = rb_utf8_encoding();
7854 if (*encp && utf8 != *encp) {
7855 YYLTYPE loc = RUBY_INIT_YYLLOC();
7856 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7857 parser_show_error_line(p, &loc);
7858 return wide;
7859 }
7860 *encp = utf8;
7861 tokaddmbc(p, codepoint, *encp);
7862 }
7863 else {
7864 tokadd(p, codepoint);
7865 }
7866 return TRUE;
7867}
7868
7869static int tokadd_mbchar(struct parser_params *p, int c);
7870
7871static int
7872tokskip_mbchar(struct parser_params *p)
7873{
7874 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7875 if (len > 0) {
7876 p->lex.pcur += len - 1;
7877 }
7878 return len;
7879}
7880
7881/* return value is for ?\u3042 */
7882static void
7883tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7884 int term, int symbol_literal, int regexp_literal)
7885{
7886 /*
7887 * If `term` is not -1, then we allow multiple codepoints in \u{}
7888 * upto `term` byte, otherwise we're parsing a character literal.
7889 * And then add the codepoints to the current token.
7890 */
7891 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7892
7893 const int open_brace = '{', close_brace = '}';
7894
7895 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7896
7897 if (peek(p, open_brace)) { /* handle \u{...} form */
7898 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7899 /*
7900 * Skip parsing validation code and copy bytes as-is until term or
7901 * closing brace, in order to correctly handle extended regexps where
7902 * invalid unicode escapes are allowed in comments. The regexp parser
7903 * does its own validation and will catch any issues.
7904 */
7905 tokadd(p, open_brace);
7906 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7907 int c = peekc(p);
7908 if (c == close_brace) {
7909 tokadd(p, c);
7910 ++p->lex.pcur;
7911 break;
7912 }
7913 else if (c == term) {
7914 break;
7915 }
7916 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7917 tokadd(p, c);
7918 c = *++p->lex.pcur;
7919 }
7920 tokadd_mbchar(p, c);
7921 }
7922 }
7923 else {
7924 const char *second = NULL;
7925 int c, last = nextc(p);
7926 if (lex_eol_p(p)) goto unterminated;
7927 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7928 while (c != close_brace) {
7929 if (c == term) goto unterminated;
7930 if (second == multiple_codepoints)
7931 second = p->lex.pcur;
7932 if (regexp_literal) tokadd(p, last);
7933 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7934 break;
7935 }
7936 while (ISSPACE(c = peekc(p))) {
7937 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7938 last = c;
7939 }
7940 if (term == -1 && !second)
7941 second = multiple_codepoints;
7942 }
7943
7944 if (c != close_brace) {
7945 unterminated:
7946 flush_string_content(p, rb_utf8_encoding(), 0);
7947 yyerror0("unterminated Unicode escape");
7948 dispatch_scan_event(p, tSTRING_CONTENT);
7949 return;
7950 }
7951 if (second && second != multiple_codepoints) {
7952 const char *pcur = p->lex.pcur;
7953 p->lex.pcur = second;
7954 dispatch_scan_event(p, tSTRING_CONTENT);
7955 token_flush(p);
7956 p->lex.pcur = pcur;
7957 yyerror0(multiple_codepoints);
7958 token_flush(p);
7959 }
7960
7961 if (regexp_literal) tokadd(p, close_brace);
7962 nextc(p);
7963 }
7964 }
7965 else { /* handle \uxxxx form */
7966 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
7967 token_flush(p);
7968 return;
7969 }
7970 }
7971}
7972
7973#define ESCAPE_CONTROL 1
7974#define ESCAPE_META 2
7975
7976static int
7977read_escape(struct parser_params *p, int flags, const char *begin)
7978{
7979 int c;
7980 size_t numlen;
7981
7982 switch (c = nextc(p)) {
7983 case '\\': /* Backslash */
7984 return c;
7985
7986 case 'n': /* newline */
7987 return '\n';
7988
7989 case 't': /* horizontal tab */
7990 return '\t';
7991
7992 case 'r': /* carriage-return */
7993 return '\r';
7994
7995 case 'f': /* form-feed */
7996 return '\f';
7997
7998 case 'v': /* vertical tab */
7999 return '\13';
8000
8001 case 'a': /* alarm(bell) */
8002 return '\007';
8003
8004 case 'e': /* escape */
8005 return 033;
8006
8007 case '0': case '1': case '2': case '3': /* octal constant */
8008 case '4': case '5': case '6': case '7':
8009 pushback(p, c);
8010 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8011 p->lex.pcur += numlen;
8012 return c;
8013
8014 case 'x': /* hex constant */
8015 c = tok_hex(p, &numlen);
8016 if (numlen == 0) return 0;
8017 return c;
8018
8019 case 'b': /* backspace */
8020 return '\010';
8021
8022 case 's': /* space */
8023 return ' ';
8024
8025 case 'M':
8026 if (flags & ESCAPE_META) goto eof;
8027 if ((c = nextc(p)) != '-') {
8028 goto eof;
8029 }
8030 if ((c = nextc(p)) == '\\') {
8031 switch (peekc(p)) {
8032 case 'u': case 'U':
8033 nextc(p);
8034 goto eof;
8035 }
8036 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8037 }
8038 else if (c == -1) goto eof;
8039 else if (!ISASCII(c)) {
8040 tokskip_mbchar(p);
8041 goto eof;
8042 }
8043 else {
8044 int c2 = escaped_control_code(c);
8045 if (c2) {
8046 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8047 WARN_SPACE_CHAR(c2, "\\M-");
8048 }
8049 else {
8050 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8051 }
8052 }
8053 else if (ISCNTRL(c)) goto eof;
8054 return ((c & 0xff) | 0x80);
8055 }
8056
8057 case 'C':
8058 if ((c = nextc(p)) != '-') {
8059 goto eof;
8060 }
8061 case 'c':
8062 if (flags & ESCAPE_CONTROL) goto eof;
8063 if ((c = nextc(p))== '\\') {
8064 switch (peekc(p)) {
8065 case 'u': case 'U':
8066 nextc(p);
8067 goto eof;
8068 }
8069 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8070 }
8071 else if (c == '?')
8072 return 0177;
8073 else if (c == -1) goto eof;
8074 else if (!ISASCII(c)) {
8075 tokskip_mbchar(p);
8076 goto eof;
8077 }
8078 else {
8079 int c2 = escaped_control_code(c);
8080 if (c2) {
8081 if (ISCNTRL(c)) {
8082 if (flags & ESCAPE_META) {
8083 WARN_SPACE_CHAR(c2, "\\M-");
8084 }
8085 else {
8086 WARN_SPACE_CHAR(c2, "");
8087 }
8088 }
8089 else {
8090 if (flags & ESCAPE_META) {
8091 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8092 }
8093 else {
8094 WARN_SPACE_CHAR(c2, "\\C-");
8095 }
8096 }
8097 }
8098 else if (ISCNTRL(c)) goto eof;
8099 }
8100 return c & 0x9f;
8101
8102 eof:
8103 case -1:
8104 flush_string_content(p, p->enc, p->lex.pcur - begin);
8105 yyerror0("Invalid escape character syntax");
8106 dispatch_scan_event(p, tSTRING_CONTENT);
8107 return '\0';
8108
8109 default:
8110 if (!ISASCII(c)) {
8111 tokskip_mbchar(p);
8112 goto eof;
8113 }
8114 return c;
8115 }
8116}
8117
8118static void
8119tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8120{
8121 int len = rb_enc_codelen(c, enc);
8122 rb_enc_mbcput(c, tokspace(p, len), enc);
8123}
8124
8125static int
8126tokadd_escape(struct parser_params *p)
8127{
8128 int c;
8129 size_t numlen;
8130 const char *begin = p->lex.pcur;
8131
8132 switch (c = nextc(p)) {
8133 case '\n':
8134 return 0; /* just ignore */
8135
8136 case '0': case '1': case '2': case '3': /* octal constant */
8137 case '4': case '5': case '6': case '7':
8138 {
8139 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8140 if (numlen == 0) goto eof;
8141 p->lex.pcur += numlen;
8142 tokcopy(p, (int)numlen + 1);
8143 }
8144 return 0;
8145
8146 case 'x': /* hex constant */
8147 {
8148 tok_hex(p, &numlen);
8149 if (numlen == 0) return -1;
8150 tokcopy(p, (int)numlen + 2);
8151 }
8152 return 0;
8153
8154 eof:
8155 case -1:
8156 flush_string_content(p, p->enc, p->lex.pcur - begin);
8157 yyerror0("Invalid escape character syntax");
8158 token_flush(p);
8159 return -1;
8160
8161 default:
8162 tokadd(p, '\\');
8163 tokadd(p, c);
8164 }
8165 return 0;
8166}
8167
8168static int
8169char_to_option(int c)
8170{
8171 int val;
8172
8173 switch (c) {
8174 case 'i':
8175 val = RE_ONIG_OPTION_IGNORECASE;
8176 break;
8177 case 'x':
8178 val = RE_ONIG_OPTION_EXTEND;
8179 break;
8180 case 'm':
8181 val = RE_ONIG_OPTION_MULTILINE;
8182 break;
8183 default:
8184 val = 0;
8185 break;
8186 }
8187 return val;
8188}
8189
8190#define ARG_ENCODING_FIXED 16
8191#define ARG_ENCODING_NONE 32
8192#define ENC_ASCII8BIT 1
8193#define ENC_EUC_JP 2
8194#define ENC_Windows_31J 3
8195#define ENC_UTF8 4
8196
8197static int
8198char_to_option_kcode(int c, int *option, int *kcode)
8199{
8200 *option = 0;
8201
8202 switch (c) {
8203 case 'n':
8204 *kcode = ENC_ASCII8BIT;
8205 return (*option = ARG_ENCODING_NONE);
8206 case 'e':
8207 *kcode = ENC_EUC_JP;
8208 break;
8209 case 's':
8210 *kcode = ENC_Windows_31J;
8211 break;
8212 case 'u':
8213 *kcode = ENC_UTF8;
8214 break;
8215 default:
8216 *kcode = -1;
8217 return (*option = char_to_option(c));
8218 }
8219 *option = ARG_ENCODING_FIXED;
8220 return 1;
8221}
8222
8223static int
8224regx_options(struct parser_params *p)
8225{
8226 int kcode = 0;
8227 int kopt = 0;
8228 int options = 0;
8229 int c, opt, kc;
8230
8231 newtok(p);
8232 while (c = nextc(p), ISALPHA(c)) {
8233 if (c == 'o') {
8234 options |= RE_OPTION_ONCE;
8235 }
8236 else if (char_to_option_kcode(c, &opt, &kc)) {
8237 if (kc >= 0) {
8238 if (kc != ENC_ASCII8BIT) kcode = c;
8239 kopt = opt;
8240 }
8241 else {
8242 options |= opt;
8243 }
8244 }
8245 else {
8246 tokadd(p, c);
8247 }
8248 }
8249 options |= kopt;
8250 pushback(p, c);
8251 if (toklen(p)) {
8252 YYLTYPE loc = RUBY_INIT_YYLLOC();
8253 tokfix(p);
8254 compile_error(p, "unknown regexp option%s - %*s",
8255 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8256 parser_show_error_line(p, &loc);
8257 }
8258 return options | RE_OPTION_ENCODING(kcode);
8259}
8260
8261static int
8262tokadd_mbchar(struct parser_params *p, int c)
8263{
8264 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8265 if (len < 0) return -1;
8266 tokadd(p, c);
8267 p->lex.pcur += --len;
8268 if (len > 0) tokcopy(p, len);
8269 return c;
8270}
8271
8272static inline int
8273simple_re_meta(int c)
8274{
8275 switch (c) {
8276 case '$': case '*': case '+': case '.':
8277 case '?': case '^': case '|':
8278 case ')': case ']': case '}': case '>':
8279 return TRUE;
8280 default:
8281 return FALSE;
8282 }
8283}
8284
8285static int
8286parser_update_heredoc_indent(struct parser_params *p, int c)
8287{
8288 if (p->heredoc_line_indent == -1) {
8289 if (c == '\n') p->heredoc_line_indent = 0;
8290 }
8291 else {
8292 if (c == ' ') {
8293 p->heredoc_line_indent++;
8294 return TRUE;
8295 }
8296 else if (c == '\t') {
8297 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8298 p->heredoc_line_indent = w * TAB_WIDTH;
8299 return TRUE;
8300 }
8301 else if (c != '\n') {
8302 if (p->heredoc_indent > p->heredoc_line_indent) {
8303 p->heredoc_indent = p->heredoc_line_indent;
8304 }
8305 p->heredoc_line_indent = -1;
8306 }
8307 else {
8308 /* Whitespace only line has no indentation */
8309 p->heredoc_line_indent = 0;
8310 }
8311 }
8312 return FALSE;
8313}
8314
8315static void
8316parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8317{
8318 YYLTYPE loc = RUBY_INIT_YYLLOC();
8319 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8320 compile_error(p, "%s mixed within %s source", n1, n2);
8321 parser_show_error_line(p, &loc);
8322}
8323
8324static void
8325parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8326{
8327 const char *pos = p->lex.pcur;
8328 p->lex.pcur = beg;
8329 parser_mixed_error(p, enc1, enc2);
8330 p->lex.pcur = pos;
8331}
8332
8333static inline char
8334nibble_char_upper(unsigned int c)
8335{
8336 c &= 0xf;
8337 return c + (c < 10 ? '0' : 'A' - 10);
8338}
8339
8340static int
8341tokadd_string(struct parser_params *p,
8342 int func, int term, int paren, long *nest,
8343 rb_encoding **encp, rb_encoding **enc)
8344{
8345 int c;
8346 bool erred = false;
8347#ifdef RIPPER
8348 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8349 int top_of_line = FALSE;
8350#endif
8351
8352#define mixed_error(enc1, enc2) \
8353 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8354#define mixed_escape(beg, enc1, enc2) \
8355 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8356
8357 while ((c = nextc(p)) != -1) {
8358 if (p->heredoc_indent > 0) {
8359 parser_update_heredoc_indent(p, c);
8360 }
8361#ifdef RIPPER
8362 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8363 pushback(p, c);
8364 break;
8365 }
8366#endif
8367
8368 if (paren && c == paren) {
8369 ++*nest;
8370 }
8371 else if (c == term) {
8372 if (!nest || !*nest) {
8373 pushback(p, c);
8374 break;
8375 }
8376 --*nest;
8377 }
8378 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8379 unsigned char c2 = *p->lex.pcur;
8380 if (c2 == '$' || c2 == '@' || c2 == '{') {
8381 pushback(p, c);
8382 break;
8383 }
8384 }
8385 else if (c == '\\') {
8386 c = nextc(p);
8387 switch (c) {
8388 case '\n':
8389 if (func & STR_FUNC_QWORDS) break;
8390 if (func & STR_FUNC_EXPAND) {
8391 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8392 continue;
8393 if (c == term) {
8394 c = '\\';
8395 goto terminate;
8396 }
8397 }
8398 tokadd(p, '\\');
8399 break;
8400
8401 case '\\':
8402 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8403 break;
8404
8405 case 'u':
8406 if ((func & STR_FUNC_EXPAND) == 0) {
8407 tokadd(p, '\\');
8408 break;
8409 }
8410 tokadd_utf8(p, enc, term,
8411 func & STR_FUNC_SYMBOL,
8412 func & STR_FUNC_REGEXP);
8413 continue;
8414
8415 default:
8416 if (c == -1) return -1;
8417 if (!ISASCII(c)) {
8418 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8419 goto non_ascii;
8420 }
8421 if (func & STR_FUNC_REGEXP) {
8422 switch (c) {
8423 case 'c':
8424 case 'C':
8425 case 'M': {
8426 pushback(p, c);
8427 c = read_escape(p, 0, p->lex.pcur - 1);
8428
8429 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8430 *t++ = '\\';
8431 *t++ = 'x';
8432 *t++ = nibble_char_upper(c >> 4);
8433 *t++ = nibble_char_upper(c);
8434 continue;
8435 }
8436 }
8437
8438 if (c == term && !simple_re_meta(c)) {
8439 tokadd(p, c);
8440 continue;
8441 }
8442 pushback(p, c);
8443 if ((c = tokadd_escape(p)) < 0)
8444 return -1;
8445 if (*enc && *enc != *encp) {
8446 mixed_escape(p->lex.ptok+2, *enc, *encp);
8447 }
8448 continue;
8449 }
8450 else if (func & STR_FUNC_EXPAND) {
8451 pushback(p, c);
8452 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8453 c = read_escape(p, 0, p->lex.pcur - 1);
8454 }
8455 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8456 /* ignore backslashed spaces in %w */
8457 }
8458 else if (c != term && !(paren && c == paren)) {
8459 tokadd(p, '\\');
8460 pushback(p, c);
8461 continue;
8462 }
8463 }
8464 }
8465 else if (!parser_isascii(p)) {
8466 non_ascii:
8467 if (!*enc) {
8468 *enc = *encp;
8469 }
8470 else if (*enc != *encp) {
8471 mixed_error(*enc, *encp);
8472 continue;
8473 }
8474 if (tokadd_mbchar(p, c) == -1) return -1;
8475 continue;
8476 }
8477 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8478 pushback(p, c);
8479 break;
8480 }
8481 if (c & 0x80) {
8482 if (!*enc) {
8483 *enc = *encp;
8484 }
8485 else if (*enc != *encp) {
8486 mixed_error(*enc, *encp);
8487 continue;
8488 }
8489 }
8490 tokadd(p, c);
8491#ifdef RIPPER
8492 top_of_line = (c == '\n');
8493#endif
8494 }
8495 terminate:
8496 if (*enc) *encp = *enc;
8497 return c;
8498}
8499
8500#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8501
8502static void
8503flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8504{
8505 p->lex.pcur -= back;
8506 if (has_delayed_token(p)) {
8507 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8508 if (len > 0) {
8509 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8510 p->delayed.end_line = p->ruby_sourceline;
8511 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8512 }
8513 dispatch_delayed_token(p, tSTRING_CONTENT);
8514 p->lex.ptok = p->lex.pcur;
8515 }
8516 dispatch_scan_event(p, tSTRING_CONTENT);
8517 p->lex.pcur += back;
8518}
8519
8520/* this can be shared with ripper, since it's independent from struct
8521 * parser_params. */
8522#ifndef RIPPER
8523#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8524#define SPECIAL_PUNCT(idx) ( \
8525 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8526 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8527 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8528 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8529 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8530 BIT('0', idx))
8531const uint_least32_t ruby_global_name_punct_bits[] = {
8532 SPECIAL_PUNCT(0),
8533 SPECIAL_PUNCT(1),
8534 SPECIAL_PUNCT(2),
8535};
8536#undef BIT
8537#undef SPECIAL_PUNCT
8538#endif
8539
8540static enum yytokentype
8541parser_peek_variable_name(struct parser_params *p)
8542{
8543 int c;
8544 const char *ptr = p->lex.pcur;
8545
8546 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8547 c = *ptr++;
8548 switch (c) {
8549 case '$':
8550 if ((c = *ptr) == '-') {
8551 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8552 c = *ptr;
8553 }
8554 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8555 return tSTRING_DVAR;
8556 }
8557 break;
8558 case '@':
8559 if ((c = *ptr) == '@') {
8560 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8561 c = *ptr;
8562 }
8563 break;
8564 case '{':
8565 p->lex.pcur = ptr;
8566 p->command_start = TRUE;
8567 yylval.state = p->lex.state;
8568 return tSTRING_DBEG;
8569 default:
8570 return 0;
8571 }
8572 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8573 return tSTRING_DVAR;
8574 return 0;
8575}
8576
8577#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8578#define IS_END() IS_lex_state(EXPR_END_ANY)
8579#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8580#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8581#define IS_LABEL_POSSIBLE() (\
8582 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8583 IS_ARG())
8584#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8585#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8586
8587static inline enum yytokentype
8588parser_string_term(struct parser_params *p, int func)
8589{
8590 xfree(p->lex.strterm);
8591 p->lex.strterm = 0;
8592 if (func & STR_FUNC_REGEXP) {
8593 set_yylval_num(regx_options(p));
8594 dispatch_scan_event(p, tREGEXP_END);
8595 SET_LEX_STATE(EXPR_END);
8596 return tREGEXP_END;
8597 }
8598 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8599 nextc(p);
8600 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8601 return tLABEL_END;
8602 }
8603 SET_LEX_STATE(EXPR_END);
8604 return tSTRING_END;
8605}
8606
8607static enum yytokentype
8608parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8609{
8610 int func = quote->func;
8611 int term = quote->term;
8612 int paren = quote->paren;
8613 int c, space = 0;
8614 rb_encoding *enc = p->enc;
8615 rb_encoding *base_enc = 0;
8616 rb_parser_string_t *lit;
8617
8618 if (func & STR_FUNC_TERM) {
8619 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8620 SET_LEX_STATE(EXPR_END);
8621 xfree(p->lex.strterm);
8622 p->lex.strterm = 0;
8623 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8624 }
8625 c = nextc(p);
8626 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8627 while (c != '\n' && ISSPACE(c = nextc(p)));
8628 space = 1;
8629 }
8630 if (func & STR_FUNC_LIST) {
8631 quote->func &= ~STR_FUNC_LIST;
8632 space = 1;
8633 }
8634 if (c == term && !quote->nest) {
8635 if (func & STR_FUNC_QWORDS) {
8636 quote->func |= STR_FUNC_TERM;
8637 pushback(p, c); /* dispatch the term at tSTRING_END */
8638 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8639 return ' ';
8640 }
8641 return parser_string_term(p, func);
8642 }
8643 if (space) {
8644 if (!ISSPACE(c)) pushback(p, c);
8645 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8646 return ' ';
8647 }
8648 newtok(p);
8649 if ((func & STR_FUNC_EXPAND) && c == '#') {
8650 enum yytokentype t = parser_peek_variable_name(p);
8651 if (t) return t;
8652 tokadd(p, '#');
8653 c = nextc(p);
8654 }
8655 pushback(p, c);
8656 if (tokadd_string(p, func, term, paren, &quote->nest,
8657 &enc, &base_enc) == -1) {
8658 if (p->eofp) {
8659#ifndef RIPPER
8660# define unterminated_literal(mesg) yyerror0(mesg)
8661#else
8662# define unterminated_literal(mesg) compile_error(p, mesg)
8663#endif
8664 literal_flush(p, p->lex.pcur);
8665 if (func & STR_FUNC_QWORDS) {
8666 /* no content to add, bailing out here */
8667 unterminated_literal("unterminated list meets end of file");
8668 xfree(p->lex.strterm);
8669 p->lex.strterm = 0;
8670 return tSTRING_END;
8671 }
8672 if (func & STR_FUNC_REGEXP) {
8673 unterminated_literal("unterminated regexp meets end of file");
8674 }
8675 else {
8676 unterminated_literal("unterminated string meets end of file");
8677 }
8678 quote->func |= STR_FUNC_TERM;
8679 }
8680 }
8681
8682 tokfix(p);
8683 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8684 set_yylval_str(lit);
8685 flush_string_content(p, enc, 0);
8686
8687 return tSTRING_CONTENT;
8688}
8689
8690static enum yytokentype
8691heredoc_identifier(struct parser_params *p)
8692{
8693 /*
8694 * term_len is length of `<<"END"` except `END`,
8695 * in this case term_len is 4 (<, <, " and ").
8696 */
8697 long len, offset = p->lex.pcur - p->lex.pbeg;
8698 int c = nextc(p), term, func = 0, quote = 0;
8699 enum yytokentype token = tSTRING_BEG;
8700 int indent = 0;
8701
8702 if (c == '-') {
8703 c = nextc(p);
8704 func = STR_FUNC_INDENT;
8705 offset++;
8706 }
8707 else if (c == '~') {
8708 c = nextc(p);
8709 func = STR_FUNC_INDENT;
8710 offset++;
8711 indent = INT_MAX;
8712 }
8713 switch (c) {
8714 case '\'':
8715 func |= str_squote; goto quoted;
8716 case '"':
8717 func |= str_dquote; goto quoted;
8718 case '`':
8719 token = tXSTRING_BEG;
8720 func |= str_xquote; goto quoted;
8721
8722 quoted:
8723 quote++;
8724 offset++;
8725 term = c;
8726 len = 0;
8727 while ((c = nextc(p)) != term) {
8728 if (c == -1 || c == '\r' || c == '\n') {
8729 yyerror0("unterminated here document identifier");
8730 return -1;
8731 }
8732 }
8733 break;
8734
8735 default:
8736 if (!parser_is_identchar(p)) {
8737 pushback(p, c);
8738 if (func & STR_FUNC_INDENT) {
8739 pushback(p, indent > 0 ? '~' : '-');
8740 }
8741 return 0;
8742 }
8743 func |= str_dquote;
8744 do {
8745 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8746 if (n < 0) return 0;
8747 p->lex.pcur += --n;
8748 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8749 pushback(p, c);
8750 break;
8751 }
8752
8753 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8754 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8755 yyerror0("too long here document identifier");
8756 dispatch_scan_event(p, tHEREDOC_BEG);
8757 lex_goto_eol(p);
8758
8759 p->lex.strterm = new_heredoc(p);
8760 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8761 here->offset = offset;
8762 here->sourceline = p->ruby_sourceline;
8763 here->length = (unsigned)len;
8764 here->quote = quote;
8765 here->func = func;
8766 here->lastline = p->lex.lastline;
8767
8768 token_flush(p);
8769 p->heredoc_indent = indent;
8770 p->heredoc_line_indent = 0;
8771 return token;
8772}
8773
8774static void
8775heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8776{
8777 rb_parser_string_t *line;
8778 rb_strterm_t *term = p->lex.strterm;
8779
8780 p->lex.strterm = 0;
8781 line = here->lastline;
8782 p->lex.lastline = line;
8783 p->lex.pbeg = PARSER_STRING_PTR(line);
8784 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8785 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8786 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8787 p->heredoc_end = p->ruby_sourceline;
8788 p->ruby_sourceline = (int)here->sourceline;
8789 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8790 p->eofp = 0;
8791 xfree(term);
8792}
8793
8794static int
8795dedent_string_column(const char *str, long len, int width)
8796{
8797 int i, col = 0;
8798
8799 for (i = 0; i < len && col < width; i++) {
8800 if (str[i] == ' ') {
8801 col++;
8802 }
8803 else if (str[i] == '\t') {
8804 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8805 if (n > width) break;
8806 col = n;
8807 }
8808 else {
8809 break;
8810 }
8811 }
8812
8813 return i;
8814}
8815
8816static int
8817dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8818{
8819 char *str;
8820 long len;
8821 int i;
8822
8823 len = PARSER_STRING_LEN(string);
8824 str = PARSER_STRING_PTR(string);
8825
8826 i = dedent_string_column(str, len, width);
8827 if (!i) return 0;
8828
8829 rb_parser_str_modify(string);
8830 str = PARSER_STRING_PTR(string);
8831 if (PARSER_STRING_LEN(string) != len)
8832 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8833 MEMMOVE(str, str + i, char, len - i);
8834 rb_parser_str_set_len(p, string, len - i);
8835 return i;
8836}
8837
8838static NODE *
8839heredoc_dedent(struct parser_params *p, NODE *root)
8840{
8841 NODE *node, *str_node, *prev_node;
8842 int indent = p->heredoc_indent;
8843 rb_parser_string_t *prev_lit = 0;
8844
8845 if (indent <= 0) return root;
8846 if (!root) return root;
8847
8848 prev_node = node = str_node = root;
8849 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8850
8851 while (str_node) {
8852 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8853 if (nd_fl_newline(str_node)) {
8854 dedent_string(p, lit, indent);
8855 }
8856 if (!prev_lit) {
8857 prev_lit = lit;
8858 }
8859 else if (!literal_concat0(p, prev_lit, lit)) {
8860 return 0;
8861 }
8862 else {
8863 NODE *end = RNODE_LIST(node)->as.nd_end;
8864 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8865 if (!node) {
8866 if (nd_type_p(prev_node, NODE_DSTR))
8867 nd_set_type(prev_node, NODE_STR);
8868 break;
8869 }
8870 RNODE_LIST(node)->as.nd_end = end;
8871 goto next_str;
8872 }
8873
8874 str_node = 0;
8875 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8876 next_str:
8877 if (!nd_type_p(node, NODE_LIST)) break;
8878 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8879 enum node_type type = nd_type(str_node);
8880 if (type == NODE_STR || type == NODE_DSTR) break;
8881 prev_lit = 0;
8882 str_node = 0;
8883 }
8884 }
8885 }
8886 return root;
8887}
8888
8889static int
8890whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8891{
8892 const char *beg = p->lex.pbeg;
8893 const char *ptr = p->lex.pend;
8894
8895 if (ptr - beg < len) return FALSE;
8896 if (ptr > beg && ptr[-1] == '\n') {
8897 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8898 if (ptr - beg < len) return FALSE;
8899 }
8900 if (strncmp(eos, ptr -= len, len)) return FALSE;
8901 if (indent) {
8902 while (beg < ptr && ISSPACE(*beg)) beg++;
8903 }
8904 return beg == ptr;
8905}
8906
8907static int
8908word_match_p(struct parser_params *p, const char *word, long len)
8909{
8910 if (strncmp(p->lex.pcur, word, len)) return 0;
8911 if (lex_eol_n_p(p, len)) return 1;
8912 int c = (unsigned char)p->lex.pcur[len];
8913 if (ISSPACE(c)) return 1;
8914 switch (c) {
8915 case '\0': case '\004': case '\032': return 1;
8916 }
8917 return 0;
8918}
8919
8920#define NUM_SUFFIX_R (1<<0)
8921#define NUM_SUFFIX_I (1<<1)
8922#define NUM_SUFFIX_ALL 3
8923
8924static int
8925number_literal_suffix(struct parser_params *p, int mask)
8926{
8927 int c, result = 0;
8928 const char *lastp = p->lex.pcur;
8929
8930 while ((c = nextc(p)) != -1) {
8931 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8932 result |= (mask & NUM_SUFFIX_I);
8933 mask &= ~NUM_SUFFIX_I;
8934 /* r after i, rational of complex is disallowed */
8935 mask &= ~NUM_SUFFIX_R;
8936 continue;
8937 }
8938 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8939 result |= (mask & NUM_SUFFIX_R);
8940 mask &= ~NUM_SUFFIX_R;
8941 continue;
8942 }
8943 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8944 p->lex.pcur = lastp;
8945 literal_flush(p, p->lex.pcur);
8946 return 0;
8947 }
8948 pushback(p, c);
8949 break;
8950 }
8951 return result;
8952}
8953
8954static enum yytokentype
8955set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8956{
8957 enum rb_numeric_type numeric_type = integer_literal;
8958
8959 if (type == tFLOAT) {
8960 numeric_type = float_literal;
8961 }
8962
8963 if (suffix & NUM_SUFFIX_R) {
8964 type = tRATIONAL;
8965 numeric_type = rational_literal;
8966 }
8967 if (suffix & NUM_SUFFIX_I) {
8968 type = tIMAGINARY;
8969 }
8970
8971 switch (type) {
8972 case tINTEGER:
8973 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
8974 break;
8975 case tFLOAT:
8976 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
8977 break;
8978 case tRATIONAL:
8979 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
8980 break;
8981 case tIMAGINARY:
8982 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
8983 (void)numeric_type; /* for ripper */
8984 break;
8985 default:
8986 rb_bug("unexpected token: %d", type);
8987 }
8988 SET_LEX_STATE(EXPR_END);
8989 return type;
8990}
8991
8992#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8993static void
8994parser_dispatch_heredoc_end(struct parser_params *p, int line)
8995{
8996 if (has_delayed_token(p))
8997 dispatch_delayed_token(p, tSTRING_CONTENT);
8998
8999#ifdef RIPPER
9000 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
9001 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9002#else
9003 if (p->keep_tokens) {
9004 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9005 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9006 parser_append_tokens(p, str, tHEREDOC_END, line);
9007 }
9008#endif
9009
9010 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9011 lex_goto_eol(p);
9012 token_flush(p);
9013}
9014
9015static enum yytokentype
9016here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9017{
9018 int c, func, indent = 0;
9019 const char *eos, *ptr, *ptr_end;
9020 long len;
9021 rb_parser_string_t *str = 0;
9022 rb_encoding *enc = p->enc;
9023 rb_encoding *base_enc = 0;
9024 int bol;
9025#ifdef RIPPER
9026 VALUE s_value;
9027#endif
9028
9029 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9030 len = here->length;
9031 indent = (func = here->func) & STR_FUNC_INDENT;
9032
9033 if ((c = nextc(p)) == -1) {
9034 error:
9035#ifdef RIPPER
9036 if (!has_delayed_token(p)) {
9037 dispatch_scan_event(p, tSTRING_CONTENT);
9038 }
9039 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9040 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9041 if (!(func & STR_FUNC_REGEXP)) {
9042 int cr = ENC_CODERANGE_UNKNOWN;
9043 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9044 if (cr != ENC_CODERANGE_7BIT &&
9045 rb_is_usascii_enc(p->enc) &&
9046 enc != rb_utf8_encoding()) {
9047 enc = rb_ascii8bit_encoding();
9048 }
9049 }
9050 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9051 }
9052 dispatch_delayed_token(p, tSTRING_CONTENT);
9053 }
9054 else {
9055 dispatch_delayed_token(p, tSTRING_CONTENT);
9056 dispatch_scan_event(p, tSTRING_CONTENT);
9057 }
9058 lex_goto_eol(p);
9059#endif
9060 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9061 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9062 (int)len, eos);
9063 token_flush(p);
9064 SET_LEX_STATE(EXPR_END);
9065 return tSTRING_END;
9066 }
9067 bol = was_bol(p);
9068 if (!bol) {
9069 /* not beginning of line, cannot be the terminator */
9070 }
9071 else if (p->heredoc_line_indent == -1) {
9072 /* `heredoc_line_indent == -1` means
9073 * - "after an interpolation in the same line", or
9074 * - "in a continuing line"
9075 */
9076 p->heredoc_line_indent = 0;
9077 }
9078 else if (whole_match_p(p, eos, len, indent)) {
9079 dispatch_heredoc_end(p);
9080 restore:
9081 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9082 token_flush(p);
9083 SET_LEX_STATE(EXPR_END);
9084 return tSTRING_END;
9085 }
9086
9087 if (!(func & STR_FUNC_EXPAND)) {
9088 do {
9089 ptr = PARSER_STRING_PTR(p->lex.lastline);
9090 ptr_end = p->lex.pend;
9091 if (ptr_end > ptr) {
9092 switch (ptr_end[-1]) {
9093 case '\n':
9094 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9095 ptr_end++;
9096 break;
9097 }
9098 case '\r':
9099 --ptr_end;
9100 }
9101 }
9102
9103 if (p->heredoc_indent > 0) {
9104 long i = 0;
9105 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9106 i++;
9107 p->heredoc_line_indent = 0;
9108 }
9109
9110 if (str)
9111 parser_str_cat(str, ptr, ptr_end - ptr);
9112 else
9113 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9114 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9115 lex_goto_eol(p);
9116 if (p->heredoc_indent > 0) {
9117 goto flush_str;
9118 }
9119 if (nextc(p) == -1) {
9120 if (str) {
9121 rb_parser_string_free(p, str);
9122 str = 0;
9123 }
9124 goto error;
9125 }
9126 } while (!whole_match_p(p, eos, len, indent));
9127 }
9128 else {
9129 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9130 newtok(p);
9131 if (c == '#') {
9132 enum yytokentype t = parser_peek_variable_name(p);
9133 if (p->heredoc_line_indent != -1) {
9134 if (p->heredoc_indent > p->heredoc_line_indent) {
9135 p->heredoc_indent = p->heredoc_line_indent;
9136 }
9137 p->heredoc_line_indent = -1;
9138 }
9139 if (t) return t;
9140 tokadd(p, '#');
9141 c = nextc(p);
9142 }
9143 do {
9144 pushback(p, c);
9145 enc = p->enc;
9146 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9147 if (p->eofp) goto error;
9148 goto restore;
9149 }
9150 if (c != '\n') {
9151 if (c == '\\') p->heredoc_line_indent = -1;
9152 flush:
9153 str = STR_NEW3(tok(p), toklen(p), enc, func);
9154 flush_str:
9155 set_yylval_str(str);
9156#ifndef RIPPER
9157 if (bol) nd_set_fl_newline(yylval.node);
9158#endif
9159 flush_string_content(p, enc, 0);
9160 return tSTRING_CONTENT;
9161 }
9162 tokadd(p, nextc(p));
9163 if (p->heredoc_indent > 0) {
9164 lex_goto_eol(p);
9165 goto flush;
9166 }
9167 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9168 if ((c = nextc(p)) == -1) goto error;
9169 } while (!whole_match_p(p, eos, len, indent));
9170 str = STR_NEW3(tok(p), toklen(p), enc, func);
9171 }
9172 dispatch_heredoc_end(p);
9173 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9174 token_flush(p);
9175 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9176#ifdef RIPPER
9177 /* Preserve s_value for set_yylval_str */
9178 s_value = p->s_value;
9179#endif
9180 set_yylval_str(str);
9181#ifdef RIPPER
9182 set_parser_s_value(s_value);
9183#endif
9184
9185#ifndef RIPPER
9186 if (bol) nd_set_fl_newline(yylval.node);
9187#endif
9188 return tSTRING_CONTENT;
9189}
9190
9191#include "lex.c"
9192
9193static int
9194arg_ambiguous(struct parser_params *p, char c)
9195{
9196#ifndef RIPPER
9197 if (c == '/') {
9198 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after '%c' operator", WARN_I(c));
9199 }
9200 else {
9201 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9202 }
9203#else
9204 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9205#endif
9206 return TRUE;
9207}
9208
9209/* returns true value if formal argument error;
9210 * Qtrue, or error message if ripper */
9211static VALUE
9212formal_argument_error(struct parser_params *p, ID id)
9213{
9214 switch (id_type(id)) {
9215 case ID_LOCAL:
9216 break;
9217#ifndef RIPPER
9218# define ERR(mesg) (yyerror0(mesg), Qtrue)
9219#else
9220# define ERR(mesg) WARN_S(mesg)
9221#endif
9222 case ID_CONST:
9223 return ERR("formal argument cannot be a constant");
9224 case ID_INSTANCE:
9225 return ERR("formal argument cannot be an instance variable");
9226 case ID_GLOBAL:
9227 return ERR("formal argument cannot be a global variable");
9228 case ID_CLASS:
9229 return ERR("formal argument cannot be a class variable");
9230 default:
9231 return ERR("formal argument must be local variable");
9232#undef ERR
9233 }
9234 shadowing_lvar(p, id);
9235
9236 return Qfalse;
9237}
9238
9239static int
9240lvar_defined(struct parser_params *p, ID id)
9241{
9242 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9243}
9244
9245/* emacsen -*- hack */
9246static long
9247parser_encode_length(struct parser_params *p, const char *name, long len)
9248{
9249 long nlen;
9250
9251 if (len > 5 && name[nlen = len - 5] == '-') {
9252 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9253 return nlen;
9254 }
9255 if (len > 4 && name[nlen = len - 4] == '-') {
9256 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9257 return nlen;
9258 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9259 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9260 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9261 return nlen;
9262 }
9263 return len;
9264}
9265
9266static void
9267parser_set_encode(struct parser_params *p, const char *name)
9268{
9269 rb_encoding *enc;
9270 VALUE excargs[3];
9271 int idx = 0;
9272
9273 const char *wrong = 0;
9274 switch (*name) {
9275 case 'e': case 'E': wrong = "external"; break;
9276 case 'i': case 'I': wrong = "internal"; break;
9277 case 'f': case 'F': wrong = "filesystem"; break;
9278 case 'l': case 'L': wrong = "locale"; break;
9279 }
9280 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9281 idx = rb_enc_find_index(name);
9282 if (idx < 0) {
9283 unknown:
9284 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9285 error:
9286 excargs[0] = rb_eArgError;
9287 excargs[2] = rb_make_backtrace();
9288 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9289 VALUE exc = rb_make_exception(3, excargs);
9290 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9291
9292 rb_ast_free(p->ast);
9293 p->ast = NULL;
9294
9295 rb_exc_raise(exc);
9296 }
9297 enc = rb_enc_from_index(idx);
9298 if (!rb_enc_asciicompat(enc)) {
9299 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9300 goto error;
9301 }
9302 p->enc = enc;
9303#ifndef RIPPER
9304 if (p->debug_lines) {
9305 long i;
9306 for (i = 0; i < p->debug_lines->len; i++) {
9307 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9308 }
9309 }
9310#endif
9311}
9312
9313static bool
9314comment_at_top(struct parser_params *p)
9315{
9316 if (p->token_seen) return false;
9317 return (p->line_count == (p->has_shebang ? 2 : 1));
9318}
9319
9320typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9321typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9322
9323static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9324
9325static void
9326magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9327{
9328 if (!comment_at_top(p)) {
9329 return;
9330 }
9331 parser_set_encode(p, val);
9332}
9333
9334static int
9335parser_get_bool(struct parser_params *p, const char *name, const char *val)
9336{
9337 switch (*val) {
9338 case 't': case 'T':
9339 if (STRCASECMP(val, "true") == 0) {
9340 return TRUE;
9341 }
9342 break;
9343 case 'f': case 'F':
9344 if (STRCASECMP(val, "false") == 0) {
9345 return FALSE;
9346 }
9347 break;
9348 }
9349 return parser_invalid_pragma_value(p, name, val);
9350}
9351
9352static int
9353parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9354{
9355 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9356 return -1;
9357}
9358
9359static void
9360parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9361{
9362 int b = parser_get_bool(p, name, val);
9363 if (b >= 0) p->token_info_enabled = b;
9364}
9365
9366static void
9367parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9368{
9369 int b;
9370
9371 if (p->token_seen) {
9372 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9373 return;
9374 }
9375
9376 b = parser_get_bool(p, name, val);
9377 if (b < 0) return;
9378
9379 p->frozen_string_literal = b;
9380}
9381
9382static void
9383parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9384{
9385 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9386 if (*s == ' ' || *s == '\t') continue;
9387 if (*s == '#') break;
9388 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9389 return;
9390 }
9391
9392 switch (*val) {
9393 case 'n': case 'N':
9394 if (STRCASECMP(val, "none") == 0) {
9395 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9396 return;
9397 }
9398 break;
9399 case 'l': case 'L':
9400 if (STRCASECMP(val, "literal") == 0) {
9401 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9402 return;
9403 }
9404 break;
9405 case 'e': case 'E':
9406 if (STRCASECMP(val, "experimental_copy") == 0) {
9407 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9408 return;
9409 }
9410 if (STRCASECMP(val, "experimental_everything") == 0) {
9411 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9412 return;
9413 }
9414 break;
9415 }
9416 parser_invalid_pragma_value(p, name, val);
9417}
9418
9419# if WARN_PAST_SCOPE
9420static void
9421parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9422{
9423 int b = parser_get_bool(p, name, val);
9424 if (b >= 0) p->past_scope_enabled = b;
9425}
9426# endif
9427
9428struct magic_comment {
9429 const char *name;
9430 rb_magic_comment_setter_t func;
9431 rb_magic_comment_length_t length;
9432};
9433
9434static const struct magic_comment magic_comments[] = {
9435 {"coding", magic_comment_encoding, parser_encode_length},
9436 {"encoding", magic_comment_encoding, parser_encode_length},
9437 {"frozen_string_literal", parser_set_frozen_string_literal},
9438 {"shareable_constant_value", parser_set_shareable_constant_value},
9439 {"warn_indent", parser_set_token_info},
9440# if WARN_PAST_SCOPE
9441 {"warn_past_scope", parser_set_past_scope},
9442# endif
9443};
9444
9445static const char *
9446magic_comment_marker(const char *str, long len)
9447{
9448 long i = 2;
9449
9450 while (i < len) {
9451 switch (str[i]) {
9452 case '-':
9453 if (str[i-1] == '*' && str[i-2] == '-') {
9454 return str + i + 1;
9455 }
9456 i += 2;
9457 break;
9458 case '*':
9459 if (i + 1 >= len) return 0;
9460 if (str[i+1] != '-') {
9461 i += 4;
9462 }
9463 else if (str[i-1] != '-') {
9464 i += 2;
9465 }
9466 else {
9467 return str + i + 2;
9468 }
9469 break;
9470 default:
9471 i += 3;
9472 break;
9473 }
9474 }
9475 return 0;
9476}
9477
9478static int
9479parser_magic_comment(struct parser_params *p, const char *str, long len)
9480{
9481 int indicator = 0;
9482 VALUE name = 0, val = 0;
9483 const char *beg, *end, *vbeg, *vend;
9484#define str_copy(_s, _p, _n) ((_s) \
9485 ? (void)(rb_str_resize((_s), (_n)), \
9486 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9487 : (void)((_s) = STR_NEW((_p), (_n))))
9488
9489 if (len <= 7) return FALSE;
9490 if (!!(beg = magic_comment_marker(str, len))) {
9491 if (!(end = magic_comment_marker(beg, str + len - beg)))
9492 return FALSE;
9493 indicator = TRUE;
9494 str = beg;
9495 len = end - beg - 3;
9496 }
9497
9498 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9499 while (len > 0) {
9500 const struct magic_comment *mc = magic_comments;
9501 char *s;
9502 int i;
9503 long n = 0;
9504
9505 for (; len > 0 && *str; str++, --len) {
9506 switch (*str) {
9507 case '\'': case '"': case ':': case ';':
9508 continue;
9509 }
9510 if (!ISSPACE(*str)) break;
9511 }
9512 for (beg = str; len > 0; str++, --len) {
9513 switch (*str) {
9514 case '\'': case '"': case ':': case ';':
9515 break;
9516 default:
9517 if (ISSPACE(*str)) break;
9518 continue;
9519 }
9520 break;
9521 }
9522 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9523 if (!len) break;
9524 if (*str != ':') {
9525 if (!indicator) return FALSE;
9526 continue;
9527 }
9528
9529 do str++; while (--len > 0 && ISSPACE(*str));
9530 if (!len) break;
9531 const char *tok_beg = str;
9532 if (*str == '"') {
9533 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9534 if (*str == '\\') {
9535 --len;
9536 ++str;
9537 }
9538 }
9539 vend = str;
9540 if (len) {
9541 --len;
9542 ++str;
9543 }
9544 }
9545 else {
9546 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9547 vend = str;
9548 }
9549 const char *tok_end = str;
9550 if (indicator) {
9551 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9552 }
9553 else {
9554 while (len > 0 && (ISSPACE(*str))) --len, str++;
9555 if (len) return FALSE;
9556 }
9557
9558 n = end - beg;
9559 str_copy(name, beg, n);
9560 s = RSTRING_PTR(name);
9561 for (i = 0; i < n; ++i) {
9562 if (s[i] == '-') s[i] = '_';
9563 }
9564 do {
9565 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9566 n = vend - vbeg;
9567 if (mc->length) {
9568 n = (*mc->length)(p, vbeg, n);
9569 }
9570 str_copy(val, vbeg, n);
9571 p->lex.ptok = tok_beg;
9572 p->lex.pcur = tok_end;
9573 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9574 break;
9575 }
9576 } while (++mc < magic_comments + numberof(magic_comments));
9577#ifdef RIPPER
9578 str_copy(val, vbeg, vend - vbeg);
9579 dispatch2(magic_comment, name, val);
9580#endif
9581 }
9582
9583 return TRUE;
9584}
9585
9586static void
9587set_file_encoding(struct parser_params *p, const char *str, const char *send)
9588{
9589 int sep = 0;
9590 const char *beg = str;
9591 VALUE s;
9592
9593 for (;;) {
9594 if (send - str <= 6) return;
9595 switch (str[6]) {
9596 case 'C': case 'c': str += 6; continue;
9597 case 'O': case 'o': str += 5; continue;
9598 case 'D': case 'd': str += 4; continue;
9599 case 'I': case 'i': str += 3; continue;
9600 case 'N': case 'n': str += 2; continue;
9601 case 'G': case 'g': str += 1; continue;
9602 case '=': case ':':
9603 sep = 1;
9604 str += 6;
9605 break;
9606 default:
9607 str += 6;
9608 if (ISSPACE(*str)) break;
9609 continue;
9610 }
9611 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9612 sep = 0;
9613 }
9614 for (;;) {
9615 do {
9616 if (++str >= send) return;
9617 } while (ISSPACE(*str));
9618 if (sep) break;
9619 if (*str != '=' && *str != ':') return;
9620 sep = 1;
9621 str++;
9622 }
9623 beg = str;
9624 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9625 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9626 p->lex.ptok = beg;
9627 p->lex.pcur = str;
9628 parser_set_encode(p, RSTRING_PTR(s));
9629 rb_str_resize(s, 0);
9630}
9631
9632static void
9633parser_prepare(struct parser_params *p)
9634{
9635 int c = nextc0(p, FALSE);
9636 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9637 switch (c) {
9638 case '#':
9639 if (peek(p, '!')) p->has_shebang = 1;
9640 break;
9641 case 0xef: /* UTF-8 BOM marker */
9642 if (!lex_eol_n_p(p, 2) &&
9643 (unsigned char)p->lex.pcur[0] == 0xbb &&
9644 (unsigned char)p->lex.pcur[1] == 0xbf) {
9645 p->enc = rb_utf8_encoding();
9646 p->lex.pcur += 2;
9647#ifndef RIPPER
9648 if (p->debug_lines) {
9649 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9650 }
9651#endif
9652 p->lex.pbeg = p->lex.pcur;
9653 token_flush(p);
9654 return;
9655 }
9656 break;
9657 case -1: /* end of script. */
9658 return;
9659 }
9660 pushback(p, c);
9661 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9662}
9663
9664#ifndef RIPPER
9665#define ambiguous_operator(tok, op, syn) ( \
9666 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9667 rb_warning0("even though it seems like "syn""))
9668#else
9669#define ambiguous_operator(tok, op, syn) \
9670 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9671#endif
9672#define warn_balanced(tok, op, syn) ((void) \
9673 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9674 space_seen && !ISSPACE(c) && \
9675 (ambiguous_operator(tok, op, syn), 0)), \
9676 (enum yytokentype)(tok))
9677
9678static enum yytokentype
9679no_digits(struct parser_params *p)
9680{
9681 yyerror0("numeric literal without digits");
9682 if (peek(p, '_')) nextc(p);
9683 /* dummy 0, for tUMINUS_NUM at numeric */
9684 return set_number_literal(p, tINTEGER, 0, 10, 0);
9685}
9686
9687static enum yytokentype
9688parse_numeric(struct parser_params *p, int c)
9689{
9690 int is_float, seen_point, seen_e, nondigit;
9691 int suffix;
9692
9693 is_float = seen_point = seen_e = nondigit = 0;
9694 SET_LEX_STATE(EXPR_END);
9695 newtok(p);
9696 if (c == '-' || c == '+') {
9697 tokadd(p, c);
9698 c = nextc(p);
9699 }
9700 if (c == '0') {
9701 int start = toklen(p);
9702 c = nextc(p);
9703 if (c == 'x' || c == 'X') {
9704 /* hexadecimal */
9705 c = nextc(p);
9706 if (c != -1 && ISXDIGIT(c)) {
9707 do {
9708 if (c == '_') {
9709 if (nondigit) break;
9710 nondigit = c;
9711 continue;
9712 }
9713 if (!ISXDIGIT(c)) break;
9714 nondigit = 0;
9715 tokadd(p, c);
9716 } while ((c = nextc(p)) != -1);
9717 }
9718 pushback(p, c);
9719 tokfix(p);
9720 if (toklen(p) == start) {
9721 return no_digits(p);
9722 }
9723 else if (nondigit) goto trailing_uc;
9724 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9725 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9726 }
9727 if (c == 'b' || c == 'B') {
9728 /* binary */
9729 c = nextc(p);
9730 if (c == '0' || c == '1') {
9731 do {
9732 if (c == '_') {
9733 if (nondigit) break;
9734 nondigit = c;
9735 continue;
9736 }
9737 if (c != '0' && c != '1') break;
9738 nondigit = 0;
9739 tokadd(p, c);
9740 } while ((c = nextc(p)) != -1);
9741 }
9742 pushback(p, c);
9743 tokfix(p);
9744 if (toklen(p) == start) {
9745 return no_digits(p);
9746 }
9747 else if (nondigit) goto trailing_uc;
9748 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9749 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9750 }
9751 if (c == 'd' || c == 'D') {
9752 /* decimal */
9753 c = nextc(p);
9754 if (c != -1 && ISDIGIT(c)) {
9755 do {
9756 if (c == '_') {
9757 if (nondigit) break;
9758 nondigit = c;
9759 continue;
9760 }
9761 if (!ISDIGIT(c)) break;
9762 nondigit = 0;
9763 tokadd(p, c);
9764 } while ((c = nextc(p)) != -1);
9765 }
9766 pushback(p, c);
9767 tokfix(p);
9768 if (toklen(p) == start) {
9769 return no_digits(p);
9770 }
9771 else if (nondigit) goto trailing_uc;
9772 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9773 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9774 }
9775 if (c == '_') {
9776 /* 0_0 */
9777 goto octal_number;
9778 }
9779 if (c == 'o' || c == 'O') {
9780 /* prefixed octal */
9781 c = nextc(p);
9782 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9783 tokfix(p);
9784 return no_digits(p);
9785 }
9786 }
9787 if (c >= '0' && c <= '7') {
9788 /* octal */
9789 octal_number:
9790 do {
9791 if (c == '_') {
9792 if (nondigit) break;
9793 nondigit = c;
9794 continue;
9795 }
9796 if (c < '0' || c > '9') break;
9797 if (c > '7') goto invalid_octal;
9798 nondigit = 0;
9799 tokadd(p, c);
9800 } while ((c = nextc(p)) != -1);
9801 if (toklen(p) > start) {
9802 pushback(p, c);
9803 tokfix(p);
9804 if (nondigit) goto trailing_uc;
9805 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9806 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9807 }
9808 if (nondigit) {
9809 pushback(p, c);
9810 goto trailing_uc;
9811 }
9812 }
9813 if (c > '7' && c <= '9') {
9814 invalid_octal:
9815 yyerror0("Invalid octal digit");
9816 }
9817 else if (c == '.' || c == 'e' || c == 'E') {
9818 tokadd(p, '0');
9819 }
9820 else {
9821 pushback(p, c);
9822 tokfix(p);
9823 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9824 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9825 }
9826 }
9827
9828 for (;;) {
9829 switch (c) {
9830 case '0': case '1': case '2': case '3': case '4':
9831 case '5': case '6': case '7': case '8': case '9':
9832 nondigit = 0;
9833 tokadd(p, c);
9834 break;
9835
9836 case '.':
9837 if (nondigit) goto trailing_uc;
9838 if (seen_point || seen_e) {
9839 goto decode_num;
9840 }
9841 else {
9842 int c0 = nextc(p);
9843 if (c0 == -1 || !ISDIGIT(c0)) {
9844 pushback(p, c0);
9845 goto decode_num;
9846 }
9847 c = c0;
9848 }
9849 seen_point = toklen(p);
9850 tokadd(p, '.');
9851 tokadd(p, c);
9852 is_float++;
9853 nondigit = 0;
9854 break;
9855
9856 case 'e':
9857 case 'E':
9858 if (nondigit) {
9859 pushback(p, c);
9860 c = nondigit;
9861 goto decode_num;
9862 }
9863 if (seen_e) {
9864 goto decode_num;
9865 }
9866 nondigit = c;
9867 c = nextc(p);
9868 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9869 pushback(p, c);
9870 c = nondigit;
9871 nondigit = 0;
9872 goto decode_num;
9873 }
9874 tokadd(p, nondigit);
9875 seen_e++;
9876 is_float++;
9877 tokadd(p, c);
9878 nondigit = (c == '-' || c == '+') ? c : 0;
9879 break;
9880
9881 case '_': /* `_' in number just ignored */
9882 if (nondigit) goto decode_num;
9883 nondigit = c;
9884 break;
9885
9886 default:
9887 goto decode_num;
9888 }
9889 c = nextc(p);
9890 }
9891
9892 decode_num:
9893 pushback(p, c);
9894 if (nondigit) {
9895 trailing_uc:
9896 literal_flush(p, p->lex.pcur - 1);
9897 YYLTYPE loc = RUBY_INIT_YYLLOC();
9898 compile_error(p, "trailing '%c' in number", nondigit);
9899 parser_show_error_line(p, &loc);
9900 }
9901 tokfix(p);
9902 if (is_float) {
9903 enum yytokentype type = tFLOAT;
9904
9905 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9906 if (suffix & NUM_SUFFIX_R) {
9907 type = tRATIONAL;
9908 }
9909 else {
9910 strtod(tok(p), 0);
9911 if (errno == ERANGE) {
9912 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9913 errno = 0;
9914 }
9915 }
9916 return set_number_literal(p, type, suffix, 0, seen_point);
9917 }
9918 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9919 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9920}
9921
9922static enum yytokentype
9923parse_qmark(struct parser_params *p, int space_seen)
9924{
9925 rb_encoding *enc;
9926 register int c;
9927 rb_parser_string_t *lit;
9928 const char *start = p->lex.pcur;
9929
9930 if (IS_END()) {
9931 SET_LEX_STATE(EXPR_VALUE);
9932 return '?';
9933 }
9934 c = nextc(p);
9935 if (c == -1) {
9936 compile_error(p, "incomplete character syntax");
9937 return 0;
9938 }
9939 if (rb_enc_isspace(c, p->enc)) {
9940 if (!IS_ARG()) {
9941 int c2 = escaped_control_code(c);
9942 if (c2) {
9943 WARN_SPACE_CHAR(c2, "?");
9944 }
9945 }
9946 ternary:
9947 pushback(p, c);
9948 SET_LEX_STATE(EXPR_VALUE);
9949 return '?';
9950 }
9951 newtok(p);
9952 enc = p->enc;
9953 int w = parser_precise_mbclen(p, start);
9954 if (is_identchar(p, start, p->lex.pend, p->enc) &&
9955 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
9956 if (space_seen) {
9957 const char *ptr = start;
9958 do {
9959 int n = parser_precise_mbclen(p, ptr);
9960 if (n < 0) return -1;
9961 ptr += n;
9962 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9963 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9964 " a conditional operator, put a space after '?'",
9965 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9966 }
9967 goto ternary;
9968 }
9969 else if (c == '\\') {
9970 if (peek(p, 'u')) {
9971 nextc(p);
9972 enc = rb_utf8_encoding();
9973 tokadd_utf8(p, &enc, -1, 0, 0);
9974 }
9975 else if (!ISASCII(c = peekc(p)) && c != -1) {
9976 nextc(p);
9977 if (tokadd_mbchar(p, c) == -1) return 0;
9978 }
9979 else {
9980 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
9981 tokadd(p, c);
9982 }
9983 }
9984 else {
9985 if (tokadd_mbchar(p, c) == -1) return 0;
9986 }
9987 tokfix(p);
9988 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9989 set_yylval_str(lit);
9990 SET_LEX_STATE(EXPR_END);
9991 return tCHAR;
9992}
9993
9994static enum yytokentype
9995parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9996{
9997 register int c;
9998 const char *ptok = p->lex.pcur;
9999
10000 if (IS_BEG()) {
10001 int term;
10002 int paren;
10003
10004 c = nextc(p);
10005 quotation:
10006 if (c == -1) goto unterminated;
10007 if (!ISALNUM(c)) {
10008 term = c;
10009 if (!ISASCII(c)) goto unknown;
10010 c = 'Q';
10011 }
10012 else {
10013 term = nextc(p);
10014 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10015 unknown:
10016 pushback(p, term);
10017 c = parser_precise_mbclen(p, p->lex.pcur);
10018 if (c < 0) return 0;
10019 p->lex.pcur += c;
10020 yyerror0("unknown type of %string");
10021 return 0;
10022 }
10023 }
10024 if (term == -1) {
10025 unterminated:
10026 compile_error(p, "unterminated quoted string meets end of file");
10027 return 0;
10028 }
10029 paren = term;
10030 if (term == '(') term = ')';
10031 else if (term == '[') term = ']';
10032 else if (term == '{') term = '}';
10033 else if (term == '<') term = '>';
10034 else paren = 0;
10035
10036 p->lex.ptok = ptok-1;
10037 switch (c) {
10038 case 'Q':
10039 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10040 return tSTRING_BEG;
10041
10042 case 'q':
10043 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10044 return tSTRING_BEG;
10045
10046 case 'W':
10047 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10048 return tWORDS_BEG;
10049
10050 case 'w':
10051 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10052 return tQWORDS_BEG;
10053
10054 case 'I':
10055 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10056 return tSYMBOLS_BEG;
10057
10058 case 'i':
10059 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10060 return tQSYMBOLS_BEG;
10061
10062 case 'x':
10063 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10064 return tXSTRING_BEG;
10065
10066 case 'r':
10067 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10068 return tREGEXP_BEG;
10069
10070 case 's':
10071 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10072 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10073 return tSYMBEG;
10074
10075 default:
10076 yyerror0("unknown type of %string");
10077 return 0;
10078 }
10079 }
10080 if ((c = nextc(p)) == '=') {
10081 set_yylval_id('%');
10082 SET_LEX_STATE(EXPR_BEG);
10083 return tOP_ASGN;
10084 }
10085 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10086 goto quotation;
10087 }
10088 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10089 pushback(p, c);
10090 return warn_balanced('%', "%%", "string literal");
10091}
10092
10093static int
10094tokadd_ident(struct parser_params *p, int c)
10095{
10096 do {
10097 if (tokadd_mbchar(p, c) == -1) return -1;
10098 c = nextc(p);
10099 } while (parser_is_identchar(p));
10100 pushback(p, c);
10101 return 0;
10102}
10103
10104static ID
10105tokenize_ident(struct parser_params *p)
10106{
10107 ID ident = TOK_INTERN();
10108
10109 set_yylval_name(ident);
10110
10111 return ident;
10112}
10113
10114static int
10115parse_numvar(struct parser_params *p)
10116{
10117 size_t len;
10118 int overflow;
10119 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10120 const unsigned long nth_ref_max =
10121 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10122 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10123 * turned into a Fixnum, in compile.c */
10124
10125 if (overflow || n > nth_ref_max) {
10126 /* compile_error()? */
10127 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10128 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10129 }
10130 else {
10131 return (int)n;
10132 }
10133}
10134
10135static enum yytokentype
10136parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10137{
10138 const char *ptr = p->lex.pcur;
10139 register int c;
10140
10141 SET_LEX_STATE(EXPR_END);
10142 p->lex.ptok = ptr - 1; /* from '$' */
10143 newtok(p);
10144 c = nextc(p);
10145 switch (c) {
10146 case '_': /* $_: last read line string */
10147 c = nextc(p);
10148 if (parser_is_identchar(p)) {
10149 tokadd(p, '$');
10150 tokadd(p, '_');
10151 break;
10152 }
10153 pushback(p, c);
10154 c = '_';
10155 /* fall through */
10156 case '~': /* $~: match-data */
10157 case '*': /* $*: argv */
10158 case '$': /* $$: pid */
10159 case '?': /* $?: last status */
10160 case '!': /* $!: error string */
10161 case '@': /* $@: error position */
10162 case '/': /* $/: input record separator */
10163 case '\\': /* $\: output record separator */
10164 case ';': /* $;: field separator */
10165 case ',': /* $,: output field separator */
10166 case '.': /* $.: last read line number */
10167 case '=': /* $=: ignorecase */
10168 case ':': /* $:: load path */
10169 case '<': /* $<: default input handle */
10170 case '>': /* $>: default output handle */
10171 case '\"': /* $": already loaded files */
10172 tokadd(p, '$');
10173 tokadd(p, c);
10174 goto gvar;
10175
10176 case '-':
10177 tokadd(p, '$');
10178 tokadd(p, c);
10179 c = nextc(p);
10180 if (parser_is_identchar(p)) {
10181 if (tokadd_mbchar(p, c) == -1) return 0;
10182 }
10183 else {
10184 pushback(p, c);
10185 pushback(p, '-');
10186 return '$';
10187 }
10188 gvar:
10189 tokenize_ident(p);
10190 return tGVAR;
10191
10192 case '&': /* $&: last match */
10193 case '`': /* $`: string before last match */
10194 case '\'': /* $': string after last match */
10195 case '+': /* $+: string matches last paren. */
10196 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10197 tokadd(p, '$');
10198 tokadd(p, c);
10199 goto gvar;
10200 }
10201 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10202 return tBACK_REF;
10203
10204 case '1': case '2': case '3':
10205 case '4': case '5': case '6':
10206 case '7': case '8': case '9':
10207 tokadd(p, '$');
10208 do {
10209 tokadd(p, c);
10210 c = nextc(p);
10211 } while (c != -1 && ISDIGIT(c));
10212 pushback(p, c);
10213 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10214 tokfix(p);
10215 c = parse_numvar(p);
10216 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10217 return tNTH_REF;
10218
10219 default:
10220 if (!parser_is_identchar(p)) {
10221 YYLTYPE loc = RUBY_INIT_YYLLOC();
10222 if (c == -1 || ISSPACE(c)) {
10223 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10224 }
10225 else {
10226 pushback(p, c);
10227 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10228 }
10229 parser_show_error_line(p, &loc);
10230 set_yylval_noname();
10231 return tGVAR;
10232 }
10233 /* fall through */
10234 case '0':
10235 tokadd(p, '$');
10236 }
10237
10238 if (tokadd_ident(p, c)) return 0;
10239 SET_LEX_STATE(EXPR_END);
10240 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10241 tokenize_ident(p);
10242 }
10243 else {
10244 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10245 set_yylval_noname();
10246 }
10247 return tGVAR;
10248}
10249
10250static bool
10251parser_numbered_param(struct parser_params *p, int n)
10252{
10253 if (n < 0) return false;
10254
10255 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10256 return false;
10257 }
10258 if (p->max_numparam == ORDINAL_PARAM) {
10259 compile_error(p, "ordinary parameter is defined");
10260 return false;
10261 }
10262 struct vtable *args = p->lvtbl->args;
10263 if (p->max_numparam < n) {
10264 p->max_numparam = n;
10265 }
10266 while (n > args->pos) {
10267 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10268 }
10269 return true;
10270}
10271
10272static enum yytokentype
10273parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10274{
10275 const char *ptr = p->lex.pcur;
10276 enum yytokentype result = tIVAR;
10277 register int c = nextc(p);
10278 YYLTYPE loc;
10279
10280 p->lex.ptok = ptr - 1; /* from '@' */
10281 newtok(p);
10282 tokadd(p, '@');
10283 if (c == '@') {
10284 result = tCVAR;
10285 tokadd(p, '@');
10286 c = nextc(p);
10287 }
10288 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10289 if (c == -1 || !parser_is_identchar(p)) {
10290 pushback(p, c);
10291 RUBY_SET_YYLLOC(loc);
10292 if (result == tIVAR) {
10293 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10294 }
10295 else {
10296 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10297 }
10298 parser_show_error_line(p, &loc);
10299 set_yylval_noname();
10300 SET_LEX_STATE(EXPR_END);
10301 return result;
10302 }
10303 else if (ISDIGIT(c)) {
10304 pushback(p, c);
10305 RUBY_SET_YYLLOC(loc);
10306 if (result == tIVAR) {
10307 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10308 }
10309 else {
10310 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10311 }
10312 parser_show_error_line(p, &loc);
10313 set_yylval_noname();
10314 SET_LEX_STATE(EXPR_END);
10315 return result;
10316 }
10317
10318 if (tokadd_ident(p, c)) return 0;
10319 tokenize_ident(p);
10320 return result;
10321}
10322
10323static enum yytokentype
10324parse_ident(struct parser_params *p, int c, int cmd_state)
10325{
10326 enum yytokentype result;
10327 bool is_ascii = true;
10328 const enum lex_state_e last_state = p->lex.state;
10329 ID ident;
10330 int enforce_keyword_end = 0;
10331
10332 do {
10333 if (!ISASCII(c)) is_ascii = false;
10334 if (tokadd_mbchar(p, c) == -1) return 0;
10335 c = nextc(p);
10336 } while (parser_is_identchar(p));
10337 if ((c == '!' || c == '?') && !peek(p, '=')) {
10338 result = tFID;
10339 tokadd(p, c);
10340 }
10341 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10342 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10343 result = tIDENTIFIER;
10344 tokadd(p, c);
10345 }
10346 else {
10347 result = tCONSTANT; /* assume provisionally */
10348 pushback(p, c);
10349 }
10350 tokfix(p);
10351
10352 if (IS_LABEL_POSSIBLE()) {
10353 if (IS_LABEL_SUFFIX(0)) {
10354 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10355 nextc(p);
10356 tokenize_ident(p);
10357 return tLABEL;
10358 }
10359 }
10360
10361#ifndef RIPPER
10362 if (peek_end_expect_token_locations(p)) {
10363 const rb_code_position_t *end_pos;
10364 int lineno, column;
10365 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10366
10367 end_pos = peek_end_expect_token_locations(p)->pos;
10368 lineno = end_pos->lineno;
10369 column = end_pos->column;
10370
10371 if (p->debug) {
10372 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10373 p->ruby_sourceline, beg_pos, lineno, column);
10374 }
10375
10376 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10377 const struct kwtable *kw;
10378
10379 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10380 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10381 enforce_keyword_end = 1;
10382 }
10383 }
10384 }
10385#endif
10386
10387 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10388 const struct kwtable *kw;
10389
10390 /* See if it is a reserved word. */
10391 kw = rb_reserved_word(tok(p), toklen(p));
10392 if (kw) {
10393 enum lex_state_e state = p->lex.state;
10394 if (IS_lex_state_for(state, EXPR_FNAME)) {
10395 SET_LEX_STATE(EXPR_ENDFN);
10396 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10397 return kw->id[0];
10398 }
10399 SET_LEX_STATE(kw->state);
10400 if (IS_lex_state(EXPR_BEG)) {
10401 p->command_start = TRUE;
10402 }
10403 if (kw->id[0] == keyword_do) {
10404 if (lambda_beginning_p()) {
10405 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10406 return keyword_do_LAMBDA;
10407 }
10408 if (COND_P()) return keyword_do_cond;
10409 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10410 return keyword_do_block;
10411 return keyword_do;
10412 }
10413 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10414 return kw->id[0];
10415 else {
10416 if (kw->id[0] != kw->id[1])
10417 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10418 return kw->id[1];
10419 }
10420 }
10421 }
10422
10423 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10424 if (cmd_state) {
10425 SET_LEX_STATE(EXPR_CMDARG);
10426 }
10427 else {
10428 SET_LEX_STATE(EXPR_ARG);
10429 }
10430 }
10431 else if (p->lex.state == EXPR_FNAME) {
10432 SET_LEX_STATE(EXPR_ENDFN);
10433 }
10434 else {
10435 SET_LEX_STATE(EXPR_END);
10436 }
10437
10438 ident = tokenize_ident(p);
10439 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10440 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10441 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10442 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10443 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10444 }
10445 return result;
10446}
10447
10448static void
10449warn_cr(struct parser_params *p)
10450{
10451 if (!p->cr_seen) {
10452 p->cr_seen = TRUE;
10453 /* carried over with p->lex.nextline for nextc() */
10454 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10455 }
10456}
10457
10458static enum yytokentype
10459parser_yylex(struct parser_params *p)
10460{
10461 register int c;
10462 int space_seen = 0;
10463 int cmd_state;
10464 int label;
10465 enum lex_state_e last_state;
10466 int fallthru = FALSE;
10467 int token_seen = p->token_seen;
10468
10469 if (p->lex.strterm) {
10470 if (strterm_is_heredoc(p->lex.strterm)) {
10471 token_flush(p);
10472 return here_document(p, &p->lex.strterm->u.heredoc);
10473 }
10474 else {
10475 token_flush(p);
10476 return parse_string(p, &p->lex.strterm->u.literal);
10477 }
10478 }
10479 cmd_state = p->command_start;
10480 p->command_start = FALSE;
10481 p->token_seen = TRUE;
10482#ifndef RIPPER
10483 token_flush(p);
10484#endif
10485 retry:
10486 last_state = p->lex.state;
10487 switch (c = nextc(p)) {
10488 case '\0': /* NUL */
10489 case '\004': /* ^D */
10490 case '\032': /* ^Z */
10491 case -1: /* end of script. */
10492 p->eofp = 1;
10493#ifndef RIPPER
10494 if (p->end_expect_token_locations) {
10495 pop_end_expect_token_locations(p);
10496 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10497 return tDUMNY_END;
10498 }
10499#endif
10500 /* Set location for end-of-input because dispatch_scan_event is not called. */
10501 RUBY_SET_YYLLOC(*p->yylloc);
10502 return END_OF_INPUT;
10503
10504 /* white spaces */
10505 case '\r':
10506 warn_cr(p);
10507 /* fall through */
10508 case ' ': case '\t': case '\f':
10509 case '\13': /* '\v' */
10510 space_seen = 1;
10511 while ((c = nextc(p))) {
10512 switch (c) {
10513 case '\r':
10514 warn_cr(p);
10515 /* fall through */
10516 case ' ': case '\t': case '\f':
10517 case '\13': /* '\v' */
10518 break;
10519 default:
10520 goto outofloop;
10521 }
10522 }
10523 outofloop:
10524 pushback(p, c);
10525 dispatch_scan_event(p, tSP);
10526#ifndef RIPPER
10527 token_flush(p);
10528#endif
10529 goto retry;
10530
10531 case '#': /* it's a comment */
10532 p->token_seen = token_seen;
10533 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10534 /* no magic_comment in shebang line */
10535 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10536 if (comment_at_top(p)) {
10537 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10538 }
10539 }
10540 p->lex.pcur = pcur, p->lex.ptok = ptok;
10541 lex_goto_eol(p);
10542 dispatch_scan_event(p, tCOMMENT);
10543 fallthru = TRUE;
10544 /* fall through */
10545 case '\n':
10546 p->token_seen = token_seen;
10547 rb_parser_string_t *prevline = p->lex.lastline;
10548 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10549 !IS_lex_state(EXPR_LABELED));
10550 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10551 if (!fallthru) {
10552 dispatch_scan_event(p, tIGNORED_NL);
10553 }
10554 fallthru = FALSE;
10555 if (!c && p->ctxt.in_kwarg) {
10556 goto normal_newline;
10557 }
10558 goto retry;
10559 }
10560 while (1) {
10561 switch (c = nextc(p)) {
10562 case ' ': case '\t': case '\f': case '\r':
10563 case '\13': /* '\v' */
10564 space_seen = 1;
10565 break;
10566 case '#':
10567 pushback(p, c);
10568 if (space_seen) {
10569 dispatch_scan_event(p, tSP);
10570 token_flush(p);
10571 }
10572 goto retry;
10573 case '&':
10574 case '.': {
10575 dispatch_delayed_token(p, tIGNORED_NL);
10576 if (peek(p, '.') == (c == '&')) {
10577 pushback(p, c);
10578 dispatch_scan_event(p, tSP);
10579 goto retry;
10580 }
10581 }
10582 default:
10583 p->ruby_sourceline--;
10584 p->lex.nextline = p->lex.lastline;
10585 set_lastline(p, prevline);
10586 case -1: /* EOF no decrement*/
10587 if (c == -1 && space_seen) {
10588 dispatch_scan_event(p, tSP);
10589 }
10590 lex_goto_eol(p);
10591 if (c != -1) {
10592 token_flush(p);
10593 RUBY_SET_YYLLOC(*p->yylloc);
10594 }
10595 goto normal_newline;
10596 }
10597 }
10598 normal_newline:
10599 p->command_start = TRUE;
10600 SET_LEX_STATE(EXPR_BEG);
10601 return '\n';
10602
10603 case '*':
10604 if ((c = nextc(p)) == '*') {
10605 if ((c = nextc(p)) == '=') {
10606 set_yylval_id(idPow);
10607 SET_LEX_STATE(EXPR_BEG);
10608 return tOP_ASGN;
10609 }
10610 pushback(p, c);
10611 if (IS_SPCARG(c)) {
10612 rb_warning0("'**' interpreted as argument prefix");
10613 c = tDSTAR;
10614 }
10615 else if (IS_BEG()) {
10616 c = tDSTAR;
10617 }
10618 else {
10619 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10620 }
10621 }
10622 else {
10623 if (c == '=') {
10624 set_yylval_id('*');
10625 SET_LEX_STATE(EXPR_BEG);
10626 return tOP_ASGN;
10627 }
10628 pushback(p, c);
10629 if (IS_SPCARG(c)) {
10630 rb_warning0("'*' interpreted as argument prefix");
10631 c = tSTAR;
10632 }
10633 else if (IS_BEG()) {
10634 c = tSTAR;
10635 }
10636 else {
10637 c = warn_balanced('*', "*", "argument prefix");
10638 }
10639 }
10640 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10641 return c;
10642
10643 case '!':
10644 c = nextc(p);
10645 if (IS_AFTER_OPERATOR()) {
10646 SET_LEX_STATE(EXPR_ARG);
10647 if (c == '@') {
10648 return '!';
10649 }
10650 }
10651 else {
10652 SET_LEX_STATE(EXPR_BEG);
10653 }
10654 if (c == '=') {
10655 return tNEQ;
10656 }
10657 if (c == '~') {
10658 return tNMATCH;
10659 }
10660 pushback(p, c);
10661 return '!';
10662
10663 case '=':
10664 if (was_bol(p)) {
10665 /* skip embedded rd document */
10666 if (word_match_p(p, "begin", 5)) {
10667 int first_p = TRUE;
10668
10669 lex_goto_eol(p);
10670 dispatch_scan_event(p, tEMBDOC_BEG);
10671 for (;;) {
10672 lex_goto_eol(p);
10673 if (!first_p) {
10674 dispatch_scan_event(p, tEMBDOC);
10675 }
10676 first_p = FALSE;
10677 c = nextc(p);
10678 if (c == -1) {
10679 compile_error(p, "embedded document meets end of file");
10680 return END_OF_INPUT;
10681 }
10682 if (c == '=' && word_match_p(p, "end", 3)) {
10683 break;
10684 }
10685 pushback(p, c);
10686 }
10687 lex_goto_eol(p);
10688 dispatch_scan_event(p, tEMBDOC_END);
10689 goto retry;
10690 }
10691 }
10692
10693 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10694 if ((c = nextc(p)) == '=') {
10695 if ((c = nextc(p)) == '=') {
10696 return tEQQ;
10697 }
10698 pushback(p, c);
10699 return tEQ;
10700 }
10701 if (c == '~') {
10702 return tMATCH;
10703 }
10704 else if (c == '>') {
10705 return tASSOC;
10706 }
10707 pushback(p, c);
10708 return '=';
10709
10710 case '<':
10711 c = nextc(p);
10712 if (c == '<' &&
10713 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10714 !IS_END() &&
10715 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10716 enum yytokentype token = heredoc_identifier(p);
10717 if (token) return token < 0 ? 0 : token;
10718 }
10719 if (IS_AFTER_OPERATOR()) {
10720 SET_LEX_STATE(EXPR_ARG);
10721 }
10722 else {
10723 if (IS_lex_state(EXPR_CLASS))
10724 p->command_start = TRUE;
10725 SET_LEX_STATE(EXPR_BEG);
10726 }
10727 if (c == '=') {
10728 if ((c = nextc(p)) == '>') {
10729 return tCMP;
10730 }
10731 pushback(p, c);
10732 return tLEQ;
10733 }
10734 if (c == '<') {
10735 if ((c = nextc(p)) == '=') {
10736 set_yylval_id(idLTLT);
10737 SET_LEX_STATE(EXPR_BEG);
10738 return tOP_ASGN;
10739 }
10740 pushback(p, c);
10741 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10742 }
10743 pushback(p, c);
10744 return '<';
10745
10746 case '>':
10747 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10748 if ((c = nextc(p)) == '=') {
10749 return tGEQ;
10750 }
10751 if (c == '>') {
10752 if ((c = nextc(p)) == '=') {
10753 set_yylval_id(idGTGT);
10754 SET_LEX_STATE(EXPR_BEG);
10755 return tOP_ASGN;
10756 }
10757 pushback(p, c);
10758 return tRSHFT;
10759 }
10760 pushback(p, c);
10761 return '>';
10762
10763 case '"':
10764 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10765 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10766 p->lex.ptok = p->lex.pcur-1;
10767 return tSTRING_BEG;
10768
10769 case '`':
10770 if (IS_lex_state(EXPR_FNAME)) {
10771 SET_LEX_STATE(EXPR_ENDFN);
10772 return c;
10773 }
10774 if (IS_lex_state(EXPR_DOT)) {
10775 if (cmd_state)
10776 SET_LEX_STATE(EXPR_CMDARG);
10777 else
10778 SET_LEX_STATE(EXPR_ARG);
10779 return c;
10780 }
10781 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10782 return tXSTRING_BEG;
10783
10784 case '\'':
10785 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10786 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10787 p->lex.ptok = p->lex.pcur-1;
10788 return tSTRING_BEG;
10789
10790 case '?':
10791 return parse_qmark(p, space_seen);
10792
10793 case '&':
10794 if ((c = nextc(p)) == '&') {
10795 SET_LEX_STATE(EXPR_BEG);
10796 if ((c = nextc(p)) == '=') {
10797 set_yylval_id(idANDOP);
10798 SET_LEX_STATE(EXPR_BEG);
10799 return tOP_ASGN;
10800 }
10801 pushback(p, c);
10802 return tANDOP;
10803 }
10804 else if (c == '=') {
10805 set_yylval_id('&');
10806 SET_LEX_STATE(EXPR_BEG);
10807 return tOP_ASGN;
10808 }
10809 else if (c == '.') {
10810 set_yylval_id(idANDDOT);
10811 SET_LEX_STATE(EXPR_DOT);
10812 return tANDDOT;
10813 }
10814 pushback(p, c);
10815 if (IS_SPCARG(c)) {
10816 if ((c != ':') ||
10817 (c = peekc_n(p, 1)) == -1 ||
10818 !(c == '\'' || c == '"' ||
10819 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10820 rb_warning0("'&' interpreted as argument prefix");
10821 }
10822 c = tAMPER;
10823 }
10824 else if (IS_BEG()) {
10825 c = tAMPER;
10826 }
10827 else {
10828 c = warn_balanced('&', "&", "argument prefix");
10829 }
10830 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10831 return c;
10832
10833 case '|':
10834 if ((c = nextc(p)) == '|') {
10835 SET_LEX_STATE(EXPR_BEG);
10836 if ((c = nextc(p)) == '=') {
10837 set_yylval_id(idOROP);
10838 SET_LEX_STATE(EXPR_BEG);
10839 return tOP_ASGN;
10840 }
10841 pushback(p, c);
10842 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10843 c = '|';
10844 pushback(p, '|');
10845 return c;
10846 }
10847 return tOROP;
10848 }
10849 if (c == '=') {
10850 set_yylval_id('|');
10851 SET_LEX_STATE(EXPR_BEG);
10852 return tOP_ASGN;
10853 }
10854 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10855 pushback(p, c);
10856 return '|';
10857
10858 case '+':
10859 c = nextc(p);
10860 if (IS_AFTER_OPERATOR()) {
10861 SET_LEX_STATE(EXPR_ARG);
10862 if (c == '@') {
10863 return tUPLUS;
10864 }
10865 pushback(p, c);
10866 return '+';
10867 }
10868 if (c == '=') {
10869 set_yylval_id('+');
10870 SET_LEX_STATE(EXPR_BEG);
10871 return tOP_ASGN;
10872 }
10873 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10874 SET_LEX_STATE(EXPR_BEG);
10875 pushback(p, c);
10876 if (c != -1 && ISDIGIT(c)) {
10877 return parse_numeric(p, '+');
10878 }
10879 return tUPLUS;
10880 }
10881 SET_LEX_STATE(EXPR_BEG);
10882 pushback(p, c);
10883 return warn_balanced('+', "+", "unary operator");
10884
10885 case '-':
10886 c = nextc(p);
10887 if (IS_AFTER_OPERATOR()) {
10888 SET_LEX_STATE(EXPR_ARG);
10889 if (c == '@') {
10890 return tUMINUS;
10891 }
10892 pushback(p, c);
10893 return '-';
10894 }
10895 if (c == '=') {
10896 set_yylval_id('-');
10897 SET_LEX_STATE(EXPR_BEG);
10898 return tOP_ASGN;
10899 }
10900 if (c == '>') {
10901 SET_LEX_STATE(EXPR_ENDFN);
10902 yylval.num = p->lex.lpar_beg;
10903 p->lex.lpar_beg = p->lex.paren_nest;
10904 return tLAMBDA;
10905 }
10906 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10907 SET_LEX_STATE(EXPR_BEG);
10908 pushback(p, c);
10909 if (c != -1 && ISDIGIT(c)) {
10910 return tUMINUS_NUM;
10911 }
10912 return tUMINUS;
10913 }
10914 SET_LEX_STATE(EXPR_BEG);
10915 pushback(p, c);
10916 return warn_balanced('-', "-", "unary operator");
10917
10918 case '.': {
10919 int is_beg = IS_BEG();
10920 SET_LEX_STATE(EXPR_BEG);
10921 if ((c = nextc(p)) == '.') {
10922 if ((c = nextc(p)) == '.') {
10923 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10924 SET_LEX_STATE(EXPR_ENDARG);
10925 return tBDOT3;
10926 }
10927 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10928 rb_warn0("... at EOL, should be parenthesized?");
10929 }
10930 return is_beg ? tBDOT3 : tDOT3;
10931 }
10932 pushback(p, c);
10933 return is_beg ? tBDOT2 : tDOT2;
10934 }
10935 pushback(p, c);
10936 if (c != -1 && ISDIGIT(c)) {
10937 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10938 parse_numeric(p, '.');
10939 if (ISDIGIT(prev)) {
10940 yyerror0("unexpected fraction part after numeric literal");
10941 }
10942 else {
10943 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10944 }
10945 SET_LEX_STATE(EXPR_END);
10946 p->lex.ptok = p->lex.pcur;
10947 goto retry;
10948 }
10949 set_yylval_id('.');
10950 SET_LEX_STATE(EXPR_DOT);
10951 return '.';
10952 }
10953
10954 case '0': case '1': case '2': case '3': case '4':
10955 case '5': case '6': case '7': case '8': case '9':
10956 return parse_numeric(p, c);
10957
10958 case ')':
10959 COND_POP();
10960 CMDARG_POP();
10961 SET_LEX_STATE(EXPR_ENDFN);
10962 p->lex.paren_nest--;
10963 return c;
10964
10965 case ']':
10966 COND_POP();
10967 CMDARG_POP();
10968 SET_LEX_STATE(EXPR_END);
10969 p->lex.paren_nest--;
10970 return c;
10971
10972 case '}':
10973 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10974 if (!p->lex.brace_nest--) return tSTRING_DEND;
10975 COND_POP();
10976 CMDARG_POP();
10977 SET_LEX_STATE(EXPR_END);
10978 p->lex.paren_nest--;
10979 return c;
10980
10981 case ':':
10982 c = nextc(p);
10983 if (c == ':') {
10984 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10985 SET_LEX_STATE(EXPR_BEG);
10986 return tCOLON3;
10987 }
10988 set_yylval_id(idCOLON2);
10989 SET_LEX_STATE(EXPR_DOT);
10990 return tCOLON2;
10991 }
10992 if (IS_END() || ISSPACE(c) || c == '#') {
10993 pushback(p, c);
10994 c = warn_balanced(':', ":", "symbol literal");
10995 SET_LEX_STATE(EXPR_BEG);
10996 return c;
10997 }
10998 switch (c) {
10999 case '\'':
11000 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11001 break;
11002 case '"':
11003 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11004 break;
11005 default:
11006 pushback(p, c);
11007 break;
11008 }
11009 SET_LEX_STATE(EXPR_FNAME);
11010 return tSYMBEG;
11011
11012 case '/':
11013 if (IS_BEG()) {
11014 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11015 return tREGEXP_BEG;
11016 }
11017 if ((c = nextc(p)) == '=') {
11018 set_yylval_id('/');
11019 SET_LEX_STATE(EXPR_BEG);
11020 return tOP_ASGN;
11021 }
11022 pushback(p, c);
11023 if (IS_SPCARG(c)) {
11024 arg_ambiguous(p, '/');
11025 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11026 return tREGEXP_BEG;
11027 }
11028 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11029 return warn_balanced('/', "/", "regexp literal");
11030
11031 case '^':
11032 if ((c = nextc(p)) == '=') {
11033 set_yylval_id('^');
11034 SET_LEX_STATE(EXPR_BEG);
11035 return tOP_ASGN;
11036 }
11037 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11038 pushback(p, c);
11039 return '^';
11040
11041 case ';':
11042 SET_LEX_STATE(EXPR_BEG);
11043 p->command_start = TRUE;
11044 return ';';
11045
11046 case ',':
11047 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11048 return ',';
11049
11050 case '~':
11051 if (IS_AFTER_OPERATOR()) {
11052 if ((c = nextc(p)) != '@') {
11053 pushback(p, c);
11054 }
11055 SET_LEX_STATE(EXPR_ARG);
11056 }
11057 else {
11058 SET_LEX_STATE(EXPR_BEG);
11059 }
11060 return '~';
11061
11062 case '(':
11063 if (IS_BEG()) {
11064 c = tLPAREN;
11065 }
11066 else if (!space_seen) {
11067 /* foo( ... ) => method call, no ambiguity */
11068 }
11069 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11070 c = tLPAREN_ARG;
11071 }
11072 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11073 rb_warning0("parentheses after method name is interpreted as "
11074 "an argument list, not a decomposed argument");
11075 }
11076 p->lex.paren_nest++;
11077 COND_PUSH(0);
11078 CMDARG_PUSH(0);
11079 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11080 return c;
11081
11082 case '[':
11083 p->lex.paren_nest++;
11084 if (IS_AFTER_OPERATOR()) {
11085 if ((c = nextc(p)) == ']') {
11086 p->lex.paren_nest--;
11087 SET_LEX_STATE(EXPR_ARG);
11088 if ((c = nextc(p)) == '=') {
11089 return tASET;
11090 }
11091 pushback(p, c);
11092 return tAREF;
11093 }
11094 pushback(p, c);
11095 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11096 return '[';
11097 }
11098 else if (IS_BEG()) {
11099 c = tLBRACK;
11100 }
11101 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11102 c = tLBRACK;
11103 }
11104 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11105 COND_PUSH(0);
11106 CMDARG_PUSH(0);
11107 return c;
11108
11109 case '{':
11110 ++p->lex.brace_nest;
11111 if (lambda_beginning_p())
11112 c = tLAMBEG;
11113 else if (IS_lex_state(EXPR_LABELED))
11114 c = tLBRACE; /* hash */
11115 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11116 c = '{'; /* block (primary) */
11117 else if (IS_lex_state(EXPR_ENDARG))
11118 c = tLBRACE_ARG; /* block (expr) */
11119 else
11120 c = tLBRACE; /* hash */
11121 if (c != tLBRACE) {
11122 p->command_start = TRUE;
11123 SET_LEX_STATE(EXPR_BEG);
11124 }
11125 else {
11126 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11127 }
11128 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11129 COND_PUSH(0);
11130 CMDARG_PUSH(0);
11131 return c;
11132
11133 case '\\':
11134 c = nextc(p);
11135 if (c == '\n') {
11136 space_seen = 1;
11137 dispatch_scan_event(p, tSP);
11138 goto retry; /* skip \\n */
11139 }
11140 if (c == ' ') return tSP;
11141 if (ISSPACE(c)) return c;
11142 pushback(p, c);
11143 return '\\';
11144
11145 case '%':
11146 return parse_percent(p, space_seen, last_state);
11147
11148 case '$':
11149 return parse_gvar(p, last_state);
11150
11151 case '@':
11152 return parse_atmark(p, last_state);
11153
11154 case '_':
11155 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11156 p->ruby__end__seen = 1;
11157 p->eofp = 1;
11158#ifdef RIPPER
11159 lex_goto_eol(p);
11160 dispatch_scan_event(p, k__END__);
11161#endif
11162 return END_OF_INPUT;
11163 }
11164 newtok(p);
11165 break;
11166
11167 default:
11168 if (!parser_is_identchar(p)) {
11169 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11170 token_flush(p);
11171 goto retry;
11172 }
11173
11174 newtok(p);
11175 break;
11176 }
11177
11178 return parse_ident(p, c, cmd_state);
11179}
11180
11181static enum yytokentype
11182yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11183{
11184 enum yytokentype t;
11185
11186 p->lval = lval;
11187 lval->node = 0;
11188 p->yylloc = yylloc;
11189
11190 t = parser_yylex(p);
11191
11192 if (has_delayed_token(p))
11193 dispatch_delayed_token(p, t);
11194 else if (t != END_OF_INPUT)
11195 dispatch_scan_event(p, t);
11196
11197 return t;
11198}
11199
11200#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11201
11202static NODE*
11203node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11204{
11205 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11206
11207 rb_node_init(n, type);
11208 return n;
11209}
11210
11211static NODE *
11212nd_set_loc(NODE *nd, const YYLTYPE *loc)
11213{
11214 nd->nd_loc = *loc;
11215 nd_set_line(nd, loc->beg_pos.lineno);
11216 return nd;
11217}
11218
11219static NODE*
11220node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11221{
11222 NODE *n = node_new_internal(p, type, size, alignment);
11223
11224 nd_set_loc(n, loc);
11225 nd_set_node_id(n, parser_get_node_id(p));
11226 return n;
11227}
11228
11229#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11230
11231static rb_node_scope_t *
11232rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11233{
11234 rb_ast_id_table_t *nd_tbl;
11235 nd_tbl = local_tbl(p);
11236 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11237 n->nd_tbl = nd_tbl;
11238 n->nd_body = nd_body;
11239 n->nd_args = nd_args;
11240
11241 return n;
11242}
11243
11244static rb_node_scope_t *
11245rb_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)
11246{
11247 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11248 n->nd_tbl = nd_tbl;
11249 n->nd_body = nd_body;
11250 n->nd_args = nd_args;
11251
11252 return n;
11253}
11254
11255static rb_node_defn_t *
11256rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11257{
11258 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11259 n->nd_mid = nd_mid;
11260 n->nd_defn = nd_defn;
11261
11262 return n;
11263}
11264
11265static rb_node_defs_t *
11266rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11267{
11268 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11269 n->nd_recv = nd_recv;
11270 n->nd_mid = nd_mid;
11271 n->nd_defn = nd_defn;
11272
11273 return n;
11274}
11275
11276static rb_node_block_t *
11277rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11278{
11279 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11280 n->nd_head = nd_head;
11281 n->nd_end = (NODE *)n;
11282 n->nd_next = 0;
11283
11284 return n;
11285}
11286
11287static rb_node_for_t *
11288rb_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)
11289{
11290 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11291 n->nd_body = nd_body;
11292 n->nd_iter = nd_iter;
11293 n->for_keyword_loc = *for_keyword_loc;
11294 n->in_keyword_loc = *in_keyword_loc;
11295 n->do_keyword_loc = *do_keyword_loc;
11296 n->end_keyword_loc = *end_keyword_loc;
11297
11298 return n;
11299}
11300
11301static rb_node_for_masgn_t *
11302rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11303{
11304 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11305 n->nd_var = nd_var;
11306
11307 return n;
11308}
11309
11310static rb_node_retry_t *
11311rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11312{
11313 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11314
11315 return n;
11316}
11317
11318static rb_node_begin_t *
11319rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11320{
11321 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11322 n->nd_body = nd_body;
11323
11324 return n;
11325}
11326
11327static rb_node_rescue_t *
11328rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11329{
11330 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11331 n->nd_head = nd_head;
11332 n->nd_resq = nd_resq;
11333 n->nd_else = nd_else;
11334
11335 return n;
11336}
11337
11338static rb_node_resbody_t *
11339rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11340{
11341 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11342 n->nd_args = nd_args;
11343 n->nd_exc_var = nd_exc_var;
11344 n->nd_body = nd_body;
11345 n->nd_next = nd_next;
11346
11347 return n;
11348}
11349
11350static rb_node_ensure_t *
11351rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11352{
11353 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11354 n->nd_head = nd_head;
11355 n->nd_ensr = nd_ensr;
11356
11357 return n;
11358}
11359
11360static rb_node_and_t *
11361rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11362{
11363 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11364 n->nd_1st = nd_1st;
11365 n->nd_2nd = nd_2nd;
11366 n->operator_loc = *operator_loc;
11367
11368 return n;
11369}
11370
11371static rb_node_or_t *
11372rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11373{
11374 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11375 n->nd_1st = nd_1st;
11376 n->nd_2nd = nd_2nd;
11377 n->operator_loc = *operator_loc;
11378
11379 return n;
11380}
11381
11382static rb_node_return_t *
11383rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11384{
11385 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11386 n->nd_stts = nd_stts;
11387 n->keyword_loc = *keyword_loc;
11388 return n;
11389}
11390
11391static rb_node_yield_t *
11392rb_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)
11393{
11394 if (nd_head) no_blockarg(p, nd_head);
11395
11396 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11397 n->nd_head = nd_head;
11398 n->keyword_loc = *keyword_loc;
11399 n->lparen_loc = *lparen_loc;
11400 n->rparen_loc = *rparen_loc;
11401
11402 return n;
11403}
11404
11405static rb_node_if_t *
11406rb_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)
11407{
11408 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11409 n->nd_cond = nd_cond;
11410 n->nd_body = nd_body;
11411 n->nd_else = nd_else;
11412 n->if_keyword_loc = *if_keyword_loc;
11413 n->then_keyword_loc = *then_keyword_loc;
11414 n->end_keyword_loc = *end_keyword_loc;
11415
11416 return n;
11417}
11418
11419static rb_node_unless_t *
11420rb_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)
11421{
11422 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11423 n->nd_cond = nd_cond;
11424 n->nd_body = nd_body;
11425 n->nd_else = nd_else;
11426 n->keyword_loc = *keyword_loc;
11427 n->then_keyword_loc = *then_keyword_loc;
11428 n->end_keyword_loc = *end_keyword_loc;
11429
11430 return n;
11431}
11432
11433static rb_node_class_t *
11434rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *inheritance_operator_loc, const YYLTYPE *end_keyword_loc)
11435{
11436 /* Keep the order of node creation */
11437 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11438 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11439 n->nd_cpath = nd_cpath;
11440 n->nd_body = scope;
11441 n->nd_super = nd_super;
11442 n->class_keyword_loc = *class_keyword_loc;
11443 n->inheritance_operator_loc = *inheritance_operator_loc;
11444 n->end_keyword_loc = *end_keyword_loc;
11445
11446 return n;
11447}
11448
11449static rb_node_sclass_t *
11450rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc)
11451{
11452 /* Keep the order of node creation */
11453 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11454 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11455 n->nd_recv = nd_recv;
11456 n->nd_body = scope;
11457
11458 return n;
11459}
11460
11461static rb_node_module_t *
11462rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc)
11463{
11464 /* Keep the order of node creation */
11465 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11466 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11467 n->nd_cpath = nd_cpath;
11468 n->nd_body = scope;
11469
11470 return n;
11471}
11472
11473static rb_node_iter_t *
11474rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11475{
11476 /* Keep the order of node creation */
11477 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11478 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11479 n->nd_body = scope;
11480 n->nd_iter = 0;
11481
11482 return n;
11483}
11484
11485static rb_node_lambda_t *
11486rb_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)
11487{
11488 /* Keep the order of node creation */
11489 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11490 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11491 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11492 n->nd_body = scope;
11493 n->operator_loc = *operator_loc;
11494 n->opening_loc = *opening_loc;
11495 n->closing_loc = *closing_loc;
11496
11497 return n;
11498}
11499
11500static rb_node_case_t *
11501rb_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)
11502{
11503 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11504 n->nd_head = nd_head;
11505 n->nd_body = nd_body;
11506 n->case_keyword_loc = *case_keyword_loc;
11507 n->end_keyword_loc = *end_keyword_loc;
11508
11509 return n;
11510}
11511
11512static rb_node_case2_t *
11513rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11514{
11515 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11516 n->nd_head = 0;
11517 n->nd_body = nd_body;
11518 n->case_keyword_loc = *case_keyword_loc;
11519 n->end_keyword_loc = *end_keyword_loc;
11520
11521 return n;
11522}
11523
11524static rb_node_case3_t *
11525rb_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)
11526{
11527 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11528 n->nd_head = nd_head;
11529 n->nd_body = nd_body;
11530 n->case_keyword_loc = *case_keyword_loc;
11531 n->end_keyword_loc = *end_keyword_loc;
11532
11533 return n;
11534}
11535
11536static rb_node_when_t *
11537rb_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)
11538{
11539 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11540 n->nd_head = nd_head;
11541 n->nd_body = nd_body;
11542 n->nd_next = nd_next;
11543 n->keyword_loc = *keyword_loc;
11544 n->then_keyword_loc = *then_keyword_loc;
11545
11546 return n;
11547}
11548
11549static rb_node_in_t *
11550rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11551{
11552 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11553 n->nd_head = nd_head;
11554 n->nd_body = nd_body;
11555 n->nd_next = nd_next;
11556
11557 return n;
11558}
11559
11560static rb_node_while_t *
11561rb_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)
11562{
11563 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11564 n->nd_cond = nd_cond;
11565 n->nd_body = nd_body;
11566 n->nd_state = nd_state;
11567 n->keyword_loc = *keyword_loc;
11568 n->closing_loc = *closing_loc;
11569
11570 return n;
11571}
11572
11573static rb_node_until_t *
11574rb_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)
11575{
11576 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11577 n->nd_cond = nd_cond;
11578 n->nd_body = nd_body;
11579 n->nd_state = nd_state;
11580 n->keyword_loc = *keyword_loc;
11581 n->closing_loc = *closing_loc;
11582
11583 return n;
11584}
11585
11586static rb_node_colon2_t *
11587rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc)
11588{
11589 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11590 n->nd_head = nd_head;
11591 n->nd_mid = nd_mid;
11592
11593 return n;
11594}
11595
11596static rb_node_colon3_t *
11597rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11598{
11599 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11600 n->nd_mid = nd_mid;
11601
11602 return n;
11603}
11604
11605static rb_node_dot2_t *
11606rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11607{
11608 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11609 n->nd_beg = nd_beg;
11610 n->nd_end = nd_end;
11611 n->operator_loc = *operator_loc;
11612
11613 return n;
11614}
11615
11616static rb_node_dot3_t *
11617rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11618{
11619 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11620 n->nd_beg = nd_beg;
11621 n->nd_end = nd_end;
11622 n->operator_loc = *operator_loc;
11623
11624 return n;
11625}
11626
11627static rb_node_self_t *
11628rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11629{
11630 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11631 n->nd_state = 1;
11632
11633 return n;
11634}
11635
11636static rb_node_nil_t *
11637rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11638{
11639 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11640
11641 return n;
11642}
11643
11644static rb_node_true_t *
11645rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11646{
11647 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11648
11649 return n;
11650}
11651
11652static rb_node_false_t *
11653rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11654{
11655 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11656
11657 return n;
11658}
11659
11660static rb_node_super_t *
11661rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11662 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11663{
11664 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11665 n->nd_args = nd_args;
11666 n->keyword_loc = *keyword_loc;
11667 n->lparen_loc = *lparen_loc;
11668 n->rparen_loc = *rparen_loc;
11669
11670 return n;
11671}
11672
11673static rb_node_zsuper_t *
11674rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11675{
11676 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11677
11678 return n;
11679}
11680
11681static rb_node_match2_t *
11682rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11683{
11684 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11685 n->nd_recv = nd_recv;
11686 n->nd_value = nd_value;
11687 n->nd_args = 0;
11688
11689 return n;
11690}
11691
11692static rb_node_match3_t *
11693rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11694{
11695 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11696 n->nd_recv = nd_recv;
11697 n->nd_value = nd_value;
11698
11699 return n;
11700}
11701
11702/* TODO: Use union for NODE_LIST2 */
11703static rb_node_list_t *
11704rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11705{
11706 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11707 n->nd_head = nd_head;
11708 n->as.nd_alen = 1;
11709 n->nd_next = 0;
11710
11711 return n;
11712}
11713
11714static rb_node_list_t *
11715rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11716{
11717 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11718 n->nd_head = nd_head;
11719 n->as.nd_alen = nd_alen;
11720 n->nd_next = nd_next;
11721
11722 return n;
11723}
11724
11725static rb_node_zlist_t *
11726rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11727{
11728 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11729
11730 return n;
11731}
11732
11733static rb_node_hash_t *
11734rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11735{
11736 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11737 n->nd_head = nd_head;
11738 n->nd_brace = 0;
11739
11740 return n;
11741}
11742
11743static rb_node_masgn_t *
11744rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11745{
11746 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11747 n->nd_head = nd_head;
11748 n->nd_value = 0;
11749 n->nd_args = nd_args;
11750
11751 return n;
11752}
11753
11754static rb_node_gasgn_t *
11755rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11756{
11757 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11758 n->nd_vid = nd_vid;
11759 n->nd_value = nd_value;
11760
11761 return n;
11762}
11763
11764static rb_node_lasgn_t *
11765rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11766{
11767 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11768 n->nd_vid = nd_vid;
11769 n->nd_value = nd_value;
11770
11771 return n;
11772}
11773
11774static rb_node_dasgn_t *
11775rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11776{
11777 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11778 n->nd_vid = nd_vid;
11779 n->nd_value = nd_value;
11780
11781 return n;
11782}
11783
11784static rb_node_iasgn_t *
11785rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11786{
11787 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11788 n->nd_vid = nd_vid;
11789 n->nd_value = nd_value;
11790
11791 return n;
11792}
11793
11794static rb_node_cvasgn_t *
11795rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11796{
11797 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11798 n->nd_vid = nd_vid;
11799 n->nd_value = nd_value;
11800
11801 return n;
11802}
11803
11804static rb_node_op_asgn1_t *
11805rb_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)
11806{
11807 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11808 n->nd_recv = nd_recv;
11809 n->nd_mid = nd_mid;
11810 n->nd_index = index;
11811 n->nd_rvalue = rvalue;
11812 n->call_operator_loc = *call_operator_loc;
11813 n->opening_loc = *opening_loc;
11814 n->closing_loc = *closing_loc;
11815 n->binary_operator_loc = *binary_operator_loc;
11816
11817 return n;
11818}
11819
11820static rb_node_op_asgn2_t *
11821rb_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)
11822{
11823 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11824 n->nd_recv = nd_recv;
11825 n->nd_value = nd_value;
11826 n->nd_vid = nd_vid;
11827 n->nd_mid = nd_mid;
11828 n->nd_aid = nd_aid;
11829 n->call_operator_loc = *call_operator_loc;
11830 n->message_loc = *message_loc;
11831 n->binary_operator_loc = *binary_operator_loc;
11832
11833 return n;
11834}
11835
11836static rb_node_op_asgn_or_t *
11837rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11838{
11839 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11840 n->nd_head = nd_head;
11841 n->nd_value = nd_value;
11842
11843 return n;
11844}
11845
11846static rb_node_op_asgn_and_t *
11847rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11848{
11849 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11850 n->nd_head = nd_head;
11851 n->nd_value = nd_value;
11852
11853 return n;
11854}
11855
11856static rb_node_gvar_t *
11857rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11858{
11859 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11860 n->nd_vid = nd_vid;
11861
11862 return n;
11863}
11864
11865static rb_node_lvar_t *
11866rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11867{
11868 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11869 n->nd_vid = nd_vid;
11870
11871 return n;
11872}
11873
11874static rb_node_dvar_t *
11875rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11876{
11877 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11878 n->nd_vid = nd_vid;
11879
11880 return n;
11881}
11882
11883static rb_node_ivar_t *
11884rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11885{
11886 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11887 n->nd_vid = nd_vid;
11888
11889 return n;
11890}
11891
11892static rb_node_const_t *
11893rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11894{
11895 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11896 n->nd_vid = nd_vid;
11897
11898 return n;
11899}
11900
11901static rb_node_cvar_t *
11902rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11903{
11904 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11905 n->nd_vid = nd_vid;
11906
11907 return n;
11908}
11909
11910static rb_node_nth_ref_t *
11911rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11912{
11913 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11914 n->nd_nth = nd_nth;
11915
11916 return n;
11917}
11918
11919static rb_node_back_ref_t *
11920rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11921{
11922 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11923 n->nd_nth = nd_nth;
11924
11925 return n;
11926}
11927
11928static rb_node_integer_t *
11929rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11930{
11931 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11932 n->val = val;
11933 n->minus = FALSE;
11934 n->base = base;
11935
11936 return n;
11937}
11938
11939static rb_node_float_t *
11940rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11941{
11942 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
11943 n->val = val;
11944 n->minus = FALSE;
11945
11946 return n;
11947}
11948
11949static rb_node_rational_t *
11950rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
11951{
11952 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
11953 n->val = val;
11954 n->minus = FALSE;
11955 n->base = base;
11956 n->seen_point = seen_point;
11957
11958 return n;
11959}
11960
11961static rb_node_imaginary_t *
11962rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
11963{
11964 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
11965 n->val = val;
11966 n->minus = FALSE;
11967 n->base = base;
11968 n->seen_point = seen_point;
11969 n->type = numeric_type;
11970
11971 return n;
11972}
11973
11974static rb_node_str_t *
11975rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
11976{
11977 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
11978 n->string = string;
11979
11980 return n;
11981}
11982
11983/* TODO; Use union for NODE_DSTR2 */
11984static rb_node_dstr_t *
11985rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11986{
11987 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
11988 n->string = string;
11989 n->as.nd_alen = nd_alen;
11990 n->nd_next = (rb_node_list_t *)nd_next;
11991
11992 return n;
11993}
11994
11995static rb_node_dstr_t *
11996rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
11997{
11998 return rb_node_dstr_new0(p, string, 1, 0, loc);
11999}
12000
12001static rb_node_xstr_t *
12002rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12003{
12004 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12005 n->string = string;
12006
12007 return n;
12008}
12009
12010static rb_node_dxstr_t *
12011rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12012{
12013 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_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_sym_t *
12022rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12023{
12024 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12025 n->string = rb_str_to_parser_string(p, str);
12026
12027 return n;
12028}
12029
12030static rb_node_dsym_t *
12031rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12032{
12033 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12034 n->string = string;
12035 n->as.nd_alen = nd_alen;
12036 n->nd_next = (rb_node_list_t *)nd_next;
12037
12038 return n;
12039}
12040
12041static rb_node_evstr_t *
12042rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12043{
12044 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12045 n->nd_body = nd_body;
12046 n->opening_loc = *opening_loc;
12047 n->closing_loc = *closing_loc;
12048
12049 return n;
12050}
12051
12052static rb_node_regx_t *
12053rb_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)
12054{
12055 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12056 n->string = string;
12057 n->options = options & RE_OPTION_MASK;
12058 n->opening_loc = *opening_loc;
12059 n->content_loc = *content_loc;
12060 n->closing_loc = *closing_loc;
12061
12062 return n;
12063}
12064
12065static rb_node_call_t *
12066rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12067{
12068 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12069 n->nd_recv = nd_recv;
12070 n->nd_mid = nd_mid;
12071 n->nd_args = nd_args;
12072
12073 return n;
12074}
12075
12076static rb_node_opcall_t *
12077rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12078{
12079 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12080 n->nd_recv = nd_recv;
12081 n->nd_mid = nd_mid;
12082 n->nd_args = nd_args;
12083
12084 return n;
12085}
12086
12087static rb_node_fcall_t *
12088rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12089{
12090 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12091 n->nd_mid = nd_mid;
12092 n->nd_args = nd_args;
12093
12094 return n;
12095}
12096
12097static rb_node_qcall_t *
12098rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12099{
12100 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12101 n->nd_recv = nd_recv;
12102 n->nd_mid = nd_mid;
12103 n->nd_args = nd_args;
12104
12105 return n;
12106}
12107
12108static rb_node_vcall_t *
12109rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12110{
12111 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12112 n->nd_mid = nd_mid;
12113
12114 return n;
12115}
12116
12117static rb_node_once_t *
12118rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12119{
12120 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12121 n->nd_body = nd_body;
12122
12123 return n;
12124}
12125
12126static rb_node_args_t *
12127rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12128{
12129 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12130 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12131
12132 return n;
12133}
12134
12135static rb_node_args_aux_t *
12136rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12137{
12138 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12139 n->nd_pid = nd_pid;
12140 n->nd_plen = nd_plen;
12141 n->nd_next = 0;
12142
12143 return n;
12144}
12145
12146static rb_node_opt_arg_t *
12147rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12148{
12149 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12150 n->nd_body = nd_body;
12151 n->nd_next = 0;
12152
12153 return n;
12154}
12155
12156static rb_node_kw_arg_t *
12157rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12158{
12159 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12160 n->nd_body = nd_body;
12161 n->nd_next = 0;
12162
12163 return n;
12164}
12165
12166static rb_node_postarg_t *
12167rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12168{
12169 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12170 n->nd_1st = nd_1st;
12171 n->nd_2nd = nd_2nd;
12172
12173 return n;
12174}
12175
12176static rb_node_argscat_t *
12177rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12178{
12179 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12180 n->nd_head = nd_head;
12181 n->nd_body = nd_body;
12182
12183 return n;
12184}
12185
12186static rb_node_argspush_t *
12187rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12188{
12189 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12190 n->nd_head = nd_head;
12191 n->nd_body = nd_body;
12192
12193 return n;
12194}
12195
12196static rb_node_splat_t *
12197rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12198{
12199 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12200 n->nd_head = nd_head;
12201 n->operator_loc = *operator_loc;
12202
12203 return n;
12204}
12205
12206static rb_node_block_pass_t *
12207rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12208{
12209 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12210 n->forwarding = 0;
12211 n->nd_head = 0;
12212 n->nd_body = nd_body;
12213 n->operator_loc = *operator_loc;
12214
12215 return n;
12216}
12217
12218static rb_node_alias_t *
12219rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12220{
12221 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12222 n->nd_1st = nd_1st;
12223 n->nd_2nd = nd_2nd;
12224 n->keyword_loc = *keyword_loc;
12225
12226 return n;
12227}
12228
12229static rb_node_valias_t *
12230rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12231{
12232 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12233 n->nd_alias = nd_alias;
12234 n->nd_orig = nd_orig;
12235 n->keyword_loc = *keyword_loc;
12236
12237 return n;
12238}
12239
12240static rb_node_undef_t *
12241rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12242{
12243 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12244 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12245 n->keyword_loc = NULL_LOC;
12246 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12247
12248 return n;
12249}
12250
12251static rb_node_errinfo_t *
12252rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12253{
12254 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12255
12256 return n;
12257}
12258
12259static rb_node_defined_t *
12260rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12261{
12262 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12263 n->nd_head = nd_head;
12264
12265 return n;
12266}
12267
12268static rb_node_postexe_t *
12269rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12270{
12271 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12272 n->nd_body = nd_body;
12273 n->keyword_loc = *keyword_loc;
12274 n->opening_loc = *opening_loc;
12275 n->closing_loc = *closing_loc;
12276
12277 return n;
12278}
12279
12280static rb_node_attrasgn_t *
12281rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12282{
12283 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12284 n->nd_recv = nd_recv;
12285 n->nd_mid = nd_mid;
12286 n->nd_args = nd_args;
12287
12288 return n;
12289}
12290
12291static rb_node_aryptn_t *
12292rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12293{
12294 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12295 n->nd_pconst = 0;
12296 n->pre_args = pre_args;
12297 n->rest_arg = rest_arg;
12298 n->post_args = post_args;
12299
12300 return n;
12301}
12302
12303static rb_node_hshptn_t *
12304rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12305{
12306 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12307 n->nd_pconst = nd_pconst;
12308 n->nd_pkwargs = nd_pkwargs;
12309 n->nd_pkwrestarg = nd_pkwrestarg;
12310
12311 return n;
12312}
12313
12314static rb_node_fndptn_t *
12315rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12316{
12317 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12318 n->nd_pconst = 0;
12319 n->pre_rest_arg = pre_rest_arg;
12320 n->args = args;
12321 n->post_rest_arg = post_rest_arg;
12322
12323 return n;
12324}
12325
12326static rb_node_line_t *
12327rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12328{
12329 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12330
12331 return n;
12332}
12333
12334static rb_node_file_t *
12335rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12336{
12337 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12338 n->path = rb_str_to_parser_string(p, str);
12339
12340 return n;
12341}
12342
12343static rb_node_encoding_t *
12344rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12345{
12346 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12347 n->enc = p->enc;
12348
12349 return n;
12350}
12351
12352static rb_node_cdecl_t *
12353rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12354{
12355 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12356 n->nd_vid = nd_vid;
12357 n->nd_value = nd_value;
12358 n->nd_else = nd_else;
12359 n->shareability = shareability;
12360
12361 return n;
12362}
12363
12364static rb_node_op_cdecl_t *
12365rb_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)
12366{
12367 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12368 n->nd_head = nd_head;
12369 n->nd_value = nd_value;
12370 n->nd_aid = nd_aid;
12371 n->shareability = shareability;
12372
12373 return n;
12374}
12375
12376static rb_node_error_t *
12377rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12378{
12379 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12380
12381 return n;
12382}
12383
12384static rb_node_break_t *
12385rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12386{
12387 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12388 n->nd_stts = nd_stts;
12389 n->nd_chain = 0;
12390 n->keyword_loc = *keyword_loc;
12391
12392 return n;
12393}
12394
12395static rb_node_next_t *
12396rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12397{
12398 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12399 n->nd_stts = nd_stts;
12400 n->nd_chain = 0;
12401 n->keyword_loc = *keyword_loc;
12402
12403 return n;
12404}
12405
12406static rb_node_redo_t *
12407rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12408{
12409 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12410 n->nd_chain = 0;
12411 n->keyword_loc = *keyword_loc;
12412
12413 return n;
12414}
12415
12416static rb_node_def_temp_t *
12417rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12418{
12419 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12420 n->save.numparam_save = 0;
12421 n->save.max_numparam = 0;
12422 n->save.ctxt = p->ctxt;
12423 n->nd_def = 0;
12424 n->nd_mid = 0;
12425
12426 return n;
12427}
12428
12429static rb_node_def_temp_t *
12430def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12431{
12432 n->save.numparam_save = numparam_push(p);
12433 n->save.max_numparam = p->max_numparam;
12434 return n;
12435}
12436
12437#ifndef RIPPER
12438static enum node_type
12439nodetype(NODE *node) /* for debug */
12440{
12441 return (enum node_type)nd_type(node);
12442}
12443
12444static int
12445nodeline(NODE *node)
12446{
12447 return nd_line(node);
12448}
12449#endif
12450
12451static NODE*
12452newline_node(NODE *node)
12453{
12454 if (node) {
12455 node = remove_begin(node);
12456 nd_set_fl_newline(node);
12457 }
12458 return node;
12459}
12460
12461static void
12462fixpos(NODE *node, NODE *orig)
12463{
12464 if (!node) return;
12465 if (!orig) return;
12466 nd_set_line(node, nd_line(orig));
12467}
12468
12469static NODE*
12470block_append(struct parser_params *p, NODE *head, NODE *tail)
12471{
12472 NODE *end, *h = head, *nd;
12473
12474 if (tail == 0) return head;
12475
12476 if (h == 0) return tail;
12477 switch (nd_type(h)) {
12478 default:
12479 h = end = NEW_BLOCK(head, &head->nd_loc);
12480 head = end;
12481 break;
12482 case NODE_BLOCK:
12483 end = RNODE_BLOCK(h)->nd_end;
12484 break;
12485 }
12486
12487 nd = RNODE_BLOCK(end)->nd_head;
12488 switch (nd_type(nd)) {
12489 case NODE_RETURN:
12490 case NODE_BREAK:
12491 case NODE_NEXT:
12492 case NODE_REDO:
12493 case NODE_RETRY:
12494 rb_warning0L(nd_line(tail), "statement not reached");
12495 break;
12496
12497 default:
12498 break;
12499 }
12500
12501 if (!nd_type_p(tail, NODE_BLOCK)) {
12502 tail = NEW_BLOCK(tail, &tail->nd_loc);
12503 }
12504 RNODE_BLOCK(end)->nd_next = tail;
12505 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12506 nd_set_last_loc(head, nd_last_loc(tail));
12507 return head;
12508}
12509
12510/* append item to the list */
12511static NODE*
12512list_append(struct parser_params *p, NODE *list, NODE *item)
12513{
12514 NODE *last;
12515
12516 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12517 if (RNODE_LIST(list)->nd_next) {
12518 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12519 }
12520 else {
12521 last = list;
12522 }
12523
12524 RNODE_LIST(list)->as.nd_alen += 1;
12525 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12526 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12527
12528 nd_set_last_loc(list, nd_last_loc(item));
12529
12530 return list;
12531}
12532
12533/* concat two lists */
12534static NODE*
12535list_concat(NODE *head, NODE *tail)
12536{
12537 NODE *last;
12538
12539 if (RNODE_LIST(head)->nd_next) {
12540 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12541 }
12542 else {
12543 last = head;
12544 }
12545
12546 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12547 RNODE_LIST(last)->nd_next = tail;
12548 if (RNODE_LIST(tail)->nd_next) {
12549 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12550 }
12551 else {
12552 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12553 }
12554
12555 nd_set_last_loc(head, nd_last_loc(tail));
12556
12557 return head;
12558}
12559
12560static int
12561literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12562{
12563 if (!tail) return 1;
12564 if (!rb_parser_enc_compatible(p, head, tail)) {
12565 compile_error(p, "string literal encodings differ (%s / %s)",
12566 rb_enc_name(rb_parser_str_get_encoding(head)),
12567 rb_enc_name(rb_parser_str_get_encoding(tail)));
12568 rb_parser_str_resize(p, head, 0);
12569 rb_parser_str_resize(p, tail, 0);
12570 return 0;
12571 }
12572 rb_parser_str_buf_append(p, head, tail);
12573 return 1;
12574}
12575
12576static rb_parser_string_t *
12577string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12578{
12579 if (htype != NODE_DSTR) return NULL;
12580 if (RNODE_DSTR(head)->nd_next) {
12581 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12582 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12583 }
12584 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12585 ASSUME(lit);
12586 return lit;
12587}
12588
12589#ifndef RIPPER
12590static rb_parser_string_t *
12591rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12592{
12593 rb_parser_string_t *copy;
12594 if (!orig) return NULL;
12595 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12596 copy->coderange = orig->coderange;
12597 copy->enc = orig->enc;
12598 return copy;
12599}
12600#endif
12601
12602/* concat two string literals */
12603static NODE *
12604literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12605{
12606 enum node_type htype;
12607 rb_parser_string_t *lit;
12608
12609 if (!head) return tail;
12610 if (!tail) return head;
12611
12612 htype = nd_type(head);
12613 if (htype == NODE_EVSTR) {
12614 head = new_dstr(p, head, loc);
12615 htype = NODE_DSTR;
12616 }
12617 if (p->heredoc_indent > 0) {
12618 switch (htype) {
12619 case NODE_STR:
12620 head = str2dstr(p, head);
12621 case NODE_DSTR:
12622 return list_append(p, head, tail);
12623 default:
12624 break;
12625 }
12626 }
12627 switch (nd_type(tail)) {
12628 case NODE_STR:
12629 if ((lit = string_literal_head(p, htype, head)) != false) {
12630 htype = NODE_STR;
12631 }
12632 else {
12633 lit = RNODE_DSTR(head)->string;
12634 }
12635 if (htype == NODE_STR) {
12636 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12637 error:
12638 rb_discard_node(p, head);
12639 rb_discard_node(p, tail);
12640 return 0;
12641 }
12642 rb_discard_node(p, tail);
12643 }
12644 else {
12645 list_append(p, head, tail);
12646 }
12647 break;
12648
12649 case NODE_DSTR:
12650 if (htype == NODE_STR) {
12651 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12652 goto error;
12653 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12654 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12655 RNODE_STR(head)->string = NULL;
12656 rb_discard_node(p, head);
12657 head = tail;
12658 }
12659 else if (!RNODE_DSTR(tail)->string) {
12660 append:
12661 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12662 if (!RNODE_DSTR(head)->nd_next) {
12663 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12664 }
12665 else if (RNODE_DSTR(tail)->nd_next) {
12666 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12667 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12668 }
12669 rb_discard_node(p, tail);
12670 }
12671 else if ((lit = string_literal_head(p, htype, head)) != false) {
12672 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12673 goto error;
12674 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12675 RNODE_DSTR(tail)->string = 0;
12676 goto append;
12677 }
12678 else {
12679 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));
12680 RNODE_DSTR(tail)->string = 0;
12681 }
12682 break;
12683
12684 case NODE_EVSTR:
12685 if (htype == NODE_STR) {
12686 head = str2dstr(p, head);
12687 RNODE_DSTR(head)->as.nd_alen = 1;
12688 }
12689 list_append(p, head, tail);
12690 break;
12691 }
12692 return head;
12693}
12694
12695static void
12696nd_copy_flag(NODE *new_node, NODE *old_node)
12697{
12698 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12699 nd_set_line(new_node, nd_line(old_node));
12700 new_node->nd_loc = old_node->nd_loc;
12701 new_node->node_id = old_node->node_id;
12702}
12703
12704static NODE *
12705str2dstr(struct parser_params *p, NODE *node)
12706{
12707 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12708 nd_copy_flag(new_node, node);
12709 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12710 RNODE_DSTR(new_node)->as.nd_alen = 0;
12711 RNODE_DSTR(new_node)->nd_next = 0;
12712 RNODE_STR(node)->string = 0;
12713
12714 return new_node;
12715}
12716
12717static NODE *
12718str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12719{
12720 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12721 nd_copy_flag(new_node, node);
12722 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12723 RNODE_REGX(new_node)->options = options;
12724 nd_set_loc(new_node, loc);
12725 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12726 RNODE_REGX(new_node)->content_loc = *content_loc;
12727 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12728 RNODE_STR(node)->string = 0;
12729
12730 return new_node;
12731}
12732
12733static NODE *
12734evstr2dstr(struct parser_params *p, NODE *node)
12735{
12736 if (nd_type_p(node, NODE_EVSTR)) {
12737 node = new_dstr(p, node, &node->nd_loc);
12738 }
12739 return node;
12740}
12741
12742static NODE *
12743new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12744{
12745 NODE *head = node;
12746
12747 if (node) {
12748 switch (nd_type(node)) {
12749 case NODE_STR:
12750 return str2dstr(p, node);
12751 case NODE_DSTR:
12752 break;
12753 case NODE_EVSTR:
12754 return node;
12755 }
12756 }
12757 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12758}
12759
12760static NODE *
12761new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12762{
12763 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12764 return list_append(p, dstr, node);
12765}
12766
12767static NODE *
12768call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12769 const YYLTYPE *op_loc, const YYLTYPE *loc)
12770{
12771 NODE *expr;
12772 value_expr(recv);
12773 value_expr(arg1);
12774 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12775 nd_set_line(expr, op_loc->beg_pos.lineno);
12776 return expr;
12777}
12778
12779static NODE *
12780call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12781{
12782 NODE *opcall;
12783 value_expr(recv);
12784 opcall = NEW_OPCALL(recv, id, 0, loc);
12785 nd_set_line(opcall, op_loc->beg_pos.lineno);
12786 return opcall;
12787}
12788
12789static NODE *
12790new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12791{
12792 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12793 nd_set_line(qcall, op_loc->beg_pos.lineno);
12794 return qcall;
12795}
12796
12797static NODE*
12798new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12799{
12800 NODE *ret;
12801 if (block) block_dup_check(p, args, block);
12802 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12803 if (block) ret = method_add_block(p, ret, block, loc);
12804 fixpos(ret, recv);
12805 return ret;
12806}
12807
12808static rb_locations_lambda_body_t*
12809new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12810{
12811 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12812 body->node = node;
12813 body->opening_loc = *opening_loc;
12814 body->closing_loc = *closing_loc;
12815 return body;
12816}
12817
12818#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12819
12820static NODE*
12821last_expr_once_body(NODE *node)
12822{
12823 if (!node) return 0;
12824 return nd_once_body(node);
12825}
12826
12827static NODE*
12828match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12829{
12830 NODE *n;
12831 int line = op_loc->beg_pos.lineno;
12832
12833 value_expr(node1);
12834 value_expr(node2);
12835
12836 if ((n = last_expr_once_body(node1)) != 0) {
12837 switch (nd_type(n)) {
12838 case NODE_DREGX:
12839 {
12840 NODE *match = NEW_MATCH2(node1, node2, loc);
12841 nd_set_line(match, line);
12842 return match;
12843 }
12844
12845 case NODE_REGX:
12846 {
12847 const VALUE lit = rb_node_regx_string_val(n);
12848 if (!NIL_P(lit)) {
12849 NODE *match = NEW_MATCH2(node1, node2, loc);
12850 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12851 nd_set_line(match, line);
12852 return match;
12853 }
12854 }
12855 }
12856 }
12857
12858 if ((n = last_expr_once_body(node2)) != 0) {
12859 NODE *match3;
12860
12861 switch (nd_type(n)) {
12862 case NODE_DREGX:
12863 match3 = NEW_MATCH3(node2, node1, loc);
12864 return match3;
12865 }
12866 }
12867
12868 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12869 nd_set_line(n, line);
12870 return n;
12871}
12872
12873# if WARN_PAST_SCOPE
12874static int
12875past_dvar_p(struct parser_params *p, ID id)
12876{
12877 struct vtable *past = p->lvtbl->past;
12878 while (past) {
12879 if (vtable_included(past, id)) return 1;
12880 past = past->prev;
12881 }
12882 return 0;
12883}
12884# endif
12885
12886static int
12887numparam_nested_p(struct parser_params *p)
12888{
12889 struct local_vars *local = p->lvtbl;
12890 NODE *outer = local->numparam.outer;
12891 NODE *inner = local->numparam.inner;
12892 if (outer || inner) {
12893 NODE *used = outer ? outer : inner;
12894 compile_error(p, "numbered parameter is already used in\n"
12895 "%s:%d: %s block here",
12896 p->ruby_sourcefile, nd_line(used),
12897 outer ? "outer" : "inner");
12898 parser_show_error_line(p, &used->nd_loc);
12899 return 1;
12900 }
12901 return 0;
12902}
12903
12904static int
12905numparam_used_p(struct parser_params *p)
12906{
12907 NODE *numparam = p->lvtbl->numparam.current;
12908 if (numparam) {
12909 compile_error(p, "numbered parameter is already used in\n"
12910 "%s:%d: current block here",
12911 p->ruby_sourcefile, nd_line(numparam));
12912 parser_show_error_line(p, &numparam->nd_loc);
12913 return 1;
12914 }
12915 return 0;
12916}
12917
12918static int
12919it_used_p(struct parser_params *p)
12920{
12921 NODE *it = p->lvtbl->it;
12922 if (it) {
12923 compile_error(p, "'it' is already used in\n"
12924 "%s:%d: current block here",
12925 p->ruby_sourcefile, nd_line(it));
12926 parser_show_error_line(p, &it->nd_loc);
12927 return 1;
12928 }
12929 return 0;
12930}
12931
12932static NODE*
12933gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12934{
12935 ID *vidp = NULL;
12936 NODE *node;
12937 switch (id) {
12938 case keyword_self:
12939 return NEW_SELF(loc);
12940 case keyword_nil:
12941 return NEW_NIL(loc);
12942 case keyword_true:
12943 return NEW_TRUE(loc);
12944 case keyword_false:
12945 return NEW_FALSE(loc);
12946 case keyword__FILE__:
12947 {
12948 VALUE file = p->ruby_sourcefile_string;
12949 if (NIL_P(file))
12950 file = rb_str_new(0, 0);
12951 node = NEW_FILE(file, loc);
12952 }
12953 return node;
12954 case keyword__LINE__:
12955 return NEW_LINE(loc);
12956 case keyword__ENCODING__:
12957 return NEW_ENCODING(loc);
12958
12959 }
12960 switch (id_type(id)) {
12961 case ID_LOCAL:
12962 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
12963 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
12964 if (vidp) *vidp |= LVAR_USED;
12965 node = NEW_DVAR(id, loc);
12966 return node;
12967 }
12968 if (local_id_ref(p, id, &vidp)) {
12969 if (vidp) *vidp |= LVAR_USED;
12970 node = NEW_LVAR(id, loc);
12971 return node;
12972 }
12973 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
12974 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
12975 if (numparam_nested_p(p) || it_used_p(p)) return 0;
12976 node = NEW_DVAR(id, loc);
12977 struct local_vars *local = p->lvtbl;
12978 if (!local->numparam.current) local->numparam.current = node;
12979 return node;
12980 }
12981# if WARN_PAST_SCOPE
12982 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
12983 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
12984 }
12985# endif
12986 /* method call without arguments */
12987 if (dyna_in_block(p) && id == rb_intern("it") && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
12988 if (numparam_used_p(p)) return 0;
12989 if (p->max_numparam == ORDINAL_PARAM) {
12990 compile_error(p, "ordinary parameter is defined");
12991 return 0;
12992 }
12993 if (!p->it_id) {
12994 p->it_id = internal_id(p);
12995 vtable_add(p->lvtbl->args, p->it_id);
12996 }
12997 NODE *node = NEW_DVAR(p->it_id, loc);
12998 if (!p->lvtbl->it) p->lvtbl->it = node;
12999 return node;
13000 }
13001 return NEW_VCALL(id, loc);
13002 case ID_GLOBAL:
13003 return NEW_GVAR(id, loc);
13004 case ID_INSTANCE:
13005 return NEW_IVAR(id, loc);
13006 case ID_CONST:
13007 return NEW_CONST(id, loc);
13008 case ID_CLASS:
13009 return NEW_CVAR(id, loc);
13010 }
13011 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13012 return 0;
13013}
13014
13015static rb_node_opt_arg_t *
13016opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13017{
13018 rb_node_opt_arg_t *opts = opt_list;
13019 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13020
13021 while (opts->nd_next) {
13022 opts = opts->nd_next;
13023 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13024 }
13025 opts->nd_next = opt;
13026
13027 return opt_list;
13028}
13029
13030static rb_node_kw_arg_t *
13031kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13032{
13033 if (kwlist) {
13034 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13035 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13036 }
13037 return kwlist;
13038}
13039
13040static NODE *
13041new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
13042{
13043 NODE *n = expr;
13044 while (n) {
13045 if (nd_type_p(n, NODE_BEGIN)) {
13046 n = RNODE_BEGIN(n)->nd_body;
13047 }
13048 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13049 n = RNODE_BLOCK(n)->nd_head;
13050 }
13051 else {
13052 break;
13053 }
13054 }
13055 return NEW_DEFINED(n, loc);
13056}
13057
13058static NODE*
13059str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13060{
13061 VALUE lit;
13062 rb_parser_string_t *str = RNODE_STR(node)->string;
13063 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13064 yyerror1(loc, "invalid symbol");
13065 lit = STR_NEW0();
13066 }
13067 else {
13068 lit = rb_str_new_parser_string(str);
13069 }
13070 return NEW_SYM(lit, loc);
13071}
13072
13073static NODE*
13074symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13075{
13076 enum node_type type = nd_type(symbol);
13077 switch (type) {
13078 case NODE_DSTR:
13079 nd_set_type(symbol, NODE_DSYM);
13080 break;
13081 case NODE_STR:
13082 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13083 break;
13084 default:
13085 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13086 }
13087 return list_append(p, symbols, symbol);
13088}
13089
13090static void
13091dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13092{
13093 if (dreg->string) {
13094 reg_fragment_setenc(p, dreg->string, options);
13095 }
13096 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13097 NODE *frag = list->nd_head;
13098 if (nd_type_p(frag, NODE_STR)) {
13099 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13100 }
13101 else if (nd_type_p(frag, NODE_DSTR)) {
13102 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13103 }
13104 }
13105}
13106
13107static NODE *
13108new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13109{
13110 if (!node) {
13111 /* Check string is valid regex */
13112 rb_parser_string_t *str = STRING_NEW0();
13113 reg_compile(p, str, options);
13114 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13115 return node;
13116 }
13117 switch (nd_type(node)) {
13118 case NODE_STR:
13119 {
13120 /* Check string is valid regex */
13121 reg_compile(p, RNODE_STR(node)->string, options);
13122 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13123 }
13124 break;
13125 default:
13126 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13127 /* fall through */
13128 case NODE_DSTR:
13129 nd_set_type(node, NODE_DREGX);
13130 nd_set_loc(node, loc);
13131 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13132 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13133 if (dreg->nd_next) {
13134 dregex_fragment_setenc(p, dreg, options);
13135 }
13136 if (options & RE_OPTION_ONCE) {
13137 node = NEW_ONCE(node, loc);
13138 }
13139 break;
13140 }
13141 return node;
13142}
13143
13144static rb_node_kw_arg_t *
13145new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13146{
13147 if (!k) return 0;
13148 return NEW_KW_ARG((k), loc);
13149}
13150
13151static NODE *
13152new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13153{
13154 if (!node) {
13155 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13156 return xstr;
13157 }
13158 switch (nd_type(node)) {
13159 case NODE_STR:
13160 nd_set_type(node, NODE_XSTR);
13161 nd_set_loc(node, loc);
13162 break;
13163 case NODE_DSTR:
13164 nd_set_type(node, NODE_DXSTR);
13165 nd_set_loc(node, loc);
13166 break;
13167 default:
13168 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13169 break;
13170 }
13171 return node;
13172}
13173
13174static const
13175struct st_hash_type literal_type = {
13176 literal_cmp,
13177 literal_hash,
13178};
13179
13180static int nd_type_st_key_enable_p(NODE *node);
13181
13182static void
13183check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13184{
13185 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13186 if (!arg || !p->case_labels) return;
13187 if (!nd_type_st_key_enable_p(arg)) return;
13188
13189 if (p->case_labels == CHECK_LITERAL_WHEN) {
13190 p->case_labels = st_init_table(&literal_type);
13191 }
13192 else {
13193 st_data_t line;
13194 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13195 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13196 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13197 return;
13198 }
13199 }
13200 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13201}
13202
13203#ifdef RIPPER
13204static int
13205id_is_var(struct parser_params *p, ID id)
13206{
13207 if (is_notop_id(id)) {
13208 switch (id & ID_SCOPE_MASK) {
13209 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13210 return 1;
13211 case ID_LOCAL:
13212 if (dyna_in_block(p)) {
13213 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13214 }
13215 if (local_id(p, id)) return 1;
13216 /* method call without arguments */
13217 return 0;
13218 }
13219 }
13220 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13221 return 0;
13222}
13223#endif
13224
13225static inline enum lex_state_e
13226parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13227{
13228 if (p->debug) {
13229 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13230 }
13231 return p->lex.state = ls;
13232}
13233
13234#ifndef RIPPER
13235static void
13236flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13237{
13238 VALUE mesg = p->debug_buffer;
13239
13240 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13241 p->debug_buffer = Qnil;
13242 rb_io_puts(1, &mesg, out);
13243 }
13244 if (!NIL_P(str) && RSTRING_LEN(str)) {
13245 rb_io_write(p->debug_output, str);
13246 }
13247}
13248
13249static const char rb_parser_lex_state_names[][8] = {
13250 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13251 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13252 "LABEL", "LABELED","FITEM",
13253};
13254
13255static VALUE
13256append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13257{
13258 int i, sep = 0;
13259 unsigned int mask = 1;
13260 static const char none[] = "NONE";
13261
13262 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13263 if ((unsigned)state & mask) {
13264 if (sep) {
13265 rb_str_cat(buf, "|", 1);
13266 }
13267 sep = 1;
13268 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13269 }
13270 }
13271 if (!sep) {
13272 rb_str_cat(buf, none, sizeof(none)-1);
13273 }
13274 return buf;
13275}
13276
13277enum lex_state_e
13278rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13279 enum lex_state_e to, int line)
13280{
13281 VALUE mesg;
13282 mesg = rb_str_new_cstr("lex_state: ");
13283 append_lex_state_name(p, from, mesg);
13284 rb_str_cat_cstr(mesg, " -> ");
13285 append_lex_state_name(p, to, mesg);
13286 rb_str_catf(mesg, " at line %d\n", line);
13287 flush_debug_buffer(p, p->debug_output, mesg);
13288 return to;
13289}
13290
13291VALUE
13292rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13293{
13294 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13295}
13296
13297static void
13298append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13299{
13300 if (stack == 0) {
13301 rb_str_cat_cstr(mesg, "0");
13302 }
13303 else {
13304 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13305 for (; mask && !(stack & mask); mask >>= 1) continue;
13306 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13307 }
13308}
13309
13310void
13311rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13312 const char *name, int line)
13313{
13314 VALUE mesg = rb_sprintf("%s: ", name);
13315 append_bitstack_value(p, stack, mesg);
13316 rb_str_catf(mesg, " at line %d\n", line);
13317 flush_debug_buffer(p, p->debug_output, mesg);
13318}
13319
13320void
13321rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13322{
13323 va_list ap;
13324 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13325
13326 va_start(ap, fmt);
13327 rb_str_vcatf(mesg, fmt, ap);
13328 va_end(ap);
13329 yyerror0(RSTRING_PTR(mesg));
13330 RB_GC_GUARD(mesg);
13331
13332 mesg = rb_str_new(0, 0);
13333 append_lex_state_name(p, p->lex.state, mesg);
13334 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13335 rb_str_resize(mesg, 0);
13336 append_bitstack_value(p, p->cond_stack, mesg);
13337 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13338 rb_str_resize(mesg, 0);
13339 append_bitstack_value(p, p->cmdarg_stack, mesg);
13340 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13341 if (p->debug_output == rb_ractor_stdout())
13342 p->debug_output = rb_ractor_stderr();
13343 p->debug = TRUE;
13344}
13345
13346static YYLTYPE *
13347rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13348{
13349 yylloc->beg_pos.lineno = sourceline;
13350 yylloc->beg_pos.column = beg_pos;
13351 yylloc->end_pos.lineno = sourceline;
13352 yylloc->end_pos.column = end_pos;
13353 return yylloc;
13354}
13355
13356YYLTYPE *
13357rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13358{
13359 int sourceline = here->sourceline;
13360 int beg_pos = (int)here->offset - here->quote
13361 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13362 int end_pos = (int)here->offset + here->length + here->quote;
13363
13364 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13365}
13366
13367YYLTYPE *
13368rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13369{
13370 yylloc->beg_pos.lineno = p->delayed.beg_line;
13371 yylloc->beg_pos.column = p->delayed.beg_col;
13372 yylloc->end_pos.lineno = p->delayed.end_line;
13373 yylloc->end_pos.column = p->delayed.end_col;
13374
13375 return yylloc;
13376}
13377
13378YYLTYPE *
13379rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13380{
13381 int sourceline = p->ruby_sourceline;
13382 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13383 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13384 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13385}
13386
13387YYLTYPE *
13388rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13389{
13390 yylloc->end_pos = yylloc->beg_pos;
13391
13392 return yylloc;
13393}
13394
13395YYLTYPE *
13396rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13397{
13398 int sourceline = p->ruby_sourceline;
13399 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13400 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13401 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13402}
13403
13404YYLTYPE *
13405rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13406{
13407 int sourceline = p->ruby_sourceline;
13408 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13409 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13410 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13411}
13412#endif /* !RIPPER */
13413
13414static int
13415assignable0(struct parser_params *p, ID id, const char **err)
13416{
13417 if (!id) return -1;
13418 switch (id) {
13419 case keyword_self:
13420 *err = "Can't change the value of self";
13421 return -1;
13422 case keyword_nil:
13423 *err = "Can't assign to nil";
13424 return -1;
13425 case keyword_true:
13426 *err = "Can't assign to true";
13427 return -1;
13428 case keyword_false:
13429 *err = "Can't assign to false";
13430 return -1;
13431 case keyword__FILE__:
13432 *err = "Can't assign to __FILE__";
13433 return -1;
13434 case keyword__LINE__:
13435 *err = "Can't assign to __LINE__";
13436 return -1;
13437 case keyword__ENCODING__:
13438 *err = "Can't assign to __ENCODING__";
13439 return -1;
13440 }
13441 switch (id_type(id)) {
13442 case ID_LOCAL:
13443 if (dyna_in_block(p)) {
13444 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13445 compile_error(p, "Can't assign to numbered parameter _%d",
13446 NUMPARAM_ID_TO_IDX(id));
13447 return -1;
13448 }
13449 if (dvar_curr(p, id)) return NODE_DASGN;
13450 if (dvar_defined(p, id)) return NODE_DASGN;
13451 if (local_id(p, id)) return NODE_LASGN;
13452 dyna_var(p, id);
13453 return NODE_DASGN;
13454 }
13455 else {
13456 if (!local_id(p, id)) local_var(p, id);
13457 return NODE_LASGN;
13458 }
13459 break;
13460 case ID_GLOBAL: return NODE_GASGN;
13461 case ID_INSTANCE: return NODE_IASGN;
13462 case ID_CONST:
13463 if (!p->ctxt.in_def) return NODE_CDECL;
13464 *err = "dynamic constant assignment";
13465 return -1;
13466 case ID_CLASS: return NODE_CVASGN;
13467 default:
13468 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13469 }
13470 return -1;
13471}
13472
13473static NODE*
13474assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13475{
13476 const char *err = 0;
13477 int node_type = assignable0(p, id, &err);
13478 switch (node_type) {
13479 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13480 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13481 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13482 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13483 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13484 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13485 }
13486/* TODO: FIXME */
13487#ifndef RIPPER
13488 if (err) yyerror1(loc, err);
13489#else
13490 if (err) set_value(assign_error(p, err, p->s_lvalue));
13491#endif
13492 return NEW_ERROR(loc);
13493}
13494
13495static int
13496is_private_local_id(struct parser_params *p, ID name)
13497{
13498 VALUE s;
13499 if (name == idUScore) return 1;
13500 if (!is_local_id(name)) return 0;
13501 s = rb_id2str(name);
13502 if (!s) return 0;
13503 return RSTRING_PTR(s)[0] == '_';
13504}
13505
13506static int
13507shadowing_lvar_0(struct parser_params *p, ID name)
13508{
13509 if (dyna_in_block(p)) {
13510 if (dvar_curr(p, name)) {
13511 if (is_private_local_id(p, name)) return 1;
13512 yyerror0("duplicated argument name");
13513 }
13514 else if (dvar_defined(p, name) || local_id(p, name)) {
13515 vtable_add(p->lvtbl->vars, name);
13516 if (p->lvtbl->used) {
13517 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13518 }
13519 return 0;
13520 }
13521 }
13522 else {
13523 if (local_id(p, name)) {
13524 if (is_private_local_id(p, name)) return 1;
13525 yyerror0("duplicated argument name");
13526 }
13527 }
13528 return 1;
13529}
13530
13531static ID
13532shadowing_lvar(struct parser_params *p, ID name)
13533{
13534 shadowing_lvar_0(p, name);
13535 return name;
13536}
13537
13538static void
13539new_bv(struct parser_params *p, ID name)
13540{
13541 if (!name) return;
13542 if (!is_local_id(name)) {
13543 compile_error(p, "invalid local variable - %"PRIsVALUE,
13544 rb_id2str(name));
13545 return;
13546 }
13547 if (!shadowing_lvar_0(p, name)) return;
13548 dyna_var(p, name);
13549 ID *vidp = 0;
13550 if (dvar_defined_ref(p, name, &vidp)) {
13551 if (vidp) *vidp |= LVAR_USED;
13552 }
13553}
13554
13555static void
13556aryset_check(struct parser_params *p, NODE *args)
13557{
13558 NODE *block = 0, *kwds = 0;
13559 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13560 block = RNODE_BLOCK_PASS(args)->nd_body;
13561 args = RNODE_BLOCK_PASS(args)->nd_head;
13562 }
13563 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13564 args = RNODE_ARGSCAT(args)->nd_body;
13565 }
13566 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13567 kwds = RNODE_ARGSPUSH(args)->nd_body;
13568 }
13569 else {
13570 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13571 next = RNODE_LIST(next)->nd_next) {
13572 kwds = RNODE_LIST(next)->nd_head;
13573 }
13574 }
13575 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13576 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13577 }
13578 if (block) {
13579 yyerror1(&block->nd_loc, "block arg given in index assignment");
13580 }
13581}
13582
13583static NODE *
13584aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13585{
13586 aryset_check(p, idx);
13587 return NEW_ATTRASGN(recv, tASET, idx, loc);
13588}
13589
13590static void
13591block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13592{
13593 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13594 compile_error(p, "both block arg and actual block given");
13595 }
13596}
13597
13598static NODE *
13599attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13600{
13601 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13602 return NEW_ATTRASGN(recv, id, 0, loc);
13603}
13604
13605static VALUE
13606rb_backref_error(struct parser_params *p, NODE *node)
13607{
13608#ifndef RIPPER
13609# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13610#else
13611# define ERR(...) rb_sprintf(__VA_ARGS__)
13612#endif
13613 switch (nd_type(node)) {
13614 case NODE_NTH_REF:
13615 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13616 case NODE_BACK_REF:
13617 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13618 }
13619#undef ERR
13620 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13621}
13622
13623static NODE *
13624arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13625{
13626 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13627 switch (nd_type(node1)) {
13628 case NODE_LIST:
13629 return list_append(p, node1, node2);
13630 case NODE_BLOCK_PASS:
13631 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13632 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13633 return node1;
13634 case NODE_ARGSPUSH:
13635 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13636 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13637 nd_set_type(node1, NODE_ARGSCAT);
13638 return node1;
13639 case NODE_ARGSCAT:
13640 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13641 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13642 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13643 return node1;
13644 }
13645 return NEW_ARGSPUSH(node1, node2, loc);
13646}
13647
13648static NODE *
13649arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13650{
13651 if (!node2) return node1;
13652 switch (nd_type(node1)) {
13653 case NODE_BLOCK_PASS:
13654 if (RNODE_BLOCK_PASS(node1)->nd_head)
13655 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13656 else
13657 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13658 return node1;
13659 case NODE_ARGSPUSH:
13660 if (!nd_type_p(node2, NODE_LIST)) break;
13661 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13662 nd_set_type(node1, NODE_ARGSCAT);
13663 return node1;
13664 case NODE_ARGSCAT:
13665 if (!nd_type_p(node2, NODE_LIST) ||
13666 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13667 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13668 return node1;
13669 }
13670 return NEW_ARGSCAT(node1, node2, loc);
13671}
13672
13673static NODE *
13674last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13675{
13676 NODE *n1;
13677 if ((n1 = splat_array(args)) != 0) {
13678 return list_append(p, n1, last_arg);
13679 }
13680 return arg_append(p, args, last_arg, loc);
13681}
13682
13683static NODE *
13684rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13685{
13686 NODE *n1;
13687 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13688 return list_concat(n1, rest_arg);
13689 }
13690 return arg_concat(p, args, rest_arg, loc);
13691}
13692
13693static NODE *
13694splat_array(NODE* node)
13695{
13696 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13697 if (nd_type_p(node, NODE_LIST)) return node;
13698 return 0;
13699}
13700
13701static void
13702mark_lvar_used(struct parser_params *p, NODE *rhs)
13703{
13704 ID *vidp = NULL;
13705 if (!rhs) return;
13706 switch (nd_type(rhs)) {
13707 case NODE_LASGN:
13708 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13709 if (vidp) *vidp |= LVAR_USED;
13710 }
13711 break;
13712 case NODE_DASGN:
13713 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13714 if (vidp) *vidp |= LVAR_USED;
13715 }
13716 break;
13717#if 0
13718 case NODE_MASGN:
13719 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13720 mark_lvar_used(p, rhs->nd_head);
13721 }
13722 break;
13723#endif
13724 }
13725}
13726
13727static int is_static_content(NODE *node);
13728
13729static NODE *
13730node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13731{
13732 if (!lhs) return 0;
13733
13734 switch (nd_type(lhs)) {
13735 case NODE_CDECL:
13736 case NODE_GASGN:
13737 case NODE_IASGN:
13738 case NODE_LASGN:
13739 case NODE_DASGN:
13740 case NODE_MASGN:
13741 case NODE_CVASGN:
13742 set_nd_value(p, lhs, rhs);
13743 nd_set_loc(lhs, loc);
13744 break;
13745
13746 case NODE_ATTRASGN:
13747 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13748 nd_set_loc(lhs, loc);
13749 break;
13750
13751 default:
13752 /* should not happen */
13753 break;
13754 }
13755
13756 return lhs;
13757}
13758
13759static NODE *
13760value_expr_check(struct parser_params *p, NODE *node)
13761{
13762 NODE *void_node = 0, *vn;
13763
13764 if (!node) {
13765 rb_warning0("empty expression");
13766 }
13767 while (node) {
13768 switch (nd_type(node)) {
13769 case NODE_ENSURE:
13770 vn = RNODE_ENSURE(node)->nd_head;
13771 node = RNODE_ENSURE(node)->nd_ensr;
13772 /* nd_ensr should not be NULL, check it out next */
13773 if (vn && (vn = value_expr_check(p, vn))) {
13774 goto found;
13775 }
13776 break;
13777
13778 case NODE_RESCUE:
13779 /* void only if all children are void */
13780 vn = RNODE_RESCUE(node)->nd_head;
13781 if (!vn || !(vn = value_expr_check(p, vn))) return NULL;
13782 if (!void_node) void_node = vn;
13783 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13784 if (!nd_type_p(r, NODE_RESBODY)) {
13785 compile_error(p, "unexpected node");
13786 return NULL;
13787 }
13788 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13789 void_node = 0;
13790 break;
13791 }
13792 if (!void_node) void_node = vn;
13793 }
13794 node = RNODE_RESCUE(node)->nd_else;
13795 if (!node) return void_node;
13796 break;
13797
13798 case NODE_RETURN:
13799 case NODE_BREAK:
13800 case NODE_NEXT:
13801 case NODE_REDO:
13802 case NODE_RETRY:
13803 goto found;
13804
13805 case NODE_CASE3:
13806 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13807 compile_error(p, "unexpected node");
13808 return NULL;
13809 }
13810 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13811 return NULL;
13812 }
13813 /* single line pattern matching with "=>" operator */
13814 goto found;
13815
13816 case NODE_BLOCK:
13817 while (RNODE_BLOCK(node)->nd_next) {
13818 node = RNODE_BLOCK(node)->nd_next;
13819 }
13820 node = RNODE_BLOCK(node)->nd_head;
13821 break;
13822
13823 case NODE_BEGIN:
13824 node = RNODE_BEGIN(node)->nd_body;
13825 break;
13826
13827 case NODE_IF:
13828 case NODE_UNLESS:
13829 if (!RNODE_IF(node)->nd_body) {
13830 return NULL;
13831 }
13832 else if (!RNODE_IF(node)->nd_else) {
13833 return NULL;
13834 }
13835 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13836 if (!vn) return NULL;
13837 if (!void_node) void_node = vn;
13838 node = RNODE_IF(node)->nd_else;
13839 break;
13840
13841 case NODE_AND:
13842 case NODE_OR:
13843 node = RNODE_AND(node)->nd_1st;
13844 break;
13845
13846 case NODE_LASGN:
13847 case NODE_DASGN:
13848 case NODE_MASGN:
13849 mark_lvar_used(p, node);
13850 return NULL;
13851
13852 default:
13853 return NULL;
13854 }
13855 }
13856
13857 return NULL;
13858
13859 found:
13860 /* return the first found node */
13861 return void_node ? void_node : node;
13862}
13863
13864static int
13865value_expr_gen(struct parser_params *p, NODE *node)
13866{
13867 NODE *void_node = value_expr_check(p, node);
13868 if (void_node) {
13869 yyerror1(&void_node->nd_loc, "void value expression");
13870 /* or "control never reach"? */
13871 return FALSE;
13872 }
13873 return TRUE;
13874}
13875
13876static void
13877void_expr(struct parser_params *p, NODE *node)
13878{
13879 const char *useless = 0;
13880
13881 if (!RTEST(ruby_verbose)) return;
13882
13883 if (!node || !(node = nd_once_body(node))) return;
13884 switch (nd_type(node)) {
13885 case NODE_OPCALL:
13886 switch (RNODE_OPCALL(node)->nd_mid) {
13887 case '+':
13888 case '-':
13889 case '*':
13890 case '/':
13891 case '%':
13892 case tPOW:
13893 case tUPLUS:
13894 case tUMINUS:
13895 case '|':
13896 case '^':
13897 case '&':
13898 case tCMP:
13899 case '>':
13900 case tGEQ:
13901 case '<':
13902 case tLEQ:
13903 case tEQ:
13904 case tNEQ:
13905 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13906 break;
13907 }
13908 break;
13909
13910 case NODE_LVAR:
13911 case NODE_DVAR:
13912 case NODE_GVAR:
13913 case NODE_IVAR:
13914 case NODE_CVAR:
13915 case NODE_NTH_REF:
13916 case NODE_BACK_REF:
13917 useless = "a variable";
13918 break;
13919 case NODE_CONST:
13920 useless = "a constant";
13921 break;
13922 case NODE_SYM:
13923 case NODE_LINE:
13924 case NODE_FILE:
13925 case NODE_ENCODING:
13926 case NODE_INTEGER:
13927 case NODE_FLOAT:
13928 case NODE_RATIONAL:
13929 case NODE_IMAGINARY:
13930 case NODE_STR:
13931 case NODE_DSTR:
13932 case NODE_REGX:
13933 case NODE_DREGX:
13934 useless = "a literal";
13935 break;
13936 case NODE_COLON2:
13937 case NODE_COLON3:
13938 useless = "::";
13939 break;
13940 case NODE_DOT2:
13941 useless = "..";
13942 break;
13943 case NODE_DOT3:
13944 useless = "...";
13945 break;
13946 case NODE_SELF:
13947 useless = "self";
13948 break;
13949 case NODE_NIL:
13950 useless = "nil";
13951 break;
13952 case NODE_TRUE:
13953 useless = "true";
13954 break;
13955 case NODE_FALSE:
13956 useless = "false";
13957 break;
13958 case NODE_DEFINED:
13959 useless = "defined?";
13960 break;
13961 }
13962
13963 if (useless) {
13964 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
13965 }
13966}
13967
13968/* warns useless use of block and returns the last statement node */
13969static NODE *
13970void_stmts(struct parser_params *p, NODE *node)
13971{
13972 NODE *const n = node;
13973 if (!RTEST(ruby_verbose)) return n;
13974 if (!node) return n;
13975 if (!nd_type_p(node, NODE_BLOCK)) return n;
13976
13977 while (RNODE_BLOCK(node)->nd_next) {
13978 void_expr(p, RNODE_BLOCK(node)->nd_head);
13979 node = RNODE_BLOCK(node)->nd_next;
13980 }
13981 return RNODE_BLOCK(node)->nd_head;
13982}
13983
13984static NODE *
13985remove_begin(NODE *node)
13986{
13987 NODE **n = &node, *n1 = node;
13988 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
13989 *n = n1 = RNODE_BEGIN(n1)->nd_body;
13990 }
13991 return node;
13992}
13993
13994static void
13995reduce_nodes(struct parser_params *p, NODE **body)
13996{
13997 NODE *node = *body;
13998
13999 if (!node) {
14000 *body = NEW_NIL(&NULL_LOC);
14001 return;
14002 }
14003#define subnodes(type, n1, n2) \
14004 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14005 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14006 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14007
14008 while (node) {
14009 int newline = (int)nd_fl_newline(node);
14010 switch (nd_type(node)) {
14011 end:
14012 case NODE_NIL:
14013 *body = 0;
14014 return;
14015 case NODE_BEGIN:
14016 *body = node = RNODE_BEGIN(node)->nd_body;
14017 if (newline && node) nd_set_fl_newline(node);
14018 continue;
14019 case NODE_BLOCK:
14020 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14021 break;
14022 case NODE_IF:
14023 case NODE_UNLESS:
14024 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14025 return;
14026 case NODE_CASE:
14027 body = &RNODE_CASE(node)->nd_body;
14028 break;
14029 case NODE_WHEN:
14030 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14031 break;
14032 case NODE_ENSURE:
14033 body = &RNODE_ENSURE(node)->nd_head;
14034 break;
14035 case NODE_RESCUE:
14036 newline = 0; // RESBODY should not be a NEWLINE
14037 if (RNODE_RESCUE(node)->nd_else) {
14038 body = &RNODE_RESCUE(node)->nd_resq;
14039 break;
14040 }
14041 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14042 break;
14043 default:
14044 return;
14045 }
14046 node = *body;
14047 if (newline && node) nd_set_fl_newline(node);
14048 }
14049
14050#undef subnodes
14051}
14052
14053static int
14054is_static_content(NODE *node)
14055{
14056 if (!node) return 1;
14057 switch (nd_type(node)) {
14058 case NODE_HASH:
14059 if (!(node = RNODE_HASH(node)->nd_head)) break;
14060 case NODE_LIST:
14061 do {
14062 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14063 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14064 case NODE_SYM:
14065 case NODE_REGX:
14066 case NODE_LINE:
14067 case NODE_FILE:
14068 case NODE_ENCODING:
14069 case NODE_INTEGER:
14070 case NODE_FLOAT:
14071 case NODE_RATIONAL:
14072 case NODE_IMAGINARY:
14073 case NODE_STR:
14074 case NODE_NIL:
14075 case NODE_TRUE:
14076 case NODE_FALSE:
14077 case NODE_ZLIST:
14078 break;
14079 default:
14080 return 0;
14081 }
14082 return 1;
14083}
14084
14085static int
14086assign_in_cond(struct parser_params *p, NODE *node)
14087{
14088 switch (nd_type(node)) {
14089 case NODE_MASGN:
14090 case NODE_LASGN:
14091 case NODE_DASGN:
14092 case NODE_GASGN:
14093 case NODE_IASGN:
14094 case NODE_CVASGN:
14095 case NODE_CDECL:
14096 break;
14097
14098 default:
14099 return 0;
14100 }
14101
14102 if (!get_nd_value(p, node)) return 1;
14103 if (is_static_content(get_nd_value(p, node))) {
14104 /* reports always */
14105 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14106 }
14107 return 1;
14108}
14109
14110enum cond_type {
14111 COND_IN_OP,
14112 COND_IN_COND,
14113 COND_IN_FF
14114};
14115
14116#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14117 switch (t) { \
14118 case COND_IN_OP: break; \
14119 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14120 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14121 } \
14122} while (0)
14123
14124static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14125
14126static NODE*
14127range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14128{
14129 enum node_type type;
14130
14131 if (node == 0) return 0;
14132
14133 type = nd_type(node);
14134 value_expr(node);
14135 if (type == NODE_INTEGER) {
14136 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14137 ID lineno = rb_intern("$.");
14138 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14139 }
14140 return cond0(p, node, COND_IN_FF, loc, true);
14141}
14142
14143static NODE*
14144cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14145{
14146 if (node == 0) return 0;
14147 if (!(node = nd_once_body(node))) return 0;
14148 assign_in_cond(p, node);
14149
14150 switch (nd_type(node)) {
14151 case NODE_BEGIN:
14152 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14153 break;
14154
14155 case NODE_DSTR:
14156 case NODE_EVSTR:
14157 case NODE_STR:
14158 case NODE_FILE:
14159 SWITCH_BY_COND_TYPE(type, warn, "string ");
14160 break;
14161
14162 case NODE_REGX:
14163 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14164 nd_set_type(node, NODE_MATCH);
14165 break;
14166
14167 case NODE_DREGX:
14168 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14169
14170 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14171
14172 case NODE_BLOCK:
14173 {
14174 NODE *end = RNODE_BLOCK(node)->nd_end;
14175 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14176 if (top) top = node == end;
14177 *expr = cond0(p, *expr, type, loc, top);
14178 }
14179 break;
14180
14181 case NODE_AND:
14182 case NODE_OR:
14183 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14184 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14185 break;
14186
14187 case NODE_DOT2:
14188 case NODE_DOT3:
14189 if (!top) break;
14190 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14191 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14192 switch (nd_type(node)) {
14193 case NODE_DOT2:
14194 nd_set_type(node,NODE_FLIP2);
14195 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14196 (void)flip2;
14197 break;
14198 case NODE_DOT3:
14199 nd_set_type(node, NODE_FLIP3);
14200 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14201 (void)flip3;
14202 break;
14203 }
14204 break;
14205
14206 case NODE_SYM:
14207 case NODE_DSYM:
14208 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14209 break;
14210
14211 case NODE_LINE:
14212 case NODE_ENCODING:
14213 case NODE_INTEGER:
14214 case NODE_FLOAT:
14215 case NODE_RATIONAL:
14216 case NODE_IMAGINARY:
14217 SWITCH_BY_COND_TYPE(type, warning, "");
14218 break;
14219
14220 default:
14221 break;
14222 }
14223 return node;
14224}
14225
14226static NODE*
14227cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14228{
14229 if (node == 0) return 0;
14230 return cond0(p, node, COND_IN_COND, loc, true);
14231}
14232
14233static NODE*
14234method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14235{
14236 if (node == 0) return 0;
14237 return cond0(p, node, COND_IN_OP, loc, true);
14238}
14239
14240static NODE*
14241new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14242{
14243 YYLTYPE loc = {*pos, *pos};
14244 return NEW_NIL(&loc);
14245}
14246
14247static NODE*
14248new_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)
14249{
14250 if (!cc) return right;
14251 cc = cond0(p, cc, COND_IN_COND, loc, true);
14252 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14253}
14254
14255static NODE*
14256new_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)
14257{
14258 if (!cc) return right;
14259 cc = cond0(p, cc, COND_IN_COND, loc, true);
14260 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14261}
14262
14263#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))
14264
14265static NODE*
14266logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14267 const YYLTYPE *op_loc, const YYLTYPE *loc)
14268{
14269 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14270 NODE *op;
14271 value_expr(left);
14272 if (left && nd_type_p(left, type)) {
14273 NODE *node = left, *second;
14274 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14275 node = second;
14276 }
14277 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14278 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14279 left->nd_loc.end_pos = loc->end_pos;
14280 return left;
14281 }
14282 op = NEW_AND_OR(type, left, right, loc, op_loc);
14283 nd_set_line(op, op_loc->beg_pos.lineno);
14284 return op;
14285}
14286
14287#undef NEW_AND_OR
14288
14289static void
14290no_blockarg(struct parser_params *p, NODE *node)
14291{
14292 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14293 compile_error(p, "block argument should not be given");
14294 }
14295}
14296
14297static NODE *
14298ret_args(struct parser_params *p, NODE *node)
14299{
14300 if (node) {
14301 no_blockarg(p, node);
14302 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14303 node = RNODE_LIST(node)->nd_head;
14304 }
14305 }
14306 return node;
14307}
14308
14309static NODE*
14310negate_lit(struct parser_params *p, NODE* node)
14311{
14312 switch (nd_type(node)) {
14313 case NODE_INTEGER:
14314 RNODE_INTEGER(node)->minus = TRUE;
14315 break;
14316 case NODE_FLOAT:
14317 RNODE_FLOAT(node)->minus = TRUE;
14318 break;
14319 case NODE_RATIONAL:
14320 RNODE_RATIONAL(node)->minus = TRUE;
14321 break;
14322 case NODE_IMAGINARY:
14323 RNODE_IMAGINARY(node)->minus = TRUE;
14324 break;
14325 }
14326 return node;
14327}
14328
14329static NODE *
14330arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14331{
14332 if (node2) {
14333 if (!node1) return (NODE *)node2;
14334 node2->nd_head = node1;
14335 nd_set_first_lineno(node2, nd_first_lineno(node1));
14336 nd_set_first_column(node2, nd_first_column(node1));
14337 return (NODE *)node2;
14338 }
14339 return node1;
14340}
14341
14342static bool
14343args_info_empty_p(struct rb_args_info *args)
14344{
14345 if (args->pre_args_num) return false;
14346 if (args->post_args_num) return false;
14347 if (args->rest_arg) return false;
14348 if (args->opt_args) return false;
14349 if (args->block_arg) return false;
14350 if (args->kw_args) return false;
14351 if (args->kw_rest_arg) return false;
14352 return true;
14353}
14354
14355static rb_node_args_t *
14356new_args(struct parser_params *p, rb_node_args_aux_t *pre_args, rb_node_opt_arg_t *opt_args, ID rest_arg, rb_node_args_aux_t *post_args, rb_node_args_t *tail, const YYLTYPE *loc)
14357{
14358 struct rb_args_info *args = &tail->nd_ainfo;
14359
14360 if (args->forwarding) {
14361 if (rest_arg) {
14362 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14363 return tail;
14364 }
14365 rest_arg = idFWD_REST;
14366 }
14367
14368 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14369 args->pre_init = pre_args ? pre_args->nd_next : 0;
14370
14371 args->post_args_num = post_args ? post_args->nd_plen : 0;
14372 args->post_init = post_args ? post_args->nd_next : 0;
14373 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14374
14375 args->rest_arg = rest_arg;
14376
14377 args->opt_args = opt_args;
14378
14379#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14380 args->ruby2_keywords = args->forwarding;
14381#else
14382 args->ruby2_keywords = 0;
14383#endif
14384
14385 nd_set_loc(RNODE(tail), loc);
14386
14387 return tail;
14388}
14389
14390static rb_node_args_t *
14391new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14392{
14393 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14394 struct rb_args_info *args = &node->nd_ainfo;
14395 if (p->error_p) return node;
14396
14397 args->block_arg = block;
14398 args->kw_args = kw_args;
14399
14400 if (kw_args) {
14401 /*
14402 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14403 * variable order: k1, kr1, k2, &b, internal_id, krest
14404 * #=> <reorder>
14405 * variable order: kr1, k1, k2, internal_id, krest, &b
14406 */
14407 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14408 struct vtable *vtargs = p->lvtbl->args;
14409 rb_node_kw_arg_t *kwn = kw_args;
14410
14411 if (block) block = vtargs->tbl[vtargs->pos-1];
14412 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14413 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14414 while (kwn) {
14415 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14416 --kw_vars;
14417 --required_kw_vars;
14418 kwn = kwn->nd_next;
14419 }
14420
14421 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14422 ID vid = get_nd_vid(p, kwn->nd_body);
14423 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14424 *required_kw_vars++ = vid;
14425 }
14426 else {
14427 *kw_vars++ = vid;
14428 }
14429 }
14430
14431 arg_var(p, kw_bits);
14432 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14433 if (block) arg_var(p, block);
14434
14435 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14436 }
14437 else if (kw_rest_arg == idNil) {
14438 args->no_kwarg = 1;
14439 }
14440 else if (kw_rest_arg) {
14441 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14442 }
14443
14444 return node;
14445}
14446
14447static rb_node_args_t *
14448args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14449{
14450 if (max_numparam > NO_PARAM || it_id) {
14451 if (!args) {
14452 YYLTYPE loc = RUBY_INIT_YYLLOC();
14453 args = new_args_tail(p, 0, 0, 0, 0);
14454 nd_set_loc(RNODE(args), &loc);
14455 }
14456 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14457 }
14458 return args;
14459}
14460
14461static NODE*
14462new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14463{
14464 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14465
14466 if (pre_arg) {
14467 NODE *pre_args = NEW_LIST(pre_arg, loc);
14468 if (RNODE_ARYPTN(aryptn)->pre_args) {
14469 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14470 }
14471 else {
14472 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14473 }
14474 }
14475 return aryptn;
14476}
14477
14478static NODE*
14479new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14480{
14481 if (has_rest) {
14482 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14483 }
14484 else {
14485 rest_arg = NULL;
14486 }
14487 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14488
14489 return node;
14490}
14491
14492static NODE*
14493new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14494{
14495 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14496
14497 return fndptn;
14498}
14499
14500static NODE*
14501new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14502{
14503 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14504 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14505 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14506
14507 return node;
14508}
14509
14510static NODE*
14511new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14512{
14513 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14514 return hshptn;
14515}
14516
14517static NODE*
14518new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14519{
14520 NODE *node, *kw_rest_arg_node;
14521
14522 if (kw_rest_arg == idNil) {
14523 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14524 }
14525 else if (kw_rest_arg) {
14526 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14527 }
14528 else {
14529 kw_rest_arg_node = NULL;
14530 }
14531
14532 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14533
14534 return node;
14535}
14536
14537static NODE*
14538dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14539{
14540 if (!node) {
14541 return NEW_SYM(STR_NEW0(), loc);
14542 }
14543
14544 switch (nd_type(node)) {
14545 case NODE_DSTR:
14546 nd_set_type(node, NODE_DSYM);
14547 nd_set_loc(node, loc);
14548 break;
14549 case NODE_STR:
14550 node = str_to_sym_node(p, node, loc);
14551 break;
14552 default:
14553 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14554 break;
14555 }
14556 return node;
14557}
14558
14559static int
14560nd_type_st_key_enable_p(NODE *node)
14561{
14562 switch (nd_type(node)) {
14563 case NODE_INTEGER:
14564 case NODE_FLOAT:
14565 case NODE_RATIONAL:
14566 case NODE_IMAGINARY:
14567 case NODE_STR:
14568 case NODE_SYM:
14569 case NODE_REGX:
14570 case NODE_LINE:
14571 case NODE_FILE:
14572 case NODE_ENCODING:
14573 return true;
14574 default:
14575 return false;
14576 }
14577}
14578
14579static VALUE
14580nd_value(struct parser_params *p, NODE *node)
14581{
14582 switch (nd_type(node)) {
14583 case NODE_STR:
14584 return rb_node_str_string_val(node);
14585 case NODE_INTEGER:
14586 return rb_node_integer_literal_val(node);
14587 case NODE_FLOAT:
14588 return rb_node_float_literal_val(node);
14589 case NODE_RATIONAL:
14590 return rb_node_rational_literal_val(node);
14591 case NODE_IMAGINARY:
14592 return rb_node_imaginary_literal_val(node);
14593 case NODE_SYM:
14594 return rb_node_sym_string_val(node);
14595 case NODE_REGX:
14596 return rb_node_regx_string_val(node);
14597 case NODE_LINE:
14598 return rb_node_line_lineno_val(node);
14599 case NODE_ENCODING:
14600 return rb_node_encoding_val(node);
14601 case NODE_FILE:
14602 return rb_node_file_path_val(node);
14603 default:
14604 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14605 UNREACHABLE_RETURN(0);
14606 }
14607}
14608
14609static void
14610warn_duplicate_keys(struct parser_params *p, NODE *hash)
14611{
14612 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14613 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14614 while (hash && RNODE_LIST(hash)->nd_next) {
14615 NODE *head = RNODE_LIST(hash)->nd_head;
14616 NODE *value = RNODE_LIST(hash)->nd_next;
14617 NODE *next = RNODE_LIST(value)->nd_next;
14618 st_data_t key;
14619 st_data_t data;
14620
14621 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14622 if (!head) {
14623 head = value;
14624 }
14625
14626 if (nd_type_st_key_enable_p(head)) {
14627 key = (st_data_t)head;
14628
14629 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14630 rb_warn2L(nd_line((NODE *)data),
14631 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14632 nd_value(p, head), WARN_I(nd_line(head)));
14633 }
14634 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14635 }
14636 hash = next;
14637 }
14638 st_free_table(p->warn_duplicate_keys_table);
14639 p->warn_duplicate_keys_table = NULL;
14640}
14641
14642static NODE *
14643new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14644{
14645 if (hash) warn_duplicate_keys(p, hash);
14646 return NEW_HASH(hash, loc);
14647}
14648
14649static void
14650error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14651{
14652 if (is_private_local_id(p, id)) {
14653 return;
14654 }
14655 if (st_is_member(p->pvtbl, id)) {
14656 yyerror1(loc, "duplicated variable name");
14657 }
14658 else {
14659 st_insert(p->pvtbl, (st_data_t)id, 0);
14660 }
14661}
14662
14663static void
14664error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14665{
14666 if (!p->pktbl) {
14667 p->pktbl = st_init_numtable();
14668 }
14669 else if (st_is_member(p->pktbl, key)) {
14670 yyerror1(loc, "duplicated key name");
14671 return;
14672 }
14673 st_insert(p->pktbl, (st_data_t)key, 0);
14674}
14675
14676static NODE *
14677new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14678{
14679 return NEW_HASH(hash, loc);
14680}
14681
14682static NODE *
14683new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14684{
14685 NODE *asgn;
14686
14687 if (lhs) {
14688 ID vid = get_nd_vid(p, lhs);
14689 YYLTYPE lhs_loc = lhs->nd_loc;
14690 if (op == tOROP) {
14691 set_nd_value(p, lhs, rhs);
14692 nd_set_loc(lhs, loc);
14693 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14694 }
14695 else if (op == tANDOP) {
14696 set_nd_value(p, lhs, rhs);
14697 nd_set_loc(lhs, loc);
14698 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14699 }
14700 else {
14701 asgn = lhs;
14702 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14703 set_nd_value(p, asgn, rhs);
14704 nd_set_loc(asgn, loc);
14705 }
14706 }
14707 else {
14708 asgn = NEW_ERROR(loc);
14709 }
14710 return asgn;
14711}
14712
14713static NODE *
14714new_ary_op_assign(struct parser_params *p, NODE *ary,
14715 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14716 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14717{
14718 NODE *asgn;
14719
14720 aryset_check(p, args);
14721 args = make_list(args, args_loc);
14722 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14723 fixpos(asgn, ary);
14724 return asgn;
14725}
14726
14727static NODE *
14728new_attr_op_assign(struct parser_params *p, NODE *lhs,
14729 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14730 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14731{
14732 NODE *asgn;
14733
14734 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14735 fixpos(asgn, lhs);
14736 return asgn;
14737}
14738
14739static NODE *
14740new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14741{
14742 NODE *asgn;
14743
14744 if (lhs) {
14745 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14746 }
14747 else {
14748 asgn = NEW_ERROR(loc);
14749 }
14750 fixpos(asgn, lhs);
14751 return asgn;
14752}
14753
14754static NODE *
14755const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14756{
14757 if (p->ctxt.in_def) {
14758#ifndef RIPPER
14759 yyerror1(loc, "dynamic constant assignment");
14760#else
14761 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14762#endif
14763 }
14764 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14765}
14766
14767#ifdef RIPPER
14768static VALUE
14769assign_error(struct parser_params *p, const char *mesg, VALUE a)
14770{
14771 a = dispatch2(assign_error, ERR_MESG(), a);
14772 ripper_error(p);
14773 return a;
14774}
14775#endif
14776
14777static NODE *
14778new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14779{
14780 NODE *result = head;
14781 if (rescue) {
14782 NODE *tmp = rescue_else ? rescue_else : rescue;
14783 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14784
14785 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14786 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14787 }
14788 if (ensure) {
14789 result = NEW_ENSURE(result, ensure, loc);
14790 }
14791 fixpos(result, head);
14792 return result;
14793}
14794
14795static void
14796warn_unused_var(struct parser_params *p, struct local_vars *local)
14797{
14798 int cnt;
14799
14800 if (!local->used) return;
14801 cnt = local->used->pos;
14802 if (cnt != local->vars->pos) {
14803 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14804 }
14805#ifndef RIPPER
14806 ID *v = local->vars->tbl;
14807 ID *u = local->used->tbl;
14808 for (int i = 0; i < cnt; ++i) {
14809 if (!v[i] || (u[i] & LVAR_USED)) continue;
14810 if (is_private_local_id(p, v[i])) continue;
14811 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14812 }
14813#endif
14814}
14815
14816static void
14817local_push(struct parser_params *p, int toplevel_scope)
14818{
14819 struct local_vars *local;
14820 int inherits_dvars = toplevel_scope && compile_for_eval;
14821 int warn_unused_vars = RTEST(ruby_verbose);
14822
14823 local = ALLOC(struct local_vars);
14824 local->prev = p->lvtbl;
14825 local->args = vtable_alloc(0);
14826 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14827#ifndef RIPPER
14828 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14829 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14830#endif
14831 local->numparam.outer = 0;
14832 local->numparam.inner = 0;
14833 local->numparam.current = 0;
14834 local->it = 0;
14835 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14836
14837# if WARN_PAST_SCOPE
14838 local->past = 0;
14839# endif
14840 CMDARG_PUSH(0);
14841 COND_PUSH(0);
14842 p->lvtbl = local;
14843}
14844
14845static void
14846vtable_chain_free(struct parser_params *p, struct vtable *table)
14847{
14848 while (!DVARS_TERMINAL_P(table)) {
14849 struct vtable *cur_table = table;
14850 table = cur_table->prev;
14851 vtable_free(cur_table);
14852 }
14853}
14854
14855static void
14856local_free(struct parser_params *p, struct local_vars *local)
14857{
14858 vtable_chain_free(p, local->used);
14859
14860# if WARN_PAST_SCOPE
14861 vtable_chain_free(p, local->past);
14862# endif
14863
14864 vtable_chain_free(p, local->args);
14865 vtable_chain_free(p, local->vars);
14866
14867 ruby_sized_xfree(local, sizeof(struct local_vars));
14868}
14869
14870static void
14871local_pop(struct parser_params *p)
14872{
14873 struct local_vars *local = p->lvtbl->prev;
14874 if (p->lvtbl->used) {
14875 warn_unused_var(p, p->lvtbl);
14876 }
14877
14878 local_free(p, p->lvtbl);
14879 p->lvtbl = local;
14880
14881 CMDARG_POP();
14882 COND_POP();
14883}
14884
14885static rb_ast_id_table_t *
14886local_tbl(struct parser_params *p)
14887{
14888 int cnt_args = vtable_size(p->lvtbl->args);
14889 int cnt_vars = vtable_size(p->lvtbl->vars);
14890 int cnt = cnt_args + cnt_vars;
14891 int i, j;
14892 rb_ast_id_table_t *tbl;
14893
14894 if (cnt <= 0) return 0;
14895 tbl = rb_ast_new_local_table(p->ast, cnt);
14896 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14897 /* remove IDs duplicated to warn shadowing */
14898 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14899 ID id = p->lvtbl->vars->tbl[i];
14900 if (!vtable_included(p->lvtbl->args, id)) {
14901 tbl->ids[j++] = id;
14902 }
14903 }
14904 if (j < cnt) {
14905 tbl = rb_ast_resize_latest_local_table(p->ast, j);
14906 }
14907
14908 return tbl;
14909}
14910
14911static void
14912numparam_name(struct parser_params *p, ID id)
14913{
14914 if (!NUMPARAM_ID_P(id)) return;
14915 compile_error(p, "_%d is reserved for numbered parameter",
14916 NUMPARAM_ID_TO_IDX(id));
14917}
14918
14919static void
14920arg_var(struct parser_params *p, ID id)
14921{
14922 numparam_name(p, id);
14923 vtable_add(p->lvtbl->args, id);
14924}
14925
14926static void
14927local_var(struct parser_params *p, ID id)
14928{
14929 numparam_name(p, id);
14930 vtable_add(p->lvtbl->vars, id);
14931 if (p->lvtbl->used) {
14932 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
14933 }
14934}
14935
14936#ifndef RIPPER
14937int
14938rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
14939{
14940 return rb_local_defined(id, iseq);
14941}
14942#endif
14943
14944static int
14945local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
14946{
14947 struct vtable *vars, *args, *used;
14948
14949 vars = p->lvtbl->vars;
14950 args = p->lvtbl->args;
14951 used = p->lvtbl->used;
14952
14953 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
14954 vars = vars->prev;
14955 args = args->prev;
14956 if (used) used = used->prev;
14957 }
14958
14959 if (vars && vars->prev == DVARS_INHERIT) {
14960 return rb_parser_local_defined(p, id, p->parent_iseq);
14961 }
14962 else if (vtable_included(args, id)) {
14963 return 1;
14964 }
14965 else {
14966 int i = vtable_included(vars, id);
14967 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
14968 return i != 0;
14969 }
14970}
14971
14972static int
14973local_id(struct parser_params *p, ID id)
14974{
14975 return local_id_ref(p, id, NULL);
14976}
14977
14978static int
14979check_forwarding_args(struct parser_params *p)
14980{
14981 if (local_id(p, idFWD_ALL)) return TRUE;
14982 compile_error(p, "unexpected ...");
14983 return FALSE;
14984}
14985
14986static void
14987add_forwarding_args(struct parser_params *p)
14988{
14989 arg_var(p, idFWD_REST);
14990#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14991 arg_var(p, idFWD_KWREST);
14992#endif
14993 arg_var(p, idFWD_BLOCK);
14994 arg_var(p, idFWD_ALL);
14995}
14996
14997static void
14998forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
14999{
15000 bool conflict = false;
15001
15002 struct vtable *vars, *args;
15003
15004 vars = p->lvtbl->vars;
15005 args = p->lvtbl->args;
15006
15007 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15008 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15009 vars = vars->prev;
15010 args = args->prev;
15011 }
15012
15013 bool found = false;
15014 if (vars && vars->prev == DVARS_INHERIT && !found) {
15015 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15016 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15017 }
15018 else {
15019 found = (vtable_included(args, arg) &&
15020 !(all && vtable_included(args, all)));
15021 }
15022
15023 if (!found) {
15024 compile_error(p, "no anonymous %s parameter", var);
15025 }
15026 else if (conflict) {
15027 compile_error(p, "anonymous %s parameter is also used within block", var);
15028 }
15029}
15030
15031static NODE *
15032new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15033{
15034 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15035#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15036 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15037#endif
15038 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15039 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15040 block->forwarding = TRUE;
15041#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15042 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15043#endif
15044 return arg_blk_pass(args, block);
15045}
15046
15047static NODE *
15048numparam_push(struct parser_params *p)
15049{
15050 struct local_vars *local = p->lvtbl;
15051 NODE *inner = local->numparam.inner;
15052 if (!local->numparam.outer) {
15053 local->numparam.outer = local->numparam.current;
15054 }
15055 local->numparam.inner = 0;
15056 local->numparam.current = 0;
15057 local->it = 0;
15058 return inner;
15059}
15060
15061static void
15062numparam_pop(struct parser_params *p, NODE *prev_inner)
15063{
15064 struct local_vars *local = p->lvtbl;
15065 if (prev_inner) {
15066 /* prefer first one */
15067 local->numparam.inner = prev_inner;
15068 }
15069 else if (local->numparam.current) {
15070 /* current and inner are exclusive */
15071 local->numparam.inner = local->numparam.current;
15072 }
15073 if (p->max_numparam > NO_PARAM) {
15074 /* current and outer are exclusive */
15075 local->numparam.current = local->numparam.outer;
15076 local->numparam.outer = 0;
15077 }
15078 else {
15079 /* no numbered parameter */
15080 local->numparam.current = 0;
15081 }
15082 local->it = 0;
15083}
15084
15085static const struct vtable *
15086dyna_push(struct parser_params *p)
15087{
15088 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15089 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15090 if (p->lvtbl->used) {
15091 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15092 }
15093 return p->lvtbl->args;
15094}
15095
15096static void
15097dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15098{
15099 struct vtable *tmp = *vtblp;
15100 *vtblp = tmp->prev;
15101# if WARN_PAST_SCOPE
15102 if (p->past_scope_enabled) {
15103 tmp->prev = p->lvtbl->past;
15104 p->lvtbl->past = tmp;
15105 return;
15106 }
15107# endif
15108 vtable_free(tmp);
15109}
15110
15111static void
15112dyna_pop_1(struct parser_params *p)
15113{
15114 struct vtable *tmp;
15115
15116 if ((tmp = p->lvtbl->used) != 0) {
15117 warn_unused_var(p, p->lvtbl);
15118 p->lvtbl->used = p->lvtbl->used->prev;
15119 vtable_free(tmp);
15120 }
15121 dyna_pop_vtable(p, &p->lvtbl->args);
15122 dyna_pop_vtable(p, &p->lvtbl->vars);
15123}
15124
15125static void
15126dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15127{
15128 while (p->lvtbl->args != lvargs) {
15129 dyna_pop_1(p);
15130 if (!p->lvtbl->args) {
15131 struct local_vars *local = p->lvtbl->prev;
15132 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15133 p->lvtbl = local;
15134 }
15135 }
15136 dyna_pop_1(p);
15137}
15138
15139static int
15140dyna_in_block(struct parser_params *p)
15141{
15142 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15143}
15144
15145#ifndef RIPPER
15146int
15147dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15148{
15149 struct vtable *vars, *args, *used;
15150 int i;
15151
15152 args = p->lvtbl->args;
15153 vars = p->lvtbl->vars;
15154 used = p->lvtbl->used;
15155
15156 while (!DVARS_TERMINAL_P(vars)) {
15157 if (vtable_included(args, id)) {
15158 return 1;
15159 }
15160 if ((i = vtable_included(vars, id)) != 0) {
15161 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15162 return 1;
15163 }
15164 args = args->prev;
15165 vars = vars->prev;
15166 if (!vidrefp) used = 0;
15167 if (used) used = used->prev;
15168 }
15169
15170 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15171 return rb_dvar_defined(id, p->parent_iseq);
15172 }
15173
15174 return 0;
15175}
15176#endif
15177
15178static int
15179dvar_defined(struct parser_params *p, ID id)
15180{
15181 return dvar_defined_ref(p, id, NULL);
15182}
15183
15184static int
15185dvar_curr(struct parser_params *p, ID id)
15186{
15187 return (vtable_included(p->lvtbl->args, id) ||
15188 vtable_included(p->lvtbl->vars, id));
15189}
15190
15191static void
15192reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15193{
15194 compile_error(p,
15195 "regexp encoding option '%c' differs from source encoding '%s'",
15196 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15197}
15198
15199#ifndef RIPPER
15200static rb_encoding *
15201find_enc(struct parser_params* p, const char *name)
15202{
15203 int idx = rb_enc_find_index(name);
15204 if (idx < 0) {
15205 rb_bug("unknown encoding name: %s", name);
15206 }
15207
15208 return rb_enc_from_index(idx);
15209}
15210
15211static rb_encoding *
15212kcode_to_enc(struct parser_params* p, int kcode)
15213{
15214 rb_encoding *enc;
15215
15216 switch (kcode) {
15217 case ENC_ASCII8BIT:
15218 enc = rb_ascii8bit_encoding();
15219 break;
15220 case ENC_EUC_JP:
15221 enc = find_enc(p, "EUC-JP");
15222 break;
15223 case ENC_Windows_31J:
15224 enc = find_enc(p, "Windows-31J");
15225 break;
15226 case ENC_UTF8:
15227 enc = rb_utf8_encoding();
15228 break;
15229 default:
15230 enc = NULL;
15231 break;
15232 }
15233
15234 return enc;
15235}
15236
15237int
15238rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15239{
15240 int c = RE_OPTION_ENCODING_IDX(options);
15241
15242 if (c) {
15243 int opt, idx;
15244 rb_encoding *enc;
15245
15246 char_to_option_kcode(c, &opt, &idx);
15247 enc = kcode_to_enc(p, idx);
15248 if (enc != rb_parser_str_get_encoding(str) &&
15249 !rb_parser_is_ascii_string(p, str)) {
15250 goto error;
15251 }
15252 rb_parser_string_set_encoding(str, enc);
15253 }
15254 else if (RE_OPTION_ENCODING_NONE(options)) {
15255 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15256 !rb_parser_is_ascii_string(p, str)) {
15257 c = 'n';
15258 goto error;
15259 }
15260 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15261 }
15262 else if (rb_is_usascii_enc(p->enc)) {
15263 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15264 }
15265 return 0;
15266
15267 error:
15268 return c;
15269}
15270#endif
15271
15272static void
15273reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15274{
15275 int c = rb_reg_fragment_setenc(p, str, options);
15276 if (c) reg_fragment_enc_error(p, str, c);
15277}
15278
15279#ifndef UNIVERSAL_PARSER
15280typedef struct {
15281 struct parser_params* parser;
15282 rb_encoding *enc;
15283 NODE *succ_block;
15284 const YYLTYPE *loc;
15285 rb_parser_assignable_func assignable;
15286} reg_named_capture_assign_t;
15287
15288static int
15289reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15290 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15291{
15292 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15293 struct parser_params* p = arg->parser;
15294 rb_encoding *enc = arg->enc;
15295 long len = name_end - name;
15296 const char *s = (const char *)name;
15297
15298 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15299}
15300
15301static NODE *
15302reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15303{
15304 reg_named_capture_assign_t arg;
15305
15306 arg.parser = p;
15307 arg.enc = rb_enc_get(regexp);
15308 arg.succ_block = 0;
15309 arg.loc = loc;
15310 arg.assignable = assignable;
15311 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15312
15313 if (!arg.succ_block) return 0;
15314 return RNODE_BLOCK(arg.succ_block)->nd_next;
15315}
15316#endif
15317
15318#ifndef RIPPER
15319NODE *
15320rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15321{
15322 return assignable(p, id, val, loc);
15323}
15324
15325int
15326rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15327 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15328{
15329 ID var;
15330 NODE *node, *succ;
15331
15332 if (!len) return ST_CONTINUE;
15333 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15334 return ST_CONTINUE;
15335
15336 var = intern_cstr(s, len, enc);
15337 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15338 if (!lvar_defined(p, var)) return ST_CONTINUE;
15339 }
15340 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15341 succ = *succ_block;
15342 if (!succ) succ = NEW_ERROR(loc);
15343 succ = block_append(p, succ, node);
15344 *succ_block = succ;
15345 return ST_CONTINUE;
15346}
15347#endif
15348
15349static VALUE
15350parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15351{
15352 VALUE str2;
15353 reg_fragment_setenc(p, str, options);
15354 str2 = rb_str_new_parser_string(str);
15355 return rb_parser_reg_compile(p, str2, options);
15356}
15357
15358#ifndef RIPPER
15359VALUE
15360rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15361{
15362 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15363}
15364#endif
15365
15366static VALUE
15367reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15368{
15369 VALUE re;
15370 VALUE err;
15371
15372 err = rb_errinfo();
15373 re = parser_reg_compile(p, str, options);
15374 if (NIL_P(re)) {
15375 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15376 rb_set_errinfo(err);
15377 compile_error(p, "%"PRIsVALUE, m);
15378 return Qnil;
15379 }
15380 return re;
15381}
15382
15383#ifndef RIPPER
15384void
15385rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15386{
15387 p->do_print = print;
15388 p->do_loop = loop;
15389 p->do_chomp = chomp;
15390 p->do_split = split;
15391}
15392
15393static NODE *
15394parser_append_options(struct parser_params *p, NODE *node)
15395{
15396 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15397 const YYLTYPE *const LOC = &default_location;
15398
15399 if (p->do_print) {
15400 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15401 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15402 LOC);
15403 node = block_append(p, node, print);
15404 }
15405
15406 if (p->do_loop) {
15407 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15408
15409 if (p->do_split) {
15410 ID ifs = rb_intern("$;");
15411 ID fields = rb_intern("$F");
15412 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15413 NODE *split = NEW_GASGN(fields,
15414 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15415 rb_intern("split"), args, LOC),
15416 LOC);
15417 node = block_append(p, split, node);
15418 }
15419 if (p->do_chomp) {
15420 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15421 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15422 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15423 }
15424
15425 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15426 }
15427
15428 return node;
15429}
15430
15431void
15432rb_init_parse(void)
15433{
15434 /* just to suppress unused-function warnings */
15435 (void)nodetype;
15436 (void)nodeline;
15437}
15438
15439ID
15440internal_id(struct parser_params *p)
15441{
15442 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15443}
15444#endif /* !RIPPER */
15445
15446static void
15447parser_initialize(struct parser_params *p)
15448{
15449 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15450 p->command_start = TRUE;
15451 p->ruby_sourcefile_string = Qnil;
15452 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15453 string_buffer_init(p);
15454 p->node_id = 0;
15455 p->delayed.token = NULL;
15456 p->frozen_string_literal = -1; /* not specified */
15457#ifndef RIPPER
15458 p->error_buffer = Qfalse;
15459 p->end_expect_token_locations = NULL;
15460 p->token_id = 0;
15461 p->tokens = NULL;
15462#else
15463 p->result = Qnil;
15464 p->parsing_thread = Qnil;
15465 p->s_value = Qnil;
15466 p->s_lvalue = Qnil;
15467 p->s_value_stack = rb_ary_new();
15468#endif
15469 p->debug_buffer = Qnil;
15470 p->debug_output = rb_ractor_stdout();
15471 p->enc = rb_utf8_encoding();
15472 p->exits = 0;
15473}
15474
15475#ifdef RIPPER
15476#define rb_ruby_parser_mark ripper_parser_mark
15477#define rb_ruby_parser_free ripper_parser_free
15478#define rb_ruby_parser_memsize ripper_parser_memsize
15479#endif
15480
15481void
15482rb_ruby_parser_mark(void *ptr)
15483{
15484 struct parser_params *p = (struct parser_params*)ptr;
15485
15486 rb_gc_mark(p->ruby_sourcefile_string);
15487#ifndef RIPPER
15488 rb_gc_mark(p->error_buffer);
15489#else
15490 rb_gc_mark(p->value);
15491 rb_gc_mark(p->result);
15492 rb_gc_mark(p->parsing_thread);
15493 rb_gc_mark(p->s_value);
15494 rb_gc_mark(p->s_lvalue);
15495 rb_gc_mark(p->s_value_stack);
15496#endif
15497 rb_gc_mark(p->debug_buffer);
15498 rb_gc_mark(p->debug_output);
15499}
15500
15501void
15502rb_ruby_parser_free(void *ptr)
15503{
15504 struct parser_params *p = (struct parser_params*)ptr;
15505 struct local_vars *local, *prev;
15506
15507 if (p->ast) {
15508 rb_ast_free(p->ast);
15509 }
15510
15511 if (p->warn_duplicate_keys_table) {
15512 st_free_table(p->warn_duplicate_keys_table);
15513 }
15514
15515#ifndef RIPPER
15516 if (p->tokens) {
15517 rb_parser_ary_free(p, p->tokens);
15518 }
15519#endif
15520
15521 if (p->tokenbuf) {
15522 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15523 }
15524
15525 for (local = p->lvtbl; local; local = prev) {
15526 prev = local->prev;
15527 local_free(p, local);
15528 }
15529
15530 {
15531 token_info *ptinfo;
15532 while ((ptinfo = p->token_info) != 0) {
15533 p->token_info = ptinfo->next;
15534 xfree(ptinfo);
15535 }
15536 }
15537 string_buffer_free(p);
15538
15539 if (p->pvtbl) {
15540 st_free_table(p->pvtbl);
15541 }
15542
15543 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15544 st_free_table(p->case_labels);
15545 }
15546
15547 xfree(p->lex.strterm);
15548 p->lex.strterm = 0;
15549
15550 xfree(ptr);
15551}
15552
15553size_t
15554rb_ruby_parser_memsize(const void *ptr)
15555{
15556 struct parser_params *p = (struct parser_params*)ptr;
15557 struct local_vars *local;
15558 size_t size = sizeof(*p);
15559
15560 size += p->toksiz;
15561 for (local = p->lvtbl; local; local = local->prev) {
15562 size += sizeof(*local);
15563 if (local->vars) size += local->vars->capa * sizeof(ID);
15564 }
15565 return size;
15566}
15567
15568#ifndef RIPPER
15569#undef rb_reserved_word
15570
15571const struct kwtable *
15572rb_reserved_word(const char *str, unsigned int len)
15573{
15574 return reserved_word(str, len);
15575}
15576
15577#ifdef UNIVERSAL_PARSER
15578rb_parser_t *
15579rb_ruby_parser_allocate(const rb_parser_config_t *config)
15580{
15581 /* parser_initialize expects fields to be set to 0 */
15582 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15583 p->config = config;
15584 return p;
15585}
15586
15587rb_parser_t *
15588rb_ruby_parser_new(const rb_parser_config_t *config)
15589{
15590 /* parser_initialize expects fields to be set to 0 */
15591 rb_parser_t *p = rb_ruby_parser_allocate(config);
15592 parser_initialize(p);
15593 return p;
15594}
15595#else
15596rb_parser_t *
15597rb_ruby_parser_allocate(void)
15598{
15599 /* parser_initialize expects fields to be set to 0 */
15600 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15601 return p;
15602}
15603
15604rb_parser_t *
15605rb_ruby_parser_new(void)
15606{
15607 /* parser_initialize expects fields to be set to 0 */
15608 rb_parser_t *p = rb_ruby_parser_allocate();
15609 parser_initialize(p);
15610 return p;
15611}
15612#endif
15613
15614rb_parser_t *
15615rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15616{
15617 p->error_buffer = main ? Qfalse : Qnil;
15618 p->parent_iseq = base;
15619 return p;
15620}
15621
15622void
15623rb_ruby_parser_set_script_lines(rb_parser_t *p)
15624{
15625 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15626}
15627
15628void
15629rb_ruby_parser_error_tolerant(rb_parser_t *p)
15630{
15631 p->error_tolerant = 1;
15632}
15633
15634void
15635rb_ruby_parser_keep_tokens(rb_parser_t *p)
15636{
15637 p->keep_tokens = 1;
15638 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15639}
15640
15641rb_encoding *
15642rb_ruby_parser_encoding(rb_parser_t *p)
15643{
15644 return p->enc;
15645}
15646
15647int
15648rb_ruby_parser_end_seen_p(rb_parser_t *p)
15649{
15650 return p->ruby__end__seen;
15651}
15652
15653int
15654rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15655{
15656 p->debug = flag;
15657 return flag;
15658}
15659#endif /* !RIPPER */
15660
15661#ifdef RIPPER
15662int
15663rb_ruby_parser_get_yydebug(rb_parser_t *p)
15664{
15665 return p->debug;
15666}
15667
15668void
15669rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15670{
15671 p->value = value;
15672}
15673
15674int
15675rb_ruby_parser_error_p(rb_parser_t *p)
15676{
15677 return p->error_p;
15678}
15679
15680VALUE
15681rb_ruby_parser_debug_output(rb_parser_t *p)
15682{
15683 return p->debug_output;
15684}
15685
15686void
15687rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15688{
15689 p->debug_output = output;
15690}
15691
15692VALUE
15693rb_ruby_parser_parsing_thread(rb_parser_t *p)
15694{
15695 return p->parsing_thread;
15696}
15697
15698void
15699rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15700{
15701 p->parsing_thread = parsing_thread;
15702}
15703
15704void
15705rb_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)
15706{
15707 p->lex.gets = gets;
15708 p->lex.input = input;
15709 p->eofp = 0;
15710 p->ruby_sourcefile_string = sourcefile_string;
15711 p->ruby_sourcefile = sourcefile;
15712 p->ruby_sourceline = sourceline;
15713}
15714
15715VALUE
15716rb_ruby_parser_result(rb_parser_t *p)
15717{
15718 return p->result;
15719}
15720
15721rb_encoding *
15722rb_ruby_parser_enc(rb_parser_t *p)
15723{
15724 return p->enc;
15725}
15726
15727VALUE
15728rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15729{
15730 return p->ruby_sourcefile_string;
15731}
15732
15733int
15734rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15735{
15736 return p->ruby_sourceline;
15737}
15738
15739int
15740rb_ruby_parser_lex_state(rb_parser_t *p)
15741{
15742 return p->lex.state;
15743}
15744
15745void
15746rb_ruby_ripper_parse0(rb_parser_t *p)
15747{
15748 parser_prepare(p);
15749 p->ast = rb_ast_new();
15750 ripper_yyparse((void*)p);
15751 rb_ast_free(p->ast);
15752 p->ast = 0;
15753 p->eval_tree = 0;
15754 p->eval_tree_begin = 0;
15755}
15756
15757int
15758rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15759{
15760 return dedent_string(p, string, width);
15761}
15762
15763int
15764rb_ruby_ripper_initialized_p(rb_parser_t *p)
15765{
15766 return p->lex.input != 0;
15767}
15768
15769void
15770rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15771{
15772 parser_initialize(p);
15773}
15774
15775long
15776rb_ruby_ripper_column(rb_parser_t *p)
15777{
15778 return p->lex.ptok - p->lex.pbeg;
15779}
15780
15781long
15782rb_ruby_ripper_token_len(rb_parser_t *p)
15783{
15784 return p->lex.pcur - p->lex.ptok;
15785}
15786
15787rb_parser_string_t *
15788rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15789{
15790 return p->lex.lastline;
15791}
15792
15793VALUE
15794rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15795{
15796 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15797}
15798
15799#ifdef UNIVERSAL_PARSER
15800rb_parser_t *
15801rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15802{
15803 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15804 p->config = config;
15805 return p;
15806}
15807#endif
15808
15809struct parser_params*
15810rb_ruby_ripper_parser_allocate(void)
15811{
15812 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15813}
15814#endif /* RIPPER */
15815
15816#ifndef RIPPER
15817void
15818rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15819{
15820 va_list ap;
15821 VALUE mesg = p->debug_buffer;
15822
15823 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15824 va_start(ap, fmt);
15825 rb_str_vcatf(mesg, fmt, ap);
15826 va_end(ap);
15827 if (char_at_end(p, mesg, 0) == '\n') {
15828 rb_io_write(p->debug_output, mesg);
15829 p->debug_buffer = Qnil;
15830 }
15831}
15832
15833static void
15834parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15835{
15836 va_list ap;
15837 int lineno, column;
15838
15839 if (loc) {
15840 lineno = loc->end_pos.lineno;
15841 column = loc->end_pos.column;
15842 }
15843 else {
15844 lineno = p->ruby_sourceline;
15845 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15846 }
15847
15848 rb_io_flush(p->debug_output);
15849 p->error_p = 1;
15850 va_start(ap, fmt);
15851 p->error_buffer =
15852 rb_syntax_error_append(p->error_buffer,
15853 p->ruby_sourcefile_string,
15854 lineno, column,
15855 p->enc, fmt, ap);
15856 va_end(ap);
15857}
15858
15859static size_t
15860count_char(const char *str, int c)
15861{
15862 int n = 0;
15863 while (str[n] == c) ++n;
15864 return n;
15865}
15866
15867/*
15868 * strip enclosing double-quotes, same as the default yytnamerr except
15869 * for that single-quotes matching back-quotes do not stop stripping.
15870 *
15871 * "\"`class' keyword\"" => "`class' keyword"
15872 */
15873size_t
15874rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
15875{
15876 if (*yystr == '"') {
15877 size_t yyn = 0, bquote = 0;
15878 const char *yyp = yystr;
15879
15880 while (*++yyp) {
15881 switch (*yyp) {
15882 case '\'':
15883 if (!bquote) {
15884 bquote = count_char(yyp+1, '\'') + 1;
15885 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
15886 yyn += bquote;
15887 yyp += bquote - 1;
15888 break;
15889 }
15890 else {
15891 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
15892 if (yyres) memcpy(yyres + yyn, yyp, bquote);
15893 yyn += bquote;
15894 yyp += bquote - 1;
15895 bquote = 0;
15896 break;
15897 }
15898 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
15899 if (yyres) memcpy(yyres + yyn, yyp, 3);
15900 yyn += 3;
15901 yyp += 2;
15902 break;
15903 }
15904 goto do_not_strip_quotes;
15905 }
15906
15907 case ',':
15908 goto do_not_strip_quotes;
15909
15910 case '\\':
15911 if (*++yyp != '\\')
15912 goto do_not_strip_quotes;
15913 /* Fall through. */
15914 default:
15915 if (yyres)
15916 yyres[yyn] = *yyp;
15917 yyn++;
15918 break;
15919
15920 case '"':
15921 case '\0':
15922 if (yyres)
15923 yyres[yyn] = '\0';
15924 return yyn;
15925 }
15926 }
15927 do_not_strip_quotes: ;
15928 }
15929
15930 if (!yyres) return strlen(yystr);
15931
15932 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
15933}
15934#endif
15935
15936#ifdef RIPPER
15937#define validate(x) (void)(x)
15938
15939static VALUE
15940ripper_dispatch0(struct parser_params *p, ID mid)
15941{
15942 return rb_funcall(p->value, mid, 0);
15943}
15944
15945static VALUE
15946ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
15947{
15948 validate(a);
15949 return rb_funcall(p->value, mid, 1, a);
15950}
15951
15952static VALUE
15953ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
15954{
15955 validate(a);
15956 validate(b);
15957 return rb_funcall(p->value, mid, 2, a, b);
15958}
15959
15960static VALUE
15961ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
15962{
15963 validate(a);
15964 validate(b);
15965 validate(c);
15966 return rb_funcall(p->value, mid, 3, a, b, c);
15967}
15968
15969static VALUE
15970ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
15971{
15972 validate(a);
15973 validate(b);
15974 validate(c);
15975 validate(d);
15976 return rb_funcall(p->value, mid, 4, a, b, c, d);
15977}
15978
15979static VALUE
15980ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
15981{
15982 validate(a);
15983 validate(b);
15984 validate(c);
15985 validate(d);
15986 validate(e);
15987 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
15988}
15989
15990static VALUE
15991ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
15992{
15993 validate(a);
15994 validate(b);
15995 validate(c);
15996 validate(d);
15997 validate(e);
15998 validate(f);
15999 validate(g);
16000 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16001}
16002
16003void
16004ripper_error(struct parser_params *p)
16005{
16006 p->error_p = TRUE;
16007}
16008
16009VALUE
16010ripper_value(struct parser_params *p)
16011{
16012 (void)yystpcpy; /* may not used in newer bison */
16013
16014 return p->value;
16015}
16016
16017#endif /* RIPPER */
16018/*
16019 * Local variables:
16020 * mode: c
16021 * c-file-style: "ruby"
16022 * End:
16023 */