Ruby: parse.y Source File

Ruby 3.4.1p0 (2024-12-25 revision 48d4efcb85000e1ebae42004e963b5d0cedddcf2)

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);
1483#define reg_fragment_check rb_parser_reg_fragment_check
1484int reg_fragment_check(struct parser_params*, rb_parser_string_t*, int);
1485
1486static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1487static NODE *heredoc_dedent(struct parser_params*,NODE*);
1488
1489static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1490
1491#ifdef RIPPER
1492#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1493#define set_value(val) (p->s_lvalue = val)
1494static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1495static int id_is_var(struct parser_params *p, ID id);
1496#endif
1497
1498RUBY_SYMBOL_EXPORT_BEGIN
1499VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1500int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1501enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1502VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1503void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1504PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1505YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1506YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1507YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1508YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1509YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1510YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1511void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1512RUBY_SYMBOL_EXPORT_END
1513
1514static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1515static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1516static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1517static VALUE formal_argument_error(struct parser_params*, ID);
1518static ID shadowing_lvar(struct parser_params*,ID);
1519static void new_bv(struct parser_params*,ID);
1520
1521static void local_push(struct parser_params*,int);
1522static void local_pop(struct parser_params*);
1523static void local_var(struct parser_params*, ID);
1524static void arg_var(struct parser_params*, ID);
1525static int local_id(struct parser_params *p, ID id);
1526static int local_id_ref(struct parser_params*, ID, ID **);
1527#define internal_id rb_parser_internal_id
1528ID internal_id(struct parser_params*);
1529static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1530static int check_forwarding_args(struct parser_params*);
1531static void add_forwarding_args(struct parser_params *p);
1532static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1533
1534static const struct vtable *dyna_push(struct parser_params *);
1535static void dyna_pop(struct parser_params*, const struct vtable *);
1536static int dyna_in_block(struct parser_params*);
1537#define dyna_var(p, id) local_var(p, id)
1538static int dvar_defined(struct parser_params*, ID);
1539#define dvar_defined_ref rb_parser_dvar_defined_ref
1540int dvar_defined_ref(struct parser_params*, ID, ID**);
1541static int dvar_curr(struct parser_params*,ID);
1542
1543static int lvar_defined(struct parser_params*, ID);
1544
1545static NODE *numparam_push(struct parser_params *p);
1546static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1547
1548#define METHOD_NOT '!'
1549
1550#define idFWD_REST '*'
1551#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1552#define idFWD_BLOCK '&'
1553#define idFWD_ALL idDot3
1554#define arg_FWD_BLOCK idFWD_BLOCK
1555
1556#define RE_ONIG_OPTION_IGNORECASE 1
1557#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1558#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1559#define RE_OPTION_ONCE (1<<16)
1560#define RE_OPTION_ENCODING_SHIFT 8
1561#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1562#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1563#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1564#define RE_OPTION_MASK 0xff
1565#define RE_OPTION_ARG_ENCODING_NONE 32
1566
1567#define CHECK_LITERAL_WHEN (st_table *)1
1568#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1569
1570#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1571RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1572
1573#define TOKEN2ID(tok) ( \
1574 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1575 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1576 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1577 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1578 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1579 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1580 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1581
1582/****** Ripper *******/
1583
1584#ifdef RIPPER
1585
1586#include "eventids1.h"
1587#include "eventids2.h"
1588
1589extern const struct ripper_parser_ids ripper_parser_ids;
1590
1591static VALUE ripper_dispatch0(struct parser_params*,ID);
1592static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1593static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1594static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1595static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1596static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1597static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1598void ripper_error(struct parser_params *p);
1599
1600#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1601#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1602#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1603#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1604#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1605#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1606#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1607
1608#define yyparse ripper_yyparse
1609
1610static VALUE
1611aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1612{
1613 if (!NIL_P(pre_arg)) {
1614 if (!NIL_P(pre_args)) {
1615 rb_ary_unshift(pre_args, pre_arg);
1616 }
1617 else {
1618 pre_args = rb_ary_new_from_args(1, pre_arg);
1619 }
1620 }
1621 return pre_args;
1622}
1623
1624#define ID2VAL(id) STATIC_ID2SYM(id)
1625#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1626#endif /* RIPPER */
1627
1628#define KWD2EID(t, v) keyword_##t
1629
1630static NODE *
1631new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, const YYLTYPE *loc)
1632{
1633 body = remove_begin(body);
1634 reduce_nodes(p, &body);
1635 NODE *n = NEW_SCOPE(args, body, loc);
1636 nd_set_line(n, loc->end_pos.lineno);
1637 set_line_body(body, loc->beg_pos.lineno);
1638 return n;
1639}
1640
1641static NODE *
1642rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1643 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1644{
1645 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1646 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1647 loc.beg_pos = arg_loc->beg_pos;
1648 return NEW_RESCUE(arg, rescue, 0, &loc);
1649}
1650
1651static NODE *add_block_exit(struct parser_params *p, NODE *node);
1652static rb_node_exits_t *init_block_exit(struct parser_params *p);
1653static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1654static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1655static void clear_block_exit(struct parser_params *p, bool error);
1656
1657static void
1658next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1659{
1660 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1661}
1662
1663static void
1664restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1665{
1666 /* See: def_name action */
1667 struct lex_context ctxt = temp->save.ctxt;
1668 p->ctxt.in_def = ctxt.in_def;
1669 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1670 p->ctxt.in_rescue = ctxt.in_rescue;
1671 p->max_numparam = temp->save.max_numparam;
1672 numparam_pop(p, temp->save.numparam_save);
1673 clear_block_exit(p, true);
1674}
1675
1676static void
1677endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1678{
1679 if (is_attrset_id(mid)) {
1680 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1681 }
1682 token_info_drop(p, "def", loc->beg_pos);
1683}
1684
1685#define debug_token_line(p, name, line) do { \
1686 if (p->debug) { \
1687 const char *const pcur = p->lex.pcur; \
1688 const char *const ptok = p->lex.ptok; \
1689 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1690 line, p->ruby_sourceline, \
1691 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1692 } \
1693 } while (0)
1694
1695#define begin_definition(k, loc_beg, loc_end) \
1696 do { \
1697 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1698 /* singleton class */ \
1699 p->ctxt.cant_return = !p->ctxt.in_def; \
1700 p->ctxt.in_def = 0; \
1701 } \
1702 else if (p->ctxt.in_def) { \
1703 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1704 yyerror1(&loc, k " definition in method body"); \
1705 } \
1706 else { \
1707 p->ctxt.cant_return = 1; \
1708 } \
1709 local_push(p, 0); \
1710 } while (0)
1711
1712#ifndef RIPPER
1713# define ifndef_ripper(x) (x)
1714# define ifdef_ripper(r,x) (x)
1715#else
1716# define ifndef_ripper(x)
1717# define ifdef_ripper(r,x) (r)
1718#endif
1719
1720# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1721# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1722# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1723# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1724# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1725# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1726# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1727# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1728# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1729# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1730# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1731# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1732# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1733# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1734# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1735# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1736# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1737# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1738# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1739# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1740#ifdef RIPPER
1741extern const ID id_warn, id_warning, id_gets, id_assoc;
1742# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1743# define WARN_S_L(s,l) STR_NEW(s,l)
1744# define WARN_S(s) STR_NEW2(s)
1745# define WARN_I(i) INT2NUM(i)
1746# define WARN_ID(i) rb_id2str(i)
1747# define PRIsWARN PRIsVALUE
1748# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1749# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1750# ifdef HAVE_VA_ARGS_MACRO
1751# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1752# else
1753# define WARN_CALL rb_funcall
1754# endif
1755# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1756# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1757# ifdef HAVE_VA_ARGS_MACRO
1758# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1759# else
1760# define WARNING_CALL rb_funcall
1761# endif
1762# define compile_error ripper_compile_error
1763#else
1764# define WARN_S_L(s,l) s
1765# define WARN_S(s) s
1766# define WARN_I(i) i
1767# define WARN_ID(i) rb_id2name(i)
1768# define PRIsWARN PRIsVALUE
1769# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1770# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1771# define WARN_CALL rb_compile_warn
1772# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1773# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1774# define WARNING_CALL rb_compile_warning
1775PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1776# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1777#endif
1778
1779#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1780
1781static NODE *
1782add_block_exit(struct parser_params *p, NODE *node)
1783{
1784 if (!node) {
1785 compile_error(p, "unexpected null node");
1786 return 0;
1787 }
1788 switch (nd_type(node)) {
1789 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1790 default:
1791 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1792 return node;
1793 }
1794 if (!p->ctxt.in_defined) {
1795 rb_node_exits_t *exits = p->exits;
1796 if (exits) {
1797 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1798 exits->nd_stts = node;
1799 }
1800 }
1801 return node;
1802}
1803
1804static rb_node_exits_t *
1805init_block_exit(struct parser_params *p)
1806{
1807 rb_node_exits_t *old = p->exits;
1808 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1809 exits->nd_chain = 0;
1810 exits->nd_stts = RNODE(exits);
1811 p->exits = exits;
1812 return old;
1813}
1814
1815static rb_node_exits_t *
1816allow_block_exit(struct parser_params *p)
1817{
1818 rb_node_exits_t *exits = p->exits;
1819 p->exits = 0;
1820 return exits;
1821}
1822
1823static void
1824restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1825{
1826 p->exits = exits;
1827}
1828
1829static void
1830clear_block_exit(struct parser_params *p, bool error)
1831{
1832 rb_node_exits_t *exits = p->exits;
1833 if (!exits) return;
1834 if (error) {
1835 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1836 switch (nd_type(e)) {
1837 case NODE_BREAK:
1838 yyerror1(&e->nd_loc, "Invalid break");
1839 break;
1840 case NODE_NEXT:
1841 yyerror1(&e->nd_loc, "Invalid next");
1842 break;
1843 case NODE_REDO:
1844 yyerror1(&e->nd_loc, "Invalid redo");
1845 break;
1846 default:
1847 yyerror1(&e->nd_loc, "unexpected node");
1848 goto end_checks; /* no nd_chain */
1849 }
1850 }
1851 end_checks:;
1852 }
1853 exits->nd_stts = RNODE(exits);
1854 exits->nd_chain = 0;
1855}
1856
1857#define WARN_EOL(tok) \
1858 (looking_at_eol_p(p) ? \
1859 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1860 (void)0)
1861static int looking_at_eol_p(struct parser_params *p);
1862
1863static NODE *
1864get_nd_value(struct parser_params *p, NODE *node)
1865{
1866 switch (nd_type(node)) {
1867 case NODE_GASGN:
1868 return RNODE_GASGN(node)->nd_value;
1869 case NODE_IASGN:
1870 return RNODE_IASGN(node)->nd_value;
1871 case NODE_LASGN:
1872 return RNODE_LASGN(node)->nd_value;
1873 case NODE_DASGN:
1874 return RNODE_DASGN(node)->nd_value;
1875 case NODE_MASGN:
1876 return RNODE_MASGN(node)->nd_value;
1877 case NODE_CVASGN:
1878 return RNODE_CVASGN(node)->nd_value;
1879 case NODE_CDECL:
1880 return RNODE_CDECL(node)->nd_value;
1881 default:
1882 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1883 return 0;
1884 }
1885}
1886
1887static void
1888set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1889{
1890 switch (nd_type(node)) {
1891 case NODE_CDECL:
1892 RNODE_CDECL(node)->nd_value = rhs;
1893 break;
1894 case NODE_GASGN:
1895 RNODE_GASGN(node)->nd_value = rhs;
1896 break;
1897 case NODE_IASGN:
1898 RNODE_IASGN(node)->nd_value = rhs;
1899 break;
1900 case NODE_LASGN:
1901 RNODE_LASGN(node)->nd_value = rhs;
1902 break;
1903 case NODE_DASGN:
1904 RNODE_DASGN(node)->nd_value = rhs;
1905 break;
1906 case NODE_MASGN:
1907 RNODE_MASGN(node)->nd_value = rhs;
1908 break;
1909 case NODE_CVASGN:
1910 RNODE_CVASGN(node)->nd_value = rhs;
1911 break;
1912 default:
1913 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1914 break;
1915 }
1916}
1917
1918static ID
1919get_nd_vid(struct parser_params *p, NODE *node)
1920{
1921 switch (nd_type(node)) {
1922 case NODE_CDECL:
1923 return RNODE_CDECL(node)->nd_vid;
1924 case NODE_GASGN:
1925 return RNODE_GASGN(node)->nd_vid;
1926 case NODE_IASGN:
1927 return RNODE_IASGN(node)->nd_vid;
1928 case NODE_LASGN:
1929 return RNODE_LASGN(node)->nd_vid;
1930 case NODE_DASGN:
1931 return RNODE_DASGN(node)->nd_vid;
1932 case NODE_CVASGN:
1933 return RNODE_CVASGN(node)->nd_vid;
1934 default:
1935 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1936 return 0;
1937 }
1938}
1939
1940static NODE *
1941get_nd_args(struct parser_params *p, NODE *node)
1942{
1943 switch (nd_type(node)) {
1944 case NODE_CALL:
1945 return RNODE_CALL(node)->nd_args;
1946 case NODE_OPCALL:
1947 return RNODE_OPCALL(node)->nd_args;
1948 case NODE_FCALL:
1949 return RNODE_FCALL(node)->nd_args;
1950 case NODE_QCALL:
1951 return RNODE_QCALL(node)->nd_args;
1952 case NODE_SUPER:
1953 return RNODE_SUPER(node)->nd_args;
1954 case NODE_VCALL:
1955 case NODE_ZSUPER:
1956 case NODE_YIELD:
1957 case NODE_RETURN:
1958 case NODE_BREAK:
1959 case NODE_NEXT:
1960 return 0;
1961 default:
1962 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1963 return 0;
1964 }
1965}
1966
1967static st_index_t
1968djb2(const uint8_t *str, size_t len)
1969{
1970 st_index_t hash = 5381;
1971
1972 for (size_t i = 0; i < len; i++) {
1973 hash = ((hash << 5) + hash) + str[i];
1974 }
1975
1976 return hash;
1977}
1978
1979static st_index_t
1980parser_memhash(const void *ptr, long len)
1981{
1982 return djb2(ptr, len);
1983}
1984
1985#define PARSER_STRING_PTR(str) (str->ptr)
1986#define PARSER_STRING_LEN(str) (str->len)
1987#define PARSER_STRING_END(str) (&str->ptr[str->len])
1988#define STRING_SIZE(str) ((size_t)str->len + 1)
1989#define STRING_TERM_LEN(str) (1)
1990#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
1991#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
1992 SIZED_REALLOC_N(str->ptr, char, (size_t)total + termlen, STRING_SIZE(str)); \
1993 str->len = total; \
1994} while (0)
1995#define STRING_SET_LEN(str, n) do { \
1996 (str)->len = (n); \
1997} while (0)
1998#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
1999 ((ptrvar) = str->ptr, \
2000 (lenvar) = str->len)
2001
2002static inline int
2003parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2004{
2005 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2006}
2007
2008static rb_parser_string_t *
2009rb_parser_string_new(rb_parser_t *p, const char *ptr, long len)
2010{
2011 rb_parser_string_t *str;
2012
2013 if (len < 0) {
2014 rb_bug("negative string size (or size too big): %ld", len);
2015 }
2016
2017 str = xcalloc(1, sizeof(rb_parser_string_t));
2018 str->ptr = xcalloc(len + 1, sizeof(char));
2019
2020 if (ptr) {
2021 memcpy(PARSER_STRING_PTR(str), ptr, len);
2022 }
2023 STRING_SET_LEN(str, len);
2024 STRING_TERM_FILL(str);
2025 return str;
2026}
2027
2028static rb_parser_string_t *
2029rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, long len, rb_encoding *enc)
2030{
2031 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2032 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2033 str->enc = enc;
2034 return str;
2035}
2036
2037#ifndef RIPPER
2038rb_parser_string_t *
2039rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2040{
2041 /* Type check */
2042 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2043 RB_GC_GUARD(str);
2044 return ret;
2045}
2046
2047void
2048rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2049{
2050 if (!str) return;
2051 xfree(PARSER_STRING_PTR(str));
2052 xfree(str);
2053}
2054#endif
2055
2056static st_index_t
2057rb_parser_str_hash(rb_parser_string_t *str)
2058{
2059 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2060}
2061
2062static st_index_t
2063rb_char_p_hash(const char *c)
2064{
2065 return parser_memhash((const void *)c, strlen(c));
2066}
2067
2068static size_t
2069rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2070{
2071 return PARSER_STRING_LEN(str);
2072}
2073
2074#ifndef RIPPER
2075static char *
2076rb_parser_string_end(rb_parser_string_t *str)
2077{
2078 return &str->ptr[str->len];
2079}
2080#endif
2081
2082static void
2083rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2084{
2085 str->enc = enc;
2086}
2087
2088static rb_encoding *
2089rb_parser_str_get_encoding(rb_parser_string_t *str)
2090{
2091 return str->enc;
2092}
2093
2094#ifndef RIPPER
2095static bool
2096PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2097{
2098 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2099}
2100#endif
2101
2102static int
2103PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2104{
2105 return str->coderange;
2106}
2107
2108static void
2109PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2110{
2111 str->coderange = coderange;
2112}
2113
2114static void
2115PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2116{
2117 rb_parser_string_set_encoding(str, enc);
2118 PARSER_ENC_CODERANGE_SET(str, cr);
2119}
2120
2121static void
2122PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2123{
2124 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2125}
2126
2127static bool
2128PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2129{
2130 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2131}
2132
2133static bool
2134PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2135{
2136 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2137}
2138
2139static const char *
2140rb_parser_search_nonascii(const char *p, const char *e)
2141{
2142 const char *s = p;
2143
2144 for (; s < e; s++) {
2145 if (*s & 0x80) return s;
2146 }
2147
2148 return NULL;
2149}
2150
2151static int
2152rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc)
2153{
2154 const char *e = ptr + len;
2155
2156 if (enc == rb_ascii8bit_encoding()) {
2157 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2158 ptr = rb_parser_search_nonascii(ptr, e);
2159 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2160 }
2161
2162 /* parser string encoding is always asciicompat */
2163 ptr = rb_parser_search_nonascii(ptr, e);
2164 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2165 for (;;) {
2166 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2167 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2168 ptr += MBCLEN_CHARFOUND_LEN(ret);
2169 if (ptr == e) break;
2170 ptr = rb_parser_search_nonascii(ptr, e);
2171 if (!ptr) break;
2172 }
2173
2174 return RB_PARSER_ENC_CODERANGE_VALID;
2175}
2176
2177static int
2178rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2179{
2180 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2181}
2182
2183static int
2184rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2185{
2186 int cr = PARSER_ENC_CODERANGE(str);
2187
2188 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2189 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2190 PARSER_ENC_CODERANGE_SET(str, cr);
2191 }
2192
2193 return cr;
2194}
2195
2196static rb_parser_string_t *
2197rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2198{
2199 if (rb_parser_str_get_encoding(str) == enc)
2200 return str;
2201 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2202 PARSER_ENC_CODERANGE_CLEAR(str);
2203 }
2204 rb_parser_string_set_encoding(str, enc);
2205 return str;
2206}
2207
2208static bool
2209rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2210{
2211 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2212}
2213
2214static rb_encoding *
2215rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2216{
2217 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2218 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2219
2220 if (enc1 == NULL || enc2 == NULL)
2221 return 0;
2222
2223 if (enc1 == enc2) {
2224 return enc1;
2225 }
2226
2227 if (PARSER_STRING_LEN(str2) == 0)
2228 return enc1;
2229 if (PARSER_STRING_LEN(str1) == 0)
2230 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2231
2232 int cr1, cr2;
2233
2234 cr1 = rb_parser_enc_str_coderange(p, str1);
2235 cr2 = rb_parser_enc_str_coderange(p, str2);
2236
2237 if (cr1 != cr2) {
2238 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2239 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2240 }
2241
2242 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2243 return enc1;
2244 }
2245
2246 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2247 return enc2;
2248 }
2249
2250 return 0;
2251}
2252
2253static void
2254rb_parser_str_modify(rb_parser_string_t *str)
2255{
2256 PARSER_ENC_CODERANGE_CLEAR(str);
2257}
2258
2259static void
2260rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len)
2261{
2262 long capa;
2263 const int termlen = STRING_TERM_LEN(str);
2264
2265 if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2266 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
2267 }
2268
2269 int cr = PARSER_ENC_CODERANGE(str);
2270 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2271 /* Leave unknown. */
2272 }
2273 else if (len > PARSER_STRING_LEN(str)) {
2274 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2275 }
2276 else if (len < PARSER_STRING_LEN(str)) {
2277 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2278 /* ASCII-only string is keeping after truncated. Valid
2279 * and broken may be invalid or valid, leave unknown. */
2280 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2281 }
2282 }
2283
2284 STRING_SET_LEN(str, len);
2285 STRING_TERM_FILL(str);
2286}
2287
2288static rb_parser_string_t *
2289rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len)
2290{
2291 rb_parser_str_modify(str);
2292 if (len == 0) return 0;
2293
2294 long total, olen, off = -1;
2295 char *sptr;
2296 const int termlen = STRING_TERM_LEN(str);
2297
2298 PARSER_STRING_GETMEM(str, sptr, olen);
2299 if (ptr >= sptr && ptr <= sptr + olen) {
2300 off = ptr - sptr;
2301 }
2302
2303 if (olen > LONG_MAX - len) {
2304 compile_error(p, "string sizes too big");
2305 return 0;
2306 }
2307 total = olen + len;
2308 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2309 sptr = PARSER_STRING_PTR(str);
2310 if (off != -1) {
2311 ptr = sptr + off;
2312 }
2313 memcpy(sptr + olen, ptr, len);
2314 STRING_SET_LEN(str, total);
2315 STRING_TERM_FILL(str);
2316
2317 return str;
2318}
2319
2320#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2321#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2322
2323static rb_parser_string_t *
2324rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2325 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2326{
2327 int str_cr, res_cr;
2328 rb_encoding *str_enc, *res_enc;
2329
2330 str_enc = rb_parser_str_get_encoding(str);
2331 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2332
2333 if (str_enc == ptr_enc) {
2334 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2335 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2336 }
2337 }
2338 else {
2339 /* parser string encoding is always asciicompat */
2340 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2341 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2342 }
2343 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2344 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2345 str_cr = rb_parser_enc_str_coderange(p, str);
2346 }
2347 }
2348 }
2349 if (ptr_cr_ret)
2350 *ptr_cr_ret = ptr_cr;
2351
2352 if (str_enc != ptr_enc &&
2353 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2354 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2355 goto incompatible;
2356 }
2357
2358 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2359 res_enc = str_enc;
2360 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2361 }
2362 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2363 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2364 res_enc = str_enc;
2365 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2366 }
2367 else {
2368 res_enc = ptr_enc;
2369 res_cr = ptr_cr;
2370 }
2371 }
2372 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2373 res_enc = str_enc;
2374 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2375 res_cr = str_cr;
2376 else
2377 res_cr = ptr_cr;
2378 }
2379 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2380 res_enc = str_enc;
2381 res_cr = str_cr;
2382 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2383 }
2384
2385 if (len < 0) {
2386 compile_error(p, "negative string size (or size too big)");
2387 }
2388 parser_str_cat(str, ptr, len);
2389 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2390 return str;
2391
2392 incompatible:
2393 compile_error(p, "incompatible character encodings: %s and %s",
2394 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2395 UNREACHABLE_RETURN(0);
2396
2397}
2398
2399static rb_parser_string_t *
2400rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2401 rb_encoding *ptr_enc)
2402{
2403 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2404}
2405
2406static rb_parser_string_t *
2407rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2408{
2409 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2410
2411 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2412 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2413
2414 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2415
2416 return str;
2417}
2418
2419static rb_parser_string_t *
2420rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len)
2421{
2422 if (len < 0) {
2423 rb_bug("negative string size (or size too big)");
2424 }
2425
2426 long slen = PARSER_STRING_LEN(str);
2427
2428 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2429 PARSER_ENC_CODERANGE_CLEAR(str);
2430 }
2431
2432 {
2433 long capa;
2434 const int termlen = STRING_TERM_LEN(str);
2435
2436 if ((capa = slen) < len) {
2437 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2438 }
2439 else if (len == slen) return str;
2440 STRING_SET_LEN(str, len);
2441 STRING_TERM_FILL(str);
2442 }
2443 return str;
2444}
2445
2446# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2447 ((ptrvar) = str->ptr, \
2448 (lenvar) = str->len, \
2449 (encvar) = str->enc)
2450
2451static int
2452rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2453{
2454 long len1, len2;
2455 const char *ptr1, *ptr2;
2456 rb_encoding *enc1, *enc2;
2457
2458 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2459 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2460
2461 return (len1 != len2 ||
2462 enc1 != enc2 ||
2463 memcmp(ptr1, ptr2, len1) != 0);
2464}
2465
2466static void
2467rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2468{
2469 long i;
2470 if (ary->capa < len) {
2471 ary->capa = len;
2472 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2473 for (i = ary->len; i < len; i++) {
2474 ary->data[i] = 0;
2475 }
2476 }
2477}
2478
2479/*
2480 * Do not call this directly.
2481 * Use rb_parser_ary_new_capa_for_XXX() instead.
2482 */
2483static rb_parser_ary_t *
2484parser_ary_new_capa(rb_parser_t *p, long len)
2485{
2486 if (len < 0) {
2487 rb_bug("negative array size (or size too big): %ld", len);
2488 }
2489 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2490 ary->data_type = 0;
2491 ary->len = 0;
2492 ary->capa = len;
2493 if (0 < len) {
2494 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2495 }
2496 else {
2497 ary->data = NULL;
2498 }
2499 return ary;
2500}
2501
2502#ifndef RIPPER
2503static rb_parser_ary_t *
2504rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2505{
2506 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2507 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2508 return ary;
2509}
2510
2511static rb_parser_ary_t *
2512rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2513{
2514 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2515 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2516 return ary;
2517}
2518#endif
2519
2520static rb_parser_ary_t *
2521rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2522{
2523 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2524 ary->data_type = PARSER_ARY_DATA_NODE;
2525 return ary;
2526}
2527
2528/*
2529 * Do not call this directly.
2530 * Use rb_parser_ary_push_XXX() instead.
2531 */
2532static rb_parser_ary_t *
2533parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2534{
2535 if (ary->len == ary->capa) {
2536 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2537 }
2538 ary->data[ary->len++] = val;
2539 return ary;
2540}
2541
2542#ifndef RIPPER
2543static rb_parser_ary_t *
2544rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2545{
2546 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2547 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2548 }
2549 return parser_ary_push(p, ary, val);
2550}
2551
2552static rb_parser_ary_t *
2553rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2554{
2555 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2556 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2557 }
2558 return parser_ary_push(p, ary, val);
2559}
2560#endif
2561
2562static rb_parser_ary_t *
2563rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2564{
2565 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2566 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2567 }
2568 return parser_ary_push(p, ary, val);
2569}
2570
2571#ifndef RIPPER
2572static void
2573rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2574{
2575 if (!token) return;
2576 rb_parser_string_free(p, token->str);
2577 xfree(token);
2578}
2579
2580static void
2581rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2582{
2583# define foreach_ary(ptr) \
2584 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2585 ptr < end_ary_data; ptr++)
2586 switch (ary->data_type) {
2587 case PARSER_ARY_DATA_AST_TOKEN:
2588 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2589 break;
2590 case PARSER_ARY_DATA_SCRIPT_LINE:
2591 foreach_ary(data) {rb_parser_string_free(p, *data);}
2592 break;
2593 case PARSER_ARY_DATA_NODE:
2594 /* Do nothing because nodes are freed when rb_ast_t is freed */
2595 break;
2596 default:
2597 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2598 break;
2599 }
2600# undef foreach_ary
2601 xfree(ary->data);
2602 xfree(ary);
2603}
2604
2605#endif /* !RIPPER */
2606%}
2607
2608%expect 0
2609%define api.pure
2610%define parse.error verbose
2611%printer {
2612 if ((NODE *)$$ == (NODE *)-1) {
2613 rb_parser_printf(p, "NODE_SPECIAL");
2614 }
2615 else if ($$) {
2616 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2617 }
2618} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2619 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2620%printer {
2621 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2622} <id>
2623%printer {
2624 switch (nd_type(RNODE($$))) {
2625 case NODE_INTEGER:
2626 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2627 break;
2628 case NODE_FLOAT:
2629 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2630 break;
2631 case NODE_RATIONAL:
2632 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2633 break;
2634 case NODE_IMAGINARY:
2635 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2636 break;
2637 default:
2638 break;
2639 }
2640} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2641%printer {
2642 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2643} tNTH_REF
2644%printer {
2645 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2646} tBACK_REF
2647
2648%destructor {
2649 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2650} <labels>
2651
2652%lex-param {struct parser_params *p}
2653%parse-param {struct parser_params *p}
2654%initial-action
2655{
2656 RUBY_SET_YYLLOC_OF_NONE(@$);
2657};
2658%after-shift after_shift
2659%before-reduce before_reduce
2660%after-reduce after_reduce
2661%after-shift-error-token after_shift_error_token
2662%after-pop-stack after_pop_stack
2663
2664%union {
2665 NODE *node;
2666 rb_node_fcall_t *node_fcall;
2667 rb_node_args_t *node_args;
2668 rb_node_args_aux_t *node_args_aux;
2669 rb_node_opt_arg_t *node_opt_arg;
2670 rb_node_kw_arg_t *node_kw_arg;
2671 rb_node_block_pass_t *node_block_pass;
2672 rb_node_masgn_t *node_masgn;
2673 rb_node_def_temp_t *node_def_temp;
2674 rb_node_exits_t *node_exits;
2675 ID id;
2676 int num;
2677 st_table *tbl;
2678 st_table *labels;
2679 const struct vtable *vars;
2680 struct rb_strterm_struct *strterm;
2681 struct lex_context ctxt;
2682 enum lex_state_e state;
2683}
2684
2685%token <id>
2686 keyword_class "'class'"
2687 keyword_module "'module'"
2688 keyword_def "'def'"
2689 keyword_undef "'undef'"
2690 keyword_begin "'begin'"
2691 keyword_rescue "'rescue'"
2692 keyword_ensure "'ensure'"
2693 keyword_end "'end'"
2694 keyword_if "'if'"
2695 keyword_unless "'unless'"
2696 keyword_then "'then'"
2697 keyword_elsif "'elsif'"
2698 keyword_else "'else'"
2699 keyword_case "'case'"
2700 keyword_when "'when'"
2701 keyword_while "'while'"
2702 keyword_until "'until'"
2703 keyword_for "'for'"
2704 keyword_break "'break'"
2705 keyword_next "'next'"
2706 keyword_redo "'redo'"
2707 keyword_retry "'retry'"
2708 keyword_in "'in'"
2709 keyword_do "'do'"
2710 keyword_do_cond "'do' for condition"
2711 keyword_do_block "'do' for block"
2712 keyword_do_LAMBDA "'do' for lambda"
2713 keyword_return "'return'"
2714 keyword_yield "'yield'"
2715 keyword_super "'super'"
2716 keyword_self "'self'"
2717 keyword_nil "'nil'"
2718 keyword_true "'true'"
2719 keyword_false "'false'"
2720 keyword_and "'and'"
2721 keyword_or "'or'"
2722 keyword_not "'not'"
2723 modifier_if "'if' modifier"
2724 modifier_unless "'unless' modifier"
2725 modifier_while "'while' modifier"
2726 modifier_until "'until' modifier"
2727 modifier_rescue "'rescue' modifier"
2728 keyword_alias "'alias'"
2729 keyword_defined "'defined?'"
2730 keyword_BEGIN "'BEGIN'"
2731 keyword_END "'END'"
2732 keyword__LINE__ "'__LINE__'"
2733 keyword__FILE__ "'__FILE__'"
2734 keyword__ENCODING__ "'__ENCODING__'"
2735
2736%token <id> tIDENTIFIER "local variable or method"
2737%token <id> tFID "method"
2738%token <id> tGVAR "global variable"
2739%token <id> tIVAR "instance variable"
2740%token <id> tCONSTANT "constant"
2741%token <id> tCVAR "class variable"
2742%token <id> tLABEL "label"
2743%token <node> tINTEGER "integer literal"
2744%token <node> tFLOAT "float literal"
2745%token <node> tRATIONAL "rational literal"
2746%token <node> tIMAGINARY "imaginary literal"
2747%token <node> tCHAR "char literal"
2748%token <node> tNTH_REF "numbered reference"
2749%token <node> tBACK_REF "back reference"
2750%token <node> tSTRING_CONTENT "literal content"
2751%token <num> tREGEXP_END
2752%token <num> tDUMNY_END "dummy end"
2753
2754%type <node> singleton strings string string1 xstring regexp
2755%type <node> string_contents xstring_contents regexp_contents string_content
2756%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2757%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2758%type <node_def_temp> defn_head defs_head k_def
2759%type <node_exits> block_open k_while k_until k_for allow_exits
2760%type <node> top_compstmt top_stmts top_stmt begin_block endless_arg endless_command
2761%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
2762%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2763%type <node_fcall> fcall
2764%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2765%type <node> args arg_splat call_args opt_call_args
2766%type <node> paren_args opt_paren_args
2767%type <node_args> args_tail block_args_tail
2768%type <node> command_args aref_args
2769%type <node_block_pass> opt_block_arg block_arg
2770%type <node> var_ref var_lhs
2771%type <node> command_rhs arg_rhs
2772%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2773%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args
2774%type <node_args_aux> f_arg f_arg_item
2775%type <node> f_marg f_marg_list f_rest_marg
2776%type <node_masgn> f_margs
2777%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2778%type <node_args> block_param opt_block_param block_param_def
2779%type <node_kw_arg> f_kw f_block_kw
2780%type <id> bv_decls opt_bv_decl bvar
2781%type <node> lambda lambda_body brace_body do_body
2782%type <node_args> f_larglist
2783%type <node> brace_block cmd_brace_block do_block lhs none fitem
2784%type <node> mlhs_head mlhs_item mlhs_node mlhs_post
2785%type <node_masgn> mlhs mlhs_basic mlhs_inner
2786%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2787%type <node> p_expr p_as p_alt p_expr_basic p_find
2788%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2789%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2790%type <node> p_kwargs p_kwarg p_kw
2791%type <id> keyword_variable user_variable sym operation operation2 operation3
2792%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
2793%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2794%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2795%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2796%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2797%type <ctxt> p_in_kwarg
2798%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2799%type <num> max_numparam
2800%type <node> numparam
2801%type <id> it_id
2802%token END_OF_INPUT 0 "end-of-input"
2803%token <id> '.'
2804
2805/* escaped chars, should be ignored otherwise */
2806%token <id> '\\' "backslash"
2807%token tSP "escaped space"
2808%token <id> '\t' "escaped horizontal tab"
2809%token <id> '\f' "escaped form feed"
2810%token <id> '\r' "escaped carriage return"
2811%token <id> '\13' "escaped vertical tab"
2812%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2813%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2814%token tPOW RUBY_TOKEN(POW) "**"
2815%token tCMP RUBY_TOKEN(CMP) "<=>"
2816%token tEQ RUBY_TOKEN(EQ) "=="
2817%token tEQQ RUBY_TOKEN(EQQ) "==="
2818%token tNEQ RUBY_TOKEN(NEQ) "!="
2819%token tGEQ RUBY_TOKEN(GEQ) ">="
2820%token tLEQ RUBY_TOKEN(LEQ) "<="
2821%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2822%token tOROP RUBY_TOKEN(OROP) "||"
2823%token tMATCH RUBY_TOKEN(MATCH) "=~"
2824%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2825%token tDOT2 RUBY_TOKEN(DOT2) ".."
2826%token tDOT3 RUBY_TOKEN(DOT3) "..."
2827%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2828%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2829%token tAREF RUBY_TOKEN(AREF) "[]"
2830%token tASET RUBY_TOKEN(ASET) "[]="
2831%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2832%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2833%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2834%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2835%token tCOLON3 ":: at EXPR_BEG"
2836%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2837%token tASSOC "=>"
2838%token tLPAREN "("
2839%token tLPAREN_ARG "( arg"
2840%token tRPAREN ")"
2841%token tLBRACK "["
2842%token tLBRACE "{"
2843%token tLBRACE_ARG "{ arg"
2844%token tSTAR "*"
2845%token tDSTAR "**arg"
2846%token tAMPER "&"
2847%token <num> tLAMBDA "->"
2848%token tSYMBEG "symbol literal"
2849%token tSTRING_BEG "string literal"
2850%token tXSTRING_BEG "backtick literal"
2851%token tREGEXP_BEG "regexp literal"
2852%token tWORDS_BEG "word list"
2853%token tQWORDS_BEG "verbatim word list"
2854%token tSYMBOLS_BEG "symbol list"
2855%token tQSYMBOLS_BEG "verbatim symbol list"
2856%token tSTRING_END "terminator"
2857%token tSTRING_DEND "'}'"
2858%token <state> tSTRING_DBEG "'#{'"
2859%token tSTRING_DVAR tLAMBEG tLABEL_END
2860
2861%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2862%token tHEREDOC_BEG tHEREDOC_END k__END__
2863
2864/*
2865 * precedence table
2866 */
2867
2868%nonassoc tLOWEST
2869%nonassoc tLBRACE_ARG
2870
2871%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2872%left keyword_or keyword_and
2873%right keyword_not
2874%nonassoc keyword_defined
2875%right '=' tOP_ASGN
2876%left modifier_rescue
2877%right '?' ':'
2878%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2879%left tOROP
2880%left tANDOP
2881%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2882%left '>' tGEQ '<' tLEQ
2883%left '|' '^'
2884%left '&'
2885%left tLSHFT tRSHFT
2886%left '+' '-'
2887%left '*' '/' '%'
2888%right tUMINUS_NUM tUMINUS
2889%right tPOW
2890%right '!' '~' tUPLUS
2891
2892%token tLAST_TOKEN
2893
2894/*
2895 * inlining rules
2896 */
2897%rule %inline ident_or_const
2898 : tIDENTIFIER
2899 | tCONSTANT
2900 ;
2901
2902%rule %inline user_or_keyword_variable
2903 : user_variable
2904 | keyword_variable
2905 ;
2906
2907/*
2908 * parameterizing rules
2909 */
2910%rule f_opt(value) <node_opt_arg>
2911 : f_arg_asgn f_eq value
2912 {
2913 p->ctxt.in_argdef = 1;
2914 $$ = NEW_OPT_ARG(assignable(p, $1, $3, &@$), &@$);
2915 /*% ripper: [$:$, $:3] %*/
2916 }
2917 ;
2918
2919%rule f_optarg(value) <node_opt_arg>
2920 : f_opt(value)
2921 {
2922 $$ = $1;
2923 /*% ripper: rb_ary_new3(1, $:1) %*/
2924 }
2925 | f_optarg(value) ',' f_opt(value)
2926 {
2927 $$ = opt_arg_append($1, $3);
2928 /*% ripper: rb_ary_push($:1, $:3) %*/
2929 }
2930 ;
2931
2932%rule f_kwarg(kw) <node_kw_arg>
2933 : kw
2934 {
2935 $$ = $1;
2936 /*% ripper: rb_ary_new3(1, $:1) %*/
2937 }
2938 | f_kwarg(kw) ',' kw
2939 {
2940 $$ = kwd_append($1, $3);
2941 /*% ripper: rb_ary_push($:1, $:3) %*/
2942 }
2943 ;
2944
2945%rule opt_args_tail(tail) <node_args>
2946 : ',' tail
2947 {
2948 $$ = $2;
2949 /*% ripper: $:2 %*/
2950 }
2951 | /* none */
2952 {
2953 $$ = new_args_tail(p, 0, 0, 0, &@0);
2954 /*% ripper: [Qnil, Qnil, Qnil] %*/
2955 }
2956 ;
2957
2958%rule words(begin, word_list)
2959 : begin ' '+ word_list tSTRING_END
2960 {
2961 $$ = make_list($3, &@$);
2962 /*% ripper: array!($:3) %*/
2963 }
2964 ;
2965
2966%%
2967program : {
2968 SET_LEX_STATE(EXPR_BEG);
2969 local_push(p, ifndef_ripper(1)+0);
2970 /* jumps are possible in the top-level loop. */
2971 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
2972 }
2973 top_compstmt
2974 {
2975 if ($2 && !compile_for_eval) {
2976 NODE *node = $2;
2977 /* last expression should not be void */
2978 if (nd_type_p(node, NODE_BLOCK)) {
2979 while (RNODE_BLOCK(node)->nd_next) {
2980 node = RNODE_BLOCK(node)->nd_next;
2981 }
2982 node = RNODE_BLOCK(node)->nd_head;
2983 }
2984 node = remove_begin(node);
2985 void_expr(p, node);
2986 }
2987 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
2988 /*% ripper[final]: program!($:2) %*/
2989 local_pop(p);
2990 }
2991 ;
2992
2993top_compstmt : top_stmts terms?
2994 {
2995 void_stmts(p, $$ = $1);
2996 }
2997 ;
2998
2999top_stmts : none
3000 {
3001 $$ = NEW_BEGIN(0, &@$);
3002 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3003 }
3004 | top_stmt
3005 {
3006 $$ = newline_node($1);
3007 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3008 }
3009 | top_stmts terms top_stmt
3010 {
3011 $$ = block_append(p, $1, newline_node($3));
3012 /*% ripper: stmts_add!($:1, $:3) %*/
3013 }
3014 ;
3015
3016top_stmt : stmt
3017 {
3018 clear_block_exit(p, true);
3019 $$ = $1;
3020 }
3021 | keyword_BEGIN begin_block
3022 {
3023 $$ = $2;
3024 /*% ripper: $:2 %*/
3025 }
3026 ;
3027
3028block_open : '{' {$$ = init_block_exit(p);};
3029
3030begin_block : block_open top_compstmt '}'
3031 {
3032 restore_block_exit(p, $block_open);
3033 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3034 NEW_BEGIN($2, &@$));
3035 $$ = NEW_BEGIN(0, &@$);
3036 /*% ripper: BEGIN!($:2) %*/
3037 }
3038 ;
3039
3040bodystmt : compstmt[body]
3041 lex_ctxt[ctxt]
3042 opt_rescue
3043 k_else
3044 {
3045 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3046 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3047 }
3048 compstmt[elsebody]
3049 {
3050 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3051 }
3052 opt_ensure
3053 {
3054 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3055 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3056 }
3057 | compstmt[body]
3058 lex_ctxt[ctxt]
3059 opt_rescue
3060 {
3061 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3062 }
3063 opt_ensure
3064 {
3065 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3066 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3067 }
3068 ;
3069
3070compstmt : stmts terms?
3071 {
3072 void_stmts(p, $$ = $1);
3073 }
3074 ;
3075
3076stmts : none
3077 {
3078 $$ = NEW_BEGIN(0, &@$);
3079 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3080 }
3081 | stmt_or_begin
3082 {
3083 $$ = newline_node($1);
3084 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3085 }
3086 | stmts terms stmt_or_begin
3087 {
3088 $$ = block_append(p, $1, newline_node($3));
3089 /*% ripper: stmts_add!($:1, $:3) %*/
3090 }
3091 ;
3092
3093stmt_or_begin : stmt
3094 {
3095 $$ = $1;
3096 }
3097 | keyword_BEGIN
3098 {
3099 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3100 }
3101 begin_block
3102 {
3103 $$ = $3;
3104 }
3105 ;
3106
3107allow_exits : {$$ = allow_block_exit(p);};
3108
3109k_END : keyword_END lex_ctxt
3110 {
3111 $$ = $2;
3112 p->ctxt.in_rescue = before_rescue;
3113 /*% ripper: $:2 %*/
3114 };
3115
3116stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3117 {
3118 $$ = NEW_ALIAS($2, $4, &@$, &@1);
3119 /*% ripper: alias!($:2, $:4) %*/
3120 }
3121 | keyword_alias tGVAR tGVAR
3122 {
3123 $$ = NEW_VALIAS($2, $3, &@$, &@1);
3124 /*% ripper: var_alias!($:2, $:3) %*/
3125 }
3126 | keyword_alias tGVAR tBACK_REF
3127 {
3128 char buf[2];
3129 buf[0] = '$';
3130 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
3131 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$, &@1);
3132 /*% ripper: var_alias!($:2, $:3) %*/
3133 }
3134 | keyword_alias tGVAR tNTH_REF
3135 {
3136 static const char mesg[] = "can't make alias for the number variables";
3137 /*%%%*/
3138 yyerror1(&@3, mesg);
3139 /*% %*/
3140 $$ = NEW_ERROR(&@$);
3141 /*% ripper[error]: alias_error!(ERR_MESG(), $:3) %*/
3142 }
3143 | keyword_undef undef_list
3144 {
3145 nd_set_first_loc($2, @1.beg_pos);
3146 RNODE_UNDEF($2)->keyword_loc = @1;
3147 $$ = $2;
3148 /*% ripper: undef!($:2) %*/
3149 }
3150 | stmt modifier_if expr_value
3151 {
3152 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
3153 fixpos($$, $3);
3154 /*% ripper: if_mod!($:3, $:1) %*/
3155 }
3156 | stmt modifier_unless expr_value
3157 {
3158 $$ = new_unless(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3159 fixpos($$, $3);
3160 /*% ripper: unless_mod!($:3, $:1) %*/
3161 }
3162 | stmt modifier_while expr_value
3163 {
3164 clear_block_exit(p, false);
3165 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3166 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3167 }
3168 else {
3169 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3170 }
3171 /*% ripper: while_mod!($:3, $:1) %*/
3172 }
3173 | stmt modifier_until expr_value
3174 {
3175 clear_block_exit(p, false);
3176 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3177 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3178 }
3179 else {
3180 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3181 }
3182 /*% ripper: until_mod!($:3, $:1) %*/
3183 }
3184 | stmt modifier_rescue after_rescue stmt
3185 {
3186 p->ctxt.in_rescue = $3.in_rescue;
3187 NODE *resq;
3188 YYLTYPE loc = code_loc_gen(&@2, &@4);
3189 resq = NEW_RESBODY(0, 0, remove_begin($4), 0, &loc);
3190 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
3191 /*% ripper: rescue_mod!($:1, $:4) %*/
3192 }
3193 | k_END allow_exits '{' compstmt '}'
3194 {
3195 if (p->ctxt.in_def) {
3196 rb_warn0("END in method; use at_exit");
3197 }
3198 restore_block_exit(p, $allow_exits);
3199 p->ctxt = $k_END;
3200 {
3201 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, &@$);
3202 $$ = NEW_POSTEXE(scope, &@$);
3203 }
3204 /*% ripper: END!($:compstmt) %*/
3205 }
3206 | command_asgn
3207 | mlhs '=' lex_ctxt command_call
3208 {
3209 value_expr($4);
3210 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3211 /*% ripper: massign!($:1, $:4) %*/
3212 }
3213 | lhs '=' lex_ctxt mrhs
3214 {
3215 $$ = node_assign(p, $1, $4, $3, &@$);
3216 /*% ripper: assign!($:1, $:4) %*/
3217 }
3218 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
3219 after_rescue stmt[resbody]
3220 {
3221 p->ctxt.in_rescue = $3.in_rescue;
3222 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3223 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3224 loc.beg_pos = @mrhs_arg.beg_pos;
3225 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3226 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
3227 /*% ripper: massign!($:1, rescue_mod!($:4, $:7)) %*/
3228 }
3229 | mlhs '=' lex_ctxt mrhs_arg
3230 {
3231 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3232 /*% ripper: massign!($:1, $:4) %*/
3233 }
3234 | expr
3235 | error
3236 {
3237 (void)yynerrs;
3238 $$ = NEW_ERROR(&@$);
3239 }
3240 ;
3241
3242command_asgn : lhs '=' lex_ctxt command_rhs
3243 {
3244 $$ = node_assign(p, $1, $4, $3, &@$);
3245 /*% ripper: assign!($:1, $:4) %*/
3246 }
3247 | var_lhs tOP_ASGN lex_ctxt command_rhs
3248 {
3249 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
3250 /*% ripper: opassign!($:1, $:2, $:4) %*/
3251 }
3252 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
3253 {
3254 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$, &NULL_LOC, &@2, &@4, &@5);
3255 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3256
3257 }
3258 | primary_value call_op ident_or_const tOP_ASGN lex_ctxt command_rhs
3259 {
3260 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$, &@2, &@3, &@4);
3261 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3262 }
3263 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
3264 {
3265 YYLTYPE loc = code_loc_gen(&@1, &@3);
3266 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
3267 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3268 }
3269 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
3270 {
3271 $$ = new_attr_op_assign(p, $1, idCOLON2, $3, $4, $6, &@$, &@2, &@3, &@4);
3272 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3273 }
3274 | defn_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
3275 {
3276 endless_method_name(p, $head->nd_mid, &@head);
3277 restore_defun(p, $head);
3278 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3279 ($$ = $head->nd_def)->nd_loc = @$;
3280 RNODE_DEFN($$)->nd_defn = $bodystmt;
3281 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
3282 /*% ripper: def!($:head, $:args, $:$) %*/
3283 local_pop(p);
3284 }
3285 | defs_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
3286 {
3287 endless_method_name(p, $head->nd_mid, &@head);
3288 restore_defun(p, $head);
3289 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3290 ($$ = $head->nd_def)->nd_loc = @$;
3291 RNODE_DEFS($$)->nd_defn = $bodystmt;
3292 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
3293 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
3294 local_pop(p);
3295 }
3296 | backref tOP_ASGN lex_ctxt command_rhs
3297 {
3298 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3299 $$ = NEW_ERROR(&@$);
3300 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3301 }
3302 ;
3303
3304endless_command : command
3305 | endless_command modifier_rescue after_rescue arg
3306 {
3307 p->ctxt.in_rescue = $3.in_rescue;
3308 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3309 /*% ripper: rescue_mod!($:1, $:4) %*/
3310 }
3311 | keyword_not '\n'? endless_command
3312 {
3313 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3314 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3315 }
3316 ;
3317
3318command_rhs : command_call %prec tOP_ASGN
3319 {
3320 value_expr($1);
3321 $$ = $1;
3322 }
3323 | command_call modifier_rescue after_rescue stmt
3324 {
3325 p->ctxt.in_rescue = $3.in_rescue;
3326 YYLTYPE loc = code_loc_gen(&@2, &@4);
3327 value_expr($1);
3328 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3329 /*% ripper: rescue_mod!($:1, $:4) %*/
3330 }
3331 | command_asgn
3332 ;
3333
3334expr : command_call
3335 | expr keyword_and expr
3336 {
3337 $$ = logop(p, idAND, $1, $3, &@2, &@$);
3338 /*% ripper: binary!($:1, ID2VAL(idAND), $:3) %*/
3339 }
3340 | expr keyword_or expr
3341 {
3342 $$ = logop(p, idOR, $1, $3, &@2, &@$);
3343 /*% ripper: binary!($:1, ID2VAL(idOR), $:3) %*/
3344 }
3345 | keyword_not '\n'? expr
3346 {
3347 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3348 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3349 }
3350 | '!' command_call
3351 {
3352 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3353 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
3354 }
3355 | arg tASSOC
3356 {
3357 value_expr($arg);
3358 }
3359 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3360 p_top_expr_body[body]
3361 {
3362 pop_pktbl(p, $p_pktbl);
3363 pop_pvtbl(p, $p_pvtbl);
3364 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3365 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body), &@$, &NULL_LOC, &NULL_LOC);
3366 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3367 }
3368 | arg keyword_in
3369 {
3370 value_expr($arg);
3371 }
3372 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3373 p_top_expr_body[body]
3374 {
3375 pop_pktbl(p, $p_pktbl);
3376 pop_pvtbl(p, $p_pvtbl);
3377 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3378 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body), &@$, &NULL_LOC, &NULL_LOC);
3379 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3380 }
3381 | arg %prec tLBRACE_ARG
3382 ;
3383
3384def_name : fname
3385 {
3386 ID fname = $1;
3387 numparam_name(p, fname);
3388 local_push(p, 0);
3389 p->ctxt.in_def = 1;
3390 p->ctxt.in_rescue = before_rescue;
3391 p->ctxt.cant_return = 0;
3392 $$ = $1;
3393 }
3394 ;
3395
3396defn_head : k_def def_name
3397 {
3398 $$ = def_head_save(p, $k_def);
3399 $$->nd_mid = $def_name;
3400 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3401 /*% ripper: $:def_name %*/
3402 }
3403 ;
3404
3405defs_head : k_def singleton dot_or_colon
3406 {
3407 SET_LEX_STATE(EXPR_FNAME);
3408 }
3409 def_name
3410 {
3411 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3412 $$ = def_head_save(p, $k_def);
3413 $$->nd_mid = $def_name;
3414 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3415 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3416 }
3417 ;
3418
3419expr_value : expr
3420 {
3421 value_expr($1);
3422 $$ = $1;
3423 }
3424 | error
3425 {
3426 $$ = NEW_ERROR(&@$);
3427 }
3428 ;
3429
3430expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3431 {
3432 $$ = $2;
3433 /*% ripper: $:2 %*/
3434 }
3435 ;
3436
3437command_call : command
3438 | block_command
3439 ;
3440
3441block_command : block_call
3442 | block_call call_op2 operation2 command_args
3443 {
3444 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3445 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3446 }
3447 ;
3448
3449cmd_brace_block : tLBRACE_ARG brace_body '}'
3450 {
3451 $$ = $2;
3452 set_embraced_location($$, &@1, &@3);
3453 /*% ripper: $:2 %*/
3454 }
3455 ;
3456
3457fcall : operation
3458 {
3459 $$ = NEW_FCALL($1, 0, &@$);
3460 /*% ripper: $:1 %*/
3461 }
3462 ;
3463
3464command : fcall command_args %prec tLOWEST
3465 {
3466 $1->nd_args = $2;
3467 nd_set_last_loc($1, @2.end_pos);
3468 $$ = (NODE *)$1;
3469 /*% ripper: command!($:1, $:2) %*/
3470 }
3471 | fcall command_args cmd_brace_block
3472 {
3473 block_dup_check(p, $2, $3);
3474 $1->nd_args = $2;
3475 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3476 fixpos($$, RNODE($1));
3477 nd_set_last_loc($1, @2.end_pos);
3478 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3479 }
3480 | primary_value call_op operation2 command_args %prec tLOWEST
3481 {
3482 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3483 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3484 }
3485 | primary_value call_op operation2 command_args cmd_brace_block
3486 {
3487 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3488 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3489 }
3490 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3491 {
3492 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3493 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3494 }
3495 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3496 {
3497 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3498 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3499 }
3500 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3501 {
3502 set_embraced_location($5, &@4, &@6);
3503 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3504 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3505 }
3506 | keyword_super command_args
3507 {
3508 $$ = NEW_SUPER($2, &@$);
3509 fixpos($$, $2);
3510 /*% ripper: super!($:2) %*/
3511 }
3512 | k_yield command_args
3513 {
3514 $$ = new_yield(p, $2, &@$);
3515 fixpos($$, $2);
3516 /*% ripper: yield!($:2) %*/
3517 }
3518 | k_return call_args
3519 {
3520 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3521 /*% ripper: return!($:2) %*/
3522 }
3523 | keyword_break call_args
3524 {
3525 NODE *args = 0;
3526 args = ret_args(p, $2);
3527 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3528 /*% ripper: break!($:2) %*/
3529 }
3530 | keyword_next call_args
3531 {
3532 NODE *args = 0;
3533 args = ret_args(p, $2);
3534 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3535 /*% ripper: next!($:2) %*/
3536 }
3537 ;
3538
3539mlhs : mlhs_basic
3540 | tLPAREN mlhs_inner rparen
3541 {
3542 $$ = $2;
3543 /*% ripper: mlhs_paren!($:2) %*/
3544 }
3545 ;
3546
3547mlhs_inner : mlhs_basic
3548 | tLPAREN mlhs_inner rparen
3549 {
3550 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3551 /*% ripper: mlhs_paren!($:2) %*/
3552 }
3553 ;
3554
3555mlhs_basic : mlhs_head
3556 {
3557 $$ = NEW_MASGN($1, 0, &@$);
3558 /*% ripper: $:1 %*/
3559 }
3560 | mlhs_head mlhs_item
3561 {
3562 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3563 /*% ripper: mlhs_add!($:1, $:2) %*/
3564 }
3565 | mlhs_head tSTAR mlhs_node
3566 {
3567 $$ = NEW_MASGN($1, $3, &@$);
3568 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3569 }
3570 | mlhs_head tSTAR mlhs_node ',' mlhs_post
3571 {
3572 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3573 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3574 }
3575 | mlhs_head tSTAR
3576 {
3577 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3578 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3579 }
3580 | mlhs_head tSTAR ',' mlhs_post
3581 {
3582 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3583 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3584 }
3585 | tSTAR mlhs_node
3586 {
3587 $$ = NEW_MASGN(0, $2, &@$);
3588 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3589 }
3590 | tSTAR mlhs_node ',' mlhs_post
3591 {
3592 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3593 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3594 }
3595 | tSTAR
3596 {
3597 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3598 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3599 }
3600 | tSTAR ',' mlhs_post
3601 {
3602 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3603 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3604 }
3605 ;
3606
3607mlhs_item : mlhs_node
3608 | tLPAREN mlhs_inner rparen
3609 {
3610 $$ = (NODE *)$2;
3611 /*% ripper: mlhs_paren!($:2) %*/
3612 }
3613 ;
3614
3615mlhs_head : mlhs_item ','
3616 {
3617 $$ = NEW_LIST($1, &@1);
3618 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3619 }
3620 | mlhs_head mlhs_item ','
3621 {
3622 $$ = list_append(p, $1, $2);
3623 /*% ripper: mlhs_add!($:1, $:2) %*/
3624 }
3625 ;
3626
3627mlhs_post : mlhs_item
3628 {
3629 $$ = NEW_LIST($1, &@$);
3630 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3631 }
3632 | mlhs_post ',' mlhs_item
3633 {
3634 $$ = list_append(p, $1, $3);
3635 /*% ripper: mlhs_add!($:1, $:3) %*/
3636 }
3637 ;
3638
3639mlhs_node : user_or_keyword_variable
3640 {
3641 /*% ripper: var_field!($:1) %*/
3642 $$ = assignable(p, $1, 0, &@$);
3643 }
3644 | primary_value '[' opt_call_args rbracket
3645 {
3646 $$ = aryset(p, $1, $3, &@$);
3647 /*% ripper: aref_field!($:1, $:3) %*/
3648 }
3649 | primary_value call_op tIDENTIFIER
3650 {
3651 anddot_multiple_assignment_check(p, &@2, $2);
3652 $$ = attrset(p, $1, $2, $3, &@$);
3653 /*% ripper: field!($:1, $:2, $:3) %*/
3654 }
3655 | primary_value tCOLON2 tIDENTIFIER
3656 {
3657 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3658 /*% ripper: const_path_field!($:1, $:3) %*/
3659 }
3660 | primary_value call_op tCONSTANT
3661 {
3662 anddot_multiple_assignment_check(p, &@2, $2);
3663 $$ = attrset(p, $1, $2, $3, &@$);
3664 /*% ripper: field!($:1, $:2, $:3) %*/
3665 }
3666 | primary_value tCOLON2 tCONSTANT
3667 {
3668 /*% ripper: const_path_field!($:1, $:3) %*/
3669 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3670 }
3671 | tCOLON3 tCONSTANT
3672 {
3673 /*% ripper: top_const_field!($:2) %*/
3674 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3675 }
3676 | backref
3677 {
3678 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3679 $$ = NEW_ERROR(&@$);
3680 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3681 }
3682 ;
3683
3684lhs : user_or_keyword_variable
3685 {
3686 /*% ripper: var_field!($:1) %*/
3687 $$ = assignable(p, $1, 0, &@$);
3688 }
3689 | primary_value '[' opt_call_args rbracket
3690 {
3691 $$ = aryset(p, $1, $3, &@$);
3692 /*% ripper: aref_field!($:1, $:3) %*/
3693 }
3694 | primary_value call_op tIDENTIFIER
3695 {
3696 $$ = attrset(p, $1, $2, $3, &@$);
3697 /*% ripper: field!($:1, $:2, $:3) %*/
3698 }
3699 | primary_value tCOLON2 tIDENTIFIER
3700 {
3701 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3702 /*% ripper: field!($:1, $:2, $:3) %*/
3703 }
3704 | primary_value call_op tCONSTANT
3705 {
3706 $$ = attrset(p, $1, $2, $3, &@$);
3707 /*% ripper: field!($:1, $:2, $:3) %*/
3708 }
3709 | primary_value tCOLON2 tCONSTANT
3710 {
3711 /*% ripper: const_path_field!($:1, $:3) %*/
3712 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3713 }
3714 | tCOLON3 tCONSTANT
3715 {
3716 /*% ripper: top_const_field!($:2) %*/
3717 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3718 }
3719 | backref
3720 {
3721 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3722 $$ = NEW_ERROR(&@$);
3723 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3724 }
3725 ;
3726
3727cname : tIDENTIFIER
3728 {
3729 static const char mesg[] = "class/module name must be CONSTANT";
3730 /*%%%*/
3731 yyerror1(&@1, mesg);
3732 /*% %*/
3733 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3734 }
3735 | tCONSTANT
3736 ;
3737
3738cpath : tCOLON3 cname
3739 {
3740 $$ = NEW_COLON3($2, &@$);
3741 /*% ripper: top_const_ref!($:2) %*/
3742 }
3743 | cname
3744 {
3745 $$ = NEW_COLON2(0, $1, &@$);
3746 /*% ripper: const_ref!($:1) %*/
3747 }
3748 | primary_value tCOLON2 cname
3749 {
3750 $$ = NEW_COLON2($1, $3, &@$);
3751 /*% ripper: const_path_ref!($:1, $:3) %*/
3752 }
3753 ;
3754
3755fname : ident_or_const
3756 | tFID
3757 | op
3758 {
3759 SET_LEX_STATE(EXPR_ENDFN);
3760 $$ = $1;
3761 }
3762 | reswords
3763 ;
3764
3765fitem : fname
3766 {
3767 $$ = NEW_SYM(rb_id2str($1), &@$);
3768 /*% ripper: symbol_literal!($:1) %*/
3769 }
3770 | symbol
3771 ;
3772
3773undef_list : fitem
3774 {
3775 $$ = NEW_UNDEF($1, &@$);
3776 /*% ripper: rb_ary_new3(1, $:1) %*/
3777 }
3778 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3779 {
3780 nd_set_last_loc($1, @4.end_pos);
3781 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3782 /*% ripper: rb_ary_push($:1, $:4) %*/
3783 }
3784 ;
3785
3786op : '|' { $$ = '|'; }
3787 | '^' { $$ = '^'; }
3788 | '&' { $$ = '&'; }
3789 | tCMP { $$ = tCMP; }
3790 | tEQ { $$ = tEQ; }
3791 | tEQQ { $$ = tEQQ; }
3792 | tMATCH { $$ = tMATCH; }
3793 | tNMATCH { $$ = tNMATCH; }
3794 | '>' { $$ = '>'; }
3795 | tGEQ { $$ = tGEQ; }
3796 | '<' { $$ = '<'; }
3797 | tLEQ { $$ = tLEQ; }
3798 | tNEQ { $$ = tNEQ; }
3799 | tLSHFT { $$ = tLSHFT; }
3800 | tRSHFT { $$ = tRSHFT; }
3801 | '+' { $$ = '+'; }
3802 | '-' { $$ = '-'; }
3803 | '*' { $$ = '*'; }
3804 | tSTAR { $$ = '*'; }
3805 | '/' { $$ = '/'; }
3806 | '%' { $$ = '%'; }
3807 | tPOW { $$ = tPOW; }
3808 | tDSTAR { $$ = tDSTAR; }
3809 | '!' { $$ = '!'; }
3810 | '~' { $$ = '~'; }
3811 | tUPLUS { $$ = tUPLUS; }
3812 | tUMINUS { $$ = tUMINUS; }
3813 | tAREF { $$ = tAREF; }
3814 | tASET { $$ = tASET; }
3815 | '`' { $$ = '`'; }
3816 ;
3817
3818reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3819 | keyword_BEGIN | keyword_END
3820 | keyword_alias | keyword_and | keyword_begin
3821 | keyword_break | keyword_case | keyword_class | keyword_def
3822 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3823 | keyword_end | keyword_ensure | keyword_false
3824 | keyword_for | keyword_in | keyword_module | keyword_next
3825 | keyword_nil | keyword_not | keyword_or | keyword_redo
3826 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3827 | keyword_super | keyword_then | keyword_true | keyword_undef
3828 | keyword_when | keyword_yield | keyword_if | keyword_unless
3829 | keyword_while | keyword_until
3830 ;
3831
3832arg : lhs '=' lex_ctxt arg_rhs
3833 {
3834 $$ = node_assign(p, $1, $4, $3, &@$);
3835 /*% ripper: assign!($:1, $:4) %*/
3836 }
3837 | var_lhs tOP_ASGN lex_ctxt arg_rhs
3838 {
3839 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
3840 /*% ripper: opassign!($:1, $:2, $:4) %*/
3841 }
3842 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
3843 {
3844 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$, &NULL_LOC, &@2, &@4, &@5);
3845 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3846 }
3847 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3848 {
3849 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$, &@2, &@3, &@4);
3850 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3851 }
3852 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3853 {
3854 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$, &@2, &@3, &@4);
3855 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3856 }
3857 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3858 {
3859 $$ = new_attr_op_assign(p, $1, idCOLON2, $3, $4, $6, &@$, &@2, &@3, &@4);
3860 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3861 }
3862 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3863 {
3864 YYLTYPE loc = code_loc_gen(&@1, &@3);
3865 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
3866 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3867 }
3868 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3869 {
3870 YYLTYPE loc = code_loc_gen(&@1, &@2);
3871 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
3872 /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
3873 }
3874 | backref tOP_ASGN lex_ctxt arg_rhs
3875 {
3876 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3877 $$ = NEW_ERROR(&@$);
3878 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3879 }
3880 | arg tDOT2 arg
3881 {
3882 value_expr($1);
3883 value_expr($3);
3884 $$ = NEW_DOT2($1, $3, &@$);
3885 /*% ripper: dot2!($:1, $:3) %*/
3886 }
3887 | arg tDOT3 arg
3888 {
3889 value_expr($1);
3890 value_expr($3);
3891 $$ = NEW_DOT3($1, $3, &@$);
3892 /*% ripper: dot3!($:1, $:3) %*/
3893 }
3894 | arg tDOT2
3895 {
3896 value_expr($1);
3897 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
3898 /*% ripper: dot2!($:1, Qnil) %*/
3899 }
3900 | arg tDOT3
3901 {
3902 value_expr($1);
3903 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
3904 /*% ripper: dot3!($:1, Qnil) %*/
3905 }
3906 | tBDOT2 arg
3907 {
3908 value_expr($2);
3909 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
3910 /*% ripper: dot2!(Qnil, $:2) %*/
3911 }
3912 | tBDOT3 arg
3913 {
3914 value_expr($2);
3915 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
3916 /*% ripper: dot3!(Qnil, $:2) %*/
3917 }
3918 | arg '+' arg
3919 {
3920 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3921 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3922 }
3923 | arg '-' arg
3924 {
3925 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3926 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3927 }
3928 | arg '*' arg
3929 {
3930 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3931 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3932 }
3933 | arg '/' arg
3934 {
3935 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3936 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3937 }
3938 | arg '%' arg
3939 {
3940 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3941 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3942 }
3943 | arg tPOW arg
3944 {
3945 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3946 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3947 }
3948 | tUMINUS_NUM simple_numeric tPOW arg
3949 {
3950 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3951 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3952 }
3953 | tUPLUS arg
3954 {
3955 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3956 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3957 }
3958 | tUMINUS arg
3959 {
3960 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3961 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3962 }
3963 | arg '|' arg
3964 {
3965 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3966 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3967 }
3968 | arg '^' arg
3969 {
3970 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3971 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3972 }
3973 | arg '&' arg
3974 {
3975 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3976 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
3977 }
3978 | arg tCMP arg
3979 {
3980 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3981 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
3982 }
3983 | rel_expr %prec tCMP
3984 | arg tEQ arg
3985 {
3986 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3987 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
3988 }
3989 | arg tEQQ arg
3990 {
3991 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
3992 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
3993 }
3994 | arg tNEQ arg
3995 {
3996 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
3997 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
3998 }
3999 | arg tMATCH arg
4000 {
4001 $$ = match_op(p, $1, $3, &@2, &@$);
4002 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4003 }
4004 | arg tNMATCH arg
4005 {
4006 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4007 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4008 }
4009 | '!' arg
4010 {
4011 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4012 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4013 }
4014 | '~' arg
4015 {
4016 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4017 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4018 }
4019 | arg tLSHFT arg
4020 {
4021 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4022 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4023 }
4024 | arg tRSHFT arg
4025 {
4026 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4027 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4028 }
4029 | arg tANDOP arg
4030 {
4031 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4032 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4033 }
4034 | arg tOROP arg
4035 {
4036 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4037 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4038 }
4039 | keyword_defined '\n'? begin_defined arg
4040 {
4041 p->ctxt.in_defined = $3.in_defined;
4042 $$ = new_defined(p, $4, &@$);
4043 /*% ripper: defined!($:4) %*/
4044 }
4045 | arg '?' arg '\n'? ':' arg
4046 {
4047 value_expr($1);
4048 $$ = new_if(p, $1, $3, $6, &@$);
4049 fixpos($$, $1);
4050 /*% ripper: ifop!($:1, $:3, $:6) %*/
4051 }
4052 | defn_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
4053 {
4054 endless_method_name(p, $head->nd_mid, &@head);
4055 restore_defun(p, $head);
4056 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4057 ($$ = $head->nd_def)->nd_loc = @$;
4058 RNODE_DEFN($$)->nd_defn = $bodystmt;
4059 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
4060 /*% ripper: def!($:head, $:args, $:$) %*/
4061 local_pop(p);
4062 }
4063 | defs_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
4064 {
4065 endless_method_name(p, $head->nd_mid, &@head);
4066 restore_defun(p, $head);
4067 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4068 ($$ = $head->nd_def)->nd_loc = @$;
4069 RNODE_DEFS($$)->nd_defn = $bodystmt;
4070 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
4071 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
4072 local_pop(p);
4073 }
4074 | primary
4075 {
4076 $$ = $1;
4077 }
4078 ;
4079
4080endless_arg : arg %prec modifier_rescue
4081 | endless_arg modifier_rescue after_rescue arg
4082 {
4083 p->ctxt.in_rescue = $3.in_rescue;
4084 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4085 /*% ripper: rescue_mod!($:1, $:4) %*/
4086 }
4087 | keyword_not '\n'? endless_arg
4088 {
4089 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4090 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4091 }
4092 ;
4093
4094relop : '>' {$$ = '>';}
4095 | '<' {$$ = '<';}
4096 | tGEQ {$$ = idGE;}
4097 | tLEQ {$$ = idLE;}
4098 ;
4099
4100rel_expr : arg relop arg %prec '>'
4101 {
4102 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4103 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4104 }
4105 | rel_expr relop arg %prec '>'
4106 {
4107 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4108 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4109 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4110 }
4111 ;
4112
4113lex_ctxt : none
4114 {
4115 $$ = p->ctxt;
4116 }
4117 ;
4118
4119begin_defined : lex_ctxt
4120 {
4121 p->ctxt.in_defined = 1;
4122 $$ = $1;
4123 }
4124 ;
4125
4126after_rescue : lex_ctxt
4127 {
4128 p->ctxt.in_rescue = after_rescue;
4129 $$ = $1;
4130 }
4131 ;
4132
4133arg_value : arg
4134 {
4135 value_expr($1);
4136 $$ = $1;
4137 }
4138 ;
4139
4140aref_args : none
4141 | args trailer
4142 {
4143 $$ = $1;
4144 }
4145 | args ',' assocs trailer
4146 {
4147 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4148 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4149 }
4150 | assocs trailer
4151 {
4152 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4153 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4154 }
4155 ;
4156
4157arg_rhs : arg %prec tOP_ASGN
4158 {
4159 value_expr($1);
4160 $$ = $1;
4161 }
4162 | arg modifier_rescue after_rescue arg
4163 {
4164 p->ctxt.in_rescue = $3.in_rescue;
4165 value_expr($1);
4166 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4167 /*% ripper: rescue_mod!($:1, $:4) %*/
4168 }
4169 ;
4170
4171paren_args : '(' opt_call_args rparen
4172 {
4173 $$ = $2;
4174 /*% ripper: arg_paren!($:2) %*/
4175 }
4176 | '(' args ',' args_forward rparen
4177 {
4178 if (!check_forwarding_args(p)) {
4179 $$ = 0;
4180 }
4181 else {
4182 $$ = new_args_forward_call(p, $2, &@4, &@$);
4183 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4184 }
4185 }
4186 | '(' args_forward rparen
4187 {
4188 if (!check_forwarding_args(p)) {
4189 $$ = 0;
4190 }
4191 else {
4192 $$ = new_args_forward_call(p, 0, &@2, &@$);
4193 /*% ripper: arg_paren!($:2) %*/
4194 }
4195 }
4196 ;
4197
4198opt_paren_args : none
4199 | paren_args
4200 {
4201 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4202 }
4203 ;
4204
4205opt_call_args : none
4206 | call_args
4207 | args ','
4208 {
4209 $$ = $1;
4210 }
4211 | args ',' assocs ','
4212 {
4213 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4214 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4215 }
4216 | assocs ','
4217 {
4218 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4219 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4220 }
4221 ;
4222
4223call_args : command
4224 {
4225 value_expr($1);
4226 $$ = NEW_LIST($1, &@$);
4227 /*% ripper: args_add!(args_new!, $:1) %*/
4228 }
4229 | args opt_block_arg
4230 {
4231 $$ = arg_blk_pass($1, $2);
4232 /*% ripper: args_add_block!($:1, $:2) %*/
4233 }
4234 | assocs opt_block_arg
4235 {
4236 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4237 $$ = arg_blk_pass($$, $2);
4238 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4239 }
4240 | args ',' assocs opt_block_arg
4241 {
4242 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4243 $$ = arg_blk_pass($$, $4);
4244 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4245 }
4246 | block_arg
4247 /*% ripper: args_add_block!(args_new!, $:1) %*/
4248 ;
4249
4250command_args : {
4251 /* If call_args starts with a open paren '(' or '[',
4252 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4253 * but the push must be done after CMDARG_PUSH(1).
4254 * So this code makes them consistent by first cancelling
4255 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4256 * and finally redoing CMDARG_PUSH(0).
4257 */
4258 int lookahead = 0;
4259 switch (yychar) {
4260 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4261 lookahead = 1;
4262 }
4263 if (lookahead) CMDARG_POP();
4264 CMDARG_PUSH(1);
4265 if (lookahead) CMDARG_PUSH(0);
4266 }
4267 call_args
4268 {
4269 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4270 * but the push must be done after CMDARG_POP() in the parser.
4271 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4272 * CMDARG_POP() to pop 1 pushed by command_args,
4273 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4274 */
4275 int lookahead = 0;
4276 switch (yychar) {
4277 case tLBRACE_ARG:
4278 lookahead = 1;
4279 }
4280 if (lookahead) CMDARG_POP();
4281 CMDARG_POP();
4282 if (lookahead) CMDARG_PUSH(0);
4283 $$ = $2;
4284 /*% ripper: $:2 %*/
4285 }
4286 ;
4287
4288block_arg : tAMPER arg_value
4289 {
4290 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4291 /*% ripper: $:2 %*/
4292 }
4293 | tAMPER
4294 {
4295 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4296 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4297 /*% ripper: Qnil %*/
4298 }
4299 ;
4300
4301opt_block_arg : ',' block_arg
4302 {
4303 $$ = $2;
4304 /*% ripper: $:2 %*/
4305 }
4306 | none
4307 {
4308 $$ = 0;
4309 /*% ripper: Qfalse %*/
4310 }
4311 ;
4312
4313/* value */
4314args : arg_value
4315 {
4316 $$ = NEW_LIST($arg_value, &@$);
4317 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4318 }
4319 | arg_splat
4320 {
4321 $$ = $arg_splat;
4322 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4323 }
4324 | args[non_last_args] ',' arg_value
4325 {
4326 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4327 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4328 }
4329 | args[non_last_args] ',' arg_splat
4330 {
4331 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4332 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4333 }
4334 ;
4335
4336/* value */
4337arg_splat : tSTAR arg_value
4338 {
4339 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4340 /*% ripper: $:arg_value %*/
4341 }
4342 | tSTAR /* none */
4343 {
4344 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4345 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4346 /*% ripper: Qnil %*/
4347 }
4348 ;
4349
4350/* value */
4351mrhs_arg : mrhs
4352 | arg_value
4353 ;
4354
4355/* value */
4356mrhs : args ',' arg_value
4357 {
4358 $$ = last_arg_append(p, $args, $arg_value, &@$);
4359 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4360 }
4361 | args ',' tSTAR arg_value
4362 {
4363 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4364 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4365 }
4366 | tSTAR arg_value
4367 {
4368 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4369 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4370 }
4371 ;
4372
4373%rule %inline inline_primary
4374 : literal
4375 | strings
4376 | xstring
4377 | regexp
4378 | words
4379 | qwords
4380 | symbols
4381 | qsymbols
4382 ;
4383
4384primary : inline_primary
4385 | var_ref
4386 | backref
4387 | tFID
4388 {
4389 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
4390 /*% ripper: method_add_arg!(fcall!($:1), args_new!) %*/
4391 }
4392 | k_begin
4393 {
4394 CMDARG_PUSH(0);
4395 }
4396 bodystmt
4397 k_end
4398 {
4399 CMDARG_POP();
4400 set_line_body($3, @1.end_pos.lineno);
4401 $$ = NEW_BEGIN($3, &@$);
4402 nd_set_line($$, @1.end_pos.lineno);
4403 /*% ripper: begin!($:3) %*/
4404 }
4405 | tLPAREN_ARG compstmt {SET_LEX_STATE(EXPR_ENDARG);} ')'
4406 {
4407 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4408 $$ = $2;
4409 /*% ripper: paren!($:2) %*/
4410 }
4411 | tLPAREN compstmt ')'
4412 {
4413 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4414 $$ = NEW_BLOCK($2, &@$);
4415 /*% ripper: paren!($:2) %*/
4416 }
4417 | primary_value tCOLON2 tCONSTANT
4418 {
4419 $$ = NEW_COLON2($1, $3, &@$);
4420 /*% ripper: const_path_ref!($:1, $:3) %*/
4421 }
4422 | tCOLON3 tCONSTANT
4423 {
4424 $$ = NEW_COLON3($2, &@$);
4425 /*% ripper: top_const_ref!($:2) %*/
4426 }
4427 | tLBRACK aref_args ']'
4428 {
4429 $$ = make_list($2, &@$);
4430 /*% ripper: array!($:2) %*/
4431 }
4432 | tLBRACE assoc_list '}'
4433 {
4434 $$ = new_hash(p, $2, &@$);
4435 RNODE_HASH($$)->nd_brace = TRUE;
4436 /*% ripper: hash!($:2) %*/
4437 }
4438 | k_return
4439 {
4440 $$ = NEW_RETURN(0, &@$, &@1);
4441 /*% ripper: return0! %*/
4442 }
4443 | k_yield '(' call_args rparen
4444 {
4445 $$ = new_yield(p, $3, &@$);
4446 /*% ripper: yield!(paren!($:3)) %*/
4447 }
4448 | k_yield '(' rparen
4449 {
4450 $$ = NEW_YIELD(0, &@$);
4451 /*% ripper: yield!(paren!(args_new!)) %*/
4452 }
4453 | k_yield
4454 {
4455 $$ = NEW_YIELD(0, &@$);
4456 /*% ripper: yield0! %*/
4457 }
4458 | keyword_defined '\n'? '(' begin_defined expr rparen
4459 {
4460 p->ctxt.in_defined = $4.in_defined;
4461 $$ = new_defined(p, $5, &@$);
4462 /*% ripper: defined!($:5) %*/
4463 }
4464 | keyword_not '(' expr rparen
4465 {
4466 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4467 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4468 }
4469 | keyword_not '(' rparen
4470 {
4471 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
4472 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4473 }
4474 | fcall brace_block
4475 {
4476 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
4477 /*% ripper: method_add_block!(method_add_arg!(fcall!($:1), args_new!), $:2) %*/
4478 }
4479 | method_call
4480 | method_call brace_block
4481 {
4482 block_dup_check(p, get_nd_args(p, $1), $2);
4483 $$ = method_add_block(p, $1, $2, &@$);
4484 /*% ripper: method_add_block!($:1, $:2) %*/
4485 }
4486 | lambda
4487 | k_if expr_value then
4488 compstmt
4489 if_tail
4490 k_end
4491 {
4492 $$ = new_if(p, $2, $4, $5, &@$);
4493 fixpos($$, $2);
4494 /*% ripper: if!($:2, $:4, $:5) %*/
4495 }
4496 | k_unless expr_value then
4497 compstmt
4498 opt_else
4499 k_end
4500 {
4501 $$ = new_unless(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4502 fixpos($$, $2);
4503 /*% ripper: unless!($:2, $:4, $:5) %*/
4504 }
4505 | k_while expr_value_do
4506 compstmt
4507 k_end
4508 {
4509 restore_block_exit(p, $1);
4510 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4511 fixpos($$, $2);
4512 /*% ripper: while!($:2, $:3) %*/
4513 }
4514 | k_until expr_value_do
4515 compstmt
4516 k_end
4517 {
4518 restore_block_exit(p, $1);
4519 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4520 fixpos($$, $2);
4521 /*% ripper: until!($:2, $:3) %*/
4522 }
4523 | k_case expr_value terms?
4524 {
4525 $$ = p->case_labels;
4526 p->case_labels = CHECK_LITERAL_WHEN;
4527 }<labels>
4528 case_body
4529 k_end
4530 {
4531 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4532 p->case_labels = $4;
4533 $$ = NEW_CASE($2, $5, &@$, &@1, &@6);
4534 fixpos($$, $2);
4535 /*% ripper: case!($:2, $:5) %*/
4536 }
4537 | k_case terms?
4538 {
4539 $$ = p->case_labels;
4540 p->case_labels = 0;
4541 }<labels>
4542 case_body
4543 k_end
4544 {
4545 if (p->case_labels) st_free_table(p->case_labels);
4546 p->case_labels = $3;
4547 $$ = NEW_CASE2($4, &@$, &@1, &@5);
4548 /*% ripper: case!(Qnil, $:4) %*/
4549 }
4550 | k_case expr_value terms?
4551 p_case_body
4552 k_end
4553 {
4554 $$ = NEW_CASE3($2, $4, &@$, &@1, &@5);
4555 /*% ripper: case!($:2, $:4) %*/
4556 }
4557 | k_for for_var keyword_in expr_value_do
4558 compstmt
4559 k_end
4560 {
4561 restore_block_exit(p, $1);
4562 /*
4563 * for a, b, c in e
4564 * #=>
4565 * e.each{|*x| a, b, c = x}
4566 *
4567 * for a in e
4568 * #=>
4569 * e.each{|x| a, = x}
4570 */
4571 ID id = internal_id(p);
4572 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4573 rb_node_args_t *args;
4574 NODE *scope, *internal_var = NEW_DVAR(id, &@2);
4575 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4576 tbl->ids[0] = id; /* internal id */
4577
4578 switch (nd_type($2)) {
4579 case NODE_LASGN:
4580 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4581 set_nd_value(p, $2, internal_var);
4582 id = 0;
4583 m->nd_plen = 1;
4584 m->nd_next = $2;
4585 break;
4586 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4587 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
4588 break;
4589 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4590 m->nd_next = node_assign(p, (NODE *)NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
4591 }
4592 /* {|*internal_id| <m> = internal_id; ... } */
4593 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
4594 scope = NEW_SCOPE2(tbl, args, $5, &@$);
4595 $$ = NEW_FOR($4, scope, &@$);
4596 fixpos($$, $2);
4597 /*% ripper: for!($:2, $:4, $:5) %*/
4598 }
4599 | k_class cpath superclass
4600 {
4601 begin_definition("class", &@k_class, &@cpath);
4602 }
4603 bodystmt
4604 k_end
4605 {
4606 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$);
4607 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4608 set_line_body($bodystmt, @superclass.end_pos.lineno);
4609 nd_set_line($$, @superclass.end_pos.lineno);
4610 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4611 local_pop(p);
4612 p->ctxt.in_class = $k_class.in_class;
4613 p->ctxt.cant_return = $k_class.cant_return;
4614 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4615 }
4616 | k_class tLSHFT expr_value
4617 {
4618 begin_definition("", &@k_class, &@tLSHFT);
4619 }
4620 term
4621 bodystmt
4622 k_end
4623 {
4624 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$);
4625 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4626 set_line_body($bodystmt, nd_line($expr_value));
4627 fixpos($$, $expr_value);
4628 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4629 local_pop(p);
4630 p->ctxt.in_def = $k_class.in_def;
4631 p->ctxt.in_class = $k_class.in_class;
4632 p->ctxt.cant_return = $k_class.cant_return;
4633 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4634 }
4635 | k_module cpath
4636 {
4637 begin_definition("module", &@k_module, &@cpath);
4638 }
4639 bodystmt
4640 k_end
4641 {
4642 $$ = NEW_MODULE($cpath, $bodystmt, &@$);
4643 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4644 set_line_body($bodystmt, @cpath.end_pos.lineno);
4645 nd_set_line($$, @cpath.end_pos.lineno);
4646 /*% ripper: module!($:cpath, $:bodystmt) %*/
4647 local_pop(p);
4648 p->ctxt.in_class = $k_module.in_class;
4649 p->ctxt.cant_return = $k_module.cant_return;
4650 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4651 }
4652 | defn_head[head]
4653 f_arglist[args]
4654 {
4655 push_end_expect_token_locations(p, &@head.beg_pos);
4656 }
4657 bodystmt
4658 k_end
4659 {
4660 restore_defun(p, $head);
4661 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4662 ($$ = $head->nd_def)->nd_loc = @$;
4663 RNODE_DEFN($$)->nd_defn = $bodystmt;
4664 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4665 local_pop(p);
4666 }
4667 | defs_head[head]
4668 f_arglist[args]
4669 {
4670 push_end_expect_token_locations(p, &@head.beg_pos);
4671 }
4672 bodystmt
4673 k_end
4674 {
4675 restore_defun(p, $head);
4676 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4677 ($$ = $head->nd_def)->nd_loc = @$;
4678 RNODE_DEFS($$)->nd_defn = $bodystmt;
4679 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4680 local_pop(p);
4681 }
4682 | keyword_break
4683 {
4684 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@1));
4685 /*% ripper: break!(args_new!) %*/
4686 }
4687 | keyword_next
4688 {
4689 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@1));
4690 /*% ripper: next!(args_new!) %*/
4691 }
4692 | keyword_redo
4693 {
4694 $$ = add_block_exit(p, NEW_REDO(&@$, &@1));
4695 /*% ripper: redo! %*/
4696 }
4697 | keyword_retry
4698 {
4699 if (!p->ctxt.in_defined) {
4700 switch (p->ctxt.in_rescue) {
4701 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4702 case after_rescue: /* ok */ break;
4703 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4704 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4705 }
4706 }
4707 $$ = NEW_RETRY(&@$);
4708 /*% ripper: retry! %*/
4709 }
4710 ;
4711
4712primary_value : primary
4713 {
4714 value_expr($1);
4715 $$ = $1;
4716 }
4717 ;
4718
4719k_begin : keyword_begin
4720 {
4721 token_info_push(p, "begin", &@$);
4722 push_end_expect_token_locations(p, &@1.beg_pos);
4723 }
4724 ;
4725
4726k_if : keyword_if
4727 {
4728 WARN_EOL("if");
4729 token_info_push(p, "if", &@$);
4730 if (p->token_info && p->token_info->nonspc &&
4731 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4732 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4733 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4734 beg += rb_strlen_lit("else");
4735 while (beg < tok && ISSPACE(*beg)) beg++;
4736 if (beg == tok) {
4737 p->token_info->nonspc = 0;
4738 }
4739 }
4740 push_end_expect_token_locations(p, &@1.beg_pos);
4741 }
4742 ;
4743
4744k_unless : keyword_unless
4745 {
4746 token_info_push(p, "unless", &@$);
4747 push_end_expect_token_locations(p, &@1.beg_pos);
4748 }
4749 ;
4750
4751k_while : keyword_while allow_exits
4752 {
4753 $$ = $allow_exits;
4754 token_info_push(p, "while", &@$);
4755 push_end_expect_token_locations(p, &@1.beg_pos);
4756 }
4757 ;
4758
4759k_until : keyword_until allow_exits
4760 {
4761 $$ = $allow_exits;
4762 token_info_push(p, "until", &@$);
4763 push_end_expect_token_locations(p, &@1.beg_pos);
4764 }
4765 ;
4766
4767k_case : keyword_case
4768 {
4769 token_info_push(p, "case", &@$);
4770 push_end_expect_token_locations(p, &@1.beg_pos);
4771 }
4772 ;
4773
4774k_for : keyword_for allow_exits
4775 {
4776 $$ = $allow_exits;
4777 token_info_push(p, "for", &@$);
4778 push_end_expect_token_locations(p, &@1.beg_pos);
4779 }
4780 ;
4781
4782k_class : keyword_class
4783 {
4784 token_info_push(p, "class", &@$);
4785 $$ = p->ctxt;
4786 p->ctxt.in_rescue = before_rescue;
4787 push_end_expect_token_locations(p, &@1.beg_pos);
4788 }
4789 ;
4790
4791k_module : keyword_module
4792 {
4793 token_info_push(p, "module", &@$);
4794 $$ = p->ctxt;
4795 p->ctxt.in_rescue = before_rescue;
4796 push_end_expect_token_locations(p, &@1.beg_pos);
4797 }
4798 ;
4799
4800k_def : keyword_def
4801 {
4802 token_info_push(p, "def", &@$);
4803 $$ = NEW_DEF_TEMP(&@$);
4804 p->ctxt.in_argdef = 1;
4805 }
4806 ;
4807
4808k_do : keyword_do
4809 {
4810 token_info_push(p, "do", &@$);
4811 push_end_expect_token_locations(p, &@1.beg_pos);
4812 }
4813 ;
4814
4815k_do_block : keyword_do_block
4816 {
4817 token_info_push(p, "do", &@$);
4818 push_end_expect_token_locations(p, &@1.beg_pos);
4819 }
4820 ;
4821
4822k_rescue : keyword_rescue
4823 {
4824 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4825 $$ = p->ctxt;
4826 p->ctxt.in_rescue = after_rescue;
4827 }
4828 ;
4829
4830k_ensure : keyword_ensure
4831 {
4832 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4833 $$ = p->ctxt;
4834 }
4835 ;
4836
4837k_when : keyword_when
4838 {
4839 token_info_warn(p, "when", p->token_info, 0, &@$);
4840 }
4841 ;
4842
4843k_else : keyword_else
4844 {
4845 token_info *ptinfo_beg = p->token_info;
4846 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4847 token_info_warn(p, "else", p->token_info, same, &@$);
4848 if (same) {
4849 token_info e;
4850 e.next = ptinfo_beg->next;
4851 e.token = "else";
4852 token_info_setup(&e, p->lex.pbeg, &@$);
4853 if (!e.nonspc) *ptinfo_beg = e;
4854 }
4855 }
4856 ;
4857
4858k_elsif : keyword_elsif
4859 {
4860 WARN_EOL("elsif");
4861 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4862 }
4863 ;
4864
4865k_end : keyword_end
4866 {
4867 token_info_pop(p, "end", &@$);
4868 pop_end_expect_token_locations(p);
4869 }
4870 | tDUMNY_END
4871 {
4872 compile_error(p, "syntax error, unexpected end-of-input");
4873 }
4874 ;
4875
4876k_return : keyword_return
4877 {
4878 if (p->ctxt.cant_return && !dyna_in_block(p))
4879 yyerror1(&@1, "Invalid return in class/module body");
4880 }
4881 ;
4882
4883k_yield : keyword_yield
4884 {
4885 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4886 yyerror1(&@1, "Invalid yield");
4887 }
4888 ;
4889
4890then : term
4891 | keyword_then
4892 | term keyword_then
4893 ;
4894
4895do : term
4896 | keyword_do_cond
4897 ;
4898
4899if_tail : opt_else
4900 | k_elsif expr_value then
4901 compstmt
4902 if_tail
4903 {
4904 $$ = new_if(p, $2, $4, $5, &@$);
4905 fixpos($$, $2);
4906 /*% ripper: elsif!($:2, $:4, $:5) %*/
4907 }
4908 ;
4909
4910opt_else : none
4911 | k_else compstmt
4912 {
4913 $$ = $2;
4914 /*% ripper: else!($:2) %*/
4915 }
4916 ;
4917
4918for_var : lhs
4919 | mlhs
4920 ;
4921
4922f_marg : f_norm_arg
4923 {
4924 $$ = assignable(p, $1, 0, &@$);
4925 mark_lvar_used(p, $$);
4926 }
4927 | tLPAREN f_margs rparen
4928 {
4929 $$ = (NODE *)$2;
4930 /*% ripper: mlhs_paren!($:2) %*/
4931 }
4932 ;
4933
4934f_marg_list : f_marg
4935 {
4936 $$ = NEW_LIST($1, &@$);
4937 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
4938 }
4939 | f_marg_list ',' f_marg
4940 {
4941 $$ = list_append(p, $1, $3);
4942 /*% ripper: mlhs_add!($:1, $:3) %*/
4943 }
4944 ;
4945
4946f_margs : f_marg_list
4947 {
4948 $$ = NEW_MASGN($1, 0, &@$);
4949 /*% ripper: $:1 %*/
4950 }
4951 | f_marg_list ',' f_rest_marg
4952 {
4953 $$ = NEW_MASGN($1, $3, &@$);
4954 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4955 }
4956 | f_marg_list ',' f_rest_marg ',' f_marg_list
4957 {
4958 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4959 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4960 }
4961 | f_rest_marg
4962 {
4963 $$ = NEW_MASGN(0, $1, &@$);
4964 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4965 }
4966 | f_rest_marg ',' f_marg_list
4967 {
4968 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4969 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4970 }
4971 ;
4972
4973f_rest_marg : tSTAR f_norm_arg
4974 {
4975 /*% ripper: $:2 %*/
4976 $$ = assignable(p, $2, 0, &@$);
4977 mark_lvar_used(p, $$);
4978 }
4979 | tSTAR
4980 {
4981 $$ = NODE_SPECIAL_NO_NAME_REST;
4982 /*% ripper: Qnil %*/
4983 }
4984 ;
4985
4986f_any_kwrest : f_kwrest
4987 | f_no_kwarg
4988 {
4989 $$ = idNil;
4990 /*% ripper: ID2VAL(idNil) %*/
4991 }
4992 ;
4993
4994f_eq : {p->ctxt.in_argdef = 0;} '=';
4995
4996block_args_tail : f_kwarg(f_block_kw) ',' f_kwrest opt_f_block_arg
4997 {
4998 $$ = new_args_tail(p, $1, $3, $4, &@3);
4999 /*% ripper: [$:1, $:3, $:4] %*/
5000 }
5001 | f_kwarg(f_block_kw) opt_f_block_arg
5002 {
5003 $$ = new_args_tail(p, $1, 0, $2, &@1);
5004 /*% ripper: [$:1, Qnil, $:2] %*/
5005 }
5006 | f_any_kwrest opt_f_block_arg
5007 {
5008 $$ = new_args_tail(p, 0, $1, $2, &@1);
5009 /*% ripper: [Qnil, $:1, $:2] %*/
5010 }
5011 | f_block_arg
5012 {
5013 $$ = new_args_tail(p, 0, 0, $1, &@1);
5014 /*% ripper: [Qnil, Qnil, $:1] %*/
5015 }
5016 ;
5017
5018excessed_comma : ','
5019 {
5020 /* magic number for rest_id in iseq_set_arguments() */
5021 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
5022 /*% ripper: excessed_comma! %*/
5023 }
5024 ;
5025
5026block_param : f_arg ',' f_optarg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5027 {
5028 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
5029 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
5030 }
5031 | f_arg ',' f_optarg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5032 {
5033 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
5034 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
5035 }
5036 | f_arg ',' f_optarg(primary_value) opt_args_tail(block_args_tail)
5037 {
5038 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
5039 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
5040 }
5041 | f_arg ',' f_optarg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5042 {
5043 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
5044 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
5045 }
5046 | f_arg ',' f_rest_arg opt_args_tail(block_args_tail)
5047 {
5048 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
5049 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
5050 }
5051 | f_arg excessed_comma
5052 {
5053 $$ = new_args_tail(p, 0, 0, 0, &@2);
5054 $$ = new_args(p, $1, 0, $2, 0, $$, &@$);
5055 /*% ripper: params!($:1, Qnil, $:2, Qnil, Qnil, Qnil, Qnil) %*/
5056 }
5057 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5058 {
5059 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
5060 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
5061 }
5062 | f_arg opt_args_tail(block_args_tail)
5063 {
5064 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
5065 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
5066 }
5067 | f_optarg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5068 {
5069 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
5070 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
5071 }
5072 | f_optarg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5073 {
5074 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
5075 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
5076 }
5077 | f_optarg(primary_value) opt_args_tail(block_args_tail)
5078 {
5079 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
5080 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
5081 }
5082 | f_optarg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5083 {
5084 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
5085 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
5086 }
5087 | f_rest_arg opt_args_tail(block_args_tail)
5088 {
5089 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
5090 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
5091 }
5092 | f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5093 {
5094 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
5095 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
5096 }
5097 | block_args_tail
5098 {
5099 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
5100 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
5101 }
5102 ;
5103
5104opt_block_param : none
5105 | block_param_def
5106 {
5107 p->command_start = TRUE;
5108 }
5109 ;
5110
5111block_param_def : '|' opt_bv_decl '|'
5112 {
5113 p->max_numparam = ORDINAL_PARAM;
5114 p->ctxt.in_argdef = 0;
5115 $$ = 0;
5116 /*% ripper: block_var!(params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), $:2) %*/
5117 }
5118 | '|' block_param opt_bv_decl '|'
5119 {
5120 p->max_numparam = ORDINAL_PARAM;
5121 p->ctxt.in_argdef = 0;
5122 $$ = $2;
5123 /*% ripper: block_var!($:2, $:3) %*/
5124 }
5125 ;
5126
5127
5128opt_bv_decl : '\n'?
5129 {
5130 $$ = 0;
5131 /*% ripper: Qfalse %*/
5132 }
5133 | '\n'? ';' bv_decls '\n'?
5134 {
5135 $$ = 0;
5136 /*% ripper: $:3 %*/
5137 }
5138 ;
5139
5140bv_decls : bvar
5141 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5142 | bv_decls ',' bvar
5143 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5144 ;
5145
5146bvar : tIDENTIFIER
5147 {
5148 new_bv(p, $1);
5149 /*% ripper: $:1 %*/
5150 }
5151 | f_bad_arg
5152 {
5153 $$ = 0;
5154 }
5155 ;
5156
5157max_numparam : {
5158 $$ = p->max_numparam;
5159 p->max_numparam = 0;
5160 }
5161 ;
5162
5163numparam : {
5164 $$ = numparam_push(p);
5165 }
5166 ;
5167
5168it_id : {
5169 $$ = p->it_id;
5170 p->it_id = 0;
5171 }
5172 ;
5173
5174lambda : tLAMBDA[lpar]
5175 {
5176 token_info_push(p, "->", &@1);
5177 $$ = dyna_push(p);
5178 }[dyna]<vars>
5179 max_numparam numparam it_id allow_exits
5180 f_larglist[args]
5181 {
5182 CMDARG_PUSH(0);
5183 }
5184 lambda_body[body]
5185 {
5186 int max_numparam = p->max_numparam;
5187 ID it_id = p->it_id;
5188 p->lex.lpar_beg = $lpar;
5189 p->max_numparam = $max_numparam;
5190 p->it_id = $it_id;
5191 restore_block_exit(p, $allow_exits);
5192 CMDARG_POP();
5193 $args = args_with_numbered(p, $args, max_numparam, it_id);
5194 {
5195 YYLTYPE loc = code_loc_gen(&@args, &@body);
5196 $$ = NEW_LAMBDA($args, $body, &loc);
5197 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5198 nd_set_line($$, @args.end_pos.lineno);
5199 nd_set_first_loc($$, @1.beg_pos);
5200 }
5201 /*% ripper: lambda!($:args, $:body) %*/
5202 numparam_pop(p, $numparam);
5203 dyna_pop(p, $dyna);
5204 }
5205 ;
5206
5207f_larglist : '(' f_args opt_bv_decl ')'
5208 {
5209 p->ctxt.in_argdef = 0;
5210 $$ = $f_args;
5211 p->max_numparam = ORDINAL_PARAM;
5212 /*% ripper: paren!($:2) %*/
5213 }
5214 | f_args
5215 {
5216 p->ctxt.in_argdef = 0;
5217 if (!args_info_empty_p(&$1->nd_ainfo))
5218 p->max_numparam = ORDINAL_PARAM;
5219 $$ = $f_args;
5220 }
5221 ;
5222
5223lambda_body : tLAMBEG compstmt '}'
5224 {
5225 token_info_pop(p, "}", &@3);
5226 $$ = $2;
5227 /*% ripper: $:2 %*/
5228 }
5229 | keyword_do_LAMBDA
5230 {
5231 push_end_expect_token_locations(p, &@1.beg_pos);
5232 }
5233 bodystmt k_end
5234 {
5235 $$ = $3;
5236 /*% ripper: $:3 %*/
5237 }
5238 ;
5239
5240do_block : k_do_block do_body k_end
5241 {
5242 $$ = $2;
5243 set_embraced_location($$, &@1, &@3);
5244 /*% ripper: $:2 %*/
5245 }
5246 ;
5247
5248block_call : command do_block
5249 {
5250 if (nd_type_p($1, NODE_YIELD)) {
5251 compile_error(p, "block given to yield");
5252 }
5253 else {
5254 block_dup_check(p, get_nd_args(p, $1), $2);
5255 }
5256 $$ = method_add_block(p, $1, $2, &@$);
5257 fixpos($$, $1);
5258 /*% ripper: method_add_block!($:1, $:2) %*/
5259 }
5260 | block_call call_op2 operation2 opt_paren_args
5261 {
5262 bool has_args = $4 != 0;
5263 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5264 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5265 /*% ripper: call!($:1, $:2, $:3) %*/
5266 if (has_args) {
5267 /*% ripper: method_add_arg!($:$, $:4) %*/
5268 }
5269 }
5270 | block_call call_op2 operation2 opt_paren_args brace_block
5271 {
5272 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5273 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5274 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
5275 if ($5) {
5276 /*% ripper: method_add_block!($:$, $:5) %*/
5277 }
5278 }
5279 | block_call call_op2 operation2 command_args do_block
5280 {
5281 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5282 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5283 }
5284 ;
5285
5286method_call : fcall paren_args
5287 {
5288 $1->nd_args = $2;
5289 $$ = (NODE *)$1;
5290 nd_set_last_loc($1, @2.end_pos);
5291 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5292 }
5293 | primary_value call_op operation2 opt_paren_args
5294 {
5295 bool has_args = $4 != 0;
5296 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5297 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5298 nd_set_line($$, @3.end_pos.lineno);
5299 /*% ripper: call!($:1, $:2, $:3) %*/
5300 if (has_args) {
5301 /*% ripper: method_add_arg!($:$, $:4) %*/
5302 }
5303 }
5304 | primary_value tCOLON2 operation2 paren_args
5305 {
5306 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5307 nd_set_line($$, @3.end_pos.lineno);
5308 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5309 }
5310 | primary_value tCOLON2 operation3
5311 {
5312 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5313 /*% ripper: call!($:1, $:2, $:3) %*/
5314 }
5315 | primary_value call_op paren_args
5316 {
5317 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5318 nd_set_line($$, @2.end_pos.lineno);
5319 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5320 }
5321 | primary_value tCOLON2 paren_args
5322 {
5323 $$ = new_qcall(p, idCOLON2, $1, idCall, $3, &@2, &@$);
5324 nd_set_line($$, @2.end_pos.lineno);
5325 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5326 }
5327 | keyword_super paren_args
5328 {
5329 $$ = NEW_SUPER($2, &@$);
5330 /*% ripper: super!($:2) %*/
5331 }
5332 | keyword_super
5333 {
5334 $$ = NEW_ZSUPER(&@$);
5335 /*% ripper: zsuper! %*/
5336 }
5337 | primary_value '[' opt_call_args rbracket
5338 {
5339 $$ = NEW_CALL($1, tAREF, $3, &@$);
5340 fixpos($$, $1);
5341 /*% ripper: aref!($:1, $:3) %*/
5342 }
5343 ;
5344
5345brace_block : '{' brace_body '}'
5346 {
5347 $$ = $2;
5348 set_embraced_location($$, &@1, &@3);
5349 /*% ripper: $:2 %*/
5350 }
5351 | k_do do_body k_end
5352 {
5353 $$ = $2;
5354 set_embraced_location($$, &@1, &@3);
5355 /*% ripper: $:2 %*/
5356 }
5357 ;
5358
5359brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5360 max_numparam numparam it_id allow_exits
5361 opt_block_param[args] compstmt
5362 {
5363 int max_numparam = p->max_numparam;
5364 ID it_id = p->it_id;
5365 p->max_numparam = $max_numparam;
5366 p->it_id = $it_id;
5367 $args = args_with_numbered(p, $args, max_numparam, it_id);
5368 $$ = NEW_ITER($args, $compstmt, &@$);
5369 /*% ripper: brace_block!($:args, $:compstmt) %*/
5370 restore_block_exit(p, $allow_exits);
5371 numparam_pop(p, $numparam);
5372 dyna_pop(p, $dyna);
5373 }
5374 ;
5375
5376do_body : {
5377 $$ = dyna_push(p);
5378 CMDARG_PUSH(0);
5379 }[dyna]<vars>
5380 max_numparam numparam it_id allow_exits
5381 opt_block_param[args] bodystmt
5382 {
5383 int max_numparam = p->max_numparam;
5384 ID it_id = p->it_id;
5385 p->max_numparam = $max_numparam;
5386 p->it_id = $it_id;
5387 $args = args_with_numbered(p, $args, max_numparam, it_id);
5388 $$ = NEW_ITER($args, $bodystmt, &@$);
5389 /*% ripper: do_block!($:args, $:bodystmt) %*/
5390 CMDARG_POP();
5391 restore_block_exit(p, $allow_exits);
5392 numparam_pop(p, $numparam);
5393 dyna_pop(p, $dyna);
5394 }
5395 ;
5396
5397case_args : arg_value
5398 {
5399 check_literal_when(p, $arg_value, &@arg_value);
5400 $$ = NEW_LIST($arg_value, &@$);
5401 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5402 }
5403 | tSTAR arg_value
5404 {
5405 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5406 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5407 }
5408 | case_args[non_last_args] ',' arg_value
5409 {
5410 check_literal_when(p, $arg_value, &@arg_value);
5411 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5412 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5413 }
5414 | case_args[non_last_args] ',' tSTAR arg_value
5415 {
5416 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5417 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5418 }
5419 ;
5420
5421case_body : k_when case_args then
5422 compstmt
5423 cases
5424 {
5425 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5426 fixpos($$, $2);
5427 /*% ripper: when!($:2, $:4, $:5) %*/
5428 }
5429 ;
5430
5431cases : opt_else
5432 | case_body
5433 ;
5434
5435p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5436p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5437
5438p_in_kwarg : {
5439 $$ = p->ctxt;
5440 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5441 p->command_start = FALSE;
5442 p->ctxt.in_kwarg = 1;
5443 }
5444 ;
5445
5446p_case_body : keyword_in
5447 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5448 p_top_expr[expr] then
5449 {
5450 pop_pktbl(p, $p_pktbl);
5451 pop_pvtbl(p, $p_pvtbl);
5452 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5453 }
5454 compstmt
5455 p_cases[cases]
5456 {
5457 $$ = NEW_IN($expr, $compstmt, $cases, &@$);
5458 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5459 }
5460 ;
5461
5462p_cases : opt_else
5463 | p_case_body
5464 ;
5465
5466p_top_expr : p_top_expr_body
5467 | p_top_expr_body modifier_if expr_value
5468 {
5469 $$ = new_if(p, $3, $1, 0, &@$);
5470 fixpos($$, $3);
5471 /*% ripper: if_mod!($:3, $:1) %*/
5472 }
5473 | p_top_expr_body modifier_unless expr_value
5474 {
5475 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5476 fixpos($$, $3);
5477 /*% ripper: unless_mod!($:3, $:1) %*/
5478 }
5479 ;
5480
5481p_top_expr_body : p_expr
5482 | p_expr ','
5483 {
5484 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5485 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5486 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5487 }
5488 | p_expr ',' p_args
5489 {
5490 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5491 nd_set_first_loc($$, @1.beg_pos);
5492 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5493 }
5494 | p_find
5495 {
5496 $$ = new_find_pattern(p, 0, $1, &@$);
5497 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5498 }
5499 | p_args_tail
5500 {
5501 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5502 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5503 }
5504 | p_kwargs
5505 {
5506 $$ = new_hash_pattern(p, 0, $1, &@$);
5507 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5508 }
5509 ;
5510
5511p_expr : p_as
5512 ;
5513
5514p_as : p_expr tASSOC p_variable
5515 {
5516 NODE *n = NEW_LIST($1, &@$);
5517 n = list_append(p, n, $3);
5518 $$ = new_hash(p, n, &@$);
5519 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5520 }
5521 | p_alt
5522 ;
5523
5524p_alt : p_alt '|' p_expr_basic
5525 {
5526 $$ = NEW_OR($1, $3, &@$, &@2);
5527 /*% ripper: binary!($:1, ID2VAL(idOr), $:3) %*/
5528 }
5529 | p_expr_basic
5530 ;
5531
5532p_lparen : '(' p_pktbl
5533 {
5534 $$ = $2;
5535 /*% ripper: $:2 %*/
5536 }
5537 ;
5538
5539p_lbracket : '[' p_pktbl
5540 {
5541 $$ = $2;
5542 /*% ripper: $:2 %*/
5543 }
5544 ;
5545
5546p_expr_basic : p_value
5547 | p_variable
5548 | p_const p_lparen[p_pktbl] p_args rparen
5549 {
5550 pop_pktbl(p, $p_pktbl);
5551 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5552 nd_set_first_loc($$, @p_const.beg_pos);
5553 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5554 }
5555 | p_const p_lparen[p_pktbl] p_find rparen
5556 {
5557 pop_pktbl(p, $p_pktbl);
5558 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5559 nd_set_first_loc($$, @p_const.beg_pos);
5560 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5561 }
5562 | p_const p_lparen[p_pktbl] p_kwargs rparen
5563 {
5564 pop_pktbl(p, $p_pktbl);
5565 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5566 nd_set_first_loc($$, @p_const.beg_pos);
5567 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5568 }
5569 | p_const '(' rparen
5570 {
5571 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5572 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5573 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5574 }
5575 | p_const p_lbracket[p_pktbl] p_args rbracket
5576 {
5577 pop_pktbl(p, $p_pktbl);
5578 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5579 nd_set_first_loc($$, @p_const.beg_pos);
5580 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5581 }
5582 | p_const p_lbracket[p_pktbl] p_find rbracket
5583 {
5584 pop_pktbl(p, $p_pktbl);
5585 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5586 nd_set_first_loc($$, @p_const.beg_pos);
5587 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5588 }
5589 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5590 {
5591 pop_pktbl(p, $p_pktbl);
5592 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5593 nd_set_first_loc($$, @p_const.beg_pos);
5594 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5595 }
5596 | p_const '[' rbracket
5597 {
5598 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5599 $$ = new_array_pattern(p, $1, 0, $$, &@$);
5600 /*% ripper: aryptn!($:1, Qnil, Qnil, Qnil) %*/
5601 }
5602 | tLBRACK p_args rbracket
5603 {
5604 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5605 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5606 }
5607 | tLBRACK p_find rbracket
5608 {
5609 $$ = new_find_pattern(p, 0, $p_find, &@$);
5610 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5611 }
5612 | tLBRACK rbracket
5613 {
5614 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5615 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5616 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5617 }
5618 | tLBRACE p_pktbl lex_ctxt[ctxt]
5619 {
5620 p->ctxt.in_kwarg = 0;
5621 }
5622 p_kwargs rbrace
5623 {
5624 pop_pktbl(p, $p_pktbl);
5625 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5626 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5627 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5628 }
5629 | tLBRACE rbrace
5630 {
5631 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5632 $$ = new_hash_pattern(p, 0, $$, &@$);
5633 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5634 }
5635 | tLPAREN p_pktbl p_expr rparen
5636 {
5637 pop_pktbl(p, $p_pktbl);
5638 $$ = $p_expr;
5639 /*% ripper: $:p_expr %*/
5640 }
5641 ;
5642
5643p_args : p_expr
5644 {
5645 NODE *pre_args = NEW_LIST($1, &@$);
5646 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5647 /*% ripper: [[$:1], Qnil, Qnil] %*/
5648 }
5649 | p_args_head
5650 {
5651 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5652 /*% ripper: [$:1, Qnil, Qnil] %*/
5653 }
5654 | p_args_head p_arg
5655 {
5656 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5657 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5658 }
5659 | p_args_head p_rest
5660 {
5661 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5662 /*% ripper: [$:1, $:2, Qnil] %*/
5663 }
5664 | p_args_head p_rest ',' p_args_post
5665 {
5666 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5667 /*% ripper: [$:1, $:2, $:4] %*/
5668 }
5669 | p_args_tail
5670 ;
5671
5672p_args_head : p_arg ','
5673 {
5674 $$ = $1;
5675 }
5676 | p_args_head p_arg ','
5677 {
5678 $$ = list_concat($1, $2);
5679 /*% ripper: rb_ary_concat($:1, $:2) %*/
5680 }
5681 ;
5682
5683p_args_tail : p_rest
5684 {
5685 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5686 /*% ripper: [Qnil, $:1, Qnil] %*/
5687 }
5688 | p_rest ',' p_args_post
5689 {
5690 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5691 /*% ripper: [Qnil, $:1, $:3] %*/
5692 }
5693 ;
5694
5695p_find : p_rest ',' p_args_post ',' p_rest
5696 {
5697 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5698 /*% ripper: [$:1, $:3, $:5] %*/
5699 }
5700 ;
5701
5702
5703p_rest : tSTAR tIDENTIFIER
5704 {
5705 error_duplicate_pattern_variable(p, $2, &@2);
5706 /*% ripper: var_field!($:2) %*/
5707 $$ = assignable(p, $2, 0, &@$);
5708 }
5709 | tSTAR
5710 {
5711 $$ = 0;
5712 /*% ripper: var_field!(Qnil) %*/
5713 }
5714 ;
5715
5716p_args_post : p_arg
5717 | p_args_post ',' p_arg
5718 {
5719 $$ = list_concat($1, $3);
5720 /*% ripper: rb_ary_concat($:1, $:3) %*/
5721 }
5722 ;
5723
5724p_arg : p_expr
5725 {
5726 $$ = NEW_LIST($1, &@$);
5727 /*% ripper: [$:1] %*/
5728 }
5729 ;
5730
5731p_kwargs : p_kwarg ',' p_any_kwrest
5732 {
5733 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5734 /*% ripper: [$:1, $:3] %*/
5735 }
5736 | p_kwarg
5737 {
5738 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5739 /*% ripper: [$:1, Qnil] %*/
5740 }
5741 | p_kwarg ','
5742 {
5743 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5744 /*% ripper: [$:1, Qnil] %*/
5745 }
5746 | p_any_kwrest
5747 {
5748 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5749 /*% ripper: [[], $:1] %*/
5750 }
5751 ;
5752
5753p_kwarg : p_kw
5754 /*% ripper[brace]: [$:1] %*/
5755 | p_kwarg ',' p_kw
5756 {
5757 $$ = list_concat($1, $3);
5758 /*% ripper: rb_ary_push($:1, $:3) %*/
5759 }
5760 ;
5761
5762p_kw : p_kw_label p_expr
5763 {
5764 error_duplicate_pattern_key(p, $1, &@1);
5765 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5766 /*% ripper: [$:1, $:2] %*/
5767 }
5768 | p_kw_label
5769 {
5770 error_duplicate_pattern_key(p, $1, &@1);
5771 if ($1 && !is_local_id($1)) {
5772 yyerror1(&@1, "key must be valid as local variables");
5773 }
5774 error_duplicate_pattern_variable(p, $1, &@1);
5775 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5776 /*% ripper: [$:1, Qnil] %*/
5777 }
5778 ;
5779
5780p_kw_label : tLABEL
5781 | tSTRING_BEG string_contents tLABEL_END
5782 {
5783 YYLTYPE loc = code_loc_gen(&@1, &@3);
5784 if (!$2 || nd_type_p($2, NODE_STR)) {
5785 NODE *node = dsym_node(p, $2, &loc);
5786 $$ = rb_sym2id(rb_node_sym_string_val(node));
5787 }
5788 else {
5789 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5790 $$ = rb_intern_str(STR_NEW0());
5791 }
5792 /*% ripper: $:2 %*/
5793 }
5794 ;
5795
5796p_kwrest : kwrest_mark tIDENTIFIER
5797 {
5798 $$ = $2;
5799 /*% ripper: var_field!($:2) %*/
5800 }
5801 | kwrest_mark
5802 {
5803 $$ = 0;
5804 /*% ripper: Qnil %*/
5805 }
5806 ;
5807
5808p_kwnorest : kwrest_mark keyword_nil
5809 {
5810 $$ = 0;
5811 }
5812 ;
5813
5814p_any_kwrest : p_kwrest
5815 | p_kwnorest
5816 {
5817 $$ = idNil;
5818 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5819 }
5820 ;
5821
5822p_value : p_primitive
5823 | p_primitive tDOT2 p_primitive
5824 {
5825 value_expr($1);
5826 value_expr($3);
5827 $$ = NEW_DOT2($1, $3, &@$);
5828 /*% ripper: dot2!($:1, $:3) %*/
5829 }
5830 | p_primitive tDOT3 p_primitive
5831 {
5832 value_expr($1);
5833 value_expr($3);
5834 $$ = NEW_DOT3($1, $3, &@$);
5835 /*% ripper: dot3!($:1, $:3) %*/
5836 }
5837 | p_primitive tDOT2
5838 {
5839 value_expr($1);
5840 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
5841 /*% ripper: dot2!($:1, Qnil) %*/
5842 }
5843 | p_primitive tDOT3
5844 {
5845 value_expr($1);
5846 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
5847 /*% ripper: dot3!($:1, Qnil) %*/
5848 }
5849 | p_var_ref
5850 | p_expr_ref
5851 | p_const
5852 | tBDOT2 p_primitive
5853 {
5854 value_expr($2);
5855 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
5856 /*% ripper: dot2!(Qnil, $:2) %*/
5857 }
5858 | tBDOT3 p_primitive
5859 {
5860 value_expr($2);
5861 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
5862 /*% ripper: dot3!(Qnil, $:2) %*/
5863 }
5864 ;
5865
5866p_primitive : inline_primary
5867 | keyword_variable
5868 {
5869 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5870 /*% ripper: var_ref!($:1) %*/
5871 }
5872 | lambda
5873 ;
5874
5875p_variable : tIDENTIFIER
5876 {
5877 error_duplicate_pattern_variable(p, $1, &@1);
5878 /*% ripper: var_field!($:1) %*/
5879 $$ = assignable(p, $1, 0, &@$);
5880 }
5881 ;
5882
5883p_var_ref : '^' tIDENTIFIER
5884 {
5885 NODE *n = gettable(p, $2, &@$);
5886 if (!n) {
5887 n = NEW_ERROR(&@$);
5888 }
5889 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5890 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5891 }
5892 $$ = n;
5893 /*% ripper: var_ref!($:2) %*/
5894 }
5895 | '^' nonlocal_var
5896 {
5897 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5898 /*% ripper: var_ref!($:2) %*/
5899 }
5900 ;
5901
5902p_expr_ref : '^' tLPAREN expr_value rparen
5903 {
5904 $$ = NEW_BLOCK($3, &@$);
5905 /*% ripper: begin!($:3) %*/
5906 }
5907 ;
5908
5909p_const : tCOLON3 cname
5910 {
5911 $$ = NEW_COLON3($2, &@$);
5912 /*% ripper: top_const_ref!($:2) %*/
5913 }
5914 | p_const tCOLON2 cname
5915 {
5916 $$ = NEW_COLON2($1, $3, &@$);
5917 /*% ripper: const_path_ref!($:1, $:3) %*/
5918 }
5919 | tCONSTANT
5920 {
5921 $$ = gettable(p, $1, &@$);
5922 /*% ripper: var_ref!($:1) %*/
5923 }
5924 ;
5925
5926opt_rescue : k_rescue exc_list exc_var then
5927 compstmt
5928 opt_rescue
5929 {
5930 NODE *err = $3;
5931 if ($3) {
5932 err = NEW_ERRINFO(&@3);
5933 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5934 }
5935 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5936 if ($2) {
5937 fixpos($$, $2);
5938 }
5939 else if ($3) {
5940 fixpos($$, $3);
5941 }
5942 else {
5943 fixpos($$, $5);
5944 }
5945 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5946 }
5947 | none
5948 ;
5949
5950exc_list : arg_value
5951 {
5952 $$ = NEW_LIST($1, &@$);
5953 /*% ripper: rb_ary_new3(1, $:1) %*/
5954 }
5955 | mrhs
5956 {
5957 if (!($$ = splat_array($1))) $$ = $1;
5958 }
5959 | none
5960 ;
5961
5962exc_var : tASSOC lhs
5963 {
5964 $$ = $2;
5965 /*% ripper: $:2 %*/
5966 }
5967 | none
5968 ;
5969
5970opt_ensure : k_ensure stmts terms?
5971 {
5972 p->ctxt.in_rescue = $1.in_rescue;
5973 $$ = $2;
5974 void_expr(p, void_stmts(p, $$));
5975 /*% ripper: ensure!($:2) %*/
5976 }
5977 | none
5978 ;
5979
5980literal : numeric
5981 | symbol
5982 ;
5983
5984strings : string
5985 {
5986 NODE *node = $1;
5987 if (!node) {
5988 node = NEW_STR(STRING_NEW0(), &@$);
5989 }
5990 else {
5991 node = evstr2dstr(p, node);
5992 }
5993 $$ = node;
5994 /*% ripper: $:1 %*/
5995 }
5996 ;
5997
5998string : tCHAR
5999 | string1
6000 | string string1
6001 {
6002 $$ = literal_concat(p, $1, $2, &@$);
6003 /*% ripper: string_concat!($:1, $:2) %*/
6004 }
6005 ;
6006
6007string1 : tSTRING_BEG string_contents tSTRING_END
6008 {
6009 $$ = heredoc_dedent(p, $2);
6010 if ($$) nd_set_loc($$, &@$);
6011 /*% ripper: $:2 %*/
6012 if (p->heredoc_indent > 0) {
6013 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
6014 p->heredoc_indent = 0;
6015 }
6016 /*% ripper: string_literal!($:$) %*/
6017 }
6018 ;
6019
6020xstring : tXSTRING_BEG xstring_contents tSTRING_END
6021 {
6022 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
6023 /*% ripper: $:2 %*/
6024 if (p->heredoc_indent > 0) {
6025 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
6026 p->heredoc_indent = 0;
6027 }
6028 /*% ripper: xstring_literal!($:$) %*/
6029 }
6030 ;
6031
6032regexp : tREGEXP_BEG regexp_contents tREGEXP_END
6033 {
6034 $$ = new_regexp(p, $2, $3, &@$);
6035 /*% ripper: regexp_literal!($:2, $:3) %*/
6036 }
6037 ;
6038
6039words : words(tWORDS_BEG, word_list) <node>
6040 ;
6041
6042word_list : /* none */
6043 {
6044 $$ = 0;
6045 /*% ripper: words_new! %*/
6046 }
6047 | word_list word ' '+
6048 {
6049 $$ = list_append(p, $1, evstr2dstr(p, $2));
6050 /*% ripper: words_add!($:1, $:2) %*/
6051 }
6052 ;
6053
6054word : string_content
6055 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
6056 | word string_content
6057 {
6058 $$ = literal_concat(p, $1, $2, &@$);
6059 /*% ripper: word_add!($:1, $:2) %*/
6060 }
6061 ;
6062
6063symbols : words(tSYMBOLS_BEG, symbol_list) <node>
6064 ;
6065
6066symbol_list : /* none */
6067 {
6068 $$ = 0;
6069 /*% ripper: symbols_new! %*/
6070 }
6071 | symbol_list word ' '+
6072 {
6073 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
6074 /*% ripper: symbols_add!($:1, $:2) %*/
6075 }
6076 ;
6077
6078qwords : words(tQWORDS_BEG, qword_list) <node>
6079 ;
6080
6081qsymbols : words(tQSYMBOLS_BEG, qsym_list) <node>
6082 ;
6083
6084qword_list : /* none */
6085 {
6086 $$ = 0;
6087 /*% ripper: qwords_new! %*/
6088 }
6089 | qword_list tSTRING_CONTENT ' '+
6090 {
6091 $$ = list_append(p, $1, $2);
6092 /*% ripper: qwords_add!($:1, $:2) %*/
6093 }
6094 ;
6095
6096qsym_list : /* none */
6097 {
6098 $$ = 0;
6099 /*% ripper: qsymbols_new! %*/
6100 }
6101 | qsym_list tSTRING_CONTENT ' '+
6102 {
6103 $$ = symbol_append(p, $1, $2);
6104 /*% ripper: qsymbols_add!($:1, $:2) %*/
6105 }
6106 ;
6107
6108string_contents : /* none */
6109 {
6110 $$ = 0;
6111 /*% ripper: string_content! %*/
6112 }
6113 | string_contents string_content
6114 {
6115 $$ = literal_concat(p, $1, $2, &@$);
6116 /*% ripper: string_add!($:1, $:2) %*/
6117 }
6118 ;
6119
6120xstring_contents: /* none */
6121 {
6122 $$ = 0;
6123 /*% ripper: xstring_new! %*/
6124 }
6125 | xstring_contents string_content
6126 {
6127 $$ = literal_concat(p, $1, $2, &@$);
6128 /*% ripper: xstring_add!($:1, $:2) %*/
6129 }
6130 ;
6131
6132regexp_contents: /* none */
6133 {
6134 $$ = 0;
6135 /*% ripper: regexp_new! %*/
6136 }
6137 | regexp_contents string_content
6138 {
6139 NODE *head = $1, *tail = $2;
6140 if (!head) {
6141 $$ = tail;
6142 }
6143 else if (!tail) {
6144 $$ = head;
6145 }
6146 else {
6147 switch (nd_type(head)) {
6148 case NODE_STR:
6149 head = str2dstr(p, head);
6150 break;
6151 case NODE_DSTR:
6152 break;
6153 default:
6154 head = list_append(p, NEW_DSTR(0, &@$), head);
6155 break;
6156 }
6157 $$ = list_append(p, head, tail);
6158 }
6159 /*% ripper: regexp_add!($:1, $:2) %*/
6160 }
6161 ;
6162
6163string_content : tSTRING_CONTENT
6164 /*% ripper[brace]: $:1 %*/
6165 | tSTRING_DVAR
6166 {
6167 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6168 $$ = p->lex.strterm;
6169 p->lex.strterm = 0;
6170 SET_LEX_STATE(EXPR_BEG);
6171 }<strterm>
6172 string_dvar
6173 {
6174 p->lex.strterm = $2;
6175 $$ = NEW_EVSTR($3, &@$);
6176 nd_set_line($$, @3.end_pos.lineno);
6177 /*% ripper: string_dvar!($:3) %*/
6178 }
6179 | tSTRING_DBEG[state]
6180 {
6181 CMDARG_PUSH(0);
6182 COND_PUSH(0);
6183 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6184 $$ = p->lex.strterm;
6185 p->lex.strterm = 0;
6186 SET_LEX_STATE(EXPR_BEG);
6187 }[term]<strterm>
6188 {
6189 $$ = p->lex.brace_nest;
6190 p->lex.brace_nest = 0;
6191 }[brace]<num>
6192 {
6193 $$ = p->heredoc_indent;
6194 p->heredoc_indent = 0;
6195 }[indent]<num>
6196 compstmt string_dend
6197 {
6198 COND_POP();
6199 CMDARG_POP();
6200 p->lex.strterm = $term;
6201 SET_LEX_STATE($state);
6202 p->lex.brace_nest = $brace;
6203 p->heredoc_indent = $indent;
6204 p->heredoc_line_indent = -1;
6205 if ($compstmt) nd_unset_fl_newline($compstmt);
6206 $$ = new_evstr(p, $compstmt, &@$);
6207 /*% ripper: string_embexpr!($:compstmt) %*/
6208 }
6209 ;
6210
6211string_dend : tSTRING_DEND
6212 | END_OF_INPUT
6213 ;
6214
6215string_dvar : nonlocal_var
6216 {
6217 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6218 /*% ripper: var_ref!($:1) %*/
6219 }
6220 | backref
6221 ;
6222
6223symbol : ssym
6224 | dsym
6225 ;
6226
6227ssym : tSYMBEG sym
6228 {
6229 SET_LEX_STATE(EXPR_END);
6230 VALUE str = rb_id2str($2);
6231 /*
6232 * TODO:
6233 * set_yylval_noname sets invalid id to yylval.
6234 * This branch can be removed once yylval is changed to
6235 * hold lexed string.
6236 */
6237 if (!str) str = STR_NEW0();
6238 $$ = NEW_SYM(str, &@$);
6239 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6240 }
6241 ;
6242
6243sym : fname
6244 | nonlocal_var
6245 ;
6246
6247dsym : tSYMBEG string_contents tSTRING_END
6248 {
6249 SET_LEX_STATE(EXPR_END);
6250 $$ = dsym_node(p, $2, &@$);
6251 /*% ripper: dyna_symbol!($:2) %*/
6252 }
6253 ;
6254
6255numeric : simple_numeric
6256 | tUMINUS_NUM simple_numeric %prec tLOWEST
6257 {
6258 $$ = $2;
6259 negate_lit(p, $$);
6260 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6261 }
6262 ;
6263
6264simple_numeric : tINTEGER
6265 | tFLOAT
6266 | tRATIONAL
6267 | tIMAGINARY
6268 ;
6269
6270nonlocal_var : tIVAR
6271 | tGVAR
6272 | tCVAR
6273 ;
6274
6275user_variable : ident_or_const
6276 | nonlocal_var
6277 ;
6278
6279keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6280 | keyword_self {$$ = KWD2EID(self, $1);}
6281 | keyword_true {$$ = KWD2EID(true, $1);}
6282 | keyword_false {$$ = KWD2EID(false, $1);}
6283 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6284 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6285 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6286 ;
6287
6288var_ref : user_variable
6289 {
6290 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6291 if (ifdef_ripper(id_is_var(p, $1), false)) {
6292 /*% ripper: var_ref!($:1) %*/
6293 }
6294 else {
6295 /*% ripper: vcall!($:1) %*/
6296 }
6297 }
6298 | keyword_variable
6299 {
6300 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6301 /*% ripper: var_ref!($:1) %*/
6302 }
6303 ;
6304
6305var_lhs : user_or_keyword_variable
6306 {
6307 /*% ripper: var_field!($:1) %*/
6308 $$ = assignable(p, $1, 0, &@$);
6309 }
6310 ;
6311
6312backref : tNTH_REF
6313 | tBACK_REF
6314 ;
6315
6316superclass : '<'
6317 {
6318 SET_LEX_STATE(EXPR_BEG);
6319 p->command_start = TRUE;
6320 }
6321 expr_value term
6322 {
6323 $$ = $3;
6324 /*% ripper: $:3 %*/
6325 }
6326 | /* none */
6327 {
6328 $$ = 0;
6329 /*% ripper: Qnil %*/
6330 }
6331 ;
6332
6333f_opt_paren_args: f_paren_args
6334 | none
6335 {
6336 p->ctxt.in_argdef = 0;
6337 $$ = new_args_tail(p, 0, 0, 0, &@0);
6338 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6339 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6340 }
6341 ;
6342
6343f_paren_args : '(' f_args rparen
6344 {
6345 $$ = $2;
6346 /*% ripper: paren!($:2) %*/
6347 SET_LEX_STATE(EXPR_BEG);
6348 p->command_start = TRUE;
6349 p->ctxt.in_argdef = 0;
6350 }
6351 ;
6352
6353f_arglist : f_paren_args
6354 | {
6355 $$ = p->ctxt;
6356 p->ctxt.in_kwarg = 1;
6357 p->ctxt.in_argdef = 1;
6358 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6359 }<ctxt>
6360 f_args term
6361 {
6362 p->ctxt.in_kwarg = $1.in_kwarg;
6363 p->ctxt.in_argdef = 0;
6364 $$ = $2;
6365 SET_LEX_STATE(EXPR_BEG);
6366 p->command_start = TRUE;
6367 /*% ripper: $:2 %*/
6368 }
6369 ;
6370
6371args_tail : f_kwarg(f_kw) ',' f_kwrest opt_f_block_arg
6372 {
6373 $$ = new_args_tail(p, $1, $3, $4, &@3);
6374 /*% ripper: [$:1, $:3, $:4] %*/
6375 }
6376 | f_kwarg(f_kw) opt_f_block_arg
6377 {
6378 $$ = new_args_tail(p, $1, 0, $2, &@1);
6379 /*% ripper: [$:1, Qnil, $:2] %*/
6380 }
6381 | f_any_kwrest opt_f_block_arg
6382 {
6383 $$ = new_args_tail(p, 0, $1, $2, &@1);
6384 /*% ripper: [Qnil, $:1, $:2] %*/
6385 }
6386 | f_block_arg
6387 {
6388 $$ = new_args_tail(p, 0, 0, $1, &@1);
6389 /*% ripper: [Qnil, Qnil, $:1] %*/
6390 }
6391 | args_forward
6392 {
6393 ID fwd = $args_forward;
6394 if (lambda_beginning_p() ||
6395 (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest)) {
6396 yyerror0("unexpected ... in lambda argument");
6397 fwd = 0;
6398 }
6399 else {
6400 add_forwarding_args(p);
6401 }
6402 $$ = new_args_tail(p, 0, fwd, arg_FWD_BLOCK, &@1);
6403 $$->nd_ainfo.forwarding = 1;
6404 /*% ripper: [Qnil, $:1, Qnil] %*/
6405 }
6406 ;
6407
6408f_args : f_arg ',' f_optarg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6409 {
6410 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
6411 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
6412 }
6413 | f_arg ',' f_optarg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6414 {
6415 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6416 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
6417 }
6418 | f_arg ',' f_optarg(arg_value) opt_args_tail(args_tail)
6419 {
6420 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
6421 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
6422 }
6423 | f_arg ',' f_optarg(arg_value) ',' f_arg opt_args_tail(args_tail)
6424 {
6425 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
6426 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
6427 }
6428 | f_arg ',' f_rest_arg opt_args_tail(args_tail)
6429 {
6430 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
6431 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
6432 }
6433 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6434 {
6435 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
6436 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
6437 }
6438 | f_arg opt_args_tail(args_tail)
6439 {
6440 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
6441 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
6442 }
6443 | f_optarg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6444 {
6445 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
6446 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
6447 }
6448 | f_optarg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6449 {
6450 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
6451 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
6452 }
6453 | f_optarg(arg_value) opt_args_tail(args_tail)
6454 {
6455 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
6456 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
6457 }
6458 | f_optarg(arg_value) ',' f_arg opt_args_tail(args_tail)
6459 {
6460 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
6461 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
6462 }
6463 | f_rest_arg opt_args_tail(args_tail)
6464 {
6465 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
6466 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
6467 }
6468 | f_rest_arg ',' f_arg opt_args_tail(args_tail)
6469 {
6470 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
6471 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
6472 }
6473 | args_tail
6474 {
6475 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
6476 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
6477 }
6478 | /* none */
6479 {
6480 $$ = new_args_tail(p, 0, 0, 0, &@0);
6481 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6482 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6483 }
6484 ;
6485
6486args_forward : tBDOT3
6487 {
6488#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
6489 $$ = 0;
6490#else
6491 $$ = idFWD_KWREST;
6492#endif
6493 /*% ripper: args_forward! %*/
6494 }
6495 ;
6496
6497f_bad_arg : tCONSTANT
6498 {
6499 static const char mesg[] = "formal argument cannot be a constant";
6500 /*%%%*/
6501 yyerror1(&@1, mesg);
6502 /*% %*/
6503 $$ = 0;
6504 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6505 }
6506 | tIVAR
6507 {
6508 static const char mesg[] = "formal argument cannot be an instance variable";
6509 /*%%%*/
6510 yyerror1(&@1, mesg);
6511 /*% %*/
6512 $$ = 0;
6513 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6514 }
6515 | tGVAR
6516 {
6517 static const char mesg[] = "formal argument cannot be a global variable";
6518 /*%%%*/
6519 yyerror1(&@1, mesg);
6520 /*% %*/
6521 $$ = 0;
6522 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6523 }
6524 | tCVAR
6525 {
6526 static const char mesg[] = "formal argument cannot be a class variable";
6527 /*%%%*/
6528 yyerror1(&@1, mesg);
6529 /*% %*/
6530 $$ = 0;
6531 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6532 }
6533 ;
6534
6535f_norm_arg : f_bad_arg
6536 | tIDENTIFIER
6537 {
6538 VALUE e = formal_argument_error(p, $$ = $1);
6539 if (e) {
6540 /*% ripper[error]: param_error!(?e, $:1) %*/
6541 }
6542 p->max_numparam = ORDINAL_PARAM;
6543 }
6544 ;
6545
6546f_arg_asgn : f_norm_arg
6547 {
6548 ID id = $1;
6549 arg_var(p, id);
6550 $$ = $1;
6551 }
6552 ;
6553
6554f_arg_item : f_arg_asgn
6555 {
6556 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6557 /*% ripper: $:1 %*/
6558 }
6559 | tLPAREN f_margs rparen
6560 {
6561 ID tid = internal_id(p);
6562 YYLTYPE loc;
6563 loc.beg_pos = @2.beg_pos;
6564 loc.end_pos = @2.beg_pos;
6565 arg_var(p, tid);
6566 if (dyna_in_block(p)) {
6567 $2->nd_value = NEW_DVAR(tid, &loc);
6568 }
6569 else {
6570 $2->nd_value = NEW_LVAR(tid, &loc);
6571 }
6572 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6573 $$->nd_next = (NODE *)$2;
6574 /*% ripper: mlhs_paren!($:2) %*/
6575 }
6576 ;
6577
6578f_arg : f_arg_item
6579 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6580 | f_arg ',' f_arg_item
6581 {
6582 $$ = $1;
6583 $$->nd_plen++;
6584 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6585 rb_discard_node(p, (NODE *)$3);
6586 /*% ripper: rb_ary_push($:1, $:3) %*/
6587 }
6588 ;
6589
6590
6591f_label : tLABEL
6592 {
6593 VALUE e = formal_argument_error(p, $$ = $1);
6594 if (e) {
6595 $$ = 0;
6596 /*% ripper[error]: param_error!(?e, $:1) %*/
6597 }
6598 /*
6599 * Workaround for Prism::ParseTest#test_filepath for
6600 * "unparser/corpus/literal/def.txt"
6601 *
6602 * See the discussion on https://github.com/ruby/ruby/pull/9923
6603 */
6604 arg_var(p, ifdef_ripper(0, $1));
6605 /*% ripper: $:1 %*/
6606 p->max_numparam = ORDINAL_PARAM;
6607 p->ctxt.in_argdef = 0;
6608 }
6609 ;
6610
6611f_kw : f_label arg_value
6612 {
6613 p->ctxt.in_argdef = 1;
6614 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6615 /*% ripper: [$:$, $:2] %*/
6616 }
6617 | f_label
6618 {
6619 p->ctxt.in_argdef = 1;
6620 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6621 /*% ripper: [$:$, 0] %*/
6622 }
6623 ;
6624
6625f_block_kw : f_label primary_value
6626 {
6627 p->ctxt.in_argdef = 1;
6628 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6629 /*% ripper: [$:$, $:2] %*/
6630 }
6631 | f_label
6632 {
6633 p->ctxt.in_argdef = 1;
6634 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6635 /*% ripper: [$:$, 0] %*/
6636 }
6637 ;
6638
6639kwrest_mark : tPOW
6640 | tDSTAR
6641 ;
6642
6643f_no_kwarg : p_kwnorest
6644 {
6645 /*% ripper: nokw_param!(Qnil) %*/
6646 }
6647 ;
6648
6649f_kwrest : kwrest_mark tIDENTIFIER
6650 {
6651 arg_var(p, shadowing_lvar(p, $2));
6652 $$ = $2;
6653 /*% ripper: kwrest_param!($:2) %*/
6654 }
6655 | kwrest_mark
6656 {
6657 arg_var(p, idFWD_KWREST);
6658 $$ = idFWD_KWREST;
6659 /*% ripper: kwrest_param!(Qnil) %*/
6660 }
6661 ;
6662
6663restarg_mark : '*'
6664 | tSTAR
6665 ;
6666
6667f_rest_arg : restarg_mark tIDENTIFIER
6668 {
6669 arg_var(p, shadowing_lvar(p, $2));
6670 $$ = $2;
6671 /*% ripper: rest_param!($:2) %*/
6672 }
6673 | restarg_mark
6674 {
6675 arg_var(p, idFWD_REST);
6676 $$ = idFWD_REST;
6677 /*% ripper: rest_param!(Qnil) %*/
6678 }
6679 ;
6680
6681blkarg_mark : '&'
6682 | tAMPER
6683 ;
6684
6685f_block_arg : blkarg_mark tIDENTIFIER
6686 {
6687 arg_var(p, shadowing_lvar(p, $2));
6688 $$ = $2;
6689 /*% ripper: blockarg!($:2) %*/
6690 }
6691 | blkarg_mark
6692 {
6693 arg_var(p, idFWD_BLOCK);
6694 $$ = idFWD_BLOCK;
6695 /*% ripper: blockarg!(Qnil) %*/
6696 }
6697 ;
6698
6699opt_f_block_arg : ',' f_block_arg
6700 {
6701 $$ = $2;
6702 /*% ripper: $:2 %*/
6703 }
6704 | none
6705 {
6706 $$ = 0;
6707 /*% ripper: Qnil %*/
6708 }
6709 ;
6710
6711singleton : var_ref
6712 {
6713 value_expr($1);
6714 $$ = $1;
6715 }
6716 | '('
6717 {
6718 SET_LEX_STATE(EXPR_BEG);
6719 p->ctxt.in_argdef = 0;
6720 }
6721 expr rparen
6722 {
6723 p->ctxt.in_argdef = 1;
6724 NODE *expr = last_expr_node($3);
6725 switch (nd_type(expr)) {
6726 case NODE_STR:
6727 case NODE_DSTR:
6728 case NODE_XSTR:
6729 case NODE_DXSTR:
6730 case NODE_REGX:
6731 case NODE_DREGX:
6732 case NODE_SYM:
6733 case NODE_LINE:
6734 case NODE_FILE:
6735 case NODE_ENCODING:
6736 case NODE_INTEGER:
6737 case NODE_FLOAT:
6738 case NODE_RATIONAL:
6739 case NODE_IMAGINARY:
6740 case NODE_DSYM:
6741 case NODE_LIST:
6742 case NODE_ZLIST:
6743 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6744 break;
6745 default:
6746 value_expr($3);
6747 break;
6748 }
6749 $$ = $3;
6750 /*% ripper: paren!($:3) %*/
6751 }
6752 ;
6753
6754assoc_list : none
6755 | assocs trailer
6756 {
6757 $$ = $1;
6758 /*% ripper: assoclist_from_args!($:1) %*/
6759 }
6760 ;
6761
6762assocs : assoc
6763 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6764 | assocs ',' assoc
6765 {
6766 NODE *assocs = $1;
6767 NODE *tail = $3;
6768 if (!assocs) {
6769 assocs = tail;
6770 }
6771 else if (tail) {
6772 if (RNODE_LIST(assocs)->nd_head) {
6773 NODE *n = RNODE_LIST(tail)->nd_next;
6774 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6775 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6776 /* DSTAR */
6777 tail = RNODE_HASH(n)->nd_head;
6778 }
6779 }
6780 if (tail) {
6781 assocs = list_concat(assocs, tail);
6782 }
6783 }
6784 $$ = assocs;
6785 /*% ripper: rb_ary_push($:1, $:3) %*/
6786 }
6787 ;
6788
6789assoc : arg_value tASSOC arg_value
6790 {
6791 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6792 /*% ripper: assoc_new!($:1, $:3) %*/
6793 }
6794 | tLABEL arg_value
6795 {
6796 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6797 /*% ripper: assoc_new!($:1, $:2) %*/
6798 }
6799 | tLABEL
6800 {
6801 NODE *val = gettable(p, $1, &@$);
6802 if (!val) val = NEW_ERROR(&@$);
6803 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6804 /*% ripper: assoc_new!($:1, Qnil) %*/
6805 }
6806 | tSTRING_BEG string_contents tLABEL_END arg_value
6807 {
6808 YYLTYPE loc = code_loc_gen(&@1, &@3);
6809 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6810 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6811 }
6812 | tDSTAR arg_value
6813 {
6814 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6815 /*% ripper: assoc_splat!($:2) %*/
6816 }
6817 | tDSTAR
6818 {
6819 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6820 $$ = list_append(p, NEW_LIST(0, &@$),
6821 NEW_LVAR(idFWD_KWREST, &@$));
6822 /*% ripper: assoc_splat!(Qnil) %*/
6823 }
6824 ;
6825
6826operation : ident_or_const
6827 | tFID
6828 ;
6829
6830operation2 : operation
6831 | op
6832 ;
6833
6834operation3 : tIDENTIFIER
6835 | tFID
6836 | op
6837 ;
6838
6839dot_or_colon : '.'
6840 | tCOLON2
6841 ;
6842
6843call_op : '.'
6844 | tANDDOT
6845 ;
6846
6847call_op2 : call_op
6848 | tCOLON2
6849 ;
6850
6851rparen : '\n'? ')'
6852 ;
6853
6854rbracket : '\n'? ']'
6855 ;
6856
6857rbrace : '\n'? '}'
6858 ;
6859
6860trailer : '\n'?
6861 | ','
6862 ;
6863
6864term : ';' {yyerrok;token_flush(p);}
6865 | '\n'
6866 {
6867 @$.end_pos = @$.beg_pos;
6868 token_flush(p);
6869 }
6870 ;
6871
6872terms : term
6873 | terms ';' {yyerrok;}
6874 ;
6875
6876none : /* none */
6877 {
6878 $$ = 0;
6879 }
6880 ;
6881%%
6882# undef p
6883# undef yylex
6884# undef yylval
6885# define yylval (*p->lval)
6886
6887static int regx_options(struct parser_params*);
6888static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6889static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6890static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6891static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6892
6893#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6894
6895# define set_yylval_node(x) { \
6896 YYLTYPE _cur_loc; \
6897 rb_parser_set_location(p, &_cur_loc); \
6898 yylval.node = (x); \
6899 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6900}
6901# define set_yylval_str(x) \
6902do { \
6903 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6904 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6905} while(0)
6906# define set_yylval_num(x) { \
6907 yylval.num = (x); \
6908 set_parser_s_value(x); \
6909}
6910# define set_yylval_id(x) (yylval.id = (x))
6911# define set_yylval_name(x) { \
6912 (yylval.id = (x)); \
6913 set_parser_s_value(ID2SYM(x)); \
6914}
6915# define yylval_id() (yylval.id)
6916
6917#define set_yylval_noname() set_yylval_id(keyword_nil)
6918#define has_delayed_token(p) (p->delayed.token != NULL)
6919
6920#ifndef RIPPER
6921#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6922#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6923
6924static bool
6925parser_has_token(struct parser_params *p)
6926{
6927 const char *const pcur = p->lex.pcur;
6928 const char *const ptok = p->lex.ptok;
6929 if (p->keep_tokens && (pcur < ptok)) {
6930 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6931 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6932 }
6933 return pcur > ptok;
6934}
6935
6936static const char *
6937escaped_char(int c)
6938{
6939 switch (c) {
6940 case '"': return "\\\"";
6941 case '\\': return "\\\\";
6942 case '\0': return "\\0";
6943 case '\n': return "\\n";
6944 case '\r': return "\\r";
6945 case '\t': return "\\t";
6946 case '\f': return "\\f";
6947 case '\013': return "\\v";
6948 case '\010': return "\\b";
6949 case '\007': return "\\a";
6950 case '\033': return "\\e";
6951 case '\x7f': return "\\c?";
6952 }
6953 return NULL;
6954}
6955
6956static rb_parser_string_t *
6957rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6958{
6959 rb_encoding *enc = p->enc;
6960 const char *ptr = str->ptr;
6961 const char *pend = ptr + str->len;
6962 const char *prev = ptr;
6963 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6964 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6965
6966 while (ptr < pend) {
6967 unsigned int c;
6968 const char *cc;
6969 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6970 if (!MBCLEN_CHARFOUND_P(n)) {
6971 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6972 n = rb_enc_mbminlen(enc);
6973 if (pend < ptr + n)
6974 n = (int)(pend - ptr);
6975 while (n--) {
6976 c = *ptr & 0xf0 >> 4;
6977 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6978 c = *ptr & 0x0f;
6979 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6980 parser_str_cat(result, charbuf, 4);
6981 prev = ++ptr;
6982 }
6983 continue;
6984 }
6985 n = MBCLEN_CHARFOUND_LEN(n);
6986 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6987 ptr += n;
6988 cc = escaped_char(c);
6989 if (cc) {
6990 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6991 parser_str_cat_cstr(result, cc);
6992 prev = ptr;
6993 }
6994 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6995 }
6996 else {
6997 if (ptr - n > prev) {
6998 parser_str_cat(result, prev, ptr - n - prev);
6999 prev = ptr - n;
7000 }
7001 parser_str_cat(result, prev, ptr - prev);
7002 prev = ptr;
7003 }
7004 }
7005 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
7006
7007 return result;
7008}
7009
7010static void
7011parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
7012{
7013 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
7014 token->id = p->token_id;
7015 token->type_name = parser_token2char(p, t);
7016 token->str = str;
7017 token->loc.beg_pos = p->yylloc->beg_pos;
7018 token->loc.end_pos = p->yylloc->end_pos;
7019 rb_parser_ary_push_ast_token(p, p->tokens, token);
7020 p->token_id++;
7021
7022 if (p->debug) {
7023 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
7024 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
7025 line, token->id, token->type_name, str_escaped->ptr,
7026 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
7027 token->loc.end_pos.lineno, token->loc.end_pos.column);
7028 rb_parser_string_free(p, str_escaped);
7029 }
7030}
7031
7032static void
7033parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
7034{
7035 debug_token_line(p, "parser_dispatch_scan_event", line);
7036
7037 if (!parser_has_token(p)) return;
7038
7039 RUBY_SET_YYLLOC(*p->yylloc);
7040
7041 if (p->keep_tokens) {
7042 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
7043 parser_append_tokens(p, str, t, line);
7044 }
7045
7046 token_flush(p);
7047}
7048
7049#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
7050static void
7051parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
7052{
7053 debug_token_line(p, "parser_dispatch_delayed_token", line);
7054
7055 if (!has_delayed_token(p)) return;
7056
7057 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
7058
7059 if (p->keep_tokens) {
7060 /* p->delayed.token is freed by rb_parser_tokens_free */
7061 parser_append_tokens(p, p->delayed.token, t, line);
7062 } else {
7063 rb_parser_string_free(p, p->delayed.token);
7064 }
7065
7066 p->delayed.token = NULL;
7067}
7068#else
7069#define literal_flush(p, ptr) ((void)(ptr))
7070
7071static int
7072ripper_has_scan_event(struct parser_params *p)
7073{
7074 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
7075 return p->lex.pcur > p->lex.ptok;
7076}
7077
7078static VALUE
7079ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
7080{
7081 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
7082 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
7083 RUBY_SET_YYLLOC(*p->yylloc);
7084 token_flush(p);
7085 return rval;
7086}
7087
7088static void
7089ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
7090{
7091 if (!ripper_has_scan_event(p)) return;
7092
7093 set_parser_s_value(ripper_scan_event_val(p, t));
7094}
7095#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
7096
7097static void
7098ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
7099{
7100 /* save and adjust the location to delayed token for callbacks */
7101 int saved_line = p->ruby_sourceline;
7102 const char *saved_tokp = p->lex.ptok;
7103 VALUE s_value, str;
7104
7105 if (!has_delayed_token(p)) return;
7106 p->ruby_sourceline = p->delayed.beg_line;
7107 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
7108 str = rb_str_new_mutable_parser_string(p->delayed.token);
7109 rb_parser_string_free(p, p->delayed.token);
7110 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
7111 set_parser_s_value(s_value);
7112 p->delayed.token = NULL;
7113 p->ruby_sourceline = saved_line;
7114 p->lex.ptok = saved_tokp;
7115}
7116#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
7117#endif /* RIPPER */
7118
7119static inline int
7120is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
7121{
7122 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
7123}
7124
7125static inline int
7126parser_is_identchar(struct parser_params *p)
7127{
7128 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7129}
7130
7131static inline int
7132parser_isascii(struct parser_params *p)
7133{
7134 return ISASCII(*(p->lex.pcur-1));
7135}
7136
7137static void
7138token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7139{
7140 int column = 1, nonspc = 0, i;
7141 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7142 if (*ptr == '\t') {
7143 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7144 }
7145 column++;
7146 if (*ptr != ' ' && *ptr != '\t') {
7147 nonspc = 1;
7148 }
7149 }
7150
7151 ptinfo->beg = loc->beg_pos;
7152 ptinfo->indent = column;
7153 ptinfo->nonspc = nonspc;
7154}
7155
7156static void
7157token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7158{
7159 token_info *ptinfo;
7160
7161 if (!p->token_info_enabled) return;
7162 ptinfo = ALLOC(token_info);
7163 ptinfo->token = token;
7164 ptinfo->next = p->token_info;
7165 token_info_setup(ptinfo, p->lex.pbeg, loc);
7166
7167 p->token_info = ptinfo;
7168}
7169
7170static void
7171token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7172{
7173 token_info *ptinfo_beg = p->token_info;
7174
7175 if (!ptinfo_beg) return;
7176
7177 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7178 token_info_warn(p, token, ptinfo_beg, 1, loc);
7179
7180 p->token_info = ptinfo_beg->next;
7181 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7182}
7183
7184static void
7185token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7186{
7187 token_info *ptinfo_beg = p->token_info;
7188
7189 if (!ptinfo_beg) return;
7190 p->token_info = ptinfo_beg->next;
7191
7192 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7193 ptinfo_beg->beg.column != beg_pos.column ||
7194 strcmp(ptinfo_beg->token, token)) {
7195 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7196 beg_pos.lineno, beg_pos.column, token,
7197 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7198 ptinfo_beg->token);
7199 }
7200
7201 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7202}
7203
7204static void
7205token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7206{
7207 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7208 if (!p->token_info_enabled) return;
7209 if (!ptinfo_beg) return;
7210 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7211 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7212 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7213 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7214 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7215 rb_warn3L(ptinfo_end->beg.lineno,
7216 "mismatched indentations at '%s' with '%s' at %d",
7217 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7218}
7219
7220static int
7221parser_precise_mbclen(struct parser_params *p, const char *ptr)
7222{
7223 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7224 if (!MBCLEN_CHARFOUND_P(len)) {
7225 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7226 return -1;
7227 }
7228 return len;
7229}
7230
7231#ifndef RIPPER
7232static inline void
7233parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7234{
7235 rb_parser_string_t *str;
7236 int lineno = p->ruby_sourceline;
7237 if (!yylloc) {
7238 return;
7239 }
7240 else if (yylloc->beg_pos.lineno == lineno) {
7241 str = p->lex.lastline;
7242 }
7243 else {
7244 return;
7245 }
7246 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7247}
7248
7249static int
7250parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7251{
7252#if 0
7253 YYLTYPE current;
7254
7255 if (!yylloc) {
7256 yylloc = RUBY_SET_YYLLOC(current);
7257 }
7258 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7259 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7260 yylloc = 0;
7261 }
7262#endif
7263 parser_compile_error(p, yylloc, "%s", msg);
7264 parser_show_error_line(p, yylloc);
7265 return 0;
7266}
7267
7268static int
7269parser_yyerror0(struct parser_params *p, const char *msg)
7270{
7271 YYLTYPE current;
7272 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7273}
7274
7275void
7276ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7277{
7278 VALUE mesg;
7279 const int max_line_margin = 30;
7280 const char *ptr, *ptr_end, *pt, *pb;
7281 const char *pre = "", *post = "", *pend;
7282 const char *code = "", *caret = "";
7283 const char *lim;
7284 const char *const pbeg = PARSER_STRING_PTR(str);
7285 char *buf;
7286 long len;
7287 int i;
7288
7289 if (!yylloc) return;
7290 pend = rb_parser_string_end(str);
7291 if (pend > pbeg && pend[-1] == '\n') {
7292 if (--pend > pbeg && pend[-1] == '\r') --pend;
7293 }
7294
7295 pt = pend;
7296 if (lineno == yylloc->end_pos.lineno &&
7297 (pend - pbeg) > yylloc->end_pos.column) {
7298 pt = pbeg + yylloc->end_pos.column;
7299 }
7300
7301 ptr = ptr_end = pt;
7302 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7303 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7304
7305 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7306 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7307
7308 len = ptr_end - ptr;
7309 if (len > 4) {
7310 if (ptr > pbeg) {
7311 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7312 if (ptr > pbeg) pre = "...";
7313 }
7314 if (ptr_end < pend) {
7315 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7316 if (ptr_end < pend) post = "...";
7317 }
7318 }
7319 pb = pbeg;
7320 if (lineno == yylloc->beg_pos.lineno) {
7321 pb += yylloc->beg_pos.column;
7322 if (pb > pt) pb = pt;
7323 }
7324 if (pb < ptr) pb = ptr;
7325 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7326 return;
7327 }
7328 if (RTEST(errbuf)) {
7329 mesg = rb_attr_get(errbuf, idMesg);
7330 if (char_at_end(p, mesg, '\n') != '\n')
7331 rb_str_cat_cstr(mesg, "\n");
7332 }
7333 else {
7334 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7335 }
7336 if (!errbuf && rb_stderr_tty_p()) {
7337#define CSI_BEGIN "\033["
7338#define CSI_SGR "m"
7339 rb_str_catf(mesg,
7340 CSI_BEGIN""CSI_SGR"%s" /* pre */
7341 CSI_BEGIN"1"CSI_SGR"%.*s"
7342 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7343 CSI_BEGIN";1"CSI_SGR"%.*s"
7344 CSI_BEGIN""CSI_SGR"%s" /* post */
7345 "\n",
7346 pre,
7347 (int)(pb - ptr), ptr,
7348 (int)(pt - pb), pb,
7349 (int)(ptr_end - pt), pt,
7350 post);
7351 }
7352 else {
7353 char *p2;
7354
7355 len = ptr_end - ptr;
7356 lim = pt < pend ? pt : pend;
7357 i = (int)(lim - ptr);
7358 buf = ALLOCA_N(char, i+2);
7359 code = ptr;
7360 caret = p2 = buf;
7361 if (ptr <= pb) {
7362 while (ptr < pb) {
7363 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7364 }
7365 *p2++ = '^';
7366 ptr++;
7367 }
7368 if (lim > ptr) {
7369 memset(p2, '~', (lim - ptr));
7370 p2 += (lim - ptr);
7371 }
7372 *p2 = '\0';
7373 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7374 pre, (int)len, code, post,
7375 pre, caret);
7376 }
7377 if (!errbuf) rb_write_error_str(mesg);
7378}
7379#else
7380
7381static int
7382parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7383{
7384 const char *pcur = 0, *ptok = 0;
7385 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7386 p->ruby_sourceline == yylloc->end_pos.lineno) {
7387 pcur = p->lex.pcur;
7388 ptok = p->lex.ptok;
7389 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7390 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7391 }
7392 parser_yyerror0(p, msg);
7393 if (pcur) {
7394 p->lex.ptok = ptok;
7395 p->lex.pcur = pcur;
7396 }
7397 return 0;
7398}
7399
7400static int
7401parser_yyerror0(struct parser_params *p, const char *msg)
7402{
7403 dispatch1(parse_error, STR_NEW2(msg));
7404 ripper_error(p);
7405 return 0;
7406}
7407
7408static inline void
7409parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7410{
7411}
7412#endif /* !RIPPER */
7413
7414static int
7415vtable_size(const struct vtable *tbl)
7416{
7417 if (!DVARS_TERMINAL_P(tbl)) {
7418 return tbl->pos;
7419 }
7420 else {
7421 return 0;
7422 }
7423}
7424
7425static struct vtable *
7426vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7427{
7428 struct vtable *tbl = ALLOC(struct vtable);
7429 tbl->pos = 0;
7430 tbl->capa = 8;
7431 tbl->tbl = ALLOC_N(ID, tbl->capa);
7432 tbl->prev = prev;
7433#ifndef RIPPER
7434 if (p->debug) {
7435 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7436 }
7437#endif
7438 return tbl;
7439}
7440#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7441
7442static void
7443vtable_free_gen(struct parser_params *p, int line, const char *name,
7444 struct vtable *tbl)
7445{
7446#ifndef RIPPER
7447 if (p->debug) {
7448 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7449 }
7450#endif
7451 if (!DVARS_TERMINAL_P(tbl)) {
7452 if (tbl->tbl) {
7453 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7454 }
7455 ruby_sized_xfree(tbl, sizeof(*tbl));
7456 }
7457}
7458#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7459
7460static void
7461vtable_add_gen(struct parser_params *p, int line, const char *name,
7462 struct vtable *tbl, ID id)
7463{
7464#ifndef RIPPER
7465 if (p->debug) {
7466 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7467 line, name, (void *)tbl, rb_id2name(id));
7468 }
7469#endif
7470 if (DVARS_TERMINAL_P(tbl)) {
7471 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7472 return;
7473 }
7474 if (tbl->pos == tbl->capa) {
7475 tbl->capa = tbl->capa * 2;
7476 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7477 }
7478 tbl->tbl[tbl->pos++] = id;
7479}
7480#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7481
7482static void
7483vtable_pop_gen(struct parser_params *p, int line, const char *name,
7484 struct vtable *tbl, int n)
7485{
7486 if (p->debug) {
7487 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7488 line, name, (void *)tbl, n);
7489 }
7490 if (tbl->pos < n) {
7491 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7492 return;
7493 }
7494 tbl->pos -= n;
7495}
7496#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7497
7498static int
7499vtable_included(const struct vtable * tbl, ID id)
7500{
7501 int i;
7502
7503 if (!DVARS_TERMINAL_P(tbl)) {
7504 for (i = 0; i < tbl->pos; i++) {
7505 if (tbl->tbl[i] == id) {
7506 return i+1;
7507 }
7508 }
7509 }
7510 return 0;
7511}
7512
7513static void parser_prepare(struct parser_params *p);
7514
7515static int
7516e_option_supplied(struct parser_params *p)
7517{
7518 return strcmp(p->ruby_sourcefile, "-e") == 0;
7519}
7520
7521#ifndef RIPPER
7522static NODE *parser_append_options(struct parser_params *p, NODE *node);
7523
7524static VALUE
7525yycompile0(VALUE arg)
7526{
7527 int n;
7528 NODE *tree;
7529 struct parser_params *p = (struct parser_params *)arg;
7530 int cov = FALSE;
7531
7532 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7533 cov = TRUE;
7534 }
7535
7536 if (p->debug_lines) {
7537 p->ast->body.script_lines = p->debug_lines;
7538 }
7539
7540 parser_prepare(p);
7541#define RUBY_DTRACE_PARSE_HOOK(name) \
7542 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7543 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7544 }
7545 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7546 n = yyparse(p);
7547 RUBY_DTRACE_PARSE_HOOK(END);
7548
7549 p->debug_lines = 0;
7550
7551 xfree(p->lex.strterm);
7552 p->lex.strterm = 0;
7553 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7554 if (n || p->error_p) {
7555 VALUE mesg = p->error_buffer;
7556 if (!mesg) {
7557 mesg = syntax_error_new();
7558 }
7559 if (!p->error_tolerant) {
7560 rb_set_errinfo(mesg);
7561 return FALSE;
7562 }
7563 }
7564 tree = p->eval_tree;
7565 if (!tree) {
7566 tree = NEW_NIL(&NULL_LOC);
7567 }
7568 else {
7569 rb_parser_ary_t *tokens = p->tokens;
7570 NODE *prelude;
7571 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7572 prelude = block_append(p, p->eval_tree_begin, body);
7573 RNODE_SCOPE(tree)->nd_body = prelude;
7574 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7575 p->ast->body.coverage_enabled = cov;
7576 if (p->keep_tokens) {
7577 p->ast->node_buffer->tokens = tokens;
7578 p->tokens = NULL;
7579 }
7580 }
7581 p->ast->body.root = tree;
7582 p->ast->body.line_count = p->line_count;
7583 return TRUE;
7584}
7585
7586static rb_ast_t *
7587yycompile(struct parser_params *p, VALUE fname, int line)
7588{
7589 rb_ast_t *ast;
7590 if (NIL_P(fname)) {
7591 p->ruby_sourcefile_string = Qnil;
7592 p->ruby_sourcefile = "(none)";
7593 }
7594 else {
7595 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7596 p->ruby_sourcefile = StringValueCStr(fname);
7597 }
7598 p->ruby_sourceline = line - 1;
7599
7600 p->lvtbl = NULL;
7601
7602 p->ast = ast = rb_ast_new();
7603 compile_callback(yycompile0, (VALUE)p);
7604 p->ast = 0;
7605
7606 while (p->lvtbl) {
7607 local_pop(p);
7608 }
7609
7610 return ast;
7611}
7612#endif /* !RIPPER */
7613
7614static rb_encoding *
7615must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7616{
7617 rb_encoding *enc = rb_parser_str_get_encoding(s);
7618 if (!rb_enc_asciicompat(enc)) {
7619 rb_raise(rb_eArgError, "invalid source encoding");
7620 }
7621 return enc;
7622}
7623
7624static rb_parser_string_t *
7625lex_getline(struct parser_params *p)
7626{
7627 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7628 if (!line) return 0;
7629 p->line_count++;
7630 string_buffer_append(p, line);
7631 must_be_ascii_compatible(p, line);
7632 return line;
7633}
7634
7635#ifndef RIPPER
7636rb_ast_t*
7637rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7638{
7639 p->lex.gets = gets;
7640 p->lex.input = input;
7641 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7642
7643 return yycompile(p, fname, line);
7644}
7645#endif /* !RIPPER */
7646
7647#define STR_FUNC_ESCAPE 0x01
7648#define STR_FUNC_EXPAND 0x02
7649#define STR_FUNC_REGEXP 0x04
7650#define STR_FUNC_QWORDS 0x08
7651#define STR_FUNC_SYMBOL 0x10
7652#define STR_FUNC_INDENT 0x20
7653#define STR_FUNC_LABEL 0x40
7654#define STR_FUNC_LIST 0x4000
7655#define STR_FUNC_TERM 0x8000
7656
7657enum string_type {
7658 str_label = STR_FUNC_LABEL,
7659 str_squote = (0),
7660 str_dquote = (STR_FUNC_EXPAND),
7661 str_xquote = (STR_FUNC_EXPAND),
7662 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7663 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7664 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7665 str_ssym = (STR_FUNC_SYMBOL),
7666 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7667};
7668
7669static rb_parser_string_t *
7670parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7671{
7672 rb_parser_string_t *pstr;
7673
7674 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7675
7676 if (!(func & STR_FUNC_REGEXP)) {
7677 if (rb_parser_is_ascii_string(p, pstr)) {
7678 }
7679 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7680 /* everything is valid in ASCII-8BIT */
7681 enc = rb_ascii8bit_encoding();
7682 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7683 }
7684 }
7685
7686 return pstr;
7687}
7688
7689static int
7690strterm_is_heredoc(rb_strterm_t *strterm)
7691{
7692 return strterm->heredoc;
7693}
7694
7695static rb_strterm_t *
7696new_strterm(struct parser_params *p, int func, int term, int paren)
7697{
7698 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7699 strterm->u.literal.func = func;
7700 strterm->u.literal.term = term;
7701 strterm->u.literal.paren = paren;
7702 return strterm;
7703}
7704
7705static rb_strterm_t *
7706new_heredoc(struct parser_params *p)
7707{
7708 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7709 strterm->heredoc = true;
7710 return strterm;
7711}
7712
7713#define peek(p,c) peek_n(p, (c), 0)
7714#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7715#define peekc(p) peekc_n(p, 0)
7716#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7717
7718#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7719static void
7720parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7721{
7722 debug_token_line(p, "add_delayed_token", line);
7723
7724 if (tok < end) {
7725 if (has_delayed_token(p)) {
7726 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7727 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7728 int end_col = (next_line ? 0 : p->delayed.end_col);
7729 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7730 dispatch_delayed_token(p, tSTRING_CONTENT);
7731 }
7732 }
7733 if (!has_delayed_token(p)) {
7734 p->delayed.token = rb_parser_string_new(p, 0, 0);
7735 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7736 p->delayed.beg_line = p->ruby_sourceline;
7737 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7738 }
7739 parser_str_cat(p->delayed.token, tok, end - tok);
7740 p->delayed.end_line = p->ruby_sourceline;
7741 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7742 p->lex.ptok = end;
7743 }
7744}
7745
7746static void
7747set_lastline(struct parser_params *p, rb_parser_string_t *str)
7748{
7749 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7750 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7751 p->lex.lastline = str;
7752}
7753
7754static int
7755nextline(struct parser_params *p, int set_encoding)
7756{
7757 rb_parser_string_t *str = p->lex.nextline;
7758 p->lex.nextline = 0;
7759 if (!str) {
7760 if (p->eofp)
7761 return -1;
7762
7763 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7764 goto end_of_input;
7765 }
7766
7767 if (!p->lex.input || !(str = lex_getline(p))) {
7768 end_of_input:
7769 p->eofp = 1;
7770 lex_goto_eol(p);
7771 return -1;
7772 }
7773#ifndef RIPPER
7774 if (p->debug_lines) {
7775 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7776 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7777 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7778 }
7779#endif
7780 p->cr_seen = FALSE;
7781 }
7782 else if (str == AFTER_HEREDOC_WITHOUT_TERMINTOR) {
7783 /* after here-document without terminator */
7784 goto end_of_input;
7785 }
7786 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7787 if (p->heredoc_end > 0) {
7788 p->ruby_sourceline = p->heredoc_end;
7789 p->heredoc_end = 0;
7790 }
7791 p->ruby_sourceline++;
7792 set_lastline(p, str);
7793 token_flush(p);
7794 return 0;
7795}
7796
7797static int
7798parser_cr(struct parser_params *p, int c)
7799{
7800 if (peek(p, '\n')) {
7801 p->lex.pcur++;
7802 c = '\n';
7803 }
7804 return c;
7805}
7806
7807static inline int
7808nextc0(struct parser_params *p, int set_encoding)
7809{
7810 int c;
7811
7812 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINTOR)) {
7813 if (nextline(p, set_encoding)) return -1;
7814 }
7815 c = (unsigned char)*p->lex.pcur++;
7816 if (UNLIKELY(c == '\r')) {
7817 c = parser_cr(p, c);
7818 }
7819
7820 return c;
7821}
7822#define nextc(p) nextc0(p, TRUE)
7823
7824static void
7825pushback(struct parser_params *p, int c)
7826{
7827 if (c == -1) return;
7828 p->eofp = 0;
7829 p->lex.pcur--;
7830 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7831 p->lex.pcur--;
7832 }
7833}
7834
7835#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7836
7837#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7838#define tok(p) (p)->tokenbuf
7839#define toklen(p) (p)->tokidx
7840
7841static int
7842looking_at_eol_p(struct parser_params *p)
7843{
7844 const char *ptr = p->lex.pcur;
7845 while (!lex_eol_ptr_p(p, ptr)) {
7846 int c = (unsigned char)*ptr++;
7847 int eol = (c == '\n' || c == '#');
7848 if (eol || !ISSPACE(c)) {
7849 return eol;
7850 }
7851 }
7852 return TRUE;
7853}
7854
7855static char*
7856newtok(struct parser_params *p)
7857{
7858 p->tokidx = 0;
7859 if (!p->tokenbuf) {
7860 p->toksiz = 60;
7861 p->tokenbuf = ALLOC_N(char, 60);
7862 }
7863 if (p->toksiz > 4096) {
7864 p->toksiz = 60;
7865 REALLOC_N(p->tokenbuf, char, 60);
7866 }
7867 return p->tokenbuf;
7868}
7869
7870static char *
7871tokspace(struct parser_params *p, int n)
7872{
7873 p->tokidx += n;
7874
7875 if (p->tokidx >= p->toksiz) {
7876 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7877 REALLOC_N(p->tokenbuf, char, p->toksiz);
7878 }
7879 return &p->tokenbuf[p->tokidx-n];
7880}
7881
7882static void
7883tokadd(struct parser_params *p, int c)
7884{
7885 p->tokenbuf[p->tokidx++] = (char)c;
7886 if (p->tokidx >= p->toksiz) {
7887 p->toksiz *= 2;
7888 REALLOC_N(p->tokenbuf, char, p->toksiz);
7889 }
7890}
7891
7892static int
7893tok_hex(struct parser_params *p, size_t *numlen)
7894{
7895 int c;
7896
7897 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7898 if (!*numlen) {
7899 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7900 yyerror0("invalid hex escape");
7901 dispatch_scan_event(p, tSTRING_CONTENT);
7902 return 0;
7903 }
7904 p->lex.pcur += *numlen;
7905 return c;
7906}
7907
7908#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7909
7910static int
7911escaped_control_code(int c)
7912{
7913 int c2 = 0;
7914 switch (c) {
7915 case ' ':
7916 c2 = 's';
7917 break;
7918 case '\n':
7919 c2 = 'n';
7920 break;
7921 case '\t':
7922 c2 = 't';
7923 break;
7924 case '\v':
7925 c2 = 'v';
7926 break;
7927 case '\r':
7928 c2 = 'r';
7929 break;
7930 case '\f':
7931 c2 = 'f';
7932 break;
7933 }
7934 return c2;
7935}
7936
7937#define WARN_SPACE_CHAR(c, prefix) \
7938 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7939
7940static int
7941tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7942 int regexp_literal, const char *begin)
7943{
7944 const int wide = !begin;
7945 size_t numlen;
7946 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7947
7948 p->lex.pcur += numlen;
7949 if (p->lex.strterm == NULL ||
7950 strterm_is_heredoc(p->lex.strterm) ||
7951 (p->lex.strterm->u.literal.func != str_regexp)) {
7952 if (!begin) begin = p->lex.pcur;
7953 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7954 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7955 yyerror0("invalid Unicode escape");
7956 dispatch_scan_event(p, tSTRING_CONTENT);
7957 return wide && numlen > 0;
7958 }
7959 if (codepoint > 0x10ffff) {
7960 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7961 yyerror0("invalid Unicode codepoint (too large)");
7962 dispatch_scan_event(p, tSTRING_CONTENT);
7963 return wide;
7964 }
7965 if ((codepoint & 0xfffff800) == 0xd800) {
7966 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7967 yyerror0("invalid Unicode codepoint");
7968 dispatch_scan_event(p, tSTRING_CONTENT);
7969 return wide;
7970 }
7971 }
7972 if (regexp_literal) {
7973 tokcopy(p, (int)numlen);
7974 }
7975 else if (codepoint >= 0x80) {
7976 rb_encoding *utf8 = rb_utf8_encoding();
7977 if (*encp && utf8 != *encp) {
7978 YYLTYPE loc = RUBY_INIT_YYLLOC();
7979 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7980 parser_show_error_line(p, &loc);
7981 return wide;
7982 }
7983 *encp = utf8;
7984 tokaddmbc(p, codepoint, *encp);
7985 }
7986 else {
7987 tokadd(p, codepoint);
7988 }
7989 return TRUE;
7990}
7991
7992static int tokadd_mbchar(struct parser_params *p, int c);
7993
7994static int
7995tokskip_mbchar(struct parser_params *p)
7996{
7997 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7998 if (len > 0) {
7999 p->lex.pcur += len - 1;
8000 }
8001 return len;
8002}
8003
8004/* return value is for ?\u3042 */
8005static void
8006tokadd_utf8(struct parser_params *p, rb_encoding **encp,
8007 int term, int symbol_literal, int regexp_literal)
8008{
8009 /*
8010 * If `term` is not -1, then we allow multiple codepoints in \u{}
8011 * upto `term` byte, otherwise we're parsing a character literal.
8012 * And then add the codepoints to the current token.
8013 */
8014 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
8015
8016 const int open_brace = '{', close_brace = '}';
8017
8018 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
8019
8020 if (peek(p, open_brace)) { /* handle \u{...} form */
8021 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
8022 /*
8023 * Skip parsing validation code and copy bytes as-is until term or
8024 * closing brace, in order to correctly handle extended regexps where
8025 * invalid unicode escapes are allowed in comments. The regexp parser
8026 * does its own validation and will catch any issues.
8027 */
8028 tokadd(p, open_brace);
8029 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
8030 int c = peekc(p);
8031 if (c == close_brace) {
8032 tokadd(p, c);
8033 ++p->lex.pcur;
8034 break;
8035 }
8036 else if (c == term) {
8037 break;
8038 }
8039 if (c == '\\' && !lex_eol_n_p(p, 1)) {
8040 tokadd(p, c);
8041 c = *++p->lex.pcur;
8042 }
8043 tokadd_mbchar(p, c);
8044 }
8045 }
8046 else {
8047 const char *second = NULL;
8048 int c, last = nextc(p);
8049 if (lex_eol_p(p)) goto unterminated;
8050 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
8051 while (c != close_brace) {
8052 if (c == term) goto unterminated;
8053 if (second == multiple_codepoints)
8054 second = p->lex.pcur;
8055 if (regexp_literal) tokadd(p, last);
8056 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
8057 break;
8058 }
8059 while (ISSPACE(c = peekc(p))) {
8060 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
8061 last = c;
8062 }
8063 if (term == -1 && !second)
8064 second = multiple_codepoints;
8065 }
8066
8067 if (c != close_brace) {
8068 unterminated:
8069 flush_string_content(p, rb_utf8_encoding(), 0);
8070 yyerror0("unterminated Unicode escape");
8071 dispatch_scan_event(p, tSTRING_CONTENT);
8072 return;
8073 }
8074 if (second && second != multiple_codepoints) {
8075 const char *pcur = p->lex.pcur;
8076 p->lex.pcur = second;
8077 dispatch_scan_event(p, tSTRING_CONTENT);
8078 token_flush(p);
8079 p->lex.pcur = pcur;
8080 yyerror0(multiple_codepoints);
8081 token_flush(p);
8082 }
8083
8084 if (regexp_literal) tokadd(p, close_brace);
8085 nextc(p);
8086 }
8087 }
8088 else { /* handle \uxxxx form */
8089 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
8090 token_flush(p);
8091 return;
8092 }
8093 }
8094}
8095
8096#define ESCAPE_CONTROL 1
8097#define ESCAPE_META 2
8098
8099static int
8100read_escape(struct parser_params *p, int flags, const char *begin)
8101{
8102 int c;
8103 size_t numlen;
8104
8105 switch (c = nextc(p)) {
8106 case '\\': /* Backslash */
8107 return c;
8108
8109 case 'n': /* newline */
8110 return '\n';
8111
8112 case 't': /* horizontal tab */
8113 return '\t';
8114
8115 case 'r': /* carriage-return */
8116 return '\r';
8117
8118 case 'f': /* form-feed */
8119 return '\f';
8120
8121 case 'v': /* vertical tab */
8122 return '\13';
8123
8124 case 'a': /* alarm(bell) */
8125 return '\007';
8126
8127 case 'e': /* escape */
8128 return 033;
8129
8130 case '0': case '1': case '2': case '3': /* octal constant */
8131 case '4': case '5': case '6': case '7':
8132 pushback(p, c);
8133 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8134 p->lex.pcur += numlen;
8135 return c;
8136
8137 case 'x': /* hex constant */
8138 c = tok_hex(p, &numlen);
8139 if (numlen == 0) return 0;
8140 return c;
8141
8142 case 'b': /* backspace */
8143 return '\010';
8144
8145 case 's': /* space */
8146 return ' ';
8147
8148 case 'M':
8149 if (flags & ESCAPE_META) goto eof;
8150 if ((c = nextc(p)) != '-') {
8151 goto eof;
8152 }
8153 if ((c = nextc(p)) == '\\') {
8154 switch (peekc(p)) {
8155 case 'u': case 'U':
8156 nextc(p);
8157 goto eof;
8158 }
8159 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8160 }
8161 else if (c == -1) goto eof;
8162 else if (!ISASCII(c)) {
8163 tokskip_mbchar(p);
8164 goto eof;
8165 }
8166 else {
8167 int c2 = escaped_control_code(c);
8168 if (c2) {
8169 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8170 WARN_SPACE_CHAR(c2, "\\M-");
8171 }
8172 else {
8173 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8174 }
8175 }
8176 else if (ISCNTRL(c)) goto eof;
8177 return ((c & 0xff) | 0x80);
8178 }
8179
8180 case 'C':
8181 if ((c = nextc(p)) != '-') {
8182 goto eof;
8183 }
8184 case 'c':
8185 if (flags & ESCAPE_CONTROL) goto eof;
8186 if ((c = nextc(p))== '\\') {
8187 switch (peekc(p)) {
8188 case 'u': case 'U':
8189 nextc(p);
8190 goto eof;
8191 }
8192 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8193 }
8194 else if (c == '?')
8195 return 0177;
8196 else if (c == -1) goto eof;
8197 else if (!ISASCII(c)) {
8198 tokskip_mbchar(p);
8199 goto eof;
8200 }
8201 else {
8202 int c2 = escaped_control_code(c);
8203 if (c2) {
8204 if (ISCNTRL(c)) {
8205 if (flags & ESCAPE_META) {
8206 WARN_SPACE_CHAR(c2, "\\M-");
8207 }
8208 else {
8209 WARN_SPACE_CHAR(c2, "");
8210 }
8211 }
8212 else {
8213 if (flags & ESCAPE_META) {
8214 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8215 }
8216 else {
8217 WARN_SPACE_CHAR(c2, "\\C-");
8218 }
8219 }
8220 }
8221 else if (ISCNTRL(c)) goto eof;
8222 }
8223 return c & 0x9f;
8224
8225 eof:
8226 case -1:
8227 flush_string_content(p, p->enc, p->lex.pcur - begin);
8228 yyerror0("Invalid escape character syntax");
8229 dispatch_scan_event(p, tSTRING_CONTENT);
8230 return '\0';
8231
8232 default:
8233 return c;
8234 }
8235}
8236
8237static void
8238tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8239{
8240 int len = rb_enc_codelen(c, enc);
8241 rb_enc_mbcput(c, tokspace(p, len), enc);
8242}
8243
8244static int
8245tokadd_escape(struct parser_params *p)
8246{
8247 int c;
8248 size_t numlen;
8249 const char *begin = p->lex.pcur;
8250
8251 switch (c = nextc(p)) {
8252 case '\n':
8253 return 0; /* just ignore */
8254
8255 case '0': case '1': case '2': case '3': /* octal constant */
8256 case '4': case '5': case '6': case '7':
8257 {
8258 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8259 if (numlen == 0) goto eof;
8260 p->lex.pcur += numlen;
8261 tokcopy(p, (int)numlen + 1);
8262 }
8263 return 0;
8264
8265 case 'x': /* hex constant */
8266 {
8267 tok_hex(p, &numlen);
8268 if (numlen == 0) return -1;
8269 tokcopy(p, (int)numlen + 2);
8270 }
8271 return 0;
8272
8273 eof:
8274 case -1:
8275 flush_string_content(p, p->enc, p->lex.pcur - begin);
8276 yyerror0("Invalid escape character syntax");
8277 token_flush(p);
8278 return -1;
8279
8280 default:
8281 tokadd(p, '\\');
8282 tokadd(p, c);
8283 }
8284 return 0;
8285}
8286
8287static int
8288char_to_option(int c)
8289{
8290 int val;
8291
8292 switch (c) {
8293 case 'i':
8294 val = RE_ONIG_OPTION_IGNORECASE;
8295 break;
8296 case 'x':
8297 val = RE_ONIG_OPTION_EXTEND;
8298 break;
8299 case 'm':
8300 val = RE_ONIG_OPTION_MULTILINE;
8301 break;
8302 default:
8303 val = 0;
8304 break;
8305 }
8306 return val;
8307}
8308
8309#define ARG_ENCODING_FIXED 16
8310#define ARG_ENCODING_NONE 32
8311#define ENC_ASCII8BIT 1
8312#define ENC_EUC_JP 2
8313#define ENC_Windows_31J 3
8314#define ENC_UTF8 4
8315
8316static int
8317char_to_option_kcode(int c, int *option, int *kcode)
8318{
8319 *option = 0;
8320
8321 switch (c) {
8322 case 'n':
8323 *kcode = ENC_ASCII8BIT;
8324 return (*option = ARG_ENCODING_NONE);
8325 case 'e':
8326 *kcode = ENC_EUC_JP;
8327 break;
8328 case 's':
8329 *kcode = ENC_Windows_31J;
8330 break;
8331 case 'u':
8332 *kcode = ENC_UTF8;
8333 break;
8334 default:
8335 *kcode = -1;
8336 return (*option = char_to_option(c));
8337 }
8338 *option = ARG_ENCODING_FIXED;
8339 return 1;
8340}
8341
8342static int
8343regx_options(struct parser_params *p)
8344{
8345 int kcode = 0;
8346 int kopt = 0;
8347 int options = 0;
8348 int c, opt, kc;
8349
8350 newtok(p);
8351 while (c = nextc(p), ISALPHA(c)) {
8352 if (c == 'o') {
8353 options |= RE_OPTION_ONCE;
8354 }
8355 else if (char_to_option_kcode(c, &opt, &kc)) {
8356 if (kc >= 0) {
8357 if (kc != ENC_ASCII8BIT) kcode = c;
8358 kopt = opt;
8359 }
8360 else {
8361 options |= opt;
8362 }
8363 }
8364 else {
8365 tokadd(p, c);
8366 }
8367 }
8368 options |= kopt;
8369 pushback(p, c);
8370 if (toklen(p)) {
8371 YYLTYPE loc = RUBY_INIT_YYLLOC();
8372 tokfix(p);
8373 compile_error(p, "unknown regexp option%s - %*s",
8374 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8375 parser_show_error_line(p, &loc);
8376 }
8377 return options | RE_OPTION_ENCODING(kcode);
8378}
8379
8380static int
8381tokadd_mbchar(struct parser_params *p, int c)
8382{
8383 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8384 if (len < 0) return -1;
8385 tokadd(p, c);
8386 p->lex.pcur += --len;
8387 if (len > 0) tokcopy(p, len);
8388 return c;
8389}
8390
8391static inline int
8392simple_re_meta(int c)
8393{
8394 switch (c) {
8395 case '$': case '*': case '+': case '.':
8396 case '?': case '^': case '|':
8397 case ')': case ']': case '}': case '>':
8398 return TRUE;
8399 default:
8400 return FALSE;
8401 }
8402}
8403
8404static int
8405parser_update_heredoc_indent(struct parser_params *p, int c)
8406{
8407 if (p->heredoc_line_indent == -1) {
8408 if (c == '\n') p->heredoc_line_indent = 0;
8409 }
8410 else {
8411 if (c == ' ') {
8412 p->heredoc_line_indent++;
8413 return TRUE;
8414 }
8415 else if (c == '\t') {
8416 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8417 p->heredoc_line_indent = w * TAB_WIDTH;
8418 return TRUE;
8419 }
8420 else if (c != '\n') {
8421 if (p->heredoc_indent > p->heredoc_line_indent) {
8422 p->heredoc_indent = p->heredoc_line_indent;
8423 }
8424 p->heredoc_line_indent = -1;
8425 }
8426 else {
8427 /* Whitespace only line has no indentation */
8428 p->heredoc_line_indent = 0;
8429 }
8430 }
8431 return FALSE;
8432}
8433
8434static void
8435parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8436{
8437 YYLTYPE loc = RUBY_INIT_YYLLOC();
8438 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8439 compile_error(p, "%s mixed within %s source", n1, n2);
8440 parser_show_error_line(p, &loc);
8441}
8442
8443static void
8444parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8445{
8446 const char *pos = p->lex.pcur;
8447 p->lex.pcur = beg;
8448 parser_mixed_error(p, enc1, enc2);
8449 p->lex.pcur = pos;
8450}
8451
8452static inline char
8453nibble_char_upper(unsigned int c)
8454{
8455 c &= 0xf;
8456 return c + (c < 10 ? '0' : 'A' - 10);
8457}
8458
8459static int
8460tokadd_string(struct parser_params *p,
8461 int func, int term, int paren, long *nest,
8462 rb_encoding **encp, rb_encoding **enc)
8463{
8464 int c;
8465 bool erred = false;
8466#ifdef RIPPER
8467 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8468 int top_of_line = FALSE;
8469#endif
8470
8471#define mixed_error(enc1, enc2) \
8472 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8473#define mixed_escape(beg, enc1, enc2) \
8474 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8475
8476 while ((c = nextc(p)) != -1) {
8477 if (p->heredoc_indent > 0) {
8478 parser_update_heredoc_indent(p, c);
8479 }
8480#ifdef RIPPER
8481 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8482 pushback(p, c);
8483 break;
8484 }
8485#endif
8486
8487 if (paren && c == paren) {
8488 ++*nest;
8489 }
8490 else if (c == term) {
8491 if (!nest || !*nest) {
8492 pushback(p, c);
8493 break;
8494 }
8495 --*nest;
8496 }
8497 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8498 unsigned char c2 = *p->lex.pcur;
8499 if (c2 == '$' || c2 == '@' || c2 == '{') {
8500 pushback(p, c);
8501 break;
8502 }
8503 }
8504 else if (c == '\\') {
8505 c = nextc(p);
8506 switch (c) {
8507 case '\n':
8508 if (func & STR_FUNC_QWORDS) break;
8509 if (func & STR_FUNC_EXPAND) {
8510 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8511 continue;
8512 if (c == term) {
8513 c = '\\';
8514 goto terminate;
8515 }
8516 }
8517 tokadd(p, '\\');
8518 break;
8519
8520 case '\\':
8521 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8522 break;
8523
8524 case 'u':
8525 if ((func & STR_FUNC_EXPAND) == 0) {
8526 tokadd(p, '\\');
8527 break;
8528 }
8529 tokadd_utf8(p, enc, term,
8530 func & STR_FUNC_SYMBOL,
8531 func & STR_FUNC_REGEXP);
8532 continue;
8533
8534 default:
8535 if (c == -1) return -1;
8536 if (!ISASCII(c)) {
8537 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8538 goto non_ascii;
8539 }
8540 if (func & STR_FUNC_REGEXP) {
8541 switch (c) {
8542 case 'c':
8543 case 'C':
8544 case 'M': {
8545 pushback(p, c);
8546 c = read_escape(p, 0, p->lex.pcur - 1);
8547
8548 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8549 *t++ = '\\';
8550 *t++ = 'x';
8551 *t++ = nibble_char_upper(c >> 4);
8552 *t++ = nibble_char_upper(c);
8553 continue;
8554 }
8555 }
8556
8557 if (c == term && !simple_re_meta(c)) {
8558 tokadd(p, c);
8559 continue;
8560 }
8561 pushback(p, c);
8562 if ((c = tokadd_escape(p)) < 0)
8563 return -1;
8564 if (*enc && *enc != *encp) {
8565 mixed_escape(p->lex.ptok+2, *enc, *encp);
8566 }
8567 continue;
8568 }
8569 else if (func & STR_FUNC_EXPAND) {
8570 pushback(p, c);
8571 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8572 c = read_escape(p, 0, p->lex.pcur - 1);
8573 }
8574 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8575 /* ignore backslashed spaces in %w */
8576 }
8577 else if (c != term && !(paren && c == paren)) {
8578 tokadd(p, '\\');
8579 pushback(p, c);
8580 continue;
8581 }
8582 }
8583 }
8584 else if (!parser_isascii(p)) {
8585 non_ascii:
8586 if (!*enc) {
8587 *enc = *encp;
8588 }
8589 else if (*enc != *encp) {
8590 mixed_error(*enc, *encp);
8591 continue;
8592 }
8593 if (tokadd_mbchar(p, c) == -1) return -1;
8594 continue;
8595 }
8596 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8597 pushback(p, c);
8598 break;
8599 }
8600 if (c & 0x80) {
8601 if (!*enc) {
8602 *enc = *encp;
8603 }
8604 else if (*enc != *encp) {
8605 mixed_error(*enc, *encp);
8606 continue;
8607 }
8608 }
8609 tokadd(p, c);
8610#ifdef RIPPER
8611 top_of_line = (c == '\n');
8612#endif
8613 }
8614 terminate:
8615 if (*enc) *encp = *enc;
8616 return c;
8617}
8618
8619#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8620
8621static void
8622flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8623{
8624 p->lex.pcur -= back;
8625 if (has_delayed_token(p)) {
8626 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8627 if (len > 0) {
8628 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8629 p->delayed.end_line = p->ruby_sourceline;
8630 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8631 }
8632 dispatch_delayed_token(p, tSTRING_CONTENT);
8633 p->lex.ptok = p->lex.pcur;
8634 }
8635 dispatch_scan_event(p, tSTRING_CONTENT);
8636 p->lex.pcur += back;
8637}
8638
8639/* this can be shared with ripper, since it's independent from struct
8640 * parser_params. */
8641#ifndef RIPPER
8642#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8643#define SPECIAL_PUNCT(idx) ( \
8644 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', 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('0', idx))
8650const uint_least32_t ruby_global_name_punct_bits[] = {
8651 SPECIAL_PUNCT(0),
8652 SPECIAL_PUNCT(1),
8653 SPECIAL_PUNCT(2),
8654};
8655#undef BIT
8656#undef SPECIAL_PUNCT
8657#endif
8658
8659static enum yytokentype
8660parser_peek_variable_name(struct parser_params *p)
8661{
8662 int c;
8663 const char *ptr = p->lex.pcur;
8664
8665 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8666 c = *ptr++;
8667 switch (c) {
8668 case '$':
8669 if ((c = *ptr) == '-') {
8670 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8671 c = *ptr;
8672 }
8673 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8674 return tSTRING_DVAR;
8675 }
8676 break;
8677 case '@':
8678 if ((c = *ptr) == '@') {
8679 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8680 c = *ptr;
8681 }
8682 break;
8683 case '{':
8684 p->lex.pcur = ptr;
8685 p->command_start = TRUE;
8686 yylval.state = p->lex.state;
8687 return tSTRING_DBEG;
8688 default:
8689 return 0;
8690 }
8691 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8692 return tSTRING_DVAR;
8693 return 0;
8694}
8695
8696#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8697#define IS_END() IS_lex_state(EXPR_END_ANY)
8698#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8699#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8700#define IS_LABEL_POSSIBLE() (\
8701 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8702 IS_ARG())
8703#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8704#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8705
8706static inline enum yytokentype
8707parser_string_term(struct parser_params *p, int func)
8708{
8709 xfree(p->lex.strterm);
8710 p->lex.strterm = 0;
8711 if (func & STR_FUNC_REGEXP) {
8712 set_yylval_num(regx_options(p));
8713 dispatch_scan_event(p, tREGEXP_END);
8714 SET_LEX_STATE(EXPR_END);
8715 return tREGEXP_END;
8716 }
8717 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8718 nextc(p);
8719 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8720 return tLABEL_END;
8721 }
8722 SET_LEX_STATE(EXPR_END);
8723 return tSTRING_END;
8724}
8725
8726static enum yytokentype
8727parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8728{
8729 int func = quote->func;
8730 int term = quote->term;
8731 int paren = quote->paren;
8732 int c, space = 0;
8733 rb_encoding *enc = p->enc;
8734 rb_encoding *base_enc = 0;
8735 rb_parser_string_t *lit;
8736
8737 if (func & STR_FUNC_TERM) {
8738 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8739 SET_LEX_STATE(EXPR_END);
8740 xfree(p->lex.strterm);
8741 p->lex.strterm = 0;
8742 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8743 }
8744 c = nextc(p);
8745 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8746 while (c != '\n' && ISSPACE(c = nextc(p)));
8747 space = 1;
8748 }
8749 if (func & STR_FUNC_LIST) {
8750 quote->func &= ~STR_FUNC_LIST;
8751 space = 1;
8752 }
8753 if (c == term && !quote->nest) {
8754 if (func & STR_FUNC_QWORDS) {
8755 quote->func |= STR_FUNC_TERM;
8756 pushback(p, c); /* dispatch the term at tSTRING_END */
8757 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8758 return ' ';
8759 }
8760 return parser_string_term(p, func);
8761 }
8762 if (space) {
8763 if (!ISSPACE(c)) pushback(p, c);
8764 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8765 return ' ';
8766 }
8767 newtok(p);
8768 if ((func & STR_FUNC_EXPAND) && c == '#') {
8769 enum yytokentype t = parser_peek_variable_name(p);
8770 if (t) return t;
8771 tokadd(p, '#');
8772 c = nextc(p);
8773 }
8774 pushback(p, c);
8775 if (tokadd_string(p, func, term, paren, &quote->nest,
8776 &enc, &base_enc) == -1) {
8777 if (p->eofp) {
8778#ifndef RIPPER
8779# define unterminated_literal(mesg) yyerror0(mesg)
8780#else
8781# define unterminated_literal(mesg) compile_error(p, mesg)
8782#endif
8783 literal_flush(p, p->lex.pcur);
8784 if (func & STR_FUNC_QWORDS) {
8785 /* no content to add, bailing out here */
8786 unterminated_literal("unterminated list meets end of file");
8787 xfree(p->lex.strterm);
8788 p->lex.strterm = 0;
8789 return tSTRING_END;
8790 }
8791 if (func & STR_FUNC_REGEXP) {
8792 unterminated_literal("unterminated regexp meets end of file");
8793 }
8794 else {
8795 unterminated_literal("unterminated string meets end of file");
8796 }
8797 quote->func |= STR_FUNC_TERM;
8798 }
8799 }
8800
8801 tokfix(p);
8802 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8803 set_yylval_str(lit);
8804 flush_string_content(p, enc, 0);
8805
8806 return tSTRING_CONTENT;
8807}
8808
8809static enum yytokentype
8810heredoc_identifier(struct parser_params *p)
8811{
8812 /*
8813 * term_len is length of `<<"END"` except `END`,
8814 * in this case term_len is 4 (<, <, " and ").
8815 */
8816 long len, offset = p->lex.pcur - p->lex.pbeg;
8817 int c = nextc(p), term, func = 0, quote = 0;
8818 enum yytokentype token = tSTRING_BEG;
8819 int indent = 0;
8820
8821 if (c == '-') {
8822 c = nextc(p);
8823 func = STR_FUNC_INDENT;
8824 offset++;
8825 }
8826 else if (c == '~') {
8827 c = nextc(p);
8828 func = STR_FUNC_INDENT;
8829 offset++;
8830 indent = INT_MAX;
8831 }
8832 switch (c) {
8833 case '\'':
8834 func |= str_squote; goto quoted;
8835 case '"':
8836 func |= str_dquote; goto quoted;
8837 case '`':
8838 token = tXSTRING_BEG;
8839 func |= str_xquote; goto quoted;
8840
8841 quoted:
8842 quote++;
8843 offset++;
8844 term = c;
8845 len = 0;
8846 while ((c = nextc(p)) != term) {
8847 if (c == -1 || c == '\r' || c == '\n') {
8848 yyerror0("unterminated here document identifier");
8849 return -1;
8850 }
8851 }
8852 break;
8853
8854 default:
8855 if (!parser_is_identchar(p)) {
8856 pushback(p, c);
8857 if (func & STR_FUNC_INDENT) {
8858 pushback(p, indent > 0 ? '~' : '-');
8859 }
8860 return 0;
8861 }
8862 func |= str_dquote;
8863 do {
8864 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8865 if (n < 0) return 0;
8866 p->lex.pcur += --n;
8867 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8868 pushback(p, c);
8869 break;
8870 }
8871
8872 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8873 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8874 yyerror0("too long here document identifier");
8875 dispatch_scan_event(p, tHEREDOC_BEG);
8876 lex_goto_eol(p);
8877
8878 p->lex.strterm = new_heredoc(p);
8879 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8880 here->offset = offset;
8881 here->sourceline = p->ruby_sourceline;
8882 here->length = (unsigned)len;
8883 here->quote = quote;
8884 here->func = func;
8885 here->lastline = p->lex.lastline;
8886
8887 token_flush(p);
8888 p->heredoc_indent = indent;
8889 p->heredoc_line_indent = 0;
8890 return token;
8891}
8892
8893static void
8894heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8895{
8896 rb_parser_string_t *line;
8897 rb_strterm_t *term = p->lex.strterm;
8898
8899 p->lex.strterm = 0;
8900 line = here->lastline;
8901 p->lex.lastline = line;
8902 p->lex.pbeg = PARSER_STRING_PTR(line);
8903 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8904 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8905 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8906 p->heredoc_end = p->ruby_sourceline;
8907 p->ruby_sourceline = (int)here->sourceline;
8908 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINTOR;
8909 p->eofp = 0;
8910 xfree(term);
8911}
8912
8913static int
8914dedent_string_column(const char *str, long len, int width)
8915{
8916 int i, col = 0;
8917
8918 for (i = 0; i < len && col < width; i++) {
8919 if (str[i] == ' ') {
8920 col++;
8921 }
8922 else if (str[i] == '\t') {
8923 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8924 if (n > width) break;
8925 col = n;
8926 }
8927 else {
8928 break;
8929 }
8930 }
8931
8932 return i;
8933}
8934
8935static int
8936dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8937{
8938 char *str;
8939 long len;
8940 int i;
8941
8942 len = PARSER_STRING_LEN(string);
8943 str = PARSER_STRING_PTR(string);
8944
8945 i = dedent_string_column(str, len, width);
8946 if (!i) return 0;
8947
8948 rb_parser_str_modify(string);
8949 str = PARSER_STRING_PTR(string);
8950 if (PARSER_STRING_LEN(string) != len)
8951 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8952 MEMMOVE(str, str + i, char, len - i);
8953 rb_parser_str_set_len(p, string, len - i);
8954 return i;
8955}
8956
8957static NODE *
8958heredoc_dedent(struct parser_params *p, NODE *root)
8959{
8960 NODE *node, *str_node, *prev_node;
8961 int indent = p->heredoc_indent;
8962 rb_parser_string_t *prev_lit = 0;
8963
8964 if (indent <= 0) return root;
8965 if (!root) return root;
8966
8967 prev_node = node = str_node = root;
8968 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8969
8970 while (str_node) {
8971 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8972 if (nd_fl_newline(str_node)) {
8973 dedent_string(p, lit, indent);
8974 }
8975 if (!prev_lit) {
8976 prev_lit = lit;
8977 }
8978 else if (!literal_concat0(p, prev_lit, lit)) {
8979 return 0;
8980 }
8981 else {
8982 NODE *end = RNODE_LIST(node)->as.nd_end;
8983 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8984 if (!node) {
8985 if (nd_type_p(prev_node, NODE_DSTR))
8986 nd_set_type(prev_node, NODE_STR);
8987 break;
8988 }
8989 RNODE_LIST(node)->as.nd_end = end;
8990 goto next_str;
8991 }
8992
8993 str_node = 0;
8994 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8995 next_str:
8996 if (!nd_type_p(node, NODE_LIST)) break;
8997 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8998 enum node_type type = nd_type(str_node);
8999 if (type == NODE_STR || type == NODE_DSTR) break;
9000 prev_lit = 0;
9001 str_node = 0;
9002 }
9003 }
9004 }
9005 return root;
9006}
9007
9008static int
9009whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
9010{
9011 const char *beg = p->lex.pbeg;
9012 const char *ptr = p->lex.pend;
9013
9014 if (ptr - beg < len) return FALSE;
9015 if (ptr > beg && ptr[-1] == '\n') {
9016 if (--ptr > beg && ptr[-1] == '\r') --ptr;
9017 if (ptr - beg < len) return FALSE;
9018 }
9019 if (strncmp(eos, ptr -= len, len)) return FALSE;
9020 if (indent) {
9021 while (beg < ptr && ISSPACE(*beg)) beg++;
9022 }
9023 return beg == ptr;
9024}
9025
9026static int
9027word_match_p(struct parser_params *p, const char *word, long len)
9028{
9029 if (strncmp(p->lex.pcur, word, len)) return 0;
9030 if (lex_eol_n_p(p, len)) return 1;
9031 int c = (unsigned char)p->lex.pcur[len];
9032 if (ISSPACE(c)) return 1;
9033 switch (c) {
9034 case '\0': case '\004': case '\032': return 1;
9035 }
9036 return 0;
9037}
9038
9039#define NUM_SUFFIX_R (1<<0)
9040#define NUM_SUFFIX_I (1<<1)
9041#define NUM_SUFFIX_ALL 3
9042
9043static int
9044number_literal_suffix(struct parser_params *p, int mask)
9045{
9046 int c, result = 0;
9047 const char *lastp = p->lex.pcur;
9048
9049 while ((c = nextc(p)) != -1) {
9050 if ((mask & NUM_SUFFIX_I) && c == 'i') {
9051 result |= (mask & NUM_SUFFIX_I);
9052 mask &= ~NUM_SUFFIX_I;
9053 /* r after i, rational of complex is disallowed */
9054 mask &= ~NUM_SUFFIX_R;
9055 continue;
9056 }
9057 if ((mask & NUM_SUFFIX_R) && c == 'r') {
9058 result |= (mask & NUM_SUFFIX_R);
9059 mask &= ~NUM_SUFFIX_R;
9060 continue;
9061 }
9062 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
9063 p->lex.pcur = lastp;
9064 literal_flush(p, p->lex.pcur);
9065 return 0;
9066 }
9067 pushback(p, c);
9068 break;
9069 }
9070 return result;
9071}
9072
9073static enum yytokentype
9074set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
9075{
9076 enum rb_numeric_type numeric_type = integer_literal;
9077
9078 if (type == tFLOAT) {
9079 numeric_type = float_literal;
9080 }
9081
9082 if (suffix & NUM_SUFFIX_R) {
9083 type = tRATIONAL;
9084 numeric_type = rational_literal;
9085 }
9086 if (suffix & NUM_SUFFIX_I) {
9087 type = tIMAGINARY;
9088 }
9089
9090 switch (type) {
9091 case tINTEGER:
9092 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
9093 break;
9094 case tFLOAT:
9095 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
9096 break;
9097 case tRATIONAL:
9098 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
9099 break;
9100 case tIMAGINARY:
9101 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
9102 (void)numeric_type; /* for ripper */
9103 break;
9104 default:
9105 rb_bug("unexpected token: %d", type);
9106 }
9107 SET_LEX_STATE(EXPR_END);
9108 return type;
9109}
9110
9111#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
9112static void
9113parser_dispatch_heredoc_end(struct parser_params *p, int line)
9114{
9115 if (has_delayed_token(p))
9116 dispatch_delayed_token(p, tSTRING_CONTENT);
9117
9118#ifdef RIPPER
9119 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
9120 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9121#else
9122 if (p->keep_tokens) {
9123 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9124 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9125 parser_append_tokens(p, str, tHEREDOC_END, line);
9126 }
9127#endif
9128
9129 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9130 lex_goto_eol(p);
9131 token_flush(p);
9132}
9133
9134static enum yytokentype
9135here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9136{
9137 int c, func, indent = 0;
9138 const char *eos, *ptr, *ptr_end;
9139 long len;
9140 rb_parser_string_t *str = 0;
9141 rb_encoding *enc = p->enc;
9142 rb_encoding *base_enc = 0;
9143 int bol;
9144#ifdef RIPPER
9145 VALUE s_value;
9146#endif
9147
9148 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9149 len = here->length;
9150 indent = (func = here->func) & STR_FUNC_INDENT;
9151
9152 if ((c = nextc(p)) == -1) {
9153 error:
9154#ifdef RIPPER
9155 if (!has_delayed_token(p)) {
9156 dispatch_scan_event(p, tSTRING_CONTENT);
9157 }
9158 else {
9159 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9160 if (!(func & STR_FUNC_REGEXP)) {
9161 int cr = ENC_CODERANGE_UNKNOWN;
9162 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9163 if (cr != ENC_CODERANGE_7BIT &&
9164 rb_is_usascii_enc(p->enc) &&
9165 enc != rb_utf8_encoding()) {
9166 enc = rb_ascii8bit_encoding();
9167 }
9168 }
9169 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9170 }
9171 dispatch_delayed_token(p, tSTRING_CONTENT);
9172 }
9173 lex_goto_eol(p);
9174#endif
9175 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9176 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9177 (int)len, eos);
9178 token_flush(p);
9179 SET_LEX_STATE(EXPR_END);
9180 return tSTRING_END;
9181 }
9182 bol = was_bol(p);
9183 if (!bol) {
9184 /* not beginning of line, cannot be the terminator */
9185 }
9186 else if (p->heredoc_line_indent == -1) {
9187 /* `heredoc_line_indent == -1` means
9188 * - "after an interpolation in the same line", or
9189 * - "in a continuing line"
9190 */
9191 p->heredoc_line_indent = 0;
9192 }
9193 else if (whole_match_p(p, eos, len, indent)) {
9194 dispatch_heredoc_end(p);
9195 restore:
9196 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9197 token_flush(p);
9198 SET_LEX_STATE(EXPR_END);
9199 return tSTRING_END;
9200 }
9201
9202 if (!(func & STR_FUNC_EXPAND)) {
9203 do {
9204 ptr = PARSER_STRING_PTR(p->lex.lastline);
9205 ptr_end = p->lex.pend;
9206 if (ptr_end > ptr) {
9207 switch (ptr_end[-1]) {
9208 case '\n':
9209 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9210 ptr_end++;
9211 break;
9212 }
9213 case '\r':
9214 --ptr_end;
9215 }
9216 }
9217
9218 if (p->heredoc_indent > 0) {
9219 long i = 0;
9220 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9221 i++;
9222 p->heredoc_line_indent = 0;
9223 }
9224
9225 if (str)
9226 parser_str_cat(str, ptr, ptr_end - ptr);
9227 else
9228 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9229 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9230 lex_goto_eol(p);
9231 if (p->heredoc_indent > 0) {
9232 goto flush_str;
9233 }
9234 if (nextc(p) == -1) {
9235 if (str) {
9236 rb_parser_string_free(p, str);
9237 str = 0;
9238 }
9239 goto error;
9240 }
9241 } while (!whole_match_p(p, eos, len, indent));
9242 }
9243 else {
9244 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9245 newtok(p);
9246 if (c == '#') {
9247 enum yytokentype t = parser_peek_variable_name(p);
9248 if (p->heredoc_line_indent != -1) {
9249 if (p->heredoc_indent > p->heredoc_line_indent) {
9250 p->heredoc_indent = p->heredoc_line_indent;
9251 }
9252 p->heredoc_line_indent = -1;
9253 }
9254 if (t) return t;
9255 tokadd(p, '#');
9256 c = nextc(p);
9257 }
9258 do {
9259 pushback(p, c);
9260 enc = p->enc;
9261 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9262 if (p->eofp) goto error;
9263 goto restore;
9264 }
9265 if (c != '\n') {
9266 if (c == '\\') p->heredoc_line_indent = -1;
9267 flush:
9268 str = STR_NEW3(tok(p), toklen(p), enc, func);
9269 flush_str:
9270 set_yylval_str(str);
9271#ifndef RIPPER
9272 if (bol) nd_set_fl_newline(yylval.node);
9273#endif
9274 flush_string_content(p, enc, 0);
9275 return tSTRING_CONTENT;
9276 }
9277 tokadd(p, nextc(p));
9278 if (p->heredoc_indent > 0) {
9279 lex_goto_eol(p);
9280 goto flush;
9281 }
9282 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9283 if ((c = nextc(p)) == -1) goto error;
9284 } while (!whole_match_p(p, eos, len, indent));
9285 str = STR_NEW3(tok(p), toklen(p), enc, func);
9286 }
9287 dispatch_heredoc_end(p);
9288 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9289 token_flush(p);
9290 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9291#ifdef RIPPER
9292 /* Preserve s_value for set_yylval_str */
9293 s_value = p->s_value;
9294#endif
9295 set_yylval_str(str);
9296#ifdef RIPPER
9297 set_parser_s_value(s_value);
9298#endif
9299
9300#ifndef RIPPER
9301 if (bol) nd_set_fl_newline(yylval.node);
9302#endif
9303 return tSTRING_CONTENT;
9304}
9305
9306#include "lex.c"
9307
9308static int
9309arg_ambiguous(struct parser_params *p, char c)
9310{
9311#ifndef RIPPER
9312 if (c == '/') {
9313 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after '%c' operator", WARN_I(c));
9314 }
9315 else {
9316 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9317 }
9318#else
9319 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9320#endif
9321 return TRUE;
9322}
9323
9324/* returns true value if formal argument error;
9325 * Qtrue, or error message if ripper */
9326static VALUE
9327formal_argument_error(struct parser_params *p, ID id)
9328{
9329 switch (id_type(id)) {
9330 case ID_LOCAL:
9331 break;
9332#ifndef RIPPER
9333# define ERR(mesg) (yyerror0(mesg), Qtrue)
9334#else
9335# define ERR(mesg) WARN_S(mesg)
9336#endif
9337 case ID_CONST:
9338 return ERR("formal argument cannot be a constant");
9339 case ID_INSTANCE:
9340 return ERR("formal argument cannot be an instance variable");
9341 case ID_GLOBAL:
9342 return ERR("formal argument cannot be a global variable");
9343 case ID_CLASS:
9344 return ERR("formal argument cannot be a class variable");
9345 default:
9346 return ERR("formal argument must be local variable");
9347#undef ERR
9348 }
9349 shadowing_lvar(p, id);
9350
9351 return Qfalse;
9352}
9353
9354static int
9355lvar_defined(struct parser_params *p, ID id)
9356{
9357 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9358}
9359
9360/* emacsen -*- hack */
9361static long
9362parser_encode_length(struct parser_params *p, const char *name, long len)
9363{
9364 long nlen;
9365
9366 if (len > 5 && name[nlen = len - 5] == '-') {
9367 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9368 return nlen;
9369 }
9370 if (len > 4 && name[nlen = len - 4] == '-') {
9371 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9372 return nlen;
9373 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9374 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9375 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9376 return nlen;
9377 }
9378 return len;
9379}
9380
9381static void
9382parser_set_encode(struct parser_params *p, const char *name)
9383{
9384 rb_encoding *enc;
9385 VALUE excargs[3];
9386 int idx = 0;
9387
9388 const char *wrong = 0;
9389 switch (*name) {
9390 case 'e': case 'E': wrong = "external"; break;
9391 case 'i': case 'I': wrong = "internal"; break;
9392 case 'f': case 'F': wrong = "filesystem"; break;
9393 case 'l': case 'L': wrong = "locale"; break;
9394 }
9395 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9396 idx = rb_enc_find_index(name);
9397 if (idx < 0) {
9398 unknown:
9399 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9400 error:
9401 excargs[0] = rb_eArgError;
9402 excargs[2] = rb_make_backtrace();
9403 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9404 VALUE exc = rb_make_exception(3, excargs);
9405 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9406
9407 rb_ast_free(p->ast);
9408 p->ast = NULL;
9409
9410 rb_exc_raise(exc);
9411 }
9412 enc = rb_enc_from_index(idx);
9413 if (!rb_enc_asciicompat(enc)) {
9414 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9415 goto error;
9416 }
9417 p->enc = enc;
9418#ifndef RIPPER
9419 if (p->debug_lines) {
9420 long i;
9421 for (i = 0; i < p->debug_lines->len; i++) {
9422 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9423 }
9424 }
9425#endif
9426}
9427
9428static bool
9429comment_at_top(struct parser_params *p)
9430{
9431 if (p->token_seen) return false;
9432 return (p->line_count == (p->has_shebang ? 2 : 1));
9433}
9434
9435typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9436typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9437
9438static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9439
9440static void
9441magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9442{
9443 if (!comment_at_top(p)) {
9444 return;
9445 }
9446 parser_set_encode(p, val);
9447}
9448
9449static int
9450parser_get_bool(struct parser_params *p, const char *name, const char *val)
9451{
9452 switch (*val) {
9453 case 't': case 'T':
9454 if (STRCASECMP(val, "true") == 0) {
9455 return TRUE;
9456 }
9457 break;
9458 case 'f': case 'F':
9459 if (STRCASECMP(val, "false") == 0) {
9460 return FALSE;
9461 }
9462 break;
9463 }
9464 return parser_invalid_pragma_value(p, name, val);
9465}
9466
9467static int
9468parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9469{
9470 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9471 return -1;
9472}
9473
9474static void
9475parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9476{
9477 int b = parser_get_bool(p, name, val);
9478 if (b >= 0) p->token_info_enabled = b;
9479}
9480
9481static void
9482parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9483{
9484 int b;
9485
9486 if (p->token_seen) {
9487 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9488 return;
9489 }
9490
9491 b = parser_get_bool(p, name, val);
9492 if (b < 0) return;
9493
9494 p->frozen_string_literal = b;
9495}
9496
9497static void
9498parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9499{
9500 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9501 if (*s == ' ' || *s == '\t') continue;
9502 if (*s == '#') break;
9503 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9504 return;
9505 }
9506
9507 switch (*val) {
9508 case 'n': case 'N':
9509 if (STRCASECMP(val, "none") == 0) {
9510 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9511 return;
9512 }
9513 break;
9514 case 'l': case 'L':
9515 if (STRCASECMP(val, "literal") == 0) {
9516 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9517 return;
9518 }
9519 break;
9520 case 'e': case 'E':
9521 if (STRCASECMP(val, "experimental_copy") == 0) {
9522 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9523 return;
9524 }
9525 if (STRCASECMP(val, "experimental_everything") == 0) {
9526 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9527 return;
9528 }
9529 break;
9530 }
9531 parser_invalid_pragma_value(p, name, val);
9532}
9533
9534# if WARN_PAST_SCOPE
9535static void
9536parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9537{
9538 int b = parser_get_bool(p, name, val);
9539 if (b >= 0) p->past_scope_enabled = b;
9540}
9541# endif
9542
9543struct magic_comment {
9544 const char *name;
9545 rb_magic_comment_setter_t func;
9546 rb_magic_comment_length_t length;
9547};
9548
9549static const struct magic_comment magic_comments[] = {
9550 {"coding", magic_comment_encoding, parser_encode_length},
9551 {"encoding", magic_comment_encoding, parser_encode_length},
9552 {"frozen_string_literal", parser_set_frozen_string_literal},
9553 {"shareable_constant_value", parser_set_shareable_constant_value},
9554 {"warn_indent", parser_set_token_info},
9555# if WARN_PAST_SCOPE
9556 {"warn_past_scope", parser_set_past_scope},
9557# endif
9558};
9559
9560static const char *
9561magic_comment_marker(const char *str, long len)
9562{
9563 long i = 2;
9564
9565 while (i < len) {
9566 switch (str[i]) {
9567 case '-':
9568 if (str[i-1] == '*' && str[i-2] == '-') {
9569 return str + i + 1;
9570 }
9571 i += 2;
9572 break;
9573 case '*':
9574 if (i + 1 >= len) return 0;
9575 if (str[i+1] != '-') {
9576 i += 4;
9577 }
9578 else if (str[i-1] != '-') {
9579 i += 2;
9580 }
9581 else {
9582 return str + i + 2;
9583 }
9584 break;
9585 default:
9586 i += 3;
9587 break;
9588 }
9589 }
9590 return 0;
9591}
9592
9593static int
9594parser_magic_comment(struct parser_params *p, const char *str, long len)
9595{
9596 int indicator = 0;
9597 VALUE name = 0, val = 0;
9598 const char *beg, *end, *vbeg, *vend;
9599#define str_copy(_s, _p, _n) ((_s) \
9600 ? (void)(rb_str_resize((_s), (_n)), \
9601 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9602 : (void)((_s) = STR_NEW((_p), (_n))))
9603
9604 if (len <= 7) return FALSE;
9605 if (!!(beg = magic_comment_marker(str, len))) {
9606 if (!(end = magic_comment_marker(beg, str + len - beg)))
9607 return FALSE;
9608 indicator = TRUE;
9609 str = beg;
9610 len = end - beg - 3;
9611 }
9612
9613 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9614 while (len > 0) {
9615 const struct magic_comment *mc = magic_comments;
9616 char *s;
9617 int i;
9618 long n = 0;
9619
9620 for (; len > 0 && *str; str++, --len) {
9621 switch (*str) {
9622 case '\'': case '"': case ':': case ';':
9623 continue;
9624 }
9625 if (!ISSPACE(*str)) break;
9626 }
9627 for (beg = str; len > 0; str++, --len) {
9628 switch (*str) {
9629 case '\'': case '"': case ':': case ';':
9630 break;
9631 default:
9632 if (ISSPACE(*str)) break;
9633 continue;
9634 }
9635 break;
9636 }
9637 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9638 if (!len) break;
9639 if (*str != ':') {
9640 if (!indicator) return FALSE;
9641 continue;
9642 }
9643
9644 do str++; while (--len > 0 && ISSPACE(*str));
9645 if (!len) break;
9646 const char *tok_beg = str;
9647 if (*str == '"') {
9648 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9649 if (*str == '\\') {
9650 --len;
9651 ++str;
9652 }
9653 }
9654 vend = str;
9655 if (len) {
9656 --len;
9657 ++str;
9658 }
9659 }
9660 else {
9661 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9662 vend = str;
9663 }
9664 const char *tok_end = str;
9665 if (indicator) {
9666 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9667 }
9668 else {
9669 while (len > 0 && (ISSPACE(*str))) --len, str++;
9670 if (len) return FALSE;
9671 }
9672
9673 n = end - beg;
9674 str_copy(name, beg, n);
9675 s = RSTRING_PTR(name);
9676 for (i = 0; i < n; ++i) {
9677 if (s[i] == '-') s[i] = '_';
9678 }
9679 do {
9680 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9681 n = vend - vbeg;
9682 if (mc->length) {
9683 n = (*mc->length)(p, vbeg, n);
9684 }
9685 str_copy(val, vbeg, n);
9686 p->lex.ptok = tok_beg;
9687 p->lex.pcur = tok_end;
9688 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9689 break;
9690 }
9691 } while (++mc < magic_comments + numberof(magic_comments));
9692#ifdef RIPPER
9693 str_copy(val, vbeg, vend - vbeg);
9694 dispatch2(magic_comment, name, val);
9695#endif
9696 }
9697
9698 return TRUE;
9699}
9700
9701static void
9702set_file_encoding(struct parser_params *p, const char *str, const char *send)
9703{
9704 int sep = 0;
9705 const char *beg = str;
9706 VALUE s;
9707
9708 for (;;) {
9709 if (send - str <= 6) return;
9710 switch (str[6]) {
9711 case 'C': case 'c': str += 6; continue;
9712 case 'O': case 'o': str += 5; continue;
9713 case 'D': case 'd': str += 4; continue;
9714 case 'I': case 'i': str += 3; continue;
9715 case 'N': case 'n': str += 2; continue;
9716 case 'G': case 'g': str += 1; continue;
9717 case '=': case ':':
9718 sep = 1;
9719 str += 6;
9720 break;
9721 default:
9722 str += 6;
9723 if (ISSPACE(*str)) break;
9724 continue;
9725 }
9726 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9727 sep = 0;
9728 }
9729 for (;;) {
9730 do {
9731 if (++str >= send) return;
9732 } while (ISSPACE(*str));
9733 if (sep) break;
9734 if (*str != '=' && *str != ':') return;
9735 sep = 1;
9736 str++;
9737 }
9738 beg = str;
9739 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9740 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9741 p->lex.ptok = beg;
9742 p->lex.pcur = str;
9743 parser_set_encode(p, RSTRING_PTR(s));
9744 rb_str_resize(s, 0);
9745}
9746
9747static void
9748parser_prepare(struct parser_params *p)
9749{
9750 int c = nextc0(p, FALSE);
9751 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9752 switch (c) {
9753 case '#':
9754 if (peek(p, '!')) p->has_shebang = 1;
9755 break;
9756 case 0xef: /* UTF-8 BOM marker */
9757 if (!lex_eol_n_p(p, 2) &&
9758 (unsigned char)p->lex.pcur[0] == 0xbb &&
9759 (unsigned char)p->lex.pcur[1] == 0xbf) {
9760 p->enc = rb_utf8_encoding();
9761 p->lex.pcur += 2;
9762#ifndef RIPPER
9763 if (p->debug_lines) {
9764 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9765 }
9766#endif
9767 p->lex.pbeg = p->lex.pcur;
9768 token_flush(p);
9769 return;
9770 }
9771 break;
9772 case -1: /* end of script. */
9773 return;
9774 }
9775 pushback(p, c);
9776 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9777}
9778
9779#ifndef RIPPER
9780#define ambiguous_operator(tok, op, syn) ( \
9781 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9782 rb_warning0("even though it seems like "syn""))
9783#else
9784#define ambiguous_operator(tok, op, syn) \
9785 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9786#endif
9787#define warn_balanced(tok, op, syn) ((void) \
9788 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9789 space_seen && !ISSPACE(c) && \
9790 (ambiguous_operator(tok, op, syn), 0)), \
9791 (enum yytokentype)(tok))
9792
9793static enum yytokentype
9794no_digits(struct parser_params *p)
9795{
9796 yyerror0("numeric literal without digits");
9797 if (peek(p, '_')) nextc(p);
9798 /* dummy 0, for tUMINUS_NUM at numeric */
9799 return set_number_literal(p, tINTEGER, 0, 10, 0);
9800}
9801
9802static enum yytokentype
9803parse_numeric(struct parser_params *p, int c)
9804{
9805 int is_float, seen_point, seen_e, nondigit;
9806 int suffix;
9807
9808 is_float = seen_point = seen_e = nondigit = 0;
9809 SET_LEX_STATE(EXPR_END);
9810 newtok(p);
9811 if (c == '-' || c == '+') {
9812 tokadd(p, c);
9813 c = nextc(p);
9814 }
9815 if (c == '0') {
9816 int start = toklen(p);
9817 c = nextc(p);
9818 if (c == 'x' || c == 'X') {
9819 /* hexadecimal */
9820 c = nextc(p);
9821 if (c != -1 && ISXDIGIT(c)) {
9822 do {
9823 if (c == '_') {
9824 if (nondigit) break;
9825 nondigit = c;
9826 continue;
9827 }
9828 if (!ISXDIGIT(c)) break;
9829 nondigit = 0;
9830 tokadd(p, c);
9831 } while ((c = nextc(p)) != -1);
9832 }
9833 pushback(p, c);
9834 tokfix(p);
9835 if (toklen(p) == start) {
9836 return no_digits(p);
9837 }
9838 else if (nondigit) goto trailing_uc;
9839 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9840 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9841 }
9842 if (c == 'b' || c == 'B') {
9843 /* binary */
9844 c = nextc(p);
9845 if (c == '0' || c == '1') {
9846 do {
9847 if (c == '_') {
9848 if (nondigit) break;
9849 nondigit = c;
9850 continue;
9851 }
9852 if (c != '0' && c != '1') break;
9853 nondigit = 0;
9854 tokadd(p, c);
9855 } while ((c = nextc(p)) != -1);
9856 }
9857 pushback(p, c);
9858 tokfix(p);
9859 if (toklen(p) == start) {
9860 return no_digits(p);
9861 }
9862 else if (nondigit) goto trailing_uc;
9863 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9864 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9865 }
9866 if (c == 'd' || c == 'D') {
9867 /* decimal */
9868 c = nextc(p);
9869 if (c != -1 && ISDIGIT(c)) {
9870 do {
9871 if (c == '_') {
9872 if (nondigit) break;
9873 nondigit = c;
9874 continue;
9875 }
9876 if (!ISDIGIT(c)) break;
9877 nondigit = 0;
9878 tokadd(p, c);
9879 } while ((c = nextc(p)) != -1);
9880 }
9881 pushback(p, c);
9882 tokfix(p);
9883 if (toklen(p) == start) {
9884 return no_digits(p);
9885 }
9886 else if (nondigit) goto trailing_uc;
9887 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9888 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9889 }
9890 if (c == '_') {
9891 /* 0_0 */
9892 goto octal_number;
9893 }
9894 if (c == 'o' || c == 'O') {
9895 /* prefixed octal */
9896 c = nextc(p);
9897 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9898 tokfix(p);
9899 return no_digits(p);
9900 }
9901 }
9902 if (c >= '0' && c <= '7') {
9903 /* octal */
9904 octal_number:
9905 do {
9906 if (c == '_') {
9907 if (nondigit) break;
9908 nondigit = c;
9909 continue;
9910 }
9911 if (c < '0' || c > '9') break;
9912 if (c > '7') goto invalid_octal;
9913 nondigit = 0;
9914 tokadd(p, c);
9915 } while ((c = nextc(p)) != -1);
9916 if (toklen(p) > start) {
9917 pushback(p, c);
9918 tokfix(p);
9919 if (nondigit) goto trailing_uc;
9920 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9921 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9922 }
9923 if (nondigit) {
9924 pushback(p, c);
9925 goto trailing_uc;
9926 }
9927 }
9928 if (c > '7' && c <= '9') {
9929 invalid_octal:
9930 yyerror0("Invalid octal digit");
9931 }
9932 else if (c == '.' || c == 'e' || c == 'E') {
9933 tokadd(p, '0');
9934 }
9935 else {
9936 pushback(p, c);
9937 tokfix(p);
9938 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9939 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9940 }
9941 }
9942
9943 for (;;) {
9944 switch (c) {
9945 case '0': case '1': case '2': case '3': case '4':
9946 case '5': case '6': case '7': case '8': case '9':
9947 nondigit = 0;
9948 tokadd(p, c);
9949 break;
9950
9951 case '.':
9952 if (nondigit) goto trailing_uc;
9953 if (seen_point || seen_e) {
9954 goto decode_num;
9955 }
9956 else {
9957 int c0 = nextc(p);
9958 if (c0 == -1 || !ISDIGIT(c0)) {
9959 pushback(p, c0);
9960 goto decode_num;
9961 }
9962 c = c0;
9963 }
9964 seen_point = toklen(p);
9965 tokadd(p, '.');
9966 tokadd(p, c);
9967 is_float++;
9968 nondigit = 0;
9969 break;
9970
9971 case 'e':
9972 case 'E':
9973 if (nondigit) {
9974 pushback(p, c);
9975 c = nondigit;
9976 goto decode_num;
9977 }
9978 if (seen_e) {
9979 goto decode_num;
9980 }
9981 nondigit = c;
9982 c = nextc(p);
9983 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9984 pushback(p, c);
9985 c = nondigit;
9986 nondigit = 0;
9987 goto decode_num;
9988 }
9989 tokadd(p, nondigit);
9990 seen_e++;
9991 is_float++;
9992 tokadd(p, c);
9993 nondigit = (c == '-' || c == '+') ? c : 0;
9994 break;
9995
9996 case '_': /* `_' in number just ignored */
9997 if (nondigit) goto decode_num;
9998 nondigit = c;
9999 break;
10000
10001 default:
10002 goto decode_num;
10003 }
10004 c = nextc(p);
10005 }
10006
10007 decode_num:
10008 pushback(p, c);
10009 if (nondigit) {
10010 trailing_uc:
10011 literal_flush(p, p->lex.pcur - 1);
10012 YYLTYPE loc = RUBY_INIT_YYLLOC();
10013 compile_error(p, "trailing '%c' in number", nondigit);
10014 parser_show_error_line(p, &loc);
10015 }
10016 tokfix(p);
10017 if (is_float) {
10018 enum yytokentype type = tFLOAT;
10019
10020 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
10021 if (suffix & NUM_SUFFIX_R) {
10022 type = tRATIONAL;
10023 }
10024 else {
10025 strtod(tok(p), 0);
10026 if (errno == ERANGE) {
10027 rb_warning1("Float %s out of range", WARN_S(tok(p)));
10028 errno = 0;
10029 }
10030 }
10031 return set_number_literal(p, type, suffix, 0, seen_point);
10032 }
10033 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
10034 return set_number_literal(p, tINTEGER, suffix, 10, 0);
10035}
10036
10037static enum yytokentype
10038parse_qmark(struct parser_params *p, int space_seen)
10039{
10040 rb_encoding *enc;
10041 register int c;
10042 rb_parser_string_t *lit;
10043
10044 if (IS_END()) {
10045 SET_LEX_STATE(EXPR_VALUE);
10046 return '?';
10047 }
10048 c = nextc(p);
10049 if (c == -1) {
10050 compile_error(p, "incomplete character syntax");
10051 return 0;
10052 }
10053 if (rb_enc_isspace(c, p->enc)) {
10054 if (!IS_ARG()) {
10055 int c2 = escaped_control_code(c);
10056 if (c2) {
10057 WARN_SPACE_CHAR(c2, "?");
10058 }
10059 }
10060 ternary:
10061 pushback(p, c);
10062 SET_LEX_STATE(EXPR_VALUE);
10063 return '?';
10064 }
10065 newtok(p);
10066 enc = p->enc;
10067 if (!parser_isascii(p)) {
10068 if (tokadd_mbchar(p, c) == -1) return 0;
10069 }
10070 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
10071 !lex_eol_p(p) && is_identchar(p, p->lex.pcur, p->lex.pend, p->enc)) {
10072 if (space_seen) {
10073 const char *start = p->lex.pcur - 1, *ptr = start;
10074 do {
10075 int n = parser_precise_mbclen(p, ptr);
10076 if (n < 0) return -1;
10077 ptr += n;
10078 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
10079 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
10080 " a conditional operator, put a space after '?'",
10081 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
10082 }
10083 goto ternary;
10084 }
10085 else if (c == '\\') {
10086 if (peek(p, 'u')) {
10087 nextc(p);
10088 enc = rb_utf8_encoding();
10089 tokadd_utf8(p, &enc, -1, 0, 0);
10090 }
10091 else if (!ISASCII(c = peekc(p)) && c != -1) {
10092 nextc(p);
10093 if (tokadd_mbchar(p, c) == -1) return 0;
10094 }
10095 else {
10096 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
10097 tokadd(p, c);
10098 }
10099 }
10100 else {
10101 tokadd(p, c);
10102 }
10103 tokfix(p);
10104 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
10105 set_yylval_str(lit);
10106 SET_LEX_STATE(EXPR_END);
10107 return tCHAR;
10108}
10109
10110static enum yytokentype
10111parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
10112{
10113 register int c;
10114 const char *ptok = p->lex.pcur;
10115
10116 if (IS_BEG()) {
10117 int term;
10118 int paren;
10119
10120 c = nextc(p);
10121 quotation:
10122 if (c == -1) goto unterminated;
10123 if (!ISALNUM(c)) {
10124 term = c;
10125 if (!ISASCII(c)) goto unknown;
10126 c = 'Q';
10127 }
10128 else {
10129 term = nextc(p);
10130 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10131 unknown:
10132 pushback(p, term);
10133 c = parser_precise_mbclen(p, p->lex.pcur);
10134 if (c < 0) return 0;
10135 p->lex.pcur += c;
10136 yyerror0("unknown type of %string");
10137 return 0;
10138 }
10139 }
10140 if (term == -1) {
10141 unterminated:
10142 compile_error(p, "unterminated quoted string meets end of file");
10143 return 0;
10144 }
10145 paren = term;
10146 if (term == '(') term = ')';
10147 else if (term == '[') term = ']';
10148 else if (term == '{') term = '}';
10149 else if (term == '<') term = '>';
10150 else paren = 0;
10151
10152 p->lex.ptok = ptok-1;
10153 switch (c) {
10154 case 'Q':
10155 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10156 return tSTRING_BEG;
10157
10158 case 'q':
10159 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10160 return tSTRING_BEG;
10161
10162 case 'W':
10163 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10164 return tWORDS_BEG;
10165
10166 case 'w':
10167 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10168 return tQWORDS_BEG;
10169
10170 case 'I':
10171 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10172 return tSYMBOLS_BEG;
10173
10174 case 'i':
10175 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10176 return tQSYMBOLS_BEG;
10177
10178 case 'x':
10179 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10180 return tXSTRING_BEG;
10181
10182 case 'r':
10183 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10184 return tREGEXP_BEG;
10185
10186 case 's':
10187 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10188 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10189 return tSYMBEG;
10190
10191 default:
10192 yyerror0("unknown type of %string");
10193 return 0;
10194 }
10195 }
10196 if ((c = nextc(p)) == '=') {
10197 set_yylval_id('%');
10198 SET_LEX_STATE(EXPR_BEG);
10199 return tOP_ASGN;
10200 }
10201 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10202 goto quotation;
10203 }
10204 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10205 pushback(p, c);
10206 return warn_balanced('%', "%%", "string literal");
10207}
10208
10209static int
10210tokadd_ident(struct parser_params *p, int c)
10211{
10212 do {
10213 if (tokadd_mbchar(p, c) == -1) return -1;
10214 c = nextc(p);
10215 } while (parser_is_identchar(p));
10216 pushback(p, c);
10217 return 0;
10218}
10219
10220static ID
10221tokenize_ident(struct parser_params *p)
10222{
10223 ID ident = TOK_INTERN();
10224
10225 set_yylval_name(ident);
10226
10227 return ident;
10228}
10229
10230static int
10231parse_numvar(struct parser_params *p)
10232{
10233 size_t len;
10234 int overflow;
10235 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10236 const unsigned long nth_ref_max =
10237 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10238 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10239 * turned into a Fixnum, in compile.c */
10240
10241 if (overflow || n > nth_ref_max) {
10242 /* compile_error()? */
10243 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10244 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10245 }
10246 else {
10247 return (int)n;
10248 }
10249}
10250
10251static enum yytokentype
10252parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10253{
10254 const char *ptr = p->lex.pcur;
10255 register int c;
10256
10257 SET_LEX_STATE(EXPR_END);
10258 p->lex.ptok = ptr - 1; /* from '$' */
10259 newtok(p);
10260 c = nextc(p);
10261 switch (c) {
10262 case '_': /* $_: last read line string */
10263 c = nextc(p);
10264 if (parser_is_identchar(p)) {
10265 tokadd(p, '$');
10266 tokadd(p, '_');
10267 break;
10268 }
10269 pushback(p, c);
10270 c = '_';
10271 /* fall through */
10272 case '~': /* $~: match-data */
10273 case '*': /* $*: argv */
10274 case '$': /* $$: pid */
10275 case '?': /* $?: last status */
10276 case '!': /* $!: error string */
10277 case '@': /* $@: error position */
10278 case '/': /* $/: input record separator */
10279 case '\\': /* $\: output record separator */
10280 case ';': /* $;: field separator */
10281 case ',': /* $,: output field separator */
10282 case '.': /* $.: last read line number */
10283 case '=': /* $=: ignorecase */
10284 case ':': /* $:: load path */
10285 case '<': /* $<: default input handle */
10286 case '>': /* $>: default output handle */
10287 case '\"': /* $": already loaded files */
10288 tokadd(p, '$');
10289 tokadd(p, c);
10290 goto gvar;
10291
10292 case '-':
10293 tokadd(p, '$');
10294 tokadd(p, c);
10295 c = nextc(p);
10296 if (parser_is_identchar(p)) {
10297 if (tokadd_mbchar(p, c) == -1) return 0;
10298 }
10299 else {
10300 pushback(p, c);
10301 pushback(p, '-');
10302 return '$';
10303 }
10304 gvar:
10305 tokenize_ident(p);
10306 return tGVAR;
10307
10308 case '&': /* $&: last match */
10309 case '`': /* $`: string before last match */
10310 case '\'': /* $': string after last match */
10311 case '+': /* $+: string matches last paren. */
10312 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10313 tokadd(p, '$');
10314 tokadd(p, c);
10315 goto gvar;
10316 }
10317 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10318 return tBACK_REF;
10319
10320 case '1': case '2': case '3':
10321 case '4': case '5': case '6':
10322 case '7': case '8': case '9':
10323 tokadd(p, '$');
10324 do {
10325 tokadd(p, c);
10326 c = nextc(p);
10327 } while (c != -1 && ISDIGIT(c));
10328 pushback(p, c);
10329 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10330 tokfix(p);
10331 c = parse_numvar(p);
10332 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10333 return tNTH_REF;
10334
10335 default:
10336 if (!parser_is_identchar(p)) {
10337 YYLTYPE loc = RUBY_INIT_YYLLOC();
10338 if (c == -1 || ISSPACE(c)) {
10339 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10340 }
10341 else {
10342 pushback(p, c);
10343 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10344 }
10345 parser_show_error_line(p, &loc);
10346 set_yylval_noname();
10347 return tGVAR;
10348 }
10349 /* fall through */
10350 case '0':
10351 tokadd(p, '$');
10352 }
10353
10354 if (tokadd_ident(p, c)) return 0;
10355 SET_LEX_STATE(EXPR_END);
10356 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10357 tokenize_ident(p);
10358 }
10359 else {
10360 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10361 set_yylval_noname();
10362 }
10363 return tGVAR;
10364}
10365
10366static bool
10367parser_numbered_param(struct parser_params *p, int n)
10368{
10369 if (n < 0) return false;
10370
10371 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10372 return false;
10373 }
10374 if (p->max_numparam == ORDINAL_PARAM) {
10375 compile_error(p, "ordinary parameter is defined");
10376 return false;
10377 }
10378 struct vtable *args = p->lvtbl->args;
10379 if (p->max_numparam < n) {
10380 p->max_numparam = n;
10381 }
10382 while (n > args->pos) {
10383 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10384 }
10385 return true;
10386}
10387
10388static enum yytokentype
10389parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10390{
10391 const char *ptr = p->lex.pcur;
10392 enum yytokentype result = tIVAR;
10393 register int c = nextc(p);
10394 YYLTYPE loc;
10395
10396 p->lex.ptok = ptr - 1; /* from '@' */
10397 newtok(p);
10398 tokadd(p, '@');
10399 if (c == '@') {
10400 result = tCVAR;
10401 tokadd(p, '@');
10402 c = nextc(p);
10403 }
10404 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10405 if (c == -1 || !parser_is_identchar(p)) {
10406 pushback(p, c);
10407 RUBY_SET_YYLLOC(loc);
10408 if (result == tIVAR) {
10409 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10410 }
10411 else {
10412 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10413 }
10414 parser_show_error_line(p, &loc);
10415 set_yylval_noname();
10416 SET_LEX_STATE(EXPR_END);
10417 return result;
10418 }
10419 else if (ISDIGIT(c)) {
10420 pushback(p, c);
10421 RUBY_SET_YYLLOC(loc);
10422 if (result == tIVAR) {
10423 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10424 }
10425 else {
10426 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10427 }
10428 parser_show_error_line(p, &loc);
10429 set_yylval_noname();
10430 SET_LEX_STATE(EXPR_END);
10431 return result;
10432 }
10433
10434 if (tokadd_ident(p, c)) return 0;
10435 tokenize_ident(p);
10436 return result;
10437}
10438
10439static enum yytokentype
10440parse_ident(struct parser_params *p, int c, int cmd_state)
10441{
10442 enum yytokentype result;
10443 bool is_ascii = true;
10444 const enum lex_state_e last_state = p->lex.state;
10445 ID ident;
10446 int enforce_keyword_end = 0;
10447
10448 do {
10449 if (!ISASCII(c)) is_ascii = false;
10450 if (tokadd_mbchar(p, c) == -1) return 0;
10451 c = nextc(p);
10452 } while (parser_is_identchar(p));
10453 if ((c == '!' || c == '?') && !peek(p, '=')) {
10454 result = tFID;
10455 tokadd(p, c);
10456 }
10457 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10458 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10459 result = tIDENTIFIER;
10460 tokadd(p, c);
10461 }
10462 else {
10463 result = tCONSTANT; /* assume provisionally */
10464 pushback(p, c);
10465 }
10466 tokfix(p);
10467
10468 if (IS_LABEL_POSSIBLE()) {
10469 if (IS_LABEL_SUFFIX(0)) {
10470 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10471 nextc(p);
10472 tokenize_ident(p);
10473 return tLABEL;
10474 }
10475 }
10476
10477#ifndef RIPPER
10478 if (peek_end_expect_token_locations(p)) {
10479 const rb_code_position_t *end_pos;
10480 int lineno, column;
10481 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10482
10483 end_pos = peek_end_expect_token_locations(p)->pos;
10484 lineno = end_pos->lineno;
10485 column = end_pos->column;
10486
10487 if (p->debug) {
10488 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10489 p->ruby_sourceline, beg_pos, lineno, column);
10490 }
10491
10492 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10493 const struct kwtable *kw;
10494
10495 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10496 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10497 enforce_keyword_end = 1;
10498 }
10499 }
10500 }
10501#endif
10502
10503 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10504 const struct kwtable *kw;
10505
10506 /* See if it is a reserved word. */
10507 kw = rb_reserved_word(tok(p), toklen(p));
10508 if (kw) {
10509 enum lex_state_e state = p->lex.state;
10510 if (IS_lex_state_for(state, EXPR_FNAME)) {
10511 SET_LEX_STATE(EXPR_ENDFN);
10512 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10513 return kw->id[0];
10514 }
10515 SET_LEX_STATE(kw->state);
10516 if (IS_lex_state(EXPR_BEG)) {
10517 p->command_start = TRUE;
10518 }
10519 if (kw->id[0] == keyword_do) {
10520 if (lambda_beginning_p()) {
10521 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10522 return keyword_do_LAMBDA;
10523 }
10524 if (COND_P()) return keyword_do_cond;
10525 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10526 return keyword_do_block;
10527 return keyword_do;
10528 }
10529 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10530 return kw->id[0];
10531 else {
10532 if (kw->id[0] != kw->id[1])
10533 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10534 return kw->id[1];
10535 }
10536 }
10537 }
10538
10539 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10540 if (cmd_state) {
10541 SET_LEX_STATE(EXPR_CMDARG);
10542 }
10543 else {
10544 SET_LEX_STATE(EXPR_ARG);
10545 }
10546 }
10547 else if (p->lex.state == EXPR_FNAME) {
10548 SET_LEX_STATE(EXPR_ENDFN);
10549 }
10550 else {
10551 SET_LEX_STATE(EXPR_END);
10552 }
10553
10554 ident = tokenize_ident(p);
10555 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10556 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10557 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10558 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10559 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10560 }
10561 return result;
10562}
10563
10564static void
10565warn_cr(struct parser_params *p)
10566{
10567 if (!p->cr_seen) {
10568 p->cr_seen = TRUE;
10569 /* carried over with p->lex.nextline for nextc() */
10570 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10571 }
10572}
10573
10574static enum yytokentype
10575parser_yylex(struct parser_params *p)
10576{
10577 register int c;
10578 int space_seen = 0;
10579 int cmd_state;
10580 int label;
10581 enum lex_state_e last_state;
10582 int fallthru = FALSE;
10583 int token_seen = p->token_seen;
10584
10585 if (p->lex.strterm) {
10586 if (strterm_is_heredoc(p->lex.strterm)) {
10587 token_flush(p);
10588 return here_document(p, &p->lex.strterm->u.heredoc);
10589 }
10590 else {
10591 token_flush(p);
10592 return parse_string(p, &p->lex.strterm->u.literal);
10593 }
10594 }
10595 cmd_state = p->command_start;
10596 p->command_start = FALSE;
10597 p->token_seen = TRUE;
10598#ifndef RIPPER
10599 token_flush(p);
10600#endif
10601 retry:
10602 last_state = p->lex.state;
10603 switch (c = nextc(p)) {
10604 case '\0': /* NUL */
10605 case '\004': /* ^D */
10606 case '\032': /* ^Z */
10607 case -1: /* end of script. */
10608 p->eofp = 1;
10609#ifndef RIPPER
10610 if (p->end_expect_token_locations) {
10611 pop_end_expect_token_locations(p);
10612 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10613 return tDUMNY_END;
10614 }
10615#endif
10616 /* Set location for end-of-input because dispatch_scan_event is not called. */
10617 RUBY_SET_YYLLOC(*p->yylloc);
10618 return END_OF_INPUT;
10619
10620 /* white spaces */
10621 case '\r':
10622 warn_cr(p);
10623 /* fall through */
10624 case ' ': case '\t': case '\f':
10625 case '\13': /* '\v' */
10626 space_seen = 1;
10627 while ((c = nextc(p))) {
10628 switch (c) {
10629 case '\r':
10630 warn_cr(p);
10631 /* fall through */
10632 case ' ': case '\t': case '\f':
10633 case '\13': /* '\v' */
10634 break;
10635 default:
10636 goto outofloop;
10637 }
10638 }
10639 outofloop:
10640 pushback(p, c);
10641 dispatch_scan_event(p, tSP);
10642#ifndef RIPPER
10643 token_flush(p);
10644#endif
10645 goto retry;
10646
10647 case '#': /* it's a comment */
10648 p->token_seen = token_seen;
10649 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10650 /* no magic_comment in shebang line */
10651 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10652 if (comment_at_top(p)) {
10653 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10654 }
10655 }
10656 p->lex.pcur = pcur, p->lex.ptok = ptok;
10657 lex_goto_eol(p);
10658 dispatch_scan_event(p, tCOMMENT);
10659 fallthru = TRUE;
10660 /* fall through */
10661 case '\n':
10662 p->token_seen = token_seen;
10663 rb_parser_string_t *prevline = p->lex.lastline;
10664 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10665 !IS_lex_state(EXPR_LABELED));
10666 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10667 if (!fallthru) {
10668 dispatch_scan_event(p, tIGNORED_NL);
10669 }
10670 fallthru = FALSE;
10671 if (!c && p->ctxt.in_kwarg) {
10672 goto normal_newline;
10673 }
10674 goto retry;
10675 }
10676 while (1) {
10677 switch (c = nextc(p)) {
10678 case ' ': case '\t': case '\f': case '\r':
10679 case '\13': /* '\v' */
10680 space_seen = 1;
10681 break;
10682 case '#':
10683 pushback(p, c);
10684 if (space_seen) {
10685 dispatch_scan_event(p, tSP);
10686 token_flush(p);
10687 }
10688 goto retry;
10689 case '&':
10690 case '.': {
10691 dispatch_delayed_token(p, tIGNORED_NL);
10692 if (peek(p, '.') == (c == '&')) {
10693 pushback(p, c);
10694 dispatch_scan_event(p, tSP);
10695 goto retry;
10696 }
10697 }
10698 default:
10699 p->ruby_sourceline--;
10700 p->lex.nextline = p->lex.lastline;
10701 set_lastline(p, prevline);
10702 case -1: /* EOF no decrement*/
10703 if (c == -1 && space_seen) {
10704 dispatch_scan_event(p, tSP);
10705 }
10706 lex_goto_eol(p);
10707 if (c != -1) {
10708 token_flush(p);
10709 RUBY_SET_YYLLOC(*p->yylloc);
10710 }
10711 goto normal_newline;
10712 }
10713 }
10714 normal_newline:
10715 p->command_start = TRUE;
10716 SET_LEX_STATE(EXPR_BEG);
10717 return '\n';
10718
10719 case '*':
10720 if ((c = nextc(p)) == '*') {
10721 if ((c = nextc(p)) == '=') {
10722 set_yylval_id(idPow);
10723 SET_LEX_STATE(EXPR_BEG);
10724 return tOP_ASGN;
10725 }
10726 pushback(p, c);
10727 if (IS_SPCARG(c)) {
10728 rb_warning0("'**' interpreted as argument prefix");
10729 c = tDSTAR;
10730 }
10731 else if (IS_BEG()) {
10732 c = tDSTAR;
10733 }
10734 else {
10735 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10736 }
10737 }
10738 else {
10739 if (c == '=') {
10740 set_yylval_id('*');
10741 SET_LEX_STATE(EXPR_BEG);
10742 return tOP_ASGN;
10743 }
10744 pushback(p, c);
10745 if (IS_SPCARG(c)) {
10746 rb_warning0("'*' interpreted as argument prefix");
10747 c = tSTAR;
10748 }
10749 else if (IS_BEG()) {
10750 c = tSTAR;
10751 }
10752 else {
10753 c = warn_balanced('*', "*", "argument prefix");
10754 }
10755 }
10756 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10757 return c;
10758
10759 case '!':
10760 c = nextc(p);
10761 if (IS_AFTER_OPERATOR()) {
10762 SET_LEX_STATE(EXPR_ARG);
10763 if (c == '@') {
10764 return '!';
10765 }
10766 }
10767 else {
10768 SET_LEX_STATE(EXPR_BEG);
10769 }
10770 if (c == '=') {
10771 return tNEQ;
10772 }
10773 if (c == '~') {
10774 return tNMATCH;
10775 }
10776 pushback(p, c);
10777 return '!';
10778
10779 case '=':
10780 if (was_bol(p)) {
10781 /* skip embedded rd document */
10782 if (word_match_p(p, "begin", 5)) {
10783 int first_p = TRUE;
10784
10785 lex_goto_eol(p);
10786 dispatch_scan_event(p, tEMBDOC_BEG);
10787 for (;;) {
10788 lex_goto_eol(p);
10789 if (!first_p) {
10790 dispatch_scan_event(p, tEMBDOC);
10791 }
10792 first_p = FALSE;
10793 c = nextc(p);
10794 if (c == -1) {
10795 compile_error(p, "embedded document meets end of file");
10796 return END_OF_INPUT;
10797 }
10798 if (c == '=' && word_match_p(p, "end", 3)) {
10799 break;
10800 }
10801 pushback(p, c);
10802 }
10803 lex_goto_eol(p);
10804 dispatch_scan_event(p, tEMBDOC_END);
10805 goto retry;
10806 }
10807 }
10808
10809 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10810 if ((c = nextc(p)) == '=') {
10811 if ((c = nextc(p)) == '=') {
10812 return tEQQ;
10813 }
10814 pushback(p, c);
10815 return tEQ;
10816 }
10817 if (c == '~') {
10818 return tMATCH;
10819 }
10820 else if (c == '>') {
10821 return tASSOC;
10822 }
10823 pushback(p, c);
10824 return '=';
10825
10826 case '<':
10827 c = nextc(p);
10828 if (c == '<' &&
10829 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10830 !IS_END() &&
10831 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10832 enum yytokentype token = heredoc_identifier(p);
10833 if (token) return token < 0 ? 0 : token;
10834 }
10835 if (IS_AFTER_OPERATOR()) {
10836 SET_LEX_STATE(EXPR_ARG);
10837 }
10838 else {
10839 if (IS_lex_state(EXPR_CLASS))
10840 p->command_start = TRUE;
10841 SET_LEX_STATE(EXPR_BEG);
10842 }
10843 if (c == '=') {
10844 if ((c = nextc(p)) == '>') {
10845 return tCMP;
10846 }
10847 pushback(p, c);
10848 return tLEQ;
10849 }
10850 if (c == '<') {
10851 if ((c = nextc(p)) == '=') {
10852 set_yylval_id(idLTLT);
10853 SET_LEX_STATE(EXPR_BEG);
10854 return tOP_ASGN;
10855 }
10856 pushback(p, c);
10857 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10858 }
10859 pushback(p, c);
10860 return '<';
10861
10862 case '>':
10863 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10864 if ((c = nextc(p)) == '=') {
10865 return tGEQ;
10866 }
10867 if (c == '>') {
10868 if ((c = nextc(p)) == '=') {
10869 set_yylval_id(idGTGT);
10870 SET_LEX_STATE(EXPR_BEG);
10871 return tOP_ASGN;
10872 }
10873 pushback(p, c);
10874 return tRSHFT;
10875 }
10876 pushback(p, c);
10877 return '>';
10878
10879 case '"':
10880 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10881 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10882 p->lex.ptok = p->lex.pcur-1;
10883 return tSTRING_BEG;
10884
10885 case '`':
10886 if (IS_lex_state(EXPR_FNAME)) {
10887 SET_LEX_STATE(EXPR_ENDFN);
10888 return c;
10889 }
10890 if (IS_lex_state(EXPR_DOT)) {
10891 if (cmd_state)
10892 SET_LEX_STATE(EXPR_CMDARG);
10893 else
10894 SET_LEX_STATE(EXPR_ARG);
10895 return c;
10896 }
10897 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10898 return tXSTRING_BEG;
10899
10900 case '\'':
10901 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10902 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10903 p->lex.ptok = p->lex.pcur-1;
10904 return tSTRING_BEG;
10905
10906 case '?':
10907 return parse_qmark(p, space_seen);
10908
10909 case '&':
10910 if ((c = nextc(p)) == '&') {
10911 SET_LEX_STATE(EXPR_BEG);
10912 if ((c = nextc(p)) == '=') {
10913 set_yylval_id(idANDOP);
10914 SET_LEX_STATE(EXPR_BEG);
10915 return tOP_ASGN;
10916 }
10917 pushback(p, c);
10918 return tANDOP;
10919 }
10920 else if (c == '=') {
10921 set_yylval_id('&');
10922 SET_LEX_STATE(EXPR_BEG);
10923 return tOP_ASGN;
10924 }
10925 else if (c == '.') {
10926 set_yylval_id(idANDDOT);
10927 SET_LEX_STATE(EXPR_DOT);
10928 return tANDDOT;
10929 }
10930 pushback(p, c);
10931 if (IS_SPCARG(c)) {
10932 if ((c != ':') ||
10933 (c = peekc_n(p, 1)) == -1 ||
10934 !(c == '\'' || c == '"' ||
10935 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10936 rb_warning0("'&' interpreted as argument prefix");
10937 }
10938 c = tAMPER;
10939 }
10940 else if (IS_BEG()) {
10941 c = tAMPER;
10942 }
10943 else {
10944 c = warn_balanced('&', "&", "argument prefix");
10945 }
10946 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10947 return c;
10948
10949 case '|':
10950 if ((c = nextc(p)) == '|') {
10951 SET_LEX_STATE(EXPR_BEG);
10952 if ((c = nextc(p)) == '=') {
10953 set_yylval_id(idOROP);
10954 SET_LEX_STATE(EXPR_BEG);
10955 return tOP_ASGN;
10956 }
10957 pushback(p, c);
10958 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10959 c = '|';
10960 pushback(p, '|');
10961 return c;
10962 }
10963 return tOROP;
10964 }
10965 if (c == '=') {
10966 set_yylval_id('|');
10967 SET_LEX_STATE(EXPR_BEG);
10968 return tOP_ASGN;
10969 }
10970 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10971 pushback(p, c);
10972 return '|';
10973
10974 case '+':
10975 c = nextc(p);
10976 if (IS_AFTER_OPERATOR()) {
10977 SET_LEX_STATE(EXPR_ARG);
10978 if (c == '@') {
10979 return tUPLUS;
10980 }
10981 pushback(p, c);
10982 return '+';
10983 }
10984 if (c == '=') {
10985 set_yylval_id('+');
10986 SET_LEX_STATE(EXPR_BEG);
10987 return tOP_ASGN;
10988 }
10989 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10990 SET_LEX_STATE(EXPR_BEG);
10991 pushback(p, c);
10992 if (c != -1 && ISDIGIT(c)) {
10993 return parse_numeric(p, '+');
10994 }
10995 return tUPLUS;
10996 }
10997 SET_LEX_STATE(EXPR_BEG);
10998 pushback(p, c);
10999 return warn_balanced('+', "+", "unary operator");
11000
11001 case '-':
11002 c = nextc(p);
11003 if (IS_AFTER_OPERATOR()) {
11004 SET_LEX_STATE(EXPR_ARG);
11005 if (c == '@') {
11006 return tUMINUS;
11007 }
11008 pushback(p, c);
11009 return '-';
11010 }
11011 if (c == '=') {
11012 set_yylval_id('-');
11013 SET_LEX_STATE(EXPR_BEG);
11014 return tOP_ASGN;
11015 }
11016 if (c == '>') {
11017 SET_LEX_STATE(EXPR_ENDFN);
11018 yylval.num = p->lex.lpar_beg;
11019 p->lex.lpar_beg = p->lex.paren_nest;
11020 return tLAMBDA;
11021 }
11022 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
11023 SET_LEX_STATE(EXPR_BEG);
11024 pushback(p, c);
11025 if (c != -1 && ISDIGIT(c)) {
11026 return tUMINUS_NUM;
11027 }
11028 return tUMINUS;
11029 }
11030 SET_LEX_STATE(EXPR_BEG);
11031 pushback(p, c);
11032 return warn_balanced('-', "-", "unary operator");
11033
11034 case '.': {
11035 int is_beg = IS_BEG();
11036 SET_LEX_STATE(EXPR_BEG);
11037 if ((c = nextc(p)) == '.') {
11038 if ((c = nextc(p)) == '.') {
11039 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
11040 SET_LEX_STATE(EXPR_ENDARG);
11041 return tBDOT3;
11042 }
11043 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
11044 rb_warn0("... at EOL, should be parenthesized?");
11045 }
11046 return is_beg ? tBDOT3 : tDOT3;
11047 }
11048 pushback(p, c);
11049 return is_beg ? tBDOT2 : tDOT2;
11050 }
11051 pushback(p, c);
11052 if (c != -1 && ISDIGIT(c)) {
11053 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
11054 parse_numeric(p, '.');
11055 if (ISDIGIT(prev)) {
11056 yyerror0("unexpected fraction part after numeric literal");
11057 }
11058 else {
11059 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
11060 }
11061 SET_LEX_STATE(EXPR_END);
11062 p->lex.ptok = p->lex.pcur;
11063 goto retry;
11064 }
11065 set_yylval_id('.');
11066 SET_LEX_STATE(EXPR_DOT);
11067 return '.';
11068 }
11069
11070 case '0': case '1': case '2': case '3': case '4':
11071 case '5': case '6': case '7': case '8': case '9':
11072 return parse_numeric(p, c);
11073
11074 case ')':
11075 COND_POP();
11076 CMDARG_POP();
11077 SET_LEX_STATE(EXPR_ENDFN);
11078 p->lex.paren_nest--;
11079 return c;
11080
11081 case ']':
11082 COND_POP();
11083 CMDARG_POP();
11084 SET_LEX_STATE(EXPR_END);
11085 p->lex.paren_nest--;
11086 return c;
11087
11088 case '}':
11089 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
11090 if (!p->lex.brace_nest--) return tSTRING_DEND;
11091 COND_POP();
11092 CMDARG_POP();
11093 SET_LEX_STATE(EXPR_END);
11094 p->lex.paren_nest--;
11095 return c;
11096
11097 case ':':
11098 c = nextc(p);
11099 if (c == ':') {
11100 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
11101 SET_LEX_STATE(EXPR_BEG);
11102 return tCOLON3;
11103 }
11104 set_yylval_id(idCOLON2);
11105 SET_LEX_STATE(EXPR_DOT);
11106 return tCOLON2;
11107 }
11108 if (IS_END() || ISSPACE(c) || c == '#') {
11109 pushback(p, c);
11110 c = warn_balanced(':', ":", "symbol literal");
11111 SET_LEX_STATE(EXPR_BEG);
11112 return c;
11113 }
11114 switch (c) {
11115 case '\'':
11116 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11117 break;
11118 case '"':
11119 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11120 break;
11121 default:
11122 pushback(p, c);
11123 break;
11124 }
11125 SET_LEX_STATE(EXPR_FNAME);
11126 return tSYMBEG;
11127
11128 case '/':
11129 if (IS_BEG()) {
11130 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11131 return tREGEXP_BEG;
11132 }
11133 if ((c = nextc(p)) == '=') {
11134 set_yylval_id('/');
11135 SET_LEX_STATE(EXPR_BEG);
11136 return tOP_ASGN;
11137 }
11138 pushback(p, c);
11139 if (IS_SPCARG(c)) {
11140 arg_ambiguous(p, '/');
11141 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11142 return tREGEXP_BEG;
11143 }
11144 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11145 return warn_balanced('/', "/", "regexp literal");
11146
11147 case '^':
11148 if ((c = nextc(p)) == '=') {
11149 set_yylval_id('^');
11150 SET_LEX_STATE(EXPR_BEG);
11151 return tOP_ASGN;
11152 }
11153 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11154 pushback(p, c);
11155 return '^';
11156
11157 case ';':
11158 SET_LEX_STATE(EXPR_BEG);
11159 p->command_start = TRUE;
11160 return ';';
11161
11162 case ',':
11163 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11164 return ',';
11165
11166 case '~':
11167 if (IS_AFTER_OPERATOR()) {
11168 if ((c = nextc(p)) != '@') {
11169 pushback(p, c);
11170 }
11171 SET_LEX_STATE(EXPR_ARG);
11172 }
11173 else {
11174 SET_LEX_STATE(EXPR_BEG);
11175 }
11176 return '~';
11177
11178 case '(':
11179 if (IS_BEG()) {
11180 c = tLPAREN;
11181 }
11182 else if (!space_seen) {
11183 /* foo( ... ) => method call, no ambiguity */
11184 }
11185 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11186 c = tLPAREN_ARG;
11187 }
11188 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11189 rb_warning0("parentheses after method name is interpreted as "
11190 "an argument list, not a decomposed argument");
11191 }
11192 p->lex.paren_nest++;
11193 COND_PUSH(0);
11194 CMDARG_PUSH(0);
11195 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11196 return c;
11197
11198 case '[':
11199 p->lex.paren_nest++;
11200 if (IS_AFTER_OPERATOR()) {
11201 if ((c = nextc(p)) == ']') {
11202 p->lex.paren_nest--;
11203 SET_LEX_STATE(EXPR_ARG);
11204 if ((c = nextc(p)) == '=') {
11205 return tASET;
11206 }
11207 pushback(p, c);
11208 return tAREF;
11209 }
11210 pushback(p, c);
11211 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11212 return '[';
11213 }
11214 else if (IS_BEG()) {
11215 c = tLBRACK;
11216 }
11217 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11218 c = tLBRACK;
11219 }
11220 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11221 COND_PUSH(0);
11222 CMDARG_PUSH(0);
11223 return c;
11224
11225 case '{':
11226 ++p->lex.brace_nest;
11227 if (lambda_beginning_p())
11228 c = tLAMBEG;
11229 else if (IS_lex_state(EXPR_LABELED))
11230 c = tLBRACE; /* hash */
11231 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11232 c = '{'; /* block (primary) */
11233 else if (IS_lex_state(EXPR_ENDARG))
11234 c = tLBRACE_ARG; /* block (expr) */
11235 else
11236 c = tLBRACE; /* hash */
11237 if (c != tLBRACE) {
11238 p->command_start = TRUE;
11239 SET_LEX_STATE(EXPR_BEG);
11240 }
11241 else {
11242 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11243 }
11244 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11245 COND_PUSH(0);
11246 CMDARG_PUSH(0);
11247 return c;
11248
11249 case '\\':
11250 c = nextc(p);
11251 if (c == '\n') {
11252 space_seen = 1;
11253 dispatch_scan_event(p, tSP);
11254 goto retry; /* skip \\n */
11255 }
11256 if (c == ' ') return tSP;
11257 if (ISSPACE(c)) return c;
11258 pushback(p, c);
11259 return '\\';
11260
11261 case '%':
11262 return parse_percent(p, space_seen, last_state);
11263
11264 case '$':
11265 return parse_gvar(p, last_state);
11266
11267 case '@':
11268 return parse_atmark(p, last_state);
11269
11270 case '_':
11271 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11272 p->ruby__end__seen = 1;
11273 p->eofp = 1;
11274#ifdef RIPPER
11275 lex_goto_eol(p);
11276 dispatch_scan_event(p, k__END__);
11277#endif
11278 return END_OF_INPUT;
11279 }
11280 newtok(p);
11281 break;
11282
11283 default:
11284 if (!parser_is_identchar(p)) {
11285 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11286 token_flush(p);
11287 goto retry;
11288 }
11289
11290 newtok(p);
11291 break;
11292 }
11293
11294 return parse_ident(p, c, cmd_state);
11295}
11296
11297static enum yytokentype
11298yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11299{
11300 enum yytokentype t;
11301
11302 p->lval = lval;
11303 lval->node = 0;
11304 p->yylloc = yylloc;
11305
11306 t = parser_yylex(p);
11307
11308 if (has_delayed_token(p))
11309 dispatch_delayed_token(p, t);
11310 else if (t != END_OF_INPUT)
11311 dispatch_scan_event(p, t);
11312
11313 return t;
11314}
11315
11316#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11317
11318static NODE*
11319node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11320{
11321 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11322
11323 rb_node_init(n, type);
11324 return n;
11325}
11326
11327static NODE *
11328nd_set_loc(NODE *nd, const YYLTYPE *loc)
11329{
11330 nd->nd_loc = *loc;
11331 nd_set_line(nd, loc->beg_pos.lineno);
11332 return nd;
11333}
11334
11335static NODE*
11336node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11337{
11338 NODE *n = node_new_internal(p, type, size, alignment);
11339
11340 nd_set_loc(n, loc);
11341 nd_set_node_id(n, parser_get_node_id(p));
11342 return n;
11343}
11344
11345#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11346
11347static rb_node_scope_t *
11348rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11349{
11350 rb_ast_id_table_t *nd_tbl;
11351 nd_tbl = local_tbl(p);
11352 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11353 n->nd_tbl = nd_tbl;
11354 n->nd_body = nd_body;
11355 n->nd_args = nd_args;
11356
11357 return n;
11358}
11359
11360static rb_node_scope_t *
11361rb_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)
11362{
11363 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11364 n->nd_tbl = nd_tbl;
11365 n->nd_body = nd_body;
11366 n->nd_args = nd_args;
11367
11368 return n;
11369}
11370
11371static rb_node_defn_t *
11372rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11373{
11374 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11375 n->nd_mid = nd_mid;
11376 n->nd_defn = nd_defn;
11377
11378 return n;
11379}
11380
11381static rb_node_defs_t *
11382rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11383{
11384 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11385 n->nd_recv = nd_recv;
11386 n->nd_mid = nd_mid;
11387 n->nd_defn = nd_defn;
11388
11389 return n;
11390}
11391
11392static rb_node_block_t *
11393rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11394{
11395 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11396 n->nd_head = nd_head;
11397 n->nd_end = (NODE *)n;
11398 n->nd_next = 0;
11399
11400 return n;
11401}
11402
11403static rb_node_for_t *
11404rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc)
11405{
11406 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11407 n->nd_body = nd_body;
11408 n->nd_iter = nd_iter;
11409
11410 return n;
11411}
11412
11413static rb_node_for_masgn_t *
11414rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11415{
11416 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11417 n->nd_var = nd_var;
11418
11419 return n;
11420}
11421
11422static rb_node_retry_t *
11423rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11424{
11425 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11426
11427 return n;
11428}
11429
11430static rb_node_begin_t *
11431rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11432{
11433 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11434 n->nd_body = nd_body;
11435
11436 return n;
11437}
11438
11439static rb_node_rescue_t *
11440rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11441{
11442 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11443 n->nd_head = nd_head;
11444 n->nd_resq = nd_resq;
11445 n->nd_else = nd_else;
11446
11447 return n;
11448}
11449
11450static rb_node_resbody_t *
11451rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11452{
11453 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11454 n->nd_args = nd_args;
11455 n->nd_exc_var = nd_exc_var;
11456 n->nd_body = nd_body;
11457 n->nd_next = nd_next;
11458
11459 return n;
11460}
11461
11462static rb_node_ensure_t *
11463rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11464{
11465 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11466 n->nd_head = nd_head;
11467 n->nd_ensr = nd_ensr;
11468
11469 return n;
11470}
11471
11472static rb_node_and_t *
11473rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11474{
11475 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11476 n->nd_1st = nd_1st;
11477 n->nd_2nd = nd_2nd;
11478 n->operator_loc = *operator_loc;
11479
11480 return n;
11481}
11482
11483static rb_node_or_t *
11484rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11485{
11486 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11487 n->nd_1st = nd_1st;
11488 n->nd_2nd = nd_2nd;
11489 n->operator_loc = *operator_loc;
11490
11491 return n;
11492}
11493
11494static rb_node_return_t *
11495rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11496{
11497 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11498 n->nd_stts = nd_stts;
11499 n->keyword_loc = *keyword_loc;
11500 return n;
11501}
11502
11503static rb_node_yield_t *
11504rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11505{
11506 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11507 n->nd_head = nd_head;
11508
11509 return n;
11510}
11511
11512static rb_node_if_t *
11513rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11514{
11515 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11516 n->nd_cond = nd_cond;
11517 n->nd_body = nd_body;
11518 n->nd_else = nd_else;
11519
11520 return n;
11521}
11522
11523static rb_node_unless_t *
11524rb_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)
11525{
11526 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11527 n->nd_cond = nd_cond;
11528 n->nd_body = nd_body;
11529 n->nd_else = nd_else;
11530 n->keyword_loc = *keyword_loc;
11531 n->then_keyword_loc = *then_keyword_loc;
11532 n->end_keyword_loc = *end_keyword_loc;
11533
11534 return n;
11535}
11536
11537static rb_node_class_t *
11538rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc)
11539{
11540 /* Keep the order of node creation */
11541 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11542 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11543 n->nd_cpath = nd_cpath;
11544 n->nd_body = scope;
11545 n->nd_super = nd_super;
11546
11547 return n;
11548}
11549
11550static rb_node_sclass_t *
11551rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc)
11552{
11553 /* Keep the order of node creation */
11554 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11555 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11556 n->nd_recv = nd_recv;
11557 n->nd_body = scope;
11558
11559 return n;
11560}
11561
11562static rb_node_module_t *
11563rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc)
11564{
11565 /* Keep the order of node creation */
11566 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11567 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11568 n->nd_cpath = nd_cpath;
11569 n->nd_body = scope;
11570
11571 return n;
11572}
11573
11574static rb_node_iter_t *
11575rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11576{
11577 /* Keep the order of node creation */
11578 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11579 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11580 n->nd_body = scope;
11581 n->nd_iter = 0;
11582
11583 return n;
11584}
11585
11586static rb_node_lambda_t *
11587rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11588{
11589 /* Keep the order of node creation */
11590 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11591 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, loc);
11592 n->nd_body = scope;
11593
11594 return n;
11595}
11596
11597static rb_node_case_t *
11598rb_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)
11599{
11600 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11601 n->nd_head = nd_head;
11602 n->nd_body = nd_body;
11603 n->case_keyword_loc = *case_keyword_loc;
11604 n->end_keyword_loc = *end_keyword_loc;
11605
11606 return n;
11607}
11608
11609static rb_node_case2_t *
11610rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11611{
11612 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11613 n->nd_head = 0;
11614 n->nd_body = nd_body;
11615 n->case_keyword_loc = *case_keyword_loc;
11616 n->end_keyword_loc = *end_keyword_loc;
11617
11618 return n;
11619}
11620
11621static rb_node_case3_t *
11622rb_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)
11623{
11624 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11625 n->nd_head = nd_head;
11626 n->nd_body = nd_body;
11627 n->case_keyword_loc = *case_keyword_loc;
11628 n->end_keyword_loc = *end_keyword_loc;
11629
11630 return n;
11631}
11632
11633static rb_node_when_t *
11634rb_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)
11635{
11636 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11637 n->nd_head = nd_head;
11638 n->nd_body = nd_body;
11639 n->nd_next = nd_next;
11640 n->keyword_loc = *keyword_loc;
11641 n->then_keyword_loc = *then_keyword_loc;
11642
11643 return n;
11644}
11645
11646static rb_node_in_t *
11647rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11648{
11649 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11650 n->nd_head = nd_head;
11651 n->nd_body = nd_body;
11652 n->nd_next = nd_next;
11653
11654 return n;
11655}
11656
11657static rb_node_while_t *
11658rb_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)
11659{
11660 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11661 n->nd_cond = nd_cond;
11662 n->nd_body = nd_body;
11663 n->nd_state = nd_state;
11664 n->keyword_loc = *keyword_loc;
11665 n->closing_loc = *closing_loc;
11666
11667 return n;
11668}
11669
11670static rb_node_until_t *
11671rb_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)
11672{
11673 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11674 n->nd_cond = nd_cond;
11675 n->nd_body = nd_body;
11676 n->nd_state = nd_state;
11677 n->keyword_loc = *keyword_loc;
11678 n->closing_loc = *closing_loc;
11679
11680 return n;
11681}
11682
11683static rb_node_colon2_t *
11684rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc)
11685{
11686 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11687 n->nd_head = nd_head;
11688 n->nd_mid = nd_mid;
11689
11690 return n;
11691}
11692
11693static rb_node_colon3_t *
11694rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11695{
11696 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11697 n->nd_mid = nd_mid;
11698
11699 return n;
11700}
11701
11702static rb_node_dot2_t *
11703rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11704{
11705 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11706 n->nd_beg = nd_beg;
11707 n->nd_end = nd_end;
11708
11709 return n;
11710}
11711
11712static rb_node_dot3_t *
11713rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11714{
11715 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11716 n->nd_beg = nd_beg;
11717 n->nd_end = nd_end;
11718
11719 return n;
11720}
11721
11722static rb_node_self_t *
11723rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11724{
11725 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11726 n->nd_state = 1;
11727
11728 return n;
11729}
11730
11731static rb_node_nil_t *
11732rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11733{
11734 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11735
11736 return n;
11737}
11738
11739static rb_node_true_t *
11740rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11741{
11742 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11743
11744 return n;
11745}
11746
11747static rb_node_false_t *
11748rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11749{
11750 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11751
11752 return n;
11753}
11754
11755static rb_node_super_t *
11756rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc)
11757{
11758 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11759 n->nd_args = nd_args;
11760
11761 return n;
11762}
11763
11764static rb_node_zsuper_t *
11765rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11766{
11767 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11768
11769 return n;
11770}
11771
11772static rb_node_match2_t *
11773rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11774{
11775 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11776 n->nd_recv = nd_recv;
11777 n->nd_value = nd_value;
11778 n->nd_args = 0;
11779
11780 return n;
11781}
11782
11783static rb_node_match3_t *
11784rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11785{
11786 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11787 n->nd_recv = nd_recv;
11788 n->nd_value = nd_value;
11789
11790 return n;
11791}
11792
11793/* TODO: Use union for NODE_LIST2 */
11794static rb_node_list_t *
11795rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11796{
11797 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11798 n->nd_head = nd_head;
11799 n->as.nd_alen = 1;
11800 n->nd_next = 0;
11801
11802 return n;
11803}
11804
11805static rb_node_list_t *
11806rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11807{
11808 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11809 n->nd_head = nd_head;
11810 n->as.nd_alen = nd_alen;
11811 n->nd_next = nd_next;
11812
11813 return n;
11814}
11815
11816static rb_node_zlist_t *
11817rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11818{
11819 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11820
11821 return n;
11822}
11823
11824static rb_node_hash_t *
11825rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11826{
11827 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11828 n->nd_head = nd_head;
11829 n->nd_brace = 0;
11830
11831 return n;
11832}
11833
11834static rb_node_masgn_t *
11835rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11836{
11837 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11838 n->nd_head = nd_head;
11839 n->nd_value = 0;
11840 n->nd_args = nd_args;
11841
11842 return n;
11843}
11844
11845static rb_node_gasgn_t *
11846rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11847{
11848 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11849 n->nd_vid = nd_vid;
11850 n->nd_value = nd_value;
11851
11852 return n;
11853}
11854
11855static rb_node_lasgn_t *
11856rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11857{
11858 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11859 n->nd_vid = nd_vid;
11860 n->nd_value = nd_value;
11861
11862 return n;
11863}
11864
11865static rb_node_dasgn_t *
11866rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11867{
11868 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11869 n->nd_vid = nd_vid;
11870 n->nd_value = nd_value;
11871
11872 return n;
11873}
11874
11875static rb_node_iasgn_t *
11876rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11877{
11878 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11879 n->nd_vid = nd_vid;
11880 n->nd_value = nd_value;
11881
11882 return n;
11883}
11884
11885static rb_node_cvasgn_t *
11886rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11887{
11888 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11889 n->nd_vid = nd_vid;
11890 n->nd_value = nd_value;
11891
11892 return n;
11893}
11894
11895static rb_node_op_asgn1_t *
11896rb_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)
11897{
11898 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11899 n->nd_recv = nd_recv;
11900 n->nd_mid = nd_mid;
11901 n->nd_index = index;
11902 n->nd_rvalue = rvalue;
11903 n->call_operator_loc = *call_operator_loc;
11904 n->opening_loc = *opening_loc;
11905 n->closing_loc = *closing_loc;
11906 n->binary_operator_loc = *binary_operator_loc;
11907
11908 return n;
11909}
11910
11911static rb_node_op_asgn2_t *
11912rb_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)
11913{
11914 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11915 n->nd_recv = nd_recv;
11916 n->nd_value = nd_value;
11917 n->nd_vid = nd_vid;
11918 n->nd_mid = nd_mid;
11919 n->nd_aid = nd_aid;
11920 n->call_operator_loc = *call_operator_loc;
11921 n->message_loc = *message_loc;
11922 n->binary_operator_loc = *binary_operator_loc;
11923
11924 return n;
11925}
11926
11927static rb_node_op_asgn_or_t *
11928rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11929{
11930 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11931 n->nd_head = nd_head;
11932 n->nd_value = nd_value;
11933
11934 return n;
11935}
11936
11937static rb_node_op_asgn_and_t *
11938rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11939{
11940 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11941 n->nd_head = nd_head;
11942 n->nd_value = nd_value;
11943
11944 return n;
11945}
11946
11947static rb_node_gvar_t *
11948rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11949{
11950 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11951 n->nd_vid = nd_vid;
11952
11953 return n;
11954}
11955
11956static rb_node_lvar_t *
11957rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11958{
11959 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11960 n->nd_vid = nd_vid;
11961
11962 return n;
11963}
11964
11965static rb_node_dvar_t *
11966rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11967{
11968 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11969 n->nd_vid = nd_vid;
11970
11971 return n;
11972}
11973
11974static rb_node_ivar_t *
11975rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11976{
11977 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11978 n->nd_vid = nd_vid;
11979
11980 return n;
11981}
11982
11983static rb_node_const_t *
11984rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11985{
11986 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11987 n->nd_vid = nd_vid;
11988
11989 return n;
11990}
11991
11992static rb_node_cvar_t *
11993rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11994{
11995 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11996 n->nd_vid = nd_vid;
11997
11998 return n;
11999}
12000
12001static rb_node_nth_ref_t *
12002rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
12003{
12004 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
12005 n->nd_nth = nd_nth;
12006
12007 return n;
12008}
12009
12010static rb_node_back_ref_t *
12011rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
12012{
12013 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
12014 n->nd_nth = nd_nth;
12015
12016 return n;
12017}
12018
12019static rb_node_integer_t *
12020rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
12021{
12022 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
12023 n->val = val;
12024 n->minus = FALSE;
12025 n->base = base;
12026
12027 return n;
12028}
12029
12030static rb_node_float_t *
12031rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
12032{
12033 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
12034 n->val = val;
12035 n->minus = FALSE;
12036
12037 return n;
12038}
12039
12040static rb_node_rational_t *
12041rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
12042{
12043 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
12044 n->val = val;
12045 n->minus = FALSE;
12046 n->base = base;
12047 n->seen_point = seen_point;
12048
12049 return n;
12050}
12051
12052static rb_node_imaginary_t *
12053rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
12054{
12055 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
12056 n->val = val;
12057 n->minus = FALSE;
12058 n->base = base;
12059 n->seen_point = seen_point;
12060 n->type = numeric_type;
12061
12062 return n;
12063}
12064
12065static rb_node_str_t *
12066rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12067{
12068 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12069 n->string = string;
12070
12071 return n;
12072}
12073
12074/* TODO; Use union for NODE_DSTR2 */
12075static rb_node_dstr_t *
12076rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12077{
12078 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12079 n->string = string;
12080 n->as.nd_alen = nd_alen;
12081 n->nd_next = (rb_node_list_t *)nd_next;
12082
12083 return n;
12084}
12085
12086static rb_node_dstr_t *
12087rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12088{
12089 return rb_node_dstr_new0(p, string, 1, 0, loc);
12090}
12091
12092static rb_node_xstr_t *
12093rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12094{
12095 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12096 n->string = string;
12097
12098 return n;
12099}
12100
12101static rb_node_dxstr_t *
12102rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12103{
12104 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12105 n->string = string;
12106 n->as.nd_alen = nd_alen;
12107 n->nd_next = (rb_node_list_t *)nd_next;
12108
12109 return n;
12110}
12111
12112static rb_node_sym_t *
12113rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12114{
12115 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12116 n->string = rb_str_to_parser_string(p, str);
12117
12118 return n;
12119}
12120
12121static rb_node_dsym_t *
12122rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12123{
12124 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12125 n->string = string;
12126 n->as.nd_alen = nd_alen;
12127 n->nd_next = (rb_node_list_t *)nd_next;
12128
12129 return n;
12130}
12131
12132static rb_node_evstr_t *
12133rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12134{
12135 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12136 n->nd_body = nd_body;
12137
12138 return n;
12139}
12140
12141static rb_node_regx_t *
12142rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc)
12143{
12144 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12145 n->string = string;
12146 n->options = options & RE_OPTION_MASK;
12147
12148 return n;
12149}
12150
12151static rb_node_call_t *
12152rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12153{
12154 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12155 n->nd_recv = nd_recv;
12156 n->nd_mid = nd_mid;
12157 n->nd_args = nd_args;
12158
12159 return n;
12160}
12161
12162static rb_node_opcall_t *
12163rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12164{
12165 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12166 n->nd_recv = nd_recv;
12167 n->nd_mid = nd_mid;
12168 n->nd_args = nd_args;
12169
12170 return n;
12171}
12172
12173static rb_node_fcall_t *
12174rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12175{
12176 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12177 n->nd_mid = nd_mid;
12178 n->nd_args = nd_args;
12179
12180 return n;
12181}
12182
12183static rb_node_qcall_t *
12184rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12185{
12186 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12187 n->nd_recv = nd_recv;
12188 n->nd_mid = nd_mid;
12189 n->nd_args = nd_args;
12190
12191 return n;
12192}
12193
12194static rb_node_vcall_t *
12195rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12196{
12197 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12198 n->nd_mid = nd_mid;
12199
12200 return n;
12201}
12202
12203static rb_node_once_t *
12204rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12205{
12206 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12207 n->nd_body = nd_body;
12208
12209 return n;
12210}
12211
12212static rb_node_args_t *
12213rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12214{
12215 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12216 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12217
12218 return n;
12219}
12220
12221static rb_node_args_aux_t *
12222rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12223{
12224 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12225 n->nd_pid = nd_pid;
12226 n->nd_plen = nd_plen;
12227 n->nd_next = 0;
12228
12229 return n;
12230}
12231
12232static rb_node_opt_arg_t *
12233rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12234{
12235 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12236 n->nd_body = nd_body;
12237 n->nd_next = 0;
12238
12239 return n;
12240}
12241
12242static rb_node_kw_arg_t *
12243rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12244{
12245 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12246 n->nd_body = nd_body;
12247 n->nd_next = 0;
12248
12249 return n;
12250}
12251
12252static rb_node_postarg_t *
12253rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12254{
12255 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12256 n->nd_1st = nd_1st;
12257 n->nd_2nd = nd_2nd;
12258
12259 return n;
12260}
12261
12262static rb_node_argscat_t *
12263rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12264{
12265 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12266 n->nd_head = nd_head;
12267 n->nd_body = nd_body;
12268
12269 return n;
12270}
12271
12272static rb_node_argspush_t *
12273rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12274{
12275 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12276 n->nd_head = nd_head;
12277 n->nd_body = nd_body;
12278
12279 return n;
12280}
12281
12282static rb_node_splat_t *
12283rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12284{
12285 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12286 n->nd_head = nd_head;
12287 n->operator_loc = *operator_loc;
12288
12289 return n;
12290}
12291
12292static rb_node_block_pass_t *
12293rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12294{
12295 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12296 n->forwarding = 0;
12297 n->nd_head = 0;
12298 n->nd_body = nd_body;
12299 n->operator_loc = *operator_loc;
12300
12301 return n;
12302}
12303
12304static rb_node_alias_t *
12305rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12306{
12307 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12308 n->nd_1st = nd_1st;
12309 n->nd_2nd = nd_2nd;
12310 n->keyword_loc = *keyword_loc;
12311
12312 return n;
12313}
12314
12315static rb_node_valias_t *
12316rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12317{
12318 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12319 n->nd_alias = nd_alias;
12320 n->nd_orig = nd_orig;
12321 n->keyword_loc = *keyword_loc;
12322
12323 return n;
12324}
12325
12326static rb_node_undef_t *
12327rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12328{
12329 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12330 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12331 n->keyword_loc = NULL_LOC;
12332 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12333
12334 return n;
12335}
12336
12337static rb_node_errinfo_t *
12338rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12339{
12340 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12341
12342 return n;
12343}
12344
12345static rb_node_defined_t *
12346rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12347{
12348 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12349 n->nd_head = nd_head;
12350
12351 return n;
12352}
12353
12354static rb_node_postexe_t *
12355rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12356{
12357 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12358 n->nd_body = nd_body;
12359
12360 return n;
12361}
12362
12363static rb_node_attrasgn_t *
12364rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12365{
12366 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12367 n->nd_recv = nd_recv;
12368 n->nd_mid = nd_mid;
12369 n->nd_args = nd_args;
12370
12371 return n;
12372}
12373
12374static rb_node_aryptn_t *
12375rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12376{
12377 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12378 n->nd_pconst = 0;
12379 n->pre_args = pre_args;
12380 n->rest_arg = rest_arg;
12381 n->post_args = post_args;
12382
12383 return n;
12384}
12385
12386static rb_node_hshptn_t *
12387rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12388{
12389 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12390 n->nd_pconst = nd_pconst;
12391 n->nd_pkwargs = nd_pkwargs;
12392 n->nd_pkwrestarg = nd_pkwrestarg;
12393
12394 return n;
12395}
12396
12397static rb_node_fndptn_t *
12398rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12399{
12400 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12401 n->nd_pconst = 0;
12402 n->pre_rest_arg = pre_rest_arg;
12403 n->args = args;
12404 n->post_rest_arg = post_rest_arg;
12405
12406 return n;
12407}
12408
12409static rb_node_line_t *
12410rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12411{
12412 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12413
12414 return n;
12415}
12416
12417static rb_node_file_t *
12418rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12419{
12420 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12421 n->path = rb_str_to_parser_string(p, str);
12422
12423 return n;
12424}
12425
12426static rb_node_encoding_t *
12427rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12428{
12429 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12430 n->enc = p->enc;
12431
12432 return n;
12433}
12434
12435static rb_node_cdecl_t *
12436rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12437{
12438 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12439 n->nd_vid = nd_vid;
12440 n->nd_value = nd_value;
12441 n->nd_else = nd_else;
12442 n->shareability = shareability;
12443
12444 return n;
12445}
12446
12447static rb_node_op_cdecl_t *
12448rb_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)
12449{
12450 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12451 n->nd_head = nd_head;
12452 n->nd_value = nd_value;
12453 n->nd_aid = nd_aid;
12454 n->shareability = shareability;
12455
12456 return n;
12457}
12458
12459static rb_node_error_t *
12460rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12461{
12462 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12463
12464 return n;
12465}
12466
12467static rb_node_break_t *
12468rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12469{
12470 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12471 n->nd_stts = nd_stts;
12472 n->nd_chain = 0;
12473 n->keyword_loc = *keyword_loc;
12474
12475 return n;
12476}
12477
12478static rb_node_next_t *
12479rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12480{
12481 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12482 n->nd_stts = nd_stts;
12483 n->nd_chain = 0;
12484 n->keyword_loc = *keyword_loc;
12485
12486 return n;
12487}
12488
12489static rb_node_redo_t *
12490rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12491{
12492 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12493 n->nd_chain = 0;
12494 n->keyword_loc = *keyword_loc;
12495
12496 return n;
12497}
12498
12499static rb_node_def_temp_t *
12500rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12501{
12502 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12503 n->save.numparam_save = 0;
12504 n->save.max_numparam = 0;
12505 n->save.ctxt = p->ctxt;
12506 n->nd_def = 0;
12507 n->nd_mid = 0;
12508
12509 return n;
12510}
12511
12512static rb_node_def_temp_t *
12513def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12514{
12515 n->save.numparam_save = numparam_push(p);
12516 n->save.max_numparam = p->max_numparam;
12517 return n;
12518}
12519
12520#ifndef RIPPER
12521static enum node_type
12522nodetype(NODE *node) /* for debug */
12523{
12524 return (enum node_type)nd_type(node);
12525}
12526
12527static int
12528nodeline(NODE *node)
12529{
12530 return nd_line(node);
12531}
12532#endif
12533
12534static NODE*
12535newline_node(NODE *node)
12536{
12537 if (node) {
12538 node = remove_begin(node);
12539 nd_set_fl_newline(node);
12540 }
12541 return node;
12542}
12543
12544static void
12545fixpos(NODE *node, NODE *orig)
12546{
12547 if (!node) return;
12548 if (!orig) return;
12549 nd_set_line(node, nd_line(orig));
12550}
12551
12552static NODE*
12553block_append(struct parser_params *p, NODE *head, NODE *tail)
12554{
12555 NODE *end, *h = head, *nd;
12556
12557 if (tail == 0) return head;
12558
12559 if (h == 0) return tail;
12560 switch (nd_type(h)) {
12561 default:
12562 h = end = NEW_BLOCK(head, &head->nd_loc);
12563 head = end;
12564 break;
12565 case NODE_BLOCK:
12566 end = RNODE_BLOCK(h)->nd_end;
12567 break;
12568 }
12569
12570 nd = RNODE_BLOCK(end)->nd_head;
12571 switch (nd_type(nd)) {
12572 case NODE_RETURN:
12573 case NODE_BREAK:
12574 case NODE_NEXT:
12575 case NODE_REDO:
12576 case NODE_RETRY:
12577 rb_warning0L(nd_line(tail), "statement not reached");
12578 break;
12579
12580 default:
12581 break;
12582 }
12583
12584 if (!nd_type_p(tail, NODE_BLOCK)) {
12585 tail = NEW_BLOCK(tail, &tail->nd_loc);
12586 }
12587 RNODE_BLOCK(end)->nd_next = tail;
12588 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12589 nd_set_last_loc(head, nd_last_loc(tail));
12590 return head;
12591}
12592
12593/* append item to the list */
12594static NODE*
12595list_append(struct parser_params *p, NODE *list, NODE *item)
12596{
12597 NODE *last;
12598
12599 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12600 if (RNODE_LIST(list)->nd_next) {
12601 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12602 }
12603 else {
12604 last = list;
12605 }
12606
12607 RNODE_LIST(list)->as.nd_alen += 1;
12608 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12609 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12610
12611 nd_set_last_loc(list, nd_last_loc(item));
12612
12613 return list;
12614}
12615
12616/* concat two lists */
12617static NODE*
12618list_concat(NODE *head, NODE *tail)
12619{
12620 NODE *last;
12621
12622 if (RNODE_LIST(head)->nd_next) {
12623 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12624 }
12625 else {
12626 last = head;
12627 }
12628
12629 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12630 RNODE_LIST(last)->nd_next = tail;
12631 if (RNODE_LIST(tail)->nd_next) {
12632 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12633 }
12634 else {
12635 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12636 }
12637
12638 nd_set_last_loc(head, nd_last_loc(tail));
12639
12640 return head;
12641}
12642
12643static int
12644literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12645{
12646 if (!tail) return 1;
12647 if (!rb_parser_enc_compatible(p, head, tail)) {
12648 compile_error(p, "string literal encodings differ (%s / %s)",
12649 rb_enc_name(rb_parser_str_get_encoding(head)),
12650 rb_enc_name(rb_parser_str_get_encoding(tail)));
12651 rb_parser_str_resize(p, head, 0);
12652 rb_parser_str_resize(p, tail, 0);
12653 return 0;
12654 }
12655 rb_parser_str_buf_append(p, head, tail);
12656 return 1;
12657}
12658
12659static rb_parser_string_t *
12660string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12661{
12662 if (htype != NODE_DSTR) return NULL;
12663 if (RNODE_DSTR(head)->nd_next) {
12664 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12665 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12666 }
12667 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12668 ASSUME(lit);
12669 return lit;
12670}
12671
12672#ifndef RIPPER
12673static rb_parser_string_t *
12674rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12675{
12676 rb_parser_string_t *copy;
12677 if (!orig) return NULL;
12678 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12679 copy->coderange = orig->coderange;
12680 copy->enc = orig->enc;
12681 return copy;
12682}
12683#endif
12684
12685/* concat two string literals */
12686static NODE *
12687literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12688{
12689 enum node_type htype;
12690 rb_parser_string_t *lit;
12691
12692 if (!head) return tail;
12693 if (!tail) return head;
12694
12695 htype = nd_type(head);
12696 if (htype == NODE_EVSTR) {
12697 head = new_dstr(p, head, loc);
12698 htype = NODE_DSTR;
12699 }
12700 if (p->heredoc_indent > 0) {
12701 switch (htype) {
12702 case NODE_STR:
12703 head = str2dstr(p, head);
12704 case NODE_DSTR:
12705 return list_append(p, head, tail);
12706 default:
12707 break;
12708 }
12709 }
12710 switch (nd_type(tail)) {
12711 case NODE_STR:
12712 if ((lit = string_literal_head(p, htype, head)) != false) {
12713 htype = NODE_STR;
12714 }
12715 else {
12716 lit = RNODE_DSTR(head)->string;
12717 }
12718 if (htype == NODE_STR) {
12719 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12720 error:
12721 rb_discard_node(p, head);
12722 rb_discard_node(p, tail);
12723 return 0;
12724 }
12725 rb_discard_node(p, tail);
12726 }
12727 else {
12728 list_append(p, head, tail);
12729 }
12730 break;
12731
12732 case NODE_DSTR:
12733 if (htype == NODE_STR) {
12734 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12735 goto error;
12736 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12737 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12738 RNODE_STR(head)->string = NULL;
12739 rb_discard_node(p, head);
12740 head = tail;
12741 }
12742 else if (!RNODE_DSTR(tail)->string) {
12743 append:
12744 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12745 if (!RNODE_DSTR(head)->nd_next) {
12746 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12747 }
12748 else if (RNODE_DSTR(tail)->nd_next) {
12749 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12750 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12751 }
12752 rb_discard_node(p, tail);
12753 }
12754 else if ((lit = string_literal_head(p, htype, head)) != false) {
12755 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12756 goto error;
12757 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12758 RNODE_DSTR(tail)->string = 0;
12759 goto append;
12760 }
12761 else {
12762 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));
12763 RNODE_DSTR(tail)->string = 0;
12764 }
12765 break;
12766
12767 case NODE_EVSTR:
12768 if (htype == NODE_STR) {
12769 head = str2dstr(p, head);
12770 RNODE_DSTR(head)->as.nd_alen = 1;
12771 }
12772 list_append(p, head, tail);
12773 break;
12774 }
12775 return head;
12776}
12777
12778static void
12779nd_copy_flag(NODE *new_node, NODE *old_node)
12780{
12781 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12782 nd_set_line(new_node, nd_line(old_node));
12783 new_node->nd_loc = old_node->nd_loc;
12784 new_node->node_id = old_node->node_id;
12785}
12786
12787static NODE *
12788str2dstr(struct parser_params *p, NODE *node)
12789{
12790 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12791 nd_copy_flag(new_node, node);
12792 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12793 RNODE_DSTR(new_node)->as.nd_alen = 0;
12794 RNODE_DSTR(new_node)->nd_next = 0;
12795 RNODE_STR(node)->string = 0;
12796
12797 return new_node;
12798}
12799
12800static NODE *
12801str2regx(struct parser_params *p, NODE *node, int options)
12802{
12803 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12804 nd_copy_flag(new_node, node);
12805 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12806 RNODE_REGX(new_node)->options = options;
12807 RNODE_STR(node)->string = 0;
12808
12809 return new_node;
12810}
12811
12812static NODE *
12813evstr2dstr(struct parser_params *p, NODE *node)
12814{
12815 if (nd_type_p(node, NODE_EVSTR)) {
12816 node = new_dstr(p, node, &node->nd_loc);
12817 }
12818 return node;
12819}
12820
12821static NODE *
12822new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12823{
12824 NODE *head = node;
12825
12826 if (node) {
12827 switch (nd_type(node)) {
12828 case NODE_STR:
12829 return str2dstr(p, node);
12830 case NODE_DSTR:
12831 break;
12832 case NODE_EVSTR:
12833 return node;
12834 }
12835 }
12836 return NEW_EVSTR(head, loc);
12837}
12838
12839static NODE *
12840new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12841{
12842 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12843 return list_append(p, dstr, node);
12844}
12845
12846static NODE *
12847call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12848 const YYLTYPE *op_loc, const YYLTYPE *loc)
12849{
12850 NODE *expr;
12851 value_expr(recv);
12852 value_expr(arg1);
12853 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12854 nd_set_line(expr, op_loc->beg_pos.lineno);
12855 return expr;
12856}
12857
12858static NODE *
12859call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12860{
12861 NODE *opcall;
12862 value_expr(recv);
12863 opcall = NEW_OPCALL(recv, id, 0, loc);
12864 nd_set_line(opcall, op_loc->beg_pos.lineno);
12865 return opcall;
12866}
12867
12868static NODE *
12869new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12870{
12871 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12872 nd_set_line(qcall, op_loc->beg_pos.lineno);
12873 return qcall;
12874}
12875
12876static NODE*
12877new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12878{
12879 NODE *ret;
12880 if (block) block_dup_check(p, args, block);
12881 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12882 if (block) ret = method_add_block(p, ret, block, loc);
12883 fixpos(ret, recv);
12884 return ret;
12885}
12886
12887#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12888
12889static NODE*
12890last_expr_once_body(NODE *node)
12891{
12892 if (!node) return 0;
12893 return nd_once_body(node);
12894}
12895
12896static NODE*
12897match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12898{
12899 NODE *n;
12900 int line = op_loc->beg_pos.lineno;
12901
12902 value_expr(node1);
12903 value_expr(node2);
12904
12905 if ((n = last_expr_once_body(node1)) != 0) {
12906 switch (nd_type(n)) {
12907 case NODE_DREGX:
12908 {
12909 NODE *match = NEW_MATCH2(node1, node2, loc);
12910 nd_set_line(match, line);
12911 return match;
12912 }
12913
12914 case NODE_REGX:
12915 {
12916 const VALUE lit = rb_node_regx_string_val(n);
12917 if (!NIL_P(lit)) {
12918 NODE *match = NEW_MATCH2(node1, node2, loc);
12919 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12920 nd_set_line(match, line);
12921 return match;
12922 }
12923 }
12924 }
12925 }
12926
12927 if ((n = last_expr_once_body(node2)) != 0) {
12928 NODE *match3;
12929
12930 switch (nd_type(n)) {
12931 case NODE_DREGX:
12932 match3 = NEW_MATCH3(node2, node1, loc);
12933 return match3;
12934 }
12935 }
12936
12937 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12938 nd_set_line(n, line);
12939 return n;
12940}
12941
12942# if WARN_PAST_SCOPE
12943static int
12944past_dvar_p(struct parser_params *p, ID id)
12945{
12946 struct vtable *past = p->lvtbl->past;
12947 while (past) {
12948 if (vtable_included(past, id)) return 1;
12949 past = past->prev;
12950 }
12951 return 0;
12952}
12953# endif
12954
12955static int
12956numparam_nested_p(struct parser_params *p)
12957{
12958 struct local_vars *local = p->lvtbl;
12959 NODE *outer = local->numparam.outer;
12960 NODE *inner = local->numparam.inner;
12961 if (outer || inner) {
12962 NODE *used = outer ? outer : inner;
12963 compile_error(p, "numbered parameter is already used in\n"
12964 "%s:%d: %s block here",
12965 p->ruby_sourcefile, nd_line(used),
12966 outer ? "outer" : "inner");
12967 parser_show_error_line(p, &used->nd_loc);
12968 return 1;
12969 }
12970 return 0;
12971}
12972
12973static int
12974numparam_used_p(struct parser_params *p)
12975{
12976 NODE *numparam = p->lvtbl->numparam.current;
12977 if (numparam) {
12978 compile_error(p, "numbered parameter is already used in\n"
12979 "%s:%d: current block here",
12980 p->ruby_sourcefile, nd_line(numparam));
12981 parser_show_error_line(p, &numparam->nd_loc);
12982 return 1;
12983 }
12984 return 0;
12985}
12986
12987static int
12988it_used_p(struct parser_params *p)
12989{
12990 NODE *it = p->lvtbl->it;
12991 if (it) {
12992 compile_error(p, "'it' is already used in\n"
12993 "%s:%d: current block here",
12994 p->ruby_sourcefile, nd_line(it));
12995 parser_show_error_line(p, &it->nd_loc);
12996 return 1;
12997 }
12998 return 0;
12999}
13000
13001static NODE*
13002gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
13003{
13004 ID *vidp = NULL;
13005 NODE *node;
13006 switch (id) {
13007 case keyword_self:
13008 return NEW_SELF(loc);
13009 case keyword_nil:
13010 return NEW_NIL(loc);
13011 case keyword_true:
13012 return NEW_TRUE(loc);
13013 case keyword_false:
13014 return NEW_FALSE(loc);
13015 case keyword__FILE__:
13016 {
13017 VALUE file = p->ruby_sourcefile_string;
13018 if (NIL_P(file))
13019 file = rb_str_new(0, 0);
13020 node = NEW_FILE(file, loc);
13021 }
13022 return node;
13023 case keyword__LINE__:
13024 return NEW_LINE(loc);
13025 case keyword__ENCODING__:
13026 return NEW_ENCODING(loc);
13027
13028 }
13029 switch (id_type(id)) {
13030 case ID_LOCAL:
13031 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13032 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13033 if (vidp) *vidp |= LVAR_USED;
13034 node = NEW_DVAR(id, loc);
13035 return node;
13036 }
13037 if (local_id_ref(p, id, &vidp)) {
13038 if (vidp) *vidp |= LVAR_USED;
13039 node = NEW_LVAR(id, loc);
13040 return node;
13041 }
13042 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13043 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13044 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13045 node = NEW_DVAR(id, loc);
13046 struct local_vars *local = p->lvtbl;
13047 if (!local->numparam.current) local->numparam.current = node;
13048 return node;
13049 }
13050# if WARN_PAST_SCOPE
13051 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13052 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13053 }
13054# endif
13055 /* method call without arguments */
13056 if (dyna_in_block(p) && id == rb_intern("it") && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13057 if (numparam_used_p(p)) return 0;
13058 if (p->max_numparam == ORDINAL_PARAM) {
13059 compile_error(p, "ordinary parameter is defined");
13060 return 0;
13061 }
13062 if (!p->it_id) {
13063 p->it_id = internal_id(p);
13064 vtable_add(p->lvtbl->args, p->it_id);
13065 }
13066 NODE *node = NEW_DVAR(p->it_id, loc);
13067 if (!p->lvtbl->it) p->lvtbl->it = node;
13068 return node;
13069 }
13070 return NEW_VCALL(id, loc);
13071 case ID_GLOBAL:
13072 return NEW_GVAR(id, loc);
13073 case ID_INSTANCE:
13074 return NEW_IVAR(id, loc);
13075 case ID_CONST:
13076 return NEW_CONST(id, loc);
13077 case ID_CLASS:
13078 return NEW_CVAR(id, loc);
13079 }
13080 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13081 return 0;
13082}
13083
13084static rb_node_opt_arg_t *
13085opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13086{
13087 rb_node_opt_arg_t *opts = opt_list;
13088 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13089
13090 while (opts->nd_next) {
13091 opts = opts->nd_next;
13092 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13093 }
13094 opts->nd_next = opt;
13095
13096 return opt_list;
13097}
13098
13099static rb_node_kw_arg_t *
13100kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13101{
13102 if (kwlist) {
13103 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13104 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13105 }
13106 return kwlist;
13107}
13108
13109static NODE *
13110new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
13111{
13112 NODE *n = expr;
13113 while (n) {
13114 if (nd_type_p(n, NODE_BEGIN)) {
13115 n = RNODE_BEGIN(n)->nd_body;
13116 }
13117 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13118 n = RNODE_BLOCK(n)->nd_head;
13119 }
13120 else {
13121 break;
13122 }
13123 }
13124 return NEW_DEFINED(n, loc);
13125}
13126
13127static NODE*
13128str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13129{
13130 VALUE lit;
13131 rb_parser_string_t *str = RNODE_STR(node)->string;
13132 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13133 yyerror1(loc, "invalid symbol");
13134 lit = STR_NEW0();
13135 }
13136 else {
13137 lit = rb_str_new_parser_string(str);
13138 }
13139 return NEW_SYM(lit, loc);
13140}
13141
13142static NODE*
13143symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13144{
13145 enum node_type type = nd_type(symbol);
13146 switch (type) {
13147 case NODE_DSTR:
13148 nd_set_type(symbol, NODE_DSYM);
13149 break;
13150 case NODE_STR:
13151 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13152 break;
13153 default:
13154 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13155 }
13156 return list_append(p, symbols, symbol);
13157}
13158
13159static NODE *
13160new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
13161{
13162 struct RNode_LIST *list;
13163 NODE *prev;
13164
13165 if (!node) {
13166 /* Check string is valid regex */
13167 rb_parser_string_t *str = STRING_NEW0();
13168 reg_compile(p, str, options);
13169 node = NEW_REGX(str, options, loc);
13170 return node;
13171 }
13172 switch (nd_type(node)) {
13173 case NODE_STR:
13174 {
13175 /* Check string is valid regex */
13176 reg_compile(p, RNODE_STR(node)->string, options);
13177 node = str2regx(p, node, options);
13178 }
13179 break;
13180 default:
13181 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13182 /* fall through */
13183 case NODE_DSTR:
13184 nd_set_type(node, NODE_DREGX);
13185 nd_set_loc(node, loc);
13186 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13187 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13188 if (dreg->string) reg_fragment_check(p, dreg->string, options);
13189 prev = node;
13190 for (list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13191 NODE *frag = list->nd_head;
13192 enum node_type type = nd_type(frag);
13193 if (type == NODE_STR || (type == NODE_DSTR && !RNODE_DSTR(frag)->nd_next)) {
13194 rb_parser_string_t *tail = RNODE_STR(frag)->string;
13195 if (reg_fragment_check(p, tail, options) && prev && RNODE_DREGX(prev)->string) {
13196 rb_parser_string_t *lit = prev == node ? dreg->string : RNODE_STR(RNODE_LIST(prev)->nd_head)->string;
13197 if (!literal_concat0(p, lit, tail)) {
13198 return NEW_NIL(loc); /* dummy node on error */
13199 }
13200 rb_parser_str_resize(p, tail, 0);
13201 RNODE_LIST(prev)->nd_next = list->nd_next;
13202 rb_discard_node(p, list->nd_head);
13203 rb_discard_node(p, (NODE *)list);
13204 list = RNODE_LIST(prev);
13205 }
13206 else {
13207 prev = (NODE *)list;
13208 }
13209 }
13210 else {
13211 prev = 0;
13212 }
13213 }
13214 if (!dreg->nd_next) {
13215 /* Check string is valid regex */
13216 reg_compile(p, dreg->string, options);
13217 }
13218 if (options & RE_OPTION_ONCE) {
13219 node = NEW_ONCE(node, loc);
13220 }
13221 break;
13222 }
13223 return node;
13224}
13225
13226static rb_node_kw_arg_t *
13227new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13228{
13229 if (!k) return 0;
13230 return NEW_KW_ARG((k), loc);
13231}
13232
13233static NODE *
13234new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13235{
13236 if (!node) {
13237 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13238 return xstr;
13239 }
13240 switch (nd_type(node)) {
13241 case NODE_STR:
13242 nd_set_type(node, NODE_XSTR);
13243 nd_set_loc(node, loc);
13244 break;
13245 case NODE_DSTR:
13246 nd_set_type(node, NODE_DXSTR);
13247 nd_set_loc(node, loc);
13248 break;
13249 default:
13250 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13251 break;
13252 }
13253 return node;
13254}
13255
13256static const
13257struct st_hash_type literal_type = {
13258 literal_cmp,
13259 literal_hash,
13260};
13261
13262static int nd_type_st_key_enable_p(NODE *node);
13263
13264static void
13265check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13266{
13267 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13268 if (!arg || !p->case_labels) return;
13269 if (!nd_type_st_key_enable_p(arg)) return;
13270
13271 if (p->case_labels == CHECK_LITERAL_WHEN) {
13272 p->case_labels = st_init_table(&literal_type);
13273 }
13274 else {
13275 st_data_t line;
13276 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13277 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13278 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13279 return;
13280 }
13281 }
13282 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13283}
13284
13285#ifdef RIPPER
13286static int
13287id_is_var(struct parser_params *p, ID id)
13288{
13289 if (is_notop_id(id)) {
13290 switch (id & ID_SCOPE_MASK) {
13291 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13292 return 1;
13293 case ID_LOCAL:
13294 if (dyna_in_block(p)) {
13295 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13296 }
13297 if (local_id(p, id)) return 1;
13298 /* method call without arguments */
13299 return 0;
13300 }
13301 }
13302 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13303 return 0;
13304}
13305#endif
13306
13307static inline enum lex_state_e
13308parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13309{
13310 if (p->debug) {
13311 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13312 }
13313 return p->lex.state = ls;
13314}
13315
13316#ifndef RIPPER
13317static void
13318flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13319{
13320 VALUE mesg = p->debug_buffer;
13321
13322 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13323 p->debug_buffer = Qnil;
13324 rb_io_puts(1, &mesg, out);
13325 }
13326 if (!NIL_P(str) && RSTRING_LEN(str)) {
13327 rb_io_write(p->debug_output, str);
13328 }
13329}
13330
13331static const char rb_parser_lex_state_names[][8] = {
13332 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13333 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13334 "LABEL", "LABELED","FITEM",
13335};
13336
13337static VALUE
13338append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13339{
13340 int i, sep = 0;
13341 unsigned int mask = 1;
13342 static const char none[] = "NONE";
13343
13344 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13345 if ((unsigned)state & mask) {
13346 if (sep) {
13347 rb_str_cat(buf, "|", 1);
13348 }
13349 sep = 1;
13350 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13351 }
13352 }
13353 if (!sep) {
13354 rb_str_cat(buf, none, sizeof(none)-1);
13355 }
13356 return buf;
13357}
13358
13359enum lex_state_e
13360rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13361 enum lex_state_e to, int line)
13362{
13363 VALUE mesg;
13364 mesg = rb_str_new_cstr("lex_state: ");
13365 append_lex_state_name(p, from, mesg);
13366 rb_str_cat_cstr(mesg, " -> ");
13367 append_lex_state_name(p, to, mesg);
13368 rb_str_catf(mesg, " at line %d\n", line);
13369 flush_debug_buffer(p, p->debug_output, mesg);
13370 return to;
13371}
13372
13373VALUE
13374rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13375{
13376 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13377}
13378
13379static void
13380append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13381{
13382 if (stack == 0) {
13383 rb_str_cat_cstr(mesg, "0");
13384 }
13385 else {
13386 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13387 for (; mask && !(stack & mask); mask >>= 1) continue;
13388 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13389 }
13390}
13391
13392void
13393rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13394 const char *name, int line)
13395{
13396 VALUE mesg = rb_sprintf("%s: ", name);
13397 append_bitstack_value(p, stack, mesg);
13398 rb_str_catf(mesg, " at line %d\n", line);
13399 flush_debug_buffer(p, p->debug_output, mesg);
13400}
13401
13402void
13403rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13404{
13405 va_list ap;
13406 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13407
13408 va_start(ap, fmt);
13409 rb_str_vcatf(mesg, fmt, ap);
13410 va_end(ap);
13411 yyerror0(RSTRING_PTR(mesg));
13412 RB_GC_GUARD(mesg);
13413
13414 mesg = rb_str_new(0, 0);
13415 append_lex_state_name(p, p->lex.state, mesg);
13416 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13417 rb_str_resize(mesg, 0);
13418 append_bitstack_value(p, p->cond_stack, mesg);
13419 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13420 rb_str_resize(mesg, 0);
13421 append_bitstack_value(p, p->cmdarg_stack, mesg);
13422 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13423 if (p->debug_output == rb_ractor_stdout())
13424 p->debug_output = rb_ractor_stderr();
13425 p->debug = TRUE;
13426}
13427
13428static YYLTYPE *
13429rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13430{
13431 yylloc->beg_pos.lineno = sourceline;
13432 yylloc->beg_pos.column = beg_pos;
13433 yylloc->end_pos.lineno = sourceline;
13434 yylloc->end_pos.column = end_pos;
13435 return yylloc;
13436}
13437
13438YYLTYPE *
13439rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13440{
13441 int sourceline = here->sourceline;
13442 int beg_pos = (int)here->offset - here->quote
13443 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13444 int end_pos = (int)here->offset + here->length + here->quote;
13445
13446 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13447}
13448
13449YYLTYPE *
13450rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13451{
13452 yylloc->beg_pos.lineno = p->delayed.beg_line;
13453 yylloc->beg_pos.column = p->delayed.beg_col;
13454 yylloc->end_pos.lineno = p->delayed.end_line;
13455 yylloc->end_pos.column = p->delayed.end_col;
13456
13457 return yylloc;
13458}
13459
13460YYLTYPE *
13461rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13462{
13463 int sourceline = p->ruby_sourceline;
13464 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13465 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13466 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13467}
13468
13469YYLTYPE *
13470rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13471{
13472 yylloc->end_pos = yylloc->beg_pos;
13473
13474 return yylloc;
13475}
13476
13477YYLTYPE *
13478rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13479{
13480 int sourceline = p->ruby_sourceline;
13481 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13482 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13483 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13484}
13485
13486YYLTYPE *
13487rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13488{
13489 int sourceline = p->ruby_sourceline;
13490 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13491 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13492 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13493}
13494#endif /* !RIPPER */
13495
13496static int
13497assignable0(struct parser_params *p, ID id, const char **err)
13498{
13499 if (!id) return -1;
13500 switch (id) {
13501 case keyword_self:
13502 *err = "Can't change the value of self";
13503 return -1;
13504 case keyword_nil:
13505 *err = "Can't assign to nil";
13506 return -1;
13507 case keyword_true:
13508 *err = "Can't assign to true";
13509 return -1;
13510 case keyword_false:
13511 *err = "Can't assign to false";
13512 return -1;
13513 case keyword__FILE__:
13514 *err = "Can't assign to __FILE__";
13515 return -1;
13516 case keyword__LINE__:
13517 *err = "Can't assign to __LINE__";
13518 return -1;
13519 case keyword__ENCODING__:
13520 *err = "Can't assign to __ENCODING__";
13521 return -1;
13522 }
13523 switch (id_type(id)) {
13524 case ID_LOCAL:
13525 if (dyna_in_block(p)) {
13526 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13527 compile_error(p, "Can't assign to numbered parameter _%d",
13528 NUMPARAM_ID_TO_IDX(id));
13529 return -1;
13530 }
13531 if (dvar_curr(p, id)) return NODE_DASGN;
13532 if (dvar_defined(p, id)) return NODE_DASGN;
13533 if (local_id(p, id)) return NODE_LASGN;
13534 dyna_var(p, id);
13535 return NODE_DASGN;
13536 }
13537 else {
13538 if (!local_id(p, id)) local_var(p, id);
13539 return NODE_LASGN;
13540 }
13541 break;
13542 case ID_GLOBAL: return NODE_GASGN;
13543 case ID_INSTANCE: return NODE_IASGN;
13544 case ID_CONST:
13545 if (!p->ctxt.in_def) return NODE_CDECL;
13546 *err = "dynamic constant assignment";
13547 return -1;
13548 case ID_CLASS: return NODE_CVASGN;
13549 default:
13550 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13551 }
13552 return -1;
13553}
13554
13555static NODE*
13556assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13557{
13558 const char *err = 0;
13559 int node_type = assignable0(p, id, &err);
13560 switch (node_type) {
13561 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13562 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13563 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13564 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13565 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13566 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13567 }
13568/* TODO: FIXME */
13569#ifndef RIPPER
13570 if (err) yyerror1(loc, err);
13571#else
13572 if (err) set_value(assign_error(p, err, p->s_lvalue));
13573#endif
13574 return NEW_ERROR(loc);
13575}
13576
13577static int
13578is_private_local_id(struct parser_params *p, ID name)
13579{
13580 VALUE s;
13581 if (name == idUScore) return 1;
13582 if (!is_local_id(name)) return 0;
13583 s = rb_id2str(name);
13584 if (!s) return 0;
13585 return RSTRING_PTR(s)[0] == '_';
13586}
13587
13588static int
13589shadowing_lvar_0(struct parser_params *p, ID name)
13590{
13591 if (dyna_in_block(p)) {
13592 if (dvar_curr(p, name)) {
13593 if (is_private_local_id(p, name)) return 1;
13594 yyerror0("duplicated argument name");
13595 }
13596 else if (dvar_defined(p, name) || local_id(p, name)) {
13597 vtable_add(p->lvtbl->vars, name);
13598 if (p->lvtbl->used) {
13599 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13600 }
13601 return 0;
13602 }
13603 }
13604 else {
13605 if (local_id(p, name)) {
13606 if (is_private_local_id(p, name)) return 1;
13607 yyerror0("duplicated argument name");
13608 }
13609 }
13610 return 1;
13611}
13612
13613static ID
13614shadowing_lvar(struct parser_params *p, ID name)
13615{
13616 shadowing_lvar_0(p, name);
13617 return name;
13618}
13619
13620static void
13621new_bv(struct parser_params *p, ID name)
13622{
13623 if (!name) return;
13624 if (!is_local_id(name)) {
13625 compile_error(p, "invalid local variable - %"PRIsVALUE,
13626 rb_id2str(name));
13627 return;
13628 }
13629 if (!shadowing_lvar_0(p, name)) return;
13630 dyna_var(p, name);
13631 ID *vidp = 0;
13632 if (dvar_defined_ref(p, name, &vidp)) {
13633 if (vidp) *vidp |= LVAR_USED;
13634 }
13635}
13636
13637static void
13638aryset_check(struct parser_params *p, NODE *args)
13639{
13640 NODE *block = 0, *kwds = 0;
13641 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13642 block = RNODE_BLOCK_PASS(args)->nd_body;
13643 args = RNODE_BLOCK_PASS(args)->nd_head;
13644 }
13645 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13646 args = RNODE_ARGSCAT(args)->nd_body;
13647 }
13648 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13649 kwds = RNODE_ARGSPUSH(args)->nd_body;
13650 }
13651 else {
13652 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13653 next = RNODE_LIST(next)->nd_next) {
13654 kwds = RNODE_LIST(next)->nd_head;
13655 }
13656 }
13657 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13658 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13659 }
13660 if (block) {
13661 yyerror1(&block->nd_loc, "block arg given in index assignment");
13662 }
13663}
13664
13665static NODE *
13666aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13667{
13668 aryset_check(p, idx);
13669 return NEW_ATTRASGN(recv, tASET, idx, loc);
13670}
13671
13672static void
13673block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13674{
13675 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13676 compile_error(p, "both block arg and actual block given");
13677 }
13678}
13679
13680static NODE *
13681attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13682{
13683 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13684 return NEW_ATTRASGN(recv, id, 0, loc);
13685}
13686
13687static VALUE
13688rb_backref_error(struct parser_params *p, NODE *node)
13689{
13690#ifndef RIPPER
13691# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13692#else
13693# define ERR(...) rb_sprintf(__VA_ARGS__)
13694#endif
13695 switch (nd_type(node)) {
13696 case NODE_NTH_REF:
13697 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13698 case NODE_BACK_REF:
13699 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13700 }
13701#undef ERR
13702 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13703}
13704
13705static NODE *
13706arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13707{
13708 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13709 switch (nd_type(node1)) {
13710 case NODE_LIST:
13711 return list_append(p, node1, node2);
13712 case NODE_BLOCK_PASS:
13713 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13714 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13715 return node1;
13716 case NODE_ARGSPUSH:
13717 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13718 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13719 nd_set_type(node1, NODE_ARGSCAT);
13720 return node1;
13721 case NODE_ARGSCAT:
13722 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13723 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13724 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13725 return node1;
13726 }
13727 return NEW_ARGSPUSH(node1, node2, loc);
13728}
13729
13730static NODE *
13731arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13732{
13733 if (!node2) return node1;
13734 switch (nd_type(node1)) {
13735 case NODE_BLOCK_PASS:
13736 if (RNODE_BLOCK_PASS(node1)->nd_head)
13737 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13738 else
13739 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13740 return node1;
13741 case NODE_ARGSPUSH:
13742 if (!nd_type_p(node2, NODE_LIST)) break;
13743 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13744 nd_set_type(node1, NODE_ARGSCAT);
13745 return node1;
13746 case NODE_ARGSCAT:
13747 if (!nd_type_p(node2, NODE_LIST) ||
13748 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13749 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13750 return node1;
13751 }
13752 return NEW_ARGSCAT(node1, node2, loc);
13753}
13754
13755static NODE *
13756last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13757{
13758 NODE *n1;
13759 if ((n1 = splat_array(args)) != 0) {
13760 return list_append(p, n1, last_arg);
13761 }
13762 return arg_append(p, args, last_arg, loc);
13763}
13764
13765static NODE *
13766rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13767{
13768 NODE *n1;
13769 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13770 return list_concat(n1, rest_arg);
13771 }
13772 return arg_concat(p, args, rest_arg, loc);
13773}
13774
13775static NODE *
13776splat_array(NODE* node)
13777{
13778 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13779 if (nd_type_p(node, NODE_LIST)) return node;
13780 return 0;
13781}
13782
13783static void
13784mark_lvar_used(struct parser_params *p, NODE *rhs)
13785{
13786 ID *vidp = NULL;
13787 if (!rhs) return;
13788 switch (nd_type(rhs)) {
13789 case NODE_LASGN:
13790 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13791 if (vidp) *vidp |= LVAR_USED;
13792 }
13793 break;
13794 case NODE_DASGN:
13795 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13796 if (vidp) *vidp |= LVAR_USED;
13797 }
13798 break;
13799#if 0
13800 case NODE_MASGN:
13801 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13802 mark_lvar_used(p, rhs->nd_head);
13803 }
13804 break;
13805#endif
13806 }
13807}
13808
13809static int is_static_content(NODE *node);
13810
13811static NODE *
13812node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13813{
13814 if (!lhs) return 0;
13815
13816 switch (nd_type(lhs)) {
13817 case NODE_CDECL:
13818 case NODE_GASGN:
13819 case NODE_IASGN:
13820 case NODE_LASGN:
13821 case NODE_DASGN:
13822 case NODE_MASGN:
13823 case NODE_CVASGN:
13824 set_nd_value(p, lhs, rhs);
13825 nd_set_loc(lhs, loc);
13826 break;
13827
13828 case NODE_ATTRASGN:
13829 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13830 nd_set_loc(lhs, loc);
13831 break;
13832
13833 default:
13834 /* should not happen */
13835 break;
13836 }
13837
13838 return lhs;
13839}
13840
13841static NODE *
13842value_expr_check(struct parser_params *p, NODE *node)
13843{
13844 NODE *void_node = 0, *vn;
13845
13846 if (!node) {
13847 rb_warning0("empty expression");
13848 }
13849 while (node) {
13850 switch (nd_type(node)) {
13851 case NODE_ENSURE:
13852 vn = RNODE_ENSURE(node)->nd_head;
13853 node = RNODE_ENSURE(node)->nd_ensr;
13854 /* nd_ensr should not be NULL, check it out next */
13855 if (vn && (vn = value_expr_check(p, vn))) {
13856 goto found;
13857 }
13858 break;
13859
13860 case NODE_RESCUE:
13861 /* void only if all children are void */
13862 vn = RNODE_RESCUE(node)->nd_head;
13863 if (!vn || !(vn = value_expr_check(p, vn))) return NULL;
13864 if (!void_node) void_node = vn;
13865 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13866 if (!nd_type_p(r, NODE_RESBODY)) {
13867 compile_error(p, "unexpected node");
13868 return NULL;
13869 }
13870 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13871 void_node = 0;
13872 break;
13873 }
13874 if (!void_node) void_node = vn;
13875 }
13876 node = RNODE_RESCUE(node)->nd_else;
13877 if (!node) return void_node;
13878 break;
13879
13880 case NODE_RETURN:
13881 case NODE_BREAK:
13882 case NODE_NEXT:
13883 case NODE_REDO:
13884 case NODE_RETRY:
13885 goto found;
13886
13887 case NODE_CASE3:
13888 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13889 compile_error(p, "unexpected node");
13890 return NULL;
13891 }
13892 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13893 return NULL;
13894 }
13895 /* single line pattern matching with "=>" operator */
13896 goto found;
13897
13898 case NODE_BLOCK:
13899 while (RNODE_BLOCK(node)->nd_next) {
13900 node = RNODE_BLOCK(node)->nd_next;
13901 }
13902 node = RNODE_BLOCK(node)->nd_head;
13903 break;
13904
13905 case NODE_BEGIN:
13906 node = RNODE_BEGIN(node)->nd_body;
13907 break;
13908
13909 case NODE_IF:
13910 case NODE_UNLESS:
13911 if (!RNODE_IF(node)->nd_body) {
13912 return NULL;
13913 }
13914 else if (!RNODE_IF(node)->nd_else) {
13915 return NULL;
13916 }
13917 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13918 if (!vn) return NULL;
13919 if (!void_node) void_node = vn;
13920 node = RNODE_IF(node)->nd_else;
13921 break;
13922
13923 case NODE_AND:
13924 case NODE_OR:
13925 node = RNODE_AND(node)->nd_1st;
13926 break;
13927
13928 case NODE_LASGN:
13929 case NODE_DASGN:
13930 case NODE_MASGN:
13931 mark_lvar_used(p, node);
13932 return NULL;
13933
13934 default:
13935 return NULL;
13936 }
13937 }
13938
13939 return NULL;
13940
13941 found:
13942 /* return the first found node */
13943 return void_node ? void_node : node;
13944}
13945
13946static int
13947value_expr_gen(struct parser_params *p, NODE *node)
13948{
13949 NODE *void_node = value_expr_check(p, node);
13950 if (void_node) {
13951 yyerror1(&void_node->nd_loc, "void value expression");
13952 /* or "control never reach"? */
13953 return FALSE;
13954 }
13955 return TRUE;
13956}
13957
13958static void
13959void_expr(struct parser_params *p, NODE *node)
13960{
13961 const char *useless = 0;
13962
13963 if (!RTEST(ruby_verbose)) return;
13964
13965 if (!node || !(node = nd_once_body(node))) return;
13966 switch (nd_type(node)) {
13967 case NODE_OPCALL:
13968 switch (RNODE_OPCALL(node)->nd_mid) {
13969 case '+':
13970 case '-':
13971 case '*':
13972 case '/':
13973 case '%':
13974 case tPOW:
13975 case tUPLUS:
13976 case tUMINUS:
13977 case '|':
13978 case '^':
13979 case '&':
13980 case tCMP:
13981 case '>':
13982 case tGEQ:
13983 case '<':
13984 case tLEQ:
13985 case tEQ:
13986 case tNEQ:
13987 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13988 break;
13989 }
13990 break;
13991
13992 case NODE_LVAR:
13993 case NODE_DVAR:
13994 case NODE_GVAR:
13995 case NODE_IVAR:
13996 case NODE_CVAR:
13997 case NODE_NTH_REF:
13998 case NODE_BACK_REF:
13999 useless = "a variable";
14000 break;
14001 case NODE_CONST:
14002 useless = "a constant";
14003 break;
14004 case NODE_SYM:
14005 case NODE_LINE:
14006 case NODE_FILE:
14007 case NODE_ENCODING:
14008 case NODE_INTEGER:
14009 case NODE_FLOAT:
14010 case NODE_RATIONAL:
14011 case NODE_IMAGINARY:
14012 case NODE_STR:
14013 case NODE_DSTR:
14014 case NODE_REGX:
14015 case NODE_DREGX:
14016 useless = "a literal";
14017 break;
14018 case NODE_COLON2:
14019 case NODE_COLON3:
14020 useless = "::";
14021 break;
14022 case NODE_DOT2:
14023 useless = "..";
14024 break;
14025 case NODE_DOT3:
14026 useless = "...";
14027 break;
14028 case NODE_SELF:
14029 useless = "self";
14030 break;
14031 case NODE_NIL:
14032 useless = "nil";
14033 break;
14034 case NODE_TRUE:
14035 useless = "true";
14036 break;
14037 case NODE_FALSE:
14038 useless = "false";
14039 break;
14040 case NODE_DEFINED:
14041 useless = "defined?";
14042 break;
14043 }
14044
14045 if (useless) {
14046 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14047 }
14048}
14049
14050/* warns useless use of block and returns the last statement node */
14051static NODE *
14052void_stmts(struct parser_params *p, NODE *node)
14053{
14054 NODE *const n = node;
14055 if (!RTEST(ruby_verbose)) return n;
14056 if (!node) return n;
14057 if (!nd_type_p(node, NODE_BLOCK)) return n;
14058
14059 while (RNODE_BLOCK(node)->nd_next) {
14060 void_expr(p, RNODE_BLOCK(node)->nd_head);
14061 node = RNODE_BLOCK(node)->nd_next;
14062 }
14063 return RNODE_BLOCK(node)->nd_head;
14064}
14065
14066static NODE *
14067remove_begin(NODE *node)
14068{
14069 NODE **n = &node, *n1 = node;
14070 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14071 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14072 }
14073 return node;
14074}
14075
14076static void
14077reduce_nodes(struct parser_params *p, NODE **body)
14078{
14079 NODE *node = *body;
14080
14081 if (!node) {
14082 *body = NEW_NIL(&NULL_LOC);
14083 return;
14084 }
14085#define subnodes(type, n1, n2) \
14086 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14087 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14088 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14089
14090 while (node) {
14091 int newline = (int)nd_fl_newline(node);
14092 switch (nd_type(node)) {
14093 end:
14094 case NODE_NIL:
14095 *body = 0;
14096 return;
14097 case NODE_BEGIN:
14098 *body = node = RNODE_BEGIN(node)->nd_body;
14099 if (newline && node) nd_set_fl_newline(node);
14100 continue;
14101 case NODE_BLOCK:
14102 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14103 break;
14104 case NODE_IF:
14105 case NODE_UNLESS:
14106 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14107 return;
14108 case NODE_CASE:
14109 body = &RNODE_CASE(node)->nd_body;
14110 break;
14111 case NODE_WHEN:
14112 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14113 break;
14114 case NODE_ENSURE:
14115 body = &RNODE_ENSURE(node)->nd_head;
14116 break;
14117 case NODE_RESCUE:
14118 newline = 0; // RESBODY should not be a NEWLINE
14119 if (RNODE_RESCUE(node)->nd_else) {
14120 body = &RNODE_RESCUE(node)->nd_resq;
14121 break;
14122 }
14123 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14124 break;
14125 default:
14126 return;
14127 }
14128 node = *body;
14129 if (newline && node) nd_set_fl_newline(node);
14130 }
14131
14132#undef subnodes
14133}
14134
14135static int
14136is_static_content(NODE *node)
14137{
14138 if (!node) return 1;
14139 switch (nd_type(node)) {
14140 case NODE_HASH:
14141 if (!(node = RNODE_HASH(node)->nd_head)) break;
14142 case NODE_LIST:
14143 do {
14144 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14145 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14146 case NODE_SYM:
14147 case NODE_REGX:
14148 case NODE_LINE:
14149 case NODE_FILE:
14150 case NODE_ENCODING:
14151 case NODE_INTEGER:
14152 case NODE_FLOAT:
14153 case NODE_RATIONAL:
14154 case NODE_IMAGINARY:
14155 case NODE_STR:
14156 case NODE_NIL:
14157 case NODE_TRUE:
14158 case NODE_FALSE:
14159 case NODE_ZLIST:
14160 break;
14161 default:
14162 return 0;
14163 }
14164 return 1;
14165}
14166
14167static int
14168assign_in_cond(struct parser_params *p, NODE *node)
14169{
14170 switch (nd_type(node)) {
14171 case NODE_MASGN:
14172 case NODE_LASGN:
14173 case NODE_DASGN:
14174 case NODE_GASGN:
14175 case NODE_IASGN:
14176 case NODE_CVASGN:
14177 case NODE_CDECL:
14178 break;
14179
14180 default:
14181 return 0;
14182 }
14183
14184 if (!get_nd_value(p, node)) return 1;
14185 if (is_static_content(get_nd_value(p, node))) {
14186 /* reports always */
14187 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14188 }
14189 return 1;
14190}
14191
14192enum cond_type {
14193 COND_IN_OP,
14194 COND_IN_COND,
14195 COND_IN_FF
14196};
14197
14198#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14199 switch (t) { \
14200 case COND_IN_OP: break; \
14201 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14202 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14203 } \
14204} while (0)
14205
14206static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14207
14208static NODE*
14209range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14210{
14211 enum node_type type;
14212
14213 if (node == 0) return 0;
14214
14215 type = nd_type(node);
14216 value_expr(node);
14217 if (type == NODE_INTEGER) {
14218 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14219 ID lineno = rb_intern("$.");
14220 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14221 }
14222 return cond0(p, node, COND_IN_FF, loc, true);
14223}
14224
14225static NODE*
14226cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14227{
14228 if (node == 0) return 0;
14229 if (!(node = nd_once_body(node))) return 0;
14230 assign_in_cond(p, node);
14231
14232 switch (nd_type(node)) {
14233 case NODE_BEGIN:
14234 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14235 break;
14236
14237 case NODE_DSTR:
14238 case NODE_EVSTR:
14239 case NODE_STR:
14240 case NODE_FILE:
14241 SWITCH_BY_COND_TYPE(type, warn, "string ");
14242 break;
14243
14244 case NODE_REGX:
14245 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14246 nd_set_type(node, NODE_MATCH);
14247 break;
14248
14249 case NODE_DREGX:
14250 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14251
14252 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14253
14254 case NODE_BLOCK:
14255 {
14256 NODE *end = RNODE_BLOCK(node)->nd_end;
14257 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14258 if (top) top = node == end;
14259 *expr = cond0(p, *expr, type, loc, top);
14260 }
14261 break;
14262
14263 case NODE_AND:
14264 case NODE_OR:
14265 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14266 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14267 break;
14268
14269 case NODE_DOT2:
14270 case NODE_DOT3:
14271 if (!top) break;
14272 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14273 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14274 switch (nd_type(node)) {
14275 case NODE_DOT2:
14276 nd_set_type(node,NODE_FLIP2);
14277 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14278 (void)flip2;
14279 break;
14280 case NODE_DOT3:
14281 nd_set_type(node, NODE_FLIP3);
14282 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14283 (void)flip3;
14284 break;
14285 }
14286 break;
14287
14288 case NODE_SYM:
14289 case NODE_DSYM:
14290 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14291 break;
14292
14293 case NODE_LINE:
14294 SWITCH_BY_COND_TYPE(type, warning, "");
14295 break;
14296
14297 case NODE_ENCODING:
14298 SWITCH_BY_COND_TYPE(type, warning, "");
14299 break;
14300
14301 case NODE_INTEGER:
14302 case NODE_FLOAT:
14303 case NODE_RATIONAL:
14304 case NODE_IMAGINARY:
14305 SWITCH_BY_COND_TYPE(type, warning, "");
14306 break;
14307
14308 default:
14309 break;
14310 }
14311 return node;
14312}
14313
14314static NODE*
14315cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14316{
14317 if (node == 0) return 0;
14318 return cond0(p, node, COND_IN_COND, loc, true);
14319}
14320
14321static NODE*
14322method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14323{
14324 if (node == 0) return 0;
14325 return cond0(p, node, COND_IN_OP, loc, true);
14326}
14327
14328static NODE*
14329new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14330{
14331 YYLTYPE loc = {*pos, *pos};
14332 return NEW_NIL(&loc);
14333}
14334
14335static NODE*
14336new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14337{
14338 if (!cc) return right;
14339 cc = cond0(p, cc, COND_IN_COND, loc, true);
14340 return newline_node(NEW_IF(cc, left, right, loc));
14341}
14342
14343static NODE*
14344new_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)
14345{
14346 if (!cc) return right;
14347 cc = cond0(p, cc, COND_IN_COND, loc, true);
14348 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14349}
14350
14351#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))
14352
14353static NODE*
14354logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14355 const YYLTYPE *op_loc, const YYLTYPE *loc)
14356{
14357 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14358 NODE *op;
14359 value_expr(left);
14360 if (left && nd_type_p(left, type)) {
14361 NODE *node = left, *second;
14362 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14363 node = second;
14364 }
14365 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14366 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14367 left->nd_loc.end_pos = loc->end_pos;
14368 return left;
14369 }
14370 op = NEW_AND_OR(type, left, right, loc, op_loc);
14371 nd_set_line(op, op_loc->beg_pos.lineno);
14372 return op;
14373}
14374
14375#undef NEW_AND_OR
14376
14377static void
14378no_blockarg(struct parser_params *p, NODE *node)
14379{
14380 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14381 compile_error(p, "block argument should not be given");
14382 }
14383}
14384
14385static NODE *
14386ret_args(struct parser_params *p, NODE *node)
14387{
14388 if (node) {
14389 no_blockarg(p, node);
14390 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14391 node = RNODE_LIST(node)->nd_head;
14392 }
14393 }
14394 return node;
14395}
14396
14397static NODE *
14398new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14399{
14400 if (node) no_blockarg(p, node);
14401
14402 return NEW_YIELD(node, loc);
14403}
14404
14405static NODE*
14406negate_lit(struct parser_params *p, NODE* node)
14407{
14408 switch (nd_type(node)) {
14409 case NODE_INTEGER:
14410 RNODE_INTEGER(node)->minus = TRUE;
14411 break;
14412 case NODE_FLOAT:
14413 RNODE_FLOAT(node)->minus = TRUE;
14414 break;
14415 case NODE_RATIONAL:
14416 RNODE_RATIONAL(node)->minus = TRUE;
14417 break;
14418 case NODE_IMAGINARY:
14419 RNODE_IMAGINARY(node)->minus = TRUE;
14420 break;
14421 }
14422 return node;
14423}
14424
14425static NODE *
14426arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14427{
14428 if (node2) {
14429 if (!node1) return (NODE *)node2;
14430 node2->nd_head = node1;
14431 nd_set_first_lineno(node2, nd_first_lineno(node1));
14432 nd_set_first_column(node2, nd_first_column(node1));
14433 return (NODE *)node2;
14434 }
14435 return node1;
14436}
14437
14438static bool
14439args_info_empty_p(struct rb_args_info *args)
14440{
14441 if (args->pre_args_num) return false;
14442 if (args->post_args_num) return false;
14443 if (args->rest_arg) return false;
14444 if (args->opt_args) return false;
14445 if (args->block_arg) return false;
14446 if (args->kw_args) return false;
14447 if (args->kw_rest_arg) return false;
14448 return true;
14449}
14450
14451static rb_node_args_t *
14452new_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)
14453{
14454 struct rb_args_info *args = &tail->nd_ainfo;
14455
14456 if (args->forwarding) {
14457 if (rest_arg) {
14458 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14459 return tail;
14460 }
14461 rest_arg = idFWD_REST;
14462 }
14463
14464 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14465 args->pre_init = pre_args ? pre_args->nd_next : 0;
14466
14467 args->post_args_num = post_args ? post_args->nd_plen : 0;
14468 args->post_init = post_args ? post_args->nd_next : 0;
14469 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14470
14471 args->rest_arg = rest_arg;
14472
14473 args->opt_args = opt_args;
14474
14475#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14476 args->ruby2_keywords = args->forwarding;
14477#else
14478 args->ruby2_keywords = 0;
14479#endif
14480
14481 nd_set_loc(RNODE(tail), loc);
14482
14483 return tail;
14484}
14485
14486static rb_node_args_t *
14487new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14488{
14489 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14490 struct rb_args_info *args = &node->nd_ainfo;
14491 if (p->error_p) return node;
14492
14493 args->block_arg = block;
14494 args->kw_args = kw_args;
14495
14496 if (kw_args) {
14497 /*
14498 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14499 * variable order: k1, kr1, k2, &b, internal_id, krest
14500 * #=> <reorder>
14501 * variable order: kr1, k1, k2, internal_id, krest, &b
14502 */
14503 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14504 struct vtable *vtargs = p->lvtbl->args;
14505 rb_node_kw_arg_t *kwn = kw_args;
14506
14507 if (block) block = vtargs->tbl[vtargs->pos-1];
14508 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14509 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14510 while (kwn) {
14511 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14512 --kw_vars;
14513 --required_kw_vars;
14514 kwn = kwn->nd_next;
14515 }
14516
14517 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14518 ID vid = get_nd_vid(p, kwn->nd_body);
14519 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14520 *required_kw_vars++ = vid;
14521 }
14522 else {
14523 *kw_vars++ = vid;
14524 }
14525 }
14526
14527 arg_var(p, kw_bits);
14528 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14529 if (block) arg_var(p, block);
14530
14531 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14532 }
14533 else if (kw_rest_arg == idNil) {
14534 args->no_kwarg = 1;
14535 }
14536 else if (kw_rest_arg) {
14537 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14538 }
14539
14540 return node;
14541}
14542
14543static rb_node_args_t *
14544args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14545{
14546 if (max_numparam > NO_PARAM || it_id) {
14547 if (!args) {
14548 YYLTYPE loc = RUBY_INIT_YYLLOC();
14549 args = new_args_tail(p, 0, 0, 0, 0);
14550 nd_set_loc(RNODE(args), &loc);
14551 }
14552 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14553 }
14554 return args;
14555}
14556
14557static NODE*
14558new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14559{
14560 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14561
14562 if (pre_arg) {
14563 NODE *pre_args = NEW_LIST(pre_arg, loc);
14564 if (RNODE_ARYPTN(aryptn)->pre_args) {
14565 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14566 }
14567 else {
14568 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14569 }
14570 }
14571 return aryptn;
14572}
14573
14574static NODE*
14575new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14576{
14577 if (has_rest) {
14578 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14579 }
14580 else {
14581 rest_arg = NULL;
14582 }
14583 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14584
14585 return node;
14586}
14587
14588static NODE*
14589new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14590{
14591 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14592
14593 return fndptn;
14594}
14595
14596static NODE*
14597new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14598{
14599 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14600 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14601 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14602
14603 return node;
14604}
14605
14606static NODE*
14607new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14608{
14609 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14610 return hshptn;
14611}
14612
14613static NODE*
14614new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14615{
14616 NODE *node, *kw_rest_arg_node;
14617
14618 if (kw_rest_arg == idNil) {
14619 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14620 }
14621 else if (kw_rest_arg) {
14622 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14623 }
14624 else {
14625 kw_rest_arg_node = NULL;
14626 }
14627
14628 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14629
14630 return node;
14631}
14632
14633static NODE*
14634dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14635{
14636 if (!node) {
14637 return NEW_SYM(STR_NEW0(), loc);
14638 }
14639
14640 switch (nd_type(node)) {
14641 case NODE_DSTR:
14642 nd_set_type(node, NODE_DSYM);
14643 nd_set_loc(node, loc);
14644 break;
14645 case NODE_STR:
14646 node = str_to_sym_node(p, node, loc);
14647 break;
14648 default:
14649 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14650 break;
14651 }
14652 return node;
14653}
14654
14655static int
14656nd_type_st_key_enable_p(NODE *node)
14657{
14658 switch (nd_type(node)) {
14659 case NODE_INTEGER:
14660 case NODE_FLOAT:
14661 case NODE_RATIONAL:
14662 case NODE_IMAGINARY:
14663 case NODE_STR:
14664 case NODE_SYM:
14665 case NODE_REGX:
14666 case NODE_LINE:
14667 case NODE_FILE:
14668 case NODE_ENCODING:
14669 return true;
14670 default:
14671 return false;
14672 }
14673}
14674
14675static VALUE
14676nd_value(struct parser_params *p, NODE *node)
14677{
14678 switch (nd_type(node)) {
14679 case NODE_STR:
14680 return rb_node_str_string_val(node);
14681 case NODE_INTEGER:
14682 return rb_node_integer_literal_val(node);
14683 case NODE_FLOAT:
14684 return rb_node_float_literal_val(node);
14685 case NODE_RATIONAL:
14686 return rb_node_rational_literal_val(node);
14687 case NODE_IMAGINARY:
14688 return rb_node_imaginary_literal_val(node);
14689 case NODE_SYM:
14690 return rb_node_sym_string_val(node);
14691 case NODE_REGX:
14692 return rb_node_regx_string_val(node);
14693 case NODE_LINE:
14694 return rb_node_line_lineno_val(node);
14695 case NODE_ENCODING:
14696 return rb_node_encoding_val(node);
14697 case NODE_FILE:
14698 return rb_node_file_path_val(node);
14699 default:
14700 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14701 UNREACHABLE_RETURN(0);
14702 }
14703}
14704
14705static void
14706warn_duplicate_keys(struct parser_params *p, NODE *hash)
14707{
14708 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14709 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14710 while (hash && RNODE_LIST(hash)->nd_next) {
14711 NODE *head = RNODE_LIST(hash)->nd_head;
14712 NODE *value = RNODE_LIST(hash)->nd_next;
14713 NODE *next = RNODE_LIST(value)->nd_next;
14714 st_data_t key;
14715 st_data_t data;
14716
14717 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14718 if (!head) {
14719 head = value;
14720 }
14721
14722 if (nd_type_st_key_enable_p(head)) {
14723 key = (st_data_t)head;
14724
14725 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14726 rb_warn2L(nd_line((NODE *)data),
14727 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14728 nd_value(p, head), WARN_I(nd_line(head)));
14729 }
14730 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14731 }
14732 hash = next;
14733 }
14734 st_free_table(p->warn_duplicate_keys_table);
14735 p->warn_duplicate_keys_table = NULL;
14736}
14737
14738static NODE *
14739new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14740{
14741 if (hash) warn_duplicate_keys(p, hash);
14742 return NEW_HASH(hash, loc);
14743}
14744
14745static void
14746error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14747{
14748 if (is_private_local_id(p, id)) {
14749 return;
14750 }
14751 if (st_is_member(p->pvtbl, id)) {
14752 yyerror1(loc, "duplicated variable name");
14753 }
14754 else {
14755 st_insert(p->pvtbl, (st_data_t)id, 0);
14756 }
14757}
14758
14759static void
14760error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14761{
14762 if (!p->pktbl) {
14763 p->pktbl = st_init_numtable();
14764 }
14765 else if (st_is_member(p->pktbl, key)) {
14766 yyerror1(loc, "duplicated key name");
14767 return;
14768 }
14769 st_insert(p->pktbl, (st_data_t)key, 0);
14770}
14771
14772static NODE *
14773new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14774{
14775 return NEW_HASH(hash, loc);
14776}
14777
14778static NODE *
14779new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14780{
14781 NODE *asgn;
14782
14783 if (lhs) {
14784 ID vid = get_nd_vid(p, lhs);
14785 YYLTYPE lhs_loc = lhs->nd_loc;
14786 if (op == tOROP) {
14787 set_nd_value(p, lhs, rhs);
14788 nd_set_loc(lhs, loc);
14789 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14790 }
14791 else if (op == tANDOP) {
14792 set_nd_value(p, lhs, rhs);
14793 nd_set_loc(lhs, loc);
14794 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14795 }
14796 else {
14797 asgn = lhs;
14798 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14799 set_nd_value(p, asgn, rhs);
14800 nd_set_loc(asgn, loc);
14801 }
14802 }
14803 else {
14804 asgn = NEW_ERROR(loc);
14805 }
14806 return asgn;
14807}
14808
14809static NODE *
14810new_ary_op_assign(struct parser_params *p, NODE *ary,
14811 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14812 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14813{
14814 NODE *asgn;
14815
14816 aryset_check(p, args);
14817 args = make_list(args, args_loc);
14818 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14819 fixpos(asgn, ary);
14820 return asgn;
14821}
14822
14823static NODE *
14824new_attr_op_assign(struct parser_params *p, NODE *lhs,
14825 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14826 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14827{
14828 NODE *asgn;
14829
14830 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14831 fixpos(asgn, lhs);
14832 return asgn;
14833}
14834
14835static NODE *
14836new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14837{
14838 NODE *asgn;
14839
14840 if (lhs) {
14841 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14842 }
14843 else {
14844 asgn = NEW_ERROR(loc);
14845 }
14846 fixpos(asgn, lhs);
14847 return asgn;
14848}
14849
14850static NODE *
14851const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14852{
14853 if (p->ctxt.in_def) {
14854#ifndef RIPPER
14855 yyerror1(loc, "dynamic constant assignment");
14856#else
14857 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14858#endif
14859 }
14860 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14861}
14862
14863#ifdef RIPPER
14864static VALUE
14865assign_error(struct parser_params *p, const char *mesg, VALUE a)
14866{
14867 a = dispatch2(assign_error, ERR_MESG(), a);
14868 ripper_error(p);
14869 return a;
14870}
14871#endif
14872
14873static NODE *
14874new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14875{
14876 NODE *result = head;
14877 if (rescue) {
14878 NODE *tmp = rescue_else ? rescue_else : rescue;
14879 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14880
14881 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14882 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14883 }
14884 if (ensure) {
14885 result = NEW_ENSURE(result, ensure, loc);
14886 }
14887 fixpos(result, head);
14888 return result;
14889}
14890
14891static void
14892warn_unused_var(struct parser_params *p, struct local_vars *local)
14893{
14894 int cnt;
14895
14896 if (!local->used) return;
14897 cnt = local->used->pos;
14898 if (cnt != local->vars->pos) {
14899 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14900 }
14901#ifndef RIPPER
14902 ID *v = local->vars->tbl;
14903 ID *u = local->used->tbl;
14904 for (int i = 0; i < cnt; ++i) {
14905 if (!v[i] || (u[i] & LVAR_USED)) continue;
14906 if (is_private_local_id(p, v[i])) continue;
14907 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14908 }
14909#endif
14910}
14911
14912static void
14913local_push(struct parser_params *p, int toplevel_scope)
14914{
14915 struct local_vars *local;
14916 int inherits_dvars = toplevel_scope && compile_for_eval;
14917 int warn_unused_vars = RTEST(ruby_verbose);
14918
14919 local = ALLOC(struct local_vars);
14920 local->prev = p->lvtbl;
14921 local->args = vtable_alloc(0);
14922 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14923#ifndef RIPPER
14924 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14925 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14926#endif
14927 local->numparam.outer = 0;
14928 local->numparam.inner = 0;
14929 local->numparam.current = 0;
14930 local->it = 0;
14931 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14932
14933# if WARN_PAST_SCOPE
14934 local->past = 0;
14935# endif
14936 CMDARG_PUSH(0);
14937 COND_PUSH(0);
14938 p->lvtbl = local;
14939}
14940
14941static void
14942vtable_chain_free(struct parser_params *p, struct vtable *table)
14943{
14944 while (!DVARS_TERMINAL_P(table)) {
14945 struct vtable *cur_table = table;
14946 table = cur_table->prev;
14947 vtable_free(cur_table);
14948 }
14949}
14950
14951static void
14952local_free(struct parser_params *p, struct local_vars *local)
14953{
14954 vtable_chain_free(p, local->used);
14955
14956# if WARN_PAST_SCOPE
14957 vtable_chain_free(p, local->past);
14958# endif
14959
14960 vtable_chain_free(p, local->args);
14961 vtable_chain_free(p, local->vars);
14962
14963 ruby_sized_xfree(local, sizeof(struct local_vars));
14964}
14965
14966static void
14967local_pop(struct parser_params *p)
14968{
14969 struct local_vars *local = p->lvtbl->prev;
14970 if (p->lvtbl->used) {
14971 warn_unused_var(p, p->lvtbl);
14972 }
14973
14974 local_free(p, p->lvtbl);
14975 p->lvtbl = local;
14976
14977 CMDARG_POP();
14978 COND_POP();
14979}
14980
14981static rb_ast_id_table_t *
14982local_tbl(struct parser_params *p)
14983{
14984 int cnt_args = vtable_size(p->lvtbl->args);
14985 int cnt_vars = vtable_size(p->lvtbl->vars);
14986 int cnt = cnt_args + cnt_vars;
14987 int i, j;
14988 rb_ast_id_table_t *tbl;
14989
14990 if (cnt <= 0) return 0;
14991 tbl = rb_ast_new_local_table(p->ast, cnt);
14992 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14993 /* remove IDs duplicated to warn shadowing */
14994 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14995 ID id = p->lvtbl->vars->tbl[i];
14996 if (!vtable_included(p->lvtbl->args, id)) {
14997 tbl->ids[j++] = id;
14998 }
14999 }
15000 if (j < cnt) {
15001 tbl = rb_ast_resize_latest_local_table(p->ast, j);
15002 }
15003
15004 return tbl;
15005}
15006
15007static void
15008numparam_name(struct parser_params *p, ID id)
15009{
15010 if (!NUMPARAM_ID_P(id)) return;
15011 compile_error(p, "_%d is reserved for numbered parameter",
15012 NUMPARAM_ID_TO_IDX(id));
15013}
15014
15015static void
15016arg_var(struct parser_params *p, ID id)
15017{
15018 numparam_name(p, id);
15019 vtable_add(p->lvtbl->args, id);
15020}
15021
15022static void
15023local_var(struct parser_params *p, ID id)
15024{
15025 numparam_name(p, id);
15026 vtable_add(p->lvtbl->vars, id);
15027 if (p->lvtbl->used) {
15028 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15029 }
15030}
15031
15032#ifndef RIPPER
15033int
15034rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15035{
15036 return rb_local_defined(id, iseq);
15037}
15038#endif
15039
15040static int
15041local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15042{
15043 struct vtable *vars, *args, *used;
15044
15045 vars = p->lvtbl->vars;
15046 args = p->lvtbl->args;
15047 used = p->lvtbl->used;
15048
15049 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15050 vars = vars->prev;
15051 args = args->prev;
15052 if (used) used = used->prev;
15053 }
15054
15055 if (vars && vars->prev == DVARS_INHERIT) {
15056 return rb_parser_local_defined(p, id, p->parent_iseq);
15057 }
15058 else if (vtable_included(args, id)) {
15059 return 1;
15060 }
15061 else {
15062 int i = vtable_included(vars, id);
15063 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15064 return i != 0;
15065 }
15066}
15067
15068static int
15069local_id(struct parser_params *p, ID id)
15070{
15071 return local_id_ref(p, id, NULL);
15072}
15073
15074static int
15075check_forwarding_args(struct parser_params *p)
15076{
15077 if (local_id(p, idFWD_ALL)) return TRUE;
15078 compile_error(p, "unexpected ...");
15079 return FALSE;
15080}
15081
15082static void
15083add_forwarding_args(struct parser_params *p)
15084{
15085 arg_var(p, idFWD_REST);
15086#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15087 arg_var(p, idFWD_KWREST);
15088#endif
15089 arg_var(p, idFWD_BLOCK);
15090 arg_var(p, idFWD_ALL);
15091}
15092
15093static void
15094forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15095{
15096 bool conflict = false;
15097
15098 struct vtable *vars, *args;
15099
15100 vars = p->lvtbl->vars;
15101 args = p->lvtbl->args;
15102
15103 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15104 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15105 vars = vars->prev;
15106 args = args->prev;
15107 }
15108
15109 bool found = false;
15110 if (vars && vars->prev == DVARS_INHERIT && !found) {
15111 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15112 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15113 }
15114 else {
15115 found = (vtable_included(args, arg) &&
15116 !(all && vtable_included(args, all)));
15117 }
15118
15119 if (!found) {
15120 compile_error(p, "no anonymous %s parameter", var);
15121 }
15122 else if (conflict) {
15123 compile_error(p, "anonymous %s parameter is also used within block", var);
15124 }
15125}
15126
15127static NODE *
15128new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15129{
15130 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15131#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15132 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15133#endif
15134 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15135 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15136 block->forwarding = TRUE;
15137#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15138 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15139#endif
15140 return arg_blk_pass(args, block);
15141}
15142
15143static NODE *
15144numparam_push(struct parser_params *p)
15145{
15146 struct local_vars *local = p->lvtbl;
15147 NODE *inner = local->numparam.inner;
15148 if (!local->numparam.outer) {
15149 local->numparam.outer = local->numparam.current;
15150 }
15151 local->numparam.inner = 0;
15152 local->numparam.current = 0;
15153 local->it = 0;
15154 return inner;
15155}
15156
15157static void
15158numparam_pop(struct parser_params *p, NODE *prev_inner)
15159{
15160 struct local_vars *local = p->lvtbl;
15161 if (prev_inner) {
15162 /* prefer first one */
15163 local->numparam.inner = prev_inner;
15164 }
15165 else if (local->numparam.current) {
15166 /* current and inner are exclusive */
15167 local->numparam.inner = local->numparam.current;
15168 }
15169 if (p->max_numparam > NO_PARAM) {
15170 /* current and outer are exclusive */
15171 local->numparam.current = local->numparam.outer;
15172 local->numparam.outer = 0;
15173 }
15174 else {
15175 /* no numbered parameter */
15176 local->numparam.current = 0;
15177 }
15178 local->it = 0;
15179}
15180
15181static const struct vtable *
15182dyna_push(struct parser_params *p)
15183{
15184 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15185 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15186 if (p->lvtbl->used) {
15187 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15188 }
15189 return p->lvtbl->args;
15190}
15191
15192static void
15193dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15194{
15195 struct vtable *tmp = *vtblp;
15196 *vtblp = tmp->prev;
15197# if WARN_PAST_SCOPE
15198 if (p->past_scope_enabled) {
15199 tmp->prev = p->lvtbl->past;
15200 p->lvtbl->past = tmp;
15201 return;
15202 }
15203# endif
15204 vtable_free(tmp);
15205}
15206
15207static void
15208dyna_pop_1(struct parser_params *p)
15209{
15210 struct vtable *tmp;
15211
15212 if ((tmp = p->lvtbl->used) != 0) {
15213 warn_unused_var(p, p->lvtbl);
15214 p->lvtbl->used = p->lvtbl->used->prev;
15215 vtable_free(tmp);
15216 }
15217 dyna_pop_vtable(p, &p->lvtbl->args);
15218 dyna_pop_vtable(p, &p->lvtbl->vars);
15219}
15220
15221static void
15222dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15223{
15224 while (p->lvtbl->args != lvargs) {
15225 dyna_pop_1(p);
15226 if (!p->lvtbl->args) {
15227 struct local_vars *local = p->lvtbl->prev;
15228 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15229 p->lvtbl = local;
15230 }
15231 }
15232 dyna_pop_1(p);
15233}
15234
15235static int
15236dyna_in_block(struct parser_params *p)
15237{
15238 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15239}
15240
15241#ifndef RIPPER
15242int
15243dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15244{
15245 struct vtable *vars, *args, *used;
15246 int i;
15247
15248 args = p->lvtbl->args;
15249 vars = p->lvtbl->vars;
15250 used = p->lvtbl->used;
15251
15252 while (!DVARS_TERMINAL_P(vars)) {
15253 if (vtable_included(args, id)) {
15254 return 1;
15255 }
15256 if ((i = vtable_included(vars, id)) != 0) {
15257 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15258 return 1;
15259 }
15260 args = args->prev;
15261 vars = vars->prev;
15262 if (!vidrefp) used = 0;
15263 if (used) used = used->prev;
15264 }
15265
15266 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15267 return rb_dvar_defined(id, p->parent_iseq);
15268 }
15269
15270 return 0;
15271}
15272#endif
15273
15274static int
15275dvar_defined(struct parser_params *p, ID id)
15276{
15277 return dvar_defined_ref(p, id, NULL);
15278}
15279
15280static int
15281dvar_curr(struct parser_params *p, ID id)
15282{
15283 return (vtable_included(p->lvtbl->args, id) ||
15284 vtable_included(p->lvtbl->vars, id));
15285}
15286
15287static void
15288reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15289{
15290 compile_error(p,
15291 "regexp encoding option '%c' differs from source encoding '%s'",
15292 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15293}
15294
15295#ifndef RIPPER
15296static rb_encoding *
15297find_enc(struct parser_params* p, const char *name)
15298{
15299 int idx = rb_enc_find_index(name);
15300 if (idx < 0) {
15301 rb_bug("unknown encoding name: %s", name);
15302 }
15303
15304 return rb_enc_from_index(idx);
15305}
15306
15307static rb_encoding *
15308kcode_to_enc(struct parser_params* p, int kcode)
15309{
15310 rb_encoding *enc;
15311
15312 switch (kcode) {
15313 case ENC_ASCII8BIT:
15314 enc = rb_ascii8bit_encoding();
15315 break;
15316 case ENC_EUC_JP:
15317 enc = find_enc(p, "EUC-JP");
15318 break;
15319 case ENC_Windows_31J:
15320 enc = find_enc(p, "Windows-31J");
15321 break;
15322 case ENC_UTF8:
15323 enc = rb_utf8_encoding();
15324 break;
15325 default:
15326 enc = NULL;
15327 break;
15328 }
15329
15330 return enc;
15331}
15332
15333int
15334rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15335{
15336 int c = RE_OPTION_ENCODING_IDX(options);
15337
15338 if (c) {
15339 int opt, idx;
15340 rb_encoding *enc;
15341
15342 char_to_option_kcode(c, &opt, &idx);
15343 enc = kcode_to_enc(p, idx);
15344 if (enc != rb_parser_str_get_encoding(str) &&
15345 !rb_parser_is_ascii_string(p, str)) {
15346 goto error;
15347 }
15348 rb_parser_string_set_encoding(str, enc);
15349 }
15350 else if (RE_OPTION_ENCODING_NONE(options)) {
15351 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15352 !rb_parser_is_ascii_string(p, str)) {
15353 c = 'n';
15354 goto error;
15355 }
15356 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15357 }
15358 else if (rb_is_usascii_enc(p->enc)) {
15359 if (!rb_parser_is_ascii_string(p, str)) {
15360 /* raise in re.c */
15361 rb_parser_enc_associate(p, str, rb_usascii_encoding());
15362 }
15363 else {
15364 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15365 }
15366 }
15367 return 0;
15368
15369 error:
15370 return c;
15371}
15372#endif
15373
15374static void
15375reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15376{
15377 int c = rb_reg_fragment_setenc(p, str, options);
15378 if (c) reg_fragment_enc_error(p, str, c);
15379}
15380
15381#ifndef RIPPER
15382int
15383reg_fragment_check(struct parser_params* p, rb_parser_string_t *str, int options)
15384{
15385 VALUE err, str2;
15386 reg_fragment_setenc(p, str, options);
15387 /* TODO */
15388 str2 = rb_str_new_parser_string(str);
15389 err = rb_reg_check_preprocess(str2);
15390 if (err != Qnil) {
15391 err = rb_obj_as_string(err);
15392 compile_error(p, "%"PRIsVALUE, err);
15393 return 0;
15394 }
15395 return 1;
15396}
15397#endif
15398
15399#ifndef UNIVERSAL_PARSER
15400typedef struct {
15401 struct parser_params* parser;
15402 rb_encoding *enc;
15403 NODE *succ_block;
15404 const YYLTYPE *loc;
15405 rb_parser_assignable_func assignable;
15406} reg_named_capture_assign_t;
15407
15408static int
15409reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15410 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15411{
15412 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15413 struct parser_params* p = arg->parser;
15414 rb_encoding *enc = arg->enc;
15415 long len = name_end - name;
15416 const char *s = (const char *)name;
15417
15418 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15419}
15420
15421static NODE *
15422reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15423{
15424 reg_named_capture_assign_t arg;
15425
15426 arg.parser = p;
15427 arg.enc = rb_enc_get(regexp);
15428 arg.succ_block = 0;
15429 arg.loc = loc;
15430 arg.assignable = assignable;
15431 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15432
15433 if (!arg.succ_block) return 0;
15434 return RNODE_BLOCK(arg.succ_block)->nd_next;
15435}
15436#endif
15437
15438#ifndef RIPPER
15439NODE *
15440rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15441{
15442 return assignable(p, id, val, loc);
15443}
15444
15445int
15446rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15447 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15448{
15449 ID var;
15450 NODE *node, *succ;
15451
15452 if (!len) return ST_CONTINUE;
15453 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15454 return ST_CONTINUE;
15455
15456 var = intern_cstr(s, len, enc);
15457 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15458 if (!lvar_defined(p, var)) return ST_CONTINUE;
15459 }
15460 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15461 succ = *succ_block;
15462 if (!succ) succ = NEW_ERROR(loc);
15463 succ = block_append(p, succ, node);
15464 *succ_block = succ;
15465 return ST_CONTINUE;
15466}
15467#endif
15468
15469static VALUE
15470parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15471{
15472 VALUE str2;
15473 reg_fragment_setenc(p, str, options);
15474 str2 = rb_str_new_parser_string(str);
15475 return rb_parser_reg_compile(p, str2, options);
15476}
15477
15478#ifndef RIPPER
15479VALUE
15480rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15481{
15482 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15483}
15484#endif
15485
15486static VALUE
15487reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15488{
15489 VALUE re;
15490 VALUE err;
15491
15492 err = rb_errinfo();
15493 re = parser_reg_compile(p, str, options);
15494 if (NIL_P(re)) {
15495 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15496 rb_set_errinfo(err);
15497 compile_error(p, "%"PRIsVALUE, m);
15498 return Qnil;
15499 }
15500 return re;
15501}
15502
15503#ifndef RIPPER
15504void
15505rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15506{
15507 p->do_print = print;
15508 p->do_loop = loop;
15509 p->do_chomp = chomp;
15510 p->do_split = split;
15511}
15512
15513static NODE *
15514parser_append_options(struct parser_params *p, NODE *node)
15515{
15516 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15517 const YYLTYPE *const LOC = &default_location;
15518
15519 if (p->do_print) {
15520 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15521 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15522 LOC);
15523 node = block_append(p, node, print);
15524 }
15525
15526 if (p->do_loop) {
15527 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15528
15529 if (p->do_split) {
15530 ID ifs = rb_intern("$;");
15531 ID fields = rb_intern("$F");
15532 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15533 NODE *split = NEW_GASGN(fields,
15534 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15535 rb_intern("split"), args, LOC),
15536 LOC);
15537 node = block_append(p, split, node);
15538 }
15539 if (p->do_chomp) {
15540 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15541 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15542 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15543 }
15544
15545 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15546 }
15547
15548 return node;
15549}
15550
15551void
15552rb_init_parse(void)
15553{
15554 /* just to suppress unused-function warnings */
15555 (void)nodetype;
15556 (void)nodeline;
15557}
15558
15559ID
15560internal_id(struct parser_params *p)
15561{
15562 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15563}
15564#endif /* !RIPPER */
15565
15566static void
15567parser_initialize(struct parser_params *p)
15568{
15569 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15570 p->command_start = TRUE;
15571 p->ruby_sourcefile_string = Qnil;
15572 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15573 string_buffer_init(p);
15574 p->node_id = 0;
15575 p->delayed.token = NULL;
15576 p->frozen_string_literal = -1; /* not specified */
15577#ifndef RIPPER
15578 p->error_buffer = Qfalse;
15579 p->end_expect_token_locations = NULL;
15580 p->token_id = 0;
15581 p->tokens = NULL;
15582#else
15583 p->result = Qnil;
15584 p->parsing_thread = Qnil;
15585 p->s_value = Qnil;
15586 p->s_lvalue = Qnil;
15587 p->s_value_stack = rb_ary_new();
15588#endif
15589 p->debug_buffer = Qnil;
15590 p->debug_output = rb_ractor_stdout();
15591 p->enc = rb_utf8_encoding();
15592 p->exits = 0;
15593}
15594
15595#ifdef RIPPER
15596#define rb_ruby_parser_mark ripper_parser_mark
15597#define rb_ruby_parser_free ripper_parser_free
15598#define rb_ruby_parser_memsize ripper_parser_memsize
15599#endif
15600
15601void
15602rb_ruby_parser_mark(void *ptr)
15603{
15604 struct parser_params *p = (struct parser_params*)ptr;
15605
15606 rb_gc_mark(p->ruby_sourcefile_string);
15607#ifndef RIPPER
15608 rb_gc_mark(p->error_buffer);
15609#else
15610 rb_gc_mark(p->value);
15611 rb_gc_mark(p->result);
15612 rb_gc_mark(p->parsing_thread);
15613 rb_gc_mark(p->s_value);
15614 rb_gc_mark(p->s_lvalue);
15615 rb_gc_mark(p->s_value_stack);
15616#endif
15617 rb_gc_mark(p->debug_buffer);
15618 rb_gc_mark(p->debug_output);
15619}
15620
15621void
15622rb_ruby_parser_free(void *ptr)
15623{
15624 struct parser_params *p = (struct parser_params*)ptr;
15625 struct local_vars *local, *prev;
15626
15627 if (p->ast) {
15628 rb_ast_free(p->ast);
15629 }
15630
15631 if (p->warn_duplicate_keys_table) {
15632 st_free_table(p->warn_duplicate_keys_table);
15633 }
15634
15635#ifndef RIPPER
15636 if (p->tokens) {
15637 rb_parser_ary_free(p, p->tokens);
15638 }
15639#endif
15640
15641 if (p->tokenbuf) {
15642 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15643 }
15644
15645 for (local = p->lvtbl; local; local = prev) {
15646 prev = local->prev;
15647 local_free(p, local);
15648 }
15649
15650 {
15651 token_info *ptinfo;
15652 while ((ptinfo = p->token_info) != 0) {
15653 p->token_info = ptinfo->next;
15654 xfree(ptinfo);
15655 }
15656 }
15657 string_buffer_free(p);
15658
15659 if (p->pvtbl) {
15660 st_free_table(p->pvtbl);
15661 }
15662
15663 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15664 st_free_table(p->case_labels);
15665 }
15666
15667 xfree(p->lex.strterm);
15668 p->lex.strterm = 0;
15669
15670 xfree(ptr);
15671}
15672
15673size_t
15674rb_ruby_parser_memsize(const void *ptr)
15675{
15676 struct parser_params *p = (struct parser_params*)ptr;
15677 struct local_vars *local;
15678 size_t size = sizeof(*p);
15679
15680 size += p->toksiz;
15681 for (local = p->lvtbl; local; local = local->prev) {
15682 size += sizeof(*local);
15683 if (local->vars) size += local->vars->capa * sizeof(ID);
15684 }
15685 return size;
15686}
15687
15688#ifndef RIPPER
15689#undef rb_reserved_word
15690
15691const struct kwtable *
15692rb_reserved_word(const char *str, unsigned int len)
15693{
15694 return reserved_word(str, len);
15695}
15696
15697#ifdef UNIVERSAL_PARSER
15698rb_parser_t *
15699rb_ruby_parser_allocate(const rb_parser_config_t *config)
15700{
15701 /* parser_initialize expects fields to be set to 0 */
15702 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15703 p->config = config;
15704 return p;
15705}
15706
15707rb_parser_t *
15708rb_ruby_parser_new(const rb_parser_config_t *config)
15709{
15710 /* parser_initialize expects fields to be set to 0 */
15711 rb_parser_t *p = rb_ruby_parser_allocate(config);
15712 parser_initialize(p);
15713 return p;
15714}
15715#else
15716rb_parser_t *
15717rb_ruby_parser_allocate(void)
15718{
15719 /* parser_initialize expects fields to be set to 0 */
15720 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15721 return p;
15722}
15723
15724rb_parser_t *
15725rb_ruby_parser_new(void)
15726{
15727 /* parser_initialize expects fields to be set to 0 */
15728 rb_parser_t *p = rb_ruby_parser_allocate();
15729 parser_initialize(p);
15730 return p;
15731}
15732#endif
15733
15734rb_parser_t *
15735rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15736{
15737 p->error_buffer = main ? Qfalse : Qnil;
15738 p->parent_iseq = base;
15739 return p;
15740}
15741
15742void
15743rb_ruby_parser_set_script_lines(rb_parser_t *p)
15744{
15745 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15746}
15747
15748void
15749rb_ruby_parser_error_tolerant(rb_parser_t *p)
15750{
15751 p->error_tolerant = 1;
15752}
15753
15754void
15755rb_ruby_parser_keep_tokens(rb_parser_t *p)
15756{
15757 p->keep_tokens = 1;
15758 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15759}
15760
15761rb_encoding *
15762rb_ruby_parser_encoding(rb_parser_t *p)
15763{
15764 return p->enc;
15765}
15766
15767int
15768rb_ruby_parser_end_seen_p(rb_parser_t *p)
15769{
15770 return p->ruby__end__seen;
15771}
15772
15773int
15774rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15775{
15776 p->debug = flag;
15777 return flag;
15778}
15779#endif /* !RIPPER */
15780
15781#ifdef RIPPER
15782int
15783rb_ruby_parser_get_yydebug(rb_parser_t *p)
15784{
15785 return p->debug;
15786}
15787
15788void
15789rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15790{
15791 p->value = value;
15792}
15793
15794int
15795rb_ruby_parser_error_p(rb_parser_t *p)
15796{
15797 return p->error_p;
15798}
15799
15800VALUE
15801rb_ruby_parser_debug_output(rb_parser_t *p)
15802{
15803 return p->debug_output;
15804}
15805
15806void
15807rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15808{
15809 p->debug_output = output;
15810}
15811
15812VALUE
15813rb_ruby_parser_parsing_thread(rb_parser_t *p)
15814{
15815 return p->parsing_thread;
15816}
15817
15818void
15819rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15820{
15821 p->parsing_thread = parsing_thread;
15822}
15823
15824void
15825rb_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)
15826{
15827 p->lex.gets = gets;
15828 p->lex.input = input;
15829 p->eofp = 0;
15830 p->ruby_sourcefile_string = sourcefile_string;
15831 p->ruby_sourcefile = sourcefile;
15832 p->ruby_sourceline = sourceline;
15833}
15834
15835VALUE
15836rb_ruby_parser_result(rb_parser_t *p)
15837{
15838 return p->result;
15839}
15840
15841rb_encoding *
15842rb_ruby_parser_enc(rb_parser_t *p)
15843{
15844 return p->enc;
15845}
15846
15847VALUE
15848rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15849{
15850 return p->ruby_sourcefile_string;
15851}
15852
15853int
15854rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15855{
15856 return p->ruby_sourceline;
15857}
15858
15859int
15860rb_ruby_parser_lex_state(rb_parser_t *p)
15861{
15862 return p->lex.state;
15863}
15864
15865void
15866rb_ruby_ripper_parse0(rb_parser_t *p)
15867{
15868 parser_prepare(p);
15869 p->ast = rb_ast_new();
15870 ripper_yyparse((void*)p);
15871 rb_ast_free(p->ast);
15872 p->ast = 0;
15873 p->eval_tree = 0;
15874 p->eval_tree_begin = 0;
15875}
15876
15877int
15878rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15879{
15880 return dedent_string(p, string, width);
15881}
15882
15883int
15884rb_ruby_ripper_initialized_p(rb_parser_t *p)
15885{
15886 return p->lex.input != 0;
15887}
15888
15889void
15890rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15891{
15892 parser_initialize(p);
15893}
15894
15895long
15896rb_ruby_ripper_column(rb_parser_t *p)
15897{
15898 return p->lex.ptok - p->lex.pbeg;
15899}
15900
15901long
15902rb_ruby_ripper_token_len(rb_parser_t *p)
15903{
15904 return p->lex.pcur - p->lex.ptok;
15905}
15906
15907rb_parser_string_t *
15908rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15909{
15910 return p->lex.lastline;
15911}
15912
15913VALUE
15914rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15915{
15916 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15917}
15918
15919#ifdef UNIVERSAL_PARSER
15920rb_parser_t *
15921rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15922{
15923 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15924 p->config = config;
15925 return p;
15926}
15927#endif
15928
15929struct parser_params*
15930rb_ruby_ripper_parser_allocate(void)
15931{
15932 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15933}
15934#endif /* RIPPER */
15935
15936#ifndef RIPPER
15937void
15938rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15939{
15940 va_list ap;
15941 VALUE mesg = p->debug_buffer;
15942
15943 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15944 va_start(ap, fmt);
15945 rb_str_vcatf(mesg, fmt, ap);
15946 va_end(ap);
15947 if (char_at_end(p, mesg, 0) == '\n') {
15948 rb_io_write(p->debug_output, mesg);
15949 p->debug_buffer = Qnil;
15950 }
15951}
15952
15953static void
15954parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15955{
15956 va_list ap;
15957 int lineno, column;
15958
15959 if (loc) {
15960 lineno = loc->end_pos.lineno;
15961 column = loc->end_pos.column;
15962 }
15963 else {
15964 lineno = p->ruby_sourceline;
15965 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15966 }
15967
15968 rb_io_flush(p->debug_output);
15969 p->error_p = 1;
15970 va_start(ap, fmt);
15971 p->error_buffer =
15972 rb_syntax_error_append(p->error_buffer,
15973 p->ruby_sourcefile_string,
15974 lineno, column,
15975 p->enc, fmt, ap);
15976 va_end(ap);
15977}
15978
15979static size_t
15980count_char(const char *str, int c)
15981{
15982 int n = 0;
15983 while (str[n] == c) ++n;
15984 return n;
15985}
15986
15987/*
15988 * strip enclosing double-quotes, same as the default yytnamerr except
15989 * for that single-quotes matching back-quotes do not stop stripping.
15990 *
15991 * "\"`class' keyword\"" => "`class' keyword"
15992 */
15993size_t
15994rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
15995{
15996 if (*yystr == '"') {
15997 size_t yyn = 0, bquote = 0;
15998 const char *yyp = yystr;
15999
16000 while (*++yyp) {
16001 switch (*yyp) {
16002 case '\'':
16003 if (!bquote) {
16004 bquote = count_char(yyp+1, '\'') + 1;
16005 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16006 yyn += bquote;
16007 yyp += bquote - 1;
16008 break;
16009 }
16010 else {
16011 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16012 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16013 yyn += bquote;
16014 yyp += bquote - 1;
16015 bquote = 0;
16016 break;
16017 }
16018 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16019 if (yyres) memcpy(yyres + yyn, yyp, 3);
16020 yyn += 3;
16021 yyp += 2;
16022 break;
16023 }
16024 goto do_not_strip_quotes;
16025 }
16026
16027 case ',':
16028 goto do_not_strip_quotes;
16029
16030 case '\\':
16031 if (*++yyp != '\\')
16032 goto do_not_strip_quotes;
16033 /* Fall through. */
16034 default:
16035 if (yyres)
16036 yyres[yyn] = *yyp;
16037 yyn++;
16038 break;
16039
16040 case '"':
16041 case '\0':
16042 if (yyres)
16043 yyres[yyn] = '\0';
16044 return yyn;
16045 }
16046 }
16047 do_not_strip_quotes: ;
16048 }
16049
16050 if (!yyres) return strlen(yystr);
16051
16052 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16053}
16054#endif
16055
16056#ifdef RIPPER
16057#define validate(x) (void)(x)
16058
16059static VALUE
16060ripper_dispatch0(struct parser_params *p, ID mid)
16061{
16062 return rb_funcall(p->value, mid, 0);
16063}
16064
16065static VALUE
16066ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16067{
16068 validate(a);
16069 return rb_funcall(p->value, mid, 1, a);
16070}
16071
16072static VALUE
16073ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16074{
16075 validate(a);
16076 validate(b);
16077 return rb_funcall(p->value, mid, 2, a, b);
16078}
16079
16080static VALUE
16081ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16082{
16083 validate(a);
16084 validate(b);
16085 validate(c);
16086 return rb_funcall(p->value, mid, 3, a, b, c);
16087}
16088
16089static VALUE
16090ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16091{
16092 validate(a);
16093 validate(b);
16094 validate(c);
16095 validate(d);
16096 return rb_funcall(p->value, mid, 4, a, b, c, d);
16097}
16098
16099static VALUE
16100ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16101{
16102 validate(a);
16103 validate(b);
16104 validate(c);
16105 validate(d);
16106 validate(e);
16107 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16108}
16109
16110static VALUE
16111ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16112{
16113 validate(a);
16114 validate(b);
16115 validate(c);
16116 validate(d);
16117 validate(e);
16118 validate(f);
16119 validate(g);
16120 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16121}
16122
16123void
16124ripper_error(struct parser_params *p)
16125{
16126 p->error_p = TRUE;
16127}
16128
16129VALUE
16130ripper_value(struct parser_params *p)
16131{
16132 (void)yystpcpy; /* may not used in newer bison */
16133
16134 return p->value;
16135}
16136
16137#endif /* RIPPER */
16138/*
16139 * Local variables:
16140 * mode: c
16141 * c-file-style: "ruby"
16142 * End:
16143 */