Ruby 4.1.0dev (2026-11-19 revision 1e71e987c206d5d77bf37e883aaa6262747b2458)
parse.y (1e71e987c206d5d77bf37e883aaa6262747b2458)
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#include "internal/thread.h"
28
29#include <errno.h>
30
31#ifdef UNIVERSAL_PARSER
32
33#include "internal/ruby_parser.h"
34#include "parser_node.h"
35#include "universal_parser.c"
36
37#ifdef RIPPER
38#define STATIC_ID2SYM p->config->static_id2sym
39#endif
40
41#else
42
43#include "internal.h"
44#include "internal/array.h"
45#include "internal/compile.h"
46#include "internal/compilers.h"
47#include "internal/complex.h"
48#include "internal/encoding.h"
49#include "internal/error.h"
50#include "internal/gc.h"
51#include "internal/hash.h"
52#include "internal/io.h"
53#include "internal/numeric.h"
54#include "internal/parse.h"
55#include "internal/rational.h"
56#include "internal/re.h"
57#include "internal/ruby_parser.h"
58#include "internal/symbol.h"
59#include "internal/thread.h"
60#include "node.h"
61#include "parser_node.h"
62#include "probes.h"
63#include "regenc.h"
64#include "ruby/encoding.h"
65#include "ruby/regex.h"
66#include "ruby/ruby.h"
67#include "ruby/st.h"
68#include "ruby/util.h"
69#include "ruby/ractor.h"
70#include "symbol.h"
71
72#ifndef RIPPER
73static VALUE
74syntax_error_new(void)
75{
76 return rb_class_new_instance(0, 0, rb_eSyntaxError);
77}
78#endif
79
80static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable);
81
82#define compile_callback rb_suppress_tracing
83#endif /* !UNIVERSAL_PARSER */
84
85#define NODE_SPECIAL_EMPTY_ARGS ((NODE *)-1)
86#define NODE_EMPTY_ARGS_P(node) ((node) == NODE_SPECIAL_EMPTY_ARGS)
87
88static int rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2);
89
90#ifndef RIPPER
91static rb_parser_string_t *rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *original);
92#endif
93
94static int
95node_integer_cmp(rb_node_integer_t *n1, rb_node_integer_t *n2)
96{
97 return (n1->minus != n2->minus ||
98 n1->base != n2->base ||
99 strcmp(n1->val, n2->val));
100}
101
102static int
103node_float_cmp(rb_node_float_t *n1, rb_node_float_t *n2)
104{
105 return (n1->minus != n2->minus ||
106 strcmp(n1->val, n2->val));
107}
108
109static int
110node_rational_cmp(rb_node_rational_t *n1, rb_node_rational_t *n2)
111{
112 return (n1->minus != n2->minus ||
113 n1->base != n2->base ||
114 n1->seen_point != n2->seen_point ||
115 strcmp(n1->val, n2->val));
116}
117
118static int
119node_imaginary_cmp(rb_node_imaginary_t *n1, rb_node_imaginary_t *n2)
120{
121 return (n1->minus != n2->minus ||
122 n1->base != n2->base ||
123 n1->seen_point != n2->seen_point ||
124 n1->type != n2->type ||
125 strcmp(n1->val, n2->val));
126}
127
128static int
129rb_parser_regx_hash_cmp(rb_node_regx_t *n1, rb_node_regx_t *n2)
130{
131 return (n1->options != n2->options ||
132 rb_parser_string_hash_cmp(n1->string, n2->string));
133}
134
135static st_index_t rb_parser_str_hash(rb_parser_string_t *str);
136static st_index_t rb_char_p_hash(const char *c);
137
138static int
139literal_cmp(st_data_t val, st_data_t lit)
140{
141 if (val == lit) return 0;
142
143 NODE *node_val = RNODE(val);
144 NODE *node_lit = RNODE(lit);
145 enum node_type type_val = nd_type(node_val);
146 enum node_type type_lit = nd_type(node_lit);
147
148 if (type_val != type_lit) {
149 return -1;
150 }
151
152 switch (type_lit) {
153 case NODE_INTEGER:
154 return node_integer_cmp(RNODE_INTEGER(node_val), RNODE_INTEGER(node_lit));
155 case NODE_FLOAT:
156 return node_float_cmp(RNODE_FLOAT(node_val), RNODE_FLOAT(node_lit));
157 case NODE_RATIONAL:
158 return node_rational_cmp(RNODE_RATIONAL(node_val), RNODE_RATIONAL(node_lit));
159 case NODE_IMAGINARY:
160 return node_imaginary_cmp(RNODE_IMAGINARY(node_val), RNODE_IMAGINARY(node_lit));
161 case NODE_STR:
162 return rb_parser_string_hash_cmp(RNODE_STR(node_val)->string, RNODE_STR(node_lit)->string);
163 case NODE_SYM:
164 return rb_parser_string_hash_cmp(RNODE_SYM(node_val)->string, RNODE_SYM(node_lit)->string);
165 case NODE_REGX:
166 return rb_parser_regx_hash_cmp(RNODE_REGX(node_val), RNODE_REGX(node_lit));
167 case NODE_LINE:
168 return node_val->nd_loc.beg_pos.lineno != node_lit->nd_loc.beg_pos.lineno;
169 case NODE_FILE:
170 return rb_parser_string_hash_cmp(RNODE_FILE(node_val)->path, RNODE_FILE(node_lit)->path);
171 case NODE_ENCODING:
172 return RNODE_ENCODING(node_val)->enc != RNODE_ENCODING(node_lit)->enc;
173 default:
174#ifdef UNIVERSAL_PARSER
175 abort();
176#else
177 rb_bug("unexpected node: %s, %s", ruby_node_name(type_val), ruby_node_name(type_lit));
178#endif
179 }
180}
181
182static st_index_t
183literal_hash(st_data_t a)
184{
185 NODE *node = (NODE *)a;
186 enum node_type type = nd_type(node);
187
188 switch (type) {
189 case NODE_INTEGER:
190 return rb_char_p_hash(RNODE_INTEGER(node)->val);
191 case NODE_FLOAT:
192 return rb_char_p_hash(RNODE_FLOAT(node)->val);
193 case NODE_RATIONAL:
194 return rb_char_p_hash(RNODE_RATIONAL(node)->val);
195 case NODE_IMAGINARY:
196 return rb_char_p_hash(RNODE_IMAGINARY(node)->val);
197 case NODE_STR:
198 return rb_parser_str_hash(RNODE_STR(node)->string);
199 case NODE_SYM:
200 return rb_parser_str_hash(RNODE_SYM(node)->string);
201 case NODE_REGX:
202 return rb_parser_str_hash(RNODE_REGX(node)->string);
203 case NODE_LINE:
204 return (st_index_t)node->nd_loc.beg_pos.lineno;
205 case NODE_FILE:
206 return rb_parser_str_hash(RNODE_FILE(node)->path);
207 case NODE_ENCODING:
208 return (st_index_t)RNODE_ENCODING(node)->enc;
209 default:
210#ifdef UNIVERSAL_PARSER
211 abort();
212#else
213 rb_bug("unexpected node: %s", ruby_node_name(type));
214#endif
215 }
216}
217
218static inline int
219parse_isascii(int c)
220{
221 return '\0' <= c && c <= '\x7f';
222}
223
224#undef ISASCII
225#define ISASCII parse_isascii
226
227static inline int
228parse_isspace(int c)
229{
230 return c == ' ' || ('\t' <= c && c <= '\r');
231}
232
233#undef ISSPACE
234#define ISSPACE parse_isspace
235
236static inline int
237parse_iscntrl(int c)
238{
239 return ('\0' <= c && c < ' ') || c == '\x7f';
240}
241
242#undef ISCNTRL
243#define ISCNTRL(c) parse_iscntrl(c)
244
245static inline int
246parse_isupper(int c)
247{
248 return 'A' <= c && c <= 'Z';
249}
250
251static inline int
252parse_islower(int c)
253{
254 return 'a' <= c && c <= 'z';
255}
256
257static inline int
258parse_isalpha(int c)
259{
260 return parse_isupper(c) || parse_islower(c);
261}
262
263#undef ISALPHA
264#define ISALPHA(c) parse_isalpha(c)
265
266static inline int
267parse_isdigit(int c)
268{
269 return '0' <= c && c <= '9';
270}
271
272#undef ISDIGIT
273#define ISDIGIT(c) parse_isdigit(c)
274
275static inline int
276parse_isalnum(int c)
277{
278 return ISALPHA(c) || ISDIGIT(c);
279}
280
281#undef ISALNUM
282#define ISALNUM(c) parse_isalnum(c)
283
284static inline int
285parse_isxdigit(int c)
286{
287 return ISDIGIT(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
288}
289
290#undef ISXDIGIT
291#define ISXDIGIT(c) parse_isxdigit(c)
292
293#include "parser_st.h"
294
295#undef STRCASECMP
296#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
297
298#undef STRNCASECMP
299#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
300
301#ifdef RIPPER
302#include "ripper_init.h"
303#endif
304
305enum rescue_context {
306 before_rescue,
307 after_rescue,
308 after_else,
309 after_ensure,
310};
311
312struct lex_context {
313 unsigned int in_defined: 1;
314 unsigned int in_kwarg: 1;
315 unsigned int in_argdef: 1;
316 unsigned int in_def: 1;
317 unsigned int in_class: 1;
318 unsigned int has_trailing_semicolon: 1;
319 BITFIELD(enum rb_parser_shareability, shareable_constant_value, 2);
320 BITFIELD(enum rescue_context, in_rescue, 2);
321 unsigned int cant_return: 1;
322 unsigned int in_alt_pattern: 1;
323 unsigned int capture_in_pattern: 1;
324};
325
326typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
327
328#if defined(__GNUC__) && !defined(__clang__)
329// Suppress "parameter passing for argument of type 'struct
330// lex_context' changed" notes. `struct lex_context` is file scope,
331// and has no ABI compatibility issue.
332RBIMPL_WARNING_PUSH()
333RBIMPL_WARNING_IGNORED(-Wpsabi)
334RBIMPL_WARNING_POP()
335// Not sure why effective even after popped.
336#endif
337
338#include "parse.h"
339
340#define NO_LEX_CTXT (struct lex_context){0}
341
342#ifndef WARN_PAST_SCOPE
343# define WARN_PAST_SCOPE 0
344#endif
345
346#define TAB_WIDTH 8
347
348#define yydebug (p->debug) /* disable the global variable definition */
349
350#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__)
351#define YY_LOCATION_PRINT(File, loc, p) \
352 rb_parser_printf(p, "%d.%d-%d.%d", \
353 (loc).beg_pos.lineno, (loc).beg_pos.column,\
354 (loc).end_pos.lineno, (loc).end_pos.column)
355#define YYLLOC_DEFAULT(Current, Rhs, N) \
356 do \
357 if (N) \
358 { \
359 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
360 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
361 } \
362 else \
363 { \
364 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
365 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
366 } \
367 while (0)
368#define YY_(Msgid) \
369 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
370 "nesting too deep" : (Msgid))
371
372#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
373 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
374#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
375 rb_parser_set_location_of_delayed_token(p, &(Current))
376#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
377 rb_parser_set_location_of_heredoc_end(p, &(Current))
378#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
379 rb_parser_set_location_of_dummy_end(p, &(Current))
380#define RUBY_SET_YYLLOC_OF_NONE(Current) \
381 rb_parser_set_location_of_none(p, &(Current))
382#define RUBY_SET_YYLLOC(Current) \
383 rb_parser_set_location(p, &(Current))
384#define RUBY_INIT_YYLLOC() \
385 { \
386 {p->ruby_sourceline, parser_column(p->lex.ptok - p->lex.pbeg)}, \
387 {p->ruby_sourceline, parser_column(p->lex.pcur - p->lex.pbeg)}, \
388 }
389
390/* Saturate instead of wrapping to a column before the line. */
391static inline int
392parser_column(rb_len_t pos)
393{
394#if SIZEOF_INT < SIZEOF_RB_LEN_T
395 if (pos > INT_MAX) return INT_MAX;
396#endif
397 return (int)pos;
398}
399
400#define IS_lex_state_for(x, ls) ((x) & (ls))
401#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
402#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
403#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
404
405# define SET_LEX_STATE(ls) \
406 parser_set_lex_state(p, ls, __LINE__)
407static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
408
409typedef VALUE stack_type;
410
411static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
412
413# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
414# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
415# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
416# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
417# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
418
419/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
420 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
421#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
422#define COND_POP() BITSTACK_POP(cond_stack)
423#define COND_P() BITSTACK_SET_P(cond_stack)
424#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
425
426/* A flag to identify keyword_do_block; "do" keyword after command_call.
427 Example: `foo 1, 2 do`. */
428#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
429#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
430#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
431#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
432
433struct vtable {
434 ID *tbl;
435 int pos;
436 int capa;
437 struct vtable *prev;
438};
439
440struct local_vars {
441 struct vtable *args;
442 struct vtable *vars;
443 struct vtable *used;
444# if WARN_PAST_SCOPE
445 struct vtable *past;
446# endif
447 struct local_vars *prev;
448 struct {
449 NODE *outer, *inner, *current;
450 } numparam;
451 NODE *it;
452};
453
454typedef struct rb_locations_lambda_body_t {
455 NODE *node;
456 YYLTYPE opening_loc;
457 YYLTYPE closing_loc;
458} rb_locations_lambda_body_t;
459
460enum {
461 ORDINAL_PARAM = -1,
462 NO_PARAM = 0,
463 NUMPARAM_MAX = 9,
464};
465
466#define DVARS_INHERIT ((void*)1)
467#define DVARS_TOPSCOPE NULL
468#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
469
470typedef struct token_info {
471 const char *token;
472 rb_code_position_t beg;
473 int indent;
474 int nonspc;
475 struct token_info *next;
476} token_info;
477
478typedef struct end_expect_token_locations {
479 const rb_code_position_t *pos;
480 struct end_expect_token_locations *prev;
481} end_expect_token_locations_t;
482
483typedef struct parser_string_buffer_elem {
484 struct parser_string_buffer_elem *next;
485 long len; /* Total length of allocated buf */
486 long used; /* Current usage of buf */
487 rb_parser_string_t *buf[FLEX_ARY_LEN];
488} parser_string_buffer_elem_t;
489
490typedef struct parser_string_buffer {
491 parser_string_buffer_elem_t *head;
492 parser_string_buffer_elem_t *last;
493} parser_string_buffer_t;
494
495#define AFTER_HEREDOC_WITHOUT_TERMINATOR ((rb_parser_string_t *)1)
496
497/*
498 Structure of Lexer Buffer:
499
500 lex.pbeg lex.ptok lex.pcur lex.pend
501 | | | |
502 |------------+------------+------------|
503 |<---------->|
504 token
505*/
506struct parser_params {
507 YYSTYPE *lval;
508 YYLTYPE *yylloc;
509
510 struct {
511 rb_strterm_t *strterm;
512 rb_parser_lex_gets_func *gets;
513 rb_parser_input_data input;
514 parser_string_buffer_t string_buffer;
515 rb_parser_string_t *lastline;
516 rb_parser_string_t *nextline;
517 const char *pbeg;
518 const char *pcur;
519 const char *pend;
520 const char *ptok;
521 enum lex_state_e state;
522 /* track the nest level of any parens "()[]{}" */
523 int paren_nest;
524 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
525 int lpar_beg;
526 /* track the nest level of only braces "{}" */
527 int brace_nest;
528 } lex;
529 /* Cache the tail of the and/or chain most recently built by logop(), so a
530 * left-associative chain does not rescan its right branch on every
531 * operator. head is the chain's top node, tail the node whose nd_2nd is the
532 * next insertion point. */
533 struct {
534 NODE *head;
535 NODE *tail;
536 } logop;
537 stack_type cond_stack;
538 stack_type cmdarg_stack;
539 int tokidx;
540 int toksiz;
541 int heredoc_end;
542 int heredoc_indent;
543 int heredoc_line_indent;
544 char *tokenbuf;
545 struct local_vars *lvtbl;
546 st_table *pvtbl;
547 st_table *pktbl;
548 int line_count;
549 int ruby_sourceline; /* current line no. */
550 const char *ruby_sourcefile; /* current source file */
551 VALUE ruby_sourcefile_string;
552 rb_encoding *enc;
553 token_info *token_info;
554 st_table *case_labels;
555 rb_node_exits_t *exits;
556
557 VALUE debug_buffer;
558 VALUE debug_output;
559
560 struct {
561 rb_parser_string_t *token;
562 int beg_line;
563 int beg_col;
564 int end_line;
565 int end_col;
566 } delayed;
567
568 rb_ast_t *ast;
569 int node_id;
570
571 st_table *warn_duplicate_keys_table;
572
573 int max_numparam;
574 ID it_id;
575
576 struct lex_context ctxt;
577
578 NODE *eval_tree_begin;
579 NODE *eval_tree;
580 const struct rb_iseq_struct *parent_iseq;
581
582#ifdef UNIVERSAL_PARSER
583 const rb_parser_config_t *config;
584#endif
585 /* compile_option */
586 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
587
588 unsigned int command_start:1;
589 unsigned int eofp: 1;
590 unsigned int ruby__end__seen: 1;
591 unsigned int debug: 1;
592 unsigned int has_shebang: 1;
593 unsigned int token_seen: 1;
594 unsigned int token_info_enabled: 1;
595# if WARN_PAST_SCOPE
596 unsigned int past_scope_enabled: 1;
597# endif
598 unsigned int error_p: 1;
599 unsigned int cr_seen: 1;
600
601 /* Streaming hash state of the source bytes read so far. */
602 rb_source_hash_state_t source_hash;
603
604#ifndef RIPPER
605 /* Ruby core only */
606
607 unsigned int do_print: 1;
608 unsigned int do_loop: 1;
609 unsigned int do_chomp: 1;
610 unsigned int do_split: 1;
611 unsigned int error_tolerant: 1;
612 unsigned int keep_tokens: 1;
613
614 VALUE error_buffer;
615 rb_parser_ary_t *debug_lines;
616 /*
617 * Store specific keyword locations to generate dummy end token.
618 * Refer to the tail of list element.
619 */
620 end_expect_token_locations_t *end_expect_token_locations;
621 /* id for terms */
622 int token_id;
623 /* Array for term tokens */
624 rb_parser_ary_t *tokens;
625#else
626 /* Ripper only */
627
628 VALUE value;
629 VALUE result;
630 VALUE parsing_thread;
631 VALUE s_value; /* Token VALUE */
632 VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
633 VALUE s_value_stack;
634#endif
635};
636
637#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
638#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
639#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
640static int
641numparam_id_p(struct parser_params *p, ID id)
642{
643 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
644 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
645 return idx > 0 && idx <= NUMPARAM_MAX;
646}
647static void numparam_name(struct parser_params *p, ID id);
648
649#ifdef RIPPER
650static void
651after_shift(struct parser_params *p)
652{
653 if (p->debug) {
654 rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
655 }
656 rb_ary_push(p->s_value_stack, p->s_value);
657 p->s_value = Qnil;
658}
659
660static void
661before_reduce(int len, struct parser_params *p)
662{
663 // Initialize $$ with $1.
664 if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
665}
666
667static void
668after_reduce(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-reduce pop: %+"PRIsVALUE"\n", tos);
674 }
675 }
676 if (p->debug) {
677 rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
678 }
679 rb_ary_push(p->s_value_stack, p->s_lvalue);
680 p->s_lvalue = Qnil;
681}
682
683static void
684after_shift_error_token(struct parser_params *p)
685{
686 if (p->debug) {
687 rb_parser_printf(p, "after-shift-error-token:\n");
688 }
689 rb_ary_push(p->s_value_stack, Qnil);
690}
691
692static void
693after_pop_stack(int len, struct parser_params *p)
694{
695 for (int i = 0; i < len; i++) {
696 VALUE tos = rb_ary_pop(p->s_value_stack);
697 if (p->debug) {
698 rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
699 }
700 }
701}
702#else
703static void
704after_shift(struct parser_params *p)
705{
706}
707
708static void
709before_reduce(int len, struct parser_params *p)
710{
711}
712
713static void
714after_reduce(int len, struct parser_params *p)
715{
716}
717
718static void
719after_shift_error_token(struct parser_params *p)
720{
721}
722
723static void
724after_pop_stack(int len, struct parser_params *p)
725{
726}
727#endif
728
729#define intern_cstr(n,l,en) rb_intern3(n,l,en)
730
731#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
732
733#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
734#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
735#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
736#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
737#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
738#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
739
740#ifndef RIPPER
741static inline int
742char_at_end(struct parser_params *p, VALUE str, int when_empty)
743{
744 rb_len_t len = RSTRING_LEN(str);
745 return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
746}
747#endif
748
749static void
750pop_pvtbl(struct parser_params *p, st_table *tbl)
751{
752 st_free_table(p->pvtbl);
753 p->pvtbl = tbl;
754}
755
756static void
757pop_pktbl(struct parser_params *p, st_table *tbl)
758{
759 if (p->pktbl) st_free_table(p->pktbl);
760 p->pktbl = tbl;
761}
762
763#define STRING_BUF_DEFAULT_LEN 16
764
765static void
766string_buffer_init(struct parser_params *p)
767{
768 parser_string_buffer_t *buf = &p->lex.string_buffer;
769 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN;
770
771 buf->head = buf->last = xmalloc(size);
772 buf->head->len = STRING_BUF_DEFAULT_LEN;
773 buf->head->used = 0;
774 buf->head->next = NULL;
775}
776
777static void
778string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
779{
780 parser_string_buffer_t *buf = &p->lex.string_buffer;
781
782 if (buf->head->used >= buf->head->len) {
783 parser_string_buffer_elem_t *elem;
784 long n = buf->head->len * 2;
785 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n;
786
787 elem = xmalloc(size);
788 elem->len = n;
789 elem->used = 0;
790 elem->next = NULL;
791 buf->last->next = elem;
792 buf->last = elem;
793 }
794 buf->last->buf[buf->last->used++] = str;
795}
796
797static void
798string_buffer_free(struct parser_params *p)
799{
800 parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
801
802 while (elem) {
803 parser_string_buffer_elem_t *next_elem = elem->next;
804
805 for (long i = 0; i < elem->used; i++) {
806 rb_parser_string_free(p, elem->buf[i]);
807 }
808
809 xfree(elem);
810 elem = next_elem;
811 }
812}
813
814#ifndef RIPPER
815static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
816
817static void
818debug_end_expect_token_locations(struct parser_params *p, const char *name)
819{
820 if(p->debug) {
821 VALUE mesg = rb_sprintf("%s: [", name);
822 int i = 0;
823 for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
824 if (i > 0)
825 rb_str_cat_cstr(mesg, ", ");
826 rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
827 i++;
828 }
829 rb_str_cat_cstr(mesg, "]\n");
830 flush_debug_buffer(p, p->debug_output, mesg);
831 }
832}
833
834static void
835push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
836{
837 if(!p->error_tolerant) return;
838
839 end_expect_token_locations_t *locations;
840 locations = ALLOC(end_expect_token_locations_t);
841 locations->pos = pos;
842 locations->prev = p->end_expect_token_locations;
843 p->end_expect_token_locations = locations;
844
845 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
846}
847
848static void
849pop_end_expect_token_locations(struct parser_params *p)
850{
851 if(!p->end_expect_token_locations) return;
852
853 end_expect_token_locations_t *locations = p->end_expect_token_locations->prev;
854 ruby_xfree_sized(p->end_expect_token_locations, sizeof(end_expect_token_locations_t));
855 p->end_expect_token_locations = locations;
856
857 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
858}
859
860static end_expect_token_locations_t *
861peek_end_expect_token_locations(struct parser_params *p)
862{
863 return p->end_expect_token_locations;
864}
865
866static const char *
867parser_token2char(struct parser_params *p, enum yytokentype tok)
868{
869 switch ((int) tok) {
870#define TOKEN2CHAR(tok) case tok: return (#tok);
871#define TOKEN2CHAR2(tok, name) case tok: return (name);
872 TOKEN2CHAR2(' ', "word_sep");
873 TOKEN2CHAR2('!', "!")
874 TOKEN2CHAR2('%', "%");
875 TOKEN2CHAR2('&', "&");
876 TOKEN2CHAR2('*', "*");
877 TOKEN2CHAR2('+', "+");
878 TOKEN2CHAR2('-', "-");
879 TOKEN2CHAR2('/', "/");
880 TOKEN2CHAR2('<', "<");
881 TOKEN2CHAR2('=', "=");
882 TOKEN2CHAR2('>', ">");
883 TOKEN2CHAR2('?', "?");
884 TOKEN2CHAR2('^', "^");
885 TOKEN2CHAR2('|', "|");
886 TOKEN2CHAR2('~', "~");
887 TOKEN2CHAR2(':', ":");
888 TOKEN2CHAR2(',', ",");
889 TOKEN2CHAR2('.', ".");
890 TOKEN2CHAR2(';', ";");
891 TOKEN2CHAR2('`', "`");
892 TOKEN2CHAR2('\n', "nl");
893 TOKEN2CHAR2('{', "\"{\"");
894 TOKEN2CHAR2('}', "\"}\"");
895 TOKEN2CHAR2('[', "\"[\"");
896 TOKEN2CHAR2(']', "\"]\"");
897 TOKEN2CHAR2('(', "\"(\"");
898 TOKEN2CHAR2(')', "\")\"");
899 TOKEN2CHAR2('\\', "backslash");
900 TOKEN2CHAR(keyword_class);
901 TOKEN2CHAR(keyword_module);
902 TOKEN2CHAR(keyword_def);
903 TOKEN2CHAR(keyword_undef);
904 TOKEN2CHAR(keyword_begin);
905 TOKEN2CHAR(keyword_rescue);
906 TOKEN2CHAR(keyword_ensure);
907 TOKEN2CHAR(keyword_end);
908 TOKEN2CHAR(keyword_if);
909 TOKEN2CHAR(keyword_unless);
910 TOKEN2CHAR(keyword_then);
911 TOKEN2CHAR(keyword_elsif);
912 TOKEN2CHAR(keyword_else);
913 TOKEN2CHAR(keyword_case);
914 TOKEN2CHAR(keyword_when);
915 TOKEN2CHAR(keyword_while);
916 TOKEN2CHAR(keyword_until);
917 TOKEN2CHAR(keyword_for);
918 TOKEN2CHAR(keyword_break);
919 TOKEN2CHAR(keyword_next);
920 TOKEN2CHAR(keyword_redo);
921 TOKEN2CHAR(keyword_retry);
922 TOKEN2CHAR(keyword_in);
923 TOKEN2CHAR(keyword_do);
924 TOKEN2CHAR(keyword_do_cond);
925 TOKEN2CHAR(keyword_do_block);
926 TOKEN2CHAR(keyword_do_LAMBDA);
927 TOKEN2CHAR(keyword_return);
928 TOKEN2CHAR(keyword_yield);
929 TOKEN2CHAR(keyword_super);
930 TOKEN2CHAR(keyword_self);
931 TOKEN2CHAR(keyword_nil);
932 TOKEN2CHAR(keyword_true);
933 TOKEN2CHAR(keyword_false);
934 TOKEN2CHAR(keyword_and);
935 TOKEN2CHAR(keyword_or);
936 TOKEN2CHAR(keyword_not);
937 TOKEN2CHAR(modifier_if);
938 TOKEN2CHAR(modifier_unless);
939 TOKEN2CHAR(modifier_while);
940 TOKEN2CHAR(modifier_until);
941 TOKEN2CHAR(modifier_rescue);
942 TOKEN2CHAR(keyword_alias);
943 TOKEN2CHAR(keyword_defined);
944 TOKEN2CHAR(keyword_BEGIN);
945 TOKEN2CHAR(keyword_END);
946 TOKEN2CHAR(keyword__LINE__);
947 TOKEN2CHAR(keyword__FILE__);
948 TOKEN2CHAR(keyword__ENCODING__);
949 TOKEN2CHAR(tIDENTIFIER);
950 TOKEN2CHAR(tFID);
951 TOKEN2CHAR(tGVAR);
952 TOKEN2CHAR(tIVAR);
953 TOKEN2CHAR(tCONSTANT);
954 TOKEN2CHAR(tCVAR);
955 TOKEN2CHAR(tLABEL);
956 TOKEN2CHAR(tINTEGER);
957 TOKEN2CHAR(tFLOAT);
958 TOKEN2CHAR(tRATIONAL);
959 TOKEN2CHAR(tIMAGINARY);
960 TOKEN2CHAR(tCHAR);
961 TOKEN2CHAR(tNTH_REF);
962 TOKEN2CHAR(tBACK_REF);
963 TOKEN2CHAR(tSTRING_CONTENT);
964 TOKEN2CHAR(tREGEXP_END);
965 TOKEN2CHAR(tDUMNY_END);
966 TOKEN2CHAR(tSP);
967 TOKEN2CHAR(tUPLUS);
968 TOKEN2CHAR(tUMINUS);
969 TOKEN2CHAR(tPOW);
970 TOKEN2CHAR(tCMP);
971 TOKEN2CHAR(tEQ);
972 TOKEN2CHAR(tEQQ);
973 TOKEN2CHAR(tNEQ);
974 TOKEN2CHAR(tGEQ);
975 TOKEN2CHAR(tLEQ);
976 TOKEN2CHAR(tANDOP);
977 TOKEN2CHAR(tOROP);
978 TOKEN2CHAR(tMATCH);
979 TOKEN2CHAR(tNMATCH);
980 TOKEN2CHAR(tDOT2);
981 TOKEN2CHAR(tDOT3);
982 TOKEN2CHAR(tBDOT2);
983 TOKEN2CHAR(tBDOT3);
984 TOKEN2CHAR(tAREF);
985 TOKEN2CHAR(tASET);
986 TOKEN2CHAR(tLSHFT);
987 TOKEN2CHAR(tRSHFT);
988 TOKEN2CHAR(tANDDOT);
989 TOKEN2CHAR(tCOLON2);
990 TOKEN2CHAR(tCOLON3);
991 TOKEN2CHAR(tOP_ASGN);
992 TOKEN2CHAR(tASSOC);
993 TOKEN2CHAR(tLPAREN);
994 TOKEN2CHAR(tLPAREN_ARG);
995 TOKEN2CHAR(tLBRACK);
996 TOKEN2CHAR(tLBRACE);
997 TOKEN2CHAR(tLBRACE_ARG);
998 TOKEN2CHAR(tSTAR);
999 TOKEN2CHAR(tDSTAR);
1000 TOKEN2CHAR(tAMPER);
1001 TOKEN2CHAR(tLAMBDA);
1002 TOKEN2CHAR(tSYMBEG);
1003 TOKEN2CHAR(tSTRING_BEG);
1004 TOKEN2CHAR(tXSTRING_BEG);
1005 TOKEN2CHAR(tREGEXP_BEG);
1006 TOKEN2CHAR(tWORDS_BEG);
1007 TOKEN2CHAR(tQWORDS_BEG);
1008 TOKEN2CHAR(tSYMBOLS_BEG);
1009 TOKEN2CHAR(tQSYMBOLS_BEG);
1010 TOKEN2CHAR(tSTRING_END);
1011 TOKEN2CHAR(tSTRING_DEND);
1012 TOKEN2CHAR(tSTRING_DBEG);
1013 TOKEN2CHAR(tSTRING_DVAR);
1014 TOKEN2CHAR(tLAMBEG);
1015 TOKEN2CHAR(tLABEL_END);
1016 TOKEN2CHAR(tIGNORED_NL);
1017 TOKEN2CHAR(tCOMMENT);
1018 TOKEN2CHAR(tEMBDOC_BEG);
1019 TOKEN2CHAR(tEMBDOC);
1020 TOKEN2CHAR(tEMBDOC_END);
1021 TOKEN2CHAR(tHEREDOC_BEG);
1022 TOKEN2CHAR(tHEREDOC_END);
1023 TOKEN2CHAR(k__END__);
1024 TOKEN2CHAR(tLOWEST);
1025 TOKEN2CHAR(tUMINUS_NUM);
1026 TOKEN2CHAR(tLAST_TOKEN);
1027#undef TOKEN2CHAR
1028#undef TOKEN2CHAR2
1029 }
1030
1031 rb_bug("parser_token2id: unknown token %d", tok);
1032
1033 UNREACHABLE_RETURN(0);
1034}
1035#else
1036static void
1037push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
1038{
1039}
1040
1041static void
1042pop_end_expect_token_locations(struct parser_params *p)
1043{
1044}
1045#endif
1046
1047RBIMPL_ATTR_NONNULL((1, 2, 3))
1048static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
1049RBIMPL_ATTR_NONNULL((1, 2))
1050static int parser_yyerror0(struct parser_params*, const char*);
1051#define yyerror0(msg) parser_yyerror0(p, (msg))
1052#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
1053#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
1054#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
1055#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
1056#define lex_eol_p(p) lex_eol_n_p(p, 0)
1057#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
1058#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
1059#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
1060
1061static enum yytokentype parser_variable_name_p(const struct parser_params *p);
1062
1063static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
1064static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
1065static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
1066static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
1067static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
1068
1069#ifdef RIPPER
1070#define compile_for_eval (0)
1071#else
1072#define compile_for_eval (p->parent_iseq != 0)
1073#endif
1074
1075#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
1076
1077#define CALL_Q_P(q) ((q) == tANDDOT)
1078#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
1079
1080#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
1081
1082static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
1083
1084static inline void
1085rb_discard_node(struct parser_params *p, NODE *n)
1086{
1087 rb_ast_delete_node(p->ast, n);
1088}
1089
1090static rb_node_scope_t *rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc);
1091static 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, NODE *nd_parent, const YYLTYPE *loc);
1092static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1093static 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);
1094static 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);
1095static 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);
1096static 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);
1097static 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);
1098static 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);
1099static rb_node_in_t *rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *operator_loc);
1100static 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);
1101static 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);
1102static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1103static 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);
1104static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
1105static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
1106static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1107static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
1108static 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);
1109static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
1110static 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);
1111static 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);
1112static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
1113static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1114static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1115static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1116static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1117static 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);
1118static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1119static 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);
1120static 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);
1121static 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);
1122static 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);
1123static 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);
1124static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1125static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1126static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1127static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1128static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1129static 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);
1130static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
1131static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1132static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1133static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
1134static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1135static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1136static 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);
1137static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1138static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1139static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1140static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1141static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1142static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1143static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1144static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1145static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1146static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1147static rb_node_integer_t * rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc);
1148static rb_node_float_t * rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc);
1149static rb_node_rational_t * rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc);
1150static 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);
1151static rb_node_str_t *rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1152static 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);
1153static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1154static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1155static 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);
1156static 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);
1157static 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);
1158static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1159static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
1160static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc);
1161static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1162static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1163static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
1164static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1165static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1166static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1167static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1168static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1169static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1170static 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);
1171static 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);
1172static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
1173static 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);
1174static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *module_keyword_loc, const YYLTYPE *end_keyword_loc);
1175static rb_node_sclass_t *rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *operator_loc, const YYLTYPE *end_keyword_loc);
1176static rb_node_colon2_t *rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc);
1177static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc);
1178static 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);
1179static 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);
1180static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
1181static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
1182static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc);
1183static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
1184static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
1185static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1186static 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);
1187static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1188static 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);
1189static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1190static 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);
1191static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1192static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
1193static 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);
1194static rb_node_line_t *rb_node_line_new(struct parser_params *p, const YYLTYPE *loc);
1195static rb_node_file_t *rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1196static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
1197
1198#define NEW_SCOPE(a,b,c,loc) (NODE *)rb_node_scope_new(p,a,b,c,loc)
1199#define NEW_SCOPE2(t,a,b,c,loc) (NODE *)rb_node_scope_new2(p,t,a,b,c,loc)
1200#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
1201#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)
1202#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)
1203#define NEW_CASE(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case_new(p,h,b,loc,ck_loc,ek_loc)
1204#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc)
1205#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc)
1206#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)
1207#define NEW_IN(c,t,e,loc,ik_loc,tk_loc,o_loc) (NODE *)rb_node_in_new(p,c,t,e,loc,ik_loc,tk_loc,o_loc)
1208#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)
1209#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)
1210#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
1211#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)
1212#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
1213#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
1214#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
1215#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
1216#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc)
1217#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
1218#define NEW_AND(f,s,loc,op_loc) (NODE *)rb_node_and_new(p,f,s,loc,op_loc)
1219#define NEW_OR(f,s,loc,op_loc) (NODE *)rb_node_or_new(p,f,s,loc,op_loc)
1220#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
1221#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
1222#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
1223#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
1224#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
1225#define NEW_CDECL(v,val,path,share,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,share,loc)
1226#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
1227#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)
1228#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)
1229#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
1230#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
1231#define NEW_OP_CDECL(v,op,val,share,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,share,loc)
1232#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
1233#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
1234#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
1235#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
1236#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
1237#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)
1238#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
1239#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
1240#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
1241#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1242#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1243#define NEW_RETURN(s,loc,k_loc) (NODE *)rb_node_return_new(p,s,loc,k_loc)
1244#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)
1245#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1246#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1247#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1248#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1249#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1250#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1251#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1252#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1253#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1254#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1255#define NEW_INTEGER(val, base,loc) (NODE *)rb_node_integer_new(p,val,base,loc)
1256#define NEW_FLOAT(val,loc) (NODE *)rb_node_float_new(p,val,loc)
1257#define NEW_RATIONAL(val,base,seen_point,loc) (NODE *)rb_node_rational_new(p,val,base,seen_point,loc)
1258#define NEW_IMAGINARY(val,base,seen_point,numeric_type,loc) (NODE *)rb_node_imaginary_new(p,val,base,seen_point,numeric_type,loc)
1259#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1260#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1261#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1262#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1263#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1264#define NEW_EVSTR(n,loc,o_loc,c_loc) (NODE *)rb_node_evstr_new(p,n,loc,o_loc,c_loc)
1265#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)
1266#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1267#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1268#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1269#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1270#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1271#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1272#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1273#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1274#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc)
1275#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_loc)
1276#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1277#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1278#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
1279#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
1280#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1281#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)
1282#define NEW_MODULE(n,b,loc,mk_loc,ek_loc) (NODE *)rb_node_module_new(p,n,b,loc,mk_loc,ek_loc)
1283#define NEW_SCLASS(r,b,loc,ck_loc,op_loc,ek_loc) (NODE *)rb_node_sclass_new(p,r,b,loc,ck_loc,op_loc,ek_loc)
1284#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc)
1285#define NEW_COLON3(i,loc,d_loc,n_loc) (NODE *)rb_node_colon3_new(p,i,loc,d_loc,n_loc)
1286#define NEW_DOT2(b,e,loc,op_loc) (NODE *)rb_node_dot2_new(p,b,e,loc,op_loc)
1287#define NEW_DOT3(b,e,loc,op_loc) (NODE *)rb_node_dot3_new(p,b,e,loc,op_loc)
1288#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
1289#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
1290#define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc)
1291#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
1292#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
1293#define NEW_DEFINED(e,loc,k_loc) (NODE *)rb_node_defined_new(p,e,loc, k_loc)
1294#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)
1295#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc)
1296#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1297#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1298#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)
1299#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1300#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1301#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1302#define NEW_LINE(loc) (NODE *)rb_node_line_new(p,loc)
1303#define NEW_FILE(str,loc) (NODE *)rb_node_file_new(p,str,loc)
1304#define NEW_ENCODING(loc) (NODE *)rb_node_encoding_new(p,loc)
1305#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1306
1307enum internal_node_type {
1308 NODE_INTERNAL_ONLY = NODE_LAST,
1309 NODE_DEF_TEMP,
1310 NODE_EXITS,
1311 NODE_INTERNAL_LAST
1312};
1313
1314static const char *
1315parser_node_name(int node)
1316{
1317 switch (node) {
1318 case NODE_DEF_TEMP:
1319 return "NODE_DEF_TEMP";
1320 case NODE_EXITS:
1321 return "NODE_EXITS";
1322 default:
1323 return ruby_node_name(node);
1324 }
1325}
1326
1327/* This node is parse.y internal */
1328struct RNode_DEF_TEMP {
1329 NODE node;
1330
1331 /* for NODE_DEFN/NODE_DEFS */
1332
1333 struct RNode *nd_def;
1334 ID nd_mid;
1335
1336 struct {
1337 int max_numparam;
1338 NODE *numparam_save;
1339 struct lex_context ctxt;
1340 } save;
1341};
1342
1343#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1344
1345static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1346static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1347static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1348static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1349static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1350
1351#define NEW_BREAK(s,loc,k_loc) (NODE *)rb_node_break_new(p,s,loc,k_loc)
1352#define NEW_NEXT(s,loc,k_loc) (NODE *)rb_node_next_new(p,s,loc,k_loc)
1353#define NEW_REDO(loc,k_loc) (NODE *)rb_node_redo_new(p,loc,k_loc)
1354#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1355
1356/* Make a new internal node, which should not be appeared in the
1357 * result AST and does not have node_id and location. */
1358static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1359#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1360
1361static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1362
1363static int
1364parser_get_node_id(struct parser_params *p)
1365{
1366 int node_id = p->node_id;
1367 p->node_id++;
1368 return node_id;
1369}
1370
1371static void
1372anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1373{
1374 if (id == tANDDOT) {
1375 yyerror1(loc, "&. inside multiple assignment destination");
1376 }
1377}
1378
1379static inline void
1380set_line_body(NODE *body, int line)
1381{
1382 if (!body) return;
1383 switch (nd_type(body)) {
1384 case NODE_RESCUE:
1385 case NODE_ENSURE:
1386 nd_set_line(body, line);
1387 }
1388}
1389
1390static void
1391set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1392{
1393 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1394 nd_set_line(node, beg->end_pos.lineno);
1395}
1396
1397static NODE *
1398last_expr_node(NODE *expr)
1399{
1400 while (expr) {
1401 if (nd_type_p(expr, NODE_BLOCK)) {
1402 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1403 }
1404 else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) {
1405 expr = RNODE_BEGIN(expr)->nd_body;
1406 }
1407 else {
1408 break;
1409 }
1410 }
1411 return expr;
1412}
1413
1414#ifndef RIPPER
1415#define yyparse ruby_yyparse
1416#endif
1417
1418static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1419static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1420static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1421static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1422static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1423static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1424
1425static NODE *newline_node(NODE*);
1426static void fixpos(NODE*,NODE*);
1427
1428static int value_expr(struct parser_params*,NODE*);
1429static void void_expr(struct parser_params*,NODE*);
1430static NODE *remove_begin(NODE*);
1431static NODE *void_stmts(struct parser_params*,NODE*);
1432static void reduce_nodes(struct parser_params*,NODE**);
1433static void block_dup_check(struct parser_params*,NODE*,NODE*);
1434
1435static NODE *block_append(struct parser_params*,NODE*,NODE*);
1436static NODE *list_append(struct parser_params*,NODE*,NODE*);
1437static NODE *list_concat(NODE*,NODE*);
1438static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1439static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1440static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1441static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1442static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1443static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1444static NODE *str2dstr(struct parser_params*,NODE*);
1445static NODE *evstr2dstr(struct parser_params*,NODE*);
1446static NODE *splat_array(NODE*);
1447static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1448
1449static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1450static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1451static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1452static 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);
1453static 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;}
1454static NODE *command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc);
1455
1456static bool args_info_empty_p(struct rb_args_info *args);
1457static 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*);
1458static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1459#define new_empty_args_tail(p, loc) new_args_tail(p, 0, 0, 0, loc)
1460static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1461static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1462static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1463static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1464static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1465static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1466
1467static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1468static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1469
1470static NODE* negate_lit(struct parser_params*, NODE*,const YYLTYPE*);
1471static void no_blockarg(struct parser_params*,NODE*);
1472static NODE *ret_args(struct parser_params*,NODE*);
1473static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1474static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1475
1476static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1477static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1478
1479static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1480static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1481
1482static VALUE rb_backref_error(struct parser_params*,NODE*);
1483static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1484
1485static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1486static 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);
1487static 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);
1488static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1489static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1490
1491static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1492
1493static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1494static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1495
1496static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1497static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1498
1499static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1500
1501static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *);
1502
1503#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1504
1505static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1506
1507static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1508
1509static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1510
1511static rb_ast_id_table_t *local_tbl(struct parser_params*);
1512
1513static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1514static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1515
1516static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1517static NODE *heredoc_dedent(struct parser_params*,NODE*);
1518
1519static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1520
1521static 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);
1522
1523#ifdef RIPPER
1524#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1525#define set_value(val) (p->s_lvalue = val)
1526static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1527static int id_is_var(struct parser_params *p, ID id);
1528#endif
1529
1530RUBY_SYMBOL_EXPORT_BEGIN
1531VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1532int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1533enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1534VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1535void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1536PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1537YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1538YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1539YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1540YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1541YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1542YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1543void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1544RUBY_SYMBOL_EXPORT_END
1545
1546static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1547static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1548static int pattern_has_capture(struct parser_params *p, NODE *node);
1549static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1550static VALUE formal_argument_error(struct parser_params*, ID);
1551static ID shadowing_lvar(struct parser_params*,ID);
1552static void new_bv(struct parser_params*,ID);
1553
1554static void local_push(struct parser_params*,int);
1555static void local_pop(struct parser_params*);
1556static void local_var(struct parser_params*, ID);
1557static void arg_var(struct parser_params*, ID);
1558static int local_id(struct parser_params *p, ID id);
1559static int local_id_ref(struct parser_params*, ID, ID **);
1560#define internal_id rb_parser_internal_id
1561ID internal_id(struct parser_params*);
1562static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1563static int check_forwarding_args(struct parser_params*);
1564static void add_forwarding_args(struct parser_params *p);
1565static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1566
1567static const struct vtable *dyna_push(struct parser_params *);
1568static void dyna_pop(struct parser_params*, const struct vtable *);
1569static int dyna_in_block(struct parser_params*);
1570#define dyna_var(p, id) local_var(p, id)
1571static int dvar_defined(struct parser_params*, ID);
1572#define dvar_defined_ref rb_parser_dvar_defined_ref
1573int dvar_defined_ref(struct parser_params*, ID, ID**);
1574static int dvar_curr(struct parser_params*,ID);
1575
1576static int lvar_defined(struct parser_params*, ID);
1577
1578static NODE *numparam_push(struct parser_params *p);
1579static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1580
1581#define METHOD_NOT '!'
1582
1583#define idFWD_REST '*'
1584#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1585#define idFWD_BLOCK '&'
1586#define idFWD_ALL idDot3
1587#define arg_FWD_BLOCK idFWD_BLOCK
1588
1589#define RE_ONIG_OPTION_IGNORECASE 1
1590#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1591#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1592#define RE_OPTION_ONCE (1<<16)
1593#define RE_OPTION_ENCODING_SHIFT 8
1594#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1595#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1596#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1597#define RE_OPTION_MASK 0xff
1598#define RE_OPTION_ARG_ENCODING_NONE 32
1599
1600#define CHECK_LITERAL_WHEN (st_table *)1
1601#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1602
1603#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1604RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1605
1606#define TOKEN2ID(tok) ( \
1607 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1608 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1609 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1610 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1611 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1612 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1613 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1614
1615/****** Ripper *******/
1616
1617#ifdef RIPPER
1618
1619#include "eventids1.h"
1620#include "eventids2.h"
1621
1622extern const struct ripper_parser_ids ripper_parser_ids;
1623
1624static VALUE ripper_dispatch0(struct parser_params*,ID);
1625static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1626static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1627static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1628static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1629static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1630static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1631void ripper_error(struct parser_params *p);
1632
1633#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1634#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1635#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1636#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1637#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1638#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1639#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1640
1641#define yyparse ripper_yyparse
1642
1643static VALUE
1644aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1645{
1646 if (!NIL_P(pre_arg)) {
1647 if (!NIL_P(pre_args)) {
1648 rb_ary_unshift(pre_args, pre_arg);
1649 }
1650 else {
1651 pre_args = rb_ary_new_from_args(1, pre_arg);
1652 }
1653 }
1654 return pre_args;
1655}
1656
1657#define ID2VAL(id) STATIC_ID2SYM(id)
1658#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1659#endif /* RIPPER */
1660
1661#define KWD2EID(t, v) keyword_##t
1662
1663static NODE *
1664new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, NODE *parent, const YYLTYPE *loc)
1665{
1666 body = remove_begin(body);
1667 reduce_nodes(p, &body);
1668 NODE *n = NEW_SCOPE(args, body, parent, loc);
1669 nd_set_line(n, loc->end_pos.lineno);
1670 set_line_body(body, loc->beg_pos.lineno);
1671 return n;
1672}
1673
1674static NODE *
1675rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1676 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1677{
1678 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1679 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1680 loc.beg_pos = arg_loc->beg_pos;
1681 return NEW_RESCUE(arg, rescue, 0, &loc);
1682}
1683
1684static NODE *add_block_exit(struct parser_params *p, NODE *node);
1685static rb_node_exits_t *init_block_exit(struct parser_params *p);
1686static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1687static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1688static void clear_block_exit(struct parser_params *p, bool error);
1689
1690static void
1691next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1692{
1693 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1694}
1695
1696static void
1697restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1698{
1699 /* See: def_name action */
1700 struct lex_context ctxt = temp->save.ctxt;
1701 p->ctxt.in_def = ctxt.in_def;
1702 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1703 p->ctxt.in_rescue = ctxt.in_rescue;
1704 p->max_numparam = temp->save.max_numparam;
1705 numparam_pop(p, temp->save.numparam_save);
1706 clear_block_exit(p, true);
1707}
1708
1709static void
1710endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1711{
1712 if (is_attrset_id(mid)) {
1713 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1714 }
1715 token_info_drop(p, "def", loc->beg_pos);
1716}
1717
1718#define debug_token_line(p, name, line) do { \
1719 if (p->debug) { \
1720 const char *const pcur = p->lex.pcur; \
1721 const char *const ptok = p->lex.ptok; \
1722 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1723 line, p->ruby_sourceline, \
1724 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1725 } \
1726 } while (0)
1727
1728#define begin_definition(k, loc_beg, loc_end) \
1729 do { \
1730 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1731 /* singleton class */ \
1732 p->ctxt.cant_return = !p->ctxt.in_def; \
1733 p->ctxt.in_def = 0; \
1734 } \
1735 else if (p->ctxt.in_def) { \
1736 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1737 yyerror1(&loc, k " definition in method body"); \
1738 } \
1739 else { \
1740 p->ctxt.cant_return = 1; \
1741 } \
1742 local_push(p, 0); \
1743 } while (0)
1744
1745#ifndef RIPPER
1746# define ifndef_ripper(x) (x)
1747# define ifdef_ripper(r,x) (x)
1748#else
1749# define ifndef_ripper(x)
1750# define ifdef_ripper(r,x) (r)
1751#endif
1752
1753# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1754# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1755# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1756# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1757# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1758# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1759# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1760# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1761# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1762# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1763# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1764# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1765# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1766# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1767# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1768# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1769# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1770# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1771# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1772# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1773#ifdef RIPPER
1774extern const ID id_warn, id_warning, id_gets, id_assoc;
1775# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1776# define WARN_S_L(s,l) STR_NEW(s,l)
1777# define WARN_S(s) STR_NEW2(s)
1778# define WARN_I(i) INT2NUM(i)
1779# define WARN_ID(i) rb_id2str(i)
1780# define PRIsWARN PRIsVALUE
1781# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1782# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1783# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1784# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1785# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1786# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1787# define compile_error ripper_compile_error
1788#else
1789# define WARN_S_L(s,l) s
1790# define WARN_S(s) s
1791# define WARN_I(i) i
1792# define WARN_ID(i) rb_id2name(i)
1793# define PRIsWARN PRIsVALUE
1794# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1795# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1796# define WARN_CALL rb_compile_warn
1797# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1798# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1799# define WARNING_CALL rb_compile_warning
1800PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1801# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1802#endif
1803
1804#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1805
1806static NODE *
1807add_block_exit(struct parser_params *p, NODE *node)
1808{
1809 if (!node) {
1810 compile_error(p, "unexpected null node");
1811 return 0;
1812 }
1813 switch (nd_type(node)) {
1814 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1815 default:
1816 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1817 return node;
1818 }
1819 if (!p->ctxt.in_defined) {
1820 rb_node_exits_t *exits = p->exits;
1821 if (exits) {
1822 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1823 exits->nd_stts = node;
1824 }
1825 }
1826 return node;
1827}
1828
1829static rb_node_exits_t *
1830init_block_exit(struct parser_params *p)
1831{
1832 rb_node_exits_t *old = p->exits;
1833 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1834 exits->nd_chain = 0;
1835 exits->nd_stts = RNODE(exits);
1836 p->exits = exits;
1837 return old;
1838}
1839
1840static rb_node_exits_t *
1841allow_block_exit(struct parser_params *p)
1842{
1843 rb_node_exits_t *exits = p->exits;
1844 p->exits = 0;
1845 return exits;
1846}
1847
1848static void
1849restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1850{
1851 p->exits = exits;
1852}
1853
1854static void
1855clear_block_exit(struct parser_params *p, bool error)
1856{
1857 rb_node_exits_t *exits = p->exits;
1858 if (!exits) return;
1859 if (error) {
1860 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1861 switch (nd_type(e)) {
1862 case NODE_BREAK:
1863 yyerror1(&e->nd_loc, "Invalid break");
1864 break;
1865 case NODE_NEXT:
1866 yyerror1(&e->nd_loc, "Invalid next");
1867 break;
1868 case NODE_REDO:
1869 yyerror1(&e->nd_loc, "Invalid redo");
1870 break;
1871 default:
1872 yyerror1(&e->nd_loc, "unexpected node");
1873 goto end_checks; /* no nd_chain */
1874 }
1875 }
1876 end_checks:;
1877 }
1878 exits->nd_stts = RNODE(exits);
1879 exits->nd_chain = 0;
1880}
1881
1882#define WARN_EOL(tok) \
1883 (looking_at_eol_p(p) ? \
1884 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1885 (void)0)
1886static int looking_at_eol_p(struct parser_params *p);
1887
1888static NODE *
1889get_nd_value(struct parser_params *p, NODE *node)
1890{
1891 switch (nd_type(node)) {
1892 case NODE_GASGN:
1893 return RNODE_GASGN(node)->nd_value;
1894 case NODE_IASGN:
1895 return RNODE_IASGN(node)->nd_value;
1896 case NODE_LASGN:
1897 return RNODE_LASGN(node)->nd_value;
1898 case NODE_DASGN:
1899 return RNODE_DASGN(node)->nd_value;
1900 case NODE_MASGN:
1901 return RNODE_MASGN(node)->nd_value;
1902 case NODE_CVASGN:
1903 return RNODE_CVASGN(node)->nd_value;
1904 case NODE_CDECL:
1905 return RNODE_CDECL(node)->nd_value;
1906 default:
1907 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1908 return 0;
1909 }
1910}
1911
1912static void
1913set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1914{
1915 switch (nd_type(node)) {
1916 case NODE_CDECL:
1917 RNODE_CDECL(node)->nd_value = rhs;
1918 break;
1919 case NODE_GASGN:
1920 RNODE_GASGN(node)->nd_value = rhs;
1921 break;
1922 case NODE_IASGN:
1923 RNODE_IASGN(node)->nd_value = rhs;
1924 break;
1925 case NODE_LASGN:
1926 RNODE_LASGN(node)->nd_value = rhs;
1927 break;
1928 case NODE_DASGN:
1929 RNODE_DASGN(node)->nd_value = rhs;
1930 break;
1931 case NODE_MASGN:
1932 RNODE_MASGN(node)->nd_value = rhs;
1933 break;
1934 case NODE_CVASGN:
1935 RNODE_CVASGN(node)->nd_value = rhs;
1936 break;
1937 default:
1938 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1939 break;
1940 }
1941}
1942
1943static ID
1944get_nd_vid(struct parser_params *p, NODE *node)
1945{
1946 switch (nd_type(node)) {
1947 case NODE_CDECL:
1948 return RNODE_CDECL(node)->nd_vid;
1949 case NODE_GASGN:
1950 return RNODE_GASGN(node)->nd_vid;
1951 case NODE_IASGN:
1952 return RNODE_IASGN(node)->nd_vid;
1953 case NODE_LASGN:
1954 return RNODE_LASGN(node)->nd_vid;
1955 case NODE_DASGN:
1956 return RNODE_DASGN(node)->nd_vid;
1957 case NODE_CVASGN:
1958 return RNODE_CVASGN(node)->nd_vid;
1959 default:
1960 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1961 return 0;
1962 }
1963}
1964
1965static NODE *
1966get_nd_args(struct parser_params *p, NODE *node)
1967{
1968 switch (nd_type(node)) {
1969 case NODE_CALL:
1970 return RNODE_CALL(node)->nd_args;
1971 case NODE_OPCALL:
1972 return RNODE_OPCALL(node)->nd_args;
1973 case NODE_FCALL:
1974 return RNODE_FCALL(node)->nd_args;
1975 case NODE_QCALL:
1976 return RNODE_QCALL(node)->nd_args;
1977 case NODE_SUPER:
1978 return RNODE_SUPER(node)->nd_args;
1979 case NODE_VCALL:
1980 case NODE_ZSUPER:
1981 case NODE_YIELD:
1982 case NODE_RETURN:
1983 case NODE_BREAK:
1984 case NODE_NEXT:
1985 return 0;
1986 default:
1987 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1988 return 0;
1989 }
1990}
1991
1992static st_index_t
1993djb2(const uint8_t *str, size_t len)
1994{
1995 st_index_t hash = 5381;
1996
1997 for (size_t i = 0; i < len; i++) {
1998 hash = ((hash << 5) + hash) + str[i];
1999 }
2000
2001 return hash;
2002}
2003
2004static st_index_t
2005parser_memhash(const void *ptr, rb_len_t len)
2006{
2007 return djb2(ptr, len);
2008}
2009
2010#define PARSER_STRING_PTR(str) (str->ptr)
2011#define PARSER_STRING_LEN(str) (str->len)
2012#define PARSER_STRING_END(str) (&str->ptr[str->len])
2013#define STRING_SIZE(str) ((size_t)str->len + 1)
2014#define STRING_TERM_LEN(str) (1)
2015#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
2016#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
2017 REALLOC_N(str->ptr, char, (size_t)total + termlen); \
2018 str->len = total; \
2019} while (0)
2020#define STRING_SET_LEN(str, n) do { \
2021 (str)->len = (n); \
2022} while (0)
2023#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2024 ((ptrvar) = str->ptr, \
2025 (lenvar) = str->len)
2026
2027static inline int
2028parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2029{
2030 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2031}
2032
2033static rb_parser_string_t *
2034rb_parser_string_new(rb_parser_t *p, const char *ptr, rb_len_t len)
2035{
2036 rb_parser_string_t *str;
2037
2038 if (len < 0) {
2039 rb_bug("negative string size (or size too big): %"PRIdLEN, len);
2040 }
2041
2042 str = xcalloc(1, sizeof(rb_parser_string_t));
2043 str->ptr = xcalloc(len + 1, sizeof(char));
2044
2045 if (ptr) {
2046 memcpy(PARSER_STRING_PTR(str), ptr, len);
2047 }
2048 STRING_SET_LEN(str, len);
2049 STRING_TERM_FILL(str);
2050 return str;
2051}
2052
2053static rb_parser_string_t *
2054rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, rb_len_t len, rb_encoding *enc)
2055{
2056 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2057 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2058 str->enc = enc;
2059 return str;
2060}
2061
2062#ifndef RIPPER
2063rb_parser_string_t *
2064rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2065{
2066 /* Type check */
2067 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2068 RB_GC_GUARD(str);
2069 return ret;
2070}
2071
2072void
2073rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2074{
2075 if (!str) return;
2076 xfree(PARSER_STRING_PTR(str));
2077 xfree(str);
2078}
2079#endif
2080
2081static st_index_t
2082rb_parser_str_hash(rb_parser_string_t *str)
2083{
2084 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2085}
2086
2087static st_index_t
2088rb_char_p_hash(const char *c)
2089{
2090 return parser_memhash((const void *)c, strlen(c));
2091}
2092
2093static size_t
2094rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2095{
2096 return PARSER_STRING_LEN(str);
2097}
2098
2099#ifndef RIPPER
2100static char *
2101rb_parser_string_end(rb_parser_string_t *str)
2102{
2103 return &str->ptr[str->len];
2104}
2105#endif
2106
2107static void
2108rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2109{
2110 str->enc = enc;
2111}
2112
2113static rb_encoding *
2114rb_parser_str_get_encoding(rb_parser_string_t *str)
2115{
2116 return str->enc;
2117}
2118
2119#ifndef RIPPER
2120static bool
2121PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2122{
2123 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2124}
2125#endif
2126
2127static int
2128PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2129{
2130 return str->coderange;
2131}
2132
2133static void
2134PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2135{
2136 str->coderange = coderange;
2137}
2138
2139static void
2140PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2141{
2142 rb_parser_string_set_encoding(str, enc);
2143 PARSER_ENC_CODERANGE_SET(str, cr);
2144}
2145
2146static void
2147PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2148{
2149 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2150}
2151
2152static bool
2153PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2154{
2155 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2156}
2157
2158static bool
2159PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2160{
2161 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2162}
2163
2164static const char *
2165rb_parser_search_nonascii(const char *p, const char *e)
2166{
2167 const char *s = p;
2168
2169 for (; s < e; s++) {
2170 if (*s & 0x80) return s;
2171 }
2172
2173 return NULL;
2174}
2175
2176static int
2177rb_parser_coderange_scan(struct parser_params *p, const char *ptr, rb_len_t len, rb_encoding *enc)
2178{
2179 const char *e = ptr + len;
2180
2181 if (enc == rb_ascii8bit_encoding()) {
2182 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2183 ptr = rb_parser_search_nonascii(ptr, e);
2184 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2185 }
2186
2187 /* parser string encoding is always asciicompat */
2188 ptr = rb_parser_search_nonascii(ptr, e);
2189 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2190 for (;;) {
2191 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2192 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2193 ptr += MBCLEN_CHARFOUND_LEN(ret);
2194 if (ptr == e) break;
2195 ptr = rb_parser_search_nonascii(ptr, e);
2196 if (!ptr) break;
2197 }
2198
2199 return RB_PARSER_ENC_CODERANGE_VALID;
2200}
2201
2202static int
2203rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2204{
2205 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2206}
2207
2208static int
2209rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2210{
2211 int cr = PARSER_ENC_CODERANGE(str);
2212
2213 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2214 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2215 PARSER_ENC_CODERANGE_SET(str, cr);
2216 }
2217
2218 return cr;
2219}
2220
2221static rb_parser_string_t *
2222rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2223{
2224 if (rb_parser_str_get_encoding(str) == enc)
2225 return str;
2226 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2227 PARSER_ENC_CODERANGE_CLEAR(str);
2228 }
2229 rb_parser_string_set_encoding(str, enc);
2230 return str;
2231}
2232
2233static bool
2234rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2235{
2236 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2237}
2238
2239static rb_encoding *
2240rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2241{
2242 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2243 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2244
2245 if (enc1 == NULL || enc2 == NULL)
2246 return 0;
2247
2248 if (enc1 == enc2) {
2249 return enc1;
2250 }
2251
2252 if (PARSER_STRING_LEN(str2) == 0)
2253 return enc1;
2254 if (PARSER_STRING_LEN(str1) == 0)
2255 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2256
2257 int cr1, cr2;
2258
2259 cr1 = rb_parser_enc_str_coderange(p, str1);
2260 cr2 = rb_parser_enc_str_coderange(p, str2);
2261
2262 if (cr1 != cr2) {
2263 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2264 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2265 }
2266
2267 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2268 return enc1;
2269 }
2270
2271 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2272 return enc2;
2273 }
2274
2275 return 0;
2276}
2277
2278static void
2279rb_parser_str_modify(rb_parser_string_t *str)
2280{
2281 PARSER_ENC_CODERANGE_CLEAR(str);
2282}
2283
2284static void
2285rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, rb_len_t len)
2286{
2287 rb_len_t capa;
2288 const int termlen = STRING_TERM_LEN(str);
2289
2290 if (len > (capa = (rb_len_t)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2291 rb_bug("probable buffer overflow: %"PRIdLEN" for %"PRIdLEN, len, capa);
2292 }
2293
2294 int cr = PARSER_ENC_CODERANGE(str);
2295 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2296 /* Leave unknown. */
2297 }
2298 else if (len > PARSER_STRING_LEN(str)) {
2299 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2300 }
2301 else if (len < PARSER_STRING_LEN(str)) {
2302 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2303 /* ASCII-only string is keeping after truncated. Valid
2304 * and broken may be invalid or valid, leave unknown. */
2305 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2306 }
2307 }
2308
2309 STRING_SET_LEN(str, len);
2310 STRING_TERM_FILL(str);
2311}
2312
2313static rb_parser_string_t *
2314rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, rb_len_t len)
2315{
2316 rb_parser_str_modify(str);
2317 if (len == 0) return 0;
2318
2319 rb_len_t total, olen, off = -1;
2320 char *sptr;
2321 const int termlen = STRING_TERM_LEN(str);
2322
2323 PARSER_STRING_GETMEM(str, sptr, olen);
2324 if (ptr >= sptr && ptr <= sptr + olen) {
2325 off = ptr - sptr;
2326 }
2327
2328 if (olen > RB_LEN_MAX - len) {
2329 compile_error(p, "string sizes too big");
2330 return 0;
2331 }
2332 total = olen + len;
2333 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2334 sptr = PARSER_STRING_PTR(str);
2335 if (off != -1) {
2336 ptr = sptr + off;
2337 }
2338 memcpy(sptr + olen, ptr, len);
2339 STRING_SET_LEN(str, total);
2340 STRING_TERM_FILL(str);
2341
2342 return str;
2343}
2344
2345#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2346#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2347
2348static rb_parser_string_t *
2349rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, rb_len_t len,
2350 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2351{
2352 int str_cr, res_cr;
2353 rb_encoding *str_enc, *res_enc;
2354
2355 str_enc = rb_parser_str_get_encoding(str);
2356 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2357
2358 if (str_enc == ptr_enc) {
2359 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2360 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2361 }
2362 }
2363 else {
2364 /* parser string encoding is always asciicompat */
2365 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2366 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2367 }
2368 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2369 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2370 str_cr = rb_parser_enc_str_coderange(p, str);
2371 }
2372 }
2373 }
2374 if (ptr_cr_ret)
2375 *ptr_cr_ret = ptr_cr;
2376
2377 if (str_enc != ptr_enc &&
2378 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2379 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2380 goto incompatible;
2381 }
2382
2383 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2384 res_enc = str_enc;
2385 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2386 }
2387 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2388 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2389 res_enc = str_enc;
2390 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2391 }
2392 else {
2393 res_enc = ptr_enc;
2394 res_cr = ptr_cr;
2395 }
2396 }
2397 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2398 res_enc = str_enc;
2399 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2400 res_cr = str_cr;
2401 else
2402 res_cr = ptr_cr;
2403 }
2404 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2405 res_enc = str_enc;
2406 res_cr = str_cr;
2407 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2408 }
2409
2410 if (len < 0) {
2411 compile_error(p, "negative string size (or size too big)");
2412 return 0;
2413 }
2414 parser_str_cat(str, ptr, len);
2415 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2416 return str;
2417
2418 incompatible:
2419 compile_error(p, "incompatible character encodings: %s and %s",
2420 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2421 return 0;
2422}
2423
2424static rb_parser_string_t *
2425rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, rb_len_t len,
2426 rb_encoding *ptr_enc)
2427{
2428 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2429}
2430
2431static rb_parser_string_t *
2432rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2433{
2434 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2435
2436 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2437 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2438
2439 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2440
2441 return str;
2442}
2443
2444static rb_parser_string_t *
2445rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, rb_len_t len)
2446{
2447 if (len < 0) {
2448 rb_bug("negative string size (or size too big)");
2449 }
2450
2451 rb_len_t slen = PARSER_STRING_LEN(str);
2452
2453 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2454 PARSER_ENC_CODERANGE_CLEAR(str);
2455 }
2456
2457 {
2458 rb_len_t capa;
2459 const int termlen = STRING_TERM_LEN(str);
2460
2461 if ((capa = slen) < len) {
2462 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2463 }
2464 else if (len == slen) return str;
2465 STRING_SET_LEN(str, len);
2466 STRING_TERM_FILL(str);
2467 }
2468 return str;
2469}
2470
2471# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2472 ((ptrvar) = str->ptr, \
2473 (lenvar) = str->len, \
2474 (encvar) = str->enc)
2475
2476static int
2477rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2478{
2479 rb_len_t len1, len2;
2480 const char *ptr1, *ptr2;
2481 rb_encoding *enc1, *enc2;
2482
2483 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2484 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2485
2486 return (len1 != len2 ||
2487 enc1 != enc2 ||
2488 memcmp(ptr1, ptr2, len1) != 0);
2489}
2490
2491static void
2492rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2493{
2494 long i;
2495 if (ary->capa < len) {
2496 ary->capa = len;
2497 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2498 for (i = ary->len; i < len; i++) {
2499 ary->data[i] = 0;
2500 }
2501 }
2502}
2503
2504/*
2505 * Do not call this directly.
2506 * Use rb_parser_ary_new_capa_for_XXX() instead.
2507 */
2508static rb_parser_ary_t *
2509parser_ary_new_capa(rb_parser_t *p, long len)
2510{
2511 if (len < 0) {
2512 rb_bug("negative array size (or size too big): %ld", len);
2513 }
2514 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2515 ary->data_type = 0;
2516 ary->len = 0;
2517 ary->capa = len;
2518 if (0 < len) {
2519 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2520 }
2521 else {
2522 ary->data = NULL;
2523 }
2524 return ary;
2525}
2526
2527#ifndef RIPPER
2528static rb_parser_ary_t *
2529rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2530{
2531 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2532 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2533 return ary;
2534}
2535
2536static rb_parser_ary_t *
2537rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2538{
2539 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2540 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2541 return ary;
2542}
2543#endif
2544
2545static rb_parser_ary_t *
2546rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2547{
2548 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2549 ary->data_type = PARSER_ARY_DATA_NODE;
2550 return ary;
2551}
2552
2553/*
2554 * Do not call this directly.
2555 * Use rb_parser_ary_push_XXX() instead.
2556 */
2557static rb_parser_ary_t *
2558parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2559{
2560 if (ary->len == ary->capa) {
2561 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2562 }
2563 ary->data[ary->len++] = val;
2564 return ary;
2565}
2566
2567#ifndef RIPPER
2568static rb_parser_ary_t *
2569rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2570{
2571 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2572 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2573 }
2574 return parser_ary_push(p, ary, val);
2575}
2576
2577static rb_parser_ary_t *
2578rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2579{
2580 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2581 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2582 }
2583 return parser_ary_push(p, ary, val);
2584}
2585#endif
2586
2587static rb_parser_ary_t *
2588rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2589{
2590 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2591 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2592 }
2593 return parser_ary_push(p, ary, val);
2594}
2595
2596#ifndef RIPPER
2597static void
2598rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2599{
2600 if (!token) return;
2601 rb_parser_string_free(p, token->str);
2602 xfree(token);
2603}
2604
2605static void
2606rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2607{
2608# define foreach_ary(ptr) \
2609 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2610 ptr < end_ary_data; ptr++)
2611 switch (ary->data_type) {
2612 case PARSER_ARY_DATA_AST_TOKEN:
2613 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2614 break;
2615 case PARSER_ARY_DATA_SCRIPT_LINE:
2616 foreach_ary(data) {rb_parser_string_free(p, *data);}
2617 break;
2618 case PARSER_ARY_DATA_NODE:
2619 /* Do nothing because nodes are freed when rb_ast_t is freed */
2620 break;
2621 default:
2622 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2623 break;
2624 }
2625# undef foreach_ary
2626 xfree(ary->data);
2627 xfree(ary);
2628}
2629
2630#endif /* !RIPPER */
2631%}
2632
2633%expect 0
2634%define api.pure
2635%define parse.error verbose
2636%printer {
2637 if ((NODE *)$$ == (NODE *)-1) {
2638 rb_parser_printf(p, "NODE_SPECIAL");
2639 }
2640 else if ($$) {
2641 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2642 }
2643} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2644 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2645%printer {
2646 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2647} <id>
2648%printer {
2649 switch (nd_type(RNODE($$))) {
2650 case NODE_INTEGER:
2651 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2652 break;
2653 case NODE_FLOAT:
2654 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2655 break;
2656 case NODE_RATIONAL:
2657 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2658 break;
2659 case NODE_IMAGINARY:
2660 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2661 break;
2662 default:
2663 break;
2664 }
2665} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2666%printer {
2667 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2668} tNTH_REF
2669%printer {
2670 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2671} tBACK_REF
2672
2673%destructor {
2674 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2675} <labels>
2676
2677%lex-param {struct parser_params *p}
2678%parse-param {struct parser_params *p}
2679%initial-action
2680{
2681 RUBY_SET_YYLLOC_OF_NONE(@$);
2682};
2683%after-shift after_shift
2684%before-reduce before_reduce
2685%after-reduce after_reduce
2686%after-shift-error-token after_shift_error_token
2687%after-pop-stack after_pop_stack
2688
2689%union {
2690 NODE *node;
2691 rb_node_fcall_t *node_fcall;
2692 rb_node_args_t *node_args;
2693 rb_node_args_aux_t *node_args_aux;
2694 rb_node_opt_arg_t *node_opt_arg;
2695 rb_node_kw_arg_t *node_kw_arg;
2696 rb_node_block_pass_t *node_block_pass;
2697 rb_node_masgn_t *node_masgn;
2698 rb_node_def_temp_t *node_def_temp;
2699 rb_node_exits_t *node_exits;
2700 struct rb_locations_lambda_body_t *locations_lambda_body;
2701 ID id;
2702 int num;
2703 st_table *tbl;
2704 st_table *labels;
2705 const struct vtable *vars;
2706 struct rb_strterm_struct *strterm;
2707 struct lex_context ctxt;
2708 enum lex_state_e state;
2709}
2710
2711%token <id>
2712 keyword_class "'class'"
2713 keyword_module "'module'"
2714 keyword_def "'def'"
2715 keyword_undef "'undef'"
2716 keyword_begin "'begin'"
2717 keyword_rescue "'rescue'"
2718 keyword_ensure "'ensure'"
2719 keyword_end "'end'"
2720 keyword_if "'if'"
2721 keyword_unless "'unless'"
2722 keyword_then "'then'"
2723 keyword_elsif "'elsif'"
2724 keyword_else "'else'"
2725 keyword_case "'case'"
2726 keyword_when "'when'"
2727 keyword_while "'while'"
2728 keyword_until "'until'"
2729 keyword_for "'for'"
2730 keyword_break "'break'"
2731 keyword_next "'next'"
2732 keyword_redo "'redo'"
2733 keyword_retry "'retry'"
2734 keyword_in "'in'"
2735 keyword_do "'do'"
2736 keyword_do_cond "'do' for condition"
2737 keyword_do_block "'do' for block"
2738 keyword_do_LAMBDA "'do' for lambda"
2739 keyword_return "'return'"
2740 keyword_yield "'yield'"
2741 keyword_super "'super'"
2742 keyword_self "'self'"
2743 keyword_nil "'nil'"
2744 keyword_true "'true'"
2745 keyword_false "'false'"
2746 keyword_and "'and'"
2747 keyword_or "'or'"
2748 keyword_not "'not'"
2749 modifier_if "'if' modifier"
2750 modifier_unless "'unless' modifier"
2751 modifier_while "'while' modifier"
2752 modifier_until "'until' modifier"
2753 modifier_rescue "'rescue' modifier"
2754 keyword_alias "'alias'"
2755 keyword_defined "'defined?'"
2756 keyword_BEGIN "'BEGIN'"
2757 keyword_END "'END'"
2758 keyword__LINE__ "'__LINE__'"
2759 keyword__FILE__ "'__FILE__'"
2760 keyword__ENCODING__ "'__ENCODING__'"
2761
2762%token <id> tIDENTIFIER "local variable or method"
2763%token <id> tFID "method"
2764%token <id> tGVAR "global variable"
2765%token <id> tIVAR "instance variable"
2766%token <id> tCONSTANT "constant"
2767%token <id> tCVAR "class variable"
2768%token <id> tLABEL "label"
2769%token <node> tINTEGER "integer literal"
2770%token <node> tFLOAT "float literal"
2771%token <node> tRATIONAL "rational literal"
2772%token <node> tIMAGINARY "imaginary literal"
2773%token <node> tCHAR "char literal"
2774%token <node> tNTH_REF "numbered reference"
2775%token <node> tBACK_REF "back reference"
2776%token <node> tSTRING_CONTENT "literal content"
2777%token <num> tREGEXP_END
2778%token <num> tDUMNY_END "dummy end"
2779
2780%type <node> singleton singleton_expr strings string string1 xstring regexp
2781%type <node> string_contents xstring_contents regexp_contents string_content
2782%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2783%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2784%type <node_def_temp> defn_head defs_head k_def
2785%type <node_exits> block_open k_while k_until k_for allow_exits
2786%type <node> top_stmts top_stmt begin_block endless_arg endless_command
2787%type <node> bodystmt stmts stmt_or_begin stmt expr arg ternary primary
2788%type <node> command command_call command_call_value method_call
2789%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2790%type <node_fcall> fcall
2791%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2792%type <node> args arg_splat call_args opt_call_args
2793%type <node> paren_args opt_paren_args
2794%type <node_args> args_tail block_args_tail
2795%type <node> command_args aref_args
2796%type <node_block_pass> opt_block_arg block_arg
2797%type <node> var_ref var_lhs
2798%type <node> command_rhs arg_rhs
2799%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2800%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args f_empty_arg
2801%type <node_args_aux> f_arg f_arg_item
2802%type <node> f_marg f_rest_marg
2803%type <node_masgn> f_margs
2804%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2805%type <node_args> block_param opt_block_param_def block_param_def opt_block_param
2806%type <id> do bv_decls opt_bv_decl bvar
2807%type <node> lambda brace_body do_body
2808%type <locations_lambda_body> lambda_body
2809%type <node_args> f_larglist f_largs largs_tail
2810%type <node> brace_block cmd_brace_block do_block lhs none fitem
2811%type <node> mlhs_head mlhs_item mlhs_node
2812%type <node_masgn> mlhs mlhs_basic mlhs_inner
2813%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2814%type <node> p_expr p_as p_alt p_expr_basic p_find
2815%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2816%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2817%type <node> p_kwargs p_kwarg p_kw
2818%type <id> keyword_variable user_variable sym operation2 operation3
2819%type <id> cname fname op f_rest_arg f_block_arg opt_comma f_norm_arg f_bad_arg
2820%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2821%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2822%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2823%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2824%type <ctxt> p_in_kwarg p_alt_begin
2825%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2826%type <num> max_numparam
2827%type <node> numparam
2828%type <id> it_id
2829%token END_OF_INPUT 0 "end-of-input"
2830%token <id> '.'
2831
2832/* escaped chars, should be ignored otherwise */
2833%token <id> '\\' "backslash"
2834%token tSP "escaped space"
2835%token <id> '\t' "escaped horizontal tab"
2836%token <id> '\f' "escaped form feed"
2837%token <id> '\r' "escaped carriage return"
2838%token <id> '\13' "escaped vertical tab"
2839%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2840%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2841%token tPOW RUBY_TOKEN(POW) "**"
2842%token tCMP RUBY_TOKEN(CMP) "<=>"
2843%token tEQ RUBY_TOKEN(EQ) "=="
2844%token tEQQ RUBY_TOKEN(EQQ) "==="
2845%token tNEQ RUBY_TOKEN(NEQ) "!="
2846%token tGEQ RUBY_TOKEN(GEQ) ">="
2847%token tLEQ RUBY_TOKEN(LEQ) "<="
2848%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2849%token tOROP RUBY_TOKEN(OROP) "||"
2850%token tMATCH RUBY_TOKEN(MATCH) "=~"
2851%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2852%token tDOT2 RUBY_TOKEN(DOT2) ".."
2853%token tDOT3 RUBY_TOKEN(DOT3) "..."
2854%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2855%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2856%token tAREF RUBY_TOKEN(AREF) "[]"
2857%token tASET RUBY_TOKEN(ASET) "[]="
2858%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2859%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2860%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2861%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2862%token tCOLON3 ":: at EXPR_BEG"
2863%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2864%token tASSOC "=>"
2865%token tLPAREN "("
2866%token tLPAREN_ARG "( arg"
2867%token tLBRACK "["
2868%token tLBRACE "{"
2869%token tLBRACE_ARG "{ arg"
2870%token tSTAR "*"
2871%token tDSTAR "**arg"
2872%token tAMPER "&"
2873%token <num> tLAMBDA "->"
2874%token tSYMBEG "symbol literal"
2875%token tSTRING_BEG "string literal"
2876%token tXSTRING_BEG "backtick literal"
2877%token tREGEXP_BEG "regexp literal"
2878%token tWORDS_BEG "word list"
2879%token tQWORDS_BEG "verbatim word list"
2880%token tSYMBOLS_BEG "symbol list"
2881%token tQSYMBOLS_BEG "verbatim symbol list"
2882%token tSTRING_END "terminator"
2883%token tSTRING_DEND "'}'"
2884%token <state> tSTRING_DBEG "'#{'"
2885%token tSTRING_DVAR tLAMBEG tLABEL_END
2886
2887%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2888%token tHEREDOC_BEG tHEREDOC_END k__END__
2889
2890/*
2891 * precedence table
2892 */
2893
2894%nonassoc tLOWEST
2895%nonassoc tLBRACE_ARG
2896
2897%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2898%left keyword_or keyword_and
2899%right keyword_not
2900%nonassoc keyword_defined
2901%right '=' tOP_ASGN
2902%left modifier_rescue
2903%right '?' ':'
2904%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2905%left tOROP
2906%left tANDOP
2907%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2908%left '>' tGEQ '<' tLEQ
2909%left '|' '^'
2910%left '&'
2911%left tLSHFT tRSHFT
2912%left '+' '-'
2913%left '*' '/' '%'
2914%right tUMINUS_NUM tUMINUS
2915%right tPOW
2916%right '!' '~' tUPLUS
2917
2918%token tLAST_TOKEN
2919
2920/*
2921 * inlining rules
2922 */
2923%rule %inline ident_or_const
2924 : tIDENTIFIER
2925 | tCONSTANT
2926 ;
2927
2928%rule %inline user_or_keyword_variable
2929 : user_variable
2930 | keyword_variable
2931 ;
2932
2933/*
2934 * parameterizing rules
2935 */
2936%rule asgn(rhs) <node>
2937 : lhs '=' lex_ctxt rhs
2938 {
2939 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2940 /*% ripper: assign!($:1, $:4) %*/
2941 }
2942 ;
2943
2944%rule args_tail_basic(value, trailing) <node_args>
2945 : f_kwarg(value) ',' f_kwrest opt_f_block_arg(trailing)
2946 {
2947 $$ = new_args_tail(p, $1, $3, $4, &@3);
2948 /*% ripper: [$:1, $:3, $:4] %*/
2949 }
2950 | f_kwarg(value) opt_f_block_arg(trailing)
2951 {
2952 $$ = new_args_tail(p, $1, 0, $2, &@1);
2953 /*% ripper: [$:1, Qnil, $:2] %*/
2954 }
2955 | f_any_kwrest opt_f_block_arg(trailing)
2956 {
2957 $$ = new_args_tail(p, 0, $1, $2, &@1);
2958 /*% ripper: [Qnil, $:1, $:2] %*/
2959 }
2960 | f_block_arg
2961 {
2962 $$ = new_args_tail(p, 0, 0, $1, &@1);
2963 /*% ripper: [Qnil, Qnil, $:1] %*/
2964 }
2965 ;
2966
2967%rule opt_f_block_arg(trailing) <id>
2968 : ',' f_block_arg
2969 {
2970 $$ = $2;
2971 /*% ripper: $:2 %*/
2972 }
2973 | trailing
2974 ;
2975
2976%rule def_endless_method(bodystmt) <node>
2977 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2978 {
2979 endless_method_name(p, $head->nd_mid, &@head);
2980 restore_defun(p, $head);
2981 ($$ = $head->nd_def)->nd_loc = @$;
2982 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2983 RNODE_DEFN($$)->nd_defn = $bodystmt;
2984 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2985 /*% ripper: def!($:head, $:args, $:$) %*/
2986 local_pop(p);
2987 }
2988 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2989 {
2990 endless_method_name(p, $head->nd_mid, &@head);
2991 restore_defun(p, $head);
2992 ($$ = $head->nd_def)->nd_loc = @$;
2993 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2994 RNODE_DEFS($$)->nd_defn = $bodystmt;
2995 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2996 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2997 local_pop(p);
2998 }
2999 ;
3000
3001%rule compstmt(stmts) <node>
3002 : stmts terms?
3003 {
3004 void_stmts(p, $$ = $stmts);
3005 }
3006 ;
3007
3008%rule f_opt(value) <node_opt_arg>
3009 : f_arg_asgn f_eq value
3010 {
3011 p->ctxt.in_argdef = 1;
3012 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
3013 /*% ripper: [$:$, $:3] %*/
3014 }
3015 ;
3016
3017%rule f_opt_arg(value) <node_opt_arg>
3018 : f_opt(value)
3019 {
3020 $$ = $f_opt;
3021 /*% ripper: rb_ary_new3(1, $:1) %*/
3022 }
3023 | f_opt_arg(value) ',' f_opt(value)
3024 {
3025 $$ = opt_arg_append($f_opt_arg, $f_opt);
3026 /*% ripper: rb_ary_push($:1, $:3) %*/
3027 }
3028 ;
3029
3030%rule f_kw(value) <node_kw_arg>
3031 : f_label value
3032 {
3033 p->ctxt.in_argdef = 1;
3034 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3035 /*% ripper: [$:$, $:value] %*/
3036 }
3037 | f_label
3038 {
3039 p->ctxt.in_argdef = 1;
3040 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3041 /*% ripper: [$:$, 0] %*/
3042 }
3043 ;
3044
3045%rule f_kwarg(value) <node_kw_arg>
3046 : f_kw(value)
3047 {
3048 $$ = $f_kw;
3049 /*% ripper: rb_ary_new3(1, $:1) %*/
3050 }
3051 | f_kwarg(value) ',' f_kw(value)
3052 {
3053 $$ = kwd_append($f_kwarg, $f_kw);
3054 /*% ripper: rb_ary_push($:1, $:3) %*/
3055 }
3056 ;
3057
3058%rule mlhs_items(item) <node>
3059 : item
3060 {
3061 $$ = NEW_LIST($1, &@$);
3062 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3063 }
3064 | mlhs_items(item) ',' item
3065 {
3066 $$ = list_append(p, $1, $3);
3067 /*% ripper: mlhs_add!($:1, $:3) %*/
3068 }
3069 ;
3070
3071%rule op_asgn(rhs) <node>
3072 : var_lhs tOP_ASGN lex_ctxt rhs
3073 {
3074 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3075 /*% ripper: opassign!($:var_lhs, $:tOP_ASGN, $:rhs) %*/
3076 }
3077 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3078 {
3079 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3080 /*% ripper: opassign!(aref_field!($:primary_value, $:opt_call_args), $:tOP_ASGN, $:rhs) %*/
3081 }
3082 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3083 {
3084 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3085 /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
3086 }
3087 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3088 {
3089 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3090 /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3091 }
3092 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3093 {
3094 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3095 /*% ripper: opassign!(field!($:primary_value, $:tCOLON2, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
3096 }
3097 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3098 {
3099 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3100 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3101 /*% ripper: opassign!(const_path_field!($:primary_value, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3102 }
3103 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
3104 {
3105 YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3106 $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3107 /*% ripper: opassign!(top_const_field!($:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3108 }
3109 | backref tOP_ASGN lex_ctxt rhs
3110 {
3111 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3112 $$ = NEW_ERROR(&@$);
3113 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:backref), $:tOP_ASGN, $:rhs)) %*/
3114 }
3115 ;
3116
3117%rule opt_args_tail(tail, trailing) <node_args>
3118 : ',' tail
3119 {
3120 $$ = $tail;
3121 /*% ripper: $:tail %*/
3122 }
3123 | trailing
3124 {
3125 $$ = new_empty_args_tail(p, &@$);
3126 /*% ripper: [Qnil, Qnil, Qnil] %*/
3127 }
3128 ;
3129
3130%rule range_expr(range) <node>
3131 : range tDOT2 range
3132 {
3133 value_expr(p, $1);
3134 value_expr(p, $3);
3135 $$ = NEW_DOT2($1, $3, &@$, &@2);
3136 /*% ripper: dot2!($:1, $:3) %*/
3137 }
3138 | range tDOT3 range
3139 {
3140 value_expr(p, $1);
3141 value_expr(p, $3);
3142 $$ = NEW_DOT3($1, $3, &@$, &@2);
3143 /*% ripper: dot3!($:1, $:3) %*/
3144 }
3145 | range tDOT2
3146 {
3147 value_expr(p, $1);
3148 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3149 /*% ripper: dot2!($:1, Qnil) %*/
3150 }
3151 | range tDOT3
3152 {
3153 value_expr(p, $1);
3154 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3155 /*% ripper: dot3!($:1, Qnil) %*/
3156 }
3157 | tBDOT2 range
3158 {
3159 value_expr(p, $2);
3160 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3161 /*% ripper: dot2!(Qnil, $:2) %*/
3162 }
3163 | tBDOT3 range
3164 {
3165 value_expr(p, $2);
3166 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3167 /*% ripper: dot3!(Qnil, $:2) %*/
3168 }
3169 ;
3170
3171%rule value_expr(value) <node>
3172 : value
3173 {
3174 value_expr(p, $1);
3175 $$ = $1;
3176 }
3177 ;
3178
3179%rule words(begin, word_list) <node>
3180 : begin ' '+ word_list tSTRING_END
3181 {
3182 $$ = make_list($word_list, &@$);
3183 /*% ripper: array!($:word_list) %*/
3184 }
3185 ;
3186
3187%%
3188program : {
3189 SET_LEX_STATE(EXPR_BEG);
3190 local_push(p, ifndef_ripper(1)+0);
3191 /* jumps are possible in the top-level loop. */
3192 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3193 }
3194 compstmt(top_stmts)
3195 {
3196 if ($2 && !compile_for_eval) {
3197 NODE *node = $2;
3198 /* last expression should not be void */
3199 if (nd_type_p(node, NODE_BLOCK)) {
3200 while (RNODE_BLOCK(node)->nd_next) {
3201 node = RNODE_BLOCK(node)->nd_next;
3202 }
3203 node = RNODE_BLOCK(node)->nd_head;
3204 }
3205 node = remove_begin(node);
3206 void_expr(p, node);
3207 }
3208 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$);
3209 /*% ripper[final]: program!($:2) %*/
3210 local_pop(p);
3211 }
3212 ;
3213
3214top_stmts : none
3215 {
3216 $$ = NEW_BEGIN(0, &@$);
3217 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3218 }
3219 | top_stmt
3220 {
3221 $$ = newline_node($1);
3222 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3223 }
3224 | top_stmts terms top_stmt
3225 {
3226 $$ = block_append(p, $1, newline_node($3));
3227 /*% ripper: stmts_add!($:1, $:3) %*/
3228 }
3229 ;
3230
3231top_stmt : stmt
3232 {
3233 clear_block_exit(p, true);
3234 $$ = $1;
3235 }
3236 | keyword_BEGIN begin_block
3237 {
3238 $$ = $2;
3239 /*% ripper: $:2 %*/
3240 }
3241 ;
3242
3243block_open : '{' {$$ = init_block_exit(p);};
3244
3245begin_block : block_open compstmt(top_stmts) '}'
3246 {
3247 restore_block_exit(p, $block_open);
3248 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3249 NEW_BEGIN($compstmt, &@$));
3250 $$ = NEW_BEGIN(0, &@$);
3251 /*% ripper: BEGIN!($:compstmt) %*/
3252 }
3253 ;
3254
3255bodystmt : compstmt(stmts)[body]
3256 lex_ctxt[ctxt]
3257 opt_rescue
3258 k_else
3259 {
3260 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3261 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3262 }
3263 compstmt(stmts)[elsebody]
3264 {
3265 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3266 }
3267 opt_ensure
3268 {
3269 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3270 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3271 }
3272 | compstmt(stmts)[body]
3273 lex_ctxt[ctxt]
3274 opt_rescue
3275 {
3276 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3277 }
3278 opt_ensure
3279 {
3280 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3281 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3282 }
3283 ;
3284
3285stmts : none
3286 {
3287 $$ = NEW_BEGIN(0, &@$);
3288 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3289 }
3290 | stmt_or_begin
3291 {
3292 $$ = newline_node($1);
3293 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3294 }
3295 | stmts terms stmt_or_begin
3296 {
3297 $$ = block_append(p, $1, newline_node($3));
3298 /*% ripper: stmts_add!($:1, $:3) %*/
3299 }
3300 ;
3301
3302stmt_or_begin : stmt
3303 | keyword_BEGIN
3304 {
3305 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3306 }
3307 begin_block
3308 {
3309 $$ = $3;
3310 }
3311 ;
3312
3313allow_exits : {$$ = allow_block_exit(p);};
3314
3315k_END : keyword_END lex_ctxt
3316 {
3317 if (p->ctxt.in_def) {
3318 rb_warn0("END in method; use at_exit");
3319 }
3320 $$ = $2;
3321 p->ctxt.in_rescue = before_rescue;
3322 /*% ripper: $:2 %*/
3323 };
3324
3325stmt : keyword_alias[kw] fitem[new] {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem[old]
3326 {
3327 $$ = NEW_ALIAS($new, $old, &@$, &@kw);
3328 /*% ripper: alias!($:new, $:old) %*/
3329 }
3330 | keyword_alias[kw] tGVAR[new] tGVAR[old]
3331 {
3332 $$ = NEW_VALIAS($new, $old, &@$, &@kw);
3333 /*% ripper: var_alias!($:new, $:old) %*/
3334 }
3335 | keyword_alias[kw] tGVAR[new] tBACK_REF[old]
3336 {
3337 char buf[2];
3338 buf[0] = '$';
3339 buf[1] = (char)RNODE_BACK_REF($old)->nd_nth;
3340 $$ = NEW_VALIAS($new, rb_intern2(buf, 2), &@$, &@kw);
3341 /*% ripper: var_alias!($:new, $:old) %*/
3342 }
3343 | keyword_alias tGVAR tNTH_REF[nth]
3344 {
3345 static const char mesg[] = "can't make alias for the number variables";
3346 /*%%%*/
3347 yyerror1(&@nth, mesg);
3348 /*% %*/
3349 $$ = NEW_ERROR(&@$);
3350 /*% ripper[error]: alias_error!(ERR_MESG(), $:nth) %*/
3351 }
3352 | keyword_undef[kw] undef_list[list]
3353 {
3354 nd_set_first_loc($list, @kw.beg_pos);
3355 RNODE_UNDEF($list)->keyword_loc = @kw;
3356 $$ = $list;
3357 /*% ripper: undef!($:list) %*/
3358 }
3359 | stmt[body] modifier_if[mod] expr_value[cond]
3360 {
3361 $$ = new_if(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3362 fixpos($$, $cond);
3363 /*% ripper: if_mod!($:cond, $:body) %*/
3364 }
3365 | stmt[body] modifier_unless[mod] expr_value[cond]
3366 {
3367 $$ = new_unless(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3368 fixpos($$, $cond);
3369 /*% ripper: unless_mod!($:cond, $:body) %*/
3370 }
3371 | stmt[body] modifier_while[mod] expr_value[cond_expr]
3372 {
3373 clear_block_exit(p, false);
3374 if ($body && nd_type_p($body, NODE_BEGIN)) {
3375 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3376 }
3377 else {
3378 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3379 }
3380 /*% ripper: while_mod!($:cond_expr, $:body) %*/
3381 }
3382 | stmt[body] modifier_until[mod] expr_value[cond_expr]
3383 {
3384 clear_block_exit(p, 0);
3385 if ($body && nd_type_p($body, NODE_BEGIN)) {
3386 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3387 }
3388 else {
3389 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3390 }
3391 /*% ripper: until_mod!($:cond_expr, $:body) %*/
3392 }
3393 | stmt[body] modifier_rescue[mod] after_rescue[ctxt] stmt[resbody]
3394 {
3395 p->ctxt.in_rescue = $ctxt.in_rescue;
3396 NODE *resq;
3397 YYLTYPE loc = code_loc_gen(&@mod, &@resbody);
3398 resq = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3399 $$ = NEW_RESCUE(remove_begin($body), resq, 0, &@$);
3400 /*% ripper: rescue_mod!($:body, $:resbody) %*/
3401 }
3402 | k_END[k_end] block_open[lbrace] compstmt(stmts)[body] '}'[rbrace]
3403 {
3404 clear_block_exit(p, true);
3405 restore_block_exit(p, $block_open);
3406 p->ctxt = $k_end;
3407 {
3408 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $body /* body */, NULL /* parent */, &@$);
3409 $$ = NEW_POSTEXE(scope, &@$, &@k_end, &@lbrace, &@rbrace);
3410 RNODE_SCOPE(scope)->nd_parent = $$;
3411 }
3412 /*% ripper: END!($:body) %*/
3413 }
3414 | command_asgn
3415 | mlhs[lhs] '=' lex_ctxt[ctxt] command_call_value[rhs]
3416 {
3417 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3418 /*% ripper: massign!($:lhs, $:rhs) %*/
3419 }
3420 | asgn(mrhs)
3421 | mlhs[lhs] '=' lex_ctxt[lex_ctxt] mrhs_arg[mrhs_arg] modifier_rescue[modifier_rescue]
3422 after_rescue[after_rescue] stmt[resbody]
3423 {
3424 p->ctxt.in_rescue = $after_rescue.in_rescue;
3425 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3426 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3427 loc.beg_pos = @mrhs_arg.beg_pos;
3428 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3429 $$ = node_assign(p, (NODE *)$lhs, $mrhs_arg, $lex_ctxt, &@$);
3430 /*% ripper: massign!($:lhs, rescue_mod!($:mrhs_arg, $:resbody)) %*/
3431 }
3432 | mlhs[lhs] '=' lex_ctxt[ctxt] mrhs_arg[rhs]
3433 {
3434 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3435 /*% ripper: massign!($:lhs, $:rhs) %*/
3436 }
3437 | expr
3438 | error
3439 {
3440 (void)yynerrs;
3441 $$ = NEW_ERROR(&@$);
3442 }
3443 ;
3444
3445command_asgn : asgn(command_rhs)
3446 | op_asgn(command_rhs)
3447 | def_endless_method(endless_command)
3448 ;
3449
3450endless_command : command
3451 | endless_command modifier_rescue after_rescue arg
3452 {
3453 p->ctxt.in_rescue = $3.in_rescue;
3454 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3455 /*% ripper: rescue_mod!($:1, $:4) %*/
3456 }
3457 | keyword_not '\n'? endless_command
3458 {
3459 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3460 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3461 }
3462 ;
3463
3464command_rhs : command_call_value %prec tOP_ASGN
3465 | command_call_value modifier_rescue after_rescue stmt
3466 {
3467 p->ctxt.in_rescue = $3.in_rescue;
3468 YYLTYPE loc = code_loc_gen(&@2, &@4);
3469 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3470 /*% ripper: rescue_mod!($:1, $:4) %*/
3471 }
3472 | command_asgn
3473 ;
3474
3475expr : command_call
3476 | expr[left] keyword_and[op] expr[right]
3477 {
3478 $$ = logop(p, idAND, $left, $right, &@op, &@$);
3479 /*% ripper: binary!($:left, ID2VAL(idAND), $:right) %*/
3480 }
3481 | expr[left] keyword_or[op] expr[right]
3482 {
3483 $$ = logop(p, idOR, $left, $right, &@op, &@$);
3484 /*% ripper: binary!($:left, ID2VAL(idOR), $:right) %*/
3485 }
3486 | keyword_not[not] '\n'? expr[arg]
3487 {
3488 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@not, &@$);
3489 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
3490 }
3491 | '!'[not] command_call[arg]
3492 {
3493 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), '!', &@not, &@$);
3494 /*% ripper: unary!(ID2VAL('\'!\''), $:arg) %*/
3495 }
3496 | arg tASSOC[assoc]
3497 {
3498 value_expr(p, $arg);
3499 }
3500 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3501 p_top_expr_body[body]
3502 {
3503 pop_pktbl(p, $p_pktbl);
3504 pop_pvtbl(p, $p_pvtbl);
3505 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3506 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3507 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3508 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@assoc), &@$, &NULL_LOC, &NULL_LOC);
3509 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3510 }
3511 | arg keyword_in
3512 {
3513 value_expr(p, $arg);
3514 }
3515 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3516 p_top_expr_body[body]
3517 {
3518 pop_pktbl(p, $p_pktbl);
3519 pop_pvtbl(p, $p_pvtbl);
3520 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3521 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3522 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3523 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body, &@keyword_in, &NULL_LOC, &NULL_LOC), &@$, &NULL_LOC, &NULL_LOC);
3524 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3525 }
3526 | arg %prec tLBRACE_ARG
3527 ;
3528
3529def_name : fname
3530 {
3531 numparam_name(p, $fname);
3532 local_push(p, 0);
3533 p->ctxt.in_def = 1;
3534 p->ctxt.in_rescue = before_rescue;
3535 p->ctxt.cant_return = 0;
3536 $$ = $fname;
3537 }
3538 ;
3539
3540defn_head : k_def def_name
3541 {
3542 $$ = def_head_save(p, $k_def);
3543 $$->nd_mid = $def_name;
3544 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3545 /*% ripper: $:def_name %*/
3546 }
3547 ;
3548
3549defs_head : k_def singleton dot_or_colon
3550 {
3551 SET_LEX_STATE(EXPR_FNAME);
3552 }
3553 def_name
3554 {
3555 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3556 $$ = def_head_save(p, $k_def);
3557 $$->nd_mid = $def_name;
3558 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3559 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3560 }
3561 ;
3562
3563expr_value : value_expr(expr)
3564 | error
3565 {
3566 $$ = NEW_ERROR(&@$);
3567 }
3568 ;
3569
3570expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3571 {
3572 $$ = $2;
3573 /*% ripper: $:2 %*/
3574 }
3575 ;
3576
3577command_call : command
3578 | block_command
3579 ;
3580
3581command_call_value : value_expr(command_call)
3582 ;
3583
3584block_command : block_call
3585 | block_call call_op2 operation2 command_args
3586 {
3587 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3588 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3589 }
3590 ;
3591
3592cmd_brace_block : tLBRACE_ARG brace_body '}'
3593 {
3594 $$ = $2;
3595 set_embraced_location($$, &@1, &@3);
3596 /*% ripper: $:2 %*/
3597 }
3598 ;
3599
3600fcall : operation
3601 {
3602 $$ = NEW_FCALL($1, 0, &@$);
3603 /*% ripper: $:1 %*/
3604 }
3605 ;
3606
3607command : fcall command_args %prec tLOWEST
3608 {
3609 $1->nd_args = $2;
3610 nd_set_last_loc($1, @2.end_pos);
3611 $$ = (NODE *)$1;
3612 /*% ripper: command!($:1, $:2) %*/
3613 }
3614 | fcall command_args cmd_brace_block
3615 {
3616 block_dup_check(p, $2, $3);
3617 $1->nd_args = $2;
3618 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3619 fixpos($$, RNODE($1));
3620 nd_set_last_loc($1, @2.end_pos);
3621 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3622 }
3623 | primary_value call_op operation2 command_args %prec tLOWEST
3624 {
3625 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3626 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3627 }
3628 | primary_value call_op operation2 command_args cmd_brace_block
3629 {
3630 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3631 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3632 }
3633 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3634 {
3635 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3636 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3637 }
3638 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3639 {
3640 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3641 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3642 }
3643 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3644 {
3645 set_embraced_location($5, &@4, &@6);
3646 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3647 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3648 }
3649 | keyword_super command_args
3650 {
3651 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3652 fixpos($$, $2);
3653 /*% ripper: super!($:2) %*/
3654 }
3655 | k_yield command_args
3656 {
3657 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3658 fixpos($$, $2);
3659 /*% ripper: yield!($:2) %*/
3660 }
3661 | k_return_arg call_args
3662 {
3663 CMDARG_POP();
3664 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3665 /*% ripper: return!($:2) %*/
3666 }
3667 | k_break_arg call_args
3668 {
3669 CMDARG_POP();
3670 NODE *args = 0;
3671 args = ret_args(p, $2);
3672 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3673 /*% ripper: break!($:2) %*/
3674 }
3675 | k_next_arg call_args
3676 {
3677 CMDARG_POP();
3678 NODE *args = 0;
3679 args = ret_args(p, $2);
3680 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3681 /*% ripper: next!($:2) %*/
3682 }
3683 ;
3684
3685mlhs : mlhs_basic
3686 | tLPAREN mlhs_inner rparen
3687 {
3688 $$ = $2;
3689 /*% ripper: mlhs_paren!($:2) %*/
3690 }
3691 ;
3692
3693mlhs_inner : mlhs_basic
3694 | tLPAREN mlhs_inner rparen
3695 {
3696 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3697 /*% ripper: mlhs_paren!($:2) %*/
3698 }
3699 ;
3700
3701mlhs_basic : mlhs_head
3702 {
3703 $$ = NEW_MASGN($1, 0, &@$);
3704 /*% ripper: $:1 %*/
3705 }
3706 | mlhs_head mlhs_item
3707 {
3708 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3709 /*% ripper: mlhs_add!($:1, $:2) %*/
3710 }
3711 | mlhs_head tSTAR mlhs_node
3712 {
3713 $$ = NEW_MASGN($1, $3, &@$);
3714 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3715 }
3716 | mlhs_head tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3717 {
3718 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3719 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3720 }
3721 | mlhs_head tSTAR
3722 {
3723 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3724 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3725 }
3726 | mlhs_head tSTAR ',' mlhs_items(mlhs_item)
3727 {
3728 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3729 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3730 }
3731 | tSTAR mlhs_node
3732 {
3733 $$ = NEW_MASGN(0, $2, &@$);
3734 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3735 }
3736 | tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3737 {
3738 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3739 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3740 }
3741 | tSTAR
3742 {
3743 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3744 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3745 }
3746 | tSTAR ',' mlhs_items(mlhs_item)
3747 {
3748 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3749 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3750 }
3751 ;
3752
3753mlhs_item : mlhs_node
3754 | tLPAREN mlhs_inner rparen
3755 {
3756 $$ = (NODE *)$2;
3757 /*% ripper: mlhs_paren!($:2) %*/
3758 }
3759 ;
3760
3761mlhs_head : mlhs_item ','
3762 {
3763 $$ = NEW_LIST($1, &@1);
3764 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3765 }
3766 | mlhs_head mlhs_item ','
3767 {
3768 $$ = list_append(p, $1, $2);
3769 /*% ripper: mlhs_add!($:1, $:2) %*/
3770 }
3771 ;
3772
3773
3774mlhs_node : user_or_keyword_variable
3775 {
3776 /*% ripper: var_field!($:1) %*/
3777 $$ = assignable(p, $1, 0, &@$);
3778 }
3779 | primary_value '[' opt_call_args rbracket
3780 {
3781 $$ = aryset(p, $1, $3, &@$);
3782 /*% ripper: aref_field!($:1, $:3) %*/
3783 }
3784 | primary_value call_op ident_or_const
3785 {
3786 anddot_multiple_assignment_check(p, &@2, $2);
3787 $$ = attrset(p, $1, $2, $3, &@$);
3788 /*% ripper: field!($:1, $:2, $:3) %*/
3789 }
3790 | primary_value tCOLON2 tIDENTIFIER
3791 {
3792 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3793 /*% ripper: const_path_field!($:1, $:3) %*/
3794 }
3795 | primary_value tCOLON2 tCONSTANT
3796 {
3797 /*% ripper: const_path_field!($:1, $:3) %*/
3798 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3799 }
3800 | tCOLON3 tCONSTANT
3801 {
3802 /*% ripper: top_const_field!($:2) %*/
3803 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3804 }
3805 | backref
3806 {
3807 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3808 $$ = NEW_ERROR(&@$);
3809 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3810 }
3811 ;
3812
3813lhs : user_or_keyword_variable
3814 {
3815 /*% ripper: var_field!($:1) %*/
3816 $$ = assignable(p, $1, 0, &@$);
3817 }
3818 | primary_value '[' opt_call_args rbracket
3819 {
3820 $$ = aryset(p, $1, $3, &@$);
3821 /*% ripper: aref_field!($:1, $:3) %*/
3822 }
3823 | primary_value call_op ident_or_const
3824 {
3825 $$ = attrset(p, $1, $2, $3, &@$);
3826 /*% ripper: field!($:1, $:2, $:3) %*/
3827 }
3828 | primary_value tCOLON2 tIDENTIFIER
3829 {
3830 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3831 /*% ripper: field!($:1, $:2, $:3) %*/
3832 }
3833 | primary_value tCOLON2 tCONSTANT
3834 {
3835 /*% ripper: const_path_field!($:1, $:3) %*/
3836 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3837 }
3838 | tCOLON3 tCONSTANT
3839 {
3840 /*% ripper: top_const_field!($:2) %*/
3841 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3842 }
3843 | backref
3844 {
3845 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3846 $$ = NEW_ERROR(&@$);
3847 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3848 }
3849 ;
3850
3851cname : tIDENTIFIER
3852 {
3853 static const char mesg[] = "class/module name must be CONSTANT";
3854 /*%%%*/
3855 yyerror1(&@1, mesg);
3856 /*% %*/
3857 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3858 }
3859 | tCONSTANT
3860 ;
3861
3862cpath : tCOLON3 cname
3863 {
3864 $$ = NEW_COLON3($2, &@$, &@1, &@2);
3865 /*% ripper: top_const_ref!($:2) %*/
3866 }
3867 | cname
3868 {
3869 $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
3870 /*% ripper: const_ref!($:1) %*/
3871 }
3872 | primary_value tCOLON2 cname
3873 {
3874 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
3875 /*% ripper: const_path_ref!($:1, $:3) %*/
3876 }
3877 ;
3878
3879fname : operation
3880 | op
3881 {
3882 SET_LEX_STATE(EXPR_ENDFN);
3883 $$ = $1;
3884 }
3885 | reswords
3886 ;
3887
3888fitem : fname
3889 {
3890 $$ = NEW_SYM(rb_id2str($1), &@$);
3891 /*% ripper: symbol_literal!($:1) %*/
3892 }
3893 | symbol
3894 ;
3895
3896undef_list : fitem
3897 {
3898 $$ = NEW_UNDEF($1, &@$);
3899 /*% ripper: rb_ary_new3(1, $:1) %*/
3900 }
3901 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3902 {
3903 nd_set_last_loc($1, @4.end_pos);
3904 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3905 /*% ripper: rb_ary_push($:1, $:4) %*/
3906 }
3907 ;
3908
3909op : '|' { $$ = '|'; }
3910 | '^' { $$ = '^'; }
3911 | '&' { $$ = '&'; }
3912 | tCMP { $$ = tCMP; }
3913 | tEQ { $$ = tEQ; }
3914 | tEQQ { $$ = tEQQ; }
3915 | tMATCH { $$ = tMATCH; }
3916 | tNMATCH { $$ = tNMATCH; }
3917 | '>' { $$ = '>'; }
3918 | tGEQ { $$ = tGEQ; }
3919 | '<' { $$ = '<'; }
3920 | tLEQ { $$ = tLEQ; }
3921 | tNEQ { $$ = tNEQ; }
3922 | tLSHFT { $$ = tLSHFT; }
3923 | tRSHFT { $$ = tRSHFT; }
3924 | '+' { $$ = '+'; }
3925 | '-' { $$ = '-'; }
3926 | '*' { $$ = '*'; }
3927 | tSTAR { $$ = '*'; }
3928 | '/' { $$ = '/'; }
3929 | '%' { $$ = '%'; }
3930 | tPOW { $$ = tPOW; }
3931 | tDSTAR { $$ = tDSTAR; }
3932 | '!' { $$ = '!'; }
3933 | '~' { $$ = '~'; }
3934 | tUPLUS { $$ = tUPLUS; }
3935 | tUMINUS { $$ = tUMINUS; }
3936 | tAREF { $$ = tAREF; }
3937 | tASET { $$ = tASET; }
3938 | '`' { $$ = '`'; }
3939 ;
3940
3941reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3942 | keyword_BEGIN | keyword_END
3943 | keyword_alias | keyword_and | keyword_begin
3944 | keyword_break | keyword_case | keyword_class | keyword_def
3945 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3946 | keyword_end | keyword_ensure | keyword_false
3947 | keyword_for | keyword_in | keyword_module | keyword_next
3948 | keyword_nil | keyword_not | keyword_or | keyword_redo
3949 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3950 | keyword_super | keyword_then | keyword_true | keyword_undef
3951 | keyword_when | keyword_yield | keyword_if | keyword_unless
3952 | keyword_while | keyword_until
3953 ;
3954
3955arg : asgn(arg_rhs)
3956 | op_asgn(arg_rhs)
3957 | range_expr(arg)
3958 | arg '+' arg
3959 {
3960 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3961 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3962 }
3963 | arg '-' arg
3964 {
3965 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3966 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3967 }
3968 | arg '*' arg
3969 {
3970 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3971 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3972 }
3973 | arg '/' arg
3974 {
3975 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3976 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3977 }
3978 | arg '%' arg
3979 {
3980 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3981 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3982 }
3983 | arg tPOW arg
3984 {
3985 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3986 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3987 }
3988 | tUMINUS_NUM simple_numeric tPOW arg
3989 {
3990 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3991 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3992 }
3993 | tUPLUS arg
3994 {
3995 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3996 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3997 }
3998 | tUMINUS arg
3999 {
4000 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
4001 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
4002 }
4003 | arg '|' arg
4004 {
4005 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
4006 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
4007 }
4008 | arg '^' arg
4009 {
4010 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
4011 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
4012 }
4013 | arg '&' arg
4014 {
4015 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
4016 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
4017 }
4018 | arg tCMP arg
4019 {
4020 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
4021 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
4022 }
4023 | rel_expr %prec tCMP
4024 | arg tEQ arg
4025 {
4026 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
4027 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
4028 }
4029 | arg tEQQ arg
4030 {
4031 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
4032 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
4033 }
4034 | arg tNEQ arg
4035 {
4036 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4037 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4038 }
4039 | arg tMATCH arg
4040 {
4041 $$ = match_op(p, $1, $3, &@2, &@$);
4042 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4043 }
4044 | arg tNMATCH arg
4045 {
4046 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4047 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4048 }
4049 | '!' arg
4050 {
4051 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4052 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4053 }
4054 | '~' arg
4055 {
4056 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4057 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4058 }
4059 | arg tLSHFT arg
4060 {
4061 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4062 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4063 }
4064 | arg tRSHFT arg
4065 {
4066 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4067 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4068 }
4069 | arg tANDOP arg
4070 {
4071 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4072 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4073 }
4074 | arg tOROP arg
4075 {
4076 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4077 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4078 }
4079 | keyword_defined '\n'? begin_defined arg
4080 {
4081 p->ctxt.in_defined = $3.in_defined;
4082 $$ = new_defined(p, $4, &@$, &@1);
4083 p->ctxt.has_trailing_semicolon = $3.has_trailing_semicolon;
4084 /*% ripper: defined!($:4) %*/
4085 }
4086 | def_endless_method(endless_arg)
4087 | ternary
4088 | primary
4089 ;
4090
4091ternary : arg '?' arg '\n'? ':' arg
4092 {
4093 value_expr(p, $1);
4094 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4095 fixpos($$, $1);
4096 /*% ripper: ifop!($:1, $:3, $:6) %*/
4097 }
4098 ;
4099
4100endless_arg : arg %prec modifier_rescue
4101 | endless_arg modifier_rescue after_rescue arg
4102 {
4103 p->ctxt.in_rescue = $3.in_rescue;
4104 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4105 /*% ripper: rescue_mod!($:1, $:4) %*/
4106 }
4107 | keyword_not '\n'? endless_arg
4108 {
4109 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4110 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4111 }
4112 ;
4113
4114relop : '>' {$$ = '>';}
4115 | '<' {$$ = '<';}
4116 | tGEQ {$$ = idGE;}
4117 | tLEQ {$$ = idLE;}
4118 ;
4119
4120rel_expr : arg relop arg %prec '>'
4121 {
4122 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4123 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4124 }
4125 | rel_expr relop arg %prec '>'
4126 {
4127 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4128 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4129 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4130 }
4131 ;
4132
4133lex_ctxt : none
4134 {
4135 $$ = p->ctxt;
4136 }
4137 ;
4138
4139begin_defined : lex_ctxt
4140 {
4141 p->ctxt.in_defined = 1;
4142 $$ = $1;
4143 }
4144 ;
4145
4146after_rescue : lex_ctxt
4147 {
4148 p->ctxt.in_rescue = after_rescue;
4149 $$ = $1;
4150 }
4151 ;
4152
4153arg_value : value_expr(arg)
4154 ;
4155
4156aref_args : none
4157 | args trailer
4158 | args ',' assocs trailer
4159 {
4160 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4161 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4162 }
4163 | assocs trailer
4164 {
4165 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4166 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4167 }
4168 ;
4169
4170arg_rhs : arg %prec tOP_ASGN
4171 {
4172 value_expr(p, $1);
4173 $$ = $1;
4174 }
4175 | arg modifier_rescue after_rescue arg
4176 {
4177 p->ctxt.in_rescue = $3.in_rescue;
4178 value_expr(p, $1);
4179 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4180 /*% ripper: rescue_mod!($:1, $:4) %*/
4181 }
4182 ;
4183
4184paren_args : '(' opt_call_args rparen
4185 {
4186 $$ = $2;
4187 /*% ripper: arg_paren!($:2) %*/
4188 }
4189 | '(' args ',' args_forward rparen
4190 {
4191 if (!check_forwarding_args(p)) {
4192 $$ = 0;
4193 }
4194 else {
4195 $$ = new_args_forward_call(p, $2, &@4, &@$);
4196 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4197 }
4198 }
4199 | '(' args_forward rparen
4200 {
4201 if (!check_forwarding_args(p)) {
4202 $$ = 0;
4203 }
4204 else {
4205 $$ = new_args_forward_call(p, 0, &@2, &@$);
4206 /*% ripper: arg_paren!($:2) %*/
4207 }
4208 }
4209 ;
4210
4211opt_paren_args : none
4212 | paren_args
4213 {
4214 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4215 }
4216 ;
4217
4218opt_call_args : none
4219 | call_args
4220 | args ','
4221 | args ',' assocs ','
4222 {
4223 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4224 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4225 }
4226 | assocs ','
4227 {
4228 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4229 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4230 }
4231 ;
4232
4233call_args : value_expr(command)
4234 {
4235 $$ = NEW_LIST($1, &@$);
4236 /*% ripper: args_add!(args_new!, $:1) %*/
4237 }
4238 | def_endless_method(endless_command)
4239 {
4240 $$ = NEW_LIST($1, &@$);
4241 /*% ripper: args_add!(args_new!, $:1) %*/
4242 }
4243 | args opt_block_arg
4244 {
4245 $$ = arg_blk_pass($1, $2);
4246 /*% ripper: args_add_block!($:1, $:2) %*/
4247 }
4248 | assocs opt_block_arg
4249 {
4250 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4251 $$ = arg_blk_pass($$, $2);
4252 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4253 }
4254 | args ',' assocs opt_block_arg
4255 {
4256 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4257 $$ = arg_blk_pass($$, $4);
4258 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4259 }
4260 | block_arg
4261 /*% ripper: args_add_block!(args_new!, $:1) %*/
4262 ;
4263
4264command_args : {
4265 /* If call_args starts with a open paren '(' or '[',
4266 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4267 * but the push must be done after CMDARG_PUSH(1).
4268 * So this code makes them consistent by first cancelling
4269 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4270 * and finally redoing CMDARG_PUSH(0).
4271 */
4272 int lookahead = 0;
4273 switch (yychar) {
4274 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4275 lookahead = 1;
4276 }
4277 if (lookahead) CMDARG_POP();
4278 CMDARG_PUSH(1);
4279 if (lookahead) CMDARG_PUSH(0);
4280 }
4281 call_args
4282 {
4283 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4284 * but the push must be done after CMDARG_POP() in the parser.
4285 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4286 * CMDARG_POP() to pop 1 pushed by command_args,
4287 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4288 */
4289 int lookahead = 0;
4290 switch (yychar) {
4291 case tLBRACE_ARG:
4292 lookahead = 1;
4293 }
4294 if (lookahead) CMDARG_POP();
4295 CMDARG_POP();
4296 if (lookahead) CMDARG_PUSH(0);
4297 $$ = $2;
4298 /*% ripper: $:2 %*/
4299 }
4300 ;
4301
4302block_arg : tAMPER arg_value
4303 {
4304 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4305 /*% ripper: $:2 %*/
4306 }
4307 | tAMPER
4308 {
4309 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4310 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4311 /*% ripper: Qnil %*/
4312 }
4313 ;
4314
4315opt_block_arg : ',' block_arg
4316 {
4317 $$ = $2;
4318 /*% ripper: $:2 %*/
4319 }
4320 | none
4321 {
4322 $$ = 0;
4323 /*% ripper: Qfalse %*/
4324 }
4325 ;
4326
4327/* value */
4328args : arg_value
4329 {
4330 $$ = NEW_LIST($arg_value, &@$);
4331 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4332 }
4333 | arg_splat
4334 {
4335 $$ = $arg_splat;
4336 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4337 }
4338 | args[non_last_args] ',' arg_value
4339 {
4340 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4341 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4342 }
4343 | args[non_last_args] ',' arg_splat
4344 {
4345 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4346 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4347 }
4348 ;
4349
4350/* value */
4351arg_splat : tSTAR arg_value
4352 {
4353 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4354 /*% ripper: $:arg_value %*/
4355 }
4356 | tSTAR /* none */
4357 {
4358 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4359 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4360 /*% ripper: Qnil %*/
4361 }
4362 ;
4363
4364/* value */
4365mrhs_arg : mrhs
4366 | arg_value
4367 ;
4368
4369/* value */
4370mrhs : args ',' arg_value
4371 {
4372 $$ = last_arg_append(p, $args, $arg_value, &@$);
4373 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4374 }
4375 | args ',' tSTAR arg_value
4376 {
4377 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4378 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4379 }
4380 | tSTAR arg_value
4381 {
4382 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4383 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4384 }
4385 ;
4386
4387%rule %inline inline_primary
4388 : literal
4389 | strings
4390 | xstring
4391 | regexp
4392 | words
4393 | qwords
4394 | symbols
4395 | qsymbols
4396 ;
4397
4398primary : inline_primary
4399 | var_ref
4400 | backref
4401 | tFID[fid]
4402 {
4403 $$ = (NODE *)NEW_FCALL($fid, 0, &@$);
4404 /*% ripper: method_add_arg!(fcall!($:fid), args_new!) %*/
4405 }
4406 | k_begin[kw]
4407 {
4408 CMDARG_PUSH(0);
4409 }
4410 bodystmt[body]
4411 k_end[k_end]
4412 {
4413 CMDARG_POP();
4414 set_line_body($body, @kw.end_pos.lineno);
4415 $$ = NEW_BEGIN($body, &@$);
4416 nd_set_line($$, @kw.end_pos.lineno);
4417 /*% ripper: begin!($:body) %*/
4418 }
4419 | tLPAREN_ARG compstmt(stmts)[body] {SET_LEX_STATE(EXPR_ENDARG);} ')'
4420 {
4421 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4422 $$ = $body;
4423 /*% ripper: paren!($:body) %*/
4424 }
4425 | tLPAREN compstmt(stmts)[body] ')'
4426 {
4427 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4428 $$ = NEW_BLOCK($body, &@$);
4429 /*% ripper: paren!($:body) %*/
4430 }
4431 | primary_value[recv] tCOLON2[op] tCONSTANT[name]
4432 {
4433 $$ = NEW_COLON2($recv, $name, &@$, &@op, &@name);
4434 /*% ripper: const_path_ref!($:recv, $:name) %*/
4435 }
4436 | tCOLON3[top] tCONSTANT[name]
4437 {
4438 $$ = NEW_COLON3($name, &@$, &@top, &@name);
4439 /*% ripper: top_const_ref!($:name) %*/
4440 }
4441 | tLBRACK aref_args[args] ']'
4442 {
4443 $$ = make_list($args, &@$);
4444 /*% ripper: array!($:args) %*/
4445 }
4446 | tLBRACE assoc_list[list] '}'
4447 {
4448 $$ = new_hash(p, $list, &@$);
4449 RNODE_HASH($$)->nd_brace = TRUE;
4450 /*% ripper: hash!($:list) %*/
4451 }
4452 | k_return[kw]
4453 {
4454 $$ = NEW_RETURN(0, &@$, &@kw);
4455 /*% ripper: return0! %*/
4456 }
4457 | k_yield[kw] '('[lpar] call_args[args] rparen[rpar]
4458 {
4459 $$ = NEW_YIELD($args, &@$, &@kw, &@lpar, &@rpar);
4460 /*% ripper: yield!(paren!($:args)) %*/
4461 }
4462 | k_yield[kw] '('[lpar] rparen[rpar]
4463 {
4464 $$ = NEW_YIELD(0, &@$, &@kw, &@lpar, &@rpar);
4465 /*% ripper: yield!(paren!(args_new!)) %*/
4466 }
4467 | k_yield[kw]
4468 {
4469 $$ = NEW_YIELD(0, &@$, &@kw, &NULL_LOC, &NULL_LOC);
4470 /*% ripper: yield0! %*/
4471 }
4472 | keyword_defined[kw] '\n'? '(' begin_defined[ctxt] expr[arg] rparen
4473 {
4474 p->ctxt.in_defined = $ctxt.in_defined;
4475 $$ = new_defined(p, $arg, &@$, &@kw);
4476 p->ctxt.has_trailing_semicolon = $ctxt.has_trailing_semicolon;
4477 /*% ripper: defined!($:arg) %*/
4478 }
4479 | keyword_not[kw] '(' expr[arg] rparen
4480 {
4481 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@kw, &@$);
4482 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
4483 }
4484 | keyword_not[kw] '('[lpar] rparen
4485 {
4486 $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@lpar), &@lpar), METHOD_NOT, &@kw, &@$);
4487 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4488 }
4489 | fcall[call] brace_block[block]
4490 {
4491 $$ = method_add_block(p, (NODE *)$call, $block, &@$);
4492 /*% ripper: method_add_block!(method_add_arg!(fcall!($:call), args_new!), $:block) %*/
4493 }
4494 | method_call
4495 | method_call[call] brace_block[block]
4496 {
4497 block_dup_check(p, get_nd_args(p, $call), $block);
4498 $$ = method_add_block(p, $call, $block, &@$);
4499 /*% ripper: method_add_block!($:call, $:block) %*/
4500 }
4501 | lambda
4502 | k_if[kw] expr_value[cond] then[then]
4503 compstmt(stmts)[body]
4504 if_tail[tail]
4505 k_end[k_end]
4506 {
4507 if ($tail && nd_type_p($tail, NODE_IF))
4508 RNODE_IF($tail)->end_keyword_loc = @k_end;
4509
4510 $$ = new_if(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4511 fixpos($$, $cond);
4512 /*% ripper: if!($:cond, $:body, $:tail) %*/
4513 }
4514 | k_unless[kw] expr_value[cond] then[then]
4515 compstmt(stmts)[body]
4516 opt_else[tail]
4517 k_end[k_end]
4518 {
4519 $$ = new_unless(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4520 fixpos($$, $cond);
4521 /*% ripper: unless!($:cond, $:body, $:tail) %*/
4522 }
4523 | k_while[kw] expr_value_do[cond]
4524 compstmt(stmts)[body]
4525 k_end[k_end]
4526 {
4527 restore_block_exit(p, $kw);
4528 $$ = NEW_WHILE(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4529 fixpos($$, $cond);
4530 /*% ripper: while!($:cond, $:body) %*/
4531 }
4532 | k_until[kw] expr_value_do[cond]
4533 compstmt(stmts)[body]
4534 k_end[k_end]
4535 {
4536 restore_block_exit(p, $kw);
4537 $$ = NEW_UNTIL(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4538 fixpos($$, $cond);
4539 /*% ripper: until!($:cond, $:body) %*/
4540 }
4541 | k_case[k_case] expr_value[expr] terms?
4542 {
4543 $$ = p->case_labels;
4544 p->case_labels = CHECK_LITERAL_WHEN;
4545 }[labels]<labels>
4546 case_body[body]
4547 k_end[k_end]
4548 {
4549 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4550 p->case_labels = $labels;
4551 $$ = NEW_CASE($expr, $body, &@$, &@k_case, &@k_end);
4552 fixpos($$, $expr);
4553 /*% ripper: case!($:expr, $:body) %*/
4554 }
4555 | k_case[k_case] terms?
4556 {
4557 $$ = p->case_labels;
4558 p->case_labels = 0;
4559 }[labels]<labels>
4560 case_body[body]
4561 k_end[k_end]
4562 {
4563 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4564 p->case_labels = $labels;
4565 $$ = NEW_CASE2($body, &@$, &@k_case, &@k_end);
4566 /*% ripper: case!(Qnil, $:body) %*/
4567 }
4568 | k_case[k_case] expr_value[expr] terms?
4569 p_case_body[body]
4570 k_end[k_end]
4571 {
4572 $$ = NEW_CASE3($expr, $body, &@$, &@k_case, &@k_end);
4573 /*% ripper: case!($:expr, $:body) %*/
4574 }
4575 | k_for[k_for] for_var[for_var] keyword_in[keyword_in]
4576 {COND_PUSH(1);} expr_value[expr_value] do[do] {COND_POP();}
4577 compstmt(stmts)[compstmt]
4578 k_end[k_end]
4579 {
4580 restore_block_exit(p, $k_for);
4581 /*
4582 * for a, b, c in e
4583 * #=>
4584 * e.each{|*x| a, b, c = x}
4585 *
4586 * for a in e
4587 * #=>
4588 * e.each{|x| a, = x}
4589 */
4590 ID id = internal_id(p);
4591 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4592 rb_node_args_t *args;
4593 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4594 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4595 tbl->ids[0] = id; /* internal id */
4596
4597 switch (nd_type($for_var)) {
4598 case NODE_LASGN:
4599 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4600 set_nd_value(p, $for_var, internal_var);
4601 id = 0;
4602 m->nd_plen = 1;
4603 m->nd_next = $for_var;
4604 break;
4605 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4606 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4607 break;
4608 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4609 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);
4610 }
4611 /* {|*internal_id| <m> = internal_id; ... } */
4612 args = new_args(p, m, 0, id, 0, new_empty_args_tail(p, &@for_var), &@for_var);
4613 scope = NEW_SCOPE2(tbl, args, $compstmt, NULL, &@$);
4614 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4615 $$ = NEW_FOR($expr_value, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4616 RNODE_SCOPE(scope)->nd_parent = $$;
4617 fixpos($$, $for_var);
4618 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4619 }
4620 | k_class cpath superclass
4621 {
4622 begin_definition("class", &@k_class, &@cpath);
4623 }
4624 bodystmt
4625 k_end
4626 {
4627 YYLTYPE inheritance_operator_loc = NULL_LOC;
4628 if ($superclass) {
4629 inheritance_operator_loc = @superclass;
4630 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
4631 }
4632 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
4633 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4634 set_line_body($bodystmt, @superclass.end_pos.lineno);
4635 nd_set_line($$, @superclass.end_pos.lineno);
4636 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4637 local_pop(p);
4638 p->ctxt.in_class = $k_class.in_class;
4639 p->ctxt.cant_return = $k_class.cant_return;
4640 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4641 p->ctxt.in_rescue = $k_class.in_rescue;
4642 }
4643 | k_class tLSHFT expr_value
4644 {
4645 begin_definition("", &@k_class, &@tLSHFT);
4646 }
4647 term
4648 bodystmt
4649 k_end
4650 {
4651 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$, &@k_class, &@tLSHFT, &@k_end);
4652 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4653 set_line_body($bodystmt, nd_line($expr_value));
4654 fixpos($$, $expr_value);
4655 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4656 local_pop(p);
4657 p->ctxt.in_def = $k_class.in_def;
4658 p->ctxt.in_class = $k_class.in_class;
4659 p->ctxt.cant_return = $k_class.cant_return;
4660 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4661 p->ctxt.in_rescue = $k_class.in_rescue;
4662 }
4663 | k_module cpath
4664 {
4665 begin_definition("module", &@k_module, &@cpath);
4666 }
4667 bodystmt
4668 k_end
4669 {
4670 $$ = NEW_MODULE($cpath, $bodystmt, &@$, &@k_module, &@k_end);
4671 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4672 set_line_body($bodystmt, @cpath.end_pos.lineno);
4673 nd_set_line($$, @cpath.end_pos.lineno);
4674 /*% ripper: module!($:cpath, $:bodystmt) %*/
4675 local_pop(p);
4676 p->ctxt.in_class = $k_module.in_class;
4677 p->ctxt.cant_return = $k_module.cant_return;
4678 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4679 p->ctxt.in_rescue = $k_module.in_rescue;
4680 }
4681 | defn_head[head]
4682 f_arglist[args]
4683 {
4684 push_end_expect_token_locations(p, &@head.beg_pos);
4685 }
4686 bodystmt
4687 k_end
4688 {
4689 restore_defun(p, $head);
4690 ($$ = $head->nd_def)->nd_loc = @$;
4691 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4692 RNODE_DEFN($$)->nd_defn = $bodystmt;
4693 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4694 local_pop(p);
4695 }
4696 | defs_head[head]
4697 f_arglist[args]
4698 {
4699 push_end_expect_token_locations(p, &@head.beg_pos);
4700 }
4701 bodystmt
4702 k_end
4703 {
4704 restore_defun(p, $head);
4705 ($$ = $head->nd_def)->nd_loc = @$;
4706 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4707 RNODE_DEFS($$)->nd_defn = $bodystmt;
4708 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4709 local_pop(p);
4710 }
4711 | keyword_break[kw]
4712 {
4713 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@kw));
4714 /*% ripper: break!(args_new!) %*/
4715 }
4716 | keyword_next[kw]
4717 {
4718 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@kw));
4719 /*% ripper: next!(args_new!) %*/
4720 }
4721 | keyword_redo[kw]
4722 {
4723 $$ = add_block_exit(p, NEW_REDO(&@$, &@kw));
4724 /*% ripper: redo! %*/
4725 }
4726 | keyword_retry[kw]
4727 {
4728 if (!p->ctxt.in_defined) {
4729 switch (p->ctxt.in_rescue) {
4730 case before_rescue: yyerror1(&@kw, "Invalid retry without rescue"); break;
4731 case after_rescue: /* ok */ break;
4732 case after_else: yyerror1(&@kw, "Invalid retry after else"); break;
4733 case after_ensure: yyerror1(&@kw, "Invalid retry after ensure"); break;
4734 }
4735 }
4736 $$ = NEW_RETRY(&@$);
4737 /*% ripper: retry! %*/
4738 }
4739 ;
4740
4741primary_value : value_expr(primary)
4742 ;
4743
4744k_begin : keyword_begin
4745 {
4746 token_info_push(p, "begin", &@$);
4747 push_end_expect_token_locations(p, &@1.beg_pos);
4748 }
4749 ;
4750
4751k_if : keyword_if
4752 {
4753 WARN_EOL("if");
4754 token_info_push(p, "if", &@$);
4755 if (p->token_info && p->token_info->nonspc &&
4756 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4757 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4758 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4759 beg += rb_strlen_lit("else");
4760 while (beg < tok && ISSPACE(*beg)) beg++;
4761 if (beg == tok) {
4762 p->token_info->nonspc = 0;
4763 }
4764 }
4765 push_end_expect_token_locations(p, &@1.beg_pos);
4766 }
4767 ;
4768
4769k_unless : keyword_unless
4770 {
4771 token_info_push(p, "unless", &@$);
4772 push_end_expect_token_locations(p, &@1.beg_pos);
4773 }
4774 ;
4775
4776k_while : keyword_while[kw] allow_exits
4777 {
4778 $$ = $allow_exits;
4779 token_info_push(p, "while", &@$);
4780 push_end_expect_token_locations(p, &@kw.beg_pos);
4781 }
4782 ;
4783
4784k_until : keyword_until[kw] allow_exits
4785 {
4786 $$ = $allow_exits;
4787 token_info_push(p, "until", &@$);
4788 push_end_expect_token_locations(p, &@kw.beg_pos);
4789 }
4790 ;
4791
4792k_case : keyword_case
4793 {
4794 token_info_push(p, "case", &@$);
4795 push_end_expect_token_locations(p, &@1.beg_pos);
4796 }
4797 ;
4798
4799k_for : keyword_for[kw] allow_exits
4800 {
4801 $$ = $allow_exits;
4802 token_info_push(p, "for", &@$);
4803 push_end_expect_token_locations(p, &@kw.beg_pos);
4804 }
4805 ;
4806
4807k_class : keyword_class
4808 {
4809 token_info_push(p, "class", &@$);
4810 $$ = p->ctxt;
4811 p->ctxt.in_rescue = before_rescue;
4812 push_end_expect_token_locations(p, &@1.beg_pos);
4813 }
4814 ;
4815
4816k_module : keyword_module
4817 {
4818 token_info_push(p, "module", &@$);
4819 $$ = p->ctxt;
4820 p->ctxt.in_rescue = before_rescue;
4821 push_end_expect_token_locations(p, &@1.beg_pos);
4822 }
4823 ;
4824
4825k_def : keyword_def
4826 {
4827 token_info_push(p, "def", &@$);
4828 $$ = NEW_DEF_TEMP(&@$);
4829 p->ctxt.in_argdef = 1;
4830 }
4831 ;
4832
4833k_do : keyword_do
4834 {
4835 token_info_push(p, "do", &@$);
4836 push_end_expect_token_locations(p, &@1.beg_pos);
4837 }
4838 ;
4839
4840k_do_block : keyword_do_block
4841 {
4842 token_info_push(p, "do", &@$);
4843 push_end_expect_token_locations(p, &@1.beg_pos);
4844 }
4845 ;
4846
4847k_rescue : keyword_rescue
4848 {
4849 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4850 $$ = p->ctxt;
4851 p->ctxt.in_rescue = after_rescue;
4852 }
4853 ;
4854
4855k_ensure : keyword_ensure
4856 {
4857 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4858 $$ = p->ctxt;
4859 }
4860 ;
4861
4862k_when : keyword_when
4863 {
4864 token_info_warn(p, "when", p->token_info, 0, &@$);
4865 }
4866 ;
4867
4868k_else : keyword_else
4869 {
4870 token_info *ptinfo_beg = p->token_info;
4871 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4872 token_info_warn(p, "else", p->token_info, same, &@$);
4873 if (same) {
4874 token_info e;
4875 e.next = ptinfo_beg->next;
4876 e.token = "else";
4877 token_info_setup(&e, p->lex.pbeg, &@$);
4878 if (!e.nonspc) *ptinfo_beg = e;
4879 }
4880 }
4881 ;
4882
4883k_elsif : keyword_elsif
4884 {
4885 WARN_EOL("elsif");
4886 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4887 }
4888 ;
4889
4890k_end : keyword_end
4891 {
4892 token_info_pop(p, "end", &@$);
4893 pop_end_expect_token_locations(p);
4894 }
4895 | tDUMNY_END
4896 {
4897 compile_error(p, "syntax error, unexpected end-of-input");
4898 }
4899 ;
4900
4901k_return : keyword_return
4902 {
4903 if (p->ctxt.cant_return && !dyna_in_block(p))
4904 yyerror1(&@1, "Invalid return in class/module body");
4905 }
4906 ;
4907
4908k_return_arg : k_return
4909 {
4910 CMDARG_PUSH(0);
4911 }
4912 ;
4913
4914k_break_arg : keyword_break
4915 {
4916 CMDARG_PUSH(0);
4917 }
4918 ;
4919
4920k_next_arg : keyword_next
4921 {
4922 CMDARG_PUSH(0);
4923 }
4924 ;
4925
4926k_yield : keyword_yield
4927 {
4928 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4929 yyerror1(&@1, "Invalid yield");
4930 }
4931 ;
4932
4933then : term
4934 | keyword_then
4935 | term keyword_then
4936 ;
4937
4938do : term
4939 | keyword_do_cond { $$ = keyword_do_cond; }
4940 ;
4941
4942if_tail : opt_else
4943 | k_elsif expr_value then
4944 compstmt(stmts)
4945 if_tail
4946 {
4947 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4948 fixpos($$, $2);
4949 /*% ripper: elsif!($:2, $:4, $:5) %*/
4950 }
4951 ;
4952
4953opt_else : none
4954 | k_else compstmt(stmts)
4955 {
4956 $$ = $2;
4957 /*% ripper: else!($:2) %*/
4958 }
4959 ;
4960
4961for_var : lhs
4962 | mlhs
4963 ;
4964
4965f_marg : f_norm_arg
4966 {
4967 $$ = assignable(p, $1, 0, &@$);
4968 mark_lvar_used(p, $$);
4969 }
4970 | tLPAREN f_margs rparen
4971 {
4972 $$ = (NODE *)$2;
4973 /*% ripper: mlhs_paren!($:2) %*/
4974 }
4975 ;
4976
4977
4978f_margs : mlhs_items(f_marg)
4979 {
4980 $$ = NEW_MASGN($1, 0, &@$);
4981 /*% ripper: $:1 %*/
4982 }
4983 | mlhs_items(f_marg) ',' f_rest_marg
4984 {
4985 $$ = NEW_MASGN($1, $3, &@$);
4986 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4987 }
4988 | mlhs_items(f_marg) ',' f_rest_marg ',' mlhs_items(f_marg)
4989 {
4990 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4991 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4992 }
4993 | f_rest_marg
4994 {
4995 $$ = NEW_MASGN(0, $1, &@$);
4996 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4997 }
4998 | f_rest_marg ',' mlhs_items(f_marg)
4999 {
5000 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
5001 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
5002 }
5003 ;
5004
5005f_rest_marg : tSTAR f_norm_arg
5006 {
5007 /*% ripper: $:2 %*/
5008 $$ = assignable(p, $2, 0, &@$);
5009 mark_lvar_used(p, $$);
5010 }
5011 | tSTAR
5012 {
5013 $$ = NODE_SPECIAL_NO_NAME_REST;
5014 /*% ripper: Qnil %*/
5015 }
5016 ;
5017
5018f_any_kwrest : f_kwrest
5019 | f_no_kwarg
5020 {
5021 $$ = idNil;
5022 /*% ripper: ID2VAL(idNil) %*/
5023 }
5024 ;
5025
5026f_eq : {p->ctxt.in_argdef = 0;} '=';
5027
5028block_args_tail : args_tail_basic(primary_value, none)
5029 ;
5030
5031excessed_comma : ','
5032 {
5033 /* magic number for rest_id in iseq_set_arguments() */
5034 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
5035 /*% ripper: excessed_comma! %*/
5036 }
5037 ;
5038
5039block_param : args-list(primary_value, opt_args_tail(block_args_tail, none))
5040 | f_arg[pre] excessed_comma
5041 {
5042 $$ = new_empty_args_tail(p, &@excessed_comma);
5043 $$ = new_args(p, $pre, 0, $excessed_comma, 0, $$, &@$);
5044 /*% ripper: params!($:pre, Qnil, $:excessed_comma, Qnil, Qnil, Qnil, Qnil) %*/
5045 }
5046 | f_arg[pre] opt_args_tail(block_args_tail, none)[tail]
5047 {
5048 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
5049 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
5050 }
5051 | tail-only-args(block_args_tail)
5052 ;
5053
5054opt_block_param_def : none
5055 | block_param_def
5056 {
5057 p->command_start = TRUE;
5058 }
5059 ;
5060
5061block_param_def : '|' opt_block_param opt_bv_decl '|'
5062 {
5063 p->max_numparam = ORDINAL_PARAM;
5064 p->ctxt.in_argdef = 0;
5065 $$ = $2;
5066 /*% ripper: block_var!($:2, $:3) %*/
5067 }
5068 ;
5069
5070opt_block_param : /* none */
5071 {
5072 $$ = 0;
5073 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5074 }
5075 | block_param
5076 ;
5077
5078opt_bv_decl : '\n'?
5079 {
5080 $$ = 0;
5081 /*% ripper: Qfalse %*/
5082 }
5083 | '\n'? ';' bv_decls '\n'?
5084 {
5085 $$ = 0;
5086 /*% ripper: $:3 %*/
5087 }
5088 ;
5089
5090bv_decls : bvar
5091 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5092 | bv_decls ',' bvar
5093 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5094 ;
5095
5096bvar : tIDENTIFIER
5097 {
5098 new_bv(p, $1);
5099 /*% ripper: $:1 %*/
5100 }
5101 | f_bad_arg
5102 ;
5103
5104max_numparam : {
5105 $$ = p->max_numparam;
5106 p->max_numparam = 0;
5107 }
5108 ;
5109
5110numparam : {
5111 $$ = numparam_push(p);
5112 }
5113 ;
5114
5115it_id : {
5116 $$ = p->it_id;
5117 p->it_id = 0;
5118 }
5119 ;
5120
5121lambda : tLAMBDA[lpar]
5122 {
5123 token_info_push(p, "->", &@lpar);
5124 $$ = dyna_push(p);
5125 }[dyna]<vars>
5126 max_numparam numparam it_id allow_exits
5127 f_larglist[args]
5128 {
5129 CMDARG_PUSH(0);
5130 }
5131 lambda_body[body]
5132 {
5133 int max_numparam = p->max_numparam;
5134 ID it_id = p->it_id;
5135 p->lex.lpar_beg = $lpar;
5136 p->max_numparam = $max_numparam;
5137 p->it_id = $it_id;
5138 restore_block_exit(p, $allow_exits);
5139 CMDARG_POP();
5140 $args = args_with_numbered(p, $args, max_numparam, it_id);
5141 {
5142 YYLTYPE loc = code_loc_gen(&@lpar, &@body);
5143 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5144 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5145 nd_set_line($$, @args.end_pos.lineno);
5146 nd_set_first_loc($$, @lpar.beg_pos);
5147 xfree($body);
5148 }
5149 /*% ripper: lambda!($:args, $:body) %*/
5150 numparam_pop(p, $numparam);
5151 dyna_pop(p, $dyna);
5152 }
5153 ;
5154
5155f_larglist : '(' f_largs[args] opt_bv_decl ')'
5156 {
5157 p->ctxt.in_argdef = 0;
5158 $$ = $args;
5159 p->max_numparam = ORDINAL_PARAM;
5160 /*% ripper: paren!($:args) %*/
5161 }
5162 | f_largs[args]
5163 {
5164 p->ctxt.in_argdef = 0;
5165 if (!args_info_empty_p(&$args->nd_ainfo))
5166 p->max_numparam = ORDINAL_PARAM;
5167 $$ = $args;
5168 }
5169 ;
5170
5171lambda_body : tLAMBEG compstmt(stmts) '}'
5172 {
5173 token_info_pop(p, "}", &@3);
5174 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5175 /*% ripper: $:2 %*/
5176 }
5177 | keyword_do_LAMBDA
5178 {
5179 push_end_expect_token_locations(p, &@1.beg_pos);
5180 }
5181 bodystmt k_end
5182 {
5183 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5184 /*% ripper: $:3 %*/
5185 }
5186 ;
5187
5188do_block : k_do_block do_body k_end
5189 {
5190 $$ = $2;
5191 set_embraced_location($$, &@1, &@3);
5192 /*% ripper: $:2 %*/
5193 }
5194 ;
5195
5196block_call : command do_block
5197 {
5198 $$ = command_add_block(p, $1, $2, &@$);
5199 fixpos($$, $1);
5200 /*% ripper: method_add_block!($:1, $:2) %*/
5201 }
5202 | block_call call_op2 operation2 opt_paren_args
5203 {
5204 bool has_args = $4 != 0;
5205 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5206 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5207 /*% ripper: call!($:1, $:2, $:3) %*/
5208 if (has_args) {
5209 /*% ripper: method_add_arg!($:$, $:4) %*/
5210 }
5211 }
5212 | block_call call_op2 operation2 opt_paren_args brace_block
5213 {
5214 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5215 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5216 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5217 }
5218 | block_call call_op2 operation2 command_args do_block
5219 {
5220 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5221 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5222 }
5223 | block_call call_op2 operation2 command_args cmd_brace_block
5224 {
5225 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5226 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5227 }
5228 | block_call call_op2 paren_args
5229 {
5230 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5231 nd_set_line($$, @2.end_pos.lineno);
5232 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5233 }
5234 ;
5235
5236method_call : fcall paren_args
5237 {
5238 $1->nd_args = $2;
5239 $$ = (NODE *)$1;
5240 nd_set_last_loc($1, @2.end_pos);
5241 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5242 }
5243 | primary_value call_op operation2 opt_paren_args
5244 {
5245 bool has_args = $4 != 0;
5246 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5247 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5248 nd_set_line($$, @3.end_pos.lineno);
5249 /*% ripper: call!($:1, $:2, $:3) %*/
5250 if (has_args) {
5251 /*% ripper: method_add_arg!($:$, $:4) %*/
5252 }
5253 }
5254 | primary_value tCOLON2 operation2 paren_args
5255 {
5256 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5257 nd_set_line($$, @3.end_pos.lineno);
5258 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5259 }
5260 | primary_value tCOLON2 operation3
5261 {
5262 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5263 /*% ripper: call!($:1, $:2, $:3) %*/
5264 }
5265 | primary_value call_op2 paren_args
5266 {
5267 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5268 nd_set_line($$, @2.end_pos.lineno);
5269 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5270 }
5271 | keyword_super paren_args
5272 {
5273 rb_code_location_t lparen_loc = @2;
5274 rb_code_location_t rparen_loc = @2;
5275 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5276 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5277
5278 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5279 /*% ripper: super!($:2) %*/
5280 }
5281 | keyword_super
5282 {
5283 $$ = NEW_ZSUPER(&@$);
5284 /*% ripper: zsuper! %*/
5285 }
5286 | primary_value '[' opt_call_args rbracket
5287 {
5288 $$ = NEW_CALL($1, tAREF, $3, &@$);
5289 fixpos($$, $1);
5290 /*% ripper: aref!($:1, $:3) %*/
5291 }
5292 ;
5293
5294brace_block : '{' brace_body '}'
5295 {
5296 $$ = $2;
5297 set_embraced_location($$, &@1, &@3);
5298 /*% ripper: $:2 %*/
5299 }
5300 | k_do do_body k_end
5301 {
5302 $$ = $2;
5303 set_embraced_location($$, &@1, &@3);
5304 /*% ripper: $:2 %*/
5305 }
5306 ;
5307
5308brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5309 max_numparam numparam it_id allow_exits
5310 opt_block_param_def[args] compstmt(stmts)
5311 {
5312 int max_numparam = p->max_numparam;
5313 ID it_id = p->it_id;
5314 p->max_numparam = $max_numparam;
5315 p->it_id = $it_id;
5316 $args = args_with_numbered(p, $args, max_numparam, it_id);
5317 $$ = NEW_ITER($args, $compstmt, &@$);
5318 /*% ripper: brace_block!($:args, $:compstmt) %*/
5319 restore_block_exit(p, $allow_exits);
5320 numparam_pop(p, $numparam);
5321 dyna_pop(p, $dyna);
5322 }
5323 ;
5324
5325do_body : {
5326 $$ = dyna_push(p);
5327 CMDARG_PUSH(0);
5328 }[dyna]<vars>
5329 max_numparam numparam it_id allow_exits
5330 opt_block_param_def[args] bodystmt
5331 {
5332 int max_numparam = p->max_numparam;
5333 ID it_id = p->it_id;
5334 p->max_numparam = $max_numparam;
5335 p->it_id = $it_id;
5336 $args = args_with_numbered(p, $args, max_numparam, it_id);
5337 $$ = NEW_ITER($args, $bodystmt, &@$);
5338 /*% ripper: do_block!($:args, $:bodystmt) %*/
5339 CMDARG_POP();
5340 restore_block_exit(p, $allow_exits);
5341 numparam_pop(p, $numparam);
5342 dyna_pop(p, $dyna);
5343 }
5344 ;
5345
5346case_args : arg_value
5347 {
5348 check_literal_when(p, $arg_value, &@arg_value);
5349 $$ = NEW_LIST($arg_value, &@$);
5350 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5351 }
5352 | tSTAR arg_value
5353 {
5354 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5355 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5356 }
5357 | case_args[non_last_args] ',' arg_value
5358 {
5359 check_literal_when(p, $arg_value, &@arg_value);
5360 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5361 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5362 }
5363 | case_args[non_last_args] ',' tSTAR arg_value
5364 {
5365 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5366 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5367 }
5368 ;
5369
5370case_body : k_when case_args then
5371 compstmt(stmts)
5372 cases
5373 {
5374 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5375 fixpos($$, $2);
5376 /*% ripper: when!($:2, $:4, $:5) %*/
5377 }
5378 ;
5379
5380cases : opt_else
5381 | case_body
5382 ;
5383
5384p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5385p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5386
5387p_in_kwarg : {
5388 $$ = p->ctxt;
5389 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5390 p->command_start = FALSE;
5391 p->ctxt.in_kwarg = 1;
5392 p->ctxt.in_alt_pattern = 0;
5393 p->ctxt.capture_in_pattern = 0;
5394 }
5395 ;
5396
5397p_case_body : keyword_in
5398 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5399 p_top_expr[expr] then
5400 {
5401 pop_pktbl(p, $p_pktbl);
5402 pop_pvtbl(p, $p_pvtbl);
5403 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5404 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
5405 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
5406 }
5407 compstmt(stmts)
5408 p_cases[cases]
5409 {
5410 $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
5411 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5412 }
5413 ;
5414
5415p_cases : opt_else
5416 | p_case_body
5417 ;
5418
5419p_top_expr : p_top_expr_body
5420 | p_top_expr_body modifier_if expr_value
5421 {
5422 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5423 fixpos($$, $3);
5424 /*% ripper: if_mod!($:3, $:1) %*/
5425 }
5426 | p_top_expr_body modifier_unless expr_value
5427 {
5428 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5429 fixpos($$, $3);
5430 /*% ripper: unless_mod!($:3, $:1) %*/
5431 }
5432 ;
5433
5434p_top_expr_body : p_expr
5435 | p_expr ','
5436 {
5437 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5438 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5439 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5440 }
5441 | p_expr ',' p_args
5442 {
5443 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5444 nd_set_first_loc($$, @1.beg_pos);
5445 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5446 }
5447 | p_find
5448 {
5449 $$ = new_find_pattern(p, 0, $1, &@$);
5450 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5451 }
5452 | p_args_tail
5453 {
5454 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5455 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5456 }
5457 | p_kwargs
5458 {
5459 $$ = new_hash_pattern(p, 0, $1, &@$);
5460 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5461 }
5462 ;
5463
5464p_expr : p_as
5465 ;
5466
5467p_as : p_expr tASSOC p_variable
5468 {
5469 NODE *n = NEW_LIST($1, &@$);
5470 n = list_append(p, n, $3);
5471 $$ = new_hash(p, n, &@$);
5472 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5473 }
5474 | p_alt
5475 ;
5476
5477p_alt : p_alt[left] '|'[alt] p_alt_begin[ctxt] p_expr_basic[right]
5478 {
5479 /* `capture_in_pattern` is only a cheap pre-filter: it is set by any
5480 * binding in the whole `in` clause, so verify the left operand
5481 * actually contains a binding before rejecting (like Prism does). */
5482 if (p->ctxt.capture_in_pattern && pattern_has_capture(p, $left)) {
5483 yyerror1(&@alt, "alternative pattern after variable capture");
5484 }
5485 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
5486 $$ = NEW_OR($left, $right, &@$, &@alt);
5487 /*% ripper: binary!($:left, ID2VAL(idOr), $:right) %*/
5488 }
5489 | p_expr_basic
5490 ;
5491
5492p_alt_begin : {
5493 $$ = p->ctxt;
5494 p->ctxt.in_alt_pattern = 1;
5495 }
5496 ;
5497
5498p_lparen : '(' p_pktbl
5499 {
5500 $$ = $2;
5501 /*% ripper: $:2 %*/
5502 }
5503 ;
5504
5505p_lbracket : '[' p_pktbl
5506 {
5507 $$ = $2;
5508 /*% ripper: $:2 %*/
5509 }
5510 ;
5511
5512p_expr_basic : p_value
5513 | p_variable
5514 | p_const p_lparen[p_pktbl] p_args rparen
5515 {
5516 pop_pktbl(p, $p_pktbl);
5517 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5518 nd_set_first_loc($$, @p_const.beg_pos);
5519 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5520 }
5521 | p_const p_lparen[p_pktbl] p_find rparen
5522 {
5523 pop_pktbl(p, $p_pktbl);
5524 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5525 nd_set_first_loc($$, @p_const.beg_pos);
5526 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5527 }
5528 | p_const p_lparen[p_pktbl] p_kwargs rparen
5529 {
5530 pop_pktbl(p, $p_pktbl);
5531 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5532 nd_set_first_loc($$, @p_const.beg_pos);
5533 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5534 }
5535 | p_const '(' rparen
5536 {
5537 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5538 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5539 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5540 }
5541 | p_const p_lbracket[p_pktbl] p_args rbracket
5542 {
5543 pop_pktbl(p, $p_pktbl);
5544 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5545 nd_set_first_loc($$, @p_const.beg_pos);
5546 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5547 }
5548 | p_const p_lbracket[p_pktbl] p_find rbracket
5549 {
5550 pop_pktbl(p, $p_pktbl);
5551 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5552 nd_set_first_loc($$, @p_const.beg_pos);
5553 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5554 }
5555 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5556 {
5557 pop_pktbl(p, $p_pktbl);
5558 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5559 nd_set_first_loc($$, @p_const.beg_pos);
5560 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5561 }
5562 | p_const '[' rbracket
5563 {
5564 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5565 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5566 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5567 }
5568 | tLBRACK p_args rbracket
5569 {
5570 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5571 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5572 }
5573 | tLBRACK p_find rbracket
5574 {
5575 $$ = new_find_pattern(p, 0, $p_find, &@$);
5576 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5577 }
5578 | tLBRACK rbracket
5579 {
5580 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5581 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5582 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5583 }
5584 | tLBRACE p_pktbl lex_ctxt[ctxt]
5585 {
5586 p->ctxt.in_kwarg = 0;
5587 }
5588 p_kwargs rbrace
5589 {
5590 pop_pktbl(p, $p_pktbl);
5591 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5592 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5593 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5594 }
5595 | tLBRACE rbrace
5596 {
5597 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5598 $$ = new_hash_pattern(p, 0, $$, &@$);
5599 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5600 }
5601 | tLPAREN p_pktbl p_expr rparen
5602 {
5603 pop_pktbl(p, $p_pktbl);
5604 $$ = $p_expr;
5605 /*% ripper: $:p_expr %*/
5606 }
5607 ;
5608
5609p_args : p_expr
5610 {
5611 NODE *pre_args = NEW_LIST($1, &@$);
5612 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5613 /*% ripper: [[$:1], Qnil, Qnil] %*/
5614 }
5615 | p_args_head
5616 {
5617 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5618 /*% ripper: [$:1, Qnil, Qnil] %*/
5619 }
5620 | p_args_head p_arg
5621 {
5622 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5623 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5624 }
5625 | p_args_head p_rest
5626 {
5627 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5628 /*% ripper: [$:1, $:2, Qnil] %*/
5629 }
5630 | p_args_head p_rest ',' p_args_post
5631 {
5632 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5633 /*% ripper: [$:1, $:2, $:4] %*/
5634 }
5635 | p_args_tail
5636 ;
5637
5638p_args_head : p_arg ','
5639 | p_args_head p_arg ','
5640 {
5641 $$ = list_concat($1, $2);
5642 /*% ripper: rb_ary_concat($:1, $:2) %*/
5643 }
5644 ;
5645
5646p_args_tail : p_rest
5647 {
5648 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5649 /*% ripper: [Qnil, $:1, Qnil] %*/
5650 }
5651 | p_rest ',' p_args_post
5652 {
5653 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5654 /*% ripper: [Qnil, $:1, $:3] %*/
5655 }
5656 ;
5657
5658p_find : p_rest ',' p_args_post ',' p_rest
5659 {
5660 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5661 /*% ripper: [$:1, $:3, $:5] %*/
5662 }
5663 ;
5664
5665
5666p_rest : tSTAR tIDENTIFIER
5667 {
5668 error_duplicate_pattern_variable(p, $2, &@2);
5669 /*% ripper: var_field!($:2) %*/
5670 $$ = assignable(p, $2, 0, &@$);
5671 }
5672 | tSTAR
5673 {
5674 $$ = 0;
5675 /*% ripper: var_field!(Qnil) %*/
5676 }
5677 ;
5678
5679p_args_post : p_arg
5680 | p_args_post ',' p_arg
5681 {
5682 $$ = list_concat($1, $3);
5683 /*% ripper: rb_ary_concat($:1, $:3) %*/
5684 }
5685 ;
5686
5687p_arg : p_expr
5688 {
5689 $$ = NEW_LIST($1, &@$);
5690 /*% ripper: [$:1] %*/
5691 }
5692 ;
5693
5694p_kwargs : p_kwarg ',' p_any_kwrest
5695 {
5696 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5697 /*% ripper: [$:1, $:3] %*/
5698 }
5699 | p_kwarg
5700 {
5701 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5702 /*% ripper: [$:1, Qnil] %*/
5703 }
5704 | p_kwarg ','
5705 {
5706 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5707 /*% ripper: [$:1, Qnil] %*/
5708 }
5709 | p_any_kwrest
5710 {
5711 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5712 /*% ripper: [[], $:1] %*/
5713 }
5714 ;
5715
5716p_kwarg : p_kw
5717 /*% ripper[brace]: [$:1] %*/
5718 | p_kwarg ',' p_kw
5719 {
5720 $$ = list_concat($1, $3);
5721 /*% ripper: rb_ary_push($:1, $:3) %*/
5722 }
5723 ;
5724
5725p_kw : p_kw_label p_expr
5726 {
5727 error_duplicate_pattern_key(p, $1, &@1);
5728 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5729 /*% ripper: [$:1, $:2] %*/
5730 }
5731 | p_kw_label
5732 {
5733 error_duplicate_pattern_key(p, $1, &@1);
5734 if ($1 && !is_local_id($1)) {
5735 yyerror1(&@1, "key must be valid as local variables");
5736 }
5737 error_duplicate_pattern_variable(p, $1, &@1);
5738 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5739 /*% ripper: [$:1, Qnil] %*/
5740 }
5741 ;
5742
5743p_kw_label : tLABEL
5744 | tSTRING_BEG string_contents tLABEL_END
5745 {
5746 YYLTYPE loc = code_loc_gen(&@1, &@3);
5747 if (!$2 || nd_type_p($2, NODE_STR)) {
5748 NODE *node = dsym_node(p, $2, &loc);
5749 $$ = rb_sym2id(rb_node_sym_string_val(node));
5750 }
5751 else {
5752 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5753 $$ = rb_intern_str(STR_NEW0());
5754 }
5755 /*% ripper: $:2 %*/
5756 }
5757 ;
5758
5759p_kwrest : kwrest_mark tIDENTIFIER
5760 {
5761 error_duplicate_pattern_variable(p, $2, &@2);
5762 $$ = $2;
5763 /*% ripper: var_field!($:2) %*/
5764 }
5765 | kwrest_mark
5766 {
5767 $$ = 0;
5768 /*% ripper: Qnil %*/
5769 }
5770 ;
5771
5772p_kwnorest : kwrest_mark keyword_nil
5773 {
5774 $$ = 0;
5775 }
5776 ;
5777
5778p_any_kwrest : p_kwrest
5779 | p_kwnorest
5780 {
5781 $$ = idNil;
5782 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5783 }
5784 ;
5785
5786p_value : p_primitive
5787 | range_expr(p_primitive)
5788 | p_var_ref
5789 | p_expr_ref
5790 | p_const
5791 ;
5792
5793p_primitive : inline_primary
5794 | keyword_variable
5795 {
5796 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5797 /*% ripper: var_ref!($:1) %*/
5798 }
5799 | lambda
5800 ;
5801
5802p_variable : tIDENTIFIER
5803 {
5804 error_duplicate_pattern_variable(p, $1, &@1);
5805 /*% ripper: var_field!($:1) %*/
5806 $$ = assignable(p, $1, 0, &@$);
5807 }
5808 ;
5809
5810p_var_ref : '^' tIDENTIFIER
5811 {
5812 NODE *n = gettable(p, $2, &@$);
5813 if (!n) {
5814 n = NEW_ERROR(&@$);
5815 }
5816 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5817 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5818 }
5819 $$ = n;
5820 /*% ripper: var_ref!($:2) %*/
5821 }
5822 | '^' nonlocal_var
5823 {
5824 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5825 /*% ripper: var_ref!($:2) %*/
5826 }
5827 ;
5828
5829p_expr_ref : '^' tLPAREN expr_value rparen
5830 {
5831 $$ = NEW_BLOCK($3, &@$);
5832 /*% ripper: begin!($:3) %*/
5833 }
5834 ;
5835
5836p_const : tCOLON3 cname
5837 {
5838 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5839 /*% ripper: top_const_ref!($:2) %*/
5840 }
5841 | p_const tCOLON2 cname
5842 {
5843 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5844 /*% ripper: const_path_ref!($:1, $:3) %*/
5845 }
5846 | tCONSTANT
5847 {
5848 $$ = gettable(p, $1, &@$);
5849 /*% ripper: var_ref!($:1) %*/
5850 }
5851 ;
5852
5853opt_rescue : k_rescue exc_list exc_var then
5854 compstmt(stmts)
5855 opt_rescue
5856 {
5857 NODE *err = $3;
5858 if ($3) {
5859 err = NEW_ERRINFO(&@3);
5860 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5861 }
5862 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5863 if ($2) {
5864 fixpos($$, $2);
5865 }
5866 else if ($3) {
5867 fixpos($$, $3);
5868 }
5869 else {
5870 fixpos($$, $5);
5871 }
5872 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5873 }
5874 | none
5875 ;
5876
5877exc_list : arg_value
5878 {
5879 $$ = NEW_LIST($1, &@$);
5880 /*% ripper: rb_ary_new3(1, $:1) %*/
5881 }
5882 | mrhs
5883 {
5884 if (!($$ = splat_array($1))) $$ = $1;
5885 }
5886 | none
5887 ;
5888
5889exc_var : tASSOC lhs
5890 {
5891 $$ = $2;
5892 /*% ripper: $:2 %*/
5893 }
5894 | none
5895 ;
5896
5897opt_ensure : k_ensure stmts terms?
5898 {
5899 p->ctxt.in_rescue = $1.in_rescue;
5900 $$ = $2;
5901 void_expr(p, void_stmts(p, $$));
5902 /*% ripper: ensure!($:2) %*/
5903 }
5904 | none
5905 ;
5906
5907literal : numeric
5908 | symbol
5909 ;
5910
5911strings : string
5912 {
5913 if (!$1) {
5914 $$ = NEW_STR(STRING_NEW0(), &@$);
5915 }
5916 else {
5917 $$ = evstr2dstr(p, $1);
5918 }
5919 /*% ripper: $:1 %*/
5920 }
5921 ;
5922
5923string : tCHAR
5924 | string1
5925 | string string1
5926 {
5927 $$ = literal_concat(p, $1, $2, &@$);
5928 /*% ripper: string_concat!($:1, $:2) %*/
5929 }
5930 ;
5931
5932string1 : tSTRING_BEG string_contents tSTRING_END
5933 {
5934 $$ = heredoc_dedent(p, $2);
5935 if ($$) nd_set_loc($$, &@$);
5936 /*% ripper: $:2 %*/
5937 if (p->heredoc_indent > 0) {
5938 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5939 p->heredoc_indent = 0;
5940 }
5941 /*% ripper: string_literal!($:$) %*/
5942 }
5943 ;
5944
5945xstring : tXSTRING_BEG xstring_contents tSTRING_END
5946 {
5947 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5948 /*% ripper: $:2 %*/
5949 if (p->heredoc_indent > 0) {
5950 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5951 p->heredoc_indent = 0;
5952 }
5953 /*% ripper: xstring_literal!($:$) %*/
5954 }
5955 ;
5956
5957regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5958 {
5959 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5960 /*% ripper: regexp_literal!($:2, $:3) %*/
5961 }
5962 ;
5963
5964words : words(tWORDS_BEG, word_list)
5965 ;
5966
5967word_list : /* none */
5968 {
5969 $$ = 0;
5970 /*% ripper: words_new! %*/
5971 }
5972 | word_list word ' '+
5973 {
5974 $$ = $2 ? list_append(p, $1, evstr2dstr(p, $2)) : $2;
5975 /*% ripper: words_add!($:1, $:2) %*/
5976 }
5977 ;
5978
5979word : string_content
5980 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5981 | word string_content
5982 {
5983 $$ = literal_concat(p, $1, $2, &@$);
5984 /*% ripper: word_add!($:1, $:2) %*/
5985 }
5986 ;
5987
5988symbols : words(tSYMBOLS_BEG, symbol_list)
5989 ;
5990
5991symbol_list : /* none */
5992 {
5993 $$ = 0;
5994 /*% ripper: symbols_new! %*/
5995 }
5996 | symbol_list word ' '+
5997 {
5998 $$ = $2 ? symbol_append(p, $1, evstr2dstr(p, $2)) : $2;
5999 /*% ripper: symbols_add!($:1, $:2) %*/
6000 }
6001 ;
6002
6003qwords : words(tQWORDS_BEG, qword_list)
6004 ;
6005
6006qsymbols : words(tQSYMBOLS_BEG, qsym_list)
6007 ;
6008
6009qword_list : /* none */
6010 {
6011 $$ = 0;
6012 /*% ripper: qwords_new! %*/
6013 }
6014 | qword_list tSTRING_CONTENT ' '+
6015 {
6016 $$ = list_append(p, $1, $2);
6017 /*% ripper: qwords_add!($:1, $:2) %*/
6018 }
6019 ;
6020
6021qsym_list : /* none */
6022 {
6023 $$ = 0;
6024 /*% ripper: qsymbols_new! %*/
6025 }
6026 | qsym_list tSTRING_CONTENT ' '+
6027 {
6028 $$ = symbol_append(p, $1, $2);
6029 /*% ripper: qsymbols_add!($:1, $:2) %*/
6030 }
6031 ;
6032
6033string_contents : /* none */
6034 {
6035 $$ = 0;
6036 /*% ripper: string_content! %*/
6037 }
6038 | string_contents string_content
6039 {
6040 $$ = literal_concat(p, $1, $2, &@$);
6041 /*% ripper: string_add!($:1, $:2) %*/
6042 }
6043 ;
6044
6045xstring_contents: /* none */
6046 {
6047 $$ = 0;
6048 /*% ripper: xstring_new! %*/
6049 }
6050 | xstring_contents string_content
6051 {
6052 $$ = literal_concat(p, $1, $2, &@$);
6053 /*% ripper: xstring_add!($:1, $:2) %*/
6054 }
6055 ;
6056
6057regexp_contents : /* none */
6058 {
6059 $$ = 0;
6060 /*% ripper: regexp_new! %*/
6061 }
6062 | regexp_contents string_content
6063 {
6064 NODE *head = $1, *tail = $2;
6065 if (!head) {
6066 $$ = tail;
6067 }
6068 else if (!tail) {
6069 $$ = head;
6070 }
6071 else {
6072 switch (nd_type(head)) {
6073 case NODE_STR:
6074 head = str2dstr(p, head);
6075 break;
6076 case NODE_DSTR:
6077 break;
6078 default:
6079 head = list_append(p, NEW_DSTR(0, &@$), head);
6080 break;
6081 }
6082 $$ = list_append(p, head, tail);
6083 }
6084 /*% ripper: regexp_add!($:1, $:2) %*/
6085 }
6086 ;
6087
6088string_content : tSTRING_CONTENT[content]
6089 /*% ripper[brace]: $:content %*/
6090 | tSTRING_DVAR[state]
6091 {
6092 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6093 $$ = p->lex.strterm;
6094 p->lex.strterm = 0;
6095 SET_LEX_STATE(EXPR_BEG);
6096 }[strterm]<strterm>
6097 string_dvar[dvar]
6098 {
6099 p->lex.strterm = $strterm;
6100 $$ = NEW_EVSTR($dvar, &@$, &@state, &NULL_LOC);
6101 nd_set_line($$, @dvar.end_pos.lineno);
6102 /*% ripper: string_dvar!($:dvar) %*/
6103 }
6104 | tSTRING_DBEG[state]
6105 {
6106 CMDARG_PUSH(0);
6107 COND_PUSH(0);
6108 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6109 $$ = p->lex.strterm;
6110 p->lex.strterm = 0;
6111 SET_LEX_STATE(EXPR_BEG);
6112 }[term]<strterm>
6113 {
6114 $$ = p->lex.brace_nest;
6115 p->lex.brace_nest = 0;
6116 }[brace]<num>
6117 {
6118 $$ = p->lex.lpar_beg;
6119 p->lex.lpar_beg = -1;
6120 }[lpar]<num>
6121 {
6122 $$ = p->heredoc_indent;
6123 p->heredoc_indent = 0;
6124 }[indent]<num>
6125 compstmt(stmts) string_dend
6126 {
6127 COND_POP();
6128 CMDARG_POP();
6129 p->lex.strterm = $term;
6130 SET_LEX_STATE($state);
6131 p->lex.brace_nest = $brace;
6132 p->lex.lpar_beg = $lpar;
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 | f_empty_arg
6261 {
6262 p->ctxt.in_argdef = 0;
6263 }
6264 ;
6265
6266f_empty_arg : /* none */
6267 {
6268 $$ = new_empty_args_tail(p, &@$);
6269 $$ = new_args(p, 0, 0, 0, 0, $$, &@$);
6270 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6271 }
6272 ;
6273
6274f_paren_args : '(' f_args rparen
6275 {
6276 $$ = $2;
6277 /*% ripper: paren!($:2) %*/
6278 SET_LEX_STATE(EXPR_BEG);
6279 p->command_start = TRUE;
6280 p->ctxt.in_argdef = 0;
6281 }
6282 ;
6283
6284f_arglist : f_paren_args
6285 | {
6286 $$ = p->ctxt;
6287 p->ctxt.in_kwarg = 1;
6288 p->ctxt.in_argdef = 1;
6289 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6290 }<ctxt>
6291 f_args term
6292 {
6293 p->ctxt.in_kwarg = $1.in_kwarg;
6294 p->ctxt.in_argdef = 0;
6295 $$ = $2;
6296 SET_LEX_STATE(EXPR_BEG);
6297 p->command_start = TRUE;
6298 /*% ripper: $:2 %*/
6299 }
6300 ;
6301
6302args_tail : args_tail_basic(arg_value, opt_comma)
6303 | args_forward
6304 {
6305 add_forwarding_args(p);
6306 $$ = new_args_tail(p, 0, $args_forward, arg_FWD_BLOCK, &@args_forward);
6307 $$->nd_ainfo.forwarding = 1;
6308 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6309 }
6310 ;
6311
6312largs_tail : args_tail_basic(arg_value, none)
6313 | args_forward
6314 {
6315 yyerror1(&@args_forward, "unexpected ... in lambda argument");
6316 $$ = new_args_tail(p, 0, 0, 0, &@args_forward);
6317 $$->nd_ainfo.forwarding = 1;
6318 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6319 }
6320 ;
6321
6322%rule args-list(value, tail) <node_args>
6323 : f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6324 {
6325 $$ = new_args(p, $pre, $opt, $rest, 0, $tail, &@$);
6326 /*% ripper: params!($:pre, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6327 }
6328 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6329 {
6330 $$ = new_args(p, $pre, $opt, $rest, $post, $tail, &@$);
6331 /*% ripper: params!($:pre, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6332 }
6333 | f_arg[pre] ',' f_opt_arg(value)[opt] tail
6334 {
6335 $$ = new_args(p, $pre, $opt, 0, 0, $tail, &@$);
6336 /*% ripper: params!($:pre, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6337 }
6338 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_arg[post] tail
6339 {
6340 $$ = new_args(p, $pre, $opt, 0, $post, $tail, &@$);
6341 /*% ripper: params!($:pre, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6342 }
6343 | f_arg[pre] ',' f_rest_arg[rest] tail
6344 {
6345 $$ = new_args(p, $pre, 0, $rest, 0, $tail, &@$);
6346 /*% ripper: params!($:pre, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6347 }
6348 | f_arg[pre] ',' f_rest_arg[rest] ',' f_arg[post] tail
6349 {
6350 $$ = new_args(p, $pre, 0, $rest, $post, $tail, &@$);
6351 /*% ripper: params!($:pre, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6352 }
6353 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6354 {
6355 $$ = new_args(p, 0, $opt, $rest, 0, $tail, &@$);
6356 /*% ripper: params!(Qnil, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6357 }
6358 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6359 {
6360 $$ = new_args(p, 0, $opt, $rest, $post, $tail, &@$);
6361 /*% ripper: params!(Qnil, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6362 }
6363 | f_opt_arg(value)[opt] tail
6364 {
6365 $$ = new_args(p, 0, $opt, 0, 0, $tail, &@$);
6366 /*% ripper: params!(Qnil, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6367 }
6368 | f_opt_arg(value)[opt] ',' f_arg[post] tail
6369 {
6370 $$ = new_args(p, 0, $opt, 0, $post, $tail, &@$);
6371 /*% ripper: params!(Qnil, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6372 }
6373 | f_rest_arg[rest] tail
6374 {
6375 $$ = new_args(p, 0, 0, $rest, 0, $tail, &@$);
6376 /*% ripper: params!(Qnil, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6377 }
6378 | f_rest_arg[rest] ',' f_arg[post] tail
6379 {
6380 $$ = new_args(p, 0, 0, $rest, $post, $tail, &@$);
6381 /*% ripper: params!(Qnil, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6382 }
6383 ;
6384
6385%rule tail-only-args(tail) <node_args>
6386 : tail
6387 {
6388 $$ = new_args(p, 0, 0, 0, 0, $tail, &@$);
6389 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6390 }
6391 ;
6392
6393%rule f_args-list(tail, trailing) <node_args>
6394 : args-list(arg_value, opt_args_tail(tail, trailing))
6395 | f_arg[pre] opt_args_tail(tail, trailing)[tail]
6396 {
6397 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
6398 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6399 }
6400 | tail-only-args(tail)
6401 | f_empty_arg
6402 ;
6403
6404f_args : f_args-list(args_tail, opt_comma)
6405 ;
6406
6407f_largs : f_args-list(largs_tail, none)
6408 ;
6409
6410args_forward : tBDOT3
6411 {
6412 $$ = idFWD_KWREST;
6413 /*% ripper: args_forward! %*/
6414 }
6415 ;
6416
6417f_bad_arg : tCONSTANT
6418 {
6419 static const char mesg[] = "formal argument cannot be a constant";
6420 /*%%%*/
6421 yyerror1(&@1, mesg);
6422 /*% %*/
6423 $$ = 0;
6424 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6425 }
6426 | tIVAR
6427 {
6428 static const char mesg[] = "formal argument cannot be an instance variable";
6429 /*%%%*/
6430 yyerror1(&@1, mesg);
6431 /*% %*/
6432 $$ = 0;
6433 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6434 }
6435 | tGVAR
6436 {
6437 static const char mesg[] = "formal argument cannot be a global variable";
6438 /*%%%*/
6439 yyerror1(&@1, mesg);
6440 /*% %*/
6441 $$ = 0;
6442 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6443 }
6444 | tCVAR
6445 {
6446 static const char mesg[] = "formal argument cannot be a class variable";
6447 /*%%%*/
6448 yyerror1(&@1, mesg);
6449 /*% %*/
6450 $$ = 0;
6451 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6452 }
6453 ;
6454
6455f_norm_arg : f_bad_arg
6456 | tIDENTIFIER
6457 {
6458 VALUE e = formal_argument_error(p, $$ = $1);
6459 if (e) {
6460 /*% ripper[error]: param_error!(?e, $:1) %*/
6461 }
6462 p->max_numparam = ORDINAL_PARAM;
6463 }
6464 ;
6465
6466f_arg_asgn : f_norm_arg
6467 {
6468 arg_var(p, $1);
6469 $$ = $1;
6470 }
6471 ;
6472
6473f_arg_item : f_arg_asgn
6474 {
6475 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6476 /*% ripper: $:1 %*/
6477 }
6478 | tLPAREN f_margs rparen
6479 {
6480 ID tid = internal_id(p);
6481 YYLTYPE loc;
6482 loc.beg_pos = @2.beg_pos;
6483 loc.end_pos = @2.beg_pos;
6484 arg_var(p, tid);
6485 if (dyna_in_block(p)) {
6486 $2->nd_value = NEW_DVAR(tid, &loc);
6487 }
6488 else {
6489 $2->nd_value = NEW_LVAR(tid, &loc);
6490 }
6491 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6492 $$->nd_next = (NODE *)$2;
6493 /*% ripper: mlhs_paren!($:2) %*/
6494 }
6495 ;
6496
6497f_arg : f_arg_item
6498 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6499 | f_arg ',' f_arg_item
6500 {
6501 $$ = $1;
6502 $$->nd_plen++;
6503 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6504 rb_discard_node(p, (NODE *)$3);
6505 /*% ripper: rb_ary_push($:1, $:3) %*/
6506 }
6507 ;
6508
6509
6510f_label : tLABEL
6511 {
6512 VALUE e = formal_argument_error(p, $$ = $1);
6513 if (e) {
6514 $$ = 0;
6515 /*% ripper[error]: param_error!(?e, $:1) %*/
6516 }
6517 /*
6518 * Workaround for Prism::ParseTest#test_filepath for
6519 * "unparser/corpus/literal/def.txt"
6520 *
6521 * See the discussion on https://github.com/ruby/ruby/pull/9923
6522 */
6523 arg_var(p, ifdef_ripper(0, $1));
6524 /*% ripper: $:1 %*/
6525 p->max_numparam = ORDINAL_PARAM;
6526 p->ctxt.in_argdef = 0;
6527 }
6528 ;
6529
6530kwrest_mark : tPOW
6531 | tDSTAR
6532 ;
6533
6534f_no_kwarg : p_kwnorest
6535 {
6536 /*% ripper: nokw_param!(Qnil) %*/
6537 }
6538 ;
6539
6540f_kwrest : kwrest_mark tIDENTIFIER
6541 {
6542 arg_var(p, shadowing_lvar(p, $2));
6543 $$ = $2;
6544 /*% ripper: kwrest_param!($:2) %*/
6545 }
6546 | kwrest_mark
6547 {
6548 arg_var(p, idFWD_KWREST);
6549 $$ = idFWD_KWREST;
6550 /*% ripper: kwrest_param!(Qnil) %*/
6551 }
6552 ;
6553
6554restarg_mark : '*'
6555 | tSTAR
6556 ;
6557
6558f_rest_arg : restarg_mark tIDENTIFIER
6559 {
6560 arg_var(p, shadowing_lvar(p, $2));
6561 $$ = $2;
6562 /*% ripper: rest_param!($:2) %*/
6563 }
6564 | restarg_mark
6565 {
6566 arg_var(p, idFWD_REST);
6567 $$ = idFWD_REST;
6568 /*% ripper: rest_param!(Qnil) %*/
6569 }
6570 ;
6571
6572blkarg_mark : '&'
6573 | tAMPER
6574 ;
6575
6576f_block_arg : blkarg_mark tIDENTIFIER
6577 {
6578 arg_var(p, shadowing_lvar(p, $2));
6579 $$ = $2;
6580 /*% ripper: blockarg!($:2) %*/
6581 }
6582 | blkarg_mark keyword_nil
6583 {
6584 $$ = idNil;
6585 /*% ripper: blockarg!(ID2VAL(idNil)) %*/
6586 }
6587 | blkarg_mark
6588 {
6589 arg_var(p, idFWD_BLOCK);
6590 $$ = idFWD_BLOCK;
6591 /*% ripper: blockarg!(Qnil) %*/
6592 }
6593 ;
6594
6595opt_comma : ','?
6596 {
6597 $$ = 0;
6598 /*% ripper: Qnil %*/
6599 }
6600 ;
6601
6602
6603singleton : value_expr(singleton_expr)
6604 {
6605 NODE *expr = last_expr_node($1);
6606 switch (nd_type(expr)) {
6607 case NODE_STR:
6608 case NODE_DSTR:
6609 case NODE_XSTR:
6610 case NODE_DXSTR:
6611 case NODE_REGX:
6612 case NODE_DREGX:
6613 case NODE_SYM:
6614 case NODE_LINE:
6615 case NODE_FILE:
6616 case NODE_ENCODING:
6617 case NODE_INTEGER:
6618 case NODE_FLOAT:
6619 case NODE_RATIONAL:
6620 case NODE_IMAGINARY:
6621 case NODE_DSYM:
6622 case NODE_LIST:
6623 case NODE_ZLIST:
6624 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6625 break;
6626 default:
6627 break;
6628 }
6629 $$ = $1;
6630 }
6631 ;
6632
6633singleton_expr : var_ref
6634 | '('
6635 {
6636 SET_LEX_STATE(EXPR_BEG);
6637 p->ctxt.in_argdef = 0;
6638 }
6639 expr rparen
6640 {
6641 p->ctxt.in_argdef = 1;
6642 $$ = $3;
6643 /*% ripper: paren!($:3) %*/
6644 }
6645 ;
6646
6647assoc_list : none
6648 | assocs trailer
6649 {
6650 $$ = $1;
6651 /*% ripper: assoclist_from_args!($:1) %*/
6652 }
6653 ;
6654
6655assocs : assoc
6656 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6657 | assocs ',' assoc
6658 {
6659 NODE *assocs = $1;
6660 NODE *tail = $3;
6661 if (!assocs) {
6662 assocs = tail;
6663 }
6664 else if (tail) {
6665 NODE *n = RNODE_LIST(tail)->nd_next;
6666 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6667 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6668 /* DSTAR */
6669 tail = RNODE_HASH(n)->nd_head;
6670 }
6671 if (tail) {
6672 assocs = list_concat(assocs, tail);
6673 }
6674 }
6675 $$ = assocs;
6676 /*% ripper: rb_ary_push($:1, $:3) %*/
6677 }
6678 ;
6679
6680assoc : arg_value tASSOC arg_value
6681 {
6682 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6683 /*% ripper: assoc_new!($:1, $:3) %*/
6684 }
6685 | tLABEL arg_value
6686 {
6687 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6688 /*% ripper: assoc_new!($:1, $:2) %*/
6689 }
6690 | tLABEL
6691 {
6692 NODE *val = gettable(p, $1, &@$);
6693 if (!val) val = NEW_ERROR(&@$);
6694 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6695 /*% ripper: assoc_new!($:1, Qnil) %*/
6696 }
6697 | tSTRING_BEG string_contents tLABEL_END arg_value
6698 {
6699 YYLTYPE loc = code_loc_gen(&@1, &@3);
6700 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6701 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6702 }
6703 | tDSTAR arg_value
6704 {
6705 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6706 /*% ripper: assoc_splat!($:2) %*/
6707 }
6708 | tDSTAR
6709 {
6710 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6711 $$ = list_append(p, NEW_LIST(0, &@$),
6712 NEW_LVAR(idFWD_KWREST, &@$));
6713 /*% ripper: assoc_splat!(Qnil) %*/
6714 }
6715 ;
6716
6717%rule %inline operation : ident_or_const
6718 | tFID
6719 ;
6720
6721operation2 : operation
6722 | op
6723 ;
6724
6725operation3 : tIDENTIFIER
6726 | tFID
6727 | op
6728 ;
6729
6730dot_or_colon : '.'
6731 | tCOLON2
6732 ;
6733
6734call_op : '.'
6735 | tANDDOT
6736 ;
6737
6738call_op2 : call_op
6739 | tCOLON2
6740 ;
6741
6742rparen : '\n'? ')'
6743 ;
6744
6745rbracket : '\n'? ']'
6746 ;
6747
6748rbrace : '\n'? '}'
6749 ;
6750
6751trailer : '\n'?
6752 | ','
6753 ;
6754
6755term : ';'
6756 {
6757 yyerrok;
6758 token_flush(p);
6759 if (p->ctxt.in_defined) {
6760 p->ctxt.has_trailing_semicolon = 1;
6761 }
6762 }
6763 | '\n'
6764 {
6765 @$.end_pos = @$.beg_pos;
6766 token_flush(p);
6767 }
6768 ;
6769
6770terms : term
6771 | terms ';' {yyerrok;}
6772 ;
6773
6774none : /* none */
6775 {
6776 $$ = 0;
6777 /*% ripper: Qnil %*/
6778 }
6779 ;
6780%%
6781# undef p
6782# undef yylex
6783# undef yylval
6784# define yylval (*p->lval)
6785
6786static int regx_options(struct parser_params*);
6787static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6788static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6789static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6790static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6791
6792#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6793
6794# define set_yylval_node(x) { \
6795 YYLTYPE _cur_loc; \
6796 rb_parser_set_location(p, &_cur_loc); \
6797 yylval.node = (x); \
6798 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6799}
6800# define set_yylval_str(x) \
6801do { \
6802 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6803 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6804} while(0)
6805# define set_yylval_num(x) { \
6806 yylval.num = (x); \
6807 set_parser_s_value(x); \
6808}
6809# define set_yylval_id(x) (yylval.id = (x))
6810# define set_yylval_name(x) { \
6811 (yylval.id = (x)); \
6812 set_parser_s_value(ID2SYM(x)); \
6813}
6814# define yylval_id() (yylval.id)
6815
6816#define set_yylval_noname() set_yylval_id(keyword_nil)
6817#define has_delayed_token(p) (p->delayed.token != NULL)
6818
6819#ifndef RIPPER
6820#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6821#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6822
6823static bool
6824parser_has_token(struct parser_params *p)
6825{
6826 const char *const pcur = p->lex.pcur;
6827 const char *const ptok = p->lex.ptok;
6828 if (p->keep_tokens && (pcur < ptok)) {
6829 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6830 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6831 }
6832 return pcur > ptok;
6833}
6834
6835static const char *
6836escaped_char(int c)
6837{
6838 switch (c) {
6839 case '"': return "\\\"";
6840 case '\\': return "\\\\";
6841 case '\0': return "\\0";
6842 case '\n': return "\\n";
6843 case '\r': return "\\r";
6844 case '\t': return "\\t";
6845 case '\f': return "\\f";
6846 case '\013': return "\\v";
6847 case '\010': return "\\b";
6848 case '\007': return "\\a";
6849 case '\033': return "\\e";
6850 case '\x7f': return "\\c?";
6851 }
6852 return NULL;
6853}
6854
6855static rb_parser_string_t *
6856rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6857{
6858 rb_encoding *enc = p->enc;
6859 const char *ptr = str->ptr;
6860 const char *pend = ptr + str->len;
6861 const char *prev = ptr;
6862 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6863 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6864
6865 while (ptr < pend) {
6866 unsigned int c;
6867 const char *cc;
6868 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6869 if (!MBCLEN_CHARFOUND_P(n)) {
6870 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6871 n = rb_enc_mbminlen(enc);
6872 if (pend < ptr + n)
6873 n = (int)(pend - ptr);
6874 while (n--) {
6875 c = ((unsigned char)*ptr >> 4) & 0x0f;
6876 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6877 c = *ptr & 0x0f;
6878 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6879 parser_str_cat(result, charbuf, 4);
6880 prev = ++ptr;
6881 }
6882 continue;
6883 }
6884 n = MBCLEN_CHARFOUND_LEN(n);
6885 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6886 ptr += n;
6887 cc = escaped_char(c);
6888 if (cc) {
6889 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6890 parser_str_cat_cstr(result, cc);
6891 prev = ptr;
6892 }
6893 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6894 }
6895 else {
6896 if (ptr - n > prev) {
6897 parser_str_cat(result, prev, ptr - n - prev);
6898 prev = ptr - n;
6899 }
6900 parser_str_cat(result, prev, ptr - prev);
6901 prev = ptr;
6902 }
6903 }
6904 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6905
6906 return result;
6907}
6908
6909static void
6910parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6911{
6912 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6913 token->id = p->token_id;
6914 token->type_name = parser_token2char(p, t);
6915 token->str = str;
6916 token->loc.beg_pos = p->yylloc->beg_pos;
6917 token->loc.end_pos = p->yylloc->end_pos;
6918 rb_parser_ary_push_ast_token(p, p->tokens, token);
6919 p->token_id++;
6920
6921 if (p->debug) {
6922 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6923 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6924 line, token->id, token->type_name, str_escaped->ptr,
6925 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6926 token->loc.end_pos.lineno, token->loc.end_pos.column);
6927 rb_parser_string_free(p, str_escaped);
6928 }
6929}
6930
6931static void
6932parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6933{
6934 debug_token_line(p, "parser_dispatch_scan_event", line);
6935
6936 if (!parser_has_token(p)) return;
6937
6938 RUBY_SET_YYLLOC(*p->yylloc);
6939
6940 if (p->keep_tokens) {
6941 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6942 parser_append_tokens(p, str, t, line);
6943 }
6944
6945 token_flush(p);
6946}
6947
6948#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6949static void
6950parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6951{
6952 debug_token_line(p, "parser_dispatch_delayed_token", line);
6953
6954 if (!has_delayed_token(p)) return;
6955
6956 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6957
6958 if (p->keep_tokens) {
6959 /* p->delayed.token is freed by rb_parser_tokens_free */
6960 parser_append_tokens(p, p->delayed.token, t, line);
6961 }
6962 else {
6963 rb_parser_string_free(p, p->delayed.token);
6964 }
6965
6966 p->delayed.token = NULL;
6967}
6968#else
6969#define literal_flush(p, ptr) ((void)(ptr))
6970
6971static int
6972ripper_has_scan_event(struct parser_params *p)
6973{
6974 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6975 return p->lex.pcur > p->lex.ptok;
6976}
6977
6978static VALUE
6979ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6980{
6981 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6982 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6983 RUBY_SET_YYLLOC(*p->yylloc);
6984 token_flush(p);
6985 return rval;
6986}
6987
6988static void
6989ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6990{
6991 if (!ripper_has_scan_event(p)) return;
6992
6993 set_parser_s_value(ripper_scan_event_val(p, t));
6994}
6995#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6996
6997static void
6998ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6999{
7000 /* save and adjust the location to delayed token for callbacks */
7001 int saved_line = p->ruby_sourceline;
7002 const char *saved_tokp = p->lex.ptok;
7003 VALUE s_value, str;
7004
7005 if (!has_delayed_token(p)) return;
7006 p->ruby_sourceline = p->delayed.beg_line;
7007 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
7008 str = rb_str_new_mutable_parser_string(p->delayed.token);
7009 rb_parser_string_free(p, p->delayed.token);
7010 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
7011 set_parser_s_value(s_value);
7012 p->delayed.token = NULL;
7013 p->ruby_sourceline = saved_line;
7014 p->lex.ptok = saved_tokp;
7015}
7016#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
7017#endif /* RIPPER */
7018
7019static inline int
7020is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
7021{
7022 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
7023}
7024
7025static inline bool
7026peek_word_at(struct parser_params *p, const char *str, size_t len, int at)
7027{
7028 const char *ptr = p->lex.pcur + at;
7029 if (lex_eol_ptr_n_p(p, ptr, len-1)) return false;
7030 if (memcmp(ptr, str, len)) return false;
7031 if (lex_eol_ptr_n_p(p, ptr, len)) return true;
7032 switch (ptr[len]) {
7033 case '!': case '?': return false;
7034 }
7035 return !is_identchar(p, ptr+len, p->lex.pend, p->enc);
7036}
7037
7038static inline int
7039parser_is_identchar(struct parser_params *p)
7040{
7041 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7042}
7043
7044static inline int
7045parser_isascii(struct parser_params *p)
7046{
7047 return ISASCII(*(p->lex.pcur-1));
7048}
7049
7050static void
7051token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7052{
7053 int column = 1, nonspc = 0, i;
7054 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7055 if (*ptr == '\t') {
7056 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7057 }
7058 column++;
7059 if (*ptr != ' ' && *ptr != '\t') {
7060 nonspc = 1;
7061 }
7062 }
7063
7064 ptinfo->beg = loc->beg_pos;
7065 ptinfo->indent = column;
7066 ptinfo->nonspc = nonspc;
7067}
7068
7069static void
7070token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7071{
7072 token_info *ptinfo;
7073
7074 if (!p->token_info_enabled) return;
7075 ptinfo = ALLOC(token_info);
7076 ptinfo->token = token;
7077 ptinfo->next = p->token_info;
7078 token_info_setup(ptinfo, p->lex.pbeg, loc);
7079
7080 p->token_info = ptinfo;
7081}
7082
7083static void
7084token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7085{
7086 token_info *ptinfo_beg = p->token_info;
7087
7088 if (!ptinfo_beg) return;
7089
7090 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7091 token_info_warn(p, token, ptinfo_beg, 1, loc);
7092
7093 p->token_info = ptinfo_beg->next;
7094 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7095}
7096
7097static void
7098token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7099{
7100 token_info *ptinfo_beg = p->token_info;
7101
7102 if (!ptinfo_beg) return;
7103 p->token_info = ptinfo_beg->next;
7104
7105 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7106 ptinfo_beg->beg.column != beg_pos.column ||
7107 strcmp(ptinfo_beg->token, token)) {
7108 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7109 beg_pos.lineno, beg_pos.column, token,
7110 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7111 ptinfo_beg->token);
7112 }
7113
7114 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7115}
7116
7117static void
7118token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7119{
7120 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7121 if (!p->token_info_enabled) return;
7122 if (!ptinfo_beg) return;
7123 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7124 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7125 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7126 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7127 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7128 rb_warn3L(ptinfo_end->beg.lineno,
7129 "mismatched indentations at '%s' with '%s' at %d",
7130 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7131}
7132
7133static int
7134parser_precise_mbclen(struct parser_params *p, const char *ptr)
7135{
7136 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7137 if (!MBCLEN_CHARFOUND_P(len)) {
7138 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7139 return -1;
7140 }
7141 return len;
7142}
7143
7144#ifndef RIPPER
7145static inline void
7146parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7147{
7148 rb_parser_string_t *str;
7149 int lineno = p->ruby_sourceline;
7150 if (!yylloc) {
7151 return;
7152 }
7153 else if (yylloc->beg_pos.lineno == lineno) {
7154 str = p->lex.lastline;
7155 }
7156 else {
7157 return;
7158 }
7159 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7160}
7161
7162static int
7163parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7164{
7165#if 0
7166 YYLTYPE current;
7167
7168 if (!yylloc) {
7169 yylloc = RUBY_SET_YYLLOC(current);
7170 }
7171 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7172 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7173 yylloc = 0;
7174 }
7175#endif
7176 parser_compile_error(p, yylloc, "%s", msg);
7177 parser_show_error_line(p, yylloc);
7178 return 0;
7179}
7180
7181static int
7182parser_yyerror0(struct parser_params *p, const char *msg)
7183{
7184 YYLTYPE current;
7185 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7186}
7187
7188void
7189ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7190{
7191 VALUE mesg;
7192 const int max_line_margin = 30;
7193 const char *ptr, *ptr_end, *pt, *pb;
7194 const char *pre = "", *post = "", *pend;
7195 const char *code = "", *caret = "";
7196 const char *lim;
7197 const char *const pbeg = PARSER_STRING_PTR(str);
7198 char *buf;
7199 long len;
7200 int i;
7201
7202 if (!yylloc) return;
7203 pend = rb_parser_string_end(str);
7204 if (pend > pbeg && pend[-1] == '\n') {
7205 if (--pend > pbeg && pend[-1] == '\r') --pend;
7206 }
7207
7208 pt = pend;
7209 if (lineno == yylloc->end_pos.lineno &&
7210 (pend - pbeg) > yylloc->end_pos.column) {
7211 pt = pbeg + yylloc->end_pos.column;
7212 }
7213
7214 ptr = ptr_end = pt;
7215 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7216 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7217
7218 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7219 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7220
7221 len = ptr_end - ptr;
7222 if (len > 4) {
7223 if (ptr > pbeg) {
7224 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7225 if (ptr > pbeg) pre = "...";
7226 }
7227 if (ptr_end < pend) {
7228 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7229 if (ptr_end < pend) post = "...";
7230 }
7231 }
7232 pb = pbeg;
7233 if (lineno == yylloc->beg_pos.lineno) {
7234 pb += yylloc->beg_pos.column;
7235 if (pb > pt) pb = pt;
7236 }
7237 if (pb < ptr) pb = ptr;
7238 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7239 return;
7240 }
7241 if (RTEST(errbuf)) {
7242 mesg = rb_attr_get(errbuf, idMesg);
7243 if (char_at_end(p, mesg, '\n') != '\n')
7244 rb_str_cat_cstr(mesg, "\n");
7245 }
7246 else {
7247 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7248 }
7249 if (!errbuf && rb_stderr_tty_p()) {
7250#define CSI_BEGIN "\033["
7251#define CSI_SGR "m"
7252 rb_str_catf(mesg,
7253 CSI_BEGIN""CSI_SGR"%s" /* pre */
7254 CSI_BEGIN"1"CSI_SGR"%.*s"
7255 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7256 CSI_BEGIN";1"CSI_SGR"%.*s"
7257 CSI_BEGIN""CSI_SGR"%s" /* post */
7258 "\n",
7259 pre,
7260 (int)(pb - ptr), ptr,
7261 (int)(pt - pb), pb,
7262 (int)(ptr_end - pt), pt,
7263 post);
7264 }
7265 else {
7266 char *p2;
7267
7268 len = ptr_end - ptr;
7269 lim = pt < pend ? pt : pend;
7270 i = (int)(lim - ptr);
7271 buf = ALLOCA_N(char, i+2);
7272 code = ptr;
7273 caret = p2 = buf;
7274 if (ptr <= pb) {
7275 while (ptr < pb) {
7276 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7277 }
7278 *p2++ = '^';
7279 ptr++;
7280 }
7281 if (lim > ptr) {
7282 memset(p2, '~', (lim - ptr));
7283 p2 += (lim - ptr);
7284 }
7285 *p2 = '\0';
7286 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7287 pre, (int)len, code, post,
7288 pre, caret);
7289 }
7290 if (!errbuf) rb_write_error_str(mesg);
7291}
7292#else
7293
7294static int
7295parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7296{
7297 const char *pcur = 0, *ptok = 0;
7298 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7299 p->ruby_sourceline == yylloc->end_pos.lineno) {
7300 pcur = p->lex.pcur;
7301 ptok = p->lex.ptok;
7302 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7303 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7304 }
7305 parser_yyerror0(p, msg);
7306 if (pcur) {
7307 p->lex.ptok = ptok;
7308 p->lex.pcur = pcur;
7309 }
7310 return 0;
7311}
7312
7313static int
7314parser_yyerror0(struct parser_params *p, const char *msg)
7315{
7316 dispatch1(parse_error, STR_NEW2(msg));
7317 ripper_error(p);
7318 return 0;
7319}
7320
7321static inline void
7322parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7323{
7324}
7325#endif /* !RIPPER */
7326
7327static int
7328vtable_size(const struct vtable *tbl)
7329{
7330 if (!DVARS_TERMINAL_P(tbl)) {
7331 return tbl->pos;
7332 }
7333 else {
7334 return 0;
7335 }
7336}
7337
7338static struct vtable *
7339vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7340{
7341 struct vtable *tbl = ALLOC(struct vtable);
7342 tbl->pos = 0;
7343 tbl->capa = 8;
7344 tbl->tbl = ALLOC_N(ID, tbl->capa);
7345 tbl->prev = prev;
7346#ifndef RIPPER
7347 if (p->debug) {
7348 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7349 }
7350#endif
7351 return tbl;
7352}
7353#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7354
7355static void
7356vtable_free_gen(struct parser_params *p, int line, const char *name,
7357 struct vtable *tbl)
7358{
7359#ifndef RIPPER
7360 if (p->debug) {
7361 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7362 }
7363#endif
7364 if (!DVARS_TERMINAL_P(tbl)) {
7365 if (tbl->tbl) {
7366 ruby_xfree_sized(tbl->tbl, tbl->capa * sizeof(ID));
7367 }
7368 ruby_xfree_sized(tbl, sizeof(*tbl));
7369 }
7370}
7371#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7372
7373static void
7374vtable_add_gen(struct parser_params *p, int line, const char *name,
7375 struct vtable *tbl, ID id)
7376{
7377#ifndef RIPPER
7378 if (p->debug) {
7379 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7380 line, name, (void *)tbl, rb_id2name(id));
7381 }
7382#endif
7383 if (DVARS_TERMINAL_P(tbl)) {
7384 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7385 return;
7386 }
7387 if (tbl->pos == tbl->capa) {
7388 tbl->capa = tbl->capa * 2;
7389 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7390 }
7391 tbl->tbl[tbl->pos++] = id;
7392}
7393#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7394
7395static void
7396vtable_pop_gen(struct parser_params *p, int line, const char *name,
7397 struct vtable *tbl, int n)
7398{
7399 if (p->debug) {
7400 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7401 line, name, (void *)tbl, n);
7402 }
7403 if (tbl->pos < n) {
7404 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7405 return;
7406 }
7407 tbl->pos -= n;
7408}
7409#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7410
7411static int
7412vtable_included(const struct vtable * tbl, ID id)
7413{
7414 int i;
7415
7416 if (!DVARS_TERMINAL_P(tbl)) {
7417 for (i = 0; i < tbl->pos; i++) {
7418 if (tbl->tbl[i] == id) {
7419 return i+1;
7420 }
7421 }
7422 }
7423 return 0;
7424}
7425
7426static void parser_prepare(struct parser_params *p);
7427
7428static int
7429e_option_supplied(struct parser_params *p)
7430{
7431 return strcmp(p->ruby_sourcefile, "-e") == 0;
7432}
7433
7434#ifndef RIPPER
7435static NODE *parser_append_options(struct parser_params *p, NODE *node);
7436
7437static VALUE
7438yycompile0(VALUE arg)
7439{
7440 int n;
7441 NODE *tree;
7442 struct parser_params *p = (struct parser_params *)arg;
7443 int cov = FALSE;
7444
7445 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7446 cov = TRUE;
7447 }
7448
7449 if (p->debug_lines) {
7450 p->ast->body.script_lines = p->debug_lines;
7451 }
7452
7453 parser_prepare(p);
7454#define RUBY_DTRACE_PARSE_HOOK(name) \
7455 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7456 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7457 }
7458 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7459 n = yyparse(p);
7460 RUBY_DTRACE_PARSE_HOOK(END);
7461
7462 p->debug_lines = 0;
7463
7464 xfree(p->lex.strterm);
7465 p->lex.strterm = 0;
7466 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7467 if (n || p->error_p) {
7468 VALUE mesg = p->error_buffer;
7469 if (!mesg) {
7470 mesg = syntax_error_new();
7471 }
7472 if (!p->error_tolerant) {
7473 rb_set_errinfo(mesg);
7474 return FALSE;
7475 }
7476 }
7477 tree = p->eval_tree;
7478 if (!tree) {
7479 tree = NEW_NIL(&NULL_LOC);
7480 }
7481 else {
7482 rb_parser_ary_t *tokens = p->tokens;
7483 NODE *prelude;
7484 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7485 prelude = block_append(p, p->eval_tree_begin, body);
7486 RNODE_SCOPE(tree)->nd_body = prelude;
7487 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7488 p->ast->body.coverage_enabled = cov;
7489 if (p->keep_tokens) {
7490 p->ast->node_buffer->tokens = tokens;
7491 p->tokens = NULL;
7492 }
7493 }
7494 p->ast->body.root = tree;
7495 p->ast->body.line_count = p->line_count;
7496 return TRUE;
7497}
7498
7499static rb_ast_t *
7500yycompile(struct parser_params *p, VALUE fname, int line)
7501{
7502 rb_ast_t *ast;
7503 if (NIL_P(fname)) {
7504 p->ruby_sourcefile_string = Qnil;
7505 p->ruby_sourcefile = "(none)";
7506 }
7507 else {
7508 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7509 p->ruby_sourcefile = StringValueCStr(fname);
7510 }
7511 p->ruby_sourceline = line - 1;
7512
7513 p->lvtbl = NULL;
7514
7515 p->ast = ast = rb_ast_new();
7516 compile_callback(yycompile0, (VALUE)p);
7517 ast->body.source_hash = rb_source_hash_finalize(&p->source_hash);
7518 ast->body.has_source_hash = 1;
7519 p->ast = 0;
7520
7521 while (p->lvtbl) {
7522 local_pop(p);
7523 }
7524
7525 return ast;
7526}
7527#endif /* !RIPPER */
7528
7529static rb_encoding *
7530must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7531{
7532 rb_encoding *enc = rb_parser_str_get_encoding(s);
7533 if (!rb_enc_asciicompat(enc)) {
7534 rb_raise(rb_eArgError, "invalid source encoding");
7535 }
7536 return enc;
7537}
7538
7539static rb_parser_string_t *
7540lex_getline(struct parser_params *p)
7541{
7542 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7543 if (!line) return 0;
7544 p->line_count++;
7545 rb_source_hash_update(&p->source_hash, (const uint8_t *)line->ptr, (size_t)line->len);
7546 string_buffer_append(p, line);
7547 must_be_ascii_compatible(p, line);
7548 return line;
7549}
7550
7551#ifndef RIPPER
7552rb_ast_t*
7553rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7554{
7555 p->lex.gets = gets;
7556 p->lex.input = input;
7557 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7558
7559 return yycompile(p, fname, line);
7560}
7561#endif /* !RIPPER */
7562
7563#define STR_FUNC_ESCAPE 0x01
7564#define STR_FUNC_EXPAND 0x02
7565#define STR_FUNC_REGEXP 0x04
7566#define STR_FUNC_QWORDS 0x08
7567#define STR_FUNC_SYMBOL 0x10
7568#define STR_FUNC_INDENT 0x20
7569#define STR_FUNC_LABEL 0x40
7570#define STR_FUNC_LIST 0x4000
7571#define STR_FUNC_TERM 0x8000
7572
7573enum string_type {
7574 str_label = STR_FUNC_LABEL,
7575 str_squote = (0),
7576 str_dquote = (STR_FUNC_EXPAND),
7577 str_xquote = (STR_FUNC_EXPAND),
7578 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7579 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7580 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7581 str_ssym = (STR_FUNC_SYMBOL),
7582 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7583};
7584
7585static rb_parser_string_t *
7586parser_str_new(struct parser_params *p, const char *ptr, rb_len_t len, rb_encoding *enc, int func, rb_encoding *enc0)
7587{
7588 rb_parser_string_t *pstr;
7589
7590 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7591
7592 if (!(func & STR_FUNC_REGEXP)) {
7593 if (rb_parser_is_ascii_string(p, pstr)) {
7594 }
7595 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7596 /* everything is valid in ASCII-8BIT */
7597 enc = rb_ascii8bit_encoding();
7598 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7599 }
7600 }
7601
7602 return pstr;
7603}
7604
7605static int
7606strterm_is_heredoc(rb_strterm_t *strterm)
7607{
7608 return strterm->heredoc;
7609}
7610
7611static rb_strterm_t *
7612new_strterm(struct parser_params *p, int func, int term, int paren)
7613{
7614 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7615 strterm->u.literal.func = func;
7616 strterm->u.literal.term = term;
7617 strterm->u.literal.paren = paren;
7618 return strterm;
7619}
7620
7621static rb_strterm_t *
7622new_heredoc(struct parser_params *p)
7623{
7624 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7625 strterm->heredoc = true;
7626 return strterm;
7627}
7628
7629#define peek(p,c) peek_n(p, (c), 0)
7630#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7631#define peekc(p) peekc_n(p, 0)
7632#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7633
7634#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7635static void
7636parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7637{
7638 debug_token_line(p, "add_delayed_token", line);
7639
7640 if (tok < end) {
7641 if (has_delayed_token(p)) {
7642 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7643 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7644 int end_col = (next_line ? 0 : p->delayed.end_col);
7645 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7646 dispatch_delayed_token(p, tSTRING_CONTENT);
7647 }
7648 }
7649 if (!has_delayed_token(p)) {
7650 p->delayed.token = rb_parser_string_new(p, 0, 0);
7651 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7652 p->delayed.beg_line = p->ruby_sourceline;
7653 p->delayed.beg_col = rb_len2int(tok - p->lex.pbeg);
7654 }
7655 parser_str_cat(p->delayed.token, tok, end - tok);
7656 p->delayed.end_line = p->ruby_sourceline;
7657 p->delayed.end_col = rb_len2int(end - p->lex.pbeg);
7658 p->lex.ptok = end;
7659 }
7660}
7661
7662static void
7663set_lastline(struct parser_params *p, rb_parser_string_t *str)
7664{
7665 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7666 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7667 p->lex.lastline = str;
7668}
7669
7670static int
7671nextline(struct parser_params *p, int set_encoding)
7672{
7673 rb_parser_string_t *str = p->lex.nextline;
7674 p->lex.nextline = 0;
7675 if (!str) {
7676 if (p->eofp)
7677 return -1;
7678
7679 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7680 goto end_of_input;
7681 }
7682
7683 if (!p->lex.input || !(str = lex_getline(p))) {
7684 end_of_input:
7685 p->eofp = 1;
7686 lex_goto_eol(p);
7687 return -1;
7688 }
7689#ifndef RIPPER
7690 if (p->debug_lines) {
7691 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7692 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7693 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7694 }
7695#endif
7696 p->cr_seen = FALSE;
7697 }
7698 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7699 /* after here-document without terminator */
7700 goto end_of_input;
7701 }
7702 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7703 if (p->heredoc_end > 0) {
7704 p->ruby_sourceline = p->heredoc_end;
7705 p->heredoc_end = 0;
7706 }
7707 p->ruby_sourceline++;
7708 set_lastline(p, str);
7709 token_flush(p);
7710 return 0;
7711}
7712
7713static int
7714parser_cr(struct parser_params *p, int c)
7715{
7716 if (peek(p, '\n')) {
7717 p->lex.pcur++;
7718 c = '\n';
7719 }
7720 return c;
7721}
7722
7723static inline int
7724nextc0(struct parser_params *p, int set_encoding)
7725{
7726 int c;
7727
7728 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7729 if (nextline(p, set_encoding)) return -1;
7730 }
7731 c = (unsigned char)*p->lex.pcur++;
7732 if (UNLIKELY(c == '\r')) {
7733 c = parser_cr(p, c);
7734 }
7735
7736 return c;
7737}
7738#define nextc(p) nextc0(p, TRUE)
7739
7740static void
7741pushback(struct parser_params *p, int c)
7742{
7743 if (c == -1) return;
7744 p->eofp = 0;
7745 p->lex.pcur--;
7746 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7747 p->lex.pcur--;
7748 }
7749}
7750
7751static inline bool
7752was_bol(struct parser_params *p)
7753{
7754 switch (p->lex.pcur - p->lex.pbeg) {
7755 case 1:
7756 return true;
7757 case 2:
7758 return *(p->lex.pcur-1) == '\n';
7759 }
7760 return false;
7761}
7762
7763#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7764#define tok(p) (p)->tokenbuf
7765#define toklen(p) (p)->tokidx
7766
7767static int
7768looking_at_eol_p(struct parser_params *p)
7769{
7770 const char *ptr = p->lex.pcur;
7771 while (!lex_eol_ptr_p(p, ptr)) {
7772 int c = (unsigned char)*ptr++;
7773 int eol = (c == '\n' || c == '#');
7774 if (eol || !ISSPACE(c)) {
7775 return eol;
7776 }
7777 }
7778 return TRUE;
7779}
7780
7781static char*
7782newtok(struct parser_params *p)
7783{
7784 p->tokidx = 0;
7785 if (!p->tokenbuf) {
7786 p->toksiz = 60;
7787 p->tokenbuf = ALLOC_N(char, 60);
7788 }
7789 if (p->toksiz > 4096) {
7790 p->toksiz = 60;
7791 REALLOC_N(p->tokenbuf, char, 60);
7792 }
7793 return p->tokenbuf;
7794}
7795
7796static char *
7797tokspace(struct parser_params *p, int n)
7798{
7799 p->tokidx += n;
7800
7801 if (p->tokidx >= p->toksiz) {
7802 do {p->toksiz *= 2;} while (p->toksiz <= p->tokidx);
7803 REALLOC_N(p->tokenbuf, char, p->toksiz);
7804 }
7805 return &p->tokenbuf[p->tokidx-n];
7806}
7807
7808static void
7809tokadd(struct parser_params *p, int c)
7810{
7811 p->tokenbuf[p->tokidx++] = (char)c;
7812 if (p->tokidx >= p->toksiz) {
7813 p->toksiz *= 2;
7814 REALLOC_N(p->tokenbuf, char, p->toksiz);
7815 }
7816}
7817
7818static int
7819tok_hex(struct parser_params *p, size_t *numlen)
7820{
7821 int c;
7822
7823 c = (int)ruby_scan_digits(p->lex.pcur, 2, 16, numlen, &c);
7824 if (!*numlen) {
7825 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7826 yyerror0("invalid hex escape");
7827 dispatch_scan_event(p, tSTRING_CONTENT);
7828 return 0;
7829 }
7830 p->lex.pcur += *numlen;
7831 return c;
7832}
7833
7834#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7835
7836static int
7837escaped_control_code(int c)
7838{
7839 int c2 = 0;
7840 switch (c) {
7841 case ' ':
7842 c2 = 's';
7843 break;
7844 case '\n':
7845 c2 = 'n';
7846 break;
7847 case '\t':
7848 c2 = 't';
7849 break;
7850 case '\v':
7851 c2 = 'v';
7852 break;
7853 case '\r':
7854 c2 = 'r';
7855 break;
7856 case '\f':
7857 c2 = 'f';
7858 break;
7859 }
7860 return c2;
7861}
7862
7863#define WARN_SPACE_CHAR(c, prefix) \
7864 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7865
7866static int
7867tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7868 int regexp_literal, const char *begin)
7869{
7870 const int wide = !begin;
7871 size_t numlen = wide ? p->lex.pend - p->lex.pcur : 4;
7872 int ov, codepoint = (int)ruby_scan_digits(p->lex.pcur, numlen, 16, &numlen, &ov);
7873
7874 p->lex.pcur += numlen;
7875 if (p->lex.strterm == NULL ||
7876 strterm_is_heredoc(p->lex.strterm) ||
7877 (p->lex.strterm->u.literal.func != str_regexp)) {
7878 if (!begin) begin = p->lex.pcur;
7879 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7880 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7881 yyerror0("invalid Unicode escape");
7882 dispatch_scan_event(p, tSTRING_CONTENT);
7883 return wide && numlen > 0;
7884 }
7885 if (codepoint > 0x10ffff) {
7886 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7887 yyerror0("invalid Unicode codepoint (too large)");
7888 dispatch_scan_event(p, tSTRING_CONTENT);
7889 return wide;
7890 }
7891 if ((codepoint & 0xfffff800) == 0xd800) {
7892 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7893 yyerror0("invalid Unicode codepoint");
7894 dispatch_scan_event(p, tSTRING_CONTENT);
7895 return wide;
7896 }
7897 }
7898 if (regexp_literal) {
7899 tokcopy(p, (int)numlen);
7900 }
7901 else if (codepoint >= 0x80) {
7902 rb_encoding *utf8 = rb_utf8_encoding();
7903 if (*encp && utf8 != *encp) {
7904 YYLTYPE loc = RUBY_INIT_YYLLOC();
7905 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7906 parser_show_error_line(p, &loc);
7907 return wide;
7908 }
7909 *encp = utf8;
7910 tokaddmbc(p, codepoint, *encp);
7911 }
7912 else {
7913 tokadd(p, codepoint);
7914 }
7915 return TRUE;
7916}
7917
7918static int tokadd_mbchar(struct parser_params *p, int c);
7919
7920static int
7921tokskip_mbchar(struct parser_params *p)
7922{
7923 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7924 if (len > 0) {
7925 p->lex.pcur += len - 1;
7926 }
7927 return len;
7928}
7929
7930/* return value is for ?\u3042 */
7931static void
7932tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7933 int term, int symbol_literal, int regexp_literal)
7934{
7935 /*
7936 * If `term` is not -1, then we allow multiple codepoints in \u{}
7937 * upto `term` byte, otherwise we're parsing a character literal.
7938 * And then add the codepoints to the current token.
7939 */
7940 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7941
7942 const int open_brace = '{', close_brace = '}';
7943
7944 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7945
7946 if (peek(p, open_brace)) { /* handle \u{...} form */
7947 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7948 /*
7949 * Skip parsing validation code and copy bytes as-is until term or
7950 * closing brace, in order to correctly handle extended regexps where
7951 * invalid unicode escapes are allowed in comments. The regexp parser
7952 * does its own validation and will catch any issues.
7953 */
7954 tokadd(p, open_brace);
7955 ++p->lex.pcur;
7956 while (!lex_eol_p(p)) {
7957 int c = peekc(p);
7958 if (c == close_brace) {
7959 tokadd(p, c);
7960 ++p->lex.pcur;
7961 break;
7962 }
7963 else if (c == term) {
7964 break;
7965 }
7966 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7967 tokadd(p, c);
7968 c = *++p->lex.pcur;
7969 }
7970 ++p->lex.pcur;
7971 tokadd_mbchar(p, c);
7972 }
7973 }
7974 else {
7975 const char *second = NULL;
7976 int c, last = nextc(p);
7977 if (lex_eol_p(p)) goto unterminated;
7978 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7979 while (c != close_brace) {
7980 if (c == term) goto unterminated;
7981 if (second == multiple_codepoints)
7982 second = p->lex.pcur;
7983 if (regexp_literal) tokadd(p, last);
7984 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7985 break;
7986 }
7987 while (ISSPACE(c = peekc(p))) {
7988 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7989 last = c;
7990 }
7991 if (term == -1 && !second)
7992 second = multiple_codepoints;
7993 }
7994
7995 if (c != close_brace) {
7996 unterminated:
7997 flush_string_content(p, rb_utf8_encoding(), 0);
7998 yyerror0("unterminated Unicode escape");
7999 dispatch_scan_event(p, tSTRING_CONTENT);
8000 return;
8001 }
8002 if (second && second != multiple_codepoints) {
8003 const char *pcur = p->lex.pcur;
8004 p->lex.pcur = second;
8005 dispatch_scan_event(p, tSTRING_CONTENT);
8006 token_flush(p);
8007 p->lex.pcur = pcur;
8008 yyerror0(multiple_codepoints);
8009 token_flush(p);
8010 }
8011
8012 if (regexp_literal) tokadd(p, close_brace);
8013 nextc(p);
8014 }
8015 }
8016 else { /* handle \uxxxx form */
8017 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
8018 token_flush(p);
8019 return;
8020 }
8021 }
8022}
8023
8024#define ESCAPE_CONTROL 1
8025#define ESCAPE_META 2
8026
8027static int
8028read_escape(struct parser_params *p, int flags, const char *begin)
8029{
8030 int c;
8031 size_t numlen;
8032
8033 switch (c = nextc(p)) {
8034 case '\\': /* Backslash */
8035 return c;
8036
8037 case 'n': /* newline */
8038 return '\n';
8039
8040 case 't': /* horizontal tab */
8041 return '\t';
8042
8043 case 'r': /* carriage-return */
8044 return '\r';
8045
8046 case 'f': /* form-feed */
8047 return '\f';
8048
8049 case 'v': /* vertical tab */
8050 return '\13';
8051
8052 case 'a': /* alarm(bell) */
8053 return '\007';
8054
8055 case 'e': /* escape */
8056 return 033;
8057
8058 case '0': case '1': case '2': case '3': /* octal constant */
8059 case '4': case '5': case '6': case '7':
8060 pushback(p, c);
8061 int ov;
8062 c = (int)ruby_scan_digits(p->lex.pcur, 3, 8, &numlen, &ov);
8063 p->lex.pcur += numlen;
8064 return c;
8065
8066 case 'x': /* hex constant */
8067 c = tok_hex(p, &numlen);
8068 if (numlen == 0) return 0;
8069 return c;
8070
8071 case 'b': /* backspace */
8072 return '\010';
8073
8074 case 's': /* space */
8075 return ' ';
8076
8077 case 'M':
8078 if (flags & ESCAPE_META) goto eof;
8079 if ((c = nextc(p)) != '-') {
8080 goto eof;
8081 }
8082 if ((c = nextc(p)) == '\\') {
8083 switch (peekc(p)) {
8084 case 'u': case 'U':
8085 nextc(p);
8086 goto eof;
8087 }
8088 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8089 }
8090 else if (c == -1) goto eof;
8091 else if (!ISASCII(c)) {
8092 tokskip_mbchar(p);
8093 goto eof;
8094 }
8095 else {
8096 int c2 = escaped_control_code(c);
8097 if (c2) {
8098 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8099 WARN_SPACE_CHAR(c2, "\\M-");
8100 }
8101 else {
8102 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8103 }
8104 }
8105 else if (ISCNTRL(c)) goto eof;
8106 return ((c & 0xff) | 0x80);
8107 }
8108
8109 case 'C':
8110 if ((c = nextc(p)) != '-') {
8111 goto eof;
8112 }
8113 case 'c':
8114 if (flags & ESCAPE_CONTROL) goto eof;
8115 if ((c = nextc(p))== '\\') {
8116 switch (peekc(p)) {
8117 case 'u': case 'U':
8118 nextc(p);
8119 goto eof;
8120 }
8121 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8122 }
8123 else if (c == '?')
8124 return 0177;
8125 else if (c == -1) goto eof;
8126 else if (!ISASCII(c)) {
8127 tokskip_mbchar(p);
8128 goto eof;
8129 }
8130 else {
8131 int c2 = escaped_control_code(c);
8132 if (c2) {
8133 if (ISCNTRL(c)) {
8134 if (flags & ESCAPE_META) {
8135 WARN_SPACE_CHAR(c2, "\\M-");
8136 }
8137 else {
8138 WARN_SPACE_CHAR(c2, "");
8139 }
8140 }
8141 else {
8142 if (flags & ESCAPE_META) {
8143 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8144 }
8145 else {
8146 WARN_SPACE_CHAR(c2, "\\C-");
8147 }
8148 }
8149 }
8150 else if (ISCNTRL(c)) goto eof;
8151 }
8152 return c & 0x9f;
8153
8154 eof:
8155 case -1:
8156 flush_string_content(p, p->enc, p->lex.pcur - begin);
8157 yyerror0("Invalid escape character syntax");
8158 dispatch_scan_event(p, tSTRING_CONTENT);
8159 return '\0';
8160
8161 default:
8162 if (!ISASCII(c)) {
8163 tokskip_mbchar(p);
8164 goto eof;
8165 }
8166 return c;
8167 }
8168}
8169
8170static void
8171tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8172{
8173 int len = rb_enc_codelen(c, enc);
8174 rb_enc_mbcput(c, tokspace(p, len), enc);
8175}
8176
8177static int
8178tokadd_escape(struct parser_params *p)
8179{
8180 int c;
8181 size_t numlen;
8182 const char *begin = p->lex.pcur;
8183
8184 switch (c = nextc(p)) {
8185 case '\n':
8186 return 0; /* just ignore */
8187
8188 case '0': case '1': case '2': case '3': /* octal constant */
8189 case '4': case '5': case '6': case '7':
8190 {
8191 int ov;
8192 ruby_scan_digits(--p->lex.pcur, 3, 8, &numlen, &ov);
8193 if (numlen == 0) goto eof;
8194 p->lex.pcur += numlen;
8195 tokcopy(p, (int)numlen + 1);
8196 }
8197 return 0;
8198
8199 case 'x': /* hex constant */
8200 {
8201 tok_hex(p, &numlen);
8202 if (numlen == 0) return -1;
8203 tokcopy(p, (int)numlen + 2);
8204 }
8205 return 0;
8206
8207 eof:
8208 case -1:
8209 flush_string_content(p, p->enc, p->lex.pcur - begin);
8210 yyerror0("Invalid escape character syntax");
8211 token_flush(p);
8212 return -1;
8213
8214 default:
8215 tokadd(p, '\\');
8216 tokadd(p, c);
8217 }
8218 return 0;
8219}
8220
8221static int
8222char_to_option(int c)
8223{
8224 int val;
8225
8226 switch (c) {
8227 case 'i':
8228 val = RE_ONIG_OPTION_IGNORECASE;
8229 break;
8230 case 'x':
8231 val = RE_ONIG_OPTION_EXTEND;
8232 break;
8233 case 'm':
8234 val = RE_ONIG_OPTION_MULTILINE;
8235 break;
8236 default:
8237 val = 0;
8238 break;
8239 }
8240 return val;
8241}
8242
8243#define ARG_ENCODING_FIXED 16
8244#define ARG_ENCODING_NONE 32
8245#define ENC_ASCII8BIT 1
8246#define ENC_EUC_JP 2
8247#define ENC_Windows_31J 3
8248#define ENC_UTF8 4
8249
8250static int
8251char_to_option_kcode(int c, int *option, int *kcode)
8252{
8253 *option = 0;
8254
8255 switch (c) {
8256 case 'n':
8257 *kcode = ENC_ASCII8BIT;
8258 return (*option = ARG_ENCODING_NONE);
8259 case 'e':
8260 *kcode = ENC_EUC_JP;
8261 break;
8262 case 's':
8263 *kcode = ENC_Windows_31J;
8264 break;
8265 case 'u':
8266 *kcode = ENC_UTF8;
8267 break;
8268 default:
8269 *kcode = -1;
8270 return (*option = char_to_option(c));
8271 }
8272 *option = ARG_ENCODING_FIXED;
8273 return 1;
8274}
8275
8276static int
8277regx_options(struct parser_params *p)
8278{
8279 int kcode = 0;
8280 int kopt = 0;
8281 int options = 0;
8282 int c, opt, kc;
8283
8284 newtok(p);
8285 while (c = nextc(p), ISALPHA(c)) {
8286 if (c == 'o') {
8287 options |= RE_OPTION_ONCE;
8288 }
8289 else if (char_to_option_kcode(c, &opt, &kc)) {
8290 if (kc >= 0) {
8291 if (kc != ENC_ASCII8BIT) kcode = c;
8292 kopt = opt;
8293 }
8294 else {
8295 options |= opt;
8296 }
8297 }
8298 else {
8299 tokadd(p, c);
8300 }
8301 }
8302 options |= kopt;
8303 pushback(p, c);
8304 if (toklen(p)) {
8305 YYLTYPE loc = RUBY_INIT_YYLLOC();
8306 tokfix(p);
8307 compile_error(p, "unknown regexp option%s - %*s",
8308 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8309 parser_show_error_line(p, &loc);
8310 }
8311 return options | RE_OPTION_ENCODING(kcode);
8312}
8313
8314static int
8315tokadd_mbchar(struct parser_params *p, int c)
8316{
8317 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8318 if (len < 0) return -1;
8319 tokadd(p, c);
8320 p->lex.pcur += --len;
8321 if (len > 0) tokcopy(p, len);
8322 return c;
8323}
8324
8325static inline int
8326simple_re_meta(int c)
8327{
8328 switch (c) {
8329 case '$': case '*': case '+': case '.':
8330 case '?': case '^': case '|':
8331 case ')': case ']': case '}': case '>':
8332 return TRUE;
8333 default:
8334 return FALSE;
8335 }
8336}
8337
8338static int
8339parser_update_heredoc_indent(struct parser_params *p, int c)
8340{
8341 if (p->heredoc_line_indent == -1) {
8342 if (c == '\n') p->heredoc_line_indent = 0;
8343 }
8344 else {
8345 if (c == ' ') {
8346 p->heredoc_line_indent++;
8347 return TRUE;
8348 }
8349 else if (c == '\t') {
8350 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8351 p->heredoc_line_indent = w * TAB_WIDTH;
8352 return TRUE;
8353 }
8354 else if (c != '\n') {
8355 if (p->heredoc_indent > p->heredoc_line_indent) {
8356 p->heredoc_indent = p->heredoc_line_indent;
8357 }
8358 p->heredoc_line_indent = -1;
8359 }
8360 else {
8361 /* Whitespace only line has no indentation */
8362 p->heredoc_line_indent = 0;
8363 }
8364 }
8365 return FALSE;
8366}
8367
8368static void
8369parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8370{
8371 YYLTYPE loc = RUBY_INIT_YYLLOC();
8372 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8373 compile_error(p, "%s mixed within %s source", n1, n2);
8374 parser_show_error_line(p, &loc);
8375}
8376
8377static void
8378parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8379{
8380 const char *pos = p->lex.pcur;
8381 p->lex.pcur = beg;
8382 parser_mixed_error(p, enc1, enc2);
8383 p->lex.pcur = pos;
8384}
8385
8386static inline char
8387nibble_char_upper(unsigned int c)
8388{
8389 c &= 0xf;
8390 return c + (c < 10 ? '0' : 'A' - 10);
8391}
8392
8393static int
8394tokadd_string(struct parser_params *p,
8395 int func, int term, int paren, long *nest,
8396 rb_encoding **encp, rb_encoding **enc)
8397{
8398 int c;
8399 bool erred = false;
8400#ifdef RIPPER
8401 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8402 int top_of_line = FALSE;
8403#endif
8404
8405#define mixed_error(enc1, enc2) \
8406 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8407#define mixed_escape(beg, enc1, enc2) \
8408 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8409
8410 while ((c = nextc(p)) != -1) {
8411 if (p->heredoc_indent > 0) {
8412 parser_update_heredoc_indent(p, c);
8413 }
8414#ifdef RIPPER
8415 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8416 pushback(p, c);
8417 break;
8418 }
8419#endif
8420
8421 if (paren && c == paren) {
8422 ++*nest;
8423 }
8424 else if (c == term) {
8425 if (!nest || !*nest) {
8426 pushback(p, c);
8427 break;
8428 }
8429 --*nest;
8430 }
8431 else if ((func & STR_FUNC_EXPAND) && c == '#' && parser_variable_name_p(p)) {
8432 pushback(p, c);
8433 break;
8434 }
8435 else if (c == '\\') {
8436 c = nextc(p);
8437 switch (c) {
8438 case '\n':
8439 if (func & STR_FUNC_QWORDS) break;
8440 if (func & STR_FUNC_EXPAND) {
8441 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8442 continue;
8443 if (c == term) {
8444 c = '\\';
8445 goto terminate;
8446 }
8447 }
8448 tokadd(p, '\\');
8449 break;
8450
8451 case '\\':
8452 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8453 break;
8454
8455 case 'u':
8456 if ((func & STR_FUNC_EXPAND) == 0) {
8457 tokadd(p, '\\');
8458 break;
8459 }
8460 tokadd_utf8(p, enc, term,
8461 func & STR_FUNC_SYMBOL,
8462 func & STR_FUNC_REGEXP);
8463 continue;
8464
8465 default:
8466 if (c == -1) return -1;
8467 if (!ISASCII(c)) {
8468 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8469 goto non_ascii;
8470 }
8471 if (func & STR_FUNC_REGEXP) {
8472 switch (c) {
8473 case 'c':
8474 case 'C':
8475 case 'M': {
8476 pushback(p, c);
8477 c = read_escape(p, 0, p->lex.pcur - 1);
8478
8479 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8480 *t++ = '\\';
8481 *t++ = 'x';
8482 *t++ = nibble_char_upper(c >> 4);
8483 *t++ = nibble_char_upper(c);
8484 continue;
8485 }
8486 }
8487
8488 if (c == term && !simple_re_meta(c)) {
8489 tokadd(p, c);
8490 continue;
8491 }
8492 pushback(p, c);
8493 if ((c = tokadd_escape(p)) < 0)
8494 return -1;
8495 if (*enc && *enc != *encp) {
8496 mixed_escape(p->lex.ptok+2, *enc, *encp);
8497 }
8498 continue;
8499 }
8500 else if (func & STR_FUNC_EXPAND) {
8501 pushback(p, c);
8502 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8503 c = read_escape(p, 0, p->lex.pcur - 1);
8504 }
8505 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8506 /* ignore backslashed spaces in %w */
8507 }
8508 else if (c != term && !(paren && c == paren)) {
8509 tokadd(p, '\\');
8510 pushback(p, c);
8511 continue;
8512 }
8513 }
8514 }
8515 else if (!parser_isascii(p)) {
8516 non_ascii:
8517 if (!*enc) {
8518 *enc = *encp;
8519 }
8520 else if (*enc != *encp) {
8521 mixed_error(*enc, *encp);
8522 continue;
8523 }
8524 if (tokadd_mbchar(p, c) == -1) return -1;
8525 continue;
8526 }
8527 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8528 pushback(p, c);
8529 break;
8530 }
8531 if (c & 0x80) {
8532 if (!*enc) {
8533 *enc = *encp;
8534 }
8535 else if (*enc != *encp) {
8536 mixed_error(*enc, *encp);
8537 continue;
8538 }
8539 }
8540 tokadd(p, c);
8541#ifdef RIPPER
8542 top_of_line = (c == '\n');
8543#endif
8544 }
8545 terminate:
8546 if (*enc) *encp = *enc;
8547 return c;
8548}
8549
8550#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8551
8552static void
8553flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8554{
8555 p->lex.pcur -= back;
8556 if (has_delayed_token(p)) {
8557 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8558 if (len > 0) {
8559 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8560 p->delayed.end_line = p->ruby_sourceline;
8561 p->delayed.end_col = rb_len2int(p->lex.pcur - p->lex.pbeg);
8562 }
8563 dispatch_delayed_token(p, tSTRING_CONTENT);
8564 p->lex.ptok = p->lex.pcur;
8565 }
8566 dispatch_scan_event(p, tSTRING_CONTENT);
8567 p->lex.pcur += back;
8568}
8569
8570/* this can be shared with ripper, since it's independent from struct
8571 * parser_params. */
8572#ifndef RIPPER
8573#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8574#define SPECIAL_PUNCT(idx) ( \
8575 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8576 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8577 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8578 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8579 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8580 BIT('0', idx))
8581const uint_least32_t ruby_global_name_punct_bits[] = {
8582 SPECIAL_PUNCT(0),
8583 SPECIAL_PUNCT(1),
8584 SPECIAL_PUNCT(2),
8585};
8586#undef BIT
8587#undef SPECIAL_PUNCT
8588#endif
8589
8590static enum yytokentype
8591parser_peek_variable_name(struct parser_params *p)
8592{
8593 enum yytokentype t = parser_variable_name_p(p);
8594 if (t == tSTRING_DBEG) {
8595 p->lex.pcur++;
8596 p->command_start = TRUE;
8597 yylval.state = p->lex.state;
8598 }
8599 return t;
8600}
8601
8602static enum yytokentype
8603parser_variable_name_p(const struct parser_params *p)
8604{
8605 int c;
8606 const char *ptr = p->lex.pcur;
8607
8608 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8609 c = *ptr++;
8610 switch (c) {
8611 case '$':
8612 if ((c = *ptr) == '-') {
8613 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8614 c = *ptr;
8615 }
8616 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8617 return tSTRING_DVAR;
8618 }
8619 break;
8620 case '@':
8621 if ((c = *ptr) == '@') {
8622 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8623 c = *ptr;
8624 }
8625 break;
8626 case '{':
8627 return tSTRING_DBEG;
8628 default:
8629 return 0;
8630 }
8631 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8632 return tSTRING_DVAR;
8633 return 0;
8634}
8635
8636#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8637#define IS_END() IS_lex_state(EXPR_END_ANY)
8638#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8639#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8640#define IS_LABEL_POSSIBLE() (\
8641 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8642 IS_ARG())
8643#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8644#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8645
8646static inline enum yytokentype
8647parser_string_term(struct parser_params *p, int func)
8648{
8649 xfree(p->lex.strterm);
8650 p->lex.strterm = 0;
8651 if (func & STR_FUNC_REGEXP) {
8652 set_yylval_num(regx_options(p));
8653 dispatch_scan_event(p, tREGEXP_END);
8654 SET_LEX_STATE(EXPR_END);
8655 return tREGEXP_END;
8656 }
8657 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8658 nextc(p);
8659 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8660 return tLABEL_END;
8661 }
8662 SET_LEX_STATE(EXPR_END);
8663 return tSTRING_END;
8664}
8665
8666static enum yytokentype
8667parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8668{
8669 int func = quote->func;
8670 int term = quote->term;
8671 int paren = quote->paren;
8672 int c, space = 0;
8673 rb_encoding *enc = p->enc;
8674 rb_encoding *base_enc = 0;
8675 rb_parser_string_t *lit;
8676
8677 if (func & STR_FUNC_TERM) {
8678 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8679 SET_LEX_STATE(EXPR_END);
8680 xfree(p->lex.strterm);
8681 p->lex.strterm = 0;
8682 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8683 }
8684 c = nextc(p);
8685 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8686 while (c != '\n' && ISSPACE(c = nextc(p)));
8687 space = 1;
8688 }
8689 if (func & STR_FUNC_LIST) {
8690 quote->func &= ~STR_FUNC_LIST;
8691 space = 1;
8692 }
8693 if (c == term && !quote->nest) {
8694 if (func & STR_FUNC_QWORDS) {
8695 quote->func |= STR_FUNC_TERM;
8696 pushback(p, c); /* dispatch the term at tSTRING_END */
8697 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8698 return ' ';
8699 }
8700 return parser_string_term(p, func);
8701 }
8702 if (space) {
8703 if (!ISSPACE(c)) pushback(p, c);
8704 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8705 return ' ';
8706 }
8707 newtok(p);
8708 if ((func & STR_FUNC_EXPAND) && c == '#') {
8709 enum yytokentype t = parser_peek_variable_name(p);
8710 if (t) return t;
8711 tokadd(p, '#');
8712 c = nextc(p);
8713 }
8714 pushback(p, c);
8715 if (tokadd_string(p, func, term, paren, &quote->nest,
8716 &enc, &base_enc) == -1) {
8717 if (p->eofp) {
8718#ifndef RIPPER
8719# define unterminated_literal(mesg) yyerror0(mesg)
8720#else
8721# define unterminated_literal(mesg) compile_error(p, mesg)
8722#endif
8723 literal_flush(p, p->lex.pcur);
8724 if (func & STR_FUNC_QWORDS) {
8725 /* no content to add, bailing out here */
8726 unterminated_literal("unterminated list meets end of file");
8727 xfree(p->lex.strterm);
8728 p->lex.strterm = 0;
8729 return tSTRING_END;
8730 }
8731 if (func & STR_FUNC_REGEXP) {
8732 unterminated_literal("unterminated regexp meets end of file");
8733 }
8734 else {
8735 unterminated_literal("unterminated string meets end of file");
8736 }
8737 quote->func |= STR_FUNC_TERM;
8738 }
8739 }
8740
8741 tokfix(p);
8742 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8743 set_yylval_str(lit);
8744 flush_string_content(p, enc, 0);
8745
8746 return tSTRING_CONTENT;
8747}
8748
8749static enum yytokentype
8750heredoc_identifier(struct parser_params *p)
8751{
8752 /*
8753 * term_len is length of `<<"END"` except `END`,
8754 * in this case term_len is 4 (<, <, " and ").
8755 */
8756 rb_len_t len, offset = p->lex.pcur - p->lex.pbeg;
8757 int c = nextc(p), term, func = 0, quote = 0;
8758 enum yytokentype token = tSTRING_BEG;
8759 int indent = 0;
8760
8761 if (c == '-') {
8762 c = nextc(p);
8763 func = STR_FUNC_INDENT;
8764 offset++;
8765 }
8766 else if (c == '~') {
8767 c = nextc(p);
8768 func = STR_FUNC_INDENT;
8769 offset++;
8770 indent = INT_MAX;
8771 }
8772 switch (c) {
8773 case '\'':
8774 func |= str_squote; goto quoted;
8775 case '"':
8776 func |= str_dquote; goto quoted;
8777 case '`':
8778 token = tXSTRING_BEG;
8779 func |= str_xquote; goto quoted;
8780
8781 quoted:
8782 quote++;
8783 offset++;
8784 term = c;
8785 len = 0;
8786 while ((c = nextc(p)) != term) {
8787 if (c == -1 || c == '\r' || c == '\n') {
8788 yyerror0("unterminated here document identifier");
8789 return -1;
8790 }
8791 }
8792 break;
8793
8794 default:
8795 if (!parser_is_identchar(p)) {
8796 pushback(p, c);
8797 if (func & STR_FUNC_INDENT) {
8798 pushback(p, indent > 0 ? '~' : '-');
8799 }
8800 return 0;
8801 }
8802 func |= str_dquote;
8803 do {
8804 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8805 if (n < 0) return 0;
8806 p->lex.pcur += --n;
8807 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8808 pushback(p, c);
8809 break;
8810 }
8811
8812 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8813 if ((rb_ulen_t)len >= HERETERM_LENGTH_MAX)
8814 yyerror0("too long here document identifier");
8815 dispatch_scan_event(p, tHEREDOC_BEG);
8816 lex_goto_eol(p);
8817
8818 p->lex.strterm = new_heredoc(p);
8819 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8820 here->offset = offset + (p->lex.pbeg - PARSER_STRING_PTR(p->lex.lastline));
8821 here->sourceline = p->ruby_sourceline;
8822 here->length = (unsigned)len;
8823 here->quote = quote;
8824 here->func = func;
8825 here->lastline = p->lex.lastline;
8826
8827 token_flush(p);
8828 p->heredoc_indent = indent;
8829 p->heredoc_line_indent = 0;
8830 return token;
8831}
8832
8833static void
8834heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8835{
8836 rb_parser_string_t *line;
8837 rb_strterm_t *term = p->lex.strterm;
8838
8839 p->lex.strterm = 0;
8840 line = here->lastline;
8841 p->lex.lastline = line;
8842 p->lex.pbeg = PARSER_STRING_PTR(line);
8843 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8844 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8845 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8846 p->heredoc_end = p->ruby_sourceline;
8847 p->ruby_sourceline = (int)here->sourceline;
8848 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8849 p->eofp = 0;
8850 xfree(term);
8851}
8852
8853static int
8854dedent_string_column(const char *str, rb_len_t len, int width)
8855{
8856 int i, col = 0;
8857
8858 for (i = 0; i < len && col < width; i++) {
8859 if (str[i] == ' ') {
8860 col++;
8861 }
8862 else if (str[i] == '\t') {
8863 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8864 if (n > width) break;
8865 col = n;
8866 }
8867 else {
8868 break;
8869 }
8870 }
8871
8872 return i;
8873}
8874
8875static int
8876dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8877{
8878 char *str;
8879 rb_len_t len;
8880 int i;
8881
8882 len = PARSER_STRING_LEN(string);
8883 str = PARSER_STRING_PTR(string);
8884
8885 i = dedent_string_column(str, len, width);
8886 if (!i) return 0;
8887
8888 rb_parser_str_modify(string);
8889 str = PARSER_STRING_PTR(string);
8890 if (PARSER_STRING_LEN(string) != len)
8891 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8892 MEMMOVE(str, str + i, char, len - i);
8893 rb_parser_str_set_len(p, string, len - i);
8894 return i;
8895}
8896
8897static NODE *
8898heredoc_dedent(struct parser_params *p, NODE *root)
8899{
8900 NODE *node, *str_node, *prev_node;
8901 int indent = p->heredoc_indent;
8902 rb_parser_string_t *prev_lit = 0;
8903
8904 if (indent <= 0) return root;
8905 if (!root) return root;
8906
8907 prev_node = node = str_node = root;
8908 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8909
8910 while (str_node) {
8911 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8912 if (nd_fl_newline(str_node)) {
8913 dedent_string(p, lit, indent);
8914 }
8915 if (!prev_lit) {
8916 prev_lit = lit;
8917 }
8918 else if (!literal_concat0(p, prev_lit, lit)) {
8919 return 0;
8920 }
8921 else {
8922 NODE *end = RNODE_LIST(node)->as.nd_end;
8923 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8924 if (!node) {
8925 if (nd_type_p(prev_node, NODE_DSTR))
8926 nd_set_type(prev_node, NODE_STR);
8927 break;
8928 }
8929 RNODE_LIST(node)->as.nd_end = end;
8930 goto next_str;
8931 }
8932
8933 str_node = 0;
8934 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8935 next_str:
8936 if (!nd_type_p(node, NODE_LIST)) break;
8937 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8938 enum node_type type = nd_type(str_node);
8939 if (type == NODE_STR || type == NODE_DSTR) break;
8940 prev_lit = 0;
8941 str_node = 0;
8942 }
8943 }
8944 }
8945 return root;
8946}
8947
8948static int
8949whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8950{
8951 const char *beg = p->lex.pbeg;
8952 const char *ptr = p->lex.pend;
8953
8954 if (ptr - beg < len) return FALSE;
8955 if (ptr > beg && ptr[-1] == '\n') {
8956 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8957 if (ptr - beg < len) return FALSE;
8958 }
8959 if (strncmp(eos, ptr -= len, len)) return FALSE;
8960 if (indent) {
8961 while (beg < ptr && ISSPACE(*beg)) beg++;
8962 }
8963 return beg == ptr;
8964}
8965
8966static int
8967word_match_p(struct parser_params *p, const char *word, long len)
8968{
8969 if (strncmp(p->lex.pcur, word, len)) return 0;
8970 if (lex_eol_n_p(p, len)) return 1;
8971 int c = (unsigned char)p->lex.pcur[len];
8972 if (ISSPACE(c)) return 1;
8973 switch (c) {
8974 case '\0': case '\004': case '\032': return 1;
8975 }
8976 return 0;
8977}
8978
8979#define NUM_SUFFIX_R (1<<0)
8980#define NUM_SUFFIX_I (1<<1)
8981#define NUM_SUFFIX_ALL 3
8982
8983static int
8984number_literal_suffix(struct parser_params *p, int mask)
8985{
8986 int c, result = 0;
8987 const char *lastp = p->lex.pcur;
8988
8989 while ((c = nextc(p)) != -1) {
8990 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8991 result |= (mask & NUM_SUFFIX_I);
8992 mask &= ~NUM_SUFFIX_I;
8993 /* r after i, rational of complex is disallowed */
8994 mask &= ~NUM_SUFFIX_R;
8995 continue;
8996 }
8997 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8998 result |= (mask & NUM_SUFFIX_R);
8999 mask &= ~NUM_SUFFIX_R;
9000 continue;
9001 }
9002 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
9003 p->lex.pcur = lastp;
9004 return 0;
9005 }
9006 pushback(p, c);
9007 break;
9008 }
9009 return result;
9010}
9011
9012static enum yytokentype
9013set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
9014{
9015 enum rb_numeric_type numeric_type = integer_literal;
9016
9017 if (type == tFLOAT) {
9018 numeric_type = float_literal;
9019 }
9020
9021 if (suffix & NUM_SUFFIX_R) {
9022 type = tRATIONAL;
9023 numeric_type = rational_literal;
9024 }
9025 if (suffix & NUM_SUFFIX_I) {
9026 type = tIMAGINARY;
9027 }
9028
9029 switch (type) {
9030 case tINTEGER:
9031 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
9032 break;
9033 case tFLOAT:
9034 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
9035 break;
9036 case tRATIONAL:
9037 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
9038 break;
9039 case tIMAGINARY:
9040 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
9041 (void)numeric_type; /* for ripper */
9042 break;
9043 default:
9044 rb_bug("unexpected token: %d", type);
9045 }
9046 SET_LEX_STATE(EXPR_END);
9047 return type;
9048}
9049
9050#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
9051static void
9052parser_dispatch_heredoc_end(struct parser_params *p, int line)
9053{
9054 if (has_delayed_token(p))
9055 dispatch_delayed_token(p, tSTRING_CONTENT);
9056
9057#ifdef RIPPER
9058 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
9059 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9060#else
9061 if (p->keep_tokens) {
9062 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9063 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9064 parser_append_tokens(p, str, tHEREDOC_END, line);
9065 }
9066#endif
9067
9068 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9069 lex_goto_eol(p);
9070 token_flush(p);
9071}
9072
9073static enum yytokentype
9074here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9075{
9076 int c, func, indent = 0;
9077 const char *eos, *ptr, *ptr_end;
9078 long len;
9079 rb_parser_string_t *str = 0;
9080 rb_encoding *enc = p->enc;
9081 rb_encoding *base_enc = 0;
9082 int bol;
9083#ifdef RIPPER
9084 VALUE s_value;
9085#endif
9086
9087 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9088 len = here->length;
9089 indent = (func = here->func) & STR_FUNC_INDENT;
9090
9091 if ((c = nextc(p)) == -1) {
9092 error:
9093#ifdef RIPPER
9094 if (!has_delayed_token(p)) {
9095 dispatch_scan_event(p, tSTRING_CONTENT);
9096 }
9097 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9098 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9099 if (!(func & STR_FUNC_REGEXP)) {
9100 int cr = ENC_CODERANGE_UNKNOWN;
9101 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9102 if (cr != ENC_CODERANGE_7BIT &&
9103 rb_is_usascii_enc(p->enc) &&
9104 enc != rb_utf8_encoding()) {
9105 enc = rb_ascii8bit_encoding();
9106 }
9107 }
9108 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9109 }
9110 dispatch_delayed_token(p, tSTRING_CONTENT);
9111 }
9112 else {
9113 dispatch_delayed_token(p, tSTRING_CONTENT);
9114 dispatch_scan_event(p, tSTRING_CONTENT);
9115 }
9116 lex_goto_eol(p);
9117#endif
9118 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9119 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9120 (int)len, eos);
9121 token_flush(p);
9122 SET_LEX_STATE(EXPR_END);
9123 return tSTRING_END;
9124 }
9125 bol = was_bol(p);
9126 if (!bol) {
9127 /* not beginning of line, cannot be the terminator */
9128 }
9129 else if (p->heredoc_line_indent == -1) {
9130 /* `heredoc_line_indent == -1` means
9131 * - "after an interpolation in the same line", or
9132 * - "in a continuing line"
9133 */
9134 p->heredoc_line_indent = 0;
9135 }
9136 else if (whole_match_p(p, eos, len, indent)) {
9137 dispatch_heredoc_end(p);
9138 restore:
9139 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9140 token_flush(p);
9141 SET_LEX_STATE(EXPR_END);
9142 return tSTRING_END;
9143 }
9144
9145 if (!(func & STR_FUNC_EXPAND)) {
9146 do {
9147 ptr = PARSER_STRING_PTR(p->lex.lastline);
9148 ptr_end = p->lex.pend;
9149 if (ptr_end > ptr) {
9150 switch (ptr_end[-1]) {
9151 case '\n':
9152 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9153 ptr_end++;
9154 break;
9155 }
9156 case '\r':
9157 --ptr_end;
9158 }
9159 }
9160
9161 if (p->heredoc_indent > 0) {
9162 long i = 0;
9163 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9164 i++;
9165 p->heredoc_line_indent = 0;
9166 }
9167
9168 if (str)
9169 parser_str_cat(str, ptr, ptr_end - ptr);
9170 else
9171 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9172 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9173 lex_goto_eol(p);
9174 if (p->heredoc_indent > 0) {
9175 goto flush_str;
9176 }
9177 if (nextc(p) == -1) {
9178 if (str) {
9179 rb_parser_string_free(p, str);
9180 str = 0;
9181 }
9182 goto error;
9183 }
9184 } while (!whole_match_p(p, eos, len, indent));
9185 }
9186 else {
9187 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9188 newtok(p);
9189 if (c == '#') {
9190 enum yytokentype t = parser_peek_variable_name(p);
9191 if (p->heredoc_line_indent != -1) {
9192 if (p->heredoc_indent > p->heredoc_line_indent) {
9193 p->heredoc_indent = p->heredoc_line_indent;
9194 }
9195 p->heredoc_line_indent = -1;
9196 }
9197 if (t) return t;
9198 tokadd(p, '#');
9199 c = nextc(p);
9200 }
9201 do {
9202 pushback(p, c);
9203 enc = p->enc;
9204 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9205 if (p->eofp) goto error;
9206 goto restore;
9207 }
9208 if (c != '\n') {
9209 if (c == '\\') p->heredoc_line_indent = -1;
9210 flush:
9211 str = STR_NEW3(tok(p), toklen(p), enc, func);
9212 flush_str:
9213 set_yylval_str(str);
9214#ifndef RIPPER
9215 if (bol) nd_set_fl_newline(yylval.node);
9216#endif
9217 flush_string_content(p, enc, 0);
9218 return tSTRING_CONTENT;
9219 }
9220 tokadd(p, nextc(p));
9221 if (p->heredoc_indent > 0) {
9222 lex_goto_eol(p);
9223 goto flush;
9224 }
9225 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9226 if ((c = nextc(p)) == -1) goto error;
9227 } while (!whole_match_p(p, eos, len, indent));
9228 str = STR_NEW3(tok(p), toklen(p), enc, func);
9229 }
9230 dispatch_heredoc_end(p);
9231 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9232 token_flush(p);
9233 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9234#ifdef RIPPER
9235 /* Preserve s_value for set_yylval_str */
9236 s_value = p->s_value;
9237#endif
9238 set_yylval_str(str);
9239#ifdef RIPPER
9240 set_parser_s_value(s_value);
9241#endif
9242
9243#ifndef RIPPER
9244 if (bol) nd_set_fl_newline(yylval.node);
9245#endif
9246 return tSTRING_CONTENT;
9247}
9248
9249#include "lex.c"
9250
9251static int
9252arg_ambiguous(struct parser_params *p, char c)
9253{
9254#ifndef RIPPER
9255 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9256#else
9257 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9258#endif
9259 return TRUE;
9260}
9261
9262/* returns true value if formal argument error;
9263 * Qtrue, or error message if ripper */
9264static VALUE
9265formal_argument_error(struct parser_params *p, ID id)
9266{
9267 switch (id_type(id)) {
9268 case ID_LOCAL:
9269 break;
9270#ifndef RIPPER
9271# define ERR(mesg) (yyerror0(mesg), Qtrue)
9272#else
9273# define ERR(mesg) WARN_S(mesg)
9274#endif
9275 case ID_CONST:
9276 return ERR("formal argument cannot be a constant");
9277 case ID_INSTANCE:
9278 return ERR("formal argument cannot be an instance variable");
9279 case ID_GLOBAL:
9280 return ERR("formal argument cannot be a global variable");
9281 case ID_CLASS:
9282 return ERR("formal argument cannot be a class variable");
9283 default:
9284 return ERR("formal argument must be local variable");
9285#undef ERR
9286 }
9287 shadowing_lvar(p, id);
9288
9289 return Qfalse;
9290}
9291
9292static int
9293lvar_defined(struct parser_params *p, ID id)
9294{
9295 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9296}
9297
9298/* emacsen -*- hack */
9299static long
9300parser_encode_length(struct parser_params *p, const char *name, long len)
9301{
9302 long nlen;
9303
9304 if (len > 5 && name[nlen = len - 5] == '-') {
9305 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9306 return nlen;
9307 }
9308 if (len > 4 && name[nlen = len - 4] == '-') {
9309 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9310 return nlen;
9311 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9312 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9313 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9314 return nlen;
9315 }
9316 return len;
9317}
9318
9319static void
9320parser_set_encode(struct parser_params *p, const char *name)
9321{
9322 rb_encoding *enc;
9323 VALUE excargs[3];
9324 int idx = 0;
9325
9326 const char *wrong = 0;
9327 switch (*name) {
9328 case 'e': case 'E': wrong = "external"; break;
9329 case 'i': case 'I': wrong = "internal"; break;
9330 case 'f': case 'F': wrong = "filesystem"; break;
9331 case 'l': case 'L': wrong = "locale"; break;
9332 }
9333 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9334 idx = rb_enc_find_index(name);
9335 if (idx < 0) {
9336 unknown:
9337 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9338 error:
9339 excargs[0] = rb_eArgError;
9340 excargs[2] = rb_make_backtrace();
9341 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9342 VALUE exc = rb_make_exception(3, excargs);
9343 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9344
9345 rb_ast_free(p->ast);
9346 p->ast = NULL;
9347
9348 rb_exc_raise(exc);
9349 }
9350 enc = rb_enc_from_index(idx);
9351 if (!rb_enc_asciicompat(enc)) {
9352 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9353 goto error;
9354 }
9355 p->enc = enc;
9356#ifndef RIPPER
9357 if (p->debug_lines) {
9358 long i;
9359 for (i = 0; i < p->debug_lines->len; i++) {
9360 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9361 }
9362 }
9363#endif
9364}
9365
9366static bool
9367comment_at_top(struct parser_params *p)
9368{
9369 if (p->token_seen) return false;
9370 return (p->line_count == (p->has_shebang ? 2 : 1));
9371}
9372
9373typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9374typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9375
9376static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9377
9378static void
9379magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9380{
9381 if (!comment_at_top(p)) {
9382 return;
9383 }
9384 parser_set_encode(p, val);
9385}
9386
9387static int
9388parser_get_bool(struct parser_params *p, const char *name, const char *val)
9389{
9390 switch (*val) {
9391 case 't': case 'T':
9392 if (STRCASECMP(val, "true") == 0) {
9393 return TRUE;
9394 }
9395 break;
9396 case 'f': case 'F':
9397 if (STRCASECMP(val, "false") == 0) {
9398 return FALSE;
9399 }
9400 break;
9401 }
9402 return parser_invalid_pragma_value(p, name, val);
9403}
9404
9405static int
9406parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9407{
9408 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9409 return -1;
9410}
9411
9412static void
9413parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9414{
9415 int b = parser_get_bool(p, name, val);
9416 if (b >= 0) p->token_info_enabled = b;
9417}
9418
9419static void
9420parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9421{
9422 int b;
9423
9424 if (p->token_seen) {
9425 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9426 return;
9427 }
9428
9429 b = parser_get_bool(p, name, val);
9430 if (b < 0) return;
9431
9432 p->frozen_string_literal = b;
9433}
9434
9435static void
9436parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9437{
9438 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9439 if (*s == ' ' || *s == '\t') continue;
9440 if (*s == '#') break;
9441 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9442 return;
9443 }
9444
9445 switch (*val) {
9446 case 'n': case 'N':
9447 if (STRCASECMP(val, "none") == 0) {
9448 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9449 return;
9450 }
9451 break;
9452 case 'l': case 'L':
9453 if (STRCASECMP(val, "literal") == 0) {
9454 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9455 return;
9456 }
9457 break;
9458 case 'e': case 'E':
9459 if (STRCASECMP(val, "experimental_copy") == 0) {
9460 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9461 return;
9462 }
9463 if (STRCASECMP(val, "experimental_everything") == 0) {
9464 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9465 return;
9466 }
9467 break;
9468 }
9469 parser_invalid_pragma_value(p, name, val);
9470}
9471
9472# if WARN_PAST_SCOPE
9473static void
9474parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9475{
9476 int b = parser_get_bool(p, name, val);
9477 if (b >= 0) p->past_scope_enabled = b;
9478}
9479# endif
9480
9481struct magic_comment {
9482 const char *name;
9483 rb_magic_comment_setter_t func;
9484 rb_magic_comment_length_t length;
9485};
9486
9487static const struct magic_comment magic_comments[] = {
9488 {"coding", magic_comment_encoding, parser_encode_length},
9489 {"encoding", magic_comment_encoding, parser_encode_length},
9490 {"frozen_string_literal", parser_set_frozen_string_literal},
9491 {"shareable_constant_value", parser_set_shareable_constant_value},
9492 {"warn_indent", parser_set_token_info},
9493# if WARN_PAST_SCOPE
9494 {"warn_past_scope", parser_set_past_scope},
9495# endif
9496};
9497
9498static const char *
9499magic_comment_marker(const char *str, long len)
9500{
9501 long i = 2;
9502
9503 while (i < len) {
9504 switch (str[i]) {
9505 case '-':
9506 if (str[i-1] == '*' && str[i-2] == '-') {
9507 return str + i + 1;
9508 }
9509 i += 2;
9510 break;
9511 case '*':
9512 if (i + 1 >= len) return 0;
9513 if (str[i+1] != '-') {
9514 i += 4;
9515 }
9516 else if (str[i-1] != '-') {
9517 i += 2;
9518 }
9519 else {
9520 return str + i + 2;
9521 }
9522 break;
9523 default:
9524 i += 3;
9525 break;
9526 }
9527 }
9528 return 0;
9529}
9530
9531static int
9532parser_magic_comment(struct parser_params *p, const char *str, long len)
9533{
9534 int indicator = 0;
9535 VALUE name = 0, val = 0;
9536 const char *beg, *end, *vbeg, *vend;
9537#define str_copy(_s, _p, _n) ((_s) \
9538 ? (void)(rb_str_resize((_s), (_n)), \
9539 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9540 : (void)((_s) = STR_NEW((_p), (_n))))
9541
9542 if (len <= 7) return FALSE;
9543 if (!!(beg = magic_comment_marker(str, len))) {
9544 if (!(end = magic_comment_marker(beg, str + len - beg)))
9545 return FALSE;
9546 indicator = TRUE;
9547 str = beg;
9548 len = end - beg - 3;
9549 }
9550
9551 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9552 while (len > 0) {
9553 const struct magic_comment *mc = magic_comments;
9554 char *s;
9555 int i;
9556 long n = 0;
9557
9558 for (; len > 0 && *str; str++, --len) {
9559 switch (*str) {
9560 case '\'': case '"': case ':': case ';':
9561 continue;
9562 }
9563 if (!ISSPACE(*str)) break;
9564 }
9565 for (beg = str; len > 0; str++, --len) {
9566 switch (*str) {
9567 case '\'': case '"': case ':': case ';':
9568 break;
9569 default:
9570 if (ISSPACE(*str)) break;
9571 continue;
9572 }
9573 break;
9574 }
9575 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9576 if (!len) break;
9577 if (*str != ':') {
9578 if (!indicator) return FALSE;
9579 continue;
9580 }
9581
9582 do str++; while (--len > 0 && ISSPACE(*str));
9583 if (!len) break;
9584 const char *tok_beg = str;
9585 if (*str == '"') {
9586 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9587 if (*str == '\\') {
9588 --len;
9589 ++str;
9590 }
9591 }
9592 vend = str;
9593 if (len) {
9594 --len;
9595 ++str;
9596 }
9597 }
9598 else {
9599 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9600 vend = str;
9601 }
9602 const char *tok_end = str;
9603 if (indicator) {
9604 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9605 }
9606 else {
9607 while (len > 0 && (ISSPACE(*str))) --len, str++;
9608 if (len) return FALSE;
9609 }
9610
9611 n = end - beg;
9612 str_copy(name, beg, n);
9613 s = RSTRING_PTR(name);
9614 for (i = 0; i < n; ++i) {
9615 if (s[i] == '-') s[i] = '_';
9616 }
9617 do {
9618 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9619 n = vend - vbeg;
9620 if (mc->length) {
9621 n = (*mc->length)(p, vbeg, n);
9622 }
9623 str_copy(val, vbeg, n);
9624 p->lex.ptok = tok_beg;
9625 p->lex.pcur = tok_end;
9626 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9627 break;
9628 }
9629 } while (++mc < magic_comments + numberof(magic_comments));
9630#ifdef RIPPER
9631 str_copy(val, vbeg, vend - vbeg);
9632 dispatch2(magic_comment, name, val);
9633#endif
9634 }
9635
9636 return TRUE;
9637}
9638
9639static void
9640set_file_encoding(struct parser_params *p, const char *str, const char *send)
9641{
9642 int sep = 0;
9643 const char *beg = str;
9644 VALUE s;
9645
9646 for (;;) {
9647 if (send - str <= 6) return;
9648 switch (str[6]) {
9649 case 'C': case 'c': str += 6; continue;
9650 case 'O': case 'o': str += 5; continue;
9651 case 'D': case 'd': str += 4; continue;
9652 case 'I': case 'i': str += 3; continue;
9653 case 'N': case 'n': str += 2; continue;
9654 case 'G': case 'g': str += 1; continue;
9655 case '=': case ':':
9656 sep = 1;
9657 str += 6;
9658 break;
9659 default:
9660 str += 6;
9661 if (ISSPACE(*str)) break;
9662 continue;
9663 }
9664 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9665 sep = 0;
9666 }
9667 for (;;) {
9668 do {
9669 if (++str >= send) return;
9670 } while (ISSPACE(*str));
9671 if (sep) break;
9672 if (*str != '=' && *str != ':') return;
9673 sep = 1;
9674 str++;
9675 }
9676 beg = str;
9677 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9678 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9679 p->lex.ptok = beg;
9680 p->lex.pcur = str;
9681 parser_set_encode(p, RSTRING_PTR(s));
9682 rb_str_resize(s, 0);
9683}
9684
9685static void
9686parser_prepare(struct parser_params *p)
9687{
9688 int c = nextc0(p, FALSE);
9689 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9690 switch (c) {
9691 case '#':
9692 if (peek(p, '!')) p->has_shebang = 1;
9693 break;
9694 case 0xef: /* UTF-8 BOM marker */
9695 if (!lex_eol_n_p(p, 1) &&
9696 (unsigned char)p->lex.pcur[0] == 0xbb &&
9697 (unsigned char)p->lex.pcur[1] == 0xbf) {
9698 p->enc = rb_utf8_encoding();
9699 p->lex.pcur += 2;
9700#ifndef RIPPER
9701 if (p->debug_lines) {
9702 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9703 }
9704#endif
9705 p->lex.pbeg = p->lex.pcur;
9706 token_flush(p);
9707 return;
9708 }
9709 break;
9710 case -1: /* end of script. */
9711 return;
9712 }
9713 pushback(p, c);
9714 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9715}
9716
9717#ifndef RIPPER
9718#define ambiguous_operator(tok, op, syn) ( \
9719 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9720 rb_warning0("even though it seems like "syn""))
9721#else
9722#define ambiguous_operator(tok, op, syn) \
9723 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9724#endif
9725#define warn_balanced(tok, op, syn) ((void) \
9726 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9727 space_seen && !ISSPACE(c) && \
9728 (ambiguous_operator(tok, op, syn), 0)), \
9729 (enum yytokentype)(tok))
9730
9731static enum yytokentype
9732no_digits(struct parser_params *p)
9733{
9734 yyerror0("numeric literal without digits");
9735 if (peek(p, '_')) nextc(p);
9736 /* dummy 0, for tUMINUS_NUM at numeric */
9737 return set_number_literal(p, tINTEGER, 0, 10, 0);
9738}
9739
9740static enum yytokentype
9741parse_numeric(struct parser_params *p, int c)
9742{
9743 int is_float, seen_point, seen_e, nondigit;
9744 int suffix;
9745
9746 is_float = seen_point = seen_e = nondigit = 0;
9747 SET_LEX_STATE(EXPR_END);
9748 newtok(p);
9749 if (c == '-' || c == '+') {
9750 tokadd(p, c);
9751 c = nextc(p);
9752 }
9753 if (c == '0') {
9754 int start = toklen(p);
9755 c = nextc(p);
9756 if (c == 'x' || c == 'X') {
9757 /* hexadecimal */
9758 c = nextc(p);
9759 if (c != -1 && ISXDIGIT(c)) {
9760 do {
9761 if (c == '_') {
9762 if (nondigit) break;
9763 nondigit = c;
9764 continue;
9765 }
9766 if (!ISXDIGIT(c)) break;
9767 nondigit = 0;
9768 tokadd(p, c);
9769 } while ((c = nextc(p)) != -1);
9770 }
9771 pushback(p, c);
9772 tokfix(p);
9773 if (toklen(p) == start) {
9774 return no_digits(p);
9775 }
9776 else if (nondigit) goto trailing_uc;
9777 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9778 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9779 }
9780 if (c == 'b' || c == 'B') {
9781 /* binary */
9782 c = nextc(p);
9783 if (c == '0' || c == '1') {
9784 do {
9785 if (c == '_') {
9786 if (nondigit) break;
9787 nondigit = c;
9788 continue;
9789 }
9790 if (c != '0' && c != '1') break;
9791 nondigit = 0;
9792 tokadd(p, c);
9793 } while ((c = nextc(p)) != -1);
9794 }
9795 pushback(p, c);
9796 tokfix(p);
9797 if (toklen(p) == start) {
9798 return no_digits(p);
9799 }
9800 else if (nondigit) goto trailing_uc;
9801 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9802 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9803 }
9804 if (c == 'd' || c == 'D') {
9805 /* decimal */
9806 c = nextc(p);
9807 if (c != -1 && ISDIGIT(c)) {
9808 do {
9809 if (c == '_') {
9810 if (nondigit) break;
9811 nondigit = c;
9812 continue;
9813 }
9814 if (!ISDIGIT(c)) break;
9815 nondigit = 0;
9816 tokadd(p, c);
9817 } while ((c = nextc(p)) != -1);
9818 }
9819 pushback(p, c);
9820 tokfix(p);
9821 if (toklen(p) == start) {
9822 return no_digits(p);
9823 }
9824 else if (nondigit) goto trailing_uc;
9825 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9826 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9827 }
9828 if (c == '_') {
9829 /* 0_0 */
9830 goto octal_number;
9831 }
9832 if (c == 'o' || c == 'O') {
9833 /* prefixed octal */
9834 c = nextc(p);
9835 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9836 tokfix(p);
9837 return no_digits(p);
9838 }
9839 }
9840 if (c >= '0' && c <= '7') {
9841 /* octal */
9842 octal_number:
9843 do {
9844 if (c == '_') {
9845 if (nondigit) break;
9846 nondigit = c;
9847 continue;
9848 }
9849 if (c < '0' || c > '9') break;
9850 if (c > '7') goto invalid_octal;
9851 nondigit = 0;
9852 tokadd(p, c);
9853 } while ((c = nextc(p)) != -1);
9854 if (toklen(p) > start) {
9855 pushback(p, c);
9856 tokfix(p);
9857 if (nondigit) goto trailing_uc;
9858 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9859 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9860 }
9861 if (nondigit) {
9862 pushback(p, c);
9863 goto trailing_uc;
9864 }
9865 }
9866 if (c > '7' && c <= '9') {
9867 invalid_octal:
9868 yyerror0("Invalid octal digit");
9869 }
9870 else if (c == '.' || c == 'e' || c == 'E') {
9871 tokadd(p, '0');
9872 }
9873 else {
9874 pushback(p, c);
9875 tokfix(p);
9876 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9877 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9878 }
9879 }
9880
9881 for (;;) {
9882 switch (c) {
9883 case '0': case '1': case '2': case '3': case '4':
9884 case '5': case '6': case '7': case '8': case '9':
9885 nondigit = 0;
9886 tokadd(p, c);
9887 break;
9888
9889 case '.':
9890 if (nondigit) goto trailing_uc;
9891 if (seen_point || seen_e) {
9892 goto decode_num;
9893 }
9894 else {
9895 int c0 = nextc(p);
9896 if (c0 == -1 || !ISDIGIT(c0)) {
9897 pushback(p, c0);
9898 goto decode_num;
9899 }
9900 c = c0;
9901 }
9902 seen_point = toklen(p);
9903 tokadd(p, '.');
9904 tokadd(p, c);
9905 is_float++;
9906 nondigit = 0;
9907 break;
9908
9909 case 'e':
9910 case 'E':
9911 if (nondigit) {
9912 pushback(p, c);
9913 c = nondigit;
9914 goto decode_num;
9915 }
9916 if (seen_e) {
9917 goto decode_num;
9918 }
9919 nondigit = c;
9920 c = nextc(p);
9921 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9922 pushback(p, c);
9923 c = nondigit;
9924 nondigit = 0;
9925 goto decode_num;
9926 }
9927 tokadd(p, nondigit);
9928 seen_e++;
9929 is_float++;
9930 tokadd(p, c);
9931 nondigit = (c == '-' || c == '+') ? c : 0;
9932 break;
9933
9934 case '_': /* `_' in number just ignored */
9935 if (nondigit) goto decode_num;
9936 nondigit = c;
9937 break;
9938
9939 default:
9940 goto decode_num;
9941 }
9942 c = nextc(p);
9943 }
9944
9945 decode_num:
9946 pushback(p, c);
9947 if (nondigit) {
9948 trailing_uc:
9949 literal_flush(p, p->lex.pcur - 1);
9950 YYLTYPE loc = RUBY_INIT_YYLLOC();
9951 compile_error(p, "trailing '%c' in number", nondigit);
9952 parser_show_error_line(p, &loc);
9953 }
9954 tokfix(p);
9955 if (is_float) {
9956 enum yytokentype type = tFLOAT;
9957
9958 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9959 if (suffix & NUM_SUFFIX_R) {
9960 type = tRATIONAL;
9961 }
9962 else {
9963 errno = 0;
9964 strtod(tok(p), 0);
9965 if (errno == ERANGE) {
9966 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9967 errno = 0;
9968 }
9969 }
9970 return set_number_literal(p, type, suffix, 0, seen_point);
9971 }
9972 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9973 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9974}
9975
9976static enum yytokentype
9977parse_qmark(struct parser_params *p, int space_seen)
9978{
9979 rb_encoding *enc;
9980 register int c;
9981 rb_parser_string_t *lit;
9982 const char *start = p->lex.pcur;
9983
9984 if (IS_END()) {
9985 SET_LEX_STATE(EXPR_VALUE);
9986 return '?';
9987 }
9988 c = nextc(p);
9989 if (c == -1) {
9990 compile_error(p, "incomplete character syntax");
9991 return 0;
9992 }
9993 if (rb_enc_isspace(c, p->enc)) {
9994 if (!IS_ARG()) {
9995 int c2 = escaped_control_code(c);
9996 if (c2) {
9997 WARN_SPACE_CHAR(c2, "?");
9998 }
9999 }
10000 ternary:
10001 pushback(p, c);
10002 SET_LEX_STATE(EXPR_VALUE);
10003 return '?';
10004 }
10005 newtok(p);
10006 enc = p->enc;
10007 int w = parser_precise_mbclen(p, start);
10008 if (is_identchar(p, start, p->lex.pend, p->enc) &&
10009 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
10010 if (space_seen) {
10011 const char *ptr = start;
10012 do {
10013 int n = parser_precise_mbclen(p, ptr);
10014 if (n < 0) return -1;
10015 ptr += n;
10016 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
10017 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
10018 " a conditional operator, put a space after '?'",
10019 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
10020 }
10021 goto ternary;
10022 }
10023 else if (c == '\\') {
10024 if (peek(p, 'u')) {
10025 nextc(p);
10026 enc = rb_utf8_encoding();
10027 tokadd_utf8(p, &enc, -1, 0, 0);
10028 }
10029 else if (!ISASCII(c = peekc(p)) && c != -1) {
10030 nextc(p);
10031 if (tokadd_mbchar(p, c) == -1) return 0;
10032 }
10033 else {
10034 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
10035 tokadd(p, c);
10036 }
10037 }
10038 else {
10039 if (tokadd_mbchar(p, c) == -1) return 0;
10040 }
10041 tokfix(p);
10042 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
10043 set_yylval_str(lit);
10044 SET_LEX_STATE(EXPR_END);
10045 return tCHAR;
10046}
10047
10048static enum yytokentype
10049parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
10050{
10051 register int c;
10052 const char *ptok = p->lex.pcur;
10053
10054 if (IS_BEG()) {
10055 int term;
10056 int paren;
10057
10058 c = nextc(p);
10059 quotation:
10060 if (c == -1) goto unterminated;
10061 if (!ISALNUM(c)) {
10062 term = c;
10063 if (!ISASCII(c)) goto unknown;
10064 c = 'Q';
10065 }
10066 else {
10067 term = nextc(p);
10068 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10069 unknown:
10070 pushback(p, term);
10071 c = parser_precise_mbclen(p, p->lex.pcur);
10072 if (c < 0) return 0;
10073 p->lex.pcur += c;
10074 yyerror0("unknown type of %string");
10075 return 0;
10076 }
10077 }
10078 if (term == -1) {
10079 unterminated:
10080 compile_error(p, "unterminated quoted string meets end of file");
10081 return 0;
10082 }
10083 paren = term;
10084 if (term == '(') term = ')';
10085 else if (term == '[') term = ']';
10086 else if (term == '{') term = '}';
10087 else if (term == '<') term = '>';
10088 else paren = 0;
10089
10090 p->lex.ptok = ptok-1;
10091 switch (c) {
10092 case 'Q':
10093 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10094 return tSTRING_BEG;
10095
10096 case 'q':
10097 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10098 return tSTRING_BEG;
10099
10100 case 'W':
10101 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10102 return tWORDS_BEG;
10103
10104 case 'w':
10105 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10106 return tQWORDS_BEG;
10107
10108 case 'I':
10109 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10110 return tSYMBOLS_BEG;
10111
10112 case 'i':
10113 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10114 return tQSYMBOLS_BEG;
10115
10116 case 'x':
10117 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10118 return tXSTRING_BEG;
10119
10120 case 'r':
10121 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10122 return tREGEXP_BEG;
10123
10124 case 's':
10125 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10126 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10127 return tSYMBEG;
10128
10129 default:
10130 yyerror0("unknown type of %string");
10131 return 0;
10132 }
10133 }
10134 if ((c = nextc(p)) == '=') {
10135 set_yylval_id('%');
10136 SET_LEX_STATE(EXPR_BEG);
10137 return tOP_ASGN;
10138 }
10139 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10140 goto quotation;
10141 }
10142 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10143 pushback(p, c);
10144 return warn_balanced('%', "%%", "string literal");
10145}
10146
10147static int
10148tokadd_ident(struct parser_params *p, int c)
10149{
10150 do {
10151 if (tokadd_mbchar(p, c) == -1) return -1;
10152 c = nextc(p);
10153 } while (parser_is_identchar(p));
10154 pushback(p, c);
10155 return 0;
10156}
10157
10158static ID
10159tokenize_ident(struct parser_params *p)
10160{
10161 ID ident = TOK_INTERN();
10162
10163 set_yylval_name(ident);
10164
10165 return ident;
10166}
10167
10168static int
10169parse_numvar(struct parser_params *p)
10170{
10171 size_t len;
10172 int overflow;
10173 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10174 const unsigned long nth_ref_max =
10175 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10176 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10177 * turned into a Fixnum, in compile.c */
10178
10179 if (overflow || n > nth_ref_max) {
10180 /* compile_error()? */
10181 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10182 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10183 }
10184 else {
10185 return (int)n;
10186 }
10187}
10188
10189static enum yytokentype
10190parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10191{
10192 const char *ptr = p->lex.pcur;
10193 register int c;
10194
10195 SET_LEX_STATE(EXPR_END);
10196 p->lex.ptok = ptr - 1; /* from '$' */
10197 newtok(p);
10198 c = nextc(p);
10199 switch (c) {
10200 case '_': /* $_: last read line string */
10201 c = nextc(p);
10202 if (parser_is_identchar(p)) {
10203 tokadd(p, '$');
10204 tokadd(p, '_');
10205 break;
10206 }
10207 pushback(p, c);
10208 c = '_';
10209 /* fall through */
10210 case '~': /* $~: match-data */
10211 case '*': /* $*: argv */
10212 case '$': /* $$: pid */
10213 case '?': /* $?: last status */
10214 case '!': /* $!: error string */
10215 case '@': /* $@: error position */
10216 case '/': /* $/: input record separator */
10217 case '\\': /* $\: output record separator */
10218 case ';': /* $;: field separator */
10219 case ',': /* $,: output field separator */
10220 case '.': /* $.: last read line number */
10221 case '=': /* $=: ignorecase */
10222 case ':': /* $:: load path */
10223 case '<': /* $<: default input handle */
10224 case '>': /* $>: default output handle */
10225 case '\"': /* $": already loaded files */
10226 tokadd(p, '$');
10227 tokadd(p, c);
10228 goto gvar;
10229
10230 case '-':
10231 tokadd(p, '$');
10232 tokadd(p, c);
10233 c = nextc(p);
10234 if (parser_is_identchar(p)) {
10235 if (tokadd_mbchar(p, c) == -1) return 0;
10236 }
10237 else {
10238 pushback(p, c);
10239 pushback(p, '-');
10240 return '$';
10241 }
10242 gvar:
10243 tokenize_ident(p);
10244 return tGVAR;
10245
10246 case '&': /* $&: last match */
10247 case '`': /* $`: string before last match */
10248 case '\'': /* $': string after last match */
10249 case '+': /* $+: string matches last paren. */
10250 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10251 tokadd(p, '$');
10252 tokadd(p, c);
10253 goto gvar;
10254 }
10255 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10256 return tBACK_REF;
10257
10258 case '1': case '2': case '3':
10259 case '4': case '5': case '6':
10260 case '7': case '8': case '9':
10261 tokadd(p, '$');
10262 do {
10263 tokadd(p, c);
10264 c = nextc(p);
10265 } while (c != -1 && ISDIGIT(c));
10266 pushback(p, c);
10267 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10268 tokfix(p);
10269 c = parse_numvar(p);
10270 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10271 return tNTH_REF;
10272
10273 default:
10274 if (!parser_is_identchar(p)) {
10275 YYLTYPE loc = RUBY_INIT_YYLLOC();
10276 if (c == -1 || ISSPACE(c)) {
10277 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10278 }
10279 else {
10280 pushback(p, c);
10281 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10282 }
10283 parser_show_error_line(p, &loc);
10284 set_yylval_noname();
10285 return tGVAR;
10286 }
10287 /* fall through */
10288 case '0':
10289 tokadd(p, '$');
10290 }
10291
10292 if (tokadd_ident(p, c)) return 0;
10293 SET_LEX_STATE(EXPR_END);
10294 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10295 tokenize_ident(p);
10296 }
10297 else {
10298 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10299 set_yylval_noname();
10300 }
10301 return tGVAR;
10302}
10303
10304static bool
10305parser_numbered_param(struct parser_params *p, int n)
10306{
10307 if (n < 0) return false;
10308
10309 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10310 return false;
10311 }
10312 if (p->max_numparam == ORDINAL_PARAM) {
10313 compile_error(p, "ordinary parameter is defined");
10314 return false;
10315 }
10316 struct vtable *args = p->lvtbl->args;
10317 if (p->max_numparam < n) {
10318 p->max_numparam = n;
10319 }
10320 while (n > args->pos) {
10321 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10322 }
10323 return true;
10324}
10325
10326static enum yytokentype
10327parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10328{
10329 const char *ptr = p->lex.pcur;
10330 enum yytokentype result = tIVAR;
10331 register int c = nextc(p);
10332 YYLTYPE loc;
10333
10334 p->lex.ptok = ptr - 1; /* from '@' */
10335 newtok(p);
10336 tokadd(p, '@');
10337 if (c == '@') {
10338 result = tCVAR;
10339 tokadd(p, '@');
10340 c = nextc(p);
10341 }
10342 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10343 if (c == -1 || !parser_is_identchar(p)) {
10344 pushback(p, c);
10345 RUBY_SET_YYLLOC(loc);
10346 if (result == tIVAR) {
10347 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10348 }
10349 else {
10350 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10351 }
10352 parser_show_error_line(p, &loc);
10353 set_yylval_noname();
10354 SET_LEX_STATE(EXPR_END);
10355 return result;
10356 }
10357 else if (ISDIGIT(c)) {
10358 pushback(p, c);
10359 RUBY_SET_YYLLOC(loc);
10360 if (result == tIVAR) {
10361 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10362 }
10363 else {
10364 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10365 }
10366 parser_show_error_line(p, &loc);
10367 set_yylval_noname();
10368 SET_LEX_STATE(EXPR_END);
10369 return result;
10370 }
10371
10372 if (tokadd_ident(p, c)) return 0;
10373 tokenize_ident(p);
10374 return result;
10375}
10376
10377static enum yytokentype
10378parse_ident(struct parser_params *p, int c, int cmd_state)
10379{
10380 enum yytokentype result;
10381 bool is_ascii = true;
10382 const enum lex_state_e last_state = p->lex.state;
10383 ID ident;
10384 int enforce_keyword_end = 0;
10385
10386 do {
10387 if (!ISASCII(c)) is_ascii = false;
10388 if (tokadd_mbchar(p, c) == -1) return 0;
10389 c = nextc(p);
10390 } while (parser_is_identchar(p));
10391 if ((c == '!' || c == '?') && !peek(p, '=')) {
10392 result = tFID;
10393 tokadd(p, c);
10394 }
10395 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10396 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10397 result = tIDENTIFIER;
10398 tokadd(p, c);
10399 }
10400 else {
10401 result = tCONSTANT; /* assume provisionally */
10402 pushback(p, c);
10403 }
10404 tokfix(p);
10405
10406 if (IS_LABEL_POSSIBLE()) {
10407 if (IS_LABEL_SUFFIX(0)) {
10408 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10409 nextc(p);
10410 tokenize_ident(p);
10411 return tLABEL;
10412 }
10413 }
10414
10415#ifndef RIPPER
10416 if (peek_end_expect_token_locations(p)) {
10417 const rb_code_position_t *end_pos;
10418 int lineno, column;
10419 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
10420
10421 end_pos = peek_end_expect_token_locations(p)->pos;
10422 lineno = end_pos->lineno;
10423 column = end_pos->column;
10424
10425 if (p->debug) {
10426 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10427 p->ruby_sourceline, beg_pos, lineno, column);
10428 }
10429
10430 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10431 const struct kwtable *kw;
10432
10433 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10434 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10435 enforce_keyword_end = 1;
10436 }
10437 }
10438 }
10439#endif
10440
10441 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10442 const struct kwtable *kw;
10443
10444 /* See if it is a reserved word. */
10445 kw = rb_reserved_word(tok(p), toklen(p));
10446 if (kw) {
10447 enum lex_state_e state = p->lex.state;
10448 if (IS_lex_state_for(state, EXPR_FNAME)) {
10449 SET_LEX_STATE(EXPR_ENDFN);
10450 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10451 return kw->id[0];
10452 }
10453 SET_LEX_STATE(kw->state);
10454 if (IS_lex_state(EXPR_BEG)) {
10455 p->command_start = TRUE;
10456 }
10457 if (kw->id[0] == keyword_do) {
10458 if (lambda_beginning_p()) {
10459 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10460 return keyword_do_LAMBDA;
10461 }
10462 if (COND_P()) return keyword_do_cond;
10463 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10464 return keyword_do_block;
10465 return keyword_do;
10466 }
10467 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10468 return kw->id[0];
10469 else {
10470 if (kw->id[0] != kw->id[1])
10471 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10472 return kw->id[1];
10473 }
10474 }
10475 }
10476
10477 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10478 if (cmd_state) {
10479 SET_LEX_STATE(EXPR_CMDARG);
10480 }
10481 else {
10482 SET_LEX_STATE(EXPR_ARG);
10483 }
10484 }
10485 else if (p->lex.state == EXPR_FNAME) {
10486 SET_LEX_STATE(EXPR_ENDFN);
10487 }
10488 else {
10489 SET_LEX_STATE(EXPR_END);
10490 }
10491
10492 ident = tokenize_ident(p);
10493 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10494 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10495 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10496 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10497 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10498 }
10499 return result;
10500}
10501
10502static void
10503warn_cr(struct parser_params *p)
10504{
10505 if (!p->cr_seen) {
10506 p->cr_seen = TRUE;
10507 /* carried over with p->lex.nextline for nextc() */
10508 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10509 }
10510}
10511
10512static enum yytokentype
10513parser_yylex(struct parser_params *p)
10514{
10515 register int c;
10516 int space_seen = 0;
10517 int cmd_state;
10518 int label;
10519 enum lex_state_e last_state;
10520 int fallthru = FALSE;
10521 int token_seen = p->token_seen;
10522
10523 if (p->lex.strterm) {
10524 if (strterm_is_heredoc(p->lex.strterm)) {
10525 token_flush(p);
10526 return here_document(p, &p->lex.strterm->u.heredoc);
10527 }
10528 else {
10529 token_flush(p);
10530 return parse_string(p, &p->lex.strterm->u.literal);
10531 }
10532 }
10533 cmd_state = p->command_start;
10534 p->command_start = FALSE;
10535 p->token_seen = TRUE;
10536#ifndef RIPPER
10537 token_flush(p);
10538#endif
10539 retry:
10540 last_state = p->lex.state;
10541 switch (c = nextc(p)) {
10542 case '\0': /* NUL */
10543 case '\004': /* ^D */
10544 case '\032': /* ^Z */
10545 case -1: /* end of script. */
10546 p->eofp = 1;
10547#ifndef RIPPER
10548 if (p->end_expect_token_locations) {
10549 pop_end_expect_token_locations(p);
10550 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10551 return tDUMNY_END;
10552 }
10553#endif
10554 /* Set location for end-of-input because dispatch_scan_event is not called. */
10555 RUBY_SET_YYLLOC(*p->yylloc);
10556 return END_OF_INPUT;
10557
10558 /* white spaces */
10559 case '\r':
10560 warn_cr(p);
10561 /* fall through */
10562 case ' ': case '\t': case '\f':
10563 case '\13': /* '\v' */
10564 space_seen = 1;
10565 while ((c = nextc(p))) {
10566 switch (c) {
10567 case '\r':
10568 warn_cr(p);
10569 /* fall through */
10570 case ' ': case '\t': case '\f':
10571 case '\13': /* '\v' */
10572 break;
10573 default:
10574 goto outofloop;
10575 }
10576 }
10577 outofloop:
10578 pushback(p, c);
10579 dispatch_scan_event(p, tSP);
10580#ifndef RIPPER
10581 token_flush(p);
10582#endif
10583 goto retry;
10584
10585 case '#': /* it's a comment */
10586 p->token_seen = token_seen;
10587 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10588 /* no magic_comment in shebang line */
10589 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10590 if (comment_at_top(p)) {
10591 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10592 }
10593 }
10594 p->lex.pcur = pcur, p->lex.ptok = ptok;
10595 lex_goto_eol(p);
10596 dispatch_scan_event(p, tCOMMENT);
10597 fallthru = TRUE;
10598 /* fall through */
10599 case '\n':
10600 p->token_seen = token_seen;
10601 rb_parser_string_t *prevline = p->lex.lastline;
10602 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10603 !IS_lex_state(EXPR_LABELED));
10604 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10605 if (!fallthru) {
10606 dispatch_scan_event(p, tIGNORED_NL);
10607 }
10608 fallthru = FALSE;
10609 if (!c && p->ctxt.in_kwarg) {
10610 goto normal_newline;
10611 }
10612 goto retry;
10613 }
10614 while (1) {
10615 switch (c = nextc(p)) {
10616 case ' ': case '\t': case '\f': case '\r':
10617 case '\13': /* '\v' */
10618 space_seen = 1;
10619 break;
10620 case '#':
10621 pushback(p, c);
10622 if (space_seen) {
10623 dispatch_scan_event(p, tSP);
10624 token_flush(p);
10625 }
10626 goto retry;
10627 case 'a':
10628 if (peek_word_at(p, "nd", 2, 0)) goto leading_logical;
10629 goto bol;
10630 case 'o':
10631 if (peek_word_at(p, "r", 1, 0)) goto leading_logical;
10632 goto bol;
10633 case '|':
10634 if (peek(p, '|') && !peek_n(p, '=', 1)) goto leading_logical;
10635 goto bol;
10636 case '&':
10637 if (peek(p, '&') && !peek_n(p, '=', 1)) {
10638 leading_logical:
10639 pushback(p, c);
10640 dispatch_delayed_token(p, tIGNORED_NL);
10641 cmd_state = FALSE;
10642 goto retry;
10643 }
10644 /* fall through */
10645 case '.': {
10646 dispatch_delayed_token(p, tIGNORED_NL);
10647 if (peek(p, '.') == (c == '&')) {
10648 pushback(p, c);
10649 dispatch_scan_event(p, tSP);
10650 goto retry;
10651 }
10652 }
10653 bol:
10654 default:
10655 p->ruby_sourceline--;
10656 p->lex.nextline = p->lex.lastline;
10657 set_lastline(p, prevline);
10658 case -1: /* EOF no decrement*/
10659 if (c == -1 && space_seen) {
10660 dispatch_scan_event(p, tSP);
10661 }
10662 lex_goto_eol(p);
10663 if (c != -1) {
10664 token_flush(p);
10665 RUBY_SET_YYLLOC(*p->yylloc);
10666 }
10667 goto normal_newline;
10668 }
10669 }
10670 normal_newline:
10671 p->command_start = TRUE;
10672 SET_LEX_STATE(EXPR_BEG);
10673 return '\n';
10674
10675 case '*':
10676 if ((c = nextc(p)) == '*') {
10677 if ((c = nextc(p)) == '=') {
10678 set_yylval_id(idPow);
10679 SET_LEX_STATE(EXPR_BEG);
10680 return tOP_ASGN;
10681 }
10682 pushback(p, c);
10683 if (IS_SPCARG(c)) {
10684 rb_warning0("'**' interpreted as argument prefix");
10685 c = tDSTAR;
10686 }
10687 else if (IS_BEG()) {
10688 c = tDSTAR;
10689 }
10690 else {
10691 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10692 }
10693 }
10694 else {
10695 if (c == '=') {
10696 set_yylval_id('*');
10697 SET_LEX_STATE(EXPR_BEG);
10698 return tOP_ASGN;
10699 }
10700 pushback(p, c);
10701 if (IS_SPCARG(c)) {
10702 rb_warning0("'*' interpreted as argument prefix");
10703 c = tSTAR;
10704 }
10705 else if (IS_BEG()) {
10706 c = tSTAR;
10707 }
10708 else {
10709 c = warn_balanced('*', "*", "argument prefix");
10710 }
10711 }
10712 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10713 return c;
10714
10715 case '!':
10716 c = nextc(p);
10717 if (IS_AFTER_OPERATOR()) {
10718 SET_LEX_STATE(EXPR_ARG);
10719 if (c == '@') {
10720 return '!';
10721 }
10722 }
10723 else {
10724 SET_LEX_STATE(EXPR_BEG);
10725 }
10726 if (c == '=') {
10727 return tNEQ;
10728 }
10729 if (c == '~') {
10730 return tNMATCH;
10731 }
10732 pushback(p, c);
10733 return '!';
10734
10735 case '=':
10736 if (was_bol(p)) {
10737 /* skip embedded rd document */
10738 if (word_match_p(p, "begin", 5)) {
10739 int first_p = TRUE;
10740
10741 lex_goto_eol(p);
10742 dispatch_scan_event(p, tEMBDOC_BEG);
10743 for (;;) {
10744 lex_goto_eol(p);
10745 if (!first_p) {
10746 dispatch_scan_event(p, tEMBDOC);
10747 }
10748 first_p = FALSE;
10749 c = nextc(p);
10750 if (c == -1) {
10751 compile_error(p, "embedded document meets end of file");
10752 return END_OF_INPUT;
10753 }
10754 if (c == '=' && word_match_p(p, "end", 3)) {
10755 break;
10756 }
10757 pushback(p, c);
10758 }
10759 lex_goto_eol(p);
10760 dispatch_scan_event(p, tEMBDOC_END);
10761 goto retry;
10762 }
10763 }
10764
10765 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10766 if ((c = nextc(p)) == '=') {
10767 if ((c = nextc(p)) == '=') {
10768 return tEQQ;
10769 }
10770 pushback(p, c);
10771 return tEQ;
10772 }
10773 if (c == '~') {
10774 return tMATCH;
10775 }
10776 else if (c == '>') {
10777 return tASSOC;
10778 }
10779 pushback(p, c);
10780 return '=';
10781
10782 case '<':
10783 c = nextc(p);
10784 if (c == '<' &&
10785 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10786 !IS_END() &&
10787 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10788 enum yytokentype token = heredoc_identifier(p);
10789 if (token) return token < 0 ? 0 : token;
10790 }
10791 if (IS_AFTER_OPERATOR()) {
10792 SET_LEX_STATE(EXPR_ARG);
10793 }
10794 else {
10795 if (IS_lex_state(EXPR_CLASS))
10796 p->command_start = TRUE;
10797 SET_LEX_STATE(EXPR_BEG);
10798 }
10799 if (c == '=') {
10800 if ((c = nextc(p)) == '>') {
10801 return tCMP;
10802 }
10803 pushback(p, c);
10804 return tLEQ;
10805 }
10806 if (c == '<') {
10807 if ((c = nextc(p)) == '=') {
10808 set_yylval_id(idLTLT);
10809 SET_LEX_STATE(EXPR_BEG);
10810 return tOP_ASGN;
10811 }
10812 pushback(p, c);
10813 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10814 }
10815 pushback(p, c);
10816 return '<';
10817
10818 case '>':
10819 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10820 if ((c = nextc(p)) == '=') {
10821 return tGEQ;
10822 }
10823 if (c == '>') {
10824 if ((c = nextc(p)) == '=') {
10825 set_yylval_id(idGTGT);
10826 SET_LEX_STATE(EXPR_BEG);
10827 return tOP_ASGN;
10828 }
10829 pushback(p, c);
10830 return tRSHFT;
10831 }
10832 pushback(p, c);
10833 return '>';
10834
10835 case '"':
10836 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10837 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10838 p->lex.ptok = p->lex.pcur-1;
10839 return tSTRING_BEG;
10840
10841 case '`':
10842 if (IS_lex_state(EXPR_FNAME)) {
10843 SET_LEX_STATE(EXPR_ENDFN);
10844 return c;
10845 }
10846 if (IS_lex_state(EXPR_DOT)) {
10847 if (cmd_state)
10848 SET_LEX_STATE(EXPR_CMDARG);
10849 else
10850 SET_LEX_STATE(EXPR_ARG);
10851 return c;
10852 }
10853 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10854 return tXSTRING_BEG;
10855
10856 case '\'':
10857 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10858 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10859 p->lex.ptok = p->lex.pcur-1;
10860 return tSTRING_BEG;
10861
10862 case '?':
10863 return parse_qmark(p, space_seen);
10864
10865 case '&':
10866 if ((c = nextc(p)) == '&') {
10867 SET_LEX_STATE(EXPR_BEG);
10868 if ((c = nextc(p)) == '=') {
10869 set_yylval_id(idANDOP);
10870 SET_LEX_STATE(EXPR_BEG);
10871 return tOP_ASGN;
10872 }
10873 pushback(p, c);
10874 return tANDOP;
10875 }
10876 else if (c == '=') {
10877 set_yylval_id('&');
10878 SET_LEX_STATE(EXPR_BEG);
10879 return tOP_ASGN;
10880 }
10881 else if (c == '.') {
10882 set_yylval_id(idANDDOT);
10883 SET_LEX_STATE(EXPR_DOT);
10884 return tANDDOT;
10885 }
10886 pushback(p, c);
10887 if (IS_SPCARG(c)) {
10888 if ((c != ':') ||
10889 (c = peekc_n(p, 1)) == -1 ||
10890 !(c == '\'' || c == '"' ||
10891 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10892 rb_warning0("'&' interpreted as argument prefix");
10893 }
10894 c = tAMPER;
10895 }
10896 else if (IS_BEG()) {
10897 c = tAMPER;
10898 }
10899 else {
10900 c = warn_balanced('&', "&", "argument prefix");
10901 }
10902 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10903 return c;
10904
10905 case '|':
10906 if ((c = nextc(p)) == '|') {
10907 SET_LEX_STATE(EXPR_BEG);
10908 if ((c = nextc(p)) == '=') {
10909 set_yylval_id(idOROP);
10910 SET_LEX_STATE(EXPR_BEG);
10911 return tOP_ASGN;
10912 }
10913 pushback(p, c);
10914 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10915 c = '|';
10916 pushback(p, '|');
10917 return c;
10918 }
10919 return tOROP;
10920 }
10921 if (c == '=') {
10922 set_yylval_id('|');
10923 SET_LEX_STATE(EXPR_BEG);
10924 return tOP_ASGN;
10925 }
10926 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10927 pushback(p, c);
10928 return '|';
10929
10930 case '+':
10931 c = nextc(p);
10932 if (IS_AFTER_OPERATOR()) {
10933 SET_LEX_STATE(EXPR_ARG);
10934 if (c == '@') {
10935 return tUPLUS;
10936 }
10937 pushback(p, c);
10938 return '+';
10939 }
10940 if (c == '=') {
10941 set_yylval_id('+');
10942 SET_LEX_STATE(EXPR_BEG);
10943 return tOP_ASGN;
10944 }
10945 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10946 SET_LEX_STATE(EXPR_BEG);
10947 pushback(p, c);
10948 if (c != -1 && ISDIGIT(c)) {
10949 return parse_numeric(p, '+');
10950 }
10951 return tUPLUS;
10952 }
10953 SET_LEX_STATE(EXPR_BEG);
10954 pushback(p, c);
10955 return warn_balanced('+', "+", "unary operator");
10956
10957 case '-':
10958 c = nextc(p);
10959 if (IS_AFTER_OPERATOR()) {
10960 SET_LEX_STATE(EXPR_ARG);
10961 if (c == '@') {
10962 return tUMINUS;
10963 }
10964 pushback(p, c);
10965 return '-';
10966 }
10967 if (c == '=') {
10968 set_yylval_id('-');
10969 SET_LEX_STATE(EXPR_BEG);
10970 return tOP_ASGN;
10971 }
10972 if (c == '>') {
10973 SET_LEX_STATE(EXPR_ENDFN);
10974 yylval.num = p->lex.lpar_beg;
10975 p->lex.lpar_beg = p->lex.paren_nest;
10976 return tLAMBDA;
10977 }
10978 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10979 SET_LEX_STATE(EXPR_BEG);
10980 pushback(p, c);
10981 if (c != -1 && ISDIGIT(c)) {
10982 return tUMINUS_NUM;
10983 }
10984 return tUMINUS;
10985 }
10986 SET_LEX_STATE(EXPR_BEG);
10987 pushback(p, c);
10988 return warn_balanced('-', "-", "unary operator");
10989
10990 case '.': {
10991 int is_beg = IS_BEG();
10992 SET_LEX_STATE(EXPR_BEG);
10993 if ((c = nextc(p)) == '.') {
10994 if ((c = nextc(p)) == '.') {
10995 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10996 SET_LEX_STATE(EXPR_ENDARG);
10997 return tBDOT3;
10998 }
10999 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
11000 rb_warn0("... at EOL, should be parenthesized?");
11001 }
11002 return is_beg ? tBDOT3 : tDOT3;
11003 }
11004 pushback(p, c);
11005 return is_beg ? tBDOT2 : tDOT2;
11006 }
11007 pushback(p, c);
11008 if (c != -1 && ISDIGIT(c)) {
11009 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
11010 parse_numeric(p, '.');
11011 if (ISDIGIT(prev)) {
11012 yyerror0("unexpected fraction part after numeric literal");
11013 }
11014 else {
11015 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
11016 }
11017 set_yylval_id(idNULL);
11018 SET_LEX_STATE(EXPR_END);
11019 p->lex.ptok = p->lex.pcur;
11020 goto retry;
11021 }
11022 set_yylval_id('.');
11023 SET_LEX_STATE(EXPR_DOT);
11024 return '.';
11025 }
11026
11027 case '0': case '1': case '2': case '3': case '4':
11028 case '5': case '6': case '7': case '8': case '9':
11029 return parse_numeric(p, c);
11030
11031 case ')':
11032 COND_POP();
11033 CMDARG_POP();
11034 SET_LEX_STATE(EXPR_ENDFN);
11035 p->lex.paren_nest--;
11036 return c;
11037
11038 case ']':
11039 COND_POP();
11040 CMDARG_POP();
11041 SET_LEX_STATE(EXPR_END);
11042 p->lex.paren_nest--;
11043 return c;
11044
11045 case '}':
11046 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
11047 if (!p->lex.brace_nest--) return tSTRING_DEND;
11048 COND_POP();
11049 CMDARG_POP();
11050 SET_LEX_STATE(EXPR_END);
11051 p->lex.paren_nest--;
11052 return c;
11053
11054 case ':':
11055 c = nextc(p);
11056 if (c == ':') {
11057 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
11058 SET_LEX_STATE(EXPR_BEG);
11059 return tCOLON3;
11060 }
11061 set_yylval_id(idCOLON2);
11062 SET_LEX_STATE(EXPR_DOT);
11063 return tCOLON2;
11064 }
11065 if (IS_END() || ISSPACE(c) || c == '#') {
11066 pushback(p, c);
11067 c = warn_balanced(':', ":", "symbol literal");
11068 SET_LEX_STATE(EXPR_BEG);
11069 return c;
11070 }
11071 switch (c) {
11072 case '\'':
11073 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11074 break;
11075 case '"':
11076 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11077 break;
11078 default:
11079 pushback(p, c);
11080 break;
11081 }
11082 SET_LEX_STATE(EXPR_FNAME);
11083 return tSYMBEG;
11084
11085 case '/':
11086 if (IS_BEG()) {
11087 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11088 return tREGEXP_BEG;
11089 }
11090 if ((c = nextc(p)) == '=') {
11091 set_yylval_id('/');
11092 SET_LEX_STATE(EXPR_BEG);
11093 return tOP_ASGN;
11094 }
11095 pushback(p, c);
11096 if (IS_SPCARG(c)) {
11097 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11098 return tREGEXP_BEG;
11099 }
11100 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11101 return warn_balanced('/', "/", "regexp literal");
11102
11103 case '^':
11104 if ((c = nextc(p)) == '=') {
11105 set_yylval_id('^');
11106 SET_LEX_STATE(EXPR_BEG);
11107 return tOP_ASGN;
11108 }
11109 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11110 pushback(p, c);
11111 return '^';
11112
11113 case ';':
11114 SET_LEX_STATE(EXPR_BEG);
11115 p->command_start = TRUE;
11116 return ';';
11117
11118 case ',':
11119 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11120 return ',';
11121
11122 case '~':
11123 if (IS_AFTER_OPERATOR()) {
11124 if ((c = nextc(p)) != '@') {
11125 pushback(p, c);
11126 }
11127 SET_LEX_STATE(EXPR_ARG);
11128 }
11129 else {
11130 SET_LEX_STATE(EXPR_BEG);
11131 }
11132 return '~';
11133
11134 case '(':
11135 if (IS_BEG()) {
11136 c = tLPAREN;
11137 }
11138 else if (!space_seen) {
11139 /* foo( ... ) => method call, no ambiguity */
11140 }
11141 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11142 c = tLPAREN_ARG;
11143 }
11144 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11145 rb_warning0("parentheses after method name is interpreted as "
11146 "an argument list, not a decomposed argument");
11147 }
11148 p->lex.paren_nest++;
11149 COND_PUSH(0);
11150 CMDARG_PUSH(0);
11151 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11152 return c;
11153
11154 case '[':
11155 p->lex.paren_nest++;
11156 if (IS_AFTER_OPERATOR()) {
11157 if ((c = nextc(p)) == ']') {
11158 p->lex.paren_nest--;
11159 SET_LEX_STATE(EXPR_ARG);
11160 if ((c = nextc(p)) == '=') {
11161 return tASET;
11162 }
11163 pushback(p, c);
11164 return tAREF;
11165 }
11166 pushback(p, c);
11167 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11168 return '[';
11169 }
11170 else if (IS_BEG()) {
11171 c = tLBRACK;
11172 }
11173 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11174 c = tLBRACK;
11175 }
11176 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11177 COND_PUSH(0);
11178 CMDARG_PUSH(0);
11179 return c;
11180
11181 case '{':
11182 ++p->lex.brace_nest;
11183 if (lambda_beginning_p())
11184 c = tLAMBEG;
11185 else if (IS_lex_state(EXPR_LABELED))
11186 c = tLBRACE; /* hash */
11187 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11188 c = '{'; /* block (primary) */
11189 else if (IS_lex_state(EXPR_ENDARG))
11190 c = tLBRACE_ARG; /* block (expr) */
11191 else
11192 c = tLBRACE; /* hash */
11193 if (c != tLBRACE) {
11194 p->command_start = TRUE;
11195 SET_LEX_STATE(EXPR_BEG);
11196 }
11197 else {
11198 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11199 }
11200 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11201 COND_PUSH(0);
11202 CMDARG_PUSH(0);
11203 return c;
11204
11205 case '\\':
11206 c = nextc(p);
11207 if (c == '\n') {
11208 space_seen = 1;
11209 dispatch_scan_event(p, tSP);
11210 goto retry; /* skip \\n */
11211 }
11212 if (c == ' ') return tSP;
11213 if (ISSPACE(c)) return c;
11214 pushback(p, c);
11215 return '\\';
11216
11217 case '%':
11218 return parse_percent(p, space_seen, last_state);
11219
11220 case '$':
11221 return parse_gvar(p, last_state);
11222
11223 case '@':
11224 return parse_atmark(p, last_state);
11225
11226 case '_':
11227 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11228 p->ruby__end__seen = 1;
11229 p->eofp = 1;
11230#ifdef RIPPER
11231 lex_goto_eol(p);
11232 dispatch_scan_event(p, k__END__);
11233#endif
11234 return END_OF_INPUT;
11235 }
11236 newtok(p);
11237 break;
11238
11239 default:
11240 if (!parser_is_identchar(p)) {
11241 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11242 token_flush(p);
11243 goto retry;
11244 }
11245
11246 newtok(p);
11247 break;
11248 }
11249
11250 return parse_ident(p, c, cmd_state);
11251}
11252
11253static enum yytokentype
11254yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11255{
11256 enum yytokentype t;
11257
11258 p->lval = lval;
11259 lval->node = 0;
11260 p->yylloc = yylloc;
11261
11262 t = parser_yylex(p);
11263
11264 if (has_delayed_token(p))
11265 dispatch_delayed_token(p, t);
11266 else if (t != END_OF_INPUT)
11267 dispatch_scan_event(p, t);
11268
11269 return t;
11270}
11271
11272#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11273
11274static NODE*
11275node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11276{
11277 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11278
11279 rb_node_init(n, type);
11280 return n;
11281}
11282
11283static NODE *
11284nd_set_loc(NODE *nd, const YYLTYPE *loc)
11285{
11286 nd->nd_loc = *loc;
11287 nd_set_line(nd, loc->beg_pos.lineno);
11288 return nd;
11289}
11290
11291static NODE*
11292node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11293{
11294 NODE *n = node_new_internal(p, type, size, alignment);
11295
11296 nd_set_loc(n, loc);
11297 nd_set_node_id(n, parser_get_node_id(p));
11298 return n;
11299}
11300
11301#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11302
11303static rb_node_scope_t *
11304rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11305{
11306 rb_ast_id_table_t *nd_tbl;
11307 nd_tbl = local_tbl(p);
11308 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11309 n->nd_tbl = nd_tbl;
11310 n->nd_body = nd_body;
11311 n->nd_parent = nd_parent;
11312 n->nd_args = nd_args;
11313
11314 return n;
11315}
11316
11317static rb_node_scope_t *
11318rb_node_scope_new2(struct parser_params *p, rb_ast_id_table_t *nd_tbl, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11319{
11320 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11321 n->nd_tbl = nd_tbl;
11322 n->nd_body = nd_body;
11323 n->nd_parent = nd_parent;
11324 n->nd_args = nd_args;
11325
11326 return n;
11327}
11328
11329static rb_node_defn_t *
11330rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11331{
11332 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11333 n->nd_mid = nd_mid;
11334 n->nd_defn = nd_defn;
11335
11336 return n;
11337}
11338
11339static rb_node_defs_t *
11340rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11341{
11342 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11343 n->nd_recv = nd_recv;
11344 n->nd_mid = nd_mid;
11345 n->nd_defn = nd_defn;
11346
11347 return n;
11348}
11349
11350static rb_node_block_t *
11351rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11352{
11353 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11354 n->nd_head = nd_head;
11355 n->nd_end = (NODE *)n;
11356 n->nd_next = 0;
11357
11358 return n;
11359}
11360
11361static rb_node_for_t *
11362rb_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)
11363{
11364 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11365 n->nd_body = nd_body;
11366 n->nd_iter = nd_iter;
11367 n->for_keyword_loc = *for_keyword_loc;
11368 n->in_keyword_loc = *in_keyword_loc;
11369 n->do_keyword_loc = *do_keyword_loc;
11370 n->end_keyword_loc = *end_keyword_loc;
11371
11372 return n;
11373}
11374
11375static rb_node_for_masgn_t *
11376rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11377{
11378 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11379 n->nd_var = nd_var;
11380
11381 return n;
11382}
11383
11384static rb_node_retry_t *
11385rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11386{
11387 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11388
11389 return n;
11390}
11391
11392static rb_node_begin_t *
11393rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11394{
11395 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11396 n->nd_body = nd_body;
11397
11398 return n;
11399}
11400
11401static rb_node_rescue_t *
11402rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11403{
11404 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11405 n->nd_head = nd_head;
11406 n->nd_resq = nd_resq;
11407 n->nd_else = nd_else;
11408
11409 return n;
11410}
11411
11412static rb_node_resbody_t *
11413rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11414{
11415 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11416 n->nd_args = nd_args;
11417 n->nd_exc_var = nd_exc_var;
11418 n->nd_body = nd_body;
11419 n->nd_next = nd_next;
11420
11421 return n;
11422}
11423
11424static rb_node_ensure_t *
11425rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11426{
11427 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11428 n->nd_head = nd_head;
11429 n->nd_ensr = nd_ensr;
11430
11431 return n;
11432}
11433
11434static rb_node_and_t *
11435rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11436{
11437 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11438 n->nd_1st = nd_1st;
11439 n->nd_2nd = nd_2nd;
11440 n->operator_loc = *operator_loc;
11441
11442 return n;
11443}
11444
11445static rb_node_or_t *
11446rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11447{
11448 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11449 n->nd_1st = nd_1st;
11450 n->nd_2nd = nd_2nd;
11451 n->operator_loc = *operator_loc;
11452
11453 return n;
11454}
11455
11456static rb_node_return_t *
11457rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11458{
11459 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11460 n->nd_stts = nd_stts;
11461 n->keyword_loc = *keyword_loc;
11462 return n;
11463}
11464
11465static rb_node_yield_t *
11466rb_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)
11467{
11468 if (nd_head) no_blockarg(p, nd_head);
11469
11470 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11471 n->nd_head = nd_head;
11472 n->keyword_loc = *keyword_loc;
11473 n->lparen_loc = *lparen_loc;
11474 n->rparen_loc = *rparen_loc;
11475
11476 return n;
11477}
11478
11479static rb_node_if_t *
11480rb_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)
11481{
11482 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11483 n->nd_cond = nd_cond;
11484 n->nd_body = nd_body;
11485 n->nd_else = nd_else;
11486 n->if_keyword_loc = *if_keyword_loc;
11487 n->then_keyword_loc = *then_keyword_loc;
11488 n->end_keyword_loc = *end_keyword_loc;
11489
11490 return n;
11491}
11492
11493static rb_node_unless_t *
11494rb_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)
11495{
11496 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11497 n->nd_cond = nd_cond;
11498 n->nd_body = nd_body;
11499 n->nd_else = nd_else;
11500 n->keyword_loc = *keyword_loc;
11501 n->then_keyword_loc = *then_keyword_loc;
11502 n->end_keyword_loc = *end_keyword_loc;
11503
11504 return n;
11505}
11506
11507static rb_node_class_t *
11508rb_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)
11509{
11510 /* Keep the order of node creation */
11511 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11512 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11513 RNODE_SCOPE(scope)->nd_parent = &n->node;
11514 n->nd_cpath = nd_cpath;
11515 n->nd_body = scope;
11516 n->nd_super = nd_super;
11517 n->class_keyword_loc = *class_keyword_loc;
11518 n->inheritance_operator_loc = *inheritance_operator_loc;
11519 n->end_keyword_loc = *end_keyword_loc;
11520
11521 return n;
11522}
11523
11524static rb_node_sclass_t *
11525rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *operator_loc, const YYLTYPE *end_keyword_loc)
11526{
11527 /* Keep the order of node creation */
11528 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11529 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11530 RNODE_SCOPE(scope)->nd_parent = &n->node;
11531 n->nd_recv = nd_recv;
11532 n->nd_body = scope;
11533 n->class_keyword_loc = *class_keyword_loc;
11534 n->operator_loc = *operator_loc;
11535 n->end_keyword_loc = *end_keyword_loc;
11536
11537 return n;
11538}
11539
11540static rb_node_module_t *
11541rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *module_keyword_loc, const YYLTYPE *end_keyword_loc)
11542{
11543 /* Keep the order of node creation */
11544 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11545 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11546 RNODE_SCOPE(scope)->nd_parent = &n->node;
11547 n->nd_cpath = nd_cpath;
11548 n->nd_body = scope;
11549 n->module_keyword_loc = *module_keyword_loc;
11550 n->end_keyword_loc = *end_keyword_loc;
11551
11552 return n;
11553}
11554
11555static rb_node_iter_t *
11556rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11557{
11558 /* Keep the order of node creation */
11559 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11560 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11561 RNODE_SCOPE(scope)->nd_parent = &n->node;
11562 n->nd_body = scope;
11563 n->nd_iter = 0;
11564
11565 return n;
11566}
11567
11568static rb_node_lambda_t *
11569rb_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)
11570{
11571 /* Keep the order of node creation */
11572 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11573 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11574 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11575 RNODE_SCOPE(scope)->nd_parent = &n->node;
11576 n->nd_body = scope;
11577 n->operator_loc = *operator_loc;
11578 n->opening_loc = *opening_loc;
11579 n->closing_loc = *closing_loc;
11580
11581 return n;
11582}
11583
11584static rb_node_case_t *
11585rb_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)
11586{
11587 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11588 n->nd_head = nd_head;
11589 n->nd_body = nd_body;
11590 n->case_keyword_loc = *case_keyword_loc;
11591 n->end_keyword_loc = *end_keyword_loc;
11592
11593 return n;
11594}
11595
11596static rb_node_case2_t *
11597rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11598{
11599 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11600 n->nd_head = 0;
11601 n->nd_body = nd_body;
11602 n->case_keyword_loc = *case_keyword_loc;
11603 n->end_keyword_loc = *end_keyword_loc;
11604
11605 return n;
11606}
11607
11608static rb_node_case3_t *
11609rb_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)
11610{
11611 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11612 n->nd_head = nd_head;
11613 n->nd_body = nd_body;
11614 n->case_keyword_loc = *case_keyword_loc;
11615 n->end_keyword_loc = *end_keyword_loc;
11616
11617 return n;
11618}
11619
11620static rb_node_when_t *
11621rb_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)
11622{
11623 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11624 n->nd_head = nd_head;
11625 n->nd_body = nd_body;
11626 n->nd_next = nd_next;
11627 n->keyword_loc = *keyword_loc;
11628 n->then_keyword_loc = *then_keyword_loc;
11629
11630 return n;
11631}
11632
11633static rb_node_in_t *
11634rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *operator_loc)
11635{
11636 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11637 n->nd_head = nd_head;
11638 n->nd_body = nd_body;
11639 n->nd_next = nd_next;
11640 n->in_keyword_loc = *in_keyword_loc;
11641 n->then_keyword_loc = *then_keyword_loc;
11642 n->operator_loc = *operator_loc;
11643
11644 return n;
11645}
11646
11647static rb_node_while_t *
11648rb_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)
11649{
11650 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11651 n->nd_cond = nd_cond;
11652 n->nd_body = nd_body;
11653 n->nd_state = nd_state;
11654 n->keyword_loc = *keyword_loc;
11655 n->closing_loc = *closing_loc;
11656
11657 return n;
11658}
11659
11660static rb_node_until_t *
11661rb_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)
11662{
11663 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11664 n->nd_cond = nd_cond;
11665 n->nd_body = nd_body;
11666 n->nd_state = nd_state;
11667 n->keyword_loc = *keyword_loc;
11668 n->closing_loc = *closing_loc;
11669
11670 return n;
11671}
11672
11673static rb_node_colon2_t *
11674rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11675{
11676 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11677 n->nd_head = nd_head;
11678 n->nd_mid = nd_mid;
11679 n->delimiter_loc = *delimiter_loc;
11680 n->name_loc = *name_loc;
11681
11682 return n;
11683}
11684
11685static rb_node_colon3_t *
11686rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11687{
11688 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11689 n->nd_mid = nd_mid;
11690 n->delimiter_loc = *delimiter_loc;
11691 n->name_loc = *name_loc;
11692
11693 return n;
11694}
11695
11696static rb_node_dot2_t *
11697rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11698{
11699 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11700 n->nd_beg = nd_beg;
11701 n->nd_end = nd_end;
11702 n->operator_loc = *operator_loc;
11703
11704 return n;
11705}
11706
11707static rb_node_dot3_t *
11708rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11709{
11710 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11711 n->nd_beg = nd_beg;
11712 n->nd_end = nd_end;
11713 n->operator_loc = *operator_loc;
11714
11715 return n;
11716}
11717
11718static rb_node_self_t *
11719rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11720{
11721 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11722 n->nd_state = 1;
11723
11724 return n;
11725}
11726
11727static rb_node_nil_t *
11728rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11729{
11730 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11731
11732 return n;
11733}
11734
11735static rb_node_true_t *
11736rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11737{
11738 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11739
11740 return n;
11741}
11742
11743static rb_node_false_t *
11744rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11745{
11746 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11747
11748 return n;
11749}
11750
11751static rb_node_super_t *
11752rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11753 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11754{
11755 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11756 n->nd_args = nd_args;
11757 n->keyword_loc = *keyword_loc;
11758 n->lparen_loc = *lparen_loc;
11759 n->rparen_loc = *rparen_loc;
11760
11761 return n;
11762}
11763
11764static rb_node_zsuper_t *
11765rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11766{
11767 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11768
11769 return n;
11770}
11771
11772static rb_node_match2_t *
11773rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11774{
11775 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11776 n->nd_recv = nd_recv;
11777 n->nd_value = nd_value;
11778 n->nd_args = 0;
11779
11780 return n;
11781}
11782
11783static rb_node_match3_t *
11784rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11785{
11786 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11787 n->nd_recv = nd_recv;
11788 n->nd_value = nd_value;
11789
11790 return n;
11791}
11792
11793static rb_node_list_t *
11794rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11795{
11796 return rb_node_list_new2(p, nd_head, 1, 0, loc);
11797}
11798
11799static rb_node_list_t *
11800rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11801{
11802 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11803 n->nd_head = nd_head;
11804 n->as.nd_alen = nd_alen;
11805 n->nd_next = nd_next;
11806
11807 return n;
11808}
11809
11810static rb_node_zlist_t *
11811rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11812{
11813 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11814
11815 return n;
11816}
11817
11818static rb_node_hash_t *
11819rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11820{
11821 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11822 n->nd_head = nd_head;
11823 n->nd_brace = 0;
11824
11825 return n;
11826}
11827
11828static rb_node_masgn_t *
11829rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11830{
11831 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11832 n->nd_head = nd_head;
11833 n->nd_value = 0;
11834 n->nd_args = nd_args;
11835
11836 return n;
11837}
11838
11839static rb_node_gasgn_t *
11840rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11841{
11842 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11843 n->nd_vid = nd_vid;
11844 n->nd_value = nd_value;
11845
11846 return n;
11847}
11848
11849static rb_node_lasgn_t *
11850rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11851{
11852 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11853 n->nd_vid = nd_vid;
11854 n->nd_value = nd_value;
11855
11856 return n;
11857}
11858
11859static rb_node_dasgn_t *
11860rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11861{
11862 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11863 n->nd_vid = nd_vid;
11864 n->nd_value = nd_value;
11865
11866 return n;
11867}
11868
11869static rb_node_iasgn_t *
11870rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11871{
11872 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11873 n->nd_vid = nd_vid;
11874 n->nd_value = nd_value;
11875
11876 return n;
11877}
11878
11879static rb_node_cvasgn_t *
11880rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11881{
11882 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11883 n->nd_vid = nd_vid;
11884 n->nd_value = nd_value;
11885
11886 return n;
11887}
11888
11889static rb_node_op_asgn1_t *
11890rb_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)
11891{
11892 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11893 n->nd_recv = nd_recv;
11894 n->nd_mid = nd_mid;
11895 n->nd_index = index;
11896 n->nd_rvalue = rvalue;
11897 n->call_operator_loc = *call_operator_loc;
11898 n->opening_loc = *opening_loc;
11899 n->closing_loc = *closing_loc;
11900 n->binary_operator_loc = *binary_operator_loc;
11901
11902 return n;
11903}
11904
11905static rb_node_op_asgn2_t *
11906rb_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)
11907{
11908 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11909 n->nd_recv = nd_recv;
11910 n->nd_value = nd_value;
11911 n->nd_vid = nd_vid;
11912 n->nd_mid = nd_mid;
11913 n->nd_aid = nd_aid;
11914 n->call_operator_loc = *call_operator_loc;
11915 n->message_loc = *message_loc;
11916 n->binary_operator_loc = *binary_operator_loc;
11917
11918 return n;
11919}
11920
11921static rb_node_op_asgn_or_t *
11922rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11923{
11924 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11925 n->nd_head = nd_head;
11926 n->nd_value = nd_value;
11927
11928 return n;
11929}
11930
11931static rb_node_op_asgn_and_t *
11932rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11933{
11934 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11935 n->nd_head = nd_head;
11936 n->nd_value = nd_value;
11937
11938 return n;
11939}
11940
11941static rb_node_gvar_t *
11942rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11943{
11944 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11945 n->nd_vid = nd_vid;
11946
11947 return n;
11948}
11949
11950static rb_node_lvar_t *
11951rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11952{
11953 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11954 n->nd_vid = nd_vid;
11955
11956 return n;
11957}
11958
11959static rb_node_dvar_t *
11960rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11961{
11962 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11963 n->nd_vid = nd_vid;
11964
11965 return n;
11966}
11967
11968static rb_node_ivar_t *
11969rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11970{
11971 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11972 n->nd_vid = nd_vid;
11973
11974 return n;
11975}
11976
11977static rb_node_const_t *
11978rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11979{
11980 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11981 n->nd_vid = nd_vid;
11982
11983 return n;
11984}
11985
11986static rb_node_cvar_t *
11987rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11988{
11989 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11990 n->nd_vid = nd_vid;
11991
11992 return n;
11993}
11994
11995static rb_node_nth_ref_t *
11996rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11997{
11998 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11999 n->nd_nth = nd_nth;
12000
12001 return n;
12002}
12003
12004static rb_node_back_ref_t *
12005rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
12006{
12007 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
12008 n->nd_nth = nd_nth;
12009
12010 return n;
12011}
12012
12013static rb_node_integer_t *
12014rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
12015{
12016 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
12017 n->val = val;
12018 n->minus = FALSE;
12019 n->base = base;
12020
12021 return n;
12022}
12023
12024static rb_node_float_t *
12025rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
12026{
12027 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
12028 n->val = val;
12029 n->minus = FALSE;
12030
12031 return n;
12032}
12033
12034static rb_node_rational_t *
12035rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
12036{
12037 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
12038 n->val = val;
12039 n->minus = FALSE;
12040 n->base = base;
12041 n->seen_point = seen_point;
12042
12043 return n;
12044}
12045
12046static rb_node_imaginary_t *
12047rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
12048{
12049 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
12050 n->val = val;
12051 n->minus = FALSE;
12052 n->base = base;
12053 n->seen_point = seen_point;
12054 n->type = numeric_type;
12055
12056 return n;
12057}
12058
12059static rb_node_str_t *
12060rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12061{
12062 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12063 n->string = string;
12064
12065 return n;
12066}
12067
12068/* TODO; Use union for NODE_DSTR2 */
12069static rb_node_dstr_t *
12070rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12071{
12072 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12073 n->string = string;
12074 n->as.nd_alen = nd_alen;
12075 n->nd_next = (rb_node_list_t *)nd_next;
12076
12077 return n;
12078}
12079
12080static rb_node_dstr_t *
12081rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12082{
12083 return rb_node_dstr_new0(p, string, 1, 0, loc);
12084}
12085
12086static rb_node_xstr_t *
12087rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12088{
12089 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12090 n->string = string;
12091
12092 return n;
12093}
12094
12095static rb_node_dxstr_t *
12096rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12097{
12098 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12099 n->string = string;
12100 n->as.nd_alen = nd_alen;
12101 n->nd_next = (rb_node_list_t *)nd_next;
12102
12103 return n;
12104}
12105
12106static rb_node_sym_t *
12107rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12108{
12109 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12110 n->string = rb_str_to_parser_string(p, str);
12111
12112 return n;
12113}
12114
12115static rb_node_dsym_t *
12116rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12117{
12118 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12119 n->string = string;
12120 n->as.nd_alen = nd_alen;
12121 n->nd_next = (rb_node_list_t *)nd_next;
12122
12123 return n;
12124}
12125
12126static rb_node_evstr_t *
12127rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12128{
12129 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12130 n->nd_body = nd_body;
12131 n->opening_loc = *opening_loc;
12132 n->closing_loc = *closing_loc;
12133
12134 return n;
12135}
12136
12137static rb_node_regx_t *
12138rb_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)
12139{
12140 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12141 n->string = string;
12142 n->options = options & RE_OPTION_MASK;
12143 n->opening_loc = *opening_loc;
12144 n->content_loc = *content_loc;
12145 n->closing_loc = *closing_loc;
12146
12147 return n;
12148}
12149
12150static rb_node_call_t *
12151rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12152{
12153 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12154 n->nd_recv = nd_recv;
12155 n->nd_mid = nd_mid;
12156 n->nd_args = nd_args;
12157
12158 return n;
12159}
12160
12161static rb_node_opcall_t *
12162rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12163{
12164 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12165 n->nd_recv = nd_recv;
12166 n->nd_mid = nd_mid;
12167 n->nd_args = nd_args;
12168
12169 return n;
12170}
12171
12172static rb_node_fcall_t *
12173rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12174{
12175 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12176 n->nd_mid = nd_mid;
12177 n->nd_args = nd_args;
12178
12179 return n;
12180}
12181
12182static rb_node_qcall_t *
12183rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12184{
12185 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12186 n->nd_recv = nd_recv;
12187 n->nd_mid = nd_mid;
12188 n->nd_args = nd_args;
12189
12190 return n;
12191}
12192
12193static rb_node_vcall_t *
12194rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12195{
12196 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12197 n->nd_mid = nd_mid;
12198
12199 return n;
12200}
12201
12202static rb_node_once_t *
12203rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12204{
12205 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12206 n->nd_body = nd_body;
12207
12208 return n;
12209}
12210
12211static rb_node_args_t *
12212rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12213{
12214 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12215 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12216
12217 return n;
12218}
12219
12220static rb_node_args_aux_t *
12221rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12222{
12223 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12224 n->nd_pid = nd_pid;
12225 n->nd_plen = nd_plen;
12226 n->nd_next = 0;
12227
12228 return n;
12229}
12230
12231static rb_node_opt_arg_t *
12232rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12233{
12234 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12235 n->nd_body = nd_body;
12236 n->nd_next = 0;
12237
12238 return n;
12239}
12240
12241static rb_node_kw_arg_t *
12242rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12243{
12244 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12245 n->nd_body = nd_body;
12246 n->nd_next = 0;
12247
12248 return n;
12249}
12250
12251static rb_node_postarg_t *
12252rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12253{
12254 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12255 n->nd_1st = nd_1st;
12256 n->nd_2nd = nd_2nd;
12257
12258 return n;
12259}
12260
12261static rb_node_argscat_t *
12262rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12263{
12264 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12265 n->nd_head = nd_head;
12266 n->nd_body = nd_body;
12267
12268 return n;
12269}
12270
12271static rb_node_argspush_t *
12272rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12273{
12274 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12275 n->nd_head = nd_head;
12276 n->nd_body = nd_body;
12277
12278 return n;
12279}
12280
12281static rb_node_splat_t *
12282rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12283{
12284 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12285 n->nd_head = nd_head;
12286 n->operator_loc = *operator_loc;
12287
12288 return n;
12289}
12290
12291static rb_node_block_pass_t *
12292rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12293{
12294 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12295 n->forwarding = 0;
12296 n->nd_head = 0;
12297 n->nd_body = nd_body;
12298 n->operator_loc = *operator_loc;
12299
12300 return n;
12301}
12302
12303static rb_node_alias_t *
12304rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12305{
12306 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12307 n->nd_1st = nd_1st;
12308 n->nd_2nd = nd_2nd;
12309 n->keyword_loc = *keyword_loc;
12310
12311 return n;
12312}
12313
12314static rb_node_valias_t *
12315rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12316{
12317 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12318 n->nd_alias = nd_alias;
12319 n->nd_orig = nd_orig;
12320 n->keyword_loc = *keyword_loc;
12321
12322 return n;
12323}
12324
12325static rb_node_undef_t *
12326rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12327{
12328 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12329 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12330 n->keyword_loc = NULL_LOC;
12331 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12332
12333 return n;
12334}
12335
12336static rb_node_errinfo_t *
12337rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12338{
12339 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12340
12341 return n;
12342}
12343
12344static rb_node_defined_t *
12345rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12346{
12347 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12348 n->nd_head = nd_head;
12349 n->keyword_loc = *keyword_loc;
12350
12351 return n;
12352}
12353
12354static rb_node_postexe_t *
12355rb_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)
12356{
12357 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12358 n->nd_body = nd_body;
12359 n->keyword_loc = *keyword_loc;
12360 n->opening_loc = *opening_loc;
12361 n->closing_loc = *closing_loc;
12362
12363 return n;
12364}
12365
12366static rb_node_attrasgn_t *
12367rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12368{
12369 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12370 n->nd_recv = nd_recv;
12371 n->nd_mid = nd_mid;
12372 n->nd_args = nd_args;
12373
12374 return n;
12375}
12376
12377static rb_node_aryptn_t *
12378rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12379{
12380 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12381 n->nd_pconst = 0;
12382 n->pre_args = pre_args;
12383 n->rest_arg = rest_arg;
12384 n->post_args = post_args;
12385
12386 return n;
12387}
12388
12389static rb_node_hshptn_t *
12390rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12391{
12392 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12393 n->nd_pconst = nd_pconst;
12394 n->nd_pkwargs = nd_pkwargs;
12395 n->nd_pkwrestarg = nd_pkwrestarg;
12396
12397 return n;
12398}
12399
12400static rb_node_fndptn_t *
12401rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12402{
12403 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12404 n->nd_pconst = 0;
12405 n->pre_rest_arg = pre_rest_arg;
12406 n->args = args;
12407 n->post_rest_arg = post_rest_arg;
12408
12409 return n;
12410}
12411
12412static rb_node_line_t *
12413rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12414{
12415 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12416
12417 return n;
12418}
12419
12420static rb_node_file_t *
12421rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12422{
12423 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12424 n->path = rb_str_to_parser_string(p, str);
12425
12426 return n;
12427}
12428
12429static rb_node_encoding_t *
12430rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12431{
12432 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12433 n->enc = p->enc;
12434
12435 return n;
12436}
12437
12438static rb_node_cdecl_t *
12439rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12440{
12441 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12442 n->nd_vid = nd_vid;
12443 n->nd_value = nd_value;
12444 n->nd_else = nd_else;
12445 n->shareability = shareability;
12446
12447 return n;
12448}
12449
12450static rb_node_op_cdecl_t *
12451rb_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)
12452{
12453 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12454 n->nd_head = nd_head;
12455 n->nd_value = nd_value;
12456 n->nd_aid = nd_aid;
12457 n->shareability = shareability;
12458
12459 return n;
12460}
12461
12462static rb_node_error_t *
12463rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12464{
12465 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12466
12467 return n;
12468}
12469
12470static rb_node_break_t *
12471rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12472{
12473 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12474 n->nd_stts = nd_stts;
12475 n->nd_chain = 0;
12476 n->keyword_loc = *keyword_loc;
12477
12478 return n;
12479}
12480
12481static rb_node_next_t *
12482rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12483{
12484 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12485 n->nd_stts = nd_stts;
12486 n->nd_chain = 0;
12487 n->keyword_loc = *keyword_loc;
12488
12489 return n;
12490}
12491
12492static rb_node_redo_t *
12493rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12494{
12495 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12496 n->nd_chain = 0;
12497 n->keyword_loc = *keyword_loc;
12498
12499 return n;
12500}
12501
12502static rb_node_def_temp_t *
12503rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12504{
12505 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12506 n->save.numparam_save = 0;
12507 n->save.max_numparam = 0;
12508 n->save.ctxt = p->ctxt;
12509 n->nd_def = 0;
12510 n->nd_mid = 0;
12511
12512 return n;
12513}
12514
12515static rb_node_def_temp_t *
12516def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12517{
12518 n->save.numparam_save = numparam_push(p);
12519 n->save.max_numparam = p->max_numparam;
12520 return n;
12521}
12522
12523#ifndef RIPPER
12524static enum node_type
12525nodetype(NODE *node) /* for debug */
12526{
12527 return (enum node_type)nd_type(node);
12528}
12529
12530static int
12531nodeline(NODE *node)
12532{
12533 return nd_line(node);
12534}
12535#endif
12536
12537static NODE*
12538newline_node(NODE *node)
12539{
12540 if (node) {
12541 node = remove_begin(node);
12542 nd_set_fl_newline(node);
12543 }
12544 return node;
12545}
12546
12547static void
12548fixpos(NODE *node, NODE *orig)
12549{
12550 if (!node) return;
12551 if (!orig) return;
12552 nd_set_line(node, nd_line(orig));
12553}
12554
12555static NODE*
12556block_append(struct parser_params *p, NODE *head, NODE *tail)
12557{
12558 NODE *end, *h = head, *nd;
12559
12560 if (tail == 0) return head;
12561
12562 if (h == 0) return tail;
12563 switch (nd_type(h)) {
12564 default:
12565 h = end = NEW_BLOCK(head, &head->nd_loc);
12566 head = end;
12567 break;
12568 case NODE_BLOCK:
12569 end = RNODE_BLOCK(h)->nd_end;
12570 break;
12571 }
12572
12573 nd = RNODE_BLOCK(end)->nd_head;
12574 switch (nd_type(nd)) {
12575 case NODE_RETURN:
12576 case NODE_BREAK:
12577 case NODE_NEXT:
12578 case NODE_REDO:
12579 case NODE_RETRY:
12580 rb_warning0L(nd_line(tail), "statement not reached");
12581 break;
12582
12583 default:
12584 break;
12585 }
12586
12587 if (!nd_type_p(tail, NODE_BLOCK)) {
12588 tail = NEW_BLOCK(tail, &tail->nd_loc);
12589 }
12590 RNODE_BLOCK(end)->nd_next = tail;
12591 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12592 nd_set_last_loc(head, nd_last_loc(tail));
12593 return head;
12594}
12595
12596/* append item to the list */
12597static NODE*
12598list_append(struct parser_params *p, NODE *list, NODE *item)
12599{
12600 NODE *last;
12601
12602 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12603 if (RNODE_LIST(list)->nd_next) {
12604 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12605 }
12606 else {
12607 last = list;
12608 }
12609
12610 RNODE_LIST(list)->as.nd_alen += 1;
12611 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12612 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12613
12614 nd_set_last_loc(list, nd_last_loc(item));
12615
12616 return list;
12617}
12618
12619/* concat two lists */
12620static NODE*
12621list_concat(NODE *head, NODE *tail)
12622{
12623 NODE *last;
12624
12625 if (RNODE_LIST(head)->nd_next) {
12626 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12627 }
12628 else {
12629 last = head;
12630 }
12631
12632 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12633 RNODE_LIST(last)->nd_next = tail;
12634 if (RNODE_LIST(tail)->nd_next) {
12635 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12636 }
12637 else {
12638 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12639 }
12640
12641 nd_set_last_loc(head, nd_last_loc(tail));
12642
12643 return head;
12644}
12645
12646static int
12647literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12648{
12649 if (!tail) return 1;
12650 if (!rb_parser_enc_compatible(p, head, tail)) {
12651 compile_error(p, "string literal encodings differ (%s / %s)",
12652 rb_enc_name(rb_parser_str_get_encoding(head)),
12653 rb_enc_name(rb_parser_str_get_encoding(tail)));
12654 rb_parser_str_resize(p, head, 0);
12655 rb_parser_str_resize(p, tail, 0);
12656 return 0;
12657 }
12658 rb_parser_str_buf_append(p, head, tail);
12659 return 1;
12660}
12661
12662static rb_parser_string_t *
12663string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12664{
12665 if (htype != NODE_DSTR) return NULL;
12666 if (RNODE_DSTR(head)->nd_next) {
12667 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12668 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12669 }
12670 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12671 ASSUME(lit);
12672 return lit;
12673}
12674
12675#ifndef RIPPER
12676static rb_parser_string_t *
12677rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12678{
12679 rb_parser_string_t *copy;
12680 if (!orig) return NULL;
12681 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12682 copy->coderange = orig->coderange;
12683 copy->enc = orig->enc;
12684 return copy;
12685}
12686#endif
12687
12688/* concat two string literals */
12689static NODE *
12690literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12691{
12692 enum node_type htype;
12693 rb_parser_string_t *lit;
12694
12695 if (!head) return tail;
12696 if (!tail) return head;
12697
12698 htype = nd_type(head);
12699 if (htype == NODE_EVSTR) {
12700 head = new_dstr(p, head, loc);
12701 htype = NODE_DSTR;
12702 }
12703 if (p->heredoc_indent > 0) {
12704 switch (htype) {
12705 case NODE_STR:
12706 head = str2dstr(p, head);
12707 case NODE_DSTR:
12708 return list_append(p, head, tail);
12709 default:
12710 break;
12711 }
12712 }
12713 switch (nd_type(tail)) {
12714 case NODE_STR:
12715 if ((lit = string_literal_head(p, htype, head)) != false) {
12716 htype = NODE_STR;
12717 }
12718 else {
12719 lit = RNODE_DSTR(head)->string;
12720 }
12721 if (htype == NODE_STR) {
12722 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12723 error:
12724 rb_discard_node(p, head);
12725 rb_discard_node(p, tail);
12726 return 0;
12727 }
12728 rb_discard_node(p, tail);
12729 }
12730 else {
12731 list_append(p, head, tail);
12732 }
12733 break;
12734
12735 case NODE_DSTR:
12736 if (htype == NODE_STR) {
12737 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12738 goto error;
12739 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12740 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12741 RNODE_STR(head)->string = NULL;
12742 rb_discard_node(p, head);
12743 head = tail;
12744 }
12745 else if (!RNODE_DSTR(tail)->string) {
12746 append:
12747 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12748 if (!RNODE_DSTR(head)->nd_next) {
12749 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12750 }
12751 else if (RNODE_DSTR(tail)->nd_next) {
12752 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12753 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12754 }
12755 rb_discard_node(p, tail);
12756 }
12757 else if ((lit = string_literal_head(p, htype, head)) != false) {
12758 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12759 goto error;
12760 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12761 RNODE_DSTR(tail)->string = 0;
12762 goto append;
12763 }
12764 else {
12765 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));
12766 RNODE_DSTR(tail)->string = 0;
12767 }
12768 break;
12769
12770 case NODE_EVSTR:
12771 if (htype == NODE_STR) {
12772 head = str2dstr(p, head);
12773 RNODE_DSTR(head)->as.nd_alen = 1;
12774 }
12775 list_append(p, head, tail);
12776 break;
12777 }
12778 return head;
12779}
12780
12781static void
12782nd_copy_flag(NODE *new_node, NODE *old_node)
12783{
12784 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12785 nd_set_line(new_node, nd_line(old_node));
12786 new_node->nd_loc = old_node->nd_loc;
12787 new_node->node_id = old_node->node_id;
12788}
12789
12790static NODE *
12791str2dstr(struct parser_params *p, NODE *node)
12792{
12793 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12794 nd_copy_flag(new_node, node);
12795 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12796 RNODE_DSTR(new_node)->as.nd_alen = 0;
12797 RNODE_DSTR(new_node)->nd_next = 0;
12798 RNODE_STR(node)->string = 0;
12799
12800 return new_node;
12801}
12802
12803static NODE *
12804str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12805{
12806 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12807 nd_copy_flag(new_node, node);
12808 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12809 RNODE_REGX(new_node)->options = options;
12810 nd_set_loc(new_node, loc);
12811 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12812 RNODE_REGX(new_node)->content_loc = *content_loc;
12813 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12814 RNODE_STR(node)->string = 0;
12815
12816 return new_node;
12817}
12818
12819static NODE *
12820evstr2dstr(struct parser_params *p, NODE *node)
12821{
12822 if (nd_type_p(node, NODE_EVSTR)) {
12823 node = new_dstr(p, node, &node->nd_loc);
12824 }
12825 return node;
12826}
12827
12828static NODE *
12829new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12830{
12831 NODE *head = node;
12832
12833 if (node) {
12834 switch (nd_type(node)) {
12835 case NODE_STR:
12836 return str2dstr(p, node);
12837 case NODE_DSTR:
12838 break;
12839 case NODE_EVSTR:
12840 return node;
12841 }
12842 }
12843 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12844}
12845
12846static NODE *
12847new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12848{
12849 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12850 return list_append(p, dstr, node);
12851}
12852
12853static NODE *
12854call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12855 const YYLTYPE *op_loc, const YYLTYPE *loc)
12856{
12857 NODE *expr;
12858 value_expr(p, recv);
12859 value_expr(p, arg1);
12860 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12861 nd_set_line(expr, op_loc->beg_pos.lineno);
12862 return expr;
12863}
12864
12865static NODE *
12866call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12867{
12868 NODE *opcall;
12869 value_expr(p, recv);
12870 opcall = NEW_OPCALL(recv, id, 0, loc);
12871 nd_set_line(opcall, op_loc->beg_pos.lineno);
12872 return opcall;
12873}
12874
12875static NODE *
12876new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12877{
12878 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12879 nd_set_line(qcall, op_loc->beg_pos.lineno);
12880 return qcall;
12881}
12882
12883static NODE*
12884new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12885{
12886 NODE *ret;
12887 if (block) block_dup_check(p, args, block);
12888 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12889 if (block) ret = method_add_block(p, ret, block, loc);
12890 fixpos(ret, recv);
12891 return ret;
12892}
12893
12894static rb_locations_lambda_body_t*
12895new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12896{
12897 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12898 body->node = node;
12899 body->opening_loc = *opening_loc;
12900 body->closing_loc = *closing_loc;
12901 return body;
12902}
12903
12904static NODE *
12905command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc)
12906{
12907 NODE **body;
12908 enum node_type type = nd_type(m);
12909 switch (type) {
12910 case NODE_YIELD:
12911 compile_error(p, "block given to yield");
12912 return m;
12913 case NODE_RETURN:
12914 body = &RNODE_RETURN(m)->nd_stts;
12915 break;
12916 case NODE_BREAK:
12917 case NODE_NEXT:
12918 body = &RNODE_EXITS(m)->nd_stts;
12919 break;
12920 case NODE_REDO: /* `redo` has no argument */
12921 compile_error(p, "command_add_block: unexpected node: NODE_REDO");
12922 return m;
12923 case NODE_RETRY: /* `retry` has no argument */
12924 compile_error(p, "command_add_block: unexpected node: NODE_RETRY");
12925 return m;
12926 default:
12927 block_dup_check(p, get_nd_args(p, m), b);
12928 return method_add_block(p, m, b, loc);
12929 }
12930 RUBY_ASSERT(*body, "no argument %s with do_block", parser_node_name(type));
12931 YYLTYPE b_loc = code_loc_gen(&(*body)->nd_loc, loc);
12932 *body = command_add_block(p, *body, b, &b_loc);
12933 m->nd_loc.end_pos = loc->end_pos;
12934 return m;
12935}
12936
12937#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12938
12939static NODE*
12940last_expr_once_body(NODE *node)
12941{
12942 if (!node) return 0;
12943 return nd_once_body(node);
12944}
12945
12946static NODE*
12947match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12948{
12949 NODE *n;
12950 int line = op_loc->beg_pos.lineno;
12951
12952 value_expr(p, node1);
12953 value_expr(p, node2);
12954
12955 if ((n = last_expr_once_body(node1)) != 0) {
12956 switch (nd_type(n)) {
12957 case NODE_DREGX:
12958 {
12959 NODE *match = NEW_MATCH2(node1, node2, loc);
12960 nd_set_line(match, line);
12961 return match;
12962 }
12963
12964 case NODE_REGX:
12965 {
12966 const VALUE lit = rb_node_regx_string_val(n);
12967 if (!NIL_P(lit)) {
12968 NODE *match = NEW_MATCH2(node1, node2, loc);
12969 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12970 nd_set_line(match, line);
12971 return match;
12972 }
12973 }
12974 }
12975 }
12976
12977 if ((n = last_expr_once_body(node2)) != 0) {
12978 NODE *match3;
12979
12980 switch (nd_type(n)) {
12981 case NODE_DREGX:
12982 match3 = NEW_MATCH3(node2, node1, loc);
12983 return match3;
12984 }
12985 }
12986
12987 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12988 nd_set_line(n, line);
12989 return n;
12990}
12991
12992# if WARN_PAST_SCOPE
12993static int
12994past_dvar_p(struct parser_params *p, ID id)
12995{
12996 struct vtable *past = p->lvtbl->past;
12997 while (past) {
12998 if (vtable_included(past, id)) return 1;
12999 past = past->prev;
13000 }
13001 return 0;
13002}
13003# endif
13004
13005static int
13006numparam_nested_p(struct parser_params *p)
13007{
13008 struct local_vars *local = p->lvtbl;
13009 NODE *outer = local->numparam.outer;
13010 NODE *inner = local->numparam.inner;
13011 if (outer || inner) {
13012 NODE *used = outer ? outer : inner;
13013 compile_error(p, "numbered parameter is already used in %s block\n"
13014 "%s:%d: numbered parameter is already used here",
13015 outer ? "outer" : "inner",
13016 p->ruby_sourcefile, nd_line(used));
13017 parser_show_error_line(p, &used->nd_loc);
13018 return 1;
13019 }
13020 return 0;
13021}
13022
13023static int
13024numparam_used_p(struct parser_params *p)
13025{
13026 NODE *numparam = p->lvtbl->numparam.current;
13027 if (numparam) {
13028 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
13029 "%s:%d: numbered parameter is already used here",
13030 p->ruby_sourcefile, nd_line(numparam));
13031 parser_show_error_line(p, &numparam->nd_loc);
13032 return 1;
13033 }
13034 return 0;
13035}
13036
13037static int
13038it_used_p(struct parser_params *p)
13039{
13040 NODE *it = p->lvtbl->it;
13041 if (it) {
13042 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
13043 "%s:%d: 'it' is already used here",
13044 p->ruby_sourcefile, nd_line(it));
13045 parser_show_error_line(p, &it->nd_loc);
13046 return 1;
13047 }
13048 return 0;
13049}
13050
13051static NODE*
13052gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
13053{
13054 ID *vidp = NULL;
13055 NODE *node;
13056 switch (id) {
13057 case keyword_self:
13058 return NEW_SELF(loc);
13059 case keyword_nil:
13060 return NEW_NIL(loc);
13061 case keyword_true:
13062 return NEW_TRUE(loc);
13063 case keyword_false:
13064 return NEW_FALSE(loc);
13065 case keyword__FILE__:
13066 {
13067 VALUE file = p->ruby_sourcefile_string;
13068 if (NIL_P(file))
13069 file = rb_str_new(0, 0);
13070 node = NEW_FILE(file, loc);
13071 }
13072 return node;
13073 case keyword__LINE__:
13074 return NEW_LINE(loc);
13075 case keyword__ENCODING__:
13076 return NEW_ENCODING(loc);
13077
13078 }
13079 switch (id_type(id)) {
13080 case ID_LOCAL:
13081 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13082 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13083 if (vidp) *vidp |= LVAR_USED;
13084 node = NEW_DVAR(id, loc);
13085 return node;
13086 }
13087 if (local_id_ref(p, id, &vidp)) {
13088 if (vidp) *vidp |= LVAR_USED;
13089 node = NEW_LVAR(id, loc);
13090 return node;
13091 }
13092 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13093 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13094 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13095 node = NEW_DVAR(id, loc);
13096 struct local_vars *local = p->lvtbl;
13097 if (!local->numparam.current) local->numparam.current = node;
13098 return node;
13099 }
13100# if WARN_PAST_SCOPE
13101 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13102 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13103 }
13104# endif
13105 /* method call without arguments */
13106 if (dyna_in_block(p) && id == idIt && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13107 if (numparam_used_p(p)) return 0;
13108 if (p->max_numparam == ORDINAL_PARAM) {
13109 compile_error(p, "ordinary parameter is defined");
13110 return 0;
13111 }
13112 if (!p->it_id) {
13113 p->it_id = idItImplicit;
13114 vtable_add(p->lvtbl->args, p->it_id);
13115 }
13116 NODE *node = NEW_DVAR(p->it_id, loc);
13117 if (!p->lvtbl->it) p->lvtbl->it = node;
13118 return node;
13119 }
13120 return NEW_VCALL(id, loc);
13121 case ID_GLOBAL:
13122 return NEW_GVAR(id, loc);
13123 case ID_INSTANCE:
13124 return NEW_IVAR(id, loc);
13125 case ID_CONST:
13126 return NEW_CONST(id, loc);
13127 case ID_CLASS:
13128 return NEW_CVAR(id, loc);
13129 }
13130 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13131 return 0;
13132}
13133
13134static rb_node_opt_arg_t *
13135opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13136{
13137 rb_node_opt_arg_t *opts = opt_list;
13138 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13139
13140 while (opts->nd_next) {
13141 opts = opts->nd_next;
13142 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13143 }
13144 opts->nd_next = opt;
13145
13146 return opt_list;
13147}
13148
13149static rb_node_kw_arg_t *
13150kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13151{
13152 if (kwlist) {
13153 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13154 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13155 }
13156 return kwlist;
13157}
13158
13159static NODE *
13160new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
13161{
13162 int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13163 p->ctxt.has_trailing_semicolon = 0;
13164
13165 NODE *n = expr;
13166 while (n) {
13167 if (nd_type_p(n, NODE_BEGIN)) {
13168 n = RNODE_BEGIN(n)->nd_body;
13169 }
13170 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13171 n = RNODE_BLOCK(n)->nd_head;
13172 }
13173 else {
13174 break;
13175 }
13176 }
13177
13178 if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13179 NODE *block = NEW_BLOCK(expr, loc);
13180 return NEW_DEFINED(block, loc, keyword_loc);
13181 }
13182
13183 return NEW_DEFINED(n, loc, keyword_loc);
13184}
13185
13186static NODE*
13187str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13188{
13189 VALUE lit;
13190 rb_parser_string_t *str = RNODE_STR(node)->string;
13191 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13192 yyerror1(loc, "invalid symbol");
13193 lit = STR_NEW0();
13194 }
13195 else {
13196 lit = rb_str_new_parser_string(str);
13197 }
13198 return NEW_SYM(lit, loc);
13199}
13200
13201static NODE*
13202symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13203{
13204 enum node_type type = nd_type(symbol);
13205 switch (type) {
13206 case NODE_DSTR:
13207 nd_set_type(symbol, NODE_DSYM);
13208 break;
13209 case NODE_STR:
13210 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13211 break;
13212 default:
13213 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13214 }
13215 return list_append(p, symbols, symbol);
13216}
13217
13218static void
13219dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13220{
13221 if (dreg->string) {
13222 reg_fragment_setenc(p, dreg->string, options);
13223 }
13224 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13225 NODE *frag = list->nd_head;
13226 if (nd_type_p(frag, NODE_STR)) {
13227 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13228 }
13229 else if (nd_type_p(frag, NODE_DSTR)) {
13230 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13231 }
13232 }
13233}
13234
13235static NODE *
13236new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13237{
13238 if (!node) {
13239 /* Check string is valid regex */
13240 rb_parser_string_t *str = STRING_NEW0();
13241 reg_compile(p, str, options);
13242 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13243 return node;
13244 }
13245 switch (nd_type(node)) {
13246 case NODE_STR:
13247 {
13248 /* Check string is valid regex */
13249 reg_compile(p, RNODE_STR(node)->string, options);
13250 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13251 }
13252 break;
13253 default:
13254 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13255 /* fall through */
13256 case NODE_DSTR:
13257 nd_set_type(node, NODE_DREGX);
13258 nd_set_loc(node, loc);
13259 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13260 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13261 if (dreg->nd_next) {
13262 dregex_fragment_setenc(p, dreg, options);
13263 }
13264 if (options & RE_OPTION_ONCE) {
13265 node = NEW_ONCE(node, loc);
13266 }
13267 break;
13268 }
13269 return node;
13270}
13271
13272static rb_node_kw_arg_t *
13273new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13274{
13275 if (!k) return 0;
13276 return NEW_KW_ARG((k), loc);
13277}
13278
13279static NODE *
13280new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13281{
13282 if (!node) {
13283 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13284 return xstr;
13285 }
13286 switch (nd_type(node)) {
13287 case NODE_STR:
13288 nd_set_type(node, NODE_XSTR);
13289 nd_set_loc(node, loc);
13290 break;
13291 case NODE_DSTR:
13292 nd_set_type(node, NODE_DXSTR);
13293 nd_set_loc(node, loc);
13294 break;
13295 default:
13296 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13297 break;
13298 }
13299 return node;
13300}
13301
13302static const
13303struct st_hash_type literal_type = {
13304 literal_cmp,
13305 literal_hash,
13306};
13307
13308static int nd_type_st_key_enable_p(NODE *node);
13309
13310static void
13311check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13312{
13313 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13314 if (!arg || !p->case_labels) return;
13315 if (!nd_type_st_key_enable_p(arg)) return;
13316
13317 if (p->case_labels == CHECK_LITERAL_WHEN) {
13318 p->case_labels = st_init_table(&literal_type);
13319 }
13320 else {
13321 st_data_t line;
13322 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13323 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13324 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13325 return;
13326 }
13327 }
13328 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13329}
13330
13331#ifdef RIPPER
13332static int
13333id_is_var(struct parser_params *p, ID id)
13334{
13335 if (is_notop_id(id)) {
13336 switch (id & ID_SCOPE_MASK) {
13337 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13338 return 1;
13339 case ID_LOCAL:
13340 if (dyna_in_block(p)) {
13341 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13342 }
13343 if (local_id(p, id)) return 1;
13344 /* method call without arguments */
13345 return 0;
13346 }
13347 }
13348 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13349 return 0;
13350}
13351#endif
13352
13353static inline enum lex_state_e
13354parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13355{
13356 if (p->debug) {
13357 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13358 }
13359 return p->lex.state = ls;
13360}
13361
13362#ifndef RIPPER
13363static void
13364flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13365{
13366 VALUE mesg = p->debug_buffer;
13367
13368 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13369 p->debug_buffer = Qnil;
13370 rb_io_puts(1, &mesg, out);
13371 }
13372 if (!NIL_P(str) && RSTRING_LEN(str)) {
13373 rb_io_write(p->debug_output, str);
13374 }
13375}
13376
13377static const char rb_parser_lex_state_names[][8] = {
13378 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13379 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13380 "LABEL", "LABELED","FITEM",
13381};
13382
13383static VALUE
13384append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13385{
13386 int i, sep = 0;
13387 unsigned int mask = 1;
13388 static const char none[] = "NONE";
13389
13390 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13391 if ((unsigned)state & mask) {
13392 if (sep) {
13393 rb_str_cat(buf, "|", 1);
13394 }
13395 sep = 1;
13396 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13397 }
13398 }
13399 if (!sep) {
13400 rb_str_cat(buf, none, sizeof(none)-1);
13401 }
13402 return buf;
13403}
13404
13405enum lex_state_e
13406rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13407 enum lex_state_e to, int line)
13408{
13409 VALUE mesg;
13410 mesg = rb_str_new_cstr("lex_state: ");
13411 append_lex_state_name(p, from, mesg);
13412 rb_str_cat_cstr(mesg, " -> ");
13413 append_lex_state_name(p, to, mesg);
13414 rb_str_catf(mesg, " at line %d\n", line);
13415 flush_debug_buffer(p, p->debug_output, mesg);
13416 return to;
13417}
13418
13419VALUE
13420rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13421{
13422 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13423}
13424
13425static void
13426append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13427{
13428 if (stack == 0) {
13429 rb_str_cat_cstr(mesg, "0");
13430 }
13431 else {
13432 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13433 for (; mask && !(stack & mask); mask >>= 1) continue;
13434 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13435 }
13436}
13437
13438void
13439rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13440 const char *name, int line)
13441{
13442 VALUE mesg = rb_sprintf("%s: ", name);
13443 append_bitstack_value(p, stack, mesg);
13444 rb_str_catf(mesg, " at line %d\n", line);
13445 flush_debug_buffer(p, p->debug_output, mesg);
13446}
13447
13448void
13449rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13450{
13451 va_list ap;
13452 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13453
13454 va_start(ap, fmt);
13455 rb_str_vcatf(mesg, fmt, ap);
13456 va_end(ap);
13457 yyerror0(RSTRING_PTR(mesg));
13458 RB_GC_GUARD(mesg);
13459
13460 mesg = rb_str_new(0, 0);
13461 append_lex_state_name(p, p->lex.state, mesg);
13462 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13463 rb_str_resize(mesg, 0);
13464 append_bitstack_value(p, p->cond_stack, mesg);
13465 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13466 rb_str_resize(mesg, 0);
13467 append_bitstack_value(p, p->cmdarg_stack, mesg);
13468 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13469 if (p->debug_output == rb_ractor_stdout())
13470 p->debug_output = rb_ractor_stderr();
13471 p->debug = TRUE;
13472}
13473
13474static YYLTYPE *
13475rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13476{
13477 yylloc->beg_pos.lineno = sourceline;
13478 yylloc->beg_pos.column = beg_pos;
13479 yylloc->end_pos.lineno = sourceline;
13480 yylloc->end_pos.column = end_pos;
13481 return yylloc;
13482}
13483
13484YYLTYPE *
13485rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13486{
13487 int sourceline = here->sourceline;
13488 int beg_pos = parser_column(here->offset - here->quote
13489 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT)));
13490 int end_pos = parser_column(here->offset + here->length + here->quote);
13491
13492 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13493}
13494
13495YYLTYPE *
13496rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13497{
13498 yylloc->beg_pos.lineno = p->delayed.beg_line;
13499 yylloc->beg_pos.column = p->delayed.beg_col;
13500 yylloc->end_pos.lineno = p->delayed.end_line;
13501 yylloc->end_pos.column = p->delayed.end_col;
13502
13503 return yylloc;
13504}
13505
13506YYLTYPE *
13507rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13508{
13509 int sourceline = p->ruby_sourceline;
13510 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13511 int end_pos = parser_column(p->lex.pend - p->lex.pbeg);
13512 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13513}
13514
13515YYLTYPE *
13516rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13517{
13518 yylloc->end_pos = yylloc->beg_pos;
13519
13520 return yylloc;
13521}
13522
13523YYLTYPE *
13524rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13525{
13526 int sourceline = p->ruby_sourceline;
13527 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13528 int end_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13529 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13530}
13531
13532YYLTYPE *
13533rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13534{
13535 int sourceline = p->ruby_sourceline;
13536 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13537 int end_pos = parser_column(p->lex.pcur - p->lex.pbeg);
13538 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13539}
13540#endif /* !RIPPER */
13541
13542static int
13543assignable0(struct parser_params *p, ID id, const char **err)
13544{
13545 if (!id) return -1;
13546 switch (id) {
13547 case keyword_self:
13548 *err = "Can't change the value of self";
13549 return -1;
13550 case keyword_nil:
13551 *err = "Can't assign to nil";
13552 return -1;
13553 case keyword_true:
13554 *err = "Can't assign to true";
13555 return -1;
13556 case keyword_false:
13557 *err = "Can't assign to false";
13558 return -1;
13559 case keyword__FILE__:
13560 *err = "Can't assign to __FILE__";
13561 return -1;
13562 case keyword__LINE__:
13563 *err = "Can't assign to __LINE__";
13564 return -1;
13565 case keyword__ENCODING__:
13566 *err = "Can't assign to __ENCODING__";
13567 return -1;
13568 }
13569 switch (id_type(id)) {
13570 case ID_LOCAL:
13571 if (dyna_in_block(p)) {
13572 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13573 compile_error(p, "Can't assign to numbered parameter _%d",
13574 NUMPARAM_ID_TO_IDX(id));
13575 return -1;
13576 }
13577 if (dvar_curr(p, id)) return NODE_DASGN;
13578 if (dvar_defined(p, id)) return NODE_DASGN;
13579 if (local_id(p, id)) return NODE_LASGN;
13580 dyna_var(p, id);
13581 return NODE_DASGN;
13582 }
13583 else {
13584 if (!local_id(p, id)) local_var(p, id);
13585 return NODE_LASGN;
13586 }
13587 break;
13588 case ID_GLOBAL: return NODE_GASGN;
13589 case ID_INSTANCE: return NODE_IASGN;
13590 case ID_CONST:
13591 if (!p->ctxt.in_def) return NODE_CDECL;
13592 *err = "dynamic constant assignment";
13593 return -1;
13594 case ID_CLASS: return NODE_CVASGN;
13595 default:
13596 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13597 }
13598 return -1;
13599}
13600
13601static NODE*
13602assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13603{
13604 const char *err = 0;
13605 int node_type = assignable0(p, id, &err);
13606 switch (node_type) {
13607 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13608 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13609 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13610 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13611 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13612 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13613 }
13614/* TODO: FIXME */
13615#ifndef RIPPER
13616 if (err) yyerror1(loc, err);
13617#else
13618 if (err) set_value(assign_error(p, err, p->s_lvalue));
13619#endif
13620 return NEW_ERROR(loc);
13621}
13622
13623static int
13624is_private_local_id(struct parser_params *p, ID name)
13625{
13626 VALUE s;
13627 if (name == idUScore) return 1;
13628 if (!is_local_id(name)) return 0;
13629 s = rb_id2str(name);
13630 if (!s) return 0;
13631 return RSTRING_PTR(s)[0] == '_';
13632}
13633
13634static int
13635shadowing_lvar_0(struct parser_params *p, ID name)
13636{
13637 if (dyna_in_block(p)) {
13638 if (dvar_curr(p, name)) {
13639 if (is_private_local_id(p, name)) return 1;
13640 yyerror0("duplicated argument name");
13641 }
13642 else if (dvar_defined(p, name) || local_id(p, name)) {
13643 vtable_add(p->lvtbl->vars, name);
13644 if (p->lvtbl->used) {
13645 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13646 }
13647 return 0;
13648 }
13649 }
13650 else {
13651 if (local_id(p, name)) {
13652 if (is_private_local_id(p, name)) return 1;
13653 yyerror0("duplicated argument name");
13654 }
13655 }
13656 return 1;
13657}
13658
13659static ID
13660shadowing_lvar(struct parser_params *p, ID name)
13661{
13662 shadowing_lvar_0(p, name);
13663 return name;
13664}
13665
13666static void
13667new_bv(struct parser_params *p, ID name)
13668{
13669 if (!name) return;
13670 if (!is_local_id(name)) {
13671 compile_error(p, "invalid local variable - %"PRIsVALUE,
13672 rb_id2str(name));
13673 return;
13674 }
13675 if (!shadowing_lvar_0(p, name)) return;
13676 dyna_var(p, name);
13677 ID *vidp = 0;
13678 if (dvar_defined_ref(p, name, &vidp)) {
13679 if (vidp) *vidp |= LVAR_USED;
13680 }
13681}
13682
13683static void
13684aryset_check(struct parser_params *p, NODE *args)
13685{
13686 NODE *block = 0, *kwds = 0;
13687 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13688 block = RNODE_BLOCK_PASS(args)->nd_body;
13689 args = RNODE_BLOCK_PASS(args)->nd_head;
13690 }
13691 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13692 args = RNODE_ARGSCAT(args)->nd_body;
13693 }
13694 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13695 kwds = RNODE_ARGSPUSH(args)->nd_body;
13696 }
13697 else {
13698 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13699 next = RNODE_LIST(next)->nd_next) {
13700 kwds = RNODE_LIST(next)->nd_head;
13701 }
13702 }
13703 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13704 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13705 }
13706 if (block) {
13707 yyerror1(&block->nd_loc, "block arg given in index assignment");
13708 }
13709}
13710
13711static NODE *
13712aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13713{
13714 aryset_check(p, idx);
13715 return NEW_ATTRASGN(recv, tASET, idx, loc);
13716}
13717
13718static void
13719block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13720{
13721 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13722 compile_error(p, "both block arg and actual block given");
13723 }
13724}
13725
13726static NODE *
13727attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13728{
13729 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13730 return NEW_ATTRASGN(recv, id, 0, loc);
13731}
13732
13733static VALUE
13734rb_backref_error(struct parser_params *p, NODE *node)
13735{
13736#ifndef RIPPER
13737# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13738#else
13739# define ERR(...) rb_sprintf(__VA_ARGS__)
13740#endif
13741 switch (nd_type(node)) {
13742 case NODE_NTH_REF:
13743 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13744 case NODE_BACK_REF:
13745 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13746 }
13747#undef ERR
13748 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13749}
13750
13751static NODE *
13752arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13753{
13754 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13755 switch (nd_type(node1)) {
13756 case NODE_LIST:
13757 return list_append(p, node1, node2);
13758 case NODE_BLOCK_PASS:
13759 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13760 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13761 return node1;
13762 case NODE_ARGSPUSH:
13763 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13764 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13765 nd_set_type(node1, NODE_ARGSCAT);
13766 return node1;
13767 case NODE_ARGSCAT:
13768 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13769 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13770 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13771 return node1;
13772 }
13773 return NEW_ARGSPUSH(node1, node2, loc);
13774}
13775
13776static NODE *
13777arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13778{
13779 if (!node2) return node1;
13780 switch (nd_type(node1)) {
13781 case NODE_BLOCK_PASS:
13782 if (RNODE_BLOCK_PASS(node1)->nd_head)
13783 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13784 else
13785 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13786 return node1;
13787 case NODE_ARGSPUSH:
13788 if (!nd_type_p(node2, NODE_LIST)) break;
13789 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13790 nd_set_type(node1, NODE_ARGSCAT);
13791 return node1;
13792 case NODE_ARGSCAT:
13793 if (!nd_type_p(node2, NODE_LIST) ||
13794 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13795 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13796 return node1;
13797 }
13798 return NEW_ARGSCAT(node1, node2, loc);
13799}
13800
13801static NODE *
13802last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13803{
13804 NODE *n1;
13805 if ((n1 = splat_array(args)) != 0) {
13806 return list_append(p, n1, last_arg);
13807 }
13808 return arg_append(p, args, last_arg, loc);
13809}
13810
13811static NODE *
13812rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13813{
13814 NODE *n1;
13815 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13816 return list_concat(n1, rest_arg);
13817 }
13818 return arg_concat(p, args, rest_arg, loc);
13819}
13820
13821static NODE *
13822splat_array(NODE* node)
13823{
13824 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13825 if (nd_type_p(node, NODE_LIST)) return node;
13826 return 0;
13827}
13828
13829static void
13830mark_lvar_used(struct parser_params *p, NODE *rhs)
13831{
13832 ID *vidp = NULL;
13833 if (!rhs) return;
13834 switch (nd_type(rhs)) {
13835 case NODE_LASGN:
13836 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13837 if (vidp) *vidp |= LVAR_USED;
13838 }
13839 break;
13840 case NODE_DASGN:
13841 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13842 if (vidp) *vidp |= LVAR_USED;
13843 }
13844 break;
13845#if 0
13846 case NODE_MASGN:
13847 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13848 mark_lvar_used(p, rhs->nd_head);
13849 }
13850 break;
13851#endif
13852 }
13853}
13854
13855static int is_static_content(NODE *node);
13856
13857static NODE *
13858node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13859{
13860 if (!lhs) return 0;
13861
13862 switch (nd_type(lhs)) {
13863 case NODE_CDECL:
13864 case NODE_GASGN:
13865 case NODE_IASGN:
13866 case NODE_LASGN:
13867 case NODE_DASGN:
13868 case NODE_MASGN:
13869 case NODE_CVASGN:
13870 set_nd_value(p, lhs, rhs);
13871 nd_set_loc(lhs, loc);
13872 break;
13873
13874 case NODE_ATTRASGN:
13875 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13876 nd_set_loc(lhs, loc);
13877 break;
13878
13879 default:
13880 /* should not happen */
13881 break;
13882 }
13883
13884 return lhs;
13885}
13886
13887static NODE *
13888value_expr_check(struct parser_params *p, NODE *node)
13889{
13890 NODE *void_node = 0, *vn;
13891
13892 if (!node) {
13893 rb_warning0("empty expression");
13894 }
13895 while (node) {
13896 switch (nd_type(node)) {
13897 case NODE_ENSURE:
13898 vn = RNODE_ENSURE(node)->nd_head;
13899 node = RNODE_ENSURE(node)->nd_ensr;
13900 /* nd_ensr should not be NULL, check it out next */
13901 if (vn && (vn = value_expr_check(p, vn))) {
13902 goto found;
13903 }
13904 break;
13905
13906 case NODE_RESCUE:
13907 /* void only if all children are void */
13908 vn = RNODE_RESCUE(node)->nd_head;
13909 if (!vn || !(vn = value_expr_check(p, vn))) {
13910 if (!RNODE_RESCUE(node)->nd_else) return NULL;
13911 }
13912 if (!void_node) void_node = vn;
13913 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13914 if (!nd_type_p(r, NODE_RESBODY)) {
13915 compile_error(p, "unexpected node");
13916 return NULL;
13917 }
13918 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13919 return NULL;
13920 }
13921 if (!void_node) void_node = vn;
13922 }
13923 node = RNODE_RESCUE(node)->nd_else;
13924 if (!node) return void_node;
13925 break;
13926
13927 case NODE_RETURN:
13928 case NODE_BREAK:
13929 case NODE_NEXT:
13930 case NODE_REDO:
13931 case NODE_RETRY:
13932 goto found;
13933
13934 case NODE_CASE:
13935 case NODE_CASE2:
13936 for (node = RNODE_CASE(node)->nd_body;
13937 node && nd_type_p(node, NODE_WHEN);
13938 node = RNODE_WHEN(node)->nd_next) {
13939 if (!(vn = value_expr_check(p, RNODE_WHEN(node)->nd_body))) {
13940 return NULL;
13941 }
13942 if (!void_node) void_node = vn;
13943 }
13944 break;
13945
13946 case NODE_CASE3:
13947 {
13948 NODE *in = RNODE_CASE3(node)->nd_body;
13949 if (!in || !nd_type_p(in, NODE_IN)) {
13950 compile_error(p, "unexpected node");
13951 return NULL;
13952 }
13953 if (!RNODE_IN(in)->nd_body) {
13954 /* single line pattern matching with "=>" operator */
13955 goto found;
13956 }
13957 do {
13958 vn = value_expr_check(p, RNODE_IN(in)->nd_body);
13959 if (!vn) return NULL;
13960 if (!void_node) void_node = vn;
13961 in = RNODE_IN(in)->nd_next;
13962 } while (in && nd_type_p(in, NODE_IN));
13963 node = in; /* else */
13964 }
13965 break;
13966
13967 case NODE_BLOCK:
13968 while (RNODE_BLOCK(node)->nd_next) {
13969 vn = value_expr_check(p, RNODE_BLOCK(node)->nd_head);
13970 if (vn) return vn;
13971 node = RNODE_BLOCK(node)->nd_next;
13972 }
13973 node = RNODE_BLOCK(node)->nd_head;
13974 break;
13975
13976 case NODE_BEGIN:
13977 node = RNODE_BEGIN(node)->nd_body;
13978 break;
13979
13980 case NODE_IF:
13981 case NODE_UNLESS:
13982 if (!RNODE_IF(node)->nd_body) {
13983 return NULL;
13984 }
13985 else if (!RNODE_IF(node)->nd_else) {
13986 return NULL;
13987 }
13988 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13989 if (!vn) return NULL;
13990 if (!void_node) void_node = vn;
13991 node = RNODE_IF(node)->nd_else;
13992 break;
13993
13994 case NODE_AND:
13995 case NODE_OR:
13996 /* The left operand was already checked for a value when logop()
13997 * built this node, so stop here instead of re-reporting the same
13998 * void value once per operator in a chain. */
13999 return NULL;
14000
14001 case NODE_LASGN:
14002 case NODE_DASGN:
14003 case NODE_MASGN:
14004 mark_lvar_used(p, node);
14005 return NULL;
14006
14007 default:
14008 return NULL;
14009 }
14010 }
14011
14012 return NULL;
14013
14014 found:
14015 /* return the first found node */
14016 return void_node ? void_node : node;
14017}
14018
14019static int
14020value_expr(struct parser_params *p, NODE *node)
14021{
14022 NODE *void_node = value_expr_check(p, node);
14023 if (void_node) {
14024 yyerror1(&void_node->nd_loc, "void value expression");
14025 /* or "control never reach"? */
14026 return FALSE;
14027 }
14028 return TRUE;
14029}
14030
14031static void
14032void_expr(struct parser_params *p, NODE *node)
14033{
14034 const char *useless = 0;
14035
14036 if (!RTEST(ruby_verbose)) return;
14037
14038 if (!node || !(node = nd_once_body(node))) return;
14039 switch (nd_type(node)) {
14040 case NODE_OPCALL:
14041 switch (RNODE_OPCALL(node)->nd_mid) {
14042 case '+':
14043 case '-':
14044 case '*':
14045 case '/':
14046 case '%':
14047 case tPOW:
14048 case tUPLUS:
14049 case tUMINUS:
14050 case '|':
14051 case '^':
14052 case '&':
14053 case tCMP:
14054 case '>':
14055 case tGEQ:
14056 case '<':
14057 case tLEQ:
14058 case tEQ:
14059 case tNEQ:
14060 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
14061 break;
14062 }
14063 break;
14064
14065 case NODE_LVAR:
14066 case NODE_DVAR:
14067 case NODE_GVAR:
14068 case NODE_IVAR:
14069 case NODE_CVAR:
14070 case NODE_NTH_REF:
14071 case NODE_BACK_REF:
14072 useless = "a variable";
14073 break;
14074 case NODE_CONST:
14075 useless = "a constant";
14076 break;
14077 case NODE_SYM:
14078 case NODE_LINE:
14079 case NODE_FILE:
14080 case NODE_ENCODING:
14081 case NODE_INTEGER:
14082 case NODE_FLOAT:
14083 case NODE_RATIONAL:
14084 case NODE_IMAGINARY:
14085 case NODE_STR:
14086 case NODE_DSTR:
14087 case NODE_REGX:
14088 case NODE_DREGX:
14089 useless = "a literal";
14090 break;
14091 case NODE_COLON2:
14092 case NODE_COLON3:
14093 useless = "::";
14094 break;
14095 case NODE_DOT2:
14096 useless = "..";
14097 break;
14098 case NODE_DOT3:
14099 useless = "...";
14100 break;
14101 case NODE_SELF:
14102 useless = "self";
14103 break;
14104 case NODE_NIL:
14105 useless = "nil";
14106 break;
14107 case NODE_TRUE:
14108 useless = "true";
14109 break;
14110 case NODE_FALSE:
14111 useless = "false";
14112 break;
14113 case NODE_DEFINED:
14114 useless = "defined?";
14115 break;
14116 }
14117
14118 if (useless) {
14119 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14120 }
14121}
14122
14123/* warns useless use of block and returns the last statement node */
14124static NODE *
14125void_stmts(struct parser_params *p, NODE *node)
14126{
14127 NODE *const n = node;
14128 if (!RTEST(ruby_verbose)) return n;
14129 if (!node) return n;
14130 if (!nd_type_p(node, NODE_BLOCK)) return n;
14131
14132 while (RNODE_BLOCK(node)->nd_next) {
14133 void_expr(p, RNODE_BLOCK(node)->nd_head);
14134 node = RNODE_BLOCK(node)->nd_next;
14135 }
14136 return RNODE_BLOCK(node)->nd_head;
14137}
14138
14139static NODE *
14140remove_begin(NODE *node)
14141{
14142 NODE **n = &node, *n1 = node;
14143 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14144 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14145 }
14146 return node;
14147}
14148
14149static void
14150reduce_nodes(struct parser_params *p, NODE **body)
14151{
14152 NODE *node = *body;
14153
14154 if (!node) {
14155 *body = NEW_NIL(&NULL_LOC);
14156 return;
14157 }
14158#define subnodes(type, n1, n2) \
14159 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14160 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14161 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14162
14163 while (node) {
14164 int newline = (int)nd_fl_newline(node);
14165 switch (nd_type(node)) {
14166 case NODE_NIL:
14167 // Keep an explicit nil in a method's tail (value) position when it
14168 // is on its own line, so it still emits a :line event and records
14169 // line coverage for that line. [Bug #22302]
14170 if (newline) return;
14171 end:
14172 *body = 0;
14173 return;
14174 case NODE_BEGIN:
14175 *body = node = RNODE_BEGIN(node)->nd_body;
14176 if (newline && node) nd_set_fl_newline(node);
14177 continue;
14178 case NODE_BLOCK:
14179 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14180 break;
14181 case NODE_IF:
14182 case NODE_UNLESS:
14183 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14184 return;
14185 case NODE_CASE:
14186 body = &RNODE_CASE(node)->nd_body;
14187 break;
14188 case NODE_WHEN:
14189 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14190 break;
14191 case NODE_ENSURE:
14192 body = &RNODE_ENSURE(node)->nd_head;
14193 break;
14194 case NODE_RESCUE:
14195 newline = 0; // RESBODY should not be a NEWLINE
14196 if (RNODE_RESCUE(node)->nd_else) {
14197 body = &RNODE_RESCUE(node)->nd_resq;
14198 break;
14199 }
14200 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14201 break;
14202 default:
14203 return;
14204 }
14205 node = *body;
14206 if (newline && node) nd_set_fl_newline(node);
14207 }
14208
14209#undef subnodes
14210}
14211
14212static int
14213is_static_content(NODE *node)
14214{
14215 if (!node) return 1;
14216 switch (nd_type(node)) {
14217 case NODE_HASH:
14218 if (!(node = RNODE_HASH(node)->nd_head)) break;
14219 case NODE_LIST:
14220 do {
14221 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14222 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14223 case NODE_SYM:
14224 case NODE_REGX:
14225 case NODE_LINE:
14226 case NODE_FILE:
14227 case NODE_ENCODING:
14228 case NODE_INTEGER:
14229 case NODE_FLOAT:
14230 case NODE_RATIONAL:
14231 case NODE_IMAGINARY:
14232 case NODE_STR:
14233 case NODE_NIL:
14234 case NODE_TRUE:
14235 case NODE_FALSE:
14236 case NODE_ZLIST:
14237 break;
14238 default:
14239 return 0;
14240 }
14241 return 1;
14242}
14243
14244static int
14245assign_in_cond(struct parser_params *p, NODE *node)
14246{
14247 switch (nd_type(node)) {
14248 case NODE_MASGN:
14249 case NODE_LASGN:
14250 case NODE_DASGN:
14251 case NODE_GASGN:
14252 case NODE_IASGN:
14253 case NODE_CVASGN:
14254 case NODE_CDECL:
14255 break;
14256
14257 default:
14258 return 0;
14259 }
14260
14261 if (!get_nd_value(p, node)) return 1;
14262 if (is_static_content(get_nd_value(p, node))) {
14263 /* reports always */
14264 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14265 }
14266 return 1;
14267}
14268
14269enum cond_type {
14270 COND_IN_OP,
14271 COND_IN_COND,
14272 COND_IN_FF
14273};
14274
14275#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14276 switch (t) { \
14277 case COND_IN_OP: break; \
14278 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14279 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14280 } \
14281} while (0)
14282
14283static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14284
14285static NODE*
14286range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14287{
14288 enum node_type type;
14289
14290 if (node == 0) return 0;
14291
14292 type = nd_type(node);
14293 value_expr(p, node);
14294 if (type == NODE_INTEGER) {
14295 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14296 ID lineno = rb_intern("$.");
14297 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14298 }
14299 return cond0(p, node, COND_IN_FF, loc, true);
14300}
14301
14302static NODE*
14303cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14304{
14305 if (node == 0) return 0;
14306 NODE *original = node;
14307 if (!(node = nd_once_body(node))) return 0;
14308 assign_in_cond(p, node);
14309
14310 switch (nd_type(node)) {
14311 case NODE_BEGIN:
14312 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14313 break;
14314
14315 case NODE_DSTR:
14316 case NODE_EVSTR:
14317 case NODE_STR:
14318 case NODE_FILE:
14319 SWITCH_BY_COND_TYPE(type, warn, "string ");
14320 break;
14321
14322 case NODE_REGX:
14323 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14324 nd_set_type(node, NODE_MATCH);
14325 break;
14326
14327 case NODE_DREGX:
14328 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14329
14330 /* Preserve the NODE_ONCE wrapper around interpolated /o regexps. */
14331 return NEW_MATCH2(original, NEW_GVAR(idLASTLINE, loc), loc);
14332
14333 case NODE_BLOCK:
14334 {
14335 NODE *end = RNODE_BLOCK(node)->nd_end;
14336 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14337 if (top) top = node == end;
14338 *expr = cond0(p, *expr, type, loc, top);
14339 }
14340 break;
14341
14342 case NODE_AND:
14343 case NODE_OR:
14344 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14345 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14346 break;
14347
14348 case NODE_DOT2:
14349 case NODE_DOT3:
14350 if (!top) break;
14351 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14352 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14353 switch (nd_type(node)) {
14354 case NODE_DOT2:
14355 nd_set_type(node,NODE_FLIP2);
14356 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14357 (void)flip2;
14358 break;
14359 case NODE_DOT3:
14360 nd_set_type(node, NODE_FLIP3);
14361 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14362 (void)flip3;
14363 break;
14364 }
14365 break;
14366
14367 case NODE_SYM:
14368 case NODE_DSYM:
14369 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14370 break;
14371
14372 case NODE_LINE:
14373 case NODE_ENCODING:
14374 case NODE_INTEGER:
14375 case NODE_FLOAT:
14376 case NODE_RATIONAL:
14377 case NODE_IMAGINARY:
14378 SWITCH_BY_COND_TYPE(type, warning, "");
14379 break;
14380
14381 default:
14382 break;
14383 }
14384 return node;
14385}
14386
14387static NODE*
14388cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14389{
14390 if (node == 0) return 0;
14391 return cond0(p, node, COND_IN_COND, loc, true);
14392}
14393
14394static NODE*
14395method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14396{
14397 if (node == 0) return 0;
14398 return cond0(p, node, COND_IN_OP, loc, true);
14399}
14400
14401static NODE*
14402new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14403{
14404 YYLTYPE loc = {*pos, *pos};
14405 return NEW_NIL(&loc);
14406}
14407
14408static NODE*
14409new_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)
14410{
14411 if (!cc) return right;
14412 cc = cond0(p, cc, COND_IN_COND, loc, true);
14413 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14414}
14415
14416static NODE*
14417new_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)
14418{
14419 if (!cc) return right;
14420 cc = cond0(p, cc, COND_IN_COND, loc, true);
14421 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14422}
14423
14424#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))
14425
14426/* A cached logop tail is usable only if it is still a node of the chain's type
14427 * whose nd_2nd is the insertion point, i.e. not itself another node of that
14428 * type. This rejects a stale cache left over from an earlier parse. */
14429static int
14430logop_valid_tail(NODE *tail, enum node_type type)
14431{
14432 NODE *second;
14433 return tail && nd_type_p(tail, type) &&
14434 ((second = RNODE_AND(tail)->nd_2nd) == 0 || !nd_type_p(second, type));
14435}
14436
14437static NODE*
14438logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14439 const YYLTYPE *op_loc, const YYLTYPE *loc)
14440{
14441 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14442 NODE *op;
14443 /* Snapshot the cache from the previous logop() call before overwriting it. */
14444 NODE *prev_head = p->logop.head;
14445 NODE *prev_tail = p->logop.tail;
14446 NODE *node, *second;
14447 value_expr(p, left);
14448 if (left && nd_type_p(left, type)) {
14449 /* The insertion point is the far end of left's right branch.
14450 * Reusing the cached tail keeps a chain such as `a && a && ... && a`
14451 * linear instead of rescanning the whole branch on every operator. The
14452 * cache is validated (its nd_2nd is not another node of the same type)
14453 * so a stale entry falls back to the scan. */
14454 if (left == prev_head && logop_valid_tail(prev_tail, type)) {
14455 node = prev_tail;
14456 }
14457 else {
14458 node = left;
14459 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14460 node = second;
14461 }
14462 }
14463 second = RNODE_AND(node)->nd_2nd;
14464 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14465 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14466 left->nd_loc.end_pos = loc->end_pos;
14467 p->logop.head = left;
14468 p->logop.tail = RNODE_AND(node)->nd_2nd;
14469 return left;
14470 }
14471 op = NEW_AND_OR(type, left, right, loc, op_loc);
14472 nd_set_line(op, op_loc->beg_pos.lineno);
14473 /* Record where the next operator will extend op, mirroring the scan the
14474 * chained branch above would perform: if right is itself a chain of the
14475 * same type (from parentheses), its far end; otherwise op itself. */
14476 p->logop.head = op;
14477 if (right == prev_head && logop_valid_tail(prev_tail, type)) {
14478 p->logop.tail = prev_tail;
14479 }
14480 else if (right && nd_type_p(right, type)) {
14481 node = right;
14482 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14483 node = second;
14484 }
14485 p->logop.tail = node;
14486 }
14487 else {
14488 p->logop.tail = op;
14489 }
14490 return op;
14491}
14492
14493#undef NEW_AND_OR
14494
14495static void
14496no_blockarg(struct parser_params *p, NODE *node)
14497{
14498 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14499 compile_error(p, "block argument should not be given");
14500 }
14501}
14502
14503static NODE *
14504ret_args(struct parser_params *p, NODE *node)
14505{
14506 if (node) {
14507 no_blockarg(p, node);
14508 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14509 node = RNODE_LIST(node)->nd_head;
14510 }
14511 }
14512 return node;
14513}
14514
14515static NODE*
14516negate_lit(struct parser_params *p, NODE* node, const YYLTYPE *loc)
14517{
14518 switch (nd_type(node)) {
14519 case NODE_INTEGER:
14520 RNODE_INTEGER(node)->minus = TRUE;
14521 break;
14522 case NODE_FLOAT:
14523 RNODE_FLOAT(node)->minus = TRUE;
14524 break;
14525 case NODE_RATIONAL:
14526 RNODE_RATIONAL(node)->minus = TRUE;
14527 break;
14528 case NODE_IMAGINARY:
14529 RNODE_IMAGINARY(node)->minus = TRUE;
14530 break;
14531 }
14532 node->nd_loc = *loc;
14533 return node;
14534}
14535
14536static NODE *
14537arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14538{
14539 if (node2) {
14540 if (!node1) return (NODE *)node2;
14541 node2->nd_head = node1;
14542 nd_set_first_lineno(node2, nd_first_lineno(node1));
14543 nd_set_first_column(node2, nd_first_column(node1));
14544 return (NODE *)node2;
14545 }
14546 return node1;
14547}
14548
14549static bool
14550args_info_empty_p(struct rb_args_info *args)
14551{
14552 if (args->pre_args_num) return false;
14553 if (args->post_args_num) return false;
14554 if (args->rest_arg) return false;
14555 if (args->opt_args) return false;
14556 if (args->block_arg) return false;
14557 if (args->kw_args) return false;
14558 if (args->kw_rest_arg) return false;
14559 return true;
14560}
14561
14562static rb_node_args_t *
14563new_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)
14564{
14565 struct rb_args_info *args = &tail->nd_ainfo;
14566
14567 if (args->forwarding) {
14568 if (rest_arg) {
14569 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14570 return tail;
14571 }
14572 rest_arg = idFWD_REST;
14573 }
14574
14575 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14576 args->pre_init = pre_args ? pre_args->nd_next : 0;
14577
14578 args->post_args_num = post_args ? post_args->nd_plen : 0;
14579 args->post_init = post_args ? post_args->nd_next : 0;
14580 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14581
14582 args->rest_arg = rest_arg;
14583
14584 args->opt_args = opt_args;
14585
14586 nd_set_loc(RNODE(tail), loc);
14587
14588 return tail;
14589}
14590
14591static rb_node_args_t *
14592new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14593{
14594 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14595 struct rb_args_info *args = &node->nd_ainfo;
14596 if (p->error_p) return node;
14597
14598 if (block == idNil) {
14599 block = 0;
14600 args->no_blockarg = TRUE;
14601 }
14602 args->block_arg = block;
14603 args->kw_args = kw_args;
14604
14605 if (kw_args) {
14606 /*
14607 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14608 * variable order: k1, kr1, k2, &b, internal_id, krest
14609 * #=> <reorder>
14610 * variable order: kr1, k1, k2, internal_id, krest, &b
14611 */
14612 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14613 struct vtable *vtargs = p->lvtbl->args;
14614 rb_node_kw_arg_t *kwn = kw_args;
14615
14616 if (block) block = vtargs->tbl[vtargs->pos-1];
14617 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14618 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14619 while (kwn) {
14620 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14621 --kw_vars;
14622 --required_kw_vars;
14623 kwn = kwn->nd_next;
14624 }
14625
14626 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14627 ID vid = get_nd_vid(p, kwn->nd_body);
14628 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14629 *required_kw_vars++ = vid;
14630 }
14631 else {
14632 *kw_vars++ = vid;
14633 }
14634 }
14635
14636 arg_var(p, kw_bits);
14637 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14638 if (block) arg_var(p, block);
14639
14640 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14641 }
14642 else if (kw_rest_arg == idNil) {
14643 args->no_kwarg = 1;
14644 }
14645 else if (kw_rest_arg) {
14646 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14647 }
14648
14649 return node;
14650}
14651
14652static rb_node_args_t *
14653args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14654{
14655 if (max_numparam > NO_PARAM || it_id) {
14656 if (!args) {
14657 YYLTYPE loc = RUBY_INIT_YYLLOC();
14658 args = new_empty_args_tail(p, 0);
14659 nd_set_loc(RNODE(args), &loc);
14660 }
14661 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14662 }
14663 return args;
14664}
14665
14666static NODE*
14667new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14668{
14669 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14670
14671 if (pre_arg) {
14672 NODE *pre_args = NEW_LIST(pre_arg, loc);
14673 if (RNODE_ARYPTN(aryptn)->pre_args) {
14674 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14675 }
14676 else {
14677 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14678 }
14679 }
14680 return aryptn;
14681}
14682
14683static NODE*
14684new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14685{
14686 if (has_rest) {
14687 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14688 }
14689 else {
14690 rest_arg = NULL;
14691 }
14692 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14693
14694 return node;
14695}
14696
14697static NODE*
14698new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14699{
14700 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14701
14702 return fndptn;
14703}
14704
14705static NODE*
14706new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14707{
14708 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14709 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14710 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14711
14712 return node;
14713}
14714
14715static NODE*
14716new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14717{
14718 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14719 return hshptn;
14720}
14721
14722static NODE*
14723new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14724{
14725 NODE *node, *kw_rest_arg_node;
14726
14727 if (kw_rest_arg == idNil) {
14728 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14729 }
14730 else if (kw_rest_arg) {
14731 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14732 }
14733 else {
14734 kw_rest_arg_node = NULL;
14735 }
14736
14737 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14738
14739 return node;
14740}
14741
14742static NODE*
14743dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14744{
14745 if (!node) {
14746 return NEW_SYM(STR_NEW0(), loc);
14747 }
14748
14749 switch (nd_type(node)) {
14750 case NODE_DSTR:
14751 nd_set_type(node, NODE_DSYM);
14752 nd_set_loc(node, loc);
14753 break;
14754 case NODE_STR:
14755 node = str_to_sym_node(p, node, loc);
14756 break;
14757 default:
14758 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14759 break;
14760 }
14761 return node;
14762}
14763
14764static int
14765nd_type_st_key_enable_p(NODE *node)
14766{
14767 switch (nd_type(node)) {
14768 case NODE_INTEGER:
14769 case NODE_FLOAT:
14770 case NODE_RATIONAL:
14771 case NODE_IMAGINARY:
14772 case NODE_STR:
14773 case NODE_SYM:
14774 case NODE_REGX:
14775 case NODE_LINE:
14776 case NODE_FILE:
14777 case NODE_ENCODING:
14778 return true;
14779 default:
14780 return false;
14781 }
14782}
14783
14784static VALUE
14785nd_value(struct parser_params *p, NODE *node)
14786{
14787 switch (nd_type(node)) {
14788 case NODE_STR:
14789 return rb_node_str_string_val(node);
14790 case NODE_INTEGER:
14791 return rb_node_integer_literal_val(node);
14792 case NODE_FLOAT:
14793 return rb_node_float_literal_val(node);
14794 case NODE_RATIONAL:
14795 return rb_node_rational_literal_val(node);
14796 case NODE_IMAGINARY:
14797 return rb_node_imaginary_literal_val(node);
14798 case NODE_SYM:
14799 return rb_node_sym_string_val(node);
14800 case NODE_REGX:
14801 return rb_node_regx_string_val(node);
14802 case NODE_LINE:
14803 return rb_node_line_lineno_val(node);
14804 case NODE_ENCODING:
14805 return rb_node_encoding_val(node);
14806 case NODE_FILE:
14807 return rb_node_file_path_val(node);
14808 default:
14809 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14810 UNREACHABLE_RETURN(0);
14811 }
14812}
14813
14814static void
14815warn_duplicate_keys(struct parser_params *p, NODE *hash)
14816{
14817 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14818 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14819 while (hash && RNODE_LIST(hash)->nd_next) {
14820 NODE *head = RNODE_LIST(hash)->nd_head;
14821 NODE *value = RNODE_LIST(hash)->nd_next;
14822 NODE *next = RNODE_LIST(value)->nd_next;
14823 st_data_t key;
14824 st_data_t data;
14825
14826 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14827 if (!head) {
14828 head = value;
14829 }
14830
14831 if (nd_type_st_key_enable_p(head)) {
14832 key = (st_data_t)head;
14833
14834 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14835 rb_warn2L(nd_line((NODE *)data),
14836 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14837 nd_value(p, head), WARN_I(nd_line(head)));
14838 }
14839 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14840 }
14841 hash = next;
14842 }
14843 st_free_table(p->warn_duplicate_keys_table);
14844 p->warn_duplicate_keys_table = NULL;
14845}
14846
14847static NODE *
14848new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14849{
14850 if (hash) warn_duplicate_keys(p, hash);
14851 return NEW_HASH(hash, loc);
14852}
14853
14854static void
14855error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14856{
14857 if (is_private_local_id(p, id)) {
14858 return;
14859 }
14860 if (st_is_member(p->pvtbl, id)) {
14861 yyerror1(loc, "duplicated variable name");
14862 }
14863 else if (p->ctxt.in_alt_pattern && id) {
14864 yyerror1(loc, "variable capture in alternative pattern");
14865 }
14866 else {
14867 p->ctxt.capture_in_pattern = 1;
14868 st_insert(p->pvtbl, (st_data_t)id, 0);
14869 }
14870}
14871
14872/* Does the pattern node bind any (non-private) local variable? */
14873static int
14874pattern_has_capture(struct parser_params *p, NODE *node)
14875{
14876 if (!node || node == NODE_SPECIAL_NO_NAME_REST || node == NODE_SPECIAL_NO_REST_KEYWORD) return 0;
14877 switch (nd_type(node)) {
14878 case NODE_LASGN:
14879 return !is_private_local_id(p, RNODE_LASGN(node)->nd_vid);
14880 case NODE_DASGN:
14881 return !is_private_local_id(p, RNODE_DASGN(node)->nd_vid);
14882 case NODE_LIST:
14883 for (; node; node = RNODE_LIST(node)->nd_next) {
14884 if (pattern_has_capture(p, RNODE_LIST(node)->nd_head)) return 1;
14885 }
14886 return 0;
14887 case NODE_HASH: /* `pattern => var` and hash pattern key/value list */
14888 return pattern_has_capture(p, RNODE_HASH(node)->nd_head);
14889 case NODE_ARYPTN:
14890 return pattern_has_capture(p, RNODE_ARYPTN(node)->pre_args) ||
14891 pattern_has_capture(p, RNODE_ARYPTN(node)->rest_arg) ||
14892 pattern_has_capture(p, RNODE_ARYPTN(node)->post_args);
14893 case NODE_FNDPTN:
14894 return pattern_has_capture(p, RNODE_FNDPTN(node)->pre_rest_arg) ||
14895 pattern_has_capture(p, RNODE_FNDPTN(node)->args) ||
14896 pattern_has_capture(p, RNODE_FNDPTN(node)->post_rest_arg);
14897 case NODE_HSHPTN:
14898 return pattern_has_capture(p, RNODE_HSHPTN(node)->nd_pkwargs) ||
14899 pattern_has_capture(p, RNODE_HSHPTN(node)->nd_pkwrestarg);
14900 default: /* values, constants, pins (`^x`, `^(expr)`) bind nothing */
14901 return 0;
14902 }
14903}
14904
14905static void
14906error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14907{
14908 if (!p->pktbl) {
14909 p->pktbl = st_init_numtable();
14910 }
14911 else if (st_is_member(p->pktbl, key)) {
14912 yyerror1(loc, "duplicated key name");
14913 return;
14914 }
14915 st_insert(p->pktbl, (st_data_t)key, 0);
14916}
14917
14918static NODE *
14919new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14920{
14921 return NEW_HASH(hash, loc);
14922}
14923
14924static NODE *
14925new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14926{
14927 NODE *asgn;
14928
14929 if (lhs) {
14930 ID vid = get_nd_vid(p, lhs);
14931 YYLTYPE lhs_loc = lhs->nd_loc;
14932 /* A constant read can raise NameError. Prism has no separate read node
14933 * for `Const op= value` and reports the whole expression, so give the
14934 * NODE_CONST the location of the whole operator assignment as well,
14935 * for consistent Thread::Backtrace::Location#source_range between both
14936 * parsers. */
14937 const YYLTYPE *read_loc = nd_type_p(lhs, NODE_CDECL) ? loc : &lhs_loc;
14938 if (op == tOROP) {
14939 set_nd_value(p, lhs, rhs);
14940 nd_set_loc(lhs, loc);
14941 asgn = NEW_OP_ASGN_OR(gettable(p, vid, read_loc), lhs, loc);
14942 }
14943 else if (op == tANDOP) {
14944 set_nd_value(p, lhs, rhs);
14945 nd_set_loc(lhs, loc);
14946 asgn = NEW_OP_ASGN_AND(gettable(p, vid, read_loc), lhs, loc);
14947 }
14948 else {
14949 asgn = lhs;
14950 rhs = NEW_CALL(gettable(p, vid, read_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14951 set_nd_value(p, asgn, rhs);
14952 nd_set_loc(asgn, loc);
14953 }
14954 }
14955 else {
14956 asgn = NEW_ERROR(loc);
14957 }
14958 return asgn;
14959}
14960
14961static NODE *
14962new_ary_op_assign(struct parser_params *p, NODE *ary,
14963 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14964 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14965{
14966 NODE *asgn;
14967
14968 aryset_check(p, args);
14969 args = make_list(args, args_loc);
14970 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14971 fixpos(asgn, ary);
14972 return asgn;
14973}
14974
14975static NODE *
14976new_attr_op_assign(struct parser_params *p, NODE *lhs,
14977 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14978 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14979{
14980 NODE *asgn;
14981
14982 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14983 fixpos(asgn, lhs);
14984 return asgn;
14985}
14986
14987static NODE *
14988new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14989{
14990 NODE *asgn;
14991
14992 if (lhs) {
14993 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14994 }
14995 else {
14996 asgn = NEW_ERROR(loc);
14997 }
14998 fixpos(asgn, lhs);
14999 return asgn;
15000}
15001
15002static NODE *
15003const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
15004{
15005 if (p->ctxt.in_def) {
15006#ifndef RIPPER
15007 yyerror1(loc, "dynamic constant assignment");
15008#else
15009 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
15010#endif
15011 }
15012 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
15013}
15014
15015#ifdef RIPPER
15016static VALUE
15017assign_error(struct parser_params *p, const char *mesg, VALUE a)
15018{
15019 a = dispatch2(assign_error, ERR_MESG(), a);
15020 ripper_error(p);
15021 return a;
15022}
15023#endif
15024
15025static NODE *
15026new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
15027{
15028 NODE *result = head;
15029 if (rescue) {
15030 NODE *tmp = rescue_else ? rescue_else : rescue;
15031 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
15032
15033 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
15034 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
15035 }
15036 if (ensure) {
15037 result = NEW_ENSURE(result, ensure, loc);
15038 }
15039 fixpos(result, head);
15040 return result;
15041}
15042
15043static void
15044warn_unused_var(struct parser_params *p, struct local_vars *local)
15045{
15046 int cnt;
15047
15048 if (!local->used) return;
15049 cnt = local->used->pos;
15050 if (cnt != local->vars->pos) {
15051 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
15052 }
15053#ifndef RIPPER
15054 ID *v = local->vars->tbl;
15055 ID *u = local->used->tbl;
15056 for (int i = 0; i < cnt; ++i) {
15057 if (!v[i] || (u[i] & LVAR_USED)) continue;
15058 if (is_private_local_id(p, v[i])) continue;
15059 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
15060 }
15061#endif
15062}
15063
15064static void
15065local_push(struct parser_params *p, int toplevel_scope)
15066{
15067 struct local_vars *local;
15068 int inherits_dvars = toplevel_scope && compile_for_eval;
15069 int warn_unused_vars = RTEST(ruby_verbose);
15070
15071 local = ALLOC(struct local_vars);
15072 local->prev = p->lvtbl;
15073 local->args = vtable_alloc(0);
15074 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
15075#ifndef RIPPER
15076 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
15077 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
15078#endif
15079 local->numparam.outer = 0;
15080 local->numparam.inner = 0;
15081 local->numparam.current = 0;
15082 local->it = 0;
15083 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
15084
15085# if WARN_PAST_SCOPE
15086 local->past = 0;
15087# endif
15088 CMDARG_PUSH(0);
15089 COND_PUSH(0);
15090 p->lvtbl = local;
15091}
15092
15093static void
15094vtable_chain_free(struct parser_params *p, struct vtable *table)
15095{
15096 while (!DVARS_TERMINAL_P(table)) {
15097 struct vtable *cur_table = table;
15098 table = cur_table->prev;
15099 vtable_free(cur_table);
15100 }
15101}
15102
15103static void
15104local_free(struct parser_params *p, struct local_vars *local)
15105{
15106 vtable_chain_free(p, local->used);
15107
15108# if WARN_PAST_SCOPE
15109 vtable_chain_free(p, local->past);
15110# endif
15111
15112 vtable_chain_free(p, local->args);
15113 vtable_chain_free(p, local->vars);
15114
15115 ruby_xfree_sized(local, sizeof(struct local_vars));
15116}
15117
15118static void
15119local_pop(struct parser_params *p)
15120{
15121 struct local_vars *local = p->lvtbl->prev;
15122 if (p->lvtbl->used) {
15123 warn_unused_var(p, p->lvtbl);
15124 }
15125
15126 local_free(p, p->lvtbl);
15127 p->lvtbl = local;
15128
15129 CMDARG_POP();
15130 COND_POP();
15131}
15132
15133static rb_ast_id_table_t *
15134local_tbl(struct parser_params *p)
15135{
15136 int cnt_args = vtable_size(p->lvtbl->args);
15137 int cnt_vars = vtable_size(p->lvtbl->vars);
15138 int cnt = cnt_args + cnt_vars;
15139 int i, j;
15140 rb_ast_id_table_t *tbl;
15141
15142 if (cnt <= 0) return 0;
15143 tbl = rb_ast_new_local_table(p->ast, cnt);
15144 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
15145 /* remove IDs duplicated to warn shadowing */
15146 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
15147 ID id = p->lvtbl->vars->tbl[i];
15148 if (!vtable_included(p->lvtbl->args, id)) {
15149 tbl->ids[j++] = id;
15150 }
15151 }
15152 if (j < cnt) {
15153 tbl = rb_ast_resize_latest_local_table(p->ast, j);
15154 }
15155
15156 return tbl;
15157}
15158
15159static void
15160numparam_name(struct parser_params *p, ID id)
15161{
15162 if (!NUMPARAM_ID_P(id)) return;
15163 compile_error(p, "_%d is reserved for numbered parameter",
15164 NUMPARAM_ID_TO_IDX(id));
15165}
15166
15167static void
15168arg_var(struct parser_params *p, ID id)
15169{
15170 numparam_name(p, id);
15171 vtable_add(p->lvtbl->args, id);
15172}
15173
15174static void
15175local_var(struct parser_params *p, ID id)
15176{
15177 numparam_name(p, id);
15178 vtable_add(p->lvtbl->vars, id);
15179 if (p->lvtbl->used) {
15180 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15181 }
15182}
15183
15184#ifndef RIPPER
15185int
15186rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15187{
15188 return rb_local_defined(id, iseq);
15189}
15190#endif
15191
15192static int
15193local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15194{
15195 struct vtable *vars, *args, *used;
15196
15197 vars = p->lvtbl->vars;
15198 args = p->lvtbl->args;
15199 used = p->lvtbl->used;
15200
15201 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15202 vars = vars->prev;
15203 args = args->prev;
15204 if (used) used = used->prev;
15205 }
15206
15207 if (vars && vars->prev == DVARS_INHERIT) {
15208 return rb_parser_local_defined(p, id, p->parent_iseq);
15209 }
15210 else if (vtable_included(args, id)) {
15211 return 1;
15212 }
15213 else {
15214 int i = vtable_included(vars, id);
15215 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15216 return i != 0;
15217 }
15218}
15219
15220static int
15221local_id(struct parser_params *p, ID id)
15222{
15223 return local_id_ref(p, id, NULL);
15224}
15225
15226static int
15227check_forwarding_args(struct parser_params *p)
15228{
15229 if (local_id(p, idFWD_ALL)) return TRUE;
15230 compile_error(p, "unexpected ...");
15231 return FALSE;
15232}
15233
15234static void
15235add_forwarding_args(struct parser_params *p)
15236{
15237 arg_var(p, idFWD_REST);
15238 arg_var(p, idFWD_KWREST);
15239 arg_var(p, idFWD_BLOCK);
15240 arg_var(p, idFWD_ALL);
15241}
15242
15243static void
15244forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15245{
15246 bool conflict = false;
15247
15248 struct vtable *vars, *args;
15249
15250 vars = p->lvtbl->vars;
15251 args = p->lvtbl->args;
15252
15253 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15254 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15255 vars = vars->prev;
15256 args = args->prev;
15257 }
15258
15259 bool found = false;
15260 if (vars && vars->prev == DVARS_INHERIT && !found) {
15261 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15262 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15263 }
15264 else {
15265 found = (vtable_included(args, arg) &&
15266 !(all && vtable_included(args, all)));
15267 }
15268
15269 if (!found) {
15270 compile_error(p, "no anonymous %s parameter", var);
15271 }
15272 else if (conflict) {
15273 compile_error(p, "anonymous %s parameter is also used within block", var);
15274 }
15275}
15276
15277static NODE *
15278new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15279{
15280 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15281 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15282 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15283 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15284 block->forwarding = TRUE;
15285 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15286 return arg_blk_pass(args, block);
15287}
15288
15289static NODE *
15290numparam_push(struct parser_params *p)
15291{
15292 struct local_vars *local = p->lvtbl;
15293 NODE *inner = local->numparam.inner;
15294 if (!local->numparam.outer) {
15295 local->numparam.outer = local->numparam.current;
15296 }
15297 local->numparam.inner = 0;
15298 local->numparam.current = 0;
15299 local->it = 0;
15300 return inner;
15301}
15302
15303static void
15304numparam_pop(struct parser_params *p, NODE *prev_inner)
15305{
15306 struct local_vars *local = p->lvtbl;
15307 if (prev_inner) {
15308 /* prefer first one */
15309 local->numparam.inner = prev_inner;
15310 }
15311 else if (local->numparam.current) {
15312 /* current and inner are exclusive */
15313 local->numparam.inner = local->numparam.current;
15314 }
15315 if (p->max_numparam > NO_PARAM) {
15316 /* current and outer are exclusive */
15317 local->numparam.current = local->numparam.outer;
15318 local->numparam.outer = 0;
15319 }
15320 else {
15321 /* no numbered parameter */
15322 local->numparam.current = 0;
15323 }
15324 local->it = 0;
15325}
15326
15327static const struct vtable *
15328dyna_push(struct parser_params *p)
15329{
15330 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15331 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15332 if (p->lvtbl->used) {
15333 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15334 }
15335 return p->lvtbl->args;
15336}
15337
15338static void
15339dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15340{
15341 struct vtable *tmp = *vtblp;
15342 *vtblp = tmp->prev;
15343# if WARN_PAST_SCOPE
15344 if (p->past_scope_enabled) {
15345 tmp->prev = p->lvtbl->past;
15346 p->lvtbl->past = tmp;
15347 return;
15348 }
15349# endif
15350 vtable_free(tmp);
15351}
15352
15353static void
15354dyna_pop_1(struct parser_params *p)
15355{
15356 struct vtable *tmp;
15357
15358 if ((tmp = p->lvtbl->used) != 0) {
15359 warn_unused_var(p, p->lvtbl);
15360 p->lvtbl->used = p->lvtbl->used->prev;
15361 vtable_free(tmp);
15362 }
15363 dyna_pop_vtable(p, &p->lvtbl->args);
15364 dyna_pop_vtable(p, &p->lvtbl->vars);
15365}
15366
15367static void
15368dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15369{
15370 while (p->lvtbl->args != lvargs) {
15371 dyna_pop_1(p);
15372 if (!p->lvtbl->args) {
15373 struct local_vars *local = p->lvtbl->prev;
15374 ruby_xfree_sized(p->lvtbl, sizeof(*p->lvtbl));
15375 p->lvtbl = local;
15376 }
15377 }
15378 dyna_pop_1(p);
15379}
15380
15381static int
15382dyna_in_block(struct parser_params *p)
15383{
15384 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15385}
15386
15387#ifndef RIPPER
15388int
15389dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15390{
15391 struct vtable *vars, *args, *used;
15392 int i;
15393
15394 args = p->lvtbl->args;
15395 vars = p->lvtbl->vars;
15396 used = p->lvtbl->used;
15397
15398 while (!DVARS_TERMINAL_P(vars)) {
15399 if (vtable_included(args, id)) {
15400 return 1;
15401 }
15402 if ((i = vtable_included(vars, id)) != 0) {
15403 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15404 return 1;
15405 }
15406 args = args->prev;
15407 vars = vars->prev;
15408 if (!vidrefp) used = 0;
15409 if (used) used = used->prev;
15410 }
15411
15412 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15413 return rb_dvar_defined(id, p->parent_iseq);
15414 }
15415
15416 return 0;
15417}
15418#endif
15419
15420static int
15421dvar_defined(struct parser_params *p, ID id)
15422{
15423 return dvar_defined_ref(p, id, NULL);
15424}
15425
15426static int
15427dvar_curr(struct parser_params *p, ID id)
15428{
15429 return (vtable_included(p->lvtbl->args, id) ||
15430 vtable_included(p->lvtbl->vars, id));
15431}
15432
15433static void
15434reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15435{
15436 compile_error(p,
15437 "regexp encoding option '%c' differs from source encoding '%s'",
15438 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15439}
15440
15441#ifndef RIPPER
15442static rb_encoding *
15443find_enc(struct parser_params* p, const char *name)
15444{
15445 int idx = rb_enc_find_index(name);
15446 if (idx < 0) {
15447 rb_bug("unknown encoding name: %s", name);
15448 }
15449
15450 return rb_enc_from_index(idx);
15451}
15452
15453static rb_encoding *
15454kcode_to_enc(struct parser_params* p, int kcode)
15455{
15456 rb_encoding *enc;
15457
15458 switch (kcode) {
15459 case ENC_ASCII8BIT:
15460 enc = rb_ascii8bit_encoding();
15461 break;
15462 case ENC_EUC_JP:
15463 enc = find_enc(p, "EUC-JP");
15464 break;
15465 case ENC_Windows_31J:
15466 enc = find_enc(p, "Windows-31J");
15467 break;
15468 case ENC_UTF8:
15469 enc = rb_utf8_encoding();
15470 break;
15471 default:
15472 enc = NULL;
15473 break;
15474 }
15475
15476 return enc;
15477}
15478
15479int
15480rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15481{
15482 int c = RE_OPTION_ENCODING_IDX(options);
15483
15484 if (c) {
15485 int opt, idx;
15486 rb_encoding *enc;
15487
15488 char_to_option_kcode(c, &opt, &idx);
15489 enc = kcode_to_enc(p, idx);
15490 if (enc != rb_parser_str_get_encoding(str) &&
15491 !rb_parser_is_ascii_string(p, str)) {
15492 goto error;
15493 }
15494 rb_parser_string_set_encoding(str, enc);
15495 }
15496 else if (RE_OPTION_ENCODING_NONE(options)) {
15497 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15498 !rb_parser_is_ascii_string(p, str)) {
15499 c = 'n';
15500 goto error;
15501 }
15502 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15503 }
15504 else if (rb_is_usascii_enc(p->enc)) {
15505 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15506 }
15507 return 0;
15508
15509 error:
15510 return c;
15511}
15512#endif
15513
15514static void
15515reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15516{
15517 int c = rb_reg_fragment_setenc(p, str, options);
15518 if (c) reg_fragment_enc_error(p, str, c);
15519}
15520
15521#ifndef UNIVERSAL_PARSER
15522typedef struct {
15523 struct parser_params* parser;
15524 rb_encoding *enc;
15525 NODE *succ_block;
15526 const YYLTYPE *loc;
15527 rb_parser_assignable_func assignable;
15528} reg_named_capture_assign_t;
15529
15530static int
15531reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15532 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15533{
15534 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15535 struct parser_params* p = arg->parser;
15536 rb_encoding *enc = arg->enc;
15537 long len = name_end - name;
15538 const char *s = (const char *)name;
15539
15540 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15541}
15542
15543static NODE *
15544reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15545{
15546 reg_named_capture_assign_t arg;
15547
15548 arg.parser = p;
15549 arg.enc = rb_enc_get(regexp);
15550 arg.succ_block = 0;
15551 arg.loc = loc;
15552 arg.assignable = assignable;
15553 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15554
15555 if (!arg.succ_block) return 0;
15556 return RNODE_BLOCK(arg.succ_block)->nd_next;
15557}
15558#endif
15559
15560#ifndef RIPPER
15561NODE *
15562rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15563{
15564 return assignable(p, id, val, loc);
15565}
15566
15567int
15568rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15569 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15570{
15571 ID var;
15572 NODE *node, *succ;
15573
15574 if (!len) return ST_CONTINUE;
15575
15576 if (UNLIKELY(p->enc != enc)) {
15577 int cr = ENC_CODERANGE_7BIT;
15578 rb_str_coderange_scan_restartable(s, s + len, rb_ascii8bit_encoding(), &cr);
15579 if (UNLIKELY(cr != ENC_CODERANGE_7BIT)) return ST_CONTINUE;
15580 }
15581
15582 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15583 return ST_CONTINUE;
15584
15585 var = intern_cstr(s, len, enc);
15586 if (len <= MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15587 if (!lvar_defined(p, var)) return ST_CONTINUE;
15588 }
15589 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15590 succ = *succ_block;
15591 if (!succ) succ = NEW_ERROR(loc);
15592 succ = block_append(p, succ, node);
15593 *succ_block = succ;
15594 return ST_CONTINUE;
15595}
15596#endif
15597
15598static VALUE
15599parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15600{
15601 VALUE str2;
15602 reg_fragment_setenc(p, str, options);
15603 str2 = rb_str_new_parser_string(str);
15604 return rb_parser_reg_compile(p, str2, options);
15605}
15606
15607#ifndef RIPPER
15608VALUE
15609rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15610{
15611 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15612}
15613#endif
15614
15615static VALUE
15616reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15617{
15618 VALUE re;
15619 VALUE err;
15620
15621 err = rb_errinfo();
15622 re = parser_reg_compile(p, str, options);
15623 if (NIL_P(re)) {
15624 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15625 rb_set_errinfo(err);
15626 compile_error(p, "%"PRIsVALUE, m);
15627 return Qnil;
15628 }
15629 return re;
15630}
15631
15632#ifndef RIPPER
15633void
15634rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15635{
15636 p->do_print = print;
15637 p->do_loop = loop;
15638 p->do_chomp = chomp;
15639 p->do_split = split;
15640}
15641
15642static NODE *
15643parser_append_options(struct parser_params *p, NODE *node)
15644{
15645 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15646 const YYLTYPE *const LOC = &default_location;
15647
15648 if (p->do_print) {
15649 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15650 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15651 LOC);
15652 node = block_append(p, node, print);
15653 }
15654
15655 if (p->do_loop) {
15656 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15657
15658 if (p->do_split) {
15659 ID ifs = rb_intern("$;");
15660 ID fields = rb_intern("$F");
15661 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15662 NODE *split = NEW_GASGN(fields,
15663 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15664 rb_intern("split"), args, LOC),
15665 LOC);
15666 node = block_append(p, split, node);
15667 }
15668 if (p->do_chomp) {
15669 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15670 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15671 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15672 }
15673
15674 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15675 }
15676
15677 return node;
15678}
15679
15680void
15681rb_init_parse(void)
15682{
15683 /* just to suppress unused-function warnings */
15684 (void)nodetype;
15685 (void)nodeline;
15686}
15687
15688ID
15689internal_id(struct parser_params *p)
15690{
15691 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15692}
15693#endif /* !RIPPER */
15694
15695static void
15696parser_initialize(struct parser_params *p)
15697{
15698 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15699 p->command_start = TRUE;
15700 p->ruby_sourcefile_string = Qnil;
15701 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15702 string_buffer_init(p);
15703 p->node_id = 0;
15704 p->delayed.token = NULL;
15705 p->frozen_string_literal = -1; /* not specified */
15706 rb_source_hash_init(&p->source_hash);
15707#ifndef RIPPER
15708 p->error_buffer = Qfalse;
15709 p->end_expect_token_locations = NULL;
15710 p->token_id = 0;
15711 p->tokens = NULL;
15712#else
15713 p->result = Qnil;
15714 p->parsing_thread = Qnil;
15715 p->s_value = Qnil;
15716 p->s_lvalue = Qnil;
15717 p->s_value_stack = rb_ary_new();
15718#endif
15719 p->debug_buffer = Qnil;
15720 p->debug_output = rb_ractor_stdout();
15721 p->enc = rb_utf8_encoding();
15722 p->exits = 0;
15723}
15724
15725#ifdef RIPPER
15726#define rb_ruby_parser_mark ripper_parser_mark
15727#define rb_ruby_parser_free ripper_parser_free
15728#define rb_ruby_parser_memsize ripper_parser_memsize
15729#endif
15730
15731void
15732rb_ruby_parser_mark(void *ptr)
15733{
15734 struct parser_params *p = (struct parser_params*)ptr;
15735
15736 rb_gc_mark(p->ruby_sourcefile_string);
15737#ifndef RIPPER
15738 rb_gc_mark(p->error_buffer);
15739#else
15740 rb_gc_mark(p->value);
15741 rb_gc_mark(p->result);
15742 rb_gc_mark(p->parsing_thread);
15743 rb_gc_mark(p->s_value);
15744 rb_gc_mark(p->s_lvalue);
15745 rb_gc_mark(p->s_value_stack);
15746#endif
15747 rb_gc_mark(p->debug_buffer);
15748 rb_gc_mark(p->debug_output);
15749}
15750
15751void
15752rb_ruby_parser_free(void *ptr)
15753{
15754 struct parser_params *p = (struct parser_params*)ptr;
15755 struct local_vars *local, *prev;
15756
15757 if (p->ast) {
15758 rb_ast_free(p->ast);
15759 }
15760
15761 if (p->warn_duplicate_keys_table) {
15762 st_free_table(p->warn_duplicate_keys_table);
15763 }
15764
15765#ifndef RIPPER
15766 if (p->tokens) {
15767 rb_parser_ary_free(p, p->tokens);
15768 }
15769#endif
15770
15771 if (p->tokenbuf) {
15772 ruby_xfree_sized(p->tokenbuf, p->toksiz);
15773 }
15774
15775 for (local = p->lvtbl; local; local = prev) {
15776 prev = local->prev;
15777 local_free(p, local);
15778 }
15779
15780 {
15781 token_info *ptinfo;
15782 while ((ptinfo = p->token_info) != 0) {
15783 p->token_info = ptinfo->next;
15784 xfree(ptinfo);
15785 }
15786 }
15787 string_buffer_free(p);
15788
15789 if (p->pvtbl) {
15790 st_free_table(p->pvtbl);
15791 }
15792
15793 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15794 st_free_table(p->case_labels);
15795 }
15796
15797 xfree(p->lex.strterm);
15798 p->lex.strterm = 0;
15799
15800 xfree(ptr);
15801}
15802
15803size_t
15804rb_ruby_parser_memsize(const void *ptr)
15805{
15806 struct parser_params *p = (struct parser_params*)ptr;
15807 struct local_vars *local;
15808 size_t size = sizeof(*p);
15809
15810 size += p->toksiz;
15811 for (local = p->lvtbl; local; local = local->prev) {
15812 size += sizeof(*local);
15813 if (local->vars) size += local->vars->capa * sizeof(ID);
15814 }
15815 return size;
15816}
15817
15818#ifndef RIPPER
15819#undef rb_reserved_word
15820
15821const struct kwtable *
15822rb_reserved_word(const char *str, unsigned int len)
15823{
15824 return reserved_word(str, len);
15825}
15826
15827#ifdef UNIVERSAL_PARSER
15828rb_parser_t *
15829rb_ruby_parser_allocate(const rb_parser_config_t *config)
15830{
15831 /* parser_initialize expects fields to be set to 0 */
15832 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15833 p->config = config;
15834 return p;
15835}
15836
15837rb_parser_t *
15838rb_ruby_parser_new(const rb_parser_config_t *config)
15839{
15840 /* parser_initialize expects fields to be set to 0 */
15841 rb_parser_t *p = rb_ruby_parser_allocate(config);
15842 parser_initialize(p);
15843 return p;
15844}
15845#else
15846rb_parser_t *
15847rb_ruby_parser_allocate(void)
15848{
15849 /* parser_initialize expects fields to be set to 0 */
15850 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15851 return p;
15852}
15853
15854rb_parser_t *
15855rb_ruby_parser_new(void)
15856{
15857 /* parser_initialize expects fields to be set to 0 */
15858 rb_parser_t *p = rb_ruby_parser_allocate();
15859 parser_initialize(p);
15860 return p;
15861}
15862#endif
15863
15864rb_parser_t *
15865rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15866{
15867 p->error_buffer = main ? Qfalse : Qnil;
15868 p->parent_iseq = base;
15869 return p;
15870}
15871
15872void
15873rb_ruby_parser_set_script_lines(rb_parser_t *p)
15874{
15875 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15876}
15877
15878void
15879rb_ruby_parser_error_tolerant(rb_parser_t *p)
15880{
15881 p->error_tolerant = 1;
15882}
15883
15884void
15885rb_ruby_parser_keep_tokens(rb_parser_t *p)
15886{
15887 p->keep_tokens = 1;
15888 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15889}
15890
15891rb_encoding *
15892rb_ruby_parser_encoding(rb_parser_t *p)
15893{
15894 return p->enc;
15895}
15896
15897int
15898rb_ruby_parser_end_seen_p(rb_parser_t *p)
15899{
15900 return p->ruby__end__seen;
15901}
15902
15903int
15904rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15905{
15906 p->debug = flag;
15907 return flag;
15908}
15909#endif /* !RIPPER */
15910
15911#ifdef RIPPER
15912int
15913rb_ruby_parser_get_yydebug(rb_parser_t *p)
15914{
15915 return p->debug;
15916}
15917
15918void
15919rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15920{
15921 p->value = value;
15922}
15923
15924int
15925rb_ruby_parser_error_p(rb_parser_t *p)
15926{
15927 return p->error_p;
15928}
15929
15930VALUE
15931rb_ruby_parser_debug_output(rb_parser_t *p)
15932{
15933 return p->debug_output;
15934}
15935
15936void
15937rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15938{
15939 p->debug_output = output;
15940}
15941
15942VALUE
15943rb_ruby_parser_parsing_thread(rb_parser_t *p)
15944{
15945 return p->parsing_thread;
15946}
15947
15948void
15949rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15950{
15951 p->parsing_thread = parsing_thread;
15952}
15953
15954void
15955rb_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)
15956{
15957 p->lex.gets = gets;
15958 p->lex.input = input;
15959 p->eofp = 0;
15960 p->ruby_sourcefile_string = sourcefile_string;
15961 p->ruby_sourcefile = sourcefile;
15962 p->ruby_sourceline = sourceline;
15963}
15964
15965VALUE
15966rb_ruby_parser_result(rb_parser_t *p)
15967{
15968 return p->result;
15969}
15970
15971rb_encoding *
15972rb_ruby_parser_enc(rb_parser_t *p)
15973{
15974 return p->enc;
15975}
15976
15977VALUE
15978rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15979{
15980 return p->ruby_sourcefile_string;
15981}
15982
15983int
15984rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15985{
15986 return p->ruby_sourceline;
15987}
15988
15989int
15990rb_ruby_parser_lex_state(rb_parser_t *p)
15991{
15992 return p->lex.state;
15993}
15994
15995void
15996rb_ruby_ripper_parse0(rb_parser_t *p)
15997{
15998 parser_prepare(p);
15999 p->ast = rb_ast_new();
16000 ripper_yyparse((void*)p);
16001 rb_ast_free(p->ast);
16002 p->ast = 0;
16003 p->eval_tree = 0;
16004 p->eval_tree_begin = 0;
16005}
16006
16007int
16008rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
16009{
16010 return dedent_string(p, string, width);
16011}
16012
16013int
16014rb_ruby_ripper_initialized_p(rb_parser_t *p)
16015{
16016 return p->lex.input != 0;
16017}
16018
16019void
16020rb_ruby_ripper_parser_initialize(rb_parser_t *p)
16021{
16022 parser_initialize(p);
16023}
16024
16025long
16026rb_ruby_ripper_column(rb_parser_t *p)
16027{
16028 return p->lex.ptok - p->lex.pbeg;
16029}
16030
16031long
16032rb_ruby_ripper_token_len(rb_parser_t *p)
16033{
16034 return p->lex.pcur - p->lex.ptok;
16035}
16036
16037rb_parser_string_t *
16038rb_ruby_ripper_lex_lastline(rb_parser_t *p)
16039{
16040 return p->lex.lastline;
16041}
16042
16043VALUE
16044rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
16045{
16046 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
16047}
16048
16049#ifdef UNIVERSAL_PARSER
16050rb_parser_t *
16051rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
16052{
16053 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
16054 p->config = config;
16055 return p;
16056}
16057#endif
16058
16059struct parser_params*
16060rb_ruby_ripper_parser_allocate(void)
16061{
16062 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
16063}
16064#endif /* RIPPER */
16065
16066#ifndef RIPPER
16067void
16068rb_parser_printf(struct parser_params *p, const char *fmt, ...)
16069{
16070 va_list ap;
16071 VALUE mesg = p->debug_buffer;
16072
16073 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
16074 va_start(ap, fmt);
16075 rb_str_vcatf(mesg, fmt, ap);
16076 va_end(ap);
16077 if (char_at_end(p, mesg, 0) == '\n') {
16078 rb_io_write(p->debug_output, mesg);
16079 p->debug_buffer = Qnil;
16080 }
16081}
16082
16083static void
16084parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
16085{
16086 va_list ap;
16087 int lineno, column;
16088
16089 if (loc) {
16090 lineno = loc->end_pos.lineno;
16091 column = loc->end_pos.column;
16092 }
16093 else {
16094 lineno = p->ruby_sourceline;
16095 column = rb_len2int(p->lex.pcur - p->lex.pbeg);
16096 }
16097
16098 rb_io_flush(p->debug_output);
16099 p->error_p = 1;
16100 va_start(ap, fmt);
16101 p->error_buffer =
16102 rb_syntax_error_append(p->error_buffer,
16103 p->ruby_sourcefile_string,
16104 lineno, column,
16105 p->enc, fmt, ap);
16106 va_end(ap);
16107}
16108
16109static size_t
16110count_char(const char *str, int c)
16111{
16112 int n = 0;
16113 while (str[n] == c) ++n;
16114 return n;
16115}
16116
16117/*
16118 * strip enclosing double-quotes, same as the default yytnamerr except
16119 * for that single-quotes matching back-quotes do not stop stripping.
16120 *
16121 * "\"`class' keyword\"" => "`class' keyword"
16122 */
16123size_t
16124rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16125{
16126 if (*yystr == '"') {
16127 size_t yyn = 0, bquote = 0;
16128 const char *yyp = yystr;
16129
16130 while (*++yyp) {
16131 switch (*yyp) {
16132 case '\'':
16133 if (!bquote) {
16134 bquote = count_char(yyp+1, '\'') + 1;
16135 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16136 yyn += bquote;
16137 yyp += bquote - 1;
16138 break;
16139 }
16140 else {
16141 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16142 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16143 yyn += bquote;
16144 yyp += bquote - 1;
16145 bquote = 0;
16146 break;
16147 }
16148 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16149 if (yyres) memcpy(yyres + yyn, yyp, 3);
16150 yyn += 3;
16151 yyp += 2;
16152 break;
16153 }
16154 goto do_not_strip_quotes;
16155 }
16156
16157 case ',':
16158 goto do_not_strip_quotes;
16159
16160 case '\\':
16161 if (*++yyp != '\\')
16162 goto do_not_strip_quotes;
16163 /* Fall through. */
16164 default:
16165 if (yyres)
16166 yyres[yyn] = *yyp;
16167 yyn++;
16168 break;
16169
16170 case '"':
16171 case '\0':
16172 if (yyres)
16173 yyres[yyn] = '\0';
16174 return yyn;
16175 }
16176 }
16177 do_not_strip_quotes: ;
16178 }
16179
16180 if (!yyres) return strlen(yystr);
16181
16182 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16183}
16184#endif
16185
16186#ifdef RIPPER
16187#define validate(x) (void)(x)
16188
16189static VALUE
16190ripper_dispatch0(struct parser_params *p, ID mid)
16191{
16192 return rb_funcall(p->value, mid, 0);
16193}
16194
16195static VALUE
16196ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16197{
16198 validate(a);
16199 return rb_funcall(p->value, mid, 1, a);
16200}
16201
16202static VALUE
16203ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16204{
16205 validate(a);
16206 validate(b);
16207 return rb_funcall(p->value, mid, 2, a, b);
16208}
16209
16210static VALUE
16211ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16212{
16213 validate(a);
16214 validate(b);
16215 validate(c);
16216 return rb_funcall(p->value, mid, 3, a, b, c);
16217}
16218
16219static VALUE
16220ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16221{
16222 validate(a);
16223 validate(b);
16224 validate(c);
16225 validate(d);
16226 return rb_funcall(p->value, mid, 4, a, b, c, d);
16227}
16228
16229static VALUE
16230ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16231{
16232 validate(a);
16233 validate(b);
16234 validate(c);
16235 validate(d);
16236 validate(e);
16237 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16238}
16239
16240static VALUE
16241ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16242{
16243 validate(a);
16244 validate(b);
16245 validate(c);
16246 validate(d);
16247 validate(e);
16248 validate(f);
16249 validate(g);
16250 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16251}
16252
16253void
16254ripper_error(struct parser_params *p)
16255{
16256 p->error_p = TRUE;
16257}
16258
16259VALUE
16260ripper_value(struct parser_params *p)
16261{
16262 (void)yystpcpy; /* may not used in newer bison */
16263
16264 return p->value;
16265}
16266
16267#endif /* RIPPER */
16268/*
16269 * Local variables:
16270 * mode: c
16271 * c-file-style: "ruby"
16272 * End:
16273 */