Ruby 3.5.0dev (2025-09-16 revision 1213adfe5526d65cce81a9fb127074130c8faea7)
parse.y (1213adfe5526d65cce81a9fb127074130c8faea7)
1/**********************************************************************
2
3 parse.y -
4
5 $Author$
6 created at: Fri May 28 18:02:42 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12%{
13
14#if !YYPURE
15# error needs pure parser
16#endif
17#define YYDEBUG 1
18#define YYERROR_VERBOSE 1
19#define YYSTACK_USE_ALLOCA 0
20
21/* For Ripper */
22#ifdef RUBY_EXTCONF_H
23# include RUBY_EXTCONF_H
24#endif
25
26#include "ruby/internal/config.h"
27
28#include <errno.h>
29
30#ifdef UNIVERSAL_PARSER
31
32#include "internal/ruby_parser.h"
33#include "parser_node.h"
34#include "universal_parser.c"
35
36#ifdef RIPPER
37#define STATIC_ID2SYM p->config->static_id2sym
38#define rb_str_coderange_scan_restartable p->config->str_coderange_scan_restartable
39#endif
40
41#else
42
43#include "internal.h"
44#include "internal/compile.h"
45#include "internal/compilers.h"
46#include "internal/complex.h"
47#include "internal/encoding.h"
48#include "internal/error.h"
49#include "internal/hash.h"
50#include "internal/io.h"
51#include "internal/numeric.h"
52#include "internal/parse.h"
53#include "internal/rational.h"
54#include "internal/re.h"
55#include "internal/ruby_parser.h"
56#include "internal/symbol.h"
57#include "internal/thread.h"
58#include "internal/variable.h"
59#include "node.h"
60#include "parser_node.h"
61#include "probes.h"
62#include "regenc.h"
63#include "ruby/encoding.h"
64#include "ruby/regex.h"
65#include "ruby/ruby.h"
66#include "ruby/st.h"
67#include "ruby/util.h"
68#include "ruby/ractor.h"
69#include "symbol.h"
70
71#ifndef RIPPER
72static VALUE
73syntax_error_new(void)
74{
75 return rb_class_new_instance(0, 0, rb_eSyntaxError);
76}
77#endif
78
79static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable);
80
81#define compile_callback rb_suppress_tracing
82#endif /* !UNIVERSAL_PARSER */
83
84#define NODE_SPECIAL_EMPTY_ARGS ((NODE *)-1)
85#define NODE_EMPTY_ARGS_P(node) ((node) == NODE_SPECIAL_EMPTY_ARGS)
86
87static int rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2);
88
89#ifndef RIPPER
90static rb_parser_string_t *rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *original);
91#endif
92
93static int
94node_integer_cmp(rb_node_integer_t *n1, rb_node_integer_t *n2)
95{
96 return (n1->minus != n2->minus ||
97 n1->base != n2->base ||
98 strcmp(n1->val, n2->val));
99}
100
101static int
102node_float_cmp(rb_node_float_t *n1, rb_node_float_t *n2)
103{
104 return (n1->minus != n2->minus ||
105 strcmp(n1->val, n2->val));
106}
107
108static int
109node_rational_cmp(rb_node_rational_t *n1, rb_node_rational_t *n2)
110{
111 return (n1->minus != n2->minus ||
112 n1->base != n2->base ||
113 n1->seen_point != n2->seen_point ||
114 strcmp(n1->val, n2->val));
115}
116
117static int
118node_imaginary_cmp(rb_node_imaginary_t *n1, rb_node_imaginary_t *n2)
119{
120 return (n1->minus != n2->minus ||
121 n1->base != n2->base ||
122 n1->seen_point != n2->seen_point ||
123 n1->type != n2->type ||
124 strcmp(n1->val, n2->val));
125}
126
127static int
128rb_parser_regx_hash_cmp(rb_node_regx_t *n1, rb_node_regx_t *n2)
129{
130 return (n1->options != n2->options ||
131 rb_parser_string_hash_cmp(n1->string, n2->string));
132}
133
134static st_index_t rb_parser_str_hash(rb_parser_string_t *str);
135static st_index_t rb_char_p_hash(const char *c);
136
137static int
138literal_cmp(st_data_t val, st_data_t lit)
139{
140 if (val == lit) return 0;
141
142 NODE *node_val = RNODE(val);
143 NODE *node_lit = RNODE(lit);
144 enum node_type type_val = nd_type(node_val);
145 enum node_type type_lit = nd_type(node_lit);
146
147 if (type_val != type_lit) {
148 return -1;
149 }
150
151 switch (type_lit) {
152 case NODE_INTEGER:
153 return node_integer_cmp(RNODE_INTEGER(node_val), RNODE_INTEGER(node_lit));
154 case NODE_FLOAT:
155 return node_float_cmp(RNODE_FLOAT(node_val), RNODE_FLOAT(node_lit));
156 case NODE_RATIONAL:
157 return node_rational_cmp(RNODE_RATIONAL(node_val), RNODE_RATIONAL(node_lit));
158 case NODE_IMAGINARY:
159 return node_imaginary_cmp(RNODE_IMAGINARY(node_val), RNODE_IMAGINARY(node_lit));
160 case NODE_STR:
161 return rb_parser_string_hash_cmp(RNODE_STR(node_val)->string, RNODE_STR(node_lit)->string);
162 case NODE_SYM:
163 return rb_parser_string_hash_cmp(RNODE_SYM(node_val)->string, RNODE_SYM(node_lit)->string);
164 case NODE_REGX:
165 return rb_parser_regx_hash_cmp(RNODE_REGX(node_val), RNODE_REGX(node_lit));
166 case NODE_LINE:
167 return node_val->nd_loc.beg_pos.lineno != node_lit->nd_loc.beg_pos.lineno;
168 case NODE_FILE:
169 return rb_parser_string_hash_cmp(RNODE_FILE(node_val)->path, RNODE_FILE(node_lit)->path);
170 case NODE_ENCODING:
171 return RNODE_ENCODING(node_val)->enc != RNODE_ENCODING(node_lit)->enc;
172 default:
173#ifdef UNIVERSAL_PARSER
174 abort();
175#else
176 rb_bug("unexpected node: %s, %s", ruby_node_name(type_val), ruby_node_name(type_lit));
177#endif
178 }
179}
180
181static st_index_t
182literal_hash(st_data_t a)
183{
184 NODE *node = (NODE *)a;
185 enum node_type type = nd_type(node);
186
187 switch (type) {
188 case NODE_INTEGER:
189 return rb_char_p_hash(RNODE_INTEGER(node)->val);
190 case NODE_FLOAT:
191 return rb_char_p_hash(RNODE_FLOAT(node)->val);
192 case NODE_RATIONAL:
193 return rb_char_p_hash(RNODE_RATIONAL(node)->val);
194 case NODE_IMAGINARY:
195 return rb_char_p_hash(RNODE_IMAGINARY(node)->val);
196 case NODE_STR:
197 return rb_parser_str_hash(RNODE_STR(node)->string);
198 case NODE_SYM:
199 return rb_parser_str_hash(RNODE_SYM(node)->string);
200 case NODE_REGX:
201 return rb_parser_str_hash(RNODE_REGX(node)->string);
202 case NODE_LINE:
203 return (st_index_t)node->nd_loc.beg_pos.lineno;
204 case NODE_FILE:
205 return rb_parser_str_hash(RNODE_FILE(node)->path);
206 case NODE_ENCODING:
207 return (st_index_t)RNODE_ENCODING(node)->enc;
208 default:
209#ifdef UNIVERSAL_PARSER
210 abort();
211#else
212 rb_bug("unexpected node: %s", ruby_node_name(type));
213#endif
214 }
215}
216
217static inline int
218parse_isascii(int c)
219{
220 return '\0' <= c && c <= '\x7f';
221}
222
223#undef ISASCII
224#define ISASCII parse_isascii
225
226static inline int
227parse_isspace(int c)
228{
229 return c == ' ' || ('\t' <= c && c <= '\r');
230}
231
232#undef ISSPACE
233#define ISSPACE parse_isspace
234
235static inline int
236parse_iscntrl(int c)
237{
238 return ('\0' <= c && c < ' ') || c == '\x7f';
239}
240
241#undef ISCNTRL
242#define ISCNTRL(c) parse_iscntrl(c)
243
244static inline int
245parse_isupper(int c)
246{
247 return 'A' <= c && c <= 'Z';
248}
249
250static inline int
251parse_islower(int c)
252{
253 return 'a' <= c && c <= 'z';
254}
255
256static inline int
257parse_isalpha(int c)
258{
259 return parse_isupper(c) || parse_islower(c);
260}
261
262#undef ISALPHA
263#define ISALPHA(c) parse_isalpha(c)
264
265static inline int
266parse_isdigit(int c)
267{
268 return '0' <= c && c <= '9';
269}
270
271#undef ISDIGIT
272#define ISDIGIT(c) parse_isdigit(c)
273
274static inline int
275parse_isalnum(int c)
276{
277 return ISALPHA(c) || ISDIGIT(c);
278}
279
280#undef ISALNUM
281#define ISALNUM(c) parse_isalnum(c)
282
283static inline int
284parse_isxdigit(int c)
285{
286 return ISDIGIT(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
287}
288
289#undef ISXDIGIT
290#define ISXDIGIT(c) parse_isxdigit(c)
291
292#include "parser_st.h"
293
294#undef STRCASECMP
295#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
296
297#undef STRNCASECMP
298#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
299
300#ifdef RIPPER
301#include "ripper_init.h"
302#endif
303
304enum rescue_context {
305 before_rescue,
306 after_rescue,
307 after_else,
308 after_ensure,
309};
310
311struct lex_context {
312 unsigned int in_defined: 1;
313 unsigned int in_kwarg: 1;
314 unsigned int in_argdef: 1;
315 unsigned int in_def: 1;
316 unsigned int in_class: 1;
317 unsigned int has_trailing_semicolon: 1;
318 BITFIELD(enum rb_parser_shareability, shareable_constant_value, 2);
319 BITFIELD(enum rescue_context, in_rescue, 2);
320 unsigned int cant_return: 1;
321};
322
323typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
324
325#if defined(__GNUC__) && !defined(__clang__)
326// Suppress "parameter passing for argument of type 'struct
327// lex_context' changed" notes. `struct lex_context` is file scope,
328// and has no ABI compatibility issue.
329RBIMPL_WARNING_PUSH()
330RBIMPL_WARNING_IGNORED(-Wpsabi)
331RBIMPL_WARNING_POP()
332// Not sure why effective even after popped.
333#endif
334
335#include "parse.h"
336
337#define NO_LEX_CTXT (struct lex_context){0}
338
339#ifndef WARN_PAST_SCOPE
340# define WARN_PAST_SCOPE 0
341#endif
342
343#define TAB_WIDTH 8
344
345#define yydebug (p->debug) /* disable the global variable definition */
346
347#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__)
348#define YY_LOCATION_PRINT(File, loc, p) \
349 rb_parser_printf(p, "%d.%d-%d.%d", \
350 (loc).beg_pos.lineno, (loc).beg_pos.column,\
351 (loc).end_pos.lineno, (loc).end_pos.column)
352#define YYLLOC_DEFAULT(Current, Rhs, N) \
353 do \
354 if (N) \
355 { \
356 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
357 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
358 } \
359 else \
360 { \
361 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
362 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
363 } \
364 while (0)
365#define YY_(Msgid) \
366 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
367 "nesting too deep" : (Msgid))
368
369#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
370 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
371#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
372 rb_parser_set_location_of_delayed_token(p, &(Current))
373#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
374 rb_parser_set_location_of_heredoc_end(p, &(Current))
375#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
376 rb_parser_set_location_of_dummy_end(p, &(Current))
377#define RUBY_SET_YYLLOC_OF_NONE(Current) \
378 rb_parser_set_location_of_none(p, &(Current))
379#define RUBY_SET_YYLLOC(Current) \
380 rb_parser_set_location(p, &(Current))
381#define RUBY_INIT_YYLLOC() \
382 { \
383 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
384 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
385 }
386
387#define IS_lex_state_for(x, ls) ((x) & (ls))
388#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
389#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
390#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
391
392# define SET_LEX_STATE(ls) \
393 parser_set_lex_state(p, ls, __LINE__)
394static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
395
396typedef VALUE stack_type;
397
398static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
399
400# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
401# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
402# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
403# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
404# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
405
406/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
407 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
408#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
409#define COND_POP() BITSTACK_POP(cond_stack)
410#define COND_P() BITSTACK_SET_P(cond_stack)
411#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
412
413/* A flag to identify keyword_do_block; "do" keyword after command_call.
414 Example: `foo 1, 2 do`. */
415#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
416#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
417#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
418#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
419
420struct vtable {
421 ID *tbl;
422 int pos;
423 int capa;
424 struct vtable *prev;
425};
426
427struct local_vars {
428 struct vtable *args;
429 struct vtable *vars;
430 struct vtable *used;
431# if WARN_PAST_SCOPE
432 struct vtable *past;
433# endif
434 struct local_vars *prev;
435 struct {
436 NODE *outer, *inner, *current;
437 } numparam;
438 NODE *it;
439};
440
441typedef struct rb_locations_lambda_body_t {
442 NODE *node;
443 YYLTYPE opening_loc;
444 YYLTYPE closing_loc;
445} rb_locations_lambda_body_t;
446
447enum {
448 ORDINAL_PARAM = -1,
449 NO_PARAM = 0,
450 NUMPARAM_MAX = 9,
451};
452
453#define DVARS_INHERIT ((void*)1)
454#define DVARS_TOPSCOPE NULL
455#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
456
457typedef struct token_info {
458 const char *token;
459 rb_code_position_t beg;
460 int indent;
461 int nonspc;
462 struct token_info *next;
463} token_info;
464
465typedef struct end_expect_token_locations {
466 const rb_code_position_t *pos;
467 struct end_expect_token_locations *prev;
468} end_expect_token_locations_t;
469
470typedef struct parser_string_buffer_elem {
471 struct parser_string_buffer_elem *next;
472 long len; /* Total length of allocated buf */
473 long used; /* Current usage of buf */
474 rb_parser_string_t *buf[FLEX_ARY_LEN];
475} parser_string_buffer_elem_t;
476
477typedef struct parser_string_buffer {
478 parser_string_buffer_elem_t *head;
479 parser_string_buffer_elem_t *last;
480} parser_string_buffer_t;
481
482#define AFTER_HEREDOC_WITHOUT_TERMINATOR ((rb_parser_string_t *)1)
483
484/*
485 Structure of Lexer Buffer:
486
487 lex.pbeg lex.ptok lex.pcur lex.pend
488 | | | |
489 |------------+------------+------------|
490 |<---------->|
491 token
492*/
493struct parser_params {
494 YYSTYPE *lval;
495 YYLTYPE *yylloc;
496
497 struct {
498 rb_strterm_t *strterm;
499 rb_parser_lex_gets_func *gets;
500 rb_parser_input_data input;
501 parser_string_buffer_t string_buffer;
502 rb_parser_string_t *lastline;
503 rb_parser_string_t *nextline;
504 const char *pbeg;
505 const char *pcur;
506 const char *pend;
507 const char *ptok;
508 enum lex_state_e state;
509 /* track the nest level of any parens "()[]{}" */
510 int paren_nest;
511 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
512 int lpar_beg;
513 /* track the nest level of only braces "{}" */
514 int brace_nest;
515 } lex;
516 stack_type cond_stack;
517 stack_type cmdarg_stack;
518 int tokidx;
519 int toksiz;
520 int heredoc_end;
521 int heredoc_indent;
522 int heredoc_line_indent;
523 char *tokenbuf;
524 struct local_vars *lvtbl;
525 st_table *pvtbl;
526 st_table *pktbl;
527 int line_count;
528 int ruby_sourceline; /* current line no. */
529 const char *ruby_sourcefile; /* current source file */
530 VALUE ruby_sourcefile_string;
531 rb_encoding *enc;
532 token_info *token_info;
533 st_table *case_labels;
534 rb_node_exits_t *exits;
535
536 VALUE debug_buffer;
537 VALUE debug_output;
538
539 struct {
540 rb_parser_string_t *token;
541 int beg_line;
542 int beg_col;
543 int end_line;
544 int end_col;
545 } delayed;
546
547 rb_ast_t *ast;
548 int node_id;
549
550 st_table *warn_duplicate_keys_table;
551
552 int max_numparam;
553 ID it_id;
554
555 struct lex_context ctxt;
556
557 NODE *eval_tree_begin;
558 NODE *eval_tree;
559 const struct rb_iseq_struct *parent_iseq;
560
561#ifdef UNIVERSAL_PARSER
562 const rb_parser_config_t *config;
563#endif
564 /* compile_option */
565 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
566
567 unsigned int command_start:1;
568 unsigned int eofp: 1;
569 unsigned int ruby__end__seen: 1;
570 unsigned int debug: 1;
571 unsigned int has_shebang: 1;
572 unsigned int token_seen: 1;
573 unsigned int token_info_enabled: 1;
574# if WARN_PAST_SCOPE
575 unsigned int past_scope_enabled: 1;
576# endif
577 unsigned int error_p: 1;
578 unsigned int cr_seen: 1;
579
580#ifndef RIPPER
581 /* Ruby core only */
582
583 unsigned int do_print: 1;
584 unsigned int do_loop: 1;
585 unsigned int do_chomp: 1;
586 unsigned int do_split: 1;
587 unsigned int error_tolerant: 1;
588 unsigned int keep_tokens: 1;
589
590 VALUE error_buffer;
591 rb_parser_ary_t *debug_lines;
592 /*
593 * Store specific keyword locations to generate dummy end token.
594 * Refer to the tail of list element.
595 */
596 end_expect_token_locations_t *end_expect_token_locations;
597 /* id for terms */
598 int token_id;
599 /* Array for term tokens */
600 rb_parser_ary_t *tokens;
601#else
602 /* Ripper only */
603
604 VALUE value;
605 VALUE result;
606 VALUE parsing_thread;
607 VALUE s_value; /* Token VALUE */
608 VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
609 VALUE s_value_stack;
610#endif
611};
612
613#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
614#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
615#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
616static int
617numparam_id_p(struct parser_params *p, ID id)
618{
619 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
620 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
621 return idx > 0 && idx <= NUMPARAM_MAX;
622}
623static void numparam_name(struct parser_params *p, ID id);
624
625#ifdef RIPPER
626static void
627after_shift(struct parser_params *p)
628{
629 if (p->debug) {
630 rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
631 }
632 rb_ary_push(p->s_value_stack, p->s_value);
633 p->s_value = Qnil;
634}
635
636static void
637before_reduce(int len, struct parser_params *p)
638{
639 // Initialize $$ with $1.
640 if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
641}
642
643static void
644after_reduce(int len, struct parser_params *p)
645{
646 for (int i = 0; i < len; i++) {
647 VALUE tos = rb_ary_pop(p->s_value_stack);
648 if (p->debug) {
649 rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos);
650 }
651 }
652 if (p->debug) {
653 rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
654 }
655 rb_ary_push(p->s_value_stack, p->s_lvalue);
656 p->s_lvalue = Qnil;
657}
658
659static void
660after_shift_error_token(struct parser_params *p)
661{
662 if (p->debug) {
663 rb_parser_printf(p, "after-shift-error-token:\n");
664 }
665 rb_ary_push(p->s_value_stack, Qnil);
666}
667
668static void
669after_pop_stack(int len, struct parser_params *p)
670{
671 for (int i = 0; i < len; i++) {
672 VALUE tos = rb_ary_pop(p->s_value_stack);
673 if (p->debug) {
674 rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
675 }
676 }
677}
678#else
679static void
680after_shift(struct parser_params *p)
681{
682}
683
684static void
685before_reduce(int len, struct parser_params *p)
686{
687}
688
689static void
690after_reduce(int len, struct parser_params *p)
691{
692}
693
694static void
695after_shift_error_token(struct parser_params *p)
696{
697}
698
699static void
700after_pop_stack(int len, struct parser_params *p)
701{
702}
703#endif
704
705#define intern_cstr(n,l,en) rb_intern3(n,l,en)
706
707#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
708
709#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
710#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
711#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
712#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
713#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
714#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
715
716#ifndef RIPPER
717static inline int
718char_at_end(struct parser_params *p, VALUE str, int when_empty)
719{
720 long len = RSTRING_LEN(str);
721 return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
722}
723#endif
724
725static void
726pop_pvtbl(struct parser_params *p, st_table *tbl)
727{
728 st_free_table(p->pvtbl);
729 p->pvtbl = tbl;
730}
731
732static void
733pop_pktbl(struct parser_params *p, st_table *tbl)
734{
735 if (p->pktbl) st_free_table(p->pktbl);
736 p->pktbl = tbl;
737}
738
739#define STRING_BUF_DEFAULT_LEN 16
740
741static void
742string_buffer_init(struct parser_params *p)
743{
744 parser_string_buffer_t *buf = &p->lex.string_buffer;
745 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN;
746
747 buf->head = buf->last = xmalloc(size);
748 buf->head->len = STRING_BUF_DEFAULT_LEN;
749 buf->head->used = 0;
750 buf->head->next = NULL;
751}
752
753static void
754string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
755{
756 parser_string_buffer_t *buf = &p->lex.string_buffer;
757
758 if (buf->head->used >= buf->head->len) {
759 parser_string_buffer_elem_t *elem;
760 long n = buf->head->len * 2;
761 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n;
762
763 elem = xmalloc(size);
764 elem->len = n;
765 elem->used = 0;
766 elem->next = NULL;
767 buf->last->next = elem;
768 buf->last = elem;
769 }
770 buf->last->buf[buf->last->used++] = str;
771}
772
773static void
774string_buffer_free(struct parser_params *p)
775{
776 parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
777
778 while (elem) {
779 parser_string_buffer_elem_t *next_elem = elem->next;
780
781 for (long i = 0; i < elem->used; i++) {
782 rb_parser_string_free(p, elem->buf[i]);
783 }
784
785 xfree(elem);
786 elem = next_elem;
787 }
788}
789
790#ifndef RIPPER
791static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
792
793static void
794debug_end_expect_token_locations(struct parser_params *p, const char *name)
795{
796 if(p->debug) {
797 VALUE mesg = rb_sprintf("%s: [", name);
798 int i = 0;
799 for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
800 if (i > 0)
801 rb_str_cat_cstr(mesg, ", ");
802 rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
803 i++;
804 }
805 rb_str_cat_cstr(mesg, "]\n");
806 flush_debug_buffer(p, p->debug_output, mesg);
807 }
808}
809
810static void
811push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
812{
813 if(!p->error_tolerant) return;
814
815 end_expect_token_locations_t *locations;
816 locations = ALLOC(end_expect_token_locations_t);
817 locations->pos = pos;
818 locations->prev = p->end_expect_token_locations;
819 p->end_expect_token_locations = locations;
820
821 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
822}
823
824static void
825pop_end_expect_token_locations(struct parser_params *p)
826{
827 if(!p->end_expect_token_locations) return;
828
829 end_expect_token_locations_t *locations = p->end_expect_token_locations->prev;
830 ruby_sized_xfree(p->end_expect_token_locations, sizeof(end_expect_token_locations_t));
831 p->end_expect_token_locations = locations;
832
833 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
834}
835
836static end_expect_token_locations_t *
837peek_end_expect_token_locations(struct parser_params *p)
838{
839 return p->end_expect_token_locations;
840}
841
842static const char *
843parser_token2char(struct parser_params *p, enum yytokentype tok)
844{
845 switch ((int) tok) {
846#define TOKEN2CHAR(tok) case tok: return (#tok);
847#define TOKEN2CHAR2(tok, name) case tok: return (name);
848 TOKEN2CHAR2(' ', "word_sep");
849 TOKEN2CHAR2('!', "!")
850 TOKEN2CHAR2('%', "%");
851 TOKEN2CHAR2('&', "&");
852 TOKEN2CHAR2('*', "*");
853 TOKEN2CHAR2('+', "+");
854 TOKEN2CHAR2('-', "-");
855 TOKEN2CHAR2('/', "/");
856 TOKEN2CHAR2('<', "<");
857 TOKEN2CHAR2('=', "=");
858 TOKEN2CHAR2('>', ">");
859 TOKEN2CHAR2('?', "?");
860 TOKEN2CHAR2('^', "^");
861 TOKEN2CHAR2('|', "|");
862 TOKEN2CHAR2('~', "~");
863 TOKEN2CHAR2(':', ":");
864 TOKEN2CHAR2(',', ",");
865 TOKEN2CHAR2('.', ".");
866 TOKEN2CHAR2(';', ";");
867 TOKEN2CHAR2('`', "`");
868 TOKEN2CHAR2('\n', "nl");
869 TOKEN2CHAR2('{', "\"{\"");
870 TOKEN2CHAR2('}', "\"}\"");
871 TOKEN2CHAR2('[', "\"[\"");
872 TOKEN2CHAR2(']', "\"]\"");
873 TOKEN2CHAR2('(', "\"(\"");
874 TOKEN2CHAR2(')', "\")\"");
875 TOKEN2CHAR2('\\', "backslash");
876 TOKEN2CHAR(keyword_class);
877 TOKEN2CHAR(keyword_module);
878 TOKEN2CHAR(keyword_def);
879 TOKEN2CHAR(keyword_undef);
880 TOKEN2CHAR(keyword_begin);
881 TOKEN2CHAR(keyword_rescue);
882 TOKEN2CHAR(keyword_ensure);
883 TOKEN2CHAR(keyword_end);
884 TOKEN2CHAR(keyword_if);
885 TOKEN2CHAR(keyword_unless);
886 TOKEN2CHAR(keyword_then);
887 TOKEN2CHAR(keyword_elsif);
888 TOKEN2CHAR(keyword_else);
889 TOKEN2CHAR(keyword_case);
890 TOKEN2CHAR(keyword_when);
891 TOKEN2CHAR(keyword_while);
892 TOKEN2CHAR(keyword_until);
893 TOKEN2CHAR(keyword_for);
894 TOKEN2CHAR(keyword_break);
895 TOKEN2CHAR(keyword_next);
896 TOKEN2CHAR(keyword_redo);
897 TOKEN2CHAR(keyword_retry);
898 TOKEN2CHAR(keyword_in);
899 TOKEN2CHAR(keyword_do);
900 TOKEN2CHAR(keyword_do_cond);
901 TOKEN2CHAR(keyword_do_block);
902 TOKEN2CHAR(keyword_do_LAMBDA);
903 TOKEN2CHAR(keyword_return);
904 TOKEN2CHAR(keyword_yield);
905 TOKEN2CHAR(keyword_super);
906 TOKEN2CHAR(keyword_self);
907 TOKEN2CHAR(keyword_nil);
908 TOKEN2CHAR(keyword_true);
909 TOKEN2CHAR(keyword_false);
910 TOKEN2CHAR(keyword_and);
911 TOKEN2CHAR(keyword_or);
912 TOKEN2CHAR(keyword_not);
913 TOKEN2CHAR(modifier_if);
914 TOKEN2CHAR(modifier_unless);
915 TOKEN2CHAR(modifier_while);
916 TOKEN2CHAR(modifier_until);
917 TOKEN2CHAR(modifier_rescue);
918 TOKEN2CHAR(keyword_alias);
919 TOKEN2CHAR(keyword_defined);
920 TOKEN2CHAR(keyword_BEGIN);
921 TOKEN2CHAR(keyword_END);
922 TOKEN2CHAR(keyword__LINE__);
923 TOKEN2CHAR(keyword__FILE__);
924 TOKEN2CHAR(keyword__ENCODING__);
925 TOKEN2CHAR(tIDENTIFIER);
926 TOKEN2CHAR(tFID);
927 TOKEN2CHAR(tGVAR);
928 TOKEN2CHAR(tIVAR);
929 TOKEN2CHAR(tCONSTANT);
930 TOKEN2CHAR(tCVAR);
931 TOKEN2CHAR(tLABEL);
932 TOKEN2CHAR(tINTEGER);
933 TOKEN2CHAR(tFLOAT);
934 TOKEN2CHAR(tRATIONAL);
935 TOKEN2CHAR(tIMAGINARY);
936 TOKEN2CHAR(tCHAR);
937 TOKEN2CHAR(tNTH_REF);
938 TOKEN2CHAR(tBACK_REF);
939 TOKEN2CHAR(tSTRING_CONTENT);
940 TOKEN2CHAR(tREGEXP_END);
941 TOKEN2CHAR(tDUMNY_END);
942 TOKEN2CHAR(tSP);
943 TOKEN2CHAR(tUPLUS);
944 TOKEN2CHAR(tUMINUS);
945 TOKEN2CHAR(tPOW);
946 TOKEN2CHAR(tCMP);
947 TOKEN2CHAR(tEQ);
948 TOKEN2CHAR(tEQQ);
949 TOKEN2CHAR(tNEQ);
950 TOKEN2CHAR(tGEQ);
951 TOKEN2CHAR(tLEQ);
952 TOKEN2CHAR(tANDOP);
953 TOKEN2CHAR(tOROP);
954 TOKEN2CHAR(tMATCH);
955 TOKEN2CHAR(tNMATCH);
956 TOKEN2CHAR(tDOT2);
957 TOKEN2CHAR(tDOT3);
958 TOKEN2CHAR(tBDOT2);
959 TOKEN2CHAR(tBDOT3);
960 TOKEN2CHAR(tAREF);
961 TOKEN2CHAR(tASET);
962 TOKEN2CHAR(tLSHFT);
963 TOKEN2CHAR(tRSHFT);
964 TOKEN2CHAR(tANDDOT);
965 TOKEN2CHAR(tCOLON2);
966 TOKEN2CHAR(tCOLON3);
967 TOKEN2CHAR(tOP_ASGN);
968 TOKEN2CHAR(tASSOC);
969 TOKEN2CHAR(tLPAREN);
970 TOKEN2CHAR(tLPAREN_ARG);
971 TOKEN2CHAR(tLBRACK);
972 TOKEN2CHAR(tLBRACE);
973 TOKEN2CHAR(tLBRACE_ARG);
974 TOKEN2CHAR(tSTAR);
975 TOKEN2CHAR(tDSTAR);
976 TOKEN2CHAR(tAMPER);
977 TOKEN2CHAR(tLAMBDA);
978 TOKEN2CHAR(tSYMBEG);
979 TOKEN2CHAR(tSTRING_BEG);
980 TOKEN2CHAR(tXSTRING_BEG);
981 TOKEN2CHAR(tREGEXP_BEG);
982 TOKEN2CHAR(tWORDS_BEG);
983 TOKEN2CHAR(tQWORDS_BEG);
984 TOKEN2CHAR(tSYMBOLS_BEG);
985 TOKEN2CHAR(tQSYMBOLS_BEG);
986 TOKEN2CHAR(tSTRING_END);
987 TOKEN2CHAR(tSTRING_DEND);
988 TOKEN2CHAR(tSTRING_DBEG);
989 TOKEN2CHAR(tSTRING_DVAR);
990 TOKEN2CHAR(tLAMBEG);
991 TOKEN2CHAR(tLABEL_END);
992 TOKEN2CHAR(tIGNORED_NL);
993 TOKEN2CHAR(tCOMMENT);
994 TOKEN2CHAR(tEMBDOC_BEG);
995 TOKEN2CHAR(tEMBDOC);
996 TOKEN2CHAR(tEMBDOC_END);
997 TOKEN2CHAR(tHEREDOC_BEG);
998 TOKEN2CHAR(tHEREDOC_END);
999 TOKEN2CHAR(k__END__);
1000 TOKEN2CHAR(tLOWEST);
1001 TOKEN2CHAR(tUMINUS_NUM);
1002 TOKEN2CHAR(tLAST_TOKEN);
1003#undef TOKEN2CHAR
1004#undef TOKEN2CHAR2
1005 }
1006
1007 rb_bug("parser_token2id: unknown token %d", tok);
1008
1009 UNREACHABLE_RETURN(0);
1010}
1011#else
1012static void
1013push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
1014{
1015}
1016
1017static void
1018pop_end_expect_token_locations(struct parser_params *p)
1019{
1020}
1021#endif
1022
1023RBIMPL_ATTR_NONNULL((1, 2, 3))
1024static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
1025RBIMPL_ATTR_NONNULL((1, 2))
1026static int parser_yyerror0(struct parser_params*, const char*);
1027#define yyerror0(msg) parser_yyerror0(p, (msg))
1028#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
1029#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
1030#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
1031#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
1032#define lex_eol_p(p) lex_eol_n_p(p, 0)
1033#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
1034#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
1035#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
1036
1037static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
1038static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
1039static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
1040static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
1041static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
1042
1043#ifdef RIPPER
1044#define compile_for_eval (0)
1045#else
1046#define compile_for_eval (p->parent_iseq != 0)
1047#endif
1048
1049#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
1050
1051#define CALL_Q_P(q) ((q) == tANDDOT)
1052#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
1053
1054#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
1055
1056static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
1057
1058static inline void
1059rb_discard_node(struct parser_params *p, NODE *n)
1060{
1061 rb_ast_delete_node(p->ast, n);
1062}
1063
1064static 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);
1065static 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);
1066static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1067static 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);
1068static 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);
1069static 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);
1070static 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);
1071static 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);
1072static 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);
1073static 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);
1074static 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);
1075static 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);
1076static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1077static 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);
1078static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
1079static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
1080static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1081static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
1082static 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);
1083static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
1084static 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);
1085static 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);
1086static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
1087static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1088static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1089static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1090static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1091static 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);
1092static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1093static 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);
1094static 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);
1095static 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);
1096static 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);
1097static 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);
1098static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1099static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1100static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1101static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1102static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1103static 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);
1104static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
1105static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1106static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1107static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
1108static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1109static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1110static 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);
1111static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1112static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1113static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1114static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1115static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1116static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1117static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1118static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1119static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1120static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1121static rb_node_integer_t * rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc);
1122static rb_node_float_t * rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc);
1123static rb_node_rational_t * rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc);
1124static 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);
1125static rb_node_str_t *rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1126static 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);
1127static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1128static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1129static 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);
1130static 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);
1131static 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);
1132static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1133static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
1134static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc);
1135static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1136static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1137static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
1138static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1139static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1140static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1141static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1142static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1143static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1144static 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);
1145static 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);
1146static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
1147static 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);
1148static 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);
1149static 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);
1150static 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);
1151static 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);
1152static 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);
1153static 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);
1154static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
1155static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
1156static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc);
1157static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
1158static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
1159static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1160static 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);
1161static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1162static 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);
1163static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1164static 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);
1165static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1166static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
1167static 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);
1168static rb_node_line_t *rb_node_line_new(struct parser_params *p, const YYLTYPE *loc);
1169static rb_node_file_t *rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1170static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
1171
1172#define NEW_SCOPE(a,b,c,loc) (NODE *)rb_node_scope_new(p,a,b,c,loc)
1173#define NEW_SCOPE2(t,a,b,c,loc) (NODE *)rb_node_scope_new2(p,t,a,b,c,loc)
1174#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
1175#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)
1176#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)
1177#define NEW_CASE(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case_new(p,h,b,loc,ck_loc,ek_loc)
1178#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc)
1179#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc)
1180#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)
1181#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)
1182#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)
1183#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)
1184#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
1185#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)
1186#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
1187#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
1188#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
1189#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
1190#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc)
1191#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
1192#define NEW_AND(f,s,loc,op_loc) (NODE *)rb_node_and_new(p,f,s,loc,op_loc)
1193#define NEW_OR(f,s,loc,op_loc) (NODE *)rb_node_or_new(p,f,s,loc,op_loc)
1194#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
1195#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
1196#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
1197#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
1198#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
1199#define NEW_CDECL(v,val,path,share,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,share,loc)
1200#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
1201#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)
1202#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)
1203#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
1204#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
1205#define NEW_OP_CDECL(v,op,val,share,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,share,loc)
1206#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
1207#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
1208#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
1209#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
1210#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
1211#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)
1212#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
1213#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
1214#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
1215#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1216#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1217#define NEW_RETURN(s,loc,k_loc) (NODE *)rb_node_return_new(p,s,loc,k_loc)
1218#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)
1219#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1220#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1221#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1222#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1223#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1224#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1225#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1226#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1227#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1228#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1229#define NEW_INTEGER(val, base,loc) (NODE *)rb_node_integer_new(p,val,base,loc)
1230#define NEW_FLOAT(val,loc) (NODE *)rb_node_float_new(p,val,loc)
1231#define NEW_RATIONAL(val,base,seen_point,loc) (NODE *)rb_node_rational_new(p,val,base,seen_point,loc)
1232#define NEW_IMAGINARY(val,base,seen_point,numeric_type,loc) (NODE *)rb_node_imaginary_new(p,val,base,seen_point,numeric_type,loc)
1233#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1234#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1235#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1236#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1237#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1238#define NEW_EVSTR(n,loc,o_loc,c_loc) (NODE *)rb_node_evstr_new(p,n,loc,o_loc,c_loc)
1239#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)
1240#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1241#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1242#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1243#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1244#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1245#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1246#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1247#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1248#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc)
1249#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_loc)
1250#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1251#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1252#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
1253#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
1254#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1255#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)
1256#define NEW_MODULE(n,b,loc,mk_loc,ek_loc) (NODE *)rb_node_module_new(p,n,b,loc,mk_loc,ek_loc)
1257#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)
1258#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc)
1259#define NEW_COLON3(i,loc,d_loc,n_loc) (NODE *)rb_node_colon3_new(p,i,loc,d_loc,n_loc)
1260#define NEW_DOT2(b,e,loc,op_loc) (NODE *)rb_node_dot2_new(p,b,e,loc,op_loc)
1261#define NEW_DOT3(b,e,loc,op_loc) (NODE *)rb_node_dot3_new(p,b,e,loc,op_loc)
1262#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
1263#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
1264#define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc)
1265#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
1266#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
1267#define NEW_DEFINED(e,loc,k_loc) (NODE *)rb_node_defined_new(p,e,loc, k_loc)
1268#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)
1269#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc)
1270#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1271#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1272#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)
1273#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1274#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1275#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1276#define NEW_LINE(loc) (NODE *)rb_node_line_new(p,loc)
1277#define NEW_FILE(str,loc) (NODE *)rb_node_file_new(p,str,loc)
1278#define NEW_ENCODING(loc) (NODE *)rb_node_encoding_new(p,loc)
1279#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1280
1281enum internal_node_type {
1282 NODE_INTERNAL_ONLY = NODE_LAST,
1283 NODE_DEF_TEMP,
1284 NODE_EXITS,
1285 NODE_INTERNAL_LAST
1286};
1287
1288static const char *
1289parser_node_name(int node)
1290{
1291 switch (node) {
1292 case NODE_DEF_TEMP:
1293 return "NODE_DEF_TEMP";
1294 case NODE_EXITS:
1295 return "NODE_EXITS";
1296 default:
1297 return ruby_node_name(node);
1298 }
1299}
1300
1301/* This node is parse.y internal */
1302struct RNode_DEF_TEMP {
1303 NODE node;
1304
1305 /* for NODE_DEFN/NODE_DEFS */
1306
1307 struct RNode *nd_def;
1308 ID nd_mid;
1309
1310 struct {
1311 int max_numparam;
1312 NODE *numparam_save;
1313 struct lex_context ctxt;
1314 } save;
1315};
1316
1317#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1318
1319static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1320static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1321static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1322static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1323static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1324
1325#define NEW_BREAK(s,loc,k_loc) (NODE *)rb_node_break_new(p,s,loc,k_loc)
1326#define NEW_NEXT(s,loc,k_loc) (NODE *)rb_node_next_new(p,s,loc,k_loc)
1327#define NEW_REDO(loc,k_loc) (NODE *)rb_node_redo_new(p,loc,k_loc)
1328#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1329
1330/* Make a new internal node, which should not be appeared in the
1331 * result AST and does not have node_id and location. */
1332static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1333#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1334
1335static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1336
1337static int
1338parser_get_node_id(struct parser_params *p)
1339{
1340 int node_id = p->node_id;
1341 p->node_id++;
1342 return node_id;
1343}
1344
1345static void
1346anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1347{
1348 if (id == tANDDOT) {
1349 yyerror1(loc, "&. inside multiple assignment destination");
1350 }
1351}
1352
1353static inline void
1354set_line_body(NODE *body, int line)
1355{
1356 if (!body) return;
1357 switch (nd_type(body)) {
1358 case NODE_RESCUE:
1359 case NODE_ENSURE:
1360 nd_set_line(body, line);
1361 }
1362}
1363
1364static void
1365set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1366{
1367 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1368 nd_set_line(node, beg->end_pos.lineno);
1369}
1370
1371static NODE *
1372last_expr_node(NODE *expr)
1373{
1374 while (expr) {
1375 if (nd_type_p(expr, NODE_BLOCK)) {
1376 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1377 }
1378 else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) {
1379 expr = RNODE_BEGIN(expr)->nd_body;
1380 }
1381 else {
1382 break;
1383 }
1384 }
1385 return expr;
1386}
1387
1388#ifndef RIPPER
1389#define yyparse ruby_yyparse
1390#endif
1391
1392static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1393static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1394#define new_nil(loc) NEW_NIL(loc)
1395static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1396static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1397static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1398static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1399
1400static NODE *newline_node(NODE*);
1401static void fixpos(NODE*,NODE*);
1402
1403static int value_expr_gen(struct parser_params*,NODE*);
1404static void void_expr(struct parser_params*,NODE*);
1405static NODE *remove_begin(NODE*);
1406#define value_expr(node) value_expr_gen(p, (node))
1407static NODE *void_stmts(struct parser_params*,NODE*);
1408static void reduce_nodes(struct parser_params*,NODE**);
1409static void block_dup_check(struct parser_params*,NODE*,NODE*);
1410
1411static NODE *block_append(struct parser_params*,NODE*,NODE*);
1412static NODE *list_append(struct parser_params*,NODE*,NODE*);
1413static NODE *list_concat(NODE*,NODE*);
1414static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1415static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1416static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1417static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1418static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1419static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1420static NODE *str2dstr(struct parser_params*,NODE*);
1421static NODE *evstr2dstr(struct parser_params*,NODE*);
1422static NODE *splat_array(NODE*);
1423static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1424
1425static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1426static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1427static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1428static 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);
1429static 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;}
1430
1431static bool args_info_empty_p(struct rb_args_info *args);
1432static 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*);
1433static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1434static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1435static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1436static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1437static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1438static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1439static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1440
1441static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1442static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1443
1444static NODE* negate_lit(struct parser_params*, NODE*);
1445static void no_blockarg(struct parser_params*,NODE*);
1446static NODE *ret_args(struct parser_params*,NODE*);
1447static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1448static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1449
1450static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1451static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1452
1453static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1454static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1455
1456static VALUE rb_backref_error(struct parser_params*,NODE*);
1457static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1458
1459static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1460static 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);
1461static 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);
1462static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1463static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1464
1465static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1466
1467static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1468static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1469
1470static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1471static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1472
1473static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1474
1475static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *);
1476
1477#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1478
1479static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1480
1481static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1482
1483static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1484
1485static rb_ast_id_table_t *local_tbl(struct parser_params*);
1486
1487static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1488static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1489
1490static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1491static NODE *heredoc_dedent(struct parser_params*,NODE*);
1492
1493static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1494
1495static 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);
1496
1497#ifdef RIPPER
1498#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1499#define set_value(val) (p->s_lvalue = val)
1500static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1501static int id_is_var(struct parser_params *p, ID id);
1502#endif
1503
1504RUBY_SYMBOL_EXPORT_BEGIN
1505VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1506int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1507enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1508VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1509void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1510PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1511YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1512YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1513YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1514YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1515YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1516YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1517void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1518RUBY_SYMBOL_EXPORT_END
1519
1520static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1521static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1522static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1523static VALUE formal_argument_error(struct parser_params*, ID);
1524static ID shadowing_lvar(struct parser_params*,ID);
1525static void new_bv(struct parser_params*,ID);
1526
1527static void local_push(struct parser_params*,int);
1528static void local_pop(struct parser_params*);
1529static void local_var(struct parser_params*, ID);
1530static void arg_var(struct parser_params*, ID);
1531static int local_id(struct parser_params *p, ID id);
1532static int local_id_ref(struct parser_params*, ID, ID **);
1533#define internal_id rb_parser_internal_id
1534ID internal_id(struct parser_params*);
1535static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1536static int check_forwarding_args(struct parser_params*);
1537static void add_forwarding_args(struct parser_params *p);
1538static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1539
1540static const struct vtable *dyna_push(struct parser_params *);
1541static void dyna_pop(struct parser_params*, const struct vtable *);
1542static int dyna_in_block(struct parser_params*);
1543#define dyna_var(p, id) local_var(p, id)
1544static int dvar_defined(struct parser_params*, ID);
1545#define dvar_defined_ref rb_parser_dvar_defined_ref
1546int dvar_defined_ref(struct parser_params*, ID, ID**);
1547static int dvar_curr(struct parser_params*,ID);
1548
1549static int lvar_defined(struct parser_params*, ID);
1550
1551static NODE *numparam_push(struct parser_params *p);
1552static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1553
1554#define METHOD_NOT '!'
1555
1556#define idFWD_REST '*'
1557#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1558#define idFWD_BLOCK '&'
1559#define idFWD_ALL idDot3
1560#define arg_FWD_BLOCK idFWD_BLOCK
1561
1562#define RE_ONIG_OPTION_IGNORECASE 1
1563#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1564#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1565#define RE_OPTION_ONCE (1<<16)
1566#define RE_OPTION_ENCODING_SHIFT 8
1567#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1568#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1569#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1570#define RE_OPTION_MASK 0xff
1571#define RE_OPTION_ARG_ENCODING_NONE 32
1572
1573#define CHECK_LITERAL_WHEN (st_table *)1
1574#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1575
1576#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1577RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1578
1579#define TOKEN2ID(tok) ( \
1580 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1581 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1582 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1583 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1584 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1585 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1586 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1587
1588/****** Ripper *******/
1589
1590#ifdef RIPPER
1591
1592#include "eventids1.h"
1593#include "eventids2.h"
1594
1595extern const struct ripper_parser_ids ripper_parser_ids;
1596
1597static VALUE ripper_dispatch0(struct parser_params*,ID);
1598static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1599static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1600static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1601static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1602static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1603static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1604void ripper_error(struct parser_params *p);
1605
1606#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1607#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1608#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1609#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1610#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1611#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1612#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1613
1614#define yyparse ripper_yyparse
1615
1616static VALUE
1617aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1618{
1619 if (!NIL_P(pre_arg)) {
1620 if (!NIL_P(pre_args)) {
1621 rb_ary_unshift(pre_args, pre_arg);
1622 }
1623 else {
1624 pre_args = rb_ary_new_from_args(1, pre_arg);
1625 }
1626 }
1627 return pre_args;
1628}
1629
1630#define ID2VAL(id) STATIC_ID2SYM(id)
1631#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1632#endif /* RIPPER */
1633
1634#define KWD2EID(t, v) keyword_##t
1635
1636static NODE *
1637new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, NODE *parent, const YYLTYPE *loc)
1638{
1639 body = remove_begin(body);
1640 reduce_nodes(p, &body);
1641 NODE *n = NEW_SCOPE(args, body, parent, loc);
1642 nd_set_line(n, loc->end_pos.lineno);
1643 set_line_body(body, loc->beg_pos.lineno);
1644 return n;
1645}
1646
1647static NODE *
1648rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1649 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1650{
1651 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1652 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1653 loc.beg_pos = arg_loc->beg_pos;
1654 return NEW_RESCUE(arg, rescue, 0, &loc);
1655}
1656
1657static NODE *add_block_exit(struct parser_params *p, NODE *node);
1658static rb_node_exits_t *init_block_exit(struct parser_params *p);
1659static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1660static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1661static void clear_block_exit(struct parser_params *p, bool error);
1662
1663static void
1664next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1665{
1666 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1667}
1668
1669static void
1670restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1671{
1672 /* See: def_name action */
1673 struct lex_context ctxt = temp->save.ctxt;
1674 p->ctxt.in_def = ctxt.in_def;
1675 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1676 p->ctxt.in_rescue = ctxt.in_rescue;
1677 p->max_numparam = temp->save.max_numparam;
1678 numparam_pop(p, temp->save.numparam_save);
1679 clear_block_exit(p, true);
1680}
1681
1682static void
1683endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1684{
1685 if (is_attrset_id(mid)) {
1686 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1687 }
1688 token_info_drop(p, "def", loc->beg_pos);
1689}
1690
1691#define debug_token_line(p, name, line) do { \
1692 if (p->debug) { \
1693 const char *const pcur = p->lex.pcur; \
1694 const char *const ptok = p->lex.ptok; \
1695 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1696 line, p->ruby_sourceline, \
1697 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1698 } \
1699 } while (0)
1700
1701#define begin_definition(k, loc_beg, loc_end) \
1702 do { \
1703 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1704 /* singleton class */ \
1705 p->ctxt.cant_return = !p->ctxt.in_def; \
1706 p->ctxt.in_def = 0; \
1707 } \
1708 else if (p->ctxt.in_def) { \
1709 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1710 yyerror1(&loc, k " definition in method body"); \
1711 } \
1712 else { \
1713 p->ctxt.cant_return = 1; \
1714 } \
1715 local_push(p, 0); \
1716 } while (0)
1717
1718#ifndef RIPPER
1719# define ifndef_ripper(x) (x)
1720# define ifdef_ripper(r,x) (x)
1721#else
1722# define ifndef_ripper(x)
1723# define ifdef_ripper(r,x) (r)
1724#endif
1725
1726# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1727# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1728# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1729# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1730# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1731# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1732# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1733# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1734# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1735# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1736# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1737# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1738# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1739# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1740# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1741# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1742# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1743# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1744# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1745# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1746#ifdef RIPPER
1747extern const ID id_warn, id_warning, id_gets, id_assoc;
1748# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1749# define WARN_S_L(s,l) STR_NEW(s,l)
1750# define WARN_S(s) STR_NEW2(s)
1751# define WARN_I(i) INT2NUM(i)
1752# define WARN_ID(i) rb_id2str(i)
1753# define PRIsWARN PRIsVALUE
1754# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1755# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1756# ifdef HAVE_VA_ARGS_MACRO
1757# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1758# else
1759# define WARN_CALL rb_funcall
1760# endif
1761# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1762# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1763# ifdef HAVE_VA_ARGS_MACRO
1764# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1765# else
1766# define WARNING_CALL rb_funcall
1767# endif
1768# define compile_error ripper_compile_error
1769#else
1770# define WARN_S_L(s,l) s
1771# define WARN_S(s) s
1772# define WARN_I(i) i
1773# define WARN_ID(i) rb_id2name(i)
1774# define PRIsWARN PRIsVALUE
1775# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1776# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1777# define WARN_CALL rb_compile_warn
1778# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1779# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1780# define WARNING_CALL rb_compile_warning
1781PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1782# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1783#endif
1784
1785#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1786
1787static NODE *
1788add_block_exit(struct parser_params *p, NODE *node)
1789{
1790 if (!node) {
1791 compile_error(p, "unexpected null node");
1792 return 0;
1793 }
1794 switch (nd_type(node)) {
1795 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1796 default:
1797 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1798 return node;
1799 }
1800 if (!p->ctxt.in_defined) {
1801 rb_node_exits_t *exits = p->exits;
1802 if (exits) {
1803 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1804 exits->nd_stts = node;
1805 }
1806 }
1807 return node;
1808}
1809
1810static rb_node_exits_t *
1811init_block_exit(struct parser_params *p)
1812{
1813 rb_node_exits_t *old = p->exits;
1814 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1815 exits->nd_chain = 0;
1816 exits->nd_stts = RNODE(exits);
1817 p->exits = exits;
1818 return old;
1819}
1820
1821static rb_node_exits_t *
1822allow_block_exit(struct parser_params *p)
1823{
1824 rb_node_exits_t *exits = p->exits;
1825 p->exits = 0;
1826 return exits;
1827}
1828
1829static void
1830restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1831{
1832 p->exits = exits;
1833}
1834
1835static void
1836clear_block_exit(struct parser_params *p, bool error)
1837{
1838 rb_node_exits_t *exits = p->exits;
1839 if (!exits) return;
1840 if (error) {
1841 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1842 switch (nd_type(e)) {
1843 case NODE_BREAK:
1844 yyerror1(&e->nd_loc, "Invalid break");
1845 break;
1846 case NODE_NEXT:
1847 yyerror1(&e->nd_loc, "Invalid next");
1848 break;
1849 case NODE_REDO:
1850 yyerror1(&e->nd_loc, "Invalid redo");
1851 break;
1852 default:
1853 yyerror1(&e->nd_loc, "unexpected node");
1854 goto end_checks; /* no nd_chain */
1855 }
1856 }
1857 end_checks:;
1858 }
1859 exits->nd_stts = RNODE(exits);
1860 exits->nd_chain = 0;
1861}
1862
1863#define WARN_EOL(tok) \
1864 (looking_at_eol_p(p) ? \
1865 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1866 (void)0)
1867static int looking_at_eol_p(struct parser_params *p);
1868
1869static NODE *
1870get_nd_value(struct parser_params *p, NODE *node)
1871{
1872 switch (nd_type(node)) {
1873 case NODE_GASGN:
1874 return RNODE_GASGN(node)->nd_value;
1875 case NODE_IASGN:
1876 return RNODE_IASGN(node)->nd_value;
1877 case NODE_LASGN:
1878 return RNODE_LASGN(node)->nd_value;
1879 case NODE_DASGN:
1880 return RNODE_DASGN(node)->nd_value;
1881 case NODE_MASGN:
1882 return RNODE_MASGN(node)->nd_value;
1883 case NODE_CVASGN:
1884 return RNODE_CVASGN(node)->nd_value;
1885 case NODE_CDECL:
1886 return RNODE_CDECL(node)->nd_value;
1887 default:
1888 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1889 return 0;
1890 }
1891}
1892
1893static void
1894set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1895{
1896 switch (nd_type(node)) {
1897 case NODE_CDECL:
1898 RNODE_CDECL(node)->nd_value = rhs;
1899 break;
1900 case NODE_GASGN:
1901 RNODE_GASGN(node)->nd_value = rhs;
1902 break;
1903 case NODE_IASGN:
1904 RNODE_IASGN(node)->nd_value = rhs;
1905 break;
1906 case NODE_LASGN:
1907 RNODE_LASGN(node)->nd_value = rhs;
1908 break;
1909 case NODE_DASGN:
1910 RNODE_DASGN(node)->nd_value = rhs;
1911 break;
1912 case NODE_MASGN:
1913 RNODE_MASGN(node)->nd_value = rhs;
1914 break;
1915 case NODE_CVASGN:
1916 RNODE_CVASGN(node)->nd_value = rhs;
1917 break;
1918 default:
1919 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1920 break;
1921 }
1922}
1923
1924static ID
1925get_nd_vid(struct parser_params *p, NODE *node)
1926{
1927 switch (nd_type(node)) {
1928 case NODE_CDECL:
1929 return RNODE_CDECL(node)->nd_vid;
1930 case NODE_GASGN:
1931 return RNODE_GASGN(node)->nd_vid;
1932 case NODE_IASGN:
1933 return RNODE_IASGN(node)->nd_vid;
1934 case NODE_LASGN:
1935 return RNODE_LASGN(node)->nd_vid;
1936 case NODE_DASGN:
1937 return RNODE_DASGN(node)->nd_vid;
1938 case NODE_CVASGN:
1939 return RNODE_CVASGN(node)->nd_vid;
1940 default:
1941 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1942 return 0;
1943 }
1944}
1945
1946static NODE *
1947get_nd_args(struct parser_params *p, NODE *node)
1948{
1949 switch (nd_type(node)) {
1950 case NODE_CALL:
1951 return RNODE_CALL(node)->nd_args;
1952 case NODE_OPCALL:
1953 return RNODE_OPCALL(node)->nd_args;
1954 case NODE_FCALL:
1955 return RNODE_FCALL(node)->nd_args;
1956 case NODE_QCALL:
1957 return RNODE_QCALL(node)->nd_args;
1958 case NODE_SUPER:
1959 return RNODE_SUPER(node)->nd_args;
1960 case NODE_VCALL:
1961 case NODE_ZSUPER:
1962 case NODE_YIELD:
1963 case NODE_RETURN:
1964 case NODE_BREAK:
1965 case NODE_NEXT:
1966 return 0;
1967 default:
1968 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1969 return 0;
1970 }
1971}
1972
1973static st_index_t
1974djb2(const uint8_t *str, size_t len)
1975{
1976 st_index_t hash = 5381;
1977
1978 for (size_t i = 0; i < len; i++) {
1979 hash = ((hash << 5) + hash) + str[i];
1980 }
1981
1982 return hash;
1983}
1984
1985static st_index_t
1986parser_memhash(const void *ptr, long len)
1987{
1988 return djb2(ptr, len);
1989}
1990
1991#define PARSER_STRING_PTR(str) (str->ptr)
1992#define PARSER_STRING_LEN(str) (str->len)
1993#define PARSER_STRING_END(str) (&str->ptr[str->len])
1994#define STRING_SIZE(str) ((size_t)str->len + 1)
1995#define STRING_TERM_LEN(str) (1)
1996#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
1997#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
1998 SIZED_REALLOC_N(str->ptr, char, (size_t)total + termlen, STRING_SIZE(str)); \
1999 str->len = total; \
2000} while (0)
2001#define STRING_SET_LEN(str, n) do { \
2002 (str)->len = (n); \
2003} while (0)
2004#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2005 ((ptrvar) = str->ptr, \
2006 (lenvar) = str->len)
2007
2008static inline int
2009parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2010{
2011 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2012}
2013
2014static rb_parser_string_t *
2015rb_parser_string_new(rb_parser_t *p, const char *ptr, long len)
2016{
2017 rb_parser_string_t *str;
2018
2019 if (len < 0) {
2020 rb_bug("negative string size (or size too big): %ld", len);
2021 }
2022
2023 str = xcalloc(1, sizeof(rb_parser_string_t));
2024 str->ptr = xcalloc(len + 1, sizeof(char));
2025
2026 if (ptr) {
2027 memcpy(PARSER_STRING_PTR(str), ptr, len);
2028 }
2029 STRING_SET_LEN(str, len);
2030 STRING_TERM_FILL(str);
2031 return str;
2032}
2033
2034static rb_parser_string_t *
2035rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, long len, rb_encoding *enc)
2036{
2037 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2038 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2039 str->enc = enc;
2040 return str;
2041}
2042
2043#ifndef RIPPER
2044rb_parser_string_t *
2045rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2046{
2047 /* Type check */
2048 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2049 RB_GC_GUARD(str);
2050 return ret;
2051}
2052
2053void
2054rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2055{
2056 if (!str) return;
2057 xfree(PARSER_STRING_PTR(str));
2058 xfree(str);
2059}
2060#endif
2061
2062static st_index_t
2063rb_parser_str_hash(rb_parser_string_t *str)
2064{
2065 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2066}
2067
2068static st_index_t
2069rb_char_p_hash(const char *c)
2070{
2071 return parser_memhash((const void *)c, strlen(c));
2072}
2073
2074static size_t
2075rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2076{
2077 return PARSER_STRING_LEN(str);
2078}
2079
2080#ifndef RIPPER
2081static char *
2082rb_parser_string_end(rb_parser_string_t *str)
2083{
2084 return &str->ptr[str->len];
2085}
2086#endif
2087
2088static void
2089rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2090{
2091 str->enc = enc;
2092}
2093
2094static rb_encoding *
2095rb_parser_str_get_encoding(rb_parser_string_t *str)
2096{
2097 return str->enc;
2098}
2099
2100#ifndef RIPPER
2101static bool
2102PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2103{
2104 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2105}
2106#endif
2107
2108static int
2109PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2110{
2111 return str->coderange;
2112}
2113
2114static void
2115PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2116{
2117 str->coderange = coderange;
2118}
2119
2120static void
2121PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2122{
2123 rb_parser_string_set_encoding(str, enc);
2124 PARSER_ENC_CODERANGE_SET(str, cr);
2125}
2126
2127static void
2128PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2129{
2130 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2131}
2132
2133static bool
2134PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2135{
2136 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2137}
2138
2139static bool
2140PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2141{
2142 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2143}
2144
2145static const char *
2146rb_parser_search_nonascii(const char *p, const char *e)
2147{
2148 const char *s = p;
2149
2150 for (; s < e; s++) {
2151 if (*s & 0x80) return s;
2152 }
2153
2154 return NULL;
2155}
2156
2157static int
2158rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc)
2159{
2160 const char *e = ptr + len;
2161
2162 if (enc == rb_ascii8bit_encoding()) {
2163 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2164 ptr = rb_parser_search_nonascii(ptr, e);
2165 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2166 }
2167
2168 /* parser string encoding is always asciicompat */
2169 ptr = rb_parser_search_nonascii(ptr, e);
2170 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2171 for (;;) {
2172 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2173 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2174 ptr += MBCLEN_CHARFOUND_LEN(ret);
2175 if (ptr == e) break;
2176 ptr = rb_parser_search_nonascii(ptr, e);
2177 if (!ptr) break;
2178 }
2179
2180 return RB_PARSER_ENC_CODERANGE_VALID;
2181}
2182
2183static int
2184rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2185{
2186 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2187}
2188
2189static int
2190rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2191{
2192 int cr = PARSER_ENC_CODERANGE(str);
2193
2194 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2195 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2196 PARSER_ENC_CODERANGE_SET(str, cr);
2197 }
2198
2199 return cr;
2200}
2201
2202static rb_parser_string_t *
2203rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2204{
2205 if (rb_parser_str_get_encoding(str) == enc)
2206 return str;
2207 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2208 PARSER_ENC_CODERANGE_CLEAR(str);
2209 }
2210 rb_parser_string_set_encoding(str, enc);
2211 return str;
2212}
2213
2214static bool
2215rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2216{
2217 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2218}
2219
2220static rb_encoding *
2221rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2222{
2223 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2224 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2225
2226 if (enc1 == NULL || enc2 == NULL)
2227 return 0;
2228
2229 if (enc1 == enc2) {
2230 return enc1;
2231 }
2232
2233 if (PARSER_STRING_LEN(str2) == 0)
2234 return enc1;
2235 if (PARSER_STRING_LEN(str1) == 0)
2236 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2237
2238 int cr1, cr2;
2239
2240 cr1 = rb_parser_enc_str_coderange(p, str1);
2241 cr2 = rb_parser_enc_str_coderange(p, str2);
2242
2243 if (cr1 != cr2) {
2244 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2245 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2246 }
2247
2248 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2249 return enc1;
2250 }
2251
2252 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2253 return enc2;
2254 }
2255
2256 return 0;
2257}
2258
2259static void
2260rb_parser_str_modify(rb_parser_string_t *str)
2261{
2262 PARSER_ENC_CODERANGE_CLEAR(str);
2263}
2264
2265static void
2266rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len)
2267{
2268 long capa;
2269 const int termlen = STRING_TERM_LEN(str);
2270
2271 if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2272 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
2273 }
2274
2275 int cr = PARSER_ENC_CODERANGE(str);
2276 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2277 /* Leave unknown. */
2278 }
2279 else if (len > PARSER_STRING_LEN(str)) {
2280 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2281 }
2282 else if (len < PARSER_STRING_LEN(str)) {
2283 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2284 /* ASCII-only string is keeping after truncated. Valid
2285 * and broken may be invalid or valid, leave unknown. */
2286 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2287 }
2288 }
2289
2290 STRING_SET_LEN(str, len);
2291 STRING_TERM_FILL(str);
2292}
2293
2294static rb_parser_string_t *
2295rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len)
2296{
2297 rb_parser_str_modify(str);
2298 if (len == 0) return 0;
2299
2300 long total, olen, off = -1;
2301 char *sptr;
2302 const int termlen = STRING_TERM_LEN(str);
2303
2304 PARSER_STRING_GETMEM(str, sptr, olen);
2305 if (ptr >= sptr && ptr <= sptr + olen) {
2306 off = ptr - sptr;
2307 }
2308
2309 if (olen > LONG_MAX - len) {
2310 compile_error(p, "string sizes too big");
2311 return 0;
2312 }
2313 total = olen + len;
2314 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2315 sptr = PARSER_STRING_PTR(str);
2316 if (off != -1) {
2317 ptr = sptr + off;
2318 }
2319 memcpy(sptr + olen, ptr, len);
2320 STRING_SET_LEN(str, total);
2321 STRING_TERM_FILL(str);
2322
2323 return str;
2324}
2325
2326#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2327#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2328
2329static rb_parser_string_t *
2330rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2331 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2332{
2333 int str_cr, res_cr;
2334 rb_encoding *str_enc, *res_enc;
2335
2336 str_enc = rb_parser_str_get_encoding(str);
2337 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2338
2339 if (str_enc == ptr_enc) {
2340 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2341 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2342 }
2343 }
2344 else {
2345 /* parser string encoding is always asciicompat */
2346 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2347 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2348 }
2349 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2350 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2351 str_cr = rb_parser_enc_str_coderange(p, str);
2352 }
2353 }
2354 }
2355 if (ptr_cr_ret)
2356 *ptr_cr_ret = ptr_cr;
2357
2358 if (str_enc != ptr_enc &&
2359 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2360 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2361 goto incompatible;
2362 }
2363
2364 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2365 res_enc = str_enc;
2366 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2367 }
2368 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2369 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2370 res_enc = str_enc;
2371 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2372 }
2373 else {
2374 res_enc = ptr_enc;
2375 res_cr = ptr_cr;
2376 }
2377 }
2378 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2379 res_enc = str_enc;
2380 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2381 res_cr = str_cr;
2382 else
2383 res_cr = ptr_cr;
2384 }
2385 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2386 res_enc = str_enc;
2387 res_cr = str_cr;
2388 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2389 }
2390
2391 if (len < 0) {
2392 compile_error(p, "negative string size (or size too big)");
2393 }
2394 parser_str_cat(str, ptr, len);
2395 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2396 return str;
2397
2398 incompatible:
2399 compile_error(p, "incompatible character encodings: %s and %s",
2400 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2401 UNREACHABLE_RETURN(0);
2402
2403}
2404
2405static rb_parser_string_t *
2406rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2407 rb_encoding *ptr_enc)
2408{
2409 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2410}
2411
2412static rb_parser_string_t *
2413rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2414{
2415 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2416
2417 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2418 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2419
2420 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2421
2422 return str;
2423}
2424
2425static rb_parser_string_t *
2426rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len)
2427{
2428 if (len < 0) {
2429 rb_bug("negative string size (or size too big)");
2430 }
2431
2432 long slen = PARSER_STRING_LEN(str);
2433
2434 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2435 PARSER_ENC_CODERANGE_CLEAR(str);
2436 }
2437
2438 {
2439 long capa;
2440 const int termlen = STRING_TERM_LEN(str);
2441
2442 if ((capa = slen) < len) {
2443 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2444 }
2445 else if (len == slen) return str;
2446 STRING_SET_LEN(str, len);
2447 STRING_TERM_FILL(str);
2448 }
2449 return str;
2450}
2451
2452# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2453 ((ptrvar) = str->ptr, \
2454 (lenvar) = str->len, \
2455 (encvar) = str->enc)
2456
2457static int
2458rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2459{
2460 long len1, len2;
2461 const char *ptr1, *ptr2;
2462 rb_encoding *enc1, *enc2;
2463
2464 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2465 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2466
2467 return (len1 != len2 ||
2468 enc1 != enc2 ||
2469 memcmp(ptr1, ptr2, len1) != 0);
2470}
2471
2472static void
2473rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2474{
2475 long i;
2476 if (ary->capa < len) {
2477 ary->capa = len;
2478 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2479 for (i = ary->len; i < len; i++) {
2480 ary->data[i] = 0;
2481 }
2482 }
2483}
2484
2485/*
2486 * Do not call this directly.
2487 * Use rb_parser_ary_new_capa_for_XXX() instead.
2488 */
2489static rb_parser_ary_t *
2490parser_ary_new_capa(rb_parser_t *p, long len)
2491{
2492 if (len < 0) {
2493 rb_bug("negative array size (or size too big): %ld", len);
2494 }
2495 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2496 ary->data_type = 0;
2497 ary->len = 0;
2498 ary->capa = len;
2499 if (0 < len) {
2500 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2501 }
2502 else {
2503 ary->data = NULL;
2504 }
2505 return ary;
2506}
2507
2508#ifndef RIPPER
2509static rb_parser_ary_t *
2510rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2511{
2512 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2513 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2514 return ary;
2515}
2516
2517static rb_parser_ary_t *
2518rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2519{
2520 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2521 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2522 return ary;
2523}
2524#endif
2525
2526static rb_parser_ary_t *
2527rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2528{
2529 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2530 ary->data_type = PARSER_ARY_DATA_NODE;
2531 return ary;
2532}
2533
2534/*
2535 * Do not call this directly.
2536 * Use rb_parser_ary_push_XXX() instead.
2537 */
2538static rb_parser_ary_t *
2539parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2540{
2541 if (ary->len == ary->capa) {
2542 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2543 }
2544 ary->data[ary->len++] = val;
2545 return ary;
2546}
2547
2548#ifndef RIPPER
2549static rb_parser_ary_t *
2550rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2551{
2552 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2553 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2554 }
2555 return parser_ary_push(p, ary, val);
2556}
2557
2558static rb_parser_ary_t *
2559rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2560{
2561 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2562 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2563 }
2564 return parser_ary_push(p, ary, val);
2565}
2566#endif
2567
2568static rb_parser_ary_t *
2569rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2570{
2571 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2572 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2573 }
2574 return parser_ary_push(p, ary, val);
2575}
2576
2577#ifndef RIPPER
2578static void
2579rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2580{
2581 if (!token) return;
2582 rb_parser_string_free(p, token->str);
2583 xfree(token);
2584}
2585
2586static void
2587rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2588{
2589# define foreach_ary(ptr) \
2590 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2591 ptr < end_ary_data; ptr++)
2592 switch (ary->data_type) {
2593 case PARSER_ARY_DATA_AST_TOKEN:
2594 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2595 break;
2596 case PARSER_ARY_DATA_SCRIPT_LINE:
2597 foreach_ary(data) {rb_parser_string_free(p, *data);}
2598 break;
2599 case PARSER_ARY_DATA_NODE:
2600 /* Do nothing because nodes are freed when rb_ast_t is freed */
2601 break;
2602 default:
2603 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2604 break;
2605 }
2606# undef foreach_ary
2607 xfree(ary->data);
2608 xfree(ary);
2609}
2610
2611#endif /* !RIPPER */
2612%}
2613
2614%expect 0
2615%define api.pure
2616%define parse.error verbose
2617%printer {
2618 if ((NODE *)$$ == (NODE *)-1) {
2619 rb_parser_printf(p, "NODE_SPECIAL");
2620 }
2621 else if ($$) {
2622 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2623 }
2624} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2625 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2626%printer {
2627 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2628} <id>
2629%printer {
2630 switch (nd_type(RNODE($$))) {
2631 case NODE_INTEGER:
2632 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2633 break;
2634 case NODE_FLOAT:
2635 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2636 break;
2637 case NODE_RATIONAL:
2638 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2639 break;
2640 case NODE_IMAGINARY:
2641 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2642 break;
2643 default:
2644 break;
2645 }
2646} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2647%printer {
2648 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2649} tNTH_REF
2650%printer {
2651 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2652} tBACK_REF
2653
2654%destructor {
2655 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2656} <labels>
2657
2658%lex-param {struct parser_params *p}
2659%parse-param {struct parser_params *p}
2660%initial-action
2661{
2662 RUBY_SET_YYLLOC_OF_NONE(@$);
2663};
2664%after-shift after_shift
2665%before-reduce before_reduce
2666%after-reduce after_reduce
2667%after-shift-error-token after_shift_error_token
2668%after-pop-stack after_pop_stack
2669
2670%union {
2671 NODE *node;
2672 rb_node_fcall_t *node_fcall;
2673 rb_node_args_t *node_args;
2674 rb_node_args_aux_t *node_args_aux;
2675 rb_node_opt_arg_t *node_opt_arg;
2676 rb_node_kw_arg_t *node_kw_arg;
2677 rb_node_block_pass_t *node_block_pass;
2678 rb_node_masgn_t *node_masgn;
2679 rb_node_def_temp_t *node_def_temp;
2680 rb_node_exits_t *node_exits;
2681 struct rb_locations_lambda_body_t *locations_lambda_body;
2682 ID id;
2683 int num;
2684 st_table *tbl;
2685 st_table *labels;
2686 const struct vtable *vars;
2687 struct rb_strterm_struct *strterm;
2688 struct lex_context ctxt;
2689 enum lex_state_e state;
2690}
2691
2692%token <id>
2693 keyword_class "'class'"
2694 keyword_module "'module'"
2695 keyword_def "'def'"
2696 keyword_undef "'undef'"
2697 keyword_begin "'begin'"
2698 keyword_rescue "'rescue'"
2699 keyword_ensure "'ensure'"
2700 keyword_end "'end'"
2701 keyword_if "'if'"
2702 keyword_unless "'unless'"
2703 keyword_then "'then'"
2704 keyword_elsif "'elsif'"
2705 keyword_else "'else'"
2706 keyword_case "'case'"
2707 keyword_when "'when'"
2708 keyword_while "'while'"
2709 keyword_until "'until'"
2710 keyword_for "'for'"
2711 keyword_break "'break'"
2712 keyword_next "'next'"
2713 keyword_redo "'redo'"
2714 keyword_retry "'retry'"
2715 keyword_in "'in'"
2716 keyword_do "'do'"
2717 keyword_do_cond "'do' for condition"
2718 keyword_do_block "'do' for block"
2719 keyword_do_LAMBDA "'do' for lambda"
2720 keyword_return "'return'"
2721 keyword_yield "'yield'"
2722 keyword_super "'super'"
2723 keyword_self "'self'"
2724 keyword_nil "'nil'"
2725 keyword_true "'true'"
2726 keyword_false "'false'"
2727 keyword_and "'and'"
2728 keyword_or "'or'"
2729 keyword_not "'not'"
2730 modifier_if "'if' modifier"
2731 modifier_unless "'unless' modifier"
2732 modifier_while "'while' modifier"
2733 modifier_until "'until' modifier"
2734 modifier_rescue "'rescue' modifier"
2735 keyword_alias "'alias'"
2736 keyword_defined "'defined?'"
2737 keyword_BEGIN "'BEGIN'"
2738 keyword_END "'END'"
2739 keyword__LINE__ "'__LINE__'"
2740 keyword__FILE__ "'__FILE__'"
2741 keyword__ENCODING__ "'__ENCODING__'"
2742
2743%token <id> tIDENTIFIER "local variable or method"
2744%token <id> tFID "method"
2745%token <id> tGVAR "global variable"
2746%token <id> tIVAR "instance variable"
2747%token <id> tCONSTANT "constant"
2748%token <id> tCVAR "class variable"
2749%token <id> tLABEL "label"
2750%token <node> tINTEGER "integer literal"
2751%token <node> tFLOAT "float literal"
2752%token <node> tRATIONAL "rational literal"
2753%token <node> tIMAGINARY "imaginary literal"
2754%token <node> tCHAR "char literal"
2755%token <node> tNTH_REF "numbered reference"
2756%token <node> tBACK_REF "back reference"
2757%token <node> tSTRING_CONTENT "literal content"
2758%token <num> tREGEXP_END
2759%token <num> tDUMNY_END "dummy end"
2760
2761%type <node> singleton singleton_expr strings string string1 xstring regexp
2762%type <node> string_contents xstring_contents regexp_contents string_content
2763%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2764%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2765%type <node_def_temp> defn_head defs_head k_def
2766%type <node_exits> block_open k_while k_until k_for allow_exits
2767%type <node> top_stmts top_stmt begin_block endless_arg endless_command
2768%type <node> bodystmt stmts stmt_or_begin stmt expr arg ternary primary
2769%type <node> command command_call command_call_value method_call
2770%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2771%type <node_fcall> fcall
2772%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2773%type <node> args arg_splat call_args opt_call_args
2774%type <node> paren_args opt_paren_args
2775%type <node_args> args_tail block_args_tail
2776%type <node> command_args aref_args
2777%type <node_block_pass> opt_block_arg block_arg
2778%type <node> var_ref var_lhs
2779%type <node> command_rhs arg_rhs
2780%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2781%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args
2782%type <node_args_aux> f_arg f_arg_item
2783%type <node> f_marg f_rest_marg
2784%type <node_masgn> f_margs
2785%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2786%type <node_args> block_param opt_block_param_def block_param_def opt_block_param
2787%type <id> do bv_decls opt_bv_decl bvar
2788%type <node> lambda brace_body do_body
2789%type <locations_lambda_body> lambda_body
2790%type <node_args> f_larglist
2791%type <node> brace_block cmd_brace_block do_block lhs none fitem
2792%type <node> mlhs_head mlhs_item mlhs_node
2793%type <node_masgn> mlhs mlhs_basic mlhs_inner
2794%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2795%type <node> p_expr p_as p_alt p_expr_basic p_find
2796%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2797%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2798%type <node> p_kwargs p_kwarg p_kw
2799%type <id> keyword_variable user_variable sym operation2 operation3
2800%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
2801%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2802%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2803%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2804%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2805%type <ctxt> p_in_kwarg
2806%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2807%type <num> max_numparam
2808%type <node> numparam
2809%type <id> it_id
2810%token END_OF_INPUT 0 "end-of-input"
2811%token <id> '.'
2812
2813/* escaped chars, should be ignored otherwise */
2814%token <id> '\\' "backslash"
2815%token tSP "escaped space"
2816%token <id> '\t' "escaped horizontal tab"
2817%token <id> '\f' "escaped form feed"
2818%token <id> '\r' "escaped carriage return"
2819%token <id> '\13' "escaped vertical tab"
2820%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2821%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2822%token tPOW RUBY_TOKEN(POW) "**"
2823%token tCMP RUBY_TOKEN(CMP) "<=>"
2824%token tEQ RUBY_TOKEN(EQ) "=="
2825%token tEQQ RUBY_TOKEN(EQQ) "==="
2826%token tNEQ RUBY_TOKEN(NEQ) "!="
2827%token tGEQ RUBY_TOKEN(GEQ) ">="
2828%token tLEQ RUBY_TOKEN(LEQ) "<="
2829%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2830%token tOROP RUBY_TOKEN(OROP) "||"
2831%token tMATCH RUBY_TOKEN(MATCH) "=~"
2832%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2833%token tDOT2 RUBY_TOKEN(DOT2) ".."
2834%token tDOT3 RUBY_TOKEN(DOT3) "..."
2835%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2836%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2837%token tAREF RUBY_TOKEN(AREF) "[]"
2838%token tASET RUBY_TOKEN(ASET) "[]="
2839%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2840%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2841%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2842%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2843%token tCOLON3 ":: at EXPR_BEG"
2844%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2845%token tASSOC "=>"
2846%token tLPAREN "("
2847%token tLPAREN_ARG "( arg"
2848%token tLBRACK "["
2849%token tLBRACE "{"
2850%token tLBRACE_ARG "{ arg"
2851%token tSTAR "*"
2852%token tDSTAR "**arg"
2853%token tAMPER "&"
2854%token <num> tLAMBDA "->"
2855%token tSYMBEG "symbol literal"
2856%token tSTRING_BEG "string literal"
2857%token tXSTRING_BEG "backtick literal"
2858%token tREGEXP_BEG "regexp literal"
2859%token tWORDS_BEG "word list"
2860%token tQWORDS_BEG "verbatim word list"
2861%token tSYMBOLS_BEG "symbol list"
2862%token tQSYMBOLS_BEG "verbatim symbol list"
2863%token tSTRING_END "terminator"
2864%token tSTRING_DEND "'}'"
2865%token <state> tSTRING_DBEG "'#{'"
2866%token tSTRING_DVAR tLAMBEG tLABEL_END
2867
2868%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2869%token tHEREDOC_BEG tHEREDOC_END k__END__
2870
2871/*
2872 * precedence table
2873 */
2874
2875%nonassoc tLOWEST
2876%nonassoc tLBRACE_ARG
2877
2878%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2879%left keyword_or keyword_and
2880%right keyword_not
2881%nonassoc keyword_defined
2882%right '=' tOP_ASGN
2883%left modifier_rescue
2884%right '?' ':'
2885%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2886%left tOROP
2887%left tANDOP
2888%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2889%left '>' tGEQ '<' tLEQ
2890%left '|' '^'
2891%left '&'
2892%left tLSHFT tRSHFT
2893%left '+' '-'
2894%left '*' '/' '%'
2895%right tUMINUS_NUM tUMINUS
2896%right tPOW
2897%right '!' '~' tUPLUS
2898
2899%token tLAST_TOKEN
2900
2901/*
2902 * inlining rules
2903 */
2904%rule %inline ident_or_const
2905 : tIDENTIFIER
2906 | tCONSTANT
2907 ;
2908
2909%rule %inline user_or_keyword_variable
2910 : user_variable
2911 | keyword_variable
2912 ;
2913
2914/*
2915 * parameterizing rules
2916 */
2917%rule asgn(rhs) <node>
2918 : lhs '=' lex_ctxt rhs
2919 {
2920 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2921 /*% ripper: assign!($:1, $:4) %*/
2922 }
2923 ;
2924
2925%rule args_tail_basic(value) <node_args>
2926 : f_kwarg(value) ',' f_kwrest opt_f_block_arg
2927 {
2928 $$ = new_args_tail(p, $1, $3, $4, &@3);
2929 /*% ripper: [$:1, $:3, $:4] %*/
2930 }
2931 | f_kwarg(value) opt_f_block_arg
2932 {
2933 $$ = new_args_tail(p, $1, 0, $2, &@1);
2934 /*% ripper: [$:1, Qnil, $:2] %*/
2935 }
2936 | f_any_kwrest opt_f_block_arg
2937 {
2938 $$ = new_args_tail(p, 0, $1, $2, &@1);
2939 /*% ripper: [Qnil, $:1, $:2] %*/
2940 }
2941 | f_block_arg
2942 {
2943 $$ = new_args_tail(p, 0, 0, $1, &@1);
2944 /*% ripper: [Qnil, Qnil, $:1] %*/
2945 }
2946
2947%rule def_endless_method(bodystmt) <node>
2948 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2949 {
2950 endless_method_name(p, $head->nd_mid, &@head);
2951 restore_defun(p, $head);
2952 ($$ = $head->nd_def)->nd_loc = @$;
2953 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2954 RNODE_DEFN($$)->nd_defn = $bodystmt;
2955 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2956 /*% ripper: def!($:head, $:args, $:$) %*/
2957 local_pop(p);
2958 }
2959 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2960 {
2961 endless_method_name(p, $head->nd_mid, &@head);
2962 restore_defun(p, $head);
2963 ($$ = $head->nd_def)->nd_loc = @$;
2964 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2965 RNODE_DEFS($$)->nd_defn = $bodystmt;
2966 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2967 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2968 local_pop(p);
2969 }
2970 ;
2971
2972%rule compstmt(stmts) <node>
2973 : stmts terms?
2974 {
2975 void_stmts(p, $$ = $stmts);
2976 }
2977 ;
2978
2979%rule f_opt(value) <node_opt_arg>
2980 : f_arg_asgn f_eq value
2981 {
2982 p->ctxt.in_argdef = 1;
2983 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
2984 /*% ripper: [$:$, $:3] %*/
2985 }
2986 ;
2987
2988%rule f_opt_arg(value) <node_opt_arg>
2989 : f_opt(value)
2990 {
2991 $$ = $f_opt;
2992 /*% ripper: rb_ary_new3(1, $:1) %*/
2993 }
2994 | f_opt_arg(value) ',' f_opt(value)
2995 {
2996 $$ = opt_arg_append($f_opt_arg, $f_opt);
2997 /*% ripper: rb_ary_push($:1, $:3) %*/
2998 }
2999 ;
3000
3001%rule f_kw(value) <node_kw_arg>
3002 : f_label value
3003 {
3004 p->ctxt.in_argdef = 1;
3005 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3006 /*% ripper: [$:$, $:value] %*/
3007 }
3008 | f_label
3009 {
3010 p->ctxt.in_argdef = 1;
3011 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3012 /*% ripper: [$:$, 0] %*/
3013 }
3014 ;
3015
3016%rule f_kwarg(value) <node_kw_arg>
3017 : f_kw(value)
3018 {
3019 $$ = $f_kw;
3020 /*% ripper: rb_ary_new3(1, $:1) %*/
3021 }
3022 | f_kwarg(value) ',' f_kw(value)
3023 {
3024 $$ = kwd_append($f_kwarg, $f_kw);
3025 /*% ripper: rb_ary_push($:1, $:3) %*/
3026 }
3027 ;
3028
3029%rule mlhs(item) <node>
3030 : item
3031 {
3032 $$ = NEW_LIST($1, &@$);
3033 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3034 }
3035 | mlhs(item) ',' item
3036 {
3037 $$ = list_append(p, $1, $3);
3038 /*% ripper: mlhs_add!($:1, $:3) %*/
3039 }
3040 ;
3041
3042%rule op_asgn(rhs) <node>
3043 : var_lhs tOP_ASGN lex_ctxt rhs
3044 {
3045 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3046 /*% ripper: opassign!($:1, $:2, $:4) %*/
3047 }
3048 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3049 {
3050 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3051 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3052 }
3053 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3054 {
3055 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3056 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3057 }
3058 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3059 {
3060 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3061 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3062 }
3063 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3064 {
3065 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3066 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3067 }
3068 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3069 {
3070 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3071 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3072 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3073 }
3074 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
3075 {
3076 YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3077 $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3078 /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
3079 }
3080 | backref tOP_ASGN lex_ctxt rhs
3081 {
3082 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3083 $$ = NEW_ERROR(&@$);
3084 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3085 }
3086 ;
3087
3088%rule opt_args_tail(tail) <node_args>
3089 : ',' tail
3090 {
3091 $$ = $tail;
3092 /*% ripper: $:2 %*/
3093 }
3094 | /* none */
3095 {
3096 $$ = new_args_tail(p, 0, 0, 0, &@0);
3097 /*% ripper: [Qnil, Qnil, Qnil] %*/
3098 }
3099 ;
3100
3101%rule range_expr(range) <node>
3102 : range tDOT2 range
3103 {
3104 value_expr($1);
3105 value_expr($3);
3106 $$ = NEW_DOT2($1, $3, &@$, &@2);
3107 /*% ripper: dot2!($:1, $:3) %*/
3108 }
3109 | range tDOT3 range
3110 {
3111 value_expr($1);
3112 value_expr($3);
3113 $$ = NEW_DOT3($1, $3, &@$, &@2);
3114 /*% ripper: dot3!($:1, $:3) %*/
3115 }
3116 | range tDOT2
3117 {
3118 value_expr($1);
3119 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3120 /*% ripper: dot2!($:1, Qnil) %*/
3121 }
3122 | range tDOT3
3123 {
3124 value_expr($1);
3125 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3126 /*% ripper: dot3!($:1, Qnil) %*/
3127 }
3128 | tBDOT2 range
3129 {
3130 value_expr($2);
3131 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3132 /*% ripper: dot2!(Qnil, $:2) %*/
3133 }
3134 | tBDOT3 range
3135 {
3136 value_expr($2);
3137 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3138 /*% ripper: dot3!(Qnil, $:2) %*/
3139 }
3140 ;
3141
3142%rule value_expr(value) <node>
3143 : value
3144 {
3145 value_expr($1);
3146 $$ = $1;
3147 }
3148 ;
3149
3150%rule words(begin, word_list) <node>
3151 : begin ' '+ word_list tSTRING_END
3152 {
3153 $$ = make_list($word_list, &@$);
3154 /*% ripper: array!($:3) %*/
3155 }
3156 ;
3157
3158%%
3159program : {
3160 SET_LEX_STATE(EXPR_BEG);
3161 local_push(p, ifndef_ripper(1)+0);
3162 /* jumps are possible in the top-level loop. */
3163 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3164 }
3165 compstmt(top_stmts)
3166 {
3167 if ($2 && !compile_for_eval) {
3168 NODE *node = $2;
3169 /* last expression should not be void */
3170 if (nd_type_p(node, NODE_BLOCK)) {
3171 while (RNODE_BLOCK(node)->nd_next) {
3172 node = RNODE_BLOCK(node)->nd_next;
3173 }
3174 node = RNODE_BLOCK(node)->nd_head;
3175 }
3176 node = remove_begin(node);
3177 void_expr(p, node);
3178 }
3179 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$);
3180 /*% ripper[final]: program!($:2) %*/
3181 local_pop(p);
3182 }
3183 ;
3184
3185top_stmts : none
3186 {
3187 $$ = NEW_BEGIN(0, &@$);
3188 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3189 }
3190 | top_stmt
3191 {
3192 $$ = newline_node($1);
3193 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3194 }
3195 | top_stmts terms top_stmt
3196 {
3197 $$ = block_append(p, $1, newline_node($3));
3198 /*% ripper: stmts_add!($:1, $:3) %*/
3199 }
3200 ;
3201
3202top_stmt : stmt
3203 {
3204 clear_block_exit(p, true);
3205 $$ = $1;
3206 }
3207 | keyword_BEGIN begin_block
3208 {
3209 $$ = $2;
3210 /*% ripper: $:2 %*/
3211 }
3212 ;
3213
3214block_open : '{' {$$ = init_block_exit(p);};
3215
3216begin_block : block_open compstmt(top_stmts) '}'
3217 {
3218 restore_block_exit(p, $block_open);
3219 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3220 NEW_BEGIN($2, &@$));
3221 $$ = NEW_BEGIN(0, &@$);
3222 /*% ripper: BEGIN!($:2) %*/
3223 }
3224 ;
3225
3226bodystmt : compstmt(stmts)[body]
3227 lex_ctxt[ctxt]
3228 opt_rescue
3229 k_else
3230 {
3231 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3232 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3233 }
3234 compstmt(stmts)[elsebody]
3235 {
3236 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3237 }
3238 opt_ensure
3239 {
3240 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3241 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3242 }
3243 | compstmt(stmts)[body]
3244 lex_ctxt[ctxt]
3245 opt_rescue
3246 {
3247 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3248 }
3249 opt_ensure
3250 {
3251 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3252 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3253 }
3254 ;
3255
3256stmts : none
3257 {
3258 $$ = NEW_BEGIN(0, &@$);
3259 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3260 }
3261 | stmt_or_begin
3262 {
3263 $$ = newline_node($1);
3264 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3265 }
3266 | stmts terms stmt_or_begin
3267 {
3268 $$ = block_append(p, $1, newline_node($3));
3269 /*% ripper: stmts_add!($:1, $:3) %*/
3270 }
3271 ;
3272
3273stmt_or_begin : stmt
3274 | keyword_BEGIN
3275 {
3276 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3277 }
3278 begin_block
3279 {
3280 $$ = $3;
3281 }
3282 ;
3283
3284allow_exits : {$$ = allow_block_exit(p);};
3285
3286k_END : keyword_END lex_ctxt
3287 {
3288 $$ = $2;
3289 p->ctxt.in_rescue = before_rescue;
3290 /*% ripper: $:2 %*/
3291 };
3292
3293stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3294 {
3295 $$ = NEW_ALIAS($2, $4, &@$, &@1);
3296 /*% ripper: alias!($:2, $:4) %*/
3297 }
3298 | keyword_alias tGVAR tGVAR
3299 {
3300 $$ = NEW_VALIAS($2, $3, &@$, &@1);
3301 /*% ripper: var_alias!($:2, $:3) %*/
3302 }
3303 | keyword_alias tGVAR tBACK_REF
3304 {
3305 char buf[2];
3306 buf[0] = '$';
3307 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
3308 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$, &@1);
3309 /*% ripper: var_alias!($:2, $:3) %*/
3310 }
3311 | keyword_alias tGVAR tNTH_REF
3312 {
3313 static const char mesg[] = "can't make alias for the number variables";
3314 /*%%%*/
3315 yyerror1(&@3, mesg);
3316 /*% %*/
3317 $$ = NEW_ERROR(&@$);
3318 /*% ripper[error]: alias_error!(ERR_MESG(), $:3) %*/
3319 }
3320 | keyword_undef undef_list
3321 {
3322 nd_set_first_loc($2, @1.beg_pos);
3323 RNODE_UNDEF($2)->keyword_loc = @1;
3324 $$ = $2;
3325 /*% ripper: undef!($:2) %*/
3326 }
3327 | stmt modifier_if expr_value
3328 {
3329 $$ = new_if(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3330 fixpos($$, $3);
3331 /*% ripper: if_mod!($:3, $:1) %*/
3332 }
3333 | stmt modifier_unless expr_value
3334 {
3335 $$ = new_unless(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3336 fixpos($$, $3);
3337 /*% ripper: unless_mod!($:3, $:1) %*/
3338 }
3339 | stmt modifier_while expr_value
3340 {
3341 clear_block_exit(p, false);
3342 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3343 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3344 }
3345 else {
3346 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3347 }
3348 /*% ripper: while_mod!($:3, $:1) %*/
3349 }
3350 | stmt modifier_until expr_value
3351 {
3352 clear_block_exit(p, false);
3353 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3354 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3355 }
3356 else {
3357 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3358 }
3359 /*% ripper: until_mod!($:3, $:1) %*/
3360 }
3361 | stmt modifier_rescue after_rescue stmt
3362 {
3363 p->ctxt.in_rescue = $3.in_rescue;
3364 NODE *resq;
3365 YYLTYPE loc = code_loc_gen(&@2, &@4);
3366 resq = NEW_RESBODY(0, 0, remove_begin($4), 0, &loc);
3367 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
3368 /*% ripper: rescue_mod!($:1, $:4) %*/
3369 }
3370 | k_END allow_exits '{' compstmt(stmts) '}'
3371 {
3372 if (p->ctxt.in_def) {
3373 rb_warn0("END in method; use at_exit");
3374 }
3375 restore_block_exit(p, $allow_exits);
3376 p->ctxt = $k_END;
3377 {
3378 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, NULL /* parent */, &@$);
3379 $$ = NEW_POSTEXE(scope, &@$, &@1, &@3, &@5);
3380 RNODE_SCOPE(scope)->nd_parent = $$;
3381 }
3382 /*% ripper: END!($:compstmt) %*/
3383 }
3384 | command_asgn
3385 | mlhs '=' lex_ctxt command_call_value
3386 {
3387 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3388 /*% ripper: massign!($:1, $:4) %*/
3389 }
3390 | asgn(mrhs)
3391 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
3392 after_rescue stmt[resbody]
3393 {
3394 p->ctxt.in_rescue = $3.in_rescue;
3395 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3396 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3397 loc.beg_pos = @mrhs_arg.beg_pos;
3398 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3399 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
3400 /*% ripper: massign!($:1, rescue_mod!($:4, $:7)) %*/
3401 }
3402 | mlhs '=' lex_ctxt mrhs_arg
3403 {
3404 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3405 /*% ripper: massign!($:1, $:4) %*/
3406 }
3407 | expr
3408 | error
3409 {
3410 (void)yynerrs;
3411 $$ = NEW_ERROR(&@$);
3412 }
3413 ;
3414
3415command_asgn : asgn(command_rhs)
3416 | op_asgn(command_rhs)
3417 | def_endless_method(endless_command)
3418 ;
3419
3420endless_command : command
3421 | endless_command modifier_rescue after_rescue arg
3422 {
3423 p->ctxt.in_rescue = $3.in_rescue;
3424 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3425 /*% ripper: rescue_mod!($:1, $:4) %*/
3426 }
3427 | keyword_not '\n'? endless_command
3428 {
3429 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3430 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3431 }
3432 ;
3433
3434command_rhs : command_call_value %prec tOP_ASGN
3435 | command_call_value modifier_rescue after_rescue stmt
3436 {
3437 p->ctxt.in_rescue = $3.in_rescue;
3438 YYLTYPE loc = code_loc_gen(&@2, &@4);
3439 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3440 /*% ripper: rescue_mod!($:1, $:4) %*/
3441 }
3442 | command_asgn
3443 ;
3444
3445expr : command_call
3446 | expr keyword_and expr
3447 {
3448 $$ = logop(p, idAND, $1, $3, &@2, &@$);
3449 /*% ripper: binary!($:1, ID2VAL(idAND), $:3) %*/
3450 }
3451 | expr keyword_or expr
3452 {
3453 $$ = logop(p, idOR, $1, $3, &@2, &@$);
3454 /*% ripper: binary!($:1, ID2VAL(idOR), $:3) %*/
3455 }
3456 | keyword_not '\n'? expr
3457 {
3458 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3459 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3460 }
3461 | '!' command_call
3462 {
3463 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3464 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
3465 }
3466 | arg tASSOC
3467 {
3468 value_expr($arg);
3469 }
3470 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3471 p_top_expr_body[body]
3472 {
3473 pop_pktbl(p, $p_pktbl);
3474 pop_pvtbl(p, $p_pvtbl);
3475 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3476 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@2), &@$, &NULL_LOC, &NULL_LOC);
3477 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3478 }
3479 | arg keyword_in
3480 {
3481 value_expr($arg);
3482 }
3483 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3484 p_top_expr_body[body]
3485 {
3486 pop_pktbl(p, $p_pktbl);
3487 pop_pvtbl(p, $p_pvtbl);
3488 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3489 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body, &@keyword_in, &NULL_LOC, &NULL_LOC), &@$, &NULL_LOC, &NULL_LOC);
3490 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3491 }
3492 | arg %prec tLBRACE_ARG
3493 ;
3494
3495def_name : fname
3496 {
3497 numparam_name(p, $fname);
3498 local_push(p, 0);
3499 p->ctxt.in_def = 1;
3500 p->ctxt.in_rescue = before_rescue;
3501 p->ctxt.cant_return = 0;
3502 $$ = $fname;
3503 }
3504 ;
3505
3506defn_head : k_def def_name
3507 {
3508 $$ = def_head_save(p, $k_def);
3509 $$->nd_mid = $def_name;
3510 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3511 /*% ripper: $:def_name %*/
3512 }
3513 ;
3514
3515defs_head : k_def singleton dot_or_colon
3516 {
3517 SET_LEX_STATE(EXPR_FNAME);
3518 }
3519 def_name
3520 {
3521 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3522 $$ = def_head_save(p, $k_def);
3523 $$->nd_mid = $def_name;
3524 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3525 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3526 }
3527 ;
3528
3529expr_value : value_expr(expr)
3530 | error
3531 {
3532 $$ = NEW_ERROR(&@$);
3533 }
3534 ;
3535
3536expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3537 {
3538 $$ = $2;
3539 /*% ripper: $:2 %*/
3540 }
3541 ;
3542
3543command_call : command
3544 | block_command
3545 ;
3546
3547command_call_value : value_expr(command_call)
3548 ;
3549
3550block_command : block_call
3551 | block_call call_op2 operation2 command_args
3552 {
3553 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3554 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3555 }
3556 ;
3557
3558cmd_brace_block : tLBRACE_ARG brace_body '}'
3559 {
3560 $$ = $2;
3561 set_embraced_location($$, &@1, &@3);
3562 /*% ripper: $:2 %*/
3563 }
3564 ;
3565
3566fcall : operation
3567 {
3568 $$ = NEW_FCALL($1, 0, &@$);
3569 /*% ripper: $:1 %*/
3570 }
3571 ;
3572
3573command : fcall command_args %prec tLOWEST
3574 {
3575 $1->nd_args = $2;
3576 nd_set_last_loc($1, @2.end_pos);
3577 $$ = (NODE *)$1;
3578 /*% ripper: command!($:1, $:2) %*/
3579 }
3580 | fcall command_args cmd_brace_block
3581 {
3582 block_dup_check(p, $2, $3);
3583 $1->nd_args = $2;
3584 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3585 fixpos($$, RNODE($1));
3586 nd_set_last_loc($1, @2.end_pos);
3587 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3588 }
3589 | primary_value call_op operation2 command_args %prec tLOWEST
3590 {
3591 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3592 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3593 }
3594 | primary_value call_op operation2 command_args cmd_brace_block
3595 {
3596 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3597 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3598 }
3599 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3600 {
3601 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3602 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3603 }
3604 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3605 {
3606 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3607 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3608 }
3609 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3610 {
3611 set_embraced_location($5, &@4, &@6);
3612 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3613 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3614 }
3615 | keyword_super command_args
3616 {
3617 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3618 fixpos($$, $2);
3619 /*% ripper: super!($:2) %*/
3620 }
3621 | k_yield command_args
3622 {
3623 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3624 fixpos($$, $2);
3625 /*% ripper: yield!($:2) %*/
3626 }
3627 | k_return call_args
3628 {
3629 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3630 /*% ripper: return!($:2) %*/
3631 }
3632 | keyword_break call_args
3633 {
3634 NODE *args = 0;
3635 args = ret_args(p, $2);
3636 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3637 /*% ripper: break!($:2) %*/
3638 }
3639 | keyword_next call_args
3640 {
3641 NODE *args = 0;
3642 args = ret_args(p, $2);
3643 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3644 /*% ripper: next!($:2) %*/
3645 }
3646 ;
3647
3648mlhs : mlhs_basic
3649 | tLPAREN mlhs_inner rparen
3650 {
3651 $$ = $2;
3652 /*% ripper: mlhs_paren!($:2) %*/
3653 }
3654 ;
3655
3656mlhs_inner : mlhs_basic
3657 | tLPAREN mlhs_inner rparen
3658 {
3659 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3660 /*% ripper: mlhs_paren!($:2) %*/
3661 }
3662 ;
3663
3664mlhs_basic : mlhs_head
3665 {
3666 $$ = NEW_MASGN($1, 0, &@$);
3667 /*% ripper: $:1 %*/
3668 }
3669 | mlhs_head mlhs_item
3670 {
3671 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3672 /*% ripper: mlhs_add!($:1, $:2) %*/
3673 }
3674 | mlhs_head tSTAR mlhs_node
3675 {
3676 $$ = NEW_MASGN($1, $3, &@$);
3677 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3678 }
3679 | mlhs_head tSTAR mlhs_node ',' mlhs(mlhs_item)
3680 {
3681 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3682 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3683 }
3684 | mlhs_head tSTAR
3685 {
3686 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3687 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3688 }
3689 | mlhs_head tSTAR ',' mlhs(mlhs_item)
3690 {
3691 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3692 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3693 }
3694 | tSTAR mlhs_node
3695 {
3696 $$ = NEW_MASGN(0, $2, &@$);
3697 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3698 }
3699 | tSTAR mlhs_node ',' mlhs(mlhs_item)
3700 {
3701 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3702 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3703 }
3704 | tSTAR
3705 {
3706 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3707 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3708 }
3709 | tSTAR ',' mlhs(mlhs_item)
3710 {
3711 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3712 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3713 }
3714 ;
3715
3716mlhs_item : mlhs_node
3717 | tLPAREN mlhs_inner rparen
3718 {
3719 $$ = (NODE *)$2;
3720 /*% ripper: mlhs_paren!($:2) %*/
3721 }
3722 ;
3723
3724mlhs_head : mlhs_item ','
3725 {
3726 $$ = NEW_LIST($1, &@1);
3727 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3728 }
3729 | mlhs_head mlhs_item ','
3730 {
3731 $$ = list_append(p, $1, $2);
3732 /*% ripper: mlhs_add!($:1, $:2) %*/
3733 }
3734 ;
3735
3736
3737mlhs_node : user_or_keyword_variable
3738 {
3739 /*% ripper: var_field!($:1) %*/
3740 $$ = assignable(p, $1, 0, &@$);
3741 }
3742 | primary_value '[' opt_call_args rbracket
3743 {
3744 $$ = aryset(p, $1, $3, &@$);
3745 /*% ripper: aref_field!($:1, $:3) %*/
3746 }
3747 | primary_value call_op ident_or_const
3748 {
3749 anddot_multiple_assignment_check(p, &@2, $2);
3750 $$ = attrset(p, $1, $2, $3, &@$);
3751 /*% ripper: field!($:1, $:2, $:3) %*/
3752 }
3753 | primary_value tCOLON2 tIDENTIFIER
3754 {
3755 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3756 /*% ripper: const_path_field!($:1, $:3) %*/
3757 }
3758 | primary_value tCOLON2 tCONSTANT
3759 {
3760 /*% ripper: const_path_field!($:1, $:3) %*/
3761 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3762 }
3763 | tCOLON3 tCONSTANT
3764 {
3765 /*% ripper: top_const_field!($:2) %*/
3766 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3767 }
3768 | backref
3769 {
3770 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3771 $$ = NEW_ERROR(&@$);
3772 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3773 }
3774 ;
3775
3776lhs : user_or_keyword_variable
3777 {
3778 /*% ripper: var_field!($:1) %*/
3779 $$ = assignable(p, $1, 0, &@$);
3780 }
3781 | primary_value '[' opt_call_args rbracket
3782 {
3783 $$ = aryset(p, $1, $3, &@$);
3784 /*% ripper: aref_field!($:1, $:3) %*/
3785 }
3786 | primary_value call_op ident_or_const
3787 {
3788 $$ = attrset(p, $1, $2, $3, &@$);
3789 /*% ripper: field!($:1, $:2, $:3) %*/
3790 }
3791 | primary_value tCOLON2 tIDENTIFIER
3792 {
3793 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3794 /*% ripper: field!($:1, $:2, $:3) %*/
3795 }
3796 | primary_value tCOLON2 tCONSTANT
3797 {
3798 /*% ripper: const_path_field!($:1, $:3) %*/
3799 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3800 }
3801 | tCOLON3 tCONSTANT
3802 {
3803 /*% ripper: top_const_field!($:2) %*/
3804 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3805 }
3806 | backref
3807 {
3808 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3809 $$ = NEW_ERROR(&@$);
3810 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3811 }
3812 ;
3813
3814cname : tIDENTIFIER
3815 {
3816 static const char mesg[] = "class/module name must be CONSTANT";
3817 /*%%%*/
3818 yyerror1(&@1, mesg);
3819 /*% %*/
3820 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3821 }
3822 | tCONSTANT
3823 ;
3824
3825cpath : tCOLON3 cname
3826 {
3827 $$ = NEW_COLON3($2, &@$, &@1, &@2);
3828 /*% ripper: top_const_ref!($:2) %*/
3829 }
3830 | cname
3831 {
3832 $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
3833 /*% ripper: const_ref!($:1) %*/
3834 }
3835 | primary_value tCOLON2 cname
3836 {
3837 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
3838 /*% ripper: const_path_ref!($:1, $:3) %*/
3839 }
3840 ;
3841
3842fname : operation
3843 | op
3844 {
3845 SET_LEX_STATE(EXPR_ENDFN);
3846 $$ = $1;
3847 }
3848 | reswords
3849 ;
3850
3851fitem : fname
3852 {
3853 $$ = NEW_SYM(rb_id2str($1), &@$);
3854 /*% ripper: symbol_literal!($:1) %*/
3855 }
3856 | symbol
3857 ;
3858
3859undef_list : fitem
3860 {
3861 $$ = NEW_UNDEF($1, &@$);
3862 /*% ripper: rb_ary_new3(1, $:1) %*/
3863 }
3864 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3865 {
3866 nd_set_last_loc($1, @4.end_pos);
3867 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3868 /*% ripper: rb_ary_push($:1, $:4) %*/
3869 }
3870 ;
3871
3872op : '|' { $$ = '|'; }
3873 | '^' { $$ = '^'; }
3874 | '&' { $$ = '&'; }
3875 | tCMP { $$ = tCMP; }
3876 | tEQ { $$ = tEQ; }
3877 | tEQQ { $$ = tEQQ; }
3878 | tMATCH { $$ = tMATCH; }
3879 | tNMATCH { $$ = tNMATCH; }
3880 | '>' { $$ = '>'; }
3881 | tGEQ { $$ = tGEQ; }
3882 | '<' { $$ = '<'; }
3883 | tLEQ { $$ = tLEQ; }
3884 | tNEQ { $$ = tNEQ; }
3885 | tLSHFT { $$ = tLSHFT; }
3886 | tRSHFT { $$ = tRSHFT; }
3887 | '+' { $$ = '+'; }
3888 | '-' { $$ = '-'; }
3889 | '*' { $$ = '*'; }
3890 | tSTAR { $$ = '*'; }
3891 | '/' { $$ = '/'; }
3892 | '%' { $$ = '%'; }
3893 | tPOW { $$ = tPOW; }
3894 | tDSTAR { $$ = tDSTAR; }
3895 | '!' { $$ = '!'; }
3896 | '~' { $$ = '~'; }
3897 | tUPLUS { $$ = tUPLUS; }
3898 | tUMINUS { $$ = tUMINUS; }
3899 | tAREF { $$ = tAREF; }
3900 | tASET { $$ = tASET; }
3901 | '`' { $$ = '`'; }
3902 ;
3903
3904reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3905 | keyword_BEGIN | keyword_END
3906 | keyword_alias | keyword_and | keyword_begin
3907 | keyword_break | keyword_case | keyword_class | keyword_def
3908 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3909 | keyword_end | keyword_ensure | keyword_false
3910 | keyword_for | keyword_in | keyword_module | keyword_next
3911 | keyword_nil | keyword_not | keyword_or | keyword_redo
3912 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3913 | keyword_super | keyword_then | keyword_true | keyword_undef
3914 | keyword_when | keyword_yield | keyword_if | keyword_unless
3915 | keyword_while | keyword_until
3916 ;
3917
3918arg : asgn(arg_rhs)
3919 | op_asgn(arg_rhs)
3920 | range_expr(arg)
3921 | arg '+' arg
3922 {
3923 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3924 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3925 }
3926 | arg '-' arg
3927 {
3928 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3929 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3930 }
3931 | arg '*' arg
3932 {
3933 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3934 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3935 }
3936 | arg '/' arg
3937 {
3938 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3939 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3940 }
3941 | arg '%' arg
3942 {
3943 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3944 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3945 }
3946 | arg tPOW arg
3947 {
3948 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3949 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3950 }
3951 | tUMINUS_NUM simple_numeric tPOW arg
3952 {
3953 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3954 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3955 }
3956 | tUPLUS arg
3957 {
3958 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3959 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3960 }
3961 | tUMINUS arg
3962 {
3963 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3964 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3965 }
3966 | arg '|' arg
3967 {
3968 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3969 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3970 }
3971 | arg '^' arg
3972 {
3973 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3974 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3975 }
3976 | arg '&' arg
3977 {
3978 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3979 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
3980 }
3981 | arg tCMP arg
3982 {
3983 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3984 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
3985 }
3986 | rel_expr %prec tCMP
3987 | arg tEQ arg
3988 {
3989 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3990 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
3991 }
3992 | arg tEQQ arg
3993 {
3994 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
3995 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
3996 }
3997 | arg tNEQ arg
3998 {
3999 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4000 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4001 }
4002 | arg tMATCH arg
4003 {
4004 $$ = match_op(p, $1, $3, &@2, &@$);
4005 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4006 }
4007 | arg tNMATCH arg
4008 {
4009 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4010 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4011 }
4012 | '!' arg
4013 {
4014 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4015 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4016 }
4017 | '~' arg
4018 {
4019 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4020 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4021 }
4022 | arg tLSHFT arg
4023 {
4024 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4025 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4026 }
4027 | arg tRSHFT arg
4028 {
4029 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4030 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4031 }
4032 | arg tANDOP arg
4033 {
4034 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4035 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4036 }
4037 | arg tOROP arg
4038 {
4039 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4040 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4041 }
4042 | keyword_defined '\n'? begin_defined arg
4043 {
4044 p->ctxt.in_defined = $3.in_defined;
4045 $$ = new_defined(p, $4, &@$, &@1);
4046 p->ctxt.has_trailing_semicolon = $3.has_trailing_semicolon;
4047 /*% ripper: defined!($:4) %*/
4048 }
4049 | def_endless_method(endless_arg)
4050 | ternary
4051 | primary
4052 ;
4053
4054ternary : arg '?' arg '\n'? ':' arg
4055 {
4056 value_expr($1);
4057 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4058 fixpos($$, $1);
4059 /*% ripper: ifop!($:1, $:3, $:6) %*/
4060 }
4061 ;
4062
4063endless_arg : arg %prec modifier_rescue
4064 | endless_arg modifier_rescue after_rescue arg
4065 {
4066 p->ctxt.in_rescue = $3.in_rescue;
4067 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4068 /*% ripper: rescue_mod!($:1, $:4) %*/
4069 }
4070 | keyword_not '\n'? endless_arg
4071 {
4072 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4073 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4074 }
4075 ;
4076
4077relop : '>' {$$ = '>';}
4078 | '<' {$$ = '<';}
4079 | tGEQ {$$ = idGE;}
4080 | tLEQ {$$ = idLE;}
4081 ;
4082
4083rel_expr : arg relop arg %prec '>'
4084 {
4085 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4086 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4087 }
4088 | rel_expr relop arg %prec '>'
4089 {
4090 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4091 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4092 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4093 }
4094 ;
4095
4096lex_ctxt : none
4097 {
4098 $$ = p->ctxt;
4099 }
4100 ;
4101
4102begin_defined : lex_ctxt
4103 {
4104 p->ctxt.in_defined = 1;
4105 $$ = $1;
4106 }
4107 ;
4108
4109after_rescue : lex_ctxt
4110 {
4111 p->ctxt.in_rescue = after_rescue;
4112 $$ = $1;
4113 }
4114 ;
4115
4116arg_value : value_expr(arg)
4117 ;
4118
4119aref_args : none
4120 | args trailer
4121 | args ',' assocs trailer
4122 {
4123 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4124 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4125 }
4126 | assocs trailer
4127 {
4128 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4129 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4130 }
4131 ;
4132
4133arg_rhs : arg %prec tOP_ASGN
4134 {
4135 value_expr($1);
4136 $$ = $1;
4137 }
4138 | arg modifier_rescue after_rescue arg
4139 {
4140 p->ctxt.in_rescue = $3.in_rescue;
4141 value_expr($1);
4142 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4143 /*% ripper: rescue_mod!($:1, $:4) %*/
4144 }
4145 ;
4146
4147paren_args : '(' opt_call_args rparen
4148 {
4149 $$ = $2;
4150 /*% ripper: arg_paren!($:2) %*/
4151 }
4152 | '(' args ',' args_forward rparen
4153 {
4154 if (!check_forwarding_args(p)) {
4155 $$ = 0;
4156 }
4157 else {
4158 $$ = new_args_forward_call(p, $2, &@4, &@$);
4159 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4160 }
4161 }
4162 | '(' args_forward rparen
4163 {
4164 if (!check_forwarding_args(p)) {
4165 $$ = 0;
4166 }
4167 else {
4168 $$ = new_args_forward_call(p, 0, &@2, &@$);
4169 /*% ripper: arg_paren!($:2) %*/
4170 }
4171 }
4172 ;
4173
4174opt_paren_args : none
4175 | paren_args
4176 {
4177 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4178 }
4179 ;
4180
4181opt_call_args : none
4182 | call_args
4183 | args ','
4184 | args ',' assocs ','
4185 {
4186 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4187 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4188 }
4189 | assocs ','
4190 {
4191 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4192 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4193 }
4194 ;
4195
4196call_args : value_expr(command)
4197 {
4198 $$ = NEW_LIST($1, &@$);
4199 /*% ripper: args_add!(args_new!, $:1) %*/
4200 }
4201 | def_endless_method(endless_command)
4202 {
4203 $$ = NEW_LIST($1, &@$);
4204 /*% ripper: args_add!(args_new!, $:1) %*/
4205 }
4206 | args opt_block_arg
4207 {
4208 $$ = arg_blk_pass($1, $2);
4209 /*% ripper: args_add_block!($:1, $:2) %*/
4210 }
4211 | assocs opt_block_arg
4212 {
4213 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4214 $$ = arg_blk_pass($$, $2);
4215 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4216 }
4217 | args ',' assocs opt_block_arg
4218 {
4219 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4220 $$ = arg_blk_pass($$, $4);
4221 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4222 }
4223 | block_arg
4224 /*% ripper: args_add_block!(args_new!, $:1) %*/
4225 ;
4226
4227command_args : {
4228 /* If call_args starts with a open paren '(' or '[',
4229 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4230 * but the push must be done after CMDARG_PUSH(1).
4231 * So this code makes them consistent by first cancelling
4232 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4233 * and finally redoing CMDARG_PUSH(0).
4234 */
4235 int lookahead = 0;
4236 switch (yychar) {
4237 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4238 lookahead = 1;
4239 }
4240 if (lookahead) CMDARG_POP();
4241 CMDARG_PUSH(1);
4242 if (lookahead) CMDARG_PUSH(0);
4243 }
4244 call_args
4245 {
4246 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4247 * but the push must be done after CMDARG_POP() in the parser.
4248 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4249 * CMDARG_POP() to pop 1 pushed by command_args,
4250 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4251 */
4252 int lookahead = 0;
4253 switch (yychar) {
4254 case tLBRACE_ARG:
4255 lookahead = 1;
4256 }
4257 if (lookahead) CMDARG_POP();
4258 CMDARG_POP();
4259 if (lookahead) CMDARG_PUSH(0);
4260 $$ = $2;
4261 /*% ripper: $:2 %*/
4262 }
4263 ;
4264
4265block_arg : tAMPER arg_value
4266 {
4267 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4268 /*% ripper: $:2 %*/
4269 }
4270 | tAMPER
4271 {
4272 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4273 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4274 /*% ripper: Qnil %*/
4275 }
4276 ;
4277
4278opt_block_arg : ',' block_arg
4279 {
4280 $$ = $2;
4281 /*% ripper: $:2 %*/
4282 }
4283 | none
4284 {
4285 $$ = 0;
4286 /*% ripper: Qfalse %*/
4287 }
4288 ;
4289
4290/* value */
4291args : arg_value
4292 {
4293 $$ = NEW_LIST($arg_value, &@$);
4294 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4295 }
4296 | arg_splat
4297 {
4298 $$ = $arg_splat;
4299 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4300 }
4301 | args[non_last_args] ',' arg_value
4302 {
4303 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4304 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4305 }
4306 | args[non_last_args] ',' arg_splat
4307 {
4308 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4309 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4310 }
4311 ;
4312
4313/* value */
4314arg_splat : tSTAR arg_value
4315 {
4316 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4317 /*% ripper: $:arg_value %*/
4318 }
4319 | tSTAR /* none */
4320 {
4321 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4322 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4323 /*% ripper: Qnil %*/
4324 }
4325 ;
4326
4327/* value */
4328mrhs_arg : mrhs
4329 | arg_value
4330 ;
4331
4332/* value */
4333mrhs : args ',' arg_value
4334 {
4335 $$ = last_arg_append(p, $args, $arg_value, &@$);
4336 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4337 }
4338 | args ',' tSTAR arg_value
4339 {
4340 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4341 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4342 }
4343 | tSTAR arg_value
4344 {
4345 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4346 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4347 }
4348 ;
4349
4350%rule %inline inline_primary
4351 : literal
4352 | strings
4353 | xstring
4354 | regexp
4355 | words
4356 | qwords
4357 | symbols
4358 | qsymbols
4359 ;
4360
4361primary : inline_primary
4362 | var_ref
4363 | backref
4364 | tFID
4365 {
4366 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
4367 /*% ripper: method_add_arg!(fcall!($:1), args_new!) %*/
4368 }
4369 | k_begin
4370 {
4371 CMDARG_PUSH(0);
4372 }
4373 bodystmt
4374 k_end
4375 {
4376 CMDARG_POP();
4377 set_line_body($3, @1.end_pos.lineno);
4378 $$ = NEW_BEGIN($3, &@$);
4379 nd_set_line($$, @1.end_pos.lineno);
4380 /*% ripper: begin!($:3) %*/
4381 }
4382 | tLPAREN_ARG compstmt(stmts) {SET_LEX_STATE(EXPR_ENDARG);} ')'
4383 {
4384 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4385 $$ = $2;
4386 /*% ripper: paren!($:2) %*/
4387 }
4388 | tLPAREN compstmt(stmts) ')'
4389 {
4390 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4391 $$ = NEW_BLOCK($2, &@$);
4392 /*% ripper: paren!($:2) %*/
4393 }
4394 | primary_value tCOLON2 tCONSTANT
4395 {
4396 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
4397 /*% ripper: const_path_ref!($:1, $:3) %*/
4398 }
4399 | tCOLON3 tCONSTANT
4400 {
4401 $$ = NEW_COLON3($2, &@$, &@1, &@2);
4402 /*% ripper: top_const_ref!($:2) %*/
4403 }
4404 | tLBRACK aref_args ']'
4405 {
4406 $$ = make_list($2, &@$);
4407 /*% ripper: array!($:2) %*/
4408 }
4409 | tLBRACE assoc_list '}'
4410 {
4411 $$ = new_hash(p, $2, &@$);
4412 RNODE_HASH($$)->nd_brace = TRUE;
4413 /*% ripper: hash!($:2) %*/
4414 }
4415 | k_return
4416 {
4417 $$ = NEW_RETURN(0, &@$, &@1);
4418 /*% ripper: return0! %*/
4419 }
4420 | k_yield '(' call_args rparen
4421 {
4422 $$ = NEW_YIELD($3, &@$, &@1, &@2, &@4);
4423 /*% ripper: yield!(paren!($:3)) %*/
4424 }
4425 | k_yield '(' rparen
4426 {
4427 $$ = NEW_YIELD(0, &@$, &@1, &@2, &@3);
4428 /*% ripper: yield!(paren!(args_new!)) %*/
4429 }
4430 | k_yield
4431 {
4432 $$ = NEW_YIELD(0, &@$, &@1, &NULL_LOC, &NULL_LOC);
4433 /*% ripper: yield0! %*/
4434 }
4435 | keyword_defined '\n'? '(' begin_defined expr rparen
4436 {
4437 p->ctxt.in_defined = $4.in_defined;
4438 $$ = new_defined(p, $5, &@$, &@1);
4439 p->ctxt.has_trailing_semicolon = $4.has_trailing_semicolon;
4440 /*% ripper: defined!($:5) %*/
4441 }
4442 | keyword_not '(' expr rparen
4443 {
4444 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4445 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4446 }
4447 | keyword_not '(' rparen
4448 {
4449 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
4450 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4451 }
4452 | fcall brace_block
4453 {
4454 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
4455 /*% ripper: method_add_block!(method_add_arg!(fcall!($:1), args_new!), $:2) %*/
4456 }
4457 | method_call
4458 | method_call brace_block
4459 {
4460 block_dup_check(p, get_nd_args(p, $1), $2);
4461 $$ = method_add_block(p, $1, $2, &@$);
4462 /*% ripper: method_add_block!($:1, $:2) %*/
4463 }
4464 | lambda
4465 | k_if expr_value then
4466 compstmt(stmts)
4467 if_tail
4468 k_end
4469 {
4470 if ($5 && nd_type_p($5, NODE_IF))
4471 RNODE_IF($5)->end_keyword_loc = @6;
4472
4473 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4474 fixpos($$, $2);
4475 /*% ripper: if!($:2, $:4, $:5) %*/
4476 }
4477 | k_unless expr_value then
4478 compstmt(stmts)
4479 opt_else
4480 k_end
4481 {
4482 $$ = new_unless(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4483 fixpos($$, $2);
4484 /*% ripper: unless!($:2, $:4, $:5) %*/
4485 }
4486 | k_while expr_value_do
4487 compstmt(stmts)
4488 k_end
4489 {
4490 restore_block_exit(p, $1);
4491 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4492 fixpos($$, $2);
4493 /*% ripper: while!($:2, $:3) %*/
4494 }
4495 | k_until expr_value_do
4496 compstmt(stmts)
4497 k_end
4498 {
4499 restore_block_exit(p, $1);
4500 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4501 fixpos($$, $2);
4502 /*% ripper: until!($:2, $:3) %*/
4503 }
4504 | k_case expr_value terms?
4505 {
4506 $$ = p->case_labels;
4507 p->case_labels = CHECK_LITERAL_WHEN;
4508 }<labels>
4509 case_body
4510 k_end
4511 {
4512 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4513 p->case_labels = $4;
4514 $$ = NEW_CASE($2, $5, &@$, &@1, &@6);
4515 fixpos($$, $2);
4516 /*% ripper: case!($:2, $:5) %*/
4517 }
4518 | k_case terms?
4519 {
4520 $$ = p->case_labels;
4521 p->case_labels = 0;
4522 }<labels>
4523 case_body
4524 k_end
4525 {
4526 if (p->case_labels) st_free_table(p->case_labels);
4527 p->case_labels = $3;
4528 $$ = NEW_CASE2($4, &@$, &@1, &@5);
4529 /*% ripper: case!(Qnil, $:4) %*/
4530 }
4531 | k_case expr_value terms?
4532 p_case_body
4533 k_end
4534 {
4535 $$ = NEW_CASE3($2, $4, &@$, &@1, &@5);
4536 /*% ripper: case!($:2, $:4) %*/
4537 }
4538 | k_for for_var keyword_in
4539 {COND_PUSH(1);} expr_value do {COND_POP();}
4540 compstmt(stmts)
4541 k_end
4542 {
4543 restore_block_exit(p, $k_for);
4544 /*
4545 * for a, b, c in e
4546 * #=>
4547 * e.each{|*x| a, b, c = x}
4548 *
4549 * for a in e
4550 * #=>
4551 * e.each{|x| a, = x}
4552 */
4553 ID id = internal_id(p);
4554 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4555 rb_node_args_t *args;
4556 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4557 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4558 tbl->ids[0] = id; /* internal id */
4559
4560 switch (nd_type($for_var)) {
4561 case NODE_LASGN:
4562 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4563 set_nd_value(p, $for_var, internal_var);
4564 id = 0;
4565 m->nd_plen = 1;
4566 m->nd_next = $for_var;
4567 break;
4568 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4569 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4570 break;
4571 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4572 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);
4573 }
4574 /* {|*internal_id| <m> = internal_id; ... } */
4575 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@for_var), &@for_var);
4576 scope = NEW_SCOPE2(tbl, args, $compstmt, NULL, &@$);
4577 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4578 $$ = NEW_FOR($5, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4579 RNODE_SCOPE(scope)->nd_parent = $$;
4580 fixpos($$, $for_var);
4581 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4582 }
4583 | k_class cpath superclass
4584 {
4585 begin_definition("class", &@k_class, &@cpath);
4586 }
4587 bodystmt
4588 k_end
4589 {
4590 YYLTYPE inheritance_operator_loc = NULL_LOC;
4591 if ($superclass) {
4592 inheritance_operator_loc = @superclass;
4593 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
4594 }
4595 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
4596 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4597 set_line_body($bodystmt, @superclass.end_pos.lineno);
4598 nd_set_line($$, @superclass.end_pos.lineno);
4599 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4600 local_pop(p);
4601 p->ctxt.in_class = $k_class.in_class;
4602 p->ctxt.cant_return = $k_class.cant_return;
4603 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4604 }
4605 | k_class tLSHFT expr_value
4606 {
4607 begin_definition("", &@k_class, &@tLSHFT);
4608 }
4609 term
4610 bodystmt
4611 k_end
4612 {
4613 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$, &@k_class, &@tLSHFT, &@k_end);
4614 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4615 set_line_body($bodystmt, nd_line($expr_value));
4616 fixpos($$, $expr_value);
4617 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4618 local_pop(p);
4619 p->ctxt.in_def = $k_class.in_def;
4620 p->ctxt.in_class = $k_class.in_class;
4621 p->ctxt.cant_return = $k_class.cant_return;
4622 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4623 }
4624 | k_module cpath
4625 {
4626 begin_definition("module", &@k_module, &@cpath);
4627 }
4628 bodystmt
4629 k_end
4630 {
4631 $$ = NEW_MODULE($cpath, $bodystmt, &@$, &@k_module, &@k_end);
4632 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4633 set_line_body($bodystmt, @cpath.end_pos.lineno);
4634 nd_set_line($$, @cpath.end_pos.lineno);
4635 /*% ripper: module!($:cpath, $:bodystmt) %*/
4636 local_pop(p);
4637 p->ctxt.in_class = $k_module.in_class;
4638 p->ctxt.cant_return = $k_module.cant_return;
4639 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4640 }
4641 | defn_head[head]
4642 f_arglist[args]
4643 {
4644 push_end_expect_token_locations(p, &@head.beg_pos);
4645 }
4646 bodystmt
4647 k_end
4648 {
4649 restore_defun(p, $head);
4650 ($$ = $head->nd_def)->nd_loc = @$;
4651 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4652 RNODE_DEFN($$)->nd_defn = $bodystmt;
4653 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4654 local_pop(p);
4655 }
4656 | defs_head[head]
4657 f_arglist[args]
4658 {
4659 push_end_expect_token_locations(p, &@head.beg_pos);
4660 }
4661 bodystmt
4662 k_end
4663 {
4664 restore_defun(p, $head);
4665 ($$ = $head->nd_def)->nd_loc = @$;
4666 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4667 RNODE_DEFS($$)->nd_defn = $bodystmt;
4668 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4669 local_pop(p);
4670 }
4671 | keyword_break
4672 {
4673 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@1));
4674 /*% ripper: break!(args_new!) %*/
4675 }
4676 | keyword_next
4677 {
4678 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@1));
4679 /*% ripper: next!(args_new!) %*/
4680 }
4681 | keyword_redo
4682 {
4683 $$ = add_block_exit(p, NEW_REDO(&@$, &@1));
4684 /*% ripper: redo! %*/
4685 }
4686 | keyword_retry
4687 {
4688 if (!p->ctxt.in_defined) {
4689 switch (p->ctxt.in_rescue) {
4690 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4691 case after_rescue: /* ok */ break;
4692 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4693 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4694 }
4695 }
4696 $$ = NEW_RETRY(&@$);
4697 /*% ripper: retry! %*/
4698 }
4699 ;
4700
4701primary_value : value_expr(primary)
4702 ;
4703
4704k_begin : keyword_begin
4705 {
4706 token_info_push(p, "begin", &@$);
4707 push_end_expect_token_locations(p, &@1.beg_pos);
4708 }
4709 ;
4710
4711k_if : keyword_if
4712 {
4713 WARN_EOL("if");
4714 token_info_push(p, "if", &@$);
4715 if (p->token_info && p->token_info->nonspc &&
4716 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4717 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4718 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4719 beg += rb_strlen_lit("else");
4720 while (beg < tok && ISSPACE(*beg)) beg++;
4721 if (beg == tok) {
4722 p->token_info->nonspc = 0;
4723 }
4724 }
4725 push_end_expect_token_locations(p, &@1.beg_pos);
4726 }
4727 ;
4728
4729k_unless : keyword_unless
4730 {
4731 token_info_push(p, "unless", &@$);
4732 push_end_expect_token_locations(p, &@1.beg_pos);
4733 }
4734 ;
4735
4736k_while : keyword_while allow_exits
4737 {
4738 $$ = $allow_exits;
4739 token_info_push(p, "while", &@$);
4740 push_end_expect_token_locations(p, &@1.beg_pos);
4741 }
4742 ;
4743
4744k_until : keyword_until allow_exits
4745 {
4746 $$ = $allow_exits;
4747 token_info_push(p, "until", &@$);
4748 push_end_expect_token_locations(p, &@1.beg_pos);
4749 }
4750 ;
4751
4752k_case : keyword_case
4753 {
4754 token_info_push(p, "case", &@$);
4755 push_end_expect_token_locations(p, &@1.beg_pos);
4756 }
4757 ;
4758
4759k_for : keyword_for allow_exits
4760 {
4761 $$ = $allow_exits;
4762 token_info_push(p, "for", &@$);
4763 push_end_expect_token_locations(p, &@1.beg_pos);
4764 }
4765 ;
4766
4767k_class : keyword_class
4768 {
4769 token_info_push(p, "class", &@$);
4770 $$ = p->ctxt;
4771 p->ctxt.in_rescue = before_rescue;
4772 push_end_expect_token_locations(p, &@1.beg_pos);
4773 }
4774 ;
4775
4776k_module : keyword_module
4777 {
4778 token_info_push(p, "module", &@$);
4779 $$ = p->ctxt;
4780 p->ctxt.in_rescue = before_rescue;
4781 push_end_expect_token_locations(p, &@1.beg_pos);
4782 }
4783 ;
4784
4785k_def : keyword_def
4786 {
4787 token_info_push(p, "def", &@$);
4788 $$ = NEW_DEF_TEMP(&@$);
4789 p->ctxt.in_argdef = 1;
4790 }
4791 ;
4792
4793k_do : keyword_do
4794 {
4795 token_info_push(p, "do", &@$);
4796 push_end_expect_token_locations(p, &@1.beg_pos);
4797 }
4798 ;
4799
4800k_do_block : keyword_do_block
4801 {
4802 token_info_push(p, "do", &@$);
4803 push_end_expect_token_locations(p, &@1.beg_pos);
4804 }
4805 ;
4806
4807k_rescue : keyword_rescue
4808 {
4809 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4810 $$ = p->ctxt;
4811 p->ctxt.in_rescue = after_rescue;
4812 }
4813 ;
4814
4815k_ensure : keyword_ensure
4816 {
4817 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4818 $$ = p->ctxt;
4819 }
4820 ;
4821
4822k_when : keyword_when
4823 {
4824 token_info_warn(p, "when", p->token_info, 0, &@$);
4825 }
4826 ;
4827
4828k_else : keyword_else
4829 {
4830 token_info *ptinfo_beg = p->token_info;
4831 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4832 token_info_warn(p, "else", p->token_info, same, &@$);
4833 if (same) {
4834 token_info e;
4835 e.next = ptinfo_beg->next;
4836 e.token = "else";
4837 token_info_setup(&e, p->lex.pbeg, &@$);
4838 if (!e.nonspc) *ptinfo_beg = e;
4839 }
4840 }
4841 ;
4842
4843k_elsif : keyword_elsif
4844 {
4845 WARN_EOL("elsif");
4846 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4847 }
4848 ;
4849
4850k_end : keyword_end
4851 {
4852 token_info_pop(p, "end", &@$);
4853 pop_end_expect_token_locations(p);
4854 }
4855 | tDUMNY_END
4856 {
4857 compile_error(p, "syntax error, unexpected end-of-input");
4858 }
4859 ;
4860
4861k_return : keyword_return
4862 {
4863 if (p->ctxt.cant_return && !dyna_in_block(p))
4864 yyerror1(&@1, "Invalid return in class/module body");
4865 }
4866 ;
4867
4868k_yield : keyword_yield
4869 {
4870 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4871 yyerror1(&@1, "Invalid yield");
4872 }
4873 ;
4874
4875then : term
4876 | keyword_then
4877 | term keyword_then
4878 ;
4879
4880do : term
4881 | keyword_do_cond { $$ = keyword_do_cond; }
4882 ;
4883
4884if_tail : opt_else
4885 | k_elsif expr_value then
4886 compstmt(stmts)
4887 if_tail
4888 {
4889 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4890 fixpos($$, $2);
4891 /*% ripper: elsif!($:2, $:4, $:5) %*/
4892 }
4893 ;
4894
4895opt_else : none
4896 | k_else compstmt(stmts)
4897 {
4898 $$ = $2;
4899 /*% ripper: else!($:2) %*/
4900 }
4901 ;
4902
4903for_var : lhs
4904 | mlhs
4905 ;
4906
4907f_marg : f_norm_arg
4908 {
4909 $$ = assignable(p, $1, 0, &@$);
4910 mark_lvar_used(p, $$);
4911 }
4912 | tLPAREN f_margs rparen
4913 {
4914 $$ = (NODE *)$2;
4915 /*% ripper: mlhs_paren!($:2) %*/
4916 }
4917 ;
4918
4919
4920f_margs : mlhs(f_marg)
4921 {
4922 $$ = NEW_MASGN($1, 0, &@$);
4923 /*% ripper: $:1 %*/
4924 }
4925 | mlhs(f_marg) ',' f_rest_marg
4926 {
4927 $$ = NEW_MASGN($1, $3, &@$);
4928 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4929 }
4930 | mlhs(f_marg) ',' f_rest_marg ',' mlhs(f_marg)
4931 {
4932 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4933 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4934 }
4935 | f_rest_marg
4936 {
4937 $$ = NEW_MASGN(0, $1, &@$);
4938 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4939 }
4940 | f_rest_marg ',' mlhs(f_marg)
4941 {
4942 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4943 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4944 }
4945 ;
4946
4947f_rest_marg : tSTAR f_norm_arg
4948 {
4949 /*% ripper: $:2 %*/
4950 $$ = assignable(p, $2, 0, &@$);
4951 mark_lvar_used(p, $$);
4952 }
4953 | tSTAR
4954 {
4955 $$ = NODE_SPECIAL_NO_NAME_REST;
4956 /*% ripper: Qnil %*/
4957 }
4958 ;
4959
4960f_any_kwrest : f_kwrest
4961 | f_no_kwarg
4962 {
4963 $$ = idNil;
4964 /*% ripper: ID2VAL(idNil) %*/
4965 }
4966 ;
4967
4968f_eq : {p->ctxt.in_argdef = 0;} '=';
4969
4970block_args_tail : args_tail_basic(primary_value)
4971 ;
4972
4973excessed_comma : ','
4974 {
4975 /* magic number for rest_id in iseq_set_arguments() */
4976 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4977 /*% ripper: excessed_comma! %*/
4978 }
4979 ;
4980
4981block_param : f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
4982 {
4983 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
4984 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
4985 }
4986 | f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
4987 {
4988 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4989 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
4990 }
4991 | f_arg ',' f_opt_arg(primary_value) opt_args_tail(block_args_tail)
4992 {
4993 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
4994 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
4995 }
4996 | f_arg ',' f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
4997 {
4998 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
4999 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
5000 }
5001 | f_arg ',' f_rest_arg opt_args_tail(block_args_tail)
5002 {
5003 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
5004 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
5005 }
5006 | f_arg excessed_comma
5007 {
5008 $$ = new_args_tail(p, 0, 0, 0, &@2);
5009 $$ = new_args(p, $1, 0, $2, 0, $$, &@$);
5010 /*% ripper: params!($:1, Qnil, $:2, Qnil, Qnil, Qnil, Qnil) %*/
5011 }
5012 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5013 {
5014 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
5015 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
5016 }
5017 | f_arg opt_args_tail(block_args_tail)
5018 {
5019 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
5020 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
5021 }
5022 | f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5023 {
5024 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
5025 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
5026 }
5027 | f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5028 {
5029 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
5030 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
5031 }
5032 | f_opt_arg(primary_value) opt_args_tail(block_args_tail)
5033 {
5034 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
5035 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
5036 }
5037 | f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5038 {
5039 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
5040 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
5041 }
5042 | f_rest_arg opt_args_tail(block_args_tail)
5043 {
5044 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
5045 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
5046 }
5047 | f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5048 {
5049 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
5050 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
5051 }
5052 | block_args_tail
5053 {
5054 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
5055 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
5056 }
5057 ;
5058
5059opt_block_param_def : none
5060 | block_param_def
5061 {
5062 p->command_start = TRUE;
5063 }
5064 ;
5065
5066block_param_def : '|' opt_block_param opt_bv_decl '|'
5067 {
5068 p->max_numparam = ORDINAL_PARAM;
5069 p->ctxt.in_argdef = 0;
5070 $$ = $2;
5071 /*% ripper: block_var!($:2, $:3) %*/
5072 }
5073 ;
5074
5075opt_block_param : /* none */
5076 {
5077 $$ = 0;
5078 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5079 }
5080 | block_param
5081 ;
5082
5083opt_bv_decl : '\n'?
5084 {
5085 $$ = 0;
5086 /*% ripper: Qfalse %*/
5087 }
5088 | '\n'? ';' bv_decls '\n'?
5089 {
5090 $$ = 0;
5091 /*% ripper: $:3 %*/
5092 }
5093 ;
5094
5095bv_decls : bvar
5096 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5097 | bv_decls ',' bvar
5098 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5099 ;
5100
5101bvar : tIDENTIFIER
5102 {
5103 new_bv(p, $1);
5104 /*% ripper: $:1 %*/
5105 }
5106 | f_bad_arg
5107 ;
5108
5109max_numparam : {
5110 $$ = p->max_numparam;
5111 p->max_numparam = 0;
5112 }
5113 ;
5114
5115numparam : {
5116 $$ = numparam_push(p);
5117 }
5118 ;
5119
5120it_id : {
5121 $$ = p->it_id;
5122 p->it_id = 0;
5123 }
5124 ;
5125
5126lambda : tLAMBDA[lpar]
5127 {
5128 token_info_push(p, "->", &@1);
5129 $$ = dyna_push(p);
5130 }[dyna]<vars>
5131 max_numparam numparam it_id allow_exits
5132 f_larglist[args]
5133 {
5134 CMDARG_PUSH(0);
5135 }
5136 lambda_body[body]
5137 {
5138 int max_numparam = p->max_numparam;
5139 ID it_id = p->it_id;
5140 p->lex.lpar_beg = $lpar;
5141 p->max_numparam = $max_numparam;
5142 p->it_id = $it_id;
5143 restore_block_exit(p, $allow_exits);
5144 CMDARG_POP();
5145 $args = args_with_numbered(p, $args, max_numparam, it_id);
5146 {
5147 YYLTYPE loc = code_loc_gen(&@args, &@body);
5148 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5149 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5150 nd_set_line($$, @args.end_pos.lineno);
5151 nd_set_first_loc($$, @1.beg_pos);
5152 xfree($body);
5153 }
5154 /*% ripper: lambda!($:args, $:body) %*/
5155 numparam_pop(p, $numparam);
5156 dyna_pop(p, $dyna);
5157 }
5158 ;
5159
5160f_larglist : '(' f_args opt_bv_decl ')'
5161 {
5162 p->ctxt.in_argdef = 0;
5163 $$ = $f_args;
5164 p->max_numparam = ORDINAL_PARAM;
5165 /*% ripper: paren!($:2) %*/
5166 }
5167 | f_args
5168 {
5169 p->ctxt.in_argdef = 0;
5170 if (!args_info_empty_p(&$1->nd_ainfo))
5171 p->max_numparam = ORDINAL_PARAM;
5172 $$ = $f_args;
5173 }
5174 ;
5175
5176lambda_body : tLAMBEG compstmt(stmts) '}'
5177 {
5178 token_info_pop(p, "}", &@3);
5179 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5180 /*% ripper: $:2 %*/
5181 }
5182 | keyword_do_LAMBDA
5183 {
5184 push_end_expect_token_locations(p, &@1.beg_pos);
5185 }
5186 bodystmt k_end
5187 {
5188 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5189 /*% ripper: $:3 %*/
5190 }
5191 ;
5192
5193do_block : k_do_block do_body k_end
5194 {
5195 $$ = $2;
5196 set_embraced_location($$, &@1, &@3);
5197 /*% ripper: $:2 %*/
5198 }
5199 ;
5200
5201block_call : command do_block
5202 {
5203 if (nd_type_p($1, NODE_YIELD)) {
5204 compile_error(p, "block given to yield");
5205 }
5206 else {
5207 block_dup_check(p, get_nd_args(p, $1), $2);
5208 }
5209 $$ = method_add_block(p, $1, $2, &@$);
5210 fixpos($$, $1);
5211 /*% ripper: method_add_block!($:1, $:2) %*/
5212 }
5213 | block_call call_op2 operation2 opt_paren_args
5214 {
5215 bool has_args = $4 != 0;
5216 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5217 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5218 /*% ripper: call!($:1, $:2, $:3) %*/
5219 if (has_args) {
5220 /*% ripper: method_add_arg!($:$, $:4) %*/
5221 }
5222 }
5223 | block_call call_op2 operation2 opt_paren_args brace_block
5224 {
5225 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5226 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5227 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
5228 if ($5) {
5229 /*% ripper: method_add_block!($:$, $:5) %*/
5230 }
5231 }
5232 | block_call call_op2 operation2 command_args do_block
5233 {
5234 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5235 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5236 }
5237 ;
5238
5239method_call : fcall paren_args
5240 {
5241 $1->nd_args = $2;
5242 $$ = (NODE *)$1;
5243 nd_set_last_loc($1, @2.end_pos);
5244 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5245 }
5246 | primary_value call_op operation2 opt_paren_args
5247 {
5248 bool has_args = $4 != 0;
5249 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5250 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5251 nd_set_line($$, @3.end_pos.lineno);
5252 /*% ripper: call!($:1, $:2, $:3) %*/
5253 if (has_args) {
5254 /*% ripper: method_add_arg!($:$, $:4) %*/
5255 }
5256 }
5257 | primary_value tCOLON2 operation2 paren_args
5258 {
5259 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5260 nd_set_line($$, @3.end_pos.lineno);
5261 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5262 }
5263 | primary_value tCOLON2 operation3
5264 {
5265 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5266 /*% ripper: call!($:1, $:2, $:3) %*/
5267 }
5268 | primary_value call_op2 paren_args
5269 {
5270 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5271 nd_set_line($$, @2.end_pos.lineno);
5272 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5273 }
5274 | keyword_super paren_args
5275 {
5276 rb_code_location_t lparen_loc = @2;
5277 rb_code_location_t rparen_loc = @2;
5278 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5279 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5280
5281 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5282 /*% ripper: super!($:2) %*/
5283 }
5284 | keyword_super
5285 {
5286 $$ = NEW_ZSUPER(&@$);
5287 /*% ripper: zsuper! %*/
5288 }
5289 | primary_value '[' opt_call_args rbracket
5290 {
5291 $$ = NEW_CALL($1, tAREF, $3, &@$);
5292 fixpos($$, $1);
5293 /*% ripper: aref!($:1, $:3) %*/
5294 }
5295 ;
5296
5297brace_block : '{' brace_body '}'
5298 {
5299 $$ = $2;
5300 set_embraced_location($$, &@1, &@3);
5301 /*% ripper: $:2 %*/
5302 }
5303 | k_do do_body k_end
5304 {
5305 $$ = $2;
5306 set_embraced_location($$, &@1, &@3);
5307 /*% ripper: $:2 %*/
5308 }
5309 ;
5310
5311brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5312 max_numparam numparam it_id allow_exits
5313 opt_block_param_def[args] compstmt(stmts)
5314 {
5315 int max_numparam = p->max_numparam;
5316 ID it_id = p->it_id;
5317 p->max_numparam = $max_numparam;
5318 p->it_id = $it_id;
5319 $args = args_with_numbered(p, $args, max_numparam, it_id);
5320 $$ = NEW_ITER($args, $compstmt, &@$);
5321 /*% ripper: brace_block!($:args, $:compstmt) %*/
5322 restore_block_exit(p, $allow_exits);
5323 numparam_pop(p, $numparam);
5324 dyna_pop(p, $dyna);
5325 }
5326 ;
5327
5328do_body : {
5329 $$ = dyna_push(p);
5330 CMDARG_PUSH(0);
5331 }[dyna]<vars>
5332 max_numparam numparam it_id allow_exits
5333 opt_block_param_def[args] bodystmt
5334 {
5335 int max_numparam = p->max_numparam;
5336 ID it_id = p->it_id;
5337 p->max_numparam = $max_numparam;
5338 p->it_id = $it_id;
5339 $args = args_with_numbered(p, $args, max_numparam, it_id);
5340 $$ = NEW_ITER($args, $bodystmt, &@$);
5341 /*% ripper: do_block!($:args, $:bodystmt) %*/
5342 CMDARG_POP();
5343 restore_block_exit(p, $allow_exits);
5344 numparam_pop(p, $numparam);
5345 dyna_pop(p, $dyna);
5346 }
5347 ;
5348
5349case_args : arg_value
5350 {
5351 check_literal_when(p, $arg_value, &@arg_value);
5352 $$ = NEW_LIST($arg_value, &@$);
5353 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5354 }
5355 | tSTAR arg_value
5356 {
5357 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5358 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5359 }
5360 | case_args[non_last_args] ',' arg_value
5361 {
5362 check_literal_when(p, $arg_value, &@arg_value);
5363 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5364 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5365 }
5366 | case_args[non_last_args] ',' tSTAR arg_value
5367 {
5368 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5369 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5370 }
5371 ;
5372
5373case_body : k_when case_args then
5374 compstmt(stmts)
5375 cases
5376 {
5377 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5378 fixpos($$, $2);
5379 /*% ripper: when!($:2, $:4, $:5) %*/
5380 }
5381 ;
5382
5383cases : opt_else
5384 | case_body
5385 ;
5386
5387p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5388p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5389
5390p_in_kwarg : {
5391 $$ = p->ctxt;
5392 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5393 p->command_start = FALSE;
5394 p->ctxt.in_kwarg = 1;
5395 }
5396 ;
5397
5398p_case_body : keyword_in
5399 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5400 p_top_expr[expr] then
5401 {
5402 pop_pktbl(p, $p_pktbl);
5403 pop_pvtbl(p, $p_pvtbl);
5404 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5405 }
5406 compstmt(stmts)
5407 p_cases[cases]
5408 {
5409 $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
5410 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5411 }
5412 ;
5413
5414p_cases : opt_else
5415 | p_case_body
5416 ;
5417
5418p_top_expr : p_top_expr_body
5419 | p_top_expr_body modifier_if expr_value
5420 {
5421 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5422 fixpos($$, $3);
5423 /*% ripper: if_mod!($:3, $:1) %*/
5424 }
5425 | p_top_expr_body modifier_unless expr_value
5426 {
5427 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5428 fixpos($$, $3);
5429 /*% ripper: unless_mod!($:3, $:1) %*/
5430 }
5431 ;
5432
5433p_top_expr_body : p_expr
5434 | p_expr ','
5435 {
5436 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5437 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5438 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5439 }
5440 | p_expr ',' p_args
5441 {
5442 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5443 nd_set_first_loc($$, @1.beg_pos);
5444 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5445 }
5446 | p_find
5447 {
5448 $$ = new_find_pattern(p, 0, $1, &@$);
5449 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5450 }
5451 | p_args_tail
5452 {
5453 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5454 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5455 }
5456 | p_kwargs
5457 {
5458 $$ = new_hash_pattern(p, 0, $1, &@$);
5459 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5460 }
5461 ;
5462
5463p_expr : p_as
5464 ;
5465
5466p_as : p_expr tASSOC p_variable
5467 {
5468 NODE *n = NEW_LIST($1, &@$);
5469 n = list_append(p, n, $3);
5470 $$ = new_hash(p, n, &@$);
5471 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5472 }
5473 | p_alt
5474 ;
5475
5476p_alt : p_alt '|' p_expr_basic
5477 {
5478 $$ = NEW_OR($1, $3, &@$, &@2);
5479 /*% ripper: binary!($:1, ID2VAL(idOr), $:3) %*/
5480 }
5481 | p_expr_basic
5482 ;
5483
5484p_lparen : '(' p_pktbl
5485 {
5486 $$ = $2;
5487 /*% ripper: $:2 %*/
5488 }
5489 ;
5490
5491p_lbracket : '[' p_pktbl
5492 {
5493 $$ = $2;
5494 /*% ripper: $:2 %*/
5495 }
5496 ;
5497
5498p_expr_basic : p_value
5499 | p_variable
5500 | p_const p_lparen[p_pktbl] p_args rparen
5501 {
5502 pop_pktbl(p, $p_pktbl);
5503 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5504 nd_set_first_loc($$, @p_const.beg_pos);
5505 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5506 }
5507 | p_const p_lparen[p_pktbl] p_find rparen
5508 {
5509 pop_pktbl(p, $p_pktbl);
5510 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5511 nd_set_first_loc($$, @p_const.beg_pos);
5512 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5513 }
5514 | p_const p_lparen[p_pktbl] p_kwargs rparen
5515 {
5516 pop_pktbl(p, $p_pktbl);
5517 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5518 nd_set_first_loc($$, @p_const.beg_pos);
5519 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5520 }
5521 | p_const '(' rparen
5522 {
5523 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5524 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5525 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5526 }
5527 | p_const p_lbracket[p_pktbl] p_args rbracket
5528 {
5529 pop_pktbl(p, $p_pktbl);
5530 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5531 nd_set_first_loc($$, @p_const.beg_pos);
5532 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5533 }
5534 | p_const p_lbracket[p_pktbl] p_find rbracket
5535 {
5536 pop_pktbl(p, $p_pktbl);
5537 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5538 nd_set_first_loc($$, @p_const.beg_pos);
5539 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5540 }
5541 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5542 {
5543 pop_pktbl(p, $p_pktbl);
5544 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5545 nd_set_first_loc($$, @p_const.beg_pos);
5546 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5547 }
5548 | p_const '[' rbracket
5549 {
5550 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5551 $$ = new_array_pattern(p, $1, 0, $$, &@$);
5552 /*% ripper: aryptn!($:1, Qnil, Qnil, Qnil) %*/
5553 }
5554 | tLBRACK p_args rbracket
5555 {
5556 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5557 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5558 }
5559 | tLBRACK p_find rbracket
5560 {
5561 $$ = new_find_pattern(p, 0, $p_find, &@$);
5562 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5563 }
5564 | tLBRACK rbracket
5565 {
5566 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5567 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5568 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5569 }
5570 | tLBRACE p_pktbl lex_ctxt[ctxt]
5571 {
5572 p->ctxt.in_kwarg = 0;
5573 }
5574 p_kwargs rbrace
5575 {
5576 pop_pktbl(p, $p_pktbl);
5577 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5578 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5579 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5580 }
5581 | tLBRACE rbrace
5582 {
5583 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5584 $$ = new_hash_pattern(p, 0, $$, &@$);
5585 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5586 }
5587 | tLPAREN p_pktbl p_expr rparen
5588 {
5589 pop_pktbl(p, $p_pktbl);
5590 $$ = $p_expr;
5591 /*% ripper: $:p_expr %*/
5592 }
5593 ;
5594
5595p_args : p_expr
5596 {
5597 NODE *pre_args = NEW_LIST($1, &@$);
5598 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5599 /*% ripper: [[$:1], Qnil, Qnil] %*/
5600 }
5601 | p_args_head
5602 {
5603 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5604 /*% ripper: [$:1, Qnil, Qnil] %*/
5605 }
5606 | p_args_head p_arg
5607 {
5608 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5609 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5610 }
5611 | p_args_head p_rest
5612 {
5613 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5614 /*% ripper: [$:1, $:2, Qnil] %*/
5615 }
5616 | p_args_head p_rest ',' p_args_post
5617 {
5618 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5619 /*% ripper: [$:1, $:2, $:4] %*/
5620 }
5621 | p_args_tail
5622 ;
5623
5624p_args_head : p_arg ','
5625 | p_args_head p_arg ','
5626 {
5627 $$ = list_concat($1, $2);
5628 /*% ripper: rb_ary_concat($:1, $:2) %*/
5629 }
5630 ;
5631
5632p_args_tail : p_rest
5633 {
5634 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5635 /*% ripper: [Qnil, $:1, Qnil] %*/
5636 }
5637 | p_rest ',' p_args_post
5638 {
5639 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5640 /*% ripper: [Qnil, $:1, $:3] %*/
5641 }
5642 ;
5643
5644p_find : p_rest ',' p_args_post ',' p_rest
5645 {
5646 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5647 /*% ripper: [$:1, $:3, $:5] %*/
5648 }
5649 ;
5650
5651
5652p_rest : tSTAR tIDENTIFIER
5653 {
5654 error_duplicate_pattern_variable(p, $2, &@2);
5655 /*% ripper: var_field!($:2) %*/
5656 $$ = assignable(p, $2, 0, &@$);
5657 }
5658 | tSTAR
5659 {
5660 $$ = 0;
5661 /*% ripper: var_field!(Qnil) %*/
5662 }
5663 ;
5664
5665p_args_post : p_arg
5666 | p_args_post ',' p_arg
5667 {
5668 $$ = list_concat($1, $3);
5669 /*% ripper: rb_ary_concat($:1, $:3) %*/
5670 }
5671 ;
5672
5673p_arg : p_expr
5674 {
5675 $$ = NEW_LIST($1, &@$);
5676 /*% ripper: [$:1] %*/
5677 }
5678 ;
5679
5680p_kwargs : p_kwarg ',' p_any_kwrest
5681 {
5682 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5683 /*% ripper: [$:1, $:3] %*/
5684 }
5685 | p_kwarg
5686 {
5687 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5688 /*% ripper: [$:1, Qnil] %*/
5689 }
5690 | p_kwarg ','
5691 {
5692 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5693 /*% ripper: [$:1, Qnil] %*/
5694 }
5695 | p_any_kwrest
5696 {
5697 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5698 /*% ripper: [[], $:1] %*/
5699 }
5700 ;
5701
5702p_kwarg : p_kw
5703 /*% ripper[brace]: [$:1] %*/
5704 | p_kwarg ',' p_kw
5705 {
5706 $$ = list_concat($1, $3);
5707 /*% ripper: rb_ary_push($:1, $:3) %*/
5708 }
5709 ;
5710
5711p_kw : p_kw_label p_expr
5712 {
5713 error_duplicate_pattern_key(p, $1, &@1);
5714 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5715 /*% ripper: [$:1, $:2] %*/
5716 }
5717 | p_kw_label
5718 {
5719 error_duplicate_pattern_key(p, $1, &@1);
5720 if ($1 && !is_local_id($1)) {
5721 yyerror1(&@1, "key must be valid as local variables");
5722 }
5723 error_duplicate_pattern_variable(p, $1, &@1);
5724 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5725 /*% ripper: [$:1, Qnil] %*/
5726 }
5727 ;
5728
5729p_kw_label : tLABEL
5730 | tSTRING_BEG string_contents tLABEL_END
5731 {
5732 YYLTYPE loc = code_loc_gen(&@1, &@3);
5733 if (!$2 || nd_type_p($2, NODE_STR)) {
5734 NODE *node = dsym_node(p, $2, &loc);
5735 $$ = rb_sym2id(rb_node_sym_string_val(node));
5736 }
5737 else {
5738 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5739 $$ = rb_intern_str(STR_NEW0());
5740 }
5741 /*% ripper: $:2 %*/
5742 }
5743 ;
5744
5745p_kwrest : kwrest_mark tIDENTIFIER
5746 {
5747 $$ = $2;
5748 /*% ripper: var_field!($:2) %*/
5749 }
5750 | kwrest_mark
5751 {
5752 $$ = 0;
5753 /*% ripper: Qnil %*/
5754 }
5755 ;
5756
5757p_kwnorest : kwrest_mark keyword_nil
5758 {
5759 $$ = 0;
5760 }
5761 ;
5762
5763p_any_kwrest : p_kwrest
5764 | p_kwnorest
5765 {
5766 $$ = idNil;
5767 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5768 }
5769 ;
5770
5771p_value : p_primitive
5772 | range_expr(p_primitive)
5773 | p_var_ref
5774 | p_expr_ref
5775 | p_const
5776 ;
5777
5778p_primitive : inline_primary
5779 | keyword_variable
5780 {
5781 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5782 /*% ripper: var_ref!($:1) %*/
5783 }
5784 | lambda
5785 ;
5786
5787p_variable : tIDENTIFIER
5788 {
5789 error_duplicate_pattern_variable(p, $1, &@1);
5790 /*% ripper: var_field!($:1) %*/
5791 $$ = assignable(p, $1, 0, &@$);
5792 }
5793 ;
5794
5795p_var_ref : '^' tIDENTIFIER
5796 {
5797 NODE *n = gettable(p, $2, &@$);
5798 if (!n) {
5799 n = NEW_ERROR(&@$);
5800 }
5801 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5802 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5803 }
5804 $$ = n;
5805 /*% ripper: var_ref!($:2) %*/
5806 }
5807 | '^' nonlocal_var
5808 {
5809 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5810 /*% ripper: var_ref!($:2) %*/
5811 }
5812 ;
5813
5814p_expr_ref : '^' tLPAREN expr_value rparen
5815 {
5816 $$ = NEW_BLOCK($3, &@$);
5817 /*% ripper: begin!($:3) %*/
5818 }
5819 ;
5820
5821p_const : tCOLON3 cname
5822 {
5823 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5824 /*% ripper: top_const_ref!($:2) %*/
5825 }
5826 | p_const tCOLON2 cname
5827 {
5828 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5829 /*% ripper: const_path_ref!($:1, $:3) %*/
5830 }
5831 | tCONSTANT
5832 {
5833 $$ = gettable(p, $1, &@$);
5834 /*% ripper: var_ref!($:1) %*/
5835 }
5836 ;
5837
5838opt_rescue : k_rescue exc_list exc_var then
5839 compstmt(stmts)
5840 opt_rescue
5841 {
5842 NODE *err = $3;
5843 if ($3) {
5844 err = NEW_ERRINFO(&@3);
5845 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5846 }
5847 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5848 if ($2) {
5849 fixpos($$, $2);
5850 }
5851 else if ($3) {
5852 fixpos($$, $3);
5853 }
5854 else {
5855 fixpos($$, $5);
5856 }
5857 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5858 }
5859 | none
5860 ;
5861
5862exc_list : arg_value
5863 {
5864 $$ = NEW_LIST($1, &@$);
5865 /*% ripper: rb_ary_new3(1, $:1) %*/
5866 }
5867 | mrhs
5868 {
5869 if (!($$ = splat_array($1))) $$ = $1;
5870 }
5871 | none
5872 ;
5873
5874exc_var : tASSOC lhs
5875 {
5876 $$ = $2;
5877 /*% ripper: $:2 %*/
5878 }
5879 | none
5880 ;
5881
5882opt_ensure : k_ensure stmts terms?
5883 {
5884 p->ctxt.in_rescue = $1.in_rescue;
5885 $$ = $2;
5886 void_expr(p, void_stmts(p, $$));
5887 /*% ripper: ensure!($:2) %*/
5888 }
5889 | none
5890 ;
5891
5892literal : numeric
5893 | symbol
5894 ;
5895
5896strings : string
5897 {
5898 if (!$1) {
5899 $$ = NEW_STR(STRING_NEW0(), &@$);
5900 }
5901 else {
5902 $$ = evstr2dstr(p, $1);
5903 }
5904 /*% ripper: $:1 %*/
5905 }
5906 ;
5907
5908string : tCHAR
5909 | string1
5910 | string string1
5911 {
5912 $$ = literal_concat(p, $1, $2, &@$);
5913 /*% ripper: string_concat!($:1, $:2) %*/
5914 }
5915 ;
5916
5917string1 : tSTRING_BEG string_contents tSTRING_END
5918 {
5919 $$ = heredoc_dedent(p, $2);
5920 if ($$) nd_set_loc($$, &@$);
5921 /*% ripper: $:2 %*/
5922 if (p->heredoc_indent > 0) {
5923 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5924 p->heredoc_indent = 0;
5925 }
5926 /*% ripper: string_literal!($:$) %*/
5927 }
5928 ;
5929
5930xstring : tXSTRING_BEG xstring_contents tSTRING_END
5931 {
5932 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5933 /*% ripper: $:2 %*/
5934 if (p->heredoc_indent > 0) {
5935 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5936 p->heredoc_indent = 0;
5937 }
5938 /*% ripper: xstring_literal!($:$) %*/
5939 }
5940 ;
5941
5942regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5943 {
5944 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5945 /*% ripper: regexp_literal!($:2, $:3) %*/
5946 }
5947 ;
5948
5949words : words(tWORDS_BEG, word_list)
5950 ;
5951
5952word_list : /* none */
5953 {
5954 $$ = 0;
5955 /*% ripper: words_new! %*/
5956 }
5957 | word_list word ' '+
5958 {
5959 $$ = list_append(p, $1, evstr2dstr(p, $2));
5960 /*% ripper: words_add!($:1, $:2) %*/
5961 }
5962 ;
5963
5964word : string_content
5965 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5966 | word string_content
5967 {
5968 $$ = literal_concat(p, $1, $2, &@$);
5969 /*% ripper: word_add!($:1, $:2) %*/
5970 }
5971 ;
5972
5973symbols : words(tSYMBOLS_BEG, symbol_list)
5974 ;
5975
5976symbol_list : /* none */
5977 {
5978 $$ = 0;
5979 /*% ripper: symbols_new! %*/
5980 }
5981 | symbol_list word ' '+
5982 {
5983 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5984 /*% ripper: symbols_add!($:1, $:2) %*/
5985 }
5986 ;
5987
5988qwords : words(tQWORDS_BEG, qword_list)
5989 ;
5990
5991qsymbols : words(tQSYMBOLS_BEG, qsym_list)
5992 ;
5993
5994qword_list : /* none */
5995 {
5996 $$ = 0;
5997 /*% ripper: qwords_new! %*/
5998 }
5999 | qword_list tSTRING_CONTENT ' '+
6000 {
6001 $$ = list_append(p, $1, $2);
6002 /*% ripper: qwords_add!($:1, $:2) %*/
6003 }
6004 ;
6005
6006qsym_list : /* none */
6007 {
6008 $$ = 0;
6009 /*% ripper: qsymbols_new! %*/
6010 }
6011 | qsym_list tSTRING_CONTENT ' '+
6012 {
6013 $$ = symbol_append(p, $1, $2);
6014 /*% ripper: qsymbols_add!($:1, $:2) %*/
6015 }
6016 ;
6017
6018string_contents : /* none */
6019 {
6020 $$ = 0;
6021 /*% ripper: string_content! %*/
6022 }
6023 | string_contents string_content
6024 {
6025 $$ = literal_concat(p, $1, $2, &@$);
6026 /*% ripper: string_add!($:1, $:2) %*/
6027 }
6028 ;
6029
6030xstring_contents: /* none */
6031 {
6032 $$ = 0;
6033 /*% ripper: xstring_new! %*/
6034 }
6035 | xstring_contents string_content
6036 {
6037 $$ = literal_concat(p, $1, $2, &@$);
6038 /*% ripper: xstring_add!($:1, $:2) %*/
6039 }
6040 ;
6041
6042regexp_contents: /* none */
6043 {
6044 $$ = 0;
6045 /*% ripper: regexp_new! %*/
6046 }
6047 | regexp_contents string_content
6048 {
6049 NODE *head = $1, *tail = $2;
6050 if (!head) {
6051 $$ = tail;
6052 }
6053 else if (!tail) {
6054 $$ = head;
6055 }
6056 else {
6057 switch (nd_type(head)) {
6058 case NODE_STR:
6059 head = str2dstr(p, head);
6060 break;
6061 case NODE_DSTR:
6062 break;
6063 default:
6064 head = list_append(p, NEW_DSTR(0, &@$), head);
6065 break;
6066 }
6067 $$ = list_append(p, head, tail);
6068 }
6069 /*% ripper: regexp_add!($:1, $:2) %*/
6070 }
6071 ;
6072
6073string_content : tSTRING_CONTENT
6074 /*% ripper[brace]: $:1 %*/
6075 | tSTRING_DVAR
6076 {
6077 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6078 $$ = p->lex.strterm;
6079 p->lex.strterm = 0;
6080 SET_LEX_STATE(EXPR_BEG);
6081 }<strterm>
6082 string_dvar
6083 {
6084 p->lex.strterm = $2;
6085 $$ = NEW_EVSTR($3, &@$, &@1, &NULL_LOC);
6086 nd_set_line($$, @3.end_pos.lineno);
6087 /*% ripper: string_dvar!($:3) %*/
6088 }
6089 | tSTRING_DBEG[state]
6090 {
6091 CMDARG_PUSH(0);
6092 COND_PUSH(0);
6093 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6094 $$ = p->lex.strterm;
6095 p->lex.strterm = 0;
6096 SET_LEX_STATE(EXPR_BEG);
6097 }[term]<strterm>
6098 {
6099 $$ = p->lex.brace_nest;
6100 p->lex.brace_nest = 0;
6101 }[brace]<num>
6102 {
6103 $$ = p->heredoc_indent;
6104 p->heredoc_indent = 0;
6105 }[indent]<num>
6106 compstmt(stmts) string_dend
6107 {
6108 COND_POP();
6109 CMDARG_POP();
6110 p->lex.strterm = $term;
6111 SET_LEX_STATE($state);
6112 p->lex.brace_nest = $brace;
6113 p->heredoc_indent = $indent;
6114 p->heredoc_line_indent = -1;
6115 if ($compstmt) nd_unset_fl_newline($compstmt);
6116 $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend);
6117 /*% ripper: string_embexpr!($:compstmt) %*/
6118 }
6119 ;
6120
6121string_dend : tSTRING_DEND
6122 | END_OF_INPUT
6123 ;
6124
6125string_dvar : nonlocal_var
6126 {
6127 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6128 /*% ripper: var_ref!($:1) %*/
6129 }
6130 | backref
6131 ;
6132
6133symbol : ssym
6134 | dsym
6135 ;
6136
6137ssym : tSYMBEG sym
6138 {
6139 SET_LEX_STATE(EXPR_END);
6140 VALUE str = rb_id2str($2);
6141 /*
6142 * TODO:
6143 * set_yylval_noname sets invalid id to yylval.
6144 * This branch can be removed once yylval is changed to
6145 * hold lexed string.
6146 */
6147 if (!str) str = STR_NEW0();
6148 $$ = NEW_SYM(str, &@$);
6149 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6150 }
6151 ;
6152
6153sym : fname
6154 | nonlocal_var
6155 ;
6156
6157dsym : tSYMBEG string_contents tSTRING_END
6158 {
6159 SET_LEX_STATE(EXPR_END);
6160 $$ = dsym_node(p, $2, &@$);
6161 /*% ripper: dyna_symbol!($:2) %*/
6162 }
6163 ;
6164
6165numeric : simple_numeric
6166 | tUMINUS_NUM simple_numeric %prec tLOWEST
6167 {
6168 $$ = $2;
6169 negate_lit(p, $$);
6170 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6171 }
6172 ;
6173
6174simple_numeric : tINTEGER
6175 | tFLOAT
6176 | tRATIONAL
6177 | tIMAGINARY
6178 ;
6179
6180nonlocal_var : tIVAR
6181 | tGVAR
6182 | tCVAR
6183 ;
6184
6185user_variable : ident_or_const
6186 | nonlocal_var
6187 ;
6188
6189keyword_variable : keyword_nil {$$ = KWD2EID(nil, $1);}
6190 | keyword_self {$$ = KWD2EID(self, $1);}
6191 | keyword_true {$$ = KWD2EID(true, $1);}
6192 | keyword_false {$$ = KWD2EID(false, $1);}
6193 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6194 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6195 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6196 ;
6197
6198var_ref : user_variable
6199 {
6200 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6201 if (ifdef_ripper(id_is_var(p, $1), false)) {
6202 /*% ripper: var_ref!($:1) %*/
6203 }
6204 else {
6205 /*% ripper: vcall!($:1) %*/
6206 }
6207 }
6208 | keyword_variable
6209 {
6210 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6211 /*% ripper: var_ref!($:1) %*/
6212 }
6213 ;
6214
6215var_lhs : user_or_keyword_variable
6216 {
6217 /*% ripper: var_field!($:1) %*/
6218 $$ = assignable(p, $1, 0, &@$);
6219 }
6220 ;
6221
6222backref : tNTH_REF
6223 | tBACK_REF
6224 ;
6225
6226superclass : '<'
6227 {
6228 SET_LEX_STATE(EXPR_BEG);
6229 p->command_start = TRUE;
6230 }
6231 expr_value term
6232 {
6233 $$ = $3;
6234 /*% ripper: $:3 %*/
6235 }
6236 | none
6237 ;
6238
6239f_opt_paren_args: f_paren_args
6240 | none
6241 {
6242 p->ctxt.in_argdef = 0;
6243 $$ = new_args_tail(p, 0, 0, 0, &@0);
6244 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6245 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6246 }
6247 ;
6248
6249f_paren_args : '(' f_args rparen
6250 {
6251 $$ = $2;
6252 /*% ripper: paren!($:2) %*/
6253 SET_LEX_STATE(EXPR_BEG);
6254 p->command_start = TRUE;
6255 p->ctxt.in_argdef = 0;
6256 }
6257 ;
6258
6259f_arglist : f_paren_args
6260 | {
6261 $$ = p->ctxt;
6262 p->ctxt.in_kwarg = 1;
6263 p->ctxt.in_argdef = 1;
6264 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6265 }<ctxt>
6266 f_args term
6267 {
6268 p->ctxt.in_kwarg = $1.in_kwarg;
6269 p->ctxt.in_argdef = 0;
6270 $$ = $2;
6271 SET_LEX_STATE(EXPR_BEG);
6272 p->command_start = TRUE;
6273 /*% ripper: $:2 %*/
6274 }
6275 ;
6276
6277args_tail : args_tail_basic(arg_value)
6278 | args_forward
6279 {
6280 ID fwd = $args_forward;
6281 if (lambda_beginning_p() ||
6282 (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest)) {
6283 yyerror0("unexpected ... in lambda argument");
6284 fwd = 0;
6285 }
6286 else {
6287 add_forwarding_args(p);
6288 }
6289 $$ = new_args_tail(p, 0, fwd, arg_FWD_BLOCK, &@1);
6290 $$->nd_ainfo.forwarding = 1;
6291 /*% ripper: [Qnil, $:1, Qnil] %*/
6292 }
6293 ;
6294
6295f_args : f_arg ',' f_opt_arg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6296 {
6297 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
6298 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
6299 }
6300 | f_arg ',' f_opt_arg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6301 {
6302 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6303 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
6304 }
6305 | f_arg ',' f_opt_arg(arg_value) opt_args_tail(args_tail)
6306 {
6307 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
6308 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
6309 }
6310 | f_arg ',' f_opt_arg(arg_value) ',' f_arg opt_args_tail(args_tail)
6311 {
6312 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
6313 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
6314 }
6315 | f_arg ',' f_rest_arg opt_args_tail(args_tail)
6316 {
6317 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
6318 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
6319 }
6320 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6321 {
6322 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
6323 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
6324 }
6325 | f_arg opt_args_tail(args_tail)
6326 {
6327 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
6328 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
6329 }
6330 | f_opt_arg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6331 {
6332 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
6333 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
6334 }
6335 | f_opt_arg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6336 {
6337 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
6338 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
6339 }
6340 | f_opt_arg(arg_value) opt_args_tail(args_tail)
6341 {
6342 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
6343 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
6344 }
6345 | f_opt_arg(arg_value) ',' f_arg opt_args_tail(args_tail)
6346 {
6347 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
6348 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
6349 }
6350 | f_rest_arg opt_args_tail(args_tail)
6351 {
6352 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
6353 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
6354 }
6355 | f_rest_arg ',' f_arg opt_args_tail(args_tail)
6356 {
6357 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
6358 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
6359 }
6360 | args_tail
6361 {
6362 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
6363 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
6364 }
6365 | /* none */
6366 {
6367 $$ = new_args_tail(p, 0, 0, 0, &@0);
6368 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6369 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6370 }
6371 ;
6372
6373args_forward : tBDOT3
6374 {
6375#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
6376 $$ = 0;
6377#else
6378 $$ = idFWD_KWREST;
6379#endif
6380 /*% ripper: args_forward! %*/
6381 }
6382 ;
6383
6384f_bad_arg : tCONSTANT
6385 {
6386 static const char mesg[] = "formal argument cannot be a constant";
6387 /*%%%*/
6388 yyerror1(&@1, mesg);
6389 /*% %*/
6390 $$ = 0;
6391 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6392 }
6393 | tIVAR
6394 {
6395 static const char mesg[] = "formal argument cannot be an instance variable";
6396 /*%%%*/
6397 yyerror1(&@1, mesg);
6398 /*% %*/
6399 $$ = 0;
6400 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6401 }
6402 | tGVAR
6403 {
6404 static const char mesg[] = "formal argument cannot be a global variable";
6405 /*%%%*/
6406 yyerror1(&@1, mesg);
6407 /*% %*/
6408 $$ = 0;
6409 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6410 }
6411 | tCVAR
6412 {
6413 static const char mesg[] = "formal argument cannot be a class variable";
6414 /*%%%*/
6415 yyerror1(&@1, mesg);
6416 /*% %*/
6417 $$ = 0;
6418 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6419 }
6420 ;
6421
6422f_norm_arg : f_bad_arg
6423 | tIDENTIFIER
6424 {
6425 VALUE e = formal_argument_error(p, $$ = $1);
6426 if (e) {
6427 /*% ripper[error]: param_error!(?e, $:1) %*/
6428 }
6429 p->max_numparam = ORDINAL_PARAM;
6430 }
6431 ;
6432
6433f_arg_asgn : f_norm_arg
6434 {
6435 arg_var(p, $1);
6436 $$ = $1;
6437 }
6438 ;
6439
6440f_arg_item : f_arg_asgn
6441 {
6442 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6443 /*% ripper: $:1 %*/
6444 }
6445 | tLPAREN f_margs rparen
6446 {
6447 ID tid = internal_id(p);
6448 YYLTYPE loc;
6449 loc.beg_pos = @2.beg_pos;
6450 loc.end_pos = @2.beg_pos;
6451 arg_var(p, tid);
6452 if (dyna_in_block(p)) {
6453 $2->nd_value = NEW_DVAR(tid, &loc);
6454 }
6455 else {
6456 $2->nd_value = NEW_LVAR(tid, &loc);
6457 }
6458 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6459 $$->nd_next = (NODE *)$2;
6460 /*% ripper: mlhs_paren!($:2) %*/
6461 }
6462 ;
6463
6464f_arg : f_arg_item
6465 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6466 | f_arg ',' f_arg_item
6467 {
6468 $$ = $1;
6469 $$->nd_plen++;
6470 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6471 rb_discard_node(p, (NODE *)$3);
6472 /*% ripper: rb_ary_push($:1, $:3) %*/
6473 }
6474 ;
6475
6476
6477f_label : tLABEL
6478 {
6479 VALUE e = formal_argument_error(p, $$ = $1);
6480 if (e) {
6481 $$ = 0;
6482 /*% ripper[error]: param_error!(?e, $:1) %*/
6483 }
6484 /*
6485 * Workaround for Prism::ParseTest#test_filepath for
6486 * "unparser/corpus/literal/def.txt"
6487 *
6488 * See the discussion on https://github.com/ruby/ruby/pull/9923
6489 */
6490 arg_var(p, ifdef_ripper(0, $1));
6491 /*% ripper: $:1 %*/
6492 p->max_numparam = ORDINAL_PARAM;
6493 p->ctxt.in_argdef = 0;
6494 }
6495 ;
6496
6497kwrest_mark : tPOW
6498 | tDSTAR
6499 ;
6500
6501f_no_kwarg : p_kwnorest
6502 {
6503 /*% ripper: nokw_param!(Qnil) %*/
6504 }
6505 ;
6506
6507f_kwrest : kwrest_mark tIDENTIFIER
6508 {
6509 arg_var(p, shadowing_lvar(p, $2));
6510 $$ = $2;
6511 /*% ripper: kwrest_param!($:2) %*/
6512 }
6513 | kwrest_mark
6514 {
6515 arg_var(p, idFWD_KWREST);
6516 $$ = idFWD_KWREST;
6517 /*% ripper: kwrest_param!(Qnil) %*/
6518 }
6519 ;
6520
6521restarg_mark : '*'
6522 | tSTAR
6523 ;
6524
6525f_rest_arg : restarg_mark tIDENTIFIER
6526 {
6527 arg_var(p, shadowing_lvar(p, $2));
6528 $$ = $2;
6529 /*% ripper: rest_param!($:2) %*/
6530 }
6531 | restarg_mark
6532 {
6533 arg_var(p, idFWD_REST);
6534 $$ = idFWD_REST;
6535 /*% ripper: rest_param!(Qnil) %*/
6536 }
6537 ;
6538
6539blkarg_mark : '&'
6540 | tAMPER
6541 ;
6542
6543f_block_arg : blkarg_mark tIDENTIFIER
6544 {
6545 arg_var(p, shadowing_lvar(p, $2));
6546 $$ = $2;
6547 /*% ripper: blockarg!($:2) %*/
6548 }
6549 | blkarg_mark
6550 {
6551 arg_var(p, idFWD_BLOCK);
6552 $$ = idFWD_BLOCK;
6553 /*% ripper: blockarg!(Qnil) %*/
6554 }
6555 ;
6556
6557opt_f_block_arg : ',' f_block_arg
6558 {
6559 $$ = $2;
6560 /*% ripper: $:2 %*/
6561 }
6562 | none
6563 ;
6564
6565
6566singleton : value_expr(singleton_expr)
6567 {
6568 NODE *expr = last_expr_node($1);
6569 switch (nd_type(expr)) {
6570 case NODE_STR:
6571 case NODE_DSTR:
6572 case NODE_XSTR:
6573 case NODE_DXSTR:
6574 case NODE_REGX:
6575 case NODE_DREGX:
6576 case NODE_SYM:
6577 case NODE_LINE:
6578 case NODE_FILE:
6579 case NODE_ENCODING:
6580 case NODE_INTEGER:
6581 case NODE_FLOAT:
6582 case NODE_RATIONAL:
6583 case NODE_IMAGINARY:
6584 case NODE_DSYM:
6585 case NODE_LIST:
6586 case NODE_ZLIST:
6587 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6588 break;
6589 default:
6590 break;
6591 }
6592 $$ = $1;
6593 }
6594 ;
6595
6596singleton_expr : var_ref
6597 | '('
6598 {
6599 SET_LEX_STATE(EXPR_BEG);
6600 p->ctxt.in_argdef = 0;
6601 }
6602 expr rparen
6603 {
6604 p->ctxt.in_argdef = 1;
6605 $$ = $3;
6606 /*% ripper: paren!($:3) %*/
6607 }
6608 ;
6609
6610assoc_list : none
6611 | assocs trailer
6612 {
6613 $$ = $1;
6614 /*% ripper: assoclist_from_args!($:1) %*/
6615 }
6616 ;
6617
6618assocs : assoc
6619 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6620 | assocs ',' assoc
6621 {
6622 NODE *assocs = $1;
6623 NODE *tail = $3;
6624 if (!assocs) {
6625 assocs = tail;
6626 }
6627 else if (tail) {
6628 if (RNODE_LIST(assocs)->nd_head) {
6629 NODE *n = RNODE_LIST(tail)->nd_next;
6630 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6631 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6632 /* DSTAR */
6633 tail = RNODE_HASH(n)->nd_head;
6634 }
6635 }
6636 if (tail) {
6637 assocs = list_concat(assocs, tail);
6638 }
6639 }
6640 $$ = assocs;
6641 /*% ripper: rb_ary_push($:1, $:3) %*/
6642 }
6643 ;
6644
6645assoc : arg_value tASSOC arg_value
6646 {
6647 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6648 /*% ripper: assoc_new!($:1, $:3) %*/
6649 }
6650 | tLABEL arg_value
6651 {
6652 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6653 /*% ripper: assoc_new!($:1, $:2) %*/
6654 }
6655 | tLABEL
6656 {
6657 NODE *val = gettable(p, $1, &@$);
6658 if (!val) val = NEW_ERROR(&@$);
6659 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6660 /*% ripper: assoc_new!($:1, Qnil) %*/
6661 }
6662 | tSTRING_BEG string_contents tLABEL_END arg_value
6663 {
6664 YYLTYPE loc = code_loc_gen(&@1, &@3);
6665 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6666 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6667 }
6668 | tDSTAR arg_value
6669 {
6670 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6671 /*% ripper: assoc_splat!($:2) %*/
6672 }
6673 | tDSTAR
6674 {
6675 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6676 $$ = list_append(p, NEW_LIST(0, &@$),
6677 NEW_LVAR(idFWD_KWREST, &@$));
6678 /*% ripper: assoc_splat!(Qnil) %*/
6679 }
6680 ;
6681
6682%rule %inline operation : ident_or_const
6683 | tFID
6684 ;
6685
6686operation2 : operation
6687 | op
6688 ;
6689
6690operation3 : tIDENTIFIER
6691 | tFID
6692 | op
6693 ;
6694
6695dot_or_colon : '.'
6696 | tCOLON2
6697 ;
6698
6699call_op : '.'
6700 | tANDDOT
6701 ;
6702
6703call_op2 : call_op
6704 | tCOLON2
6705 ;
6706
6707rparen : '\n'? ')'
6708 ;
6709
6710rbracket : '\n'? ']'
6711 ;
6712
6713rbrace : '\n'? '}'
6714 ;
6715
6716trailer : '\n'?
6717 | ','
6718 ;
6719
6720term : ';'
6721 {
6722 yyerrok;
6723 token_flush(p);
6724 if (p->ctxt.in_defined) {
6725 p->ctxt.has_trailing_semicolon = 1;
6726 }
6727 }
6728 | '\n'
6729 {
6730 @$.end_pos = @$.beg_pos;
6731 token_flush(p);
6732 }
6733 ;
6734
6735terms : term
6736 | terms ';' {yyerrok;}
6737 ;
6738
6739none : /* none */
6740 {
6741 $$ = 0;
6742 /*% ripper: Qnil %*/
6743 }
6744 ;
6745%%
6746# undef p
6747# undef yylex
6748# undef yylval
6749# define yylval (*p->lval)
6750
6751static int regx_options(struct parser_params*);
6752static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6753static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6754static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6755static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6756
6757#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6758
6759# define set_yylval_node(x) { \
6760 YYLTYPE _cur_loc; \
6761 rb_parser_set_location(p, &_cur_loc); \
6762 yylval.node = (x); \
6763 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6764}
6765# define set_yylval_str(x) \
6766do { \
6767 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6768 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6769} while(0)
6770# define set_yylval_num(x) { \
6771 yylval.num = (x); \
6772 set_parser_s_value(x); \
6773}
6774# define set_yylval_id(x) (yylval.id = (x))
6775# define set_yylval_name(x) { \
6776 (yylval.id = (x)); \
6777 set_parser_s_value(ID2SYM(x)); \
6778}
6779# define yylval_id() (yylval.id)
6780
6781#define set_yylval_noname() set_yylval_id(keyword_nil)
6782#define has_delayed_token(p) (p->delayed.token != NULL)
6783
6784#ifndef RIPPER
6785#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6786#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6787
6788static bool
6789parser_has_token(struct parser_params *p)
6790{
6791 const char *const pcur = p->lex.pcur;
6792 const char *const ptok = p->lex.ptok;
6793 if (p->keep_tokens && (pcur < ptok)) {
6794 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6795 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6796 }
6797 return pcur > ptok;
6798}
6799
6800static const char *
6801escaped_char(int c)
6802{
6803 switch (c) {
6804 case '"': return "\\\"";
6805 case '\\': return "\\\\";
6806 case '\0': return "\\0";
6807 case '\n': return "\\n";
6808 case '\r': return "\\r";
6809 case '\t': return "\\t";
6810 case '\f': return "\\f";
6811 case '\013': return "\\v";
6812 case '\010': return "\\b";
6813 case '\007': return "\\a";
6814 case '\033': return "\\e";
6815 case '\x7f': return "\\c?";
6816 }
6817 return NULL;
6818}
6819
6820static rb_parser_string_t *
6821rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6822{
6823 rb_encoding *enc = p->enc;
6824 const char *ptr = str->ptr;
6825 const char *pend = ptr + str->len;
6826 const char *prev = ptr;
6827 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6828 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6829
6830 while (ptr < pend) {
6831 unsigned int c;
6832 const char *cc;
6833 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6834 if (!MBCLEN_CHARFOUND_P(n)) {
6835 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6836 n = rb_enc_mbminlen(enc);
6837 if (pend < ptr + n)
6838 n = (int)(pend - ptr);
6839 while (n--) {
6840 c = *ptr & 0xf0 >> 4;
6841 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6842 c = *ptr & 0x0f;
6843 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6844 parser_str_cat(result, charbuf, 4);
6845 prev = ++ptr;
6846 }
6847 continue;
6848 }
6849 n = MBCLEN_CHARFOUND_LEN(n);
6850 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6851 ptr += n;
6852 cc = escaped_char(c);
6853 if (cc) {
6854 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6855 parser_str_cat_cstr(result, cc);
6856 prev = ptr;
6857 }
6858 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6859 }
6860 else {
6861 if (ptr - n > prev) {
6862 parser_str_cat(result, prev, ptr - n - prev);
6863 prev = ptr - n;
6864 }
6865 parser_str_cat(result, prev, ptr - prev);
6866 prev = ptr;
6867 }
6868 }
6869 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6870
6871 return result;
6872}
6873
6874static void
6875parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6876{
6877 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6878 token->id = p->token_id;
6879 token->type_name = parser_token2char(p, t);
6880 token->str = str;
6881 token->loc.beg_pos = p->yylloc->beg_pos;
6882 token->loc.end_pos = p->yylloc->end_pos;
6883 rb_parser_ary_push_ast_token(p, p->tokens, token);
6884 p->token_id++;
6885
6886 if (p->debug) {
6887 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6888 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6889 line, token->id, token->type_name, str_escaped->ptr,
6890 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6891 token->loc.end_pos.lineno, token->loc.end_pos.column);
6892 rb_parser_string_free(p, str_escaped);
6893 }
6894}
6895
6896static void
6897parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6898{
6899 debug_token_line(p, "parser_dispatch_scan_event", line);
6900
6901 if (!parser_has_token(p)) return;
6902
6903 RUBY_SET_YYLLOC(*p->yylloc);
6904
6905 if (p->keep_tokens) {
6906 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6907 parser_append_tokens(p, str, t, line);
6908 }
6909
6910 token_flush(p);
6911}
6912
6913#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6914static void
6915parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6916{
6917 debug_token_line(p, "parser_dispatch_delayed_token", line);
6918
6919 if (!has_delayed_token(p)) return;
6920
6921 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6922
6923 if (p->keep_tokens) {
6924 /* p->delayed.token is freed by rb_parser_tokens_free */
6925 parser_append_tokens(p, p->delayed.token, t, line);
6926 }
6927 else {
6928 rb_parser_string_free(p, p->delayed.token);
6929 }
6930
6931 p->delayed.token = NULL;
6932}
6933#else
6934#define literal_flush(p, ptr) ((void)(ptr))
6935
6936static int
6937ripper_has_scan_event(struct parser_params *p)
6938{
6939 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6940 return p->lex.pcur > p->lex.ptok;
6941}
6942
6943static VALUE
6944ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6945{
6946 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6947 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6948 RUBY_SET_YYLLOC(*p->yylloc);
6949 token_flush(p);
6950 return rval;
6951}
6952
6953static void
6954ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6955{
6956 if (!ripper_has_scan_event(p)) return;
6957
6958 set_parser_s_value(ripper_scan_event_val(p, t));
6959}
6960#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6961
6962static void
6963ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6964{
6965 /* save and adjust the location to delayed token for callbacks */
6966 int saved_line = p->ruby_sourceline;
6967 const char *saved_tokp = p->lex.ptok;
6968 VALUE s_value, str;
6969
6970 if (!has_delayed_token(p)) return;
6971 p->ruby_sourceline = p->delayed.beg_line;
6972 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6973 str = rb_str_new_mutable_parser_string(p->delayed.token);
6974 rb_parser_string_free(p, p->delayed.token);
6975 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
6976 set_parser_s_value(s_value);
6977 p->delayed.token = NULL;
6978 p->ruby_sourceline = saved_line;
6979 p->lex.ptok = saved_tokp;
6980}
6981#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6982#endif /* RIPPER */
6983
6984static inline int
6985is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6986{
6987 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6988}
6989
6990static inline bool
6991peek_word_at(struct parser_params *p, const char *str, size_t len, int at)
6992{
6993 const char *ptr = p->lex.pcur + at;
6994 if (lex_eol_ptr_n_p(p, ptr, len-1)) return false;
6995 if (memcmp(ptr, str, len)) return false;
6996 if (lex_eol_ptr_n_p(p, ptr, len)) return true;
6997 return !is_identchar(p, ptr+len, p->lex.pend, p->enc);
6998}
6999
7000static inline int
7001parser_is_identchar(struct parser_params *p)
7002{
7003 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7004}
7005
7006static inline int
7007parser_isascii(struct parser_params *p)
7008{
7009 return ISASCII(*(p->lex.pcur-1));
7010}
7011
7012static void
7013token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7014{
7015 int column = 1, nonspc = 0, i;
7016 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7017 if (*ptr == '\t') {
7018 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7019 }
7020 column++;
7021 if (*ptr != ' ' && *ptr != '\t') {
7022 nonspc = 1;
7023 }
7024 }
7025
7026 ptinfo->beg = loc->beg_pos;
7027 ptinfo->indent = column;
7028 ptinfo->nonspc = nonspc;
7029}
7030
7031static void
7032token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7033{
7034 token_info *ptinfo;
7035
7036 if (!p->token_info_enabled) return;
7037 ptinfo = ALLOC(token_info);
7038 ptinfo->token = token;
7039 ptinfo->next = p->token_info;
7040 token_info_setup(ptinfo, p->lex.pbeg, loc);
7041
7042 p->token_info = ptinfo;
7043}
7044
7045static void
7046token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7047{
7048 token_info *ptinfo_beg = p->token_info;
7049
7050 if (!ptinfo_beg) return;
7051
7052 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7053 token_info_warn(p, token, ptinfo_beg, 1, loc);
7054
7055 p->token_info = ptinfo_beg->next;
7056 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7057}
7058
7059static void
7060token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7061{
7062 token_info *ptinfo_beg = p->token_info;
7063
7064 if (!ptinfo_beg) return;
7065 p->token_info = ptinfo_beg->next;
7066
7067 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7068 ptinfo_beg->beg.column != beg_pos.column ||
7069 strcmp(ptinfo_beg->token, token)) {
7070 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7071 beg_pos.lineno, beg_pos.column, token,
7072 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7073 ptinfo_beg->token);
7074 }
7075
7076 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7077}
7078
7079static void
7080token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7081{
7082 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7083 if (!p->token_info_enabled) return;
7084 if (!ptinfo_beg) return;
7085 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7086 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7087 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7088 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7089 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7090 rb_warn3L(ptinfo_end->beg.lineno,
7091 "mismatched indentations at '%s' with '%s' at %d",
7092 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7093}
7094
7095static int
7096parser_precise_mbclen(struct parser_params *p, const char *ptr)
7097{
7098 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7099 if (!MBCLEN_CHARFOUND_P(len)) {
7100 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7101 return -1;
7102 }
7103 return len;
7104}
7105
7106#ifndef RIPPER
7107static inline void
7108parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7109{
7110 rb_parser_string_t *str;
7111 int lineno = p->ruby_sourceline;
7112 if (!yylloc) {
7113 return;
7114 }
7115 else if (yylloc->beg_pos.lineno == lineno) {
7116 str = p->lex.lastline;
7117 }
7118 else {
7119 return;
7120 }
7121 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7122}
7123
7124static int
7125parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7126{
7127#if 0
7128 YYLTYPE current;
7129
7130 if (!yylloc) {
7131 yylloc = RUBY_SET_YYLLOC(current);
7132 }
7133 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7134 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7135 yylloc = 0;
7136 }
7137#endif
7138 parser_compile_error(p, yylloc, "%s", msg);
7139 parser_show_error_line(p, yylloc);
7140 return 0;
7141}
7142
7143static int
7144parser_yyerror0(struct parser_params *p, const char *msg)
7145{
7146 YYLTYPE current;
7147 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7148}
7149
7150void
7151ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7152{
7153 VALUE mesg;
7154 const int max_line_margin = 30;
7155 const char *ptr, *ptr_end, *pt, *pb;
7156 const char *pre = "", *post = "", *pend;
7157 const char *code = "", *caret = "";
7158 const char *lim;
7159 const char *const pbeg = PARSER_STRING_PTR(str);
7160 char *buf;
7161 long len;
7162 int i;
7163
7164 if (!yylloc) return;
7165 pend = rb_parser_string_end(str);
7166 if (pend > pbeg && pend[-1] == '\n') {
7167 if (--pend > pbeg && pend[-1] == '\r') --pend;
7168 }
7169
7170 pt = pend;
7171 if (lineno == yylloc->end_pos.lineno &&
7172 (pend - pbeg) > yylloc->end_pos.column) {
7173 pt = pbeg + yylloc->end_pos.column;
7174 }
7175
7176 ptr = ptr_end = pt;
7177 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7178 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7179
7180 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7181 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7182
7183 len = ptr_end - ptr;
7184 if (len > 4) {
7185 if (ptr > pbeg) {
7186 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7187 if (ptr > pbeg) pre = "...";
7188 }
7189 if (ptr_end < pend) {
7190 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7191 if (ptr_end < pend) post = "...";
7192 }
7193 }
7194 pb = pbeg;
7195 if (lineno == yylloc->beg_pos.lineno) {
7196 pb += yylloc->beg_pos.column;
7197 if (pb > pt) pb = pt;
7198 }
7199 if (pb < ptr) pb = ptr;
7200 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7201 return;
7202 }
7203 if (RTEST(errbuf)) {
7204 mesg = rb_attr_get(errbuf, idMesg);
7205 if (char_at_end(p, mesg, '\n') != '\n')
7206 rb_str_cat_cstr(mesg, "\n");
7207 }
7208 else {
7209 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7210 }
7211 if (!errbuf && rb_stderr_tty_p()) {
7212#define CSI_BEGIN "\033["
7213#define CSI_SGR "m"
7214 rb_str_catf(mesg,
7215 CSI_BEGIN""CSI_SGR"%s" /* pre */
7216 CSI_BEGIN"1"CSI_SGR"%.*s"
7217 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7218 CSI_BEGIN";1"CSI_SGR"%.*s"
7219 CSI_BEGIN""CSI_SGR"%s" /* post */
7220 "\n",
7221 pre,
7222 (int)(pb - ptr), ptr,
7223 (int)(pt - pb), pb,
7224 (int)(ptr_end - pt), pt,
7225 post);
7226 }
7227 else {
7228 char *p2;
7229
7230 len = ptr_end - ptr;
7231 lim = pt < pend ? pt : pend;
7232 i = (int)(lim - ptr);
7233 buf = ALLOCA_N(char, i+2);
7234 code = ptr;
7235 caret = p2 = buf;
7236 if (ptr <= pb) {
7237 while (ptr < pb) {
7238 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7239 }
7240 *p2++ = '^';
7241 ptr++;
7242 }
7243 if (lim > ptr) {
7244 memset(p2, '~', (lim - ptr));
7245 p2 += (lim - ptr);
7246 }
7247 *p2 = '\0';
7248 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7249 pre, (int)len, code, post,
7250 pre, caret);
7251 }
7252 if (!errbuf) rb_write_error_str(mesg);
7253}
7254#else
7255
7256static int
7257parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7258{
7259 const char *pcur = 0, *ptok = 0;
7260 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7261 p->ruby_sourceline == yylloc->end_pos.lineno) {
7262 pcur = p->lex.pcur;
7263 ptok = p->lex.ptok;
7264 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7265 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7266 }
7267 parser_yyerror0(p, msg);
7268 if (pcur) {
7269 p->lex.ptok = ptok;
7270 p->lex.pcur = pcur;
7271 }
7272 return 0;
7273}
7274
7275static int
7276parser_yyerror0(struct parser_params *p, const char *msg)
7277{
7278 dispatch1(parse_error, STR_NEW2(msg));
7279 ripper_error(p);
7280 return 0;
7281}
7282
7283static inline void
7284parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7285{
7286}
7287#endif /* !RIPPER */
7288
7289static int
7290vtable_size(const struct vtable *tbl)
7291{
7292 if (!DVARS_TERMINAL_P(tbl)) {
7293 return tbl->pos;
7294 }
7295 else {
7296 return 0;
7297 }
7298}
7299
7300static struct vtable *
7301vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7302{
7303 struct vtable *tbl = ALLOC(struct vtable);
7304 tbl->pos = 0;
7305 tbl->capa = 8;
7306 tbl->tbl = ALLOC_N(ID, tbl->capa);
7307 tbl->prev = prev;
7308#ifndef RIPPER
7309 if (p->debug) {
7310 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7311 }
7312#endif
7313 return tbl;
7314}
7315#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7316
7317static void
7318vtable_free_gen(struct parser_params *p, int line, const char *name,
7319 struct vtable *tbl)
7320{
7321#ifndef RIPPER
7322 if (p->debug) {
7323 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7324 }
7325#endif
7326 if (!DVARS_TERMINAL_P(tbl)) {
7327 if (tbl->tbl) {
7328 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7329 }
7330 ruby_sized_xfree(tbl, sizeof(*tbl));
7331 }
7332}
7333#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7334
7335static void
7336vtable_add_gen(struct parser_params *p, int line, const char *name,
7337 struct vtable *tbl, ID id)
7338{
7339#ifndef RIPPER
7340 if (p->debug) {
7341 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7342 line, name, (void *)tbl, rb_id2name(id));
7343 }
7344#endif
7345 if (DVARS_TERMINAL_P(tbl)) {
7346 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7347 return;
7348 }
7349 if (tbl->pos == tbl->capa) {
7350 tbl->capa = tbl->capa * 2;
7351 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7352 }
7353 tbl->tbl[tbl->pos++] = id;
7354}
7355#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7356
7357static void
7358vtable_pop_gen(struct parser_params *p, int line, const char *name,
7359 struct vtable *tbl, int n)
7360{
7361 if (p->debug) {
7362 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7363 line, name, (void *)tbl, n);
7364 }
7365 if (tbl->pos < n) {
7366 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7367 return;
7368 }
7369 tbl->pos -= n;
7370}
7371#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7372
7373static int
7374vtable_included(const struct vtable * tbl, ID id)
7375{
7376 int i;
7377
7378 if (!DVARS_TERMINAL_P(tbl)) {
7379 for (i = 0; i < tbl->pos; i++) {
7380 if (tbl->tbl[i] == id) {
7381 return i+1;
7382 }
7383 }
7384 }
7385 return 0;
7386}
7387
7388static void parser_prepare(struct parser_params *p);
7389
7390static int
7391e_option_supplied(struct parser_params *p)
7392{
7393 return strcmp(p->ruby_sourcefile, "-e") == 0;
7394}
7395
7396#ifndef RIPPER
7397static NODE *parser_append_options(struct parser_params *p, NODE *node);
7398
7399static VALUE
7400yycompile0(VALUE arg)
7401{
7402 int n;
7403 NODE *tree;
7404 struct parser_params *p = (struct parser_params *)arg;
7405 int cov = FALSE;
7406
7407 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7408 cov = TRUE;
7409 }
7410
7411 if (p->debug_lines) {
7412 p->ast->body.script_lines = p->debug_lines;
7413 }
7414
7415 parser_prepare(p);
7416#define RUBY_DTRACE_PARSE_HOOK(name) \
7417 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7418 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7419 }
7420 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7421 n = yyparse(p);
7422 RUBY_DTRACE_PARSE_HOOK(END);
7423
7424 p->debug_lines = 0;
7425
7426 xfree(p->lex.strterm);
7427 p->lex.strterm = 0;
7428 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7429 if (n || p->error_p) {
7430 VALUE mesg = p->error_buffer;
7431 if (!mesg) {
7432 mesg = syntax_error_new();
7433 }
7434 if (!p->error_tolerant) {
7435 rb_set_errinfo(mesg);
7436 return FALSE;
7437 }
7438 }
7439 tree = p->eval_tree;
7440 if (!tree) {
7441 tree = NEW_NIL(&NULL_LOC);
7442 }
7443 else {
7444 rb_parser_ary_t *tokens = p->tokens;
7445 NODE *prelude;
7446 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7447 prelude = block_append(p, p->eval_tree_begin, body);
7448 RNODE_SCOPE(tree)->nd_body = prelude;
7449 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7450 p->ast->body.coverage_enabled = cov;
7451 if (p->keep_tokens) {
7452 p->ast->node_buffer->tokens = tokens;
7453 p->tokens = NULL;
7454 }
7455 }
7456 p->ast->body.root = tree;
7457 p->ast->body.line_count = p->line_count;
7458 return TRUE;
7459}
7460
7461static rb_ast_t *
7462yycompile(struct parser_params *p, VALUE fname, int line)
7463{
7464 rb_ast_t *ast;
7465 if (NIL_P(fname)) {
7466 p->ruby_sourcefile_string = Qnil;
7467 p->ruby_sourcefile = "(none)";
7468 }
7469 else {
7470 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7471 p->ruby_sourcefile = StringValueCStr(fname);
7472 }
7473 p->ruby_sourceline = line - 1;
7474
7475 p->lvtbl = NULL;
7476
7477 p->ast = ast = rb_ast_new();
7478 compile_callback(yycompile0, (VALUE)p);
7479 p->ast = 0;
7480
7481 while (p->lvtbl) {
7482 local_pop(p);
7483 }
7484
7485 return ast;
7486}
7487#endif /* !RIPPER */
7488
7489static rb_encoding *
7490must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7491{
7492 rb_encoding *enc = rb_parser_str_get_encoding(s);
7493 if (!rb_enc_asciicompat(enc)) {
7494 rb_raise(rb_eArgError, "invalid source encoding");
7495 }
7496 return enc;
7497}
7498
7499static rb_parser_string_t *
7500lex_getline(struct parser_params *p)
7501{
7502 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7503 if (!line) return 0;
7504 p->line_count++;
7505 string_buffer_append(p, line);
7506 must_be_ascii_compatible(p, line);
7507 return line;
7508}
7509
7510#ifndef RIPPER
7511rb_ast_t*
7512rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7513{
7514 p->lex.gets = gets;
7515 p->lex.input = input;
7516 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7517
7518 return yycompile(p, fname, line);
7519}
7520#endif /* !RIPPER */
7521
7522#define STR_FUNC_ESCAPE 0x01
7523#define STR_FUNC_EXPAND 0x02
7524#define STR_FUNC_REGEXP 0x04
7525#define STR_FUNC_QWORDS 0x08
7526#define STR_FUNC_SYMBOL 0x10
7527#define STR_FUNC_INDENT 0x20
7528#define STR_FUNC_LABEL 0x40
7529#define STR_FUNC_LIST 0x4000
7530#define STR_FUNC_TERM 0x8000
7531
7532enum string_type {
7533 str_label = STR_FUNC_LABEL,
7534 str_squote = (0),
7535 str_dquote = (STR_FUNC_EXPAND),
7536 str_xquote = (STR_FUNC_EXPAND),
7537 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7538 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7539 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7540 str_ssym = (STR_FUNC_SYMBOL),
7541 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7542};
7543
7544static rb_parser_string_t *
7545parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7546{
7547 rb_parser_string_t *pstr;
7548
7549 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7550
7551 if (!(func & STR_FUNC_REGEXP)) {
7552 if (rb_parser_is_ascii_string(p, pstr)) {
7553 }
7554 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7555 /* everything is valid in ASCII-8BIT */
7556 enc = rb_ascii8bit_encoding();
7557 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7558 }
7559 }
7560
7561 return pstr;
7562}
7563
7564static int
7565strterm_is_heredoc(rb_strterm_t *strterm)
7566{
7567 return strterm->heredoc;
7568}
7569
7570static rb_strterm_t *
7571new_strterm(struct parser_params *p, int func, int term, int paren)
7572{
7573 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7574 strterm->u.literal.func = func;
7575 strterm->u.literal.term = term;
7576 strterm->u.literal.paren = paren;
7577 return strterm;
7578}
7579
7580static rb_strterm_t *
7581new_heredoc(struct parser_params *p)
7582{
7583 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7584 strterm->heredoc = true;
7585 return strterm;
7586}
7587
7588#define peek(p,c) peek_n(p, (c), 0)
7589#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7590#define peekc(p) peekc_n(p, 0)
7591#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7592
7593#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7594static void
7595parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7596{
7597 debug_token_line(p, "add_delayed_token", line);
7598
7599 if (tok < end) {
7600 if (has_delayed_token(p)) {
7601 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7602 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7603 int end_col = (next_line ? 0 : p->delayed.end_col);
7604 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7605 dispatch_delayed_token(p, tSTRING_CONTENT);
7606 }
7607 }
7608 if (!has_delayed_token(p)) {
7609 p->delayed.token = rb_parser_string_new(p, 0, 0);
7610 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7611 p->delayed.beg_line = p->ruby_sourceline;
7612 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7613 }
7614 parser_str_cat(p->delayed.token, tok, end - tok);
7615 p->delayed.end_line = p->ruby_sourceline;
7616 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7617 p->lex.ptok = end;
7618 }
7619}
7620
7621static void
7622set_lastline(struct parser_params *p, rb_parser_string_t *str)
7623{
7624 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7625 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7626 p->lex.lastline = str;
7627}
7628
7629static int
7630nextline(struct parser_params *p, int set_encoding)
7631{
7632 rb_parser_string_t *str = p->lex.nextline;
7633 p->lex.nextline = 0;
7634 if (!str) {
7635 if (p->eofp)
7636 return -1;
7637
7638 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7639 goto end_of_input;
7640 }
7641
7642 if (!p->lex.input || !(str = lex_getline(p))) {
7643 end_of_input:
7644 p->eofp = 1;
7645 lex_goto_eol(p);
7646 return -1;
7647 }
7648#ifndef RIPPER
7649 if (p->debug_lines) {
7650 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7651 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7652 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7653 }
7654#endif
7655 p->cr_seen = FALSE;
7656 }
7657 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7658 /* after here-document without terminator */
7659 goto end_of_input;
7660 }
7661 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7662 if (p->heredoc_end > 0) {
7663 p->ruby_sourceline = p->heredoc_end;
7664 p->heredoc_end = 0;
7665 }
7666 p->ruby_sourceline++;
7667 set_lastline(p, str);
7668 token_flush(p);
7669 return 0;
7670}
7671
7672static int
7673parser_cr(struct parser_params *p, int c)
7674{
7675 if (peek(p, '\n')) {
7676 p->lex.pcur++;
7677 c = '\n';
7678 }
7679 return c;
7680}
7681
7682static inline int
7683nextc0(struct parser_params *p, int set_encoding)
7684{
7685 int c;
7686
7687 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7688 if (nextline(p, set_encoding)) return -1;
7689 }
7690 c = (unsigned char)*p->lex.pcur++;
7691 if (UNLIKELY(c == '\r')) {
7692 c = parser_cr(p, c);
7693 }
7694
7695 return c;
7696}
7697#define nextc(p) nextc0(p, TRUE)
7698
7699static void
7700pushback(struct parser_params *p, int c)
7701{
7702 if (c == -1) return;
7703 p->eofp = 0;
7704 p->lex.pcur--;
7705 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7706 p->lex.pcur--;
7707 }
7708}
7709
7710#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7711
7712#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7713#define tok(p) (p)->tokenbuf
7714#define toklen(p) (p)->tokidx
7715
7716static int
7717looking_at_eol_p(struct parser_params *p)
7718{
7719 const char *ptr = p->lex.pcur;
7720 while (!lex_eol_ptr_p(p, ptr)) {
7721 int c = (unsigned char)*ptr++;
7722 int eol = (c == '\n' || c == '#');
7723 if (eol || !ISSPACE(c)) {
7724 return eol;
7725 }
7726 }
7727 return TRUE;
7728}
7729
7730static char*
7731newtok(struct parser_params *p)
7732{
7733 p->tokidx = 0;
7734 if (!p->tokenbuf) {
7735 p->toksiz = 60;
7736 p->tokenbuf = ALLOC_N(char, 60);
7737 }
7738 if (p->toksiz > 4096) {
7739 p->toksiz = 60;
7740 REALLOC_N(p->tokenbuf, char, 60);
7741 }
7742 return p->tokenbuf;
7743}
7744
7745static char *
7746tokspace(struct parser_params *p, int n)
7747{
7748 p->tokidx += n;
7749
7750 if (p->tokidx >= p->toksiz) {
7751 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7752 REALLOC_N(p->tokenbuf, char, p->toksiz);
7753 }
7754 return &p->tokenbuf[p->tokidx-n];
7755}
7756
7757static void
7758tokadd(struct parser_params *p, int c)
7759{
7760 p->tokenbuf[p->tokidx++] = (char)c;
7761 if (p->tokidx >= p->toksiz) {
7762 p->toksiz *= 2;
7763 REALLOC_N(p->tokenbuf, char, p->toksiz);
7764 }
7765}
7766
7767static int
7768tok_hex(struct parser_params *p, size_t *numlen)
7769{
7770 int c;
7771
7772 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7773 if (!*numlen) {
7774 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7775 yyerror0("invalid hex escape");
7776 dispatch_scan_event(p, tSTRING_CONTENT);
7777 return 0;
7778 }
7779 p->lex.pcur += *numlen;
7780 return c;
7781}
7782
7783#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7784
7785static int
7786escaped_control_code(int c)
7787{
7788 int c2 = 0;
7789 switch (c) {
7790 case ' ':
7791 c2 = 's';
7792 break;
7793 case '\n':
7794 c2 = 'n';
7795 break;
7796 case '\t':
7797 c2 = 't';
7798 break;
7799 case '\v':
7800 c2 = 'v';
7801 break;
7802 case '\r':
7803 c2 = 'r';
7804 break;
7805 case '\f':
7806 c2 = 'f';
7807 break;
7808 }
7809 return c2;
7810}
7811
7812#define WARN_SPACE_CHAR(c, prefix) \
7813 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7814
7815static int
7816tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7817 int regexp_literal, const char *begin)
7818{
7819 const int wide = !begin;
7820 size_t numlen;
7821 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7822
7823 p->lex.pcur += numlen;
7824 if (p->lex.strterm == NULL ||
7825 strterm_is_heredoc(p->lex.strterm) ||
7826 (p->lex.strterm->u.literal.func != str_regexp)) {
7827 if (!begin) begin = p->lex.pcur;
7828 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7829 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7830 yyerror0("invalid Unicode escape");
7831 dispatch_scan_event(p, tSTRING_CONTENT);
7832 return wide && numlen > 0;
7833 }
7834 if (codepoint > 0x10ffff) {
7835 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7836 yyerror0("invalid Unicode codepoint (too large)");
7837 dispatch_scan_event(p, tSTRING_CONTENT);
7838 return wide;
7839 }
7840 if ((codepoint & 0xfffff800) == 0xd800) {
7841 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7842 yyerror0("invalid Unicode codepoint");
7843 dispatch_scan_event(p, tSTRING_CONTENT);
7844 return wide;
7845 }
7846 }
7847 if (regexp_literal) {
7848 tokcopy(p, (int)numlen);
7849 }
7850 else if (codepoint >= 0x80) {
7851 rb_encoding *utf8 = rb_utf8_encoding();
7852 if (*encp && utf8 != *encp) {
7853 YYLTYPE loc = RUBY_INIT_YYLLOC();
7854 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7855 parser_show_error_line(p, &loc);
7856 return wide;
7857 }
7858 *encp = utf8;
7859 tokaddmbc(p, codepoint, *encp);
7860 }
7861 else {
7862 tokadd(p, codepoint);
7863 }
7864 return TRUE;
7865}
7866
7867static int tokadd_mbchar(struct parser_params *p, int c);
7868
7869static int
7870tokskip_mbchar(struct parser_params *p)
7871{
7872 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7873 if (len > 0) {
7874 p->lex.pcur += len - 1;
7875 }
7876 return len;
7877}
7878
7879/* return value is for ?\u3042 */
7880static void
7881tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7882 int term, int symbol_literal, int regexp_literal)
7883{
7884 /*
7885 * If `term` is not -1, then we allow multiple codepoints in \u{}
7886 * upto `term` byte, otherwise we're parsing a character literal.
7887 * And then add the codepoints to the current token.
7888 */
7889 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7890
7891 const int open_brace = '{', close_brace = '}';
7892
7893 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7894
7895 if (peek(p, open_brace)) { /* handle \u{...} form */
7896 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7897 /*
7898 * Skip parsing validation code and copy bytes as-is until term or
7899 * closing brace, in order to correctly handle extended regexps where
7900 * invalid unicode escapes are allowed in comments. The regexp parser
7901 * does its own validation and will catch any issues.
7902 */
7903 tokadd(p, open_brace);
7904 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7905 int c = peekc(p);
7906 if (c == close_brace) {
7907 tokadd(p, c);
7908 ++p->lex.pcur;
7909 break;
7910 }
7911 else if (c == term) {
7912 break;
7913 }
7914 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7915 tokadd(p, c);
7916 c = *++p->lex.pcur;
7917 }
7918 tokadd_mbchar(p, c);
7919 }
7920 }
7921 else {
7922 const char *second = NULL;
7923 int c, last = nextc(p);
7924 if (lex_eol_p(p)) goto unterminated;
7925 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7926 while (c != close_brace) {
7927 if (c == term) goto unterminated;
7928 if (second == multiple_codepoints)
7929 second = p->lex.pcur;
7930 if (regexp_literal) tokadd(p, last);
7931 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7932 break;
7933 }
7934 while (ISSPACE(c = peekc(p))) {
7935 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7936 last = c;
7937 }
7938 if (term == -1 && !second)
7939 second = multiple_codepoints;
7940 }
7941
7942 if (c != close_brace) {
7943 unterminated:
7944 flush_string_content(p, rb_utf8_encoding(), 0);
7945 yyerror0("unterminated Unicode escape");
7946 dispatch_scan_event(p, tSTRING_CONTENT);
7947 return;
7948 }
7949 if (second && second != multiple_codepoints) {
7950 const char *pcur = p->lex.pcur;
7951 p->lex.pcur = second;
7952 dispatch_scan_event(p, tSTRING_CONTENT);
7953 token_flush(p);
7954 p->lex.pcur = pcur;
7955 yyerror0(multiple_codepoints);
7956 token_flush(p);
7957 }
7958
7959 if (regexp_literal) tokadd(p, close_brace);
7960 nextc(p);
7961 }
7962 }
7963 else { /* handle \uxxxx form */
7964 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
7965 token_flush(p);
7966 return;
7967 }
7968 }
7969}
7970
7971#define ESCAPE_CONTROL 1
7972#define ESCAPE_META 2
7973
7974static int
7975read_escape(struct parser_params *p, int flags, const char *begin)
7976{
7977 int c;
7978 size_t numlen;
7979
7980 switch (c = nextc(p)) {
7981 case '\\': /* Backslash */
7982 return c;
7983
7984 case 'n': /* newline */
7985 return '\n';
7986
7987 case 't': /* horizontal tab */
7988 return '\t';
7989
7990 case 'r': /* carriage-return */
7991 return '\r';
7992
7993 case 'f': /* form-feed */
7994 return '\f';
7995
7996 case 'v': /* vertical tab */
7997 return '\13';
7998
7999 case 'a': /* alarm(bell) */
8000 return '\007';
8001
8002 case 'e': /* escape */
8003 return 033;
8004
8005 case '0': case '1': case '2': case '3': /* octal constant */
8006 case '4': case '5': case '6': case '7':
8007 pushback(p, c);
8008 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8009 p->lex.pcur += numlen;
8010 return c;
8011
8012 case 'x': /* hex constant */
8013 c = tok_hex(p, &numlen);
8014 if (numlen == 0) return 0;
8015 return c;
8016
8017 case 'b': /* backspace */
8018 return '\010';
8019
8020 case 's': /* space */
8021 return ' ';
8022
8023 case 'M':
8024 if (flags & ESCAPE_META) goto eof;
8025 if ((c = nextc(p)) != '-') {
8026 goto eof;
8027 }
8028 if ((c = nextc(p)) == '\\') {
8029 switch (peekc(p)) {
8030 case 'u': case 'U':
8031 nextc(p);
8032 goto eof;
8033 }
8034 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8035 }
8036 else if (c == -1) goto eof;
8037 else if (!ISASCII(c)) {
8038 tokskip_mbchar(p);
8039 goto eof;
8040 }
8041 else {
8042 int c2 = escaped_control_code(c);
8043 if (c2) {
8044 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8045 WARN_SPACE_CHAR(c2, "\\M-");
8046 }
8047 else {
8048 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8049 }
8050 }
8051 else if (ISCNTRL(c)) goto eof;
8052 return ((c & 0xff) | 0x80);
8053 }
8054
8055 case 'C':
8056 if ((c = nextc(p)) != '-') {
8057 goto eof;
8058 }
8059 case 'c':
8060 if (flags & ESCAPE_CONTROL) goto eof;
8061 if ((c = nextc(p))== '\\') {
8062 switch (peekc(p)) {
8063 case 'u': case 'U':
8064 nextc(p);
8065 goto eof;
8066 }
8067 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8068 }
8069 else if (c == '?')
8070 return 0177;
8071 else if (c == -1) goto eof;
8072 else if (!ISASCII(c)) {
8073 tokskip_mbchar(p);
8074 goto eof;
8075 }
8076 else {
8077 int c2 = escaped_control_code(c);
8078 if (c2) {
8079 if (ISCNTRL(c)) {
8080 if (flags & ESCAPE_META) {
8081 WARN_SPACE_CHAR(c2, "\\M-");
8082 }
8083 else {
8084 WARN_SPACE_CHAR(c2, "");
8085 }
8086 }
8087 else {
8088 if (flags & ESCAPE_META) {
8089 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8090 }
8091 else {
8092 WARN_SPACE_CHAR(c2, "\\C-");
8093 }
8094 }
8095 }
8096 else if (ISCNTRL(c)) goto eof;
8097 }
8098 return c & 0x9f;
8099
8100 eof:
8101 case -1:
8102 flush_string_content(p, p->enc, p->lex.pcur - begin);
8103 yyerror0("Invalid escape character syntax");
8104 dispatch_scan_event(p, tSTRING_CONTENT);
8105 return '\0';
8106
8107 default:
8108 if (!ISASCII(c)) {
8109 tokskip_mbchar(p);
8110 goto eof;
8111 }
8112 return c;
8113 }
8114}
8115
8116static void
8117tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8118{
8119 int len = rb_enc_codelen(c, enc);
8120 rb_enc_mbcput(c, tokspace(p, len), enc);
8121}
8122
8123static int
8124tokadd_escape(struct parser_params *p)
8125{
8126 int c;
8127 size_t numlen;
8128 const char *begin = p->lex.pcur;
8129
8130 switch (c = nextc(p)) {
8131 case '\n':
8132 return 0; /* just ignore */
8133
8134 case '0': case '1': case '2': case '3': /* octal constant */
8135 case '4': case '5': case '6': case '7':
8136 {
8137 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8138 if (numlen == 0) goto eof;
8139 p->lex.pcur += numlen;
8140 tokcopy(p, (int)numlen + 1);
8141 }
8142 return 0;
8143
8144 case 'x': /* hex constant */
8145 {
8146 tok_hex(p, &numlen);
8147 if (numlen == 0) return -1;
8148 tokcopy(p, (int)numlen + 2);
8149 }
8150 return 0;
8151
8152 eof:
8153 case -1:
8154 flush_string_content(p, p->enc, p->lex.pcur - begin);
8155 yyerror0("Invalid escape character syntax");
8156 token_flush(p);
8157 return -1;
8158
8159 default:
8160 tokadd(p, '\\');
8161 tokadd(p, c);
8162 }
8163 return 0;
8164}
8165
8166static int
8167char_to_option(int c)
8168{
8169 int val;
8170
8171 switch (c) {
8172 case 'i':
8173 val = RE_ONIG_OPTION_IGNORECASE;
8174 break;
8175 case 'x':
8176 val = RE_ONIG_OPTION_EXTEND;
8177 break;
8178 case 'm':
8179 val = RE_ONIG_OPTION_MULTILINE;
8180 break;
8181 default:
8182 val = 0;
8183 break;
8184 }
8185 return val;
8186}
8187
8188#define ARG_ENCODING_FIXED 16
8189#define ARG_ENCODING_NONE 32
8190#define ENC_ASCII8BIT 1
8191#define ENC_EUC_JP 2
8192#define ENC_Windows_31J 3
8193#define ENC_UTF8 4
8194
8195static int
8196char_to_option_kcode(int c, int *option, int *kcode)
8197{
8198 *option = 0;
8199
8200 switch (c) {
8201 case 'n':
8202 *kcode = ENC_ASCII8BIT;
8203 return (*option = ARG_ENCODING_NONE);
8204 case 'e':
8205 *kcode = ENC_EUC_JP;
8206 break;
8207 case 's':
8208 *kcode = ENC_Windows_31J;
8209 break;
8210 case 'u':
8211 *kcode = ENC_UTF8;
8212 break;
8213 default:
8214 *kcode = -1;
8215 return (*option = char_to_option(c));
8216 }
8217 *option = ARG_ENCODING_FIXED;
8218 return 1;
8219}
8220
8221static int
8222regx_options(struct parser_params *p)
8223{
8224 int kcode = 0;
8225 int kopt = 0;
8226 int options = 0;
8227 int c, opt, kc;
8228
8229 newtok(p);
8230 while (c = nextc(p), ISALPHA(c)) {
8231 if (c == 'o') {
8232 options |= RE_OPTION_ONCE;
8233 }
8234 else if (char_to_option_kcode(c, &opt, &kc)) {
8235 if (kc >= 0) {
8236 if (kc != ENC_ASCII8BIT) kcode = c;
8237 kopt = opt;
8238 }
8239 else {
8240 options |= opt;
8241 }
8242 }
8243 else {
8244 tokadd(p, c);
8245 }
8246 }
8247 options |= kopt;
8248 pushback(p, c);
8249 if (toklen(p)) {
8250 YYLTYPE loc = RUBY_INIT_YYLLOC();
8251 tokfix(p);
8252 compile_error(p, "unknown regexp option%s - %*s",
8253 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8254 parser_show_error_line(p, &loc);
8255 }
8256 return options | RE_OPTION_ENCODING(kcode);
8257}
8258
8259static int
8260tokadd_mbchar(struct parser_params *p, int c)
8261{
8262 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8263 if (len < 0) return -1;
8264 tokadd(p, c);
8265 p->lex.pcur += --len;
8266 if (len > 0) tokcopy(p, len);
8267 return c;
8268}
8269
8270static inline int
8271simple_re_meta(int c)
8272{
8273 switch (c) {
8274 case '$': case '*': case '+': case '.':
8275 case '?': case '^': case '|':
8276 case ')': case ']': case '}': case '>':
8277 return TRUE;
8278 default:
8279 return FALSE;
8280 }
8281}
8282
8283static int
8284parser_update_heredoc_indent(struct parser_params *p, int c)
8285{
8286 if (p->heredoc_line_indent == -1) {
8287 if (c == '\n') p->heredoc_line_indent = 0;
8288 }
8289 else {
8290 if (c == ' ') {
8291 p->heredoc_line_indent++;
8292 return TRUE;
8293 }
8294 else if (c == '\t') {
8295 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8296 p->heredoc_line_indent = w * TAB_WIDTH;
8297 return TRUE;
8298 }
8299 else if (c != '\n') {
8300 if (p->heredoc_indent > p->heredoc_line_indent) {
8301 p->heredoc_indent = p->heredoc_line_indent;
8302 }
8303 p->heredoc_line_indent = -1;
8304 }
8305 else {
8306 /* Whitespace only line has no indentation */
8307 p->heredoc_line_indent = 0;
8308 }
8309 }
8310 return FALSE;
8311}
8312
8313static void
8314parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8315{
8316 YYLTYPE loc = RUBY_INIT_YYLLOC();
8317 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8318 compile_error(p, "%s mixed within %s source", n1, n2);
8319 parser_show_error_line(p, &loc);
8320}
8321
8322static void
8323parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8324{
8325 const char *pos = p->lex.pcur;
8326 p->lex.pcur = beg;
8327 parser_mixed_error(p, enc1, enc2);
8328 p->lex.pcur = pos;
8329}
8330
8331static inline char
8332nibble_char_upper(unsigned int c)
8333{
8334 c &= 0xf;
8335 return c + (c < 10 ? '0' : 'A' - 10);
8336}
8337
8338static int
8339tokadd_string(struct parser_params *p,
8340 int func, int term, int paren, long *nest,
8341 rb_encoding **encp, rb_encoding **enc)
8342{
8343 int c;
8344 bool erred = false;
8345#ifdef RIPPER
8346 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8347 int top_of_line = FALSE;
8348#endif
8349
8350#define mixed_error(enc1, enc2) \
8351 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8352#define mixed_escape(beg, enc1, enc2) \
8353 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8354
8355 while ((c = nextc(p)) != -1) {
8356 if (p->heredoc_indent > 0) {
8357 parser_update_heredoc_indent(p, c);
8358 }
8359#ifdef RIPPER
8360 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8361 pushback(p, c);
8362 break;
8363 }
8364#endif
8365
8366 if (paren && c == paren) {
8367 ++*nest;
8368 }
8369 else if (c == term) {
8370 if (!nest || !*nest) {
8371 pushback(p, c);
8372 break;
8373 }
8374 --*nest;
8375 }
8376 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8377 unsigned char c2 = *p->lex.pcur;
8378 if (c2 == '$' || c2 == '@' || c2 == '{') {
8379 pushback(p, c);
8380 break;
8381 }
8382 }
8383 else if (c == '\\') {
8384 c = nextc(p);
8385 switch (c) {
8386 case '\n':
8387 if (func & STR_FUNC_QWORDS) break;
8388 if (func & STR_FUNC_EXPAND) {
8389 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8390 continue;
8391 if (c == term) {
8392 c = '\\';
8393 goto terminate;
8394 }
8395 }
8396 tokadd(p, '\\');
8397 break;
8398
8399 case '\\':
8400 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8401 break;
8402
8403 case 'u':
8404 if ((func & STR_FUNC_EXPAND) == 0) {
8405 tokadd(p, '\\');
8406 break;
8407 }
8408 tokadd_utf8(p, enc, term,
8409 func & STR_FUNC_SYMBOL,
8410 func & STR_FUNC_REGEXP);
8411 continue;
8412
8413 default:
8414 if (c == -1) return -1;
8415 if (!ISASCII(c)) {
8416 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8417 goto non_ascii;
8418 }
8419 if (func & STR_FUNC_REGEXP) {
8420 switch (c) {
8421 case 'c':
8422 case 'C':
8423 case 'M': {
8424 pushback(p, c);
8425 c = read_escape(p, 0, p->lex.pcur - 1);
8426
8427 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8428 *t++ = '\\';
8429 *t++ = 'x';
8430 *t++ = nibble_char_upper(c >> 4);
8431 *t++ = nibble_char_upper(c);
8432 continue;
8433 }
8434 }
8435
8436 if (c == term && !simple_re_meta(c)) {
8437 tokadd(p, c);
8438 continue;
8439 }
8440 pushback(p, c);
8441 if ((c = tokadd_escape(p)) < 0)
8442 return -1;
8443 if (*enc && *enc != *encp) {
8444 mixed_escape(p->lex.ptok+2, *enc, *encp);
8445 }
8446 continue;
8447 }
8448 else if (func & STR_FUNC_EXPAND) {
8449 pushback(p, c);
8450 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8451 c = read_escape(p, 0, p->lex.pcur - 1);
8452 }
8453 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8454 /* ignore backslashed spaces in %w */
8455 }
8456 else if (c != term && !(paren && c == paren)) {
8457 tokadd(p, '\\');
8458 pushback(p, c);
8459 continue;
8460 }
8461 }
8462 }
8463 else if (!parser_isascii(p)) {
8464 non_ascii:
8465 if (!*enc) {
8466 *enc = *encp;
8467 }
8468 else if (*enc != *encp) {
8469 mixed_error(*enc, *encp);
8470 continue;
8471 }
8472 if (tokadd_mbchar(p, c) == -1) return -1;
8473 continue;
8474 }
8475 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8476 pushback(p, c);
8477 break;
8478 }
8479 if (c & 0x80) {
8480 if (!*enc) {
8481 *enc = *encp;
8482 }
8483 else if (*enc != *encp) {
8484 mixed_error(*enc, *encp);
8485 continue;
8486 }
8487 }
8488 tokadd(p, c);
8489#ifdef RIPPER
8490 top_of_line = (c == '\n');
8491#endif
8492 }
8493 terminate:
8494 if (*enc) *encp = *enc;
8495 return c;
8496}
8497
8498#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8499
8500static void
8501flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8502{
8503 p->lex.pcur -= back;
8504 if (has_delayed_token(p)) {
8505 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8506 if (len > 0) {
8507 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8508 p->delayed.end_line = p->ruby_sourceline;
8509 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8510 }
8511 dispatch_delayed_token(p, tSTRING_CONTENT);
8512 p->lex.ptok = p->lex.pcur;
8513 }
8514 dispatch_scan_event(p, tSTRING_CONTENT);
8515 p->lex.pcur += back;
8516}
8517
8518/* this can be shared with ripper, since it's independent from struct
8519 * parser_params. */
8520#ifndef RIPPER
8521#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8522#define SPECIAL_PUNCT(idx) ( \
8523 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8524 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8525 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8526 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8527 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8528 BIT('0', idx))
8529const uint_least32_t ruby_global_name_punct_bits[] = {
8530 SPECIAL_PUNCT(0),
8531 SPECIAL_PUNCT(1),
8532 SPECIAL_PUNCT(2),
8533};
8534#undef BIT
8535#undef SPECIAL_PUNCT
8536#endif
8537
8538static enum yytokentype
8539parser_peek_variable_name(struct parser_params *p)
8540{
8541 int c;
8542 const char *ptr = p->lex.pcur;
8543
8544 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8545 c = *ptr++;
8546 switch (c) {
8547 case '$':
8548 if ((c = *ptr) == '-') {
8549 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8550 c = *ptr;
8551 }
8552 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8553 return tSTRING_DVAR;
8554 }
8555 break;
8556 case '@':
8557 if ((c = *ptr) == '@') {
8558 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8559 c = *ptr;
8560 }
8561 break;
8562 case '{':
8563 p->lex.pcur = ptr;
8564 p->command_start = TRUE;
8565 yylval.state = p->lex.state;
8566 return tSTRING_DBEG;
8567 default:
8568 return 0;
8569 }
8570 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8571 return tSTRING_DVAR;
8572 return 0;
8573}
8574
8575#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8576#define IS_END() IS_lex_state(EXPR_END_ANY)
8577#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8578#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8579#define IS_LABEL_POSSIBLE() (\
8580 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8581 IS_ARG())
8582#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8583#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8584
8585static inline enum yytokentype
8586parser_string_term(struct parser_params *p, int func)
8587{
8588 xfree(p->lex.strterm);
8589 p->lex.strterm = 0;
8590 if (func & STR_FUNC_REGEXP) {
8591 set_yylval_num(regx_options(p));
8592 dispatch_scan_event(p, tREGEXP_END);
8593 SET_LEX_STATE(EXPR_END);
8594 return tREGEXP_END;
8595 }
8596 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8597 nextc(p);
8598 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8599 return tLABEL_END;
8600 }
8601 SET_LEX_STATE(EXPR_END);
8602 return tSTRING_END;
8603}
8604
8605static enum yytokentype
8606parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8607{
8608 int func = quote->func;
8609 int term = quote->term;
8610 int paren = quote->paren;
8611 int c, space = 0;
8612 rb_encoding *enc = p->enc;
8613 rb_encoding *base_enc = 0;
8614 rb_parser_string_t *lit;
8615
8616 if (func & STR_FUNC_TERM) {
8617 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8618 SET_LEX_STATE(EXPR_END);
8619 xfree(p->lex.strterm);
8620 p->lex.strterm = 0;
8621 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8622 }
8623 c = nextc(p);
8624 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8625 while (c != '\n' && ISSPACE(c = nextc(p)));
8626 space = 1;
8627 }
8628 if (func & STR_FUNC_LIST) {
8629 quote->func &= ~STR_FUNC_LIST;
8630 space = 1;
8631 }
8632 if (c == term && !quote->nest) {
8633 if (func & STR_FUNC_QWORDS) {
8634 quote->func |= STR_FUNC_TERM;
8635 pushback(p, c); /* dispatch the term at tSTRING_END */
8636 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8637 return ' ';
8638 }
8639 return parser_string_term(p, func);
8640 }
8641 if (space) {
8642 if (!ISSPACE(c)) pushback(p, c);
8643 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8644 return ' ';
8645 }
8646 newtok(p);
8647 if ((func & STR_FUNC_EXPAND) && c == '#') {
8648 enum yytokentype t = parser_peek_variable_name(p);
8649 if (t) return t;
8650 tokadd(p, '#');
8651 c = nextc(p);
8652 }
8653 pushback(p, c);
8654 if (tokadd_string(p, func, term, paren, &quote->nest,
8655 &enc, &base_enc) == -1) {
8656 if (p->eofp) {
8657#ifndef RIPPER
8658# define unterminated_literal(mesg) yyerror0(mesg)
8659#else
8660# define unterminated_literal(mesg) compile_error(p, mesg)
8661#endif
8662 literal_flush(p, p->lex.pcur);
8663 if (func & STR_FUNC_QWORDS) {
8664 /* no content to add, bailing out here */
8665 unterminated_literal("unterminated list meets end of file");
8666 xfree(p->lex.strterm);
8667 p->lex.strterm = 0;
8668 return tSTRING_END;
8669 }
8670 if (func & STR_FUNC_REGEXP) {
8671 unterminated_literal("unterminated regexp meets end of file");
8672 }
8673 else {
8674 unterminated_literal("unterminated string meets end of file");
8675 }
8676 quote->func |= STR_FUNC_TERM;
8677 }
8678 }
8679
8680 tokfix(p);
8681 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8682 set_yylval_str(lit);
8683 flush_string_content(p, enc, 0);
8684
8685 return tSTRING_CONTENT;
8686}
8687
8688static enum yytokentype
8689heredoc_identifier(struct parser_params *p)
8690{
8691 /*
8692 * term_len is length of `<<"END"` except `END`,
8693 * in this case term_len is 4 (<, <, " and ").
8694 */
8695 long len, offset = p->lex.pcur - p->lex.pbeg;
8696 int c = nextc(p), term, func = 0, quote = 0;
8697 enum yytokentype token = tSTRING_BEG;
8698 int indent = 0;
8699
8700 if (c == '-') {
8701 c = nextc(p);
8702 func = STR_FUNC_INDENT;
8703 offset++;
8704 }
8705 else if (c == '~') {
8706 c = nextc(p);
8707 func = STR_FUNC_INDENT;
8708 offset++;
8709 indent = INT_MAX;
8710 }
8711 switch (c) {
8712 case '\'':
8713 func |= str_squote; goto quoted;
8714 case '"':
8715 func |= str_dquote; goto quoted;
8716 case '`':
8717 token = tXSTRING_BEG;
8718 func |= str_xquote; goto quoted;
8719
8720 quoted:
8721 quote++;
8722 offset++;
8723 term = c;
8724 len = 0;
8725 while ((c = nextc(p)) != term) {
8726 if (c == -1 || c == '\r' || c == '\n') {
8727 yyerror0("unterminated here document identifier");
8728 return -1;
8729 }
8730 }
8731 break;
8732
8733 default:
8734 if (!parser_is_identchar(p)) {
8735 pushback(p, c);
8736 if (func & STR_FUNC_INDENT) {
8737 pushback(p, indent > 0 ? '~' : '-');
8738 }
8739 return 0;
8740 }
8741 func |= str_dquote;
8742 do {
8743 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8744 if (n < 0) return 0;
8745 p->lex.pcur += --n;
8746 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8747 pushback(p, c);
8748 break;
8749 }
8750
8751 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8752 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8753 yyerror0("too long here document identifier");
8754 dispatch_scan_event(p, tHEREDOC_BEG);
8755 lex_goto_eol(p);
8756
8757 p->lex.strterm = new_heredoc(p);
8758 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8759 here->offset = offset;
8760 here->sourceline = p->ruby_sourceline;
8761 here->length = (unsigned)len;
8762 here->quote = quote;
8763 here->func = func;
8764 here->lastline = p->lex.lastline;
8765
8766 token_flush(p);
8767 p->heredoc_indent = indent;
8768 p->heredoc_line_indent = 0;
8769 return token;
8770}
8771
8772static void
8773heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8774{
8775 rb_parser_string_t *line;
8776 rb_strterm_t *term = p->lex.strterm;
8777
8778 p->lex.strterm = 0;
8779 line = here->lastline;
8780 p->lex.lastline = line;
8781 p->lex.pbeg = PARSER_STRING_PTR(line);
8782 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8783 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8784 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8785 p->heredoc_end = p->ruby_sourceline;
8786 p->ruby_sourceline = (int)here->sourceline;
8787 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8788 p->eofp = 0;
8789 xfree(term);
8790}
8791
8792static int
8793dedent_string_column(const char *str, long len, int width)
8794{
8795 int i, col = 0;
8796
8797 for (i = 0; i < len && col < width; i++) {
8798 if (str[i] == ' ') {
8799 col++;
8800 }
8801 else if (str[i] == '\t') {
8802 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8803 if (n > width) break;
8804 col = n;
8805 }
8806 else {
8807 break;
8808 }
8809 }
8810
8811 return i;
8812}
8813
8814static int
8815dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8816{
8817 char *str;
8818 long len;
8819 int i;
8820
8821 len = PARSER_STRING_LEN(string);
8822 str = PARSER_STRING_PTR(string);
8823
8824 i = dedent_string_column(str, len, width);
8825 if (!i) return 0;
8826
8827 rb_parser_str_modify(string);
8828 str = PARSER_STRING_PTR(string);
8829 if (PARSER_STRING_LEN(string) != len)
8830 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8831 MEMMOVE(str, str + i, char, len - i);
8832 rb_parser_str_set_len(p, string, len - i);
8833 return i;
8834}
8835
8836static NODE *
8837heredoc_dedent(struct parser_params *p, NODE *root)
8838{
8839 NODE *node, *str_node, *prev_node;
8840 int indent = p->heredoc_indent;
8841 rb_parser_string_t *prev_lit = 0;
8842
8843 if (indent <= 0) return root;
8844 if (!root) return root;
8845
8846 prev_node = node = str_node = root;
8847 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8848
8849 while (str_node) {
8850 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8851 if (nd_fl_newline(str_node)) {
8852 dedent_string(p, lit, indent);
8853 }
8854 if (!prev_lit) {
8855 prev_lit = lit;
8856 }
8857 else if (!literal_concat0(p, prev_lit, lit)) {
8858 return 0;
8859 }
8860 else {
8861 NODE *end = RNODE_LIST(node)->as.nd_end;
8862 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8863 if (!node) {
8864 if (nd_type_p(prev_node, NODE_DSTR))
8865 nd_set_type(prev_node, NODE_STR);
8866 break;
8867 }
8868 RNODE_LIST(node)->as.nd_end = end;
8869 goto next_str;
8870 }
8871
8872 str_node = 0;
8873 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8874 next_str:
8875 if (!nd_type_p(node, NODE_LIST)) break;
8876 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8877 enum node_type type = nd_type(str_node);
8878 if (type == NODE_STR || type == NODE_DSTR) break;
8879 prev_lit = 0;
8880 str_node = 0;
8881 }
8882 }
8883 }
8884 return root;
8885}
8886
8887static int
8888whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8889{
8890 const char *beg = p->lex.pbeg;
8891 const char *ptr = p->lex.pend;
8892
8893 if (ptr - beg < len) return FALSE;
8894 if (ptr > beg && ptr[-1] == '\n') {
8895 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8896 if (ptr - beg < len) return FALSE;
8897 }
8898 if (strncmp(eos, ptr -= len, len)) return FALSE;
8899 if (indent) {
8900 while (beg < ptr && ISSPACE(*beg)) beg++;
8901 }
8902 return beg == ptr;
8903}
8904
8905static int
8906word_match_p(struct parser_params *p, const char *word, long len)
8907{
8908 if (strncmp(p->lex.pcur, word, len)) return 0;
8909 if (lex_eol_n_p(p, len)) return 1;
8910 int c = (unsigned char)p->lex.pcur[len];
8911 if (ISSPACE(c)) return 1;
8912 switch (c) {
8913 case '\0': case '\004': case '\032': return 1;
8914 }
8915 return 0;
8916}
8917
8918#define NUM_SUFFIX_R (1<<0)
8919#define NUM_SUFFIX_I (1<<1)
8920#define NUM_SUFFIX_ALL 3
8921
8922static int
8923number_literal_suffix(struct parser_params *p, int mask)
8924{
8925 int c, result = 0;
8926 const char *lastp = p->lex.pcur;
8927
8928 while ((c = nextc(p)) != -1) {
8929 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8930 result |= (mask & NUM_SUFFIX_I);
8931 mask &= ~NUM_SUFFIX_I;
8932 /* r after i, rational of complex is disallowed */
8933 mask &= ~NUM_SUFFIX_R;
8934 continue;
8935 }
8936 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8937 result |= (mask & NUM_SUFFIX_R);
8938 mask &= ~NUM_SUFFIX_R;
8939 continue;
8940 }
8941 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8942 p->lex.pcur = lastp;
8943 literal_flush(p, p->lex.pcur);
8944 return 0;
8945 }
8946 pushback(p, c);
8947 break;
8948 }
8949 return result;
8950}
8951
8952static enum yytokentype
8953set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8954{
8955 enum rb_numeric_type numeric_type = integer_literal;
8956
8957 if (type == tFLOAT) {
8958 numeric_type = float_literal;
8959 }
8960
8961 if (suffix & NUM_SUFFIX_R) {
8962 type = tRATIONAL;
8963 numeric_type = rational_literal;
8964 }
8965 if (suffix & NUM_SUFFIX_I) {
8966 type = tIMAGINARY;
8967 }
8968
8969 switch (type) {
8970 case tINTEGER:
8971 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
8972 break;
8973 case tFLOAT:
8974 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
8975 break;
8976 case tRATIONAL:
8977 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
8978 break;
8979 case tIMAGINARY:
8980 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
8981 (void)numeric_type; /* for ripper */
8982 break;
8983 default:
8984 rb_bug("unexpected token: %d", type);
8985 }
8986 SET_LEX_STATE(EXPR_END);
8987 return type;
8988}
8989
8990#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8991static void
8992parser_dispatch_heredoc_end(struct parser_params *p, int line)
8993{
8994 if (has_delayed_token(p))
8995 dispatch_delayed_token(p, tSTRING_CONTENT);
8996
8997#ifdef RIPPER
8998 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8999 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9000#else
9001 if (p->keep_tokens) {
9002 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9003 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9004 parser_append_tokens(p, str, tHEREDOC_END, line);
9005 }
9006#endif
9007
9008 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9009 lex_goto_eol(p);
9010 token_flush(p);
9011}
9012
9013static enum yytokentype
9014here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9015{
9016 int c, func, indent = 0;
9017 const char *eos, *ptr, *ptr_end;
9018 long len;
9019 rb_parser_string_t *str = 0;
9020 rb_encoding *enc = p->enc;
9021 rb_encoding *base_enc = 0;
9022 int bol;
9023#ifdef RIPPER
9024 VALUE s_value;
9025#endif
9026
9027 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9028 len = here->length;
9029 indent = (func = here->func) & STR_FUNC_INDENT;
9030
9031 if ((c = nextc(p)) == -1) {
9032 error:
9033#ifdef RIPPER
9034 if (!has_delayed_token(p)) {
9035 dispatch_scan_event(p, tSTRING_CONTENT);
9036 }
9037 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9038 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9039 if (!(func & STR_FUNC_REGEXP)) {
9040 int cr = ENC_CODERANGE_UNKNOWN;
9041 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9042 if (cr != ENC_CODERANGE_7BIT &&
9043 rb_is_usascii_enc(p->enc) &&
9044 enc != rb_utf8_encoding()) {
9045 enc = rb_ascii8bit_encoding();
9046 }
9047 }
9048 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9049 }
9050 dispatch_delayed_token(p, tSTRING_CONTENT);
9051 }
9052 else {
9053 dispatch_delayed_token(p, tSTRING_CONTENT);
9054 dispatch_scan_event(p, tSTRING_CONTENT);
9055 }
9056 lex_goto_eol(p);
9057#endif
9058 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9059 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9060 (int)len, eos);
9061 token_flush(p);
9062 SET_LEX_STATE(EXPR_END);
9063 return tSTRING_END;
9064 }
9065 bol = was_bol(p);
9066 if (!bol) {
9067 /* not beginning of line, cannot be the terminator */
9068 }
9069 else if (p->heredoc_line_indent == -1) {
9070 /* `heredoc_line_indent == -1` means
9071 * - "after an interpolation in the same line", or
9072 * - "in a continuing line"
9073 */
9074 p->heredoc_line_indent = 0;
9075 }
9076 else if (whole_match_p(p, eos, len, indent)) {
9077 dispatch_heredoc_end(p);
9078 restore:
9079 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9080 token_flush(p);
9081 SET_LEX_STATE(EXPR_END);
9082 return tSTRING_END;
9083 }
9084
9085 if (!(func & STR_FUNC_EXPAND)) {
9086 do {
9087 ptr = PARSER_STRING_PTR(p->lex.lastline);
9088 ptr_end = p->lex.pend;
9089 if (ptr_end > ptr) {
9090 switch (ptr_end[-1]) {
9091 case '\n':
9092 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9093 ptr_end++;
9094 break;
9095 }
9096 case '\r':
9097 --ptr_end;
9098 }
9099 }
9100
9101 if (p->heredoc_indent > 0) {
9102 long i = 0;
9103 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9104 i++;
9105 p->heredoc_line_indent = 0;
9106 }
9107
9108 if (str)
9109 parser_str_cat(str, ptr, ptr_end - ptr);
9110 else
9111 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9112 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9113 lex_goto_eol(p);
9114 if (p->heredoc_indent > 0) {
9115 goto flush_str;
9116 }
9117 if (nextc(p) == -1) {
9118 if (str) {
9119 rb_parser_string_free(p, str);
9120 str = 0;
9121 }
9122 goto error;
9123 }
9124 } while (!whole_match_p(p, eos, len, indent));
9125 }
9126 else {
9127 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9128 newtok(p);
9129 if (c == '#') {
9130 enum yytokentype t = parser_peek_variable_name(p);
9131 if (p->heredoc_line_indent != -1) {
9132 if (p->heredoc_indent > p->heredoc_line_indent) {
9133 p->heredoc_indent = p->heredoc_line_indent;
9134 }
9135 p->heredoc_line_indent = -1;
9136 }
9137 if (t) return t;
9138 tokadd(p, '#');
9139 c = nextc(p);
9140 }
9141 do {
9142 pushback(p, c);
9143 enc = p->enc;
9144 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9145 if (p->eofp) goto error;
9146 goto restore;
9147 }
9148 if (c != '\n') {
9149 if (c == '\\') p->heredoc_line_indent = -1;
9150 flush:
9151 str = STR_NEW3(tok(p), toklen(p), enc, func);
9152 flush_str:
9153 set_yylval_str(str);
9154#ifndef RIPPER
9155 if (bol) nd_set_fl_newline(yylval.node);
9156#endif
9157 flush_string_content(p, enc, 0);
9158 return tSTRING_CONTENT;
9159 }
9160 tokadd(p, nextc(p));
9161 if (p->heredoc_indent > 0) {
9162 lex_goto_eol(p);
9163 goto flush;
9164 }
9165 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9166 if ((c = nextc(p)) == -1) goto error;
9167 } while (!whole_match_p(p, eos, len, indent));
9168 str = STR_NEW3(tok(p), toklen(p), enc, func);
9169 }
9170 dispatch_heredoc_end(p);
9171 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9172 token_flush(p);
9173 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9174#ifdef RIPPER
9175 /* Preserve s_value for set_yylval_str */
9176 s_value = p->s_value;
9177#endif
9178 set_yylval_str(str);
9179#ifdef RIPPER
9180 set_parser_s_value(s_value);
9181#endif
9182
9183#ifndef RIPPER
9184 if (bol) nd_set_fl_newline(yylval.node);
9185#endif
9186 return tSTRING_CONTENT;
9187}
9188
9189#include "lex.c"
9190
9191static int
9192arg_ambiguous(struct parser_params *p, char c)
9193{
9194#ifndef RIPPER
9195 if (c == '/') {
9196 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after '%c' operator", WARN_I(c));
9197 }
9198 else {
9199 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9200 }
9201#else
9202 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9203#endif
9204 return TRUE;
9205}
9206
9207/* returns true value if formal argument error;
9208 * Qtrue, or error message if ripper */
9209static VALUE
9210formal_argument_error(struct parser_params *p, ID id)
9211{
9212 switch (id_type(id)) {
9213 case ID_LOCAL:
9214 break;
9215#ifndef RIPPER
9216# define ERR(mesg) (yyerror0(mesg), Qtrue)
9217#else
9218# define ERR(mesg) WARN_S(mesg)
9219#endif
9220 case ID_CONST:
9221 return ERR("formal argument cannot be a constant");
9222 case ID_INSTANCE:
9223 return ERR("formal argument cannot be an instance variable");
9224 case ID_GLOBAL:
9225 return ERR("formal argument cannot be a global variable");
9226 case ID_CLASS:
9227 return ERR("formal argument cannot be a class variable");
9228 default:
9229 return ERR("formal argument must be local variable");
9230#undef ERR
9231 }
9232 shadowing_lvar(p, id);
9233
9234 return Qfalse;
9235}
9236
9237static int
9238lvar_defined(struct parser_params *p, ID id)
9239{
9240 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9241}
9242
9243/* emacsen -*- hack */
9244static long
9245parser_encode_length(struct parser_params *p, const char *name, long len)
9246{
9247 long nlen;
9248
9249 if (len > 5 && name[nlen = len - 5] == '-') {
9250 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9251 return nlen;
9252 }
9253 if (len > 4 && name[nlen = len - 4] == '-') {
9254 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9255 return nlen;
9256 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9257 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9258 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9259 return nlen;
9260 }
9261 return len;
9262}
9263
9264static void
9265parser_set_encode(struct parser_params *p, const char *name)
9266{
9267 rb_encoding *enc;
9268 VALUE excargs[3];
9269 int idx = 0;
9270
9271 const char *wrong = 0;
9272 switch (*name) {
9273 case 'e': case 'E': wrong = "external"; break;
9274 case 'i': case 'I': wrong = "internal"; break;
9275 case 'f': case 'F': wrong = "filesystem"; break;
9276 case 'l': case 'L': wrong = "locale"; break;
9277 }
9278 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9279 idx = rb_enc_find_index(name);
9280 if (idx < 0) {
9281 unknown:
9282 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9283 error:
9284 excargs[0] = rb_eArgError;
9285 excargs[2] = rb_make_backtrace();
9286 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9287 VALUE exc = rb_make_exception(3, excargs);
9288 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9289
9290 rb_ast_free(p->ast);
9291 p->ast = NULL;
9292
9293 rb_exc_raise(exc);
9294 }
9295 enc = rb_enc_from_index(idx);
9296 if (!rb_enc_asciicompat(enc)) {
9297 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9298 goto error;
9299 }
9300 p->enc = enc;
9301#ifndef RIPPER
9302 if (p->debug_lines) {
9303 long i;
9304 for (i = 0; i < p->debug_lines->len; i++) {
9305 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9306 }
9307 }
9308#endif
9309}
9310
9311static bool
9312comment_at_top(struct parser_params *p)
9313{
9314 if (p->token_seen) return false;
9315 return (p->line_count == (p->has_shebang ? 2 : 1));
9316}
9317
9318typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9319typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9320
9321static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9322
9323static void
9324magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9325{
9326 if (!comment_at_top(p)) {
9327 return;
9328 }
9329 parser_set_encode(p, val);
9330}
9331
9332static int
9333parser_get_bool(struct parser_params *p, const char *name, const char *val)
9334{
9335 switch (*val) {
9336 case 't': case 'T':
9337 if (STRCASECMP(val, "true") == 0) {
9338 return TRUE;
9339 }
9340 break;
9341 case 'f': case 'F':
9342 if (STRCASECMP(val, "false") == 0) {
9343 return FALSE;
9344 }
9345 break;
9346 }
9347 return parser_invalid_pragma_value(p, name, val);
9348}
9349
9350static int
9351parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9352{
9353 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9354 return -1;
9355}
9356
9357static void
9358parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9359{
9360 int b = parser_get_bool(p, name, val);
9361 if (b >= 0) p->token_info_enabled = b;
9362}
9363
9364static void
9365parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9366{
9367 int b;
9368
9369 if (p->token_seen) {
9370 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9371 return;
9372 }
9373
9374 b = parser_get_bool(p, name, val);
9375 if (b < 0) return;
9376
9377 p->frozen_string_literal = b;
9378}
9379
9380static void
9381parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9382{
9383 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9384 if (*s == ' ' || *s == '\t') continue;
9385 if (*s == '#') break;
9386 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9387 return;
9388 }
9389
9390 switch (*val) {
9391 case 'n': case 'N':
9392 if (STRCASECMP(val, "none") == 0) {
9393 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9394 return;
9395 }
9396 break;
9397 case 'l': case 'L':
9398 if (STRCASECMP(val, "literal") == 0) {
9399 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9400 return;
9401 }
9402 break;
9403 case 'e': case 'E':
9404 if (STRCASECMP(val, "experimental_copy") == 0) {
9405 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9406 return;
9407 }
9408 if (STRCASECMP(val, "experimental_everything") == 0) {
9409 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9410 return;
9411 }
9412 break;
9413 }
9414 parser_invalid_pragma_value(p, name, val);
9415}
9416
9417# if WARN_PAST_SCOPE
9418static void
9419parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9420{
9421 int b = parser_get_bool(p, name, val);
9422 if (b >= 0) p->past_scope_enabled = b;
9423}
9424# endif
9425
9426struct magic_comment {
9427 const char *name;
9428 rb_magic_comment_setter_t func;
9429 rb_magic_comment_length_t length;
9430};
9431
9432static const struct magic_comment magic_comments[] = {
9433 {"coding", magic_comment_encoding, parser_encode_length},
9434 {"encoding", magic_comment_encoding, parser_encode_length},
9435 {"frozen_string_literal", parser_set_frozen_string_literal},
9436 {"shareable_constant_value", parser_set_shareable_constant_value},
9437 {"warn_indent", parser_set_token_info},
9438# if WARN_PAST_SCOPE
9439 {"warn_past_scope", parser_set_past_scope},
9440# endif
9441};
9442
9443static const char *
9444magic_comment_marker(const char *str, long len)
9445{
9446 long i = 2;
9447
9448 while (i < len) {
9449 switch (str[i]) {
9450 case '-':
9451 if (str[i-1] == '*' && str[i-2] == '-') {
9452 return str + i + 1;
9453 }
9454 i += 2;
9455 break;
9456 case '*':
9457 if (i + 1 >= len) return 0;
9458 if (str[i+1] != '-') {
9459 i += 4;
9460 }
9461 else if (str[i-1] != '-') {
9462 i += 2;
9463 }
9464 else {
9465 return str + i + 2;
9466 }
9467 break;
9468 default:
9469 i += 3;
9470 break;
9471 }
9472 }
9473 return 0;
9474}
9475
9476static int
9477parser_magic_comment(struct parser_params *p, const char *str, long len)
9478{
9479 int indicator = 0;
9480 VALUE name = 0, val = 0;
9481 const char *beg, *end, *vbeg, *vend;
9482#define str_copy(_s, _p, _n) ((_s) \
9483 ? (void)(rb_str_resize((_s), (_n)), \
9484 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9485 : (void)((_s) = STR_NEW((_p), (_n))))
9486
9487 if (len <= 7) return FALSE;
9488 if (!!(beg = magic_comment_marker(str, len))) {
9489 if (!(end = magic_comment_marker(beg, str + len - beg)))
9490 return FALSE;
9491 indicator = TRUE;
9492 str = beg;
9493 len = end - beg - 3;
9494 }
9495
9496 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9497 while (len > 0) {
9498 const struct magic_comment *mc = magic_comments;
9499 char *s;
9500 int i;
9501 long n = 0;
9502
9503 for (; len > 0 && *str; str++, --len) {
9504 switch (*str) {
9505 case '\'': case '"': case ':': case ';':
9506 continue;
9507 }
9508 if (!ISSPACE(*str)) break;
9509 }
9510 for (beg = str; len > 0; str++, --len) {
9511 switch (*str) {
9512 case '\'': case '"': case ':': case ';':
9513 break;
9514 default:
9515 if (ISSPACE(*str)) break;
9516 continue;
9517 }
9518 break;
9519 }
9520 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9521 if (!len) break;
9522 if (*str != ':') {
9523 if (!indicator) return FALSE;
9524 continue;
9525 }
9526
9527 do str++; while (--len > 0 && ISSPACE(*str));
9528 if (!len) break;
9529 const char *tok_beg = str;
9530 if (*str == '"') {
9531 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9532 if (*str == '\\') {
9533 --len;
9534 ++str;
9535 }
9536 }
9537 vend = str;
9538 if (len) {
9539 --len;
9540 ++str;
9541 }
9542 }
9543 else {
9544 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9545 vend = str;
9546 }
9547 const char *tok_end = str;
9548 if (indicator) {
9549 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9550 }
9551 else {
9552 while (len > 0 && (ISSPACE(*str))) --len, str++;
9553 if (len) return FALSE;
9554 }
9555
9556 n = end - beg;
9557 str_copy(name, beg, n);
9558 s = RSTRING_PTR(name);
9559 for (i = 0; i < n; ++i) {
9560 if (s[i] == '-') s[i] = '_';
9561 }
9562 do {
9563 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9564 n = vend - vbeg;
9565 if (mc->length) {
9566 n = (*mc->length)(p, vbeg, n);
9567 }
9568 str_copy(val, vbeg, n);
9569 p->lex.ptok = tok_beg;
9570 p->lex.pcur = tok_end;
9571 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9572 break;
9573 }
9574 } while (++mc < magic_comments + numberof(magic_comments));
9575#ifdef RIPPER
9576 str_copy(val, vbeg, vend - vbeg);
9577 dispatch2(magic_comment, name, val);
9578#endif
9579 }
9580
9581 return TRUE;
9582}
9583
9584static void
9585set_file_encoding(struct parser_params *p, const char *str, const char *send)
9586{
9587 int sep = 0;
9588 const char *beg = str;
9589 VALUE s;
9590
9591 for (;;) {
9592 if (send - str <= 6) return;
9593 switch (str[6]) {
9594 case 'C': case 'c': str += 6; continue;
9595 case 'O': case 'o': str += 5; continue;
9596 case 'D': case 'd': str += 4; continue;
9597 case 'I': case 'i': str += 3; continue;
9598 case 'N': case 'n': str += 2; continue;
9599 case 'G': case 'g': str += 1; continue;
9600 case '=': case ':':
9601 sep = 1;
9602 str += 6;
9603 break;
9604 default:
9605 str += 6;
9606 if (ISSPACE(*str)) break;
9607 continue;
9608 }
9609 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9610 sep = 0;
9611 }
9612 for (;;) {
9613 do {
9614 if (++str >= send) return;
9615 } while (ISSPACE(*str));
9616 if (sep) break;
9617 if (*str != '=' && *str != ':') return;
9618 sep = 1;
9619 str++;
9620 }
9621 beg = str;
9622 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9623 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9624 p->lex.ptok = beg;
9625 p->lex.pcur = str;
9626 parser_set_encode(p, RSTRING_PTR(s));
9627 rb_str_resize(s, 0);
9628}
9629
9630static void
9631parser_prepare(struct parser_params *p)
9632{
9633 int c = nextc0(p, FALSE);
9634 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9635 switch (c) {
9636 case '#':
9637 if (peek(p, '!')) p->has_shebang = 1;
9638 break;
9639 case 0xef: /* UTF-8 BOM marker */
9640 if (!lex_eol_n_p(p, 2) &&
9641 (unsigned char)p->lex.pcur[0] == 0xbb &&
9642 (unsigned char)p->lex.pcur[1] == 0xbf) {
9643 p->enc = rb_utf8_encoding();
9644 p->lex.pcur += 2;
9645#ifndef RIPPER
9646 if (p->debug_lines) {
9647 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9648 }
9649#endif
9650 p->lex.pbeg = p->lex.pcur;
9651 token_flush(p);
9652 return;
9653 }
9654 break;
9655 case -1: /* end of script. */
9656 return;
9657 }
9658 pushback(p, c);
9659 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9660}
9661
9662#ifndef RIPPER
9663#define ambiguous_operator(tok, op, syn) ( \
9664 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9665 rb_warning0("even though it seems like "syn""))
9666#else
9667#define ambiguous_operator(tok, op, syn) \
9668 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9669#endif
9670#define warn_balanced(tok, op, syn) ((void) \
9671 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9672 space_seen && !ISSPACE(c) && \
9673 (ambiguous_operator(tok, op, syn), 0)), \
9674 (enum yytokentype)(tok))
9675
9676static enum yytokentype
9677no_digits(struct parser_params *p)
9678{
9679 yyerror0("numeric literal without digits");
9680 if (peek(p, '_')) nextc(p);
9681 /* dummy 0, for tUMINUS_NUM at numeric */
9682 return set_number_literal(p, tINTEGER, 0, 10, 0);
9683}
9684
9685static enum yytokentype
9686parse_numeric(struct parser_params *p, int c)
9687{
9688 int is_float, seen_point, seen_e, nondigit;
9689 int suffix;
9690
9691 is_float = seen_point = seen_e = nondigit = 0;
9692 SET_LEX_STATE(EXPR_END);
9693 newtok(p);
9694 if (c == '-' || c == '+') {
9695 tokadd(p, c);
9696 c = nextc(p);
9697 }
9698 if (c == '0') {
9699 int start = toklen(p);
9700 c = nextc(p);
9701 if (c == 'x' || c == 'X') {
9702 /* hexadecimal */
9703 c = nextc(p);
9704 if (c != -1 && ISXDIGIT(c)) {
9705 do {
9706 if (c == '_') {
9707 if (nondigit) break;
9708 nondigit = c;
9709 continue;
9710 }
9711 if (!ISXDIGIT(c)) break;
9712 nondigit = 0;
9713 tokadd(p, c);
9714 } while ((c = nextc(p)) != -1);
9715 }
9716 pushback(p, c);
9717 tokfix(p);
9718 if (toklen(p) == start) {
9719 return no_digits(p);
9720 }
9721 else if (nondigit) goto trailing_uc;
9722 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9723 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9724 }
9725 if (c == 'b' || c == 'B') {
9726 /* binary */
9727 c = nextc(p);
9728 if (c == '0' || c == '1') {
9729 do {
9730 if (c == '_') {
9731 if (nondigit) break;
9732 nondigit = c;
9733 continue;
9734 }
9735 if (c != '0' && c != '1') break;
9736 nondigit = 0;
9737 tokadd(p, c);
9738 } while ((c = nextc(p)) != -1);
9739 }
9740 pushback(p, c);
9741 tokfix(p);
9742 if (toklen(p) == start) {
9743 return no_digits(p);
9744 }
9745 else if (nondigit) goto trailing_uc;
9746 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9747 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9748 }
9749 if (c == 'd' || c == 'D') {
9750 /* decimal */
9751 c = nextc(p);
9752 if (c != -1 && ISDIGIT(c)) {
9753 do {
9754 if (c == '_') {
9755 if (nondigit) break;
9756 nondigit = c;
9757 continue;
9758 }
9759 if (!ISDIGIT(c)) break;
9760 nondigit = 0;
9761 tokadd(p, c);
9762 } while ((c = nextc(p)) != -1);
9763 }
9764 pushback(p, c);
9765 tokfix(p);
9766 if (toklen(p) == start) {
9767 return no_digits(p);
9768 }
9769 else if (nondigit) goto trailing_uc;
9770 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9771 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9772 }
9773 if (c == '_') {
9774 /* 0_0 */
9775 goto octal_number;
9776 }
9777 if (c == 'o' || c == 'O') {
9778 /* prefixed octal */
9779 c = nextc(p);
9780 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9781 tokfix(p);
9782 return no_digits(p);
9783 }
9784 }
9785 if (c >= '0' && c <= '7') {
9786 /* octal */
9787 octal_number:
9788 do {
9789 if (c == '_') {
9790 if (nondigit) break;
9791 nondigit = c;
9792 continue;
9793 }
9794 if (c < '0' || c > '9') break;
9795 if (c > '7') goto invalid_octal;
9796 nondigit = 0;
9797 tokadd(p, c);
9798 } while ((c = nextc(p)) != -1);
9799 if (toklen(p) > start) {
9800 pushback(p, c);
9801 tokfix(p);
9802 if (nondigit) goto trailing_uc;
9803 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9804 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9805 }
9806 if (nondigit) {
9807 pushback(p, c);
9808 goto trailing_uc;
9809 }
9810 }
9811 if (c > '7' && c <= '9') {
9812 invalid_octal:
9813 yyerror0("Invalid octal digit");
9814 }
9815 else if (c == '.' || c == 'e' || c == 'E') {
9816 tokadd(p, '0');
9817 }
9818 else {
9819 pushback(p, c);
9820 tokfix(p);
9821 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9822 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9823 }
9824 }
9825
9826 for (;;) {
9827 switch (c) {
9828 case '0': case '1': case '2': case '3': case '4':
9829 case '5': case '6': case '7': case '8': case '9':
9830 nondigit = 0;
9831 tokadd(p, c);
9832 break;
9833
9834 case '.':
9835 if (nondigit) goto trailing_uc;
9836 if (seen_point || seen_e) {
9837 goto decode_num;
9838 }
9839 else {
9840 int c0 = nextc(p);
9841 if (c0 == -1 || !ISDIGIT(c0)) {
9842 pushback(p, c0);
9843 goto decode_num;
9844 }
9845 c = c0;
9846 }
9847 seen_point = toklen(p);
9848 tokadd(p, '.');
9849 tokadd(p, c);
9850 is_float++;
9851 nondigit = 0;
9852 break;
9853
9854 case 'e':
9855 case 'E':
9856 if (nondigit) {
9857 pushback(p, c);
9858 c = nondigit;
9859 goto decode_num;
9860 }
9861 if (seen_e) {
9862 goto decode_num;
9863 }
9864 nondigit = c;
9865 c = nextc(p);
9866 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9867 pushback(p, c);
9868 c = nondigit;
9869 nondigit = 0;
9870 goto decode_num;
9871 }
9872 tokadd(p, nondigit);
9873 seen_e++;
9874 is_float++;
9875 tokadd(p, c);
9876 nondigit = (c == '-' || c == '+') ? c : 0;
9877 break;
9878
9879 case '_': /* `_' in number just ignored */
9880 if (nondigit) goto decode_num;
9881 nondigit = c;
9882 break;
9883
9884 default:
9885 goto decode_num;
9886 }
9887 c = nextc(p);
9888 }
9889
9890 decode_num:
9891 pushback(p, c);
9892 if (nondigit) {
9893 trailing_uc:
9894 literal_flush(p, p->lex.pcur - 1);
9895 YYLTYPE loc = RUBY_INIT_YYLLOC();
9896 compile_error(p, "trailing '%c' in number", nondigit);
9897 parser_show_error_line(p, &loc);
9898 }
9899 tokfix(p);
9900 if (is_float) {
9901 enum yytokentype type = tFLOAT;
9902
9903 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9904 if (suffix & NUM_SUFFIX_R) {
9905 type = tRATIONAL;
9906 }
9907 else {
9908 strtod(tok(p), 0);
9909 if (errno == ERANGE) {
9910 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9911 errno = 0;
9912 }
9913 }
9914 return set_number_literal(p, type, suffix, 0, seen_point);
9915 }
9916 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9917 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9918}
9919
9920static enum yytokentype
9921parse_qmark(struct parser_params *p, int space_seen)
9922{
9923 rb_encoding *enc;
9924 register int c;
9925 rb_parser_string_t *lit;
9926 const char *start = p->lex.pcur;
9927
9928 if (IS_END()) {
9929 SET_LEX_STATE(EXPR_VALUE);
9930 return '?';
9931 }
9932 c = nextc(p);
9933 if (c == -1) {
9934 compile_error(p, "incomplete character syntax");
9935 return 0;
9936 }
9937 if (rb_enc_isspace(c, p->enc)) {
9938 if (!IS_ARG()) {
9939 int c2 = escaped_control_code(c);
9940 if (c2) {
9941 WARN_SPACE_CHAR(c2, "?");
9942 }
9943 }
9944 ternary:
9945 pushback(p, c);
9946 SET_LEX_STATE(EXPR_VALUE);
9947 return '?';
9948 }
9949 newtok(p);
9950 enc = p->enc;
9951 int w = parser_precise_mbclen(p, start);
9952 if (is_identchar(p, start, p->lex.pend, p->enc) &&
9953 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
9954 if (space_seen) {
9955 const char *ptr = start;
9956 do {
9957 int n = parser_precise_mbclen(p, ptr);
9958 if (n < 0) return -1;
9959 ptr += n;
9960 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9961 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9962 " a conditional operator, put a space after '?'",
9963 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9964 }
9965 goto ternary;
9966 }
9967 else if (c == '\\') {
9968 if (peek(p, 'u')) {
9969 nextc(p);
9970 enc = rb_utf8_encoding();
9971 tokadd_utf8(p, &enc, -1, 0, 0);
9972 }
9973 else if (!ISASCII(c = peekc(p)) && c != -1) {
9974 nextc(p);
9975 if (tokadd_mbchar(p, c) == -1) return 0;
9976 }
9977 else {
9978 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
9979 tokadd(p, c);
9980 }
9981 }
9982 else {
9983 if (tokadd_mbchar(p, c) == -1) return 0;
9984 }
9985 tokfix(p);
9986 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9987 set_yylval_str(lit);
9988 SET_LEX_STATE(EXPR_END);
9989 return tCHAR;
9990}
9991
9992static enum yytokentype
9993parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9994{
9995 register int c;
9996 const char *ptok = p->lex.pcur;
9997
9998 if (IS_BEG()) {
9999 int term;
10000 int paren;
10001
10002 c = nextc(p);
10003 quotation:
10004 if (c == -1) goto unterminated;
10005 if (!ISALNUM(c)) {
10006 term = c;
10007 if (!ISASCII(c)) goto unknown;
10008 c = 'Q';
10009 }
10010 else {
10011 term = nextc(p);
10012 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10013 unknown:
10014 pushback(p, term);
10015 c = parser_precise_mbclen(p, p->lex.pcur);
10016 if (c < 0) return 0;
10017 p->lex.pcur += c;
10018 yyerror0("unknown type of %string");
10019 return 0;
10020 }
10021 }
10022 if (term == -1) {
10023 unterminated:
10024 compile_error(p, "unterminated quoted string meets end of file");
10025 return 0;
10026 }
10027 paren = term;
10028 if (term == '(') term = ')';
10029 else if (term == '[') term = ']';
10030 else if (term == '{') term = '}';
10031 else if (term == '<') term = '>';
10032 else paren = 0;
10033
10034 p->lex.ptok = ptok-1;
10035 switch (c) {
10036 case 'Q':
10037 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10038 return tSTRING_BEG;
10039
10040 case 'q':
10041 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10042 return tSTRING_BEG;
10043
10044 case 'W':
10045 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10046 return tWORDS_BEG;
10047
10048 case 'w':
10049 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10050 return tQWORDS_BEG;
10051
10052 case 'I':
10053 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10054 return tSYMBOLS_BEG;
10055
10056 case 'i':
10057 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10058 return tQSYMBOLS_BEG;
10059
10060 case 'x':
10061 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10062 return tXSTRING_BEG;
10063
10064 case 'r':
10065 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10066 return tREGEXP_BEG;
10067
10068 case 's':
10069 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10070 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10071 return tSYMBEG;
10072
10073 default:
10074 yyerror0("unknown type of %string");
10075 return 0;
10076 }
10077 }
10078 if ((c = nextc(p)) == '=') {
10079 set_yylval_id('%');
10080 SET_LEX_STATE(EXPR_BEG);
10081 return tOP_ASGN;
10082 }
10083 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10084 goto quotation;
10085 }
10086 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10087 pushback(p, c);
10088 return warn_balanced('%', "%%", "string literal");
10089}
10090
10091static int
10092tokadd_ident(struct parser_params *p, int c)
10093{
10094 do {
10095 if (tokadd_mbchar(p, c) == -1) return -1;
10096 c = nextc(p);
10097 } while (parser_is_identchar(p));
10098 pushback(p, c);
10099 return 0;
10100}
10101
10102static ID
10103tokenize_ident(struct parser_params *p)
10104{
10105 ID ident = TOK_INTERN();
10106
10107 set_yylval_name(ident);
10108
10109 return ident;
10110}
10111
10112static int
10113parse_numvar(struct parser_params *p)
10114{
10115 size_t len;
10116 int overflow;
10117 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10118 const unsigned long nth_ref_max =
10119 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10120 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10121 * turned into a Fixnum, in compile.c */
10122
10123 if (overflow || n > nth_ref_max) {
10124 /* compile_error()? */
10125 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10126 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10127 }
10128 else {
10129 return (int)n;
10130 }
10131}
10132
10133static enum yytokentype
10134parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10135{
10136 const char *ptr = p->lex.pcur;
10137 register int c;
10138
10139 SET_LEX_STATE(EXPR_END);
10140 p->lex.ptok = ptr - 1; /* from '$' */
10141 newtok(p);
10142 c = nextc(p);
10143 switch (c) {
10144 case '_': /* $_: last read line string */
10145 c = nextc(p);
10146 if (parser_is_identchar(p)) {
10147 tokadd(p, '$');
10148 tokadd(p, '_');
10149 break;
10150 }
10151 pushback(p, c);
10152 c = '_';
10153 /* fall through */
10154 case '~': /* $~: match-data */
10155 case '*': /* $*: argv */
10156 case '$': /* $$: pid */
10157 case '?': /* $?: last status */
10158 case '!': /* $!: error string */
10159 case '@': /* $@: error position */
10160 case '/': /* $/: input record separator */
10161 case '\\': /* $\: output record separator */
10162 case ';': /* $;: field separator */
10163 case ',': /* $,: output field separator */
10164 case '.': /* $.: last read line number */
10165 case '=': /* $=: ignorecase */
10166 case ':': /* $:: load path */
10167 case '<': /* $<: default input handle */
10168 case '>': /* $>: default output handle */
10169 case '\"': /* $": already loaded files */
10170 tokadd(p, '$');
10171 tokadd(p, c);
10172 goto gvar;
10173
10174 case '-':
10175 tokadd(p, '$');
10176 tokadd(p, c);
10177 c = nextc(p);
10178 if (parser_is_identchar(p)) {
10179 if (tokadd_mbchar(p, c) == -1) return 0;
10180 }
10181 else {
10182 pushback(p, c);
10183 pushback(p, '-');
10184 return '$';
10185 }
10186 gvar:
10187 tokenize_ident(p);
10188 return tGVAR;
10189
10190 case '&': /* $&: last match */
10191 case '`': /* $`: string before last match */
10192 case '\'': /* $': string after last match */
10193 case '+': /* $+: string matches last paren. */
10194 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10195 tokadd(p, '$');
10196 tokadd(p, c);
10197 goto gvar;
10198 }
10199 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10200 return tBACK_REF;
10201
10202 case '1': case '2': case '3':
10203 case '4': case '5': case '6':
10204 case '7': case '8': case '9':
10205 tokadd(p, '$');
10206 do {
10207 tokadd(p, c);
10208 c = nextc(p);
10209 } while (c != -1 && ISDIGIT(c));
10210 pushback(p, c);
10211 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10212 tokfix(p);
10213 c = parse_numvar(p);
10214 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10215 return tNTH_REF;
10216
10217 default:
10218 if (!parser_is_identchar(p)) {
10219 YYLTYPE loc = RUBY_INIT_YYLLOC();
10220 if (c == -1 || ISSPACE(c)) {
10221 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10222 }
10223 else {
10224 pushback(p, c);
10225 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10226 }
10227 parser_show_error_line(p, &loc);
10228 set_yylval_noname();
10229 return tGVAR;
10230 }
10231 /* fall through */
10232 case '0':
10233 tokadd(p, '$');
10234 }
10235
10236 if (tokadd_ident(p, c)) return 0;
10237 SET_LEX_STATE(EXPR_END);
10238 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10239 tokenize_ident(p);
10240 }
10241 else {
10242 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10243 set_yylval_noname();
10244 }
10245 return tGVAR;
10246}
10247
10248static bool
10249parser_numbered_param(struct parser_params *p, int n)
10250{
10251 if (n < 0) return false;
10252
10253 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10254 return false;
10255 }
10256 if (p->max_numparam == ORDINAL_PARAM) {
10257 compile_error(p, "ordinary parameter is defined");
10258 return false;
10259 }
10260 struct vtable *args = p->lvtbl->args;
10261 if (p->max_numparam < n) {
10262 p->max_numparam = n;
10263 }
10264 while (n > args->pos) {
10265 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10266 }
10267 return true;
10268}
10269
10270static enum yytokentype
10271parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10272{
10273 const char *ptr = p->lex.pcur;
10274 enum yytokentype result = tIVAR;
10275 register int c = nextc(p);
10276 YYLTYPE loc;
10277
10278 p->lex.ptok = ptr - 1; /* from '@' */
10279 newtok(p);
10280 tokadd(p, '@');
10281 if (c == '@') {
10282 result = tCVAR;
10283 tokadd(p, '@');
10284 c = nextc(p);
10285 }
10286 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10287 if (c == -1 || !parser_is_identchar(p)) {
10288 pushback(p, c);
10289 RUBY_SET_YYLLOC(loc);
10290 if (result == tIVAR) {
10291 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10292 }
10293 else {
10294 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10295 }
10296 parser_show_error_line(p, &loc);
10297 set_yylval_noname();
10298 SET_LEX_STATE(EXPR_END);
10299 return result;
10300 }
10301 else if (ISDIGIT(c)) {
10302 pushback(p, c);
10303 RUBY_SET_YYLLOC(loc);
10304 if (result == tIVAR) {
10305 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10306 }
10307 else {
10308 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10309 }
10310 parser_show_error_line(p, &loc);
10311 set_yylval_noname();
10312 SET_LEX_STATE(EXPR_END);
10313 return result;
10314 }
10315
10316 if (tokadd_ident(p, c)) return 0;
10317 tokenize_ident(p);
10318 return result;
10319}
10320
10321static enum yytokentype
10322parse_ident(struct parser_params *p, int c, int cmd_state)
10323{
10324 enum yytokentype result;
10325 bool is_ascii = true;
10326 const enum lex_state_e last_state = p->lex.state;
10327 ID ident;
10328 int enforce_keyword_end = 0;
10329
10330 do {
10331 if (!ISASCII(c)) is_ascii = false;
10332 if (tokadd_mbchar(p, c) == -1) return 0;
10333 c = nextc(p);
10334 } while (parser_is_identchar(p));
10335 if ((c == '!' || c == '?') && !peek(p, '=')) {
10336 result = tFID;
10337 tokadd(p, c);
10338 }
10339 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10340 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10341 result = tIDENTIFIER;
10342 tokadd(p, c);
10343 }
10344 else {
10345 result = tCONSTANT; /* assume provisionally */
10346 pushback(p, c);
10347 }
10348 tokfix(p);
10349
10350 if (IS_LABEL_POSSIBLE()) {
10351 if (IS_LABEL_SUFFIX(0)) {
10352 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10353 nextc(p);
10354 tokenize_ident(p);
10355 return tLABEL;
10356 }
10357 }
10358
10359#ifndef RIPPER
10360 if (peek_end_expect_token_locations(p)) {
10361 const rb_code_position_t *end_pos;
10362 int lineno, column;
10363 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10364
10365 end_pos = peek_end_expect_token_locations(p)->pos;
10366 lineno = end_pos->lineno;
10367 column = end_pos->column;
10368
10369 if (p->debug) {
10370 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10371 p->ruby_sourceline, beg_pos, lineno, column);
10372 }
10373
10374 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10375 const struct kwtable *kw;
10376
10377 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10378 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10379 enforce_keyword_end = 1;
10380 }
10381 }
10382 }
10383#endif
10384
10385 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10386 const struct kwtable *kw;
10387
10388 /* See if it is a reserved word. */
10389 kw = rb_reserved_word(tok(p), toklen(p));
10390 if (kw) {
10391 enum lex_state_e state = p->lex.state;
10392 if (IS_lex_state_for(state, EXPR_FNAME)) {
10393 SET_LEX_STATE(EXPR_ENDFN);
10394 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10395 return kw->id[0];
10396 }
10397 SET_LEX_STATE(kw->state);
10398 if (IS_lex_state(EXPR_BEG)) {
10399 p->command_start = TRUE;
10400 }
10401 if (kw->id[0] == keyword_do) {
10402 if (lambda_beginning_p()) {
10403 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10404 return keyword_do_LAMBDA;
10405 }
10406 if (COND_P()) return keyword_do_cond;
10407 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10408 return keyword_do_block;
10409 return keyword_do;
10410 }
10411 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10412 return kw->id[0];
10413 else {
10414 if (kw->id[0] != kw->id[1])
10415 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10416 return kw->id[1];
10417 }
10418 }
10419 }
10420
10421 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10422 if (cmd_state) {
10423 SET_LEX_STATE(EXPR_CMDARG);
10424 }
10425 else {
10426 SET_LEX_STATE(EXPR_ARG);
10427 }
10428 }
10429 else if (p->lex.state == EXPR_FNAME) {
10430 SET_LEX_STATE(EXPR_ENDFN);
10431 }
10432 else {
10433 SET_LEX_STATE(EXPR_END);
10434 }
10435
10436 ident = tokenize_ident(p);
10437 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10438 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10439 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10440 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10441 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10442 }
10443 return result;
10444}
10445
10446static void
10447warn_cr(struct parser_params *p)
10448{
10449 if (!p->cr_seen) {
10450 p->cr_seen = TRUE;
10451 /* carried over with p->lex.nextline for nextc() */
10452 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10453 }
10454}
10455
10456static enum yytokentype
10457parser_yylex(struct parser_params *p)
10458{
10459 register int c;
10460 int space_seen = 0;
10461 int cmd_state;
10462 int label;
10463 enum lex_state_e last_state;
10464 int fallthru = FALSE;
10465 int token_seen = p->token_seen;
10466
10467 if (p->lex.strterm) {
10468 if (strterm_is_heredoc(p->lex.strterm)) {
10469 token_flush(p);
10470 return here_document(p, &p->lex.strterm->u.heredoc);
10471 }
10472 else {
10473 token_flush(p);
10474 return parse_string(p, &p->lex.strterm->u.literal);
10475 }
10476 }
10477 cmd_state = p->command_start;
10478 p->command_start = FALSE;
10479 p->token_seen = TRUE;
10480#ifndef RIPPER
10481 token_flush(p);
10482#endif
10483 retry:
10484 last_state = p->lex.state;
10485 switch (c = nextc(p)) {
10486 case '\0': /* NUL */
10487 case '\004': /* ^D */
10488 case '\032': /* ^Z */
10489 case -1: /* end of script. */
10490 p->eofp = 1;
10491#ifndef RIPPER
10492 if (p->end_expect_token_locations) {
10493 pop_end_expect_token_locations(p);
10494 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10495 return tDUMNY_END;
10496 }
10497#endif
10498 /* Set location for end-of-input because dispatch_scan_event is not called. */
10499 RUBY_SET_YYLLOC(*p->yylloc);
10500 return END_OF_INPUT;
10501
10502 /* white spaces */
10503 case '\r':
10504 warn_cr(p);
10505 /* fall through */
10506 case ' ': case '\t': case '\f':
10507 case '\13': /* '\v' */
10508 space_seen = 1;
10509 while ((c = nextc(p))) {
10510 switch (c) {
10511 case '\r':
10512 warn_cr(p);
10513 /* fall through */
10514 case ' ': case '\t': case '\f':
10515 case '\13': /* '\v' */
10516 break;
10517 default:
10518 goto outofloop;
10519 }
10520 }
10521 outofloop:
10522 pushback(p, c);
10523 dispatch_scan_event(p, tSP);
10524#ifndef RIPPER
10525 token_flush(p);
10526#endif
10527 goto retry;
10528
10529 case '#': /* it's a comment */
10530 p->token_seen = token_seen;
10531 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10532 /* no magic_comment in shebang line */
10533 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10534 if (comment_at_top(p)) {
10535 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10536 }
10537 }
10538 p->lex.pcur = pcur, p->lex.ptok = ptok;
10539 lex_goto_eol(p);
10540 dispatch_scan_event(p, tCOMMENT);
10541 fallthru = TRUE;
10542 /* fall through */
10543 case '\n':
10544 p->token_seen = token_seen;
10545 rb_parser_string_t *prevline = p->lex.lastline;
10546 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10547 !IS_lex_state(EXPR_LABELED));
10548 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10549 if (!fallthru) {
10550 dispatch_scan_event(p, tIGNORED_NL);
10551 }
10552 fallthru = FALSE;
10553 if (!c && p->ctxt.in_kwarg) {
10554 goto normal_newline;
10555 }
10556 goto retry;
10557 }
10558 while (1) {
10559 switch (c = nextc(p)) {
10560 case ' ': case '\t': case '\f': case '\r':
10561 case '\13': /* '\v' */
10562 space_seen = 1;
10563 break;
10564 case '#':
10565 pushback(p, c);
10566 if (space_seen) {
10567 dispatch_scan_event(p, tSP);
10568 token_flush(p);
10569 }
10570 goto retry;
10571 case 'a':
10572 if (peek_word_at(p, "nd", 2, 0)) goto leading_logical;
10573 goto bol;
10574 case 'o':
10575 if (peek_word_at(p, "r", 1, 0)) goto leading_logical;
10576 goto bol;
10577 case '|':
10578 if (peek(p, '|')) goto leading_logical;
10579 goto bol;
10580 case '&':
10581 if (peek(p, '&')) {
10582 leading_logical:
10583 pushback(p, c);
10584 dispatch_delayed_token(p, tIGNORED_NL);
10585 cmd_state = FALSE;
10586 goto retry;
10587 }
10588 /* fall through */
10589 case '.': {
10590 dispatch_delayed_token(p, tIGNORED_NL);
10591 if (peek(p, '.') == (c == '&')) {
10592 pushback(p, c);
10593 dispatch_scan_event(p, tSP);
10594 goto retry;
10595 }
10596 }
10597 bol:
10598 default:
10599 p->ruby_sourceline--;
10600 p->lex.nextline = p->lex.lastline;
10601 set_lastline(p, prevline);
10602 case -1: /* EOF no decrement*/
10603 if (c == -1 && space_seen) {
10604 dispatch_scan_event(p, tSP);
10605 }
10606 lex_goto_eol(p);
10607 if (c != -1) {
10608 token_flush(p);
10609 RUBY_SET_YYLLOC(*p->yylloc);
10610 }
10611 goto normal_newline;
10612 }
10613 }
10614 normal_newline:
10615 p->command_start = TRUE;
10616 SET_LEX_STATE(EXPR_BEG);
10617 return '\n';
10618
10619 case '*':
10620 if ((c = nextc(p)) == '*') {
10621 if ((c = nextc(p)) == '=') {
10622 set_yylval_id(idPow);
10623 SET_LEX_STATE(EXPR_BEG);
10624 return tOP_ASGN;
10625 }
10626 pushback(p, c);
10627 if (IS_SPCARG(c)) {
10628 rb_warning0("'**' interpreted as argument prefix");
10629 c = tDSTAR;
10630 }
10631 else if (IS_BEG()) {
10632 c = tDSTAR;
10633 }
10634 else {
10635 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10636 }
10637 }
10638 else {
10639 if (c == '=') {
10640 set_yylval_id('*');
10641 SET_LEX_STATE(EXPR_BEG);
10642 return tOP_ASGN;
10643 }
10644 pushback(p, c);
10645 if (IS_SPCARG(c)) {
10646 rb_warning0("'*' interpreted as argument prefix");
10647 c = tSTAR;
10648 }
10649 else if (IS_BEG()) {
10650 c = tSTAR;
10651 }
10652 else {
10653 c = warn_balanced('*', "*", "argument prefix");
10654 }
10655 }
10656 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10657 return c;
10658
10659 case '!':
10660 c = nextc(p);
10661 if (IS_AFTER_OPERATOR()) {
10662 SET_LEX_STATE(EXPR_ARG);
10663 if (c == '@') {
10664 return '!';
10665 }
10666 }
10667 else {
10668 SET_LEX_STATE(EXPR_BEG);
10669 }
10670 if (c == '=') {
10671 return tNEQ;
10672 }
10673 if (c == '~') {
10674 return tNMATCH;
10675 }
10676 pushback(p, c);
10677 return '!';
10678
10679 case '=':
10680 if (was_bol(p)) {
10681 /* skip embedded rd document */
10682 if (word_match_p(p, "begin", 5)) {
10683 int first_p = TRUE;
10684
10685 lex_goto_eol(p);
10686 dispatch_scan_event(p, tEMBDOC_BEG);
10687 for (;;) {
10688 lex_goto_eol(p);
10689 if (!first_p) {
10690 dispatch_scan_event(p, tEMBDOC);
10691 }
10692 first_p = FALSE;
10693 c = nextc(p);
10694 if (c == -1) {
10695 compile_error(p, "embedded document meets end of file");
10696 return END_OF_INPUT;
10697 }
10698 if (c == '=' && word_match_p(p, "end", 3)) {
10699 break;
10700 }
10701 pushback(p, c);
10702 }
10703 lex_goto_eol(p);
10704 dispatch_scan_event(p, tEMBDOC_END);
10705 goto retry;
10706 }
10707 }
10708
10709 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10710 if ((c = nextc(p)) == '=') {
10711 if ((c = nextc(p)) == '=') {
10712 return tEQQ;
10713 }
10714 pushback(p, c);
10715 return tEQ;
10716 }
10717 if (c == '~') {
10718 return tMATCH;
10719 }
10720 else if (c == '>') {
10721 return tASSOC;
10722 }
10723 pushback(p, c);
10724 return '=';
10725
10726 case '<':
10727 c = nextc(p);
10728 if (c == '<' &&
10729 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10730 !IS_END() &&
10731 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10732 enum yytokentype token = heredoc_identifier(p);
10733 if (token) return token < 0 ? 0 : token;
10734 }
10735 if (IS_AFTER_OPERATOR()) {
10736 SET_LEX_STATE(EXPR_ARG);
10737 }
10738 else {
10739 if (IS_lex_state(EXPR_CLASS))
10740 p->command_start = TRUE;
10741 SET_LEX_STATE(EXPR_BEG);
10742 }
10743 if (c == '=') {
10744 if ((c = nextc(p)) == '>') {
10745 return tCMP;
10746 }
10747 pushback(p, c);
10748 return tLEQ;
10749 }
10750 if (c == '<') {
10751 if ((c = nextc(p)) == '=') {
10752 set_yylval_id(idLTLT);
10753 SET_LEX_STATE(EXPR_BEG);
10754 return tOP_ASGN;
10755 }
10756 pushback(p, c);
10757 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10758 }
10759 pushback(p, c);
10760 return '<';
10761
10762 case '>':
10763 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10764 if ((c = nextc(p)) == '=') {
10765 return tGEQ;
10766 }
10767 if (c == '>') {
10768 if ((c = nextc(p)) == '=') {
10769 set_yylval_id(idGTGT);
10770 SET_LEX_STATE(EXPR_BEG);
10771 return tOP_ASGN;
10772 }
10773 pushback(p, c);
10774 return tRSHFT;
10775 }
10776 pushback(p, c);
10777 return '>';
10778
10779 case '"':
10780 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10781 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10782 p->lex.ptok = p->lex.pcur-1;
10783 return tSTRING_BEG;
10784
10785 case '`':
10786 if (IS_lex_state(EXPR_FNAME)) {
10787 SET_LEX_STATE(EXPR_ENDFN);
10788 return c;
10789 }
10790 if (IS_lex_state(EXPR_DOT)) {
10791 if (cmd_state)
10792 SET_LEX_STATE(EXPR_CMDARG);
10793 else
10794 SET_LEX_STATE(EXPR_ARG);
10795 return c;
10796 }
10797 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10798 return tXSTRING_BEG;
10799
10800 case '\'':
10801 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10802 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10803 p->lex.ptok = p->lex.pcur-1;
10804 return tSTRING_BEG;
10805
10806 case '?':
10807 return parse_qmark(p, space_seen);
10808
10809 case '&':
10810 if ((c = nextc(p)) == '&') {
10811 SET_LEX_STATE(EXPR_BEG);
10812 if ((c = nextc(p)) == '=') {
10813 set_yylval_id(idANDOP);
10814 SET_LEX_STATE(EXPR_BEG);
10815 return tOP_ASGN;
10816 }
10817 pushback(p, c);
10818 return tANDOP;
10819 }
10820 else if (c == '=') {
10821 set_yylval_id('&');
10822 SET_LEX_STATE(EXPR_BEG);
10823 return tOP_ASGN;
10824 }
10825 else if (c == '.') {
10826 set_yylval_id(idANDDOT);
10827 SET_LEX_STATE(EXPR_DOT);
10828 return tANDDOT;
10829 }
10830 pushback(p, c);
10831 if (IS_SPCARG(c)) {
10832 if ((c != ':') ||
10833 (c = peekc_n(p, 1)) == -1 ||
10834 !(c == '\'' || c == '"' ||
10835 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10836 rb_warning0("'&' interpreted as argument prefix");
10837 }
10838 c = tAMPER;
10839 }
10840 else if (IS_BEG()) {
10841 c = tAMPER;
10842 }
10843 else {
10844 c = warn_balanced('&', "&", "argument prefix");
10845 }
10846 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10847 return c;
10848
10849 case '|':
10850 if ((c = nextc(p)) == '|') {
10851 SET_LEX_STATE(EXPR_BEG);
10852 if ((c = nextc(p)) == '=') {
10853 set_yylval_id(idOROP);
10854 SET_LEX_STATE(EXPR_BEG);
10855 return tOP_ASGN;
10856 }
10857 pushback(p, c);
10858 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10859 c = '|';
10860 pushback(p, '|');
10861 return c;
10862 }
10863 return tOROP;
10864 }
10865 if (c == '=') {
10866 set_yylval_id('|');
10867 SET_LEX_STATE(EXPR_BEG);
10868 return tOP_ASGN;
10869 }
10870 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10871 pushback(p, c);
10872 return '|';
10873
10874 case '+':
10875 c = nextc(p);
10876 if (IS_AFTER_OPERATOR()) {
10877 SET_LEX_STATE(EXPR_ARG);
10878 if (c == '@') {
10879 return tUPLUS;
10880 }
10881 pushback(p, c);
10882 return '+';
10883 }
10884 if (c == '=') {
10885 set_yylval_id('+');
10886 SET_LEX_STATE(EXPR_BEG);
10887 return tOP_ASGN;
10888 }
10889 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10890 SET_LEX_STATE(EXPR_BEG);
10891 pushback(p, c);
10892 if (c != -1 && ISDIGIT(c)) {
10893 return parse_numeric(p, '+');
10894 }
10895 return tUPLUS;
10896 }
10897 SET_LEX_STATE(EXPR_BEG);
10898 pushback(p, c);
10899 return warn_balanced('+', "+", "unary operator");
10900
10901 case '-':
10902 c = nextc(p);
10903 if (IS_AFTER_OPERATOR()) {
10904 SET_LEX_STATE(EXPR_ARG);
10905 if (c == '@') {
10906 return tUMINUS;
10907 }
10908 pushback(p, c);
10909 return '-';
10910 }
10911 if (c == '=') {
10912 set_yylval_id('-');
10913 SET_LEX_STATE(EXPR_BEG);
10914 return tOP_ASGN;
10915 }
10916 if (c == '>') {
10917 SET_LEX_STATE(EXPR_ENDFN);
10918 yylval.num = p->lex.lpar_beg;
10919 p->lex.lpar_beg = p->lex.paren_nest;
10920 return tLAMBDA;
10921 }
10922 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10923 SET_LEX_STATE(EXPR_BEG);
10924 pushback(p, c);
10925 if (c != -1 && ISDIGIT(c)) {
10926 return tUMINUS_NUM;
10927 }
10928 return tUMINUS;
10929 }
10930 SET_LEX_STATE(EXPR_BEG);
10931 pushback(p, c);
10932 return warn_balanced('-', "-", "unary operator");
10933
10934 case '.': {
10935 int is_beg = IS_BEG();
10936 SET_LEX_STATE(EXPR_BEG);
10937 if ((c = nextc(p)) == '.') {
10938 if ((c = nextc(p)) == '.') {
10939 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10940 SET_LEX_STATE(EXPR_ENDARG);
10941 return tBDOT3;
10942 }
10943 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10944 rb_warn0("... at EOL, should be parenthesized?");
10945 }
10946 return is_beg ? tBDOT3 : tDOT3;
10947 }
10948 pushback(p, c);
10949 return is_beg ? tBDOT2 : tDOT2;
10950 }
10951 pushback(p, c);
10952 if (c != -1 && ISDIGIT(c)) {
10953 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10954 parse_numeric(p, '.');
10955 if (ISDIGIT(prev)) {
10956 yyerror0("unexpected fraction part after numeric literal");
10957 }
10958 else {
10959 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10960 }
10961 SET_LEX_STATE(EXPR_END);
10962 p->lex.ptok = p->lex.pcur;
10963 goto retry;
10964 }
10965 set_yylval_id('.');
10966 SET_LEX_STATE(EXPR_DOT);
10967 return '.';
10968 }
10969
10970 case '0': case '1': case '2': case '3': case '4':
10971 case '5': case '6': case '7': case '8': case '9':
10972 return parse_numeric(p, c);
10973
10974 case ')':
10975 COND_POP();
10976 CMDARG_POP();
10977 SET_LEX_STATE(EXPR_ENDFN);
10978 p->lex.paren_nest--;
10979 return c;
10980
10981 case ']':
10982 COND_POP();
10983 CMDARG_POP();
10984 SET_LEX_STATE(EXPR_END);
10985 p->lex.paren_nest--;
10986 return c;
10987
10988 case '}':
10989 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10990 if (!p->lex.brace_nest--) return tSTRING_DEND;
10991 COND_POP();
10992 CMDARG_POP();
10993 SET_LEX_STATE(EXPR_END);
10994 p->lex.paren_nest--;
10995 return c;
10996
10997 case ':':
10998 c = nextc(p);
10999 if (c == ':') {
11000 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
11001 SET_LEX_STATE(EXPR_BEG);
11002 return tCOLON3;
11003 }
11004 set_yylval_id(idCOLON2);
11005 SET_LEX_STATE(EXPR_DOT);
11006 return tCOLON2;
11007 }
11008 if (IS_END() || ISSPACE(c) || c == '#') {
11009 pushback(p, c);
11010 c = warn_balanced(':', ":", "symbol literal");
11011 SET_LEX_STATE(EXPR_BEG);
11012 return c;
11013 }
11014 switch (c) {
11015 case '\'':
11016 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11017 break;
11018 case '"':
11019 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11020 break;
11021 default:
11022 pushback(p, c);
11023 break;
11024 }
11025 SET_LEX_STATE(EXPR_FNAME);
11026 return tSYMBEG;
11027
11028 case '/':
11029 if (IS_BEG()) {
11030 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11031 return tREGEXP_BEG;
11032 }
11033 if ((c = nextc(p)) == '=') {
11034 set_yylval_id('/');
11035 SET_LEX_STATE(EXPR_BEG);
11036 return tOP_ASGN;
11037 }
11038 pushback(p, c);
11039 if (IS_SPCARG(c)) {
11040 arg_ambiguous(p, '/');
11041 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11042 return tREGEXP_BEG;
11043 }
11044 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11045 return warn_balanced('/', "/", "regexp literal");
11046
11047 case '^':
11048 if ((c = nextc(p)) == '=') {
11049 set_yylval_id('^');
11050 SET_LEX_STATE(EXPR_BEG);
11051 return tOP_ASGN;
11052 }
11053 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11054 pushback(p, c);
11055 return '^';
11056
11057 case ';':
11058 SET_LEX_STATE(EXPR_BEG);
11059 p->command_start = TRUE;
11060 return ';';
11061
11062 case ',':
11063 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11064 return ',';
11065
11066 case '~':
11067 if (IS_AFTER_OPERATOR()) {
11068 if ((c = nextc(p)) != '@') {
11069 pushback(p, c);
11070 }
11071 SET_LEX_STATE(EXPR_ARG);
11072 }
11073 else {
11074 SET_LEX_STATE(EXPR_BEG);
11075 }
11076 return '~';
11077
11078 case '(':
11079 if (IS_BEG()) {
11080 c = tLPAREN;
11081 }
11082 else if (!space_seen) {
11083 /* foo( ... ) => method call, no ambiguity */
11084 }
11085 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11086 c = tLPAREN_ARG;
11087 }
11088 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11089 rb_warning0("parentheses after method name is interpreted as "
11090 "an argument list, not a decomposed argument");
11091 }
11092 p->lex.paren_nest++;
11093 COND_PUSH(0);
11094 CMDARG_PUSH(0);
11095 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11096 return c;
11097
11098 case '[':
11099 p->lex.paren_nest++;
11100 if (IS_AFTER_OPERATOR()) {
11101 if ((c = nextc(p)) == ']') {
11102 p->lex.paren_nest--;
11103 SET_LEX_STATE(EXPR_ARG);
11104 if ((c = nextc(p)) == '=') {
11105 return tASET;
11106 }
11107 pushback(p, c);
11108 return tAREF;
11109 }
11110 pushback(p, c);
11111 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11112 return '[';
11113 }
11114 else if (IS_BEG()) {
11115 c = tLBRACK;
11116 }
11117 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11118 c = tLBRACK;
11119 }
11120 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11121 COND_PUSH(0);
11122 CMDARG_PUSH(0);
11123 return c;
11124
11125 case '{':
11126 ++p->lex.brace_nest;
11127 if (lambda_beginning_p())
11128 c = tLAMBEG;
11129 else if (IS_lex_state(EXPR_LABELED))
11130 c = tLBRACE; /* hash */
11131 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11132 c = '{'; /* block (primary) */
11133 else if (IS_lex_state(EXPR_ENDARG))
11134 c = tLBRACE_ARG; /* block (expr) */
11135 else
11136 c = tLBRACE; /* hash */
11137 if (c != tLBRACE) {
11138 p->command_start = TRUE;
11139 SET_LEX_STATE(EXPR_BEG);
11140 }
11141 else {
11142 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11143 }
11144 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11145 COND_PUSH(0);
11146 CMDARG_PUSH(0);
11147 return c;
11148
11149 case '\\':
11150 c = nextc(p);
11151 if (c == '\n') {
11152 space_seen = 1;
11153 dispatch_scan_event(p, tSP);
11154 goto retry; /* skip \\n */
11155 }
11156 if (c == ' ') return tSP;
11157 if (ISSPACE(c)) return c;
11158 pushback(p, c);
11159 return '\\';
11160
11161 case '%':
11162 return parse_percent(p, space_seen, last_state);
11163
11164 case '$':
11165 return parse_gvar(p, last_state);
11166
11167 case '@':
11168 return parse_atmark(p, last_state);
11169
11170 case '_':
11171 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11172 p->ruby__end__seen = 1;
11173 p->eofp = 1;
11174#ifdef RIPPER
11175 lex_goto_eol(p);
11176 dispatch_scan_event(p, k__END__);
11177#endif
11178 return END_OF_INPUT;
11179 }
11180 newtok(p);
11181 break;
11182
11183 default:
11184 if (!parser_is_identchar(p)) {
11185 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11186 token_flush(p);
11187 goto retry;
11188 }
11189
11190 newtok(p);
11191 break;
11192 }
11193
11194 return parse_ident(p, c, cmd_state);
11195}
11196
11197static enum yytokentype
11198yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11199{
11200 enum yytokentype t;
11201
11202 p->lval = lval;
11203 lval->node = 0;
11204 p->yylloc = yylloc;
11205
11206 t = parser_yylex(p);
11207
11208 if (has_delayed_token(p))
11209 dispatch_delayed_token(p, t);
11210 else if (t != END_OF_INPUT)
11211 dispatch_scan_event(p, t);
11212
11213 return t;
11214}
11215
11216#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11217
11218static NODE*
11219node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11220{
11221 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11222
11223 rb_node_init(n, type);
11224 return n;
11225}
11226
11227static NODE *
11228nd_set_loc(NODE *nd, const YYLTYPE *loc)
11229{
11230 nd->nd_loc = *loc;
11231 nd_set_line(nd, loc->beg_pos.lineno);
11232 return nd;
11233}
11234
11235static NODE*
11236node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11237{
11238 NODE *n = node_new_internal(p, type, size, alignment);
11239
11240 nd_set_loc(n, loc);
11241 nd_set_node_id(n, parser_get_node_id(p));
11242 return n;
11243}
11244
11245#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11246
11247static rb_node_scope_t *
11248rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11249{
11250 rb_ast_id_table_t *nd_tbl;
11251 nd_tbl = local_tbl(p);
11252 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11253 n->nd_tbl = nd_tbl;
11254 n->nd_body = nd_body;
11255 n->nd_parent = nd_parent;
11256 n->nd_args = nd_args;
11257
11258 return n;
11259}
11260
11261static rb_node_scope_t *
11262rb_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)
11263{
11264 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11265 n->nd_tbl = nd_tbl;
11266 n->nd_body = nd_body;
11267 n->nd_parent = nd_parent;
11268 n->nd_args = nd_args;
11269
11270 return n;
11271}
11272
11273static rb_node_defn_t *
11274rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11275{
11276 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11277 n->nd_mid = nd_mid;
11278 n->nd_defn = nd_defn;
11279
11280 return n;
11281}
11282
11283static rb_node_defs_t *
11284rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11285{
11286 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11287 n->nd_recv = nd_recv;
11288 n->nd_mid = nd_mid;
11289 n->nd_defn = nd_defn;
11290
11291 return n;
11292}
11293
11294static rb_node_block_t *
11295rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11296{
11297 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11298 n->nd_head = nd_head;
11299 n->nd_end = (NODE *)n;
11300 n->nd_next = 0;
11301
11302 return n;
11303}
11304
11305static rb_node_for_t *
11306rb_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)
11307{
11308 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11309 n->nd_body = nd_body;
11310 n->nd_iter = nd_iter;
11311 n->for_keyword_loc = *for_keyword_loc;
11312 n->in_keyword_loc = *in_keyword_loc;
11313 n->do_keyword_loc = *do_keyword_loc;
11314 n->end_keyword_loc = *end_keyword_loc;
11315
11316 return n;
11317}
11318
11319static rb_node_for_masgn_t *
11320rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11321{
11322 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11323 n->nd_var = nd_var;
11324
11325 return n;
11326}
11327
11328static rb_node_retry_t *
11329rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11330{
11331 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11332
11333 return n;
11334}
11335
11336static rb_node_begin_t *
11337rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11338{
11339 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11340 n->nd_body = nd_body;
11341
11342 return n;
11343}
11344
11345static rb_node_rescue_t *
11346rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11347{
11348 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11349 n->nd_head = nd_head;
11350 n->nd_resq = nd_resq;
11351 n->nd_else = nd_else;
11352
11353 return n;
11354}
11355
11356static rb_node_resbody_t *
11357rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11358{
11359 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11360 n->nd_args = nd_args;
11361 n->nd_exc_var = nd_exc_var;
11362 n->nd_body = nd_body;
11363 n->nd_next = nd_next;
11364
11365 return n;
11366}
11367
11368static rb_node_ensure_t *
11369rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11370{
11371 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11372 n->nd_head = nd_head;
11373 n->nd_ensr = nd_ensr;
11374
11375 return n;
11376}
11377
11378static rb_node_and_t *
11379rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11380{
11381 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11382 n->nd_1st = nd_1st;
11383 n->nd_2nd = nd_2nd;
11384 n->operator_loc = *operator_loc;
11385
11386 return n;
11387}
11388
11389static rb_node_or_t *
11390rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11391{
11392 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11393 n->nd_1st = nd_1st;
11394 n->nd_2nd = nd_2nd;
11395 n->operator_loc = *operator_loc;
11396
11397 return n;
11398}
11399
11400static rb_node_return_t *
11401rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11402{
11403 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11404 n->nd_stts = nd_stts;
11405 n->keyword_loc = *keyword_loc;
11406 return n;
11407}
11408
11409static rb_node_yield_t *
11410rb_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)
11411{
11412 if (nd_head) no_blockarg(p, nd_head);
11413
11414 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11415 n->nd_head = nd_head;
11416 n->keyword_loc = *keyword_loc;
11417 n->lparen_loc = *lparen_loc;
11418 n->rparen_loc = *rparen_loc;
11419
11420 return n;
11421}
11422
11423static rb_node_if_t *
11424rb_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)
11425{
11426 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11427 n->nd_cond = nd_cond;
11428 n->nd_body = nd_body;
11429 n->nd_else = nd_else;
11430 n->if_keyword_loc = *if_keyword_loc;
11431 n->then_keyword_loc = *then_keyword_loc;
11432 n->end_keyword_loc = *end_keyword_loc;
11433
11434 return n;
11435}
11436
11437static rb_node_unless_t *
11438rb_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)
11439{
11440 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11441 n->nd_cond = nd_cond;
11442 n->nd_body = nd_body;
11443 n->nd_else = nd_else;
11444 n->keyword_loc = *keyword_loc;
11445 n->then_keyword_loc = *then_keyword_loc;
11446 n->end_keyword_loc = *end_keyword_loc;
11447
11448 return n;
11449}
11450
11451static rb_node_class_t *
11452rb_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)
11453{
11454 /* Keep the order of node creation */
11455 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11456 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11457 RNODE_SCOPE(scope)->nd_parent = &n->node;
11458 n->nd_cpath = nd_cpath;
11459 n->nd_body = scope;
11460 n->nd_super = nd_super;
11461 n->class_keyword_loc = *class_keyword_loc;
11462 n->inheritance_operator_loc = *inheritance_operator_loc;
11463 n->end_keyword_loc = *end_keyword_loc;
11464
11465 return n;
11466}
11467
11468static rb_node_sclass_t *
11469rb_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)
11470{
11471 /* Keep the order of node creation */
11472 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11473 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11474 RNODE_SCOPE(scope)->nd_parent = &n->node;
11475 n->nd_recv = nd_recv;
11476 n->nd_body = scope;
11477 n->class_keyword_loc = *class_keyword_loc;
11478 n->operator_loc = *operator_loc;
11479 n->end_keyword_loc = *end_keyword_loc;
11480
11481 return n;
11482}
11483
11484static rb_node_module_t *
11485rb_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)
11486{
11487 /* Keep the order of node creation */
11488 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11489 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11490 RNODE_SCOPE(scope)->nd_parent = &n->node;
11491 n->nd_cpath = nd_cpath;
11492 n->nd_body = scope;
11493 n->module_keyword_loc = *module_keyword_loc;
11494 n->end_keyword_loc = *end_keyword_loc;
11495
11496 return n;
11497}
11498
11499static rb_node_iter_t *
11500rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11501{
11502 /* Keep the order of node creation */
11503 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11504 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11505 RNODE_SCOPE(scope)->nd_parent = &n->node;
11506 n->nd_body = scope;
11507 n->nd_iter = 0;
11508
11509 return n;
11510}
11511
11512static rb_node_lambda_t *
11513rb_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)
11514{
11515 /* Keep the order of node creation */
11516 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11517 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11518 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11519 RNODE_SCOPE(scope)->nd_parent = &n->node;
11520 n->nd_body = scope;
11521 n->operator_loc = *operator_loc;
11522 n->opening_loc = *opening_loc;
11523 n->closing_loc = *closing_loc;
11524
11525 return n;
11526}
11527
11528static rb_node_case_t *
11529rb_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)
11530{
11531 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11532 n->nd_head = nd_head;
11533 n->nd_body = nd_body;
11534 n->case_keyword_loc = *case_keyword_loc;
11535 n->end_keyword_loc = *end_keyword_loc;
11536
11537 return n;
11538}
11539
11540static rb_node_case2_t *
11541rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11542{
11543 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11544 n->nd_head = 0;
11545 n->nd_body = nd_body;
11546 n->case_keyword_loc = *case_keyword_loc;
11547 n->end_keyword_loc = *end_keyword_loc;
11548
11549 return n;
11550}
11551
11552static rb_node_case3_t *
11553rb_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)
11554{
11555 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11556 n->nd_head = nd_head;
11557 n->nd_body = nd_body;
11558 n->case_keyword_loc = *case_keyword_loc;
11559 n->end_keyword_loc = *end_keyword_loc;
11560
11561 return n;
11562}
11563
11564static rb_node_when_t *
11565rb_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)
11566{
11567 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11568 n->nd_head = nd_head;
11569 n->nd_body = nd_body;
11570 n->nd_next = nd_next;
11571 n->keyword_loc = *keyword_loc;
11572 n->then_keyword_loc = *then_keyword_loc;
11573
11574 return n;
11575}
11576
11577static rb_node_in_t *
11578rb_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)
11579{
11580 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11581 n->nd_head = nd_head;
11582 n->nd_body = nd_body;
11583 n->nd_next = nd_next;
11584 n->in_keyword_loc = *in_keyword_loc;
11585 n->then_keyword_loc = *then_keyword_loc;
11586 n->operator_loc = *operator_loc;
11587
11588 return n;
11589}
11590
11591static rb_node_while_t *
11592rb_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)
11593{
11594 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11595 n->nd_cond = nd_cond;
11596 n->nd_body = nd_body;
11597 n->nd_state = nd_state;
11598 n->keyword_loc = *keyword_loc;
11599 n->closing_loc = *closing_loc;
11600
11601 return n;
11602}
11603
11604static rb_node_until_t *
11605rb_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)
11606{
11607 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11608 n->nd_cond = nd_cond;
11609 n->nd_body = nd_body;
11610 n->nd_state = nd_state;
11611 n->keyword_loc = *keyword_loc;
11612 n->closing_loc = *closing_loc;
11613
11614 return n;
11615}
11616
11617static rb_node_colon2_t *
11618rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11619{
11620 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11621 n->nd_head = nd_head;
11622 n->nd_mid = nd_mid;
11623 n->delimiter_loc = *delimiter_loc;
11624 n->name_loc = *name_loc;
11625
11626 return n;
11627}
11628
11629static rb_node_colon3_t *
11630rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11631{
11632 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11633 n->nd_mid = nd_mid;
11634 n->delimiter_loc = *delimiter_loc;
11635 n->name_loc = *name_loc;
11636
11637 return n;
11638}
11639
11640static rb_node_dot2_t *
11641rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11642{
11643 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11644 n->nd_beg = nd_beg;
11645 n->nd_end = nd_end;
11646 n->operator_loc = *operator_loc;
11647
11648 return n;
11649}
11650
11651static rb_node_dot3_t *
11652rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11653{
11654 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11655 n->nd_beg = nd_beg;
11656 n->nd_end = nd_end;
11657 n->operator_loc = *operator_loc;
11658
11659 return n;
11660}
11661
11662static rb_node_self_t *
11663rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11664{
11665 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11666 n->nd_state = 1;
11667
11668 return n;
11669}
11670
11671static rb_node_nil_t *
11672rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11673{
11674 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11675
11676 return n;
11677}
11678
11679static rb_node_true_t *
11680rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11681{
11682 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11683
11684 return n;
11685}
11686
11687static rb_node_false_t *
11688rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11689{
11690 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11691
11692 return n;
11693}
11694
11695static rb_node_super_t *
11696rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11697 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11698{
11699 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11700 n->nd_args = nd_args;
11701 n->keyword_loc = *keyword_loc;
11702 n->lparen_loc = *lparen_loc;
11703 n->rparen_loc = *rparen_loc;
11704
11705 return n;
11706}
11707
11708static rb_node_zsuper_t *
11709rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11710{
11711 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11712
11713 return n;
11714}
11715
11716static rb_node_match2_t *
11717rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11718{
11719 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11720 n->nd_recv = nd_recv;
11721 n->nd_value = nd_value;
11722 n->nd_args = 0;
11723
11724 return n;
11725}
11726
11727static rb_node_match3_t *
11728rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11729{
11730 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11731 n->nd_recv = nd_recv;
11732 n->nd_value = nd_value;
11733
11734 return n;
11735}
11736
11737/* TODO: Use union for NODE_LIST2 */
11738static rb_node_list_t *
11739rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11740{
11741 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11742 n->nd_head = nd_head;
11743 n->as.nd_alen = 1;
11744 n->nd_next = 0;
11745
11746 return n;
11747}
11748
11749static rb_node_list_t *
11750rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11751{
11752 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11753 n->nd_head = nd_head;
11754 n->as.nd_alen = nd_alen;
11755 n->nd_next = nd_next;
11756
11757 return n;
11758}
11759
11760static rb_node_zlist_t *
11761rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11762{
11763 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11764
11765 return n;
11766}
11767
11768static rb_node_hash_t *
11769rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11770{
11771 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11772 n->nd_head = nd_head;
11773 n->nd_brace = 0;
11774
11775 return n;
11776}
11777
11778static rb_node_masgn_t *
11779rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11780{
11781 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11782 n->nd_head = nd_head;
11783 n->nd_value = 0;
11784 n->nd_args = nd_args;
11785
11786 return n;
11787}
11788
11789static rb_node_gasgn_t *
11790rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11791{
11792 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11793 n->nd_vid = nd_vid;
11794 n->nd_value = nd_value;
11795
11796 return n;
11797}
11798
11799static rb_node_lasgn_t *
11800rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11801{
11802 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11803 n->nd_vid = nd_vid;
11804 n->nd_value = nd_value;
11805
11806 return n;
11807}
11808
11809static rb_node_dasgn_t *
11810rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11811{
11812 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11813 n->nd_vid = nd_vid;
11814 n->nd_value = nd_value;
11815
11816 return n;
11817}
11818
11819static rb_node_iasgn_t *
11820rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11821{
11822 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11823 n->nd_vid = nd_vid;
11824 n->nd_value = nd_value;
11825
11826 return n;
11827}
11828
11829static rb_node_cvasgn_t *
11830rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11831{
11832 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11833 n->nd_vid = nd_vid;
11834 n->nd_value = nd_value;
11835
11836 return n;
11837}
11838
11839static rb_node_op_asgn1_t *
11840rb_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)
11841{
11842 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11843 n->nd_recv = nd_recv;
11844 n->nd_mid = nd_mid;
11845 n->nd_index = index;
11846 n->nd_rvalue = rvalue;
11847 n->call_operator_loc = *call_operator_loc;
11848 n->opening_loc = *opening_loc;
11849 n->closing_loc = *closing_loc;
11850 n->binary_operator_loc = *binary_operator_loc;
11851
11852 return n;
11853}
11854
11855static rb_node_op_asgn2_t *
11856rb_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)
11857{
11858 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11859 n->nd_recv = nd_recv;
11860 n->nd_value = nd_value;
11861 n->nd_vid = nd_vid;
11862 n->nd_mid = nd_mid;
11863 n->nd_aid = nd_aid;
11864 n->call_operator_loc = *call_operator_loc;
11865 n->message_loc = *message_loc;
11866 n->binary_operator_loc = *binary_operator_loc;
11867
11868 return n;
11869}
11870
11871static rb_node_op_asgn_or_t *
11872rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11873{
11874 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11875 n->nd_head = nd_head;
11876 n->nd_value = nd_value;
11877
11878 return n;
11879}
11880
11881static rb_node_op_asgn_and_t *
11882rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11883{
11884 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11885 n->nd_head = nd_head;
11886 n->nd_value = nd_value;
11887
11888 return n;
11889}
11890
11891static rb_node_gvar_t *
11892rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11893{
11894 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11895 n->nd_vid = nd_vid;
11896
11897 return n;
11898}
11899
11900static rb_node_lvar_t *
11901rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11902{
11903 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11904 n->nd_vid = nd_vid;
11905
11906 return n;
11907}
11908
11909static rb_node_dvar_t *
11910rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11911{
11912 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11913 n->nd_vid = nd_vid;
11914
11915 return n;
11916}
11917
11918static rb_node_ivar_t *
11919rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11920{
11921 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11922 n->nd_vid = nd_vid;
11923
11924 return n;
11925}
11926
11927static rb_node_const_t *
11928rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11929{
11930 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11931 n->nd_vid = nd_vid;
11932
11933 return n;
11934}
11935
11936static rb_node_cvar_t *
11937rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11938{
11939 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11940 n->nd_vid = nd_vid;
11941
11942 return n;
11943}
11944
11945static rb_node_nth_ref_t *
11946rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11947{
11948 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11949 n->nd_nth = nd_nth;
11950
11951 return n;
11952}
11953
11954static rb_node_back_ref_t *
11955rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11956{
11957 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11958 n->nd_nth = nd_nth;
11959
11960 return n;
11961}
11962
11963static rb_node_integer_t *
11964rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11965{
11966 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11967 n->val = val;
11968 n->minus = FALSE;
11969 n->base = base;
11970
11971 return n;
11972}
11973
11974static rb_node_float_t *
11975rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11976{
11977 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
11978 n->val = val;
11979 n->minus = FALSE;
11980
11981 return n;
11982}
11983
11984static rb_node_rational_t *
11985rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
11986{
11987 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
11988 n->val = val;
11989 n->minus = FALSE;
11990 n->base = base;
11991 n->seen_point = seen_point;
11992
11993 return n;
11994}
11995
11996static rb_node_imaginary_t *
11997rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
11998{
11999 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
12000 n->val = val;
12001 n->minus = FALSE;
12002 n->base = base;
12003 n->seen_point = seen_point;
12004 n->type = numeric_type;
12005
12006 return n;
12007}
12008
12009static rb_node_str_t *
12010rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12011{
12012 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12013 n->string = string;
12014
12015 return n;
12016}
12017
12018/* TODO; Use union for NODE_DSTR2 */
12019static rb_node_dstr_t *
12020rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12021{
12022 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12023 n->string = string;
12024 n->as.nd_alen = nd_alen;
12025 n->nd_next = (rb_node_list_t *)nd_next;
12026
12027 return n;
12028}
12029
12030static rb_node_dstr_t *
12031rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12032{
12033 return rb_node_dstr_new0(p, string, 1, 0, loc);
12034}
12035
12036static rb_node_xstr_t *
12037rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12038{
12039 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12040 n->string = string;
12041
12042 return n;
12043}
12044
12045static rb_node_dxstr_t *
12046rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12047{
12048 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12049 n->string = string;
12050 n->as.nd_alen = nd_alen;
12051 n->nd_next = (rb_node_list_t *)nd_next;
12052
12053 return n;
12054}
12055
12056static rb_node_sym_t *
12057rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12058{
12059 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12060 n->string = rb_str_to_parser_string(p, str);
12061
12062 return n;
12063}
12064
12065static rb_node_dsym_t *
12066rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12067{
12068 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12069 n->string = string;
12070 n->as.nd_alen = nd_alen;
12071 n->nd_next = (rb_node_list_t *)nd_next;
12072
12073 return n;
12074}
12075
12076static rb_node_evstr_t *
12077rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12078{
12079 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12080 n->nd_body = nd_body;
12081 n->opening_loc = *opening_loc;
12082 n->closing_loc = *closing_loc;
12083
12084 return n;
12085}
12086
12087static rb_node_regx_t *
12088rb_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)
12089{
12090 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12091 n->string = string;
12092 n->options = options & RE_OPTION_MASK;
12093 n->opening_loc = *opening_loc;
12094 n->content_loc = *content_loc;
12095 n->closing_loc = *closing_loc;
12096
12097 return n;
12098}
12099
12100static rb_node_call_t *
12101rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12102{
12103 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12104 n->nd_recv = nd_recv;
12105 n->nd_mid = nd_mid;
12106 n->nd_args = nd_args;
12107
12108 return n;
12109}
12110
12111static rb_node_opcall_t *
12112rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12113{
12114 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12115 n->nd_recv = nd_recv;
12116 n->nd_mid = nd_mid;
12117 n->nd_args = nd_args;
12118
12119 return n;
12120}
12121
12122static rb_node_fcall_t *
12123rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12124{
12125 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12126 n->nd_mid = nd_mid;
12127 n->nd_args = nd_args;
12128
12129 return n;
12130}
12131
12132static rb_node_qcall_t *
12133rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12134{
12135 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12136 n->nd_recv = nd_recv;
12137 n->nd_mid = nd_mid;
12138 n->nd_args = nd_args;
12139
12140 return n;
12141}
12142
12143static rb_node_vcall_t *
12144rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12145{
12146 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12147 n->nd_mid = nd_mid;
12148
12149 return n;
12150}
12151
12152static rb_node_once_t *
12153rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12154{
12155 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12156 n->nd_body = nd_body;
12157
12158 return n;
12159}
12160
12161static rb_node_args_t *
12162rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12163{
12164 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12165 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12166
12167 return n;
12168}
12169
12170static rb_node_args_aux_t *
12171rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12172{
12173 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12174 n->nd_pid = nd_pid;
12175 n->nd_plen = nd_plen;
12176 n->nd_next = 0;
12177
12178 return n;
12179}
12180
12181static rb_node_opt_arg_t *
12182rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12183{
12184 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12185 n->nd_body = nd_body;
12186 n->nd_next = 0;
12187
12188 return n;
12189}
12190
12191static rb_node_kw_arg_t *
12192rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12193{
12194 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12195 n->nd_body = nd_body;
12196 n->nd_next = 0;
12197
12198 return n;
12199}
12200
12201static rb_node_postarg_t *
12202rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12203{
12204 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12205 n->nd_1st = nd_1st;
12206 n->nd_2nd = nd_2nd;
12207
12208 return n;
12209}
12210
12211static rb_node_argscat_t *
12212rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12213{
12214 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12215 n->nd_head = nd_head;
12216 n->nd_body = nd_body;
12217
12218 return n;
12219}
12220
12221static rb_node_argspush_t *
12222rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12223{
12224 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12225 n->nd_head = nd_head;
12226 n->nd_body = nd_body;
12227
12228 return n;
12229}
12230
12231static rb_node_splat_t *
12232rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12233{
12234 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12235 n->nd_head = nd_head;
12236 n->operator_loc = *operator_loc;
12237
12238 return n;
12239}
12240
12241static rb_node_block_pass_t *
12242rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12243{
12244 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12245 n->forwarding = 0;
12246 n->nd_head = 0;
12247 n->nd_body = nd_body;
12248 n->operator_loc = *operator_loc;
12249
12250 return n;
12251}
12252
12253static rb_node_alias_t *
12254rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12255{
12256 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12257 n->nd_1st = nd_1st;
12258 n->nd_2nd = nd_2nd;
12259 n->keyword_loc = *keyword_loc;
12260
12261 return n;
12262}
12263
12264static rb_node_valias_t *
12265rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12266{
12267 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12268 n->nd_alias = nd_alias;
12269 n->nd_orig = nd_orig;
12270 n->keyword_loc = *keyword_loc;
12271
12272 return n;
12273}
12274
12275static rb_node_undef_t *
12276rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12277{
12278 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12279 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12280 n->keyword_loc = NULL_LOC;
12281 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12282
12283 return n;
12284}
12285
12286static rb_node_errinfo_t *
12287rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12288{
12289 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12290
12291 return n;
12292}
12293
12294static rb_node_defined_t *
12295rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12296{
12297 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12298 n->nd_head = nd_head;
12299 n->keyword_loc = *keyword_loc;
12300
12301 return n;
12302}
12303
12304static rb_node_postexe_t *
12305rb_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)
12306{
12307 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12308 n->nd_body = nd_body;
12309 n->keyword_loc = *keyword_loc;
12310 n->opening_loc = *opening_loc;
12311 n->closing_loc = *closing_loc;
12312
12313 return n;
12314}
12315
12316static rb_node_attrasgn_t *
12317rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12318{
12319 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12320 n->nd_recv = nd_recv;
12321 n->nd_mid = nd_mid;
12322 n->nd_args = nd_args;
12323
12324 return n;
12325}
12326
12327static rb_node_aryptn_t *
12328rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12329{
12330 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12331 n->nd_pconst = 0;
12332 n->pre_args = pre_args;
12333 n->rest_arg = rest_arg;
12334 n->post_args = post_args;
12335
12336 return n;
12337}
12338
12339static rb_node_hshptn_t *
12340rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12341{
12342 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12343 n->nd_pconst = nd_pconst;
12344 n->nd_pkwargs = nd_pkwargs;
12345 n->nd_pkwrestarg = nd_pkwrestarg;
12346
12347 return n;
12348}
12349
12350static rb_node_fndptn_t *
12351rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12352{
12353 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12354 n->nd_pconst = 0;
12355 n->pre_rest_arg = pre_rest_arg;
12356 n->args = args;
12357 n->post_rest_arg = post_rest_arg;
12358
12359 return n;
12360}
12361
12362static rb_node_line_t *
12363rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12364{
12365 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12366
12367 return n;
12368}
12369
12370static rb_node_file_t *
12371rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12372{
12373 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12374 n->path = rb_str_to_parser_string(p, str);
12375
12376 return n;
12377}
12378
12379static rb_node_encoding_t *
12380rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12381{
12382 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12383 n->enc = p->enc;
12384
12385 return n;
12386}
12387
12388static rb_node_cdecl_t *
12389rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12390{
12391 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12392 n->nd_vid = nd_vid;
12393 n->nd_value = nd_value;
12394 n->nd_else = nd_else;
12395 n->shareability = shareability;
12396
12397 return n;
12398}
12399
12400static rb_node_op_cdecl_t *
12401rb_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)
12402{
12403 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12404 n->nd_head = nd_head;
12405 n->nd_value = nd_value;
12406 n->nd_aid = nd_aid;
12407 n->shareability = shareability;
12408
12409 return n;
12410}
12411
12412static rb_node_error_t *
12413rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12414{
12415 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12416
12417 return n;
12418}
12419
12420static rb_node_break_t *
12421rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12422{
12423 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12424 n->nd_stts = nd_stts;
12425 n->nd_chain = 0;
12426 n->keyword_loc = *keyword_loc;
12427
12428 return n;
12429}
12430
12431static rb_node_next_t *
12432rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12433{
12434 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12435 n->nd_stts = nd_stts;
12436 n->nd_chain = 0;
12437 n->keyword_loc = *keyword_loc;
12438
12439 return n;
12440}
12441
12442static rb_node_redo_t *
12443rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12444{
12445 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12446 n->nd_chain = 0;
12447 n->keyword_loc = *keyword_loc;
12448
12449 return n;
12450}
12451
12452static rb_node_def_temp_t *
12453rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12454{
12455 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12456 n->save.numparam_save = 0;
12457 n->save.max_numparam = 0;
12458 n->save.ctxt = p->ctxt;
12459 n->nd_def = 0;
12460 n->nd_mid = 0;
12461
12462 return n;
12463}
12464
12465static rb_node_def_temp_t *
12466def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12467{
12468 n->save.numparam_save = numparam_push(p);
12469 n->save.max_numparam = p->max_numparam;
12470 return n;
12471}
12472
12473#ifndef RIPPER
12474static enum node_type
12475nodetype(NODE *node) /* for debug */
12476{
12477 return (enum node_type)nd_type(node);
12478}
12479
12480static int
12481nodeline(NODE *node)
12482{
12483 return nd_line(node);
12484}
12485#endif
12486
12487static NODE*
12488newline_node(NODE *node)
12489{
12490 if (node) {
12491 node = remove_begin(node);
12492 nd_set_fl_newline(node);
12493 }
12494 return node;
12495}
12496
12497static void
12498fixpos(NODE *node, NODE *orig)
12499{
12500 if (!node) return;
12501 if (!orig) return;
12502 nd_set_line(node, nd_line(orig));
12503}
12504
12505static NODE*
12506block_append(struct parser_params *p, NODE *head, NODE *tail)
12507{
12508 NODE *end, *h = head, *nd;
12509
12510 if (tail == 0) return head;
12511
12512 if (h == 0) return tail;
12513 switch (nd_type(h)) {
12514 default:
12515 h = end = NEW_BLOCK(head, &head->nd_loc);
12516 head = end;
12517 break;
12518 case NODE_BLOCK:
12519 end = RNODE_BLOCK(h)->nd_end;
12520 break;
12521 }
12522
12523 nd = RNODE_BLOCK(end)->nd_head;
12524 switch (nd_type(nd)) {
12525 case NODE_RETURN:
12526 case NODE_BREAK:
12527 case NODE_NEXT:
12528 case NODE_REDO:
12529 case NODE_RETRY:
12530 rb_warning0L(nd_line(tail), "statement not reached");
12531 break;
12532
12533 default:
12534 break;
12535 }
12536
12537 if (!nd_type_p(tail, NODE_BLOCK)) {
12538 tail = NEW_BLOCK(tail, &tail->nd_loc);
12539 }
12540 RNODE_BLOCK(end)->nd_next = tail;
12541 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12542 nd_set_last_loc(head, nd_last_loc(tail));
12543 return head;
12544}
12545
12546/* append item to the list */
12547static NODE*
12548list_append(struct parser_params *p, NODE *list, NODE *item)
12549{
12550 NODE *last;
12551
12552 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12553 if (RNODE_LIST(list)->nd_next) {
12554 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12555 }
12556 else {
12557 last = list;
12558 }
12559
12560 RNODE_LIST(list)->as.nd_alen += 1;
12561 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12562 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12563
12564 nd_set_last_loc(list, nd_last_loc(item));
12565
12566 return list;
12567}
12568
12569/* concat two lists */
12570static NODE*
12571list_concat(NODE *head, NODE *tail)
12572{
12573 NODE *last;
12574
12575 if (RNODE_LIST(head)->nd_next) {
12576 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12577 }
12578 else {
12579 last = head;
12580 }
12581
12582 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12583 RNODE_LIST(last)->nd_next = tail;
12584 if (RNODE_LIST(tail)->nd_next) {
12585 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12586 }
12587 else {
12588 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12589 }
12590
12591 nd_set_last_loc(head, nd_last_loc(tail));
12592
12593 return head;
12594}
12595
12596static int
12597literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12598{
12599 if (!tail) return 1;
12600 if (!rb_parser_enc_compatible(p, head, tail)) {
12601 compile_error(p, "string literal encodings differ (%s / %s)",
12602 rb_enc_name(rb_parser_str_get_encoding(head)),
12603 rb_enc_name(rb_parser_str_get_encoding(tail)));
12604 rb_parser_str_resize(p, head, 0);
12605 rb_parser_str_resize(p, tail, 0);
12606 return 0;
12607 }
12608 rb_parser_str_buf_append(p, head, tail);
12609 return 1;
12610}
12611
12612static rb_parser_string_t *
12613string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12614{
12615 if (htype != NODE_DSTR) return NULL;
12616 if (RNODE_DSTR(head)->nd_next) {
12617 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12618 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12619 }
12620 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12621 ASSUME(lit);
12622 return lit;
12623}
12624
12625#ifndef RIPPER
12626static rb_parser_string_t *
12627rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12628{
12629 rb_parser_string_t *copy;
12630 if (!orig) return NULL;
12631 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12632 copy->coderange = orig->coderange;
12633 copy->enc = orig->enc;
12634 return copy;
12635}
12636#endif
12637
12638/* concat two string literals */
12639static NODE *
12640literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12641{
12642 enum node_type htype;
12643 rb_parser_string_t *lit;
12644
12645 if (!head) return tail;
12646 if (!tail) return head;
12647
12648 htype = nd_type(head);
12649 if (htype == NODE_EVSTR) {
12650 head = new_dstr(p, head, loc);
12651 htype = NODE_DSTR;
12652 }
12653 if (p->heredoc_indent > 0) {
12654 switch (htype) {
12655 case NODE_STR:
12656 head = str2dstr(p, head);
12657 case NODE_DSTR:
12658 return list_append(p, head, tail);
12659 default:
12660 break;
12661 }
12662 }
12663 switch (nd_type(tail)) {
12664 case NODE_STR:
12665 if ((lit = string_literal_head(p, htype, head)) != false) {
12666 htype = NODE_STR;
12667 }
12668 else {
12669 lit = RNODE_DSTR(head)->string;
12670 }
12671 if (htype == NODE_STR) {
12672 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12673 error:
12674 rb_discard_node(p, head);
12675 rb_discard_node(p, tail);
12676 return 0;
12677 }
12678 rb_discard_node(p, tail);
12679 }
12680 else {
12681 list_append(p, head, tail);
12682 }
12683 break;
12684
12685 case NODE_DSTR:
12686 if (htype == NODE_STR) {
12687 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12688 goto error;
12689 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12690 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12691 RNODE_STR(head)->string = NULL;
12692 rb_discard_node(p, head);
12693 head = tail;
12694 }
12695 else if (!RNODE_DSTR(tail)->string) {
12696 append:
12697 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12698 if (!RNODE_DSTR(head)->nd_next) {
12699 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12700 }
12701 else if (RNODE_DSTR(tail)->nd_next) {
12702 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12703 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12704 }
12705 rb_discard_node(p, tail);
12706 }
12707 else if ((lit = string_literal_head(p, htype, head)) != false) {
12708 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12709 goto error;
12710 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12711 RNODE_DSTR(tail)->string = 0;
12712 goto append;
12713 }
12714 else {
12715 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));
12716 RNODE_DSTR(tail)->string = 0;
12717 }
12718 break;
12719
12720 case NODE_EVSTR:
12721 if (htype == NODE_STR) {
12722 head = str2dstr(p, head);
12723 RNODE_DSTR(head)->as.nd_alen = 1;
12724 }
12725 list_append(p, head, tail);
12726 break;
12727 }
12728 return head;
12729}
12730
12731static void
12732nd_copy_flag(NODE *new_node, NODE *old_node)
12733{
12734 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12735 nd_set_line(new_node, nd_line(old_node));
12736 new_node->nd_loc = old_node->nd_loc;
12737 new_node->node_id = old_node->node_id;
12738}
12739
12740static NODE *
12741str2dstr(struct parser_params *p, NODE *node)
12742{
12743 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12744 nd_copy_flag(new_node, node);
12745 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12746 RNODE_DSTR(new_node)->as.nd_alen = 0;
12747 RNODE_DSTR(new_node)->nd_next = 0;
12748 RNODE_STR(node)->string = 0;
12749
12750 return new_node;
12751}
12752
12753static NODE *
12754str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12755{
12756 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12757 nd_copy_flag(new_node, node);
12758 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12759 RNODE_REGX(new_node)->options = options;
12760 nd_set_loc(new_node, loc);
12761 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12762 RNODE_REGX(new_node)->content_loc = *content_loc;
12763 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12764 RNODE_STR(node)->string = 0;
12765
12766 return new_node;
12767}
12768
12769static NODE *
12770evstr2dstr(struct parser_params *p, NODE *node)
12771{
12772 if (nd_type_p(node, NODE_EVSTR)) {
12773 node = new_dstr(p, node, &node->nd_loc);
12774 }
12775 return node;
12776}
12777
12778static NODE *
12779new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12780{
12781 NODE *head = node;
12782
12783 if (node) {
12784 switch (nd_type(node)) {
12785 case NODE_STR:
12786 return str2dstr(p, node);
12787 case NODE_DSTR:
12788 break;
12789 case NODE_EVSTR:
12790 return node;
12791 }
12792 }
12793 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12794}
12795
12796static NODE *
12797new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12798{
12799 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12800 return list_append(p, dstr, node);
12801}
12802
12803static NODE *
12804call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12805 const YYLTYPE *op_loc, const YYLTYPE *loc)
12806{
12807 NODE *expr;
12808 value_expr(recv);
12809 value_expr(arg1);
12810 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12811 nd_set_line(expr, op_loc->beg_pos.lineno);
12812 return expr;
12813}
12814
12815static NODE *
12816call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12817{
12818 NODE *opcall;
12819 value_expr(recv);
12820 opcall = NEW_OPCALL(recv, id, 0, loc);
12821 nd_set_line(opcall, op_loc->beg_pos.lineno);
12822 return opcall;
12823}
12824
12825static NODE *
12826new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12827{
12828 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12829 nd_set_line(qcall, op_loc->beg_pos.lineno);
12830 return qcall;
12831}
12832
12833static NODE*
12834new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12835{
12836 NODE *ret;
12837 if (block) block_dup_check(p, args, block);
12838 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12839 if (block) ret = method_add_block(p, ret, block, loc);
12840 fixpos(ret, recv);
12841 return ret;
12842}
12843
12844static rb_locations_lambda_body_t*
12845new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12846{
12847 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12848 body->node = node;
12849 body->opening_loc = *opening_loc;
12850 body->closing_loc = *closing_loc;
12851 return body;
12852}
12853
12854#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12855
12856static NODE*
12857last_expr_once_body(NODE *node)
12858{
12859 if (!node) return 0;
12860 return nd_once_body(node);
12861}
12862
12863static NODE*
12864match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12865{
12866 NODE *n;
12867 int line = op_loc->beg_pos.lineno;
12868
12869 value_expr(node1);
12870 value_expr(node2);
12871
12872 if ((n = last_expr_once_body(node1)) != 0) {
12873 switch (nd_type(n)) {
12874 case NODE_DREGX:
12875 {
12876 NODE *match = NEW_MATCH2(node1, node2, loc);
12877 nd_set_line(match, line);
12878 return match;
12879 }
12880
12881 case NODE_REGX:
12882 {
12883 const VALUE lit = rb_node_regx_string_val(n);
12884 if (!NIL_P(lit)) {
12885 NODE *match = NEW_MATCH2(node1, node2, loc);
12886 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12887 nd_set_line(match, line);
12888 return match;
12889 }
12890 }
12891 }
12892 }
12893
12894 if ((n = last_expr_once_body(node2)) != 0) {
12895 NODE *match3;
12896
12897 switch (nd_type(n)) {
12898 case NODE_DREGX:
12899 match3 = NEW_MATCH3(node2, node1, loc);
12900 return match3;
12901 }
12902 }
12903
12904 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12905 nd_set_line(n, line);
12906 return n;
12907}
12908
12909# if WARN_PAST_SCOPE
12910static int
12911past_dvar_p(struct parser_params *p, ID id)
12912{
12913 struct vtable *past = p->lvtbl->past;
12914 while (past) {
12915 if (vtable_included(past, id)) return 1;
12916 past = past->prev;
12917 }
12918 return 0;
12919}
12920# endif
12921
12922static int
12923numparam_nested_p(struct parser_params *p)
12924{
12925 struct local_vars *local = p->lvtbl;
12926 NODE *outer = local->numparam.outer;
12927 NODE *inner = local->numparam.inner;
12928 if (outer || inner) {
12929 NODE *used = outer ? outer : inner;
12930 compile_error(p, "numbered parameter is already used in %s block\n"
12931 "%s:%d: numbered parameter is already used here",
12932 outer ? "outer" : "inner",
12933 p->ruby_sourcefile, nd_line(used));
12934 parser_show_error_line(p, &used->nd_loc);
12935 return 1;
12936 }
12937 return 0;
12938}
12939
12940static int
12941numparam_used_p(struct parser_params *p)
12942{
12943 NODE *numparam = p->lvtbl->numparam.current;
12944 if (numparam) {
12945 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
12946 "%s:%d: numbered parameter is already used here",
12947 p->ruby_sourcefile, nd_line(numparam));
12948 parser_show_error_line(p, &numparam->nd_loc);
12949 return 1;
12950 }
12951 return 0;
12952}
12953
12954static int
12955it_used_p(struct parser_params *p)
12956{
12957 NODE *it = p->lvtbl->it;
12958 if (it) {
12959 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
12960 "%s:%d: 'it' is already used here",
12961 p->ruby_sourcefile, nd_line(it));
12962 parser_show_error_line(p, &it->nd_loc);
12963 return 1;
12964 }
12965 return 0;
12966}
12967
12968static NODE*
12969gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12970{
12971 ID *vidp = NULL;
12972 NODE *node;
12973 switch (id) {
12974 case keyword_self:
12975 return NEW_SELF(loc);
12976 case keyword_nil:
12977 return NEW_NIL(loc);
12978 case keyword_true:
12979 return NEW_TRUE(loc);
12980 case keyword_false:
12981 return NEW_FALSE(loc);
12982 case keyword__FILE__:
12983 {
12984 VALUE file = p->ruby_sourcefile_string;
12985 if (NIL_P(file))
12986 file = rb_str_new(0, 0);
12987 node = NEW_FILE(file, loc);
12988 }
12989 return node;
12990 case keyword__LINE__:
12991 return NEW_LINE(loc);
12992 case keyword__ENCODING__:
12993 return NEW_ENCODING(loc);
12994
12995 }
12996 switch (id_type(id)) {
12997 case ID_LOCAL:
12998 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
12999 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13000 if (vidp) *vidp |= LVAR_USED;
13001 node = NEW_DVAR(id, loc);
13002 return node;
13003 }
13004 if (local_id_ref(p, id, &vidp)) {
13005 if (vidp) *vidp |= LVAR_USED;
13006 node = NEW_LVAR(id, loc);
13007 return node;
13008 }
13009 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13010 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13011 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13012 node = NEW_DVAR(id, loc);
13013 struct local_vars *local = p->lvtbl;
13014 if (!local->numparam.current) local->numparam.current = node;
13015 return node;
13016 }
13017# if WARN_PAST_SCOPE
13018 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13019 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13020 }
13021# endif
13022 /* method call without arguments */
13023 if (dyna_in_block(p) && id == rb_intern("it") && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13024 if (numparam_used_p(p)) return 0;
13025 if (p->max_numparam == ORDINAL_PARAM) {
13026 compile_error(p, "ordinary parameter is defined");
13027 return 0;
13028 }
13029 if (!p->it_id) {
13030 p->it_id = internal_id(p);
13031 vtable_add(p->lvtbl->args, p->it_id);
13032 }
13033 NODE *node = NEW_DVAR(p->it_id, loc);
13034 if (!p->lvtbl->it) p->lvtbl->it = node;
13035 return node;
13036 }
13037 return NEW_VCALL(id, loc);
13038 case ID_GLOBAL:
13039 return NEW_GVAR(id, loc);
13040 case ID_INSTANCE:
13041 return NEW_IVAR(id, loc);
13042 case ID_CONST:
13043 return NEW_CONST(id, loc);
13044 case ID_CLASS:
13045 return NEW_CVAR(id, loc);
13046 }
13047 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13048 return 0;
13049}
13050
13051static rb_node_opt_arg_t *
13052opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13053{
13054 rb_node_opt_arg_t *opts = opt_list;
13055 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13056
13057 while (opts->nd_next) {
13058 opts = opts->nd_next;
13059 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13060 }
13061 opts->nd_next = opt;
13062
13063 return opt_list;
13064}
13065
13066static rb_node_kw_arg_t *
13067kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13068{
13069 if (kwlist) {
13070 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13071 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13072 }
13073 return kwlist;
13074}
13075
13076static NODE *
13077new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
13078{
13079 int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13080 p->ctxt.has_trailing_semicolon = 0;
13081
13082 NODE *n = expr;
13083 while (n) {
13084 if (nd_type_p(n, NODE_BEGIN)) {
13085 n = RNODE_BEGIN(n)->nd_body;
13086 }
13087 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13088 n = RNODE_BLOCK(n)->nd_head;
13089 }
13090 else {
13091 break;
13092 }
13093 }
13094
13095 if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13096 NODE *block = NEW_BLOCK(expr, loc);
13097 return NEW_DEFINED(block, loc, keyword_loc);
13098 }
13099
13100 return NEW_DEFINED(n, loc, keyword_loc);
13101}
13102
13103static NODE*
13104str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13105{
13106 VALUE lit;
13107 rb_parser_string_t *str = RNODE_STR(node)->string;
13108 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13109 yyerror1(loc, "invalid symbol");
13110 lit = STR_NEW0();
13111 }
13112 else {
13113 lit = rb_str_new_parser_string(str);
13114 }
13115 return NEW_SYM(lit, loc);
13116}
13117
13118static NODE*
13119symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13120{
13121 enum node_type type = nd_type(symbol);
13122 switch (type) {
13123 case NODE_DSTR:
13124 nd_set_type(symbol, NODE_DSYM);
13125 break;
13126 case NODE_STR:
13127 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13128 break;
13129 default:
13130 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13131 }
13132 return list_append(p, symbols, symbol);
13133}
13134
13135static void
13136dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13137{
13138 if (dreg->string) {
13139 reg_fragment_setenc(p, dreg->string, options);
13140 }
13141 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13142 NODE *frag = list->nd_head;
13143 if (nd_type_p(frag, NODE_STR)) {
13144 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13145 }
13146 else if (nd_type_p(frag, NODE_DSTR)) {
13147 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13148 }
13149 }
13150}
13151
13152static NODE *
13153new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13154{
13155 if (!node) {
13156 /* Check string is valid regex */
13157 rb_parser_string_t *str = STRING_NEW0();
13158 reg_compile(p, str, options);
13159 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13160 return node;
13161 }
13162 switch (nd_type(node)) {
13163 case NODE_STR:
13164 {
13165 /* Check string is valid regex */
13166 reg_compile(p, RNODE_STR(node)->string, options);
13167 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13168 }
13169 break;
13170 default:
13171 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13172 /* fall through */
13173 case NODE_DSTR:
13174 nd_set_type(node, NODE_DREGX);
13175 nd_set_loc(node, loc);
13176 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13177 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13178 if (dreg->nd_next) {
13179 dregex_fragment_setenc(p, dreg, options);
13180 }
13181 if (options & RE_OPTION_ONCE) {
13182 node = NEW_ONCE(node, loc);
13183 }
13184 break;
13185 }
13186 return node;
13187}
13188
13189static rb_node_kw_arg_t *
13190new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13191{
13192 if (!k) return 0;
13193 return NEW_KW_ARG((k), loc);
13194}
13195
13196static NODE *
13197new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13198{
13199 if (!node) {
13200 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13201 return xstr;
13202 }
13203 switch (nd_type(node)) {
13204 case NODE_STR:
13205 nd_set_type(node, NODE_XSTR);
13206 nd_set_loc(node, loc);
13207 break;
13208 case NODE_DSTR:
13209 nd_set_type(node, NODE_DXSTR);
13210 nd_set_loc(node, loc);
13211 break;
13212 default:
13213 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13214 break;
13215 }
13216 return node;
13217}
13218
13219static const
13220struct st_hash_type literal_type = {
13221 literal_cmp,
13222 literal_hash,
13223};
13224
13225static int nd_type_st_key_enable_p(NODE *node);
13226
13227static void
13228check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13229{
13230 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13231 if (!arg || !p->case_labels) return;
13232 if (!nd_type_st_key_enable_p(arg)) return;
13233
13234 if (p->case_labels == CHECK_LITERAL_WHEN) {
13235 p->case_labels = st_init_table(&literal_type);
13236 }
13237 else {
13238 st_data_t line;
13239 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13240 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13241 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13242 return;
13243 }
13244 }
13245 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13246}
13247
13248#ifdef RIPPER
13249static int
13250id_is_var(struct parser_params *p, ID id)
13251{
13252 if (is_notop_id(id)) {
13253 switch (id & ID_SCOPE_MASK) {
13254 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13255 return 1;
13256 case ID_LOCAL:
13257 if (dyna_in_block(p)) {
13258 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13259 }
13260 if (local_id(p, id)) return 1;
13261 /* method call without arguments */
13262 return 0;
13263 }
13264 }
13265 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13266 return 0;
13267}
13268#endif
13269
13270static inline enum lex_state_e
13271parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13272{
13273 if (p->debug) {
13274 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13275 }
13276 return p->lex.state = ls;
13277}
13278
13279#ifndef RIPPER
13280static void
13281flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13282{
13283 VALUE mesg = p->debug_buffer;
13284
13285 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13286 p->debug_buffer = Qnil;
13287 rb_io_puts(1, &mesg, out);
13288 }
13289 if (!NIL_P(str) && RSTRING_LEN(str)) {
13290 rb_io_write(p->debug_output, str);
13291 }
13292}
13293
13294static const char rb_parser_lex_state_names[][8] = {
13295 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13296 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13297 "LABEL", "LABELED","FITEM",
13298};
13299
13300static VALUE
13301append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13302{
13303 int i, sep = 0;
13304 unsigned int mask = 1;
13305 static const char none[] = "NONE";
13306
13307 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13308 if ((unsigned)state & mask) {
13309 if (sep) {
13310 rb_str_cat(buf, "|", 1);
13311 }
13312 sep = 1;
13313 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13314 }
13315 }
13316 if (!sep) {
13317 rb_str_cat(buf, none, sizeof(none)-1);
13318 }
13319 return buf;
13320}
13321
13322enum lex_state_e
13323rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13324 enum lex_state_e to, int line)
13325{
13326 VALUE mesg;
13327 mesg = rb_str_new_cstr("lex_state: ");
13328 append_lex_state_name(p, from, mesg);
13329 rb_str_cat_cstr(mesg, " -> ");
13330 append_lex_state_name(p, to, mesg);
13331 rb_str_catf(mesg, " at line %d\n", line);
13332 flush_debug_buffer(p, p->debug_output, mesg);
13333 return to;
13334}
13335
13336VALUE
13337rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13338{
13339 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13340}
13341
13342static void
13343append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13344{
13345 if (stack == 0) {
13346 rb_str_cat_cstr(mesg, "0");
13347 }
13348 else {
13349 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13350 for (; mask && !(stack & mask); mask >>= 1) continue;
13351 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13352 }
13353}
13354
13355void
13356rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13357 const char *name, int line)
13358{
13359 VALUE mesg = rb_sprintf("%s: ", name);
13360 append_bitstack_value(p, stack, mesg);
13361 rb_str_catf(mesg, " at line %d\n", line);
13362 flush_debug_buffer(p, p->debug_output, mesg);
13363}
13364
13365void
13366rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13367{
13368 va_list ap;
13369 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13370
13371 va_start(ap, fmt);
13372 rb_str_vcatf(mesg, fmt, ap);
13373 va_end(ap);
13374 yyerror0(RSTRING_PTR(mesg));
13375 RB_GC_GUARD(mesg);
13376
13377 mesg = rb_str_new(0, 0);
13378 append_lex_state_name(p, p->lex.state, mesg);
13379 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13380 rb_str_resize(mesg, 0);
13381 append_bitstack_value(p, p->cond_stack, mesg);
13382 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13383 rb_str_resize(mesg, 0);
13384 append_bitstack_value(p, p->cmdarg_stack, mesg);
13385 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13386 if (p->debug_output == rb_ractor_stdout())
13387 p->debug_output = rb_ractor_stderr();
13388 p->debug = TRUE;
13389}
13390
13391static YYLTYPE *
13392rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13393{
13394 yylloc->beg_pos.lineno = sourceline;
13395 yylloc->beg_pos.column = beg_pos;
13396 yylloc->end_pos.lineno = sourceline;
13397 yylloc->end_pos.column = end_pos;
13398 return yylloc;
13399}
13400
13401YYLTYPE *
13402rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13403{
13404 int sourceline = here->sourceline;
13405 int beg_pos = (int)here->offset - here->quote
13406 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13407 int end_pos = (int)here->offset + here->length + here->quote;
13408
13409 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13410}
13411
13412YYLTYPE *
13413rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13414{
13415 yylloc->beg_pos.lineno = p->delayed.beg_line;
13416 yylloc->beg_pos.column = p->delayed.beg_col;
13417 yylloc->end_pos.lineno = p->delayed.end_line;
13418 yylloc->end_pos.column = p->delayed.end_col;
13419
13420 return yylloc;
13421}
13422
13423YYLTYPE *
13424rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13425{
13426 int sourceline = p->ruby_sourceline;
13427 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13428 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13429 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13430}
13431
13432YYLTYPE *
13433rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13434{
13435 yylloc->end_pos = yylloc->beg_pos;
13436
13437 return yylloc;
13438}
13439
13440YYLTYPE *
13441rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13442{
13443 int sourceline = p->ruby_sourceline;
13444 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13445 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13446 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13447}
13448
13449YYLTYPE *
13450rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13451{
13452 int sourceline = p->ruby_sourceline;
13453 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13454 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13455 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13456}
13457#endif /* !RIPPER */
13458
13459static int
13460assignable0(struct parser_params *p, ID id, const char **err)
13461{
13462 if (!id) return -1;
13463 switch (id) {
13464 case keyword_self:
13465 *err = "Can't change the value of self";
13466 return -1;
13467 case keyword_nil:
13468 *err = "Can't assign to nil";
13469 return -1;
13470 case keyword_true:
13471 *err = "Can't assign to true";
13472 return -1;
13473 case keyword_false:
13474 *err = "Can't assign to false";
13475 return -1;
13476 case keyword__FILE__:
13477 *err = "Can't assign to __FILE__";
13478 return -1;
13479 case keyword__LINE__:
13480 *err = "Can't assign to __LINE__";
13481 return -1;
13482 case keyword__ENCODING__:
13483 *err = "Can't assign to __ENCODING__";
13484 return -1;
13485 }
13486 switch (id_type(id)) {
13487 case ID_LOCAL:
13488 if (dyna_in_block(p)) {
13489 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13490 compile_error(p, "Can't assign to numbered parameter _%d",
13491 NUMPARAM_ID_TO_IDX(id));
13492 return -1;
13493 }
13494 if (dvar_curr(p, id)) return NODE_DASGN;
13495 if (dvar_defined(p, id)) return NODE_DASGN;
13496 if (local_id(p, id)) return NODE_LASGN;
13497 dyna_var(p, id);
13498 return NODE_DASGN;
13499 }
13500 else {
13501 if (!local_id(p, id)) local_var(p, id);
13502 return NODE_LASGN;
13503 }
13504 break;
13505 case ID_GLOBAL: return NODE_GASGN;
13506 case ID_INSTANCE: return NODE_IASGN;
13507 case ID_CONST:
13508 if (!p->ctxt.in_def) return NODE_CDECL;
13509 *err = "dynamic constant assignment";
13510 return -1;
13511 case ID_CLASS: return NODE_CVASGN;
13512 default:
13513 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13514 }
13515 return -1;
13516}
13517
13518static NODE*
13519assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13520{
13521 const char *err = 0;
13522 int node_type = assignable0(p, id, &err);
13523 switch (node_type) {
13524 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13525 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13526 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13527 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13528 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13529 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13530 }
13531/* TODO: FIXME */
13532#ifndef RIPPER
13533 if (err) yyerror1(loc, err);
13534#else
13535 if (err) set_value(assign_error(p, err, p->s_lvalue));
13536#endif
13537 return NEW_ERROR(loc);
13538}
13539
13540static int
13541is_private_local_id(struct parser_params *p, ID name)
13542{
13543 VALUE s;
13544 if (name == idUScore) return 1;
13545 if (!is_local_id(name)) return 0;
13546 s = rb_id2str(name);
13547 if (!s) return 0;
13548 return RSTRING_PTR(s)[0] == '_';
13549}
13550
13551static int
13552shadowing_lvar_0(struct parser_params *p, ID name)
13553{
13554 if (dyna_in_block(p)) {
13555 if (dvar_curr(p, name)) {
13556 if (is_private_local_id(p, name)) return 1;
13557 yyerror0("duplicated argument name");
13558 }
13559 else if (dvar_defined(p, name) || local_id(p, name)) {
13560 vtable_add(p->lvtbl->vars, name);
13561 if (p->lvtbl->used) {
13562 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13563 }
13564 return 0;
13565 }
13566 }
13567 else {
13568 if (local_id(p, name)) {
13569 if (is_private_local_id(p, name)) return 1;
13570 yyerror0("duplicated argument name");
13571 }
13572 }
13573 return 1;
13574}
13575
13576static ID
13577shadowing_lvar(struct parser_params *p, ID name)
13578{
13579 shadowing_lvar_0(p, name);
13580 return name;
13581}
13582
13583static void
13584new_bv(struct parser_params *p, ID name)
13585{
13586 if (!name) return;
13587 if (!is_local_id(name)) {
13588 compile_error(p, "invalid local variable - %"PRIsVALUE,
13589 rb_id2str(name));
13590 return;
13591 }
13592 if (!shadowing_lvar_0(p, name)) return;
13593 dyna_var(p, name);
13594 ID *vidp = 0;
13595 if (dvar_defined_ref(p, name, &vidp)) {
13596 if (vidp) *vidp |= LVAR_USED;
13597 }
13598}
13599
13600static void
13601aryset_check(struct parser_params *p, NODE *args)
13602{
13603 NODE *block = 0, *kwds = 0;
13604 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13605 block = RNODE_BLOCK_PASS(args)->nd_body;
13606 args = RNODE_BLOCK_PASS(args)->nd_head;
13607 }
13608 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13609 args = RNODE_ARGSCAT(args)->nd_body;
13610 }
13611 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13612 kwds = RNODE_ARGSPUSH(args)->nd_body;
13613 }
13614 else {
13615 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13616 next = RNODE_LIST(next)->nd_next) {
13617 kwds = RNODE_LIST(next)->nd_head;
13618 }
13619 }
13620 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13621 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13622 }
13623 if (block) {
13624 yyerror1(&block->nd_loc, "block arg given in index assignment");
13625 }
13626}
13627
13628static NODE *
13629aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13630{
13631 aryset_check(p, idx);
13632 return NEW_ATTRASGN(recv, tASET, idx, loc);
13633}
13634
13635static void
13636block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13637{
13638 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13639 compile_error(p, "both block arg and actual block given");
13640 }
13641}
13642
13643static NODE *
13644attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13645{
13646 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13647 return NEW_ATTRASGN(recv, id, 0, loc);
13648}
13649
13650static VALUE
13651rb_backref_error(struct parser_params *p, NODE *node)
13652{
13653#ifndef RIPPER
13654# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13655#else
13656# define ERR(...) rb_sprintf(__VA_ARGS__)
13657#endif
13658 switch (nd_type(node)) {
13659 case NODE_NTH_REF:
13660 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13661 case NODE_BACK_REF:
13662 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13663 }
13664#undef ERR
13665 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13666}
13667
13668static NODE *
13669arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13670{
13671 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13672 switch (nd_type(node1)) {
13673 case NODE_LIST:
13674 return list_append(p, node1, node2);
13675 case NODE_BLOCK_PASS:
13676 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13677 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13678 return node1;
13679 case NODE_ARGSPUSH:
13680 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13681 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13682 nd_set_type(node1, NODE_ARGSCAT);
13683 return node1;
13684 case NODE_ARGSCAT:
13685 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13686 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13687 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13688 return node1;
13689 }
13690 return NEW_ARGSPUSH(node1, node2, loc);
13691}
13692
13693static NODE *
13694arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13695{
13696 if (!node2) return node1;
13697 switch (nd_type(node1)) {
13698 case NODE_BLOCK_PASS:
13699 if (RNODE_BLOCK_PASS(node1)->nd_head)
13700 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13701 else
13702 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13703 return node1;
13704 case NODE_ARGSPUSH:
13705 if (!nd_type_p(node2, NODE_LIST)) break;
13706 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13707 nd_set_type(node1, NODE_ARGSCAT);
13708 return node1;
13709 case NODE_ARGSCAT:
13710 if (!nd_type_p(node2, NODE_LIST) ||
13711 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13712 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13713 return node1;
13714 }
13715 return NEW_ARGSCAT(node1, node2, loc);
13716}
13717
13718static NODE *
13719last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13720{
13721 NODE *n1;
13722 if ((n1 = splat_array(args)) != 0) {
13723 return list_append(p, n1, last_arg);
13724 }
13725 return arg_append(p, args, last_arg, loc);
13726}
13727
13728static NODE *
13729rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13730{
13731 NODE *n1;
13732 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13733 return list_concat(n1, rest_arg);
13734 }
13735 return arg_concat(p, args, rest_arg, loc);
13736}
13737
13738static NODE *
13739splat_array(NODE* node)
13740{
13741 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13742 if (nd_type_p(node, NODE_LIST)) return node;
13743 return 0;
13744}
13745
13746static void
13747mark_lvar_used(struct parser_params *p, NODE *rhs)
13748{
13749 ID *vidp = NULL;
13750 if (!rhs) return;
13751 switch (nd_type(rhs)) {
13752 case NODE_LASGN:
13753 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13754 if (vidp) *vidp |= LVAR_USED;
13755 }
13756 break;
13757 case NODE_DASGN:
13758 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13759 if (vidp) *vidp |= LVAR_USED;
13760 }
13761 break;
13762#if 0
13763 case NODE_MASGN:
13764 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13765 mark_lvar_used(p, rhs->nd_head);
13766 }
13767 break;
13768#endif
13769 }
13770}
13771
13772static int is_static_content(NODE *node);
13773
13774static NODE *
13775node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13776{
13777 if (!lhs) return 0;
13778
13779 switch (nd_type(lhs)) {
13780 case NODE_CDECL:
13781 case NODE_GASGN:
13782 case NODE_IASGN:
13783 case NODE_LASGN:
13784 case NODE_DASGN:
13785 case NODE_MASGN:
13786 case NODE_CVASGN:
13787 set_nd_value(p, lhs, rhs);
13788 nd_set_loc(lhs, loc);
13789 break;
13790
13791 case NODE_ATTRASGN:
13792 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13793 nd_set_loc(lhs, loc);
13794 break;
13795
13796 default:
13797 /* should not happen */
13798 break;
13799 }
13800
13801 return lhs;
13802}
13803
13804static NODE *
13805value_expr_check(struct parser_params *p, NODE *node)
13806{
13807 NODE *void_node = 0, *vn;
13808
13809 if (!node) {
13810 rb_warning0("empty expression");
13811 }
13812 while (node) {
13813 switch (nd_type(node)) {
13814 case NODE_ENSURE:
13815 vn = RNODE_ENSURE(node)->nd_head;
13816 node = RNODE_ENSURE(node)->nd_ensr;
13817 /* nd_ensr should not be NULL, check it out next */
13818 if (vn && (vn = value_expr_check(p, vn))) {
13819 goto found;
13820 }
13821 break;
13822
13823 case NODE_RESCUE:
13824 /* void only if all children are void */
13825 vn = RNODE_RESCUE(node)->nd_head;
13826 if (!vn || !(vn = value_expr_check(p, vn))) return NULL;
13827 if (!void_node) void_node = vn;
13828 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13829 if (!nd_type_p(r, NODE_RESBODY)) {
13830 compile_error(p, "unexpected node");
13831 return NULL;
13832 }
13833 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13834 void_node = 0;
13835 break;
13836 }
13837 if (!void_node) void_node = vn;
13838 }
13839 node = RNODE_RESCUE(node)->nd_else;
13840 if (!node) return void_node;
13841 break;
13842
13843 case NODE_RETURN:
13844 case NODE_BREAK:
13845 case NODE_NEXT:
13846 case NODE_REDO:
13847 case NODE_RETRY:
13848 goto found;
13849
13850 case NODE_CASE3:
13851 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13852 compile_error(p, "unexpected node");
13853 return NULL;
13854 }
13855 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13856 return NULL;
13857 }
13858 /* single line pattern matching with "=>" operator */
13859 goto found;
13860
13861 case NODE_BLOCK:
13862 while (RNODE_BLOCK(node)->nd_next) {
13863 node = RNODE_BLOCK(node)->nd_next;
13864 }
13865 node = RNODE_BLOCK(node)->nd_head;
13866 break;
13867
13868 case NODE_BEGIN:
13869 node = RNODE_BEGIN(node)->nd_body;
13870 break;
13871
13872 case NODE_IF:
13873 case NODE_UNLESS:
13874 if (!RNODE_IF(node)->nd_body) {
13875 return NULL;
13876 }
13877 else if (!RNODE_IF(node)->nd_else) {
13878 return NULL;
13879 }
13880 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13881 if (!vn) return NULL;
13882 if (!void_node) void_node = vn;
13883 node = RNODE_IF(node)->nd_else;
13884 break;
13885
13886 case NODE_AND:
13887 case NODE_OR:
13888 node = RNODE_AND(node)->nd_1st;
13889 break;
13890
13891 case NODE_LASGN:
13892 case NODE_DASGN:
13893 case NODE_MASGN:
13894 mark_lvar_used(p, node);
13895 return NULL;
13896
13897 default:
13898 return NULL;
13899 }
13900 }
13901
13902 return NULL;
13903
13904 found:
13905 /* return the first found node */
13906 return void_node ? void_node : node;
13907}
13908
13909static int
13910value_expr_gen(struct parser_params *p, NODE *node)
13911{
13912 NODE *void_node = value_expr_check(p, node);
13913 if (void_node) {
13914 yyerror1(&void_node->nd_loc, "void value expression");
13915 /* or "control never reach"? */
13916 return FALSE;
13917 }
13918 return TRUE;
13919}
13920
13921static void
13922void_expr(struct parser_params *p, NODE *node)
13923{
13924 const char *useless = 0;
13925
13926 if (!RTEST(ruby_verbose)) return;
13927
13928 if (!node || !(node = nd_once_body(node))) return;
13929 switch (nd_type(node)) {
13930 case NODE_OPCALL:
13931 switch (RNODE_OPCALL(node)->nd_mid) {
13932 case '+':
13933 case '-':
13934 case '*':
13935 case '/':
13936 case '%':
13937 case tPOW:
13938 case tUPLUS:
13939 case tUMINUS:
13940 case '|':
13941 case '^':
13942 case '&':
13943 case tCMP:
13944 case '>':
13945 case tGEQ:
13946 case '<':
13947 case tLEQ:
13948 case tEQ:
13949 case tNEQ:
13950 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13951 break;
13952 }
13953 break;
13954
13955 case NODE_LVAR:
13956 case NODE_DVAR:
13957 case NODE_GVAR:
13958 case NODE_IVAR:
13959 case NODE_CVAR:
13960 case NODE_NTH_REF:
13961 case NODE_BACK_REF:
13962 useless = "a variable";
13963 break;
13964 case NODE_CONST:
13965 useless = "a constant";
13966 break;
13967 case NODE_SYM:
13968 case NODE_LINE:
13969 case NODE_FILE:
13970 case NODE_ENCODING:
13971 case NODE_INTEGER:
13972 case NODE_FLOAT:
13973 case NODE_RATIONAL:
13974 case NODE_IMAGINARY:
13975 case NODE_STR:
13976 case NODE_DSTR:
13977 case NODE_REGX:
13978 case NODE_DREGX:
13979 useless = "a literal";
13980 break;
13981 case NODE_COLON2:
13982 case NODE_COLON3:
13983 useless = "::";
13984 break;
13985 case NODE_DOT2:
13986 useless = "..";
13987 break;
13988 case NODE_DOT3:
13989 useless = "...";
13990 break;
13991 case NODE_SELF:
13992 useless = "self";
13993 break;
13994 case NODE_NIL:
13995 useless = "nil";
13996 break;
13997 case NODE_TRUE:
13998 useless = "true";
13999 break;
14000 case NODE_FALSE:
14001 useless = "false";
14002 break;
14003 case NODE_DEFINED:
14004 useless = "defined?";
14005 break;
14006 }
14007
14008 if (useless) {
14009 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14010 }
14011}
14012
14013/* warns useless use of block and returns the last statement node */
14014static NODE *
14015void_stmts(struct parser_params *p, NODE *node)
14016{
14017 NODE *const n = node;
14018 if (!RTEST(ruby_verbose)) return n;
14019 if (!node) return n;
14020 if (!nd_type_p(node, NODE_BLOCK)) return n;
14021
14022 while (RNODE_BLOCK(node)->nd_next) {
14023 void_expr(p, RNODE_BLOCK(node)->nd_head);
14024 node = RNODE_BLOCK(node)->nd_next;
14025 }
14026 return RNODE_BLOCK(node)->nd_head;
14027}
14028
14029static NODE *
14030remove_begin(NODE *node)
14031{
14032 NODE **n = &node, *n1 = node;
14033 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14034 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14035 }
14036 return node;
14037}
14038
14039static void
14040reduce_nodes(struct parser_params *p, NODE **body)
14041{
14042 NODE *node = *body;
14043
14044 if (!node) {
14045 *body = NEW_NIL(&NULL_LOC);
14046 return;
14047 }
14048#define subnodes(type, n1, n2) \
14049 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14050 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14051 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14052
14053 while (node) {
14054 int newline = (int)nd_fl_newline(node);
14055 switch (nd_type(node)) {
14056 end:
14057 case NODE_NIL:
14058 *body = 0;
14059 return;
14060 case NODE_BEGIN:
14061 *body = node = RNODE_BEGIN(node)->nd_body;
14062 if (newline && node) nd_set_fl_newline(node);
14063 continue;
14064 case NODE_BLOCK:
14065 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14066 break;
14067 case NODE_IF:
14068 case NODE_UNLESS:
14069 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14070 return;
14071 case NODE_CASE:
14072 body = &RNODE_CASE(node)->nd_body;
14073 break;
14074 case NODE_WHEN:
14075 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14076 break;
14077 case NODE_ENSURE:
14078 body = &RNODE_ENSURE(node)->nd_head;
14079 break;
14080 case NODE_RESCUE:
14081 newline = 0; // RESBODY should not be a NEWLINE
14082 if (RNODE_RESCUE(node)->nd_else) {
14083 body = &RNODE_RESCUE(node)->nd_resq;
14084 break;
14085 }
14086 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14087 break;
14088 default:
14089 return;
14090 }
14091 node = *body;
14092 if (newline && node) nd_set_fl_newline(node);
14093 }
14094
14095#undef subnodes
14096}
14097
14098static int
14099is_static_content(NODE *node)
14100{
14101 if (!node) return 1;
14102 switch (nd_type(node)) {
14103 case NODE_HASH:
14104 if (!(node = RNODE_HASH(node)->nd_head)) break;
14105 case NODE_LIST:
14106 do {
14107 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14108 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14109 case NODE_SYM:
14110 case NODE_REGX:
14111 case NODE_LINE:
14112 case NODE_FILE:
14113 case NODE_ENCODING:
14114 case NODE_INTEGER:
14115 case NODE_FLOAT:
14116 case NODE_RATIONAL:
14117 case NODE_IMAGINARY:
14118 case NODE_STR:
14119 case NODE_NIL:
14120 case NODE_TRUE:
14121 case NODE_FALSE:
14122 case NODE_ZLIST:
14123 break;
14124 default:
14125 return 0;
14126 }
14127 return 1;
14128}
14129
14130static int
14131assign_in_cond(struct parser_params *p, NODE *node)
14132{
14133 switch (nd_type(node)) {
14134 case NODE_MASGN:
14135 case NODE_LASGN:
14136 case NODE_DASGN:
14137 case NODE_GASGN:
14138 case NODE_IASGN:
14139 case NODE_CVASGN:
14140 case NODE_CDECL:
14141 break;
14142
14143 default:
14144 return 0;
14145 }
14146
14147 if (!get_nd_value(p, node)) return 1;
14148 if (is_static_content(get_nd_value(p, node))) {
14149 /* reports always */
14150 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14151 }
14152 return 1;
14153}
14154
14155enum cond_type {
14156 COND_IN_OP,
14157 COND_IN_COND,
14158 COND_IN_FF
14159};
14160
14161#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14162 switch (t) { \
14163 case COND_IN_OP: break; \
14164 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14165 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14166 } \
14167} while (0)
14168
14169static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14170
14171static NODE*
14172range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14173{
14174 enum node_type type;
14175
14176 if (node == 0) return 0;
14177
14178 type = nd_type(node);
14179 value_expr(node);
14180 if (type == NODE_INTEGER) {
14181 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14182 ID lineno = rb_intern("$.");
14183 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14184 }
14185 return cond0(p, node, COND_IN_FF, loc, true);
14186}
14187
14188static NODE*
14189cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14190{
14191 if (node == 0) return 0;
14192 if (!(node = nd_once_body(node))) return 0;
14193 assign_in_cond(p, node);
14194
14195 switch (nd_type(node)) {
14196 case NODE_BEGIN:
14197 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14198 break;
14199
14200 case NODE_DSTR:
14201 case NODE_EVSTR:
14202 case NODE_STR:
14203 case NODE_FILE:
14204 SWITCH_BY_COND_TYPE(type, warn, "string ");
14205 break;
14206
14207 case NODE_REGX:
14208 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14209 nd_set_type(node, NODE_MATCH);
14210 break;
14211
14212 case NODE_DREGX:
14213 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14214
14215 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14216
14217 case NODE_BLOCK:
14218 {
14219 NODE *end = RNODE_BLOCK(node)->nd_end;
14220 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14221 if (top) top = node == end;
14222 *expr = cond0(p, *expr, type, loc, top);
14223 }
14224 break;
14225
14226 case NODE_AND:
14227 case NODE_OR:
14228 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14229 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14230 break;
14231
14232 case NODE_DOT2:
14233 case NODE_DOT3:
14234 if (!top) break;
14235 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14236 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14237 switch (nd_type(node)) {
14238 case NODE_DOT2:
14239 nd_set_type(node,NODE_FLIP2);
14240 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14241 (void)flip2;
14242 break;
14243 case NODE_DOT3:
14244 nd_set_type(node, NODE_FLIP3);
14245 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14246 (void)flip3;
14247 break;
14248 }
14249 break;
14250
14251 case NODE_SYM:
14252 case NODE_DSYM:
14253 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14254 break;
14255
14256 case NODE_LINE:
14257 case NODE_ENCODING:
14258 case NODE_INTEGER:
14259 case NODE_FLOAT:
14260 case NODE_RATIONAL:
14261 case NODE_IMAGINARY:
14262 SWITCH_BY_COND_TYPE(type, warning, "");
14263 break;
14264
14265 default:
14266 break;
14267 }
14268 return node;
14269}
14270
14271static NODE*
14272cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14273{
14274 if (node == 0) return 0;
14275 return cond0(p, node, COND_IN_COND, loc, true);
14276}
14277
14278static NODE*
14279method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14280{
14281 if (node == 0) return 0;
14282 return cond0(p, node, COND_IN_OP, loc, true);
14283}
14284
14285static NODE*
14286new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14287{
14288 YYLTYPE loc = {*pos, *pos};
14289 return NEW_NIL(&loc);
14290}
14291
14292static NODE*
14293new_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)
14294{
14295 if (!cc) return right;
14296 cc = cond0(p, cc, COND_IN_COND, loc, true);
14297 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14298}
14299
14300static NODE*
14301new_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)
14302{
14303 if (!cc) return right;
14304 cc = cond0(p, cc, COND_IN_COND, loc, true);
14305 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14306}
14307
14308#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))
14309
14310static NODE*
14311logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14312 const YYLTYPE *op_loc, const YYLTYPE *loc)
14313{
14314 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14315 NODE *op;
14316 value_expr(left);
14317 if (left && nd_type_p(left, type)) {
14318 NODE *node = left, *second;
14319 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14320 node = second;
14321 }
14322 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14323 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14324 left->nd_loc.end_pos = loc->end_pos;
14325 return left;
14326 }
14327 op = NEW_AND_OR(type, left, right, loc, op_loc);
14328 nd_set_line(op, op_loc->beg_pos.lineno);
14329 return op;
14330}
14331
14332#undef NEW_AND_OR
14333
14334static void
14335no_blockarg(struct parser_params *p, NODE *node)
14336{
14337 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14338 compile_error(p, "block argument should not be given");
14339 }
14340}
14341
14342static NODE *
14343ret_args(struct parser_params *p, NODE *node)
14344{
14345 if (node) {
14346 no_blockarg(p, node);
14347 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14348 node = RNODE_LIST(node)->nd_head;
14349 }
14350 }
14351 return node;
14352}
14353
14354static NODE*
14355negate_lit(struct parser_params *p, NODE* node)
14356{
14357 switch (nd_type(node)) {
14358 case NODE_INTEGER:
14359 RNODE_INTEGER(node)->minus = TRUE;
14360 break;
14361 case NODE_FLOAT:
14362 RNODE_FLOAT(node)->minus = TRUE;
14363 break;
14364 case NODE_RATIONAL:
14365 RNODE_RATIONAL(node)->minus = TRUE;
14366 break;
14367 case NODE_IMAGINARY:
14368 RNODE_IMAGINARY(node)->minus = TRUE;
14369 break;
14370 }
14371 return node;
14372}
14373
14374static NODE *
14375arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14376{
14377 if (node2) {
14378 if (!node1) return (NODE *)node2;
14379 node2->nd_head = node1;
14380 nd_set_first_lineno(node2, nd_first_lineno(node1));
14381 nd_set_first_column(node2, nd_first_column(node1));
14382 return (NODE *)node2;
14383 }
14384 return node1;
14385}
14386
14387static bool
14388args_info_empty_p(struct rb_args_info *args)
14389{
14390 if (args->pre_args_num) return false;
14391 if (args->post_args_num) return false;
14392 if (args->rest_arg) return false;
14393 if (args->opt_args) return false;
14394 if (args->block_arg) return false;
14395 if (args->kw_args) return false;
14396 if (args->kw_rest_arg) return false;
14397 return true;
14398}
14399
14400static rb_node_args_t *
14401new_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)
14402{
14403 struct rb_args_info *args = &tail->nd_ainfo;
14404
14405 if (args->forwarding) {
14406 if (rest_arg) {
14407 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14408 return tail;
14409 }
14410 rest_arg = idFWD_REST;
14411 }
14412
14413 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14414 args->pre_init = pre_args ? pre_args->nd_next : 0;
14415
14416 args->post_args_num = post_args ? post_args->nd_plen : 0;
14417 args->post_init = post_args ? post_args->nd_next : 0;
14418 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14419
14420 args->rest_arg = rest_arg;
14421
14422 args->opt_args = opt_args;
14423
14424#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14425 args->ruby2_keywords = args->forwarding;
14426#else
14427 args->ruby2_keywords = 0;
14428#endif
14429
14430 nd_set_loc(RNODE(tail), loc);
14431
14432 return tail;
14433}
14434
14435static rb_node_args_t *
14436new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14437{
14438 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14439 struct rb_args_info *args = &node->nd_ainfo;
14440 if (p->error_p) return node;
14441
14442 args->block_arg = block;
14443 args->kw_args = kw_args;
14444
14445 if (kw_args) {
14446 /*
14447 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14448 * variable order: k1, kr1, k2, &b, internal_id, krest
14449 * #=> <reorder>
14450 * variable order: kr1, k1, k2, internal_id, krest, &b
14451 */
14452 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14453 struct vtable *vtargs = p->lvtbl->args;
14454 rb_node_kw_arg_t *kwn = kw_args;
14455
14456 if (block) block = vtargs->tbl[vtargs->pos-1];
14457 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14458 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14459 while (kwn) {
14460 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14461 --kw_vars;
14462 --required_kw_vars;
14463 kwn = kwn->nd_next;
14464 }
14465
14466 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14467 ID vid = get_nd_vid(p, kwn->nd_body);
14468 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14469 *required_kw_vars++ = vid;
14470 }
14471 else {
14472 *kw_vars++ = vid;
14473 }
14474 }
14475
14476 arg_var(p, kw_bits);
14477 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14478 if (block) arg_var(p, block);
14479
14480 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14481 }
14482 else if (kw_rest_arg == idNil) {
14483 args->no_kwarg = 1;
14484 }
14485 else if (kw_rest_arg) {
14486 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14487 }
14488
14489 return node;
14490}
14491
14492static rb_node_args_t *
14493args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14494{
14495 if (max_numparam > NO_PARAM || it_id) {
14496 if (!args) {
14497 YYLTYPE loc = RUBY_INIT_YYLLOC();
14498 args = new_args_tail(p, 0, 0, 0, 0);
14499 nd_set_loc(RNODE(args), &loc);
14500 }
14501 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14502 }
14503 return args;
14504}
14505
14506static NODE*
14507new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14508{
14509 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14510
14511 if (pre_arg) {
14512 NODE *pre_args = NEW_LIST(pre_arg, loc);
14513 if (RNODE_ARYPTN(aryptn)->pre_args) {
14514 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14515 }
14516 else {
14517 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14518 }
14519 }
14520 return aryptn;
14521}
14522
14523static NODE*
14524new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14525{
14526 if (has_rest) {
14527 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14528 }
14529 else {
14530 rest_arg = NULL;
14531 }
14532 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14533
14534 return node;
14535}
14536
14537static NODE*
14538new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14539{
14540 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14541
14542 return fndptn;
14543}
14544
14545static NODE*
14546new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14547{
14548 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14549 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14550 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14551
14552 return node;
14553}
14554
14555static NODE*
14556new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14557{
14558 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14559 return hshptn;
14560}
14561
14562static NODE*
14563new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14564{
14565 NODE *node, *kw_rest_arg_node;
14566
14567 if (kw_rest_arg == idNil) {
14568 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14569 }
14570 else if (kw_rest_arg) {
14571 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14572 }
14573 else {
14574 kw_rest_arg_node = NULL;
14575 }
14576
14577 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14578
14579 return node;
14580}
14581
14582static NODE*
14583dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14584{
14585 if (!node) {
14586 return NEW_SYM(STR_NEW0(), loc);
14587 }
14588
14589 switch (nd_type(node)) {
14590 case NODE_DSTR:
14591 nd_set_type(node, NODE_DSYM);
14592 nd_set_loc(node, loc);
14593 break;
14594 case NODE_STR:
14595 node = str_to_sym_node(p, node, loc);
14596 break;
14597 default:
14598 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14599 break;
14600 }
14601 return node;
14602}
14603
14604static int
14605nd_type_st_key_enable_p(NODE *node)
14606{
14607 switch (nd_type(node)) {
14608 case NODE_INTEGER:
14609 case NODE_FLOAT:
14610 case NODE_RATIONAL:
14611 case NODE_IMAGINARY:
14612 case NODE_STR:
14613 case NODE_SYM:
14614 case NODE_REGX:
14615 case NODE_LINE:
14616 case NODE_FILE:
14617 case NODE_ENCODING:
14618 return true;
14619 default:
14620 return false;
14621 }
14622}
14623
14624static VALUE
14625nd_value(struct parser_params *p, NODE *node)
14626{
14627 switch (nd_type(node)) {
14628 case NODE_STR:
14629 return rb_node_str_string_val(node);
14630 case NODE_INTEGER:
14631 return rb_node_integer_literal_val(node);
14632 case NODE_FLOAT:
14633 return rb_node_float_literal_val(node);
14634 case NODE_RATIONAL:
14635 return rb_node_rational_literal_val(node);
14636 case NODE_IMAGINARY:
14637 return rb_node_imaginary_literal_val(node);
14638 case NODE_SYM:
14639 return rb_node_sym_string_val(node);
14640 case NODE_REGX:
14641 return rb_node_regx_string_val(node);
14642 case NODE_LINE:
14643 return rb_node_line_lineno_val(node);
14644 case NODE_ENCODING:
14645 return rb_node_encoding_val(node);
14646 case NODE_FILE:
14647 return rb_node_file_path_val(node);
14648 default:
14649 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14650 UNREACHABLE_RETURN(0);
14651 }
14652}
14653
14654static void
14655warn_duplicate_keys(struct parser_params *p, NODE *hash)
14656{
14657 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14658 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14659 while (hash && RNODE_LIST(hash)->nd_next) {
14660 NODE *head = RNODE_LIST(hash)->nd_head;
14661 NODE *value = RNODE_LIST(hash)->nd_next;
14662 NODE *next = RNODE_LIST(value)->nd_next;
14663 st_data_t key;
14664 st_data_t data;
14665
14666 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14667 if (!head) {
14668 head = value;
14669 }
14670
14671 if (nd_type_st_key_enable_p(head)) {
14672 key = (st_data_t)head;
14673
14674 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14675 rb_warn2L(nd_line((NODE *)data),
14676 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14677 nd_value(p, head), WARN_I(nd_line(head)));
14678 }
14679 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14680 }
14681 hash = next;
14682 }
14683 st_free_table(p->warn_duplicate_keys_table);
14684 p->warn_duplicate_keys_table = NULL;
14685}
14686
14687static NODE *
14688new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14689{
14690 if (hash) warn_duplicate_keys(p, hash);
14691 return NEW_HASH(hash, loc);
14692}
14693
14694static void
14695error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14696{
14697 if (is_private_local_id(p, id)) {
14698 return;
14699 }
14700 if (st_is_member(p->pvtbl, id)) {
14701 yyerror1(loc, "duplicated variable name");
14702 }
14703 else {
14704 st_insert(p->pvtbl, (st_data_t)id, 0);
14705 }
14706}
14707
14708static void
14709error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14710{
14711 if (!p->pktbl) {
14712 p->pktbl = st_init_numtable();
14713 }
14714 else if (st_is_member(p->pktbl, key)) {
14715 yyerror1(loc, "duplicated key name");
14716 return;
14717 }
14718 st_insert(p->pktbl, (st_data_t)key, 0);
14719}
14720
14721static NODE *
14722new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14723{
14724 return NEW_HASH(hash, loc);
14725}
14726
14727static NODE *
14728new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14729{
14730 NODE *asgn;
14731
14732 if (lhs) {
14733 ID vid = get_nd_vid(p, lhs);
14734 YYLTYPE lhs_loc = lhs->nd_loc;
14735 if (op == tOROP) {
14736 set_nd_value(p, lhs, rhs);
14737 nd_set_loc(lhs, loc);
14738 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14739 }
14740 else if (op == tANDOP) {
14741 set_nd_value(p, lhs, rhs);
14742 nd_set_loc(lhs, loc);
14743 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14744 }
14745 else {
14746 asgn = lhs;
14747 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14748 set_nd_value(p, asgn, rhs);
14749 nd_set_loc(asgn, loc);
14750 }
14751 }
14752 else {
14753 asgn = NEW_ERROR(loc);
14754 }
14755 return asgn;
14756}
14757
14758static NODE *
14759new_ary_op_assign(struct parser_params *p, NODE *ary,
14760 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14761 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14762{
14763 NODE *asgn;
14764
14765 aryset_check(p, args);
14766 args = make_list(args, args_loc);
14767 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14768 fixpos(asgn, ary);
14769 return asgn;
14770}
14771
14772static NODE *
14773new_attr_op_assign(struct parser_params *p, NODE *lhs,
14774 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14775 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14776{
14777 NODE *asgn;
14778
14779 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14780 fixpos(asgn, lhs);
14781 return asgn;
14782}
14783
14784static NODE *
14785new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14786{
14787 NODE *asgn;
14788
14789 if (lhs) {
14790 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14791 }
14792 else {
14793 asgn = NEW_ERROR(loc);
14794 }
14795 fixpos(asgn, lhs);
14796 return asgn;
14797}
14798
14799static NODE *
14800const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14801{
14802 if (p->ctxt.in_def) {
14803#ifndef RIPPER
14804 yyerror1(loc, "dynamic constant assignment");
14805#else
14806 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14807#endif
14808 }
14809 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14810}
14811
14812#ifdef RIPPER
14813static VALUE
14814assign_error(struct parser_params *p, const char *mesg, VALUE a)
14815{
14816 a = dispatch2(assign_error, ERR_MESG(), a);
14817 ripper_error(p);
14818 return a;
14819}
14820#endif
14821
14822static NODE *
14823new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14824{
14825 NODE *result = head;
14826 if (rescue) {
14827 NODE *tmp = rescue_else ? rescue_else : rescue;
14828 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14829
14830 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14831 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14832 }
14833 if (ensure) {
14834 result = NEW_ENSURE(result, ensure, loc);
14835 }
14836 fixpos(result, head);
14837 return result;
14838}
14839
14840static void
14841warn_unused_var(struct parser_params *p, struct local_vars *local)
14842{
14843 int cnt;
14844
14845 if (!local->used) return;
14846 cnt = local->used->pos;
14847 if (cnt != local->vars->pos) {
14848 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14849 }
14850#ifndef RIPPER
14851 ID *v = local->vars->tbl;
14852 ID *u = local->used->tbl;
14853 for (int i = 0; i < cnt; ++i) {
14854 if (!v[i] || (u[i] & LVAR_USED)) continue;
14855 if (is_private_local_id(p, v[i])) continue;
14856 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14857 }
14858#endif
14859}
14860
14861static void
14862local_push(struct parser_params *p, int toplevel_scope)
14863{
14864 struct local_vars *local;
14865 int inherits_dvars = toplevel_scope && compile_for_eval;
14866 int warn_unused_vars = RTEST(ruby_verbose);
14867
14868 local = ALLOC(struct local_vars);
14869 local->prev = p->lvtbl;
14870 local->args = vtable_alloc(0);
14871 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14872#ifndef RIPPER
14873 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14874 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14875#endif
14876 local->numparam.outer = 0;
14877 local->numparam.inner = 0;
14878 local->numparam.current = 0;
14879 local->it = 0;
14880 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14881
14882# if WARN_PAST_SCOPE
14883 local->past = 0;
14884# endif
14885 CMDARG_PUSH(0);
14886 COND_PUSH(0);
14887 p->lvtbl = local;
14888}
14889
14890static void
14891vtable_chain_free(struct parser_params *p, struct vtable *table)
14892{
14893 while (!DVARS_TERMINAL_P(table)) {
14894 struct vtable *cur_table = table;
14895 table = cur_table->prev;
14896 vtable_free(cur_table);
14897 }
14898}
14899
14900static void
14901local_free(struct parser_params *p, struct local_vars *local)
14902{
14903 vtable_chain_free(p, local->used);
14904
14905# if WARN_PAST_SCOPE
14906 vtable_chain_free(p, local->past);
14907# endif
14908
14909 vtable_chain_free(p, local->args);
14910 vtable_chain_free(p, local->vars);
14911
14912 ruby_sized_xfree(local, sizeof(struct local_vars));
14913}
14914
14915static void
14916local_pop(struct parser_params *p)
14917{
14918 struct local_vars *local = p->lvtbl->prev;
14919 if (p->lvtbl->used) {
14920 warn_unused_var(p, p->lvtbl);
14921 }
14922
14923 local_free(p, p->lvtbl);
14924 p->lvtbl = local;
14925
14926 CMDARG_POP();
14927 COND_POP();
14928}
14929
14930static rb_ast_id_table_t *
14931local_tbl(struct parser_params *p)
14932{
14933 int cnt_args = vtable_size(p->lvtbl->args);
14934 int cnt_vars = vtable_size(p->lvtbl->vars);
14935 int cnt = cnt_args + cnt_vars;
14936 int i, j;
14937 rb_ast_id_table_t *tbl;
14938
14939 if (cnt <= 0) return 0;
14940 tbl = rb_ast_new_local_table(p->ast, cnt);
14941 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14942 /* remove IDs duplicated to warn shadowing */
14943 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14944 ID id = p->lvtbl->vars->tbl[i];
14945 if (!vtable_included(p->lvtbl->args, id)) {
14946 tbl->ids[j++] = id;
14947 }
14948 }
14949 if (j < cnt) {
14950 tbl = rb_ast_resize_latest_local_table(p->ast, j);
14951 }
14952
14953 return tbl;
14954}
14955
14956static void
14957numparam_name(struct parser_params *p, ID id)
14958{
14959 if (!NUMPARAM_ID_P(id)) return;
14960 compile_error(p, "_%d is reserved for numbered parameter",
14961 NUMPARAM_ID_TO_IDX(id));
14962}
14963
14964static void
14965arg_var(struct parser_params *p, ID id)
14966{
14967 numparam_name(p, id);
14968 vtable_add(p->lvtbl->args, id);
14969}
14970
14971static void
14972local_var(struct parser_params *p, ID id)
14973{
14974 numparam_name(p, id);
14975 vtable_add(p->lvtbl->vars, id);
14976 if (p->lvtbl->used) {
14977 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
14978 }
14979}
14980
14981#ifndef RIPPER
14982int
14983rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
14984{
14985 return rb_local_defined(id, iseq);
14986}
14987#endif
14988
14989static int
14990local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
14991{
14992 struct vtable *vars, *args, *used;
14993
14994 vars = p->lvtbl->vars;
14995 args = p->lvtbl->args;
14996 used = p->lvtbl->used;
14997
14998 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
14999 vars = vars->prev;
15000 args = args->prev;
15001 if (used) used = used->prev;
15002 }
15003
15004 if (vars && vars->prev == DVARS_INHERIT) {
15005 return rb_parser_local_defined(p, id, p->parent_iseq);
15006 }
15007 else if (vtable_included(args, id)) {
15008 return 1;
15009 }
15010 else {
15011 int i = vtable_included(vars, id);
15012 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15013 return i != 0;
15014 }
15015}
15016
15017static int
15018local_id(struct parser_params *p, ID id)
15019{
15020 return local_id_ref(p, id, NULL);
15021}
15022
15023static int
15024check_forwarding_args(struct parser_params *p)
15025{
15026 if (local_id(p, idFWD_ALL)) return TRUE;
15027 compile_error(p, "unexpected ...");
15028 return FALSE;
15029}
15030
15031static void
15032add_forwarding_args(struct parser_params *p)
15033{
15034 arg_var(p, idFWD_REST);
15035#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15036 arg_var(p, idFWD_KWREST);
15037#endif
15038 arg_var(p, idFWD_BLOCK);
15039 arg_var(p, idFWD_ALL);
15040}
15041
15042static void
15043forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15044{
15045 bool conflict = false;
15046
15047 struct vtable *vars, *args;
15048
15049 vars = p->lvtbl->vars;
15050 args = p->lvtbl->args;
15051
15052 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15053 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15054 vars = vars->prev;
15055 args = args->prev;
15056 }
15057
15058 bool found = false;
15059 if (vars && vars->prev == DVARS_INHERIT && !found) {
15060 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15061 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15062 }
15063 else {
15064 found = (vtable_included(args, arg) &&
15065 !(all && vtable_included(args, all)));
15066 }
15067
15068 if (!found) {
15069 compile_error(p, "no anonymous %s parameter", var);
15070 }
15071 else if (conflict) {
15072 compile_error(p, "anonymous %s parameter is also used within block", var);
15073 }
15074}
15075
15076static NODE *
15077new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15078{
15079 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15080#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15081 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15082#endif
15083 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15084 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15085 block->forwarding = TRUE;
15086#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15087 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15088#endif
15089 return arg_blk_pass(args, block);
15090}
15091
15092static NODE *
15093numparam_push(struct parser_params *p)
15094{
15095 struct local_vars *local = p->lvtbl;
15096 NODE *inner = local->numparam.inner;
15097 if (!local->numparam.outer) {
15098 local->numparam.outer = local->numparam.current;
15099 }
15100 local->numparam.inner = 0;
15101 local->numparam.current = 0;
15102 local->it = 0;
15103 return inner;
15104}
15105
15106static void
15107numparam_pop(struct parser_params *p, NODE *prev_inner)
15108{
15109 struct local_vars *local = p->lvtbl;
15110 if (prev_inner) {
15111 /* prefer first one */
15112 local->numparam.inner = prev_inner;
15113 }
15114 else if (local->numparam.current) {
15115 /* current and inner are exclusive */
15116 local->numparam.inner = local->numparam.current;
15117 }
15118 if (p->max_numparam > NO_PARAM) {
15119 /* current and outer are exclusive */
15120 local->numparam.current = local->numparam.outer;
15121 local->numparam.outer = 0;
15122 }
15123 else {
15124 /* no numbered parameter */
15125 local->numparam.current = 0;
15126 }
15127 local->it = 0;
15128}
15129
15130static const struct vtable *
15131dyna_push(struct parser_params *p)
15132{
15133 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15134 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15135 if (p->lvtbl->used) {
15136 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15137 }
15138 return p->lvtbl->args;
15139}
15140
15141static void
15142dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15143{
15144 struct vtable *tmp = *vtblp;
15145 *vtblp = tmp->prev;
15146# if WARN_PAST_SCOPE
15147 if (p->past_scope_enabled) {
15148 tmp->prev = p->lvtbl->past;
15149 p->lvtbl->past = tmp;
15150 return;
15151 }
15152# endif
15153 vtable_free(tmp);
15154}
15155
15156static void
15157dyna_pop_1(struct parser_params *p)
15158{
15159 struct vtable *tmp;
15160
15161 if ((tmp = p->lvtbl->used) != 0) {
15162 warn_unused_var(p, p->lvtbl);
15163 p->lvtbl->used = p->lvtbl->used->prev;
15164 vtable_free(tmp);
15165 }
15166 dyna_pop_vtable(p, &p->lvtbl->args);
15167 dyna_pop_vtable(p, &p->lvtbl->vars);
15168}
15169
15170static void
15171dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15172{
15173 while (p->lvtbl->args != lvargs) {
15174 dyna_pop_1(p);
15175 if (!p->lvtbl->args) {
15176 struct local_vars *local = p->lvtbl->prev;
15177 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15178 p->lvtbl = local;
15179 }
15180 }
15181 dyna_pop_1(p);
15182}
15183
15184static int
15185dyna_in_block(struct parser_params *p)
15186{
15187 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15188}
15189
15190#ifndef RIPPER
15191int
15192dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15193{
15194 struct vtable *vars, *args, *used;
15195 int i;
15196
15197 args = p->lvtbl->args;
15198 vars = p->lvtbl->vars;
15199 used = p->lvtbl->used;
15200
15201 while (!DVARS_TERMINAL_P(vars)) {
15202 if (vtable_included(args, id)) {
15203 return 1;
15204 }
15205 if ((i = vtable_included(vars, id)) != 0) {
15206 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15207 return 1;
15208 }
15209 args = args->prev;
15210 vars = vars->prev;
15211 if (!vidrefp) used = 0;
15212 if (used) used = used->prev;
15213 }
15214
15215 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15216 return rb_dvar_defined(id, p->parent_iseq);
15217 }
15218
15219 return 0;
15220}
15221#endif
15222
15223static int
15224dvar_defined(struct parser_params *p, ID id)
15225{
15226 return dvar_defined_ref(p, id, NULL);
15227}
15228
15229static int
15230dvar_curr(struct parser_params *p, ID id)
15231{
15232 return (vtable_included(p->lvtbl->args, id) ||
15233 vtable_included(p->lvtbl->vars, id));
15234}
15235
15236static void
15237reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15238{
15239 compile_error(p,
15240 "regexp encoding option '%c' differs from source encoding '%s'",
15241 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15242}
15243
15244#ifndef RIPPER
15245static rb_encoding *
15246find_enc(struct parser_params* p, const char *name)
15247{
15248 int idx = rb_enc_find_index(name);
15249 if (idx < 0) {
15250 rb_bug("unknown encoding name: %s", name);
15251 }
15252
15253 return rb_enc_from_index(idx);
15254}
15255
15256static rb_encoding *
15257kcode_to_enc(struct parser_params* p, int kcode)
15258{
15259 rb_encoding *enc;
15260
15261 switch (kcode) {
15262 case ENC_ASCII8BIT:
15263 enc = rb_ascii8bit_encoding();
15264 break;
15265 case ENC_EUC_JP:
15266 enc = find_enc(p, "EUC-JP");
15267 break;
15268 case ENC_Windows_31J:
15269 enc = find_enc(p, "Windows-31J");
15270 break;
15271 case ENC_UTF8:
15272 enc = rb_utf8_encoding();
15273 break;
15274 default:
15275 enc = NULL;
15276 break;
15277 }
15278
15279 return enc;
15280}
15281
15282int
15283rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15284{
15285 int c = RE_OPTION_ENCODING_IDX(options);
15286
15287 if (c) {
15288 int opt, idx;
15289 rb_encoding *enc;
15290
15291 char_to_option_kcode(c, &opt, &idx);
15292 enc = kcode_to_enc(p, idx);
15293 if (enc != rb_parser_str_get_encoding(str) &&
15294 !rb_parser_is_ascii_string(p, str)) {
15295 goto error;
15296 }
15297 rb_parser_string_set_encoding(str, enc);
15298 }
15299 else if (RE_OPTION_ENCODING_NONE(options)) {
15300 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15301 !rb_parser_is_ascii_string(p, str)) {
15302 c = 'n';
15303 goto error;
15304 }
15305 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15306 }
15307 else if (rb_is_usascii_enc(p->enc)) {
15308 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15309 }
15310 return 0;
15311
15312 error:
15313 return c;
15314}
15315#endif
15316
15317static void
15318reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15319{
15320 int c = rb_reg_fragment_setenc(p, str, options);
15321 if (c) reg_fragment_enc_error(p, str, c);
15322}
15323
15324#ifndef UNIVERSAL_PARSER
15325typedef struct {
15326 struct parser_params* parser;
15327 rb_encoding *enc;
15328 NODE *succ_block;
15329 const YYLTYPE *loc;
15330 rb_parser_assignable_func assignable;
15331} reg_named_capture_assign_t;
15332
15333static int
15334reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15335 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15336{
15337 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15338 struct parser_params* p = arg->parser;
15339 rb_encoding *enc = arg->enc;
15340 long len = name_end - name;
15341 const char *s = (const char *)name;
15342
15343 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15344}
15345
15346static NODE *
15347reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15348{
15349 reg_named_capture_assign_t arg;
15350
15351 arg.parser = p;
15352 arg.enc = rb_enc_get(regexp);
15353 arg.succ_block = 0;
15354 arg.loc = loc;
15355 arg.assignable = assignable;
15356 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15357
15358 if (!arg.succ_block) return 0;
15359 return RNODE_BLOCK(arg.succ_block)->nd_next;
15360}
15361#endif
15362
15363#ifndef RIPPER
15364NODE *
15365rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15366{
15367 return assignable(p, id, val, loc);
15368}
15369
15370int
15371rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15372 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15373{
15374 ID var;
15375 NODE *node, *succ;
15376
15377 if (!len) return ST_CONTINUE;
15378 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15379 return ST_CONTINUE;
15380
15381 var = intern_cstr(s, len, enc);
15382 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15383 if (!lvar_defined(p, var)) return ST_CONTINUE;
15384 }
15385 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15386 succ = *succ_block;
15387 if (!succ) succ = NEW_ERROR(loc);
15388 succ = block_append(p, succ, node);
15389 *succ_block = succ;
15390 return ST_CONTINUE;
15391}
15392#endif
15393
15394static VALUE
15395parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15396{
15397 VALUE str2;
15398 reg_fragment_setenc(p, str, options);
15399 str2 = rb_str_new_parser_string(str);
15400 return rb_parser_reg_compile(p, str2, options);
15401}
15402
15403#ifndef RIPPER
15404VALUE
15405rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15406{
15407 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15408}
15409#endif
15410
15411static VALUE
15412reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15413{
15414 VALUE re;
15415 VALUE err;
15416
15417 err = rb_errinfo();
15418 re = parser_reg_compile(p, str, options);
15419 if (NIL_P(re)) {
15420 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15421 rb_set_errinfo(err);
15422 compile_error(p, "%"PRIsVALUE, m);
15423 return Qnil;
15424 }
15425 return re;
15426}
15427
15428#ifndef RIPPER
15429void
15430rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15431{
15432 p->do_print = print;
15433 p->do_loop = loop;
15434 p->do_chomp = chomp;
15435 p->do_split = split;
15436}
15437
15438static NODE *
15439parser_append_options(struct parser_params *p, NODE *node)
15440{
15441 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15442 const YYLTYPE *const LOC = &default_location;
15443
15444 if (p->do_print) {
15445 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15446 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15447 LOC);
15448 node = block_append(p, node, print);
15449 }
15450
15451 if (p->do_loop) {
15452 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15453
15454 if (p->do_split) {
15455 ID ifs = rb_intern("$;");
15456 ID fields = rb_intern("$F");
15457 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15458 NODE *split = NEW_GASGN(fields,
15459 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15460 rb_intern("split"), args, LOC),
15461 LOC);
15462 node = block_append(p, split, node);
15463 }
15464 if (p->do_chomp) {
15465 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15466 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15467 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15468 }
15469
15470 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15471 }
15472
15473 return node;
15474}
15475
15476void
15477rb_init_parse(void)
15478{
15479 /* just to suppress unused-function warnings */
15480 (void)nodetype;
15481 (void)nodeline;
15482}
15483
15484ID
15485internal_id(struct parser_params *p)
15486{
15487 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15488}
15489#endif /* !RIPPER */
15490
15491static void
15492parser_initialize(struct parser_params *p)
15493{
15494 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15495 p->command_start = TRUE;
15496 p->ruby_sourcefile_string = Qnil;
15497 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15498 string_buffer_init(p);
15499 p->node_id = 0;
15500 p->delayed.token = NULL;
15501 p->frozen_string_literal = -1; /* not specified */
15502#ifndef RIPPER
15503 p->error_buffer = Qfalse;
15504 p->end_expect_token_locations = NULL;
15505 p->token_id = 0;
15506 p->tokens = NULL;
15507#else
15508 p->result = Qnil;
15509 p->parsing_thread = Qnil;
15510 p->s_value = Qnil;
15511 p->s_lvalue = Qnil;
15512 p->s_value_stack = rb_ary_new();
15513#endif
15514 p->debug_buffer = Qnil;
15515 p->debug_output = rb_ractor_stdout();
15516 p->enc = rb_utf8_encoding();
15517 p->exits = 0;
15518}
15519
15520#ifdef RIPPER
15521#define rb_ruby_parser_mark ripper_parser_mark
15522#define rb_ruby_parser_free ripper_parser_free
15523#define rb_ruby_parser_memsize ripper_parser_memsize
15524#endif
15525
15526void
15527rb_ruby_parser_mark(void *ptr)
15528{
15529 struct parser_params *p = (struct parser_params*)ptr;
15530
15531 rb_gc_mark(p->ruby_sourcefile_string);
15532#ifndef RIPPER
15533 rb_gc_mark(p->error_buffer);
15534#else
15535 rb_gc_mark(p->value);
15536 rb_gc_mark(p->result);
15537 rb_gc_mark(p->parsing_thread);
15538 rb_gc_mark(p->s_value);
15539 rb_gc_mark(p->s_lvalue);
15540 rb_gc_mark(p->s_value_stack);
15541#endif
15542 rb_gc_mark(p->debug_buffer);
15543 rb_gc_mark(p->debug_output);
15544}
15545
15546void
15547rb_ruby_parser_free(void *ptr)
15548{
15549 struct parser_params *p = (struct parser_params*)ptr;
15550 struct local_vars *local, *prev;
15551
15552 if (p->ast) {
15553 rb_ast_free(p->ast);
15554 }
15555
15556 if (p->warn_duplicate_keys_table) {
15557 st_free_table(p->warn_duplicate_keys_table);
15558 }
15559
15560#ifndef RIPPER
15561 if (p->tokens) {
15562 rb_parser_ary_free(p, p->tokens);
15563 }
15564#endif
15565
15566 if (p->tokenbuf) {
15567 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15568 }
15569
15570 for (local = p->lvtbl; local; local = prev) {
15571 prev = local->prev;
15572 local_free(p, local);
15573 }
15574
15575 {
15576 token_info *ptinfo;
15577 while ((ptinfo = p->token_info) != 0) {
15578 p->token_info = ptinfo->next;
15579 xfree(ptinfo);
15580 }
15581 }
15582 string_buffer_free(p);
15583
15584 if (p->pvtbl) {
15585 st_free_table(p->pvtbl);
15586 }
15587
15588 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15589 st_free_table(p->case_labels);
15590 }
15591
15592 xfree(p->lex.strterm);
15593 p->lex.strterm = 0;
15594
15595 xfree(ptr);
15596}
15597
15598size_t
15599rb_ruby_parser_memsize(const void *ptr)
15600{
15601 struct parser_params *p = (struct parser_params*)ptr;
15602 struct local_vars *local;
15603 size_t size = sizeof(*p);
15604
15605 size += p->toksiz;
15606 for (local = p->lvtbl; local; local = local->prev) {
15607 size += sizeof(*local);
15608 if (local->vars) size += local->vars->capa * sizeof(ID);
15609 }
15610 return size;
15611}
15612
15613#ifndef RIPPER
15614#undef rb_reserved_word
15615
15616const struct kwtable *
15617rb_reserved_word(const char *str, unsigned int len)
15618{
15619 return reserved_word(str, len);
15620}
15621
15622#ifdef UNIVERSAL_PARSER
15623rb_parser_t *
15624rb_ruby_parser_allocate(const rb_parser_config_t *config)
15625{
15626 /* parser_initialize expects fields to be set to 0 */
15627 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15628 p->config = config;
15629 return p;
15630}
15631
15632rb_parser_t *
15633rb_ruby_parser_new(const rb_parser_config_t *config)
15634{
15635 /* parser_initialize expects fields to be set to 0 */
15636 rb_parser_t *p = rb_ruby_parser_allocate(config);
15637 parser_initialize(p);
15638 return p;
15639}
15640#else
15641rb_parser_t *
15642rb_ruby_parser_allocate(void)
15643{
15644 /* parser_initialize expects fields to be set to 0 */
15645 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15646 return p;
15647}
15648
15649rb_parser_t *
15650rb_ruby_parser_new(void)
15651{
15652 /* parser_initialize expects fields to be set to 0 */
15653 rb_parser_t *p = rb_ruby_parser_allocate();
15654 parser_initialize(p);
15655 return p;
15656}
15657#endif
15658
15659rb_parser_t *
15660rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15661{
15662 p->error_buffer = main ? Qfalse : Qnil;
15663 p->parent_iseq = base;
15664 return p;
15665}
15666
15667void
15668rb_ruby_parser_set_script_lines(rb_parser_t *p)
15669{
15670 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15671}
15672
15673void
15674rb_ruby_parser_error_tolerant(rb_parser_t *p)
15675{
15676 p->error_tolerant = 1;
15677}
15678
15679void
15680rb_ruby_parser_keep_tokens(rb_parser_t *p)
15681{
15682 p->keep_tokens = 1;
15683 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15684}
15685
15686rb_encoding *
15687rb_ruby_parser_encoding(rb_parser_t *p)
15688{
15689 return p->enc;
15690}
15691
15692int
15693rb_ruby_parser_end_seen_p(rb_parser_t *p)
15694{
15695 return p->ruby__end__seen;
15696}
15697
15698int
15699rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15700{
15701 p->debug = flag;
15702 return flag;
15703}
15704#endif /* !RIPPER */
15705
15706#ifdef RIPPER
15707int
15708rb_ruby_parser_get_yydebug(rb_parser_t *p)
15709{
15710 return p->debug;
15711}
15712
15713void
15714rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15715{
15716 p->value = value;
15717}
15718
15719int
15720rb_ruby_parser_error_p(rb_parser_t *p)
15721{
15722 return p->error_p;
15723}
15724
15725VALUE
15726rb_ruby_parser_debug_output(rb_parser_t *p)
15727{
15728 return p->debug_output;
15729}
15730
15731void
15732rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15733{
15734 p->debug_output = output;
15735}
15736
15737VALUE
15738rb_ruby_parser_parsing_thread(rb_parser_t *p)
15739{
15740 return p->parsing_thread;
15741}
15742
15743void
15744rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15745{
15746 p->parsing_thread = parsing_thread;
15747}
15748
15749void
15750rb_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)
15751{
15752 p->lex.gets = gets;
15753 p->lex.input = input;
15754 p->eofp = 0;
15755 p->ruby_sourcefile_string = sourcefile_string;
15756 p->ruby_sourcefile = sourcefile;
15757 p->ruby_sourceline = sourceline;
15758}
15759
15760VALUE
15761rb_ruby_parser_result(rb_parser_t *p)
15762{
15763 return p->result;
15764}
15765
15766rb_encoding *
15767rb_ruby_parser_enc(rb_parser_t *p)
15768{
15769 return p->enc;
15770}
15771
15772VALUE
15773rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15774{
15775 return p->ruby_sourcefile_string;
15776}
15777
15778int
15779rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15780{
15781 return p->ruby_sourceline;
15782}
15783
15784int
15785rb_ruby_parser_lex_state(rb_parser_t *p)
15786{
15787 return p->lex.state;
15788}
15789
15790void
15791rb_ruby_ripper_parse0(rb_parser_t *p)
15792{
15793 parser_prepare(p);
15794 p->ast = rb_ast_new();
15795 ripper_yyparse((void*)p);
15796 rb_ast_free(p->ast);
15797 p->ast = 0;
15798 p->eval_tree = 0;
15799 p->eval_tree_begin = 0;
15800}
15801
15802int
15803rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15804{
15805 return dedent_string(p, string, width);
15806}
15807
15808int
15809rb_ruby_ripper_initialized_p(rb_parser_t *p)
15810{
15811 return p->lex.input != 0;
15812}
15813
15814void
15815rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15816{
15817 parser_initialize(p);
15818}
15819
15820long
15821rb_ruby_ripper_column(rb_parser_t *p)
15822{
15823 return p->lex.ptok - p->lex.pbeg;
15824}
15825
15826long
15827rb_ruby_ripper_token_len(rb_parser_t *p)
15828{
15829 return p->lex.pcur - p->lex.ptok;
15830}
15831
15832rb_parser_string_t *
15833rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15834{
15835 return p->lex.lastline;
15836}
15837
15838VALUE
15839rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15840{
15841 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15842}
15843
15844#ifdef UNIVERSAL_PARSER
15845rb_parser_t *
15846rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15847{
15848 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15849 p->config = config;
15850 return p;
15851}
15852#endif
15853
15854struct parser_params*
15855rb_ruby_ripper_parser_allocate(void)
15856{
15857 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15858}
15859#endif /* RIPPER */
15860
15861#ifndef RIPPER
15862void
15863rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15864{
15865 va_list ap;
15866 VALUE mesg = p->debug_buffer;
15867
15868 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15869 va_start(ap, fmt);
15870 rb_str_vcatf(mesg, fmt, ap);
15871 va_end(ap);
15872 if (char_at_end(p, mesg, 0) == '\n') {
15873 rb_io_write(p->debug_output, mesg);
15874 p->debug_buffer = Qnil;
15875 }
15876}
15877
15878static void
15879parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15880{
15881 va_list ap;
15882 int lineno, column;
15883
15884 if (loc) {
15885 lineno = loc->end_pos.lineno;
15886 column = loc->end_pos.column;
15887 }
15888 else {
15889 lineno = p->ruby_sourceline;
15890 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15891 }
15892
15893 rb_io_flush(p->debug_output);
15894 p->error_p = 1;
15895 va_start(ap, fmt);
15896 p->error_buffer =
15897 rb_syntax_error_append(p->error_buffer,
15898 p->ruby_sourcefile_string,
15899 lineno, column,
15900 p->enc, fmt, ap);
15901 va_end(ap);
15902}
15903
15904static size_t
15905count_char(const char *str, int c)
15906{
15907 int n = 0;
15908 while (str[n] == c) ++n;
15909 return n;
15910}
15911
15912/*
15913 * strip enclosing double-quotes, same as the default yytnamerr except
15914 * for that single-quotes matching back-quotes do not stop stripping.
15915 *
15916 * "\"`class' keyword\"" => "`class' keyword"
15917 */
15918size_t
15919rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
15920{
15921 if (*yystr == '"') {
15922 size_t yyn = 0, bquote = 0;
15923 const char *yyp = yystr;
15924
15925 while (*++yyp) {
15926 switch (*yyp) {
15927 case '\'':
15928 if (!bquote) {
15929 bquote = count_char(yyp+1, '\'') + 1;
15930 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
15931 yyn += bquote;
15932 yyp += bquote - 1;
15933 break;
15934 }
15935 else {
15936 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
15937 if (yyres) memcpy(yyres + yyn, yyp, bquote);
15938 yyn += bquote;
15939 yyp += bquote - 1;
15940 bquote = 0;
15941 break;
15942 }
15943 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
15944 if (yyres) memcpy(yyres + yyn, yyp, 3);
15945 yyn += 3;
15946 yyp += 2;
15947 break;
15948 }
15949 goto do_not_strip_quotes;
15950 }
15951
15952 case ',':
15953 goto do_not_strip_quotes;
15954
15955 case '\\':
15956 if (*++yyp != '\\')
15957 goto do_not_strip_quotes;
15958 /* Fall through. */
15959 default:
15960 if (yyres)
15961 yyres[yyn] = *yyp;
15962 yyn++;
15963 break;
15964
15965 case '"':
15966 case '\0':
15967 if (yyres)
15968 yyres[yyn] = '\0';
15969 return yyn;
15970 }
15971 }
15972 do_not_strip_quotes: ;
15973 }
15974
15975 if (!yyres) return strlen(yystr);
15976
15977 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
15978}
15979#endif
15980
15981#ifdef RIPPER
15982#define validate(x) (void)(x)
15983
15984static VALUE
15985ripper_dispatch0(struct parser_params *p, ID mid)
15986{
15987 return rb_funcall(p->value, mid, 0);
15988}
15989
15990static VALUE
15991ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
15992{
15993 validate(a);
15994 return rb_funcall(p->value, mid, 1, a);
15995}
15996
15997static VALUE
15998ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
15999{
16000 validate(a);
16001 validate(b);
16002 return rb_funcall(p->value, mid, 2, a, b);
16003}
16004
16005static VALUE
16006ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16007{
16008 validate(a);
16009 validate(b);
16010 validate(c);
16011 return rb_funcall(p->value, mid, 3, a, b, c);
16012}
16013
16014static VALUE
16015ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16016{
16017 validate(a);
16018 validate(b);
16019 validate(c);
16020 validate(d);
16021 return rb_funcall(p->value, mid, 4, a, b, c, d);
16022}
16023
16024static VALUE
16025ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16026{
16027 validate(a);
16028 validate(b);
16029 validate(c);
16030 validate(d);
16031 validate(e);
16032 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16033}
16034
16035static VALUE
16036ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16037{
16038 validate(a);
16039 validate(b);
16040 validate(c);
16041 validate(d);
16042 validate(e);
16043 validate(f);
16044 validate(g);
16045 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16046}
16047
16048void
16049ripper_error(struct parser_params *p)
16050{
16051 p->error_p = TRUE;
16052}
16053
16054VALUE
16055ripper_value(struct parser_params *p)
16056{
16057 (void)yystpcpy; /* may not used in newer bison */
16058
16059 return p->value;
16060}
16061
16062#endif /* RIPPER */
16063/*
16064 * Local variables:
16065 * mode: c
16066 * c-file-style: "ruby"
16067 * End:
16068 */