Ruby 3.4.2p28 (2025-02-15 revision d2930f8e7a5db8a7337fa43370940381b420cc3e)
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 | defn_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
3276 {
3277 endless_method_name(p, $head->nd_mid, &@head);
3278 restore_defun(p, $head);
3279 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3280 ($$ = $head->nd_def)->nd_loc = @$;
3281 RNODE_DEFN($$)->nd_defn = $bodystmt;
3282 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
3283 /*% ripper: def!($:head, $:args, $:$) %*/
3284 local_pop(p);
3285 }
3286 | defs_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
3287 {
3288 endless_method_name(p, $head->nd_mid, &@head);
3289 restore_defun(p, $head);
3290 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3291 ($$ = $head->nd_def)->nd_loc = @$;
3292 RNODE_DEFS($$)->nd_defn = $bodystmt;
3293 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
3294 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
3295 local_pop(p);
3296 }
3297 | backref tOP_ASGN lex_ctxt command_rhs
3298 {
3299 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3300 $$ = NEW_ERROR(&@$);
3301 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3302 }
3303 ;
3304
3305endless_command : command
3306 | endless_command modifier_rescue after_rescue arg
3307 {
3308 p->ctxt.in_rescue = $3.in_rescue;
3309 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3310 /*% ripper: rescue_mod!($:1, $:4) %*/
3311 }
3312 | keyword_not '\n'? endless_command
3313 {
3314 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3315 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3316 }
3317 ;
3318
3319command_rhs : command_call %prec tOP_ASGN
3320 {
3321 value_expr($1);
3322 $$ = $1;
3323 }
3324 | command_call modifier_rescue after_rescue stmt
3325 {
3326 p->ctxt.in_rescue = $3.in_rescue;
3327 YYLTYPE loc = code_loc_gen(&@2, &@4);
3328 value_expr($1);
3329 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3330 /*% ripper: rescue_mod!($:1, $:4) %*/
3331 }
3332 | command_asgn
3333 ;
3334
3335expr : command_call
3336 | expr keyword_and expr
3337 {
3338 $$ = logop(p, idAND, $1, $3, &@2, &@$);
3339 /*% ripper: binary!($:1, ID2VAL(idAND), $:3) %*/
3340 }
3341 | expr keyword_or expr
3342 {
3343 $$ = logop(p, idOR, $1, $3, &@2, &@$);
3344 /*% ripper: binary!($:1, ID2VAL(idOR), $:3) %*/
3345 }
3346 | keyword_not '\n'? expr
3347 {
3348 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3349 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3350 }
3351 | '!' command_call
3352 {
3353 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3354 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
3355 }
3356 | arg tASSOC
3357 {
3358 value_expr($arg);
3359 }
3360 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3361 p_top_expr_body[body]
3362 {
3363 pop_pktbl(p, $p_pktbl);
3364 pop_pvtbl(p, $p_pvtbl);
3365 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3366 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body), &@$, &NULL_LOC, &NULL_LOC);
3367 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3368 }
3369 | arg keyword_in
3370 {
3371 value_expr($arg);
3372 }
3373 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3374 p_top_expr_body[body]
3375 {
3376 pop_pktbl(p, $p_pktbl);
3377 pop_pvtbl(p, $p_pvtbl);
3378 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3379 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body), &@$, &NULL_LOC, &NULL_LOC);
3380 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3381 }
3382 | arg %prec tLBRACE_ARG
3383 ;
3384
3385def_name : fname
3386 {
3387 ID fname = $1;
3388 numparam_name(p, fname);
3389 local_push(p, 0);
3390 p->ctxt.in_def = 1;
3391 p->ctxt.in_rescue = before_rescue;
3392 p->ctxt.cant_return = 0;
3393 $$ = $1;
3394 }
3395 ;
3396
3397defn_head : k_def def_name
3398 {
3399 $$ = def_head_save(p, $k_def);
3400 $$->nd_mid = $def_name;
3401 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3402 /*% ripper: $:def_name %*/
3403 }
3404 ;
3405
3406defs_head : k_def singleton dot_or_colon
3407 {
3408 SET_LEX_STATE(EXPR_FNAME);
3409 }
3410 def_name
3411 {
3412 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3413 $$ = def_head_save(p, $k_def);
3414 $$->nd_mid = $def_name;
3415 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3416 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3417 }
3418 ;
3419
3420expr_value : expr
3421 {
3422 value_expr($1);
3423 $$ = $1;
3424 }
3425 | error
3426 {
3427 $$ = NEW_ERROR(&@$);
3428 }
3429 ;
3430
3431expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3432 {
3433 $$ = $2;
3434 /*% ripper: $:2 %*/
3435 }
3436 ;
3437
3438command_call : command
3439 | block_command
3440 ;
3441
3442block_command : block_call
3443 | block_call call_op2 operation2 command_args
3444 {
3445 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3446 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3447 }
3448 ;
3449
3450cmd_brace_block : tLBRACE_ARG brace_body '}'
3451 {
3452 $$ = $2;
3453 set_embraced_location($$, &@1, &@3);
3454 /*% ripper: $:2 %*/
3455 }
3456 ;
3457
3458fcall : operation
3459 {
3460 $$ = NEW_FCALL($1, 0, &@$);
3461 /*% ripper: $:1 %*/
3462 }
3463 ;
3464
3465command : fcall command_args %prec tLOWEST
3466 {
3467 $1->nd_args = $2;
3468 nd_set_last_loc($1, @2.end_pos);
3469 $$ = (NODE *)$1;
3470 /*% ripper: command!($:1, $:2) %*/
3471 }
3472 | fcall command_args cmd_brace_block
3473 {
3474 block_dup_check(p, $2, $3);
3475 $1->nd_args = $2;
3476 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3477 fixpos($$, RNODE($1));
3478 nd_set_last_loc($1, @2.end_pos);
3479 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3480 }
3481 | primary_value call_op operation2 command_args %prec tLOWEST
3482 {
3483 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3484 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3485 }
3486 | primary_value call_op operation2 command_args cmd_brace_block
3487 {
3488 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3489 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3490 }
3491 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3492 {
3493 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3494 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3495 }
3496 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3497 {
3498 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3499 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3500 }
3501 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3502 {
3503 set_embraced_location($5, &@4, &@6);
3504 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3505 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3506 }
3507 | keyword_super command_args
3508 {
3509 $$ = NEW_SUPER($2, &@$);
3510 fixpos($$, $2);
3511 /*% ripper: super!($:2) %*/
3512 }
3513 | k_yield command_args
3514 {
3515 $$ = new_yield(p, $2, &@$);
3516 fixpos($$, $2);
3517 /*% ripper: yield!($:2) %*/
3518 }
3519 | k_return call_args
3520 {
3521 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3522 /*% ripper: return!($:2) %*/
3523 }
3524 | keyword_break call_args
3525 {
3526 NODE *args = 0;
3527 args = ret_args(p, $2);
3528 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3529 /*% ripper: break!($:2) %*/
3530 }
3531 | keyword_next call_args
3532 {
3533 NODE *args = 0;
3534 args = ret_args(p, $2);
3535 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3536 /*% ripper: next!($:2) %*/
3537 }
3538 ;
3539
3540mlhs : mlhs_basic
3541 | tLPAREN mlhs_inner rparen
3542 {
3543 $$ = $2;
3544 /*% ripper: mlhs_paren!($:2) %*/
3545 }
3546 ;
3547
3548mlhs_inner : mlhs_basic
3549 | tLPAREN mlhs_inner rparen
3550 {
3551 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3552 /*% ripper: mlhs_paren!($:2) %*/
3553 }
3554 ;
3555
3556mlhs_basic : mlhs_head
3557 {
3558 $$ = NEW_MASGN($1, 0, &@$);
3559 /*% ripper: $:1 %*/
3560 }
3561 | mlhs_head mlhs_item
3562 {
3563 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3564 /*% ripper: mlhs_add!($:1, $:2) %*/
3565 }
3566 | mlhs_head tSTAR mlhs_node
3567 {
3568 $$ = NEW_MASGN($1, $3, &@$);
3569 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3570 }
3571 | mlhs_head tSTAR mlhs_node ',' mlhs_post
3572 {
3573 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3574 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3575 }
3576 | mlhs_head tSTAR
3577 {
3578 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3579 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3580 }
3581 | mlhs_head tSTAR ',' mlhs_post
3582 {
3583 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3584 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3585 }
3586 | tSTAR mlhs_node
3587 {
3588 $$ = NEW_MASGN(0, $2, &@$);
3589 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3590 }
3591 | tSTAR mlhs_node ',' mlhs_post
3592 {
3593 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3594 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3595 }
3596 | tSTAR
3597 {
3598 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3599 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3600 }
3601 | tSTAR ',' mlhs_post
3602 {
3603 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3604 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3605 }
3606 ;
3607
3608mlhs_item : mlhs_node
3609 | tLPAREN mlhs_inner rparen
3610 {
3611 $$ = (NODE *)$2;
3612 /*% ripper: mlhs_paren!($:2) %*/
3613 }
3614 ;
3615
3616mlhs_head : mlhs_item ','
3617 {
3618 $$ = NEW_LIST($1, &@1);
3619 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3620 }
3621 | mlhs_head mlhs_item ','
3622 {
3623 $$ = list_append(p, $1, $2);
3624 /*% ripper: mlhs_add!($:1, $:2) %*/
3625 }
3626 ;
3627
3628mlhs_post : mlhs_item
3629 {
3630 $$ = NEW_LIST($1, &@$);
3631 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3632 }
3633 | mlhs_post ',' mlhs_item
3634 {
3635 $$ = list_append(p, $1, $3);
3636 /*% ripper: mlhs_add!($:1, $:3) %*/
3637 }
3638 ;
3639
3640mlhs_node : user_or_keyword_variable
3641 {
3642 /*% ripper: var_field!($:1) %*/
3643 $$ = assignable(p, $1, 0, &@$);
3644 }
3645 | primary_value '[' opt_call_args rbracket
3646 {
3647 $$ = aryset(p, $1, $3, &@$);
3648 /*% ripper: aref_field!($:1, $:3) %*/
3649 }
3650 | primary_value call_op tIDENTIFIER
3651 {
3652 anddot_multiple_assignment_check(p, &@2, $2);
3653 $$ = attrset(p, $1, $2, $3, &@$);
3654 /*% ripper: field!($:1, $:2, $:3) %*/
3655 }
3656 | primary_value tCOLON2 tIDENTIFIER
3657 {
3658 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3659 /*% ripper: const_path_field!($:1, $:3) %*/
3660 }
3661 | primary_value call_op tCONSTANT
3662 {
3663 anddot_multiple_assignment_check(p, &@2, $2);
3664 $$ = attrset(p, $1, $2, $3, &@$);
3665 /*% ripper: field!($:1, $:2, $:3) %*/
3666 }
3667 | primary_value tCOLON2 tCONSTANT
3668 {
3669 /*% ripper: const_path_field!($:1, $:3) %*/
3670 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3671 }
3672 | tCOLON3 tCONSTANT
3673 {
3674 /*% ripper: top_const_field!($:2) %*/
3675 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3676 }
3677 | backref
3678 {
3679 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3680 $$ = NEW_ERROR(&@$);
3681 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3682 }
3683 ;
3684
3685lhs : user_or_keyword_variable
3686 {
3687 /*% ripper: var_field!($:1) %*/
3688 $$ = assignable(p, $1, 0, &@$);
3689 }
3690 | primary_value '[' opt_call_args rbracket
3691 {
3692 $$ = aryset(p, $1, $3, &@$);
3693 /*% ripper: aref_field!($:1, $:3) %*/
3694 }
3695 | primary_value call_op tIDENTIFIER
3696 {
3697 $$ = attrset(p, $1, $2, $3, &@$);
3698 /*% ripper: field!($:1, $:2, $:3) %*/
3699 }
3700 | primary_value tCOLON2 tIDENTIFIER
3701 {
3702 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3703 /*% ripper: field!($:1, $:2, $:3) %*/
3704 }
3705 | primary_value call_op tCONSTANT
3706 {
3707 $$ = attrset(p, $1, $2, $3, &@$);
3708 /*% ripper: field!($:1, $:2, $:3) %*/
3709 }
3710 | primary_value tCOLON2 tCONSTANT
3711 {
3712 /*% ripper: const_path_field!($:1, $:3) %*/
3713 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3714 }
3715 | tCOLON3 tCONSTANT
3716 {
3717 /*% ripper: top_const_field!($:2) %*/
3718 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3719 }
3720 | backref
3721 {
3722 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3723 $$ = NEW_ERROR(&@$);
3724 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3725 }
3726 ;
3727
3728cname : tIDENTIFIER
3729 {
3730 static const char mesg[] = "class/module name must be CONSTANT";
3731 /*%%%*/
3732 yyerror1(&@1, mesg);
3733 /*% %*/
3734 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3735 }
3736 | tCONSTANT
3737 ;
3738
3739cpath : tCOLON3 cname
3740 {
3741 $$ = NEW_COLON3($2, &@$);
3742 /*% ripper: top_const_ref!($:2) %*/
3743 }
3744 | cname
3745 {
3746 $$ = NEW_COLON2(0, $1, &@$);
3747 /*% ripper: const_ref!($:1) %*/
3748 }
3749 | primary_value tCOLON2 cname
3750 {
3751 $$ = NEW_COLON2($1, $3, &@$);
3752 /*% ripper: const_path_ref!($:1, $:3) %*/
3753 }
3754 ;
3755
3756fname : ident_or_const
3757 | tFID
3758 | op
3759 {
3760 SET_LEX_STATE(EXPR_ENDFN);
3761 $$ = $1;
3762 }
3763 | reswords
3764 ;
3765
3766fitem : fname
3767 {
3768 $$ = NEW_SYM(rb_id2str($1), &@$);
3769 /*% ripper: symbol_literal!($:1) %*/
3770 }
3771 | symbol
3772 ;
3773
3774undef_list : fitem
3775 {
3776 $$ = NEW_UNDEF($1, &@$);
3777 /*% ripper: rb_ary_new3(1, $:1) %*/
3778 }
3779 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3780 {
3781 nd_set_last_loc($1, @4.end_pos);
3782 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3783 /*% ripper: rb_ary_push($:1, $:4) %*/
3784 }
3785 ;
3786
3787op : '|' { $$ = '|'; }
3788 | '^' { $$ = '^'; }
3789 | '&' { $$ = '&'; }
3790 | tCMP { $$ = tCMP; }
3791 | tEQ { $$ = tEQ; }
3792 | tEQQ { $$ = tEQQ; }
3793 | tMATCH { $$ = tMATCH; }
3794 | tNMATCH { $$ = tNMATCH; }
3795 | '>' { $$ = '>'; }
3796 | tGEQ { $$ = tGEQ; }
3797 | '<' { $$ = '<'; }
3798 | tLEQ { $$ = tLEQ; }
3799 | tNEQ { $$ = tNEQ; }
3800 | tLSHFT { $$ = tLSHFT; }
3801 | tRSHFT { $$ = tRSHFT; }
3802 | '+' { $$ = '+'; }
3803 | '-' { $$ = '-'; }
3804 | '*' { $$ = '*'; }
3805 | tSTAR { $$ = '*'; }
3806 | '/' { $$ = '/'; }
3807 | '%' { $$ = '%'; }
3808 | tPOW { $$ = tPOW; }
3809 | tDSTAR { $$ = tDSTAR; }
3810 | '!' { $$ = '!'; }
3811 | '~' { $$ = '~'; }
3812 | tUPLUS { $$ = tUPLUS; }
3813 | tUMINUS { $$ = tUMINUS; }
3814 | tAREF { $$ = tAREF; }
3815 | tASET { $$ = tASET; }
3816 | '`' { $$ = '`'; }
3817 ;
3818
3819reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3820 | keyword_BEGIN | keyword_END
3821 | keyword_alias | keyword_and | keyword_begin
3822 | keyword_break | keyword_case | keyword_class | keyword_def
3823 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3824 | keyword_end | keyword_ensure | keyword_false
3825 | keyword_for | keyword_in | keyword_module | keyword_next
3826 | keyword_nil | keyword_not | keyword_or | keyword_redo
3827 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3828 | keyword_super | keyword_then | keyword_true | keyword_undef
3829 | keyword_when | keyword_yield | keyword_if | keyword_unless
3830 | keyword_while | keyword_until
3831 ;
3832
3833arg : lhs '=' lex_ctxt arg_rhs
3834 {
3835 $$ = node_assign(p, $1, $4, $3, &@$);
3836 /*% ripper: assign!($:1, $:4) %*/
3837 }
3838 | var_lhs tOP_ASGN lex_ctxt arg_rhs
3839 {
3840 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
3841 /*% ripper: opassign!($:1, $:2, $:4) %*/
3842 }
3843 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
3844 {
3845 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$, &NULL_LOC, &@2, &@4, &@5);
3846 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3847 }
3848 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3849 {
3850 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$, &@2, &@3, &@4);
3851 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3852 }
3853 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3854 {
3855 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$, &@2, &@3, &@4);
3856 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3857 }
3858 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3859 {
3860 $$ = new_attr_op_assign(p, $1, idCOLON2, $3, $4, $6, &@$, &@2, &@3, &@4);
3861 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3862 }
3863 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3864 {
3865 YYLTYPE loc = code_loc_gen(&@1, &@3);
3866 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
3867 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3868 }
3869 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3870 {
3871 YYLTYPE loc = code_loc_gen(&@1, &@2);
3872 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
3873 /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
3874 }
3875 | backref tOP_ASGN lex_ctxt arg_rhs
3876 {
3877 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3878 $$ = NEW_ERROR(&@$);
3879 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3880 }
3881 | arg tDOT2 arg
3882 {
3883 value_expr($1);
3884 value_expr($3);
3885 $$ = NEW_DOT2($1, $3, &@$);
3886 /*% ripper: dot2!($:1, $:3) %*/
3887 }
3888 | arg tDOT3 arg
3889 {
3890 value_expr($1);
3891 value_expr($3);
3892 $$ = NEW_DOT3($1, $3, &@$);
3893 /*% ripper: dot3!($:1, $:3) %*/
3894 }
3895 | arg tDOT2
3896 {
3897 value_expr($1);
3898 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
3899 /*% ripper: dot2!($:1, Qnil) %*/
3900 }
3901 | arg tDOT3
3902 {
3903 value_expr($1);
3904 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
3905 /*% ripper: dot3!($:1, Qnil) %*/
3906 }
3907 | tBDOT2 arg
3908 {
3909 value_expr($2);
3910 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
3911 /*% ripper: dot2!(Qnil, $:2) %*/
3912 }
3913 | tBDOT3 arg
3914 {
3915 value_expr($2);
3916 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
3917 /*% ripper: dot3!(Qnil, $:2) %*/
3918 }
3919 | arg '+' arg
3920 {
3921 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3922 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3923 }
3924 | arg '-' arg
3925 {
3926 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3927 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3928 }
3929 | arg '*' arg
3930 {
3931 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3932 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3933 }
3934 | arg '/' arg
3935 {
3936 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3937 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3938 }
3939 | arg '%' arg
3940 {
3941 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3942 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3943 }
3944 | arg tPOW arg
3945 {
3946 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3947 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3948 }
3949 | tUMINUS_NUM simple_numeric tPOW arg
3950 {
3951 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3952 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3953 }
3954 | tUPLUS arg
3955 {
3956 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3957 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3958 }
3959 | tUMINUS arg
3960 {
3961 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3962 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3963 }
3964 | arg '|' arg
3965 {
3966 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3967 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3968 }
3969 | arg '^' arg
3970 {
3971 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3972 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3973 }
3974 | arg '&' arg
3975 {
3976 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3977 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
3978 }
3979 | arg tCMP arg
3980 {
3981 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3982 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
3983 }
3984 | rel_expr %prec tCMP
3985 | arg tEQ arg
3986 {
3987 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3988 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
3989 }
3990 | arg tEQQ arg
3991 {
3992 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
3993 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
3994 }
3995 | arg tNEQ arg
3996 {
3997 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
3998 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
3999 }
4000 | arg tMATCH arg
4001 {
4002 $$ = match_op(p, $1, $3, &@2, &@$);
4003 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4004 }
4005 | arg tNMATCH arg
4006 {
4007 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4008 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4009 }
4010 | '!' arg
4011 {
4012 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4013 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4014 }
4015 | '~' arg
4016 {
4017 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4018 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4019 }
4020 | arg tLSHFT arg
4021 {
4022 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4023 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4024 }
4025 | arg tRSHFT arg
4026 {
4027 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4028 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4029 }
4030 | arg tANDOP arg
4031 {
4032 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4033 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4034 }
4035 | arg tOROP arg
4036 {
4037 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4038 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4039 }
4040 | keyword_defined '\n'? begin_defined arg
4041 {
4042 p->ctxt.in_defined = $3.in_defined;
4043 $$ = new_defined(p, $4, &@$);
4044 /*% ripper: defined!($:4) %*/
4045 }
4046 | arg '?' arg '\n'? ':' arg
4047 {
4048 value_expr($1);
4049 $$ = new_if(p, $1, $3, $6, &@$);
4050 fixpos($$, $1);
4051 /*% ripper: ifop!($:1, $:3, $:6) %*/
4052 }
4053 | defn_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
4054 {
4055 endless_method_name(p, $head->nd_mid, &@head);
4056 restore_defun(p, $head);
4057 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4058 ($$ = $head->nd_def)->nd_loc = @$;
4059 RNODE_DEFN($$)->nd_defn = $bodystmt;
4060 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
4061 /*% ripper: def!($:head, $:args, $:$) %*/
4062 local_pop(p);
4063 }
4064 | defs_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
4065 {
4066 endless_method_name(p, $head->nd_mid, &@head);
4067 restore_defun(p, $head);
4068 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4069 ($$ = $head->nd_def)->nd_loc = @$;
4070 RNODE_DEFS($$)->nd_defn = $bodystmt;
4071 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
4072 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
4073 local_pop(p);
4074 }
4075 | primary
4076 {
4077 $$ = $1;
4078 }
4079 ;
4080
4081endless_arg : arg %prec modifier_rescue
4082 | endless_arg modifier_rescue after_rescue arg
4083 {
4084 p->ctxt.in_rescue = $3.in_rescue;
4085 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4086 /*% ripper: rescue_mod!($:1, $:4) %*/
4087 }
4088 | keyword_not '\n'? endless_arg
4089 {
4090 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4091 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4092 }
4093 ;
4094
4095relop : '>' {$$ = '>';}
4096 | '<' {$$ = '<';}
4097 | tGEQ {$$ = idGE;}
4098 | tLEQ {$$ = idLE;}
4099 ;
4100
4101rel_expr : arg relop arg %prec '>'
4102 {
4103 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4104 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4105 }
4106 | rel_expr relop arg %prec '>'
4107 {
4108 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4109 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4110 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4111 }
4112 ;
4113
4114lex_ctxt : none
4115 {
4116 $$ = p->ctxt;
4117 }
4118 ;
4119
4120begin_defined : lex_ctxt
4121 {
4122 p->ctxt.in_defined = 1;
4123 $$ = $1;
4124 }
4125 ;
4126
4127after_rescue : lex_ctxt
4128 {
4129 p->ctxt.in_rescue = after_rescue;
4130 $$ = $1;
4131 }
4132 ;
4133
4134arg_value : arg
4135 {
4136 value_expr($1);
4137 $$ = $1;
4138 }
4139 ;
4140
4141aref_args : none
4142 | args trailer
4143 {
4144 $$ = $1;
4145 }
4146 | args ',' assocs trailer
4147 {
4148 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4149 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4150 }
4151 | assocs trailer
4152 {
4153 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4154 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4155 }
4156 ;
4157
4158arg_rhs : arg %prec tOP_ASGN
4159 {
4160 value_expr($1);
4161 $$ = $1;
4162 }
4163 | arg modifier_rescue after_rescue arg
4164 {
4165 p->ctxt.in_rescue = $3.in_rescue;
4166 value_expr($1);
4167 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4168 /*% ripper: rescue_mod!($:1, $:4) %*/
4169 }
4170 ;
4171
4172paren_args : '(' opt_call_args rparen
4173 {
4174 $$ = $2;
4175 /*% ripper: arg_paren!($:2) %*/
4176 }
4177 | '(' args ',' args_forward rparen
4178 {
4179 if (!check_forwarding_args(p)) {
4180 $$ = 0;
4181 }
4182 else {
4183 $$ = new_args_forward_call(p, $2, &@4, &@$);
4184 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4185 }
4186 }
4187 | '(' args_forward rparen
4188 {
4189 if (!check_forwarding_args(p)) {
4190 $$ = 0;
4191 }
4192 else {
4193 $$ = new_args_forward_call(p, 0, &@2, &@$);
4194 /*% ripper: arg_paren!($:2) %*/
4195 }
4196 }
4197 ;
4198
4199opt_paren_args : none
4200 | paren_args
4201 {
4202 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4203 }
4204 ;
4205
4206opt_call_args : none
4207 | call_args
4208 | args ','
4209 {
4210 $$ = $1;
4211 }
4212 | args ',' assocs ','
4213 {
4214 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4215 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4216 }
4217 | assocs ','
4218 {
4219 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4220 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4221 }
4222 ;
4223
4224call_args : command
4225 {
4226 value_expr($1);
4227 $$ = NEW_LIST($1, &@$);
4228 /*% ripper: args_add!(args_new!, $:1) %*/
4229 }
4230 | args opt_block_arg
4231 {
4232 $$ = arg_blk_pass($1, $2);
4233 /*% ripper: args_add_block!($:1, $:2) %*/
4234 }
4235 | assocs opt_block_arg
4236 {
4237 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4238 $$ = arg_blk_pass($$, $2);
4239 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4240 }
4241 | args ',' assocs opt_block_arg
4242 {
4243 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4244 $$ = arg_blk_pass($$, $4);
4245 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4246 }
4247 | block_arg
4248 /*% ripper: args_add_block!(args_new!, $:1) %*/
4249 ;
4250
4251command_args : {
4252 /* If call_args starts with a open paren '(' or '[',
4253 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4254 * but the push must be done after CMDARG_PUSH(1).
4255 * So this code makes them consistent by first cancelling
4256 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4257 * and finally redoing CMDARG_PUSH(0).
4258 */
4259 int lookahead = 0;
4260 switch (yychar) {
4261 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4262 lookahead = 1;
4263 }
4264 if (lookahead) CMDARG_POP();
4265 CMDARG_PUSH(1);
4266 if (lookahead) CMDARG_PUSH(0);
4267 }
4268 call_args
4269 {
4270 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4271 * but the push must be done after CMDARG_POP() in the parser.
4272 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4273 * CMDARG_POP() to pop 1 pushed by command_args,
4274 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4275 */
4276 int lookahead = 0;
4277 switch (yychar) {
4278 case tLBRACE_ARG:
4279 lookahead = 1;
4280 }
4281 if (lookahead) CMDARG_POP();
4282 CMDARG_POP();
4283 if (lookahead) CMDARG_PUSH(0);
4284 $$ = $2;
4285 /*% ripper: $:2 %*/
4286 }
4287 ;
4288
4289block_arg : tAMPER arg_value
4290 {
4291 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4292 /*% ripper: $:2 %*/
4293 }
4294 | tAMPER
4295 {
4296 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4297 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4298 /*% ripper: Qnil %*/
4299 }
4300 ;
4301
4302opt_block_arg : ',' block_arg
4303 {
4304 $$ = $2;
4305 /*% ripper: $:2 %*/
4306 }
4307 | none
4308 {
4309 $$ = 0;
4310 /*% ripper: Qfalse %*/
4311 }
4312 ;
4313
4314/* value */
4315args : arg_value
4316 {
4317 $$ = NEW_LIST($arg_value, &@$);
4318 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4319 }
4320 | arg_splat
4321 {
4322 $$ = $arg_splat;
4323 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4324 }
4325 | args[non_last_args] ',' arg_value
4326 {
4327 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4328 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4329 }
4330 | args[non_last_args] ',' arg_splat
4331 {
4332 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4333 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4334 }
4335 ;
4336
4337/* value */
4338arg_splat : tSTAR arg_value
4339 {
4340 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4341 /*% ripper: $:arg_value %*/
4342 }
4343 | tSTAR /* none */
4344 {
4345 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4346 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4347 /*% ripper: Qnil %*/
4348 }
4349 ;
4350
4351/* value */
4352mrhs_arg : mrhs
4353 | arg_value
4354 ;
4355
4356/* value */
4357mrhs : args ',' arg_value
4358 {
4359 $$ = last_arg_append(p, $args, $arg_value, &@$);
4360 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4361 }
4362 | args ',' tSTAR arg_value
4363 {
4364 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4365 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4366 }
4367 | tSTAR arg_value
4368 {
4369 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4370 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4371 }
4372 ;
4373
4374%rule %inline inline_primary
4375 : literal
4376 | strings
4377 | xstring
4378 | regexp
4379 | words
4380 | qwords
4381 | symbols
4382 | qsymbols
4383 ;
4384
4385primary : inline_primary
4386 | var_ref
4387 | backref
4388 | tFID
4389 {
4390 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
4391 /*% ripper: method_add_arg!(fcall!($:1), args_new!) %*/
4392 }
4393 | k_begin
4394 {
4395 CMDARG_PUSH(0);
4396 }
4397 bodystmt
4398 k_end
4399 {
4400 CMDARG_POP();
4401 set_line_body($3, @1.end_pos.lineno);
4402 $$ = NEW_BEGIN($3, &@$);
4403 nd_set_line($$, @1.end_pos.lineno);
4404 /*% ripper: begin!($:3) %*/
4405 }
4406 | tLPAREN_ARG compstmt {SET_LEX_STATE(EXPR_ENDARG);} ')'
4407 {
4408 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4409 $$ = $2;
4410 /*% ripper: paren!($:2) %*/
4411 }
4412 | tLPAREN compstmt ')'
4413 {
4414 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4415 $$ = NEW_BLOCK($2, &@$);
4416 /*% ripper: paren!($:2) %*/
4417 }
4418 | primary_value tCOLON2 tCONSTANT
4419 {
4420 $$ = NEW_COLON2($1, $3, &@$);
4421 /*% ripper: const_path_ref!($:1, $:3) %*/
4422 }
4423 | tCOLON3 tCONSTANT
4424 {
4425 $$ = NEW_COLON3($2, &@$);
4426 /*% ripper: top_const_ref!($:2) %*/
4427 }
4428 | tLBRACK aref_args ']'
4429 {
4430 $$ = make_list($2, &@$);
4431 /*% ripper: array!($:2) %*/
4432 }
4433 | tLBRACE assoc_list '}'
4434 {
4435 $$ = new_hash(p, $2, &@$);
4436 RNODE_HASH($$)->nd_brace = TRUE;
4437 /*% ripper: hash!($:2) %*/
4438 }
4439 | k_return
4440 {
4441 $$ = NEW_RETURN(0, &@$, &@1);
4442 /*% ripper: return0! %*/
4443 }
4444 | k_yield '(' call_args rparen
4445 {
4446 $$ = new_yield(p, $3, &@$);
4447 /*% ripper: yield!(paren!($:3)) %*/
4448 }
4449 | k_yield '(' rparen
4450 {
4451 $$ = NEW_YIELD(0, &@$);
4452 /*% ripper: yield!(paren!(args_new!)) %*/
4453 }
4454 | k_yield
4455 {
4456 $$ = NEW_YIELD(0, &@$);
4457 /*% ripper: yield0! %*/
4458 }
4459 | keyword_defined '\n'? '(' begin_defined expr rparen
4460 {
4461 p->ctxt.in_defined = $4.in_defined;
4462 $$ = new_defined(p, $5, &@$);
4463 /*% ripper: defined!($:5) %*/
4464 }
4465 | keyword_not '(' expr rparen
4466 {
4467 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4468 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4469 }
4470 | keyword_not '(' rparen
4471 {
4472 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
4473 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4474 }
4475 | fcall brace_block
4476 {
4477 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
4478 /*% ripper: method_add_block!(method_add_arg!(fcall!($:1), args_new!), $:2) %*/
4479 }
4480 | method_call
4481 | method_call brace_block
4482 {
4483 block_dup_check(p, get_nd_args(p, $1), $2);
4484 $$ = method_add_block(p, $1, $2, &@$);
4485 /*% ripper: method_add_block!($:1, $:2) %*/
4486 }
4487 | lambda
4488 | k_if expr_value then
4489 compstmt
4490 if_tail
4491 k_end
4492 {
4493 $$ = new_if(p, $2, $4, $5, &@$);
4494 fixpos($$, $2);
4495 /*% ripper: if!($:2, $:4, $:5) %*/
4496 }
4497 | k_unless expr_value then
4498 compstmt
4499 opt_else
4500 k_end
4501 {
4502 $$ = new_unless(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4503 fixpos($$, $2);
4504 /*% ripper: unless!($:2, $:4, $:5) %*/
4505 }
4506 | k_while expr_value_do
4507 compstmt
4508 k_end
4509 {
4510 restore_block_exit(p, $1);
4511 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4512 fixpos($$, $2);
4513 /*% ripper: while!($:2, $:3) %*/
4514 }
4515 | k_until expr_value_do
4516 compstmt
4517 k_end
4518 {
4519 restore_block_exit(p, $1);
4520 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4521 fixpos($$, $2);
4522 /*% ripper: until!($:2, $:3) %*/
4523 }
4524 | k_case expr_value terms?
4525 {
4526 $$ = p->case_labels;
4527 p->case_labels = CHECK_LITERAL_WHEN;
4528 }<labels>
4529 case_body
4530 k_end
4531 {
4532 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4533 p->case_labels = $4;
4534 $$ = NEW_CASE($2, $5, &@$, &@1, &@6);
4535 fixpos($$, $2);
4536 /*% ripper: case!($:2, $:5) %*/
4537 }
4538 | k_case terms?
4539 {
4540 $$ = p->case_labels;
4541 p->case_labels = 0;
4542 }<labels>
4543 case_body
4544 k_end
4545 {
4546 if (p->case_labels) st_free_table(p->case_labels);
4547 p->case_labels = $3;
4548 $$ = NEW_CASE2($4, &@$, &@1, &@5);
4549 /*% ripper: case!(Qnil, $:4) %*/
4550 }
4551 | k_case expr_value terms?
4552 p_case_body
4553 k_end
4554 {
4555 $$ = NEW_CASE3($2, $4, &@$, &@1, &@5);
4556 /*% ripper: case!($:2, $:4) %*/
4557 }
4558 | k_for for_var keyword_in expr_value_do
4559 compstmt
4560 k_end
4561 {
4562 restore_block_exit(p, $1);
4563 /*
4564 * for a, b, c in e
4565 * #=>
4566 * e.each{|*x| a, b, c = x}
4567 *
4568 * for a in e
4569 * #=>
4570 * e.each{|x| a, = x}
4571 */
4572 ID id = internal_id(p);
4573 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4574 rb_node_args_t *args;
4575 NODE *scope, *internal_var = NEW_DVAR(id, &@2);
4576 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4577 tbl->ids[0] = id; /* internal id */
4578
4579 switch (nd_type($2)) {
4580 case NODE_LASGN:
4581 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4582 set_nd_value(p, $2, internal_var);
4583 id = 0;
4584 m->nd_plen = 1;
4585 m->nd_next = $2;
4586 break;
4587 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4588 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
4589 break;
4590 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4591 m->nd_next = node_assign(p, (NODE *)NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
4592 }
4593 /* {|*internal_id| <m> = internal_id; ... } */
4594 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
4595 scope = NEW_SCOPE2(tbl, args, $5, &@$);
4596 $$ = NEW_FOR($4, scope, &@$);
4597 fixpos($$, $2);
4598 /*% ripper: for!($:2, $:4, $:5) %*/
4599 }
4600 | k_class cpath superclass
4601 {
4602 begin_definition("class", &@k_class, &@cpath);
4603 }
4604 bodystmt
4605 k_end
4606 {
4607 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$);
4608 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4609 set_line_body($bodystmt, @superclass.end_pos.lineno);
4610 nd_set_line($$, @superclass.end_pos.lineno);
4611 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4612 local_pop(p);
4613 p->ctxt.in_class = $k_class.in_class;
4614 p->ctxt.cant_return = $k_class.cant_return;
4615 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4616 }
4617 | k_class tLSHFT expr_value
4618 {
4619 begin_definition("", &@k_class, &@tLSHFT);
4620 }
4621 term
4622 bodystmt
4623 k_end
4624 {
4625 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$);
4626 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4627 set_line_body($bodystmt, nd_line($expr_value));
4628 fixpos($$, $expr_value);
4629 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4630 local_pop(p);
4631 p->ctxt.in_def = $k_class.in_def;
4632 p->ctxt.in_class = $k_class.in_class;
4633 p->ctxt.cant_return = $k_class.cant_return;
4634 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4635 }
4636 | k_module cpath
4637 {
4638 begin_definition("module", &@k_module, &@cpath);
4639 }
4640 bodystmt
4641 k_end
4642 {
4643 $$ = NEW_MODULE($cpath, $bodystmt, &@$);
4644 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4645 set_line_body($bodystmt, @cpath.end_pos.lineno);
4646 nd_set_line($$, @cpath.end_pos.lineno);
4647 /*% ripper: module!($:cpath, $:bodystmt) %*/
4648 local_pop(p);
4649 p->ctxt.in_class = $k_module.in_class;
4650 p->ctxt.cant_return = $k_module.cant_return;
4651 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4652 }
4653 | defn_head[head]
4654 f_arglist[args]
4655 {
4656 push_end_expect_token_locations(p, &@head.beg_pos);
4657 }
4658 bodystmt
4659 k_end
4660 {
4661 restore_defun(p, $head);
4662 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4663 ($$ = $head->nd_def)->nd_loc = @$;
4664 RNODE_DEFN($$)->nd_defn = $bodystmt;
4665 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4666 local_pop(p);
4667 }
4668 | defs_head[head]
4669 f_arglist[args]
4670 {
4671 push_end_expect_token_locations(p, &@head.beg_pos);
4672 }
4673 bodystmt
4674 k_end
4675 {
4676 restore_defun(p, $head);
4677 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4678 ($$ = $head->nd_def)->nd_loc = @$;
4679 RNODE_DEFS($$)->nd_defn = $bodystmt;
4680 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4681 local_pop(p);
4682 }
4683 | keyword_break
4684 {
4685 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@1));
4686 /*% ripper: break!(args_new!) %*/
4687 }
4688 | keyword_next
4689 {
4690 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@1));
4691 /*% ripper: next!(args_new!) %*/
4692 }
4693 | keyword_redo
4694 {
4695 $$ = add_block_exit(p, NEW_REDO(&@$, &@1));
4696 /*% ripper: redo! %*/
4697 }
4698 | keyword_retry
4699 {
4700 if (!p->ctxt.in_defined) {
4701 switch (p->ctxt.in_rescue) {
4702 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4703 case after_rescue: /* ok */ break;
4704 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4705 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4706 }
4707 }
4708 $$ = NEW_RETRY(&@$);
4709 /*% ripper: retry! %*/
4710 }
4711 ;
4712
4713primary_value : primary
4714 {
4715 value_expr($1);
4716 $$ = $1;
4717 }
4718 ;
4719
4720k_begin : keyword_begin
4721 {
4722 token_info_push(p, "begin", &@$);
4723 push_end_expect_token_locations(p, &@1.beg_pos);
4724 }
4725 ;
4726
4727k_if : keyword_if
4728 {
4729 WARN_EOL("if");
4730 token_info_push(p, "if", &@$);
4731 if (p->token_info && p->token_info->nonspc &&
4732 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4733 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4734 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4735 beg += rb_strlen_lit("else");
4736 while (beg < tok && ISSPACE(*beg)) beg++;
4737 if (beg == tok) {
4738 p->token_info->nonspc = 0;
4739 }
4740 }
4741 push_end_expect_token_locations(p, &@1.beg_pos);
4742 }
4743 ;
4744
4745k_unless : keyword_unless
4746 {
4747 token_info_push(p, "unless", &@$);
4748 push_end_expect_token_locations(p, &@1.beg_pos);
4749 }
4750 ;
4751
4752k_while : keyword_while allow_exits
4753 {
4754 $$ = $allow_exits;
4755 token_info_push(p, "while", &@$);
4756 push_end_expect_token_locations(p, &@1.beg_pos);
4757 }
4758 ;
4759
4760k_until : keyword_until allow_exits
4761 {
4762 $$ = $allow_exits;
4763 token_info_push(p, "until", &@$);
4764 push_end_expect_token_locations(p, &@1.beg_pos);
4765 }
4766 ;
4767
4768k_case : keyword_case
4769 {
4770 token_info_push(p, "case", &@$);
4771 push_end_expect_token_locations(p, &@1.beg_pos);
4772 }
4773 ;
4774
4775k_for : keyword_for allow_exits
4776 {
4777 $$ = $allow_exits;
4778 token_info_push(p, "for", &@$);
4779 push_end_expect_token_locations(p, &@1.beg_pos);
4780 }
4781 ;
4782
4783k_class : keyword_class
4784 {
4785 token_info_push(p, "class", &@$);
4786 $$ = p->ctxt;
4787 p->ctxt.in_rescue = before_rescue;
4788 push_end_expect_token_locations(p, &@1.beg_pos);
4789 }
4790 ;
4791
4792k_module : keyword_module
4793 {
4794 token_info_push(p, "module", &@$);
4795 $$ = p->ctxt;
4796 p->ctxt.in_rescue = before_rescue;
4797 push_end_expect_token_locations(p, &@1.beg_pos);
4798 }
4799 ;
4800
4801k_def : keyword_def
4802 {
4803 token_info_push(p, "def", &@$);
4804 $$ = NEW_DEF_TEMP(&@$);
4805 p->ctxt.in_argdef = 1;
4806 }
4807 ;
4808
4809k_do : keyword_do
4810 {
4811 token_info_push(p, "do", &@$);
4812 push_end_expect_token_locations(p, &@1.beg_pos);
4813 }
4814 ;
4815
4816k_do_block : keyword_do_block
4817 {
4818 token_info_push(p, "do", &@$);
4819 push_end_expect_token_locations(p, &@1.beg_pos);
4820 }
4821 ;
4822
4823k_rescue : keyword_rescue
4824 {
4825 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4826 $$ = p->ctxt;
4827 p->ctxt.in_rescue = after_rescue;
4828 }
4829 ;
4830
4831k_ensure : keyword_ensure
4832 {
4833 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4834 $$ = p->ctxt;
4835 }
4836 ;
4837
4838k_when : keyword_when
4839 {
4840 token_info_warn(p, "when", p->token_info, 0, &@$);
4841 }
4842 ;
4843
4844k_else : keyword_else
4845 {
4846 token_info *ptinfo_beg = p->token_info;
4847 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4848 token_info_warn(p, "else", p->token_info, same, &@$);
4849 if (same) {
4850 token_info e;
4851 e.next = ptinfo_beg->next;
4852 e.token = "else";
4853 token_info_setup(&e, p->lex.pbeg, &@$);
4854 if (!e.nonspc) *ptinfo_beg = e;
4855 }
4856 }
4857 ;
4858
4859k_elsif : keyword_elsif
4860 {
4861 WARN_EOL("elsif");
4862 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4863 }
4864 ;
4865
4866k_end : keyword_end
4867 {
4868 token_info_pop(p, "end", &@$);
4869 pop_end_expect_token_locations(p);
4870 }
4871 | tDUMNY_END
4872 {
4873 compile_error(p, "syntax error, unexpected end-of-input");
4874 }
4875 ;
4876
4877k_return : keyword_return
4878 {
4879 if (p->ctxt.cant_return && !dyna_in_block(p))
4880 yyerror1(&@1, "Invalid return in class/module body");
4881 }
4882 ;
4883
4884k_yield : keyword_yield
4885 {
4886 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4887 yyerror1(&@1, "Invalid yield");
4888 }
4889 ;
4890
4891then : term
4892 | keyword_then
4893 | term keyword_then
4894 ;
4895
4896do : term
4897 | keyword_do_cond
4898 ;
4899
4900if_tail : opt_else
4901 | k_elsif expr_value then
4902 compstmt
4903 if_tail
4904 {
4905 $$ = new_if(p, $2, $4, $5, &@$);
4906 fixpos($$, $2);
4907 /*% ripper: elsif!($:2, $:4, $:5) %*/
4908 }
4909 ;
4910
4911opt_else : none
4912 | k_else compstmt
4913 {
4914 $$ = $2;
4915 /*% ripper: else!($:2) %*/
4916 }
4917 ;
4918
4919for_var : lhs
4920 | mlhs
4921 ;
4922
4923f_marg : f_norm_arg
4924 {
4925 $$ = assignable(p, $1, 0, &@$);
4926 mark_lvar_used(p, $$);
4927 }
4928 | tLPAREN f_margs rparen
4929 {
4930 $$ = (NODE *)$2;
4931 /*% ripper: mlhs_paren!($:2) %*/
4932 }
4933 ;
4934
4935f_marg_list : f_marg
4936 {
4937 $$ = NEW_LIST($1, &@$);
4938 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
4939 }
4940 | f_marg_list ',' f_marg
4941 {
4942 $$ = list_append(p, $1, $3);
4943 /*% ripper: mlhs_add!($:1, $:3) %*/
4944 }
4945 ;
4946
4947f_margs : f_marg_list
4948 {
4949 $$ = NEW_MASGN($1, 0, &@$);
4950 /*% ripper: $:1 %*/
4951 }
4952 | f_marg_list ',' f_rest_marg
4953 {
4954 $$ = NEW_MASGN($1, $3, &@$);
4955 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4956 }
4957 | f_marg_list ',' f_rest_marg ',' f_marg_list
4958 {
4959 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4960 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4961 }
4962 | f_rest_marg
4963 {
4964 $$ = NEW_MASGN(0, $1, &@$);
4965 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4966 }
4967 | f_rest_marg ',' f_marg_list
4968 {
4969 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4970 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4971 }
4972 ;
4973
4974f_rest_marg : tSTAR f_norm_arg
4975 {
4976 /*% ripper: $:2 %*/
4977 $$ = assignable(p, $2, 0, &@$);
4978 mark_lvar_used(p, $$);
4979 }
4980 | tSTAR
4981 {
4982 $$ = NODE_SPECIAL_NO_NAME_REST;
4983 /*% ripper: Qnil %*/
4984 }
4985 ;
4986
4987f_any_kwrest : f_kwrest
4988 | f_no_kwarg
4989 {
4990 $$ = idNil;
4991 /*% ripper: ID2VAL(idNil) %*/
4992 }
4993 ;
4994
4995f_eq : {p->ctxt.in_argdef = 0;} '=';
4996
4997block_args_tail : f_kwarg(f_block_kw) ',' f_kwrest opt_f_block_arg
4998 {
4999 $$ = new_args_tail(p, $1, $3, $4, &@3);
5000 /*% ripper: [$:1, $:3, $:4] %*/
5001 }
5002 | f_kwarg(f_block_kw) opt_f_block_arg
5003 {
5004 $$ = new_args_tail(p, $1, 0, $2, &@1);
5005 /*% ripper: [$:1, Qnil, $:2] %*/
5006 }
5007 | f_any_kwrest opt_f_block_arg
5008 {
5009 $$ = new_args_tail(p, 0, $1, $2, &@1);
5010 /*% ripper: [Qnil, $:1, $:2] %*/
5011 }
5012 | f_block_arg
5013 {
5014 $$ = new_args_tail(p, 0, 0, $1, &@1);
5015 /*% ripper: [Qnil, Qnil, $:1] %*/
5016 }
5017 ;
5018
5019excessed_comma : ','
5020 {
5021 /* magic number for rest_id in iseq_set_arguments() */
5022 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
5023 /*% ripper: excessed_comma! %*/
5024 }
5025 ;
5026
5027block_param : f_arg ',' f_optarg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5028 {
5029 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
5030 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
5031 }
5032 | f_arg ',' f_optarg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5033 {
5034 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
5035 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
5036 }
5037 | f_arg ',' f_optarg(primary_value) opt_args_tail(block_args_tail)
5038 {
5039 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
5040 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
5041 }
5042 | f_arg ',' f_optarg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5043 {
5044 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
5045 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
5046 }
5047 | f_arg ',' f_rest_arg opt_args_tail(block_args_tail)
5048 {
5049 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
5050 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
5051 }
5052 | f_arg excessed_comma
5053 {
5054 $$ = new_args_tail(p, 0, 0, 0, &@2);
5055 $$ = new_args(p, $1, 0, $2, 0, $$, &@$);
5056 /*% ripper: params!($:1, Qnil, $:2, Qnil, Qnil, Qnil, Qnil) %*/
5057 }
5058 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5059 {
5060 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
5061 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
5062 }
5063 | f_arg opt_args_tail(block_args_tail)
5064 {
5065 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
5066 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
5067 }
5068 | f_optarg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5069 {
5070 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
5071 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
5072 }
5073 | f_optarg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5074 {
5075 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
5076 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
5077 }
5078 | f_optarg(primary_value) opt_args_tail(block_args_tail)
5079 {
5080 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
5081 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
5082 }
5083 | f_optarg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5084 {
5085 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
5086 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
5087 }
5088 | f_rest_arg opt_args_tail(block_args_tail)
5089 {
5090 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
5091 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
5092 }
5093 | f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5094 {
5095 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
5096 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
5097 }
5098 | block_args_tail
5099 {
5100 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
5101 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
5102 }
5103 ;
5104
5105opt_block_param : none
5106 | block_param_def
5107 {
5108 p->command_start = TRUE;
5109 }
5110 ;
5111
5112block_param_def : '|' opt_bv_decl '|'
5113 {
5114 p->max_numparam = ORDINAL_PARAM;
5115 p->ctxt.in_argdef = 0;
5116 $$ = 0;
5117 /*% ripper: block_var!(params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), $:2) %*/
5118 }
5119 | '|' block_param opt_bv_decl '|'
5120 {
5121 p->max_numparam = ORDINAL_PARAM;
5122 p->ctxt.in_argdef = 0;
5123 $$ = $2;
5124 /*% ripper: block_var!($:2, $:3) %*/
5125 }
5126 ;
5127
5128
5129opt_bv_decl : '\n'?
5130 {
5131 $$ = 0;
5132 /*% ripper: Qfalse %*/
5133 }
5134 | '\n'? ';' bv_decls '\n'?
5135 {
5136 $$ = 0;
5137 /*% ripper: $:3 %*/
5138 }
5139 ;
5140
5141bv_decls : bvar
5142 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5143 | bv_decls ',' bvar
5144 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5145 ;
5146
5147bvar : tIDENTIFIER
5148 {
5149 new_bv(p, $1);
5150 /*% ripper: $:1 %*/
5151 }
5152 | f_bad_arg
5153 {
5154 $$ = 0;
5155 }
5156 ;
5157
5158max_numparam : {
5159 $$ = p->max_numparam;
5160 p->max_numparam = 0;
5161 }
5162 ;
5163
5164numparam : {
5165 $$ = numparam_push(p);
5166 }
5167 ;
5168
5169it_id : {
5170 $$ = p->it_id;
5171 p->it_id = 0;
5172 }
5173 ;
5174
5175lambda : tLAMBDA[lpar]
5176 {
5177 token_info_push(p, "->", &@1);
5178 $$ = dyna_push(p);
5179 }[dyna]<vars>
5180 max_numparam numparam it_id allow_exits
5181 f_larglist[args]
5182 {
5183 CMDARG_PUSH(0);
5184 }
5185 lambda_body[body]
5186 {
5187 int max_numparam = p->max_numparam;
5188 ID it_id = p->it_id;
5189 p->lex.lpar_beg = $lpar;
5190 p->max_numparam = $max_numparam;
5191 p->it_id = $it_id;
5192 restore_block_exit(p, $allow_exits);
5193 CMDARG_POP();
5194 $args = args_with_numbered(p, $args, max_numparam, it_id);
5195 {
5196 YYLTYPE loc = code_loc_gen(&@args, &@body);
5197 $$ = NEW_LAMBDA($args, $body, &loc);
5198 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5199 nd_set_line($$, @args.end_pos.lineno);
5200 nd_set_first_loc($$, @1.beg_pos);
5201 }
5202 /*% ripper: lambda!($:args, $:body) %*/
5203 numparam_pop(p, $numparam);
5204 dyna_pop(p, $dyna);
5205 }
5206 ;
5207
5208f_larglist : '(' f_args opt_bv_decl ')'
5209 {
5210 p->ctxt.in_argdef = 0;
5211 $$ = $f_args;
5212 p->max_numparam = ORDINAL_PARAM;
5213 /*% ripper: paren!($:2) %*/
5214 }
5215 | f_args
5216 {
5217 p->ctxt.in_argdef = 0;
5218 if (!args_info_empty_p(&$1->nd_ainfo))
5219 p->max_numparam = ORDINAL_PARAM;
5220 $$ = $f_args;
5221 }
5222 ;
5223
5224lambda_body : tLAMBEG compstmt '}'
5225 {
5226 token_info_pop(p, "}", &@3);
5227 $$ = $2;
5228 /*% ripper: $:2 %*/
5229 }
5230 | keyword_do_LAMBDA
5231 {
5232 push_end_expect_token_locations(p, &@1.beg_pos);
5233 }
5234 bodystmt k_end
5235 {
5236 $$ = $3;
5237 /*% ripper: $:3 %*/
5238 }
5239 ;
5240
5241do_block : k_do_block do_body k_end
5242 {
5243 $$ = $2;
5244 set_embraced_location($$, &@1, &@3);
5245 /*% ripper: $:2 %*/
5246 }
5247 ;
5248
5249block_call : command do_block
5250 {
5251 if (nd_type_p($1, NODE_YIELD)) {
5252 compile_error(p, "block given to yield");
5253 }
5254 else {
5255 block_dup_check(p, get_nd_args(p, $1), $2);
5256 }
5257 $$ = method_add_block(p, $1, $2, &@$);
5258 fixpos($$, $1);
5259 /*% ripper: method_add_block!($:1, $:2) %*/
5260 }
5261 | block_call call_op2 operation2 opt_paren_args
5262 {
5263 bool has_args = $4 != 0;
5264 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5265 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5266 /*% ripper: call!($:1, $:2, $:3) %*/
5267 if (has_args) {
5268 /*% ripper: method_add_arg!($:$, $:4) %*/
5269 }
5270 }
5271 | block_call call_op2 operation2 opt_paren_args brace_block
5272 {
5273 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5274 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5275 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
5276 if ($5) {
5277 /*% ripper: method_add_block!($:$, $:5) %*/
5278 }
5279 }
5280 | block_call call_op2 operation2 command_args do_block
5281 {
5282 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5283 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5284 }
5285 ;
5286
5287method_call : fcall paren_args
5288 {
5289 $1->nd_args = $2;
5290 $$ = (NODE *)$1;
5291 nd_set_last_loc($1, @2.end_pos);
5292 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5293 }
5294 | primary_value call_op operation2 opt_paren_args
5295 {
5296 bool has_args = $4 != 0;
5297 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5298 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5299 nd_set_line($$, @3.end_pos.lineno);
5300 /*% ripper: call!($:1, $:2, $:3) %*/
5301 if (has_args) {
5302 /*% ripper: method_add_arg!($:$, $:4) %*/
5303 }
5304 }
5305 | primary_value tCOLON2 operation2 paren_args
5306 {
5307 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5308 nd_set_line($$, @3.end_pos.lineno);
5309 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5310 }
5311 | primary_value tCOLON2 operation3
5312 {
5313 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5314 /*% ripper: call!($:1, $:2, $:3) %*/
5315 }
5316 | primary_value call_op paren_args
5317 {
5318 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5319 nd_set_line($$, @2.end_pos.lineno);
5320 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5321 }
5322 | primary_value tCOLON2 paren_args
5323 {
5324 $$ = new_qcall(p, idCOLON2, $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 | keyword_super paren_args
5329 {
5330 $$ = NEW_SUPER($2, &@$);
5331 /*% ripper: super!($:2) %*/
5332 }
5333 | keyword_super
5334 {
5335 $$ = NEW_ZSUPER(&@$);
5336 /*% ripper: zsuper! %*/
5337 }
5338 | primary_value '[' opt_call_args rbracket
5339 {
5340 $$ = NEW_CALL($1, tAREF, $3, &@$);
5341 fixpos($$, $1);
5342 /*% ripper: aref!($:1, $:3) %*/
5343 }
5344 ;
5345
5346brace_block : '{' brace_body '}'
5347 {
5348 $$ = $2;
5349 set_embraced_location($$, &@1, &@3);
5350 /*% ripper: $:2 %*/
5351 }
5352 | k_do do_body k_end
5353 {
5354 $$ = $2;
5355 set_embraced_location($$, &@1, &@3);
5356 /*% ripper: $:2 %*/
5357 }
5358 ;
5359
5360brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5361 max_numparam numparam it_id allow_exits
5362 opt_block_param[args] compstmt
5363 {
5364 int max_numparam = p->max_numparam;
5365 ID it_id = p->it_id;
5366 p->max_numparam = $max_numparam;
5367 p->it_id = $it_id;
5368 $args = args_with_numbered(p, $args, max_numparam, it_id);
5369 $$ = NEW_ITER($args, $compstmt, &@$);
5370 /*% ripper: brace_block!($:args, $:compstmt) %*/
5371 restore_block_exit(p, $allow_exits);
5372 numparam_pop(p, $numparam);
5373 dyna_pop(p, $dyna);
5374 }
5375 ;
5376
5377do_body : {
5378 $$ = dyna_push(p);
5379 CMDARG_PUSH(0);
5380 }[dyna]<vars>
5381 max_numparam numparam it_id allow_exits
5382 opt_block_param[args] bodystmt
5383 {
5384 int max_numparam = p->max_numparam;
5385 ID it_id = p->it_id;
5386 p->max_numparam = $max_numparam;
5387 p->it_id = $it_id;
5388 $args = args_with_numbered(p, $args, max_numparam, it_id);
5389 $$ = NEW_ITER($args, $bodystmt, &@$);
5390 /*% ripper: do_block!($:args, $:bodystmt) %*/
5391 CMDARG_POP();
5392 restore_block_exit(p, $allow_exits);
5393 numparam_pop(p, $numparam);
5394 dyna_pop(p, $dyna);
5395 }
5396 ;
5397
5398case_args : arg_value
5399 {
5400 check_literal_when(p, $arg_value, &@arg_value);
5401 $$ = NEW_LIST($arg_value, &@$);
5402 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5403 }
5404 | tSTAR arg_value
5405 {
5406 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5407 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5408 }
5409 | case_args[non_last_args] ',' arg_value
5410 {
5411 check_literal_when(p, $arg_value, &@arg_value);
5412 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5413 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5414 }
5415 | case_args[non_last_args] ',' tSTAR arg_value
5416 {
5417 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5418 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5419 }
5420 ;
5421
5422case_body : k_when case_args then
5423 compstmt
5424 cases
5425 {
5426 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5427 fixpos($$, $2);
5428 /*% ripper: when!($:2, $:4, $:5) %*/
5429 }
5430 ;
5431
5432cases : opt_else
5433 | case_body
5434 ;
5435
5436p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5437p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5438
5439p_in_kwarg : {
5440 $$ = p->ctxt;
5441 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5442 p->command_start = FALSE;
5443 p->ctxt.in_kwarg = 1;
5444 }
5445 ;
5446
5447p_case_body : keyword_in
5448 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5449 p_top_expr[expr] then
5450 {
5451 pop_pktbl(p, $p_pktbl);
5452 pop_pvtbl(p, $p_pvtbl);
5453 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5454 }
5455 compstmt
5456 p_cases[cases]
5457 {
5458 $$ = NEW_IN($expr, $compstmt, $cases, &@$);
5459 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5460 }
5461 ;
5462
5463p_cases : opt_else
5464 | p_case_body
5465 ;
5466
5467p_top_expr : p_top_expr_body
5468 | p_top_expr_body modifier_if expr_value
5469 {
5470 $$ = new_if(p, $3, $1, 0, &@$);
5471 fixpos($$, $3);
5472 /*% ripper: if_mod!($:3, $:1) %*/
5473 }
5474 | p_top_expr_body modifier_unless expr_value
5475 {
5476 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5477 fixpos($$, $3);
5478 /*% ripper: unless_mod!($:3, $:1) %*/
5479 }
5480 ;
5481
5482p_top_expr_body : p_expr
5483 | p_expr ','
5484 {
5485 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5486 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5487 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5488 }
5489 | p_expr ',' p_args
5490 {
5491 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5492 nd_set_first_loc($$, @1.beg_pos);
5493 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5494 }
5495 | p_find
5496 {
5497 $$ = new_find_pattern(p, 0, $1, &@$);
5498 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5499 }
5500 | p_args_tail
5501 {
5502 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5503 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5504 }
5505 | p_kwargs
5506 {
5507 $$ = new_hash_pattern(p, 0, $1, &@$);
5508 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5509 }
5510 ;
5511
5512p_expr : p_as
5513 ;
5514
5515p_as : p_expr tASSOC p_variable
5516 {
5517 NODE *n = NEW_LIST($1, &@$);
5518 n = list_append(p, n, $3);
5519 $$ = new_hash(p, n, &@$);
5520 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5521 }
5522 | p_alt
5523 ;
5524
5525p_alt : p_alt '|' p_expr_basic
5526 {
5527 $$ = NEW_OR($1, $3, &@$, &@2);
5528 /*% ripper: binary!($:1, ID2VAL(idOr), $:3) %*/
5529 }
5530 | p_expr_basic
5531 ;
5532
5533p_lparen : '(' p_pktbl
5534 {
5535 $$ = $2;
5536 /*% ripper: $:2 %*/
5537 }
5538 ;
5539
5540p_lbracket : '[' p_pktbl
5541 {
5542 $$ = $2;
5543 /*% ripper: $:2 %*/
5544 }
5545 ;
5546
5547p_expr_basic : p_value
5548 | p_variable
5549 | p_const p_lparen[p_pktbl] p_args rparen
5550 {
5551 pop_pktbl(p, $p_pktbl);
5552 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5553 nd_set_first_loc($$, @p_const.beg_pos);
5554 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5555 }
5556 | p_const p_lparen[p_pktbl] p_find rparen
5557 {
5558 pop_pktbl(p, $p_pktbl);
5559 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5560 nd_set_first_loc($$, @p_const.beg_pos);
5561 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5562 }
5563 | p_const p_lparen[p_pktbl] p_kwargs rparen
5564 {
5565 pop_pktbl(p, $p_pktbl);
5566 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5567 nd_set_first_loc($$, @p_const.beg_pos);
5568 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5569 }
5570 | p_const '(' rparen
5571 {
5572 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5573 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5574 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5575 }
5576 | p_const p_lbracket[p_pktbl] p_args rbracket
5577 {
5578 pop_pktbl(p, $p_pktbl);
5579 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5580 nd_set_first_loc($$, @p_const.beg_pos);
5581 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5582 }
5583 | p_const p_lbracket[p_pktbl] p_find rbracket
5584 {
5585 pop_pktbl(p, $p_pktbl);
5586 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5587 nd_set_first_loc($$, @p_const.beg_pos);
5588 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5589 }
5590 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5591 {
5592 pop_pktbl(p, $p_pktbl);
5593 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5594 nd_set_first_loc($$, @p_const.beg_pos);
5595 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5596 }
5597 | p_const '[' rbracket
5598 {
5599 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5600 $$ = new_array_pattern(p, $1, 0, $$, &@$);
5601 /*% ripper: aryptn!($:1, Qnil, Qnil, Qnil) %*/
5602 }
5603 | tLBRACK p_args rbracket
5604 {
5605 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5606 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5607 }
5608 | tLBRACK p_find rbracket
5609 {
5610 $$ = new_find_pattern(p, 0, $p_find, &@$);
5611 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5612 }
5613 | tLBRACK rbracket
5614 {
5615 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5616 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5617 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5618 }
5619 | tLBRACE p_pktbl lex_ctxt[ctxt]
5620 {
5621 p->ctxt.in_kwarg = 0;
5622 }
5623 p_kwargs rbrace
5624 {
5625 pop_pktbl(p, $p_pktbl);
5626 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5627 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5628 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5629 }
5630 | tLBRACE rbrace
5631 {
5632 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5633 $$ = new_hash_pattern(p, 0, $$, &@$);
5634 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5635 }
5636 | tLPAREN p_pktbl p_expr rparen
5637 {
5638 pop_pktbl(p, $p_pktbl);
5639 $$ = $p_expr;
5640 /*% ripper: $:p_expr %*/
5641 }
5642 ;
5643
5644p_args : p_expr
5645 {
5646 NODE *pre_args = NEW_LIST($1, &@$);
5647 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5648 /*% ripper: [[$:1], Qnil, Qnil] %*/
5649 }
5650 | p_args_head
5651 {
5652 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5653 /*% ripper: [$:1, Qnil, Qnil] %*/
5654 }
5655 | p_args_head p_arg
5656 {
5657 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5658 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5659 }
5660 | p_args_head p_rest
5661 {
5662 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5663 /*% ripper: [$:1, $:2, Qnil] %*/
5664 }
5665 | p_args_head p_rest ',' p_args_post
5666 {
5667 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5668 /*% ripper: [$:1, $:2, $:4] %*/
5669 }
5670 | p_args_tail
5671 ;
5672
5673p_args_head : p_arg ','
5674 {
5675 $$ = $1;
5676 }
5677 | p_args_head p_arg ','
5678 {
5679 $$ = list_concat($1, $2);
5680 /*% ripper: rb_ary_concat($:1, $:2) %*/
5681 }
5682 ;
5683
5684p_args_tail : p_rest
5685 {
5686 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5687 /*% ripper: [Qnil, $:1, Qnil] %*/
5688 }
5689 | p_rest ',' p_args_post
5690 {
5691 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5692 /*% ripper: [Qnil, $:1, $:3] %*/
5693 }
5694 ;
5695
5696p_find : p_rest ',' p_args_post ',' p_rest
5697 {
5698 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5699 /*% ripper: [$:1, $:3, $:5] %*/
5700 }
5701 ;
5702
5703
5704p_rest : tSTAR tIDENTIFIER
5705 {
5706 error_duplicate_pattern_variable(p, $2, &@2);
5707 /*% ripper: var_field!($:2) %*/
5708 $$ = assignable(p, $2, 0, &@$);
5709 }
5710 | tSTAR
5711 {
5712 $$ = 0;
5713 /*% ripper: var_field!(Qnil) %*/
5714 }
5715 ;
5716
5717p_args_post : p_arg
5718 | p_args_post ',' p_arg
5719 {
5720 $$ = list_concat($1, $3);
5721 /*% ripper: rb_ary_concat($:1, $:3) %*/
5722 }
5723 ;
5724
5725p_arg : p_expr
5726 {
5727 $$ = NEW_LIST($1, &@$);
5728 /*% ripper: [$:1] %*/
5729 }
5730 ;
5731
5732p_kwargs : p_kwarg ',' p_any_kwrest
5733 {
5734 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5735 /*% ripper: [$:1, $:3] %*/
5736 }
5737 | p_kwarg
5738 {
5739 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5740 /*% ripper: [$:1, Qnil] %*/
5741 }
5742 | p_kwarg ','
5743 {
5744 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5745 /*% ripper: [$:1, Qnil] %*/
5746 }
5747 | p_any_kwrest
5748 {
5749 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5750 /*% ripper: [[], $:1] %*/
5751 }
5752 ;
5753
5754p_kwarg : p_kw
5755 /*% ripper[brace]: [$:1] %*/
5756 | p_kwarg ',' p_kw
5757 {
5758 $$ = list_concat($1, $3);
5759 /*% ripper: rb_ary_push($:1, $:3) %*/
5760 }
5761 ;
5762
5763p_kw : p_kw_label p_expr
5764 {
5765 error_duplicate_pattern_key(p, $1, &@1);
5766 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5767 /*% ripper: [$:1, $:2] %*/
5768 }
5769 | p_kw_label
5770 {
5771 error_duplicate_pattern_key(p, $1, &@1);
5772 if ($1 && !is_local_id($1)) {
5773 yyerror1(&@1, "key must be valid as local variables");
5774 }
5775 error_duplicate_pattern_variable(p, $1, &@1);
5776 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5777 /*% ripper: [$:1, Qnil] %*/
5778 }
5779 ;
5780
5781p_kw_label : tLABEL
5782 | tSTRING_BEG string_contents tLABEL_END
5783 {
5784 YYLTYPE loc = code_loc_gen(&@1, &@3);
5785 if (!$2 || nd_type_p($2, NODE_STR)) {
5786 NODE *node = dsym_node(p, $2, &loc);
5787 $$ = rb_sym2id(rb_node_sym_string_val(node));
5788 }
5789 else {
5790 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5791 $$ = rb_intern_str(STR_NEW0());
5792 }
5793 /*% ripper: $:2 %*/
5794 }
5795 ;
5796
5797p_kwrest : kwrest_mark tIDENTIFIER
5798 {
5799 $$ = $2;
5800 /*% ripper: var_field!($:2) %*/
5801 }
5802 | kwrest_mark
5803 {
5804 $$ = 0;
5805 /*% ripper: Qnil %*/
5806 }
5807 ;
5808
5809p_kwnorest : kwrest_mark keyword_nil
5810 {
5811 $$ = 0;
5812 }
5813 ;
5814
5815p_any_kwrest : p_kwrest
5816 | p_kwnorest
5817 {
5818 $$ = idNil;
5819 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5820 }
5821 ;
5822
5823p_value : p_primitive
5824 | p_primitive tDOT2 p_primitive
5825 {
5826 value_expr($1);
5827 value_expr($3);
5828 $$ = NEW_DOT2($1, $3, &@$);
5829 /*% ripper: dot2!($:1, $:3) %*/
5830 }
5831 | p_primitive tDOT3 p_primitive
5832 {
5833 value_expr($1);
5834 value_expr($3);
5835 $$ = NEW_DOT3($1, $3, &@$);
5836 /*% ripper: dot3!($:1, $:3) %*/
5837 }
5838 | p_primitive tDOT2
5839 {
5840 value_expr($1);
5841 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
5842 /*% ripper: dot2!($:1, Qnil) %*/
5843 }
5844 | p_primitive tDOT3
5845 {
5846 value_expr($1);
5847 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
5848 /*% ripper: dot3!($:1, Qnil) %*/
5849 }
5850 | p_var_ref
5851 | p_expr_ref
5852 | p_const
5853 | tBDOT2 p_primitive
5854 {
5855 value_expr($2);
5856 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
5857 /*% ripper: dot2!(Qnil, $:2) %*/
5858 }
5859 | tBDOT3 p_primitive
5860 {
5861 value_expr($2);
5862 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
5863 /*% ripper: dot3!(Qnil, $:2) %*/
5864 }
5865 ;
5866
5867p_primitive : inline_primary
5868 | keyword_variable
5869 {
5870 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5871 /*% ripper: var_ref!($:1) %*/
5872 }
5873 | lambda
5874 ;
5875
5876p_variable : tIDENTIFIER
5877 {
5878 error_duplicate_pattern_variable(p, $1, &@1);
5879 /*% ripper: var_field!($:1) %*/
5880 $$ = assignable(p, $1, 0, &@$);
5881 }
5882 ;
5883
5884p_var_ref : '^' tIDENTIFIER
5885 {
5886 NODE *n = gettable(p, $2, &@$);
5887 if (!n) {
5888 n = NEW_ERROR(&@$);
5889 }
5890 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5891 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5892 }
5893 $$ = n;
5894 /*% ripper: var_ref!($:2) %*/
5895 }
5896 | '^' nonlocal_var
5897 {
5898 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5899 /*% ripper: var_ref!($:2) %*/
5900 }
5901 ;
5902
5903p_expr_ref : '^' tLPAREN expr_value rparen
5904 {
5905 $$ = NEW_BLOCK($3, &@$);
5906 /*% ripper: begin!($:3) %*/
5907 }
5908 ;
5909
5910p_const : tCOLON3 cname
5911 {
5912 $$ = NEW_COLON3($2, &@$);
5913 /*% ripper: top_const_ref!($:2) %*/
5914 }
5915 | p_const tCOLON2 cname
5916 {
5917 $$ = NEW_COLON2($1, $3, &@$);
5918 /*% ripper: const_path_ref!($:1, $:3) %*/
5919 }
5920 | tCONSTANT
5921 {
5922 $$ = gettable(p, $1, &@$);
5923 /*% ripper: var_ref!($:1) %*/
5924 }
5925 ;
5926
5927opt_rescue : k_rescue exc_list exc_var then
5928 compstmt
5929 opt_rescue
5930 {
5931 NODE *err = $3;
5932 if ($3) {
5933 err = NEW_ERRINFO(&@3);
5934 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5935 }
5936 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5937 if ($2) {
5938 fixpos($$, $2);
5939 }
5940 else if ($3) {
5941 fixpos($$, $3);
5942 }
5943 else {
5944 fixpos($$, $5);
5945 }
5946 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5947 }
5948 | none
5949 ;
5950
5951exc_list : arg_value
5952 {
5953 $$ = NEW_LIST($1, &@$);
5954 /*% ripper: rb_ary_new3(1, $:1) %*/
5955 }
5956 | mrhs
5957 {
5958 if (!($$ = splat_array($1))) $$ = $1;
5959 }
5960 | none
5961 ;
5962
5963exc_var : tASSOC lhs
5964 {
5965 $$ = $2;
5966 /*% ripper: $:2 %*/
5967 }
5968 | none
5969 ;
5970
5971opt_ensure : k_ensure stmts terms?
5972 {
5973 p->ctxt.in_rescue = $1.in_rescue;
5974 $$ = $2;
5975 void_expr(p, void_stmts(p, $$));
5976 /*% ripper: ensure!($:2) %*/
5977 }
5978 | none
5979 ;
5980
5981literal : numeric
5982 | symbol
5983 ;
5984
5985strings : string
5986 {
5987 NODE *node = $1;
5988 if (!node) {
5989 node = NEW_STR(STRING_NEW0(), &@$);
5990 }
5991 else {
5992 node = evstr2dstr(p, node);
5993 }
5994 $$ = node;
5995 /*% ripper: $:1 %*/
5996 }
5997 ;
5998
5999string : tCHAR
6000 | string1
6001 | string string1
6002 {
6003 $$ = literal_concat(p, $1, $2, &@$);
6004 /*% ripper: string_concat!($:1, $:2) %*/
6005 }
6006 ;
6007
6008string1 : tSTRING_BEG string_contents tSTRING_END
6009 {
6010 $$ = heredoc_dedent(p, $2);
6011 if ($$) nd_set_loc($$, &@$);
6012 /*% ripper: $:2 %*/
6013 if (p->heredoc_indent > 0) {
6014 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
6015 p->heredoc_indent = 0;
6016 }
6017 /*% ripper: string_literal!($:$) %*/
6018 }
6019 ;
6020
6021xstring : tXSTRING_BEG xstring_contents tSTRING_END
6022 {
6023 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
6024 /*% ripper: $:2 %*/
6025 if (p->heredoc_indent > 0) {
6026 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
6027 p->heredoc_indent = 0;
6028 }
6029 /*% ripper: xstring_literal!($:$) %*/
6030 }
6031 ;
6032
6033regexp : tREGEXP_BEG regexp_contents tREGEXP_END
6034 {
6035 $$ = new_regexp(p, $2, $3, &@$);
6036 /*% ripper: regexp_literal!($:2, $:3) %*/
6037 }
6038 ;
6039
6040words : words(tWORDS_BEG, word_list) <node>
6041 ;
6042
6043word_list : /* none */
6044 {
6045 $$ = 0;
6046 /*% ripper: words_new! %*/
6047 }
6048 | word_list word ' '+
6049 {
6050 $$ = list_append(p, $1, evstr2dstr(p, $2));
6051 /*% ripper: words_add!($:1, $:2) %*/
6052 }
6053 ;
6054
6055word : string_content
6056 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
6057 | word string_content
6058 {
6059 $$ = literal_concat(p, $1, $2, &@$);
6060 /*% ripper: word_add!($:1, $:2) %*/
6061 }
6062 ;
6063
6064symbols : words(tSYMBOLS_BEG, symbol_list) <node>
6065 ;
6066
6067symbol_list : /* none */
6068 {
6069 $$ = 0;
6070 /*% ripper: symbols_new! %*/
6071 }
6072 | symbol_list word ' '+
6073 {
6074 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
6075 /*% ripper: symbols_add!($:1, $:2) %*/
6076 }
6077 ;
6078
6079qwords : words(tQWORDS_BEG, qword_list) <node>
6080 ;
6081
6082qsymbols : words(tQSYMBOLS_BEG, qsym_list) <node>
6083 ;
6084
6085qword_list : /* none */
6086 {
6087 $$ = 0;
6088 /*% ripper: qwords_new! %*/
6089 }
6090 | qword_list tSTRING_CONTENT ' '+
6091 {
6092 $$ = list_append(p, $1, $2);
6093 /*% ripper: qwords_add!($:1, $:2) %*/
6094 }
6095 ;
6096
6097qsym_list : /* none */
6098 {
6099 $$ = 0;
6100 /*% ripper: qsymbols_new! %*/
6101 }
6102 | qsym_list tSTRING_CONTENT ' '+
6103 {
6104 $$ = symbol_append(p, $1, $2);
6105 /*% ripper: qsymbols_add!($:1, $:2) %*/
6106 }
6107 ;
6108
6109string_contents : /* none */
6110 {
6111 $$ = 0;
6112 /*% ripper: string_content! %*/
6113 }
6114 | string_contents string_content
6115 {
6116 $$ = literal_concat(p, $1, $2, &@$);
6117 /*% ripper: string_add!($:1, $:2) %*/
6118 }
6119 ;
6120
6121xstring_contents: /* none */
6122 {
6123 $$ = 0;
6124 /*% ripper: xstring_new! %*/
6125 }
6126 | xstring_contents string_content
6127 {
6128 $$ = literal_concat(p, $1, $2, &@$);
6129 /*% ripper: xstring_add!($:1, $:2) %*/
6130 }
6131 ;
6132
6133regexp_contents: /* none */
6134 {
6135 $$ = 0;
6136 /*% ripper: regexp_new! %*/
6137 }
6138 | regexp_contents string_content
6139 {
6140 NODE *head = $1, *tail = $2;
6141 if (!head) {
6142 $$ = tail;
6143 }
6144 else if (!tail) {
6145 $$ = head;
6146 }
6147 else {
6148 switch (nd_type(head)) {
6149 case NODE_STR:
6150 head = str2dstr(p, head);
6151 break;
6152 case NODE_DSTR:
6153 break;
6154 default:
6155 head = list_append(p, NEW_DSTR(0, &@$), head);
6156 break;
6157 }
6158 $$ = list_append(p, head, tail);
6159 }
6160 /*% ripper: regexp_add!($:1, $:2) %*/
6161 }
6162 ;
6163
6164string_content : tSTRING_CONTENT
6165 /*% ripper[brace]: $:1 %*/
6166 | tSTRING_DVAR
6167 {
6168 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6169 $$ = p->lex.strterm;
6170 p->lex.strterm = 0;
6171 SET_LEX_STATE(EXPR_BEG);
6172 }<strterm>
6173 string_dvar
6174 {
6175 p->lex.strterm = $2;
6176 $$ = NEW_EVSTR($3, &@$);
6177 nd_set_line($$, @3.end_pos.lineno);
6178 /*% ripper: string_dvar!($:3) %*/
6179 }
6180 | tSTRING_DBEG[state]
6181 {
6182 CMDARG_PUSH(0);
6183 COND_PUSH(0);
6184 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6185 $$ = p->lex.strterm;
6186 p->lex.strterm = 0;
6187 SET_LEX_STATE(EXPR_BEG);
6188 }[term]<strterm>
6189 {
6190 $$ = p->lex.brace_nest;
6191 p->lex.brace_nest = 0;
6192 }[brace]<num>
6193 {
6194 $$ = p->heredoc_indent;
6195 p->heredoc_indent = 0;
6196 }[indent]<num>
6197 compstmt string_dend
6198 {
6199 COND_POP();
6200 CMDARG_POP();
6201 p->lex.strterm = $term;
6202 SET_LEX_STATE($state);
6203 p->lex.brace_nest = $brace;
6204 p->heredoc_indent = $indent;
6205 p->heredoc_line_indent = -1;
6206 if ($compstmt) nd_unset_fl_newline($compstmt);
6207 $$ = new_evstr(p, $compstmt, &@$);
6208 /*% ripper: string_embexpr!($:compstmt) %*/
6209 }
6210 ;
6211
6212string_dend : tSTRING_DEND
6213 | END_OF_INPUT
6214 ;
6215
6216string_dvar : nonlocal_var
6217 {
6218 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6219 /*% ripper: var_ref!($:1) %*/
6220 }
6221 | backref
6222 ;
6223
6224symbol : ssym
6225 | dsym
6226 ;
6227
6228ssym : tSYMBEG sym
6229 {
6230 SET_LEX_STATE(EXPR_END);
6231 VALUE str = rb_id2str($2);
6232 /*
6233 * TODO:
6234 * set_yylval_noname sets invalid id to yylval.
6235 * This branch can be removed once yylval is changed to
6236 * hold lexed string.
6237 */
6238 if (!str) str = STR_NEW0();
6239 $$ = NEW_SYM(str, &@$);
6240 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6241 }
6242 ;
6243
6244sym : fname
6245 | nonlocal_var
6246 ;
6247
6248dsym : tSYMBEG string_contents tSTRING_END
6249 {
6250 SET_LEX_STATE(EXPR_END);
6251 $$ = dsym_node(p, $2, &@$);
6252 /*% ripper: dyna_symbol!($:2) %*/
6253 }
6254 ;
6255
6256numeric : simple_numeric
6257 | tUMINUS_NUM simple_numeric %prec tLOWEST
6258 {
6259 $$ = $2;
6260 negate_lit(p, $$);
6261 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6262 }
6263 ;
6264
6265simple_numeric : tINTEGER
6266 | tFLOAT
6267 | tRATIONAL
6268 | tIMAGINARY
6269 ;
6270
6271nonlocal_var : tIVAR
6272 | tGVAR
6273 | tCVAR
6274 ;
6275
6276user_variable : ident_or_const
6277 | nonlocal_var
6278 ;
6279
6280keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6281 | keyword_self {$$ = KWD2EID(self, $1);}
6282 | keyword_true {$$ = KWD2EID(true, $1);}
6283 | keyword_false {$$ = KWD2EID(false, $1);}
6284 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6285 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6286 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6287 ;
6288
6289var_ref : user_variable
6290 {
6291 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6292 if (ifdef_ripper(id_is_var(p, $1), false)) {
6293 /*% ripper: var_ref!($:1) %*/
6294 }
6295 else {
6296 /*% ripper: vcall!($:1) %*/
6297 }
6298 }
6299 | keyword_variable
6300 {
6301 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6302 /*% ripper: var_ref!($:1) %*/
6303 }
6304 ;
6305
6306var_lhs : user_or_keyword_variable
6307 {
6308 /*% ripper: var_field!($:1) %*/
6309 $$ = assignable(p, $1, 0, &@$);
6310 }
6311 ;
6312
6313backref : tNTH_REF
6314 | tBACK_REF
6315 ;
6316
6317superclass : '<'
6318 {
6319 SET_LEX_STATE(EXPR_BEG);
6320 p->command_start = TRUE;
6321 }
6322 expr_value term
6323 {
6324 $$ = $3;
6325 /*% ripper: $:3 %*/
6326 }
6327 | /* none */
6328 {
6329 $$ = 0;
6330 /*% ripper: Qnil %*/
6331 }
6332 ;
6333
6334f_opt_paren_args: f_paren_args
6335 | none
6336 {
6337 p->ctxt.in_argdef = 0;
6338 $$ = new_args_tail(p, 0, 0, 0, &@0);
6339 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6340 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6341 }
6342 ;
6343
6344f_paren_args : '(' f_args rparen
6345 {
6346 $$ = $2;
6347 /*% ripper: paren!($:2) %*/
6348 SET_LEX_STATE(EXPR_BEG);
6349 p->command_start = TRUE;
6350 p->ctxt.in_argdef = 0;
6351 }
6352 ;
6353
6354f_arglist : f_paren_args
6355 | {
6356 $$ = p->ctxt;
6357 p->ctxt.in_kwarg = 1;
6358 p->ctxt.in_argdef = 1;
6359 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6360 }<ctxt>
6361 f_args term
6362 {
6363 p->ctxt.in_kwarg = $1.in_kwarg;
6364 p->ctxt.in_argdef = 0;
6365 $$ = $2;
6366 SET_LEX_STATE(EXPR_BEG);
6367 p->command_start = TRUE;
6368 /*% ripper: $:2 %*/
6369 }
6370 ;
6371
6372args_tail : f_kwarg(f_kw) ',' f_kwrest opt_f_block_arg
6373 {
6374 $$ = new_args_tail(p, $1, $3, $4, &@3);
6375 /*% ripper: [$:1, $:3, $:4] %*/
6376 }
6377 | f_kwarg(f_kw) opt_f_block_arg
6378 {
6379 $$ = new_args_tail(p, $1, 0, $2, &@1);
6380 /*% ripper: [$:1, Qnil, $:2] %*/
6381 }
6382 | f_any_kwrest opt_f_block_arg
6383 {
6384 $$ = new_args_tail(p, 0, $1, $2, &@1);
6385 /*% ripper: [Qnil, $:1, $:2] %*/
6386 }
6387 | f_block_arg
6388 {
6389 $$ = new_args_tail(p, 0, 0, $1, &@1);
6390 /*% ripper: [Qnil, Qnil, $:1] %*/
6391 }
6392 | args_forward
6393 {
6394 ID fwd = $args_forward;
6395 if (lambda_beginning_p() ||
6396 (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest)) {
6397 yyerror0("unexpected ... in lambda argument");
6398 fwd = 0;
6399 }
6400 else {
6401 add_forwarding_args(p);
6402 }
6403 $$ = new_args_tail(p, 0, fwd, arg_FWD_BLOCK, &@1);
6404 $$->nd_ainfo.forwarding = 1;
6405 /*% ripper: [Qnil, $:1, Qnil] %*/
6406 }
6407 ;
6408
6409f_args : f_arg ',' f_optarg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6410 {
6411 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
6412 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
6413 }
6414 | f_arg ',' f_optarg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6415 {
6416 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6417 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
6418 }
6419 | f_arg ',' f_optarg(arg_value) opt_args_tail(args_tail)
6420 {
6421 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
6422 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
6423 }
6424 | f_arg ',' f_optarg(arg_value) ',' f_arg opt_args_tail(args_tail)
6425 {
6426 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
6427 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
6428 }
6429 | f_arg ',' f_rest_arg opt_args_tail(args_tail)
6430 {
6431 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
6432 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
6433 }
6434 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6435 {
6436 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
6437 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
6438 }
6439 | f_arg opt_args_tail(args_tail)
6440 {
6441 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
6442 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
6443 }
6444 | f_optarg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6445 {
6446 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
6447 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
6448 }
6449 | f_optarg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6450 {
6451 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
6452 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
6453 }
6454 | f_optarg(arg_value) opt_args_tail(args_tail)
6455 {
6456 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
6457 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
6458 }
6459 | f_optarg(arg_value) ',' f_arg opt_args_tail(args_tail)
6460 {
6461 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
6462 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
6463 }
6464 | f_rest_arg opt_args_tail(args_tail)
6465 {
6466 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
6467 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
6468 }
6469 | f_rest_arg ',' f_arg opt_args_tail(args_tail)
6470 {
6471 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
6472 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
6473 }
6474 | args_tail
6475 {
6476 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
6477 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
6478 }
6479 | /* none */
6480 {
6481 $$ = new_args_tail(p, 0, 0, 0, &@0);
6482 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6483 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6484 }
6485 ;
6486
6487args_forward : tBDOT3
6488 {
6489#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
6490 $$ = 0;
6491#else
6492 $$ = idFWD_KWREST;
6493#endif
6494 /*% ripper: args_forward! %*/
6495 }
6496 ;
6497
6498f_bad_arg : tCONSTANT
6499 {
6500 static const char mesg[] = "formal argument cannot be a constant";
6501 /*%%%*/
6502 yyerror1(&@1, mesg);
6503 /*% %*/
6504 $$ = 0;
6505 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6506 }
6507 | tIVAR
6508 {
6509 static const char mesg[] = "formal argument cannot be an instance variable";
6510 /*%%%*/
6511 yyerror1(&@1, mesg);
6512 /*% %*/
6513 $$ = 0;
6514 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6515 }
6516 | tGVAR
6517 {
6518 static const char mesg[] = "formal argument cannot be a global variable";
6519 /*%%%*/
6520 yyerror1(&@1, mesg);
6521 /*% %*/
6522 $$ = 0;
6523 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6524 }
6525 | tCVAR
6526 {
6527 static const char mesg[] = "formal argument cannot be a class variable";
6528 /*%%%*/
6529 yyerror1(&@1, mesg);
6530 /*% %*/
6531 $$ = 0;
6532 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6533 }
6534 ;
6535
6536f_norm_arg : f_bad_arg
6537 | tIDENTIFIER
6538 {
6539 VALUE e = formal_argument_error(p, $$ = $1);
6540 if (e) {
6541 /*% ripper[error]: param_error!(?e, $:1) %*/
6542 }
6543 p->max_numparam = ORDINAL_PARAM;
6544 }
6545 ;
6546
6547f_arg_asgn : f_norm_arg
6548 {
6549 ID id = $1;
6550 arg_var(p, id);
6551 $$ = $1;
6552 }
6553 ;
6554
6555f_arg_item : f_arg_asgn
6556 {
6557 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6558 /*% ripper: $:1 %*/
6559 }
6560 | tLPAREN f_margs rparen
6561 {
6562 ID tid = internal_id(p);
6563 YYLTYPE loc;
6564 loc.beg_pos = @2.beg_pos;
6565 loc.end_pos = @2.beg_pos;
6566 arg_var(p, tid);
6567 if (dyna_in_block(p)) {
6568 $2->nd_value = NEW_DVAR(tid, &loc);
6569 }
6570 else {
6571 $2->nd_value = NEW_LVAR(tid, &loc);
6572 }
6573 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6574 $$->nd_next = (NODE *)$2;
6575 /*% ripper: mlhs_paren!($:2) %*/
6576 }
6577 ;
6578
6579f_arg : f_arg_item
6580 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6581 | f_arg ',' f_arg_item
6582 {
6583 $$ = $1;
6584 $$->nd_plen++;
6585 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6586 rb_discard_node(p, (NODE *)$3);
6587 /*% ripper: rb_ary_push($:1, $:3) %*/
6588 }
6589 ;
6590
6591
6592f_label : tLABEL
6593 {
6594 VALUE e = formal_argument_error(p, $$ = $1);
6595 if (e) {
6596 $$ = 0;
6597 /*% ripper[error]: param_error!(?e, $:1) %*/
6598 }
6599 /*
6600 * Workaround for Prism::ParseTest#test_filepath for
6601 * "unparser/corpus/literal/def.txt"
6602 *
6603 * See the discussion on https://github.com/ruby/ruby/pull/9923
6604 */
6605 arg_var(p, ifdef_ripper(0, $1));
6606 /*% ripper: $:1 %*/
6607 p->max_numparam = ORDINAL_PARAM;
6608 p->ctxt.in_argdef = 0;
6609 }
6610 ;
6611
6612f_kw : f_label arg_value
6613 {
6614 p->ctxt.in_argdef = 1;
6615 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6616 /*% ripper: [$:$, $:2] %*/
6617 }
6618 | f_label
6619 {
6620 p->ctxt.in_argdef = 1;
6621 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6622 /*% ripper: [$:$, 0] %*/
6623 }
6624 ;
6625
6626f_block_kw : f_label primary_value
6627 {
6628 p->ctxt.in_argdef = 1;
6629 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6630 /*% ripper: [$:$, $:2] %*/
6631 }
6632 | f_label
6633 {
6634 p->ctxt.in_argdef = 1;
6635 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6636 /*% ripper: [$:$, 0] %*/
6637 }
6638 ;
6639
6640kwrest_mark : tPOW
6641 | tDSTAR
6642 ;
6643
6644f_no_kwarg : p_kwnorest
6645 {
6646 /*% ripper: nokw_param!(Qnil) %*/
6647 }
6648 ;
6649
6650f_kwrest : kwrest_mark tIDENTIFIER
6651 {
6652 arg_var(p, shadowing_lvar(p, $2));
6653 $$ = $2;
6654 /*% ripper: kwrest_param!($:2) %*/
6655 }
6656 | kwrest_mark
6657 {
6658 arg_var(p, idFWD_KWREST);
6659 $$ = idFWD_KWREST;
6660 /*% ripper: kwrest_param!(Qnil) %*/
6661 }
6662 ;
6663
6664restarg_mark : '*'
6665 | tSTAR
6666 ;
6667
6668f_rest_arg : restarg_mark tIDENTIFIER
6669 {
6670 arg_var(p, shadowing_lvar(p, $2));
6671 $$ = $2;
6672 /*% ripper: rest_param!($:2) %*/
6673 }
6674 | restarg_mark
6675 {
6676 arg_var(p, idFWD_REST);
6677 $$ = idFWD_REST;
6678 /*% ripper: rest_param!(Qnil) %*/
6679 }
6680 ;
6681
6682blkarg_mark : '&'
6683 | tAMPER
6684 ;
6685
6686f_block_arg : blkarg_mark tIDENTIFIER
6687 {
6688 arg_var(p, shadowing_lvar(p, $2));
6689 $$ = $2;
6690 /*% ripper: blockarg!($:2) %*/
6691 }
6692 | blkarg_mark
6693 {
6694 arg_var(p, idFWD_BLOCK);
6695 $$ = idFWD_BLOCK;
6696 /*% ripper: blockarg!(Qnil) %*/
6697 }
6698 ;
6699
6700opt_f_block_arg : ',' f_block_arg
6701 {
6702 $$ = $2;
6703 /*% ripper: $:2 %*/
6704 }
6705 | none
6706 {
6707 $$ = 0;
6708 /*% ripper: Qnil %*/
6709 }
6710 ;
6711
6712singleton : var_ref
6713 {
6714 value_expr($1);
6715 $$ = $1;
6716 }
6717 | '('
6718 {
6719 SET_LEX_STATE(EXPR_BEG);
6720 p->ctxt.in_argdef = 0;
6721 }
6722 expr rparen
6723 {
6724 p->ctxt.in_argdef = 1;
6725 NODE *expr = last_expr_node($3);
6726 switch (nd_type(expr)) {
6727 case NODE_STR:
6728 case NODE_DSTR:
6729 case NODE_XSTR:
6730 case NODE_DXSTR:
6731 case NODE_REGX:
6732 case NODE_DREGX:
6733 case NODE_SYM:
6734 case NODE_LINE:
6735 case NODE_FILE:
6736 case NODE_ENCODING:
6737 case NODE_INTEGER:
6738 case NODE_FLOAT:
6739 case NODE_RATIONAL:
6740 case NODE_IMAGINARY:
6741 case NODE_DSYM:
6742 case NODE_LIST:
6743 case NODE_ZLIST:
6744 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6745 break;
6746 default:
6747 value_expr($3);
6748 break;
6749 }
6750 $$ = $3;
6751 /*% ripper: paren!($:3) %*/
6752 }
6753 ;
6754
6755assoc_list : none
6756 | assocs trailer
6757 {
6758 $$ = $1;
6759 /*% ripper: assoclist_from_args!($:1) %*/
6760 }
6761 ;
6762
6763assocs : assoc
6764 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6765 | assocs ',' assoc
6766 {
6767 NODE *assocs = $1;
6768 NODE *tail = $3;
6769 if (!assocs) {
6770 assocs = tail;
6771 }
6772 else if (tail) {
6773 if (RNODE_LIST(assocs)->nd_head) {
6774 NODE *n = RNODE_LIST(tail)->nd_next;
6775 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6776 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6777 /* DSTAR */
6778 tail = RNODE_HASH(n)->nd_head;
6779 }
6780 }
6781 if (tail) {
6782 assocs = list_concat(assocs, tail);
6783 }
6784 }
6785 $$ = assocs;
6786 /*% ripper: rb_ary_push($:1, $:3) %*/
6787 }
6788 ;
6789
6790assoc : arg_value tASSOC arg_value
6791 {
6792 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6793 /*% ripper: assoc_new!($:1, $:3) %*/
6794 }
6795 | tLABEL arg_value
6796 {
6797 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6798 /*% ripper: assoc_new!($:1, $:2) %*/
6799 }
6800 | tLABEL
6801 {
6802 NODE *val = gettable(p, $1, &@$);
6803 if (!val) val = NEW_ERROR(&@$);
6804 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6805 /*% ripper: assoc_new!($:1, Qnil) %*/
6806 }
6807 | tSTRING_BEG string_contents tLABEL_END arg_value
6808 {
6809 YYLTYPE loc = code_loc_gen(&@1, &@3);
6810 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6811 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6812 }
6813 | tDSTAR arg_value
6814 {
6815 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6816 /*% ripper: assoc_splat!($:2) %*/
6817 }
6818 | tDSTAR
6819 {
6820 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6821 $$ = list_append(p, NEW_LIST(0, &@$),
6822 NEW_LVAR(idFWD_KWREST, &@$));
6823 /*% ripper: assoc_splat!(Qnil) %*/
6824 }
6825 ;
6826
6827operation : ident_or_const
6828 | tFID
6829 ;
6830
6831operation2 : operation
6832 | op
6833 ;
6834
6835operation3 : tIDENTIFIER
6836 | tFID
6837 | op
6838 ;
6839
6840dot_or_colon : '.'
6841 | tCOLON2
6842 ;
6843
6844call_op : '.'
6845 | tANDDOT
6846 ;
6847
6848call_op2 : call_op
6849 | tCOLON2
6850 ;
6851
6852rparen : '\n'? ')'
6853 ;
6854
6855rbracket : '\n'? ']'
6856 ;
6857
6858rbrace : '\n'? '}'
6859 ;
6860
6861trailer : '\n'?
6862 | ','
6863 ;
6864
6865term : ';' {yyerrok;token_flush(p);}
6866 | '\n'
6867 {
6868 @$.end_pos = @$.beg_pos;
6869 token_flush(p);
6870 }
6871 ;
6872
6873terms : term
6874 | terms ';' {yyerrok;}
6875 ;
6876
6877none : /* none */
6878 {
6879 $$ = 0;
6880 }
6881 ;
6882%%
6883# undef p
6884# undef yylex
6885# undef yylval
6886# define yylval (*p->lval)
6887
6888static int regx_options(struct parser_params*);
6889static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6890static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6891static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6892static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6893
6894#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6895
6896# define set_yylval_node(x) { \
6897 YYLTYPE _cur_loc; \
6898 rb_parser_set_location(p, &_cur_loc); \
6899 yylval.node = (x); \
6900 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6901}
6902# define set_yylval_str(x) \
6903do { \
6904 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6905 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6906} while(0)
6907# define set_yylval_num(x) { \
6908 yylval.num = (x); \
6909 set_parser_s_value(x); \
6910}
6911# define set_yylval_id(x) (yylval.id = (x))
6912# define set_yylval_name(x) { \
6913 (yylval.id = (x)); \
6914 set_parser_s_value(ID2SYM(x)); \
6915}
6916# define yylval_id() (yylval.id)
6917
6918#define set_yylval_noname() set_yylval_id(keyword_nil)
6919#define has_delayed_token(p) (p->delayed.token != NULL)
6920
6921#ifndef RIPPER
6922#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6923#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6924
6925static bool
6926parser_has_token(struct parser_params *p)
6927{
6928 const char *const pcur = p->lex.pcur;
6929 const char *const ptok = p->lex.ptok;
6930 if (p->keep_tokens && (pcur < ptok)) {
6931 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6932 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6933 }
6934 return pcur > ptok;
6935}
6936
6937static const char *
6938escaped_char(int c)
6939{
6940 switch (c) {
6941 case '"': return "\\\"";
6942 case '\\': return "\\\\";
6943 case '\0': return "\\0";
6944 case '\n': return "\\n";
6945 case '\r': return "\\r";
6946 case '\t': return "\\t";
6947 case '\f': return "\\f";
6948 case '\013': return "\\v";
6949 case '\010': return "\\b";
6950 case '\007': return "\\a";
6951 case '\033': return "\\e";
6952 case '\x7f': return "\\c?";
6953 }
6954 return NULL;
6955}
6956
6957static rb_parser_string_t *
6958rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6959{
6960 rb_encoding *enc = p->enc;
6961 const char *ptr = str->ptr;
6962 const char *pend = ptr + str->len;
6963 const char *prev = ptr;
6964 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6965 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6966
6967 while (ptr < pend) {
6968 unsigned int c;
6969 const char *cc;
6970 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6971 if (!MBCLEN_CHARFOUND_P(n)) {
6972 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6973 n = rb_enc_mbminlen(enc);
6974 if (pend < ptr + n)
6975 n = (int)(pend - ptr);
6976 while (n--) {
6977 c = *ptr & 0xf0 >> 4;
6978 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6979 c = *ptr & 0x0f;
6980 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6981 parser_str_cat(result, charbuf, 4);
6982 prev = ++ptr;
6983 }
6984 continue;
6985 }
6986 n = MBCLEN_CHARFOUND_LEN(n);
6987 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6988 ptr += n;
6989 cc = escaped_char(c);
6990 if (cc) {
6991 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6992 parser_str_cat_cstr(result, cc);
6993 prev = ptr;
6994 }
6995 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6996 }
6997 else {
6998 if (ptr - n > prev) {
6999 parser_str_cat(result, prev, ptr - n - prev);
7000 prev = ptr - n;
7001 }
7002 parser_str_cat(result, prev, ptr - prev);
7003 prev = ptr;
7004 }
7005 }
7006 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
7007
7008 return result;
7009}
7010
7011static void
7012parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
7013{
7014 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
7015 token->id = p->token_id;
7016 token->type_name = parser_token2char(p, t);
7017 token->str = str;
7018 token->loc.beg_pos = p->yylloc->beg_pos;
7019 token->loc.end_pos = p->yylloc->end_pos;
7020 rb_parser_ary_push_ast_token(p, p->tokens, token);
7021 p->token_id++;
7022
7023 if (p->debug) {
7024 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
7025 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
7026 line, token->id, token->type_name, str_escaped->ptr,
7027 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
7028 token->loc.end_pos.lineno, token->loc.end_pos.column);
7029 rb_parser_string_free(p, str_escaped);
7030 }
7031}
7032
7033static void
7034parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
7035{
7036 debug_token_line(p, "parser_dispatch_scan_event", line);
7037
7038 if (!parser_has_token(p)) return;
7039
7040 RUBY_SET_YYLLOC(*p->yylloc);
7041
7042 if (p->keep_tokens) {
7043 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
7044 parser_append_tokens(p, str, t, line);
7045 }
7046
7047 token_flush(p);
7048}
7049
7050#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
7051static void
7052parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
7053{
7054 debug_token_line(p, "parser_dispatch_delayed_token", line);
7055
7056 if (!has_delayed_token(p)) return;
7057
7058 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
7059
7060 if (p->keep_tokens) {
7061 /* p->delayed.token is freed by rb_parser_tokens_free */
7062 parser_append_tokens(p, p->delayed.token, t, line);
7063 } else {
7064 rb_parser_string_free(p, p->delayed.token);
7065 }
7066
7067 p->delayed.token = NULL;
7068}
7069#else
7070#define literal_flush(p, ptr) ((void)(ptr))
7071
7072static int
7073ripper_has_scan_event(struct parser_params *p)
7074{
7075 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
7076 return p->lex.pcur > p->lex.ptok;
7077}
7078
7079static VALUE
7080ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
7081{
7082 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
7083 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
7084 RUBY_SET_YYLLOC(*p->yylloc);
7085 token_flush(p);
7086 return rval;
7087}
7088
7089static void
7090ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
7091{
7092 if (!ripper_has_scan_event(p)) return;
7093
7094 set_parser_s_value(ripper_scan_event_val(p, t));
7095}
7096#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
7097
7098static void
7099ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
7100{
7101 /* save and adjust the location to delayed token for callbacks */
7102 int saved_line = p->ruby_sourceline;
7103 const char *saved_tokp = p->lex.ptok;
7104 VALUE s_value, str;
7105
7106 if (!has_delayed_token(p)) return;
7107 p->ruby_sourceline = p->delayed.beg_line;
7108 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
7109 str = rb_str_new_mutable_parser_string(p->delayed.token);
7110 rb_parser_string_free(p, p->delayed.token);
7111 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
7112 set_parser_s_value(s_value);
7113 p->delayed.token = NULL;
7114 p->ruby_sourceline = saved_line;
7115 p->lex.ptok = saved_tokp;
7116}
7117#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
7118#endif /* RIPPER */
7119
7120static inline int
7121is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
7122{
7123 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
7124}
7125
7126static inline int
7127parser_is_identchar(struct parser_params *p)
7128{
7129 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7130}
7131
7132static inline int
7133parser_isascii(struct parser_params *p)
7134{
7135 return ISASCII(*(p->lex.pcur-1));
7136}
7137
7138static void
7139token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7140{
7141 int column = 1, nonspc = 0, i;
7142 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7143 if (*ptr == '\t') {
7144 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7145 }
7146 column++;
7147 if (*ptr != ' ' && *ptr != '\t') {
7148 nonspc = 1;
7149 }
7150 }
7151
7152 ptinfo->beg = loc->beg_pos;
7153 ptinfo->indent = column;
7154 ptinfo->nonspc = nonspc;
7155}
7156
7157static void
7158token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7159{
7160 token_info *ptinfo;
7161
7162 if (!p->token_info_enabled) return;
7163 ptinfo = ALLOC(token_info);
7164 ptinfo->token = token;
7165 ptinfo->next = p->token_info;
7166 token_info_setup(ptinfo, p->lex.pbeg, loc);
7167
7168 p->token_info = ptinfo;
7169}
7170
7171static void
7172token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7173{
7174 token_info *ptinfo_beg = p->token_info;
7175
7176 if (!ptinfo_beg) return;
7177
7178 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7179 token_info_warn(p, token, ptinfo_beg, 1, loc);
7180
7181 p->token_info = ptinfo_beg->next;
7182 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7183}
7184
7185static void
7186token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7187{
7188 token_info *ptinfo_beg = p->token_info;
7189
7190 if (!ptinfo_beg) return;
7191 p->token_info = ptinfo_beg->next;
7192
7193 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7194 ptinfo_beg->beg.column != beg_pos.column ||
7195 strcmp(ptinfo_beg->token, token)) {
7196 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7197 beg_pos.lineno, beg_pos.column, token,
7198 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7199 ptinfo_beg->token);
7200 }
7201
7202 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7203}
7204
7205static void
7206token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7207{
7208 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7209 if (!p->token_info_enabled) return;
7210 if (!ptinfo_beg) return;
7211 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7212 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7213 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7214 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7215 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7216 rb_warn3L(ptinfo_end->beg.lineno,
7217 "mismatched indentations at '%s' with '%s' at %d",
7218 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7219}
7220
7221static int
7222parser_precise_mbclen(struct parser_params *p, const char *ptr)
7223{
7224 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7225 if (!MBCLEN_CHARFOUND_P(len)) {
7226 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7227 return -1;
7228 }
7229 return len;
7230}
7231
7232#ifndef RIPPER
7233static inline void
7234parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7235{
7236 rb_parser_string_t *str;
7237 int lineno = p->ruby_sourceline;
7238 if (!yylloc) {
7239 return;
7240 }
7241 else if (yylloc->beg_pos.lineno == lineno) {
7242 str = p->lex.lastline;
7243 }
7244 else {
7245 return;
7246 }
7247 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7248}
7249
7250static int
7251parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7252{
7253#if 0
7254 YYLTYPE current;
7255
7256 if (!yylloc) {
7257 yylloc = RUBY_SET_YYLLOC(current);
7258 }
7259 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7260 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7261 yylloc = 0;
7262 }
7263#endif
7264 parser_compile_error(p, yylloc, "%s", msg);
7265 parser_show_error_line(p, yylloc);
7266 return 0;
7267}
7268
7269static int
7270parser_yyerror0(struct parser_params *p, const char *msg)
7271{
7272 YYLTYPE current;
7273 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7274}
7275
7276void
7277ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7278{
7279 VALUE mesg;
7280 const int max_line_margin = 30;
7281 const char *ptr, *ptr_end, *pt, *pb;
7282 const char *pre = "", *post = "", *pend;
7283 const char *code = "", *caret = "";
7284 const char *lim;
7285 const char *const pbeg = PARSER_STRING_PTR(str);
7286 char *buf;
7287 long len;
7288 int i;
7289
7290 if (!yylloc) return;
7291 pend = rb_parser_string_end(str);
7292 if (pend > pbeg && pend[-1] == '\n') {
7293 if (--pend > pbeg && pend[-1] == '\r') --pend;
7294 }
7295
7296 pt = pend;
7297 if (lineno == yylloc->end_pos.lineno &&
7298 (pend - pbeg) > yylloc->end_pos.column) {
7299 pt = pbeg + yylloc->end_pos.column;
7300 }
7301
7302 ptr = ptr_end = pt;
7303 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7304 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7305
7306 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7307 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7308
7309 len = ptr_end - ptr;
7310 if (len > 4) {
7311 if (ptr > pbeg) {
7312 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7313 if (ptr > pbeg) pre = "...";
7314 }
7315 if (ptr_end < pend) {
7316 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7317 if (ptr_end < pend) post = "...";
7318 }
7319 }
7320 pb = pbeg;
7321 if (lineno == yylloc->beg_pos.lineno) {
7322 pb += yylloc->beg_pos.column;
7323 if (pb > pt) pb = pt;
7324 }
7325 if (pb < ptr) pb = ptr;
7326 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7327 return;
7328 }
7329 if (RTEST(errbuf)) {
7330 mesg = rb_attr_get(errbuf, idMesg);
7331 if (char_at_end(p, mesg, '\n') != '\n')
7332 rb_str_cat_cstr(mesg, "\n");
7333 }
7334 else {
7335 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7336 }
7337 if (!errbuf && rb_stderr_tty_p()) {
7338#define CSI_BEGIN "\033["
7339#define CSI_SGR "m"
7340 rb_str_catf(mesg,
7341 CSI_BEGIN""CSI_SGR"%s" /* pre */
7342 CSI_BEGIN"1"CSI_SGR"%.*s"
7343 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7344 CSI_BEGIN";1"CSI_SGR"%.*s"
7345 CSI_BEGIN""CSI_SGR"%s" /* post */
7346 "\n",
7347 pre,
7348 (int)(pb - ptr), ptr,
7349 (int)(pt - pb), pb,
7350 (int)(ptr_end - pt), pt,
7351 post);
7352 }
7353 else {
7354 char *p2;
7355
7356 len = ptr_end - ptr;
7357 lim = pt < pend ? pt : pend;
7358 i = (int)(lim - ptr);
7359 buf = ALLOCA_N(char, i+2);
7360 code = ptr;
7361 caret = p2 = buf;
7362 if (ptr <= pb) {
7363 while (ptr < pb) {
7364 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7365 }
7366 *p2++ = '^';
7367 ptr++;
7368 }
7369 if (lim > ptr) {
7370 memset(p2, '~', (lim - ptr));
7371 p2 += (lim - ptr);
7372 }
7373 *p2 = '\0';
7374 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7375 pre, (int)len, code, post,
7376 pre, caret);
7377 }
7378 if (!errbuf) rb_write_error_str(mesg);
7379}
7380#else
7381
7382static int
7383parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7384{
7385 const char *pcur = 0, *ptok = 0;
7386 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7387 p->ruby_sourceline == yylloc->end_pos.lineno) {
7388 pcur = p->lex.pcur;
7389 ptok = p->lex.ptok;
7390 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7391 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7392 }
7393 parser_yyerror0(p, msg);
7394 if (pcur) {
7395 p->lex.ptok = ptok;
7396 p->lex.pcur = pcur;
7397 }
7398 return 0;
7399}
7400
7401static int
7402parser_yyerror0(struct parser_params *p, const char *msg)
7403{
7404 dispatch1(parse_error, STR_NEW2(msg));
7405 ripper_error(p);
7406 return 0;
7407}
7408
7409static inline void
7410parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7411{
7412}
7413#endif /* !RIPPER */
7414
7415static int
7416vtable_size(const struct vtable *tbl)
7417{
7418 if (!DVARS_TERMINAL_P(tbl)) {
7419 return tbl->pos;
7420 }
7421 else {
7422 return 0;
7423 }
7424}
7425
7426static struct vtable *
7427vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7428{
7429 struct vtable *tbl = ALLOC(struct vtable);
7430 tbl->pos = 0;
7431 tbl->capa = 8;
7432 tbl->tbl = ALLOC_N(ID, tbl->capa);
7433 tbl->prev = prev;
7434#ifndef RIPPER
7435 if (p->debug) {
7436 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7437 }
7438#endif
7439 return tbl;
7440}
7441#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7442
7443static void
7444vtable_free_gen(struct parser_params *p, int line, const char *name,
7445 struct vtable *tbl)
7446{
7447#ifndef RIPPER
7448 if (p->debug) {
7449 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7450 }
7451#endif
7452 if (!DVARS_TERMINAL_P(tbl)) {
7453 if (tbl->tbl) {
7454 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7455 }
7456 ruby_sized_xfree(tbl, sizeof(*tbl));
7457 }
7458}
7459#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7460
7461static void
7462vtable_add_gen(struct parser_params *p, int line, const char *name,
7463 struct vtable *tbl, ID id)
7464{
7465#ifndef RIPPER
7466 if (p->debug) {
7467 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7468 line, name, (void *)tbl, rb_id2name(id));
7469 }
7470#endif
7471 if (DVARS_TERMINAL_P(tbl)) {
7472 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7473 return;
7474 }
7475 if (tbl->pos == tbl->capa) {
7476 tbl->capa = tbl->capa * 2;
7477 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7478 }
7479 tbl->tbl[tbl->pos++] = id;
7480}
7481#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7482
7483static void
7484vtable_pop_gen(struct parser_params *p, int line, const char *name,
7485 struct vtable *tbl, int n)
7486{
7487 if (p->debug) {
7488 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7489 line, name, (void *)tbl, n);
7490 }
7491 if (tbl->pos < n) {
7492 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7493 return;
7494 }
7495 tbl->pos -= n;
7496}
7497#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7498
7499static int
7500vtable_included(const struct vtable * tbl, ID id)
7501{
7502 int i;
7503
7504 if (!DVARS_TERMINAL_P(tbl)) {
7505 for (i = 0; i < tbl->pos; i++) {
7506 if (tbl->tbl[i] == id) {
7507 return i+1;
7508 }
7509 }
7510 }
7511 return 0;
7512}
7513
7514static void parser_prepare(struct parser_params *p);
7515
7516static int
7517e_option_supplied(struct parser_params *p)
7518{
7519 return strcmp(p->ruby_sourcefile, "-e") == 0;
7520}
7521
7522#ifndef RIPPER
7523static NODE *parser_append_options(struct parser_params *p, NODE *node);
7524
7525static VALUE
7526yycompile0(VALUE arg)
7527{
7528 int n;
7529 NODE *tree;
7530 struct parser_params *p = (struct parser_params *)arg;
7531 int cov = FALSE;
7532
7533 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7534 cov = TRUE;
7535 }
7536
7537 if (p->debug_lines) {
7538 p->ast->body.script_lines = p->debug_lines;
7539 }
7540
7541 parser_prepare(p);
7542#define RUBY_DTRACE_PARSE_HOOK(name) \
7543 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7544 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7545 }
7546 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7547 n = yyparse(p);
7548 RUBY_DTRACE_PARSE_HOOK(END);
7549
7550 p->debug_lines = 0;
7551
7552 xfree(p->lex.strterm);
7553 p->lex.strterm = 0;
7554 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7555 if (n || p->error_p) {
7556 VALUE mesg = p->error_buffer;
7557 if (!mesg) {
7558 mesg = syntax_error_new();
7559 }
7560 if (!p->error_tolerant) {
7561 rb_set_errinfo(mesg);
7562 return FALSE;
7563 }
7564 }
7565 tree = p->eval_tree;
7566 if (!tree) {
7567 tree = NEW_NIL(&NULL_LOC);
7568 }
7569 else {
7570 rb_parser_ary_t *tokens = p->tokens;
7571 NODE *prelude;
7572 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7573 prelude = block_append(p, p->eval_tree_begin, body);
7574 RNODE_SCOPE(tree)->nd_body = prelude;
7575 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7576 p->ast->body.coverage_enabled = cov;
7577 if (p->keep_tokens) {
7578 p->ast->node_buffer->tokens = tokens;
7579 p->tokens = NULL;
7580 }
7581 }
7582 p->ast->body.root = tree;
7583 p->ast->body.line_count = p->line_count;
7584 return TRUE;
7585}
7586
7587static rb_ast_t *
7588yycompile(struct parser_params *p, VALUE fname, int line)
7589{
7590 rb_ast_t *ast;
7591 if (NIL_P(fname)) {
7592 p->ruby_sourcefile_string = Qnil;
7593 p->ruby_sourcefile = "(none)";
7594 }
7595 else {
7596 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7597 p->ruby_sourcefile = StringValueCStr(fname);
7598 }
7599 p->ruby_sourceline = line - 1;
7600
7601 p->lvtbl = NULL;
7602
7603 p->ast = ast = rb_ast_new();
7604 compile_callback(yycompile0, (VALUE)p);
7605 p->ast = 0;
7606
7607 while (p->lvtbl) {
7608 local_pop(p);
7609 }
7610
7611 return ast;
7612}
7613#endif /* !RIPPER */
7614
7615static rb_encoding *
7616must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7617{
7618 rb_encoding *enc = rb_parser_str_get_encoding(s);
7619 if (!rb_enc_asciicompat(enc)) {
7620 rb_raise(rb_eArgError, "invalid source encoding");
7621 }
7622 return enc;
7623}
7624
7625static rb_parser_string_t *
7626lex_getline(struct parser_params *p)
7627{
7628 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7629 if (!line) return 0;
7630 p->line_count++;
7631 string_buffer_append(p, line);
7632 must_be_ascii_compatible(p, line);
7633 return line;
7634}
7635
7636#ifndef RIPPER
7637rb_ast_t*
7638rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7639{
7640 p->lex.gets = gets;
7641 p->lex.input = input;
7642 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7643
7644 return yycompile(p, fname, line);
7645}
7646#endif /* !RIPPER */
7647
7648#define STR_FUNC_ESCAPE 0x01
7649#define STR_FUNC_EXPAND 0x02
7650#define STR_FUNC_REGEXP 0x04
7651#define STR_FUNC_QWORDS 0x08
7652#define STR_FUNC_SYMBOL 0x10
7653#define STR_FUNC_INDENT 0x20
7654#define STR_FUNC_LABEL 0x40
7655#define STR_FUNC_LIST 0x4000
7656#define STR_FUNC_TERM 0x8000
7657
7658enum string_type {
7659 str_label = STR_FUNC_LABEL,
7660 str_squote = (0),
7661 str_dquote = (STR_FUNC_EXPAND),
7662 str_xquote = (STR_FUNC_EXPAND),
7663 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7664 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7665 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7666 str_ssym = (STR_FUNC_SYMBOL),
7667 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7668};
7669
7670static rb_parser_string_t *
7671parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7672{
7673 rb_parser_string_t *pstr;
7674
7675 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7676
7677 if (!(func & STR_FUNC_REGEXP)) {
7678 if (rb_parser_is_ascii_string(p, pstr)) {
7679 }
7680 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7681 /* everything is valid in ASCII-8BIT */
7682 enc = rb_ascii8bit_encoding();
7683 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7684 }
7685 }
7686
7687 return pstr;
7688}
7689
7690static int
7691strterm_is_heredoc(rb_strterm_t *strterm)
7692{
7693 return strterm->heredoc;
7694}
7695
7696static rb_strterm_t *
7697new_strterm(struct parser_params *p, int func, int term, int paren)
7698{
7699 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7700 strterm->u.literal.func = func;
7701 strterm->u.literal.term = term;
7702 strterm->u.literal.paren = paren;
7703 return strterm;
7704}
7705
7706static rb_strterm_t *
7707new_heredoc(struct parser_params *p)
7708{
7709 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7710 strterm->heredoc = true;
7711 return strterm;
7712}
7713
7714#define peek(p,c) peek_n(p, (c), 0)
7715#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7716#define peekc(p) peekc_n(p, 0)
7717#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7718
7719#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7720static void
7721parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7722{
7723 debug_token_line(p, "add_delayed_token", line);
7724
7725 if (tok < end) {
7726 if (has_delayed_token(p)) {
7727 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7728 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7729 int end_col = (next_line ? 0 : p->delayed.end_col);
7730 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7731 dispatch_delayed_token(p, tSTRING_CONTENT);
7732 }
7733 }
7734 if (!has_delayed_token(p)) {
7735 p->delayed.token = rb_parser_string_new(p, 0, 0);
7736 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7737 p->delayed.beg_line = p->ruby_sourceline;
7738 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7739 }
7740 parser_str_cat(p->delayed.token, tok, end - tok);
7741 p->delayed.end_line = p->ruby_sourceline;
7742 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7743 p->lex.ptok = end;
7744 }
7745}
7746
7747static void
7748set_lastline(struct parser_params *p, rb_parser_string_t *str)
7749{
7750 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7751 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7752 p->lex.lastline = str;
7753}
7754
7755static int
7756nextline(struct parser_params *p, int set_encoding)
7757{
7758 rb_parser_string_t *str = p->lex.nextline;
7759 p->lex.nextline = 0;
7760 if (!str) {
7761 if (p->eofp)
7762 return -1;
7763
7764 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7765 goto end_of_input;
7766 }
7767
7768 if (!p->lex.input || !(str = lex_getline(p))) {
7769 end_of_input:
7770 p->eofp = 1;
7771 lex_goto_eol(p);
7772 return -1;
7773 }
7774#ifndef RIPPER
7775 if (p->debug_lines) {
7776 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7777 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7778 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7779 }
7780#endif
7781 p->cr_seen = FALSE;
7782 }
7783 else if (str == AFTER_HEREDOC_WITHOUT_TERMINTOR) {
7784 /* after here-document without terminator */
7785 goto end_of_input;
7786 }
7787 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7788 if (p->heredoc_end > 0) {
7789 p->ruby_sourceline = p->heredoc_end;
7790 p->heredoc_end = 0;
7791 }
7792 p->ruby_sourceline++;
7793 set_lastline(p, str);
7794 token_flush(p);
7795 return 0;
7796}
7797
7798static int
7799parser_cr(struct parser_params *p, int c)
7800{
7801 if (peek(p, '\n')) {
7802 p->lex.pcur++;
7803 c = '\n';
7804 }
7805 return c;
7806}
7807
7808static inline int
7809nextc0(struct parser_params *p, int set_encoding)
7810{
7811 int c;
7812
7813 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINTOR)) {
7814 if (nextline(p, set_encoding)) return -1;
7815 }
7816 c = (unsigned char)*p->lex.pcur++;
7817 if (UNLIKELY(c == '\r')) {
7818 c = parser_cr(p, c);
7819 }
7820
7821 return c;
7822}
7823#define nextc(p) nextc0(p, TRUE)
7824
7825static void
7826pushback(struct parser_params *p, int c)
7827{
7828 if (c == -1) return;
7829 p->eofp = 0;
7830 p->lex.pcur--;
7831 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7832 p->lex.pcur--;
7833 }
7834}
7835
7836#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7837
7838#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7839#define tok(p) (p)->tokenbuf
7840#define toklen(p) (p)->tokidx
7841
7842static int
7843looking_at_eol_p(struct parser_params *p)
7844{
7845 const char *ptr = p->lex.pcur;
7846 while (!lex_eol_ptr_p(p, ptr)) {
7847 int c = (unsigned char)*ptr++;
7848 int eol = (c == '\n' || c == '#');
7849 if (eol || !ISSPACE(c)) {
7850 return eol;
7851 }
7852 }
7853 return TRUE;
7854}
7855
7856static char*
7857newtok(struct parser_params *p)
7858{
7859 p->tokidx = 0;
7860 if (!p->tokenbuf) {
7861 p->toksiz = 60;
7862 p->tokenbuf = ALLOC_N(char, 60);
7863 }
7864 if (p->toksiz > 4096) {
7865 p->toksiz = 60;
7866 REALLOC_N(p->tokenbuf, char, 60);
7867 }
7868 return p->tokenbuf;
7869}
7870
7871static char *
7872tokspace(struct parser_params *p, int n)
7873{
7874 p->tokidx += n;
7875
7876 if (p->tokidx >= p->toksiz) {
7877 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7878 REALLOC_N(p->tokenbuf, char, p->toksiz);
7879 }
7880 return &p->tokenbuf[p->tokidx-n];
7881}
7882
7883static void
7884tokadd(struct parser_params *p, int c)
7885{
7886 p->tokenbuf[p->tokidx++] = (char)c;
7887 if (p->tokidx >= p->toksiz) {
7888 p->toksiz *= 2;
7889 REALLOC_N(p->tokenbuf, char, p->toksiz);
7890 }
7891}
7892
7893static int
7894tok_hex(struct parser_params *p, size_t *numlen)
7895{
7896 int c;
7897
7898 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7899 if (!*numlen) {
7900 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7901 yyerror0("invalid hex escape");
7902 dispatch_scan_event(p, tSTRING_CONTENT);
7903 return 0;
7904 }
7905 p->lex.pcur += *numlen;
7906 return c;
7907}
7908
7909#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7910
7911static int
7912escaped_control_code(int c)
7913{
7914 int c2 = 0;
7915 switch (c) {
7916 case ' ':
7917 c2 = 's';
7918 break;
7919 case '\n':
7920 c2 = 'n';
7921 break;
7922 case '\t':
7923 c2 = 't';
7924 break;
7925 case '\v':
7926 c2 = 'v';
7927 break;
7928 case '\r':
7929 c2 = 'r';
7930 break;
7931 case '\f':
7932 c2 = 'f';
7933 break;
7934 }
7935 return c2;
7936}
7937
7938#define WARN_SPACE_CHAR(c, prefix) \
7939 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7940
7941static int
7942tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7943 int regexp_literal, const char *begin)
7944{
7945 const int wide = !begin;
7946 size_t numlen;
7947 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7948
7949 p->lex.pcur += numlen;
7950 if (p->lex.strterm == NULL ||
7951 strterm_is_heredoc(p->lex.strterm) ||
7952 (p->lex.strterm->u.literal.func != str_regexp)) {
7953 if (!begin) begin = p->lex.pcur;
7954 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7955 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7956 yyerror0("invalid Unicode escape");
7957 dispatch_scan_event(p, tSTRING_CONTENT);
7958 return wide && numlen > 0;
7959 }
7960 if (codepoint > 0x10ffff) {
7961 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7962 yyerror0("invalid Unicode codepoint (too large)");
7963 dispatch_scan_event(p, tSTRING_CONTENT);
7964 return wide;
7965 }
7966 if ((codepoint & 0xfffff800) == 0xd800) {
7967 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7968 yyerror0("invalid Unicode codepoint");
7969 dispatch_scan_event(p, tSTRING_CONTENT);
7970 return wide;
7971 }
7972 }
7973 if (regexp_literal) {
7974 tokcopy(p, (int)numlen);
7975 }
7976 else if (codepoint >= 0x80) {
7977 rb_encoding *utf8 = rb_utf8_encoding();
7978 if (*encp && utf8 != *encp) {
7979 YYLTYPE loc = RUBY_INIT_YYLLOC();
7980 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7981 parser_show_error_line(p, &loc);
7982 return wide;
7983 }
7984 *encp = utf8;
7985 tokaddmbc(p, codepoint, *encp);
7986 }
7987 else {
7988 tokadd(p, codepoint);
7989 }
7990 return TRUE;
7991}
7992
7993static int tokadd_mbchar(struct parser_params *p, int c);
7994
7995static int
7996tokskip_mbchar(struct parser_params *p)
7997{
7998 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7999 if (len > 0) {
8000 p->lex.pcur += len - 1;
8001 }
8002 return len;
8003}
8004
8005/* return value is for ?\u3042 */
8006static void
8007tokadd_utf8(struct parser_params *p, rb_encoding **encp,
8008 int term, int symbol_literal, int regexp_literal)
8009{
8010 /*
8011 * If `term` is not -1, then we allow multiple codepoints in \u{}
8012 * upto `term` byte, otherwise we're parsing a character literal.
8013 * And then add the codepoints to the current token.
8014 */
8015 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
8016
8017 const int open_brace = '{', close_brace = '}';
8018
8019 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
8020
8021 if (peek(p, open_brace)) { /* handle \u{...} form */
8022 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
8023 /*
8024 * Skip parsing validation code and copy bytes as-is until term or
8025 * closing brace, in order to correctly handle extended regexps where
8026 * invalid unicode escapes are allowed in comments. The regexp parser
8027 * does its own validation and will catch any issues.
8028 */
8029 tokadd(p, open_brace);
8030 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
8031 int c = peekc(p);
8032 if (c == close_brace) {
8033 tokadd(p, c);
8034 ++p->lex.pcur;
8035 break;
8036 }
8037 else if (c == term) {
8038 break;
8039 }
8040 if (c == '\\' && !lex_eol_n_p(p, 1)) {
8041 tokadd(p, c);
8042 c = *++p->lex.pcur;
8043 }
8044 tokadd_mbchar(p, c);
8045 }
8046 }
8047 else {
8048 const char *second = NULL;
8049 int c, last = nextc(p);
8050 if (lex_eol_p(p)) goto unterminated;
8051 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
8052 while (c != close_brace) {
8053 if (c == term) goto unterminated;
8054 if (second == multiple_codepoints)
8055 second = p->lex.pcur;
8056 if (regexp_literal) tokadd(p, last);
8057 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
8058 break;
8059 }
8060 while (ISSPACE(c = peekc(p))) {
8061 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
8062 last = c;
8063 }
8064 if (term == -1 && !second)
8065 second = multiple_codepoints;
8066 }
8067
8068 if (c != close_brace) {
8069 unterminated:
8070 flush_string_content(p, rb_utf8_encoding(), 0);
8071 yyerror0("unterminated Unicode escape");
8072 dispatch_scan_event(p, tSTRING_CONTENT);
8073 return;
8074 }
8075 if (second && second != multiple_codepoints) {
8076 const char *pcur = p->lex.pcur;
8077 p->lex.pcur = second;
8078 dispatch_scan_event(p, tSTRING_CONTENT);
8079 token_flush(p);
8080 p->lex.pcur = pcur;
8081 yyerror0(multiple_codepoints);
8082 token_flush(p);
8083 }
8084
8085 if (regexp_literal) tokadd(p, close_brace);
8086 nextc(p);
8087 }
8088 }
8089 else { /* handle \uxxxx form */
8090 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
8091 token_flush(p);
8092 return;
8093 }
8094 }
8095}
8096
8097#define ESCAPE_CONTROL 1
8098#define ESCAPE_META 2
8099
8100static int
8101read_escape(struct parser_params *p, int flags, const char *begin)
8102{
8103 int c;
8104 size_t numlen;
8105
8106 switch (c = nextc(p)) {
8107 case '\\': /* Backslash */
8108 return c;
8109
8110 case 'n': /* newline */
8111 return '\n';
8112
8113 case 't': /* horizontal tab */
8114 return '\t';
8115
8116 case 'r': /* carriage-return */
8117 return '\r';
8118
8119 case 'f': /* form-feed */
8120 return '\f';
8121
8122 case 'v': /* vertical tab */
8123 return '\13';
8124
8125 case 'a': /* alarm(bell) */
8126 return '\007';
8127
8128 case 'e': /* escape */
8129 return 033;
8130
8131 case '0': case '1': case '2': case '3': /* octal constant */
8132 case '4': case '5': case '6': case '7':
8133 pushback(p, c);
8134 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8135 p->lex.pcur += numlen;
8136 return c;
8137
8138 case 'x': /* hex constant */
8139 c = tok_hex(p, &numlen);
8140 if (numlen == 0) return 0;
8141 return c;
8142
8143 case 'b': /* backspace */
8144 return '\010';
8145
8146 case 's': /* space */
8147 return ' ';
8148
8149 case 'M':
8150 if (flags & ESCAPE_META) goto eof;
8151 if ((c = nextc(p)) != '-') {
8152 goto eof;
8153 }
8154 if ((c = nextc(p)) == '\\') {
8155 switch (peekc(p)) {
8156 case 'u': case 'U':
8157 nextc(p);
8158 goto eof;
8159 }
8160 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8161 }
8162 else if (c == -1) goto eof;
8163 else if (!ISASCII(c)) {
8164 tokskip_mbchar(p);
8165 goto eof;
8166 }
8167 else {
8168 int c2 = escaped_control_code(c);
8169 if (c2) {
8170 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8171 WARN_SPACE_CHAR(c2, "\\M-");
8172 }
8173 else {
8174 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8175 }
8176 }
8177 else if (ISCNTRL(c)) goto eof;
8178 return ((c & 0xff) | 0x80);
8179 }
8180
8181 case 'C':
8182 if ((c = nextc(p)) != '-') {
8183 goto eof;
8184 }
8185 case 'c':
8186 if (flags & ESCAPE_CONTROL) goto eof;
8187 if ((c = nextc(p))== '\\') {
8188 switch (peekc(p)) {
8189 case 'u': case 'U':
8190 nextc(p);
8191 goto eof;
8192 }
8193 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8194 }
8195 else if (c == '?')
8196 return 0177;
8197 else if (c == -1) goto eof;
8198 else if (!ISASCII(c)) {
8199 tokskip_mbchar(p);
8200 goto eof;
8201 }
8202 else {
8203 int c2 = escaped_control_code(c);
8204 if (c2) {
8205 if (ISCNTRL(c)) {
8206 if (flags & ESCAPE_META) {
8207 WARN_SPACE_CHAR(c2, "\\M-");
8208 }
8209 else {
8210 WARN_SPACE_CHAR(c2, "");
8211 }
8212 }
8213 else {
8214 if (flags & ESCAPE_META) {
8215 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8216 }
8217 else {
8218 WARN_SPACE_CHAR(c2, "\\C-");
8219 }
8220 }
8221 }
8222 else if (ISCNTRL(c)) goto eof;
8223 }
8224 return c & 0x9f;
8225
8226 eof:
8227 case -1:
8228 flush_string_content(p, p->enc, p->lex.pcur - begin);
8229 yyerror0("Invalid escape character syntax");
8230 dispatch_scan_event(p, tSTRING_CONTENT);
8231 return '\0';
8232
8233 default:
8234 return c;
8235 }
8236}
8237
8238static void
8239tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8240{
8241 int len = rb_enc_codelen(c, enc);
8242 rb_enc_mbcput(c, tokspace(p, len), enc);
8243}
8244
8245static int
8246tokadd_escape(struct parser_params *p)
8247{
8248 int c;
8249 size_t numlen;
8250 const char *begin = p->lex.pcur;
8251
8252 switch (c = nextc(p)) {
8253 case '\n':
8254 return 0; /* just ignore */
8255
8256 case '0': case '1': case '2': case '3': /* octal constant */
8257 case '4': case '5': case '6': case '7':
8258 {
8259 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8260 if (numlen == 0) goto eof;
8261 p->lex.pcur += numlen;
8262 tokcopy(p, (int)numlen + 1);
8263 }
8264 return 0;
8265
8266 case 'x': /* hex constant */
8267 {
8268 tok_hex(p, &numlen);
8269 if (numlen == 0) return -1;
8270 tokcopy(p, (int)numlen + 2);
8271 }
8272 return 0;
8273
8274 eof:
8275 case -1:
8276 flush_string_content(p, p->enc, p->lex.pcur - begin);
8277 yyerror0("Invalid escape character syntax");
8278 token_flush(p);
8279 return -1;
8280
8281 default:
8282 tokadd(p, '\\');
8283 tokadd(p, c);
8284 }
8285 return 0;
8286}
8287
8288static int
8289char_to_option(int c)
8290{
8291 int val;
8292
8293 switch (c) {
8294 case 'i':
8295 val = RE_ONIG_OPTION_IGNORECASE;
8296 break;
8297 case 'x':
8298 val = RE_ONIG_OPTION_EXTEND;
8299 break;
8300 case 'm':
8301 val = RE_ONIG_OPTION_MULTILINE;
8302 break;
8303 default:
8304 val = 0;
8305 break;
8306 }
8307 return val;
8308}
8309
8310#define ARG_ENCODING_FIXED 16
8311#define ARG_ENCODING_NONE 32
8312#define ENC_ASCII8BIT 1
8313#define ENC_EUC_JP 2
8314#define ENC_Windows_31J 3
8315#define ENC_UTF8 4
8316
8317static int
8318char_to_option_kcode(int c, int *option, int *kcode)
8319{
8320 *option = 0;
8321
8322 switch (c) {
8323 case 'n':
8324 *kcode = ENC_ASCII8BIT;
8325 return (*option = ARG_ENCODING_NONE);
8326 case 'e':
8327 *kcode = ENC_EUC_JP;
8328 break;
8329 case 's':
8330 *kcode = ENC_Windows_31J;
8331 break;
8332 case 'u':
8333 *kcode = ENC_UTF8;
8334 break;
8335 default:
8336 *kcode = -1;
8337 return (*option = char_to_option(c));
8338 }
8339 *option = ARG_ENCODING_FIXED;
8340 return 1;
8341}
8342
8343static int
8344regx_options(struct parser_params *p)
8345{
8346 int kcode = 0;
8347 int kopt = 0;
8348 int options = 0;
8349 int c, opt, kc;
8350
8351 newtok(p);
8352 while (c = nextc(p), ISALPHA(c)) {
8353 if (c == 'o') {
8354 options |= RE_OPTION_ONCE;
8355 }
8356 else if (char_to_option_kcode(c, &opt, &kc)) {
8357 if (kc >= 0) {
8358 if (kc != ENC_ASCII8BIT) kcode = c;
8359 kopt = opt;
8360 }
8361 else {
8362 options |= opt;
8363 }
8364 }
8365 else {
8366 tokadd(p, c);
8367 }
8368 }
8369 options |= kopt;
8370 pushback(p, c);
8371 if (toklen(p)) {
8372 YYLTYPE loc = RUBY_INIT_YYLLOC();
8373 tokfix(p);
8374 compile_error(p, "unknown regexp option%s - %*s",
8375 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8376 parser_show_error_line(p, &loc);
8377 }
8378 return options | RE_OPTION_ENCODING(kcode);
8379}
8380
8381static int
8382tokadd_mbchar(struct parser_params *p, int c)
8383{
8384 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8385 if (len < 0) return -1;
8386 tokadd(p, c);
8387 p->lex.pcur += --len;
8388 if (len > 0) tokcopy(p, len);
8389 return c;
8390}
8391
8392static inline int
8393simple_re_meta(int c)
8394{
8395 switch (c) {
8396 case '$': case '*': case '+': case '.':
8397 case '?': case '^': case '|':
8398 case ')': case ']': case '}': case '>':
8399 return TRUE;
8400 default:
8401 return FALSE;
8402 }
8403}
8404
8405static int
8406parser_update_heredoc_indent(struct parser_params *p, int c)
8407{
8408 if (p->heredoc_line_indent == -1) {
8409 if (c == '\n') p->heredoc_line_indent = 0;
8410 }
8411 else {
8412 if (c == ' ') {
8413 p->heredoc_line_indent++;
8414 return TRUE;
8415 }
8416 else if (c == '\t') {
8417 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8418 p->heredoc_line_indent = w * TAB_WIDTH;
8419 return TRUE;
8420 }
8421 else if (c != '\n') {
8422 if (p->heredoc_indent > p->heredoc_line_indent) {
8423 p->heredoc_indent = p->heredoc_line_indent;
8424 }
8425 p->heredoc_line_indent = -1;
8426 }
8427 else {
8428 /* Whitespace only line has no indentation */
8429 p->heredoc_line_indent = 0;
8430 }
8431 }
8432 return FALSE;
8433}
8434
8435static void
8436parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8437{
8438 YYLTYPE loc = RUBY_INIT_YYLLOC();
8439 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8440 compile_error(p, "%s mixed within %s source", n1, n2);
8441 parser_show_error_line(p, &loc);
8442}
8443
8444static void
8445parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8446{
8447 const char *pos = p->lex.pcur;
8448 p->lex.pcur = beg;
8449 parser_mixed_error(p, enc1, enc2);
8450 p->lex.pcur = pos;
8451}
8452
8453static inline char
8454nibble_char_upper(unsigned int c)
8455{
8456 c &= 0xf;
8457 return c + (c < 10 ? '0' : 'A' - 10);
8458}
8459
8460static int
8461tokadd_string(struct parser_params *p,
8462 int func, int term, int paren, long *nest,
8463 rb_encoding **encp, rb_encoding **enc)
8464{
8465 int c;
8466 bool erred = false;
8467#ifdef RIPPER
8468 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8469 int top_of_line = FALSE;
8470#endif
8471
8472#define mixed_error(enc1, enc2) \
8473 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8474#define mixed_escape(beg, enc1, enc2) \
8475 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8476
8477 while ((c = nextc(p)) != -1) {
8478 if (p->heredoc_indent > 0) {
8479 parser_update_heredoc_indent(p, c);
8480 }
8481#ifdef RIPPER
8482 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8483 pushback(p, c);
8484 break;
8485 }
8486#endif
8487
8488 if (paren && c == paren) {
8489 ++*nest;
8490 }
8491 else if (c == term) {
8492 if (!nest || !*nest) {
8493 pushback(p, c);
8494 break;
8495 }
8496 --*nest;
8497 }
8498 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8499 unsigned char c2 = *p->lex.pcur;
8500 if (c2 == '$' || c2 == '@' || c2 == '{') {
8501 pushback(p, c);
8502 break;
8503 }
8504 }
8505 else if (c == '\\') {
8506 c = nextc(p);
8507 switch (c) {
8508 case '\n':
8509 if (func & STR_FUNC_QWORDS) break;
8510 if (func & STR_FUNC_EXPAND) {
8511 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8512 continue;
8513 if (c == term) {
8514 c = '\\';
8515 goto terminate;
8516 }
8517 }
8518 tokadd(p, '\\');
8519 break;
8520
8521 case '\\':
8522 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8523 break;
8524
8525 case 'u':
8526 if ((func & STR_FUNC_EXPAND) == 0) {
8527 tokadd(p, '\\');
8528 break;
8529 }
8530 tokadd_utf8(p, enc, term,
8531 func & STR_FUNC_SYMBOL,
8532 func & STR_FUNC_REGEXP);
8533 continue;
8534
8535 default:
8536 if (c == -1) return -1;
8537 if (!ISASCII(c)) {
8538 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8539 goto non_ascii;
8540 }
8541 if (func & STR_FUNC_REGEXP) {
8542 switch (c) {
8543 case 'c':
8544 case 'C':
8545 case 'M': {
8546 pushback(p, c);
8547 c = read_escape(p, 0, p->lex.pcur - 1);
8548
8549 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8550 *t++ = '\\';
8551 *t++ = 'x';
8552 *t++ = nibble_char_upper(c >> 4);
8553 *t++ = nibble_char_upper(c);
8554 continue;
8555 }
8556 }
8557
8558 if (c == term && !simple_re_meta(c)) {
8559 tokadd(p, c);
8560 continue;
8561 }
8562 pushback(p, c);
8563 if ((c = tokadd_escape(p)) < 0)
8564 return -1;
8565 if (*enc && *enc != *encp) {
8566 mixed_escape(p->lex.ptok+2, *enc, *encp);
8567 }
8568 continue;
8569 }
8570 else if (func & STR_FUNC_EXPAND) {
8571 pushback(p, c);
8572 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8573 c = read_escape(p, 0, p->lex.pcur - 1);
8574 }
8575 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8576 /* ignore backslashed spaces in %w */
8577 }
8578 else if (c != term && !(paren && c == paren)) {
8579 tokadd(p, '\\');
8580 pushback(p, c);
8581 continue;
8582 }
8583 }
8584 }
8585 else if (!parser_isascii(p)) {
8586 non_ascii:
8587 if (!*enc) {
8588 *enc = *encp;
8589 }
8590 else if (*enc != *encp) {
8591 mixed_error(*enc, *encp);
8592 continue;
8593 }
8594 if (tokadd_mbchar(p, c) == -1) return -1;
8595 continue;
8596 }
8597 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8598 pushback(p, c);
8599 break;
8600 }
8601 if (c & 0x80) {
8602 if (!*enc) {
8603 *enc = *encp;
8604 }
8605 else if (*enc != *encp) {
8606 mixed_error(*enc, *encp);
8607 continue;
8608 }
8609 }
8610 tokadd(p, c);
8611#ifdef RIPPER
8612 top_of_line = (c == '\n');
8613#endif
8614 }
8615 terminate:
8616 if (*enc) *encp = *enc;
8617 return c;
8618}
8619
8620#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8621
8622static void
8623flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8624{
8625 p->lex.pcur -= back;
8626 if (has_delayed_token(p)) {
8627 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8628 if (len > 0) {
8629 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8630 p->delayed.end_line = p->ruby_sourceline;
8631 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8632 }
8633 dispatch_delayed_token(p, tSTRING_CONTENT);
8634 p->lex.ptok = p->lex.pcur;
8635 }
8636 dispatch_scan_event(p, tSTRING_CONTENT);
8637 p->lex.pcur += back;
8638}
8639
8640/* this can be shared with ripper, since it's independent from struct
8641 * parser_params. */
8642#ifndef RIPPER
8643#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8644#define SPECIAL_PUNCT(idx) ( \
8645 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8646 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8647 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8648 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8649 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8650 BIT('0', idx))
8651const uint_least32_t ruby_global_name_punct_bits[] = {
8652 SPECIAL_PUNCT(0),
8653 SPECIAL_PUNCT(1),
8654 SPECIAL_PUNCT(2),
8655};
8656#undef BIT
8657#undef SPECIAL_PUNCT
8658#endif
8659
8660static enum yytokentype
8661parser_peek_variable_name(struct parser_params *p)
8662{
8663 int c;
8664 const char *ptr = p->lex.pcur;
8665
8666 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8667 c = *ptr++;
8668 switch (c) {
8669 case '$':
8670 if ((c = *ptr) == '-') {
8671 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8672 c = *ptr;
8673 }
8674 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8675 return tSTRING_DVAR;
8676 }
8677 break;
8678 case '@':
8679 if ((c = *ptr) == '@') {
8680 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8681 c = *ptr;
8682 }
8683 break;
8684 case '{':
8685 p->lex.pcur = ptr;
8686 p->command_start = TRUE;
8687 yylval.state = p->lex.state;
8688 return tSTRING_DBEG;
8689 default:
8690 return 0;
8691 }
8692 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8693 return tSTRING_DVAR;
8694 return 0;
8695}
8696
8697#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8698#define IS_END() IS_lex_state(EXPR_END_ANY)
8699#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8700#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8701#define IS_LABEL_POSSIBLE() (\
8702 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8703 IS_ARG())
8704#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8705#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8706
8707static inline enum yytokentype
8708parser_string_term(struct parser_params *p, int func)
8709{
8710 xfree(p->lex.strterm);
8711 p->lex.strterm = 0;
8712 if (func & STR_FUNC_REGEXP) {
8713 set_yylval_num(regx_options(p));
8714 dispatch_scan_event(p, tREGEXP_END);
8715 SET_LEX_STATE(EXPR_END);
8716 return tREGEXP_END;
8717 }
8718 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8719 nextc(p);
8720 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8721 return tLABEL_END;
8722 }
8723 SET_LEX_STATE(EXPR_END);
8724 return tSTRING_END;
8725}
8726
8727static enum yytokentype
8728parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8729{
8730 int func = quote->func;
8731 int term = quote->term;
8732 int paren = quote->paren;
8733 int c, space = 0;
8734 rb_encoding *enc = p->enc;
8735 rb_encoding *base_enc = 0;
8736 rb_parser_string_t *lit;
8737
8738 if (func & STR_FUNC_TERM) {
8739 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8740 SET_LEX_STATE(EXPR_END);
8741 xfree(p->lex.strterm);
8742 p->lex.strterm = 0;
8743 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8744 }
8745 c = nextc(p);
8746 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8747 while (c != '\n' && ISSPACE(c = nextc(p)));
8748 space = 1;
8749 }
8750 if (func & STR_FUNC_LIST) {
8751 quote->func &= ~STR_FUNC_LIST;
8752 space = 1;
8753 }
8754 if (c == term && !quote->nest) {
8755 if (func & STR_FUNC_QWORDS) {
8756 quote->func |= STR_FUNC_TERM;
8757 pushback(p, c); /* dispatch the term at tSTRING_END */
8758 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8759 return ' ';
8760 }
8761 return parser_string_term(p, func);
8762 }
8763 if (space) {
8764 if (!ISSPACE(c)) pushback(p, c);
8765 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8766 return ' ';
8767 }
8768 newtok(p);
8769 if ((func & STR_FUNC_EXPAND) && c == '#') {
8770 enum yytokentype t = parser_peek_variable_name(p);
8771 if (t) return t;
8772 tokadd(p, '#');
8773 c = nextc(p);
8774 }
8775 pushback(p, c);
8776 if (tokadd_string(p, func, term, paren, &quote->nest,
8777 &enc, &base_enc) == -1) {
8778 if (p->eofp) {
8779#ifndef RIPPER
8780# define unterminated_literal(mesg) yyerror0(mesg)
8781#else
8782# define unterminated_literal(mesg) compile_error(p, mesg)
8783#endif
8784 literal_flush(p, p->lex.pcur);
8785 if (func & STR_FUNC_QWORDS) {
8786 /* no content to add, bailing out here */
8787 unterminated_literal("unterminated list meets end of file");
8788 xfree(p->lex.strterm);
8789 p->lex.strterm = 0;
8790 return tSTRING_END;
8791 }
8792 if (func & STR_FUNC_REGEXP) {
8793 unterminated_literal("unterminated regexp meets end of file");
8794 }
8795 else {
8796 unterminated_literal("unterminated string meets end of file");
8797 }
8798 quote->func |= STR_FUNC_TERM;
8799 }
8800 }
8801
8802 tokfix(p);
8803 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8804 set_yylval_str(lit);
8805 flush_string_content(p, enc, 0);
8806
8807 return tSTRING_CONTENT;
8808}
8809
8810static enum yytokentype
8811heredoc_identifier(struct parser_params *p)
8812{
8813 /*
8814 * term_len is length of `<<"END"` except `END`,
8815 * in this case term_len is 4 (<, <, " and ").
8816 */
8817 long len, offset = p->lex.pcur - p->lex.pbeg;
8818 int c = nextc(p), term, func = 0, quote = 0;
8819 enum yytokentype token = tSTRING_BEG;
8820 int indent = 0;
8821
8822 if (c == '-') {
8823 c = nextc(p);
8824 func = STR_FUNC_INDENT;
8825 offset++;
8826 }
8827 else if (c == '~') {
8828 c = nextc(p);
8829 func = STR_FUNC_INDENT;
8830 offset++;
8831 indent = INT_MAX;
8832 }
8833 switch (c) {
8834 case '\'':
8835 func |= str_squote; goto quoted;
8836 case '"':
8837 func |= str_dquote; goto quoted;
8838 case '`':
8839 token = tXSTRING_BEG;
8840 func |= str_xquote; goto quoted;
8841
8842 quoted:
8843 quote++;
8844 offset++;
8845 term = c;
8846 len = 0;
8847 while ((c = nextc(p)) != term) {
8848 if (c == -1 || c == '\r' || c == '\n') {
8849 yyerror0("unterminated here document identifier");
8850 return -1;
8851 }
8852 }
8853 break;
8854
8855 default:
8856 if (!parser_is_identchar(p)) {
8857 pushback(p, c);
8858 if (func & STR_FUNC_INDENT) {
8859 pushback(p, indent > 0 ? '~' : '-');
8860 }
8861 return 0;
8862 }
8863 func |= str_dquote;
8864 do {
8865 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8866 if (n < 0) return 0;
8867 p->lex.pcur += --n;
8868 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8869 pushback(p, c);
8870 break;
8871 }
8872
8873 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8874 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8875 yyerror0("too long here document identifier");
8876 dispatch_scan_event(p, tHEREDOC_BEG);
8877 lex_goto_eol(p);
8878
8879 p->lex.strterm = new_heredoc(p);
8880 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8881 here->offset = offset;
8882 here->sourceline = p->ruby_sourceline;
8883 here->length = (unsigned)len;
8884 here->quote = quote;
8885 here->func = func;
8886 here->lastline = p->lex.lastline;
8887
8888 token_flush(p);
8889 p->heredoc_indent = indent;
8890 p->heredoc_line_indent = 0;
8891 return token;
8892}
8893
8894static void
8895heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8896{
8897 rb_parser_string_t *line;
8898 rb_strterm_t *term = p->lex.strterm;
8899
8900 p->lex.strterm = 0;
8901 line = here->lastline;
8902 p->lex.lastline = line;
8903 p->lex.pbeg = PARSER_STRING_PTR(line);
8904 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8905 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8906 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8907 p->heredoc_end = p->ruby_sourceline;
8908 p->ruby_sourceline = (int)here->sourceline;
8909 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINTOR;
8910 p->eofp = 0;
8911 xfree(term);
8912}
8913
8914static int
8915dedent_string_column(const char *str, long len, int width)
8916{
8917 int i, col = 0;
8918
8919 for (i = 0; i < len && col < width; i++) {
8920 if (str[i] == ' ') {
8921 col++;
8922 }
8923 else if (str[i] == '\t') {
8924 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8925 if (n > width) break;
8926 col = n;
8927 }
8928 else {
8929 break;
8930 }
8931 }
8932
8933 return i;
8934}
8935
8936static int
8937dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8938{
8939 char *str;
8940 long len;
8941 int i;
8942
8943 len = PARSER_STRING_LEN(string);
8944 str = PARSER_STRING_PTR(string);
8945
8946 i = dedent_string_column(str, len, width);
8947 if (!i) return 0;
8948
8949 rb_parser_str_modify(string);
8950 str = PARSER_STRING_PTR(string);
8951 if (PARSER_STRING_LEN(string) != len)
8952 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8953 MEMMOVE(str, str + i, char, len - i);
8954 rb_parser_str_set_len(p, string, len - i);
8955 return i;
8956}
8957
8958static NODE *
8959heredoc_dedent(struct parser_params *p, NODE *root)
8960{
8961 NODE *node, *str_node, *prev_node;
8962 int indent = p->heredoc_indent;
8963 rb_parser_string_t *prev_lit = 0;
8964
8965 if (indent <= 0) return root;
8966 if (!root) return root;
8967
8968 prev_node = node = str_node = root;
8969 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8970
8971 while (str_node) {
8972 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8973 if (nd_fl_newline(str_node)) {
8974 dedent_string(p, lit, indent);
8975 }
8976 if (!prev_lit) {
8977 prev_lit = lit;
8978 }
8979 else if (!literal_concat0(p, prev_lit, lit)) {
8980 return 0;
8981 }
8982 else {
8983 NODE *end = RNODE_LIST(node)->as.nd_end;
8984 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8985 if (!node) {
8986 if (nd_type_p(prev_node, NODE_DSTR))
8987 nd_set_type(prev_node, NODE_STR);
8988 break;
8989 }
8990 RNODE_LIST(node)->as.nd_end = end;
8991 goto next_str;
8992 }
8993
8994 str_node = 0;
8995 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8996 next_str:
8997 if (!nd_type_p(node, NODE_LIST)) break;
8998 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8999 enum node_type type = nd_type(str_node);
9000 if (type == NODE_STR || type == NODE_DSTR) break;
9001 prev_lit = 0;
9002 str_node = 0;
9003 }
9004 }
9005 }
9006 return root;
9007}
9008
9009static int
9010whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
9011{
9012 const char *beg = p->lex.pbeg;
9013 const char *ptr = p->lex.pend;
9014
9015 if (ptr - beg < len) return FALSE;
9016 if (ptr > beg && ptr[-1] == '\n') {
9017 if (--ptr > beg && ptr[-1] == '\r') --ptr;
9018 if (ptr - beg < len) return FALSE;
9019 }
9020 if (strncmp(eos, ptr -= len, len)) return FALSE;
9021 if (indent) {
9022 while (beg < ptr && ISSPACE(*beg)) beg++;
9023 }
9024 return beg == ptr;
9025}
9026
9027static int
9028word_match_p(struct parser_params *p, const char *word, long len)
9029{
9030 if (strncmp(p->lex.pcur, word, len)) return 0;
9031 if (lex_eol_n_p(p, len)) return 1;
9032 int c = (unsigned char)p->lex.pcur[len];
9033 if (ISSPACE(c)) return 1;
9034 switch (c) {
9035 case '\0': case '\004': case '\032': return 1;
9036 }
9037 return 0;
9038}
9039
9040#define NUM_SUFFIX_R (1<<0)
9041#define NUM_SUFFIX_I (1<<1)
9042#define NUM_SUFFIX_ALL 3
9043
9044static int
9045number_literal_suffix(struct parser_params *p, int mask)
9046{
9047 int c, result = 0;
9048 const char *lastp = p->lex.pcur;
9049
9050 while ((c = nextc(p)) != -1) {
9051 if ((mask & NUM_SUFFIX_I) && c == 'i') {
9052 result |= (mask & NUM_SUFFIX_I);
9053 mask &= ~NUM_SUFFIX_I;
9054 /* r after i, rational of complex is disallowed */
9055 mask &= ~NUM_SUFFIX_R;
9056 continue;
9057 }
9058 if ((mask & NUM_SUFFIX_R) && c == 'r') {
9059 result |= (mask & NUM_SUFFIX_R);
9060 mask &= ~NUM_SUFFIX_R;
9061 continue;
9062 }
9063 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
9064 p->lex.pcur = lastp;
9065 literal_flush(p, p->lex.pcur);
9066 return 0;
9067 }
9068 pushback(p, c);
9069 break;
9070 }
9071 return result;
9072}
9073
9074static enum yytokentype
9075set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
9076{
9077 enum rb_numeric_type numeric_type = integer_literal;
9078
9079 if (type == tFLOAT) {
9080 numeric_type = float_literal;
9081 }
9082
9083 if (suffix & NUM_SUFFIX_R) {
9084 type = tRATIONAL;
9085 numeric_type = rational_literal;
9086 }
9087 if (suffix & NUM_SUFFIX_I) {
9088 type = tIMAGINARY;
9089 }
9090
9091 switch (type) {
9092 case tINTEGER:
9093 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
9094 break;
9095 case tFLOAT:
9096 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
9097 break;
9098 case tRATIONAL:
9099 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
9100 break;
9101 case tIMAGINARY:
9102 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
9103 (void)numeric_type; /* for ripper */
9104 break;
9105 default:
9106 rb_bug("unexpected token: %d", type);
9107 }
9108 SET_LEX_STATE(EXPR_END);
9109 return type;
9110}
9111
9112#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
9113static void
9114parser_dispatch_heredoc_end(struct parser_params *p, int line)
9115{
9116 if (has_delayed_token(p))
9117 dispatch_delayed_token(p, tSTRING_CONTENT);
9118
9119#ifdef RIPPER
9120 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
9121 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9122#else
9123 if (p->keep_tokens) {
9124 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9125 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9126 parser_append_tokens(p, str, tHEREDOC_END, line);
9127 }
9128#endif
9129
9130 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9131 lex_goto_eol(p);
9132 token_flush(p);
9133}
9134
9135static enum yytokentype
9136here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9137{
9138 int c, func, indent = 0;
9139 const char *eos, *ptr, *ptr_end;
9140 long len;
9141 rb_parser_string_t *str = 0;
9142 rb_encoding *enc = p->enc;
9143 rb_encoding *base_enc = 0;
9144 int bol;
9145#ifdef RIPPER
9146 VALUE s_value;
9147#endif
9148
9149 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9150 len = here->length;
9151 indent = (func = here->func) & STR_FUNC_INDENT;
9152
9153 if ((c = nextc(p)) == -1) {
9154 error:
9155#ifdef RIPPER
9156 if (!has_delayed_token(p)) {
9157 dispatch_scan_event(p, tSTRING_CONTENT);
9158 }
9159 else {
9160 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9161 if (!(func & STR_FUNC_REGEXP)) {
9162 int cr = ENC_CODERANGE_UNKNOWN;
9163 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9164 if (cr != ENC_CODERANGE_7BIT &&
9165 rb_is_usascii_enc(p->enc) &&
9166 enc != rb_utf8_encoding()) {
9167 enc = rb_ascii8bit_encoding();
9168 }
9169 }
9170 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9171 }
9172 dispatch_delayed_token(p, tSTRING_CONTENT);
9173 }
9174 lex_goto_eol(p);
9175#endif
9176 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9177 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9178 (int)len, eos);
9179 token_flush(p);
9180 SET_LEX_STATE(EXPR_END);
9181 return tSTRING_END;
9182 }
9183 bol = was_bol(p);
9184 if (!bol) {
9185 /* not beginning of line, cannot be the terminator */
9186 }
9187 else if (p->heredoc_line_indent == -1) {
9188 /* `heredoc_line_indent == -1` means
9189 * - "after an interpolation in the same line", or
9190 * - "in a continuing line"
9191 */
9192 p->heredoc_line_indent = 0;
9193 }
9194 else if (whole_match_p(p, eos, len, indent)) {
9195 dispatch_heredoc_end(p);
9196 restore:
9197 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9198 token_flush(p);
9199 SET_LEX_STATE(EXPR_END);
9200 return tSTRING_END;
9201 }
9202
9203 if (!(func & STR_FUNC_EXPAND)) {
9204 do {
9205 ptr = PARSER_STRING_PTR(p->lex.lastline);
9206 ptr_end = p->lex.pend;
9207 if (ptr_end > ptr) {
9208 switch (ptr_end[-1]) {
9209 case '\n':
9210 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9211 ptr_end++;
9212 break;
9213 }
9214 case '\r':
9215 --ptr_end;
9216 }
9217 }
9218
9219 if (p->heredoc_indent > 0) {
9220 long i = 0;
9221 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9222 i++;
9223 p->heredoc_line_indent = 0;
9224 }
9225
9226 if (str)
9227 parser_str_cat(str, ptr, ptr_end - ptr);
9228 else
9229 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9230 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9231 lex_goto_eol(p);
9232 if (p->heredoc_indent > 0) {
9233 goto flush_str;
9234 }
9235 if (nextc(p) == -1) {
9236 if (str) {
9237 rb_parser_string_free(p, str);
9238 str = 0;
9239 }
9240 goto error;
9241 }
9242 } while (!whole_match_p(p, eos, len, indent));
9243 }
9244 else {
9245 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9246 newtok(p);
9247 if (c == '#') {
9248 enum yytokentype t = parser_peek_variable_name(p);
9249 if (p->heredoc_line_indent != -1) {
9250 if (p->heredoc_indent > p->heredoc_line_indent) {
9251 p->heredoc_indent = p->heredoc_line_indent;
9252 }
9253 p->heredoc_line_indent = -1;
9254 }
9255 if (t) return t;
9256 tokadd(p, '#');
9257 c = nextc(p);
9258 }
9259 do {
9260 pushback(p, c);
9261 enc = p->enc;
9262 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9263 if (p->eofp) goto error;
9264 goto restore;
9265 }
9266 if (c != '\n') {
9267 if (c == '\\') p->heredoc_line_indent = -1;
9268 flush:
9269 str = STR_NEW3(tok(p), toklen(p), enc, func);
9270 flush_str:
9271 set_yylval_str(str);
9272#ifndef RIPPER
9273 if (bol) nd_set_fl_newline(yylval.node);
9274#endif
9275 flush_string_content(p, enc, 0);
9276 return tSTRING_CONTENT;
9277 }
9278 tokadd(p, nextc(p));
9279 if (p->heredoc_indent > 0) {
9280 lex_goto_eol(p);
9281 goto flush;
9282 }
9283 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9284 if ((c = nextc(p)) == -1) goto error;
9285 } while (!whole_match_p(p, eos, len, indent));
9286 str = STR_NEW3(tok(p), toklen(p), enc, func);
9287 }
9288 dispatch_heredoc_end(p);
9289 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9290 token_flush(p);
9291 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9292#ifdef RIPPER
9293 /* Preserve s_value for set_yylval_str */
9294 s_value = p->s_value;
9295#endif
9296 set_yylval_str(str);
9297#ifdef RIPPER
9298 set_parser_s_value(s_value);
9299#endif
9300
9301#ifndef RIPPER
9302 if (bol) nd_set_fl_newline(yylval.node);
9303#endif
9304 return tSTRING_CONTENT;
9305}
9306
9307#include "lex.c"
9308
9309static int
9310arg_ambiguous(struct parser_params *p, char c)
9311{
9312#ifndef RIPPER
9313 if (c == '/') {
9314 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after '%c' operator", WARN_I(c));
9315 }
9316 else {
9317 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9318 }
9319#else
9320 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9321#endif
9322 return TRUE;
9323}
9324
9325/* returns true value if formal argument error;
9326 * Qtrue, or error message if ripper */
9327static VALUE
9328formal_argument_error(struct parser_params *p, ID id)
9329{
9330 switch (id_type(id)) {
9331 case ID_LOCAL:
9332 break;
9333#ifndef RIPPER
9334# define ERR(mesg) (yyerror0(mesg), Qtrue)
9335#else
9336# define ERR(mesg) WARN_S(mesg)
9337#endif
9338 case ID_CONST:
9339 return ERR("formal argument cannot be a constant");
9340 case ID_INSTANCE:
9341 return ERR("formal argument cannot be an instance variable");
9342 case ID_GLOBAL:
9343 return ERR("formal argument cannot be a global variable");
9344 case ID_CLASS:
9345 return ERR("formal argument cannot be a class variable");
9346 default:
9347 return ERR("formal argument must be local variable");
9348#undef ERR
9349 }
9350 shadowing_lvar(p, id);
9351
9352 return Qfalse;
9353}
9354
9355static int
9356lvar_defined(struct parser_params *p, ID id)
9357{
9358 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9359}
9360
9361/* emacsen -*- hack */
9362static long
9363parser_encode_length(struct parser_params *p, const char *name, long len)
9364{
9365 long nlen;
9366
9367 if (len > 5 && name[nlen = len - 5] == '-') {
9368 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9369 return nlen;
9370 }
9371 if (len > 4 && name[nlen = len - 4] == '-') {
9372 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9373 return nlen;
9374 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9375 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9376 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9377 return nlen;
9378 }
9379 return len;
9380}
9381
9382static void
9383parser_set_encode(struct parser_params *p, const char *name)
9384{
9385 rb_encoding *enc;
9386 VALUE excargs[3];
9387 int idx = 0;
9388
9389 const char *wrong = 0;
9390 switch (*name) {
9391 case 'e': case 'E': wrong = "external"; break;
9392 case 'i': case 'I': wrong = "internal"; break;
9393 case 'f': case 'F': wrong = "filesystem"; break;
9394 case 'l': case 'L': wrong = "locale"; break;
9395 }
9396 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9397 idx = rb_enc_find_index(name);
9398 if (idx < 0) {
9399 unknown:
9400 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9401 error:
9402 excargs[0] = rb_eArgError;
9403 excargs[2] = rb_make_backtrace();
9404 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9405 VALUE exc = rb_make_exception(3, excargs);
9406 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9407
9408 rb_ast_free(p->ast);
9409 p->ast = NULL;
9410
9411 rb_exc_raise(exc);
9412 }
9413 enc = rb_enc_from_index(idx);
9414 if (!rb_enc_asciicompat(enc)) {
9415 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9416 goto error;
9417 }
9418 p->enc = enc;
9419#ifndef RIPPER
9420 if (p->debug_lines) {
9421 long i;
9422 for (i = 0; i < p->debug_lines->len; i++) {
9423 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9424 }
9425 }
9426#endif
9427}
9428
9429static bool
9430comment_at_top(struct parser_params *p)
9431{
9432 if (p->token_seen) return false;
9433 return (p->line_count == (p->has_shebang ? 2 : 1));
9434}
9435
9436typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9437typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9438
9439static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9440
9441static void
9442magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9443{
9444 if (!comment_at_top(p)) {
9445 return;
9446 }
9447 parser_set_encode(p, val);
9448}
9449
9450static int
9451parser_get_bool(struct parser_params *p, const char *name, const char *val)
9452{
9453 switch (*val) {
9454 case 't': case 'T':
9455 if (STRCASECMP(val, "true") == 0) {
9456 return TRUE;
9457 }
9458 break;
9459 case 'f': case 'F':
9460 if (STRCASECMP(val, "false") == 0) {
9461 return FALSE;
9462 }
9463 break;
9464 }
9465 return parser_invalid_pragma_value(p, name, val);
9466}
9467
9468static int
9469parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9470{
9471 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9472 return -1;
9473}
9474
9475static void
9476parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9477{
9478 int b = parser_get_bool(p, name, val);
9479 if (b >= 0) p->token_info_enabled = b;
9480}
9481
9482static void
9483parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9484{
9485 int b;
9486
9487 if (p->token_seen) {
9488 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9489 return;
9490 }
9491
9492 b = parser_get_bool(p, name, val);
9493 if (b < 0) return;
9494
9495 p->frozen_string_literal = b;
9496}
9497
9498static void
9499parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9500{
9501 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9502 if (*s == ' ' || *s == '\t') continue;
9503 if (*s == '#') break;
9504 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9505 return;
9506 }
9507
9508 switch (*val) {
9509 case 'n': case 'N':
9510 if (STRCASECMP(val, "none") == 0) {
9511 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9512 return;
9513 }
9514 break;
9515 case 'l': case 'L':
9516 if (STRCASECMP(val, "literal") == 0) {
9517 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9518 return;
9519 }
9520 break;
9521 case 'e': case 'E':
9522 if (STRCASECMP(val, "experimental_copy") == 0) {
9523 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9524 return;
9525 }
9526 if (STRCASECMP(val, "experimental_everything") == 0) {
9527 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9528 return;
9529 }
9530 break;
9531 }
9532 parser_invalid_pragma_value(p, name, val);
9533}
9534
9535# if WARN_PAST_SCOPE
9536static void
9537parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9538{
9539 int b = parser_get_bool(p, name, val);
9540 if (b >= 0) p->past_scope_enabled = b;
9541}
9542# endif
9543
9544struct magic_comment {
9545 const char *name;
9546 rb_magic_comment_setter_t func;
9547 rb_magic_comment_length_t length;
9548};
9549
9550static const struct magic_comment magic_comments[] = {
9551 {"coding", magic_comment_encoding, parser_encode_length},
9552 {"encoding", magic_comment_encoding, parser_encode_length},
9553 {"frozen_string_literal", parser_set_frozen_string_literal},
9554 {"shareable_constant_value", parser_set_shareable_constant_value},
9555 {"warn_indent", parser_set_token_info},
9556# if WARN_PAST_SCOPE
9557 {"warn_past_scope", parser_set_past_scope},
9558# endif
9559};
9560
9561static const char *
9562magic_comment_marker(const char *str, long len)
9563{
9564 long i = 2;
9565
9566 while (i < len) {
9567 switch (str[i]) {
9568 case '-':
9569 if (str[i-1] == '*' && str[i-2] == '-') {
9570 return str + i + 1;
9571 }
9572 i += 2;
9573 break;
9574 case '*':
9575 if (i + 1 >= len) return 0;
9576 if (str[i+1] != '-') {
9577 i += 4;
9578 }
9579 else if (str[i-1] != '-') {
9580 i += 2;
9581 }
9582 else {
9583 return str + i + 2;
9584 }
9585 break;
9586 default:
9587 i += 3;
9588 break;
9589 }
9590 }
9591 return 0;
9592}
9593
9594static int
9595parser_magic_comment(struct parser_params *p, const char *str, long len)
9596{
9597 int indicator = 0;
9598 VALUE name = 0, val = 0;
9599 const char *beg, *end, *vbeg, *vend;
9600#define str_copy(_s, _p, _n) ((_s) \
9601 ? (void)(rb_str_resize((_s), (_n)), \
9602 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9603 : (void)((_s) = STR_NEW((_p), (_n))))
9604
9605 if (len <= 7) return FALSE;
9606 if (!!(beg = magic_comment_marker(str, len))) {
9607 if (!(end = magic_comment_marker(beg, str + len - beg)))
9608 return FALSE;
9609 indicator = TRUE;
9610 str = beg;
9611 len = end - beg - 3;
9612 }
9613
9614 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9615 while (len > 0) {
9616 const struct magic_comment *mc = magic_comments;
9617 char *s;
9618 int i;
9619 long n = 0;
9620
9621 for (; len > 0 && *str; str++, --len) {
9622 switch (*str) {
9623 case '\'': case '"': case ':': case ';':
9624 continue;
9625 }
9626 if (!ISSPACE(*str)) break;
9627 }
9628 for (beg = str; len > 0; str++, --len) {
9629 switch (*str) {
9630 case '\'': case '"': case ':': case ';':
9631 break;
9632 default:
9633 if (ISSPACE(*str)) break;
9634 continue;
9635 }
9636 break;
9637 }
9638 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9639 if (!len) break;
9640 if (*str != ':') {
9641 if (!indicator) return FALSE;
9642 continue;
9643 }
9644
9645 do str++; while (--len > 0 && ISSPACE(*str));
9646 if (!len) break;
9647 const char *tok_beg = str;
9648 if (*str == '"') {
9649 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9650 if (*str == '\\') {
9651 --len;
9652 ++str;
9653 }
9654 }
9655 vend = str;
9656 if (len) {
9657 --len;
9658 ++str;
9659 }
9660 }
9661 else {
9662 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9663 vend = str;
9664 }
9665 const char *tok_end = str;
9666 if (indicator) {
9667 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9668 }
9669 else {
9670 while (len > 0 && (ISSPACE(*str))) --len, str++;
9671 if (len) return FALSE;
9672 }
9673
9674 n = end - beg;
9675 str_copy(name, beg, n);
9676 s = RSTRING_PTR(name);
9677 for (i = 0; i < n; ++i) {
9678 if (s[i] == '-') s[i] = '_';
9679 }
9680 do {
9681 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9682 n = vend - vbeg;
9683 if (mc->length) {
9684 n = (*mc->length)(p, vbeg, n);
9685 }
9686 str_copy(val, vbeg, n);
9687 p->lex.ptok = tok_beg;
9688 p->lex.pcur = tok_end;
9689 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9690 break;
9691 }
9692 } while (++mc < magic_comments + numberof(magic_comments));
9693#ifdef RIPPER
9694 str_copy(val, vbeg, vend - vbeg);
9695 dispatch2(magic_comment, name, val);
9696#endif
9697 }
9698
9699 return TRUE;
9700}
9701
9702static void
9703set_file_encoding(struct parser_params *p, const char *str, const char *send)
9704{
9705 int sep = 0;
9706 const char *beg = str;
9707 VALUE s;
9708
9709 for (;;) {
9710 if (send - str <= 6) return;
9711 switch (str[6]) {
9712 case 'C': case 'c': str += 6; continue;
9713 case 'O': case 'o': str += 5; continue;
9714 case 'D': case 'd': str += 4; continue;
9715 case 'I': case 'i': str += 3; continue;
9716 case 'N': case 'n': str += 2; continue;
9717 case 'G': case 'g': str += 1; continue;
9718 case '=': case ':':
9719 sep = 1;
9720 str += 6;
9721 break;
9722 default:
9723 str += 6;
9724 if (ISSPACE(*str)) break;
9725 continue;
9726 }
9727 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9728 sep = 0;
9729 }
9730 for (;;) {
9731 do {
9732 if (++str >= send) return;
9733 } while (ISSPACE(*str));
9734 if (sep) break;
9735 if (*str != '=' && *str != ':') return;
9736 sep = 1;
9737 str++;
9738 }
9739 beg = str;
9740 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9741 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9742 p->lex.ptok = beg;
9743 p->lex.pcur = str;
9744 parser_set_encode(p, RSTRING_PTR(s));
9745 rb_str_resize(s, 0);
9746}
9747
9748static void
9749parser_prepare(struct parser_params *p)
9750{
9751 int c = nextc0(p, FALSE);
9752 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9753 switch (c) {
9754 case '#':
9755 if (peek(p, '!')) p->has_shebang = 1;
9756 break;
9757 case 0xef: /* UTF-8 BOM marker */
9758 if (!lex_eol_n_p(p, 2) &&
9759 (unsigned char)p->lex.pcur[0] == 0xbb &&
9760 (unsigned char)p->lex.pcur[1] == 0xbf) {
9761 p->enc = rb_utf8_encoding();
9762 p->lex.pcur += 2;
9763#ifndef RIPPER
9764 if (p->debug_lines) {
9765 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9766 }
9767#endif
9768 p->lex.pbeg = p->lex.pcur;
9769 token_flush(p);
9770 return;
9771 }
9772 break;
9773 case -1: /* end of script. */
9774 return;
9775 }
9776 pushback(p, c);
9777 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9778}
9779
9780#ifndef RIPPER
9781#define ambiguous_operator(tok, op, syn) ( \
9782 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9783 rb_warning0("even though it seems like "syn""))
9784#else
9785#define ambiguous_operator(tok, op, syn) \
9786 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9787#endif
9788#define warn_balanced(tok, op, syn) ((void) \
9789 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9790 space_seen && !ISSPACE(c) && \
9791 (ambiguous_operator(tok, op, syn), 0)), \
9792 (enum yytokentype)(tok))
9793
9794static enum yytokentype
9795no_digits(struct parser_params *p)
9796{
9797 yyerror0("numeric literal without digits");
9798 if (peek(p, '_')) nextc(p);
9799 /* dummy 0, for tUMINUS_NUM at numeric */
9800 return set_number_literal(p, tINTEGER, 0, 10, 0);
9801}
9802
9803static enum yytokentype
9804parse_numeric(struct parser_params *p, int c)
9805{
9806 int is_float, seen_point, seen_e, nondigit;
9807 int suffix;
9808
9809 is_float = seen_point = seen_e = nondigit = 0;
9810 SET_LEX_STATE(EXPR_END);
9811 newtok(p);
9812 if (c == '-' || c == '+') {
9813 tokadd(p, c);
9814 c = nextc(p);
9815 }
9816 if (c == '0') {
9817 int start = toklen(p);
9818 c = nextc(p);
9819 if (c == 'x' || c == 'X') {
9820 /* hexadecimal */
9821 c = nextc(p);
9822 if (c != -1 && ISXDIGIT(c)) {
9823 do {
9824 if (c == '_') {
9825 if (nondigit) break;
9826 nondigit = c;
9827 continue;
9828 }
9829 if (!ISXDIGIT(c)) break;
9830 nondigit = 0;
9831 tokadd(p, c);
9832 } while ((c = nextc(p)) != -1);
9833 }
9834 pushback(p, c);
9835 tokfix(p);
9836 if (toklen(p) == start) {
9837 return no_digits(p);
9838 }
9839 else if (nondigit) goto trailing_uc;
9840 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9841 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9842 }
9843 if (c == 'b' || c == 'B') {
9844 /* binary */
9845 c = nextc(p);
9846 if (c == '0' || c == '1') {
9847 do {
9848 if (c == '_') {
9849 if (nondigit) break;
9850 nondigit = c;
9851 continue;
9852 }
9853 if (c != '0' && c != '1') break;
9854 nondigit = 0;
9855 tokadd(p, c);
9856 } while ((c = nextc(p)) != -1);
9857 }
9858 pushback(p, c);
9859 tokfix(p);
9860 if (toklen(p) == start) {
9861 return no_digits(p);
9862 }
9863 else if (nondigit) goto trailing_uc;
9864 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9865 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9866 }
9867 if (c == 'd' || c == 'D') {
9868 /* decimal */
9869 c = nextc(p);
9870 if (c != -1 && ISDIGIT(c)) {
9871 do {
9872 if (c == '_') {
9873 if (nondigit) break;
9874 nondigit = c;
9875 continue;
9876 }
9877 if (!ISDIGIT(c)) break;
9878 nondigit = 0;
9879 tokadd(p, c);
9880 } while ((c = nextc(p)) != -1);
9881 }
9882 pushback(p, c);
9883 tokfix(p);
9884 if (toklen(p) == start) {
9885 return no_digits(p);
9886 }
9887 else if (nondigit) goto trailing_uc;
9888 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9889 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9890 }
9891 if (c == '_') {
9892 /* 0_0 */
9893 goto octal_number;
9894 }
9895 if (c == 'o' || c == 'O') {
9896 /* prefixed octal */
9897 c = nextc(p);
9898 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9899 tokfix(p);
9900 return no_digits(p);
9901 }
9902 }
9903 if (c >= '0' && c <= '7') {
9904 /* octal */
9905 octal_number:
9906 do {
9907 if (c == '_') {
9908 if (nondigit) break;
9909 nondigit = c;
9910 continue;
9911 }
9912 if (c < '0' || c > '9') break;
9913 if (c > '7') goto invalid_octal;
9914 nondigit = 0;
9915 tokadd(p, c);
9916 } while ((c = nextc(p)) != -1);
9917 if (toklen(p) > start) {
9918 pushback(p, c);
9919 tokfix(p);
9920 if (nondigit) goto trailing_uc;
9921 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9922 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9923 }
9924 if (nondigit) {
9925 pushback(p, c);
9926 goto trailing_uc;
9927 }
9928 }
9929 if (c > '7' && c <= '9') {
9930 invalid_octal:
9931 yyerror0("Invalid octal digit");
9932 }
9933 else if (c == '.' || c == 'e' || c == 'E') {
9934 tokadd(p, '0');
9935 }
9936 else {
9937 pushback(p, c);
9938 tokfix(p);
9939 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9940 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9941 }
9942 }
9943
9944 for (;;) {
9945 switch (c) {
9946 case '0': case '1': case '2': case '3': case '4':
9947 case '5': case '6': case '7': case '8': case '9':
9948 nondigit = 0;
9949 tokadd(p, c);
9950 break;
9951
9952 case '.':
9953 if (nondigit) goto trailing_uc;
9954 if (seen_point || seen_e) {
9955 goto decode_num;
9956 }
9957 else {
9958 int c0 = nextc(p);
9959 if (c0 == -1 || !ISDIGIT(c0)) {
9960 pushback(p, c0);
9961 goto decode_num;
9962 }
9963 c = c0;
9964 }
9965 seen_point = toklen(p);
9966 tokadd(p, '.');
9967 tokadd(p, c);
9968 is_float++;
9969 nondigit = 0;
9970 break;
9971
9972 case 'e':
9973 case 'E':
9974 if (nondigit) {
9975 pushback(p, c);
9976 c = nondigit;
9977 goto decode_num;
9978 }
9979 if (seen_e) {
9980 goto decode_num;
9981 }
9982 nondigit = c;
9983 c = nextc(p);
9984 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9985 pushback(p, c);
9986 c = nondigit;
9987 nondigit = 0;
9988 goto decode_num;
9989 }
9990 tokadd(p, nondigit);
9991 seen_e++;
9992 is_float++;
9993 tokadd(p, c);
9994 nondigit = (c == '-' || c == '+') ? c : 0;
9995 break;
9996
9997 case '_': /* `_' in number just ignored */
9998 if (nondigit) goto decode_num;
9999 nondigit = c;
10000 break;
10001
10002 default:
10003 goto decode_num;
10004 }
10005 c = nextc(p);
10006 }
10007
10008 decode_num:
10009 pushback(p, c);
10010 if (nondigit) {
10011 trailing_uc:
10012 literal_flush(p, p->lex.pcur - 1);
10013 YYLTYPE loc = RUBY_INIT_YYLLOC();
10014 compile_error(p, "trailing '%c' in number", nondigit);
10015 parser_show_error_line(p, &loc);
10016 }
10017 tokfix(p);
10018 if (is_float) {
10019 enum yytokentype type = tFLOAT;
10020
10021 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
10022 if (suffix & NUM_SUFFIX_R) {
10023 type = tRATIONAL;
10024 }
10025 else {
10026 strtod(tok(p), 0);
10027 if (errno == ERANGE) {
10028 rb_warning1("Float %s out of range", WARN_S(tok(p)));
10029 errno = 0;
10030 }
10031 }
10032 return set_number_literal(p, type, suffix, 0, seen_point);
10033 }
10034 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
10035 return set_number_literal(p, tINTEGER, suffix, 10, 0);
10036}
10037
10038static enum yytokentype
10039parse_qmark(struct parser_params *p, int space_seen)
10040{
10041 rb_encoding *enc;
10042 register int c;
10043 rb_parser_string_t *lit;
10044
10045 if (IS_END()) {
10046 SET_LEX_STATE(EXPR_VALUE);
10047 return '?';
10048 }
10049 c = nextc(p);
10050 if (c == -1) {
10051 compile_error(p, "incomplete character syntax");
10052 return 0;
10053 }
10054 if (rb_enc_isspace(c, p->enc)) {
10055 if (!IS_ARG()) {
10056 int c2 = escaped_control_code(c);
10057 if (c2) {
10058 WARN_SPACE_CHAR(c2, "?");
10059 }
10060 }
10061 ternary:
10062 pushback(p, c);
10063 SET_LEX_STATE(EXPR_VALUE);
10064 return '?';
10065 }
10066 newtok(p);
10067 enc = p->enc;
10068 if (!parser_isascii(p)) {
10069 if (tokadd_mbchar(p, c) == -1) return 0;
10070 }
10071 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
10072 !lex_eol_p(p) && is_identchar(p, p->lex.pcur, p->lex.pend, p->enc)) {
10073 if (space_seen) {
10074 const char *start = p->lex.pcur - 1, *ptr = start;
10075 do {
10076 int n = parser_precise_mbclen(p, ptr);
10077 if (n < 0) return -1;
10078 ptr += n;
10079 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
10080 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
10081 " a conditional operator, put a space after '?'",
10082 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
10083 }
10084 goto ternary;
10085 }
10086 else if (c == '\\') {
10087 if (peek(p, 'u')) {
10088 nextc(p);
10089 enc = rb_utf8_encoding();
10090 tokadd_utf8(p, &enc, -1, 0, 0);
10091 }
10092 else if (!ISASCII(c = peekc(p)) && c != -1) {
10093 nextc(p);
10094 if (tokadd_mbchar(p, c) == -1) return 0;
10095 }
10096 else {
10097 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
10098 tokadd(p, c);
10099 }
10100 }
10101 else {
10102 tokadd(p, c);
10103 }
10104 tokfix(p);
10105 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
10106 set_yylval_str(lit);
10107 SET_LEX_STATE(EXPR_END);
10108 return tCHAR;
10109}
10110
10111static enum yytokentype
10112parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
10113{
10114 register int c;
10115 const char *ptok = p->lex.pcur;
10116
10117 if (IS_BEG()) {
10118 int term;
10119 int paren;
10120
10121 c = nextc(p);
10122 quotation:
10123 if (c == -1) goto unterminated;
10124 if (!ISALNUM(c)) {
10125 term = c;
10126 if (!ISASCII(c)) goto unknown;
10127 c = 'Q';
10128 }
10129 else {
10130 term = nextc(p);
10131 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10132 unknown:
10133 pushback(p, term);
10134 c = parser_precise_mbclen(p, p->lex.pcur);
10135 if (c < 0) return 0;
10136 p->lex.pcur += c;
10137 yyerror0("unknown type of %string");
10138 return 0;
10139 }
10140 }
10141 if (term == -1) {
10142 unterminated:
10143 compile_error(p, "unterminated quoted string meets end of file");
10144 return 0;
10145 }
10146 paren = term;
10147 if (term == '(') term = ')';
10148 else if (term == '[') term = ']';
10149 else if (term == '{') term = '}';
10150 else if (term == '<') term = '>';
10151 else paren = 0;
10152
10153 p->lex.ptok = ptok-1;
10154 switch (c) {
10155 case 'Q':
10156 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10157 return tSTRING_BEG;
10158
10159 case 'q':
10160 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10161 return tSTRING_BEG;
10162
10163 case 'W':
10164 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10165 return tWORDS_BEG;
10166
10167 case 'w':
10168 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10169 return tQWORDS_BEG;
10170
10171 case 'I':
10172 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10173 return tSYMBOLS_BEG;
10174
10175 case 'i':
10176 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10177 return tQSYMBOLS_BEG;
10178
10179 case 'x':
10180 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10181 return tXSTRING_BEG;
10182
10183 case 'r':
10184 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10185 return tREGEXP_BEG;
10186
10187 case 's':
10188 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10189 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10190 return tSYMBEG;
10191
10192 default:
10193 yyerror0("unknown type of %string");
10194 return 0;
10195 }
10196 }
10197 if ((c = nextc(p)) == '=') {
10198 set_yylval_id('%');
10199 SET_LEX_STATE(EXPR_BEG);
10200 return tOP_ASGN;
10201 }
10202 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10203 goto quotation;
10204 }
10205 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10206 pushback(p, c);
10207 return warn_balanced('%', "%%", "string literal");
10208}
10209
10210static int
10211tokadd_ident(struct parser_params *p, int c)
10212{
10213 do {
10214 if (tokadd_mbchar(p, c) == -1) return -1;
10215 c = nextc(p);
10216 } while (parser_is_identchar(p));
10217 pushback(p, c);
10218 return 0;
10219}
10220
10221static ID
10222tokenize_ident(struct parser_params *p)
10223{
10224 ID ident = TOK_INTERN();
10225
10226 set_yylval_name(ident);
10227
10228 return ident;
10229}
10230
10231static int
10232parse_numvar(struct parser_params *p)
10233{
10234 size_t len;
10235 int overflow;
10236 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10237 const unsigned long nth_ref_max =
10238 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10239 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10240 * turned into a Fixnum, in compile.c */
10241
10242 if (overflow || n > nth_ref_max) {
10243 /* compile_error()? */
10244 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10245 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10246 }
10247 else {
10248 return (int)n;
10249 }
10250}
10251
10252static enum yytokentype
10253parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10254{
10255 const char *ptr = p->lex.pcur;
10256 register int c;
10257
10258 SET_LEX_STATE(EXPR_END);
10259 p->lex.ptok = ptr - 1; /* from '$' */
10260 newtok(p);
10261 c = nextc(p);
10262 switch (c) {
10263 case '_': /* $_: last read line string */
10264 c = nextc(p);
10265 if (parser_is_identchar(p)) {
10266 tokadd(p, '$');
10267 tokadd(p, '_');
10268 break;
10269 }
10270 pushback(p, c);
10271 c = '_';
10272 /* fall through */
10273 case '~': /* $~: match-data */
10274 case '*': /* $*: argv */
10275 case '$': /* $$: pid */
10276 case '?': /* $?: last status */
10277 case '!': /* $!: error string */
10278 case '@': /* $@: error position */
10279 case '/': /* $/: input record separator */
10280 case '\\': /* $\: output record separator */
10281 case ';': /* $;: field separator */
10282 case ',': /* $,: output field separator */
10283 case '.': /* $.: last read line number */
10284 case '=': /* $=: ignorecase */
10285 case ':': /* $:: load path */
10286 case '<': /* $<: default input handle */
10287 case '>': /* $>: default output handle */
10288 case '\"': /* $": already loaded files */
10289 tokadd(p, '$');
10290 tokadd(p, c);
10291 goto gvar;
10292
10293 case '-':
10294 tokadd(p, '$');
10295 tokadd(p, c);
10296 c = nextc(p);
10297 if (parser_is_identchar(p)) {
10298 if (tokadd_mbchar(p, c) == -1) return 0;
10299 }
10300 else {
10301 pushback(p, c);
10302 pushback(p, '-');
10303 return '$';
10304 }
10305 gvar:
10306 tokenize_ident(p);
10307 return tGVAR;
10308
10309 case '&': /* $&: last match */
10310 case '`': /* $`: string before last match */
10311 case '\'': /* $': string after last match */
10312 case '+': /* $+: string matches last paren. */
10313 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10314 tokadd(p, '$');
10315 tokadd(p, c);
10316 goto gvar;
10317 }
10318 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10319 return tBACK_REF;
10320
10321 case '1': case '2': case '3':
10322 case '4': case '5': case '6':
10323 case '7': case '8': case '9':
10324 tokadd(p, '$');
10325 do {
10326 tokadd(p, c);
10327 c = nextc(p);
10328 } while (c != -1 && ISDIGIT(c));
10329 pushback(p, c);
10330 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10331 tokfix(p);
10332 c = parse_numvar(p);
10333 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10334 return tNTH_REF;
10335
10336 default:
10337 if (!parser_is_identchar(p)) {
10338 YYLTYPE loc = RUBY_INIT_YYLLOC();
10339 if (c == -1 || ISSPACE(c)) {
10340 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10341 }
10342 else {
10343 pushback(p, c);
10344 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10345 }
10346 parser_show_error_line(p, &loc);
10347 set_yylval_noname();
10348 return tGVAR;
10349 }
10350 /* fall through */
10351 case '0':
10352 tokadd(p, '$');
10353 }
10354
10355 if (tokadd_ident(p, c)) return 0;
10356 SET_LEX_STATE(EXPR_END);
10357 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10358 tokenize_ident(p);
10359 }
10360 else {
10361 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10362 set_yylval_noname();
10363 }
10364 return tGVAR;
10365}
10366
10367static bool
10368parser_numbered_param(struct parser_params *p, int n)
10369{
10370 if (n < 0) return false;
10371
10372 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10373 return false;
10374 }
10375 if (p->max_numparam == ORDINAL_PARAM) {
10376 compile_error(p, "ordinary parameter is defined");
10377 return false;
10378 }
10379 struct vtable *args = p->lvtbl->args;
10380 if (p->max_numparam < n) {
10381 p->max_numparam = n;
10382 }
10383 while (n > args->pos) {
10384 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10385 }
10386 return true;
10387}
10388
10389static enum yytokentype
10390parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10391{
10392 const char *ptr = p->lex.pcur;
10393 enum yytokentype result = tIVAR;
10394 register int c = nextc(p);
10395 YYLTYPE loc;
10396
10397 p->lex.ptok = ptr - 1; /* from '@' */
10398 newtok(p);
10399 tokadd(p, '@');
10400 if (c == '@') {
10401 result = tCVAR;
10402 tokadd(p, '@');
10403 c = nextc(p);
10404 }
10405 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10406 if (c == -1 || !parser_is_identchar(p)) {
10407 pushback(p, c);
10408 RUBY_SET_YYLLOC(loc);
10409 if (result == tIVAR) {
10410 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10411 }
10412 else {
10413 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10414 }
10415 parser_show_error_line(p, &loc);
10416 set_yylval_noname();
10417 SET_LEX_STATE(EXPR_END);
10418 return result;
10419 }
10420 else if (ISDIGIT(c)) {
10421 pushback(p, c);
10422 RUBY_SET_YYLLOC(loc);
10423 if (result == tIVAR) {
10424 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10425 }
10426 else {
10427 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10428 }
10429 parser_show_error_line(p, &loc);
10430 set_yylval_noname();
10431 SET_LEX_STATE(EXPR_END);
10432 return result;
10433 }
10434
10435 if (tokadd_ident(p, c)) return 0;
10436 tokenize_ident(p);
10437 return result;
10438}
10439
10440static enum yytokentype
10441parse_ident(struct parser_params *p, int c, int cmd_state)
10442{
10443 enum yytokentype result;
10444 bool is_ascii = true;
10445 const enum lex_state_e last_state = p->lex.state;
10446 ID ident;
10447 int enforce_keyword_end = 0;
10448
10449 do {
10450 if (!ISASCII(c)) is_ascii = false;
10451 if (tokadd_mbchar(p, c) == -1) return 0;
10452 c = nextc(p);
10453 } while (parser_is_identchar(p));
10454 if ((c == '!' || c == '?') && !peek(p, '=')) {
10455 result = tFID;
10456 tokadd(p, c);
10457 }
10458 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10459 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10460 result = tIDENTIFIER;
10461 tokadd(p, c);
10462 }
10463 else {
10464 result = tCONSTANT; /* assume provisionally */
10465 pushback(p, c);
10466 }
10467 tokfix(p);
10468
10469 if (IS_LABEL_POSSIBLE()) {
10470 if (IS_LABEL_SUFFIX(0)) {
10471 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10472 nextc(p);
10473 tokenize_ident(p);
10474 return tLABEL;
10475 }
10476 }
10477
10478#ifndef RIPPER
10479 if (peek_end_expect_token_locations(p)) {
10480 const rb_code_position_t *end_pos;
10481 int lineno, column;
10482 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10483
10484 end_pos = peek_end_expect_token_locations(p)->pos;
10485 lineno = end_pos->lineno;
10486 column = end_pos->column;
10487
10488 if (p->debug) {
10489 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10490 p->ruby_sourceline, beg_pos, lineno, column);
10491 }
10492
10493 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10494 const struct kwtable *kw;
10495
10496 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10497 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10498 enforce_keyword_end = 1;
10499 }
10500 }
10501 }
10502#endif
10503
10504 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10505 const struct kwtable *kw;
10506
10507 /* See if it is a reserved word. */
10508 kw = rb_reserved_word(tok(p), toklen(p));
10509 if (kw) {
10510 enum lex_state_e state = p->lex.state;
10511 if (IS_lex_state_for(state, EXPR_FNAME)) {
10512 SET_LEX_STATE(EXPR_ENDFN);
10513 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10514 return kw->id[0];
10515 }
10516 SET_LEX_STATE(kw->state);
10517 if (IS_lex_state(EXPR_BEG)) {
10518 p->command_start = TRUE;
10519 }
10520 if (kw->id[0] == keyword_do) {
10521 if (lambda_beginning_p()) {
10522 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10523 return keyword_do_LAMBDA;
10524 }
10525 if (COND_P()) return keyword_do_cond;
10526 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10527 return keyword_do_block;
10528 return keyword_do;
10529 }
10530 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10531 return kw->id[0];
10532 else {
10533 if (kw->id[0] != kw->id[1])
10534 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10535 return kw->id[1];
10536 }
10537 }
10538 }
10539
10540 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10541 if (cmd_state) {
10542 SET_LEX_STATE(EXPR_CMDARG);
10543 }
10544 else {
10545 SET_LEX_STATE(EXPR_ARG);
10546 }
10547 }
10548 else if (p->lex.state == EXPR_FNAME) {
10549 SET_LEX_STATE(EXPR_ENDFN);
10550 }
10551 else {
10552 SET_LEX_STATE(EXPR_END);
10553 }
10554
10555 ident = tokenize_ident(p);
10556 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10557 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10558 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10559 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10560 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10561 }
10562 return result;
10563}
10564
10565static void
10566warn_cr(struct parser_params *p)
10567{
10568 if (!p->cr_seen) {
10569 p->cr_seen = TRUE;
10570 /* carried over with p->lex.nextline for nextc() */
10571 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10572 }
10573}
10574
10575static enum yytokentype
10576parser_yylex(struct parser_params *p)
10577{
10578 register int c;
10579 int space_seen = 0;
10580 int cmd_state;
10581 int label;
10582 enum lex_state_e last_state;
10583 int fallthru = FALSE;
10584 int token_seen = p->token_seen;
10585
10586 if (p->lex.strterm) {
10587 if (strterm_is_heredoc(p->lex.strterm)) {
10588 token_flush(p);
10589 return here_document(p, &p->lex.strterm->u.heredoc);
10590 }
10591 else {
10592 token_flush(p);
10593 return parse_string(p, &p->lex.strterm->u.literal);
10594 }
10595 }
10596 cmd_state = p->command_start;
10597 p->command_start = FALSE;
10598 p->token_seen = TRUE;
10599#ifndef RIPPER
10600 token_flush(p);
10601#endif
10602 retry:
10603 last_state = p->lex.state;
10604 switch (c = nextc(p)) {
10605 case '\0': /* NUL */
10606 case '\004': /* ^D */
10607 case '\032': /* ^Z */
10608 case -1: /* end of script. */
10609 p->eofp = 1;
10610#ifndef RIPPER
10611 if (p->end_expect_token_locations) {
10612 pop_end_expect_token_locations(p);
10613 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10614 return tDUMNY_END;
10615 }
10616#endif
10617 /* Set location for end-of-input because dispatch_scan_event is not called. */
10618 RUBY_SET_YYLLOC(*p->yylloc);
10619 return END_OF_INPUT;
10620
10621 /* white spaces */
10622 case '\r':
10623 warn_cr(p);
10624 /* fall through */
10625 case ' ': case '\t': case '\f':
10626 case '\13': /* '\v' */
10627 space_seen = 1;
10628 while ((c = nextc(p))) {
10629 switch (c) {
10630 case '\r':
10631 warn_cr(p);
10632 /* fall through */
10633 case ' ': case '\t': case '\f':
10634 case '\13': /* '\v' */
10635 break;
10636 default:
10637 goto outofloop;
10638 }
10639 }
10640 outofloop:
10641 pushback(p, c);
10642 dispatch_scan_event(p, tSP);
10643#ifndef RIPPER
10644 token_flush(p);
10645#endif
10646 goto retry;
10647
10648 case '#': /* it's a comment */
10649 p->token_seen = token_seen;
10650 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10651 /* no magic_comment in shebang line */
10652 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10653 if (comment_at_top(p)) {
10654 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10655 }
10656 }
10657 p->lex.pcur = pcur, p->lex.ptok = ptok;
10658 lex_goto_eol(p);
10659 dispatch_scan_event(p, tCOMMENT);
10660 fallthru = TRUE;
10661 /* fall through */
10662 case '\n':
10663 p->token_seen = token_seen;
10664 rb_parser_string_t *prevline = p->lex.lastline;
10665 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10666 !IS_lex_state(EXPR_LABELED));
10667 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10668 if (!fallthru) {
10669 dispatch_scan_event(p, tIGNORED_NL);
10670 }
10671 fallthru = FALSE;
10672 if (!c && p->ctxt.in_kwarg) {
10673 goto normal_newline;
10674 }
10675 goto retry;
10676 }
10677 while (1) {
10678 switch (c = nextc(p)) {
10679 case ' ': case '\t': case '\f': case '\r':
10680 case '\13': /* '\v' */
10681 space_seen = 1;
10682 break;
10683 case '#':
10684 pushback(p, c);
10685 if (space_seen) {
10686 dispatch_scan_event(p, tSP);
10687 token_flush(p);
10688 }
10689 goto retry;
10690 case '&':
10691 case '.': {
10692 dispatch_delayed_token(p, tIGNORED_NL);
10693 if (peek(p, '.') == (c == '&')) {
10694 pushback(p, c);
10695 dispatch_scan_event(p, tSP);
10696 goto retry;
10697 }
10698 }
10699 default:
10700 p->ruby_sourceline--;
10701 p->lex.nextline = p->lex.lastline;
10702 set_lastline(p, prevline);
10703 case -1: /* EOF no decrement*/
10704 if (c == -1 && space_seen) {
10705 dispatch_scan_event(p, tSP);
10706 }
10707 lex_goto_eol(p);
10708 if (c != -1) {
10709 token_flush(p);
10710 RUBY_SET_YYLLOC(*p->yylloc);
10711 }
10712 goto normal_newline;
10713 }
10714 }
10715 normal_newline:
10716 p->command_start = TRUE;
10717 SET_LEX_STATE(EXPR_BEG);
10718 return '\n';
10719
10720 case '*':
10721 if ((c = nextc(p)) == '*') {
10722 if ((c = nextc(p)) == '=') {
10723 set_yylval_id(idPow);
10724 SET_LEX_STATE(EXPR_BEG);
10725 return tOP_ASGN;
10726 }
10727 pushback(p, c);
10728 if (IS_SPCARG(c)) {
10729 rb_warning0("'**' interpreted as argument prefix");
10730 c = tDSTAR;
10731 }
10732 else if (IS_BEG()) {
10733 c = tDSTAR;
10734 }
10735 else {
10736 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10737 }
10738 }
10739 else {
10740 if (c == '=') {
10741 set_yylval_id('*');
10742 SET_LEX_STATE(EXPR_BEG);
10743 return tOP_ASGN;
10744 }
10745 pushback(p, c);
10746 if (IS_SPCARG(c)) {
10747 rb_warning0("'*' interpreted as argument prefix");
10748 c = tSTAR;
10749 }
10750 else if (IS_BEG()) {
10751 c = tSTAR;
10752 }
10753 else {
10754 c = warn_balanced('*', "*", "argument prefix");
10755 }
10756 }
10757 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10758 return c;
10759
10760 case '!':
10761 c = nextc(p);
10762 if (IS_AFTER_OPERATOR()) {
10763 SET_LEX_STATE(EXPR_ARG);
10764 if (c == '@') {
10765 return '!';
10766 }
10767 }
10768 else {
10769 SET_LEX_STATE(EXPR_BEG);
10770 }
10771 if (c == '=') {
10772 return tNEQ;
10773 }
10774 if (c == '~') {
10775 return tNMATCH;
10776 }
10777 pushback(p, c);
10778 return '!';
10779
10780 case '=':
10781 if (was_bol(p)) {
10782 /* skip embedded rd document */
10783 if (word_match_p(p, "begin", 5)) {
10784 int first_p = TRUE;
10785
10786 lex_goto_eol(p);
10787 dispatch_scan_event(p, tEMBDOC_BEG);
10788 for (;;) {
10789 lex_goto_eol(p);
10790 if (!first_p) {
10791 dispatch_scan_event(p, tEMBDOC);
10792 }
10793 first_p = FALSE;
10794 c = nextc(p);
10795 if (c == -1) {
10796 compile_error(p, "embedded document meets end of file");
10797 return END_OF_INPUT;
10798 }
10799 if (c == '=' && word_match_p(p, "end", 3)) {
10800 break;
10801 }
10802 pushback(p, c);
10803 }
10804 lex_goto_eol(p);
10805 dispatch_scan_event(p, tEMBDOC_END);
10806 goto retry;
10807 }
10808 }
10809
10810 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10811 if ((c = nextc(p)) == '=') {
10812 if ((c = nextc(p)) == '=') {
10813 return tEQQ;
10814 }
10815 pushback(p, c);
10816 return tEQ;
10817 }
10818 if (c == '~') {
10819 return tMATCH;
10820 }
10821 else if (c == '>') {
10822 return tASSOC;
10823 }
10824 pushback(p, c);
10825 return '=';
10826
10827 case '<':
10828 c = nextc(p);
10829 if (c == '<' &&
10830 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10831 !IS_END() &&
10832 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10833 enum yytokentype token = heredoc_identifier(p);
10834 if (token) return token < 0 ? 0 : token;
10835 }
10836 if (IS_AFTER_OPERATOR()) {
10837 SET_LEX_STATE(EXPR_ARG);
10838 }
10839 else {
10840 if (IS_lex_state(EXPR_CLASS))
10841 p->command_start = TRUE;
10842 SET_LEX_STATE(EXPR_BEG);
10843 }
10844 if (c == '=') {
10845 if ((c = nextc(p)) == '>') {
10846 return tCMP;
10847 }
10848 pushback(p, c);
10849 return tLEQ;
10850 }
10851 if (c == '<') {
10852 if ((c = nextc(p)) == '=') {
10853 set_yylval_id(idLTLT);
10854 SET_LEX_STATE(EXPR_BEG);
10855 return tOP_ASGN;
10856 }
10857 pushback(p, c);
10858 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10859 }
10860 pushback(p, c);
10861 return '<';
10862
10863 case '>':
10864 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10865 if ((c = nextc(p)) == '=') {
10866 return tGEQ;
10867 }
10868 if (c == '>') {
10869 if ((c = nextc(p)) == '=') {
10870 set_yylval_id(idGTGT);
10871 SET_LEX_STATE(EXPR_BEG);
10872 return tOP_ASGN;
10873 }
10874 pushback(p, c);
10875 return tRSHFT;
10876 }
10877 pushback(p, c);
10878 return '>';
10879
10880 case '"':
10881 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10882 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10883 p->lex.ptok = p->lex.pcur-1;
10884 return tSTRING_BEG;
10885
10886 case '`':
10887 if (IS_lex_state(EXPR_FNAME)) {
10888 SET_LEX_STATE(EXPR_ENDFN);
10889 return c;
10890 }
10891 if (IS_lex_state(EXPR_DOT)) {
10892 if (cmd_state)
10893 SET_LEX_STATE(EXPR_CMDARG);
10894 else
10895 SET_LEX_STATE(EXPR_ARG);
10896 return c;
10897 }
10898 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10899 return tXSTRING_BEG;
10900
10901 case '\'':
10902 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10903 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10904 p->lex.ptok = p->lex.pcur-1;
10905 return tSTRING_BEG;
10906
10907 case '?':
10908 return parse_qmark(p, space_seen);
10909
10910 case '&':
10911 if ((c = nextc(p)) == '&') {
10912 SET_LEX_STATE(EXPR_BEG);
10913 if ((c = nextc(p)) == '=') {
10914 set_yylval_id(idANDOP);
10915 SET_LEX_STATE(EXPR_BEG);
10916 return tOP_ASGN;
10917 }
10918 pushback(p, c);
10919 return tANDOP;
10920 }
10921 else if (c == '=') {
10922 set_yylval_id('&');
10923 SET_LEX_STATE(EXPR_BEG);
10924 return tOP_ASGN;
10925 }
10926 else if (c == '.') {
10927 set_yylval_id(idANDDOT);
10928 SET_LEX_STATE(EXPR_DOT);
10929 return tANDDOT;
10930 }
10931 pushback(p, c);
10932 if (IS_SPCARG(c)) {
10933 if ((c != ':') ||
10934 (c = peekc_n(p, 1)) == -1 ||
10935 !(c == '\'' || c == '"' ||
10936 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10937 rb_warning0("'&' interpreted as argument prefix");
10938 }
10939 c = tAMPER;
10940 }
10941 else if (IS_BEG()) {
10942 c = tAMPER;
10943 }
10944 else {
10945 c = warn_balanced('&', "&", "argument prefix");
10946 }
10947 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10948 return c;
10949
10950 case '|':
10951 if ((c = nextc(p)) == '|') {
10952 SET_LEX_STATE(EXPR_BEG);
10953 if ((c = nextc(p)) == '=') {
10954 set_yylval_id(idOROP);
10955 SET_LEX_STATE(EXPR_BEG);
10956 return tOP_ASGN;
10957 }
10958 pushback(p, c);
10959 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10960 c = '|';
10961 pushback(p, '|');
10962 return c;
10963 }
10964 return tOROP;
10965 }
10966 if (c == '=') {
10967 set_yylval_id('|');
10968 SET_LEX_STATE(EXPR_BEG);
10969 return tOP_ASGN;
10970 }
10971 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10972 pushback(p, c);
10973 return '|';
10974
10975 case '+':
10976 c = nextc(p);
10977 if (IS_AFTER_OPERATOR()) {
10978 SET_LEX_STATE(EXPR_ARG);
10979 if (c == '@') {
10980 return tUPLUS;
10981 }
10982 pushback(p, c);
10983 return '+';
10984 }
10985 if (c == '=') {
10986 set_yylval_id('+');
10987 SET_LEX_STATE(EXPR_BEG);
10988 return tOP_ASGN;
10989 }
10990 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10991 SET_LEX_STATE(EXPR_BEG);
10992 pushback(p, c);
10993 if (c != -1 && ISDIGIT(c)) {
10994 return parse_numeric(p, '+');
10995 }
10996 return tUPLUS;
10997 }
10998 SET_LEX_STATE(EXPR_BEG);
10999 pushback(p, c);
11000 return warn_balanced('+', "+", "unary operator");
11001
11002 case '-':
11003 c = nextc(p);
11004 if (IS_AFTER_OPERATOR()) {
11005 SET_LEX_STATE(EXPR_ARG);
11006 if (c == '@') {
11007 return tUMINUS;
11008 }
11009 pushback(p, c);
11010 return '-';
11011 }
11012 if (c == '=') {
11013 set_yylval_id('-');
11014 SET_LEX_STATE(EXPR_BEG);
11015 return tOP_ASGN;
11016 }
11017 if (c == '>') {
11018 SET_LEX_STATE(EXPR_ENDFN);
11019 yylval.num = p->lex.lpar_beg;
11020 p->lex.lpar_beg = p->lex.paren_nest;
11021 return tLAMBDA;
11022 }
11023 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
11024 SET_LEX_STATE(EXPR_BEG);
11025 pushback(p, c);
11026 if (c != -1 && ISDIGIT(c)) {
11027 return tUMINUS_NUM;
11028 }
11029 return tUMINUS;
11030 }
11031 SET_LEX_STATE(EXPR_BEG);
11032 pushback(p, c);
11033 return warn_balanced('-', "-", "unary operator");
11034
11035 case '.': {
11036 int is_beg = IS_BEG();
11037 SET_LEX_STATE(EXPR_BEG);
11038 if ((c = nextc(p)) == '.') {
11039 if ((c = nextc(p)) == '.') {
11040 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
11041 SET_LEX_STATE(EXPR_ENDARG);
11042 return tBDOT3;
11043 }
11044 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
11045 rb_warn0("... at EOL, should be parenthesized?");
11046 }
11047 return is_beg ? tBDOT3 : tDOT3;
11048 }
11049 pushback(p, c);
11050 return is_beg ? tBDOT2 : tDOT2;
11051 }
11052 pushback(p, c);
11053 if (c != -1 && ISDIGIT(c)) {
11054 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
11055 parse_numeric(p, '.');
11056 if (ISDIGIT(prev)) {
11057 yyerror0("unexpected fraction part after numeric literal");
11058 }
11059 else {
11060 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
11061 }
11062 SET_LEX_STATE(EXPR_END);
11063 p->lex.ptok = p->lex.pcur;
11064 goto retry;
11065 }
11066 set_yylval_id('.');
11067 SET_LEX_STATE(EXPR_DOT);
11068 return '.';
11069 }
11070
11071 case '0': case '1': case '2': case '3': case '4':
11072 case '5': case '6': case '7': case '8': case '9':
11073 return parse_numeric(p, c);
11074
11075 case ')':
11076 COND_POP();
11077 CMDARG_POP();
11078 SET_LEX_STATE(EXPR_ENDFN);
11079 p->lex.paren_nest--;
11080 return c;
11081
11082 case ']':
11083 COND_POP();
11084 CMDARG_POP();
11085 SET_LEX_STATE(EXPR_END);
11086 p->lex.paren_nest--;
11087 return c;
11088
11089 case '}':
11090 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
11091 if (!p->lex.brace_nest--) return tSTRING_DEND;
11092 COND_POP();
11093 CMDARG_POP();
11094 SET_LEX_STATE(EXPR_END);
11095 p->lex.paren_nest--;
11096 return c;
11097
11098 case ':':
11099 c = nextc(p);
11100 if (c == ':') {
11101 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
11102 SET_LEX_STATE(EXPR_BEG);
11103 return tCOLON3;
11104 }
11105 set_yylval_id(idCOLON2);
11106 SET_LEX_STATE(EXPR_DOT);
11107 return tCOLON2;
11108 }
11109 if (IS_END() || ISSPACE(c) || c == '#') {
11110 pushback(p, c);
11111 c = warn_balanced(':', ":", "symbol literal");
11112 SET_LEX_STATE(EXPR_BEG);
11113 return c;
11114 }
11115 switch (c) {
11116 case '\'':
11117 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11118 break;
11119 case '"':
11120 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11121 break;
11122 default:
11123 pushback(p, c);
11124 break;
11125 }
11126 SET_LEX_STATE(EXPR_FNAME);
11127 return tSYMBEG;
11128
11129 case '/':
11130 if (IS_BEG()) {
11131 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11132 return tREGEXP_BEG;
11133 }
11134 if ((c = nextc(p)) == '=') {
11135 set_yylval_id('/');
11136 SET_LEX_STATE(EXPR_BEG);
11137 return tOP_ASGN;
11138 }
11139 pushback(p, c);
11140 if (IS_SPCARG(c)) {
11141 arg_ambiguous(p, '/');
11142 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11143 return tREGEXP_BEG;
11144 }
11145 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11146 return warn_balanced('/', "/", "regexp literal");
11147
11148 case '^':
11149 if ((c = nextc(p)) == '=') {
11150 set_yylval_id('^');
11151 SET_LEX_STATE(EXPR_BEG);
11152 return tOP_ASGN;
11153 }
11154 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11155 pushback(p, c);
11156 return '^';
11157
11158 case ';':
11159 SET_LEX_STATE(EXPR_BEG);
11160 p->command_start = TRUE;
11161 return ';';
11162
11163 case ',':
11164 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11165 return ',';
11166
11167 case '~':
11168 if (IS_AFTER_OPERATOR()) {
11169 if ((c = nextc(p)) != '@') {
11170 pushback(p, c);
11171 }
11172 SET_LEX_STATE(EXPR_ARG);
11173 }
11174 else {
11175 SET_LEX_STATE(EXPR_BEG);
11176 }
11177 return '~';
11178
11179 case '(':
11180 if (IS_BEG()) {
11181 c = tLPAREN;
11182 }
11183 else if (!space_seen) {
11184 /* foo( ... ) => method call, no ambiguity */
11185 }
11186 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11187 c = tLPAREN_ARG;
11188 }
11189 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11190 rb_warning0("parentheses after method name is interpreted as "
11191 "an argument list, not a decomposed argument");
11192 }
11193 p->lex.paren_nest++;
11194 COND_PUSH(0);
11195 CMDARG_PUSH(0);
11196 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11197 return c;
11198
11199 case '[':
11200 p->lex.paren_nest++;
11201 if (IS_AFTER_OPERATOR()) {
11202 if ((c = nextc(p)) == ']') {
11203 p->lex.paren_nest--;
11204 SET_LEX_STATE(EXPR_ARG);
11205 if ((c = nextc(p)) == '=') {
11206 return tASET;
11207 }
11208 pushback(p, c);
11209 return tAREF;
11210 }
11211 pushback(p, c);
11212 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11213 return '[';
11214 }
11215 else if (IS_BEG()) {
11216 c = tLBRACK;
11217 }
11218 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11219 c = tLBRACK;
11220 }
11221 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11222 COND_PUSH(0);
11223 CMDARG_PUSH(0);
11224 return c;
11225
11226 case '{':
11227 ++p->lex.brace_nest;
11228 if (lambda_beginning_p())
11229 c = tLAMBEG;
11230 else if (IS_lex_state(EXPR_LABELED))
11231 c = tLBRACE; /* hash */
11232 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11233 c = '{'; /* block (primary) */
11234 else if (IS_lex_state(EXPR_ENDARG))
11235 c = tLBRACE_ARG; /* block (expr) */
11236 else
11237 c = tLBRACE; /* hash */
11238 if (c != tLBRACE) {
11239 p->command_start = TRUE;
11240 SET_LEX_STATE(EXPR_BEG);
11241 }
11242 else {
11243 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11244 }
11245 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11246 COND_PUSH(0);
11247 CMDARG_PUSH(0);
11248 return c;
11249
11250 case '\\':
11251 c = nextc(p);
11252 if (c == '\n') {
11253 space_seen = 1;
11254 dispatch_scan_event(p, tSP);
11255 goto retry; /* skip \\n */
11256 }
11257 if (c == ' ') return tSP;
11258 if (ISSPACE(c)) return c;
11259 pushback(p, c);
11260 return '\\';
11261
11262 case '%':
11263 return parse_percent(p, space_seen, last_state);
11264
11265 case '$':
11266 return parse_gvar(p, last_state);
11267
11268 case '@':
11269 return parse_atmark(p, last_state);
11270
11271 case '_':
11272 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11273 p->ruby__end__seen = 1;
11274 p->eofp = 1;
11275#ifdef RIPPER
11276 lex_goto_eol(p);
11277 dispatch_scan_event(p, k__END__);
11278#endif
11279 return END_OF_INPUT;
11280 }
11281 newtok(p);
11282 break;
11283
11284 default:
11285 if (!parser_is_identchar(p)) {
11286 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11287 token_flush(p);
11288 goto retry;
11289 }
11290
11291 newtok(p);
11292 break;
11293 }
11294
11295 return parse_ident(p, c, cmd_state);
11296}
11297
11298static enum yytokentype
11299yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11300{
11301 enum yytokentype t;
11302
11303 p->lval = lval;
11304 lval->node = 0;
11305 p->yylloc = yylloc;
11306
11307 t = parser_yylex(p);
11308
11309 if (has_delayed_token(p))
11310 dispatch_delayed_token(p, t);
11311 else if (t != END_OF_INPUT)
11312 dispatch_scan_event(p, t);
11313
11314 return t;
11315}
11316
11317#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11318
11319static NODE*
11320node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11321{
11322 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11323
11324 rb_node_init(n, type);
11325 return n;
11326}
11327
11328static NODE *
11329nd_set_loc(NODE *nd, const YYLTYPE *loc)
11330{
11331 nd->nd_loc = *loc;
11332 nd_set_line(nd, loc->beg_pos.lineno);
11333 return nd;
11334}
11335
11336static NODE*
11337node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11338{
11339 NODE *n = node_new_internal(p, type, size, alignment);
11340
11341 nd_set_loc(n, loc);
11342 nd_set_node_id(n, parser_get_node_id(p));
11343 return n;
11344}
11345
11346#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11347
11348static rb_node_scope_t *
11349rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11350{
11351 rb_ast_id_table_t *nd_tbl;
11352 nd_tbl = local_tbl(p);
11353 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11354 n->nd_tbl = nd_tbl;
11355 n->nd_body = nd_body;
11356 n->nd_args = nd_args;
11357
11358 return n;
11359}
11360
11361static rb_node_scope_t *
11362rb_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)
11363{
11364 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11365 n->nd_tbl = nd_tbl;
11366 n->nd_body = nd_body;
11367 n->nd_args = nd_args;
11368
11369 return n;
11370}
11371
11372static rb_node_defn_t *
11373rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11374{
11375 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11376 n->nd_mid = nd_mid;
11377 n->nd_defn = nd_defn;
11378
11379 return n;
11380}
11381
11382static rb_node_defs_t *
11383rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11384{
11385 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11386 n->nd_recv = nd_recv;
11387 n->nd_mid = nd_mid;
11388 n->nd_defn = nd_defn;
11389
11390 return n;
11391}
11392
11393static rb_node_block_t *
11394rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11395{
11396 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11397 n->nd_head = nd_head;
11398 n->nd_end = (NODE *)n;
11399 n->nd_next = 0;
11400
11401 return n;
11402}
11403
11404static rb_node_for_t *
11405rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc)
11406{
11407 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11408 n->nd_body = nd_body;
11409 n->nd_iter = nd_iter;
11410
11411 return n;
11412}
11413
11414static rb_node_for_masgn_t *
11415rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11416{
11417 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11418 n->nd_var = nd_var;
11419
11420 return n;
11421}
11422
11423static rb_node_retry_t *
11424rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11425{
11426 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11427
11428 return n;
11429}
11430
11431static rb_node_begin_t *
11432rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11433{
11434 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11435 n->nd_body = nd_body;
11436
11437 return n;
11438}
11439
11440static rb_node_rescue_t *
11441rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11442{
11443 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11444 n->nd_head = nd_head;
11445 n->nd_resq = nd_resq;
11446 n->nd_else = nd_else;
11447
11448 return n;
11449}
11450
11451static rb_node_resbody_t *
11452rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11453{
11454 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11455 n->nd_args = nd_args;
11456 n->nd_exc_var = nd_exc_var;
11457 n->nd_body = nd_body;
11458 n->nd_next = nd_next;
11459
11460 return n;
11461}
11462
11463static rb_node_ensure_t *
11464rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11465{
11466 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11467 n->nd_head = nd_head;
11468 n->nd_ensr = nd_ensr;
11469
11470 return n;
11471}
11472
11473static rb_node_and_t *
11474rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11475{
11476 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11477 n->nd_1st = nd_1st;
11478 n->nd_2nd = nd_2nd;
11479 n->operator_loc = *operator_loc;
11480
11481 return n;
11482}
11483
11484static rb_node_or_t *
11485rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11486{
11487 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11488 n->nd_1st = nd_1st;
11489 n->nd_2nd = nd_2nd;
11490 n->operator_loc = *operator_loc;
11491
11492 return n;
11493}
11494
11495static rb_node_return_t *
11496rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11497{
11498 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11499 n->nd_stts = nd_stts;
11500 n->keyword_loc = *keyword_loc;
11501 return n;
11502}
11503
11504static rb_node_yield_t *
11505rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11506{
11507 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11508 n->nd_head = nd_head;
11509
11510 return n;
11511}
11512
11513static rb_node_if_t *
11514rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11515{
11516 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11517 n->nd_cond = nd_cond;
11518 n->nd_body = nd_body;
11519 n->nd_else = nd_else;
11520
11521 return n;
11522}
11523
11524static rb_node_unless_t *
11525rb_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)
11526{
11527 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11528 n->nd_cond = nd_cond;
11529 n->nd_body = nd_body;
11530 n->nd_else = nd_else;
11531 n->keyword_loc = *keyword_loc;
11532 n->then_keyword_loc = *then_keyword_loc;
11533 n->end_keyword_loc = *end_keyword_loc;
11534
11535 return n;
11536}
11537
11538static rb_node_class_t *
11539rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc)
11540{
11541 /* Keep the order of node creation */
11542 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11543 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11544 n->nd_cpath = nd_cpath;
11545 n->nd_body = scope;
11546 n->nd_super = nd_super;
11547
11548 return n;
11549}
11550
11551static rb_node_sclass_t *
11552rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc)
11553{
11554 /* Keep the order of node creation */
11555 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11556 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11557 n->nd_recv = nd_recv;
11558 n->nd_body = scope;
11559
11560 return n;
11561}
11562
11563static rb_node_module_t *
11564rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc)
11565{
11566 /* Keep the order of node creation */
11567 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11568 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11569 n->nd_cpath = nd_cpath;
11570 n->nd_body = scope;
11571
11572 return n;
11573}
11574
11575static rb_node_iter_t *
11576rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11577{
11578 /* Keep the order of node creation */
11579 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11580 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11581 n->nd_body = scope;
11582 n->nd_iter = 0;
11583
11584 return n;
11585}
11586
11587static rb_node_lambda_t *
11588rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11589{
11590 /* Keep the order of node creation */
11591 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11592 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, loc);
11593 n->nd_body = scope;
11594
11595 return n;
11596}
11597
11598static rb_node_case_t *
11599rb_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)
11600{
11601 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11602 n->nd_head = nd_head;
11603 n->nd_body = nd_body;
11604 n->case_keyword_loc = *case_keyword_loc;
11605 n->end_keyword_loc = *end_keyword_loc;
11606
11607 return n;
11608}
11609
11610static rb_node_case2_t *
11611rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11612{
11613 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11614 n->nd_head = 0;
11615 n->nd_body = nd_body;
11616 n->case_keyword_loc = *case_keyword_loc;
11617 n->end_keyword_loc = *end_keyword_loc;
11618
11619 return n;
11620}
11621
11622static rb_node_case3_t *
11623rb_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)
11624{
11625 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11626 n->nd_head = nd_head;
11627 n->nd_body = nd_body;
11628 n->case_keyword_loc = *case_keyword_loc;
11629 n->end_keyword_loc = *end_keyword_loc;
11630
11631 return n;
11632}
11633
11634static rb_node_when_t *
11635rb_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)
11636{
11637 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11638 n->nd_head = nd_head;
11639 n->nd_body = nd_body;
11640 n->nd_next = nd_next;
11641 n->keyword_loc = *keyword_loc;
11642 n->then_keyword_loc = *then_keyword_loc;
11643
11644 return n;
11645}
11646
11647static rb_node_in_t *
11648rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11649{
11650 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11651 n->nd_head = nd_head;
11652 n->nd_body = nd_body;
11653 n->nd_next = nd_next;
11654
11655 return n;
11656}
11657
11658static rb_node_while_t *
11659rb_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)
11660{
11661 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11662 n->nd_cond = nd_cond;
11663 n->nd_body = nd_body;
11664 n->nd_state = nd_state;
11665 n->keyword_loc = *keyword_loc;
11666 n->closing_loc = *closing_loc;
11667
11668 return n;
11669}
11670
11671static rb_node_until_t *
11672rb_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)
11673{
11674 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11675 n->nd_cond = nd_cond;
11676 n->nd_body = nd_body;
11677 n->nd_state = nd_state;
11678 n->keyword_loc = *keyword_loc;
11679 n->closing_loc = *closing_loc;
11680
11681 return n;
11682}
11683
11684static rb_node_colon2_t *
11685rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc)
11686{
11687 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11688 n->nd_head = nd_head;
11689 n->nd_mid = nd_mid;
11690
11691 return n;
11692}
11693
11694static rb_node_colon3_t *
11695rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11696{
11697 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11698 n->nd_mid = nd_mid;
11699
11700 return n;
11701}
11702
11703static rb_node_dot2_t *
11704rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11705{
11706 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11707 n->nd_beg = nd_beg;
11708 n->nd_end = nd_end;
11709
11710 return n;
11711}
11712
11713static rb_node_dot3_t *
11714rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11715{
11716 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11717 n->nd_beg = nd_beg;
11718 n->nd_end = nd_end;
11719
11720 return n;
11721}
11722
11723static rb_node_self_t *
11724rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11725{
11726 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11727 n->nd_state = 1;
11728
11729 return n;
11730}
11731
11732static rb_node_nil_t *
11733rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11734{
11735 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11736
11737 return n;
11738}
11739
11740static rb_node_true_t *
11741rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11742{
11743 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11744
11745 return n;
11746}
11747
11748static rb_node_false_t *
11749rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11750{
11751 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11752
11753 return n;
11754}
11755
11756static rb_node_super_t *
11757rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc)
11758{
11759 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11760 n->nd_args = nd_args;
11761
11762 return n;
11763}
11764
11765static rb_node_zsuper_t *
11766rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11767{
11768 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11769
11770 return n;
11771}
11772
11773static rb_node_match2_t *
11774rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11775{
11776 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11777 n->nd_recv = nd_recv;
11778 n->nd_value = nd_value;
11779 n->nd_args = 0;
11780
11781 return n;
11782}
11783
11784static rb_node_match3_t *
11785rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11786{
11787 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11788 n->nd_recv = nd_recv;
11789 n->nd_value = nd_value;
11790
11791 return n;
11792}
11793
11794/* TODO: Use union for NODE_LIST2 */
11795static rb_node_list_t *
11796rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11797{
11798 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11799 n->nd_head = nd_head;
11800 n->as.nd_alen = 1;
11801 n->nd_next = 0;
11802
11803 return n;
11804}
11805
11806static rb_node_list_t *
11807rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11808{
11809 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11810 n->nd_head = nd_head;
11811 n->as.nd_alen = nd_alen;
11812 n->nd_next = nd_next;
11813
11814 return n;
11815}
11816
11817static rb_node_zlist_t *
11818rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11819{
11820 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11821
11822 return n;
11823}
11824
11825static rb_node_hash_t *
11826rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11827{
11828 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11829 n->nd_head = nd_head;
11830 n->nd_brace = 0;
11831
11832 return n;
11833}
11834
11835static rb_node_masgn_t *
11836rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11837{
11838 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11839 n->nd_head = nd_head;
11840 n->nd_value = 0;
11841 n->nd_args = nd_args;
11842
11843 return n;
11844}
11845
11846static rb_node_gasgn_t *
11847rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11848{
11849 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11850 n->nd_vid = nd_vid;
11851 n->nd_value = nd_value;
11852
11853 return n;
11854}
11855
11856static rb_node_lasgn_t *
11857rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11858{
11859 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11860 n->nd_vid = nd_vid;
11861 n->nd_value = nd_value;
11862
11863 return n;
11864}
11865
11866static rb_node_dasgn_t *
11867rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11868{
11869 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11870 n->nd_vid = nd_vid;
11871 n->nd_value = nd_value;
11872
11873 return n;
11874}
11875
11876static rb_node_iasgn_t *
11877rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11878{
11879 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11880 n->nd_vid = nd_vid;
11881 n->nd_value = nd_value;
11882
11883 return n;
11884}
11885
11886static rb_node_cvasgn_t *
11887rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11888{
11889 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11890 n->nd_vid = nd_vid;
11891 n->nd_value = nd_value;
11892
11893 return n;
11894}
11895
11896static rb_node_op_asgn1_t *
11897rb_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)
11898{
11899 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11900 n->nd_recv = nd_recv;
11901 n->nd_mid = nd_mid;
11902 n->nd_index = index;
11903 n->nd_rvalue = rvalue;
11904 n->call_operator_loc = *call_operator_loc;
11905 n->opening_loc = *opening_loc;
11906 n->closing_loc = *closing_loc;
11907 n->binary_operator_loc = *binary_operator_loc;
11908
11909 return n;
11910}
11911
11912static rb_node_op_asgn2_t *
11913rb_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)
11914{
11915 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11916 n->nd_recv = nd_recv;
11917 n->nd_value = nd_value;
11918 n->nd_vid = nd_vid;
11919 n->nd_mid = nd_mid;
11920 n->nd_aid = nd_aid;
11921 n->call_operator_loc = *call_operator_loc;
11922 n->message_loc = *message_loc;
11923 n->binary_operator_loc = *binary_operator_loc;
11924
11925 return n;
11926}
11927
11928static rb_node_op_asgn_or_t *
11929rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11930{
11931 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11932 n->nd_head = nd_head;
11933 n->nd_value = nd_value;
11934
11935 return n;
11936}
11937
11938static rb_node_op_asgn_and_t *
11939rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11940{
11941 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11942 n->nd_head = nd_head;
11943 n->nd_value = nd_value;
11944
11945 return n;
11946}
11947
11948static rb_node_gvar_t *
11949rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11950{
11951 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11952 n->nd_vid = nd_vid;
11953
11954 return n;
11955}
11956
11957static rb_node_lvar_t *
11958rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11959{
11960 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11961 n->nd_vid = nd_vid;
11962
11963 return n;
11964}
11965
11966static rb_node_dvar_t *
11967rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11968{
11969 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11970 n->nd_vid = nd_vid;
11971
11972 return n;
11973}
11974
11975static rb_node_ivar_t *
11976rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11977{
11978 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11979 n->nd_vid = nd_vid;
11980
11981 return n;
11982}
11983
11984static rb_node_const_t *
11985rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11986{
11987 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11988 n->nd_vid = nd_vid;
11989
11990 return n;
11991}
11992
11993static rb_node_cvar_t *
11994rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11995{
11996 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11997 n->nd_vid = nd_vid;
11998
11999 return n;
12000}
12001
12002static rb_node_nth_ref_t *
12003rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
12004{
12005 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
12006 n->nd_nth = nd_nth;
12007
12008 return n;
12009}
12010
12011static rb_node_back_ref_t *
12012rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
12013{
12014 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
12015 n->nd_nth = nd_nth;
12016
12017 return n;
12018}
12019
12020static rb_node_integer_t *
12021rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
12022{
12023 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
12024 n->val = val;
12025 n->minus = FALSE;
12026 n->base = base;
12027
12028 return n;
12029}
12030
12031static rb_node_float_t *
12032rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
12033{
12034 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
12035 n->val = val;
12036 n->minus = FALSE;
12037
12038 return n;
12039}
12040
12041static rb_node_rational_t *
12042rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
12043{
12044 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
12045 n->val = val;
12046 n->minus = FALSE;
12047 n->base = base;
12048 n->seen_point = seen_point;
12049
12050 return n;
12051}
12052
12053static rb_node_imaginary_t *
12054rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
12055{
12056 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
12057 n->val = val;
12058 n->minus = FALSE;
12059 n->base = base;
12060 n->seen_point = seen_point;
12061 n->type = numeric_type;
12062
12063 return n;
12064}
12065
12066static rb_node_str_t *
12067rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12068{
12069 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12070 n->string = string;
12071
12072 return n;
12073}
12074
12075/* TODO; Use union for NODE_DSTR2 */
12076static rb_node_dstr_t *
12077rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12078{
12079 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12080 n->string = string;
12081 n->as.nd_alen = nd_alen;
12082 n->nd_next = (rb_node_list_t *)nd_next;
12083
12084 return n;
12085}
12086
12087static rb_node_dstr_t *
12088rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12089{
12090 return rb_node_dstr_new0(p, string, 1, 0, loc);
12091}
12092
12093static rb_node_xstr_t *
12094rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12095{
12096 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12097 n->string = string;
12098
12099 return n;
12100}
12101
12102static rb_node_dxstr_t *
12103rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12104{
12105 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12106 n->string = string;
12107 n->as.nd_alen = nd_alen;
12108 n->nd_next = (rb_node_list_t *)nd_next;
12109
12110 return n;
12111}
12112
12113static rb_node_sym_t *
12114rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12115{
12116 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12117 n->string = rb_str_to_parser_string(p, str);
12118
12119 return n;
12120}
12121
12122static rb_node_dsym_t *
12123rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12124{
12125 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12126 n->string = string;
12127 n->as.nd_alen = nd_alen;
12128 n->nd_next = (rb_node_list_t *)nd_next;
12129
12130 return n;
12131}
12132
12133static rb_node_evstr_t *
12134rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12135{
12136 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12137 n->nd_body = nd_body;
12138
12139 return n;
12140}
12141
12142static rb_node_regx_t *
12143rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc)
12144{
12145 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12146 n->string = string;
12147 n->options = options & RE_OPTION_MASK;
12148
12149 return n;
12150}
12151
12152static rb_node_call_t *
12153rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12154{
12155 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12156 n->nd_recv = nd_recv;
12157 n->nd_mid = nd_mid;
12158 n->nd_args = nd_args;
12159
12160 return n;
12161}
12162
12163static rb_node_opcall_t *
12164rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12165{
12166 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12167 n->nd_recv = nd_recv;
12168 n->nd_mid = nd_mid;
12169 n->nd_args = nd_args;
12170
12171 return n;
12172}
12173
12174static rb_node_fcall_t *
12175rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12176{
12177 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12178 n->nd_mid = nd_mid;
12179 n->nd_args = nd_args;
12180
12181 return n;
12182}
12183
12184static rb_node_qcall_t *
12185rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12186{
12187 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12188 n->nd_recv = nd_recv;
12189 n->nd_mid = nd_mid;
12190 n->nd_args = nd_args;
12191
12192 return n;
12193}
12194
12195static rb_node_vcall_t *
12196rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12197{
12198 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12199 n->nd_mid = nd_mid;
12200
12201 return n;
12202}
12203
12204static rb_node_once_t *
12205rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12206{
12207 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12208 n->nd_body = nd_body;
12209
12210 return n;
12211}
12212
12213static rb_node_args_t *
12214rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12215{
12216 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12217 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12218
12219 return n;
12220}
12221
12222static rb_node_args_aux_t *
12223rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12224{
12225 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12226 n->nd_pid = nd_pid;
12227 n->nd_plen = nd_plen;
12228 n->nd_next = 0;
12229
12230 return n;
12231}
12232
12233static rb_node_opt_arg_t *
12234rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12235{
12236 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12237 n->nd_body = nd_body;
12238 n->nd_next = 0;
12239
12240 return n;
12241}
12242
12243static rb_node_kw_arg_t *
12244rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12245{
12246 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12247 n->nd_body = nd_body;
12248 n->nd_next = 0;
12249
12250 return n;
12251}
12252
12253static rb_node_postarg_t *
12254rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12255{
12256 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12257 n->nd_1st = nd_1st;
12258 n->nd_2nd = nd_2nd;
12259
12260 return n;
12261}
12262
12263static rb_node_argscat_t *
12264rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12265{
12266 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12267 n->nd_head = nd_head;
12268 n->nd_body = nd_body;
12269
12270 return n;
12271}
12272
12273static rb_node_argspush_t *
12274rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12275{
12276 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12277 n->nd_head = nd_head;
12278 n->nd_body = nd_body;
12279
12280 return n;
12281}
12282
12283static rb_node_splat_t *
12284rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12285{
12286 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12287 n->nd_head = nd_head;
12288 n->operator_loc = *operator_loc;
12289
12290 return n;
12291}
12292
12293static rb_node_block_pass_t *
12294rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12295{
12296 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12297 n->forwarding = 0;
12298 n->nd_head = 0;
12299 n->nd_body = nd_body;
12300 n->operator_loc = *operator_loc;
12301
12302 return n;
12303}
12304
12305static rb_node_alias_t *
12306rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12307{
12308 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12309 n->nd_1st = nd_1st;
12310 n->nd_2nd = nd_2nd;
12311 n->keyword_loc = *keyword_loc;
12312
12313 return n;
12314}
12315
12316static rb_node_valias_t *
12317rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12318{
12319 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12320 n->nd_alias = nd_alias;
12321 n->nd_orig = nd_orig;
12322 n->keyword_loc = *keyword_loc;
12323
12324 return n;
12325}
12326
12327static rb_node_undef_t *
12328rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12329{
12330 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12331 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12332 n->keyword_loc = NULL_LOC;
12333 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12334
12335 return n;
12336}
12337
12338static rb_node_errinfo_t *
12339rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12340{
12341 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12342
12343 return n;
12344}
12345
12346static rb_node_defined_t *
12347rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12348{
12349 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12350 n->nd_head = nd_head;
12351
12352 return n;
12353}
12354
12355static rb_node_postexe_t *
12356rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12357{
12358 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12359 n->nd_body = nd_body;
12360
12361 return n;
12362}
12363
12364static rb_node_attrasgn_t *
12365rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12366{
12367 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12368 n->nd_recv = nd_recv;
12369 n->nd_mid = nd_mid;
12370 n->nd_args = nd_args;
12371
12372 return n;
12373}
12374
12375static rb_node_aryptn_t *
12376rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12377{
12378 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12379 n->nd_pconst = 0;
12380 n->pre_args = pre_args;
12381 n->rest_arg = rest_arg;
12382 n->post_args = post_args;
12383
12384 return n;
12385}
12386
12387static rb_node_hshptn_t *
12388rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12389{
12390 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12391 n->nd_pconst = nd_pconst;
12392 n->nd_pkwargs = nd_pkwargs;
12393 n->nd_pkwrestarg = nd_pkwrestarg;
12394
12395 return n;
12396}
12397
12398static rb_node_fndptn_t *
12399rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12400{
12401 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12402 n->nd_pconst = 0;
12403 n->pre_rest_arg = pre_rest_arg;
12404 n->args = args;
12405 n->post_rest_arg = post_rest_arg;
12406
12407 return n;
12408}
12409
12410static rb_node_line_t *
12411rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12412{
12413 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12414
12415 return n;
12416}
12417
12418static rb_node_file_t *
12419rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12420{
12421 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12422 n->path = rb_str_to_parser_string(p, str);
12423
12424 return n;
12425}
12426
12427static rb_node_encoding_t *
12428rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12429{
12430 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12431 n->enc = p->enc;
12432
12433 return n;
12434}
12435
12436static rb_node_cdecl_t *
12437rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12438{
12439 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12440 n->nd_vid = nd_vid;
12441 n->nd_value = nd_value;
12442 n->nd_else = nd_else;
12443 n->shareability = shareability;
12444
12445 return n;
12446}
12447
12448static rb_node_op_cdecl_t *
12449rb_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)
12450{
12451 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12452 n->nd_head = nd_head;
12453 n->nd_value = nd_value;
12454 n->nd_aid = nd_aid;
12455 n->shareability = shareability;
12456
12457 return n;
12458}
12459
12460static rb_node_error_t *
12461rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12462{
12463 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12464
12465 return n;
12466}
12467
12468static rb_node_break_t *
12469rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12470{
12471 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12472 n->nd_stts = nd_stts;
12473 n->nd_chain = 0;
12474 n->keyword_loc = *keyword_loc;
12475
12476 return n;
12477}
12478
12479static rb_node_next_t *
12480rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12481{
12482 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12483 n->nd_stts = nd_stts;
12484 n->nd_chain = 0;
12485 n->keyword_loc = *keyword_loc;
12486
12487 return n;
12488}
12489
12490static rb_node_redo_t *
12491rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12492{
12493 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12494 n->nd_chain = 0;
12495 n->keyword_loc = *keyword_loc;
12496
12497 return n;
12498}
12499
12500static rb_node_def_temp_t *
12501rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12502{
12503 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12504 n->save.numparam_save = 0;
12505 n->save.max_numparam = 0;
12506 n->save.ctxt = p->ctxt;
12507 n->nd_def = 0;
12508 n->nd_mid = 0;
12509
12510 return n;
12511}
12512
12513static rb_node_def_temp_t *
12514def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12515{
12516 n->save.numparam_save = numparam_push(p);
12517 n->save.max_numparam = p->max_numparam;
12518 return n;
12519}
12520
12521#ifndef RIPPER
12522static enum node_type
12523nodetype(NODE *node) /* for debug */
12524{
12525 return (enum node_type)nd_type(node);
12526}
12527
12528static int
12529nodeline(NODE *node)
12530{
12531 return nd_line(node);
12532}
12533#endif
12534
12535static NODE*
12536newline_node(NODE *node)
12537{
12538 if (node) {
12539 node = remove_begin(node);
12540 nd_set_fl_newline(node);
12541 }
12542 return node;
12543}
12544
12545static void
12546fixpos(NODE *node, NODE *orig)
12547{
12548 if (!node) return;
12549 if (!orig) return;
12550 nd_set_line(node, nd_line(orig));
12551}
12552
12553static NODE*
12554block_append(struct parser_params *p, NODE *head, NODE *tail)
12555{
12556 NODE *end, *h = head, *nd;
12557
12558 if (tail == 0) return head;
12559
12560 if (h == 0) return tail;
12561 switch (nd_type(h)) {
12562 default:
12563 h = end = NEW_BLOCK(head, &head->nd_loc);
12564 head = end;
12565 break;
12566 case NODE_BLOCK:
12567 end = RNODE_BLOCK(h)->nd_end;
12568 break;
12569 }
12570
12571 nd = RNODE_BLOCK(end)->nd_head;
12572 switch (nd_type(nd)) {
12573 case NODE_RETURN:
12574 case NODE_BREAK:
12575 case NODE_NEXT:
12576 case NODE_REDO:
12577 case NODE_RETRY:
12578 rb_warning0L(nd_line(tail), "statement not reached");
12579 break;
12580
12581 default:
12582 break;
12583 }
12584
12585 if (!nd_type_p(tail, NODE_BLOCK)) {
12586 tail = NEW_BLOCK(tail, &tail->nd_loc);
12587 }
12588 RNODE_BLOCK(end)->nd_next = tail;
12589 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12590 nd_set_last_loc(head, nd_last_loc(tail));
12591 return head;
12592}
12593
12594/* append item to the list */
12595static NODE*
12596list_append(struct parser_params *p, NODE *list, NODE *item)
12597{
12598 NODE *last;
12599
12600 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12601 if (RNODE_LIST(list)->nd_next) {
12602 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12603 }
12604 else {
12605 last = list;
12606 }
12607
12608 RNODE_LIST(list)->as.nd_alen += 1;
12609 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12610 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12611
12612 nd_set_last_loc(list, nd_last_loc(item));
12613
12614 return list;
12615}
12616
12617/* concat two lists */
12618static NODE*
12619list_concat(NODE *head, NODE *tail)
12620{
12621 NODE *last;
12622
12623 if (RNODE_LIST(head)->nd_next) {
12624 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12625 }
12626 else {
12627 last = head;
12628 }
12629
12630 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12631 RNODE_LIST(last)->nd_next = tail;
12632 if (RNODE_LIST(tail)->nd_next) {
12633 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12634 }
12635 else {
12636 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12637 }
12638
12639 nd_set_last_loc(head, nd_last_loc(tail));
12640
12641 return head;
12642}
12643
12644static int
12645literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12646{
12647 if (!tail) return 1;
12648 if (!rb_parser_enc_compatible(p, head, tail)) {
12649 compile_error(p, "string literal encodings differ (%s / %s)",
12650 rb_enc_name(rb_parser_str_get_encoding(head)),
12651 rb_enc_name(rb_parser_str_get_encoding(tail)));
12652 rb_parser_str_resize(p, head, 0);
12653 rb_parser_str_resize(p, tail, 0);
12654 return 0;
12655 }
12656 rb_parser_str_buf_append(p, head, tail);
12657 return 1;
12658}
12659
12660static rb_parser_string_t *
12661string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12662{
12663 if (htype != NODE_DSTR) return NULL;
12664 if (RNODE_DSTR(head)->nd_next) {
12665 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12666 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12667 }
12668 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12669 ASSUME(lit);
12670 return lit;
12671}
12672
12673#ifndef RIPPER
12674static rb_parser_string_t *
12675rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12676{
12677 rb_parser_string_t *copy;
12678 if (!orig) return NULL;
12679 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12680 copy->coderange = orig->coderange;
12681 copy->enc = orig->enc;
12682 return copy;
12683}
12684#endif
12685
12686/* concat two string literals */
12687static NODE *
12688literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12689{
12690 enum node_type htype;
12691 rb_parser_string_t *lit;
12692
12693 if (!head) return tail;
12694 if (!tail) return head;
12695
12696 htype = nd_type(head);
12697 if (htype == NODE_EVSTR) {
12698 head = new_dstr(p, head, loc);
12699 htype = NODE_DSTR;
12700 }
12701 if (p->heredoc_indent > 0) {
12702 switch (htype) {
12703 case NODE_STR:
12704 head = str2dstr(p, head);
12705 case NODE_DSTR:
12706 return list_append(p, head, tail);
12707 default:
12708 break;
12709 }
12710 }
12711 switch (nd_type(tail)) {
12712 case NODE_STR:
12713 if ((lit = string_literal_head(p, htype, head)) != false) {
12714 htype = NODE_STR;
12715 }
12716 else {
12717 lit = RNODE_DSTR(head)->string;
12718 }
12719 if (htype == NODE_STR) {
12720 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12721 error:
12722 rb_discard_node(p, head);
12723 rb_discard_node(p, tail);
12724 return 0;
12725 }
12726 rb_discard_node(p, tail);
12727 }
12728 else {
12729 list_append(p, head, tail);
12730 }
12731 break;
12732
12733 case NODE_DSTR:
12734 if (htype == NODE_STR) {
12735 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12736 goto error;
12737 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12738 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12739 RNODE_STR(head)->string = NULL;
12740 rb_discard_node(p, head);
12741 head = tail;
12742 }
12743 else if (!RNODE_DSTR(tail)->string) {
12744 append:
12745 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12746 if (!RNODE_DSTR(head)->nd_next) {
12747 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12748 }
12749 else if (RNODE_DSTR(tail)->nd_next) {
12750 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12751 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12752 }
12753 rb_discard_node(p, tail);
12754 }
12755 else if ((lit = string_literal_head(p, htype, head)) != false) {
12756 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12757 goto error;
12758 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12759 RNODE_DSTR(tail)->string = 0;
12760 goto append;
12761 }
12762 else {
12763 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));
12764 RNODE_DSTR(tail)->string = 0;
12765 }
12766 break;
12767
12768 case NODE_EVSTR:
12769 if (htype == NODE_STR) {
12770 head = str2dstr(p, head);
12771 RNODE_DSTR(head)->as.nd_alen = 1;
12772 }
12773 list_append(p, head, tail);
12774 break;
12775 }
12776 return head;
12777}
12778
12779static void
12780nd_copy_flag(NODE *new_node, NODE *old_node)
12781{
12782 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12783 nd_set_line(new_node, nd_line(old_node));
12784 new_node->nd_loc = old_node->nd_loc;
12785 new_node->node_id = old_node->node_id;
12786}
12787
12788static NODE *
12789str2dstr(struct parser_params *p, NODE *node)
12790{
12791 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12792 nd_copy_flag(new_node, node);
12793 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12794 RNODE_DSTR(new_node)->as.nd_alen = 0;
12795 RNODE_DSTR(new_node)->nd_next = 0;
12796 RNODE_STR(node)->string = 0;
12797
12798 return new_node;
12799}
12800
12801static NODE *
12802str2regx(struct parser_params *p, NODE *node, int options)
12803{
12804 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12805 nd_copy_flag(new_node, node);
12806 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12807 RNODE_REGX(new_node)->options = options;
12808 RNODE_STR(node)->string = 0;
12809
12810 return new_node;
12811}
12812
12813static NODE *
12814evstr2dstr(struct parser_params *p, NODE *node)
12815{
12816 if (nd_type_p(node, NODE_EVSTR)) {
12817 node = new_dstr(p, node, &node->nd_loc);
12818 }
12819 return node;
12820}
12821
12822static NODE *
12823new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12824{
12825 NODE *head = node;
12826
12827 if (node) {
12828 switch (nd_type(node)) {
12829 case NODE_STR:
12830 return str2dstr(p, node);
12831 case NODE_DSTR:
12832 break;
12833 case NODE_EVSTR:
12834 return node;
12835 }
12836 }
12837 return NEW_EVSTR(head, loc);
12838}
12839
12840static NODE *
12841new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12842{
12843 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12844 return list_append(p, dstr, node);
12845}
12846
12847static NODE *
12848call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12849 const YYLTYPE *op_loc, const YYLTYPE *loc)
12850{
12851 NODE *expr;
12852 value_expr(recv);
12853 value_expr(arg1);
12854 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12855 nd_set_line(expr, op_loc->beg_pos.lineno);
12856 return expr;
12857}
12858
12859static NODE *
12860call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12861{
12862 NODE *opcall;
12863 value_expr(recv);
12864 opcall = NEW_OPCALL(recv, id, 0, loc);
12865 nd_set_line(opcall, op_loc->beg_pos.lineno);
12866 return opcall;
12867}
12868
12869static NODE *
12870new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12871{
12872 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12873 nd_set_line(qcall, op_loc->beg_pos.lineno);
12874 return qcall;
12875}
12876
12877static NODE*
12878new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12879{
12880 NODE *ret;
12881 if (block) block_dup_check(p, args, block);
12882 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12883 if (block) ret = method_add_block(p, ret, block, loc);
12884 fixpos(ret, recv);
12885 return ret;
12886}
12887
12888#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12889
12890static NODE*
12891last_expr_once_body(NODE *node)
12892{
12893 if (!node) return 0;
12894 return nd_once_body(node);
12895}
12896
12897static NODE*
12898match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12899{
12900 NODE *n;
12901 int line = op_loc->beg_pos.lineno;
12902
12903 value_expr(node1);
12904 value_expr(node2);
12905
12906 if ((n = last_expr_once_body(node1)) != 0) {
12907 switch (nd_type(n)) {
12908 case NODE_DREGX:
12909 {
12910 NODE *match = NEW_MATCH2(node1, node2, loc);
12911 nd_set_line(match, line);
12912 return match;
12913 }
12914
12915 case NODE_REGX:
12916 {
12917 const VALUE lit = rb_node_regx_string_val(n);
12918 if (!NIL_P(lit)) {
12919 NODE *match = NEW_MATCH2(node1, node2, loc);
12920 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12921 nd_set_line(match, line);
12922 return match;
12923 }
12924 }
12925 }
12926 }
12927
12928 if ((n = last_expr_once_body(node2)) != 0) {
12929 NODE *match3;
12930
12931 switch (nd_type(n)) {
12932 case NODE_DREGX:
12933 match3 = NEW_MATCH3(node2, node1, loc);
12934 return match3;
12935 }
12936 }
12937
12938 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12939 nd_set_line(n, line);
12940 return n;
12941}
12942
12943# if WARN_PAST_SCOPE
12944static int
12945past_dvar_p(struct parser_params *p, ID id)
12946{
12947 struct vtable *past = p->lvtbl->past;
12948 while (past) {
12949 if (vtable_included(past, id)) return 1;
12950 past = past->prev;
12951 }
12952 return 0;
12953}
12954# endif
12955
12956static int
12957numparam_nested_p(struct parser_params *p)
12958{
12959 struct local_vars *local = p->lvtbl;
12960 NODE *outer = local->numparam.outer;
12961 NODE *inner = local->numparam.inner;
12962 if (outer || inner) {
12963 NODE *used = outer ? outer : inner;
12964 compile_error(p, "numbered parameter is already used in\n"
12965 "%s:%d: %s block here",
12966 p->ruby_sourcefile, nd_line(used),
12967 outer ? "outer" : "inner");
12968 parser_show_error_line(p, &used->nd_loc);
12969 return 1;
12970 }
12971 return 0;
12972}
12973
12974static int
12975numparam_used_p(struct parser_params *p)
12976{
12977 NODE *numparam = p->lvtbl->numparam.current;
12978 if (numparam) {
12979 compile_error(p, "numbered parameter is already used in\n"
12980 "%s:%d: current block here",
12981 p->ruby_sourcefile, nd_line(numparam));
12982 parser_show_error_line(p, &numparam->nd_loc);
12983 return 1;
12984 }
12985 return 0;
12986}
12987
12988static int
12989it_used_p(struct parser_params *p)
12990{
12991 NODE *it = p->lvtbl->it;
12992 if (it) {
12993 compile_error(p, "'it' is already used in\n"
12994 "%s:%d: current block here",
12995 p->ruby_sourcefile, nd_line(it));
12996 parser_show_error_line(p, &it->nd_loc);
12997 return 1;
12998 }
12999 return 0;
13000}
13001
13002static NODE*
13003gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
13004{
13005 ID *vidp = NULL;
13006 NODE *node;
13007 switch (id) {
13008 case keyword_self:
13009 return NEW_SELF(loc);
13010 case keyword_nil:
13011 return NEW_NIL(loc);
13012 case keyword_true:
13013 return NEW_TRUE(loc);
13014 case keyword_false:
13015 return NEW_FALSE(loc);
13016 case keyword__FILE__:
13017 {
13018 VALUE file = p->ruby_sourcefile_string;
13019 if (NIL_P(file))
13020 file = rb_str_new(0, 0);
13021 node = NEW_FILE(file, loc);
13022 }
13023 return node;
13024 case keyword__LINE__:
13025 return NEW_LINE(loc);
13026 case keyword__ENCODING__:
13027 return NEW_ENCODING(loc);
13028
13029 }
13030 switch (id_type(id)) {
13031 case ID_LOCAL:
13032 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13033 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13034 if (vidp) *vidp |= LVAR_USED;
13035 node = NEW_DVAR(id, loc);
13036 return node;
13037 }
13038 if (local_id_ref(p, id, &vidp)) {
13039 if (vidp) *vidp |= LVAR_USED;
13040 node = NEW_LVAR(id, loc);
13041 return node;
13042 }
13043 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13044 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13045 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13046 node = NEW_DVAR(id, loc);
13047 struct local_vars *local = p->lvtbl;
13048 if (!local->numparam.current) local->numparam.current = node;
13049 return node;
13050 }
13051# if WARN_PAST_SCOPE
13052 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13053 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13054 }
13055# endif
13056 /* method call without arguments */
13057 if (dyna_in_block(p) && id == rb_intern("it") && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13058 if (numparam_used_p(p)) return 0;
13059 if (p->max_numparam == ORDINAL_PARAM) {
13060 compile_error(p, "ordinary parameter is defined");
13061 return 0;
13062 }
13063 if (!p->it_id) {
13064 p->it_id = internal_id(p);
13065 vtable_add(p->lvtbl->args, p->it_id);
13066 }
13067 NODE *node = NEW_DVAR(p->it_id, loc);
13068 if (!p->lvtbl->it) p->lvtbl->it = node;
13069 return node;
13070 }
13071 return NEW_VCALL(id, loc);
13072 case ID_GLOBAL:
13073 return NEW_GVAR(id, loc);
13074 case ID_INSTANCE:
13075 return NEW_IVAR(id, loc);
13076 case ID_CONST:
13077 return NEW_CONST(id, loc);
13078 case ID_CLASS:
13079 return NEW_CVAR(id, loc);
13080 }
13081 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13082 return 0;
13083}
13084
13085static rb_node_opt_arg_t *
13086opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13087{
13088 rb_node_opt_arg_t *opts = opt_list;
13089 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13090
13091 while (opts->nd_next) {
13092 opts = opts->nd_next;
13093 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13094 }
13095 opts->nd_next = opt;
13096
13097 return opt_list;
13098}
13099
13100static rb_node_kw_arg_t *
13101kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13102{
13103 if (kwlist) {
13104 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13105 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13106 }
13107 return kwlist;
13108}
13109
13110static NODE *
13111new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
13112{
13113 NODE *n = expr;
13114 while (n) {
13115 if (nd_type_p(n, NODE_BEGIN)) {
13116 n = RNODE_BEGIN(n)->nd_body;
13117 }
13118 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13119 n = RNODE_BLOCK(n)->nd_head;
13120 }
13121 else {
13122 break;
13123 }
13124 }
13125 return NEW_DEFINED(n, loc);
13126}
13127
13128static NODE*
13129str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13130{
13131 VALUE lit;
13132 rb_parser_string_t *str = RNODE_STR(node)->string;
13133 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13134 yyerror1(loc, "invalid symbol");
13135 lit = STR_NEW0();
13136 }
13137 else {
13138 lit = rb_str_new_parser_string(str);
13139 }
13140 return NEW_SYM(lit, loc);
13141}
13142
13143static NODE*
13144symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13145{
13146 enum node_type type = nd_type(symbol);
13147 switch (type) {
13148 case NODE_DSTR:
13149 nd_set_type(symbol, NODE_DSYM);
13150 break;
13151 case NODE_STR:
13152 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13153 break;
13154 default:
13155 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13156 }
13157 return list_append(p, symbols, symbol);
13158}
13159
13160static NODE *
13161new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
13162{
13163 struct RNode_LIST *list;
13164 NODE *prev;
13165
13166 if (!node) {
13167 /* Check string is valid regex */
13168 rb_parser_string_t *str = STRING_NEW0();
13169 reg_compile(p, str, options);
13170 node = NEW_REGX(str, options, loc);
13171 return node;
13172 }
13173 switch (nd_type(node)) {
13174 case NODE_STR:
13175 {
13176 /* Check string is valid regex */
13177 reg_compile(p, RNODE_STR(node)->string, options);
13178 node = str2regx(p, node, options);
13179 }
13180 break;
13181 default:
13182 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13183 /* fall through */
13184 case NODE_DSTR:
13185 nd_set_type(node, NODE_DREGX);
13186 nd_set_loc(node, loc);
13187 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13188 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13189 if (dreg->string) reg_fragment_check(p, dreg->string, options);
13190 prev = node;
13191 for (list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13192 NODE *frag = list->nd_head;
13193 enum node_type type = nd_type(frag);
13194 if (type == NODE_STR || (type == NODE_DSTR && !RNODE_DSTR(frag)->nd_next)) {
13195 rb_parser_string_t *tail = RNODE_STR(frag)->string;
13196 if (reg_fragment_check(p, tail, options) && prev && RNODE_DREGX(prev)->string) {
13197 rb_parser_string_t *lit = prev == node ? dreg->string : RNODE_STR(RNODE_LIST(prev)->nd_head)->string;
13198 if (!literal_concat0(p, lit, tail)) {
13199 return NEW_NIL(loc); /* dummy node on error */
13200 }
13201 rb_parser_str_resize(p, tail, 0);
13202 RNODE_LIST(prev)->nd_next = list->nd_next;
13203 rb_discard_node(p, list->nd_head);
13204 rb_discard_node(p, (NODE *)list);
13205 list = RNODE_LIST(prev);
13206 }
13207 else {
13208 prev = (NODE *)list;
13209 }
13210 }
13211 else {
13212 prev = 0;
13213 }
13214 }
13215 if (!dreg->nd_next) {
13216 /* Check string is valid regex */
13217 reg_compile(p, dreg->string, options);
13218 }
13219 if (options & RE_OPTION_ONCE) {
13220 node = NEW_ONCE(node, loc);
13221 }
13222 break;
13223 }
13224 return node;
13225}
13226
13227static rb_node_kw_arg_t *
13228new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13229{
13230 if (!k) return 0;
13231 return NEW_KW_ARG((k), loc);
13232}
13233
13234static NODE *
13235new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13236{
13237 if (!node) {
13238 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13239 return xstr;
13240 }
13241 switch (nd_type(node)) {
13242 case NODE_STR:
13243 nd_set_type(node, NODE_XSTR);
13244 nd_set_loc(node, loc);
13245 break;
13246 case NODE_DSTR:
13247 nd_set_type(node, NODE_DXSTR);
13248 nd_set_loc(node, loc);
13249 break;
13250 default:
13251 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13252 break;
13253 }
13254 return node;
13255}
13256
13257static const
13258struct st_hash_type literal_type = {
13259 literal_cmp,
13260 literal_hash,
13261};
13262
13263static int nd_type_st_key_enable_p(NODE *node);
13264
13265static void
13266check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13267{
13268 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13269 if (!arg || !p->case_labels) return;
13270 if (!nd_type_st_key_enable_p(arg)) return;
13271
13272 if (p->case_labels == CHECK_LITERAL_WHEN) {
13273 p->case_labels = st_init_table(&literal_type);
13274 }
13275 else {
13276 st_data_t line;
13277 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13278 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13279 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13280 return;
13281 }
13282 }
13283 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13284}
13285
13286#ifdef RIPPER
13287static int
13288id_is_var(struct parser_params *p, ID id)
13289{
13290 if (is_notop_id(id)) {
13291 switch (id & ID_SCOPE_MASK) {
13292 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13293 return 1;
13294 case ID_LOCAL:
13295 if (dyna_in_block(p)) {
13296 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13297 }
13298 if (local_id(p, id)) return 1;
13299 /* method call without arguments */
13300 return 0;
13301 }
13302 }
13303 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13304 return 0;
13305}
13306#endif
13307
13308static inline enum lex_state_e
13309parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13310{
13311 if (p->debug) {
13312 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13313 }
13314 return p->lex.state = ls;
13315}
13316
13317#ifndef RIPPER
13318static void
13319flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13320{
13321 VALUE mesg = p->debug_buffer;
13322
13323 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13324 p->debug_buffer = Qnil;
13325 rb_io_puts(1, &mesg, out);
13326 }
13327 if (!NIL_P(str) && RSTRING_LEN(str)) {
13328 rb_io_write(p->debug_output, str);
13329 }
13330}
13331
13332static const char rb_parser_lex_state_names[][8] = {
13333 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13334 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13335 "LABEL", "LABELED","FITEM",
13336};
13337
13338static VALUE
13339append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13340{
13341 int i, sep = 0;
13342 unsigned int mask = 1;
13343 static const char none[] = "NONE";
13344
13345 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13346 if ((unsigned)state & mask) {
13347 if (sep) {
13348 rb_str_cat(buf, "|", 1);
13349 }
13350 sep = 1;
13351 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13352 }
13353 }
13354 if (!sep) {
13355 rb_str_cat(buf, none, sizeof(none)-1);
13356 }
13357 return buf;
13358}
13359
13360enum lex_state_e
13361rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13362 enum lex_state_e to, int line)
13363{
13364 VALUE mesg;
13365 mesg = rb_str_new_cstr("lex_state: ");
13366 append_lex_state_name(p, from, mesg);
13367 rb_str_cat_cstr(mesg, " -> ");
13368 append_lex_state_name(p, to, mesg);
13369 rb_str_catf(mesg, " at line %d\n", line);
13370 flush_debug_buffer(p, p->debug_output, mesg);
13371 return to;
13372}
13373
13374VALUE
13375rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13376{
13377 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13378}
13379
13380static void
13381append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13382{
13383 if (stack == 0) {
13384 rb_str_cat_cstr(mesg, "0");
13385 }
13386 else {
13387 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13388 for (; mask && !(stack & mask); mask >>= 1) continue;
13389 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13390 }
13391}
13392
13393void
13394rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13395 const char *name, int line)
13396{
13397 VALUE mesg = rb_sprintf("%s: ", name);
13398 append_bitstack_value(p, stack, mesg);
13399 rb_str_catf(mesg, " at line %d\n", line);
13400 flush_debug_buffer(p, p->debug_output, mesg);
13401}
13402
13403void
13404rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13405{
13406 va_list ap;
13407 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13408
13409 va_start(ap, fmt);
13410 rb_str_vcatf(mesg, fmt, ap);
13411 va_end(ap);
13412 yyerror0(RSTRING_PTR(mesg));
13413 RB_GC_GUARD(mesg);
13414
13415 mesg = rb_str_new(0, 0);
13416 append_lex_state_name(p, p->lex.state, mesg);
13417 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13418 rb_str_resize(mesg, 0);
13419 append_bitstack_value(p, p->cond_stack, mesg);
13420 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13421 rb_str_resize(mesg, 0);
13422 append_bitstack_value(p, p->cmdarg_stack, mesg);
13423 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13424 if (p->debug_output == rb_ractor_stdout())
13425 p->debug_output = rb_ractor_stderr();
13426 p->debug = TRUE;
13427}
13428
13429static YYLTYPE *
13430rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13431{
13432 yylloc->beg_pos.lineno = sourceline;
13433 yylloc->beg_pos.column = beg_pos;
13434 yylloc->end_pos.lineno = sourceline;
13435 yylloc->end_pos.column = end_pos;
13436 return yylloc;
13437}
13438
13439YYLTYPE *
13440rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13441{
13442 int sourceline = here->sourceline;
13443 int beg_pos = (int)here->offset - here->quote
13444 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13445 int end_pos = (int)here->offset + here->length + here->quote;
13446
13447 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13448}
13449
13450YYLTYPE *
13451rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13452{
13453 yylloc->beg_pos.lineno = p->delayed.beg_line;
13454 yylloc->beg_pos.column = p->delayed.beg_col;
13455 yylloc->end_pos.lineno = p->delayed.end_line;
13456 yylloc->end_pos.column = p->delayed.end_col;
13457
13458 return yylloc;
13459}
13460
13461YYLTYPE *
13462rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13463{
13464 int sourceline = p->ruby_sourceline;
13465 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13466 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13467 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13468}
13469
13470YYLTYPE *
13471rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13472{
13473 yylloc->end_pos = yylloc->beg_pos;
13474
13475 return yylloc;
13476}
13477
13478YYLTYPE *
13479rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13480{
13481 int sourceline = p->ruby_sourceline;
13482 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13483 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13484 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13485}
13486
13487YYLTYPE *
13488rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13489{
13490 int sourceline = p->ruby_sourceline;
13491 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13492 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13493 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13494}
13495#endif /* !RIPPER */
13496
13497static int
13498assignable0(struct parser_params *p, ID id, const char **err)
13499{
13500 if (!id) return -1;
13501 switch (id) {
13502 case keyword_self:
13503 *err = "Can't change the value of self";
13504 return -1;
13505 case keyword_nil:
13506 *err = "Can't assign to nil";
13507 return -1;
13508 case keyword_true:
13509 *err = "Can't assign to true";
13510 return -1;
13511 case keyword_false:
13512 *err = "Can't assign to false";
13513 return -1;
13514 case keyword__FILE__:
13515 *err = "Can't assign to __FILE__";
13516 return -1;
13517 case keyword__LINE__:
13518 *err = "Can't assign to __LINE__";
13519 return -1;
13520 case keyword__ENCODING__:
13521 *err = "Can't assign to __ENCODING__";
13522 return -1;
13523 }
13524 switch (id_type(id)) {
13525 case ID_LOCAL:
13526 if (dyna_in_block(p)) {
13527 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13528 compile_error(p, "Can't assign to numbered parameter _%d",
13529 NUMPARAM_ID_TO_IDX(id));
13530 return -1;
13531 }
13532 if (dvar_curr(p, id)) return NODE_DASGN;
13533 if (dvar_defined(p, id)) return NODE_DASGN;
13534 if (local_id(p, id)) return NODE_LASGN;
13535 dyna_var(p, id);
13536 return NODE_DASGN;
13537 }
13538 else {
13539 if (!local_id(p, id)) local_var(p, id);
13540 return NODE_LASGN;
13541 }
13542 break;
13543 case ID_GLOBAL: return NODE_GASGN;
13544 case ID_INSTANCE: return NODE_IASGN;
13545 case ID_CONST:
13546 if (!p->ctxt.in_def) return NODE_CDECL;
13547 *err = "dynamic constant assignment";
13548 return -1;
13549 case ID_CLASS: return NODE_CVASGN;
13550 default:
13551 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13552 }
13553 return -1;
13554}
13555
13556static NODE*
13557assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13558{
13559 const char *err = 0;
13560 int node_type = assignable0(p, id, &err);
13561 switch (node_type) {
13562 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13563 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13564 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13565 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13566 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13567 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13568 }
13569/* TODO: FIXME */
13570#ifndef RIPPER
13571 if (err) yyerror1(loc, err);
13572#else
13573 if (err) set_value(assign_error(p, err, p->s_lvalue));
13574#endif
13575 return NEW_ERROR(loc);
13576}
13577
13578static int
13579is_private_local_id(struct parser_params *p, ID name)
13580{
13581 VALUE s;
13582 if (name == idUScore) return 1;
13583 if (!is_local_id(name)) return 0;
13584 s = rb_id2str(name);
13585 if (!s) return 0;
13586 return RSTRING_PTR(s)[0] == '_';
13587}
13588
13589static int
13590shadowing_lvar_0(struct parser_params *p, ID name)
13591{
13592 if (dyna_in_block(p)) {
13593 if (dvar_curr(p, name)) {
13594 if (is_private_local_id(p, name)) return 1;
13595 yyerror0("duplicated argument name");
13596 }
13597 else if (dvar_defined(p, name) || local_id(p, name)) {
13598 vtable_add(p->lvtbl->vars, name);
13599 if (p->lvtbl->used) {
13600 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13601 }
13602 return 0;
13603 }
13604 }
13605 else {
13606 if (local_id(p, name)) {
13607 if (is_private_local_id(p, name)) return 1;
13608 yyerror0("duplicated argument name");
13609 }
13610 }
13611 return 1;
13612}
13613
13614static ID
13615shadowing_lvar(struct parser_params *p, ID name)
13616{
13617 shadowing_lvar_0(p, name);
13618 return name;
13619}
13620
13621static void
13622new_bv(struct parser_params *p, ID name)
13623{
13624 if (!name) return;
13625 if (!is_local_id(name)) {
13626 compile_error(p, "invalid local variable - %"PRIsVALUE,
13627 rb_id2str(name));
13628 return;
13629 }
13630 if (!shadowing_lvar_0(p, name)) return;
13631 dyna_var(p, name);
13632 ID *vidp = 0;
13633 if (dvar_defined_ref(p, name, &vidp)) {
13634 if (vidp) *vidp |= LVAR_USED;
13635 }
13636}
13637
13638static void
13639aryset_check(struct parser_params *p, NODE *args)
13640{
13641 NODE *block = 0, *kwds = 0;
13642 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13643 block = RNODE_BLOCK_PASS(args)->nd_body;
13644 args = RNODE_BLOCK_PASS(args)->nd_head;
13645 }
13646 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13647 args = RNODE_ARGSCAT(args)->nd_body;
13648 }
13649 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13650 kwds = RNODE_ARGSPUSH(args)->nd_body;
13651 }
13652 else {
13653 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13654 next = RNODE_LIST(next)->nd_next) {
13655 kwds = RNODE_LIST(next)->nd_head;
13656 }
13657 }
13658 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13659 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13660 }
13661 if (block) {
13662 yyerror1(&block->nd_loc, "block arg given in index assignment");
13663 }
13664}
13665
13666static NODE *
13667aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13668{
13669 aryset_check(p, idx);
13670 return NEW_ATTRASGN(recv, tASET, idx, loc);
13671}
13672
13673static void
13674block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13675{
13676 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13677 compile_error(p, "both block arg and actual block given");
13678 }
13679}
13680
13681static NODE *
13682attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13683{
13684 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13685 return NEW_ATTRASGN(recv, id, 0, loc);
13686}
13687
13688static VALUE
13689rb_backref_error(struct parser_params *p, NODE *node)
13690{
13691#ifndef RIPPER
13692# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13693#else
13694# define ERR(...) rb_sprintf(__VA_ARGS__)
13695#endif
13696 switch (nd_type(node)) {
13697 case NODE_NTH_REF:
13698 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13699 case NODE_BACK_REF:
13700 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13701 }
13702#undef ERR
13703 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13704}
13705
13706static NODE *
13707arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13708{
13709 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13710 switch (nd_type(node1)) {
13711 case NODE_LIST:
13712 return list_append(p, node1, node2);
13713 case NODE_BLOCK_PASS:
13714 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13715 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13716 return node1;
13717 case NODE_ARGSPUSH:
13718 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13719 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13720 nd_set_type(node1, NODE_ARGSCAT);
13721 return node1;
13722 case NODE_ARGSCAT:
13723 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13724 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13725 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13726 return node1;
13727 }
13728 return NEW_ARGSPUSH(node1, node2, loc);
13729}
13730
13731static NODE *
13732arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13733{
13734 if (!node2) return node1;
13735 switch (nd_type(node1)) {
13736 case NODE_BLOCK_PASS:
13737 if (RNODE_BLOCK_PASS(node1)->nd_head)
13738 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13739 else
13740 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13741 return node1;
13742 case NODE_ARGSPUSH:
13743 if (!nd_type_p(node2, NODE_LIST)) break;
13744 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13745 nd_set_type(node1, NODE_ARGSCAT);
13746 return node1;
13747 case NODE_ARGSCAT:
13748 if (!nd_type_p(node2, NODE_LIST) ||
13749 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13750 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13751 return node1;
13752 }
13753 return NEW_ARGSCAT(node1, node2, loc);
13754}
13755
13756static NODE *
13757last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13758{
13759 NODE *n1;
13760 if ((n1 = splat_array(args)) != 0) {
13761 return list_append(p, n1, last_arg);
13762 }
13763 return arg_append(p, args, last_arg, loc);
13764}
13765
13766static NODE *
13767rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13768{
13769 NODE *n1;
13770 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13771 return list_concat(n1, rest_arg);
13772 }
13773 return arg_concat(p, args, rest_arg, loc);
13774}
13775
13776static NODE *
13777splat_array(NODE* node)
13778{
13779 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13780 if (nd_type_p(node, NODE_LIST)) return node;
13781 return 0;
13782}
13783
13784static void
13785mark_lvar_used(struct parser_params *p, NODE *rhs)
13786{
13787 ID *vidp = NULL;
13788 if (!rhs) return;
13789 switch (nd_type(rhs)) {
13790 case NODE_LASGN:
13791 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13792 if (vidp) *vidp |= LVAR_USED;
13793 }
13794 break;
13795 case NODE_DASGN:
13796 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13797 if (vidp) *vidp |= LVAR_USED;
13798 }
13799 break;
13800#if 0
13801 case NODE_MASGN:
13802 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13803 mark_lvar_used(p, rhs->nd_head);
13804 }
13805 break;
13806#endif
13807 }
13808}
13809
13810static int is_static_content(NODE *node);
13811
13812static NODE *
13813node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13814{
13815 if (!lhs) return 0;
13816
13817 switch (nd_type(lhs)) {
13818 case NODE_CDECL:
13819 case NODE_GASGN:
13820 case NODE_IASGN:
13821 case NODE_LASGN:
13822 case NODE_DASGN:
13823 case NODE_MASGN:
13824 case NODE_CVASGN:
13825 set_nd_value(p, lhs, rhs);
13826 nd_set_loc(lhs, loc);
13827 break;
13828
13829 case NODE_ATTRASGN:
13830 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13831 nd_set_loc(lhs, loc);
13832 break;
13833
13834 default:
13835 /* should not happen */
13836 break;
13837 }
13838
13839 return lhs;
13840}
13841
13842static NODE *
13843value_expr_check(struct parser_params *p, NODE *node)
13844{
13845 NODE *void_node = 0, *vn;
13846
13847 if (!node) {
13848 rb_warning0("empty expression");
13849 }
13850 while (node) {
13851 switch (nd_type(node)) {
13852 case NODE_ENSURE:
13853 vn = RNODE_ENSURE(node)->nd_head;
13854 node = RNODE_ENSURE(node)->nd_ensr;
13855 /* nd_ensr should not be NULL, check it out next */
13856 if (vn && (vn = value_expr_check(p, vn))) {
13857 goto found;
13858 }
13859 break;
13860
13861 case NODE_RESCUE:
13862 /* void only if all children are void */
13863 vn = RNODE_RESCUE(node)->nd_head;
13864 if (!vn || !(vn = value_expr_check(p, vn))) return NULL;
13865 if (!void_node) void_node = vn;
13866 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13867 if (!nd_type_p(r, NODE_RESBODY)) {
13868 compile_error(p, "unexpected node");
13869 return NULL;
13870 }
13871 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13872 void_node = 0;
13873 break;
13874 }
13875 if (!void_node) void_node = vn;
13876 }
13877 node = RNODE_RESCUE(node)->nd_else;
13878 if (!node) return void_node;
13879 break;
13880
13881 case NODE_RETURN:
13882 case NODE_BREAK:
13883 case NODE_NEXT:
13884 case NODE_REDO:
13885 case NODE_RETRY:
13886 goto found;
13887
13888 case NODE_CASE3:
13889 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13890 compile_error(p, "unexpected node");
13891 return NULL;
13892 }
13893 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13894 return NULL;
13895 }
13896 /* single line pattern matching with "=>" operator */
13897 goto found;
13898
13899 case NODE_BLOCK:
13900 while (RNODE_BLOCK(node)->nd_next) {
13901 node = RNODE_BLOCK(node)->nd_next;
13902 }
13903 node = RNODE_BLOCK(node)->nd_head;
13904 break;
13905
13906 case NODE_BEGIN:
13907 node = RNODE_BEGIN(node)->nd_body;
13908 break;
13909
13910 case NODE_IF:
13911 case NODE_UNLESS:
13912 if (!RNODE_IF(node)->nd_body) {
13913 return NULL;
13914 }
13915 else if (!RNODE_IF(node)->nd_else) {
13916 return NULL;
13917 }
13918 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13919 if (!vn) return NULL;
13920 if (!void_node) void_node = vn;
13921 node = RNODE_IF(node)->nd_else;
13922 break;
13923
13924 case NODE_AND:
13925 case NODE_OR:
13926 node = RNODE_AND(node)->nd_1st;
13927 break;
13928
13929 case NODE_LASGN:
13930 case NODE_DASGN:
13931 case NODE_MASGN:
13932 mark_lvar_used(p, node);
13933 return NULL;
13934
13935 default:
13936 return NULL;
13937 }
13938 }
13939
13940 return NULL;
13941
13942 found:
13943 /* return the first found node */
13944 return void_node ? void_node : node;
13945}
13946
13947static int
13948value_expr_gen(struct parser_params *p, NODE *node)
13949{
13950 NODE *void_node = value_expr_check(p, node);
13951 if (void_node) {
13952 yyerror1(&void_node->nd_loc, "void value expression");
13953 /* or "control never reach"? */
13954 return FALSE;
13955 }
13956 return TRUE;
13957}
13958
13959static void
13960void_expr(struct parser_params *p, NODE *node)
13961{
13962 const char *useless = 0;
13963
13964 if (!RTEST(ruby_verbose)) return;
13965
13966 if (!node || !(node = nd_once_body(node))) return;
13967 switch (nd_type(node)) {
13968 case NODE_OPCALL:
13969 switch (RNODE_OPCALL(node)->nd_mid) {
13970 case '+':
13971 case '-':
13972 case '*':
13973 case '/':
13974 case '%':
13975 case tPOW:
13976 case tUPLUS:
13977 case tUMINUS:
13978 case '|':
13979 case '^':
13980 case '&':
13981 case tCMP:
13982 case '>':
13983 case tGEQ:
13984 case '<':
13985 case tLEQ:
13986 case tEQ:
13987 case tNEQ:
13988 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13989 break;
13990 }
13991 break;
13992
13993 case NODE_LVAR:
13994 case NODE_DVAR:
13995 case NODE_GVAR:
13996 case NODE_IVAR:
13997 case NODE_CVAR:
13998 case NODE_NTH_REF:
13999 case NODE_BACK_REF:
14000 useless = "a variable";
14001 break;
14002 case NODE_CONST:
14003 useless = "a constant";
14004 break;
14005 case NODE_SYM:
14006 case NODE_LINE:
14007 case NODE_FILE:
14008 case NODE_ENCODING:
14009 case NODE_INTEGER:
14010 case NODE_FLOAT:
14011 case NODE_RATIONAL:
14012 case NODE_IMAGINARY:
14013 case NODE_STR:
14014 case NODE_DSTR:
14015 case NODE_REGX:
14016 case NODE_DREGX:
14017 useless = "a literal";
14018 break;
14019 case NODE_COLON2:
14020 case NODE_COLON3:
14021 useless = "::";
14022 break;
14023 case NODE_DOT2:
14024 useless = "..";
14025 break;
14026 case NODE_DOT3:
14027 useless = "...";
14028 break;
14029 case NODE_SELF:
14030 useless = "self";
14031 break;
14032 case NODE_NIL:
14033 useless = "nil";
14034 break;
14035 case NODE_TRUE:
14036 useless = "true";
14037 break;
14038 case NODE_FALSE:
14039 useless = "false";
14040 break;
14041 case NODE_DEFINED:
14042 useless = "defined?";
14043 break;
14044 }
14045
14046 if (useless) {
14047 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14048 }
14049}
14050
14051/* warns useless use of block and returns the last statement node */
14052static NODE *
14053void_stmts(struct parser_params *p, NODE *node)
14054{
14055 NODE *const n = node;
14056 if (!RTEST(ruby_verbose)) return n;
14057 if (!node) return n;
14058 if (!nd_type_p(node, NODE_BLOCK)) return n;
14059
14060 while (RNODE_BLOCK(node)->nd_next) {
14061 void_expr(p, RNODE_BLOCK(node)->nd_head);
14062 node = RNODE_BLOCK(node)->nd_next;
14063 }
14064 return RNODE_BLOCK(node)->nd_head;
14065}
14066
14067static NODE *
14068remove_begin(NODE *node)
14069{
14070 NODE **n = &node, *n1 = node;
14071 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14072 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14073 }
14074 return node;
14075}
14076
14077static void
14078reduce_nodes(struct parser_params *p, NODE **body)
14079{
14080 NODE *node = *body;
14081
14082 if (!node) {
14083 *body = NEW_NIL(&NULL_LOC);
14084 return;
14085 }
14086#define subnodes(type, n1, n2) \
14087 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14088 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14089 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14090
14091 while (node) {
14092 int newline = (int)nd_fl_newline(node);
14093 switch (nd_type(node)) {
14094 end:
14095 case NODE_NIL:
14096 *body = 0;
14097 return;
14098 case NODE_BEGIN:
14099 *body = node = RNODE_BEGIN(node)->nd_body;
14100 if (newline && node) nd_set_fl_newline(node);
14101 continue;
14102 case NODE_BLOCK:
14103 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14104 break;
14105 case NODE_IF:
14106 case NODE_UNLESS:
14107 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14108 return;
14109 case NODE_CASE:
14110 body = &RNODE_CASE(node)->nd_body;
14111 break;
14112 case NODE_WHEN:
14113 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14114 break;
14115 case NODE_ENSURE:
14116 body = &RNODE_ENSURE(node)->nd_head;
14117 break;
14118 case NODE_RESCUE:
14119 newline = 0; // RESBODY should not be a NEWLINE
14120 if (RNODE_RESCUE(node)->nd_else) {
14121 body = &RNODE_RESCUE(node)->nd_resq;
14122 break;
14123 }
14124 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14125 break;
14126 default:
14127 return;
14128 }
14129 node = *body;
14130 if (newline && node) nd_set_fl_newline(node);
14131 }
14132
14133#undef subnodes
14134}
14135
14136static int
14137is_static_content(NODE *node)
14138{
14139 if (!node) return 1;
14140 switch (nd_type(node)) {
14141 case NODE_HASH:
14142 if (!(node = RNODE_HASH(node)->nd_head)) break;
14143 case NODE_LIST:
14144 do {
14145 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14146 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14147 case NODE_SYM:
14148 case NODE_REGX:
14149 case NODE_LINE:
14150 case NODE_FILE:
14151 case NODE_ENCODING:
14152 case NODE_INTEGER:
14153 case NODE_FLOAT:
14154 case NODE_RATIONAL:
14155 case NODE_IMAGINARY:
14156 case NODE_STR:
14157 case NODE_NIL:
14158 case NODE_TRUE:
14159 case NODE_FALSE:
14160 case NODE_ZLIST:
14161 break;
14162 default:
14163 return 0;
14164 }
14165 return 1;
14166}
14167
14168static int
14169assign_in_cond(struct parser_params *p, NODE *node)
14170{
14171 switch (nd_type(node)) {
14172 case NODE_MASGN:
14173 case NODE_LASGN:
14174 case NODE_DASGN:
14175 case NODE_GASGN:
14176 case NODE_IASGN:
14177 case NODE_CVASGN:
14178 case NODE_CDECL:
14179 break;
14180
14181 default:
14182 return 0;
14183 }
14184
14185 if (!get_nd_value(p, node)) return 1;
14186 if (is_static_content(get_nd_value(p, node))) {
14187 /* reports always */
14188 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14189 }
14190 return 1;
14191}
14192
14193enum cond_type {
14194 COND_IN_OP,
14195 COND_IN_COND,
14196 COND_IN_FF
14197};
14198
14199#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14200 switch (t) { \
14201 case COND_IN_OP: break; \
14202 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14203 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14204 } \
14205} while (0)
14206
14207static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14208
14209static NODE*
14210range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14211{
14212 enum node_type type;
14213
14214 if (node == 0) return 0;
14215
14216 type = nd_type(node);
14217 value_expr(node);
14218 if (type == NODE_INTEGER) {
14219 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14220 ID lineno = rb_intern("$.");
14221 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14222 }
14223 return cond0(p, node, COND_IN_FF, loc, true);
14224}
14225
14226static NODE*
14227cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14228{
14229 if (node == 0) return 0;
14230 if (!(node = nd_once_body(node))) return 0;
14231 assign_in_cond(p, node);
14232
14233 switch (nd_type(node)) {
14234 case NODE_BEGIN:
14235 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14236 break;
14237
14238 case NODE_DSTR:
14239 case NODE_EVSTR:
14240 case NODE_STR:
14241 case NODE_FILE:
14242 SWITCH_BY_COND_TYPE(type, warn, "string ");
14243 break;
14244
14245 case NODE_REGX:
14246 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14247 nd_set_type(node, NODE_MATCH);
14248 break;
14249
14250 case NODE_DREGX:
14251 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14252
14253 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14254
14255 case NODE_BLOCK:
14256 {
14257 NODE *end = RNODE_BLOCK(node)->nd_end;
14258 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14259 if (top) top = node == end;
14260 *expr = cond0(p, *expr, type, loc, top);
14261 }
14262 break;
14263
14264 case NODE_AND:
14265 case NODE_OR:
14266 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14267 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14268 break;
14269
14270 case NODE_DOT2:
14271 case NODE_DOT3:
14272 if (!top) break;
14273 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14274 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14275 switch (nd_type(node)) {
14276 case NODE_DOT2:
14277 nd_set_type(node,NODE_FLIP2);
14278 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14279 (void)flip2;
14280 break;
14281 case NODE_DOT3:
14282 nd_set_type(node, NODE_FLIP3);
14283 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14284 (void)flip3;
14285 break;
14286 }
14287 break;
14288
14289 case NODE_SYM:
14290 case NODE_DSYM:
14291 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14292 break;
14293
14294 case NODE_LINE:
14295 SWITCH_BY_COND_TYPE(type, warning, "");
14296 break;
14297
14298 case NODE_ENCODING:
14299 SWITCH_BY_COND_TYPE(type, warning, "");
14300 break;
14301
14302 case NODE_INTEGER:
14303 case NODE_FLOAT:
14304 case NODE_RATIONAL:
14305 case NODE_IMAGINARY:
14306 SWITCH_BY_COND_TYPE(type, warning, "");
14307 break;
14308
14309 default:
14310 break;
14311 }
14312 return node;
14313}
14314
14315static NODE*
14316cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14317{
14318 if (node == 0) return 0;
14319 return cond0(p, node, COND_IN_COND, loc, true);
14320}
14321
14322static NODE*
14323method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14324{
14325 if (node == 0) return 0;
14326 return cond0(p, node, COND_IN_OP, loc, true);
14327}
14328
14329static NODE*
14330new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14331{
14332 YYLTYPE loc = {*pos, *pos};
14333 return NEW_NIL(&loc);
14334}
14335
14336static NODE*
14337new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14338{
14339 if (!cc) return right;
14340 cc = cond0(p, cc, COND_IN_COND, loc, true);
14341 return newline_node(NEW_IF(cc, left, right, loc));
14342}
14343
14344static NODE*
14345new_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)
14346{
14347 if (!cc) return right;
14348 cc = cond0(p, cc, COND_IN_COND, loc, true);
14349 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14350}
14351
14352#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))
14353
14354static NODE*
14355logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14356 const YYLTYPE *op_loc, const YYLTYPE *loc)
14357{
14358 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14359 NODE *op;
14360 value_expr(left);
14361 if (left && nd_type_p(left, type)) {
14362 NODE *node = left, *second;
14363 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14364 node = second;
14365 }
14366 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14367 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14368 left->nd_loc.end_pos = loc->end_pos;
14369 return left;
14370 }
14371 op = NEW_AND_OR(type, left, right, loc, op_loc);
14372 nd_set_line(op, op_loc->beg_pos.lineno);
14373 return op;
14374}
14375
14376#undef NEW_AND_OR
14377
14378static void
14379no_blockarg(struct parser_params *p, NODE *node)
14380{
14381 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14382 compile_error(p, "block argument should not be given");
14383 }
14384}
14385
14386static NODE *
14387ret_args(struct parser_params *p, NODE *node)
14388{
14389 if (node) {
14390 no_blockarg(p, node);
14391 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14392 node = RNODE_LIST(node)->nd_head;
14393 }
14394 }
14395 return node;
14396}
14397
14398static NODE *
14399new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14400{
14401 if (node) no_blockarg(p, node);
14402
14403 return NEW_YIELD(node, loc);
14404}
14405
14406static NODE*
14407negate_lit(struct parser_params *p, NODE* node)
14408{
14409 switch (nd_type(node)) {
14410 case NODE_INTEGER:
14411 RNODE_INTEGER(node)->minus = TRUE;
14412 break;
14413 case NODE_FLOAT:
14414 RNODE_FLOAT(node)->minus = TRUE;
14415 break;
14416 case NODE_RATIONAL:
14417 RNODE_RATIONAL(node)->minus = TRUE;
14418 break;
14419 case NODE_IMAGINARY:
14420 RNODE_IMAGINARY(node)->minus = TRUE;
14421 break;
14422 }
14423 return node;
14424}
14425
14426static NODE *
14427arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14428{
14429 if (node2) {
14430 if (!node1) return (NODE *)node2;
14431 node2->nd_head = node1;
14432 nd_set_first_lineno(node2, nd_first_lineno(node1));
14433 nd_set_first_column(node2, nd_first_column(node1));
14434 return (NODE *)node2;
14435 }
14436 return node1;
14437}
14438
14439static bool
14440args_info_empty_p(struct rb_args_info *args)
14441{
14442 if (args->pre_args_num) return false;
14443 if (args->post_args_num) return false;
14444 if (args->rest_arg) return false;
14445 if (args->opt_args) return false;
14446 if (args->block_arg) return false;
14447 if (args->kw_args) return false;
14448 if (args->kw_rest_arg) return false;
14449 return true;
14450}
14451
14452static rb_node_args_t *
14453new_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)
14454{
14455 struct rb_args_info *args = &tail->nd_ainfo;
14456
14457 if (args->forwarding) {
14458 if (rest_arg) {
14459 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14460 return tail;
14461 }
14462 rest_arg = idFWD_REST;
14463 }
14464
14465 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14466 args->pre_init = pre_args ? pre_args->nd_next : 0;
14467
14468 args->post_args_num = post_args ? post_args->nd_plen : 0;
14469 args->post_init = post_args ? post_args->nd_next : 0;
14470 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14471
14472 args->rest_arg = rest_arg;
14473
14474 args->opt_args = opt_args;
14475
14476#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14477 args->ruby2_keywords = args->forwarding;
14478#else
14479 args->ruby2_keywords = 0;
14480#endif
14481
14482 nd_set_loc(RNODE(tail), loc);
14483
14484 return tail;
14485}
14486
14487static rb_node_args_t *
14488new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14489{
14490 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14491 struct rb_args_info *args = &node->nd_ainfo;
14492 if (p->error_p) return node;
14493
14494 args->block_arg = block;
14495 args->kw_args = kw_args;
14496
14497 if (kw_args) {
14498 /*
14499 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14500 * variable order: k1, kr1, k2, &b, internal_id, krest
14501 * #=> <reorder>
14502 * variable order: kr1, k1, k2, internal_id, krest, &b
14503 */
14504 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14505 struct vtable *vtargs = p->lvtbl->args;
14506 rb_node_kw_arg_t *kwn = kw_args;
14507
14508 if (block) block = vtargs->tbl[vtargs->pos-1];
14509 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14510 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14511 while (kwn) {
14512 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14513 --kw_vars;
14514 --required_kw_vars;
14515 kwn = kwn->nd_next;
14516 }
14517
14518 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14519 ID vid = get_nd_vid(p, kwn->nd_body);
14520 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14521 *required_kw_vars++ = vid;
14522 }
14523 else {
14524 *kw_vars++ = vid;
14525 }
14526 }
14527
14528 arg_var(p, kw_bits);
14529 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14530 if (block) arg_var(p, block);
14531
14532 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14533 }
14534 else if (kw_rest_arg == idNil) {
14535 args->no_kwarg = 1;
14536 }
14537 else if (kw_rest_arg) {
14538 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14539 }
14540
14541 return node;
14542}
14543
14544static rb_node_args_t *
14545args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14546{
14547 if (max_numparam > NO_PARAM || it_id) {
14548 if (!args) {
14549 YYLTYPE loc = RUBY_INIT_YYLLOC();
14550 args = new_args_tail(p, 0, 0, 0, 0);
14551 nd_set_loc(RNODE(args), &loc);
14552 }
14553 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14554 }
14555 return args;
14556}
14557
14558static NODE*
14559new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14560{
14561 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14562
14563 if (pre_arg) {
14564 NODE *pre_args = NEW_LIST(pre_arg, loc);
14565 if (RNODE_ARYPTN(aryptn)->pre_args) {
14566 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14567 }
14568 else {
14569 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14570 }
14571 }
14572 return aryptn;
14573}
14574
14575static NODE*
14576new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14577{
14578 if (has_rest) {
14579 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14580 }
14581 else {
14582 rest_arg = NULL;
14583 }
14584 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14585
14586 return node;
14587}
14588
14589static NODE*
14590new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14591{
14592 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14593
14594 return fndptn;
14595}
14596
14597static NODE*
14598new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14599{
14600 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14601 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14602 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14603
14604 return node;
14605}
14606
14607static NODE*
14608new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14609{
14610 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14611 return hshptn;
14612}
14613
14614static NODE*
14615new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14616{
14617 NODE *node, *kw_rest_arg_node;
14618
14619 if (kw_rest_arg == idNil) {
14620 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14621 }
14622 else if (kw_rest_arg) {
14623 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14624 }
14625 else {
14626 kw_rest_arg_node = NULL;
14627 }
14628
14629 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14630
14631 return node;
14632}
14633
14634static NODE*
14635dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14636{
14637 if (!node) {
14638 return NEW_SYM(STR_NEW0(), loc);
14639 }
14640
14641 switch (nd_type(node)) {
14642 case NODE_DSTR:
14643 nd_set_type(node, NODE_DSYM);
14644 nd_set_loc(node, loc);
14645 break;
14646 case NODE_STR:
14647 node = str_to_sym_node(p, node, loc);
14648 break;
14649 default:
14650 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14651 break;
14652 }
14653 return node;
14654}
14655
14656static int
14657nd_type_st_key_enable_p(NODE *node)
14658{
14659 switch (nd_type(node)) {
14660 case NODE_INTEGER:
14661 case NODE_FLOAT:
14662 case NODE_RATIONAL:
14663 case NODE_IMAGINARY:
14664 case NODE_STR:
14665 case NODE_SYM:
14666 case NODE_REGX:
14667 case NODE_LINE:
14668 case NODE_FILE:
14669 case NODE_ENCODING:
14670 return true;
14671 default:
14672 return false;
14673 }
14674}
14675
14676static VALUE
14677nd_value(struct parser_params *p, NODE *node)
14678{
14679 switch (nd_type(node)) {
14680 case NODE_STR:
14681 return rb_node_str_string_val(node);
14682 case NODE_INTEGER:
14683 return rb_node_integer_literal_val(node);
14684 case NODE_FLOAT:
14685 return rb_node_float_literal_val(node);
14686 case NODE_RATIONAL:
14687 return rb_node_rational_literal_val(node);
14688 case NODE_IMAGINARY:
14689 return rb_node_imaginary_literal_val(node);
14690 case NODE_SYM:
14691 return rb_node_sym_string_val(node);
14692 case NODE_REGX:
14693 return rb_node_regx_string_val(node);
14694 case NODE_LINE:
14695 return rb_node_line_lineno_val(node);
14696 case NODE_ENCODING:
14697 return rb_node_encoding_val(node);
14698 case NODE_FILE:
14699 return rb_node_file_path_val(node);
14700 default:
14701 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14702 UNREACHABLE_RETURN(0);
14703 }
14704}
14705
14706static void
14707warn_duplicate_keys(struct parser_params *p, NODE *hash)
14708{
14709 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14710 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14711 while (hash && RNODE_LIST(hash)->nd_next) {
14712 NODE *head = RNODE_LIST(hash)->nd_head;
14713 NODE *value = RNODE_LIST(hash)->nd_next;
14714 NODE *next = RNODE_LIST(value)->nd_next;
14715 st_data_t key;
14716 st_data_t data;
14717
14718 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14719 if (!head) {
14720 head = value;
14721 }
14722
14723 if (nd_type_st_key_enable_p(head)) {
14724 key = (st_data_t)head;
14725
14726 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14727 rb_warn2L(nd_line((NODE *)data),
14728 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14729 nd_value(p, head), WARN_I(nd_line(head)));
14730 }
14731 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14732 }
14733 hash = next;
14734 }
14735 st_free_table(p->warn_duplicate_keys_table);
14736 p->warn_duplicate_keys_table = NULL;
14737}
14738
14739static NODE *
14740new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14741{
14742 if (hash) warn_duplicate_keys(p, hash);
14743 return NEW_HASH(hash, loc);
14744}
14745
14746static void
14747error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14748{
14749 if (is_private_local_id(p, id)) {
14750 return;
14751 }
14752 if (st_is_member(p->pvtbl, id)) {
14753 yyerror1(loc, "duplicated variable name");
14754 }
14755 else {
14756 st_insert(p->pvtbl, (st_data_t)id, 0);
14757 }
14758}
14759
14760static void
14761error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14762{
14763 if (!p->pktbl) {
14764 p->pktbl = st_init_numtable();
14765 }
14766 else if (st_is_member(p->pktbl, key)) {
14767 yyerror1(loc, "duplicated key name");
14768 return;
14769 }
14770 st_insert(p->pktbl, (st_data_t)key, 0);
14771}
14772
14773static NODE *
14774new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14775{
14776 return NEW_HASH(hash, loc);
14777}
14778
14779static NODE *
14780new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14781{
14782 NODE *asgn;
14783
14784 if (lhs) {
14785 ID vid = get_nd_vid(p, lhs);
14786 YYLTYPE lhs_loc = lhs->nd_loc;
14787 if (op == tOROP) {
14788 set_nd_value(p, lhs, rhs);
14789 nd_set_loc(lhs, loc);
14790 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14791 }
14792 else if (op == tANDOP) {
14793 set_nd_value(p, lhs, rhs);
14794 nd_set_loc(lhs, loc);
14795 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14796 }
14797 else {
14798 asgn = lhs;
14799 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14800 set_nd_value(p, asgn, rhs);
14801 nd_set_loc(asgn, loc);
14802 }
14803 }
14804 else {
14805 asgn = NEW_ERROR(loc);
14806 }
14807 return asgn;
14808}
14809
14810static NODE *
14811new_ary_op_assign(struct parser_params *p, NODE *ary,
14812 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14813 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14814{
14815 NODE *asgn;
14816
14817 aryset_check(p, args);
14818 args = make_list(args, args_loc);
14819 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14820 fixpos(asgn, ary);
14821 return asgn;
14822}
14823
14824static NODE *
14825new_attr_op_assign(struct parser_params *p, NODE *lhs,
14826 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14827 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14828{
14829 NODE *asgn;
14830
14831 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14832 fixpos(asgn, lhs);
14833 return asgn;
14834}
14835
14836static NODE *
14837new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14838{
14839 NODE *asgn;
14840
14841 if (lhs) {
14842 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14843 }
14844 else {
14845 asgn = NEW_ERROR(loc);
14846 }
14847 fixpos(asgn, lhs);
14848 return asgn;
14849}
14850
14851static NODE *
14852const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14853{
14854 if (p->ctxt.in_def) {
14855#ifndef RIPPER
14856 yyerror1(loc, "dynamic constant assignment");
14857#else
14858 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14859#endif
14860 }
14861 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14862}
14863
14864#ifdef RIPPER
14865static VALUE
14866assign_error(struct parser_params *p, const char *mesg, VALUE a)
14867{
14868 a = dispatch2(assign_error, ERR_MESG(), a);
14869 ripper_error(p);
14870 return a;
14871}
14872#endif
14873
14874static NODE *
14875new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14876{
14877 NODE *result = head;
14878 if (rescue) {
14879 NODE *tmp = rescue_else ? rescue_else : rescue;
14880 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14881
14882 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14883 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14884 }
14885 if (ensure) {
14886 result = NEW_ENSURE(result, ensure, loc);
14887 }
14888 fixpos(result, head);
14889 return result;
14890}
14891
14892static void
14893warn_unused_var(struct parser_params *p, struct local_vars *local)
14894{
14895 int cnt;
14896
14897 if (!local->used) return;
14898 cnt = local->used->pos;
14899 if (cnt != local->vars->pos) {
14900 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14901 }
14902#ifndef RIPPER
14903 ID *v = local->vars->tbl;
14904 ID *u = local->used->tbl;
14905 for (int i = 0; i < cnt; ++i) {
14906 if (!v[i] || (u[i] & LVAR_USED)) continue;
14907 if (is_private_local_id(p, v[i])) continue;
14908 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14909 }
14910#endif
14911}
14912
14913static void
14914local_push(struct parser_params *p, int toplevel_scope)
14915{
14916 struct local_vars *local;
14917 int inherits_dvars = toplevel_scope && compile_for_eval;
14918 int warn_unused_vars = RTEST(ruby_verbose);
14919
14920 local = ALLOC(struct local_vars);
14921 local->prev = p->lvtbl;
14922 local->args = vtable_alloc(0);
14923 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14924#ifndef RIPPER
14925 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14926 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14927#endif
14928 local->numparam.outer = 0;
14929 local->numparam.inner = 0;
14930 local->numparam.current = 0;
14931 local->it = 0;
14932 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14933
14934# if WARN_PAST_SCOPE
14935 local->past = 0;
14936# endif
14937 CMDARG_PUSH(0);
14938 COND_PUSH(0);
14939 p->lvtbl = local;
14940}
14941
14942static void
14943vtable_chain_free(struct parser_params *p, struct vtable *table)
14944{
14945 while (!DVARS_TERMINAL_P(table)) {
14946 struct vtable *cur_table = table;
14947 table = cur_table->prev;
14948 vtable_free(cur_table);
14949 }
14950}
14951
14952static void
14953local_free(struct parser_params *p, struct local_vars *local)
14954{
14955 vtable_chain_free(p, local->used);
14956
14957# if WARN_PAST_SCOPE
14958 vtable_chain_free(p, local->past);
14959# endif
14960
14961 vtable_chain_free(p, local->args);
14962 vtable_chain_free(p, local->vars);
14963
14964 ruby_sized_xfree(local, sizeof(struct local_vars));
14965}
14966
14967static void
14968local_pop(struct parser_params *p)
14969{
14970 struct local_vars *local = p->lvtbl->prev;
14971 if (p->lvtbl->used) {
14972 warn_unused_var(p, p->lvtbl);
14973 }
14974
14975 local_free(p, p->lvtbl);
14976 p->lvtbl = local;
14977
14978 CMDARG_POP();
14979 COND_POP();
14980}
14981
14982static rb_ast_id_table_t *
14983local_tbl(struct parser_params *p)
14984{
14985 int cnt_args = vtable_size(p->lvtbl->args);
14986 int cnt_vars = vtable_size(p->lvtbl->vars);
14987 int cnt = cnt_args + cnt_vars;
14988 int i, j;
14989 rb_ast_id_table_t *tbl;
14990
14991 if (cnt <= 0) return 0;
14992 tbl = rb_ast_new_local_table(p->ast, cnt);
14993 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14994 /* remove IDs duplicated to warn shadowing */
14995 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14996 ID id = p->lvtbl->vars->tbl[i];
14997 if (!vtable_included(p->lvtbl->args, id)) {
14998 tbl->ids[j++] = id;
14999 }
15000 }
15001 if (j < cnt) {
15002 tbl = rb_ast_resize_latest_local_table(p->ast, j);
15003 }
15004
15005 return tbl;
15006}
15007
15008static void
15009numparam_name(struct parser_params *p, ID id)
15010{
15011 if (!NUMPARAM_ID_P(id)) return;
15012 compile_error(p, "_%d is reserved for numbered parameter",
15013 NUMPARAM_ID_TO_IDX(id));
15014}
15015
15016static void
15017arg_var(struct parser_params *p, ID id)
15018{
15019 numparam_name(p, id);
15020 vtable_add(p->lvtbl->args, id);
15021}
15022
15023static void
15024local_var(struct parser_params *p, ID id)
15025{
15026 numparam_name(p, id);
15027 vtable_add(p->lvtbl->vars, id);
15028 if (p->lvtbl->used) {
15029 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15030 }
15031}
15032
15033#ifndef RIPPER
15034int
15035rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15036{
15037 return rb_local_defined(id, iseq);
15038}
15039#endif
15040
15041static int
15042local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15043{
15044 struct vtable *vars, *args, *used;
15045
15046 vars = p->lvtbl->vars;
15047 args = p->lvtbl->args;
15048 used = p->lvtbl->used;
15049
15050 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15051 vars = vars->prev;
15052 args = args->prev;
15053 if (used) used = used->prev;
15054 }
15055
15056 if (vars && vars->prev == DVARS_INHERIT) {
15057 return rb_parser_local_defined(p, id, p->parent_iseq);
15058 }
15059 else if (vtable_included(args, id)) {
15060 return 1;
15061 }
15062 else {
15063 int i = vtable_included(vars, id);
15064 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15065 return i != 0;
15066 }
15067}
15068
15069static int
15070local_id(struct parser_params *p, ID id)
15071{
15072 return local_id_ref(p, id, NULL);
15073}
15074
15075static int
15076check_forwarding_args(struct parser_params *p)
15077{
15078 if (local_id(p, idFWD_ALL)) return TRUE;
15079 compile_error(p, "unexpected ...");
15080 return FALSE;
15081}
15082
15083static void
15084add_forwarding_args(struct parser_params *p)
15085{
15086 arg_var(p, idFWD_REST);
15087#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15088 arg_var(p, idFWD_KWREST);
15089#endif
15090 arg_var(p, idFWD_BLOCK);
15091 arg_var(p, idFWD_ALL);
15092}
15093
15094static void
15095forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15096{
15097 bool conflict = false;
15098
15099 struct vtable *vars, *args;
15100
15101 vars = p->lvtbl->vars;
15102 args = p->lvtbl->args;
15103
15104 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15105 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15106 vars = vars->prev;
15107 args = args->prev;
15108 }
15109
15110 bool found = false;
15111 if (vars && vars->prev == DVARS_INHERIT && !found) {
15112 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15113 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15114 }
15115 else {
15116 found = (vtable_included(args, arg) &&
15117 !(all && vtable_included(args, all)));
15118 }
15119
15120 if (!found) {
15121 compile_error(p, "no anonymous %s parameter", var);
15122 }
15123 else if (conflict) {
15124 compile_error(p, "anonymous %s parameter is also used within block", var);
15125 }
15126}
15127
15128static NODE *
15129new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15130{
15131 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15132#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15133 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15134#endif
15135 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15136 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15137 block->forwarding = TRUE;
15138#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15139 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15140#endif
15141 return arg_blk_pass(args, block);
15142}
15143
15144static NODE *
15145numparam_push(struct parser_params *p)
15146{
15147 struct local_vars *local = p->lvtbl;
15148 NODE *inner = local->numparam.inner;
15149 if (!local->numparam.outer) {
15150 local->numparam.outer = local->numparam.current;
15151 }
15152 local->numparam.inner = 0;
15153 local->numparam.current = 0;
15154 local->it = 0;
15155 return inner;
15156}
15157
15158static void
15159numparam_pop(struct parser_params *p, NODE *prev_inner)
15160{
15161 struct local_vars *local = p->lvtbl;
15162 if (prev_inner) {
15163 /* prefer first one */
15164 local->numparam.inner = prev_inner;
15165 }
15166 else if (local->numparam.current) {
15167 /* current and inner are exclusive */
15168 local->numparam.inner = local->numparam.current;
15169 }
15170 if (p->max_numparam > NO_PARAM) {
15171 /* current and outer are exclusive */
15172 local->numparam.current = local->numparam.outer;
15173 local->numparam.outer = 0;
15174 }
15175 else {
15176 /* no numbered parameter */
15177 local->numparam.current = 0;
15178 }
15179 local->it = 0;
15180}
15181
15182static const struct vtable *
15183dyna_push(struct parser_params *p)
15184{
15185 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15186 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15187 if (p->lvtbl->used) {
15188 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15189 }
15190 return p->lvtbl->args;
15191}
15192
15193static void
15194dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15195{
15196 struct vtable *tmp = *vtblp;
15197 *vtblp = tmp->prev;
15198# if WARN_PAST_SCOPE
15199 if (p->past_scope_enabled) {
15200 tmp->prev = p->lvtbl->past;
15201 p->lvtbl->past = tmp;
15202 return;
15203 }
15204# endif
15205 vtable_free(tmp);
15206}
15207
15208static void
15209dyna_pop_1(struct parser_params *p)
15210{
15211 struct vtable *tmp;
15212
15213 if ((tmp = p->lvtbl->used) != 0) {
15214 warn_unused_var(p, p->lvtbl);
15215 p->lvtbl->used = p->lvtbl->used->prev;
15216 vtable_free(tmp);
15217 }
15218 dyna_pop_vtable(p, &p->lvtbl->args);
15219 dyna_pop_vtable(p, &p->lvtbl->vars);
15220}
15221
15222static void
15223dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15224{
15225 while (p->lvtbl->args != lvargs) {
15226 dyna_pop_1(p);
15227 if (!p->lvtbl->args) {
15228 struct local_vars *local = p->lvtbl->prev;
15229 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15230 p->lvtbl = local;
15231 }
15232 }
15233 dyna_pop_1(p);
15234}
15235
15236static int
15237dyna_in_block(struct parser_params *p)
15238{
15239 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15240}
15241
15242#ifndef RIPPER
15243int
15244dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15245{
15246 struct vtable *vars, *args, *used;
15247 int i;
15248
15249 args = p->lvtbl->args;
15250 vars = p->lvtbl->vars;
15251 used = p->lvtbl->used;
15252
15253 while (!DVARS_TERMINAL_P(vars)) {
15254 if (vtable_included(args, id)) {
15255 return 1;
15256 }
15257 if ((i = vtable_included(vars, id)) != 0) {
15258 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15259 return 1;
15260 }
15261 args = args->prev;
15262 vars = vars->prev;
15263 if (!vidrefp) used = 0;
15264 if (used) used = used->prev;
15265 }
15266
15267 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15268 return rb_dvar_defined(id, p->parent_iseq);
15269 }
15270
15271 return 0;
15272}
15273#endif
15274
15275static int
15276dvar_defined(struct parser_params *p, ID id)
15277{
15278 return dvar_defined_ref(p, id, NULL);
15279}
15280
15281static int
15282dvar_curr(struct parser_params *p, ID id)
15283{
15284 return (vtable_included(p->lvtbl->args, id) ||
15285 vtable_included(p->lvtbl->vars, id));
15286}
15287
15288static void
15289reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15290{
15291 compile_error(p,
15292 "regexp encoding option '%c' differs from source encoding '%s'",
15293 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15294}
15295
15296#ifndef RIPPER
15297static rb_encoding *
15298find_enc(struct parser_params* p, const char *name)
15299{
15300 int idx = rb_enc_find_index(name);
15301 if (idx < 0) {
15302 rb_bug("unknown encoding name: %s", name);
15303 }
15304
15305 return rb_enc_from_index(idx);
15306}
15307
15308static rb_encoding *
15309kcode_to_enc(struct parser_params* p, int kcode)
15310{
15311 rb_encoding *enc;
15312
15313 switch (kcode) {
15314 case ENC_ASCII8BIT:
15315 enc = rb_ascii8bit_encoding();
15316 break;
15317 case ENC_EUC_JP:
15318 enc = find_enc(p, "EUC-JP");
15319 break;
15320 case ENC_Windows_31J:
15321 enc = find_enc(p, "Windows-31J");
15322 break;
15323 case ENC_UTF8:
15324 enc = rb_utf8_encoding();
15325 break;
15326 default:
15327 enc = NULL;
15328 break;
15329 }
15330
15331 return enc;
15332}
15333
15334int
15335rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15336{
15337 int c = RE_OPTION_ENCODING_IDX(options);
15338
15339 if (c) {
15340 int opt, idx;
15341 rb_encoding *enc;
15342
15343 char_to_option_kcode(c, &opt, &idx);
15344 enc = kcode_to_enc(p, idx);
15345 if (enc != rb_parser_str_get_encoding(str) &&
15346 !rb_parser_is_ascii_string(p, str)) {
15347 goto error;
15348 }
15349 rb_parser_string_set_encoding(str, enc);
15350 }
15351 else if (RE_OPTION_ENCODING_NONE(options)) {
15352 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15353 !rb_parser_is_ascii_string(p, str)) {
15354 c = 'n';
15355 goto error;
15356 }
15357 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15358 }
15359 else if (rb_is_usascii_enc(p->enc)) {
15360 if (!rb_parser_is_ascii_string(p, str)) {
15361 /* raise in re.c */
15362 rb_parser_enc_associate(p, str, rb_usascii_encoding());
15363 }
15364 else {
15365 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15366 }
15367 }
15368 return 0;
15369
15370 error:
15371 return c;
15372}
15373#endif
15374
15375static void
15376reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15377{
15378 int c = rb_reg_fragment_setenc(p, str, options);
15379 if (c) reg_fragment_enc_error(p, str, c);
15380}
15381
15382static void
15383reg_fragment_error(struct parser_params* p, VALUE err)
15384{
15385 compile_error(p, "%"PRIsVALUE, err);
15386}
15387
15388#ifndef RIPPER
15389int
15390rb_parser_reg_fragment_check(struct parser_params* p, rb_parser_string_t *str, int options, rb_parser_reg_fragment_error_func error)
15391{
15392 VALUE err, str2;
15393 reg_fragment_setenc(p, str, options);
15394 /* TODO */
15395 str2 = rb_str_new_parser_string(str);
15396 err = rb_reg_check_preprocess(str2);
15397 if (err != Qnil) {
15398 err = rb_obj_as_string(err);
15399 error(p, err);
15400 return 0;
15401 }
15402 return 1;
15403}
15404#endif
15405
15406#ifndef UNIVERSAL_PARSER
15407typedef struct {
15408 struct parser_params* parser;
15409 rb_encoding *enc;
15410 NODE *succ_block;
15411 const YYLTYPE *loc;
15412 rb_parser_assignable_func assignable;
15413} reg_named_capture_assign_t;
15414
15415static int
15416reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15417 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15418{
15419 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15420 struct parser_params* p = arg->parser;
15421 rb_encoding *enc = arg->enc;
15422 long len = name_end - name;
15423 const char *s = (const char *)name;
15424
15425 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15426}
15427
15428static NODE *
15429reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15430{
15431 reg_named_capture_assign_t arg;
15432
15433 arg.parser = p;
15434 arg.enc = rb_enc_get(regexp);
15435 arg.succ_block = 0;
15436 arg.loc = loc;
15437 arg.assignable = assignable;
15438 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15439
15440 if (!arg.succ_block) return 0;
15441 return RNODE_BLOCK(arg.succ_block)->nd_next;
15442}
15443#endif
15444
15445#ifndef RIPPER
15446NODE *
15447rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15448{
15449 return assignable(p, id, val, loc);
15450}
15451
15452int
15453rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15454 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15455{
15456 ID var;
15457 NODE *node, *succ;
15458
15459 if (!len) return ST_CONTINUE;
15460 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15461 return ST_CONTINUE;
15462
15463 var = intern_cstr(s, len, enc);
15464 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15465 if (!lvar_defined(p, var)) return ST_CONTINUE;
15466 }
15467 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15468 succ = *succ_block;
15469 if (!succ) succ = NEW_ERROR(loc);
15470 succ = block_append(p, succ, node);
15471 *succ_block = succ;
15472 return ST_CONTINUE;
15473}
15474#endif
15475
15476static VALUE
15477parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15478{
15479 VALUE str2;
15480 reg_fragment_setenc(p, str, options);
15481 str2 = rb_str_new_parser_string(str);
15482 return rb_parser_reg_compile(p, str2, options);
15483}
15484
15485#ifndef RIPPER
15486VALUE
15487rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15488{
15489 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15490}
15491#endif
15492
15493static VALUE
15494reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15495{
15496 VALUE re;
15497 VALUE err;
15498
15499 err = rb_errinfo();
15500 re = parser_reg_compile(p, str, options);
15501 if (NIL_P(re)) {
15502 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15503 rb_set_errinfo(err);
15504 reg_fragment_error(p, m);
15505 return Qnil;
15506 }
15507 return re;
15508}
15509
15510#ifndef RIPPER
15511void
15512rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15513{
15514 p->do_print = print;
15515 p->do_loop = loop;
15516 p->do_chomp = chomp;
15517 p->do_split = split;
15518}
15519
15520static NODE *
15521parser_append_options(struct parser_params *p, NODE *node)
15522{
15523 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15524 const YYLTYPE *const LOC = &default_location;
15525
15526 if (p->do_print) {
15527 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15528 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15529 LOC);
15530 node = block_append(p, node, print);
15531 }
15532
15533 if (p->do_loop) {
15534 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15535
15536 if (p->do_split) {
15537 ID ifs = rb_intern("$;");
15538 ID fields = rb_intern("$F");
15539 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15540 NODE *split = NEW_GASGN(fields,
15541 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15542 rb_intern("split"), args, LOC),
15543 LOC);
15544 node = block_append(p, split, node);
15545 }
15546 if (p->do_chomp) {
15547 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15548 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15549 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15550 }
15551
15552 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15553 }
15554
15555 return node;
15556}
15557
15558void
15559rb_init_parse(void)
15560{
15561 /* just to suppress unused-function warnings */
15562 (void)nodetype;
15563 (void)nodeline;
15564}
15565
15566ID
15567internal_id(struct parser_params *p)
15568{
15569 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15570}
15571#endif /* !RIPPER */
15572
15573static void
15574parser_initialize(struct parser_params *p)
15575{
15576 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15577 p->command_start = TRUE;
15578 p->ruby_sourcefile_string = Qnil;
15579 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15580 string_buffer_init(p);
15581 p->node_id = 0;
15582 p->delayed.token = NULL;
15583 p->frozen_string_literal = -1; /* not specified */
15584#ifndef RIPPER
15585 p->error_buffer = Qfalse;
15586 p->end_expect_token_locations = NULL;
15587 p->token_id = 0;
15588 p->tokens = NULL;
15589#else
15590 p->result = Qnil;
15591 p->parsing_thread = Qnil;
15592 p->s_value = Qnil;
15593 p->s_lvalue = Qnil;
15594 p->s_value_stack = rb_ary_new();
15595#endif
15596 p->debug_buffer = Qnil;
15597 p->debug_output = rb_ractor_stdout();
15598 p->enc = rb_utf8_encoding();
15599 p->exits = 0;
15600}
15601
15602#ifdef RIPPER
15603#define rb_ruby_parser_mark ripper_parser_mark
15604#define rb_ruby_parser_free ripper_parser_free
15605#define rb_ruby_parser_memsize ripper_parser_memsize
15606#endif
15607
15608void
15609rb_ruby_parser_mark(void *ptr)
15610{
15611 struct parser_params *p = (struct parser_params*)ptr;
15612
15613 rb_gc_mark(p->ruby_sourcefile_string);
15614#ifndef RIPPER
15615 rb_gc_mark(p->error_buffer);
15616#else
15617 rb_gc_mark(p->value);
15618 rb_gc_mark(p->result);
15619 rb_gc_mark(p->parsing_thread);
15620 rb_gc_mark(p->s_value);
15621 rb_gc_mark(p->s_lvalue);
15622 rb_gc_mark(p->s_value_stack);
15623#endif
15624 rb_gc_mark(p->debug_buffer);
15625 rb_gc_mark(p->debug_output);
15626}
15627
15628void
15629rb_ruby_parser_free(void *ptr)
15630{
15631 struct parser_params *p = (struct parser_params*)ptr;
15632 struct local_vars *local, *prev;
15633
15634 if (p->ast) {
15635 rb_ast_free(p->ast);
15636 }
15637
15638 if (p->warn_duplicate_keys_table) {
15639 st_free_table(p->warn_duplicate_keys_table);
15640 }
15641
15642#ifndef RIPPER
15643 if (p->tokens) {
15644 rb_parser_ary_free(p, p->tokens);
15645 }
15646#endif
15647
15648 if (p->tokenbuf) {
15649 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15650 }
15651
15652 for (local = p->lvtbl; local; local = prev) {
15653 prev = local->prev;
15654 local_free(p, local);
15655 }
15656
15657 {
15658 token_info *ptinfo;
15659 while ((ptinfo = p->token_info) != 0) {
15660 p->token_info = ptinfo->next;
15661 xfree(ptinfo);
15662 }
15663 }
15664 string_buffer_free(p);
15665
15666 if (p->pvtbl) {
15667 st_free_table(p->pvtbl);
15668 }
15669
15670 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15671 st_free_table(p->case_labels);
15672 }
15673
15674 xfree(p->lex.strterm);
15675 p->lex.strterm = 0;
15676
15677 xfree(ptr);
15678}
15679
15680size_t
15681rb_ruby_parser_memsize(const void *ptr)
15682{
15683 struct parser_params *p = (struct parser_params*)ptr;
15684 struct local_vars *local;
15685 size_t size = sizeof(*p);
15686
15687 size += p->toksiz;
15688 for (local = p->lvtbl; local; local = local->prev) {
15689 size += sizeof(*local);
15690 if (local->vars) size += local->vars->capa * sizeof(ID);
15691 }
15692 return size;
15693}
15694
15695#ifndef RIPPER
15696#undef rb_reserved_word
15697
15698const struct kwtable *
15699rb_reserved_word(const char *str, unsigned int len)
15700{
15701 return reserved_word(str, len);
15702}
15703
15704#ifdef UNIVERSAL_PARSER
15705rb_parser_t *
15706rb_ruby_parser_allocate(const rb_parser_config_t *config)
15707{
15708 /* parser_initialize expects fields to be set to 0 */
15709 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15710 p->config = config;
15711 return p;
15712}
15713
15714rb_parser_t *
15715rb_ruby_parser_new(const rb_parser_config_t *config)
15716{
15717 /* parser_initialize expects fields to be set to 0 */
15718 rb_parser_t *p = rb_ruby_parser_allocate(config);
15719 parser_initialize(p);
15720 return p;
15721}
15722#else
15723rb_parser_t *
15724rb_ruby_parser_allocate(void)
15725{
15726 /* parser_initialize expects fields to be set to 0 */
15727 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15728 return p;
15729}
15730
15731rb_parser_t *
15732rb_ruby_parser_new(void)
15733{
15734 /* parser_initialize expects fields to be set to 0 */
15735 rb_parser_t *p = rb_ruby_parser_allocate();
15736 parser_initialize(p);
15737 return p;
15738}
15739#endif
15740
15741rb_parser_t *
15742rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15743{
15744 p->error_buffer = main ? Qfalse : Qnil;
15745 p->parent_iseq = base;
15746 return p;
15747}
15748
15749void
15750rb_ruby_parser_set_script_lines(rb_parser_t *p)
15751{
15752 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15753}
15754
15755void
15756rb_ruby_parser_error_tolerant(rb_parser_t *p)
15757{
15758 p->error_tolerant = 1;
15759}
15760
15761void
15762rb_ruby_parser_keep_tokens(rb_parser_t *p)
15763{
15764 p->keep_tokens = 1;
15765 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15766}
15767
15768rb_encoding *
15769rb_ruby_parser_encoding(rb_parser_t *p)
15770{
15771 return p->enc;
15772}
15773
15774int
15775rb_ruby_parser_end_seen_p(rb_parser_t *p)
15776{
15777 return p->ruby__end__seen;
15778}
15779
15780int
15781rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15782{
15783 p->debug = flag;
15784 return flag;
15785}
15786#endif /* !RIPPER */
15787
15788#ifdef RIPPER
15789int
15790rb_ruby_parser_get_yydebug(rb_parser_t *p)
15791{
15792 return p->debug;
15793}
15794
15795void
15796rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15797{
15798 p->value = value;
15799}
15800
15801int
15802rb_ruby_parser_error_p(rb_parser_t *p)
15803{
15804 return p->error_p;
15805}
15806
15807VALUE
15808rb_ruby_parser_debug_output(rb_parser_t *p)
15809{
15810 return p->debug_output;
15811}
15812
15813void
15814rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15815{
15816 p->debug_output = output;
15817}
15818
15819VALUE
15820rb_ruby_parser_parsing_thread(rb_parser_t *p)
15821{
15822 return p->parsing_thread;
15823}
15824
15825void
15826rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15827{
15828 p->parsing_thread = parsing_thread;
15829}
15830
15831void
15832rb_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)
15833{
15834 p->lex.gets = gets;
15835 p->lex.input = input;
15836 p->eofp = 0;
15837 p->ruby_sourcefile_string = sourcefile_string;
15838 p->ruby_sourcefile = sourcefile;
15839 p->ruby_sourceline = sourceline;
15840}
15841
15842VALUE
15843rb_ruby_parser_result(rb_parser_t *p)
15844{
15845 return p->result;
15846}
15847
15848rb_encoding *
15849rb_ruby_parser_enc(rb_parser_t *p)
15850{
15851 return p->enc;
15852}
15853
15854VALUE
15855rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15856{
15857 return p->ruby_sourcefile_string;
15858}
15859
15860int
15861rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15862{
15863 return p->ruby_sourceline;
15864}
15865
15866int
15867rb_ruby_parser_lex_state(rb_parser_t *p)
15868{
15869 return p->lex.state;
15870}
15871
15872void
15873rb_ruby_ripper_parse0(rb_parser_t *p)
15874{
15875 parser_prepare(p);
15876 p->ast = rb_ast_new();
15877 ripper_yyparse((void*)p);
15878 rb_ast_free(p->ast);
15879 p->ast = 0;
15880 p->eval_tree = 0;
15881 p->eval_tree_begin = 0;
15882}
15883
15884int
15885rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15886{
15887 return dedent_string(p, string, width);
15888}
15889
15890int
15891rb_ruby_ripper_initialized_p(rb_parser_t *p)
15892{
15893 return p->lex.input != 0;
15894}
15895
15896void
15897rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15898{
15899 parser_initialize(p);
15900}
15901
15902long
15903rb_ruby_ripper_column(rb_parser_t *p)
15904{
15905 return p->lex.ptok - p->lex.pbeg;
15906}
15907
15908long
15909rb_ruby_ripper_token_len(rb_parser_t *p)
15910{
15911 return p->lex.pcur - p->lex.ptok;
15912}
15913
15914rb_parser_string_t *
15915rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15916{
15917 return p->lex.lastline;
15918}
15919
15920VALUE
15921rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15922{
15923 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15924}
15925
15926#ifdef UNIVERSAL_PARSER
15927rb_parser_t *
15928rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15929{
15930 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15931 p->config = config;
15932 return p;
15933}
15934#endif
15935
15936struct parser_params*
15937rb_ruby_ripper_parser_allocate(void)
15938{
15939 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15940}
15941#endif /* RIPPER */
15942
15943#ifndef RIPPER
15944void
15945rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15946{
15947 va_list ap;
15948 VALUE mesg = p->debug_buffer;
15949
15950 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15951 va_start(ap, fmt);
15952 rb_str_vcatf(mesg, fmt, ap);
15953 va_end(ap);
15954 if (char_at_end(p, mesg, 0) == '\n') {
15955 rb_io_write(p->debug_output, mesg);
15956 p->debug_buffer = Qnil;
15957 }
15958}
15959
15960static void
15961parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15962{
15963 va_list ap;
15964 int lineno, column;
15965
15966 if (loc) {
15967 lineno = loc->end_pos.lineno;
15968 column = loc->end_pos.column;
15969 }
15970 else {
15971 lineno = p->ruby_sourceline;
15972 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15973 }
15974
15975 rb_io_flush(p->debug_output);
15976 p->error_p = 1;
15977 va_start(ap, fmt);
15978 p->error_buffer =
15979 rb_syntax_error_append(p->error_buffer,
15980 p->ruby_sourcefile_string,
15981 lineno, column,
15982 p->enc, fmt, ap);
15983 va_end(ap);
15984}
15985
15986static size_t
15987count_char(const char *str, int c)
15988{
15989 int n = 0;
15990 while (str[n] == c) ++n;
15991 return n;
15992}
15993
15994/*
15995 * strip enclosing double-quotes, same as the default yytnamerr except
15996 * for that single-quotes matching back-quotes do not stop stripping.
15997 *
15998 * "\"`class' keyword\"" => "`class' keyword"
15999 */
16000size_t
16001rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16002{
16003 if (*yystr == '"') {
16004 size_t yyn = 0, bquote = 0;
16005 const char *yyp = yystr;
16006
16007 while (*++yyp) {
16008 switch (*yyp) {
16009 case '\'':
16010 if (!bquote) {
16011 bquote = count_char(yyp+1, '\'') + 1;
16012 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16013 yyn += bquote;
16014 yyp += bquote - 1;
16015 break;
16016 }
16017 else {
16018 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16019 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16020 yyn += bquote;
16021 yyp += bquote - 1;
16022 bquote = 0;
16023 break;
16024 }
16025 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16026 if (yyres) memcpy(yyres + yyn, yyp, 3);
16027 yyn += 3;
16028 yyp += 2;
16029 break;
16030 }
16031 goto do_not_strip_quotes;
16032 }
16033
16034 case ',':
16035 goto do_not_strip_quotes;
16036
16037 case '\\':
16038 if (*++yyp != '\\')
16039 goto do_not_strip_quotes;
16040 /* Fall through. */
16041 default:
16042 if (yyres)
16043 yyres[yyn] = *yyp;
16044 yyn++;
16045 break;
16046
16047 case '"':
16048 case '\0':
16049 if (yyres)
16050 yyres[yyn] = '\0';
16051 return yyn;
16052 }
16053 }
16054 do_not_strip_quotes: ;
16055 }
16056
16057 if (!yyres) return strlen(yystr);
16058
16059 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16060}
16061#endif
16062
16063#ifdef RIPPER
16064#define validate(x) (void)(x)
16065
16066static VALUE
16067ripper_dispatch0(struct parser_params *p, ID mid)
16068{
16069 return rb_funcall(p->value, mid, 0);
16070}
16071
16072static VALUE
16073ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16074{
16075 validate(a);
16076 return rb_funcall(p->value, mid, 1, a);
16077}
16078
16079static VALUE
16080ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16081{
16082 validate(a);
16083 validate(b);
16084 return rb_funcall(p->value, mid, 2, a, b);
16085}
16086
16087static VALUE
16088ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16089{
16090 validate(a);
16091 validate(b);
16092 validate(c);
16093 return rb_funcall(p->value, mid, 3, a, b, c);
16094}
16095
16096static VALUE
16097ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16098{
16099 validate(a);
16100 validate(b);
16101 validate(c);
16102 validate(d);
16103 return rb_funcall(p->value, mid, 4, a, b, c, d);
16104}
16105
16106static VALUE
16107ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16108{
16109 validate(a);
16110 validate(b);
16111 validate(c);
16112 validate(d);
16113 validate(e);
16114 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16115}
16116
16117static VALUE
16118ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16119{
16120 validate(a);
16121 validate(b);
16122 validate(c);
16123 validate(d);
16124 validate(e);
16125 validate(f);
16126 validate(g);
16127 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16128}
16129
16130void
16131ripper_error(struct parser_params *p)
16132{
16133 p->error_p = TRUE;
16134}
16135
16136VALUE
16137ripper_value(struct parser_params *p)
16138{
16139 (void)yystpcpy; /* may not used in newer bison */
16140
16141 return p->value;
16142}
16143
16144#endif /* RIPPER */
16145/*
16146 * Local variables:
16147 * mode: c
16148 * c-file-style: "ruby"
16149 * End:
16150 */