Ruby 3.4.3p32 (2025-04-14 revision d0b7e5b6a04bde21ca483d20a1546b28b401c2d4)
parse.y
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 parse_isalpha(c) || parse_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 parse_isdigit(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
287}
288
289#undef ISXDIGIT
290#define ISXDIGIT(c) parse_isxdigit(c)
291
292#include "parser_st.h"
293
294#undef STRCASECMP
295#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
296
297#undef STRNCASECMP
298#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
299
300#ifdef RIPPER
301#include "ripper_init.h"
302#endif
303
304enum rescue_context {
305 before_rescue,
306 after_rescue,
307 after_else,
308 after_ensure,
309};
310
311struct lex_context {
312 unsigned int in_defined: 1;
313 unsigned int in_kwarg: 1;
314 unsigned int in_argdef: 1;
315 unsigned int in_def: 1;
316 unsigned int in_class: 1;
317 BITFIELD(enum rb_parser_shareability, shareable_constant_value, 2);
318 BITFIELD(enum rescue_context, in_rescue, 2);
319 unsigned int cant_return: 1;
320};
321
322typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
323
324#if defined(__GNUC__) && !defined(__clang__)
325// Suppress "parameter passing for argument of type 'struct
326// lex_context' changed" notes. `struct lex_context` is file scope,
327// and has no ABI compatibility issue.
328RBIMPL_WARNING_PUSH()
329RBIMPL_WARNING_IGNORED(-Wpsabi)
330RBIMPL_WARNING_POP()
331// Not sure why effective even after popped.
332#endif
333
334#include "parse.h"
335
336#define NO_LEX_CTXT (struct lex_context){0}
337
338#ifndef WARN_PAST_SCOPE
339# define WARN_PAST_SCOPE 0
340#endif
341
342#define TAB_WIDTH 8
343
344#define yydebug (p->debug) /* disable the global variable definition */
345
346#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__)
347#define YY_LOCATION_PRINT(File, loc, p) \
348 rb_parser_printf(p, "%d.%d-%d.%d", \
349 (loc).beg_pos.lineno, (loc).beg_pos.column,\
350 (loc).end_pos.lineno, (loc).end_pos.column)
351#define YYLLOC_DEFAULT(Current, Rhs, N) \
352 do \
353 if (N) \
354 { \
355 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
356 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
357 } \
358 else \
359 { \
360 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
361 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
362 } \
363 while (0)
364#define YY_(Msgid) \
365 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
366 "nesting too deep" : (Msgid))
367
368#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
369 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
370#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
371 rb_parser_set_location_of_delayed_token(p, &(Current))
372#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
373 rb_parser_set_location_of_heredoc_end(p, &(Current))
374#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
375 rb_parser_set_location_of_dummy_end(p, &(Current))
376#define RUBY_SET_YYLLOC_OF_NONE(Current) \
377 rb_parser_set_location_of_none(p, &(Current))
378#define RUBY_SET_YYLLOC(Current) \
379 rb_parser_set_location(p, &(Current))
380#define RUBY_INIT_YYLLOC() \
381 { \
382 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
383 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
384 }
385
386#define IS_lex_state_for(x, ls) ((x) & (ls))
387#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
388#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
389#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
390
391# define SET_LEX_STATE(ls) \
392 parser_set_lex_state(p, ls, __LINE__)
393static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
394
395typedef VALUE stack_type;
396
397static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
398
399# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
400# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
401# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
402# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
403# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
404
405/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
406 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
407#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
408#define COND_POP() BITSTACK_POP(cond_stack)
409#define COND_P() BITSTACK_SET_P(cond_stack)
410#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
411
412/* A flag to identify keyword_do_block; "do" keyword after command_call.
413 Example: `foo 1, 2 do`. */
414#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
415#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
416#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
417#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
418
419struct vtable {
420 ID *tbl;
421 int pos;
422 int capa;
423 struct vtable *prev;
424};
425
426struct local_vars {
427 struct vtable *args;
428 struct vtable *vars;
429 struct vtable *used;
430# if WARN_PAST_SCOPE
431 struct vtable *past;
432# endif
433 struct local_vars *prev;
434 struct {
435 NODE *outer, *inner, *current;
436 } numparam;
437 NODE *it;
438};
439
440enum {
441 ORDINAL_PARAM = -1,
442 NO_PARAM = 0,
443 NUMPARAM_MAX = 9,
444};
445
446#define DVARS_INHERIT ((void*)1)
447#define DVARS_TOPSCOPE NULL
448#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
449
450typedef struct token_info {
451 const char *token;
452 rb_code_position_t beg;
453 int indent;
454 int nonspc;
455 struct token_info *next;
456} token_info;
457
458typedef struct end_expect_token_locations {
459 const rb_code_position_t *pos;
460 struct end_expect_token_locations *prev;
461} end_expect_token_locations_t;
462
463typedef struct parser_string_buffer_elem {
464 struct parser_string_buffer_elem *next;
465 long len; /* Total length of allocated buf */
466 long used; /* Current usage of buf */
467 rb_parser_string_t *buf[FLEX_ARY_LEN];
468} parser_string_buffer_elem_t;
469
470typedef struct parser_string_buffer {
471 parser_string_buffer_elem_t *head;
472 parser_string_buffer_elem_t *last;
473} parser_string_buffer_t;
474
475#define AFTER_HEREDOC_WITHOUT_TERMINTOR ((rb_parser_string_t *)1)
476
477/*
478 Structure of Lexer Buffer:
479
480 lex.pbeg lex.ptok lex.pcur lex.pend
481 | | | |
482 |------------+------------+------------|
483 |<---------->|
484 token
485*/
486struct parser_params {
487 YYSTYPE *lval;
488 YYLTYPE *yylloc;
489
490 struct {
491 rb_strterm_t *strterm;
492 rb_parser_lex_gets_func *gets;
493 rb_parser_input_data input;
494 parser_string_buffer_t string_buffer;
495 rb_parser_string_t *lastline;
496 rb_parser_string_t *nextline;
497 const char *pbeg;
498 const char *pcur;
499 const char *pend;
500 const char *ptok;
501 enum lex_state_e state;
502 /* track the nest level of any parens "()[]{}" */
503 int paren_nest;
504 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
505 int lpar_beg;
506 /* track the nest level of only braces "{}" */
507 int brace_nest;
508 } lex;
509 stack_type cond_stack;
510 stack_type cmdarg_stack;
511 int tokidx;
512 int toksiz;
513 int heredoc_end;
514 int heredoc_indent;
515 int heredoc_line_indent;
516 char *tokenbuf;
517 struct local_vars *lvtbl;
518 st_table *pvtbl;
519 st_table *pktbl;
520 int line_count;
521 int ruby_sourceline; /* current line no. */
522 const char *ruby_sourcefile; /* current source file */
523 VALUE ruby_sourcefile_string;
524 rb_encoding *enc;
525 token_info *token_info;
526 st_table *case_labels;
527 rb_node_exits_t *exits;
528
529 VALUE debug_buffer;
530 VALUE debug_output;
531
532 struct {
533 rb_parser_string_t *token;
534 int beg_line;
535 int beg_col;
536 int end_line;
537 int end_col;
538 } delayed;
539
540 rb_ast_t *ast;
541 int node_id;
542
543 st_table *warn_duplicate_keys_table;
544
545 int max_numparam;
546 ID it_id;
547
548 struct lex_context ctxt;
549
550 NODE *eval_tree_begin;
551 NODE *eval_tree;
552 const struct rb_iseq_struct *parent_iseq;
553
554#ifdef UNIVERSAL_PARSER
555 const rb_parser_config_t *config;
556#endif
557 /* compile_option */
558 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
559
560 unsigned int command_start:1;
561 unsigned int eofp: 1;
562 unsigned int ruby__end__seen: 1;
563 unsigned int debug: 1;
564 unsigned int has_shebang: 1;
565 unsigned int token_seen: 1;
566 unsigned int token_info_enabled: 1;
567# if WARN_PAST_SCOPE
568 unsigned int past_scope_enabled: 1;
569# endif
570 unsigned int error_p: 1;
571 unsigned int cr_seen: 1;
572
573#ifndef RIPPER
574 /* Ruby core only */
575
576 unsigned int do_print: 1;
577 unsigned int do_loop: 1;
578 unsigned int do_chomp: 1;
579 unsigned int do_split: 1;
580 unsigned int error_tolerant: 1;
581 unsigned int keep_tokens: 1;
582
583 VALUE error_buffer;
584 rb_parser_ary_t *debug_lines;
585 /*
586 * Store specific keyword locations to generate dummy end token.
587 * Refer to the tail of list element.
588 */
589 end_expect_token_locations_t *end_expect_token_locations;
590 /* id for terms */
591 int token_id;
592 /* Array for term tokens */
593 rb_parser_ary_t *tokens;
594#else
595 /* Ripper only */
596
597 VALUE value;
598 VALUE result;
599 VALUE parsing_thread;
600 VALUE s_value; /* Token VALUE */
601 VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
602 VALUE s_value_stack;
603#endif
604};
605
606#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
607#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
608#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
609static int
610numparam_id_p(struct parser_params *p, ID id)
611{
612 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
613 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
614 return idx > 0 && idx <= NUMPARAM_MAX;
615}
616static void numparam_name(struct parser_params *p, ID id);
617
618#ifdef RIPPER
619static void
620after_shift(struct parser_params *p)
621{
622 if (p->debug) {
623 rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
624 }
625 rb_ary_push(p->s_value_stack, p->s_value);
626 p->s_value = Qnil;
627}
628
629static void
630before_reduce(int len, struct parser_params *p)
631{
632 // Initialize $$ with $1.
633 if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
634}
635
636static void
637after_reduce(int len, struct parser_params *p)
638{
639 for (int i = 0; i < len; i++) {
640 VALUE tos = rb_ary_pop(p->s_value_stack);
641 if (p->debug) {
642 rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos);
643 }
644 }
645 if (p->debug) {
646 rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
647 }
648 rb_ary_push(p->s_value_stack, p->s_lvalue);
649 p->s_lvalue = Qnil;
650}
651
652static void
653after_shift_error_token(struct parser_params *p)
654{
655 if (p->debug) {
656 rb_parser_printf(p, "after-shift-error-token:\n");
657 }
658 rb_ary_push(p->s_value_stack, Qnil);
659}
660
661static void
662after_pop_stack(int len, struct parser_params *p)
663{
664 for (int i = 0; i < len; i++) {
665 VALUE tos = rb_ary_pop(p->s_value_stack);
666 if (p->debug) {
667 rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
668 }
669 }
670}
671#else
672static void
673after_shift(struct parser_params *p)
674{
675}
676
677static void
678before_reduce(int len, struct parser_params *p)
679{
680}
681
682static void
683after_reduce(int len, struct parser_params *p)
684{
685}
686
687static void
688after_shift_error_token(struct parser_params *p)
689{
690}
691
692static void
693after_pop_stack(int len, struct parser_params *p)
694{
695}
696#endif
697
698#define intern_cstr(n,l,en) rb_intern3(n,l,en)
699
700#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
701
702#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
703#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
704#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
705#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
706#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
707#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
708
709#ifndef RIPPER
710static inline int
711char_at_end(struct parser_params *p, VALUE str, int when_empty)
712{
713 long len = RSTRING_LEN(str);
714 return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
715}
716#endif
717
718static void
719pop_pvtbl(struct parser_params *p, st_table *tbl)
720{
721 st_free_table(p->pvtbl);
722 p->pvtbl = tbl;
723}
724
725static void
726pop_pktbl(struct parser_params *p, st_table *tbl)
727{
728 if (p->pktbl) st_free_table(p->pktbl);
729 p->pktbl = tbl;
730}
731
732#define STRING_BUF_DEFAULT_LEN 16
733
734static void
735string_buffer_init(struct parser_params *p)
736{
737 parser_string_buffer_t *buf = &p->lex.string_buffer;
738 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN;
739
740 buf->head = buf->last = xmalloc(size);
741 buf->head->len = STRING_BUF_DEFAULT_LEN;
742 buf->head->used = 0;
743 buf->head->next = NULL;
744}
745
746static void
747string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
748{
749 parser_string_buffer_t *buf = &p->lex.string_buffer;
750
751 if (buf->head->used >= buf->head->len) {
752 parser_string_buffer_elem_t *elem;
753 long n = buf->head->len * 2;
754 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n;
755
756 elem = xmalloc(size);
757 elem->len = n;
758 elem->used = 0;
759 elem->next = NULL;
760 buf->last->next = elem;
761 buf->last = elem;
762 }
763 buf->last->buf[buf->last->used++] = str;
764}
765
766static void
767string_buffer_free(struct parser_params *p)
768{
769 parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
770
771 while (elem) {
772 parser_string_buffer_elem_t *next_elem = elem->next;
773
774 for (long i = 0; i < elem->used; i++) {
775 rb_parser_string_free(p, elem->buf[i]);
776 }
777
778 xfree(elem);
779 elem = next_elem;
780 }
781}
782
783#ifndef RIPPER
784static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
785
786static void
787debug_end_expect_token_locations(struct parser_params *p, const char *name)
788{
789 if(p->debug) {
790 VALUE mesg = rb_sprintf("%s: [", name);
791 int i = 0;
792 for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
793 if (i > 0)
794 rb_str_cat_cstr(mesg, ", ");
795 rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
796 i++;
797 }
798 rb_str_cat_cstr(mesg, "]\n");
799 flush_debug_buffer(p, p->debug_output, mesg);
800 }
801}
802
803static void
804push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
805{
806 if(!p->error_tolerant) return;
807
808 end_expect_token_locations_t *locations;
809 locations = ALLOC(end_expect_token_locations_t);
810 locations->pos = pos;
811 locations->prev = p->end_expect_token_locations;
812 p->end_expect_token_locations = locations;
813
814 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
815}
816
817static void
818pop_end_expect_token_locations(struct parser_params *p)
819{
820 if(!p->end_expect_token_locations) return;
821
822 end_expect_token_locations_t *locations = p->end_expect_token_locations->prev;
823 ruby_sized_xfree(p->end_expect_token_locations, sizeof(end_expect_token_locations_t));
824 p->end_expect_token_locations = locations;
825
826 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
827}
828
829static end_expect_token_locations_t *
830peek_end_expect_token_locations(struct parser_params *p)
831{
832 return p->end_expect_token_locations;
833}
834
835static const char *
836parser_token2char(struct parser_params *p, enum yytokentype tok)
837{
838 switch ((int) tok) {
839#define TOKEN2CHAR(tok) case tok: return (#tok);
840#define TOKEN2CHAR2(tok, name) case tok: return (name);
841 TOKEN2CHAR2(' ', "word_sep");
842 TOKEN2CHAR2('!', "!")
843 TOKEN2CHAR2('%', "%");
844 TOKEN2CHAR2('&', "&");
845 TOKEN2CHAR2('*', "*");
846 TOKEN2CHAR2('+', "+");
847 TOKEN2CHAR2('-', "-");
848 TOKEN2CHAR2('/', "/");
849 TOKEN2CHAR2('<', "<");
850 TOKEN2CHAR2('=', "=");
851 TOKEN2CHAR2('>', ">");
852 TOKEN2CHAR2('?', "?");
853 TOKEN2CHAR2('^', "^");
854 TOKEN2CHAR2('|', "|");
855 TOKEN2CHAR2('~', "~");
856 TOKEN2CHAR2(':', ":");
857 TOKEN2CHAR2(',', ",");
858 TOKEN2CHAR2('.', ".");
859 TOKEN2CHAR2(';', ";");
860 TOKEN2CHAR2('`', "`");
861 TOKEN2CHAR2('\n', "nl");
862 TOKEN2CHAR2('{', "\"{\"");
863 TOKEN2CHAR2('}', "\"}\"");
864 TOKEN2CHAR2('[', "\"[\"");
865 TOKEN2CHAR2(']', "\"]\"");
866 TOKEN2CHAR2('(', "\"(\"");
867 TOKEN2CHAR2(')', "\")\"");
868 TOKEN2CHAR2('\\', "backslash");
869 TOKEN2CHAR(keyword_class);
870 TOKEN2CHAR(keyword_module);
871 TOKEN2CHAR(keyword_def);
872 TOKEN2CHAR(keyword_undef);
873 TOKEN2CHAR(keyword_begin);
874 TOKEN2CHAR(keyword_rescue);
875 TOKEN2CHAR(keyword_ensure);
876 TOKEN2CHAR(keyword_end);
877 TOKEN2CHAR(keyword_if);
878 TOKEN2CHAR(keyword_unless);
879 TOKEN2CHAR(keyword_then);
880 TOKEN2CHAR(keyword_elsif);
881 TOKEN2CHAR(keyword_else);
882 TOKEN2CHAR(keyword_case);
883 TOKEN2CHAR(keyword_when);
884 TOKEN2CHAR(keyword_while);
885 TOKEN2CHAR(keyword_until);
886 TOKEN2CHAR(keyword_for);
887 TOKEN2CHAR(keyword_break);
888 TOKEN2CHAR(keyword_next);
889 TOKEN2CHAR(keyword_redo);
890 TOKEN2CHAR(keyword_retry);
891 TOKEN2CHAR(keyword_in);
892 TOKEN2CHAR(keyword_do);
893 TOKEN2CHAR(keyword_do_cond);
894 TOKEN2CHAR(keyword_do_block);
895 TOKEN2CHAR(keyword_do_LAMBDA);
896 TOKEN2CHAR(keyword_return);
897 TOKEN2CHAR(keyword_yield);
898 TOKEN2CHAR(keyword_super);
899 TOKEN2CHAR(keyword_self);
900 TOKEN2CHAR(keyword_nil);
901 TOKEN2CHAR(keyword_true);
902 TOKEN2CHAR(keyword_false);
903 TOKEN2CHAR(keyword_and);
904 TOKEN2CHAR(keyword_or);
905 TOKEN2CHAR(keyword_not);
906 TOKEN2CHAR(modifier_if);
907 TOKEN2CHAR(modifier_unless);
908 TOKEN2CHAR(modifier_while);
909 TOKEN2CHAR(modifier_until);
910 TOKEN2CHAR(modifier_rescue);
911 TOKEN2CHAR(keyword_alias);
912 TOKEN2CHAR(keyword_defined);
913 TOKEN2CHAR(keyword_BEGIN);
914 TOKEN2CHAR(keyword_END);
915 TOKEN2CHAR(keyword__LINE__);
916 TOKEN2CHAR(keyword__FILE__);
917 TOKEN2CHAR(keyword__ENCODING__);
918 TOKEN2CHAR(tIDENTIFIER);
919 TOKEN2CHAR(tFID);
920 TOKEN2CHAR(tGVAR);
921 TOKEN2CHAR(tIVAR);
922 TOKEN2CHAR(tCONSTANT);
923 TOKEN2CHAR(tCVAR);
924 TOKEN2CHAR(tLABEL);
925 TOKEN2CHAR(tINTEGER);
926 TOKEN2CHAR(tFLOAT);
927 TOKEN2CHAR(tRATIONAL);
928 TOKEN2CHAR(tIMAGINARY);
929 TOKEN2CHAR(tCHAR);
930 TOKEN2CHAR(tNTH_REF);
931 TOKEN2CHAR(tBACK_REF);
932 TOKEN2CHAR(tSTRING_CONTENT);
933 TOKEN2CHAR(tREGEXP_END);
934 TOKEN2CHAR(tDUMNY_END);
935 TOKEN2CHAR(tSP);
936 TOKEN2CHAR(tUPLUS);
937 TOKEN2CHAR(tUMINUS);
938 TOKEN2CHAR(tPOW);
939 TOKEN2CHAR(tCMP);
940 TOKEN2CHAR(tEQ);
941 TOKEN2CHAR(tEQQ);
942 TOKEN2CHAR(tNEQ);
943 TOKEN2CHAR(tGEQ);
944 TOKEN2CHAR(tLEQ);
945 TOKEN2CHAR(tANDOP);
946 TOKEN2CHAR(tOROP);
947 TOKEN2CHAR(tMATCH);
948 TOKEN2CHAR(tNMATCH);
949 TOKEN2CHAR(tDOT2);
950 TOKEN2CHAR(tDOT3);
951 TOKEN2CHAR(tBDOT2);
952 TOKEN2CHAR(tBDOT3);
953 TOKEN2CHAR(tAREF);
954 TOKEN2CHAR(tASET);
955 TOKEN2CHAR(tLSHFT);
956 TOKEN2CHAR(tRSHFT);
957 TOKEN2CHAR(tANDDOT);
958 TOKEN2CHAR(tCOLON2);
959 TOKEN2CHAR(tCOLON3);
960 TOKEN2CHAR(tOP_ASGN);
961 TOKEN2CHAR(tASSOC);
962 TOKEN2CHAR(tLPAREN);
963 TOKEN2CHAR(tLPAREN_ARG);
964 TOKEN2CHAR(tRPAREN);
965 TOKEN2CHAR(tLBRACK);
966 TOKEN2CHAR(tLBRACE);
967 TOKEN2CHAR(tLBRACE_ARG);
968 TOKEN2CHAR(tSTAR);
969 TOKEN2CHAR(tDSTAR);
970 TOKEN2CHAR(tAMPER);
971 TOKEN2CHAR(tLAMBDA);
972 TOKEN2CHAR(tSYMBEG);
973 TOKEN2CHAR(tSTRING_BEG);
974 TOKEN2CHAR(tXSTRING_BEG);
975 TOKEN2CHAR(tREGEXP_BEG);
976 TOKEN2CHAR(tWORDS_BEG);
977 TOKEN2CHAR(tQWORDS_BEG);
978 TOKEN2CHAR(tSYMBOLS_BEG);
979 TOKEN2CHAR(tQSYMBOLS_BEG);
980 TOKEN2CHAR(tSTRING_END);
981 TOKEN2CHAR(tSTRING_DEND);
982 TOKEN2CHAR(tSTRING_DBEG);
983 TOKEN2CHAR(tSTRING_DVAR);
984 TOKEN2CHAR(tLAMBEG);
985 TOKEN2CHAR(tLABEL_END);
986 TOKEN2CHAR(tIGNORED_NL);
987 TOKEN2CHAR(tCOMMENT);
988 TOKEN2CHAR(tEMBDOC_BEG);
989 TOKEN2CHAR(tEMBDOC);
990 TOKEN2CHAR(tEMBDOC_END);
991 TOKEN2CHAR(tHEREDOC_BEG);
992 TOKEN2CHAR(tHEREDOC_END);
993 TOKEN2CHAR(k__END__);
994 TOKEN2CHAR(tLOWEST);
995 TOKEN2CHAR(tUMINUS_NUM);
996 TOKEN2CHAR(tLAST_TOKEN);
997#undef TOKEN2CHAR
998#undef TOKEN2CHAR2
999 }
1000
1001 rb_bug("parser_token2id: unknown token %d", tok);
1002
1003 UNREACHABLE_RETURN(0);
1004}
1005#else
1006static void
1007push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
1008{
1009}
1010
1011static void
1012pop_end_expect_token_locations(struct parser_params *p)
1013{
1014}
1015#endif
1016
1017RBIMPL_ATTR_NONNULL((1, 2, 3))
1018static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
1019RBIMPL_ATTR_NONNULL((1, 2))
1020static int parser_yyerror0(struct parser_params*, const char*);
1021#define yyerror0(msg) parser_yyerror0(p, (msg))
1022#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
1023#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
1024#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
1025#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
1026#define lex_eol_p(p) lex_eol_n_p(p, 0)
1027#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
1028#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
1029#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
1030
1031static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
1032static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
1033static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
1034static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
1035static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
1036
1037#ifdef RIPPER
1038#define compile_for_eval (0)
1039#else
1040#define compile_for_eval (p->parent_iseq != 0)
1041#endif
1042
1043#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
1044
1045#define CALL_Q_P(q) ((q) == tANDDOT)
1046#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
1047
1048#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
1049
1050static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
1051
1052static inline void
1053rb_discard_node(struct parser_params *p, NODE *n)
1054{
1055 rb_ast_delete_node(p->ast, n);
1056}
1057
1058static rb_node_scope_t *rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1059static rb_node_scope_t *rb_node_scope_new2(struct parser_params *p, rb_ast_id_table_t *nd_tbl, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1060static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1061static rb_node_if_t *rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc);
1062static 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);
1063static 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);
1064static 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);
1065static 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);
1066static 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);
1067static rb_node_in_t *rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc);
1068static 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);
1069static 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);
1070static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1071static rb_node_for_t *rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc);
1072static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
1073static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
1074static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1075static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
1076static 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);
1077static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
1078static 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);
1079static 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);
1080static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
1081static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1082static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1083static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1084static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1085static 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);
1086static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1087static 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);
1088static 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);
1089static 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);
1090static 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);
1091static 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);
1092static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1093static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1094static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1095static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1096static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1097static rb_node_super_t *rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc);
1098static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
1099static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1100static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1101static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
1102static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1103static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1104static rb_node_yield_t *rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1105static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1106static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1107static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1108static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1109static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1110static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1111static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1112static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1113static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1114static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1115static rb_node_integer_t * rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc);
1116static rb_node_float_t * rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc);
1117static rb_node_rational_t * rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc);
1118static 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);
1119static rb_node_str_t *rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1120static 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);
1121static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1122static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1123static 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);
1124static rb_node_evstr_t *rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1125static rb_node_regx_t *rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc);
1126static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1127static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
1128static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc);
1129static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1130static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1131static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
1132static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1133static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1134static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1135static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1136static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1137static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1138static 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);
1139static 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);
1140static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
1141static rb_node_class_t *rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc);
1142static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc);
1143static rb_node_sclass_t *rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc);
1144static rb_node_colon2_t *rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc);
1145static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1146static rb_node_dot2_t *rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc);
1147static rb_node_dot3_t *rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc);
1148static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
1149static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
1150static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc);
1151static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
1152static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
1153static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1154static rb_node_postexe_t *rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1155static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1156static 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);
1157static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1158static rb_node_lambda_t *rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1159static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1160static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
1161static 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);
1162static rb_node_line_t *rb_node_line_new(struct parser_params *p, const YYLTYPE *loc);
1163static rb_node_file_t *rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1164static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
1165
1166#define NEW_SCOPE(a,b,loc) (NODE *)rb_node_scope_new(p,a,b,loc)
1167#define NEW_SCOPE2(t,a,b,loc) (NODE *)rb_node_scope_new2(p,t,a,b,loc)
1168#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
1169#define NEW_IF(c,t,e,loc) (NODE *)rb_node_if_new(p,c,t,e,loc)
1170#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)
1171#define NEW_CASE(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case_new(p,h,b,loc,ck_loc,ek_loc)
1172#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc)
1173#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc)
1174#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)
1175#define NEW_IN(c,t,e,loc) (NODE *)rb_node_in_new(p,c,t,e,loc)
1176#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)
1177#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)
1178#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
1179#define NEW_FOR(i,b,loc) (NODE *)rb_node_for_new(p,i,b,loc)
1180#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
1181#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
1182#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
1183#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
1184#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc)
1185#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
1186#define NEW_AND(f,s,loc,op_loc) (NODE *)rb_node_and_new(p,f,s,loc,op_loc)
1187#define NEW_OR(f,s,loc,op_loc) (NODE *)rb_node_or_new(p,f,s,loc,op_loc)
1188#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
1189#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
1190#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
1191#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
1192#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
1193#define NEW_CDECL(v,val,path,share,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,share,loc)
1194#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
1195#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)
1196#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)
1197#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
1198#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
1199#define NEW_OP_CDECL(v,op,val,share,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,share,loc)
1200#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
1201#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
1202#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
1203#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
1204#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
1205#define NEW_SUPER(a,loc) (NODE *)rb_node_super_new(p,a,loc)
1206#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
1207#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
1208#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
1209#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1210#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1211#define NEW_RETURN(s,loc,k_loc) (NODE *)rb_node_return_new(p,s,loc,k_loc)
1212#define NEW_YIELD(a,loc) (NODE *)rb_node_yield_new(p,a,loc)
1213#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1214#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1215#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1216#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1217#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1218#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1219#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1220#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1221#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1222#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1223#define NEW_INTEGER(val, base,loc) (NODE *)rb_node_integer_new(p,val,base,loc)
1224#define NEW_FLOAT(val,loc) (NODE *)rb_node_float_new(p,val,loc)
1225#define NEW_RATIONAL(val,base,seen_point,loc) (NODE *)rb_node_rational_new(p,val,base,seen_point,loc)
1226#define NEW_IMAGINARY(val,base,seen_point,numeric_type,loc) (NODE *)rb_node_imaginary_new(p,val,base,seen_point,numeric_type,loc)
1227#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1228#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1229#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1230#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1231#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1232#define NEW_EVSTR(n,loc) (NODE *)rb_node_evstr_new(p,n,loc)
1233#define NEW_REGX(str,opts,loc) (NODE *)rb_node_regx_new(p,str,opts,loc)
1234#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1235#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1236#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1237#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1238#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1239#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1240#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1241#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1242#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc)
1243#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_loc)
1244#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1245#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1246#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
1247#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
1248#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1249#define NEW_CLASS(n,b,s,loc) (NODE *)rb_node_class_new(p,n,b,s,loc)
1250#define NEW_MODULE(n,b,loc) (NODE *)rb_node_module_new(p,n,b,loc)
1251#define NEW_SCLASS(r,b,loc) (NODE *)rb_node_sclass_new(p,r,b,loc)
1252#define NEW_COLON2(c,i,loc) (NODE *)rb_node_colon2_new(p,c,i,loc)
1253#define NEW_COLON3(i,loc) (NODE *)rb_node_colon3_new(p,i,loc)
1254#define NEW_DOT2(b,e,loc) (NODE *)rb_node_dot2_new(p,b,e,loc)
1255#define NEW_DOT3(b,e,loc) (NODE *)rb_node_dot3_new(p,b,e,loc)
1256#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
1257#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
1258#define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc)
1259#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
1260#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
1261#define NEW_DEFINED(e,loc) (NODE *)rb_node_defined_new(p,e,loc)
1262#define NEW_POSTEXE(b,loc) (NODE *)rb_node_postexe_new(p,b,loc)
1263#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc)
1264#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1265#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1266#define NEW_LAMBDA(a,b,loc) (NODE *)rb_node_lambda_new(p,a,b,loc)
1267#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1268#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1269#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1270#define NEW_LINE(loc) (NODE *)rb_node_line_new(p,loc)
1271#define NEW_FILE(str,loc) (NODE *)rb_node_file_new(p,str,loc)
1272#define NEW_ENCODING(loc) (NODE *)rb_node_encoding_new(p,loc)
1273#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1274
1275enum internal_node_type {
1276 NODE_INTERNAL_ONLY = NODE_LAST,
1277 NODE_DEF_TEMP,
1278 NODE_EXITS,
1279 NODE_INTERNAL_LAST
1280};
1281
1282static const char *
1283parser_node_name(int node)
1284{
1285 switch (node) {
1286 case NODE_DEF_TEMP:
1287 return "NODE_DEF_TEMP";
1288 case NODE_EXITS:
1289 return "NODE_EXITS";
1290 default:
1291 return ruby_node_name(node);
1292 }
1293}
1294
1295/* This node is parse.y internal */
1296struct RNode_DEF_TEMP {
1297 NODE node;
1298
1299 /* for NODE_DEFN/NODE_DEFS */
1300
1301 struct RNode *nd_def;
1302 ID nd_mid;
1303
1304 struct {
1305 int max_numparam;
1306 NODE *numparam_save;
1307 struct lex_context ctxt;
1308 } save;
1309};
1310
1311#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1312
1313static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1314static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1315static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1316static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1317static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1318
1319#define NEW_BREAK(s,loc,k_loc) (NODE *)rb_node_break_new(p,s,loc,k_loc)
1320#define NEW_NEXT(s,loc,k_loc) (NODE *)rb_node_next_new(p,s,loc,k_loc)
1321#define NEW_REDO(loc,k_loc) (NODE *)rb_node_redo_new(p,loc,k_loc)
1322#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1323
1324/* Make a new internal node, which should not be appeared in the
1325 * result AST and does not have node_id and location. */
1326static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1327#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1328
1329static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1330
1331static int
1332parser_get_node_id(struct parser_params *p)
1333{
1334 int node_id = p->node_id;
1335 p->node_id++;
1336 return node_id;
1337}
1338
1339static void
1340anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1341{
1342 if (id == tANDDOT) {
1343 yyerror1(loc, "&. inside multiple assignment destination");
1344 }
1345}
1346
1347static inline void
1348set_line_body(NODE *body, int line)
1349{
1350 if (!body) return;
1351 switch (nd_type(body)) {
1352 case NODE_RESCUE:
1353 case NODE_ENSURE:
1354 nd_set_line(body, line);
1355 }
1356}
1357
1358static void
1359set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1360{
1361 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1362 nd_set_line(node, beg->end_pos.lineno);
1363}
1364
1365static NODE *
1366last_expr_node(NODE *expr)
1367{
1368 while (expr) {
1369 if (nd_type_p(expr, NODE_BLOCK)) {
1370 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1371 }
1372 else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) {
1373 expr = RNODE_BEGIN(expr)->nd_body;
1374 }
1375 else {
1376 break;
1377 }
1378 }
1379 return expr;
1380}
1381
1382#ifndef RIPPER
1383#define yyparse ruby_yyparse
1384#endif
1385
1386static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1387static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1388#define new_nil(loc) NEW_NIL(loc)
1389static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1390static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
1391static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1392static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1393
1394static NODE *newline_node(NODE*);
1395static void fixpos(NODE*,NODE*);
1396
1397static int value_expr_gen(struct parser_params*,NODE*);
1398static void void_expr(struct parser_params*,NODE*);
1399static NODE *remove_begin(NODE*);
1400#define value_expr(node) value_expr_gen(p, (node))
1401static NODE *void_stmts(struct parser_params*,NODE*);
1402static void reduce_nodes(struct parser_params*,NODE**);
1403static void block_dup_check(struct parser_params*,NODE*,NODE*);
1404
1405static NODE *block_append(struct parser_params*,NODE*,NODE*);
1406static NODE *list_append(struct parser_params*,NODE*,NODE*);
1407static NODE *list_concat(NODE*,NODE*);
1408static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1409static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1410static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1411static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1412static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
1413static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1414static NODE *str2dstr(struct parser_params*,NODE*);
1415static NODE *evstr2dstr(struct parser_params*,NODE*);
1416static NODE *splat_array(NODE*);
1417static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1418
1419static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1420static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1421static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1422static 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);
1423static 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;}
1424
1425static bool args_info_empty_p(struct rb_args_info *args);
1426static 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*);
1427static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1428static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1429static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1430static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1431static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1432static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1433static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1434
1435static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1436static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1437
1438static NODE* negate_lit(struct parser_params*, NODE*);
1439static NODE *ret_args(struct parser_params*,NODE*);
1440static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1441static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
1442static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1443
1444static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1445static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1446
1447static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1448static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1449
1450static VALUE rb_backref_error(struct parser_params*,NODE*);
1451static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1452
1453static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1454static 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);
1455static 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);
1456static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1457static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1458
1459static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1460
1461static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1462static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1463
1464static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1465static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1466
1467static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
1468
1469static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
1470
1471#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1472
1473static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1474
1475static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1476
1477static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1478
1479static rb_ast_id_table_t *local_tbl(struct parser_params*);
1480
1481static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1482static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1483int rb_parser_reg_fragment_check(struct parser_params*, rb_parser_string_t*, int, rb_parser_reg_fragment_error_func);
1484static void reg_fragment_error(struct parser_params *, VALUE);
1485#define reg_fragment_check(p, str, option) rb_parser_reg_fragment_check(p, str, option, reg_fragment_error)
1486
1487static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1488static NODE *heredoc_dedent(struct parser_params*,NODE*);
1489
1490static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1491
1492#ifdef RIPPER
1493#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1494#define set_value(val) (p->s_lvalue = val)
1495static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1496static int id_is_var(struct parser_params *p, ID id);
1497#endif
1498
1499RUBY_SYMBOL_EXPORT_BEGIN
1500VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1501int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1502enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1503VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1504void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1505PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1506YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1507YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1508YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1509YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1510YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1511YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1512void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1513RUBY_SYMBOL_EXPORT_END
1514
1515static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1516static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1517static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1518static VALUE formal_argument_error(struct parser_params*, ID);
1519static ID shadowing_lvar(struct parser_params*,ID);
1520static void new_bv(struct parser_params*,ID);
1521
1522static void local_push(struct parser_params*,int);
1523static void local_pop(struct parser_params*);
1524static void local_var(struct parser_params*, ID);
1525static void arg_var(struct parser_params*, ID);
1526static int local_id(struct parser_params *p, ID id);
1527static int local_id_ref(struct parser_params*, ID, ID **);
1528#define internal_id rb_parser_internal_id
1529ID internal_id(struct parser_params*);
1530static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1531static int check_forwarding_args(struct parser_params*);
1532static void add_forwarding_args(struct parser_params *p);
1533static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1534
1535static const struct vtable *dyna_push(struct parser_params *);
1536static void dyna_pop(struct parser_params*, const struct vtable *);
1537static int dyna_in_block(struct parser_params*);
1538#define dyna_var(p, id) local_var(p, id)
1539static int dvar_defined(struct parser_params*, ID);
1540#define dvar_defined_ref rb_parser_dvar_defined_ref
1541int dvar_defined_ref(struct parser_params*, ID, ID**);
1542static int dvar_curr(struct parser_params*,ID);
1543
1544static int lvar_defined(struct parser_params*, ID);
1545
1546static NODE *numparam_push(struct parser_params *p);
1547static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1548
1549#define METHOD_NOT '!'
1550
1551#define idFWD_REST '*'
1552#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1553#define idFWD_BLOCK '&'
1554#define idFWD_ALL idDot3
1555#define arg_FWD_BLOCK idFWD_BLOCK
1556
1557#define RE_ONIG_OPTION_IGNORECASE 1
1558#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1559#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1560#define RE_OPTION_ONCE (1<<16)
1561#define RE_OPTION_ENCODING_SHIFT 8
1562#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1563#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1564#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1565#define RE_OPTION_MASK 0xff
1566#define RE_OPTION_ARG_ENCODING_NONE 32
1567
1568#define CHECK_LITERAL_WHEN (st_table *)1
1569#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1570
1571#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1572RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1573
1574#define TOKEN2ID(tok) ( \
1575 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1576 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1577 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1578 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1579 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1580 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1581 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1582
1583/****** Ripper *******/
1584
1585#ifdef RIPPER
1586
1587#include "eventids1.h"
1588#include "eventids2.h"
1589
1590extern const struct ripper_parser_ids ripper_parser_ids;
1591
1592static VALUE ripper_dispatch0(struct parser_params*,ID);
1593static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1594static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1595static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1596static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1597static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1598static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1599void ripper_error(struct parser_params *p);
1600
1601#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1602#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1603#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1604#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1605#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1606#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1607#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1608
1609#define yyparse ripper_yyparse
1610
1611static VALUE
1612aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1613{
1614 if (!NIL_P(pre_arg)) {
1615 if (!NIL_P(pre_args)) {
1616 rb_ary_unshift(pre_args, pre_arg);
1617 }
1618 else {
1619 pre_args = rb_ary_new_from_args(1, pre_arg);
1620 }
1621 }
1622 return pre_args;
1623}
1624
1625#define ID2VAL(id) STATIC_ID2SYM(id)
1626#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1627#endif /* RIPPER */
1628
1629#define KWD2EID(t, v) keyword_##t
1630
1631static NODE *
1632new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, const YYLTYPE *loc)
1633{
1634 body = remove_begin(body);
1635 reduce_nodes(p, &body);
1636 NODE *n = NEW_SCOPE(args, body, loc);
1637 nd_set_line(n, loc->end_pos.lineno);
1638 set_line_body(body, loc->beg_pos.lineno);
1639 return n;
1640}
1641
1642static NODE *
1643rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1644 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1645{
1646 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1647 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1648 loc.beg_pos = arg_loc->beg_pos;
1649 return NEW_RESCUE(arg, rescue, 0, &loc);
1650}
1651
1652static NODE *add_block_exit(struct parser_params *p, NODE *node);
1653static rb_node_exits_t *init_block_exit(struct parser_params *p);
1654static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1655static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1656static void clear_block_exit(struct parser_params *p, bool error);
1657
1658static void
1659next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1660{
1661 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1662}
1663
1664static void
1665restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1666{
1667 /* See: def_name action */
1668 struct lex_context ctxt = temp->save.ctxt;
1669 p->ctxt.in_def = ctxt.in_def;
1670 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1671 p->ctxt.in_rescue = ctxt.in_rescue;
1672 p->max_numparam = temp->save.max_numparam;
1673 numparam_pop(p, temp->save.numparam_save);
1674 clear_block_exit(p, true);
1675}
1676
1677static void
1678endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1679{
1680 if (is_attrset_id(mid)) {
1681 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1682 }
1683 token_info_drop(p, "def", loc->beg_pos);
1684}
1685
1686#define debug_token_line(p, name, line) do { \
1687 if (p->debug) { \
1688 const char *const pcur = p->lex.pcur; \
1689 const char *const ptok = p->lex.ptok; \
1690 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1691 line, p->ruby_sourceline, \
1692 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1693 } \
1694 } while (0)
1695
1696#define begin_definition(k, loc_beg, loc_end) \
1697 do { \
1698 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1699 /* singleton class */ \
1700 p->ctxt.cant_return = !p->ctxt.in_def; \
1701 p->ctxt.in_def = 0; \
1702 } \
1703 else if (p->ctxt.in_def) { \
1704 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1705 yyerror1(&loc, k " definition in method body"); \
1706 } \
1707 else { \
1708 p->ctxt.cant_return = 1; \
1709 } \
1710 local_push(p, 0); \
1711 } while (0)
1712
1713#ifndef RIPPER
1714# define ifndef_ripper(x) (x)
1715# define ifdef_ripper(r,x) (x)
1716#else
1717# define ifndef_ripper(x)
1718# define ifdef_ripper(r,x) (r)
1719#endif
1720
1721# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1722# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1723# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1724# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1725# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1726# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1727# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1728# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1729# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1730# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1731# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1732# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1733# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1734# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1735# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1736# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1737# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1738# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1739# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1740# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1741#ifdef RIPPER
1742extern const ID id_warn, id_warning, id_gets, id_assoc;
1743# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1744# define WARN_S_L(s,l) STR_NEW(s,l)
1745# define WARN_S(s) STR_NEW2(s)
1746# define WARN_I(i) INT2NUM(i)
1747# define WARN_ID(i) rb_id2str(i)
1748# define PRIsWARN PRIsVALUE
1749# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1750# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1751# ifdef HAVE_VA_ARGS_MACRO
1752# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1753# else
1754# define WARN_CALL rb_funcall
1755# endif
1756# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1757# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1758# ifdef HAVE_VA_ARGS_MACRO
1759# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1760# else
1761# define WARNING_CALL rb_funcall
1762# endif
1763# define compile_error ripper_compile_error
1764#else
1765# define WARN_S_L(s,l) s
1766# define WARN_S(s) s
1767# define WARN_I(i) i
1768# define WARN_ID(i) rb_id2name(i)
1769# define PRIsWARN PRIsVALUE
1770# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1771# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1772# define WARN_CALL rb_compile_warn
1773# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1774# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1775# define WARNING_CALL rb_compile_warning
1776PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1777# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1778#endif
1779
1780#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1781
1782static NODE *
1783add_block_exit(struct parser_params *p, NODE *node)
1784{
1785 if (!node) {
1786 compile_error(p, "unexpected null node");
1787 return 0;
1788 }
1789 switch (nd_type(node)) {
1790 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1791 default:
1792 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1793 return node;
1794 }
1795 if (!p->ctxt.in_defined) {
1796 rb_node_exits_t *exits = p->exits;
1797 if (exits) {
1798 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1799 exits->nd_stts = node;
1800 }
1801 }
1802 return node;
1803}
1804
1805static rb_node_exits_t *
1806init_block_exit(struct parser_params *p)
1807{
1808 rb_node_exits_t *old = p->exits;
1809 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1810 exits->nd_chain = 0;
1811 exits->nd_stts = RNODE(exits);
1812 p->exits = exits;
1813 return old;
1814}
1815
1816static rb_node_exits_t *
1817allow_block_exit(struct parser_params *p)
1818{
1819 rb_node_exits_t *exits = p->exits;
1820 p->exits = 0;
1821 return exits;
1822}
1823
1824static void
1825restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1826{
1827 p->exits = exits;
1828}
1829
1830static void
1831clear_block_exit(struct parser_params *p, bool error)
1832{
1833 rb_node_exits_t *exits = p->exits;
1834 if (!exits) return;
1835 if (error) {
1836 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1837 switch (nd_type(e)) {
1838 case NODE_BREAK:
1839 yyerror1(&e->nd_loc, "Invalid break");
1840 break;
1841 case NODE_NEXT:
1842 yyerror1(&e->nd_loc, "Invalid next");
1843 break;
1844 case NODE_REDO:
1845 yyerror1(&e->nd_loc, "Invalid redo");
1846 break;
1847 default:
1848 yyerror1(&e->nd_loc, "unexpected node");
1849 goto end_checks; /* no nd_chain */
1850 }
1851 }
1852 end_checks:;
1853 }
1854 exits->nd_stts = RNODE(exits);
1855 exits->nd_chain = 0;
1856}
1857
1858#define WARN_EOL(tok) \
1859 (looking_at_eol_p(p) ? \
1860 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1861 (void)0)
1862static int looking_at_eol_p(struct parser_params *p);
1863
1864static NODE *
1865get_nd_value(struct parser_params *p, NODE *node)
1866{
1867 switch (nd_type(node)) {
1868 case NODE_GASGN:
1869 return RNODE_GASGN(node)->nd_value;
1870 case NODE_IASGN:
1871 return RNODE_IASGN(node)->nd_value;
1872 case NODE_LASGN:
1873 return RNODE_LASGN(node)->nd_value;
1874 case NODE_DASGN:
1875 return RNODE_DASGN(node)->nd_value;
1876 case NODE_MASGN:
1877 return RNODE_MASGN(node)->nd_value;
1878 case NODE_CVASGN:
1879 return RNODE_CVASGN(node)->nd_value;
1880 case NODE_CDECL:
1881 return RNODE_CDECL(node)->nd_value;
1882 default:
1883 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1884 return 0;
1885 }
1886}
1887
1888static void
1889set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1890{
1891 switch (nd_type(node)) {
1892 case NODE_CDECL:
1893 RNODE_CDECL(node)->nd_value = rhs;
1894 break;
1895 case NODE_GASGN:
1896 RNODE_GASGN(node)->nd_value = rhs;
1897 break;
1898 case NODE_IASGN:
1899 RNODE_IASGN(node)->nd_value = rhs;
1900 break;
1901 case NODE_LASGN:
1902 RNODE_LASGN(node)->nd_value = rhs;
1903 break;
1904 case NODE_DASGN:
1905 RNODE_DASGN(node)->nd_value = rhs;
1906 break;
1907 case NODE_MASGN:
1908 RNODE_MASGN(node)->nd_value = rhs;
1909 break;
1910 case NODE_CVASGN:
1911 RNODE_CVASGN(node)->nd_value = rhs;
1912 break;
1913 default:
1914 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1915 break;
1916 }
1917}
1918
1919static ID
1920get_nd_vid(struct parser_params *p, NODE *node)
1921{
1922 switch (nd_type(node)) {
1923 case NODE_CDECL:
1924 return RNODE_CDECL(node)->nd_vid;
1925 case NODE_GASGN:
1926 return RNODE_GASGN(node)->nd_vid;
1927 case NODE_IASGN:
1928 return RNODE_IASGN(node)->nd_vid;
1929 case NODE_LASGN:
1930 return RNODE_LASGN(node)->nd_vid;
1931 case NODE_DASGN:
1932 return RNODE_DASGN(node)->nd_vid;
1933 case NODE_CVASGN:
1934 return RNODE_CVASGN(node)->nd_vid;
1935 default:
1936 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1937 return 0;
1938 }
1939}
1940
1941static NODE *
1942get_nd_args(struct parser_params *p, NODE *node)
1943{
1944 switch (nd_type(node)) {
1945 case NODE_CALL:
1946 return RNODE_CALL(node)->nd_args;
1947 case NODE_OPCALL:
1948 return RNODE_OPCALL(node)->nd_args;
1949 case NODE_FCALL:
1950 return RNODE_FCALL(node)->nd_args;
1951 case NODE_QCALL:
1952 return RNODE_QCALL(node)->nd_args;
1953 case NODE_SUPER:
1954 return RNODE_SUPER(node)->nd_args;
1955 case NODE_VCALL:
1956 case NODE_ZSUPER:
1957 case NODE_YIELD:
1958 case NODE_RETURN:
1959 case NODE_BREAK:
1960 case NODE_NEXT:
1961 return 0;
1962 default:
1963 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1964 return 0;
1965 }
1966}
1967
1968static st_index_t
1969djb2(const uint8_t *str, size_t len)
1970{
1971 st_index_t hash = 5381;
1972
1973 for (size_t i = 0; i < len; i++) {
1974 hash = ((hash << 5) + hash) + str[i];
1975 }
1976
1977 return hash;
1978}
1979
1980static st_index_t
1981parser_memhash(const void *ptr, long len)
1982{
1983 return djb2(ptr, len);
1984}
1985
1986#define PARSER_STRING_PTR(str) (str->ptr)
1987#define PARSER_STRING_LEN(str) (str->len)
1988#define PARSER_STRING_END(str) (&str->ptr[str->len])
1989#define STRING_SIZE(str) ((size_t)str->len + 1)
1990#define STRING_TERM_LEN(str) (1)
1991#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
1992#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
1993 SIZED_REALLOC_N(str->ptr, char, (size_t)total + termlen, STRING_SIZE(str)); \
1994 str->len = total; \
1995} while (0)
1996#define STRING_SET_LEN(str, n) do { \
1997 (str)->len = (n); \
1998} while (0)
1999#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2000 ((ptrvar) = str->ptr, \
2001 (lenvar) = str->len)
2002
2003static inline int
2004parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2005{
2006 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2007}
2008
2009static rb_parser_string_t *
2010rb_parser_string_new(rb_parser_t *p, const char *ptr, long len)
2011{
2012 rb_parser_string_t *str;
2013
2014 if (len < 0) {
2015 rb_bug("negative string size (or size too big): %ld", len);
2016 }
2017
2018 str = xcalloc(1, sizeof(rb_parser_string_t));
2019 str->ptr = xcalloc(len + 1, sizeof(char));
2020
2021 if (ptr) {
2022 memcpy(PARSER_STRING_PTR(str), ptr, len);
2023 }
2024 STRING_SET_LEN(str, len);
2025 STRING_TERM_FILL(str);
2026 return str;
2027}
2028
2029static rb_parser_string_t *
2030rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, long len, rb_encoding *enc)
2031{
2032 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2033 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2034 str->enc = enc;
2035 return str;
2036}
2037
2038#ifndef RIPPER
2039rb_parser_string_t *
2040rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2041{
2042 /* Type check */
2043 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2044 RB_GC_GUARD(str);
2045 return ret;
2046}
2047
2048void
2049rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2050{
2051 if (!str) return;
2052 xfree(PARSER_STRING_PTR(str));
2053 xfree(str);
2054}
2055#endif
2056
2057static st_index_t
2058rb_parser_str_hash(rb_parser_string_t *str)
2059{
2060 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2061}
2062
2063static st_index_t
2064rb_char_p_hash(const char *c)
2065{
2066 return parser_memhash((const void *)c, strlen(c));
2067}
2068
2069static size_t
2070rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2071{
2072 return PARSER_STRING_LEN(str);
2073}
2074
2075#ifndef RIPPER
2076static char *
2077rb_parser_string_end(rb_parser_string_t *str)
2078{
2079 return &str->ptr[str->len];
2080}
2081#endif
2082
2083static void
2084rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2085{
2086 str->enc = enc;
2087}
2088
2089static rb_encoding *
2090rb_parser_str_get_encoding(rb_parser_string_t *str)
2091{
2092 return str->enc;
2093}
2094
2095#ifndef RIPPER
2096static bool
2097PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2098{
2099 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2100}
2101#endif
2102
2103static int
2104PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2105{
2106 return str->coderange;
2107}
2108
2109static void
2110PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2111{
2112 str->coderange = coderange;
2113}
2114
2115static void
2116PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2117{
2118 rb_parser_string_set_encoding(str, enc);
2119 PARSER_ENC_CODERANGE_SET(str, cr);
2120}
2121
2122static void
2123PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2124{
2125 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2126}
2127
2128static bool
2129PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2130{
2131 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2132}
2133
2134static bool
2135PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2136{
2137 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2138}
2139
2140static const char *
2141rb_parser_search_nonascii(const char *p, const char *e)
2142{
2143 const char *s = p;
2144
2145 for (; s < e; s++) {
2146 if (*s & 0x80) return s;
2147 }
2148
2149 return NULL;
2150}
2151
2152static int
2153rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc)
2154{
2155 const char *e = ptr + len;
2156
2157 if (enc == rb_ascii8bit_encoding()) {
2158 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2159 ptr = rb_parser_search_nonascii(ptr, e);
2160 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2161 }
2162
2163 /* parser string encoding is always asciicompat */
2164 ptr = rb_parser_search_nonascii(ptr, e);
2165 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2166 for (;;) {
2167 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2168 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2169 ptr += MBCLEN_CHARFOUND_LEN(ret);
2170 if (ptr == e) break;
2171 ptr = rb_parser_search_nonascii(ptr, e);
2172 if (!ptr) break;
2173 }
2174
2175 return RB_PARSER_ENC_CODERANGE_VALID;
2176}
2177
2178static int
2179rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2180{
2181 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2182}
2183
2184static int
2185rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2186{
2187 int cr = PARSER_ENC_CODERANGE(str);
2188
2189 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2190 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2191 PARSER_ENC_CODERANGE_SET(str, cr);
2192 }
2193
2194 return cr;
2195}
2196
2197static rb_parser_string_t *
2198rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2199{
2200 if (rb_parser_str_get_encoding(str) == enc)
2201 return str;
2202 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2203 PARSER_ENC_CODERANGE_CLEAR(str);
2204 }
2205 rb_parser_string_set_encoding(str, enc);
2206 return str;
2207}
2208
2209static bool
2210rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2211{
2212 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2213}
2214
2215static rb_encoding *
2216rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2217{
2218 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2219 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2220
2221 if (enc1 == NULL || enc2 == NULL)
2222 return 0;
2223
2224 if (enc1 == enc2) {
2225 return enc1;
2226 }
2227
2228 if (PARSER_STRING_LEN(str2) == 0)
2229 return enc1;
2230 if (PARSER_STRING_LEN(str1) == 0)
2231 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2232
2233 int cr1, cr2;
2234
2235 cr1 = rb_parser_enc_str_coderange(p, str1);
2236 cr2 = rb_parser_enc_str_coderange(p, str2);
2237
2238 if (cr1 != cr2) {
2239 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2240 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2241 }
2242
2243 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2244 return enc1;
2245 }
2246
2247 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2248 return enc2;
2249 }
2250
2251 return 0;
2252}
2253
2254static void
2255rb_parser_str_modify(rb_parser_string_t *str)
2256{
2257 PARSER_ENC_CODERANGE_CLEAR(str);
2258}
2259
2260static void
2261rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len)
2262{
2263 long capa;
2264 const int termlen = STRING_TERM_LEN(str);
2265
2266 if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2267 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
2268 }
2269
2270 int cr = PARSER_ENC_CODERANGE(str);
2271 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2272 /* Leave unknown. */
2273 }
2274 else if (len > PARSER_STRING_LEN(str)) {
2275 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2276 }
2277 else if (len < PARSER_STRING_LEN(str)) {
2278 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2279 /* ASCII-only string is keeping after truncated. Valid
2280 * and broken may be invalid or valid, leave unknown. */
2281 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2282 }
2283 }
2284
2285 STRING_SET_LEN(str, len);
2286 STRING_TERM_FILL(str);
2287}
2288
2289static rb_parser_string_t *
2290rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len)
2291{
2292 rb_parser_str_modify(str);
2293 if (len == 0) return 0;
2294
2295 long total, olen, off = -1;
2296 char *sptr;
2297 const int termlen = STRING_TERM_LEN(str);
2298
2299 PARSER_STRING_GETMEM(str, sptr, olen);
2300 if (ptr >= sptr && ptr <= sptr + olen) {
2301 off = ptr - sptr;
2302 }
2303
2304 if (olen > LONG_MAX - len) {
2305 compile_error(p, "string sizes too big");
2306 return 0;
2307 }
2308 total = olen + len;
2309 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2310 sptr = PARSER_STRING_PTR(str);
2311 if (off != -1) {
2312 ptr = sptr + off;
2313 }
2314 memcpy(sptr + olen, ptr, len);
2315 STRING_SET_LEN(str, total);
2316 STRING_TERM_FILL(str);
2317
2318 return str;
2319}
2320
2321#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2322#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2323
2324static rb_parser_string_t *
2325rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2326 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2327{
2328 int str_cr, res_cr;
2329 rb_encoding *str_enc, *res_enc;
2330
2331 str_enc = rb_parser_str_get_encoding(str);
2332 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2333
2334 if (str_enc == ptr_enc) {
2335 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2336 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2337 }
2338 }
2339 else {
2340 /* parser string encoding is always asciicompat */
2341 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2342 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2343 }
2344 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2345 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2346 str_cr = rb_parser_enc_str_coderange(p, str);
2347 }
2348 }
2349 }
2350 if (ptr_cr_ret)
2351 *ptr_cr_ret = ptr_cr;
2352
2353 if (str_enc != ptr_enc &&
2354 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2355 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2356 goto incompatible;
2357 }
2358
2359 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2360 res_enc = str_enc;
2361 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2362 }
2363 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2364 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2365 res_enc = str_enc;
2366 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2367 }
2368 else {
2369 res_enc = ptr_enc;
2370 res_cr = ptr_cr;
2371 }
2372 }
2373 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2374 res_enc = str_enc;
2375 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2376 res_cr = str_cr;
2377 else
2378 res_cr = ptr_cr;
2379 }
2380 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2381 res_enc = str_enc;
2382 res_cr = str_cr;
2383 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2384 }
2385
2386 if (len < 0) {
2387 compile_error(p, "negative string size (or size too big)");
2388 }
2389 parser_str_cat(str, ptr, len);
2390 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2391 return str;
2392
2393 incompatible:
2394 compile_error(p, "incompatible character encodings: %s and %s",
2395 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2396 UNREACHABLE_RETURN(0);
2397
2398}
2399
2400static rb_parser_string_t *
2401rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2402 rb_encoding *ptr_enc)
2403{
2404 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2405}
2406
2407static rb_parser_string_t *
2408rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2409{
2410 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2411
2412 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2413 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2414
2415 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2416
2417 return str;
2418}
2419
2420static rb_parser_string_t *
2421rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len)
2422{
2423 if (len < 0) {
2424 rb_bug("negative string size (or size too big)");
2425 }
2426
2427 long slen = PARSER_STRING_LEN(str);
2428
2429 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2430 PARSER_ENC_CODERANGE_CLEAR(str);
2431 }
2432
2433 {
2434 long capa;
2435 const int termlen = STRING_TERM_LEN(str);
2436
2437 if ((capa = slen) < len) {
2438 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2439 }
2440 else if (len == slen) return str;
2441 STRING_SET_LEN(str, len);
2442 STRING_TERM_FILL(str);
2443 }
2444 return str;
2445}
2446
2447# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2448 ((ptrvar) = str->ptr, \
2449 (lenvar) = str->len, \
2450 (encvar) = str->enc)
2451
2452static int
2453rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2454{
2455 long len1, len2;
2456 const char *ptr1, *ptr2;
2457 rb_encoding *enc1, *enc2;
2458
2459 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2460 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2461
2462 return (len1 != len2 ||
2463 enc1 != enc2 ||
2464 memcmp(ptr1, ptr2, len1) != 0);
2465}
2466
2467static void
2468rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2469{
2470 long i;
2471 if (ary->capa < len) {
2472 ary->capa = len;
2473 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2474 for (i = ary->len; i < len; i++) {
2475 ary->data[i] = 0;
2476 }
2477 }
2478}
2479
2480/*
2481 * Do not call this directly.
2482 * Use rb_parser_ary_new_capa_for_XXX() instead.
2483 */
2484static rb_parser_ary_t *
2485parser_ary_new_capa(rb_parser_t *p, long len)
2486{
2487 if (len < 0) {
2488 rb_bug("negative array size (or size too big): %ld", len);
2489 }
2490 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2491 ary->data_type = 0;
2492 ary->len = 0;
2493 ary->capa = len;
2494 if (0 < len) {
2495 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2496 }
2497 else {
2498 ary->data = NULL;
2499 }
2500 return ary;
2501}
2502
2503#ifndef RIPPER
2504static rb_parser_ary_t *
2505rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2506{
2507 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2508 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2509 return ary;
2510}
2511
2512static rb_parser_ary_t *
2513rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2514{
2515 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2516 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2517 return ary;
2518}
2519#endif
2520
2521static rb_parser_ary_t *
2522rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2523{
2524 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2525 ary->data_type = PARSER_ARY_DATA_NODE;
2526 return ary;
2527}
2528
2529/*
2530 * Do not call this directly.
2531 * Use rb_parser_ary_push_XXX() instead.
2532 */
2533static rb_parser_ary_t *
2534parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2535{
2536 if (ary->len == ary->capa) {
2537 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2538 }
2539 ary->data[ary->len++] = val;
2540 return ary;
2541}
2542
2543#ifndef RIPPER
2544static rb_parser_ary_t *
2545rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2546{
2547 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2548 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2549 }
2550 return parser_ary_push(p, ary, val);
2551}
2552
2553static rb_parser_ary_t *
2554rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2555{
2556 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2557 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2558 }
2559 return parser_ary_push(p, ary, val);
2560}
2561#endif
2562
2563static rb_parser_ary_t *
2564rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2565{
2566 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2567 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2568 }
2569 return parser_ary_push(p, ary, val);
2570}
2571
2572#ifndef RIPPER
2573static void
2574rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2575{
2576 if (!token) return;
2577 rb_parser_string_free(p, token->str);
2578 xfree(token);
2579}
2580
2581static void
2582rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2583{
2584# define foreach_ary(ptr) \
2585 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2586 ptr < end_ary_data; ptr++)
2587 switch (ary->data_type) {
2588 case PARSER_ARY_DATA_AST_TOKEN:
2589 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2590 break;
2591 case PARSER_ARY_DATA_SCRIPT_LINE:
2592 foreach_ary(data) {rb_parser_string_free(p, *data);}
2593 break;
2594 case PARSER_ARY_DATA_NODE:
2595 /* Do nothing because nodes are freed when rb_ast_t is freed */
2596 break;
2597 default:
2598 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2599 break;
2600 }
2601# undef foreach_ary
2602 xfree(ary->data);
2603 xfree(ary);
2604}
2605
2606#endif /* !RIPPER */
2607%}
2608
2609%expect 0
2610%define api.pure
2611%define parse.error verbose
2612%printer {
2613 if ((NODE *)$$ == (NODE *)-1) {
2614 rb_parser_printf(p, "NODE_SPECIAL");
2615 }
2616 else if ($$) {
2617 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2618 }
2619} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2620 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2621%printer {
2622 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2623} <id>
2624%printer {
2625 switch (nd_type(RNODE($$))) {
2626 case NODE_INTEGER:
2627 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2628 break;
2629 case NODE_FLOAT:
2630 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2631 break;
2632 case NODE_RATIONAL:
2633 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2634 break;
2635 case NODE_IMAGINARY:
2636 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2637 break;
2638 default:
2639 break;
2640 }
2641} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2642%printer {
2643 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2644} tNTH_REF
2645%printer {
2646 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2647} tBACK_REF
2648
2649%destructor {
2650 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2651} <labels>
2652
2653%lex-param {struct parser_params *p}
2654%parse-param {struct parser_params *p}
2655%initial-action
2656{
2657 RUBY_SET_YYLLOC_OF_NONE(@$);
2658};
2659%after-shift after_shift
2660%before-reduce before_reduce
2661%after-reduce after_reduce
2662%after-shift-error-token after_shift_error_token
2663%after-pop-stack after_pop_stack
2664
2665%union {
2666 NODE *node;
2667 rb_node_fcall_t *node_fcall;
2668 rb_node_args_t *node_args;
2669 rb_node_args_aux_t *node_args_aux;
2670 rb_node_opt_arg_t *node_opt_arg;
2671 rb_node_kw_arg_t *node_kw_arg;
2672 rb_node_block_pass_t *node_block_pass;
2673 rb_node_masgn_t *node_masgn;
2674 rb_node_def_temp_t *node_def_temp;
2675 rb_node_exits_t *node_exits;
2676 ID id;
2677 int num;
2678 st_table *tbl;
2679 st_table *labels;
2680 const struct vtable *vars;
2681 struct rb_strterm_struct *strterm;
2682 struct lex_context ctxt;
2683 enum lex_state_e state;
2684}
2685
2686%token <id>
2687 keyword_class "'class'"
2688 keyword_module "'module'"
2689 keyword_def "'def'"
2690 keyword_undef "'undef'"
2691 keyword_begin "'begin'"
2692 keyword_rescue "'rescue'"
2693 keyword_ensure "'ensure'"
2694 keyword_end "'end'"
2695 keyword_if "'if'"
2696 keyword_unless "'unless'"
2697 keyword_then "'then'"
2698 keyword_elsif "'elsif'"
2699 keyword_else "'else'"
2700 keyword_case "'case'"
2701 keyword_when "'when'"
2702 keyword_while "'while'"
2703 keyword_until "'until'"
2704 keyword_for "'for'"
2705 keyword_break "'break'"
2706 keyword_next "'next'"
2707 keyword_redo "'redo'"
2708 keyword_retry "'retry'"
2709 keyword_in "'in'"
2710 keyword_do "'do'"
2711 keyword_do_cond "'do' for condition"
2712 keyword_do_block "'do' for block"
2713 keyword_do_LAMBDA "'do' for lambda"
2714 keyword_return "'return'"
2715 keyword_yield "'yield'"
2716 keyword_super "'super'"
2717 keyword_self "'self'"
2718 keyword_nil "'nil'"
2719 keyword_true "'true'"
2720 keyword_false "'false'"
2721 keyword_and "'and'"
2722 keyword_or "'or'"
2723 keyword_not "'not'"
2724 modifier_if "'if' modifier"
2725 modifier_unless "'unless' modifier"
2726 modifier_while "'while' modifier"
2727 modifier_until "'until' modifier"
2728 modifier_rescue "'rescue' modifier"
2729 keyword_alias "'alias'"
2730 keyword_defined "'defined?'"
2731 keyword_BEGIN "'BEGIN'"
2732 keyword_END "'END'"
2733 keyword__LINE__ "'__LINE__'"
2734 keyword__FILE__ "'__FILE__'"
2735 keyword__ENCODING__ "'__ENCODING__'"
2736
2737%token <id> tIDENTIFIER "local variable or method"
2738%token <id> tFID "method"
2739%token <id> tGVAR "global variable"
2740%token <id> tIVAR "instance variable"
2741%token <id> tCONSTANT "constant"
2742%token <id> tCVAR "class variable"
2743%token <id> tLABEL "label"
2744%token <node> tINTEGER "integer literal"
2745%token <node> tFLOAT "float literal"
2746%token <node> tRATIONAL "rational literal"
2747%token <node> tIMAGINARY "imaginary literal"
2748%token <node> tCHAR "char literal"
2749%token <node> tNTH_REF "numbered reference"
2750%token <node> tBACK_REF "back reference"
2751%token <node> tSTRING_CONTENT "literal content"
2752%token <num> tREGEXP_END
2753%token <num> tDUMNY_END "dummy end"
2754
2755%type <node> singleton strings string string1 xstring regexp
2756%type <node> string_contents xstring_contents regexp_contents string_content
2757%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2758%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2759%type <node_def_temp> defn_head defs_head k_def
2760%type <node_exits> block_open k_while k_until k_for allow_exits
2761%type <node> top_compstmt top_stmts top_stmt begin_block endless_arg endless_command
2762%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
2763%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2764%type <node_fcall> fcall
2765%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2766%type <node> args arg_splat call_args opt_call_args
2767%type <node> paren_args opt_paren_args
2768%type <node_args> args_tail block_args_tail
2769%type <node> command_args aref_args
2770%type <node_block_pass> opt_block_arg block_arg
2771%type <node> var_ref var_lhs
2772%type <node> command_rhs arg_rhs
2773%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2774%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args
2775%type <node_args_aux> f_arg f_arg_item
2776%type <node> f_marg f_marg_list f_rest_marg
2777%type <node_masgn> f_margs
2778%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2779%type <node_args> block_param opt_block_param block_param_def
2780%type <node_kw_arg> f_kw f_block_kw
2781%type <id> bv_decls opt_bv_decl bvar
2782%type <node> lambda lambda_body brace_body do_body
2783%type <node_args> f_larglist
2784%type <node> brace_block cmd_brace_block do_block lhs none fitem
2785%type <node> mlhs_head mlhs_item mlhs_node mlhs_post
2786%type <node_masgn> mlhs mlhs_basic mlhs_inner
2787%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2788%type <node> p_expr p_as p_alt p_expr_basic p_find
2789%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2790%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2791%type <node> p_kwargs p_kwarg p_kw
2792%type <id> keyword_variable user_variable sym operation operation2 operation3
2793%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
2794%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2795%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2796%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2797%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2798%type <ctxt> p_in_kwarg
2799%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2800%type <num> max_numparam
2801%type <node> numparam
2802%type <id> it_id
2803%token END_OF_INPUT 0 "end-of-input"
2804%token <id> '.'
2805
2806/* escaped chars, should be ignored otherwise */
2807%token <id> '\\' "backslash"
2808%token tSP "escaped space"
2809%token <id> '\t' "escaped horizontal tab"
2810%token <id> '\f' "escaped form feed"
2811%token <id> '\r' "escaped carriage return"
2812%token <id> '\13' "escaped vertical tab"
2813%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2814%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2815%token tPOW RUBY_TOKEN(POW) "**"
2816%token tCMP RUBY_TOKEN(CMP) "<=>"
2817%token tEQ RUBY_TOKEN(EQ) "=="
2818%token tEQQ RUBY_TOKEN(EQQ) "==="
2819%token tNEQ RUBY_TOKEN(NEQ) "!="
2820%token tGEQ RUBY_TOKEN(GEQ) ">="
2821%token tLEQ RUBY_TOKEN(LEQ) "<="
2822%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2823%token tOROP RUBY_TOKEN(OROP) "||"
2824%token tMATCH RUBY_TOKEN(MATCH) "=~"
2825%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2826%token tDOT2 RUBY_TOKEN(DOT2) ".."
2827%token tDOT3 RUBY_TOKEN(DOT3) "..."
2828%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2829%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2830%token tAREF RUBY_TOKEN(AREF) "[]"
2831%token tASET RUBY_TOKEN(ASET) "[]="
2832%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2833%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2834%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2835%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2836%token tCOLON3 ":: at EXPR_BEG"
2837%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2838%token tASSOC "=>"
2839%token tLPAREN "("
2840%token tLPAREN_ARG "( arg"
2841%token tRPAREN ")"
2842%token tLBRACK "["
2843%token tLBRACE "{"
2844%token tLBRACE_ARG "{ arg"
2845%token tSTAR "*"
2846%token tDSTAR "**arg"
2847%token tAMPER "&"
2848%token <num> tLAMBDA "->"
2849%token tSYMBEG "symbol literal"
2850%token tSTRING_BEG "string literal"
2851%token tXSTRING_BEG "backtick literal"
2852%token tREGEXP_BEG "regexp literal"
2853%token tWORDS_BEG "word list"
2854%token tQWORDS_BEG "verbatim word list"
2855%token tSYMBOLS_BEG "symbol list"
2856%token tQSYMBOLS_BEG "verbatim symbol list"
2857%token tSTRING_END "terminator"
2858%token tSTRING_DEND "'}'"
2859%token <state> tSTRING_DBEG "'#{'"
2860%token tSTRING_DVAR tLAMBEG tLABEL_END
2861
2862%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2863%token tHEREDOC_BEG tHEREDOC_END k__END__
2864
2865/*
2866 * precedence table
2867 */
2868
2869%nonassoc tLOWEST
2870%nonassoc tLBRACE_ARG
2871
2872%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2873%left keyword_or keyword_and
2874%right keyword_not
2875%nonassoc keyword_defined
2876%right '=' tOP_ASGN
2877%left modifier_rescue
2878%right '?' ':'
2879%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2880%left tOROP
2881%left tANDOP
2882%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2883%left '>' tGEQ '<' tLEQ
2884%left '|' '^'
2885%left '&'
2886%left tLSHFT tRSHFT
2887%left '+' '-'
2888%left '*' '/' '%'
2889%right tUMINUS_NUM tUMINUS
2890%right tPOW
2891%right '!' '~' tUPLUS
2892
2893%token tLAST_TOKEN
2894
2895/*
2896 * inlining rules
2897 */
2898%rule %inline ident_or_const
2899 : tIDENTIFIER
2900 | tCONSTANT
2901 ;
2902
2903%rule %inline user_or_keyword_variable
2904 : user_variable
2905 | keyword_variable
2906 ;
2907
2908/*
2909 * parameterizing rules
2910 */
2911%rule f_opt(value) <node_opt_arg>
2912 : f_arg_asgn f_eq value
2913 {
2914 p->ctxt.in_argdef = 1;
2915 $$ = NEW_OPT_ARG(assignable(p, $1, $3, &@$), &@$);
2916 /*% ripper: [$:$, $:3] %*/
2917 }
2918 ;
2919
2920%rule f_optarg(value) <node_opt_arg>
2921 : f_opt(value)
2922 {
2923 $$ = $1;
2924 /*% ripper: rb_ary_new3(1, $:1) %*/
2925 }
2926 | f_optarg(value) ',' f_opt(value)
2927 {
2928 $$ = opt_arg_append($1, $3);
2929 /*% ripper: rb_ary_push($:1, $:3) %*/
2930 }
2931 ;
2932
2933%rule f_kwarg(kw) <node_kw_arg>
2934 : kw
2935 {
2936 $$ = $1;
2937 /*% ripper: rb_ary_new3(1, $:1) %*/
2938 }
2939 | f_kwarg(kw) ',' kw
2940 {
2941 $$ = kwd_append($1, $3);
2942 /*% ripper: rb_ary_push($:1, $:3) %*/
2943 }
2944 ;
2945
2946%rule opt_args_tail(tail) <node_args>
2947 : ',' tail
2948 {
2949 $$ = $2;
2950 /*% ripper: $:2 %*/
2951 }
2952 | /* none */
2953 {
2954 $$ = new_args_tail(p, 0, 0, 0, &@0);
2955 /*% ripper: [Qnil, Qnil, Qnil] %*/
2956 }
2957 ;
2958
2959%rule words(begin, word_list)
2960 : begin ' '+ word_list tSTRING_END
2961 {
2962 $$ = make_list($3, &@$);
2963 /*% ripper: array!($:3) %*/
2964 }
2965 ;
2966
2967%%
2968program : {
2969 SET_LEX_STATE(EXPR_BEG);
2970 local_push(p, ifndef_ripper(1)+0);
2971 /* jumps are possible in the top-level loop. */
2972 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
2973 }
2974 top_compstmt
2975 {
2976 if ($2 && !compile_for_eval) {
2977 NODE *node = $2;
2978 /* last expression should not be void */
2979 if (nd_type_p(node, NODE_BLOCK)) {
2980 while (RNODE_BLOCK(node)->nd_next) {
2981 node = RNODE_BLOCK(node)->nd_next;
2982 }
2983 node = RNODE_BLOCK(node)->nd_head;
2984 }
2985 node = remove_begin(node);
2986 void_expr(p, node);
2987 }
2988 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
2989 /*% ripper[final]: program!($:2) %*/
2990 local_pop(p);
2991 }
2992 ;
2993
2994top_compstmt : top_stmts terms?
2995 {
2996 void_stmts(p, $$ = $1);
2997 }
2998 ;
2999
3000top_stmts : none
3001 {
3002 $$ = NEW_BEGIN(0, &@$);
3003 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3004 }
3005 | top_stmt
3006 {
3007 $$ = newline_node($1);
3008 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3009 }
3010 | top_stmts terms top_stmt
3011 {
3012 $$ = block_append(p, $1, newline_node($3));
3013 /*% ripper: stmts_add!($:1, $:3) %*/
3014 }
3015 ;
3016
3017top_stmt : stmt
3018 {
3019 clear_block_exit(p, true);
3020 $$ = $1;
3021 }
3022 | keyword_BEGIN begin_block
3023 {
3024 $$ = $2;
3025 /*% ripper: $:2 %*/
3026 }
3027 ;
3028
3029block_open : '{' {$$ = init_block_exit(p);};
3030
3031begin_block : block_open top_compstmt '}'
3032 {
3033 restore_block_exit(p, $block_open);
3034 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3035 NEW_BEGIN($2, &@$));
3036 $$ = NEW_BEGIN(0, &@$);
3037 /*% ripper: BEGIN!($:2) %*/
3038 }
3039 ;
3040
3041bodystmt : compstmt[body]
3042 lex_ctxt[ctxt]
3043 opt_rescue
3044 k_else
3045 {
3046 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3047 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3048 }
3049 compstmt[elsebody]
3050 {
3051 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3052 }
3053 opt_ensure
3054 {
3055 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3056 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3057 }
3058 | compstmt[body]
3059 lex_ctxt[ctxt]
3060 opt_rescue
3061 {
3062 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3063 }
3064 opt_ensure
3065 {
3066 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3067 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3068 }
3069 ;
3070
3071compstmt : stmts terms?
3072 {
3073 void_stmts(p, $$ = $1);
3074 }
3075 ;
3076
3077stmts : none
3078 {
3079 $$ = NEW_BEGIN(0, &@$);
3080 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3081 }
3082 | stmt_or_begin
3083 {
3084 $$ = newline_node($1);
3085 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3086 }
3087 | stmts terms stmt_or_begin
3088 {
3089 $$ = block_append(p, $1, newline_node($3));
3090 /*% ripper: stmts_add!($:1, $:3) %*/
3091 }
3092 ;
3093
3094stmt_or_begin : stmt
3095 {
3096 $$ = $1;
3097 }
3098 | keyword_BEGIN
3099 {
3100 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3101 }
3102 begin_block
3103 {
3104 $$ = $3;
3105 }
3106 ;
3107
3108allow_exits : {$$ = allow_block_exit(p);};
3109
3110k_END : keyword_END lex_ctxt
3111 {
3112 $$ = $2;
3113 p->ctxt.in_rescue = before_rescue;
3114 /*% ripper: $:2 %*/
3115 };
3116
3117stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3118 {
3119 $$ = NEW_ALIAS($2, $4, &@$, &@1);
3120 /*% ripper: alias!($:2, $:4) %*/
3121 }
3122 | keyword_alias tGVAR tGVAR
3123 {
3124 $$ = NEW_VALIAS($2, $3, &@$, &@1);
3125 /*% ripper: var_alias!($:2, $:3) %*/
3126 }
3127 | keyword_alias tGVAR tBACK_REF
3128 {
3129 char buf[2];
3130 buf[0] = '$';
3131 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
3132 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$, &@1);
3133 /*% ripper: var_alias!($:2, $:3) %*/
3134 }
3135 | keyword_alias tGVAR tNTH_REF
3136 {
3137 static const char mesg[] = "can't make alias for the number variables";
3138 /*%%%*/
3139 yyerror1(&@3, mesg);
3140 /*% %*/
3141 $$ = NEW_ERROR(&@$);
3142 /*% ripper[error]: alias_error!(ERR_MESG(), $:3) %*/
3143 }
3144 | keyword_undef undef_list
3145 {
3146 nd_set_first_loc($2, @1.beg_pos);
3147 RNODE_UNDEF($2)->keyword_loc = @1;
3148 $$ = $2;
3149 /*% ripper: undef!($:2) %*/
3150 }
3151 | stmt modifier_if expr_value
3152 {
3153 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
3154 fixpos($$, $3);
3155 /*% ripper: if_mod!($:3, $:1) %*/
3156 }
3157 | stmt modifier_unless expr_value
3158 {
3159 $$ = new_unless(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3160 fixpos($$, $3);
3161 /*% ripper: unless_mod!($:3, $:1) %*/
3162 }
3163 | stmt modifier_while expr_value
3164 {
3165 clear_block_exit(p, false);
3166 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3167 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3168 }
3169 else {
3170 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3171 }
3172 /*% ripper: while_mod!($:3, $:1) %*/
3173 }
3174 | stmt modifier_until expr_value
3175 {
3176 clear_block_exit(p, false);
3177 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3178 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3179 }
3180 else {
3181 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3182 }
3183 /*% ripper: until_mod!($:3, $:1) %*/
3184 }
3185 | stmt modifier_rescue after_rescue stmt
3186 {
3187 p->ctxt.in_rescue = $3.in_rescue;
3188 NODE *resq;
3189 YYLTYPE loc = code_loc_gen(&@2, &@4);
3190 resq = NEW_RESBODY(0, 0, remove_begin($4), 0, &loc);
3191 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
3192 /*% ripper: rescue_mod!($:1, $:4) %*/
3193 }
3194 | k_END allow_exits '{' compstmt '}'
3195 {
3196 if (p->ctxt.in_def) {
3197 rb_warn0("END in method; use at_exit");
3198 }
3199 restore_block_exit(p, $allow_exits);
3200 p->ctxt = $k_END;
3201 {
3202 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, &@$);
3203 $$ = NEW_POSTEXE(scope, &@$);
3204 }
3205 /*% ripper: END!($:compstmt) %*/
3206 }
3207 | command_asgn
3208 | mlhs '=' lex_ctxt command_call
3209 {
3210 value_expr($4);
3211 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3212 /*% ripper: massign!($:1, $:4) %*/
3213 }
3214 | lhs '=' lex_ctxt mrhs
3215 {
3216 $$ = node_assign(p, $1, $4, $3, &@$);
3217 /*% ripper: assign!($:1, $:4) %*/
3218 }
3219 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
3220 after_rescue stmt[resbody]
3221 {
3222 p->ctxt.in_rescue = $3.in_rescue;
3223 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3224 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3225 loc.beg_pos = @mrhs_arg.beg_pos;
3226 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3227 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
3228 /*% ripper: massign!($:1, rescue_mod!($:4, $:7)) %*/
3229 }
3230 | mlhs '=' lex_ctxt mrhs_arg
3231 {
3232 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3233 /*% ripper: massign!($:1, $:4) %*/
3234 }
3235 | expr
3236 | error
3237 {
3238 (void)yynerrs;
3239 $$ = NEW_ERROR(&@$);
3240 }
3241 ;
3242
3243command_asgn : lhs '=' lex_ctxt command_rhs
3244 {
3245 $$ = node_assign(p, $1, $4, $3, &@$);
3246 /*% ripper: assign!($:1, $:4) %*/
3247 }
3248 | var_lhs tOP_ASGN lex_ctxt command_rhs
3249 {
3250 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
3251 /*% ripper: opassign!($:1, $:2, $:4) %*/
3252 }
3253 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
3254 {
3255 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$, &NULL_LOC, &@2, &@4, &@5);
3256 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3257
3258 }
3259 | primary_value call_op ident_or_const tOP_ASGN lex_ctxt command_rhs
3260 {
3261 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$, &@2, &@3, &@4);
3262 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3263 }
3264 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
3265 {
3266 YYLTYPE loc = code_loc_gen(&@1, &@3);
3267 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
3268 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3269 }
3270 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
3271 {
3272 $$ = new_attr_op_assign(p, $1, idCOLON2, $3, $4, $6, &@$, &@2, &@3, &@4);
3273 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3274 }
3275 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt command_rhs
3276 {
3277 YYLTYPE loc = code_loc_gen(&@1, &@2);
3278 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
3279 /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
3280 }
3281 | defn_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
3282 {
3283 endless_method_name(p, $head->nd_mid, &@head);
3284 restore_defun(p, $head);
3285 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3286 ($$ = $head->nd_def)->nd_loc = @$;
3287 RNODE_DEFN($$)->nd_defn = $bodystmt;
3288 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
3289 /*% ripper: def!($:head, $:args, $:$) %*/
3290 local_pop(p);
3291 }
3292 | defs_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
3293 {
3294 endless_method_name(p, $head->nd_mid, &@head);
3295 restore_defun(p, $head);
3296 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3297 ($$ = $head->nd_def)->nd_loc = @$;
3298 RNODE_DEFS($$)->nd_defn = $bodystmt;
3299 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
3300 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
3301 local_pop(p);
3302 }
3303 | backref tOP_ASGN lex_ctxt command_rhs
3304 {
3305 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3306 $$ = NEW_ERROR(&@$);
3307 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3308 }
3309 ;
3310
3311endless_command : command
3312 | endless_command modifier_rescue after_rescue arg
3313 {
3314 p->ctxt.in_rescue = $3.in_rescue;
3315 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3316 /*% ripper: rescue_mod!($:1, $:4) %*/
3317 }
3318 | keyword_not '\n'? endless_command
3319 {
3320 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3321 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3322 }
3323 ;
3324
3325command_rhs : command_call %prec tOP_ASGN
3326 {
3327 value_expr($1);
3328 $$ = $1;
3329 }
3330 | command_call modifier_rescue after_rescue stmt
3331 {
3332 p->ctxt.in_rescue = $3.in_rescue;
3333 YYLTYPE loc = code_loc_gen(&@2, &@4);
3334 value_expr($1);
3335 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3336 /*% ripper: rescue_mod!($:1, $:4) %*/
3337 }
3338 | command_asgn
3339 ;
3340
3341expr : command_call
3342 | expr keyword_and expr
3343 {
3344 $$ = logop(p, idAND, $1, $3, &@2, &@$);
3345 /*% ripper: binary!($:1, ID2VAL(idAND), $:3) %*/
3346 }
3347 | expr keyword_or expr
3348 {
3349 $$ = logop(p, idOR, $1, $3, &@2, &@$);
3350 /*% ripper: binary!($:1, ID2VAL(idOR), $:3) %*/
3351 }
3352 | keyword_not '\n'? expr
3353 {
3354 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3355 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3356 }
3357 | '!' command_call
3358 {
3359 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3360 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
3361 }
3362 | arg tASSOC
3363 {
3364 value_expr($arg);
3365 }
3366 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3367 p_top_expr_body[body]
3368 {
3369 pop_pktbl(p, $p_pktbl);
3370 pop_pvtbl(p, $p_pvtbl);
3371 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3372 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body), &@$, &NULL_LOC, &NULL_LOC);
3373 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3374 }
3375 | arg keyword_in
3376 {
3377 value_expr($arg);
3378 }
3379 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3380 p_top_expr_body[body]
3381 {
3382 pop_pktbl(p, $p_pktbl);
3383 pop_pvtbl(p, $p_pvtbl);
3384 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3385 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body), &@$, &NULL_LOC, &NULL_LOC);
3386 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3387 }
3388 | arg %prec tLBRACE_ARG
3389 ;
3390
3391def_name : fname
3392 {
3393 ID fname = $1;
3394 numparam_name(p, fname);
3395 local_push(p, 0);
3396 p->ctxt.in_def = 1;
3397 p->ctxt.in_rescue = before_rescue;
3398 p->ctxt.cant_return = 0;
3399 $$ = $1;
3400 }
3401 ;
3402
3403defn_head : k_def def_name
3404 {
3405 $$ = def_head_save(p, $k_def);
3406 $$->nd_mid = $def_name;
3407 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3408 /*% ripper: $:def_name %*/
3409 }
3410 ;
3411
3412defs_head : k_def singleton dot_or_colon
3413 {
3414 SET_LEX_STATE(EXPR_FNAME);
3415 }
3416 def_name
3417 {
3418 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3419 $$ = def_head_save(p, $k_def);
3420 $$->nd_mid = $def_name;
3421 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3422 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3423 }
3424 ;
3425
3426expr_value : expr
3427 {
3428 value_expr($1);
3429 $$ = $1;
3430 }
3431 | error
3432 {
3433 $$ = NEW_ERROR(&@$);
3434 }
3435 ;
3436
3437expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3438 {
3439 $$ = $2;
3440 /*% ripper: $:2 %*/
3441 }
3442 ;
3443
3444command_call : command
3445 | block_command
3446 ;
3447
3448block_command : block_call
3449 | block_call call_op2 operation2 command_args
3450 {
3451 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3452 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3453 }
3454 ;
3455
3456cmd_brace_block : tLBRACE_ARG brace_body '}'
3457 {
3458 $$ = $2;
3459 set_embraced_location($$, &@1, &@3);
3460 /*% ripper: $:2 %*/
3461 }
3462 ;
3463
3464fcall : operation
3465 {
3466 $$ = NEW_FCALL($1, 0, &@$);
3467 /*% ripper: $:1 %*/
3468 }
3469 ;
3470
3471command : fcall command_args %prec tLOWEST
3472 {
3473 $1->nd_args = $2;
3474 nd_set_last_loc($1, @2.end_pos);
3475 $$ = (NODE *)$1;
3476 /*% ripper: command!($:1, $:2) %*/
3477 }
3478 | fcall command_args cmd_brace_block
3479 {
3480 block_dup_check(p, $2, $3);
3481 $1->nd_args = $2;
3482 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3483 fixpos($$, RNODE($1));
3484 nd_set_last_loc($1, @2.end_pos);
3485 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3486 }
3487 | primary_value call_op operation2 command_args %prec tLOWEST
3488 {
3489 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3490 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3491 }
3492 | primary_value call_op operation2 command_args cmd_brace_block
3493 {
3494 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3495 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3496 }
3497 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3498 {
3499 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3500 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3501 }
3502 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3503 {
3504 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3505 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3506 }
3507 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3508 {
3509 set_embraced_location($5, &@4, &@6);
3510 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3511 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3512 }
3513 | keyword_super command_args
3514 {
3515 $$ = NEW_SUPER($2, &@$);
3516 fixpos($$, $2);
3517 /*% ripper: super!($:2) %*/
3518 }
3519 | k_yield command_args
3520 {
3521 $$ = new_yield(p, $2, &@$);
3522 fixpos($$, $2);
3523 /*% ripper: yield!($:2) %*/
3524 }
3525 | k_return call_args
3526 {
3527 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3528 /*% ripper: return!($:2) %*/
3529 }
3530 | keyword_break call_args
3531 {
3532 NODE *args = 0;
3533 args = ret_args(p, $2);
3534 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3535 /*% ripper: break!($:2) %*/
3536 }
3537 | keyword_next call_args
3538 {
3539 NODE *args = 0;
3540 args = ret_args(p, $2);
3541 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3542 /*% ripper: next!($:2) %*/
3543 }
3544 ;
3545
3546mlhs : mlhs_basic
3547 | tLPAREN mlhs_inner rparen
3548 {
3549 $$ = $2;
3550 /*% ripper: mlhs_paren!($:2) %*/
3551 }
3552 ;
3553
3554mlhs_inner : mlhs_basic
3555 | tLPAREN mlhs_inner rparen
3556 {
3557 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3558 /*% ripper: mlhs_paren!($:2) %*/
3559 }
3560 ;
3561
3562mlhs_basic : mlhs_head
3563 {
3564 $$ = NEW_MASGN($1, 0, &@$);
3565 /*% ripper: $:1 %*/
3566 }
3567 | mlhs_head mlhs_item
3568 {
3569 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3570 /*% ripper: mlhs_add!($:1, $:2) %*/
3571 }
3572 | mlhs_head tSTAR mlhs_node
3573 {
3574 $$ = NEW_MASGN($1, $3, &@$);
3575 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3576 }
3577 | mlhs_head tSTAR mlhs_node ',' mlhs_post
3578 {
3579 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3580 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3581 }
3582 | mlhs_head tSTAR
3583 {
3584 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3585 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3586 }
3587 | mlhs_head tSTAR ',' mlhs_post
3588 {
3589 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3590 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3591 }
3592 | tSTAR mlhs_node
3593 {
3594 $$ = NEW_MASGN(0, $2, &@$);
3595 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3596 }
3597 | tSTAR mlhs_node ',' mlhs_post
3598 {
3599 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3600 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3601 }
3602 | tSTAR
3603 {
3604 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3605 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3606 }
3607 | tSTAR ',' mlhs_post
3608 {
3609 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3610 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3611 }
3612 ;
3613
3614mlhs_item : mlhs_node
3615 | tLPAREN mlhs_inner rparen
3616 {
3617 $$ = (NODE *)$2;
3618 /*% ripper: mlhs_paren!($:2) %*/
3619 }
3620 ;
3621
3622mlhs_head : mlhs_item ','
3623 {
3624 $$ = NEW_LIST($1, &@1);
3625 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3626 }
3627 | mlhs_head mlhs_item ','
3628 {
3629 $$ = list_append(p, $1, $2);
3630 /*% ripper: mlhs_add!($:1, $:2) %*/
3631 }
3632 ;
3633
3634mlhs_post : mlhs_item
3635 {
3636 $$ = NEW_LIST($1, &@$);
3637 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3638 }
3639 | mlhs_post ',' mlhs_item
3640 {
3641 $$ = list_append(p, $1, $3);
3642 /*% ripper: mlhs_add!($:1, $:3) %*/
3643 }
3644 ;
3645
3646mlhs_node : user_or_keyword_variable
3647 {
3648 /*% ripper: var_field!($:1) %*/
3649 $$ = assignable(p, $1, 0, &@$);
3650 }
3651 | primary_value '[' opt_call_args rbracket
3652 {
3653 $$ = aryset(p, $1, $3, &@$);
3654 /*% ripper: aref_field!($:1, $:3) %*/
3655 }
3656 | primary_value call_op tIDENTIFIER
3657 {
3658 anddot_multiple_assignment_check(p, &@2, $2);
3659 $$ = attrset(p, $1, $2, $3, &@$);
3660 /*% ripper: field!($:1, $:2, $:3) %*/
3661 }
3662 | primary_value tCOLON2 tIDENTIFIER
3663 {
3664 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3665 /*% ripper: const_path_field!($:1, $:3) %*/
3666 }
3667 | primary_value call_op tCONSTANT
3668 {
3669 anddot_multiple_assignment_check(p, &@2, $2);
3670 $$ = attrset(p, $1, $2, $3, &@$);
3671 /*% ripper: field!($:1, $:2, $:3) %*/
3672 }
3673 | primary_value tCOLON2 tCONSTANT
3674 {
3675 /*% ripper: const_path_field!($:1, $:3) %*/
3676 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3677 }
3678 | tCOLON3 tCONSTANT
3679 {
3680 /*% ripper: top_const_field!($:2) %*/
3681 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3682 }
3683 | backref
3684 {
3685 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3686 $$ = NEW_ERROR(&@$);
3687 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3688 }
3689 ;
3690
3691lhs : user_or_keyword_variable
3692 {
3693 /*% ripper: var_field!($:1) %*/
3694 $$ = assignable(p, $1, 0, &@$);
3695 }
3696 | primary_value '[' opt_call_args rbracket
3697 {
3698 $$ = aryset(p, $1, $3, &@$);
3699 /*% ripper: aref_field!($:1, $:3) %*/
3700 }
3701 | primary_value call_op tIDENTIFIER
3702 {
3703 $$ = attrset(p, $1, $2, $3, &@$);
3704 /*% ripper: field!($:1, $:2, $:3) %*/
3705 }
3706 | primary_value tCOLON2 tIDENTIFIER
3707 {
3708 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3709 /*% ripper: field!($:1, $:2, $:3) %*/
3710 }
3711 | primary_value call_op tCONSTANT
3712 {
3713 $$ = attrset(p, $1, $2, $3, &@$);
3714 /*% ripper: field!($:1, $:2, $:3) %*/
3715 }
3716 | primary_value tCOLON2 tCONSTANT
3717 {
3718 /*% ripper: const_path_field!($:1, $:3) %*/
3719 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3720 }
3721 | tCOLON3 tCONSTANT
3722 {
3723 /*% ripper: top_const_field!($:2) %*/
3724 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3725 }
3726 | backref
3727 {
3728 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3729 $$ = NEW_ERROR(&@$);
3730 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3731 }
3732 ;
3733
3734cname : tIDENTIFIER
3735 {
3736 static const char mesg[] = "class/module name must be CONSTANT";
3737 /*%%%*/
3738 yyerror1(&@1, mesg);
3739 /*% %*/
3740 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3741 }
3742 | tCONSTANT
3743 ;
3744
3745cpath : tCOLON3 cname
3746 {
3747 $$ = NEW_COLON3($2, &@$);
3748 /*% ripper: top_const_ref!($:2) %*/
3749 }
3750 | cname
3751 {
3752 $$ = NEW_COLON2(0, $1, &@$);
3753 /*% ripper: const_ref!($:1) %*/
3754 }
3755 | primary_value tCOLON2 cname
3756 {
3757 $$ = NEW_COLON2($1, $3, &@$);
3758 /*% ripper: const_path_ref!($:1, $:3) %*/
3759 }
3760 ;
3761
3762fname : ident_or_const
3763 | tFID
3764 | op
3765 {
3766 SET_LEX_STATE(EXPR_ENDFN);
3767 $$ = $1;
3768 }
3769 | reswords
3770 ;
3771
3772fitem : fname
3773 {
3774 $$ = NEW_SYM(rb_id2str($1), &@$);
3775 /*% ripper: symbol_literal!($:1) %*/
3776 }
3777 | symbol
3778 ;
3779
3780undef_list : fitem
3781 {
3782 $$ = NEW_UNDEF($1, &@$);
3783 /*% ripper: rb_ary_new3(1, $:1) %*/
3784 }
3785 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3786 {
3787 nd_set_last_loc($1, @4.end_pos);
3788 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3789 /*% ripper: rb_ary_push($:1, $:4) %*/
3790 }
3791 ;
3792
3793op : '|' { $$ = '|'; }
3794 | '^' { $$ = '^'; }
3795 | '&' { $$ = '&'; }
3796 | tCMP { $$ = tCMP; }
3797 | tEQ { $$ = tEQ; }
3798 | tEQQ { $$ = tEQQ; }
3799 | tMATCH { $$ = tMATCH; }
3800 | tNMATCH { $$ = tNMATCH; }
3801 | '>' { $$ = '>'; }
3802 | tGEQ { $$ = tGEQ; }
3803 | '<' { $$ = '<'; }
3804 | tLEQ { $$ = tLEQ; }
3805 | tNEQ { $$ = tNEQ; }
3806 | tLSHFT { $$ = tLSHFT; }
3807 | tRSHFT { $$ = tRSHFT; }
3808 | '+' { $$ = '+'; }
3809 | '-' { $$ = '-'; }
3810 | '*' { $$ = '*'; }
3811 | tSTAR { $$ = '*'; }
3812 | '/' { $$ = '/'; }
3813 | '%' { $$ = '%'; }
3814 | tPOW { $$ = tPOW; }
3815 | tDSTAR { $$ = tDSTAR; }
3816 | '!' { $$ = '!'; }
3817 | '~' { $$ = '~'; }
3818 | tUPLUS { $$ = tUPLUS; }
3819 | tUMINUS { $$ = tUMINUS; }
3820 | tAREF { $$ = tAREF; }
3821 | tASET { $$ = tASET; }
3822 | '`' { $$ = '`'; }
3823 ;
3824
3825reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3826 | keyword_BEGIN | keyword_END
3827 | keyword_alias | keyword_and | keyword_begin
3828 | keyword_break | keyword_case | keyword_class | keyword_def
3829 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3830 | keyword_end | keyword_ensure | keyword_false
3831 | keyword_for | keyword_in | keyword_module | keyword_next
3832 | keyword_nil | keyword_not | keyword_or | keyword_redo
3833 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3834 | keyword_super | keyword_then | keyword_true | keyword_undef
3835 | keyword_when | keyword_yield | keyword_if | keyword_unless
3836 | keyword_while | keyword_until
3837 ;
3838
3839arg : lhs '=' lex_ctxt arg_rhs
3840 {
3841 $$ = node_assign(p, $1, $4, $3, &@$);
3842 /*% ripper: assign!($:1, $:4) %*/
3843 }
3844 | var_lhs tOP_ASGN lex_ctxt arg_rhs
3845 {
3846 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
3847 /*% ripper: opassign!($:1, $:2, $:4) %*/
3848 }
3849 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
3850 {
3851 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$, &NULL_LOC, &@2, &@4, &@5);
3852 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3853 }
3854 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3855 {
3856 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$, &@2, &@3, &@4);
3857 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3858 }
3859 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3860 {
3861 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$, &@2, &@3, &@4);
3862 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3863 }
3864 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3865 {
3866 $$ = new_attr_op_assign(p, $1, idCOLON2, $3, $4, $6, &@$, &@2, &@3, &@4);
3867 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3868 }
3869 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3870 {
3871 YYLTYPE loc = code_loc_gen(&@1, &@3);
3872 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
3873 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3874 }
3875 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3876 {
3877 YYLTYPE loc = code_loc_gen(&@1, &@2);
3878 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
3879 /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
3880 }
3881 | backref tOP_ASGN lex_ctxt arg_rhs
3882 {
3883 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3884 $$ = NEW_ERROR(&@$);
3885 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3886 }
3887 | arg tDOT2 arg
3888 {
3889 value_expr($1);
3890 value_expr($3);
3891 $$ = NEW_DOT2($1, $3, &@$);
3892 /*% ripper: dot2!($:1, $:3) %*/
3893 }
3894 | arg tDOT3 arg
3895 {
3896 value_expr($1);
3897 value_expr($3);
3898 $$ = NEW_DOT3($1, $3, &@$);
3899 /*% ripper: dot3!($:1, $:3) %*/
3900 }
3901 | arg tDOT2
3902 {
3903 value_expr($1);
3904 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
3905 /*% ripper: dot2!($:1, Qnil) %*/
3906 }
3907 | arg tDOT3
3908 {
3909 value_expr($1);
3910 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
3911 /*% ripper: dot3!($:1, Qnil) %*/
3912 }
3913 | tBDOT2 arg
3914 {
3915 value_expr($2);
3916 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
3917 /*% ripper: dot2!(Qnil, $:2) %*/
3918 }
3919 | tBDOT3 arg
3920 {
3921 value_expr($2);
3922 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
3923 /*% ripper: dot3!(Qnil, $:2) %*/
3924 }
3925 | arg '+' arg
3926 {
3927 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3928 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3929 }
3930 | arg '-' arg
3931 {
3932 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3933 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3934 }
3935 | arg '*' arg
3936 {
3937 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3938 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3939 }
3940 | arg '/' arg
3941 {
3942 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3943 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3944 }
3945 | arg '%' arg
3946 {
3947 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3948 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3949 }
3950 | arg tPOW arg
3951 {
3952 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3953 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3954 }
3955 | tUMINUS_NUM simple_numeric tPOW arg
3956 {
3957 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3958 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3959 }
3960 | tUPLUS arg
3961 {
3962 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3963 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3964 }
3965 | tUMINUS arg
3966 {
3967 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3968 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3969 }
3970 | arg '|' arg
3971 {
3972 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3973 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3974 }
3975 | arg '^' arg
3976 {
3977 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3978 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3979 }
3980 | arg '&' arg
3981 {
3982 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3983 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
3984 }
3985 | arg tCMP arg
3986 {
3987 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3988 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
3989 }
3990 | rel_expr %prec tCMP
3991 | arg tEQ arg
3992 {
3993 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3994 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
3995 }
3996 | arg tEQQ arg
3997 {
3998 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
3999 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
4000 }
4001 | arg tNEQ arg
4002 {
4003 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4004 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4005 }
4006 | arg tMATCH arg
4007 {
4008 $$ = match_op(p, $1, $3, &@2, &@$);
4009 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4010 }
4011 | arg tNMATCH arg
4012 {
4013 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4014 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4015 }
4016 | '!' arg
4017 {
4018 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4019 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4020 }
4021 | '~' arg
4022 {
4023 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4024 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4025 }
4026 | arg tLSHFT arg
4027 {
4028 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4029 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4030 }
4031 | arg tRSHFT arg
4032 {
4033 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4034 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4035 }
4036 | arg tANDOP arg
4037 {
4038 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4039 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4040 }
4041 | arg tOROP arg
4042 {
4043 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4044 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4045 }
4046 | keyword_defined '\n'? begin_defined arg
4047 {
4048 p->ctxt.in_defined = $3.in_defined;
4049 $$ = new_defined(p, $4, &@$);
4050 /*% ripper: defined!($:4) %*/
4051 }
4052 | arg '?' arg '\n'? ':' arg
4053 {
4054 value_expr($1);
4055 $$ = new_if(p, $1, $3, $6, &@$);
4056 fixpos($$, $1);
4057 /*% ripper: ifop!($:1, $:3, $:6) %*/
4058 }
4059 | defn_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
4060 {
4061 endless_method_name(p, $head->nd_mid, &@head);
4062 restore_defun(p, $head);
4063 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4064 ($$ = $head->nd_def)->nd_loc = @$;
4065 RNODE_DEFN($$)->nd_defn = $bodystmt;
4066 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
4067 /*% ripper: def!($:head, $:args, $:$) %*/
4068 local_pop(p);
4069 }
4070 | defs_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
4071 {
4072 endless_method_name(p, $head->nd_mid, &@head);
4073 restore_defun(p, $head);
4074 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4075 ($$ = $head->nd_def)->nd_loc = @$;
4076 RNODE_DEFS($$)->nd_defn = $bodystmt;
4077 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
4078 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
4079 local_pop(p);
4080 }
4081 | primary
4082 {
4083 $$ = $1;
4084 }
4085 ;
4086
4087endless_arg : arg %prec modifier_rescue
4088 | endless_arg modifier_rescue after_rescue arg
4089 {
4090 p->ctxt.in_rescue = $3.in_rescue;
4091 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4092 /*% ripper: rescue_mod!($:1, $:4) %*/
4093 }
4094 | keyword_not '\n'? endless_arg
4095 {
4096 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4097 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4098 }
4099 ;
4100
4101relop : '>' {$$ = '>';}
4102 | '<' {$$ = '<';}
4103 | tGEQ {$$ = idGE;}
4104 | tLEQ {$$ = idLE;}
4105 ;
4106
4107rel_expr : arg relop arg %prec '>'
4108 {
4109 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4110 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4111 }
4112 | rel_expr relop arg %prec '>'
4113 {
4114 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4115 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4116 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4117 }
4118 ;
4119
4120lex_ctxt : none
4121 {
4122 $$ = p->ctxt;
4123 }
4124 ;
4125
4126begin_defined : lex_ctxt
4127 {
4128 p->ctxt.in_defined = 1;
4129 $$ = $1;
4130 }
4131 ;
4132
4133after_rescue : lex_ctxt
4134 {
4135 p->ctxt.in_rescue = after_rescue;
4136 $$ = $1;
4137 }
4138 ;
4139
4140arg_value : arg
4141 {
4142 value_expr($1);
4143 $$ = $1;
4144 }
4145 ;
4146
4147aref_args : none
4148 | args trailer
4149 {
4150 $$ = $1;
4151 }
4152 | args ',' assocs trailer
4153 {
4154 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4155 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4156 }
4157 | assocs trailer
4158 {
4159 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4160 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4161 }
4162 ;
4163
4164arg_rhs : arg %prec tOP_ASGN
4165 {
4166 value_expr($1);
4167 $$ = $1;
4168 }
4169 | arg modifier_rescue after_rescue arg
4170 {
4171 p->ctxt.in_rescue = $3.in_rescue;
4172 value_expr($1);
4173 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4174 /*% ripper: rescue_mod!($:1, $:4) %*/
4175 }
4176 ;
4177
4178paren_args : '(' opt_call_args rparen
4179 {
4180 $$ = $2;
4181 /*% ripper: arg_paren!($:2) %*/
4182 }
4183 | '(' args ',' args_forward rparen
4184 {
4185 if (!check_forwarding_args(p)) {
4186 $$ = 0;
4187 }
4188 else {
4189 $$ = new_args_forward_call(p, $2, &@4, &@$);
4190 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4191 }
4192 }
4193 | '(' args_forward rparen
4194 {
4195 if (!check_forwarding_args(p)) {
4196 $$ = 0;
4197 }
4198 else {
4199 $$ = new_args_forward_call(p, 0, &@2, &@$);
4200 /*% ripper: arg_paren!($:2) %*/
4201 }
4202 }
4203 ;
4204
4205opt_paren_args : none
4206 | paren_args
4207 {
4208 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4209 }
4210 ;
4211
4212opt_call_args : none
4213 | call_args
4214 | args ','
4215 {
4216 $$ = $1;
4217 }
4218 | args ',' assocs ','
4219 {
4220 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4221 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4222 }
4223 | assocs ','
4224 {
4225 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4226 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4227 }
4228 ;
4229
4230call_args : command
4231 {
4232 value_expr($1);
4233 $$ = NEW_LIST($1, &@$);
4234 /*% ripper: args_add!(args_new!, $:1) %*/
4235 }
4236 | args opt_block_arg
4237 {
4238 $$ = arg_blk_pass($1, $2);
4239 /*% ripper: args_add_block!($:1, $:2) %*/
4240 }
4241 | assocs opt_block_arg
4242 {
4243 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4244 $$ = arg_blk_pass($$, $2);
4245 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4246 }
4247 | args ',' assocs opt_block_arg
4248 {
4249 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4250 $$ = arg_blk_pass($$, $4);
4251 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4252 }
4253 | block_arg
4254 /*% ripper: args_add_block!(args_new!, $:1) %*/
4255 ;
4256
4257command_args : {
4258 /* If call_args starts with a open paren '(' or '[',
4259 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4260 * but the push must be done after CMDARG_PUSH(1).
4261 * So this code makes them consistent by first cancelling
4262 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4263 * and finally redoing CMDARG_PUSH(0).
4264 */
4265 int lookahead = 0;
4266 switch (yychar) {
4267 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4268 lookahead = 1;
4269 }
4270 if (lookahead) CMDARG_POP();
4271 CMDARG_PUSH(1);
4272 if (lookahead) CMDARG_PUSH(0);
4273 }
4274 call_args
4275 {
4276 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4277 * but the push must be done after CMDARG_POP() in the parser.
4278 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4279 * CMDARG_POP() to pop 1 pushed by command_args,
4280 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4281 */
4282 int lookahead = 0;
4283 switch (yychar) {
4284 case tLBRACE_ARG:
4285 lookahead = 1;
4286 }
4287 if (lookahead) CMDARG_POP();
4288 CMDARG_POP();
4289 if (lookahead) CMDARG_PUSH(0);
4290 $$ = $2;
4291 /*% ripper: $:2 %*/
4292 }
4293 ;
4294
4295block_arg : tAMPER arg_value
4296 {
4297 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4298 /*% ripper: $:2 %*/
4299 }
4300 | tAMPER
4301 {
4302 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4303 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4304 /*% ripper: Qnil %*/
4305 }
4306 ;
4307
4308opt_block_arg : ',' block_arg
4309 {
4310 $$ = $2;
4311 /*% ripper: $:2 %*/
4312 }
4313 | none
4314 {
4315 $$ = 0;
4316 /*% ripper: Qfalse %*/
4317 }
4318 ;
4319
4320/* value */
4321args : arg_value
4322 {
4323 $$ = NEW_LIST($arg_value, &@$);
4324 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4325 }
4326 | arg_splat
4327 {
4328 $$ = $arg_splat;
4329 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4330 }
4331 | args[non_last_args] ',' arg_value
4332 {
4333 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4334 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4335 }
4336 | args[non_last_args] ',' arg_splat
4337 {
4338 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4339 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4340 }
4341 ;
4342
4343/* value */
4344arg_splat : tSTAR arg_value
4345 {
4346 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4347 /*% ripper: $:arg_value %*/
4348 }
4349 | tSTAR /* none */
4350 {
4351 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4352 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4353 /*% ripper: Qnil %*/
4354 }
4355 ;
4356
4357/* value */
4358mrhs_arg : mrhs
4359 | arg_value
4360 ;
4361
4362/* value */
4363mrhs : args ',' arg_value
4364 {
4365 $$ = last_arg_append(p, $args, $arg_value, &@$);
4366 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4367 }
4368 | args ',' tSTAR arg_value
4369 {
4370 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4371 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4372 }
4373 | tSTAR arg_value
4374 {
4375 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4376 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4377 }
4378 ;
4379
4380%rule %inline inline_primary
4381 : literal
4382 | strings
4383 | xstring
4384 | regexp
4385 | words
4386 | qwords
4387 | symbols
4388 | qsymbols
4389 ;
4390
4391primary : inline_primary
4392 | var_ref
4393 | backref
4394 | tFID
4395 {
4396 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
4397 /*% ripper: method_add_arg!(fcall!($:1), args_new!) %*/
4398 }
4399 | k_begin
4400 {
4401 CMDARG_PUSH(0);
4402 }
4403 bodystmt
4404 k_end
4405 {
4406 CMDARG_POP();
4407 set_line_body($3, @1.end_pos.lineno);
4408 $$ = NEW_BEGIN($3, &@$);
4409 nd_set_line($$, @1.end_pos.lineno);
4410 /*% ripper: begin!($:3) %*/
4411 }
4412 | tLPAREN_ARG compstmt {SET_LEX_STATE(EXPR_ENDARG);} ')'
4413 {
4414 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4415 $$ = $2;
4416 /*% ripper: paren!($:2) %*/
4417 }
4418 | tLPAREN compstmt ')'
4419 {
4420 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4421 $$ = NEW_BLOCK($2, &@$);
4422 /*% ripper: paren!($:2) %*/
4423 }
4424 | primary_value tCOLON2 tCONSTANT
4425 {
4426 $$ = NEW_COLON2($1, $3, &@$);
4427 /*% ripper: const_path_ref!($:1, $:3) %*/
4428 }
4429 | tCOLON3 tCONSTANT
4430 {
4431 $$ = NEW_COLON3($2, &@$);
4432 /*% ripper: top_const_ref!($:2) %*/
4433 }
4434 | tLBRACK aref_args ']'
4435 {
4436 $$ = make_list($2, &@$);
4437 /*% ripper: array!($:2) %*/
4438 }
4439 | tLBRACE assoc_list '}'
4440 {
4441 $$ = new_hash(p, $2, &@$);
4442 RNODE_HASH($$)->nd_brace = TRUE;
4443 /*% ripper: hash!($:2) %*/
4444 }
4445 | k_return
4446 {
4447 $$ = NEW_RETURN(0, &@$, &@1);
4448 /*% ripper: return0! %*/
4449 }
4450 | k_yield '(' call_args rparen
4451 {
4452 $$ = new_yield(p, $3, &@$);
4453 /*% ripper: yield!(paren!($:3)) %*/
4454 }
4455 | k_yield '(' rparen
4456 {
4457 $$ = NEW_YIELD(0, &@$);
4458 /*% ripper: yield!(paren!(args_new!)) %*/
4459 }
4460 | k_yield
4461 {
4462 $$ = NEW_YIELD(0, &@$);
4463 /*% ripper: yield0! %*/
4464 }
4465 | keyword_defined '\n'? '(' begin_defined expr rparen
4466 {
4467 p->ctxt.in_defined = $4.in_defined;
4468 $$ = new_defined(p, $5, &@$);
4469 /*% ripper: defined!($:5) %*/
4470 }
4471 | keyword_not '(' expr rparen
4472 {
4473 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4474 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4475 }
4476 | keyword_not '(' rparen
4477 {
4478 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
4479 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4480 }
4481 | fcall brace_block
4482 {
4483 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
4484 /*% ripper: method_add_block!(method_add_arg!(fcall!($:1), args_new!), $:2) %*/
4485 }
4486 | method_call
4487 | method_call brace_block
4488 {
4489 block_dup_check(p, get_nd_args(p, $1), $2);
4490 $$ = method_add_block(p, $1, $2, &@$);
4491 /*% ripper: method_add_block!($:1, $:2) %*/
4492 }
4493 | lambda
4494 | k_if expr_value then
4495 compstmt
4496 if_tail
4497 k_end
4498 {
4499 $$ = new_if(p, $2, $4, $5, &@$);
4500 fixpos($$, $2);
4501 /*% ripper: if!($:2, $:4, $:5) %*/
4502 }
4503 | k_unless expr_value then
4504 compstmt
4505 opt_else
4506 k_end
4507 {
4508 $$ = new_unless(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4509 fixpos($$, $2);
4510 /*% ripper: unless!($:2, $:4, $:5) %*/
4511 }
4512 | k_while expr_value_do
4513 compstmt
4514 k_end
4515 {
4516 restore_block_exit(p, $1);
4517 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4518 fixpos($$, $2);
4519 /*% ripper: while!($:2, $:3) %*/
4520 }
4521 | k_until expr_value_do
4522 compstmt
4523 k_end
4524 {
4525 restore_block_exit(p, $1);
4526 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4527 fixpos($$, $2);
4528 /*% ripper: until!($:2, $:3) %*/
4529 }
4530 | k_case expr_value terms?
4531 {
4532 $$ = p->case_labels;
4533 p->case_labels = CHECK_LITERAL_WHEN;
4534 }<labels>
4535 case_body
4536 k_end
4537 {
4538 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4539 p->case_labels = $4;
4540 $$ = NEW_CASE($2, $5, &@$, &@1, &@6);
4541 fixpos($$, $2);
4542 /*% ripper: case!($:2, $:5) %*/
4543 }
4544 | k_case terms?
4545 {
4546 $$ = p->case_labels;
4547 p->case_labels = 0;
4548 }<labels>
4549 case_body
4550 k_end
4551 {
4552 if (p->case_labels) st_free_table(p->case_labels);
4553 p->case_labels = $3;
4554 $$ = NEW_CASE2($4, &@$, &@1, &@5);
4555 /*% ripper: case!(Qnil, $:4) %*/
4556 }
4557 | k_case expr_value terms?
4558 p_case_body
4559 k_end
4560 {
4561 $$ = NEW_CASE3($2, $4, &@$, &@1, &@5);
4562 /*% ripper: case!($:2, $:4) %*/
4563 }
4564 | k_for for_var keyword_in expr_value_do
4565 compstmt
4566 k_end
4567 {
4568 restore_block_exit(p, $1);
4569 /*
4570 * for a, b, c in e
4571 * #=>
4572 * e.each{|*x| a, b, c = x}
4573 *
4574 * for a in e
4575 * #=>
4576 * e.each{|x| a, = x}
4577 */
4578 ID id = internal_id(p);
4579 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4580 rb_node_args_t *args;
4581 NODE *scope, *internal_var = NEW_DVAR(id, &@2);
4582 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4583 tbl->ids[0] = id; /* internal id */
4584
4585 switch (nd_type($2)) {
4586 case NODE_LASGN:
4587 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4588 set_nd_value(p, $2, internal_var);
4589 id = 0;
4590 m->nd_plen = 1;
4591 m->nd_next = $2;
4592 break;
4593 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4594 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
4595 break;
4596 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4597 m->nd_next = node_assign(p, (NODE *)NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
4598 }
4599 /* {|*internal_id| <m> = internal_id; ... } */
4600 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
4601 scope = NEW_SCOPE2(tbl, args, $5, &@$);
4602 $$ = NEW_FOR($4, scope, &@$);
4603 fixpos($$, $2);
4604 /*% ripper: for!($:2, $:4, $:5) %*/
4605 }
4606 | k_class cpath superclass
4607 {
4608 begin_definition("class", &@k_class, &@cpath);
4609 }
4610 bodystmt
4611 k_end
4612 {
4613 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$);
4614 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4615 set_line_body($bodystmt, @superclass.end_pos.lineno);
4616 nd_set_line($$, @superclass.end_pos.lineno);
4617 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4618 local_pop(p);
4619 p->ctxt.in_class = $k_class.in_class;
4620 p->ctxt.cant_return = $k_class.cant_return;
4621 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4622 }
4623 | k_class tLSHFT expr_value
4624 {
4625 begin_definition("", &@k_class, &@tLSHFT);
4626 }
4627 term
4628 bodystmt
4629 k_end
4630 {
4631 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$);
4632 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4633 set_line_body($bodystmt, nd_line($expr_value));
4634 fixpos($$, $expr_value);
4635 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4636 local_pop(p);
4637 p->ctxt.in_def = $k_class.in_def;
4638 p->ctxt.in_class = $k_class.in_class;
4639 p->ctxt.cant_return = $k_class.cant_return;
4640 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4641 }
4642 | k_module cpath
4643 {
4644 begin_definition("module", &@k_module, &@cpath);
4645 }
4646 bodystmt
4647 k_end
4648 {
4649 $$ = NEW_MODULE($cpath, $bodystmt, &@$);
4650 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4651 set_line_body($bodystmt, @cpath.end_pos.lineno);
4652 nd_set_line($$, @cpath.end_pos.lineno);
4653 /*% ripper: module!($:cpath, $:bodystmt) %*/
4654 local_pop(p);
4655 p->ctxt.in_class = $k_module.in_class;
4656 p->ctxt.cant_return = $k_module.cant_return;
4657 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4658 }
4659 | defn_head[head]
4660 f_arglist[args]
4661 {
4662 push_end_expect_token_locations(p, &@head.beg_pos);
4663 }
4664 bodystmt
4665 k_end
4666 {
4667 restore_defun(p, $head);
4668 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4669 ($$ = $head->nd_def)->nd_loc = @$;
4670 RNODE_DEFN($$)->nd_defn = $bodystmt;
4671 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4672 local_pop(p);
4673 }
4674 | defs_head[head]
4675 f_arglist[args]
4676 {
4677 push_end_expect_token_locations(p, &@head.beg_pos);
4678 }
4679 bodystmt
4680 k_end
4681 {
4682 restore_defun(p, $head);
4683 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4684 ($$ = $head->nd_def)->nd_loc = @$;
4685 RNODE_DEFS($$)->nd_defn = $bodystmt;
4686 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4687 local_pop(p);
4688 }
4689 | keyword_break
4690 {
4691 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@1));
4692 /*% ripper: break!(args_new!) %*/
4693 }
4694 | keyword_next
4695 {
4696 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@1));
4697 /*% ripper: next!(args_new!) %*/
4698 }
4699 | keyword_redo
4700 {
4701 $$ = add_block_exit(p, NEW_REDO(&@$, &@1));
4702 /*% ripper: redo! %*/
4703 }
4704 | keyword_retry
4705 {
4706 if (!p->ctxt.in_defined) {
4707 switch (p->ctxt.in_rescue) {
4708 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4709 case after_rescue: /* ok */ break;
4710 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4711 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4712 }
4713 }
4714 $$ = NEW_RETRY(&@$);
4715 /*% ripper: retry! %*/
4716 }
4717 ;
4718
4719primary_value : primary
4720 {
4721 value_expr($1);
4722 $$ = $1;
4723 }
4724 ;
4725
4726k_begin : keyword_begin
4727 {
4728 token_info_push(p, "begin", &@$);
4729 push_end_expect_token_locations(p, &@1.beg_pos);
4730 }
4731 ;
4732
4733k_if : keyword_if
4734 {
4735 WARN_EOL("if");
4736 token_info_push(p, "if", &@$);
4737 if (p->token_info && p->token_info->nonspc &&
4738 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4739 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4740 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4741 beg += rb_strlen_lit("else");
4742 while (beg < tok && ISSPACE(*beg)) beg++;
4743 if (beg == tok) {
4744 p->token_info->nonspc = 0;
4745 }
4746 }
4747 push_end_expect_token_locations(p, &@1.beg_pos);
4748 }
4749 ;
4750
4751k_unless : keyword_unless
4752 {
4753 token_info_push(p, "unless", &@$);
4754 push_end_expect_token_locations(p, &@1.beg_pos);
4755 }
4756 ;
4757
4758k_while : keyword_while allow_exits
4759 {
4760 $$ = $allow_exits;
4761 token_info_push(p, "while", &@$);
4762 push_end_expect_token_locations(p, &@1.beg_pos);
4763 }
4764 ;
4765
4766k_until : keyword_until allow_exits
4767 {
4768 $$ = $allow_exits;
4769 token_info_push(p, "until", &@$);
4770 push_end_expect_token_locations(p, &@1.beg_pos);
4771 }
4772 ;
4773
4774k_case : keyword_case
4775 {
4776 token_info_push(p, "case", &@$);
4777 push_end_expect_token_locations(p, &@1.beg_pos);
4778 }
4779 ;
4780
4781k_for : keyword_for allow_exits
4782 {
4783 $$ = $allow_exits;
4784 token_info_push(p, "for", &@$);
4785 push_end_expect_token_locations(p, &@1.beg_pos);
4786 }
4787 ;
4788
4789k_class : keyword_class
4790 {
4791 token_info_push(p, "class", &@$);
4792 $$ = p->ctxt;
4793 p->ctxt.in_rescue = before_rescue;
4794 push_end_expect_token_locations(p, &@1.beg_pos);
4795 }
4796 ;
4797
4798k_module : keyword_module
4799 {
4800 token_info_push(p, "module", &@$);
4801 $$ = p->ctxt;
4802 p->ctxt.in_rescue = before_rescue;
4803 push_end_expect_token_locations(p, &@1.beg_pos);
4804 }
4805 ;
4806
4807k_def : keyword_def
4808 {
4809 token_info_push(p, "def", &@$);
4810 $$ = NEW_DEF_TEMP(&@$);
4811 p->ctxt.in_argdef = 1;
4812 }
4813 ;
4814
4815k_do : keyword_do
4816 {
4817 token_info_push(p, "do", &@$);
4818 push_end_expect_token_locations(p, &@1.beg_pos);
4819 }
4820 ;
4821
4822k_do_block : keyword_do_block
4823 {
4824 token_info_push(p, "do", &@$);
4825 push_end_expect_token_locations(p, &@1.beg_pos);
4826 }
4827 ;
4828
4829k_rescue : keyword_rescue
4830 {
4831 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4832 $$ = p->ctxt;
4833 p->ctxt.in_rescue = after_rescue;
4834 }
4835 ;
4836
4837k_ensure : keyword_ensure
4838 {
4839 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4840 $$ = p->ctxt;
4841 }
4842 ;
4843
4844k_when : keyword_when
4845 {
4846 token_info_warn(p, "when", p->token_info, 0, &@$);
4847 }
4848 ;
4849
4850k_else : keyword_else
4851 {
4852 token_info *ptinfo_beg = p->token_info;
4853 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4854 token_info_warn(p, "else", p->token_info, same, &@$);
4855 if (same) {
4856 token_info e;
4857 e.next = ptinfo_beg->next;
4858 e.token = "else";
4859 token_info_setup(&e, p->lex.pbeg, &@$);
4860 if (!e.nonspc) *ptinfo_beg = e;
4861 }
4862 }
4863 ;
4864
4865k_elsif : keyword_elsif
4866 {
4867 WARN_EOL("elsif");
4868 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4869 }
4870 ;
4871
4872k_end : keyword_end
4873 {
4874 token_info_pop(p, "end", &@$);
4875 pop_end_expect_token_locations(p);
4876 }
4877 | tDUMNY_END
4878 {
4879 compile_error(p, "syntax error, unexpected end-of-input");
4880 }
4881 ;
4882
4883k_return : keyword_return
4884 {
4885 if (p->ctxt.cant_return && !dyna_in_block(p))
4886 yyerror1(&@1, "Invalid return in class/module body");
4887 }
4888 ;
4889
4890k_yield : keyword_yield
4891 {
4892 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4893 yyerror1(&@1, "Invalid yield");
4894 }
4895 ;
4896
4897then : term
4898 | keyword_then
4899 | term keyword_then
4900 ;
4901
4902do : term
4903 | keyword_do_cond
4904 ;
4905
4906if_tail : opt_else
4907 | k_elsif expr_value then
4908 compstmt
4909 if_tail
4910 {
4911 $$ = new_if(p, $2, $4, $5, &@$);
4912 fixpos($$, $2);
4913 /*% ripper: elsif!($:2, $:4, $:5) %*/
4914 }
4915 ;
4916
4917opt_else : none
4918 | k_else compstmt
4919 {
4920 $$ = $2;
4921 /*% ripper: else!($:2) %*/
4922 }
4923 ;
4924
4925for_var : lhs
4926 | mlhs
4927 ;
4928
4929f_marg : f_norm_arg
4930 {
4931 $$ = assignable(p, $1, 0, &@$);
4932 mark_lvar_used(p, $$);
4933 }
4934 | tLPAREN f_margs rparen
4935 {
4936 $$ = (NODE *)$2;
4937 /*% ripper: mlhs_paren!($:2) %*/
4938 }
4939 ;
4940
4941f_marg_list : f_marg
4942 {
4943 $$ = NEW_LIST($1, &@$);
4944 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
4945 }
4946 | f_marg_list ',' f_marg
4947 {
4948 $$ = list_append(p, $1, $3);
4949 /*% ripper: mlhs_add!($:1, $:3) %*/
4950 }
4951 ;
4952
4953f_margs : f_marg_list
4954 {
4955 $$ = NEW_MASGN($1, 0, &@$);
4956 /*% ripper: $:1 %*/
4957 }
4958 | f_marg_list ',' f_rest_marg
4959 {
4960 $$ = NEW_MASGN($1, $3, &@$);
4961 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4962 }
4963 | f_marg_list ',' f_rest_marg ',' f_marg_list
4964 {
4965 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4966 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4967 }
4968 | f_rest_marg
4969 {
4970 $$ = NEW_MASGN(0, $1, &@$);
4971 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4972 }
4973 | f_rest_marg ',' f_marg_list
4974 {
4975 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4976 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4977 }
4978 ;
4979
4980f_rest_marg : tSTAR f_norm_arg
4981 {
4982 /*% ripper: $:2 %*/
4983 $$ = assignable(p, $2, 0, &@$);
4984 mark_lvar_used(p, $$);
4985 }
4986 | tSTAR
4987 {
4988 $$ = NODE_SPECIAL_NO_NAME_REST;
4989 /*% ripper: Qnil %*/
4990 }
4991 ;
4992
4993f_any_kwrest : f_kwrest
4994 | f_no_kwarg
4995 {
4996 $$ = idNil;
4997 /*% ripper: ID2VAL(idNil) %*/
4998 }
4999 ;
5000
5001f_eq : {p->ctxt.in_argdef = 0;} '=';
5002
5003block_args_tail : f_kwarg(f_block_kw) ',' f_kwrest opt_f_block_arg
5004 {
5005 $$ = new_args_tail(p, $1, $3, $4, &@3);
5006 /*% ripper: [$:1, $:3, $:4] %*/
5007 }
5008 | f_kwarg(f_block_kw) opt_f_block_arg
5009 {
5010 $$ = new_args_tail(p, $1, 0, $2, &@1);
5011 /*% ripper: [$:1, Qnil, $:2] %*/
5012 }
5013 | f_any_kwrest opt_f_block_arg
5014 {
5015 $$ = new_args_tail(p, 0, $1, $2, &@1);
5016 /*% ripper: [Qnil, $:1, $:2] %*/
5017 }
5018 | f_block_arg
5019 {
5020 $$ = new_args_tail(p, 0, 0, $1, &@1);
5021 /*% ripper: [Qnil, Qnil, $:1] %*/
5022 }
5023 ;
5024
5025excessed_comma : ','
5026 {
5027 /* magic number for rest_id in iseq_set_arguments() */
5028 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
5029 /*% ripper: excessed_comma! %*/
5030 }
5031 ;
5032
5033block_param : f_arg ',' f_optarg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5034 {
5035 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
5036 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
5037 }
5038 | f_arg ',' f_optarg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5039 {
5040 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
5041 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
5042 }
5043 | f_arg ',' f_optarg(primary_value) opt_args_tail(block_args_tail)
5044 {
5045 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
5046 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
5047 }
5048 | f_arg ',' f_optarg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5049 {
5050 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
5051 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
5052 }
5053 | f_arg ',' f_rest_arg opt_args_tail(block_args_tail)
5054 {
5055 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
5056 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
5057 }
5058 | f_arg excessed_comma
5059 {
5060 $$ = new_args_tail(p, 0, 0, 0, &@2);
5061 $$ = new_args(p, $1, 0, $2, 0, $$, &@$);
5062 /*% ripper: params!($:1, Qnil, $:2, Qnil, Qnil, Qnil, Qnil) %*/
5063 }
5064 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5065 {
5066 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
5067 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
5068 }
5069 | f_arg opt_args_tail(block_args_tail)
5070 {
5071 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
5072 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
5073 }
5074 | f_optarg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5075 {
5076 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
5077 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
5078 }
5079 | f_optarg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5080 {
5081 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
5082 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
5083 }
5084 | f_optarg(primary_value) opt_args_tail(block_args_tail)
5085 {
5086 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
5087 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
5088 }
5089 | f_optarg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5090 {
5091 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
5092 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
5093 }
5094 | f_rest_arg opt_args_tail(block_args_tail)
5095 {
5096 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
5097 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
5098 }
5099 | f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5100 {
5101 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
5102 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
5103 }
5104 | block_args_tail
5105 {
5106 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
5107 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
5108 }
5109 ;
5110
5111opt_block_param : none
5112 | block_param_def
5113 {
5114 p->command_start = TRUE;
5115 }
5116 ;
5117
5118block_param_def : '|' opt_bv_decl '|'
5119 {
5120 p->max_numparam = ORDINAL_PARAM;
5121 p->ctxt.in_argdef = 0;
5122 $$ = 0;
5123 /*% ripper: block_var!(params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), $:2) %*/
5124 }
5125 | '|' block_param opt_bv_decl '|'
5126 {
5127 p->max_numparam = ORDINAL_PARAM;
5128 p->ctxt.in_argdef = 0;
5129 $$ = $2;
5130 /*% ripper: block_var!($:2, $:3) %*/
5131 }
5132 ;
5133
5134
5135opt_bv_decl : '\n'?
5136 {
5137 $$ = 0;
5138 /*% ripper: Qfalse %*/
5139 }
5140 | '\n'? ';' bv_decls '\n'?
5141 {
5142 $$ = 0;
5143 /*% ripper: $:3 %*/
5144 }
5145 ;
5146
5147bv_decls : bvar
5148 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5149 | bv_decls ',' bvar
5150 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5151 ;
5152
5153bvar : tIDENTIFIER
5154 {
5155 new_bv(p, $1);
5156 /*% ripper: $:1 %*/
5157 }
5158 | f_bad_arg
5159 {
5160 $$ = 0;
5161 }
5162 ;
5163
5164max_numparam : {
5165 $$ = p->max_numparam;
5166 p->max_numparam = 0;
5167 }
5168 ;
5169
5170numparam : {
5171 $$ = numparam_push(p);
5172 }
5173 ;
5174
5175it_id : {
5176 $$ = p->it_id;
5177 p->it_id = 0;
5178 }
5179 ;
5180
5181lambda : tLAMBDA[lpar]
5182 {
5183 token_info_push(p, "->", &@1);
5184 $$ = dyna_push(p);
5185 }[dyna]<vars>
5186 max_numparam numparam it_id allow_exits
5187 f_larglist[args]
5188 {
5189 CMDARG_PUSH(0);
5190 }
5191 lambda_body[body]
5192 {
5193 int max_numparam = p->max_numparam;
5194 ID it_id = p->it_id;
5195 p->lex.lpar_beg = $lpar;
5196 p->max_numparam = $max_numparam;
5197 p->it_id = $it_id;
5198 restore_block_exit(p, $allow_exits);
5199 CMDARG_POP();
5200 $args = args_with_numbered(p, $args, max_numparam, it_id);
5201 {
5202 YYLTYPE loc = code_loc_gen(&@args, &@body);
5203 $$ = NEW_LAMBDA($args, $body, &loc);
5204 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5205 nd_set_line($$, @args.end_pos.lineno);
5206 nd_set_first_loc($$, @1.beg_pos);
5207 }
5208 /*% ripper: lambda!($:args, $:body) %*/
5209 numparam_pop(p, $numparam);
5210 dyna_pop(p, $dyna);
5211 }
5212 ;
5213
5214f_larglist : '(' f_args opt_bv_decl ')'
5215 {
5216 p->ctxt.in_argdef = 0;
5217 $$ = $f_args;
5218 p->max_numparam = ORDINAL_PARAM;
5219 /*% ripper: paren!($:2) %*/
5220 }
5221 | f_args
5222 {
5223 p->ctxt.in_argdef = 0;
5224 if (!args_info_empty_p(&$1->nd_ainfo))
5225 p->max_numparam = ORDINAL_PARAM;
5226 $$ = $f_args;
5227 }
5228 ;
5229
5230lambda_body : tLAMBEG compstmt '}'
5231 {
5232 token_info_pop(p, "}", &@3);
5233 $$ = $2;
5234 /*% ripper: $:2 %*/
5235 }
5236 | keyword_do_LAMBDA
5237 {
5238 push_end_expect_token_locations(p, &@1.beg_pos);
5239 }
5240 bodystmt k_end
5241 {
5242 $$ = $3;
5243 /*% ripper: $:3 %*/
5244 }
5245 ;
5246
5247do_block : k_do_block do_body k_end
5248 {
5249 $$ = $2;
5250 set_embraced_location($$, &@1, &@3);
5251 /*% ripper: $:2 %*/
5252 }
5253 ;
5254
5255block_call : command do_block
5256 {
5257 if (nd_type_p($1, NODE_YIELD)) {
5258 compile_error(p, "block given to yield");
5259 }
5260 else {
5261 block_dup_check(p, get_nd_args(p, $1), $2);
5262 }
5263 $$ = method_add_block(p, $1, $2, &@$);
5264 fixpos($$, $1);
5265 /*% ripper: method_add_block!($:1, $:2) %*/
5266 }
5267 | block_call call_op2 operation2 opt_paren_args
5268 {
5269 bool has_args = $4 != 0;
5270 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5271 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5272 /*% ripper: call!($:1, $:2, $:3) %*/
5273 if (has_args) {
5274 /*% ripper: method_add_arg!($:$, $:4) %*/
5275 }
5276 }
5277 | block_call call_op2 operation2 opt_paren_args brace_block
5278 {
5279 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5280 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5281 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
5282 if ($5) {
5283 /*% ripper: method_add_block!($:$, $:5) %*/
5284 }
5285 }
5286 | block_call call_op2 operation2 command_args do_block
5287 {
5288 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5289 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5290 }
5291 ;
5292
5293method_call : fcall paren_args
5294 {
5295 $1->nd_args = $2;
5296 $$ = (NODE *)$1;
5297 nd_set_last_loc($1, @2.end_pos);
5298 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5299 }
5300 | primary_value call_op operation2 opt_paren_args
5301 {
5302 bool has_args = $4 != 0;
5303 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5304 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5305 nd_set_line($$, @3.end_pos.lineno);
5306 /*% ripper: call!($:1, $:2, $:3) %*/
5307 if (has_args) {
5308 /*% ripper: method_add_arg!($:$, $:4) %*/
5309 }
5310 }
5311 | primary_value tCOLON2 operation2 paren_args
5312 {
5313 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5314 nd_set_line($$, @3.end_pos.lineno);
5315 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5316 }
5317 | primary_value tCOLON2 operation3
5318 {
5319 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5320 /*% ripper: call!($:1, $:2, $:3) %*/
5321 }
5322 | primary_value call_op paren_args
5323 {
5324 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5325 nd_set_line($$, @2.end_pos.lineno);
5326 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5327 }
5328 | primary_value tCOLON2 paren_args
5329 {
5330 $$ = new_qcall(p, idCOLON2, $1, idCall, $3, &@2, &@$);
5331 nd_set_line($$, @2.end_pos.lineno);
5332 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5333 }
5334 | keyword_super paren_args
5335 {
5336 $$ = NEW_SUPER($2, &@$);
5337 /*% ripper: super!($:2) %*/
5338 }
5339 | keyword_super
5340 {
5341 $$ = NEW_ZSUPER(&@$);
5342 /*% ripper: zsuper! %*/
5343 }
5344 | primary_value '[' opt_call_args rbracket
5345 {
5346 $$ = NEW_CALL($1, tAREF, $3, &@$);
5347 fixpos($$, $1);
5348 /*% ripper: aref!($:1, $:3) %*/
5349 }
5350 ;
5351
5352brace_block : '{' brace_body '}'
5353 {
5354 $$ = $2;
5355 set_embraced_location($$, &@1, &@3);
5356 /*% ripper: $:2 %*/
5357 }
5358 | k_do do_body k_end
5359 {
5360 $$ = $2;
5361 set_embraced_location($$, &@1, &@3);
5362 /*% ripper: $:2 %*/
5363 }
5364 ;
5365
5366brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5367 max_numparam numparam it_id allow_exits
5368 opt_block_param[args] compstmt
5369 {
5370 int max_numparam = p->max_numparam;
5371 ID it_id = p->it_id;
5372 p->max_numparam = $max_numparam;
5373 p->it_id = $it_id;
5374 $args = args_with_numbered(p, $args, max_numparam, it_id);
5375 $$ = NEW_ITER($args, $compstmt, &@$);
5376 /*% ripper: brace_block!($:args, $:compstmt) %*/
5377 restore_block_exit(p, $allow_exits);
5378 numparam_pop(p, $numparam);
5379 dyna_pop(p, $dyna);
5380 }
5381 ;
5382
5383do_body : {
5384 $$ = dyna_push(p);
5385 CMDARG_PUSH(0);
5386 }[dyna]<vars>
5387 max_numparam numparam it_id allow_exits
5388 opt_block_param[args] bodystmt
5389 {
5390 int max_numparam = p->max_numparam;
5391 ID it_id = p->it_id;
5392 p->max_numparam = $max_numparam;
5393 p->it_id = $it_id;
5394 $args = args_with_numbered(p, $args, max_numparam, it_id);
5395 $$ = NEW_ITER($args, $bodystmt, &@$);
5396 /*% ripper: do_block!($:args, $:bodystmt) %*/
5397 CMDARG_POP();
5398 restore_block_exit(p, $allow_exits);
5399 numparam_pop(p, $numparam);
5400 dyna_pop(p, $dyna);
5401 }
5402 ;
5403
5404case_args : arg_value
5405 {
5406 check_literal_when(p, $arg_value, &@arg_value);
5407 $$ = NEW_LIST($arg_value, &@$);
5408 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5409 }
5410 | tSTAR arg_value
5411 {
5412 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5413 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5414 }
5415 | case_args[non_last_args] ',' arg_value
5416 {
5417 check_literal_when(p, $arg_value, &@arg_value);
5418 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5419 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5420 }
5421 | case_args[non_last_args] ',' tSTAR arg_value
5422 {
5423 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5424 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5425 }
5426 ;
5427
5428case_body : k_when case_args then
5429 compstmt
5430 cases
5431 {
5432 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5433 fixpos($$, $2);
5434 /*% ripper: when!($:2, $:4, $:5) %*/
5435 }
5436 ;
5437
5438cases : opt_else
5439 | case_body
5440 ;
5441
5442p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5443p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5444
5445p_in_kwarg : {
5446 $$ = p->ctxt;
5447 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5448 p->command_start = FALSE;
5449 p->ctxt.in_kwarg = 1;
5450 }
5451 ;
5452
5453p_case_body : keyword_in
5454 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5455 p_top_expr[expr] then
5456 {
5457 pop_pktbl(p, $p_pktbl);
5458 pop_pvtbl(p, $p_pvtbl);
5459 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5460 }
5461 compstmt
5462 p_cases[cases]
5463 {
5464 $$ = NEW_IN($expr, $compstmt, $cases, &@$);
5465 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5466 }
5467 ;
5468
5469p_cases : opt_else
5470 | p_case_body
5471 ;
5472
5473p_top_expr : p_top_expr_body
5474 | p_top_expr_body modifier_if expr_value
5475 {
5476 $$ = new_if(p, $3, $1, 0, &@$);
5477 fixpos($$, $3);
5478 /*% ripper: if_mod!($:3, $:1) %*/
5479 }
5480 | p_top_expr_body modifier_unless expr_value
5481 {
5482 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5483 fixpos($$, $3);
5484 /*% ripper: unless_mod!($:3, $:1) %*/
5485 }
5486 ;
5487
5488p_top_expr_body : p_expr
5489 | p_expr ','
5490 {
5491 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5492 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5493 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5494 }
5495 | p_expr ',' p_args
5496 {
5497 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5498 nd_set_first_loc($$, @1.beg_pos);
5499 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5500 }
5501 | p_find
5502 {
5503 $$ = new_find_pattern(p, 0, $1, &@$);
5504 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5505 }
5506 | p_args_tail
5507 {
5508 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5509 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5510 }
5511 | p_kwargs
5512 {
5513 $$ = new_hash_pattern(p, 0, $1, &@$);
5514 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5515 }
5516 ;
5517
5518p_expr : p_as
5519 ;
5520
5521p_as : p_expr tASSOC p_variable
5522 {
5523 NODE *n = NEW_LIST($1, &@$);
5524 n = list_append(p, n, $3);
5525 $$ = new_hash(p, n, &@$);
5526 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5527 }
5528 | p_alt
5529 ;
5530
5531p_alt : p_alt '|' p_expr_basic
5532 {
5533 $$ = NEW_OR($1, $3, &@$, &@2);
5534 /*% ripper: binary!($:1, ID2VAL(idOr), $:3) %*/
5535 }
5536 | p_expr_basic
5537 ;
5538
5539p_lparen : '(' p_pktbl
5540 {
5541 $$ = $2;
5542 /*% ripper: $:2 %*/
5543 }
5544 ;
5545
5546p_lbracket : '[' p_pktbl
5547 {
5548 $$ = $2;
5549 /*% ripper: $:2 %*/
5550 }
5551 ;
5552
5553p_expr_basic : p_value
5554 | p_variable
5555 | p_const p_lparen[p_pktbl] p_args rparen
5556 {
5557 pop_pktbl(p, $p_pktbl);
5558 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5559 nd_set_first_loc($$, @p_const.beg_pos);
5560 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5561 }
5562 | p_const p_lparen[p_pktbl] p_find rparen
5563 {
5564 pop_pktbl(p, $p_pktbl);
5565 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5566 nd_set_first_loc($$, @p_const.beg_pos);
5567 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5568 }
5569 | p_const p_lparen[p_pktbl] p_kwargs rparen
5570 {
5571 pop_pktbl(p, $p_pktbl);
5572 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5573 nd_set_first_loc($$, @p_const.beg_pos);
5574 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5575 }
5576 | p_const '(' rparen
5577 {
5578 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5579 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5580 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5581 }
5582 | p_const p_lbracket[p_pktbl] p_args rbracket
5583 {
5584 pop_pktbl(p, $p_pktbl);
5585 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5586 nd_set_first_loc($$, @p_const.beg_pos);
5587 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5588 }
5589 | p_const p_lbracket[p_pktbl] p_find rbracket
5590 {
5591 pop_pktbl(p, $p_pktbl);
5592 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5593 nd_set_first_loc($$, @p_const.beg_pos);
5594 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5595 }
5596 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5597 {
5598 pop_pktbl(p, $p_pktbl);
5599 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5600 nd_set_first_loc($$, @p_const.beg_pos);
5601 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5602 }
5603 | p_const '[' rbracket
5604 {
5605 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5606 $$ = new_array_pattern(p, $1, 0, $$, &@$);
5607 /*% ripper: aryptn!($:1, Qnil, Qnil, Qnil) %*/
5608 }
5609 | tLBRACK p_args rbracket
5610 {
5611 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5612 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5613 }
5614 | tLBRACK p_find rbracket
5615 {
5616 $$ = new_find_pattern(p, 0, $p_find, &@$);
5617 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5618 }
5619 | tLBRACK rbracket
5620 {
5621 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5622 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5623 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5624 }
5625 | tLBRACE p_pktbl lex_ctxt[ctxt]
5626 {
5627 p->ctxt.in_kwarg = 0;
5628 }
5629 p_kwargs rbrace
5630 {
5631 pop_pktbl(p, $p_pktbl);
5632 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5633 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5634 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5635 }
5636 | tLBRACE rbrace
5637 {
5638 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5639 $$ = new_hash_pattern(p, 0, $$, &@$);
5640 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5641 }
5642 | tLPAREN p_pktbl p_expr rparen
5643 {
5644 pop_pktbl(p, $p_pktbl);
5645 $$ = $p_expr;
5646 /*% ripper: $:p_expr %*/
5647 }
5648 ;
5649
5650p_args : p_expr
5651 {
5652 NODE *pre_args = NEW_LIST($1, &@$);
5653 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5654 /*% ripper: [[$:1], Qnil, Qnil] %*/
5655 }
5656 | p_args_head
5657 {
5658 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5659 /*% ripper: [$:1, Qnil, Qnil] %*/
5660 }
5661 | p_args_head p_arg
5662 {
5663 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5664 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5665 }
5666 | p_args_head p_rest
5667 {
5668 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5669 /*% ripper: [$:1, $:2, Qnil] %*/
5670 }
5671 | p_args_head p_rest ',' p_args_post
5672 {
5673 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5674 /*% ripper: [$:1, $:2, $:4] %*/
5675 }
5676 | p_args_tail
5677 ;
5678
5679p_args_head : p_arg ','
5680 {
5681 $$ = $1;
5682 }
5683 | p_args_head p_arg ','
5684 {
5685 $$ = list_concat($1, $2);
5686 /*% ripper: rb_ary_concat($:1, $:2) %*/
5687 }
5688 ;
5689
5690p_args_tail : p_rest
5691 {
5692 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5693 /*% ripper: [Qnil, $:1, Qnil] %*/
5694 }
5695 | p_rest ',' p_args_post
5696 {
5697 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5698 /*% ripper: [Qnil, $:1, $:3] %*/
5699 }
5700 ;
5701
5702p_find : p_rest ',' p_args_post ',' p_rest
5703 {
5704 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5705 /*% ripper: [$:1, $:3, $:5] %*/
5706 }
5707 ;
5708
5709
5710p_rest : tSTAR tIDENTIFIER
5711 {
5712 error_duplicate_pattern_variable(p, $2, &@2);
5713 /*% ripper: var_field!($:2) %*/
5714 $$ = assignable(p, $2, 0, &@$);
5715 }
5716 | tSTAR
5717 {
5718 $$ = 0;
5719 /*% ripper: var_field!(Qnil) %*/
5720 }
5721 ;
5722
5723p_args_post : p_arg
5724 | p_args_post ',' p_arg
5725 {
5726 $$ = list_concat($1, $3);
5727 /*% ripper: rb_ary_concat($:1, $:3) %*/
5728 }
5729 ;
5730
5731p_arg : p_expr
5732 {
5733 $$ = NEW_LIST($1, &@$);
5734 /*% ripper: [$:1] %*/
5735 }
5736 ;
5737
5738p_kwargs : p_kwarg ',' p_any_kwrest
5739 {
5740 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5741 /*% ripper: [$:1, $:3] %*/
5742 }
5743 | p_kwarg
5744 {
5745 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5746 /*% ripper: [$:1, Qnil] %*/
5747 }
5748 | p_kwarg ','
5749 {
5750 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5751 /*% ripper: [$:1, Qnil] %*/
5752 }
5753 | p_any_kwrest
5754 {
5755 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5756 /*% ripper: [[], $:1] %*/
5757 }
5758 ;
5759
5760p_kwarg : p_kw
5761 /*% ripper[brace]: [$:1] %*/
5762 | p_kwarg ',' p_kw
5763 {
5764 $$ = list_concat($1, $3);
5765 /*% ripper: rb_ary_push($:1, $:3) %*/
5766 }
5767 ;
5768
5769p_kw : p_kw_label p_expr
5770 {
5771 error_duplicate_pattern_key(p, $1, &@1);
5772 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5773 /*% ripper: [$:1, $:2] %*/
5774 }
5775 | p_kw_label
5776 {
5777 error_duplicate_pattern_key(p, $1, &@1);
5778 if ($1 && !is_local_id($1)) {
5779 yyerror1(&@1, "key must be valid as local variables");
5780 }
5781 error_duplicate_pattern_variable(p, $1, &@1);
5782 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5783 /*% ripper: [$:1, Qnil] %*/
5784 }
5785 ;
5786
5787p_kw_label : tLABEL
5788 | tSTRING_BEG string_contents tLABEL_END
5789 {
5790 YYLTYPE loc = code_loc_gen(&@1, &@3);
5791 if (!$2 || nd_type_p($2, NODE_STR)) {
5792 NODE *node = dsym_node(p, $2, &loc);
5793 $$ = rb_sym2id(rb_node_sym_string_val(node));
5794 }
5795 else {
5796 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5797 $$ = rb_intern_str(STR_NEW0());
5798 }
5799 /*% ripper: $:2 %*/
5800 }
5801 ;
5802
5803p_kwrest : kwrest_mark tIDENTIFIER
5804 {
5805 $$ = $2;
5806 /*% ripper: var_field!($:2) %*/
5807 }
5808 | kwrest_mark
5809 {
5810 $$ = 0;
5811 /*% ripper: Qnil %*/
5812 }
5813 ;
5814
5815p_kwnorest : kwrest_mark keyword_nil
5816 {
5817 $$ = 0;
5818 }
5819 ;
5820
5821p_any_kwrest : p_kwrest
5822 | p_kwnorest
5823 {
5824 $$ = idNil;
5825 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5826 }
5827 ;
5828
5829p_value : p_primitive
5830 | p_primitive tDOT2 p_primitive
5831 {
5832 value_expr($1);
5833 value_expr($3);
5834 $$ = NEW_DOT2($1, $3, &@$);
5835 /*% ripper: dot2!($:1, $:3) %*/
5836 }
5837 | p_primitive tDOT3 p_primitive
5838 {
5839 value_expr($1);
5840 value_expr($3);
5841 $$ = NEW_DOT3($1, $3, &@$);
5842 /*% ripper: dot3!($:1, $:3) %*/
5843 }
5844 | p_primitive tDOT2
5845 {
5846 value_expr($1);
5847 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
5848 /*% ripper: dot2!($:1, Qnil) %*/
5849 }
5850 | p_primitive tDOT3
5851 {
5852 value_expr($1);
5853 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
5854 /*% ripper: dot3!($:1, Qnil) %*/
5855 }
5856 | p_var_ref
5857 | p_expr_ref
5858 | p_const
5859 | tBDOT2 p_primitive
5860 {
5861 value_expr($2);
5862 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
5863 /*% ripper: dot2!(Qnil, $:2) %*/
5864 }
5865 | tBDOT3 p_primitive
5866 {
5867 value_expr($2);
5868 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
5869 /*% ripper: dot3!(Qnil, $:2) %*/
5870 }
5871 ;
5872
5873p_primitive : inline_primary
5874 | keyword_variable
5875 {
5876 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5877 /*% ripper: var_ref!($:1) %*/
5878 }
5879 | lambda
5880 ;
5881
5882p_variable : tIDENTIFIER
5883 {
5884 error_duplicate_pattern_variable(p, $1, &@1);
5885 /*% ripper: var_field!($:1) %*/
5886 $$ = assignable(p, $1, 0, &@$);
5887 }
5888 ;
5889
5890p_var_ref : '^' tIDENTIFIER
5891 {
5892 NODE *n = gettable(p, $2, &@$);
5893 if (!n) {
5894 n = NEW_ERROR(&@$);
5895 }
5896 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5897 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5898 }
5899 $$ = n;
5900 /*% ripper: var_ref!($:2) %*/
5901 }
5902 | '^' nonlocal_var
5903 {
5904 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5905 /*% ripper: var_ref!($:2) %*/
5906 }
5907 ;
5908
5909p_expr_ref : '^' tLPAREN expr_value rparen
5910 {
5911 $$ = NEW_BLOCK($3, &@$);
5912 /*% ripper: begin!($:3) %*/
5913 }
5914 ;
5915
5916p_const : tCOLON3 cname
5917 {
5918 $$ = NEW_COLON3($2, &@$);
5919 /*% ripper: top_const_ref!($:2) %*/
5920 }
5921 | p_const tCOLON2 cname
5922 {
5923 $$ = NEW_COLON2($1, $3, &@$);
5924 /*% ripper: const_path_ref!($:1, $:3) %*/
5925 }
5926 | tCONSTANT
5927 {
5928 $$ = gettable(p, $1, &@$);
5929 /*% ripper: var_ref!($:1) %*/
5930 }
5931 ;
5932
5933opt_rescue : k_rescue exc_list exc_var then
5934 compstmt
5935 opt_rescue
5936 {
5937 NODE *err = $3;
5938 if ($3) {
5939 err = NEW_ERRINFO(&@3);
5940 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5941 }
5942 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5943 if ($2) {
5944 fixpos($$, $2);
5945 }
5946 else if ($3) {
5947 fixpos($$, $3);
5948 }
5949 else {
5950 fixpos($$, $5);
5951 }
5952 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5953 }
5954 | none
5955 ;
5956
5957exc_list : arg_value
5958 {
5959 $$ = NEW_LIST($1, &@$);
5960 /*% ripper: rb_ary_new3(1, $:1) %*/
5961 }
5962 | mrhs
5963 {
5964 if (!($$ = splat_array($1))) $$ = $1;
5965 }
5966 | none
5967 ;
5968
5969exc_var : tASSOC lhs
5970 {
5971 $$ = $2;
5972 /*% ripper: $:2 %*/
5973 }
5974 | none
5975 ;
5976
5977opt_ensure : k_ensure stmts terms?
5978 {
5979 p->ctxt.in_rescue = $1.in_rescue;
5980 $$ = $2;
5981 void_expr(p, void_stmts(p, $$));
5982 /*% ripper: ensure!($:2) %*/
5983 }
5984 | none
5985 ;
5986
5987literal : numeric
5988 | symbol
5989 ;
5990
5991strings : string
5992 {
5993 NODE *node = $1;
5994 if (!node) {
5995 node = NEW_STR(STRING_NEW0(), &@$);
5996 }
5997 else {
5998 node = evstr2dstr(p, node);
5999 }
6000 $$ = node;
6001 /*% ripper: $:1 %*/
6002 }
6003 ;
6004
6005string : tCHAR
6006 | string1
6007 | string string1
6008 {
6009 $$ = literal_concat(p, $1, $2, &@$);
6010 /*% ripper: string_concat!($:1, $:2) %*/
6011 }
6012 ;
6013
6014string1 : tSTRING_BEG string_contents tSTRING_END
6015 {
6016 $$ = heredoc_dedent(p, $2);
6017 if ($$) nd_set_loc($$, &@$);
6018 /*% ripper: $:2 %*/
6019 if (p->heredoc_indent > 0) {
6020 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
6021 p->heredoc_indent = 0;
6022 }
6023 /*% ripper: string_literal!($:$) %*/
6024 }
6025 ;
6026
6027xstring : tXSTRING_BEG xstring_contents tSTRING_END
6028 {
6029 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
6030 /*% ripper: $:2 %*/
6031 if (p->heredoc_indent > 0) {
6032 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
6033 p->heredoc_indent = 0;
6034 }
6035 /*% ripper: xstring_literal!($:$) %*/
6036 }
6037 ;
6038
6039regexp : tREGEXP_BEG regexp_contents tREGEXP_END
6040 {
6041 $$ = new_regexp(p, $2, $3, &@$);
6042 /*% ripper: regexp_literal!($:2, $:3) %*/
6043 }
6044 ;
6045
6046words : words(tWORDS_BEG, word_list) <node>
6047 ;
6048
6049word_list : /* none */
6050 {
6051 $$ = 0;
6052 /*% ripper: words_new! %*/
6053 }
6054 | word_list word ' '+
6055 {
6056 $$ = list_append(p, $1, evstr2dstr(p, $2));
6057 /*% ripper: words_add!($:1, $:2) %*/
6058 }
6059 ;
6060
6061word : string_content
6062 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
6063 | word string_content
6064 {
6065 $$ = literal_concat(p, $1, $2, &@$);
6066 /*% ripper: word_add!($:1, $:2) %*/
6067 }
6068 ;
6069
6070symbols : words(tSYMBOLS_BEG, symbol_list) <node>
6071 ;
6072
6073symbol_list : /* none */
6074 {
6075 $$ = 0;
6076 /*% ripper: symbols_new! %*/
6077 }
6078 | symbol_list word ' '+
6079 {
6080 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
6081 /*% ripper: symbols_add!($:1, $:2) %*/
6082 }
6083 ;
6084
6085qwords : words(tQWORDS_BEG, qword_list) <node>
6086 ;
6087
6088qsymbols : words(tQSYMBOLS_BEG, qsym_list) <node>
6089 ;
6090
6091qword_list : /* none */
6092 {
6093 $$ = 0;
6094 /*% ripper: qwords_new! %*/
6095 }
6096 | qword_list tSTRING_CONTENT ' '+
6097 {
6098 $$ = list_append(p, $1, $2);
6099 /*% ripper: qwords_add!($:1, $:2) %*/
6100 }
6101 ;
6102
6103qsym_list : /* none */
6104 {
6105 $$ = 0;
6106 /*% ripper: qsymbols_new! %*/
6107 }
6108 | qsym_list tSTRING_CONTENT ' '+
6109 {
6110 $$ = symbol_append(p, $1, $2);
6111 /*% ripper: qsymbols_add!($:1, $:2) %*/
6112 }
6113 ;
6114
6115string_contents : /* none */
6116 {
6117 $$ = 0;
6118 /*% ripper: string_content! %*/
6119 }
6120 | string_contents string_content
6121 {
6122 $$ = literal_concat(p, $1, $2, &@$);
6123 /*% ripper: string_add!($:1, $:2) %*/
6124 }
6125 ;
6126
6127xstring_contents: /* none */
6128 {
6129 $$ = 0;
6130 /*% ripper: xstring_new! %*/
6131 }
6132 | xstring_contents string_content
6133 {
6134 $$ = literal_concat(p, $1, $2, &@$);
6135 /*% ripper: xstring_add!($:1, $:2) %*/
6136 }
6137 ;
6138
6139regexp_contents: /* none */
6140 {
6141 $$ = 0;
6142 /*% ripper: regexp_new! %*/
6143 }
6144 | regexp_contents string_content
6145 {
6146 NODE *head = $1, *tail = $2;
6147 if (!head) {
6148 $$ = tail;
6149 }
6150 else if (!tail) {
6151 $$ = head;
6152 }
6153 else {
6154 switch (nd_type(head)) {
6155 case NODE_STR:
6156 head = str2dstr(p, head);
6157 break;
6158 case NODE_DSTR:
6159 break;
6160 default:
6161 head = list_append(p, NEW_DSTR(0, &@$), head);
6162 break;
6163 }
6164 $$ = list_append(p, head, tail);
6165 }
6166 /*% ripper: regexp_add!($:1, $:2) %*/
6167 }
6168 ;
6169
6170string_content : tSTRING_CONTENT
6171 /*% ripper[brace]: $:1 %*/
6172 | tSTRING_DVAR
6173 {
6174 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6175 $$ = p->lex.strterm;
6176 p->lex.strterm = 0;
6177 SET_LEX_STATE(EXPR_BEG);
6178 }<strterm>
6179 string_dvar
6180 {
6181 p->lex.strterm = $2;
6182 $$ = NEW_EVSTR($3, &@$);
6183 nd_set_line($$, @3.end_pos.lineno);
6184 /*% ripper: string_dvar!($:3) %*/
6185 }
6186 | tSTRING_DBEG[state]
6187 {
6188 CMDARG_PUSH(0);
6189 COND_PUSH(0);
6190 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6191 $$ = p->lex.strterm;
6192 p->lex.strterm = 0;
6193 SET_LEX_STATE(EXPR_BEG);
6194 }[term]<strterm>
6195 {
6196 $$ = p->lex.brace_nest;
6197 p->lex.brace_nest = 0;
6198 }[brace]<num>
6199 {
6200 $$ = p->heredoc_indent;
6201 p->heredoc_indent = 0;
6202 }[indent]<num>
6203 compstmt string_dend
6204 {
6205 COND_POP();
6206 CMDARG_POP();
6207 p->lex.strterm = $term;
6208 SET_LEX_STATE($state);
6209 p->lex.brace_nest = $brace;
6210 p->heredoc_indent = $indent;
6211 p->heredoc_line_indent = -1;
6212 if ($compstmt) nd_unset_fl_newline($compstmt);
6213 $$ = new_evstr(p, $compstmt, &@$);
6214 /*% ripper: string_embexpr!($:compstmt) %*/
6215 }
6216 ;
6217
6218string_dend : tSTRING_DEND
6219 | END_OF_INPUT
6220 ;
6221
6222string_dvar : nonlocal_var
6223 {
6224 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6225 /*% ripper: var_ref!($:1) %*/
6226 }
6227 | backref
6228 ;
6229
6230symbol : ssym
6231 | dsym
6232 ;
6233
6234ssym : tSYMBEG sym
6235 {
6236 SET_LEX_STATE(EXPR_END);
6237 VALUE str = rb_id2str($2);
6238 /*
6239 * TODO:
6240 * set_yylval_noname sets invalid id to yylval.
6241 * This branch can be removed once yylval is changed to
6242 * hold lexed string.
6243 */
6244 if (!str) str = STR_NEW0();
6245 $$ = NEW_SYM(str, &@$);
6246 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6247 }
6248 ;
6249
6250sym : fname
6251 | nonlocal_var
6252 ;
6253
6254dsym : tSYMBEG string_contents tSTRING_END
6255 {
6256 SET_LEX_STATE(EXPR_END);
6257 $$ = dsym_node(p, $2, &@$);
6258 /*% ripper: dyna_symbol!($:2) %*/
6259 }
6260 ;
6261
6262numeric : simple_numeric
6263 | tUMINUS_NUM simple_numeric %prec tLOWEST
6264 {
6265 $$ = $2;
6266 negate_lit(p, $$);
6267 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6268 }
6269 ;
6270
6271simple_numeric : tINTEGER
6272 | tFLOAT
6273 | tRATIONAL
6274 | tIMAGINARY
6275 ;
6276
6277nonlocal_var : tIVAR
6278 | tGVAR
6279 | tCVAR
6280 ;
6281
6282user_variable : ident_or_const
6283 | nonlocal_var
6284 ;
6285
6286keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6287 | keyword_self {$$ = KWD2EID(self, $1);}
6288 | keyword_true {$$ = KWD2EID(true, $1);}
6289 | keyword_false {$$ = KWD2EID(false, $1);}
6290 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6291 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6292 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6293 ;
6294
6295var_ref : user_variable
6296 {
6297 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6298 if (ifdef_ripper(id_is_var(p, $1), false)) {
6299 /*% ripper: var_ref!($:1) %*/
6300 }
6301 else {
6302 /*% ripper: vcall!($:1) %*/
6303 }
6304 }
6305 | keyword_variable
6306 {
6307 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6308 /*% ripper: var_ref!($:1) %*/
6309 }
6310 ;
6311
6312var_lhs : user_or_keyword_variable
6313 {
6314 /*% ripper: var_field!($:1) %*/
6315 $$ = assignable(p, $1, 0, &@$);
6316 }
6317 ;
6318
6319backref : tNTH_REF
6320 | tBACK_REF
6321 ;
6322
6323superclass : '<'
6324 {
6325 SET_LEX_STATE(EXPR_BEG);
6326 p->command_start = TRUE;
6327 }
6328 expr_value term
6329 {
6330 $$ = $3;
6331 /*% ripper: $:3 %*/
6332 }
6333 | /* none */
6334 {
6335 $$ = 0;
6336 /*% ripper: Qnil %*/
6337 }
6338 ;
6339
6340f_opt_paren_args: f_paren_args
6341 | none
6342 {
6343 p->ctxt.in_argdef = 0;
6344 $$ = new_args_tail(p, 0, 0, 0, &@0);
6345 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6346 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6347 }
6348 ;
6349
6350f_paren_args : '(' f_args rparen
6351 {
6352 $$ = $2;
6353 /*% ripper: paren!($:2) %*/
6354 SET_LEX_STATE(EXPR_BEG);
6355 p->command_start = TRUE;
6356 p->ctxt.in_argdef = 0;
6357 }
6358 ;
6359
6360f_arglist : f_paren_args
6361 | {
6362 $$ = p->ctxt;
6363 p->ctxt.in_kwarg = 1;
6364 p->ctxt.in_argdef = 1;
6365 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6366 }<ctxt>
6367 f_args term
6368 {
6369 p->ctxt.in_kwarg = $1.in_kwarg;
6370 p->ctxt.in_argdef = 0;
6371 $$ = $2;
6372 SET_LEX_STATE(EXPR_BEG);
6373 p->command_start = TRUE;
6374 /*% ripper: $:2 %*/
6375 }
6376 ;
6377
6378args_tail : f_kwarg(f_kw) ',' f_kwrest opt_f_block_arg
6379 {
6380 $$ = new_args_tail(p, $1, $3, $4, &@3);
6381 /*% ripper: [$:1, $:3, $:4] %*/
6382 }
6383 | f_kwarg(f_kw) opt_f_block_arg
6384 {
6385 $$ = new_args_tail(p, $1, 0, $2, &@1);
6386 /*% ripper: [$:1, Qnil, $:2] %*/
6387 }
6388 | f_any_kwrest opt_f_block_arg
6389 {
6390 $$ = new_args_tail(p, 0, $1, $2, &@1);
6391 /*% ripper: [Qnil, $:1, $:2] %*/
6392 }
6393 | f_block_arg
6394 {
6395 $$ = new_args_tail(p, 0, 0, $1, &@1);
6396 /*% ripper: [Qnil, Qnil, $:1] %*/
6397 }
6398 | args_forward
6399 {
6400 ID fwd = $args_forward;
6401 if (lambda_beginning_p() ||
6402 (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest)) {
6403 yyerror0("unexpected ... in lambda argument");
6404 fwd = 0;
6405 }
6406 else {
6407 add_forwarding_args(p);
6408 }
6409 $$ = new_args_tail(p, 0, fwd, arg_FWD_BLOCK, &@1);
6410 $$->nd_ainfo.forwarding = 1;
6411 /*% ripper: [Qnil, $:1, Qnil] %*/
6412 }
6413 ;
6414
6415f_args : f_arg ',' f_optarg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6416 {
6417 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
6418 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
6419 }
6420 | f_arg ',' f_optarg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6421 {
6422 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6423 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
6424 }
6425 | f_arg ',' f_optarg(arg_value) opt_args_tail(args_tail)
6426 {
6427 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
6428 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
6429 }
6430 | f_arg ',' f_optarg(arg_value) ',' f_arg opt_args_tail(args_tail)
6431 {
6432 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
6433 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
6434 }
6435 | f_arg ',' f_rest_arg opt_args_tail(args_tail)
6436 {
6437 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
6438 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
6439 }
6440 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6441 {
6442 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
6443 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
6444 }
6445 | f_arg opt_args_tail(args_tail)
6446 {
6447 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
6448 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
6449 }
6450 | f_optarg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6451 {
6452 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
6453 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
6454 }
6455 | f_optarg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6456 {
6457 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
6458 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
6459 }
6460 | f_optarg(arg_value) opt_args_tail(args_tail)
6461 {
6462 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
6463 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
6464 }
6465 | f_optarg(arg_value) ',' f_arg opt_args_tail(args_tail)
6466 {
6467 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
6468 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
6469 }
6470 | f_rest_arg opt_args_tail(args_tail)
6471 {
6472 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
6473 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
6474 }
6475 | f_rest_arg ',' f_arg opt_args_tail(args_tail)
6476 {
6477 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
6478 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
6479 }
6480 | args_tail
6481 {
6482 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
6483 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
6484 }
6485 | /* none */
6486 {
6487 $$ = new_args_tail(p, 0, 0, 0, &@0);
6488 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6489 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6490 }
6491 ;
6492
6493args_forward : tBDOT3
6494 {
6495#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
6496 $$ = 0;
6497#else
6498 $$ = idFWD_KWREST;
6499#endif
6500 /*% ripper: args_forward! %*/
6501 }
6502 ;
6503
6504f_bad_arg : tCONSTANT
6505 {
6506 static const char mesg[] = "formal argument cannot be a constant";
6507 /*%%%*/
6508 yyerror1(&@1, mesg);
6509 /*% %*/
6510 $$ = 0;
6511 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6512 }
6513 | tIVAR
6514 {
6515 static const char mesg[] = "formal argument cannot be an instance variable";
6516 /*%%%*/
6517 yyerror1(&@1, mesg);
6518 /*% %*/
6519 $$ = 0;
6520 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6521 }
6522 | tGVAR
6523 {
6524 static const char mesg[] = "formal argument cannot be a global variable";
6525 /*%%%*/
6526 yyerror1(&@1, mesg);
6527 /*% %*/
6528 $$ = 0;
6529 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6530 }
6531 | tCVAR
6532 {
6533 static const char mesg[] = "formal argument cannot be a class variable";
6534 /*%%%*/
6535 yyerror1(&@1, mesg);
6536 /*% %*/
6537 $$ = 0;
6538 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6539 }
6540 ;
6541
6542f_norm_arg : f_bad_arg
6543 | tIDENTIFIER
6544 {
6545 VALUE e = formal_argument_error(p, $$ = $1);
6546 if (e) {
6547 /*% ripper[error]: param_error!(?e, $:1) %*/
6548 }
6549 p->max_numparam = ORDINAL_PARAM;
6550 }
6551 ;
6552
6553f_arg_asgn : f_norm_arg
6554 {
6555 ID id = $1;
6556 arg_var(p, id);
6557 $$ = $1;
6558 }
6559 ;
6560
6561f_arg_item : f_arg_asgn
6562 {
6563 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6564 /*% ripper: $:1 %*/
6565 }
6566 | tLPAREN f_margs rparen
6567 {
6568 ID tid = internal_id(p);
6569 YYLTYPE loc;
6570 loc.beg_pos = @2.beg_pos;
6571 loc.end_pos = @2.beg_pos;
6572 arg_var(p, tid);
6573 if (dyna_in_block(p)) {
6574 $2->nd_value = NEW_DVAR(tid, &loc);
6575 }
6576 else {
6577 $2->nd_value = NEW_LVAR(tid, &loc);
6578 }
6579 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6580 $$->nd_next = (NODE *)$2;
6581 /*% ripper: mlhs_paren!($:2) %*/
6582 }
6583 ;
6584
6585f_arg : f_arg_item
6586 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6587 | f_arg ',' f_arg_item
6588 {
6589 $$ = $1;
6590 $$->nd_plen++;
6591 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6592 rb_discard_node(p, (NODE *)$3);
6593 /*% ripper: rb_ary_push($:1, $:3) %*/
6594 }
6595 ;
6596
6597
6598f_label : tLABEL
6599 {
6600 VALUE e = formal_argument_error(p, $$ = $1);
6601 if (e) {
6602 $$ = 0;
6603 /*% ripper[error]: param_error!(?e, $:1) %*/
6604 }
6605 /*
6606 * Workaround for Prism::ParseTest#test_filepath for
6607 * "unparser/corpus/literal/def.txt"
6608 *
6609 * See the discussion on https://github.com/ruby/ruby/pull/9923
6610 */
6611 arg_var(p, ifdef_ripper(0, $1));
6612 /*% ripper: $:1 %*/
6613 p->max_numparam = ORDINAL_PARAM;
6614 p->ctxt.in_argdef = 0;
6615 }
6616 ;
6617
6618f_kw : f_label arg_value
6619 {
6620 p->ctxt.in_argdef = 1;
6621 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6622 /*% ripper: [$:$, $:2] %*/
6623 }
6624 | f_label
6625 {
6626 p->ctxt.in_argdef = 1;
6627 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6628 /*% ripper: [$:$, 0] %*/
6629 }
6630 ;
6631
6632f_block_kw : f_label primary_value
6633 {
6634 p->ctxt.in_argdef = 1;
6635 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6636 /*% ripper: [$:$, $:2] %*/
6637 }
6638 | f_label
6639 {
6640 p->ctxt.in_argdef = 1;
6641 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6642 /*% ripper: [$:$, 0] %*/
6643 }
6644 ;
6645
6646kwrest_mark : tPOW
6647 | tDSTAR
6648 ;
6649
6650f_no_kwarg : p_kwnorest
6651 {
6652 /*% ripper: nokw_param!(Qnil) %*/
6653 }
6654 ;
6655
6656f_kwrest : kwrest_mark tIDENTIFIER
6657 {
6658 arg_var(p, shadowing_lvar(p, $2));
6659 $$ = $2;
6660 /*% ripper: kwrest_param!($:2) %*/
6661 }
6662 | kwrest_mark
6663 {
6664 arg_var(p, idFWD_KWREST);
6665 $$ = idFWD_KWREST;
6666 /*% ripper: kwrest_param!(Qnil) %*/
6667 }
6668 ;
6669
6670restarg_mark : '*'
6671 | tSTAR
6672 ;
6673
6674f_rest_arg : restarg_mark tIDENTIFIER
6675 {
6676 arg_var(p, shadowing_lvar(p, $2));
6677 $$ = $2;
6678 /*% ripper: rest_param!($:2) %*/
6679 }
6680 | restarg_mark
6681 {
6682 arg_var(p, idFWD_REST);
6683 $$ = idFWD_REST;
6684 /*% ripper: rest_param!(Qnil) %*/
6685 }
6686 ;
6687
6688blkarg_mark : '&'
6689 | tAMPER
6690 ;
6691
6692f_block_arg : blkarg_mark tIDENTIFIER
6693 {
6694 arg_var(p, shadowing_lvar(p, $2));
6695 $$ = $2;
6696 /*% ripper: blockarg!($:2) %*/
6697 }
6698 | blkarg_mark
6699 {
6700 arg_var(p, idFWD_BLOCK);
6701 $$ = idFWD_BLOCK;
6702 /*% ripper: blockarg!(Qnil) %*/
6703 }
6704 ;
6705
6706opt_f_block_arg : ',' f_block_arg
6707 {
6708 $$ = $2;
6709 /*% ripper: $:2 %*/
6710 }
6711 | none
6712 {
6713 $$ = 0;
6714 /*% ripper: Qnil %*/
6715 }
6716 ;
6717
6718singleton : var_ref
6719 {
6720 value_expr($1);
6721 $$ = $1;
6722 }
6723 | '('
6724 {
6725 SET_LEX_STATE(EXPR_BEG);
6726 p->ctxt.in_argdef = 0;
6727 }
6728 expr rparen
6729 {
6730 p->ctxt.in_argdef = 1;
6731 NODE *expr = last_expr_node($3);
6732 switch (nd_type(expr)) {
6733 case NODE_STR:
6734 case NODE_DSTR:
6735 case NODE_XSTR:
6736 case NODE_DXSTR:
6737 case NODE_REGX:
6738 case NODE_DREGX:
6739 case NODE_SYM:
6740 case NODE_LINE:
6741 case NODE_FILE:
6742 case NODE_ENCODING:
6743 case NODE_INTEGER:
6744 case NODE_FLOAT:
6745 case NODE_RATIONAL:
6746 case NODE_IMAGINARY:
6747 case NODE_DSYM:
6748 case NODE_LIST:
6749 case NODE_ZLIST:
6750 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6751 break;
6752 default:
6753 value_expr($3);
6754 break;
6755 }
6756 $$ = $3;
6757 /*% ripper: paren!($:3) %*/
6758 }
6759 ;
6760
6761assoc_list : none
6762 | assocs trailer
6763 {
6764 $$ = $1;
6765 /*% ripper: assoclist_from_args!($:1) %*/
6766 }
6767 ;
6768
6769assocs : assoc
6770 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6771 | assocs ',' assoc
6772 {
6773 NODE *assocs = $1;
6774 NODE *tail = $3;
6775 if (!assocs) {
6776 assocs = tail;
6777 }
6778 else if (tail) {
6779 if (RNODE_LIST(assocs)->nd_head) {
6780 NODE *n = RNODE_LIST(tail)->nd_next;
6781 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6782 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6783 /* DSTAR */
6784 tail = RNODE_HASH(n)->nd_head;
6785 }
6786 }
6787 if (tail) {
6788 assocs = list_concat(assocs, tail);
6789 }
6790 }
6791 $$ = assocs;
6792 /*% ripper: rb_ary_push($:1, $:3) %*/
6793 }
6794 ;
6795
6796assoc : arg_value tASSOC arg_value
6797 {
6798 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6799 /*% ripper: assoc_new!($:1, $:3) %*/
6800 }
6801 | tLABEL arg_value
6802 {
6803 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6804 /*% ripper: assoc_new!($:1, $:2) %*/
6805 }
6806 | tLABEL
6807 {
6808 NODE *val = gettable(p, $1, &@$);
6809 if (!val) val = NEW_ERROR(&@$);
6810 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6811 /*% ripper: assoc_new!($:1, Qnil) %*/
6812 }
6813 | tSTRING_BEG string_contents tLABEL_END arg_value
6814 {
6815 YYLTYPE loc = code_loc_gen(&@1, &@3);
6816 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6817 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6818 }
6819 | tDSTAR arg_value
6820 {
6821 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6822 /*% ripper: assoc_splat!($:2) %*/
6823 }
6824 | tDSTAR
6825 {
6826 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6827 $$ = list_append(p, NEW_LIST(0, &@$),
6828 NEW_LVAR(idFWD_KWREST, &@$));
6829 /*% ripper: assoc_splat!(Qnil) %*/
6830 }
6831 ;
6832
6833operation : ident_or_const
6834 | tFID
6835 ;
6836
6837operation2 : operation
6838 | op
6839 ;
6840
6841operation3 : tIDENTIFIER
6842 | tFID
6843 | op
6844 ;
6845
6846dot_or_colon : '.'
6847 | tCOLON2
6848 ;
6849
6850call_op : '.'
6851 | tANDDOT
6852 ;
6853
6854call_op2 : call_op
6855 | tCOLON2
6856 ;
6857
6858rparen : '\n'? ')'
6859 ;
6860
6861rbracket : '\n'? ']'
6862 ;
6863
6864rbrace : '\n'? '}'
6865 ;
6866
6867trailer : '\n'?
6868 | ','
6869 ;
6870
6871term : ';' {yyerrok;token_flush(p);}
6872 | '\n'
6873 {
6874 @$.end_pos = @$.beg_pos;
6875 token_flush(p);
6876 }
6877 ;
6878
6879terms : term
6880 | terms ';' {yyerrok;}
6881 ;
6882
6883none : /* none */
6884 {
6885 $$ = 0;
6886 }
6887 ;
6888%%
6889# undef p
6890# undef yylex
6891# undef yylval
6892# define yylval (*p->lval)
6893
6894static int regx_options(struct parser_params*);
6895static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6896static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6897static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6898static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6899
6900#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6901
6902# define set_yylval_node(x) { \
6903 YYLTYPE _cur_loc; \
6904 rb_parser_set_location(p, &_cur_loc); \
6905 yylval.node = (x); \
6906 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6907}
6908# define set_yylval_str(x) \
6909do { \
6910 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6911 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6912} while(0)
6913# define set_yylval_num(x) { \
6914 yylval.num = (x); \
6915 set_parser_s_value(x); \
6916}
6917# define set_yylval_id(x) (yylval.id = (x))
6918# define set_yylval_name(x) { \
6919 (yylval.id = (x)); \
6920 set_parser_s_value(ID2SYM(x)); \
6921}
6922# define yylval_id() (yylval.id)
6923
6924#define set_yylval_noname() set_yylval_id(keyword_nil)
6925#define has_delayed_token(p) (p->delayed.token != NULL)
6926
6927#ifndef RIPPER
6928#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6929#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6930
6931static bool
6932parser_has_token(struct parser_params *p)
6933{
6934 const char *const pcur = p->lex.pcur;
6935 const char *const ptok = p->lex.ptok;
6936 if (p->keep_tokens && (pcur < ptok)) {
6937 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6938 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6939 }
6940 return pcur > ptok;
6941}
6942
6943static const char *
6944escaped_char(int c)
6945{
6946 switch (c) {
6947 case '"': return "\\\"";
6948 case '\\': return "\\\\";
6949 case '\0': return "\\0";
6950 case '\n': return "\\n";
6951 case '\r': return "\\r";
6952 case '\t': return "\\t";
6953 case '\f': return "\\f";
6954 case '\013': return "\\v";
6955 case '\010': return "\\b";
6956 case '\007': return "\\a";
6957 case '\033': return "\\e";
6958 case '\x7f': return "\\c?";
6959 }
6960 return NULL;
6961}
6962
6963static rb_parser_string_t *
6964rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6965{
6966 rb_encoding *enc = p->enc;
6967 const char *ptr = str->ptr;
6968 const char *pend = ptr + str->len;
6969 const char *prev = ptr;
6970 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6971 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6972
6973 while (ptr < pend) {
6974 unsigned int c;
6975 const char *cc;
6976 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6977 if (!MBCLEN_CHARFOUND_P(n)) {
6978 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6979 n = rb_enc_mbminlen(enc);
6980 if (pend < ptr + n)
6981 n = (int)(pend - ptr);
6982 while (n--) {
6983 c = *ptr & 0xf0 >> 4;
6984 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6985 c = *ptr & 0x0f;
6986 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6987 parser_str_cat(result, charbuf, 4);
6988 prev = ++ptr;
6989 }
6990 continue;
6991 }
6992 n = MBCLEN_CHARFOUND_LEN(n);
6993 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6994 ptr += n;
6995 cc = escaped_char(c);
6996 if (cc) {
6997 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6998 parser_str_cat_cstr(result, cc);
6999 prev = ptr;
7000 }
7001 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
7002 }
7003 else {
7004 if (ptr - n > prev) {
7005 parser_str_cat(result, prev, ptr - n - prev);
7006 prev = ptr - n;
7007 }
7008 parser_str_cat(result, prev, ptr - prev);
7009 prev = ptr;
7010 }
7011 }
7012 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
7013
7014 return result;
7015}
7016
7017static void
7018parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
7019{
7020 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
7021 token->id = p->token_id;
7022 token->type_name = parser_token2char(p, t);
7023 token->str = str;
7024 token->loc.beg_pos = p->yylloc->beg_pos;
7025 token->loc.end_pos = p->yylloc->end_pos;
7026 rb_parser_ary_push_ast_token(p, p->tokens, token);
7027 p->token_id++;
7028
7029 if (p->debug) {
7030 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
7031 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
7032 line, token->id, token->type_name, str_escaped->ptr,
7033 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
7034 token->loc.end_pos.lineno, token->loc.end_pos.column);
7035 rb_parser_string_free(p, str_escaped);
7036 }
7037}
7038
7039static void
7040parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
7041{
7042 debug_token_line(p, "parser_dispatch_scan_event", line);
7043
7044 if (!parser_has_token(p)) return;
7045
7046 RUBY_SET_YYLLOC(*p->yylloc);
7047
7048 if (p->keep_tokens) {
7049 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
7050 parser_append_tokens(p, str, t, line);
7051 }
7052
7053 token_flush(p);
7054}
7055
7056#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
7057static void
7058parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
7059{
7060 debug_token_line(p, "parser_dispatch_delayed_token", line);
7061
7062 if (!has_delayed_token(p)) return;
7063
7064 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
7065
7066 if (p->keep_tokens) {
7067 /* p->delayed.token is freed by rb_parser_tokens_free */
7068 parser_append_tokens(p, p->delayed.token, t, line);
7069 } else {
7070 rb_parser_string_free(p, p->delayed.token);
7071 }
7072
7073 p->delayed.token = NULL;
7074}
7075#else
7076#define literal_flush(p, ptr) ((void)(ptr))
7077
7078static int
7079ripper_has_scan_event(struct parser_params *p)
7080{
7081 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
7082 return p->lex.pcur > p->lex.ptok;
7083}
7084
7085static VALUE
7086ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
7087{
7088 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
7089 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
7090 RUBY_SET_YYLLOC(*p->yylloc);
7091 token_flush(p);
7092 return rval;
7093}
7094
7095static void
7096ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
7097{
7098 if (!ripper_has_scan_event(p)) return;
7099
7100 set_parser_s_value(ripper_scan_event_val(p, t));
7101}
7102#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
7103
7104static void
7105ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
7106{
7107 /* save and adjust the location to delayed token for callbacks */
7108 int saved_line = p->ruby_sourceline;
7109 const char *saved_tokp = p->lex.ptok;
7110 VALUE s_value, str;
7111
7112 if (!has_delayed_token(p)) return;
7113 p->ruby_sourceline = p->delayed.beg_line;
7114 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
7115 str = rb_str_new_mutable_parser_string(p->delayed.token);
7116 rb_parser_string_free(p, p->delayed.token);
7117 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
7118 set_parser_s_value(s_value);
7119 p->delayed.token = NULL;
7120 p->ruby_sourceline = saved_line;
7121 p->lex.ptok = saved_tokp;
7122}
7123#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
7124#endif /* RIPPER */
7125
7126static inline int
7127is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
7128{
7129 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
7130}
7131
7132static inline int
7133parser_is_identchar(struct parser_params *p)
7134{
7135 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7136}
7137
7138static inline int
7139parser_isascii(struct parser_params *p)
7140{
7141 return ISASCII(*(p->lex.pcur-1));
7142}
7143
7144static void
7145token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7146{
7147 int column = 1, nonspc = 0, i;
7148 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7149 if (*ptr == '\t') {
7150 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7151 }
7152 column++;
7153 if (*ptr != ' ' && *ptr != '\t') {
7154 nonspc = 1;
7155 }
7156 }
7157
7158 ptinfo->beg = loc->beg_pos;
7159 ptinfo->indent = column;
7160 ptinfo->nonspc = nonspc;
7161}
7162
7163static void
7164token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7165{
7166 token_info *ptinfo;
7167
7168 if (!p->token_info_enabled) return;
7169 ptinfo = ALLOC(token_info);
7170 ptinfo->token = token;
7171 ptinfo->next = p->token_info;
7172 token_info_setup(ptinfo, p->lex.pbeg, loc);
7173
7174 p->token_info = ptinfo;
7175}
7176
7177static void
7178token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7179{
7180 token_info *ptinfo_beg = p->token_info;
7181
7182 if (!ptinfo_beg) return;
7183
7184 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7185 token_info_warn(p, token, ptinfo_beg, 1, loc);
7186
7187 p->token_info = ptinfo_beg->next;
7188 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7189}
7190
7191static void
7192token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7193{
7194 token_info *ptinfo_beg = p->token_info;
7195
7196 if (!ptinfo_beg) return;
7197 p->token_info = ptinfo_beg->next;
7198
7199 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7200 ptinfo_beg->beg.column != beg_pos.column ||
7201 strcmp(ptinfo_beg->token, token)) {
7202 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7203 beg_pos.lineno, beg_pos.column, token,
7204 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7205 ptinfo_beg->token);
7206 }
7207
7208 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7209}
7210
7211static void
7212token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7213{
7214 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7215 if (!p->token_info_enabled) return;
7216 if (!ptinfo_beg) return;
7217 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7218 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7219 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7220 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7221 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7222 rb_warn3L(ptinfo_end->beg.lineno,
7223 "mismatched indentations at '%s' with '%s' at %d",
7224 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7225}
7226
7227static int
7228parser_precise_mbclen(struct parser_params *p, const char *ptr)
7229{
7230 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7231 if (!MBCLEN_CHARFOUND_P(len)) {
7232 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7233 return -1;
7234 }
7235 return len;
7236}
7237
7238#ifndef RIPPER
7239static inline void
7240parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7241{
7242 rb_parser_string_t *str;
7243 int lineno = p->ruby_sourceline;
7244 if (!yylloc) {
7245 return;
7246 }
7247 else if (yylloc->beg_pos.lineno == lineno) {
7248 str = p->lex.lastline;
7249 }
7250 else {
7251 return;
7252 }
7253 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7254}
7255
7256static int
7257parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7258{
7259#if 0
7260 YYLTYPE current;
7261
7262 if (!yylloc) {
7263 yylloc = RUBY_SET_YYLLOC(current);
7264 }
7265 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7266 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7267 yylloc = 0;
7268 }
7269#endif
7270 parser_compile_error(p, yylloc, "%s", msg);
7271 parser_show_error_line(p, yylloc);
7272 return 0;
7273}
7274
7275static int
7276parser_yyerror0(struct parser_params *p, const char *msg)
7277{
7278 YYLTYPE current;
7279 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7280}
7281
7282void
7283ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7284{
7285 VALUE mesg;
7286 const int max_line_margin = 30;
7287 const char *ptr, *ptr_end, *pt, *pb;
7288 const char *pre = "", *post = "", *pend;
7289 const char *code = "", *caret = "";
7290 const char *lim;
7291 const char *const pbeg = PARSER_STRING_PTR(str);
7292 char *buf;
7293 long len;
7294 int i;
7295
7296 if (!yylloc) return;
7297 pend = rb_parser_string_end(str);
7298 if (pend > pbeg && pend[-1] == '\n') {
7299 if (--pend > pbeg && pend[-1] == '\r') --pend;
7300 }
7301
7302 pt = pend;
7303 if (lineno == yylloc->end_pos.lineno &&
7304 (pend - pbeg) > yylloc->end_pos.column) {
7305 pt = pbeg + yylloc->end_pos.column;
7306 }
7307
7308 ptr = ptr_end = pt;
7309 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7310 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7311
7312 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7313 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7314
7315 len = ptr_end - ptr;
7316 if (len > 4) {
7317 if (ptr > pbeg) {
7318 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7319 if (ptr > pbeg) pre = "...";
7320 }
7321 if (ptr_end < pend) {
7322 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7323 if (ptr_end < pend) post = "...";
7324 }
7325 }
7326 pb = pbeg;
7327 if (lineno == yylloc->beg_pos.lineno) {
7328 pb += yylloc->beg_pos.column;
7329 if (pb > pt) pb = pt;
7330 }
7331 if (pb < ptr) pb = ptr;
7332 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7333 return;
7334 }
7335 if (RTEST(errbuf)) {
7336 mesg = rb_attr_get(errbuf, idMesg);
7337 if (char_at_end(p, mesg, '\n') != '\n')
7338 rb_str_cat_cstr(mesg, "\n");
7339 }
7340 else {
7341 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7342 }
7343 if (!errbuf && rb_stderr_tty_p()) {
7344#define CSI_BEGIN "\033["
7345#define CSI_SGR "m"
7346 rb_str_catf(mesg,
7347 CSI_BEGIN""CSI_SGR"%s" /* pre */
7348 CSI_BEGIN"1"CSI_SGR"%.*s"
7349 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7350 CSI_BEGIN";1"CSI_SGR"%.*s"
7351 CSI_BEGIN""CSI_SGR"%s" /* post */
7352 "\n",
7353 pre,
7354 (int)(pb - ptr), ptr,
7355 (int)(pt - pb), pb,
7356 (int)(ptr_end - pt), pt,
7357 post);
7358 }
7359 else {
7360 char *p2;
7361
7362 len = ptr_end - ptr;
7363 lim = pt < pend ? pt : pend;
7364 i = (int)(lim - ptr);
7365 buf = ALLOCA_N(char, i+2);
7366 code = ptr;
7367 caret = p2 = buf;
7368 if (ptr <= pb) {
7369 while (ptr < pb) {
7370 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7371 }
7372 *p2++ = '^';
7373 ptr++;
7374 }
7375 if (lim > ptr) {
7376 memset(p2, '~', (lim - ptr));
7377 p2 += (lim - ptr);
7378 }
7379 *p2 = '\0';
7380 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7381 pre, (int)len, code, post,
7382 pre, caret);
7383 }
7384 if (!errbuf) rb_write_error_str(mesg);
7385}
7386#else
7387
7388static int
7389parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7390{
7391 const char *pcur = 0, *ptok = 0;
7392 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7393 p->ruby_sourceline == yylloc->end_pos.lineno) {
7394 pcur = p->lex.pcur;
7395 ptok = p->lex.ptok;
7396 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7397 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7398 }
7399 parser_yyerror0(p, msg);
7400 if (pcur) {
7401 p->lex.ptok = ptok;
7402 p->lex.pcur = pcur;
7403 }
7404 return 0;
7405}
7406
7407static int
7408parser_yyerror0(struct parser_params *p, const char *msg)
7409{
7410 dispatch1(parse_error, STR_NEW2(msg));
7411 ripper_error(p);
7412 return 0;
7413}
7414
7415static inline void
7416parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7417{
7418}
7419#endif /* !RIPPER */
7420
7421static int
7422vtable_size(const struct vtable *tbl)
7423{
7424 if (!DVARS_TERMINAL_P(tbl)) {
7425 return tbl->pos;
7426 }
7427 else {
7428 return 0;
7429 }
7430}
7431
7432static struct vtable *
7433vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7434{
7435 struct vtable *tbl = ALLOC(struct vtable);
7436 tbl->pos = 0;
7437 tbl->capa = 8;
7438 tbl->tbl = ALLOC_N(ID, tbl->capa);
7439 tbl->prev = prev;
7440#ifndef RIPPER
7441 if (p->debug) {
7442 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7443 }
7444#endif
7445 return tbl;
7446}
7447#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7448
7449static void
7450vtable_free_gen(struct parser_params *p, int line, const char *name,
7451 struct vtable *tbl)
7452{
7453#ifndef RIPPER
7454 if (p->debug) {
7455 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7456 }
7457#endif
7458 if (!DVARS_TERMINAL_P(tbl)) {
7459 if (tbl->tbl) {
7460 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7461 }
7462 ruby_sized_xfree(tbl, sizeof(*tbl));
7463 }
7464}
7465#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7466
7467static void
7468vtable_add_gen(struct parser_params *p, int line, const char *name,
7469 struct vtable *tbl, ID id)
7470{
7471#ifndef RIPPER
7472 if (p->debug) {
7473 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7474 line, name, (void *)tbl, rb_id2name(id));
7475 }
7476#endif
7477 if (DVARS_TERMINAL_P(tbl)) {
7478 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7479 return;
7480 }
7481 if (tbl->pos == tbl->capa) {
7482 tbl->capa = tbl->capa * 2;
7483 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7484 }
7485 tbl->tbl[tbl->pos++] = id;
7486}
7487#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7488
7489static void
7490vtable_pop_gen(struct parser_params *p, int line, const char *name,
7491 struct vtable *tbl, int n)
7492{
7493 if (p->debug) {
7494 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7495 line, name, (void *)tbl, n);
7496 }
7497 if (tbl->pos < n) {
7498 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7499 return;
7500 }
7501 tbl->pos -= n;
7502}
7503#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7504
7505static int
7506vtable_included(const struct vtable * tbl, ID id)
7507{
7508 int i;
7509
7510 if (!DVARS_TERMINAL_P(tbl)) {
7511 for (i = 0; i < tbl->pos; i++) {
7512 if (tbl->tbl[i] == id) {
7513 return i+1;
7514 }
7515 }
7516 }
7517 return 0;
7518}
7519
7520static void parser_prepare(struct parser_params *p);
7521
7522static int
7523e_option_supplied(struct parser_params *p)
7524{
7525 return strcmp(p->ruby_sourcefile, "-e") == 0;
7526}
7527
7528#ifndef RIPPER
7529static NODE *parser_append_options(struct parser_params *p, NODE *node);
7530
7531static VALUE
7532yycompile0(VALUE arg)
7533{
7534 int n;
7535 NODE *tree;
7536 struct parser_params *p = (struct parser_params *)arg;
7537 int cov = FALSE;
7538
7539 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7540 cov = TRUE;
7541 }
7542
7543 if (p->debug_lines) {
7544 p->ast->body.script_lines = p->debug_lines;
7545 }
7546
7547 parser_prepare(p);
7548#define RUBY_DTRACE_PARSE_HOOK(name) \
7549 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7550 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7551 }
7552 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7553 n = yyparse(p);
7554 RUBY_DTRACE_PARSE_HOOK(END);
7555
7556 p->debug_lines = 0;
7557
7558 xfree(p->lex.strterm);
7559 p->lex.strterm = 0;
7560 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7561 if (n || p->error_p) {
7562 VALUE mesg = p->error_buffer;
7563 if (!mesg) {
7564 mesg = syntax_error_new();
7565 }
7566 if (!p->error_tolerant) {
7567 rb_set_errinfo(mesg);
7568 return FALSE;
7569 }
7570 }
7571 tree = p->eval_tree;
7572 if (!tree) {
7573 tree = NEW_NIL(&NULL_LOC);
7574 }
7575 else {
7576 rb_parser_ary_t *tokens = p->tokens;
7577 NODE *prelude;
7578 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7579 prelude = block_append(p, p->eval_tree_begin, body);
7580 RNODE_SCOPE(tree)->nd_body = prelude;
7581 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7582 p->ast->body.coverage_enabled = cov;
7583 if (p->keep_tokens) {
7584 p->ast->node_buffer->tokens = tokens;
7585 p->tokens = NULL;
7586 }
7587 }
7588 p->ast->body.root = tree;
7589 p->ast->body.line_count = p->line_count;
7590 return TRUE;
7591}
7592
7593static rb_ast_t *
7594yycompile(struct parser_params *p, VALUE fname, int line)
7595{
7596 rb_ast_t *ast;
7597 if (NIL_P(fname)) {
7598 p->ruby_sourcefile_string = Qnil;
7599 p->ruby_sourcefile = "(none)";
7600 }
7601 else {
7602 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7603 p->ruby_sourcefile = StringValueCStr(fname);
7604 }
7605 p->ruby_sourceline = line - 1;
7606
7607 p->lvtbl = NULL;
7608
7609 p->ast = ast = rb_ast_new();
7610 compile_callback(yycompile0, (VALUE)p);
7611 p->ast = 0;
7612
7613 while (p->lvtbl) {
7614 local_pop(p);
7615 }
7616
7617 return ast;
7618}
7619#endif /* !RIPPER */
7620
7621static rb_encoding *
7622must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7623{
7624 rb_encoding *enc = rb_parser_str_get_encoding(s);
7625 if (!rb_enc_asciicompat(enc)) {
7626 rb_raise(rb_eArgError, "invalid source encoding");
7627 }
7628 return enc;
7629}
7630
7631static rb_parser_string_t *
7632lex_getline(struct parser_params *p)
7633{
7634 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7635 if (!line) return 0;
7636 p->line_count++;
7637 string_buffer_append(p, line);
7638 must_be_ascii_compatible(p, line);
7639 return line;
7640}
7641
7642#ifndef RIPPER
7643rb_ast_t*
7644rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7645{
7646 p->lex.gets = gets;
7647 p->lex.input = input;
7648 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7649
7650 return yycompile(p, fname, line);
7651}
7652#endif /* !RIPPER */
7653
7654#define STR_FUNC_ESCAPE 0x01
7655#define STR_FUNC_EXPAND 0x02
7656#define STR_FUNC_REGEXP 0x04
7657#define STR_FUNC_QWORDS 0x08
7658#define STR_FUNC_SYMBOL 0x10
7659#define STR_FUNC_INDENT 0x20
7660#define STR_FUNC_LABEL 0x40
7661#define STR_FUNC_LIST 0x4000
7662#define STR_FUNC_TERM 0x8000
7663
7664enum string_type {
7665 str_label = STR_FUNC_LABEL,
7666 str_squote = (0),
7667 str_dquote = (STR_FUNC_EXPAND),
7668 str_xquote = (STR_FUNC_EXPAND),
7669 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7670 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7671 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7672 str_ssym = (STR_FUNC_SYMBOL),
7673 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7674};
7675
7676static rb_parser_string_t *
7677parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7678{
7679 rb_parser_string_t *pstr;
7680
7681 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7682
7683 if (!(func & STR_FUNC_REGEXP)) {
7684 if (rb_parser_is_ascii_string(p, pstr)) {
7685 }
7686 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7687 /* everything is valid in ASCII-8BIT */
7688 enc = rb_ascii8bit_encoding();
7689 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7690 }
7691 }
7692
7693 return pstr;
7694}
7695
7696static int
7697strterm_is_heredoc(rb_strterm_t *strterm)
7698{
7699 return strterm->heredoc;
7700}
7701
7702static rb_strterm_t *
7703new_strterm(struct parser_params *p, int func, int term, int paren)
7704{
7705 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7706 strterm->u.literal.func = func;
7707 strterm->u.literal.term = term;
7708 strterm->u.literal.paren = paren;
7709 return strterm;
7710}
7711
7712static rb_strterm_t *
7713new_heredoc(struct parser_params *p)
7714{
7715 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7716 strterm->heredoc = true;
7717 return strterm;
7718}
7719
7720#define peek(p,c) peek_n(p, (c), 0)
7721#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7722#define peekc(p) peekc_n(p, 0)
7723#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7724
7725#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7726static void
7727parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7728{
7729 debug_token_line(p, "add_delayed_token", line);
7730
7731 if (tok < end) {
7732 if (has_delayed_token(p)) {
7733 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7734 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7735 int end_col = (next_line ? 0 : p->delayed.end_col);
7736 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7737 dispatch_delayed_token(p, tSTRING_CONTENT);
7738 }
7739 }
7740 if (!has_delayed_token(p)) {
7741 p->delayed.token = rb_parser_string_new(p, 0, 0);
7742 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7743 p->delayed.beg_line = p->ruby_sourceline;
7744 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7745 }
7746 parser_str_cat(p->delayed.token, tok, end - tok);
7747 p->delayed.end_line = p->ruby_sourceline;
7748 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7749 p->lex.ptok = end;
7750 }
7751}
7752
7753static void
7754set_lastline(struct parser_params *p, rb_parser_string_t *str)
7755{
7756 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7757 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7758 p->lex.lastline = str;
7759}
7760
7761static int
7762nextline(struct parser_params *p, int set_encoding)
7763{
7764 rb_parser_string_t *str = p->lex.nextline;
7765 p->lex.nextline = 0;
7766 if (!str) {
7767 if (p->eofp)
7768 return -1;
7769
7770 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7771 goto end_of_input;
7772 }
7773
7774 if (!p->lex.input || !(str = lex_getline(p))) {
7775 end_of_input:
7776 p->eofp = 1;
7777 lex_goto_eol(p);
7778 return -1;
7779 }
7780#ifndef RIPPER
7781 if (p->debug_lines) {
7782 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7783 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7784 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7785 }
7786#endif
7787 p->cr_seen = FALSE;
7788 }
7789 else if (str == AFTER_HEREDOC_WITHOUT_TERMINTOR) {
7790 /* after here-document without terminator */
7791 goto end_of_input;
7792 }
7793 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7794 if (p->heredoc_end > 0) {
7795 p->ruby_sourceline = p->heredoc_end;
7796 p->heredoc_end = 0;
7797 }
7798 p->ruby_sourceline++;
7799 set_lastline(p, str);
7800 token_flush(p);
7801 return 0;
7802}
7803
7804static int
7805parser_cr(struct parser_params *p, int c)
7806{
7807 if (peek(p, '\n')) {
7808 p->lex.pcur++;
7809 c = '\n';
7810 }
7811 return c;
7812}
7813
7814static inline int
7815nextc0(struct parser_params *p, int set_encoding)
7816{
7817 int c;
7818
7819 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINTOR)) {
7820 if (nextline(p, set_encoding)) return -1;
7821 }
7822 c = (unsigned char)*p->lex.pcur++;
7823 if (UNLIKELY(c == '\r')) {
7824 c = parser_cr(p, c);
7825 }
7826
7827 return c;
7828}
7829#define nextc(p) nextc0(p, TRUE)
7830
7831static void
7832pushback(struct parser_params *p, int c)
7833{
7834 if (c == -1) return;
7835 p->eofp = 0;
7836 p->lex.pcur--;
7837 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7838 p->lex.pcur--;
7839 }
7840}
7841
7842#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7843
7844#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7845#define tok(p) (p)->tokenbuf
7846#define toklen(p) (p)->tokidx
7847
7848static int
7849looking_at_eol_p(struct parser_params *p)
7850{
7851 const char *ptr = p->lex.pcur;
7852 while (!lex_eol_ptr_p(p, ptr)) {
7853 int c = (unsigned char)*ptr++;
7854 int eol = (c == '\n' || c == '#');
7855 if (eol || !ISSPACE(c)) {
7856 return eol;
7857 }
7858 }
7859 return TRUE;
7860}
7861
7862static char*
7863newtok(struct parser_params *p)
7864{
7865 p->tokidx = 0;
7866 if (!p->tokenbuf) {
7867 p->toksiz = 60;
7868 p->tokenbuf = ALLOC_N(char, 60);
7869 }
7870 if (p->toksiz > 4096) {
7871 p->toksiz = 60;
7872 REALLOC_N(p->tokenbuf, char, 60);
7873 }
7874 return p->tokenbuf;
7875}
7876
7877static char *
7878tokspace(struct parser_params *p, int n)
7879{
7880 p->tokidx += n;
7881
7882 if (p->tokidx >= p->toksiz) {
7883 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7884 REALLOC_N(p->tokenbuf, char, p->toksiz);
7885 }
7886 return &p->tokenbuf[p->tokidx-n];
7887}
7888
7889static void
7890tokadd(struct parser_params *p, int c)
7891{
7892 p->tokenbuf[p->tokidx++] = (char)c;
7893 if (p->tokidx >= p->toksiz) {
7894 p->toksiz *= 2;
7895 REALLOC_N(p->tokenbuf, char, p->toksiz);
7896 }
7897}
7898
7899static int
7900tok_hex(struct parser_params *p, size_t *numlen)
7901{
7902 int c;
7903
7904 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7905 if (!*numlen) {
7906 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7907 yyerror0("invalid hex escape");
7908 dispatch_scan_event(p, tSTRING_CONTENT);
7909 return 0;
7910 }
7911 p->lex.pcur += *numlen;
7912 return c;
7913}
7914
7915#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7916
7917static int
7918escaped_control_code(int c)
7919{
7920 int c2 = 0;
7921 switch (c) {
7922 case ' ':
7923 c2 = 's';
7924 break;
7925 case '\n':
7926 c2 = 'n';
7927 break;
7928 case '\t':
7929 c2 = 't';
7930 break;
7931 case '\v':
7932 c2 = 'v';
7933 break;
7934 case '\r':
7935 c2 = 'r';
7936 break;
7937 case '\f':
7938 c2 = 'f';
7939 break;
7940 }
7941 return c2;
7942}
7943
7944#define WARN_SPACE_CHAR(c, prefix) \
7945 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7946
7947static int
7948tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7949 int regexp_literal, const char *begin)
7950{
7951 const int wide = !begin;
7952 size_t numlen;
7953 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7954
7955 p->lex.pcur += numlen;
7956 if (p->lex.strterm == NULL ||
7957 strterm_is_heredoc(p->lex.strterm) ||
7958 (p->lex.strterm->u.literal.func != str_regexp)) {
7959 if (!begin) begin = p->lex.pcur;
7960 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7961 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7962 yyerror0("invalid Unicode escape");
7963 dispatch_scan_event(p, tSTRING_CONTENT);
7964 return wide && numlen > 0;
7965 }
7966 if (codepoint > 0x10ffff) {
7967 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7968 yyerror0("invalid Unicode codepoint (too large)");
7969 dispatch_scan_event(p, tSTRING_CONTENT);
7970 return wide;
7971 }
7972 if ((codepoint & 0xfffff800) == 0xd800) {
7973 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7974 yyerror0("invalid Unicode codepoint");
7975 dispatch_scan_event(p, tSTRING_CONTENT);
7976 return wide;
7977 }
7978 }
7979 if (regexp_literal) {
7980 tokcopy(p, (int)numlen);
7981 }
7982 else if (codepoint >= 0x80) {
7983 rb_encoding *utf8 = rb_utf8_encoding();
7984 if (*encp && utf8 != *encp) {
7985 YYLTYPE loc = RUBY_INIT_YYLLOC();
7986 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7987 parser_show_error_line(p, &loc);
7988 return wide;
7989 }
7990 *encp = utf8;
7991 tokaddmbc(p, codepoint, *encp);
7992 }
7993 else {
7994 tokadd(p, codepoint);
7995 }
7996 return TRUE;
7997}
7998
7999static int tokadd_mbchar(struct parser_params *p, int c);
8000
8001static int
8002tokskip_mbchar(struct parser_params *p)
8003{
8004 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8005 if (len > 0) {
8006 p->lex.pcur += len - 1;
8007 }
8008 return len;
8009}
8010
8011/* return value is for ?\u3042 */
8012static void
8013tokadd_utf8(struct parser_params *p, rb_encoding **encp,
8014 int term, int symbol_literal, int regexp_literal)
8015{
8016 /*
8017 * If `term` is not -1, then we allow multiple codepoints in \u{}
8018 * upto `term` byte, otherwise we're parsing a character literal.
8019 * And then add the codepoints to the current token.
8020 */
8021 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
8022
8023 const int open_brace = '{', close_brace = '}';
8024
8025 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
8026
8027 if (peek(p, open_brace)) { /* handle \u{...} form */
8028 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
8029 /*
8030 * Skip parsing validation code and copy bytes as-is until term or
8031 * closing brace, in order to correctly handle extended regexps where
8032 * invalid unicode escapes are allowed in comments. The regexp parser
8033 * does its own validation and will catch any issues.
8034 */
8035 tokadd(p, open_brace);
8036 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
8037 int c = peekc(p);
8038 if (c == close_brace) {
8039 tokadd(p, c);
8040 ++p->lex.pcur;
8041 break;
8042 }
8043 else if (c == term) {
8044 break;
8045 }
8046 if (c == '\\' && !lex_eol_n_p(p, 1)) {
8047 tokadd(p, c);
8048 c = *++p->lex.pcur;
8049 }
8050 tokadd_mbchar(p, c);
8051 }
8052 }
8053 else {
8054 const char *second = NULL;
8055 int c, last = nextc(p);
8056 if (lex_eol_p(p)) goto unterminated;
8057 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
8058 while (c != close_brace) {
8059 if (c == term) goto unterminated;
8060 if (second == multiple_codepoints)
8061 second = p->lex.pcur;
8062 if (regexp_literal) tokadd(p, last);
8063 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
8064 break;
8065 }
8066 while (ISSPACE(c = peekc(p))) {
8067 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
8068 last = c;
8069 }
8070 if (term == -1 && !second)
8071 second = multiple_codepoints;
8072 }
8073
8074 if (c != close_brace) {
8075 unterminated:
8076 flush_string_content(p, rb_utf8_encoding(), 0);
8077 yyerror0("unterminated Unicode escape");
8078 dispatch_scan_event(p, tSTRING_CONTENT);
8079 return;
8080 }
8081 if (second && second != multiple_codepoints) {
8082 const char *pcur = p->lex.pcur;
8083 p->lex.pcur = second;
8084 dispatch_scan_event(p, tSTRING_CONTENT);
8085 token_flush(p);
8086 p->lex.pcur = pcur;
8087 yyerror0(multiple_codepoints);
8088 token_flush(p);
8089 }
8090
8091 if (regexp_literal) tokadd(p, close_brace);
8092 nextc(p);
8093 }
8094 }
8095 else { /* handle \uxxxx form */
8096 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
8097 token_flush(p);
8098 return;
8099 }
8100 }
8101}
8102
8103#define ESCAPE_CONTROL 1
8104#define ESCAPE_META 2
8105
8106static int
8107read_escape(struct parser_params *p, int flags, const char *begin)
8108{
8109 int c;
8110 size_t numlen;
8111
8112 switch (c = nextc(p)) {
8113 case '\\': /* Backslash */
8114 return c;
8115
8116 case 'n': /* newline */
8117 return '\n';
8118
8119 case 't': /* horizontal tab */
8120 return '\t';
8121
8122 case 'r': /* carriage-return */
8123 return '\r';
8124
8125 case 'f': /* form-feed */
8126 return '\f';
8127
8128 case 'v': /* vertical tab */
8129 return '\13';
8130
8131 case 'a': /* alarm(bell) */
8132 return '\007';
8133
8134 case 'e': /* escape */
8135 return 033;
8136
8137 case '0': case '1': case '2': case '3': /* octal constant */
8138 case '4': case '5': case '6': case '7':
8139 pushback(p, c);
8140 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8141 p->lex.pcur += numlen;
8142 return c;
8143
8144 case 'x': /* hex constant */
8145 c = tok_hex(p, &numlen);
8146 if (numlen == 0) return 0;
8147 return c;
8148
8149 case 'b': /* backspace */
8150 return '\010';
8151
8152 case 's': /* space */
8153 return ' ';
8154
8155 case 'M':
8156 if (flags & ESCAPE_META) goto eof;
8157 if ((c = nextc(p)) != '-') {
8158 goto eof;
8159 }
8160 if ((c = nextc(p)) == '\\') {
8161 switch (peekc(p)) {
8162 case 'u': case 'U':
8163 nextc(p);
8164 goto eof;
8165 }
8166 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8167 }
8168 else if (c == -1) goto eof;
8169 else if (!ISASCII(c)) {
8170 tokskip_mbchar(p);
8171 goto eof;
8172 }
8173 else {
8174 int c2 = escaped_control_code(c);
8175 if (c2) {
8176 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8177 WARN_SPACE_CHAR(c2, "\\M-");
8178 }
8179 else {
8180 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8181 }
8182 }
8183 else if (ISCNTRL(c)) goto eof;
8184 return ((c & 0xff) | 0x80);
8185 }
8186
8187 case 'C':
8188 if ((c = nextc(p)) != '-') {
8189 goto eof;
8190 }
8191 case 'c':
8192 if (flags & ESCAPE_CONTROL) goto eof;
8193 if ((c = nextc(p))== '\\') {
8194 switch (peekc(p)) {
8195 case 'u': case 'U':
8196 nextc(p);
8197 goto eof;
8198 }
8199 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8200 }
8201 else if (c == '?')
8202 return 0177;
8203 else if (c == -1) goto eof;
8204 else if (!ISASCII(c)) {
8205 tokskip_mbchar(p);
8206 goto eof;
8207 }
8208 else {
8209 int c2 = escaped_control_code(c);
8210 if (c2) {
8211 if (ISCNTRL(c)) {
8212 if (flags & ESCAPE_META) {
8213 WARN_SPACE_CHAR(c2, "\\M-");
8214 }
8215 else {
8216 WARN_SPACE_CHAR(c2, "");
8217 }
8218 }
8219 else {
8220 if (flags & ESCAPE_META) {
8221 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8222 }
8223 else {
8224 WARN_SPACE_CHAR(c2, "\\C-");
8225 }
8226 }
8227 }
8228 else if (ISCNTRL(c)) goto eof;
8229 }
8230 return c & 0x9f;
8231
8232 eof:
8233 case -1:
8234 flush_string_content(p, p->enc, p->lex.pcur - begin);
8235 yyerror0("Invalid escape character syntax");
8236 dispatch_scan_event(p, tSTRING_CONTENT);
8237 return '\0';
8238
8239 default:
8240 return c;
8241 }
8242}
8243
8244static void
8245tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8246{
8247 int len = rb_enc_codelen(c, enc);
8248 rb_enc_mbcput(c, tokspace(p, len), enc);
8249}
8250
8251static int
8252tokadd_escape(struct parser_params *p)
8253{
8254 int c;
8255 size_t numlen;
8256 const char *begin = p->lex.pcur;
8257
8258 switch (c = nextc(p)) {
8259 case '\n':
8260 return 0; /* just ignore */
8261
8262 case '0': case '1': case '2': case '3': /* octal constant */
8263 case '4': case '5': case '6': case '7':
8264 {
8265 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8266 if (numlen == 0) goto eof;
8267 p->lex.pcur += numlen;
8268 tokcopy(p, (int)numlen + 1);
8269 }
8270 return 0;
8271
8272 case 'x': /* hex constant */
8273 {
8274 tok_hex(p, &numlen);
8275 if (numlen == 0) return -1;
8276 tokcopy(p, (int)numlen + 2);
8277 }
8278 return 0;
8279
8280 eof:
8281 case -1:
8282 flush_string_content(p, p->enc, p->lex.pcur - begin);
8283 yyerror0("Invalid escape character syntax");
8284 token_flush(p);
8285 return -1;
8286
8287 default:
8288 tokadd(p, '\\');
8289 tokadd(p, c);
8290 }
8291 return 0;
8292}
8293
8294static int
8295char_to_option(int c)
8296{
8297 int val;
8298
8299 switch (c) {
8300 case 'i':
8301 val = RE_ONIG_OPTION_IGNORECASE;
8302 break;
8303 case 'x':
8304 val = RE_ONIG_OPTION_EXTEND;
8305 break;
8306 case 'm':
8307 val = RE_ONIG_OPTION_MULTILINE;
8308 break;
8309 default:
8310 val = 0;
8311 break;
8312 }
8313 return val;
8314}
8315
8316#define ARG_ENCODING_FIXED 16
8317#define ARG_ENCODING_NONE 32
8318#define ENC_ASCII8BIT 1
8319#define ENC_EUC_JP 2
8320#define ENC_Windows_31J 3
8321#define ENC_UTF8 4
8322
8323static int
8324char_to_option_kcode(int c, int *option, int *kcode)
8325{
8326 *option = 0;
8327
8328 switch (c) {
8329 case 'n':
8330 *kcode = ENC_ASCII8BIT;
8331 return (*option = ARG_ENCODING_NONE);
8332 case 'e':
8333 *kcode = ENC_EUC_JP;
8334 break;
8335 case 's':
8336 *kcode = ENC_Windows_31J;
8337 break;
8338 case 'u':
8339 *kcode = ENC_UTF8;
8340 break;
8341 default:
8342 *kcode = -1;
8343 return (*option = char_to_option(c));
8344 }
8345 *option = ARG_ENCODING_FIXED;
8346 return 1;
8347}
8348
8349static int
8350regx_options(struct parser_params *p)
8351{
8352 int kcode = 0;
8353 int kopt = 0;
8354 int options = 0;
8355 int c, opt, kc;
8356
8357 newtok(p);
8358 while (c = nextc(p), ISALPHA(c)) {
8359 if (c == 'o') {
8360 options |= RE_OPTION_ONCE;
8361 }
8362 else if (char_to_option_kcode(c, &opt, &kc)) {
8363 if (kc >= 0) {
8364 if (kc != ENC_ASCII8BIT) kcode = c;
8365 kopt = opt;
8366 }
8367 else {
8368 options |= opt;
8369 }
8370 }
8371 else {
8372 tokadd(p, c);
8373 }
8374 }
8375 options |= kopt;
8376 pushback(p, c);
8377 if (toklen(p)) {
8378 YYLTYPE loc = RUBY_INIT_YYLLOC();
8379 tokfix(p);
8380 compile_error(p, "unknown regexp option%s - %*s",
8381 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8382 parser_show_error_line(p, &loc);
8383 }
8384 return options | RE_OPTION_ENCODING(kcode);
8385}
8386
8387static int
8388tokadd_mbchar(struct parser_params *p, int c)
8389{
8390 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8391 if (len < 0) return -1;
8392 tokadd(p, c);
8393 p->lex.pcur += --len;
8394 if (len > 0) tokcopy(p, len);
8395 return c;
8396}
8397
8398static inline int
8399simple_re_meta(int c)
8400{
8401 switch (c) {
8402 case '$': case '*': case '+': case '.':
8403 case '?': case '^': case '|':
8404 case ')': case ']': case '}': case '>':
8405 return TRUE;
8406 default:
8407 return FALSE;
8408 }
8409}
8410
8411static int
8412parser_update_heredoc_indent(struct parser_params *p, int c)
8413{
8414 if (p->heredoc_line_indent == -1) {
8415 if (c == '\n') p->heredoc_line_indent = 0;
8416 }
8417 else {
8418 if (c == ' ') {
8419 p->heredoc_line_indent++;
8420 return TRUE;
8421 }
8422 else if (c == '\t') {
8423 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8424 p->heredoc_line_indent = w * TAB_WIDTH;
8425 return TRUE;
8426 }
8427 else if (c != '\n') {
8428 if (p->heredoc_indent > p->heredoc_line_indent) {
8429 p->heredoc_indent = p->heredoc_line_indent;
8430 }
8431 p->heredoc_line_indent = -1;
8432 }
8433 else {
8434 /* Whitespace only line has no indentation */
8435 p->heredoc_line_indent = 0;
8436 }
8437 }
8438 return FALSE;
8439}
8440
8441static void
8442parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8443{
8444 YYLTYPE loc = RUBY_INIT_YYLLOC();
8445 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8446 compile_error(p, "%s mixed within %s source", n1, n2);
8447 parser_show_error_line(p, &loc);
8448}
8449
8450static void
8451parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8452{
8453 const char *pos = p->lex.pcur;
8454 p->lex.pcur = beg;
8455 parser_mixed_error(p, enc1, enc2);
8456 p->lex.pcur = pos;
8457}
8458
8459static inline char
8460nibble_char_upper(unsigned int c)
8461{
8462 c &= 0xf;
8463 return c + (c < 10 ? '0' : 'A' - 10);
8464}
8465
8466static int
8467tokadd_string(struct parser_params *p,
8468 int func, int term, int paren, long *nest,
8469 rb_encoding **encp, rb_encoding **enc)
8470{
8471 int c;
8472 bool erred = false;
8473#ifdef RIPPER
8474 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8475 int top_of_line = FALSE;
8476#endif
8477
8478#define mixed_error(enc1, enc2) \
8479 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8480#define mixed_escape(beg, enc1, enc2) \
8481 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8482
8483 while ((c = nextc(p)) != -1) {
8484 if (p->heredoc_indent > 0) {
8485 parser_update_heredoc_indent(p, c);
8486 }
8487#ifdef RIPPER
8488 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8489 pushback(p, c);
8490 break;
8491 }
8492#endif
8493
8494 if (paren && c == paren) {
8495 ++*nest;
8496 }
8497 else if (c == term) {
8498 if (!nest || !*nest) {
8499 pushback(p, c);
8500 break;
8501 }
8502 --*nest;
8503 }
8504 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8505 unsigned char c2 = *p->lex.pcur;
8506 if (c2 == '$' || c2 == '@' || c2 == '{') {
8507 pushback(p, c);
8508 break;
8509 }
8510 }
8511 else if (c == '\\') {
8512 c = nextc(p);
8513 switch (c) {
8514 case '\n':
8515 if (func & STR_FUNC_QWORDS) break;
8516 if (func & STR_FUNC_EXPAND) {
8517 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8518 continue;
8519 if (c == term) {
8520 c = '\\';
8521 goto terminate;
8522 }
8523 }
8524 tokadd(p, '\\');
8525 break;
8526
8527 case '\\':
8528 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8529 break;
8530
8531 case 'u':
8532 if ((func & STR_FUNC_EXPAND) == 0) {
8533 tokadd(p, '\\');
8534 break;
8535 }
8536 tokadd_utf8(p, enc, term,
8537 func & STR_FUNC_SYMBOL,
8538 func & STR_FUNC_REGEXP);
8539 continue;
8540
8541 default:
8542 if (c == -1) return -1;
8543 if (!ISASCII(c)) {
8544 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8545 goto non_ascii;
8546 }
8547 if (func & STR_FUNC_REGEXP) {
8548 switch (c) {
8549 case 'c':
8550 case 'C':
8551 case 'M': {
8552 pushback(p, c);
8553 c = read_escape(p, 0, p->lex.pcur - 1);
8554
8555 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8556 *t++ = '\\';
8557 *t++ = 'x';
8558 *t++ = nibble_char_upper(c >> 4);
8559 *t++ = nibble_char_upper(c);
8560 continue;
8561 }
8562 }
8563
8564 if (c == term && !simple_re_meta(c)) {
8565 tokadd(p, c);
8566 continue;
8567 }
8568 pushback(p, c);
8569 if ((c = tokadd_escape(p)) < 0)
8570 return -1;
8571 if (*enc && *enc != *encp) {
8572 mixed_escape(p->lex.ptok+2, *enc, *encp);
8573 }
8574 continue;
8575 }
8576 else if (func & STR_FUNC_EXPAND) {
8577 pushback(p, c);
8578 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8579 c = read_escape(p, 0, p->lex.pcur - 1);
8580 }
8581 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8582 /* ignore backslashed spaces in %w */
8583 }
8584 else if (c != term && !(paren && c == paren)) {
8585 tokadd(p, '\\');
8586 pushback(p, c);
8587 continue;
8588 }
8589 }
8590 }
8591 else if (!parser_isascii(p)) {
8592 non_ascii:
8593 if (!*enc) {
8594 *enc = *encp;
8595 }
8596 else if (*enc != *encp) {
8597 mixed_error(*enc, *encp);
8598 continue;
8599 }
8600 if (tokadd_mbchar(p, c) == -1) return -1;
8601 continue;
8602 }
8603 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8604 pushback(p, c);
8605 break;
8606 }
8607 if (c & 0x80) {
8608 if (!*enc) {
8609 *enc = *encp;
8610 }
8611 else if (*enc != *encp) {
8612 mixed_error(*enc, *encp);
8613 continue;
8614 }
8615 }
8616 tokadd(p, c);
8617#ifdef RIPPER
8618 top_of_line = (c == '\n');
8619#endif
8620 }
8621 terminate:
8622 if (*enc) *encp = *enc;
8623 return c;
8624}
8625
8626#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8627
8628static void
8629flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8630{
8631 p->lex.pcur -= back;
8632 if (has_delayed_token(p)) {
8633 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8634 if (len > 0) {
8635 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8636 p->delayed.end_line = p->ruby_sourceline;
8637 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8638 }
8639 dispatch_delayed_token(p, tSTRING_CONTENT);
8640 p->lex.ptok = p->lex.pcur;
8641 }
8642 dispatch_scan_event(p, tSTRING_CONTENT);
8643 p->lex.pcur += back;
8644}
8645
8646/* this can be shared with ripper, since it's independent from struct
8647 * parser_params. */
8648#ifndef RIPPER
8649#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8650#define SPECIAL_PUNCT(idx) ( \
8651 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8652 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8653 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8654 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8655 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8656 BIT('0', idx))
8657const uint_least32_t ruby_global_name_punct_bits[] = {
8658 SPECIAL_PUNCT(0),
8659 SPECIAL_PUNCT(1),
8660 SPECIAL_PUNCT(2),
8661};
8662#undef BIT
8663#undef SPECIAL_PUNCT
8664#endif
8665
8666static enum yytokentype
8667parser_peek_variable_name(struct parser_params *p)
8668{
8669 int c;
8670 const char *ptr = p->lex.pcur;
8671
8672 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8673 c = *ptr++;
8674 switch (c) {
8675 case '$':
8676 if ((c = *ptr) == '-') {
8677 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8678 c = *ptr;
8679 }
8680 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8681 return tSTRING_DVAR;
8682 }
8683 break;
8684 case '@':
8685 if ((c = *ptr) == '@') {
8686 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8687 c = *ptr;
8688 }
8689 break;
8690 case '{':
8691 p->lex.pcur = ptr;
8692 p->command_start = TRUE;
8693 yylval.state = p->lex.state;
8694 return tSTRING_DBEG;
8695 default:
8696 return 0;
8697 }
8698 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8699 return tSTRING_DVAR;
8700 return 0;
8701}
8702
8703#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8704#define IS_END() IS_lex_state(EXPR_END_ANY)
8705#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8706#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8707#define IS_LABEL_POSSIBLE() (\
8708 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8709 IS_ARG())
8710#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8711#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8712
8713static inline enum yytokentype
8714parser_string_term(struct parser_params *p, int func)
8715{
8716 xfree(p->lex.strterm);
8717 p->lex.strterm = 0;
8718 if (func & STR_FUNC_REGEXP) {
8719 set_yylval_num(regx_options(p));
8720 dispatch_scan_event(p, tREGEXP_END);
8721 SET_LEX_STATE(EXPR_END);
8722 return tREGEXP_END;
8723 }
8724 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8725 nextc(p);
8726 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8727 return tLABEL_END;
8728 }
8729 SET_LEX_STATE(EXPR_END);
8730 return tSTRING_END;
8731}
8732
8733static enum yytokentype
8734parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8735{
8736 int func = quote->func;
8737 int term = quote->term;
8738 int paren = quote->paren;
8739 int c, space = 0;
8740 rb_encoding *enc = p->enc;
8741 rb_encoding *base_enc = 0;
8742 rb_parser_string_t *lit;
8743
8744 if (func & STR_FUNC_TERM) {
8745 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8746 SET_LEX_STATE(EXPR_END);
8747 xfree(p->lex.strterm);
8748 p->lex.strterm = 0;
8749 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8750 }
8751 c = nextc(p);
8752 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8753 while (c != '\n' && ISSPACE(c = nextc(p)));
8754 space = 1;
8755 }
8756 if (func & STR_FUNC_LIST) {
8757 quote->func &= ~STR_FUNC_LIST;
8758 space = 1;
8759 }
8760 if (c == term && !quote->nest) {
8761 if (func & STR_FUNC_QWORDS) {
8762 quote->func |= STR_FUNC_TERM;
8763 pushback(p, c); /* dispatch the term at tSTRING_END */
8764 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8765 return ' ';
8766 }
8767 return parser_string_term(p, func);
8768 }
8769 if (space) {
8770 if (!ISSPACE(c)) pushback(p, c);
8771 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8772 return ' ';
8773 }
8774 newtok(p);
8775 if ((func & STR_FUNC_EXPAND) && c == '#') {
8776 enum yytokentype t = parser_peek_variable_name(p);
8777 if (t) return t;
8778 tokadd(p, '#');
8779 c = nextc(p);
8780 }
8781 pushback(p, c);
8782 if (tokadd_string(p, func, term, paren, &quote->nest,
8783 &enc, &base_enc) == -1) {
8784 if (p->eofp) {
8785#ifndef RIPPER
8786# define unterminated_literal(mesg) yyerror0(mesg)
8787#else
8788# define unterminated_literal(mesg) compile_error(p, mesg)
8789#endif
8790 literal_flush(p, p->lex.pcur);
8791 if (func & STR_FUNC_QWORDS) {
8792 /* no content to add, bailing out here */
8793 unterminated_literal("unterminated list meets end of file");
8794 xfree(p->lex.strterm);
8795 p->lex.strterm = 0;
8796 return tSTRING_END;
8797 }
8798 if (func & STR_FUNC_REGEXP) {
8799 unterminated_literal("unterminated regexp meets end of file");
8800 }
8801 else {
8802 unterminated_literal("unterminated string meets end of file");
8803 }
8804 quote->func |= STR_FUNC_TERM;
8805 }
8806 }
8807
8808 tokfix(p);
8809 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8810 set_yylval_str(lit);
8811 flush_string_content(p, enc, 0);
8812
8813 return tSTRING_CONTENT;
8814}
8815
8816static enum yytokentype
8817heredoc_identifier(struct parser_params *p)
8818{
8819 /*
8820 * term_len is length of `<<"END"` except `END`,
8821 * in this case term_len is 4 (<, <, " and ").
8822 */
8823 long len, offset = p->lex.pcur - p->lex.pbeg;
8824 int c = nextc(p), term, func = 0, quote = 0;
8825 enum yytokentype token = tSTRING_BEG;
8826 int indent = 0;
8827
8828 if (c == '-') {
8829 c = nextc(p);
8830 func = STR_FUNC_INDENT;
8831 offset++;
8832 }
8833 else if (c == '~') {
8834 c = nextc(p);
8835 func = STR_FUNC_INDENT;
8836 offset++;
8837 indent = INT_MAX;
8838 }
8839 switch (c) {
8840 case '\'':
8841 func |= str_squote; goto quoted;
8842 case '"':
8843 func |= str_dquote; goto quoted;
8844 case '`':
8845 token = tXSTRING_BEG;
8846 func |= str_xquote; goto quoted;
8847
8848 quoted:
8849 quote++;
8850 offset++;
8851 term = c;
8852 len = 0;
8853 while ((c = nextc(p)) != term) {
8854 if (c == -1 || c == '\r' || c == '\n') {
8855 yyerror0("unterminated here document identifier");
8856 return -1;
8857 }
8858 }
8859 break;
8860
8861 default:
8862 if (!parser_is_identchar(p)) {
8863 pushback(p, c);
8864 if (func & STR_FUNC_INDENT) {
8865 pushback(p, indent > 0 ? '~' : '-');
8866 }
8867 return 0;
8868 }
8869 func |= str_dquote;
8870 do {
8871 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8872 if (n < 0) return 0;
8873 p->lex.pcur += --n;
8874 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8875 pushback(p, c);
8876 break;
8877 }
8878
8879 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8880 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8881 yyerror0("too long here document identifier");
8882 dispatch_scan_event(p, tHEREDOC_BEG);
8883 lex_goto_eol(p);
8884
8885 p->lex.strterm = new_heredoc(p);
8886 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8887 here->offset = offset;
8888 here->sourceline = p->ruby_sourceline;
8889 here->length = (unsigned)len;
8890 here->quote = quote;
8891 here->func = func;
8892 here->lastline = p->lex.lastline;
8893
8894 token_flush(p);
8895 p->heredoc_indent = indent;
8896 p->heredoc_line_indent = 0;
8897 return token;
8898}
8899
8900static void
8901heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8902{
8903 rb_parser_string_t *line;
8904 rb_strterm_t *term = p->lex.strterm;
8905
8906 p->lex.strterm = 0;
8907 line = here->lastline;
8908 p->lex.lastline = line;
8909 p->lex.pbeg = PARSER_STRING_PTR(line);
8910 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8911 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8912 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8913 p->heredoc_end = p->ruby_sourceline;
8914 p->ruby_sourceline = (int)here->sourceline;
8915 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINTOR;
8916 p->eofp = 0;
8917 xfree(term);
8918}
8919
8920static int
8921dedent_string_column(const char *str, long len, int width)
8922{
8923 int i, col = 0;
8924
8925 for (i = 0; i < len && col < width; i++) {
8926 if (str[i] == ' ') {
8927 col++;
8928 }
8929 else if (str[i] == '\t') {
8930 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8931 if (n > width) break;
8932 col = n;
8933 }
8934 else {
8935 break;
8936 }
8937 }
8938
8939 return i;
8940}
8941
8942static int
8943dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8944{
8945 char *str;
8946 long len;
8947 int i;
8948
8949 len = PARSER_STRING_LEN(string);
8950 str = PARSER_STRING_PTR(string);
8951
8952 i = dedent_string_column(str, len, width);
8953 if (!i) return 0;
8954
8955 rb_parser_str_modify(string);
8956 str = PARSER_STRING_PTR(string);
8957 if (PARSER_STRING_LEN(string) != len)
8958 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8959 MEMMOVE(str, str + i, char, len - i);
8960 rb_parser_str_set_len(p, string, len - i);
8961 return i;
8962}
8963
8964static NODE *
8965heredoc_dedent(struct parser_params *p, NODE *root)
8966{
8967 NODE *node, *str_node, *prev_node;
8968 int indent = p->heredoc_indent;
8969 rb_parser_string_t *prev_lit = 0;
8970
8971 if (indent <= 0) return root;
8972 if (!root) return root;
8973
8974 prev_node = node = str_node = root;
8975 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8976
8977 while (str_node) {
8978 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8979 if (nd_fl_newline(str_node)) {
8980 dedent_string(p, lit, indent);
8981 }
8982 if (!prev_lit) {
8983 prev_lit = lit;
8984 }
8985 else if (!literal_concat0(p, prev_lit, lit)) {
8986 return 0;
8987 }
8988 else {
8989 NODE *end = RNODE_LIST(node)->as.nd_end;
8990 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8991 if (!node) {
8992 if (nd_type_p(prev_node, NODE_DSTR))
8993 nd_set_type(prev_node, NODE_STR);
8994 break;
8995 }
8996 RNODE_LIST(node)->as.nd_end = end;
8997 goto next_str;
8998 }
8999
9000 str_node = 0;
9001 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
9002 next_str:
9003 if (!nd_type_p(node, NODE_LIST)) break;
9004 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
9005 enum node_type type = nd_type(str_node);
9006 if (type == NODE_STR || type == NODE_DSTR) break;
9007 prev_lit = 0;
9008 str_node = 0;
9009 }
9010 }
9011 }
9012 return root;
9013}
9014
9015static int
9016whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
9017{
9018 const char *beg = p->lex.pbeg;
9019 const char *ptr = p->lex.pend;
9020
9021 if (ptr - beg < len) return FALSE;
9022 if (ptr > beg && ptr[-1] == '\n') {
9023 if (--ptr > beg && ptr[-1] == '\r') --ptr;
9024 if (ptr - beg < len) return FALSE;
9025 }
9026 if (strncmp(eos, ptr -= len, len)) return FALSE;
9027 if (indent) {
9028 while (beg < ptr && ISSPACE(*beg)) beg++;
9029 }
9030 return beg == ptr;
9031}
9032
9033static int
9034word_match_p(struct parser_params *p, const char *word, long len)
9035{
9036 if (strncmp(p->lex.pcur, word, len)) return 0;
9037 if (lex_eol_n_p(p, len)) return 1;
9038 int c = (unsigned char)p->lex.pcur[len];
9039 if (ISSPACE(c)) return 1;
9040 switch (c) {
9041 case '\0': case '\004': case '\032': return 1;
9042 }
9043 return 0;
9044}
9045
9046#define NUM_SUFFIX_R (1<<0)
9047#define NUM_SUFFIX_I (1<<1)
9048#define NUM_SUFFIX_ALL 3
9049
9050static int
9051number_literal_suffix(struct parser_params *p, int mask)
9052{
9053 int c, result = 0;
9054 const char *lastp = p->lex.pcur;
9055
9056 while ((c = nextc(p)) != -1) {
9057 if ((mask & NUM_SUFFIX_I) && c == 'i') {
9058 result |= (mask & NUM_SUFFIX_I);
9059 mask &= ~NUM_SUFFIX_I;
9060 /* r after i, rational of complex is disallowed */
9061 mask &= ~NUM_SUFFIX_R;
9062 continue;
9063 }
9064 if ((mask & NUM_SUFFIX_R) && c == 'r') {
9065 result |= (mask & NUM_SUFFIX_R);
9066 mask &= ~NUM_SUFFIX_R;
9067 continue;
9068 }
9069 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
9070 p->lex.pcur = lastp;
9071 literal_flush(p, p->lex.pcur);
9072 return 0;
9073 }
9074 pushback(p, c);
9075 break;
9076 }
9077 return result;
9078}
9079
9080static enum yytokentype
9081set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
9082{
9083 enum rb_numeric_type numeric_type = integer_literal;
9084
9085 if (type == tFLOAT) {
9086 numeric_type = float_literal;
9087 }
9088
9089 if (suffix & NUM_SUFFIX_R) {
9090 type = tRATIONAL;
9091 numeric_type = rational_literal;
9092 }
9093 if (suffix & NUM_SUFFIX_I) {
9094 type = tIMAGINARY;
9095 }
9096
9097 switch (type) {
9098 case tINTEGER:
9099 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
9100 break;
9101 case tFLOAT:
9102 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
9103 break;
9104 case tRATIONAL:
9105 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
9106 break;
9107 case tIMAGINARY:
9108 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
9109 (void)numeric_type; /* for ripper */
9110 break;
9111 default:
9112 rb_bug("unexpected token: %d", type);
9113 }
9114 SET_LEX_STATE(EXPR_END);
9115 return type;
9116}
9117
9118#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
9119static void
9120parser_dispatch_heredoc_end(struct parser_params *p, int line)
9121{
9122 if (has_delayed_token(p))
9123 dispatch_delayed_token(p, tSTRING_CONTENT);
9124
9125#ifdef RIPPER
9126 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
9127 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9128#else
9129 if (p->keep_tokens) {
9130 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9131 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9132 parser_append_tokens(p, str, tHEREDOC_END, line);
9133 }
9134#endif
9135
9136 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9137 lex_goto_eol(p);
9138 token_flush(p);
9139}
9140
9141static enum yytokentype
9142here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9143{
9144 int c, func, indent = 0;
9145 const char *eos, *ptr, *ptr_end;
9146 long len;
9147 rb_parser_string_t *str = 0;
9148 rb_encoding *enc = p->enc;
9149 rb_encoding *base_enc = 0;
9150 int bol;
9151#ifdef RIPPER
9152 VALUE s_value;
9153#endif
9154
9155 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9156 len = here->length;
9157 indent = (func = here->func) & STR_FUNC_INDENT;
9158
9159 if ((c = nextc(p)) == -1) {
9160 error:
9161#ifdef RIPPER
9162 if (!has_delayed_token(p)) {
9163 dispatch_scan_event(p, tSTRING_CONTENT);
9164 }
9165 else {
9166 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9167 if (!(func & STR_FUNC_REGEXP)) {
9168 int cr = ENC_CODERANGE_UNKNOWN;
9169 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9170 if (cr != ENC_CODERANGE_7BIT &&
9171 rb_is_usascii_enc(p->enc) &&
9172 enc != rb_utf8_encoding()) {
9173 enc = rb_ascii8bit_encoding();
9174 }
9175 }
9176 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9177 }
9178 dispatch_delayed_token(p, tSTRING_CONTENT);
9179 }
9180 lex_goto_eol(p);
9181#endif
9182 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9183 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9184 (int)len, eos);
9185 token_flush(p);
9186 SET_LEX_STATE(EXPR_END);
9187 return tSTRING_END;
9188 }
9189 bol = was_bol(p);
9190 if (!bol) {
9191 /* not beginning of line, cannot be the terminator */
9192 }
9193 else if (p->heredoc_line_indent == -1) {
9194 /* `heredoc_line_indent == -1` means
9195 * - "after an interpolation in the same line", or
9196 * - "in a continuing line"
9197 */
9198 p->heredoc_line_indent = 0;
9199 }
9200 else if (whole_match_p(p, eos, len, indent)) {
9201 dispatch_heredoc_end(p);
9202 restore:
9203 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9204 token_flush(p);
9205 SET_LEX_STATE(EXPR_END);
9206 return tSTRING_END;
9207 }
9208
9209 if (!(func & STR_FUNC_EXPAND)) {
9210 do {
9211 ptr = PARSER_STRING_PTR(p->lex.lastline);
9212 ptr_end = p->lex.pend;
9213 if (ptr_end > ptr) {
9214 switch (ptr_end[-1]) {
9215 case '\n':
9216 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9217 ptr_end++;
9218 break;
9219 }
9220 case '\r':
9221 --ptr_end;
9222 }
9223 }
9224
9225 if (p->heredoc_indent > 0) {
9226 long i = 0;
9227 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9228 i++;
9229 p->heredoc_line_indent = 0;
9230 }
9231
9232 if (str)
9233 parser_str_cat(str, ptr, ptr_end - ptr);
9234 else
9235 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9236 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9237 lex_goto_eol(p);
9238 if (p->heredoc_indent > 0) {
9239 goto flush_str;
9240 }
9241 if (nextc(p) == -1) {
9242 if (str) {
9243 rb_parser_string_free(p, str);
9244 str = 0;
9245 }
9246 goto error;
9247 }
9248 } while (!whole_match_p(p, eos, len, indent));
9249 }
9250 else {
9251 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9252 newtok(p);
9253 if (c == '#') {
9254 enum yytokentype t = parser_peek_variable_name(p);
9255 if (p->heredoc_line_indent != -1) {
9256 if (p->heredoc_indent > p->heredoc_line_indent) {
9257 p->heredoc_indent = p->heredoc_line_indent;
9258 }
9259 p->heredoc_line_indent = -1;
9260 }
9261 if (t) return t;
9262 tokadd(p, '#');
9263 c = nextc(p);
9264 }
9265 do {
9266 pushback(p, c);
9267 enc = p->enc;
9268 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9269 if (p->eofp) goto error;
9270 goto restore;
9271 }
9272 if (c != '\n') {
9273 if (c == '\\') p->heredoc_line_indent = -1;
9274 flush:
9275 str = STR_NEW3(tok(p), toklen(p), enc, func);
9276 flush_str:
9277 set_yylval_str(str);
9278#ifndef RIPPER
9279 if (bol) nd_set_fl_newline(yylval.node);
9280#endif
9281 flush_string_content(p, enc, 0);
9282 return tSTRING_CONTENT;
9283 }
9284 tokadd(p, nextc(p));
9285 if (p->heredoc_indent > 0) {
9286 lex_goto_eol(p);
9287 goto flush;
9288 }
9289 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9290 if ((c = nextc(p)) == -1) goto error;
9291 } while (!whole_match_p(p, eos, len, indent));
9292 str = STR_NEW3(tok(p), toklen(p), enc, func);
9293 }
9294 dispatch_heredoc_end(p);
9295 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9296 token_flush(p);
9297 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9298#ifdef RIPPER
9299 /* Preserve s_value for set_yylval_str */
9300 s_value = p->s_value;
9301#endif
9302 set_yylval_str(str);
9303#ifdef RIPPER
9304 set_parser_s_value(s_value);
9305#endif
9306
9307#ifndef RIPPER
9308 if (bol) nd_set_fl_newline(yylval.node);
9309#endif
9310 return tSTRING_CONTENT;
9311}
9312
9313#include "lex.c"
9314
9315static int
9316arg_ambiguous(struct parser_params *p, char c)
9317{
9318#ifndef RIPPER
9319 if (c == '/') {
9320 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after '%c' operator", WARN_I(c));
9321 }
9322 else {
9323 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9324 }
9325#else
9326 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9327#endif
9328 return TRUE;
9329}
9330
9331/* returns true value if formal argument error;
9332 * Qtrue, or error message if ripper */
9333static VALUE
9334formal_argument_error(struct parser_params *p, ID id)
9335{
9336 switch (id_type(id)) {
9337 case ID_LOCAL:
9338 break;
9339#ifndef RIPPER
9340# define ERR(mesg) (yyerror0(mesg), Qtrue)
9341#else
9342# define ERR(mesg) WARN_S(mesg)
9343#endif
9344 case ID_CONST:
9345 return ERR("formal argument cannot be a constant");
9346 case ID_INSTANCE:
9347 return ERR("formal argument cannot be an instance variable");
9348 case ID_GLOBAL:
9349 return ERR("formal argument cannot be a global variable");
9350 case ID_CLASS:
9351 return ERR("formal argument cannot be a class variable");
9352 default:
9353 return ERR("formal argument must be local variable");
9354#undef ERR
9355 }
9356 shadowing_lvar(p, id);
9357
9358 return Qfalse;
9359}
9360
9361static int
9362lvar_defined(struct parser_params *p, ID id)
9363{
9364 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9365}
9366
9367/* emacsen -*- hack */
9368static long
9369parser_encode_length(struct parser_params *p, const char *name, long len)
9370{
9371 long nlen;
9372
9373 if (len > 5 && name[nlen = len - 5] == '-') {
9374 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9375 return nlen;
9376 }
9377 if (len > 4 && name[nlen = len - 4] == '-') {
9378 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9379 return nlen;
9380 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9381 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9382 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9383 return nlen;
9384 }
9385 return len;
9386}
9387
9388static void
9389parser_set_encode(struct parser_params *p, const char *name)
9390{
9391 rb_encoding *enc;
9392 VALUE excargs[3];
9393 int idx = 0;
9394
9395 const char *wrong = 0;
9396 switch (*name) {
9397 case 'e': case 'E': wrong = "external"; break;
9398 case 'i': case 'I': wrong = "internal"; break;
9399 case 'f': case 'F': wrong = "filesystem"; break;
9400 case 'l': case 'L': wrong = "locale"; break;
9401 }
9402 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9403 idx = rb_enc_find_index(name);
9404 if (idx < 0) {
9405 unknown:
9406 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9407 error:
9408 excargs[0] = rb_eArgError;
9409 excargs[2] = rb_make_backtrace();
9410 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9411 VALUE exc = rb_make_exception(3, excargs);
9412 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9413
9414 rb_ast_free(p->ast);
9415 p->ast = NULL;
9416
9417 rb_exc_raise(exc);
9418 }
9419 enc = rb_enc_from_index(idx);
9420 if (!rb_enc_asciicompat(enc)) {
9421 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9422 goto error;
9423 }
9424 p->enc = enc;
9425#ifndef RIPPER
9426 if (p->debug_lines) {
9427 long i;
9428 for (i = 0; i < p->debug_lines->len; i++) {
9429 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9430 }
9431 }
9432#endif
9433}
9434
9435static bool
9436comment_at_top(struct parser_params *p)
9437{
9438 if (p->token_seen) return false;
9439 return (p->line_count == (p->has_shebang ? 2 : 1));
9440}
9441
9442typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9443typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9444
9445static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9446
9447static void
9448magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9449{
9450 if (!comment_at_top(p)) {
9451 return;
9452 }
9453 parser_set_encode(p, val);
9454}
9455
9456static int
9457parser_get_bool(struct parser_params *p, const char *name, const char *val)
9458{
9459 switch (*val) {
9460 case 't': case 'T':
9461 if (STRCASECMP(val, "true") == 0) {
9462 return TRUE;
9463 }
9464 break;
9465 case 'f': case 'F':
9466 if (STRCASECMP(val, "false") == 0) {
9467 return FALSE;
9468 }
9469 break;
9470 }
9471 return parser_invalid_pragma_value(p, name, val);
9472}
9473
9474static int
9475parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9476{
9477 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9478 return -1;
9479}
9480
9481static void
9482parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9483{
9484 int b = parser_get_bool(p, name, val);
9485 if (b >= 0) p->token_info_enabled = b;
9486}
9487
9488static void
9489parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9490{
9491 int b;
9492
9493 if (p->token_seen) {
9494 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9495 return;
9496 }
9497
9498 b = parser_get_bool(p, name, val);
9499 if (b < 0) return;
9500
9501 p->frozen_string_literal = b;
9502}
9503
9504static void
9505parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9506{
9507 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9508 if (*s == ' ' || *s == '\t') continue;
9509 if (*s == '#') break;
9510 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9511 return;
9512 }
9513
9514 switch (*val) {
9515 case 'n': case 'N':
9516 if (STRCASECMP(val, "none") == 0) {
9517 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9518 return;
9519 }
9520 break;
9521 case 'l': case 'L':
9522 if (STRCASECMP(val, "literal") == 0) {
9523 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9524 return;
9525 }
9526 break;
9527 case 'e': case 'E':
9528 if (STRCASECMP(val, "experimental_copy") == 0) {
9529 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9530 return;
9531 }
9532 if (STRCASECMP(val, "experimental_everything") == 0) {
9533 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9534 return;
9535 }
9536 break;
9537 }
9538 parser_invalid_pragma_value(p, name, val);
9539}
9540
9541# if WARN_PAST_SCOPE
9542static void
9543parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9544{
9545 int b = parser_get_bool(p, name, val);
9546 if (b >= 0) p->past_scope_enabled = b;
9547}
9548# endif
9549
9550struct magic_comment {
9551 const char *name;
9552 rb_magic_comment_setter_t func;
9553 rb_magic_comment_length_t length;
9554};
9555
9556static const struct magic_comment magic_comments[] = {
9557 {"coding", magic_comment_encoding, parser_encode_length},
9558 {"encoding", magic_comment_encoding, parser_encode_length},
9559 {"frozen_string_literal", parser_set_frozen_string_literal},
9560 {"shareable_constant_value", parser_set_shareable_constant_value},
9561 {"warn_indent", parser_set_token_info},
9562# if WARN_PAST_SCOPE
9563 {"warn_past_scope", parser_set_past_scope},
9564# endif
9565};
9566
9567static const char *
9568magic_comment_marker(const char *str, long len)
9569{
9570 long i = 2;
9571
9572 while (i < len) {
9573 switch (str[i]) {
9574 case '-':
9575 if (str[i-1] == '*' && str[i-2] == '-') {
9576 return str + i + 1;
9577 }
9578 i += 2;
9579 break;
9580 case '*':
9581 if (i + 1 >= len) return 0;
9582 if (str[i+1] != '-') {
9583 i += 4;
9584 }
9585 else if (str[i-1] != '-') {
9586 i += 2;
9587 }
9588 else {
9589 return str + i + 2;
9590 }
9591 break;
9592 default:
9593 i += 3;
9594 break;
9595 }
9596 }
9597 return 0;
9598}
9599
9600static int
9601parser_magic_comment(struct parser_params *p, const char *str, long len)
9602{
9603 int indicator = 0;
9604 VALUE name = 0, val = 0;
9605 const char *beg, *end, *vbeg, *vend;
9606#define str_copy(_s, _p, _n) ((_s) \
9607 ? (void)(rb_str_resize((_s), (_n)), \
9608 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9609 : (void)((_s) = STR_NEW((_p), (_n))))
9610
9611 if (len <= 7) return FALSE;
9612 if (!!(beg = magic_comment_marker(str, len))) {
9613 if (!(end = magic_comment_marker(beg, str + len - beg)))
9614 return FALSE;
9615 indicator = TRUE;
9616 str = beg;
9617 len = end - beg - 3;
9618 }
9619
9620 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9621 while (len > 0) {
9622 const struct magic_comment *mc = magic_comments;
9623 char *s;
9624 int i;
9625 long n = 0;
9626
9627 for (; len > 0 && *str; str++, --len) {
9628 switch (*str) {
9629 case '\'': case '"': case ':': case ';':
9630 continue;
9631 }
9632 if (!ISSPACE(*str)) break;
9633 }
9634 for (beg = str; len > 0; str++, --len) {
9635 switch (*str) {
9636 case '\'': case '"': case ':': case ';':
9637 break;
9638 default:
9639 if (ISSPACE(*str)) break;
9640 continue;
9641 }
9642 break;
9643 }
9644 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9645 if (!len) break;
9646 if (*str != ':') {
9647 if (!indicator) return FALSE;
9648 continue;
9649 }
9650
9651 do str++; while (--len > 0 && ISSPACE(*str));
9652 if (!len) break;
9653 const char *tok_beg = str;
9654 if (*str == '"') {
9655 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9656 if (*str == '\\') {
9657 --len;
9658 ++str;
9659 }
9660 }
9661 vend = str;
9662 if (len) {
9663 --len;
9664 ++str;
9665 }
9666 }
9667 else {
9668 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9669 vend = str;
9670 }
9671 const char *tok_end = str;
9672 if (indicator) {
9673 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9674 }
9675 else {
9676 while (len > 0 && (ISSPACE(*str))) --len, str++;
9677 if (len) return FALSE;
9678 }
9679
9680 n = end - beg;
9681 str_copy(name, beg, n);
9682 s = RSTRING_PTR(name);
9683 for (i = 0; i < n; ++i) {
9684 if (s[i] == '-') s[i] = '_';
9685 }
9686 do {
9687 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9688 n = vend - vbeg;
9689 if (mc->length) {
9690 n = (*mc->length)(p, vbeg, n);
9691 }
9692 str_copy(val, vbeg, n);
9693 p->lex.ptok = tok_beg;
9694 p->lex.pcur = tok_end;
9695 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9696 break;
9697 }
9698 } while (++mc < magic_comments + numberof(magic_comments));
9699#ifdef RIPPER
9700 str_copy(val, vbeg, vend - vbeg);
9701 dispatch2(magic_comment, name, val);
9702#endif
9703 }
9704
9705 return TRUE;
9706}
9707
9708static void
9709set_file_encoding(struct parser_params *p, const char *str, const char *send)
9710{
9711 int sep = 0;
9712 const char *beg = str;
9713 VALUE s;
9714
9715 for (;;) {
9716 if (send - str <= 6) return;
9717 switch (str[6]) {
9718 case 'C': case 'c': str += 6; continue;
9719 case 'O': case 'o': str += 5; continue;
9720 case 'D': case 'd': str += 4; continue;
9721 case 'I': case 'i': str += 3; continue;
9722 case 'N': case 'n': str += 2; continue;
9723 case 'G': case 'g': str += 1; continue;
9724 case '=': case ':':
9725 sep = 1;
9726 str += 6;
9727 break;
9728 default:
9729 str += 6;
9730 if (ISSPACE(*str)) break;
9731 continue;
9732 }
9733 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9734 sep = 0;
9735 }
9736 for (;;) {
9737 do {
9738 if (++str >= send) return;
9739 } while (ISSPACE(*str));
9740 if (sep) break;
9741 if (*str != '=' && *str != ':') return;
9742 sep = 1;
9743 str++;
9744 }
9745 beg = str;
9746 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9747 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9748 p->lex.ptok = beg;
9749 p->lex.pcur = str;
9750 parser_set_encode(p, RSTRING_PTR(s));
9751 rb_str_resize(s, 0);
9752}
9753
9754static void
9755parser_prepare(struct parser_params *p)
9756{
9757 int c = nextc0(p, FALSE);
9758 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9759 switch (c) {
9760 case '#':
9761 if (peek(p, '!')) p->has_shebang = 1;
9762 break;
9763 case 0xef: /* UTF-8 BOM marker */
9764 if (!lex_eol_n_p(p, 2) &&
9765 (unsigned char)p->lex.pcur[0] == 0xbb &&
9766 (unsigned char)p->lex.pcur[1] == 0xbf) {
9767 p->enc = rb_utf8_encoding();
9768 p->lex.pcur += 2;
9769#ifndef RIPPER
9770 if (p->debug_lines) {
9771 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9772 }
9773#endif
9774 p->lex.pbeg = p->lex.pcur;
9775 token_flush(p);
9776 return;
9777 }
9778 break;
9779 case -1: /* end of script. */
9780 return;
9781 }
9782 pushback(p, c);
9783 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9784}
9785
9786#ifndef RIPPER
9787#define ambiguous_operator(tok, op, syn) ( \
9788 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9789 rb_warning0("even though it seems like "syn""))
9790#else
9791#define ambiguous_operator(tok, op, syn) \
9792 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9793#endif
9794#define warn_balanced(tok, op, syn) ((void) \
9795 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9796 space_seen && !ISSPACE(c) && \
9797 (ambiguous_operator(tok, op, syn), 0)), \
9798 (enum yytokentype)(tok))
9799
9800static enum yytokentype
9801no_digits(struct parser_params *p)
9802{
9803 yyerror0("numeric literal without digits");
9804 if (peek(p, '_')) nextc(p);
9805 /* dummy 0, for tUMINUS_NUM at numeric */
9806 return set_number_literal(p, tINTEGER, 0, 10, 0);
9807}
9808
9809static enum yytokentype
9810parse_numeric(struct parser_params *p, int c)
9811{
9812 int is_float, seen_point, seen_e, nondigit;
9813 int suffix;
9814
9815 is_float = seen_point = seen_e = nondigit = 0;
9816 SET_LEX_STATE(EXPR_END);
9817 newtok(p);
9818 if (c == '-' || c == '+') {
9819 tokadd(p, c);
9820 c = nextc(p);
9821 }
9822 if (c == '0') {
9823 int start = toklen(p);
9824 c = nextc(p);
9825 if (c == 'x' || c == 'X') {
9826 /* hexadecimal */
9827 c = nextc(p);
9828 if (c != -1 && ISXDIGIT(c)) {
9829 do {
9830 if (c == '_') {
9831 if (nondigit) break;
9832 nondigit = c;
9833 continue;
9834 }
9835 if (!ISXDIGIT(c)) break;
9836 nondigit = 0;
9837 tokadd(p, c);
9838 } while ((c = nextc(p)) != -1);
9839 }
9840 pushback(p, c);
9841 tokfix(p);
9842 if (toklen(p) == start) {
9843 return no_digits(p);
9844 }
9845 else if (nondigit) goto trailing_uc;
9846 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9847 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9848 }
9849 if (c == 'b' || c == 'B') {
9850 /* binary */
9851 c = nextc(p);
9852 if (c == '0' || c == '1') {
9853 do {
9854 if (c == '_') {
9855 if (nondigit) break;
9856 nondigit = c;
9857 continue;
9858 }
9859 if (c != '0' && c != '1') break;
9860 nondigit = 0;
9861 tokadd(p, c);
9862 } while ((c = nextc(p)) != -1);
9863 }
9864 pushback(p, c);
9865 tokfix(p);
9866 if (toklen(p) == start) {
9867 return no_digits(p);
9868 }
9869 else if (nondigit) goto trailing_uc;
9870 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9871 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9872 }
9873 if (c == 'd' || c == 'D') {
9874 /* decimal */
9875 c = nextc(p);
9876 if (c != -1 && ISDIGIT(c)) {
9877 do {
9878 if (c == '_') {
9879 if (nondigit) break;
9880 nondigit = c;
9881 continue;
9882 }
9883 if (!ISDIGIT(c)) break;
9884 nondigit = 0;
9885 tokadd(p, c);
9886 } while ((c = nextc(p)) != -1);
9887 }
9888 pushback(p, c);
9889 tokfix(p);
9890 if (toklen(p) == start) {
9891 return no_digits(p);
9892 }
9893 else if (nondigit) goto trailing_uc;
9894 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9895 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9896 }
9897 if (c == '_') {
9898 /* 0_0 */
9899 goto octal_number;
9900 }
9901 if (c == 'o' || c == 'O') {
9902 /* prefixed octal */
9903 c = nextc(p);
9904 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9905 tokfix(p);
9906 return no_digits(p);
9907 }
9908 }
9909 if (c >= '0' && c <= '7') {
9910 /* octal */
9911 octal_number:
9912 do {
9913 if (c == '_') {
9914 if (nondigit) break;
9915 nondigit = c;
9916 continue;
9917 }
9918 if (c < '0' || c > '9') break;
9919 if (c > '7') goto invalid_octal;
9920 nondigit = 0;
9921 tokadd(p, c);
9922 } while ((c = nextc(p)) != -1);
9923 if (toklen(p) > start) {
9924 pushback(p, c);
9925 tokfix(p);
9926 if (nondigit) goto trailing_uc;
9927 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9928 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9929 }
9930 if (nondigit) {
9931 pushback(p, c);
9932 goto trailing_uc;
9933 }
9934 }
9935 if (c > '7' && c <= '9') {
9936 invalid_octal:
9937 yyerror0("Invalid octal digit");
9938 }
9939 else if (c == '.' || c == 'e' || c == 'E') {
9940 tokadd(p, '0');
9941 }
9942 else {
9943 pushback(p, c);
9944 tokfix(p);
9945 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9946 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9947 }
9948 }
9949
9950 for (;;) {
9951 switch (c) {
9952 case '0': case '1': case '2': case '3': case '4':
9953 case '5': case '6': case '7': case '8': case '9':
9954 nondigit = 0;
9955 tokadd(p, c);
9956 break;
9957
9958 case '.':
9959 if (nondigit) goto trailing_uc;
9960 if (seen_point || seen_e) {
9961 goto decode_num;
9962 }
9963 else {
9964 int c0 = nextc(p);
9965 if (c0 == -1 || !ISDIGIT(c0)) {
9966 pushback(p, c0);
9967 goto decode_num;
9968 }
9969 c = c0;
9970 }
9971 seen_point = toklen(p);
9972 tokadd(p, '.');
9973 tokadd(p, c);
9974 is_float++;
9975 nondigit = 0;
9976 break;
9977
9978 case 'e':
9979 case 'E':
9980 if (nondigit) {
9981 pushback(p, c);
9982 c = nondigit;
9983 goto decode_num;
9984 }
9985 if (seen_e) {
9986 goto decode_num;
9987 }
9988 nondigit = c;
9989 c = nextc(p);
9990 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9991 pushback(p, c);
9992 c = nondigit;
9993 nondigit = 0;
9994 goto decode_num;
9995 }
9996 tokadd(p, nondigit);
9997 seen_e++;
9998 is_float++;
9999 tokadd(p, c);
10000 nondigit = (c == '-' || c == '+') ? c : 0;
10001 break;
10002
10003 case '_': /* `_' in number just ignored */
10004 if (nondigit) goto decode_num;
10005 nondigit = c;
10006 break;
10007
10008 default:
10009 goto decode_num;
10010 }
10011 c = nextc(p);
10012 }
10013
10014 decode_num:
10015 pushback(p, c);
10016 if (nondigit) {
10017 trailing_uc:
10018 literal_flush(p, p->lex.pcur - 1);
10019 YYLTYPE loc = RUBY_INIT_YYLLOC();
10020 compile_error(p, "trailing '%c' in number", nondigit);
10021 parser_show_error_line(p, &loc);
10022 }
10023 tokfix(p);
10024 if (is_float) {
10025 enum yytokentype type = tFLOAT;
10026
10027 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
10028 if (suffix & NUM_SUFFIX_R) {
10029 type = tRATIONAL;
10030 }
10031 else {
10032 strtod(tok(p), 0);
10033 if (errno == ERANGE) {
10034 rb_warning1("Float %s out of range", WARN_S(tok(p)));
10035 errno = 0;
10036 }
10037 }
10038 return set_number_literal(p, type, suffix, 0, seen_point);
10039 }
10040 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
10041 return set_number_literal(p, tINTEGER, suffix, 10, 0);
10042}
10043
10044static enum yytokentype
10045parse_qmark(struct parser_params *p, int space_seen)
10046{
10047 rb_encoding *enc;
10048 register int c;
10049 rb_parser_string_t *lit;
10050 const char *start = p->lex.pcur;
10051
10052 if (IS_END()) {
10053 SET_LEX_STATE(EXPR_VALUE);
10054 return '?';
10055 }
10056 c = nextc(p);
10057 if (c == -1) {
10058 compile_error(p, "incomplete character syntax");
10059 return 0;
10060 }
10061 if (rb_enc_isspace(c, p->enc)) {
10062 if (!IS_ARG()) {
10063 int c2 = escaped_control_code(c);
10064 if (c2) {
10065 WARN_SPACE_CHAR(c2, "?");
10066 }
10067 }
10068 ternary:
10069 pushback(p, c);
10070 SET_LEX_STATE(EXPR_VALUE);
10071 return '?';
10072 }
10073 newtok(p);
10074 enc = p->enc;
10075 int w = parser_precise_mbclen(p, start);
10076 if (is_identchar(p, start, p->lex.pend, p->enc) &&
10077 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
10078 if (space_seen) {
10079 const char *ptr = start;
10080 do {
10081 int n = parser_precise_mbclen(p, ptr);
10082 if (n < 0) return -1;
10083 ptr += n;
10084 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
10085 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
10086 " a conditional operator, put a space after '?'",
10087 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
10088 }
10089 goto ternary;
10090 }
10091 else if (c == '\\') {
10092 if (peek(p, 'u')) {
10093 nextc(p);
10094 enc = rb_utf8_encoding();
10095 tokadd_utf8(p, &enc, -1, 0, 0);
10096 }
10097 else if (!ISASCII(c = peekc(p)) && c != -1) {
10098 nextc(p);
10099 if (tokadd_mbchar(p, c) == -1) return 0;
10100 }
10101 else {
10102 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
10103 tokadd(p, c);
10104 }
10105 }
10106 else {
10107 if (tokadd_mbchar(p, c) == -1) return 0;
10108 }
10109 tokfix(p);
10110 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
10111 set_yylval_str(lit);
10112 SET_LEX_STATE(EXPR_END);
10113 return tCHAR;
10114}
10115
10116static enum yytokentype
10117parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
10118{
10119 register int c;
10120 const char *ptok = p->lex.pcur;
10121
10122 if (IS_BEG()) {
10123 int term;
10124 int paren;
10125
10126 c = nextc(p);
10127 quotation:
10128 if (c == -1) goto unterminated;
10129 if (!ISALNUM(c)) {
10130 term = c;
10131 if (!ISASCII(c)) goto unknown;
10132 c = 'Q';
10133 }
10134 else {
10135 term = nextc(p);
10136 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10137 unknown:
10138 pushback(p, term);
10139 c = parser_precise_mbclen(p, p->lex.pcur);
10140 if (c < 0) return 0;
10141 p->lex.pcur += c;
10142 yyerror0("unknown type of %string");
10143 return 0;
10144 }
10145 }
10146 if (term == -1) {
10147 unterminated:
10148 compile_error(p, "unterminated quoted string meets end of file");
10149 return 0;
10150 }
10151 paren = term;
10152 if (term == '(') term = ')';
10153 else if (term == '[') term = ']';
10154 else if (term == '{') term = '}';
10155 else if (term == '<') term = '>';
10156 else paren = 0;
10157
10158 p->lex.ptok = ptok-1;
10159 switch (c) {
10160 case 'Q':
10161 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10162 return tSTRING_BEG;
10163
10164 case 'q':
10165 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10166 return tSTRING_BEG;
10167
10168 case 'W':
10169 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10170 return tWORDS_BEG;
10171
10172 case 'w':
10173 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10174 return tQWORDS_BEG;
10175
10176 case 'I':
10177 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10178 return tSYMBOLS_BEG;
10179
10180 case 'i':
10181 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10182 return tQSYMBOLS_BEG;
10183
10184 case 'x':
10185 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10186 return tXSTRING_BEG;
10187
10188 case 'r':
10189 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10190 return tREGEXP_BEG;
10191
10192 case 's':
10193 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10194 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10195 return tSYMBEG;
10196
10197 default:
10198 yyerror0("unknown type of %string");
10199 return 0;
10200 }
10201 }
10202 if ((c = nextc(p)) == '=') {
10203 set_yylval_id('%');
10204 SET_LEX_STATE(EXPR_BEG);
10205 return tOP_ASGN;
10206 }
10207 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10208 goto quotation;
10209 }
10210 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10211 pushback(p, c);
10212 return warn_balanced('%', "%%", "string literal");
10213}
10214
10215static int
10216tokadd_ident(struct parser_params *p, int c)
10217{
10218 do {
10219 if (tokadd_mbchar(p, c) == -1) return -1;
10220 c = nextc(p);
10221 } while (parser_is_identchar(p));
10222 pushback(p, c);
10223 return 0;
10224}
10225
10226static ID
10227tokenize_ident(struct parser_params *p)
10228{
10229 ID ident = TOK_INTERN();
10230
10231 set_yylval_name(ident);
10232
10233 return ident;
10234}
10235
10236static int
10237parse_numvar(struct parser_params *p)
10238{
10239 size_t len;
10240 int overflow;
10241 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10242 const unsigned long nth_ref_max =
10243 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10244 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10245 * turned into a Fixnum, in compile.c */
10246
10247 if (overflow || n > nth_ref_max) {
10248 /* compile_error()? */
10249 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10250 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10251 }
10252 else {
10253 return (int)n;
10254 }
10255}
10256
10257static enum yytokentype
10258parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10259{
10260 const char *ptr = p->lex.pcur;
10261 register int c;
10262
10263 SET_LEX_STATE(EXPR_END);
10264 p->lex.ptok = ptr - 1; /* from '$' */
10265 newtok(p);
10266 c = nextc(p);
10267 switch (c) {
10268 case '_': /* $_: last read line string */
10269 c = nextc(p);
10270 if (parser_is_identchar(p)) {
10271 tokadd(p, '$');
10272 tokadd(p, '_');
10273 break;
10274 }
10275 pushback(p, c);
10276 c = '_';
10277 /* fall through */
10278 case '~': /* $~: match-data */
10279 case '*': /* $*: argv */
10280 case '$': /* $$: pid */
10281 case '?': /* $?: last status */
10282 case '!': /* $!: error string */
10283 case '@': /* $@: error position */
10284 case '/': /* $/: input record separator */
10285 case '\\': /* $\: output record separator */
10286 case ';': /* $;: field separator */
10287 case ',': /* $,: output field separator */
10288 case '.': /* $.: last read line number */
10289 case '=': /* $=: ignorecase */
10290 case ':': /* $:: load path */
10291 case '<': /* $<: default input handle */
10292 case '>': /* $>: default output handle */
10293 case '\"': /* $": already loaded files */
10294 tokadd(p, '$');
10295 tokadd(p, c);
10296 goto gvar;
10297
10298 case '-':
10299 tokadd(p, '$');
10300 tokadd(p, c);
10301 c = nextc(p);
10302 if (parser_is_identchar(p)) {
10303 if (tokadd_mbchar(p, c) == -1) return 0;
10304 }
10305 else {
10306 pushback(p, c);
10307 pushback(p, '-');
10308 return '$';
10309 }
10310 gvar:
10311 tokenize_ident(p);
10312 return tGVAR;
10313
10314 case '&': /* $&: last match */
10315 case '`': /* $`: string before last match */
10316 case '\'': /* $': string after last match */
10317 case '+': /* $+: string matches last paren. */
10318 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10319 tokadd(p, '$');
10320 tokadd(p, c);
10321 goto gvar;
10322 }
10323 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10324 return tBACK_REF;
10325
10326 case '1': case '2': case '3':
10327 case '4': case '5': case '6':
10328 case '7': case '8': case '9':
10329 tokadd(p, '$');
10330 do {
10331 tokadd(p, c);
10332 c = nextc(p);
10333 } while (c != -1 && ISDIGIT(c));
10334 pushback(p, c);
10335 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10336 tokfix(p);
10337 c = parse_numvar(p);
10338 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10339 return tNTH_REF;
10340
10341 default:
10342 if (!parser_is_identchar(p)) {
10343 YYLTYPE loc = RUBY_INIT_YYLLOC();
10344 if (c == -1 || ISSPACE(c)) {
10345 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10346 }
10347 else {
10348 pushback(p, c);
10349 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10350 }
10351 parser_show_error_line(p, &loc);
10352 set_yylval_noname();
10353 return tGVAR;
10354 }
10355 /* fall through */
10356 case '0':
10357 tokadd(p, '$');
10358 }
10359
10360 if (tokadd_ident(p, c)) return 0;
10361 SET_LEX_STATE(EXPR_END);
10362 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10363 tokenize_ident(p);
10364 }
10365 else {
10366 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10367 set_yylval_noname();
10368 }
10369 return tGVAR;
10370}
10371
10372static bool
10373parser_numbered_param(struct parser_params *p, int n)
10374{
10375 if (n < 0) return false;
10376
10377 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10378 return false;
10379 }
10380 if (p->max_numparam == ORDINAL_PARAM) {
10381 compile_error(p, "ordinary parameter is defined");
10382 return false;
10383 }
10384 struct vtable *args = p->lvtbl->args;
10385 if (p->max_numparam < n) {
10386 p->max_numparam = n;
10387 }
10388 while (n > args->pos) {
10389 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10390 }
10391 return true;
10392}
10393
10394static enum yytokentype
10395parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10396{
10397 const char *ptr = p->lex.pcur;
10398 enum yytokentype result = tIVAR;
10399 register int c = nextc(p);
10400 YYLTYPE loc;
10401
10402 p->lex.ptok = ptr - 1; /* from '@' */
10403 newtok(p);
10404 tokadd(p, '@');
10405 if (c == '@') {
10406 result = tCVAR;
10407 tokadd(p, '@');
10408 c = nextc(p);
10409 }
10410 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10411 if (c == -1 || !parser_is_identchar(p)) {
10412 pushback(p, c);
10413 RUBY_SET_YYLLOC(loc);
10414 if (result == tIVAR) {
10415 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10416 }
10417 else {
10418 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10419 }
10420 parser_show_error_line(p, &loc);
10421 set_yylval_noname();
10422 SET_LEX_STATE(EXPR_END);
10423 return result;
10424 }
10425 else if (ISDIGIT(c)) {
10426 pushback(p, c);
10427 RUBY_SET_YYLLOC(loc);
10428 if (result == tIVAR) {
10429 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10430 }
10431 else {
10432 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10433 }
10434 parser_show_error_line(p, &loc);
10435 set_yylval_noname();
10436 SET_LEX_STATE(EXPR_END);
10437 return result;
10438 }
10439
10440 if (tokadd_ident(p, c)) return 0;
10441 tokenize_ident(p);
10442 return result;
10443}
10444
10445static enum yytokentype
10446parse_ident(struct parser_params *p, int c, int cmd_state)
10447{
10448 enum yytokentype result;
10449 bool is_ascii = true;
10450 const enum lex_state_e last_state = p->lex.state;
10451 ID ident;
10452 int enforce_keyword_end = 0;
10453
10454 do {
10455 if (!ISASCII(c)) is_ascii = false;
10456 if (tokadd_mbchar(p, c) == -1) return 0;
10457 c = nextc(p);
10458 } while (parser_is_identchar(p));
10459 if ((c == '!' || c == '?') && !peek(p, '=')) {
10460 result = tFID;
10461 tokadd(p, c);
10462 }
10463 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10464 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10465 result = tIDENTIFIER;
10466 tokadd(p, c);
10467 }
10468 else {
10469 result = tCONSTANT; /* assume provisionally */
10470 pushback(p, c);
10471 }
10472 tokfix(p);
10473
10474 if (IS_LABEL_POSSIBLE()) {
10475 if (IS_LABEL_SUFFIX(0)) {
10476 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10477 nextc(p);
10478 tokenize_ident(p);
10479 return tLABEL;
10480 }
10481 }
10482
10483#ifndef RIPPER
10484 if (peek_end_expect_token_locations(p)) {
10485 const rb_code_position_t *end_pos;
10486 int lineno, column;
10487 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10488
10489 end_pos = peek_end_expect_token_locations(p)->pos;
10490 lineno = end_pos->lineno;
10491 column = end_pos->column;
10492
10493 if (p->debug) {
10494 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10495 p->ruby_sourceline, beg_pos, lineno, column);
10496 }
10497
10498 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10499 const struct kwtable *kw;
10500
10501 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10502 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10503 enforce_keyword_end = 1;
10504 }
10505 }
10506 }
10507#endif
10508
10509 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10510 const struct kwtable *kw;
10511
10512 /* See if it is a reserved word. */
10513 kw = rb_reserved_word(tok(p), toklen(p));
10514 if (kw) {
10515 enum lex_state_e state = p->lex.state;
10516 if (IS_lex_state_for(state, EXPR_FNAME)) {
10517 SET_LEX_STATE(EXPR_ENDFN);
10518 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10519 return kw->id[0];
10520 }
10521 SET_LEX_STATE(kw->state);
10522 if (IS_lex_state(EXPR_BEG)) {
10523 p->command_start = TRUE;
10524 }
10525 if (kw->id[0] == keyword_do) {
10526 if (lambda_beginning_p()) {
10527 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10528 return keyword_do_LAMBDA;
10529 }
10530 if (COND_P()) return keyword_do_cond;
10531 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10532 return keyword_do_block;
10533 return keyword_do;
10534 }
10535 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10536 return kw->id[0];
10537 else {
10538 if (kw->id[0] != kw->id[1])
10539 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10540 return kw->id[1];
10541 }
10542 }
10543 }
10544
10545 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10546 if (cmd_state) {
10547 SET_LEX_STATE(EXPR_CMDARG);
10548 }
10549 else {
10550 SET_LEX_STATE(EXPR_ARG);
10551 }
10552 }
10553 else if (p->lex.state == EXPR_FNAME) {
10554 SET_LEX_STATE(EXPR_ENDFN);
10555 }
10556 else {
10557 SET_LEX_STATE(EXPR_END);
10558 }
10559
10560 ident = tokenize_ident(p);
10561 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10562 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10563 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10564 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10565 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10566 }
10567 return result;
10568}
10569
10570static void
10571warn_cr(struct parser_params *p)
10572{
10573 if (!p->cr_seen) {
10574 p->cr_seen = TRUE;
10575 /* carried over with p->lex.nextline for nextc() */
10576 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10577 }
10578}
10579
10580static enum yytokentype
10581parser_yylex(struct parser_params *p)
10582{
10583 register int c;
10584 int space_seen = 0;
10585 int cmd_state;
10586 int label;
10587 enum lex_state_e last_state;
10588 int fallthru = FALSE;
10589 int token_seen = p->token_seen;
10590
10591 if (p->lex.strterm) {
10592 if (strterm_is_heredoc(p->lex.strterm)) {
10593 token_flush(p);
10594 return here_document(p, &p->lex.strterm->u.heredoc);
10595 }
10596 else {
10597 token_flush(p);
10598 return parse_string(p, &p->lex.strterm->u.literal);
10599 }
10600 }
10601 cmd_state = p->command_start;
10602 p->command_start = FALSE;
10603 p->token_seen = TRUE;
10604#ifndef RIPPER
10605 token_flush(p);
10606#endif
10607 retry:
10608 last_state = p->lex.state;
10609 switch (c = nextc(p)) {
10610 case '\0': /* NUL */
10611 case '\004': /* ^D */
10612 case '\032': /* ^Z */
10613 case -1: /* end of script. */
10614 p->eofp = 1;
10615#ifndef RIPPER
10616 if (p->end_expect_token_locations) {
10617 pop_end_expect_token_locations(p);
10618 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10619 return tDUMNY_END;
10620 }
10621#endif
10622 /* Set location for end-of-input because dispatch_scan_event is not called. */
10623 RUBY_SET_YYLLOC(*p->yylloc);
10624 return END_OF_INPUT;
10625
10626 /* white spaces */
10627 case '\r':
10628 warn_cr(p);
10629 /* fall through */
10630 case ' ': case '\t': case '\f':
10631 case '\13': /* '\v' */
10632 space_seen = 1;
10633 while ((c = nextc(p))) {
10634 switch (c) {
10635 case '\r':
10636 warn_cr(p);
10637 /* fall through */
10638 case ' ': case '\t': case '\f':
10639 case '\13': /* '\v' */
10640 break;
10641 default:
10642 goto outofloop;
10643 }
10644 }
10645 outofloop:
10646 pushback(p, c);
10647 dispatch_scan_event(p, tSP);
10648#ifndef RIPPER
10649 token_flush(p);
10650#endif
10651 goto retry;
10652
10653 case '#': /* it's a comment */
10654 p->token_seen = token_seen;
10655 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10656 /* no magic_comment in shebang line */
10657 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10658 if (comment_at_top(p)) {
10659 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10660 }
10661 }
10662 p->lex.pcur = pcur, p->lex.ptok = ptok;
10663 lex_goto_eol(p);
10664 dispatch_scan_event(p, tCOMMENT);
10665 fallthru = TRUE;
10666 /* fall through */
10667 case '\n':
10668 p->token_seen = token_seen;
10669 rb_parser_string_t *prevline = p->lex.lastline;
10670 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10671 !IS_lex_state(EXPR_LABELED));
10672 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10673 if (!fallthru) {
10674 dispatch_scan_event(p, tIGNORED_NL);
10675 }
10676 fallthru = FALSE;
10677 if (!c && p->ctxt.in_kwarg) {
10678 goto normal_newline;
10679 }
10680 goto retry;
10681 }
10682 while (1) {
10683 switch (c = nextc(p)) {
10684 case ' ': case '\t': case '\f': case '\r':
10685 case '\13': /* '\v' */
10686 space_seen = 1;
10687 break;
10688 case '#':
10689 pushback(p, c);
10690 if (space_seen) {
10691 dispatch_scan_event(p, tSP);
10692 token_flush(p);
10693 }
10694 goto retry;
10695 case '&':
10696 case '.': {
10697 dispatch_delayed_token(p, tIGNORED_NL);
10698 if (peek(p, '.') == (c == '&')) {
10699 pushback(p, c);
10700 dispatch_scan_event(p, tSP);
10701 goto retry;
10702 }
10703 }
10704 default:
10705 p->ruby_sourceline--;
10706 p->lex.nextline = p->lex.lastline;
10707 set_lastline(p, prevline);
10708 case -1: /* EOF no decrement*/
10709 if (c == -1 && space_seen) {
10710 dispatch_scan_event(p, tSP);
10711 }
10712 lex_goto_eol(p);
10713 if (c != -1) {
10714 token_flush(p);
10715 RUBY_SET_YYLLOC(*p->yylloc);
10716 }
10717 goto normal_newline;
10718 }
10719 }
10720 normal_newline:
10721 p->command_start = TRUE;
10722 SET_LEX_STATE(EXPR_BEG);
10723 return '\n';
10724
10725 case '*':
10726 if ((c = nextc(p)) == '*') {
10727 if ((c = nextc(p)) == '=') {
10728 set_yylval_id(idPow);
10729 SET_LEX_STATE(EXPR_BEG);
10730 return tOP_ASGN;
10731 }
10732 pushback(p, c);
10733 if (IS_SPCARG(c)) {
10734 rb_warning0("'**' interpreted as argument prefix");
10735 c = tDSTAR;
10736 }
10737 else if (IS_BEG()) {
10738 c = tDSTAR;
10739 }
10740 else {
10741 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10742 }
10743 }
10744 else {
10745 if (c == '=') {
10746 set_yylval_id('*');
10747 SET_LEX_STATE(EXPR_BEG);
10748 return tOP_ASGN;
10749 }
10750 pushback(p, c);
10751 if (IS_SPCARG(c)) {
10752 rb_warning0("'*' interpreted as argument prefix");
10753 c = tSTAR;
10754 }
10755 else if (IS_BEG()) {
10756 c = tSTAR;
10757 }
10758 else {
10759 c = warn_balanced('*', "*", "argument prefix");
10760 }
10761 }
10762 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10763 return c;
10764
10765 case '!':
10766 c = nextc(p);
10767 if (IS_AFTER_OPERATOR()) {
10768 SET_LEX_STATE(EXPR_ARG);
10769 if (c == '@') {
10770 return '!';
10771 }
10772 }
10773 else {
10774 SET_LEX_STATE(EXPR_BEG);
10775 }
10776 if (c == '=') {
10777 return tNEQ;
10778 }
10779 if (c == '~') {
10780 return tNMATCH;
10781 }
10782 pushback(p, c);
10783 return '!';
10784
10785 case '=':
10786 if (was_bol(p)) {
10787 /* skip embedded rd document */
10788 if (word_match_p(p, "begin", 5)) {
10789 int first_p = TRUE;
10790
10791 lex_goto_eol(p);
10792 dispatch_scan_event(p, tEMBDOC_BEG);
10793 for (;;) {
10794 lex_goto_eol(p);
10795 if (!first_p) {
10796 dispatch_scan_event(p, tEMBDOC);
10797 }
10798 first_p = FALSE;
10799 c = nextc(p);
10800 if (c == -1) {
10801 compile_error(p, "embedded document meets end of file");
10802 return END_OF_INPUT;
10803 }
10804 if (c == '=' && word_match_p(p, "end", 3)) {
10805 break;
10806 }
10807 pushback(p, c);
10808 }
10809 lex_goto_eol(p);
10810 dispatch_scan_event(p, tEMBDOC_END);
10811 goto retry;
10812 }
10813 }
10814
10815 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10816 if ((c = nextc(p)) == '=') {
10817 if ((c = nextc(p)) == '=') {
10818 return tEQQ;
10819 }
10820 pushback(p, c);
10821 return tEQ;
10822 }
10823 if (c == '~') {
10824 return tMATCH;
10825 }
10826 else if (c == '>') {
10827 return tASSOC;
10828 }
10829 pushback(p, c);
10830 return '=';
10831
10832 case '<':
10833 c = nextc(p);
10834 if (c == '<' &&
10835 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10836 !IS_END() &&
10837 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10838 enum yytokentype token = heredoc_identifier(p);
10839 if (token) return token < 0 ? 0 : token;
10840 }
10841 if (IS_AFTER_OPERATOR()) {
10842 SET_LEX_STATE(EXPR_ARG);
10843 }
10844 else {
10845 if (IS_lex_state(EXPR_CLASS))
10846 p->command_start = TRUE;
10847 SET_LEX_STATE(EXPR_BEG);
10848 }
10849 if (c == '=') {
10850 if ((c = nextc(p)) == '>') {
10851 return tCMP;
10852 }
10853 pushback(p, c);
10854 return tLEQ;
10855 }
10856 if (c == '<') {
10857 if ((c = nextc(p)) == '=') {
10858 set_yylval_id(idLTLT);
10859 SET_LEX_STATE(EXPR_BEG);
10860 return tOP_ASGN;
10861 }
10862 pushback(p, c);
10863 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10864 }
10865 pushback(p, c);
10866 return '<';
10867
10868 case '>':
10869 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10870 if ((c = nextc(p)) == '=') {
10871 return tGEQ;
10872 }
10873 if (c == '>') {
10874 if ((c = nextc(p)) == '=') {
10875 set_yylval_id(idGTGT);
10876 SET_LEX_STATE(EXPR_BEG);
10877 return tOP_ASGN;
10878 }
10879 pushback(p, c);
10880 return tRSHFT;
10881 }
10882 pushback(p, c);
10883 return '>';
10884
10885 case '"':
10886 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10887 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10888 p->lex.ptok = p->lex.pcur-1;
10889 return tSTRING_BEG;
10890
10891 case '`':
10892 if (IS_lex_state(EXPR_FNAME)) {
10893 SET_LEX_STATE(EXPR_ENDFN);
10894 return c;
10895 }
10896 if (IS_lex_state(EXPR_DOT)) {
10897 if (cmd_state)
10898 SET_LEX_STATE(EXPR_CMDARG);
10899 else
10900 SET_LEX_STATE(EXPR_ARG);
10901 return c;
10902 }
10903 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10904 return tXSTRING_BEG;
10905
10906 case '\'':
10907 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10908 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10909 p->lex.ptok = p->lex.pcur-1;
10910 return tSTRING_BEG;
10911
10912 case '?':
10913 return parse_qmark(p, space_seen);
10914
10915 case '&':
10916 if ((c = nextc(p)) == '&') {
10917 SET_LEX_STATE(EXPR_BEG);
10918 if ((c = nextc(p)) == '=') {
10919 set_yylval_id(idANDOP);
10920 SET_LEX_STATE(EXPR_BEG);
10921 return tOP_ASGN;
10922 }
10923 pushback(p, c);
10924 return tANDOP;
10925 }
10926 else if (c == '=') {
10927 set_yylval_id('&');
10928 SET_LEX_STATE(EXPR_BEG);
10929 return tOP_ASGN;
10930 }
10931 else if (c == '.') {
10932 set_yylval_id(idANDDOT);
10933 SET_LEX_STATE(EXPR_DOT);
10934 return tANDDOT;
10935 }
10936 pushback(p, c);
10937 if (IS_SPCARG(c)) {
10938 if ((c != ':') ||
10939 (c = peekc_n(p, 1)) == -1 ||
10940 !(c == '\'' || c == '"' ||
10941 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10942 rb_warning0("'&' interpreted as argument prefix");
10943 }
10944 c = tAMPER;
10945 }
10946 else if (IS_BEG()) {
10947 c = tAMPER;
10948 }
10949 else {
10950 c = warn_balanced('&', "&", "argument prefix");
10951 }
10952 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10953 return c;
10954
10955 case '|':
10956 if ((c = nextc(p)) == '|') {
10957 SET_LEX_STATE(EXPR_BEG);
10958 if ((c = nextc(p)) == '=') {
10959 set_yylval_id(idOROP);
10960 SET_LEX_STATE(EXPR_BEG);
10961 return tOP_ASGN;
10962 }
10963 pushback(p, c);
10964 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10965 c = '|';
10966 pushback(p, '|');
10967 return c;
10968 }
10969 return tOROP;
10970 }
10971 if (c == '=') {
10972 set_yylval_id('|');
10973 SET_LEX_STATE(EXPR_BEG);
10974 return tOP_ASGN;
10975 }
10976 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10977 pushback(p, c);
10978 return '|';
10979
10980 case '+':
10981 c = nextc(p);
10982 if (IS_AFTER_OPERATOR()) {
10983 SET_LEX_STATE(EXPR_ARG);
10984 if (c == '@') {
10985 return tUPLUS;
10986 }
10987 pushback(p, c);
10988 return '+';
10989 }
10990 if (c == '=') {
10991 set_yylval_id('+');
10992 SET_LEX_STATE(EXPR_BEG);
10993 return tOP_ASGN;
10994 }
10995 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10996 SET_LEX_STATE(EXPR_BEG);
10997 pushback(p, c);
10998 if (c != -1 && ISDIGIT(c)) {
10999 return parse_numeric(p, '+');
11000 }
11001 return tUPLUS;
11002 }
11003 SET_LEX_STATE(EXPR_BEG);
11004 pushback(p, c);
11005 return warn_balanced('+', "+", "unary operator");
11006
11007 case '-':
11008 c = nextc(p);
11009 if (IS_AFTER_OPERATOR()) {
11010 SET_LEX_STATE(EXPR_ARG);
11011 if (c == '@') {
11012 return tUMINUS;
11013 }
11014 pushback(p, c);
11015 return '-';
11016 }
11017 if (c == '=') {
11018 set_yylval_id('-');
11019 SET_LEX_STATE(EXPR_BEG);
11020 return tOP_ASGN;
11021 }
11022 if (c == '>') {
11023 SET_LEX_STATE(EXPR_ENDFN);
11024 yylval.num = p->lex.lpar_beg;
11025 p->lex.lpar_beg = p->lex.paren_nest;
11026 return tLAMBDA;
11027 }
11028 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
11029 SET_LEX_STATE(EXPR_BEG);
11030 pushback(p, c);
11031 if (c != -1 && ISDIGIT(c)) {
11032 return tUMINUS_NUM;
11033 }
11034 return tUMINUS;
11035 }
11036 SET_LEX_STATE(EXPR_BEG);
11037 pushback(p, c);
11038 return warn_balanced('-', "-", "unary operator");
11039
11040 case '.': {
11041 int is_beg = IS_BEG();
11042 SET_LEX_STATE(EXPR_BEG);
11043 if ((c = nextc(p)) == '.') {
11044 if ((c = nextc(p)) == '.') {
11045 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
11046 SET_LEX_STATE(EXPR_ENDARG);
11047 return tBDOT3;
11048 }
11049 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
11050 rb_warn0("... at EOL, should be parenthesized?");
11051 }
11052 return is_beg ? tBDOT3 : tDOT3;
11053 }
11054 pushback(p, c);
11055 return is_beg ? tBDOT2 : tDOT2;
11056 }
11057 pushback(p, c);
11058 if (c != -1 && ISDIGIT(c)) {
11059 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
11060 parse_numeric(p, '.');
11061 if (ISDIGIT(prev)) {
11062 yyerror0("unexpected fraction part after numeric literal");
11063 }
11064 else {
11065 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
11066 }
11067 SET_LEX_STATE(EXPR_END);
11068 p->lex.ptok = p->lex.pcur;
11069 goto retry;
11070 }
11071 set_yylval_id('.');
11072 SET_LEX_STATE(EXPR_DOT);
11073 return '.';
11074 }
11075
11076 case '0': case '1': case '2': case '3': case '4':
11077 case '5': case '6': case '7': case '8': case '9':
11078 return parse_numeric(p, c);
11079
11080 case ')':
11081 COND_POP();
11082 CMDARG_POP();
11083 SET_LEX_STATE(EXPR_ENDFN);
11084 p->lex.paren_nest--;
11085 return c;
11086
11087 case ']':
11088 COND_POP();
11089 CMDARG_POP();
11090 SET_LEX_STATE(EXPR_END);
11091 p->lex.paren_nest--;
11092 return c;
11093
11094 case '}':
11095 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
11096 if (!p->lex.brace_nest--) return tSTRING_DEND;
11097 COND_POP();
11098 CMDARG_POP();
11099 SET_LEX_STATE(EXPR_END);
11100 p->lex.paren_nest--;
11101 return c;
11102
11103 case ':':
11104 c = nextc(p);
11105 if (c == ':') {
11106 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
11107 SET_LEX_STATE(EXPR_BEG);
11108 return tCOLON3;
11109 }
11110 set_yylval_id(idCOLON2);
11111 SET_LEX_STATE(EXPR_DOT);
11112 return tCOLON2;
11113 }
11114 if (IS_END() || ISSPACE(c) || c == '#') {
11115 pushback(p, c);
11116 c = warn_balanced(':', ":", "symbol literal");
11117 SET_LEX_STATE(EXPR_BEG);
11118 return c;
11119 }
11120 switch (c) {
11121 case '\'':
11122 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11123 break;
11124 case '"':
11125 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11126 break;
11127 default:
11128 pushback(p, c);
11129 break;
11130 }
11131 SET_LEX_STATE(EXPR_FNAME);
11132 return tSYMBEG;
11133
11134 case '/':
11135 if (IS_BEG()) {
11136 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11137 return tREGEXP_BEG;
11138 }
11139 if ((c = nextc(p)) == '=') {
11140 set_yylval_id('/');
11141 SET_LEX_STATE(EXPR_BEG);
11142 return tOP_ASGN;
11143 }
11144 pushback(p, c);
11145 if (IS_SPCARG(c)) {
11146 arg_ambiguous(p, '/');
11147 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11148 return tREGEXP_BEG;
11149 }
11150 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11151 return warn_balanced('/', "/", "regexp literal");
11152
11153 case '^':
11154 if ((c = nextc(p)) == '=') {
11155 set_yylval_id('^');
11156 SET_LEX_STATE(EXPR_BEG);
11157 return tOP_ASGN;
11158 }
11159 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11160 pushback(p, c);
11161 return '^';
11162
11163 case ';':
11164 SET_LEX_STATE(EXPR_BEG);
11165 p->command_start = TRUE;
11166 return ';';
11167
11168 case ',':
11169 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11170 return ',';
11171
11172 case '~':
11173 if (IS_AFTER_OPERATOR()) {
11174 if ((c = nextc(p)) != '@') {
11175 pushback(p, c);
11176 }
11177 SET_LEX_STATE(EXPR_ARG);
11178 }
11179 else {
11180 SET_LEX_STATE(EXPR_BEG);
11181 }
11182 return '~';
11183
11184 case '(':
11185 if (IS_BEG()) {
11186 c = tLPAREN;
11187 }
11188 else if (!space_seen) {
11189 /* foo( ... ) => method call, no ambiguity */
11190 }
11191 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11192 c = tLPAREN_ARG;
11193 }
11194 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11195 rb_warning0("parentheses after method name is interpreted as "
11196 "an argument list, not a decomposed argument");
11197 }
11198 p->lex.paren_nest++;
11199 COND_PUSH(0);
11200 CMDARG_PUSH(0);
11201 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11202 return c;
11203
11204 case '[':
11205 p->lex.paren_nest++;
11206 if (IS_AFTER_OPERATOR()) {
11207 if ((c = nextc(p)) == ']') {
11208 p->lex.paren_nest--;
11209 SET_LEX_STATE(EXPR_ARG);
11210 if ((c = nextc(p)) == '=') {
11211 return tASET;
11212 }
11213 pushback(p, c);
11214 return tAREF;
11215 }
11216 pushback(p, c);
11217 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11218 return '[';
11219 }
11220 else if (IS_BEG()) {
11221 c = tLBRACK;
11222 }
11223 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11224 c = tLBRACK;
11225 }
11226 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11227 COND_PUSH(0);
11228 CMDARG_PUSH(0);
11229 return c;
11230
11231 case '{':
11232 ++p->lex.brace_nest;
11233 if (lambda_beginning_p())
11234 c = tLAMBEG;
11235 else if (IS_lex_state(EXPR_LABELED))
11236 c = tLBRACE; /* hash */
11237 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11238 c = '{'; /* block (primary) */
11239 else if (IS_lex_state(EXPR_ENDARG))
11240 c = tLBRACE_ARG; /* block (expr) */
11241 else
11242 c = tLBRACE; /* hash */
11243 if (c != tLBRACE) {
11244 p->command_start = TRUE;
11245 SET_LEX_STATE(EXPR_BEG);
11246 }
11247 else {
11248 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11249 }
11250 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11251 COND_PUSH(0);
11252 CMDARG_PUSH(0);
11253 return c;
11254
11255 case '\\':
11256 c = nextc(p);
11257 if (c == '\n') {
11258 space_seen = 1;
11259 dispatch_scan_event(p, tSP);
11260 goto retry; /* skip \\n */
11261 }
11262 if (c == ' ') return tSP;
11263 if (ISSPACE(c)) return c;
11264 pushback(p, c);
11265 return '\\';
11266
11267 case '%':
11268 return parse_percent(p, space_seen, last_state);
11269
11270 case '$':
11271 return parse_gvar(p, last_state);
11272
11273 case '@':
11274 return parse_atmark(p, last_state);
11275
11276 case '_':
11277 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11278 p->ruby__end__seen = 1;
11279 p->eofp = 1;
11280#ifdef RIPPER
11281 lex_goto_eol(p);
11282 dispatch_scan_event(p, k__END__);
11283#endif
11284 return END_OF_INPUT;
11285 }
11286 newtok(p);
11287 break;
11288
11289 default:
11290 if (!parser_is_identchar(p)) {
11291 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11292 token_flush(p);
11293 goto retry;
11294 }
11295
11296 newtok(p);
11297 break;
11298 }
11299
11300 return parse_ident(p, c, cmd_state);
11301}
11302
11303static enum yytokentype
11304yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11305{
11306 enum yytokentype t;
11307
11308 p->lval = lval;
11309 lval->node = 0;
11310 p->yylloc = yylloc;
11311
11312 t = parser_yylex(p);
11313
11314 if (has_delayed_token(p))
11315 dispatch_delayed_token(p, t);
11316 else if (t != END_OF_INPUT)
11317 dispatch_scan_event(p, t);
11318
11319 return t;
11320}
11321
11322#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11323
11324static NODE*
11325node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11326{
11327 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11328
11329 rb_node_init(n, type);
11330 return n;
11331}
11332
11333static NODE *
11334nd_set_loc(NODE *nd, const YYLTYPE *loc)
11335{
11336 nd->nd_loc = *loc;
11337 nd_set_line(nd, loc->beg_pos.lineno);
11338 return nd;
11339}
11340
11341static NODE*
11342node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11343{
11344 NODE *n = node_new_internal(p, type, size, alignment);
11345
11346 nd_set_loc(n, loc);
11347 nd_set_node_id(n, parser_get_node_id(p));
11348 return n;
11349}
11350
11351#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11352
11353static rb_node_scope_t *
11354rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11355{
11356 rb_ast_id_table_t *nd_tbl;
11357 nd_tbl = local_tbl(p);
11358 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11359 n->nd_tbl = nd_tbl;
11360 n->nd_body = nd_body;
11361 n->nd_args = nd_args;
11362
11363 return n;
11364}
11365
11366static rb_node_scope_t *
11367rb_node_scope_new2(struct parser_params *p, rb_ast_id_table_t *nd_tbl, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11368{
11369 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11370 n->nd_tbl = nd_tbl;
11371 n->nd_body = nd_body;
11372 n->nd_args = nd_args;
11373
11374 return n;
11375}
11376
11377static rb_node_defn_t *
11378rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11379{
11380 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11381 n->nd_mid = nd_mid;
11382 n->nd_defn = nd_defn;
11383
11384 return n;
11385}
11386
11387static rb_node_defs_t *
11388rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11389{
11390 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11391 n->nd_recv = nd_recv;
11392 n->nd_mid = nd_mid;
11393 n->nd_defn = nd_defn;
11394
11395 return n;
11396}
11397
11398static rb_node_block_t *
11399rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11400{
11401 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11402 n->nd_head = nd_head;
11403 n->nd_end = (NODE *)n;
11404 n->nd_next = 0;
11405
11406 return n;
11407}
11408
11409static rb_node_for_t *
11410rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc)
11411{
11412 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11413 n->nd_body = nd_body;
11414 n->nd_iter = nd_iter;
11415
11416 return n;
11417}
11418
11419static rb_node_for_masgn_t *
11420rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11421{
11422 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11423 n->nd_var = nd_var;
11424
11425 return n;
11426}
11427
11428static rb_node_retry_t *
11429rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11430{
11431 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11432
11433 return n;
11434}
11435
11436static rb_node_begin_t *
11437rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11438{
11439 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11440 n->nd_body = nd_body;
11441
11442 return n;
11443}
11444
11445static rb_node_rescue_t *
11446rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11447{
11448 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11449 n->nd_head = nd_head;
11450 n->nd_resq = nd_resq;
11451 n->nd_else = nd_else;
11452
11453 return n;
11454}
11455
11456static rb_node_resbody_t *
11457rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11458{
11459 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11460 n->nd_args = nd_args;
11461 n->nd_exc_var = nd_exc_var;
11462 n->nd_body = nd_body;
11463 n->nd_next = nd_next;
11464
11465 return n;
11466}
11467
11468static rb_node_ensure_t *
11469rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11470{
11471 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11472 n->nd_head = nd_head;
11473 n->nd_ensr = nd_ensr;
11474
11475 return n;
11476}
11477
11478static rb_node_and_t *
11479rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11480{
11481 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11482 n->nd_1st = nd_1st;
11483 n->nd_2nd = nd_2nd;
11484 n->operator_loc = *operator_loc;
11485
11486 return n;
11487}
11488
11489static rb_node_or_t *
11490rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11491{
11492 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11493 n->nd_1st = nd_1st;
11494 n->nd_2nd = nd_2nd;
11495 n->operator_loc = *operator_loc;
11496
11497 return n;
11498}
11499
11500static rb_node_return_t *
11501rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11502{
11503 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11504 n->nd_stts = nd_stts;
11505 n->keyword_loc = *keyword_loc;
11506 return n;
11507}
11508
11509static rb_node_yield_t *
11510rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11511{
11512 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11513 n->nd_head = nd_head;
11514
11515 return n;
11516}
11517
11518static rb_node_if_t *
11519rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11520{
11521 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11522 n->nd_cond = nd_cond;
11523 n->nd_body = nd_body;
11524 n->nd_else = nd_else;
11525
11526 return n;
11527}
11528
11529static rb_node_unless_t *
11530rb_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)
11531{
11532 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11533 n->nd_cond = nd_cond;
11534 n->nd_body = nd_body;
11535 n->nd_else = nd_else;
11536 n->keyword_loc = *keyword_loc;
11537 n->then_keyword_loc = *then_keyword_loc;
11538 n->end_keyword_loc = *end_keyword_loc;
11539
11540 return n;
11541}
11542
11543static rb_node_class_t *
11544rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc)
11545{
11546 /* Keep the order of node creation */
11547 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11548 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11549 n->nd_cpath = nd_cpath;
11550 n->nd_body = scope;
11551 n->nd_super = nd_super;
11552
11553 return n;
11554}
11555
11556static rb_node_sclass_t *
11557rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc)
11558{
11559 /* Keep the order of node creation */
11560 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11561 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11562 n->nd_recv = nd_recv;
11563 n->nd_body = scope;
11564
11565 return n;
11566}
11567
11568static rb_node_module_t *
11569rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc)
11570{
11571 /* Keep the order of node creation */
11572 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11573 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11574 n->nd_cpath = nd_cpath;
11575 n->nd_body = scope;
11576
11577 return n;
11578}
11579
11580static rb_node_iter_t *
11581rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11582{
11583 /* Keep the order of node creation */
11584 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11585 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11586 n->nd_body = scope;
11587 n->nd_iter = 0;
11588
11589 return n;
11590}
11591
11592static rb_node_lambda_t *
11593rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11594{
11595 /* Keep the order of node creation */
11596 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11597 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, loc);
11598 n->nd_body = scope;
11599
11600 return n;
11601}
11602
11603static rb_node_case_t *
11604rb_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)
11605{
11606 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11607 n->nd_head = nd_head;
11608 n->nd_body = nd_body;
11609 n->case_keyword_loc = *case_keyword_loc;
11610 n->end_keyword_loc = *end_keyword_loc;
11611
11612 return n;
11613}
11614
11615static rb_node_case2_t *
11616rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11617{
11618 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11619 n->nd_head = 0;
11620 n->nd_body = nd_body;
11621 n->case_keyword_loc = *case_keyword_loc;
11622 n->end_keyword_loc = *end_keyword_loc;
11623
11624 return n;
11625}
11626
11627static rb_node_case3_t *
11628rb_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)
11629{
11630 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11631 n->nd_head = nd_head;
11632 n->nd_body = nd_body;
11633 n->case_keyword_loc = *case_keyword_loc;
11634 n->end_keyword_loc = *end_keyword_loc;
11635
11636 return n;
11637}
11638
11639static rb_node_when_t *
11640rb_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)
11641{
11642 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11643 n->nd_head = nd_head;
11644 n->nd_body = nd_body;
11645 n->nd_next = nd_next;
11646 n->keyword_loc = *keyword_loc;
11647 n->then_keyword_loc = *then_keyword_loc;
11648
11649 return n;
11650}
11651
11652static rb_node_in_t *
11653rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11654{
11655 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11656 n->nd_head = nd_head;
11657 n->nd_body = nd_body;
11658 n->nd_next = nd_next;
11659
11660 return n;
11661}
11662
11663static rb_node_while_t *
11664rb_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)
11665{
11666 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11667 n->nd_cond = nd_cond;
11668 n->nd_body = nd_body;
11669 n->nd_state = nd_state;
11670 n->keyword_loc = *keyword_loc;
11671 n->closing_loc = *closing_loc;
11672
11673 return n;
11674}
11675
11676static rb_node_until_t *
11677rb_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)
11678{
11679 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11680 n->nd_cond = nd_cond;
11681 n->nd_body = nd_body;
11682 n->nd_state = nd_state;
11683 n->keyword_loc = *keyword_loc;
11684 n->closing_loc = *closing_loc;
11685
11686 return n;
11687}
11688
11689static rb_node_colon2_t *
11690rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc)
11691{
11692 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11693 n->nd_head = nd_head;
11694 n->nd_mid = nd_mid;
11695
11696 return n;
11697}
11698
11699static rb_node_colon3_t *
11700rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11701{
11702 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11703 n->nd_mid = nd_mid;
11704
11705 return n;
11706}
11707
11708static rb_node_dot2_t *
11709rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11710{
11711 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11712 n->nd_beg = nd_beg;
11713 n->nd_end = nd_end;
11714
11715 return n;
11716}
11717
11718static rb_node_dot3_t *
11719rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11720{
11721 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11722 n->nd_beg = nd_beg;
11723 n->nd_end = nd_end;
11724
11725 return n;
11726}
11727
11728static rb_node_self_t *
11729rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11730{
11731 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11732 n->nd_state = 1;
11733
11734 return n;
11735}
11736
11737static rb_node_nil_t *
11738rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11739{
11740 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11741
11742 return n;
11743}
11744
11745static rb_node_true_t *
11746rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11747{
11748 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11749
11750 return n;
11751}
11752
11753static rb_node_false_t *
11754rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11755{
11756 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11757
11758 return n;
11759}
11760
11761static rb_node_super_t *
11762rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc)
11763{
11764 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11765 n->nd_args = nd_args;
11766
11767 return n;
11768}
11769
11770static rb_node_zsuper_t *
11771rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11772{
11773 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11774
11775 return n;
11776}
11777
11778static rb_node_match2_t *
11779rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11780{
11781 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11782 n->nd_recv = nd_recv;
11783 n->nd_value = nd_value;
11784 n->nd_args = 0;
11785
11786 return n;
11787}
11788
11789static rb_node_match3_t *
11790rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11791{
11792 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11793 n->nd_recv = nd_recv;
11794 n->nd_value = nd_value;
11795
11796 return n;
11797}
11798
11799/* TODO: Use union for NODE_LIST2 */
11800static rb_node_list_t *
11801rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11802{
11803 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11804 n->nd_head = nd_head;
11805 n->as.nd_alen = 1;
11806 n->nd_next = 0;
11807
11808 return n;
11809}
11810
11811static rb_node_list_t *
11812rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11813{
11814 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11815 n->nd_head = nd_head;
11816 n->as.nd_alen = nd_alen;
11817 n->nd_next = nd_next;
11818
11819 return n;
11820}
11821
11822static rb_node_zlist_t *
11823rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11824{
11825 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11826
11827 return n;
11828}
11829
11830static rb_node_hash_t *
11831rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11832{
11833 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11834 n->nd_head = nd_head;
11835 n->nd_brace = 0;
11836
11837 return n;
11838}
11839
11840static rb_node_masgn_t *
11841rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11842{
11843 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11844 n->nd_head = nd_head;
11845 n->nd_value = 0;
11846 n->nd_args = nd_args;
11847
11848 return n;
11849}
11850
11851static rb_node_gasgn_t *
11852rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11853{
11854 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11855 n->nd_vid = nd_vid;
11856 n->nd_value = nd_value;
11857
11858 return n;
11859}
11860
11861static rb_node_lasgn_t *
11862rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11863{
11864 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11865 n->nd_vid = nd_vid;
11866 n->nd_value = nd_value;
11867
11868 return n;
11869}
11870
11871static rb_node_dasgn_t *
11872rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11873{
11874 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11875 n->nd_vid = nd_vid;
11876 n->nd_value = nd_value;
11877
11878 return n;
11879}
11880
11881static rb_node_iasgn_t *
11882rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11883{
11884 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11885 n->nd_vid = nd_vid;
11886 n->nd_value = nd_value;
11887
11888 return n;
11889}
11890
11891static rb_node_cvasgn_t *
11892rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11893{
11894 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11895 n->nd_vid = nd_vid;
11896 n->nd_value = nd_value;
11897
11898 return n;
11899}
11900
11901static rb_node_op_asgn1_t *
11902rb_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)
11903{
11904 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11905 n->nd_recv = nd_recv;
11906 n->nd_mid = nd_mid;
11907 n->nd_index = index;
11908 n->nd_rvalue = rvalue;
11909 n->call_operator_loc = *call_operator_loc;
11910 n->opening_loc = *opening_loc;
11911 n->closing_loc = *closing_loc;
11912 n->binary_operator_loc = *binary_operator_loc;
11913
11914 return n;
11915}
11916
11917static rb_node_op_asgn2_t *
11918rb_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)
11919{
11920 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11921 n->nd_recv = nd_recv;
11922 n->nd_value = nd_value;
11923 n->nd_vid = nd_vid;
11924 n->nd_mid = nd_mid;
11925 n->nd_aid = nd_aid;
11926 n->call_operator_loc = *call_operator_loc;
11927 n->message_loc = *message_loc;
11928 n->binary_operator_loc = *binary_operator_loc;
11929
11930 return n;
11931}
11932
11933static rb_node_op_asgn_or_t *
11934rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11935{
11936 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11937 n->nd_head = nd_head;
11938 n->nd_value = nd_value;
11939
11940 return n;
11941}
11942
11943static rb_node_op_asgn_and_t *
11944rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11945{
11946 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11947 n->nd_head = nd_head;
11948 n->nd_value = nd_value;
11949
11950 return n;
11951}
11952
11953static rb_node_gvar_t *
11954rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11955{
11956 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11957 n->nd_vid = nd_vid;
11958
11959 return n;
11960}
11961
11962static rb_node_lvar_t *
11963rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11964{
11965 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11966 n->nd_vid = nd_vid;
11967
11968 return n;
11969}
11970
11971static rb_node_dvar_t *
11972rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11973{
11974 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11975 n->nd_vid = nd_vid;
11976
11977 return n;
11978}
11979
11980static rb_node_ivar_t *
11981rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11982{
11983 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11984 n->nd_vid = nd_vid;
11985
11986 return n;
11987}
11988
11989static rb_node_const_t *
11990rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11991{
11992 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11993 n->nd_vid = nd_vid;
11994
11995 return n;
11996}
11997
11998static rb_node_cvar_t *
11999rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
12000{
12001 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
12002 n->nd_vid = nd_vid;
12003
12004 return n;
12005}
12006
12007static rb_node_nth_ref_t *
12008rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
12009{
12010 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
12011 n->nd_nth = nd_nth;
12012
12013 return n;
12014}
12015
12016static rb_node_back_ref_t *
12017rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
12018{
12019 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
12020 n->nd_nth = nd_nth;
12021
12022 return n;
12023}
12024
12025static rb_node_integer_t *
12026rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
12027{
12028 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
12029 n->val = val;
12030 n->minus = FALSE;
12031 n->base = base;
12032
12033 return n;
12034}
12035
12036static rb_node_float_t *
12037rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
12038{
12039 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
12040 n->val = val;
12041 n->minus = FALSE;
12042
12043 return n;
12044}
12045
12046static rb_node_rational_t *
12047rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
12048{
12049 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
12050 n->val = val;
12051 n->minus = FALSE;
12052 n->base = base;
12053 n->seen_point = seen_point;
12054
12055 return n;
12056}
12057
12058static rb_node_imaginary_t *
12059rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
12060{
12061 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
12062 n->val = val;
12063 n->minus = FALSE;
12064 n->base = base;
12065 n->seen_point = seen_point;
12066 n->type = numeric_type;
12067
12068 return n;
12069}
12070
12071static rb_node_str_t *
12072rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12073{
12074 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12075 n->string = string;
12076
12077 return n;
12078}
12079
12080/* TODO; Use union for NODE_DSTR2 */
12081static rb_node_dstr_t *
12082rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12083{
12084 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12085 n->string = string;
12086 n->as.nd_alen = nd_alen;
12087 n->nd_next = (rb_node_list_t *)nd_next;
12088
12089 return n;
12090}
12091
12092static rb_node_dstr_t *
12093rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12094{
12095 return rb_node_dstr_new0(p, string, 1, 0, loc);
12096}
12097
12098static rb_node_xstr_t *
12099rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12100{
12101 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12102 n->string = string;
12103
12104 return n;
12105}
12106
12107static rb_node_dxstr_t *
12108rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12109{
12110 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12111 n->string = string;
12112 n->as.nd_alen = nd_alen;
12113 n->nd_next = (rb_node_list_t *)nd_next;
12114
12115 return n;
12116}
12117
12118static rb_node_sym_t *
12119rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12120{
12121 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12122 n->string = rb_str_to_parser_string(p, str);
12123
12124 return n;
12125}
12126
12127static rb_node_dsym_t *
12128rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12129{
12130 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12131 n->string = string;
12132 n->as.nd_alen = nd_alen;
12133 n->nd_next = (rb_node_list_t *)nd_next;
12134
12135 return n;
12136}
12137
12138static rb_node_evstr_t *
12139rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12140{
12141 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12142 n->nd_body = nd_body;
12143
12144 return n;
12145}
12146
12147static rb_node_regx_t *
12148rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc)
12149{
12150 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12151 n->string = string;
12152 n->options = options & RE_OPTION_MASK;
12153
12154 return n;
12155}
12156
12157static rb_node_call_t *
12158rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12159{
12160 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12161 n->nd_recv = nd_recv;
12162 n->nd_mid = nd_mid;
12163 n->nd_args = nd_args;
12164
12165 return n;
12166}
12167
12168static rb_node_opcall_t *
12169rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12170{
12171 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12172 n->nd_recv = nd_recv;
12173 n->nd_mid = nd_mid;
12174 n->nd_args = nd_args;
12175
12176 return n;
12177}
12178
12179static rb_node_fcall_t *
12180rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12181{
12182 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12183 n->nd_mid = nd_mid;
12184 n->nd_args = nd_args;
12185
12186 return n;
12187}
12188
12189static rb_node_qcall_t *
12190rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12191{
12192 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12193 n->nd_recv = nd_recv;
12194 n->nd_mid = nd_mid;
12195 n->nd_args = nd_args;
12196
12197 return n;
12198}
12199
12200static rb_node_vcall_t *
12201rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12202{
12203 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12204 n->nd_mid = nd_mid;
12205
12206 return n;
12207}
12208
12209static rb_node_once_t *
12210rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12211{
12212 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12213 n->nd_body = nd_body;
12214
12215 return n;
12216}
12217
12218static rb_node_args_t *
12219rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12220{
12221 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12222 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12223
12224 return n;
12225}
12226
12227static rb_node_args_aux_t *
12228rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12229{
12230 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12231 n->nd_pid = nd_pid;
12232 n->nd_plen = nd_plen;
12233 n->nd_next = 0;
12234
12235 return n;
12236}
12237
12238static rb_node_opt_arg_t *
12239rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12240{
12241 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12242 n->nd_body = nd_body;
12243 n->nd_next = 0;
12244
12245 return n;
12246}
12247
12248static rb_node_kw_arg_t *
12249rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12250{
12251 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12252 n->nd_body = nd_body;
12253 n->nd_next = 0;
12254
12255 return n;
12256}
12257
12258static rb_node_postarg_t *
12259rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12260{
12261 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12262 n->nd_1st = nd_1st;
12263 n->nd_2nd = nd_2nd;
12264
12265 return n;
12266}
12267
12268static rb_node_argscat_t *
12269rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12270{
12271 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12272 n->nd_head = nd_head;
12273 n->nd_body = nd_body;
12274
12275 return n;
12276}
12277
12278static rb_node_argspush_t *
12279rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12280{
12281 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12282 n->nd_head = nd_head;
12283 n->nd_body = nd_body;
12284
12285 return n;
12286}
12287
12288static rb_node_splat_t *
12289rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12290{
12291 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12292 n->nd_head = nd_head;
12293 n->operator_loc = *operator_loc;
12294
12295 return n;
12296}
12297
12298static rb_node_block_pass_t *
12299rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12300{
12301 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12302 n->forwarding = 0;
12303 n->nd_head = 0;
12304 n->nd_body = nd_body;
12305 n->operator_loc = *operator_loc;
12306
12307 return n;
12308}
12309
12310static rb_node_alias_t *
12311rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12312{
12313 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12314 n->nd_1st = nd_1st;
12315 n->nd_2nd = nd_2nd;
12316 n->keyword_loc = *keyword_loc;
12317
12318 return n;
12319}
12320
12321static rb_node_valias_t *
12322rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12323{
12324 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12325 n->nd_alias = nd_alias;
12326 n->nd_orig = nd_orig;
12327 n->keyword_loc = *keyword_loc;
12328
12329 return n;
12330}
12331
12332static rb_node_undef_t *
12333rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12334{
12335 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12336 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12337 n->keyword_loc = NULL_LOC;
12338 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12339
12340 return n;
12341}
12342
12343static rb_node_errinfo_t *
12344rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12345{
12346 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12347
12348 return n;
12349}
12350
12351static rb_node_defined_t *
12352rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12353{
12354 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12355 n->nd_head = nd_head;
12356
12357 return n;
12358}
12359
12360static rb_node_postexe_t *
12361rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12362{
12363 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12364 n->nd_body = nd_body;
12365
12366 return n;
12367}
12368
12369static rb_node_attrasgn_t *
12370rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12371{
12372 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12373 n->nd_recv = nd_recv;
12374 n->nd_mid = nd_mid;
12375 n->nd_args = nd_args;
12376
12377 return n;
12378}
12379
12380static rb_node_aryptn_t *
12381rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12382{
12383 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12384 n->nd_pconst = 0;
12385 n->pre_args = pre_args;
12386 n->rest_arg = rest_arg;
12387 n->post_args = post_args;
12388
12389 return n;
12390}
12391
12392static rb_node_hshptn_t *
12393rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12394{
12395 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12396 n->nd_pconst = nd_pconst;
12397 n->nd_pkwargs = nd_pkwargs;
12398 n->nd_pkwrestarg = nd_pkwrestarg;
12399
12400 return n;
12401}
12402
12403static rb_node_fndptn_t *
12404rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12405{
12406 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12407 n->nd_pconst = 0;
12408 n->pre_rest_arg = pre_rest_arg;
12409 n->args = args;
12410 n->post_rest_arg = post_rest_arg;
12411
12412 return n;
12413}
12414
12415static rb_node_line_t *
12416rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12417{
12418 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12419
12420 return n;
12421}
12422
12423static rb_node_file_t *
12424rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12425{
12426 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12427 n->path = rb_str_to_parser_string(p, str);
12428
12429 return n;
12430}
12431
12432static rb_node_encoding_t *
12433rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12434{
12435 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12436 n->enc = p->enc;
12437
12438 return n;
12439}
12440
12441static rb_node_cdecl_t *
12442rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12443{
12444 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12445 n->nd_vid = nd_vid;
12446 n->nd_value = nd_value;
12447 n->nd_else = nd_else;
12448 n->shareability = shareability;
12449
12450 return n;
12451}
12452
12453static rb_node_op_cdecl_t *
12454rb_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)
12455{
12456 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12457 n->nd_head = nd_head;
12458 n->nd_value = nd_value;
12459 n->nd_aid = nd_aid;
12460 n->shareability = shareability;
12461
12462 return n;
12463}
12464
12465static rb_node_error_t *
12466rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12467{
12468 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12469
12470 return n;
12471}
12472
12473static rb_node_break_t *
12474rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12475{
12476 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12477 n->nd_stts = nd_stts;
12478 n->nd_chain = 0;
12479 n->keyword_loc = *keyword_loc;
12480
12481 return n;
12482}
12483
12484static rb_node_next_t *
12485rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12486{
12487 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12488 n->nd_stts = nd_stts;
12489 n->nd_chain = 0;
12490 n->keyword_loc = *keyword_loc;
12491
12492 return n;
12493}
12494
12495static rb_node_redo_t *
12496rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12497{
12498 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12499 n->nd_chain = 0;
12500 n->keyword_loc = *keyword_loc;
12501
12502 return n;
12503}
12504
12505static rb_node_def_temp_t *
12506rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12507{
12508 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12509 n->save.numparam_save = 0;
12510 n->save.max_numparam = 0;
12511 n->save.ctxt = p->ctxt;
12512 n->nd_def = 0;
12513 n->nd_mid = 0;
12514
12515 return n;
12516}
12517
12518static rb_node_def_temp_t *
12519def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12520{
12521 n->save.numparam_save = numparam_push(p);
12522 n->save.max_numparam = p->max_numparam;
12523 return n;
12524}
12525
12526#ifndef RIPPER
12527static enum node_type
12528nodetype(NODE *node) /* for debug */
12529{
12530 return (enum node_type)nd_type(node);
12531}
12532
12533static int
12534nodeline(NODE *node)
12535{
12536 return nd_line(node);
12537}
12538#endif
12539
12540static NODE*
12541newline_node(NODE *node)
12542{
12543 if (node) {
12544 node = remove_begin(node);
12545 nd_set_fl_newline(node);
12546 }
12547 return node;
12548}
12549
12550static void
12551fixpos(NODE *node, NODE *orig)
12552{
12553 if (!node) return;
12554 if (!orig) return;
12555 nd_set_line(node, nd_line(orig));
12556}
12557
12558static NODE*
12559block_append(struct parser_params *p, NODE *head, NODE *tail)
12560{
12561 NODE *end, *h = head, *nd;
12562
12563 if (tail == 0) return head;
12564
12565 if (h == 0) return tail;
12566 switch (nd_type(h)) {
12567 default:
12568 h = end = NEW_BLOCK(head, &head->nd_loc);
12569 head = end;
12570 break;
12571 case NODE_BLOCK:
12572 end = RNODE_BLOCK(h)->nd_end;
12573 break;
12574 }
12575
12576 nd = RNODE_BLOCK(end)->nd_head;
12577 switch (nd_type(nd)) {
12578 case NODE_RETURN:
12579 case NODE_BREAK:
12580 case NODE_NEXT:
12581 case NODE_REDO:
12582 case NODE_RETRY:
12583 rb_warning0L(nd_line(tail), "statement not reached");
12584 break;
12585
12586 default:
12587 break;
12588 }
12589
12590 if (!nd_type_p(tail, NODE_BLOCK)) {
12591 tail = NEW_BLOCK(tail, &tail->nd_loc);
12592 }
12593 RNODE_BLOCK(end)->nd_next = tail;
12594 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12595 nd_set_last_loc(head, nd_last_loc(tail));
12596 return head;
12597}
12598
12599/* append item to the list */
12600static NODE*
12601list_append(struct parser_params *p, NODE *list, NODE *item)
12602{
12603 NODE *last;
12604
12605 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12606 if (RNODE_LIST(list)->nd_next) {
12607 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12608 }
12609 else {
12610 last = list;
12611 }
12612
12613 RNODE_LIST(list)->as.nd_alen += 1;
12614 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12615 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12616
12617 nd_set_last_loc(list, nd_last_loc(item));
12618
12619 return list;
12620}
12621
12622/* concat two lists */
12623static NODE*
12624list_concat(NODE *head, NODE *tail)
12625{
12626 NODE *last;
12627
12628 if (RNODE_LIST(head)->nd_next) {
12629 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12630 }
12631 else {
12632 last = head;
12633 }
12634
12635 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12636 RNODE_LIST(last)->nd_next = tail;
12637 if (RNODE_LIST(tail)->nd_next) {
12638 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12639 }
12640 else {
12641 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12642 }
12643
12644 nd_set_last_loc(head, nd_last_loc(tail));
12645
12646 return head;
12647}
12648
12649static int
12650literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12651{
12652 if (!tail) return 1;
12653 if (!rb_parser_enc_compatible(p, head, tail)) {
12654 compile_error(p, "string literal encodings differ (%s / %s)",
12655 rb_enc_name(rb_parser_str_get_encoding(head)),
12656 rb_enc_name(rb_parser_str_get_encoding(tail)));
12657 rb_parser_str_resize(p, head, 0);
12658 rb_parser_str_resize(p, tail, 0);
12659 return 0;
12660 }
12661 rb_parser_str_buf_append(p, head, tail);
12662 return 1;
12663}
12664
12665static rb_parser_string_t *
12666string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12667{
12668 if (htype != NODE_DSTR) return NULL;
12669 if (RNODE_DSTR(head)->nd_next) {
12670 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12671 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12672 }
12673 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12674 ASSUME(lit);
12675 return lit;
12676}
12677
12678#ifndef RIPPER
12679static rb_parser_string_t *
12680rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12681{
12682 rb_parser_string_t *copy;
12683 if (!orig) return NULL;
12684 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12685 copy->coderange = orig->coderange;
12686 copy->enc = orig->enc;
12687 return copy;
12688}
12689#endif
12690
12691/* concat two string literals */
12692static NODE *
12693literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12694{
12695 enum node_type htype;
12696 rb_parser_string_t *lit;
12697
12698 if (!head) return tail;
12699 if (!tail) return head;
12700
12701 htype = nd_type(head);
12702 if (htype == NODE_EVSTR) {
12703 head = new_dstr(p, head, loc);
12704 htype = NODE_DSTR;
12705 }
12706 if (p->heredoc_indent > 0) {
12707 switch (htype) {
12708 case NODE_STR:
12709 head = str2dstr(p, head);
12710 case NODE_DSTR:
12711 return list_append(p, head, tail);
12712 default:
12713 break;
12714 }
12715 }
12716 switch (nd_type(tail)) {
12717 case NODE_STR:
12718 if ((lit = string_literal_head(p, htype, head)) != false) {
12719 htype = NODE_STR;
12720 }
12721 else {
12722 lit = RNODE_DSTR(head)->string;
12723 }
12724 if (htype == NODE_STR) {
12725 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12726 error:
12727 rb_discard_node(p, head);
12728 rb_discard_node(p, tail);
12729 return 0;
12730 }
12731 rb_discard_node(p, tail);
12732 }
12733 else {
12734 list_append(p, head, tail);
12735 }
12736 break;
12737
12738 case NODE_DSTR:
12739 if (htype == NODE_STR) {
12740 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12741 goto error;
12742 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12743 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12744 RNODE_STR(head)->string = NULL;
12745 rb_discard_node(p, head);
12746 head = tail;
12747 }
12748 else if (!RNODE_DSTR(tail)->string) {
12749 append:
12750 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12751 if (!RNODE_DSTR(head)->nd_next) {
12752 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12753 }
12754 else if (RNODE_DSTR(tail)->nd_next) {
12755 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12756 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12757 }
12758 rb_discard_node(p, tail);
12759 }
12760 else if ((lit = string_literal_head(p, htype, head)) != false) {
12761 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12762 goto error;
12763 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12764 RNODE_DSTR(tail)->string = 0;
12765 goto append;
12766 }
12767 else {
12768 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));
12769 RNODE_DSTR(tail)->string = 0;
12770 }
12771 break;
12772
12773 case NODE_EVSTR:
12774 if (htype == NODE_STR) {
12775 head = str2dstr(p, head);
12776 RNODE_DSTR(head)->as.nd_alen = 1;
12777 }
12778 list_append(p, head, tail);
12779 break;
12780 }
12781 return head;
12782}
12783
12784static void
12785nd_copy_flag(NODE *new_node, NODE *old_node)
12786{
12787 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12788 nd_set_line(new_node, nd_line(old_node));
12789 new_node->nd_loc = old_node->nd_loc;
12790 new_node->node_id = old_node->node_id;
12791}
12792
12793static NODE *
12794str2dstr(struct parser_params *p, NODE *node)
12795{
12796 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12797 nd_copy_flag(new_node, node);
12798 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12799 RNODE_DSTR(new_node)->as.nd_alen = 0;
12800 RNODE_DSTR(new_node)->nd_next = 0;
12801 RNODE_STR(node)->string = 0;
12802
12803 return new_node;
12804}
12805
12806static NODE *
12807str2regx(struct parser_params *p, NODE *node, int options)
12808{
12809 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12810 nd_copy_flag(new_node, node);
12811 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12812 RNODE_REGX(new_node)->options = options;
12813 RNODE_STR(node)->string = 0;
12814
12815 return new_node;
12816}
12817
12818static NODE *
12819evstr2dstr(struct parser_params *p, NODE *node)
12820{
12821 if (nd_type_p(node, NODE_EVSTR)) {
12822 node = new_dstr(p, node, &node->nd_loc);
12823 }
12824 return node;
12825}
12826
12827static NODE *
12828new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12829{
12830 NODE *head = node;
12831
12832 if (node) {
12833 switch (nd_type(node)) {
12834 case NODE_STR:
12835 return str2dstr(p, node);
12836 case NODE_DSTR:
12837 break;
12838 case NODE_EVSTR:
12839 return node;
12840 }
12841 }
12842 return NEW_EVSTR(head, loc);
12843}
12844
12845static NODE *
12846new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12847{
12848 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12849 return list_append(p, dstr, node);
12850}
12851
12852static NODE *
12853call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12854 const YYLTYPE *op_loc, const YYLTYPE *loc)
12855{
12856 NODE *expr;
12857 value_expr(recv);
12858 value_expr(arg1);
12859 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12860 nd_set_line(expr, op_loc->beg_pos.lineno);
12861 return expr;
12862}
12863
12864static NODE *
12865call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12866{
12867 NODE *opcall;
12868 value_expr(recv);
12869 opcall = NEW_OPCALL(recv, id, 0, loc);
12870 nd_set_line(opcall, op_loc->beg_pos.lineno);
12871 return opcall;
12872}
12873
12874static NODE *
12875new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12876{
12877 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12878 nd_set_line(qcall, op_loc->beg_pos.lineno);
12879 return qcall;
12880}
12881
12882static NODE*
12883new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12884{
12885 NODE *ret;
12886 if (block) block_dup_check(p, args, block);
12887 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12888 if (block) ret = method_add_block(p, ret, block, loc);
12889 fixpos(ret, recv);
12890 return ret;
12891}
12892
12893#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12894
12895static NODE*
12896last_expr_once_body(NODE *node)
12897{
12898 if (!node) return 0;
12899 return nd_once_body(node);
12900}
12901
12902static NODE*
12903match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12904{
12905 NODE *n;
12906 int line = op_loc->beg_pos.lineno;
12907
12908 value_expr(node1);
12909 value_expr(node2);
12910
12911 if ((n = last_expr_once_body(node1)) != 0) {
12912 switch (nd_type(n)) {
12913 case NODE_DREGX:
12914 {
12915 NODE *match = NEW_MATCH2(node1, node2, loc);
12916 nd_set_line(match, line);
12917 return match;
12918 }
12919
12920 case NODE_REGX:
12921 {
12922 const VALUE lit = rb_node_regx_string_val(n);
12923 if (!NIL_P(lit)) {
12924 NODE *match = NEW_MATCH2(node1, node2, loc);
12925 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12926 nd_set_line(match, line);
12927 return match;
12928 }
12929 }
12930 }
12931 }
12932
12933 if ((n = last_expr_once_body(node2)) != 0) {
12934 NODE *match3;
12935
12936 switch (nd_type(n)) {
12937 case NODE_DREGX:
12938 match3 = NEW_MATCH3(node2, node1, loc);
12939 return match3;
12940 }
12941 }
12942
12943 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12944 nd_set_line(n, line);
12945 return n;
12946}
12947
12948# if WARN_PAST_SCOPE
12949static int
12950past_dvar_p(struct parser_params *p, ID id)
12951{
12952 struct vtable *past = p->lvtbl->past;
12953 while (past) {
12954 if (vtable_included(past, id)) return 1;
12955 past = past->prev;
12956 }
12957 return 0;
12958}
12959# endif
12960
12961static int
12962numparam_nested_p(struct parser_params *p)
12963{
12964 struct local_vars *local = p->lvtbl;
12965 NODE *outer = local->numparam.outer;
12966 NODE *inner = local->numparam.inner;
12967 if (outer || inner) {
12968 NODE *used = outer ? outer : inner;
12969 compile_error(p, "numbered parameter is already used in\n"
12970 "%s:%d: %s block here",
12971 p->ruby_sourcefile, nd_line(used),
12972 outer ? "outer" : "inner");
12973 parser_show_error_line(p, &used->nd_loc);
12974 return 1;
12975 }
12976 return 0;
12977}
12978
12979static int
12980numparam_used_p(struct parser_params *p)
12981{
12982 NODE *numparam = p->lvtbl->numparam.current;
12983 if (numparam) {
12984 compile_error(p, "numbered parameter is already used in\n"
12985 "%s:%d: current block here",
12986 p->ruby_sourcefile, nd_line(numparam));
12987 parser_show_error_line(p, &numparam->nd_loc);
12988 return 1;
12989 }
12990 return 0;
12991}
12992
12993static int
12994it_used_p(struct parser_params *p)
12995{
12996 NODE *it = p->lvtbl->it;
12997 if (it) {
12998 compile_error(p, "'it' is already used in\n"
12999 "%s:%d: current block here",
13000 p->ruby_sourcefile, nd_line(it));
13001 parser_show_error_line(p, &it->nd_loc);
13002 return 1;
13003 }
13004 return 0;
13005}
13006
13007static NODE*
13008gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
13009{
13010 ID *vidp = NULL;
13011 NODE *node;
13012 switch (id) {
13013 case keyword_self:
13014 return NEW_SELF(loc);
13015 case keyword_nil:
13016 return NEW_NIL(loc);
13017 case keyword_true:
13018 return NEW_TRUE(loc);
13019 case keyword_false:
13020 return NEW_FALSE(loc);
13021 case keyword__FILE__:
13022 {
13023 VALUE file = p->ruby_sourcefile_string;
13024 if (NIL_P(file))
13025 file = rb_str_new(0, 0);
13026 node = NEW_FILE(file, loc);
13027 }
13028 return node;
13029 case keyword__LINE__:
13030 return NEW_LINE(loc);
13031 case keyword__ENCODING__:
13032 return NEW_ENCODING(loc);
13033
13034 }
13035 switch (id_type(id)) {
13036 case ID_LOCAL:
13037 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13038 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13039 if (vidp) *vidp |= LVAR_USED;
13040 node = NEW_DVAR(id, loc);
13041 return node;
13042 }
13043 if (local_id_ref(p, id, &vidp)) {
13044 if (vidp) *vidp |= LVAR_USED;
13045 node = NEW_LVAR(id, loc);
13046 return node;
13047 }
13048 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13049 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13050 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13051 node = NEW_DVAR(id, loc);
13052 struct local_vars *local = p->lvtbl;
13053 if (!local->numparam.current) local->numparam.current = node;
13054 return node;
13055 }
13056# if WARN_PAST_SCOPE
13057 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13058 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13059 }
13060# endif
13061 /* method call without arguments */
13062 if (dyna_in_block(p) && id == rb_intern("it") && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13063 if (numparam_used_p(p)) return 0;
13064 if (p->max_numparam == ORDINAL_PARAM) {
13065 compile_error(p, "ordinary parameter is defined");
13066 return 0;
13067 }
13068 if (!p->it_id) {
13069 p->it_id = internal_id(p);
13070 vtable_add(p->lvtbl->args, p->it_id);
13071 }
13072 NODE *node = NEW_DVAR(p->it_id, loc);
13073 if (!p->lvtbl->it) p->lvtbl->it = node;
13074 return node;
13075 }
13076 return NEW_VCALL(id, loc);
13077 case ID_GLOBAL:
13078 return NEW_GVAR(id, loc);
13079 case ID_INSTANCE:
13080 return NEW_IVAR(id, loc);
13081 case ID_CONST:
13082 return NEW_CONST(id, loc);
13083 case ID_CLASS:
13084 return NEW_CVAR(id, loc);
13085 }
13086 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13087 return 0;
13088}
13089
13090static rb_node_opt_arg_t *
13091opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13092{
13093 rb_node_opt_arg_t *opts = opt_list;
13094 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13095
13096 while (opts->nd_next) {
13097 opts = opts->nd_next;
13098 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13099 }
13100 opts->nd_next = opt;
13101
13102 return opt_list;
13103}
13104
13105static rb_node_kw_arg_t *
13106kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13107{
13108 if (kwlist) {
13109 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13110 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13111 }
13112 return kwlist;
13113}
13114
13115static NODE *
13116new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
13117{
13118 NODE *n = expr;
13119 while (n) {
13120 if (nd_type_p(n, NODE_BEGIN)) {
13121 n = RNODE_BEGIN(n)->nd_body;
13122 }
13123 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13124 n = RNODE_BLOCK(n)->nd_head;
13125 }
13126 else {
13127 break;
13128 }
13129 }
13130 return NEW_DEFINED(n, loc);
13131}
13132
13133static NODE*
13134str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13135{
13136 VALUE lit;
13137 rb_parser_string_t *str = RNODE_STR(node)->string;
13138 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13139 yyerror1(loc, "invalid symbol");
13140 lit = STR_NEW0();
13141 }
13142 else {
13143 lit = rb_str_new_parser_string(str);
13144 }
13145 return NEW_SYM(lit, loc);
13146}
13147
13148static NODE*
13149symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13150{
13151 enum node_type type = nd_type(symbol);
13152 switch (type) {
13153 case NODE_DSTR:
13154 nd_set_type(symbol, NODE_DSYM);
13155 break;
13156 case NODE_STR:
13157 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13158 break;
13159 default:
13160 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13161 }
13162 return list_append(p, symbols, symbol);
13163}
13164
13165static NODE *
13166new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
13167{
13168 struct RNode_LIST *list;
13169 NODE *prev;
13170
13171 if (!node) {
13172 /* Check string is valid regex */
13173 rb_parser_string_t *str = STRING_NEW0();
13174 reg_compile(p, str, options);
13175 node = NEW_REGX(str, options, loc);
13176 return node;
13177 }
13178 switch (nd_type(node)) {
13179 case NODE_STR:
13180 {
13181 /* Check string is valid regex */
13182 reg_compile(p, RNODE_STR(node)->string, options);
13183 node = str2regx(p, node, options);
13184 }
13185 break;
13186 default:
13187 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13188 /* fall through */
13189 case NODE_DSTR:
13190 nd_set_type(node, NODE_DREGX);
13191 nd_set_loc(node, loc);
13192 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13193 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13194 if (dreg->string) reg_fragment_check(p, dreg->string, options);
13195 prev = node;
13196 for (list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13197 NODE *frag = list->nd_head;
13198 enum node_type type = nd_type(frag);
13199 if (type == NODE_STR || (type == NODE_DSTR && !RNODE_DSTR(frag)->nd_next)) {
13200 rb_parser_string_t *tail = RNODE_STR(frag)->string;
13201 if (reg_fragment_check(p, tail, options) && prev && RNODE_DREGX(prev)->string) {
13202 rb_parser_string_t *lit = prev == node ? dreg->string : RNODE_STR(RNODE_LIST(prev)->nd_head)->string;
13203 if (!literal_concat0(p, lit, tail)) {
13204 return NEW_NIL(loc); /* dummy node on error */
13205 }
13206 rb_parser_str_resize(p, tail, 0);
13207 RNODE_LIST(prev)->nd_next = list->nd_next;
13208 rb_discard_node(p, list->nd_head);
13209 rb_discard_node(p, (NODE *)list);
13210 list = RNODE_LIST(prev);
13211 }
13212 else {
13213 prev = (NODE *)list;
13214 }
13215 }
13216 else {
13217 prev = 0;
13218 }
13219 }
13220 if (!dreg->nd_next) {
13221 /* Check string is valid regex */
13222 reg_compile(p, dreg->string, options);
13223 }
13224 if (options & RE_OPTION_ONCE) {
13225 node = NEW_ONCE(node, loc);
13226 }
13227 break;
13228 }
13229 return node;
13230}
13231
13232static rb_node_kw_arg_t *
13233new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13234{
13235 if (!k) return 0;
13236 return NEW_KW_ARG((k), loc);
13237}
13238
13239static NODE *
13240new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13241{
13242 if (!node) {
13243 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13244 return xstr;
13245 }
13246 switch (nd_type(node)) {
13247 case NODE_STR:
13248 nd_set_type(node, NODE_XSTR);
13249 nd_set_loc(node, loc);
13250 break;
13251 case NODE_DSTR:
13252 nd_set_type(node, NODE_DXSTR);
13253 nd_set_loc(node, loc);
13254 break;
13255 default:
13256 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13257 break;
13258 }
13259 return node;
13260}
13261
13262static const
13263struct st_hash_type literal_type = {
13264 literal_cmp,
13265 literal_hash,
13266};
13267
13268static int nd_type_st_key_enable_p(NODE *node);
13269
13270static void
13271check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13272{
13273 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13274 if (!arg || !p->case_labels) return;
13275 if (!nd_type_st_key_enable_p(arg)) return;
13276
13277 if (p->case_labels == CHECK_LITERAL_WHEN) {
13278 p->case_labels = st_init_table(&literal_type);
13279 }
13280 else {
13281 st_data_t line;
13282 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13283 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13284 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13285 return;
13286 }
13287 }
13288 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13289}
13290
13291#ifdef RIPPER
13292static int
13293id_is_var(struct parser_params *p, ID id)
13294{
13295 if (is_notop_id(id)) {
13296 switch (id & ID_SCOPE_MASK) {
13297 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13298 return 1;
13299 case ID_LOCAL:
13300 if (dyna_in_block(p)) {
13301 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13302 }
13303 if (local_id(p, id)) return 1;
13304 /* method call without arguments */
13305 return 0;
13306 }
13307 }
13308 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13309 return 0;
13310}
13311#endif
13312
13313static inline enum lex_state_e
13314parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13315{
13316 if (p->debug) {
13317 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13318 }
13319 return p->lex.state = ls;
13320}
13321
13322#ifndef RIPPER
13323static void
13324flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13325{
13326 VALUE mesg = p->debug_buffer;
13327
13328 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13329 p->debug_buffer = Qnil;
13330 rb_io_puts(1, &mesg, out);
13331 }
13332 if (!NIL_P(str) && RSTRING_LEN(str)) {
13333 rb_io_write(p->debug_output, str);
13334 }
13335}
13336
13337static const char rb_parser_lex_state_names[][8] = {
13338 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13339 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13340 "LABEL", "LABELED","FITEM",
13341};
13342
13343static VALUE
13344append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13345{
13346 int i, sep = 0;
13347 unsigned int mask = 1;
13348 static const char none[] = "NONE";
13349
13350 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13351 if ((unsigned)state & mask) {
13352 if (sep) {
13353 rb_str_cat(buf, "|", 1);
13354 }
13355 sep = 1;
13356 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13357 }
13358 }
13359 if (!sep) {
13360 rb_str_cat(buf, none, sizeof(none)-1);
13361 }
13362 return buf;
13363}
13364
13365enum lex_state_e
13366rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13367 enum lex_state_e to, int line)
13368{
13369 VALUE mesg;
13370 mesg = rb_str_new_cstr("lex_state: ");
13371 append_lex_state_name(p, from, mesg);
13372 rb_str_cat_cstr(mesg, " -> ");
13373 append_lex_state_name(p, to, mesg);
13374 rb_str_catf(mesg, " at line %d\n", line);
13375 flush_debug_buffer(p, p->debug_output, mesg);
13376 return to;
13377}
13378
13379VALUE
13380rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13381{
13382 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13383}
13384
13385static void
13386append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13387{
13388 if (stack == 0) {
13389 rb_str_cat_cstr(mesg, "0");
13390 }
13391 else {
13392 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13393 for (; mask && !(stack & mask); mask >>= 1) continue;
13394 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13395 }
13396}
13397
13398void
13399rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13400 const char *name, int line)
13401{
13402 VALUE mesg = rb_sprintf("%s: ", name);
13403 append_bitstack_value(p, stack, mesg);
13404 rb_str_catf(mesg, " at line %d\n", line);
13405 flush_debug_buffer(p, p->debug_output, mesg);
13406}
13407
13408void
13409rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13410{
13411 va_list ap;
13412 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13413
13414 va_start(ap, fmt);
13415 rb_str_vcatf(mesg, fmt, ap);
13416 va_end(ap);
13417 yyerror0(RSTRING_PTR(mesg));
13418 RB_GC_GUARD(mesg);
13419
13420 mesg = rb_str_new(0, 0);
13421 append_lex_state_name(p, p->lex.state, mesg);
13422 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13423 rb_str_resize(mesg, 0);
13424 append_bitstack_value(p, p->cond_stack, mesg);
13425 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13426 rb_str_resize(mesg, 0);
13427 append_bitstack_value(p, p->cmdarg_stack, mesg);
13428 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13429 if (p->debug_output == rb_ractor_stdout())
13430 p->debug_output = rb_ractor_stderr();
13431 p->debug = TRUE;
13432}
13433
13434static YYLTYPE *
13435rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13436{
13437 yylloc->beg_pos.lineno = sourceline;
13438 yylloc->beg_pos.column = beg_pos;
13439 yylloc->end_pos.lineno = sourceline;
13440 yylloc->end_pos.column = end_pos;
13441 return yylloc;
13442}
13443
13444YYLTYPE *
13445rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13446{
13447 int sourceline = here->sourceline;
13448 int beg_pos = (int)here->offset - here->quote
13449 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13450 int end_pos = (int)here->offset + here->length + here->quote;
13451
13452 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13453}
13454
13455YYLTYPE *
13456rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13457{
13458 yylloc->beg_pos.lineno = p->delayed.beg_line;
13459 yylloc->beg_pos.column = p->delayed.beg_col;
13460 yylloc->end_pos.lineno = p->delayed.end_line;
13461 yylloc->end_pos.column = p->delayed.end_col;
13462
13463 return yylloc;
13464}
13465
13466YYLTYPE *
13467rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13468{
13469 int sourceline = p->ruby_sourceline;
13470 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13471 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13472 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13473}
13474
13475YYLTYPE *
13476rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13477{
13478 yylloc->end_pos = yylloc->beg_pos;
13479
13480 return yylloc;
13481}
13482
13483YYLTYPE *
13484rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13485{
13486 int sourceline = p->ruby_sourceline;
13487 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13488 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13489 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13490}
13491
13492YYLTYPE *
13493rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13494{
13495 int sourceline = p->ruby_sourceline;
13496 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13497 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13498 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13499}
13500#endif /* !RIPPER */
13501
13502static int
13503assignable0(struct parser_params *p, ID id, const char **err)
13504{
13505 if (!id) return -1;
13506 switch (id) {
13507 case keyword_self:
13508 *err = "Can't change the value of self";
13509 return -1;
13510 case keyword_nil:
13511 *err = "Can't assign to nil";
13512 return -1;
13513 case keyword_true:
13514 *err = "Can't assign to true";
13515 return -1;
13516 case keyword_false:
13517 *err = "Can't assign to false";
13518 return -1;
13519 case keyword__FILE__:
13520 *err = "Can't assign to __FILE__";
13521 return -1;
13522 case keyword__LINE__:
13523 *err = "Can't assign to __LINE__";
13524 return -1;
13525 case keyword__ENCODING__:
13526 *err = "Can't assign to __ENCODING__";
13527 return -1;
13528 }
13529 switch (id_type(id)) {
13530 case ID_LOCAL:
13531 if (dyna_in_block(p)) {
13532 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13533 compile_error(p, "Can't assign to numbered parameter _%d",
13534 NUMPARAM_ID_TO_IDX(id));
13535 return -1;
13536 }
13537 if (dvar_curr(p, id)) return NODE_DASGN;
13538 if (dvar_defined(p, id)) return NODE_DASGN;
13539 if (local_id(p, id)) return NODE_LASGN;
13540 dyna_var(p, id);
13541 return NODE_DASGN;
13542 }
13543 else {
13544 if (!local_id(p, id)) local_var(p, id);
13545 return NODE_LASGN;
13546 }
13547 break;
13548 case ID_GLOBAL: return NODE_GASGN;
13549 case ID_INSTANCE: return NODE_IASGN;
13550 case ID_CONST:
13551 if (!p->ctxt.in_def) return NODE_CDECL;
13552 *err = "dynamic constant assignment";
13553 return -1;
13554 case ID_CLASS: return NODE_CVASGN;
13555 default:
13556 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13557 }
13558 return -1;
13559}
13560
13561static NODE*
13562assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13563{
13564 const char *err = 0;
13565 int node_type = assignable0(p, id, &err);
13566 switch (node_type) {
13567 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13568 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13569 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13570 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13571 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13572 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13573 }
13574/* TODO: FIXME */
13575#ifndef RIPPER
13576 if (err) yyerror1(loc, err);
13577#else
13578 if (err) set_value(assign_error(p, err, p->s_lvalue));
13579#endif
13580 return NEW_ERROR(loc);
13581}
13582
13583static int
13584is_private_local_id(struct parser_params *p, ID name)
13585{
13586 VALUE s;
13587 if (name == idUScore) return 1;
13588 if (!is_local_id(name)) return 0;
13589 s = rb_id2str(name);
13590 if (!s) return 0;
13591 return RSTRING_PTR(s)[0] == '_';
13592}
13593
13594static int
13595shadowing_lvar_0(struct parser_params *p, ID name)
13596{
13597 if (dyna_in_block(p)) {
13598 if (dvar_curr(p, name)) {
13599 if (is_private_local_id(p, name)) return 1;
13600 yyerror0("duplicated argument name");
13601 }
13602 else if (dvar_defined(p, name) || local_id(p, name)) {
13603 vtable_add(p->lvtbl->vars, name);
13604 if (p->lvtbl->used) {
13605 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13606 }
13607 return 0;
13608 }
13609 }
13610 else {
13611 if (local_id(p, name)) {
13612 if (is_private_local_id(p, name)) return 1;
13613 yyerror0("duplicated argument name");
13614 }
13615 }
13616 return 1;
13617}
13618
13619static ID
13620shadowing_lvar(struct parser_params *p, ID name)
13621{
13622 shadowing_lvar_0(p, name);
13623 return name;
13624}
13625
13626static void
13627new_bv(struct parser_params *p, ID name)
13628{
13629 if (!name) return;
13630 if (!is_local_id(name)) {
13631 compile_error(p, "invalid local variable - %"PRIsVALUE,
13632 rb_id2str(name));
13633 return;
13634 }
13635 if (!shadowing_lvar_0(p, name)) return;
13636 dyna_var(p, name);
13637 ID *vidp = 0;
13638 if (dvar_defined_ref(p, name, &vidp)) {
13639 if (vidp) *vidp |= LVAR_USED;
13640 }
13641}
13642
13643static void
13644aryset_check(struct parser_params *p, NODE *args)
13645{
13646 NODE *block = 0, *kwds = 0;
13647 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13648 block = RNODE_BLOCK_PASS(args)->nd_body;
13649 args = RNODE_BLOCK_PASS(args)->nd_head;
13650 }
13651 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13652 args = RNODE_ARGSCAT(args)->nd_body;
13653 }
13654 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13655 kwds = RNODE_ARGSPUSH(args)->nd_body;
13656 }
13657 else {
13658 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13659 next = RNODE_LIST(next)->nd_next) {
13660 kwds = RNODE_LIST(next)->nd_head;
13661 }
13662 }
13663 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13664 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13665 }
13666 if (block) {
13667 yyerror1(&block->nd_loc, "block arg given in index assignment");
13668 }
13669}
13670
13671static NODE *
13672aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13673{
13674 aryset_check(p, idx);
13675 return NEW_ATTRASGN(recv, tASET, idx, loc);
13676}
13677
13678static void
13679block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13680{
13681 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13682 compile_error(p, "both block arg and actual block given");
13683 }
13684}
13685
13686static NODE *
13687attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13688{
13689 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13690 return NEW_ATTRASGN(recv, id, 0, loc);
13691}
13692
13693static VALUE
13694rb_backref_error(struct parser_params *p, NODE *node)
13695{
13696#ifndef RIPPER
13697# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13698#else
13699# define ERR(...) rb_sprintf(__VA_ARGS__)
13700#endif
13701 switch (nd_type(node)) {
13702 case NODE_NTH_REF:
13703 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13704 case NODE_BACK_REF:
13705 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13706 }
13707#undef ERR
13708 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13709}
13710
13711static NODE *
13712arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13713{
13714 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13715 switch (nd_type(node1)) {
13716 case NODE_LIST:
13717 return list_append(p, node1, node2);
13718 case NODE_BLOCK_PASS:
13719 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13720 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13721 return node1;
13722 case NODE_ARGSPUSH:
13723 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13724 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13725 nd_set_type(node1, NODE_ARGSCAT);
13726 return node1;
13727 case NODE_ARGSCAT:
13728 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13729 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13730 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13731 return node1;
13732 }
13733 return NEW_ARGSPUSH(node1, node2, loc);
13734}
13735
13736static NODE *
13737arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13738{
13739 if (!node2) return node1;
13740 switch (nd_type(node1)) {
13741 case NODE_BLOCK_PASS:
13742 if (RNODE_BLOCK_PASS(node1)->nd_head)
13743 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13744 else
13745 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13746 return node1;
13747 case NODE_ARGSPUSH:
13748 if (!nd_type_p(node2, NODE_LIST)) break;
13749 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13750 nd_set_type(node1, NODE_ARGSCAT);
13751 return node1;
13752 case NODE_ARGSCAT:
13753 if (!nd_type_p(node2, NODE_LIST) ||
13754 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13755 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13756 return node1;
13757 }
13758 return NEW_ARGSCAT(node1, node2, loc);
13759}
13760
13761static NODE *
13762last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13763{
13764 NODE *n1;
13765 if ((n1 = splat_array(args)) != 0) {
13766 return list_append(p, n1, last_arg);
13767 }
13768 return arg_append(p, args, last_arg, loc);
13769}
13770
13771static NODE *
13772rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13773{
13774 NODE *n1;
13775 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13776 return list_concat(n1, rest_arg);
13777 }
13778 return arg_concat(p, args, rest_arg, loc);
13779}
13780
13781static NODE *
13782splat_array(NODE* node)
13783{
13784 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13785 if (nd_type_p(node, NODE_LIST)) return node;
13786 return 0;
13787}
13788
13789static void
13790mark_lvar_used(struct parser_params *p, NODE *rhs)
13791{
13792 ID *vidp = NULL;
13793 if (!rhs) return;
13794 switch (nd_type(rhs)) {
13795 case NODE_LASGN:
13796 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13797 if (vidp) *vidp |= LVAR_USED;
13798 }
13799 break;
13800 case NODE_DASGN:
13801 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13802 if (vidp) *vidp |= LVAR_USED;
13803 }
13804 break;
13805#if 0
13806 case NODE_MASGN:
13807 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13808 mark_lvar_used(p, rhs->nd_head);
13809 }
13810 break;
13811#endif
13812 }
13813}
13814
13815static int is_static_content(NODE *node);
13816
13817static NODE *
13818node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13819{
13820 if (!lhs) return 0;
13821
13822 switch (nd_type(lhs)) {
13823 case NODE_CDECL:
13824 case NODE_GASGN:
13825 case NODE_IASGN:
13826 case NODE_LASGN:
13827 case NODE_DASGN:
13828 case NODE_MASGN:
13829 case NODE_CVASGN:
13830 set_nd_value(p, lhs, rhs);
13831 nd_set_loc(lhs, loc);
13832 break;
13833
13834 case NODE_ATTRASGN:
13835 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13836 nd_set_loc(lhs, loc);
13837 break;
13838
13839 default:
13840 /* should not happen */
13841 break;
13842 }
13843
13844 return lhs;
13845}
13846
13847static NODE *
13848value_expr_check(struct parser_params *p, NODE *node)
13849{
13850 NODE *void_node = 0, *vn;
13851
13852 if (!node) {
13853 rb_warning0("empty expression");
13854 }
13855 while (node) {
13856 switch (nd_type(node)) {
13857 case NODE_ENSURE:
13858 vn = RNODE_ENSURE(node)->nd_head;
13859 node = RNODE_ENSURE(node)->nd_ensr;
13860 /* nd_ensr should not be NULL, check it out next */
13861 if (vn && (vn = value_expr_check(p, vn))) {
13862 goto found;
13863 }
13864 break;
13865
13866 case NODE_RESCUE:
13867 /* void only if all children are void */
13868 vn = RNODE_RESCUE(node)->nd_head;
13869 if (!vn || !(vn = value_expr_check(p, vn))) return NULL;
13870 if (!void_node) void_node = vn;
13871 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13872 if (!nd_type_p(r, NODE_RESBODY)) {
13873 compile_error(p, "unexpected node");
13874 return NULL;
13875 }
13876 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13877 void_node = 0;
13878 break;
13879 }
13880 if (!void_node) void_node = vn;
13881 }
13882 node = RNODE_RESCUE(node)->nd_else;
13883 if (!node) return void_node;
13884 break;
13885
13886 case NODE_RETURN:
13887 case NODE_BREAK:
13888 case NODE_NEXT:
13889 case NODE_REDO:
13890 case NODE_RETRY:
13891 goto found;
13892
13893 case NODE_CASE3:
13894 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13895 compile_error(p, "unexpected node");
13896 return NULL;
13897 }
13898 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13899 return NULL;
13900 }
13901 /* single line pattern matching with "=>" operator */
13902 goto found;
13903
13904 case NODE_BLOCK:
13905 while (RNODE_BLOCK(node)->nd_next) {
13906 node = RNODE_BLOCK(node)->nd_next;
13907 }
13908 node = RNODE_BLOCK(node)->nd_head;
13909 break;
13910
13911 case NODE_BEGIN:
13912 node = RNODE_BEGIN(node)->nd_body;
13913 break;
13914
13915 case NODE_IF:
13916 case NODE_UNLESS:
13917 if (!RNODE_IF(node)->nd_body) {
13918 return NULL;
13919 }
13920 else if (!RNODE_IF(node)->nd_else) {
13921 return NULL;
13922 }
13923 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13924 if (!vn) return NULL;
13925 if (!void_node) void_node = vn;
13926 node = RNODE_IF(node)->nd_else;
13927 break;
13928
13929 case NODE_AND:
13930 case NODE_OR:
13931 node = RNODE_AND(node)->nd_1st;
13932 break;
13933
13934 case NODE_LASGN:
13935 case NODE_DASGN:
13936 case NODE_MASGN:
13937 mark_lvar_used(p, node);
13938 return NULL;
13939
13940 default:
13941 return NULL;
13942 }
13943 }
13944
13945 return NULL;
13946
13947 found:
13948 /* return the first found node */
13949 return void_node ? void_node : node;
13950}
13951
13952static int
13953value_expr_gen(struct parser_params *p, NODE *node)
13954{
13955 NODE *void_node = value_expr_check(p, node);
13956 if (void_node) {
13957 yyerror1(&void_node->nd_loc, "void value expression");
13958 /* or "control never reach"? */
13959 return FALSE;
13960 }
13961 return TRUE;
13962}
13963
13964static void
13965void_expr(struct parser_params *p, NODE *node)
13966{
13967 const char *useless = 0;
13968
13969 if (!RTEST(ruby_verbose)) return;
13970
13971 if (!node || !(node = nd_once_body(node))) return;
13972 switch (nd_type(node)) {
13973 case NODE_OPCALL:
13974 switch (RNODE_OPCALL(node)->nd_mid) {
13975 case '+':
13976 case '-':
13977 case '*':
13978 case '/':
13979 case '%':
13980 case tPOW:
13981 case tUPLUS:
13982 case tUMINUS:
13983 case '|':
13984 case '^':
13985 case '&':
13986 case tCMP:
13987 case '>':
13988 case tGEQ:
13989 case '<':
13990 case tLEQ:
13991 case tEQ:
13992 case tNEQ:
13993 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13994 break;
13995 }
13996 break;
13997
13998 case NODE_LVAR:
13999 case NODE_DVAR:
14000 case NODE_GVAR:
14001 case NODE_IVAR:
14002 case NODE_CVAR:
14003 case NODE_NTH_REF:
14004 case NODE_BACK_REF:
14005 useless = "a variable";
14006 break;
14007 case NODE_CONST:
14008 useless = "a constant";
14009 break;
14010 case NODE_SYM:
14011 case NODE_LINE:
14012 case NODE_FILE:
14013 case NODE_ENCODING:
14014 case NODE_INTEGER:
14015 case NODE_FLOAT:
14016 case NODE_RATIONAL:
14017 case NODE_IMAGINARY:
14018 case NODE_STR:
14019 case NODE_DSTR:
14020 case NODE_REGX:
14021 case NODE_DREGX:
14022 useless = "a literal";
14023 break;
14024 case NODE_COLON2:
14025 case NODE_COLON3:
14026 useless = "::";
14027 break;
14028 case NODE_DOT2:
14029 useless = "..";
14030 break;
14031 case NODE_DOT3:
14032 useless = "...";
14033 break;
14034 case NODE_SELF:
14035 useless = "self";
14036 break;
14037 case NODE_NIL:
14038 useless = "nil";
14039 break;
14040 case NODE_TRUE:
14041 useless = "true";
14042 break;
14043 case NODE_FALSE:
14044 useless = "false";
14045 break;
14046 case NODE_DEFINED:
14047 useless = "defined?";
14048 break;
14049 }
14050
14051 if (useless) {
14052 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14053 }
14054}
14055
14056/* warns useless use of block and returns the last statement node */
14057static NODE *
14058void_stmts(struct parser_params *p, NODE *node)
14059{
14060 NODE *const n = node;
14061 if (!RTEST(ruby_verbose)) return n;
14062 if (!node) return n;
14063 if (!nd_type_p(node, NODE_BLOCK)) return n;
14064
14065 while (RNODE_BLOCK(node)->nd_next) {
14066 void_expr(p, RNODE_BLOCK(node)->nd_head);
14067 node = RNODE_BLOCK(node)->nd_next;
14068 }
14069 return RNODE_BLOCK(node)->nd_head;
14070}
14071
14072static NODE *
14073remove_begin(NODE *node)
14074{
14075 NODE **n = &node, *n1 = node;
14076 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14077 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14078 }
14079 return node;
14080}
14081
14082static void
14083reduce_nodes(struct parser_params *p, NODE **body)
14084{
14085 NODE *node = *body;
14086
14087 if (!node) {
14088 *body = NEW_NIL(&NULL_LOC);
14089 return;
14090 }
14091#define subnodes(type, n1, n2) \
14092 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14093 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14094 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14095
14096 while (node) {
14097 int newline = (int)nd_fl_newline(node);
14098 switch (nd_type(node)) {
14099 end:
14100 case NODE_NIL:
14101 *body = 0;
14102 return;
14103 case NODE_BEGIN:
14104 *body = node = RNODE_BEGIN(node)->nd_body;
14105 if (newline && node) nd_set_fl_newline(node);
14106 continue;
14107 case NODE_BLOCK:
14108 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14109 break;
14110 case NODE_IF:
14111 case NODE_UNLESS:
14112 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14113 return;
14114 case NODE_CASE:
14115 body = &RNODE_CASE(node)->nd_body;
14116 break;
14117 case NODE_WHEN:
14118 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14119 break;
14120 case NODE_ENSURE:
14121 body = &RNODE_ENSURE(node)->nd_head;
14122 break;
14123 case NODE_RESCUE:
14124 newline = 0; // RESBODY should not be a NEWLINE
14125 if (RNODE_RESCUE(node)->nd_else) {
14126 body = &RNODE_RESCUE(node)->nd_resq;
14127 break;
14128 }
14129 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14130 break;
14131 default:
14132 return;
14133 }
14134 node = *body;
14135 if (newline && node) nd_set_fl_newline(node);
14136 }
14137
14138#undef subnodes
14139}
14140
14141static int
14142is_static_content(NODE *node)
14143{
14144 if (!node) return 1;
14145 switch (nd_type(node)) {
14146 case NODE_HASH:
14147 if (!(node = RNODE_HASH(node)->nd_head)) break;
14148 case NODE_LIST:
14149 do {
14150 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14151 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14152 case NODE_SYM:
14153 case NODE_REGX:
14154 case NODE_LINE:
14155 case NODE_FILE:
14156 case NODE_ENCODING:
14157 case NODE_INTEGER:
14158 case NODE_FLOAT:
14159 case NODE_RATIONAL:
14160 case NODE_IMAGINARY:
14161 case NODE_STR:
14162 case NODE_NIL:
14163 case NODE_TRUE:
14164 case NODE_FALSE:
14165 case NODE_ZLIST:
14166 break;
14167 default:
14168 return 0;
14169 }
14170 return 1;
14171}
14172
14173static int
14174assign_in_cond(struct parser_params *p, NODE *node)
14175{
14176 switch (nd_type(node)) {
14177 case NODE_MASGN:
14178 case NODE_LASGN:
14179 case NODE_DASGN:
14180 case NODE_GASGN:
14181 case NODE_IASGN:
14182 case NODE_CVASGN:
14183 case NODE_CDECL:
14184 break;
14185
14186 default:
14187 return 0;
14188 }
14189
14190 if (!get_nd_value(p, node)) return 1;
14191 if (is_static_content(get_nd_value(p, node))) {
14192 /* reports always */
14193 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14194 }
14195 return 1;
14196}
14197
14198enum cond_type {
14199 COND_IN_OP,
14200 COND_IN_COND,
14201 COND_IN_FF
14202};
14203
14204#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14205 switch (t) { \
14206 case COND_IN_OP: break; \
14207 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14208 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14209 } \
14210} while (0)
14211
14212static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14213
14214static NODE*
14215range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14216{
14217 enum node_type type;
14218
14219 if (node == 0) return 0;
14220
14221 type = nd_type(node);
14222 value_expr(node);
14223 if (type == NODE_INTEGER) {
14224 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14225 ID lineno = rb_intern("$.");
14226 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14227 }
14228 return cond0(p, node, COND_IN_FF, loc, true);
14229}
14230
14231static NODE*
14232cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14233{
14234 if (node == 0) return 0;
14235 if (!(node = nd_once_body(node))) return 0;
14236 assign_in_cond(p, node);
14237
14238 switch (nd_type(node)) {
14239 case NODE_BEGIN:
14240 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14241 break;
14242
14243 case NODE_DSTR:
14244 case NODE_EVSTR:
14245 case NODE_STR:
14246 case NODE_FILE:
14247 SWITCH_BY_COND_TYPE(type, warn, "string ");
14248 break;
14249
14250 case NODE_REGX:
14251 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14252 nd_set_type(node, NODE_MATCH);
14253 break;
14254
14255 case NODE_DREGX:
14256 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14257
14258 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14259
14260 case NODE_BLOCK:
14261 {
14262 NODE *end = RNODE_BLOCK(node)->nd_end;
14263 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14264 if (top) top = node == end;
14265 *expr = cond0(p, *expr, type, loc, top);
14266 }
14267 break;
14268
14269 case NODE_AND:
14270 case NODE_OR:
14271 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14272 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14273 break;
14274
14275 case NODE_DOT2:
14276 case NODE_DOT3:
14277 if (!top) break;
14278 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14279 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14280 switch (nd_type(node)) {
14281 case NODE_DOT2:
14282 nd_set_type(node,NODE_FLIP2);
14283 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14284 (void)flip2;
14285 break;
14286 case NODE_DOT3:
14287 nd_set_type(node, NODE_FLIP3);
14288 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14289 (void)flip3;
14290 break;
14291 }
14292 break;
14293
14294 case NODE_SYM:
14295 case NODE_DSYM:
14296 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14297 break;
14298
14299 case NODE_LINE:
14300 SWITCH_BY_COND_TYPE(type, warning, "");
14301 break;
14302
14303 case NODE_ENCODING:
14304 SWITCH_BY_COND_TYPE(type, warning, "");
14305 break;
14306
14307 case NODE_INTEGER:
14308 case NODE_FLOAT:
14309 case NODE_RATIONAL:
14310 case NODE_IMAGINARY:
14311 SWITCH_BY_COND_TYPE(type, warning, "");
14312 break;
14313
14314 default:
14315 break;
14316 }
14317 return node;
14318}
14319
14320static NODE*
14321cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14322{
14323 if (node == 0) return 0;
14324 return cond0(p, node, COND_IN_COND, loc, true);
14325}
14326
14327static NODE*
14328method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14329{
14330 if (node == 0) return 0;
14331 return cond0(p, node, COND_IN_OP, loc, true);
14332}
14333
14334static NODE*
14335new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14336{
14337 YYLTYPE loc = {*pos, *pos};
14338 return NEW_NIL(&loc);
14339}
14340
14341static NODE*
14342new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14343{
14344 if (!cc) return right;
14345 cc = cond0(p, cc, COND_IN_COND, loc, true);
14346 return newline_node(NEW_IF(cc, left, right, loc));
14347}
14348
14349static NODE*
14350new_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)
14351{
14352 if (!cc) return right;
14353 cc = cond0(p, cc, COND_IN_COND, loc, true);
14354 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14355}
14356
14357#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))
14358
14359static NODE*
14360logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14361 const YYLTYPE *op_loc, const YYLTYPE *loc)
14362{
14363 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14364 NODE *op;
14365 value_expr(left);
14366 if (left && nd_type_p(left, type)) {
14367 NODE *node = left, *second;
14368 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14369 node = second;
14370 }
14371 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14372 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14373 left->nd_loc.end_pos = loc->end_pos;
14374 return left;
14375 }
14376 op = NEW_AND_OR(type, left, right, loc, op_loc);
14377 nd_set_line(op, op_loc->beg_pos.lineno);
14378 return op;
14379}
14380
14381#undef NEW_AND_OR
14382
14383static void
14384no_blockarg(struct parser_params *p, NODE *node)
14385{
14386 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14387 compile_error(p, "block argument should not be given");
14388 }
14389}
14390
14391static NODE *
14392ret_args(struct parser_params *p, NODE *node)
14393{
14394 if (node) {
14395 no_blockarg(p, node);
14396 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14397 node = RNODE_LIST(node)->nd_head;
14398 }
14399 }
14400 return node;
14401}
14402
14403static NODE *
14404new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14405{
14406 if (node) no_blockarg(p, node);
14407
14408 return NEW_YIELD(node, loc);
14409}
14410
14411static NODE*
14412negate_lit(struct parser_params *p, NODE* node)
14413{
14414 switch (nd_type(node)) {
14415 case NODE_INTEGER:
14416 RNODE_INTEGER(node)->minus = TRUE;
14417 break;
14418 case NODE_FLOAT:
14419 RNODE_FLOAT(node)->minus = TRUE;
14420 break;
14421 case NODE_RATIONAL:
14422 RNODE_RATIONAL(node)->minus = TRUE;
14423 break;
14424 case NODE_IMAGINARY:
14425 RNODE_IMAGINARY(node)->minus = TRUE;
14426 break;
14427 }
14428 return node;
14429}
14430
14431static NODE *
14432arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14433{
14434 if (node2) {
14435 if (!node1) return (NODE *)node2;
14436 node2->nd_head = node1;
14437 nd_set_first_lineno(node2, nd_first_lineno(node1));
14438 nd_set_first_column(node2, nd_first_column(node1));
14439 return (NODE *)node2;
14440 }
14441 return node1;
14442}
14443
14444static bool
14445args_info_empty_p(struct rb_args_info *args)
14446{
14447 if (args->pre_args_num) return false;
14448 if (args->post_args_num) return false;
14449 if (args->rest_arg) return false;
14450 if (args->opt_args) return false;
14451 if (args->block_arg) return false;
14452 if (args->kw_args) return false;
14453 if (args->kw_rest_arg) return false;
14454 return true;
14455}
14456
14457static rb_node_args_t *
14458new_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)
14459{
14460 struct rb_args_info *args = &tail->nd_ainfo;
14461
14462 if (args->forwarding) {
14463 if (rest_arg) {
14464 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14465 return tail;
14466 }
14467 rest_arg = idFWD_REST;
14468 }
14469
14470 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14471 args->pre_init = pre_args ? pre_args->nd_next : 0;
14472
14473 args->post_args_num = post_args ? post_args->nd_plen : 0;
14474 args->post_init = post_args ? post_args->nd_next : 0;
14475 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14476
14477 args->rest_arg = rest_arg;
14478
14479 args->opt_args = opt_args;
14480
14481#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14482 args->ruby2_keywords = args->forwarding;
14483#else
14484 args->ruby2_keywords = 0;
14485#endif
14486
14487 nd_set_loc(RNODE(tail), loc);
14488
14489 return tail;
14490}
14491
14492static rb_node_args_t *
14493new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14494{
14495 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14496 struct rb_args_info *args = &node->nd_ainfo;
14497 if (p->error_p) return node;
14498
14499 args->block_arg = block;
14500 args->kw_args = kw_args;
14501
14502 if (kw_args) {
14503 /*
14504 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14505 * variable order: k1, kr1, k2, &b, internal_id, krest
14506 * #=> <reorder>
14507 * variable order: kr1, k1, k2, internal_id, krest, &b
14508 */
14509 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14510 struct vtable *vtargs = p->lvtbl->args;
14511 rb_node_kw_arg_t *kwn = kw_args;
14512
14513 if (block) block = vtargs->tbl[vtargs->pos-1];
14514 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14515 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14516 while (kwn) {
14517 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14518 --kw_vars;
14519 --required_kw_vars;
14520 kwn = kwn->nd_next;
14521 }
14522
14523 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14524 ID vid = get_nd_vid(p, kwn->nd_body);
14525 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14526 *required_kw_vars++ = vid;
14527 }
14528 else {
14529 *kw_vars++ = vid;
14530 }
14531 }
14532
14533 arg_var(p, kw_bits);
14534 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14535 if (block) arg_var(p, block);
14536
14537 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14538 }
14539 else if (kw_rest_arg == idNil) {
14540 args->no_kwarg = 1;
14541 }
14542 else if (kw_rest_arg) {
14543 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14544 }
14545
14546 return node;
14547}
14548
14549static rb_node_args_t *
14550args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14551{
14552 if (max_numparam > NO_PARAM || it_id) {
14553 if (!args) {
14554 YYLTYPE loc = RUBY_INIT_YYLLOC();
14555 args = new_args_tail(p, 0, 0, 0, 0);
14556 nd_set_loc(RNODE(args), &loc);
14557 }
14558 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14559 }
14560 return args;
14561}
14562
14563static NODE*
14564new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14565{
14566 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14567
14568 if (pre_arg) {
14569 NODE *pre_args = NEW_LIST(pre_arg, loc);
14570 if (RNODE_ARYPTN(aryptn)->pre_args) {
14571 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14572 }
14573 else {
14574 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14575 }
14576 }
14577 return aryptn;
14578}
14579
14580static NODE*
14581new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14582{
14583 if (has_rest) {
14584 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14585 }
14586 else {
14587 rest_arg = NULL;
14588 }
14589 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14590
14591 return node;
14592}
14593
14594static NODE*
14595new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14596{
14597 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14598
14599 return fndptn;
14600}
14601
14602static NODE*
14603new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14604{
14605 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14606 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14607 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14608
14609 return node;
14610}
14611
14612static NODE*
14613new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14614{
14615 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14616 return hshptn;
14617}
14618
14619static NODE*
14620new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14621{
14622 NODE *node, *kw_rest_arg_node;
14623
14624 if (kw_rest_arg == idNil) {
14625 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14626 }
14627 else if (kw_rest_arg) {
14628 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14629 }
14630 else {
14631 kw_rest_arg_node = NULL;
14632 }
14633
14634 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14635
14636 return node;
14637}
14638
14639static NODE*
14640dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14641{
14642 if (!node) {
14643 return NEW_SYM(STR_NEW0(), loc);
14644 }
14645
14646 switch (nd_type(node)) {
14647 case NODE_DSTR:
14648 nd_set_type(node, NODE_DSYM);
14649 nd_set_loc(node, loc);
14650 break;
14651 case NODE_STR:
14652 node = str_to_sym_node(p, node, loc);
14653 break;
14654 default:
14655 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14656 break;
14657 }
14658 return node;
14659}
14660
14661static int
14662nd_type_st_key_enable_p(NODE *node)
14663{
14664 switch (nd_type(node)) {
14665 case NODE_INTEGER:
14666 case NODE_FLOAT:
14667 case NODE_RATIONAL:
14668 case NODE_IMAGINARY:
14669 case NODE_STR:
14670 case NODE_SYM:
14671 case NODE_REGX:
14672 case NODE_LINE:
14673 case NODE_FILE:
14674 case NODE_ENCODING:
14675 return true;
14676 default:
14677 return false;
14678 }
14679}
14680
14681static VALUE
14682nd_value(struct parser_params *p, NODE *node)
14683{
14684 switch (nd_type(node)) {
14685 case NODE_STR:
14686 return rb_node_str_string_val(node);
14687 case NODE_INTEGER:
14688 return rb_node_integer_literal_val(node);
14689 case NODE_FLOAT:
14690 return rb_node_float_literal_val(node);
14691 case NODE_RATIONAL:
14692 return rb_node_rational_literal_val(node);
14693 case NODE_IMAGINARY:
14694 return rb_node_imaginary_literal_val(node);
14695 case NODE_SYM:
14696 return rb_node_sym_string_val(node);
14697 case NODE_REGX:
14698 return rb_node_regx_string_val(node);
14699 case NODE_LINE:
14700 return rb_node_line_lineno_val(node);
14701 case NODE_ENCODING:
14702 return rb_node_encoding_val(node);
14703 case NODE_FILE:
14704 return rb_node_file_path_val(node);
14705 default:
14706 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14707 UNREACHABLE_RETURN(0);
14708 }
14709}
14710
14711static void
14712warn_duplicate_keys(struct parser_params *p, NODE *hash)
14713{
14714 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14715 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14716 while (hash && RNODE_LIST(hash)->nd_next) {
14717 NODE *head = RNODE_LIST(hash)->nd_head;
14718 NODE *value = RNODE_LIST(hash)->nd_next;
14719 NODE *next = RNODE_LIST(value)->nd_next;
14720 st_data_t key;
14721 st_data_t data;
14722
14723 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14724 if (!head) {
14725 head = value;
14726 }
14727
14728 if (nd_type_st_key_enable_p(head)) {
14729 key = (st_data_t)head;
14730
14731 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14732 rb_warn2L(nd_line((NODE *)data),
14733 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14734 nd_value(p, head), WARN_I(nd_line(head)));
14735 }
14736 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14737 }
14738 hash = next;
14739 }
14740 st_free_table(p->warn_duplicate_keys_table);
14741 p->warn_duplicate_keys_table = NULL;
14742}
14743
14744static NODE *
14745new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14746{
14747 if (hash) warn_duplicate_keys(p, hash);
14748 return NEW_HASH(hash, loc);
14749}
14750
14751static void
14752error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14753{
14754 if (is_private_local_id(p, id)) {
14755 return;
14756 }
14757 if (st_is_member(p->pvtbl, id)) {
14758 yyerror1(loc, "duplicated variable name");
14759 }
14760 else {
14761 st_insert(p->pvtbl, (st_data_t)id, 0);
14762 }
14763}
14764
14765static void
14766error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14767{
14768 if (!p->pktbl) {
14769 p->pktbl = st_init_numtable();
14770 }
14771 else if (st_is_member(p->pktbl, key)) {
14772 yyerror1(loc, "duplicated key name");
14773 return;
14774 }
14775 st_insert(p->pktbl, (st_data_t)key, 0);
14776}
14777
14778static NODE *
14779new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14780{
14781 return NEW_HASH(hash, loc);
14782}
14783
14784static NODE *
14785new_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 ID vid = get_nd_vid(p, lhs);
14791 YYLTYPE lhs_loc = lhs->nd_loc;
14792 if (op == tOROP) {
14793 set_nd_value(p, lhs, rhs);
14794 nd_set_loc(lhs, loc);
14795 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14796 }
14797 else if (op == tANDOP) {
14798 set_nd_value(p, lhs, rhs);
14799 nd_set_loc(lhs, loc);
14800 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14801 }
14802 else {
14803 asgn = lhs;
14804 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14805 set_nd_value(p, asgn, rhs);
14806 nd_set_loc(asgn, loc);
14807 }
14808 }
14809 else {
14810 asgn = NEW_ERROR(loc);
14811 }
14812 return asgn;
14813}
14814
14815static NODE *
14816new_ary_op_assign(struct parser_params *p, NODE *ary,
14817 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14818 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14819{
14820 NODE *asgn;
14821
14822 aryset_check(p, args);
14823 args = make_list(args, args_loc);
14824 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14825 fixpos(asgn, ary);
14826 return asgn;
14827}
14828
14829static NODE *
14830new_attr_op_assign(struct parser_params *p, NODE *lhs,
14831 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14832 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14833{
14834 NODE *asgn;
14835
14836 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14837 fixpos(asgn, lhs);
14838 return asgn;
14839}
14840
14841static NODE *
14842new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14843{
14844 NODE *asgn;
14845
14846 if (lhs) {
14847 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14848 }
14849 else {
14850 asgn = NEW_ERROR(loc);
14851 }
14852 fixpos(asgn, lhs);
14853 return asgn;
14854}
14855
14856static NODE *
14857const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14858{
14859 if (p->ctxt.in_def) {
14860#ifndef RIPPER
14861 yyerror1(loc, "dynamic constant assignment");
14862#else
14863 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14864#endif
14865 }
14866 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14867}
14868
14869#ifdef RIPPER
14870static VALUE
14871assign_error(struct parser_params *p, const char *mesg, VALUE a)
14872{
14873 a = dispatch2(assign_error, ERR_MESG(), a);
14874 ripper_error(p);
14875 return a;
14876}
14877#endif
14878
14879static NODE *
14880new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14881{
14882 NODE *result = head;
14883 if (rescue) {
14884 NODE *tmp = rescue_else ? rescue_else : rescue;
14885 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14886
14887 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14888 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14889 }
14890 if (ensure) {
14891 result = NEW_ENSURE(result, ensure, loc);
14892 }
14893 fixpos(result, head);
14894 return result;
14895}
14896
14897static void
14898warn_unused_var(struct parser_params *p, struct local_vars *local)
14899{
14900 int cnt;
14901
14902 if (!local->used) return;
14903 cnt = local->used->pos;
14904 if (cnt != local->vars->pos) {
14905 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14906 }
14907#ifndef RIPPER
14908 ID *v = local->vars->tbl;
14909 ID *u = local->used->tbl;
14910 for (int i = 0; i < cnt; ++i) {
14911 if (!v[i] || (u[i] & LVAR_USED)) continue;
14912 if (is_private_local_id(p, v[i])) continue;
14913 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14914 }
14915#endif
14916}
14917
14918static void
14919local_push(struct parser_params *p, int toplevel_scope)
14920{
14921 struct local_vars *local;
14922 int inherits_dvars = toplevel_scope && compile_for_eval;
14923 int warn_unused_vars = RTEST(ruby_verbose);
14924
14925 local = ALLOC(struct local_vars);
14926 local->prev = p->lvtbl;
14927 local->args = vtable_alloc(0);
14928 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14929#ifndef RIPPER
14930 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14931 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14932#endif
14933 local->numparam.outer = 0;
14934 local->numparam.inner = 0;
14935 local->numparam.current = 0;
14936 local->it = 0;
14937 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14938
14939# if WARN_PAST_SCOPE
14940 local->past = 0;
14941# endif
14942 CMDARG_PUSH(0);
14943 COND_PUSH(0);
14944 p->lvtbl = local;
14945}
14946
14947static void
14948vtable_chain_free(struct parser_params *p, struct vtable *table)
14949{
14950 while (!DVARS_TERMINAL_P(table)) {
14951 struct vtable *cur_table = table;
14952 table = cur_table->prev;
14953 vtable_free(cur_table);
14954 }
14955}
14956
14957static void
14958local_free(struct parser_params *p, struct local_vars *local)
14959{
14960 vtable_chain_free(p, local->used);
14961
14962# if WARN_PAST_SCOPE
14963 vtable_chain_free(p, local->past);
14964# endif
14965
14966 vtable_chain_free(p, local->args);
14967 vtable_chain_free(p, local->vars);
14968
14969 ruby_sized_xfree(local, sizeof(struct local_vars));
14970}
14971
14972static void
14973local_pop(struct parser_params *p)
14974{
14975 struct local_vars *local = p->lvtbl->prev;
14976 if (p->lvtbl->used) {
14977 warn_unused_var(p, p->lvtbl);
14978 }
14979
14980 local_free(p, p->lvtbl);
14981 p->lvtbl = local;
14982
14983 CMDARG_POP();
14984 COND_POP();
14985}
14986
14987static rb_ast_id_table_t *
14988local_tbl(struct parser_params *p)
14989{
14990 int cnt_args = vtable_size(p->lvtbl->args);
14991 int cnt_vars = vtable_size(p->lvtbl->vars);
14992 int cnt = cnt_args + cnt_vars;
14993 int i, j;
14994 rb_ast_id_table_t *tbl;
14995
14996 if (cnt <= 0) return 0;
14997 tbl = rb_ast_new_local_table(p->ast, cnt);
14998 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14999 /* remove IDs duplicated to warn shadowing */
15000 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
15001 ID id = p->lvtbl->vars->tbl[i];
15002 if (!vtable_included(p->lvtbl->args, id)) {
15003 tbl->ids[j++] = id;
15004 }
15005 }
15006 if (j < cnt) {
15007 tbl = rb_ast_resize_latest_local_table(p->ast, j);
15008 }
15009
15010 return tbl;
15011}
15012
15013static void
15014numparam_name(struct parser_params *p, ID id)
15015{
15016 if (!NUMPARAM_ID_P(id)) return;
15017 compile_error(p, "_%d is reserved for numbered parameter",
15018 NUMPARAM_ID_TO_IDX(id));
15019}
15020
15021static void
15022arg_var(struct parser_params *p, ID id)
15023{
15024 numparam_name(p, id);
15025 vtable_add(p->lvtbl->args, id);
15026}
15027
15028static void
15029local_var(struct parser_params *p, ID id)
15030{
15031 numparam_name(p, id);
15032 vtable_add(p->lvtbl->vars, id);
15033 if (p->lvtbl->used) {
15034 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15035 }
15036}
15037
15038#ifndef RIPPER
15039int
15040rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15041{
15042 return rb_local_defined(id, iseq);
15043}
15044#endif
15045
15046static int
15047local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15048{
15049 struct vtable *vars, *args, *used;
15050
15051 vars = p->lvtbl->vars;
15052 args = p->lvtbl->args;
15053 used = p->lvtbl->used;
15054
15055 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15056 vars = vars->prev;
15057 args = args->prev;
15058 if (used) used = used->prev;
15059 }
15060
15061 if (vars && vars->prev == DVARS_INHERIT) {
15062 return rb_parser_local_defined(p, id, p->parent_iseq);
15063 }
15064 else if (vtable_included(args, id)) {
15065 return 1;
15066 }
15067 else {
15068 int i = vtable_included(vars, id);
15069 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15070 return i != 0;
15071 }
15072}
15073
15074static int
15075local_id(struct parser_params *p, ID id)
15076{
15077 return local_id_ref(p, id, NULL);
15078}
15079
15080static int
15081check_forwarding_args(struct parser_params *p)
15082{
15083 if (local_id(p, idFWD_ALL)) return TRUE;
15084 compile_error(p, "unexpected ...");
15085 return FALSE;
15086}
15087
15088static void
15089add_forwarding_args(struct parser_params *p)
15090{
15091 arg_var(p, idFWD_REST);
15092#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15093 arg_var(p, idFWD_KWREST);
15094#endif
15095 arg_var(p, idFWD_BLOCK);
15096 arg_var(p, idFWD_ALL);
15097}
15098
15099static void
15100forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15101{
15102 bool conflict = false;
15103
15104 struct vtable *vars, *args;
15105
15106 vars = p->lvtbl->vars;
15107 args = p->lvtbl->args;
15108
15109 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15110 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15111 vars = vars->prev;
15112 args = args->prev;
15113 }
15114
15115 bool found = false;
15116 if (vars && vars->prev == DVARS_INHERIT && !found) {
15117 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15118 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15119 }
15120 else {
15121 found = (vtable_included(args, arg) &&
15122 !(all && vtable_included(args, all)));
15123 }
15124
15125 if (!found) {
15126 compile_error(p, "no anonymous %s parameter", var);
15127 }
15128 else if (conflict) {
15129 compile_error(p, "anonymous %s parameter is also used within block", var);
15130 }
15131}
15132
15133static NODE *
15134new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15135{
15136 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15137#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15138 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15139#endif
15140 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15141 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15142 block->forwarding = TRUE;
15143#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15144 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15145#endif
15146 return arg_blk_pass(args, block);
15147}
15148
15149static NODE *
15150numparam_push(struct parser_params *p)
15151{
15152 struct local_vars *local = p->lvtbl;
15153 NODE *inner = local->numparam.inner;
15154 if (!local->numparam.outer) {
15155 local->numparam.outer = local->numparam.current;
15156 }
15157 local->numparam.inner = 0;
15158 local->numparam.current = 0;
15159 local->it = 0;
15160 return inner;
15161}
15162
15163static void
15164numparam_pop(struct parser_params *p, NODE *prev_inner)
15165{
15166 struct local_vars *local = p->lvtbl;
15167 if (prev_inner) {
15168 /* prefer first one */
15169 local->numparam.inner = prev_inner;
15170 }
15171 else if (local->numparam.current) {
15172 /* current and inner are exclusive */
15173 local->numparam.inner = local->numparam.current;
15174 }
15175 if (p->max_numparam > NO_PARAM) {
15176 /* current and outer are exclusive */
15177 local->numparam.current = local->numparam.outer;
15178 local->numparam.outer = 0;
15179 }
15180 else {
15181 /* no numbered parameter */
15182 local->numparam.current = 0;
15183 }
15184 local->it = 0;
15185}
15186
15187static const struct vtable *
15188dyna_push(struct parser_params *p)
15189{
15190 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15191 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15192 if (p->lvtbl->used) {
15193 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15194 }
15195 return p->lvtbl->args;
15196}
15197
15198static void
15199dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15200{
15201 struct vtable *tmp = *vtblp;
15202 *vtblp = tmp->prev;
15203# if WARN_PAST_SCOPE
15204 if (p->past_scope_enabled) {
15205 tmp->prev = p->lvtbl->past;
15206 p->lvtbl->past = tmp;
15207 return;
15208 }
15209# endif
15210 vtable_free(tmp);
15211}
15212
15213static void
15214dyna_pop_1(struct parser_params *p)
15215{
15216 struct vtable *tmp;
15217
15218 if ((tmp = p->lvtbl->used) != 0) {
15219 warn_unused_var(p, p->lvtbl);
15220 p->lvtbl->used = p->lvtbl->used->prev;
15221 vtable_free(tmp);
15222 }
15223 dyna_pop_vtable(p, &p->lvtbl->args);
15224 dyna_pop_vtable(p, &p->lvtbl->vars);
15225}
15226
15227static void
15228dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15229{
15230 while (p->lvtbl->args != lvargs) {
15231 dyna_pop_1(p);
15232 if (!p->lvtbl->args) {
15233 struct local_vars *local = p->lvtbl->prev;
15234 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15235 p->lvtbl = local;
15236 }
15237 }
15238 dyna_pop_1(p);
15239}
15240
15241static int
15242dyna_in_block(struct parser_params *p)
15243{
15244 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15245}
15246
15247#ifndef RIPPER
15248int
15249dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15250{
15251 struct vtable *vars, *args, *used;
15252 int i;
15253
15254 args = p->lvtbl->args;
15255 vars = p->lvtbl->vars;
15256 used = p->lvtbl->used;
15257
15258 while (!DVARS_TERMINAL_P(vars)) {
15259 if (vtable_included(args, id)) {
15260 return 1;
15261 }
15262 if ((i = vtable_included(vars, id)) != 0) {
15263 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15264 return 1;
15265 }
15266 args = args->prev;
15267 vars = vars->prev;
15268 if (!vidrefp) used = 0;
15269 if (used) used = used->prev;
15270 }
15271
15272 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15273 return rb_dvar_defined(id, p->parent_iseq);
15274 }
15275
15276 return 0;
15277}
15278#endif
15279
15280static int
15281dvar_defined(struct parser_params *p, ID id)
15282{
15283 return dvar_defined_ref(p, id, NULL);
15284}
15285
15286static int
15287dvar_curr(struct parser_params *p, ID id)
15288{
15289 return (vtable_included(p->lvtbl->args, id) ||
15290 vtable_included(p->lvtbl->vars, id));
15291}
15292
15293static void
15294reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15295{
15296 compile_error(p,
15297 "regexp encoding option '%c' differs from source encoding '%s'",
15298 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15299}
15300
15301#ifndef RIPPER
15302static rb_encoding *
15303find_enc(struct parser_params* p, const char *name)
15304{
15305 int idx = rb_enc_find_index(name);
15306 if (idx < 0) {
15307 rb_bug("unknown encoding name: %s", name);
15308 }
15309
15310 return rb_enc_from_index(idx);
15311}
15312
15313static rb_encoding *
15314kcode_to_enc(struct parser_params* p, int kcode)
15315{
15316 rb_encoding *enc;
15317
15318 switch (kcode) {
15319 case ENC_ASCII8BIT:
15320 enc = rb_ascii8bit_encoding();
15321 break;
15322 case ENC_EUC_JP:
15323 enc = find_enc(p, "EUC-JP");
15324 break;
15325 case ENC_Windows_31J:
15326 enc = find_enc(p, "Windows-31J");
15327 break;
15328 case ENC_UTF8:
15329 enc = rb_utf8_encoding();
15330 break;
15331 default:
15332 enc = NULL;
15333 break;
15334 }
15335
15336 return enc;
15337}
15338
15339int
15340rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15341{
15342 int c = RE_OPTION_ENCODING_IDX(options);
15343
15344 if (c) {
15345 int opt, idx;
15346 rb_encoding *enc;
15347
15348 char_to_option_kcode(c, &opt, &idx);
15349 enc = kcode_to_enc(p, idx);
15350 if (enc != rb_parser_str_get_encoding(str) &&
15351 !rb_parser_is_ascii_string(p, str)) {
15352 goto error;
15353 }
15354 rb_parser_string_set_encoding(str, enc);
15355 }
15356 else if (RE_OPTION_ENCODING_NONE(options)) {
15357 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15358 !rb_parser_is_ascii_string(p, str)) {
15359 c = 'n';
15360 goto error;
15361 }
15362 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15363 }
15364 else if (rb_is_usascii_enc(p->enc)) {
15365 if (!rb_parser_is_ascii_string(p, str)) {
15366 /* raise in re.c */
15367 rb_parser_enc_associate(p, str, rb_usascii_encoding());
15368 }
15369 else {
15370 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15371 }
15372 }
15373 return 0;
15374
15375 error:
15376 return c;
15377}
15378#endif
15379
15380static void
15381reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15382{
15383 int c = rb_reg_fragment_setenc(p, str, options);
15384 if (c) reg_fragment_enc_error(p, str, c);
15385}
15386
15387static void
15388reg_fragment_error(struct parser_params* p, VALUE err)
15389{
15390 compile_error(p, "%"PRIsVALUE, err);
15391}
15392
15393#ifndef RIPPER
15394int
15395rb_parser_reg_fragment_check(struct parser_params* p, rb_parser_string_t *str, int options, rb_parser_reg_fragment_error_func error)
15396{
15397 VALUE err, str2;
15398 reg_fragment_setenc(p, str, options);
15399 /* TODO */
15400 str2 = rb_str_new_parser_string(str);
15401 err = rb_reg_check_preprocess(str2);
15402 if (err != Qnil) {
15403 err = rb_obj_as_string(err);
15404 error(p, err);
15405 return 0;
15406 }
15407 return 1;
15408}
15409#endif
15410
15411#ifndef UNIVERSAL_PARSER
15412typedef struct {
15413 struct parser_params* parser;
15414 rb_encoding *enc;
15415 NODE *succ_block;
15416 const YYLTYPE *loc;
15417 rb_parser_assignable_func assignable;
15418} reg_named_capture_assign_t;
15419
15420static int
15421reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15422 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15423{
15424 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15425 struct parser_params* p = arg->parser;
15426 rb_encoding *enc = arg->enc;
15427 long len = name_end - name;
15428 const char *s = (const char *)name;
15429
15430 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15431}
15432
15433static NODE *
15434reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15435{
15436 reg_named_capture_assign_t arg;
15437
15438 arg.parser = p;
15439 arg.enc = rb_enc_get(regexp);
15440 arg.succ_block = 0;
15441 arg.loc = loc;
15442 arg.assignable = assignable;
15443 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15444
15445 if (!arg.succ_block) return 0;
15446 return RNODE_BLOCK(arg.succ_block)->nd_next;
15447}
15448#endif
15449
15450#ifndef RIPPER
15451NODE *
15452rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15453{
15454 return assignable(p, id, val, loc);
15455}
15456
15457int
15458rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15459 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15460{
15461 ID var;
15462 NODE *node, *succ;
15463
15464 if (!len) return ST_CONTINUE;
15465 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15466 return ST_CONTINUE;
15467
15468 var = intern_cstr(s, len, enc);
15469 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15470 if (!lvar_defined(p, var)) return ST_CONTINUE;
15471 }
15472 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15473 succ = *succ_block;
15474 if (!succ) succ = NEW_ERROR(loc);
15475 succ = block_append(p, succ, node);
15476 *succ_block = succ;
15477 return ST_CONTINUE;
15478}
15479#endif
15480
15481static VALUE
15482parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15483{
15484 VALUE str2;
15485 reg_fragment_setenc(p, str, options);
15486 str2 = rb_str_new_parser_string(str);
15487 return rb_parser_reg_compile(p, str2, options);
15488}
15489
15490#ifndef RIPPER
15491VALUE
15492rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15493{
15494 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15495}
15496#endif
15497
15498static VALUE
15499reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15500{
15501 VALUE re;
15502 VALUE err;
15503
15504 err = rb_errinfo();
15505 re = parser_reg_compile(p, str, options);
15506 if (NIL_P(re)) {
15507 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15508 rb_set_errinfo(err);
15509 reg_fragment_error(p, m);
15510 return Qnil;
15511 }
15512 return re;
15513}
15514
15515#ifndef RIPPER
15516void
15517rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15518{
15519 p->do_print = print;
15520 p->do_loop = loop;
15521 p->do_chomp = chomp;
15522 p->do_split = split;
15523}
15524
15525static NODE *
15526parser_append_options(struct parser_params *p, NODE *node)
15527{
15528 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15529 const YYLTYPE *const LOC = &default_location;
15530
15531 if (p->do_print) {
15532 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15533 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15534 LOC);
15535 node = block_append(p, node, print);
15536 }
15537
15538 if (p->do_loop) {
15539 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15540
15541 if (p->do_split) {
15542 ID ifs = rb_intern("$;");
15543 ID fields = rb_intern("$F");
15544 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15545 NODE *split = NEW_GASGN(fields,
15546 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15547 rb_intern("split"), args, LOC),
15548 LOC);
15549 node = block_append(p, split, node);
15550 }
15551 if (p->do_chomp) {
15552 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15553 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15554 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15555 }
15556
15557 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15558 }
15559
15560 return node;
15561}
15562
15563void
15564rb_init_parse(void)
15565{
15566 /* just to suppress unused-function warnings */
15567 (void)nodetype;
15568 (void)nodeline;
15569}
15570
15571ID
15572internal_id(struct parser_params *p)
15573{
15574 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15575}
15576#endif /* !RIPPER */
15577
15578static void
15579parser_initialize(struct parser_params *p)
15580{
15581 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15582 p->command_start = TRUE;
15583 p->ruby_sourcefile_string = Qnil;
15584 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15585 string_buffer_init(p);
15586 p->node_id = 0;
15587 p->delayed.token = NULL;
15588 p->frozen_string_literal = -1; /* not specified */
15589#ifndef RIPPER
15590 p->error_buffer = Qfalse;
15591 p->end_expect_token_locations = NULL;
15592 p->token_id = 0;
15593 p->tokens = NULL;
15594#else
15595 p->result = Qnil;
15596 p->parsing_thread = Qnil;
15597 p->s_value = Qnil;
15598 p->s_lvalue = Qnil;
15599 p->s_value_stack = rb_ary_new();
15600#endif
15601 p->debug_buffer = Qnil;
15602 p->debug_output = rb_ractor_stdout();
15603 p->enc = rb_utf8_encoding();
15604 p->exits = 0;
15605}
15606
15607#ifdef RIPPER
15608#define rb_ruby_parser_mark ripper_parser_mark
15609#define rb_ruby_parser_free ripper_parser_free
15610#define rb_ruby_parser_memsize ripper_parser_memsize
15611#endif
15612
15613void
15614rb_ruby_parser_mark(void *ptr)
15615{
15616 struct parser_params *p = (struct parser_params*)ptr;
15617
15618 rb_gc_mark(p->ruby_sourcefile_string);
15619#ifndef RIPPER
15620 rb_gc_mark(p->error_buffer);
15621#else
15622 rb_gc_mark(p->value);
15623 rb_gc_mark(p->result);
15624 rb_gc_mark(p->parsing_thread);
15625 rb_gc_mark(p->s_value);
15626 rb_gc_mark(p->s_lvalue);
15627 rb_gc_mark(p->s_value_stack);
15628#endif
15629 rb_gc_mark(p->debug_buffer);
15630 rb_gc_mark(p->debug_output);
15631}
15632
15633void
15634rb_ruby_parser_free(void *ptr)
15635{
15636 struct parser_params *p = (struct parser_params*)ptr;
15637 struct local_vars *local, *prev;
15638
15639 if (p->ast) {
15640 rb_ast_free(p->ast);
15641 }
15642
15643 if (p->warn_duplicate_keys_table) {
15644 st_free_table(p->warn_duplicate_keys_table);
15645 }
15646
15647#ifndef RIPPER
15648 if (p->tokens) {
15649 rb_parser_ary_free(p, p->tokens);
15650 }
15651#endif
15652
15653 if (p->tokenbuf) {
15654 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15655 }
15656
15657 for (local = p->lvtbl; local; local = prev) {
15658 prev = local->prev;
15659 local_free(p, local);
15660 }
15661
15662 {
15663 token_info *ptinfo;
15664 while ((ptinfo = p->token_info) != 0) {
15665 p->token_info = ptinfo->next;
15666 xfree(ptinfo);
15667 }
15668 }
15669 string_buffer_free(p);
15670
15671 if (p->pvtbl) {
15672 st_free_table(p->pvtbl);
15673 }
15674
15675 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15676 st_free_table(p->case_labels);
15677 }
15678
15679 xfree(p->lex.strterm);
15680 p->lex.strterm = 0;
15681
15682 xfree(ptr);
15683}
15684
15685size_t
15686rb_ruby_parser_memsize(const void *ptr)
15687{
15688 struct parser_params *p = (struct parser_params*)ptr;
15689 struct local_vars *local;
15690 size_t size = sizeof(*p);
15691
15692 size += p->toksiz;
15693 for (local = p->lvtbl; local; local = local->prev) {
15694 size += sizeof(*local);
15695 if (local->vars) size += local->vars->capa * sizeof(ID);
15696 }
15697 return size;
15698}
15699
15700#ifndef RIPPER
15701#undef rb_reserved_word
15702
15703const struct kwtable *
15704rb_reserved_word(const char *str, unsigned int len)
15705{
15706 return reserved_word(str, len);
15707}
15708
15709#ifdef UNIVERSAL_PARSER
15710rb_parser_t *
15711rb_ruby_parser_allocate(const rb_parser_config_t *config)
15712{
15713 /* parser_initialize expects fields to be set to 0 */
15714 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15715 p->config = config;
15716 return p;
15717}
15718
15719rb_parser_t *
15720rb_ruby_parser_new(const rb_parser_config_t *config)
15721{
15722 /* parser_initialize expects fields to be set to 0 */
15723 rb_parser_t *p = rb_ruby_parser_allocate(config);
15724 parser_initialize(p);
15725 return p;
15726}
15727#else
15728rb_parser_t *
15729rb_ruby_parser_allocate(void)
15730{
15731 /* parser_initialize expects fields to be set to 0 */
15732 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15733 return p;
15734}
15735
15736rb_parser_t *
15737rb_ruby_parser_new(void)
15738{
15739 /* parser_initialize expects fields to be set to 0 */
15740 rb_parser_t *p = rb_ruby_parser_allocate();
15741 parser_initialize(p);
15742 return p;
15743}
15744#endif
15745
15746rb_parser_t *
15747rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15748{
15749 p->error_buffer = main ? Qfalse : Qnil;
15750 p->parent_iseq = base;
15751 return p;
15752}
15753
15754void
15755rb_ruby_parser_set_script_lines(rb_parser_t *p)
15756{
15757 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15758}
15759
15760void
15761rb_ruby_parser_error_tolerant(rb_parser_t *p)
15762{
15763 p->error_tolerant = 1;
15764}
15765
15766void
15767rb_ruby_parser_keep_tokens(rb_parser_t *p)
15768{
15769 p->keep_tokens = 1;
15770 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15771}
15772
15773rb_encoding *
15774rb_ruby_parser_encoding(rb_parser_t *p)
15775{
15776 return p->enc;
15777}
15778
15779int
15780rb_ruby_parser_end_seen_p(rb_parser_t *p)
15781{
15782 return p->ruby__end__seen;
15783}
15784
15785int
15786rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15787{
15788 p->debug = flag;
15789 return flag;
15790}
15791#endif /* !RIPPER */
15792
15793#ifdef RIPPER
15794int
15795rb_ruby_parser_get_yydebug(rb_parser_t *p)
15796{
15797 return p->debug;
15798}
15799
15800void
15801rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15802{
15803 p->value = value;
15804}
15805
15806int
15807rb_ruby_parser_error_p(rb_parser_t *p)
15808{
15809 return p->error_p;
15810}
15811
15812VALUE
15813rb_ruby_parser_debug_output(rb_parser_t *p)
15814{
15815 return p->debug_output;
15816}
15817
15818void
15819rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15820{
15821 p->debug_output = output;
15822}
15823
15824VALUE
15825rb_ruby_parser_parsing_thread(rb_parser_t *p)
15826{
15827 return p->parsing_thread;
15828}
15829
15830void
15831rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15832{
15833 p->parsing_thread = parsing_thread;
15834}
15835
15836void
15837rb_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)
15838{
15839 p->lex.gets = gets;
15840 p->lex.input = input;
15841 p->eofp = 0;
15842 p->ruby_sourcefile_string = sourcefile_string;
15843 p->ruby_sourcefile = sourcefile;
15844 p->ruby_sourceline = sourceline;
15845}
15846
15847VALUE
15848rb_ruby_parser_result(rb_parser_t *p)
15849{
15850 return p->result;
15851}
15852
15853rb_encoding *
15854rb_ruby_parser_enc(rb_parser_t *p)
15855{
15856 return p->enc;
15857}
15858
15859VALUE
15860rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15861{
15862 return p->ruby_sourcefile_string;
15863}
15864
15865int
15866rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15867{
15868 return p->ruby_sourceline;
15869}
15870
15871int
15872rb_ruby_parser_lex_state(rb_parser_t *p)
15873{
15874 return p->lex.state;
15875}
15876
15877void
15878rb_ruby_ripper_parse0(rb_parser_t *p)
15879{
15880 parser_prepare(p);
15881 p->ast = rb_ast_new();
15882 ripper_yyparse((void*)p);
15883 rb_ast_free(p->ast);
15884 p->ast = 0;
15885 p->eval_tree = 0;
15886 p->eval_tree_begin = 0;
15887}
15888
15889int
15890rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15891{
15892 return dedent_string(p, string, width);
15893}
15894
15895int
15896rb_ruby_ripper_initialized_p(rb_parser_t *p)
15897{
15898 return p->lex.input != 0;
15899}
15900
15901void
15902rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15903{
15904 parser_initialize(p);
15905}
15906
15907long
15908rb_ruby_ripper_column(rb_parser_t *p)
15909{
15910 return p->lex.ptok - p->lex.pbeg;
15911}
15912
15913long
15914rb_ruby_ripper_token_len(rb_parser_t *p)
15915{
15916 return p->lex.pcur - p->lex.ptok;
15917}
15918
15919rb_parser_string_t *
15920rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15921{
15922 return p->lex.lastline;
15923}
15924
15925VALUE
15926rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15927{
15928 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15929}
15930
15931#ifdef UNIVERSAL_PARSER
15932rb_parser_t *
15933rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15934{
15935 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15936 p->config = config;
15937 return p;
15938}
15939#endif
15940
15941struct parser_params*
15942rb_ruby_ripper_parser_allocate(void)
15943{
15944 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15945}
15946#endif /* RIPPER */
15947
15948#ifndef RIPPER
15949void
15950rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15951{
15952 va_list ap;
15953 VALUE mesg = p->debug_buffer;
15954
15955 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15956 va_start(ap, fmt);
15957 rb_str_vcatf(mesg, fmt, ap);
15958 va_end(ap);
15959 if (char_at_end(p, mesg, 0) == '\n') {
15960 rb_io_write(p->debug_output, mesg);
15961 p->debug_buffer = Qnil;
15962 }
15963}
15964
15965static void
15966parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15967{
15968 va_list ap;
15969 int lineno, column;
15970
15971 if (loc) {
15972 lineno = loc->end_pos.lineno;
15973 column = loc->end_pos.column;
15974 }
15975 else {
15976 lineno = p->ruby_sourceline;
15977 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15978 }
15979
15980 rb_io_flush(p->debug_output);
15981 p->error_p = 1;
15982 va_start(ap, fmt);
15983 p->error_buffer =
15984 rb_syntax_error_append(p->error_buffer,
15985 p->ruby_sourcefile_string,
15986 lineno, column,
15987 p->enc, fmt, ap);
15988 va_end(ap);
15989}
15990
15991static size_t
15992count_char(const char *str, int c)
15993{
15994 int n = 0;
15995 while (str[n] == c) ++n;
15996 return n;
15997}
15998
15999/*
16000 * strip enclosing double-quotes, same as the default yytnamerr except
16001 * for that single-quotes matching back-quotes do not stop stripping.
16002 *
16003 * "\"`class' keyword\"" => "`class' keyword"
16004 */
16005size_t
16006rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16007{
16008 if (*yystr == '"') {
16009 size_t yyn = 0, bquote = 0;
16010 const char *yyp = yystr;
16011
16012 while (*++yyp) {
16013 switch (*yyp) {
16014 case '\'':
16015 if (!bquote) {
16016 bquote = count_char(yyp+1, '\'') + 1;
16017 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16018 yyn += bquote;
16019 yyp += bquote - 1;
16020 break;
16021 }
16022 else {
16023 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16024 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16025 yyn += bquote;
16026 yyp += bquote - 1;
16027 bquote = 0;
16028 break;
16029 }
16030 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16031 if (yyres) memcpy(yyres + yyn, yyp, 3);
16032 yyn += 3;
16033 yyp += 2;
16034 break;
16035 }
16036 goto do_not_strip_quotes;
16037 }
16038
16039 case ',':
16040 goto do_not_strip_quotes;
16041
16042 case '\\':
16043 if (*++yyp != '\\')
16044 goto do_not_strip_quotes;
16045 /* Fall through. */
16046 default:
16047 if (yyres)
16048 yyres[yyn] = *yyp;
16049 yyn++;
16050 break;
16051
16052 case '"':
16053 case '\0':
16054 if (yyres)
16055 yyres[yyn] = '\0';
16056 return yyn;
16057 }
16058 }
16059 do_not_strip_quotes: ;
16060 }
16061
16062 if (!yyres) return strlen(yystr);
16063
16064 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16065}
16066#endif
16067
16068#ifdef RIPPER
16069#define validate(x) (void)(x)
16070
16071static VALUE
16072ripper_dispatch0(struct parser_params *p, ID mid)
16073{
16074 return rb_funcall(p->value, mid, 0);
16075}
16076
16077static VALUE
16078ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16079{
16080 validate(a);
16081 return rb_funcall(p->value, mid, 1, a);
16082}
16083
16084static VALUE
16085ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16086{
16087 validate(a);
16088 validate(b);
16089 return rb_funcall(p->value, mid, 2, a, b);
16090}
16091
16092static VALUE
16093ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16094{
16095 validate(a);
16096 validate(b);
16097 validate(c);
16098 return rb_funcall(p->value, mid, 3, a, b, c);
16099}
16100
16101static VALUE
16102ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16103{
16104 validate(a);
16105 validate(b);
16106 validate(c);
16107 validate(d);
16108 return rb_funcall(p->value, mid, 4, a, b, c, d);
16109}
16110
16111static VALUE
16112ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16113{
16114 validate(a);
16115 validate(b);
16116 validate(c);
16117 validate(d);
16118 validate(e);
16119 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16120}
16121
16122static VALUE
16123ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16124{
16125 validate(a);
16126 validate(b);
16127 validate(c);
16128 validate(d);
16129 validate(e);
16130 validate(f);
16131 validate(g);
16132 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16133}
16134
16135void
16136ripper_error(struct parser_params *p)
16137{
16138 p->error_p = TRUE;
16139}
16140
16141VALUE
16142ripper_value(struct parser_params *p)
16143{
16144 (void)yystpcpy; /* may not used in newer bison */
16145
16146 return p->value;
16147}
16148
16149#endif /* RIPPER */
16150/*
16151 * Local variables:
16152 * mode: c
16153 * c-file-style: "ruby"
16154 * End:
16155 */